blob: ec7e9556ae93b01cab460e3c9bc7ba2555bc6c4c [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 Tarreau403bfbb2019-10-23 06:59:31 +0200157static THREAD_LOCAL struct lru64_head *pat_lru_tree;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200158static unsigned long long pat_lru_seed;
159
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100160/*
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100161 *
162 * The following functions are not exported and are used by internals process
163 * of pattern matching
164 *
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100165 */
166
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100167/* Background: Fast way to find a zero byte in a word
168 * http://graphics.stanford.edu/~seander/bithacks.html#ZeroInWord
169 * hasZeroByte = (v - 0x01010101UL) & ~v & 0x80808080UL;
170 *
171 * To look for 4 different byte values, xor the word with those bytes and
172 * then check for zero bytes:
173 *
174 * v = (((unsigned char)c * 0x1010101U) ^ delimiter)
175 * where <delimiter> is the 4 byte values to look for (as an uint)
176 * and <c> is the character that is being tested
177 */
178static inline unsigned int is_delimiter(unsigned char c, unsigned int mask)
179{
180 mask ^= (c * 0x01010101); /* propagate the char to all 4 bytes */
181 return (mask - 0x01010101) & ~mask & 0x80808080U;
182}
183
184static inline unsigned int make_4delim(unsigned char d1, unsigned char d2, unsigned char d3, unsigned char d4)
185{
186 return d1 << 24 | d2 << 16 | d3 << 8 | d4;
187}
188
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100189
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100190/*
191 *
192 * These functions are exported and may be used by any other component.
193 *
Willy Tarreau5def8ef2014-08-29 15:19:33 +0200194 * The following functions are used for parsing pattern matching input value.
195 * The <text> contain the string to be parsed. <pattern> must be a preallocated
196 * pattern. The pat_parse_* functions fill this structure with the parsed value.
197 * <err> is filled with an error message built with memprintf() function. It is
198 * allowed to use a trash as a temporary storage for the returned pattern, as
199 * the next call after these functions will be pat_idx_*.
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100200 *
Willy Tarreau5def8ef2014-08-29 15:19:33 +0200201 * In success case, the pat_parse_* function returns 1. If the function
202 * fails, it returns 0 and <err> is filled.
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100203 */
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100204
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100205/* ignore the current line */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200206int pat_parse_nothing(const char *text, struct pattern *pattern, int mflags, char **err)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100207{
208 return 1;
209}
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100210
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100211/* Parse a string. It is allocated and duplicated. */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200212int pat_parse_str(const char *text, struct pattern *pattern, int mflags, char **err)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100213{
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100214 pattern->type = SMP_T_STR;
Thierry FOURNIERedc15c32013-12-13 15:36:59 +0100215 pattern->ptr.str = (char *)text;
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100216 pattern->len = strlen(text);
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100217 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100218}
219
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100220/* Parse a binary written in hexa. It is allocated. */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200221int pat_parse_bin(const char *text, struct pattern *pattern, int mflags, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100222{
Willy Tarreau83061a82018-07-13 11:56:34 +0200223 struct buffer *trash;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100224
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100225 pattern->type = SMP_T_BIN;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100226 trash = get_trash_chunk();
227 pattern->len = trash->size;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200228 pattern->ptr.str = trash->area;
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100229 return !!parse_binary(text, &pattern->ptr.str, &pattern->len, err);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100230}
231
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100232/* Parse a regex. It is allocated. */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200233int pat_parse_reg(const char *text, struct pattern *pattern, int mflags, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100234{
Thierry FOURNIER0b6d15f2014-01-29 19:35:16 +0100235 pattern->ptr.str = (char *)text;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100236 return 1;
237}
238
239/* Parse a range of positive integers delimited by either ':' or '-'. If only
240 * one integer is read, it is set as both min and max. An operator may be
241 * specified as the prefix, among this list of 5 :
242 *
243 * 0:eq, 1:gt, 2:ge, 3:lt, 4:le
244 *
245 * The default operator is "eq". It supports range matching. Ranges are
246 * rejected for other operators. The operator may be changed at any time.
247 * The operator is stored in the 'opaque' argument.
248 *
249 * If err is non-NULL, an error message will be returned there on errors and
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100250 * the caller will have to free it. The function returns zero on error, and
251 * non-zero on success.
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100252 *
253 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200254int pat_parse_int(const char *text, struct pattern *pattern, int mflags, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100255{
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100256 const char *ptr = text;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100257
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200258 pattern->type = SMP_T_SINT;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100259
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100260 /* Empty string is not valid */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100261 if (!*text)
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100262 goto not_valid_range;
263
264 /* Search ':' or '-' separator. */
265 while (*ptr != '\0' && *ptr != ':' && *ptr != '-')
266 ptr++;
267
268 /* If separator not found. */
269 if (!*ptr) {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100270 if (strl2llrc(text, ptr - text, &pattern->val.range.min) != 0) {
271 memprintf(err, "'%s' is not a number", text);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100272 return 0;
273 }
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100274 pattern->val.range.max = pattern->val.range.min;
275 pattern->val.range.min_set = 1;
276 pattern->val.range.max_set = 1;
277 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100278 }
279
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100280 /* If the separator is the first character. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100281 if (ptr == text && *(ptr + 1) != '\0') {
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100282 if (strl2llrc(ptr + 1, strlen(ptr + 1), &pattern->val.range.max) != 0)
283 goto not_valid_range;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100284
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100285 pattern->val.range.min_set = 0;
286 pattern->val.range.max_set = 1;
287 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100288 }
289
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100290 /* If separator is the last character. */
291 if (*(ptr + 1) == '\0') {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100292 if (strl2llrc(text, ptr - text, &pattern->val.range.min) != 0)
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100293 goto not_valid_range;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100294
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100295 pattern->val.range.min_set = 1;
296 pattern->val.range.max_set = 0;
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100297 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100298 }
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100299
300 /* Else, parse two numbers. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100301 if (strl2llrc(text, ptr - text, &pattern->val.range.min) != 0)
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100302 goto not_valid_range;
303
304 if (strl2llrc(ptr + 1, strlen(ptr + 1), &pattern->val.range.max) != 0)
305 goto not_valid_range;
306
307 if (pattern->val.range.min > pattern->val.range.max)
308 goto not_valid_range;
309
310 pattern->val.range.min_set = 1;
311 pattern->val.range.max_set = 1;
312 return 1;
313
314 not_valid_range:
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100315 memprintf(err, "'%s' is not a valid number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100316 return 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100317}
318
319/* Parse a range of positive 2-component versions delimited by either ':' or
320 * '-'. The version consists in a major and a minor, both of which must be
321 * smaller than 65536, because internally they will be represented as a 32-bit
322 * integer.
323 * If only one version is read, it is set as both min and max. Just like for
324 * pure integers, an operator may be specified as the prefix, among this list
325 * of 5 :
326 *
327 * 0:eq, 1:gt, 2:ge, 3:lt, 4:le
328 *
329 * The default operator is "eq". It supports range matching. Ranges are
330 * rejected for other operators. The operator may be changed at any time.
331 * The operator is stored in the 'opaque' argument. This allows constructs
332 * such as the following one :
333 *
334 * acl obsolete_ssl ssl_req_proto lt 3
335 * acl unsupported_ssl ssl_req_proto gt 3.1
336 * acl valid_ssl ssl_req_proto 3.0-3.1
337 *
338 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200339int pat_parse_dotted_ver(const char *text, struct pattern *pattern, int mflags, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100340{
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100341 const char *ptr = text;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100342
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200343 pattern->type = SMP_T_SINT;
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100344
345 /* Search ':' or '-' separator. */
346 while (*ptr != '\0' && *ptr != ':' && *ptr != '-')
347 ptr++;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100348
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100349 /* If separator not found. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100350 if (*ptr == '\0' && ptr > text) {
351 if (strl2llrc_dotted(text, ptr-text, &pattern->val.range.min) != 0) {
352 memprintf(err, "'%s' is not a dotted number", text);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100353 return 0;
354 }
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100355 pattern->val.range.max = pattern->val.range.min;
356 pattern->val.range.min_set = 1;
357 pattern->val.range.max_set = 1;
358 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100359 }
360
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100361 /* If the separator is the first character. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100362 if (ptr == text && *(ptr+1) != '\0') {
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100363 if (strl2llrc_dotted(ptr+1, strlen(ptr+1), &pattern->val.range.max) != 0) {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100364 memprintf(err, "'%s' is not a valid dotted number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100365 return 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100366 }
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100367 pattern->val.range.min_set = 0;
368 pattern->val.range.max_set = 1;
369 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100370 }
371
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100372 /* If separator is the last character. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100373 if (ptr == &text[strlen(text)-1]) {
374 if (strl2llrc_dotted(text, ptr-text, &pattern->val.range.min) != 0) {
375 memprintf(err, "'%s' is not a valid dotted number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100376 return 0;
377 }
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100378 pattern->val.range.min_set = 1;
379 pattern->val.range.max_set = 0;
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100380 return 1;
381 }
382
383 /* Else, parse two numbers. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100384 if (strl2llrc_dotted(text, ptr-text, &pattern->val.range.min) != 0) {
385 memprintf(err, "'%s' is not a valid dotted number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100386 return 0;
387 }
388 if (strl2llrc_dotted(ptr+1, strlen(ptr+1), &pattern->val.range.max) != 0) {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100389 memprintf(err, "'%s' is not a valid dotted number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100390 return 0;
391 }
392 if (pattern->val.range.min > pattern->val.range.max) {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100393 memprintf(err, "'%s' is not a valid dotted number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100394 return 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100395 }
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100396 pattern->val.range.min_set = 1;
397 pattern->val.range.max_set = 1;
398 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100399}
400
401/* Parse an IP address and an optional mask in the form addr[/mask].
402 * The addr may either be an IPv4 address or a hostname. The mask
403 * may either be a dotted mask or a number of bits. Returns 1 if OK,
404 * otherwise 0. NOTE: IP address patterns are typed (IPV4/IPV6).
405 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200406int pat_parse_ip(const char *text, struct pattern *pattern, int mflags, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100407{
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200408 if (str2net(text, !(mflags & PAT_MF_NO_DNS) && (global.mode & MODE_STARTING),
Thierry FOURNIERfc7ac7b2014-02-11 15:23:04 +0100409 &pattern->val.ipv4.addr, &pattern->val.ipv4.mask)) {
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100410 pattern->type = SMP_T_IPV4;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100411 return 1;
412 }
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100413 else if (str62net(text, &pattern->val.ipv6.addr, &pattern->val.ipv6.mask)) {
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100414 pattern->type = SMP_T_IPV6;
415 return 1;
416 }
417 else {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100418 memprintf(err, "'%s' is not a valid IPv4 or IPv6 address", text);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100419 return 0;
420 }
421}
422
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100423/*
424 *
425 * These functions are exported and may be used by any other component.
426 *
Joseph Herlant4189d672018-11-15 10:22:31 -0800427 * This function just takes a sample <smp> and checks if this sample matches
428 * with the pattern <pattern>. This function returns only PAT_MATCH or
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100429 * PAT_NOMATCH.
430 *
431 */
432
433/* always return false */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100434struct pattern *pat_match_nothing(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100435{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200436 if (smp->data.u.sint) {
Thierry FOURNIERe5978bf2014-03-17 19:53:10 +0100437 if (fill) {
Thierry FOURNIER503bb092015-08-19 08:35:43 +0200438 static_pattern.data = NULL;
Thierry FOURNIERe5978bf2014-03-17 19:53:10 +0100439 static_pattern.ref = NULL;
Thierry FOURNIERe5978bf2014-03-17 19:53:10 +0100440 static_pattern.type = 0;
441 static_pattern.ptr.str = NULL;
442 }
443 return &static_pattern;
444 }
445 else
446 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100447}
448
449
Joseph Herlant4189d672018-11-15 10:22:31 -0800450/* NB: For two strings to be identical, it is required that their length match */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100451struct pattern *pat_match_str(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100452{
453 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100454 struct ebmb_node *node;
455 char prev;
456 struct pattern_tree *elt;
457 struct pattern_list *lst;
458 struct pattern *pattern;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200459 struct pattern *ret = NULL;
460 struct lru64 *lru = NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100461
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100462 /* Lookup a string in the expression's pattern tree. */
463 if (!eb_is_empty(&expr->pattern_tree)) {
464 /* we may have to force a trailing zero on the test pattern */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200465 prev = smp->data.u.str.area[smp->data.u.str.data];
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100466 if (prev)
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200467 smp->data.u.str.area[smp->data.u.str.data] = '\0';
468 node = ebst_lookup(&expr->pattern_tree, smp->data.u.str.area);
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100469 if (prev)
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200470 smp->data.u.str.area[smp->data.u.str.data] = prev;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100471
472 if (node) {
473 if (fill) {
474 elt = ebmb_entry(node, struct pattern_tree, node);
Thierry FOURNIER503bb092015-08-19 08:35:43 +0200475 static_pattern.data = elt->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +0100476 static_pattern.ref = elt->ref;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200477 static_pattern.sflags = PAT_SF_TREE;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100478 static_pattern.type = SMP_T_STR;
479 static_pattern.ptr.str = (char *)elt->node.key;
480 }
481 return &static_pattern;
482 }
483 }
484
485 /* look in the list */
Willy Tarreauf3045d22015-04-29 16:24:50 +0200486 if (pat_lru_tree) {
Willy Tarreauaee93142015-05-04 17:18:42 +0200487 unsigned long long seed = pat_lru_seed ^ (long)expr;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200488
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200489 lru = lru64_get(XXH64(smp->data.u.str.area, smp->data.u.str.data, seed),
Willy Tarreauf3045d22015-04-29 16:24:50 +0200490 pat_lru_tree, expr, expr->revision);
Willy Tarreau403bfbb2019-10-23 06:59:31 +0200491 if (lru && lru->domain) {
Emeric Brunb5997f72017-07-03 11:34:05 +0200492 ret = lru->data;
Emeric Brunb5997f72017-07-03 11:34:05 +0200493 return ret;
494 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200495 }
496
Emeric Brunb5997f72017-07-03 11:34:05 +0200497
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100498 list_for_each_entry(lst, &expr->patterns, list) {
499 pattern = &lst->pat;
500
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200501 if (pattern->len != smp->data.u.str.data)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100502 continue;
503
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200504 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200505 if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.str.area, smp->data.u.str.data) == 0) ||
506 (!icase && strncmp(pattern->ptr.str, smp->data.u.str.area, smp->data.u.str.data) == 0)) {
Willy Tarreauf3045d22015-04-29 16:24:50 +0200507 ret = pattern;
508 break;
509 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100510 }
511
Willy Tarreau403bfbb2019-10-23 06:59:31 +0200512 if (lru)
Emeric Brunb5997f72017-07-03 11:34:05 +0200513 lru64_commit(lru, ret, expr, expr->revision, NULL);
Willy Tarreauf3045d22015-04-29 16:24:50 +0200514
515 return ret;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100516}
517
518/* NB: For two binaries buf to be identical, it is required that their lengths match */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100519struct pattern *pat_match_bin(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100520{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100521 struct pattern_list *lst;
522 struct pattern *pattern;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200523 struct pattern *ret = NULL;
524 struct lru64 *lru = NULL;
525
526 if (pat_lru_tree) {
Willy Tarreauaee93142015-05-04 17:18:42 +0200527 unsigned long long seed = pat_lru_seed ^ (long)expr;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100528
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200529 lru = lru64_get(XXH64(smp->data.u.str.area, smp->data.u.str.data, seed),
Willy Tarreauf3045d22015-04-29 16:24:50 +0200530 pat_lru_tree, expr, expr->revision);
Willy Tarreau403bfbb2019-10-23 06:59:31 +0200531 if (lru && lru->domain) {
Emeric Brunb5997f72017-07-03 11:34:05 +0200532 ret = lru->data;
Emeric Brunb5997f72017-07-03 11:34:05 +0200533 return ret;
534 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200535 }
536
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100537 list_for_each_entry(lst, &expr->patterns, list) {
538 pattern = &lst->pat;
539
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200540 if (pattern->len != smp->data.u.str.data)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100541 continue;
542
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200543 if (memcmp(pattern->ptr.str, smp->data.u.str.area, smp->data.u.str.data) == 0) {
Willy Tarreauf3045d22015-04-29 16:24:50 +0200544 ret = pattern;
545 break;
546 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100547 }
548
Willy Tarreau403bfbb2019-10-23 06:59:31 +0200549 if (lru)
Emeric Brunb5997f72017-07-03 11:34:05 +0200550 lru64_commit(lru, ret, expr, expr->revision, NULL);
Willy Tarreauf3045d22015-04-29 16:24:50 +0200551
552 return ret;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100553}
554
555/* Executes a regex. It temporarily changes the data to add a trailing zero,
Thierry Fournier8feaa662016-02-10 22:55:20 +0100556 * and restores the previous character when leaving. This function fills
557 * a matching array.
558 */
559struct pattern *pat_match_regm(struct sample *smp, struct pattern_expr *expr, int fill)
560{
561 struct pattern_list *lst;
562 struct pattern *pattern;
563 struct pattern *ret = NULL;
564
565 list_for_each_entry(lst, &expr->patterns, list) {
566 pattern = &lst->pat;
567
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200568 if (regex_exec_match2(pattern->ptr.reg, smp->data.u.str.area, smp->data.u.str.data,
Thierry Fournier8feaa662016-02-10 22:55:20 +0100569 MAX_MATCH, pmatch, 0)) {
570 ret = pattern;
571 smp->ctx.a[0] = pmatch;
572 break;
573 }
574 }
575
576 return ret;
577}
578
579/* Executes a regex. It temporarily changes the data to add a trailing zero,
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100580 * and restores the previous character when leaving.
581 */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100582struct pattern *pat_match_reg(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100583{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100584 struct pattern_list *lst;
585 struct pattern *pattern;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200586 struct pattern *ret = NULL;
587 struct lru64 *lru = NULL;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100588
Willy Tarreauf3045d22015-04-29 16:24:50 +0200589 if (pat_lru_tree) {
Willy Tarreauaee93142015-05-04 17:18:42 +0200590 unsigned long long seed = pat_lru_seed ^ (long)expr;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200591
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200592 lru = lru64_get(XXH64(smp->data.u.str.area, smp->data.u.str.data, seed),
Willy Tarreauf3045d22015-04-29 16:24:50 +0200593 pat_lru_tree, expr, expr->revision);
Willy Tarreau403bfbb2019-10-23 06:59:31 +0200594 if (lru && lru->domain) {
Emeric Brunb5997f72017-07-03 11:34:05 +0200595 ret = lru->data;
Emeric Brunb5997f72017-07-03 11:34:05 +0200596 return ret;
597 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200598 }
599
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100600 list_for_each_entry(lst, &expr->patterns, list) {
601 pattern = &lst->pat;
602
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200603 if (regex_exec2(pattern->ptr.reg, smp->data.u.str.area, smp->data.u.str.data)) {
Willy Tarreauf3045d22015-04-29 16:24:50 +0200604 ret = pattern;
605 break;
606 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100607 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200608
Willy Tarreau403bfbb2019-10-23 06:59:31 +0200609 if (lru)
Emeric Brunb5997f72017-07-03 11:34:05 +0200610 lru64_commit(lru, ret, expr, expr->revision, NULL);
Willy Tarreauf3045d22015-04-29 16:24:50 +0200611
612 return ret;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100613}
614
615/* Checks that the pattern matches the beginning of the tested string. */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100616struct pattern *pat_match_beg(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100617{
618 int icase;
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +0200619 struct ebmb_node *node;
620 char prev;
621 struct pattern_tree *elt;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100622 struct pattern_list *lst;
623 struct pattern *pattern;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200624 struct pattern *ret = NULL;
625 struct lru64 *lru = NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100626
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +0200627 /* Lookup a string in the expression's pattern tree. */
628 if (!eb_is_empty(&expr->pattern_tree)) {
629 /* we may have to force a trailing zero on the test pattern */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200630 prev = smp->data.u.str.area[smp->data.u.str.data];
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +0200631 if (prev)
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200632 smp->data.u.str.area[smp->data.u.str.data] = '\0';
633 node = ebmb_lookup_longest(&expr->pattern_tree,
634 smp->data.u.str.area);
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +0200635 if (prev)
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200636 smp->data.u.str.area[smp->data.u.str.data] = prev;
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +0200637
638 if (node) {
639 if (fill) {
640 elt = ebmb_entry(node, struct pattern_tree, node);
Thierry FOURNIER503bb092015-08-19 08:35:43 +0200641 static_pattern.data = elt->data;
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +0200642 static_pattern.ref = elt->ref;
643 static_pattern.sflags = PAT_SF_TREE;
644 static_pattern.type = SMP_T_STR;
645 static_pattern.ptr.str = (char *)elt->node.key;
646 }
647 return &static_pattern;
648 }
649 }
650
651 /* look in the list */
Willy Tarreauf3045d22015-04-29 16:24:50 +0200652 if (pat_lru_tree) {
Willy Tarreauaee93142015-05-04 17:18:42 +0200653 unsigned long long seed = pat_lru_seed ^ (long)expr;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200654
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200655 lru = lru64_get(XXH64(smp->data.u.str.area, smp->data.u.str.data, seed),
Willy Tarreauf3045d22015-04-29 16:24:50 +0200656 pat_lru_tree, expr, expr->revision);
Willy Tarreau403bfbb2019-10-23 06:59:31 +0200657 if (lru && lru->domain) {
Emeric Brunb5997f72017-07-03 11:34:05 +0200658 ret = lru->data;
Emeric Brunb5997f72017-07-03 11:34:05 +0200659 return ret;
660 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200661 }
662
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100663 list_for_each_entry(lst, &expr->patterns, list) {
664 pattern = &lst->pat;
665
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200666 if (pattern->len > smp->data.u.str.data)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100667 continue;
668
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200669 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200670 if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.str.area, pattern->len) != 0) ||
671 (!icase && strncmp(pattern->ptr.str, smp->data.u.str.area, pattern->len) != 0))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100672 continue;
673
Willy Tarreauf3045d22015-04-29 16:24:50 +0200674 ret = pattern;
675 break;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100676 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200677
Willy Tarreau403bfbb2019-10-23 06:59:31 +0200678 if (lru)
Emeric Brunb5997f72017-07-03 11:34:05 +0200679 lru64_commit(lru, ret, expr, expr->revision, NULL);
Willy Tarreauf3045d22015-04-29 16:24:50 +0200680
681 return ret;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100682}
683
684/* Checks that the pattern matches the end of the tested string. */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100685struct pattern *pat_match_end(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100686{
687 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100688 struct pattern_list *lst;
689 struct pattern *pattern;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200690 struct pattern *ret = NULL;
691 struct lru64 *lru = NULL;
692
693 if (pat_lru_tree) {
Willy Tarreauaee93142015-05-04 17:18:42 +0200694 unsigned long long seed = pat_lru_seed ^ (long)expr;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200695
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200696 lru = lru64_get(XXH64(smp->data.u.str.area, smp->data.u.str.data, seed),
Willy Tarreauf3045d22015-04-29 16:24:50 +0200697 pat_lru_tree, expr, expr->revision);
Willy Tarreau403bfbb2019-10-23 06:59:31 +0200698 if (lru && lru->domain) {
Emeric Brunb5997f72017-07-03 11:34:05 +0200699 ret = lru->data;
Emeric Brunb5997f72017-07-03 11:34:05 +0200700 return ret;
701 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200702 }
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100703
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100704 list_for_each_entry(lst, &expr->patterns, list) {
705 pattern = &lst->pat;
706
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200707 if (pattern->len > smp->data.u.str.data)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100708 continue;
709
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200710 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200711 if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.str.area + smp->data.u.str.data - pattern->len, pattern->len) != 0) ||
712 (!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 +0100713 continue;
714
Willy Tarreauf3045d22015-04-29 16:24:50 +0200715 ret = pattern;
716 break;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100717 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200718
Willy Tarreau403bfbb2019-10-23 06:59:31 +0200719 if (lru)
Emeric Brunb5997f72017-07-03 11:34:05 +0200720 lru64_commit(lru, ret, expr, expr->revision, NULL);
Willy Tarreauf3045d22015-04-29 16:24:50 +0200721
722 return ret;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100723}
724
725/* Checks that the pattern is included inside the tested string.
726 * NB: Suboptimal, should be rewritten using a Boyer-Moore method.
727 */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100728struct pattern *pat_match_sub(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100729{
730 int icase;
731 char *end;
732 char *c;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100733 struct pattern_list *lst;
734 struct pattern *pattern;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200735 struct pattern *ret = NULL;
736 struct lru64 *lru = NULL;
737
738 if (pat_lru_tree) {
Willy Tarreauaee93142015-05-04 17:18:42 +0200739 unsigned long long seed = pat_lru_seed ^ (long)expr;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200740
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200741 lru = lru64_get(XXH64(smp->data.u.str.area, smp->data.u.str.data, seed),
Willy Tarreauf3045d22015-04-29 16:24:50 +0200742 pat_lru_tree, expr, expr->revision);
Willy Tarreau403bfbb2019-10-23 06:59:31 +0200743 if (lru && lru->domain) {
Emeric Brunb5997f72017-07-03 11:34:05 +0200744 ret = lru->data;
Emeric Brunb5997f72017-07-03 11:34:05 +0200745 return ret;
746 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200747 }
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100748
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100749 list_for_each_entry(lst, &expr->patterns, list) {
750 pattern = &lst->pat;
751
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200752 if (pattern->len > smp->data.u.str.data)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100753 continue;
754
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200755 end = smp->data.u.str.area + smp->data.u.str.data - pattern->len;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200756 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100757 if (icase) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200758 for (c = smp->data.u.str.area; c <= end; c++) {
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100759 if (tolower(*c) != tolower(*pattern->ptr.str))
760 continue;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200761 if (strncasecmp(pattern->ptr.str, c, pattern->len) == 0) {
762 ret = pattern;
763 goto leave;
764 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100765 }
766 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200767 for (c = smp->data.u.str.area; c <= end; c++) {
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100768 if (*c != *pattern->ptr.str)
769 continue;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200770 if (strncmp(pattern->ptr.str, c, pattern->len) == 0) {
771 ret = pattern;
772 goto leave;
773 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100774 }
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100775 }
776 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200777 leave:
Willy Tarreau403bfbb2019-10-23 06:59:31 +0200778 if (lru)
Emeric Brunb5997f72017-07-03 11:34:05 +0200779 lru64_commit(lru, ret, expr, expr->revision, NULL);
Willy Tarreauf3045d22015-04-29 16:24:50 +0200780
781 return ret;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100782}
783
784/* This one is used by other real functions. It checks that the pattern is
785 * included inside the tested string, but enclosed between the specified
786 * delimiters or at the beginning or end of the string. The delimiters are
787 * provided as an unsigned int made by make_4delim() and match up to 4 different
788 * delimiters. Delimiters are stripped at the beginning and end of the pattern.
789 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200790static int match_word(struct sample *smp, struct pattern *pattern, int mflags, unsigned int delimiters)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100791{
792 int may_match, icase;
793 char *c, *end;
794 char *ps;
795 int pl;
796
797 pl = pattern->len;
798 ps = pattern->ptr.str;
799
800 while (pl > 0 && is_delimiter(*ps, delimiters)) {
801 pl--;
802 ps++;
803 }
804
805 while (pl > 0 && is_delimiter(ps[pl - 1], delimiters))
806 pl--;
807
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200808 if (pl > smp->data.u.str.data)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100809 return PAT_NOMATCH;
810
811 may_match = 1;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200812 icase = mflags & PAT_MF_IGNORE_CASE;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200813 end = smp->data.u.str.area + smp->data.u.str.data - pl;
814 for (c = smp->data.u.str.area; c <= end; c++) {
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100815 if (is_delimiter(*c, delimiters)) {
816 may_match = 1;
817 continue;
818 }
819
820 if (!may_match)
821 continue;
822
823 if (icase) {
824 if ((tolower(*c) == tolower(*ps)) &&
825 (strncasecmp(ps, c, pl) == 0) &&
826 (c == end || is_delimiter(c[pl], delimiters)))
827 return PAT_MATCH;
828 } else {
829 if ((*c == *ps) &&
830 (strncmp(ps, c, pl) == 0) &&
831 (c == end || is_delimiter(c[pl], delimiters)))
832 return PAT_MATCH;
833 }
834 may_match = 0;
835 }
836 return PAT_NOMATCH;
837}
838
839/* Checks that the pattern is included inside the tested string, but enclosed
840 * between the delimiters '?' or '/' or at the beginning or end of the string.
841 * Delimiters at the beginning or end of the pattern are ignored.
842 */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100843struct pattern *pat_match_dir(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100844{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100845 struct pattern_list *lst;
846 struct pattern *pattern;
847
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100848 list_for_each_entry(lst, &expr->patterns, list) {
849 pattern = &lst->pat;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200850 if (match_word(smp, pattern, expr->mflags, make_4delim('/', '?', '?', '?')))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100851 return pattern;
852 }
853 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100854}
855
856/* Checks that the pattern is included inside the tested string, but enclosed
857 * between the delmiters '/', '?', '.' or ":" or at the beginning or end of
858 * the string. Delimiters at the beginning or end of the pattern are ignored.
859 */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100860struct pattern *pat_match_dom(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100861{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100862 struct pattern_list *lst;
863 struct pattern *pattern;
864
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100865 list_for_each_entry(lst, &expr->patterns, list) {
866 pattern = &lst->pat;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200867 if (match_word(smp, pattern, expr->mflags, make_4delim('/', '?', '.', ':')))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100868 return pattern;
869 }
870 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100871}
872
873/* Checks that the integer in <test> is included between min and max */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100874struct pattern *pat_match_int(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100875{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100876 struct pattern_list *lst;
877 struct pattern *pattern;
878
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100879 list_for_each_entry(lst, &expr->patterns, list) {
880 pattern = &lst->pat;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200881 if ((!pattern->val.range.min_set || pattern->val.range.min <= smp->data.u.sint) &&
882 (!pattern->val.range.max_set || smp->data.u.sint <= pattern->val.range.max))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100883 return pattern;
884 }
885 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100886}
887
888/* Checks that the length of the pattern in <test> is included between min and max */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100889struct pattern *pat_match_len(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100890{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100891 struct pattern_list *lst;
892 struct pattern *pattern;
893
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100894 list_for_each_entry(lst, &expr->patterns, list) {
895 pattern = &lst->pat;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200896 if ((!pattern->val.range.min_set || pattern->val.range.min <= smp->data.u.str.data) &&
897 (!pattern->val.range.max_set || smp->data.u.str.data <= pattern->val.range.max))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100898 return pattern;
899 }
900 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100901}
902
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100903struct pattern *pat_match_ip(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100904{
905 unsigned int v4; /* in network byte order */
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100906 struct in6_addr tmp6;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100907 struct in_addr *s;
908 struct ebmb_node *node;
909 struct pattern_tree *elt;
910 struct pattern_list *lst;
911 struct pattern *pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100912
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100913 /* The input sample is IPv4. Try to match in the trees. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200914 if (smp->data.type == SMP_T_IPV4) {
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100915 /* Lookup an IPv4 address in the expression's pattern tree using
916 * the longest match method.
917 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200918 s = &smp->data.u.ipv4;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100919 node = ebmb_lookup_longest(&expr->pattern_tree, &s->s_addr);
920 if (node) {
921 if (fill) {
922 elt = ebmb_entry(node, struct pattern_tree, node);
Thierry FOURNIER503bb092015-08-19 08:35:43 +0200923 static_pattern.data = elt->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +0100924 static_pattern.ref = elt->ref;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200925 static_pattern.sflags = PAT_SF_TREE;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100926 static_pattern.type = SMP_T_IPV4;
Willy Tarreau33ccf1c2019-06-16 18:40:33 +0200927 static_pattern.val.ipv4.addr = *(struct in_addr *)elt->node.key;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100928 if (!cidr2dotted(elt->node.node.pfx, &static_pattern.val.ipv4.mask))
929 return NULL;
930 }
931 return &static_pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100932 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100933
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100934 /* The IPv4 sample dont match the IPv4 tree. Convert the IPv4
935 * sample address to IPv6 with the mapping method using the ::ffff:
936 * prefix, and try to lookup in the IPv6 tree.
937 */
938 memset(&tmp6, 0, 10);
939 *(uint16_t*)&tmp6.s6_addr[10] = htons(0xffff);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200940 *(uint32_t*)&tmp6.s6_addr[12] = smp->data.u.ipv4.s_addr;
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100941 node = ebmb_lookup_longest(&expr->pattern_tree_2, &tmp6);
942 if (node) {
943 if (fill) {
944 elt = ebmb_entry(node, struct pattern_tree, node);
Thierry FOURNIER503bb092015-08-19 08:35:43 +0200945 static_pattern.data = elt->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +0100946 static_pattern.ref = elt->ref;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200947 static_pattern.sflags = PAT_SF_TREE;
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100948 static_pattern.type = SMP_T_IPV6;
Willy Tarreau33ccf1c2019-06-16 18:40:33 +0200949 static_pattern.val.ipv6.addr = *(struct in6_addr *)elt->node.key;
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100950 static_pattern.val.ipv6.mask = elt->node.node.pfx;
951 }
952 return &static_pattern;
953 }
954 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100955
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100956 /* The input sample is IPv6. Try to match in the trees. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200957 if (smp->data.type == SMP_T_IPV6) {
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100958 /* Lookup an IPv6 address in the expression's pattern tree using
959 * the longest match method.
960 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200961 node = ebmb_lookup_longest(&expr->pattern_tree_2, &smp->data.u.ipv6);
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100962 if (node) {
963 if (fill) {
964 elt = ebmb_entry(node, struct pattern_tree, node);
Thierry FOURNIER503bb092015-08-19 08:35:43 +0200965 static_pattern.data = elt->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +0100966 static_pattern.ref = elt->ref;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200967 static_pattern.sflags = PAT_SF_TREE;
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100968 static_pattern.type = SMP_T_IPV6;
Willy Tarreau33ccf1c2019-06-16 18:40:33 +0200969 static_pattern.val.ipv6.addr = *(struct in6_addr *)elt->node.key;
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100970 static_pattern.val.ipv6.mask = elt->node.node.pfx;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100971 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100972 return &static_pattern;
973 }
974
975 /* Try to convert 6 to 4 when the start of the ipv6 address match the
976 * following forms :
977 * - ::ffff:ip:v4 (ipv4 mapped)
978 * - ::0000:ip:v4 (old ipv4 mapped)
979 * - 2002:ip:v4:: (6to4)
980 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200981 if ((*(uint32_t*)&smp->data.u.ipv6.s6_addr[0] == 0 &&
982 *(uint32_t*)&smp->data.u.ipv6.s6_addr[4] == 0 &&
983 (*(uint32_t*)&smp->data.u.ipv6.s6_addr[8] == 0 ||
984 *(uint32_t*)&smp->data.u.ipv6.s6_addr[8] == htonl(0xFFFF))) ||
985 *(uint16_t*)&smp->data.u.ipv6.s6_addr[0] == htons(0x2002)) {
986 if (*(uint32_t*)&smp->data.u.ipv6.s6_addr[0] == 0)
987 v4 = *(uint32_t*)&smp->data.u.ipv6.s6_addr[12];
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100988 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200989 v4 = htonl((ntohs(*(uint16_t*)&smp->data.u.ipv6.s6_addr[2]) << 16) +
990 ntohs(*(uint16_t*)&smp->data.u.ipv6.s6_addr[4]));
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100991
992 /* Lookup an IPv4 address in the expression's pattern tree using the longest
993 * match method.
994 */
995 node = ebmb_lookup_longest(&expr->pattern_tree, &v4);
996 if (node) {
997 if (fill) {
998 elt = ebmb_entry(node, struct pattern_tree, node);
Thierry FOURNIER503bb092015-08-19 08:35:43 +0200999 static_pattern.data = elt->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01001000 static_pattern.ref = elt->ref;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02001001 static_pattern.sflags = PAT_SF_TREE;
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001002 static_pattern.type = SMP_T_IPV4;
Willy Tarreau33ccf1c2019-06-16 18:40:33 +02001003 static_pattern.val.ipv4.addr = *(struct in_addr *)elt->node.key;
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001004 if (!cidr2dotted(elt->node.node.pfx, &static_pattern.val.ipv4.mask))
1005 return NULL;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01001006 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001007 return &static_pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +01001008 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001009 }
1010 }
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +01001011
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001012 /* Lookup in the list. the list contain only IPv4 patterns */
1013 list_for_each_entry(lst, &expr->patterns, list) {
1014 pattern = &lst->pat;
1015
1016 /* The input sample is IPv4, use it as is. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001017 if (smp->data.type == SMP_T_IPV4) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001018 v4 = smp->data.u.ipv4.s_addr;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +01001019 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001020 else if (smp->data.type == SMP_T_IPV6) {
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001021 /* v4 match on a V6 sample. We want to check at least for
1022 * the following forms :
1023 * - ::ffff:ip:v4 (ipv4 mapped)
1024 * - ::0000:ip:v4 (old ipv4 mapped)
1025 * - 2002:ip:v4:: (6to4)
1026 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001027 if (*(uint32_t*)&smp->data.u.ipv6.s6_addr[0] == 0 &&
1028 *(uint32_t*)&smp->data.u.ipv6.s6_addr[4] == 0 &&
1029 (*(uint32_t*)&smp->data.u.ipv6.s6_addr[8] == 0 ||
1030 *(uint32_t*)&smp->data.u.ipv6.s6_addr[8] == htonl(0xFFFF))) {
1031 v4 = *(uint32_t*)&smp->data.u.ipv6.s6_addr[12];
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01001032 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001033 else if (*(uint16_t*)&smp->data.u.ipv6.s6_addr[0] == htons(0x2002)) {
1034 v4 = htonl((ntohs(*(uint16_t*)&smp->data.u.ipv6.s6_addr[2]) << 16) +
1035 ntohs(*(uint16_t*)&smp->data.u.ipv6.s6_addr[4]));
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01001036 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001037 else
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01001038 continue;
Andreas Seltenreichf0653192016-03-03 20:08:35 +01001039 } else {
1040 /* impossible */
1041 continue;
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001042 }
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +01001043
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001044 /* Check if the input sample match the current pattern. */
1045 if (((v4 ^ pattern->val.ipv4.addr.s_addr) & pattern->val.ipv4.mask.s_addr) == 0)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01001046 return pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +01001047 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01001048 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +01001049}
1050
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001051void free_pattern_tree(struct eb_root *root)
1052{
1053 struct eb_node *node, *next;
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +01001054 struct pattern_tree *elt;
Thierry FOURNIER3ce88c72013-12-09 11:29:46 +01001055
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001056 node = eb_first(root);
1057 while (node) {
1058 next = eb_next(node);
1059 eb_delete(node);
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +01001060 elt = container_of(node, struct pattern_tree, node);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001061 free(elt->data);
Thierry FOURNIER3ce88c72013-12-09 11:29:46 +01001062 free(elt);
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001063 node = next;
1064 }
1065}
1066
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01001067void pat_prune_val(struct pattern_expr *expr)
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +01001068{
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01001069 struct pattern_list *pat, *tmp;
1070
1071 list_for_each_entry_safe(pat, tmp, &expr->patterns, list) {
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001072 free(pat->pat.data);
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01001073 free(pat);
1074 }
1075
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +01001076 free_pattern_tree(&expr->pattern_tree);
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001077 free_pattern_tree(&expr->pattern_tree_2);
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +01001078 LIST_INIT(&expr->patterns);
1079}
1080
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01001081void pat_prune_ptr(struct pattern_expr *expr)
1082{
1083 struct pattern_list *pat, *tmp;
1084
1085 list_for_each_entry_safe(pat, tmp, &expr->patterns, list) {
1086 free(pat->pat.ptr.ptr);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001087 free(pat->pat.data);
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01001088 free(pat);
1089 }
1090
1091 free_pattern_tree(&expr->pattern_tree);
1092 free_pattern_tree(&expr->pattern_tree_2);
1093 LIST_INIT(&expr->patterns);
1094}
1095
1096void pat_prune_reg(struct pattern_expr *expr)
1097{
1098 struct pattern_list *pat, *tmp;
1099
1100 list_for_each_entry_safe(pat, tmp, &expr->patterns, list) {
1101 regex_free(pat->pat.ptr.ptr);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001102 free(pat->pat.data);
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01001103 free(pat);
1104 }
1105
1106 free_pattern_tree(&expr->pattern_tree);
1107 free_pattern_tree(&expr->pattern_tree_2);
1108 LIST_INIT(&expr->patterns);
1109}
1110
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001111/*
1112 *
1113 * The following functions are used for the pattern indexation
1114 *
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001115 */
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001116
1117int pat_idx_list_val(struct pattern_expr *expr, struct pattern *pat, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001118{
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001119 struct pattern_list *patl;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001120
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001121 /* allocate pattern */
1122 patl = calloc(1, sizeof(*patl));
1123 if (!patl) {
1124 memprintf(err, "out of memory while indexing pattern");
Thierry FOURNIER972028f2014-01-23 17:53:31 +01001125 return 0;
1126 }
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001127
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001128 /* duplicate pattern */
1129 memcpy(&patl->pat, pat, sizeof(*pat));
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001130
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001131 /* chain pattern in the expression */
1132 LIST_ADDQ(&expr->patterns, &patl->list);
Willy Tarreau72f073b2015-04-29 17:53:47 +02001133 expr->revision = rdtsc();
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001134
1135 /* that's ok */
1136 return 1;
1137}
1138
1139int pat_idx_list_ptr(struct pattern_expr *expr, struct pattern *pat, char **err)
1140{
1141 struct pattern_list *patl;
1142
1143 /* allocate pattern */
1144 patl = calloc(1, sizeof(*patl));
Thierry FOURNIER8aa83842015-02-06 17:50:55 +01001145 if (!patl) {
1146 memprintf(err, "out of memory while indexing pattern");
Thierry FOURNIER972028f2014-01-23 17:53:31 +01001147 return 0;
Thierry FOURNIER8aa83842015-02-06 17:50:55 +01001148 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001149
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001150 /* duplicate pattern */
1151 memcpy(&patl->pat, pat, sizeof(*pat));
1152 patl->pat.ptr.ptr = malloc(patl->pat.len);
1153 if (!patl->pat.ptr.ptr) {
1154 free(patl);
1155 memprintf(err, "out of memory while indexing pattern");
1156 return 0;
1157 }
1158 memcpy(patl->pat.ptr.ptr, pat->ptr.ptr, pat->len);
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001159
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001160 /* chain pattern in the expression */
1161 LIST_ADDQ(&expr->patterns, &patl->list);
Willy Tarreau72f073b2015-04-29 17:53:47 +02001162 expr->revision = rdtsc();
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001163
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001164 /* that's ok */
1165 return 1;
1166}
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001167
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001168int pat_idx_list_str(struct pattern_expr *expr, struct pattern *pat, char **err)
1169{
1170 struct pattern_list *patl;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001171
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001172 /* allocate pattern */
1173 patl = calloc(1, sizeof(*patl));
1174 if (!patl) {
1175 memprintf(err, "out of memory while indexing pattern");
1176 return 0;
1177 }
1178
1179 /* duplicate pattern */
1180 memcpy(&patl->pat, pat, sizeof(*pat));
1181 patl->pat.ptr.str = malloc(patl->pat.len + 1);
1182 if (!patl->pat.ptr.str) {
1183 free(patl);
1184 memprintf(err, "out of memory while indexing pattern");
1185 return 0;
1186 }
1187 memcpy(patl->pat.ptr.ptr, pat->ptr.ptr, pat->len);
1188 patl->pat.ptr.str[patl->pat.len] = '\0';
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001189
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001190 /* chain pattern in the expression */
1191 LIST_ADDQ(&expr->patterns, &patl->list);
Willy Tarreau72f073b2015-04-29 17:53:47 +02001192 expr->revision = rdtsc();
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001193
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001194 /* that's ok */
1195 return 1;
1196}
1197
Thierry Fournier8feaa662016-02-10 22:55:20 +01001198int pat_idx_list_reg_cap(struct pattern_expr *expr, struct pattern *pat, int cap, char **err)
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001199{
1200 struct pattern_list *patl;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001201
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001202 /* allocate pattern */
1203 patl = calloc(1, sizeof(*patl));
1204 if (!patl) {
1205 memprintf(err, "out of memory while indexing pattern");
1206 return 0;
Thierry FOURNIER972028f2014-01-23 17:53:31 +01001207 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001208
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001209 /* duplicate pattern */
1210 memcpy(&patl->pat, pat, sizeof(*pat));
1211
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001212 /* compile regex */
Dragan Dosen26743032019-04-30 15:54:36 +02001213 if (!(patl->pat.ptr.reg = regex_comp(pat->ptr.str, !(expr->mflags & PAT_MF_IGNORE_CASE),
1214 cap, err))) {
Dirkjan Bussink07fcaaa2014-04-28 22:57:16 +00001215 free(patl);
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001216 return 0;
1217 }
1218
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001219 /* chain pattern in the expression */
1220 LIST_ADDQ(&expr->patterns, &patl->list);
Willy Tarreau72f073b2015-04-29 17:53:47 +02001221 expr->revision = rdtsc();
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001222
1223 /* that's ok */
1224 return 1;
1225}
1226
Thierry Fournier8feaa662016-02-10 22:55:20 +01001227int pat_idx_list_reg(struct pattern_expr *expr, struct pattern *pat, char **err)
1228{
1229 return pat_idx_list_reg_cap(expr, pat, 0, err);
1230}
1231
1232int pat_idx_list_regm(struct pattern_expr *expr, struct pattern *pat, char **err)
1233{
1234 return pat_idx_list_reg_cap(expr, pat, 1, err);
1235}
1236
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001237int pat_idx_tree_ip(struct pattern_expr *expr, struct pattern *pat, char **err)
1238{
1239 unsigned int mask;
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +01001240 struct pattern_tree *node;
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001241
1242 /* Only IPv4 can be indexed */
1243 if (pat->type == SMP_T_IPV4) {
Thierry FOURNIER972028f2014-01-23 17:53:31 +01001244 /* in IPv4 case, check if the mask is contiguous so that we can
1245 * insert the network into the tree. A continuous mask has only
1246 * ones on the left. This means that this mask + its lower bit
1247 * added once again is null.
1248 */
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001249 mask = ntohl(pat->val.ipv4.mask.s_addr);
1250 if (mask + (mask & -mask) == 0) {
1251 mask = mask ? 33 - flsnz(mask & -mask) : 0; /* equals cidr value */
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001252
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001253 /* node memory allocation */
1254 node = calloc(1, sizeof(*node) + 4);
1255 if (!node) {
1256 memprintf(err, "out of memory while loading pattern");
1257 return 0;
1258 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001259
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001260 /* copy the pointer to sample associated to this node */
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001261 node->data = pat->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01001262 node->ref = pat->ref;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001263
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001264 /* FIXME: insert <addr>/<mask> into the tree here */
1265 memcpy(node->node.key, &pat->val.ipv4.addr, 4); /* network byte order */
1266 node->node.node.pfx = mask;
Thierry FOURNIER31db4ae2014-01-30 00:27:15 +01001267
1268 /* Insert the entry. */
1269 ebmb_insert_prefix(&expr->pattern_tree, &node->node, 4);
Willy Tarreau72f073b2015-04-29 17:53:47 +02001270 expr->revision = rdtsc();
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001271
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001272 /* that's ok */
1273 return 1;
1274 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001275 else {
1276 /* If the mask is not contiguous, just add the pattern to the list */
1277 return pat_idx_list_val(expr, pat, err);
1278 }
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001279 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001280 else if (pat->type == SMP_T_IPV6) {
1281 /* IPv6 also can be indexed */
1282 node = calloc(1, sizeof(*node) + 16);
1283 if (!node) {
1284 memprintf(err, "out of memory while loading pattern");
1285 return 0;
1286 }
1287
1288 /* copy the pointer to sample associated to this node */
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001289 node->data = pat->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01001290 node->ref = pat->ref;
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001291
1292 /* FIXME: insert <addr>/<mask> into the tree here */
1293 memcpy(node->node.key, &pat->val.ipv6.addr, 16); /* network byte order */
1294 node->node.node.pfx = pat->val.ipv6.mask;
Thierry FOURNIER31db4ae2014-01-30 00:27:15 +01001295
1296 /* Insert the entry. */
1297 ebmb_insert_prefix(&expr->pattern_tree_2, &node->node, 16);
Willy Tarreau72f073b2015-04-29 17:53:47 +02001298 expr->revision = rdtsc();
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001299
1300 /* that's ok */
1301 return 1;
1302 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001303
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001304 return 0;
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001305}
1306
1307int pat_idx_tree_str(struct pattern_expr *expr, struct pattern *pat, char **err)
1308{
1309 int len;
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +01001310 struct pattern_tree *node;
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001311
1312 /* Only string can be indexed */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001313 if (pat->type != SMP_T_STR) {
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001314 memprintf(err, "internal error: string expected, but the type is '%s'",
1315 smp_to_type[pat->type]);
1316 return 0;
Thierry FOURNIER972028f2014-01-23 17:53:31 +01001317 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001318
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001319 /* If the flag PAT_F_IGNORE_CASE is set, we cannot use trees */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02001320 if (expr->mflags & PAT_MF_IGNORE_CASE)
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001321 return pat_idx_list_str(expr, pat, err);
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001322
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001323 /* Process the key len */
1324 len = strlen(pat->ptr.str) + 1;
1325
1326 /* node memory allocation */
1327 node = calloc(1, sizeof(*node) + len);
1328 if (!node) {
1329 memprintf(err, "out of memory while loading pattern");
1330 return 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001331 }
1332
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001333 /* copy the pointer to sample associated to this node */
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001334 node->data = pat->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01001335 node->ref = pat->ref;
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001336
1337 /* copy the string */
1338 memcpy(node->node.key, pat->ptr.str, len);
1339
1340 /* index the new node */
Thierry FOURNIER31db4ae2014-01-30 00:27:15 +01001341 ebst_insert(&expr->pattern_tree, &node->node);
Willy Tarreau72f073b2015-04-29 17:53:47 +02001342 expr->revision = rdtsc();
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001343
1344 /* that's ok */
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001345 return 1;
1346}
1347
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +02001348int pat_idx_tree_pfx(struct pattern_expr *expr, struct pattern *pat, char **err)
1349{
1350 int len;
1351 struct pattern_tree *node;
1352
1353 /* Only string can be indexed */
1354 if (pat->type != SMP_T_STR) {
1355 memprintf(err, "internal error: string expected, but the type is '%s'",
1356 smp_to_type[pat->type]);
1357 return 0;
1358 }
1359
1360 /* If the flag PAT_F_IGNORE_CASE is set, we cannot use trees */
1361 if (expr->mflags & PAT_MF_IGNORE_CASE)
1362 return pat_idx_list_str(expr, pat, err);
1363
1364 /* Process the key len */
1365 len = strlen(pat->ptr.str);
1366
1367 /* node memory allocation */
1368 node = calloc(1, sizeof(*node) + len + 1);
1369 if (!node) {
1370 memprintf(err, "out of memory while loading pattern");
1371 return 0;
1372 }
1373
1374 /* copy the pointer to sample associated to this node */
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001375 node->data = pat->data;
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +02001376 node->ref = pat->ref;
1377
1378 /* copy the string and the trailing zero */
1379 memcpy(node->node.key, pat->ptr.str, len + 1);
1380 node->node.node.pfx = len * 8;
1381
1382 /* index the new node */
1383 ebmb_insert_prefix(&expr->pattern_tree, &node->node, len);
Willy Tarreau72f073b2015-04-29 17:53:47 +02001384 expr->revision = rdtsc();
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +02001385
1386 /* that's ok */
1387 return 1;
1388}
1389
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001390void pat_del_list_val(struct pattern_expr *expr, struct pat_ref_elt *ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001391{
1392 struct pattern_list *pat;
1393 struct pattern_list *safe;
1394
1395 list_for_each_entry_safe(pat, safe, &expr->patterns, list) {
1396 /* Check equality. */
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001397 if (pat->pat.ref != ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001398 continue;
1399
1400 /* Delete and free entry. */
1401 LIST_DEL(&pat->list);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001402 free(pat->pat.data);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001403 free(pat);
1404 }
Willy Tarreau72f073b2015-04-29 17:53:47 +02001405 expr->revision = rdtsc();
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001406}
1407
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001408void pat_del_tree_ip(struct pattern_expr *expr, struct pat_ref_elt *ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001409{
1410 struct ebmb_node *node, *next_node;
1411 struct pattern_tree *elt;
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001412
1413 /* browse each node of the tree for IPv4 addresses. */
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001414 for (node = ebmb_first(&expr->pattern_tree), next_node = node ? ebmb_next(node) : NULL;
1415 node;
1416 node = next_node, next_node = node ? ebmb_next(node) : NULL) {
1417 /* Extract container of the tree node. */
1418 elt = container_of(node, struct pattern_tree, node);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001419
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001420 /* Check equality. */
1421 if (elt->ref != ref)
1422 continue;
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001423
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001424 /* Delete and free entry. */
1425 ebmb_delete(node);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001426 free(elt->data);
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001427 free(elt);
1428 }
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001429
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001430 /* Browse each node of the list for IPv4 addresses. */
1431 pat_del_list_val(expr, ref);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001432
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001433 /* browse each node of the tree for IPv6 addresses. */
1434 for (node = ebmb_first(&expr->pattern_tree_2), next_node = node ? ebmb_next(node) : NULL;
1435 node;
1436 node = next_node, next_node = node ? ebmb_next(node) : NULL) {
1437 /* Extract container of the tree node. */
1438 elt = container_of(node, struct pattern_tree, node);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001439
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001440 /* Check equality. */
1441 if (elt->ref != ref)
1442 continue;
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001443
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001444 /* Delete and free entry. */
1445 ebmb_delete(node);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001446 free(elt->data);
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001447 free(elt);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001448 }
Willy Tarreau72f073b2015-04-29 17:53:47 +02001449 expr->revision = rdtsc();
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001450}
1451
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001452void pat_del_list_ptr(struct pattern_expr *expr, struct pat_ref_elt *ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001453{
1454 struct pattern_list *pat;
1455 struct pattern_list *safe;
1456
1457 list_for_each_entry_safe(pat, safe, &expr->patterns, list) {
1458 /* Check equality. */
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001459 if (pat->pat.ref != ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001460 continue;
1461
1462 /* Delete and free entry. */
1463 LIST_DEL(&pat->list);
1464 free(pat->pat.ptr.ptr);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001465 free(pat->pat.data);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001466 free(pat);
1467 }
Willy Tarreau72f073b2015-04-29 17:53:47 +02001468 expr->revision = rdtsc();
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001469}
1470
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001471void pat_del_tree_str(struct pattern_expr *expr, struct pat_ref_elt *ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001472{
1473 struct ebmb_node *node, *next_node;
1474 struct pattern_tree *elt;
1475
Thierry FOURNIER73bc2852015-02-06 17:53:54 +01001476 /* If the flag PAT_F_IGNORE_CASE is set, we cannot use trees */
1477 if (expr->mflags & PAT_MF_IGNORE_CASE)
1478 return pat_del_list_ptr(expr, ref);
1479
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001480 /* browse each node of the tree. */
1481 for (node = ebmb_first(&expr->pattern_tree), next_node = node ? ebmb_next(node) : NULL;
1482 node;
1483 node = next_node, next_node = node ? ebmb_next(node) : NULL) {
1484 /* Extract container of the tree node. */
1485 elt = container_of(node, struct pattern_tree, node);
1486
1487 /* Check equality. */
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001488 if (elt->ref != ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001489 continue;
1490
1491 /* Delete and free entry. */
1492 ebmb_delete(node);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001493 free(elt->data);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001494 free(elt);
1495 }
Willy Tarreau72f073b2015-04-29 17:53:47 +02001496 expr->revision = rdtsc();
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001497}
1498
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001499void pat_del_list_reg(struct pattern_expr *expr, struct pat_ref_elt *ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001500{
1501 struct pattern_list *pat;
1502 struct pattern_list *safe;
1503
1504 list_for_each_entry_safe(pat, safe, &expr->patterns, list) {
1505 /* Check equality. */
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001506 if (pat->pat.ref != ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001507 continue;
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001508
1509 /* Delete and free entry. */
1510 LIST_DEL(&pat->list);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001511 regex_free(pat->pat.ptr.ptr);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001512 free(pat->pat.data);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001513 free(pat);
1514 }
Willy Tarreau72f073b2015-04-29 17:53:47 +02001515 expr->revision = rdtsc();
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001516}
1517
1518void pattern_init_expr(struct pattern_expr *expr)
1519{
1520 LIST_INIT(&expr->patterns);
Willy Tarreau72f073b2015-04-29 17:53:47 +02001521 expr->revision = 0;
Thierry FOURNIER31db4ae2014-01-30 00:27:15 +01001522 expr->pattern_tree = EB_ROOT;
1523 expr->pattern_tree_2 = EB_ROOT;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001524}
1525
1526void pattern_init_head(struct pattern_head *head)
1527{
1528 LIST_INIT(&head->head);
1529}
1530
1531/* The following functions are relative to the management of the reference
1532 * lists. These lists are used to store the original pattern and associated
1533 * value as string form.
1534 *
1535 * This is used with modifiable ACL and MAPS
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001536 *
1537 * The pattern reference are stored with two identifiers: the unique_id and
1538 * the reference.
1539 *
1540 * The reference identify a file. Each file with the same name point to the
1541 * same reference. We can register many times one file. If the file is modified,
1542 * all his dependencies are also modified. The reference can be used with map or
1543 * acl.
1544 *
1545 * The unique_id identify inline acl. The unique id is unique for each acl.
1546 * You cannot force the same id in the configuration file, because this repoort
1547 * an error.
1548 *
1549 * A particular case appears if the filename is a number. In this case, the
1550 * unique_id is set with the number represented by the filename and the
1551 * reference is also set. This method prevent double unique_id.
1552 *
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001553 */
1554
1555/* This function lookup for reference. If the reference is found, they return
1556 * pointer to the struct pat_ref, else return NULL.
1557 */
1558struct pat_ref *pat_ref_lookup(const char *reference)
1559{
1560 struct pat_ref *ref;
1561
1562 list_for_each_entry(ref, &pattern_reference, list)
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001563 if (ref->reference && strcmp(reference, ref->reference) == 0)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001564 return ref;
1565 return NULL;
1566}
1567
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001568/* This function lookup for unique id. If the reference is found, they return
1569 * pointer to the struct pat_ref, else return NULL.
1570 */
1571struct pat_ref *pat_ref_lookupid(int unique_id)
1572{
1573 struct pat_ref *ref;
1574
1575 list_for_each_entry(ref, &pattern_reference, list)
1576 if (ref->unique_id == unique_id)
1577 return ref;
1578 return NULL;
1579}
1580
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001581/* This function remove all pattern matching the pointer <refelt> from
1582 * the the reference and from each expr member of the reference. This
1583 * function returns 1 if the deletion is done and return 0 is the entry
1584 * is not found.
1585 */
1586int pat_ref_delete_by_id(struct pat_ref *ref, struct pat_ref_elt *refelt)
1587{
1588 struct pattern_expr *expr;
1589 struct pat_ref_elt *elt, *safe;
Emeric Brun8d85aa42017-06-29 15:40:33 +02001590 struct bref *bref, *back;
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001591
1592 /* delete pattern from reference */
1593 list_for_each_entry_safe(elt, safe, &ref->head, list) {
1594 if (elt == refelt) {
Emeric Brun8d85aa42017-06-29 15:40:33 +02001595 list_for_each_entry_safe(bref, back, &elt->back_refs, users) {
1596 /*
1597 * we have to unlink all watchers. We must not relink them if
1598 * this elt was the last one in the list.
1599 */
1600 LIST_DEL(&bref->users);
1601 LIST_INIT(&bref->users);
1602 if (elt->list.n != &ref->head)
Willy Tarreau49ee3b22019-04-30 11:43:43 +02001603 LIST_ADDQ(&LIST_ELEM(elt->list.n, typeof(elt), list)->back_refs, &bref->users);
Emeric Brun8d85aa42017-06-29 15:40:33 +02001604 bref->ref = elt->list.n;
1605 }
peter caiaede6dd2015-10-07 00:07:43 -07001606 list_for_each_entry(expr, &ref->pat, list)
1607 pattern_delete(expr, elt);
1608
Emeric Brunb5997f72017-07-03 11:34:05 +02001609 /* pat_ref_elt is trashed once all expr
1610 are cleaned and there is no ref remaining */
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001611 LIST_DEL(&elt->list);
1612 free(elt->sample);
1613 free(elt->pattern);
1614 free(elt);
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001615 return 1;
1616 }
1617 }
1618 return 0;
1619}
1620
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001621/* This function remove all pattern match <key> from the the reference
Joseph Herlant4189d672018-11-15 10:22:31 -08001622 * and from each expr member of the reference. This function returns 1
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001623 * if the deletion is done and return 0 is the entry is not found.
1624 */
1625int pat_ref_delete(struct pat_ref *ref, const char *key)
1626{
1627 struct pattern_expr *expr;
1628 struct pat_ref_elt *elt, *safe;
Emeric Brun8d85aa42017-06-29 15:40:33 +02001629 struct bref *bref, *back;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001630 int found = 0;
1631
1632 /* delete pattern from reference */
1633 list_for_each_entry_safe(elt, safe, &ref->head, list) {
1634 if (strcmp(key, elt->pattern) == 0) {
Emeric Brun8d85aa42017-06-29 15:40:33 +02001635 list_for_each_entry_safe(bref, back, &elt->back_refs, users) {
1636 /*
1637 * we have to unlink all watchers. We must not relink them if
1638 * this elt was the last one in the list.
1639 */
1640 LIST_DEL(&bref->users);
1641 LIST_INIT(&bref->users);
1642 if (elt->list.n != &ref->head)
Willy Tarreau49ee3b22019-04-30 11:43:43 +02001643 LIST_ADDQ(&LIST_ELEM(elt->list.n, typeof(elt), list)->back_refs, &bref->users);
Emeric Brun8d85aa42017-06-29 15:40:33 +02001644 bref->ref = elt->list.n;
1645 }
Dirkjan Bussink07fcaaa2014-04-28 22:57:16 +00001646 list_for_each_entry(expr, &ref->pat, list)
1647 pattern_delete(expr, elt);
1648
Emeric Brunb5997f72017-07-03 11:34:05 +02001649 /* pat_ref_elt is trashed once all expr
1650 are cleaned and there is no ref remaining */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001651 LIST_DEL(&elt->list);
1652 free(elt->sample);
1653 free(elt->pattern);
1654 free(elt);
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001655
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001656 found = 1;
1657 }
1658 }
1659
1660 if (!found)
1661 return 0;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001662 return 1;
1663}
1664
Baptiste Assmann953f74d2014-04-25 16:57:03 +02001665/*
1666 * find and return an element <elt> matching <key> in a reference <ref>
1667 * return NULL if not found
1668 */
1669struct pat_ref_elt *pat_ref_find_elt(struct pat_ref *ref, const char *key)
1670{
1671 struct pat_ref_elt *elt;
1672
1673 list_for_each_entry(elt, &ref->head, list) {
1674 if (strcmp(key, elt->pattern) == 0)
1675 return elt;
1676 }
1677
1678 return NULL;
1679}
1680
1681
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001682 /* This function modify the sample of the first pattern that match the <key>. */
1683static inline int pat_ref_set_elt(struct pat_ref *ref, struct pat_ref_elt *elt,
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001684 const char *value, char **err)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001685{
1686 struct pattern_expr *expr;
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001687 struct sample_data **data;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001688 char *sample;
Thierry FOURNIER12ba0c22015-08-14 00:02:11 +02001689 struct sample_data test;
Thierry FOURNIER149e0fe2014-01-29 19:35:06 +01001690
1691 /* Try all needed converters. */
1692 list_for_each_entry(expr, &ref->pat, list) {
1693 if (!expr->pat_head->parse_smp)
1694 continue;
1695
1696 if (!expr->pat_head->parse_smp(value, &test)) {
1697 memprintf(err, "unable to parse '%s'", value);
1698 return 0;
1699 }
1700 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001701
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001702 /* Modify pattern from reference. */
1703 sample = strdup(value);
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001704 if (!sample) {
1705 memprintf(err, "out of memory error");
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001706 return 0;
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001707 }
Thierry FOURNIER149e0fe2014-01-29 19:35:06 +01001708 /* Load sample in each reference. All the conversion are tested
1709 * below, normally these calls dosn't fail.
1710 */
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001711 list_for_each_entry(expr, &ref->pat, list) {
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001712 if (!expr->pat_head->parse_smp)
1713 continue;
1714
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001715 HA_RWLOCK_WRLOCK(PATEXP_LOCK, &expr->lock);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001716 data = pattern_find_smp(expr, elt);
1717 if (data && *data && !expr->pat_head->parse_smp(sample, *data))
1718 *data = NULL;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001719 HA_RWLOCK_WRUNLOCK(PATEXP_LOCK, &expr->lock);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001720 }
1721
Emeric Brunb5997f72017-07-03 11:34:05 +02001722 /* free old sample only when all exprs are updated */
1723 free(elt->sample);
1724 elt->sample = sample;
1725
1726
Thierry FOURNIER149e0fe2014-01-29 19:35:06 +01001727 return 1;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001728}
1729
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001730/* This function modify the sample of the first pattern that match the <key>. */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001731int 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 +01001732{
1733 struct pat_ref_elt *elt;
1734
1735 /* Look for pattern in the reference. */
1736 list_for_each_entry(elt, &ref->head, list) {
1737 if (elt == refelt) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001738 if (!pat_ref_set_elt(ref, elt, value, err))
1739 return 0;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001740 return 1;
1741 }
1742 }
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001743
1744 memprintf(err, "key or pattern not found");
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001745 return 0;
1746}
1747
1748/* This function modify the sample of the first pattern that match the <key>. */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001749int pat_ref_set(struct pat_ref *ref, const char *key, const char *value, char **err)
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001750{
1751 struct pat_ref_elt *elt;
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001752 int found = 0;
1753 char *_merr;
1754 char **merr;
1755
1756 if (err) {
1757 merr = &_merr;
1758 *merr = NULL;
1759 }
1760 else
1761 merr = NULL;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001762
1763 /* Look for pattern in the reference. */
1764 list_for_each_entry(elt, &ref->head, list) {
1765 if (strcmp(key, elt->pattern) == 0) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001766 if (!pat_ref_set_elt(ref, elt, value, merr)) {
William Lallemand579fb252018-06-11 10:53:46 +02001767 if (err && merr) {
1768 if (!found) {
1769 *err = *merr;
1770 } else {
1771 memprintf(err, "%s, %s", *err, *merr);
1772 free(*merr);
1773 *merr = NULL;
1774 }
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001775 }
1776 }
1777 found = 1;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001778 }
1779 }
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001780
1781 if (!found) {
1782 memprintf(err, "entry not found");
1783 return 0;
1784 }
1785 return 1;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001786}
1787
Joseph Herlant4189d672018-11-15 10:22:31 -08001788/* This function creates a new reference. <ref> is the reference name.
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001789 * <flags> are PAT_REF_*. /!\ The reference is not checked, and must
1790 * be unique. The user must check the reference with "pat_ref_lookup()"
Joseph Herlant4189d672018-11-15 10:22:31 -08001791 * before calling this function. If the function fail, it return NULL,
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001792 * else return new struct pat_ref.
1793 */
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001794struct pat_ref *pat_ref_new(const char *reference, const char *display, unsigned int flags)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001795{
1796 struct pat_ref *ref;
1797
1798 ref = malloc(sizeof(*ref));
1799 if (!ref)
1800 return NULL;
1801
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001802 if (display) {
1803 ref->display = strdup(display);
1804 if (!ref->display) {
1805 free(ref);
1806 return NULL;
1807 }
1808 }
1809 else
1810 ref->display = NULL;
1811
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001812 ref->reference = strdup(reference);
1813 if (!ref->reference) {
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001814 free(ref->display);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001815 free(ref);
1816 return NULL;
1817 }
1818
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001819 ref->flags = flags;
1820 ref->unique_id = -1;
1821
1822 LIST_INIT(&ref->head);
1823 LIST_INIT(&ref->pat);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001824 HA_SPIN_INIT(&ref->lock);
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001825 LIST_ADDQ(&pattern_reference, &ref->list);
1826
1827 return ref;
1828}
1829
1830/* This function create new reference. <unique_id> is the unique id. If
1831 * the value of <unique_id> is -1, the unique id is calculated later.
1832 * <flags> are PAT_REF_*. /!\ The reference is not checked, and must
1833 * be unique. The user must check the reference with "pat_ref_lookup()"
1834 * or pat_ref_lookupid before calling this function. If the function
1835 * fail, it return NULL, else return new struct pat_ref.
1836 */
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001837struct pat_ref *pat_ref_newid(int unique_id, const char *display, unsigned int flags)
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001838{
1839 struct pat_ref *ref;
1840
1841 ref = malloc(sizeof(*ref));
1842 if (!ref)
1843 return NULL;
1844
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001845 if (display) {
1846 ref->display = strdup(display);
1847 if (!ref->display) {
1848 free(ref);
1849 return NULL;
1850 }
1851 }
1852 else
1853 ref->display = NULL;
1854
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001855 ref->reference = NULL;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001856 ref->flags = flags;
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001857 ref->unique_id = unique_id;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001858 LIST_INIT(&ref->head);
1859 LIST_INIT(&ref->pat);
Aurélien Nephtali564d15a2018-04-19 16:56:07 +02001860 HA_SPIN_INIT(&ref->lock);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001861 LIST_ADDQ(&pattern_reference, &ref->list);
1862
1863 return ref;
1864}
1865
1866/* This function adds entry to <ref>. It can failed with memory error.
1867 * If the function fails, it returns 0.
1868 */
1869int pat_ref_append(struct pat_ref *ref, char *pattern, char *sample, int line)
1870{
1871 struct pat_ref_elt *elt;
1872
1873 elt = malloc(sizeof(*elt));
1874 if (!elt)
1875 return 0;
1876
1877 elt->line = line;
1878
1879 elt->pattern = strdup(pattern);
1880 if (!elt->pattern) {
1881 free(elt);
1882 return 0;
1883 }
1884
1885 if (sample) {
1886 elt->sample = strdup(sample);
1887 if (!elt->sample) {
1888 free(elt->pattern);
1889 free(elt);
1890 return 0;
1891 }
1892 }
1893 else
1894 elt->sample = NULL;
1895
Emeric Brun8d85aa42017-06-29 15:40:33 +02001896 LIST_INIT(&elt->back_refs);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001897 LIST_ADDQ(&ref->head, &elt->list);
1898
1899 return 1;
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001900}
1901
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001902/* This function create sample found in <elt>, parse the pattern also
1903 * found in <elt> and insert it in <expr>. The function copy <patflags>
1904 * in <expr>. If the function fails, it returns0 and <err> is filled.
1905 * In succes case, the function returns 1.
1906 */
1907static inline
1908int pat_ref_push(struct pat_ref_elt *elt, struct pattern_expr *expr,
1909 int patflags, char **err)
1910{
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001911 struct sample_data *data;
Thierry FOURNIERd25c8422014-01-28 15:34:35 +01001912 struct pattern pattern;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001913
1914 /* Create sample */
1915 if (elt->sample && expr->pat_head->parse_smp) {
1916 /* New sample. */
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001917 data = malloc(sizeof(*data));
1918 if (!data)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001919 return 0;
1920
1921 /* Parse value. */
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001922 if (!expr->pat_head->parse_smp(elt->sample, data)) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001923 memprintf(err, "unable to parse '%s'", elt->sample);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001924 free(data);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001925 return 0;
1926 }
1927
1928 }
1929 else
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001930 data = NULL;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001931
Thierry FOURNIERd25c8422014-01-28 15:34:35 +01001932 /* initialise pattern */
1933 memset(&pattern, 0, sizeof(pattern));
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001934 pattern.data = data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01001935 pattern.ref = elt;
Thierry FOURNIERd25c8422014-01-28 15:34:35 +01001936
1937 /* parse pattern */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02001938 if (!expr->pat_head->parse(elt->pattern, &pattern, expr->mflags, err)) {
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001939 free(data);
Thierry FOURNIERd25c8422014-01-28 15:34:35 +01001940 return 0;
1941 }
1942
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001943 HA_RWLOCK_WRLOCK(PATEXP_LOCK, &expr->lock);
Thierry FOURNIERd25c8422014-01-28 15:34:35 +01001944 /* index pattern */
1945 if (!expr->pat_head->index(expr, &pattern, err)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001946 HA_RWLOCK_WRUNLOCK(PATEXP_LOCK, &expr->lock);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001947 free(data);
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001948 return 0;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001949 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001950 HA_RWLOCK_WRUNLOCK(PATEXP_LOCK, &expr->lock);
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001951
1952 return 1;
1953}
1954
Thierry FOURNIER31db4ae2014-01-30 00:27:15 +01001955/* This function adds entry to <ref>. It can failed with memory error. The new
1956 * entry is added at all the pattern_expr registered in this reference. The
1957 * function stop on the first error encountered. It returns 0 and err is
1958 * filled. If an error is encountered, the complete add operation is cancelled.
1959 * If the insertion is a success the function returns 1.
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001960 */
1961int pat_ref_add(struct pat_ref *ref,
1962 const char *pattern, const char *sample,
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02001963 char **err)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001964{
1965 struct pat_ref_elt *elt;
1966 struct pattern_expr *expr;
1967
1968 elt = malloc(sizeof(*elt));
1969 if (!elt) {
1970 memprintf(err, "out of memory error");
1971 return 0;
1972 }
1973
1974 elt->line = -1;
1975
1976 elt->pattern = strdup(pattern);
1977 if (!elt->pattern) {
1978 free(elt);
1979 memprintf(err, "out of memory error");
1980 return 0;
1981 }
1982
1983 if (sample) {
1984 elt->sample = strdup(sample);
1985 if (!elt->sample) {
1986 free(elt->pattern);
1987 free(elt);
1988 memprintf(err, "out of memory error");
1989 return 0;
1990 }
1991 }
1992 else
1993 elt->sample = NULL;
1994
Emeric Brun8d85aa42017-06-29 15:40:33 +02001995 LIST_INIT(&elt->back_refs);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001996 LIST_ADDQ(&ref->head, &elt->list);
1997
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001998 list_for_each_entry(expr, &ref->pat, list) {
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02001999 if (!pat_ref_push(elt, expr, 0, err)) {
Thierry FOURNIER31db4ae2014-01-30 00:27:15 +01002000 /* If the insertion fails, try to delete all the added entries. */
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01002001 pat_ref_delete_by_id(ref, elt);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002002 return 0;
2003 }
2004 }
Emeric Brunb5997f72017-07-03 11:34:05 +02002005
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002006 return 1;
2007}
2008
Joseph Herlant4189d672018-11-15 10:22:31 -08002009/* This function prunes <ref>, replaces all references by the references
2010 * of <replace>, and reindexes all the news values.
Thierry FOURNIER46006bd2014-03-21 21:45:15 +01002011 *
Joseph Herlant4189d672018-11-15 10:22:31 -08002012 * The patterns are loaded in best effort and the errors are ignored,
2013 * but written in the logs.
Thierry FOURNIER46006bd2014-03-21 21:45:15 +01002014 */
2015void pat_ref_reload(struct pat_ref *ref, struct pat_ref *replace)
2016{
2017 struct pattern_expr *expr;
Emeric Brunb5997f72017-07-03 11:34:05 +02002018 struct pat_ref_elt *elt, *safe;
2019 struct bref *bref, *back;
Emeric Brunb5997f72017-07-03 11:34:05 +02002020 struct pattern pattern;
Thierry FOURNIER46006bd2014-03-21 21:45:15 +01002021
Thierry FOURNIER46006bd2014-03-21 21:45:15 +01002022
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002023 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Emeric Brunb5997f72017-07-03 11:34:05 +02002024 list_for_each_entry(expr, &ref->pat, list) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002025 HA_RWLOCK_WRLOCK(PATEXP_LOCK, &expr->lock);
Emeric Brunb5997f72017-07-03 11:34:05 +02002026 }
2027
2028 /* all expr are locked, we can safely remove all pat_ref */
2029 list_for_each_entry_safe(elt, safe, &ref->head, list) {
2030 list_for_each_entry_safe(bref, back, &elt->back_refs, users) {
2031 /*
2032 * we have to unlink all watchers. We must not relink them if
2033 * this elt was the last one in the list.
2034 */
2035 LIST_DEL(&bref->users);
2036 LIST_INIT(&bref->users);
2037 if (elt->list.n != &ref->head)
Willy Tarreau49ee3b22019-04-30 11:43:43 +02002038 LIST_ADDQ(&LIST_ELEM(elt->list.n, typeof(elt), list)->back_refs, &bref->users);
Emeric Brunb5997f72017-07-03 11:34:05 +02002039 bref->ref = elt->list.n;
2040 }
2041 LIST_DEL(&elt->list);
2042 free(elt->pattern);
2043 free(elt->sample);
2044 free(elt);
2045 }
2046
2047 /* switch pat_ret_elt lists */
Thierry FOURNIER46006bd2014-03-21 21:45:15 +01002048 LIST_ADD(&replace->head, &ref->head);
2049 LIST_DEL(&replace->head);
2050
Emeric Brunb5997f72017-07-03 11:34:05 +02002051 list_for_each_entry(expr, &ref->pat, list) {
2052 expr->pat_head->prune(expr);
2053 list_for_each_entry(elt, &ref->head, list) {
Dragan Dosenf1474792018-09-18 20:18:09 +02002054 char *err = NULL;
2055 struct sample_data *data = NULL;
2056
Emeric Brunb5997f72017-07-03 11:34:05 +02002057 /* Create sample */
2058 if (elt->sample && expr->pat_head->parse_smp) {
2059 /* New sample. */
2060 data = malloc(sizeof(*data));
2061 if (!data)
2062 continue;
2063
2064 /* Parse value. */
2065 if (!expr->pat_head->parse_smp(elt->sample, data)) {
2066 memprintf(&err, "unable to parse '%s'", elt->sample);
2067 send_log(NULL, LOG_NOTICE, "%s", err);
2068 free(err);
2069 free(data);
2070 continue;
2071 }
2072
2073 }
Emeric Brunb5997f72017-07-03 11:34:05 +02002074
2075 /* initialise pattern */
2076 memset(&pattern, 0, sizeof(pattern));
2077 pattern.data = data;
2078 pattern.ref = elt;
2079
2080 /* parse pattern */
2081 if (!expr->pat_head->parse(elt->pattern, &pattern, expr->mflags, &err)) {
Thierry FOURNIER46006bd2014-03-21 21:45:15 +01002082 send_log(NULL, LOG_NOTICE, "%s", err);
2083 free(err);
Emeric Brunb5997f72017-07-03 11:34:05 +02002084 free(data);
2085 continue;
2086 }
2087
2088 /* index pattern */
2089 if (!expr->pat_head->index(expr, &pattern, &err)) {
2090 send_log(NULL, LOG_NOTICE, "%s", err);
2091 free(err);
2092 free(data);
2093 continue;
Thierry FOURNIER46006bd2014-03-21 21:45:15 +01002094 }
2095 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002096 HA_RWLOCK_WRUNLOCK(PATEXP_LOCK, &expr->lock);
Thierry FOURNIER46006bd2014-03-21 21:45:15 +01002097 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002098 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER46006bd2014-03-21 21:45:15 +01002099}
2100
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002101/* This function prune all entries of <ref>. This function
2102 * prune the associated pattern_expr.
2103 */
2104void pat_ref_prune(struct pat_ref *ref)
2105{
2106 struct pat_ref_elt *elt, *safe;
2107 struct pattern_expr *expr;
Emeric Brun8d85aa42017-06-29 15:40:33 +02002108 struct bref *bref, *back;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002109
Emeric Brunb5997f72017-07-03 11:34:05 +02002110 list_for_each_entry(expr, &ref->pat, list) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002111 HA_RWLOCK_WRLOCK(PATEXP_LOCK, &expr->lock);
Emeric Brunb5997f72017-07-03 11:34:05 +02002112 expr->pat_head->prune(expr);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002113 HA_RWLOCK_WRUNLOCK(PATEXP_LOCK, &expr->lock);
Emeric Brunb5997f72017-07-03 11:34:05 +02002114 }
2115
2116 /* we trash pat_ref_elt in a second time to ensure that data is
2117 free once there is no ref on it */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002118 list_for_each_entry_safe(elt, safe, &ref->head, list) {
Emeric Brun8d85aa42017-06-29 15:40:33 +02002119 list_for_each_entry_safe(bref, back, &elt->back_refs, users) {
2120 /*
2121 * we have to unlink all watchers. We must not relink them if
2122 * this elt was the last one in the list.
2123 */
2124 LIST_DEL(&bref->users);
2125 LIST_INIT(&bref->users);
2126 if (elt->list.n != &ref->head)
Willy Tarreau49ee3b22019-04-30 11:43:43 +02002127 LIST_ADDQ(&LIST_ELEM(elt->list.n, typeof(elt), list)->back_refs, &bref->users);
Emeric Brun8d85aa42017-06-29 15:40:33 +02002128 bref->ref = elt->list.n;
2129 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002130 LIST_DEL(&elt->list);
2131 free(elt->pattern);
2132 free(elt->sample);
2133 free(elt);
2134 }
2135
Emeric Brunb5997f72017-07-03 11:34:05 +02002136
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002137}
2138
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002139/* This function lookup for existing reference <ref> in pattern_head <head>. */
2140struct pattern_expr *pattern_lookup_expr(struct pattern_head *head, struct pat_ref *ref)
2141{
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002142 struct pattern_expr_list *expr;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002143
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002144 list_for_each_entry(expr, &head->head, list)
2145 if (expr->expr->ref == ref)
2146 return expr->expr;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002147 return NULL;
2148}
2149
Joseph Herlant4189d672018-11-15 10:22:31 -08002150/* This function creates new pattern_expr associated to the reference <ref>.
2151 * <ref> can be NULL. If an error occurs, the function returns NULL and
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002152 * <err> is filled. Otherwise, the function returns new pattern_expr linked
2153 * with <head> and <ref>.
Thierry FOURNIER315ec422014-11-24 11:14:42 +01002154 *
Joseph Herlant4189d672018-11-15 10:22:31 -08002155 * The returned value can be an already filled pattern list, in this case the
Thierry FOURNIER315ec422014-11-24 11:14:42 +01002156 * flag <reuse> is set.
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002157 */
Thierry FOURNIER315ec422014-11-24 11:14:42 +01002158struct pattern_expr *pattern_new_expr(struct pattern_head *head, struct pat_ref *ref,
Emeric Brun7d27f3c2017-07-03 17:54:23 +02002159 int patflags, char **err, int *reuse)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002160{
2161 struct pattern_expr *expr;
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002162 struct pattern_expr_list *list;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002163
Thierry FOURNIER315ec422014-11-24 11:14:42 +01002164 if (reuse)
2165 *reuse = 0;
2166
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002167 /* Memory and initialization of the chain element. */
2168 list = malloc(sizeof(*list));
2169 if (!list) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002170 memprintf(err, "out of memory");
2171 return NULL;
2172 }
2173
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002174 /* Look for existing similar expr. No that only the index, parse and
2175 * parse_smp function must be identical for having similar pattern.
Joseph Herlant4189d672018-11-15 10:22:31 -08002176 * The other function depends of these first.
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002177 */
2178 if (ref) {
2179 list_for_each_entry(expr, &ref->pat, list)
2180 if (expr->pat_head->index == head->index &&
2181 expr->pat_head->parse == head->parse &&
Emeric Brun7d27f3c2017-07-03 17:54:23 +02002182 expr->pat_head->parse_smp == head->parse_smp &&
2183 expr->mflags == patflags)
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002184 break;
2185 if (&expr->list == &ref->pat)
2186 expr = NULL;
2187 }
2188 else
2189 expr = NULL;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002190
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002191 /* If no similar expr was found, we create new expr. */
2192 if (!expr) {
2193 /* Get a lot of memory for the expr struct. */
2194 expr = malloc(sizeof(*expr));
2195 if (!expr) {
Andreas Seltenreiche6e22e82016-03-03 20:20:23 +01002196 free(list);
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002197 memprintf(err, "out of memory");
2198 return NULL;
2199 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002200
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002201 /* Initialize this new expr. */
2202 pattern_init_expr(expr);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002203
Emeric Brun7d27f3c2017-07-03 17:54:23 +02002204 /* Copy the pattern matching and indexing flags. */
2205 expr->mflags = patflags;
2206
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002207 /* This new pattern expression reference one of his heads. */
2208 expr->pat_head = head;
2209
2210 /* Link with ref, or to self to facilitate LIST_DEL() */
2211 if (ref)
2212 LIST_ADDQ(&ref->pat, &expr->list);
2213 else
2214 LIST_INIT(&expr->list);
2215
2216 expr->ref = ref;
2217
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002218 HA_RWLOCK_INIT(&expr->lock);
Emeric Brunb5997f72017-07-03 11:34:05 +02002219
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002220 /* We must free this pattern if it is no more used. */
2221 list->do_free = 1;
2222 }
2223 else {
2224 /* If the pattern used already exists, it is already linked
2225 * with ref and we must not free it.
2226 */
2227 list->do_free = 0;
Thierry FOURNIER315ec422014-11-24 11:14:42 +01002228 if (reuse)
2229 *reuse = 1;
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002230 }
2231
2232 /* The new list element reference the pattern_expr. */
2233 list->expr = expr;
2234
2235 /* Link the list element with the pattern_head. */
2236 LIST_ADDQ(&head->head, &list->list);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002237 return expr;
2238}
2239
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002240/* Reads patterns from a file. If <err_msg> is non-NULL, an error message will
2241 * be returned there on errors and the caller will have to free it.
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002242 *
2243 * The file contains one key + value per line. Lines which start with '#' are
2244 * ignored, just like empty lines. Leading tabs/spaces are stripped. The key is
2245 * then the first "word" (series of non-space/tabs characters), and the value is
2246 * what follows this series of space/tab till the end of the line excluding
2247 * trailing spaces/tabs.
2248 *
2249 * Example :
2250 *
2251 * # this is a comment and is ignored
2252 * 62.212.114.60 1wt.eu \n
2253 * <-><-----------><---><----><---->
2254 * | | | | `--- trailing spaces ignored
2255 * | | | `-------- value
2256 * | | `--------------- middle spaces ignored
2257 * | `------------------------ key
2258 * `-------------------------------- leading spaces ignored
2259 *
2260 * Return non-zero in case of succes, otherwise 0.
2261 */
2262int pat_ref_read_from_file_smp(struct pat_ref *ref, const char *filename, char **err)
2263{
2264 FILE *file;
2265 char *c;
2266 int ret = 0;
2267 int line = 0;
2268 char *key_beg;
2269 char *key_end;
2270 char *value_beg;
2271 char *value_end;
2272
2273 file = fopen(filename, "r");
2274 if (!file) {
2275 memprintf(err, "failed to open pattern file <%s>", filename);
2276 return 0;
2277 }
2278
2279 /* now parse all patterns. The file may contain only one pattern
2280 * followed by one value per line. The start spaces, separator spaces
2281 * and and spaces are stripped. Each can contain comment started by '#'
2282 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002283 while (fgets(trash.area, trash.size, file) != NULL) {
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002284 line++;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002285 c = trash.area;
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002286
2287 /* ignore lines beginning with a dash */
2288 if (*c == '#')
2289 continue;
2290
2291 /* strip leading spaces and tabs */
2292 while (*c == ' ' || *c == '\t')
2293 c++;
2294
2295 /* empty lines are ignored too */
2296 if (*c == '\0' || *c == '\r' || *c == '\n')
2297 continue;
2298
2299 /* look for the end of the key */
2300 key_beg = c;
2301 while (*c && *c != ' ' && *c != '\t' && *c != '\n' && *c != '\r')
2302 c++;
2303
2304 key_end = c;
2305
2306 /* strip middle spaces and tabs */
2307 while (*c == ' ' || *c == '\t')
2308 c++;
2309
2310 /* look for the end of the value, it is the end of the line */
2311 value_beg = c;
2312 while (*c && *c != '\n' && *c != '\r')
2313 c++;
2314 value_end = c;
2315
2316 /* trim possibly trailing spaces and tabs */
2317 while (value_end > value_beg && (value_end[-1] == ' ' || value_end[-1] == '\t'))
2318 value_end--;
2319
2320 /* set final \0 and check entries */
2321 *key_end = '\0';
2322 *value_end = '\0';
2323
2324 /* insert values */
2325 if (!pat_ref_append(ref, key_beg, value_beg, line)) {
2326 memprintf(err, "out of memory");
2327 goto out_close;
2328 }
2329 }
2330
2331 /* succes */
2332 ret = 1;
2333
2334 out_close:
2335 fclose(file);
2336 return ret;
2337}
2338
2339/* Reads patterns from a file. If <err_msg> is non-NULL, an error message will
2340 * be returned there on errors and the caller will have to free it.
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002341 */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002342int pat_ref_read_from_file(struct pat_ref *ref, const char *filename, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002343{
2344 FILE *file;
2345 char *c;
2346 char *arg;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002347 int ret = 0;
2348 int line = 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002349
2350 file = fopen(filename, "r");
2351 if (!file) {
2352 memprintf(err, "failed to open pattern file <%s>", filename);
2353 return 0;
2354 }
2355
2356 /* now parse all patterns. The file may contain only one pattern per
2357 * line. If the line contains spaces, they will be part of the pattern.
2358 * The pattern stops at the first CR, LF or EOF encountered.
2359 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002360 while (fgets(trash.area, trash.size, file) != NULL) {
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002361 line++;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002362 c = trash.area;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002363
2364 /* ignore lines beginning with a dash */
2365 if (*c == '#')
2366 continue;
2367
2368 /* strip leading spaces and tabs */
2369 while (*c == ' ' || *c == '\t')
2370 c++;
2371
2372
2373 arg = c;
2374 while (*c && *c != '\n' && *c != '\r')
2375 c++;
2376 *c = 0;
2377
2378 /* empty lines are ignored too */
2379 if (c == arg)
2380 continue;
2381
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002382 if (!pat_ref_append(ref, arg, NULL, line)) {
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002383 memprintf(err, "out of memory when loading patterns from file <%s>", filename);
2384 goto out_close;
2385 }
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002386 }
2387
2388 ret = 1; /* success */
2389
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002390 out_close:
2391 fclose(file);
2392 return ret;
2393}
2394
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002395int pattern_read_from_file(struct pattern_head *head, unsigned int refflags,
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002396 const char *filename, int patflags, int load_smp,
Thierry FOURNIER94580c92014-02-11 14:36:45 +01002397 char **err, const char *file, int line)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002398{
2399 struct pat_ref *ref;
2400 struct pattern_expr *expr;
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002401 struct pat_ref_elt *elt;
Willy Tarreau4deaf392014-11-26 13:17:03 +01002402 int reuse = 0;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002403
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002404 /* Lookup for the existing reference. */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002405 ref = pat_ref_lookup(filename);
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002406
2407 /* If the reference doesn't exists, create it and load associated file. */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002408 if (!ref) {
Thierry FOURNIER94580c92014-02-11 14:36:45 +01002409 chunk_printf(&trash,
2410 "pattern loaded from file '%s' used by %s at file '%s' line %d",
2411 filename, refflags & PAT_REF_MAP ? "map" : "acl", file, line);
2412
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002413 ref = pat_ref_new(filename, trash.area, refflags);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002414 if (!ref) {
2415 memprintf(err, "out of memory");
2416 return 0;
2417 }
2418
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002419 if (load_smp) {
Thierry FOURNIERc0bd9102014-01-29 12:32:58 +01002420 ref->flags |= PAT_REF_SMP;
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002421 if (!pat_ref_read_from_file_smp(ref, filename, err))
2422 return 0;
2423 }
2424 else {
2425 if (!pat_ref_read_from_file(ref, filename, err))
2426 return 0;
2427 }
2428 }
2429 else {
Thierry FOURNIERc0bd9102014-01-29 12:32:58 +01002430 /* The reference already exists, check the map compatibility. */
2431
2432 /* If the load require samples and the flag PAT_REF_SMP is not set,
2433 * the reference doesn't contain sample, and cannot be used.
2434 */
2435 if (load_smp) {
2436 if (!(ref->flags & PAT_REF_SMP)) {
2437 memprintf(err, "The file \"%s\" is already used as one column file "
2438 "and cannot be used by as two column file.",
2439 filename);
2440 return 0;
2441 }
2442 }
2443 else {
2444 /* The load doesn't require samples. If the flag PAT_REF_SMP is
2445 * set, the reference contains a sample, and cannot be used.
2446 */
2447 if (ref->flags & PAT_REF_SMP) {
2448 memprintf(err, "The file \"%s\" is already used as two column file "
2449 "and cannot be used by as one column file.",
2450 filename);
2451 return 0;
2452 }
2453 }
2454
Thierry FOURNIER94580c92014-02-11 14:36:45 +01002455 /* Extends display */
2456 chunk_printf(&trash, "%s", ref->display);
2457 chunk_appendf(&trash, ", by %s at file '%s' line %d",
2458 refflags & PAT_REF_MAP ? "map" : "acl", file, line);
2459 free(ref->display);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002460 ref->display = strdup(trash.area);
Thierry FOURNIER94580c92014-02-11 14:36:45 +01002461 if (!ref->display) {
2462 memprintf(err, "out of memory");
2463 return 0;
2464 }
2465
Thierry FOURNIERc0bd9102014-01-29 12:32:58 +01002466 /* Merge flags. */
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002467 ref->flags |= refflags;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002468 }
2469
2470 /* Now, we can loading patterns from the reference. */
2471
2472 /* Lookup for existing reference in the head. If the reference
2473 * doesn't exists, create it.
2474 */
2475 expr = pattern_lookup_expr(head, ref);
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02002476 if (!expr || (expr->mflags != patflags)) {
Emeric Brun7d27f3c2017-07-03 17:54:23 +02002477 expr = pattern_new_expr(head, ref, patflags, err, &reuse);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002478 if (!expr)
2479 return 0;
2480 }
2481
Thierry FOURNIER315ec422014-11-24 11:14:42 +01002482 /* The returned expression may be not empty, because the function
2483 * "pattern_new_expr" lookup for similar pattern list and can
2484 * reuse a already filled pattern list. In this case, we can not
2485 * reload the patterns.
2486 */
2487 if (reuse)
2488 return 1;
2489
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002490 /* Load reference content in the pattern expression. */
2491 list_for_each_entry(elt, &ref->head, list) {
2492 if (!pat_ref_push(elt, expr, patflags, err)) {
2493 if (elt->line > 0)
2494 memprintf(err, "%s at line %d of file '%s'",
2495 *err, elt->line, filename);
2496 return 0;
2497 }
2498 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002499
2500 return 1;
2501}
2502
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01002503/* This function executes a pattern match on a sample. It applies pattern <expr>
2504 * to sample <smp>. The function returns NULL if the sample dont match. It returns
2505 * non-null if the sample match. If <fill> is true and the sample match, the
2506 * function returns the matched pattern. In many cases, this pattern can be a
2507 * static buffer.
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002508 */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002509struct pattern *pattern_exec_match(struct pattern_head *head, struct sample *smp, int fill)
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002510{
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002511 struct pattern_expr_list *list;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002512 struct pattern *pat;
2513
2514 if (!head->match) {
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01002515 if (fill) {
Thierry FOURNIER503bb092015-08-19 08:35:43 +02002516 static_pattern.data = NULL;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01002517 static_pattern.ref = NULL;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02002518 static_pattern.sflags = 0;
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002519 static_pattern.type = SMP_T_SINT;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01002520 static_pattern.val.i = 1;
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01002521 }
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01002522 return &static_pattern;
2523 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002524
Thierry FOURNIER5d344082014-01-27 14:19:53 +01002525 /* convert input to string */
2526 if (!sample_convert(smp, head->expect_type))
2527 return NULL;
2528
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002529 list_for_each_entry(list, &head->head, list) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002530 HA_RWLOCK_RDLOCK(PATEXP_LOCK, &list->expr->lock);
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002531 pat = head->match(smp, list->expr, fill);
Emeric Brunb5997f72017-07-03 11:34:05 +02002532 if (pat) {
2533 /* We duplicate the pattern cause it could be modified
2534 by another thread */
2535 if (pat != &static_pattern) {
2536 memcpy(&static_pattern, pat, sizeof(struct pattern));
2537 pat = &static_pattern;
2538 }
2539
2540 /* We also duplicate the sample data for
2541 same reason */
2542 if (pat->data && (pat->data != &static_sample_data)) {
Christopher Faulet09fdf4b2017-11-09 16:14:16 +01002543 switch(pat->data->type) {
Emeric Brunb5997f72017-07-03 11:34:05 +02002544 case SMP_T_STR:
2545 static_sample_data.type = SMP_T_STR;
2546 static_sample_data.u.str = *get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002547 static_sample_data.u.str.data = pat->data->u.str.data;
2548 if (static_sample_data.u.str.data >= static_sample_data.u.str.size)
2549 static_sample_data.u.str.data = static_sample_data.u.str.size - 1;
2550 memcpy(static_sample_data.u.str.area,
2551 pat->data->u.str.area,
2552 static_sample_data.u.str.data);
2553 static_sample_data.u.str.area[static_sample_data.u.str.data] = 0;
Emeric Brunb5997f72017-07-03 11:34:05 +02002554 case SMP_T_IPV4:
2555 case SMP_T_IPV6:
2556 case SMP_T_SINT:
2557 memcpy(&static_sample_data, pat->data, sizeof(struct sample_data));
2558 default:
2559 pat->data = NULL;
2560 }
2561 pat->data = &static_sample_data;
2562 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002563 HA_RWLOCK_RDUNLOCK(PATEXP_LOCK, &list->expr->lock);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002564 return pat;
Emeric Brunb5997f72017-07-03 11:34:05 +02002565 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002566 HA_RWLOCK_RDUNLOCK(PATEXP_LOCK, &list->expr->lock);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002567 }
2568 return NULL;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002569}
2570
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01002571/* This function prune the pattern expression. */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002572void pattern_prune(struct pattern_head *head)
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01002573{
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002574 struct pattern_expr_list *list, *safe;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002575
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002576 list_for_each_entry_safe(list, safe, &head->head, list) {
2577 LIST_DEL(&list->list);
2578 if (list->do_free) {
2579 LIST_DEL(&list->expr->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002580 HA_RWLOCK_WRLOCK(PATEXP_LOCK, &list->expr->lock);
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002581 head->prune(list->expr);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002582 HA_RWLOCK_WRUNLOCK(PATEXP_LOCK, &list->expr->lock);
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002583 free(list->expr);
2584 }
2585 free(list);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002586 }
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01002587}
2588
Thierry FOURNIER55d0b102014-01-15 11:25:26 +01002589/* This function lookup for a pattern matching the <key> and return a
2590 * pointer to a pointer of the sample stoarge. If the <key> dont match,
2591 * the function returns NULL. If the key cannot be parsed, the function
2592 * fill <err>.
2593 */
Thierry FOURNIER12ba0c22015-08-14 00:02:11 +02002594struct sample_data **pattern_find_smp(struct pattern_expr *expr, struct pat_ref_elt *ref)
Thierry FOURNIER55d0b102014-01-15 11:25:26 +01002595{
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01002596 struct ebmb_node *node;
2597 struct pattern_tree *elt;
2598 struct pattern_list *pat;
Thierry FOURNIER55d0b102014-01-15 11:25:26 +01002599
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01002600 for (node = ebmb_first(&expr->pattern_tree);
2601 node;
2602 node = ebmb_next(node)) {
2603 elt = container_of(node, struct pattern_tree, node);
2604 if (elt->ref == ref)
Thierry FOURNIER503bb092015-08-19 08:35:43 +02002605 return &elt->data;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01002606 }
2607
2608 for (node = ebmb_first(&expr->pattern_tree_2);
2609 node;
2610 node = ebmb_next(node)) {
2611 elt = container_of(node, struct pattern_tree, node);
2612 if (elt->ref == ref)
Thierry FOURNIER503bb092015-08-19 08:35:43 +02002613 return &elt->data;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01002614 }
2615
2616 list_for_each_entry(pat, &expr->patterns, list)
2617 if (pat->pat.ref == ref)
Thierry FOURNIER503bb092015-08-19 08:35:43 +02002618 return &pat->pat.data;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01002619
2620 return NULL;
Thierry FOURNIER55d0b102014-01-15 11:25:26 +01002621}
2622
Thierry FOURNIERb1136502014-01-15 11:38:49 +01002623/* This function search all the pattern matching the <key> and delete it.
2624 * If the parsing of the input key fails, the function returns 0 and the
2625 * <err> is filled, else return 1;
2626 */
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01002627int pattern_delete(struct pattern_expr *expr, struct pat_ref_elt *ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01002628{
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002629 HA_RWLOCK_WRLOCK(PATEXP_LOCK, &expr->lock);
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01002630 expr->pat_head->delete(expr, ref);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002631 HA_RWLOCK_WRUNLOCK(PATEXP_LOCK, &expr->lock);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01002632 return 1;
2633}
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002634
2635/* This function finalize the configuration parsing. Its set all the
2636 * automatic ids
2637 */
2638void pattern_finalize_config(void)
2639{
2640 int i = 0;
2641 struct pat_ref *ref, *ref2, *ref3;
2642 struct list pr = LIST_HEAD_INIT(pr);
2643
Willy Tarreauf3045d22015-04-29 16:24:50 +02002644 pat_lru_seed = random();
Willy Tarreauf3045d22015-04-29 16:24:50 +02002645
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002646 list_for_each_entry(ref, &pattern_reference, list) {
2647 if (ref->unique_id == -1) {
2648 /* Look for the first free id. */
2649 while (1) {
2650 list_for_each_entry(ref2, &pattern_reference, list) {
2651 if (ref2->unique_id == i) {
2652 i++;
2653 break;
2654 }
2655 }
Willy Tarreau3b786962014-04-26 12:37:25 +02002656 if (&ref2->list == &pattern_reference)
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002657 break;
2658 }
2659
2660 /* Uses the unique id and increment it for the next entry. */
2661 ref->unique_id = i;
2662 i++;
2663 }
2664 }
2665
2666 /* This sort the reference list by id. */
2667 list_for_each_entry_safe(ref, ref2, &pattern_reference, list) {
2668 LIST_DEL(&ref->list);
2669 list_for_each_entry(ref3, &pr, list) {
2670 if (ref->unique_id < ref3->unique_id) {
2671 LIST_ADDQ(&ref3->list, &ref->list);
2672 break;
2673 }
2674 }
2675 if (&ref3->list == &pr)
2676 LIST_ADDQ(&pr, &ref->list);
2677 }
2678
2679 /* swap root */
2680 LIST_ADD(&pr, &pattern_reference);
2681 LIST_DEL(&pr);
2682}
Willy Tarreau403bfbb2019-10-23 06:59:31 +02002683
2684static int pattern_per_thread_lru_alloc()
2685{
2686 if (!global.tune.pattern_cache)
2687 return 1;
2688 pat_lru_tree = lru64_new(global.tune.pattern_cache);
2689 return !!pat_lru_tree;
2690}
2691
2692static void pattern_per_thread_lru_free()
2693{
2694 lru64_destroy(pat_lru_tree);
2695}
2696
2697REGISTER_PER_THREAD_ALLOC(pattern_per_thread_lru_alloc);
2698REGISTER_PER_THREAD_FREE(pattern_per_thread_lru_free);