blob: 7b4d876e2f99641c1b85e376809ca5613b2fa361 [file] [log] [blame]
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001/*
2 * Pattern management functions.
3 *
4 * Copyright 2000-2013 Willy Tarreau <w@1wt.eu>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <ctype.h>
14#include <stdio.h>
Jerome Magninb8bd6d72020-01-17 18:01:20 +010015#include <errno.h>
Thierry FOURNIERed66c292013-11-28 11:05:19 +010016
Willy Tarreaub2551052020-06-09 09:07:15 +020017#include <import/ebsttree.h>
18#include <import/lru.h>
19#include <import/xxhash.h>
20
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020021#include <haproxy/api.h>
Willy Tarreauf268ee82020-06-04 17:05:57 +020022#include <haproxy/global.h>
Willy Tarreauaeed4a82020-06-04 22:01:04 +020023#include <haproxy/log.h>
Willy Tarreau6131d6a2020-06-02 16:48:09 +020024#include <haproxy/net_helper.h>
Willy Tarreau225a90a2020-06-04 15:06:28 +020025#include <haproxy/pattern.h>
Willy Tarreau7cd8b6e2020-06-02 17:32:26 +020026#include <haproxy/regex.h>
Willy Tarreaue6ce10b2020-06-04 15:33:47 +020027#include <haproxy/sample.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020028#include <haproxy/tools.h>
Thierry FOURNIERed66c292013-11-28 11:05:19 +010029
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 FOURNIER6f7203d2014-01-14 16:24:51 +010082void (*pat_prune_fcts[PAT_MATCH_NUM])(struct pattern_expr *) = {
Willy Tarreau6d8a6892020-11-02 19:26:02 +010083 [PAT_MATCH_FOUND] = pat_prune_gen,
84 [PAT_MATCH_BOOL] = pat_prune_gen,
85 [PAT_MATCH_INT] = pat_prune_gen,
86 [PAT_MATCH_IP] = pat_prune_gen,
87 [PAT_MATCH_BIN] = pat_prune_gen,
88 [PAT_MATCH_LEN] = pat_prune_gen,
89 [PAT_MATCH_STR] = pat_prune_gen,
90 [PAT_MATCH_BEG] = pat_prune_gen,
91 [PAT_MATCH_SUB] = pat_prune_gen,
92 [PAT_MATCH_DIR] = pat_prune_gen,
93 [PAT_MATCH_DOM] = pat_prune_gen,
94 [PAT_MATCH_END] = pat_prune_gen,
95 [PAT_MATCH_REG] = pat_prune_gen,
96 [PAT_MATCH_REGM] = pat_prune_gen,
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +010097};
98
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010099struct pattern *(*pat_match_fcts[PAT_MATCH_NUM])(struct sample *, struct pattern_expr *, int) = {
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100100 [PAT_MATCH_FOUND] = NULL,
101 [PAT_MATCH_BOOL] = pat_match_nothing,
102 [PAT_MATCH_INT] = pat_match_int,
103 [PAT_MATCH_IP] = pat_match_ip,
104 [PAT_MATCH_BIN] = pat_match_bin,
105 [PAT_MATCH_LEN] = pat_match_len,
106 [PAT_MATCH_STR] = pat_match_str,
107 [PAT_MATCH_BEG] = pat_match_beg,
108 [PAT_MATCH_SUB] = pat_match_sub,
109 [PAT_MATCH_DIR] = pat_match_dir,
110 [PAT_MATCH_DOM] = pat_match_dom,
111 [PAT_MATCH_END] = pat_match_end,
112 [PAT_MATCH_REG] = pat_match_reg,
Thierry Fournier8feaa662016-02-10 22:55:20 +0100113 [PAT_MATCH_REGM] = pat_match_regm,
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100114};
115
Thierry FOURNIERe3ded592013-12-06 15:36:54 +0100116/* Just used for checking configuration compatibility */
117int pat_match_types[PAT_MATCH_NUM] = {
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200118 [PAT_MATCH_FOUND] = SMP_T_SINT,
119 [PAT_MATCH_BOOL] = SMP_T_SINT,
120 [PAT_MATCH_INT] = SMP_T_SINT,
Thierry FOURNIERe3ded592013-12-06 15:36:54 +0100121 [PAT_MATCH_IP] = SMP_T_ADDR,
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100122 [PAT_MATCH_BIN] = SMP_T_BIN,
123 [PAT_MATCH_LEN] = SMP_T_STR,
124 [PAT_MATCH_STR] = SMP_T_STR,
125 [PAT_MATCH_BEG] = SMP_T_STR,
126 [PAT_MATCH_SUB] = SMP_T_STR,
127 [PAT_MATCH_DIR] = SMP_T_STR,
128 [PAT_MATCH_DOM] = SMP_T_STR,
129 [PAT_MATCH_END] = SMP_T_STR,
130 [PAT_MATCH_REG] = SMP_T_STR,
Thierry Fournier8feaa662016-02-10 22:55:20 +0100131 [PAT_MATCH_REGM] = SMP_T_STR,
Thierry FOURNIERe3ded592013-12-06 15:36:54 +0100132};
133
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +0100134/* this struct is used to return information */
Emeric Brunb5997f72017-07-03 11:34:05 +0200135static THREAD_LOCAL struct pattern static_pattern;
136static THREAD_LOCAL struct sample_data static_sample_data;
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +0100137
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100138/* This is the root of the list of all pattern_ref avalaibles. */
139struct list pattern_reference = LIST_HEAD_INIT(pattern_reference);
140
Willy Tarreau403bfbb2019-10-23 06:59:31 +0200141static THREAD_LOCAL struct lru64_head *pat_lru_tree;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200142static unsigned long long pat_lru_seed;
143
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100144/*
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100145 *
146 * The following functions are not exported and are used by internals process
147 * of pattern matching
148 *
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100149 */
150
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100151/* Background: Fast way to find a zero byte in a word
152 * http://graphics.stanford.edu/~seander/bithacks.html#ZeroInWord
153 * hasZeroByte = (v - 0x01010101UL) & ~v & 0x80808080UL;
154 *
155 * To look for 4 different byte values, xor the word with those bytes and
156 * then check for zero bytes:
157 *
158 * v = (((unsigned char)c * 0x1010101U) ^ delimiter)
159 * where <delimiter> is the 4 byte values to look for (as an uint)
160 * and <c> is the character that is being tested
161 */
162static inline unsigned int is_delimiter(unsigned char c, unsigned int mask)
163{
164 mask ^= (c * 0x01010101); /* propagate the char to all 4 bytes */
165 return (mask - 0x01010101) & ~mask & 0x80808080U;
166}
167
168static inline unsigned int make_4delim(unsigned char d1, unsigned char d2, unsigned char d3, unsigned char d4)
169{
170 return d1 << 24 | d2 << 16 | d3 << 8 | d4;
171}
172
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100173
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100174/*
175 *
176 * These functions are exported and may be used by any other component.
177 *
Willy Tarreau5def8ef2014-08-29 15:19:33 +0200178 * The following functions are used for parsing pattern matching input value.
179 * The <text> contain the string to be parsed. <pattern> must be a preallocated
180 * pattern. The pat_parse_* functions fill this structure with the parsed value.
181 * <err> is filled with an error message built with memprintf() function. It is
182 * allowed to use a trash as a temporary storage for the returned pattern, as
183 * the next call after these functions will be pat_idx_*.
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100184 *
Willy Tarreau5def8ef2014-08-29 15:19:33 +0200185 * In success case, the pat_parse_* function returns 1. If the function
186 * fails, it returns 0 and <err> is filled.
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100187 */
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100188
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100189/* ignore the current line */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200190int pat_parse_nothing(const char *text, struct pattern *pattern, int mflags, char **err)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100191{
192 return 1;
193}
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100194
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100195/* Parse a string. It is allocated and duplicated. */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200196int pat_parse_str(const char *text, struct pattern *pattern, int mflags, char **err)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100197{
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100198 pattern->type = SMP_T_STR;
Thierry FOURNIERedc15c32013-12-13 15:36:59 +0100199 pattern->ptr.str = (char *)text;
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100200 pattern->len = strlen(text);
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100201 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100202}
203
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100204/* Parse a binary written in hexa. It is allocated. */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200205int pat_parse_bin(const char *text, struct pattern *pattern, int mflags, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100206{
Willy Tarreau83061a82018-07-13 11:56:34 +0200207 struct buffer *trash;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100208
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100209 pattern->type = SMP_T_BIN;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100210 trash = get_trash_chunk();
211 pattern->len = trash->size;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200212 pattern->ptr.str = trash->area;
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100213 return !!parse_binary(text, &pattern->ptr.str, &pattern->len, err);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100214}
215
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100216/* Parse a regex. It is allocated. */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200217int pat_parse_reg(const char *text, struct pattern *pattern, int mflags, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100218{
Thierry FOURNIER0b6d15f2014-01-29 19:35:16 +0100219 pattern->ptr.str = (char *)text;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100220 return 1;
221}
222
223/* Parse a range of positive integers delimited by either ':' or '-'. If only
224 * one integer is read, it is set as both min and max. An operator may be
225 * specified as the prefix, among this list of 5 :
226 *
227 * 0:eq, 1:gt, 2:ge, 3:lt, 4:le
228 *
229 * The default operator is "eq". It supports range matching. Ranges are
230 * rejected for other operators. The operator may be changed at any time.
231 * The operator is stored in the 'opaque' argument.
232 *
233 * If err is non-NULL, an error message will be returned there on errors and
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100234 * the caller will have to free it. The function returns zero on error, and
235 * non-zero on success.
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100236 *
237 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200238int pat_parse_int(const char *text, struct pattern *pattern, int mflags, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100239{
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100240 const char *ptr = text;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100241
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200242 pattern->type = SMP_T_SINT;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100243
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100244 /* Empty string is not valid */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100245 if (!*text)
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100246 goto not_valid_range;
247
248 /* Search ':' or '-' separator. */
249 while (*ptr != '\0' && *ptr != ':' && *ptr != '-')
250 ptr++;
251
252 /* If separator not found. */
253 if (!*ptr) {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100254 if (strl2llrc(text, ptr - text, &pattern->val.range.min) != 0) {
255 memprintf(err, "'%s' is not a number", text);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100256 return 0;
257 }
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100258 pattern->val.range.max = pattern->val.range.min;
259 pattern->val.range.min_set = 1;
260 pattern->val.range.max_set = 1;
261 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100262 }
263
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100264 /* If the separator is the first character. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100265 if (ptr == text && *(ptr + 1) != '\0') {
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100266 if (strl2llrc(ptr + 1, strlen(ptr + 1), &pattern->val.range.max) != 0)
267 goto not_valid_range;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100268
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100269 pattern->val.range.min_set = 0;
270 pattern->val.range.max_set = 1;
271 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100272 }
273
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100274 /* If separator is the last character. */
275 if (*(ptr + 1) == '\0') {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100276 if (strl2llrc(text, ptr - text, &pattern->val.range.min) != 0)
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100277 goto not_valid_range;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100278
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100279 pattern->val.range.min_set = 1;
280 pattern->val.range.max_set = 0;
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100281 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100282 }
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100283
284 /* Else, parse two numbers. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100285 if (strl2llrc(text, ptr - text, &pattern->val.range.min) != 0)
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100286 goto not_valid_range;
287
288 if (strl2llrc(ptr + 1, strlen(ptr + 1), &pattern->val.range.max) != 0)
289 goto not_valid_range;
290
291 if (pattern->val.range.min > pattern->val.range.max)
292 goto not_valid_range;
293
294 pattern->val.range.min_set = 1;
295 pattern->val.range.max_set = 1;
296 return 1;
297
298 not_valid_range:
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100299 memprintf(err, "'%s' is not a valid number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100300 return 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100301}
302
303/* Parse a range of positive 2-component versions delimited by either ':' or
304 * '-'. The version consists in a major and a minor, both of which must be
305 * smaller than 65536, because internally they will be represented as a 32-bit
306 * integer.
307 * If only one version is read, it is set as both min and max. Just like for
308 * pure integers, an operator may be specified as the prefix, among this list
309 * of 5 :
310 *
311 * 0:eq, 1:gt, 2:ge, 3:lt, 4:le
312 *
313 * The default operator is "eq". It supports range matching. Ranges are
314 * rejected for other operators. The operator may be changed at any time.
315 * The operator is stored in the 'opaque' argument. This allows constructs
316 * such as the following one :
317 *
318 * acl obsolete_ssl ssl_req_proto lt 3
319 * acl unsupported_ssl ssl_req_proto gt 3.1
320 * acl valid_ssl ssl_req_proto 3.0-3.1
321 *
322 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200323int pat_parse_dotted_ver(const char *text, struct pattern *pattern, int mflags, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100324{
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100325 const char *ptr = text;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100326
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200327 pattern->type = SMP_T_SINT;
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100328
329 /* Search ':' or '-' separator. */
330 while (*ptr != '\0' && *ptr != ':' && *ptr != '-')
331 ptr++;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100332
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100333 /* If separator not found. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100334 if (*ptr == '\0' && ptr > text) {
335 if (strl2llrc_dotted(text, ptr-text, &pattern->val.range.min) != 0) {
336 memprintf(err, "'%s' is not a dotted number", text);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100337 return 0;
338 }
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100339 pattern->val.range.max = pattern->val.range.min;
340 pattern->val.range.min_set = 1;
341 pattern->val.range.max_set = 1;
342 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100343 }
344
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100345 /* If the separator is the first character. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100346 if (ptr == text && *(ptr+1) != '\0') {
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100347 if (strl2llrc_dotted(ptr+1, strlen(ptr+1), &pattern->val.range.max) != 0) {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100348 memprintf(err, "'%s' is not a valid dotted number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100349 return 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100350 }
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100351 pattern->val.range.min_set = 0;
352 pattern->val.range.max_set = 1;
353 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100354 }
355
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100356 /* If separator is the last character. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100357 if (ptr == &text[strlen(text)-1]) {
358 if (strl2llrc_dotted(text, ptr-text, &pattern->val.range.min) != 0) {
359 memprintf(err, "'%s' is not a valid dotted number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100360 return 0;
361 }
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100362 pattern->val.range.min_set = 1;
363 pattern->val.range.max_set = 0;
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100364 return 1;
365 }
366
367 /* Else, parse two numbers. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100368 if (strl2llrc_dotted(text, ptr-text, &pattern->val.range.min) != 0) {
369 memprintf(err, "'%s' is not a valid dotted number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100370 return 0;
371 }
372 if (strl2llrc_dotted(ptr+1, strlen(ptr+1), &pattern->val.range.max) != 0) {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100373 memprintf(err, "'%s' is not a valid dotted number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100374 return 0;
375 }
376 if (pattern->val.range.min > pattern->val.range.max) {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100377 memprintf(err, "'%s' is not a valid dotted number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100378 return 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100379 }
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100380 pattern->val.range.min_set = 1;
381 pattern->val.range.max_set = 1;
382 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100383}
384
385/* Parse an IP address and an optional mask in the form addr[/mask].
386 * The addr may either be an IPv4 address or a hostname. The mask
387 * may either be a dotted mask or a number of bits. Returns 1 if OK,
388 * otherwise 0. NOTE: IP address patterns are typed (IPV4/IPV6).
389 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200390int pat_parse_ip(const char *text, struct pattern *pattern, int mflags, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100391{
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200392 if (str2net(text, !(mflags & PAT_MF_NO_DNS) && (global.mode & MODE_STARTING),
Thierry FOURNIERfc7ac7b2014-02-11 15:23:04 +0100393 &pattern->val.ipv4.addr, &pattern->val.ipv4.mask)) {
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100394 pattern->type = SMP_T_IPV4;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100395 return 1;
396 }
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100397 else if (str62net(text, &pattern->val.ipv6.addr, &pattern->val.ipv6.mask)) {
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100398 pattern->type = SMP_T_IPV6;
399 return 1;
400 }
401 else {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100402 memprintf(err, "'%s' is not a valid IPv4 or IPv6 address", text);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100403 return 0;
404 }
405}
406
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100407/*
408 *
409 * These functions are exported and may be used by any other component.
410 *
Joseph Herlant4189d672018-11-15 10:22:31 -0800411 * This function just takes a sample <smp> and checks if this sample matches
412 * with the pattern <pattern>. This function returns only PAT_MATCH or
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100413 * PAT_NOMATCH.
414 *
415 */
416
417/* always return false */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100418struct pattern *pat_match_nothing(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100419{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200420 if (smp->data.u.sint) {
Thierry FOURNIERe5978bf2014-03-17 19:53:10 +0100421 if (fill) {
Thierry FOURNIER503bb092015-08-19 08:35:43 +0200422 static_pattern.data = NULL;
Thierry FOURNIERe5978bf2014-03-17 19:53:10 +0100423 static_pattern.ref = NULL;
Thierry FOURNIERe5978bf2014-03-17 19:53:10 +0100424 static_pattern.type = 0;
425 static_pattern.ptr.str = NULL;
426 }
427 return &static_pattern;
428 }
429 else
430 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100431}
432
433
Joseph Herlant4189d672018-11-15 10:22:31 -0800434/* NB: For two strings to be identical, it is required that their length match */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100435struct pattern *pat_match_str(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100436{
437 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100438 struct ebmb_node *node;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100439 struct pattern_tree *elt;
440 struct pattern_list *lst;
441 struct pattern *pattern;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200442 struct pattern *ret = NULL;
443 struct lru64 *lru = NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100444
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100445 /* Lookup a string in the expression's pattern tree. */
446 if (!eb_is_empty(&expr->pattern_tree)) {
Christopher Fauletb4cf7ab2020-06-30 18:52:32 +0200447 char prev = 0;
448
449 if (smp->data.u.str.data < smp->data.u.str.size) {
450 /* we may have to force a trailing zero on the test pattern and
451 * the buffer is large enough to accommodate it.
452 */
453 prev = smp->data.u.str.area[smp->data.u.str.data];
454 if (prev)
455 smp->data.u.str.area[smp->data.u.str.data] = '\0';
456 }
457 else {
458 /* Otherwise, the sample is duplicated. A trailing zero
459 * is automatically added to the string.
460 */
461 if (!smp_dup(smp))
462 return NULL;
463 }
464
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200465 node = ebst_lookup(&expr->pattern_tree, smp->data.u.str.area);
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100466 if (prev)
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200467 smp->data.u.str.area[smp->data.u.str.data] = prev;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100468 if (node) {
469 if (fill) {
470 elt = ebmb_entry(node, struct pattern_tree, node);
Thierry FOURNIER503bb092015-08-19 08:35:43 +0200471 static_pattern.data = elt->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +0100472 static_pattern.ref = elt->ref;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200473 static_pattern.sflags = PAT_SF_TREE;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100474 static_pattern.type = SMP_T_STR;
475 static_pattern.ptr.str = (char *)elt->node.key;
476 }
477 return &static_pattern;
478 }
479 }
480
481 /* look in the list */
Willy Tarreauf3045d22015-04-29 16:24:50 +0200482 if (pat_lru_tree) {
Willy Tarreauaee93142015-05-04 17:18:42 +0200483 unsigned long long seed = pat_lru_seed ^ (long)expr;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200484
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200485 lru = lru64_get(XXH64(smp->data.u.str.area, smp->data.u.str.data, seed),
Willy Tarreau3ee0de12020-11-02 15:26:51 +0100486 pat_lru_tree, expr, expr->ref->revision);
Willy Tarreau403bfbb2019-10-23 06:59:31 +0200487 if (lru && lru->domain) {
Emeric Brunb5997f72017-07-03 11:34:05 +0200488 ret = lru->data;
Emeric Brunb5997f72017-07-03 11:34:05 +0200489 return ret;
490 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200491 }
492
Emeric Brunb5997f72017-07-03 11:34:05 +0200493
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100494 list_for_each_entry(lst, &expr->patterns, list) {
495 pattern = &lst->pat;
496
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200497 if (pattern->len != smp->data.u.str.data)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100498 continue;
499
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200500 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200501 if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.str.area, smp->data.u.str.data) == 0) ||
502 (!icase && strncmp(pattern->ptr.str, smp->data.u.str.area, smp->data.u.str.data) == 0)) {
Willy Tarreauf3045d22015-04-29 16:24:50 +0200503 ret = pattern;
504 break;
505 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100506 }
507
Willy Tarreau403bfbb2019-10-23 06:59:31 +0200508 if (lru)
Willy Tarreau3ee0de12020-11-02 15:26:51 +0100509 lru64_commit(lru, ret, expr, expr->ref->revision, NULL);
Willy Tarreauf3045d22015-04-29 16:24:50 +0200510
511 return ret;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100512}
513
514/* NB: For two binaries buf to be identical, it is required that their lengths match */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100515struct pattern *pat_match_bin(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100516{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100517 struct pattern_list *lst;
518 struct pattern *pattern;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200519 struct pattern *ret = NULL;
520 struct lru64 *lru = NULL;
521
522 if (pat_lru_tree) {
Willy Tarreauaee93142015-05-04 17:18:42 +0200523 unsigned long long seed = pat_lru_seed ^ (long)expr;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100524
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200525 lru = lru64_get(XXH64(smp->data.u.str.area, smp->data.u.str.data, seed),
Willy Tarreau3ee0de12020-11-02 15:26:51 +0100526 pat_lru_tree, expr, expr->ref->revision);
Willy Tarreau403bfbb2019-10-23 06:59:31 +0200527 if (lru && lru->domain) {
Emeric Brunb5997f72017-07-03 11:34:05 +0200528 ret = lru->data;
Emeric Brunb5997f72017-07-03 11:34:05 +0200529 return ret;
530 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200531 }
532
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100533 list_for_each_entry(lst, &expr->patterns, list) {
534 pattern = &lst->pat;
535
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200536 if (pattern->len != smp->data.u.str.data)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100537 continue;
538
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200539 if (memcmp(pattern->ptr.str, smp->data.u.str.area, smp->data.u.str.data) == 0) {
Willy Tarreauf3045d22015-04-29 16:24:50 +0200540 ret = pattern;
541 break;
542 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100543 }
544
Willy Tarreau403bfbb2019-10-23 06:59:31 +0200545 if (lru)
Willy Tarreau3ee0de12020-11-02 15:26:51 +0100546 lru64_commit(lru, ret, expr, expr->ref->revision, NULL);
Willy Tarreauf3045d22015-04-29 16:24:50 +0200547
548 return ret;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100549}
550
551/* Executes a regex. It temporarily changes the data to add a trailing zero,
Thierry Fournier8feaa662016-02-10 22:55:20 +0100552 * and restores the previous character when leaving. This function fills
553 * a matching array.
554 */
555struct pattern *pat_match_regm(struct sample *smp, struct pattern_expr *expr, int fill)
556{
557 struct pattern_list *lst;
558 struct pattern *pattern;
559 struct pattern *ret = NULL;
560
561 list_for_each_entry(lst, &expr->patterns, list) {
562 pattern = &lst->pat;
563
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200564 if (regex_exec_match2(pattern->ptr.reg, smp->data.u.str.area, smp->data.u.str.data,
Thierry Fournier8feaa662016-02-10 22:55:20 +0100565 MAX_MATCH, pmatch, 0)) {
566 ret = pattern;
567 smp->ctx.a[0] = pmatch;
568 break;
569 }
570 }
571
572 return ret;
573}
574
575/* Executes a regex. It temporarily changes the data to add a trailing zero,
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100576 * and restores the previous character when leaving.
577 */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100578struct pattern *pat_match_reg(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100579{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100580 struct pattern_list *lst;
581 struct pattern *pattern;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200582 struct pattern *ret = NULL;
583 struct lru64 *lru = NULL;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100584
Willy Tarreauf3045d22015-04-29 16:24:50 +0200585 if (pat_lru_tree) {
Willy Tarreauaee93142015-05-04 17:18:42 +0200586 unsigned long long seed = pat_lru_seed ^ (long)expr;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200587
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200588 lru = lru64_get(XXH64(smp->data.u.str.area, smp->data.u.str.data, seed),
Willy Tarreau3ee0de12020-11-02 15:26:51 +0100589 pat_lru_tree, expr, expr->ref->revision);
Willy Tarreau403bfbb2019-10-23 06:59:31 +0200590 if (lru && lru->domain) {
Emeric Brunb5997f72017-07-03 11:34:05 +0200591 ret = lru->data;
Emeric Brunb5997f72017-07-03 11:34:05 +0200592 return ret;
593 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200594 }
595
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100596 list_for_each_entry(lst, &expr->patterns, list) {
597 pattern = &lst->pat;
598
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200599 if (regex_exec2(pattern->ptr.reg, smp->data.u.str.area, smp->data.u.str.data)) {
Willy Tarreauf3045d22015-04-29 16:24:50 +0200600 ret = pattern;
601 break;
602 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100603 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200604
Willy Tarreau403bfbb2019-10-23 06:59:31 +0200605 if (lru)
Willy Tarreau3ee0de12020-11-02 15:26:51 +0100606 lru64_commit(lru, ret, expr, expr->ref->revision, NULL);
Willy Tarreauf3045d22015-04-29 16:24:50 +0200607
608 return ret;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100609}
610
611/* Checks that the pattern matches the beginning of the tested string. */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100612struct pattern *pat_match_beg(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100613{
614 int icase;
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +0200615 struct ebmb_node *node;
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +0200616 struct pattern_tree *elt;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100617 struct pattern_list *lst;
618 struct pattern *pattern;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200619 struct pattern *ret = NULL;
620 struct lru64 *lru = NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100621
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +0200622 /* Lookup a string in the expression's pattern tree. */
623 if (!eb_is_empty(&expr->pattern_tree)) {
Christopher Fauletb4cf7ab2020-06-30 18:52:32 +0200624 char prev = 0;
625
626 if (smp->data.u.str.data < smp->data.u.str.size) {
627 /* we may have to force a trailing zero on the test pattern and
628 * the buffer is large enough to accommodate it.
629 */
630 prev = smp->data.u.str.area[smp->data.u.str.data];
631 if (prev)
632 smp->data.u.str.area[smp->data.u.str.data] = '\0';
633 }
634 else {
635 /* Otherwise, the sample is duplicated. A trailing zero
636 * is automatically added to the string.
637 */
638 if (!smp_dup(smp))
639 return NULL;
640 }
641
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200642 node = ebmb_lookup_longest(&expr->pattern_tree,
643 smp->data.u.str.area);
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +0200644 if (prev)
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200645 smp->data.u.str.area[smp->data.u.str.data] = prev;
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +0200646
647 if (node) {
648 if (fill) {
649 elt = ebmb_entry(node, struct pattern_tree, node);
Thierry FOURNIER503bb092015-08-19 08:35:43 +0200650 static_pattern.data = elt->data;
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +0200651 static_pattern.ref = elt->ref;
652 static_pattern.sflags = PAT_SF_TREE;
653 static_pattern.type = SMP_T_STR;
654 static_pattern.ptr.str = (char *)elt->node.key;
655 }
656 return &static_pattern;
657 }
658 }
659
660 /* look in the list */
Willy Tarreauf3045d22015-04-29 16:24:50 +0200661 if (pat_lru_tree) {
Willy Tarreauaee93142015-05-04 17:18:42 +0200662 unsigned long long seed = pat_lru_seed ^ (long)expr;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200663
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200664 lru = lru64_get(XXH64(smp->data.u.str.area, smp->data.u.str.data, seed),
Willy Tarreau3ee0de12020-11-02 15:26:51 +0100665 pat_lru_tree, expr, expr->ref->revision);
Willy Tarreau403bfbb2019-10-23 06:59:31 +0200666 if (lru && lru->domain) {
Emeric Brunb5997f72017-07-03 11:34:05 +0200667 ret = lru->data;
Emeric Brunb5997f72017-07-03 11:34:05 +0200668 return ret;
669 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200670 }
671
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100672 list_for_each_entry(lst, &expr->patterns, list) {
673 pattern = &lst->pat;
674
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200675 if (pattern->len > smp->data.u.str.data)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100676 continue;
677
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200678 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200679 if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.str.area, pattern->len) != 0) ||
680 (!icase && strncmp(pattern->ptr.str, smp->data.u.str.area, pattern->len) != 0))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100681 continue;
682
Willy Tarreauf3045d22015-04-29 16:24:50 +0200683 ret = pattern;
684 break;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100685 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200686
Willy Tarreau403bfbb2019-10-23 06:59:31 +0200687 if (lru)
Willy Tarreau3ee0de12020-11-02 15:26:51 +0100688 lru64_commit(lru, ret, expr, expr->ref->revision, NULL);
Willy Tarreauf3045d22015-04-29 16:24:50 +0200689
690 return ret;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100691}
692
693/* Checks that the pattern matches the end of the tested string. */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100694struct pattern *pat_match_end(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100695{
696 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100697 struct pattern_list *lst;
698 struct pattern *pattern;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200699 struct pattern *ret = NULL;
700 struct lru64 *lru = NULL;
701
702 if (pat_lru_tree) {
Willy Tarreauaee93142015-05-04 17:18:42 +0200703 unsigned long long seed = pat_lru_seed ^ (long)expr;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200704
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200705 lru = lru64_get(XXH64(smp->data.u.str.area, smp->data.u.str.data, seed),
Willy Tarreau3ee0de12020-11-02 15:26:51 +0100706 pat_lru_tree, expr, expr->ref->revision);
Willy Tarreau403bfbb2019-10-23 06:59:31 +0200707 if (lru && lru->domain) {
Emeric Brunb5997f72017-07-03 11:34:05 +0200708 ret = lru->data;
Emeric Brunb5997f72017-07-03 11:34:05 +0200709 return ret;
710 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200711 }
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100712
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100713 list_for_each_entry(lst, &expr->patterns, list) {
714 pattern = &lst->pat;
715
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200716 if (pattern->len > smp->data.u.str.data)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100717 continue;
718
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200719 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200720 if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.str.area + smp->data.u.str.data - pattern->len, pattern->len) != 0) ||
721 (!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 +0100722 continue;
723
Willy Tarreauf3045d22015-04-29 16:24:50 +0200724 ret = pattern;
725 break;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100726 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200727
Willy Tarreau403bfbb2019-10-23 06:59:31 +0200728 if (lru)
Willy Tarreau3ee0de12020-11-02 15:26:51 +0100729 lru64_commit(lru, ret, expr, expr->ref->revision, NULL);
Willy Tarreauf3045d22015-04-29 16:24:50 +0200730
731 return ret;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100732}
733
734/* Checks that the pattern is included inside the tested string.
735 * NB: Suboptimal, should be rewritten using a Boyer-Moore method.
736 */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100737struct pattern *pat_match_sub(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100738{
739 int icase;
740 char *end;
741 char *c;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100742 struct pattern_list *lst;
743 struct pattern *pattern;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200744 struct pattern *ret = NULL;
745 struct lru64 *lru = NULL;
746
747 if (pat_lru_tree) {
Willy Tarreauaee93142015-05-04 17:18:42 +0200748 unsigned long long seed = pat_lru_seed ^ (long)expr;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200749
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200750 lru = lru64_get(XXH64(smp->data.u.str.area, smp->data.u.str.data, seed),
Willy Tarreau3ee0de12020-11-02 15:26:51 +0100751 pat_lru_tree, expr, expr->ref->revision);
Willy Tarreau403bfbb2019-10-23 06:59:31 +0200752 if (lru && lru->domain) {
Emeric Brunb5997f72017-07-03 11:34:05 +0200753 ret = lru->data;
Emeric Brunb5997f72017-07-03 11:34:05 +0200754 return ret;
755 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200756 }
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100757
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100758 list_for_each_entry(lst, &expr->patterns, list) {
759 pattern = &lst->pat;
760
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200761 if (pattern->len > smp->data.u.str.data)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100762 continue;
763
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200764 end = smp->data.u.str.area + smp->data.u.str.data - pattern->len;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200765 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100766 if (icase) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200767 for (c = smp->data.u.str.area; c <= end; c++) {
Willy Tarreauf278eec2020-07-05 21:46:32 +0200768 if (tolower((unsigned char)*c) != tolower((unsigned char)*pattern->ptr.str))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100769 continue;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200770 if (strncasecmp(pattern->ptr.str, c, pattern->len) == 0) {
771 ret = pattern;
772 goto leave;
773 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100774 }
775 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200776 for (c = smp->data.u.str.area; c <= end; c++) {
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100777 if (*c != *pattern->ptr.str)
778 continue;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200779 if (strncmp(pattern->ptr.str, c, pattern->len) == 0) {
780 ret = pattern;
781 goto leave;
782 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100783 }
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100784 }
785 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200786 leave:
Willy Tarreau403bfbb2019-10-23 06:59:31 +0200787 if (lru)
Willy Tarreau3ee0de12020-11-02 15:26:51 +0100788 lru64_commit(lru, ret, expr, expr->ref->revision, NULL);
Willy Tarreauf3045d22015-04-29 16:24:50 +0200789
790 return ret;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100791}
792
793/* This one is used by other real functions. It checks that the pattern is
794 * included inside the tested string, but enclosed between the specified
795 * delimiters or at the beginning or end of the string. The delimiters are
796 * provided as an unsigned int made by make_4delim() and match up to 4 different
797 * delimiters. Delimiters are stripped at the beginning and end of the pattern.
798 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200799static int match_word(struct sample *smp, struct pattern *pattern, int mflags, unsigned int delimiters)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100800{
801 int may_match, icase;
802 char *c, *end;
803 char *ps;
804 int pl;
805
806 pl = pattern->len;
807 ps = pattern->ptr.str;
808
809 while (pl > 0 && is_delimiter(*ps, delimiters)) {
810 pl--;
811 ps++;
812 }
813
814 while (pl > 0 && is_delimiter(ps[pl - 1], delimiters))
815 pl--;
816
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200817 if (pl > smp->data.u.str.data)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100818 return PAT_NOMATCH;
819
820 may_match = 1;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200821 icase = mflags & PAT_MF_IGNORE_CASE;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200822 end = smp->data.u.str.area + smp->data.u.str.data - pl;
823 for (c = smp->data.u.str.area; c <= end; c++) {
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100824 if (is_delimiter(*c, delimiters)) {
825 may_match = 1;
826 continue;
827 }
828
829 if (!may_match)
830 continue;
831
832 if (icase) {
Willy Tarreauf278eec2020-07-05 21:46:32 +0200833 if ((tolower((unsigned char)*c) == tolower((unsigned char)*ps)) &&
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100834 (strncasecmp(ps, c, pl) == 0) &&
835 (c == end || is_delimiter(c[pl], delimiters)))
836 return PAT_MATCH;
837 } else {
838 if ((*c == *ps) &&
839 (strncmp(ps, c, pl) == 0) &&
840 (c == end || is_delimiter(c[pl], delimiters)))
841 return PAT_MATCH;
842 }
843 may_match = 0;
844 }
845 return PAT_NOMATCH;
846}
847
848/* Checks that the pattern is included inside the tested string, but enclosed
849 * between the delimiters '?' or '/' or at the beginning or end of the string.
850 * Delimiters at the beginning or end of the pattern are ignored.
851 */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100852struct pattern *pat_match_dir(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100853{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100854 struct pattern_list *lst;
855 struct pattern *pattern;
856
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100857 list_for_each_entry(lst, &expr->patterns, list) {
858 pattern = &lst->pat;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200859 if (match_word(smp, pattern, expr->mflags, make_4delim('/', '?', '?', '?')))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100860 return pattern;
861 }
862 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100863}
864
865/* Checks that the pattern is included inside the tested string, but enclosed
866 * between the delmiters '/', '?', '.' or ":" or at the beginning or end of
867 * the string. Delimiters at the beginning or end of the pattern are ignored.
868 */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100869struct pattern *pat_match_dom(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100870{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100871 struct pattern_list *lst;
872 struct pattern *pattern;
873
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100874 list_for_each_entry(lst, &expr->patterns, list) {
875 pattern = &lst->pat;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200876 if (match_word(smp, pattern, expr->mflags, make_4delim('/', '?', '.', ':')))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100877 return pattern;
878 }
879 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100880}
881
882/* Checks that the integer in <test> is included between min and max */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100883struct pattern *pat_match_int(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100884{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100885 struct pattern_list *lst;
886 struct pattern *pattern;
887
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100888 list_for_each_entry(lst, &expr->patterns, list) {
889 pattern = &lst->pat;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200890 if ((!pattern->val.range.min_set || pattern->val.range.min <= smp->data.u.sint) &&
891 (!pattern->val.range.max_set || smp->data.u.sint <= pattern->val.range.max))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100892 return pattern;
893 }
894 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100895}
896
897/* Checks that the length of the pattern in <test> is included between min and max */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100898struct pattern *pat_match_len(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100899{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100900 struct pattern_list *lst;
901 struct pattern *pattern;
902
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100903 list_for_each_entry(lst, &expr->patterns, list) {
904 pattern = &lst->pat;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200905 if ((!pattern->val.range.min_set || pattern->val.range.min <= smp->data.u.str.data) &&
906 (!pattern->val.range.max_set || smp->data.u.str.data <= pattern->val.range.max))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100907 return pattern;
908 }
909 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100910}
911
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100912struct pattern *pat_match_ip(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100913{
914 unsigned int v4; /* in network byte order */
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100915 struct in6_addr tmp6;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100916 struct in_addr *s;
917 struct ebmb_node *node;
918 struct pattern_tree *elt;
919 struct pattern_list *lst;
920 struct pattern *pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100921
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100922 /* The input sample is IPv4. Try to match in the trees. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200923 if (smp->data.type == SMP_T_IPV4) {
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100924 /* Lookup an IPv4 address in the expression's pattern tree using
925 * the longest match method.
926 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200927 s = &smp->data.u.ipv4;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100928 node = ebmb_lookup_longest(&expr->pattern_tree, &s->s_addr);
929 if (node) {
930 if (fill) {
931 elt = ebmb_entry(node, struct pattern_tree, node);
Thierry FOURNIER503bb092015-08-19 08:35:43 +0200932 static_pattern.data = elt->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +0100933 static_pattern.ref = elt->ref;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200934 static_pattern.sflags = PAT_SF_TREE;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100935 static_pattern.type = SMP_T_IPV4;
Willy Tarreau296cfd12020-02-25 09:58:41 +0100936 static_pattern.val.ipv4.addr.s_addr = read_u32(elt->node.key);
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100937 if (!cidr2dotted(elt->node.node.pfx, &static_pattern.val.ipv4.mask))
938 return NULL;
939 }
940 return &static_pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100941 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100942
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100943 /* The IPv4 sample dont match the IPv4 tree. Convert the IPv4
944 * sample address to IPv6 with the mapping method using the ::ffff:
945 * prefix, and try to lookup in the IPv6 tree.
946 */
947 memset(&tmp6, 0, 10);
Willy Tarreau296cfd12020-02-25 09:58:41 +0100948 write_u16(&tmp6.s6_addr[10], htons(0xffff));
949 write_u32(&tmp6.s6_addr[12], smp->data.u.ipv4.s_addr);
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100950 node = ebmb_lookup_longest(&expr->pattern_tree_2, &tmp6);
951 if (node) {
952 if (fill) {
953 elt = ebmb_entry(node, struct pattern_tree, node);
Thierry FOURNIER503bb092015-08-19 08:35:43 +0200954 static_pattern.data = elt->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +0100955 static_pattern.ref = elt->ref;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200956 static_pattern.sflags = PAT_SF_TREE;
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100957 static_pattern.type = SMP_T_IPV6;
Willy Tarreau296cfd12020-02-25 09:58:41 +0100958 memcpy(&static_pattern.val.ipv6.addr, elt->node.key, 16);
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100959 static_pattern.val.ipv6.mask = elt->node.node.pfx;
960 }
961 return &static_pattern;
962 }
963 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100964
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100965 /* The input sample is IPv6. Try to match in the trees. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200966 if (smp->data.type == SMP_T_IPV6) {
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100967 /* Lookup an IPv6 address in the expression's pattern tree using
968 * the longest match method.
969 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200970 node = ebmb_lookup_longest(&expr->pattern_tree_2, &smp->data.u.ipv6);
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100971 if (node) {
972 if (fill) {
973 elt = ebmb_entry(node, struct pattern_tree, node);
Thierry FOURNIER503bb092015-08-19 08:35:43 +0200974 static_pattern.data = elt->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +0100975 static_pattern.ref = elt->ref;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200976 static_pattern.sflags = PAT_SF_TREE;
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100977 static_pattern.type = SMP_T_IPV6;
Willy Tarreau296cfd12020-02-25 09:58:41 +0100978 memcpy(&static_pattern.val.ipv6.addr, elt->node.key, 16);
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100979 static_pattern.val.ipv6.mask = elt->node.node.pfx;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100980 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100981 return &static_pattern;
982 }
983
984 /* Try to convert 6 to 4 when the start of the ipv6 address match the
985 * following forms :
986 * - ::ffff:ip:v4 (ipv4 mapped)
987 * - ::0000:ip:v4 (old ipv4 mapped)
988 * - 2002:ip:v4:: (6to4)
989 */
Willy Tarreau296cfd12020-02-25 09:58:41 +0100990 if ((read_u64(&smp->data.u.ipv6.s6_addr[0]) == 0 &&
991 (read_u32(&smp->data.u.ipv6.s6_addr[8]) == 0 ||
992 read_u32(&smp->data.u.ipv6.s6_addr[8]) == htonl(0xFFFF))) ||
993 read_u16(&smp->data.u.ipv6.s6_addr[0]) == htons(0x2002)) {
994 if (read_u32(&smp->data.u.ipv6.s6_addr[0]) == 0)
995 v4 = read_u32(&smp->data.u.ipv6.s6_addr[12]);
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100996 else
Willy Tarreau296cfd12020-02-25 09:58:41 +0100997 v4 = htonl((ntohs(read_u16(&smp->data.u.ipv6.s6_addr[2])) << 16) +
998 ntohs(read_u16(&smp->data.u.ipv6.s6_addr[4])));
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100999
1000 /* Lookup an IPv4 address in the expression's pattern tree using the longest
1001 * match method.
1002 */
1003 node = ebmb_lookup_longest(&expr->pattern_tree, &v4);
1004 if (node) {
1005 if (fill) {
1006 elt = ebmb_entry(node, struct pattern_tree, node);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001007 static_pattern.data = elt->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01001008 static_pattern.ref = elt->ref;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02001009 static_pattern.sflags = PAT_SF_TREE;
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001010 static_pattern.type = SMP_T_IPV4;
Willy Tarreau296cfd12020-02-25 09:58:41 +01001011 static_pattern.val.ipv4.addr.s_addr = read_u32(elt->node.key);
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001012 if (!cidr2dotted(elt->node.node.pfx, &static_pattern.val.ipv4.mask))
1013 return NULL;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01001014 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001015 return &static_pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +01001016 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001017 }
1018 }
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +01001019
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001020 /* Lookup in the list. the list contain only IPv4 patterns */
1021 list_for_each_entry(lst, &expr->patterns, list) {
1022 pattern = &lst->pat;
1023
1024 /* The input sample is IPv4, use it as is. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001025 if (smp->data.type == SMP_T_IPV4) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001026 v4 = smp->data.u.ipv4.s_addr;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +01001027 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001028 else if (smp->data.type == SMP_T_IPV6) {
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001029 /* v4 match on a V6 sample. We want to check at least for
1030 * the following forms :
1031 * - ::ffff:ip:v4 (ipv4 mapped)
1032 * - ::0000:ip:v4 (old ipv4 mapped)
1033 * - 2002:ip:v4:: (6to4)
1034 */
Willy Tarreau296cfd12020-02-25 09:58:41 +01001035 if (read_u64(&smp->data.u.ipv6.s6_addr[0]) == 0 &&
1036 (read_u32(&smp->data.u.ipv6.s6_addr[8]) == 0 ||
1037 read_u32(&smp->data.u.ipv6.s6_addr[8]) == htonl(0xFFFF))) {
1038 v4 = read_u32(&smp->data.u.ipv6.s6_addr[12]);
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01001039 }
Willy Tarreau296cfd12020-02-25 09:58:41 +01001040 else if (read_u16(&smp->data.u.ipv6.s6_addr[0]) == htons(0x2002)) {
1041 v4 = htonl((ntohs(read_u16(&smp->data.u.ipv6.s6_addr[2])) << 16) +
1042 ntohs(read_u16(&smp->data.u.ipv6.s6_addr[4])));
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01001043 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001044 else
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01001045 continue;
Andreas Seltenreichf0653192016-03-03 20:08:35 +01001046 } else {
1047 /* impossible */
1048 continue;
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001049 }
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +01001050
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001051 /* Check if the input sample match the current pattern. */
1052 if (((v4 ^ pattern->val.ipv4.addr.s_addr) & pattern->val.ipv4.mask.s_addr) == 0)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01001053 return pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +01001054 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01001055 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +01001056}
1057
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001058void free_pattern_tree(struct eb_root *root)
1059{
1060 struct eb_node *node, *next;
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +01001061 struct pattern_tree *elt;
Thierry FOURNIER3ce88c72013-12-09 11:29:46 +01001062
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001063 node = eb_first(root);
1064 while (node) {
1065 next = eb_next(node);
1066 eb_delete(node);
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +01001067 elt = container_of(node, struct pattern_tree, node);
Willy Tarreau4bdd0a12020-11-02 12:10:48 +01001068 LIST_DEL(&elt->from_ref);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001069 free(elt->data);
Thierry FOURNIER3ce88c72013-12-09 11:29:46 +01001070 free(elt);
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001071 node = next;
1072 }
1073}
1074
Willy Tarreau6d8a6892020-11-02 19:26:02 +01001075void pat_prune_gen(struct pattern_expr *expr)
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +01001076{
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01001077 struct pattern_list *pat, *tmp;
1078
1079 list_for_each_entry_safe(pat, tmp, &expr->patterns, list) {
Christopher Faulet6cfc8512020-09-09 16:09:44 +02001080 LIST_DEL(&pat->list);
Willy Tarreau4bdd0a12020-11-02 12:10:48 +01001081 LIST_DEL(&pat->from_ref);
Willy Tarreau6d8a6892020-11-02 19:26:02 +01001082 if (pat->pat.sflags & PAT_SF_REGFREE)
1083 regex_free(pat->pat.ptr.ptr);
1084 else
1085 free(pat->pat.ptr.ptr);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001086 free(pat->pat.data);
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01001087 free(pat);
1088 }
1089
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +01001090 free_pattern_tree(&expr->pattern_tree);
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001091 free_pattern_tree(&expr->pattern_tree_2);
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +01001092 LIST_INIT(&expr->patterns);
Willy Tarreau3ee0de12020-11-02 15:26:51 +01001093 expr->ref->revision = rdtsc();
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +01001094}
1095
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001096/*
1097 *
1098 * The following functions are used for the pattern indexation
1099 *
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001100 */
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001101
1102int pat_idx_list_val(struct pattern_expr *expr, struct pattern *pat, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001103{
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001104 struct pattern_list *patl;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001105
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001106 /* allocate pattern */
1107 patl = calloc(1, sizeof(*patl));
1108 if (!patl) {
1109 memprintf(err, "out of memory while indexing pattern");
Thierry FOURNIER972028f2014-01-23 17:53:31 +01001110 return 0;
1111 }
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001112
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001113 /* duplicate pattern */
1114 memcpy(&patl->pat, pat, sizeof(*pat));
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001115
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001116 /* chain pattern in the expression */
1117 LIST_ADDQ(&expr->patterns, &patl->list);
Willy Tarreau4bdd0a12020-11-02 12:10:48 +01001118 /* and from the reference */
1119 LIST_ADDQ(&pat->ref->list_head, &patl->from_ref);
Willy Tarreau3ee0de12020-11-02 15:26:51 +01001120 expr->ref->revision = rdtsc();
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001121
1122 /* that's ok */
1123 return 1;
1124}
1125
1126int pat_idx_list_ptr(struct pattern_expr *expr, struct pattern *pat, char **err)
1127{
1128 struct pattern_list *patl;
1129
1130 /* allocate pattern */
1131 patl = calloc(1, sizeof(*patl));
Thierry FOURNIER8aa83842015-02-06 17:50:55 +01001132 if (!patl) {
1133 memprintf(err, "out of memory while indexing pattern");
Thierry FOURNIER972028f2014-01-23 17:53:31 +01001134 return 0;
Thierry FOURNIER8aa83842015-02-06 17:50:55 +01001135 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001136
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001137 /* duplicate pattern */
1138 memcpy(&patl->pat, pat, sizeof(*pat));
1139 patl->pat.ptr.ptr = malloc(patl->pat.len);
1140 if (!patl->pat.ptr.ptr) {
1141 free(patl);
1142 memprintf(err, "out of memory while indexing pattern");
1143 return 0;
1144 }
1145 memcpy(patl->pat.ptr.ptr, pat->ptr.ptr, pat->len);
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001146
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001147 /* chain pattern in the expression */
1148 LIST_ADDQ(&expr->patterns, &patl->list);
Willy Tarreau4bdd0a12020-11-02 12:10:48 +01001149 /* and from the reference */
1150 LIST_ADDQ(&pat->ref->list_head, &patl->from_ref);
Willy Tarreau3ee0de12020-11-02 15:26:51 +01001151 expr->ref->revision = rdtsc();
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001152
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001153 /* that's ok */
1154 return 1;
1155}
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001156
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001157int pat_idx_list_str(struct pattern_expr *expr, struct pattern *pat, char **err)
1158{
1159 struct pattern_list *patl;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001160
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001161 /* allocate pattern */
1162 patl = calloc(1, sizeof(*patl));
1163 if (!patl) {
1164 memprintf(err, "out of memory while indexing pattern");
1165 return 0;
1166 }
1167
1168 /* duplicate pattern */
1169 memcpy(&patl->pat, pat, sizeof(*pat));
1170 patl->pat.ptr.str = malloc(patl->pat.len + 1);
1171 if (!patl->pat.ptr.str) {
1172 free(patl);
1173 memprintf(err, "out of memory while indexing pattern");
1174 return 0;
1175 }
1176 memcpy(patl->pat.ptr.ptr, pat->ptr.ptr, pat->len);
1177 patl->pat.ptr.str[patl->pat.len] = '\0';
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001178
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001179 /* chain pattern in the expression */
1180 LIST_ADDQ(&expr->patterns, &patl->list);
Willy Tarreau4bdd0a12020-11-02 12:10:48 +01001181 /* and from the reference */
1182 LIST_ADDQ(&pat->ref->list_head, &patl->from_ref);
Willy Tarreau3ee0de12020-11-02 15:26:51 +01001183 expr->ref->revision = rdtsc();
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001184
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001185 /* that's ok */
1186 return 1;
1187}
1188
Thierry Fournier8feaa662016-02-10 22:55:20 +01001189int pat_idx_list_reg_cap(struct pattern_expr *expr, struct pattern *pat, int cap, char **err)
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001190{
1191 struct pattern_list *patl;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001192
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001193 /* allocate pattern */
1194 patl = calloc(1, sizeof(*patl));
1195 if (!patl) {
1196 memprintf(err, "out of memory while indexing pattern");
1197 return 0;
Thierry FOURNIER972028f2014-01-23 17:53:31 +01001198 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001199
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001200 /* duplicate pattern */
1201 memcpy(&patl->pat, pat, sizeof(*pat));
1202
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001203 /* compile regex */
Willy Tarreau9b5c8bb2020-11-02 19:16:23 +01001204 patl->pat.sflags |= PAT_SF_REGFREE;
Dragan Dosen26743032019-04-30 15:54:36 +02001205 if (!(patl->pat.ptr.reg = regex_comp(pat->ptr.str, !(expr->mflags & PAT_MF_IGNORE_CASE),
1206 cap, err))) {
Dirkjan Bussink07fcaaa2014-04-28 22:57:16 +00001207 free(patl);
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001208 return 0;
1209 }
1210
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001211 /* chain pattern in the expression */
1212 LIST_ADDQ(&expr->patterns, &patl->list);
Willy Tarreau4bdd0a12020-11-02 12:10:48 +01001213 /* and from the reference */
1214 LIST_ADDQ(&pat->ref->list_head, &patl->from_ref);
Willy Tarreau3ee0de12020-11-02 15:26:51 +01001215 expr->ref->revision = rdtsc();
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001216
1217 /* that's ok */
1218 return 1;
1219}
1220
Thierry Fournier8feaa662016-02-10 22:55:20 +01001221int pat_idx_list_reg(struct pattern_expr *expr, struct pattern *pat, char **err)
1222{
1223 return pat_idx_list_reg_cap(expr, pat, 0, err);
1224}
1225
1226int pat_idx_list_regm(struct pattern_expr *expr, struct pattern *pat, char **err)
1227{
1228 return pat_idx_list_reg_cap(expr, pat, 1, err);
1229}
1230
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001231int pat_idx_tree_ip(struct pattern_expr *expr, struct pattern *pat, char **err)
1232{
1233 unsigned int mask;
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +01001234 struct pattern_tree *node;
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001235
1236 /* Only IPv4 can be indexed */
1237 if (pat->type == SMP_T_IPV4) {
Thierry FOURNIER972028f2014-01-23 17:53:31 +01001238 /* in IPv4 case, check if the mask is contiguous so that we can
1239 * insert the network into the tree. A continuous mask has only
1240 * ones on the left. This means that this mask + its lower bit
1241 * added once again is null.
1242 */
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001243 mask = ntohl(pat->val.ipv4.mask.s_addr);
1244 if (mask + (mask & -mask) == 0) {
1245 mask = mask ? 33 - flsnz(mask & -mask) : 0; /* equals cidr value */
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001246
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001247 /* node memory allocation */
1248 node = calloc(1, sizeof(*node) + 4);
1249 if (!node) {
1250 memprintf(err, "out of memory while loading pattern");
1251 return 0;
1252 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001253
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001254 /* copy the pointer to sample associated to this node */
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001255 node->data = pat->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01001256 node->ref = pat->ref;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001257
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001258 /* FIXME: insert <addr>/<mask> into the tree here */
1259 memcpy(node->node.key, &pat->val.ipv4.addr, 4); /* network byte order */
1260 node->node.node.pfx = mask;
Thierry FOURNIER31db4ae2014-01-30 00:27:15 +01001261
1262 /* Insert the entry. */
1263 ebmb_insert_prefix(&expr->pattern_tree, &node->node, 4);
Willy Tarreau4bdd0a12020-11-02 12:10:48 +01001264 LIST_ADDQ(&pat->ref->tree_head, &node->from_ref);
Willy Tarreau3ee0de12020-11-02 15:26:51 +01001265 expr->ref->revision = rdtsc();
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001266
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001267 /* that's ok */
1268 return 1;
1269 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001270 else {
1271 /* If the mask is not contiguous, just add the pattern to the list */
1272 return pat_idx_list_val(expr, pat, err);
1273 }
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001274 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001275 else if (pat->type == SMP_T_IPV6) {
1276 /* IPv6 also can be indexed */
1277 node = calloc(1, sizeof(*node) + 16);
1278 if (!node) {
1279 memprintf(err, "out of memory while loading pattern");
1280 return 0;
1281 }
1282
1283 /* copy the pointer to sample associated to this node */
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001284 node->data = pat->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01001285 node->ref = pat->ref;
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001286
1287 /* FIXME: insert <addr>/<mask> into the tree here */
1288 memcpy(node->node.key, &pat->val.ipv6.addr, 16); /* network byte order */
1289 node->node.node.pfx = pat->val.ipv6.mask;
Thierry FOURNIER31db4ae2014-01-30 00:27:15 +01001290
1291 /* Insert the entry. */
1292 ebmb_insert_prefix(&expr->pattern_tree_2, &node->node, 16);
Willy Tarreau4bdd0a12020-11-02 12:10:48 +01001293 LIST_ADDQ(&pat->ref->tree_head, &node->from_ref);
Willy Tarreau3ee0de12020-11-02 15:26:51 +01001294 expr->ref->revision = rdtsc();
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001295
1296 /* that's ok */
1297 return 1;
1298 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001299
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001300 return 0;
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001301}
1302
1303int pat_idx_tree_str(struct pattern_expr *expr, struct pattern *pat, char **err)
1304{
1305 int len;
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +01001306 struct pattern_tree *node;
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001307
1308 /* Only string can be indexed */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001309 if (pat->type != SMP_T_STR) {
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001310 memprintf(err, "internal error: string expected, but the type is '%s'",
1311 smp_to_type[pat->type]);
1312 return 0;
Thierry FOURNIER972028f2014-01-23 17:53:31 +01001313 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001314
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001315 /* If the flag PAT_F_IGNORE_CASE is set, we cannot use trees */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02001316 if (expr->mflags & PAT_MF_IGNORE_CASE)
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001317 return pat_idx_list_str(expr, pat, err);
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001318
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001319 /* Process the key len */
1320 len = strlen(pat->ptr.str) + 1;
1321
1322 /* node memory allocation */
1323 node = calloc(1, sizeof(*node) + len);
1324 if (!node) {
1325 memprintf(err, "out of memory while loading pattern");
1326 return 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001327 }
1328
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001329 /* copy the pointer to sample associated to this node */
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001330 node->data = pat->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01001331 node->ref = pat->ref;
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001332
1333 /* copy the string */
1334 memcpy(node->node.key, pat->ptr.str, len);
1335
1336 /* index the new node */
Thierry FOURNIER31db4ae2014-01-30 00:27:15 +01001337 ebst_insert(&expr->pattern_tree, &node->node);
Willy Tarreau4bdd0a12020-11-02 12:10:48 +01001338 LIST_ADDQ(&pat->ref->tree_head, &node->from_ref);
Willy Tarreau3ee0de12020-11-02 15:26:51 +01001339 expr->ref->revision = rdtsc();
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001340
1341 /* that's ok */
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001342 return 1;
1343}
1344
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +02001345int pat_idx_tree_pfx(struct pattern_expr *expr, struct pattern *pat, char **err)
1346{
1347 int len;
1348 struct pattern_tree *node;
1349
1350 /* Only string can be indexed */
1351 if (pat->type != SMP_T_STR) {
1352 memprintf(err, "internal error: string expected, but the type is '%s'",
1353 smp_to_type[pat->type]);
1354 return 0;
1355 }
1356
1357 /* If the flag PAT_F_IGNORE_CASE is set, we cannot use trees */
1358 if (expr->mflags & PAT_MF_IGNORE_CASE)
1359 return pat_idx_list_str(expr, pat, err);
1360
1361 /* Process the key len */
1362 len = strlen(pat->ptr.str);
1363
1364 /* node memory allocation */
1365 node = calloc(1, sizeof(*node) + len + 1);
1366 if (!node) {
1367 memprintf(err, "out of memory while loading pattern");
1368 return 0;
1369 }
1370
1371 /* copy the pointer to sample associated to this node */
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001372 node->data = pat->data;
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +02001373 node->ref = pat->ref;
1374
1375 /* copy the string and the trailing zero */
1376 memcpy(node->node.key, pat->ptr.str, len + 1);
1377 node->node.node.pfx = len * 8;
1378
1379 /* index the new node */
1380 ebmb_insert_prefix(&expr->pattern_tree, &node->node, len);
Willy Tarreau4bdd0a12020-11-02 12:10:48 +01001381 LIST_ADDQ(&pat->ref->tree_head, &node->from_ref);
Willy Tarreau3ee0de12020-11-02 15:26:51 +01001382 expr->ref->revision = rdtsc();
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +02001383
1384 /* that's ok */
1385 return 1;
1386}
1387
Willy Tarreauf1c08922020-11-02 19:53:16 +01001388/* Deletes all patterns from reference <elt>. Note that all of their
Willy Tarreau78777ea2020-11-02 13:55:22 +01001389 * expressions must be locked, and the pattern lock must be held as well.
1390 */
Willy Tarreauf1c08922020-11-02 19:53:16 +01001391void pat_delete_gen(struct pat_ref *ref, struct pat_ref_elt *elt)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001392{
Willy Tarreauf1c08922020-11-02 19:53:16 +01001393 struct pattern_tree *tree, *tree_bck;
1394 struct pattern_list *pat, *pat_bck;
1395
1396 /* delete all known tree nodes. They are all allocated inline */
1397 list_for_each_entry_safe(tree, tree_bck, &elt->tree_head, from_ref) {
1398 BUG_ON(tree->ref != elt);
1399
1400 ebmb_delete(&tree->node);
1401 LIST_DEL(&tree->from_ref);
1402 free(tree->data);
1403 free(tree);
1404 }
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001405
Willy Tarreauf1c08922020-11-02 19:53:16 +01001406 /* delete all list nodes and free their pattern entries (str/reg) */
1407 list_for_each_entry_safe(pat, pat_bck, &elt->list_head, from_ref) {
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001408 /* Check equality. */
Willy Tarreau78777ea2020-11-02 13:55:22 +01001409 BUG_ON(pat->pat.ref != elt);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001410
1411 /* Delete and free entry. */
1412 LIST_DEL(&pat->list);
Willy Tarreau4bdd0a12020-11-02 12:10:48 +01001413 LIST_DEL(&pat->from_ref);
Willy Tarreau6d8a6892020-11-02 19:26:02 +01001414 if (pat->pat.sflags & PAT_SF_REGFREE)
1415 regex_free(pat->pat.ptr.reg);
1416 else
1417 free(pat->pat.ptr.ptr);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001418 free(pat->pat.data);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001419 free(pat);
1420 }
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001421
Willy Tarreauf1c08922020-11-02 19:53:16 +01001422 /* update revision number to refresh the cache */
1423 ref->revision = rdtsc();
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001424}
1425
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001426void pattern_init_expr(struct pattern_expr *expr)
1427{
1428 LIST_INIT(&expr->patterns);
Thierry FOURNIER31db4ae2014-01-30 00:27:15 +01001429 expr->pattern_tree = EB_ROOT;
1430 expr->pattern_tree_2 = EB_ROOT;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001431}
1432
1433void pattern_init_head(struct pattern_head *head)
1434{
1435 LIST_INIT(&head->head);
1436}
1437
1438/* The following functions are relative to the management of the reference
1439 * lists. These lists are used to store the original pattern and associated
1440 * value as string form.
1441 *
1442 * This is used with modifiable ACL and MAPS
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001443 *
1444 * The pattern reference are stored with two identifiers: the unique_id and
1445 * the reference.
1446 *
1447 * The reference identify a file. Each file with the same name point to the
1448 * same reference. We can register many times one file. If the file is modified,
1449 * all his dependencies are also modified. The reference can be used with map or
1450 * acl.
1451 *
1452 * The unique_id identify inline acl. The unique id is unique for each acl.
1453 * You cannot force the same id in the configuration file, because this repoort
1454 * an error.
1455 *
1456 * A particular case appears if the filename is a number. In this case, the
1457 * unique_id is set with the number represented by the filename and the
1458 * reference is also set. This method prevent double unique_id.
1459 *
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001460 */
1461
Willy Tarreaua5bbaaf2020-10-30 16:03:50 +01001462/* This function looks up a reference by name. If the reference is found, a
1463 * pointer to the struct pat_ref is returned, otherwise NULL is returned.
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001464 */
1465struct pat_ref *pat_ref_lookup(const char *reference)
1466{
1467 struct pat_ref *ref;
1468
1469 list_for_each_entry(ref, &pattern_reference, list)
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001470 if (ref->reference && strcmp(reference, ref->reference) == 0)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001471 return ref;
1472 return NULL;
1473}
1474
Willy Tarreaua5bbaaf2020-10-30 16:03:50 +01001475/* This function looks up a reference's unique id. If the reference is found, a
1476 * pointer to the struct pat_ref is returned, otherwise NULL is returned.
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001477 */
1478struct pat_ref *pat_ref_lookupid(int unique_id)
1479{
1480 struct pat_ref *ref;
1481
1482 list_for_each_entry(ref, &pattern_reference, list)
1483 if (ref->unique_id == unique_id)
1484 return ref;
1485 return NULL;
1486}
1487
Willy Tarreau1fd52f72020-11-02 17:30:17 +01001488/* This function removes from the pattern reference <ref> all the patterns
1489 * attached to the reference element <elt>, and the element itself. The
1490 * reference must be locked.
1491 */
1492void pat_ref_delete_by_ptr(struct pat_ref *ref, struct pat_ref_elt *elt)
1493{
1494 struct pattern_expr *expr;
1495 struct bref *bref, *back;
1496
1497 /*
1498 * we have to unlink all watchers from this reference pattern. We must
1499 * not relink them if this elt was the last one in the list.
1500 */
1501 list_for_each_entry_safe(bref, back, &elt->back_refs, users) {
1502 LIST_DEL(&bref->users);
1503 LIST_INIT(&bref->users);
1504 if (elt->list.n != &ref->head)
1505 LIST_ADDQ(&LIST_ELEM(elt->list.n, typeof(elt), list)->back_refs, &bref->users);
1506 bref->ref = elt->list.n;
1507 }
1508
1509 /* delete all entries from all expressions for this pattern */
1510 list_for_each_entry(expr, &ref->pat, list)
1511 HA_RWLOCK_WRLOCK(PATEXP_LOCK, &expr->lock);
1512
1513 pat_delete_gen(ref, elt);
1514
1515 list_for_each_entry(expr, &ref->pat, list)
1516 HA_RWLOCK_WRUNLOCK(PATEXP_LOCK, &expr->lock);
1517
1518 LIST_DEL(&elt->list);
1519 free(elt->sample);
1520 free(elt->pattern);
1521 free(elt);
1522}
1523
Willy Tarreaua5bbaaf2020-10-30 16:03:50 +01001524/* This function removes all the patterns matching the pointer <refelt> from
1525 * the reference and from each expr member of this reference. This function
1526 * returns 1 if the entry was found and deleted, otherwise zero.
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001527 */
1528int pat_ref_delete_by_id(struct pat_ref *ref, struct pat_ref_elt *refelt)
1529{
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001530 struct pat_ref_elt *elt, *safe;
1531
1532 /* delete pattern from reference */
1533 list_for_each_entry_safe(elt, safe, &ref->head, list) {
1534 if (elt == refelt) {
Willy Tarreau1fd52f72020-11-02 17:30:17 +01001535 pat_ref_delete_by_ptr(ref, elt);
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001536 return 1;
1537 }
1538 }
1539 return 0;
1540}
1541
Willy Tarreau1fd52f72020-11-02 17:30:17 +01001542/* This function removes all patterns matching <key> from the reference
Joseph Herlant4189d672018-11-15 10:22:31 -08001543 * and from each expr member of the reference. This function returns 1
Willy Tarreau1fd52f72020-11-02 17:30:17 +01001544 * if the deletion is done and returns 0 is the entry is not found.
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001545 */
1546int pat_ref_delete(struct pat_ref *ref, const char *key)
1547{
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001548 struct pat_ref_elt *elt, *safe;
1549 int found = 0;
1550
1551 /* delete pattern from reference */
1552 list_for_each_entry_safe(elt, safe, &ref->head, list) {
1553 if (strcmp(key, elt->pattern) == 0) {
Willy Tarreau1fd52f72020-11-02 17:30:17 +01001554 pat_ref_delete_by_ptr(ref, elt);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001555 found = 1;
1556 }
1557 }
1558
Willy Tarreau1fd52f72020-11-02 17:30:17 +01001559 return found;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001560}
1561
Baptiste Assmann953f74d2014-04-25 16:57:03 +02001562/*
1563 * find and return an element <elt> matching <key> in a reference <ref>
1564 * return NULL if not found
1565 */
1566struct pat_ref_elt *pat_ref_find_elt(struct pat_ref *ref, const char *key)
1567{
1568 struct pat_ref_elt *elt;
1569
1570 list_for_each_entry(elt, &ref->head, list) {
1571 if (strcmp(key, elt->pattern) == 0)
1572 return elt;
1573 }
1574
1575 return NULL;
1576}
1577
1578
Willy Tarreaua5bbaaf2020-10-30 16:03:50 +01001579/* This function modifies the sample of pat_ref_elt <elt> in all expressions
1580 * found under <ref> to become <value>. It is assumed that the caller has
1581 * already verified that <elt> belongs to <ref>.
1582 */
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001583static inline int pat_ref_set_elt(struct pat_ref *ref, struct pat_ref_elt *elt,
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001584 const char *value, char **err)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001585{
1586 struct pattern_expr *expr;
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001587 struct sample_data **data;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001588 char *sample;
Thierry FOURNIER12ba0c22015-08-14 00:02:11 +02001589 struct sample_data test;
Thierry FOURNIER149e0fe2014-01-29 19:35:06 +01001590
1591 /* Try all needed converters. */
1592 list_for_each_entry(expr, &ref->pat, list) {
1593 if (!expr->pat_head->parse_smp)
1594 continue;
1595
1596 if (!expr->pat_head->parse_smp(value, &test)) {
1597 memprintf(err, "unable to parse '%s'", value);
1598 return 0;
1599 }
1600 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001601
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001602 /* Modify pattern from reference. */
1603 sample = strdup(value);
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001604 if (!sample) {
1605 memprintf(err, "out of memory error");
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001606 return 0;
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001607 }
Willy Tarreaua5bbaaf2020-10-30 16:03:50 +01001608 /* Load sample in each reference. All the conversions are tested
1609 * below, normally these calls don't fail.
Thierry FOURNIER149e0fe2014-01-29 19:35:06 +01001610 */
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001611 list_for_each_entry(expr, &ref->pat, list) {
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001612 if (!expr->pat_head->parse_smp)
1613 continue;
1614
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001615 HA_RWLOCK_WRLOCK(PATEXP_LOCK, &expr->lock);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001616 data = pattern_find_smp(expr, elt);
1617 if (data && *data && !expr->pat_head->parse_smp(sample, *data))
1618 *data = NULL;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001619 HA_RWLOCK_WRUNLOCK(PATEXP_LOCK, &expr->lock);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001620 }
1621
Emeric Brunb5997f72017-07-03 11:34:05 +02001622 /* free old sample only when all exprs are updated */
1623 free(elt->sample);
1624 elt->sample = sample;
1625
1626
Thierry FOURNIER149e0fe2014-01-29 19:35:06 +01001627 return 1;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001628}
1629
Willy Tarreaua5bbaaf2020-10-30 16:03:50 +01001630/* This function modifies the sample of pat_ref_elt <refelt> in all expressions
1631 * found under <ref> to become <value>, after checking that <refelt> really
1632 * belongs to <ref>.
1633 */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001634int 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 +01001635{
1636 struct pat_ref_elt *elt;
1637
1638 /* Look for pattern in the reference. */
1639 list_for_each_entry(elt, &ref->head, list) {
1640 if (elt == refelt) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001641 if (!pat_ref_set_elt(ref, elt, value, err))
1642 return 0;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001643 return 1;
1644 }
1645 }
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001646
1647 memprintf(err, "key or pattern not found");
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001648 return 0;
1649}
1650
Willy Tarreaua5bbaaf2020-10-30 16:03:50 +01001651/* This function modifies to <value> the sample of all patterns matching <key>
1652 * under <ref>.
1653 */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001654int pat_ref_set(struct pat_ref *ref, const char *key, const char *value, char **err)
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001655{
1656 struct pat_ref_elt *elt;
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001657 int found = 0;
1658 char *_merr;
1659 char **merr;
1660
1661 if (err) {
1662 merr = &_merr;
1663 *merr = NULL;
1664 }
1665 else
1666 merr = NULL;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001667
1668 /* Look for pattern in the reference. */
1669 list_for_each_entry(elt, &ref->head, list) {
1670 if (strcmp(key, elt->pattern) == 0) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001671 if (!pat_ref_set_elt(ref, elt, value, merr)) {
William Lallemand579fb252018-06-11 10:53:46 +02001672 if (err && merr) {
1673 if (!found) {
1674 *err = *merr;
1675 } else {
1676 memprintf(err, "%s, %s", *err, *merr);
1677 free(*merr);
1678 *merr = NULL;
1679 }
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001680 }
1681 }
1682 found = 1;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001683 }
1684 }
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001685
1686 if (!found) {
1687 memprintf(err, "entry not found");
1688 return 0;
1689 }
1690 return 1;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001691}
1692
Joseph Herlant4189d672018-11-15 10:22:31 -08001693/* This function creates a new reference. <ref> is the reference name.
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001694 * <flags> are PAT_REF_*. /!\ The reference is not checked, and must
1695 * be unique. The user must check the reference with "pat_ref_lookup()"
Willy Tarreaua5bbaaf2020-10-30 16:03:50 +01001696 * before calling this function. If the function fails, it returns NULL,
1697 * otherwise it returns the new struct pat_ref.
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001698 */
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001699struct pat_ref *pat_ref_new(const char *reference, const char *display, unsigned int flags)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001700{
1701 struct pat_ref *ref;
1702
Willy Tarreau8135d9b2020-10-30 15:35:11 +01001703 ref = calloc(1, sizeof(*ref));
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001704 if (!ref)
1705 return NULL;
1706
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001707 if (display) {
1708 ref->display = strdup(display);
1709 if (!ref->display) {
1710 free(ref);
1711 return NULL;
1712 }
1713 }
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001714
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001715 ref->reference = strdup(reference);
1716 if (!ref->reference) {
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001717 free(ref->display);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001718 free(ref);
1719 return NULL;
1720 }
1721
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001722 ref->flags = flags;
1723 ref->unique_id = -1;
Willy Tarreau3ee0de12020-11-02 15:26:51 +01001724 ref->revision = 0;
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001725
1726 LIST_INIT(&ref->head);
1727 LIST_INIT(&ref->pat);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001728 HA_SPIN_INIT(&ref->lock);
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001729 LIST_ADDQ(&pattern_reference, &ref->list);
1730
1731 return ref;
1732}
1733
Willy Tarreaua5bbaaf2020-10-30 16:03:50 +01001734/* This function creates a new reference. <unique_id> is the unique id. If
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001735 * the value of <unique_id> is -1, the unique id is calculated later.
1736 * <flags> are PAT_REF_*. /!\ The reference is not checked, and must
1737 * be unique. The user must check the reference with "pat_ref_lookup()"
1738 * or pat_ref_lookupid before calling this function. If the function
Willy Tarreaua5bbaaf2020-10-30 16:03:50 +01001739 * fails, it returns NULL, otherwise it returns the new struct pat_ref.
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001740 */
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001741struct pat_ref *pat_ref_newid(int unique_id, const char *display, unsigned int flags)
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001742{
1743 struct pat_ref *ref;
1744
Willy Tarreau8135d9b2020-10-30 15:35:11 +01001745 ref = calloc(1, sizeof(*ref));
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001746 if (!ref)
1747 return NULL;
1748
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001749 if (display) {
1750 ref->display = strdup(display);
1751 if (!ref->display) {
1752 free(ref);
1753 return NULL;
1754 }
1755 }
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001756
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001757 ref->reference = NULL;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001758 ref->flags = flags;
Willy Tarreau29947742020-10-28 11:43:49 +01001759 ref->curr_gen = 0;
1760 ref->next_gen = 0;
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001761 ref->unique_id = unique_id;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001762 LIST_INIT(&ref->head);
1763 LIST_INIT(&ref->pat);
Aurélien Nephtali564d15a2018-04-19 16:56:07 +02001764 HA_SPIN_INIT(&ref->lock);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001765 LIST_ADDQ(&pattern_reference, &ref->list);
1766
1767 return ref;
1768}
1769
Willy Tarreauf4edb722020-10-28 10:52:46 +01001770/* This function adds entry to <ref>. It can fail on memory error. It returns
1771 * the newly added element on success, or NULL on failure. The PATREF_LOCK on
Willy Tarreau29947742020-10-28 11:43:49 +01001772 * <ref> must be held. It sets the newly created pattern's generation number
1773 * to the same value as the reference's.
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001774 */
Willy Tarreauf4edb722020-10-28 10:52:46 +01001775struct pat_ref_elt *pat_ref_append(struct pat_ref *ref, const char *pattern, const char *sample, int line)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001776{
1777 struct pat_ref_elt *elt;
1778
Willy Tarreau8135d9b2020-10-30 15:35:11 +01001779 elt = calloc(1, sizeof(*elt));
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001780 if (!elt)
Willy Tarreauf4edb722020-10-28 10:52:46 +01001781 goto fail;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001782
Willy Tarreau29947742020-10-28 11:43:49 +01001783 elt->gen_id = ref->curr_gen;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001784 elt->line = line;
1785
1786 elt->pattern = strdup(pattern);
Willy Tarreauf4edb722020-10-28 10:52:46 +01001787 if (!elt->pattern)
1788 goto fail;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001789
1790 if (sample) {
1791 elt->sample = strdup(sample);
Willy Tarreauf4edb722020-10-28 10:52:46 +01001792 if (!elt->sample)
1793 goto fail;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001794 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001795
Emeric Brun8d85aa42017-06-29 15:40:33 +02001796 LIST_INIT(&elt->back_refs);
Willy Tarreau4bdd0a12020-11-02 12:10:48 +01001797 LIST_INIT(&elt->list_head);
1798 LIST_INIT(&elt->tree_head);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001799 LIST_ADDQ(&ref->head, &elt->list);
Willy Tarreauf4edb722020-10-28 10:52:46 +01001800 return elt;
1801 fail:
1802 if (elt)
1803 free(elt->pattern);
1804 free(elt);
1805 return NULL;
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001806}
1807
Willy Tarreaua5bbaaf2020-10-30 16:03:50 +01001808/* This function creates sample found in <elt>, parses the pattern also
1809 * found in <elt> and inserts it in <expr>. The function copies <patflags>
1810 * into <expr>. If the function fails, it returns 0 and <err> is filled.
Ilya Shipitsin47d17182020-06-21 21:42:57 +05001811 * In success case, the function returns 1.
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001812 */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001813int pat_ref_push(struct pat_ref_elt *elt, struct pattern_expr *expr,
1814 int patflags, char **err)
1815{
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001816 struct sample_data *data;
Thierry FOURNIERd25c8422014-01-28 15:34:35 +01001817 struct pattern pattern;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001818
1819 /* Create sample */
1820 if (elt->sample && expr->pat_head->parse_smp) {
1821 /* New sample. */
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001822 data = malloc(sizeof(*data));
1823 if (!data)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001824 return 0;
1825
1826 /* Parse value. */
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001827 if (!expr->pat_head->parse_smp(elt->sample, data)) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001828 memprintf(err, "unable to parse '%s'", elt->sample);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001829 free(data);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001830 return 0;
1831 }
1832
1833 }
1834 else
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001835 data = NULL;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001836
Thierry FOURNIERd25c8422014-01-28 15:34:35 +01001837 /* initialise pattern */
1838 memset(&pattern, 0, sizeof(pattern));
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001839 pattern.data = data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01001840 pattern.ref = elt;
Thierry FOURNIERd25c8422014-01-28 15:34:35 +01001841
1842 /* parse pattern */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02001843 if (!expr->pat_head->parse(elt->pattern, &pattern, expr->mflags, err)) {
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001844 free(data);
Thierry FOURNIERd25c8422014-01-28 15:34:35 +01001845 return 0;
1846 }
1847
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001848 HA_RWLOCK_WRLOCK(PATEXP_LOCK, &expr->lock);
Thierry FOURNIERd25c8422014-01-28 15:34:35 +01001849 /* index pattern */
1850 if (!expr->pat_head->index(expr, &pattern, err)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001851 HA_RWLOCK_WRUNLOCK(PATEXP_LOCK, &expr->lock);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001852 free(data);
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001853 return 0;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001854 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001855 HA_RWLOCK_WRUNLOCK(PATEXP_LOCK, &expr->lock);
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001856
1857 return 1;
1858}
1859
Willy Tarreau6a174072020-10-28 10:58:05 +01001860/* This function adds entry to <ref>. It can fail on memory error. The new
Thierry FOURNIER31db4ae2014-01-30 00:27:15 +01001861 * entry is added at all the pattern_expr registered in this reference. The
Willy Tarreau6a174072020-10-28 10:58:05 +01001862 * function stops on the first error encountered. It returns 0 and <err> is
Thierry FOURNIER31db4ae2014-01-30 00:27:15 +01001863 * filled. If an error is encountered, the complete add operation is cancelled.
1864 * If the insertion is a success the function returns 1.
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001865 */
1866int pat_ref_add(struct pat_ref *ref,
1867 const char *pattern, const char *sample,
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02001868 char **err)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001869{
1870 struct pat_ref_elt *elt;
1871 struct pattern_expr *expr;
1872
Willy Tarreau6a174072020-10-28 10:58:05 +01001873 elt = pat_ref_append(ref, pattern, sample, -1);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001874 if (!elt) {
1875 memprintf(err, "out of memory error");
1876 return 0;
1877 }
1878
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001879 list_for_each_entry(expr, &ref->pat, list) {
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02001880 if (!pat_ref_push(elt, expr, 0, err)) {
Thierry FOURNIER31db4ae2014-01-30 00:27:15 +01001881 /* If the insertion fails, try to delete all the added entries. */
Willy Tarreau1fd52f72020-11-02 17:30:17 +01001882 pat_ref_delete_by_ptr(ref, elt);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001883 return 0;
1884 }
1885 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001886 return 1;
1887}
1888
Joseph Herlant4189d672018-11-15 10:22:31 -08001889/* This function prunes <ref>, replaces all references by the references
1890 * of <replace>, and reindexes all the news values.
Thierry FOURNIER46006bd2014-03-21 21:45:15 +01001891 *
Joseph Herlant4189d672018-11-15 10:22:31 -08001892 * The patterns are loaded in best effort and the errors are ignored,
1893 * but written in the logs.
Thierry FOURNIER46006bd2014-03-21 21:45:15 +01001894 */
1895void pat_ref_reload(struct pat_ref *ref, struct pat_ref *replace)
1896{
1897 struct pattern_expr *expr;
Emeric Brunb5997f72017-07-03 11:34:05 +02001898 struct pat_ref_elt *elt, *safe;
1899 struct bref *bref, *back;
Emeric Brunb5997f72017-07-03 11:34:05 +02001900 struct pattern pattern;
Thierry FOURNIER46006bd2014-03-21 21:45:15 +01001901
Thierry FOURNIER46006bd2014-03-21 21:45:15 +01001902
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001903 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Emeric Brunb5997f72017-07-03 11:34:05 +02001904 list_for_each_entry(expr, &ref->pat, list) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001905 HA_RWLOCK_WRLOCK(PATEXP_LOCK, &expr->lock);
Emeric Brunb5997f72017-07-03 11:34:05 +02001906 }
1907
1908 /* all expr are locked, we can safely remove all pat_ref */
1909 list_for_each_entry_safe(elt, safe, &ref->head, list) {
1910 list_for_each_entry_safe(bref, back, &elt->back_refs, users) {
Willy Tarreaud4164dc2020-10-27 18:55:20 +01001911 /* we have to unlink all watchers. */
1912 LIST_DEL_INIT(&bref->users);
1913 bref->ref = NULL;
Emeric Brunb5997f72017-07-03 11:34:05 +02001914 }
1915 LIST_DEL(&elt->list);
Willy Tarreau4bdd0a12020-11-02 12:10:48 +01001916 LIST_DEL(&elt->list_head);
1917 LIST_DEL(&elt->tree_head);
Emeric Brunb5997f72017-07-03 11:34:05 +02001918 free(elt->pattern);
1919 free(elt->sample);
1920 free(elt);
1921 }
1922
1923 /* switch pat_ret_elt lists */
Thierry FOURNIER46006bd2014-03-21 21:45:15 +01001924 LIST_ADD(&replace->head, &ref->head);
1925 LIST_DEL(&replace->head);
1926
Emeric Brunb5997f72017-07-03 11:34:05 +02001927 list_for_each_entry(expr, &ref->pat, list) {
1928 expr->pat_head->prune(expr);
1929 list_for_each_entry(elt, &ref->head, list) {
Dragan Dosenf1474792018-09-18 20:18:09 +02001930 char *err = NULL;
1931 struct sample_data *data = NULL;
1932
Emeric Brunb5997f72017-07-03 11:34:05 +02001933 /* Create sample */
1934 if (elt->sample && expr->pat_head->parse_smp) {
1935 /* New sample. */
1936 data = malloc(sizeof(*data));
1937 if (!data)
1938 continue;
1939
1940 /* Parse value. */
1941 if (!expr->pat_head->parse_smp(elt->sample, data)) {
1942 memprintf(&err, "unable to parse '%s'", elt->sample);
1943 send_log(NULL, LOG_NOTICE, "%s", err);
1944 free(err);
1945 free(data);
1946 continue;
1947 }
1948
1949 }
Emeric Brunb5997f72017-07-03 11:34:05 +02001950
1951 /* initialise pattern */
1952 memset(&pattern, 0, sizeof(pattern));
1953 pattern.data = data;
1954 pattern.ref = elt;
1955
1956 /* parse pattern */
1957 if (!expr->pat_head->parse(elt->pattern, &pattern, expr->mflags, &err)) {
Thierry FOURNIER46006bd2014-03-21 21:45:15 +01001958 send_log(NULL, LOG_NOTICE, "%s", err);
1959 free(err);
Emeric Brunb5997f72017-07-03 11:34:05 +02001960 free(data);
1961 continue;
1962 }
1963
1964 /* index pattern */
1965 if (!expr->pat_head->index(expr, &pattern, &err)) {
1966 send_log(NULL, LOG_NOTICE, "%s", err);
1967 free(err);
1968 free(data);
1969 continue;
Thierry FOURNIER46006bd2014-03-21 21:45:15 +01001970 }
1971 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001972 HA_RWLOCK_WRUNLOCK(PATEXP_LOCK, &expr->lock);
Thierry FOURNIER46006bd2014-03-21 21:45:15 +01001973 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001974 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Willy Tarreau114d6982020-11-03 15:55:35 +01001975
1976#if defined(HA_HAVE_MALLOC_TRIM)
1977 malloc_trim(0);
1978#endif
Thierry FOURNIER46006bd2014-03-21 21:45:15 +01001979}
1980
Willy Tarreaua5bbaaf2020-10-30 16:03:50 +01001981/* This function prunes all entries of <ref>. This function
Willy Tarreaud1d005d2019-12-20 18:22:02 +01001982 * prunes the associated pattern_expr. It may return before the end of
1983 * the list is reached, returning 0, to yield. The caller must call it
1984 * again. Otherwise it returns 1 once done.
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001985 */
Willy Tarreaud1d005d2019-12-20 18:22:02 +01001986int pat_ref_prune(struct pat_ref *ref)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001987{
1988 struct pat_ref_elt *elt, *safe;
1989 struct pattern_expr *expr;
Emeric Brun8d85aa42017-06-29 15:40:33 +02001990 struct bref *bref, *back;
Willy Tarreaud1d005d2019-12-20 18:22:02 +01001991 int loops = 0;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001992
Emeric Brunb5997f72017-07-03 11:34:05 +02001993 list_for_each_entry(expr, &ref->pat, list) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001994 HA_RWLOCK_WRLOCK(PATEXP_LOCK, &expr->lock);
Emeric Brunb5997f72017-07-03 11:34:05 +02001995 expr->pat_head->prune(expr);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001996 HA_RWLOCK_WRUNLOCK(PATEXP_LOCK, &expr->lock);
Willy Tarreaud1d005d2019-12-20 18:22:02 +01001997 loops++;
1998 /* yield often, some lists may be huge, especially those
1999 * having to be freed through free_pattern_tree()
2000 */
2001 if (loops > 10)
2002 return 0;
Emeric Brunb5997f72017-07-03 11:34:05 +02002003 }
2004
2005 /* we trash pat_ref_elt in a second time to ensure that data is
2006 free once there is no ref on it */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002007 list_for_each_entry_safe(elt, safe, &ref->head, list) {
Emeric Brun8d85aa42017-06-29 15:40:33 +02002008 list_for_each_entry_safe(bref, back, &elt->back_refs, users) {
2009 /*
2010 * we have to unlink all watchers. We must not relink them if
2011 * this elt was the last one in the list.
2012 */
2013 LIST_DEL(&bref->users);
2014 LIST_INIT(&bref->users);
2015 if (elt->list.n != &ref->head)
Willy Tarreau49ee3b22019-04-30 11:43:43 +02002016 LIST_ADDQ(&LIST_ELEM(elt->list.n, typeof(elt), list)->back_refs, &bref->users);
Emeric Brun8d85aa42017-06-29 15:40:33 +02002017 bref->ref = elt->list.n;
2018 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002019 LIST_DEL(&elt->list);
2020 free(elt->pattern);
2021 free(elt->sample);
2022 free(elt);
Willy Tarreaud1d005d2019-12-20 18:22:02 +01002023 loops++;
2024 if (loops > 100000)
2025 return 0;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002026 }
Willy Tarreaud1d005d2019-12-20 18:22:02 +01002027 return 1;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002028}
2029
Willy Tarreaua5bbaaf2020-10-30 16:03:50 +01002030/* This function looks up any existing reference <ref> in pattern_head <head>, and
2031 * returns the associated pattern_expr pointer if found, otherwise NULL.
2032 */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002033struct pattern_expr *pattern_lookup_expr(struct pattern_head *head, struct pat_ref *ref)
2034{
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002035 struct pattern_expr_list *expr;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002036
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002037 list_for_each_entry(expr, &head->head, list)
2038 if (expr->expr->ref == ref)
2039 return expr->expr;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002040 return NULL;
2041}
2042
Joseph Herlant4189d672018-11-15 10:22:31 -08002043/* This function creates new pattern_expr associated to the reference <ref>.
2044 * <ref> can be NULL. If an error occurs, the function returns NULL and
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002045 * <err> is filled. Otherwise, the function returns new pattern_expr linked
2046 * with <head> and <ref>.
Thierry FOURNIER315ec422014-11-24 11:14:42 +01002047 *
Joseph Herlant4189d672018-11-15 10:22:31 -08002048 * The returned value can be an already filled pattern list, in this case the
Thierry FOURNIER315ec422014-11-24 11:14:42 +01002049 * flag <reuse> is set.
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002050 */
Thierry FOURNIER315ec422014-11-24 11:14:42 +01002051struct pattern_expr *pattern_new_expr(struct pattern_head *head, struct pat_ref *ref,
Emeric Brun7d27f3c2017-07-03 17:54:23 +02002052 int patflags, char **err, int *reuse)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002053{
2054 struct pattern_expr *expr;
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002055 struct pattern_expr_list *list;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002056
Thierry FOURNIER315ec422014-11-24 11:14:42 +01002057 if (reuse)
2058 *reuse = 0;
2059
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002060 /* Memory and initialization of the chain element. */
Willy Tarreau8135d9b2020-10-30 15:35:11 +01002061 list = calloc(1, sizeof(*list));
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002062 if (!list) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002063 memprintf(err, "out of memory");
2064 return NULL;
2065 }
2066
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002067 /* Look for existing similar expr. No that only the index, parse and
2068 * parse_smp function must be identical for having similar pattern.
Joseph Herlant4189d672018-11-15 10:22:31 -08002069 * The other function depends of these first.
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002070 */
2071 if (ref) {
2072 list_for_each_entry(expr, &ref->pat, list)
2073 if (expr->pat_head->index == head->index &&
2074 expr->pat_head->parse == head->parse &&
Emeric Brun7d27f3c2017-07-03 17:54:23 +02002075 expr->pat_head->parse_smp == head->parse_smp &&
2076 expr->mflags == patflags)
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002077 break;
2078 if (&expr->list == &ref->pat)
2079 expr = NULL;
2080 }
2081 else
2082 expr = NULL;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002083
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002084 /* If no similar expr was found, we create new expr. */
2085 if (!expr) {
2086 /* Get a lot of memory for the expr struct. */
Willy Tarreau8135d9b2020-10-30 15:35:11 +01002087 expr = calloc(1, sizeof(*expr));
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002088 if (!expr) {
Andreas Seltenreiche6e22e82016-03-03 20:20:23 +01002089 free(list);
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002090 memprintf(err, "out of memory");
2091 return NULL;
2092 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002093
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002094 /* Initialize this new expr. */
2095 pattern_init_expr(expr);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002096
Emeric Brun7d27f3c2017-07-03 17:54:23 +02002097 /* Copy the pattern matching and indexing flags. */
2098 expr->mflags = patflags;
2099
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002100 /* This new pattern expression reference one of his heads. */
2101 expr->pat_head = head;
2102
2103 /* Link with ref, or to self to facilitate LIST_DEL() */
2104 if (ref)
2105 LIST_ADDQ(&ref->pat, &expr->list);
2106 else
2107 LIST_INIT(&expr->list);
2108
2109 expr->ref = ref;
2110
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002111 HA_RWLOCK_INIT(&expr->lock);
Emeric Brunb5997f72017-07-03 11:34:05 +02002112
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002113 /* We must free this pattern if it is no more used. */
2114 list->do_free = 1;
2115 }
2116 else {
2117 /* If the pattern used already exists, it is already linked
2118 * with ref and we must not free it.
2119 */
2120 list->do_free = 0;
Thierry FOURNIER315ec422014-11-24 11:14:42 +01002121 if (reuse)
2122 *reuse = 1;
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002123 }
2124
2125 /* The new list element reference the pattern_expr. */
2126 list->expr = expr;
2127
2128 /* Link the list element with the pattern_head. */
2129 LIST_ADDQ(&head->head, &list->list);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002130 return expr;
2131}
2132
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002133/* Reads patterns from a file. If <err_msg> is non-NULL, an error message will
2134 * be returned there on errors and the caller will have to free it.
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002135 *
2136 * The file contains one key + value per line. Lines which start with '#' are
2137 * ignored, just like empty lines. Leading tabs/spaces are stripped. The key is
2138 * then the first "word" (series of non-space/tabs characters), and the value is
2139 * what follows this series of space/tab till the end of the line excluding
2140 * trailing spaces/tabs.
2141 *
2142 * Example :
2143 *
2144 * # this is a comment and is ignored
2145 * 62.212.114.60 1wt.eu \n
2146 * <-><-----------><---><----><---->
2147 * | | | | `--- trailing spaces ignored
2148 * | | | `-------- value
2149 * | | `--------------- middle spaces ignored
2150 * | `------------------------ key
2151 * `-------------------------------- leading spaces ignored
2152 *
Ilya Shipitsin47d17182020-06-21 21:42:57 +05002153 * Return non-zero in case of success, otherwise 0.
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002154 */
2155int pat_ref_read_from_file_smp(struct pat_ref *ref, const char *filename, char **err)
2156{
2157 FILE *file;
2158 char *c;
2159 int ret = 0;
2160 int line = 0;
2161 char *key_beg;
2162 char *key_end;
2163 char *value_beg;
2164 char *value_end;
2165
2166 file = fopen(filename, "r");
2167 if (!file) {
2168 memprintf(err, "failed to open pattern file <%s>", filename);
2169 return 0;
2170 }
2171
2172 /* now parse all patterns. The file may contain only one pattern
2173 * followed by one value per line. The start spaces, separator spaces
2174 * and and spaces are stripped. Each can contain comment started by '#'
2175 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002176 while (fgets(trash.area, trash.size, file) != NULL) {
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002177 line++;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002178 c = trash.area;
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002179
2180 /* ignore lines beginning with a dash */
2181 if (*c == '#')
2182 continue;
2183
2184 /* strip leading spaces and tabs */
2185 while (*c == ' ' || *c == '\t')
2186 c++;
2187
2188 /* empty lines are ignored too */
2189 if (*c == '\0' || *c == '\r' || *c == '\n')
2190 continue;
2191
2192 /* look for the end of the key */
2193 key_beg = c;
2194 while (*c && *c != ' ' && *c != '\t' && *c != '\n' && *c != '\r')
2195 c++;
2196
2197 key_end = c;
2198
2199 /* strip middle spaces and tabs */
2200 while (*c == ' ' || *c == '\t')
2201 c++;
2202
2203 /* look for the end of the value, it is the end of the line */
2204 value_beg = c;
2205 while (*c && *c != '\n' && *c != '\r')
2206 c++;
2207 value_end = c;
2208
2209 /* trim possibly trailing spaces and tabs */
2210 while (value_end > value_beg && (value_end[-1] == ' ' || value_end[-1] == '\t'))
2211 value_end--;
2212
2213 /* set final \0 and check entries */
2214 *key_end = '\0';
2215 *value_end = '\0';
2216
2217 /* insert values */
2218 if (!pat_ref_append(ref, key_beg, value_beg, line)) {
2219 memprintf(err, "out of memory");
2220 goto out_close;
2221 }
2222 }
2223
Jerome Magnin3c79d4b2020-01-17 16:09:33 +01002224 if (ferror(file)) {
2225 memprintf(err, "error encountered while reading <%s> : %s",
2226 filename, strerror(errno));
2227 goto out_close;
2228 }
Ilya Shipitsin47d17182020-06-21 21:42:57 +05002229 /* success */
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002230 ret = 1;
2231
2232 out_close:
2233 fclose(file);
2234 return ret;
2235}
2236
2237/* Reads patterns from a file. If <err_msg> is non-NULL, an error message will
2238 * be returned there on errors and the caller will have to free it.
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002239 */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002240int pat_ref_read_from_file(struct pat_ref *ref, const char *filename, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002241{
2242 FILE *file;
2243 char *c;
2244 char *arg;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002245 int ret = 0;
2246 int line = 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002247
2248 file = fopen(filename, "r");
2249 if (!file) {
2250 memprintf(err, "failed to open pattern file <%s>", filename);
2251 return 0;
2252 }
2253
2254 /* now parse all patterns. The file may contain only one pattern per
2255 * line. If the line contains spaces, they will be part of the pattern.
2256 * The pattern stops at the first CR, LF or EOF encountered.
2257 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002258 while (fgets(trash.area, trash.size, file) != NULL) {
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002259 line++;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002260 c = trash.area;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002261
2262 /* ignore lines beginning with a dash */
2263 if (*c == '#')
2264 continue;
2265
2266 /* strip leading spaces and tabs */
2267 while (*c == ' ' || *c == '\t')
2268 c++;
2269
2270
2271 arg = c;
2272 while (*c && *c != '\n' && *c != '\r')
2273 c++;
2274 *c = 0;
2275
2276 /* empty lines are ignored too */
2277 if (c == arg)
2278 continue;
2279
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002280 if (!pat_ref_append(ref, arg, NULL, line)) {
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002281 memprintf(err, "out of memory when loading patterns from file <%s>", filename);
2282 goto out_close;
2283 }
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002284 }
2285
Jerome Magnin3c79d4b2020-01-17 16:09:33 +01002286 if (ferror(file)) {
2287 memprintf(err, "error encountered while reading <%s> : %s",
2288 filename, strerror(errno));
2289 goto out_close;
2290 }
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002291 ret = 1; /* success */
2292
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002293 out_close:
2294 fclose(file);
2295 return ret;
2296}
2297
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002298int pattern_read_from_file(struct pattern_head *head, unsigned int refflags,
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002299 const char *filename, int patflags, int load_smp,
Thierry FOURNIER94580c92014-02-11 14:36:45 +01002300 char **err, const char *file, int line)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002301{
2302 struct pat_ref *ref;
2303 struct pattern_expr *expr;
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002304 struct pat_ref_elt *elt;
Willy Tarreau4deaf392014-11-26 13:17:03 +01002305 int reuse = 0;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002306
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002307 /* Lookup for the existing reference. */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002308 ref = pat_ref_lookup(filename);
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002309
2310 /* If the reference doesn't exists, create it and load associated file. */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002311 if (!ref) {
Thierry FOURNIER94580c92014-02-11 14:36:45 +01002312 chunk_printf(&trash,
2313 "pattern loaded from file '%s' used by %s at file '%s' line %d",
2314 filename, refflags & PAT_REF_MAP ? "map" : "acl", file, line);
2315
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002316 ref = pat_ref_new(filename, trash.area, refflags);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002317 if (!ref) {
2318 memprintf(err, "out of memory");
2319 return 0;
2320 }
2321
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002322 if (load_smp) {
Thierry FOURNIERc0bd9102014-01-29 12:32:58 +01002323 ref->flags |= PAT_REF_SMP;
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002324 if (!pat_ref_read_from_file_smp(ref, filename, err))
2325 return 0;
2326 }
2327 else {
2328 if (!pat_ref_read_from_file(ref, filename, err))
2329 return 0;
2330 }
2331 }
2332 else {
Thierry FOURNIERc0bd9102014-01-29 12:32:58 +01002333 /* The reference already exists, check the map compatibility. */
2334
2335 /* If the load require samples and the flag PAT_REF_SMP is not set,
2336 * the reference doesn't contain sample, and cannot be used.
2337 */
2338 if (load_smp) {
2339 if (!(ref->flags & PAT_REF_SMP)) {
2340 memprintf(err, "The file \"%s\" is already used as one column file "
2341 "and cannot be used by as two column file.",
2342 filename);
2343 return 0;
2344 }
2345 }
2346 else {
2347 /* The load doesn't require samples. If the flag PAT_REF_SMP is
2348 * set, the reference contains a sample, and cannot be used.
2349 */
2350 if (ref->flags & PAT_REF_SMP) {
2351 memprintf(err, "The file \"%s\" is already used as two column file "
2352 "and cannot be used by as one column file.",
2353 filename);
2354 return 0;
2355 }
2356 }
2357
Thierry FOURNIER94580c92014-02-11 14:36:45 +01002358 /* Extends display */
2359 chunk_printf(&trash, "%s", ref->display);
2360 chunk_appendf(&trash, ", by %s at file '%s' line %d",
2361 refflags & PAT_REF_MAP ? "map" : "acl", file, line);
2362 free(ref->display);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002363 ref->display = strdup(trash.area);
Thierry FOURNIER94580c92014-02-11 14:36:45 +01002364 if (!ref->display) {
2365 memprintf(err, "out of memory");
2366 return 0;
2367 }
2368
Thierry FOURNIERc0bd9102014-01-29 12:32:58 +01002369 /* Merge flags. */
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002370 ref->flags |= refflags;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002371 }
2372
2373 /* Now, we can loading patterns from the reference. */
2374
2375 /* Lookup for existing reference in the head. If the reference
2376 * doesn't exists, create it.
2377 */
2378 expr = pattern_lookup_expr(head, ref);
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02002379 if (!expr || (expr->mflags != patflags)) {
Emeric Brun7d27f3c2017-07-03 17:54:23 +02002380 expr = pattern_new_expr(head, ref, patflags, err, &reuse);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002381 if (!expr)
2382 return 0;
2383 }
2384
Thierry FOURNIER315ec422014-11-24 11:14:42 +01002385 /* The returned expression may be not empty, because the function
2386 * "pattern_new_expr" lookup for similar pattern list and can
2387 * reuse a already filled pattern list. In this case, we can not
2388 * reload the patterns.
2389 */
2390 if (reuse)
2391 return 1;
2392
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002393 /* Load reference content in the pattern expression. */
2394 list_for_each_entry(elt, &ref->head, list) {
2395 if (!pat_ref_push(elt, expr, patflags, err)) {
2396 if (elt->line > 0)
2397 memprintf(err, "%s at line %d of file '%s'",
2398 *err, elt->line, filename);
2399 return 0;
2400 }
2401 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002402
2403 return 1;
2404}
2405
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01002406/* This function executes a pattern match on a sample. It applies pattern <expr>
2407 * to sample <smp>. The function returns NULL if the sample dont match. It returns
2408 * non-null if the sample match. If <fill> is true and the sample match, the
2409 * function returns the matched pattern. In many cases, this pattern can be a
2410 * static buffer.
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002411 */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002412struct pattern *pattern_exec_match(struct pattern_head *head, struct sample *smp, int fill)
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002413{
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002414 struct pattern_expr_list *list;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002415 struct pattern *pat;
2416
2417 if (!head->match) {
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01002418 if (fill) {
Thierry FOURNIER503bb092015-08-19 08:35:43 +02002419 static_pattern.data = NULL;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01002420 static_pattern.ref = NULL;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02002421 static_pattern.sflags = 0;
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002422 static_pattern.type = SMP_T_SINT;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01002423 static_pattern.val.i = 1;
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01002424 }
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01002425 return &static_pattern;
2426 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002427
Thierry FOURNIER5d344082014-01-27 14:19:53 +01002428 /* convert input to string */
2429 if (!sample_convert(smp, head->expect_type))
2430 return NULL;
2431
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002432 list_for_each_entry(list, &head->head, list) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002433 HA_RWLOCK_RDLOCK(PATEXP_LOCK, &list->expr->lock);
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002434 pat = head->match(smp, list->expr, fill);
Emeric Brunb5997f72017-07-03 11:34:05 +02002435 if (pat) {
2436 /* We duplicate the pattern cause it could be modified
2437 by another thread */
2438 if (pat != &static_pattern) {
2439 memcpy(&static_pattern, pat, sizeof(struct pattern));
2440 pat = &static_pattern;
2441 }
2442
2443 /* We also duplicate the sample data for
2444 same reason */
2445 if (pat->data && (pat->data != &static_sample_data)) {
Christopher Faulet09fdf4b2017-11-09 16:14:16 +01002446 switch(pat->data->type) {
Emeric Brunb5997f72017-07-03 11:34:05 +02002447 case SMP_T_STR:
2448 static_sample_data.type = SMP_T_STR;
2449 static_sample_data.u.str = *get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002450 static_sample_data.u.str.data = pat->data->u.str.data;
2451 if (static_sample_data.u.str.data >= static_sample_data.u.str.size)
2452 static_sample_data.u.str.data = static_sample_data.u.str.size - 1;
2453 memcpy(static_sample_data.u.str.area,
Willy Tarreau2fc761e2020-06-11 16:37:35 +02002454 pat->data->u.str.area, static_sample_data.u.str.data);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002455 static_sample_data.u.str.area[static_sample_data.u.str.data] = 0;
Willy Tarreau2fc761e2020-06-11 16:37:35 +02002456 pat->data = &static_sample_data;
2457 break;
2458
Emeric Brunb5997f72017-07-03 11:34:05 +02002459 case SMP_T_IPV4:
2460 case SMP_T_IPV6:
2461 case SMP_T_SINT:
2462 memcpy(&static_sample_data, pat->data, sizeof(struct sample_data));
Willy Tarreau2fc761e2020-06-11 16:37:35 +02002463 pat->data = &static_sample_data;
2464 break;
Emeric Brunb5997f72017-07-03 11:34:05 +02002465 default:
Willy Tarreau2fc761e2020-06-11 16:37:35 +02002466 /* unimplemented pattern type */
Emeric Brunb5997f72017-07-03 11:34:05 +02002467 pat->data = NULL;
Willy Tarreau2fc761e2020-06-11 16:37:35 +02002468 break;
Emeric Brunb5997f72017-07-03 11:34:05 +02002469 }
Emeric Brunb5997f72017-07-03 11:34:05 +02002470 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002471 HA_RWLOCK_RDUNLOCK(PATEXP_LOCK, &list->expr->lock);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002472 return pat;
Emeric Brunb5997f72017-07-03 11:34:05 +02002473 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002474 HA_RWLOCK_RDUNLOCK(PATEXP_LOCK, &list->expr->lock);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002475 }
2476 return NULL;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002477}
2478
Willy Tarreaua5bbaaf2020-10-30 16:03:50 +01002479/* This function prunes the pattern expressions starting at pattern_head <head>. */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002480void pattern_prune(struct pattern_head *head)
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01002481{
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002482 struct pattern_expr_list *list, *safe;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002483
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002484 list_for_each_entry_safe(list, safe, &head->head, list) {
2485 LIST_DEL(&list->list);
2486 if (list->do_free) {
2487 LIST_DEL(&list->expr->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002488 HA_RWLOCK_WRLOCK(PATEXP_LOCK, &list->expr->lock);
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002489 head->prune(list->expr);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002490 HA_RWLOCK_WRUNLOCK(PATEXP_LOCK, &list->expr->lock);
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002491 free(list->expr);
2492 }
2493 free(list);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002494 }
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01002495}
2496
Willy Tarreaua5bbaaf2020-10-30 16:03:50 +01002497/* This function searches occurrences of pattern reference element <ref> in
2498 * expression <expr> and returns a pointer to a pointer of the sample storage.
2499 * If <ref> is not found, NULL is returned.
Thierry FOURNIER55d0b102014-01-15 11:25:26 +01002500 */
Thierry FOURNIER12ba0c22015-08-14 00:02:11 +02002501struct sample_data **pattern_find_smp(struct pattern_expr *expr, struct pat_ref_elt *ref)
Thierry FOURNIER55d0b102014-01-15 11:25:26 +01002502{
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01002503 struct ebmb_node *node;
2504 struct pattern_tree *elt;
2505 struct pattern_list *pat;
Thierry FOURNIER55d0b102014-01-15 11:25:26 +01002506
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01002507 for (node = ebmb_first(&expr->pattern_tree);
2508 node;
2509 node = ebmb_next(node)) {
2510 elt = container_of(node, struct pattern_tree, node);
2511 if (elt->ref == ref)
Thierry FOURNIER503bb092015-08-19 08:35:43 +02002512 return &elt->data;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01002513 }
2514
2515 for (node = ebmb_first(&expr->pattern_tree_2);
2516 node;
2517 node = ebmb_next(node)) {
2518 elt = container_of(node, struct pattern_tree, node);
2519 if (elt->ref == ref)
Thierry FOURNIER503bb092015-08-19 08:35:43 +02002520 return &elt->data;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01002521 }
2522
2523 list_for_each_entry(pat, &expr->patterns, list)
2524 if (pat->pat.ref == ref)
Thierry FOURNIER503bb092015-08-19 08:35:43 +02002525 return &pat->pat.data;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01002526
2527 return NULL;
Thierry FOURNIER55d0b102014-01-15 11:25:26 +01002528}
2529
Willy Tarreaua5bbaaf2020-10-30 16:03:50 +01002530/* This function compares two pat_ref** on their unique_id, and returns -1/0/1
2531 * depending on their order (suitable for sorting).
2532 */
Carl Henrik Lundef91ac192020-02-27 16:45:50 +01002533static int cmp_pat_ref(const void *_a, const void *_b)
2534{
2535 struct pat_ref * const *a = _a;
2536 struct pat_ref * const *b = _b;
2537
2538 if ((*a)->unique_id < (*b)->unique_id)
2539 return -1;
2540 else if ((*a)->unique_id > (*b)->unique_id)
2541 return 1;
2542 return 0;
2543}
2544
Willy Tarreaua5bbaaf2020-10-30 16:03:50 +01002545/* This function finalizes the configuration parsing. It sets all the
2546 * automatic ids.
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002547 */
Carl Henrik Lundef91ac192020-02-27 16:45:50 +01002548int pattern_finalize_config(void)
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002549{
Tim Duesterhusb584b442020-03-17 21:08:24 +01002550 size_t len = 0;
2551 size_t unassigned_pos = 0;
Carl Henrik Lundef91ac192020-02-27 16:45:50 +01002552 int next_unique_id = 0;
Tim Duesterhusb584b442020-03-17 21:08:24 +01002553 size_t i, j;
Carl Henrik Lundef91ac192020-02-27 16:45:50 +01002554 struct pat_ref *ref, **arr;
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002555 struct list pr = LIST_HEAD_INIT(pr);
2556
Willy Tarreau52bf8392020-03-08 00:42:37 +01002557 pat_lru_seed = ha_random();
Willy Tarreauf3045d22015-04-29 16:24:50 +02002558
Carl Henrik Lundef91ac192020-02-27 16:45:50 +01002559 /* Count pat_refs with user defined unique_id and totalt count */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002560 list_for_each_entry(ref, &pattern_reference, list) {
Carl Henrik Lundef91ac192020-02-27 16:45:50 +01002561 len++;
2562 if (ref->unique_id != -1)
2563 unassigned_pos++;
2564 }
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002565
Tim Duesterhusb584b442020-03-17 21:08:24 +01002566 if (len == 0) {
2567 return 0;
2568 }
2569
Carl Henrik Lundef91ac192020-02-27 16:45:50 +01002570 arr = calloc(len, sizeof(*arr));
2571 if (arr == NULL) {
2572 ha_alert("Out of memory error.\n");
2573 return ERR_ALERT | ERR_FATAL;
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002574 }
2575
Carl Henrik Lundef91ac192020-02-27 16:45:50 +01002576 i = 0;
2577 j = unassigned_pos;
2578 list_for_each_entry(ref, &pattern_reference, list) {
2579 if (ref->unique_id != -1)
2580 arr[i++] = ref;
2581 else
2582 arr[j++] = ref;
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002583 }
2584
Carl Henrik Lundef91ac192020-02-27 16:45:50 +01002585 /* Sort first segment of array with user-defined unique ids for
2586 * fast lookup when generating unique ids
2587 */
2588 qsort(arr, unassigned_pos, sizeof(*arr), cmp_pat_ref);
2589
2590 /* Assign unique ids to the rest of the elements */
2591 for (i = unassigned_pos; i < len; i++) {
2592 do {
2593 arr[i]->unique_id = next_unique_id++;
2594 } while (bsearch(&arr[i], arr, unassigned_pos, sizeof(*arr), cmp_pat_ref));
2595 }
2596
2597 /* Sort complete array */
2598 qsort(arr, len, sizeof(*arr), cmp_pat_ref);
2599
2600 /* Convert back to linked list */
2601 for (i = 0; i < len; i++)
2602 LIST_ADDQ(&pr, &arr[i]->list);
2603
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002604 /* swap root */
2605 LIST_ADD(&pr, &pattern_reference);
2606 LIST_DEL(&pr);
Carl Henrik Lundef91ac192020-02-27 16:45:50 +01002607
2608 free(arr);
2609 return 0;
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002610}
Willy Tarreau403bfbb2019-10-23 06:59:31 +02002611
2612static int pattern_per_thread_lru_alloc()
2613{
2614 if (!global.tune.pattern_cache)
2615 return 1;
2616 pat_lru_tree = lru64_new(global.tune.pattern_cache);
2617 return !!pat_lru_tree;
2618}
2619
2620static void pattern_per_thread_lru_free()
2621{
2622 lru64_destroy(pat_lru_tree);
2623}
2624
2625REGISTER_PER_THREAD_ALLOC(pattern_per_thread_lru_alloc);
2626REGISTER_PER_THREAD_FREE(pattern_per_thread_lru_free);