blob: 3dbc285b12457564f35248d84ed09d1be027fc75 [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;
456 char prev;
457 struct pattern_tree *elt;
458 struct pattern_list *lst;
459 struct pattern *pattern;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200460 struct pattern *ret = NULL;
461 struct lru64 *lru = NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100462
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100463 /* Lookup a string in the expression's pattern tree. */
464 if (!eb_is_empty(&expr->pattern_tree)) {
465 /* we may have to force a trailing zero on the test pattern */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200466 prev = smp->data.u.str.area[smp->data.u.str.data];
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100467 if (prev)
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200468 smp->data.u.str.area[smp->data.u.str.data] = '\0';
469 node = ebst_lookup(&expr->pattern_tree, smp->data.u.str.area);
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100470 if (prev)
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200471 smp->data.u.str.area[smp->data.u.str.data] = prev;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100472
473 if (node) {
474 if (fill) {
475 elt = ebmb_entry(node, struct pattern_tree, node);
Thierry FOURNIER503bb092015-08-19 08:35:43 +0200476 static_pattern.data = elt->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +0100477 static_pattern.ref = elt->ref;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200478 static_pattern.sflags = PAT_SF_TREE;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100479 static_pattern.type = SMP_T_STR;
480 static_pattern.ptr.str = (char *)elt->node.key;
481 }
482 return &static_pattern;
483 }
484 }
485
486 /* look in the list */
Willy Tarreauf3045d22015-04-29 16:24:50 +0200487 if (pat_lru_tree) {
Willy Tarreauaee93142015-05-04 17:18:42 +0200488 unsigned long long seed = pat_lru_seed ^ (long)expr;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200489
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200490 lru = lru64_get(XXH64(smp->data.u.str.area, smp->data.u.str.data, seed),
Willy Tarreauf3045d22015-04-29 16:24:50 +0200491 pat_lru_tree, expr, expr->revision);
Willy Tarreau7fdd81c2019-10-23 06:59:31 +0200492 if (lru && lru->domain) {
Emeric Brunb5997f72017-07-03 11:34:05 +0200493 ret = lru->data;
Emeric Brunb5997f72017-07-03 11:34:05 +0200494 return ret;
495 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200496 }
497
Emeric Brunb5997f72017-07-03 11:34:05 +0200498
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100499 list_for_each_entry(lst, &expr->patterns, list) {
500 pattern = &lst->pat;
501
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200502 if (pattern->len != smp->data.u.str.data)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100503 continue;
504
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200505 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200506 if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.str.area, smp->data.u.str.data) == 0) ||
507 (!icase && strncmp(pattern->ptr.str, smp->data.u.str.area, smp->data.u.str.data) == 0)) {
Willy Tarreauf3045d22015-04-29 16:24:50 +0200508 ret = pattern;
509 break;
510 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100511 }
512
Willy Tarreau7fdd81c2019-10-23 06:59:31 +0200513 if (lru)
Emeric Brunb5997f72017-07-03 11:34:05 +0200514 lru64_commit(lru, ret, expr, expr->revision, NULL);
Willy Tarreauf3045d22015-04-29 16:24:50 +0200515
516 return ret;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100517}
518
519/* NB: For two binaries buf to be identical, it is required that their lengths match */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100520struct pattern *pat_match_bin(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100521{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100522 struct pattern_list *lst;
523 struct pattern *pattern;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200524 struct pattern *ret = NULL;
525 struct lru64 *lru = NULL;
526
527 if (pat_lru_tree) {
Willy Tarreauaee93142015-05-04 17:18:42 +0200528 unsigned long long seed = pat_lru_seed ^ (long)expr;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100529
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200530 lru = lru64_get(XXH64(smp->data.u.str.area, smp->data.u.str.data, seed),
Willy Tarreauf3045d22015-04-29 16:24:50 +0200531 pat_lru_tree, expr, expr->revision);
Willy Tarreau7fdd81c2019-10-23 06:59:31 +0200532 if (lru && lru->domain) {
Emeric Brunb5997f72017-07-03 11:34:05 +0200533 ret = lru->data;
Emeric Brunb5997f72017-07-03 11:34:05 +0200534 return ret;
535 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200536 }
537
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100538 list_for_each_entry(lst, &expr->patterns, list) {
539 pattern = &lst->pat;
540
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200541 if (pattern->len != smp->data.u.str.data)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100542 continue;
543
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200544 if (memcmp(pattern->ptr.str, smp->data.u.str.area, smp->data.u.str.data) == 0) {
Willy Tarreauf3045d22015-04-29 16:24:50 +0200545 ret = pattern;
546 break;
547 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100548 }
549
Willy Tarreau7fdd81c2019-10-23 06:59:31 +0200550 if (lru)
Emeric Brunb5997f72017-07-03 11:34:05 +0200551 lru64_commit(lru, ret, expr, expr->revision, NULL);
Willy Tarreauf3045d22015-04-29 16:24:50 +0200552
553 return ret;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100554}
555
556/* Executes a regex. It temporarily changes the data to add a trailing zero,
Thierry Fournier8feaa662016-02-10 22:55:20 +0100557 * and restores the previous character when leaving. This function fills
558 * a matching array.
559 */
560struct pattern *pat_match_regm(struct sample *smp, struct pattern_expr *expr, int fill)
561{
562 struct pattern_list *lst;
563 struct pattern *pattern;
564 struct pattern *ret = NULL;
565
566 list_for_each_entry(lst, &expr->patterns, list) {
567 pattern = &lst->pat;
568
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200569 if (regex_exec_match2(pattern->ptr.reg, smp->data.u.str.area, smp->data.u.str.data,
Thierry Fournier8feaa662016-02-10 22:55:20 +0100570 MAX_MATCH, pmatch, 0)) {
571 ret = pattern;
572 smp->ctx.a[0] = pmatch;
573 break;
574 }
575 }
576
577 return ret;
578}
579
580/* Executes a regex. It temporarily changes the data to add a trailing zero,
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100581 * and restores the previous character when leaving.
582 */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100583struct pattern *pat_match_reg(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100584{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100585 struct pattern_list *lst;
586 struct pattern *pattern;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200587 struct pattern *ret = NULL;
588 struct lru64 *lru = NULL;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100589
Willy Tarreauf3045d22015-04-29 16:24:50 +0200590 if (pat_lru_tree) {
Willy Tarreauaee93142015-05-04 17:18:42 +0200591 unsigned long long seed = pat_lru_seed ^ (long)expr;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200592
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200593 lru = lru64_get(XXH64(smp->data.u.str.area, smp->data.u.str.data, seed),
Willy Tarreauf3045d22015-04-29 16:24:50 +0200594 pat_lru_tree, expr, expr->revision);
Willy Tarreau7fdd81c2019-10-23 06:59:31 +0200595 if (lru && lru->domain) {
Emeric Brunb5997f72017-07-03 11:34:05 +0200596 ret = lru->data;
Emeric Brunb5997f72017-07-03 11:34:05 +0200597 return ret;
598 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200599 }
600
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100601 list_for_each_entry(lst, &expr->patterns, list) {
602 pattern = &lst->pat;
603
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200604 if (regex_exec2(pattern->ptr.reg, smp->data.u.str.area, smp->data.u.str.data)) {
Willy Tarreauf3045d22015-04-29 16:24:50 +0200605 ret = pattern;
606 break;
607 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100608 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200609
Willy Tarreau7fdd81c2019-10-23 06:59:31 +0200610 if (lru)
Emeric Brunb5997f72017-07-03 11:34:05 +0200611 lru64_commit(lru, ret, expr, expr->revision, NULL);
Willy Tarreauf3045d22015-04-29 16:24:50 +0200612
613 return ret;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100614}
615
616/* Checks that the pattern matches the beginning of the tested string. */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100617struct pattern *pat_match_beg(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100618{
619 int icase;
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +0200620 struct ebmb_node *node;
621 char prev;
622 struct pattern_tree *elt;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100623 struct pattern_list *lst;
624 struct pattern *pattern;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200625 struct pattern *ret = NULL;
626 struct lru64 *lru = NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100627
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +0200628 /* Lookup a string in the expression's pattern tree. */
629 if (!eb_is_empty(&expr->pattern_tree)) {
630 /* we may have to force a trailing zero on the test pattern */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200631 prev = smp->data.u.str.area[smp->data.u.str.data];
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +0200632 if (prev)
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200633 smp->data.u.str.area[smp->data.u.str.data] = '\0';
634 node = ebmb_lookup_longest(&expr->pattern_tree,
635 smp->data.u.str.area);
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +0200636 if (prev)
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200637 smp->data.u.str.area[smp->data.u.str.data] = prev;
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +0200638
639 if (node) {
640 if (fill) {
641 elt = ebmb_entry(node, struct pattern_tree, node);
Thierry FOURNIER503bb092015-08-19 08:35:43 +0200642 static_pattern.data = elt->data;
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +0200643 static_pattern.ref = elt->ref;
644 static_pattern.sflags = PAT_SF_TREE;
645 static_pattern.type = SMP_T_STR;
646 static_pattern.ptr.str = (char *)elt->node.key;
647 }
648 return &static_pattern;
649 }
650 }
651
652 /* look in the list */
Willy Tarreauf3045d22015-04-29 16:24:50 +0200653 if (pat_lru_tree) {
Willy Tarreauaee93142015-05-04 17:18:42 +0200654 unsigned long long seed = pat_lru_seed ^ (long)expr;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200655
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200656 lru = lru64_get(XXH64(smp->data.u.str.area, smp->data.u.str.data, seed),
Willy Tarreauf3045d22015-04-29 16:24:50 +0200657 pat_lru_tree, expr, expr->revision);
Willy Tarreau7fdd81c2019-10-23 06:59:31 +0200658 if (lru && lru->domain) {
Emeric Brunb5997f72017-07-03 11:34:05 +0200659 ret = lru->data;
Emeric Brunb5997f72017-07-03 11:34:05 +0200660 return ret;
661 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200662 }
663
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100664 list_for_each_entry(lst, &expr->patterns, list) {
665 pattern = &lst->pat;
666
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200667 if (pattern->len > smp->data.u.str.data)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100668 continue;
669
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200670 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200671 if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.str.area, pattern->len) != 0) ||
672 (!icase && strncmp(pattern->ptr.str, smp->data.u.str.area, pattern->len) != 0))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100673 continue;
674
Willy Tarreauf3045d22015-04-29 16:24:50 +0200675 ret = pattern;
676 break;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100677 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200678
Willy Tarreau7fdd81c2019-10-23 06:59:31 +0200679 if (lru)
Emeric Brunb5997f72017-07-03 11:34:05 +0200680 lru64_commit(lru, ret, expr, expr->revision, NULL);
Willy Tarreauf3045d22015-04-29 16:24:50 +0200681
682 return ret;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100683}
684
685/* Checks that the pattern matches the end of the tested string. */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100686struct pattern *pat_match_end(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100687{
688 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100689 struct pattern_list *lst;
690 struct pattern *pattern;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200691 struct pattern *ret = NULL;
692 struct lru64 *lru = NULL;
693
694 if (pat_lru_tree) {
Willy Tarreauaee93142015-05-04 17:18:42 +0200695 unsigned long long seed = pat_lru_seed ^ (long)expr;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200696
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200697 lru = lru64_get(XXH64(smp->data.u.str.area, smp->data.u.str.data, seed),
Willy Tarreauf3045d22015-04-29 16:24:50 +0200698 pat_lru_tree, expr, expr->revision);
Willy Tarreau7fdd81c2019-10-23 06:59:31 +0200699 if (lru && lru->domain) {
Emeric Brunb5997f72017-07-03 11:34:05 +0200700 ret = lru->data;
Emeric Brunb5997f72017-07-03 11:34:05 +0200701 return ret;
702 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200703 }
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100704
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100705 list_for_each_entry(lst, &expr->patterns, list) {
706 pattern = &lst->pat;
707
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200708 if (pattern->len > smp->data.u.str.data)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100709 continue;
710
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200711 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200712 if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.str.area + smp->data.u.str.data - pattern->len, pattern->len) != 0) ||
713 (!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 +0100714 continue;
715
Willy Tarreauf3045d22015-04-29 16:24:50 +0200716 ret = pattern;
717 break;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100718 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200719
Willy Tarreau7fdd81c2019-10-23 06:59:31 +0200720 if (lru)
Emeric Brunb5997f72017-07-03 11:34:05 +0200721 lru64_commit(lru, ret, expr, expr->revision, NULL);
Willy Tarreauf3045d22015-04-29 16:24:50 +0200722
723 return ret;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100724}
725
726/* Checks that the pattern is included inside the tested string.
727 * NB: Suboptimal, should be rewritten using a Boyer-Moore method.
728 */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100729struct pattern *pat_match_sub(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100730{
731 int icase;
732 char *end;
733 char *c;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100734 struct pattern_list *lst;
735 struct pattern *pattern;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200736 struct pattern *ret = NULL;
737 struct lru64 *lru = NULL;
738
739 if (pat_lru_tree) {
Willy Tarreauaee93142015-05-04 17:18:42 +0200740 unsigned long long seed = pat_lru_seed ^ (long)expr;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200741
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200742 lru = lru64_get(XXH64(smp->data.u.str.area, smp->data.u.str.data, seed),
Willy Tarreauf3045d22015-04-29 16:24:50 +0200743 pat_lru_tree, expr, expr->revision);
Willy Tarreau7fdd81c2019-10-23 06:59:31 +0200744 if (lru && lru->domain) {
Emeric Brunb5997f72017-07-03 11:34:05 +0200745 ret = lru->data;
Emeric Brunb5997f72017-07-03 11:34:05 +0200746 return ret;
747 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200748 }
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100749
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100750 list_for_each_entry(lst, &expr->patterns, list) {
751 pattern = &lst->pat;
752
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200753 if (pattern->len > smp->data.u.str.data)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100754 continue;
755
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200756 end = smp->data.u.str.area + smp->data.u.str.data - pattern->len;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200757 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100758 if (icase) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200759 for (c = smp->data.u.str.area; c <= end; c++) {
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100760 if (tolower(*c) != tolower(*pattern->ptr.str))
761 continue;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200762 if (strncasecmp(pattern->ptr.str, c, pattern->len) == 0) {
763 ret = pattern;
764 goto leave;
765 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100766 }
767 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200768 for (c = smp->data.u.str.area; c <= end; c++) {
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100769 if (*c != *pattern->ptr.str)
770 continue;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200771 if (strncmp(pattern->ptr.str, c, pattern->len) == 0) {
772 ret = pattern;
773 goto leave;
774 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100775 }
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100776 }
777 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200778 leave:
Willy Tarreau7fdd81c2019-10-23 06:59:31 +0200779 if (lru)
Emeric Brunb5997f72017-07-03 11:34:05 +0200780 lru64_commit(lru, ret, expr, expr->revision, NULL);
Willy Tarreauf3045d22015-04-29 16:24:50 +0200781
782 return ret;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100783}
784
785/* This one is used by other real functions. It checks that the pattern is
786 * included inside the tested string, but enclosed between the specified
787 * delimiters or at the beginning or end of the string. The delimiters are
788 * provided as an unsigned int made by make_4delim() and match up to 4 different
789 * delimiters. Delimiters are stripped at the beginning and end of the pattern.
790 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200791static int match_word(struct sample *smp, struct pattern *pattern, int mflags, unsigned int delimiters)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100792{
793 int may_match, icase;
794 char *c, *end;
795 char *ps;
796 int pl;
797
798 pl = pattern->len;
799 ps = pattern->ptr.str;
800
801 while (pl > 0 && is_delimiter(*ps, delimiters)) {
802 pl--;
803 ps++;
804 }
805
806 while (pl > 0 && is_delimiter(ps[pl - 1], delimiters))
807 pl--;
808
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200809 if (pl > smp->data.u.str.data)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100810 return PAT_NOMATCH;
811
812 may_match = 1;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200813 icase = mflags & PAT_MF_IGNORE_CASE;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200814 end = smp->data.u.str.area + smp->data.u.str.data - pl;
815 for (c = smp->data.u.str.area; c <= end; c++) {
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100816 if (is_delimiter(*c, delimiters)) {
817 may_match = 1;
818 continue;
819 }
820
821 if (!may_match)
822 continue;
823
824 if (icase) {
825 if ((tolower(*c) == tolower(*ps)) &&
826 (strncasecmp(ps, c, pl) == 0) &&
827 (c == end || is_delimiter(c[pl], delimiters)))
828 return PAT_MATCH;
829 } else {
830 if ((*c == *ps) &&
831 (strncmp(ps, c, pl) == 0) &&
832 (c == end || is_delimiter(c[pl], delimiters)))
833 return PAT_MATCH;
834 }
835 may_match = 0;
836 }
837 return PAT_NOMATCH;
838}
839
840/* Checks that the pattern is included inside the tested string, but enclosed
841 * between the delimiters '?' or '/' or at the beginning or end of the string.
842 * Delimiters at the beginning or end of the pattern are ignored.
843 */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100844struct pattern *pat_match_dir(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100845{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100846 struct pattern_list *lst;
847 struct pattern *pattern;
848
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100849 list_for_each_entry(lst, &expr->patterns, list) {
850 pattern = &lst->pat;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200851 if (match_word(smp, pattern, expr->mflags, make_4delim('/', '?', '?', '?')))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100852 return pattern;
853 }
854 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100855}
856
857/* Checks that the pattern is included inside the tested string, but enclosed
858 * between the delmiters '/', '?', '.' or ":" or at the beginning or end of
859 * the string. Delimiters at the beginning or end of the pattern are ignored.
860 */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100861struct pattern *pat_match_dom(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100862{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100863 struct pattern_list *lst;
864 struct pattern *pattern;
865
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100866 list_for_each_entry(lst, &expr->patterns, list) {
867 pattern = &lst->pat;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200868 if (match_word(smp, pattern, expr->mflags, make_4delim('/', '?', '.', ':')))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100869 return pattern;
870 }
871 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100872}
873
874/* Checks that the integer in <test> is included between min and max */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100875struct pattern *pat_match_int(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100876{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100877 struct pattern_list *lst;
878 struct pattern *pattern;
879
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100880 list_for_each_entry(lst, &expr->patterns, list) {
881 pattern = &lst->pat;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200882 if ((!pattern->val.range.min_set || pattern->val.range.min <= smp->data.u.sint) &&
883 (!pattern->val.range.max_set || smp->data.u.sint <= pattern->val.range.max))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100884 return pattern;
885 }
886 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100887}
888
889/* Checks that the length of the pattern in <test> is included between min and max */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100890struct pattern *pat_match_len(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100891{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100892 struct pattern_list *lst;
893 struct pattern *pattern;
894
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100895 list_for_each_entry(lst, &expr->patterns, list) {
896 pattern = &lst->pat;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200897 if ((!pattern->val.range.min_set || pattern->val.range.min <= smp->data.u.str.data) &&
898 (!pattern->val.range.max_set || smp->data.u.str.data <= pattern->val.range.max))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100899 return pattern;
900 }
901 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100902}
903
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100904struct pattern *pat_match_ip(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100905{
906 unsigned int v4; /* in network byte order */
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100907 struct in6_addr tmp6;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100908 struct in_addr *s;
909 struct ebmb_node *node;
910 struct pattern_tree *elt;
911 struct pattern_list *lst;
912 struct pattern *pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100913
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100914 /* The input sample is IPv4. Try to match in the trees. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200915 if (smp->data.type == SMP_T_IPV4) {
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100916 /* Lookup an IPv4 address in the expression's pattern tree using
917 * the longest match method.
918 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200919 s = &smp->data.u.ipv4;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100920 node = ebmb_lookup_longest(&expr->pattern_tree, &s->s_addr);
921 if (node) {
922 if (fill) {
923 elt = ebmb_entry(node, struct pattern_tree, node);
Thierry FOURNIER503bb092015-08-19 08:35:43 +0200924 static_pattern.data = elt->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +0100925 static_pattern.ref = elt->ref;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200926 static_pattern.sflags = PAT_SF_TREE;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100927 static_pattern.type = SMP_T_IPV4;
Willy Tarreau33ccf1c2019-06-16 18:40:33 +0200928 static_pattern.val.ipv4.addr = *(struct in_addr *)elt->node.key;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100929 if (!cidr2dotted(elt->node.node.pfx, &static_pattern.val.ipv4.mask))
930 return NULL;
931 }
932 return &static_pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100933 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100934
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100935 /* The IPv4 sample dont match the IPv4 tree. Convert the IPv4
936 * sample address to IPv6 with the mapping method using the ::ffff:
937 * prefix, and try to lookup in the IPv6 tree.
938 */
939 memset(&tmp6, 0, 10);
940 *(uint16_t*)&tmp6.s6_addr[10] = htons(0xffff);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200941 *(uint32_t*)&tmp6.s6_addr[12] = smp->data.u.ipv4.s_addr;
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100942 node = ebmb_lookup_longest(&expr->pattern_tree_2, &tmp6);
943 if (node) {
944 if (fill) {
945 elt = ebmb_entry(node, struct pattern_tree, node);
Thierry FOURNIER503bb092015-08-19 08:35:43 +0200946 static_pattern.data = elt->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +0100947 static_pattern.ref = elt->ref;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200948 static_pattern.sflags = PAT_SF_TREE;
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100949 static_pattern.type = SMP_T_IPV6;
Willy Tarreau33ccf1c2019-06-16 18:40:33 +0200950 static_pattern.val.ipv6.addr = *(struct in6_addr *)elt->node.key;
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100951 static_pattern.val.ipv6.mask = elt->node.node.pfx;
952 }
953 return &static_pattern;
954 }
955 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100956
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100957 /* The input sample is IPv6. Try to match in the trees. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200958 if (smp->data.type == SMP_T_IPV6) {
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100959 /* Lookup an IPv6 address in the expression's pattern tree using
960 * the longest match method.
961 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200962 node = ebmb_lookup_longest(&expr->pattern_tree_2, &smp->data.u.ipv6);
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100963 if (node) {
964 if (fill) {
965 elt = ebmb_entry(node, struct pattern_tree, node);
Thierry FOURNIER503bb092015-08-19 08:35:43 +0200966 static_pattern.data = elt->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +0100967 static_pattern.ref = elt->ref;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200968 static_pattern.sflags = PAT_SF_TREE;
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100969 static_pattern.type = SMP_T_IPV6;
Willy Tarreau33ccf1c2019-06-16 18:40:33 +0200970 static_pattern.val.ipv6.addr = *(struct in6_addr *)elt->node.key;
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100971 static_pattern.val.ipv6.mask = elt->node.node.pfx;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100972 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100973 return &static_pattern;
974 }
975
976 /* Try to convert 6 to 4 when the start of the ipv6 address match the
977 * following forms :
978 * - ::ffff:ip:v4 (ipv4 mapped)
979 * - ::0000:ip:v4 (old ipv4 mapped)
980 * - 2002:ip:v4:: (6to4)
981 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200982 if ((*(uint32_t*)&smp->data.u.ipv6.s6_addr[0] == 0 &&
983 *(uint32_t*)&smp->data.u.ipv6.s6_addr[4] == 0 &&
984 (*(uint32_t*)&smp->data.u.ipv6.s6_addr[8] == 0 ||
985 *(uint32_t*)&smp->data.u.ipv6.s6_addr[8] == htonl(0xFFFF))) ||
986 *(uint16_t*)&smp->data.u.ipv6.s6_addr[0] == htons(0x2002)) {
987 if (*(uint32_t*)&smp->data.u.ipv6.s6_addr[0] == 0)
988 v4 = *(uint32_t*)&smp->data.u.ipv6.s6_addr[12];
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100989 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200990 v4 = htonl((ntohs(*(uint16_t*)&smp->data.u.ipv6.s6_addr[2]) << 16) +
991 ntohs(*(uint16_t*)&smp->data.u.ipv6.s6_addr[4]));
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100992
993 /* Lookup an IPv4 address in the expression's pattern tree using the longest
994 * match method.
995 */
996 node = ebmb_lookup_longest(&expr->pattern_tree, &v4);
997 if (node) {
998 if (fill) {
999 elt = ebmb_entry(node, struct pattern_tree, node);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001000 static_pattern.data = elt->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01001001 static_pattern.ref = elt->ref;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02001002 static_pattern.sflags = PAT_SF_TREE;
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001003 static_pattern.type = SMP_T_IPV4;
Willy Tarreau33ccf1c2019-06-16 18:40:33 +02001004 static_pattern.val.ipv4.addr = *(struct in_addr *)elt->node.key;
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001005 if (!cidr2dotted(elt->node.node.pfx, &static_pattern.val.ipv4.mask))
1006 return NULL;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01001007 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001008 return &static_pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +01001009 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001010 }
1011 }
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +01001012
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001013 /* Lookup in the list. the list contain only IPv4 patterns */
1014 list_for_each_entry(lst, &expr->patterns, list) {
1015 pattern = &lst->pat;
1016
1017 /* The input sample is IPv4, use it as is. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001018 if (smp->data.type == SMP_T_IPV4) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001019 v4 = smp->data.u.ipv4.s_addr;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +01001020 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001021 else if (smp->data.type == SMP_T_IPV6) {
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001022 /* v4 match on a V6 sample. We want to check at least for
1023 * the following forms :
1024 * - ::ffff:ip:v4 (ipv4 mapped)
1025 * - ::0000:ip:v4 (old ipv4 mapped)
1026 * - 2002:ip:v4:: (6to4)
1027 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001028 if (*(uint32_t*)&smp->data.u.ipv6.s6_addr[0] == 0 &&
1029 *(uint32_t*)&smp->data.u.ipv6.s6_addr[4] == 0 &&
1030 (*(uint32_t*)&smp->data.u.ipv6.s6_addr[8] == 0 ||
1031 *(uint32_t*)&smp->data.u.ipv6.s6_addr[8] == htonl(0xFFFF))) {
1032 v4 = *(uint32_t*)&smp->data.u.ipv6.s6_addr[12];
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01001033 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001034 else if (*(uint16_t*)&smp->data.u.ipv6.s6_addr[0] == htons(0x2002)) {
1035 v4 = htonl((ntohs(*(uint16_t*)&smp->data.u.ipv6.s6_addr[2]) << 16) +
1036 ntohs(*(uint16_t*)&smp->data.u.ipv6.s6_addr[4]));
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01001037 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001038 else
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01001039 continue;
Andreas Seltenreichf0653192016-03-03 20:08:35 +01001040 } else {
1041 /* impossible */
1042 continue;
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001043 }
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +01001044
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001045 /* Check if the input sample match the current pattern. */
1046 if (((v4 ^ pattern->val.ipv4.addr.s_addr) & pattern->val.ipv4.mask.s_addr) == 0)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01001047 return pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +01001048 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01001049 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +01001050}
1051
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001052void free_pattern_tree(struct eb_root *root)
1053{
1054 struct eb_node *node, *next;
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +01001055 struct pattern_tree *elt;
Thierry FOURNIER3ce88c72013-12-09 11:29:46 +01001056
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001057 node = eb_first(root);
1058 while (node) {
1059 next = eb_next(node);
1060 eb_delete(node);
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +01001061 elt = container_of(node, struct pattern_tree, node);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001062 free(elt->data);
Thierry FOURNIER3ce88c72013-12-09 11:29:46 +01001063 free(elt);
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001064 node = next;
1065 }
1066}
1067
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01001068void pat_prune_val(struct pattern_expr *expr)
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +01001069{
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01001070 struct pattern_list *pat, *tmp;
1071
1072 list_for_each_entry_safe(pat, tmp, &expr->patterns, list) {
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001073 free(pat->pat.data);
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01001074 free(pat);
1075 }
1076
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +01001077 free_pattern_tree(&expr->pattern_tree);
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001078 free_pattern_tree(&expr->pattern_tree_2);
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +01001079 LIST_INIT(&expr->patterns);
1080}
1081
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01001082void pat_prune_ptr(struct pattern_expr *expr)
1083{
1084 struct pattern_list *pat, *tmp;
1085
1086 list_for_each_entry_safe(pat, tmp, &expr->patterns, list) {
1087 free(pat->pat.ptr.ptr);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001088 free(pat->pat.data);
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01001089 free(pat);
1090 }
1091
1092 free_pattern_tree(&expr->pattern_tree);
1093 free_pattern_tree(&expr->pattern_tree_2);
1094 LIST_INIT(&expr->patterns);
1095}
1096
1097void pat_prune_reg(struct pattern_expr *expr)
1098{
1099 struct pattern_list *pat, *tmp;
1100
1101 list_for_each_entry_safe(pat, tmp, &expr->patterns, list) {
1102 regex_free(pat->pat.ptr.ptr);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001103 free(pat->pat.data);
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01001104 free(pat);
1105 }
1106
1107 free_pattern_tree(&expr->pattern_tree);
1108 free_pattern_tree(&expr->pattern_tree_2);
1109 LIST_INIT(&expr->patterns);
1110}
1111
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001112/*
1113 *
1114 * The following functions are used for the pattern indexation
1115 *
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001116 */
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001117
1118int pat_idx_list_val(struct pattern_expr *expr, struct pattern *pat, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001119{
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001120 struct pattern_list *patl;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001121
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001122 /* allocate pattern */
1123 patl = calloc(1, sizeof(*patl));
1124 if (!patl) {
1125 memprintf(err, "out of memory while indexing pattern");
Thierry FOURNIER972028f2014-01-23 17:53:31 +01001126 return 0;
1127 }
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001128
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001129 /* duplicate pattern */
1130 memcpy(&patl->pat, pat, sizeof(*pat));
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001131
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001132 /* chain pattern in the expression */
1133 LIST_ADDQ(&expr->patterns, &patl->list);
Willy Tarreau72f073b2015-04-29 17:53:47 +02001134 expr->revision = rdtsc();
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001135
1136 /* that's ok */
1137 return 1;
1138}
1139
1140int pat_idx_list_ptr(struct pattern_expr *expr, struct pattern *pat, char **err)
1141{
1142 struct pattern_list *patl;
1143
1144 /* allocate pattern */
1145 patl = calloc(1, sizeof(*patl));
Thierry FOURNIER8aa83842015-02-06 17:50:55 +01001146 if (!patl) {
1147 memprintf(err, "out of memory while indexing pattern");
Thierry FOURNIER972028f2014-01-23 17:53:31 +01001148 return 0;
Thierry FOURNIER8aa83842015-02-06 17:50:55 +01001149 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001150
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001151 /* duplicate pattern */
1152 memcpy(&patl->pat, pat, sizeof(*pat));
1153 patl->pat.ptr.ptr = malloc(patl->pat.len);
1154 if (!patl->pat.ptr.ptr) {
1155 free(patl);
1156 memprintf(err, "out of memory while indexing pattern");
1157 return 0;
1158 }
1159 memcpy(patl->pat.ptr.ptr, pat->ptr.ptr, pat->len);
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001160
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001161 /* chain pattern in the expression */
1162 LIST_ADDQ(&expr->patterns, &patl->list);
Willy Tarreau72f073b2015-04-29 17:53:47 +02001163 expr->revision = rdtsc();
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001164
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001165 /* that's ok */
1166 return 1;
1167}
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001168
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001169int pat_idx_list_str(struct pattern_expr *expr, struct pattern *pat, char **err)
1170{
1171 struct pattern_list *patl;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001172
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001173 /* allocate pattern */
1174 patl = calloc(1, sizeof(*patl));
1175 if (!patl) {
1176 memprintf(err, "out of memory while indexing pattern");
1177 return 0;
1178 }
1179
1180 /* duplicate pattern */
1181 memcpy(&patl->pat, pat, sizeof(*pat));
1182 patl->pat.ptr.str = malloc(patl->pat.len + 1);
1183 if (!patl->pat.ptr.str) {
1184 free(patl);
1185 memprintf(err, "out of memory while indexing pattern");
1186 return 0;
1187 }
1188 memcpy(patl->pat.ptr.ptr, pat->ptr.ptr, pat->len);
1189 patl->pat.ptr.str[patl->pat.len] = '\0';
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001190
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001191 /* chain pattern in the expression */
1192 LIST_ADDQ(&expr->patterns, &patl->list);
Willy Tarreau72f073b2015-04-29 17:53:47 +02001193 expr->revision = rdtsc();
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001194
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001195 /* that's ok */
1196 return 1;
1197}
1198
Thierry Fournier8feaa662016-02-10 22:55:20 +01001199int pat_idx_list_reg_cap(struct pattern_expr *expr, struct pattern *pat, int cap, char **err)
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001200{
1201 struct pattern_list *patl;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001202
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001203 /* allocate pattern */
1204 patl = calloc(1, sizeof(*patl));
1205 if (!patl) {
1206 memprintf(err, "out of memory while indexing pattern");
1207 return 0;
Thierry FOURNIER972028f2014-01-23 17:53:31 +01001208 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001209
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001210 /* duplicate pattern */
1211 memcpy(&patl->pat, pat, sizeof(*pat));
1212
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001213 /* compile regex */
Dragan Dosen26743032019-04-30 15:54:36 +02001214 if (!(patl->pat.ptr.reg = regex_comp(pat->ptr.str, !(expr->mflags & PAT_MF_IGNORE_CASE),
1215 cap, err))) {
Dirkjan Bussink07fcaaa2014-04-28 22:57:16 +00001216 free(patl);
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001217 return 0;
1218 }
1219
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001220 /* chain pattern in the expression */
1221 LIST_ADDQ(&expr->patterns, &patl->list);
Willy Tarreau72f073b2015-04-29 17:53:47 +02001222 expr->revision = rdtsc();
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001223
1224 /* that's ok */
1225 return 1;
1226}
1227
Thierry Fournier8feaa662016-02-10 22:55:20 +01001228int pat_idx_list_reg(struct pattern_expr *expr, struct pattern *pat, char **err)
1229{
1230 return pat_idx_list_reg_cap(expr, pat, 0, err);
1231}
1232
1233int pat_idx_list_regm(struct pattern_expr *expr, struct pattern *pat, char **err)
1234{
1235 return pat_idx_list_reg_cap(expr, pat, 1, err);
1236}
1237
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001238int pat_idx_tree_ip(struct pattern_expr *expr, struct pattern *pat, char **err)
1239{
1240 unsigned int mask;
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +01001241 struct pattern_tree *node;
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001242
1243 /* Only IPv4 can be indexed */
1244 if (pat->type == SMP_T_IPV4) {
Thierry FOURNIER972028f2014-01-23 17:53:31 +01001245 /* in IPv4 case, check if the mask is contiguous so that we can
1246 * insert the network into the tree. A continuous mask has only
1247 * ones on the left. This means that this mask + its lower bit
1248 * added once again is null.
1249 */
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001250 mask = ntohl(pat->val.ipv4.mask.s_addr);
1251 if (mask + (mask & -mask) == 0) {
1252 mask = mask ? 33 - flsnz(mask & -mask) : 0; /* equals cidr value */
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001253
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001254 /* node memory allocation */
1255 node = calloc(1, sizeof(*node) + 4);
1256 if (!node) {
1257 memprintf(err, "out of memory while loading pattern");
1258 return 0;
1259 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001260
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001261 /* copy the pointer to sample associated to this node */
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001262 node->data = pat->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01001263 node->ref = pat->ref;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001264
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001265 /* FIXME: insert <addr>/<mask> into the tree here */
1266 memcpy(node->node.key, &pat->val.ipv4.addr, 4); /* network byte order */
1267 node->node.node.pfx = mask;
Thierry FOURNIER31db4ae2014-01-30 00:27:15 +01001268
1269 /* Insert the entry. */
1270 ebmb_insert_prefix(&expr->pattern_tree, &node->node, 4);
Willy Tarreau72f073b2015-04-29 17:53:47 +02001271 expr->revision = rdtsc();
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001272
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001273 /* that's ok */
1274 return 1;
1275 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001276 else {
1277 /* If the mask is not contiguous, just add the pattern to the list */
1278 return pat_idx_list_val(expr, pat, err);
1279 }
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001280 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001281 else if (pat->type == SMP_T_IPV6) {
1282 /* IPv6 also can be indexed */
1283 node = calloc(1, sizeof(*node) + 16);
1284 if (!node) {
1285 memprintf(err, "out of memory while loading pattern");
1286 return 0;
1287 }
1288
1289 /* copy the pointer to sample associated to this node */
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001290 node->data = pat->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01001291 node->ref = pat->ref;
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001292
1293 /* FIXME: insert <addr>/<mask> into the tree here */
1294 memcpy(node->node.key, &pat->val.ipv6.addr, 16); /* network byte order */
1295 node->node.node.pfx = pat->val.ipv6.mask;
Thierry FOURNIER31db4ae2014-01-30 00:27:15 +01001296
1297 /* Insert the entry. */
1298 ebmb_insert_prefix(&expr->pattern_tree_2, &node->node, 16);
Willy Tarreau72f073b2015-04-29 17:53:47 +02001299 expr->revision = rdtsc();
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001300
1301 /* that's ok */
1302 return 1;
1303 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001304
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001305 return 0;
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001306}
1307
1308int pat_idx_tree_str(struct pattern_expr *expr, struct pattern *pat, char **err)
1309{
1310 int len;
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +01001311 struct pattern_tree *node;
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001312
1313 /* Only string can be indexed */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001314 if (pat->type != SMP_T_STR) {
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001315 memprintf(err, "internal error: string expected, but the type is '%s'",
1316 smp_to_type[pat->type]);
1317 return 0;
Thierry FOURNIER972028f2014-01-23 17:53:31 +01001318 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001319
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001320 /* If the flag PAT_F_IGNORE_CASE is set, we cannot use trees */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02001321 if (expr->mflags & PAT_MF_IGNORE_CASE)
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001322 return pat_idx_list_str(expr, pat, err);
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001323
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001324 /* Process the key len */
1325 len = strlen(pat->ptr.str) + 1;
1326
1327 /* node memory allocation */
1328 node = calloc(1, sizeof(*node) + len);
1329 if (!node) {
1330 memprintf(err, "out of memory while loading pattern");
1331 return 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001332 }
1333
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001334 /* copy the pointer to sample associated to this node */
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001335 node->data = pat->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01001336 node->ref = pat->ref;
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001337
1338 /* copy the string */
1339 memcpy(node->node.key, pat->ptr.str, len);
1340
1341 /* index the new node */
Thierry FOURNIER31db4ae2014-01-30 00:27:15 +01001342 ebst_insert(&expr->pattern_tree, &node->node);
Willy Tarreau72f073b2015-04-29 17:53:47 +02001343 expr->revision = rdtsc();
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001344
1345 /* that's ok */
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001346 return 1;
1347}
1348
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +02001349int pat_idx_tree_pfx(struct pattern_expr *expr, struct pattern *pat, char **err)
1350{
1351 int len;
1352 struct pattern_tree *node;
1353
1354 /* Only string can be indexed */
1355 if (pat->type != SMP_T_STR) {
1356 memprintf(err, "internal error: string expected, but the type is '%s'",
1357 smp_to_type[pat->type]);
1358 return 0;
1359 }
1360
1361 /* If the flag PAT_F_IGNORE_CASE is set, we cannot use trees */
1362 if (expr->mflags & PAT_MF_IGNORE_CASE)
1363 return pat_idx_list_str(expr, pat, err);
1364
1365 /* Process the key len */
1366 len = strlen(pat->ptr.str);
1367
1368 /* node memory allocation */
1369 node = calloc(1, sizeof(*node) + len + 1);
1370 if (!node) {
1371 memprintf(err, "out of memory while loading pattern");
1372 return 0;
1373 }
1374
1375 /* copy the pointer to sample associated to this node */
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001376 node->data = pat->data;
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +02001377 node->ref = pat->ref;
1378
1379 /* copy the string and the trailing zero */
1380 memcpy(node->node.key, pat->ptr.str, len + 1);
1381 node->node.node.pfx = len * 8;
1382
1383 /* index the new node */
1384 ebmb_insert_prefix(&expr->pattern_tree, &node->node, len);
Willy Tarreau72f073b2015-04-29 17:53:47 +02001385 expr->revision = rdtsc();
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +02001386
1387 /* that's ok */
1388 return 1;
1389}
1390
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001391void pat_del_list_val(struct pattern_expr *expr, struct pat_ref_elt *ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001392{
1393 struct pattern_list *pat;
1394 struct pattern_list *safe;
1395
1396 list_for_each_entry_safe(pat, safe, &expr->patterns, list) {
1397 /* Check equality. */
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001398 if (pat->pat.ref != ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001399 continue;
1400
1401 /* Delete and free entry. */
1402 LIST_DEL(&pat->list);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001403 free(pat->pat.data);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001404 free(pat);
1405 }
Willy Tarreau72f073b2015-04-29 17:53:47 +02001406 expr->revision = rdtsc();
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001407}
1408
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001409void pat_del_tree_ip(struct pattern_expr *expr, struct pat_ref_elt *ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001410{
1411 struct ebmb_node *node, *next_node;
1412 struct pattern_tree *elt;
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001413
1414 /* browse each node of the tree for IPv4 addresses. */
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001415 for (node = ebmb_first(&expr->pattern_tree), next_node = node ? ebmb_next(node) : NULL;
1416 node;
1417 node = next_node, next_node = node ? ebmb_next(node) : NULL) {
1418 /* Extract container of the tree node. */
1419 elt = container_of(node, struct pattern_tree, node);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001420
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001421 /* Check equality. */
1422 if (elt->ref != ref)
1423 continue;
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001424
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001425 /* Delete and free entry. */
1426 ebmb_delete(node);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001427 free(elt->data);
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001428 free(elt);
1429 }
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001430
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001431 /* Browse each node of the list for IPv4 addresses. */
1432 pat_del_list_val(expr, ref);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001433
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001434 /* browse each node of the tree for IPv6 addresses. */
1435 for (node = ebmb_first(&expr->pattern_tree_2), next_node = node ? ebmb_next(node) : NULL;
1436 node;
1437 node = next_node, next_node = node ? ebmb_next(node) : NULL) {
1438 /* Extract container of the tree node. */
1439 elt = container_of(node, struct pattern_tree, node);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001440
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001441 /* Check equality. */
1442 if (elt->ref != ref)
1443 continue;
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001444
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001445 /* Delete and free entry. */
1446 ebmb_delete(node);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001447 free(elt->data);
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001448 free(elt);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001449 }
Willy Tarreau72f073b2015-04-29 17:53:47 +02001450 expr->revision = rdtsc();
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001451}
1452
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001453void pat_del_list_ptr(struct pattern_expr *expr, struct pat_ref_elt *ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001454{
1455 struct pattern_list *pat;
1456 struct pattern_list *safe;
1457
1458 list_for_each_entry_safe(pat, safe, &expr->patterns, list) {
1459 /* Check equality. */
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001460 if (pat->pat.ref != ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001461 continue;
1462
1463 /* Delete and free entry. */
1464 LIST_DEL(&pat->list);
1465 free(pat->pat.ptr.ptr);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001466 free(pat->pat.data);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001467 free(pat);
1468 }
Willy Tarreau72f073b2015-04-29 17:53:47 +02001469 expr->revision = rdtsc();
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001470}
1471
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001472void pat_del_tree_str(struct pattern_expr *expr, struct pat_ref_elt *ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001473{
1474 struct ebmb_node *node, *next_node;
1475 struct pattern_tree *elt;
1476
Thierry FOURNIER73bc2852015-02-06 17:53:54 +01001477 /* If the flag PAT_F_IGNORE_CASE is set, we cannot use trees */
1478 if (expr->mflags & PAT_MF_IGNORE_CASE)
1479 return pat_del_list_ptr(expr, ref);
1480
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001481 /* browse each node of the tree. */
1482 for (node = ebmb_first(&expr->pattern_tree), next_node = node ? ebmb_next(node) : NULL;
1483 node;
1484 node = next_node, next_node = node ? ebmb_next(node) : NULL) {
1485 /* Extract container of the tree node. */
1486 elt = container_of(node, struct pattern_tree, node);
1487
1488 /* Check equality. */
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001489 if (elt->ref != ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001490 continue;
1491
1492 /* Delete and free entry. */
1493 ebmb_delete(node);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001494 free(elt->data);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001495 free(elt);
1496 }
Willy Tarreau72f073b2015-04-29 17:53:47 +02001497 expr->revision = rdtsc();
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001498}
1499
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001500void pat_del_list_reg(struct pattern_expr *expr, struct pat_ref_elt *ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001501{
1502 struct pattern_list *pat;
1503 struct pattern_list *safe;
1504
1505 list_for_each_entry_safe(pat, safe, &expr->patterns, list) {
1506 /* Check equality. */
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001507 if (pat->pat.ref != ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001508 continue;
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001509
1510 /* Delete and free entry. */
1511 LIST_DEL(&pat->list);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001512 regex_free(pat->pat.ptr.ptr);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001513 free(pat->pat.data);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001514 free(pat);
1515 }
Willy Tarreau72f073b2015-04-29 17:53:47 +02001516 expr->revision = rdtsc();
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001517}
1518
1519void pattern_init_expr(struct pattern_expr *expr)
1520{
1521 LIST_INIT(&expr->patterns);
Willy Tarreau72f073b2015-04-29 17:53:47 +02001522 expr->revision = 0;
Thierry FOURNIER31db4ae2014-01-30 00:27:15 +01001523 expr->pattern_tree = EB_ROOT;
1524 expr->pattern_tree_2 = EB_ROOT;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001525}
1526
1527void pattern_init_head(struct pattern_head *head)
1528{
1529 LIST_INIT(&head->head);
1530}
1531
1532/* The following functions are relative to the management of the reference
1533 * lists. These lists are used to store the original pattern and associated
1534 * value as string form.
1535 *
1536 * This is used with modifiable ACL and MAPS
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001537 *
1538 * The pattern reference are stored with two identifiers: the unique_id and
1539 * the reference.
1540 *
1541 * The reference identify a file. Each file with the same name point to the
1542 * same reference. We can register many times one file. If the file is modified,
1543 * all his dependencies are also modified. The reference can be used with map or
1544 * acl.
1545 *
1546 * The unique_id identify inline acl. The unique id is unique for each acl.
1547 * You cannot force the same id in the configuration file, because this repoort
1548 * an error.
1549 *
1550 * A particular case appears if the filename is a number. In this case, the
1551 * unique_id is set with the number represented by the filename and the
1552 * reference is also set. This method prevent double unique_id.
1553 *
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001554 */
1555
1556/* This function lookup for reference. If the reference is found, they return
1557 * pointer to the struct pat_ref, else return NULL.
1558 */
1559struct pat_ref *pat_ref_lookup(const char *reference)
1560{
1561 struct pat_ref *ref;
1562
1563 list_for_each_entry(ref, &pattern_reference, list)
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001564 if (ref->reference && strcmp(reference, ref->reference) == 0)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001565 return ref;
1566 return NULL;
1567}
1568
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001569/* This function lookup for unique id. If the reference is found, they return
1570 * pointer to the struct pat_ref, else return NULL.
1571 */
1572struct pat_ref *pat_ref_lookupid(int unique_id)
1573{
1574 struct pat_ref *ref;
1575
1576 list_for_each_entry(ref, &pattern_reference, list)
1577 if (ref->unique_id == unique_id)
1578 return ref;
1579 return NULL;
1580}
1581
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001582/* This function remove all pattern matching the pointer <refelt> from
1583 * the the reference and from each expr member of the reference. This
1584 * function returns 1 if the deletion is done and return 0 is the entry
1585 * is not found.
1586 */
1587int pat_ref_delete_by_id(struct pat_ref *ref, struct pat_ref_elt *refelt)
1588{
1589 struct pattern_expr *expr;
1590 struct pat_ref_elt *elt, *safe;
Emeric Brun8d85aa42017-06-29 15:40:33 +02001591 struct bref *bref, *back;
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001592
1593 /* delete pattern from reference */
1594 list_for_each_entry_safe(elt, safe, &ref->head, list) {
1595 if (elt == refelt) {
Emeric Brun8d85aa42017-06-29 15:40:33 +02001596 list_for_each_entry_safe(bref, back, &elt->back_refs, users) {
1597 /*
1598 * we have to unlink all watchers. We must not relink them if
1599 * this elt was the last one in the list.
1600 */
1601 LIST_DEL(&bref->users);
1602 LIST_INIT(&bref->users);
1603 if (elt->list.n != &ref->head)
Willy Tarreau49ee3b22019-04-30 11:43:43 +02001604 LIST_ADDQ(&LIST_ELEM(elt->list.n, typeof(elt), list)->back_refs, &bref->users);
Emeric Brun8d85aa42017-06-29 15:40:33 +02001605 bref->ref = elt->list.n;
1606 }
peter caiaede6dd2015-10-07 00:07:43 -07001607 list_for_each_entry(expr, &ref->pat, list)
1608 pattern_delete(expr, elt);
1609
Emeric Brunb5997f72017-07-03 11:34:05 +02001610 /* pat_ref_elt is trashed once all expr
1611 are cleaned and there is no ref remaining */
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001612 LIST_DEL(&elt->list);
1613 free(elt->sample);
1614 free(elt->pattern);
1615 free(elt);
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001616 return 1;
1617 }
1618 }
1619 return 0;
1620}
1621
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001622/* This function remove all pattern match <key> from the the reference
Joseph Herlant4189d672018-11-15 10:22:31 -08001623 * and from each expr member of the reference. This function returns 1
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001624 * if the deletion is done and return 0 is the entry is not found.
1625 */
1626int pat_ref_delete(struct pat_ref *ref, const char *key)
1627{
1628 struct pattern_expr *expr;
1629 struct pat_ref_elt *elt, *safe;
Emeric Brun8d85aa42017-06-29 15:40:33 +02001630 struct bref *bref, *back;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001631 int found = 0;
1632
1633 /* delete pattern from reference */
1634 list_for_each_entry_safe(elt, safe, &ref->head, list) {
1635 if (strcmp(key, elt->pattern) == 0) {
Emeric Brun8d85aa42017-06-29 15:40:33 +02001636 list_for_each_entry_safe(bref, back, &elt->back_refs, users) {
1637 /*
1638 * we have to unlink all watchers. We must not relink them if
1639 * this elt was the last one in the list.
1640 */
1641 LIST_DEL(&bref->users);
1642 LIST_INIT(&bref->users);
1643 if (elt->list.n != &ref->head)
Willy Tarreau49ee3b22019-04-30 11:43:43 +02001644 LIST_ADDQ(&LIST_ELEM(elt->list.n, typeof(elt), list)->back_refs, &bref->users);
Emeric Brun8d85aa42017-06-29 15:40:33 +02001645 bref->ref = elt->list.n;
1646 }
Dirkjan Bussink07fcaaa2014-04-28 22:57:16 +00001647 list_for_each_entry(expr, &ref->pat, list)
1648 pattern_delete(expr, elt);
1649
Emeric Brunb5997f72017-07-03 11:34:05 +02001650 /* pat_ref_elt is trashed once all expr
1651 are cleaned and there is no ref remaining */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001652 LIST_DEL(&elt->list);
1653 free(elt->sample);
1654 free(elt->pattern);
1655 free(elt);
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001656
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001657 found = 1;
1658 }
1659 }
1660
1661 if (!found)
1662 return 0;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001663 return 1;
1664}
1665
Baptiste Assmann953f74d2014-04-25 16:57:03 +02001666/*
1667 * find and return an element <elt> matching <key> in a reference <ref>
1668 * return NULL if not found
1669 */
1670struct pat_ref_elt *pat_ref_find_elt(struct pat_ref *ref, const char *key)
1671{
1672 struct pat_ref_elt *elt;
1673
1674 list_for_each_entry(elt, &ref->head, list) {
1675 if (strcmp(key, elt->pattern) == 0)
1676 return elt;
1677 }
1678
1679 return NULL;
1680}
1681
1682
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001683 /* This function modify the sample of the first pattern that match the <key>. */
1684static inline int pat_ref_set_elt(struct pat_ref *ref, struct pat_ref_elt *elt,
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001685 const char *value, char **err)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001686{
1687 struct pattern_expr *expr;
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001688 struct sample_data **data;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001689 char *sample;
Thierry FOURNIER12ba0c22015-08-14 00:02:11 +02001690 struct sample_data test;
Thierry FOURNIER149e0fe2014-01-29 19:35:06 +01001691
1692 /* Try all needed converters. */
1693 list_for_each_entry(expr, &ref->pat, list) {
1694 if (!expr->pat_head->parse_smp)
1695 continue;
1696
1697 if (!expr->pat_head->parse_smp(value, &test)) {
1698 memprintf(err, "unable to parse '%s'", value);
1699 return 0;
1700 }
1701 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001702
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001703 /* Modify pattern from reference. */
1704 sample = strdup(value);
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001705 if (!sample) {
1706 memprintf(err, "out of memory error");
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001707 return 0;
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001708 }
Thierry FOURNIER149e0fe2014-01-29 19:35:06 +01001709 /* Load sample in each reference. All the conversion are tested
1710 * below, normally these calls dosn't fail.
1711 */
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001712 list_for_each_entry(expr, &ref->pat, list) {
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001713 if (!expr->pat_head->parse_smp)
1714 continue;
1715
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001716 HA_RWLOCK_WRLOCK(PATEXP_LOCK, &expr->lock);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001717 data = pattern_find_smp(expr, elt);
1718 if (data && *data && !expr->pat_head->parse_smp(sample, *data))
1719 *data = NULL;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001720 HA_RWLOCK_WRUNLOCK(PATEXP_LOCK, &expr->lock);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001721 }
1722
Emeric Brunb5997f72017-07-03 11:34:05 +02001723 /* free old sample only when all exprs are updated */
1724 free(elt->sample);
1725 elt->sample = sample;
1726
1727
Thierry FOURNIER149e0fe2014-01-29 19:35:06 +01001728 return 1;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001729}
1730
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001731/* This function modify the sample of the first pattern that match the <key>. */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001732int 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 +01001733{
1734 struct pat_ref_elt *elt;
1735
1736 /* Look for pattern in the reference. */
1737 list_for_each_entry(elt, &ref->head, list) {
1738 if (elt == refelt) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001739 if (!pat_ref_set_elt(ref, elt, value, err))
1740 return 0;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001741 return 1;
1742 }
1743 }
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001744
1745 memprintf(err, "key or pattern not found");
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001746 return 0;
1747}
1748
1749/* This function modify the sample of the first pattern that match the <key>. */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001750int pat_ref_set(struct pat_ref *ref, const char *key, const char *value, char **err)
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001751{
1752 struct pat_ref_elt *elt;
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001753 int found = 0;
1754 char *_merr;
1755 char **merr;
1756
1757 if (err) {
1758 merr = &_merr;
1759 *merr = NULL;
1760 }
1761 else
1762 merr = NULL;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001763
1764 /* Look for pattern in the reference. */
1765 list_for_each_entry(elt, &ref->head, list) {
1766 if (strcmp(key, elt->pattern) == 0) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001767 if (!pat_ref_set_elt(ref, elt, value, merr)) {
William Lallemand579fb252018-06-11 10:53:46 +02001768 if (err && merr) {
1769 if (!found) {
1770 *err = *merr;
1771 } else {
1772 memprintf(err, "%s, %s", *err, *merr);
1773 free(*merr);
1774 *merr = NULL;
1775 }
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001776 }
1777 }
1778 found = 1;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001779 }
1780 }
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001781
1782 if (!found) {
1783 memprintf(err, "entry not found");
1784 return 0;
1785 }
1786 return 1;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001787}
1788
Joseph Herlant4189d672018-11-15 10:22:31 -08001789/* This function creates a new reference. <ref> is the reference name.
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001790 * <flags> are PAT_REF_*. /!\ The reference is not checked, and must
1791 * be unique. The user must check the reference with "pat_ref_lookup()"
Joseph Herlant4189d672018-11-15 10:22:31 -08001792 * before calling this function. If the function fail, it return NULL,
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001793 * else return new struct pat_ref.
1794 */
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001795struct pat_ref *pat_ref_new(const char *reference, const char *display, unsigned int flags)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001796{
1797 struct pat_ref *ref;
1798
1799 ref = malloc(sizeof(*ref));
1800 if (!ref)
1801 return NULL;
1802
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001803 if (display) {
1804 ref->display = strdup(display);
1805 if (!ref->display) {
1806 free(ref);
1807 return NULL;
1808 }
1809 }
1810 else
1811 ref->display = NULL;
1812
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001813 ref->reference = strdup(reference);
1814 if (!ref->reference) {
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001815 free(ref->display);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001816 free(ref);
1817 return NULL;
1818 }
1819
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001820 ref->flags = flags;
1821 ref->unique_id = -1;
1822
1823 LIST_INIT(&ref->head);
1824 LIST_INIT(&ref->pat);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001825 HA_SPIN_INIT(&ref->lock);
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001826 LIST_ADDQ(&pattern_reference, &ref->list);
1827
1828 return ref;
1829}
1830
1831/* This function create new reference. <unique_id> is the unique id. If
1832 * the value of <unique_id> is -1, the unique id is calculated later.
1833 * <flags> are PAT_REF_*. /!\ The reference is not checked, and must
1834 * be unique. The user must check the reference with "pat_ref_lookup()"
1835 * or pat_ref_lookupid before calling this function. If the function
1836 * fail, it return NULL, else return new struct pat_ref.
1837 */
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001838struct pat_ref *pat_ref_newid(int unique_id, const char *display, unsigned int flags)
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001839{
1840 struct pat_ref *ref;
1841
1842 ref = malloc(sizeof(*ref));
1843 if (!ref)
1844 return NULL;
1845
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001846 if (display) {
1847 ref->display = strdup(display);
1848 if (!ref->display) {
1849 free(ref);
1850 return NULL;
1851 }
1852 }
1853 else
1854 ref->display = NULL;
1855
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001856 ref->reference = NULL;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001857 ref->flags = flags;
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001858 ref->unique_id = unique_id;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001859 LIST_INIT(&ref->head);
1860 LIST_INIT(&ref->pat);
Aurélien Nephtali564d15a2018-04-19 16:56:07 +02001861 HA_SPIN_INIT(&ref->lock);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001862 LIST_ADDQ(&pattern_reference, &ref->list);
1863
1864 return ref;
1865}
1866
1867/* This function adds entry to <ref>. It can failed with memory error.
1868 * If the function fails, it returns 0.
1869 */
1870int pat_ref_append(struct pat_ref *ref, char *pattern, char *sample, int line)
1871{
1872 struct pat_ref_elt *elt;
1873
1874 elt = malloc(sizeof(*elt));
1875 if (!elt)
1876 return 0;
1877
1878 elt->line = line;
1879
1880 elt->pattern = strdup(pattern);
1881 if (!elt->pattern) {
1882 free(elt);
1883 return 0;
1884 }
1885
1886 if (sample) {
1887 elt->sample = strdup(sample);
1888 if (!elt->sample) {
1889 free(elt->pattern);
1890 free(elt);
1891 return 0;
1892 }
1893 }
1894 else
1895 elt->sample = NULL;
1896
Emeric Brun8d85aa42017-06-29 15:40:33 +02001897 LIST_INIT(&elt->back_refs);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001898 LIST_ADDQ(&ref->head, &elt->list);
1899
1900 return 1;
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001901}
1902
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001903/* This function create sample found in <elt>, parse the pattern also
1904 * found in <elt> and insert it in <expr>. The function copy <patflags>
1905 * in <expr>. If the function fails, it returns0 and <err> is filled.
1906 * In succes case, the function returns 1.
1907 */
1908static inline
1909int pat_ref_push(struct pat_ref_elt *elt, struct pattern_expr *expr,
1910 int patflags, char **err)
1911{
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001912 struct sample_data *data;
Thierry FOURNIERd25c8422014-01-28 15:34:35 +01001913 struct pattern pattern;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001914
1915 /* Create sample */
1916 if (elt->sample && expr->pat_head->parse_smp) {
1917 /* New sample. */
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001918 data = malloc(sizeof(*data));
1919 if (!data)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001920 return 0;
1921
1922 /* Parse value. */
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001923 if (!expr->pat_head->parse_smp(elt->sample, data)) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001924 memprintf(err, "unable to parse '%s'", elt->sample);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001925 free(data);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001926 return 0;
1927 }
1928
1929 }
1930 else
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001931 data = NULL;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001932
Thierry FOURNIERd25c8422014-01-28 15:34:35 +01001933 /* initialise pattern */
1934 memset(&pattern, 0, sizeof(pattern));
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001935 pattern.data = data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01001936 pattern.ref = elt;
Thierry FOURNIERd25c8422014-01-28 15:34:35 +01001937
1938 /* parse pattern */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02001939 if (!expr->pat_head->parse(elt->pattern, &pattern, expr->mflags, err)) {
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001940 free(data);
Thierry FOURNIERd25c8422014-01-28 15:34:35 +01001941 return 0;
1942 }
1943
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001944 HA_RWLOCK_WRLOCK(PATEXP_LOCK, &expr->lock);
Thierry FOURNIERd25c8422014-01-28 15:34:35 +01001945 /* index pattern */
1946 if (!expr->pat_head->index(expr, &pattern, err)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001947 HA_RWLOCK_WRUNLOCK(PATEXP_LOCK, &expr->lock);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001948 free(data);
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001949 return 0;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001950 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001951 HA_RWLOCK_WRUNLOCK(PATEXP_LOCK, &expr->lock);
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001952
1953 return 1;
1954}
1955
Thierry FOURNIER31db4ae2014-01-30 00:27:15 +01001956/* This function adds entry to <ref>. It can failed with memory error. The new
1957 * entry is added at all the pattern_expr registered in this reference. The
1958 * function stop on the first error encountered. It returns 0 and err is
1959 * filled. If an error is encountered, the complete add operation is cancelled.
1960 * If the insertion is a success the function returns 1.
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001961 */
1962int pat_ref_add(struct pat_ref *ref,
1963 const char *pattern, const char *sample,
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02001964 char **err)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001965{
1966 struct pat_ref_elt *elt;
1967 struct pattern_expr *expr;
1968
1969 elt = malloc(sizeof(*elt));
1970 if (!elt) {
1971 memprintf(err, "out of memory error");
1972 return 0;
1973 }
1974
1975 elt->line = -1;
1976
1977 elt->pattern = strdup(pattern);
1978 if (!elt->pattern) {
1979 free(elt);
1980 memprintf(err, "out of memory error");
1981 return 0;
1982 }
1983
1984 if (sample) {
1985 elt->sample = strdup(sample);
1986 if (!elt->sample) {
1987 free(elt->pattern);
1988 free(elt);
1989 memprintf(err, "out of memory error");
1990 return 0;
1991 }
1992 }
1993 else
1994 elt->sample = NULL;
1995
Emeric Brun8d85aa42017-06-29 15:40:33 +02001996 LIST_INIT(&elt->back_refs);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001997 LIST_ADDQ(&ref->head, &elt->list);
1998
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001999 list_for_each_entry(expr, &ref->pat, list) {
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02002000 if (!pat_ref_push(elt, expr, 0, err)) {
Thierry FOURNIER31db4ae2014-01-30 00:27:15 +01002001 /* If the insertion fails, try to delete all the added entries. */
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01002002 pat_ref_delete_by_id(ref, elt);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002003 return 0;
2004 }
2005 }
Emeric Brunb5997f72017-07-03 11:34:05 +02002006
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002007 return 1;
2008}
2009
Joseph Herlant4189d672018-11-15 10:22:31 -08002010/* This function prunes <ref>, replaces all references by the references
2011 * of <replace>, and reindexes all the news values.
Thierry FOURNIER46006bd2014-03-21 21:45:15 +01002012 *
Joseph Herlant4189d672018-11-15 10:22:31 -08002013 * The patterns are loaded in best effort and the errors are ignored,
2014 * but written in the logs.
Thierry FOURNIER46006bd2014-03-21 21:45:15 +01002015 */
2016void pat_ref_reload(struct pat_ref *ref, struct pat_ref *replace)
2017{
2018 struct pattern_expr *expr;
Emeric Brunb5997f72017-07-03 11:34:05 +02002019 struct pat_ref_elt *elt, *safe;
2020 struct bref *bref, *back;
Emeric Brunb5997f72017-07-03 11:34:05 +02002021 struct pattern pattern;
Thierry FOURNIER46006bd2014-03-21 21:45:15 +01002022
Thierry FOURNIER46006bd2014-03-21 21:45:15 +01002023
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002024 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Emeric Brunb5997f72017-07-03 11:34:05 +02002025 list_for_each_entry(expr, &ref->pat, list) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002026 HA_RWLOCK_WRLOCK(PATEXP_LOCK, &expr->lock);
Emeric Brunb5997f72017-07-03 11:34:05 +02002027 }
2028
2029 /* all expr are locked, we can safely remove all pat_ref */
2030 list_for_each_entry_safe(elt, safe, &ref->head, list) {
2031 list_for_each_entry_safe(bref, back, &elt->back_refs, users) {
2032 /*
2033 * we have to unlink all watchers. We must not relink them if
2034 * this elt was the last one in the list.
2035 */
2036 LIST_DEL(&bref->users);
2037 LIST_INIT(&bref->users);
2038 if (elt->list.n != &ref->head)
Willy Tarreau49ee3b22019-04-30 11:43:43 +02002039 LIST_ADDQ(&LIST_ELEM(elt->list.n, typeof(elt), list)->back_refs, &bref->users);
Emeric Brunb5997f72017-07-03 11:34:05 +02002040 bref->ref = elt->list.n;
2041 }
2042 LIST_DEL(&elt->list);
2043 free(elt->pattern);
2044 free(elt->sample);
2045 free(elt);
2046 }
2047
2048 /* switch pat_ret_elt lists */
Thierry FOURNIER46006bd2014-03-21 21:45:15 +01002049 LIST_ADD(&replace->head, &ref->head);
2050 LIST_DEL(&replace->head);
2051
Emeric Brunb5997f72017-07-03 11:34:05 +02002052 list_for_each_entry(expr, &ref->pat, list) {
2053 expr->pat_head->prune(expr);
2054 list_for_each_entry(elt, &ref->head, list) {
Dragan Dosenf1474792018-09-18 20:18:09 +02002055 char *err = NULL;
2056 struct sample_data *data = NULL;
2057
Emeric Brunb5997f72017-07-03 11:34:05 +02002058 /* Create sample */
2059 if (elt->sample && expr->pat_head->parse_smp) {
2060 /* New sample. */
2061 data = malloc(sizeof(*data));
2062 if (!data)
2063 continue;
2064
2065 /* Parse value. */
2066 if (!expr->pat_head->parse_smp(elt->sample, data)) {
2067 memprintf(&err, "unable to parse '%s'", elt->sample);
2068 send_log(NULL, LOG_NOTICE, "%s", err);
2069 free(err);
2070 free(data);
2071 continue;
2072 }
2073
2074 }
Emeric Brunb5997f72017-07-03 11:34:05 +02002075
2076 /* initialise pattern */
2077 memset(&pattern, 0, sizeof(pattern));
2078 pattern.data = data;
2079 pattern.ref = elt;
2080
2081 /* parse pattern */
2082 if (!expr->pat_head->parse(elt->pattern, &pattern, expr->mflags, &err)) {
Thierry FOURNIER46006bd2014-03-21 21:45:15 +01002083 send_log(NULL, LOG_NOTICE, "%s", err);
2084 free(err);
Emeric Brunb5997f72017-07-03 11:34:05 +02002085 free(data);
2086 continue;
2087 }
2088
2089 /* index pattern */
2090 if (!expr->pat_head->index(expr, &pattern, &err)) {
2091 send_log(NULL, LOG_NOTICE, "%s", err);
2092 free(err);
2093 free(data);
2094 continue;
Thierry FOURNIER46006bd2014-03-21 21:45:15 +01002095 }
2096 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002097 HA_RWLOCK_WRUNLOCK(PATEXP_LOCK, &expr->lock);
Thierry FOURNIER46006bd2014-03-21 21:45:15 +01002098 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002099 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER46006bd2014-03-21 21:45:15 +01002100}
2101
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002102/* This function prune all entries of <ref>. This function
Willy Tarreaub88a37c2019-12-20 18:22:02 +01002103 * prunes the associated pattern_expr. It may return before the end of
2104 * the list is reached, returning 0, to yield. The caller must call it
2105 * again. Otherwise it returns 1 once done.
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002106 */
Willy Tarreaub88a37c2019-12-20 18:22:02 +01002107int pat_ref_prune(struct pat_ref *ref)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002108{
2109 struct pat_ref_elt *elt, *safe;
2110 struct pattern_expr *expr;
Emeric Brun8d85aa42017-06-29 15:40:33 +02002111 struct bref *bref, *back;
Willy Tarreaub88a37c2019-12-20 18:22:02 +01002112 int loops = 0;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002113
Emeric Brunb5997f72017-07-03 11:34:05 +02002114 list_for_each_entry(expr, &ref->pat, list) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002115 HA_RWLOCK_WRLOCK(PATEXP_LOCK, &expr->lock);
Emeric Brunb5997f72017-07-03 11:34:05 +02002116 expr->pat_head->prune(expr);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002117 HA_RWLOCK_WRUNLOCK(PATEXP_LOCK, &expr->lock);
Willy Tarreaub88a37c2019-12-20 18:22:02 +01002118 loops++;
2119 /* yield often, some lists may be huge, especially those
2120 * having to be freed through free_pattern_tree()
2121 */
2122 if (loops > 10)
2123 return 0;
Emeric Brunb5997f72017-07-03 11:34:05 +02002124 }
2125
2126 /* we trash pat_ref_elt in a second time to ensure that data is
2127 free once there is no ref on it */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002128 list_for_each_entry_safe(elt, safe, &ref->head, list) {
Emeric Brun8d85aa42017-06-29 15:40:33 +02002129 list_for_each_entry_safe(bref, back, &elt->back_refs, users) {
2130 /*
2131 * we have to unlink all watchers. We must not relink them if
2132 * this elt was the last one in the list.
2133 */
2134 LIST_DEL(&bref->users);
2135 LIST_INIT(&bref->users);
2136 if (elt->list.n != &ref->head)
Willy Tarreau49ee3b22019-04-30 11:43:43 +02002137 LIST_ADDQ(&LIST_ELEM(elt->list.n, typeof(elt), list)->back_refs, &bref->users);
Emeric Brun8d85aa42017-06-29 15:40:33 +02002138 bref->ref = elt->list.n;
2139 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002140 LIST_DEL(&elt->list);
2141 free(elt->pattern);
2142 free(elt->sample);
2143 free(elt);
Willy Tarreaub88a37c2019-12-20 18:22:02 +01002144 loops++;
2145 if (loops > 100000)
2146 return 0;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002147 }
Willy Tarreaub88a37c2019-12-20 18:22:02 +01002148 return 1;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002149}
2150
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002151/* This function lookup for existing reference <ref> in pattern_head <head>. */
2152struct pattern_expr *pattern_lookup_expr(struct pattern_head *head, struct pat_ref *ref)
2153{
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002154 struct pattern_expr_list *expr;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002155
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002156 list_for_each_entry(expr, &head->head, list)
2157 if (expr->expr->ref == ref)
2158 return expr->expr;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002159 return NULL;
2160}
2161
Joseph Herlant4189d672018-11-15 10:22:31 -08002162/* This function creates new pattern_expr associated to the reference <ref>.
2163 * <ref> can be NULL. If an error occurs, the function returns NULL and
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002164 * <err> is filled. Otherwise, the function returns new pattern_expr linked
2165 * with <head> and <ref>.
Thierry FOURNIER315ec422014-11-24 11:14:42 +01002166 *
Joseph Herlant4189d672018-11-15 10:22:31 -08002167 * The returned value can be an already filled pattern list, in this case the
Thierry FOURNIER315ec422014-11-24 11:14:42 +01002168 * flag <reuse> is set.
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002169 */
Thierry FOURNIER315ec422014-11-24 11:14:42 +01002170struct pattern_expr *pattern_new_expr(struct pattern_head *head, struct pat_ref *ref,
Emeric Brun7d27f3c2017-07-03 17:54:23 +02002171 int patflags, char **err, int *reuse)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002172{
2173 struct pattern_expr *expr;
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002174 struct pattern_expr_list *list;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002175
Thierry FOURNIER315ec422014-11-24 11:14:42 +01002176 if (reuse)
2177 *reuse = 0;
2178
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002179 /* Memory and initialization of the chain element. */
2180 list = malloc(sizeof(*list));
2181 if (!list) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002182 memprintf(err, "out of memory");
2183 return NULL;
2184 }
2185
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002186 /* Look for existing similar expr. No that only the index, parse and
2187 * parse_smp function must be identical for having similar pattern.
Joseph Herlant4189d672018-11-15 10:22:31 -08002188 * The other function depends of these first.
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002189 */
2190 if (ref) {
2191 list_for_each_entry(expr, &ref->pat, list)
2192 if (expr->pat_head->index == head->index &&
2193 expr->pat_head->parse == head->parse &&
Emeric Brun7d27f3c2017-07-03 17:54:23 +02002194 expr->pat_head->parse_smp == head->parse_smp &&
2195 expr->mflags == patflags)
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002196 break;
2197 if (&expr->list == &ref->pat)
2198 expr = NULL;
2199 }
2200 else
2201 expr = NULL;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002202
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002203 /* If no similar expr was found, we create new expr. */
2204 if (!expr) {
2205 /* Get a lot of memory for the expr struct. */
2206 expr = malloc(sizeof(*expr));
2207 if (!expr) {
Andreas Seltenreiche6e22e82016-03-03 20:20:23 +01002208 free(list);
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002209 memprintf(err, "out of memory");
2210 return NULL;
2211 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002212
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002213 /* Initialize this new expr. */
2214 pattern_init_expr(expr);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002215
Emeric Brun7d27f3c2017-07-03 17:54:23 +02002216 /* Copy the pattern matching and indexing flags. */
2217 expr->mflags = patflags;
2218
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002219 /* This new pattern expression reference one of his heads. */
2220 expr->pat_head = head;
2221
2222 /* Link with ref, or to self to facilitate LIST_DEL() */
2223 if (ref)
2224 LIST_ADDQ(&ref->pat, &expr->list);
2225 else
2226 LIST_INIT(&expr->list);
2227
2228 expr->ref = ref;
2229
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002230 HA_RWLOCK_INIT(&expr->lock);
Emeric Brunb5997f72017-07-03 11:34:05 +02002231
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002232 /* We must free this pattern if it is no more used. */
2233 list->do_free = 1;
2234 }
2235 else {
2236 /* If the pattern used already exists, it is already linked
2237 * with ref and we must not free it.
2238 */
2239 list->do_free = 0;
Thierry FOURNIER315ec422014-11-24 11:14:42 +01002240 if (reuse)
2241 *reuse = 1;
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002242 }
2243
2244 /* The new list element reference the pattern_expr. */
2245 list->expr = expr;
2246
2247 /* Link the list element with the pattern_head. */
2248 LIST_ADDQ(&head->head, &list->list);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002249 return expr;
2250}
2251
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002252/* Reads patterns from a file. If <err_msg> is non-NULL, an error message will
2253 * be returned there on errors and the caller will have to free it.
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002254 *
2255 * The file contains one key + value per line. Lines which start with '#' are
2256 * ignored, just like empty lines. Leading tabs/spaces are stripped. The key is
2257 * then the first "word" (series of non-space/tabs characters), and the value is
2258 * what follows this series of space/tab till the end of the line excluding
2259 * trailing spaces/tabs.
2260 *
2261 * Example :
2262 *
2263 * # this is a comment and is ignored
2264 * 62.212.114.60 1wt.eu \n
2265 * <-><-----------><---><----><---->
2266 * | | | | `--- trailing spaces ignored
2267 * | | | `-------- value
2268 * | | `--------------- middle spaces ignored
2269 * | `------------------------ key
2270 * `-------------------------------- leading spaces ignored
2271 *
2272 * Return non-zero in case of succes, otherwise 0.
2273 */
2274int pat_ref_read_from_file_smp(struct pat_ref *ref, const char *filename, char **err)
2275{
2276 FILE *file;
2277 char *c;
2278 int ret = 0;
2279 int line = 0;
2280 char *key_beg;
2281 char *key_end;
2282 char *value_beg;
2283 char *value_end;
2284
2285 file = fopen(filename, "r");
2286 if (!file) {
2287 memprintf(err, "failed to open pattern file <%s>", filename);
2288 return 0;
2289 }
2290
2291 /* now parse all patterns. The file may contain only one pattern
2292 * followed by one value per line. The start spaces, separator spaces
2293 * and and spaces are stripped. Each can contain comment started by '#'
2294 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002295 while (fgets(trash.area, trash.size, file) != NULL) {
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002296 line++;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002297 c = trash.area;
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002298
2299 /* ignore lines beginning with a dash */
2300 if (*c == '#')
2301 continue;
2302
2303 /* strip leading spaces and tabs */
2304 while (*c == ' ' || *c == '\t')
2305 c++;
2306
2307 /* empty lines are ignored too */
2308 if (*c == '\0' || *c == '\r' || *c == '\n')
2309 continue;
2310
2311 /* look for the end of the key */
2312 key_beg = c;
2313 while (*c && *c != ' ' && *c != '\t' && *c != '\n' && *c != '\r')
2314 c++;
2315
2316 key_end = c;
2317
2318 /* strip middle spaces and tabs */
2319 while (*c == ' ' || *c == '\t')
2320 c++;
2321
2322 /* look for the end of the value, it is the end of the line */
2323 value_beg = c;
2324 while (*c && *c != '\n' && *c != '\r')
2325 c++;
2326 value_end = c;
2327
2328 /* trim possibly trailing spaces and tabs */
2329 while (value_end > value_beg && (value_end[-1] == ' ' || value_end[-1] == '\t'))
2330 value_end--;
2331
2332 /* set final \0 and check entries */
2333 *key_end = '\0';
2334 *value_end = '\0';
2335
2336 /* insert values */
2337 if (!pat_ref_append(ref, key_beg, value_beg, line)) {
2338 memprintf(err, "out of memory");
2339 goto out_close;
2340 }
2341 }
2342
Jerome Magnin8991ce12020-01-17 16:09:33 +01002343 if (ferror(file)) {
2344 memprintf(err, "error encountered while reading <%s> : %s",
2345 filename, strerror(errno));
2346 goto out_close;
2347 }
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002348 /* succes */
2349 ret = 1;
2350
2351 out_close:
2352 fclose(file);
2353 return ret;
2354}
2355
2356/* Reads patterns from a file. If <err_msg> is non-NULL, an error message will
2357 * be returned there on errors and the caller will have to free it.
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002358 */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002359int pat_ref_read_from_file(struct pat_ref *ref, const char *filename, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002360{
2361 FILE *file;
2362 char *c;
2363 char *arg;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002364 int ret = 0;
2365 int line = 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002366
2367 file = fopen(filename, "r");
2368 if (!file) {
2369 memprintf(err, "failed to open pattern file <%s>", filename);
2370 return 0;
2371 }
2372
2373 /* now parse all patterns. The file may contain only one pattern per
2374 * line. If the line contains spaces, they will be part of the pattern.
2375 * The pattern stops at the first CR, LF or EOF encountered.
2376 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002377 while (fgets(trash.area, trash.size, file) != NULL) {
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002378 line++;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002379 c = trash.area;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002380
2381 /* ignore lines beginning with a dash */
2382 if (*c == '#')
2383 continue;
2384
2385 /* strip leading spaces and tabs */
2386 while (*c == ' ' || *c == '\t')
2387 c++;
2388
2389
2390 arg = c;
2391 while (*c && *c != '\n' && *c != '\r')
2392 c++;
2393 *c = 0;
2394
2395 /* empty lines are ignored too */
2396 if (c == arg)
2397 continue;
2398
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002399 if (!pat_ref_append(ref, arg, NULL, line)) {
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002400 memprintf(err, "out of memory when loading patterns from file <%s>", filename);
2401 goto out_close;
2402 }
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002403 }
2404
Jerome Magnin8991ce12020-01-17 16:09:33 +01002405 if (ferror(file)) {
2406 memprintf(err, "error encountered while reading <%s> : %s",
2407 filename, strerror(errno));
2408 goto out_close;
2409 }
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002410 ret = 1; /* success */
2411
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002412 out_close:
2413 fclose(file);
2414 return ret;
2415}
2416
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002417int pattern_read_from_file(struct pattern_head *head, unsigned int refflags,
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002418 const char *filename, int patflags, int load_smp,
Thierry FOURNIER94580c92014-02-11 14:36:45 +01002419 char **err, const char *file, int line)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002420{
2421 struct pat_ref *ref;
2422 struct pattern_expr *expr;
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002423 struct pat_ref_elt *elt;
Willy Tarreau4deaf392014-11-26 13:17:03 +01002424 int reuse = 0;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002425
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002426 /* Lookup for the existing reference. */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002427 ref = pat_ref_lookup(filename);
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002428
2429 /* If the reference doesn't exists, create it and load associated file. */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002430 if (!ref) {
Thierry FOURNIER94580c92014-02-11 14:36:45 +01002431 chunk_printf(&trash,
2432 "pattern loaded from file '%s' used by %s at file '%s' line %d",
2433 filename, refflags & PAT_REF_MAP ? "map" : "acl", file, line);
2434
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002435 ref = pat_ref_new(filename, trash.area, refflags);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002436 if (!ref) {
2437 memprintf(err, "out of memory");
2438 return 0;
2439 }
2440
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002441 if (load_smp) {
Thierry FOURNIERc0bd9102014-01-29 12:32:58 +01002442 ref->flags |= PAT_REF_SMP;
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002443 if (!pat_ref_read_from_file_smp(ref, filename, err))
2444 return 0;
2445 }
2446 else {
2447 if (!pat_ref_read_from_file(ref, filename, err))
2448 return 0;
2449 }
2450 }
2451 else {
Thierry FOURNIERc0bd9102014-01-29 12:32:58 +01002452 /* The reference already exists, check the map compatibility. */
2453
2454 /* If the load require samples and the flag PAT_REF_SMP is not set,
2455 * the reference doesn't contain sample, and cannot be used.
2456 */
2457 if (load_smp) {
2458 if (!(ref->flags & PAT_REF_SMP)) {
2459 memprintf(err, "The file \"%s\" is already used as one column file "
2460 "and cannot be used by as two column file.",
2461 filename);
2462 return 0;
2463 }
2464 }
2465 else {
2466 /* The load doesn't require samples. If the flag PAT_REF_SMP is
2467 * set, the reference contains a sample, and cannot be used.
2468 */
2469 if (ref->flags & PAT_REF_SMP) {
2470 memprintf(err, "The file \"%s\" is already used as two column file "
2471 "and cannot be used by as one column file.",
2472 filename);
2473 return 0;
2474 }
2475 }
2476
Thierry FOURNIER94580c92014-02-11 14:36:45 +01002477 /* Extends display */
2478 chunk_printf(&trash, "%s", ref->display);
2479 chunk_appendf(&trash, ", by %s at file '%s' line %d",
2480 refflags & PAT_REF_MAP ? "map" : "acl", file, line);
2481 free(ref->display);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002482 ref->display = strdup(trash.area);
Thierry FOURNIER94580c92014-02-11 14:36:45 +01002483 if (!ref->display) {
2484 memprintf(err, "out of memory");
2485 return 0;
2486 }
2487
Thierry FOURNIERc0bd9102014-01-29 12:32:58 +01002488 /* Merge flags. */
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002489 ref->flags |= refflags;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002490 }
2491
2492 /* Now, we can loading patterns from the reference. */
2493
2494 /* Lookup for existing reference in the head. If the reference
2495 * doesn't exists, create it.
2496 */
2497 expr = pattern_lookup_expr(head, ref);
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02002498 if (!expr || (expr->mflags != patflags)) {
Emeric Brun7d27f3c2017-07-03 17:54:23 +02002499 expr = pattern_new_expr(head, ref, patflags, err, &reuse);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002500 if (!expr)
2501 return 0;
2502 }
2503
Thierry FOURNIER315ec422014-11-24 11:14:42 +01002504 /* The returned expression may be not empty, because the function
2505 * "pattern_new_expr" lookup for similar pattern list and can
2506 * reuse a already filled pattern list. In this case, we can not
2507 * reload the patterns.
2508 */
2509 if (reuse)
2510 return 1;
2511
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002512 /* Load reference content in the pattern expression. */
2513 list_for_each_entry(elt, &ref->head, list) {
2514 if (!pat_ref_push(elt, expr, patflags, err)) {
2515 if (elt->line > 0)
2516 memprintf(err, "%s at line %d of file '%s'",
2517 *err, elt->line, filename);
2518 return 0;
2519 }
2520 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002521
2522 return 1;
2523}
2524
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01002525/* This function executes a pattern match on a sample. It applies pattern <expr>
2526 * to sample <smp>. The function returns NULL if the sample dont match. It returns
2527 * non-null if the sample match. If <fill> is true and the sample match, the
2528 * function returns the matched pattern. In many cases, this pattern can be a
2529 * static buffer.
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002530 */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002531struct pattern *pattern_exec_match(struct pattern_head *head, struct sample *smp, int fill)
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002532{
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002533 struct pattern_expr_list *list;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002534 struct pattern *pat;
2535
2536 if (!head->match) {
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01002537 if (fill) {
Thierry FOURNIER503bb092015-08-19 08:35:43 +02002538 static_pattern.data = NULL;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01002539 static_pattern.ref = NULL;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02002540 static_pattern.sflags = 0;
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002541 static_pattern.type = SMP_T_SINT;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01002542 static_pattern.val.i = 1;
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01002543 }
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01002544 return &static_pattern;
2545 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002546
Thierry FOURNIER5d344082014-01-27 14:19:53 +01002547 /* convert input to string */
2548 if (!sample_convert(smp, head->expect_type))
2549 return NULL;
2550
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002551 list_for_each_entry(list, &head->head, list) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002552 HA_RWLOCK_RDLOCK(PATEXP_LOCK, &list->expr->lock);
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002553 pat = head->match(smp, list->expr, fill);
Emeric Brunb5997f72017-07-03 11:34:05 +02002554 if (pat) {
2555 /* We duplicate the pattern cause it could be modified
2556 by another thread */
2557 if (pat != &static_pattern) {
2558 memcpy(&static_pattern, pat, sizeof(struct pattern));
2559 pat = &static_pattern;
2560 }
2561
2562 /* We also duplicate the sample data for
2563 same reason */
2564 if (pat->data && (pat->data != &static_sample_data)) {
Christopher Faulet09fdf4b2017-11-09 16:14:16 +01002565 switch(pat->data->type) {
Emeric Brunb5997f72017-07-03 11:34:05 +02002566 case SMP_T_STR:
2567 static_sample_data.type = SMP_T_STR;
2568 static_sample_data.u.str = *get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002569 static_sample_data.u.str.data = pat->data->u.str.data;
2570 if (static_sample_data.u.str.data >= static_sample_data.u.str.size)
2571 static_sample_data.u.str.data = static_sample_data.u.str.size - 1;
2572 memcpy(static_sample_data.u.str.area,
Willy Tarreau45b2d9a2020-06-11 16:37:35 +02002573 pat->data->u.str.area, static_sample_data.u.str.data);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002574 static_sample_data.u.str.area[static_sample_data.u.str.data] = 0;
Willy Tarreau45b2d9a2020-06-11 16:37:35 +02002575 pat->data = &static_sample_data;
2576 break;
2577
Emeric Brunb5997f72017-07-03 11:34:05 +02002578 case SMP_T_IPV4:
2579 case SMP_T_IPV6:
2580 case SMP_T_SINT:
2581 memcpy(&static_sample_data, pat->data, sizeof(struct sample_data));
Willy Tarreau45b2d9a2020-06-11 16:37:35 +02002582 pat->data = &static_sample_data;
2583 break;
Emeric Brunb5997f72017-07-03 11:34:05 +02002584 default:
Willy Tarreau45b2d9a2020-06-11 16:37:35 +02002585 /* unimplemented pattern type */
Emeric Brunb5997f72017-07-03 11:34:05 +02002586 pat->data = NULL;
Willy Tarreau45b2d9a2020-06-11 16:37:35 +02002587 break;
Emeric Brunb5997f72017-07-03 11:34:05 +02002588 }
Emeric Brunb5997f72017-07-03 11:34:05 +02002589 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002590 HA_RWLOCK_RDUNLOCK(PATEXP_LOCK, &list->expr->lock);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002591 return pat;
Emeric Brunb5997f72017-07-03 11:34:05 +02002592 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002593 HA_RWLOCK_RDUNLOCK(PATEXP_LOCK, &list->expr->lock);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002594 }
2595 return NULL;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002596}
2597
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01002598/* This function prune the pattern expression. */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002599void pattern_prune(struct pattern_head *head)
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01002600{
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002601 struct pattern_expr_list *list, *safe;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002602
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002603 list_for_each_entry_safe(list, safe, &head->head, list) {
2604 LIST_DEL(&list->list);
2605 if (list->do_free) {
2606 LIST_DEL(&list->expr->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002607 HA_RWLOCK_WRLOCK(PATEXP_LOCK, &list->expr->lock);
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002608 head->prune(list->expr);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002609 HA_RWLOCK_WRUNLOCK(PATEXP_LOCK, &list->expr->lock);
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002610 free(list->expr);
2611 }
2612 free(list);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002613 }
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01002614}
2615
Thierry FOURNIER55d0b102014-01-15 11:25:26 +01002616/* This function lookup for a pattern matching the <key> and return a
2617 * pointer to a pointer of the sample stoarge. If the <key> dont match,
2618 * the function returns NULL. If the key cannot be parsed, the function
2619 * fill <err>.
2620 */
Thierry FOURNIER12ba0c22015-08-14 00:02:11 +02002621struct sample_data **pattern_find_smp(struct pattern_expr *expr, struct pat_ref_elt *ref)
Thierry FOURNIER55d0b102014-01-15 11:25:26 +01002622{
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01002623 struct ebmb_node *node;
2624 struct pattern_tree *elt;
2625 struct pattern_list *pat;
Thierry FOURNIER55d0b102014-01-15 11:25:26 +01002626
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01002627 for (node = ebmb_first(&expr->pattern_tree);
2628 node;
2629 node = ebmb_next(node)) {
2630 elt = container_of(node, struct pattern_tree, node);
2631 if (elt->ref == ref)
Thierry FOURNIER503bb092015-08-19 08:35:43 +02002632 return &elt->data;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01002633 }
2634
2635 for (node = ebmb_first(&expr->pattern_tree_2);
2636 node;
2637 node = ebmb_next(node)) {
2638 elt = container_of(node, struct pattern_tree, node);
2639 if (elt->ref == ref)
Thierry FOURNIER503bb092015-08-19 08:35:43 +02002640 return &elt->data;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01002641 }
2642
2643 list_for_each_entry(pat, &expr->patterns, list)
2644 if (pat->pat.ref == ref)
Thierry FOURNIER503bb092015-08-19 08:35:43 +02002645 return &pat->pat.data;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01002646
2647 return NULL;
Thierry FOURNIER55d0b102014-01-15 11:25:26 +01002648}
2649
Thierry FOURNIERb1136502014-01-15 11:38:49 +01002650/* This function search all the pattern matching the <key> and delete it.
2651 * If the parsing of the input key fails, the function returns 0 and the
2652 * <err> is filled, else return 1;
2653 */
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01002654int pattern_delete(struct pattern_expr *expr, struct pat_ref_elt *ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01002655{
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002656 HA_RWLOCK_WRLOCK(PATEXP_LOCK, &expr->lock);
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01002657 expr->pat_head->delete(expr, ref);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002658 HA_RWLOCK_WRUNLOCK(PATEXP_LOCK, &expr->lock);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01002659 return 1;
2660}
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002661
Carl Henrik Lunde4944c8c2020-02-27 16:45:50 +01002662/* This function compares two pat_ref** on unique_id */
2663static int cmp_pat_ref(const void *_a, const void *_b)
2664{
2665 struct pat_ref * const *a = _a;
2666 struct pat_ref * const *b = _b;
2667
2668 if ((*a)->unique_id < (*b)->unique_id)
2669 return -1;
2670 else if ((*a)->unique_id > (*b)->unique_id)
2671 return 1;
2672 return 0;
2673}
2674
2675/* This function finalize the configuration parsing. It sets all the
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002676 * automatic ids
2677 */
Carl Henrik Lunde4944c8c2020-02-27 16:45:50 +01002678int pattern_finalize_config(void)
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002679{
Tim Duesterhusd5a8b9d2020-03-17 21:08:24 +01002680 size_t len = 0;
2681 size_t unassigned_pos = 0;
Carl Henrik Lunde4944c8c2020-02-27 16:45:50 +01002682 int next_unique_id = 0;
Tim Duesterhusd5a8b9d2020-03-17 21:08:24 +01002683 size_t i, j;
Carl Henrik Lunde4944c8c2020-02-27 16:45:50 +01002684 struct pat_ref *ref, **arr;
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002685 struct list pr = LIST_HEAD_INIT(pr);
2686
Willy Tarreau861c4ef2020-03-08 00:42:37 +01002687 pat_lru_seed = ha_random();
Willy Tarreauf3045d22015-04-29 16:24:50 +02002688
Carl Henrik Lunde4944c8c2020-02-27 16:45:50 +01002689 /* Count pat_refs with user defined unique_id and totalt count */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002690 list_for_each_entry(ref, &pattern_reference, list) {
Carl Henrik Lunde4944c8c2020-02-27 16:45:50 +01002691 len++;
2692 if (ref->unique_id != -1)
2693 unassigned_pos++;
2694 }
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002695
Tim Duesterhusd5a8b9d2020-03-17 21:08:24 +01002696 if (len == 0) {
2697 return 0;
2698 }
2699
Carl Henrik Lunde4944c8c2020-02-27 16:45:50 +01002700 arr = calloc(len, sizeof(*arr));
2701 if (arr == NULL) {
2702 ha_alert("Out of memory error.\n");
2703 return ERR_ALERT | ERR_FATAL;
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002704 }
2705
Carl Henrik Lunde4944c8c2020-02-27 16:45:50 +01002706 i = 0;
2707 j = unassigned_pos;
2708 list_for_each_entry(ref, &pattern_reference, list) {
2709 if (ref->unique_id != -1)
2710 arr[i++] = ref;
2711 else
2712 arr[j++] = ref;
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002713 }
2714
Carl Henrik Lunde4944c8c2020-02-27 16:45:50 +01002715 /* Sort first segment of array with user-defined unique ids for
2716 * fast lookup when generating unique ids
2717 */
2718 qsort(arr, unassigned_pos, sizeof(*arr), cmp_pat_ref);
2719
2720 /* Assign unique ids to the rest of the elements */
2721 for (i = unassigned_pos; i < len; i++) {
2722 do {
2723 arr[i]->unique_id = next_unique_id++;
2724 } while (bsearch(&arr[i], arr, unassigned_pos, sizeof(*arr), cmp_pat_ref));
2725 }
2726
2727 /* Sort complete array */
2728 qsort(arr, len, sizeof(*arr), cmp_pat_ref);
2729
2730 /* Convert back to linked list */
2731 for (i = 0; i < len; i++)
2732 LIST_ADDQ(&pr, &arr[i]->list);
2733
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002734 /* swap root */
2735 LIST_ADD(&pr, &pattern_reference);
2736 LIST_DEL(&pr);
Carl Henrik Lunde4944c8c2020-02-27 16:45:50 +01002737
2738 free(arr);
2739 return 0;
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002740}
Willy Tarreau7fdd81c2019-10-23 06:59:31 +02002741
2742static int pattern_per_thread_lru_alloc()
2743{
2744 if (!global.tune.pattern_cache)
2745 return 1;
2746 pat_lru_tree = lru64_new(global.tune.pattern_cache);
2747 return !!pat_lru_tree;
2748}
2749
2750static void pattern_per_thread_lru_free()
2751{
2752 lru64_destroy(pat_lru_tree);
2753}
2754
2755REGISTER_PER_THREAD_ALLOC(pattern_per_thread_lru_alloc);
2756REGISTER_PER_THREAD_FREE(pattern_per_thread_lru_free);