blob: 5817f7ecd198581e22ee89a8c80e328f1bdb93f8 [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,
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +020068 [PAT_MATCH_BEG] = pat_idx_tree_pfx,
Thierry FOURNIERb9b08462013-12-13 15:12:32 +010069 [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,
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +020084 [PAT_MATCH_BEG] = pat_del_tree_str,
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +010085 [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 *
Willy Tarreau9377c0e2014-08-29 15:19:33 +0200181 * The following functions are used for parsing pattern matching input value.
182 * The <text> contain the string to be parsed. <pattern> must be a preallocated
183 * pattern. The pat_parse_* functions fill this structure with the parsed value.
184 * <err> is filled with an error message built with memprintf() function. It is
185 * allowed to use a trash as a temporary storage for the returned pattern, as
186 * the next call after these functions will be pat_idx_*.
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100187 *
Willy Tarreau9377c0e2014-08-29 15:19:33 +0200188 * In success case, the pat_parse_* function returns 1. If the function
189 * fails, it returns 0 and <err> is filled.
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100190 */
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100191
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100192/* ignore the current line */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200193int pat_parse_nothing(const char *text, struct pattern *pattern, int mflags, char **err)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100194{
195 return 1;
196}
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100197
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100198/* Parse a string. It is allocated and duplicated. */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200199int pat_parse_str(const char *text, struct pattern *pattern, int mflags, char **err)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100200{
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100201 pattern->type = SMP_T_STR;
Thierry FOURNIERedc15c32013-12-13 15:36:59 +0100202 pattern->ptr.str = (char *)text;
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100203 pattern->len = strlen(text);
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100204 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100205}
206
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100207/* Parse a binary written in hexa. It is allocated. */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200208int pat_parse_bin(const char *text, struct pattern *pattern, int mflags, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100209{
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100210 struct chunk *trash;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100211
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100212 pattern->type = SMP_T_BIN;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100213 trash = get_trash_chunk();
214 pattern->len = trash->size;
215 pattern->ptr.str = trash->str;
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100216 return !!parse_binary(text, &pattern->ptr.str, &pattern->len, err);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100217}
218
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100219/* Parse a regex. It is allocated. */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200220int pat_parse_reg(const char *text, struct pattern *pattern, int mflags, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100221{
Thierry FOURNIER0b6d15f2014-01-29 19:35:16 +0100222 pattern->ptr.str = (char *)text;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100223 return 1;
224}
225
226/* Parse a range of positive integers delimited by either ':' or '-'. If only
227 * one integer is read, it is set as both min and max. An operator may be
228 * specified as the prefix, among this list of 5 :
229 *
230 * 0:eq, 1:gt, 2:ge, 3:lt, 4:le
231 *
232 * The default operator is "eq". It supports range matching. Ranges are
233 * rejected for other operators. The operator may be changed at any time.
234 * The operator is stored in the 'opaque' argument.
235 *
236 * If err is non-NULL, an error message will be returned there on errors and
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100237 * the caller will have to free it. The function returns zero on error, and
238 * non-zero on success.
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100239 *
240 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200241int pat_parse_int(const char *text, struct pattern *pattern, int mflags, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100242{
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100243 const char *ptr = text;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100244
245 pattern->type = SMP_T_UINT;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100246
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100247 /* Empty string is not valid */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100248 if (!*text)
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100249 goto not_valid_range;
250
251 /* Search ':' or '-' separator. */
252 while (*ptr != '\0' && *ptr != ':' && *ptr != '-')
253 ptr++;
254
255 /* If separator not found. */
256 if (!*ptr) {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100257 if (strl2llrc(text, ptr - text, &pattern->val.range.min) != 0) {
258 memprintf(err, "'%s' is not a number", text);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100259 return 0;
260 }
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100261 pattern->val.range.max = pattern->val.range.min;
262 pattern->val.range.min_set = 1;
263 pattern->val.range.max_set = 1;
264 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100265 }
266
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100267 /* If the separator is the first character. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100268 if (ptr == text && *(ptr + 1) != '\0') {
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100269 if (strl2llrc(ptr + 1, strlen(ptr + 1), &pattern->val.range.max) != 0)
270 goto not_valid_range;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100271
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100272 pattern->val.range.min_set = 0;
273 pattern->val.range.max_set = 1;
274 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100275 }
276
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100277 /* If separator is the last character. */
278 if (*(ptr + 1) == '\0') {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100279 if (strl2llrc(text, ptr - text, &pattern->val.range.min) != 0)
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100280 goto not_valid_range;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100281
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100282 pattern->val.range.min_set = 1;
283 pattern->val.range.max_set = 0;
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100284 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100285 }
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100286
287 /* Else, parse two numbers. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100288 if (strl2llrc(text, ptr - text, &pattern->val.range.min) != 0)
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100289 goto not_valid_range;
290
291 if (strl2llrc(ptr + 1, strlen(ptr + 1), &pattern->val.range.max) != 0)
292 goto not_valid_range;
293
294 if (pattern->val.range.min > pattern->val.range.max)
295 goto not_valid_range;
296
297 pattern->val.range.min_set = 1;
298 pattern->val.range.max_set = 1;
299 return 1;
300
301 not_valid_range:
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100302 memprintf(err, "'%s' is not a valid number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100303 return 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100304}
305
306/* Parse a range of positive 2-component versions delimited by either ':' or
307 * '-'. The version consists in a major and a minor, both of which must be
308 * smaller than 65536, because internally they will be represented as a 32-bit
309 * integer.
310 * If only one version is read, it is set as both min and max. Just like for
311 * pure integers, an operator may be specified as the prefix, among this list
312 * of 5 :
313 *
314 * 0:eq, 1:gt, 2:ge, 3:lt, 4:le
315 *
316 * The default operator is "eq". It supports range matching. Ranges are
317 * rejected for other operators. The operator may be changed at any time.
318 * The operator is stored in the 'opaque' argument. This allows constructs
319 * such as the following one :
320 *
321 * acl obsolete_ssl ssl_req_proto lt 3
322 * acl unsupported_ssl ssl_req_proto gt 3.1
323 * acl valid_ssl ssl_req_proto 3.0-3.1
324 *
325 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200326int pat_parse_dotted_ver(const char *text, struct pattern *pattern, int mflags, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100327{
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100328 const char *ptr = text;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100329
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100330 pattern->type = SMP_T_UINT;
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100331
332 /* Search ':' or '-' separator. */
333 while (*ptr != '\0' && *ptr != ':' && *ptr != '-')
334 ptr++;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100335
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100336 /* If separator not found. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100337 if (*ptr == '\0' && ptr > text) {
338 if (strl2llrc_dotted(text, ptr-text, &pattern->val.range.min) != 0) {
339 memprintf(err, "'%s' is not a dotted number", text);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100340 return 0;
341 }
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100342 pattern->val.range.max = pattern->val.range.min;
343 pattern->val.range.min_set = 1;
344 pattern->val.range.max_set = 1;
345 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100346 }
347
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100348 /* If the separator is the first character. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100349 if (ptr == text && *(ptr+1) != '\0') {
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100350 if (strl2llrc_dotted(ptr+1, strlen(ptr+1), &pattern->val.range.max) != 0) {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100351 memprintf(err, "'%s' is not a valid dotted number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100352 return 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100353 }
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100354 pattern->val.range.min_set = 0;
355 pattern->val.range.max_set = 1;
356 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100357 }
358
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100359 /* If separator is the last character. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100360 if (ptr == &text[strlen(text)-1]) {
361 if (strl2llrc_dotted(text, ptr-text, &pattern->val.range.min) != 0) {
362 memprintf(err, "'%s' is not a valid dotted number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100363 return 0;
364 }
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100365 pattern->val.range.min_set = 1;
366 pattern->val.range.max_set = 0;
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100367 return 1;
368 }
369
370 /* Else, parse two numbers. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100371 if (strl2llrc_dotted(text, ptr-text, &pattern->val.range.min) != 0) {
372 memprintf(err, "'%s' is not a valid dotted number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100373 return 0;
374 }
375 if (strl2llrc_dotted(ptr+1, strlen(ptr+1), &pattern->val.range.max) != 0) {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100376 memprintf(err, "'%s' is not a valid dotted number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100377 return 0;
378 }
379 if (pattern->val.range.min > pattern->val.range.max) {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100380 memprintf(err, "'%s' is not a valid dotted number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100381 return 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100382 }
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100383 pattern->val.range.min_set = 1;
384 pattern->val.range.max_set = 1;
385 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100386}
387
388/* Parse an IP address and an optional mask in the form addr[/mask].
389 * The addr may either be an IPv4 address or a hostname. The mask
390 * may either be a dotted mask or a number of bits. Returns 1 if OK,
391 * otherwise 0. NOTE: IP address patterns are typed (IPV4/IPV6).
392 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200393int pat_parse_ip(const char *text, struct pattern *pattern, int mflags, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100394{
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200395 if (str2net(text, !(mflags & PAT_MF_NO_DNS) && (global.mode & MODE_STARTING),
Thierry FOURNIERfc7ac7b2014-02-11 15:23:04 +0100396 &pattern->val.ipv4.addr, &pattern->val.ipv4.mask)) {
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100397 pattern->type = SMP_T_IPV4;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100398 return 1;
399 }
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100400 else if (str62net(text, &pattern->val.ipv6.addr, &pattern->val.ipv6.mask)) {
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100401 pattern->type = SMP_T_IPV6;
402 return 1;
403 }
404 else {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100405 memprintf(err, "'%s' is not a valid IPv4 or IPv6 address", text);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100406 return 0;
407 }
408}
409
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100410/*
411 *
412 * These functions are exported and may be used by any other component.
413 *
414 * This fucntion just take a sample <smp> and check if this sample match
415 * with the pattern <pattern>. This fucntion return just PAT_MATCH or
416 * PAT_NOMATCH.
417 *
418 */
419
420/* always return false */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100421struct pattern *pat_match_nothing(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100422{
Thierry FOURNIERe5978bf2014-03-17 19:53:10 +0100423 if (smp->data.uint) {
424 if (fill) {
425 static_pattern.smp = NULL;
426 static_pattern.ref = NULL;
Thierry FOURNIERe5978bf2014-03-17 19:53:10 +0100427 static_pattern.type = 0;
428 static_pattern.ptr.str = NULL;
429 }
430 return &static_pattern;
431 }
432 else
433 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100434}
435
436
437/* NB: For two strings to be identical, it is required that their lengths match */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100438struct pattern *pat_match_str(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100439{
440 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100441 struct ebmb_node *node;
442 char prev;
443 struct pattern_tree *elt;
444 struct pattern_list *lst;
445 struct pattern *pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100446
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100447 /* Lookup a string in the expression's pattern tree. */
448 if (!eb_is_empty(&expr->pattern_tree)) {
449 /* we may have to force a trailing zero on the test pattern */
450 prev = smp->data.str.str[smp->data.str.len];
451 if (prev)
452 smp->data.str.str[smp->data.str.len] = '\0';
453 node = ebst_lookup(&expr->pattern_tree, smp->data.str.str);
454 if (prev)
455 smp->data.str.str[smp->data.str.len] = prev;
456
457 if (node) {
458 if (fill) {
459 elt = ebmb_entry(node, struct pattern_tree, node);
460 static_pattern.smp = elt->smp;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +0100461 static_pattern.ref = elt->ref;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200462 static_pattern.sflags = PAT_SF_TREE;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100463 static_pattern.type = SMP_T_STR;
464 static_pattern.ptr.str = (char *)elt->node.key;
465 }
466 return &static_pattern;
467 }
468 }
469
470 /* look in the list */
471 list_for_each_entry(lst, &expr->patterns, list) {
472 pattern = &lst->pat;
473
474 if (pattern->len != smp->data.str.len)
475 continue;
476
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200477 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100478 if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) == 0) ||
479 (!icase && strncmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) == 0))
480 return pattern;
481 }
482
483 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100484}
485
486/* NB: For two binaries buf to be identical, it is required that their lengths match */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100487struct pattern *pat_match_bin(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100488{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100489 struct pattern_list *lst;
490 struct pattern *pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100491
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100492 /* Look in the list. */
493 list_for_each_entry(lst, &expr->patterns, list) {
494 pattern = &lst->pat;
495
496 if (pattern->len != smp->data.str.len)
497 continue;
498
499 if (memcmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) == 0)
500 return pattern;
501 }
502
503 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100504}
505
506/* Executes a regex. It temporarily changes the data to add a trailing zero,
507 * and restores the previous character when leaving.
508 */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100509struct pattern *pat_match_reg(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100510{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100511 struct pattern_list *lst;
512 struct pattern *pattern;
513
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100514 /* look in the list */
515 list_for_each_entry(lst, &expr->patterns, list) {
516 pattern = &lst->pat;
517
Thierry FOURNIERb8f980c2014-06-11 13:59:05 +0200518 if (regex_exec2(pattern->ptr.reg, smp->data.str.str, smp->data.str.len))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100519 return pattern;
520 }
521 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100522}
523
524/* Checks that the pattern matches the beginning of the tested string. */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100525struct pattern *pat_match_beg(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100526{
527 int icase;
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +0200528 struct ebmb_node *node;
529 char prev;
530 struct pattern_tree *elt;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100531 struct pattern_list *lst;
532 struct pattern *pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100533
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +0200534 /* Lookup a string in the expression's pattern tree. */
535 if (!eb_is_empty(&expr->pattern_tree)) {
536 /* we may have to force a trailing zero on the test pattern */
537 prev = smp->data.str.str[smp->data.str.len];
538 if (prev)
539 smp->data.str.str[smp->data.str.len] = '\0';
540 node = ebmb_lookup_longest(&expr->pattern_tree, smp->data.str.str);
541 if (prev)
542 smp->data.str.str[smp->data.str.len] = prev;
543
544 if (node) {
545 if (fill) {
546 elt = ebmb_entry(node, struct pattern_tree, node);
547 static_pattern.smp = elt->smp;
548 static_pattern.ref = elt->ref;
549 static_pattern.sflags = PAT_SF_TREE;
550 static_pattern.type = SMP_T_STR;
551 static_pattern.ptr.str = (char *)elt->node.key;
552 }
553 return &static_pattern;
554 }
555 }
556
557 /* look in the list */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100558 list_for_each_entry(lst, &expr->patterns, list) {
559 pattern = &lst->pat;
560
561 if (pattern->len > smp->data.str.len)
562 continue;
563
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200564 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100565 if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str, pattern->len) != 0) ||
566 (!icase && strncmp(pattern->ptr.str, smp->data.str.str, pattern->len) != 0))
567 continue;
568
569 return pattern;
570 }
571 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100572}
573
574/* Checks that the pattern matches the end of the tested string. */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100575struct pattern *pat_match_end(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100576{
577 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100578 struct pattern_list *lst;
579 struct pattern *pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100580
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100581 list_for_each_entry(lst, &expr->patterns, list) {
582 pattern = &lst->pat;
583
584 if (pattern->len > smp->data.str.len)
585 continue;
586
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200587 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100588 if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str + smp->data.str.len - pattern->len, pattern->len) != 0) ||
589 (!icase && strncmp(pattern->ptr.str, smp->data.str.str + smp->data.str.len - pattern->len, pattern->len) != 0))
590 continue;
591
592 return pattern;
593 }
594 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100595}
596
597/* Checks that the pattern is included inside the tested string.
598 * NB: Suboptimal, should be rewritten using a Boyer-Moore method.
599 */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100600struct pattern *pat_match_sub(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100601{
602 int icase;
603 char *end;
604 char *c;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100605 struct pattern_list *lst;
606 struct pattern *pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100607
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100608 list_for_each_entry(lst, &expr->patterns, list) {
609 pattern = &lst->pat;
610
611 if (pattern->len > smp->data.str.len)
612 continue;
613
614 end = smp->data.str.str + smp->data.str.len - pattern->len;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200615 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100616 if (icase) {
617 for (c = smp->data.str.str; c <= end; c++) {
618 if (tolower(*c) != tolower(*pattern->ptr.str))
619 continue;
620 if (strncasecmp(pattern->ptr.str, c, pattern->len) == 0)
621 return pattern;
622 }
623 } else {
624 for (c = smp->data.str.str; c <= end; c++) {
625 if (*c != *pattern->ptr.str)
626 continue;
627 if (strncmp(pattern->ptr.str, c, pattern->len) == 0)
628 return pattern;
629 }
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100630 }
631 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100632 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100633}
634
635/* This one is used by other real functions. It checks that the pattern is
636 * included inside the tested string, but enclosed between the specified
637 * delimiters or at the beginning or end of the string. The delimiters are
638 * provided as an unsigned int made by make_4delim() and match up to 4 different
639 * delimiters. Delimiters are stripped at the beginning and end of the pattern.
640 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200641static int match_word(struct sample *smp, struct pattern *pattern, int mflags, unsigned int delimiters)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100642{
643 int may_match, icase;
644 char *c, *end;
645 char *ps;
646 int pl;
647
648 pl = pattern->len;
649 ps = pattern->ptr.str;
650
651 while (pl > 0 && is_delimiter(*ps, delimiters)) {
652 pl--;
653 ps++;
654 }
655
656 while (pl > 0 && is_delimiter(ps[pl - 1], delimiters))
657 pl--;
658
659 if (pl > smp->data.str.len)
660 return PAT_NOMATCH;
661
662 may_match = 1;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200663 icase = mflags & PAT_MF_IGNORE_CASE;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100664 end = smp->data.str.str + smp->data.str.len - pl;
665 for (c = smp->data.str.str; c <= end; c++) {
666 if (is_delimiter(*c, delimiters)) {
667 may_match = 1;
668 continue;
669 }
670
671 if (!may_match)
672 continue;
673
674 if (icase) {
675 if ((tolower(*c) == tolower(*ps)) &&
676 (strncasecmp(ps, c, pl) == 0) &&
677 (c == end || is_delimiter(c[pl], delimiters)))
678 return PAT_MATCH;
679 } else {
680 if ((*c == *ps) &&
681 (strncmp(ps, c, pl) == 0) &&
682 (c == end || is_delimiter(c[pl], delimiters)))
683 return PAT_MATCH;
684 }
685 may_match = 0;
686 }
687 return PAT_NOMATCH;
688}
689
690/* Checks that the pattern is included inside the tested string, but enclosed
691 * between the delimiters '?' or '/' or at the beginning or end of the string.
692 * Delimiters at the beginning or end of the pattern are ignored.
693 */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100694struct pattern *pat_match_dir(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100695{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100696 struct pattern_list *lst;
697 struct pattern *pattern;
698
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100699 list_for_each_entry(lst, &expr->patterns, list) {
700 pattern = &lst->pat;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200701 if (match_word(smp, pattern, expr->mflags, make_4delim('/', '?', '?', '?')))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100702 return pattern;
703 }
704 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100705}
706
707/* Checks that the pattern is included inside the tested string, but enclosed
708 * between the delmiters '/', '?', '.' or ":" or at the beginning or end of
709 * the string. Delimiters at the beginning or end of the pattern are ignored.
710 */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100711struct pattern *pat_match_dom(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100712{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100713 struct pattern_list *lst;
714 struct pattern *pattern;
715
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100716 list_for_each_entry(lst, &expr->patterns, list) {
717 pattern = &lst->pat;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200718 if (match_word(smp, pattern, expr->mflags, make_4delim('/', '?', '.', ':')))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100719 return pattern;
720 }
721 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100722}
723
724/* Checks that the integer in <test> is included between min and max */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100725struct pattern *pat_match_int(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100726{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100727 struct pattern_list *lst;
728 struct pattern *pattern;
729
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100730 list_for_each_entry(lst, &expr->patterns, list) {
731 pattern = &lst->pat;
732 if ((!pattern->val.range.min_set || pattern->val.range.min <= smp->data.uint) &&
733 (!pattern->val.range.max_set || smp->data.uint <= pattern->val.range.max))
734 return pattern;
735 }
736 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100737}
738
739/* Checks that the length of the pattern in <test> is included between min and max */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100740struct pattern *pat_match_len(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100741{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100742 struct pattern_list *lst;
743 struct pattern *pattern;
744
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100745 list_for_each_entry(lst, &expr->patterns, list) {
746 pattern = &lst->pat;
747 if ((!pattern->val.range.min_set || pattern->val.range.min <= smp->data.str.len) &&
748 (!pattern->val.range.max_set || smp->data.str.len <= pattern->val.range.max))
749 return pattern;
750 }
751 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100752}
753
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100754struct pattern *pat_match_ip(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100755{
756 unsigned int v4; /* in network byte order */
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100757 struct in6_addr tmp6;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100758 struct in_addr *s;
759 struct ebmb_node *node;
760 struct pattern_tree *elt;
761 struct pattern_list *lst;
762 struct pattern *pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100763
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100764 /* The input sample is IPv4. Try to match in the trees. */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100765 if (smp->type == SMP_T_IPV4) {
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100766 /* Lookup an IPv4 address in the expression's pattern tree using
767 * the longest match method.
768 */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100769 s = &smp->data.ipv4;
770 node = ebmb_lookup_longest(&expr->pattern_tree, &s->s_addr);
771 if (node) {
772 if (fill) {
773 elt = ebmb_entry(node, struct pattern_tree, node);
774 static_pattern.smp = elt->smp;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +0100775 static_pattern.ref = elt->ref;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200776 static_pattern.sflags = PAT_SF_TREE;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100777 static_pattern.type = SMP_T_IPV4;
778 memcpy(&static_pattern.val.ipv4.addr.s_addr, elt->node.key, 4);
779 if (!cidr2dotted(elt->node.node.pfx, &static_pattern.val.ipv4.mask))
780 return NULL;
781 }
782 return &static_pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100783 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100784
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100785 /* The IPv4 sample dont match the IPv4 tree. Convert the IPv4
786 * sample address to IPv6 with the mapping method using the ::ffff:
787 * prefix, and try to lookup in the IPv6 tree.
788 */
789 memset(&tmp6, 0, 10);
790 *(uint16_t*)&tmp6.s6_addr[10] = htons(0xffff);
791 *(uint32_t*)&tmp6.s6_addr[12] = smp->data.ipv4.s_addr;
792 node = ebmb_lookup_longest(&expr->pattern_tree_2, &tmp6);
793 if (node) {
794 if (fill) {
795 elt = ebmb_entry(node, struct pattern_tree, node);
796 static_pattern.smp = elt->smp;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +0100797 static_pattern.ref = elt->ref;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200798 static_pattern.sflags = PAT_SF_TREE;
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100799 static_pattern.type = SMP_T_IPV6;
800 memcpy(&static_pattern.val.ipv6.addr, elt->node.key, 16);
801 static_pattern.val.ipv6.mask = elt->node.node.pfx;
802 }
803 return &static_pattern;
804 }
805 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100806
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100807 /* The input sample is IPv6. Try to match in the trees. */
808 if (smp->type == SMP_T_IPV6) {
809 /* Lookup an IPv6 address in the expression's pattern tree using
810 * the longest match method.
811 */
812 node = ebmb_lookup_longest(&expr->pattern_tree_2, &smp->data.ipv6);
813 if (node) {
814 if (fill) {
815 elt = ebmb_entry(node, struct pattern_tree, node);
816 static_pattern.smp = elt->smp;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +0100817 static_pattern.ref = elt->ref;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200818 static_pattern.sflags = PAT_SF_TREE;
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100819 static_pattern.type = SMP_T_IPV6;
820 memcpy(&static_pattern.val.ipv6.addr, elt->node.key, 16);
821 static_pattern.val.ipv6.mask = elt->node.node.pfx;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100822 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100823 return &static_pattern;
824 }
825
826 /* Try to convert 6 to 4 when the start of the ipv6 address match the
827 * following forms :
828 * - ::ffff:ip:v4 (ipv4 mapped)
829 * - ::0000:ip:v4 (old ipv4 mapped)
830 * - 2002:ip:v4:: (6to4)
831 */
832 if ((*(uint32_t*)&smp->data.ipv6.s6_addr[0] == 0 &&
833 *(uint32_t*)&smp->data.ipv6.s6_addr[4] == 0 &&
834 (*(uint32_t*)&smp->data.ipv6.s6_addr[8] == 0 ||
835 *(uint32_t*)&smp->data.ipv6.s6_addr[8] == htonl(0xFFFF))) ||
836 *(uint16_t*)&smp->data.ipv6.s6_addr[0] == htons(0x2002)) {
837 if (*(uint32_t*)&smp->data.ipv6.s6_addr[0] == 0)
838 v4 = *(uint32_t*)&smp->data.ipv6.s6_addr[12];
839 else
840 v4 = htonl((ntohs(*(uint16_t*)&smp->data.ipv6.s6_addr[2]) << 16) +
841 ntohs(*(uint16_t*)&smp->data.ipv6.s6_addr[4]));
842
843 /* Lookup an IPv4 address in the expression's pattern tree using the longest
844 * match method.
845 */
846 node = ebmb_lookup_longest(&expr->pattern_tree, &v4);
847 if (node) {
848 if (fill) {
849 elt = ebmb_entry(node, struct pattern_tree, node);
850 static_pattern.smp = elt->smp;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +0100851 static_pattern.ref = elt->ref;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200852 static_pattern.sflags = PAT_SF_TREE;
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100853 static_pattern.type = SMP_T_IPV4;
854 memcpy(&static_pattern.val.ipv4.addr.s_addr, elt->node.key, 4);
855 if (!cidr2dotted(elt->node.node.pfx, &static_pattern.val.ipv4.mask))
856 return NULL;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100857 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100858 return &static_pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100859 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100860 }
861 }
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100862
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100863 /* Lookup in the list. the list contain only IPv4 patterns */
864 list_for_each_entry(lst, &expr->patterns, list) {
865 pattern = &lst->pat;
866
867 /* The input sample is IPv4, use it as is. */
868 if (smp->type == SMP_T_IPV4) {
869 v4 = smp->data.ipv4.s_addr;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100870 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100871 else if (smp->type == SMP_T_IPV6) {
872 /* v4 match on a V6 sample. We want to check at least for
873 * the following forms :
874 * - ::ffff:ip:v4 (ipv4 mapped)
875 * - ::0000:ip:v4 (old ipv4 mapped)
876 * - 2002:ip:v4:: (6to4)
877 */
878 if (*(uint32_t*)&smp->data.ipv6.s6_addr[0] == 0 &&
879 *(uint32_t*)&smp->data.ipv6.s6_addr[4] == 0 &&
880 (*(uint32_t*)&smp->data.ipv6.s6_addr[8] == 0 ||
881 *(uint32_t*)&smp->data.ipv6.s6_addr[8] == htonl(0xFFFF))) {
882 v4 = *(uint32_t*)&smp->data.ipv6.s6_addr[12];
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100883 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100884 else if (*(uint16_t*)&smp->data.ipv6.s6_addr[0] == htons(0x2002)) {
885 v4 = htonl((ntohs(*(uint16_t*)&smp->data.ipv6.s6_addr[2]) << 16) +
886 ntohs(*(uint16_t*)&smp->data.ipv6.s6_addr[4]));
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100887 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100888 else
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100889 continue;
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100890 }
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100891
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100892 /* Check if the input sample match the current pattern. */
893 if (((v4 ^ pattern->val.ipv4.addr.s_addr) & pattern->val.ipv4.mask.s_addr) == 0)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100894 return pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100895 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100896 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100897}
898
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100899void free_pattern_tree(struct eb_root *root)
900{
901 struct eb_node *node, *next;
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +0100902 struct pattern_tree *elt;
Thierry FOURNIER3ce88c72013-12-09 11:29:46 +0100903
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100904 node = eb_first(root);
905 while (node) {
906 next = eb_next(node);
907 eb_delete(node);
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +0100908 elt = container_of(node, struct pattern_tree, node);
Thierry FOURNIERc64de3f2013-12-10 15:08:39 +0100909 free(elt->smp);
Thierry FOURNIER3ce88c72013-12-09 11:29:46 +0100910 free(elt);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100911 node = next;
912 }
913}
914
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +0100915void pat_prune_val(struct pattern_expr *expr)
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +0100916{
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +0100917 struct pattern_list *pat, *tmp;
918
919 list_for_each_entry_safe(pat, tmp, &expr->patterns, list) {
920 free(pat->pat.smp);
921 free(pat);
922 }
923
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +0100924 free_pattern_tree(&expr->pattern_tree);
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100925 free_pattern_tree(&expr->pattern_tree_2);
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +0100926 LIST_INIT(&expr->patterns);
927}
928
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +0100929void pat_prune_ptr(struct pattern_expr *expr)
930{
931 struct pattern_list *pat, *tmp;
932
933 list_for_each_entry_safe(pat, tmp, &expr->patterns, list) {
934 free(pat->pat.ptr.ptr);
935 free(pat->pat.smp);
936 free(pat);
937 }
938
939 free_pattern_tree(&expr->pattern_tree);
940 free_pattern_tree(&expr->pattern_tree_2);
941 LIST_INIT(&expr->patterns);
942}
943
944void pat_prune_reg(struct pattern_expr *expr)
945{
946 struct pattern_list *pat, *tmp;
947
948 list_for_each_entry_safe(pat, tmp, &expr->patterns, list) {
949 regex_free(pat->pat.ptr.ptr);
950 free(pat->pat.smp);
951 free(pat);
952 }
953
954 free_pattern_tree(&expr->pattern_tree);
955 free_pattern_tree(&expr->pattern_tree_2);
956 LIST_INIT(&expr->patterns);
957}
958
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100959/*
960 *
961 * The following functions are used for the pattern indexation
962 *
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100963 */
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100964
965int pat_idx_list_val(struct pattern_expr *expr, struct pattern *pat, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100966{
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100967 struct pattern_list *patl;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100968
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100969 /* allocate pattern */
970 patl = calloc(1, sizeof(*patl));
971 if (!patl) {
972 memprintf(err, "out of memory while indexing pattern");
Thierry FOURNIER972028f2014-01-23 17:53:31 +0100973 return 0;
974 }
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100975
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100976 /* duplicate pattern */
977 memcpy(&patl->pat, pat, sizeof(*pat));
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100978
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100979 /* chain pattern in the expression */
980 LIST_ADDQ(&expr->patterns, &patl->list);
981
982 /* that's ok */
983 return 1;
984}
985
986int pat_idx_list_ptr(struct pattern_expr *expr, struct pattern *pat, char **err)
987{
988 struct pattern_list *patl;
989
990 /* allocate pattern */
991 patl = calloc(1, sizeof(*patl));
Thierry FOURNIERe338a872015-02-06 17:50:55 +0100992 if (!patl) {
993 memprintf(err, "out of memory while indexing pattern");
Thierry FOURNIER972028f2014-01-23 17:53:31 +0100994 return 0;
Thierry FOURNIERe338a872015-02-06 17:50:55 +0100995 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100996
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100997 /* duplicate pattern */
998 memcpy(&patl->pat, pat, sizeof(*pat));
999 patl->pat.ptr.ptr = malloc(patl->pat.len);
1000 if (!patl->pat.ptr.ptr) {
1001 free(patl);
1002 memprintf(err, "out of memory while indexing pattern");
1003 return 0;
1004 }
1005 memcpy(patl->pat.ptr.ptr, pat->ptr.ptr, pat->len);
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001006
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001007 /* chain pattern in the expression */
1008 LIST_ADDQ(&expr->patterns, &patl->list);
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001009
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001010 /* that's ok */
1011 return 1;
1012}
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001013
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001014int pat_idx_list_str(struct pattern_expr *expr, struct pattern *pat, char **err)
1015{
1016 struct pattern_list *patl;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001017
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001018 /* allocate pattern */
1019 patl = calloc(1, sizeof(*patl));
1020 if (!patl) {
1021 memprintf(err, "out of memory while indexing pattern");
1022 return 0;
1023 }
1024
1025 /* duplicate pattern */
1026 memcpy(&patl->pat, pat, sizeof(*pat));
1027 patl->pat.ptr.str = malloc(patl->pat.len + 1);
1028 if (!patl->pat.ptr.str) {
1029 free(patl);
1030 memprintf(err, "out of memory while indexing pattern");
1031 return 0;
1032 }
1033 memcpy(patl->pat.ptr.ptr, pat->ptr.ptr, pat->len);
1034 patl->pat.ptr.str[patl->pat.len] = '\0';
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001035
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001036 /* chain pattern in the expression */
1037 LIST_ADDQ(&expr->patterns, &patl->list);
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001038
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001039 /* that's ok */
1040 return 1;
1041}
1042
1043int pat_idx_list_reg(struct pattern_expr *expr, struct pattern *pat, char **err)
1044{
1045 struct pattern_list *patl;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001046
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001047 /* allocate pattern */
1048 patl = calloc(1, sizeof(*patl));
1049 if (!patl) {
1050 memprintf(err, "out of memory while indexing pattern");
1051 return 0;
Thierry FOURNIER972028f2014-01-23 17:53:31 +01001052 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001053
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001054 /* duplicate pattern */
1055 memcpy(&patl->pat, pat, sizeof(*pat));
1056
1057 /* allocate regex */
1058 patl->pat.ptr.reg = calloc(1, sizeof(*patl->pat.ptr.reg));
1059 if (!patl->pat.ptr.reg) {
1060 free(patl);
1061 memprintf(err, "out of memory while indexing pattern");
1062 return 0;
1063 }
1064
1065 /* compile regex */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02001066 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 +01001067 free(patl->pat.ptr.reg);
Dirkjan Bussink07fcaaa2014-04-28 22:57:16 +00001068 free(patl);
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001069 return 0;
1070 }
1071
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001072 /* chain pattern in the expression */
1073 LIST_ADDQ(&expr->patterns, &patl->list);
1074
1075 /* that's ok */
1076 return 1;
1077}
1078
1079int pat_idx_tree_ip(struct pattern_expr *expr, struct pattern *pat, char **err)
1080{
1081 unsigned int mask;
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +01001082 struct pattern_tree *node;
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001083
1084 /* Only IPv4 can be indexed */
1085 if (pat->type == SMP_T_IPV4) {
Thierry FOURNIER972028f2014-01-23 17:53:31 +01001086 /* in IPv4 case, check if the mask is contiguous so that we can
1087 * insert the network into the tree. A continuous mask has only
1088 * ones on the left. This means that this mask + its lower bit
1089 * added once again is null.
1090 */
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001091 mask = ntohl(pat->val.ipv4.mask.s_addr);
1092 if (mask + (mask & -mask) == 0) {
1093 mask = mask ? 33 - flsnz(mask & -mask) : 0; /* equals cidr value */
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001094
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001095 /* node memory allocation */
1096 node = calloc(1, sizeof(*node) + 4);
1097 if (!node) {
1098 memprintf(err, "out of memory while loading pattern");
1099 return 0;
1100 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001101
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001102 /* copy the pointer to sample associated to this node */
1103 node->smp = pat->smp;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01001104 node->ref = pat->ref;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001105
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001106 /* FIXME: insert <addr>/<mask> into the tree here */
1107 memcpy(node->node.key, &pat->val.ipv4.addr, 4); /* network byte order */
1108 node->node.node.pfx = mask;
Thierry FOURNIER31db4ae2014-01-30 00:27:15 +01001109
1110 /* Insert the entry. */
1111 ebmb_insert_prefix(&expr->pattern_tree, &node->node, 4);
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001112
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001113 /* that's ok */
1114 return 1;
1115 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001116 else {
1117 /* If the mask is not contiguous, just add the pattern to the list */
1118 return pat_idx_list_val(expr, pat, err);
1119 }
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001120 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001121 else if (pat->type == SMP_T_IPV6) {
1122 /* IPv6 also can be indexed */
1123 node = calloc(1, sizeof(*node) + 16);
1124 if (!node) {
1125 memprintf(err, "out of memory while loading pattern");
1126 return 0;
1127 }
1128
1129 /* copy the pointer to sample associated to this node */
1130 node->smp = pat->smp;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01001131 node->ref = pat->ref;
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001132
1133 /* FIXME: insert <addr>/<mask> into the tree here */
1134 memcpy(node->node.key, &pat->val.ipv6.addr, 16); /* network byte order */
1135 node->node.node.pfx = pat->val.ipv6.mask;
Thierry FOURNIER31db4ae2014-01-30 00:27:15 +01001136
1137 /* Insert the entry. */
1138 ebmb_insert_prefix(&expr->pattern_tree_2, &node->node, 16);
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001139
1140 /* that's ok */
1141 return 1;
1142 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001143
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001144 return 0;
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001145}
1146
1147int pat_idx_tree_str(struct pattern_expr *expr, struct pattern *pat, char **err)
1148{
1149 int len;
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +01001150 struct pattern_tree *node;
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001151
1152 /* Only string can be indexed */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001153 if (pat->type != SMP_T_STR) {
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001154 memprintf(err, "internal error: string expected, but the type is '%s'",
1155 smp_to_type[pat->type]);
1156 return 0;
Thierry FOURNIER972028f2014-01-23 17:53:31 +01001157 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001158
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001159 /* If the flag PAT_F_IGNORE_CASE is set, we cannot use trees */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02001160 if (expr->mflags & PAT_MF_IGNORE_CASE)
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001161 return pat_idx_list_str(expr, pat, err);
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001162
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001163 /* Process the key len */
1164 len = strlen(pat->ptr.str) + 1;
1165
1166 /* node memory allocation */
1167 node = calloc(1, sizeof(*node) + len);
1168 if (!node) {
1169 memprintf(err, "out of memory while loading pattern");
1170 return 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001171 }
1172
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001173 /* copy the pointer to sample associated to this node */
1174 node->smp = pat->smp;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01001175 node->ref = pat->ref;
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001176
1177 /* copy the string */
1178 memcpy(node->node.key, pat->ptr.str, len);
1179
1180 /* index the new node */
Thierry FOURNIER31db4ae2014-01-30 00:27:15 +01001181 ebst_insert(&expr->pattern_tree, &node->node);
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001182
1183 /* that's ok */
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001184 return 1;
1185}
1186
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +02001187int pat_idx_tree_pfx(struct pattern_expr *expr, struct pattern *pat, char **err)
1188{
1189 int len;
1190 struct pattern_tree *node;
1191
1192 /* Only string can be indexed */
1193 if (pat->type != SMP_T_STR) {
1194 memprintf(err, "internal error: string expected, but the type is '%s'",
1195 smp_to_type[pat->type]);
1196 return 0;
1197 }
1198
1199 /* If the flag PAT_F_IGNORE_CASE is set, we cannot use trees */
1200 if (expr->mflags & PAT_MF_IGNORE_CASE)
1201 return pat_idx_list_str(expr, pat, err);
1202
1203 /* Process the key len */
1204 len = strlen(pat->ptr.str);
1205
1206 /* node memory allocation */
1207 node = calloc(1, sizeof(*node) + len + 1);
1208 if (!node) {
1209 memprintf(err, "out of memory while loading pattern");
1210 return 0;
1211 }
1212
1213 /* copy the pointer to sample associated to this node */
1214 node->smp = pat->smp;
1215 node->ref = pat->ref;
1216
1217 /* copy the string and the trailing zero */
1218 memcpy(node->node.key, pat->ptr.str, len + 1);
1219 node->node.node.pfx = len * 8;
1220
1221 /* index the new node */
1222 ebmb_insert_prefix(&expr->pattern_tree, &node->node, len);
1223
1224 /* that's ok */
1225 return 1;
1226}
1227
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001228void pat_del_list_val(struct pattern_expr *expr, struct pat_ref_elt *ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001229{
1230 struct pattern_list *pat;
1231 struct pattern_list *safe;
1232
1233 list_for_each_entry_safe(pat, safe, &expr->patterns, list) {
1234 /* Check equality. */
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001235 if (pat->pat.ref != ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001236 continue;
1237
1238 /* Delete and free entry. */
1239 LIST_DEL(&pat->list);
1240 free(pat->pat.smp);
1241 free(pat);
1242 }
1243}
1244
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001245void pat_del_tree_ip(struct pattern_expr *expr, struct pat_ref_elt *ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001246{
1247 struct ebmb_node *node, *next_node;
1248 struct pattern_tree *elt;
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001249
1250 /* browse each node of the tree for IPv4 addresses. */
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001251 for (node = ebmb_first(&expr->pattern_tree), next_node = node ? ebmb_next(node) : NULL;
1252 node;
1253 node = next_node, next_node = node ? ebmb_next(node) : NULL) {
1254 /* Extract container of the tree node. */
1255 elt = container_of(node, struct pattern_tree, node);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001256
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001257 /* Check equality. */
1258 if (elt->ref != ref)
1259 continue;
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001260
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001261 /* Delete and free entry. */
1262 ebmb_delete(node);
1263 free(elt->smp);
1264 free(elt);
1265 }
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001266
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001267 /* Browse each node of the list for IPv4 addresses. */
1268 pat_del_list_val(expr, ref);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001269
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001270 /* browse each node of the tree for IPv6 addresses. */
1271 for (node = ebmb_first(&expr->pattern_tree_2), next_node = node ? ebmb_next(node) : NULL;
1272 node;
1273 node = next_node, next_node = node ? ebmb_next(node) : NULL) {
1274 /* Extract container of the tree node. */
1275 elt = container_of(node, struct pattern_tree, node);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001276
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001277 /* Check equality. */
1278 if (elt->ref != ref)
1279 continue;
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001280
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001281 /* Delete and free entry. */
1282 ebmb_delete(node);
1283 free(elt->smp);
1284 free(elt);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001285 }
1286}
1287
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001288void pat_del_list_ptr(struct pattern_expr *expr, struct pat_ref_elt *ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001289{
1290 struct pattern_list *pat;
1291 struct pattern_list *safe;
1292
1293 list_for_each_entry_safe(pat, safe, &expr->patterns, list) {
1294 /* Check equality. */
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001295 if (pat->pat.ref != ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001296 continue;
1297
1298 /* Delete and free entry. */
1299 LIST_DEL(&pat->list);
1300 free(pat->pat.ptr.ptr);
1301 free(pat->pat.smp);
1302 free(pat);
1303 }
1304}
1305
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001306void pat_del_tree_str(struct pattern_expr *expr, struct pat_ref_elt *ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001307{
1308 struct ebmb_node *node, *next_node;
1309 struct pattern_tree *elt;
1310
Thierry FOURNIER623401b2015-02-06 17:53:54 +01001311 /* If the flag PAT_F_IGNORE_CASE is set, we cannot use trees */
1312 if (expr->mflags & PAT_MF_IGNORE_CASE)
1313 return pat_del_list_ptr(expr, ref);
1314
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001315 /* browse each node of the tree. */
1316 for (node = ebmb_first(&expr->pattern_tree), next_node = node ? ebmb_next(node) : NULL;
1317 node;
1318 node = next_node, next_node = node ? ebmb_next(node) : NULL) {
1319 /* Extract container of the tree node. */
1320 elt = container_of(node, struct pattern_tree, node);
1321
1322 /* Check equality. */
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001323 if (elt->ref != ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001324 continue;
1325
1326 /* Delete and free entry. */
1327 ebmb_delete(node);
1328 free(elt->smp);
1329 free(elt);
1330 }
1331}
1332
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001333void pat_del_list_reg(struct pattern_expr *expr, struct pat_ref_elt *ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001334{
1335 struct pattern_list *pat;
1336 struct pattern_list *safe;
1337
1338 list_for_each_entry_safe(pat, safe, &expr->patterns, list) {
1339 /* Check equality. */
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001340 if (pat->pat.ref != ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001341 continue;
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001342
1343 /* Delete and free entry. */
1344 LIST_DEL(&pat->list);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001345 regex_free(pat->pat.ptr.ptr);
1346 free(pat->pat.smp);
1347 free(pat);
1348 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001349}
1350
1351void pattern_init_expr(struct pattern_expr *expr)
1352{
1353 LIST_INIT(&expr->patterns);
Thierry FOURNIER31db4ae2014-01-30 00:27:15 +01001354 expr->pattern_tree = EB_ROOT;
1355 expr->pattern_tree_2 = EB_ROOT;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001356}
1357
1358void pattern_init_head(struct pattern_head *head)
1359{
1360 LIST_INIT(&head->head);
1361}
1362
1363/* The following functions are relative to the management of the reference
1364 * lists. These lists are used to store the original pattern and associated
1365 * value as string form.
1366 *
1367 * This is used with modifiable ACL and MAPS
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001368 *
1369 * The pattern reference are stored with two identifiers: the unique_id and
1370 * the reference.
1371 *
1372 * The reference identify a file. Each file with the same name point to the
1373 * same reference. We can register many times one file. If the file is modified,
1374 * all his dependencies are also modified. The reference can be used with map or
1375 * acl.
1376 *
1377 * The unique_id identify inline acl. The unique id is unique for each acl.
1378 * You cannot force the same id in the configuration file, because this repoort
1379 * an error.
1380 *
1381 * A particular case appears if the filename is a number. In this case, the
1382 * unique_id is set with the number represented by the filename and the
1383 * reference is also set. This method prevent double unique_id.
1384 *
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001385 */
1386
1387/* This function lookup for reference. If the reference is found, they return
1388 * pointer to the struct pat_ref, else return NULL.
1389 */
1390struct pat_ref *pat_ref_lookup(const char *reference)
1391{
1392 struct pat_ref *ref;
1393
1394 list_for_each_entry(ref, &pattern_reference, list)
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001395 if (ref->reference && strcmp(reference, ref->reference) == 0)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001396 return ref;
1397 return NULL;
1398}
1399
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001400/* This function lookup for unique id. If the reference is found, they return
1401 * pointer to the struct pat_ref, else return NULL.
1402 */
1403struct pat_ref *pat_ref_lookupid(int unique_id)
1404{
1405 struct pat_ref *ref;
1406
1407 list_for_each_entry(ref, &pattern_reference, list)
1408 if (ref->unique_id == unique_id)
1409 return ref;
1410 return NULL;
1411}
1412
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001413/* This function remove all pattern matching the pointer <refelt> from
1414 * the the reference and from each expr member of the reference. This
1415 * function returns 1 if the deletion is done and return 0 is the entry
1416 * is not found.
1417 */
1418int pat_ref_delete_by_id(struct pat_ref *ref, struct pat_ref_elt *refelt)
1419{
1420 struct pattern_expr *expr;
1421 struct pat_ref_elt *elt, *safe;
1422
1423 /* delete pattern from reference */
1424 list_for_each_entry_safe(elt, safe, &ref->head, list) {
1425 if (elt == refelt) {
peter cai90f8b992015-10-07 00:07:43 -07001426 list_for_each_entry(expr, &ref->pat, list)
1427 pattern_delete(expr, elt);
1428
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001429 LIST_DEL(&elt->list);
1430 free(elt->sample);
1431 free(elt->pattern);
1432 free(elt);
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001433 return 1;
1434 }
1435 }
1436 return 0;
1437}
1438
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001439/* This function remove all pattern match <key> from the the reference
1440 * and from each expr member of the reference. This fucntion returns 1
1441 * if the deletion is done and return 0 is the entry is not found.
1442 */
1443int pat_ref_delete(struct pat_ref *ref, const char *key)
1444{
1445 struct pattern_expr *expr;
1446 struct pat_ref_elt *elt, *safe;
1447 int found = 0;
1448
1449 /* delete pattern from reference */
1450 list_for_each_entry_safe(elt, safe, &ref->head, list) {
1451 if (strcmp(key, elt->pattern) == 0) {
Dirkjan Bussink07fcaaa2014-04-28 22:57:16 +00001452 list_for_each_entry(expr, &ref->pat, list)
1453 pattern_delete(expr, elt);
1454
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001455 LIST_DEL(&elt->list);
1456 free(elt->sample);
1457 free(elt->pattern);
1458 free(elt);
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001459
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001460 found = 1;
1461 }
1462 }
1463
1464 if (!found)
1465 return 0;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001466 return 1;
1467}
1468
Baptiste Assmann953f74d2014-04-25 16:57:03 +02001469/*
1470 * find and return an element <elt> matching <key> in a reference <ref>
1471 * return NULL if not found
1472 */
1473struct pat_ref_elt *pat_ref_find_elt(struct pat_ref *ref, const char *key)
1474{
1475 struct pat_ref_elt *elt;
1476
1477 list_for_each_entry(elt, &ref->head, list) {
1478 if (strcmp(key, elt->pattern) == 0)
1479 return elt;
1480 }
1481
1482 return NULL;
1483}
1484
1485
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001486 /* This function modify the sample of the first pattern that match the <key>. */
1487static inline int pat_ref_set_elt(struct pat_ref *ref, struct pat_ref_elt *elt,
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001488 const char *value, char **err)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001489{
1490 struct pattern_expr *expr;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001491 struct sample_storage **smp;
1492 char *sample;
Thierry FOURNIER149e0fe2014-01-29 19:35:06 +01001493 struct sample_storage test;
1494
1495 /* Try all needed converters. */
1496 list_for_each_entry(expr, &ref->pat, list) {
1497 if (!expr->pat_head->parse_smp)
1498 continue;
1499
1500 if (!expr->pat_head->parse_smp(value, &test)) {
1501 memprintf(err, "unable to parse '%s'", value);
1502 return 0;
1503 }
1504 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001505
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001506 /* Modify pattern from reference. */
1507 sample = strdup(value);
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001508 if (!sample) {
1509 memprintf(err, "out of memory error");
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001510 return 0;
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001511 }
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001512 free(elt->sample);
1513 elt->sample = sample;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001514
Thierry FOURNIER149e0fe2014-01-29 19:35:06 +01001515 /* Load sample in each reference. All the conversion are tested
1516 * below, normally these calls dosn't fail.
1517 */
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001518 list_for_each_entry(expr, &ref->pat, list) {
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001519 if (!expr->pat_head->parse_smp)
1520 continue;
1521
1522 smp = pattern_find_smp(expr, elt);
Thierry FOURNIER149e0fe2014-01-29 19:35:06 +01001523 if (smp && *smp && !expr->pat_head->parse_smp(sample, *smp))
1524 *smp = NULL;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001525 }
1526
Thierry FOURNIER149e0fe2014-01-29 19:35:06 +01001527 return 1;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001528}
1529
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001530/* This function modify the sample of the first pattern that match the <key>. */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001531int 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 +01001532{
1533 struct pat_ref_elt *elt;
1534
1535 /* Look for pattern in the reference. */
1536 list_for_each_entry(elt, &ref->head, list) {
1537 if (elt == refelt) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001538 if (!pat_ref_set_elt(ref, elt, value, err))
1539 return 0;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001540 return 1;
1541 }
1542 }
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001543
1544 memprintf(err, "key or pattern not found");
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001545 return 0;
1546}
1547
1548/* This function modify the sample of the first pattern that match the <key>. */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001549int pat_ref_set(struct pat_ref *ref, const char *key, const char *value, char **err)
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001550{
1551 struct pat_ref_elt *elt;
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001552 int found = 0;
1553 char *_merr;
1554 char **merr;
1555
1556 if (err) {
1557 merr = &_merr;
1558 *merr = NULL;
1559 }
1560 else
1561 merr = NULL;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001562
1563 /* Look for pattern in the reference. */
1564 list_for_each_entry(elt, &ref->head, list) {
1565 if (strcmp(key, elt->pattern) == 0) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001566 if (!pat_ref_set_elt(ref, elt, value, merr)) {
1567 if (!found)
1568 *err = *merr;
1569 else {
1570 memprintf(err, "%s, %s", *err, *merr);
1571 free(*merr);
1572 *merr = NULL;
1573 }
1574 }
1575 found = 1;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001576 }
1577 }
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001578
1579 if (!found) {
1580 memprintf(err, "entry not found");
1581 return 0;
1582 }
1583 return 1;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001584}
1585
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001586/* This function create new reference. <ref> is the reference name.
1587 * <flags> are PAT_REF_*. /!\ The reference is not checked, and must
1588 * be unique. The user must check the reference with "pat_ref_lookup()"
1589 * before calling this function. If the fucntion fail, it return NULL,
1590 * else return new struct pat_ref.
1591 */
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001592struct pat_ref *pat_ref_new(const char *reference, const char *display, unsigned int flags)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001593{
1594 struct pat_ref *ref;
1595
1596 ref = malloc(sizeof(*ref));
1597 if (!ref)
1598 return NULL;
1599
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001600 if (display) {
1601 ref->display = strdup(display);
1602 if (!ref->display) {
1603 free(ref);
1604 return NULL;
1605 }
1606 }
1607 else
1608 ref->display = NULL;
1609
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001610 ref->reference = strdup(reference);
1611 if (!ref->reference) {
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001612 free(ref->display);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001613 free(ref);
1614 return NULL;
1615 }
1616
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001617 ref->flags = flags;
1618 ref->unique_id = -1;
1619
1620 LIST_INIT(&ref->head);
1621 LIST_INIT(&ref->pat);
1622
1623 LIST_ADDQ(&pattern_reference, &ref->list);
1624
1625 return ref;
1626}
1627
1628/* This function create new reference. <unique_id> is the unique id. If
1629 * the value of <unique_id> is -1, the unique id is calculated later.
1630 * <flags> are PAT_REF_*. /!\ The reference is not checked, and must
1631 * be unique. The user must check the reference with "pat_ref_lookup()"
1632 * or pat_ref_lookupid before calling this function. If the function
1633 * fail, it return NULL, else return new struct pat_ref.
1634 */
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001635struct pat_ref *pat_ref_newid(int unique_id, const char *display, unsigned int flags)
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001636{
1637 struct pat_ref *ref;
1638
1639 ref = malloc(sizeof(*ref));
1640 if (!ref)
1641 return NULL;
1642
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001643 if (display) {
1644 ref->display = strdup(display);
1645 if (!ref->display) {
1646 free(ref);
1647 return NULL;
1648 }
1649 }
1650 else
1651 ref->display = NULL;
1652
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001653 ref->reference = NULL;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001654 ref->flags = flags;
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001655 ref->unique_id = unique_id;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001656 LIST_INIT(&ref->head);
1657 LIST_INIT(&ref->pat);
1658
1659 LIST_ADDQ(&pattern_reference, &ref->list);
1660
1661 return ref;
1662}
1663
1664/* This function adds entry to <ref>. It can failed with memory error.
1665 * If the function fails, it returns 0.
1666 */
1667int pat_ref_append(struct pat_ref *ref, char *pattern, char *sample, int line)
1668{
1669 struct pat_ref_elt *elt;
1670
1671 elt = malloc(sizeof(*elt));
1672 if (!elt)
1673 return 0;
1674
1675 elt->line = line;
1676
1677 elt->pattern = strdup(pattern);
1678 if (!elt->pattern) {
1679 free(elt);
1680 return 0;
1681 }
1682
1683 if (sample) {
1684 elt->sample = strdup(sample);
1685 if (!elt->sample) {
1686 free(elt->pattern);
1687 free(elt);
1688 return 0;
1689 }
1690 }
1691 else
1692 elt->sample = NULL;
1693
1694 LIST_ADDQ(&ref->head, &elt->list);
1695
1696 return 1;
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001697}
1698
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001699/* This function create sample found in <elt>, parse the pattern also
1700 * found in <elt> and insert it in <expr>. The function copy <patflags>
1701 * in <expr>. If the function fails, it returns0 and <err> is filled.
1702 * In succes case, the function returns 1.
1703 */
1704static inline
1705int pat_ref_push(struct pat_ref_elt *elt, struct pattern_expr *expr,
1706 int patflags, char **err)
1707{
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001708 struct sample_storage *smp;
Thierry FOURNIERd25c8422014-01-28 15:34:35 +01001709 struct pattern pattern;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001710
1711 /* Create sample */
1712 if (elt->sample && expr->pat_head->parse_smp) {
1713 /* New sample. */
1714 smp = malloc(sizeof(*smp));
1715 if (!smp)
1716 return 0;
1717
1718 /* Parse value. */
1719 if (!expr->pat_head->parse_smp(elt->sample, smp)) {
1720 memprintf(err, "unable to parse '%s'", elt->sample);
1721 free(smp);
1722 return 0;
1723 }
1724
1725 }
1726 else
1727 smp = NULL;
1728
Thierry FOURNIERd25c8422014-01-28 15:34:35 +01001729 /* initialise pattern */
1730 memset(&pattern, 0, sizeof(pattern));
Thierry FOURNIERd25c8422014-01-28 15:34:35 +01001731 pattern.smp = smp;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01001732 pattern.ref = elt;
Thierry FOURNIERd25c8422014-01-28 15:34:35 +01001733
1734 /* parse pattern */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02001735 if (!expr->pat_head->parse(elt->pattern, &pattern, expr->mflags, err)) {
Thierry FOURNIERd25c8422014-01-28 15:34:35 +01001736 free(smp);
1737 return 0;
1738 }
1739
1740 /* index pattern */
1741 if (!expr->pat_head->index(expr, &pattern, err)) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001742 free(smp);
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001743 return 0;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001744 }
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001745
1746 return 1;
1747}
1748
Thierry FOURNIER31db4ae2014-01-30 00:27:15 +01001749/* This function adds entry to <ref>. It can failed with memory error. The new
1750 * entry is added at all the pattern_expr registered in this reference. The
1751 * function stop on the first error encountered. It returns 0 and err is
1752 * filled. If an error is encountered, the complete add operation is cancelled.
1753 * If the insertion is a success the function returns 1.
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001754 */
1755int pat_ref_add(struct pat_ref *ref,
1756 const char *pattern, const char *sample,
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02001757 char **err)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001758{
1759 struct pat_ref_elt *elt;
1760 struct pattern_expr *expr;
1761
1762 elt = malloc(sizeof(*elt));
1763 if (!elt) {
1764 memprintf(err, "out of memory error");
1765 return 0;
1766 }
1767
1768 elt->line = -1;
1769
1770 elt->pattern = strdup(pattern);
1771 if (!elt->pattern) {
1772 free(elt);
1773 memprintf(err, "out of memory error");
1774 return 0;
1775 }
1776
1777 if (sample) {
1778 elt->sample = strdup(sample);
1779 if (!elt->sample) {
1780 free(elt->pattern);
1781 free(elt);
1782 memprintf(err, "out of memory error");
1783 return 0;
1784 }
1785 }
1786 else
1787 elt->sample = NULL;
1788
1789 LIST_ADDQ(&ref->head, &elt->list);
1790
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001791 list_for_each_entry(expr, &ref->pat, list) {
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02001792 if (!pat_ref_push(elt, expr, 0, err)) {
Thierry FOURNIER31db4ae2014-01-30 00:27:15 +01001793 /* If the insertion fails, try to delete all the added entries. */
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001794 pat_ref_delete_by_id(ref, elt);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001795 return 0;
1796 }
1797 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001798 return 1;
1799}
1800
Thierry FOURNIER46006bd2014-03-21 21:45:15 +01001801/* This function prune <ref>, replace all reference by the references
1802 * of <replace>, and reindex all the news values.
1803 *
1804 * The pattern are loaded in best effort and the errors are ignored,
1805 * but writed in the logs.
1806 */
1807void pat_ref_reload(struct pat_ref *ref, struct pat_ref *replace)
1808{
1809 struct pattern_expr *expr;
1810 struct pat_ref_elt *elt;
1811 char *err = NULL;
1812
1813 pat_ref_prune(ref);
1814
1815 LIST_ADD(&replace->head, &ref->head);
1816 LIST_DEL(&replace->head);
1817
1818 list_for_each_entry(elt, &ref->head, list) {
1819 list_for_each_entry(expr, &ref->pat, list) {
1820 if (!pat_ref_push(elt, expr, 0, &err)) {
1821 send_log(NULL, LOG_NOTICE, "%s", err);
1822 free(err);
1823 err = NULL;
1824 }
1825 }
1826 }
1827}
1828
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001829/* This function prune all entries of <ref>. This function
1830 * prune the associated pattern_expr.
1831 */
1832void pat_ref_prune(struct pat_ref *ref)
1833{
1834 struct pat_ref_elt *elt, *safe;
1835 struct pattern_expr *expr;
1836
1837 list_for_each_entry_safe(elt, safe, &ref->head, list) {
1838 LIST_DEL(&elt->list);
1839 free(elt->pattern);
1840 free(elt->sample);
1841 free(elt);
1842 }
1843
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001844 list_for_each_entry(expr, &ref->pat, list)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001845 expr->pat_head->prune(expr);
1846}
1847
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001848/* This function lookup for existing reference <ref> in pattern_head <head>. */
1849struct pattern_expr *pattern_lookup_expr(struct pattern_head *head, struct pat_ref *ref)
1850{
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001851 struct pattern_expr_list *expr;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001852
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001853 list_for_each_entry(expr, &head->head, list)
1854 if (expr->expr->ref == ref)
1855 return expr->expr;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001856 return NULL;
1857}
1858
1859/* This function create new pattern_expr associated to the reference <ref>.
1860 * <ref> can be NULL. If an error is occured, the function returns NULL and
1861 * <err> is filled. Otherwise, the function returns new pattern_expr linked
1862 * with <head> and <ref>.
Thierry FOURNIERbae03ea2014-11-24 11:14:42 +01001863 *
1864 * The returned value can be a alredy filled pattern list, in this case the
1865 * flag <reuse> is set.
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001866 */
Thierry FOURNIERbae03ea2014-11-24 11:14:42 +01001867struct pattern_expr *pattern_new_expr(struct pattern_head *head, struct pat_ref *ref,
1868 char **err, int *reuse)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001869{
1870 struct pattern_expr *expr;
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001871 struct pattern_expr_list *list;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001872
Thierry FOURNIERbae03ea2014-11-24 11:14:42 +01001873 if (reuse)
1874 *reuse = 0;
1875
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001876 /* Memory and initialization of the chain element. */
1877 list = malloc(sizeof(*list));
1878 if (!list) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001879 memprintf(err, "out of memory");
1880 return NULL;
1881 }
1882
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001883 /* Look for existing similar expr. No that only the index, parse and
1884 * parse_smp function must be identical for having similar pattern.
1885 * The other function depends of theses first.
1886 */
1887 if (ref) {
1888 list_for_each_entry(expr, &ref->pat, list)
1889 if (expr->pat_head->index == head->index &&
1890 expr->pat_head->parse == head->parse &&
1891 expr->pat_head->parse_smp == head->parse_smp)
1892 break;
1893 if (&expr->list == &ref->pat)
1894 expr = NULL;
1895 }
1896 else
1897 expr = NULL;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001898
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001899 /* If no similar expr was found, we create new expr. */
1900 if (!expr) {
1901 /* Get a lot of memory for the expr struct. */
1902 expr = malloc(sizeof(*expr));
1903 if (!expr) {
1904 memprintf(err, "out of memory");
1905 return NULL;
1906 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001907
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001908 /* Initialize this new expr. */
1909 pattern_init_expr(expr);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001910
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001911 /* This new pattern expression reference one of his heads. */
1912 expr->pat_head = head;
1913
1914 /* Link with ref, or to self to facilitate LIST_DEL() */
1915 if (ref)
1916 LIST_ADDQ(&ref->pat, &expr->list);
1917 else
1918 LIST_INIT(&expr->list);
1919
1920 expr->ref = ref;
1921
1922 /* We must free this pattern if it is no more used. */
1923 list->do_free = 1;
1924 }
1925 else {
1926 /* If the pattern used already exists, it is already linked
1927 * with ref and we must not free it.
1928 */
1929 list->do_free = 0;
Thierry FOURNIERbae03ea2014-11-24 11:14:42 +01001930 if (reuse)
1931 *reuse = 1;
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001932 }
1933
1934 /* The new list element reference the pattern_expr. */
1935 list->expr = expr;
1936
1937 /* Link the list element with the pattern_head. */
1938 LIST_ADDQ(&head->head, &list->list);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001939 return expr;
1940}
1941
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001942/* Reads patterns from a file. If <err_msg> is non-NULL, an error message will
1943 * be returned there on errors and the caller will have to free it.
Thierry FOURNIER39bef452014-01-29 13:29:45 +01001944 *
1945 * The file contains one key + value per line. Lines which start with '#' are
1946 * ignored, just like empty lines. Leading tabs/spaces are stripped. The key is
1947 * then the first "word" (series of non-space/tabs characters), and the value is
1948 * what follows this series of space/tab till the end of the line excluding
1949 * trailing spaces/tabs.
1950 *
1951 * Example :
1952 *
1953 * # this is a comment and is ignored
1954 * 62.212.114.60 1wt.eu \n
1955 * <-><-----------><---><----><---->
1956 * | | | | `--- trailing spaces ignored
1957 * | | | `-------- value
1958 * | | `--------------- middle spaces ignored
1959 * | `------------------------ key
1960 * `-------------------------------- leading spaces ignored
1961 *
1962 * Return non-zero in case of succes, otherwise 0.
1963 */
1964int pat_ref_read_from_file_smp(struct pat_ref *ref, const char *filename, char **err)
1965{
1966 FILE *file;
1967 char *c;
1968 int ret = 0;
1969 int line = 0;
1970 char *key_beg;
1971 char *key_end;
1972 char *value_beg;
1973 char *value_end;
1974
1975 file = fopen(filename, "r");
1976 if (!file) {
1977 memprintf(err, "failed to open pattern file <%s>", filename);
1978 return 0;
1979 }
1980
1981 /* now parse all patterns. The file may contain only one pattern
1982 * followed by one value per line. The start spaces, separator spaces
1983 * and and spaces are stripped. Each can contain comment started by '#'
1984 */
1985 while (fgets(trash.str, trash.size, file) != NULL) {
1986 line++;
1987 c = trash.str;
1988
1989 /* ignore lines beginning with a dash */
1990 if (*c == '#')
1991 continue;
1992
1993 /* strip leading spaces and tabs */
1994 while (*c == ' ' || *c == '\t')
1995 c++;
1996
1997 /* empty lines are ignored too */
1998 if (*c == '\0' || *c == '\r' || *c == '\n')
1999 continue;
2000
2001 /* look for the end of the key */
2002 key_beg = c;
2003 while (*c && *c != ' ' && *c != '\t' && *c != '\n' && *c != '\r')
2004 c++;
2005
2006 key_end = c;
2007
2008 /* strip middle spaces and tabs */
2009 while (*c == ' ' || *c == '\t')
2010 c++;
2011
2012 /* look for the end of the value, it is the end of the line */
2013 value_beg = c;
2014 while (*c && *c != '\n' && *c != '\r')
2015 c++;
2016 value_end = c;
2017
2018 /* trim possibly trailing spaces and tabs */
2019 while (value_end > value_beg && (value_end[-1] == ' ' || value_end[-1] == '\t'))
2020 value_end--;
2021
2022 /* set final \0 and check entries */
2023 *key_end = '\0';
2024 *value_end = '\0';
2025
2026 /* insert values */
2027 if (!pat_ref_append(ref, key_beg, value_beg, line)) {
2028 memprintf(err, "out of memory");
2029 goto out_close;
2030 }
2031 }
2032
2033 /* succes */
2034 ret = 1;
2035
2036 out_close:
2037 fclose(file);
2038 return ret;
2039}
2040
2041/* Reads patterns from a file. If <err_msg> is non-NULL, an error message will
2042 * be returned there on errors and the caller will have to free it.
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002043 */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002044int pat_ref_read_from_file(struct pat_ref *ref, const char *filename, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002045{
2046 FILE *file;
2047 char *c;
2048 char *arg;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002049 int ret = 0;
2050 int line = 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002051
2052 file = fopen(filename, "r");
2053 if (!file) {
2054 memprintf(err, "failed to open pattern file <%s>", filename);
2055 return 0;
2056 }
2057
2058 /* now parse all patterns. The file may contain only one pattern per
2059 * line. If the line contains spaces, they will be part of the pattern.
2060 * The pattern stops at the first CR, LF or EOF encountered.
2061 */
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002062 while (fgets(trash.str, trash.size, file) != NULL) {
2063 line++;
2064 c = trash.str;
2065
2066 /* ignore lines beginning with a dash */
2067 if (*c == '#')
2068 continue;
2069
2070 /* strip leading spaces and tabs */
2071 while (*c == ' ' || *c == '\t')
2072 c++;
2073
2074
2075 arg = c;
2076 while (*c && *c != '\n' && *c != '\r')
2077 c++;
2078 *c = 0;
2079
2080 /* empty lines are ignored too */
2081 if (c == arg)
2082 continue;
2083
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002084 if (!pat_ref_append(ref, arg, NULL, line)) {
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002085 memprintf(err, "out of memory when loading patterns from file <%s>", filename);
2086 goto out_close;
2087 }
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002088 }
2089
2090 ret = 1; /* success */
2091
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002092 out_close:
2093 fclose(file);
2094 return ret;
2095}
2096
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002097int pattern_read_from_file(struct pattern_head *head, unsigned int refflags,
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002098 const char *filename, int patflags, int load_smp,
Thierry FOURNIER94580c92014-02-11 14:36:45 +01002099 char **err, const char *file, int line)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002100{
2101 struct pat_ref *ref;
2102 struct pattern_expr *expr;
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002103 struct pat_ref_elt *elt;
Willy Tarreaubad3c6f2014-11-26 13:17:03 +01002104 int reuse = 0;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002105
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002106 /* Lookup for the existing reference. */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002107 ref = pat_ref_lookup(filename);
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002108
2109 /* If the reference doesn't exists, create it and load associated file. */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002110 if (!ref) {
Thierry FOURNIER94580c92014-02-11 14:36:45 +01002111 chunk_printf(&trash,
2112 "pattern loaded from file '%s' used by %s at file '%s' line %d",
2113 filename, refflags & PAT_REF_MAP ? "map" : "acl", file, line);
2114
2115 ref = pat_ref_new(filename, trash.str, refflags);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002116 if (!ref) {
2117 memprintf(err, "out of memory");
2118 return 0;
2119 }
2120
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002121 if (load_smp) {
Thierry FOURNIERc0bd9102014-01-29 12:32:58 +01002122 ref->flags |= PAT_REF_SMP;
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002123 if (!pat_ref_read_from_file_smp(ref, filename, err))
2124 return 0;
2125 }
2126 else {
2127 if (!pat_ref_read_from_file(ref, filename, err))
2128 return 0;
2129 }
2130 }
2131 else {
Thierry FOURNIERc0bd9102014-01-29 12:32:58 +01002132 /* The reference already exists, check the map compatibility. */
2133
2134 /* If the load require samples and the flag PAT_REF_SMP is not set,
2135 * the reference doesn't contain sample, and cannot be used.
2136 */
2137 if (load_smp) {
2138 if (!(ref->flags & PAT_REF_SMP)) {
2139 memprintf(err, "The file \"%s\" is already used as one column file "
2140 "and cannot be used by as two column file.",
2141 filename);
2142 return 0;
2143 }
2144 }
2145 else {
2146 /* The load doesn't require samples. If the flag PAT_REF_SMP is
2147 * set, the reference contains a sample, and cannot be used.
2148 */
2149 if (ref->flags & PAT_REF_SMP) {
2150 memprintf(err, "The file \"%s\" is already used as two column file "
2151 "and cannot be used by as one column file.",
2152 filename);
2153 return 0;
2154 }
2155 }
2156
Thierry FOURNIER94580c92014-02-11 14:36:45 +01002157 /* Extends display */
2158 chunk_printf(&trash, "%s", ref->display);
2159 chunk_appendf(&trash, ", by %s at file '%s' line %d",
2160 refflags & PAT_REF_MAP ? "map" : "acl", file, line);
2161 free(ref->display);
2162 ref->display = strdup(trash.str);
2163 if (!ref->display) {
2164 memprintf(err, "out of memory");
2165 return 0;
2166 }
2167
Thierry FOURNIERc0bd9102014-01-29 12:32:58 +01002168 /* Merge flags. */
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002169 ref->flags |= refflags;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002170 }
2171
2172 /* Now, we can loading patterns from the reference. */
2173
2174 /* Lookup for existing reference in the head. If the reference
2175 * doesn't exists, create it.
2176 */
2177 expr = pattern_lookup_expr(head, ref);
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02002178 if (!expr || (expr->mflags != patflags)) {
Thierry FOURNIERbae03ea2014-11-24 11:14:42 +01002179 expr = pattern_new_expr(head, ref, err, &reuse);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002180 if (!expr)
2181 return 0;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02002182 expr->mflags = patflags;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002183 }
2184
Thierry FOURNIERbae03ea2014-11-24 11:14:42 +01002185 /* The returned expression may be not empty, because the function
2186 * "pattern_new_expr" lookup for similar pattern list and can
2187 * reuse a already filled pattern list. In this case, we can not
2188 * reload the patterns.
2189 */
2190 if (reuse)
2191 return 1;
2192
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002193 /* Load reference content in the pattern expression. */
2194 list_for_each_entry(elt, &ref->head, list) {
2195 if (!pat_ref_push(elt, expr, patflags, err)) {
2196 if (elt->line > 0)
2197 memprintf(err, "%s at line %d of file '%s'",
2198 *err, elt->line, filename);
2199 return 0;
2200 }
2201 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002202
2203 return 1;
2204}
2205
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01002206/* This function executes a pattern match on a sample. It applies pattern <expr>
2207 * to sample <smp>. The function returns NULL if the sample dont match. It returns
2208 * non-null if the sample match. If <fill> is true and the sample match, the
2209 * function returns the matched pattern. In many cases, this pattern can be a
2210 * static buffer.
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002211 */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002212struct pattern *pattern_exec_match(struct pattern_head *head, struct sample *smp, int fill)
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002213{
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002214 struct pattern_expr_list *list;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002215 struct pattern *pat;
2216
2217 if (!head->match) {
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01002218 if (fill) {
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01002219 static_pattern.smp = NULL;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01002220 static_pattern.ref = NULL;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02002221 static_pattern.sflags = 0;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01002222 static_pattern.type = SMP_T_UINT;
2223 static_pattern.val.i = 1;
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01002224 }
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01002225 return &static_pattern;
2226 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002227
Thierry FOURNIER5d344082014-01-27 14:19:53 +01002228 /* convert input to string */
2229 if (!sample_convert(smp, head->expect_type))
2230 return NULL;
2231
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002232 list_for_each_entry(list, &head->head, list) {
2233 pat = head->match(smp, list->expr, fill);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002234 if (pat)
2235 return pat;
2236 }
2237 return NULL;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002238}
2239
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01002240/* This function prune the pattern expression. */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002241void pattern_prune(struct pattern_head *head)
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01002242{
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002243 struct pattern_expr_list *list, *safe;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002244
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002245 list_for_each_entry_safe(list, safe, &head->head, list) {
2246 LIST_DEL(&list->list);
2247 if (list->do_free) {
2248 LIST_DEL(&list->expr->list);
2249 head->prune(list->expr);
2250 free(list->expr);
2251 }
2252 free(list);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002253 }
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01002254}
2255
Thierry FOURNIER55d0b102014-01-15 11:25:26 +01002256/* This function lookup for a pattern matching the <key> and return a
2257 * pointer to a pointer of the sample stoarge. If the <key> dont match,
2258 * the function returns NULL. If the key cannot be parsed, the function
2259 * fill <err>.
2260 */
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01002261struct sample_storage **pattern_find_smp(struct pattern_expr *expr, struct pat_ref_elt *ref)
Thierry FOURNIER55d0b102014-01-15 11:25:26 +01002262{
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01002263 struct ebmb_node *node;
2264 struct pattern_tree *elt;
2265 struct pattern_list *pat;
Thierry FOURNIER55d0b102014-01-15 11:25:26 +01002266
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01002267 for (node = ebmb_first(&expr->pattern_tree);
2268 node;
2269 node = ebmb_next(node)) {
2270 elt = container_of(node, struct pattern_tree, node);
2271 if (elt->ref == ref)
2272 return &elt->smp;
2273 }
2274
2275 for (node = ebmb_first(&expr->pattern_tree_2);
2276 node;
2277 node = ebmb_next(node)) {
2278 elt = container_of(node, struct pattern_tree, node);
2279 if (elt->ref == ref)
2280 return &elt->smp;
2281 }
2282
2283 list_for_each_entry(pat, &expr->patterns, list)
2284 if (pat->pat.ref == ref)
2285 return &pat->pat.smp;
2286
2287 return NULL;
Thierry FOURNIER55d0b102014-01-15 11:25:26 +01002288}
2289
Thierry FOURNIERb1136502014-01-15 11:38:49 +01002290/* This function search all the pattern matching the <key> and delete it.
2291 * If the parsing of the input key fails, the function returns 0 and the
2292 * <err> is filled, else return 1;
2293 */
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01002294int pattern_delete(struct pattern_expr *expr, struct pat_ref_elt *ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01002295{
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01002296 expr->pat_head->delete(expr, ref);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01002297 return 1;
2298}
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002299
2300/* This function finalize the configuration parsing. Its set all the
2301 * automatic ids
2302 */
2303void pattern_finalize_config(void)
2304{
2305 int i = 0;
2306 struct pat_ref *ref, *ref2, *ref3;
2307 struct list pr = LIST_HEAD_INIT(pr);
2308
2309 list_for_each_entry(ref, &pattern_reference, list) {
2310 if (ref->unique_id == -1) {
2311 /* Look for the first free id. */
2312 while (1) {
2313 list_for_each_entry(ref2, &pattern_reference, list) {
2314 if (ref2->unique_id == i) {
2315 i++;
2316 break;
2317 }
2318 }
Willy Tarreau3b786962014-04-26 12:37:25 +02002319 if (&ref2->list == &pattern_reference)
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002320 break;
2321 }
2322
2323 /* Uses the unique id and increment it for the next entry. */
2324 ref->unique_id = i;
2325 i++;
2326 }
2327 }
2328
2329 /* This sort the reference list by id. */
2330 list_for_each_entry_safe(ref, ref2, &pattern_reference, list) {
2331 LIST_DEL(&ref->list);
2332 list_for_each_entry(ref3, &pr, list) {
2333 if (ref->unique_id < ref3->unique_id) {
2334 LIST_ADDQ(&ref3->list, &ref->list);
2335 break;
2336 }
2337 }
2338 if (&ref3->list == &pr)
2339 LIST_ADDQ(&pr, &ref->list);
2340 }
2341
2342 /* swap root */
2343 LIST_ADD(&pr, &pattern_reference);
2344 LIST_DEL(&pr);
2345}