blob: 254c10650ed2967c76611357b86a736f4a1ec977 [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) {
peter caiaede6dd2015-10-07 00:07:43 -07001543 list_for_each_entry(expr, &ref->pat, list)
1544 pattern_delete(expr, elt);
1545
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001546 LIST_DEL(&elt->list);
1547 free(elt->sample);
1548 free(elt->pattern);
1549 free(elt);
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001550 return 1;
1551 }
1552 }
1553 return 0;
1554}
1555
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001556/* This function remove all pattern match <key> from the the reference
1557 * and from each expr member of the reference. This fucntion returns 1
1558 * if the deletion is done and return 0 is the entry is not found.
1559 */
1560int pat_ref_delete(struct pat_ref *ref, const char *key)
1561{
1562 struct pattern_expr *expr;
1563 struct pat_ref_elt *elt, *safe;
1564 int found = 0;
1565
1566 /* delete pattern from reference */
1567 list_for_each_entry_safe(elt, safe, &ref->head, list) {
1568 if (strcmp(key, elt->pattern) == 0) {
Dirkjan Bussink07fcaaa2014-04-28 22:57:16 +00001569 list_for_each_entry(expr, &ref->pat, list)
1570 pattern_delete(expr, elt);
1571
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001572 LIST_DEL(&elt->list);
1573 free(elt->sample);
1574 free(elt->pattern);
1575 free(elt);
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001576
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001577 found = 1;
1578 }
1579 }
1580
1581 if (!found)
1582 return 0;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001583 return 1;
1584}
1585
Baptiste Assmann953f74d2014-04-25 16:57:03 +02001586/*
1587 * find and return an element <elt> matching <key> in a reference <ref>
1588 * return NULL if not found
1589 */
1590struct pat_ref_elt *pat_ref_find_elt(struct pat_ref *ref, const char *key)
1591{
1592 struct pat_ref_elt *elt;
1593
1594 list_for_each_entry(elt, &ref->head, list) {
1595 if (strcmp(key, elt->pattern) == 0)
1596 return elt;
1597 }
1598
1599 return NULL;
1600}
1601
1602
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001603 /* This function modify the sample of the first pattern that match the <key>. */
1604static inline int pat_ref_set_elt(struct pat_ref *ref, struct pat_ref_elt *elt,
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001605 const char *value, char **err)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001606{
1607 struct pattern_expr *expr;
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001608 struct sample_data **data;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001609 char *sample;
Thierry FOURNIER12ba0c22015-08-14 00:02:11 +02001610 struct sample_data test;
Thierry FOURNIER149e0fe2014-01-29 19:35:06 +01001611
1612 /* Try all needed converters. */
1613 list_for_each_entry(expr, &ref->pat, list) {
1614 if (!expr->pat_head->parse_smp)
1615 continue;
1616
1617 if (!expr->pat_head->parse_smp(value, &test)) {
1618 memprintf(err, "unable to parse '%s'", value);
1619 return 0;
1620 }
1621 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001622
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001623 /* Modify pattern from reference. */
1624 sample = strdup(value);
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001625 if (!sample) {
1626 memprintf(err, "out of memory error");
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001627 return 0;
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001628 }
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001629 free(elt->sample);
1630 elt->sample = sample;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001631
Thierry FOURNIER149e0fe2014-01-29 19:35:06 +01001632 /* Load sample in each reference. All the conversion are tested
1633 * below, normally these calls dosn't fail.
1634 */
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001635 list_for_each_entry(expr, &ref->pat, list) {
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001636 if (!expr->pat_head->parse_smp)
1637 continue;
1638
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001639 data = pattern_find_smp(expr, elt);
1640 if (data && *data && !expr->pat_head->parse_smp(sample, *data))
1641 *data = NULL;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001642 }
1643
Thierry FOURNIER149e0fe2014-01-29 19:35:06 +01001644 return 1;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001645}
1646
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001647/* This function modify the sample of the first pattern that match the <key>. */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001648int 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 +01001649{
1650 struct pat_ref_elt *elt;
1651
1652 /* Look for pattern in the reference. */
1653 list_for_each_entry(elt, &ref->head, list) {
1654 if (elt == refelt) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001655 if (!pat_ref_set_elt(ref, elt, value, err))
1656 return 0;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001657 return 1;
1658 }
1659 }
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001660
1661 memprintf(err, "key or pattern not found");
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001662 return 0;
1663}
1664
1665/* This function modify the sample of the first pattern that match the <key>. */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001666int pat_ref_set(struct pat_ref *ref, const char *key, const char *value, char **err)
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001667{
1668 struct pat_ref_elt *elt;
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001669 int found = 0;
1670 char *_merr;
1671 char **merr;
1672
1673 if (err) {
1674 merr = &_merr;
1675 *merr = NULL;
1676 }
1677 else
1678 merr = NULL;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001679
1680 /* Look for pattern in the reference. */
1681 list_for_each_entry(elt, &ref->head, list) {
1682 if (strcmp(key, elt->pattern) == 0) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001683 if (!pat_ref_set_elt(ref, elt, value, merr)) {
1684 if (!found)
1685 *err = *merr;
1686 else {
1687 memprintf(err, "%s, %s", *err, *merr);
1688 free(*merr);
1689 *merr = NULL;
1690 }
1691 }
1692 found = 1;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001693 }
1694 }
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001695
1696 if (!found) {
1697 memprintf(err, "entry not found");
1698 return 0;
1699 }
1700 return 1;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001701}
1702
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001703/* This function create new reference. <ref> is the reference name.
1704 * <flags> are PAT_REF_*. /!\ The reference is not checked, and must
1705 * be unique. The user must check the reference with "pat_ref_lookup()"
1706 * before calling this function. If the fucntion fail, it return NULL,
1707 * else return new struct pat_ref.
1708 */
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001709struct pat_ref *pat_ref_new(const char *reference, const char *display, unsigned int flags)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001710{
1711 struct pat_ref *ref;
1712
1713 ref = malloc(sizeof(*ref));
1714 if (!ref)
1715 return NULL;
1716
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001717 if (display) {
1718 ref->display = strdup(display);
1719 if (!ref->display) {
1720 free(ref);
1721 return NULL;
1722 }
1723 }
1724 else
1725 ref->display = NULL;
1726
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001727 ref->reference = strdup(reference);
1728 if (!ref->reference) {
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001729 free(ref->display);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001730 free(ref);
1731 return NULL;
1732 }
1733
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001734 ref->flags = flags;
1735 ref->unique_id = -1;
1736
1737 LIST_INIT(&ref->head);
1738 LIST_INIT(&ref->pat);
1739
1740 LIST_ADDQ(&pattern_reference, &ref->list);
1741
1742 return ref;
1743}
1744
1745/* This function create new reference. <unique_id> is the unique id. If
1746 * the value of <unique_id> is -1, the unique id is calculated later.
1747 * <flags> are PAT_REF_*. /!\ The reference is not checked, and must
1748 * be unique. The user must check the reference with "pat_ref_lookup()"
1749 * or pat_ref_lookupid before calling this function. If the function
1750 * fail, it return NULL, else return new struct pat_ref.
1751 */
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001752struct pat_ref *pat_ref_newid(int unique_id, const char *display, unsigned int flags)
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001753{
1754 struct pat_ref *ref;
1755
1756 ref = malloc(sizeof(*ref));
1757 if (!ref)
1758 return NULL;
1759
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001760 if (display) {
1761 ref->display = strdup(display);
1762 if (!ref->display) {
1763 free(ref);
1764 return NULL;
1765 }
1766 }
1767 else
1768 ref->display = NULL;
1769
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001770 ref->reference = NULL;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001771 ref->flags = flags;
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001772 ref->unique_id = unique_id;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001773 LIST_INIT(&ref->head);
1774 LIST_INIT(&ref->pat);
1775
1776 LIST_ADDQ(&pattern_reference, &ref->list);
1777
1778 return ref;
1779}
1780
1781/* This function adds entry to <ref>. It can failed with memory error.
1782 * If the function fails, it returns 0.
1783 */
1784int pat_ref_append(struct pat_ref *ref, char *pattern, char *sample, int line)
1785{
1786 struct pat_ref_elt *elt;
1787
1788 elt = malloc(sizeof(*elt));
1789 if (!elt)
1790 return 0;
1791
1792 elt->line = line;
1793
1794 elt->pattern = strdup(pattern);
1795 if (!elt->pattern) {
1796 free(elt);
1797 return 0;
1798 }
1799
1800 if (sample) {
1801 elt->sample = strdup(sample);
1802 if (!elt->sample) {
1803 free(elt->pattern);
1804 free(elt);
1805 return 0;
1806 }
1807 }
1808 else
1809 elt->sample = NULL;
1810
1811 LIST_ADDQ(&ref->head, &elt->list);
1812
1813 return 1;
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001814}
1815
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001816/* This function create sample found in <elt>, parse the pattern also
1817 * found in <elt> and insert it in <expr>. The function copy <patflags>
1818 * in <expr>. If the function fails, it returns0 and <err> is filled.
1819 * In succes case, the function returns 1.
1820 */
1821static inline
1822int pat_ref_push(struct pat_ref_elt *elt, struct pattern_expr *expr,
1823 int patflags, char **err)
1824{
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001825 struct sample_data *data;
Thierry FOURNIERd25c8422014-01-28 15:34:35 +01001826 struct pattern pattern;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001827
1828 /* Create sample */
1829 if (elt->sample && expr->pat_head->parse_smp) {
1830 /* New sample. */
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001831 data = malloc(sizeof(*data));
1832 if (!data)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001833 return 0;
1834
1835 /* Parse value. */
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001836 if (!expr->pat_head->parse_smp(elt->sample, data)) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001837 memprintf(err, "unable to parse '%s'", elt->sample);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001838 free(data);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001839 return 0;
1840 }
1841
1842 }
1843 else
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001844 data = NULL;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001845
Thierry FOURNIERd25c8422014-01-28 15:34:35 +01001846 /* initialise pattern */
1847 memset(&pattern, 0, sizeof(pattern));
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001848 pattern.data = data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01001849 pattern.ref = elt;
Thierry FOURNIERd25c8422014-01-28 15:34:35 +01001850
1851 /* parse pattern */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02001852 if (!expr->pat_head->parse(elt->pattern, &pattern, expr->mflags, err)) {
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001853 free(data);
Thierry FOURNIERd25c8422014-01-28 15:34:35 +01001854 return 0;
1855 }
1856
1857 /* index pattern */
1858 if (!expr->pat_head->index(expr, &pattern, err)) {
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001859 free(data);
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001860 return 0;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001861 }
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001862
1863 return 1;
1864}
1865
Thierry FOURNIER31db4ae2014-01-30 00:27:15 +01001866/* This function adds entry to <ref>. It can failed with memory error. The new
1867 * entry is added at all the pattern_expr registered in this reference. The
1868 * function stop on the first error encountered. It returns 0 and err is
1869 * filled. If an error is encountered, the complete add operation is cancelled.
1870 * If the insertion is a success the function returns 1.
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001871 */
1872int pat_ref_add(struct pat_ref *ref,
1873 const char *pattern, const char *sample,
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02001874 char **err)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001875{
1876 struct pat_ref_elt *elt;
1877 struct pattern_expr *expr;
1878
1879 elt = malloc(sizeof(*elt));
1880 if (!elt) {
1881 memprintf(err, "out of memory error");
1882 return 0;
1883 }
1884
1885 elt->line = -1;
1886
1887 elt->pattern = strdup(pattern);
1888 if (!elt->pattern) {
1889 free(elt);
1890 memprintf(err, "out of memory error");
1891 return 0;
1892 }
1893
1894 if (sample) {
1895 elt->sample = strdup(sample);
1896 if (!elt->sample) {
1897 free(elt->pattern);
1898 free(elt);
1899 memprintf(err, "out of memory error");
1900 return 0;
1901 }
1902 }
1903 else
1904 elt->sample = NULL;
1905
1906 LIST_ADDQ(&ref->head, &elt->list);
1907
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001908 list_for_each_entry(expr, &ref->pat, list) {
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02001909 if (!pat_ref_push(elt, expr, 0, err)) {
Thierry FOURNIER31db4ae2014-01-30 00:27:15 +01001910 /* If the insertion fails, try to delete all the added entries. */
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001911 pat_ref_delete_by_id(ref, elt);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001912 return 0;
1913 }
1914 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001915 return 1;
1916}
1917
Thierry FOURNIER46006bd2014-03-21 21:45:15 +01001918/* This function prune <ref>, replace all reference by the references
1919 * of <replace>, and reindex all the news values.
1920 *
1921 * The pattern are loaded in best effort and the errors are ignored,
1922 * but writed in the logs.
1923 */
1924void pat_ref_reload(struct pat_ref *ref, struct pat_ref *replace)
1925{
1926 struct pattern_expr *expr;
1927 struct pat_ref_elt *elt;
1928 char *err = NULL;
1929
1930 pat_ref_prune(ref);
1931
1932 LIST_ADD(&replace->head, &ref->head);
1933 LIST_DEL(&replace->head);
1934
1935 list_for_each_entry(elt, &ref->head, list) {
1936 list_for_each_entry(expr, &ref->pat, list) {
1937 if (!pat_ref_push(elt, expr, 0, &err)) {
1938 send_log(NULL, LOG_NOTICE, "%s", err);
1939 free(err);
1940 err = NULL;
1941 }
1942 }
1943 }
1944}
1945
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001946/* This function prune all entries of <ref>. This function
1947 * prune the associated pattern_expr.
1948 */
1949void pat_ref_prune(struct pat_ref *ref)
1950{
1951 struct pat_ref_elt *elt, *safe;
1952 struct pattern_expr *expr;
1953
1954 list_for_each_entry_safe(elt, safe, &ref->head, list) {
1955 LIST_DEL(&elt->list);
1956 free(elt->pattern);
1957 free(elt->sample);
1958 free(elt);
1959 }
1960
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001961 list_for_each_entry(expr, &ref->pat, list)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001962 expr->pat_head->prune(expr);
1963}
1964
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001965/* This function lookup for existing reference <ref> in pattern_head <head>. */
1966struct pattern_expr *pattern_lookup_expr(struct pattern_head *head, struct pat_ref *ref)
1967{
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001968 struct pattern_expr_list *expr;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001969
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001970 list_for_each_entry(expr, &head->head, list)
1971 if (expr->expr->ref == ref)
1972 return expr->expr;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001973 return NULL;
1974}
1975
1976/* This function create new pattern_expr associated to the reference <ref>.
1977 * <ref> can be NULL. If an error is occured, the function returns NULL and
1978 * <err> is filled. Otherwise, the function returns new pattern_expr linked
1979 * with <head> and <ref>.
Thierry FOURNIER315ec422014-11-24 11:14:42 +01001980 *
1981 * The returned value can be a alredy filled pattern list, in this case the
1982 * flag <reuse> is set.
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001983 */
Thierry FOURNIER315ec422014-11-24 11:14:42 +01001984struct pattern_expr *pattern_new_expr(struct pattern_head *head, struct pat_ref *ref,
1985 char **err, int *reuse)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001986{
1987 struct pattern_expr *expr;
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001988 struct pattern_expr_list *list;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001989
Thierry FOURNIER315ec422014-11-24 11:14:42 +01001990 if (reuse)
1991 *reuse = 0;
1992
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001993 /* Memory and initialization of the chain element. */
1994 list = malloc(sizeof(*list));
1995 if (!list) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001996 memprintf(err, "out of memory");
1997 return NULL;
1998 }
1999
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002000 /* Look for existing similar expr. No that only the index, parse and
2001 * parse_smp function must be identical for having similar pattern.
2002 * The other function depends of theses first.
2003 */
2004 if (ref) {
2005 list_for_each_entry(expr, &ref->pat, list)
2006 if (expr->pat_head->index == head->index &&
2007 expr->pat_head->parse == head->parse &&
2008 expr->pat_head->parse_smp == head->parse_smp)
2009 break;
2010 if (&expr->list == &ref->pat)
2011 expr = NULL;
2012 }
2013 else
2014 expr = NULL;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002015
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002016 /* If no similar expr was found, we create new expr. */
2017 if (!expr) {
2018 /* Get a lot of memory for the expr struct. */
2019 expr = malloc(sizeof(*expr));
2020 if (!expr) {
2021 memprintf(err, "out of memory");
2022 return NULL;
2023 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002024
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002025 /* Initialize this new expr. */
2026 pattern_init_expr(expr);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002027
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002028 /* This new pattern expression reference one of his heads. */
2029 expr->pat_head = head;
2030
2031 /* Link with ref, or to self to facilitate LIST_DEL() */
2032 if (ref)
2033 LIST_ADDQ(&ref->pat, &expr->list);
2034 else
2035 LIST_INIT(&expr->list);
2036
2037 expr->ref = ref;
2038
2039 /* We must free this pattern if it is no more used. */
2040 list->do_free = 1;
2041 }
2042 else {
2043 /* If the pattern used already exists, it is already linked
2044 * with ref and we must not free it.
2045 */
2046 list->do_free = 0;
Thierry FOURNIER315ec422014-11-24 11:14:42 +01002047 if (reuse)
2048 *reuse = 1;
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002049 }
2050
2051 /* The new list element reference the pattern_expr. */
2052 list->expr = expr;
2053
2054 /* Link the list element with the pattern_head. */
2055 LIST_ADDQ(&head->head, &list->list);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002056 return expr;
2057}
2058
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002059/* Reads patterns from a file. If <err_msg> is non-NULL, an error message will
2060 * be returned there on errors and the caller will have to free it.
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002061 *
2062 * The file contains one key + value per line. Lines which start with '#' are
2063 * ignored, just like empty lines. Leading tabs/spaces are stripped. The key is
2064 * then the first "word" (series of non-space/tabs characters), and the value is
2065 * what follows this series of space/tab till the end of the line excluding
2066 * trailing spaces/tabs.
2067 *
2068 * Example :
2069 *
2070 * # this is a comment and is ignored
2071 * 62.212.114.60 1wt.eu \n
2072 * <-><-----------><---><----><---->
2073 * | | | | `--- trailing spaces ignored
2074 * | | | `-------- value
2075 * | | `--------------- middle spaces ignored
2076 * | `------------------------ key
2077 * `-------------------------------- leading spaces ignored
2078 *
2079 * Return non-zero in case of succes, otherwise 0.
2080 */
2081int pat_ref_read_from_file_smp(struct pat_ref *ref, const char *filename, char **err)
2082{
2083 FILE *file;
2084 char *c;
2085 int ret = 0;
2086 int line = 0;
2087 char *key_beg;
2088 char *key_end;
2089 char *value_beg;
2090 char *value_end;
2091
2092 file = fopen(filename, "r");
2093 if (!file) {
2094 memprintf(err, "failed to open pattern file <%s>", filename);
2095 return 0;
2096 }
2097
2098 /* now parse all patterns. The file may contain only one pattern
2099 * followed by one value per line. The start spaces, separator spaces
2100 * and and spaces are stripped. Each can contain comment started by '#'
2101 */
2102 while (fgets(trash.str, trash.size, file) != NULL) {
2103 line++;
2104 c = trash.str;
2105
2106 /* ignore lines beginning with a dash */
2107 if (*c == '#')
2108 continue;
2109
2110 /* strip leading spaces and tabs */
2111 while (*c == ' ' || *c == '\t')
2112 c++;
2113
2114 /* empty lines are ignored too */
2115 if (*c == '\0' || *c == '\r' || *c == '\n')
2116 continue;
2117
2118 /* look for the end of the key */
2119 key_beg = c;
2120 while (*c && *c != ' ' && *c != '\t' && *c != '\n' && *c != '\r')
2121 c++;
2122
2123 key_end = c;
2124
2125 /* strip middle spaces and tabs */
2126 while (*c == ' ' || *c == '\t')
2127 c++;
2128
2129 /* look for the end of the value, it is the end of the line */
2130 value_beg = c;
2131 while (*c && *c != '\n' && *c != '\r')
2132 c++;
2133 value_end = c;
2134
2135 /* trim possibly trailing spaces and tabs */
2136 while (value_end > value_beg && (value_end[-1] == ' ' || value_end[-1] == '\t'))
2137 value_end--;
2138
2139 /* set final \0 and check entries */
2140 *key_end = '\0';
2141 *value_end = '\0';
2142
2143 /* insert values */
2144 if (!pat_ref_append(ref, key_beg, value_beg, line)) {
2145 memprintf(err, "out of memory");
2146 goto out_close;
2147 }
2148 }
2149
2150 /* succes */
2151 ret = 1;
2152
2153 out_close:
2154 fclose(file);
2155 return ret;
2156}
2157
2158/* Reads patterns from a file. If <err_msg> is non-NULL, an error message will
2159 * be returned there on errors and the caller will have to free it.
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002160 */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002161int pat_ref_read_from_file(struct pat_ref *ref, const char *filename, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002162{
2163 FILE *file;
2164 char *c;
2165 char *arg;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002166 int ret = 0;
2167 int line = 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002168
2169 file = fopen(filename, "r");
2170 if (!file) {
2171 memprintf(err, "failed to open pattern file <%s>", filename);
2172 return 0;
2173 }
2174
2175 /* now parse all patterns. The file may contain only one pattern per
2176 * line. If the line contains spaces, they will be part of the pattern.
2177 * The pattern stops at the first CR, LF or EOF encountered.
2178 */
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002179 while (fgets(trash.str, trash.size, file) != NULL) {
2180 line++;
2181 c = trash.str;
2182
2183 /* ignore lines beginning with a dash */
2184 if (*c == '#')
2185 continue;
2186
2187 /* strip leading spaces and tabs */
2188 while (*c == ' ' || *c == '\t')
2189 c++;
2190
2191
2192 arg = c;
2193 while (*c && *c != '\n' && *c != '\r')
2194 c++;
2195 *c = 0;
2196
2197 /* empty lines are ignored too */
2198 if (c == arg)
2199 continue;
2200
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002201 if (!pat_ref_append(ref, arg, NULL, line)) {
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002202 memprintf(err, "out of memory when loading patterns from file <%s>", filename);
2203 goto out_close;
2204 }
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002205 }
2206
2207 ret = 1; /* success */
2208
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002209 out_close:
2210 fclose(file);
2211 return ret;
2212}
2213
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002214int pattern_read_from_file(struct pattern_head *head, unsigned int refflags,
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002215 const char *filename, int patflags, int load_smp,
Thierry FOURNIER94580c92014-02-11 14:36:45 +01002216 char **err, const char *file, int line)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002217{
2218 struct pat_ref *ref;
2219 struct pattern_expr *expr;
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002220 struct pat_ref_elt *elt;
Willy Tarreau4deaf392014-11-26 13:17:03 +01002221 int reuse = 0;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002222
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002223 /* Lookup for the existing reference. */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002224 ref = pat_ref_lookup(filename);
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002225
2226 /* If the reference doesn't exists, create it and load associated file. */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002227 if (!ref) {
Thierry FOURNIER94580c92014-02-11 14:36:45 +01002228 chunk_printf(&trash,
2229 "pattern loaded from file '%s' used by %s at file '%s' line %d",
2230 filename, refflags & PAT_REF_MAP ? "map" : "acl", file, line);
2231
2232 ref = pat_ref_new(filename, trash.str, refflags);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002233 if (!ref) {
2234 memprintf(err, "out of memory");
2235 return 0;
2236 }
2237
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002238 if (load_smp) {
Thierry FOURNIERc0bd9102014-01-29 12:32:58 +01002239 ref->flags |= PAT_REF_SMP;
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002240 if (!pat_ref_read_from_file_smp(ref, filename, err))
2241 return 0;
2242 }
2243 else {
2244 if (!pat_ref_read_from_file(ref, filename, err))
2245 return 0;
2246 }
2247 }
2248 else {
Thierry FOURNIERc0bd9102014-01-29 12:32:58 +01002249 /* The reference already exists, check the map compatibility. */
2250
2251 /* If the load require samples and the flag PAT_REF_SMP is not set,
2252 * the reference doesn't contain sample, and cannot be used.
2253 */
2254 if (load_smp) {
2255 if (!(ref->flags & PAT_REF_SMP)) {
2256 memprintf(err, "The file \"%s\" is already used as one column file "
2257 "and cannot be used by as two column file.",
2258 filename);
2259 return 0;
2260 }
2261 }
2262 else {
2263 /* The load doesn't require samples. If the flag PAT_REF_SMP is
2264 * set, the reference contains a sample, and cannot be used.
2265 */
2266 if (ref->flags & PAT_REF_SMP) {
2267 memprintf(err, "The file \"%s\" is already used as two column file "
2268 "and cannot be used by as one column file.",
2269 filename);
2270 return 0;
2271 }
2272 }
2273
Thierry FOURNIER94580c92014-02-11 14:36:45 +01002274 /* Extends display */
2275 chunk_printf(&trash, "%s", ref->display);
2276 chunk_appendf(&trash, ", by %s at file '%s' line %d",
2277 refflags & PAT_REF_MAP ? "map" : "acl", file, line);
2278 free(ref->display);
2279 ref->display = strdup(trash.str);
2280 if (!ref->display) {
2281 memprintf(err, "out of memory");
2282 return 0;
2283 }
2284
Thierry FOURNIERc0bd9102014-01-29 12:32:58 +01002285 /* Merge flags. */
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002286 ref->flags |= refflags;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002287 }
2288
2289 /* Now, we can loading patterns from the reference. */
2290
2291 /* Lookup for existing reference in the head. If the reference
2292 * doesn't exists, create it.
2293 */
2294 expr = pattern_lookup_expr(head, ref);
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02002295 if (!expr || (expr->mflags != patflags)) {
Thierry FOURNIER315ec422014-11-24 11:14:42 +01002296 expr = pattern_new_expr(head, ref, err, &reuse);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002297 if (!expr)
2298 return 0;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02002299 expr->mflags = patflags;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002300 }
2301
Thierry FOURNIER315ec422014-11-24 11:14:42 +01002302 /* The returned expression may be not empty, because the function
2303 * "pattern_new_expr" lookup for similar pattern list and can
2304 * reuse a already filled pattern list. In this case, we can not
2305 * reload the patterns.
2306 */
2307 if (reuse)
2308 return 1;
2309
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002310 /* Load reference content in the pattern expression. */
2311 list_for_each_entry(elt, &ref->head, list) {
2312 if (!pat_ref_push(elt, expr, patflags, err)) {
2313 if (elt->line > 0)
2314 memprintf(err, "%s at line %d of file '%s'",
2315 *err, elt->line, filename);
2316 return 0;
2317 }
2318 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002319
2320 return 1;
2321}
2322
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01002323/* This function executes a pattern match on a sample. It applies pattern <expr>
2324 * to sample <smp>. The function returns NULL if the sample dont match. It returns
2325 * non-null if the sample match. If <fill> is true and the sample match, the
2326 * function returns the matched pattern. In many cases, this pattern can be a
2327 * static buffer.
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002328 */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002329struct pattern *pattern_exec_match(struct pattern_head *head, struct sample *smp, int fill)
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002330{
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002331 struct pattern_expr_list *list;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002332 struct pattern *pat;
2333
2334 if (!head->match) {
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01002335 if (fill) {
Thierry FOURNIER503bb092015-08-19 08:35:43 +02002336 static_pattern.data = NULL;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01002337 static_pattern.ref = NULL;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02002338 static_pattern.sflags = 0;
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002339 static_pattern.type = SMP_T_SINT;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01002340 static_pattern.val.i = 1;
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01002341 }
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01002342 return &static_pattern;
2343 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002344
Thierry FOURNIER5d344082014-01-27 14:19:53 +01002345 /* convert input to string */
2346 if (!sample_convert(smp, head->expect_type))
2347 return NULL;
2348
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002349 list_for_each_entry(list, &head->head, list) {
2350 pat = head->match(smp, list->expr, fill);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002351 if (pat)
2352 return pat;
2353 }
2354 return NULL;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002355}
2356
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01002357/* This function prune the pattern expression. */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002358void pattern_prune(struct pattern_head *head)
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01002359{
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002360 struct pattern_expr_list *list, *safe;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002361
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002362 list_for_each_entry_safe(list, safe, &head->head, list) {
2363 LIST_DEL(&list->list);
2364 if (list->do_free) {
2365 LIST_DEL(&list->expr->list);
2366 head->prune(list->expr);
2367 free(list->expr);
2368 }
2369 free(list);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002370 }
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01002371}
2372
Thierry FOURNIER55d0b102014-01-15 11:25:26 +01002373/* This function lookup for a pattern matching the <key> and return a
2374 * pointer to a pointer of the sample stoarge. If the <key> dont match,
2375 * the function returns NULL. If the key cannot be parsed, the function
2376 * fill <err>.
2377 */
Thierry FOURNIER12ba0c22015-08-14 00:02:11 +02002378struct sample_data **pattern_find_smp(struct pattern_expr *expr, struct pat_ref_elt *ref)
Thierry FOURNIER55d0b102014-01-15 11:25:26 +01002379{
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01002380 struct ebmb_node *node;
2381 struct pattern_tree *elt;
2382 struct pattern_list *pat;
Thierry FOURNIER55d0b102014-01-15 11:25:26 +01002383
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01002384 for (node = ebmb_first(&expr->pattern_tree);
2385 node;
2386 node = ebmb_next(node)) {
2387 elt = container_of(node, struct pattern_tree, node);
2388 if (elt->ref == ref)
Thierry FOURNIER503bb092015-08-19 08:35:43 +02002389 return &elt->data;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01002390 }
2391
2392 for (node = ebmb_first(&expr->pattern_tree_2);
2393 node;
2394 node = ebmb_next(node)) {
2395 elt = container_of(node, struct pattern_tree, node);
2396 if (elt->ref == ref)
Thierry FOURNIER503bb092015-08-19 08:35:43 +02002397 return &elt->data;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01002398 }
2399
2400 list_for_each_entry(pat, &expr->patterns, list)
2401 if (pat->pat.ref == ref)
Thierry FOURNIER503bb092015-08-19 08:35:43 +02002402 return &pat->pat.data;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01002403
2404 return NULL;
Thierry FOURNIER55d0b102014-01-15 11:25:26 +01002405}
2406
Thierry FOURNIERb1136502014-01-15 11:38:49 +01002407/* This function search all the pattern matching the <key> and delete it.
2408 * If the parsing of the input key fails, the function returns 0 and the
2409 * <err> is filled, else return 1;
2410 */
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01002411int pattern_delete(struct pattern_expr *expr, struct pat_ref_elt *ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01002412{
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01002413 expr->pat_head->delete(expr, ref);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01002414 return 1;
2415}
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002416
2417/* This function finalize the configuration parsing. Its set all the
2418 * automatic ids
2419 */
2420void pattern_finalize_config(void)
2421{
2422 int i = 0;
2423 struct pat_ref *ref, *ref2, *ref3;
2424 struct list pr = LIST_HEAD_INIT(pr);
2425
Willy Tarreauf3045d22015-04-29 16:24:50 +02002426 pat_lru_seed = random();
2427 if (global.tune.pattern_cache)
2428 pat_lru_tree = lru64_new(global.tune.pattern_cache);
2429
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002430 list_for_each_entry(ref, &pattern_reference, list) {
2431 if (ref->unique_id == -1) {
2432 /* Look for the first free id. */
2433 while (1) {
2434 list_for_each_entry(ref2, &pattern_reference, list) {
2435 if (ref2->unique_id == i) {
2436 i++;
2437 break;
2438 }
2439 }
Willy Tarreau3b786962014-04-26 12:37:25 +02002440 if (&ref2->list == &pattern_reference)
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002441 break;
2442 }
2443
2444 /* Uses the unique id and increment it for the next entry. */
2445 ref->unique_id = i;
2446 i++;
2447 }
2448 }
2449
2450 /* This sort the reference list by id. */
2451 list_for_each_entry_safe(ref, ref2, &pattern_reference, list) {
2452 LIST_DEL(&ref->list);
2453 list_for_each_entry(ref3, &pr, list) {
2454 if (ref->unique_id < ref3->unique_id) {
2455 LIST_ADDQ(&ref3->list, &ref->list);
2456 break;
2457 }
2458 }
2459 if (&ref3->list == &pr)
2460 LIST_ADDQ(&pr, &ref->list);
2461 }
2462
2463 /* swap root */
2464 LIST_ADD(&pr, &pattern_reference);
2465 LIST_DEL(&pr);
2466}