blob: 3ea1f33d44c03b40894450b53755488281d17bae [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>
Jerome Magninb8bd6d72020-01-17 18:01:20 +010015#include <errno.h>
Thierry FOURNIERed66c292013-11-28 11:05:19 +010016
17#include <common/config.h>
Willy Tarreau296cfd12020-02-25 09:58:41 +010018#include <common/net_helper.h>
Thierry FOURNIERed66c292013-11-28 11:05:19 +010019#include <common/standard.h>
20
21#include <types/global.h>
22#include <types/pattern.h>
23
Thierry FOURNIER46006bd2014-03-21 21:45:15 +010024#include <proto/log.h>
Thierry FOURNIERed66c292013-11-28 11:05:19 +010025#include <proto/pattern.h>
Thierry FOURNIERe3ded592013-12-06 15:36:54 +010026#include <proto/sample.h>
Thierry FOURNIERed66c292013-11-28 11:05:19 +010027
28#include <ebsttree.h>
Willy Tarreauf3045d22015-04-29 16:24:50 +020029#include <import/lru.h>
30#include <import/xxhash.h>
Thierry FOURNIERed66c292013-11-28 11:05:19 +010031
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010032char *pat_match_names[PAT_MATCH_NUM] = {
33 [PAT_MATCH_FOUND] = "found",
34 [PAT_MATCH_BOOL] = "bool",
35 [PAT_MATCH_INT] = "int",
36 [PAT_MATCH_IP] = "ip",
37 [PAT_MATCH_BIN] = "bin",
38 [PAT_MATCH_LEN] = "len",
39 [PAT_MATCH_STR] = "str",
40 [PAT_MATCH_BEG] = "beg",
41 [PAT_MATCH_SUB] = "sub",
42 [PAT_MATCH_DIR] = "dir",
43 [PAT_MATCH_DOM] = "dom",
44 [PAT_MATCH_END] = "end",
45 [PAT_MATCH_REG] = "reg",
Thierry Fournier8feaa662016-02-10 22:55:20 +010046 [PAT_MATCH_REGM] = "regm",
Thierry FOURNIERed66c292013-11-28 11:05:19 +010047};
48
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +020049int (*pat_parse_fcts[PAT_MATCH_NUM])(const char *, struct pattern *, int, char **) = {
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010050 [PAT_MATCH_FOUND] = pat_parse_nothing,
51 [PAT_MATCH_BOOL] = pat_parse_nothing,
52 [PAT_MATCH_INT] = pat_parse_int,
53 [PAT_MATCH_IP] = pat_parse_ip,
54 [PAT_MATCH_BIN] = pat_parse_bin,
Thierry FOURNIER5d344082014-01-27 14:19:53 +010055 [PAT_MATCH_LEN] = pat_parse_int,
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010056 [PAT_MATCH_STR] = pat_parse_str,
57 [PAT_MATCH_BEG] = pat_parse_str,
58 [PAT_MATCH_SUB] = pat_parse_str,
59 [PAT_MATCH_DIR] = pat_parse_str,
60 [PAT_MATCH_DOM] = pat_parse_str,
61 [PAT_MATCH_END] = pat_parse_str,
62 [PAT_MATCH_REG] = pat_parse_reg,
Thierry Fournier8feaa662016-02-10 22:55:20 +010063 [PAT_MATCH_REGM] = pat_parse_reg,
Thierry FOURNIERed66c292013-11-28 11:05:19 +010064};
65
Thierry FOURNIERb9b08462013-12-13 15:12:32 +010066int (*pat_index_fcts[PAT_MATCH_NUM])(struct pattern_expr *, struct pattern *, char **) = {
67 [PAT_MATCH_FOUND] = pat_idx_list_val,
68 [PAT_MATCH_BOOL] = pat_idx_list_val,
69 [PAT_MATCH_INT] = pat_idx_list_val,
70 [PAT_MATCH_IP] = pat_idx_tree_ip,
71 [PAT_MATCH_BIN] = pat_idx_list_ptr,
72 [PAT_MATCH_LEN] = pat_idx_list_val,
73 [PAT_MATCH_STR] = pat_idx_tree_str,
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +020074 [PAT_MATCH_BEG] = pat_idx_tree_pfx,
Thierry FOURNIERb9b08462013-12-13 15:12:32 +010075 [PAT_MATCH_SUB] = pat_idx_list_str,
76 [PAT_MATCH_DIR] = pat_idx_list_str,
77 [PAT_MATCH_DOM] = pat_idx_list_str,
78 [PAT_MATCH_END] = pat_idx_list_str,
79 [PAT_MATCH_REG] = pat_idx_list_reg,
Thierry Fournier8feaa662016-02-10 22:55:20 +010080 [PAT_MATCH_REGM] = pat_idx_list_regm,
Thierry FOURNIERb9b08462013-12-13 15:12:32 +010081};
82
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +010083void (*pat_delete_fcts[PAT_MATCH_NUM])(struct pattern_expr *, struct pat_ref_elt *) = {
Thierry FOURNIERb1136502014-01-15 11:38:49 +010084 [PAT_MATCH_FOUND] = pat_del_list_val,
85 [PAT_MATCH_BOOL] = pat_del_list_val,
86 [PAT_MATCH_INT] = pat_del_list_val,
87 [PAT_MATCH_IP] = pat_del_tree_ip,
88 [PAT_MATCH_BIN] = pat_del_list_ptr,
89 [PAT_MATCH_LEN] = pat_del_list_val,
90 [PAT_MATCH_STR] = pat_del_tree_str,
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +020091 [PAT_MATCH_BEG] = pat_del_tree_str,
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +010092 [PAT_MATCH_SUB] = pat_del_list_ptr,
93 [PAT_MATCH_DIR] = pat_del_list_ptr,
94 [PAT_MATCH_DOM] = pat_del_list_ptr,
95 [PAT_MATCH_END] = pat_del_list_ptr,
Thierry FOURNIERb1136502014-01-15 11:38:49 +010096 [PAT_MATCH_REG] = pat_del_list_reg,
Thierry Fournier8feaa662016-02-10 22:55:20 +010097 [PAT_MATCH_REGM] = pat_del_list_reg,
Thierry FOURNIERb1136502014-01-15 11:38:49 +010098};
99
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +0100100void (*pat_prune_fcts[PAT_MATCH_NUM])(struct pattern_expr *) = {
101 [PAT_MATCH_FOUND] = pat_prune_val,
102 [PAT_MATCH_BOOL] = pat_prune_val,
103 [PAT_MATCH_INT] = pat_prune_val,
104 [PAT_MATCH_IP] = pat_prune_val,
105 [PAT_MATCH_BIN] = pat_prune_ptr,
106 [PAT_MATCH_LEN] = pat_prune_val,
107 [PAT_MATCH_STR] = pat_prune_ptr,
108 [PAT_MATCH_BEG] = pat_prune_ptr,
109 [PAT_MATCH_SUB] = pat_prune_ptr,
110 [PAT_MATCH_DIR] = pat_prune_ptr,
111 [PAT_MATCH_DOM] = pat_prune_ptr,
112 [PAT_MATCH_END] = pat_prune_ptr,
113 [PAT_MATCH_REG] = pat_prune_reg,
Thierry Fournier8feaa662016-02-10 22:55:20 +0100114 [PAT_MATCH_REGM] = pat_prune_reg,
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +0100115};
116
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100117struct pattern *(*pat_match_fcts[PAT_MATCH_NUM])(struct sample *, struct pattern_expr *, int) = {
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100118 [PAT_MATCH_FOUND] = NULL,
119 [PAT_MATCH_BOOL] = pat_match_nothing,
120 [PAT_MATCH_INT] = pat_match_int,
121 [PAT_MATCH_IP] = pat_match_ip,
122 [PAT_MATCH_BIN] = pat_match_bin,
123 [PAT_MATCH_LEN] = pat_match_len,
124 [PAT_MATCH_STR] = pat_match_str,
125 [PAT_MATCH_BEG] = pat_match_beg,
126 [PAT_MATCH_SUB] = pat_match_sub,
127 [PAT_MATCH_DIR] = pat_match_dir,
128 [PAT_MATCH_DOM] = pat_match_dom,
129 [PAT_MATCH_END] = pat_match_end,
130 [PAT_MATCH_REG] = pat_match_reg,
Thierry Fournier8feaa662016-02-10 22:55:20 +0100131 [PAT_MATCH_REGM] = pat_match_regm,
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100132};
133
Thierry FOURNIERe3ded592013-12-06 15:36:54 +0100134/* Just used for checking configuration compatibility */
135int pat_match_types[PAT_MATCH_NUM] = {
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200136 [PAT_MATCH_FOUND] = SMP_T_SINT,
137 [PAT_MATCH_BOOL] = SMP_T_SINT,
138 [PAT_MATCH_INT] = SMP_T_SINT,
Thierry FOURNIERe3ded592013-12-06 15:36:54 +0100139 [PAT_MATCH_IP] = SMP_T_ADDR,
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100140 [PAT_MATCH_BIN] = SMP_T_BIN,
141 [PAT_MATCH_LEN] = SMP_T_STR,
142 [PAT_MATCH_STR] = SMP_T_STR,
143 [PAT_MATCH_BEG] = SMP_T_STR,
144 [PAT_MATCH_SUB] = SMP_T_STR,
145 [PAT_MATCH_DIR] = SMP_T_STR,
146 [PAT_MATCH_DOM] = SMP_T_STR,
147 [PAT_MATCH_END] = SMP_T_STR,
148 [PAT_MATCH_REG] = SMP_T_STR,
Thierry Fournier8feaa662016-02-10 22:55:20 +0100149 [PAT_MATCH_REGM] = SMP_T_STR,
Thierry FOURNIERe3ded592013-12-06 15:36:54 +0100150};
151
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +0100152/* this struct is used to return information */
Emeric Brunb5997f72017-07-03 11:34:05 +0200153static THREAD_LOCAL struct pattern static_pattern;
154static THREAD_LOCAL struct sample_data static_sample_data;
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +0100155
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100156/* This is the root of the list of all pattern_ref avalaibles. */
157struct list pattern_reference = LIST_HEAD_INIT(pattern_reference);
158
Willy Tarreau403bfbb2019-10-23 06:59:31 +0200159static THREAD_LOCAL struct lru64_head *pat_lru_tree;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200160static unsigned long long pat_lru_seed;
161
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100162/*
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100163 *
164 * The following functions are not exported and are used by internals process
165 * of pattern matching
166 *
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100167 */
168
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100169/* Background: Fast way to find a zero byte in a word
170 * http://graphics.stanford.edu/~seander/bithacks.html#ZeroInWord
171 * hasZeroByte = (v - 0x01010101UL) & ~v & 0x80808080UL;
172 *
173 * To look for 4 different byte values, xor the word with those bytes and
174 * then check for zero bytes:
175 *
176 * v = (((unsigned char)c * 0x1010101U) ^ delimiter)
177 * where <delimiter> is the 4 byte values to look for (as an uint)
178 * and <c> is the character that is being tested
179 */
180static inline unsigned int is_delimiter(unsigned char c, unsigned int mask)
181{
182 mask ^= (c * 0x01010101); /* propagate the char to all 4 bytes */
183 return (mask - 0x01010101) & ~mask & 0x80808080U;
184}
185
186static inline unsigned int make_4delim(unsigned char d1, unsigned char d2, unsigned char d3, unsigned char d4)
187{
188 return d1 << 24 | d2 << 16 | d3 << 8 | d4;
189}
190
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100191
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100192/*
193 *
194 * These functions are exported and may be used by any other component.
195 *
Willy Tarreau5def8ef2014-08-29 15:19:33 +0200196 * The following functions are used for parsing pattern matching input value.
197 * The <text> contain the string to be parsed. <pattern> must be a preallocated
198 * pattern. The pat_parse_* functions fill this structure with the parsed value.
199 * <err> is filled with an error message built with memprintf() function. It is
200 * allowed to use a trash as a temporary storage for the returned pattern, as
201 * the next call after these functions will be pat_idx_*.
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100202 *
Willy Tarreau5def8ef2014-08-29 15:19:33 +0200203 * In success case, the pat_parse_* function returns 1. If the function
204 * fails, it returns 0 and <err> is filled.
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100205 */
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100206
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100207/* ignore the current line */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200208int pat_parse_nothing(const char *text, struct pattern *pattern, int mflags, char **err)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100209{
210 return 1;
211}
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100212
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100213/* Parse a string. It is allocated and duplicated. */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200214int pat_parse_str(const char *text, struct pattern *pattern, int mflags, char **err)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100215{
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100216 pattern->type = SMP_T_STR;
Thierry FOURNIERedc15c32013-12-13 15:36:59 +0100217 pattern->ptr.str = (char *)text;
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100218 pattern->len = strlen(text);
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100219 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100220}
221
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100222/* Parse a binary written in hexa. It is allocated. */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200223int pat_parse_bin(const char *text, struct pattern *pattern, int mflags, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100224{
Willy Tarreau83061a82018-07-13 11:56:34 +0200225 struct buffer *trash;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100226
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100227 pattern->type = SMP_T_BIN;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100228 trash = get_trash_chunk();
229 pattern->len = trash->size;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200230 pattern->ptr.str = trash->area;
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100231 return !!parse_binary(text, &pattern->ptr.str, &pattern->len, err);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100232}
233
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100234/* Parse a regex. It is allocated. */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200235int pat_parse_reg(const char *text, struct pattern *pattern, int mflags, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100236{
Thierry FOURNIER0b6d15f2014-01-29 19:35:16 +0100237 pattern->ptr.str = (char *)text;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100238 return 1;
239}
240
241/* Parse a range of positive integers delimited by either ':' or '-'. If only
242 * one integer is read, it is set as both min and max. An operator may be
243 * specified as the prefix, among this list of 5 :
244 *
245 * 0:eq, 1:gt, 2:ge, 3:lt, 4:le
246 *
247 * The default operator is "eq". It supports range matching. Ranges are
248 * rejected for other operators. The operator may be changed at any time.
249 * The operator is stored in the 'opaque' argument.
250 *
251 * If err is non-NULL, an error message will be returned there on errors and
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100252 * the caller will have to free it. The function returns zero on error, and
253 * non-zero on success.
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100254 *
255 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200256int pat_parse_int(const char *text, struct pattern *pattern, int mflags, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100257{
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100258 const char *ptr = text;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100259
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200260 pattern->type = SMP_T_SINT;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100261
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100262 /* Empty string is not valid */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100263 if (!*text)
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100264 goto not_valid_range;
265
266 /* Search ':' or '-' separator. */
267 while (*ptr != '\0' && *ptr != ':' && *ptr != '-')
268 ptr++;
269
270 /* If separator not found. */
271 if (!*ptr) {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100272 if (strl2llrc(text, ptr - text, &pattern->val.range.min) != 0) {
273 memprintf(err, "'%s' is not a number", text);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100274 return 0;
275 }
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100276 pattern->val.range.max = pattern->val.range.min;
277 pattern->val.range.min_set = 1;
278 pattern->val.range.max_set = 1;
279 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100280 }
281
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100282 /* If the separator is the first character. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100283 if (ptr == text && *(ptr + 1) != '\0') {
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100284 if (strl2llrc(ptr + 1, strlen(ptr + 1), &pattern->val.range.max) != 0)
285 goto not_valid_range;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100286
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100287 pattern->val.range.min_set = 0;
288 pattern->val.range.max_set = 1;
289 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100290 }
291
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100292 /* If separator is the last character. */
293 if (*(ptr + 1) == '\0') {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100294 if (strl2llrc(text, ptr - text, &pattern->val.range.min) != 0)
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100295 goto not_valid_range;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100296
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100297 pattern->val.range.min_set = 1;
298 pattern->val.range.max_set = 0;
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100299 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100300 }
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100301
302 /* Else, parse two numbers. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100303 if (strl2llrc(text, ptr - text, &pattern->val.range.min) != 0)
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100304 goto not_valid_range;
305
306 if (strl2llrc(ptr + 1, strlen(ptr + 1), &pattern->val.range.max) != 0)
307 goto not_valid_range;
308
309 if (pattern->val.range.min > pattern->val.range.max)
310 goto not_valid_range;
311
312 pattern->val.range.min_set = 1;
313 pattern->val.range.max_set = 1;
314 return 1;
315
316 not_valid_range:
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100317 memprintf(err, "'%s' is not a valid number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100318 return 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100319}
320
321/* Parse a range of positive 2-component versions delimited by either ':' or
322 * '-'. The version consists in a major and a minor, both of which must be
323 * smaller than 65536, because internally they will be represented as a 32-bit
324 * integer.
325 * If only one version is read, it is set as both min and max. Just like for
326 * pure integers, an operator may be specified as the prefix, among this list
327 * of 5 :
328 *
329 * 0:eq, 1:gt, 2:ge, 3:lt, 4:le
330 *
331 * The default operator is "eq". It supports range matching. Ranges are
332 * rejected for other operators. The operator may be changed at any time.
333 * The operator is stored in the 'opaque' argument. This allows constructs
334 * such as the following one :
335 *
336 * acl obsolete_ssl ssl_req_proto lt 3
337 * acl unsupported_ssl ssl_req_proto gt 3.1
338 * acl valid_ssl ssl_req_proto 3.0-3.1
339 *
340 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200341int pat_parse_dotted_ver(const char *text, struct pattern *pattern, int mflags, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100342{
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100343 const char *ptr = text;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100344
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200345 pattern->type = SMP_T_SINT;
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100346
347 /* Search ':' or '-' separator. */
348 while (*ptr != '\0' && *ptr != ':' && *ptr != '-')
349 ptr++;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100350
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100351 /* If separator not found. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100352 if (*ptr == '\0' && ptr > text) {
353 if (strl2llrc_dotted(text, ptr-text, &pattern->val.range.min) != 0) {
354 memprintf(err, "'%s' is not a dotted number", text);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100355 return 0;
356 }
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100357 pattern->val.range.max = pattern->val.range.min;
358 pattern->val.range.min_set = 1;
359 pattern->val.range.max_set = 1;
360 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100361 }
362
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100363 /* If the separator is the first character. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100364 if (ptr == text && *(ptr+1) != '\0') {
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100365 if (strl2llrc_dotted(ptr+1, strlen(ptr+1), &pattern->val.range.max) != 0) {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100366 memprintf(err, "'%s' is not a valid dotted number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100367 return 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100368 }
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100369 pattern->val.range.min_set = 0;
370 pattern->val.range.max_set = 1;
371 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100372 }
373
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100374 /* If separator is the last character. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100375 if (ptr == &text[strlen(text)-1]) {
376 if (strl2llrc_dotted(text, ptr-text, &pattern->val.range.min) != 0) {
377 memprintf(err, "'%s' is not a valid dotted number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100378 return 0;
379 }
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100380 pattern->val.range.min_set = 1;
381 pattern->val.range.max_set = 0;
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100382 return 1;
383 }
384
385 /* Else, parse two numbers. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100386 if (strl2llrc_dotted(text, ptr-text, &pattern->val.range.min) != 0) {
387 memprintf(err, "'%s' is not a valid dotted number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100388 return 0;
389 }
390 if (strl2llrc_dotted(ptr+1, strlen(ptr+1), &pattern->val.range.max) != 0) {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100391 memprintf(err, "'%s' is not a valid dotted number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100392 return 0;
393 }
394 if (pattern->val.range.min > pattern->val.range.max) {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100395 memprintf(err, "'%s' is not a valid dotted number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100396 return 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100397 }
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100398 pattern->val.range.min_set = 1;
399 pattern->val.range.max_set = 1;
400 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100401}
402
403/* Parse an IP address and an optional mask in the form addr[/mask].
404 * The addr may either be an IPv4 address or a hostname. The mask
405 * may either be a dotted mask or a number of bits. Returns 1 if OK,
406 * otherwise 0. NOTE: IP address patterns are typed (IPV4/IPV6).
407 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200408int pat_parse_ip(const char *text, struct pattern *pattern, int mflags, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100409{
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200410 if (str2net(text, !(mflags & PAT_MF_NO_DNS) && (global.mode & MODE_STARTING),
Thierry FOURNIERfc7ac7b2014-02-11 15:23:04 +0100411 &pattern->val.ipv4.addr, &pattern->val.ipv4.mask)) {
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100412 pattern->type = SMP_T_IPV4;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100413 return 1;
414 }
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100415 else if (str62net(text, &pattern->val.ipv6.addr, &pattern->val.ipv6.mask)) {
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100416 pattern->type = SMP_T_IPV6;
417 return 1;
418 }
419 else {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100420 memprintf(err, "'%s' is not a valid IPv4 or IPv6 address", text);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100421 return 0;
422 }
423}
424
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100425/*
426 *
427 * These functions are exported and may be used by any other component.
428 *
Joseph Herlant4189d672018-11-15 10:22:31 -0800429 * This function just takes a sample <smp> and checks if this sample matches
430 * with the pattern <pattern>. This function returns only PAT_MATCH or
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100431 * PAT_NOMATCH.
432 *
433 */
434
435/* always return false */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100436struct pattern *pat_match_nothing(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100437{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200438 if (smp->data.u.sint) {
Thierry FOURNIERe5978bf2014-03-17 19:53:10 +0100439 if (fill) {
Thierry FOURNIER503bb092015-08-19 08:35:43 +0200440 static_pattern.data = NULL;
Thierry FOURNIERe5978bf2014-03-17 19:53:10 +0100441 static_pattern.ref = NULL;
Thierry FOURNIERe5978bf2014-03-17 19:53:10 +0100442 static_pattern.type = 0;
443 static_pattern.ptr.str = NULL;
444 }
445 return &static_pattern;
446 }
447 else
448 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100449}
450
451
Joseph Herlant4189d672018-11-15 10:22:31 -0800452/* NB: For two strings to be identical, it is required that their length match */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100453struct pattern *pat_match_str(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100454{
455 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100456 struct ebmb_node *node;
457 char prev;
458 struct pattern_tree *elt;
459 struct pattern_list *lst;
460 struct pattern *pattern;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200461 struct pattern *ret = NULL;
462 struct lru64 *lru = NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100463
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100464 /* Lookup a string in the expression's pattern tree. */
465 if (!eb_is_empty(&expr->pattern_tree)) {
466 /* we may have to force a trailing zero on the test pattern */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200467 prev = smp->data.u.str.area[smp->data.u.str.data];
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100468 if (prev)
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200469 smp->data.u.str.area[smp->data.u.str.data] = '\0';
470 node = ebst_lookup(&expr->pattern_tree, smp->data.u.str.area);
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100471 if (prev)
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200472 smp->data.u.str.area[smp->data.u.str.data] = prev;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100473
474 if (node) {
475 if (fill) {
476 elt = ebmb_entry(node, struct pattern_tree, node);
Thierry FOURNIER503bb092015-08-19 08:35:43 +0200477 static_pattern.data = elt->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +0100478 static_pattern.ref = elt->ref;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200479 static_pattern.sflags = PAT_SF_TREE;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100480 static_pattern.type = SMP_T_STR;
481 static_pattern.ptr.str = (char *)elt->node.key;
482 }
483 return &static_pattern;
484 }
485 }
486
487 /* look in the list */
Willy Tarreauf3045d22015-04-29 16:24:50 +0200488 if (pat_lru_tree) {
Willy Tarreauaee93142015-05-04 17:18:42 +0200489 unsigned long long seed = pat_lru_seed ^ (long)expr;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200490
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200491 lru = lru64_get(XXH64(smp->data.u.str.area, smp->data.u.str.data, seed),
Willy Tarreauf3045d22015-04-29 16:24:50 +0200492 pat_lru_tree, expr, expr->revision);
Willy Tarreau403bfbb2019-10-23 06:59:31 +0200493 if (lru && lru->domain) {
Emeric Brunb5997f72017-07-03 11:34:05 +0200494 ret = lru->data;
Emeric Brunb5997f72017-07-03 11:34:05 +0200495 return ret;
496 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200497 }
498
Emeric Brunb5997f72017-07-03 11:34:05 +0200499
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100500 list_for_each_entry(lst, &expr->patterns, list) {
501 pattern = &lst->pat;
502
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200503 if (pattern->len != smp->data.u.str.data)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100504 continue;
505
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200506 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200507 if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.str.area, smp->data.u.str.data) == 0) ||
508 (!icase && strncmp(pattern->ptr.str, smp->data.u.str.area, smp->data.u.str.data) == 0)) {
Willy Tarreauf3045d22015-04-29 16:24:50 +0200509 ret = pattern;
510 break;
511 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100512 }
513
Willy Tarreau403bfbb2019-10-23 06:59:31 +0200514 if (lru)
Emeric Brunb5997f72017-07-03 11:34:05 +0200515 lru64_commit(lru, ret, expr, expr->revision, NULL);
Willy Tarreauf3045d22015-04-29 16:24:50 +0200516
517 return ret;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100518}
519
520/* NB: For two binaries buf to be identical, it is required that their lengths match */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100521struct pattern *pat_match_bin(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100522{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100523 struct pattern_list *lst;
524 struct pattern *pattern;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200525 struct pattern *ret = NULL;
526 struct lru64 *lru = NULL;
527
528 if (pat_lru_tree) {
Willy Tarreauaee93142015-05-04 17:18:42 +0200529 unsigned long long seed = pat_lru_seed ^ (long)expr;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100530
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200531 lru = lru64_get(XXH64(smp->data.u.str.area, smp->data.u.str.data, seed),
Willy Tarreauf3045d22015-04-29 16:24:50 +0200532 pat_lru_tree, expr, expr->revision);
Willy Tarreau403bfbb2019-10-23 06:59:31 +0200533 if (lru && lru->domain) {
Emeric Brunb5997f72017-07-03 11:34:05 +0200534 ret = lru->data;
Emeric Brunb5997f72017-07-03 11:34:05 +0200535 return ret;
536 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200537 }
538
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100539 list_for_each_entry(lst, &expr->patterns, list) {
540 pattern = &lst->pat;
541
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200542 if (pattern->len != smp->data.u.str.data)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100543 continue;
544
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200545 if (memcmp(pattern->ptr.str, smp->data.u.str.area, smp->data.u.str.data) == 0) {
Willy Tarreauf3045d22015-04-29 16:24:50 +0200546 ret = pattern;
547 break;
548 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100549 }
550
Willy Tarreau403bfbb2019-10-23 06:59:31 +0200551 if (lru)
Emeric Brunb5997f72017-07-03 11:34:05 +0200552 lru64_commit(lru, ret, expr, expr->revision, NULL);
Willy Tarreauf3045d22015-04-29 16:24:50 +0200553
554 return ret;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100555}
556
557/* Executes a regex. It temporarily changes the data to add a trailing zero,
Thierry Fournier8feaa662016-02-10 22:55:20 +0100558 * and restores the previous character when leaving. This function fills
559 * a matching array.
560 */
561struct pattern *pat_match_regm(struct sample *smp, struct pattern_expr *expr, int fill)
562{
563 struct pattern_list *lst;
564 struct pattern *pattern;
565 struct pattern *ret = NULL;
566
567 list_for_each_entry(lst, &expr->patterns, list) {
568 pattern = &lst->pat;
569
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200570 if (regex_exec_match2(pattern->ptr.reg, smp->data.u.str.area, smp->data.u.str.data,
Thierry Fournier8feaa662016-02-10 22:55:20 +0100571 MAX_MATCH, pmatch, 0)) {
572 ret = pattern;
573 smp->ctx.a[0] = pmatch;
574 break;
575 }
576 }
577
578 return ret;
579}
580
581/* Executes a regex. It temporarily changes the data to add a trailing zero,
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100582 * and restores the previous character when leaving.
583 */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100584struct pattern *pat_match_reg(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100585{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100586 struct pattern_list *lst;
587 struct pattern *pattern;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200588 struct pattern *ret = NULL;
589 struct lru64 *lru = NULL;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100590
Willy Tarreauf3045d22015-04-29 16:24:50 +0200591 if (pat_lru_tree) {
Willy Tarreauaee93142015-05-04 17:18:42 +0200592 unsigned long long seed = pat_lru_seed ^ (long)expr;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200593
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200594 lru = lru64_get(XXH64(smp->data.u.str.area, smp->data.u.str.data, seed),
Willy Tarreauf3045d22015-04-29 16:24:50 +0200595 pat_lru_tree, expr, expr->revision);
Willy Tarreau403bfbb2019-10-23 06:59:31 +0200596 if (lru && lru->domain) {
Emeric Brunb5997f72017-07-03 11:34:05 +0200597 ret = lru->data;
Emeric Brunb5997f72017-07-03 11:34:05 +0200598 return ret;
599 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200600 }
601
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100602 list_for_each_entry(lst, &expr->patterns, list) {
603 pattern = &lst->pat;
604
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200605 if (regex_exec2(pattern->ptr.reg, smp->data.u.str.area, smp->data.u.str.data)) {
Willy Tarreauf3045d22015-04-29 16:24:50 +0200606 ret = pattern;
607 break;
608 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100609 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200610
Willy Tarreau403bfbb2019-10-23 06:59:31 +0200611 if (lru)
Emeric Brunb5997f72017-07-03 11:34:05 +0200612 lru64_commit(lru, ret, expr, expr->revision, NULL);
Willy Tarreauf3045d22015-04-29 16:24:50 +0200613
614 return ret;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100615}
616
617/* Checks that the pattern matches the beginning of the tested string. */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100618struct pattern *pat_match_beg(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100619{
620 int icase;
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +0200621 struct ebmb_node *node;
622 char prev;
623 struct pattern_tree *elt;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100624 struct pattern_list *lst;
625 struct pattern *pattern;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200626 struct pattern *ret = NULL;
627 struct lru64 *lru = NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100628
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +0200629 /* Lookup a string in the expression's pattern tree. */
630 if (!eb_is_empty(&expr->pattern_tree)) {
631 /* we may have to force a trailing zero on the test pattern */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200632 prev = smp->data.u.str.area[smp->data.u.str.data];
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +0200633 if (prev)
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200634 smp->data.u.str.area[smp->data.u.str.data] = '\0';
635 node = ebmb_lookup_longest(&expr->pattern_tree,
636 smp->data.u.str.area);
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +0200637 if (prev)
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200638 smp->data.u.str.area[smp->data.u.str.data] = prev;
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +0200639
640 if (node) {
641 if (fill) {
642 elt = ebmb_entry(node, struct pattern_tree, node);
Thierry FOURNIER503bb092015-08-19 08:35:43 +0200643 static_pattern.data = elt->data;
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +0200644 static_pattern.ref = elt->ref;
645 static_pattern.sflags = PAT_SF_TREE;
646 static_pattern.type = SMP_T_STR;
647 static_pattern.ptr.str = (char *)elt->node.key;
648 }
649 return &static_pattern;
650 }
651 }
652
653 /* look in the list */
Willy Tarreauf3045d22015-04-29 16:24:50 +0200654 if (pat_lru_tree) {
Willy Tarreauaee93142015-05-04 17:18:42 +0200655 unsigned long long seed = pat_lru_seed ^ (long)expr;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200656
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200657 lru = lru64_get(XXH64(smp->data.u.str.area, smp->data.u.str.data, seed),
Willy Tarreauf3045d22015-04-29 16:24:50 +0200658 pat_lru_tree, expr, expr->revision);
Willy Tarreau403bfbb2019-10-23 06:59:31 +0200659 if (lru && lru->domain) {
Emeric Brunb5997f72017-07-03 11:34:05 +0200660 ret = lru->data;
Emeric Brunb5997f72017-07-03 11:34:05 +0200661 return ret;
662 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200663 }
664
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100665 list_for_each_entry(lst, &expr->patterns, list) {
666 pattern = &lst->pat;
667
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200668 if (pattern->len > smp->data.u.str.data)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100669 continue;
670
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200671 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200672 if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.str.area, pattern->len) != 0) ||
673 (!icase && strncmp(pattern->ptr.str, smp->data.u.str.area, pattern->len) != 0))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100674 continue;
675
Willy Tarreauf3045d22015-04-29 16:24:50 +0200676 ret = pattern;
677 break;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100678 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200679
Willy Tarreau403bfbb2019-10-23 06:59:31 +0200680 if (lru)
Emeric Brunb5997f72017-07-03 11:34:05 +0200681 lru64_commit(lru, ret, expr, expr->revision, NULL);
Willy Tarreauf3045d22015-04-29 16:24:50 +0200682
683 return ret;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100684}
685
686/* Checks that the pattern matches the end of the tested string. */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100687struct pattern *pat_match_end(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100688{
689 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100690 struct pattern_list *lst;
691 struct pattern *pattern;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200692 struct pattern *ret = NULL;
693 struct lru64 *lru = NULL;
694
695 if (pat_lru_tree) {
Willy Tarreauaee93142015-05-04 17:18:42 +0200696 unsigned long long seed = pat_lru_seed ^ (long)expr;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200697
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200698 lru = lru64_get(XXH64(smp->data.u.str.area, smp->data.u.str.data, seed),
Willy Tarreauf3045d22015-04-29 16:24:50 +0200699 pat_lru_tree, expr, expr->revision);
Willy Tarreau403bfbb2019-10-23 06:59:31 +0200700 if (lru && lru->domain) {
Emeric Brunb5997f72017-07-03 11:34:05 +0200701 ret = lru->data;
Emeric Brunb5997f72017-07-03 11:34:05 +0200702 return ret;
703 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200704 }
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100705
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100706 list_for_each_entry(lst, &expr->patterns, list) {
707 pattern = &lst->pat;
708
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200709 if (pattern->len > smp->data.u.str.data)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100710 continue;
711
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200712 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200713 if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.str.area + smp->data.u.str.data - pattern->len, pattern->len) != 0) ||
714 (!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 +0100715 continue;
716
Willy Tarreauf3045d22015-04-29 16:24:50 +0200717 ret = pattern;
718 break;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100719 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200720
Willy Tarreau403bfbb2019-10-23 06:59:31 +0200721 if (lru)
Emeric Brunb5997f72017-07-03 11:34:05 +0200722 lru64_commit(lru, ret, expr, expr->revision, NULL);
Willy Tarreauf3045d22015-04-29 16:24:50 +0200723
724 return ret;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100725}
726
727/* Checks that the pattern is included inside the tested string.
728 * NB: Suboptimal, should be rewritten using a Boyer-Moore method.
729 */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100730struct pattern *pat_match_sub(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100731{
732 int icase;
733 char *end;
734 char *c;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100735 struct pattern_list *lst;
736 struct pattern *pattern;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200737 struct pattern *ret = NULL;
738 struct lru64 *lru = NULL;
739
740 if (pat_lru_tree) {
Willy Tarreauaee93142015-05-04 17:18:42 +0200741 unsigned long long seed = pat_lru_seed ^ (long)expr;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200742
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200743 lru = lru64_get(XXH64(smp->data.u.str.area, smp->data.u.str.data, seed),
Willy Tarreauf3045d22015-04-29 16:24:50 +0200744 pat_lru_tree, expr, expr->revision);
Willy Tarreau403bfbb2019-10-23 06:59:31 +0200745 if (lru && lru->domain) {
Emeric Brunb5997f72017-07-03 11:34:05 +0200746 ret = lru->data;
Emeric Brunb5997f72017-07-03 11:34:05 +0200747 return ret;
748 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200749 }
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100750
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100751 list_for_each_entry(lst, &expr->patterns, list) {
752 pattern = &lst->pat;
753
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200754 if (pattern->len > smp->data.u.str.data)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100755 continue;
756
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200757 end = smp->data.u.str.area + smp->data.u.str.data - pattern->len;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200758 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100759 if (icase) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200760 for (c = smp->data.u.str.area; c <= end; c++) {
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100761 if (tolower(*c) != tolower(*pattern->ptr.str))
762 continue;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200763 if (strncasecmp(pattern->ptr.str, c, pattern->len) == 0) {
764 ret = pattern;
765 goto leave;
766 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100767 }
768 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200769 for (c = smp->data.u.str.area; c <= end; c++) {
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100770 if (*c != *pattern->ptr.str)
771 continue;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200772 if (strncmp(pattern->ptr.str, c, pattern->len) == 0) {
773 ret = pattern;
774 goto leave;
775 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100776 }
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100777 }
778 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200779 leave:
Willy Tarreau403bfbb2019-10-23 06:59:31 +0200780 if (lru)
Emeric Brunb5997f72017-07-03 11:34:05 +0200781 lru64_commit(lru, ret, expr, expr->revision, NULL);
Willy Tarreauf3045d22015-04-29 16:24:50 +0200782
783 return ret;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100784}
785
786/* This one is used by other real functions. It checks that the pattern is
787 * included inside the tested string, but enclosed between the specified
788 * delimiters or at the beginning or end of the string. The delimiters are
789 * provided as an unsigned int made by make_4delim() and match up to 4 different
790 * delimiters. Delimiters are stripped at the beginning and end of the pattern.
791 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200792static int match_word(struct sample *smp, struct pattern *pattern, int mflags, unsigned int delimiters)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100793{
794 int may_match, icase;
795 char *c, *end;
796 char *ps;
797 int pl;
798
799 pl = pattern->len;
800 ps = pattern->ptr.str;
801
802 while (pl > 0 && is_delimiter(*ps, delimiters)) {
803 pl--;
804 ps++;
805 }
806
807 while (pl > 0 && is_delimiter(ps[pl - 1], delimiters))
808 pl--;
809
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200810 if (pl > smp->data.u.str.data)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100811 return PAT_NOMATCH;
812
813 may_match = 1;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200814 icase = mflags & PAT_MF_IGNORE_CASE;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200815 end = smp->data.u.str.area + smp->data.u.str.data - pl;
816 for (c = smp->data.u.str.area; c <= end; c++) {
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100817 if (is_delimiter(*c, delimiters)) {
818 may_match = 1;
819 continue;
820 }
821
822 if (!may_match)
823 continue;
824
825 if (icase) {
826 if ((tolower(*c) == tolower(*ps)) &&
827 (strncasecmp(ps, c, pl) == 0) &&
828 (c == end || is_delimiter(c[pl], delimiters)))
829 return PAT_MATCH;
830 } else {
831 if ((*c == *ps) &&
832 (strncmp(ps, c, pl) == 0) &&
833 (c == end || is_delimiter(c[pl], delimiters)))
834 return PAT_MATCH;
835 }
836 may_match = 0;
837 }
838 return PAT_NOMATCH;
839}
840
841/* Checks that the pattern is included inside the tested string, but enclosed
842 * between the delimiters '?' or '/' or at the beginning or end of the string.
843 * Delimiters at the beginning or end of the pattern are ignored.
844 */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100845struct pattern *pat_match_dir(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100846{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100847 struct pattern_list *lst;
848 struct pattern *pattern;
849
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100850 list_for_each_entry(lst, &expr->patterns, list) {
851 pattern = &lst->pat;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200852 if (match_word(smp, pattern, expr->mflags, make_4delim('/', '?', '?', '?')))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100853 return pattern;
854 }
855 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100856}
857
858/* Checks that the pattern is included inside the tested string, but enclosed
859 * between the delmiters '/', '?', '.' or ":" or at the beginning or end of
860 * the string. Delimiters at the beginning or end of the pattern are ignored.
861 */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100862struct pattern *pat_match_dom(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100863{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100864 struct pattern_list *lst;
865 struct pattern *pattern;
866
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100867 list_for_each_entry(lst, &expr->patterns, list) {
868 pattern = &lst->pat;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200869 if (match_word(smp, pattern, expr->mflags, make_4delim('/', '?', '.', ':')))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100870 return pattern;
871 }
872 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100873}
874
875/* Checks that the integer in <test> is included between min and max */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100876struct pattern *pat_match_int(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100877{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100878 struct pattern_list *lst;
879 struct pattern *pattern;
880
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100881 list_for_each_entry(lst, &expr->patterns, list) {
882 pattern = &lst->pat;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200883 if ((!pattern->val.range.min_set || pattern->val.range.min <= smp->data.u.sint) &&
884 (!pattern->val.range.max_set || smp->data.u.sint <= pattern->val.range.max))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100885 return pattern;
886 }
887 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100888}
889
890/* Checks that the length of the pattern in <test> is included between min and max */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100891struct pattern *pat_match_len(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100892{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100893 struct pattern_list *lst;
894 struct pattern *pattern;
895
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100896 list_for_each_entry(lst, &expr->patterns, list) {
897 pattern = &lst->pat;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200898 if ((!pattern->val.range.min_set || pattern->val.range.min <= smp->data.u.str.data) &&
899 (!pattern->val.range.max_set || smp->data.u.str.data <= pattern->val.range.max))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100900 return pattern;
901 }
902 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100903}
904
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100905struct pattern *pat_match_ip(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100906{
907 unsigned int v4; /* in network byte order */
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100908 struct in6_addr tmp6;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100909 struct in_addr *s;
910 struct ebmb_node *node;
911 struct pattern_tree *elt;
912 struct pattern_list *lst;
913 struct pattern *pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100914
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100915 /* The input sample is IPv4. Try to match in the trees. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200916 if (smp->data.type == SMP_T_IPV4) {
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100917 /* Lookup an IPv4 address in the expression's pattern tree using
918 * the longest match method.
919 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200920 s = &smp->data.u.ipv4;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100921 node = ebmb_lookup_longest(&expr->pattern_tree, &s->s_addr);
922 if (node) {
923 if (fill) {
924 elt = ebmb_entry(node, struct pattern_tree, node);
Thierry FOURNIER503bb092015-08-19 08:35:43 +0200925 static_pattern.data = elt->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +0100926 static_pattern.ref = elt->ref;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200927 static_pattern.sflags = PAT_SF_TREE;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100928 static_pattern.type = SMP_T_IPV4;
Willy Tarreau296cfd12020-02-25 09:58:41 +0100929 static_pattern.val.ipv4.addr.s_addr = read_u32(elt->node.key);
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100930 if (!cidr2dotted(elt->node.node.pfx, &static_pattern.val.ipv4.mask))
931 return NULL;
932 }
933 return &static_pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100934 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100935
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100936 /* The IPv4 sample dont match the IPv4 tree. Convert the IPv4
937 * sample address to IPv6 with the mapping method using the ::ffff:
938 * prefix, and try to lookup in the IPv6 tree.
939 */
940 memset(&tmp6, 0, 10);
Willy Tarreau296cfd12020-02-25 09:58:41 +0100941 write_u16(&tmp6.s6_addr[10], htons(0xffff));
942 write_u32(&tmp6.s6_addr[12], smp->data.u.ipv4.s_addr);
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100943 node = ebmb_lookup_longest(&expr->pattern_tree_2, &tmp6);
944 if (node) {
945 if (fill) {
946 elt = ebmb_entry(node, struct pattern_tree, node);
Thierry FOURNIER503bb092015-08-19 08:35:43 +0200947 static_pattern.data = elt->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +0100948 static_pattern.ref = elt->ref;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200949 static_pattern.sflags = PAT_SF_TREE;
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100950 static_pattern.type = SMP_T_IPV6;
Willy Tarreau296cfd12020-02-25 09:58:41 +0100951 memcpy(&static_pattern.val.ipv6.addr, elt->node.key, 16);
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100952 static_pattern.val.ipv6.mask = elt->node.node.pfx;
953 }
954 return &static_pattern;
955 }
956 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100957
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100958 /* The input sample is IPv6. Try to match in the trees. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200959 if (smp->data.type == SMP_T_IPV6) {
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100960 /* Lookup an IPv6 address in the expression's pattern tree using
961 * the longest match method.
962 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200963 node = ebmb_lookup_longest(&expr->pattern_tree_2, &smp->data.u.ipv6);
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100964 if (node) {
965 if (fill) {
966 elt = ebmb_entry(node, struct pattern_tree, node);
Thierry FOURNIER503bb092015-08-19 08:35:43 +0200967 static_pattern.data = elt->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +0100968 static_pattern.ref = elt->ref;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200969 static_pattern.sflags = PAT_SF_TREE;
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100970 static_pattern.type = SMP_T_IPV6;
Willy Tarreau296cfd12020-02-25 09:58:41 +0100971 memcpy(&static_pattern.val.ipv6.addr, elt->node.key, 16);
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100972 static_pattern.val.ipv6.mask = elt->node.node.pfx;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100973 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100974 return &static_pattern;
975 }
976
977 /* Try to convert 6 to 4 when the start of the ipv6 address match the
978 * following forms :
979 * - ::ffff:ip:v4 (ipv4 mapped)
980 * - ::0000:ip:v4 (old ipv4 mapped)
981 * - 2002:ip:v4:: (6to4)
982 */
Willy Tarreau296cfd12020-02-25 09:58:41 +0100983 if ((read_u64(&smp->data.u.ipv6.s6_addr[0]) == 0 &&
984 (read_u32(&smp->data.u.ipv6.s6_addr[8]) == 0 ||
985 read_u32(&smp->data.u.ipv6.s6_addr[8]) == htonl(0xFFFF))) ||
986 read_u16(&smp->data.u.ipv6.s6_addr[0]) == htons(0x2002)) {
987 if (read_u32(&smp->data.u.ipv6.s6_addr[0]) == 0)
988 v4 = read_u32(&smp->data.u.ipv6.s6_addr[12]);
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100989 else
Willy Tarreau296cfd12020-02-25 09:58:41 +0100990 v4 = htonl((ntohs(read_u16(&smp->data.u.ipv6.s6_addr[2])) << 16) +
991 ntohs(read_u16(&smp->data.u.ipv6.s6_addr[4])));
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100992
993 /* Lookup an IPv4 address in the expression's pattern tree using the longest
994 * match method.
995 */
996 node = ebmb_lookup_longest(&expr->pattern_tree, &v4);
997 if (node) {
998 if (fill) {
999 elt = ebmb_entry(node, struct pattern_tree, node);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001000 static_pattern.data = elt->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01001001 static_pattern.ref = elt->ref;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02001002 static_pattern.sflags = PAT_SF_TREE;
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001003 static_pattern.type = SMP_T_IPV4;
Willy Tarreau296cfd12020-02-25 09:58:41 +01001004 static_pattern.val.ipv4.addr.s_addr = read_u32(elt->node.key);
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001005 if (!cidr2dotted(elt->node.node.pfx, &static_pattern.val.ipv4.mask))
1006 return NULL;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01001007 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001008 return &static_pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +01001009 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001010 }
1011 }
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +01001012
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001013 /* Lookup in the list. the list contain only IPv4 patterns */
1014 list_for_each_entry(lst, &expr->patterns, list) {
1015 pattern = &lst->pat;
1016
1017 /* The input sample is IPv4, use it as is. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001018 if (smp->data.type == SMP_T_IPV4) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001019 v4 = smp->data.u.ipv4.s_addr;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +01001020 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001021 else if (smp->data.type == SMP_T_IPV6) {
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001022 /* v4 match on a V6 sample. We want to check at least for
1023 * the following forms :
1024 * - ::ffff:ip:v4 (ipv4 mapped)
1025 * - ::0000:ip:v4 (old ipv4 mapped)
1026 * - 2002:ip:v4:: (6to4)
1027 */
Willy Tarreau296cfd12020-02-25 09:58:41 +01001028 if (read_u64(&smp->data.u.ipv6.s6_addr[0]) == 0 &&
1029 (read_u32(&smp->data.u.ipv6.s6_addr[8]) == 0 ||
1030 read_u32(&smp->data.u.ipv6.s6_addr[8]) == htonl(0xFFFF))) {
1031 v4 = read_u32(&smp->data.u.ipv6.s6_addr[12]);
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01001032 }
Willy Tarreau296cfd12020-02-25 09:58:41 +01001033 else if (read_u16(&smp->data.u.ipv6.s6_addr[0]) == htons(0x2002)) {
1034 v4 = htonl((ntohs(read_u16(&smp->data.u.ipv6.s6_addr[2])) << 16) +
1035 ntohs(read_u16(&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
Jerome Magnin3c79d4b2020-01-17 16:09:33 +01002331 if (ferror(file)) {
2332 memprintf(err, "error encountered while reading <%s> : %s",
2333 filename, strerror(errno));
2334 goto out_close;
2335 }
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002336 /* succes */
2337 ret = 1;
2338
2339 out_close:
2340 fclose(file);
2341 return ret;
2342}
2343
2344/* Reads patterns from a file. If <err_msg> is non-NULL, an error message will
2345 * be returned there on errors and the caller will have to free it.
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002346 */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002347int pat_ref_read_from_file(struct pat_ref *ref, const char *filename, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002348{
2349 FILE *file;
2350 char *c;
2351 char *arg;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002352 int ret = 0;
2353 int line = 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002354
2355 file = fopen(filename, "r");
2356 if (!file) {
2357 memprintf(err, "failed to open pattern file <%s>", filename);
2358 return 0;
2359 }
2360
2361 /* now parse all patterns. The file may contain only one pattern per
2362 * line. If the line contains spaces, they will be part of the pattern.
2363 * The pattern stops at the first CR, LF or EOF encountered.
2364 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002365 while (fgets(trash.area, trash.size, file) != NULL) {
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002366 line++;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002367 c = trash.area;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002368
2369 /* ignore lines beginning with a dash */
2370 if (*c == '#')
2371 continue;
2372
2373 /* strip leading spaces and tabs */
2374 while (*c == ' ' || *c == '\t')
2375 c++;
2376
2377
2378 arg = c;
2379 while (*c && *c != '\n' && *c != '\r')
2380 c++;
2381 *c = 0;
2382
2383 /* empty lines are ignored too */
2384 if (c == arg)
2385 continue;
2386
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002387 if (!pat_ref_append(ref, arg, NULL, line)) {
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002388 memprintf(err, "out of memory when loading patterns from file <%s>", filename);
2389 goto out_close;
2390 }
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002391 }
2392
Jerome Magnin3c79d4b2020-01-17 16:09:33 +01002393 if (ferror(file)) {
2394 memprintf(err, "error encountered while reading <%s> : %s",
2395 filename, strerror(errno));
2396 goto out_close;
2397 }
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002398 ret = 1; /* success */
2399
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002400 out_close:
2401 fclose(file);
2402 return ret;
2403}
2404
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002405int pattern_read_from_file(struct pattern_head *head, unsigned int refflags,
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002406 const char *filename, int patflags, int load_smp,
Thierry FOURNIER94580c92014-02-11 14:36:45 +01002407 char **err, const char *file, int line)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002408{
2409 struct pat_ref *ref;
2410 struct pattern_expr *expr;
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002411 struct pat_ref_elt *elt;
Willy Tarreau4deaf392014-11-26 13:17:03 +01002412 int reuse = 0;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002413
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002414 /* Lookup for the existing reference. */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002415 ref = pat_ref_lookup(filename);
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002416
2417 /* If the reference doesn't exists, create it and load associated file. */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002418 if (!ref) {
Thierry FOURNIER94580c92014-02-11 14:36:45 +01002419 chunk_printf(&trash,
2420 "pattern loaded from file '%s' used by %s at file '%s' line %d",
2421 filename, refflags & PAT_REF_MAP ? "map" : "acl", file, line);
2422
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002423 ref = pat_ref_new(filename, trash.area, refflags);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002424 if (!ref) {
2425 memprintf(err, "out of memory");
2426 return 0;
2427 }
2428
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002429 if (load_smp) {
Thierry FOURNIERc0bd9102014-01-29 12:32:58 +01002430 ref->flags |= PAT_REF_SMP;
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002431 if (!pat_ref_read_from_file_smp(ref, filename, err))
2432 return 0;
2433 }
2434 else {
2435 if (!pat_ref_read_from_file(ref, filename, err))
2436 return 0;
2437 }
2438 }
2439 else {
Thierry FOURNIERc0bd9102014-01-29 12:32:58 +01002440 /* The reference already exists, check the map compatibility. */
2441
2442 /* If the load require samples and the flag PAT_REF_SMP is not set,
2443 * the reference doesn't contain sample, and cannot be used.
2444 */
2445 if (load_smp) {
2446 if (!(ref->flags & PAT_REF_SMP)) {
2447 memprintf(err, "The file \"%s\" is already used as one column file "
2448 "and cannot be used by as two column file.",
2449 filename);
2450 return 0;
2451 }
2452 }
2453 else {
2454 /* The load doesn't require samples. If the flag PAT_REF_SMP is
2455 * set, the reference contains a sample, and cannot be used.
2456 */
2457 if (ref->flags & PAT_REF_SMP) {
2458 memprintf(err, "The file \"%s\" is already used as two column file "
2459 "and cannot be used by as one column file.",
2460 filename);
2461 return 0;
2462 }
2463 }
2464
Thierry FOURNIER94580c92014-02-11 14:36:45 +01002465 /* Extends display */
2466 chunk_printf(&trash, "%s", ref->display);
2467 chunk_appendf(&trash, ", by %s at file '%s' line %d",
2468 refflags & PAT_REF_MAP ? "map" : "acl", file, line);
2469 free(ref->display);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002470 ref->display = strdup(trash.area);
Thierry FOURNIER94580c92014-02-11 14:36:45 +01002471 if (!ref->display) {
2472 memprintf(err, "out of memory");
2473 return 0;
2474 }
2475
Thierry FOURNIERc0bd9102014-01-29 12:32:58 +01002476 /* Merge flags. */
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002477 ref->flags |= refflags;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002478 }
2479
2480 /* Now, we can loading patterns from the reference. */
2481
2482 /* Lookup for existing reference in the head. If the reference
2483 * doesn't exists, create it.
2484 */
2485 expr = pattern_lookup_expr(head, ref);
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02002486 if (!expr || (expr->mflags != patflags)) {
Emeric Brun7d27f3c2017-07-03 17:54:23 +02002487 expr = pattern_new_expr(head, ref, patflags, err, &reuse);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002488 if (!expr)
2489 return 0;
2490 }
2491
Thierry FOURNIER315ec422014-11-24 11:14:42 +01002492 /* The returned expression may be not empty, because the function
2493 * "pattern_new_expr" lookup for similar pattern list and can
2494 * reuse a already filled pattern list. In this case, we can not
2495 * reload the patterns.
2496 */
2497 if (reuse)
2498 return 1;
2499
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002500 /* Load reference content in the pattern expression. */
2501 list_for_each_entry(elt, &ref->head, list) {
2502 if (!pat_ref_push(elt, expr, patflags, err)) {
2503 if (elt->line > 0)
2504 memprintf(err, "%s at line %d of file '%s'",
2505 *err, elt->line, filename);
2506 return 0;
2507 }
2508 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002509
2510 return 1;
2511}
2512
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01002513/* This function executes a pattern match on a sample. It applies pattern <expr>
2514 * to sample <smp>. The function returns NULL if the sample dont match. It returns
2515 * non-null if the sample match. If <fill> is true and the sample match, the
2516 * function returns the matched pattern. In many cases, this pattern can be a
2517 * static buffer.
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002518 */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002519struct pattern *pattern_exec_match(struct pattern_head *head, struct sample *smp, int fill)
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002520{
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002521 struct pattern_expr_list *list;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002522 struct pattern *pat;
2523
2524 if (!head->match) {
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01002525 if (fill) {
Thierry FOURNIER503bb092015-08-19 08:35:43 +02002526 static_pattern.data = NULL;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01002527 static_pattern.ref = NULL;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02002528 static_pattern.sflags = 0;
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002529 static_pattern.type = SMP_T_SINT;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01002530 static_pattern.val.i = 1;
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01002531 }
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01002532 return &static_pattern;
2533 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002534
Thierry FOURNIER5d344082014-01-27 14:19:53 +01002535 /* convert input to string */
2536 if (!sample_convert(smp, head->expect_type))
2537 return NULL;
2538
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002539 list_for_each_entry(list, &head->head, list) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002540 HA_RWLOCK_RDLOCK(PATEXP_LOCK, &list->expr->lock);
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002541 pat = head->match(smp, list->expr, fill);
Emeric Brunb5997f72017-07-03 11:34:05 +02002542 if (pat) {
2543 /* We duplicate the pattern cause it could be modified
2544 by another thread */
2545 if (pat != &static_pattern) {
2546 memcpy(&static_pattern, pat, sizeof(struct pattern));
2547 pat = &static_pattern;
2548 }
2549
2550 /* We also duplicate the sample data for
2551 same reason */
2552 if (pat->data && (pat->data != &static_sample_data)) {
Christopher Faulet09fdf4b2017-11-09 16:14:16 +01002553 switch(pat->data->type) {
Emeric Brunb5997f72017-07-03 11:34:05 +02002554 case SMP_T_STR:
2555 static_sample_data.type = SMP_T_STR;
2556 static_sample_data.u.str = *get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002557 static_sample_data.u.str.data = pat->data->u.str.data;
2558 if (static_sample_data.u.str.data >= static_sample_data.u.str.size)
2559 static_sample_data.u.str.data = static_sample_data.u.str.size - 1;
2560 memcpy(static_sample_data.u.str.area,
2561 pat->data->u.str.area,
2562 static_sample_data.u.str.data);
2563 static_sample_data.u.str.area[static_sample_data.u.str.data] = 0;
Emeric Brunb5997f72017-07-03 11:34:05 +02002564 case SMP_T_IPV4:
2565 case SMP_T_IPV6:
2566 case SMP_T_SINT:
2567 memcpy(&static_sample_data, pat->data, sizeof(struct sample_data));
2568 default:
2569 pat->data = NULL;
2570 }
2571 pat->data = &static_sample_data;
2572 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002573 HA_RWLOCK_RDUNLOCK(PATEXP_LOCK, &list->expr->lock);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002574 return pat;
Emeric Brunb5997f72017-07-03 11:34:05 +02002575 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002576 HA_RWLOCK_RDUNLOCK(PATEXP_LOCK, &list->expr->lock);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002577 }
2578 return NULL;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002579}
2580
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01002581/* This function prune the pattern expression. */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002582void pattern_prune(struct pattern_head *head)
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01002583{
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002584 struct pattern_expr_list *list, *safe;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002585
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002586 list_for_each_entry_safe(list, safe, &head->head, list) {
2587 LIST_DEL(&list->list);
2588 if (list->do_free) {
2589 LIST_DEL(&list->expr->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002590 HA_RWLOCK_WRLOCK(PATEXP_LOCK, &list->expr->lock);
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002591 head->prune(list->expr);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002592 HA_RWLOCK_WRUNLOCK(PATEXP_LOCK, &list->expr->lock);
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002593 free(list->expr);
2594 }
2595 free(list);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002596 }
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01002597}
2598
Thierry FOURNIER55d0b102014-01-15 11:25:26 +01002599/* This function lookup for a pattern matching the <key> and return a
2600 * pointer to a pointer of the sample stoarge. If the <key> dont match,
2601 * the function returns NULL. If the key cannot be parsed, the function
2602 * fill <err>.
2603 */
Thierry FOURNIER12ba0c22015-08-14 00:02:11 +02002604struct sample_data **pattern_find_smp(struct pattern_expr *expr, struct pat_ref_elt *ref)
Thierry FOURNIER55d0b102014-01-15 11:25:26 +01002605{
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01002606 struct ebmb_node *node;
2607 struct pattern_tree *elt;
2608 struct pattern_list *pat;
Thierry FOURNIER55d0b102014-01-15 11:25:26 +01002609
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01002610 for (node = ebmb_first(&expr->pattern_tree);
2611 node;
2612 node = ebmb_next(node)) {
2613 elt = container_of(node, struct pattern_tree, node);
2614 if (elt->ref == ref)
Thierry FOURNIER503bb092015-08-19 08:35:43 +02002615 return &elt->data;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01002616 }
2617
2618 for (node = ebmb_first(&expr->pattern_tree_2);
2619 node;
2620 node = ebmb_next(node)) {
2621 elt = container_of(node, struct pattern_tree, node);
2622 if (elt->ref == ref)
Thierry FOURNIER503bb092015-08-19 08:35:43 +02002623 return &elt->data;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01002624 }
2625
2626 list_for_each_entry(pat, &expr->patterns, list)
2627 if (pat->pat.ref == ref)
Thierry FOURNIER503bb092015-08-19 08:35:43 +02002628 return &pat->pat.data;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01002629
2630 return NULL;
Thierry FOURNIER55d0b102014-01-15 11:25:26 +01002631}
2632
Thierry FOURNIERb1136502014-01-15 11:38:49 +01002633/* This function search all the pattern matching the <key> and delete it.
2634 * If the parsing of the input key fails, the function returns 0 and the
2635 * <err> is filled, else return 1;
2636 */
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01002637int pattern_delete(struct pattern_expr *expr, struct pat_ref_elt *ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01002638{
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002639 HA_RWLOCK_WRLOCK(PATEXP_LOCK, &expr->lock);
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01002640 expr->pat_head->delete(expr, ref);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002641 HA_RWLOCK_WRUNLOCK(PATEXP_LOCK, &expr->lock);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01002642 return 1;
2643}
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002644
Carl Henrik Lundef91ac192020-02-27 16:45:50 +01002645/* This function compares two pat_ref** on unique_id */
2646static int cmp_pat_ref(const void *_a, const void *_b)
2647{
2648 struct pat_ref * const *a = _a;
2649 struct pat_ref * const *b = _b;
2650
2651 if ((*a)->unique_id < (*b)->unique_id)
2652 return -1;
2653 else if ((*a)->unique_id > (*b)->unique_id)
2654 return 1;
2655 return 0;
2656}
2657
2658/* This function finalize the configuration parsing. It sets all the
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002659 * automatic ids
2660 */
Carl Henrik Lundef91ac192020-02-27 16:45:50 +01002661int pattern_finalize_config(void)
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002662{
Carl Henrik Lundef91ac192020-02-27 16:45:50 +01002663 int len = 0;
2664 int unassigned_pos = 0;
2665 int next_unique_id = 0;
2666 int i, j;
2667 struct pat_ref *ref, **arr;
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002668 struct list pr = LIST_HEAD_INIT(pr);
2669
Willy Tarreau52bf8392020-03-08 00:42:37 +01002670 pat_lru_seed = ha_random();
Willy Tarreauf3045d22015-04-29 16:24:50 +02002671
Carl Henrik Lundef91ac192020-02-27 16:45:50 +01002672 /* Count pat_refs with user defined unique_id and totalt count */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002673 list_for_each_entry(ref, &pattern_reference, list) {
Carl Henrik Lundef91ac192020-02-27 16:45:50 +01002674 len++;
2675 if (ref->unique_id != -1)
2676 unassigned_pos++;
2677 }
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002678
Carl Henrik Lundef91ac192020-02-27 16:45:50 +01002679 arr = calloc(len, sizeof(*arr));
2680 if (arr == NULL) {
2681 ha_alert("Out of memory error.\n");
2682 return ERR_ALERT | ERR_FATAL;
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002683 }
2684
Carl Henrik Lundef91ac192020-02-27 16:45:50 +01002685 i = 0;
2686 j = unassigned_pos;
2687 list_for_each_entry(ref, &pattern_reference, list) {
2688 if (ref->unique_id != -1)
2689 arr[i++] = ref;
2690 else
2691 arr[j++] = ref;
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002692 }
2693
Carl Henrik Lundef91ac192020-02-27 16:45:50 +01002694 /* Sort first segment of array with user-defined unique ids for
2695 * fast lookup when generating unique ids
2696 */
2697 qsort(arr, unassigned_pos, sizeof(*arr), cmp_pat_ref);
2698
2699 /* Assign unique ids to the rest of the elements */
2700 for (i = unassigned_pos; i < len; i++) {
2701 do {
2702 arr[i]->unique_id = next_unique_id++;
2703 } while (bsearch(&arr[i], arr, unassigned_pos, sizeof(*arr), cmp_pat_ref));
2704 }
2705
2706 /* Sort complete array */
2707 qsort(arr, len, sizeof(*arr), cmp_pat_ref);
2708
2709 /* Convert back to linked list */
2710 for (i = 0; i < len; i++)
2711 LIST_ADDQ(&pr, &arr[i]->list);
2712
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002713 /* swap root */
2714 LIST_ADD(&pr, &pattern_reference);
2715 LIST_DEL(&pr);
Carl Henrik Lundef91ac192020-02-27 16:45:50 +01002716
2717 free(arr);
2718 return 0;
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002719}
Willy Tarreau403bfbb2019-10-23 06:59:31 +02002720
2721static int pattern_per_thread_lru_alloc()
2722{
2723 if (!global.tune.pattern_cache)
2724 return 1;
2725 pat_lru_tree = lru64_new(global.tune.pattern_cache);
2726 return !!pat_lru_tree;
2727}
2728
2729static void pattern_per_thread_lru_free()
2730{
2731 lru64_destroy(pat_lru_tree);
2732}
2733
2734REGISTER_PER_THREAD_ALLOC(pattern_per_thread_lru_alloc);
2735REGISTER_PER_THREAD_FREE(pattern_per_thread_lru_free);