blob: 796384dcfd664f716158941ff0fda05f894278d0 [file] [log] [blame]
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001/*
2 * Pattern management functions.
3 *
4 * Copyright 2000-2013 Willy Tarreau <w@1wt.eu>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <ctype.h>
14#include <stdio.h>
15
16#include <common/config.h>
17#include <common/standard.h>
18
19#include <types/global.h>
20#include <types/pattern.h>
21
22#include <proto/pattern.h>
Thierry FOURNIERe3ded592013-12-06 15:36:54 +010023#include <proto/sample.h>
Thierry FOURNIERed66c292013-11-28 11:05:19 +010024
25#include <ebsttree.h>
26
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010027char *pat_match_names[PAT_MATCH_NUM] = {
28 [PAT_MATCH_FOUND] = "found",
29 [PAT_MATCH_BOOL] = "bool",
30 [PAT_MATCH_INT] = "int",
31 [PAT_MATCH_IP] = "ip",
32 [PAT_MATCH_BIN] = "bin",
33 [PAT_MATCH_LEN] = "len",
34 [PAT_MATCH_STR] = "str",
35 [PAT_MATCH_BEG] = "beg",
36 [PAT_MATCH_SUB] = "sub",
37 [PAT_MATCH_DIR] = "dir",
38 [PAT_MATCH_DOM] = "dom",
39 [PAT_MATCH_END] = "end",
40 [PAT_MATCH_REG] = "reg",
Thierry FOURNIERed66c292013-11-28 11:05:19 +010041};
42
Thierry FOURNIERedc15c32013-12-13 15:36:59 +010043int (*pat_parse_fcts[PAT_MATCH_NUM])(const char *, struct pattern *, char **) = {
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010044 [PAT_MATCH_FOUND] = pat_parse_nothing,
45 [PAT_MATCH_BOOL] = pat_parse_nothing,
46 [PAT_MATCH_INT] = pat_parse_int,
47 [PAT_MATCH_IP] = pat_parse_ip,
48 [PAT_MATCH_BIN] = pat_parse_bin,
Thierry FOURNIERcc0e0b32013-12-06 16:56:40 +010049 [PAT_MATCH_LEN] = pat_parse_len,
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010050 [PAT_MATCH_STR] = pat_parse_str,
51 [PAT_MATCH_BEG] = pat_parse_str,
52 [PAT_MATCH_SUB] = pat_parse_str,
53 [PAT_MATCH_DIR] = pat_parse_str,
54 [PAT_MATCH_DOM] = pat_parse_str,
55 [PAT_MATCH_END] = pat_parse_str,
56 [PAT_MATCH_REG] = pat_parse_reg,
Thierry FOURNIERed66c292013-11-28 11:05:19 +010057};
58
Thierry FOURNIERb9b08462013-12-13 15:12:32 +010059int (*pat_index_fcts[PAT_MATCH_NUM])(struct pattern_expr *, struct pattern *, char **) = {
60 [PAT_MATCH_FOUND] = pat_idx_list_val,
61 [PAT_MATCH_BOOL] = pat_idx_list_val,
62 [PAT_MATCH_INT] = pat_idx_list_val,
63 [PAT_MATCH_IP] = pat_idx_tree_ip,
64 [PAT_MATCH_BIN] = pat_idx_list_ptr,
65 [PAT_MATCH_LEN] = pat_idx_list_val,
66 [PAT_MATCH_STR] = pat_idx_tree_str,
67 [PAT_MATCH_BEG] = pat_idx_list_str,
68 [PAT_MATCH_SUB] = pat_idx_list_str,
69 [PAT_MATCH_DIR] = pat_idx_list_str,
70 [PAT_MATCH_DOM] = pat_idx_list_str,
71 [PAT_MATCH_END] = pat_idx_list_str,
72 [PAT_MATCH_REG] = pat_idx_list_reg,
73};
74
Thierry FOURNIERb1136502014-01-15 11:38:49 +010075void (*pat_delete_fcts[PAT_MATCH_NUM])(struct pattern_expr *, struct pattern *) = {
76 [PAT_MATCH_FOUND] = pat_del_list_val,
77 [PAT_MATCH_BOOL] = pat_del_list_val,
78 [PAT_MATCH_INT] = pat_del_list_val,
79 [PAT_MATCH_IP] = pat_del_tree_ip,
80 [PAT_MATCH_BIN] = pat_del_list_ptr,
81 [PAT_MATCH_LEN] = pat_del_list_val,
82 [PAT_MATCH_STR] = pat_del_tree_str,
83 [PAT_MATCH_BEG] = pat_del_list_str,
84 [PAT_MATCH_SUB] = pat_del_list_str,
85 [PAT_MATCH_DIR] = pat_del_list_str,
86 [PAT_MATCH_DOM] = pat_del_list_str,
87 [PAT_MATCH_END] = pat_del_list_str,
88 [PAT_MATCH_REG] = pat_del_list_reg,
89};
90
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010091struct pattern *(*pat_match_fcts[PAT_MATCH_NUM])(struct sample *, struct pattern_expr *, int) = {
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010092 [PAT_MATCH_FOUND] = NULL,
93 [PAT_MATCH_BOOL] = pat_match_nothing,
94 [PAT_MATCH_INT] = pat_match_int,
95 [PAT_MATCH_IP] = pat_match_ip,
96 [PAT_MATCH_BIN] = pat_match_bin,
97 [PAT_MATCH_LEN] = pat_match_len,
98 [PAT_MATCH_STR] = pat_match_str,
99 [PAT_MATCH_BEG] = pat_match_beg,
100 [PAT_MATCH_SUB] = pat_match_sub,
101 [PAT_MATCH_DIR] = pat_match_dir,
102 [PAT_MATCH_DOM] = pat_match_dom,
103 [PAT_MATCH_END] = pat_match_end,
104 [PAT_MATCH_REG] = pat_match_reg,
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100105};
106
Thierry FOURNIERe3ded592013-12-06 15:36:54 +0100107/* Just used for checking configuration compatibility */
108int pat_match_types[PAT_MATCH_NUM] = {
109 [PAT_MATCH_FOUND] = SMP_T_UINT,
110 [PAT_MATCH_BOOL] = SMP_T_UINT,
111 [PAT_MATCH_INT] = SMP_T_UINT,
112 [PAT_MATCH_IP] = SMP_T_ADDR,
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100113 [PAT_MATCH_BIN] = SMP_T_BIN,
114 [PAT_MATCH_LEN] = SMP_T_STR,
115 [PAT_MATCH_STR] = SMP_T_STR,
116 [PAT_MATCH_BEG] = SMP_T_STR,
117 [PAT_MATCH_SUB] = SMP_T_STR,
118 [PAT_MATCH_DIR] = SMP_T_STR,
119 [PAT_MATCH_DOM] = SMP_T_STR,
120 [PAT_MATCH_END] = SMP_T_STR,
121 [PAT_MATCH_REG] = SMP_T_STR,
Thierry FOURNIERe3ded592013-12-06 15:36:54 +0100122};
123
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +0100124/* this struct is used to return information */
125static struct pattern static_pattern;
126
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100127/*
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100128 *
129 * The following functions are not exported and are used by internals process
130 * of pattern matching
131 *
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100132 */
133
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100134/* Free data allocated by pat_parse_reg */
135static void pat_free_reg(void *ptr)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100136{
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100137 regex_free(ptr);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100138}
139
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100140/* Background: Fast way to find a zero byte in a word
141 * http://graphics.stanford.edu/~seander/bithacks.html#ZeroInWord
142 * hasZeroByte = (v - 0x01010101UL) & ~v & 0x80808080UL;
143 *
144 * To look for 4 different byte values, xor the word with those bytes and
145 * then check for zero bytes:
146 *
147 * v = (((unsigned char)c * 0x1010101U) ^ delimiter)
148 * where <delimiter> is the 4 byte values to look for (as an uint)
149 * and <c> is the character that is being tested
150 */
151static inline unsigned int is_delimiter(unsigned char c, unsigned int mask)
152{
153 mask ^= (c * 0x01010101); /* propagate the char to all 4 bytes */
154 return (mask - 0x01010101) & ~mask & 0x80808080U;
155}
156
157static inline unsigned int make_4delim(unsigned char d1, unsigned char d2, unsigned char d3, unsigned char d4)
158{
159 return d1 << 24 | d2 << 16 | d3 << 8 | d4;
160}
161
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100162
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100163/*
164 *
165 * These functions are exported and may be used by any other component.
166 *
167 * The following functions are used for parsing pattern matching
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100168 * input value. The <text> contain the string to be parsed. <pattern>
169 * must be a preallocated pattern. The pat_parse_* functions fill this
170 * structure with the parsed value. <usage> can be PAT_U_COMPILE or
171 * PAT_U_LOOKUP. If the value PAT_U_COMPILE is used memory is allocated
172 * for filling the pattern. If the value PAT_U_LOOKUP is set, the parser
173 * use "trash" or return pointers to the input strings. In both cases,
174 * the caller must use the value PAT_U_LOOKUP with caution. <err> is
175 * filled with an error message built with memprintf() function.
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100176 *
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100177 * In succes case, the pat_parse_* function return 1. If the function
178 * fail, it returns 0 and <err> is filled.
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100179 *
180 */
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100181
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100182/* ignore the current line */
Thierry FOURNIERedc15c32013-12-13 15:36:59 +0100183int pat_parse_nothing(const char *text, struct pattern *pattern, char **err)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100184{
185 return 1;
186}
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100187
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100188/* Parse a string. It is allocated and duplicated. */
Thierry FOURNIERedc15c32013-12-13 15:36:59 +0100189int pat_parse_str(const char *text, struct pattern *pattern, char **err)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100190{
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100191 pattern->type = SMP_T_STR;
192 pattern->expect_type = SMP_T_STR;
Thierry FOURNIERedc15c32013-12-13 15:36:59 +0100193 pattern->ptr.str = (char *)text;
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100194 pattern->len = strlen(text);
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100195 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100196}
197
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100198/* Parse a binary written in hexa. It is allocated. */
Thierry FOURNIERedc15c32013-12-13 15:36:59 +0100199int pat_parse_bin(const char *text, struct pattern *pattern, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100200{
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100201 struct chunk *trash;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100202
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100203 pattern->type = SMP_T_BIN;
204 pattern->expect_type = SMP_T_BIN;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100205 trash = get_trash_chunk();
206 pattern->len = trash->size;
207 pattern->ptr.str = trash->str;
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100208 return !!parse_binary(text, &pattern->ptr.str, &pattern->len, err);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100209}
210
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100211/* Parse a regex. It is allocated. */
Thierry FOURNIERedc15c32013-12-13 15:36:59 +0100212int pat_parse_reg(const char *text, struct pattern *pattern, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100213{
Thierry FOURNIER0b2fe4a2013-12-06 20:33:50 +0100214 struct chunk *trash;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100215
Thierry FOURNIERedc15c32013-12-13 15:36:59 +0100216 trash = get_trash_chunk();
217 if (trash->size < sizeof(*pattern->ptr.reg)) {
218 memprintf(err, "no space avalaible in the buffer. expect %d, provides %d",
219 (int)sizeof(*pattern->ptr.reg), trash->size);
220 return 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100221 }
222
Thierry FOURNIERedc15c32013-12-13 15:36:59 +0100223 pattern->ptr.reg = (struct my_regex *)trash->str;
224 pattern->ptr.reg->regstr = (char *)text;
225 pattern->freeptrbuf = NULL;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100226
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100227 pattern->expect_type = SMP_T_STR;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100228 return 1;
229}
230
231/* Parse a range of positive integers delimited by either ':' or '-'. If only
232 * one integer is read, it is set as both min and max. An operator may be
233 * specified as the prefix, among this list of 5 :
234 *
235 * 0:eq, 1:gt, 2:ge, 3:lt, 4:le
236 *
237 * The default operator is "eq". It supports range matching. Ranges are
238 * rejected for other operators. The operator may be changed at any time.
239 * The operator is stored in the 'opaque' argument.
240 *
241 * If err is non-NULL, an error message will be returned there on errors and
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100242 * the caller will have to free it. The function returns zero on error, and
243 * non-zero on success.
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100244 *
245 */
Thierry FOURNIERedc15c32013-12-13 15:36:59 +0100246int pat_parse_int(const char *text, struct pattern *pattern, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100247{
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100248 const char *ptr = text;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100249
250 pattern->type = SMP_T_UINT;
Thierry FOURNIERcc0e0b32013-12-06 16:56:40 +0100251 pattern->expect_type = SMP_T_UINT;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100252
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100253 /* Empty string is not valid */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100254 if (!*text)
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100255 goto not_valid_range;
256
257 /* Search ':' or '-' separator. */
258 while (*ptr != '\0' && *ptr != ':' && *ptr != '-')
259 ptr++;
260
261 /* If separator not found. */
262 if (!*ptr) {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100263 if (strl2llrc(text, ptr - text, &pattern->val.range.min) != 0) {
264 memprintf(err, "'%s' is not a number", text);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100265 return 0;
266 }
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100267 pattern->val.range.max = pattern->val.range.min;
268 pattern->val.range.min_set = 1;
269 pattern->val.range.max_set = 1;
270 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100271 }
272
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100273 /* If the separator is the first character. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100274 if (ptr == text && *(ptr + 1) != '\0') {
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100275 if (strl2llrc(ptr + 1, strlen(ptr + 1), &pattern->val.range.max) != 0)
276 goto not_valid_range;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100277
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100278 pattern->val.range.min_set = 0;
279 pattern->val.range.max_set = 1;
280 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100281 }
282
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100283 /* If separator is the last character. */
284 if (*(ptr + 1) == '\0') {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100285 if (strl2llrc(text, ptr - text, &pattern->val.range.min) != 0)
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100286 goto not_valid_range;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100287
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100288 pattern->val.range.min_set = 1;
289 pattern->val.range.max_set = 0;
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100290 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100291 }
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100292
293 /* Else, parse two numbers. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100294 if (strl2llrc(text, ptr - text, &pattern->val.range.min) != 0)
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100295 goto not_valid_range;
296
297 if (strl2llrc(ptr + 1, strlen(ptr + 1), &pattern->val.range.max) != 0)
298 goto not_valid_range;
299
300 if (pattern->val.range.min > pattern->val.range.max)
301 goto not_valid_range;
302
303 pattern->val.range.min_set = 1;
304 pattern->val.range.max_set = 1;
305 return 1;
306
307 not_valid_range:
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100308 memprintf(err, "'%s' is not a valid number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100309 return 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100310}
311
Thierry FOURNIERedc15c32013-12-13 15:36:59 +0100312int pat_parse_len(const char *text, struct pattern *pattern, char **err)
Thierry FOURNIERcc0e0b32013-12-06 16:56:40 +0100313{
314 int ret;
315
Thierry FOURNIERedc15c32013-12-13 15:36:59 +0100316 ret = pat_parse_int(text, pattern, err);
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100317 pattern->expect_type = SMP_T_STR;
Thierry FOURNIERcc0e0b32013-12-06 16:56:40 +0100318 return ret;
319}
320
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100321/* Parse a range of positive 2-component versions delimited by either ':' or
322 * '-'. The version consists in a major and a minor, both of which must be
323 * smaller than 65536, because internally they will be represented as a 32-bit
324 * integer.
325 * If only one version is read, it is set as both min and max. Just like for
326 * pure integers, an operator may be specified as the prefix, among this list
327 * of 5 :
328 *
329 * 0:eq, 1:gt, 2:ge, 3:lt, 4:le
330 *
331 * The default operator is "eq". It supports range matching. Ranges are
332 * rejected for other operators. The operator may be changed at any time.
333 * The operator is stored in the 'opaque' argument. This allows constructs
334 * such as the following one :
335 *
336 * acl obsolete_ssl ssl_req_proto lt 3
337 * acl unsupported_ssl ssl_req_proto gt 3.1
338 * acl valid_ssl ssl_req_proto 3.0-3.1
339 *
340 */
Thierry FOURNIERedc15c32013-12-13 15:36:59 +0100341int pat_parse_dotted_ver(const char *text, struct pattern *pattern, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100342{
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100343 const char *ptr = text;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100344
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100345 pattern->type = SMP_T_UINT;
346 pattern->expect_type = SMP_T_UINT;
347
348 /* Search ':' or '-' separator. */
349 while (*ptr != '\0' && *ptr != ':' && *ptr != '-')
350 ptr++;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100351
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100352 /* If separator not found. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100353 if (*ptr == '\0' && ptr > text) {
354 if (strl2llrc_dotted(text, ptr-text, &pattern->val.range.min) != 0) {
355 memprintf(err, "'%s' is not a dotted number", text);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100356 return 0;
357 }
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100358 pattern->val.range.max = pattern->val.range.min;
359 pattern->val.range.min_set = 1;
360 pattern->val.range.max_set = 1;
361 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100362 }
363
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100364 /* If the separator is the first character. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100365 if (ptr == text && *(ptr+1) != '\0') {
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100366 if (strl2llrc_dotted(ptr+1, strlen(ptr+1), &pattern->val.range.max) != 0) {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100367 memprintf(err, "'%s' is not a valid dotted number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100368 return 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100369 }
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100370 pattern->val.range.min_set = 0;
371 pattern->val.range.max_set = 1;
372 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100373 }
374
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100375 /* If separator is the last character. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100376 if (ptr == &text[strlen(text)-1]) {
377 if (strl2llrc_dotted(text, ptr-text, &pattern->val.range.min) != 0) {
378 memprintf(err, "'%s' is not a valid dotted number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100379 return 0;
380 }
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100381 pattern->val.range.min_set = 1;
382 pattern->val.range.max_set = 0;
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100383 return 1;
384 }
385
386 /* Else, parse two numbers. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100387 if (strl2llrc_dotted(text, ptr-text, &pattern->val.range.min) != 0) {
388 memprintf(err, "'%s' is not a valid dotted number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100389 return 0;
390 }
391 if (strl2llrc_dotted(ptr+1, strlen(ptr+1), &pattern->val.range.max) != 0) {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100392 memprintf(err, "'%s' is not a valid dotted number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100393 return 0;
394 }
395 if (pattern->val.range.min > pattern->val.range.max) {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100396 memprintf(err, "'%s' is not a valid dotted number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100397 return 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100398 }
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100399 pattern->val.range.min_set = 1;
400 pattern->val.range.max_set = 1;
401 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100402}
403
404/* Parse an IP address and an optional mask in the form addr[/mask].
405 * The addr may either be an IPv4 address or a hostname. The mask
406 * may either be a dotted mask or a number of bits. Returns 1 if OK,
407 * otherwise 0. NOTE: IP address patterns are typed (IPV4/IPV6).
408 */
Thierry FOURNIERedc15c32013-12-13 15:36:59 +0100409int pat_parse_ip(const char *text, struct pattern *pattern, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100410{
Thierry FOURNIERcc0e0b32013-12-06 16:56:40 +0100411 pattern->expect_type = SMP_T_ADDR;
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100412 if (str2net(text, &pattern->val.ipv4.addr, &pattern->val.ipv4.mask)) {
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100413 pattern->type = SMP_T_IPV4;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100414 return 1;
415 }
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100416 else if (str62net(text, &pattern->val.ipv6.addr, &pattern->val.ipv6.mask)) {
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100417 pattern->type = SMP_T_IPV6;
418 return 1;
419 }
420 else {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100421 memprintf(err, "'%s' is not a valid IPv4 or IPv6 address", text);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100422 return 0;
423 }
424}
425
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100426/*
427 *
428 * These functions are exported and may be used by any other component.
429 *
430 * This fucntion just take a sample <smp> and check if this sample match
431 * with the pattern <pattern>. This fucntion return just PAT_MATCH or
432 * PAT_NOMATCH.
433 *
434 */
435
436/* always return false */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100437struct pattern *pat_match_nothing(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100438{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100439 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100440}
441
442
443/* NB: For two strings to be identical, it is required that their lengths match */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100444struct pattern *pat_match_str(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100445{
446 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100447 struct ebmb_node *node;
448 char prev;
449 struct pattern_tree *elt;
450 struct pattern_list *lst;
451 struct pattern *pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100452
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100453 /* convert input to string */
454 if (!sample_convert(smp, SMP_T_STR))
455 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100456
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100457 /* Lookup a string in the expression's pattern tree. */
458 if (!eb_is_empty(&expr->pattern_tree)) {
459 /* we may have to force a trailing zero on the test pattern */
460 prev = smp->data.str.str[smp->data.str.len];
461 if (prev)
462 smp->data.str.str[smp->data.str.len] = '\0';
463 node = ebst_lookup(&expr->pattern_tree, smp->data.str.str);
464 if (prev)
465 smp->data.str.str[smp->data.str.len] = prev;
466
467 if (node) {
468 if (fill) {
469 elt = ebmb_entry(node, struct pattern_tree, node);
470 static_pattern.smp = elt->smp;
471 static_pattern.flags = PAT_F_TREE;
472 static_pattern.type = SMP_T_STR;
473 static_pattern.ptr.str = (char *)elt->node.key;
474 }
475 return &static_pattern;
476 }
477 }
478
479 /* look in the list */
480 list_for_each_entry(lst, &expr->patterns, list) {
481 pattern = &lst->pat;
482
483 if (pattern->len != smp->data.str.len)
484 continue;
485
486 icase = pattern->flags & PAT_F_IGNORE_CASE;
487 if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) == 0) ||
488 (!icase && strncmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) == 0))
489 return pattern;
490 }
491
492 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100493}
494
495/* NB: For two binaries buf to be identical, it is required that their lengths match */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100496struct pattern *pat_match_bin(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100497{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100498 struct pattern_list *lst;
499 struct pattern *pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100500
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100501 /* Convert input to binary. */
502 if (!sample_convert(smp, SMP_T_BIN))
503 return NULL;
504
505 /* Look in the list. */
506 list_for_each_entry(lst, &expr->patterns, list) {
507 pattern = &lst->pat;
508
509 if (pattern->len != smp->data.str.len)
510 continue;
511
512 if (memcmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) == 0)
513 return pattern;
514 }
515
516 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100517}
518
519/* Executes a regex. It temporarily changes the data to add a trailing zero,
520 * and restores the previous character when leaving.
521 */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100522struct pattern *pat_match_reg(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100523{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100524 struct pattern_list *lst;
525 struct pattern *pattern;
526
527 /* convert input to string */
528 if (!sample_convert(smp, SMP_T_STR))
529 return NULL;
530
531 /* look in the list */
532 list_for_each_entry(lst, &expr->patterns, list) {
533 pattern = &lst->pat;
534
535 if (regex_exec(pattern->ptr.reg, smp->data.str.str, smp->data.str.len) == 0)
536 return pattern;
537 }
538 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100539}
540
541/* Checks that the pattern matches the beginning of the tested string. */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100542struct pattern *pat_match_beg(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100543{
544 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100545 struct pattern_list *lst;
546 struct pattern *pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100547
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100548 /* convert input to string */
549 if (!sample_convert(smp, SMP_T_STR))
550 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100551
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100552 list_for_each_entry(lst, &expr->patterns, list) {
553 pattern = &lst->pat;
554
555 if (pattern->len > smp->data.str.len)
556 continue;
557
558 icase = pattern->flags & PAT_F_IGNORE_CASE;
559 if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str, pattern->len) != 0) ||
560 (!icase && strncmp(pattern->ptr.str, smp->data.str.str, pattern->len) != 0))
561 continue;
562
563 return pattern;
564 }
565 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100566}
567
568/* Checks that the pattern matches the end of the tested string. */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100569struct pattern *pat_match_end(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100570{
571 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100572 struct pattern_list *lst;
573 struct pattern *pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100574
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100575 /* convert input to string */
576 if (!sample_convert(smp, SMP_T_STR))
577 return NULL;
578
579 list_for_each_entry(lst, &expr->patterns, list) {
580 pattern = &lst->pat;
581
582 if (pattern->len > smp->data.str.len)
583 continue;
584
585 icase = pattern->flags & PAT_F_IGNORE_CASE;
586 if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str + smp->data.str.len - pattern->len, pattern->len) != 0) ||
587 (!icase && strncmp(pattern->ptr.str, smp->data.str.str + smp->data.str.len - pattern->len, pattern->len) != 0))
588 continue;
589
590 return pattern;
591 }
592 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100593}
594
595/* Checks that the pattern is included inside the tested string.
596 * NB: Suboptimal, should be rewritten using a Boyer-Moore method.
597 */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100598struct pattern *pat_match_sub(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100599{
600 int icase;
601 char *end;
602 char *c;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100603 struct pattern_list *lst;
604 struct pattern *pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100605
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100606 /* convert input to string */
607 if (!sample_convert(smp, SMP_T_STR))
608 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100609
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100610 list_for_each_entry(lst, &expr->patterns, list) {
611 pattern = &lst->pat;
612
613 if (pattern->len > smp->data.str.len)
614 continue;
615
616 end = smp->data.str.str + smp->data.str.len - pattern->len;
617 icase = pattern->flags & PAT_F_IGNORE_CASE;
618 if (icase) {
619 for (c = smp->data.str.str; c <= end; c++) {
620 if (tolower(*c) != tolower(*pattern->ptr.str))
621 continue;
622 if (strncasecmp(pattern->ptr.str, c, pattern->len) == 0)
623 return pattern;
624 }
625 } else {
626 for (c = smp->data.str.str; c <= end; c++) {
627 if (*c != *pattern->ptr.str)
628 continue;
629 if (strncmp(pattern->ptr.str, c, pattern->len) == 0)
630 return pattern;
631 }
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100632 }
633 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100634 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100635}
636
637/* This one is used by other real functions. It checks that the pattern is
638 * included inside the tested string, but enclosed between the specified
639 * delimiters or at the beginning or end of the string. The delimiters are
640 * provided as an unsigned int made by make_4delim() and match up to 4 different
641 * delimiters. Delimiters are stripped at the beginning and end of the pattern.
642 */
643static int match_word(struct sample *smp, struct pattern *pattern, unsigned int delimiters)
644{
645 int may_match, icase;
646 char *c, *end;
647 char *ps;
648 int pl;
649
650 pl = pattern->len;
651 ps = pattern->ptr.str;
652
653 while (pl > 0 && is_delimiter(*ps, delimiters)) {
654 pl--;
655 ps++;
656 }
657
658 while (pl > 0 && is_delimiter(ps[pl - 1], delimiters))
659 pl--;
660
661 if (pl > smp->data.str.len)
662 return PAT_NOMATCH;
663
664 may_match = 1;
665 icase = pattern->flags & PAT_F_IGNORE_CASE;
666 end = smp->data.str.str + smp->data.str.len - pl;
667 for (c = smp->data.str.str; c <= end; c++) {
668 if (is_delimiter(*c, delimiters)) {
669 may_match = 1;
670 continue;
671 }
672
673 if (!may_match)
674 continue;
675
676 if (icase) {
677 if ((tolower(*c) == tolower(*ps)) &&
678 (strncasecmp(ps, c, pl) == 0) &&
679 (c == end || is_delimiter(c[pl], delimiters)))
680 return PAT_MATCH;
681 } else {
682 if ((*c == *ps) &&
683 (strncmp(ps, c, pl) == 0) &&
684 (c == end || is_delimiter(c[pl], delimiters)))
685 return PAT_MATCH;
686 }
687 may_match = 0;
688 }
689 return PAT_NOMATCH;
690}
691
692/* Checks that the pattern is included inside the tested string, but enclosed
693 * between the delimiters '?' or '/' or at the beginning or end of the string.
694 * Delimiters at the beginning or end of the pattern are ignored.
695 */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100696struct pattern *pat_match_dir(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100697{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100698 struct pattern_list *lst;
699 struct pattern *pattern;
700
701 /* convert input to string */
702 if (!sample_convert(smp, SMP_T_STR))
703 return NULL;
704
705 list_for_each_entry(lst, &expr->patterns, list) {
706 pattern = &lst->pat;
707 if (match_word(smp, pattern, make_4delim('/', '?', '?', '?')))
708 return pattern;
709 }
710 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100711}
712
713/* Checks that the pattern is included inside the tested string, but enclosed
714 * between the delmiters '/', '?', '.' or ":" or at the beginning or end of
715 * the string. Delimiters at the beginning or end of the pattern are ignored.
716 */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100717struct pattern *pat_match_dom(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100718{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100719 struct pattern_list *lst;
720 struct pattern *pattern;
721
722 /* convert input to string */
723 if (!sample_convert(smp, SMP_T_STR))
724 return NULL;
725
726 list_for_each_entry(lst, &expr->patterns, list) {
727 pattern = &lst->pat;
728 if (match_word(smp, pattern, make_4delim('/', '?', '.', ':')))
729 return pattern;
730 }
731 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100732}
733
734/* Checks that the integer in <test> is included between min and max */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100735struct pattern *pat_match_int(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100736{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100737 struct pattern_list *lst;
738 struct pattern *pattern;
739
740 /* convert input to integer */
741 if (!sample_convert(smp, SMP_T_UINT))
742 return NULL;
743
744 list_for_each_entry(lst, &expr->patterns, list) {
745 pattern = &lst->pat;
746 if ((!pattern->val.range.min_set || pattern->val.range.min <= smp->data.uint) &&
747 (!pattern->val.range.max_set || smp->data.uint <= pattern->val.range.max))
748 return pattern;
749 }
750 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100751}
752
753/* Checks that the length of the pattern in <test> is included between min and max */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100754struct pattern *pat_match_len(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100755{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100756 struct pattern_list *lst;
757 struct pattern *pattern;
758
759 /* convert input to string */
760 if (!sample_convert(smp, SMP_T_STR))
761 return NULL;
762
763 list_for_each_entry(lst, &expr->patterns, list) {
764 pattern = &lst->pat;
765 if ((!pattern->val.range.min_set || pattern->val.range.min <= smp->data.str.len) &&
766 (!pattern->val.range.max_set || smp->data.str.len <= pattern->val.range.max))
767 return pattern;
768 }
769 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100770}
771
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100772struct pattern *pat_match_ip(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100773{
774 unsigned int v4; /* in network byte order */
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100775 struct in6_addr tmp6;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100776 struct in_addr *s;
777 struct ebmb_node *node;
778 struct pattern_tree *elt;
779 struct pattern_list *lst;
780 struct pattern *pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100781
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100782 /* convert input to addr */
783 if (!sample_convert(smp, SMP_T_ADDR))
784 return NULL;
785
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100786 /* The input sample is IPv4. Try to match in the trees. */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100787 if (smp->type == SMP_T_IPV4) {
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100788 /* Lookup an IPv4 address in the expression's pattern tree using
789 * the longest match method.
790 */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100791 s = &smp->data.ipv4;
792 node = ebmb_lookup_longest(&expr->pattern_tree, &s->s_addr);
793 if (node) {
794 if (fill) {
795 elt = ebmb_entry(node, struct pattern_tree, node);
796 static_pattern.smp = elt->smp;
797 static_pattern.flags = PAT_F_TREE;
798 static_pattern.type = SMP_T_IPV4;
799 memcpy(&static_pattern.val.ipv4.addr.s_addr, elt->node.key, 4);
800 if (!cidr2dotted(elt->node.node.pfx, &static_pattern.val.ipv4.mask))
801 return NULL;
802 }
803 return &static_pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100804 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100805
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100806 /* The IPv4 sample dont match the IPv4 tree. Convert the IPv4
807 * sample address to IPv6 with the mapping method using the ::ffff:
808 * prefix, and try to lookup in the IPv6 tree.
809 */
810 memset(&tmp6, 0, 10);
811 *(uint16_t*)&tmp6.s6_addr[10] = htons(0xffff);
812 *(uint32_t*)&tmp6.s6_addr[12] = smp->data.ipv4.s_addr;
813 node = ebmb_lookup_longest(&expr->pattern_tree_2, &tmp6);
814 if (node) {
815 if (fill) {
816 elt = ebmb_entry(node, struct pattern_tree, node);
817 static_pattern.smp = elt->smp;
818 static_pattern.flags = PAT_F_TREE;
819 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;
822 }
823 return &static_pattern;
824 }
825 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100826
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100827 /* The input sample is IPv6. Try to match in the trees. */
828 if (smp->type == SMP_T_IPV6) {
829 /* Lookup an IPv6 address in the expression's pattern tree using
830 * the longest match method.
831 */
832 node = ebmb_lookup_longest(&expr->pattern_tree_2, &smp->data.ipv6);
833 if (node) {
834 if (fill) {
835 elt = ebmb_entry(node, struct pattern_tree, node);
836 static_pattern.smp = elt->smp;
837 static_pattern.flags = PAT_F_TREE;
838 static_pattern.type = SMP_T_IPV6;
839 memcpy(&static_pattern.val.ipv6.addr, elt->node.key, 16);
840 static_pattern.val.ipv6.mask = elt->node.node.pfx;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100841 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100842 return &static_pattern;
843 }
844
845 /* Try to convert 6 to 4 when the start of the ipv6 address match the
846 * following forms :
847 * - ::ffff:ip:v4 (ipv4 mapped)
848 * - ::0000:ip:v4 (old ipv4 mapped)
849 * - 2002:ip:v4:: (6to4)
850 */
851 if ((*(uint32_t*)&smp->data.ipv6.s6_addr[0] == 0 &&
852 *(uint32_t*)&smp->data.ipv6.s6_addr[4] == 0 &&
853 (*(uint32_t*)&smp->data.ipv6.s6_addr[8] == 0 ||
854 *(uint32_t*)&smp->data.ipv6.s6_addr[8] == htonl(0xFFFF))) ||
855 *(uint16_t*)&smp->data.ipv6.s6_addr[0] == htons(0x2002)) {
856 if (*(uint32_t*)&smp->data.ipv6.s6_addr[0] == 0)
857 v4 = *(uint32_t*)&smp->data.ipv6.s6_addr[12];
858 else
859 v4 = htonl((ntohs(*(uint16_t*)&smp->data.ipv6.s6_addr[2]) << 16) +
860 ntohs(*(uint16_t*)&smp->data.ipv6.s6_addr[4]));
861
862 /* Lookup an IPv4 address in the expression's pattern tree using the longest
863 * match method.
864 */
865 node = ebmb_lookup_longest(&expr->pattern_tree, &v4);
866 if (node) {
867 if (fill) {
868 elt = ebmb_entry(node, struct pattern_tree, node);
869 static_pattern.smp = elt->smp;
870 static_pattern.flags = PAT_F_TREE;
871 static_pattern.type = SMP_T_IPV4;
872 memcpy(&static_pattern.val.ipv4.addr.s_addr, elt->node.key, 4);
873 if (!cidr2dotted(elt->node.node.pfx, &static_pattern.val.ipv4.mask))
874 return NULL;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100875 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100876 return &static_pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100877 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100878 }
879 }
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100880
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100881 /* Lookup in the list. the list contain only IPv4 patterns */
882 list_for_each_entry(lst, &expr->patterns, list) {
883 pattern = &lst->pat;
884
885 /* The input sample is IPv4, use it as is. */
886 if (smp->type == SMP_T_IPV4) {
887 v4 = smp->data.ipv4.s_addr;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100888 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100889 else if (smp->type == SMP_T_IPV6) {
890 /* v4 match on a V6 sample. We want to check at least for
891 * the following forms :
892 * - ::ffff:ip:v4 (ipv4 mapped)
893 * - ::0000:ip:v4 (old ipv4 mapped)
894 * - 2002:ip:v4:: (6to4)
895 */
896 if (*(uint32_t*)&smp->data.ipv6.s6_addr[0] == 0 &&
897 *(uint32_t*)&smp->data.ipv6.s6_addr[4] == 0 &&
898 (*(uint32_t*)&smp->data.ipv6.s6_addr[8] == 0 ||
899 *(uint32_t*)&smp->data.ipv6.s6_addr[8] == htonl(0xFFFF))) {
900 v4 = *(uint32_t*)&smp->data.ipv6.s6_addr[12];
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100901 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100902 else if (*(uint16_t*)&smp->data.ipv6.s6_addr[0] == htons(0x2002)) {
903 v4 = htonl((ntohs(*(uint16_t*)&smp->data.ipv6.s6_addr[2]) << 16) +
904 ntohs(*(uint16_t*)&smp->data.ipv6.s6_addr[4]));
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100905 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100906 else
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100907 continue;
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100908 }
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100909
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100910 /* Check if the input sample match the current pattern. */
911 if (((v4 ^ pattern->val.ipv4.addr.s_addr) & pattern->val.ipv4.mask.s_addr) == 0)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100912 return pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100913 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100914 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100915}
916
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100917/* NB: does nothing if <pat> is NULL */
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +0100918void pattern_free(struct pattern_list *pat)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100919{
920 if (!pat)
921 return;
922
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +0100923 if (pat->pat.ptr.ptr) {
924 if (pat->pat.freeptrbuf)
925 pat->pat.freeptrbuf(pat->pat.ptr.ptr);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100926
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +0100927 free(pat->pat.ptr.ptr);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100928 }
929
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +0100930 free(pat->pat.smp);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100931 free(pat);
932}
933
934void free_pattern_list(struct list *head)
935{
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +0100936 struct pattern_list *pat, *tmp;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100937 list_for_each_entry_safe(pat, tmp, head, list)
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100938 pattern_free(pat);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100939}
940
941void free_pattern_tree(struct eb_root *root)
942{
943 struct eb_node *node, *next;
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +0100944 struct pattern_tree *elt;
Thierry FOURNIER3ce88c72013-12-09 11:29:46 +0100945
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100946 node = eb_first(root);
947 while (node) {
948 next = eb_next(node);
949 eb_delete(node);
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +0100950 elt = container_of(node, struct pattern_tree, node);
Thierry FOURNIERc64de3f2013-12-10 15:08:39 +0100951 free(elt->smp);
Thierry FOURNIER3ce88c72013-12-09 11:29:46 +0100952 free(elt);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100953 node = next;
954 }
955}
956
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100957void pattern_prune_expr(struct pattern_expr *expr)
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +0100958{
959 free_pattern_list(&expr->patterns);
960 free_pattern_tree(&expr->pattern_tree);
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100961 free_pattern_tree(&expr->pattern_tree_2);
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +0100962 LIST_INIT(&expr->patterns);
963}
964
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100965void pattern_init_expr(struct pattern_expr *expr)
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +0100966{
967 LIST_INIT(&expr->patterns);
968 expr->pattern_tree = EB_ROOT_UNIQUE;
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100969 expr->pattern_tree_2 = EB_ROOT_UNIQUE;
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +0100970}
971
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100972/*
973 *
974 * The following functions are used for the pattern indexation
975 *
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100976 */
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100977
978int pat_idx_list_val(struct pattern_expr *expr, struct pattern *pat, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100979{
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100980 struct pattern_list *patl;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100981
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100982 /* allocate pattern */
983 patl = calloc(1, sizeof(*patl));
984 if (!patl) {
985 memprintf(err, "out of memory while indexing pattern");
Thierry FOURNIER972028f2014-01-23 17:53:31 +0100986 return 0;
987 }
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100988
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100989 /* duplicate pattern */
990 memcpy(&patl->pat, pat, sizeof(*pat));
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100991
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100992 /* chain pattern in the expression */
993 LIST_ADDQ(&expr->patterns, &patl->list);
994
995 /* that's ok */
996 return 1;
997}
998
999int pat_idx_list_ptr(struct pattern_expr *expr, struct pattern *pat, char **err)
1000{
1001 struct pattern_list *patl;
1002
1003 /* allocate pattern */
1004 patl = calloc(1, sizeof(*patl));
1005 if (!patl)
Thierry FOURNIER972028f2014-01-23 17:53:31 +01001006 return 0;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001007
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001008 /* duplicate pattern */
1009 memcpy(&patl->pat, pat, sizeof(*pat));
1010 patl->pat.ptr.ptr = malloc(patl->pat.len);
1011 if (!patl->pat.ptr.ptr) {
1012 free(patl);
1013 memprintf(err, "out of memory while indexing pattern");
1014 return 0;
1015 }
1016 memcpy(patl->pat.ptr.ptr, pat->ptr.ptr, pat->len);
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001017
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001018 /* chain pattern in the expression */
1019 LIST_ADDQ(&expr->patterns, &patl->list);
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001020
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001021 /* that's ok */
1022 return 1;
1023}
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001024
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001025int pat_idx_list_str(struct pattern_expr *expr, struct pattern *pat, char **err)
1026{
1027 struct pattern_list *patl;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001028
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001029 /* allocate pattern */
1030 patl = calloc(1, sizeof(*patl));
1031 if (!patl) {
1032 memprintf(err, "out of memory while indexing pattern");
1033 return 0;
1034 }
1035
1036 /* duplicate pattern */
1037 memcpy(&patl->pat, pat, sizeof(*pat));
1038 patl->pat.ptr.str = malloc(patl->pat.len + 1);
1039 if (!patl->pat.ptr.str) {
1040 free(patl);
1041 memprintf(err, "out of memory while indexing pattern");
1042 return 0;
1043 }
1044 memcpy(patl->pat.ptr.ptr, pat->ptr.ptr, pat->len);
1045 patl->pat.ptr.str[patl->pat.len] = '\0';
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001046
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001047 /* chain pattern in the expression */
1048 LIST_ADDQ(&expr->patterns, &patl->list);
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001049
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001050 /* that's ok */
1051 return 1;
1052}
1053
1054int pat_idx_list_reg(struct pattern_expr *expr, struct pattern *pat, char **err)
1055{
1056 struct pattern_list *patl;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001057
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001058 /* allocate pattern */
1059 patl = calloc(1, sizeof(*patl));
1060 if (!patl) {
1061 memprintf(err, "out of memory while indexing pattern");
1062 return 0;
Thierry FOURNIER972028f2014-01-23 17:53:31 +01001063 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001064
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001065 /* duplicate pattern */
1066 memcpy(&patl->pat, pat, sizeof(*pat));
1067
1068 /* allocate regex */
1069 patl->pat.ptr.reg = calloc(1, sizeof(*patl->pat.ptr.reg));
1070 if (!patl->pat.ptr.reg) {
1071 free(patl);
1072 memprintf(err, "out of memory while indexing pattern");
1073 return 0;
1074 }
1075
1076 /* compile regex */
1077 if (!regex_comp(pat->ptr.reg->regstr, patl->pat.ptr.reg, !(patl->pat.flags & PAT_F_IGNORE_CASE), 0, err)) {
1078 free(patl);
1079 free(patl->pat.ptr.reg);
1080 return 0;
1081 }
1082
1083 /* free pattern method */
1084 patl->pat.freeptrbuf = &pat_free_reg;
1085
1086 /* chain pattern in the expression */
1087 LIST_ADDQ(&expr->patterns, &patl->list);
1088
1089 /* that's ok */
1090 return 1;
1091}
1092
1093int pat_idx_tree_ip(struct pattern_expr *expr, struct pattern *pat, char **err)
1094{
1095 unsigned int mask;
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +01001096 struct pattern_tree *node;
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001097
1098 /* Only IPv4 can be indexed */
1099 if (pat->type == SMP_T_IPV4) {
Thierry FOURNIER972028f2014-01-23 17:53:31 +01001100 /* in IPv4 case, check if the mask is contiguous so that we can
1101 * insert the network into the tree. A continuous mask has only
1102 * ones on the left. This means that this mask + its lower bit
1103 * added once again is null.
1104 */
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001105 mask = ntohl(pat->val.ipv4.mask.s_addr);
1106 if (mask + (mask & -mask) == 0) {
1107 mask = mask ? 33 - flsnz(mask & -mask) : 0; /* equals cidr value */
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001108
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001109 /* node memory allocation */
1110 node = calloc(1, sizeof(*node) + 4);
1111 if (!node) {
1112 memprintf(err, "out of memory while loading pattern");
1113 return 0;
1114 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001115
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001116 /* copy the pointer to sample associated to this node */
1117 node->smp = pat->smp;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001118
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001119 /* FIXME: insert <addr>/<mask> into the tree here */
1120 memcpy(node->node.key, &pat->val.ipv4.addr, 4); /* network byte order */
1121 node->node.node.pfx = mask;
1122 if (ebmb_insert_prefix(&expr->pattern_tree, &node->node, 4) != &node->node)
1123 free(node); /* was a duplicate */
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001124
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001125 /* that's ok */
1126 return 1;
1127 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001128 else {
1129 /* If the mask is not contiguous, just add the pattern to the list */
1130 return pat_idx_list_val(expr, pat, err);
1131 }
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001132 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001133 else if (pat->type == SMP_T_IPV6) {
1134 /* IPv6 also can be indexed */
1135 node = calloc(1, sizeof(*node) + 16);
1136 if (!node) {
1137 memprintf(err, "out of memory while loading pattern");
1138 return 0;
1139 }
1140
1141 /* copy the pointer to sample associated to this node */
1142 node->smp = pat->smp;
1143
1144 /* FIXME: insert <addr>/<mask> into the tree here */
1145 memcpy(node->node.key, &pat->val.ipv6.addr, 16); /* network byte order */
1146 node->node.node.pfx = pat->val.ipv6.mask;
1147 if (ebmb_insert_prefix(&expr->pattern_tree_2, &node->node, 16) != &node->node)
1148 free(node); /* was a duplicate */
1149
1150 /* that's ok */
1151 return 1;
1152 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001153
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001154 return 0;
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001155}
1156
1157int pat_idx_tree_str(struct pattern_expr *expr, struct pattern *pat, char **err)
1158{
1159 int len;
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +01001160 struct pattern_tree *node;
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001161
1162 /* Only string can be indexed */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001163 if (pat->type != SMP_T_STR) {
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001164 memprintf(err, "internal error: string expected, but the type is '%s'",
1165 smp_to_type[pat->type]);
1166 return 0;
Thierry FOURNIER972028f2014-01-23 17:53:31 +01001167 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001168
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001169 /* If the flag PAT_F_IGNORE_CASE is set, we cannot use trees */
1170 if (pat->flags & PAT_F_IGNORE_CASE)
1171 return pat_idx_list_str(expr, pat, err);
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001172
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001173 /* Process the key len */
1174 len = strlen(pat->ptr.str) + 1;
1175
1176 /* node memory allocation */
1177 node = calloc(1, sizeof(*node) + len);
1178 if (!node) {
1179 memprintf(err, "out of memory while loading pattern");
1180 return 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001181 }
1182
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001183 /* copy the pointer to sample associated to this node */
1184 node->smp = pat->smp;
1185
1186 /* copy the string */
1187 memcpy(node->node.key, pat->ptr.str, len);
1188
1189 /* index the new node */
1190 if (ebst_insert(&expr->pattern_tree, &node->node) != &node->node)
1191 free(node); /* was a duplicate */
1192
1193 /* that's ok */
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001194 return 1;
1195}
1196
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001197void pat_del_list_val(struct pattern_expr *expr, struct pattern *pattern)
1198{
1199 struct pattern_list *pat;
1200 struct pattern_list *safe;
1201
1202 list_for_each_entry_safe(pat, safe, &expr->patterns, list) {
1203 /* Check equality. */
1204 if (pattern->val.range.min_set != pat->pat.val.range.min_set)
1205 continue;
1206 if (pattern->val.range.max_set != pat->pat.val.range.max_set)
1207 continue;
1208 if (pattern->val.range.min_set &&
1209 pattern->val.range.min != pat->pat.val.range.min)
1210 continue;
1211 if (pattern->val.range.max_set &&
1212 pattern->val.range.max != pat->pat.val.range.max)
1213 continue;
1214
1215 /* Delete and free entry. */
1216 LIST_DEL(&pat->list);
1217 free(pat->pat.smp);
1218 free(pat);
1219 }
1220}
1221
1222void pat_del_tree_ip(struct pattern_expr *expr, struct pattern *pattern)
1223{
1224 struct ebmb_node *node, *next_node;
1225 struct pattern_tree *elt;
1226 struct pattern_list *pat;
1227 struct pattern_list *safe;
1228 unsigned int mask;
1229
1230 /* browse each node of the tree for IPv4 addresses. */
1231 if (pattern->type == SMP_T_IPV4) {
1232 /* Convert mask. If the mask is contiguous, browse each node
1233 * of the tree for IPv4 addresses.
1234 */
1235 mask = ntohl(pattern->val.ipv4.mask.s_addr);
1236 if (mask + (mask & -mask) == 0) {
1237 mask = mask ? 33 - flsnz(mask & -mask) : 0; /* equals cidr value */
1238
1239 for (node = ebmb_first(&expr->pattern_tree), next_node = node ? ebmb_next(node) : NULL;
1240 node;
1241 node = next_node, next_node = node ? ebmb_next(node) : NULL) {
1242 /* Extract container of the tree node. */
1243 elt = container_of(node, struct pattern_tree, node);
1244
1245 /* Check equality. */
1246 if (memcmp(&pattern->val.ipv4.addr, elt->node.key,
1247 sizeof(pattern->val.ipv4.addr)) != 0)
1248 continue;
1249 if (elt->node.node.pfx != mask)
1250 continue;
1251
1252 /* Delete and free entry. */
1253 ebmb_delete(node);
1254 free(elt->smp);
1255 free(elt);
1256 }
1257 }
1258 else {
1259 /* Browse each node of the list for IPv4 addresses. */
1260 list_for_each_entry_safe(pat, safe, &expr->patterns, list) {
1261 /* Check equality, addr then mask */
1262 if (memcmp(&pattern->val.ipv4.addr, &pat->pat.val.ipv4.addr,
1263 sizeof(pat->pat.val.ipv4.addr)) != 0)
1264 continue;
1265
1266 if (memcmp(&pattern->val.ipv4.mask, &pat->pat.val.ipv4.mask,
1267 sizeof(pat->pat.val.ipv4.addr)) != 0)
1268 continue;
1269
1270 /* Delete and free entry. */
1271 LIST_DEL(&pat->list);
1272 free(pat->pat.smp);
1273 free(pat);
1274 }
1275 }
1276 }
1277 else if (pattern->type == SMP_T_IPV6) {
1278 /* browse each node of the tree for IPv6 addresses. */
1279 for (node = ebmb_first(&expr->pattern_tree_2), next_node = node ? ebmb_next(node) : NULL;
1280 node;
1281 node = next_node, next_node = node ? ebmb_next(node) : NULL) {
1282 /* Extract container of the tree node. */
1283 elt = container_of(node, struct pattern_tree, node);
1284
1285 /* Check equality. */
1286 if (memcmp(&pattern->val.ipv6.addr, elt->node.key,
1287 sizeof(pattern->val.ipv6.addr)) != 0)
1288 continue;
1289 if (elt->node.node.pfx != pattern->val.ipv6.mask)
1290 continue;
1291
1292 /* Delete and free entry. */
1293 ebmb_delete(node);
1294 free(elt->smp);
1295 free(elt);
1296 }
1297 }
1298}
1299
1300void pat_del_list_ptr(struct pattern_expr *expr, struct pattern *pattern)
1301{
1302 struct pattern_list *pat;
1303 struct pattern_list *safe;
1304
1305 list_for_each_entry_safe(pat, safe, &expr->patterns, list) {
1306 /* Check equality. */
1307 if (pattern->len != pat->pat.len)
1308 continue;
1309 if (memcmp(pattern->ptr.ptr, pat->pat.ptr.ptr, pat->pat.len) != 0)
1310 continue;
1311
1312 /* Delete and free entry. */
1313 LIST_DEL(&pat->list);
1314 free(pat->pat.ptr.ptr);
1315 free(pat->pat.smp);
1316 free(pat);
1317 }
1318}
1319
1320void pat_del_tree_str(struct pattern_expr *expr, struct pattern *pattern)
1321{
1322 struct ebmb_node *node, *next_node;
1323 struct pattern_tree *elt;
1324
1325 /* browse each node of the tree. */
1326 for (node = ebmb_first(&expr->pattern_tree), next_node = node ? ebmb_next(node) : NULL;
1327 node;
1328 node = next_node, next_node = node ? ebmb_next(node) : NULL) {
1329 /* Extract container of the tree node. */
1330 elt = container_of(node, struct pattern_tree, node);
1331
1332 /* Check equality. */
1333 if (strcmp(pattern->ptr.str, (char *)elt->node.key) != 0)
1334 continue;
1335
1336 /* Delete and free entry. */
1337 ebmb_delete(node);
1338 free(elt->smp);
1339 free(elt);
1340 }
1341}
1342
1343void pat_del_list_str(struct pattern_expr *expr, struct pattern *pattern)
1344{
1345 struct pattern_list *pat;
1346 struct pattern_list *safe;
1347
1348 list_for_each_entry_safe(pat, safe, &expr->patterns, list) {
1349 /* Check equality. */
1350 if (pattern->len != pat->pat.len)
1351 continue;
1352 if (pat->pat.flags & PAT_F_IGNORE_CASE) {
1353 if (strncasecmp(pattern->ptr.str, pat->pat.ptr.str, pat->pat.len) != 0)
1354 continue;
1355 }
1356 else {
1357 if (strncmp(pattern->ptr.str, pat->pat.ptr.str, pat->pat.len) != 0)
1358 continue;
1359 }
1360
1361 /* Delete and free entry. */
1362 LIST_DEL(&pat->list);
1363 free(pat->pat.ptr.str);
1364 free(pat->pat.smp);
1365 free(pat);
1366 }
1367}
1368
1369void pat_del_list_reg(struct pattern_expr *expr, struct pattern *pattern)
1370{
1371 struct pattern_list *pat;
1372 struct pattern_list *safe;
1373
1374 list_for_each_entry_safe(pat, safe, &expr->patterns, list) {
1375 /* Check equality. */
1376 if (pat->pat.flags & PAT_F_IGNORE_CASE) {
1377 if (strcasecmp(pattern->ptr.reg->regstr, pat->pat.ptr.reg->regstr) != 0)
1378 continue;
1379 }
1380 else {
1381 if (strcmp(pattern->ptr.reg->regstr, pat->pat.ptr.reg->regstr) != 0)
1382 continue;
1383 }
1384
1385 /* Delete and free entry. */
1386 LIST_DEL(&pat->list);
1387 regex_free(pat->pat.ptr.ptr);
1388 free(pat->pat.smp);
1389 free(pat);
1390 }
1391}
1392
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001393/* return 1 if the process is ok
1394 * return -1 if the parser fail. The err message is filled.
1395 * return -2 if out of memory
1396 */
1397int pattern_register(struct pattern_expr *expr, const char *arg,
1398 struct sample_storage *smp,
1399 int patflags, char **err)
1400{
1401 int ret;
1402 struct pattern pattern;
1403
1404 /* initialise pattern */
1405 memset(&pattern, 0, sizeof(pattern));
1406 pattern.flags = patflags;
1407 pattern.smp = smp;
1408
1409 /* parse pattern */
Thierry FOURNIERedc15c32013-12-13 15:36:59 +01001410 ret = expr->parse(arg, &pattern, err);
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001411 if (!ret)
1412 return 0;
1413
1414 /* index pattern */
1415 if (!expr->index(expr, &pattern, err))
1416 return 0;
1417
1418 return 1;
1419}
1420
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001421/* Reads patterns from a file. If <err_msg> is non-NULL, an error message will
1422 * be returned there on errors and the caller will have to free it.
1423 */
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01001424int pattern_read_from_file(struct pattern_expr *expr,
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001425 const char *filename, int patflags,
1426 char **err)
1427{
1428 FILE *file;
1429 char *c;
1430 char *arg;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001431 int ret = 0;
1432 int line = 0;
1433 int code;
1434
1435 file = fopen(filename, "r");
1436 if (!file) {
1437 memprintf(err, "failed to open pattern file <%s>", filename);
1438 return 0;
1439 }
1440
1441 /* now parse all patterns. The file may contain only one pattern per
1442 * line. If the line contains spaces, they will be part of the pattern.
1443 * The pattern stops at the first CR, LF or EOF encountered.
1444 */
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001445 while (fgets(trash.str, trash.size, file) != NULL) {
1446 line++;
1447 c = trash.str;
1448
1449 /* ignore lines beginning with a dash */
1450 if (*c == '#')
1451 continue;
1452
1453 /* strip leading spaces and tabs */
1454 while (*c == ' ' || *c == '\t')
1455 c++;
1456
1457
1458 arg = c;
1459 while (*c && *c != '\n' && *c != '\r')
1460 c++;
1461 *c = 0;
1462
1463 /* empty lines are ignored too */
1464 if (c == arg)
1465 continue;
1466
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001467 code = pattern_register(expr, arg, NULL, patflags, err);
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001468 if (code == -2) {
1469 memprintf(err, "out of memory when loading patterns from file <%s>", filename);
1470 goto out_close;
1471 }
1472 else if (code < 0) {
1473 memprintf(err, "%s when loading patterns from file <%s>", *err, filename);
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001474 goto out_close;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001475 }
1476 }
1477
1478 ret = 1; /* success */
1479
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001480 out_close:
1481 fclose(file);
1482 return ret;
1483}
1484
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01001485/* This function executes a pattern match on a sample. It applies pattern <expr>
1486 * to sample <smp>. The function returns NULL if the sample dont match. It returns
1487 * non-null if the sample match. If <fill> is true and the sample match, the
1488 * function returns the matched pattern. In many cases, this pattern can be a
1489 * static buffer.
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001490 */
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01001491struct pattern *pattern_exec_match(struct pattern_expr *expr, struct sample *smp, int fill)
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001492{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01001493 if (!expr->match) {
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01001494 if (fill) {
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01001495 static_pattern.smp = NULL;
1496 static_pattern.flags = 0;
1497 static_pattern.type = SMP_T_UINT;
1498 static_pattern.val.i = 1;
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01001499 }
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01001500 return &static_pattern;
1501 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01001502 return expr->match(smp, expr, fill);
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001503}
1504
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001505/* This function search all the pattern matching the <key> and delete it.
1506 * If the parsing of the input key fails, the function returns 0 and the
1507 * <err> is filled, else return 1;
1508 */
1509int pattern_delete(const char *key, struct pattern_expr *expr, char **err)
1510{
1511 struct pattern pattern;
1512
1513 if (!expr->parse(key, &pattern, err))
1514 return 0;
1515 expr->delete(expr, &pattern);
1516 return 1;
1517}
1518
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001519/* This function browse the pattern expr <expr> to lookup the key <key>. On
1520 * error it returns 0. On success, it returns 1 and fills either <pat_elt>
1521 * or <idx_elt> with the respectively matched pointers, and the other one with
1522 * NULL. Pointers are not set if they're passed as NULL.
1523 */
1524int pattern_lookup(const char *key, struct pattern_expr *expr,
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +01001525 struct pattern_list **pat_elt, struct pattern_tree **idx_elt, char **err)
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001526{
1527 struct pattern pattern;
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001528 struct pattern_list *pat;
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001529 struct ebmb_node *node;
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +01001530 struct pattern_tree *elt;
Willy Tarreau668ae532013-12-15 16:42:26 +01001531 unsigned int mask = 0;
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001532
1533 /* no real pattern */
1534 if (!expr->match || expr->match == pat_match_nothing)
1535 return 0;
1536
1537 /* build lookup pattern */
Thierry FOURNIERedc15c32013-12-13 15:36:59 +01001538 if (!expr->parse(key, &pattern, NULL))
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001539 return 0;
1540
1541 pat = NULL;
1542 elt = NULL;
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001543
Thierry FOURNIERc64de3f2013-12-10 15:08:39 +01001544 /* Try to look up the tree first. IPv6 is not indexed */
1545 if (!eb_is_empty(&expr->pattern_tree) && pattern.type != SMP_T_IPV6) {
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001546 /* Check the pattern type */
1547 if (pattern.type != SMP_T_STR &&
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001548 pattern.type != SMP_T_IPV4) {
1549 memprintf(err, "Unexpected pattern type.");
1550 return 0;
1551 }
1552
1553 /* Convert mask. If the mask is not contiguous, ignore the lookup
1554 * in the tree, and browse the list.
1555 */
1556 if (expr->match == pat_match_ip) {
1557 mask = ntohl(pattern.val.ipv4.mask.s_addr);
1558 if (mask + (mask & -mask) != 0)
1559 goto browse_list;
1560 mask = mask ? 33 - flsnz(mask & -mask) : 0; /* equals cidr value */
1561 }
1562
1563 /* browse each node of the tree, and check string */
1564 if (expr->match == pat_match_str) {
1565 for (node = ebmb_first(&expr->pattern_tree);
1566 node;
1567 node = ebmb_next(node)) {
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +01001568 elt = container_of(node, struct pattern_tree, node);
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001569 if (strcmp(pattern.ptr.str, (char *)elt->node.key) == 0)
1570 goto found;
1571 }
1572 }
1573 else if (expr->match == pat_match_ip) {
1574 for (node = ebmb_first(&expr->pattern_tree);
1575 node;
1576 node = ebmb_next(node)) {
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +01001577 elt = container_of(node, struct pattern_tree, node);
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001578 if (elt->node.node.pfx == mask &&
1579 memcmp(&pattern.val.ipv4.addr.s_addr, elt->node.key, 4) == 0)
1580 goto found;
1581 }
1582 }
1583 }
1584
1585browse_list:
Thierry FOURNIERc64de3f2013-12-10 15:08:39 +01001586 elt = NULL;
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001587 if (expr->parse == pat_parse_int ||
1588 expr->parse == pat_parse_len) {
1589 list_for_each_entry(pat, &expr->patterns, list) {
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001590 if (pat->pat.flags & PAT_F_TREE)
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001591 continue;
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001592 if (pattern.val.range.min_set != pat->pat.val.range.min_set)
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001593 continue;
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001594 if (pattern.val.range.max_set != pat->pat.val.range.max_set)
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001595 continue;
1596 if (pattern.val.range.min_set &&
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001597 pattern.val.range.min != pat->pat.val.range.min)
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001598 continue;
1599 if (pattern.val.range.max_set &&
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001600 pattern.val.range.max != pat->pat.val.range.max)
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001601 continue;
1602 goto found;
1603 }
1604 }
1605 else if (expr->parse == pat_parse_ip) {
1606 list_for_each_entry(pat, &expr->patterns, list) {
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001607 if (pat->pat.flags & PAT_F_TREE)
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001608 continue;
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001609 if (pattern.type != pat->pat.type)
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001610 continue;
1611 if (pattern.type == SMP_T_IPV4 &&
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001612 memcmp(&pattern.val.ipv4.addr, &pat->pat.val.ipv4.addr, sizeof(pat->pat.val.ipv4.addr)) != 0)
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001613 continue;
1614 if (pattern.type == SMP_T_IPV4 &&
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001615 memcmp(&pattern.val.ipv4.mask, &pat->pat.val.ipv4.mask, sizeof(pat->pat.val.ipv4.addr)) != 0)
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001616 continue;
1617 if (pattern.type == SMP_T_IPV6 &&
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001618 memcmp(&pattern.val.ipv6.addr, &pat->pat.val.ipv6.addr, sizeof(pat->pat.val.ipv6.addr)) != 0)
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001619 continue;
1620 if (pattern.type == SMP_T_IPV6 &&
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001621 pattern.val.ipv6.mask != pat->pat.val.ipv6.mask)
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001622 continue;
1623 goto found;
1624 }
1625 }
1626 else if (expr->parse == pat_parse_str) {
1627 list_for_each_entry(pat, &expr->patterns, list) {
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001628 if (pat->pat.flags & PAT_F_TREE)
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001629 continue;
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001630 if (pattern.len != pat->pat.len)
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001631 continue;
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001632 if (pat->pat.flags & PAT_F_IGNORE_CASE) {
1633 if (strncasecmp(pattern.ptr.str, pat->pat.ptr.str, pat->pat.len) != 0)
Thierry FOURNIER35249cb2014-01-14 13:38:40 +01001634 continue;
1635 }
1636 else {
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001637 if (strncmp(pattern.ptr.str, pat->pat.ptr.str, pat->pat.len) != 0)
Thierry FOURNIER35249cb2014-01-14 13:38:40 +01001638 continue;
1639 }
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001640 goto found;
1641 }
1642 }
1643 else if (expr->parse == pat_parse_bin) {
1644 list_for_each_entry(pat, &expr->patterns, list) {
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001645 if (pat->pat.flags & PAT_F_TREE)
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001646 continue;
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001647 if (pattern.len != pat->pat.len)
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001648 continue;
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001649 if (memcmp(pattern.ptr.ptr, pat->pat.ptr.ptr, pat->pat.len) != 0)
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001650 continue;
1651 goto found;
1652 }
1653 }
1654 else if (expr->parse == pat_parse_reg) {
1655 list_for_each_entry(pat, &expr->patterns, list) {
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001656 if (pat->pat.flags & PAT_F_TREE)
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001657 continue;
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001658 if (pat->pat.flags & PAT_F_IGNORE_CASE) {
1659 if (strcasecmp(pattern.ptr.reg->regstr, pat->pat.ptr.reg->regstr) != 0)
Thierry FOURNIER35249cb2014-01-14 13:38:40 +01001660 continue;
1661 }
1662 else {
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001663 if (strcmp(pattern.ptr.reg->regstr, pat->pat.ptr.reg->regstr) != 0)
Thierry FOURNIER35249cb2014-01-14 13:38:40 +01001664 continue;
1665 }
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001666 goto found;
1667 }
1668 }
1669
1670 /* if we get there, we didn't find the pattern */
1671 return 0;
1672found:
1673 if (idx_elt)
1674 *idx_elt = elt;
1675
1676 if (pat_elt)
1677 *pat_elt = pat;
1678
1679 return 1;
1680}