blob: 49d05a106b0b4a22cb3e486d36b2a4045e94b418 [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;
Willy Tarreau86abe442018-11-25 20:12:18 +0100158__decl_spinlock(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{
Willy Tarreau83061a82018-07-13 11:56:34 +0200224 struct buffer *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;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200229 pattern->ptr.str = trash->area;
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 *
Joseph Herlant4189d672018-11-15 10:22:31 -0800428 * This function just takes a sample <smp> and checks if this sample matches
429 * with the pattern <pattern>. This function returns only PAT_MATCH or
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100430 * 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
Joseph Herlant4189d672018-11-15 10:22:31 -0800451/* NB: For two strings to be identical, it is required that their length 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 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200466 prev = smp->data.u.str.area[smp->data.u.str.data];
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100467 if (prev)
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200468 smp->data.u.str.area[smp->data.u.str.data] = '\0';
469 node = ebst_lookup(&expr->pattern_tree, smp->data.u.str.area);
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100470 if (prev)
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200471 smp->data.u.str.area[smp->data.u.str.data] = 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);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200491 lru = lru64_get(XXH64(smp->data.u.str.area, smp->data.u.str.data, 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
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200507 if (pattern->len != smp->data.u.str.data)
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;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200511 if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.str.area, smp->data.u.str.data) == 0) ||
512 (!icase && strncmp(pattern->ptr.str, smp->data.u.str.area, smp->data.u.str.data) == 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);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200538 lru = lru64_get(XXH64(smp->data.u.str.area, smp->data.u.str.data, 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
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200553 if (pattern->len != smp->data.u.str.data)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100554 continue;
555
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200556 if (memcmp(pattern->ptr.str, smp->data.u.str.area, smp->data.u.str.data) == 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
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200583 if (regex_exec_match2(pattern->ptr.reg, smp->data.u.str.area, smp->data.u.str.data,
Thierry Fournier8feaa662016-02-10 22:55:20 +0100584 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);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200608 lru = lru64_get(XXH64(smp->data.u.str.area, smp->data.u.str.data, 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
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200623 if (regex_exec2(pattern->ptr.reg, smp->data.u.str.area, smp->data.u.str.data)) {
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 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200652 prev = smp->data.u.str.area[smp->data.u.str.data];
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +0200653 if (prev)
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200654 smp->data.u.str.area[smp->data.u.str.data] = '\0';
655 node = ebmb_lookup_longest(&expr->pattern_tree,
656 smp->data.u.str.area);
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +0200657 if (prev)
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200658 smp->data.u.str.area[smp->data.u.str.data] = prev;
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +0200659
660 if (node) {
661 if (fill) {
662 elt = ebmb_entry(node, struct pattern_tree, node);
Thierry FOURNIER503bb092015-08-19 08:35:43 +0200663 static_pattern.data = elt->data;
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +0200664 static_pattern.ref = elt->ref;
665 static_pattern.sflags = PAT_SF_TREE;
666 static_pattern.type = SMP_T_STR;
667 static_pattern.ptr.str = (char *)elt->node.key;
668 }
669 return &static_pattern;
670 }
671 }
672
673 /* look in the list */
Willy Tarreauf3045d22015-04-29 16:24:50 +0200674 if (pat_lru_tree) {
Willy Tarreauaee93142015-05-04 17:18:42 +0200675 unsigned long long seed = pat_lru_seed ^ (long)expr;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200676
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100677 HA_SPIN_LOCK(PATLRU_LOCK, &pat_lru_tree_lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200678 lru = lru64_get(XXH64(smp->data.u.str.area, smp->data.u.str.data, seed),
Willy Tarreauf3045d22015-04-29 16:24:50 +0200679 pat_lru_tree, expr, expr->revision);
Emeric Brunb5997f72017-07-03 11:34:05 +0200680 if (!lru) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100681 HA_SPIN_UNLOCK(PATLRU_LOCK, &pat_lru_tree_lock);
Emeric Brunb5997f72017-07-03 11:34:05 +0200682 }
683 else if (lru->domain) {
684 ret = lru->data;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100685 HA_SPIN_UNLOCK(PATLRU_LOCK, &pat_lru_tree_lock);
Emeric Brunb5997f72017-07-03 11:34:05 +0200686 return ret;
687 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200688 }
689
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100690 list_for_each_entry(lst, &expr->patterns, list) {
691 pattern = &lst->pat;
692
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200693 if (pattern->len > smp->data.u.str.data)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100694 continue;
695
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200696 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200697 if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.str.area, pattern->len) != 0) ||
698 (!icase && strncmp(pattern->ptr.str, smp->data.u.str.area, pattern->len) != 0))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100699 continue;
700
Willy Tarreauf3045d22015-04-29 16:24:50 +0200701 ret = pattern;
702 break;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100703 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200704
Emeric Brunb5997f72017-07-03 11:34:05 +0200705 if (lru) {
706 lru64_commit(lru, ret, expr, expr->revision, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100707 HA_SPIN_UNLOCK(PATLRU_LOCK, &pat_lru_tree_lock);
Emeric Brunb5997f72017-07-03 11:34:05 +0200708 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200709
710 return ret;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100711}
712
713/* Checks that the pattern matches the end of the tested string. */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100714struct pattern *pat_match_end(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100715{
716 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100717 struct pattern_list *lst;
718 struct pattern *pattern;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200719 struct pattern *ret = NULL;
720 struct lru64 *lru = NULL;
721
722 if (pat_lru_tree) {
Willy Tarreauaee93142015-05-04 17:18:42 +0200723 unsigned long long seed = pat_lru_seed ^ (long)expr;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200724
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100725 HA_SPIN_LOCK(PATLRU_LOCK, &pat_lru_tree_lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200726 lru = lru64_get(XXH64(smp->data.u.str.area, smp->data.u.str.data, seed),
Willy Tarreauf3045d22015-04-29 16:24:50 +0200727 pat_lru_tree, expr, expr->revision);
Emeric Brunb5997f72017-07-03 11:34:05 +0200728 if (!lru) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100729 HA_SPIN_UNLOCK(PATLRU_LOCK, &pat_lru_tree_lock);
Emeric Brunb5997f72017-07-03 11:34:05 +0200730 }
731 else if (lru->domain) {
732 ret = lru->data;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100733 HA_SPIN_UNLOCK(PATLRU_LOCK, &pat_lru_tree_lock);
Emeric Brunb5997f72017-07-03 11:34:05 +0200734 return ret;
735 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200736 }
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100737
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100738 list_for_each_entry(lst, &expr->patterns, list) {
739 pattern = &lst->pat;
740
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200741 if (pattern->len > smp->data.u.str.data)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100742 continue;
743
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200744 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200745 if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.str.area + smp->data.u.str.data - pattern->len, pattern->len) != 0) ||
746 (!icase && strncmp(pattern->ptr.str, smp->data.u.str.area + smp->data.u.str.data - pattern->len, pattern->len) != 0))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100747 continue;
748
Willy Tarreauf3045d22015-04-29 16:24:50 +0200749 ret = pattern;
750 break;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100751 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200752
Emeric Brunb5997f72017-07-03 11:34:05 +0200753 if (lru) {
754 lru64_commit(lru, ret, expr, expr->revision, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100755 HA_SPIN_UNLOCK(PATLRU_LOCK, &pat_lru_tree_lock);
Emeric Brunb5997f72017-07-03 11:34:05 +0200756 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200757
758 return ret;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100759}
760
761/* Checks that the pattern is included inside the tested string.
762 * NB: Suboptimal, should be rewritten using a Boyer-Moore method.
763 */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100764struct pattern *pat_match_sub(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100765{
766 int icase;
767 char *end;
768 char *c;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100769 struct pattern_list *lst;
770 struct pattern *pattern;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200771 struct pattern *ret = NULL;
772 struct lru64 *lru = NULL;
773
774 if (pat_lru_tree) {
Willy Tarreauaee93142015-05-04 17:18:42 +0200775 unsigned long long seed = pat_lru_seed ^ (long)expr;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200776
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100777 HA_SPIN_LOCK(PATLRU_LOCK, &pat_lru_tree_lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200778 lru = lru64_get(XXH64(smp->data.u.str.area, smp->data.u.str.data, seed),
Willy Tarreauf3045d22015-04-29 16:24:50 +0200779 pat_lru_tree, expr, expr->revision);
Emeric Brunb5997f72017-07-03 11:34:05 +0200780 if (!lru) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100781 HA_SPIN_UNLOCK(PATLRU_LOCK, &pat_lru_tree_lock);
Emeric Brunb5997f72017-07-03 11:34:05 +0200782 }
783 else if (lru->domain) {
784 ret = lru->data;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100785 HA_SPIN_UNLOCK(PATLRU_LOCK, &pat_lru_tree_lock);
Emeric Brunb5997f72017-07-03 11:34:05 +0200786 return ret;
787 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200788 }
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100789
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100790 list_for_each_entry(lst, &expr->patterns, list) {
791 pattern = &lst->pat;
792
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200793 if (pattern->len > smp->data.u.str.data)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100794 continue;
795
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200796 end = smp->data.u.str.area + smp->data.u.str.data - pattern->len;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200797 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100798 if (icase) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200799 for (c = smp->data.u.str.area; c <= end; c++) {
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100800 if (tolower(*c) != tolower(*pattern->ptr.str))
801 continue;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200802 if (strncasecmp(pattern->ptr.str, c, pattern->len) == 0) {
803 ret = pattern;
804 goto leave;
805 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100806 }
807 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200808 for (c = smp->data.u.str.area; c <= end; c++) {
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100809 if (*c != *pattern->ptr.str)
810 continue;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200811 if (strncmp(pattern->ptr.str, c, pattern->len) == 0) {
812 ret = pattern;
813 goto leave;
814 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100815 }
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100816 }
817 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200818 leave:
Emeric Brunb5997f72017-07-03 11:34:05 +0200819 if (lru) {
820 lru64_commit(lru, ret, expr, expr->revision, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100821 HA_SPIN_UNLOCK(PATLRU_LOCK, &pat_lru_tree_lock);
Emeric Brunb5997f72017-07-03 11:34:05 +0200822 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200823
824 return ret;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100825}
826
827/* This one is used by other real functions. It checks that the pattern is
828 * included inside the tested string, but enclosed between the specified
829 * delimiters or at the beginning or end of the string. The delimiters are
830 * provided as an unsigned int made by make_4delim() and match up to 4 different
831 * delimiters. Delimiters are stripped at the beginning and end of the pattern.
832 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200833static int match_word(struct sample *smp, struct pattern *pattern, int mflags, unsigned int delimiters)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100834{
835 int may_match, icase;
836 char *c, *end;
837 char *ps;
838 int pl;
839
840 pl = pattern->len;
841 ps = pattern->ptr.str;
842
843 while (pl > 0 && is_delimiter(*ps, delimiters)) {
844 pl--;
845 ps++;
846 }
847
848 while (pl > 0 && is_delimiter(ps[pl - 1], delimiters))
849 pl--;
850
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200851 if (pl > smp->data.u.str.data)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100852 return PAT_NOMATCH;
853
854 may_match = 1;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200855 icase = mflags & PAT_MF_IGNORE_CASE;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200856 end = smp->data.u.str.area + smp->data.u.str.data - pl;
857 for (c = smp->data.u.str.area; c <= end; c++) {
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100858 if (is_delimiter(*c, delimiters)) {
859 may_match = 1;
860 continue;
861 }
862
863 if (!may_match)
864 continue;
865
866 if (icase) {
867 if ((tolower(*c) == tolower(*ps)) &&
868 (strncasecmp(ps, c, pl) == 0) &&
869 (c == end || is_delimiter(c[pl], delimiters)))
870 return PAT_MATCH;
871 } else {
872 if ((*c == *ps) &&
873 (strncmp(ps, c, pl) == 0) &&
874 (c == end || is_delimiter(c[pl], delimiters)))
875 return PAT_MATCH;
876 }
877 may_match = 0;
878 }
879 return PAT_NOMATCH;
880}
881
882/* Checks that the pattern is included inside the tested string, but enclosed
883 * between the delimiters '?' or '/' or at the beginning or end of the string.
884 * Delimiters at the beginning or end of the pattern are ignored.
885 */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100886struct pattern *pat_match_dir(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100887{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100888 struct pattern_list *lst;
889 struct pattern *pattern;
890
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100891 list_for_each_entry(lst, &expr->patterns, list) {
892 pattern = &lst->pat;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200893 if (match_word(smp, pattern, expr->mflags, make_4delim('/', '?', '?', '?')))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100894 return pattern;
895 }
896 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100897}
898
899/* Checks that the pattern is included inside the tested string, but enclosed
900 * between the delmiters '/', '?', '.' or ":" or at the beginning or end of
901 * the string. Delimiters at the beginning or end of the pattern are ignored.
902 */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100903struct pattern *pat_match_dom(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100904{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100905 struct pattern_list *lst;
906 struct pattern *pattern;
907
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100908 list_for_each_entry(lst, &expr->patterns, list) {
909 pattern = &lst->pat;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200910 if (match_word(smp, pattern, expr->mflags, make_4delim('/', '?', '.', ':')))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100911 return pattern;
912 }
913 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100914}
915
916/* Checks that the integer in <test> is included between min and max */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100917struct pattern *pat_match_int(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100918{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100919 struct pattern_list *lst;
920 struct pattern *pattern;
921
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100922 list_for_each_entry(lst, &expr->patterns, list) {
923 pattern = &lst->pat;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200924 if ((!pattern->val.range.min_set || pattern->val.range.min <= smp->data.u.sint) &&
925 (!pattern->val.range.max_set || smp->data.u.sint <= pattern->val.range.max))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100926 return pattern;
927 }
928 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100929}
930
931/* Checks that the length of the pattern in <test> is included between min and max */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100932struct pattern *pat_match_len(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100933{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100934 struct pattern_list *lst;
935 struct pattern *pattern;
936
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100937 list_for_each_entry(lst, &expr->patterns, list) {
938 pattern = &lst->pat;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200939 if ((!pattern->val.range.min_set || pattern->val.range.min <= smp->data.u.str.data) &&
940 (!pattern->val.range.max_set || smp->data.u.str.data <= pattern->val.range.max))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100941 return pattern;
942 }
943 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100944}
945
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100946struct pattern *pat_match_ip(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100947{
948 unsigned int v4; /* in network byte order */
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100949 struct in6_addr tmp6;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100950 struct in_addr *s;
951 struct ebmb_node *node;
952 struct pattern_tree *elt;
953 struct pattern_list *lst;
954 struct pattern *pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100955
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100956 /* The input sample is IPv4. Try to match in the trees. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200957 if (smp->data.type == SMP_T_IPV4) {
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100958 /* Lookup an IPv4 address in the expression's pattern tree using
959 * the longest match method.
960 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200961 s = &smp->data.u.ipv4;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100962 node = ebmb_lookup_longest(&expr->pattern_tree, &s->s_addr);
963 if (node) {
964 if (fill) {
965 elt = ebmb_entry(node, struct pattern_tree, node);
Thierry FOURNIER503bb092015-08-19 08:35:43 +0200966 static_pattern.data = elt->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +0100967 static_pattern.ref = elt->ref;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200968 static_pattern.sflags = PAT_SF_TREE;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100969 static_pattern.type = SMP_T_IPV4;
970 memcpy(&static_pattern.val.ipv4.addr.s_addr, elt->node.key, 4);
971 if (!cidr2dotted(elt->node.node.pfx, &static_pattern.val.ipv4.mask))
972 return NULL;
973 }
974 return &static_pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100975 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100976
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100977 /* The IPv4 sample dont match the IPv4 tree. Convert the IPv4
978 * sample address to IPv6 with the mapping method using the ::ffff:
979 * prefix, and try to lookup in the IPv6 tree.
980 */
981 memset(&tmp6, 0, 10);
982 *(uint16_t*)&tmp6.s6_addr[10] = htons(0xffff);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200983 *(uint32_t*)&tmp6.s6_addr[12] = smp->data.u.ipv4.s_addr;
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100984 node = ebmb_lookup_longest(&expr->pattern_tree_2, &tmp6);
985 if (node) {
986 if (fill) {
987 elt = ebmb_entry(node, struct pattern_tree, node);
Thierry FOURNIER503bb092015-08-19 08:35:43 +0200988 static_pattern.data = elt->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +0100989 static_pattern.ref = elt->ref;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200990 static_pattern.sflags = PAT_SF_TREE;
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100991 static_pattern.type = SMP_T_IPV6;
992 memcpy(&static_pattern.val.ipv6.addr, elt->node.key, 16);
993 static_pattern.val.ipv6.mask = elt->node.node.pfx;
994 }
995 return &static_pattern;
996 }
997 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100998
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100999 /* The input sample is IPv6. Try to match in the trees. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001000 if (smp->data.type == SMP_T_IPV6) {
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001001 /* Lookup an IPv6 address in the expression's pattern tree using
1002 * the longest match method.
1003 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001004 node = ebmb_lookup_longest(&expr->pattern_tree_2, &smp->data.u.ipv6);
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001005 if (node) {
1006 if (fill) {
1007 elt = ebmb_entry(node, struct pattern_tree, node);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001008 static_pattern.data = elt->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01001009 static_pattern.ref = elt->ref;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02001010 static_pattern.sflags = PAT_SF_TREE;
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001011 static_pattern.type = SMP_T_IPV6;
1012 memcpy(&static_pattern.val.ipv6.addr, elt->node.key, 16);
1013 static_pattern.val.ipv6.mask = elt->node.node.pfx;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +01001014 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001015 return &static_pattern;
1016 }
1017
1018 /* Try to convert 6 to 4 when the start of the ipv6 address match the
1019 * following forms :
1020 * - ::ffff:ip:v4 (ipv4 mapped)
1021 * - ::0000:ip:v4 (old ipv4 mapped)
1022 * - 2002:ip:v4:: (6to4)
1023 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001024 if ((*(uint32_t*)&smp->data.u.ipv6.s6_addr[0] == 0 &&
1025 *(uint32_t*)&smp->data.u.ipv6.s6_addr[4] == 0 &&
1026 (*(uint32_t*)&smp->data.u.ipv6.s6_addr[8] == 0 ||
1027 *(uint32_t*)&smp->data.u.ipv6.s6_addr[8] == htonl(0xFFFF))) ||
1028 *(uint16_t*)&smp->data.u.ipv6.s6_addr[0] == htons(0x2002)) {
1029 if (*(uint32_t*)&smp->data.u.ipv6.s6_addr[0] == 0)
1030 v4 = *(uint32_t*)&smp->data.u.ipv6.s6_addr[12];
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001031 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001032 v4 = htonl((ntohs(*(uint16_t*)&smp->data.u.ipv6.s6_addr[2]) << 16) +
1033 ntohs(*(uint16_t*)&smp->data.u.ipv6.s6_addr[4]));
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001034
1035 /* Lookup an IPv4 address in the expression's pattern tree using the longest
1036 * match method.
1037 */
1038 node = ebmb_lookup_longest(&expr->pattern_tree, &v4);
1039 if (node) {
1040 if (fill) {
1041 elt = ebmb_entry(node, struct pattern_tree, node);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001042 static_pattern.data = elt->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01001043 static_pattern.ref = elt->ref;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02001044 static_pattern.sflags = PAT_SF_TREE;
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001045 static_pattern.type = SMP_T_IPV4;
1046 memcpy(&static_pattern.val.ipv4.addr.s_addr, elt->node.key, 4);
1047 if (!cidr2dotted(elt->node.node.pfx, &static_pattern.val.ipv4.mask))
1048 return NULL;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01001049 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001050 return &static_pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +01001051 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001052 }
1053 }
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +01001054
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001055 /* Lookup in the list. the list contain only IPv4 patterns */
1056 list_for_each_entry(lst, &expr->patterns, list) {
1057 pattern = &lst->pat;
1058
1059 /* The input sample is IPv4, use it as is. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001060 if (smp->data.type == SMP_T_IPV4) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001061 v4 = smp->data.u.ipv4.s_addr;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +01001062 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001063 else if (smp->data.type == SMP_T_IPV6) {
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001064 /* v4 match on a V6 sample. We want to check at least for
1065 * the following forms :
1066 * - ::ffff:ip:v4 (ipv4 mapped)
1067 * - ::0000:ip:v4 (old ipv4 mapped)
1068 * - 2002:ip:v4:: (6to4)
1069 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001070 if (*(uint32_t*)&smp->data.u.ipv6.s6_addr[0] == 0 &&
1071 *(uint32_t*)&smp->data.u.ipv6.s6_addr[4] == 0 &&
1072 (*(uint32_t*)&smp->data.u.ipv6.s6_addr[8] == 0 ||
1073 *(uint32_t*)&smp->data.u.ipv6.s6_addr[8] == htonl(0xFFFF))) {
1074 v4 = *(uint32_t*)&smp->data.u.ipv6.s6_addr[12];
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01001075 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001076 else if (*(uint16_t*)&smp->data.u.ipv6.s6_addr[0] == htons(0x2002)) {
1077 v4 = htonl((ntohs(*(uint16_t*)&smp->data.u.ipv6.s6_addr[2]) << 16) +
1078 ntohs(*(uint16_t*)&smp->data.u.ipv6.s6_addr[4]));
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01001079 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001080 else
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01001081 continue;
Andreas Seltenreichf0653192016-03-03 20:08:35 +01001082 } else {
1083 /* impossible */
1084 continue;
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001085 }
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +01001086
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001087 /* Check if the input sample match the current pattern. */
1088 if (((v4 ^ pattern->val.ipv4.addr.s_addr) & pattern->val.ipv4.mask.s_addr) == 0)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01001089 return pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +01001090 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01001091 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +01001092}
1093
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001094void free_pattern_tree(struct eb_root *root)
1095{
1096 struct eb_node *node, *next;
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +01001097 struct pattern_tree *elt;
Thierry FOURNIER3ce88c72013-12-09 11:29:46 +01001098
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001099 node = eb_first(root);
1100 while (node) {
1101 next = eb_next(node);
1102 eb_delete(node);
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +01001103 elt = container_of(node, struct pattern_tree, node);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001104 free(elt->data);
Thierry FOURNIER3ce88c72013-12-09 11:29:46 +01001105 free(elt);
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001106 node = next;
1107 }
1108}
1109
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01001110void pat_prune_val(struct pattern_expr *expr)
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +01001111{
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01001112 struct pattern_list *pat, *tmp;
1113
1114 list_for_each_entry_safe(pat, tmp, &expr->patterns, list) {
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001115 free(pat->pat.data);
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01001116 free(pat);
1117 }
1118
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +01001119 free_pattern_tree(&expr->pattern_tree);
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001120 free_pattern_tree(&expr->pattern_tree_2);
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +01001121 LIST_INIT(&expr->patterns);
1122}
1123
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01001124void pat_prune_ptr(struct pattern_expr *expr)
1125{
1126 struct pattern_list *pat, *tmp;
1127
1128 list_for_each_entry_safe(pat, tmp, &expr->patterns, list) {
1129 free(pat->pat.ptr.ptr);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001130 free(pat->pat.data);
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01001131 free(pat);
1132 }
1133
1134 free_pattern_tree(&expr->pattern_tree);
1135 free_pattern_tree(&expr->pattern_tree_2);
1136 LIST_INIT(&expr->patterns);
1137}
1138
1139void pat_prune_reg(struct pattern_expr *expr)
1140{
1141 struct pattern_list *pat, *tmp;
1142
1143 list_for_each_entry_safe(pat, tmp, &expr->patterns, list) {
1144 regex_free(pat->pat.ptr.ptr);
Dragan Dosene99af972019-04-30 01:03:26 +02001145 free(pat->pat.ptr.ptr);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001146 free(pat->pat.data);
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01001147 free(pat);
1148 }
1149
1150 free_pattern_tree(&expr->pattern_tree);
1151 free_pattern_tree(&expr->pattern_tree_2);
1152 LIST_INIT(&expr->patterns);
1153}
1154
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001155/*
1156 *
1157 * The following functions are used for the pattern indexation
1158 *
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001159 */
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001160
1161int pat_idx_list_val(struct pattern_expr *expr, struct pattern *pat, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001162{
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001163 struct pattern_list *patl;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001164
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001165 /* allocate pattern */
1166 patl = calloc(1, sizeof(*patl));
1167 if (!patl) {
1168 memprintf(err, "out of memory while indexing pattern");
Thierry FOURNIER972028f2014-01-23 17:53:31 +01001169 return 0;
1170 }
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001171
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001172 /* duplicate pattern */
1173 memcpy(&patl->pat, pat, sizeof(*pat));
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001174
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001175 /* chain pattern in the expression */
1176 LIST_ADDQ(&expr->patterns, &patl->list);
Willy Tarreau72f073b2015-04-29 17:53:47 +02001177 expr->revision = rdtsc();
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001178
1179 /* that's ok */
1180 return 1;
1181}
1182
1183int pat_idx_list_ptr(struct pattern_expr *expr, struct pattern *pat, char **err)
1184{
1185 struct pattern_list *patl;
1186
1187 /* allocate pattern */
1188 patl = calloc(1, sizeof(*patl));
Thierry FOURNIER8aa83842015-02-06 17:50:55 +01001189 if (!patl) {
1190 memprintf(err, "out of memory while indexing pattern");
Thierry FOURNIER972028f2014-01-23 17:53:31 +01001191 return 0;
Thierry FOURNIER8aa83842015-02-06 17:50:55 +01001192 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001193
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001194 /* duplicate pattern */
1195 memcpy(&patl->pat, pat, sizeof(*pat));
1196 patl->pat.ptr.ptr = malloc(patl->pat.len);
1197 if (!patl->pat.ptr.ptr) {
1198 free(patl);
1199 memprintf(err, "out of memory while indexing pattern");
1200 return 0;
1201 }
1202 memcpy(patl->pat.ptr.ptr, pat->ptr.ptr, pat->len);
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001203
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001204 /* chain pattern in the expression */
1205 LIST_ADDQ(&expr->patterns, &patl->list);
Willy Tarreau72f073b2015-04-29 17:53:47 +02001206 expr->revision = rdtsc();
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001207
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001208 /* that's ok */
1209 return 1;
1210}
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001211
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001212int pat_idx_list_str(struct pattern_expr *expr, struct pattern *pat, char **err)
1213{
1214 struct pattern_list *patl;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001215
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001216 /* allocate pattern */
1217 patl = calloc(1, sizeof(*patl));
1218 if (!patl) {
1219 memprintf(err, "out of memory while indexing pattern");
1220 return 0;
1221 }
1222
1223 /* duplicate pattern */
1224 memcpy(&patl->pat, pat, sizeof(*pat));
1225 patl->pat.ptr.str = malloc(patl->pat.len + 1);
1226 if (!patl->pat.ptr.str) {
1227 free(patl);
1228 memprintf(err, "out of memory while indexing pattern");
1229 return 0;
1230 }
1231 memcpy(patl->pat.ptr.ptr, pat->ptr.ptr, pat->len);
1232 patl->pat.ptr.str[patl->pat.len] = '\0';
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001233
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001234 /* chain pattern in the expression */
1235 LIST_ADDQ(&expr->patterns, &patl->list);
Willy Tarreau72f073b2015-04-29 17:53:47 +02001236 expr->revision = rdtsc();
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001237
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001238 /* that's ok */
1239 return 1;
1240}
1241
Thierry Fournier8feaa662016-02-10 22:55:20 +01001242int pat_idx_list_reg_cap(struct pattern_expr *expr, struct pattern *pat, int cap, char **err)
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001243{
1244 struct pattern_list *patl;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001245
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001246 /* allocate pattern */
1247 patl = calloc(1, sizeof(*patl));
1248 if (!patl) {
1249 memprintf(err, "out of memory while indexing pattern");
1250 return 0;
Thierry FOURNIER972028f2014-01-23 17:53:31 +01001251 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001252
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001253 /* duplicate pattern */
1254 memcpy(&patl->pat, pat, sizeof(*pat));
1255
1256 /* allocate regex */
1257 patl->pat.ptr.reg = calloc(1, sizeof(*patl->pat.ptr.reg));
1258 if (!patl->pat.ptr.reg) {
1259 free(patl);
1260 memprintf(err, "out of memory while indexing pattern");
1261 return 0;
1262 }
1263
1264 /* compile regex */
Thierry Fournier8feaa662016-02-10 22:55:20 +01001265 if (!regex_comp(pat->ptr.str, patl->pat.ptr.reg,
1266 !(expr->mflags & PAT_MF_IGNORE_CASE), cap, err)) {
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001267 free(patl->pat.ptr.reg);
Dirkjan Bussink07fcaaa2014-04-28 22:57:16 +00001268 free(patl);
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001269 return 0;
1270 }
1271
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001272 /* chain pattern in the expression */
1273 LIST_ADDQ(&expr->patterns, &patl->list);
Willy Tarreau72f073b2015-04-29 17:53:47 +02001274 expr->revision = rdtsc();
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001275
1276 /* that's ok */
1277 return 1;
1278}
1279
Thierry Fournier8feaa662016-02-10 22:55:20 +01001280int pat_idx_list_reg(struct pattern_expr *expr, struct pattern *pat, char **err)
1281{
1282 return pat_idx_list_reg_cap(expr, pat, 0, err);
1283}
1284
1285int pat_idx_list_regm(struct pattern_expr *expr, struct pattern *pat, char **err)
1286{
1287 return pat_idx_list_reg_cap(expr, pat, 1, err);
1288}
1289
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001290int pat_idx_tree_ip(struct pattern_expr *expr, struct pattern *pat, char **err)
1291{
1292 unsigned int mask;
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +01001293 struct pattern_tree *node;
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001294
1295 /* Only IPv4 can be indexed */
1296 if (pat->type == SMP_T_IPV4) {
Thierry FOURNIER972028f2014-01-23 17:53:31 +01001297 /* in IPv4 case, check if the mask is contiguous so that we can
1298 * insert the network into the tree. A continuous mask has only
1299 * ones on the left. This means that this mask + its lower bit
1300 * added once again is null.
1301 */
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001302 mask = ntohl(pat->val.ipv4.mask.s_addr);
1303 if (mask + (mask & -mask) == 0) {
1304 mask = mask ? 33 - flsnz(mask & -mask) : 0; /* equals cidr value */
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001305
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001306 /* node memory allocation */
1307 node = calloc(1, sizeof(*node) + 4);
1308 if (!node) {
1309 memprintf(err, "out of memory while loading pattern");
1310 return 0;
1311 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001312
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001313 /* copy the pointer to sample associated to this node */
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001314 node->data = pat->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01001315 node->ref = pat->ref;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001316
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001317 /* FIXME: insert <addr>/<mask> into the tree here */
1318 memcpy(node->node.key, &pat->val.ipv4.addr, 4); /* network byte order */
1319 node->node.node.pfx = mask;
Thierry FOURNIER31db4ae2014-01-30 00:27:15 +01001320
1321 /* Insert the entry. */
1322 ebmb_insert_prefix(&expr->pattern_tree, &node->node, 4);
Willy Tarreau72f073b2015-04-29 17:53:47 +02001323 expr->revision = rdtsc();
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001324
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001325 /* that's ok */
1326 return 1;
1327 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001328 else {
1329 /* If the mask is not contiguous, just add the pattern to the list */
1330 return pat_idx_list_val(expr, pat, err);
1331 }
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001332 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001333 else if (pat->type == SMP_T_IPV6) {
1334 /* IPv6 also can be indexed */
1335 node = calloc(1, sizeof(*node) + 16);
1336 if (!node) {
1337 memprintf(err, "out of memory while loading pattern");
1338 return 0;
1339 }
1340
1341 /* copy the pointer to sample associated to this node */
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001342 node->data = pat->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01001343 node->ref = pat->ref;
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001344
1345 /* FIXME: insert <addr>/<mask> into the tree here */
1346 memcpy(node->node.key, &pat->val.ipv6.addr, 16); /* network byte order */
1347 node->node.node.pfx = pat->val.ipv6.mask;
Thierry FOURNIER31db4ae2014-01-30 00:27:15 +01001348
1349 /* Insert the entry. */
1350 ebmb_insert_prefix(&expr->pattern_tree_2, &node->node, 16);
Willy Tarreau72f073b2015-04-29 17:53:47 +02001351 expr->revision = rdtsc();
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001352
1353 /* that's ok */
1354 return 1;
1355 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001356
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001357 return 0;
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001358}
1359
1360int pat_idx_tree_str(struct pattern_expr *expr, struct pattern *pat, char **err)
1361{
1362 int len;
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +01001363 struct pattern_tree *node;
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001364
1365 /* Only string can be indexed */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001366 if (pat->type != SMP_T_STR) {
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001367 memprintf(err, "internal error: string expected, but the type is '%s'",
1368 smp_to_type[pat->type]);
1369 return 0;
Thierry FOURNIER972028f2014-01-23 17:53:31 +01001370 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001371
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001372 /* If the flag PAT_F_IGNORE_CASE is set, we cannot use trees */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02001373 if (expr->mflags & PAT_MF_IGNORE_CASE)
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001374 return pat_idx_list_str(expr, pat, err);
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001375
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001376 /* Process the key len */
1377 len = strlen(pat->ptr.str) + 1;
1378
1379 /* node memory allocation */
1380 node = calloc(1, sizeof(*node) + len);
1381 if (!node) {
1382 memprintf(err, "out of memory while loading pattern");
1383 return 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001384 }
1385
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001386 /* copy the pointer to sample associated to this node */
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001387 node->data = pat->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01001388 node->ref = pat->ref;
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001389
1390 /* copy the string */
1391 memcpy(node->node.key, pat->ptr.str, len);
1392
1393 /* index the new node */
Thierry FOURNIER31db4ae2014-01-30 00:27:15 +01001394 ebst_insert(&expr->pattern_tree, &node->node);
Willy Tarreau72f073b2015-04-29 17:53:47 +02001395 expr->revision = rdtsc();
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001396
1397 /* that's ok */
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001398 return 1;
1399}
1400
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +02001401int pat_idx_tree_pfx(struct pattern_expr *expr, struct pattern *pat, char **err)
1402{
1403 int len;
1404 struct pattern_tree *node;
1405
1406 /* Only string can be indexed */
1407 if (pat->type != SMP_T_STR) {
1408 memprintf(err, "internal error: string expected, but the type is '%s'",
1409 smp_to_type[pat->type]);
1410 return 0;
1411 }
1412
1413 /* If the flag PAT_F_IGNORE_CASE is set, we cannot use trees */
1414 if (expr->mflags & PAT_MF_IGNORE_CASE)
1415 return pat_idx_list_str(expr, pat, err);
1416
1417 /* Process the key len */
1418 len = strlen(pat->ptr.str);
1419
1420 /* node memory allocation */
1421 node = calloc(1, sizeof(*node) + len + 1);
1422 if (!node) {
1423 memprintf(err, "out of memory while loading pattern");
1424 return 0;
1425 }
1426
1427 /* copy the pointer to sample associated to this node */
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001428 node->data = pat->data;
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +02001429 node->ref = pat->ref;
1430
1431 /* copy the string and the trailing zero */
1432 memcpy(node->node.key, pat->ptr.str, len + 1);
1433 node->node.node.pfx = len * 8;
1434
1435 /* index the new node */
1436 ebmb_insert_prefix(&expr->pattern_tree, &node->node, len);
Willy Tarreau72f073b2015-04-29 17:53:47 +02001437 expr->revision = rdtsc();
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +02001438
1439 /* that's ok */
1440 return 1;
1441}
1442
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001443void pat_del_list_val(struct pattern_expr *expr, struct pat_ref_elt *ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001444{
1445 struct pattern_list *pat;
1446 struct pattern_list *safe;
1447
1448 list_for_each_entry_safe(pat, safe, &expr->patterns, list) {
1449 /* Check equality. */
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001450 if (pat->pat.ref != ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001451 continue;
1452
1453 /* Delete and free entry. */
1454 LIST_DEL(&pat->list);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001455 free(pat->pat.data);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001456 free(pat);
1457 }
Willy Tarreau72f073b2015-04-29 17:53:47 +02001458 expr->revision = rdtsc();
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001459}
1460
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001461void pat_del_tree_ip(struct pattern_expr *expr, struct pat_ref_elt *ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001462{
1463 struct ebmb_node *node, *next_node;
1464 struct pattern_tree *elt;
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001465
1466 /* browse each node of the tree for IPv4 addresses. */
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001467 for (node = ebmb_first(&expr->pattern_tree), next_node = node ? ebmb_next(node) : NULL;
1468 node;
1469 node = next_node, next_node = node ? ebmb_next(node) : NULL) {
1470 /* Extract container of the tree node. */
1471 elt = container_of(node, struct pattern_tree, node);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001472
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001473 /* Check equality. */
1474 if (elt->ref != ref)
1475 continue;
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001476
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001477 /* Delete and free entry. */
1478 ebmb_delete(node);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001479 free(elt->data);
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001480 free(elt);
1481 }
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001482
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001483 /* Browse each node of the list for IPv4 addresses. */
1484 pat_del_list_val(expr, ref);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001485
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001486 /* browse each node of the tree for IPv6 addresses. */
1487 for (node = ebmb_first(&expr->pattern_tree_2), next_node = node ? ebmb_next(node) : NULL;
1488 node;
1489 node = next_node, next_node = node ? ebmb_next(node) : NULL) {
1490 /* Extract container of the tree node. */
1491 elt = container_of(node, struct pattern_tree, node);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001492
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001493 /* Check equality. */
1494 if (elt->ref != ref)
1495 continue;
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001496
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001497 /* Delete and free entry. */
1498 ebmb_delete(node);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001499 free(elt->data);
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001500 free(elt);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001501 }
Willy Tarreau72f073b2015-04-29 17:53:47 +02001502 expr->revision = rdtsc();
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001503}
1504
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001505void pat_del_list_ptr(struct pattern_expr *expr, struct pat_ref_elt *ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001506{
1507 struct pattern_list *pat;
1508 struct pattern_list *safe;
1509
1510 list_for_each_entry_safe(pat, safe, &expr->patterns, list) {
1511 /* Check equality. */
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001512 if (pat->pat.ref != ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001513 continue;
1514
1515 /* Delete and free entry. */
1516 LIST_DEL(&pat->list);
1517 free(pat->pat.ptr.ptr);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001518 free(pat->pat.data);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001519 free(pat);
1520 }
Willy Tarreau72f073b2015-04-29 17:53:47 +02001521 expr->revision = rdtsc();
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001522}
1523
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001524void pat_del_tree_str(struct pattern_expr *expr, struct pat_ref_elt *ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001525{
1526 struct ebmb_node *node, *next_node;
1527 struct pattern_tree *elt;
1528
Thierry FOURNIER73bc2852015-02-06 17:53:54 +01001529 /* If the flag PAT_F_IGNORE_CASE is set, we cannot use trees */
1530 if (expr->mflags & PAT_MF_IGNORE_CASE)
1531 return pat_del_list_ptr(expr, ref);
1532
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001533 /* browse each node of the tree. */
1534 for (node = ebmb_first(&expr->pattern_tree), next_node = node ? ebmb_next(node) : NULL;
1535 node;
1536 node = next_node, next_node = node ? ebmb_next(node) : NULL) {
1537 /* Extract container of the tree node. */
1538 elt = container_of(node, struct pattern_tree, node);
1539
1540 /* Check equality. */
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001541 if (elt->ref != ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001542 continue;
1543
1544 /* Delete and free entry. */
1545 ebmb_delete(node);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001546 free(elt->data);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001547 free(elt);
1548 }
Willy Tarreau72f073b2015-04-29 17:53:47 +02001549 expr->revision = rdtsc();
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001550}
1551
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001552void pat_del_list_reg(struct pattern_expr *expr, struct pat_ref_elt *ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001553{
1554 struct pattern_list *pat;
1555 struct pattern_list *safe;
1556
1557 list_for_each_entry_safe(pat, safe, &expr->patterns, list) {
1558 /* Check equality. */
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001559 if (pat->pat.ref != ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001560 continue;
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001561
1562 /* Delete and free entry. */
1563 LIST_DEL(&pat->list);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001564 regex_free(pat->pat.ptr.ptr);
Dragan Dosene99af972019-04-30 01:03:26 +02001565 free(pat->pat.ptr.ptr);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001566 free(pat->pat.data);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001567 free(pat);
1568 }
Willy Tarreau72f073b2015-04-29 17:53:47 +02001569 expr->revision = rdtsc();
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001570}
1571
1572void pattern_init_expr(struct pattern_expr *expr)
1573{
1574 LIST_INIT(&expr->patterns);
Willy Tarreau72f073b2015-04-29 17:53:47 +02001575 expr->revision = 0;
Thierry FOURNIER31db4ae2014-01-30 00:27:15 +01001576 expr->pattern_tree = EB_ROOT;
1577 expr->pattern_tree_2 = EB_ROOT;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001578}
1579
1580void pattern_init_head(struct pattern_head *head)
1581{
1582 LIST_INIT(&head->head);
1583}
1584
1585/* The following functions are relative to the management of the reference
1586 * lists. These lists are used to store the original pattern and associated
1587 * value as string form.
1588 *
1589 * This is used with modifiable ACL and MAPS
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001590 *
1591 * The pattern reference are stored with two identifiers: the unique_id and
1592 * the reference.
1593 *
1594 * The reference identify a file. Each file with the same name point to the
1595 * same reference. We can register many times one file. If the file is modified,
1596 * all his dependencies are also modified. The reference can be used with map or
1597 * acl.
1598 *
1599 * The unique_id identify inline acl. The unique id is unique for each acl.
1600 * You cannot force the same id in the configuration file, because this repoort
1601 * an error.
1602 *
1603 * A particular case appears if the filename is a number. In this case, the
1604 * unique_id is set with the number represented by the filename and the
1605 * reference is also set. This method prevent double unique_id.
1606 *
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001607 */
1608
1609/* This function lookup for reference. If the reference is found, they return
1610 * pointer to the struct pat_ref, else return NULL.
1611 */
1612struct pat_ref *pat_ref_lookup(const char *reference)
1613{
1614 struct pat_ref *ref;
1615
1616 list_for_each_entry(ref, &pattern_reference, list)
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001617 if (ref->reference && strcmp(reference, ref->reference) == 0)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001618 return ref;
1619 return NULL;
1620}
1621
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001622/* This function lookup for unique id. If the reference is found, they return
1623 * pointer to the struct pat_ref, else return NULL.
1624 */
1625struct pat_ref *pat_ref_lookupid(int unique_id)
1626{
1627 struct pat_ref *ref;
1628
1629 list_for_each_entry(ref, &pattern_reference, list)
1630 if (ref->unique_id == unique_id)
1631 return ref;
1632 return NULL;
1633}
1634
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001635/* This function remove all pattern matching the pointer <refelt> from
1636 * the the reference and from each expr member of the reference. This
1637 * function returns 1 if the deletion is done and return 0 is the entry
1638 * is not found.
1639 */
1640int pat_ref_delete_by_id(struct pat_ref *ref, struct pat_ref_elt *refelt)
1641{
1642 struct pattern_expr *expr;
1643 struct pat_ref_elt *elt, *safe;
Emeric Brun8d85aa42017-06-29 15:40:33 +02001644 struct bref *bref, *back;
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001645
1646 /* delete pattern from reference */
1647 list_for_each_entry_safe(elt, safe, &ref->head, list) {
1648 if (elt == refelt) {
Emeric Brun8d85aa42017-06-29 15:40:33 +02001649 list_for_each_entry_safe(bref, back, &elt->back_refs, users) {
1650 /*
1651 * we have to unlink all watchers. We must not relink them if
1652 * this elt was the last one in the list.
1653 */
1654 LIST_DEL(&bref->users);
1655 LIST_INIT(&bref->users);
1656 if (elt->list.n != &ref->head)
Willy Tarreau49ee3b22019-04-30 11:43:43 +02001657 LIST_ADDQ(&LIST_ELEM(elt->list.n, typeof(elt), list)->back_refs, &bref->users);
Emeric Brun8d85aa42017-06-29 15:40:33 +02001658 bref->ref = elt->list.n;
1659 }
peter caiaede6dd2015-10-07 00:07:43 -07001660 list_for_each_entry(expr, &ref->pat, list)
1661 pattern_delete(expr, elt);
1662
Emeric Brunb5997f72017-07-03 11:34:05 +02001663 /* pat_ref_elt is trashed once all expr
1664 are cleaned and there is no ref remaining */
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001665 LIST_DEL(&elt->list);
1666 free(elt->sample);
1667 free(elt->pattern);
1668 free(elt);
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001669 return 1;
1670 }
1671 }
1672 return 0;
1673}
1674
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001675/* This function remove all pattern match <key> from the the reference
Joseph Herlant4189d672018-11-15 10:22:31 -08001676 * and from each expr member of the reference. This function returns 1
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001677 * if the deletion is done and return 0 is the entry is not found.
1678 */
1679int pat_ref_delete(struct pat_ref *ref, const char *key)
1680{
1681 struct pattern_expr *expr;
1682 struct pat_ref_elt *elt, *safe;
Emeric Brun8d85aa42017-06-29 15:40:33 +02001683 struct bref *bref, *back;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001684 int found = 0;
1685
1686 /* delete pattern from reference */
1687 list_for_each_entry_safe(elt, safe, &ref->head, list) {
1688 if (strcmp(key, elt->pattern) == 0) {
Emeric Brun8d85aa42017-06-29 15:40:33 +02001689 list_for_each_entry_safe(bref, back, &elt->back_refs, users) {
1690 /*
1691 * we have to unlink all watchers. We must not relink them if
1692 * this elt was the last one in the list.
1693 */
1694 LIST_DEL(&bref->users);
1695 LIST_INIT(&bref->users);
1696 if (elt->list.n != &ref->head)
Willy Tarreau49ee3b22019-04-30 11:43:43 +02001697 LIST_ADDQ(&LIST_ELEM(elt->list.n, typeof(elt), list)->back_refs, &bref->users);
Emeric Brun8d85aa42017-06-29 15:40:33 +02001698 bref->ref = elt->list.n;
1699 }
Dirkjan Bussink07fcaaa2014-04-28 22:57:16 +00001700 list_for_each_entry(expr, &ref->pat, list)
1701 pattern_delete(expr, elt);
1702
Emeric Brunb5997f72017-07-03 11:34:05 +02001703 /* pat_ref_elt is trashed once all expr
1704 are cleaned and there is no ref remaining */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001705 LIST_DEL(&elt->list);
1706 free(elt->sample);
1707 free(elt->pattern);
1708 free(elt);
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001709
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001710 found = 1;
1711 }
1712 }
1713
1714 if (!found)
1715 return 0;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001716 return 1;
1717}
1718
Baptiste Assmann953f74d2014-04-25 16:57:03 +02001719/*
1720 * find and return an element <elt> matching <key> in a reference <ref>
1721 * return NULL if not found
1722 */
1723struct pat_ref_elt *pat_ref_find_elt(struct pat_ref *ref, const char *key)
1724{
1725 struct pat_ref_elt *elt;
1726
1727 list_for_each_entry(elt, &ref->head, list) {
1728 if (strcmp(key, elt->pattern) == 0)
1729 return elt;
1730 }
1731
1732 return NULL;
1733}
1734
1735
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001736 /* This function modify the sample of the first pattern that match the <key>. */
1737static inline int pat_ref_set_elt(struct pat_ref *ref, struct pat_ref_elt *elt,
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001738 const char *value, char **err)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001739{
1740 struct pattern_expr *expr;
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001741 struct sample_data **data;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001742 char *sample;
Thierry FOURNIER12ba0c22015-08-14 00:02:11 +02001743 struct sample_data test;
Thierry FOURNIER149e0fe2014-01-29 19:35:06 +01001744
1745 /* Try all needed converters. */
1746 list_for_each_entry(expr, &ref->pat, list) {
1747 if (!expr->pat_head->parse_smp)
1748 continue;
1749
1750 if (!expr->pat_head->parse_smp(value, &test)) {
1751 memprintf(err, "unable to parse '%s'", value);
1752 return 0;
1753 }
1754 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001755
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001756 /* Modify pattern from reference. */
1757 sample = strdup(value);
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001758 if (!sample) {
1759 memprintf(err, "out of memory error");
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001760 return 0;
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001761 }
Thierry FOURNIER149e0fe2014-01-29 19:35:06 +01001762 /* Load sample in each reference. All the conversion are tested
1763 * below, normally these calls dosn't fail.
1764 */
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001765 list_for_each_entry(expr, &ref->pat, list) {
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001766 if (!expr->pat_head->parse_smp)
1767 continue;
1768
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001769 HA_RWLOCK_WRLOCK(PATEXP_LOCK, &expr->lock);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001770 data = pattern_find_smp(expr, elt);
1771 if (data && *data && !expr->pat_head->parse_smp(sample, *data))
1772 *data = NULL;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001773 HA_RWLOCK_WRUNLOCK(PATEXP_LOCK, &expr->lock);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001774 }
1775
Emeric Brunb5997f72017-07-03 11:34:05 +02001776 /* free old sample only when all exprs are updated */
1777 free(elt->sample);
1778 elt->sample = sample;
1779
1780
Thierry FOURNIER149e0fe2014-01-29 19:35:06 +01001781 return 1;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001782}
1783
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001784/* This function modify the sample of the first pattern that match the <key>. */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001785int 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 +01001786{
1787 struct pat_ref_elt *elt;
1788
1789 /* Look for pattern in the reference. */
1790 list_for_each_entry(elt, &ref->head, list) {
1791 if (elt == refelt) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001792 if (!pat_ref_set_elt(ref, elt, value, err))
1793 return 0;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001794 return 1;
1795 }
1796 }
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001797
1798 memprintf(err, "key or pattern not found");
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001799 return 0;
1800}
1801
1802/* This function modify the sample of the first pattern that match the <key>. */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001803int pat_ref_set(struct pat_ref *ref, const char *key, const char *value, char **err)
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001804{
1805 struct pat_ref_elt *elt;
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001806 int found = 0;
1807 char *_merr;
1808 char **merr;
1809
1810 if (err) {
1811 merr = &_merr;
1812 *merr = NULL;
1813 }
1814 else
1815 merr = NULL;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001816
1817 /* Look for pattern in the reference. */
1818 list_for_each_entry(elt, &ref->head, list) {
1819 if (strcmp(key, elt->pattern) == 0) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001820 if (!pat_ref_set_elt(ref, elt, value, merr)) {
William Lallemand579fb252018-06-11 10:53:46 +02001821 if (err && merr) {
1822 if (!found) {
1823 *err = *merr;
1824 } else {
1825 memprintf(err, "%s, %s", *err, *merr);
1826 free(*merr);
1827 *merr = NULL;
1828 }
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001829 }
1830 }
1831 found = 1;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001832 }
1833 }
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001834
1835 if (!found) {
1836 memprintf(err, "entry not found");
1837 return 0;
1838 }
1839 return 1;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001840}
1841
Joseph Herlant4189d672018-11-15 10:22:31 -08001842/* This function creates a new reference. <ref> is the reference name.
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001843 * <flags> are PAT_REF_*. /!\ The reference is not checked, and must
1844 * be unique. The user must check the reference with "pat_ref_lookup()"
Joseph Herlant4189d672018-11-15 10:22:31 -08001845 * before calling this function. If the function fail, it return NULL,
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001846 * else return new struct pat_ref.
1847 */
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001848struct pat_ref *pat_ref_new(const char *reference, const char *display, unsigned int flags)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001849{
1850 struct pat_ref *ref;
1851
1852 ref = malloc(sizeof(*ref));
1853 if (!ref)
1854 return NULL;
1855
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001856 if (display) {
1857 ref->display = strdup(display);
1858 if (!ref->display) {
1859 free(ref);
1860 return NULL;
1861 }
1862 }
1863 else
1864 ref->display = NULL;
1865
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001866 ref->reference = strdup(reference);
1867 if (!ref->reference) {
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001868 free(ref->display);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001869 free(ref);
1870 return NULL;
1871 }
1872
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001873 ref->flags = flags;
1874 ref->unique_id = -1;
1875
1876 LIST_INIT(&ref->head);
1877 LIST_INIT(&ref->pat);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001878 HA_SPIN_INIT(&ref->lock);
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001879 LIST_ADDQ(&pattern_reference, &ref->list);
1880
1881 return ref;
1882}
1883
1884/* This function create new reference. <unique_id> is the unique id. If
1885 * the value of <unique_id> is -1, the unique id is calculated later.
1886 * <flags> are PAT_REF_*. /!\ The reference is not checked, and must
1887 * be unique. The user must check the reference with "pat_ref_lookup()"
1888 * or pat_ref_lookupid before calling this function. If the function
1889 * fail, it return NULL, else return new struct pat_ref.
1890 */
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001891struct pat_ref *pat_ref_newid(int unique_id, const char *display, unsigned int flags)
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001892{
1893 struct pat_ref *ref;
1894
1895 ref = malloc(sizeof(*ref));
1896 if (!ref)
1897 return NULL;
1898
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001899 if (display) {
1900 ref->display = strdup(display);
1901 if (!ref->display) {
1902 free(ref);
1903 return NULL;
1904 }
1905 }
1906 else
1907 ref->display = NULL;
1908
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001909 ref->reference = NULL;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001910 ref->flags = flags;
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001911 ref->unique_id = unique_id;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001912 LIST_INIT(&ref->head);
1913 LIST_INIT(&ref->pat);
Aurélien Nephtali564d15a2018-04-19 16:56:07 +02001914 HA_SPIN_INIT(&ref->lock);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001915 LIST_ADDQ(&pattern_reference, &ref->list);
1916
1917 return ref;
1918}
1919
1920/* This function adds entry to <ref>. It can failed with memory error.
1921 * If the function fails, it returns 0.
1922 */
1923int pat_ref_append(struct pat_ref *ref, char *pattern, char *sample, int line)
1924{
1925 struct pat_ref_elt *elt;
1926
1927 elt = malloc(sizeof(*elt));
1928 if (!elt)
1929 return 0;
1930
1931 elt->line = line;
1932
1933 elt->pattern = strdup(pattern);
1934 if (!elt->pattern) {
1935 free(elt);
1936 return 0;
1937 }
1938
1939 if (sample) {
1940 elt->sample = strdup(sample);
1941 if (!elt->sample) {
1942 free(elt->pattern);
1943 free(elt);
1944 return 0;
1945 }
1946 }
1947 else
1948 elt->sample = NULL;
1949
Emeric Brun8d85aa42017-06-29 15:40:33 +02001950 LIST_INIT(&elt->back_refs);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001951 LIST_ADDQ(&ref->head, &elt->list);
1952
1953 return 1;
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001954}
1955
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001956/* This function create sample found in <elt>, parse the pattern also
1957 * found in <elt> and insert it in <expr>. The function copy <patflags>
1958 * in <expr>. If the function fails, it returns0 and <err> is filled.
1959 * In succes case, the function returns 1.
1960 */
1961static inline
1962int pat_ref_push(struct pat_ref_elt *elt, struct pattern_expr *expr,
1963 int patflags, char **err)
1964{
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001965 struct sample_data *data;
Thierry FOURNIERd25c8422014-01-28 15:34:35 +01001966 struct pattern pattern;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001967
1968 /* Create sample */
1969 if (elt->sample && expr->pat_head->parse_smp) {
1970 /* New sample. */
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001971 data = malloc(sizeof(*data));
1972 if (!data)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001973 return 0;
1974
1975 /* Parse value. */
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001976 if (!expr->pat_head->parse_smp(elt->sample, data)) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001977 memprintf(err, "unable to parse '%s'", elt->sample);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001978 free(data);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001979 return 0;
1980 }
1981
1982 }
1983 else
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001984 data = NULL;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001985
Thierry FOURNIERd25c8422014-01-28 15:34:35 +01001986 /* initialise pattern */
1987 memset(&pattern, 0, sizeof(pattern));
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001988 pattern.data = data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01001989 pattern.ref = elt;
Thierry FOURNIERd25c8422014-01-28 15:34:35 +01001990
1991 /* parse pattern */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02001992 if (!expr->pat_head->parse(elt->pattern, &pattern, expr->mflags, err)) {
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001993 free(data);
Thierry FOURNIERd25c8422014-01-28 15:34:35 +01001994 return 0;
1995 }
1996
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001997 HA_RWLOCK_WRLOCK(PATEXP_LOCK, &expr->lock);
Thierry FOURNIERd25c8422014-01-28 15:34:35 +01001998 /* index pattern */
1999 if (!expr->pat_head->index(expr, &pattern, err)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002000 HA_RWLOCK_WRUNLOCK(PATEXP_LOCK, &expr->lock);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02002001 free(data);
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01002002 return 0;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002003 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002004 HA_RWLOCK_WRUNLOCK(PATEXP_LOCK, &expr->lock);
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01002005
2006 return 1;
2007}
2008
Thierry FOURNIER31db4ae2014-01-30 00:27:15 +01002009/* This function adds entry to <ref>. It can failed with memory error. The new
2010 * entry is added at all the pattern_expr registered in this reference. The
2011 * function stop on the first error encountered. It returns 0 and err is
2012 * filled. If an error is encountered, the complete add operation is cancelled.
2013 * If the insertion is a success the function returns 1.
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002014 */
2015int pat_ref_add(struct pat_ref *ref,
2016 const char *pattern, const char *sample,
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02002017 char **err)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002018{
2019 struct pat_ref_elt *elt;
2020 struct pattern_expr *expr;
2021
2022 elt = malloc(sizeof(*elt));
2023 if (!elt) {
2024 memprintf(err, "out of memory error");
2025 return 0;
2026 }
2027
2028 elt->line = -1;
2029
2030 elt->pattern = strdup(pattern);
2031 if (!elt->pattern) {
2032 free(elt);
2033 memprintf(err, "out of memory error");
2034 return 0;
2035 }
2036
2037 if (sample) {
2038 elt->sample = strdup(sample);
2039 if (!elt->sample) {
2040 free(elt->pattern);
2041 free(elt);
2042 memprintf(err, "out of memory error");
2043 return 0;
2044 }
2045 }
2046 else
2047 elt->sample = NULL;
2048
Emeric Brun8d85aa42017-06-29 15:40:33 +02002049 LIST_INIT(&elt->back_refs);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002050 LIST_ADDQ(&ref->head, &elt->list);
2051
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002052 list_for_each_entry(expr, &ref->pat, list) {
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02002053 if (!pat_ref_push(elt, expr, 0, err)) {
Thierry FOURNIER31db4ae2014-01-30 00:27:15 +01002054 /* If the insertion fails, try to delete all the added entries. */
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01002055 pat_ref_delete_by_id(ref, elt);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002056 return 0;
2057 }
2058 }
Emeric Brunb5997f72017-07-03 11:34:05 +02002059
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002060 return 1;
2061}
2062
Joseph Herlant4189d672018-11-15 10:22:31 -08002063/* This function prunes <ref>, replaces all references by the references
2064 * of <replace>, and reindexes all the news values.
Thierry FOURNIER46006bd2014-03-21 21:45:15 +01002065 *
Joseph Herlant4189d672018-11-15 10:22:31 -08002066 * The patterns are loaded in best effort and the errors are ignored,
2067 * but written in the logs.
Thierry FOURNIER46006bd2014-03-21 21:45:15 +01002068 */
2069void pat_ref_reload(struct pat_ref *ref, struct pat_ref *replace)
2070{
2071 struct pattern_expr *expr;
Emeric Brunb5997f72017-07-03 11:34:05 +02002072 struct pat_ref_elt *elt, *safe;
2073 struct bref *bref, *back;
Emeric Brunb5997f72017-07-03 11:34:05 +02002074 struct pattern pattern;
Thierry FOURNIER46006bd2014-03-21 21:45:15 +01002075
Thierry FOURNIER46006bd2014-03-21 21:45:15 +01002076
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002077 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Emeric Brunb5997f72017-07-03 11:34:05 +02002078 list_for_each_entry(expr, &ref->pat, list) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002079 HA_RWLOCK_WRLOCK(PATEXP_LOCK, &expr->lock);
Emeric Brunb5997f72017-07-03 11:34:05 +02002080 }
2081
2082 /* all expr are locked, we can safely remove all pat_ref */
2083 list_for_each_entry_safe(elt, safe, &ref->head, list) {
2084 list_for_each_entry_safe(bref, back, &elt->back_refs, users) {
2085 /*
2086 * we have to unlink all watchers. We must not relink them if
2087 * this elt was the last one in the list.
2088 */
2089 LIST_DEL(&bref->users);
2090 LIST_INIT(&bref->users);
2091 if (elt->list.n != &ref->head)
Willy Tarreau49ee3b22019-04-30 11:43:43 +02002092 LIST_ADDQ(&LIST_ELEM(elt->list.n, typeof(elt), list)->back_refs, &bref->users);
Emeric Brunb5997f72017-07-03 11:34:05 +02002093 bref->ref = elt->list.n;
2094 }
2095 LIST_DEL(&elt->list);
2096 free(elt->pattern);
2097 free(elt->sample);
2098 free(elt);
2099 }
2100
2101 /* switch pat_ret_elt lists */
Thierry FOURNIER46006bd2014-03-21 21:45:15 +01002102 LIST_ADD(&replace->head, &ref->head);
2103 LIST_DEL(&replace->head);
2104
Emeric Brunb5997f72017-07-03 11:34:05 +02002105 list_for_each_entry(expr, &ref->pat, list) {
2106 expr->pat_head->prune(expr);
2107 list_for_each_entry(elt, &ref->head, list) {
Dragan Dosenf1474792018-09-18 20:18:09 +02002108 char *err = NULL;
2109 struct sample_data *data = NULL;
2110
Emeric Brunb5997f72017-07-03 11:34:05 +02002111 /* Create sample */
2112 if (elt->sample && expr->pat_head->parse_smp) {
2113 /* New sample. */
2114 data = malloc(sizeof(*data));
2115 if (!data)
2116 continue;
2117
2118 /* Parse value. */
2119 if (!expr->pat_head->parse_smp(elt->sample, data)) {
2120 memprintf(&err, "unable to parse '%s'", elt->sample);
2121 send_log(NULL, LOG_NOTICE, "%s", err);
2122 free(err);
2123 free(data);
2124 continue;
2125 }
2126
2127 }
Emeric Brunb5997f72017-07-03 11:34:05 +02002128
2129 /* initialise pattern */
2130 memset(&pattern, 0, sizeof(pattern));
2131 pattern.data = data;
2132 pattern.ref = elt;
2133
2134 /* parse pattern */
2135 if (!expr->pat_head->parse(elt->pattern, &pattern, expr->mflags, &err)) {
Thierry FOURNIER46006bd2014-03-21 21:45:15 +01002136 send_log(NULL, LOG_NOTICE, "%s", err);
2137 free(err);
Emeric Brunb5997f72017-07-03 11:34:05 +02002138 free(data);
2139 continue;
2140 }
2141
2142 /* index pattern */
2143 if (!expr->pat_head->index(expr, &pattern, &err)) {
2144 send_log(NULL, LOG_NOTICE, "%s", err);
2145 free(err);
2146 free(data);
2147 continue;
Thierry FOURNIER46006bd2014-03-21 21:45:15 +01002148 }
2149 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002150 HA_RWLOCK_WRUNLOCK(PATEXP_LOCK, &expr->lock);
Thierry FOURNIER46006bd2014-03-21 21:45:15 +01002151 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002152 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER46006bd2014-03-21 21:45:15 +01002153}
2154
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002155/* This function prune all entries of <ref>. This function
2156 * prune the associated pattern_expr.
2157 */
2158void pat_ref_prune(struct pat_ref *ref)
2159{
2160 struct pat_ref_elt *elt, *safe;
2161 struct pattern_expr *expr;
Emeric Brun8d85aa42017-06-29 15:40:33 +02002162 struct bref *bref, *back;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002163
Emeric Brunb5997f72017-07-03 11:34:05 +02002164 list_for_each_entry(expr, &ref->pat, list) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002165 HA_RWLOCK_WRLOCK(PATEXP_LOCK, &expr->lock);
Emeric Brunb5997f72017-07-03 11:34:05 +02002166 expr->pat_head->prune(expr);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002167 HA_RWLOCK_WRUNLOCK(PATEXP_LOCK, &expr->lock);
Emeric Brunb5997f72017-07-03 11:34:05 +02002168 }
2169
2170 /* we trash pat_ref_elt in a second time to ensure that data is
2171 free once there is no ref on it */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002172 list_for_each_entry_safe(elt, safe, &ref->head, list) {
Emeric Brun8d85aa42017-06-29 15:40:33 +02002173 list_for_each_entry_safe(bref, back, &elt->back_refs, users) {
2174 /*
2175 * we have to unlink all watchers. We must not relink them if
2176 * this elt was the last one in the list.
2177 */
2178 LIST_DEL(&bref->users);
2179 LIST_INIT(&bref->users);
2180 if (elt->list.n != &ref->head)
Willy Tarreau49ee3b22019-04-30 11:43:43 +02002181 LIST_ADDQ(&LIST_ELEM(elt->list.n, typeof(elt), list)->back_refs, &bref->users);
Emeric Brun8d85aa42017-06-29 15:40:33 +02002182 bref->ref = elt->list.n;
2183 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002184 LIST_DEL(&elt->list);
2185 free(elt->pattern);
2186 free(elt->sample);
2187 free(elt);
2188 }
2189
Emeric Brunb5997f72017-07-03 11:34:05 +02002190
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002191}
2192
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002193/* This function lookup for existing reference <ref> in pattern_head <head>. */
2194struct pattern_expr *pattern_lookup_expr(struct pattern_head *head, struct pat_ref *ref)
2195{
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002196 struct pattern_expr_list *expr;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002197
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002198 list_for_each_entry(expr, &head->head, list)
2199 if (expr->expr->ref == ref)
2200 return expr->expr;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002201 return NULL;
2202}
2203
Joseph Herlant4189d672018-11-15 10:22:31 -08002204/* This function creates new pattern_expr associated to the reference <ref>.
2205 * <ref> can be NULL. If an error occurs, the function returns NULL and
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002206 * <err> is filled. Otherwise, the function returns new pattern_expr linked
2207 * with <head> and <ref>.
Thierry FOURNIER315ec422014-11-24 11:14:42 +01002208 *
Joseph Herlant4189d672018-11-15 10:22:31 -08002209 * The returned value can be an already filled pattern list, in this case the
Thierry FOURNIER315ec422014-11-24 11:14:42 +01002210 * flag <reuse> is set.
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002211 */
Thierry FOURNIER315ec422014-11-24 11:14:42 +01002212struct pattern_expr *pattern_new_expr(struct pattern_head *head, struct pat_ref *ref,
Emeric Brun7d27f3c2017-07-03 17:54:23 +02002213 int patflags, char **err, int *reuse)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002214{
2215 struct pattern_expr *expr;
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002216 struct pattern_expr_list *list;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002217
Thierry FOURNIER315ec422014-11-24 11:14:42 +01002218 if (reuse)
2219 *reuse = 0;
2220
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002221 /* Memory and initialization of the chain element. */
2222 list = malloc(sizeof(*list));
2223 if (!list) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002224 memprintf(err, "out of memory");
2225 return NULL;
2226 }
2227
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002228 /* Look for existing similar expr. No that only the index, parse and
2229 * parse_smp function must be identical for having similar pattern.
Joseph Herlant4189d672018-11-15 10:22:31 -08002230 * The other function depends of these first.
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002231 */
2232 if (ref) {
2233 list_for_each_entry(expr, &ref->pat, list)
2234 if (expr->pat_head->index == head->index &&
2235 expr->pat_head->parse == head->parse &&
Emeric Brun7d27f3c2017-07-03 17:54:23 +02002236 expr->pat_head->parse_smp == head->parse_smp &&
2237 expr->mflags == patflags)
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002238 break;
2239 if (&expr->list == &ref->pat)
2240 expr = NULL;
2241 }
2242 else
2243 expr = NULL;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002244
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002245 /* If no similar expr was found, we create new expr. */
2246 if (!expr) {
2247 /* Get a lot of memory for the expr struct. */
2248 expr = malloc(sizeof(*expr));
2249 if (!expr) {
Andreas Seltenreiche6e22e82016-03-03 20:20:23 +01002250 free(list);
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002251 memprintf(err, "out of memory");
2252 return NULL;
2253 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002254
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002255 /* Initialize this new expr. */
2256 pattern_init_expr(expr);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002257
Emeric Brun7d27f3c2017-07-03 17:54:23 +02002258 /* Copy the pattern matching and indexing flags. */
2259 expr->mflags = patflags;
2260
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002261 /* This new pattern expression reference one of his heads. */
2262 expr->pat_head = head;
2263
2264 /* Link with ref, or to self to facilitate LIST_DEL() */
2265 if (ref)
2266 LIST_ADDQ(&ref->pat, &expr->list);
2267 else
2268 LIST_INIT(&expr->list);
2269
2270 expr->ref = ref;
2271
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002272 HA_RWLOCK_INIT(&expr->lock);
Emeric Brunb5997f72017-07-03 11:34:05 +02002273
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002274 /* We must free this pattern if it is no more used. */
2275 list->do_free = 1;
2276 }
2277 else {
2278 /* If the pattern used already exists, it is already linked
2279 * with ref and we must not free it.
2280 */
2281 list->do_free = 0;
Thierry FOURNIER315ec422014-11-24 11:14:42 +01002282 if (reuse)
2283 *reuse = 1;
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002284 }
2285
2286 /* The new list element reference the pattern_expr. */
2287 list->expr = expr;
2288
2289 /* Link the list element with the pattern_head. */
2290 LIST_ADDQ(&head->head, &list->list);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002291 return expr;
2292}
2293
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002294/* Reads patterns from a file. If <err_msg> is non-NULL, an error message will
2295 * be returned there on errors and the caller will have to free it.
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002296 *
2297 * The file contains one key + value per line. Lines which start with '#' are
2298 * ignored, just like empty lines. Leading tabs/spaces are stripped. The key is
2299 * then the first "word" (series of non-space/tabs characters), and the value is
2300 * what follows this series of space/tab till the end of the line excluding
2301 * trailing spaces/tabs.
2302 *
2303 * Example :
2304 *
2305 * # this is a comment and is ignored
2306 * 62.212.114.60 1wt.eu \n
2307 * <-><-----------><---><----><---->
2308 * | | | | `--- trailing spaces ignored
2309 * | | | `-------- value
2310 * | | `--------------- middle spaces ignored
2311 * | `------------------------ key
2312 * `-------------------------------- leading spaces ignored
2313 *
2314 * Return non-zero in case of succes, otherwise 0.
2315 */
2316int pat_ref_read_from_file_smp(struct pat_ref *ref, const char *filename, char **err)
2317{
2318 FILE *file;
2319 char *c;
2320 int ret = 0;
2321 int line = 0;
2322 char *key_beg;
2323 char *key_end;
2324 char *value_beg;
2325 char *value_end;
2326
2327 file = fopen(filename, "r");
2328 if (!file) {
2329 memprintf(err, "failed to open pattern file <%s>", filename);
2330 return 0;
2331 }
2332
2333 /* now parse all patterns. The file may contain only one pattern
2334 * followed by one value per line. The start spaces, separator spaces
2335 * and and spaces are stripped. Each can contain comment started by '#'
2336 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002337 while (fgets(trash.area, trash.size, file) != NULL) {
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002338 line++;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002339 c = trash.area;
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002340
2341 /* ignore lines beginning with a dash */
2342 if (*c == '#')
2343 continue;
2344
2345 /* strip leading spaces and tabs */
2346 while (*c == ' ' || *c == '\t')
2347 c++;
2348
2349 /* empty lines are ignored too */
2350 if (*c == '\0' || *c == '\r' || *c == '\n')
2351 continue;
2352
2353 /* look for the end of the key */
2354 key_beg = c;
2355 while (*c && *c != ' ' && *c != '\t' && *c != '\n' && *c != '\r')
2356 c++;
2357
2358 key_end = c;
2359
2360 /* strip middle spaces and tabs */
2361 while (*c == ' ' || *c == '\t')
2362 c++;
2363
2364 /* look for the end of the value, it is the end of the line */
2365 value_beg = c;
2366 while (*c && *c != '\n' && *c != '\r')
2367 c++;
2368 value_end = c;
2369
2370 /* trim possibly trailing spaces and tabs */
2371 while (value_end > value_beg && (value_end[-1] == ' ' || value_end[-1] == '\t'))
2372 value_end--;
2373
2374 /* set final \0 and check entries */
2375 *key_end = '\0';
2376 *value_end = '\0';
2377
2378 /* insert values */
2379 if (!pat_ref_append(ref, key_beg, value_beg, line)) {
2380 memprintf(err, "out of memory");
2381 goto out_close;
2382 }
2383 }
2384
2385 /* succes */
2386 ret = 1;
2387
2388 out_close:
2389 fclose(file);
2390 return ret;
2391}
2392
2393/* Reads patterns from a file. If <err_msg> is non-NULL, an error message will
2394 * be returned there on errors and the caller will have to free it.
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002395 */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002396int pat_ref_read_from_file(struct pat_ref *ref, const char *filename, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002397{
2398 FILE *file;
2399 char *c;
2400 char *arg;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002401 int ret = 0;
2402 int line = 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002403
2404 file = fopen(filename, "r");
2405 if (!file) {
2406 memprintf(err, "failed to open pattern file <%s>", filename);
2407 return 0;
2408 }
2409
2410 /* now parse all patterns. The file may contain only one pattern per
2411 * line. If the line contains spaces, they will be part of the pattern.
2412 * The pattern stops at the first CR, LF or EOF encountered.
2413 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002414 while (fgets(trash.area, trash.size, file) != NULL) {
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002415 line++;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002416 c = trash.area;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002417
2418 /* ignore lines beginning with a dash */
2419 if (*c == '#')
2420 continue;
2421
2422 /* strip leading spaces and tabs */
2423 while (*c == ' ' || *c == '\t')
2424 c++;
2425
2426
2427 arg = c;
2428 while (*c && *c != '\n' && *c != '\r')
2429 c++;
2430 *c = 0;
2431
2432 /* empty lines are ignored too */
2433 if (c == arg)
2434 continue;
2435
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002436 if (!pat_ref_append(ref, arg, NULL, line)) {
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002437 memprintf(err, "out of memory when loading patterns from file <%s>", filename);
2438 goto out_close;
2439 }
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002440 }
2441
2442 ret = 1; /* success */
2443
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002444 out_close:
2445 fclose(file);
2446 return ret;
2447}
2448
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002449int pattern_read_from_file(struct pattern_head *head, unsigned int refflags,
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002450 const char *filename, int patflags, int load_smp,
Thierry FOURNIER94580c92014-02-11 14:36:45 +01002451 char **err, const char *file, int line)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002452{
2453 struct pat_ref *ref;
2454 struct pattern_expr *expr;
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002455 struct pat_ref_elt *elt;
Willy Tarreau4deaf392014-11-26 13:17:03 +01002456 int reuse = 0;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002457
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002458 /* Lookup for the existing reference. */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002459 ref = pat_ref_lookup(filename);
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002460
2461 /* If the reference doesn't exists, create it and load associated file. */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002462 if (!ref) {
Thierry FOURNIER94580c92014-02-11 14:36:45 +01002463 chunk_printf(&trash,
2464 "pattern loaded from file '%s' used by %s at file '%s' line %d",
2465 filename, refflags & PAT_REF_MAP ? "map" : "acl", file, line);
2466
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002467 ref = pat_ref_new(filename, trash.area, refflags);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002468 if (!ref) {
2469 memprintf(err, "out of memory");
2470 return 0;
2471 }
2472
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002473 if (load_smp) {
Thierry FOURNIERc0bd9102014-01-29 12:32:58 +01002474 ref->flags |= PAT_REF_SMP;
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002475 if (!pat_ref_read_from_file_smp(ref, filename, err))
2476 return 0;
2477 }
2478 else {
2479 if (!pat_ref_read_from_file(ref, filename, err))
2480 return 0;
2481 }
2482 }
2483 else {
Thierry FOURNIERc0bd9102014-01-29 12:32:58 +01002484 /* The reference already exists, check the map compatibility. */
2485
2486 /* If the load require samples and the flag PAT_REF_SMP is not set,
2487 * the reference doesn't contain sample, and cannot be used.
2488 */
2489 if (load_smp) {
2490 if (!(ref->flags & PAT_REF_SMP)) {
2491 memprintf(err, "The file \"%s\" is already used as one column file "
2492 "and cannot be used by as two column file.",
2493 filename);
2494 return 0;
2495 }
2496 }
2497 else {
2498 /* The load doesn't require samples. If the flag PAT_REF_SMP is
2499 * set, the reference contains a sample, and cannot be used.
2500 */
2501 if (ref->flags & PAT_REF_SMP) {
2502 memprintf(err, "The file \"%s\" is already used as two column file "
2503 "and cannot be used by as one column file.",
2504 filename);
2505 return 0;
2506 }
2507 }
2508
Thierry FOURNIER94580c92014-02-11 14:36:45 +01002509 /* Extends display */
2510 chunk_printf(&trash, "%s", ref->display);
2511 chunk_appendf(&trash, ", by %s at file '%s' line %d",
2512 refflags & PAT_REF_MAP ? "map" : "acl", file, line);
2513 free(ref->display);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002514 ref->display = strdup(trash.area);
Thierry FOURNIER94580c92014-02-11 14:36:45 +01002515 if (!ref->display) {
2516 memprintf(err, "out of memory");
2517 return 0;
2518 }
2519
Thierry FOURNIERc0bd9102014-01-29 12:32:58 +01002520 /* Merge flags. */
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002521 ref->flags |= refflags;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002522 }
2523
2524 /* Now, we can loading patterns from the reference. */
2525
2526 /* Lookup for existing reference in the head. If the reference
2527 * doesn't exists, create it.
2528 */
2529 expr = pattern_lookup_expr(head, ref);
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02002530 if (!expr || (expr->mflags != patflags)) {
Emeric Brun7d27f3c2017-07-03 17:54:23 +02002531 expr = pattern_new_expr(head, ref, patflags, err, &reuse);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002532 if (!expr)
2533 return 0;
2534 }
2535
Thierry FOURNIER315ec422014-11-24 11:14:42 +01002536 /* The returned expression may be not empty, because the function
2537 * "pattern_new_expr" lookup for similar pattern list and can
2538 * reuse a already filled pattern list. In this case, we can not
2539 * reload the patterns.
2540 */
2541 if (reuse)
2542 return 1;
2543
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002544 /* Load reference content in the pattern expression. */
2545 list_for_each_entry(elt, &ref->head, list) {
2546 if (!pat_ref_push(elt, expr, patflags, err)) {
2547 if (elt->line > 0)
2548 memprintf(err, "%s at line %d of file '%s'",
2549 *err, elt->line, filename);
2550 return 0;
2551 }
2552 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002553
2554 return 1;
2555}
2556
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01002557/* This function executes a pattern match on a sample. It applies pattern <expr>
2558 * to sample <smp>. The function returns NULL if the sample dont match. It returns
2559 * non-null if the sample match. If <fill> is true and the sample match, the
2560 * function returns the matched pattern. In many cases, this pattern can be a
2561 * static buffer.
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002562 */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002563struct pattern *pattern_exec_match(struct pattern_head *head, struct sample *smp, int fill)
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002564{
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002565 struct pattern_expr_list *list;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002566 struct pattern *pat;
2567
2568 if (!head->match) {
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01002569 if (fill) {
Thierry FOURNIER503bb092015-08-19 08:35:43 +02002570 static_pattern.data = NULL;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01002571 static_pattern.ref = NULL;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02002572 static_pattern.sflags = 0;
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002573 static_pattern.type = SMP_T_SINT;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01002574 static_pattern.val.i = 1;
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01002575 }
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01002576 return &static_pattern;
2577 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002578
Thierry FOURNIER5d344082014-01-27 14:19:53 +01002579 /* convert input to string */
2580 if (!sample_convert(smp, head->expect_type))
2581 return NULL;
2582
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002583 list_for_each_entry(list, &head->head, list) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002584 HA_RWLOCK_RDLOCK(PATEXP_LOCK, &list->expr->lock);
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002585 pat = head->match(smp, list->expr, fill);
Emeric Brunb5997f72017-07-03 11:34:05 +02002586 if (pat) {
2587 /* We duplicate the pattern cause it could be modified
2588 by another thread */
2589 if (pat != &static_pattern) {
2590 memcpy(&static_pattern, pat, sizeof(struct pattern));
2591 pat = &static_pattern;
2592 }
2593
2594 /* We also duplicate the sample data for
2595 same reason */
2596 if (pat->data && (pat->data != &static_sample_data)) {
Christopher Faulet09fdf4b2017-11-09 16:14:16 +01002597 switch(pat->data->type) {
Emeric Brunb5997f72017-07-03 11:34:05 +02002598 case SMP_T_STR:
2599 static_sample_data.type = SMP_T_STR;
2600 static_sample_data.u.str = *get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002601 static_sample_data.u.str.data = pat->data->u.str.data;
2602 if (static_sample_data.u.str.data >= static_sample_data.u.str.size)
2603 static_sample_data.u.str.data = static_sample_data.u.str.size - 1;
2604 memcpy(static_sample_data.u.str.area,
2605 pat->data->u.str.area,
2606 static_sample_data.u.str.data);
2607 static_sample_data.u.str.area[static_sample_data.u.str.data] = 0;
Emeric Brunb5997f72017-07-03 11:34:05 +02002608 case SMP_T_IPV4:
2609 case SMP_T_IPV6:
2610 case SMP_T_SINT:
2611 memcpy(&static_sample_data, pat->data, sizeof(struct sample_data));
2612 default:
2613 pat->data = NULL;
2614 }
2615 pat->data = &static_sample_data;
2616 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002617 HA_RWLOCK_RDUNLOCK(PATEXP_LOCK, &list->expr->lock);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002618 return pat;
Emeric Brunb5997f72017-07-03 11:34:05 +02002619 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002620 HA_RWLOCK_RDUNLOCK(PATEXP_LOCK, &list->expr->lock);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002621 }
2622 return NULL;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002623}
2624
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01002625/* This function prune the pattern expression. */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002626void pattern_prune(struct pattern_head *head)
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01002627{
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002628 struct pattern_expr_list *list, *safe;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002629
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002630 list_for_each_entry_safe(list, safe, &head->head, list) {
2631 LIST_DEL(&list->list);
2632 if (list->do_free) {
2633 LIST_DEL(&list->expr->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002634 HA_RWLOCK_WRLOCK(PATEXP_LOCK, &list->expr->lock);
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002635 head->prune(list->expr);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002636 HA_RWLOCK_WRUNLOCK(PATEXP_LOCK, &list->expr->lock);
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002637 free(list->expr);
2638 }
2639 free(list);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002640 }
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01002641}
2642
Thierry FOURNIER55d0b102014-01-15 11:25:26 +01002643/* This function lookup for a pattern matching the <key> and return a
2644 * pointer to a pointer of the sample stoarge. If the <key> dont match,
2645 * the function returns NULL. If the key cannot be parsed, the function
2646 * fill <err>.
2647 */
Thierry FOURNIER12ba0c22015-08-14 00:02:11 +02002648struct sample_data **pattern_find_smp(struct pattern_expr *expr, struct pat_ref_elt *ref)
Thierry FOURNIER55d0b102014-01-15 11:25:26 +01002649{
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01002650 struct ebmb_node *node;
2651 struct pattern_tree *elt;
2652 struct pattern_list *pat;
Thierry FOURNIER55d0b102014-01-15 11:25:26 +01002653
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01002654 for (node = ebmb_first(&expr->pattern_tree);
2655 node;
2656 node = ebmb_next(node)) {
2657 elt = container_of(node, struct pattern_tree, node);
2658 if (elt->ref == ref)
Thierry FOURNIER503bb092015-08-19 08:35:43 +02002659 return &elt->data;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01002660 }
2661
2662 for (node = ebmb_first(&expr->pattern_tree_2);
2663 node;
2664 node = ebmb_next(node)) {
2665 elt = container_of(node, struct pattern_tree, node);
2666 if (elt->ref == ref)
Thierry FOURNIER503bb092015-08-19 08:35:43 +02002667 return &elt->data;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01002668 }
2669
2670 list_for_each_entry(pat, &expr->patterns, list)
2671 if (pat->pat.ref == ref)
Thierry FOURNIER503bb092015-08-19 08:35:43 +02002672 return &pat->pat.data;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01002673
2674 return NULL;
Thierry FOURNIER55d0b102014-01-15 11:25:26 +01002675}
2676
Thierry FOURNIERb1136502014-01-15 11:38:49 +01002677/* This function search all the pattern matching the <key> and delete it.
2678 * If the parsing of the input key fails, the function returns 0 and the
2679 * <err> is filled, else return 1;
2680 */
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01002681int pattern_delete(struct pattern_expr *expr, struct pat_ref_elt *ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01002682{
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002683 HA_RWLOCK_WRLOCK(PATEXP_LOCK, &expr->lock);
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01002684 expr->pat_head->delete(expr, ref);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002685 HA_RWLOCK_WRUNLOCK(PATEXP_LOCK, &expr->lock);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01002686 return 1;
2687}
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002688
2689/* This function finalize the configuration parsing. Its set all the
2690 * automatic ids
2691 */
2692void pattern_finalize_config(void)
2693{
2694 int i = 0;
2695 struct pat_ref *ref, *ref2, *ref3;
2696 struct list pr = LIST_HEAD_INIT(pr);
2697
Willy Tarreauf3045d22015-04-29 16:24:50 +02002698 pat_lru_seed = random();
Emeric Brunb5997f72017-07-03 11:34:05 +02002699 if (global.tune.pattern_cache) {
Willy Tarreauf3045d22015-04-29 16:24:50 +02002700 pat_lru_tree = lru64_new(global.tune.pattern_cache);
Emeric Brunb5997f72017-07-03 11:34:05 +02002701 }
Willy Tarreauf3045d22015-04-29 16:24:50 +02002702
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002703 list_for_each_entry(ref, &pattern_reference, list) {
2704 if (ref->unique_id == -1) {
2705 /* Look for the first free id. */
2706 while (1) {
2707 list_for_each_entry(ref2, &pattern_reference, list) {
2708 if (ref2->unique_id == i) {
2709 i++;
2710 break;
2711 }
2712 }
Willy Tarreau3b786962014-04-26 12:37:25 +02002713 if (&ref2->list == &pattern_reference)
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002714 break;
2715 }
2716
2717 /* Uses the unique id and increment it for the next entry. */
2718 ref->unique_id = i;
2719 i++;
2720 }
2721 }
2722
2723 /* This sort the reference list by id. */
2724 list_for_each_entry_safe(ref, ref2, &pattern_reference, list) {
2725 LIST_DEL(&ref->list);
2726 list_for_each_entry(ref3, &pr, list) {
2727 if (ref->unique_id < ref3->unique_id) {
2728 LIST_ADDQ(&ref3->list, &ref->list);
2729 break;
2730 }
2731 }
2732 if (&ref3->list == &pr)
2733 LIST_ADDQ(&pr, &ref->list);
2734 }
2735
2736 /* swap root */
2737 LIST_ADD(&pr, &pattern_reference);
2738 LIST_DEL(&pr);
2739}