blob: fe672f12e8314029f8cb7593d499d35ec3922b9f [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 */
Emeric Brunb5997f72017-07-03 11:34:05 +0200151static THREAD_LOCAL struct pattern static_pattern;
152static THREAD_LOCAL struct sample_data static_sample_data;
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +0100153
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100154/* This is the root of the list of all pattern_ref avalaibles. */
155struct list pattern_reference = LIST_HEAD_INIT(pattern_reference);
156
Willy Tarreauf3045d22015-04-29 16:24:50 +0200157static struct lru64_head *pat_lru_tree;
Christopher Faulet9dcf9b62017-11-13 10:34:01 +0100158__decl_hathreads(HA_SPINLOCK_T pat_lru_tree_lock);
Willy Tarreauf3045d22015-04-29 16:24:50 +0200159static unsigned long long pat_lru_seed;
160
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100161/*
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100162 *
163 * The following functions are not exported and are used by internals process
164 * of pattern matching
165 *
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100166 */
167
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100168/* Background: Fast way to find a zero byte in a word
169 * http://graphics.stanford.edu/~seander/bithacks.html#ZeroInWord
170 * hasZeroByte = (v - 0x01010101UL) & ~v & 0x80808080UL;
171 *
172 * To look for 4 different byte values, xor the word with those bytes and
173 * then check for zero bytes:
174 *
175 * v = (((unsigned char)c * 0x1010101U) ^ delimiter)
176 * where <delimiter> is the 4 byte values to look for (as an uint)
177 * and <c> is the character that is being tested
178 */
179static inline unsigned int is_delimiter(unsigned char c, unsigned int mask)
180{
181 mask ^= (c * 0x01010101); /* propagate the char to all 4 bytes */
182 return (mask - 0x01010101) & ~mask & 0x80808080U;
183}
184
185static inline unsigned int make_4delim(unsigned char d1, unsigned char d2, unsigned char d3, unsigned char d4)
186{
187 return d1 << 24 | d2 << 16 | d3 << 8 | d4;
188}
189
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100190
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100191/*
192 *
193 * These functions are exported and may be used by any other component.
194 *
Willy Tarreau5def8ef2014-08-29 15:19:33 +0200195 * The following functions are used for parsing pattern matching input value.
196 * The <text> contain the string to be parsed. <pattern> must be a preallocated
197 * pattern. The pat_parse_* functions fill this structure with the parsed value.
198 * <err> is filled with an error message built with memprintf() function. It is
199 * allowed to use a trash as a temporary storage for the returned pattern, as
200 * the next call after these functions will be pat_idx_*.
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100201 *
Willy Tarreau5def8ef2014-08-29 15:19:33 +0200202 * In success case, the pat_parse_* function returns 1. If the function
203 * fails, it returns 0 and <err> is filled.
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100204 */
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100205
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100206/* ignore the current line */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200207int pat_parse_nothing(const char *text, struct pattern *pattern, int mflags, char **err)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100208{
209 return 1;
210}
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100211
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100212/* Parse a string. It is allocated and duplicated. */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200213int pat_parse_str(const char *text, struct pattern *pattern, int mflags, char **err)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100214{
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100215 pattern->type = SMP_T_STR;
Thierry FOURNIERedc15c32013-12-13 15:36:59 +0100216 pattern->ptr.str = (char *)text;
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100217 pattern->len = strlen(text);
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100218 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100219}
220
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100221/* Parse a binary written in hexa. It is allocated. */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200222int pat_parse_bin(const char *text, struct pattern *pattern, int mflags, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100223{
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100224 struct chunk *trash;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100225
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100226 pattern->type = SMP_T_BIN;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100227 trash = get_trash_chunk();
228 pattern->len = trash->size;
229 pattern->ptr.str = trash->str;
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100230 return !!parse_binary(text, &pattern->ptr.str, &pattern->len, err);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100231}
232
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100233/* Parse a regex. It is allocated. */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200234int pat_parse_reg(const char *text, struct pattern *pattern, int mflags, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100235{
Thierry FOURNIER0b6d15f2014-01-29 19:35:16 +0100236 pattern->ptr.str = (char *)text;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100237 return 1;
238}
239
240/* Parse a range of positive integers delimited by either ':' or '-'. If only
241 * one integer is read, it is set as both min and max. An operator may be
242 * specified as the prefix, among this list of 5 :
243 *
244 * 0:eq, 1:gt, 2:ge, 3:lt, 4:le
245 *
246 * The default operator is "eq". It supports range matching. Ranges are
247 * rejected for other operators. The operator may be changed at any time.
248 * The operator is stored in the 'opaque' argument.
249 *
250 * If err is non-NULL, an error message will be returned there on errors and
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100251 * the caller will have to free it. The function returns zero on error, and
252 * non-zero on success.
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100253 *
254 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200255int pat_parse_int(const char *text, struct pattern *pattern, int mflags, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100256{
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100257 const char *ptr = text;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100258
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200259 pattern->type = SMP_T_SINT;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100260
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100261 /* Empty string is not valid */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100262 if (!*text)
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100263 goto not_valid_range;
264
265 /* Search ':' or '-' separator. */
266 while (*ptr != '\0' && *ptr != ':' && *ptr != '-')
267 ptr++;
268
269 /* If separator not found. */
270 if (!*ptr) {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100271 if (strl2llrc(text, ptr - text, &pattern->val.range.min) != 0) {
272 memprintf(err, "'%s' is not a number", text);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100273 return 0;
274 }
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100275 pattern->val.range.max = pattern->val.range.min;
276 pattern->val.range.min_set = 1;
277 pattern->val.range.max_set = 1;
278 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100279 }
280
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100281 /* If the separator is the first character. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100282 if (ptr == text && *(ptr + 1) != '\0') {
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100283 if (strl2llrc(ptr + 1, strlen(ptr + 1), &pattern->val.range.max) != 0)
284 goto not_valid_range;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100285
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100286 pattern->val.range.min_set = 0;
287 pattern->val.range.max_set = 1;
288 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100289 }
290
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100291 /* If separator is the last character. */
292 if (*(ptr + 1) == '\0') {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100293 if (strl2llrc(text, ptr - text, &pattern->val.range.min) != 0)
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100294 goto not_valid_range;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100295
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100296 pattern->val.range.min_set = 1;
297 pattern->val.range.max_set = 0;
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100298 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100299 }
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100300
301 /* Else, parse two numbers. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100302 if (strl2llrc(text, ptr - text, &pattern->val.range.min) != 0)
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100303 goto not_valid_range;
304
305 if (strl2llrc(ptr + 1, strlen(ptr + 1), &pattern->val.range.max) != 0)
306 goto not_valid_range;
307
308 if (pattern->val.range.min > pattern->val.range.max)
309 goto not_valid_range;
310
311 pattern->val.range.min_set = 1;
312 pattern->val.range.max_set = 1;
313 return 1;
314
315 not_valid_range:
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100316 memprintf(err, "'%s' is not a valid number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100317 return 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100318}
319
320/* Parse a range of positive 2-component versions delimited by either ':' or
321 * '-'. The version consists in a major and a minor, both of which must be
322 * smaller than 65536, because internally they will be represented as a 32-bit
323 * integer.
324 * If only one version is read, it is set as both min and max. Just like for
325 * pure integers, an operator may be specified as the prefix, among this list
326 * of 5 :
327 *
328 * 0:eq, 1:gt, 2:ge, 3:lt, 4:le
329 *
330 * The default operator is "eq". It supports range matching. Ranges are
331 * rejected for other operators. The operator may be changed at any time.
332 * The operator is stored in the 'opaque' argument. This allows constructs
333 * such as the following one :
334 *
335 * acl obsolete_ssl ssl_req_proto lt 3
336 * acl unsupported_ssl ssl_req_proto gt 3.1
337 * acl valid_ssl ssl_req_proto 3.0-3.1
338 *
339 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200340int pat_parse_dotted_ver(const char *text, struct pattern *pattern, int mflags, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100341{
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100342 const char *ptr = text;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100343
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200344 pattern->type = SMP_T_SINT;
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100345
346 /* Search ':' or '-' separator. */
347 while (*ptr != '\0' && *ptr != ':' && *ptr != '-')
348 ptr++;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100349
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100350 /* If separator not found. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100351 if (*ptr == '\0' && ptr > text) {
352 if (strl2llrc_dotted(text, ptr-text, &pattern->val.range.min) != 0) {
353 memprintf(err, "'%s' is not a dotted number", text);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100354 return 0;
355 }
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100356 pattern->val.range.max = pattern->val.range.min;
357 pattern->val.range.min_set = 1;
358 pattern->val.range.max_set = 1;
359 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100360 }
361
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100362 /* If the separator is the first character. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100363 if (ptr == text && *(ptr+1) != '\0') {
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100364 if (strl2llrc_dotted(ptr+1, strlen(ptr+1), &pattern->val.range.max) != 0) {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100365 memprintf(err, "'%s' is not a valid dotted number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100366 return 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100367 }
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100368 pattern->val.range.min_set = 0;
369 pattern->val.range.max_set = 1;
370 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100371 }
372
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100373 /* If separator is the last character. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100374 if (ptr == &text[strlen(text)-1]) {
375 if (strl2llrc_dotted(text, ptr-text, &pattern->val.range.min) != 0) {
376 memprintf(err, "'%s' is not a valid dotted number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100377 return 0;
378 }
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100379 pattern->val.range.min_set = 1;
380 pattern->val.range.max_set = 0;
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100381 return 1;
382 }
383
384 /* Else, parse two numbers. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100385 if (strl2llrc_dotted(text, ptr-text, &pattern->val.range.min) != 0) {
386 memprintf(err, "'%s' is not a valid dotted number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100387 return 0;
388 }
389 if (strl2llrc_dotted(ptr+1, strlen(ptr+1), &pattern->val.range.max) != 0) {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100390 memprintf(err, "'%s' is not a valid dotted number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100391 return 0;
392 }
393 if (pattern->val.range.min > pattern->val.range.max) {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100394 memprintf(err, "'%s' is not a valid dotted number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100395 return 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100396 }
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100397 pattern->val.range.min_set = 1;
398 pattern->val.range.max_set = 1;
399 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100400}
401
402/* Parse an IP address and an optional mask in the form addr[/mask].
403 * The addr may either be an IPv4 address or a hostname. The mask
404 * may either be a dotted mask or a number of bits. Returns 1 if OK,
405 * otherwise 0. NOTE: IP address patterns are typed (IPV4/IPV6).
406 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200407int pat_parse_ip(const char *text, struct pattern *pattern, int mflags, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100408{
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200409 if (str2net(text, !(mflags & PAT_MF_NO_DNS) && (global.mode & MODE_STARTING),
Thierry FOURNIERfc7ac7b2014-02-11 15:23:04 +0100410 &pattern->val.ipv4.addr, &pattern->val.ipv4.mask)) {
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100411 pattern->type = SMP_T_IPV4;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100412 return 1;
413 }
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100414 else if (str62net(text, &pattern->val.ipv6.addr, &pattern->val.ipv6.mask)) {
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100415 pattern->type = SMP_T_IPV6;
416 return 1;
417 }
418 else {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100419 memprintf(err, "'%s' is not a valid IPv4 or IPv6 address", text);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100420 return 0;
421 }
422}
423
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100424/*
425 *
426 * These functions are exported and may be used by any other component.
427 *
428 * This fucntion just take a sample <smp> and check if this sample match
429 * with the pattern <pattern>. This fucntion return just PAT_MATCH or
430 * PAT_NOMATCH.
431 *
432 */
433
434/* always return false */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100435struct pattern *pat_match_nothing(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100436{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200437 if (smp->data.u.sint) {
Thierry FOURNIERe5978bf2014-03-17 19:53:10 +0100438 if (fill) {
Thierry FOURNIER503bb092015-08-19 08:35:43 +0200439 static_pattern.data = NULL;
Thierry FOURNIERe5978bf2014-03-17 19:53:10 +0100440 static_pattern.ref = NULL;
Thierry FOURNIERe5978bf2014-03-17 19:53:10 +0100441 static_pattern.type = 0;
442 static_pattern.ptr.str = NULL;
443 }
444 return &static_pattern;
445 }
446 else
447 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100448}
449
450
451/* NB: For two strings to be identical, it is required that their lengths match */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100452struct pattern *pat_match_str(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100453{
454 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100455 struct ebmb_node *node;
456 char prev;
457 struct pattern_tree *elt;
458 struct pattern_list *lst;
459 struct pattern *pattern;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200460 struct pattern *ret = NULL;
461 struct lru64 *lru = NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100462
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100463 /* Lookup a string in the expression's pattern tree. */
464 if (!eb_is_empty(&expr->pattern_tree)) {
465 /* we may have to force a trailing zero on the test pattern */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200466 prev = smp->data.u.str.str[smp->data.u.str.len];
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100467 if (prev)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200468 smp->data.u.str.str[smp->data.u.str.len] = '\0';
469 node = ebst_lookup(&expr->pattern_tree, smp->data.u.str.str);
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100470 if (prev)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200471 smp->data.u.str.str[smp->data.u.str.len] = prev;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100472
473 if (node) {
474 if (fill) {
475 elt = ebmb_entry(node, struct pattern_tree, node);
Thierry FOURNIER503bb092015-08-19 08:35:43 +0200476 static_pattern.data = elt->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +0100477 static_pattern.ref = elt->ref;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200478 static_pattern.sflags = PAT_SF_TREE;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100479 static_pattern.type = SMP_T_STR;
480 static_pattern.ptr.str = (char *)elt->node.key;
481 }
482 return &static_pattern;
483 }
484 }
485
486 /* look in the list */
Willy Tarreauf3045d22015-04-29 16:24:50 +0200487 if (pat_lru_tree) {
Willy Tarreauaee93142015-05-04 17:18:42 +0200488 unsigned long long seed = pat_lru_seed ^ (long)expr;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200489
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100490 HA_SPIN_LOCK(PATLRU_LOCK, &pat_lru_tree_lock);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200491 lru = lru64_get(XXH64(smp->data.u.str.str, smp->data.u.str.len, seed),
Willy Tarreauf3045d22015-04-29 16:24:50 +0200492 pat_lru_tree, expr, expr->revision);
Emeric Brunb5997f72017-07-03 11:34:05 +0200493 if (!lru) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100494 HA_SPIN_UNLOCK(PATLRU_LOCK, &pat_lru_tree_lock);
Emeric Brunb5997f72017-07-03 11:34:05 +0200495 }
496 else if (lru->domain) {
497 ret = lru->data;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100498 HA_SPIN_UNLOCK(PATLRU_LOCK, &pat_lru_tree_lock);
Emeric Brunb5997f72017-07-03 11:34:05 +0200499 return ret;
500 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200501 }
502
Emeric Brunb5997f72017-07-03 11:34:05 +0200503
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100504 list_for_each_entry(lst, &expr->patterns, list) {
505 pattern = &lst->pat;
506
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200507 if (pattern->len != smp->data.u.str.len)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100508 continue;
509
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200510 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200511 if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.str.str, smp->data.u.str.len) == 0) ||
512 (!icase && strncmp(pattern->ptr.str, smp->data.u.str.str, smp->data.u.str.len) == 0)) {
Willy Tarreauf3045d22015-04-29 16:24:50 +0200513 ret = pattern;
514 break;
515 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100516 }
517
Emeric Brunb5997f72017-07-03 11:34:05 +0200518 if (lru) {
519 lru64_commit(lru, ret, expr, expr->revision, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100520 HA_SPIN_UNLOCK(PATLRU_LOCK, &pat_lru_tree_lock);
Emeric Brunb5997f72017-07-03 11:34:05 +0200521 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200522
523 return ret;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100524}
525
526/* NB: For two binaries buf to be identical, it is required that their lengths match */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100527struct pattern *pat_match_bin(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100528{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100529 struct pattern_list *lst;
530 struct pattern *pattern;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200531 struct pattern *ret = NULL;
532 struct lru64 *lru = NULL;
533
534 if (pat_lru_tree) {
Willy Tarreauaee93142015-05-04 17:18:42 +0200535 unsigned long long seed = pat_lru_seed ^ (long)expr;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100536
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100537 HA_SPIN_LOCK(PATLRU_LOCK, &pat_lru_tree_lock);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200538 lru = lru64_get(XXH64(smp->data.u.str.str, smp->data.u.str.len, seed),
Willy Tarreauf3045d22015-04-29 16:24:50 +0200539 pat_lru_tree, expr, expr->revision);
Emeric Brunb5997f72017-07-03 11:34:05 +0200540 if (!lru) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100541 HA_SPIN_UNLOCK(PATLRU_LOCK, &pat_lru_tree_lock);
Emeric Brunb5997f72017-07-03 11:34:05 +0200542 }
543 else if (lru->domain) {
544 ret = lru->data;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100545 HA_SPIN_UNLOCK(PATLRU_LOCK, &pat_lru_tree_lock);
Emeric Brunb5997f72017-07-03 11:34:05 +0200546 return ret;
547 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200548 }
549
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100550 list_for_each_entry(lst, &expr->patterns, list) {
551 pattern = &lst->pat;
552
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200553 if (pattern->len != smp->data.u.str.len)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100554 continue;
555
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200556 if (memcmp(pattern->ptr.str, smp->data.u.str.str, smp->data.u.str.len) == 0) {
Willy Tarreauf3045d22015-04-29 16:24:50 +0200557 ret = pattern;
558 break;
559 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100560 }
561
Emeric Brunb5997f72017-07-03 11:34:05 +0200562 if (lru) {
563 lru64_commit(lru, ret, expr, expr->revision, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100564 HA_SPIN_UNLOCK(PATLRU_LOCK, &pat_lru_tree_lock);
Emeric Brunb5997f72017-07-03 11:34:05 +0200565 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200566
567 return ret;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100568}
569
570/* Executes a regex. It temporarily changes the data to add a trailing zero,
Thierry Fournier8feaa662016-02-10 22:55:20 +0100571 * and restores the previous character when leaving. This function fills
572 * a matching array.
573 */
574struct pattern *pat_match_regm(struct sample *smp, struct pattern_expr *expr, int fill)
575{
576 struct pattern_list *lst;
577 struct pattern *pattern;
578 struct pattern *ret = NULL;
579
580 list_for_each_entry(lst, &expr->patterns, list) {
581 pattern = &lst->pat;
582
583 if (regex_exec_match2(pattern->ptr.reg, smp->data.u.str.str, smp->data.u.str.len,
584 MAX_MATCH, pmatch, 0)) {
585 ret = pattern;
586 smp->ctx.a[0] = pmatch;
587 break;
588 }
589 }
590
591 return ret;
592}
593
594/* Executes a regex. It temporarily changes the data to add a trailing zero,
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100595 * and restores the previous character when leaving.
596 */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100597struct pattern *pat_match_reg(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100598{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100599 struct pattern_list *lst;
600 struct pattern *pattern;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200601 struct pattern *ret = NULL;
602 struct lru64 *lru = NULL;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100603
Willy Tarreauf3045d22015-04-29 16:24:50 +0200604 if (pat_lru_tree) {
Willy Tarreauaee93142015-05-04 17:18:42 +0200605 unsigned long long seed = pat_lru_seed ^ (long)expr;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200606
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100607 HA_SPIN_LOCK(PATLRU_LOCK, &pat_lru_tree_lock);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200608 lru = lru64_get(XXH64(smp->data.u.str.str, smp->data.u.str.len, seed),
Willy Tarreauf3045d22015-04-29 16:24:50 +0200609 pat_lru_tree, expr, expr->revision);
Emeric Brunb5997f72017-07-03 11:34:05 +0200610 if (!lru) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100611 HA_SPIN_UNLOCK(PATLRU_LOCK, &pat_lru_tree_lock);
Emeric Brunb5997f72017-07-03 11:34:05 +0200612 }
613 else if (lru->domain) {
614 ret = lru->data;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100615 HA_SPIN_UNLOCK(PATLRU_LOCK, &pat_lru_tree_lock);
Emeric Brunb5997f72017-07-03 11:34:05 +0200616 return ret;
617 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200618 }
619
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100620 list_for_each_entry(lst, &expr->patterns, list) {
621 pattern = &lst->pat;
622
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200623 if (regex_exec2(pattern->ptr.reg, smp->data.u.str.str, smp->data.u.str.len)) {
Willy Tarreauf3045d22015-04-29 16:24:50 +0200624 ret = pattern;
625 break;
626 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100627 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200628
Emeric Brunb5997f72017-07-03 11:34:05 +0200629 if (lru) {
630 lru64_commit(lru, ret, expr, expr->revision, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100631 HA_SPIN_UNLOCK(PATLRU_LOCK, &pat_lru_tree_lock);
Emeric Brunb5997f72017-07-03 11:34:05 +0200632 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200633
634 return ret;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100635}
636
637/* Checks that the pattern matches the beginning of the tested string. */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100638struct pattern *pat_match_beg(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100639{
640 int icase;
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +0200641 struct ebmb_node *node;
642 char prev;
643 struct pattern_tree *elt;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100644 struct pattern_list *lst;
645 struct pattern *pattern;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200646 struct pattern *ret = NULL;
647 struct lru64 *lru = NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100648
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +0200649 /* Lookup a string in the expression's pattern tree. */
650 if (!eb_is_empty(&expr->pattern_tree)) {
651 /* we may have to force a trailing zero on the test pattern */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200652 prev = smp->data.u.str.str[smp->data.u.str.len];
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +0200653 if (prev)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200654 smp->data.u.str.str[smp->data.u.str.len] = '\0';
655 node = ebmb_lookup_longest(&expr->pattern_tree, smp->data.u.str.str);
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +0200656 if (prev)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200657 smp->data.u.str.str[smp->data.u.str.len] = prev;
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +0200658
659 if (node) {
660 if (fill) {
661 elt = ebmb_entry(node, struct pattern_tree, node);
Thierry FOURNIER503bb092015-08-19 08:35:43 +0200662 static_pattern.data = elt->data;
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +0200663 static_pattern.ref = elt->ref;
664 static_pattern.sflags = PAT_SF_TREE;
665 static_pattern.type = SMP_T_STR;
666 static_pattern.ptr.str = (char *)elt->node.key;
667 }
668 return &static_pattern;
669 }
670 }
671
672 /* look in the list */
Willy Tarreauf3045d22015-04-29 16:24:50 +0200673 if (pat_lru_tree) {
Willy Tarreauaee93142015-05-04 17:18:42 +0200674 unsigned long long seed = pat_lru_seed ^ (long)expr;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200675
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100676 HA_SPIN_LOCK(PATLRU_LOCK, &pat_lru_tree_lock);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200677 lru = lru64_get(XXH64(smp->data.u.str.str, smp->data.u.str.len, seed),
Willy Tarreauf3045d22015-04-29 16:24:50 +0200678 pat_lru_tree, expr, expr->revision);
Emeric Brunb5997f72017-07-03 11:34:05 +0200679 if (!lru) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100680 HA_SPIN_UNLOCK(PATLRU_LOCK, &pat_lru_tree_lock);
Emeric Brunb5997f72017-07-03 11:34:05 +0200681 }
682 else if (lru->domain) {
683 ret = lru->data;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100684 HA_SPIN_UNLOCK(PATLRU_LOCK, &pat_lru_tree_lock);
Emeric Brunb5997f72017-07-03 11:34:05 +0200685 return ret;
686 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200687 }
688
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100689 list_for_each_entry(lst, &expr->patterns, list) {
690 pattern = &lst->pat;
691
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200692 if (pattern->len > smp->data.u.str.len)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100693 continue;
694
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200695 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200696 if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.str.str, pattern->len) != 0) ||
697 (!icase && strncmp(pattern->ptr.str, smp->data.u.str.str, pattern->len) != 0))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100698 continue;
699
Willy Tarreauf3045d22015-04-29 16:24:50 +0200700 ret = pattern;
701 break;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100702 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200703
Emeric Brunb5997f72017-07-03 11:34:05 +0200704 if (lru) {
705 lru64_commit(lru, ret, expr, expr->revision, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100706 HA_SPIN_UNLOCK(PATLRU_LOCK, &pat_lru_tree_lock);
Emeric Brunb5997f72017-07-03 11:34:05 +0200707 }
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 matches the end of the tested string. */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100713struct pattern *pat_match_end(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100714{
715 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100716 struct pattern_list *lst;
717 struct pattern *pattern;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200718 struct pattern *ret = NULL;
719 struct lru64 *lru = NULL;
720
721 if (pat_lru_tree) {
Willy Tarreauaee93142015-05-04 17:18:42 +0200722 unsigned long long seed = pat_lru_seed ^ (long)expr;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200723
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100724 HA_SPIN_LOCK(PATLRU_LOCK, &pat_lru_tree_lock);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200725 lru = lru64_get(XXH64(smp->data.u.str.str, smp->data.u.str.len, seed),
Willy Tarreauf3045d22015-04-29 16:24:50 +0200726 pat_lru_tree, expr, expr->revision);
Emeric Brunb5997f72017-07-03 11:34:05 +0200727 if (!lru) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100728 HA_SPIN_UNLOCK(PATLRU_LOCK, &pat_lru_tree_lock);
Emeric Brunb5997f72017-07-03 11:34:05 +0200729 }
730 else if (lru->domain) {
731 ret = lru->data;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100732 HA_SPIN_UNLOCK(PATLRU_LOCK, &pat_lru_tree_lock);
Emeric Brunb5997f72017-07-03 11:34:05 +0200733 return ret;
734 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200735 }
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100736
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100737 list_for_each_entry(lst, &expr->patterns, list) {
738 pattern = &lst->pat;
739
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200740 if (pattern->len > smp->data.u.str.len)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100741 continue;
742
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200743 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200744 if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.str.str + smp->data.u.str.len - pattern->len, pattern->len) != 0) ||
745 (!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 +0100746 continue;
747
Willy Tarreauf3045d22015-04-29 16:24:50 +0200748 ret = pattern;
749 break;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100750 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200751
Emeric Brunb5997f72017-07-03 11:34:05 +0200752 if (lru) {
753 lru64_commit(lru, ret, expr, expr->revision, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100754 HA_SPIN_UNLOCK(PATLRU_LOCK, &pat_lru_tree_lock);
Emeric Brunb5997f72017-07-03 11:34:05 +0200755 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200756
757 return ret;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100758}
759
760/* Checks that the pattern is included inside the tested string.
761 * NB: Suboptimal, should be rewritten using a Boyer-Moore method.
762 */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100763struct pattern *pat_match_sub(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100764{
765 int icase;
766 char *end;
767 char *c;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100768 struct pattern_list *lst;
769 struct pattern *pattern;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200770 struct pattern *ret = NULL;
771 struct lru64 *lru = NULL;
772
773 if (pat_lru_tree) {
Willy Tarreauaee93142015-05-04 17:18:42 +0200774 unsigned long long seed = pat_lru_seed ^ (long)expr;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200775
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100776 HA_SPIN_LOCK(PATLRU_LOCK, &pat_lru_tree_lock);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200777 lru = lru64_get(XXH64(smp->data.u.str.str, smp->data.u.str.len, seed),
Willy Tarreauf3045d22015-04-29 16:24:50 +0200778 pat_lru_tree, expr, expr->revision);
Emeric Brunb5997f72017-07-03 11:34:05 +0200779 if (!lru) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100780 HA_SPIN_UNLOCK(PATLRU_LOCK, &pat_lru_tree_lock);
Emeric Brunb5997f72017-07-03 11:34:05 +0200781 }
782 else if (lru->domain) {
783 ret = lru->data;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100784 HA_SPIN_UNLOCK(PATLRU_LOCK, &pat_lru_tree_lock);
Emeric Brunb5997f72017-07-03 11:34:05 +0200785 return ret;
786 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200787 }
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100788
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100789 list_for_each_entry(lst, &expr->patterns, list) {
790 pattern = &lst->pat;
791
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200792 if (pattern->len > smp->data.u.str.len)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100793 continue;
794
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200795 end = smp->data.u.str.str + smp->data.u.str.len - pattern->len;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200796 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100797 if (icase) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200798 for (c = smp->data.u.str.str; c <= end; c++) {
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100799 if (tolower(*c) != tolower(*pattern->ptr.str))
800 continue;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200801 if (strncasecmp(pattern->ptr.str, c, pattern->len) == 0) {
802 ret = pattern;
803 goto leave;
804 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100805 }
806 } else {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200807 for (c = smp->data.u.str.str; c <= end; c++) {
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100808 if (*c != *pattern->ptr.str)
809 continue;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200810 if (strncmp(pattern->ptr.str, c, pattern->len) == 0) {
811 ret = pattern;
812 goto leave;
813 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100814 }
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100815 }
816 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200817 leave:
Emeric Brunb5997f72017-07-03 11:34:05 +0200818 if (lru) {
819 lru64_commit(lru, ret, expr, expr->revision, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100820 HA_SPIN_UNLOCK(PATLRU_LOCK, &pat_lru_tree_lock);
Emeric Brunb5997f72017-07-03 11:34:05 +0200821 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200822
823 return ret;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100824}
825
826/* This one is used by other real functions. It checks that the pattern is
827 * included inside the tested string, but enclosed between the specified
828 * delimiters or at the beginning or end of the string. The delimiters are
829 * provided as an unsigned int made by make_4delim() and match up to 4 different
830 * delimiters. Delimiters are stripped at the beginning and end of the pattern.
831 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200832static int match_word(struct sample *smp, struct pattern *pattern, int mflags, unsigned int delimiters)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100833{
834 int may_match, icase;
835 char *c, *end;
836 char *ps;
837 int pl;
838
839 pl = pattern->len;
840 ps = pattern->ptr.str;
841
842 while (pl > 0 && is_delimiter(*ps, delimiters)) {
843 pl--;
844 ps++;
845 }
846
847 while (pl > 0 && is_delimiter(ps[pl - 1], delimiters))
848 pl--;
849
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200850 if (pl > smp->data.u.str.len)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100851 return PAT_NOMATCH;
852
853 may_match = 1;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200854 icase = mflags & PAT_MF_IGNORE_CASE;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200855 end = smp->data.u.str.str + smp->data.u.str.len - pl;
856 for (c = smp->data.u.str.str; c <= end; c++) {
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100857 if (is_delimiter(*c, delimiters)) {
858 may_match = 1;
859 continue;
860 }
861
862 if (!may_match)
863 continue;
864
865 if (icase) {
866 if ((tolower(*c) == tolower(*ps)) &&
867 (strncasecmp(ps, c, pl) == 0) &&
868 (c == end || is_delimiter(c[pl], delimiters)))
869 return PAT_MATCH;
870 } else {
871 if ((*c == *ps) &&
872 (strncmp(ps, c, pl) == 0) &&
873 (c == end || is_delimiter(c[pl], delimiters)))
874 return PAT_MATCH;
875 }
876 may_match = 0;
877 }
878 return PAT_NOMATCH;
879}
880
881/* Checks that the pattern is included inside the tested string, but enclosed
882 * between the delimiters '?' or '/' or at the beginning or end of the string.
883 * Delimiters at the beginning or end of the pattern are ignored.
884 */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100885struct pattern *pat_match_dir(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100886{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100887 struct pattern_list *lst;
888 struct pattern *pattern;
889
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100890 list_for_each_entry(lst, &expr->patterns, list) {
891 pattern = &lst->pat;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200892 if (match_word(smp, pattern, expr->mflags, make_4delim('/', '?', '?', '?')))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100893 return pattern;
894 }
895 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100896}
897
898/* Checks that the pattern is included inside the tested string, but enclosed
899 * between the delmiters '/', '?', '.' or ":" or at the beginning or end of
900 * the string. Delimiters at the beginning or end of the pattern are ignored.
901 */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100902struct pattern *pat_match_dom(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100903{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100904 struct pattern_list *lst;
905 struct pattern *pattern;
906
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100907 list_for_each_entry(lst, &expr->patterns, list) {
908 pattern = &lst->pat;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200909 if (match_word(smp, pattern, expr->mflags, make_4delim('/', '?', '.', ':')))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100910 return pattern;
911 }
912 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100913}
914
915/* Checks that the integer in <test> is included between min and max */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100916struct pattern *pat_match_int(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100917{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100918 struct pattern_list *lst;
919 struct pattern *pattern;
920
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100921 list_for_each_entry(lst, &expr->patterns, list) {
922 pattern = &lst->pat;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200923 if ((!pattern->val.range.min_set || pattern->val.range.min <= smp->data.u.sint) &&
924 (!pattern->val.range.max_set || smp->data.u.sint <= pattern->val.range.max))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100925 return pattern;
926 }
927 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100928}
929
930/* Checks that the length of the pattern in <test> is included between min and max */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100931struct pattern *pat_match_len(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100932{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100933 struct pattern_list *lst;
934 struct pattern *pattern;
935
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100936 list_for_each_entry(lst, &expr->patterns, list) {
937 pattern = &lst->pat;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200938 if ((!pattern->val.range.min_set || pattern->val.range.min <= smp->data.u.str.len) &&
939 (!pattern->val.range.max_set || smp->data.u.str.len <= pattern->val.range.max))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100940 return pattern;
941 }
942 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100943}
944
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100945struct pattern *pat_match_ip(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100946{
947 unsigned int v4; /* in network byte order */
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100948 struct in6_addr tmp6;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100949 struct in_addr *s;
950 struct ebmb_node *node;
951 struct pattern_tree *elt;
952 struct pattern_list *lst;
953 struct pattern *pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100954
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100955 /* The input sample is IPv4. Try to match in the trees. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200956 if (smp->data.type == SMP_T_IPV4) {
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100957 /* Lookup an IPv4 address in the expression's pattern tree using
958 * the longest match method.
959 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200960 s = &smp->data.u.ipv4;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100961 node = ebmb_lookup_longest(&expr->pattern_tree, &s->s_addr);
962 if (node) {
963 if (fill) {
964 elt = ebmb_entry(node, struct pattern_tree, node);
Thierry FOURNIER503bb092015-08-19 08:35:43 +0200965 static_pattern.data = elt->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +0100966 static_pattern.ref = elt->ref;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200967 static_pattern.sflags = PAT_SF_TREE;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100968 static_pattern.type = SMP_T_IPV4;
969 memcpy(&static_pattern.val.ipv4.addr.s_addr, elt->node.key, 4);
970 if (!cidr2dotted(elt->node.node.pfx, &static_pattern.val.ipv4.mask))
971 return NULL;
972 }
973 return &static_pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100974 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100975
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100976 /* The IPv4 sample dont match the IPv4 tree. Convert the IPv4
977 * sample address to IPv6 with the mapping method using the ::ffff:
978 * prefix, and try to lookup in the IPv6 tree.
979 */
980 memset(&tmp6, 0, 10);
981 *(uint16_t*)&tmp6.s6_addr[10] = htons(0xffff);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200982 *(uint32_t*)&tmp6.s6_addr[12] = smp->data.u.ipv4.s_addr;
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100983 node = ebmb_lookup_longest(&expr->pattern_tree_2, &tmp6);
984 if (node) {
985 if (fill) {
986 elt = ebmb_entry(node, struct pattern_tree, node);
Thierry FOURNIER503bb092015-08-19 08:35:43 +0200987 static_pattern.data = elt->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +0100988 static_pattern.ref = elt->ref;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200989 static_pattern.sflags = PAT_SF_TREE;
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100990 static_pattern.type = SMP_T_IPV6;
991 memcpy(&static_pattern.val.ipv6.addr, elt->node.key, 16);
992 static_pattern.val.ipv6.mask = elt->node.node.pfx;
993 }
994 return &static_pattern;
995 }
996 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100997
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100998 /* The input sample is IPv6. Try to match in the trees. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200999 if (smp->data.type == SMP_T_IPV6) {
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001000 /* Lookup an IPv6 address in the expression's pattern tree using
1001 * the longest match method.
1002 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001003 node = ebmb_lookup_longest(&expr->pattern_tree_2, &smp->data.u.ipv6);
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001004 if (node) {
1005 if (fill) {
1006 elt = ebmb_entry(node, struct pattern_tree, node);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001007 static_pattern.data = elt->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01001008 static_pattern.ref = elt->ref;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02001009 static_pattern.sflags = PAT_SF_TREE;
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001010 static_pattern.type = SMP_T_IPV6;
1011 memcpy(&static_pattern.val.ipv6.addr, elt->node.key, 16);
1012 static_pattern.val.ipv6.mask = elt->node.node.pfx;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +01001013 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001014 return &static_pattern;
1015 }
1016
1017 /* Try to convert 6 to 4 when the start of the ipv6 address match the
1018 * following forms :
1019 * - ::ffff:ip:v4 (ipv4 mapped)
1020 * - ::0000:ip:v4 (old ipv4 mapped)
1021 * - 2002:ip:v4:: (6to4)
1022 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001023 if ((*(uint32_t*)&smp->data.u.ipv6.s6_addr[0] == 0 &&
1024 *(uint32_t*)&smp->data.u.ipv6.s6_addr[4] == 0 &&
1025 (*(uint32_t*)&smp->data.u.ipv6.s6_addr[8] == 0 ||
1026 *(uint32_t*)&smp->data.u.ipv6.s6_addr[8] == htonl(0xFFFF))) ||
1027 *(uint16_t*)&smp->data.u.ipv6.s6_addr[0] == htons(0x2002)) {
1028 if (*(uint32_t*)&smp->data.u.ipv6.s6_addr[0] == 0)
1029 v4 = *(uint32_t*)&smp->data.u.ipv6.s6_addr[12];
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001030 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001031 v4 = htonl((ntohs(*(uint16_t*)&smp->data.u.ipv6.s6_addr[2]) << 16) +
1032 ntohs(*(uint16_t*)&smp->data.u.ipv6.s6_addr[4]));
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001033
1034 /* Lookup an IPv4 address in the expression's pattern tree using the longest
1035 * match method.
1036 */
1037 node = ebmb_lookup_longest(&expr->pattern_tree, &v4);
1038 if (node) {
1039 if (fill) {
1040 elt = ebmb_entry(node, struct pattern_tree, node);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001041 static_pattern.data = elt->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01001042 static_pattern.ref = elt->ref;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02001043 static_pattern.sflags = PAT_SF_TREE;
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001044 static_pattern.type = SMP_T_IPV4;
1045 memcpy(&static_pattern.val.ipv4.addr.s_addr, elt->node.key, 4);
1046 if (!cidr2dotted(elt->node.node.pfx, &static_pattern.val.ipv4.mask))
1047 return NULL;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01001048 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001049 return &static_pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +01001050 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001051 }
1052 }
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +01001053
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001054 /* Lookup in the list. the list contain only IPv4 patterns */
1055 list_for_each_entry(lst, &expr->patterns, list) {
1056 pattern = &lst->pat;
1057
1058 /* The input sample is IPv4, use it as is. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001059 if (smp->data.type == SMP_T_IPV4) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001060 v4 = smp->data.u.ipv4.s_addr;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +01001061 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001062 else if (smp->data.type == SMP_T_IPV6) {
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001063 /* v4 match on a V6 sample. We want to check at least for
1064 * the following forms :
1065 * - ::ffff:ip:v4 (ipv4 mapped)
1066 * - ::0000:ip:v4 (old ipv4 mapped)
1067 * - 2002:ip:v4:: (6to4)
1068 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001069 if (*(uint32_t*)&smp->data.u.ipv6.s6_addr[0] == 0 &&
1070 *(uint32_t*)&smp->data.u.ipv6.s6_addr[4] == 0 &&
1071 (*(uint32_t*)&smp->data.u.ipv6.s6_addr[8] == 0 ||
1072 *(uint32_t*)&smp->data.u.ipv6.s6_addr[8] == htonl(0xFFFF))) {
1073 v4 = *(uint32_t*)&smp->data.u.ipv6.s6_addr[12];
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01001074 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001075 else if (*(uint16_t*)&smp->data.u.ipv6.s6_addr[0] == htons(0x2002)) {
1076 v4 = htonl((ntohs(*(uint16_t*)&smp->data.u.ipv6.s6_addr[2]) << 16) +
1077 ntohs(*(uint16_t*)&smp->data.u.ipv6.s6_addr[4]));
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01001078 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001079 else
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01001080 continue;
Andreas Seltenreichf0653192016-03-03 20:08:35 +01001081 } else {
1082 /* impossible */
1083 continue;
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001084 }
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +01001085
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001086 /* Check if the input sample match the current pattern. */
1087 if (((v4 ^ pattern->val.ipv4.addr.s_addr) & pattern->val.ipv4.mask.s_addr) == 0)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01001088 return pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +01001089 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01001090 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +01001091}
1092
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001093void free_pattern_tree(struct eb_root *root)
1094{
1095 struct eb_node *node, *next;
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +01001096 struct pattern_tree *elt;
Thierry FOURNIER3ce88c72013-12-09 11:29:46 +01001097
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001098 node = eb_first(root);
1099 while (node) {
1100 next = eb_next(node);
1101 eb_delete(node);
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +01001102 elt = container_of(node, struct pattern_tree, node);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001103 free(elt->data);
Thierry FOURNIER3ce88c72013-12-09 11:29:46 +01001104 free(elt);
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001105 node = next;
1106 }
1107}
1108
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01001109void pat_prune_val(struct pattern_expr *expr)
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +01001110{
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01001111 struct pattern_list *pat, *tmp;
1112
1113 list_for_each_entry_safe(pat, tmp, &expr->patterns, list) {
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001114 free(pat->pat.data);
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01001115 free(pat);
1116 }
1117
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +01001118 free_pattern_tree(&expr->pattern_tree);
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001119 free_pattern_tree(&expr->pattern_tree_2);
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +01001120 LIST_INIT(&expr->patterns);
1121}
1122
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01001123void pat_prune_ptr(struct pattern_expr *expr)
1124{
1125 struct pattern_list *pat, *tmp;
1126
1127 list_for_each_entry_safe(pat, tmp, &expr->patterns, list) {
1128 free(pat->pat.ptr.ptr);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001129 free(pat->pat.data);
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01001130 free(pat);
1131 }
1132
1133 free_pattern_tree(&expr->pattern_tree);
1134 free_pattern_tree(&expr->pattern_tree_2);
1135 LIST_INIT(&expr->patterns);
1136}
1137
1138void pat_prune_reg(struct pattern_expr *expr)
1139{
1140 struct pattern_list *pat, *tmp;
1141
1142 list_for_each_entry_safe(pat, tmp, &expr->patterns, list) {
1143 regex_free(pat->pat.ptr.ptr);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001144 free(pat->pat.data);
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01001145 free(pat);
1146 }
1147
1148 free_pattern_tree(&expr->pattern_tree);
1149 free_pattern_tree(&expr->pattern_tree_2);
1150 LIST_INIT(&expr->patterns);
1151}
1152
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001153/*
1154 *
1155 * The following functions are used for the pattern indexation
1156 *
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001157 */
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001158
1159int pat_idx_list_val(struct pattern_expr *expr, struct pattern *pat, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001160{
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001161 struct pattern_list *patl;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001162
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001163 /* allocate pattern */
1164 patl = calloc(1, sizeof(*patl));
1165 if (!patl) {
1166 memprintf(err, "out of memory while indexing pattern");
Thierry FOURNIER972028f2014-01-23 17:53:31 +01001167 return 0;
1168 }
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001169
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001170 /* duplicate pattern */
1171 memcpy(&patl->pat, pat, sizeof(*pat));
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001172
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001173 /* chain pattern in the expression */
1174 LIST_ADDQ(&expr->patterns, &patl->list);
Willy Tarreau72f073b2015-04-29 17:53:47 +02001175 expr->revision = rdtsc();
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001176
1177 /* that's ok */
1178 return 1;
1179}
1180
1181int pat_idx_list_ptr(struct pattern_expr *expr, struct pattern *pat, char **err)
1182{
1183 struct pattern_list *patl;
1184
1185 /* allocate pattern */
1186 patl = calloc(1, sizeof(*patl));
Thierry FOURNIER8aa83842015-02-06 17:50:55 +01001187 if (!patl) {
1188 memprintf(err, "out of memory while indexing pattern");
Thierry FOURNIER972028f2014-01-23 17:53:31 +01001189 return 0;
Thierry FOURNIER8aa83842015-02-06 17:50:55 +01001190 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001191
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001192 /* duplicate pattern */
1193 memcpy(&patl->pat, pat, sizeof(*pat));
1194 patl->pat.ptr.ptr = malloc(patl->pat.len);
1195 if (!patl->pat.ptr.ptr) {
1196 free(patl);
1197 memprintf(err, "out of memory while indexing pattern");
1198 return 0;
1199 }
1200 memcpy(patl->pat.ptr.ptr, pat->ptr.ptr, pat->len);
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001201
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001202 /* chain pattern in the expression */
1203 LIST_ADDQ(&expr->patterns, &patl->list);
Willy Tarreau72f073b2015-04-29 17:53:47 +02001204 expr->revision = rdtsc();
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001205
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001206 /* that's ok */
1207 return 1;
1208}
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001209
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001210int pat_idx_list_str(struct pattern_expr *expr, struct pattern *pat, char **err)
1211{
1212 struct pattern_list *patl;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001213
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001214 /* allocate pattern */
1215 patl = calloc(1, sizeof(*patl));
1216 if (!patl) {
1217 memprintf(err, "out of memory while indexing pattern");
1218 return 0;
1219 }
1220
1221 /* duplicate pattern */
1222 memcpy(&patl->pat, pat, sizeof(*pat));
1223 patl->pat.ptr.str = malloc(patl->pat.len + 1);
1224 if (!patl->pat.ptr.str) {
1225 free(patl);
1226 memprintf(err, "out of memory while indexing pattern");
1227 return 0;
1228 }
1229 memcpy(patl->pat.ptr.ptr, pat->ptr.ptr, pat->len);
1230 patl->pat.ptr.str[patl->pat.len] = '\0';
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001231
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001232 /* chain pattern in the expression */
1233 LIST_ADDQ(&expr->patterns, &patl->list);
Willy Tarreau72f073b2015-04-29 17:53:47 +02001234 expr->revision = rdtsc();
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001235
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001236 /* that's ok */
1237 return 1;
1238}
1239
Thierry Fournier8feaa662016-02-10 22:55:20 +01001240int pat_idx_list_reg_cap(struct pattern_expr *expr, struct pattern *pat, int cap, char **err)
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001241{
1242 struct pattern_list *patl;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001243
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001244 /* allocate pattern */
1245 patl = calloc(1, sizeof(*patl));
1246 if (!patl) {
1247 memprintf(err, "out of memory while indexing pattern");
1248 return 0;
Thierry FOURNIER972028f2014-01-23 17:53:31 +01001249 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001250
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001251 /* duplicate pattern */
1252 memcpy(&patl->pat, pat, sizeof(*pat));
1253
1254 /* allocate regex */
1255 patl->pat.ptr.reg = calloc(1, sizeof(*patl->pat.ptr.reg));
1256 if (!patl->pat.ptr.reg) {
1257 free(patl);
1258 memprintf(err, "out of memory while indexing pattern");
1259 return 0;
1260 }
1261
1262 /* compile regex */
Thierry Fournier8feaa662016-02-10 22:55:20 +01001263 if (!regex_comp(pat->ptr.str, patl->pat.ptr.reg,
1264 !(expr->mflags & PAT_MF_IGNORE_CASE), cap, err)) {
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001265 free(patl->pat.ptr.reg);
Dirkjan Bussink07fcaaa2014-04-28 22:57:16 +00001266 free(patl);
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001267 return 0;
1268 }
1269
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001270 /* chain pattern in the expression */
1271 LIST_ADDQ(&expr->patterns, &patl->list);
Willy Tarreau72f073b2015-04-29 17:53:47 +02001272 expr->revision = rdtsc();
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001273
1274 /* that's ok */
1275 return 1;
1276}
1277
Thierry Fournier8feaa662016-02-10 22:55:20 +01001278int pat_idx_list_reg(struct pattern_expr *expr, struct pattern *pat, char **err)
1279{
1280 return pat_idx_list_reg_cap(expr, pat, 0, err);
1281}
1282
1283int pat_idx_list_regm(struct pattern_expr *expr, struct pattern *pat, char **err)
1284{
1285 return pat_idx_list_reg_cap(expr, pat, 1, err);
1286}
1287
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001288int pat_idx_tree_ip(struct pattern_expr *expr, struct pattern *pat, char **err)
1289{
1290 unsigned int mask;
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +01001291 struct pattern_tree *node;
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001292
1293 /* Only IPv4 can be indexed */
1294 if (pat->type == SMP_T_IPV4) {
Thierry FOURNIER972028f2014-01-23 17:53:31 +01001295 /* in IPv4 case, check if the mask is contiguous so that we can
1296 * insert the network into the tree. A continuous mask has only
1297 * ones on the left. This means that this mask + its lower bit
1298 * added once again is null.
1299 */
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001300 mask = ntohl(pat->val.ipv4.mask.s_addr);
1301 if (mask + (mask & -mask) == 0) {
1302 mask = mask ? 33 - flsnz(mask & -mask) : 0; /* equals cidr value */
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001303
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001304 /* node memory allocation */
1305 node = calloc(1, sizeof(*node) + 4);
1306 if (!node) {
1307 memprintf(err, "out of memory while loading pattern");
1308 return 0;
1309 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001310
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001311 /* copy the pointer to sample associated to this node */
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001312 node->data = pat->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01001313 node->ref = pat->ref;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001314
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001315 /* FIXME: insert <addr>/<mask> into the tree here */
1316 memcpy(node->node.key, &pat->val.ipv4.addr, 4); /* network byte order */
1317 node->node.node.pfx = mask;
Thierry FOURNIER31db4ae2014-01-30 00:27:15 +01001318
1319 /* Insert the entry. */
1320 ebmb_insert_prefix(&expr->pattern_tree, &node->node, 4);
Willy Tarreau72f073b2015-04-29 17:53:47 +02001321 expr->revision = rdtsc();
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001322
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001323 /* that's ok */
1324 return 1;
1325 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001326 else {
1327 /* If the mask is not contiguous, just add the pattern to the list */
1328 return pat_idx_list_val(expr, pat, err);
1329 }
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001330 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001331 else if (pat->type == SMP_T_IPV6) {
1332 /* IPv6 also can be indexed */
1333 node = calloc(1, sizeof(*node) + 16);
1334 if (!node) {
1335 memprintf(err, "out of memory while loading pattern");
1336 return 0;
1337 }
1338
1339 /* copy the pointer to sample associated to this node */
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001340 node->data = pat->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01001341 node->ref = pat->ref;
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001342
1343 /* FIXME: insert <addr>/<mask> into the tree here */
1344 memcpy(node->node.key, &pat->val.ipv6.addr, 16); /* network byte order */
1345 node->node.node.pfx = pat->val.ipv6.mask;
Thierry FOURNIER31db4ae2014-01-30 00:27:15 +01001346
1347 /* Insert the entry. */
1348 ebmb_insert_prefix(&expr->pattern_tree_2, &node->node, 16);
Willy Tarreau72f073b2015-04-29 17:53:47 +02001349 expr->revision = rdtsc();
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001350
1351 /* that's ok */
1352 return 1;
1353 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001354
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001355 return 0;
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001356}
1357
1358int pat_idx_tree_str(struct pattern_expr *expr, struct pattern *pat, char **err)
1359{
1360 int len;
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +01001361 struct pattern_tree *node;
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001362
1363 /* Only string can be indexed */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001364 if (pat->type != SMP_T_STR) {
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001365 memprintf(err, "internal error: string expected, but the type is '%s'",
1366 smp_to_type[pat->type]);
1367 return 0;
Thierry FOURNIER972028f2014-01-23 17:53:31 +01001368 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001369
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001370 /* If the flag PAT_F_IGNORE_CASE is set, we cannot use trees */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02001371 if (expr->mflags & PAT_MF_IGNORE_CASE)
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001372 return pat_idx_list_str(expr, pat, err);
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001373
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001374 /* Process the key len */
1375 len = strlen(pat->ptr.str) + 1;
1376
1377 /* node memory allocation */
1378 node = calloc(1, sizeof(*node) + len);
1379 if (!node) {
1380 memprintf(err, "out of memory while loading pattern");
1381 return 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001382 }
1383
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001384 /* copy the pointer to sample associated to this node */
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001385 node->data = pat->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01001386 node->ref = pat->ref;
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001387
1388 /* copy the string */
1389 memcpy(node->node.key, pat->ptr.str, len);
1390
1391 /* index the new node */
Thierry FOURNIER31db4ae2014-01-30 00:27:15 +01001392 ebst_insert(&expr->pattern_tree, &node->node);
Willy Tarreau72f073b2015-04-29 17:53:47 +02001393 expr->revision = rdtsc();
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001394
1395 /* that's ok */
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001396 return 1;
1397}
1398
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +02001399int pat_idx_tree_pfx(struct pattern_expr *expr, struct pattern *pat, char **err)
1400{
1401 int len;
1402 struct pattern_tree *node;
1403
1404 /* Only string can be indexed */
1405 if (pat->type != SMP_T_STR) {
1406 memprintf(err, "internal error: string expected, but the type is '%s'",
1407 smp_to_type[pat->type]);
1408 return 0;
1409 }
1410
1411 /* If the flag PAT_F_IGNORE_CASE is set, we cannot use trees */
1412 if (expr->mflags & PAT_MF_IGNORE_CASE)
1413 return pat_idx_list_str(expr, pat, err);
1414
1415 /* Process the key len */
1416 len = strlen(pat->ptr.str);
1417
1418 /* node memory allocation */
1419 node = calloc(1, sizeof(*node) + len + 1);
1420 if (!node) {
1421 memprintf(err, "out of memory while loading pattern");
1422 return 0;
1423 }
1424
1425 /* copy the pointer to sample associated to this node */
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001426 node->data = pat->data;
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +02001427 node->ref = pat->ref;
1428
1429 /* copy the string and the trailing zero */
1430 memcpy(node->node.key, pat->ptr.str, len + 1);
1431 node->node.node.pfx = len * 8;
1432
1433 /* index the new node */
1434 ebmb_insert_prefix(&expr->pattern_tree, &node->node, len);
Willy Tarreau72f073b2015-04-29 17:53:47 +02001435 expr->revision = rdtsc();
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +02001436
1437 /* that's ok */
1438 return 1;
1439}
1440
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001441void pat_del_list_val(struct pattern_expr *expr, struct pat_ref_elt *ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001442{
1443 struct pattern_list *pat;
1444 struct pattern_list *safe;
1445
1446 list_for_each_entry_safe(pat, safe, &expr->patterns, list) {
1447 /* Check equality. */
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001448 if (pat->pat.ref != ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001449 continue;
1450
1451 /* Delete and free entry. */
1452 LIST_DEL(&pat->list);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001453 free(pat->pat.data);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001454 free(pat);
1455 }
Willy Tarreau72f073b2015-04-29 17:53:47 +02001456 expr->revision = rdtsc();
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001457}
1458
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001459void pat_del_tree_ip(struct pattern_expr *expr, struct pat_ref_elt *ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001460{
1461 struct ebmb_node *node, *next_node;
1462 struct pattern_tree *elt;
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001463
1464 /* browse each node of the tree for IPv4 addresses. */
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001465 for (node = ebmb_first(&expr->pattern_tree), next_node = node ? ebmb_next(node) : NULL;
1466 node;
1467 node = next_node, next_node = node ? ebmb_next(node) : NULL) {
1468 /* Extract container of the tree node. */
1469 elt = container_of(node, struct pattern_tree, node);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001470
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001471 /* Check equality. */
1472 if (elt->ref != ref)
1473 continue;
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001474
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001475 /* Delete and free entry. */
1476 ebmb_delete(node);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001477 free(elt->data);
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001478 free(elt);
1479 }
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001480
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001481 /* Browse each node of the list for IPv4 addresses. */
1482 pat_del_list_val(expr, ref);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001483
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001484 /* browse each node of the tree for IPv6 addresses. */
1485 for (node = ebmb_first(&expr->pattern_tree_2), next_node = node ? ebmb_next(node) : NULL;
1486 node;
1487 node = next_node, next_node = node ? ebmb_next(node) : NULL) {
1488 /* Extract container of the tree node. */
1489 elt = container_of(node, struct pattern_tree, node);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001490
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001491 /* Check equality. */
1492 if (elt->ref != ref)
1493 continue;
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001494
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001495 /* Delete and free entry. */
1496 ebmb_delete(node);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001497 free(elt->data);
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001498 free(elt);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001499 }
Willy Tarreau72f073b2015-04-29 17:53:47 +02001500 expr->revision = rdtsc();
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001501}
1502
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001503void pat_del_list_ptr(struct pattern_expr *expr, struct pat_ref_elt *ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001504{
1505 struct pattern_list *pat;
1506 struct pattern_list *safe;
1507
1508 list_for_each_entry_safe(pat, safe, &expr->patterns, list) {
1509 /* Check equality. */
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001510 if (pat->pat.ref != ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001511 continue;
1512
1513 /* Delete and free entry. */
1514 LIST_DEL(&pat->list);
1515 free(pat->pat.ptr.ptr);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001516 free(pat->pat.data);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001517 free(pat);
1518 }
Willy Tarreau72f073b2015-04-29 17:53:47 +02001519 expr->revision = rdtsc();
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001520}
1521
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001522void pat_del_tree_str(struct pattern_expr *expr, struct pat_ref_elt *ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001523{
1524 struct ebmb_node *node, *next_node;
1525 struct pattern_tree *elt;
1526
Thierry FOURNIER73bc2852015-02-06 17:53:54 +01001527 /* If the flag PAT_F_IGNORE_CASE is set, we cannot use trees */
1528 if (expr->mflags & PAT_MF_IGNORE_CASE)
1529 return pat_del_list_ptr(expr, ref);
1530
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001531 /* browse each node of the tree. */
1532 for (node = ebmb_first(&expr->pattern_tree), next_node = node ? ebmb_next(node) : NULL;
1533 node;
1534 node = next_node, next_node = node ? ebmb_next(node) : NULL) {
1535 /* Extract container of the tree node. */
1536 elt = container_of(node, struct pattern_tree, node);
1537
1538 /* Check equality. */
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001539 if (elt->ref != ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001540 continue;
1541
1542 /* Delete and free entry. */
1543 ebmb_delete(node);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001544 free(elt->data);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001545 free(elt);
1546 }
Willy Tarreau72f073b2015-04-29 17:53:47 +02001547 expr->revision = rdtsc();
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001548}
1549
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001550void pat_del_list_reg(struct pattern_expr *expr, struct pat_ref_elt *ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001551{
1552 struct pattern_list *pat;
1553 struct pattern_list *safe;
1554
1555 list_for_each_entry_safe(pat, safe, &expr->patterns, list) {
1556 /* Check equality. */
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001557 if (pat->pat.ref != ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001558 continue;
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001559
1560 /* Delete and free entry. */
1561 LIST_DEL(&pat->list);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001562 regex_free(pat->pat.ptr.ptr);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001563 free(pat->pat.data);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001564 free(pat);
1565 }
Willy Tarreau72f073b2015-04-29 17:53:47 +02001566 expr->revision = rdtsc();
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001567}
1568
1569void pattern_init_expr(struct pattern_expr *expr)
1570{
1571 LIST_INIT(&expr->patterns);
Willy Tarreau72f073b2015-04-29 17:53:47 +02001572 expr->revision = 0;
Thierry FOURNIER31db4ae2014-01-30 00:27:15 +01001573 expr->pattern_tree = EB_ROOT;
1574 expr->pattern_tree_2 = EB_ROOT;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001575}
1576
1577void pattern_init_head(struct pattern_head *head)
1578{
1579 LIST_INIT(&head->head);
1580}
1581
1582/* The following functions are relative to the management of the reference
1583 * lists. These lists are used to store the original pattern and associated
1584 * value as string form.
1585 *
1586 * This is used with modifiable ACL and MAPS
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001587 *
1588 * The pattern reference are stored with two identifiers: the unique_id and
1589 * the reference.
1590 *
1591 * The reference identify a file. Each file with the same name point to the
1592 * same reference. We can register many times one file. If the file is modified,
1593 * all his dependencies are also modified. The reference can be used with map or
1594 * acl.
1595 *
1596 * The unique_id identify inline acl. The unique id is unique for each acl.
1597 * You cannot force the same id in the configuration file, because this repoort
1598 * an error.
1599 *
1600 * A particular case appears if the filename is a number. In this case, the
1601 * unique_id is set with the number represented by the filename and the
1602 * reference is also set. This method prevent double unique_id.
1603 *
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001604 */
1605
1606/* This function lookup for reference. If the reference is found, they return
1607 * pointer to the struct pat_ref, else return NULL.
1608 */
1609struct pat_ref *pat_ref_lookup(const char *reference)
1610{
1611 struct pat_ref *ref;
1612
1613 list_for_each_entry(ref, &pattern_reference, list)
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001614 if (ref->reference && strcmp(reference, ref->reference) == 0)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001615 return ref;
1616 return NULL;
1617}
1618
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001619/* This function lookup for unique id. If the reference is found, they return
1620 * pointer to the struct pat_ref, else return NULL.
1621 */
1622struct pat_ref *pat_ref_lookupid(int unique_id)
1623{
1624 struct pat_ref *ref;
1625
1626 list_for_each_entry(ref, &pattern_reference, list)
1627 if (ref->unique_id == unique_id)
1628 return ref;
1629 return NULL;
1630}
1631
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001632/* This function remove all pattern matching the pointer <refelt> from
1633 * the the reference and from each expr member of the reference. This
1634 * function returns 1 if the deletion is done and return 0 is the entry
1635 * is not found.
1636 */
1637int pat_ref_delete_by_id(struct pat_ref *ref, struct pat_ref_elt *refelt)
1638{
1639 struct pattern_expr *expr;
1640 struct pat_ref_elt *elt, *safe;
Emeric Brun8d85aa42017-06-29 15:40:33 +02001641 struct bref *bref, *back;
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001642
1643 /* delete pattern from reference */
1644 list_for_each_entry_safe(elt, safe, &ref->head, list) {
1645 if (elt == refelt) {
Emeric Brun8d85aa42017-06-29 15:40:33 +02001646 list_for_each_entry_safe(bref, back, &elt->back_refs, users) {
1647 /*
1648 * we have to unlink all watchers. We must not relink them if
1649 * this elt was the last one in the list.
1650 */
1651 LIST_DEL(&bref->users);
1652 LIST_INIT(&bref->users);
1653 if (elt->list.n != &ref->head)
1654 LIST_ADDQ(&LIST_ELEM(elt->list.n, struct stream *, list)->back_refs, &bref->users);
1655 bref->ref = elt->list.n;
1656 }
peter caiaede6dd2015-10-07 00:07:43 -07001657 list_for_each_entry(expr, &ref->pat, list)
1658 pattern_delete(expr, elt);
1659
Emeric Brunb5997f72017-07-03 11:34:05 +02001660 /* pat_ref_elt is trashed once all expr
1661 are cleaned and there is no ref remaining */
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001662 LIST_DEL(&elt->list);
1663 free(elt->sample);
1664 free(elt->pattern);
1665 free(elt);
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001666 return 1;
1667 }
1668 }
1669 return 0;
1670}
1671
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001672/* This function remove all pattern match <key> from the the reference
1673 * and from each expr member of the reference. This fucntion returns 1
1674 * if the deletion is done and return 0 is the entry is not found.
1675 */
1676int pat_ref_delete(struct pat_ref *ref, const char *key)
1677{
1678 struct pattern_expr *expr;
1679 struct pat_ref_elt *elt, *safe;
Emeric Brun8d85aa42017-06-29 15:40:33 +02001680 struct bref *bref, *back;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001681 int found = 0;
1682
1683 /* delete pattern from reference */
1684 list_for_each_entry_safe(elt, safe, &ref->head, list) {
1685 if (strcmp(key, elt->pattern) == 0) {
Emeric Brun8d85aa42017-06-29 15:40:33 +02001686 list_for_each_entry_safe(bref, back, &elt->back_refs, users) {
1687 /*
1688 * we have to unlink all watchers. We must not relink them if
1689 * this elt was the last one in the list.
1690 */
1691 LIST_DEL(&bref->users);
1692 LIST_INIT(&bref->users);
1693 if (elt->list.n != &ref->head)
1694 LIST_ADDQ(&LIST_ELEM(elt->list.n, struct stream *, list)->back_refs, &bref->users);
1695 bref->ref = elt->list.n;
1696 }
Dirkjan Bussink07fcaaa2014-04-28 22:57:16 +00001697 list_for_each_entry(expr, &ref->pat, list)
1698 pattern_delete(expr, elt);
1699
Emeric Brunb5997f72017-07-03 11:34:05 +02001700 /* pat_ref_elt is trashed once all expr
1701 are cleaned and there is no ref remaining */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001702 LIST_DEL(&elt->list);
1703 free(elt->sample);
1704 free(elt->pattern);
1705 free(elt);
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001706
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001707 found = 1;
1708 }
1709 }
1710
1711 if (!found)
1712 return 0;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001713 return 1;
1714}
1715
Baptiste Assmann953f74d2014-04-25 16:57:03 +02001716/*
1717 * find and return an element <elt> matching <key> in a reference <ref>
1718 * return NULL if not found
1719 */
1720struct pat_ref_elt *pat_ref_find_elt(struct pat_ref *ref, const char *key)
1721{
1722 struct pat_ref_elt *elt;
1723
1724 list_for_each_entry(elt, &ref->head, list) {
1725 if (strcmp(key, elt->pattern) == 0)
1726 return elt;
1727 }
1728
1729 return NULL;
1730}
1731
1732
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001733 /* This function modify the sample of the first pattern that match the <key>. */
1734static inline int pat_ref_set_elt(struct pat_ref *ref, struct pat_ref_elt *elt,
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001735 const char *value, char **err)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001736{
1737 struct pattern_expr *expr;
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001738 struct sample_data **data;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001739 char *sample;
Thierry FOURNIER12ba0c22015-08-14 00:02:11 +02001740 struct sample_data test;
Thierry FOURNIER149e0fe2014-01-29 19:35:06 +01001741
1742 /* Try all needed converters. */
1743 list_for_each_entry(expr, &ref->pat, list) {
1744 if (!expr->pat_head->parse_smp)
1745 continue;
1746
1747 if (!expr->pat_head->parse_smp(value, &test)) {
1748 memprintf(err, "unable to parse '%s'", value);
1749 return 0;
1750 }
1751 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001752
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001753 /* Modify pattern from reference. */
1754 sample = strdup(value);
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001755 if (!sample) {
1756 memprintf(err, "out of memory error");
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001757 return 0;
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001758 }
Thierry FOURNIER149e0fe2014-01-29 19:35:06 +01001759 /* Load sample in each reference. All the conversion are tested
1760 * below, normally these calls dosn't fail.
1761 */
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001762 list_for_each_entry(expr, &ref->pat, list) {
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001763 if (!expr->pat_head->parse_smp)
1764 continue;
1765
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001766 HA_RWLOCK_WRLOCK(PATEXP_LOCK, &expr->lock);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001767 data = pattern_find_smp(expr, elt);
1768 if (data && *data && !expr->pat_head->parse_smp(sample, *data))
1769 *data = NULL;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001770 HA_RWLOCK_WRUNLOCK(PATEXP_LOCK, &expr->lock);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001771 }
1772
Emeric Brunb5997f72017-07-03 11:34:05 +02001773 /* free old sample only when all exprs are updated */
1774 free(elt->sample);
1775 elt->sample = sample;
1776
1777
Thierry FOURNIER149e0fe2014-01-29 19:35:06 +01001778 return 1;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001779}
1780
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001781/* This function modify the sample of the first pattern that match the <key>. */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001782int 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 +01001783{
1784 struct pat_ref_elt *elt;
1785
1786 /* Look for pattern in the reference. */
1787 list_for_each_entry(elt, &ref->head, list) {
1788 if (elt == refelt) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001789 if (!pat_ref_set_elt(ref, elt, value, err))
1790 return 0;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001791 return 1;
1792 }
1793 }
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001794
1795 memprintf(err, "key or pattern not found");
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001796 return 0;
1797}
1798
1799/* This function modify the sample of the first pattern that match the <key>. */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001800int pat_ref_set(struct pat_ref *ref, const char *key, const char *value, char **err)
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001801{
1802 struct pat_ref_elt *elt;
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001803 int found = 0;
1804 char *_merr;
1805 char **merr;
1806
1807 if (err) {
1808 merr = &_merr;
1809 *merr = NULL;
1810 }
1811 else
1812 merr = NULL;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001813
1814 /* Look for pattern in the reference. */
1815 list_for_each_entry(elt, &ref->head, list) {
1816 if (strcmp(key, elt->pattern) == 0) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001817 if (!pat_ref_set_elt(ref, elt, value, merr)) {
1818 if (!found)
1819 *err = *merr;
1820 else {
1821 memprintf(err, "%s, %s", *err, *merr);
1822 free(*merr);
1823 *merr = NULL;
1824 }
1825 }
1826 found = 1;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001827 }
1828 }
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001829
1830 if (!found) {
1831 memprintf(err, "entry not found");
1832 return 0;
1833 }
1834 return 1;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001835}
1836
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001837/* This function create new reference. <ref> is the reference name.
1838 * <flags> are PAT_REF_*. /!\ The reference is not checked, and must
1839 * be unique. The user must check the reference with "pat_ref_lookup()"
1840 * before calling this function. If the fucntion fail, it return NULL,
1841 * else return new struct pat_ref.
1842 */
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001843struct pat_ref *pat_ref_new(const char *reference, const char *display, unsigned int flags)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001844{
1845 struct pat_ref *ref;
1846
1847 ref = malloc(sizeof(*ref));
1848 if (!ref)
1849 return NULL;
1850
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001851 if (display) {
1852 ref->display = strdup(display);
1853 if (!ref->display) {
1854 free(ref);
1855 return NULL;
1856 }
1857 }
1858 else
1859 ref->display = NULL;
1860
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001861 ref->reference = strdup(reference);
1862 if (!ref->reference) {
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001863 free(ref->display);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001864 free(ref);
1865 return NULL;
1866 }
1867
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001868 ref->flags = flags;
1869 ref->unique_id = -1;
1870
1871 LIST_INIT(&ref->head);
1872 LIST_INIT(&ref->pat);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001873 HA_SPIN_INIT(&ref->lock);
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001874 LIST_ADDQ(&pattern_reference, &ref->list);
1875
1876 return ref;
1877}
1878
1879/* This function create new reference. <unique_id> is the unique id. If
1880 * the value of <unique_id> is -1, the unique id is calculated later.
1881 * <flags> are PAT_REF_*. /!\ The reference is not checked, and must
1882 * be unique. The user must check the reference with "pat_ref_lookup()"
1883 * or pat_ref_lookupid before calling this function. If the function
1884 * fail, it return NULL, else return new struct pat_ref.
1885 */
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001886struct pat_ref *pat_ref_newid(int unique_id, const char *display, unsigned int flags)
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001887{
1888 struct pat_ref *ref;
1889
1890 ref = malloc(sizeof(*ref));
1891 if (!ref)
1892 return NULL;
1893
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001894 if (display) {
1895 ref->display = strdup(display);
1896 if (!ref->display) {
1897 free(ref);
1898 return NULL;
1899 }
1900 }
1901 else
1902 ref->display = NULL;
1903
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001904 ref->reference = NULL;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001905 ref->flags = flags;
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001906 ref->unique_id = unique_id;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001907 LIST_INIT(&ref->head);
1908 LIST_INIT(&ref->pat);
1909
1910 LIST_ADDQ(&pattern_reference, &ref->list);
1911
1912 return ref;
1913}
1914
1915/* This function adds entry to <ref>. It can failed with memory error.
1916 * If the function fails, it returns 0.
1917 */
1918int pat_ref_append(struct pat_ref *ref, char *pattern, char *sample, int line)
1919{
1920 struct pat_ref_elt *elt;
1921
1922 elt = malloc(sizeof(*elt));
1923 if (!elt)
1924 return 0;
1925
1926 elt->line = line;
1927
1928 elt->pattern = strdup(pattern);
1929 if (!elt->pattern) {
1930 free(elt);
1931 return 0;
1932 }
1933
1934 if (sample) {
1935 elt->sample = strdup(sample);
1936 if (!elt->sample) {
1937 free(elt->pattern);
1938 free(elt);
1939 return 0;
1940 }
1941 }
1942 else
1943 elt->sample = NULL;
1944
Emeric Brun8d85aa42017-06-29 15:40:33 +02001945 LIST_INIT(&elt->back_refs);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001946 LIST_ADDQ(&ref->head, &elt->list);
1947
1948 return 1;
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001949}
1950
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001951/* This function create sample found in <elt>, parse the pattern also
1952 * found in <elt> and insert it in <expr>. The function copy <patflags>
1953 * in <expr>. If the function fails, it returns0 and <err> is filled.
1954 * In succes case, the function returns 1.
1955 */
1956static inline
1957int pat_ref_push(struct pat_ref_elt *elt, struct pattern_expr *expr,
1958 int patflags, char **err)
1959{
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001960 struct sample_data *data;
Thierry FOURNIERd25c8422014-01-28 15:34:35 +01001961 struct pattern pattern;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001962
1963 /* Create sample */
1964 if (elt->sample && expr->pat_head->parse_smp) {
1965 /* New sample. */
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001966 data = malloc(sizeof(*data));
1967 if (!data)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001968 return 0;
1969
1970 /* Parse value. */
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001971 if (!expr->pat_head->parse_smp(elt->sample, data)) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001972 memprintf(err, "unable to parse '%s'", elt->sample);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001973 free(data);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001974 return 0;
1975 }
1976
1977 }
1978 else
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001979 data = NULL;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001980
Thierry FOURNIERd25c8422014-01-28 15:34:35 +01001981 /* initialise pattern */
1982 memset(&pattern, 0, sizeof(pattern));
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001983 pattern.data = data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01001984 pattern.ref = elt;
Thierry FOURNIERd25c8422014-01-28 15:34:35 +01001985
1986 /* parse pattern */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02001987 if (!expr->pat_head->parse(elt->pattern, &pattern, expr->mflags, err)) {
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001988 free(data);
Thierry FOURNIERd25c8422014-01-28 15:34:35 +01001989 return 0;
1990 }
1991
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001992 HA_RWLOCK_WRLOCK(PATEXP_LOCK, &expr->lock);
Thierry FOURNIERd25c8422014-01-28 15:34:35 +01001993 /* index pattern */
1994 if (!expr->pat_head->index(expr, &pattern, err)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001995 HA_RWLOCK_WRUNLOCK(PATEXP_LOCK, &expr->lock);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001996 free(data);
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001997 return 0;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001998 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001999 HA_RWLOCK_WRUNLOCK(PATEXP_LOCK, &expr->lock);
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01002000
2001 return 1;
2002}
2003
Thierry FOURNIER31db4ae2014-01-30 00:27:15 +01002004/* This function adds entry to <ref>. It can failed with memory error. The new
2005 * entry is added at all the pattern_expr registered in this reference. The
2006 * function stop on the first error encountered. It returns 0 and err is
2007 * filled. If an error is encountered, the complete add operation is cancelled.
2008 * If the insertion is a success the function returns 1.
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002009 */
2010int pat_ref_add(struct pat_ref *ref,
2011 const char *pattern, const char *sample,
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02002012 char **err)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002013{
2014 struct pat_ref_elt *elt;
2015 struct pattern_expr *expr;
2016
2017 elt = malloc(sizeof(*elt));
2018 if (!elt) {
2019 memprintf(err, "out of memory error");
2020 return 0;
2021 }
2022
2023 elt->line = -1;
2024
2025 elt->pattern = strdup(pattern);
2026 if (!elt->pattern) {
2027 free(elt);
2028 memprintf(err, "out of memory error");
2029 return 0;
2030 }
2031
2032 if (sample) {
2033 elt->sample = strdup(sample);
2034 if (!elt->sample) {
2035 free(elt->pattern);
2036 free(elt);
2037 memprintf(err, "out of memory error");
2038 return 0;
2039 }
2040 }
2041 else
2042 elt->sample = NULL;
2043
Emeric Brun8d85aa42017-06-29 15:40:33 +02002044 LIST_INIT(&elt->back_refs);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002045 LIST_ADDQ(&ref->head, &elt->list);
2046
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002047 list_for_each_entry(expr, &ref->pat, list) {
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02002048 if (!pat_ref_push(elt, expr, 0, err)) {
Thierry FOURNIER31db4ae2014-01-30 00:27:15 +01002049 /* If the insertion fails, try to delete all the added entries. */
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01002050 pat_ref_delete_by_id(ref, elt);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002051 return 0;
2052 }
2053 }
Emeric Brunb5997f72017-07-03 11:34:05 +02002054
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002055 return 1;
2056}
2057
Thierry FOURNIER46006bd2014-03-21 21:45:15 +01002058/* This function prune <ref>, replace all reference by the references
2059 * of <replace>, and reindex all the news values.
2060 *
2061 * The pattern are loaded in best effort and the errors are ignored,
2062 * but writed in the logs.
2063 */
2064void pat_ref_reload(struct pat_ref *ref, struct pat_ref *replace)
2065{
2066 struct pattern_expr *expr;
Thierry FOURNIER46006bd2014-03-21 21:45:15 +01002067 char *err = NULL;
Emeric Brunb5997f72017-07-03 11:34:05 +02002068 struct pat_ref_elt *elt, *safe;
2069 struct bref *bref, *back;
2070 struct sample_data *data;
2071 struct pattern pattern;
Thierry FOURNIER46006bd2014-03-21 21:45:15 +01002072
Thierry FOURNIER46006bd2014-03-21 21:45:15 +01002073
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002074 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Emeric Brunb5997f72017-07-03 11:34:05 +02002075 list_for_each_entry(expr, &ref->pat, list) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002076 HA_RWLOCK_WRLOCK(PATEXP_LOCK, &expr->lock);
Emeric Brunb5997f72017-07-03 11:34:05 +02002077 }
2078
2079 /* all expr are locked, we can safely remove all pat_ref */
2080 list_for_each_entry_safe(elt, safe, &ref->head, list) {
2081 list_for_each_entry_safe(bref, back, &elt->back_refs, users) {
2082 /*
2083 * we have to unlink all watchers. We must not relink them if
2084 * this elt was the last one in the list.
2085 */
2086 LIST_DEL(&bref->users);
2087 LIST_INIT(&bref->users);
2088 if (elt->list.n != &ref->head)
2089 LIST_ADDQ(&LIST_ELEM(elt->list.n, struct stream *, list)->back_refs, &bref->users);
2090 bref->ref = elt->list.n;
2091 }
2092 LIST_DEL(&elt->list);
2093 free(elt->pattern);
2094 free(elt->sample);
2095 free(elt);
2096 }
2097
2098 /* switch pat_ret_elt lists */
Thierry FOURNIER46006bd2014-03-21 21:45:15 +01002099 LIST_ADD(&replace->head, &ref->head);
2100 LIST_DEL(&replace->head);
2101
Emeric Brunb5997f72017-07-03 11:34:05 +02002102 list_for_each_entry(expr, &ref->pat, list) {
2103 expr->pat_head->prune(expr);
2104 list_for_each_entry(elt, &ref->head, list) {
2105 /* Create sample */
2106 if (elt->sample && expr->pat_head->parse_smp) {
2107 /* New sample. */
2108 data = malloc(sizeof(*data));
2109 if (!data)
2110 continue;
2111
2112 /* Parse value. */
2113 if (!expr->pat_head->parse_smp(elt->sample, data)) {
2114 memprintf(&err, "unable to parse '%s'", elt->sample);
2115 send_log(NULL, LOG_NOTICE, "%s", err);
2116 free(err);
2117 free(data);
2118 continue;
2119 }
2120
2121 }
2122 else
2123 data = NULL;
2124
2125 /* initialise pattern */
2126 memset(&pattern, 0, sizeof(pattern));
2127 pattern.data = data;
2128 pattern.ref = elt;
2129
2130 /* parse pattern */
2131 if (!expr->pat_head->parse(elt->pattern, &pattern, expr->mflags, &err)) {
Thierry FOURNIER46006bd2014-03-21 21:45:15 +01002132 send_log(NULL, LOG_NOTICE, "%s", err);
2133 free(err);
Emeric Brunb5997f72017-07-03 11:34:05 +02002134 free(data);
2135 continue;
2136 }
2137
2138 /* index pattern */
2139 if (!expr->pat_head->index(expr, &pattern, &err)) {
2140 send_log(NULL, LOG_NOTICE, "%s", err);
2141 free(err);
2142 free(data);
2143 continue;
Thierry FOURNIER46006bd2014-03-21 21:45:15 +01002144 }
2145 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002146 HA_RWLOCK_WRUNLOCK(PATEXP_LOCK, &expr->lock);
Thierry FOURNIER46006bd2014-03-21 21:45:15 +01002147 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002148 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER46006bd2014-03-21 21:45:15 +01002149}
2150
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002151/* This function prune all entries of <ref>. This function
2152 * prune the associated pattern_expr.
2153 */
2154void pat_ref_prune(struct pat_ref *ref)
2155{
2156 struct pat_ref_elt *elt, *safe;
2157 struct pattern_expr *expr;
Emeric Brun8d85aa42017-06-29 15:40:33 +02002158 struct bref *bref, *back;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002159
Emeric Brunb5997f72017-07-03 11:34:05 +02002160 list_for_each_entry(expr, &ref->pat, list) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002161 HA_RWLOCK_WRLOCK(PATEXP_LOCK, &expr->lock);
Emeric Brunb5997f72017-07-03 11:34:05 +02002162 expr->pat_head->prune(expr);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002163 HA_RWLOCK_WRUNLOCK(PATEXP_LOCK, &expr->lock);
Emeric Brunb5997f72017-07-03 11:34:05 +02002164 }
2165
2166 /* we trash pat_ref_elt in a second time to ensure that data is
2167 free once there is no ref on it */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002168 list_for_each_entry_safe(elt, safe, &ref->head, list) {
Emeric Brun8d85aa42017-06-29 15:40:33 +02002169 list_for_each_entry_safe(bref, back, &elt->back_refs, users) {
2170 /*
2171 * we have to unlink all watchers. We must not relink them if
2172 * this elt was the last one in the list.
2173 */
2174 LIST_DEL(&bref->users);
2175 LIST_INIT(&bref->users);
2176 if (elt->list.n != &ref->head)
2177 LIST_ADDQ(&LIST_ELEM(elt->list.n, struct stream *, list)->back_refs, &bref->users);
2178 bref->ref = elt->list.n;
2179 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002180 LIST_DEL(&elt->list);
2181 free(elt->pattern);
2182 free(elt->sample);
2183 free(elt);
2184 }
2185
Emeric Brunb5997f72017-07-03 11:34:05 +02002186
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002187}
2188
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002189/* This function lookup for existing reference <ref> in pattern_head <head>. */
2190struct pattern_expr *pattern_lookup_expr(struct pattern_head *head, struct pat_ref *ref)
2191{
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002192 struct pattern_expr_list *expr;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002193
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002194 list_for_each_entry(expr, &head->head, list)
2195 if (expr->expr->ref == ref)
2196 return expr->expr;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002197 return NULL;
2198}
2199
2200/* This function create new pattern_expr associated to the reference <ref>.
2201 * <ref> can be NULL. If an error is occured, the function returns NULL and
2202 * <err> is filled. Otherwise, the function returns new pattern_expr linked
2203 * with <head> and <ref>.
Thierry FOURNIER315ec422014-11-24 11:14:42 +01002204 *
2205 * The returned value can be a alredy filled pattern list, in this case the
2206 * flag <reuse> is set.
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002207 */
Thierry FOURNIER315ec422014-11-24 11:14:42 +01002208struct pattern_expr *pattern_new_expr(struct pattern_head *head, struct pat_ref *ref,
Emeric Brun7d27f3c2017-07-03 17:54:23 +02002209 int patflags, char **err, int *reuse)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002210{
2211 struct pattern_expr *expr;
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002212 struct pattern_expr_list *list;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002213
Thierry FOURNIER315ec422014-11-24 11:14:42 +01002214 if (reuse)
2215 *reuse = 0;
2216
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002217 /* Memory and initialization of the chain element. */
2218 list = malloc(sizeof(*list));
2219 if (!list) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002220 memprintf(err, "out of memory");
2221 return NULL;
2222 }
2223
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002224 /* Look for existing similar expr. No that only the index, parse and
2225 * parse_smp function must be identical for having similar pattern.
2226 * The other function depends of theses first.
2227 */
2228 if (ref) {
2229 list_for_each_entry(expr, &ref->pat, list)
2230 if (expr->pat_head->index == head->index &&
2231 expr->pat_head->parse == head->parse &&
Emeric Brun7d27f3c2017-07-03 17:54:23 +02002232 expr->pat_head->parse_smp == head->parse_smp &&
2233 expr->mflags == patflags)
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002234 break;
2235 if (&expr->list == &ref->pat)
2236 expr = NULL;
2237 }
2238 else
2239 expr = NULL;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002240
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002241 /* If no similar expr was found, we create new expr. */
2242 if (!expr) {
2243 /* Get a lot of memory for the expr struct. */
2244 expr = malloc(sizeof(*expr));
2245 if (!expr) {
Andreas Seltenreiche6e22e82016-03-03 20:20:23 +01002246 free(list);
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002247 memprintf(err, "out of memory");
2248 return NULL;
2249 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002250
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002251 /* Initialize this new expr. */
2252 pattern_init_expr(expr);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002253
Emeric Brun7d27f3c2017-07-03 17:54:23 +02002254 /* Copy the pattern matching and indexing flags. */
2255 expr->mflags = patflags;
2256
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002257 /* This new pattern expression reference one of his heads. */
2258 expr->pat_head = head;
2259
2260 /* Link with ref, or to self to facilitate LIST_DEL() */
2261 if (ref)
2262 LIST_ADDQ(&ref->pat, &expr->list);
2263 else
2264 LIST_INIT(&expr->list);
2265
2266 expr->ref = ref;
2267
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002268 HA_RWLOCK_INIT(&expr->lock);
Emeric Brunb5997f72017-07-03 11:34:05 +02002269
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002270 /* We must free this pattern if it is no more used. */
2271 list->do_free = 1;
2272 }
2273 else {
2274 /* If the pattern used already exists, it is already linked
2275 * with ref and we must not free it.
2276 */
2277 list->do_free = 0;
Thierry FOURNIER315ec422014-11-24 11:14:42 +01002278 if (reuse)
2279 *reuse = 1;
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002280 }
2281
2282 /* The new list element reference the pattern_expr. */
2283 list->expr = expr;
2284
2285 /* Link the list element with the pattern_head. */
2286 LIST_ADDQ(&head->head, &list->list);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002287 return expr;
2288}
2289
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002290/* Reads patterns from a file. If <err_msg> is non-NULL, an error message will
2291 * be returned there on errors and the caller will have to free it.
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002292 *
2293 * The file contains one key + value per line. Lines which start with '#' are
2294 * ignored, just like empty lines. Leading tabs/spaces are stripped. The key is
2295 * then the first "word" (series of non-space/tabs characters), and the value is
2296 * what follows this series of space/tab till the end of the line excluding
2297 * trailing spaces/tabs.
2298 *
2299 * Example :
2300 *
2301 * # this is a comment and is ignored
2302 * 62.212.114.60 1wt.eu \n
2303 * <-><-----------><---><----><---->
2304 * | | | | `--- trailing spaces ignored
2305 * | | | `-------- value
2306 * | | `--------------- middle spaces ignored
2307 * | `------------------------ key
2308 * `-------------------------------- leading spaces ignored
2309 *
2310 * Return non-zero in case of succes, otherwise 0.
2311 */
2312int pat_ref_read_from_file_smp(struct pat_ref *ref, const char *filename, char **err)
2313{
2314 FILE *file;
2315 char *c;
2316 int ret = 0;
2317 int line = 0;
2318 char *key_beg;
2319 char *key_end;
2320 char *value_beg;
2321 char *value_end;
2322
2323 file = fopen(filename, "r");
2324 if (!file) {
2325 memprintf(err, "failed to open pattern file <%s>", filename);
2326 return 0;
2327 }
2328
2329 /* now parse all patterns. The file may contain only one pattern
2330 * followed by one value per line. The start spaces, separator spaces
2331 * and and spaces are stripped. Each can contain comment started by '#'
2332 */
2333 while (fgets(trash.str, trash.size, file) != NULL) {
2334 line++;
2335 c = trash.str;
2336
2337 /* ignore lines beginning with a dash */
2338 if (*c == '#')
2339 continue;
2340
2341 /* strip leading spaces and tabs */
2342 while (*c == ' ' || *c == '\t')
2343 c++;
2344
2345 /* empty lines are ignored too */
2346 if (*c == '\0' || *c == '\r' || *c == '\n')
2347 continue;
2348
2349 /* look for the end of the key */
2350 key_beg = c;
2351 while (*c && *c != ' ' && *c != '\t' && *c != '\n' && *c != '\r')
2352 c++;
2353
2354 key_end = c;
2355
2356 /* strip middle spaces and tabs */
2357 while (*c == ' ' || *c == '\t')
2358 c++;
2359
2360 /* look for the end of the value, it is the end of the line */
2361 value_beg = c;
2362 while (*c && *c != '\n' && *c != '\r')
2363 c++;
2364 value_end = c;
2365
2366 /* trim possibly trailing spaces and tabs */
2367 while (value_end > value_beg && (value_end[-1] == ' ' || value_end[-1] == '\t'))
2368 value_end--;
2369
2370 /* set final \0 and check entries */
2371 *key_end = '\0';
2372 *value_end = '\0';
2373
2374 /* insert values */
2375 if (!pat_ref_append(ref, key_beg, value_beg, line)) {
2376 memprintf(err, "out of memory");
2377 goto out_close;
2378 }
2379 }
2380
2381 /* succes */
2382 ret = 1;
2383
2384 out_close:
2385 fclose(file);
2386 return ret;
2387}
2388
2389/* Reads patterns from a file. If <err_msg> is non-NULL, an error message will
2390 * be returned there on errors and the caller will have to free it.
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002391 */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002392int pat_ref_read_from_file(struct pat_ref *ref, const char *filename, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002393{
2394 FILE *file;
2395 char *c;
2396 char *arg;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002397 int ret = 0;
2398 int line = 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002399
2400 file = fopen(filename, "r");
2401 if (!file) {
2402 memprintf(err, "failed to open pattern file <%s>", filename);
2403 return 0;
2404 }
2405
2406 /* now parse all patterns. The file may contain only one pattern per
2407 * line. If the line contains spaces, they will be part of the pattern.
2408 * The pattern stops at the first CR, LF or EOF encountered.
2409 */
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002410 while (fgets(trash.str, trash.size, file) != NULL) {
2411 line++;
2412 c = trash.str;
2413
2414 /* ignore lines beginning with a dash */
2415 if (*c == '#')
2416 continue;
2417
2418 /* strip leading spaces and tabs */
2419 while (*c == ' ' || *c == '\t')
2420 c++;
2421
2422
2423 arg = c;
2424 while (*c && *c != '\n' && *c != '\r')
2425 c++;
2426 *c = 0;
2427
2428 /* empty lines are ignored too */
2429 if (c == arg)
2430 continue;
2431
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002432 if (!pat_ref_append(ref, arg, NULL, line)) {
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002433 memprintf(err, "out of memory when loading patterns from file <%s>", filename);
2434 goto out_close;
2435 }
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002436 }
2437
2438 ret = 1; /* success */
2439
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002440 out_close:
2441 fclose(file);
2442 return ret;
2443}
2444
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002445int pattern_read_from_file(struct pattern_head *head, unsigned int refflags,
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002446 const char *filename, int patflags, int load_smp,
Thierry FOURNIER94580c92014-02-11 14:36:45 +01002447 char **err, const char *file, int line)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002448{
2449 struct pat_ref *ref;
2450 struct pattern_expr *expr;
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002451 struct pat_ref_elt *elt;
Willy Tarreau4deaf392014-11-26 13:17:03 +01002452 int reuse = 0;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002453
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002454 /* Lookup for the existing reference. */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002455 ref = pat_ref_lookup(filename);
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002456
2457 /* If the reference doesn't exists, create it and load associated file. */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002458 if (!ref) {
Thierry FOURNIER94580c92014-02-11 14:36:45 +01002459 chunk_printf(&trash,
2460 "pattern loaded from file '%s' used by %s at file '%s' line %d",
2461 filename, refflags & PAT_REF_MAP ? "map" : "acl", file, line);
2462
2463 ref = pat_ref_new(filename, trash.str, refflags);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002464 if (!ref) {
2465 memprintf(err, "out of memory");
2466 return 0;
2467 }
2468
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002469 if (load_smp) {
Thierry FOURNIERc0bd9102014-01-29 12:32:58 +01002470 ref->flags |= PAT_REF_SMP;
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002471 if (!pat_ref_read_from_file_smp(ref, filename, err))
2472 return 0;
2473 }
2474 else {
2475 if (!pat_ref_read_from_file(ref, filename, err))
2476 return 0;
2477 }
2478 }
2479 else {
Thierry FOURNIERc0bd9102014-01-29 12:32:58 +01002480 /* The reference already exists, check the map compatibility. */
2481
2482 /* If the load require samples and the flag PAT_REF_SMP is not set,
2483 * the reference doesn't contain sample, and cannot be used.
2484 */
2485 if (load_smp) {
2486 if (!(ref->flags & PAT_REF_SMP)) {
2487 memprintf(err, "The file \"%s\" is already used as one column file "
2488 "and cannot be used by as two column file.",
2489 filename);
2490 return 0;
2491 }
2492 }
2493 else {
2494 /* The load doesn't require samples. If the flag PAT_REF_SMP is
2495 * set, the reference contains a sample, and cannot be used.
2496 */
2497 if (ref->flags & PAT_REF_SMP) {
2498 memprintf(err, "The file \"%s\" is already used as two column file "
2499 "and cannot be used by as one column file.",
2500 filename);
2501 return 0;
2502 }
2503 }
2504
Thierry FOURNIER94580c92014-02-11 14:36:45 +01002505 /* Extends display */
2506 chunk_printf(&trash, "%s", ref->display);
2507 chunk_appendf(&trash, ", by %s at file '%s' line %d",
2508 refflags & PAT_REF_MAP ? "map" : "acl", file, line);
2509 free(ref->display);
2510 ref->display = strdup(trash.str);
2511 if (!ref->display) {
2512 memprintf(err, "out of memory");
2513 return 0;
2514 }
2515
Thierry FOURNIERc0bd9102014-01-29 12:32:58 +01002516 /* Merge flags. */
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002517 ref->flags |= refflags;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002518 }
2519
2520 /* Now, we can loading patterns from the reference. */
2521
2522 /* Lookup for existing reference in the head. If the reference
2523 * doesn't exists, create it.
2524 */
2525 expr = pattern_lookup_expr(head, ref);
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02002526 if (!expr || (expr->mflags != patflags)) {
Emeric Brun7d27f3c2017-07-03 17:54:23 +02002527 expr = pattern_new_expr(head, ref, patflags, err, &reuse);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002528 if (!expr)
2529 return 0;
2530 }
2531
Thierry FOURNIER315ec422014-11-24 11:14:42 +01002532 /* The returned expression may be not empty, because the function
2533 * "pattern_new_expr" lookup for similar pattern list and can
2534 * reuse a already filled pattern list. In this case, we can not
2535 * reload the patterns.
2536 */
2537 if (reuse)
2538 return 1;
2539
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002540 /* Load reference content in the pattern expression. */
2541 list_for_each_entry(elt, &ref->head, list) {
2542 if (!pat_ref_push(elt, expr, patflags, err)) {
2543 if (elt->line > 0)
2544 memprintf(err, "%s at line %d of file '%s'",
2545 *err, elt->line, filename);
2546 return 0;
2547 }
2548 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002549
2550 return 1;
2551}
2552
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01002553/* This function executes a pattern match on a sample. It applies pattern <expr>
2554 * to sample <smp>. The function returns NULL if the sample dont match. It returns
2555 * non-null if the sample match. If <fill> is true and the sample match, the
2556 * function returns the matched pattern. In many cases, this pattern can be a
2557 * static buffer.
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002558 */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002559struct pattern *pattern_exec_match(struct pattern_head *head, struct sample *smp, int fill)
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002560{
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002561 struct pattern_expr_list *list;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002562 struct pattern *pat;
2563
2564 if (!head->match) {
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01002565 if (fill) {
Thierry FOURNIER503bb092015-08-19 08:35:43 +02002566 static_pattern.data = NULL;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01002567 static_pattern.ref = NULL;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02002568 static_pattern.sflags = 0;
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002569 static_pattern.type = SMP_T_SINT;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01002570 static_pattern.val.i = 1;
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01002571 }
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01002572 return &static_pattern;
2573 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002574
Thierry FOURNIER5d344082014-01-27 14:19:53 +01002575 /* convert input to string */
2576 if (!sample_convert(smp, head->expect_type))
2577 return NULL;
2578
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002579 list_for_each_entry(list, &head->head, list) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002580 HA_RWLOCK_RDLOCK(PATEXP_LOCK, &list->expr->lock);
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002581 pat = head->match(smp, list->expr, fill);
Emeric Brunb5997f72017-07-03 11:34:05 +02002582 if (pat) {
2583 /* We duplicate the pattern cause it could be modified
2584 by another thread */
2585 if (pat != &static_pattern) {
2586 memcpy(&static_pattern, pat, sizeof(struct pattern));
2587 pat = &static_pattern;
2588 }
2589
2590 /* We also duplicate the sample data for
2591 same reason */
2592 if (pat->data && (pat->data != &static_sample_data)) {
Christopher Faulet09fdf4b2017-11-09 16:14:16 +01002593 switch(pat->data->type) {
Emeric Brunb5997f72017-07-03 11:34:05 +02002594 case SMP_T_STR:
2595 static_sample_data.type = SMP_T_STR;
2596 static_sample_data.u.str = *get_trash_chunk();
2597 static_sample_data.u.str.len = pat->data->u.str.len;
2598 if (static_sample_data.u.str.len >= static_sample_data.u.str.size)
2599 static_sample_data.u.str.len = static_sample_data.u.str.size - 1;
2600 memcpy(static_sample_data.u.str.str, pat->data->u.str.str, static_sample_data.u.str.len);
2601 static_sample_data.u.str.str[static_sample_data.u.str.len] = 0;
2602 case SMP_T_IPV4:
2603 case SMP_T_IPV6:
2604 case SMP_T_SINT:
2605 memcpy(&static_sample_data, pat->data, sizeof(struct sample_data));
2606 default:
2607 pat->data = NULL;
2608 }
2609 pat->data = &static_sample_data;
2610 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002611 HA_RWLOCK_RDUNLOCK(PATEXP_LOCK, &list->expr->lock);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002612 return pat;
Emeric Brunb5997f72017-07-03 11:34:05 +02002613 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002614 HA_RWLOCK_RDUNLOCK(PATEXP_LOCK, &list->expr->lock);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002615 }
2616 return NULL;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002617}
2618
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01002619/* This function prune the pattern expression. */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002620void pattern_prune(struct pattern_head *head)
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01002621{
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002622 struct pattern_expr_list *list, *safe;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002623
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002624 list_for_each_entry_safe(list, safe, &head->head, list) {
2625 LIST_DEL(&list->list);
2626 if (list->do_free) {
2627 LIST_DEL(&list->expr->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002628 HA_RWLOCK_WRLOCK(PATEXP_LOCK, &list->expr->lock);
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002629 head->prune(list->expr);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002630 HA_RWLOCK_WRUNLOCK(PATEXP_LOCK, &list->expr->lock);
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002631 free(list->expr);
2632 }
2633 free(list);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002634 }
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01002635}
2636
Thierry FOURNIER55d0b102014-01-15 11:25:26 +01002637/* This function lookup for a pattern matching the <key> and return a
2638 * pointer to a pointer of the sample stoarge. If the <key> dont match,
2639 * the function returns NULL. If the key cannot be parsed, the function
2640 * fill <err>.
2641 */
Thierry FOURNIER12ba0c22015-08-14 00:02:11 +02002642struct sample_data **pattern_find_smp(struct pattern_expr *expr, struct pat_ref_elt *ref)
Thierry FOURNIER55d0b102014-01-15 11:25:26 +01002643{
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01002644 struct ebmb_node *node;
2645 struct pattern_tree *elt;
2646 struct pattern_list *pat;
Thierry FOURNIER55d0b102014-01-15 11:25:26 +01002647
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01002648 for (node = ebmb_first(&expr->pattern_tree);
2649 node;
2650 node = ebmb_next(node)) {
2651 elt = container_of(node, struct pattern_tree, node);
2652 if (elt->ref == ref)
Thierry FOURNIER503bb092015-08-19 08:35:43 +02002653 return &elt->data;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01002654 }
2655
2656 for (node = ebmb_first(&expr->pattern_tree_2);
2657 node;
2658 node = ebmb_next(node)) {
2659 elt = container_of(node, struct pattern_tree, node);
2660 if (elt->ref == ref)
Thierry FOURNIER503bb092015-08-19 08:35:43 +02002661 return &elt->data;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01002662 }
2663
2664 list_for_each_entry(pat, &expr->patterns, list)
2665 if (pat->pat.ref == ref)
Thierry FOURNIER503bb092015-08-19 08:35:43 +02002666 return &pat->pat.data;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01002667
2668 return NULL;
Thierry FOURNIER55d0b102014-01-15 11:25:26 +01002669}
2670
Thierry FOURNIERb1136502014-01-15 11:38:49 +01002671/* This function search all the pattern matching the <key> and delete it.
2672 * If the parsing of the input key fails, the function returns 0 and the
2673 * <err> is filled, else return 1;
2674 */
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01002675int pattern_delete(struct pattern_expr *expr, struct pat_ref_elt *ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01002676{
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002677 HA_RWLOCK_WRLOCK(PATEXP_LOCK, &expr->lock);
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01002678 expr->pat_head->delete(expr, ref);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002679 HA_RWLOCK_WRUNLOCK(PATEXP_LOCK, &expr->lock);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01002680 return 1;
2681}
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002682
2683/* This function finalize the configuration parsing. Its set all the
2684 * automatic ids
2685 */
2686void pattern_finalize_config(void)
2687{
2688 int i = 0;
2689 struct pat_ref *ref, *ref2, *ref3;
2690 struct list pr = LIST_HEAD_INIT(pr);
2691
Willy Tarreauf3045d22015-04-29 16:24:50 +02002692 pat_lru_seed = random();
Emeric Brunb5997f72017-07-03 11:34:05 +02002693 if (global.tune.pattern_cache) {
Willy Tarreauf3045d22015-04-29 16:24:50 +02002694 pat_lru_tree = lru64_new(global.tune.pattern_cache);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002695 HA_SPIN_INIT(&pat_lru_tree_lock);
Emeric Brunb5997f72017-07-03 11:34:05 +02002696 }
Willy Tarreauf3045d22015-04-29 16:24:50 +02002697
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002698 list_for_each_entry(ref, &pattern_reference, list) {
2699 if (ref->unique_id == -1) {
2700 /* Look for the first free id. */
2701 while (1) {
2702 list_for_each_entry(ref2, &pattern_reference, list) {
2703 if (ref2->unique_id == i) {
2704 i++;
2705 break;
2706 }
2707 }
Willy Tarreau3b786962014-04-26 12:37:25 +02002708 if (&ref2->list == &pattern_reference)
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002709 break;
2710 }
2711
2712 /* Uses the unique id and increment it for the next entry. */
2713 ref->unique_id = i;
2714 i++;
2715 }
2716 }
2717
2718 /* This sort the reference list by id. */
2719 list_for_each_entry_safe(ref, ref2, &pattern_reference, list) {
2720 LIST_DEL(&ref->list);
2721 list_for_each_entry(ref3, &pr, list) {
2722 if (ref->unique_id < ref3->unique_id) {
2723 LIST_ADDQ(&ref3->list, &ref->list);
2724 break;
2725 }
2726 }
2727 if (&ref3->list == &pr)
2728 LIST_ADDQ(&pr, &ref->list);
2729 }
2730
2731 /* swap root */
2732 LIST_ADD(&pr, &pattern_reference);
2733 LIST_DEL(&pr);
2734}