blob: 60fe462a105b1ecc212d0ce18966c0f2e21282bc [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 Fournier8feaa662016-02-10 22:55:20 +010044 [PAT_MATCH_REGM] = "regm",
Thierry FOURNIERed66c292013-11-28 11:05:19 +010045};
46
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +020047int (*pat_parse_fcts[PAT_MATCH_NUM])(const char *, struct pattern *, int, char **) = {
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010048 [PAT_MATCH_FOUND] = pat_parse_nothing,
49 [PAT_MATCH_BOOL] = pat_parse_nothing,
50 [PAT_MATCH_INT] = pat_parse_int,
51 [PAT_MATCH_IP] = pat_parse_ip,
52 [PAT_MATCH_BIN] = pat_parse_bin,
Thierry FOURNIER5d344082014-01-27 14:19:53 +010053 [PAT_MATCH_LEN] = pat_parse_int,
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010054 [PAT_MATCH_STR] = pat_parse_str,
55 [PAT_MATCH_BEG] = pat_parse_str,
56 [PAT_MATCH_SUB] = pat_parse_str,
57 [PAT_MATCH_DIR] = pat_parse_str,
58 [PAT_MATCH_DOM] = pat_parse_str,
59 [PAT_MATCH_END] = pat_parse_str,
60 [PAT_MATCH_REG] = pat_parse_reg,
Thierry Fournier8feaa662016-02-10 22:55:20 +010061 [PAT_MATCH_REGM] = pat_parse_reg,
Thierry FOURNIERed66c292013-11-28 11:05:19 +010062};
63
Thierry FOURNIERb9b08462013-12-13 15:12:32 +010064int (*pat_index_fcts[PAT_MATCH_NUM])(struct pattern_expr *, struct pattern *, char **) = {
65 [PAT_MATCH_FOUND] = pat_idx_list_val,
66 [PAT_MATCH_BOOL] = pat_idx_list_val,
67 [PAT_MATCH_INT] = pat_idx_list_val,
68 [PAT_MATCH_IP] = pat_idx_tree_ip,
69 [PAT_MATCH_BIN] = pat_idx_list_ptr,
70 [PAT_MATCH_LEN] = pat_idx_list_val,
71 [PAT_MATCH_STR] = pat_idx_tree_str,
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +020072 [PAT_MATCH_BEG] = pat_idx_tree_pfx,
Thierry FOURNIERb9b08462013-12-13 15:12:32 +010073 [PAT_MATCH_SUB] = pat_idx_list_str,
74 [PAT_MATCH_DIR] = pat_idx_list_str,
75 [PAT_MATCH_DOM] = pat_idx_list_str,
76 [PAT_MATCH_END] = pat_idx_list_str,
77 [PAT_MATCH_REG] = pat_idx_list_reg,
Thierry Fournier8feaa662016-02-10 22:55:20 +010078 [PAT_MATCH_REGM] = pat_idx_list_regm,
Thierry FOURNIERb9b08462013-12-13 15:12:32 +010079};
80
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +010081void (*pat_delete_fcts[PAT_MATCH_NUM])(struct pattern_expr *, struct pat_ref_elt *) = {
Thierry FOURNIERb1136502014-01-15 11:38:49 +010082 [PAT_MATCH_FOUND] = pat_del_list_val,
83 [PAT_MATCH_BOOL] = pat_del_list_val,
84 [PAT_MATCH_INT] = pat_del_list_val,
85 [PAT_MATCH_IP] = pat_del_tree_ip,
86 [PAT_MATCH_BIN] = pat_del_list_ptr,
87 [PAT_MATCH_LEN] = pat_del_list_val,
88 [PAT_MATCH_STR] = pat_del_tree_str,
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +020089 [PAT_MATCH_BEG] = pat_del_tree_str,
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +010090 [PAT_MATCH_SUB] = pat_del_list_ptr,
91 [PAT_MATCH_DIR] = pat_del_list_ptr,
92 [PAT_MATCH_DOM] = pat_del_list_ptr,
93 [PAT_MATCH_END] = pat_del_list_ptr,
Thierry FOURNIERb1136502014-01-15 11:38:49 +010094 [PAT_MATCH_REG] = pat_del_list_reg,
Thierry Fournier8feaa662016-02-10 22:55:20 +010095 [PAT_MATCH_REGM] = pat_del_list_reg,
Thierry FOURNIERb1136502014-01-15 11:38:49 +010096};
97
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +010098void (*pat_prune_fcts[PAT_MATCH_NUM])(struct pattern_expr *) = {
99 [PAT_MATCH_FOUND] = pat_prune_val,
100 [PAT_MATCH_BOOL] = pat_prune_val,
101 [PAT_MATCH_INT] = pat_prune_val,
102 [PAT_MATCH_IP] = pat_prune_val,
103 [PAT_MATCH_BIN] = pat_prune_ptr,
104 [PAT_MATCH_LEN] = pat_prune_val,
105 [PAT_MATCH_STR] = pat_prune_ptr,
106 [PAT_MATCH_BEG] = pat_prune_ptr,
107 [PAT_MATCH_SUB] = pat_prune_ptr,
108 [PAT_MATCH_DIR] = pat_prune_ptr,
109 [PAT_MATCH_DOM] = pat_prune_ptr,
110 [PAT_MATCH_END] = pat_prune_ptr,
111 [PAT_MATCH_REG] = pat_prune_reg,
Thierry Fournier8feaa662016-02-10 22:55:20 +0100112 [PAT_MATCH_REGM] = pat_prune_reg,
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +0100113};
114
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100115struct pattern *(*pat_match_fcts[PAT_MATCH_NUM])(struct sample *, struct pattern_expr *, int) = {
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100116 [PAT_MATCH_FOUND] = NULL,
117 [PAT_MATCH_BOOL] = pat_match_nothing,
118 [PAT_MATCH_INT] = pat_match_int,
119 [PAT_MATCH_IP] = pat_match_ip,
120 [PAT_MATCH_BIN] = pat_match_bin,
121 [PAT_MATCH_LEN] = pat_match_len,
122 [PAT_MATCH_STR] = pat_match_str,
123 [PAT_MATCH_BEG] = pat_match_beg,
124 [PAT_MATCH_SUB] = pat_match_sub,
125 [PAT_MATCH_DIR] = pat_match_dir,
126 [PAT_MATCH_DOM] = pat_match_dom,
127 [PAT_MATCH_END] = pat_match_end,
128 [PAT_MATCH_REG] = pat_match_reg,
Thierry Fournier8feaa662016-02-10 22:55:20 +0100129 [PAT_MATCH_REGM] = pat_match_regm,
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100130};
131
Thierry FOURNIERe3ded592013-12-06 15:36:54 +0100132/* Just used for checking configuration compatibility */
133int pat_match_types[PAT_MATCH_NUM] = {
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200134 [PAT_MATCH_FOUND] = SMP_T_SINT,
135 [PAT_MATCH_BOOL] = SMP_T_SINT,
136 [PAT_MATCH_INT] = SMP_T_SINT,
Thierry FOURNIERe3ded592013-12-06 15:36:54 +0100137 [PAT_MATCH_IP] = SMP_T_ADDR,
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100138 [PAT_MATCH_BIN] = SMP_T_BIN,
139 [PAT_MATCH_LEN] = SMP_T_STR,
140 [PAT_MATCH_STR] = SMP_T_STR,
141 [PAT_MATCH_BEG] = SMP_T_STR,
142 [PAT_MATCH_SUB] = SMP_T_STR,
143 [PAT_MATCH_DIR] = SMP_T_STR,
144 [PAT_MATCH_DOM] = SMP_T_STR,
145 [PAT_MATCH_END] = SMP_T_STR,
146 [PAT_MATCH_REG] = SMP_T_STR,
Thierry Fournier8feaa662016-02-10 22:55:20 +0100147 [PAT_MATCH_REGM] = SMP_T_STR,
Thierry FOURNIERe3ded592013-12-06 15:36:54 +0100148};
149
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +0100150/* this struct is used to return information */
151static struct pattern static_pattern;
152
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100153/* This is the root of the list of all pattern_ref avalaibles. */
154struct list pattern_reference = LIST_HEAD_INIT(pattern_reference);
155
Willy Tarreauf3045d22015-04-29 16:24:50 +0200156static struct lru64_head *pat_lru_tree;
157static unsigned long long pat_lru_seed;
158
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100159/*
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100160 *
161 * The following functions are not exported and are used by internals process
162 * of pattern matching
163 *
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100164 */
165
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100166/* Background: Fast way to find a zero byte in a word
167 * http://graphics.stanford.edu/~seander/bithacks.html#ZeroInWord
168 * hasZeroByte = (v - 0x01010101UL) & ~v & 0x80808080UL;
169 *
170 * To look for 4 different byte values, xor the word with those bytes and
171 * then check for zero bytes:
172 *
173 * v = (((unsigned char)c * 0x1010101U) ^ delimiter)
174 * where <delimiter> is the 4 byte values to look for (as an uint)
175 * and <c> is the character that is being tested
176 */
177static inline unsigned int is_delimiter(unsigned char c, unsigned int mask)
178{
179 mask ^= (c * 0x01010101); /* propagate the char to all 4 bytes */
180 return (mask - 0x01010101) & ~mask & 0x80808080U;
181}
182
183static inline unsigned int make_4delim(unsigned char d1, unsigned char d2, unsigned char d3, unsigned char d4)
184{
185 return d1 << 24 | d2 << 16 | d3 << 8 | d4;
186}
187
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100188
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100189/*
190 *
191 * These functions are exported and may be used by any other component.
192 *
Willy Tarreau5def8ef2014-08-29 15:19:33 +0200193 * The following functions are used for parsing pattern matching input value.
194 * The <text> contain the string to be parsed. <pattern> must be a preallocated
195 * pattern. The pat_parse_* functions fill this structure with the parsed value.
196 * <err> is filled with an error message built with memprintf() function. It is
197 * allowed to use a trash as a temporary storage for the returned pattern, as
198 * the next call after these functions will be pat_idx_*.
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100199 *
Willy Tarreau5def8ef2014-08-29 15:19:33 +0200200 * In success case, the pat_parse_* function returns 1. If the function
201 * fails, it returns 0 and <err> is filled.
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100202 */
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100203
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100204/* ignore the current line */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200205int pat_parse_nothing(const char *text, struct pattern *pattern, int mflags, char **err)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100206{
207 return 1;
208}
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100209
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100210/* Parse a string. It is allocated and duplicated. */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200211int pat_parse_str(const char *text, struct pattern *pattern, int mflags, char **err)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100212{
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100213 pattern->type = SMP_T_STR;
Thierry FOURNIERedc15c32013-12-13 15:36:59 +0100214 pattern->ptr.str = (char *)text;
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100215 pattern->len = strlen(text);
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100216 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100217}
218
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100219/* Parse a binary written in hexa. It is allocated. */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200220int pat_parse_bin(const char *text, struct pattern *pattern, int mflags, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100221{
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100222 struct chunk *trash;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100223
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100224 pattern->type = SMP_T_BIN;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100225 trash = get_trash_chunk();
226 pattern->len = trash->size;
227 pattern->ptr.str = trash->str;
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100228 return !!parse_binary(text, &pattern->ptr.str, &pattern->len, err);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100229}
230
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100231/* Parse a regex. It is allocated. */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200232int pat_parse_reg(const char *text, struct pattern *pattern, int mflags, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100233{
Thierry FOURNIER0b6d15f2014-01-29 19:35:16 +0100234 pattern->ptr.str = (char *)text;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100235 return 1;
236}
237
238/* Parse a range of positive integers delimited by either ':' or '-'. If only
239 * one integer is read, it is set as both min and max. An operator may be
240 * specified as the prefix, among this list of 5 :
241 *
242 * 0:eq, 1:gt, 2:ge, 3:lt, 4:le
243 *
244 * The default operator is "eq". It supports range matching. Ranges are
245 * rejected for other operators. The operator may be changed at any time.
246 * The operator is stored in the 'opaque' argument.
247 *
248 * If err is non-NULL, an error message will be returned there on errors and
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100249 * the caller will have to free it. The function returns zero on error, and
250 * non-zero on success.
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100251 *
252 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200253int pat_parse_int(const char *text, struct pattern *pattern, int mflags, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100254{
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100255 const char *ptr = text;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100256
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200257 pattern->type = SMP_T_SINT;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100258
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100259 /* Empty string is not valid */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100260 if (!*text)
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100261 goto not_valid_range;
262
263 /* Search ':' or '-' separator. */
264 while (*ptr != '\0' && *ptr != ':' && *ptr != '-')
265 ptr++;
266
267 /* If separator not found. */
268 if (!*ptr) {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100269 if (strl2llrc(text, ptr - text, &pattern->val.range.min) != 0) {
270 memprintf(err, "'%s' is not a number", text);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100271 return 0;
272 }
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100273 pattern->val.range.max = pattern->val.range.min;
274 pattern->val.range.min_set = 1;
275 pattern->val.range.max_set = 1;
276 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100277 }
278
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100279 /* If the separator is the first character. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100280 if (ptr == text && *(ptr + 1) != '\0') {
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100281 if (strl2llrc(ptr + 1, strlen(ptr + 1), &pattern->val.range.max) != 0)
282 goto not_valid_range;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100283
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100284 pattern->val.range.min_set = 0;
285 pattern->val.range.max_set = 1;
286 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100287 }
288
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100289 /* If separator is the last character. */
290 if (*(ptr + 1) == '\0') {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100291 if (strl2llrc(text, ptr - text, &pattern->val.range.min) != 0)
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100292 goto not_valid_range;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100293
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100294 pattern->val.range.min_set = 1;
295 pattern->val.range.max_set = 0;
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100296 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100297 }
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100298
299 /* Else, parse two numbers. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100300 if (strl2llrc(text, ptr - text, &pattern->val.range.min) != 0)
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100301 goto not_valid_range;
302
303 if (strl2llrc(ptr + 1, strlen(ptr + 1), &pattern->val.range.max) != 0)
304 goto not_valid_range;
305
306 if (pattern->val.range.min > pattern->val.range.max)
307 goto not_valid_range;
308
309 pattern->val.range.min_set = 1;
310 pattern->val.range.max_set = 1;
311 return 1;
312
313 not_valid_range:
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100314 memprintf(err, "'%s' is not a valid number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100315 return 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100316}
317
318/* Parse a range of positive 2-component versions delimited by either ':' or
319 * '-'. The version consists in a major and a minor, both of which must be
320 * smaller than 65536, because internally they will be represented as a 32-bit
321 * integer.
322 * If only one version is read, it is set as both min and max. Just like for
323 * pure integers, an operator may be specified as the prefix, among this list
324 * of 5 :
325 *
326 * 0:eq, 1:gt, 2:ge, 3:lt, 4:le
327 *
328 * The default operator is "eq". It supports range matching. Ranges are
329 * rejected for other operators. The operator may be changed at any time.
330 * The operator is stored in the 'opaque' argument. This allows constructs
331 * such as the following one :
332 *
333 * acl obsolete_ssl ssl_req_proto lt 3
334 * acl unsupported_ssl ssl_req_proto gt 3.1
335 * acl valid_ssl ssl_req_proto 3.0-3.1
336 *
337 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200338int pat_parse_dotted_ver(const char *text, struct pattern *pattern, int mflags, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100339{
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100340 const char *ptr = text;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100341
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200342 pattern->type = SMP_T_SINT;
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100343
344 /* Search ':' or '-' separator. */
345 while (*ptr != '\0' && *ptr != ':' && *ptr != '-')
346 ptr++;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100347
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100348 /* If separator not found. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100349 if (*ptr == '\0' && ptr > text) {
350 if (strl2llrc_dotted(text, ptr-text, &pattern->val.range.min) != 0) {
351 memprintf(err, "'%s' is not a dotted number", text);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100352 return 0;
353 }
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100354 pattern->val.range.max = pattern->val.range.min;
355 pattern->val.range.min_set = 1;
356 pattern->val.range.max_set = 1;
357 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100358 }
359
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100360 /* If the separator is the first character. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100361 if (ptr == text && *(ptr+1) != '\0') {
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100362 if (strl2llrc_dotted(ptr+1, strlen(ptr+1), &pattern->val.range.max) != 0) {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100363 memprintf(err, "'%s' is not a valid dotted number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100364 return 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100365 }
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100366 pattern->val.range.min_set = 0;
367 pattern->val.range.max_set = 1;
368 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100369 }
370
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100371 /* If separator is the last character. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100372 if (ptr == &text[strlen(text)-1]) {
373 if (strl2llrc_dotted(text, ptr-text, &pattern->val.range.min) != 0) {
374 memprintf(err, "'%s' is not a valid dotted number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100375 return 0;
376 }
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100377 pattern->val.range.min_set = 1;
378 pattern->val.range.max_set = 0;
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100379 return 1;
380 }
381
382 /* Else, parse two numbers. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100383 if (strl2llrc_dotted(text, ptr-text, &pattern->val.range.min) != 0) {
384 memprintf(err, "'%s' is not a valid dotted number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100385 return 0;
386 }
387 if (strl2llrc_dotted(ptr+1, strlen(ptr+1), &pattern->val.range.max) != 0) {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100388 memprintf(err, "'%s' is not a valid dotted number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100389 return 0;
390 }
391 if (pattern->val.range.min > pattern->val.range.max) {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100392 memprintf(err, "'%s' is not a valid dotted number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100393 return 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100394 }
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100395 pattern->val.range.min_set = 1;
396 pattern->val.range.max_set = 1;
397 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100398}
399
400/* Parse an IP address and an optional mask in the form addr[/mask].
401 * The addr may either be an IPv4 address or a hostname. The mask
402 * may either be a dotted mask or a number of bits. Returns 1 if OK,
403 * otherwise 0. NOTE: IP address patterns are typed (IPV4/IPV6).
404 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200405int pat_parse_ip(const char *text, struct pattern *pattern, int mflags, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100406{
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200407 if (str2net(text, !(mflags & PAT_MF_NO_DNS) && (global.mode & MODE_STARTING),
Thierry FOURNIERfc7ac7b2014-02-11 15:23:04 +0100408 &pattern->val.ipv4.addr, &pattern->val.ipv4.mask)) {
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100409 pattern->type = SMP_T_IPV4;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100410 return 1;
411 }
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100412 else if (str62net(text, &pattern->val.ipv6.addr, &pattern->val.ipv6.mask)) {
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100413 pattern->type = SMP_T_IPV6;
414 return 1;
415 }
416 else {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100417 memprintf(err, "'%s' is not a valid IPv4 or IPv6 address", text);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100418 return 0;
419 }
420}
421
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100422/*
423 *
424 * These functions are exported and may be used by any other component.
425 *
426 * This fucntion just take a sample <smp> and check if this sample match
427 * with the pattern <pattern>. This fucntion return just PAT_MATCH or
428 * PAT_NOMATCH.
429 *
430 */
431
432/* always return false */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100433struct pattern *pat_match_nothing(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100434{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200435 if (smp->data.u.sint) {
Thierry FOURNIERe5978bf2014-03-17 19:53:10 +0100436 if (fill) {
Thierry FOURNIER503bb092015-08-19 08:35:43 +0200437 static_pattern.data = NULL;
Thierry FOURNIERe5978bf2014-03-17 19:53:10 +0100438 static_pattern.ref = NULL;
Thierry FOURNIERe5978bf2014-03-17 19:53:10 +0100439 static_pattern.type = 0;
440 static_pattern.ptr.str = NULL;
441 }
442 return &static_pattern;
443 }
444 else
445 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100446}
447
448
449/* NB: For two strings to be identical, it is required that their lengths match */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100450struct pattern *pat_match_str(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100451{
452 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100453 struct ebmb_node *node;
454 char prev;
455 struct pattern_tree *elt;
456 struct pattern_list *lst;
457 struct pattern *pattern;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200458 struct pattern *ret = NULL;
459 struct lru64 *lru = NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100460
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100461 /* Lookup a string in the expression's pattern tree. */
462 if (!eb_is_empty(&expr->pattern_tree)) {
463 /* we may have to force a trailing zero on the test pattern */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200464 prev = smp->data.u.str.str[smp->data.u.str.len];
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100465 if (prev)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200466 smp->data.u.str.str[smp->data.u.str.len] = '\0';
467 node = ebst_lookup(&expr->pattern_tree, smp->data.u.str.str);
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100468 if (prev)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200469 smp->data.u.str.str[smp->data.u.str.len] = prev;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100470
471 if (node) {
472 if (fill) {
473 elt = ebmb_entry(node, struct pattern_tree, node);
Thierry FOURNIER503bb092015-08-19 08:35:43 +0200474 static_pattern.data = elt->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +0100475 static_pattern.ref = elt->ref;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200476 static_pattern.sflags = PAT_SF_TREE;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100477 static_pattern.type = SMP_T_STR;
478 static_pattern.ptr.str = (char *)elt->node.key;
479 }
480 return &static_pattern;
481 }
482 }
483
484 /* look in the list */
Willy Tarreauf3045d22015-04-29 16:24:50 +0200485 if (pat_lru_tree) {
Willy Tarreauaee93142015-05-04 17:18:42 +0200486 unsigned long long seed = pat_lru_seed ^ (long)expr;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200487
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200488 lru = lru64_get(XXH64(smp->data.u.str.str, smp->data.u.str.len, seed),
Willy Tarreauf3045d22015-04-29 16:24:50 +0200489 pat_lru_tree, expr, expr->revision);
490 if (lru && lru->domain)
491 return lru->data;
492 }
493
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100494 list_for_each_entry(lst, &expr->patterns, list) {
495 pattern = &lst->pat;
496
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200497 if (pattern->len != smp->data.u.str.len)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100498 continue;
499
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200500 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200501 if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.str.str, smp->data.u.str.len) == 0) ||
502 (!icase && strncmp(pattern->ptr.str, smp->data.u.str.str, smp->data.u.str.len) == 0)) {
Willy Tarreauf3045d22015-04-29 16:24:50 +0200503 ret = pattern;
504 break;
505 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100506 }
507
Willy Tarreauf3045d22015-04-29 16:24:50 +0200508 if (lru)
Christopher Fauletf90ac552015-06-09 17:06:17 +0200509 lru64_commit(lru, ret, expr, expr->revision, NULL);
Willy Tarreauf3045d22015-04-29 16:24:50 +0200510
511 return ret;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100512}
513
514/* NB: For two binaries buf to be identical, it is required that their lengths match */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100515struct pattern *pat_match_bin(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100516{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100517 struct pattern_list *lst;
518 struct pattern *pattern;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200519 struct pattern *ret = NULL;
520 struct lru64 *lru = NULL;
521
522 if (pat_lru_tree) {
Willy Tarreauaee93142015-05-04 17:18:42 +0200523 unsigned long long seed = pat_lru_seed ^ (long)expr;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100524
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200525 lru = lru64_get(XXH64(smp->data.u.str.str, smp->data.u.str.len, seed),
Willy Tarreauf3045d22015-04-29 16:24:50 +0200526 pat_lru_tree, expr, expr->revision);
527 if (lru && lru->domain)
528 return lru->data;
529 }
530
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100531 list_for_each_entry(lst, &expr->patterns, list) {
532 pattern = &lst->pat;
533
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200534 if (pattern->len != smp->data.u.str.len)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100535 continue;
536
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200537 if (memcmp(pattern->ptr.str, smp->data.u.str.str, smp->data.u.str.len) == 0) {
Willy Tarreauf3045d22015-04-29 16:24:50 +0200538 ret = pattern;
539 break;
540 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100541 }
542
Willy Tarreauf3045d22015-04-29 16:24:50 +0200543 if (lru)
Christopher Fauletf90ac552015-06-09 17:06:17 +0200544 lru64_commit(lru, ret, expr, expr->revision, NULL);
Willy Tarreauf3045d22015-04-29 16:24:50 +0200545
546 return ret;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100547}
548
549/* Executes a regex. It temporarily changes the data to add a trailing zero,
Thierry Fournier8feaa662016-02-10 22:55:20 +0100550 * and restores the previous character when leaving. This function fills
551 * a matching array.
552 */
553struct pattern *pat_match_regm(struct sample *smp, struct pattern_expr *expr, int fill)
554{
555 struct pattern_list *lst;
556 struct pattern *pattern;
557 struct pattern *ret = NULL;
558
559 list_for_each_entry(lst, &expr->patterns, list) {
560 pattern = &lst->pat;
561
562 if (regex_exec_match2(pattern->ptr.reg, smp->data.u.str.str, smp->data.u.str.len,
563 MAX_MATCH, pmatch, 0)) {
564 ret = pattern;
565 smp->ctx.a[0] = pmatch;
566 break;
567 }
568 }
569
570 return ret;
571}
572
573/* Executes a regex. It temporarily changes the data to add a trailing zero,
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100574 * and restores the previous character when leaving.
575 */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100576struct pattern *pat_match_reg(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100577{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100578 struct pattern_list *lst;
579 struct pattern *pattern;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200580 struct pattern *ret = NULL;
581 struct lru64 *lru = NULL;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100582
Willy Tarreauf3045d22015-04-29 16:24:50 +0200583 if (pat_lru_tree) {
Willy Tarreauaee93142015-05-04 17:18:42 +0200584 unsigned long long seed = pat_lru_seed ^ (long)expr;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200585
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200586 lru = lru64_get(XXH64(smp->data.u.str.str, smp->data.u.str.len, seed),
Willy Tarreauf3045d22015-04-29 16:24:50 +0200587 pat_lru_tree, expr, expr->revision);
588 if (lru && lru->domain)
589 return lru->data;
590 }
591
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100592 list_for_each_entry(lst, &expr->patterns, list) {
593 pattern = &lst->pat;
594
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200595 if (regex_exec2(pattern->ptr.reg, smp->data.u.str.str, smp->data.u.str.len)) {
Willy Tarreauf3045d22015-04-29 16:24:50 +0200596 ret = pattern;
597 break;
598 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100599 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200600
601 if (lru)
Christopher Fauletf90ac552015-06-09 17:06:17 +0200602 lru64_commit(lru, ret, expr, expr->revision, NULL);
Willy Tarreauf3045d22015-04-29 16:24:50 +0200603
604 return ret;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100605}
606
607/* Checks that the pattern matches the beginning of the tested string. */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100608struct pattern *pat_match_beg(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100609{
610 int icase;
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +0200611 struct ebmb_node *node;
612 char prev;
613 struct pattern_tree *elt;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100614 struct pattern_list *lst;
615 struct pattern *pattern;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200616 struct pattern *ret = NULL;
617 struct lru64 *lru = NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100618
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +0200619 /* Lookup a string in the expression's pattern tree. */
620 if (!eb_is_empty(&expr->pattern_tree)) {
621 /* we may have to force a trailing zero on the test pattern */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200622 prev = smp->data.u.str.str[smp->data.u.str.len];
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +0200623 if (prev)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200624 smp->data.u.str.str[smp->data.u.str.len] = '\0';
625 node = ebmb_lookup_longest(&expr->pattern_tree, smp->data.u.str.str);
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +0200626 if (prev)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200627 smp->data.u.str.str[smp->data.u.str.len] = prev;
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +0200628
629 if (node) {
630 if (fill) {
631 elt = ebmb_entry(node, struct pattern_tree, node);
Thierry FOURNIER503bb092015-08-19 08:35:43 +0200632 static_pattern.data = elt->data;
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +0200633 static_pattern.ref = elt->ref;
634 static_pattern.sflags = PAT_SF_TREE;
635 static_pattern.type = SMP_T_STR;
636 static_pattern.ptr.str = (char *)elt->node.key;
637 }
638 return &static_pattern;
639 }
640 }
641
642 /* look in the list */
Willy Tarreauf3045d22015-04-29 16:24:50 +0200643 if (pat_lru_tree) {
Willy Tarreauaee93142015-05-04 17:18:42 +0200644 unsigned long long seed = pat_lru_seed ^ (long)expr;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200645
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200646 lru = lru64_get(XXH64(smp->data.u.str.str, smp->data.u.str.len, seed),
Willy Tarreauf3045d22015-04-29 16:24:50 +0200647 pat_lru_tree, expr, expr->revision);
648 if (lru && lru->domain)
649 return lru->data;
650 }
651
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100652 list_for_each_entry(lst, &expr->patterns, list) {
653 pattern = &lst->pat;
654
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200655 if (pattern->len > smp->data.u.str.len)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100656 continue;
657
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200658 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200659 if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.str.str, pattern->len) != 0) ||
660 (!icase && strncmp(pattern->ptr.str, smp->data.u.str.str, pattern->len) != 0))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100661 continue;
662
Willy Tarreauf3045d22015-04-29 16:24:50 +0200663 ret = pattern;
664 break;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100665 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200666
667 if (lru)
Christopher Fauletf90ac552015-06-09 17:06:17 +0200668 lru64_commit(lru, ret, expr, expr->revision, NULL);
Willy Tarreauf3045d22015-04-29 16:24:50 +0200669
670 return ret;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100671}
672
673/* Checks that the pattern matches the end of the tested string. */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100674struct pattern *pat_match_end(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100675{
676 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100677 struct pattern_list *lst;
678 struct pattern *pattern;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200679 struct pattern *ret = NULL;
680 struct lru64 *lru = NULL;
681
682 if (pat_lru_tree) {
Willy Tarreauaee93142015-05-04 17:18:42 +0200683 unsigned long long seed = pat_lru_seed ^ (long)expr;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200684
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200685 lru = lru64_get(XXH64(smp->data.u.str.str, smp->data.u.str.len, seed),
Willy Tarreauf3045d22015-04-29 16:24:50 +0200686 pat_lru_tree, expr, expr->revision);
687 if (lru && lru->domain)
688 return lru->data;
689 }
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100690
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100691 list_for_each_entry(lst, &expr->patterns, list) {
692 pattern = &lst->pat;
693
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200694 if (pattern->len > smp->data.u.str.len)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100695 continue;
696
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200697 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200698 if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.str.str + smp->data.u.str.len - pattern->len, pattern->len) != 0) ||
699 (!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 +0100700 continue;
701
Willy Tarreauf3045d22015-04-29 16:24:50 +0200702 ret = pattern;
703 break;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100704 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200705
706 if (lru)
Christopher Fauletf90ac552015-06-09 17:06:17 +0200707 lru64_commit(lru, ret, expr, expr->revision, NULL);
Willy Tarreauf3045d22015-04-29 16:24:50 +0200708
709 return ret;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100710}
711
712/* Checks that the pattern is included inside the tested string.
713 * NB: Suboptimal, should be rewritten using a Boyer-Moore method.
714 */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100715struct pattern *pat_match_sub(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100716{
717 int icase;
718 char *end;
719 char *c;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100720 struct pattern_list *lst;
721 struct pattern *pattern;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200722 struct pattern *ret = NULL;
723 struct lru64 *lru = NULL;
724
725 if (pat_lru_tree) {
Willy Tarreauaee93142015-05-04 17:18:42 +0200726 unsigned long long seed = pat_lru_seed ^ (long)expr;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200727
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200728 lru = lru64_get(XXH64(smp->data.u.str.str, smp->data.u.str.len, seed),
Willy Tarreauf3045d22015-04-29 16:24:50 +0200729 pat_lru_tree, expr, expr->revision);
730 if (lru && lru->domain)
731 return lru->data;
732 }
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100733
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100734 list_for_each_entry(lst, &expr->patterns, list) {
735 pattern = &lst->pat;
736
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200737 if (pattern->len > smp->data.u.str.len)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100738 continue;
739
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200740 end = smp->data.u.str.str + smp->data.u.str.len - pattern->len;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200741 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100742 if (icase) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200743 for (c = smp->data.u.str.str; c <= end; c++) {
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100744 if (tolower(*c) != tolower(*pattern->ptr.str))
745 continue;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200746 if (strncasecmp(pattern->ptr.str, c, pattern->len) == 0) {
747 ret = pattern;
748 goto leave;
749 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100750 }
751 } else {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200752 for (c = smp->data.u.str.str; c <= end; c++) {
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100753 if (*c != *pattern->ptr.str)
754 continue;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200755 if (strncmp(pattern->ptr.str, c, pattern->len) == 0) {
756 ret = pattern;
757 goto leave;
758 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100759 }
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100760 }
761 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200762 leave:
763 if (lru)
Christopher Fauletf90ac552015-06-09 17:06:17 +0200764 lru64_commit(lru, ret, expr, expr->revision, NULL);
Willy Tarreauf3045d22015-04-29 16:24:50 +0200765
766 return ret;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100767}
768
769/* This one is used by other real functions. It checks that the pattern is
770 * included inside the tested string, but enclosed between the specified
771 * delimiters or at the beginning or end of the string. The delimiters are
772 * provided as an unsigned int made by make_4delim() and match up to 4 different
773 * delimiters. Delimiters are stripped at the beginning and end of the pattern.
774 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200775static int match_word(struct sample *smp, struct pattern *pattern, int mflags, unsigned int delimiters)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100776{
777 int may_match, icase;
778 char *c, *end;
779 char *ps;
780 int pl;
781
782 pl = pattern->len;
783 ps = pattern->ptr.str;
784
785 while (pl > 0 && is_delimiter(*ps, delimiters)) {
786 pl--;
787 ps++;
788 }
789
790 while (pl > 0 && is_delimiter(ps[pl - 1], delimiters))
791 pl--;
792
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200793 if (pl > smp->data.u.str.len)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100794 return PAT_NOMATCH;
795
796 may_match = 1;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200797 icase = mflags & PAT_MF_IGNORE_CASE;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200798 end = smp->data.u.str.str + smp->data.u.str.len - pl;
799 for (c = smp->data.u.str.str; c <= end; c++) {
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100800 if (is_delimiter(*c, delimiters)) {
801 may_match = 1;
802 continue;
803 }
804
805 if (!may_match)
806 continue;
807
808 if (icase) {
809 if ((tolower(*c) == tolower(*ps)) &&
810 (strncasecmp(ps, c, pl) == 0) &&
811 (c == end || is_delimiter(c[pl], delimiters)))
812 return PAT_MATCH;
813 } else {
814 if ((*c == *ps) &&
815 (strncmp(ps, c, pl) == 0) &&
816 (c == end || is_delimiter(c[pl], delimiters)))
817 return PAT_MATCH;
818 }
819 may_match = 0;
820 }
821 return PAT_NOMATCH;
822}
823
824/* Checks that the pattern is included inside the tested string, but enclosed
825 * between the delimiters '?' or '/' or at the beginning or end of the string.
826 * Delimiters at the beginning or end of the pattern are ignored.
827 */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100828struct pattern *pat_match_dir(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 FOURNIERe47e4e22014-04-28 11:18:57 +0200835 if (match_word(smp, pattern, expr->mflags, make_4delim('/', '?', '?', '?')))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100836 return pattern;
837 }
838 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100839}
840
841/* Checks that the pattern is included inside the tested string, but enclosed
842 * between the delmiters '/', '?', '.' or ":" or at the beginning or end of
843 * the string. Delimiters at the beginning or end of the pattern are ignored.
844 */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100845struct pattern *pat_match_dom(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100846{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100847 struct pattern_list *lst;
848 struct pattern *pattern;
849
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100850 list_for_each_entry(lst, &expr->patterns, list) {
851 pattern = &lst->pat;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200852 if (match_word(smp, pattern, expr->mflags, make_4delim('/', '?', '.', ':')))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100853 return pattern;
854 }
855 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100856}
857
858/* Checks that the integer in <test> is included between min and max */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100859struct pattern *pat_match_int(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100860{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100861 struct pattern_list *lst;
862 struct pattern *pattern;
863
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100864 list_for_each_entry(lst, &expr->patterns, list) {
865 pattern = &lst->pat;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200866 if ((!pattern->val.range.min_set || pattern->val.range.min <= smp->data.u.sint) &&
867 (!pattern->val.range.max_set || smp->data.u.sint <= pattern->val.range.max))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100868 return pattern;
869 }
870 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100871}
872
873/* Checks that the length of the pattern in <test> is included between min and max */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100874struct pattern *pat_match_len(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100875{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100876 struct pattern_list *lst;
877 struct pattern *pattern;
878
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100879 list_for_each_entry(lst, &expr->patterns, list) {
880 pattern = &lst->pat;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200881 if ((!pattern->val.range.min_set || pattern->val.range.min <= smp->data.u.str.len) &&
882 (!pattern->val.range.max_set || smp->data.u.str.len <= pattern->val.range.max))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100883 return pattern;
884 }
885 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100886}
887
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100888struct pattern *pat_match_ip(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100889{
890 unsigned int v4; /* in network byte order */
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100891 struct in6_addr tmp6;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100892 struct in_addr *s;
893 struct ebmb_node *node;
894 struct pattern_tree *elt;
895 struct pattern_list *lst;
896 struct pattern *pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100897
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100898 /* The input sample is IPv4. Try to match in the trees. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200899 if (smp->data.type == SMP_T_IPV4) {
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100900 /* Lookup an IPv4 address in the expression's pattern tree using
901 * the longest match method.
902 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200903 s = &smp->data.u.ipv4;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100904 node = ebmb_lookup_longest(&expr->pattern_tree, &s->s_addr);
905 if (node) {
906 if (fill) {
907 elt = ebmb_entry(node, struct pattern_tree, node);
Thierry FOURNIER503bb092015-08-19 08:35:43 +0200908 static_pattern.data = elt->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +0100909 static_pattern.ref = elt->ref;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200910 static_pattern.sflags = PAT_SF_TREE;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100911 static_pattern.type = SMP_T_IPV4;
912 memcpy(&static_pattern.val.ipv4.addr.s_addr, elt->node.key, 4);
913 if (!cidr2dotted(elt->node.node.pfx, &static_pattern.val.ipv4.mask))
914 return NULL;
915 }
916 return &static_pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100917 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100918
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100919 /* The IPv4 sample dont match the IPv4 tree. Convert the IPv4
920 * sample address to IPv6 with the mapping method using the ::ffff:
921 * prefix, and try to lookup in the IPv6 tree.
922 */
923 memset(&tmp6, 0, 10);
924 *(uint16_t*)&tmp6.s6_addr[10] = htons(0xffff);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200925 *(uint32_t*)&tmp6.s6_addr[12] = smp->data.u.ipv4.s_addr;
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100926 node = ebmb_lookup_longest(&expr->pattern_tree_2, &tmp6);
927 if (node) {
928 if (fill) {
929 elt = ebmb_entry(node, struct pattern_tree, node);
Thierry FOURNIER503bb092015-08-19 08:35:43 +0200930 static_pattern.data = elt->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +0100931 static_pattern.ref = elt->ref;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200932 static_pattern.sflags = PAT_SF_TREE;
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100933 static_pattern.type = SMP_T_IPV6;
934 memcpy(&static_pattern.val.ipv6.addr, elt->node.key, 16);
935 static_pattern.val.ipv6.mask = elt->node.node.pfx;
936 }
937 return &static_pattern;
938 }
939 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100940
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100941 /* The input sample is IPv6. Try to match in the trees. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200942 if (smp->data.type == SMP_T_IPV6) {
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100943 /* Lookup an IPv6 address in the expression's pattern tree using
944 * the longest match method.
945 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200946 node = ebmb_lookup_longest(&expr->pattern_tree_2, &smp->data.u.ipv6);
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100947 if (node) {
948 if (fill) {
949 elt = ebmb_entry(node, struct pattern_tree, node);
Thierry FOURNIER503bb092015-08-19 08:35:43 +0200950 static_pattern.data = elt->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +0100951 static_pattern.ref = elt->ref;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200952 static_pattern.sflags = PAT_SF_TREE;
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100953 static_pattern.type = SMP_T_IPV6;
954 memcpy(&static_pattern.val.ipv6.addr, elt->node.key, 16);
955 static_pattern.val.ipv6.mask = elt->node.node.pfx;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100956 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100957 return &static_pattern;
958 }
959
960 /* Try to convert 6 to 4 when the start of the ipv6 address match the
961 * following forms :
962 * - ::ffff:ip:v4 (ipv4 mapped)
963 * - ::0000:ip:v4 (old ipv4 mapped)
964 * - 2002:ip:v4:: (6to4)
965 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200966 if ((*(uint32_t*)&smp->data.u.ipv6.s6_addr[0] == 0 &&
967 *(uint32_t*)&smp->data.u.ipv6.s6_addr[4] == 0 &&
968 (*(uint32_t*)&smp->data.u.ipv6.s6_addr[8] == 0 ||
969 *(uint32_t*)&smp->data.u.ipv6.s6_addr[8] == htonl(0xFFFF))) ||
970 *(uint16_t*)&smp->data.u.ipv6.s6_addr[0] == htons(0x2002)) {
971 if (*(uint32_t*)&smp->data.u.ipv6.s6_addr[0] == 0)
972 v4 = *(uint32_t*)&smp->data.u.ipv6.s6_addr[12];
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100973 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200974 v4 = htonl((ntohs(*(uint16_t*)&smp->data.u.ipv6.s6_addr[2]) << 16) +
975 ntohs(*(uint16_t*)&smp->data.u.ipv6.s6_addr[4]));
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100976
977 /* Lookup an IPv4 address in the expression's pattern tree using the longest
978 * match method.
979 */
980 node = ebmb_lookup_longest(&expr->pattern_tree, &v4);
981 if (node) {
982 if (fill) {
983 elt = ebmb_entry(node, struct pattern_tree, node);
Thierry FOURNIER503bb092015-08-19 08:35:43 +0200984 static_pattern.data = elt->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +0100985 static_pattern.ref = elt->ref;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200986 static_pattern.sflags = PAT_SF_TREE;
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100987 static_pattern.type = SMP_T_IPV4;
988 memcpy(&static_pattern.val.ipv4.addr.s_addr, elt->node.key, 4);
989 if (!cidr2dotted(elt->node.node.pfx, &static_pattern.val.ipv4.mask))
990 return NULL;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100991 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100992 return &static_pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100993 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100994 }
995 }
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100996
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100997 /* Lookup in the list. the list contain only IPv4 patterns */
998 list_for_each_entry(lst, &expr->patterns, list) {
999 pattern = &lst->pat;
1000
1001 /* The input sample is IPv4, use it as is. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001002 if (smp->data.type == SMP_T_IPV4) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001003 v4 = smp->data.u.ipv4.s_addr;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +01001004 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001005 else if (smp->data.type == SMP_T_IPV6) {
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001006 /* v4 match on a V6 sample. We want to check at least for
1007 * the following forms :
1008 * - ::ffff:ip:v4 (ipv4 mapped)
1009 * - ::0000:ip:v4 (old ipv4 mapped)
1010 * - 2002:ip:v4:: (6to4)
1011 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001012 if (*(uint32_t*)&smp->data.u.ipv6.s6_addr[0] == 0 &&
1013 *(uint32_t*)&smp->data.u.ipv6.s6_addr[4] == 0 &&
1014 (*(uint32_t*)&smp->data.u.ipv6.s6_addr[8] == 0 ||
1015 *(uint32_t*)&smp->data.u.ipv6.s6_addr[8] == htonl(0xFFFF))) {
1016 v4 = *(uint32_t*)&smp->data.u.ipv6.s6_addr[12];
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01001017 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001018 else if (*(uint16_t*)&smp->data.u.ipv6.s6_addr[0] == htons(0x2002)) {
1019 v4 = htonl((ntohs(*(uint16_t*)&smp->data.u.ipv6.s6_addr[2]) << 16) +
1020 ntohs(*(uint16_t*)&smp->data.u.ipv6.s6_addr[4]));
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01001021 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001022 else
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01001023 continue;
Andreas Seltenreichf0653192016-03-03 20:08:35 +01001024 } else {
1025 /* impossible */
1026 continue;
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001027 }
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +01001028
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001029 /* Check if the input sample match the current pattern. */
1030 if (((v4 ^ pattern->val.ipv4.addr.s_addr) & pattern->val.ipv4.mask.s_addr) == 0)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01001031 return pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +01001032 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01001033 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +01001034}
1035
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001036void free_pattern_tree(struct eb_root *root)
1037{
1038 struct eb_node *node, *next;
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +01001039 struct pattern_tree *elt;
Thierry FOURNIER3ce88c72013-12-09 11:29:46 +01001040
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001041 node = eb_first(root);
1042 while (node) {
1043 next = eb_next(node);
1044 eb_delete(node);
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +01001045 elt = container_of(node, struct pattern_tree, node);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001046 free(elt->data);
Thierry FOURNIER3ce88c72013-12-09 11:29:46 +01001047 free(elt);
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001048 node = next;
1049 }
1050}
1051
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01001052void pat_prune_val(struct pattern_expr *expr)
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +01001053{
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01001054 struct pattern_list *pat, *tmp;
1055
1056 list_for_each_entry_safe(pat, tmp, &expr->patterns, list) {
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001057 free(pat->pat.data);
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01001058 free(pat);
1059 }
1060
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +01001061 free_pattern_tree(&expr->pattern_tree);
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001062 free_pattern_tree(&expr->pattern_tree_2);
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +01001063 LIST_INIT(&expr->patterns);
1064}
1065
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01001066void pat_prune_ptr(struct pattern_expr *expr)
1067{
1068 struct pattern_list *pat, *tmp;
1069
1070 list_for_each_entry_safe(pat, tmp, &expr->patterns, list) {
1071 free(pat->pat.ptr.ptr);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001072 free(pat->pat.data);
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01001073 free(pat);
1074 }
1075
1076 free_pattern_tree(&expr->pattern_tree);
1077 free_pattern_tree(&expr->pattern_tree_2);
1078 LIST_INIT(&expr->patterns);
1079}
1080
1081void pat_prune_reg(struct pattern_expr *expr)
1082{
1083 struct pattern_list *pat, *tmp;
1084
1085 list_for_each_entry_safe(pat, tmp, &expr->patterns, list) {
1086 regex_free(pat->pat.ptr.ptr);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001087 free(pat->pat.data);
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01001088 free(pat);
1089 }
1090
1091 free_pattern_tree(&expr->pattern_tree);
1092 free_pattern_tree(&expr->pattern_tree_2);
1093 LIST_INIT(&expr->patterns);
1094}
1095
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001096/*
1097 *
1098 * The following functions are used for the pattern indexation
1099 *
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001100 */
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001101
1102int pat_idx_list_val(struct pattern_expr *expr, struct pattern *pat, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001103{
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001104 struct pattern_list *patl;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001105
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001106 /* allocate pattern */
1107 patl = calloc(1, sizeof(*patl));
1108 if (!patl) {
1109 memprintf(err, "out of memory while indexing pattern");
Thierry FOURNIER972028f2014-01-23 17:53:31 +01001110 return 0;
1111 }
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001112
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001113 /* duplicate pattern */
1114 memcpy(&patl->pat, pat, sizeof(*pat));
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001115
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001116 /* chain pattern in the expression */
1117 LIST_ADDQ(&expr->patterns, &patl->list);
Willy Tarreau72f073b2015-04-29 17:53:47 +02001118 expr->revision = rdtsc();
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001119
1120 /* that's ok */
1121 return 1;
1122}
1123
1124int pat_idx_list_ptr(struct pattern_expr *expr, struct pattern *pat, char **err)
1125{
1126 struct pattern_list *patl;
1127
1128 /* allocate pattern */
1129 patl = calloc(1, sizeof(*patl));
Thierry FOURNIER8aa83842015-02-06 17:50:55 +01001130 if (!patl) {
1131 memprintf(err, "out of memory while indexing pattern");
Thierry FOURNIER972028f2014-01-23 17:53:31 +01001132 return 0;
Thierry FOURNIER8aa83842015-02-06 17:50:55 +01001133 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001134
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001135 /* duplicate pattern */
1136 memcpy(&patl->pat, pat, sizeof(*pat));
1137 patl->pat.ptr.ptr = malloc(patl->pat.len);
1138 if (!patl->pat.ptr.ptr) {
1139 free(patl);
1140 memprintf(err, "out of memory while indexing pattern");
1141 return 0;
1142 }
1143 memcpy(patl->pat.ptr.ptr, pat->ptr.ptr, pat->len);
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001144
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001145 /* chain pattern in the expression */
1146 LIST_ADDQ(&expr->patterns, &patl->list);
Willy Tarreau72f073b2015-04-29 17:53:47 +02001147 expr->revision = rdtsc();
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001148
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001149 /* that's ok */
1150 return 1;
1151}
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001152
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001153int pat_idx_list_str(struct pattern_expr *expr, struct pattern *pat, char **err)
1154{
1155 struct pattern_list *patl;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001156
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001157 /* allocate pattern */
1158 patl = calloc(1, sizeof(*patl));
1159 if (!patl) {
1160 memprintf(err, "out of memory while indexing pattern");
1161 return 0;
1162 }
1163
1164 /* duplicate pattern */
1165 memcpy(&patl->pat, pat, sizeof(*pat));
1166 patl->pat.ptr.str = malloc(patl->pat.len + 1);
1167 if (!patl->pat.ptr.str) {
1168 free(patl);
1169 memprintf(err, "out of memory while indexing pattern");
1170 return 0;
1171 }
1172 memcpy(patl->pat.ptr.ptr, pat->ptr.ptr, pat->len);
1173 patl->pat.ptr.str[patl->pat.len] = '\0';
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001174
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001175 /* chain pattern in the expression */
1176 LIST_ADDQ(&expr->patterns, &patl->list);
Willy Tarreau72f073b2015-04-29 17:53:47 +02001177 expr->revision = rdtsc();
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001178
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001179 /* that's ok */
1180 return 1;
1181}
1182
Thierry Fournier8feaa662016-02-10 22:55:20 +01001183int pat_idx_list_reg_cap(struct pattern_expr *expr, struct pattern *pat, int cap, char **err)
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001184{
1185 struct pattern_list *patl;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001186
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001187 /* allocate pattern */
1188 patl = calloc(1, sizeof(*patl));
1189 if (!patl) {
1190 memprintf(err, "out of memory while indexing pattern");
1191 return 0;
Thierry FOURNIER972028f2014-01-23 17:53:31 +01001192 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001193
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001194 /* duplicate pattern */
1195 memcpy(&patl->pat, pat, sizeof(*pat));
1196
1197 /* allocate regex */
1198 patl->pat.ptr.reg = calloc(1, sizeof(*patl->pat.ptr.reg));
1199 if (!patl->pat.ptr.reg) {
1200 free(patl);
1201 memprintf(err, "out of memory while indexing pattern");
1202 return 0;
1203 }
1204
1205 /* compile regex */
Thierry Fournier8feaa662016-02-10 22:55:20 +01001206 if (!regex_comp(pat->ptr.str, patl->pat.ptr.reg,
1207 !(expr->mflags & PAT_MF_IGNORE_CASE), cap, err)) {
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001208 free(patl->pat.ptr.reg);
Dirkjan Bussink07fcaaa2014-04-28 22:57:16 +00001209 free(patl);
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001210 return 0;
1211 }
1212
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001213 /* chain pattern in the expression */
1214 LIST_ADDQ(&expr->patterns, &patl->list);
Willy Tarreau72f073b2015-04-29 17:53:47 +02001215 expr->revision = rdtsc();
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001216
1217 /* that's ok */
1218 return 1;
1219}
1220
Thierry Fournier8feaa662016-02-10 22:55:20 +01001221int pat_idx_list_reg(struct pattern_expr *expr, struct pattern *pat, char **err)
1222{
1223 return pat_idx_list_reg_cap(expr, pat, 0, err);
1224}
1225
1226int pat_idx_list_regm(struct pattern_expr *expr, struct pattern *pat, char **err)
1227{
1228 return pat_idx_list_reg_cap(expr, pat, 1, err);
1229}
1230
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001231int pat_idx_tree_ip(struct pattern_expr *expr, struct pattern *pat, char **err)
1232{
1233 unsigned int mask;
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +01001234 struct pattern_tree *node;
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001235
1236 /* Only IPv4 can be indexed */
1237 if (pat->type == SMP_T_IPV4) {
Thierry FOURNIER972028f2014-01-23 17:53:31 +01001238 /* in IPv4 case, check if the mask is contiguous so that we can
1239 * insert the network into the tree. A continuous mask has only
1240 * ones on the left. This means that this mask + its lower bit
1241 * added once again is null.
1242 */
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001243 mask = ntohl(pat->val.ipv4.mask.s_addr);
1244 if (mask + (mask & -mask) == 0) {
1245 mask = mask ? 33 - flsnz(mask & -mask) : 0; /* equals cidr value */
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001246
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001247 /* node memory allocation */
1248 node = calloc(1, sizeof(*node) + 4);
1249 if (!node) {
1250 memprintf(err, "out of memory while loading pattern");
1251 return 0;
1252 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001253
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001254 /* copy the pointer to sample associated to this node */
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001255 node->data = pat->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01001256 node->ref = pat->ref;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001257
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001258 /* FIXME: insert <addr>/<mask> into the tree here */
1259 memcpy(node->node.key, &pat->val.ipv4.addr, 4); /* network byte order */
1260 node->node.node.pfx = mask;
Thierry FOURNIER31db4ae2014-01-30 00:27:15 +01001261
1262 /* Insert the entry. */
1263 ebmb_insert_prefix(&expr->pattern_tree, &node->node, 4);
Willy Tarreau72f073b2015-04-29 17:53:47 +02001264 expr->revision = rdtsc();
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001265
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001266 /* that's ok */
1267 return 1;
1268 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001269 else {
1270 /* If the mask is not contiguous, just add the pattern to the list */
1271 return pat_idx_list_val(expr, pat, err);
1272 }
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001273 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001274 else if (pat->type == SMP_T_IPV6) {
1275 /* IPv6 also can be indexed */
1276 node = calloc(1, sizeof(*node) + 16);
1277 if (!node) {
1278 memprintf(err, "out of memory while loading pattern");
1279 return 0;
1280 }
1281
1282 /* 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 FOURNIER33a74332013-12-19 23:54:54 +01001285
1286 /* FIXME: insert <addr>/<mask> into the tree here */
1287 memcpy(node->node.key, &pat->val.ipv6.addr, 16); /* network byte order */
1288 node->node.node.pfx = pat->val.ipv6.mask;
Thierry FOURNIER31db4ae2014-01-30 00:27:15 +01001289
1290 /* Insert the entry. */
1291 ebmb_insert_prefix(&expr->pattern_tree_2, &node->node, 16);
Willy Tarreau72f073b2015-04-29 17:53:47 +02001292 expr->revision = rdtsc();
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001293
1294 /* that's ok */
1295 return 1;
1296 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001297
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001298 return 0;
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001299}
1300
1301int pat_idx_tree_str(struct pattern_expr *expr, struct pattern *pat, char **err)
1302{
1303 int len;
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +01001304 struct pattern_tree *node;
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001305
1306 /* Only string can be indexed */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001307 if (pat->type != SMP_T_STR) {
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001308 memprintf(err, "internal error: string expected, but the type is '%s'",
1309 smp_to_type[pat->type]);
1310 return 0;
Thierry FOURNIER972028f2014-01-23 17:53:31 +01001311 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001312
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001313 /* If the flag PAT_F_IGNORE_CASE is set, we cannot use trees */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02001314 if (expr->mflags & PAT_MF_IGNORE_CASE)
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001315 return pat_idx_list_str(expr, pat, err);
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001316
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001317 /* Process the key len */
1318 len = strlen(pat->ptr.str) + 1;
1319
1320 /* node memory allocation */
1321 node = calloc(1, sizeof(*node) + len);
1322 if (!node) {
1323 memprintf(err, "out of memory while loading pattern");
1324 return 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001325 }
1326
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001327 /* copy the pointer to sample associated to this node */
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001328 node->data = pat->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01001329 node->ref = pat->ref;
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001330
1331 /* copy the string */
1332 memcpy(node->node.key, pat->ptr.str, len);
1333
1334 /* index the new node */
Thierry FOURNIER31db4ae2014-01-30 00:27:15 +01001335 ebst_insert(&expr->pattern_tree, &node->node);
Willy Tarreau72f073b2015-04-29 17:53:47 +02001336 expr->revision = rdtsc();
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001337
1338 /* that's ok */
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001339 return 1;
1340}
1341
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +02001342int pat_idx_tree_pfx(struct pattern_expr *expr, struct pattern *pat, char **err)
1343{
1344 int len;
1345 struct pattern_tree *node;
1346
1347 /* Only string can be indexed */
1348 if (pat->type != SMP_T_STR) {
1349 memprintf(err, "internal error: string expected, but the type is '%s'",
1350 smp_to_type[pat->type]);
1351 return 0;
1352 }
1353
1354 /* If the flag PAT_F_IGNORE_CASE is set, we cannot use trees */
1355 if (expr->mflags & PAT_MF_IGNORE_CASE)
1356 return pat_idx_list_str(expr, pat, err);
1357
1358 /* Process the key len */
1359 len = strlen(pat->ptr.str);
1360
1361 /* node memory allocation */
1362 node = calloc(1, sizeof(*node) + len + 1);
1363 if (!node) {
1364 memprintf(err, "out of memory while loading pattern");
1365 return 0;
1366 }
1367
1368 /* copy the pointer to sample associated to this node */
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001369 node->data = pat->data;
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +02001370 node->ref = pat->ref;
1371
1372 /* copy the string and the trailing zero */
1373 memcpy(node->node.key, pat->ptr.str, len + 1);
1374 node->node.node.pfx = len * 8;
1375
1376 /* index the new node */
1377 ebmb_insert_prefix(&expr->pattern_tree, &node->node, len);
Willy Tarreau72f073b2015-04-29 17:53:47 +02001378 expr->revision = rdtsc();
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +02001379
1380 /* that's ok */
1381 return 1;
1382}
1383
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001384void pat_del_list_val(struct pattern_expr *expr, struct pat_ref_elt *ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001385{
1386 struct pattern_list *pat;
1387 struct pattern_list *safe;
1388
1389 list_for_each_entry_safe(pat, safe, &expr->patterns, list) {
1390 /* Check equality. */
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001391 if (pat->pat.ref != ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001392 continue;
1393
1394 /* Delete and free entry. */
1395 LIST_DEL(&pat->list);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001396 free(pat->pat.data);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001397 free(pat);
1398 }
Willy Tarreau72f073b2015-04-29 17:53:47 +02001399 expr->revision = rdtsc();
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001400}
1401
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001402void pat_del_tree_ip(struct pattern_expr *expr, struct pat_ref_elt *ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001403{
1404 struct ebmb_node *node, *next_node;
1405 struct pattern_tree *elt;
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001406
1407 /* browse each node of the tree for IPv4 addresses. */
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001408 for (node = ebmb_first(&expr->pattern_tree), next_node = node ? ebmb_next(node) : NULL;
1409 node;
1410 node = next_node, next_node = node ? ebmb_next(node) : NULL) {
1411 /* Extract container of the tree node. */
1412 elt = container_of(node, struct pattern_tree, node);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001413
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001414 /* Check equality. */
1415 if (elt->ref != ref)
1416 continue;
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001417
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001418 /* Delete and free entry. */
1419 ebmb_delete(node);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001420 free(elt->data);
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001421 free(elt);
1422 }
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001423
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001424 /* Browse each node of the list for IPv4 addresses. */
1425 pat_del_list_val(expr, ref);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001426
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001427 /* browse each node of the tree for IPv6 addresses. */
1428 for (node = ebmb_first(&expr->pattern_tree_2), next_node = node ? ebmb_next(node) : NULL;
1429 node;
1430 node = next_node, next_node = node ? ebmb_next(node) : NULL) {
1431 /* Extract container of the tree node. */
1432 elt = container_of(node, struct pattern_tree, node);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001433
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001434 /* Check equality. */
1435 if (elt->ref != ref)
1436 continue;
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001437
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001438 /* Delete and free entry. */
1439 ebmb_delete(node);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001440 free(elt->data);
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001441 free(elt);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001442 }
Willy Tarreau72f073b2015-04-29 17:53:47 +02001443 expr->revision = rdtsc();
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001444}
1445
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001446void pat_del_list_ptr(struct pattern_expr *expr, struct pat_ref_elt *ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001447{
1448 struct pattern_list *pat;
1449 struct pattern_list *safe;
1450
1451 list_for_each_entry_safe(pat, safe, &expr->patterns, list) {
1452 /* Check equality. */
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001453 if (pat->pat.ref != ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001454 continue;
1455
1456 /* Delete and free entry. */
1457 LIST_DEL(&pat->list);
1458 free(pat->pat.ptr.ptr);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001459 free(pat->pat.data);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001460 free(pat);
1461 }
Willy Tarreau72f073b2015-04-29 17:53:47 +02001462 expr->revision = rdtsc();
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001463}
1464
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001465void pat_del_tree_str(struct pattern_expr *expr, struct pat_ref_elt *ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001466{
1467 struct ebmb_node *node, *next_node;
1468 struct pattern_tree *elt;
1469
Thierry FOURNIER73bc2852015-02-06 17:53:54 +01001470 /* If the flag PAT_F_IGNORE_CASE is set, we cannot use trees */
1471 if (expr->mflags & PAT_MF_IGNORE_CASE)
1472 return pat_del_list_ptr(expr, ref);
1473
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001474 /* browse each node of the tree. */
1475 for (node = ebmb_first(&expr->pattern_tree), next_node = node ? ebmb_next(node) : NULL;
1476 node;
1477 node = next_node, next_node = node ? ebmb_next(node) : NULL) {
1478 /* Extract container of the tree node. */
1479 elt = container_of(node, struct pattern_tree, node);
1480
1481 /* Check equality. */
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001482 if (elt->ref != ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001483 continue;
1484
1485 /* Delete and free entry. */
1486 ebmb_delete(node);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001487 free(elt->data);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001488 free(elt);
1489 }
Willy Tarreau72f073b2015-04-29 17:53:47 +02001490 expr->revision = rdtsc();
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001491}
1492
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001493void pat_del_list_reg(struct pattern_expr *expr, struct pat_ref_elt *ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001494{
1495 struct pattern_list *pat;
1496 struct pattern_list *safe;
1497
1498 list_for_each_entry_safe(pat, safe, &expr->patterns, list) {
1499 /* Check equality. */
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001500 if (pat->pat.ref != ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001501 continue;
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001502
1503 /* Delete and free entry. */
1504 LIST_DEL(&pat->list);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001505 regex_free(pat->pat.ptr.ptr);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001506 free(pat->pat.data);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001507 free(pat);
1508 }
Willy Tarreau72f073b2015-04-29 17:53:47 +02001509 expr->revision = rdtsc();
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001510}
1511
1512void pattern_init_expr(struct pattern_expr *expr)
1513{
1514 LIST_INIT(&expr->patterns);
Willy Tarreau72f073b2015-04-29 17:53:47 +02001515 expr->revision = 0;
Thierry FOURNIER31db4ae2014-01-30 00:27:15 +01001516 expr->pattern_tree = EB_ROOT;
1517 expr->pattern_tree_2 = EB_ROOT;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001518}
1519
1520void pattern_init_head(struct pattern_head *head)
1521{
1522 LIST_INIT(&head->head);
1523}
1524
1525/* The following functions are relative to the management of the reference
1526 * lists. These lists are used to store the original pattern and associated
1527 * value as string form.
1528 *
1529 * This is used with modifiable ACL and MAPS
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001530 *
1531 * The pattern reference are stored with two identifiers: the unique_id and
1532 * the reference.
1533 *
1534 * The reference identify a file. Each file with the same name point to the
1535 * same reference. We can register many times one file. If the file is modified,
1536 * all his dependencies are also modified. The reference can be used with map or
1537 * acl.
1538 *
1539 * The unique_id identify inline acl. The unique id is unique for each acl.
1540 * You cannot force the same id in the configuration file, because this repoort
1541 * an error.
1542 *
1543 * A particular case appears if the filename is a number. In this case, the
1544 * unique_id is set with the number represented by the filename and the
1545 * reference is also set. This method prevent double unique_id.
1546 *
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001547 */
1548
1549/* This function lookup for reference. If the reference is found, they return
1550 * pointer to the struct pat_ref, else return NULL.
1551 */
1552struct pat_ref *pat_ref_lookup(const char *reference)
1553{
1554 struct pat_ref *ref;
1555
1556 list_for_each_entry(ref, &pattern_reference, list)
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001557 if (ref->reference && strcmp(reference, ref->reference) == 0)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001558 return ref;
1559 return NULL;
1560}
1561
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001562/* This function lookup for unique id. If the reference is found, they return
1563 * pointer to the struct pat_ref, else return NULL.
1564 */
1565struct pat_ref *pat_ref_lookupid(int unique_id)
1566{
1567 struct pat_ref *ref;
1568
1569 list_for_each_entry(ref, &pattern_reference, list)
1570 if (ref->unique_id == unique_id)
1571 return ref;
1572 return NULL;
1573}
1574
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001575/* This function remove all pattern matching the pointer <refelt> from
1576 * the the reference and from each expr member of the reference. This
1577 * function returns 1 if the deletion is done and return 0 is the entry
1578 * is not found.
1579 */
1580int pat_ref_delete_by_id(struct pat_ref *ref, struct pat_ref_elt *refelt)
1581{
1582 struct pattern_expr *expr;
1583 struct pat_ref_elt *elt, *safe;
1584
1585 /* delete pattern from reference */
1586 list_for_each_entry_safe(elt, safe, &ref->head, list) {
1587 if (elt == refelt) {
peter caiaede6dd2015-10-07 00:07:43 -07001588 list_for_each_entry(expr, &ref->pat, list)
1589 pattern_delete(expr, elt);
1590
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001591 LIST_DEL(&elt->list);
1592 free(elt->sample);
1593 free(elt->pattern);
1594 free(elt);
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001595 return 1;
1596 }
1597 }
1598 return 0;
1599}
1600
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001601/* This function remove all pattern match <key> from the the reference
1602 * and from each expr member of the reference. This fucntion returns 1
1603 * if the deletion is done and return 0 is the entry is not found.
1604 */
1605int pat_ref_delete(struct pat_ref *ref, const char *key)
1606{
1607 struct pattern_expr *expr;
1608 struct pat_ref_elt *elt, *safe;
1609 int found = 0;
1610
1611 /* delete pattern from reference */
1612 list_for_each_entry_safe(elt, safe, &ref->head, list) {
1613 if (strcmp(key, elt->pattern) == 0) {
Dirkjan Bussink07fcaaa2014-04-28 22:57:16 +00001614 list_for_each_entry(expr, &ref->pat, list)
1615 pattern_delete(expr, elt);
1616
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001617 LIST_DEL(&elt->list);
1618 free(elt->sample);
1619 free(elt->pattern);
1620 free(elt);
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001621
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001622 found = 1;
1623 }
1624 }
1625
1626 if (!found)
1627 return 0;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001628 return 1;
1629}
1630
Baptiste Assmann953f74d2014-04-25 16:57:03 +02001631/*
1632 * find and return an element <elt> matching <key> in a reference <ref>
1633 * return NULL if not found
1634 */
1635struct pat_ref_elt *pat_ref_find_elt(struct pat_ref *ref, const char *key)
1636{
1637 struct pat_ref_elt *elt;
1638
1639 list_for_each_entry(elt, &ref->head, list) {
1640 if (strcmp(key, elt->pattern) == 0)
1641 return elt;
1642 }
1643
1644 return NULL;
1645}
1646
1647
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001648 /* This function modify the sample of the first pattern that match the <key>. */
1649static inline int pat_ref_set_elt(struct pat_ref *ref, struct pat_ref_elt *elt,
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001650 const char *value, char **err)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001651{
1652 struct pattern_expr *expr;
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001653 struct sample_data **data;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001654 char *sample;
Thierry FOURNIER12ba0c22015-08-14 00:02:11 +02001655 struct sample_data test;
Thierry FOURNIER149e0fe2014-01-29 19:35:06 +01001656
1657 /* Try all needed converters. */
1658 list_for_each_entry(expr, &ref->pat, list) {
1659 if (!expr->pat_head->parse_smp)
1660 continue;
1661
1662 if (!expr->pat_head->parse_smp(value, &test)) {
1663 memprintf(err, "unable to parse '%s'", value);
1664 return 0;
1665 }
1666 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001667
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001668 /* Modify pattern from reference. */
1669 sample = strdup(value);
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001670 if (!sample) {
1671 memprintf(err, "out of memory error");
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001672 return 0;
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001673 }
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001674 free(elt->sample);
1675 elt->sample = sample;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001676
Thierry FOURNIER149e0fe2014-01-29 19:35:06 +01001677 /* Load sample in each reference. All the conversion are tested
1678 * below, normally these calls dosn't fail.
1679 */
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001680 list_for_each_entry(expr, &ref->pat, list) {
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001681 if (!expr->pat_head->parse_smp)
1682 continue;
1683
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001684 data = pattern_find_smp(expr, elt);
1685 if (data && *data && !expr->pat_head->parse_smp(sample, *data))
1686 *data = NULL;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001687 }
1688
Thierry FOURNIER149e0fe2014-01-29 19:35:06 +01001689 return 1;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001690}
1691
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001692/* This function modify the sample of the first pattern that match the <key>. */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001693int 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 +01001694{
1695 struct pat_ref_elt *elt;
1696
1697 /* Look for pattern in the reference. */
1698 list_for_each_entry(elt, &ref->head, list) {
1699 if (elt == refelt) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001700 if (!pat_ref_set_elt(ref, elt, value, err))
1701 return 0;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001702 return 1;
1703 }
1704 }
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001705
1706 memprintf(err, "key or pattern not found");
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001707 return 0;
1708}
1709
1710/* This function modify the sample of the first pattern that match the <key>. */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001711int pat_ref_set(struct pat_ref *ref, const char *key, const char *value, char **err)
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001712{
1713 struct pat_ref_elt *elt;
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001714 int found = 0;
1715 char *_merr;
1716 char **merr;
1717
1718 if (err) {
1719 merr = &_merr;
1720 *merr = NULL;
1721 }
1722 else
1723 merr = NULL;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001724
1725 /* Look for pattern in the reference. */
1726 list_for_each_entry(elt, &ref->head, list) {
1727 if (strcmp(key, elt->pattern) == 0) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001728 if (!pat_ref_set_elt(ref, elt, value, merr)) {
1729 if (!found)
1730 *err = *merr;
1731 else {
1732 memprintf(err, "%s, %s", *err, *merr);
1733 free(*merr);
1734 *merr = NULL;
1735 }
1736 }
1737 found = 1;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001738 }
1739 }
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001740
1741 if (!found) {
1742 memprintf(err, "entry not found");
1743 return 0;
1744 }
1745 return 1;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001746}
1747
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001748/* This function create new reference. <ref> is the reference name.
1749 * <flags> are PAT_REF_*. /!\ The reference is not checked, and must
1750 * be unique. The user must check the reference with "pat_ref_lookup()"
1751 * before calling this function. If the fucntion fail, it return NULL,
1752 * else return new struct pat_ref.
1753 */
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001754struct pat_ref *pat_ref_new(const char *reference, const char *display, unsigned int flags)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001755{
1756 struct pat_ref *ref;
1757
1758 ref = malloc(sizeof(*ref));
1759 if (!ref)
1760 return NULL;
1761
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001762 if (display) {
1763 ref->display = strdup(display);
1764 if (!ref->display) {
1765 free(ref);
1766 return NULL;
1767 }
1768 }
1769 else
1770 ref->display = NULL;
1771
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001772 ref->reference = strdup(reference);
1773 if (!ref->reference) {
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001774 free(ref->display);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001775 free(ref);
1776 return NULL;
1777 }
1778
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001779 ref->flags = flags;
1780 ref->unique_id = -1;
1781
1782 LIST_INIT(&ref->head);
1783 LIST_INIT(&ref->pat);
1784
1785 LIST_ADDQ(&pattern_reference, &ref->list);
1786
1787 return ref;
1788}
1789
1790/* This function create new reference. <unique_id> is the unique id. If
1791 * the value of <unique_id> is -1, the unique id is calculated later.
1792 * <flags> are PAT_REF_*. /!\ The reference is not checked, and must
1793 * be unique. The user must check the reference with "pat_ref_lookup()"
1794 * or pat_ref_lookupid before calling this function. If the function
1795 * fail, it return NULL, else return new struct pat_ref.
1796 */
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001797struct pat_ref *pat_ref_newid(int unique_id, const char *display, unsigned int flags)
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001798{
1799 struct pat_ref *ref;
1800
1801 ref = malloc(sizeof(*ref));
1802 if (!ref)
1803 return NULL;
1804
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001805 if (display) {
1806 ref->display = strdup(display);
1807 if (!ref->display) {
1808 free(ref);
1809 return NULL;
1810 }
1811 }
1812 else
1813 ref->display = NULL;
1814
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001815 ref->reference = NULL;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001816 ref->flags = flags;
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001817 ref->unique_id = unique_id;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001818 LIST_INIT(&ref->head);
1819 LIST_INIT(&ref->pat);
1820
1821 LIST_ADDQ(&pattern_reference, &ref->list);
1822
1823 return ref;
1824}
1825
1826/* This function adds entry to <ref>. It can failed with memory error.
1827 * If the function fails, it returns 0.
1828 */
1829int pat_ref_append(struct pat_ref *ref, char *pattern, char *sample, int line)
1830{
1831 struct pat_ref_elt *elt;
1832
1833 elt = malloc(sizeof(*elt));
1834 if (!elt)
1835 return 0;
1836
1837 elt->line = line;
1838
1839 elt->pattern = strdup(pattern);
1840 if (!elt->pattern) {
1841 free(elt);
1842 return 0;
1843 }
1844
1845 if (sample) {
1846 elt->sample = strdup(sample);
1847 if (!elt->sample) {
1848 free(elt->pattern);
1849 free(elt);
1850 return 0;
1851 }
1852 }
1853 else
1854 elt->sample = NULL;
1855
1856 LIST_ADDQ(&ref->head, &elt->list);
1857
1858 return 1;
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001859}
1860
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001861/* This function create sample found in <elt>, parse the pattern also
1862 * found in <elt> and insert it in <expr>. The function copy <patflags>
1863 * in <expr>. If the function fails, it returns0 and <err> is filled.
1864 * In succes case, the function returns 1.
1865 */
1866static inline
1867int pat_ref_push(struct pat_ref_elt *elt, struct pattern_expr *expr,
1868 int patflags, char **err)
1869{
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001870 struct sample_data *data;
Thierry FOURNIERd25c8422014-01-28 15:34:35 +01001871 struct pattern pattern;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001872
1873 /* Create sample */
1874 if (elt->sample && expr->pat_head->parse_smp) {
1875 /* New sample. */
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001876 data = malloc(sizeof(*data));
1877 if (!data)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001878 return 0;
1879
1880 /* Parse value. */
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001881 if (!expr->pat_head->parse_smp(elt->sample, data)) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001882 memprintf(err, "unable to parse '%s'", elt->sample);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001883 free(data);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001884 return 0;
1885 }
1886
1887 }
1888 else
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001889 data = NULL;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001890
Thierry FOURNIERd25c8422014-01-28 15:34:35 +01001891 /* initialise pattern */
1892 memset(&pattern, 0, sizeof(pattern));
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001893 pattern.data = data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01001894 pattern.ref = elt;
Thierry FOURNIERd25c8422014-01-28 15:34:35 +01001895
1896 /* parse pattern */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02001897 if (!expr->pat_head->parse(elt->pattern, &pattern, expr->mflags, err)) {
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001898 free(data);
Thierry FOURNIERd25c8422014-01-28 15:34:35 +01001899 return 0;
1900 }
1901
1902 /* index pattern */
1903 if (!expr->pat_head->index(expr, &pattern, err)) {
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001904 free(data);
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001905 return 0;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001906 }
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001907
1908 return 1;
1909}
1910
Thierry FOURNIER31db4ae2014-01-30 00:27:15 +01001911/* This function adds entry to <ref>. It can failed with memory error. The new
1912 * entry is added at all the pattern_expr registered in this reference. The
1913 * function stop on the first error encountered. It returns 0 and err is
1914 * filled. If an error is encountered, the complete add operation is cancelled.
1915 * If the insertion is a success the function returns 1.
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001916 */
1917int pat_ref_add(struct pat_ref *ref,
1918 const char *pattern, const char *sample,
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02001919 char **err)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001920{
1921 struct pat_ref_elt *elt;
1922 struct pattern_expr *expr;
1923
1924 elt = malloc(sizeof(*elt));
1925 if (!elt) {
1926 memprintf(err, "out of memory error");
1927 return 0;
1928 }
1929
1930 elt->line = -1;
1931
1932 elt->pattern = strdup(pattern);
1933 if (!elt->pattern) {
1934 free(elt);
1935 memprintf(err, "out of memory error");
1936 return 0;
1937 }
1938
1939 if (sample) {
1940 elt->sample = strdup(sample);
1941 if (!elt->sample) {
1942 free(elt->pattern);
1943 free(elt);
1944 memprintf(err, "out of memory error");
1945 return 0;
1946 }
1947 }
1948 else
1949 elt->sample = NULL;
1950
1951 LIST_ADDQ(&ref->head, &elt->list);
1952
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001953 list_for_each_entry(expr, &ref->pat, list) {
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02001954 if (!pat_ref_push(elt, expr, 0, err)) {
Thierry FOURNIER31db4ae2014-01-30 00:27:15 +01001955 /* If the insertion fails, try to delete all the added entries. */
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001956 pat_ref_delete_by_id(ref, elt);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001957 return 0;
1958 }
1959 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001960 return 1;
1961}
1962
Thierry FOURNIER46006bd2014-03-21 21:45:15 +01001963/* This function prune <ref>, replace all reference by the references
1964 * of <replace>, and reindex all the news values.
1965 *
1966 * The pattern are loaded in best effort and the errors are ignored,
1967 * but writed in the logs.
1968 */
1969void pat_ref_reload(struct pat_ref *ref, struct pat_ref *replace)
1970{
1971 struct pattern_expr *expr;
1972 struct pat_ref_elt *elt;
1973 char *err = NULL;
1974
1975 pat_ref_prune(ref);
1976
1977 LIST_ADD(&replace->head, &ref->head);
1978 LIST_DEL(&replace->head);
1979
1980 list_for_each_entry(elt, &ref->head, list) {
1981 list_for_each_entry(expr, &ref->pat, list) {
1982 if (!pat_ref_push(elt, expr, 0, &err)) {
1983 send_log(NULL, LOG_NOTICE, "%s", err);
1984 free(err);
1985 err = NULL;
1986 }
1987 }
1988 }
1989}
1990
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001991/* This function prune all entries of <ref>. This function
1992 * prune the associated pattern_expr.
1993 */
1994void pat_ref_prune(struct pat_ref *ref)
1995{
1996 struct pat_ref_elt *elt, *safe;
1997 struct pattern_expr *expr;
1998
1999 list_for_each_entry_safe(elt, safe, &ref->head, list) {
2000 LIST_DEL(&elt->list);
2001 free(elt->pattern);
2002 free(elt->sample);
2003 free(elt);
2004 }
2005
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002006 list_for_each_entry(expr, &ref->pat, list)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002007 expr->pat_head->prune(expr);
2008}
2009
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002010/* This function lookup for existing reference <ref> in pattern_head <head>. */
2011struct pattern_expr *pattern_lookup_expr(struct pattern_head *head, struct pat_ref *ref)
2012{
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002013 struct pattern_expr_list *expr;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002014
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002015 list_for_each_entry(expr, &head->head, list)
2016 if (expr->expr->ref == ref)
2017 return expr->expr;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002018 return NULL;
2019}
2020
2021/* This function create new pattern_expr associated to the reference <ref>.
2022 * <ref> can be NULL. If an error is occured, the function returns NULL and
2023 * <err> is filled. Otherwise, the function returns new pattern_expr linked
2024 * with <head> and <ref>.
Thierry FOURNIER315ec422014-11-24 11:14:42 +01002025 *
2026 * The returned value can be a alredy filled pattern list, in this case the
2027 * flag <reuse> is set.
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002028 */
Thierry FOURNIER315ec422014-11-24 11:14:42 +01002029struct pattern_expr *pattern_new_expr(struct pattern_head *head, struct pat_ref *ref,
2030 char **err, int *reuse)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002031{
2032 struct pattern_expr *expr;
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002033 struct pattern_expr_list *list;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002034
Thierry FOURNIER315ec422014-11-24 11:14:42 +01002035 if (reuse)
2036 *reuse = 0;
2037
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002038 /* Memory and initialization of the chain element. */
2039 list = malloc(sizeof(*list));
2040 if (!list) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002041 memprintf(err, "out of memory");
2042 return NULL;
2043 }
2044
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002045 /* Look for existing similar expr. No that only the index, parse and
2046 * parse_smp function must be identical for having similar pattern.
2047 * The other function depends of theses first.
2048 */
2049 if (ref) {
2050 list_for_each_entry(expr, &ref->pat, list)
2051 if (expr->pat_head->index == head->index &&
2052 expr->pat_head->parse == head->parse &&
2053 expr->pat_head->parse_smp == head->parse_smp)
2054 break;
2055 if (&expr->list == &ref->pat)
2056 expr = NULL;
2057 }
2058 else
2059 expr = NULL;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002060
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002061 /* If no similar expr was found, we create new expr. */
2062 if (!expr) {
2063 /* Get a lot of memory for the expr struct. */
2064 expr = malloc(sizeof(*expr));
2065 if (!expr) {
Andreas Seltenreiche6e22e82016-03-03 20:20:23 +01002066 free(list);
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002067 memprintf(err, "out of memory");
2068 return NULL;
2069 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002070
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002071 /* Initialize this new expr. */
2072 pattern_init_expr(expr);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002073
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002074 /* This new pattern expression reference one of his heads. */
2075 expr->pat_head = head;
2076
2077 /* Link with ref, or to self to facilitate LIST_DEL() */
2078 if (ref)
2079 LIST_ADDQ(&ref->pat, &expr->list);
2080 else
2081 LIST_INIT(&expr->list);
2082
2083 expr->ref = ref;
2084
2085 /* We must free this pattern if it is no more used. */
2086 list->do_free = 1;
2087 }
2088 else {
2089 /* If the pattern used already exists, it is already linked
2090 * with ref and we must not free it.
2091 */
2092 list->do_free = 0;
Thierry FOURNIER315ec422014-11-24 11:14:42 +01002093 if (reuse)
2094 *reuse = 1;
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002095 }
2096
2097 /* The new list element reference the pattern_expr. */
2098 list->expr = expr;
2099
2100 /* Link the list element with the pattern_head. */
2101 LIST_ADDQ(&head->head, &list->list);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002102 return expr;
2103}
2104
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002105/* Reads patterns from a file. If <err_msg> is non-NULL, an error message will
2106 * be returned there on errors and the caller will have to free it.
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002107 *
2108 * The file contains one key + value per line. Lines which start with '#' are
2109 * ignored, just like empty lines. Leading tabs/spaces are stripped. The key is
2110 * then the first "word" (series of non-space/tabs characters), and the value is
2111 * what follows this series of space/tab till the end of the line excluding
2112 * trailing spaces/tabs.
2113 *
2114 * Example :
2115 *
2116 * # this is a comment and is ignored
2117 * 62.212.114.60 1wt.eu \n
2118 * <-><-----------><---><----><---->
2119 * | | | | `--- trailing spaces ignored
2120 * | | | `-------- value
2121 * | | `--------------- middle spaces ignored
2122 * | `------------------------ key
2123 * `-------------------------------- leading spaces ignored
2124 *
2125 * Return non-zero in case of succes, otherwise 0.
2126 */
2127int pat_ref_read_from_file_smp(struct pat_ref *ref, const char *filename, char **err)
2128{
2129 FILE *file;
2130 char *c;
2131 int ret = 0;
2132 int line = 0;
2133 char *key_beg;
2134 char *key_end;
2135 char *value_beg;
2136 char *value_end;
2137
2138 file = fopen(filename, "r");
2139 if (!file) {
2140 memprintf(err, "failed to open pattern file <%s>", filename);
2141 return 0;
2142 }
2143
2144 /* now parse all patterns. The file may contain only one pattern
2145 * followed by one value per line. The start spaces, separator spaces
2146 * and and spaces are stripped. Each can contain comment started by '#'
2147 */
2148 while (fgets(trash.str, trash.size, file) != NULL) {
2149 line++;
2150 c = trash.str;
2151
2152 /* ignore lines beginning with a dash */
2153 if (*c == '#')
2154 continue;
2155
2156 /* strip leading spaces and tabs */
2157 while (*c == ' ' || *c == '\t')
2158 c++;
2159
2160 /* empty lines are ignored too */
2161 if (*c == '\0' || *c == '\r' || *c == '\n')
2162 continue;
2163
2164 /* look for the end of the key */
2165 key_beg = c;
2166 while (*c && *c != ' ' && *c != '\t' && *c != '\n' && *c != '\r')
2167 c++;
2168
2169 key_end = c;
2170
2171 /* strip middle spaces and tabs */
2172 while (*c == ' ' || *c == '\t')
2173 c++;
2174
2175 /* look for the end of the value, it is the end of the line */
2176 value_beg = c;
2177 while (*c && *c != '\n' && *c != '\r')
2178 c++;
2179 value_end = c;
2180
2181 /* trim possibly trailing spaces and tabs */
2182 while (value_end > value_beg && (value_end[-1] == ' ' || value_end[-1] == '\t'))
2183 value_end--;
2184
2185 /* set final \0 and check entries */
2186 *key_end = '\0';
2187 *value_end = '\0';
2188
2189 /* insert values */
2190 if (!pat_ref_append(ref, key_beg, value_beg, line)) {
2191 memprintf(err, "out of memory");
2192 goto out_close;
2193 }
2194 }
2195
2196 /* succes */
2197 ret = 1;
2198
2199 out_close:
2200 fclose(file);
2201 return ret;
2202}
2203
2204/* Reads patterns from a file. If <err_msg> is non-NULL, an error message will
2205 * be returned there on errors and the caller will have to free it.
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002206 */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002207int pat_ref_read_from_file(struct pat_ref *ref, const char *filename, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002208{
2209 FILE *file;
2210 char *c;
2211 char *arg;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002212 int ret = 0;
2213 int line = 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002214
2215 file = fopen(filename, "r");
2216 if (!file) {
2217 memprintf(err, "failed to open pattern file <%s>", filename);
2218 return 0;
2219 }
2220
2221 /* now parse all patterns. The file may contain only one pattern per
2222 * line. If the line contains spaces, they will be part of the pattern.
2223 * The pattern stops at the first CR, LF or EOF encountered.
2224 */
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002225 while (fgets(trash.str, trash.size, file) != NULL) {
2226 line++;
2227 c = trash.str;
2228
2229 /* ignore lines beginning with a dash */
2230 if (*c == '#')
2231 continue;
2232
2233 /* strip leading spaces and tabs */
2234 while (*c == ' ' || *c == '\t')
2235 c++;
2236
2237
2238 arg = c;
2239 while (*c && *c != '\n' && *c != '\r')
2240 c++;
2241 *c = 0;
2242
2243 /* empty lines are ignored too */
2244 if (c == arg)
2245 continue;
2246
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002247 if (!pat_ref_append(ref, arg, NULL, line)) {
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002248 memprintf(err, "out of memory when loading patterns from file <%s>", filename);
2249 goto out_close;
2250 }
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002251 }
2252
2253 ret = 1; /* success */
2254
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002255 out_close:
2256 fclose(file);
2257 return ret;
2258}
2259
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002260int pattern_read_from_file(struct pattern_head *head, unsigned int refflags,
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002261 const char *filename, int patflags, int load_smp,
Thierry FOURNIER94580c92014-02-11 14:36:45 +01002262 char **err, const char *file, int line)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002263{
2264 struct pat_ref *ref;
2265 struct pattern_expr *expr;
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002266 struct pat_ref_elt *elt;
Willy Tarreau4deaf392014-11-26 13:17:03 +01002267 int reuse = 0;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002268
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002269 /* Lookup for the existing reference. */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002270 ref = pat_ref_lookup(filename);
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002271
2272 /* If the reference doesn't exists, create it and load associated file. */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002273 if (!ref) {
Thierry FOURNIER94580c92014-02-11 14:36:45 +01002274 chunk_printf(&trash,
2275 "pattern loaded from file '%s' used by %s at file '%s' line %d",
2276 filename, refflags & PAT_REF_MAP ? "map" : "acl", file, line);
2277
2278 ref = pat_ref_new(filename, trash.str, refflags);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002279 if (!ref) {
2280 memprintf(err, "out of memory");
2281 return 0;
2282 }
2283
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002284 if (load_smp) {
Thierry FOURNIERc0bd9102014-01-29 12:32:58 +01002285 ref->flags |= PAT_REF_SMP;
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002286 if (!pat_ref_read_from_file_smp(ref, filename, err))
2287 return 0;
2288 }
2289 else {
2290 if (!pat_ref_read_from_file(ref, filename, err))
2291 return 0;
2292 }
2293 }
2294 else {
Thierry FOURNIERc0bd9102014-01-29 12:32:58 +01002295 /* The reference already exists, check the map compatibility. */
2296
2297 /* If the load require samples and the flag PAT_REF_SMP is not set,
2298 * the reference doesn't contain sample, and cannot be used.
2299 */
2300 if (load_smp) {
2301 if (!(ref->flags & PAT_REF_SMP)) {
2302 memprintf(err, "The file \"%s\" is already used as one column file "
2303 "and cannot be used by as two column file.",
2304 filename);
2305 return 0;
2306 }
2307 }
2308 else {
2309 /* The load doesn't require samples. If the flag PAT_REF_SMP is
2310 * set, the reference contains a sample, and cannot be used.
2311 */
2312 if (ref->flags & PAT_REF_SMP) {
2313 memprintf(err, "The file \"%s\" is already used as two column file "
2314 "and cannot be used by as one column file.",
2315 filename);
2316 return 0;
2317 }
2318 }
2319
Thierry FOURNIER94580c92014-02-11 14:36:45 +01002320 /* Extends display */
2321 chunk_printf(&trash, "%s", ref->display);
2322 chunk_appendf(&trash, ", by %s at file '%s' line %d",
2323 refflags & PAT_REF_MAP ? "map" : "acl", file, line);
2324 free(ref->display);
2325 ref->display = strdup(trash.str);
2326 if (!ref->display) {
2327 memprintf(err, "out of memory");
2328 return 0;
2329 }
2330
Thierry FOURNIERc0bd9102014-01-29 12:32:58 +01002331 /* Merge flags. */
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002332 ref->flags |= refflags;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002333 }
2334
2335 /* Now, we can loading patterns from the reference. */
2336
2337 /* Lookup for existing reference in the head. If the reference
2338 * doesn't exists, create it.
2339 */
2340 expr = pattern_lookup_expr(head, ref);
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02002341 if (!expr || (expr->mflags != patflags)) {
Thierry FOURNIER315ec422014-11-24 11:14:42 +01002342 expr = pattern_new_expr(head, ref, err, &reuse);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002343 if (!expr)
2344 return 0;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02002345 expr->mflags = patflags;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002346 }
2347
Thierry FOURNIER315ec422014-11-24 11:14:42 +01002348 /* The returned expression may be not empty, because the function
2349 * "pattern_new_expr" lookup for similar pattern list and can
2350 * reuse a already filled pattern list. In this case, we can not
2351 * reload the patterns.
2352 */
2353 if (reuse)
2354 return 1;
2355
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002356 /* Load reference content in the pattern expression. */
2357 list_for_each_entry(elt, &ref->head, list) {
2358 if (!pat_ref_push(elt, expr, patflags, err)) {
2359 if (elt->line > 0)
2360 memprintf(err, "%s at line %d of file '%s'",
2361 *err, elt->line, filename);
2362 return 0;
2363 }
2364 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002365
2366 return 1;
2367}
2368
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01002369/* This function executes a pattern match on a sample. It applies pattern <expr>
2370 * to sample <smp>. The function returns NULL if the sample dont match. It returns
2371 * non-null if the sample match. If <fill> is true and the sample match, the
2372 * function returns the matched pattern. In many cases, this pattern can be a
2373 * static buffer.
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002374 */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002375struct pattern *pattern_exec_match(struct pattern_head *head, struct sample *smp, int fill)
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002376{
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002377 struct pattern_expr_list *list;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002378 struct pattern *pat;
2379
2380 if (!head->match) {
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01002381 if (fill) {
Thierry FOURNIER503bb092015-08-19 08:35:43 +02002382 static_pattern.data = NULL;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01002383 static_pattern.ref = NULL;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02002384 static_pattern.sflags = 0;
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002385 static_pattern.type = SMP_T_SINT;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01002386 static_pattern.val.i = 1;
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01002387 }
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01002388 return &static_pattern;
2389 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002390
Thierry FOURNIER5d344082014-01-27 14:19:53 +01002391 /* convert input to string */
2392 if (!sample_convert(smp, head->expect_type))
2393 return NULL;
2394
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002395 list_for_each_entry(list, &head->head, list) {
2396 pat = head->match(smp, list->expr, fill);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002397 if (pat)
2398 return pat;
2399 }
2400 return NULL;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002401}
2402
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01002403/* This function prune the pattern expression. */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002404void pattern_prune(struct pattern_head *head)
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01002405{
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002406 struct pattern_expr_list *list, *safe;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002407
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002408 list_for_each_entry_safe(list, safe, &head->head, list) {
2409 LIST_DEL(&list->list);
2410 if (list->do_free) {
2411 LIST_DEL(&list->expr->list);
2412 head->prune(list->expr);
2413 free(list->expr);
2414 }
2415 free(list);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002416 }
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01002417}
2418
Thierry FOURNIER55d0b102014-01-15 11:25:26 +01002419/* This function lookup for a pattern matching the <key> and return a
2420 * pointer to a pointer of the sample stoarge. If the <key> dont match,
2421 * the function returns NULL. If the key cannot be parsed, the function
2422 * fill <err>.
2423 */
Thierry FOURNIER12ba0c22015-08-14 00:02:11 +02002424struct sample_data **pattern_find_smp(struct pattern_expr *expr, struct pat_ref_elt *ref)
Thierry FOURNIER55d0b102014-01-15 11:25:26 +01002425{
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01002426 struct ebmb_node *node;
2427 struct pattern_tree *elt;
2428 struct pattern_list *pat;
Thierry FOURNIER55d0b102014-01-15 11:25:26 +01002429
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01002430 for (node = ebmb_first(&expr->pattern_tree);
2431 node;
2432 node = ebmb_next(node)) {
2433 elt = container_of(node, struct pattern_tree, node);
2434 if (elt->ref == ref)
Thierry FOURNIER503bb092015-08-19 08:35:43 +02002435 return &elt->data;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01002436 }
2437
2438 for (node = ebmb_first(&expr->pattern_tree_2);
2439 node;
2440 node = ebmb_next(node)) {
2441 elt = container_of(node, struct pattern_tree, node);
2442 if (elt->ref == ref)
Thierry FOURNIER503bb092015-08-19 08:35:43 +02002443 return &elt->data;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01002444 }
2445
2446 list_for_each_entry(pat, &expr->patterns, list)
2447 if (pat->pat.ref == ref)
Thierry FOURNIER503bb092015-08-19 08:35:43 +02002448 return &pat->pat.data;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01002449
2450 return NULL;
Thierry FOURNIER55d0b102014-01-15 11:25:26 +01002451}
2452
Thierry FOURNIERb1136502014-01-15 11:38:49 +01002453/* This function search all the pattern matching the <key> and delete it.
2454 * If the parsing of the input key fails, the function returns 0 and the
2455 * <err> is filled, else return 1;
2456 */
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01002457int pattern_delete(struct pattern_expr *expr, struct pat_ref_elt *ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01002458{
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01002459 expr->pat_head->delete(expr, ref);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01002460 return 1;
2461}
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002462
2463/* This function finalize the configuration parsing. Its set all the
2464 * automatic ids
2465 */
2466void pattern_finalize_config(void)
2467{
2468 int i = 0;
2469 struct pat_ref *ref, *ref2, *ref3;
2470 struct list pr = LIST_HEAD_INIT(pr);
2471
Willy Tarreauf3045d22015-04-29 16:24:50 +02002472 pat_lru_seed = random();
2473 if (global.tune.pattern_cache)
2474 pat_lru_tree = lru64_new(global.tune.pattern_cache);
2475
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002476 list_for_each_entry(ref, &pattern_reference, list) {
2477 if (ref->unique_id == -1) {
2478 /* Look for the first free id. */
2479 while (1) {
2480 list_for_each_entry(ref2, &pattern_reference, list) {
2481 if (ref2->unique_id == i) {
2482 i++;
2483 break;
2484 }
2485 }
Willy Tarreau3b786962014-04-26 12:37:25 +02002486 if (&ref2->list == &pattern_reference)
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002487 break;
2488 }
2489
2490 /* Uses the unique id and increment it for the next entry. */
2491 ref->unique_id = i;
2492 i++;
2493 }
2494 }
2495
2496 /* This sort the reference list by id. */
2497 list_for_each_entry_safe(ref, ref2, &pattern_reference, list) {
2498 LIST_DEL(&ref->list);
2499 list_for_each_entry(ref3, &pr, list) {
2500 if (ref->unique_id < ref3->unique_id) {
2501 LIST_ADDQ(&ref3->list, &ref->list);
2502 break;
2503 }
2504 }
2505 if (&ref3->list == &pr)
2506 LIST_ADDQ(&pr, &ref->list);
2507 }
2508
2509 /* swap root */
2510 LIST_ADD(&pr, &pattern_reference);
2511 LIST_DEL(&pr);
2512}