blob: 80dbaab91879c7dec478244d9eec99377146021f [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 FOURNIERe47e4e22014-04-28 11:18:57 +020044int (*pat_parse_fcts[PAT_MATCH_NUM])(const char *, struct pattern *, int, 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 FOURNIERe47e4e22014-04-28 11:18:57 +0200197int pat_parse_nothing(const char *text, struct pattern *pattern, int mflags, 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 FOURNIERe47e4e22014-04-28 11:18:57 +0200203int pat_parse_str(const char *text, struct pattern *pattern, int mflags, 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 FOURNIERe47e4e22014-04-28 11:18:57 +0200212int pat_parse_bin(const char *text, struct pattern *pattern, int mflags, 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 FOURNIERe47e4e22014-04-28 11:18:57 +0200224int pat_parse_reg(const char *text, struct pattern *pattern, int mflags, 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 FOURNIERe47e4e22014-04-28 11:18:57 +0200255int pat_parse_int(const char *text, struct pattern *pattern, int mflags, 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 FOURNIERe47e4e22014-04-28 11:18:57 +0200340int pat_parse_dotted_ver(const char *text, struct pattern *pattern, int mflags, 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 FOURNIERe47e4e22014-04-28 11:18:57 +0200407int pat_parse_ip(const char *text, struct pattern *pattern, int mflags, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100408{
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200409 if (str2net(text, !(mflags & PAT_MF_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;
Thierry FOURNIERe5978bf2014-03-17 19:53:10 +0100441 static_pattern.type = 0;
442 static_pattern.ptr.str = NULL;
443 }
444 return &static_pattern;
445 }
446 else
447 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100448}
449
450
451/* NB: For two strings to be identical, it is required that their lengths match */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100452struct pattern *pat_match_str(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100453{
454 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100455 struct ebmb_node *node;
456 char prev;
457 struct pattern_tree *elt;
458 struct pattern_list *lst;
459 struct pattern *pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100460
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100461 /* Lookup a string in the expression's pattern tree. */
462 if (!eb_is_empty(&expr->pattern_tree)) {
463 /* we may have to force a trailing zero on the test pattern */
464 prev = smp->data.str.str[smp->data.str.len];
465 if (prev)
466 smp->data.str.str[smp->data.str.len] = '\0';
467 node = ebst_lookup(&expr->pattern_tree, smp->data.str.str);
468 if (prev)
469 smp->data.str.str[smp->data.str.len] = prev;
470
471 if (node) {
472 if (fill) {
473 elt = ebmb_entry(node, struct pattern_tree, node);
474 static_pattern.smp = elt->smp;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +0100475 static_pattern.ref = elt->ref;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200476 static_pattern.sflags = PAT_SF_TREE;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100477 static_pattern.type = SMP_T_STR;
478 static_pattern.ptr.str = (char *)elt->node.key;
479 }
480 return &static_pattern;
481 }
482 }
483
484 /* look in the list */
485 list_for_each_entry(lst, &expr->patterns, list) {
486 pattern = &lst->pat;
487
488 if (pattern->len != smp->data.str.len)
489 continue;
490
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200491 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100492 if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) == 0) ||
493 (!icase && strncmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) == 0))
494 return pattern;
495 }
496
497 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100498}
499
500/* NB: For two binaries buf to be identical, it is required that their lengths match */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100501struct pattern *pat_match_bin(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100502{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100503 struct pattern_list *lst;
504 struct pattern *pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100505
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100506 /* Look in the list. */
507 list_for_each_entry(lst, &expr->patterns, list) {
508 pattern = &lst->pat;
509
510 if (pattern->len != smp->data.str.len)
511 continue;
512
513 if (memcmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) == 0)
514 return pattern;
515 }
516
517 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100518}
519
520/* Executes a regex. It temporarily changes the data to add a trailing zero,
521 * and restores the previous character when leaving.
522 */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100523struct pattern *pat_match_reg(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100524{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100525 struct pattern_list *lst;
526 struct pattern *pattern;
527
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100528 /* look in the list */
529 list_for_each_entry(lst, &expr->patterns, list) {
530 pattern = &lst->pat;
531
532 if (regex_exec(pattern->ptr.reg, smp->data.str.str, smp->data.str.len) == 0)
533 return pattern;
534 }
535 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100536}
537
538/* Checks that the pattern matches the beginning of the tested string. */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100539struct pattern *pat_match_beg(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100540{
541 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100542 struct pattern_list *lst;
543 struct pattern *pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100544
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100545 list_for_each_entry(lst, &expr->patterns, list) {
546 pattern = &lst->pat;
547
548 if (pattern->len > smp->data.str.len)
549 continue;
550
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200551 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100552 if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str, pattern->len) != 0) ||
553 (!icase && strncmp(pattern->ptr.str, smp->data.str.str, pattern->len) != 0))
554 continue;
555
556 return pattern;
557 }
558 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100559}
560
561/* Checks that the pattern matches the end of the tested string. */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100562struct pattern *pat_match_end(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100563{
564 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100565 struct pattern_list *lst;
566 struct pattern *pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100567
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100568 list_for_each_entry(lst, &expr->patterns, list) {
569 pattern = &lst->pat;
570
571 if (pattern->len > smp->data.str.len)
572 continue;
573
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200574 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100575 if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str + smp->data.str.len - pattern->len, pattern->len) != 0) ||
576 (!icase && strncmp(pattern->ptr.str, smp->data.str.str + smp->data.str.len - pattern->len, pattern->len) != 0))
577 continue;
578
579 return pattern;
580 }
581 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100582}
583
584/* Checks that the pattern is included inside the tested string.
585 * NB: Suboptimal, should be rewritten using a Boyer-Moore method.
586 */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100587struct pattern *pat_match_sub(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100588{
589 int icase;
590 char *end;
591 char *c;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100592 struct pattern_list *lst;
593 struct pattern *pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100594
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100595 list_for_each_entry(lst, &expr->patterns, list) {
596 pattern = &lst->pat;
597
598 if (pattern->len > smp->data.str.len)
599 continue;
600
601 end = smp->data.str.str + smp->data.str.len - pattern->len;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200602 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100603 if (icase) {
604 for (c = smp->data.str.str; c <= end; c++) {
605 if (tolower(*c) != tolower(*pattern->ptr.str))
606 continue;
607 if (strncasecmp(pattern->ptr.str, c, pattern->len) == 0)
608 return pattern;
609 }
610 } else {
611 for (c = smp->data.str.str; c <= end; c++) {
612 if (*c != *pattern->ptr.str)
613 continue;
614 if (strncmp(pattern->ptr.str, c, pattern->len) == 0)
615 return pattern;
616 }
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100617 }
618 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100619 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100620}
621
622/* This one is used by other real functions. It checks that the pattern is
623 * included inside the tested string, but enclosed between the specified
624 * delimiters or at the beginning or end of the string. The delimiters are
625 * provided as an unsigned int made by make_4delim() and match up to 4 different
626 * delimiters. Delimiters are stripped at the beginning and end of the pattern.
627 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200628static int match_word(struct sample *smp, struct pattern *pattern, int mflags, unsigned int delimiters)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100629{
630 int may_match, icase;
631 char *c, *end;
632 char *ps;
633 int pl;
634
635 pl = pattern->len;
636 ps = pattern->ptr.str;
637
638 while (pl > 0 && is_delimiter(*ps, delimiters)) {
639 pl--;
640 ps++;
641 }
642
643 while (pl > 0 && is_delimiter(ps[pl - 1], delimiters))
644 pl--;
645
646 if (pl > smp->data.str.len)
647 return PAT_NOMATCH;
648
649 may_match = 1;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200650 icase = mflags & PAT_MF_IGNORE_CASE;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100651 end = smp->data.str.str + smp->data.str.len - pl;
652 for (c = smp->data.str.str; c <= end; c++) {
653 if (is_delimiter(*c, delimiters)) {
654 may_match = 1;
655 continue;
656 }
657
658 if (!may_match)
659 continue;
660
661 if (icase) {
662 if ((tolower(*c) == tolower(*ps)) &&
663 (strncasecmp(ps, c, pl) == 0) &&
664 (c == end || is_delimiter(c[pl], delimiters)))
665 return PAT_MATCH;
666 } else {
667 if ((*c == *ps) &&
668 (strncmp(ps, c, pl) == 0) &&
669 (c == end || is_delimiter(c[pl], delimiters)))
670 return PAT_MATCH;
671 }
672 may_match = 0;
673 }
674 return PAT_NOMATCH;
675}
676
677/* Checks that the pattern is included inside the tested string, but enclosed
678 * between the delimiters '?' or '/' or at the beginning or end of the string.
679 * Delimiters at the beginning or end of the pattern are ignored.
680 */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100681struct pattern *pat_match_dir(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100682{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100683 struct pattern_list *lst;
684 struct pattern *pattern;
685
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100686 list_for_each_entry(lst, &expr->patterns, list) {
687 pattern = &lst->pat;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200688 if (match_word(smp, pattern, expr->mflags, make_4delim('/', '?', '?', '?')))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100689 return pattern;
690 }
691 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100692}
693
694/* Checks that the pattern is included inside the tested string, but enclosed
695 * between the delmiters '/', '?', '.' or ":" or at the beginning or end of
696 * the string. Delimiters at the beginning or end of the pattern are ignored.
697 */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100698struct pattern *pat_match_dom(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100699{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100700 struct pattern_list *lst;
701 struct pattern *pattern;
702
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100703 list_for_each_entry(lst, &expr->patterns, list) {
704 pattern = &lst->pat;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200705 if (match_word(smp, pattern, expr->mflags, make_4delim('/', '?', '.', ':')))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100706 return pattern;
707 }
708 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100709}
710
711/* Checks that the integer in <test> is included between min and max */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100712struct pattern *pat_match_int(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100713{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100714 struct pattern_list *lst;
715 struct pattern *pattern;
716
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100717 list_for_each_entry(lst, &expr->patterns, list) {
718 pattern = &lst->pat;
719 if ((!pattern->val.range.min_set || pattern->val.range.min <= smp->data.uint) &&
720 (!pattern->val.range.max_set || smp->data.uint <= pattern->val.range.max))
721 return pattern;
722 }
723 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100724}
725
726/* Checks that the length of the pattern in <test> is included between min and max */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100727struct pattern *pat_match_len(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100728{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100729 struct pattern_list *lst;
730 struct pattern *pattern;
731
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100732 list_for_each_entry(lst, &expr->patterns, list) {
733 pattern = &lst->pat;
734 if ((!pattern->val.range.min_set || pattern->val.range.min <= smp->data.str.len) &&
735 (!pattern->val.range.max_set || smp->data.str.len <= pattern->val.range.max))
736 return pattern;
737 }
738 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100739}
740
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100741struct pattern *pat_match_ip(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100742{
743 unsigned int v4; /* in network byte order */
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100744 struct in6_addr tmp6;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100745 struct in_addr *s;
746 struct ebmb_node *node;
747 struct pattern_tree *elt;
748 struct pattern_list *lst;
749 struct pattern *pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100750
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100751 /* The input sample is IPv4. Try to match in the trees. */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100752 if (smp->type == SMP_T_IPV4) {
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100753 /* Lookup an IPv4 address in the expression's pattern tree using
754 * the longest match method.
755 */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100756 s = &smp->data.ipv4;
757 node = ebmb_lookup_longest(&expr->pattern_tree, &s->s_addr);
758 if (node) {
759 if (fill) {
760 elt = ebmb_entry(node, struct pattern_tree, node);
761 static_pattern.smp = elt->smp;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +0100762 static_pattern.ref = elt->ref;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200763 static_pattern.sflags = PAT_SF_TREE;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100764 static_pattern.type = SMP_T_IPV4;
765 memcpy(&static_pattern.val.ipv4.addr.s_addr, elt->node.key, 4);
766 if (!cidr2dotted(elt->node.node.pfx, &static_pattern.val.ipv4.mask))
767 return NULL;
768 }
769 return &static_pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100770 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100771
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100772 /* The IPv4 sample dont match the IPv4 tree. Convert the IPv4
773 * sample address to IPv6 with the mapping method using the ::ffff:
774 * prefix, and try to lookup in the IPv6 tree.
775 */
776 memset(&tmp6, 0, 10);
777 *(uint16_t*)&tmp6.s6_addr[10] = htons(0xffff);
778 *(uint32_t*)&tmp6.s6_addr[12] = smp->data.ipv4.s_addr;
779 node = ebmb_lookup_longest(&expr->pattern_tree_2, &tmp6);
780 if (node) {
781 if (fill) {
782 elt = ebmb_entry(node, struct pattern_tree, node);
783 static_pattern.smp = elt->smp;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +0100784 static_pattern.ref = elt->ref;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200785 static_pattern.sflags = PAT_SF_TREE;
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100786 static_pattern.type = SMP_T_IPV6;
787 memcpy(&static_pattern.val.ipv6.addr, elt->node.key, 16);
788 static_pattern.val.ipv6.mask = elt->node.node.pfx;
789 }
790 return &static_pattern;
791 }
792 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100793
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100794 /* The input sample is IPv6. Try to match in the trees. */
795 if (smp->type == SMP_T_IPV6) {
796 /* Lookup an IPv6 address in the expression's pattern tree using
797 * the longest match method.
798 */
799 node = ebmb_lookup_longest(&expr->pattern_tree_2, &smp->data.ipv6);
800 if (node) {
801 if (fill) {
802 elt = ebmb_entry(node, struct pattern_tree, node);
803 static_pattern.smp = elt->smp;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +0100804 static_pattern.ref = elt->ref;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200805 static_pattern.sflags = PAT_SF_TREE;
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100806 static_pattern.type = SMP_T_IPV6;
807 memcpy(&static_pattern.val.ipv6.addr, elt->node.key, 16);
808 static_pattern.val.ipv6.mask = elt->node.node.pfx;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100809 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100810 return &static_pattern;
811 }
812
813 /* Try to convert 6 to 4 when the start of the ipv6 address match the
814 * following forms :
815 * - ::ffff:ip:v4 (ipv4 mapped)
816 * - ::0000:ip:v4 (old ipv4 mapped)
817 * - 2002:ip:v4:: (6to4)
818 */
819 if ((*(uint32_t*)&smp->data.ipv6.s6_addr[0] == 0 &&
820 *(uint32_t*)&smp->data.ipv6.s6_addr[4] == 0 &&
821 (*(uint32_t*)&smp->data.ipv6.s6_addr[8] == 0 ||
822 *(uint32_t*)&smp->data.ipv6.s6_addr[8] == htonl(0xFFFF))) ||
823 *(uint16_t*)&smp->data.ipv6.s6_addr[0] == htons(0x2002)) {
824 if (*(uint32_t*)&smp->data.ipv6.s6_addr[0] == 0)
825 v4 = *(uint32_t*)&smp->data.ipv6.s6_addr[12];
826 else
827 v4 = htonl((ntohs(*(uint16_t*)&smp->data.ipv6.s6_addr[2]) << 16) +
828 ntohs(*(uint16_t*)&smp->data.ipv6.s6_addr[4]));
829
830 /* Lookup an IPv4 address in the expression's pattern tree using the longest
831 * match method.
832 */
833 node = ebmb_lookup_longest(&expr->pattern_tree, &v4);
834 if (node) {
835 if (fill) {
836 elt = ebmb_entry(node, struct pattern_tree, node);
837 static_pattern.smp = elt->smp;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +0100838 static_pattern.ref = elt->ref;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200839 static_pattern.sflags = PAT_SF_TREE;
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100840 static_pattern.type = SMP_T_IPV4;
841 memcpy(&static_pattern.val.ipv4.addr.s_addr, elt->node.key, 4);
842 if (!cidr2dotted(elt->node.node.pfx, &static_pattern.val.ipv4.mask))
843 return NULL;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100844 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100845 return &static_pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100846 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100847 }
848 }
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100849
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100850 /* Lookup in the list. the list contain only IPv4 patterns */
851 list_for_each_entry(lst, &expr->patterns, list) {
852 pattern = &lst->pat;
853
854 /* The input sample is IPv4, use it as is. */
855 if (smp->type == SMP_T_IPV4) {
856 v4 = smp->data.ipv4.s_addr;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100857 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100858 else if (smp->type == SMP_T_IPV6) {
859 /* v4 match on a V6 sample. We want to check at least for
860 * the following forms :
861 * - ::ffff:ip:v4 (ipv4 mapped)
862 * - ::0000:ip:v4 (old ipv4 mapped)
863 * - 2002:ip:v4:: (6to4)
864 */
865 if (*(uint32_t*)&smp->data.ipv6.s6_addr[0] == 0 &&
866 *(uint32_t*)&smp->data.ipv6.s6_addr[4] == 0 &&
867 (*(uint32_t*)&smp->data.ipv6.s6_addr[8] == 0 ||
868 *(uint32_t*)&smp->data.ipv6.s6_addr[8] == htonl(0xFFFF))) {
869 v4 = *(uint32_t*)&smp->data.ipv6.s6_addr[12];
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100870 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100871 else if (*(uint16_t*)&smp->data.ipv6.s6_addr[0] == htons(0x2002)) {
872 v4 = htonl((ntohs(*(uint16_t*)&smp->data.ipv6.s6_addr[2]) << 16) +
873 ntohs(*(uint16_t*)&smp->data.ipv6.s6_addr[4]));
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100874 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100875 else
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100876 continue;
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100877 }
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100878
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100879 /* Check if the input sample match the current pattern. */
880 if (((v4 ^ pattern->val.ipv4.addr.s_addr) & pattern->val.ipv4.mask.s_addr) == 0)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100881 return pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100882 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100883 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100884}
885
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100886void free_pattern_tree(struct eb_root *root)
887{
888 struct eb_node *node, *next;
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +0100889 struct pattern_tree *elt;
Thierry FOURNIER3ce88c72013-12-09 11:29:46 +0100890
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100891 node = eb_first(root);
892 while (node) {
893 next = eb_next(node);
894 eb_delete(node);
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +0100895 elt = container_of(node, struct pattern_tree, node);
Thierry FOURNIERc64de3f2013-12-10 15:08:39 +0100896 free(elt->smp);
Thierry FOURNIER3ce88c72013-12-09 11:29:46 +0100897 free(elt);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100898 node = next;
899 }
900}
901
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +0100902void pat_prune_val(struct pattern_expr *expr)
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +0100903{
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +0100904 struct pattern_list *pat, *tmp;
905
906 list_for_each_entry_safe(pat, tmp, &expr->patterns, list) {
907 free(pat->pat.smp);
908 free(pat);
909 }
910
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +0100911 free_pattern_tree(&expr->pattern_tree);
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100912 free_pattern_tree(&expr->pattern_tree_2);
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +0100913 LIST_INIT(&expr->patterns);
914}
915
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +0100916void pat_prune_ptr(struct pattern_expr *expr)
917{
918 struct pattern_list *pat, *tmp;
919
920 list_for_each_entry_safe(pat, tmp, &expr->patterns, list) {
921 free(pat->pat.ptr.ptr);
922 free(pat->pat.smp);
923 free(pat);
924 }
925
926 free_pattern_tree(&expr->pattern_tree);
927 free_pattern_tree(&expr->pattern_tree_2);
928 LIST_INIT(&expr->patterns);
929}
930
931void pat_prune_reg(struct pattern_expr *expr)
932{
933 struct pattern_list *pat, *tmp;
934
935 list_for_each_entry_safe(pat, tmp, &expr->patterns, list) {
936 regex_free(pat->pat.ptr.ptr);
937 free(pat->pat.smp);
938 free(pat);
939 }
940
941 free_pattern_tree(&expr->pattern_tree);
942 free_pattern_tree(&expr->pattern_tree_2);
943 LIST_INIT(&expr->patterns);
944}
945
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100946/*
947 *
948 * The following functions are used for the pattern indexation
949 *
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100950 */
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100951
952int pat_idx_list_val(struct pattern_expr *expr, struct pattern *pat, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100953{
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100954 struct pattern_list *patl;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100955
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100956 /* allocate pattern */
957 patl = calloc(1, sizeof(*patl));
958 if (!patl) {
959 memprintf(err, "out of memory while indexing pattern");
Thierry FOURNIER972028f2014-01-23 17:53:31 +0100960 return 0;
961 }
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100962
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100963 /* duplicate pattern */
964 memcpy(&patl->pat, pat, sizeof(*pat));
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100965
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100966 /* chain pattern in the expression */
967 LIST_ADDQ(&expr->patterns, &patl->list);
968
969 /* that's ok */
970 return 1;
971}
972
973int pat_idx_list_ptr(struct pattern_expr *expr, struct pattern *pat, char **err)
974{
975 struct pattern_list *patl;
976
977 /* allocate pattern */
978 patl = calloc(1, sizeof(*patl));
979 if (!patl)
Thierry FOURNIER972028f2014-01-23 17:53:31 +0100980 return 0;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100981
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100982 /* duplicate pattern */
983 memcpy(&patl->pat, pat, sizeof(*pat));
984 patl->pat.ptr.ptr = malloc(patl->pat.len);
985 if (!patl->pat.ptr.ptr) {
986 free(patl);
987 memprintf(err, "out of memory while indexing pattern");
988 return 0;
989 }
990 memcpy(patl->pat.ptr.ptr, pat->ptr.ptr, pat->len);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100991
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100992 /* chain pattern in the expression */
993 LIST_ADDQ(&expr->patterns, &patl->list);
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100994
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100995 /* that's ok */
996 return 1;
997}
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100998
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100999int pat_idx_list_str(struct pattern_expr *expr, struct pattern *pat, char **err)
1000{
1001 struct pattern_list *patl;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001002
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001003 /* allocate pattern */
1004 patl = calloc(1, sizeof(*patl));
1005 if (!patl) {
1006 memprintf(err, "out of memory while indexing pattern");
1007 return 0;
1008 }
1009
1010 /* duplicate pattern */
1011 memcpy(&patl->pat, pat, sizeof(*pat));
1012 patl->pat.ptr.str = malloc(patl->pat.len + 1);
1013 if (!patl->pat.ptr.str) {
1014 free(patl);
1015 memprintf(err, "out of memory while indexing pattern");
1016 return 0;
1017 }
1018 memcpy(patl->pat.ptr.ptr, pat->ptr.ptr, pat->len);
1019 patl->pat.ptr.str[patl->pat.len] = '\0';
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001020
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001021 /* chain pattern in the expression */
1022 LIST_ADDQ(&expr->patterns, &patl->list);
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001023
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001024 /* that's ok */
1025 return 1;
1026}
1027
1028int pat_idx_list_reg(struct pattern_expr *expr, struct pattern *pat, char **err)
1029{
1030 struct pattern_list *patl;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001031
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001032 /* allocate pattern */
1033 patl = calloc(1, sizeof(*patl));
1034 if (!patl) {
1035 memprintf(err, "out of memory while indexing pattern");
1036 return 0;
Thierry FOURNIER972028f2014-01-23 17:53:31 +01001037 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001038
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001039 /* duplicate pattern */
1040 memcpy(&patl->pat, pat, sizeof(*pat));
1041
1042 /* allocate regex */
1043 patl->pat.ptr.reg = calloc(1, sizeof(*patl->pat.ptr.reg));
1044 if (!patl->pat.ptr.reg) {
1045 free(patl);
1046 memprintf(err, "out of memory while indexing pattern");
1047 return 0;
1048 }
1049
1050 /* compile regex */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02001051 if (!regex_comp(pat->ptr.str, patl->pat.ptr.reg, !(expr->mflags & PAT_MF_IGNORE_CASE), 0, err)) {
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001052 free(patl);
1053 free(patl->pat.ptr.reg);
1054 return 0;
1055 }
1056
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001057 /* chain pattern in the expression */
1058 LIST_ADDQ(&expr->patterns, &patl->list);
1059
1060 /* that's ok */
1061 return 1;
1062}
1063
1064int pat_idx_tree_ip(struct pattern_expr *expr, struct pattern *pat, char **err)
1065{
1066 unsigned int mask;
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +01001067 struct pattern_tree *node;
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001068
1069 /* Only IPv4 can be indexed */
1070 if (pat->type == SMP_T_IPV4) {
Thierry FOURNIER972028f2014-01-23 17:53:31 +01001071 /* in IPv4 case, check if the mask is contiguous so that we can
1072 * insert the network into the tree. A continuous mask has only
1073 * ones on the left. This means that this mask + its lower bit
1074 * added once again is null.
1075 */
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001076 mask = ntohl(pat->val.ipv4.mask.s_addr);
1077 if (mask + (mask & -mask) == 0) {
1078 mask = mask ? 33 - flsnz(mask & -mask) : 0; /* equals cidr value */
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001079
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001080 /* node memory allocation */
1081 node = calloc(1, sizeof(*node) + 4);
1082 if (!node) {
1083 memprintf(err, "out of memory while loading pattern");
1084 return 0;
1085 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001086
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001087 /* copy the pointer to sample associated to this node */
1088 node->smp = pat->smp;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01001089 node->ref = pat->ref;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001090
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001091 /* FIXME: insert <addr>/<mask> into the tree here */
1092 memcpy(node->node.key, &pat->val.ipv4.addr, 4); /* network byte order */
1093 node->node.node.pfx = mask;
Thierry FOURNIER31db4ae2014-01-30 00:27:15 +01001094
1095 /* Insert the entry. */
1096 ebmb_insert_prefix(&expr->pattern_tree, &node->node, 4);
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001097
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001098 /* that's ok */
1099 return 1;
1100 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001101 else {
1102 /* If the mask is not contiguous, just add the pattern to the list */
1103 return pat_idx_list_val(expr, pat, err);
1104 }
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001105 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001106 else if (pat->type == SMP_T_IPV6) {
1107 /* IPv6 also can be indexed */
1108 node = calloc(1, sizeof(*node) + 16);
1109 if (!node) {
1110 memprintf(err, "out of memory while loading pattern");
1111 return 0;
1112 }
1113
1114 /* copy the pointer to sample associated to this node */
1115 node->smp = pat->smp;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01001116 node->ref = pat->ref;
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001117
1118 /* FIXME: insert <addr>/<mask> into the tree here */
1119 memcpy(node->node.key, &pat->val.ipv6.addr, 16); /* network byte order */
1120 node->node.node.pfx = pat->val.ipv6.mask;
Thierry FOURNIER31db4ae2014-01-30 00:27:15 +01001121
1122 /* Insert the entry. */
1123 ebmb_insert_prefix(&expr->pattern_tree_2, &node->node, 16);
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001124
1125 /* that's ok */
1126 return 1;
1127 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001128
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001129 return 0;
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001130}
1131
1132int pat_idx_tree_str(struct pattern_expr *expr, struct pattern *pat, char **err)
1133{
1134 int len;
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +01001135 struct pattern_tree *node;
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001136
1137 /* Only string can be indexed */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001138 if (pat->type != SMP_T_STR) {
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001139 memprintf(err, "internal error: string expected, but the type is '%s'",
1140 smp_to_type[pat->type]);
1141 return 0;
Thierry FOURNIER972028f2014-01-23 17:53:31 +01001142 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001143
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001144 /* If the flag PAT_F_IGNORE_CASE is set, we cannot use trees */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02001145 if (expr->mflags & PAT_MF_IGNORE_CASE)
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001146 return pat_idx_list_str(expr, pat, err);
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001147
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001148 /* Process the key len */
1149 len = strlen(pat->ptr.str) + 1;
1150
1151 /* node memory allocation */
1152 node = calloc(1, sizeof(*node) + len);
1153 if (!node) {
1154 memprintf(err, "out of memory while loading pattern");
1155 return 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001156 }
1157
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001158 /* copy the pointer to sample associated to this node */
1159 node->smp = pat->smp;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01001160 node->ref = pat->ref;
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001161
1162 /* copy the string */
1163 memcpy(node->node.key, pat->ptr.str, len);
1164
1165 /* index the new node */
Thierry FOURNIER31db4ae2014-01-30 00:27:15 +01001166 ebst_insert(&expr->pattern_tree, &node->node);
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001167
1168 /* that's ok */
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001169 return 1;
1170}
1171
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001172void pat_del_list_val(struct pattern_expr *expr, struct pat_ref_elt *ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001173{
1174 struct pattern_list *pat;
1175 struct pattern_list *safe;
1176
1177 list_for_each_entry_safe(pat, safe, &expr->patterns, list) {
1178 /* Check equality. */
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001179 if (pat->pat.ref != ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001180 continue;
1181
1182 /* Delete and free entry. */
1183 LIST_DEL(&pat->list);
1184 free(pat->pat.smp);
1185 free(pat);
1186 }
1187}
1188
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001189void pat_del_tree_ip(struct pattern_expr *expr, struct pat_ref_elt *ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001190{
1191 struct ebmb_node *node, *next_node;
1192 struct pattern_tree *elt;
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001193
1194 /* browse each node of the tree for IPv4 addresses. */
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001195 for (node = ebmb_first(&expr->pattern_tree), next_node = node ? ebmb_next(node) : NULL;
1196 node;
1197 node = next_node, next_node = node ? ebmb_next(node) : NULL) {
1198 /* Extract container of the tree node. */
1199 elt = container_of(node, struct pattern_tree, node);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001200
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001201 /* Check equality. */
1202 if (elt->ref != ref)
1203 continue;
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001204
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001205 /* Delete and free entry. */
1206 ebmb_delete(node);
1207 free(elt->smp);
1208 free(elt);
1209 }
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001210
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001211 /* Browse each node of the list for IPv4 addresses. */
1212 pat_del_list_val(expr, ref);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001213
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001214 /* browse each node of the tree for IPv6 addresses. */
1215 for (node = ebmb_first(&expr->pattern_tree_2), next_node = node ? ebmb_next(node) : NULL;
1216 node;
1217 node = next_node, next_node = node ? ebmb_next(node) : NULL) {
1218 /* Extract container of the tree node. */
1219 elt = container_of(node, struct pattern_tree, node);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001220
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001221 /* Check equality. */
1222 if (elt->ref != ref)
1223 continue;
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001224
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001225 /* Delete and free entry. */
1226 ebmb_delete(node);
1227 free(elt->smp);
1228 free(elt);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001229 }
1230}
1231
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001232void pat_del_list_ptr(struct pattern_expr *expr, struct pat_ref_elt *ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001233{
1234 struct pattern_list *pat;
1235 struct pattern_list *safe;
1236
1237 list_for_each_entry_safe(pat, safe, &expr->patterns, list) {
1238 /* Check equality. */
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001239 if (pat->pat.ref != ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001240 continue;
1241
1242 /* Delete and free entry. */
1243 LIST_DEL(&pat->list);
1244 free(pat->pat.ptr.ptr);
1245 free(pat->pat.smp);
1246 free(pat);
1247 }
1248}
1249
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001250void pat_del_tree_str(struct pattern_expr *expr, struct pat_ref_elt *ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001251{
1252 struct ebmb_node *node, *next_node;
1253 struct pattern_tree *elt;
1254
1255 /* browse each node of the tree. */
1256 for (node = ebmb_first(&expr->pattern_tree), next_node = node ? ebmb_next(node) : NULL;
1257 node;
1258 node = next_node, next_node = node ? ebmb_next(node) : NULL) {
1259 /* Extract container of the tree node. */
1260 elt = container_of(node, struct pattern_tree, node);
1261
1262 /* Check equality. */
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001263 if (elt->ref != ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001264 continue;
1265
1266 /* Delete and free entry. */
1267 ebmb_delete(node);
1268 free(elt->smp);
1269 free(elt);
1270 }
1271}
1272
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001273void pat_del_list_reg(struct pattern_expr *expr, struct pat_ref_elt *ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001274{
1275 struct pattern_list *pat;
1276 struct pattern_list *safe;
1277
1278 list_for_each_entry_safe(pat, safe, &expr->patterns, list) {
1279 /* Check equality. */
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001280 if (pat->pat.ref != ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001281 continue;
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001282
1283 /* Delete and free entry. */
1284 LIST_DEL(&pat->list);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001285 regex_free(pat->pat.ptr.ptr);
1286 free(pat->pat.smp);
1287 free(pat);
1288 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001289}
1290
1291void pattern_init_expr(struct pattern_expr *expr)
1292{
1293 LIST_INIT(&expr->patterns);
Thierry FOURNIER31db4ae2014-01-30 00:27:15 +01001294 expr->pattern_tree = EB_ROOT;
1295 expr->pattern_tree_2 = EB_ROOT;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001296}
1297
1298void pattern_init_head(struct pattern_head *head)
1299{
1300 LIST_INIT(&head->head);
1301}
1302
1303/* The following functions are relative to the management of the reference
1304 * lists. These lists are used to store the original pattern and associated
1305 * value as string form.
1306 *
1307 * This is used with modifiable ACL and MAPS
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001308 *
1309 * The pattern reference are stored with two identifiers: the unique_id and
1310 * the reference.
1311 *
1312 * The reference identify a file. Each file with the same name point to the
1313 * same reference. We can register many times one file. If the file is modified,
1314 * all his dependencies are also modified. The reference can be used with map or
1315 * acl.
1316 *
1317 * The unique_id identify inline acl. The unique id is unique for each acl.
1318 * You cannot force the same id in the configuration file, because this repoort
1319 * an error.
1320 *
1321 * A particular case appears if the filename is a number. In this case, the
1322 * unique_id is set with the number represented by the filename and the
1323 * reference is also set. This method prevent double unique_id.
1324 *
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001325 */
1326
1327/* This function lookup for reference. If the reference is found, they return
1328 * pointer to the struct pat_ref, else return NULL.
1329 */
1330struct pat_ref *pat_ref_lookup(const char *reference)
1331{
1332 struct pat_ref *ref;
1333
1334 list_for_each_entry(ref, &pattern_reference, list)
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001335 if (ref->reference && strcmp(reference, ref->reference) == 0)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001336 return ref;
1337 return NULL;
1338}
1339
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001340/* This function lookup for unique id. If the reference is found, they return
1341 * pointer to the struct pat_ref, else return NULL.
1342 */
1343struct pat_ref *pat_ref_lookupid(int unique_id)
1344{
1345 struct pat_ref *ref;
1346
1347 list_for_each_entry(ref, &pattern_reference, list)
1348 if (ref->unique_id == unique_id)
1349 return ref;
1350 return NULL;
1351}
1352
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001353/* This function remove all pattern matching the pointer <refelt> from
1354 * the the reference and from each expr member of the reference. This
1355 * function returns 1 if the deletion is done and return 0 is the entry
1356 * is not found.
1357 */
1358int pat_ref_delete_by_id(struct pat_ref *ref, struct pat_ref_elt *refelt)
1359{
1360 struct pattern_expr *expr;
1361 struct pat_ref_elt *elt, *safe;
1362
1363 /* delete pattern from reference */
1364 list_for_each_entry_safe(elt, safe, &ref->head, list) {
1365 if (elt == refelt) {
1366 LIST_DEL(&elt->list);
1367 free(elt->sample);
1368 free(elt->pattern);
1369 free(elt);
1370
1371 list_for_each_entry(expr, &ref->pat, list)
1372 pattern_delete(expr, elt);
1373
1374 return 1;
1375 }
1376 }
1377 return 0;
1378}
1379
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001380/* This function remove all pattern match <key> from the the reference
1381 * and from each expr member of the reference. This fucntion returns 1
1382 * if the deletion is done and return 0 is the entry is not found.
1383 */
1384int pat_ref_delete(struct pat_ref *ref, const char *key)
1385{
1386 struct pattern_expr *expr;
1387 struct pat_ref_elt *elt, *safe;
1388 int found = 0;
1389
1390 /* delete pattern from reference */
1391 list_for_each_entry_safe(elt, safe, &ref->head, list) {
1392 if (strcmp(key, elt->pattern) == 0) {
1393 LIST_DEL(&elt->list);
1394 free(elt->sample);
1395 free(elt->pattern);
1396 free(elt);
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001397
1398 list_for_each_entry(expr, &ref->pat, list)
1399 pattern_delete(expr, elt);
1400
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001401 found = 1;
1402 }
1403 }
1404
1405 if (!found)
1406 return 0;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001407 return 1;
1408}
1409
Baptiste Assmann953f74d2014-04-25 16:57:03 +02001410/*
1411 * find and return an element <elt> matching <key> in a reference <ref>
1412 * return NULL if not found
1413 */
1414struct pat_ref_elt *pat_ref_find_elt(struct pat_ref *ref, const char *key)
1415{
1416 struct pat_ref_elt *elt;
1417
1418 list_for_each_entry(elt, &ref->head, list) {
1419 if (strcmp(key, elt->pattern) == 0)
1420 return elt;
1421 }
1422
1423 return NULL;
1424}
1425
1426
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001427 /* This function modify the sample of the first pattern that match the <key>. */
1428static inline int pat_ref_set_elt(struct pat_ref *ref, struct pat_ref_elt *elt,
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001429 const char *value, char **err)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001430{
1431 struct pattern_expr *expr;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001432 struct sample_storage **smp;
1433 char *sample;
Thierry FOURNIER149e0fe2014-01-29 19:35:06 +01001434 struct sample_storage test;
1435
1436 /* Try all needed converters. */
1437 list_for_each_entry(expr, &ref->pat, list) {
1438 if (!expr->pat_head->parse_smp)
1439 continue;
1440
1441 if (!expr->pat_head->parse_smp(value, &test)) {
1442 memprintf(err, "unable to parse '%s'", value);
1443 return 0;
1444 }
1445 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001446
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001447 /* Modify pattern from reference. */
1448 sample = strdup(value);
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001449 if (!sample) {
1450 memprintf(err, "out of memory error");
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001451 return 0;
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001452 }
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001453 free(elt->sample);
1454 elt->sample = sample;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001455
Thierry FOURNIER149e0fe2014-01-29 19:35:06 +01001456 /* Load sample in each reference. All the conversion are tested
1457 * below, normally these calls dosn't fail.
1458 */
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001459 list_for_each_entry(expr, &ref->pat, list) {
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001460 if (!expr->pat_head->parse_smp)
1461 continue;
1462
1463 smp = pattern_find_smp(expr, elt);
Thierry FOURNIER149e0fe2014-01-29 19:35:06 +01001464 if (smp && *smp && !expr->pat_head->parse_smp(sample, *smp))
1465 *smp = NULL;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001466 }
1467
Thierry FOURNIER149e0fe2014-01-29 19:35:06 +01001468 return 1;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001469}
1470
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001471/* This function modify the sample of the first pattern that match the <key>. */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001472int 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 +01001473{
1474 struct pat_ref_elt *elt;
1475
1476 /* Look for pattern in the reference. */
1477 list_for_each_entry(elt, &ref->head, list) {
1478 if (elt == refelt) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001479 if (!pat_ref_set_elt(ref, elt, value, err))
1480 return 0;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001481 return 1;
1482 }
1483 }
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001484
1485 memprintf(err, "key or pattern not found");
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001486 return 0;
1487}
1488
1489/* This function modify the sample of the first pattern that match the <key>. */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001490int pat_ref_set(struct pat_ref *ref, const char *key, const char *value, char **err)
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001491{
1492 struct pat_ref_elt *elt;
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001493 int found = 0;
1494 char *_merr;
1495 char **merr;
1496
1497 if (err) {
1498 merr = &_merr;
1499 *merr = NULL;
1500 }
1501 else
1502 merr = NULL;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001503
1504 /* Look for pattern in the reference. */
1505 list_for_each_entry(elt, &ref->head, list) {
1506 if (strcmp(key, elt->pattern) == 0) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001507 if (!pat_ref_set_elt(ref, elt, value, merr)) {
1508 if (!found)
1509 *err = *merr;
1510 else {
1511 memprintf(err, "%s, %s", *err, *merr);
1512 free(*merr);
1513 *merr = NULL;
1514 }
1515 }
1516 found = 1;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001517 }
1518 }
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001519
1520 if (!found) {
1521 memprintf(err, "entry not found");
1522 return 0;
1523 }
1524 return 1;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001525}
1526
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001527/* This function create new reference. <ref> is the reference name.
1528 * <flags> are PAT_REF_*. /!\ The reference is not checked, and must
1529 * be unique. The user must check the reference with "pat_ref_lookup()"
1530 * before calling this function. If the fucntion fail, it return NULL,
1531 * else return new struct pat_ref.
1532 */
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001533struct pat_ref *pat_ref_new(const char *reference, const char *display, unsigned int flags)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001534{
1535 struct pat_ref *ref;
1536
1537 ref = malloc(sizeof(*ref));
1538 if (!ref)
1539 return NULL;
1540
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001541 if (display) {
1542 ref->display = strdup(display);
1543 if (!ref->display) {
1544 free(ref);
1545 return NULL;
1546 }
1547 }
1548 else
1549 ref->display = NULL;
1550
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001551 ref->reference = strdup(reference);
1552 if (!ref->reference) {
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001553 free(ref->display);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001554 free(ref);
1555 return NULL;
1556 }
1557
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001558 ref->flags = flags;
1559 ref->unique_id = -1;
1560
1561 LIST_INIT(&ref->head);
1562 LIST_INIT(&ref->pat);
1563
1564 LIST_ADDQ(&pattern_reference, &ref->list);
1565
1566 return ref;
1567}
1568
1569/* This function create new reference. <unique_id> is the unique id. If
1570 * the value of <unique_id> is -1, the unique id is calculated later.
1571 * <flags> are PAT_REF_*. /!\ The reference is not checked, and must
1572 * be unique. The user must check the reference with "pat_ref_lookup()"
1573 * or pat_ref_lookupid before calling this function. If the function
1574 * fail, it return NULL, else return new struct pat_ref.
1575 */
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001576struct pat_ref *pat_ref_newid(int unique_id, const char *display, unsigned int flags)
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001577{
1578 struct pat_ref *ref;
1579
1580 ref = malloc(sizeof(*ref));
1581 if (!ref)
1582 return NULL;
1583
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001584 if (display) {
1585 ref->display = strdup(display);
1586 if (!ref->display) {
1587 free(ref);
1588 return NULL;
1589 }
1590 }
1591 else
1592 ref->display = NULL;
1593
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001594 ref->reference = NULL;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001595 ref->flags = flags;
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001596 ref->unique_id = unique_id;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001597 LIST_INIT(&ref->head);
1598 LIST_INIT(&ref->pat);
1599
1600 LIST_ADDQ(&pattern_reference, &ref->list);
1601
1602 return ref;
1603}
1604
1605/* This function adds entry to <ref>. It can failed with memory error.
1606 * If the function fails, it returns 0.
1607 */
1608int pat_ref_append(struct pat_ref *ref, char *pattern, char *sample, int line)
1609{
1610 struct pat_ref_elt *elt;
1611
1612 elt = malloc(sizeof(*elt));
1613 if (!elt)
1614 return 0;
1615
1616 elt->line = line;
1617
1618 elt->pattern = strdup(pattern);
1619 if (!elt->pattern) {
1620 free(elt);
1621 return 0;
1622 }
1623
1624 if (sample) {
1625 elt->sample = strdup(sample);
1626 if (!elt->sample) {
1627 free(elt->pattern);
1628 free(elt);
1629 return 0;
1630 }
1631 }
1632 else
1633 elt->sample = NULL;
1634
1635 LIST_ADDQ(&ref->head, &elt->list);
1636
1637 return 1;
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001638}
1639
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001640/* This function create sample found in <elt>, parse the pattern also
1641 * found in <elt> and insert it in <expr>. The function copy <patflags>
1642 * in <expr>. If the function fails, it returns0 and <err> is filled.
1643 * In succes case, the function returns 1.
1644 */
1645static inline
1646int pat_ref_push(struct pat_ref_elt *elt, struct pattern_expr *expr,
1647 int patflags, char **err)
1648{
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001649 struct sample_storage *smp;
Thierry FOURNIERd25c8422014-01-28 15:34:35 +01001650 struct pattern pattern;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001651
1652 /* Create sample */
1653 if (elt->sample && expr->pat_head->parse_smp) {
1654 /* New sample. */
1655 smp = malloc(sizeof(*smp));
1656 if (!smp)
1657 return 0;
1658
1659 /* Parse value. */
1660 if (!expr->pat_head->parse_smp(elt->sample, smp)) {
1661 memprintf(err, "unable to parse '%s'", elt->sample);
1662 free(smp);
1663 return 0;
1664 }
1665
1666 }
1667 else
1668 smp = NULL;
1669
Thierry FOURNIERd25c8422014-01-28 15:34:35 +01001670 /* initialise pattern */
1671 memset(&pattern, 0, sizeof(pattern));
Thierry FOURNIERd25c8422014-01-28 15:34:35 +01001672 pattern.smp = smp;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01001673 pattern.ref = elt;
Thierry FOURNIERd25c8422014-01-28 15:34:35 +01001674
1675 /* parse pattern */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02001676 if (!expr->pat_head->parse(elt->pattern, &pattern, expr->mflags, err)) {
Thierry FOURNIERd25c8422014-01-28 15:34:35 +01001677 free(smp);
1678 return 0;
1679 }
1680
1681 /* index pattern */
1682 if (!expr->pat_head->index(expr, &pattern, err)) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001683 free(smp);
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001684 return 0;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001685 }
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001686
1687 return 1;
1688}
1689
Thierry FOURNIER31db4ae2014-01-30 00:27:15 +01001690/* This function adds entry to <ref>. It can failed with memory error. The new
1691 * entry is added at all the pattern_expr registered in this reference. The
1692 * function stop on the first error encountered. It returns 0 and err is
1693 * filled. If an error is encountered, the complete add operation is cancelled.
1694 * If the insertion is a success the function returns 1.
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001695 */
1696int pat_ref_add(struct pat_ref *ref,
1697 const char *pattern, const char *sample,
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02001698 char **err)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001699{
1700 struct pat_ref_elt *elt;
1701 struct pattern_expr *expr;
1702
1703 elt = malloc(sizeof(*elt));
1704 if (!elt) {
1705 memprintf(err, "out of memory error");
1706 return 0;
1707 }
1708
1709 elt->line = -1;
1710
1711 elt->pattern = strdup(pattern);
1712 if (!elt->pattern) {
1713 free(elt);
1714 memprintf(err, "out of memory error");
1715 return 0;
1716 }
1717
1718 if (sample) {
1719 elt->sample = strdup(sample);
1720 if (!elt->sample) {
1721 free(elt->pattern);
1722 free(elt);
1723 memprintf(err, "out of memory error");
1724 return 0;
1725 }
1726 }
1727 else
1728 elt->sample = NULL;
1729
1730 LIST_ADDQ(&ref->head, &elt->list);
1731
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001732 list_for_each_entry(expr, &ref->pat, list) {
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02001733 if (!pat_ref_push(elt, expr, 0, err)) {
Thierry FOURNIER31db4ae2014-01-30 00:27:15 +01001734 /* If the insertion fails, try to delete all the added entries. */
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001735 pat_ref_delete_by_id(ref, elt);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001736 return 0;
1737 }
1738 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001739 return 1;
1740}
1741
Thierry FOURNIER46006bd2014-03-21 21:45:15 +01001742/* This function prune <ref>, replace all reference by the references
1743 * of <replace>, and reindex all the news values.
1744 *
1745 * The pattern are loaded in best effort and the errors are ignored,
1746 * but writed in the logs.
1747 */
1748void pat_ref_reload(struct pat_ref *ref, struct pat_ref *replace)
1749{
1750 struct pattern_expr *expr;
1751 struct pat_ref_elt *elt;
1752 char *err = NULL;
1753
1754 pat_ref_prune(ref);
1755
1756 LIST_ADD(&replace->head, &ref->head);
1757 LIST_DEL(&replace->head);
1758
1759 list_for_each_entry(elt, &ref->head, list) {
1760 list_for_each_entry(expr, &ref->pat, list) {
1761 if (!pat_ref_push(elt, expr, 0, &err)) {
1762 send_log(NULL, LOG_NOTICE, "%s", err);
1763 free(err);
1764 err = NULL;
1765 }
1766 }
1767 }
1768}
1769
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001770/* This function prune all entries of <ref>. This function
1771 * prune the associated pattern_expr.
1772 */
1773void pat_ref_prune(struct pat_ref *ref)
1774{
1775 struct pat_ref_elt *elt, *safe;
1776 struct pattern_expr *expr;
1777
1778 list_for_each_entry_safe(elt, safe, &ref->head, list) {
1779 LIST_DEL(&elt->list);
1780 free(elt->pattern);
1781 free(elt->sample);
1782 free(elt);
1783 }
1784
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001785 list_for_each_entry(expr, &ref->pat, list)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001786 expr->pat_head->prune(expr);
1787}
1788
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001789/* This function lookup for existing reference <ref> in pattern_head <head>. */
1790struct pattern_expr *pattern_lookup_expr(struct pattern_head *head, struct pat_ref *ref)
1791{
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001792 struct pattern_expr_list *expr;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001793
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001794 list_for_each_entry(expr, &head->head, list)
1795 if (expr->expr->ref == ref)
1796 return expr->expr;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001797 return NULL;
1798}
1799
1800/* This function create new pattern_expr associated to the reference <ref>.
1801 * <ref> can be NULL. If an error is occured, the function returns NULL and
1802 * <err> is filled. Otherwise, the function returns new pattern_expr linked
1803 * with <head> and <ref>.
1804 */
1805struct pattern_expr *pattern_new_expr(struct pattern_head *head, struct pat_ref *ref, char **err)
1806{
1807 struct pattern_expr *expr;
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001808 struct pattern_expr_list *list;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001809
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001810 /* Memory and initialization of the chain element. */
1811 list = malloc(sizeof(*list));
1812 if (!list) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001813 memprintf(err, "out of memory");
1814 return NULL;
1815 }
1816
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001817 /* Look for existing similar expr. No that only the index, parse and
1818 * parse_smp function must be identical for having similar pattern.
1819 * The other function depends of theses first.
1820 */
1821 if (ref) {
1822 list_for_each_entry(expr, &ref->pat, list)
1823 if (expr->pat_head->index == head->index &&
1824 expr->pat_head->parse == head->parse &&
1825 expr->pat_head->parse_smp == head->parse_smp)
1826 break;
1827 if (&expr->list == &ref->pat)
1828 expr = NULL;
1829 }
1830 else
1831 expr = NULL;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001832
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001833 /* If no similar expr was found, we create new expr. */
1834 if (!expr) {
1835 /* Get a lot of memory for the expr struct. */
1836 expr = malloc(sizeof(*expr));
1837 if (!expr) {
1838 memprintf(err, "out of memory");
1839 return NULL;
1840 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001841
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001842 /* Initialize this new expr. */
1843 pattern_init_expr(expr);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001844
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001845 /* This new pattern expression reference one of his heads. */
1846 expr->pat_head = head;
1847
1848 /* Link with ref, or to self to facilitate LIST_DEL() */
1849 if (ref)
1850 LIST_ADDQ(&ref->pat, &expr->list);
1851 else
1852 LIST_INIT(&expr->list);
1853
1854 expr->ref = ref;
1855
1856 /* We must free this pattern if it is no more used. */
1857 list->do_free = 1;
1858 }
1859 else {
1860 /* If the pattern used already exists, it is already linked
1861 * with ref and we must not free it.
1862 */
1863 list->do_free = 0;
1864 }
1865
1866 /* The new list element reference the pattern_expr. */
1867 list->expr = expr;
1868
1869 /* Link the list element with the pattern_head. */
1870 LIST_ADDQ(&head->head, &list->list);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001871 return expr;
1872}
1873
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001874/* Reads patterns from a file. If <err_msg> is non-NULL, an error message will
1875 * be returned there on errors and the caller will have to free it.
Thierry FOURNIER39bef452014-01-29 13:29:45 +01001876 *
1877 * The file contains one key + value per line. Lines which start with '#' are
1878 * ignored, just like empty lines. Leading tabs/spaces are stripped. The key is
1879 * then the first "word" (series of non-space/tabs characters), and the value is
1880 * what follows this series of space/tab till the end of the line excluding
1881 * trailing spaces/tabs.
1882 *
1883 * Example :
1884 *
1885 * # this is a comment and is ignored
1886 * 62.212.114.60 1wt.eu \n
1887 * <-><-----------><---><----><---->
1888 * | | | | `--- trailing spaces ignored
1889 * | | | `-------- value
1890 * | | `--------------- middle spaces ignored
1891 * | `------------------------ key
1892 * `-------------------------------- leading spaces ignored
1893 *
1894 * Return non-zero in case of succes, otherwise 0.
1895 */
1896int pat_ref_read_from_file_smp(struct pat_ref *ref, const char *filename, char **err)
1897{
1898 FILE *file;
1899 char *c;
1900 int ret = 0;
1901 int line = 0;
1902 char *key_beg;
1903 char *key_end;
1904 char *value_beg;
1905 char *value_end;
1906
1907 file = fopen(filename, "r");
1908 if (!file) {
1909 memprintf(err, "failed to open pattern file <%s>", filename);
1910 return 0;
1911 }
1912
1913 /* now parse all patterns. The file may contain only one pattern
1914 * followed by one value per line. The start spaces, separator spaces
1915 * and and spaces are stripped. Each can contain comment started by '#'
1916 */
1917 while (fgets(trash.str, trash.size, file) != NULL) {
1918 line++;
1919 c = trash.str;
1920
1921 /* ignore lines beginning with a dash */
1922 if (*c == '#')
1923 continue;
1924
1925 /* strip leading spaces and tabs */
1926 while (*c == ' ' || *c == '\t')
1927 c++;
1928
1929 /* empty lines are ignored too */
1930 if (*c == '\0' || *c == '\r' || *c == '\n')
1931 continue;
1932
1933 /* look for the end of the key */
1934 key_beg = c;
1935 while (*c && *c != ' ' && *c != '\t' && *c != '\n' && *c != '\r')
1936 c++;
1937
1938 key_end = c;
1939
1940 /* strip middle spaces and tabs */
1941 while (*c == ' ' || *c == '\t')
1942 c++;
1943
1944 /* look for the end of the value, it is the end of the line */
1945 value_beg = c;
1946 while (*c && *c != '\n' && *c != '\r')
1947 c++;
1948 value_end = c;
1949
1950 /* trim possibly trailing spaces and tabs */
1951 while (value_end > value_beg && (value_end[-1] == ' ' || value_end[-1] == '\t'))
1952 value_end--;
1953
1954 /* set final \0 and check entries */
1955 *key_end = '\0';
1956 *value_end = '\0';
1957
1958 /* insert values */
1959 if (!pat_ref_append(ref, key_beg, value_beg, line)) {
1960 memprintf(err, "out of memory");
1961 goto out_close;
1962 }
1963 }
1964
1965 /* succes */
1966 ret = 1;
1967
1968 out_close:
1969 fclose(file);
1970 return ret;
1971}
1972
1973/* Reads patterns from a file. If <err_msg> is non-NULL, an error message will
1974 * be returned there on errors and the caller will have to free it.
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001975 */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001976int pat_ref_read_from_file(struct pat_ref *ref, const char *filename, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001977{
1978 FILE *file;
1979 char *c;
1980 char *arg;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001981 int ret = 0;
1982 int line = 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001983
1984 file = fopen(filename, "r");
1985 if (!file) {
1986 memprintf(err, "failed to open pattern file <%s>", filename);
1987 return 0;
1988 }
1989
1990 /* now parse all patterns. The file may contain only one pattern per
1991 * line. If the line contains spaces, they will be part of the pattern.
1992 * The pattern stops at the first CR, LF or EOF encountered.
1993 */
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001994 while (fgets(trash.str, trash.size, file) != NULL) {
1995 line++;
1996 c = trash.str;
1997
1998 /* ignore lines beginning with a dash */
1999 if (*c == '#')
2000 continue;
2001
2002 /* strip leading spaces and tabs */
2003 while (*c == ' ' || *c == '\t')
2004 c++;
2005
2006
2007 arg = c;
2008 while (*c && *c != '\n' && *c != '\r')
2009 c++;
2010 *c = 0;
2011
2012 /* empty lines are ignored too */
2013 if (c == arg)
2014 continue;
2015
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002016 if (!pat_ref_append(ref, arg, NULL, line)) {
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002017 memprintf(err, "out of memory when loading patterns from file <%s>", filename);
2018 goto out_close;
2019 }
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002020 }
2021
2022 ret = 1; /* success */
2023
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002024 out_close:
2025 fclose(file);
2026 return ret;
2027}
2028
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002029int pattern_read_from_file(struct pattern_head *head, unsigned int refflags,
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002030 const char *filename, int patflags, int load_smp,
Thierry FOURNIER94580c92014-02-11 14:36:45 +01002031 char **err, const char *file, int line)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002032{
2033 struct pat_ref *ref;
2034 struct pattern_expr *expr;
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002035 struct pat_ref_elt *elt;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002036
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002037 /* Lookup for the existing reference. */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002038 ref = pat_ref_lookup(filename);
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002039
2040 /* If the reference doesn't exists, create it and load associated file. */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002041 if (!ref) {
Thierry FOURNIER94580c92014-02-11 14:36:45 +01002042 chunk_printf(&trash,
2043 "pattern loaded from file '%s' used by %s at file '%s' line %d",
2044 filename, refflags & PAT_REF_MAP ? "map" : "acl", file, line);
2045
2046 ref = pat_ref_new(filename, trash.str, refflags);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002047 if (!ref) {
2048 memprintf(err, "out of memory");
2049 return 0;
2050 }
2051
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002052 if (load_smp) {
Thierry FOURNIERc0bd9102014-01-29 12:32:58 +01002053 ref->flags |= PAT_REF_SMP;
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002054 if (!pat_ref_read_from_file_smp(ref, filename, err))
2055 return 0;
2056 }
2057 else {
2058 if (!pat_ref_read_from_file(ref, filename, err))
2059 return 0;
2060 }
2061 }
2062 else {
Thierry FOURNIERc0bd9102014-01-29 12:32:58 +01002063 /* The reference already exists, check the map compatibility. */
2064
2065 /* If the load require samples and the flag PAT_REF_SMP is not set,
2066 * the reference doesn't contain sample, and cannot be used.
2067 */
2068 if (load_smp) {
2069 if (!(ref->flags & PAT_REF_SMP)) {
2070 memprintf(err, "The file \"%s\" is already used as one column file "
2071 "and cannot be used by as two column file.",
2072 filename);
2073 return 0;
2074 }
2075 }
2076 else {
2077 /* The load doesn't require samples. If the flag PAT_REF_SMP is
2078 * set, the reference contains a sample, and cannot be used.
2079 */
2080 if (ref->flags & PAT_REF_SMP) {
2081 memprintf(err, "The file \"%s\" is already used as two column file "
2082 "and cannot be used by as one column file.",
2083 filename);
2084 return 0;
2085 }
2086 }
2087
Thierry FOURNIER94580c92014-02-11 14:36:45 +01002088 /* Extends display */
2089 chunk_printf(&trash, "%s", ref->display);
2090 chunk_appendf(&trash, ", by %s at file '%s' line %d",
2091 refflags & PAT_REF_MAP ? "map" : "acl", file, line);
2092 free(ref->display);
2093 ref->display = strdup(trash.str);
2094 if (!ref->display) {
2095 memprintf(err, "out of memory");
2096 return 0;
2097 }
2098
Thierry FOURNIERc0bd9102014-01-29 12:32:58 +01002099 /* Merge flags. */
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002100 ref->flags |= refflags;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002101 }
2102
2103 /* Now, we can loading patterns from the reference. */
2104
2105 /* Lookup for existing reference in the head. If the reference
2106 * doesn't exists, create it.
2107 */
2108 expr = pattern_lookup_expr(head, ref);
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02002109 if (!expr || (expr->mflags != patflags)) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002110 expr = pattern_new_expr(head, ref, err);
2111 if (!expr)
2112 return 0;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02002113 expr->mflags = patflags;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002114 }
2115
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002116 /* Load reference content in the pattern expression. */
2117 list_for_each_entry(elt, &ref->head, list) {
2118 if (!pat_ref_push(elt, expr, patflags, err)) {
2119 if (elt->line > 0)
2120 memprintf(err, "%s at line %d of file '%s'",
2121 *err, elt->line, filename);
2122 return 0;
2123 }
2124 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002125
2126 return 1;
2127}
2128
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01002129/* This function executes a pattern match on a sample. It applies pattern <expr>
2130 * to sample <smp>. The function returns NULL if the sample dont match. It returns
2131 * non-null if the sample match. If <fill> is true and the sample match, the
2132 * function returns the matched pattern. In many cases, this pattern can be a
2133 * static buffer.
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002134 */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002135struct pattern *pattern_exec_match(struct pattern_head *head, struct sample *smp, int fill)
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002136{
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002137 struct pattern_expr_list *list;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002138 struct pattern *pat;
2139
2140 if (!head->match) {
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01002141 if (fill) {
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01002142 static_pattern.smp = NULL;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01002143 static_pattern.ref = NULL;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02002144 static_pattern.sflags = 0;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01002145 static_pattern.type = SMP_T_UINT;
2146 static_pattern.val.i = 1;
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01002147 }
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01002148 return &static_pattern;
2149 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002150
Thierry FOURNIER5d344082014-01-27 14:19:53 +01002151 /* convert input to string */
2152 if (!sample_convert(smp, head->expect_type))
2153 return NULL;
2154
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002155 list_for_each_entry(list, &head->head, list) {
2156 pat = head->match(smp, list->expr, fill);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002157 if (pat)
2158 return pat;
2159 }
2160 return NULL;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002161}
2162
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01002163/* This function prune the pattern expression. */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002164void pattern_prune(struct pattern_head *head)
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01002165{
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002166 struct pattern_expr_list *list, *safe;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002167
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002168 list_for_each_entry_safe(list, safe, &head->head, list) {
2169 LIST_DEL(&list->list);
2170 if (list->do_free) {
2171 LIST_DEL(&list->expr->list);
2172 head->prune(list->expr);
2173 free(list->expr);
2174 }
2175 free(list);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002176 }
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01002177}
2178
Thierry FOURNIER55d0b102014-01-15 11:25:26 +01002179/* This function lookup for a pattern matching the <key> and return a
2180 * pointer to a pointer of the sample stoarge. If the <key> dont match,
2181 * the function returns NULL. If the key cannot be parsed, the function
2182 * fill <err>.
2183 */
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01002184struct sample_storage **pattern_find_smp(struct pattern_expr *expr, struct pat_ref_elt *ref)
Thierry FOURNIER55d0b102014-01-15 11:25:26 +01002185{
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01002186 struct ebmb_node *node;
2187 struct pattern_tree *elt;
2188 struct pattern_list *pat;
Thierry FOURNIER55d0b102014-01-15 11:25:26 +01002189
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01002190 for (node = ebmb_first(&expr->pattern_tree);
2191 node;
2192 node = ebmb_next(node)) {
2193 elt = container_of(node, struct pattern_tree, node);
2194 if (elt->ref == ref)
2195 return &elt->smp;
2196 }
2197
2198 for (node = ebmb_first(&expr->pattern_tree_2);
2199 node;
2200 node = ebmb_next(node)) {
2201 elt = container_of(node, struct pattern_tree, node);
2202 if (elt->ref == ref)
2203 return &elt->smp;
2204 }
2205
2206 list_for_each_entry(pat, &expr->patterns, list)
2207 if (pat->pat.ref == ref)
2208 return &pat->pat.smp;
2209
2210 return NULL;
Thierry FOURNIER55d0b102014-01-15 11:25:26 +01002211}
2212
Thierry FOURNIERb1136502014-01-15 11:38:49 +01002213/* This function search all the pattern matching the <key> and delete it.
2214 * If the parsing of the input key fails, the function returns 0 and the
2215 * <err> is filled, else return 1;
2216 */
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01002217int pattern_delete(struct pattern_expr *expr, struct pat_ref_elt *ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01002218{
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01002219 expr->pat_head->delete(expr, ref);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01002220 return 1;
2221}
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002222
2223/* This function finalize the configuration parsing. Its set all the
2224 * automatic ids
2225 */
2226void pattern_finalize_config(void)
2227{
2228 int i = 0;
2229 struct pat_ref *ref, *ref2, *ref3;
2230 struct list pr = LIST_HEAD_INIT(pr);
2231
2232 list_for_each_entry(ref, &pattern_reference, list) {
2233 if (ref->unique_id == -1) {
2234 /* Look for the first free id. */
2235 while (1) {
2236 list_for_each_entry(ref2, &pattern_reference, list) {
2237 if (ref2->unique_id == i) {
2238 i++;
2239 break;
2240 }
2241 }
Willy Tarreau3b786962014-04-26 12:37:25 +02002242 if (&ref2->list == &pattern_reference)
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002243 break;
2244 }
2245
2246 /* Uses the unique id and increment it for the next entry. */
2247 ref->unique_id = i;
2248 i++;
2249 }
2250 }
2251
2252 /* This sort the reference list by id. */
2253 list_for_each_entry_safe(ref, ref2, &pattern_reference, list) {
2254 LIST_DEL(&ref->list);
2255 list_for_each_entry(ref3, &pr, list) {
2256 if (ref->unique_id < ref3->unique_id) {
2257 LIST_ADDQ(&ref3->list, &ref->list);
2258 break;
2259 }
2260 }
2261 if (&ref3->list == &pr)
2262 LIST_ADDQ(&pr, &ref->list);
2263 }
2264
2265 /* swap root */
2266 LIST_ADD(&pr, &pattern_reference);
2267 LIST_DEL(&pr);
2268}