blob: 06773f5602609659a3d3da198f52304a23904385 [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 Magnin556a94a2020-01-17 18:01:20 +010015#include <errno.h>
Thierry FOURNIERed66c292013-11-28 11:05:19 +010016
17#include <common/config.h>
18#include <common/standard.h>
19
20#include <types/global.h>
21#include <types/pattern.h>
22
Thierry FOURNIER46006bd2014-03-21 21:45:15 +010023#include <proto/log.h>
Thierry FOURNIERed66c292013-11-28 11:05:19 +010024#include <proto/pattern.h>
Thierry FOURNIERe3ded592013-12-06 15:36:54 +010025#include <proto/sample.h>
Thierry FOURNIERed66c292013-11-28 11:05:19 +010026
27#include <ebsttree.h>
Willy Tarreauf3045d22015-04-29 16:24:50 +020028#include <import/lru.h>
29#include <import/xxhash.h>
Thierry FOURNIERed66c292013-11-28 11:05:19 +010030
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010031char *pat_match_names[PAT_MATCH_NUM] = {
32 [PAT_MATCH_FOUND] = "found",
33 [PAT_MATCH_BOOL] = "bool",
34 [PAT_MATCH_INT] = "int",
35 [PAT_MATCH_IP] = "ip",
36 [PAT_MATCH_BIN] = "bin",
37 [PAT_MATCH_LEN] = "len",
38 [PAT_MATCH_STR] = "str",
39 [PAT_MATCH_BEG] = "beg",
40 [PAT_MATCH_SUB] = "sub",
41 [PAT_MATCH_DIR] = "dir",
42 [PAT_MATCH_DOM] = "dom",
43 [PAT_MATCH_END] = "end",
44 [PAT_MATCH_REG] = "reg",
Thierry Fournier8feaa662016-02-10 22:55:20 +010045 [PAT_MATCH_REGM] = "regm",
Thierry FOURNIERed66c292013-11-28 11:05:19 +010046};
47
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +020048int (*pat_parse_fcts[PAT_MATCH_NUM])(const char *, struct pattern *, int, char **) = {
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010049 [PAT_MATCH_FOUND] = pat_parse_nothing,
50 [PAT_MATCH_BOOL] = pat_parse_nothing,
51 [PAT_MATCH_INT] = pat_parse_int,
52 [PAT_MATCH_IP] = pat_parse_ip,
53 [PAT_MATCH_BIN] = pat_parse_bin,
Thierry FOURNIER5d344082014-01-27 14:19:53 +010054 [PAT_MATCH_LEN] = pat_parse_int,
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010055 [PAT_MATCH_STR] = pat_parse_str,
56 [PAT_MATCH_BEG] = pat_parse_str,
57 [PAT_MATCH_SUB] = pat_parse_str,
58 [PAT_MATCH_DIR] = pat_parse_str,
59 [PAT_MATCH_DOM] = pat_parse_str,
60 [PAT_MATCH_END] = pat_parse_str,
61 [PAT_MATCH_REG] = pat_parse_reg,
Thierry Fournier8feaa662016-02-10 22:55:20 +010062 [PAT_MATCH_REGM] = pat_parse_reg,
Thierry FOURNIERed66c292013-11-28 11:05:19 +010063};
64
Thierry FOURNIERb9b08462013-12-13 15:12:32 +010065int (*pat_index_fcts[PAT_MATCH_NUM])(struct pattern_expr *, struct pattern *, char **) = {
66 [PAT_MATCH_FOUND] = pat_idx_list_val,
67 [PAT_MATCH_BOOL] = pat_idx_list_val,
68 [PAT_MATCH_INT] = pat_idx_list_val,
69 [PAT_MATCH_IP] = pat_idx_tree_ip,
70 [PAT_MATCH_BIN] = pat_idx_list_ptr,
71 [PAT_MATCH_LEN] = pat_idx_list_val,
72 [PAT_MATCH_STR] = pat_idx_tree_str,
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +020073 [PAT_MATCH_BEG] = pat_idx_tree_pfx,
Thierry FOURNIERb9b08462013-12-13 15:12:32 +010074 [PAT_MATCH_SUB] = pat_idx_list_str,
75 [PAT_MATCH_DIR] = pat_idx_list_str,
76 [PAT_MATCH_DOM] = pat_idx_list_str,
77 [PAT_MATCH_END] = pat_idx_list_str,
78 [PAT_MATCH_REG] = pat_idx_list_reg,
Thierry Fournier8feaa662016-02-10 22:55:20 +010079 [PAT_MATCH_REGM] = pat_idx_list_regm,
Thierry FOURNIERb9b08462013-12-13 15:12:32 +010080};
81
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +010082void (*pat_delete_fcts[PAT_MATCH_NUM])(struct pattern_expr *, struct pat_ref_elt *) = {
Thierry FOURNIERb1136502014-01-15 11:38:49 +010083 [PAT_MATCH_FOUND] = pat_del_list_val,
84 [PAT_MATCH_BOOL] = pat_del_list_val,
85 [PAT_MATCH_INT] = pat_del_list_val,
86 [PAT_MATCH_IP] = pat_del_tree_ip,
87 [PAT_MATCH_BIN] = pat_del_list_ptr,
88 [PAT_MATCH_LEN] = pat_del_list_val,
89 [PAT_MATCH_STR] = pat_del_tree_str,
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +020090 [PAT_MATCH_BEG] = pat_del_tree_str,
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +010091 [PAT_MATCH_SUB] = pat_del_list_ptr,
92 [PAT_MATCH_DIR] = pat_del_list_ptr,
93 [PAT_MATCH_DOM] = pat_del_list_ptr,
94 [PAT_MATCH_END] = pat_del_list_ptr,
Thierry FOURNIERb1136502014-01-15 11:38:49 +010095 [PAT_MATCH_REG] = pat_del_list_reg,
Thierry Fournier8feaa662016-02-10 22:55:20 +010096 [PAT_MATCH_REGM] = pat_del_list_reg,
Thierry FOURNIERb1136502014-01-15 11:38:49 +010097};
98
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +010099void (*pat_prune_fcts[PAT_MATCH_NUM])(struct pattern_expr *) = {
100 [PAT_MATCH_FOUND] = pat_prune_val,
101 [PAT_MATCH_BOOL] = pat_prune_val,
102 [PAT_MATCH_INT] = pat_prune_val,
103 [PAT_MATCH_IP] = pat_prune_val,
104 [PAT_MATCH_BIN] = pat_prune_ptr,
105 [PAT_MATCH_LEN] = pat_prune_val,
106 [PAT_MATCH_STR] = pat_prune_ptr,
107 [PAT_MATCH_BEG] = pat_prune_ptr,
108 [PAT_MATCH_SUB] = pat_prune_ptr,
109 [PAT_MATCH_DIR] = pat_prune_ptr,
110 [PAT_MATCH_DOM] = pat_prune_ptr,
111 [PAT_MATCH_END] = pat_prune_ptr,
112 [PAT_MATCH_REG] = pat_prune_reg,
Thierry Fournier8feaa662016-02-10 22:55:20 +0100113 [PAT_MATCH_REGM] = pat_prune_reg,
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +0100114};
115
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100116struct pattern *(*pat_match_fcts[PAT_MATCH_NUM])(struct sample *, struct pattern_expr *, int) = {
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100117 [PAT_MATCH_FOUND] = NULL,
118 [PAT_MATCH_BOOL] = pat_match_nothing,
119 [PAT_MATCH_INT] = pat_match_int,
120 [PAT_MATCH_IP] = pat_match_ip,
121 [PAT_MATCH_BIN] = pat_match_bin,
122 [PAT_MATCH_LEN] = pat_match_len,
123 [PAT_MATCH_STR] = pat_match_str,
124 [PAT_MATCH_BEG] = pat_match_beg,
125 [PAT_MATCH_SUB] = pat_match_sub,
126 [PAT_MATCH_DIR] = pat_match_dir,
127 [PAT_MATCH_DOM] = pat_match_dom,
128 [PAT_MATCH_END] = pat_match_end,
129 [PAT_MATCH_REG] = pat_match_reg,
Thierry Fournier8feaa662016-02-10 22:55:20 +0100130 [PAT_MATCH_REGM] = pat_match_regm,
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100131};
132
Thierry FOURNIERe3ded592013-12-06 15:36:54 +0100133/* Just used for checking configuration compatibility */
134int pat_match_types[PAT_MATCH_NUM] = {
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200135 [PAT_MATCH_FOUND] = SMP_T_SINT,
136 [PAT_MATCH_BOOL] = SMP_T_SINT,
137 [PAT_MATCH_INT] = SMP_T_SINT,
Thierry FOURNIERe3ded592013-12-06 15:36:54 +0100138 [PAT_MATCH_IP] = SMP_T_ADDR,
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100139 [PAT_MATCH_BIN] = SMP_T_BIN,
140 [PAT_MATCH_LEN] = SMP_T_STR,
141 [PAT_MATCH_STR] = SMP_T_STR,
142 [PAT_MATCH_BEG] = SMP_T_STR,
143 [PAT_MATCH_SUB] = SMP_T_STR,
144 [PAT_MATCH_DIR] = SMP_T_STR,
145 [PAT_MATCH_DOM] = SMP_T_STR,
146 [PAT_MATCH_END] = SMP_T_STR,
147 [PAT_MATCH_REG] = SMP_T_STR,
Thierry Fournier8feaa662016-02-10 22:55:20 +0100148 [PAT_MATCH_REGM] = SMP_T_STR,
Thierry FOURNIERe3ded592013-12-06 15:36:54 +0100149};
150
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +0100151/* this struct is used to return information */
Emeric Brunb5997f72017-07-03 11:34:05 +0200152static THREAD_LOCAL struct pattern static_pattern;
153static THREAD_LOCAL struct sample_data static_sample_data;
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +0100154
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100155/* This is the root of the list of all pattern_ref avalaibles. */
156struct list pattern_reference = LIST_HEAD_INIT(pattern_reference);
157
Willy Tarreau7fdd81c2019-10-23 06:59:31 +0200158static THREAD_LOCAL struct lru64_head *pat_lru_tree;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200159static unsigned long long pat_lru_seed;
160
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100161/*
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100162 *
163 * The following functions are not exported and are used by internals process
164 * of pattern matching
165 *
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100166 */
167
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100168/* Background: Fast way to find a zero byte in a word
169 * http://graphics.stanford.edu/~seander/bithacks.html#ZeroInWord
170 * hasZeroByte = (v - 0x01010101UL) & ~v & 0x80808080UL;
171 *
172 * To look for 4 different byte values, xor the word with those bytes and
173 * then check for zero bytes:
174 *
175 * v = (((unsigned char)c * 0x1010101U) ^ delimiter)
176 * where <delimiter> is the 4 byte values to look for (as an uint)
177 * and <c> is the character that is being tested
178 */
179static inline unsigned int is_delimiter(unsigned char c, unsigned int mask)
180{
181 mask ^= (c * 0x01010101); /* propagate the char to all 4 bytes */
182 return (mask - 0x01010101) & ~mask & 0x80808080U;
183}
184
185static inline unsigned int make_4delim(unsigned char d1, unsigned char d2, unsigned char d3, unsigned char d4)
186{
187 return d1 << 24 | d2 << 16 | d3 << 8 | d4;
188}
189
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100190
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100191/*
192 *
193 * These functions are exported and may be used by any other component.
194 *
Willy Tarreau5def8ef2014-08-29 15:19:33 +0200195 * The following functions are used for parsing pattern matching input value.
196 * The <text> contain the string to be parsed. <pattern> must be a preallocated
197 * pattern. The pat_parse_* functions fill this structure with the parsed value.
198 * <err> is filled with an error message built with memprintf() function. It is
199 * allowed to use a trash as a temporary storage for the returned pattern, as
200 * the next call after these functions will be pat_idx_*.
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100201 *
Willy Tarreau5def8ef2014-08-29 15:19:33 +0200202 * In success case, the pat_parse_* function returns 1. If the function
203 * fails, it returns 0 and <err> is filled.
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100204 */
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100205
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100206/* ignore the current line */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200207int pat_parse_nothing(const char *text, struct pattern *pattern, int mflags, char **err)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100208{
209 return 1;
210}
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100211
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100212/* Parse a string. It is allocated and duplicated. */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200213int pat_parse_str(const char *text, struct pattern *pattern, int mflags, char **err)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100214{
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100215 pattern->type = SMP_T_STR;
Thierry FOURNIERedc15c32013-12-13 15:36:59 +0100216 pattern->ptr.str = (char *)text;
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100217 pattern->len = strlen(text);
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100218 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100219}
220
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100221/* Parse a binary written in hexa. It is allocated. */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200222int pat_parse_bin(const char *text, struct pattern *pattern, int mflags, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100223{
Willy Tarreau83061a82018-07-13 11:56:34 +0200224 struct buffer *trash;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100225
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100226 pattern->type = SMP_T_BIN;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100227 trash = get_trash_chunk();
228 pattern->len = trash->size;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200229 pattern->ptr.str = trash->area;
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100230 return !!parse_binary(text, &pattern->ptr.str, &pattern->len, err);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100231}
232
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100233/* Parse a regex. It is allocated. */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200234int pat_parse_reg(const char *text, struct pattern *pattern, int mflags, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100235{
Thierry FOURNIER0b6d15f2014-01-29 19:35:16 +0100236 pattern->ptr.str = (char *)text;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100237 return 1;
238}
239
240/* Parse a range of positive integers delimited by either ':' or '-'. If only
241 * one integer is read, it is set as both min and max. An operator may be
242 * specified as the prefix, among this list of 5 :
243 *
244 * 0:eq, 1:gt, 2:ge, 3:lt, 4:le
245 *
246 * The default operator is "eq". It supports range matching. Ranges are
247 * rejected for other operators. The operator may be changed at any time.
248 * The operator is stored in the 'opaque' argument.
249 *
250 * If err is non-NULL, an error message will be returned there on errors and
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100251 * the caller will have to free it. The function returns zero on error, and
252 * non-zero on success.
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100253 *
254 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200255int pat_parse_int(const char *text, struct pattern *pattern, int mflags, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100256{
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100257 const char *ptr = text;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100258
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200259 pattern->type = SMP_T_SINT;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100260
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100261 /* Empty string is not valid */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100262 if (!*text)
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100263 goto not_valid_range;
264
265 /* Search ':' or '-' separator. */
266 while (*ptr != '\0' && *ptr != ':' && *ptr != '-')
267 ptr++;
268
269 /* If separator not found. */
270 if (!*ptr) {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100271 if (strl2llrc(text, ptr - text, &pattern->val.range.min) != 0) {
272 memprintf(err, "'%s' is not a number", text);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100273 return 0;
274 }
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100275 pattern->val.range.max = pattern->val.range.min;
276 pattern->val.range.min_set = 1;
277 pattern->val.range.max_set = 1;
278 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100279 }
280
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100281 /* If the separator is the first character. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100282 if (ptr == text && *(ptr + 1) != '\0') {
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100283 if (strl2llrc(ptr + 1, strlen(ptr + 1), &pattern->val.range.max) != 0)
284 goto not_valid_range;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100285
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100286 pattern->val.range.min_set = 0;
287 pattern->val.range.max_set = 1;
288 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100289 }
290
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100291 /* If separator is the last character. */
292 if (*(ptr + 1) == '\0') {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100293 if (strl2llrc(text, ptr - text, &pattern->val.range.min) != 0)
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100294 goto not_valid_range;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100295
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100296 pattern->val.range.min_set = 1;
297 pattern->val.range.max_set = 0;
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100298 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100299 }
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100300
301 /* Else, parse two numbers. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100302 if (strl2llrc(text, ptr - text, &pattern->val.range.min) != 0)
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100303 goto not_valid_range;
304
305 if (strl2llrc(ptr + 1, strlen(ptr + 1), &pattern->val.range.max) != 0)
306 goto not_valid_range;
307
308 if (pattern->val.range.min > pattern->val.range.max)
309 goto not_valid_range;
310
311 pattern->val.range.min_set = 1;
312 pattern->val.range.max_set = 1;
313 return 1;
314
315 not_valid_range:
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100316 memprintf(err, "'%s' is not a valid number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100317 return 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100318}
319
320/* Parse a range of positive 2-component versions delimited by either ':' or
321 * '-'. The version consists in a major and a minor, both of which must be
322 * smaller than 65536, because internally they will be represented as a 32-bit
323 * integer.
324 * If only one version is read, it is set as both min and max. Just like for
325 * pure integers, an operator may be specified as the prefix, among this list
326 * of 5 :
327 *
328 * 0:eq, 1:gt, 2:ge, 3:lt, 4:le
329 *
330 * The default operator is "eq". It supports range matching. Ranges are
331 * rejected for other operators. The operator may be changed at any time.
332 * The operator is stored in the 'opaque' argument. This allows constructs
333 * such as the following one :
334 *
335 * acl obsolete_ssl ssl_req_proto lt 3
336 * acl unsupported_ssl ssl_req_proto gt 3.1
337 * acl valid_ssl ssl_req_proto 3.0-3.1
338 *
339 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200340int pat_parse_dotted_ver(const char *text, struct pattern *pattern, int mflags, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100341{
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100342 const char *ptr = text;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100343
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200344 pattern->type = SMP_T_SINT;
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100345
346 /* Search ':' or '-' separator. */
347 while (*ptr != '\0' && *ptr != ':' && *ptr != '-')
348 ptr++;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100349
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100350 /* If separator not found. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100351 if (*ptr == '\0' && ptr > text) {
352 if (strl2llrc_dotted(text, ptr-text, &pattern->val.range.min) != 0) {
353 memprintf(err, "'%s' is not a dotted number", text);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100354 return 0;
355 }
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100356 pattern->val.range.max = pattern->val.range.min;
357 pattern->val.range.min_set = 1;
358 pattern->val.range.max_set = 1;
359 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100360 }
361
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100362 /* If the separator is the first character. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100363 if (ptr == text && *(ptr+1) != '\0') {
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100364 if (strl2llrc_dotted(ptr+1, strlen(ptr+1), &pattern->val.range.max) != 0) {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100365 memprintf(err, "'%s' is not a valid dotted number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100366 return 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100367 }
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100368 pattern->val.range.min_set = 0;
369 pattern->val.range.max_set = 1;
370 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100371 }
372
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100373 /* If separator is the last character. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100374 if (ptr == &text[strlen(text)-1]) {
375 if (strl2llrc_dotted(text, ptr-text, &pattern->val.range.min) != 0) {
376 memprintf(err, "'%s' is not a valid dotted number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100377 return 0;
378 }
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100379 pattern->val.range.min_set = 1;
380 pattern->val.range.max_set = 0;
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100381 return 1;
382 }
383
384 /* Else, parse two numbers. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100385 if (strl2llrc_dotted(text, ptr-text, &pattern->val.range.min) != 0) {
386 memprintf(err, "'%s' is not a valid dotted number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100387 return 0;
388 }
389 if (strl2llrc_dotted(ptr+1, strlen(ptr+1), &pattern->val.range.max) != 0) {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100390 memprintf(err, "'%s' is not a valid dotted number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100391 return 0;
392 }
393 if (pattern->val.range.min > pattern->val.range.max) {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100394 memprintf(err, "'%s' is not a valid dotted number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100395 return 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100396 }
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100397 pattern->val.range.min_set = 1;
398 pattern->val.range.max_set = 1;
399 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100400}
401
402/* Parse an IP address and an optional mask in the form addr[/mask].
403 * The addr may either be an IPv4 address or a hostname. The mask
404 * may either be a dotted mask or a number of bits. Returns 1 if OK,
405 * otherwise 0. NOTE: IP address patterns are typed (IPV4/IPV6).
406 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200407int pat_parse_ip(const char *text, struct pattern *pattern, int mflags, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100408{
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200409 if (str2net(text, !(mflags & PAT_MF_NO_DNS) && (global.mode & MODE_STARTING),
Thierry FOURNIERfc7ac7b2014-02-11 15:23:04 +0100410 &pattern->val.ipv4.addr, &pattern->val.ipv4.mask)) {
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100411 pattern->type = SMP_T_IPV4;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100412 return 1;
413 }
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100414 else if (str62net(text, &pattern->val.ipv6.addr, &pattern->val.ipv6.mask)) {
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100415 pattern->type = SMP_T_IPV6;
416 return 1;
417 }
418 else {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100419 memprintf(err, "'%s' is not a valid IPv4 or IPv6 address", text);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100420 return 0;
421 }
422}
423
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100424/*
425 *
426 * These functions are exported and may be used by any other component.
427 *
Joseph Herlant4189d672018-11-15 10:22:31 -0800428 * This function just takes a sample <smp> and checks if this sample matches
429 * with the pattern <pattern>. This function returns only PAT_MATCH or
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100430 * PAT_NOMATCH.
431 *
432 */
433
434/* always return false */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100435struct pattern *pat_match_nothing(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100436{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200437 if (smp->data.u.sint) {
Thierry FOURNIERe5978bf2014-03-17 19:53:10 +0100438 if (fill) {
Thierry FOURNIER503bb092015-08-19 08:35:43 +0200439 static_pattern.data = NULL;
Thierry FOURNIERe5978bf2014-03-17 19:53:10 +0100440 static_pattern.ref = NULL;
Thierry FOURNIERe5978bf2014-03-17 19:53:10 +0100441 static_pattern.type = 0;
442 static_pattern.ptr.str = NULL;
443 }
444 return &static_pattern;
445 }
446 else
447 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100448}
449
450
Joseph Herlant4189d672018-11-15 10:22:31 -0800451/* NB: For two strings to be identical, it is required that their length match */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100452struct pattern *pat_match_str(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100453{
454 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100455 struct ebmb_node *node;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100456 struct pattern_tree *elt;
457 struct pattern_list *lst;
458 struct pattern *pattern;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200459 struct pattern *ret = NULL;
460 struct lru64 *lru = NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100461
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100462 /* Lookup a string in the expression's pattern tree. */
463 if (!eb_is_empty(&expr->pattern_tree)) {
Christopher Faulet135899e2020-06-30 18:52:32 +0200464 char prev = 0;
465
466 if (smp->data.u.str.data < smp->data.u.str.size) {
467 /* we may have to force a trailing zero on the test pattern and
468 * the buffer is large enough to accommodate it.
469 */
470 prev = smp->data.u.str.area[smp->data.u.str.data];
471 if (prev)
472 smp->data.u.str.area[smp->data.u.str.data] = '\0';
473 }
474 else {
475 /* Otherwise, the sample is duplicated. A trailing zero
476 * is automatically added to the string.
477 */
478 if (!smp_dup(smp))
479 return NULL;
480 }
481
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200482 node = ebst_lookup(&expr->pattern_tree, smp->data.u.str.area);
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100483 if (prev)
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200484 smp->data.u.str.area[smp->data.u.str.data] = prev;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100485 if (node) {
486 if (fill) {
487 elt = ebmb_entry(node, struct pattern_tree, node);
Thierry FOURNIER503bb092015-08-19 08:35:43 +0200488 static_pattern.data = elt->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +0100489 static_pattern.ref = elt->ref;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200490 static_pattern.sflags = PAT_SF_TREE;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100491 static_pattern.type = SMP_T_STR;
492 static_pattern.ptr.str = (char *)elt->node.key;
493 }
494 return &static_pattern;
495 }
496 }
497
498 /* look in the list */
Willy Tarreauf3045d22015-04-29 16:24:50 +0200499 if (pat_lru_tree) {
Willy Tarreauaee93142015-05-04 17:18:42 +0200500 unsigned long long seed = pat_lru_seed ^ (long)expr;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200501
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200502 lru = lru64_get(XXH64(smp->data.u.str.area, smp->data.u.str.data, seed),
Willy Tarreauf3045d22015-04-29 16:24:50 +0200503 pat_lru_tree, expr, expr->revision);
Willy Tarreau7fdd81c2019-10-23 06:59:31 +0200504 if (lru && lru->domain) {
Emeric Brunb5997f72017-07-03 11:34:05 +0200505 ret = lru->data;
Emeric Brunb5997f72017-07-03 11:34:05 +0200506 return ret;
507 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200508 }
509
Emeric Brunb5997f72017-07-03 11:34:05 +0200510
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100511 list_for_each_entry(lst, &expr->patterns, list) {
512 pattern = &lst->pat;
513
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200514 if (pattern->len != smp->data.u.str.data)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100515 continue;
516
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200517 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200518 if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.str.area, smp->data.u.str.data) == 0) ||
519 (!icase && strncmp(pattern->ptr.str, smp->data.u.str.area, smp->data.u.str.data) == 0)) {
Willy Tarreauf3045d22015-04-29 16:24:50 +0200520 ret = pattern;
521 break;
522 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100523 }
524
Willy Tarreau7fdd81c2019-10-23 06:59:31 +0200525 if (lru)
Emeric Brunb5997f72017-07-03 11:34:05 +0200526 lru64_commit(lru, ret, expr, expr->revision, NULL);
Willy Tarreauf3045d22015-04-29 16:24:50 +0200527
528 return ret;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100529}
530
531/* NB: For two binaries buf to be identical, it is required that their lengths match */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100532struct pattern *pat_match_bin(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100533{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100534 struct pattern_list *lst;
535 struct pattern *pattern;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200536 struct pattern *ret = NULL;
537 struct lru64 *lru = NULL;
538
539 if (pat_lru_tree) {
Willy Tarreauaee93142015-05-04 17:18:42 +0200540 unsigned long long seed = pat_lru_seed ^ (long)expr;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100541
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200542 lru = lru64_get(XXH64(smp->data.u.str.area, smp->data.u.str.data, seed),
Willy Tarreauf3045d22015-04-29 16:24:50 +0200543 pat_lru_tree, expr, expr->revision);
Willy Tarreau7fdd81c2019-10-23 06:59:31 +0200544 if (lru && lru->domain) {
Emeric Brunb5997f72017-07-03 11:34:05 +0200545 ret = lru->data;
Emeric Brunb5997f72017-07-03 11:34:05 +0200546 return ret;
547 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200548 }
549
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100550 list_for_each_entry(lst, &expr->patterns, list) {
551 pattern = &lst->pat;
552
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200553 if (pattern->len != smp->data.u.str.data)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100554 continue;
555
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200556 if (memcmp(pattern->ptr.str, smp->data.u.str.area, smp->data.u.str.data) == 0) {
Willy Tarreauf3045d22015-04-29 16:24:50 +0200557 ret = pattern;
558 break;
559 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100560 }
561
Willy Tarreau7fdd81c2019-10-23 06:59:31 +0200562 if (lru)
Emeric Brunb5997f72017-07-03 11:34:05 +0200563 lru64_commit(lru, ret, expr, expr->revision, NULL);
Willy Tarreauf3045d22015-04-29 16:24:50 +0200564
565 return ret;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100566}
567
568/* Executes a regex. It temporarily changes the data to add a trailing zero,
Thierry Fournier8feaa662016-02-10 22:55:20 +0100569 * and restores the previous character when leaving. This function fills
570 * a matching array.
571 */
572struct pattern *pat_match_regm(struct sample *smp, struct pattern_expr *expr, int fill)
573{
574 struct pattern_list *lst;
575 struct pattern *pattern;
576 struct pattern *ret = NULL;
577
578 list_for_each_entry(lst, &expr->patterns, list) {
579 pattern = &lst->pat;
580
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200581 if (regex_exec_match2(pattern->ptr.reg, smp->data.u.str.area, smp->data.u.str.data,
Thierry Fournier8feaa662016-02-10 22:55:20 +0100582 MAX_MATCH, pmatch, 0)) {
583 ret = pattern;
584 smp->ctx.a[0] = pmatch;
585 break;
586 }
587 }
588
589 return ret;
590}
591
592/* Executes a regex. It temporarily changes the data to add a trailing zero,
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100593 * and restores the previous character when leaving.
594 */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100595struct pattern *pat_match_reg(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100596{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100597 struct pattern_list *lst;
598 struct pattern *pattern;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200599 struct pattern *ret = NULL;
600 struct lru64 *lru = NULL;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100601
Willy Tarreauf3045d22015-04-29 16:24:50 +0200602 if (pat_lru_tree) {
Willy Tarreauaee93142015-05-04 17:18:42 +0200603 unsigned long long seed = pat_lru_seed ^ (long)expr;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200604
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200605 lru = lru64_get(XXH64(smp->data.u.str.area, smp->data.u.str.data, seed),
Willy Tarreauf3045d22015-04-29 16:24:50 +0200606 pat_lru_tree, expr, expr->revision);
Willy Tarreau7fdd81c2019-10-23 06:59:31 +0200607 if (lru && lru->domain) {
Emeric Brunb5997f72017-07-03 11:34:05 +0200608 ret = lru->data;
Emeric Brunb5997f72017-07-03 11:34:05 +0200609 return ret;
610 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200611 }
612
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100613 list_for_each_entry(lst, &expr->patterns, list) {
614 pattern = &lst->pat;
615
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200616 if (regex_exec2(pattern->ptr.reg, smp->data.u.str.area, smp->data.u.str.data)) {
Willy Tarreauf3045d22015-04-29 16:24:50 +0200617 ret = pattern;
618 break;
619 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100620 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200621
Willy Tarreau7fdd81c2019-10-23 06:59:31 +0200622 if (lru)
Emeric Brunb5997f72017-07-03 11:34:05 +0200623 lru64_commit(lru, ret, expr, expr->revision, NULL);
Willy Tarreauf3045d22015-04-29 16:24:50 +0200624
625 return ret;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100626}
627
628/* Checks that the pattern matches the beginning of the tested string. */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100629struct pattern *pat_match_beg(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100630{
631 int icase;
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +0200632 struct ebmb_node *node;
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +0200633 struct pattern_tree *elt;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100634 struct pattern_list *lst;
635 struct pattern *pattern;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200636 struct pattern *ret = NULL;
637 struct lru64 *lru = NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100638
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +0200639 /* Lookup a string in the expression's pattern tree. */
640 if (!eb_is_empty(&expr->pattern_tree)) {
Christopher Faulet135899e2020-06-30 18:52:32 +0200641 char prev = 0;
642
643 if (smp->data.u.str.data < smp->data.u.str.size) {
644 /* we may have to force a trailing zero on the test pattern and
645 * the buffer is large enough to accommodate it.
646 */
647 prev = smp->data.u.str.area[smp->data.u.str.data];
648 if (prev)
649 smp->data.u.str.area[smp->data.u.str.data] = '\0';
650 }
651 else {
652 /* Otherwise, the sample is duplicated. A trailing zero
653 * is automatically added to the string.
654 */
655 if (!smp_dup(smp))
656 return NULL;
657 }
658
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200659 node = ebmb_lookup_longest(&expr->pattern_tree,
660 smp->data.u.str.area);
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +0200661 if (prev)
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200662 smp->data.u.str.area[smp->data.u.str.data] = prev;
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +0200663
664 if (node) {
665 if (fill) {
666 elt = ebmb_entry(node, struct pattern_tree, node);
Thierry FOURNIER503bb092015-08-19 08:35:43 +0200667 static_pattern.data = elt->data;
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +0200668 static_pattern.ref = elt->ref;
669 static_pattern.sflags = PAT_SF_TREE;
670 static_pattern.type = SMP_T_STR;
671 static_pattern.ptr.str = (char *)elt->node.key;
672 }
673 return &static_pattern;
674 }
675 }
676
677 /* look in the list */
Willy Tarreauf3045d22015-04-29 16:24:50 +0200678 if (pat_lru_tree) {
Willy Tarreauaee93142015-05-04 17:18:42 +0200679 unsigned long long seed = pat_lru_seed ^ (long)expr;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200680
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200681 lru = lru64_get(XXH64(smp->data.u.str.area, smp->data.u.str.data, seed),
Willy Tarreauf3045d22015-04-29 16:24:50 +0200682 pat_lru_tree, expr, expr->revision);
Willy Tarreau7fdd81c2019-10-23 06:59:31 +0200683 if (lru && lru->domain) {
Emeric Brunb5997f72017-07-03 11:34:05 +0200684 ret = lru->data;
Emeric Brunb5997f72017-07-03 11:34:05 +0200685 return ret;
686 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200687 }
688
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100689 list_for_each_entry(lst, &expr->patterns, list) {
690 pattern = &lst->pat;
691
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200692 if (pattern->len > smp->data.u.str.data)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100693 continue;
694
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200695 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200696 if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.str.area, pattern->len) != 0) ||
697 (!icase && strncmp(pattern->ptr.str, smp->data.u.str.area, pattern->len) != 0))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100698 continue;
699
Willy Tarreauf3045d22015-04-29 16:24:50 +0200700 ret = pattern;
701 break;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100702 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200703
Willy Tarreau7fdd81c2019-10-23 06:59:31 +0200704 if (lru)
Emeric Brunb5997f72017-07-03 11:34:05 +0200705 lru64_commit(lru, ret, expr, expr->revision, NULL);
Willy Tarreauf3045d22015-04-29 16:24:50 +0200706
707 return ret;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100708}
709
710/* Checks that the pattern matches the end of the tested string. */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100711struct pattern *pat_match_end(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100712{
713 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100714 struct pattern_list *lst;
715 struct pattern *pattern;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200716 struct pattern *ret = NULL;
717 struct lru64 *lru = NULL;
718
719 if (pat_lru_tree) {
Willy Tarreauaee93142015-05-04 17:18:42 +0200720 unsigned long long seed = pat_lru_seed ^ (long)expr;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200721
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200722 lru = lru64_get(XXH64(smp->data.u.str.area, smp->data.u.str.data, seed),
Willy Tarreauf3045d22015-04-29 16:24:50 +0200723 pat_lru_tree, expr, expr->revision);
Willy Tarreau7fdd81c2019-10-23 06:59:31 +0200724 if (lru && lru->domain) {
Emeric Brunb5997f72017-07-03 11:34:05 +0200725 ret = lru->data;
Emeric Brunb5997f72017-07-03 11:34:05 +0200726 return ret;
727 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200728 }
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100729
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100730 list_for_each_entry(lst, &expr->patterns, list) {
731 pattern = &lst->pat;
732
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200733 if (pattern->len > smp->data.u.str.data)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100734 continue;
735
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200736 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200737 if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.str.area + smp->data.u.str.data - pattern->len, pattern->len) != 0) ||
738 (!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 +0100739 continue;
740
Willy Tarreauf3045d22015-04-29 16:24:50 +0200741 ret = pattern;
742 break;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100743 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200744
Willy Tarreau7fdd81c2019-10-23 06:59:31 +0200745 if (lru)
Emeric Brunb5997f72017-07-03 11:34:05 +0200746 lru64_commit(lru, ret, expr, expr->revision, NULL);
Willy Tarreauf3045d22015-04-29 16:24:50 +0200747
748 return ret;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100749}
750
751/* Checks that the pattern is included inside the tested string.
752 * NB: Suboptimal, should be rewritten using a Boyer-Moore method.
753 */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100754struct pattern *pat_match_sub(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100755{
756 int icase;
757 char *end;
758 char *c;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100759 struct pattern_list *lst;
760 struct pattern *pattern;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200761 struct pattern *ret = NULL;
762 struct lru64 *lru = NULL;
763
764 if (pat_lru_tree) {
Willy Tarreauaee93142015-05-04 17:18:42 +0200765 unsigned long long seed = pat_lru_seed ^ (long)expr;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200766
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200767 lru = lru64_get(XXH64(smp->data.u.str.area, smp->data.u.str.data, seed),
Willy Tarreauf3045d22015-04-29 16:24:50 +0200768 pat_lru_tree, expr, expr->revision);
Willy Tarreau7fdd81c2019-10-23 06:59:31 +0200769 if (lru && lru->domain) {
Emeric Brunb5997f72017-07-03 11:34:05 +0200770 ret = lru->data;
Emeric Brunb5997f72017-07-03 11:34:05 +0200771 return ret;
772 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200773 }
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100774
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100775 list_for_each_entry(lst, &expr->patterns, list) {
776 pattern = &lst->pat;
777
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200778 if (pattern->len > smp->data.u.str.data)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100779 continue;
780
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200781 end = smp->data.u.str.area + smp->data.u.str.data - pattern->len;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200782 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100783 if (icase) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200784 for (c = smp->data.u.str.area; c <= end; c++) {
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100785 if (tolower(*c) != tolower(*pattern->ptr.str))
786 continue;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200787 if (strncasecmp(pattern->ptr.str, c, pattern->len) == 0) {
788 ret = pattern;
789 goto leave;
790 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100791 }
792 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200793 for (c = smp->data.u.str.area; c <= end; c++) {
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100794 if (*c != *pattern->ptr.str)
795 continue;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200796 if (strncmp(pattern->ptr.str, c, pattern->len) == 0) {
797 ret = pattern;
798 goto leave;
799 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100800 }
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100801 }
802 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200803 leave:
Willy Tarreau7fdd81c2019-10-23 06:59:31 +0200804 if (lru)
Emeric Brunb5997f72017-07-03 11:34:05 +0200805 lru64_commit(lru, ret, expr, expr->revision, NULL);
Willy Tarreauf3045d22015-04-29 16:24:50 +0200806
807 return ret;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100808}
809
810/* This one is used by other real functions. It checks that the pattern is
811 * included inside the tested string, but enclosed between the specified
812 * delimiters or at the beginning or end of the string. The delimiters are
813 * provided as an unsigned int made by make_4delim() and match up to 4 different
814 * delimiters. Delimiters are stripped at the beginning and end of the pattern.
815 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200816static int match_word(struct sample *smp, struct pattern *pattern, int mflags, unsigned int delimiters)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100817{
818 int may_match, icase;
819 char *c, *end;
820 char *ps;
821 int pl;
822
823 pl = pattern->len;
824 ps = pattern->ptr.str;
825
826 while (pl > 0 && is_delimiter(*ps, delimiters)) {
827 pl--;
828 ps++;
829 }
830
831 while (pl > 0 && is_delimiter(ps[pl - 1], delimiters))
832 pl--;
833
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200834 if (pl > smp->data.u.str.data)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100835 return PAT_NOMATCH;
836
837 may_match = 1;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200838 icase = mflags & PAT_MF_IGNORE_CASE;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200839 end = smp->data.u.str.area + smp->data.u.str.data - pl;
840 for (c = smp->data.u.str.area; c <= end; c++) {
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100841 if (is_delimiter(*c, delimiters)) {
842 may_match = 1;
843 continue;
844 }
845
846 if (!may_match)
847 continue;
848
849 if (icase) {
850 if ((tolower(*c) == tolower(*ps)) &&
851 (strncasecmp(ps, c, pl) == 0) &&
852 (c == end || is_delimiter(c[pl], delimiters)))
853 return PAT_MATCH;
854 } else {
855 if ((*c == *ps) &&
856 (strncmp(ps, c, pl) == 0) &&
857 (c == end || is_delimiter(c[pl], delimiters)))
858 return PAT_MATCH;
859 }
860 may_match = 0;
861 }
862 return PAT_NOMATCH;
863}
864
865/* Checks that the pattern is included inside the tested string, but enclosed
866 * between the delimiters '?' or '/' or at the beginning or end of the string.
867 * Delimiters at the beginning or end of the pattern are ignored.
868 */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100869struct pattern *pat_match_dir(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100870{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100871 struct pattern_list *lst;
872 struct pattern *pattern;
873
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100874 list_for_each_entry(lst, &expr->patterns, list) {
875 pattern = &lst->pat;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200876 if (match_word(smp, pattern, expr->mflags, make_4delim('/', '?', '?', '?')))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100877 return pattern;
878 }
879 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100880}
881
882/* Checks that the pattern is included inside the tested string, but enclosed
883 * between the delmiters '/', '?', '.' or ":" or at the beginning or end of
884 * the string. Delimiters at the beginning or end of the pattern are ignored.
885 */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100886struct pattern *pat_match_dom(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100887{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100888 struct pattern_list *lst;
889 struct pattern *pattern;
890
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100891 list_for_each_entry(lst, &expr->patterns, list) {
892 pattern = &lst->pat;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200893 if (match_word(smp, pattern, expr->mflags, make_4delim('/', '?', '.', ':')))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100894 return pattern;
895 }
896 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100897}
898
899/* Checks that the integer in <test> is included between min and max */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100900struct pattern *pat_match_int(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100901{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100902 struct pattern_list *lst;
903 struct pattern *pattern;
904
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100905 list_for_each_entry(lst, &expr->patterns, list) {
906 pattern = &lst->pat;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200907 if ((!pattern->val.range.min_set || pattern->val.range.min <= smp->data.u.sint) &&
908 (!pattern->val.range.max_set || smp->data.u.sint <= pattern->val.range.max))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100909 return pattern;
910 }
911 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100912}
913
914/* Checks that the length of the pattern in <test> is included between min and max */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100915struct pattern *pat_match_len(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100916{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100917 struct pattern_list *lst;
918 struct pattern *pattern;
919
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100920 list_for_each_entry(lst, &expr->patterns, list) {
921 pattern = &lst->pat;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200922 if ((!pattern->val.range.min_set || pattern->val.range.min <= smp->data.u.str.data) &&
923 (!pattern->val.range.max_set || smp->data.u.str.data <= pattern->val.range.max))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100924 return pattern;
925 }
926 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100927}
928
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100929struct pattern *pat_match_ip(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100930{
931 unsigned int v4; /* in network byte order */
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100932 struct in6_addr tmp6;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100933 struct in_addr *s;
934 struct ebmb_node *node;
935 struct pattern_tree *elt;
936 struct pattern_list *lst;
937 struct pattern *pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100938
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100939 /* The input sample is IPv4. Try to match in the trees. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200940 if (smp->data.type == SMP_T_IPV4) {
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100941 /* Lookup an IPv4 address in the expression's pattern tree using
942 * the longest match method.
943 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200944 s = &smp->data.u.ipv4;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100945 node = ebmb_lookup_longest(&expr->pattern_tree, &s->s_addr);
946 if (node) {
947 if (fill) {
948 elt = ebmb_entry(node, struct pattern_tree, node);
Thierry FOURNIER503bb092015-08-19 08:35:43 +0200949 static_pattern.data = elt->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +0100950 static_pattern.ref = elt->ref;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200951 static_pattern.sflags = PAT_SF_TREE;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100952 static_pattern.type = SMP_T_IPV4;
Willy Tarreau33ccf1c2019-06-16 18:40:33 +0200953 static_pattern.val.ipv4.addr = *(struct in_addr *)elt->node.key;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100954 if (!cidr2dotted(elt->node.node.pfx, &static_pattern.val.ipv4.mask))
955 return NULL;
956 }
957 return &static_pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100958 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100959
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100960 /* The IPv4 sample dont match the IPv4 tree. Convert the IPv4
961 * sample address to IPv6 with the mapping method using the ::ffff:
962 * prefix, and try to lookup in the IPv6 tree.
963 */
964 memset(&tmp6, 0, 10);
965 *(uint16_t*)&tmp6.s6_addr[10] = htons(0xffff);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200966 *(uint32_t*)&tmp6.s6_addr[12] = smp->data.u.ipv4.s_addr;
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100967 node = ebmb_lookup_longest(&expr->pattern_tree_2, &tmp6);
968 if (node) {
969 if (fill) {
970 elt = ebmb_entry(node, struct pattern_tree, node);
Thierry FOURNIER503bb092015-08-19 08:35:43 +0200971 static_pattern.data = elt->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +0100972 static_pattern.ref = elt->ref;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200973 static_pattern.sflags = PAT_SF_TREE;
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100974 static_pattern.type = SMP_T_IPV6;
Willy Tarreau33ccf1c2019-06-16 18:40:33 +0200975 static_pattern.val.ipv6.addr = *(struct in6_addr *)elt->node.key;
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100976 static_pattern.val.ipv6.mask = elt->node.node.pfx;
977 }
978 return &static_pattern;
979 }
980 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100981
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100982 /* The input sample is IPv6. Try to match in the trees. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200983 if (smp->data.type == SMP_T_IPV6) {
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100984 /* Lookup an IPv6 address in the expression's pattern tree using
985 * the longest match method.
986 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200987 node = ebmb_lookup_longest(&expr->pattern_tree_2, &smp->data.u.ipv6);
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100988 if (node) {
989 if (fill) {
990 elt = ebmb_entry(node, struct pattern_tree, node);
Thierry FOURNIER503bb092015-08-19 08:35:43 +0200991 static_pattern.data = elt->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +0100992 static_pattern.ref = elt->ref;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200993 static_pattern.sflags = PAT_SF_TREE;
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100994 static_pattern.type = SMP_T_IPV6;
Willy Tarreau33ccf1c2019-06-16 18:40:33 +0200995 static_pattern.val.ipv6.addr = *(struct in6_addr *)elt->node.key;
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100996 static_pattern.val.ipv6.mask = elt->node.node.pfx;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100997 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100998 return &static_pattern;
999 }
1000
1001 /* Try to convert 6 to 4 when the start of the ipv6 address match the
1002 * following forms :
1003 * - ::ffff:ip:v4 (ipv4 mapped)
1004 * - ::0000:ip:v4 (old ipv4 mapped)
1005 * - 2002:ip:v4:: (6to4)
1006 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001007 if ((*(uint32_t*)&smp->data.u.ipv6.s6_addr[0] == 0 &&
1008 *(uint32_t*)&smp->data.u.ipv6.s6_addr[4] == 0 &&
1009 (*(uint32_t*)&smp->data.u.ipv6.s6_addr[8] == 0 ||
1010 *(uint32_t*)&smp->data.u.ipv6.s6_addr[8] == htonl(0xFFFF))) ||
1011 *(uint16_t*)&smp->data.u.ipv6.s6_addr[0] == htons(0x2002)) {
1012 if (*(uint32_t*)&smp->data.u.ipv6.s6_addr[0] == 0)
1013 v4 = *(uint32_t*)&smp->data.u.ipv6.s6_addr[12];
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001014 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001015 v4 = htonl((ntohs(*(uint16_t*)&smp->data.u.ipv6.s6_addr[2]) << 16) +
1016 ntohs(*(uint16_t*)&smp->data.u.ipv6.s6_addr[4]));
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001017
1018 /* Lookup an IPv4 address in the expression's pattern tree using the longest
1019 * match method.
1020 */
1021 node = ebmb_lookup_longest(&expr->pattern_tree, &v4);
1022 if (node) {
1023 if (fill) {
1024 elt = ebmb_entry(node, struct pattern_tree, node);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001025 static_pattern.data = elt->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01001026 static_pattern.ref = elt->ref;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02001027 static_pattern.sflags = PAT_SF_TREE;
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001028 static_pattern.type = SMP_T_IPV4;
Willy Tarreau33ccf1c2019-06-16 18:40:33 +02001029 static_pattern.val.ipv4.addr = *(struct in_addr *)elt->node.key;
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001030 if (!cidr2dotted(elt->node.node.pfx, &static_pattern.val.ipv4.mask))
1031 return NULL;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01001032 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001033 return &static_pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +01001034 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001035 }
1036 }
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +01001037
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001038 /* Lookup in the list. the list contain only IPv4 patterns */
1039 list_for_each_entry(lst, &expr->patterns, list) {
1040 pattern = &lst->pat;
1041
1042 /* The input sample is IPv4, use it as is. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001043 if (smp->data.type == SMP_T_IPV4) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001044 v4 = smp->data.u.ipv4.s_addr;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +01001045 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001046 else if (smp->data.type == SMP_T_IPV6) {
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001047 /* v4 match on a V6 sample. We want to check at least for
1048 * the following forms :
1049 * - ::ffff:ip:v4 (ipv4 mapped)
1050 * - ::0000:ip:v4 (old ipv4 mapped)
1051 * - 2002:ip:v4:: (6to4)
1052 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001053 if (*(uint32_t*)&smp->data.u.ipv6.s6_addr[0] == 0 &&
1054 *(uint32_t*)&smp->data.u.ipv6.s6_addr[4] == 0 &&
1055 (*(uint32_t*)&smp->data.u.ipv6.s6_addr[8] == 0 ||
1056 *(uint32_t*)&smp->data.u.ipv6.s6_addr[8] == htonl(0xFFFF))) {
1057 v4 = *(uint32_t*)&smp->data.u.ipv6.s6_addr[12];
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01001058 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001059 else if (*(uint16_t*)&smp->data.u.ipv6.s6_addr[0] == htons(0x2002)) {
1060 v4 = htonl((ntohs(*(uint16_t*)&smp->data.u.ipv6.s6_addr[2]) << 16) +
1061 ntohs(*(uint16_t*)&smp->data.u.ipv6.s6_addr[4]));
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01001062 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001063 else
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01001064 continue;
Andreas Seltenreichf0653192016-03-03 20:08:35 +01001065 } else {
1066 /* impossible */
1067 continue;
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001068 }
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +01001069
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001070 /* Check if the input sample match the current pattern. */
1071 if (((v4 ^ pattern->val.ipv4.addr.s_addr) & pattern->val.ipv4.mask.s_addr) == 0)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01001072 return pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +01001073 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01001074 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +01001075}
1076
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001077void free_pattern_tree(struct eb_root *root)
1078{
1079 struct eb_node *node, *next;
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +01001080 struct pattern_tree *elt;
Thierry FOURNIER3ce88c72013-12-09 11:29:46 +01001081
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001082 node = eb_first(root);
1083 while (node) {
1084 next = eb_next(node);
1085 eb_delete(node);
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +01001086 elt = container_of(node, struct pattern_tree, node);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001087 free(elt->data);
Thierry FOURNIER3ce88c72013-12-09 11:29:46 +01001088 free(elt);
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001089 node = next;
1090 }
1091}
1092
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01001093void pat_prune_val(struct pattern_expr *expr)
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +01001094{
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01001095 struct pattern_list *pat, *tmp;
1096
1097 list_for_each_entry_safe(pat, tmp, &expr->patterns, list) {
Christopher Faulet9aa1d642020-09-09 16:09:44 +02001098 LIST_DEL(&pat->list);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001099 free(pat->pat.data);
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01001100 free(pat);
1101 }
1102
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +01001103 free_pattern_tree(&expr->pattern_tree);
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001104 free_pattern_tree(&expr->pattern_tree_2);
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +01001105 LIST_INIT(&expr->patterns);
Christopher Faulet9aa1d642020-09-09 16:09:44 +02001106 expr->revision = rdtsc();
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +01001107}
1108
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01001109void pat_prune_ptr(struct pattern_expr *expr)
1110{
1111 struct pattern_list *pat, *tmp;
1112
1113 list_for_each_entry_safe(pat, tmp, &expr->patterns, list) {
Christopher Faulet9aa1d642020-09-09 16:09:44 +02001114 LIST_DEL(&pat->list);
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01001115 free(pat->pat.ptr.ptr);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001116 free(pat->pat.data);
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01001117 free(pat);
1118 }
1119
1120 free_pattern_tree(&expr->pattern_tree);
1121 free_pattern_tree(&expr->pattern_tree_2);
1122 LIST_INIT(&expr->patterns);
Christopher Faulet9aa1d642020-09-09 16:09:44 +02001123 expr->revision = rdtsc();
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01001124}
1125
1126void pat_prune_reg(struct pattern_expr *expr)
1127{
1128 struct pattern_list *pat, *tmp;
1129
1130 list_for_each_entry_safe(pat, tmp, &expr->patterns, list) {
Christopher Faulet9aa1d642020-09-09 16:09:44 +02001131 LIST_DEL(&pat->list);
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01001132 regex_free(pat->pat.ptr.ptr);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001133 free(pat->pat.data);
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01001134 free(pat);
1135 }
1136
1137 free_pattern_tree(&expr->pattern_tree);
1138 free_pattern_tree(&expr->pattern_tree_2);
1139 LIST_INIT(&expr->patterns);
Christopher Faulet9aa1d642020-09-09 16:09:44 +02001140 expr->revision = rdtsc();
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01001141}
1142
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001143/*
1144 *
1145 * The following functions are used for the pattern indexation
1146 *
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001147 */
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001148
1149int pat_idx_list_val(struct pattern_expr *expr, struct pattern *pat, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001150{
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001151 struct pattern_list *patl;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001152
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001153 /* allocate pattern */
1154 patl = calloc(1, sizeof(*patl));
1155 if (!patl) {
1156 memprintf(err, "out of memory while indexing pattern");
Thierry FOURNIER972028f2014-01-23 17:53:31 +01001157 return 0;
1158 }
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001159
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001160 /* duplicate pattern */
1161 memcpy(&patl->pat, pat, sizeof(*pat));
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001162
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001163 /* chain pattern in the expression */
1164 LIST_ADDQ(&expr->patterns, &patl->list);
Willy Tarreau72f073b2015-04-29 17:53:47 +02001165 expr->revision = rdtsc();
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001166
1167 /* that's ok */
1168 return 1;
1169}
1170
1171int pat_idx_list_ptr(struct pattern_expr *expr, struct pattern *pat, char **err)
1172{
1173 struct pattern_list *patl;
1174
1175 /* allocate pattern */
1176 patl = calloc(1, sizeof(*patl));
Thierry FOURNIER8aa83842015-02-06 17:50:55 +01001177 if (!patl) {
1178 memprintf(err, "out of memory while indexing pattern");
Thierry FOURNIER972028f2014-01-23 17:53:31 +01001179 return 0;
Thierry FOURNIER8aa83842015-02-06 17:50:55 +01001180 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001181
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001182 /* duplicate pattern */
1183 memcpy(&patl->pat, pat, sizeof(*pat));
1184 patl->pat.ptr.ptr = malloc(patl->pat.len);
1185 if (!patl->pat.ptr.ptr) {
1186 free(patl);
1187 memprintf(err, "out of memory while indexing pattern");
1188 return 0;
1189 }
1190 memcpy(patl->pat.ptr.ptr, pat->ptr.ptr, pat->len);
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001191
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001192 /* chain pattern in the expression */
1193 LIST_ADDQ(&expr->patterns, &patl->list);
Willy Tarreau72f073b2015-04-29 17:53:47 +02001194 expr->revision = rdtsc();
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001195
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001196 /* that's ok */
1197 return 1;
1198}
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001199
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001200int pat_idx_list_str(struct pattern_expr *expr, struct pattern *pat, char **err)
1201{
1202 struct pattern_list *patl;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001203
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001204 /* allocate pattern */
1205 patl = calloc(1, sizeof(*patl));
1206 if (!patl) {
1207 memprintf(err, "out of memory while indexing pattern");
1208 return 0;
1209 }
1210
1211 /* duplicate pattern */
1212 memcpy(&patl->pat, pat, sizeof(*pat));
1213 patl->pat.ptr.str = malloc(patl->pat.len + 1);
1214 if (!patl->pat.ptr.str) {
1215 free(patl);
1216 memprintf(err, "out of memory while indexing pattern");
1217 return 0;
1218 }
1219 memcpy(patl->pat.ptr.ptr, pat->ptr.ptr, pat->len);
1220 patl->pat.ptr.str[patl->pat.len] = '\0';
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001221
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001222 /* chain pattern in the expression */
1223 LIST_ADDQ(&expr->patterns, &patl->list);
Willy Tarreau72f073b2015-04-29 17:53:47 +02001224 expr->revision = rdtsc();
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001225
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001226 /* that's ok */
1227 return 1;
1228}
1229
Thierry Fournier8feaa662016-02-10 22:55:20 +01001230int pat_idx_list_reg_cap(struct pattern_expr *expr, struct pattern *pat, int cap, char **err)
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001231{
1232 struct pattern_list *patl;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001233
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001234 /* allocate pattern */
1235 patl = calloc(1, sizeof(*patl));
1236 if (!patl) {
1237 memprintf(err, "out of memory while indexing pattern");
1238 return 0;
Thierry FOURNIER972028f2014-01-23 17:53:31 +01001239 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001240
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001241 /* duplicate pattern */
1242 memcpy(&patl->pat, pat, sizeof(*pat));
1243
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001244 /* compile regex */
Dragan Dosen26743032019-04-30 15:54:36 +02001245 if (!(patl->pat.ptr.reg = regex_comp(pat->ptr.str, !(expr->mflags & PAT_MF_IGNORE_CASE),
1246 cap, err))) {
Dirkjan Bussink07fcaaa2014-04-28 22:57:16 +00001247 free(patl);
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001248 return 0;
1249 }
1250
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001251 /* chain pattern in the expression */
1252 LIST_ADDQ(&expr->patterns, &patl->list);
Willy Tarreau72f073b2015-04-29 17:53:47 +02001253 expr->revision = rdtsc();
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001254
1255 /* that's ok */
1256 return 1;
1257}
1258
Thierry Fournier8feaa662016-02-10 22:55:20 +01001259int pat_idx_list_reg(struct pattern_expr *expr, struct pattern *pat, char **err)
1260{
1261 return pat_idx_list_reg_cap(expr, pat, 0, err);
1262}
1263
1264int pat_idx_list_regm(struct pattern_expr *expr, struct pattern *pat, char **err)
1265{
1266 return pat_idx_list_reg_cap(expr, pat, 1, err);
1267}
1268
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001269int pat_idx_tree_ip(struct pattern_expr *expr, struct pattern *pat, char **err)
1270{
1271 unsigned int mask;
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +01001272 struct pattern_tree *node;
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001273
1274 /* Only IPv4 can be indexed */
1275 if (pat->type == SMP_T_IPV4) {
Thierry FOURNIER972028f2014-01-23 17:53:31 +01001276 /* in IPv4 case, check if the mask is contiguous so that we can
1277 * insert the network into the tree. A continuous mask has only
1278 * ones on the left. This means that this mask + its lower bit
1279 * added once again is null.
1280 */
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001281 mask = ntohl(pat->val.ipv4.mask.s_addr);
1282 if (mask + (mask & -mask) == 0) {
1283 mask = mask ? 33 - flsnz(mask & -mask) : 0; /* equals cidr value */
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001284
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001285 /* node memory allocation */
1286 node = calloc(1, sizeof(*node) + 4);
1287 if (!node) {
1288 memprintf(err, "out of memory while loading pattern");
1289 return 0;
1290 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001291
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001292 /* copy the pointer to sample associated to this node */
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001293 node->data = pat->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01001294 node->ref = pat->ref;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001295
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001296 /* FIXME: insert <addr>/<mask> into the tree here */
1297 memcpy(node->node.key, &pat->val.ipv4.addr, 4); /* network byte order */
1298 node->node.node.pfx = mask;
Thierry FOURNIER31db4ae2014-01-30 00:27:15 +01001299
1300 /* Insert the entry. */
1301 ebmb_insert_prefix(&expr->pattern_tree, &node->node, 4);
Willy Tarreau72f073b2015-04-29 17:53:47 +02001302 expr->revision = rdtsc();
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001303
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001304 /* that's ok */
1305 return 1;
1306 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001307 else {
1308 /* If the mask is not contiguous, just add the pattern to the list */
1309 return pat_idx_list_val(expr, pat, err);
1310 }
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001311 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001312 else if (pat->type == SMP_T_IPV6) {
1313 /* IPv6 also can be indexed */
1314 node = calloc(1, sizeof(*node) + 16);
1315 if (!node) {
1316 memprintf(err, "out of memory while loading pattern");
1317 return 0;
1318 }
1319
1320 /* copy the pointer to sample associated to this node */
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001321 node->data = pat->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01001322 node->ref = pat->ref;
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001323
1324 /* FIXME: insert <addr>/<mask> into the tree here */
1325 memcpy(node->node.key, &pat->val.ipv6.addr, 16); /* network byte order */
1326 node->node.node.pfx = pat->val.ipv6.mask;
Thierry FOURNIER31db4ae2014-01-30 00:27:15 +01001327
1328 /* Insert the entry. */
1329 ebmb_insert_prefix(&expr->pattern_tree_2, &node->node, 16);
Willy Tarreau72f073b2015-04-29 17:53:47 +02001330 expr->revision = rdtsc();
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001331
1332 /* that's ok */
1333 return 1;
1334 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001335
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001336 return 0;
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001337}
1338
1339int pat_idx_tree_str(struct pattern_expr *expr, struct pattern *pat, char **err)
1340{
1341 int len;
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +01001342 struct pattern_tree *node;
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001343
1344 /* Only string can be indexed */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001345 if (pat->type != SMP_T_STR) {
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001346 memprintf(err, "internal error: string expected, but the type is '%s'",
1347 smp_to_type[pat->type]);
1348 return 0;
Thierry FOURNIER972028f2014-01-23 17:53:31 +01001349 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001350
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001351 /* If the flag PAT_F_IGNORE_CASE is set, we cannot use trees */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02001352 if (expr->mflags & PAT_MF_IGNORE_CASE)
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001353 return pat_idx_list_str(expr, pat, err);
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001354
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001355 /* Process the key len */
1356 len = strlen(pat->ptr.str) + 1;
1357
1358 /* node memory allocation */
1359 node = calloc(1, sizeof(*node) + len);
1360 if (!node) {
1361 memprintf(err, "out of memory while loading pattern");
1362 return 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001363 }
1364
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001365 /* copy the pointer to sample associated to this node */
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001366 node->data = pat->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01001367 node->ref = pat->ref;
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001368
1369 /* copy the string */
1370 memcpy(node->node.key, pat->ptr.str, len);
1371
1372 /* index the new node */
Thierry FOURNIER31db4ae2014-01-30 00:27:15 +01001373 ebst_insert(&expr->pattern_tree, &node->node);
Willy Tarreau72f073b2015-04-29 17:53:47 +02001374 expr->revision = rdtsc();
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001375
1376 /* that's ok */
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001377 return 1;
1378}
1379
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +02001380int pat_idx_tree_pfx(struct pattern_expr *expr, struct pattern *pat, char **err)
1381{
1382 int len;
1383 struct pattern_tree *node;
1384
1385 /* Only string can be indexed */
1386 if (pat->type != SMP_T_STR) {
1387 memprintf(err, "internal error: string expected, but the type is '%s'",
1388 smp_to_type[pat->type]);
1389 return 0;
1390 }
1391
1392 /* If the flag PAT_F_IGNORE_CASE is set, we cannot use trees */
1393 if (expr->mflags & PAT_MF_IGNORE_CASE)
1394 return pat_idx_list_str(expr, pat, err);
1395
1396 /* Process the key len */
1397 len = strlen(pat->ptr.str);
1398
1399 /* node memory allocation */
1400 node = calloc(1, sizeof(*node) + len + 1);
1401 if (!node) {
1402 memprintf(err, "out of memory while loading pattern");
1403 return 0;
1404 }
1405
1406 /* copy the pointer to sample associated to this node */
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001407 node->data = pat->data;
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +02001408 node->ref = pat->ref;
1409
1410 /* copy the string and the trailing zero */
1411 memcpy(node->node.key, pat->ptr.str, len + 1);
1412 node->node.node.pfx = len * 8;
1413
1414 /* index the new node */
1415 ebmb_insert_prefix(&expr->pattern_tree, &node->node, len);
Willy Tarreau72f073b2015-04-29 17:53:47 +02001416 expr->revision = rdtsc();
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +02001417
1418 /* that's ok */
1419 return 1;
1420}
1421
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001422void pat_del_list_val(struct pattern_expr *expr, struct pat_ref_elt *ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001423{
1424 struct pattern_list *pat;
1425 struct pattern_list *safe;
1426
1427 list_for_each_entry_safe(pat, safe, &expr->patterns, list) {
1428 /* Check equality. */
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001429 if (pat->pat.ref != ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001430 continue;
1431
1432 /* Delete and free entry. */
1433 LIST_DEL(&pat->list);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001434 free(pat->pat.data);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001435 free(pat);
1436 }
Willy Tarreau72f073b2015-04-29 17:53:47 +02001437 expr->revision = rdtsc();
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001438}
1439
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001440void pat_del_tree_ip(struct pattern_expr *expr, struct pat_ref_elt *ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001441{
1442 struct ebmb_node *node, *next_node;
1443 struct pattern_tree *elt;
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001444
1445 /* browse each node of the tree for IPv4 addresses. */
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001446 for (node = ebmb_first(&expr->pattern_tree), next_node = node ? ebmb_next(node) : NULL;
1447 node;
1448 node = next_node, next_node = node ? ebmb_next(node) : NULL) {
1449 /* Extract container of the tree node. */
1450 elt = container_of(node, struct pattern_tree, node);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001451
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001452 /* Check equality. */
1453 if (elt->ref != ref)
1454 continue;
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001455
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001456 /* Delete and free entry. */
1457 ebmb_delete(node);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001458 free(elt->data);
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001459 free(elt);
1460 }
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001461
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001462 /* Browse each node of the list for IPv4 addresses. */
1463 pat_del_list_val(expr, ref);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001464
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001465 /* browse each node of the tree for IPv6 addresses. */
1466 for (node = ebmb_first(&expr->pattern_tree_2), next_node = node ? ebmb_next(node) : NULL;
1467 node;
1468 node = next_node, next_node = node ? ebmb_next(node) : NULL) {
1469 /* Extract container of the tree node. */
1470 elt = container_of(node, struct pattern_tree, node);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001471
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001472 /* Check equality. */
1473 if (elt->ref != ref)
1474 continue;
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001475
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001476 /* Delete and free entry. */
1477 ebmb_delete(node);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001478 free(elt->data);
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001479 free(elt);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001480 }
Willy Tarreau72f073b2015-04-29 17:53:47 +02001481 expr->revision = rdtsc();
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001482}
1483
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001484void pat_del_list_ptr(struct pattern_expr *expr, struct pat_ref_elt *ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001485{
1486 struct pattern_list *pat;
1487 struct pattern_list *safe;
1488
1489 list_for_each_entry_safe(pat, safe, &expr->patterns, list) {
1490 /* Check equality. */
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001491 if (pat->pat.ref != ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001492 continue;
1493
1494 /* Delete and free entry. */
1495 LIST_DEL(&pat->list);
1496 free(pat->pat.ptr.ptr);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001497 free(pat->pat.data);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001498 free(pat);
1499 }
Willy Tarreau72f073b2015-04-29 17:53:47 +02001500 expr->revision = rdtsc();
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001501}
1502
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001503void pat_del_tree_str(struct pattern_expr *expr, struct pat_ref_elt *ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001504{
1505 struct ebmb_node *node, *next_node;
1506 struct pattern_tree *elt;
1507
Thierry FOURNIER73bc2852015-02-06 17:53:54 +01001508 /* If the flag PAT_F_IGNORE_CASE is set, we cannot use trees */
1509 if (expr->mflags & PAT_MF_IGNORE_CASE)
1510 return pat_del_list_ptr(expr, ref);
1511
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001512 /* browse each node of the tree. */
1513 for (node = ebmb_first(&expr->pattern_tree), next_node = node ? ebmb_next(node) : NULL;
1514 node;
1515 node = next_node, next_node = node ? ebmb_next(node) : NULL) {
1516 /* Extract container of the tree node. */
1517 elt = container_of(node, struct pattern_tree, node);
1518
1519 /* Check equality. */
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001520 if (elt->ref != ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001521 continue;
1522
1523 /* Delete and free entry. */
1524 ebmb_delete(node);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001525 free(elt->data);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001526 free(elt);
1527 }
Willy Tarreau72f073b2015-04-29 17:53:47 +02001528 expr->revision = rdtsc();
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001529}
1530
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001531void pat_del_list_reg(struct pattern_expr *expr, struct pat_ref_elt *ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001532{
1533 struct pattern_list *pat;
1534 struct pattern_list *safe;
1535
1536 list_for_each_entry_safe(pat, safe, &expr->patterns, list) {
1537 /* Check equality. */
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001538 if (pat->pat.ref != ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001539 continue;
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001540
1541 /* Delete and free entry. */
1542 LIST_DEL(&pat->list);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001543 regex_free(pat->pat.ptr.ptr);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001544 free(pat->pat.data);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001545 free(pat);
1546 }
Willy Tarreau72f073b2015-04-29 17:53:47 +02001547 expr->revision = rdtsc();
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001548}
1549
1550void pattern_init_expr(struct pattern_expr *expr)
1551{
1552 LIST_INIT(&expr->patterns);
Willy Tarreau72f073b2015-04-29 17:53:47 +02001553 expr->revision = 0;
Thierry FOURNIER31db4ae2014-01-30 00:27:15 +01001554 expr->pattern_tree = EB_ROOT;
1555 expr->pattern_tree_2 = EB_ROOT;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001556}
1557
1558void pattern_init_head(struct pattern_head *head)
1559{
1560 LIST_INIT(&head->head);
1561}
1562
1563/* The following functions are relative to the management of the reference
1564 * lists. These lists are used to store the original pattern and associated
1565 * value as string form.
1566 *
1567 * This is used with modifiable ACL and MAPS
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001568 *
1569 * The pattern reference are stored with two identifiers: the unique_id and
1570 * the reference.
1571 *
1572 * The reference identify a file. Each file with the same name point to the
1573 * same reference. We can register many times one file. If the file is modified,
1574 * all his dependencies are also modified. The reference can be used with map or
1575 * acl.
1576 *
1577 * The unique_id identify inline acl. The unique id is unique for each acl.
1578 * You cannot force the same id in the configuration file, because this repoort
1579 * an error.
1580 *
1581 * A particular case appears if the filename is a number. In this case, the
1582 * unique_id is set with the number represented by the filename and the
1583 * reference is also set. This method prevent double unique_id.
1584 *
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001585 */
1586
1587/* This function lookup for reference. If the reference is found, they return
1588 * pointer to the struct pat_ref, else return NULL.
1589 */
1590struct pat_ref *pat_ref_lookup(const char *reference)
1591{
1592 struct pat_ref *ref;
1593
1594 list_for_each_entry(ref, &pattern_reference, list)
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001595 if (ref->reference && strcmp(reference, ref->reference) == 0)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001596 return ref;
1597 return NULL;
1598}
1599
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001600/* This function lookup for unique id. If the reference is found, they return
1601 * pointer to the struct pat_ref, else return NULL.
1602 */
1603struct pat_ref *pat_ref_lookupid(int unique_id)
1604{
1605 struct pat_ref *ref;
1606
1607 list_for_each_entry(ref, &pattern_reference, list)
1608 if (ref->unique_id == unique_id)
1609 return ref;
1610 return NULL;
1611}
1612
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001613/* This function remove all pattern matching the pointer <refelt> from
1614 * the the reference and from each expr member of the reference. This
1615 * function returns 1 if the deletion is done and return 0 is the entry
1616 * is not found.
1617 */
1618int pat_ref_delete_by_id(struct pat_ref *ref, struct pat_ref_elt *refelt)
1619{
1620 struct pattern_expr *expr;
1621 struct pat_ref_elt *elt, *safe;
Emeric Brun8d85aa42017-06-29 15:40:33 +02001622 struct bref *bref, *back;
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001623
1624 /* delete pattern from reference */
1625 list_for_each_entry_safe(elt, safe, &ref->head, list) {
1626 if (elt == refelt) {
Emeric Brun8d85aa42017-06-29 15:40:33 +02001627 list_for_each_entry_safe(bref, back, &elt->back_refs, users) {
1628 /*
1629 * we have to unlink all watchers. We must not relink them if
1630 * this elt was the last one in the list.
1631 */
1632 LIST_DEL(&bref->users);
1633 LIST_INIT(&bref->users);
1634 if (elt->list.n != &ref->head)
Willy Tarreau49ee3b22019-04-30 11:43:43 +02001635 LIST_ADDQ(&LIST_ELEM(elt->list.n, typeof(elt), list)->back_refs, &bref->users);
Emeric Brun8d85aa42017-06-29 15:40:33 +02001636 bref->ref = elt->list.n;
1637 }
peter caiaede6dd2015-10-07 00:07:43 -07001638 list_for_each_entry(expr, &ref->pat, list)
1639 pattern_delete(expr, elt);
1640
Emeric Brunb5997f72017-07-03 11:34:05 +02001641 /* pat_ref_elt is trashed once all expr
1642 are cleaned and there is no ref remaining */
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001643 LIST_DEL(&elt->list);
1644 free(elt->sample);
1645 free(elt->pattern);
1646 free(elt);
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001647 return 1;
1648 }
1649 }
1650 return 0;
1651}
1652
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001653/* This function remove all pattern match <key> from the the reference
Joseph Herlant4189d672018-11-15 10:22:31 -08001654 * and from each expr member of the reference. This function returns 1
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001655 * if the deletion is done and return 0 is the entry is not found.
1656 */
1657int pat_ref_delete(struct pat_ref *ref, const char *key)
1658{
1659 struct pattern_expr *expr;
1660 struct pat_ref_elt *elt, *safe;
Emeric Brun8d85aa42017-06-29 15:40:33 +02001661 struct bref *bref, *back;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001662 int found = 0;
1663
1664 /* delete pattern from reference */
1665 list_for_each_entry_safe(elt, safe, &ref->head, list) {
1666 if (strcmp(key, elt->pattern) == 0) {
Emeric Brun8d85aa42017-06-29 15:40:33 +02001667 list_for_each_entry_safe(bref, back, &elt->back_refs, users) {
1668 /*
1669 * we have to unlink all watchers. We must not relink them if
1670 * this elt was the last one in the list.
1671 */
1672 LIST_DEL(&bref->users);
1673 LIST_INIT(&bref->users);
1674 if (elt->list.n != &ref->head)
Willy Tarreau49ee3b22019-04-30 11:43:43 +02001675 LIST_ADDQ(&LIST_ELEM(elt->list.n, typeof(elt), list)->back_refs, &bref->users);
Emeric Brun8d85aa42017-06-29 15:40:33 +02001676 bref->ref = elt->list.n;
1677 }
Dirkjan Bussink07fcaaa2014-04-28 22:57:16 +00001678 list_for_each_entry(expr, &ref->pat, list)
1679 pattern_delete(expr, elt);
1680
Emeric Brunb5997f72017-07-03 11:34:05 +02001681 /* pat_ref_elt is trashed once all expr
1682 are cleaned and there is no ref remaining */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001683 LIST_DEL(&elt->list);
1684 free(elt->sample);
1685 free(elt->pattern);
1686 free(elt);
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001687
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001688 found = 1;
1689 }
1690 }
1691
1692 if (!found)
1693 return 0;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001694 return 1;
1695}
1696
Baptiste Assmann953f74d2014-04-25 16:57:03 +02001697/*
1698 * find and return an element <elt> matching <key> in a reference <ref>
1699 * return NULL if not found
1700 */
1701struct pat_ref_elt *pat_ref_find_elt(struct pat_ref *ref, const char *key)
1702{
1703 struct pat_ref_elt *elt;
1704
1705 list_for_each_entry(elt, &ref->head, list) {
1706 if (strcmp(key, elt->pattern) == 0)
1707 return elt;
1708 }
1709
1710 return NULL;
1711}
1712
1713
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001714 /* This function modify the sample of the first pattern that match the <key>. */
1715static inline int pat_ref_set_elt(struct pat_ref *ref, struct pat_ref_elt *elt,
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001716 const char *value, char **err)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001717{
1718 struct pattern_expr *expr;
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001719 struct sample_data **data;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001720 char *sample;
Thierry FOURNIER12ba0c22015-08-14 00:02:11 +02001721 struct sample_data test;
Thierry FOURNIER149e0fe2014-01-29 19:35:06 +01001722
1723 /* Try all needed converters. */
1724 list_for_each_entry(expr, &ref->pat, list) {
1725 if (!expr->pat_head->parse_smp)
1726 continue;
1727
1728 if (!expr->pat_head->parse_smp(value, &test)) {
1729 memprintf(err, "unable to parse '%s'", value);
1730 return 0;
1731 }
1732 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001733
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001734 /* Modify pattern from reference. */
1735 sample = strdup(value);
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001736 if (!sample) {
1737 memprintf(err, "out of memory error");
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001738 return 0;
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001739 }
Thierry FOURNIER149e0fe2014-01-29 19:35:06 +01001740 /* Load sample in each reference. All the conversion are tested
1741 * below, normally these calls dosn't fail.
1742 */
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001743 list_for_each_entry(expr, &ref->pat, list) {
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001744 if (!expr->pat_head->parse_smp)
1745 continue;
1746
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001747 HA_RWLOCK_WRLOCK(PATEXP_LOCK, &expr->lock);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001748 data = pattern_find_smp(expr, elt);
1749 if (data && *data && !expr->pat_head->parse_smp(sample, *data))
1750 *data = NULL;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001751 HA_RWLOCK_WRUNLOCK(PATEXP_LOCK, &expr->lock);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001752 }
1753
Emeric Brunb5997f72017-07-03 11:34:05 +02001754 /* free old sample only when all exprs are updated */
1755 free(elt->sample);
1756 elt->sample = sample;
1757
1758
Thierry FOURNIER149e0fe2014-01-29 19:35:06 +01001759 return 1;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001760}
1761
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001762/* This function modify the sample of the first pattern that match the <key>. */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001763int 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 +01001764{
1765 struct pat_ref_elt *elt;
1766
1767 /* Look for pattern in the reference. */
1768 list_for_each_entry(elt, &ref->head, list) {
1769 if (elt == refelt) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001770 if (!pat_ref_set_elt(ref, elt, value, err))
1771 return 0;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001772 return 1;
1773 }
1774 }
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001775
1776 memprintf(err, "key or pattern not found");
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001777 return 0;
1778}
1779
1780/* This function modify the sample of the first pattern that match the <key>. */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001781int pat_ref_set(struct pat_ref *ref, const char *key, const char *value, char **err)
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001782{
1783 struct pat_ref_elt *elt;
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001784 int found = 0;
1785 char *_merr;
1786 char **merr;
1787
1788 if (err) {
1789 merr = &_merr;
1790 *merr = NULL;
1791 }
1792 else
1793 merr = NULL;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001794
1795 /* Look for pattern in the reference. */
1796 list_for_each_entry(elt, &ref->head, list) {
1797 if (strcmp(key, elt->pattern) == 0) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001798 if (!pat_ref_set_elt(ref, elt, value, merr)) {
William Lallemand579fb252018-06-11 10:53:46 +02001799 if (err && merr) {
1800 if (!found) {
1801 *err = *merr;
1802 } else {
1803 memprintf(err, "%s, %s", *err, *merr);
1804 free(*merr);
1805 *merr = NULL;
1806 }
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001807 }
1808 }
1809 found = 1;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001810 }
1811 }
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001812
1813 if (!found) {
1814 memprintf(err, "entry not found");
1815 return 0;
1816 }
1817 return 1;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001818}
1819
Joseph Herlant4189d672018-11-15 10:22:31 -08001820/* This function creates a new reference. <ref> is the reference name.
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001821 * <flags> are PAT_REF_*. /!\ The reference is not checked, and must
1822 * be unique. The user must check the reference with "pat_ref_lookup()"
Joseph Herlant4189d672018-11-15 10:22:31 -08001823 * before calling this function. If the function fail, it return NULL,
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001824 * else return new struct pat_ref.
1825 */
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001826struct pat_ref *pat_ref_new(const char *reference, const char *display, unsigned int flags)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001827{
1828 struct pat_ref *ref;
1829
1830 ref = malloc(sizeof(*ref));
1831 if (!ref)
1832 return NULL;
1833
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001834 if (display) {
1835 ref->display = strdup(display);
1836 if (!ref->display) {
1837 free(ref);
1838 return NULL;
1839 }
1840 }
1841 else
1842 ref->display = NULL;
1843
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001844 ref->reference = strdup(reference);
1845 if (!ref->reference) {
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001846 free(ref->display);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001847 free(ref);
1848 return NULL;
1849 }
1850
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001851 ref->flags = flags;
1852 ref->unique_id = -1;
1853
1854 LIST_INIT(&ref->head);
1855 LIST_INIT(&ref->pat);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001856 HA_SPIN_INIT(&ref->lock);
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001857 LIST_ADDQ(&pattern_reference, &ref->list);
1858
1859 return ref;
1860}
1861
1862/* This function create new reference. <unique_id> is the unique id. If
1863 * the value of <unique_id> is -1, the unique id is calculated later.
1864 * <flags> are PAT_REF_*. /!\ The reference is not checked, and must
1865 * be unique. The user must check the reference with "pat_ref_lookup()"
1866 * or pat_ref_lookupid before calling this function. If the function
1867 * fail, it return NULL, else return new struct pat_ref.
1868 */
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001869struct pat_ref *pat_ref_newid(int unique_id, const char *display, unsigned int flags)
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001870{
1871 struct pat_ref *ref;
1872
1873 ref = malloc(sizeof(*ref));
1874 if (!ref)
1875 return NULL;
1876
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001877 if (display) {
1878 ref->display = strdup(display);
1879 if (!ref->display) {
1880 free(ref);
1881 return NULL;
1882 }
1883 }
1884 else
1885 ref->display = NULL;
1886
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001887 ref->reference = NULL;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001888 ref->flags = flags;
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001889 ref->unique_id = unique_id;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001890 LIST_INIT(&ref->head);
1891 LIST_INIT(&ref->pat);
Aurélien Nephtali564d15a2018-04-19 16:56:07 +02001892 HA_SPIN_INIT(&ref->lock);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001893 LIST_ADDQ(&pattern_reference, &ref->list);
1894
1895 return ref;
1896}
1897
1898/* This function adds entry to <ref>. It can failed with memory error.
1899 * If the function fails, it returns 0.
1900 */
1901int pat_ref_append(struct pat_ref *ref, char *pattern, char *sample, int line)
1902{
1903 struct pat_ref_elt *elt;
1904
1905 elt = malloc(sizeof(*elt));
1906 if (!elt)
1907 return 0;
1908
1909 elt->line = line;
1910
1911 elt->pattern = strdup(pattern);
1912 if (!elt->pattern) {
1913 free(elt);
1914 return 0;
1915 }
1916
1917 if (sample) {
1918 elt->sample = strdup(sample);
1919 if (!elt->sample) {
1920 free(elt->pattern);
1921 free(elt);
1922 return 0;
1923 }
1924 }
1925 else
1926 elt->sample = NULL;
1927
Emeric Brun8d85aa42017-06-29 15:40:33 +02001928 LIST_INIT(&elt->back_refs);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001929 LIST_ADDQ(&ref->head, &elt->list);
1930
1931 return 1;
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001932}
1933
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001934/* This function create sample found in <elt>, parse the pattern also
1935 * found in <elt> and insert it in <expr>. The function copy <patflags>
1936 * in <expr>. If the function fails, it returns0 and <err> is filled.
1937 * In succes case, the function returns 1.
1938 */
1939static inline
1940int pat_ref_push(struct pat_ref_elt *elt, struct pattern_expr *expr,
1941 int patflags, char **err)
1942{
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001943 struct sample_data *data;
Thierry FOURNIERd25c8422014-01-28 15:34:35 +01001944 struct pattern pattern;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001945
1946 /* Create sample */
1947 if (elt->sample && expr->pat_head->parse_smp) {
1948 /* New sample. */
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001949 data = malloc(sizeof(*data));
1950 if (!data)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001951 return 0;
1952
1953 /* Parse value. */
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001954 if (!expr->pat_head->parse_smp(elt->sample, data)) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001955 memprintf(err, "unable to parse '%s'", elt->sample);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001956 free(data);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001957 return 0;
1958 }
1959
1960 }
1961 else
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001962 data = NULL;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001963
Thierry FOURNIERd25c8422014-01-28 15:34:35 +01001964 /* initialise pattern */
1965 memset(&pattern, 0, sizeof(pattern));
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001966 pattern.data = data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01001967 pattern.ref = elt;
Thierry FOURNIERd25c8422014-01-28 15:34:35 +01001968
1969 /* parse pattern */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02001970 if (!expr->pat_head->parse(elt->pattern, &pattern, expr->mflags, err)) {
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001971 free(data);
Thierry FOURNIERd25c8422014-01-28 15:34:35 +01001972 return 0;
1973 }
1974
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001975 HA_RWLOCK_WRLOCK(PATEXP_LOCK, &expr->lock);
Thierry FOURNIERd25c8422014-01-28 15:34:35 +01001976 /* index pattern */
1977 if (!expr->pat_head->index(expr, &pattern, err)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001978 HA_RWLOCK_WRUNLOCK(PATEXP_LOCK, &expr->lock);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001979 free(data);
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001980 return 0;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001981 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001982 HA_RWLOCK_WRUNLOCK(PATEXP_LOCK, &expr->lock);
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001983
1984 return 1;
1985}
1986
Thierry FOURNIER31db4ae2014-01-30 00:27:15 +01001987/* This function adds entry to <ref>. It can failed with memory error. The new
1988 * entry is added at all the pattern_expr registered in this reference. The
1989 * function stop on the first error encountered. It returns 0 and err is
1990 * filled. If an error is encountered, the complete add operation is cancelled.
1991 * If the insertion is a success the function returns 1.
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001992 */
1993int pat_ref_add(struct pat_ref *ref,
1994 const char *pattern, const char *sample,
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02001995 char **err)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001996{
1997 struct pat_ref_elt *elt;
1998 struct pattern_expr *expr;
1999
2000 elt = malloc(sizeof(*elt));
2001 if (!elt) {
2002 memprintf(err, "out of memory error");
2003 return 0;
2004 }
2005
2006 elt->line = -1;
2007
2008 elt->pattern = strdup(pattern);
2009 if (!elt->pattern) {
2010 free(elt);
2011 memprintf(err, "out of memory error");
2012 return 0;
2013 }
2014
2015 if (sample) {
2016 elt->sample = strdup(sample);
2017 if (!elt->sample) {
2018 free(elt->pattern);
2019 free(elt);
2020 memprintf(err, "out of memory error");
2021 return 0;
2022 }
2023 }
2024 else
2025 elt->sample = NULL;
2026
Emeric Brun8d85aa42017-06-29 15:40:33 +02002027 LIST_INIT(&elt->back_refs);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002028 LIST_ADDQ(&ref->head, &elt->list);
2029
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002030 list_for_each_entry(expr, &ref->pat, list) {
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02002031 if (!pat_ref_push(elt, expr, 0, err)) {
Thierry FOURNIER31db4ae2014-01-30 00:27:15 +01002032 /* If the insertion fails, try to delete all the added entries. */
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01002033 pat_ref_delete_by_id(ref, elt);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002034 return 0;
2035 }
2036 }
Emeric Brunb5997f72017-07-03 11:34:05 +02002037
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002038 return 1;
2039}
2040
Joseph Herlant4189d672018-11-15 10:22:31 -08002041/* This function prunes <ref>, replaces all references by the references
2042 * of <replace>, and reindexes all the news values.
Thierry FOURNIER46006bd2014-03-21 21:45:15 +01002043 *
Joseph Herlant4189d672018-11-15 10:22:31 -08002044 * The patterns are loaded in best effort and the errors are ignored,
2045 * but written in the logs.
Thierry FOURNIER46006bd2014-03-21 21:45:15 +01002046 */
2047void pat_ref_reload(struct pat_ref *ref, struct pat_ref *replace)
2048{
2049 struct pattern_expr *expr;
Emeric Brunb5997f72017-07-03 11:34:05 +02002050 struct pat_ref_elt *elt, *safe;
2051 struct bref *bref, *back;
Emeric Brunb5997f72017-07-03 11:34:05 +02002052 struct pattern pattern;
Thierry FOURNIER46006bd2014-03-21 21:45:15 +01002053
Thierry FOURNIER46006bd2014-03-21 21:45:15 +01002054
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002055 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Emeric Brunb5997f72017-07-03 11:34:05 +02002056 list_for_each_entry(expr, &ref->pat, list) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002057 HA_RWLOCK_WRLOCK(PATEXP_LOCK, &expr->lock);
Emeric Brunb5997f72017-07-03 11:34:05 +02002058 }
2059
2060 /* all expr are locked, we can safely remove all pat_ref */
2061 list_for_each_entry_safe(elt, safe, &ref->head, list) {
2062 list_for_each_entry_safe(bref, back, &elt->back_refs, users) {
2063 /*
2064 * we have to unlink all watchers. We must not relink them if
2065 * this elt was the last one in the list.
2066 */
2067 LIST_DEL(&bref->users);
2068 LIST_INIT(&bref->users);
2069 if (elt->list.n != &ref->head)
Willy Tarreau49ee3b22019-04-30 11:43:43 +02002070 LIST_ADDQ(&LIST_ELEM(elt->list.n, typeof(elt), list)->back_refs, &bref->users);
Emeric Brunb5997f72017-07-03 11:34:05 +02002071 bref->ref = elt->list.n;
2072 }
2073 LIST_DEL(&elt->list);
2074 free(elt->pattern);
2075 free(elt->sample);
2076 free(elt);
2077 }
2078
2079 /* switch pat_ret_elt lists */
Thierry FOURNIER46006bd2014-03-21 21:45:15 +01002080 LIST_ADD(&replace->head, &ref->head);
2081 LIST_DEL(&replace->head);
2082
Emeric Brunb5997f72017-07-03 11:34:05 +02002083 list_for_each_entry(expr, &ref->pat, list) {
2084 expr->pat_head->prune(expr);
2085 list_for_each_entry(elt, &ref->head, list) {
Dragan Dosenf1474792018-09-18 20:18:09 +02002086 char *err = NULL;
2087 struct sample_data *data = NULL;
2088
Emeric Brunb5997f72017-07-03 11:34:05 +02002089 /* Create sample */
2090 if (elt->sample && expr->pat_head->parse_smp) {
2091 /* New sample. */
2092 data = malloc(sizeof(*data));
2093 if (!data)
2094 continue;
2095
2096 /* Parse value. */
2097 if (!expr->pat_head->parse_smp(elt->sample, data)) {
2098 memprintf(&err, "unable to parse '%s'", elt->sample);
2099 send_log(NULL, LOG_NOTICE, "%s", err);
2100 free(err);
2101 free(data);
2102 continue;
2103 }
2104
2105 }
Emeric Brunb5997f72017-07-03 11:34:05 +02002106
2107 /* initialise pattern */
2108 memset(&pattern, 0, sizeof(pattern));
2109 pattern.data = data;
2110 pattern.ref = elt;
2111
2112 /* parse pattern */
2113 if (!expr->pat_head->parse(elt->pattern, &pattern, expr->mflags, &err)) {
Thierry FOURNIER46006bd2014-03-21 21:45:15 +01002114 send_log(NULL, LOG_NOTICE, "%s", err);
2115 free(err);
Emeric Brunb5997f72017-07-03 11:34:05 +02002116 free(data);
2117 continue;
2118 }
2119
2120 /* index pattern */
2121 if (!expr->pat_head->index(expr, &pattern, &err)) {
2122 send_log(NULL, LOG_NOTICE, "%s", err);
2123 free(err);
2124 free(data);
2125 continue;
Thierry FOURNIER46006bd2014-03-21 21:45:15 +01002126 }
2127 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002128 HA_RWLOCK_WRUNLOCK(PATEXP_LOCK, &expr->lock);
Thierry FOURNIER46006bd2014-03-21 21:45:15 +01002129 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002130 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER46006bd2014-03-21 21:45:15 +01002131}
2132
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002133/* This function prune all entries of <ref>. This function
Willy Tarreaub88a37c2019-12-20 18:22:02 +01002134 * prunes the associated pattern_expr. It may return before the end of
2135 * the list is reached, returning 0, to yield. The caller must call it
2136 * again. Otherwise it returns 1 once done.
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002137 */
Willy Tarreaub88a37c2019-12-20 18:22:02 +01002138int pat_ref_prune(struct pat_ref *ref)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002139{
2140 struct pat_ref_elt *elt, *safe;
2141 struct pattern_expr *expr;
Emeric Brun8d85aa42017-06-29 15:40:33 +02002142 struct bref *bref, *back;
Willy Tarreaub88a37c2019-12-20 18:22:02 +01002143 int loops = 0;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002144
Emeric Brunb5997f72017-07-03 11:34:05 +02002145 list_for_each_entry(expr, &ref->pat, list) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002146 HA_RWLOCK_WRLOCK(PATEXP_LOCK, &expr->lock);
Emeric Brunb5997f72017-07-03 11:34:05 +02002147 expr->pat_head->prune(expr);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002148 HA_RWLOCK_WRUNLOCK(PATEXP_LOCK, &expr->lock);
Willy Tarreaub88a37c2019-12-20 18:22:02 +01002149 loops++;
2150 /* yield often, some lists may be huge, especially those
2151 * having to be freed through free_pattern_tree()
2152 */
2153 if (loops > 10)
2154 return 0;
Emeric Brunb5997f72017-07-03 11:34:05 +02002155 }
2156
2157 /* we trash pat_ref_elt in a second time to ensure that data is
2158 free once there is no ref on it */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002159 list_for_each_entry_safe(elt, safe, &ref->head, list) {
Emeric Brun8d85aa42017-06-29 15:40:33 +02002160 list_for_each_entry_safe(bref, back, &elt->back_refs, users) {
2161 /*
2162 * we have to unlink all watchers. We must not relink them if
2163 * this elt was the last one in the list.
2164 */
2165 LIST_DEL(&bref->users);
2166 LIST_INIT(&bref->users);
2167 if (elt->list.n != &ref->head)
Willy Tarreau49ee3b22019-04-30 11:43:43 +02002168 LIST_ADDQ(&LIST_ELEM(elt->list.n, typeof(elt), list)->back_refs, &bref->users);
Emeric Brun8d85aa42017-06-29 15:40:33 +02002169 bref->ref = elt->list.n;
2170 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002171 LIST_DEL(&elt->list);
2172 free(elt->pattern);
2173 free(elt->sample);
2174 free(elt);
Willy Tarreaub88a37c2019-12-20 18:22:02 +01002175 loops++;
2176 if (loops > 100000)
2177 return 0;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002178 }
Willy Tarreaub88a37c2019-12-20 18:22:02 +01002179 return 1;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002180}
2181
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002182/* This function lookup for existing reference <ref> in pattern_head <head>. */
2183struct pattern_expr *pattern_lookup_expr(struct pattern_head *head, struct pat_ref *ref)
2184{
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002185 struct pattern_expr_list *expr;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002186
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002187 list_for_each_entry(expr, &head->head, list)
2188 if (expr->expr->ref == ref)
2189 return expr->expr;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002190 return NULL;
2191}
2192
Joseph Herlant4189d672018-11-15 10:22:31 -08002193/* This function creates new pattern_expr associated to the reference <ref>.
2194 * <ref> can be NULL. If an error occurs, the function returns NULL and
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002195 * <err> is filled. Otherwise, the function returns new pattern_expr linked
2196 * with <head> and <ref>.
Thierry FOURNIER315ec422014-11-24 11:14:42 +01002197 *
Joseph Herlant4189d672018-11-15 10:22:31 -08002198 * The returned value can be an already filled pattern list, in this case the
Thierry FOURNIER315ec422014-11-24 11:14:42 +01002199 * flag <reuse> is set.
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002200 */
Thierry FOURNIER315ec422014-11-24 11:14:42 +01002201struct pattern_expr *pattern_new_expr(struct pattern_head *head, struct pat_ref *ref,
Emeric Brun7d27f3c2017-07-03 17:54:23 +02002202 int patflags, char **err, int *reuse)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002203{
2204 struct pattern_expr *expr;
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002205 struct pattern_expr_list *list;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002206
Thierry FOURNIER315ec422014-11-24 11:14:42 +01002207 if (reuse)
2208 *reuse = 0;
2209
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002210 /* Memory and initialization of the chain element. */
2211 list = malloc(sizeof(*list));
2212 if (!list) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002213 memprintf(err, "out of memory");
2214 return NULL;
2215 }
2216
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002217 /* Look for existing similar expr. No that only the index, parse and
2218 * parse_smp function must be identical for having similar pattern.
Joseph Herlant4189d672018-11-15 10:22:31 -08002219 * The other function depends of these first.
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002220 */
2221 if (ref) {
2222 list_for_each_entry(expr, &ref->pat, list)
2223 if (expr->pat_head->index == head->index &&
2224 expr->pat_head->parse == head->parse &&
Emeric Brun7d27f3c2017-07-03 17:54:23 +02002225 expr->pat_head->parse_smp == head->parse_smp &&
2226 expr->mflags == patflags)
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002227 break;
2228 if (&expr->list == &ref->pat)
2229 expr = NULL;
2230 }
2231 else
2232 expr = NULL;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002233
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002234 /* If no similar expr was found, we create new expr. */
2235 if (!expr) {
2236 /* Get a lot of memory for the expr struct. */
2237 expr = malloc(sizeof(*expr));
2238 if (!expr) {
Andreas Seltenreiche6e22e82016-03-03 20:20:23 +01002239 free(list);
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002240 memprintf(err, "out of memory");
2241 return NULL;
2242 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002243
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002244 /* Initialize this new expr. */
2245 pattern_init_expr(expr);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002246
Emeric Brun7d27f3c2017-07-03 17:54:23 +02002247 /* Copy the pattern matching and indexing flags. */
2248 expr->mflags = patflags;
2249
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002250 /* This new pattern expression reference one of his heads. */
2251 expr->pat_head = head;
2252
2253 /* Link with ref, or to self to facilitate LIST_DEL() */
2254 if (ref)
2255 LIST_ADDQ(&ref->pat, &expr->list);
2256 else
2257 LIST_INIT(&expr->list);
2258
2259 expr->ref = ref;
2260
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002261 HA_RWLOCK_INIT(&expr->lock);
Emeric Brunb5997f72017-07-03 11:34:05 +02002262
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002263 /* We must free this pattern if it is no more used. */
2264 list->do_free = 1;
2265 }
2266 else {
2267 /* If the pattern used already exists, it is already linked
2268 * with ref and we must not free it.
2269 */
2270 list->do_free = 0;
Thierry FOURNIER315ec422014-11-24 11:14:42 +01002271 if (reuse)
2272 *reuse = 1;
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002273 }
2274
2275 /* The new list element reference the pattern_expr. */
2276 list->expr = expr;
2277
2278 /* Link the list element with the pattern_head. */
2279 LIST_ADDQ(&head->head, &list->list);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002280 return expr;
2281}
2282
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002283/* Reads patterns from a file. If <err_msg> is non-NULL, an error message will
2284 * be returned there on errors and the caller will have to free it.
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002285 *
2286 * The file contains one key + value per line. Lines which start with '#' are
2287 * ignored, just like empty lines. Leading tabs/spaces are stripped. The key is
2288 * then the first "word" (series of non-space/tabs characters), and the value is
2289 * what follows this series of space/tab till the end of the line excluding
2290 * trailing spaces/tabs.
2291 *
2292 * Example :
2293 *
2294 * # this is a comment and is ignored
2295 * 62.212.114.60 1wt.eu \n
2296 * <-><-----------><---><----><---->
2297 * | | | | `--- trailing spaces ignored
2298 * | | | `-------- value
2299 * | | `--------------- middle spaces ignored
2300 * | `------------------------ key
2301 * `-------------------------------- leading spaces ignored
2302 *
2303 * Return non-zero in case of succes, otherwise 0.
2304 */
2305int pat_ref_read_from_file_smp(struct pat_ref *ref, const char *filename, char **err)
2306{
2307 FILE *file;
2308 char *c;
2309 int ret = 0;
2310 int line = 0;
2311 char *key_beg;
2312 char *key_end;
2313 char *value_beg;
2314 char *value_end;
2315
2316 file = fopen(filename, "r");
2317 if (!file) {
2318 memprintf(err, "failed to open pattern file <%s>", filename);
2319 return 0;
2320 }
2321
2322 /* now parse all patterns. The file may contain only one pattern
2323 * followed by one value per line. The start spaces, separator spaces
2324 * and and spaces are stripped. Each can contain comment started by '#'
2325 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002326 while (fgets(trash.area, trash.size, file) != NULL) {
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002327 line++;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002328 c = trash.area;
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002329
2330 /* ignore lines beginning with a dash */
2331 if (*c == '#')
2332 continue;
2333
2334 /* strip leading spaces and tabs */
2335 while (*c == ' ' || *c == '\t')
2336 c++;
2337
2338 /* empty lines are ignored too */
2339 if (*c == '\0' || *c == '\r' || *c == '\n')
2340 continue;
2341
2342 /* look for the end of the key */
2343 key_beg = c;
2344 while (*c && *c != ' ' && *c != '\t' && *c != '\n' && *c != '\r')
2345 c++;
2346
2347 key_end = c;
2348
2349 /* strip middle spaces and tabs */
2350 while (*c == ' ' || *c == '\t')
2351 c++;
2352
2353 /* look for the end of the value, it is the end of the line */
2354 value_beg = c;
2355 while (*c && *c != '\n' && *c != '\r')
2356 c++;
2357 value_end = c;
2358
2359 /* trim possibly trailing spaces and tabs */
2360 while (value_end > value_beg && (value_end[-1] == ' ' || value_end[-1] == '\t'))
2361 value_end--;
2362
2363 /* set final \0 and check entries */
2364 *key_end = '\0';
2365 *value_end = '\0';
2366
2367 /* insert values */
2368 if (!pat_ref_append(ref, key_beg, value_beg, line)) {
2369 memprintf(err, "out of memory");
2370 goto out_close;
2371 }
2372 }
2373
Jerome Magnin8991ce12020-01-17 16:09:33 +01002374 if (ferror(file)) {
2375 memprintf(err, "error encountered while reading <%s> : %s",
2376 filename, strerror(errno));
2377 goto out_close;
2378 }
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002379 /* succes */
2380 ret = 1;
2381
2382 out_close:
2383 fclose(file);
2384 return ret;
2385}
2386
2387/* Reads patterns from a file. If <err_msg> is non-NULL, an error message will
2388 * be returned there on errors and the caller will have to free it.
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002389 */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002390int pat_ref_read_from_file(struct pat_ref *ref, const char *filename, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002391{
2392 FILE *file;
2393 char *c;
2394 char *arg;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002395 int ret = 0;
2396 int line = 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002397
2398 file = fopen(filename, "r");
2399 if (!file) {
2400 memprintf(err, "failed to open pattern file <%s>", filename);
2401 return 0;
2402 }
2403
2404 /* now parse all patterns. The file may contain only one pattern per
2405 * line. If the line contains spaces, they will be part of the pattern.
2406 * The pattern stops at the first CR, LF or EOF encountered.
2407 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002408 while (fgets(trash.area, trash.size, file) != NULL) {
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002409 line++;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002410 c = trash.area;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002411
2412 /* ignore lines beginning with a dash */
2413 if (*c == '#')
2414 continue;
2415
2416 /* strip leading spaces and tabs */
2417 while (*c == ' ' || *c == '\t')
2418 c++;
2419
2420
2421 arg = c;
2422 while (*c && *c != '\n' && *c != '\r')
2423 c++;
2424 *c = 0;
2425
2426 /* empty lines are ignored too */
2427 if (c == arg)
2428 continue;
2429
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002430 if (!pat_ref_append(ref, arg, NULL, line)) {
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002431 memprintf(err, "out of memory when loading patterns from file <%s>", filename);
2432 goto out_close;
2433 }
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002434 }
2435
Jerome Magnin8991ce12020-01-17 16:09:33 +01002436 if (ferror(file)) {
2437 memprintf(err, "error encountered while reading <%s> : %s",
2438 filename, strerror(errno));
2439 goto out_close;
2440 }
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002441 ret = 1; /* success */
2442
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002443 out_close:
2444 fclose(file);
2445 return ret;
2446}
2447
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002448int pattern_read_from_file(struct pattern_head *head, unsigned int refflags,
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002449 const char *filename, int patflags, int load_smp,
Thierry FOURNIER94580c92014-02-11 14:36:45 +01002450 char **err, const char *file, int line)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002451{
2452 struct pat_ref *ref;
2453 struct pattern_expr *expr;
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002454 struct pat_ref_elt *elt;
Willy Tarreau4deaf392014-11-26 13:17:03 +01002455 int reuse = 0;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002456
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002457 /* Lookup for the existing reference. */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002458 ref = pat_ref_lookup(filename);
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002459
2460 /* If the reference doesn't exists, create it and load associated file. */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002461 if (!ref) {
Thierry FOURNIER94580c92014-02-11 14:36:45 +01002462 chunk_printf(&trash,
2463 "pattern loaded from file '%s' used by %s at file '%s' line %d",
2464 filename, refflags & PAT_REF_MAP ? "map" : "acl", file, line);
2465
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002466 ref = pat_ref_new(filename, trash.area, refflags);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002467 if (!ref) {
2468 memprintf(err, "out of memory");
2469 return 0;
2470 }
2471
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002472 if (load_smp) {
Thierry FOURNIERc0bd9102014-01-29 12:32:58 +01002473 ref->flags |= PAT_REF_SMP;
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002474 if (!pat_ref_read_from_file_smp(ref, filename, err))
2475 return 0;
2476 }
2477 else {
2478 if (!pat_ref_read_from_file(ref, filename, err))
2479 return 0;
2480 }
2481 }
2482 else {
Thierry FOURNIERc0bd9102014-01-29 12:32:58 +01002483 /* The reference already exists, check the map compatibility. */
2484
2485 /* If the load require samples and the flag PAT_REF_SMP is not set,
2486 * the reference doesn't contain sample, and cannot be used.
2487 */
2488 if (load_smp) {
2489 if (!(ref->flags & PAT_REF_SMP)) {
2490 memprintf(err, "The file \"%s\" is already used as one column file "
2491 "and cannot be used by as two column file.",
2492 filename);
2493 return 0;
2494 }
2495 }
2496 else {
2497 /* The load doesn't require samples. If the flag PAT_REF_SMP is
2498 * set, the reference contains a sample, and cannot be used.
2499 */
2500 if (ref->flags & PAT_REF_SMP) {
2501 memprintf(err, "The file \"%s\" is already used as two column file "
2502 "and cannot be used by as one column file.",
2503 filename);
2504 return 0;
2505 }
2506 }
2507
Thierry FOURNIER94580c92014-02-11 14:36:45 +01002508 /* Extends display */
2509 chunk_printf(&trash, "%s", ref->display);
2510 chunk_appendf(&trash, ", by %s at file '%s' line %d",
2511 refflags & PAT_REF_MAP ? "map" : "acl", file, line);
2512 free(ref->display);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002513 ref->display = strdup(trash.area);
Thierry FOURNIER94580c92014-02-11 14:36:45 +01002514 if (!ref->display) {
2515 memprintf(err, "out of memory");
2516 return 0;
2517 }
2518
Thierry FOURNIERc0bd9102014-01-29 12:32:58 +01002519 /* Merge flags. */
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002520 ref->flags |= refflags;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002521 }
2522
2523 /* Now, we can loading patterns from the reference. */
2524
2525 /* Lookup for existing reference in the head. If the reference
2526 * doesn't exists, create it.
2527 */
2528 expr = pattern_lookup_expr(head, ref);
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02002529 if (!expr || (expr->mflags != patflags)) {
Emeric Brun7d27f3c2017-07-03 17:54:23 +02002530 expr = pattern_new_expr(head, ref, patflags, err, &reuse);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002531 if (!expr)
2532 return 0;
2533 }
2534
Thierry FOURNIER315ec422014-11-24 11:14:42 +01002535 /* The returned expression may be not empty, because the function
2536 * "pattern_new_expr" lookup for similar pattern list and can
2537 * reuse a already filled pattern list. In this case, we can not
2538 * reload the patterns.
2539 */
2540 if (reuse)
2541 return 1;
2542
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002543 /* Load reference content in the pattern expression. */
2544 list_for_each_entry(elt, &ref->head, list) {
2545 if (!pat_ref_push(elt, expr, patflags, err)) {
2546 if (elt->line > 0)
2547 memprintf(err, "%s at line %d of file '%s'",
2548 *err, elt->line, filename);
2549 return 0;
2550 }
2551 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002552
2553 return 1;
2554}
2555
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01002556/* This function executes a pattern match on a sample. It applies pattern <expr>
2557 * to sample <smp>. The function returns NULL if the sample dont match. It returns
2558 * non-null if the sample match. If <fill> is true and the sample match, the
2559 * function returns the matched pattern. In many cases, this pattern can be a
2560 * static buffer.
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002561 */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002562struct pattern *pattern_exec_match(struct pattern_head *head, struct sample *smp, int fill)
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002563{
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002564 struct pattern_expr_list *list;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002565 struct pattern *pat;
2566
2567 if (!head->match) {
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01002568 if (fill) {
Thierry FOURNIER503bb092015-08-19 08:35:43 +02002569 static_pattern.data = NULL;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01002570 static_pattern.ref = NULL;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02002571 static_pattern.sflags = 0;
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002572 static_pattern.type = SMP_T_SINT;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01002573 static_pattern.val.i = 1;
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01002574 }
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01002575 return &static_pattern;
2576 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002577
Thierry FOURNIER5d344082014-01-27 14:19:53 +01002578 /* convert input to string */
2579 if (!sample_convert(smp, head->expect_type))
2580 return NULL;
2581
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002582 list_for_each_entry(list, &head->head, list) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002583 HA_RWLOCK_RDLOCK(PATEXP_LOCK, &list->expr->lock);
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002584 pat = head->match(smp, list->expr, fill);
Emeric Brunb5997f72017-07-03 11:34:05 +02002585 if (pat) {
2586 /* We duplicate the pattern cause it could be modified
2587 by another thread */
2588 if (pat != &static_pattern) {
2589 memcpy(&static_pattern, pat, sizeof(struct pattern));
2590 pat = &static_pattern;
2591 }
2592
2593 /* We also duplicate the sample data for
2594 same reason */
2595 if (pat->data && (pat->data != &static_sample_data)) {
Christopher Faulet09fdf4b2017-11-09 16:14:16 +01002596 switch(pat->data->type) {
Emeric Brunb5997f72017-07-03 11:34:05 +02002597 case SMP_T_STR:
2598 static_sample_data.type = SMP_T_STR;
2599 static_sample_data.u.str = *get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002600 static_sample_data.u.str.data = pat->data->u.str.data;
2601 if (static_sample_data.u.str.data >= static_sample_data.u.str.size)
2602 static_sample_data.u.str.data = static_sample_data.u.str.size - 1;
2603 memcpy(static_sample_data.u.str.area,
Willy Tarreau45b2d9a2020-06-11 16:37:35 +02002604 pat->data->u.str.area, static_sample_data.u.str.data);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002605 static_sample_data.u.str.area[static_sample_data.u.str.data] = 0;
Willy Tarreau45b2d9a2020-06-11 16:37:35 +02002606 pat->data = &static_sample_data;
2607 break;
2608
Emeric Brunb5997f72017-07-03 11:34:05 +02002609 case SMP_T_IPV4:
2610 case SMP_T_IPV6:
2611 case SMP_T_SINT:
2612 memcpy(&static_sample_data, pat->data, sizeof(struct sample_data));
Willy Tarreau45b2d9a2020-06-11 16:37:35 +02002613 pat->data = &static_sample_data;
2614 break;
Emeric Brunb5997f72017-07-03 11:34:05 +02002615 default:
Willy Tarreau45b2d9a2020-06-11 16:37:35 +02002616 /* unimplemented pattern type */
Emeric Brunb5997f72017-07-03 11:34:05 +02002617 pat->data = NULL;
Willy Tarreau45b2d9a2020-06-11 16:37:35 +02002618 break;
Emeric Brunb5997f72017-07-03 11:34:05 +02002619 }
Emeric Brunb5997f72017-07-03 11:34:05 +02002620 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002621 HA_RWLOCK_RDUNLOCK(PATEXP_LOCK, &list->expr->lock);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002622 return pat;
Emeric Brunb5997f72017-07-03 11:34:05 +02002623 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002624 HA_RWLOCK_RDUNLOCK(PATEXP_LOCK, &list->expr->lock);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002625 }
2626 return NULL;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002627}
2628
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01002629/* This function prune the pattern expression. */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002630void pattern_prune(struct pattern_head *head)
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01002631{
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002632 struct pattern_expr_list *list, *safe;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002633
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002634 list_for_each_entry_safe(list, safe, &head->head, list) {
2635 LIST_DEL(&list->list);
2636 if (list->do_free) {
2637 LIST_DEL(&list->expr->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002638 HA_RWLOCK_WRLOCK(PATEXP_LOCK, &list->expr->lock);
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002639 head->prune(list->expr);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002640 HA_RWLOCK_WRUNLOCK(PATEXP_LOCK, &list->expr->lock);
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002641 free(list->expr);
2642 }
2643 free(list);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002644 }
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01002645}
2646
Thierry FOURNIER55d0b102014-01-15 11:25:26 +01002647/* This function lookup for a pattern matching the <key> and return a
2648 * pointer to a pointer of the sample stoarge. If the <key> dont match,
2649 * the function returns NULL. If the key cannot be parsed, the function
2650 * fill <err>.
2651 */
Thierry FOURNIER12ba0c22015-08-14 00:02:11 +02002652struct sample_data **pattern_find_smp(struct pattern_expr *expr, struct pat_ref_elt *ref)
Thierry FOURNIER55d0b102014-01-15 11:25:26 +01002653{
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01002654 struct ebmb_node *node;
2655 struct pattern_tree *elt;
2656 struct pattern_list *pat;
Thierry FOURNIER55d0b102014-01-15 11:25:26 +01002657
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01002658 for (node = ebmb_first(&expr->pattern_tree);
2659 node;
2660 node = ebmb_next(node)) {
2661 elt = container_of(node, struct pattern_tree, node);
2662 if (elt->ref == ref)
Thierry FOURNIER503bb092015-08-19 08:35:43 +02002663 return &elt->data;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01002664 }
2665
2666 for (node = ebmb_first(&expr->pattern_tree_2);
2667 node;
2668 node = ebmb_next(node)) {
2669 elt = container_of(node, struct pattern_tree, node);
2670 if (elt->ref == ref)
Thierry FOURNIER503bb092015-08-19 08:35:43 +02002671 return &elt->data;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01002672 }
2673
2674 list_for_each_entry(pat, &expr->patterns, list)
2675 if (pat->pat.ref == ref)
Thierry FOURNIER503bb092015-08-19 08:35:43 +02002676 return &pat->pat.data;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01002677
2678 return NULL;
Thierry FOURNIER55d0b102014-01-15 11:25:26 +01002679}
2680
Thierry FOURNIERb1136502014-01-15 11:38:49 +01002681/* This function search all the pattern matching the <key> and delete it.
2682 * If the parsing of the input key fails, the function returns 0 and the
2683 * <err> is filled, else return 1;
2684 */
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01002685int pattern_delete(struct pattern_expr *expr, struct pat_ref_elt *ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01002686{
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002687 HA_RWLOCK_WRLOCK(PATEXP_LOCK, &expr->lock);
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01002688 expr->pat_head->delete(expr, ref);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002689 HA_RWLOCK_WRUNLOCK(PATEXP_LOCK, &expr->lock);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01002690 return 1;
2691}
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002692
Carl Henrik Lunde4944c8c2020-02-27 16:45:50 +01002693/* This function compares two pat_ref** on unique_id */
2694static int cmp_pat_ref(const void *_a, const void *_b)
2695{
2696 struct pat_ref * const *a = _a;
2697 struct pat_ref * const *b = _b;
2698
2699 if ((*a)->unique_id < (*b)->unique_id)
2700 return -1;
2701 else if ((*a)->unique_id > (*b)->unique_id)
2702 return 1;
2703 return 0;
2704}
2705
2706/* This function finalize the configuration parsing. It sets all the
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002707 * automatic ids
2708 */
Carl Henrik Lunde4944c8c2020-02-27 16:45:50 +01002709int pattern_finalize_config(void)
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002710{
Tim Duesterhusd5a8b9d2020-03-17 21:08:24 +01002711 size_t len = 0;
2712 size_t unassigned_pos = 0;
Carl Henrik Lunde4944c8c2020-02-27 16:45:50 +01002713 int next_unique_id = 0;
Tim Duesterhusd5a8b9d2020-03-17 21:08:24 +01002714 size_t i, j;
Carl Henrik Lunde4944c8c2020-02-27 16:45:50 +01002715 struct pat_ref *ref, **arr;
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002716 struct list pr = LIST_HEAD_INIT(pr);
2717
Willy Tarreau861c4ef2020-03-08 00:42:37 +01002718 pat_lru_seed = ha_random();
Willy Tarreauf3045d22015-04-29 16:24:50 +02002719
Carl Henrik Lunde4944c8c2020-02-27 16:45:50 +01002720 /* Count pat_refs with user defined unique_id and totalt count */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002721 list_for_each_entry(ref, &pattern_reference, list) {
Carl Henrik Lunde4944c8c2020-02-27 16:45:50 +01002722 len++;
2723 if (ref->unique_id != -1)
2724 unassigned_pos++;
2725 }
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002726
Tim Duesterhusd5a8b9d2020-03-17 21:08:24 +01002727 if (len == 0) {
2728 return 0;
2729 }
2730
Carl Henrik Lunde4944c8c2020-02-27 16:45:50 +01002731 arr = calloc(len, sizeof(*arr));
2732 if (arr == NULL) {
2733 ha_alert("Out of memory error.\n");
2734 return ERR_ALERT | ERR_FATAL;
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002735 }
2736
Carl Henrik Lunde4944c8c2020-02-27 16:45:50 +01002737 i = 0;
2738 j = unassigned_pos;
2739 list_for_each_entry(ref, &pattern_reference, list) {
2740 if (ref->unique_id != -1)
2741 arr[i++] = ref;
2742 else
2743 arr[j++] = ref;
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002744 }
2745
Carl Henrik Lunde4944c8c2020-02-27 16:45:50 +01002746 /* Sort first segment of array with user-defined unique ids for
2747 * fast lookup when generating unique ids
2748 */
2749 qsort(arr, unassigned_pos, sizeof(*arr), cmp_pat_ref);
2750
2751 /* Assign unique ids to the rest of the elements */
2752 for (i = unassigned_pos; i < len; i++) {
2753 do {
2754 arr[i]->unique_id = next_unique_id++;
2755 } while (bsearch(&arr[i], arr, unassigned_pos, sizeof(*arr), cmp_pat_ref));
2756 }
2757
2758 /* Sort complete array */
2759 qsort(arr, len, sizeof(*arr), cmp_pat_ref);
2760
2761 /* Convert back to linked list */
2762 for (i = 0; i < len; i++)
2763 LIST_ADDQ(&pr, &arr[i]->list);
2764
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002765 /* swap root */
2766 LIST_ADD(&pr, &pattern_reference);
2767 LIST_DEL(&pr);
Carl Henrik Lunde4944c8c2020-02-27 16:45:50 +01002768
2769 free(arr);
2770 return 0;
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002771}
Willy Tarreau7fdd81c2019-10-23 06:59:31 +02002772
2773static int pattern_per_thread_lru_alloc()
2774{
2775 if (!global.tune.pattern_cache)
2776 return 1;
2777 pat_lru_tree = lru64_new(global.tune.pattern_cache);
2778 return !!pat_lru_tree;
2779}
2780
2781static void pattern_per_thread_lru_free()
2782{
2783 lru64_destroy(pat_lru_tree);
2784}
2785
2786REGISTER_PER_THREAD_ALLOC(pattern_per_thread_lru_alloc);
2787REGISTER_PER_THREAD_FREE(pattern_per_thread_lru_free);