blob: 07e1a524d93dbefd5eaa14039a5b826f2f023fe5 [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>
Willy Tarreauf3045d22015-04-29 16:24:50 +020027#include <import/lru.h>
28#include <import/xxhash.h>
Thierry FOURNIERed66c292013-11-28 11:05:19 +010029
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010030char *pat_match_names[PAT_MATCH_NUM] = {
31 [PAT_MATCH_FOUND] = "found",
32 [PAT_MATCH_BOOL] = "bool",
33 [PAT_MATCH_INT] = "int",
34 [PAT_MATCH_IP] = "ip",
35 [PAT_MATCH_BIN] = "bin",
36 [PAT_MATCH_LEN] = "len",
37 [PAT_MATCH_STR] = "str",
38 [PAT_MATCH_BEG] = "beg",
39 [PAT_MATCH_SUB] = "sub",
40 [PAT_MATCH_DIR] = "dir",
41 [PAT_MATCH_DOM] = "dom",
42 [PAT_MATCH_END] = "end",
43 [PAT_MATCH_REG] = "reg",
Thierry FOURNIERed66c292013-11-28 11:05:19 +010044};
45
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +020046int (*pat_parse_fcts[PAT_MATCH_NUM])(const char *, struct pattern *, int, char **) = {
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010047 [PAT_MATCH_FOUND] = pat_parse_nothing,
48 [PAT_MATCH_BOOL] = pat_parse_nothing,
49 [PAT_MATCH_INT] = pat_parse_int,
50 [PAT_MATCH_IP] = pat_parse_ip,
51 [PAT_MATCH_BIN] = pat_parse_bin,
Thierry FOURNIER5d344082014-01-27 14:19:53 +010052 [PAT_MATCH_LEN] = pat_parse_int,
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010053 [PAT_MATCH_STR] = pat_parse_str,
54 [PAT_MATCH_BEG] = pat_parse_str,
55 [PAT_MATCH_SUB] = pat_parse_str,
56 [PAT_MATCH_DIR] = pat_parse_str,
57 [PAT_MATCH_DOM] = pat_parse_str,
58 [PAT_MATCH_END] = pat_parse_str,
59 [PAT_MATCH_REG] = pat_parse_reg,
Thierry FOURNIERed66c292013-11-28 11:05:19 +010060};
61
Thierry FOURNIERb9b08462013-12-13 15:12:32 +010062int (*pat_index_fcts[PAT_MATCH_NUM])(struct pattern_expr *, struct pattern *, char **) = {
63 [PAT_MATCH_FOUND] = pat_idx_list_val,
64 [PAT_MATCH_BOOL] = pat_idx_list_val,
65 [PAT_MATCH_INT] = pat_idx_list_val,
66 [PAT_MATCH_IP] = pat_idx_tree_ip,
67 [PAT_MATCH_BIN] = pat_idx_list_ptr,
68 [PAT_MATCH_LEN] = pat_idx_list_val,
69 [PAT_MATCH_STR] = pat_idx_tree_str,
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +020070 [PAT_MATCH_BEG] = pat_idx_tree_pfx,
Thierry FOURNIERb9b08462013-12-13 15:12:32 +010071 [PAT_MATCH_SUB] = pat_idx_list_str,
72 [PAT_MATCH_DIR] = pat_idx_list_str,
73 [PAT_MATCH_DOM] = pat_idx_list_str,
74 [PAT_MATCH_END] = pat_idx_list_str,
75 [PAT_MATCH_REG] = pat_idx_list_reg,
76};
77
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +010078void (*pat_delete_fcts[PAT_MATCH_NUM])(struct pattern_expr *, struct pat_ref_elt *) = {
Thierry FOURNIERb1136502014-01-15 11:38:49 +010079 [PAT_MATCH_FOUND] = pat_del_list_val,
80 [PAT_MATCH_BOOL] = pat_del_list_val,
81 [PAT_MATCH_INT] = pat_del_list_val,
82 [PAT_MATCH_IP] = pat_del_tree_ip,
83 [PAT_MATCH_BIN] = pat_del_list_ptr,
84 [PAT_MATCH_LEN] = pat_del_list_val,
85 [PAT_MATCH_STR] = pat_del_tree_str,
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +020086 [PAT_MATCH_BEG] = pat_del_tree_str,
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +010087 [PAT_MATCH_SUB] = pat_del_list_ptr,
88 [PAT_MATCH_DIR] = pat_del_list_ptr,
89 [PAT_MATCH_DOM] = pat_del_list_ptr,
90 [PAT_MATCH_END] = pat_del_list_ptr,
Thierry FOURNIERb1136502014-01-15 11:38:49 +010091 [PAT_MATCH_REG] = pat_del_list_reg,
92};
93
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +010094void (*pat_prune_fcts[PAT_MATCH_NUM])(struct pattern_expr *) = {
95 [PAT_MATCH_FOUND] = pat_prune_val,
96 [PAT_MATCH_BOOL] = pat_prune_val,
97 [PAT_MATCH_INT] = pat_prune_val,
98 [PAT_MATCH_IP] = pat_prune_val,
99 [PAT_MATCH_BIN] = pat_prune_ptr,
100 [PAT_MATCH_LEN] = pat_prune_val,
101 [PAT_MATCH_STR] = pat_prune_ptr,
102 [PAT_MATCH_BEG] = pat_prune_ptr,
103 [PAT_MATCH_SUB] = pat_prune_ptr,
104 [PAT_MATCH_DIR] = pat_prune_ptr,
105 [PAT_MATCH_DOM] = pat_prune_ptr,
106 [PAT_MATCH_END] = pat_prune_ptr,
107 [PAT_MATCH_REG] = pat_prune_reg,
108};
109
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100110struct pattern *(*pat_match_fcts[PAT_MATCH_NUM])(struct sample *, struct pattern_expr *, int) = {
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100111 [PAT_MATCH_FOUND] = NULL,
112 [PAT_MATCH_BOOL] = pat_match_nothing,
113 [PAT_MATCH_INT] = pat_match_int,
114 [PAT_MATCH_IP] = pat_match_ip,
115 [PAT_MATCH_BIN] = pat_match_bin,
116 [PAT_MATCH_LEN] = pat_match_len,
117 [PAT_MATCH_STR] = pat_match_str,
118 [PAT_MATCH_BEG] = pat_match_beg,
119 [PAT_MATCH_SUB] = pat_match_sub,
120 [PAT_MATCH_DIR] = pat_match_dir,
121 [PAT_MATCH_DOM] = pat_match_dom,
122 [PAT_MATCH_END] = pat_match_end,
123 [PAT_MATCH_REG] = pat_match_reg,
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100124};
125
Thierry FOURNIERe3ded592013-12-06 15:36:54 +0100126/* Just used for checking configuration compatibility */
127int pat_match_types[PAT_MATCH_NUM] = {
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200128 [PAT_MATCH_FOUND] = SMP_T_SINT,
129 [PAT_MATCH_BOOL] = SMP_T_SINT,
130 [PAT_MATCH_INT] = SMP_T_SINT,
Thierry FOURNIERe3ded592013-12-06 15:36:54 +0100131 [PAT_MATCH_IP] = SMP_T_ADDR,
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100132 [PAT_MATCH_BIN] = SMP_T_BIN,
133 [PAT_MATCH_LEN] = SMP_T_STR,
134 [PAT_MATCH_STR] = SMP_T_STR,
135 [PAT_MATCH_BEG] = SMP_T_STR,
136 [PAT_MATCH_SUB] = SMP_T_STR,
137 [PAT_MATCH_DIR] = SMP_T_STR,
138 [PAT_MATCH_DOM] = SMP_T_STR,
139 [PAT_MATCH_END] = SMP_T_STR,
140 [PAT_MATCH_REG] = SMP_T_STR,
Thierry FOURNIERe3ded592013-12-06 15:36:54 +0100141};
142
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +0100143/* this struct is used to return information */
144static struct pattern static_pattern;
145
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100146/* This is the root of the list of all pattern_ref avalaibles. */
147struct list pattern_reference = LIST_HEAD_INIT(pattern_reference);
148
Willy Tarreauf3045d22015-04-29 16:24:50 +0200149static struct lru64_head *pat_lru_tree;
150static unsigned long long pat_lru_seed;
151
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100152/*
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100153 *
154 * The following functions are not exported and are used by internals process
155 * of pattern matching
156 *
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100157 */
158
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100159/* Background: Fast way to find a zero byte in a word
160 * http://graphics.stanford.edu/~seander/bithacks.html#ZeroInWord
161 * hasZeroByte = (v - 0x01010101UL) & ~v & 0x80808080UL;
162 *
163 * To look for 4 different byte values, xor the word with those bytes and
164 * then check for zero bytes:
165 *
166 * v = (((unsigned char)c * 0x1010101U) ^ delimiter)
167 * where <delimiter> is the 4 byte values to look for (as an uint)
168 * and <c> is the character that is being tested
169 */
170static inline unsigned int is_delimiter(unsigned char c, unsigned int mask)
171{
172 mask ^= (c * 0x01010101); /* propagate the char to all 4 bytes */
173 return (mask - 0x01010101) & ~mask & 0x80808080U;
174}
175
176static inline unsigned int make_4delim(unsigned char d1, unsigned char d2, unsigned char d3, unsigned char d4)
177{
178 return d1 << 24 | d2 << 16 | d3 << 8 | d4;
179}
180
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100181
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100182/*
183 *
184 * These functions are exported and may be used by any other component.
185 *
Willy Tarreau5def8ef2014-08-29 15:19:33 +0200186 * The following functions are used for parsing pattern matching input value.
187 * The <text> contain the string to be parsed. <pattern> must be a preallocated
188 * pattern. The pat_parse_* functions fill this structure with the parsed value.
189 * <err> is filled with an error message built with memprintf() function. It is
190 * allowed to use a trash as a temporary storage for the returned pattern, as
191 * the next call after these functions will be pat_idx_*.
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100192 *
Willy Tarreau5def8ef2014-08-29 15:19:33 +0200193 * In success case, the pat_parse_* function returns 1. If the function
194 * fails, it returns 0 and <err> is filled.
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100195 */
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100196
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100197/* ignore the current line */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200198int pat_parse_nothing(const char *text, struct pattern *pattern, int mflags, char **err)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100199{
200 return 1;
201}
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100202
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100203/* Parse a string. It is allocated and duplicated. */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200204int pat_parse_str(const char *text, struct pattern *pattern, int mflags, char **err)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100205{
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100206 pattern->type = SMP_T_STR;
Thierry FOURNIERedc15c32013-12-13 15:36:59 +0100207 pattern->ptr.str = (char *)text;
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100208 pattern->len = strlen(text);
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100209 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100210}
211
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100212/* Parse a binary written in hexa. It is allocated. */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200213int pat_parse_bin(const char *text, struct pattern *pattern, int mflags, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100214{
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100215 struct chunk *trash;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100216
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100217 pattern->type = SMP_T_BIN;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100218 trash = get_trash_chunk();
219 pattern->len = trash->size;
220 pattern->ptr.str = trash->str;
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100221 return !!parse_binary(text, &pattern->ptr.str, &pattern->len, err);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100222}
223
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100224/* Parse a regex. It is allocated. */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200225int pat_parse_reg(const char *text, struct pattern *pattern, int mflags, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100226{
Thierry FOURNIER0b6d15f2014-01-29 19:35:16 +0100227 pattern->ptr.str = (char *)text;
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 FOURNIERe47e4e22014-04-28 11:18:57 +0200246int pat_parse_int(const char *text, struct pattern *pattern, int mflags, 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
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200250 pattern->type = SMP_T_SINT;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100251
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100252 /* Empty string is not valid */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100253 if (!*text)
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100254 goto not_valid_range;
255
256 /* Search ':' or '-' separator. */
257 while (*ptr != '\0' && *ptr != ':' && *ptr != '-')
258 ptr++;
259
260 /* If separator not found. */
261 if (!*ptr) {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100262 if (strl2llrc(text, ptr - text, &pattern->val.range.min) != 0) {
263 memprintf(err, "'%s' is not a number", text);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100264 return 0;
265 }
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100266 pattern->val.range.max = pattern->val.range.min;
267 pattern->val.range.min_set = 1;
268 pattern->val.range.max_set = 1;
269 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100270 }
271
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100272 /* If the separator is the first character. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100273 if (ptr == text && *(ptr + 1) != '\0') {
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100274 if (strl2llrc(ptr + 1, strlen(ptr + 1), &pattern->val.range.max) != 0)
275 goto not_valid_range;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100276
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100277 pattern->val.range.min_set = 0;
278 pattern->val.range.max_set = 1;
279 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100280 }
281
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100282 /* If separator is the last character. */
283 if (*(ptr + 1) == '\0') {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100284 if (strl2llrc(text, ptr - text, &pattern->val.range.min) != 0)
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100285 goto not_valid_range;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100286
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100287 pattern->val.range.min_set = 1;
288 pattern->val.range.max_set = 0;
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100289 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100290 }
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100291
292 /* Else, parse two numbers. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100293 if (strl2llrc(text, ptr - text, &pattern->val.range.min) != 0)
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100294 goto not_valid_range;
295
296 if (strl2llrc(ptr + 1, strlen(ptr + 1), &pattern->val.range.max) != 0)
297 goto not_valid_range;
298
299 if (pattern->val.range.min > pattern->val.range.max)
300 goto not_valid_range;
301
302 pattern->val.range.min_set = 1;
303 pattern->val.range.max_set = 1;
304 return 1;
305
306 not_valid_range:
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100307 memprintf(err, "'%s' is not a valid number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100308 return 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100309}
310
311/* Parse a range of positive 2-component versions delimited by either ':' or
312 * '-'. The version consists in a major and a minor, both of which must be
313 * smaller than 65536, because internally they will be represented as a 32-bit
314 * integer.
315 * If only one version is read, it is set as both min and max. Just like for
316 * pure integers, an operator may be specified as the prefix, among this list
317 * of 5 :
318 *
319 * 0:eq, 1:gt, 2:ge, 3:lt, 4:le
320 *
321 * The default operator is "eq". It supports range matching. Ranges are
322 * rejected for other operators. The operator may be changed at any time.
323 * The operator is stored in the 'opaque' argument. This allows constructs
324 * such as the following one :
325 *
326 * acl obsolete_ssl ssl_req_proto lt 3
327 * acl unsupported_ssl ssl_req_proto gt 3.1
328 * acl valid_ssl ssl_req_proto 3.0-3.1
329 *
330 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200331int pat_parse_dotted_ver(const char *text, struct pattern *pattern, int mflags, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100332{
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100333 const char *ptr = text;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100334
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200335 pattern->type = SMP_T_SINT;
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100336
337 /* Search ':' or '-' separator. */
338 while (*ptr != '\0' && *ptr != ':' && *ptr != '-')
339 ptr++;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100340
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100341 /* If separator not found. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100342 if (*ptr == '\0' && ptr > text) {
343 if (strl2llrc_dotted(text, ptr-text, &pattern->val.range.min) != 0) {
344 memprintf(err, "'%s' is not a dotted number", text);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100345 return 0;
346 }
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100347 pattern->val.range.max = pattern->val.range.min;
348 pattern->val.range.min_set = 1;
349 pattern->val.range.max_set = 1;
350 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100351 }
352
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100353 /* If the separator is the first character. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100354 if (ptr == text && *(ptr+1) != '\0') {
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100355 if (strl2llrc_dotted(ptr+1, strlen(ptr+1), &pattern->val.range.max) != 0) {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100356 memprintf(err, "'%s' is not a valid dotted number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100357 return 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100358 }
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100359 pattern->val.range.min_set = 0;
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 separator is the last character. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100365 if (ptr == &text[strlen(text)-1]) {
366 if (strl2llrc_dotted(text, ptr-text, &pattern->val.range.min) != 0) {
367 memprintf(err, "'%s' is not a valid dotted number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100368 return 0;
369 }
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100370 pattern->val.range.min_set = 1;
371 pattern->val.range.max_set = 0;
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100372 return 1;
373 }
374
375 /* Else, parse two numbers. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100376 if (strl2llrc_dotted(text, ptr-text, &pattern->val.range.min) != 0) {
377 memprintf(err, "'%s' is not a valid dotted number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100378 return 0;
379 }
380 if (strl2llrc_dotted(ptr+1, strlen(ptr+1), &pattern->val.range.max) != 0) {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100381 memprintf(err, "'%s' is not a valid dotted number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100382 return 0;
383 }
384 if (pattern->val.range.min > pattern->val.range.max) {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100385 memprintf(err, "'%s' is not a valid dotted number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100386 return 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100387 }
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100388 pattern->val.range.min_set = 1;
389 pattern->val.range.max_set = 1;
390 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100391}
392
393/* Parse an IP address and an optional mask in the form addr[/mask].
394 * The addr may either be an IPv4 address or a hostname. The mask
395 * may either be a dotted mask or a number of bits. Returns 1 if OK,
396 * otherwise 0. NOTE: IP address patterns are typed (IPV4/IPV6).
397 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200398int pat_parse_ip(const char *text, struct pattern *pattern, int mflags, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100399{
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200400 if (str2net(text, !(mflags & PAT_MF_NO_DNS) && (global.mode & MODE_STARTING),
Thierry FOURNIERfc7ac7b2014-02-11 15:23:04 +0100401 &pattern->val.ipv4.addr, &pattern->val.ipv4.mask)) {
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100402 pattern->type = SMP_T_IPV4;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100403 return 1;
404 }
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100405 else if (str62net(text, &pattern->val.ipv6.addr, &pattern->val.ipv6.mask)) {
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100406 pattern->type = SMP_T_IPV6;
407 return 1;
408 }
409 else {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100410 memprintf(err, "'%s' is not a valid IPv4 or IPv6 address", text);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100411 return 0;
412 }
413}
414
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100415/*
416 *
417 * These functions are exported and may be used by any other component.
418 *
419 * This fucntion just take a sample <smp> and check if this sample match
420 * with the pattern <pattern>. This fucntion return just PAT_MATCH or
421 * PAT_NOMATCH.
422 *
423 */
424
425/* always return false */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100426struct pattern *pat_match_nothing(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100427{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200428 if (smp->data.u.sint) {
Thierry FOURNIERe5978bf2014-03-17 19:53:10 +0100429 if (fill) {
Thierry FOURNIER503bb092015-08-19 08:35:43 +0200430 static_pattern.data = NULL;
Thierry FOURNIERe5978bf2014-03-17 19:53:10 +0100431 static_pattern.ref = NULL;
Thierry FOURNIERe5978bf2014-03-17 19:53:10 +0100432 static_pattern.type = 0;
433 static_pattern.ptr.str = NULL;
434 }
435 return &static_pattern;
436 }
437 else
438 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100439}
440
441
442/* NB: For two strings to be identical, it is required that their lengths match */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100443struct pattern *pat_match_str(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100444{
445 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100446 struct ebmb_node *node;
447 char prev;
448 struct pattern_tree *elt;
449 struct pattern_list *lst;
450 struct pattern *pattern;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200451 struct pattern *ret = NULL;
452 struct lru64 *lru = NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100453
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100454 /* Lookup a string in the expression's pattern tree. */
455 if (!eb_is_empty(&expr->pattern_tree)) {
456 /* we may have to force a trailing zero on the test pattern */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200457 prev = smp->data.u.str.str[smp->data.u.str.len];
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100458 if (prev)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200459 smp->data.u.str.str[smp->data.u.str.len] = '\0';
460 node = ebst_lookup(&expr->pattern_tree, smp->data.u.str.str);
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100461 if (prev)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200462 smp->data.u.str.str[smp->data.u.str.len] = prev;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100463
464 if (node) {
465 if (fill) {
466 elt = ebmb_entry(node, struct pattern_tree, node);
Thierry FOURNIER503bb092015-08-19 08:35:43 +0200467 static_pattern.data = elt->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +0100468 static_pattern.ref = elt->ref;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200469 static_pattern.sflags = PAT_SF_TREE;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100470 static_pattern.type = SMP_T_STR;
471 static_pattern.ptr.str = (char *)elt->node.key;
472 }
473 return &static_pattern;
474 }
475 }
476
477 /* look in the list */
Willy Tarreauf3045d22015-04-29 16:24:50 +0200478 if (pat_lru_tree) {
Willy Tarreauaee93142015-05-04 17:18:42 +0200479 unsigned long long seed = pat_lru_seed ^ (long)expr;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200480
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200481 lru = lru64_get(XXH64(smp->data.u.str.str, smp->data.u.str.len, seed),
Willy Tarreauf3045d22015-04-29 16:24:50 +0200482 pat_lru_tree, expr, expr->revision);
483 if (lru && lru->domain)
484 return lru->data;
485 }
486
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100487 list_for_each_entry(lst, &expr->patterns, list) {
488 pattern = &lst->pat;
489
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200490 if (pattern->len != smp->data.u.str.len)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100491 continue;
492
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200493 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200494 if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.str.str, smp->data.u.str.len) == 0) ||
495 (!icase && strncmp(pattern->ptr.str, smp->data.u.str.str, smp->data.u.str.len) == 0)) {
Willy Tarreauf3045d22015-04-29 16:24:50 +0200496 ret = pattern;
497 break;
498 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100499 }
500
Willy Tarreauf3045d22015-04-29 16:24:50 +0200501 if (lru)
Christopher Fauletf90ac552015-06-09 17:06:17 +0200502 lru64_commit(lru, ret, expr, expr->revision, NULL);
Willy Tarreauf3045d22015-04-29 16:24:50 +0200503
504 return ret;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100505}
506
507/* NB: For two binaries buf to be identical, it is required that their lengths match */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100508struct pattern *pat_match_bin(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100509{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100510 struct pattern_list *lst;
511 struct pattern *pattern;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200512 struct pattern *ret = NULL;
513 struct lru64 *lru = NULL;
514
515 if (pat_lru_tree) {
Willy Tarreauaee93142015-05-04 17:18:42 +0200516 unsigned long long seed = pat_lru_seed ^ (long)expr;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100517
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200518 lru = lru64_get(XXH64(smp->data.u.str.str, smp->data.u.str.len, seed),
Willy Tarreauf3045d22015-04-29 16:24:50 +0200519 pat_lru_tree, expr, expr->revision);
520 if (lru && lru->domain)
521 return lru->data;
522 }
523
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100524 list_for_each_entry(lst, &expr->patterns, list) {
525 pattern = &lst->pat;
526
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200527 if (pattern->len != smp->data.u.str.len)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100528 continue;
529
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200530 if (memcmp(pattern->ptr.str, smp->data.u.str.str, smp->data.u.str.len) == 0) {
Willy Tarreauf3045d22015-04-29 16:24:50 +0200531 ret = pattern;
532 break;
533 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100534 }
535
Willy Tarreauf3045d22015-04-29 16:24:50 +0200536 if (lru)
Christopher Fauletf90ac552015-06-09 17:06:17 +0200537 lru64_commit(lru, ret, expr, expr->revision, NULL);
Willy Tarreauf3045d22015-04-29 16:24:50 +0200538
539 return ret;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100540}
541
542/* Executes a regex. It temporarily changes the data to add a trailing zero,
543 * and restores the previous character when leaving.
544 */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100545struct pattern *pat_match_reg(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100546{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100547 struct pattern_list *lst;
548 struct pattern *pattern;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200549 struct pattern *ret = NULL;
550 struct lru64 *lru = NULL;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100551
Willy Tarreauf3045d22015-04-29 16:24:50 +0200552 if (pat_lru_tree) {
Willy Tarreauaee93142015-05-04 17:18:42 +0200553 unsigned long long seed = pat_lru_seed ^ (long)expr;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200554
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200555 lru = lru64_get(XXH64(smp->data.u.str.str, smp->data.u.str.len, seed),
Willy Tarreauf3045d22015-04-29 16:24:50 +0200556 pat_lru_tree, expr, expr->revision);
557 if (lru && lru->domain)
558 return lru->data;
559 }
560
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100561 list_for_each_entry(lst, &expr->patterns, list) {
562 pattern = &lst->pat;
563
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200564 if (regex_exec2(pattern->ptr.reg, smp->data.u.str.str, smp->data.u.str.len)) {
Willy Tarreauf3045d22015-04-29 16:24:50 +0200565 ret = pattern;
566 break;
567 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100568 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200569
570 if (lru)
Christopher Fauletf90ac552015-06-09 17:06:17 +0200571 lru64_commit(lru, ret, expr, expr->revision, NULL);
Willy Tarreauf3045d22015-04-29 16:24:50 +0200572
573 return ret;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100574}
575
576/* Checks that the pattern matches the beginning of the tested string. */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100577struct pattern *pat_match_beg(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100578{
579 int icase;
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +0200580 struct ebmb_node *node;
581 char prev;
582 struct pattern_tree *elt;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100583 struct pattern_list *lst;
584 struct pattern *pattern;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200585 struct pattern *ret = NULL;
586 struct lru64 *lru = NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100587
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +0200588 /* Lookup a string in the expression's pattern tree. */
589 if (!eb_is_empty(&expr->pattern_tree)) {
590 /* we may have to force a trailing zero on the test pattern */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200591 prev = smp->data.u.str.str[smp->data.u.str.len];
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +0200592 if (prev)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200593 smp->data.u.str.str[smp->data.u.str.len] = '\0';
594 node = ebmb_lookup_longest(&expr->pattern_tree, smp->data.u.str.str);
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +0200595 if (prev)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200596 smp->data.u.str.str[smp->data.u.str.len] = prev;
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +0200597
598 if (node) {
599 if (fill) {
600 elt = ebmb_entry(node, struct pattern_tree, node);
Thierry FOURNIER503bb092015-08-19 08:35:43 +0200601 static_pattern.data = elt->data;
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +0200602 static_pattern.ref = elt->ref;
603 static_pattern.sflags = PAT_SF_TREE;
604 static_pattern.type = SMP_T_STR;
605 static_pattern.ptr.str = (char *)elt->node.key;
606 }
607 return &static_pattern;
608 }
609 }
610
611 /* look in the list */
Willy Tarreauf3045d22015-04-29 16:24:50 +0200612 if (pat_lru_tree) {
Willy Tarreauaee93142015-05-04 17:18:42 +0200613 unsigned long long seed = pat_lru_seed ^ (long)expr;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200614
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200615 lru = lru64_get(XXH64(smp->data.u.str.str, smp->data.u.str.len, seed),
Willy Tarreauf3045d22015-04-29 16:24:50 +0200616 pat_lru_tree, expr, expr->revision);
617 if (lru && lru->domain)
618 return lru->data;
619 }
620
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100621 list_for_each_entry(lst, &expr->patterns, list) {
622 pattern = &lst->pat;
623
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200624 if (pattern->len > smp->data.u.str.len)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100625 continue;
626
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200627 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200628 if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.str.str, pattern->len) != 0) ||
629 (!icase && strncmp(pattern->ptr.str, smp->data.u.str.str, pattern->len) != 0))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100630 continue;
631
Willy Tarreauf3045d22015-04-29 16:24:50 +0200632 ret = pattern;
633 break;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100634 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200635
636 if (lru)
Christopher Fauletf90ac552015-06-09 17:06:17 +0200637 lru64_commit(lru, ret, expr, expr->revision, NULL);
Willy Tarreauf3045d22015-04-29 16:24:50 +0200638
639 return ret;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100640}
641
642/* Checks that the pattern matches the end of the tested string. */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100643struct pattern *pat_match_end(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100644{
645 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100646 struct pattern_list *lst;
647 struct pattern *pattern;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200648 struct pattern *ret = NULL;
649 struct lru64 *lru = NULL;
650
651 if (pat_lru_tree) {
Willy Tarreauaee93142015-05-04 17:18:42 +0200652 unsigned long long seed = pat_lru_seed ^ (long)expr;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200653
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200654 lru = lru64_get(XXH64(smp->data.u.str.str, smp->data.u.str.len, seed),
Willy Tarreauf3045d22015-04-29 16:24:50 +0200655 pat_lru_tree, expr, expr->revision);
656 if (lru && lru->domain)
657 return lru->data;
658 }
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100659
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100660 list_for_each_entry(lst, &expr->patterns, list) {
661 pattern = &lst->pat;
662
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200663 if (pattern->len > smp->data.u.str.len)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100664 continue;
665
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200666 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200667 if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.str.str + smp->data.u.str.len - pattern->len, pattern->len) != 0) ||
668 (!icase && strncmp(pattern->ptr.str, smp->data.u.str.str + smp->data.u.str.len - pattern->len, pattern->len) != 0))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100669 continue;
670
Willy Tarreauf3045d22015-04-29 16:24:50 +0200671 ret = pattern;
672 break;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100673 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200674
675 if (lru)
Christopher Fauletf90ac552015-06-09 17:06:17 +0200676 lru64_commit(lru, ret, expr, expr->revision, NULL);
Willy Tarreauf3045d22015-04-29 16:24:50 +0200677
678 return ret;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100679}
680
681/* Checks that the pattern is included inside the tested string.
682 * NB: Suboptimal, should be rewritten using a Boyer-Moore method.
683 */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100684struct pattern *pat_match_sub(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100685{
686 int icase;
687 char *end;
688 char *c;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100689 struct pattern_list *lst;
690 struct pattern *pattern;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200691 struct pattern *ret = NULL;
692 struct lru64 *lru = NULL;
693
694 if (pat_lru_tree) {
Willy Tarreauaee93142015-05-04 17:18:42 +0200695 unsigned long long seed = pat_lru_seed ^ (long)expr;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200696
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200697 lru = lru64_get(XXH64(smp->data.u.str.str, smp->data.u.str.len, seed),
Willy Tarreauf3045d22015-04-29 16:24:50 +0200698 pat_lru_tree, expr, expr->revision);
699 if (lru && lru->domain)
700 return lru->data;
701 }
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100702
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100703 list_for_each_entry(lst, &expr->patterns, list) {
704 pattern = &lst->pat;
705
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200706 if (pattern->len > smp->data.u.str.len)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100707 continue;
708
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200709 end = smp->data.u.str.str + smp->data.u.str.len - pattern->len;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200710 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100711 if (icase) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200712 for (c = smp->data.u.str.str; c <= end; c++) {
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100713 if (tolower(*c) != tolower(*pattern->ptr.str))
714 continue;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200715 if (strncasecmp(pattern->ptr.str, c, pattern->len) == 0) {
716 ret = pattern;
717 goto leave;
718 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100719 }
720 } else {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200721 for (c = smp->data.u.str.str; c <= end; c++) {
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100722 if (*c != *pattern->ptr.str)
723 continue;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200724 if (strncmp(pattern->ptr.str, c, pattern->len) == 0) {
725 ret = pattern;
726 goto leave;
727 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100728 }
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100729 }
730 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200731 leave:
732 if (lru)
Christopher Fauletf90ac552015-06-09 17:06:17 +0200733 lru64_commit(lru, ret, expr, expr->revision, NULL);
Willy Tarreauf3045d22015-04-29 16:24:50 +0200734
735 return ret;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100736}
737
738/* This one is used by other real functions. It checks that the pattern is
739 * included inside the tested string, but enclosed between the specified
740 * delimiters or at the beginning or end of the string. The delimiters are
741 * provided as an unsigned int made by make_4delim() and match up to 4 different
742 * delimiters. Delimiters are stripped at the beginning and end of the pattern.
743 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200744static int match_word(struct sample *smp, struct pattern *pattern, int mflags, unsigned int delimiters)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100745{
746 int may_match, icase;
747 char *c, *end;
748 char *ps;
749 int pl;
750
751 pl = pattern->len;
752 ps = pattern->ptr.str;
753
754 while (pl > 0 && is_delimiter(*ps, delimiters)) {
755 pl--;
756 ps++;
757 }
758
759 while (pl > 0 && is_delimiter(ps[pl - 1], delimiters))
760 pl--;
761
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200762 if (pl > smp->data.u.str.len)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100763 return PAT_NOMATCH;
764
765 may_match = 1;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200766 icase = mflags & PAT_MF_IGNORE_CASE;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200767 end = smp->data.u.str.str + smp->data.u.str.len - pl;
768 for (c = smp->data.u.str.str; c <= end; c++) {
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100769 if (is_delimiter(*c, delimiters)) {
770 may_match = 1;
771 continue;
772 }
773
774 if (!may_match)
775 continue;
776
777 if (icase) {
778 if ((tolower(*c) == tolower(*ps)) &&
779 (strncasecmp(ps, c, pl) == 0) &&
780 (c == end || is_delimiter(c[pl], delimiters)))
781 return PAT_MATCH;
782 } else {
783 if ((*c == *ps) &&
784 (strncmp(ps, c, pl) == 0) &&
785 (c == end || is_delimiter(c[pl], delimiters)))
786 return PAT_MATCH;
787 }
788 may_match = 0;
789 }
790 return PAT_NOMATCH;
791}
792
793/* Checks that the pattern is included inside the tested string, but enclosed
794 * between the delimiters '?' or '/' or at the beginning or end of the string.
795 * Delimiters at the beginning or end of the pattern are ignored.
796 */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100797struct pattern *pat_match_dir(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100798{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100799 struct pattern_list *lst;
800 struct pattern *pattern;
801
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100802 list_for_each_entry(lst, &expr->patterns, list) {
803 pattern = &lst->pat;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200804 if (match_word(smp, pattern, expr->mflags, make_4delim('/', '?', '?', '?')))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100805 return pattern;
806 }
807 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100808}
809
810/* Checks that the pattern is included inside the tested string, but enclosed
811 * between the delmiters '/', '?', '.' or ":" or at the beginning or end of
812 * the string. Delimiters at the beginning or end of the pattern are ignored.
813 */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100814struct pattern *pat_match_dom(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100815{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100816 struct pattern_list *lst;
817 struct pattern *pattern;
818
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100819 list_for_each_entry(lst, &expr->patterns, list) {
820 pattern = &lst->pat;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200821 if (match_word(smp, pattern, expr->mflags, make_4delim('/', '?', '.', ':')))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100822 return pattern;
823 }
824 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100825}
826
827/* Checks that the integer in <test> is included between min and max */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100828struct pattern *pat_match_int(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100829{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100830 struct pattern_list *lst;
831 struct pattern *pattern;
832
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100833 list_for_each_entry(lst, &expr->patterns, list) {
834 pattern = &lst->pat;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200835 if ((!pattern->val.range.min_set || pattern->val.range.min <= smp->data.u.sint) &&
836 (!pattern->val.range.max_set || smp->data.u.sint <= pattern->val.range.max))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100837 return pattern;
838 }
839 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100840}
841
842/* Checks that the length of the pattern in <test> is included between min and max */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100843struct pattern *pat_match_len(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100844{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100845 struct pattern_list *lst;
846 struct pattern *pattern;
847
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100848 list_for_each_entry(lst, &expr->patterns, list) {
849 pattern = &lst->pat;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200850 if ((!pattern->val.range.min_set || pattern->val.range.min <= smp->data.u.str.len) &&
851 (!pattern->val.range.max_set || smp->data.u.str.len <= pattern->val.range.max))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100852 return pattern;
853 }
854 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100855}
856
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100857struct pattern *pat_match_ip(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100858{
859 unsigned int v4; /* in network byte order */
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100860 struct in6_addr tmp6;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100861 struct in_addr *s;
862 struct ebmb_node *node;
863 struct pattern_tree *elt;
864 struct pattern_list *lst;
865 struct pattern *pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100866
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100867 /* The input sample is IPv4. Try to match in the trees. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200868 if (smp->data.type == SMP_T_IPV4) {
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100869 /* Lookup an IPv4 address in the expression's pattern tree using
870 * the longest match method.
871 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200872 s = &smp->data.u.ipv4;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100873 node = ebmb_lookup_longest(&expr->pattern_tree, &s->s_addr);
874 if (node) {
875 if (fill) {
876 elt = ebmb_entry(node, struct pattern_tree, node);
Thierry FOURNIER503bb092015-08-19 08:35:43 +0200877 static_pattern.data = elt->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +0100878 static_pattern.ref = elt->ref;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200879 static_pattern.sflags = PAT_SF_TREE;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100880 static_pattern.type = SMP_T_IPV4;
881 memcpy(&static_pattern.val.ipv4.addr.s_addr, elt->node.key, 4);
882 if (!cidr2dotted(elt->node.node.pfx, &static_pattern.val.ipv4.mask))
883 return NULL;
884 }
885 return &static_pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100886 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100887
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100888 /* The IPv4 sample dont match the IPv4 tree. Convert the IPv4
889 * sample address to IPv6 with the mapping method using the ::ffff:
890 * prefix, and try to lookup in the IPv6 tree.
891 */
892 memset(&tmp6, 0, 10);
893 *(uint16_t*)&tmp6.s6_addr[10] = htons(0xffff);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200894 *(uint32_t*)&tmp6.s6_addr[12] = smp->data.u.ipv4.s_addr;
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100895 node = ebmb_lookup_longest(&expr->pattern_tree_2, &tmp6);
896 if (node) {
897 if (fill) {
898 elt = ebmb_entry(node, struct pattern_tree, node);
Thierry FOURNIER503bb092015-08-19 08:35:43 +0200899 static_pattern.data = elt->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +0100900 static_pattern.ref = elt->ref;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200901 static_pattern.sflags = PAT_SF_TREE;
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100902 static_pattern.type = SMP_T_IPV6;
903 memcpy(&static_pattern.val.ipv6.addr, elt->node.key, 16);
904 static_pattern.val.ipv6.mask = elt->node.node.pfx;
905 }
906 return &static_pattern;
907 }
908 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100909
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100910 /* The input sample is IPv6. Try to match in the trees. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200911 if (smp->data.type == SMP_T_IPV6) {
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100912 /* Lookup an IPv6 address in the expression's pattern tree using
913 * the longest match method.
914 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200915 node = ebmb_lookup_longest(&expr->pattern_tree_2, &smp->data.u.ipv6);
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100916 if (node) {
917 if (fill) {
918 elt = ebmb_entry(node, struct pattern_tree, node);
Thierry FOURNIER503bb092015-08-19 08:35:43 +0200919 static_pattern.data = elt->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +0100920 static_pattern.ref = elt->ref;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200921 static_pattern.sflags = PAT_SF_TREE;
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100922 static_pattern.type = SMP_T_IPV6;
923 memcpy(&static_pattern.val.ipv6.addr, elt->node.key, 16);
924 static_pattern.val.ipv6.mask = elt->node.node.pfx;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100925 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100926 return &static_pattern;
927 }
928
929 /* Try to convert 6 to 4 when the start of the ipv6 address match the
930 * following forms :
931 * - ::ffff:ip:v4 (ipv4 mapped)
932 * - ::0000:ip:v4 (old ipv4 mapped)
933 * - 2002:ip:v4:: (6to4)
934 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200935 if ((*(uint32_t*)&smp->data.u.ipv6.s6_addr[0] == 0 &&
936 *(uint32_t*)&smp->data.u.ipv6.s6_addr[4] == 0 &&
937 (*(uint32_t*)&smp->data.u.ipv6.s6_addr[8] == 0 ||
938 *(uint32_t*)&smp->data.u.ipv6.s6_addr[8] == htonl(0xFFFF))) ||
939 *(uint16_t*)&smp->data.u.ipv6.s6_addr[0] == htons(0x2002)) {
940 if (*(uint32_t*)&smp->data.u.ipv6.s6_addr[0] == 0)
941 v4 = *(uint32_t*)&smp->data.u.ipv6.s6_addr[12];
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100942 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200943 v4 = htonl((ntohs(*(uint16_t*)&smp->data.u.ipv6.s6_addr[2]) << 16) +
944 ntohs(*(uint16_t*)&smp->data.u.ipv6.s6_addr[4]));
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100945
946 /* Lookup an IPv4 address in the expression's pattern tree using the longest
947 * match method.
948 */
949 node = ebmb_lookup_longest(&expr->pattern_tree, &v4);
950 if (node) {
951 if (fill) {
952 elt = ebmb_entry(node, struct pattern_tree, node);
Thierry FOURNIER503bb092015-08-19 08:35:43 +0200953 static_pattern.data = elt->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +0100954 static_pattern.ref = elt->ref;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200955 static_pattern.sflags = PAT_SF_TREE;
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100956 static_pattern.type = SMP_T_IPV4;
957 memcpy(&static_pattern.val.ipv4.addr.s_addr, elt->node.key, 4);
958 if (!cidr2dotted(elt->node.node.pfx, &static_pattern.val.ipv4.mask))
959 return NULL;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100960 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100961 return &static_pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100962 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100963 }
964 }
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100965
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100966 /* Lookup in the list. the list contain only IPv4 patterns */
967 list_for_each_entry(lst, &expr->patterns, list) {
968 pattern = &lst->pat;
969
970 /* The input sample is IPv4, use it as is. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200971 if (smp->data.type == SMP_T_IPV4) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200972 v4 = smp->data.u.ipv4.s_addr;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100973 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200974 else if (smp->data.type == SMP_T_IPV6) {
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100975 /* v4 match on a V6 sample. We want to check at least for
976 * the following forms :
977 * - ::ffff:ip:v4 (ipv4 mapped)
978 * - ::0000:ip:v4 (old ipv4 mapped)
979 * - 2002:ip:v4:: (6to4)
980 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200981 if (*(uint32_t*)&smp->data.u.ipv6.s6_addr[0] == 0 &&
982 *(uint32_t*)&smp->data.u.ipv6.s6_addr[4] == 0 &&
983 (*(uint32_t*)&smp->data.u.ipv6.s6_addr[8] == 0 ||
984 *(uint32_t*)&smp->data.u.ipv6.s6_addr[8] == htonl(0xFFFF))) {
985 v4 = *(uint32_t*)&smp->data.u.ipv6.s6_addr[12];
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100986 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200987 else if (*(uint16_t*)&smp->data.u.ipv6.s6_addr[0] == htons(0x2002)) {
988 v4 = htonl((ntohs(*(uint16_t*)&smp->data.u.ipv6.s6_addr[2]) << 16) +
989 ntohs(*(uint16_t*)&smp->data.u.ipv6.s6_addr[4]));
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100990 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100991 else
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100992 continue;
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100993 }
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100994
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100995 /* Check if the input sample match the current pattern. */
996 if (((v4 ^ pattern->val.ipv4.addr.s_addr) & pattern->val.ipv4.mask.s_addr) == 0)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100997 return pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100998 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100999 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +01001000}
1001
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001002void free_pattern_tree(struct eb_root *root)
1003{
1004 struct eb_node *node, *next;
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +01001005 struct pattern_tree *elt;
Thierry FOURNIER3ce88c72013-12-09 11:29:46 +01001006
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001007 node = eb_first(root);
1008 while (node) {
1009 next = eb_next(node);
1010 eb_delete(node);
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +01001011 elt = container_of(node, struct pattern_tree, node);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001012 free(elt->data);
Thierry FOURNIER3ce88c72013-12-09 11:29:46 +01001013 free(elt);
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001014 node = next;
1015 }
1016}
1017
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01001018void pat_prune_val(struct pattern_expr *expr)
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +01001019{
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01001020 struct pattern_list *pat, *tmp;
1021
1022 list_for_each_entry_safe(pat, tmp, &expr->patterns, list) {
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001023 free(pat->pat.data);
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01001024 free(pat);
1025 }
1026
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +01001027 free_pattern_tree(&expr->pattern_tree);
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001028 free_pattern_tree(&expr->pattern_tree_2);
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +01001029 LIST_INIT(&expr->patterns);
1030}
1031
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01001032void pat_prune_ptr(struct pattern_expr *expr)
1033{
1034 struct pattern_list *pat, *tmp;
1035
1036 list_for_each_entry_safe(pat, tmp, &expr->patterns, list) {
1037 free(pat->pat.ptr.ptr);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001038 free(pat->pat.data);
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01001039 free(pat);
1040 }
1041
1042 free_pattern_tree(&expr->pattern_tree);
1043 free_pattern_tree(&expr->pattern_tree_2);
1044 LIST_INIT(&expr->patterns);
1045}
1046
1047void pat_prune_reg(struct pattern_expr *expr)
1048{
1049 struct pattern_list *pat, *tmp;
1050
1051 list_for_each_entry_safe(pat, tmp, &expr->patterns, list) {
1052 regex_free(pat->pat.ptr.ptr);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001053 free(pat->pat.data);
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01001054 free(pat);
1055 }
1056
1057 free_pattern_tree(&expr->pattern_tree);
1058 free_pattern_tree(&expr->pattern_tree_2);
1059 LIST_INIT(&expr->patterns);
1060}
1061
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001062/*
1063 *
1064 * The following functions are used for the pattern indexation
1065 *
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001066 */
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001067
1068int pat_idx_list_val(struct pattern_expr *expr, struct pattern *pat, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001069{
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001070 struct pattern_list *patl;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001071
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001072 /* allocate pattern */
1073 patl = calloc(1, sizeof(*patl));
1074 if (!patl) {
1075 memprintf(err, "out of memory while indexing pattern");
Thierry FOURNIER972028f2014-01-23 17:53:31 +01001076 return 0;
1077 }
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001078
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001079 /* duplicate pattern */
1080 memcpy(&patl->pat, pat, sizeof(*pat));
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001081
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001082 /* chain pattern in the expression */
1083 LIST_ADDQ(&expr->patterns, &patl->list);
Willy Tarreau72f073b2015-04-29 17:53:47 +02001084 expr->revision = rdtsc();
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001085
1086 /* that's ok */
1087 return 1;
1088}
1089
1090int pat_idx_list_ptr(struct pattern_expr *expr, struct pattern *pat, char **err)
1091{
1092 struct pattern_list *patl;
1093
1094 /* allocate pattern */
1095 patl = calloc(1, sizeof(*patl));
Thierry FOURNIER8aa83842015-02-06 17:50:55 +01001096 if (!patl) {
1097 memprintf(err, "out of memory while indexing pattern");
Thierry FOURNIER972028f2014-01-23 17:53:31 +01001098 return 0;
Thierry FOURNIER8aa83842015-02-06 17:50:55 +01001099 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001100
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001101 /* duplicate pattern */
1102 memcpy(&patl->pat, pat, sizeof(*pat));
1103 patl->pat.ptr.ptr = malloc(patl->pat.len);
1104 if (!patl->pat.ptr.ptr) {
1105 free(patl);
1106 memprintf(err, "out of memory while indexing pattern");
1107 return 0;
1108 }
1109 memcpy(patl->pat.ptr.ptr, pat->ptr.ptr, pat->len);
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001110
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001111 /* chain pattern in the expression */
1112 LIST_ADDQ(&expr->patterns, &patl->list);
Willy Tarreau72f073b2015-04-29 17:53:47 +02001113 expr->revision = rdtsc();
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001114
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001115 /* that's ok */
1116 return 1;
1117}
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001118
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001119int pat_idx_list_str(struct pattern_expr *expr, struct pattern *pat, char **err)
1120{
1121 struct pattern_list *patl;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001122
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001123 /* allocate pattern */
1124 patl = calloc(1, sizeof(*patl));
1125 if (!patl) {
1126 memprintf(err, "out of memory while indexing pattern");
1127 return 0;
1128 }
1129
1130 /* duplicate pattern */
1131 memcpy(&patl->pat, pat, sizeof(*pat));
1132 patl->pat.ptr.str = malloc(patl->pat.len + 1);
1133 if (!patl->pat.ptr.str) {
1134 free(patl);
1135 memprintf(err, "out of memory while indexing pattern");
1136 return 0;
1137 }
1138 memcpy(patl->pat.ptr.ptr, pat->ptr.ptr, pat->len);
1139 patl->pat.ptr.str[patl->pat.len] = '\0';
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001140
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001141 /* chain pattern in the expression */
1142 LIST_ADDQ(&expr->patterns, &patl->list);
Willy Tarreau72f073b2015-04-29 17:53:47 +02001143 expr->revision = rdtsc();
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001144
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001145 /* that's ok */
1146 return 1;
1147}
1148
1149int pat_idx_list_reg(struct pattern_expr *expr, struct pattern *pat, char **err)
1150{
1151 struct pattern_list *patl;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001152
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001153 /* allocate pattern */
1154 patl = calloc(1, sizeof(*patl));
1155 if (!patl) {
1156 memprintf(err, "out of memory while indexing pattern");
1157 return 0;
Thierry FOURNIER972028f2014-01-23 17:53:31 +01001158 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001159
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001160 /* duplicate pattern */
1161 memcpy(&patl->pat, pat, sizeof(*pat));
1162
1163 /* allocate regex */
1164 patl->pat.ptr.reg = calloc(1, sizeof(*patl->pat.ptr.reg));
1165 if (!patl->pat.ptr.reg) {
1166 free(patl);
1167 memprintf(err, "out of memory while indexing pattern");
1168 return 0;
1169 }
1170
1171 /* compile regex */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02001172 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 +01001173 free(patl->pat.ptr.reg);
Dirkjan Bussink07fcaaa2014-04-28 22:57:16 +00001174 free(patl);
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001175 return 0;
1176 }
1177
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001178 /* chain pattern in the expression */
1179 LIST_ADDQ(&expr->patterns, &patl->list);
Willy Tarreau72f073b2015-04-29 17:53:47 +02001180 expr->revision = rdtsc();
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001181
1182 /* that's ok */
1183 return 1;
1184}
1185
1186int pat_idx_tree_ip(struct pattern_expr *expr, struct pattern *pat, char **err)
1187{
1188 unsigned int mask;
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +01001189 struct pattern_tree *node;
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001190
1191 /* Only IPv4 can be indexed */
1192 if (pat->type == SMP_T_IPV4) {
Thierry FOURNIER972028f2014-01-23 17:53:31 +01001193 /* in IPv4 case, check if the mask is contiguous so that we can
1194 * insert the network into the tree. A continuous mask has only
1195 * ones on the left. This means that this mask + its lower bit
1196 * added once again is null.
1197 */
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001198 mask = ntohl(pat->val.ipv4.mask.s_addr);
1199 if (mask + (mask & -mask) == 0) {
1200 mask = mask ? 33 - flsnz(mask & -mask) : 0; /* equals cidr value */
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001201
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001202 /* node memory allocation */
1203 node = calloc(1, sizeof(*node) + 4);
1204 if (!node) {
1205 memprintf(err, "out of memory while loading pattern");
1206 return 0;
1207 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001208
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001209 /* copy the pointer to sample associated to this node */
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001210 node->data = pat->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01001211 node->ref = pat->ref;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001212
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001213 /* FIXME: insert <addr>/<mask> into the tree here */
1214 memcpy(node->node.key, &pat->val.ipv4.addr, 4); /* network byte order */
1215 node->node.node.pfx = mask;
Thierry FOURNIER31db4ae2014-01-30 00:27:15 +01001216
1217 /* Insert the entry. */
1218 ebmb_insert_prefix(&expr->pattern_tree, &node->node, 4);
Willy Tarreau72f073b2015-04-29 17:53:47 +02001219 expr->revision = rdtsc();
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001220
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001221 /* that's ok */
1222 return 1;
1223 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001224 else {
1225 /* If the mask is not contiguous, just add the pattern to the list */
1226 return pat_idx_list_val(expr, pat, err);
1227 }
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001228 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001229 else if (pat->type == SMP_T_IPV6) {
1230 /* IPv6 also can be indexed */
1231 node = calloc(1, sizeof(*node) + 16);
1232 if (!node) {
1233 memprintf(err, "out of memory while loading pattern");
1234 return 0;
1235 }
1236
1237 /* copy the pointer to sample associated to this node */
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001238 node->data = pat->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01001239 node->ref = pat->ref;
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001240
1241 /* FIXME: insert <addr>/<mask> into the tree here */
1242 memcpy(node->node.key, &pat->val.ipv6.addr, 16); /* network byte order */
1243 node->node.node.pfx = pat->val.ipv6.mask;
Thierry FOURNIER31db4ae2014-01-30 00:27:15 +01001244
1245 /* Insert the entry. */
1246 ebmb_insert_prefix(&expr->pattern_tree_2, &node->node, 16);
Willy Tarreau72f073b2015-04-29 17:53:47 +02001247 expr->revision = rdtsc();
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001248
1249 /* that's ok */
1250 return 1;
1251 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001252
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001253 return 0;
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001254}
1255
1256int pat_idx_tree_str(struct pattern_expr *expr, struct pattern *pat, char **err)
1257{
1258 int len;
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +01001259 struct pattern_tree *node;
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001260
1261 /* Only string can be indexed */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001262 if (pat->type != SMP_T_STR) {
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001263 memprintf(err, "internal error: string expected, but the type is '%s'",
1264 smp_to_type[pat->type]);
1265 return 0;
Thierry FOURNIER972028f2014-01-23 17:53:31 +01001266 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001267
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001268 /* If the flag PAT_F_IGNORE_CASE is set, we cannot use trees */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02001269 if (expr->mflags & PAT_MF_IGNORE_CASE)
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001270 return pat_idx_list_str(expr, pat, err);
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001271
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001272 /* Process the key len */
1273 len = strlen(pat->ptr.str) + 1;
1274
1275 /* node memory allocation */
1276 node = calloc(1, sizeof(*node) + len);
1277 if (!node) {
1278 memprintf(err, "out of memory while loading pattern");
1279 return 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001280 }
1281
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001282 /* copy the pointer to sample associated to this node */
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001283 node->data = pat->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01001284 node->ref = pat->ref;
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001285
1286 /* copy the string */
1287 memcpy(node->node.key, pat->ptr.str, len);
1288
1289 /* index the new node */
Thierry FOURNIER31db4ae2014-01-30 00:27:15 +01001290 ebst_insert(&expr->pattern_tree, &node->node);
Willy Tarreau72f073b2015-04-29 17:53:47 +02001291 expr->revision = rdtsc();
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001292
1293 /* that's ok */
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001294 return 1;
1295}
1296
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +02001297int pat_idx_tree_pfx(struct pattern_expr *expr, struct pattern *pat, char **err)
1298{
1299 int len;
1300 struct pattern_tree *node;
1301
1302 /* Only string can be indexed */
1303 if (pat->type != SMP_T_STR) {
1304 memprintf(err, "internal error: string expected, but the type is '%s'",
1305 smp_to_type[pat->type]);
1306 return 0;
1307 }
1308
1309 /* If the flag PAT_F_IGNORE_CASE is set, we cannot use trees */
1310 if (expr->mflags & PAT_MF_IGNORE_CASE)
1311 return pat_idx_list_str(expr, pat, err);
1312
1313 /* Process the key len */
1314 len = strlen(pat->ptr.str);
1315
1316 /* node memory allocation */
1317 node = calloc(1, sizeof(*node) + len + 1);
1318 if (!node) {
1319 memprintf(err, "out of memory while loading pattern");
1320 return 0;
1321 }
1322
1323 /* copy the pointer to sample associated to this node */
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001324 node->data = pat->data;
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +02001325 node->ref = pat->ref;
1326
1327 /* copy the string and the trailing zero */
1328 memcpy(node->node.key, pat->ptr.str, len + 1);
1329 node->node.node.pfx = len * 8;
1330
1331 /* index the new node */
1332 ebmb_insert_prefix(&expr->pattern_tree, &node->node, len);
Willy Tarreau72f073b2015-04-29 17:53:47 +02001333 expr->revision = rdtsc();
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +02001334
1335 /* that's ok */
1336 return 1;
1337}
1338
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001339void pat_del_list_val(struct pattern_expr *expr, struct pat_ref_elt *ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001340{
1341 struct pattern_list *pat;
1342 struct pattern_list *safe;
1343
1344 list_for_each_entry_safe(pat, safe, &expr->patterns, list) {
1345 /* Check equality. */
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001346 if (pat->pat.ref != ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001347 continue;
1348
1349 /* Delete and free entry. */
1350 LIST_DEL(&pat->list);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001351 free(pat->pat.data);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001352 free(pat);
1353 }
Willy Tarreau72f073b2015-04-29 17:53:47 +02001354 expr->revision = rdtsc();
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001355}
1356
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001357void pat_del_tree_ip(struct pattern_expr *expr, struct pat_ref_elt *ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001358{
1359 struct ebmb_node *node, *next_node;
1360 struct pattern_tree *elt;
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001361
1362 /* browse each node of the tree for IPv4 addresses. */
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001363 for (node = ebmb_first(&expr->pattern_tree), next_node = node ? ebmb_next(node) : NULL;
1364 node;
1365 node = next_node, next_node = node ? ebmb_next(node) : NULL) {
1366 /* Extract container of the tree node. */
1367 elt = container_of(node, struct pattern_tree, node);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001368
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001369 /* Check equality. */
1370 if (elt->ref != ref)
1371 continue;
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001372
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001373 /* Delete and free entry. */
1374 ebmb_delete(node);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001375 free(elt->data);
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001376 free(elt);
1377 }
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001378
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001379 /* Browse each node of the list for IPv4 addresses. */
1380 pat_del_list_val(expr, ref);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001381
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001382 /* browse each node of the tree for IPv6 addresses. */
1383 for (node = ebmb_first(&expr->pattern_tree_2), next_node = node ? ebmb_next(node) : NULL;
1384 node;
1385 node = next_node, next_node = node ? ebmb_next(node) : NULL) {
1386 /* Extract container of the tree node. */
1387 elt = container_of(node, struct pattern_tree, node);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001388
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001389 /* Check equality. */
1390 if (elt->ref != ref)
1391 continue;
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001392
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001393 /* Delete and free entry. */
1394 ebmb_delete(node);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001395 free(elt->data);
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001396 free(elt);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001397 }
Willy Tarreau72f073b2015-04-29 17:53:47 +02001398 expr->revision = rdtsc();
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001399}
1400
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001401void pat_del_list_ptr(struct pattern_expr *expr, struct pat_ref_elt *ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001402{
1403 struct pattern_list *pat;
1404 struct pattern_list *safe;
1405
1406 list_for_each_entry_safe(pat, safe, &expr->patterns, list) {
1407 /* Check equality. */
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001408 if (pat->pat.ref != ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001409 continue;
1410
1411 /* Delete and free entry. */
1412 LIST_DEL(&pat->list);
1413 free(pat->pat.ptr.ptr);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001414 free(pat->pat.data);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001415 free(pat);
1416 }
Willy Tarreau72f073b2015-04-29 17:53:47 +02001417 expr->revision = rdtsc();
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001418}
1419
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001420void pat_del_tree_str(struct pattern_expr *expr, struct pat_ref_elt *ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001421{
1422 struct ebmb_node *node, *next_node;
1423 struct pattern_tree *elt;
1424
Thierry FOURNIER73bc2852015-02-06 17:53:54 +01001425 /* If the flag PAT_F_IGNORE_CASE is set, we cannot use trees */
1426 if (expr->mflags & PAT_MF_IGNORE_CASE)
1427 return pat_del_list_ptr(expr, ref);
1428
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001429 /* browse each node of the tree. */
1430 for (node = ebmb_first(&expr->pattern_tree), next_node = node ? ebmb_next(node) : NULL;
1431 node;
1432 node = next_node, next_node = node ? ebmb_next(node) : NULL) {
1433 /* Extract container of the tree node. */
1434 elt = container_of(node, struct pattern_tree, node);
1435
1436 /* Check equality. */
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001437 if (elt->ref != ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001438 continue;
1439
1440 /* Delete and free entry. */
1441 ebmb_delete(node);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001442 free(elt->data);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001443 free(elt);
1444 }
Willy Tarreau72f073b2015-04-29 17:53:47 +02001445 expr->revision = rdtsc();
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001446}
1447
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001448void pat_del_list_reg(struct pattern_expr *expr, struct pat_ref_elt *ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001449{
1450 struct pattern_list *pat;
1451 struct pattern_list *safe;
1452
1453 list_for_each_entry_safe(pat, safe, &expr->patterns, list) {
1454 /* Check equality. */
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001455 if (pat->pat.ref != ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001456 continue;
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001457
1458 /* Delete and free entry. */
1459 LIST_DEL(&pat->list);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001460 regex_free(pat->pat.ptr.ptr);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001461 free(pat->pat.data);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001462 free(pat);
1463 }
Willy Tarreau72f073b2015-04-29 17:53:47 +02001464 expr->revision = rdtsc();
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001465}
1466
1467void pattern_init_expr(struct pattern_expr *expr)
1468{
1469 LIST_INIT(&expr->patterns);
Willy Tarreau72f073b2015-04-29 17:53:47 +02001470 expr->revision = 0;
Thierry FOURNIER31db4ae2014-01-30 00:27:15 +01001471 expr->pattern_tree = EB_ROOT;
1472 expr->pattern_tree_2 = EB_ROOT;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001473}
1474
1475void pattern_init_head(struct pattern_head *head)
1476{
1477 LIST_INIT(&head->head);
1478}
1479
1480/* The following functions are relative to the management of the reference
1481 * lists. These lists are used to store the original pattern and associated
1482 * value as string form.
1483 *
1484 * This is used with modifiable ACL and MAPS
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001485 *
1486 * The pattern reference are stored with two identifiers: the unique_id and
1487 * the reference.
1488 *
1489 * The reference identify a file. Each file with the same name point to the
1490 * same reference. We can register many times one file. If the file is modified,
1491 * all his dependencies are also modified. The reference can be used with map or
1492 * acl.
1493 *
1494 * The unique_id identify inline acl. The unique id is unique for each acl.
1495 * You cannot force the same id in the configuration file, because this repoort
1496 * an error.
1497 *
1498 * A particular case appears if the filename is a number. In this case, the
1499 * unique_id is set with the number represented by the filename and the
1500 * reference is also set. This method prevent double unique_id.
1501 *
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001502 */
1503
1504/* This function lookup for reference. If the reference is found, they return
1505 * pointer to the struct pat_ref, else return NULL.
1506 */
1507struct pat_ref *pat_ref_lookup(const char *reference)
1508{
1509 struct pat_ref *ref;
1510
1511 list_for_each_entry(ref, &pattern_reference, list)
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001512 if (ref->reference && strcmp(reference, ref->reference) == 0)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001513 return ref;
1514 return NULL;
1515}
1516
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001517/* This function lookup for unique id. If the reference is found, they return
1518 * pointer to the struct pat_ref, else return NULL.
1519 */
1520struct pat_ref *pat_ref_lookupid(int unique_id)
1521{
1522 struct pat_ref *ref;
1523
1524 list_for_each_entry(ref, &pattern_reference, list)
1525 if (ref->unique_id == unique_id)
1526 return ref;
1527 return NULL;
1528}
1529
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001530/* This function remove all pattern matching the pointer <refelt> from
1531 * the the reference and from each expr member of the reference. This
1532 * function returns 1 if the deletion is done and return 0 is the entry
1533 * is not found.
1534 */
1535int pat_ref_delete_by_id(struct pat_ref *ref, struct pat_ref_elt *refelt)
1536{
1537 struct pattern_expr *expr;
1538 struct pat_ref_elt *elt, *safe;
1539
1540 /* delete pattern from reference */
1541 list_for_each_entry_safe(elt, safe, &ref->head, list) {
1542 if (elt == refelt) {
1543 LIST_DEL(&elt->list);
1544 free(elt->sample);
1545 free(elt->pattern);
1546 free(elt);
1547
1548 list_for_each_entry(expr, &ref->pat, list)
1549 pattern_delete(expr, elt);
1550
1551 return 1;
1552 }
1553 }
1554 return 0;
1555}
1556
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001557/* This function remove all pattern match <key> from the the reference
1558 * and from each expr member of the reference. This fucntion returns 1
1559 * if the deletion is done and return 0 is the entry is not found.
1560 */
1561int pat_ref_delete(struct pat_ref *ref, const char *key)
1562{
1563 struct pattern_expr *expr;
1564 struct pat_ref_elt *elt, *safe;
1565 int found = 0;
1566
1567 /* delete pattern from reference */
1568 list_for_each_entry_safe(elt, safe, &ref->head, list) {
1569 if (strcmp(key, elt->pattern) == 0) {
Dirkjan Bussink07fcaaa2014-04-28 22:57:16 +00001570 list_for_each_entry(expr, &ref->pat, list)
1571 pattern_delete(expr, elt);
1572
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001573 LIST_DEL(&elt->list);
1574 free(elt->sample);
1575 free(elt->pattern);
1576 free(elt);
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001577
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001578 found = 1;
1579 }
1580 }
1581
1582 if (!found)
1583 return 0;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001584 return 1;
1585}
1586
Baptiste Assmann953f74d2014-04-25 16:57:03 +02001587/*
1588 * find and return an element <elt> matching <key> in a reference <ref>
1589 * return NULL if not found
1590 */
1591struct pat_ref_elt *pat_ref_find_elt(struct pat_ref *ref, const char *key)
1592{
1593 struct pat_ref_elt *elt;
1594
1595 list_for_each_entry(elt, &ref->head, list) {
1596 if (strcmp(key, elt->pattern) == 0)
1597 return elt;
1598 }
1599
1600 return NULL;
1601}
1602
1603
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001604 /* This function modify the sample of the first pattern that match the <key>. */
1605static inline int pat_ref_set_elt(struct pat_ref *ref, struct pat_ref_elt *elt,
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001606 const char *value, char **err)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001607{
1608 struct pattern_expr *expr;
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001609 struct sample_data **data;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001610 char *sample;
Thierry FOURNIER12ba0c22015-08-14 00:02:11 +02001611 struct sample_data test;
Thierry FOURNIER149e0fe2014-01-29 19:35:06 +01001612
1613 /* Try all needed converters. */
1614 list_for_each_entry(expr, &ref->pat, list) {
1615 if (!expr->pat_head->parse_smp)
1616 continue;
1617
1618 if (!expr->pat_head->parse_smp(value, &test)) {
1619 memprintf(err, "unable to parse '%s'", value);
1620 return 0;
1621 }
1622 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001623
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001624 /* Modify pattern from reference. */
1625 sample = strdup(value);
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001626 if (!sample) {
1627 memprintf(err, "out of memory error");
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001628 return 0;
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001629 }
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001630 free(elt->sample);
1631 elt->sample = sample;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001632
Thierry FOURNIER149e0fe2014-01-29 19:35:06 +01001633 /* Load sample in each reference. All the conversion are tested
1634 * below, normally these calls dosn't fail.
1635 */
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001636 list_for_each_entry(expr, &ref->pat, list) {
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001637 if (!expr->pat_head->parse_smp)
1638 continue;
1639
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001640 data = pattern_find_smp(expr, elt);
1641 if (data && *data && !expr->pat_head->parse_smp(sample, *data))
1642 *data = NULL;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001643 }
1644
Thierry FOURNIER149e0fe2014-01-29 19:35:06 +01001645 return 1;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001646}
1647
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001648/* This function modify the sample of the first pattern that match the <key>. */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001649int 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 +01001650{
1651 struct pat_ref_elt *elt;
1652
1653 /* Look for pattern in the reference. */
1654 list_for_each_entry(elt, &ref->head, list) {
1655 if (elt == refelt) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001656 if (!pat_ref_set_elt(ref, elt, value, err))
1657 return 0;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001658 return 1;
1659 }
1660 }
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001661
1662 memprintf(err, "key or pattern not found");
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001663 return 0;
1664}
1665
1666/* This function modify the sample of the first pattern that match the <key>. */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001667int pat_ref_set(struct pat_ref *ref, const char *key, const char *value, char **err)
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001668{
1669 struct pat_ref_elt *elt;
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001670 int found = 0;
1671 char *_merr;
1672 char **merr;
1673
1674 if (err) {
1675 merr = &_merr;
1676 *merr = NULL;
1677 }
1678 else
1679 merr = NULL;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001680
1681 /* Look for pattern in the reference. */
1682 list_for_each_entry(elt, &ref->head, list) {
1683 if (strcmp(key, elt->pattern) == 0) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001684 if (!pat_ref_set_elt(ref, elt, value, merr)) {
1685 if (!found)
1686 *err = *merr;
1687 else {
1688 memprintf(err, "%s, %s", *err, *merr);
1689 free(*merr);
1690 *merr = NULL;
1691 }
1692 }
1693 found = 1;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001694 }
1695 }
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001696
1697 if (!found) {
1698 memprintf(err, "entry not found");
1699 return 0;
1700 }
1701 return 1;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001702}
1703
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001704/* This function create new reference. <ref> is the reference name.
1705 * <flags> are PAT_REF_*. /!\ The reference is not checked, and must
1706 * be unique. The user must check the reference with "pat_ref_lookup()"
1707 * before calling this function. If the fucntion fail, it return NULL,
1708 * else return new struct pat_ref.
1709 */
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001710struct pat_ref *pat_ref_new(const char *reference, const char *display, unsigned int flags)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001711{
1712 struct pat_ref *ref;
1713
1714 ref = malloc(sizeof(*ref));
1715 if (!ref)
1716 return NULL;
1717
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001718 if (display) {
1719 ref->display = strdup(display);
1720 if (!ref->display) {
1721 free(ref);
1722 return NULL;
1723 }
1724 }
1725 else
1726 ref->display = NULL;
1727
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001728 ref->reference = strdup(reference);
1729 if (!ref->reference) {
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001730 free(ref->display);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001731 free(ref);
1732 return NULL;
1733 }
1734
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001735 ref->flags = flags;
1736 ref->unique_id = -1;
1737
1738 LIST_INIT(&ref->head);
1739 LIST_INIT(&ref->pat);
1740
1741 LIST_ADDQ(&pattern_reference, &ref->list);
1742
1743 return ref;
1744}
1745
1746/* This function create new reference. <unique_id> is the unique id. If
1747 * the value of <unique_id> is -1, the unique id is calculated later.
1748 * <flags> are PAT_REF_*. /!\ The reference is not checked, and must
1749 * be unique. The user must check the reference with "pat_ref_lookup()"
1750 * or pat_ref_lookupid before calling this function. If the function
1751 * fail, it return NULL, else return new struct pat_ref.
1752 */
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001753struct pat_ref *pat_ref_newid(int unique_id, const char *display, unsigned int flags)
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001754{
1755 struct pat_ref *ref;
1756
1757 ref = malloc(sizeof(*ref));
1758 if (!ref)
1759 return NULL;
1760
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001761 if (display) {
1762 ref->display = strdup(display);
1763 if (!ref->display) {
1764 free(ref);
1765 return NULL;
1766 }
1767 }
1768 else
1769 ref->display = NULL;
1770
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001771 ref->reference = NULL;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001772 ref->flags = flags;
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001773 ref->unique_id = unique_id;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001774 LIST_INIT(&ref->head);
1775 LIST_INIT(&ref->pat);
1776
1777 LIST_ADDQ(&pattern_reference, &ref->list);
1778
1779 return ref;
1780}
1781
1782/* This function adds entry to <ref>. It can failed with memory error.
1783 * If the function fails, it returns 0.
1784 */
1785int pat_ref_append(struct pat_ref *ref, char *pattern, char *sample, int line)
1786{
1787 struct pat_ref_elt *elt;
1788
1789 elt = malloc(sizeof(*elt));
1790 if (!elt)
1791 return 0;
1792
1793 elt->line = line;
1794
1795 elt->pattern = strdup(pattern);
1796 if (!elt->pattern) {
1797 free(elt);
1798 return 0;
1799 }
1800
1801 if (sample) {
1802 elt->sample = strdup(sample);
1803 if (!elt->sample) {
1804 free(elt->pattern);
1805 free(elt);
1806 return 0;
1807 }
1808 }
1809 else
1810 elt->sample = NULL;
1811
1812 LIST_ADDQ(&ref->head, &elt->list);
1813
1814 return 1;
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001815}
1816
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001817/* This function create sample found in <elt>, parse the pattern also
1818 * found in <elt> and insert it in <expr>. The function copy <patflags>
1819 * in <expr>. If the function fails, it returns0 and <err> is filled.
1820 * In succes case, the function returns 1.
1821 */
1822static inline
1823int pat_ref_push(struct pat_ref_elt *elt, struct pattern_expr *expr,
1824 int patflags, char **err)
1825{
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001826 struct sample_data *data;
Thierry FOURNIERd25c8422014-01-28 15:34:35 +01001827 struct pattern pattern;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001828
1829 /* Create sample */
1830 if (elt->sample && expr->pat_head->parse_smp) {
1831 /* New sample. */
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001832 data = malloc(sizeof(*data));
1833 if (!data)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001834 return 0;
1835
1836 /* Parse value. */
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001837 if (!expr->pat_head->parse_smp(elt->sample, data)) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001838 memprintf(err, "unable to parse '%s'", elt->sample);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001839 free(data);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001840 return 0;
1841 }
1842
1843 }
1844 else
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001845 data = NULL;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001846
Thierry FOURNIERd25c8422014-01-28 15:34:35 +01001847 /* initialise pattern */
1848 memset(&pattern, 0, sizeof(pattern));
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001849 pattern.data = data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01001850 pattern.ref = elt;
Thierry FOURNIERd25c8422014-01-28 15:34:35 +01001851
1852 /* parse pattern */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02001853 if (!expr->pat_head->parse(elt->pattern, &pattern, expr->mflags, err)) {
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001854 free(data);
Thierry FOURNIERd25c8422014-01-28 15:34:35 +01001855 return 0;
1856 }
1857
1858 /* index pattern */
1859 if (!expr->pat_head->index(expr, &pattern, err)) {
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001860 free(data);
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001861 return 0;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001862 }
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001863
1864 return 1;
1865}
1866
Thierry FOURNIER31db4ae2014-01-30 00:27:15 +01001867/* This function adds entry to <ref>. It can failed with memory error. The new
1868 * entry is added at all the pattern_expr registered in this reference. The
1869 * function stop on the first error encountered. It returns 0 and err is
1870 * filled. If an error is encountered, the complete add operation is cancelled.
1871 * If the insertion is a success the function returns 1.
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001872 */
1873int pat_ref_add(struct pat_ref *ref,
1874 const char *pattern, const char *sample,
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02001875 char **err)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001876{
1877 struct pat_ref_elt *elt;
1878 struct pattern_expr *expr;
1879
1880 elt = malloc(sizeof(*elt));
1881 if (!elt) {
1882 memprintf(err, "out of memory error");
1883 return 0;
1884 }
1885
1886 elt->line = -1;
1887
1888 elt->pattern = strdup(pattern);
1889 if (!elt->pattern) {
1890 free(elt);
1891 memprintf(err, "out of memory error");
1892 return 0;
1893 }
1894
1895 if (sample) {
1896 elt->sample = strdup(sample);
1897 if (!elt->sample) {
1898 free(elt->pattern);
1899 free(elt);
1900 memprintf(err, "out of memory error");
1901 return 0;
1902 }
1903 }
1904 else
1905 elt->sample = NULL;
1906
1907 LIST_ADDQ(&ref->head, &elt->list);
1908
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001909 list_for_each_entry(expr, &ref->pat, list) {
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02001910 if (!pat_ref_push(elt, expr, 0, err)) {
Thierry FOURNIER31db4ae2014-01-30 00:27:15 +01001911 /* If the insertion fails, try to delete all the added entries. */
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001912 pat_ref_delete_by_id(ref, elt);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001913 return 0;
1914 }
1915 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001916 return 1;
1917}
1918
Thierry FOURNIER46006bd2014-03-21 21:45:15 +01001919/* This function prune <ref>, replace all reference by the references
1920 * of <replace>, and reindex all the news values.
1921 *
1922 * The pattern are loaded in best effort and the errors are ignored,
1923 * but writed in the logs.
1924 */
1925void pat_ref_reload(struct pat_ref *ref, struct pat_ref *replace)
1926{
1927 struct pattern_expr *expr;
1928 struct pat_ref_elt *elt;
1929 char *err = NULL;
1930
1931 pat_ref_prune(ref);
1932
1933 LIST_ADD(&replace->head, &ref->head);
1934 LIST_DEL(&replace->head);
1935
1936 list_for_each_entry(elt, &ref->head, list) {
1937 list_for_each_entry(expr, &ref->pat, list) {
1938 if (!pat_ref_push(elt, expr, 0, &err)) {
1939 send_log(NULL, LOG_NOTICE, "%s", err);
1940 free(err);
1941 err = NULL;
1942 }
1943 }
1944 }
1945}
1946
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001947/* This function prune all entries of <ref>. This function
1948 * prune the associated pattern_expr.
1949 */
1950void pat_ref_prune(struct pat_ref *ref)
1951{
1952 struct pat_ref_elt *elt, *safe;
1953 struct pattern_expr *expr;
1954
1955 list_for_each_entry_safe(elt, safe, &ref->head, list) {
1956 LIST_DEL(&elt->list);
1957 free(elt->pattern);
1958 free(elt->sample);
1959 free(elt);
1960 }
1961
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001962 list_for_each_entry(expr, &ref->pat, list)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001963 expr->pat_head->prune(expr);
1964}
1965
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001966/* This function lookup for existing reference <ref> in pattern_head <head>. */
1967struct pattern_expr *pattern_lookup_expr(struct pattern_head *head, struct pat_ref *ref)
1968{
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001969 struct pattern_expr_list *expr;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001970
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001971 list_for_each_entry(expr, &head->head, list)
1972 if (expr->expr->ref == ref)
1973 return expr->expr;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001974 return NULL;
1975}
1976
1977/* This function create new pattern_expr associated to the reference <ref>.
1978 * <ref> can be NULL. If an error is occured, the function returns NULL and
1979 * <err> is filled. Otherwise, the function returns new pattern_expr linked
1980 * with <head> and <ref>.
Thierry FOURNIER315ec422014-11-24 11:14:42 +01001981 *
1982 * The returned value can be a alredy filled pattern list, in this case the
1983 * flag <reuse> is set.
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001984 */
Thierry FOURNIER315ec422014-11-24 11:14:42 +01001985struct pattern_expr *pattern_new_expr(struct pattern_head *head, struct pat_ref *ref,
1986 char **err, int *reuse)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001987{
1988 struct pattern_expr *expr;
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001989 struct pattern_expr_list *list;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001990
Thierry FOURNIER315ec422014-11-24 11:14:42 +01001991 if (reuse)
1992 *reuse = 0;
1993
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001994 /* Memory and initialization of the chain element. */
1995 list = malloc(sizeof(*list));
1996 if (!list) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001997 memprintf(err, "out of memory");
1998 return NULL;
1999 }
2000
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002001 /* Look for existing similar expr. No that only the index, parse and
2002 * parse_smp function must be identical for having similar pattern.
2003 * The other function depends of theses first.
2004 */
2005 if (ref) {
2006 list_for_each_entry(expr, &ref->pat, list)
2007 if (expr->pat_head->index == head->index &&
2008 expr->pat_head->parse == head->parse &&
2009 expr->pat_head->parse_smp == head->parse_smp)
2010 break;
2011 if (&expr->list == &ref->pat)
2012 expr = NULL;
2013 }
2014 else
2015 expr = NULL;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002016
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002017 /* If no similar expr was found, we create new expr. */
2018 if (!expr) {
2019 /* Get a lot of memory for the expr struct. */
2020 expr = malloc(sizeof(*expr));
2021 if (!expr) {
2022 memprintf(err, "out of memory");
2023 return NULL;
2024 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002025
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002026 /* Initialize this new expr. */
2027 pattern_init_expr(expr);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002028
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002029 /* This new pattern expression reference one of his heads. */
2030 expr->pat_head = head;
2031
2032 /* Link with ref, or to self to facilitate LIST_DEL() */
2033 if (ref)
2034 LIST_ADDQ(&ref->pat, &expr->list);
2035 else
2036 LIST_INIT(&expr->list);
2037
2038 expr->ref = ref;
2039
2040 /* We must free this pattern if it is no more used. */
2041 list->do_free = 1;
2042 }
2043 else {
2044 /* If the pattern used already exists, it is already linked
2045 * with ref and we must not free it.
2046 */
2047 list->do_free = 0;
Thierry FOURNIER315ec422014-11-24 11:14:42 +01002048 if (reuse)
2049 *reuse = 1;
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002050 }
2051
2052 /* The new list element reference the pattern_expr. */
2053 list->expr = expr;
2054
2055 /* Link the list element with the pattern_head. */
2056 LIST_ADDQ(&head->head, &list->list);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002057 return expr;
2058}
2059
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002060/* Reads patterns from a file. If <err_msg> is non-NULL, an error message will
2061 * be returned there on errors and the caller will have to free it.
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002062 *
2063 * The file contains one key + value per line. Lines which start with '#' are
2064 * ignored, just like empty lines. Leading tabs/spaces are stripped. The key is
2065 * then the first "word" (series of non-space/tabs characters), and the value is
2066 * what follows this series of space/tab till the end of the line excluding
2067 * trailing spaces/tabs.
2068 *
2069 * Example :
2070 *
2071 * # this is a comment and is ignored
2072 * 62.212.114.60 1wt.eu \n
2073 * <-><-----------><---><----><---->
2074 * | | | | `--- trailing spaces ignored
2075 * | | | `-------- value
2076 * | | `--------------- middle spaces ignored
2077 * | `------------------------ key
2078 * `-------------------------------- leading spaces ignored
2079 *
2080 * Return non-zero in case of succes, otherwise 0.
2081 */
2082int pat_ref_read_from_file_smp(struct pat_ref *ref, const char *filename, char **err)
2083{
2084 FILE *file;
2085 char *c;
2086 int ret = 0;
2087 int line = 0;
2088 char *key_beg;
2089 char *key_end;
2090 char *value_beg;
2091 char *value_end;
2092
2093 file = fopen(filename, "r");
2094 if (!file) {
2095 memprintf(err, "failed to open pattern file <%s>", filename);
2096 return 0;
2097 }
2098
2099 /* now parse all patterns. The file may contain only one pattern
2100 * followed by one value per line. The start spaces, separator spaces
2101 * and and spaces are stripped. Each can contain comment started by '#'
2102 */
2103 while (fgets(trash.str, trash.size, file) != NULL) {
2104 line++;
2105 c = trash.str;
2106
2107 /* ignore lines beginning with a dash */
2108 if (*c == '#')
2109 continue;
2110
2111 /* strip leading spaces and tabs */
2112 while (*c == ' ' || *c == '\t')
2113 c++;
2114
2115 /* empty lines are ignored too */
2116 if (*c == '\0' || *c == '\r' || *c == '\n')
2117 continue;
2118
2119 /* look for the end of the key */
2120 key_beg = c;
2121 while (*c && *c != ' ' && *c != '\t' && *c != '\n' && *c != '\r')
2122 c++;
2123
2124 key_end = c;
2125
2126 /* strip middle spaces and tabs */
2127 while (*c == ' ' || *c == '\t')
2128 c++;
2129
2130 /* look for the end of the value, it is the end of the line */
2131 value_beg = c;
2132 while (*c && *c != '\n' && *c != '\r')
2133 c++;
2134 value_end = c;
2135
2136 /* trim possibly trailing spaces and tabs */
2137 while (value_end > value_beg && (value_end[-1] == ' ' || value_end[-1] == '\t'))
2138 value_end--;
2139
2140 /* set final \0 and check entries */
2141 *key_end = '\0';
2142 *value_end = '\0';
2143
2144 /* insert values */
2145 if (!pat_ref_append(ref, key_beg, value_beg, line)) {
2146 memprintf(err, "out of memory");
2147 goto out_close;
2148 }
2149 }
2150
2151 /* succes */
2152 ret = 1;
2153
2154 out_close:
2155 fclose(file);
2156 return ret;
2157}
2158
2159/* Reads patterns from a file. If <err_msg> is non-NULL, an error message will
2160 * be returned there on errors and the caller will have to free it.
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002161 */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002162int pat_ref_read_from_file(struct pat_ref *ref, const char *filename, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002163{
2164 FILE *file;
2165 char *c;
2166 char *arg;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002167 int ret = 0;
2168 int line = 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002169
2170 file = fopen(filename, "r");
2171 if (!file) {
2172 memprintf(err, "failed to open pattern file <%s>", filename);
2173 return 0;
2174 }
2175
2176 /* now parse all patterns. The file may contain only one pattern per
2177 * line. If the line contains spaces, they will be part of the pattern.
2178 * The pattern stops at the first CR, LF or EOF encountered.
2179 */
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002180 while (fgets(trash.str, trash.size, file) != NULL) {
2181 line++;
2182 c = trash.str;
2183
2184 /* ignore lines beginning with a dash */
2185 if (*c == '#')
2186 continue;
2187
2188 /* strip leading spaces and tabs */
2189 while (*c == ' ' || *c == '\t')
2190 c++;
2191
2192
2193 arg = c;
2194 while (*c && *c != '\n' && *c != '\r')
2195 c++;
2196 *c = 0;
2197
2198 /* empty lines are ignored too */
2199 if (c == arg)
2200 continue;
2201
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002202 if (!pat_ref_append(ref, arg, NULL, line)) {
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002203 memprintf(err, "out of memory when loading patterns from file <%s>", filename);
2204 goto out_close;
2205 }
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002206 }
2207
2208 ret = 1; /* success */
2209
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002210 out_close:
2211 fclose(file);
2212 return ret;
2213}
2214
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002215int pattern_read_from_file(struct pattern_head *head, unsigned int refflags,
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002216 const char *filename, int patflags, int load_smp,
Thierry FOURNIER94580c92014-02-11 14:36:45 +01002217 char **err, const char *file, int line)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002218{
2219 struct pat_ref *ref;
2220 struct pattern_expr *expr;
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002221 struct pat_ref_elt *elt;
Willy Tarreau4deaf392014-11-26 13:17:03 +01002222 int reuse = 0;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002223
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002224 /* Lookup for the existing reference. */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002225 ref = pat_ref_lookup(filename);
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002226
2227 /* If the reference doesn't exists, create it and load associated file. */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002228 if (!ref) {
Thierry FOURNIER94580c92014-02-11 14:36:45 +01002229 chunk_printf(&trash,
2230 "pattern loaded from file '%s' used by %s at file '%s' line %d",
2231 filename, refflags & PAT_REF_MAP ? "map" : "acl", file, line);
2232
2233 ref = pat_ref_new(filename, trash.str, refflags);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002234 if (!ref) {
2235 memprintf(err, "out of memory");
2236 return 0;
2237 }
2238
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002239 if (load_smp) {
Thierry FOURNIERc0bd9102014-01-29 12:32:58 +01002240 ref->flags |= PAT_REF_SMP;
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002241 if (!pat_ref_read_from_file_smp(ref, filename, err))
2242 return 0;
2243 }
2244 else {
2245 if (!pat_ref_read_from_file(ref, filename, err))
2246 return 0;
2247 }
2248 }
2249 else {
Thierry FOURNIERc0bd9102014-01-29 12:32:58 +01002250 /* The reference already exists, check the map compatibility. */
2251
2252 /* If the load require samples and the flag PAT_REF_SMP is not set,
2253 * the reference doesn't contain sample, and cannot be used.
2254 */
2255 if (load_smp) {
2256 if (!(ref->flags & PAT_REF_SMP)) {
2257 memprintf(err, "The file \"%s\" is already used as one column file "
2258 "and cannot be used by as two column file.",
2259 filename);
2260 return 0;
2261 }
2262 }
2263 else {
2264 /* The load doesn't require samples. If the flag PAT_REF_SMP is
2265 * set, the reference contains a sample, and cannot be used.
2266 */
2267 if (ref->flags & PAT_REF_SMP) {
2268 memprintf(err, "The file \"%s\" is already used as two column file "
2269 "and cannot be used by as one column file.",
2270 filename);
2271 return 0;
2272 }
2273 }
2274
Thierry FOURNIER94580c92014-02-11 14:36:45 +01002275 /* Extends display */
2276 chunk_printf(&trash, "%s", ref->display);
2277 chunk_appendf(&trash, ", by %s at file '%s' line %d",
2278 refflags & PAT_REF_MAP ? "map" : "acl", file, line);
2279 free(ref->display);
2280 ref->display = strdup(trash.str);
2281 if (!ref->display) {
2282 memprintf(err, "out of memory");
2283 return 0;
2284 }
2285
Thierry FOURNIERc0bd9102014-01-29 12:32:58 +01002286 /* Merge flags. */
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002287 ref->flags |= refflags;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002288 }
2289
2290 /* Now, we can loading patterns from the reference. */
2291
2292 /* Lookup for existing reference in the head. If the reference
2293 * doesn't exists, create it.
2294 */
2295 expr = pattern_lookup_expr(head, ref);
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02002296 if (!expr || (expr->mflags != patflags)) {
Thierry FOURNIER315ec422014-11-24 11:14:42 +01002297 expr = pattern_new_expr(head, ref, err, &reuse);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002298 if (!expr)
2299 return 0;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02002300 expr->mflags = patflags;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002301 }
2302
Thierry FOURNIER315ec422014-11-24 11:14:42 +01002303 /* The returned expression may be not empty, because the function
2304 * "pattern_new_expr" lookup for similar pattern list and can
2305 * reuse a already filled pattern list. In this case, we can not
2306 * reload the patterns.
2307 */
2308 if (reuse)
2309 return 1;
2310
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002311 /* Load reference content in the pattern expression. */
2312 list_for_each_entry(elt, &ref->head, list) {
2313 if (!pat_ref_push(elt, expr, patflags, err)) {
2314 if (elt->line > 0)
2315 memprintf(err, "%s at line %d of file '%s'",
2316 *err, elt->line, filename);
2317 return 0;
2318 }
2319 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002320
2321 return 1;
2322}
2323
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01002324/* This function executes a pattern match on a sample. It applies pattern <expr>
2325 * to sample <smp>. The function returns NULL if the sample dont match. It returns
2326 * non-null if the sample match. If <fill> is true and the sample match, the
2327 * function returns the matched pattern. In many cases, this pattern can be a
2328 * static buffer.
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002329 */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002330struct pattern *pattern_exec_match(struct pattern_head *head, struct sample *smp, int fill)
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002331{
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002332 struct pattern_expr_list *list;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002333 struct pattern *pat;
2334
2335 if (!head->match) {
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01002336 if (fill) {
Thierry FOURNIER503bb092015-08-19 08:35:43 +02002337 static_pattern.data = NULL;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01002338 static_pattern.ref = NULL;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02002339 static_pattern.sflags = 0;
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002340 static_pattern.type = SMP_T_SINT;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01002341 static_pattern.val.i = 1;
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01002342 }
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01002343 return &static_pattern;
2344 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002345
Thierry FOURNIER5d344082014-01-27 14:19:53 +01002346 /* convert input to string */
2347 if (!sample_convert(smp, head->expect_type))
2348 return NULL;
2349
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002350 list_for_each_entry(list, &head->head, list) {
2351 pat = head->match(smp, list->expr, fill);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002352 if (pat)
2353 return pat;
2354 }
2355 return NULL;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002356}
2357
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01002358/* This function prune the pattern expression. */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002359void pattern_prune(struct pattern_head *head)
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01002360{
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002361 struct pattern_expr_list *list, *safe;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002362
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002363 list_for_each_entry_safe(list, safe, &head->head, list) {
2364 LIST_DEL(&list->list);
2365 if (list->do_free) {
2366 LIST_DEL(&list->expr->list);
2367 head->prune(list->expr);
2368 free(list->expr);
2369 }
2370 free(list);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002371 }
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01002372}
2373
Thierry FOURNIER55d0b102014-01-15 11:25:26 +01002374/* This function lookup for a pattern matching the <key> and return a
2375 * pointer to a pointer of the sample stoarge. If the <key> dont match,
2376 * the function returns NULL. If the key cannot be parsed, the function
2377 * fill <err>.
2378 */
Thierry FOURNIER12ba0c22015-08-14 00:02:11 +02002379struct sample_data **pattern_find_smp(struct pattern_expr *expr, struct pat_ref_elt *ref)
Thierry FOURNIER55d0b102014-01-15 11:25:26 +01002380{
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01002381 struct ebmb_node *node;
2382 struct pattern_tree *elt;
2383 struct pattern_list *pat;
Thierry FOURNIER55d0b102014-01-15 11:25:26 +01002384
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01002385 for (node = ebmb_first(&expr->pattern_tree);
2386 node;
2387 node = ebmb_next(node)) {
2388 elt = container_of(node, struct pattern_tree, node);
2389 if (elt->ref == ref)
Thierry FOURNIER503bb092015-08-19 08:35:43 +02002390 return &elt->data;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01002391 }
2392
2393 for (node = ebmb_first(&expr->pattern_tree_2);
2394 node;
2395 node = ebmb_next(node)) {
2396 elt = container_of(node, struct pattern_tree, node);
2397 if (elt->ref == ref)
Thierry FOURNIER503bb092015-08-19 08:35:43 +02002398 return &elt->data;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01002399 }
2400
2401 list_for_each_entry(pat, &expr->patterns, list)
2402 if (pat->pat.ref == ref)
Thierry FOURNIER503bb092015-08-19 08:35:43 +02002403 return &pat->pat.data;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01002404
2405 return NULL;
Thierry FOURNIER55d0b102014-01-15 11:25:26 +01002406}
2407
Thierry FOURNIERb1136502014-01-15 11:38:49 +01002408/* This function search all the pattern matching the <key> and delete it.
2409 * If the parsing of the input key fails, the function returns 0 and the
2410 * <err> is filled, else return 1;
2411 */
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01002412int pattern_delete(struct pattern_expr *expr, struct pat_ref_elt *ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01002413{
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01002414 expr->pat_head->delete(expr, ref);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01002415 return 1;
2416}
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002417
2418/* This function finalize the configuration parsing. Its set all the
2419 * automatic ids
2420 */
2421void pattern_finalize_config(void)
2422{
2423 int i = 0;
2424 struct pat_ref *ref, *ref2, *ref3;
2425 struct list pr = LIST_HEAD_INIT(pr);
2426
Willy Tarreauf3045d22015-04-29 16:24:50 +02002427 pat_lru_seed = random();
2428 if (global.tune.pattern_cache)
2429 pat_lru_tree = lru64_new(global.tune.pattern_cache);
2430
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002431 list_for_each_entry(ref, &pattern_reference, list) {
2432 if (ref->unique_id == -1) {
2433 /* Look for the first free id. */
2434 while (1) {
2435 list_for_each_entry(ref2, &pattern_reference, list) {
2436 if (ref2->unique_id == i) {
2437 i++;
2438 break;
2439 }
2440 }
Willy Tarreau3b786962014-04-26 12:37:25 +02002441 if (&ref2->list == &pattern_reference)
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002442 break;
2443 }
2444
2445 /* Uses the unique id and increment it for the next entry. */
2446 ref->unique_id = i;
2447 i++;
2448 }
2449 }
2450
2451 /* This sort the reference list by id. */
2452 list_for_each_entry_safe(ref, ref2, &pattern_reference, list) {
2453 LIST_DEL(&ref->list);
2454 list_for_each_entry(ref3, &pr, list) {
2455 if (ref->unique_id < ref3->unique_id) {
2456 LIST_ADDQ(&ref3->list, &ref->list);
2457 break;
2458 }
2459 }
2460 if (&ref3->list == &pr)
2461 LIST_ADDQ(&pr, &ref->list);
2462 }
2463
2464 /* swap root */
2465 LIST_ADD(&pr, &pattern_reference);
2466 LIST_DEL(&pr);
2467}