blob: 89a5c4844b20ba3c1f37d15a138a8aab9dce32ce [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
Willy Tarreau78777ea2020-11-02 13:55:22 +010082void (*pat_delete_fcts[PAT_MATCH_NUM])(struct pat_ref *, struct pat_ref_elt *) = {
Willy Tarreauf1c08922020-11-02 19:53:16 +010083 [PAT_MATCH_FOUND] = pat_delete_gen,
84 [PAT_MATCH_BOOL] = pat_delete_gen,
85 [PAT_MATCH_INT] = pat_delete_gen,
86 [PAT_MATCH_IP] = pat_delete_gen,
87 [PAT_MATCH_BIN] = pat_delete_gen,
88 [PAT_MATCH_LEN] = pat_delete_gen,
89 [PAT_MATCH_STR] = pat_delete_gen,
90 [PAT_MATCH_BEG] = pat_delete_gen,
91 [PAT_MATCH_SUB] = pat_delete_gen,
92 [PAT_MATCH_DIR] = pat_delete_gen,
93 [PAT_MATCH_DOM] = pat_delete_gen,
94 [PAT_MATCH_END] = pat_delete_gen,
95 [PAT_MATCH_REG] = pat_delete_gen,
96 [PAT_MATCH_REGM] = pat_delete_gen,
Thierry FOURNIERb1136502014-01-15 11:38:49 +010097};
98
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +010099void (*pat_prune_fcts[PAT_MATCH_NUM])(struct pattern_expr *) = {
Willy Tarreau6d8a6892020-11-02 19:26:02 +0100100 [PAT_MATCH_FOUND] = pat_prune_gen,
101 [PAT_MATCH_BOOL] = pat_prune_gen,
102 [PAT_MATCH_INT] = pat_prune_gen,
103 [PAT_MATCH_IP] = pat_prune_gen,
104 [PAT_MATCH_BIN] = pat_prune_gen,
105 [PAT_MATCH_LEN] = pat_prune_gen,
106 [PAT_MATCH_STR] = pat_prune_gen,
107 [PAT_MATCH_BEG] = pat_prune_gen,
108 [PAT_MATCH_SUB] = pat_prune_gen,
109 [PAT_MATCH_DIR] = pat_prune_gen,
110 [PAT_MATCH_DOM] = pat_prune_gen,
111 [PAT_MATCH_END] = pat_prune_gen,
112 [PAT_MATCH_REG] = pat_prune_gen,
113 [PAT_MATCH_REGM] = pat_prune_gen,
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +0100114};
115
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100116struct pattern *(*pat_match_fcts[PAT_MATCH_NUM])(struct sample *, struct pattern_expr *, int) = {
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100117 [PAT_MATCH_FOUND] = NULL,
118 [PAT_MATCH_BOOL] = pat_match_nothing,
119 [PAT_MATCH_INT] = pat_match_int,
120 [PAT_MATCH_IP] = pat_match_ip,
121 [PAT_MATCH_BIN] = pat_match_bin,
122 [PAT_MATCH_LEN] = pat_match_len,
123 [PAT_MATCH_STR] = pat_match_str,
124 [PAT_MATCH_BEG] = pat_match_beg,
125 [PAT_MATCH_SUB] = pat_match_sub,
126 [PAT_MATCH_DIR] = pat_match_dir,
127 [PAT_MATCH_DOM] = pat_match_dom,
128 [PAT_MATCH_END] = pat_match_end,
129 [PAT_MATCH_REG] = pat_match_reg,
Thierry Fournier8feaa662016-02-10 22:55:20 +0100130 [PAT_MATCH_REGM] = pat_match_regm,
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100131};
132
Thierry FOURNIERe3ded592013-12-06 15:36:54 +0100133/* Just used for checking configuration compatibility */
134int pat_match_types[PAT_MATCH_NUM] = {
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200135 [PAT_MATCH_FOUND] = SMP_T_SINT,
136 [PAT_MATCH_BOOL] = SMP_T_SINT,
137 [PAT_MATCH_INT] = SMP_T_SINT,
Thierry FOURNIERe3ded592013-12-06 15:36:54 +0100138 [PAT_MATCH_IP] = SMP_T_ADDR,
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100139 [PAT_MATCH_BIN] = SMP_T_BIN,
140 [PAT_MATCH_LEN] = SMP_T_STR,
141 [PAT_MATCH_STR] = SMP_T_STR,
142 [PAT_MATCH_BEG] = SMP_T_STR,
143 [PAT_MATCH_SUB] = SMP_T_STR,
144 [PAT_MATCH_DIR] = SMP_T_STR,
145 [PAT_MATCH_DOM] = SMP_T_STR,
146 [PAT_MATCH_END] = SMP_T_STR,
147 [PAT_MATCH_REG] = SMP_T_STR,
Thierry Fournier8feaa662016-02-10 22:55:20 +0100148 [PAT_MATCH_REGM] = SMP_T_STR,
Thierry FOURNIERe3ded592013-12-06 15:36:54 +0100149};
150
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +0100151/* this struct is used to return information */
Emeric Brunb5997f72017-07-03 11:34:05 +0200152static THREAD_LOCAL struct pattern static_pattern;
153static THREAD_LOCAL struct sample_data static_sample_data;
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +0100154
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100155/* This is the root of the list of all pattern_ref avalaibles. */
156struct list pattern_reference = LIST_HEAD_INIT(pattern_reference);
157
Willy Tarreau403bfbb2019-10-23 06:59:31 +0200158static THREAD_LOCAL struct lru64_head *pat_lru_tree;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200159static unsigned long long pat_lru_seed;
160
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100161/*
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100162 *
163 * The following functions are not exported and are used by internals process
164 * of pattern matching
165 *
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100166 */
167
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100168/* Background: Fast way to find a zero byte in a word
169 * http://graphics.stanford.edu/~seander/bithacks.html#ZeroInWord
170 * hasZeroByte = (v - 0x01010101UL) & ~v & 0x80808080UL;
171 *
172 * To look for 4 different byte values, xor the word with those bytes and
173 * then check for zero bytes:
174 *
175 * v = (((unsigned char)c * 0x1010101U) ^ delimiter)
176 * where <delimiter> is the 4 byte values to look for (as an uint)
177 * and <c> is the character that is being tested
178 */
179static inline unsigned int is_delimiter(unsigned char c, unsigned int mask)
180{
181 mask ^= (c * 0x01010101); /* propagate the char to all 4 bytes */
182 return (mask - 0x01010101) & ~mask & 0x80808080U;
183}
184
185static inline unsigned int make_4delim(unsigned char d1, unsigned char d2, unsigned char d3, unsigned char d4)
186{
187 return d1 << 24 | d2 << 16 | d3 << 8 | d4;
188}
189
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100190
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100191/*
192 *
193 * These functions are exported and may be used by any other component.
194 *
Willy Tarreau5def8ef2014-08-29 15:19:33 +0200195 * The following functions are used for parsing pattern matching input value.
196 * The <text> contain the string to be parsed. <pattern> must be a preallocated
197 * pattern. The pat_parse_* functions fill this structure with the parsed value.
198 * <err> is filled with an error message built with memprintf() function. It is
199 * allowed to use a trash as a temporary storage for the returned pattern, as
200 * the next call after these functions will be pat_idx_*.
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100201 *
Willy Tarreau5def8ef2014-08-29 15:19:33 +0200202 * In success case, the pat_parse_* function returns 1. If the function
203 * fails, it returns 0 and <err> is filled.
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100204 */
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100205
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100206/* ignore the current line */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200207int pat_parse_nothing(const char *text, struct pattern *pattern, int mflags, char **err)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100208{
209 return 1;
210}
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100211
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100212/* Parse a string. It is allocated and duplicated. */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200213int pat_parse_str(const char *text, struct pattern *pattern, int mflags, char **err)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100214{
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100215 pattern->type = SMP_T_STR;
Thierry FOURNIERedc15c32013-12-13 15:36:59 +0100216 pattern->ptr.str = (char *)text;
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100217 pattern->len = strlen(text);
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100218 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100219}
220
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100221/* Parse a binary written in hexa. It is allocated. */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200222int pat_parse_bin(const char *text, struct pattern *pattern, int mflags, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100223{
Willy Tarreau83061a82018-07-13 11:56:34 +0200224 struct buffer *trash;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100225
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100226 pattern->type = SMP_T_BIN;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100227 trash = get_trash_chunk();
228 pattern->len = trash->size;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200229 pattern->ptr.str = trash->area;
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100230 return !!parse_binary(text, &pattern->ptr.str, &pattern->len, err);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100231}
232
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100233/* Parse a regex. It is allocated. */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200234int pat_parse_reg(const char *text, struct pattern *pattern, int mflags, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100235{
Thierry FOURNIER0b6d15f2014-01-29 19:35:16 +0100236 pattern->ptr.str = (char *)text;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100237 return 1;
238}
239
240/* Parse a range of positive integers delimited by either ':' or '-'. If only
241 * one integer is read, it is set as both min and max. An operator may be
242 * specified as the prefix, among this list of 5 :
243 *
244 * 0:eq, 1:gt, 2:ge, 3:lt, 4:le
245 *
246 * The default operator is "eq". It supports range matching. Ranges are
247 * rejected for other operators. The operator may be changed at any time.
248 * The operator is stored in the 'opaque' argument.
249 *
250 * If err is non-NULL, an error message will be returned there on errors and
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100251 * the caller will have to free it. The function returns zero on error, and
252 * non-zero on success.
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100253 *
254 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200255int pat_parse_int(const char *text, struct pattern *pattern, int mflags, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100256{
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100257 const char *ptr = text;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100258
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200259 pattern->type = SMP_T_SINT;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100260
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100261 /* Empty string is not valid */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100262 if (!*text)
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100263 goto not_valid_range;
264
265 /* Search ':' or '-' separator. */
266 while (*ptr != '\0' && *ptr != ':' && *ptr != '-')
267 ptr++;
268
269 /* If separator not found. */
270 if (!*ptr) {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100271 if (strl2llrc(text, ptr - text, &pattern->val.range.min) != 0) {
272 memprintf(err, "'%s' is not a number", text);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100273 return 0;
274 }
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100275 pattern->val.range.max = pattern->val.range.min;
276 pattern->val.range.min_set = 1;
277 pattern->val.range.max_set = 1;
278 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100279 }
280
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100281 /* If the separator is the first character. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100282 if (ptr == text && *(ptr + 1) != '\0') {
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100283 if (strl2llrc(ptr + 1, strlen(ptr + 1), &pattern->val.range.max) != 0)
284 goto not_valid_range;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100285
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100286 pattern->val.range.min_set = 0;
287 pattern->val.range.max_set = 1;
288 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100289 }
290
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100291 /* If separator is the last character. */
292 if (*(ptr + 1) == '\0') {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100293 if (strl2llrc(text, ptr - text, &pattern->val.range.min) != 0)
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100294 goto not_valid_range;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100295
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100296 pattern->val.range.min_set = 1;
297 pattern->val.range.max_set = 0;
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100298 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100299 }
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100300
301 /* Else, parse two numbers. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100302 if (strl2llrc(text, ptr - text, &pattern->val.range.min) != 0)
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100303 goto not_valid_range;
304
305 if (strl2llrc(ptr + 1, strlen(ptr + 1), &pattern->val.range.max) != 0)
306 goto not_valid_range;
307
308 if (pattern->val.range.min > pattern->val.range.max)
309 goto not_valid_range;
310
311 pattern->val.range.min_set = 1;
312 pattern->val.range.max_set = 1;
313 return 1;
314
315 not_valid_range:
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100316 memprintf(err, "'%s' is not a valid number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100317 return 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100318}
319
320/* Parse a range of positive 2-component versions delimited by either ':' or
321 * '-'. The version consists in a major and a minor, both of which must be
322 * smaller than 65536, because internally they will be represented as a 32-bit
323 * integer.
324 * If only one version is read, it is set as both min and max. Just like for
325 * pure integers, an operator may be specified as the prefix, among this list
326 * of 5 :
327 *
328 * 0:eq, 1:gt, 2:ge, 3:lt, 4:le
329 *
330 * The default operator is "eq". It supports range matching. Ranges are
331 * rejected for other operators. The operator may be changed at any time.
332 * The operator is stored in the 'opaque' argument. This allows constructs
333 * such as the following one :
334 *
335 * acl obsolete_ssl ssl_req_proto lt 3
336 * acl unsupported_ssl ssl_req_proto gt 3.1
337 * acl valid_ssl ssl_req_proto 3.0-3.1
338 *
339 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200340int pat_parse_dotted_ver(const char *text, struct pattern *pattern, int mflags, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100341{
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100342 const char *ptr = text;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100343
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200344 pattern->type = SMP_T_SINT;
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100345
346 /* Search ':' or '-' separator. */
347 while (*ptr != '\0' && *ptr != ':' && *ptr != '-')
348 ptr++;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100349
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100350 /* If separator not found. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100351 if (*ptr == '\0' && ptr > text) {
352 if (strl2llrc_dotted(text, ptr-text, &pattern->val.range.min) != 0) {
353 memprintf(err, "'%s' is not a dotted number", text);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100354 return 0;
355 }
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100356 pattern->val.range.max = pattern->val.range.min;
357 pattern->val.range.min_set = 1;
358 pattern->val.range.max_set = 1;
359 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100360 }
361
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100362 /* If the separator is the first character. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100363 if (ptr == text && *(ptr+1) != '\0') {
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100364 if (strl2llrc_dotted(ptr+1, strlen(ptr+1), &pattern->val.range.max) != 0) {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100365 memprintf(err, "'%s' is not a valid dotted number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100366 return 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100367 }
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100368 pattern->val.range.min_set = 0;
369 pattern->val.range.max_set = 1;
370 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100371 }
372
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100373 /* If separator is the last character. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100374 if (ptr == &text[strlen(text)-1]) {
375 if (strl2llrc_dotted(text, ptr-text, &pattern->val.range.min) != 0) {
376 memprintf(err, "'%s' is not a valid dotted number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100377 return 0;
378 }
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100379 pattern->val.range.min_set = 1;
380 pattern->val.range.max_set = 0;
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100381 return 1;
382 }
383
384 /* Else, parse two numbers. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100385 if (strl2llrc_dotted(text, ptr-text, &pattern->val.range.min) != 0) {
386 memprintf(err, "'%s' is not a valid dotted number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100387 return 0;
388 }
389 if (strl2llrc_dotted(ptr+1, strlen(ptr+1), &pattern->val.range.max) != 0) {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100390 memprintf(err, "'%s' is not a valid dotted number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100391 return 0;
392 }
393 if (pattern->val.range.min > pattern->val.range.max) {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100394 memprintf(err, "'%s' is not a valid dotted number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100395 return 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100396 }
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100397 pattern->val.range.min_set = 1;
398 pattern->val.range.max_set = 1;
399 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100400}
401
402/* Parse an IP address and an optional mask in the form addr[/mask].
403 * The addr may either be an IPv4 address or a hostname. The mask
404 * may either be a dotted mask or a number of bits. Returns 1 if OK,
405 * otherwise 0. NOTE: IP address patterns are typed (IPV4/IPV6).
406 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200407int pat_parse_ip(const char *text, struct pattern *pattern, int mflags, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100408{
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200409 if (str2net(text, !(mflags & PAT_MF_NO_DNS) && (global.mode & MODE_STARTING),
Thierry FOURNIERfc7ac7b2014-02-11 15:23:04 +0100410 &pattern->val.ipv4.addr, &pattern->val.ipv4.mask)) {
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100411 pattern->type = SMP_T_IPV4;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100412 return 1;
413 }
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100414 else if (str62net(text, &pattern->val.ipv6.addr, &pattern->val.ipv6.mask)) {
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100415 pattern->type = SMP_T_IPV6;
416 return 1;
417 }
418 else {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100419 memprintf(err, "'%s' is not a valid IPv4 or IPv6 address", text);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100420 return 0;
421 }
422}
423
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100424/*
425 *
426 * These functions are exported and may be used by any other component.
427 *
Joseph Herlant4189d672018-11-15 10:22:31 -0800428 * This function just takes a sample <smp> and checks if this sample matches
429 * with the pattern <pattern>. This function returns only PAT_MATCH or
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100430 * PAT_NOMATCH.
431 *
432 */
433
434/* always return false */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100435struct pattern *pat_match_nothing(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100436{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200437 if (smp->data.u.sint) {
Thierry FOURNIERe5978bf2014-03-17 19:53:10 +0100438 if (fill) {
Thierry FOURNIER503bb092015-08-19 08:35:43 +0200439 static_pattern.data = NULL;
Thierry FOURNIERe5978bf2014-03-17 19:53:10 +0100440 static_pattern.ref = NULL;
Thierry FOURNIERe5978bf2014-03-17 19:53:10 +0100441 static_pattern.type = 0;
442 static_pattern.ptr.str = NULL;
443 }
444 return &static_pattern;
445 }
446 else
447 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100448}
449
450
Joseph Herlant4189d672018-11-15 10:22:31 -0800451/* NB: For two strings to be identical, it is required that their length match */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100452struct pattern *pat_match_str(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100453{
454 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100455 struct ebmb_node *node;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100456 struct pattern_tree *elt;
457 struct pattern_list *lst;
458 struct pattern *pattern;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200459 struct pattern *ret = NULL;
460 struct lru64 *lru = NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100461
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100462 /* Lookup a string in the expression's pattern tree. */
463 if (!eb_is_empty(&expr->pattern_tree)) {
Christopher Fauletb4cf7ab2020-06-30 18:52:32 +0200464 char prev = 0;
465
466 if (smp->data.u.str.data < smp->data.u.str.size) {
467 /* we may have to force a trailing zero on the test pattern and
468 * the buffer is large enough to accommodate it.
469 */
470 prev = smp->data.u.str.area[smp->data.u.str.data];
471 if (prev)
472 smp->data.u.str.area[smp->data.u.str.data] = '\0';
473 }
474 else {
475 /* Otherwise, the sample is duplicated. A trailing zero
476 * is automatically added to the string.
477 */
478 if (!smp_dup(smp))
479 return NULL;
480 }
481
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200482 node = ebst_lookup(&expr->pattern_tree, smp->data.u.str.area);
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100483 if (prev)
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200484 smp->data.u.str.area[smp->data.u.str.data] = prev;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100485 if (node) {
486 if (fill) {
487 elt = ebmb_entry(node, struct pattern_tree, node);
Thierry FOURNIER503bb092015-08-19 08:35:43 +0200488 static_pattern.data = elt->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +0100489 static_pattern.ref = elt->ref;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200490 static_pattern.sflags = PAT_SF_TREE;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100491 static_pattern.type = SMP_T_STR;
492 static_pattern.ptr.str = (char *)elt->node.key;
493 }
494 return &static_pattern;
495 }
496 }
497
498 /* look in the list */
Willy Tarreauf3045d22015-04-29 16:24:50 +0200499 if (pat_lru_tree) {
Willy Tarreauaee93142015-05-04 17:18:42 +0200500 unsigned long long seed = pat_lru_seed ^ (long)expr;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200501
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200502 lru = lru64_get(XXH64(smp->data.u.str.area, smp->data.u.str.data, seed),
Willy Tarreau3ee0de12020-11-02 15:26:51 +0100503 pat_lru_tree, expr, expr->ref->revision);
Willy Tarreau403bfbb2019-10-23 06:59:31 +0200504 if (lru && lru->domain) {
Emeric Brunb5997f72017-07-03 11:34:05 +0200505 ret = lru->data;
Emeric Brunb5997f72017-07-03 11:34:05 +0200506 return ret;
507 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200508 }
509
Emeric Brunb5997f72017-07-03 11:34:05 +0200510
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100511 list_for_each_entry(lst, &expr->patterns, list) {
512 pattern = &lst->pat;
513
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200514 if (pattern->len != smp->data.u.str.data)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100515 continue;
516
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200517 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200518 if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.str.area, smp->data.u.str.data) == 0) ||
519 (!icase && strncmp(pattern->ptr.str, smp->data.u.str.area, smp->data.u.str.data) == 0)) {
Willy Tarreauf3045d22015-04-29 16:24:50 +0200520 ret = pattern;
521 break;
522 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100523 }
524
Willy Tarreau403bfbb2019-10-23 06:59:31 +0200525 if (lru)
Willy Tarreau3ee0de12020-11-02 15:26:51 +0100526 lru64_commit(lru, ret, expr, expr->ref->revision, NULL);
Willy Tarreauf3045d22015-04-29 16:24:50 +0200527
528 return ret;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100529}
530
531/* NB: For two binaries buf to be identical, it is required that their lengths match */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100532struct pattern *pat_match_bin(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100533{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100534 struct pattern_list *lst;
535 struct pattern *pattern;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200536 struct pattern *ret = NULL;
537 struct lru64 *lru = NULL;
538
539 if (pat_lru_tree) {
Willy Tarreauaee93142015-05-04 17:18:42 +0200540 unsigned long long seed = pat_lru_seed ^ (long)expr;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100541
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200542 lru = lru64_get(XXH64(smp->data.u.str.area, smp->data.u.str.data, seed),
Willy Tarreau3ee0de12020-11-02 15:26:51 +0100543 pat_lru_tree, expr, expr->ref->revision);
Willy Tarreau403bfbb2019-10-23 06:59:31 +0200544 if (lru && lru->domain) {
Emeric Brunb5997f72017-07-03 11:34:05 +0200545 ret = lru->data;
Emeric Brunb5997f72017-07-03 11:34:05 +0200546 return ret;
547 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200548 }
549
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100550 list_for_each_entry(lst, &expr->patterns, list) {
551 pattern = &lst->pat;
552
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200553 if (pattern->len != smp->data.u.str.data)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100554 continue;
555
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200556 if (memcmp(pattern->ptr.str, smp->data.u.str.area, smp->data.u.str.data) == 0) {
Willy Tarreauf3045d22015-04-29 16:24:50 +0200557 ret = pattern;
558 break;
559 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100560 }
561
Willy Tarreau403bfbb2019-10-23 06:59:31 +0200562 if (lru)
Willy Tarreau3ee0de12020-11-02 15:26:51 +0100563 lru64_commit(lru, ret, expr, expr->ref->revision, NULL);
Willy Tarreauf3045d22015-04-29 16:24:50 +0200564
565 return ret;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100566}
567
568/* Executes a regex. It temporarily changes the data to add a trailing zero,
Thierry Fournier8feaa662016-02-10 22:55:20 +0100569 * and restores the previous character when leaving. This function fills
570 * a matching array.
571 */
572struct pattern *pat_match_regm(struct sample *smp, struct pattern_expr *expr, int fill)
573{
574 struct pattern_list *lst;
575 struct pattern *pattern;
576 struct pattern *ret = NULL;
577
578 list_for_each_entry(lst, &expr->patterns, list) {
579 pattern = &lst->pat;
580
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200581 if (regex_exec_match2(pattern->ptr.reg, smp->data.u.str.area, smp->data.u.str.data,
Thierry Fournier8feaa662016-02-10 22:55:20 +0100582 MAX_MATCH, pmatch, 0)) {
583 ret = pattern;
584 smp->ctx.a[0] = pmatch;
585 break;
586 }
587 }
588
589 return ret;
590}
591
592/* Executes a regex. It temporarily changes the data to add a trailing zero,
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100593 * and restores the previous character when leaving.
594 */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100595struct pattern *pat_match_reg(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100596{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100597 struct pattern_list *lst;
598 struct pattern *pattern;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200599 struct pattern *ret = NULL;
600 struct lru64 *lru = NULL;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100601
Willy Tarreauf3045d22015-04-29 16:24:50 +0200602 if (pat_lru_tree) {
Willy Tarreauaee93142015-05-04 17:18:42 +0200603 unsigned long long seed = pat_lru_seed ^ (long)expr;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200604
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200605 lru = lru64_get(XXH64(smp->data.u.str.area, smp->data.u.str.data, seed),
Willy Tarreau3ee0de12020-11-02 15:26:51 +0100606 pat_lru_tree, expr, expr->ref->revision);
Willy Tarreau403bfbb2019-10-23 06:59:31 +0200607 if (lru && lru->domain) {
Emeric Brunb5997f72017-07-03 11:34:05 +0200608 ret = lru->data;
Emeric Brunb5997f72017-07-03 11:34:05 +0200609 return ret;
610 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200611 }
612
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100613 list_for_each_entry(lst, &expr->patterns, list) {
614 pattern = &lst->pat;
615
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200616 if (regex_exec2(pattern->ptr.reg, smp->data.u.str.area, smp->data.u.str.data)) {
Willy Tarreauf3045d22015-04-29 16:24:50 +0200617 ret = pattern;
618 break;
619 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100620 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200621
Willy Tarreau403bfbb2019-10-23 06:59:31 +0200622 if (lru)
Willy Tarreau3ee0de12020-11-02 15:26:51 +0100623 lru64_commit(lru, ret, expr, expr->ref->revision, NULL);
Willy Tarreauf3045d22015-04-29 16:24:50 +0200624
625 return ret;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100626}
627
628/* Checks that the pattern matches the beginning of the tested string. */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100629struct pattern *pat_match_beg(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100630{
631 int icase;
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +0200632 struct ebmb_node *node;
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +0200633 struct pattern_tree *elt;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100634 struct pattern_list *lst;
635 struct pattern *pattern;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200636 struct pattern *ret = NULL;
637 struct lru64 *lru = NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100638
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +0200639 /* Lookup a string in the expression's pattern tree. */
640 if (!eb_is_empty(&expr->pattern_tree)) {
Christopher Fauletb4cf7ab2020-06-30 18:52:32 +0200641 char prev = 0;
642
643 if (smp->data.u.str.data < smp->data.u.str.size) {
644 /* we may have to force a trailing zero on the test pattern and
645 * the buffer is large enough to accommodate it.
646 */
647 prev = smp->data.u.str.area[smp->data.u.str.data];
648 if (prev)
649 smp->data.u.str.area[smp->data.u.str.data] = '\0';
650 }
651 else {
652 /* Otherwise, the sample is duplicated. A trailing zero
653 * is automatically added to the string.
654 */
655 if (!smp_dup(smp))
656 return NULL;
657 }
658
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200659 node = ebmb_lookup_longest(&expr->pattern_tree,
660 smp->data.u.str.area);
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +0200661 if (prev)
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200662 smp->data.u.str.area[smp->data.u.str.data] = prev;
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +0200663
664 if (node) {
665 if (fill) {
666 elt = ebmb_entry(node, struct pattern_tree, node);
Thierry FOURNIER503bb092015-08-19 08:35:43 +0200667 static_pattern.data = elt->data;
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +0200668 static_pattern.ref = elt->ref;
669 static_pattern.sflags = PAT_SF_TREE;
670 static_pattern.type = SMP_T_STR;
671 static_pattern.ptr.str = (char *)elt->node.key;
672 }
673 return &static_pattern;
674 }
675 }
676
677 /* look in the list */
Willy Tarreauf3045d22015-04-29 16:24:50 +0200678 if (pat_lru_tree) {
Willy Tarreauaee93142015-05-04 17:18:42 +0200679 unsigned long long seed = pat_lru_seed ^ (long)expr;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200680
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200681 lru = lru64_get(XXH64(smp->data.u.str.area, smp->data.u.str.data, seed),
Willy Tarreau3ee0de12020-11-02 15:26:51 +0100682 pat_lru_tree, expr, expr->ref->revision);
Willy Tarreau403bfbb2019-10-23 06:59:31 +0200683 if (lru && lru->domain) {
Emeric Brunb5997f72017-07-03 11:34:05 +0200684 ret = lru->data;
Emeric Brunb5997f72017-07-03 11:34:05 +0200685 return ret;
686 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200687 }
688
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100689 list_for_each_entry(lst, &expr->patterns, list) {
690 pattern = &lst->pat;
691
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200692 if (pattern->len > smp->data.u.str.data)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100693 continue;
694
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200695 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200696 if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.str.area, pattern->len) != 0) ||
697 (!icase && strncmp(pattern->ptr.str, smp->data.u.str.area, pattern->len) != 0))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100698 continue;
699
Willy Tarreauf3045d22015-04-29 16:24:50 +0200700 ret = pattern;
701 break;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100702 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200703
Willy Tarreau403bfbb2019-10-23 06:59:31 +0200704 if (lru)
Willy Tarreau3ee0de12020-11-02 15:26:51 +0100705 lru64_commit(lru, ret, expr, expr->ref->revision, NULL);
Willy Tarreauf3045d22015-04-29 16:24:50 +0200706
707 return ret;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100708}
709
710/* Checks that the pattern matches the end of the tested string. */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100711struct pattern *pat_match_end(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100712{
713 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100714 struct pattern_list *lst;
715 struct pattern *pattern;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200716 struct pattern *ret = NULL;
717 struct lru64 *lru = NULL;
718
719 if (pat_lru_tree) {
Willy Tarreauaee93142015-05-04 17:18:42 +0200720 unsigned long long seed = pat_lru_seed ^ (long)expr;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200721
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200722 lru = lru64_get(XXH64(smp->data.u.str.area, smp->data.u.str.data, seed),
Willy Tarreau3ee0de12020-11-02 15:26:51 +0100723 pat_lru_tree, expr, expr->ref->revision);
Willy Tarreau403bfbb2019-10-23 06:59:31 +0200724 if (lru && lru->domain) {
Emeric Brunb5997f72017-07-03 11:34:05 +0200725 ret = lru->data;
Emeric Brunb5997f72017-07-03 11:34:05 +0200726 return ret;
727 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200728 }
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100729
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100730 list_for_each_entry(lst, &expr->patterns, list) {
731 pattern = &lst->pat;
732
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200733 if (pattern->len > smp->data.u.str.data)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100734 continue;
735
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200736 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200737 if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.str.area + smp->data.u.str.data - pattern->len, pattern->len) != 0) ||
738 (!icase && strncmp(pattern->ptr.str, smp->data.u.str.area + smp->data.u.str.data - pattern->len, pattern->len) != 0))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100739 continue;
740
Willy Tarreauf3045d22015-04-29 16:24:50 +0200741 ret = pattern;
742 break;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100743 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200744
Willy Tarreau403bfbb2019-10-23 06:59:31 +0200745 if (lru)
Willy Tarreau3ee0de12020-11-02 15:26:51 +0100746 lru64_commit(lru, ret, expr, expr->ref->revision, NULL);
Willy Tarreauf3045d22015-04-29 16:24:50 +0200747
748 return ret;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100749}
750
751/* Checks that the pattern is included inside the tested string.
752 * NB: Suboptimal, should be rewritten using a Boyer-Moore method.
753 */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100754struct pattern *pat_match_sub(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100755{
756 int icase;
757 char *end;
758 char *c;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100759 struct pattern_list *lst;
760 struct pattern *pattern;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200761 struct pattern *ret = NULL;
762 struct lru64 *lru = NULL;
763
764 if (pat_lru_tree) {
Willy Tarreauaee93142015-05-04 17:18:42 +0200765 unsigned long long seed = pat_lru_seed ^ (long)expr;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200766
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200767 lru = lru64_get(XXH64(smp->data.u.str.area, smp->data.u.str.data, seed),
Willy Tarreau3ee0de12020-11-02 15:26:51 +0100768 pat_lru_tree, expr, expr->ref->revision);
Willy Tarreau403bfbb2019-10-23 06:59:31 +0200769 if (lru && lru->domain) {
Emeric Brunb5997f72017-07-03 11:34:05 +0200770 ret = lru->data;
Emeric Brunb5997f72017-07-03 11:34:05 +0200771 return ret;
772 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200773 }
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100774
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100775 list_for_each_entry(lst, &expr->patterns, list) {
776 pattern = &lst->pat;
777
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200778 if (pattern->len > smp->data.u.str.data)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100779 continue;
780
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200781 end = smp->data.u.str.area + smp->data.u.str.data - pattern->len;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200782 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100783 if (icase) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200784 for (c = smp->data.u.str.area; c <= end; c++) {
Willy Tarreauf278eec2020-07-05 21:46:32 +0200785 if (tolower((unsigned char)*c) != tolower((unsigned char)*pattern->ptr.str))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100786 continue;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200787 if (strncasecmp(pattern->ptr.str, c, pattern->len) == 0) {
788 ret = pattern;
789 goto leave;
790 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100791 }
792 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200793 for (c = smp->data.u.str.area; c <= end; c++) {
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100794 if (*c != *pattern->ptr.str)
795 continue;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200796 if (strncmp(pattern->ptr.str, c, pattern->len) == 0) {
797 ret = pattern;
798 goto leave;
799 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100800 }
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100801 }
802 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200803 leave:
Willy Tarreau403bfbb2019-10-23 06:59:31 +0200804 if (lru)
Willy Tarreau3ee0de12020-11-02 15:26:51 +0100805 lru64_commit(lru, ret, expr, expr->ref->revision, NULL);
Willy Tarreauf3045d22015-04-29 16:24:50 +0200806
807 return ret;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100808}
809
810/* This one is used by other real functions. It checks that the pattern is
811 * included inside the tested string, but enclosed between the specified
812 * delimiters or at the beginning or end of the string. The delimiters are
813 * provided as an unsigned int made by make_4delim() and match up to 4 different
814 * delimiters. Delimiters are stripped at the beginning and end of the pattern.
815 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200816static int match_word(struct sample *smp, struct pattern *pattern, int mflags, unsigned int delimiters)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100817{
818 int may_match, icase;
819 char *c, *end;
820 char *ps;
821 int pl;
822
823 pl = pattern->len;
824 ps = pattern->ptr.str;
825
826 while (pl > 0 && is_delimiter(*ps, delimiters)) {
827 pl--;
828 ps++;
829 }
830
831 while (pl > 0 && is_delimiter(ps[pl - 1], delimiters))
832 pl--;
833
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200834 if (pl > smp->data.u.str.data)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100835 return PAT_NOMATCH;
836
837 may_match = 1;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200838 icase = mflags & PAT_MF_IGNORE_CASE;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200839 end = smp->data.u.str.area + smp->data.u.str.data - pl;
840 for (c = smp->data.u.str.area; c <= end; c++) {
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100841 if (is_delimiter(*c, delimiters)) {
842 may_match = 1;
843 continue;
844 }
845
846 if (!may_match)
847 continue;
848
849 if (icase) {
Willy Tarreauf278eec2020-07-05 21:46:32 +0200850 if ((tolower((unsigned char)*c) == tolower((unsigned char)*ps)) &&
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100851 (strncasecmp(ps, c, pl) == 0) &&
852 (c == end || is_delimiter(c[pl], delimiters)))
853 return PAT_MATCH;
854 } else {
855 if ((*c == *ps) &&
856 (strncmp(ps, c, pl) == 0) &&
857 (c == end || is_delimiter(c[pl], delimiters)))
858 return PAT_MATCH;
859 }
860 may_match = 0;
861 }
862 return PAT_NOMATCH;
863}
864
865/* Checks that the pattern is included inside the tested string, but enclosed
866 * between the delimiters '?' or '/' or at the beginning or end of the string.
867 * Delimiters at the beginning or end of the pattern are ignored.
868 */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100869struct pattern *pat_match_dir(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100870{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100871 struct pattern_list *lst;
872 struct pattern *pattern;
873
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100874 list_for_each_entry(lst, &expr->patterns, list) {
875 pattern = &lst->pat;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200876 if (match_word(smp, pattern, expr->mflags, make_4delim('/', '?', '?', '?')))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100877 return pattern;
878 }
879 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100880}
881
882/* Checks that the pattern is included inside the tested string, but enclosed
883 * between the delmiters '/', '?', '.' or ":" or at the beginning or end of
884 * the string. Delimiters at the beginning or end of the pattern are ignored.
885 */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100886struct pattern *pat_match_dom(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100887{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100888 struct pattern_list *lst;
889 struct pattern *pattern;
890
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100891 list_for_each_entry(lst, &expr->patterns, list) {
892 pattern = &lst->pat;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200893 if (match_word(smp, pattern, expr->mflags, make_4delim('/', '?', '.', ':')))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100894 return pattern;
895 }
896 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100897}
898
899/* Checks that the integer in <test> is included between min and max */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100900struct pattern *pat_match_int(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100901{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100902 struct pattern_list *lst;
903 struct pattern *pattern;
904
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100905 list_for_each_entry(lst, &expr->patterns, list) {
906 pattern = &lst->pat;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200907 if ((!pattern->val.range.min_set || pattern->val.range.min <= smp->data.u.sint) &&
908 (!pattern->val.range.max_set || smp->data.u.sint <= pattern->val.range.max))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100909 return pattern;
910 }
911 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100912}
913
914/* Checks that the length of the pattern in <test> is included between min and max */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100915struct pattern *pat_match_len(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100916{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100917 struct pattern_list *lst;
918 struct pattern *pattern;
919
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100920 list_for_each_entry(lst, &expr->patterns, list) {
921 pattern = &lst->pat;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200922 if ((!pattern->val.range.min_set || pattern->val.range.min <= smp->data.u.str.data) &&
923 (!pattern->val.range.max_set || smp->data.u.str.data <= pattern->val.range.max))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100924 return pattern;
925 }
926 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100927}
928
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100929struct pattern *pat_match_ip(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100930{
931 unsigned int v4; /* in network byte order */
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100932 struct in6_addr tmp6;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100933 struct in_addr *s;
934 struct ebmb_node *node;
935 struct pattern_tree *elt;
936 struct pattern_list *lst;
937 struct pattern *pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100938
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100939 /* The input sample is IPv4. Try to match in the trees. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200940 if (smp->data.type == SMP_T_IPV4) {
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100941 /* Lookup an IPv4 address in the expression's pattern tree using
942 * the longest match method.
943 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200944 s = &smp->data.u.ipv4;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100945 node = ebmb_lookup_longest(&expr->pattern_tree, &s->s_addr);
946 if (node) {
947 if (fill) {
948 elt = ebmb_entry(node, struct pattern_tree, node);
Thierry FOURNIER503bb092015-08-19 08:35:43 +0200949 static_pattern.data = elt->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +0100950 static_pattern.ref = elt->ref;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200951 static_pattern.sflags = PAT_SF_TREE;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100952 static_pattern.type = SMP_T_IPV4;
Willy Tarreau296cfd12020-02-25 09:58:41 +0100953 static_pattern.val.ipv4.addr.s_addr = read_u32(elt->node.key);
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100954 if (!cidr2dotted(elt->node.node.pfx, &static_pattern.val.ipv4.mask))
955 return NULL;
956 }
957 return &static_pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100958 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100959
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100960 /* The IPv4 sample dont match the IPv4 tree. Convert the IPv4
961 * sample address to IPv6 with the mapping method using the ::ffff:
962 * prefix, and try to lookup in the IPv6 tree.
963 */
964 memset(&tmp6, 0, 10);
Willy Tarreau296cfd12020-02-25 09:58:41 +0100965 write_u16(&tmp6.s6_addr[10], htons(0xffff));
966 write_u32(&tmp6.s6_addr[12], smp->data.u.ipv4.s_addr);
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100967 node = ebmb_lookup_longest(&expr->pattern_tree_2, &tmp6);
968 if (node) {
969 if (fill) {
970 elt = ebmb_entry(node, struct pattern_tree, node);
Thierry FOURNIER503bb092015-08-19 08:35:43 +0200971 static_pattern.data = elt->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +0100972 static_pattern.ref = elt->ref;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200973 static_pattern.sflags = PAT_SF_TREE;
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100974 static_pattern.type = SMP_T_IPV6;
Willy Tarreau296cfd12020-02-25 09:58:41 +0100975 memcpy(&static_pattern.val.ipv6.addr, elt->node.key, 16);
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100976 static_pattern.val.ipv6.mask = elt->node.node.pfx;
977 }
978 return &static_pattern;
979 }
980 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100981
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100982 /* The input sample is IPv6. Try to match in the trees. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200983 if (smp->data.type == SMP_T_IPV6) {
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100984 /* Lookup an IPv6 address in the expression's pattern tree using
985 * the longest match method.
986 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200987 node = ebmb_lookup_longest(&expr->pattern_tree_2, &smp->data.u.ipv6);
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100988 if (node) {
989 if (fill) {
990 elt = ebmb_entry(node, struct pattern_tree, node);
Thierry FOURNIER503bb092015-08-19 08:35:43 +0200991 static_pattern.data = elt->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +0100992 static_pattern.ref = elt->ref;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200993 static_pattern.sflags = PAT_SF_TREE;
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100994 static_pattern.type = SMP_T_IPV6;
Willy Tarreau296cfd12020-02-25 09:58:41 +0100995 memcpy(&static_pattern.val.ipv6.addr, elt->node.key, 16);
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100996 static_pattern.val.ipv6.mask = elt->node.node.pfx;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100997 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100998 return &static_pattern;
999 }
1000
1001 /* Try to convert 6 to 4 when the start of the ipv6 address match the
1002 * following forms :
1003 * - ::ffff:ip:v4 (ipv4 mapped)
1004 * - ::0000:ip:v4 (old ipv4 mapped)
1005 * - 2002:ip:v4:: (6to4)
1006 */
Willy Tarreau296cfd12020-02-25 09:58:41 +01001007 if ((read_u64(&smp->data.u.ipv6.s6_addr[0]) == 0 &&
1008 (read_u32(&smp->data.u.ipv6.s6_addr[8]) == 0 ||
1009 read_u32(&smp->data.u.ipv6.s6_addr[8]) == htonl(0xFFFF))) ||
1010 read_u16(&smp->data.u.ipv6.s6_addr[0]) == htons(0x2002)) {
1011 if (read_u32(&smp->data.u.ipv6.s6_addr[0]) == 0)
1012 v4 = read_u32(&smp->data.u.ipv6.s6_addr[12]);
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001013 else
Willy Tarreau296cfd12020-02-25 09:58:41 +01001014 v4 = htonl((ntohs(read_u16(&smp->data.u.ipv6.s6_addr[2])) << 16) +
1015 ntohs(read_u16(&smp->data.u.ipv6.s6_addr[4])));
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001016
1017 /* Lookup an IPv4 address in the expression's pattern tree using the longest
1018 * match method.
1019 */
1020 node = ebmb_lookup_longest(&expr->pattern_tree, &v4);
1021 if (node) {
1022 if (fill) {
1023 elt = ebmb_entry(node, struct pattern_tree, node);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001024 static_pattern.data = elt->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01001025 static_pattern.ref = elt->ref;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02001026 static_pattern.sflags = PAT_SF_TREE;
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001027 static_pattern.type = SMP_T_IPV4;
Willy Tarreau296cfd12020-02-25 09:58:41 +01001028 static_pattern.val.ipv4.addr.s_addr = read_u32(elt->node.key);
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001029 if (!cidr2dotted(elt->node.node.pfx, &static_pattern.val.ipv4.mask))
1030 return NULL;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01001031 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001032 return &static_pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +01001033 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001034 }
1035 }
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +01001036
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001037 /* Lookup in the list. the list contain only IPv4 patterns */
1038 list_for_each_entry(lst, &expr->patterns, list) {
1039 pattern = &lst->pat;
1040
1041 /* The input sample is IPv4, use it as is. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001042 if (smp->data.type == SMP_T_IPV4) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001043 v4 = smp->data.u.ipv4.s_addr;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +01001044 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001045 else if (smp->data.type == SMP_T_IPV6) {
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001046 /* v4 match on a V6 sample. We want to check at least for
1047 * the following forms :
1048 * - ::ffff:ip:v4 (ipv4 mapped)
1049 * - ::0000:ip:v4 (old ipv4 mapped)
1050 * - 2002:ip:v4:: (6to4)
1051 */
Willy Tarreau296cfd12020-02-25 09:58:41 +01001052 if (read_u64(&smp->data.u.ipv6.s6_addr[0]) == 0 &&
1053 (read_u32(&smp->data.u.ipv6.s6_addr[8]) == 0 ||
1054 read_u32(&smp->data.u.ipv6.s6_addr[8]) == htonl(0xFFFF))) {
1055 v4 = read_u32(&smp->data.u.ipv6.s6_addr[12]);
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01001056 }
Willy Tarreau296cfd12020-02-25 09:58:41 +01001057 else if (read_u16(&smp->data.u.ipv6.s6_addr[0]) == htons(0x2002)) {
1058 v4 = htonl((ntohs(read_u16(&smp->data.u.ipv6.s6_addr[2])) << 16) +
1059 ntohs(read_u16(&smp->data.u.ipv6.s6_addr[4])));
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01001060 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001061 else
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01001062 continue;
Andreas Seltenreichf0653192016-03-03 20:08:35 +01001063 } else {
1064 /* impossible */
1065 continue;
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001066 }
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +01001067
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001068 /* Check if the input sample match the current pattern. */
1069 if (((v4 ^ pattern->val.ipv4.addr.s_addr) & pattern->val.ipv4.mask.s_addr) == 0)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01001070 return pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +01001071 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01001072 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +01001073}
1074
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001075void free_pattern_tree(struct eb_root *root)
1076{
1077 struct eb_node *node, *next;
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +01001078 struct pattern_tree *elt;
Thierry FOURNIER3ce88c72013-12-09 11:29:46 +01001079
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001080 node = eb_first(root);
1081 while (node) {
1082 next = eb_next(node);
1083 eb_delete(node);
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +01001084 elt = container_of(node, struct pattern_tree, node);
Willy Tarreau4bdd0a12020-11-02 12:10:48 +01001085 LIST_DEL(&elt->from_ref);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001086 free(elt->data);
Thierry FOURNIER3ce88c72013-12-09 11:29:46 +01001087 free(elt);
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001088 node = next;
1089 }
1090}
1091
Willy Tarreau6d8a6892020-11-02 19:26:02 +01001092void pat_prune_gen(struct pattern_expr *expr)
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +01001093{
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01001094 struct pattern_list *pat, *tmp;
1095
1096 list_for_each_entry_safe(pat, tmp, &expr->patterns, list) {
Christopher Faulet6cfc8512020-09-09 16:09:44 +02001097 LIST_DEL(&pat->list);
Willy Tarreau4bdd0a12020-11-02 12:10:48 +01001098 LIST_DEL(&pat->from_ref);
Willy Tarreau6d8a6892020-11-02 19:26:02 +01001099 if (pat->pat.sflags & PAT_SF_REGFREE)
1100 regex_free(pat->pat.ptr.ptr);
1101 else
1102 free(pat->pat.ptr.ptr);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001103 free(pat->pat.data);
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01001104 free(pat);
1105 }
1106
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +01001107 free_pattern_tree(&expr->pattern_tree);
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001108 free_pattern_tree(&expr->pattern_tree_2);
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +01001109 LIST_INIT(&expr->patterns);
Willy Tarreau3ee0de12020-11-02 15:26:51 +01001110 expr->ref->revision = rdtsc();
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +01001111}
1112
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001113/*
1114 *
1115 * The following functions are used for the pattern indexation
1116 *
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001117 */
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001118
1119int pat_idx_list_val(struct pattern_expr *expr, struct pattern *pat, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001120{
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001121 struct pattern_list *patl;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001122
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001123 /* allocate pattern */
1124 patl = calloc(1, sizeof(*patl));
1125 if (!patl) {
1126 memprintf(err, "out of memory while indexing pattern");
Thierry FOURNIER972028f2014-01-23 17:53:31 +01001127 return 0;
1128 }
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001129
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001130 /* duplicate pattern */
1131 memcpy(&patl->pat, pat, sizeof(*pat));
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001132
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001133 /* chain pattern in the expression */
1134 LIST_ADDQ(&expr->patterns, &patl->list);
Willy Tarreau4bdd0a12020-11-02 12:10:48 +01001135 /* and from the reference */
1136 LIST_ADDQ(&pat->ref->list_head, &patl->from_ref);
Willy Tarreau3ee0de12020-11-02 15:26:51 +01001137 expr->ref->revision = rdtsc();
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001138
1139 /* that's ok */
1140 return 1;
1141}
1142
1143int pat_idx_list_ptr(struct pattern_expr *expr, struct pattern *pat, char **err)
1144{
1145 struct pattern_list *patl;
1146
1147 /* allocate pattern */
1148 patl = calloc(1, sizeof(*patl));
Thierry FOURNIER8aa83842015-02-06 17:50:55 +01001149 if (!patl) {
1150 memprintf(err, "out of memory while indexing pattern");
Thierry FOURNIER972028f2014-01-23 17:53:31 +01001151 return 0;
Thierry FOURNIER8aa83842015-02-06 17:50:55 +01001152 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001153
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001154 /* duplicate pattern */
1155 memcpy(&patl->pat, pat, sizeof(*pat));
1156 patl->pat.ptr.ptr = malloc(patl->pat.len);
1157 if (!patl->pat.ptr.ptr) {
1158 free(patl);
1159 memprintf(err, "out of memory while indexing pattern");
1160 return 0;
1161 }
1162 memcpy(patl->pat.ptr.ptr, pat->ptr.ptr, pat->len);
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001163
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001164 /* chain pattern in the expression */
1165 LIST_ADDQ(&expr->patterns, &patl->list);
Willy Tarreau4bdd0a12020-11-02 12:10:48 +01001166 /* and from the reference */
1167 LIST_ADDQ(&pat->ref->list_head, &patl->from_ref);
Willy Tarreau3ee0de12020-11-02 15:26:51 +01001168 expr->ref->revision = rdtsc();
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001169
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001170 /* that's ok */
1171 return 1;
1172}
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001173
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001174int pat_idx_list_str(struct pattern_expr *expr, struct pattern *pat, char **err)
1175{
1176 struct pattern_list *patl;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001177
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001178 /* allocate pattern */
1179 patl = calloc(1, sizeof(*patl));
1180 if (!patl) {
1181 memprintf(err, "out of memory while indexing pattern");
1182 return 0;
1183 }
1184
1185 /* duplicate pattern */
1186 memcpy(&patl->pat, pat, sizeof(*pat));
1187 patl->pat.ptr.str = malloc(patl->pat.len + 1);
1188 if (!patl->pat.ptr.str) {
1189 free(patl);
1190 memprintf(err, "out of memory while indexing pattern");
1191 return 0;
1192 }
1193 memcpy(patl->pat.ptr.ptr, pat->ptr.ptr, pat->len);
1194 patl->pat.ptr.str[patl->pat.len] = '\0';
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001195
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001196 /* chain pattern in the expression */
1197 LIST_ADDQ(&expr->patterns, &patl->list);
Willy Tarreau4bdd0a12020-11-02 12:10:48 +01001198 /* and from the reference */
1199 LIST_ADDQ(&pat->ref->list_head, &patl->from_ref);
Willy Tarreau3ee0de12020-11-02 15:26:51 +01001200 expr->ref->revision = rdtsc();
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001201
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001202 /* that's ok */
1203 return 1;
1204}
1205
Thierry Fournier8feaa662016-02-10 22:55:20 +01001206int pat_idx_list_reg_cap(struct pattern_expr *expr, struct pattern *pat, int cap, char **err)
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001207{
1208 struct pattern_list *patl;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001209
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001210 /* allocate pattern */
1211 patl = calloc(1, sizeof(*patl));
1212 if (!patl) {
1213 memprintf(err, "out of memory while indexing pattern");
1214 return 0;
Thierry FOURNIER972028f2014-01-23 17:53:31 +01001215 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001216
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001217 /* duplicate pattern */
1218 memcpy(&patl->pat, pat, sizeof(*pat));
1219
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001220 /* compile regex */
Willy Tarreau9b5c8bb2020-11-02 19:16:23 +01001221 patl->pat.sflags |= PAT_SF_REGFREE;
Dragan Dosen26743032019-04-30 15:54:36 +02001222 if (!(patl->pat.ptr.reg = regex_comp(pat->ptr.str, !(expr->mflags & PAT_MF_IGNORE_CASE),
1223 cap, err))) {
Dirkjan Bussink07fcaaa2014-04-28 22:57:16 +00001224 free(patl);
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001225 return 0;
1226 }
1227
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001228 /* chain pattern in the expression */
1229 LIST_ADDQ(&expr->patterns, &patl->list);
Willy Tarreau4bdd0a12020-11-02 12:10:48 +01001230 /* and from the reference */
1231 LIST_ADDQ(&pat->ref->list_head, &patl->from_ref);
Willy Tarreau3ee0de12020-11-02 15:26:51 +01001232 expr->ref->revision = rdtsc();
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001233
1234 /* that's ok */
1235 return 1;
1236}
1237
Thierry Fournier8feaa662016-02-10 22:55:20 +01001238int pat_idx_list_reg(struct pattern_expr *expr, struct pattern *pat, char **err)
1239{
1240 return pat_idx_list_reg_cap(expr, pat, 0, err);
1241}
1242
1243int pat_idx_list_regm(struct pattern_expr *expr, struct pattern *pat, char **err)
1244{
1245 return pat_idx_list_reg_cap(expr, pat, 1, err);
1246}
1247
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001248int pat_idx_tree_ip(struct pattern_expr *expr, struct pattern *pat, char **err)
1249{
1250 unsigned int mask;
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +01001251 struct pattern_tree *node;
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001252
1253 /* Only IPv4 can be indexed */
1254 if (pat->type == SMP_T_IPV4) {
Thierry FOURNIER972028f2014-01-23 17:53:31 +01001255 /* in IPv4 case, check if the mask is contiguous so that we can
1256 * insert the network into the tree. A continuous mask has only
1257 * ones on the left. This means that this mask + its lower bit
1258 * added once again is null.
1259 */
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001260 mask = ntohl(pat->val.ipv4.mask.s_addr);
1261 if (mask + (mask & -mask) == 0) {
1262 mask = mask ? 33 - flsnz(mask & -mask) : 0; /* equals cidr value */
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001263
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001264 /* node memory allocation */
1265 node = calloc(1, sizeof(*node) + 4);
1266 if (!node) {
1267 memprintf(err, "out of memory while loading pattern");
1268 return 0;
1269 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001270
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001271 /* copy the pointer to sample associated to this node */
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001272 node->data = pat->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01001273 node->ref = pat->ref;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001274
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001275 /* FIXME: insert <addr>/<mask> into the tree here */
1276 memcpy(node->node.key, &pat->val.ipv4.addr, 4); /* network byte order */
1277 node->node.node.pfx = mask;
Thierry FOURNIER31db4ae2014-01-30 00:27:15 +01001278
1279 /* Insert the entry. */
1280 ebmb_insert_prefix(&expr->pattern_tree, &node->node, 4);
Willy Tarreau4bdd0a12020-11-02 12:10:48 +01001281 LIST_ADDQ(&pat->ref->tree_head, &node->from_ref);
Willy Tarreau3ee0de12020-11-02 15:26:51 +01001282 expr->ref->revision = rdtsc();
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001283
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001284 /* that's ok */
1285 return 1;
1286 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001287 else {
1288 /* If the mask is not contiguous, just add the pattern to the list */
1289 return pat_idx_list_val(expr, pat, err);
1290 }
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001291 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001292 else if (pat->type == SMP_T_IPV6) {
1293 /* IPv6 also can be indexed */
1294 node = calloc(1, sizeof(*node) + 16);
1295 if (!node) {
1296 memprintf(err, "out of memory while loading pattern");
1297 return 0;
1298 }
1299
1300 /* copy the pointer to sample associated to this node */
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001301 node->data = pat->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01001302 node->ref = pat->ref;
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001303
1304 /* FIXME: insert <addr>/<mask> into the tree here */
1305 memcpy(node->node.key, &pat->val.ipv6.addr, 16); /* network byte order */
1306 node->node.node.pfx = pat->val.ipv6.mask;
Thierry FOURNIER31db4ae2014-01-30 00:27:15 +01001307
1308 /* Insert the entry. */
1309 ebmb_insert_prefix(&expr->pattern_tree_2, &node->node, 16);
Willy Tarreau4bdd0a12020-11-02 12:10:48 +01001310 LIST_ADDQ(&pat->ref->tree_head, &node->from_ref);
Willy Tarreau3ee0de12020-11-02 15:26:51 +01001311 expr->ref->revision = rdtsc();
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001312
1313 /* that's ok */
1314 return 1;
1315 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001316
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001317 return 0;
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001318}
1319
1320int pat_idx_tree_str(struct pattern_expr *expr, struct pattern *pat, char **err)
1321{
1322 int len;
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +01001323 struct pattern_tree *node;
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001324
1325 /* Only string can be indexed */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001326 if (pat->type != SMP_T_STR) {
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001327 memprintf(err, "internal error: string expected, but the type is '%s'",
1328 smp_to_type[pat->type]);
1329 return 0;
Thierry FOURNIER972028f2014-01-23 17:53:31 +01001330 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001331
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001332 /* If the flag PAT_F_IGNORE_CASE is set, we cannot use trees */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02001333 if (expr->mflags & PAT_MF_IGNORE_CASE)
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001334 return pat_idx_list_str(expr, pat, err);
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001335
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001336 /* Process the key len */
1337 len = strlen(pat->ptr.str) + 1;
1338
1339 /* node memory allocation */
1340 node = calloc(1, sizeof(*node) + len);
1341 if (!node) {
1342 memprintf(err, "out of memory while loading pattern");
1343 return 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001344 }
1345
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001346 /* copy the pointer to sample associated to this node */
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001347 node->data = pat->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01001348 node->ref = pat->ref;
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001349
1350 /* copy the string */
1351 memcpy(node->node.key, pat->ptr.str, len);
1352
1353 /* index the new node */
Thierry FOURNIER31db4ae2014-01-30 00:27:15 +01001354 ebst_insert(&expr->pattern_tree, &node->node);
Willy Tarreau4bdd0a12020-11-02 12:10:48 +01001355 LIST_ADDQ(&pat->ref->tree_head, &node->from_ref);
Willy Tarreau3ee0de12020-11-02 15:26:51 +01001356 expr->ref->revision = rdtsc();
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001357
1358 /* that's ok */
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001359 return 1;
1360}
1361
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +02001362int pat_idx_tree_pfx(struct pattern_expr *expr, struct pattern *pat, char **err)
1363{
1364 int len;
1365 struct pattern_tree *node;
1366
1367 /* Only string can be indexed */
1368 if (pat->type != SMP_T_STR) {
1369 memprintf(err, "internal error: string expected, but the type is '%s'",
1370 smp_to_type[pat->type]);
1371 return 0;
1372 }
1373
1374 /* If the flag PAT_F_IGNORE_CASE is set, we cannot use trees */
1375 if (expr->mflags & PAT_MF_IGNORE_CASE)
1376 return pat_idx_list_str(expr, pat, err);
1377
1378 /* Process the key len */
1379 len = strlen(pat->ptr.str);
1380
1381 /* node memory allocation */
1382 node = calloc(1, sizeof(*node) + len + 1);
1383 if (!node) {
1384 memprintf(err, "out of memory while loading pattern");
1385 return 0;
1386 }
1387
1388 /* copy the pointer to sample associated to this node */
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001389 node->data = pat->data;
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +02001390 node->ref = pat->ref;
1391
1392 /* copy the string and the trailing zero */
1393 memcpy(node->node.key, pat->ptr.str, len + 1);
1394 node->node.node.pfx = len * 8;
1395
1396 /* index the new node */
1397 ebmb_insert_prefix(&expr->pattern_tree, &node->node, len);
Willy Tarreau4bdd0a12020-11-02 12:10:48 +01001398 LIST_ADDQ(&pat->ref->tree_head, &node->from_ref);
Willy Tarreau3ee0de12020-11-02 15:26:51 +01001399 expr->ref->revision = rdtsc();
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +02001400
1401 /* that's ok */
1402 return 1;
1403}
1404
Willy Tarreauf1c08922020-11-02 19:53:16 +01001405/* Deletes all patterns from reference <elt>. Note that all of their
Willy Tarreau78777ea2020-11-02 13:55:22 +01001406 * expressions must be locked, and the pattern lock must be held as well.
1407 */
Willy Tarreauf1c08922020-11-02 19:53:16 +01001408void pat_delete_gen(struct pat_ref *ref, struct pat_ref_elt *elt)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001409{
Willy Tarreauf1c08922020-11-02 19:53:16 +01001410 struct pattern_tree *tree, *tree_bck;
1411 struct pattern_list *pat, *pat_bck;
1412
1413 /* delete all known tree nodes. They are all allocated inline */
1414 list_for_each_entry_safe(tree, tree_bck, &elt->tree_head, from_ref) {
1415 BUG_ON(tree->ref != elt);
1416
1417 ebmb_delete(&tree->node);
1418 LIST_DEL(&tree->from_ref);
1419 free(tree->data);
1420 free(tree);
1421 }
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001422
Willy Tarreauf1c08922020-11-02 19:53:16 +01001423 /* delete all list nodes and free their pattern entries (str/reg) */
1424 list_for_each_entry_safe(pat, pat_bck, &elt->list_head, from_ref) {
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001425 /* Check equality. */
Willy Tarreau78777ea2020-11-02 13:55:22 +01001426 BUG_ON(pat->pat.ref != elt);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001427
1428 /* Delete and free entry. */
1429 LIST_DEL(&pat->list);
Willy Tarreau4bdd0a12020-11-02 12:10:48 +01001430 LIST_DEL(&pat->from_ref);
Willy Tarreau6d8a6892020-11-02 19:26:02 +01001431 if (pat->pat.sflags & PAT_SF_REGFREE)
1432 regex_free(pat->pat.ptr.reg);
1433 else
1434 free(pat->pat.ptr.ptr);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001435 free(pat->pat.data);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001436 free(pat);
1437 }
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001438
Willy Tarreauf1c08922020-11-02 19:53:16 +01001439 /* update revision number to refresh the cache */
1440 ref->revision = rdtsc();
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001441}
1442
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001443void pattern_init_expr(struct pattern_expr *expr)
1444{
1445 LIST_INIT(&expr->patterns);
Thierry FOURNIER31db4ae2014-01-30 00:27:15 +01001446 expr->pattern_tree = EB_ROOT;
1447 expr->pattern_tree_2 = EB_ROOT;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001448}
1449
1450void pattern_init_head(struct pattern_head *head)
1451{
1452 LIST_INIT(&head->head);
1453}
1454
1455/* The following functions are relative to the management of the reference
1456 * lists. These lists are used to store the original pattern and associated
1457 * value as string form.
1458 *
1459 * This is used with modifiable ACL and MAPS
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001460 *
1461 * The pattern reference are stored with two identifiers: the unique_id and
1462 * the reference.
1463 *
1464 * The reference identify a file. Each file with the same name point to the
1465 * same reference. We can register many times one file. If the file is modified,
1466 * all his dependencies are also modified. The reference can be used with map or
1467 * acl.
1468 *
1469 * The unique_id identify inline acl. The unique id is unique for each acl.
1470 * You cannot force the same id in the configuration file, because this repoort
1471 * an error.
1472 *
1473 * A particular case appears if the filename is a number. In this case, the
1474 * unique_id is set with the number represented by the filename and the
1475 * reference is also set. This method prevent double unique_id.
1476 *
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001477 */
1478
Willy Tarreaua5bbaaf2020-10-30 16:03:50 +01001479/* This function looks up a reference by name. If the reference is found, a
1480 * pointer to the struct pat_ref is returned, otherwise NULL is returned.
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001481 */
1482struct pat_ref *pat_ref_lookup(const char *reference)
1483{
1484 struct pat_ref *ref;
1485
1486 list_for_each_entry(ref, &pattern_reference, list)
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001487 if (ref->reference && strcmp(reference, ref->reference) == 0)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001488 return ref;
1489 return NULL;
1490}
1491
Willy Tarreaua5bbaaf2020-10-30 16:03:50 +01001492/* This function looks up a reference's unique id. If the reference is found, a
1493 * pointer to the struct pat_ref is returned, otherwise NULL is returned.
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001494 */
1495struct pat_ref *pat_ref_lookupid(int unique_id)
1496{
1497 struct pat_ref *ref;
1498
1499 list_for_each_entry(ref, &pattern_reference, list)
1500 if (ref->unique_id == unique_id)
1501 return ref;
1502 return NULL;
1503}
1504
Willy Tarreaua5bbaaf2020-10-30 16:03:50 +01001505/* This function removes all the patterns matching the pointer <refelt> from
1506 * the reference and from each expr member of this reference. This function
1507 * returns 1 if the entry was found and deleted, otherwise zero.
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001508 */
1509int pat_ref_delete_by_id(struct pat_ref *ref, struct pat_ref_elt *refelt)
1510{
1511 struct pattern_expr *expr;
1512 struct pat_ref_elt *elt, *safe;
Emeric Brun8d85aa42017-06-29 15:40:33 +02001513 struct bref *bref, *back;
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001514
1515 /* delete pattern from reference */
1516 list_for_each_entry_safe(elt, safe, &ref->head, list) {
1517 if (elt == refelt) {
Emeric Brun8d85aa42017-06-29 15:40:33 +02001518 list_for_each_entry_safe(bref, back, &elt->back_refs, users) {
1519 /*
1520 * we have to unlink all watchers. We must not relink them if
1521 * this elt was the last one in the list.
1522 */
1523 LIST_DEL(&bref->users);
1524 LIST_INIT(&bref->users);
1525 if (elt->list.n != &ref->head)
Willy Tarreau49ee3b22019-04-30 11:43:43 +02001526 LIST_ADDQ(&LIST_ELEM(elt->list.n, typeof(elt), list)->back_refs, &bref->users);
Emeric Brun8d85aa42017-06-29 15:40:33 +02001527 bref->ref = elt->list.n;
1528 }
Willy Tarreau78777ea2020-11-02 13:55:22 +01001529
Willy Tarreaue828d8f2020-11-02 20:15:40 +01001530 /* delete all entries from all expressions for this pattern */
peter caiaede6dd2015-10-07 00:07:43 -07001531 list_for_each_entry(expr, &ref->pat, list)
Willy Tarreau78777ea2020-11-02 13:55:22 +01001532 HA_RWLOCK_WRLOCK(PATEXP_LOCK, &expr->lock);
1533
Willy Tarreaue828d8f2020-11-02 20:15:40 +01001534 pat_delete_gen(ref, elt);
Willy Tarreau78777ea2020-11-02 13:55:22 +01001535
1536 list_for_each_entry(expr, &ref->pat, list)
1537 HA_RWLOCK_WRUNLOCK(PATEXP_LOCK, &expr->lock);
peter caiaede6dd2015-10-07 00:07:43 -07001538
Emeric Brunb5997f72017-07-03 11:34:05 +02001539 /* pat_ref_elt is trashed once all expr
1540 are cleaned and there is no ref remaining */
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001541 LIST_DEL(&elt->list);
1542 free(elt->sample);
1543 free(elt->pattern);
1544 free(elt);
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001545 return 1;
1546 }
1547 }
1548 return 0;
1549}
1550
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001551/* This function remove all pattern match <key> from the the reference
Joseph Herlant4189d672018-11-15 10:22:31 -08001552 * and from each expr member of the reference. This function returns 1
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001553 * if the deletion is done and return 0 is the entry is not found.
1554 */
1555int pat_ref_delete(struct pat_ref *ref, const char *key)
1556{
1557 struct pattern_expr *expr;
1558 struct pat_ref_elt *elt, *safe;
Emeric Brun8d85aa42017-06-29 15:40:33 +02001559 struct bref *bref, *back;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001560 int found = 0;
1561
1562 /* delete pattern from reference */
1563 list_for_each_entry_safe(elt, safe, &ref->head, list) {
1564 if (strcmp(key, elt->pattern) == 0) {
Emeric Brun8d85aa42017-06-29 15:40:33 +02001565 list_for_each_entry_safe(bref, back, &elt->back_refs, users) {
1566 /*
1567 * we have to unlink all watchers. We must not relink them if
1568 * this elt was the last one in the list.
1569 */
1570 LIST_DEL(&bref->users);
1571 LIST_INIT(&bref->users);
1572 if (elt->list.n != &ref->head)
Willy Tarreau49ee3b22019-04-30 11:43:43 +02001573 LIST_ADDQ(&LIST_ELEM(elt->list.n, typeof(elt), list)->back_refs, &bref->users);
Emeric Brun8d85aa42017-06-29 15:40:33 +02001574 bref->ref = elt->list.n;
1575 }
Willy Tarreau78777ea2020-11-02 13:55:22 +01001576
1577 list_for_each_entry(expr, &ref->pat, list)
1578 HA_RWLOCK_WRLOCK(PATEXP_LOCK, &expr->lock);
1579
Willy Tarreaue828d8f2020-11-02 20:15:40 +01001580 pat_delete_gen(ref, elt);
Willy Tarreau78777ea2020-11-02 13:55:22 +01001581
Dirkjan Bussink07fcaaa2014-04-28 22:57:16 +00001582 list_for_each_entry(expr, &ref->pat, list)
Willy Tarreau78777ea2020-11-02 13:55:22 +01001583 HA_RWLOCK_WRUNLOCK(PATEXP_LOCK, &expr->lock);
Dirkjan Bussink07fcaaa2014-04-28 22:57:16 +00001584
Emeric Brunb5997f72017-07-03 11:34:05 +02001585 /* pat_ref_elt is trashed once all expr
1586 are cleaned and there is no ref remaining */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001587 LIST_DEL(&elt->list);
1588 free(elt->sample);
1589 free(elt->pattern);
1590 free(elt);
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001591
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001592 found = 1;
1593 }
1594 }
1595
1596 if (!found)
1597 return 0;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001598 return 1;
1599}
1600
Baptiste Assmann953f74d2014-04-25 16:57:03 +02001601/*
1602 * find and return an element <elt> matching <key> in a reference <ref>
1603 * return NULL if not found
1604 */
1605struct pat_ref_elt *pat_ref_find_elt(struct pat_ref *ref, const char *key)
1606{
1607 struct pat_ref_elt *elt;
1608
1609 list_for_each_entry(elt, &ref->head, list) {
1610 if (strcmp(key, elt->pattern) == 0)
1611 return elt;
1612 }
1613
1614 return NULL;
1615}
1616
1617
Willy Tarreaua5bbaaf2020-10-30 16:03:50 +01001618/* This function modifies the sample of pat_ref_elt <elt> in all expressions
1619 * found under <ref> to become <value>. It is assumed that the caller has
1620 * already verified that <elt> belongs to <ref>.
1621 */
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001622static inline int pat_ref_set_elt(struct pat_ref *ref, struct pat_ref_elt *elt,
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001623 const char *value, char **err)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001624{
1625 struct pattern_expr *expr;
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001626 struct sample_data **data;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001627 char *sample;
Thierry FOURNIER12ba0c22015-08-14 00:02:11 +02001628 struct sample_data test;
Thierry FOURNIER149e0fe2014-01-29 19:35:06 +01001629
1630 /* Try all needed converters. */
1631 list_for_each_entry(expr, &ref->pat, list) {
1632 if (!expr->pat_head->parse_smp)
1633 continue;
1634
1635 if (!expr->pat_head->parse_smp(value, &test)) {
1636 memprintf(err, "unable to parse '%s'", value);
1637 return 0;
1638 }
1639 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001640
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001641 /* Modify pattern from reference. */
1642 sample = strdup(value);
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001643 if (!sample) {
1644 memprintf(err, "out of memory error");
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001645 return 0;
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001646 }
Willy Tarreaua5bbaaf2020-10-30 16:03:50 +01001647 /* Load sample in each reference. All the conversions are tested
1648 * below, normally these calls don't fail.
Thierry FOURNIER149e0fe2014-01-29 19:35:06 +01001649 */
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001650 list_for_each_entry(expr, &ref->pat, list) {
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001651 if (!expr->pat_head->parse_smp)
1652 continue;
1653
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001654 HA_RWLOCK_WRLOCK(PATEXP_LOCK, &expr->lock);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001655 data = pattern_find_smp(expr, elt);
1656 if (data && *data && !expr->pat_head->parse_smp(sample, *data))
1657 *data = NULL;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001658 HA_RWLOCK_WRUNLOCK(PATEXP_LOCK, &expr->lock);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001659 }
1660
Emeric Brunb5997f72017-07-03 11:34:05 +02001661 /* free old sample only when all exprs are updated */
1662 free(elt->sample);
1663 elt->sample = sample;
1664
1665
Thierry FOURNIER149e0fe2014-01-29 19:35:06 +01001666 return 1;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001667}
1668
Willy Tarreaua5bbaaf2020-10-30 16:03:50 +01001669/* This function modifies the sample of pat_ref_elt <refelt> in all expressions
1670 * found under <ref> to become <value>, after checking that <refelt> really
1671 * belongs to <ref>.
1672 */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001673int 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 +01001674{
1675 struct pat_ref_elt *elt;
1676
1677 /* Look for pattern in the reference. */
1678 list_for_each_entry(elt, &ref->head, list) {
1679 if (elt == refelt) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001680 if (!pat_ref_set_elt(ref, elt, value, err))
1681 return 0;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001682 return 1;
1683 }
1684 }
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001685
1686 memprintf(err, "key or pattern not found");
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001687 return 0;
1688}
1689
Willy Tarreaua5bbaaf2020-10-30 16:03:50 +01001690/* This function modifies to <value> the sample of all patterns matching <key>
1691 * under <ref>.
1692 */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001693int pat_ref_set(struct pat_ref *ref, const char *key, const char *value, char **err)
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001694{
1695 struct pat_ref_elt *elt;
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001696 int found = 0;
1697 char *_merr;
1698 char **merr;
1699
1700 if (err) {
1701 merr = &_merr;
1702 *merr = NULL;
1703 }
1704 else
1705 merr = NULL;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001706
1707 /* Look for pattern in the reference. */
1708 list_for_each_entry(elt, &ref->head, list) {
1709 if (strcmp(key, elt->pattern) == 0) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001710 if (!pat_ref_set_elt(ref, elt, value, merr)) {
William Lallemand579fb252018-06-11 10:53:46 +02001711 if (err && merr) {
1712 if (!found) {
1713 *err = *merr;
1714 } else {
1715 memprintf(err, "%s, %s", *err, *merr);
1716 free(*merr);
1717 *merr = NULL;
1718 }
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001719 }
1720 }
1721 found = 1;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001722 }
1723 }
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001724
1725 if (!found) {
1726 memprintf(err, "entry not found");
1727 return 0;
1728 }
1729 return 1;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001730}
1731
Joseph Herlant4189d672018-11-15 10:22:31 -08001732/* This function creates a new reference. <ref> is the reference name.
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001733 * <flags> are PAT_REF_*. /!\ The reference is not checked, and must
1734 * be unique. The user must check the reference with "pat_ref_lookup()"
Willy Tarreaua5bbaaf2020-10-30 16:03:50 +01001735 * before calling this function. If the function fails, it returns NULL,
1736 * otherwise it returns the new struct pat_ref.
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001737 */
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001738struct pat_ref *pat_ref_new(const char *reference, const char *display, unsigned int flags)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001739{
1740 struct pat_ref *ref;
1741
Willy Tarreau8135d9b2020-10-30 15:35:11 +01001742 ref = calloc(1, sizeof(*ref));
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001743 if (!ref)
1744 return NULL;
1745
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001746 if (display) {
1747 ref->display = strdup(display);
1748 if (!ref->display) {
1749 free(ref);
1750 return NULL;
1751 }
1752 }
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001753
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001754 ref->reference = strdup(reference);
1755 if (!ref->reference) {
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001756 free(ref->display);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001757 free(ref);
1758 return NULL;
1759 }
1760
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001761 ref->flags = flags;
1762 ref->unique_id = -1;
Willy Tarreau3ee0de12020-11-02 15:26:51 +01001763 ref->revision = 0;
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001764
1765 LIST_INIT(&ref->head);
1766 LIST_INIT(&ref->pat);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001767 HA_SPIN_INIT(&ref->lock);
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001768 LIST_ADDQ(&pattern_reference, &ref->list);
1769
1770 return ref;
1771}
1772
Willy Tarreaua5bbaaf2020-10-30 16:03:50 +01001773/* This function creates a new reference. <unique_id> is the unique id. If
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001774 * the value of <unique_id> is -1, the unique id is calculated later.
1775 * <flags> are PAT_REF_*. /!\ The reference is not checked, and must
1776 * be unique. The user must check the reference with "pat_ref_lookup()"
1777 * or pat_ref_lookupid before calling this function. If the function
Willy Tarreaua5bbaaf2020-10-30 16:03:50 +01001778 * fails, it returns NULL, otherwise it returns the new struct pat_ref.
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001779 */
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001780struct pat_ref *pat_ref_newid(int unique_id, const char *display, unsigned int flags)
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001781{
1782 struct pat_ref *ref;
1783
Willy Tarreau8135d9b2020-10-30 15:35:11 +01001784 ref = calloc(1, sizeof(*ref));
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001785 if (!ref)
1786 return NULL;
1787
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001788 if (display) {
1789 ref->display = strdup(display);
1790 if (!ref->display) {
1791 free(ref);
1792 return NULL;
1793 }
1794 }
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001795
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001796 ref->reference = NULL;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001797 ref->flags = flags;
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001798 ref->unique_id = unique_id;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001799 LIST_INIT(&ref->head);
1800 LIST_INIT(&ref->pat);
Aurélien Nephtali564d15a2018-04-19 16:56:07 +02001801 HA_SPIN_INIT(&ref->lock);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001802 LIST_ADDQ(&pattern_reference, &ref->list);
1803
1804 return ref;
1805}
1806
Willy Tarreauf4edb722020-10-28 10:52:46 +01001807/* This function adds entry to <ref>. It can fail on memory error. It returns
1808 * the newly added element on success, or NULL on failure. The PATREF_LOCK on
1809 * <ref> must be held.
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001810 */
Willy Tarreauf4edb722020-10-28 10:52:46 +01001811struct 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 +01001812{
1813 struct pat_ref_elt *elt;
1814
Willy Tarreau8135d9b2020-10-30 15:35:11 +01001815 elt = calloc(1, sizeof(*elt));
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001816 if (!elt)
Willy Tarreauf4edb722020-10-28 10:52:46 +01001817 goto fail;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001818
1819 elt->line = line;
1820
1821 elt->pattern = strdup(pattern);
Willy Tarreauf4edb722020-10-28 10:52:46 +01001822 if (!elt->pattern)
1823 goto fail;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001824
1825 if (sample) {
1826 elt->sample = strdup(sample);
Willy Tarreauf4edb722020-10-28 10:52:46 +01001827 if (!elt->sample)
1828 goto fail;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001829 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001830
Emeric Brun8d85aa42017-06-29 15:40:33 +02001831 LIST_INIT(&elt->back_refs);
Willy Tarreau4bdd0a12020-11-02 12:10:48 +01001832 LIST_INIT(&elt->list_head);
1833 LIST_INIT(&elt->tree_head);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001834 LIST_ADDQ(&ref->head, &elt->list);
Willy Tarreauf4edb722020-10-28 10:52:46 +01001835 return elt;
1836 fail:
1837 if (elt)
1838 free(elt->pattern);
1839 free(elt);
1840 return NULL;
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001841}
1842
Willy Tarreaua5bbaaf2020-10-30 16:03:50 +01001843/* This function creates sample found in <elt>, parses the pattern also
1844 * found in <elt> and inserts it in <expr>. The function copies <patflags>
1845 * into <expr>. If the function fails, it returns 0 and <err> is filled.
Ilya Shipitsin47d17182020-06-21 21:42:57 +05001846 * In success case, the function returns 1.
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001847 */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001848int pat_ref_push(struct pat_ref_elt *elt, struct pattern_expr *expr,
1849 int patflags, char **err)
1850{
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001851 struct sample_data *data;
Thierry FOURNIERd25c8422014-01-28 15:34:35 +01001852 struct pattern pattern;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001853
1854 /* Create sample */
1855 if (elt->sample && expr->pat_head->parse_smp) {
1856 /* New sample. */
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001857 data = malloc(sizeof(*data));
1858 if (!data)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001859 return 0;
1860
1861 /* Parse value. */
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001862 if (!expr->pat_head->parse_smp(elt->sample, data)) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001863 memprintf(err, "unable to parse '%s'", elt->sample);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001864 free(data);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001865 return 0;
1866 }
1867
1868 }
1869 else
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001870 data = NULL;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001871
Thierry FOURNIERd25c8422014-01-28 15:34:35 +01001872 /* initialise pattern */
1873 memset(&pattern, 0, sizeof(pattern));
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001874 pattern.data = data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01001875 pattern.ref = elt;
Thierry FOURNIERd25c8422014-01-28 15:34:35 +01001876
1877 /* parse pattern */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02001878 if (!expr->pat_head->parse(elt->pattern, &pattern, expr->mflags, err)) {
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001879 free(data);
Thierry FOURNIERd25c8422014-01-28 15:34:35 +01001880 return 0;
1881 }
1882
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001883 HA_RWLOCK_WRLOCK(PATEXP_LOCK, &expr->lock);
Thierry FOURNIERd25c8422014-01-28 15:34:35 +01001884 /* index pattern */
1885 if (!expr->pat_head->index(expr, &pattern, err)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001886 HA_RWLOCK_WRUNLOCK(PATEXP_LOCK, &expr->lock);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001887 free(data);
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001888 return 0;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001889 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001890 HA_RWLOCK_WRUNLOCK(PATEXP_LOCK, &expr->lock);
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001891
1892 return 1;
1893}
1894
Willy Tarreau6a174072020-10-28 10:58:05 +01001895/* This function adds entry to <ref>. It can fail on memory error. The new
Thierry FOURNIER31db4ae2014-01-30 00:27:15 +01001896 * entry is added at all the pattern_expr registered in this reference. The
Willy Tarreau6a174072020-10-28 10:58:05 +01001897 * function stops on the first error encountered. It returns 0 and <err> is
Thierry FOURNIER31db4ae2014-01-30 00:27:15 +01001898 * filled. If an error is encountered, the complete add operation is cancelled.
1899 * If the insertion is a success the function returns 1.
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001900 */
1901int pat_ref_add(struct pat_ref *ref,
1902 const char *pattern, const char *sample,
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02001903 char **err)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001904{
1905 struct pat_ref_elt *elt;
1906 struct pattern_expr *expr;
1907
Willy Tarreau6a174072020-10-28 10:58:05 +01001908 elt = pat_ref_append(ref, pattern, sample, -1);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001909 if (!elt) {
1910 memprintf(err, "out of memory error");
1911 return 0;
1912 }
1913
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001914 list_for_each_entry(expr, &ref->pat, list) {
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02001915 if (!pat_ref_push(elt, expr, 0, err)) {
Thierry FOURNIER31db4ae2014-01-30 00:27:15 +01001916 /* If the insertion fails, try to delete all the added entries. */
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001917 pat_ref_delete_by_id(ref, elt);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001918 return 0;
1919 }
1920 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001921 return 1;
1922}
1923
Joseph Herlant4189d672018-11-15 10:22:31 -08001924/* This function prunes <ref>, replaces all references by the references
1925 * of <replace>, and reindexes all the news values.
Thierry FOURNIER46006bd2014-03-21 21:45:15 +01001926 *
Joseph Herlant4189d672018-11-15 10:22:31 -08001927 * The patterns are loaded in best effort and the errors are ignored,
1928 * but written in the logs.
Thierry FOURNIER46006bd2014-03-21 21:45:15 +01001929 */
1930void pat_ref_reload(struct pat_ref *ref, struct pat_ref *replace)
1931{
1932 struct pattern_expr *expr;
Emeric Brunb5997f72017-07-03 11:34:05 +02001933 struct pat_ref_elt *elt, *safe;
1934 struct bref *bref, *back;
Emeric Brunb5997f72017-07-03 11:34:05 +02001935 struct pattern pattern;
Thierry FOURNIER46006bd2014-03-21 21:45:15 +01001936
Thierry FOURNIER46006bd2014-03-21 21:45:15 +01001937
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001938 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Emeric Brunb5997f72017-07-03 11:34:05 +02001939 list_for_each_entry(expr, &ref->pat, list) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001940 HA_RWLOCK_WRLOCK(PATEXP_LOCK, &expr->lock);
Emeric Brunb5997f72017-07-03 11:34:05 +02001941 }
1942
1943 /* all expr are locked, we can safely remove all pat_ref */
1944 list_for_each_entry_safe(elt, safe, &ref->head, list) {
1945 list_for_each_entry_safe(bref, back, &elt->back_refs, users) {
Willy Tarreaud4164dc2020-10-27 18:55:20 +01001946 /* we have to unlink all watchers. */
1947 LIST_DEL_INIT(&bref->users);
1948 bref->ref = NULL;
Emeric Brunb5997f72017-07-03 11:34:05 +02001949 }
1950 LIST_DEL(&elt->list);
Willy Tarreau4bdd0a12020-11-02 12:10:48 +01001951 LIST_DEL(&elt->list_head);
1952 LIST_DEL(&elt->tree_head);
Emeric Brunb5997f72017-07-03 11:34:05 +02001953 free(elt->pattern);
1954 free(elt->sample);
1955 free(elt);
1956 }
1957
1958 /* switch pat_ret_elt lists */
Thierry FOURNIER46006bd2014-03-21 21:45:15 +01001959 LIST_ADD(&replace->head, &ref->head);
1960 LIST_DEL(&replace->head);
1961
Emeric Brunb5997f72017-07-03 11:34:05 +02001962 list_for_each_entry(expr, &ref->pat, list) {
1963 expr->pat_head->prune(expr);
1964 list_for_each_entry(elt, &ref->head, list) {
Dragan Dosenf1474792018-09-18 20:18:09 +02001965 char *err = NULL;
1966 struct sample_data *data = NULL;
1967
Emeric Brunb5997f72017-07-03 11:34:05 +02001968 /* Create sample */
1969 if (elt->sample && expr->pat_head->parse_smp) {
1970 /* New sample. */
1971 data = malloc(sizeof(*data));
1972 if (!data)
1973 continue;
1974
1975 /* Parse value. */
1976 if (!expr->pat_head->parse_smp(elt->sample, data)) {
1977 memprintf(&err, "unable to parse '%s'", elt->sample);
1978 send_log(NULL, LOG_NOTICE, "%s", err);
1979 free(err);
1980 free(data);
1981 continue;
1982 }
1983
1984 }
Emeric Brunb5997f72017-07-03 11:34:05 +02001985
1986 /* initialise pattern */
1987 memset(&pattern, 0, sizeof(pattern));
1988 pattern.data = data;
1989 pattern.ref = elt;
1990
1991 /* parse pattern */
1992 if (!expr->pat_head->parse(elt->pattern, &pattern, expr->mflags, &err)) {
Thierry FOURNIER46006bd2014-03-21 21:45:15 +01001993 send_log(NULL, LOG_NOTICE, "%s", err);
1994 free(err);
Emeric Brunb5997f72017-07-03 11:34:05 +02001995 free(data);
1996 continue;
1997 }
1998
1999 /* index pattern */
2000 if (!expr->pat_head->index(expr, &pattern, &err)) {
2001 send_log(NULL, LOG_NOTICE, "%s", err);
2002 free(err);
2003 free(data);
2004 continue;
Thierry FOURNIER46006bd2014-03-21 21:45:15 +01002005 }
2006 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002007 HA_RWLOCK_WRUNLOCK(PATEXP_LOCK, &expr->lock);
Thierry FOURNIER46006bd2014-03-21 21:45:15 +01002008 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002009 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Willy Tarreau114d6982020-11-03 15:55:35 +01002010
2011#if defined(HA_HAVE_MALLOC_TRIM)
2012 malloc_trim(0);
2013#endif
Thierry FOURNIER46006bd2014-03-21 21:45:15 +01002014}
2015
Willy Tarreaua5bbaaf2020-10-30 16:03:50 +01002016/* This function prunes all entries of <ref>. This function
Willy Tarreaud1d005d2019-12-20 18:22:02 +01002017 * prunes the associated pattern_expr. It may return before the end of
2018 * the list is reached, returning 0, to yield. The caller must call it
2019 * again. Otherwise it returns 1 once done.
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002020 */
Willy Tarreaud1d005d2019-12-20 18:22:02 +01002021int pat_ref_prune(struct pat_ref *ref)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002022{
2023 struct pat_ref_elt *elt, *safe;
2024 struct pattern_expr *expr;
Emeric Brun8d85aa42017-06-29 15:40:33 +02002025 struct bref *bref, *back;
Willy Tarreaud1d005d2019-12-20 18:22:02 +01002026 int loops = 0;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002027
Emeric Brunb5997f72017-07-03 11:34:05 +02002028 list_for_each_entry(expr, &ref->pat, list) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002029 HA_RWLOCK_WRLOCK(PATEXP_LOCK, &expr->lock);
Emeric Brunb5997f72017-07-03 11:34:05 +02002030 expr->pat_head->prune(expr);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002031 HA_RWLOCK_WRUNLOCK(PATEXP_LOCK, &expr->lock);
Willy Tarreaud1d005d2019-12-20 18:22:02 +01002032 loops++;
2033 /* yield often, some lists may be huge, especially those
2034 * having to be freed through free_pattern_tree()
2035 */
2036 if (loops > 10)
2037 return 0;
Emeric Brunb5997f72017-07-03 11:34:05 +02002038 }
2039
2040 /* we trash pat_ref_elt in a second time to ensure that data is
2041 free once there is no ref on it */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002042 list_for_each_entry_safe(elt, safe, &ref->head, list) {
Emeric Brun8d85aa42017-06-29 15:40:33 +02002043 list_for_each_entry_safe(bref, back, &elt->back_refs, users) {
2044 /*
2045 * we have to unlink all watchers. We must not relink them if
2046 * this elt was the last one in the list.
2047 */
2048 LIST_DEL(&bref->users);
2049 LIST_INIT(&bref->users);
2050 if (elt->list.n != &ref->head)
Willy Tarreau49ee3b22019-04-30 11:43:43 +02002051 LIST_ADDQ(&LIST_ELEM(elt->list.n, typeof(elt), list)->back_refs, &bref->users);
Emeric Brun8d85aa42017-06-29 15:40:33 +02002052 bref->ref = elt->list.n;
2053 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002054 LIST_DEL(&elt->list);
2055 free(elt->pattern);
2056 free(elt->sample);
2057 free(elt);
Willy Tarreaud1d005d2019-12-20 18:22:02 +01002058 loops++;
2059 if (loops > 100000)
2060 return 0;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002061 }
Willy Tarreaud1d005d2019-12-20 18:22:02 +01002062 return 1;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002063}
2064
Willy Tarreaua5bbaaf2020-10-30 16:03:50 +01002065/* This function looks up any existing reference <ref> in pattern_head <head>, and
2066 * returns the associated pattern_expr pointer if found, otherwise NULL.
2067 */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002068struct pattern_expr *pattern_lookup_expr(struct pattern_head *head, struct pat_ref *ref)
2069{
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002070 struct pattern_expr_list *expr;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002071
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002072 list_for_each_entry(expr, &head->head, list)
2073 if (expr->expr->ref == ref)
2074 return expr->expr;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002075 return NULL;
2076}
2077
Joseph Herlant4189d672018-11-15 10:22:31 -08002078/* This function creates new pattern_expr associated to the reference <ref>.
2079 * <ref> can be NULL. If an error occurs, the function returns NULL and
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002080 * <err> is filled. Otherwise, the function returns new pattern_expr linked
2081 * with <head> and <ref>.
Thierry FOURNIER315ec422014-11-24 11:14:42 +01002082 *
Joseph Herlant4189d672018-11-15 10:22:31 -08002083 * The returned value can be an already filled pattern list, in this case the
Thierry FOURNIER315ec422014-11-24 11:14:42 +01002084 * flag <reuse> is set.
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002085 */
Thierry FOURNIER315ec422014-11-24 11:14:42 +01002086struct pattern_expr *pattern_new_expr(struct pattern_head *head, struct pat_ref *ref,
Emeric Brun7d27f3c2017-07-03 17:54:23 +02002087 int patflags, char **err, int *reuse)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002088{
2089 struct pattern_expr *expr;
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002090 struct pattern_expr_list *list;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002091
Thierry FOURNIER315ec422014-11-24 11:14:42 +01002092 if (reuse)
2093 *reuse = 0;
2094
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002095 /* Memory and initialization of the chain element. */
Willy Tarreau8135d9b2020-10-30 15:35:11 +01002096 list = calloc(1, sizeof(*list));
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002097 if (!list) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002098 memprintf(err, "out of memory");
2099 return NULL;
2100 }
2101
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002102 /* Look for existing similar expr. No that only the index, parse and
2103 * parse_smp function must be identical for having similar pattern.
Joseph Herlant4189d672018-11-15 10:22:31 -08002104 * The other function depends of these first.
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002105 */
2106 if (ref) {
2107 list_for_each_entry(expr, &ref->pat, list)
2108 if (expr->pat_head->index == head->index &&
2109 expr->pat_head->parse == head->parse &&
Emeric Brun7d27f3c2017-07-03 17:54:23 +02002110 expr->pat_head->parse_smp == head->parse_smp &&
2111 expr->mflags == patflags)
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002112 break;
2113 if (&expr->list == &ref->pat)
2114 expr = NULL;
2115 }
2116 else
2117 expr = NULL;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002118
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002119 /* If no similar expr was found, we create new expr. */
2120 if (!expr) {
2121 /* Get a lot of memory for the expr struct. */
Willy Tarreau8135d9b2020-10-30 15:35:11 +01002122 expr = calloc(1, sizeof(*expr));
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002123 if (!expr) {
Andreas Seltenreiche6e22e82016-03-03 20:20:23 +01002124 free(list);
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002125 memprintf(err, "out of memory");
2126 return NULL;
2127 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002128
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002129 /* Initialize this new expr. */
2130 pattern_init_expr(expr);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002131
Emeric Brun7d27f3c2017-07-03 17:54:23 +02002132 /* Copy the pattern matching and indexing flags. */
2133 expr->mflags = patflags;
2134
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002135 /* This new pattern expression reference one of his heads. */
2136 expr->pat_head = head;
2137
2138 /* Link with ref, or to self to facilitate LIST_DEL() */
2139 if (ref)
2140 LIST_ADDQ(&ref->pat, &expr->list);
2141 else
2142 LIST_INIT(&expr->list);
2143
2144 expr->ref = ref;
2145
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002146 HA_RWLOCK_INIT(&expr->lock);
Emeric Brunb5997f72017-07-03 11:34:05 +02002147
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002148 /* We must free this pattern if it is no more used. */
2149 list->do_free = 1;
2150 }
2151 else {
2152 /* If the pattern used already exists, it is already linked
2153 * with ref and we must not free it.
2154 */
2155 list->do_free = 0;
Thierry FOURNIER315ec422014-11-24 11:14:42 +01002156 if (reuse)
2157 *reuse = 1;
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002158 }
2159
2160 /* The new list element reference the pattern_expr. */
2161 list->expr = expr;
2162
2163 /* Link the list element with the pattern_head. */
2164 LIST_ADDQ(&head->head, &list->list);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002165 return expr;
2166}
2167
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002168/* Reads patterns from a file. If <err_msg> is non-NULL, an error message will
2169 * be returned there on errors and the caller will have to free it.
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002170 *
2171 * The file contains one key + value per line. Lines which start with '#' are
2172 * ignored, just like empty lines. Leading tabs/spaces are stripped. The key is
2173 * then the first "word" (series of non-space/tabs characters), and the value is
2174 * what follows this series of space/tab till the end of the line excluding
2175 * trailing spaces/tabs.
2176 *
2177 * Example :
2178 *
2179 * # this is a comment and is ignored
2180 * 62.212.114.60 1wt.eu \n
2181 * <-><-----------><---><----><---->
2182 * | | | | `--- trailing spaces ignored
2183 * | | | `-------- value
2184 * | | `--------------- middle spaces ignored
2185 * | `------------------------ key
2186 * `-------------------------------- leading spaces ignored
2187 *
Ilya Shipitsin47d17182020-06-21 21:42:57 +05002188 * Return non-zero in case of success, otherwise 0.
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002189 */
2190int pat_ref_read_from_file_smp(struct pat_ref *ref, const char *filename, char **err)
2191{
2192 FILE *file;
2193 char *c;
2194 int ret = 0;
2195 int line = 0;
2196 char *key_beg;
2197 char *key_end;
2198 char *value_beg;
2199 char *value_end;
2200
2201 file = fopen(filename, "r");
2202 if (!file) {
2203 memprintf(err, "failed to open pattern file <%s>", filename);
2204 return 0;
2205 }
2206
2207 /* now parse all patterns. The file may contain only one pattern
2208 * followed by one value per line. The start spaces, separator spaces
2209 * and and spaces are stripped. Each can contain comment started by '#'
2210 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002211 while (fgets(trash.area, trash.size, file) != NULL) {
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002212 line++;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002213 c = trash.area;
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002214
2215 /* ignore lines beginning with a dash */
2216 if (*c == '#')
2217 continue;
2218
2219 /* strip leading spaces and tabs */
2220 while (*c == ' ' || *c == '\t')
2221 c++;
2222
2223 /* empty lines are ignored too */
2224 if (*c == '\0' || *c == '\r' || *c == '\n')
2225 continue;
2226
2227 /* look for the end of the key */
2228 key_beg = c;
2229 while (*c && *c != ' ' && *c != '\t' && *c != '\n' && *c != '\r')
2230 c++;
2231
2232 key_end = c;
2233
2234 /* strip middle spaces and tabs */
2235 while (*c == ' ' || *c == '\t')
2236 c++;
2237
2238 /* look for the end of the value, it is the end of the line */
2239 value_beg = c;
2240 while (*c && *c != '\n' && *c != '\r')
2241 c++;
2242 value_end = c;
2243
2244 /* trim possibly trailing spaces and tabs */
2245 while (value_end > value_beg && (value_end[-1] == ' ' || value_end[-1] == '\t'))
2246 value_end--;
2247
2248 /* set final \0 and check entries */
2249 *key_end = '\0';
2250 *value_end = '\0';
2251
2252 /* insert values */
2253 if (!pat_ref_append(ref, key_beg, value_beg, line)) {
2254 memprintf(err, "out of memory");
2255 goto out_close;
2256 }
2257 }
2258
Jerome Magnin3c79d4b2020-01-17 16:09:33 +01002259 if (ferror(file)) {
2260 memprintf(err, "error encountered while reading <%s> : %s",
2261 filename, strerror(errno));
2262 goto out_close;
2263 }
Ilya Shipitsin47d17182020-06-21 21:42:57 +05002264 /* success */
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002265 ret = 1;
2266
2267 out_close:
2268 fclose(file);
2269 return ret;
2270}
2271
2272/* Reads patterns from a file. If <err_msg> is non-NULL, an error message will
2273 * be returned there on errors and the caller will have to free it.
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002274 */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002275int pat_ref_read_from_file(struct pat_ref *ref, const char *filename, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002276{
2277 FILE *file;
2278 char *c;
2279 char *arg;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002280 int ret = 0;
2281 int line = 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002282
2283 file = fopen(filename, "r");
2284 if (!file) {
2285 memprintf(err, "failed to open pattern file <%s>", filename);
2286 return 0;
2287 }
2288
2289 /* now parse all patterns. The file may contain only one pattern per
2290 * line. If the line contains spaces, they will be part of the pattern.
2291 * The pattern stops at the first CR, LF or EOF encountered.
2292 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002293 while (fgets(trash.area, trash.size, file) != NULL) {
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002294 line++;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002295 c = trash.area;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002296
2297 /* ignore lines beginning with a dash */
2298 if (*c == '#')
2299 continue;
2300
2301 /* strip leading spaces and tabs */
2302 while (*c == ' ' || *c == '\t')
2303 c++;
2304
2305
2306 arg = c;
2307 while (*c && *c != '\n' && *c != '\r')
2308 c++;
2309 *c = 0;
2310
2311 /* empty lines are ignored too */
2312 if (c == arg)
2313 continue;
2314
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002315 if (!pat_ref_append(ref, arg, NULL, line)) {
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002316 memprintf(err, "out of memory when loading patterns from file <%s>", filename);
2317 goto out_close;
2318 }
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002319 }
2320
Jerome Magnin3c79d4b2020-01-17 16:09:33 +01002321 if (ferror(file)) {
2322 memprintf(err, "error encountered while reading <%s> : %s",
2323 filename, strerror(errno));
2324 goto out_close;
2325 }
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002326 ret = 1; /* success */
2327
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002328 out_close:
2329 fclose(file);
2330 return ret;
2331}
2332
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002333int pattern_read_from_file(struct pattern_head *head, unsigned int refflags,
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002334 const char *filename, int patflags, int load_smp,
Thierry FOURNIER94580c92014-02-11 14:36:45 +01002335 char **err, const char *file, int line)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002336{
2337 struct pat_ref *ref;
2338 struct pattern_expr *expr;
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002339 struct pat_ref_elt *elt;
Willy Tarreau4deaf392014-11-26 13:17:03 +01002340 int reuse = 0;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002341
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002342 /* Lookup for the existing reference. */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002343 ref = pat_ref_lookup(filename);
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002344
2345 /* If the reference doesn't exists, create it and load associated file. */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002346 if (!ref) {
Thierry FOURNIER94580c92014-02-11 14:36:45 +01002347 chunk_printf(&trash,
2348 "pattern loaded from file '%s' used by %s at file '%s' line %d",
2349 filename, refflags & PAT_REF_MAP ? "map" : "acl", file, line);
2350
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002351 ref = pat_ref_new(filename, trash.area, refflags);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002352 if (!ref) {
2353 memprintf(err, "out of memory");
2354 return 0;
2355 }
2356
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002357 if (load_smp) {
Thierry FOURNIERc0bd9102014-01-29 12:32:58 +01002358 ref->flags |= PAT_REF_SMP;
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002359 if (!pat_ref_read_from_file_smp(ref, filename, err))
2360 return 0;
2361 }
2362 else {
2363 if (!pat_ref_read_from_file(ref, filename, err))
2364 return 0;
2365 }
2366 }
2367 else {
Thierry FOURNIERc0bd9102014-01-29 12:32:58 +01002368 /* The reference already exists, check the map compatibility. */
2369
2370 /* If the load require samples and the flag PAT_REF_SMP is not set,
2371 * the reference doesn't contain sample, and cannot be used.
2372 */
2373 if (load_smp) {
2374 if (!(ref->flags & PAT_REF_SMP)) {
2375 memprintf(err, "The file \"%s\" is already used as one column file "
2376 "and cannot be used by as two column file.",
2377 filename);
2378 return 0;
2379 }
2380 }
2381 else {
2382 /* The load doesn't require samples. If the flag PAT_REF_SMP is
2383 * set, the reference contains a sample, and cannot be used.
2384 */
2385 if (ref->flags & PAT_REF_SMP) {
2386 memprintf(err, "The file \"%s\" is already used as two column file "
2387 "and cannot be used by as one column file.",
2388 filename);
2389 return 0;
2390 }
2391 }
2392
Thierry FOURNIER94580c92014-02-11 14:36:45 +01002393 /* Extends display */
2394 chunk_printf(&trash, "%s", ref->display);
2395 chunk_appendf(&trash, ", by %s at file '%s' line %d",
2396 refflags & PAT_REF_MAP ? "map" : "acl", file, line);
2397 free(ref->display);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002398 ref->display = strdup(trash.area);
Thierry FOURNIER94580c92014-02-11 14:36:45 +01002399 if (!ref->display) {
2400 memprintf(err, "out of memory");
2401 return 0;
2402 }
2403
Thierry FOURNIERc0bd9102014-01-29 12:32:58 +01002404 /* Merge flags. */
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002405 ref->flags |= refflags;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002406 }
2407
2408 /* Now, we can loading patterns from the reference. */
2409
2410 /* Lookup for existing reference in the head. If the reference
2411 * doesn't exists, create it.
2412 */
2413 expr = pattern_lookup_expr(head, ref);
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02002414 if (!expr || (expr->mflags != patflags)) {
Emeric Brun7d27f3c2017-07-03 17:54:23 +02002415 expr = pattern_new_expr(head, ref, patflags, err, &reuse);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002416 if (!expr)
2417 return 0;
2418 }
2419
Thierry FOURNIER315ec422014-11-24 11:14:42 +01002420 /* The returned expression may be not empty, because the function
2421 * "pattern_new_expr" lookup for similar pattern list and can
2422 * reuse a already filled pattern list. In this case, we can not
2423 * reload the patterns.
2424 */
2425 if (reuse)
2426 return 1;
2427
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002428 /* Load reference content in the pattern expression. */
2429 list_for_each_entry(elt, &ref->head, list) {
2430 if (!pat_ref_push(elt, expr, patflags, err)) {
2431 if (elt->line > 0)
2432 memprintf(err, "%s at line %d of file '%s'",
2433 *err, elt->line, filename);
2434 return 0;
2435 }
2436 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002437
2438 return 1;
2439}
2440
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01002441/* This function executes a pattern match on a sample. It applies pattern <expr>
2442 * to sample <smp>. The function returns NULL if the sample dont match. It returns
2443 * non-null if the sample match. If <fill> is true and the sample match, the
2444 * function returns the matched pattern. In many cases, this pattern can be a
2445 * static buffer.
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002446 */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002447struct pattern *pattern_exec_match(struct pattern_head *head, struct sample *smp, int fill)
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002448{
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002449 struct pattern_expr_list *list;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002450 struct pattern *pat;
2451
2452 if (!head->match) {
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01002453 if (fill) {
Thierry FOURNIER503bb092015-08-19 08:35:43 +02002454 static_pattern.data = NULL;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01002455 static_pattern.ref = NULL;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02002456 static_pattern.sflags = 0;
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002457 static_pattern.type = SMP_T_SINT;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01002458 static_pattern.val.i = 1;
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01002459 }
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01002460 return &static_pattern;
2461 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002462
Thierry FOURNIER5d344082014-01-27 14:19:53 +01002463 /* convert input to string */
2464 if (!sample_convert(smp, head->expect_type))
2465 return NULL;
2466
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002467 list_for_each_entry(list, &head->head, list) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002468 HA_RWLOCK_RDLOCK(PATEXP_LOCK, &list->expr->lock);
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002469 pat = head->match(smp, list->expr, fill);
Emeric Brunb5997f72017-07-03 11:34:05 +02002470 if (pat) {
2471 /* We duplicate the pattern cause it could be modified
2472 by another thread */
2473 if (pat != &static_pattern) {
2474 memcpy(&static_pattern, pat, sizeof(struct pattern));
2475 pat = &static_pattern;
2476 }
2477
2478 /* We also duplicate the sample data for
2479 same reason */
2480 if (pat->data && (pat->data != &static_sample_data)) {
Christopher Faulet09fdf4b2017-11-09 16:14:16 +01002481 switch(pat->data->type) {
Emeric Brunb5997f72017-07-03 11:34:05 +02002482 case SMP_T_STR:
2483 static_sample_data.type = SMP_T_STR;
2484 static_sample_data.u.str = *get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002485 static_sample_data.u.str.data = pat->data->u.str.data;
2486 if (static_sample_data.u.str.data >= static_sample_data.u.str.size)
2487 static_sample_data.u.str.data = static_sample_data.u.str.size - 1;
2488 memcpy(static_sample_data.u.str.area,
Willy Tarreau2fc761e2020-06-11 16:37:35 +02002489 pat->data->u.str.area, static_sample_data.u.str.data);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002490 static_sample_data.u.str.area[static_sample_data.u.str.data] = 0;
Willy Tarreau2fc761e2020-06-11 16:37:35 +02002491 pat->data = &static_sample_data;
2492 break;
2493
Emeric Brunb5997f72017-07-03 11:34:05 +02002494 case SMP_T_IPV4:
2495 case SMP_T_IPV6:
2496 case SMP_T_SINT:
2497 memcpy(&static_sample_data, pat->data, sizeof(struct sample_data));
Willy Tarreau2fc761e2020-06-11 16:37:35 +02002498 pat->data = &static_sample_data;
2499 break;
Emeric Brunb5997f72017-07-03 11:34:05 +02002500 default:
Willy Tarreau2fc761e2020-06-11 16:37:35 +02002501 /* unimplemented pattern type */
Emeric Brunb5997f72017-07-03 11:34:05 +02002502 pat->data = NULL;
Willy Tarreau2fc761e2020-06-11 16:37:35 +02002503 break;
Emeric Brunb5997f72017-07-03 11:34:05 +02002504 }
Emeric Brunb5997f72017-07-03 11:34:05 +02002505 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002506 HA_RWLOCK_RDUNLOCK(PATEXP_LOCK, &list->expr->lock);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002507 return pat;
Emeric Brunb5997f72017-07-03 11:34:05 +02002508 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002509 HA_RWLOCK_RDUNLOCK(PATEXP_LOCK, &list->expr->lock);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002510 }
2511 return NULL;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002512}
2513
Willy Tarreaua5bbaaf2020-10-30 16:03:50 +01002514/* This function prunes the pattern expressions starting at pattern_head <head>. */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002515void pattern_prune(struct pattern_head *head)
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01002516{
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002517 struct pattern_expr_list *list, *safe;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002518
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002519 list_for_each_entry_safe(list, safe, &head->head, list) {
2520 LIST_DEL(&list->list);
2521 if (list->do_free) {
2522 LIST_DEL(&list->expr->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002523 HA_RWLOCK_WRLOCK(PATEXP_LOCK, &list->expr->lock);
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002524 head->prune(list->expr);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002525 HA_RWLOCK_WRUNLOCK(PATEXP_LOCK, &list->expr->lock);
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002526 free(list->expr);
2527 }
2528 free(list);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002529 }
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01002530}
2531
Willy Tarreaua5bbaaf2020-10-30 16:03:50 +01002532/* This function searches occurrences of pattern reference element <ref> in
2533 * expression <expr> and returns a pointer to a pointer of the sample storage.
2534 * If <ref> is not found, NULL is returned.
Thierry FOURNIER55d0b102014-01-15 11:25:26 +01002535 */
Thierry FOURNIER12ba0c22015-08-14 00:02:11 +02002536struct sample_data **pattern_find_smp(struct pattern_expr *expr, struct pat_ref_elt *ref)
Thierry FOURNIER55d0b102014-01-15 11:25:26 +01002537{
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01002538 struct ebmb_node *node;
2539 struct pattern_tree *elt;
2540 struct pattern_list *pat;
Thierry FOURNIER55d0b102014-01-15 11:25:26 +01002541
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01002542 for (node = ebmb_first(&expr->pattern_tree);
2543 node;
2544 node = ebmb_next(node)) {
2545 elt = container_of(node, struct pattern_tree, node);
2546 if (elt->ref == ref)
Thierry FOURNIER503bb092015-08-19 08:35:43 +02002547 return &elt->data;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01002548 }
2549
2550 for (node = ebmb_first(&expr->pattern_tree_2);
2551 node;
2552 node = ebmb_next(node)) {
2553 elt = container_of(node, struct pattern_tree, node);
2554 if (elt->ref == ref)
Thierry FOURNIER503bb092015-08-19 08:35:43 +02002555 return &elt->data;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01002556 }
2557
2558 list_for_each_entry(pat, &expr->patterns, list)
2559 if (pat->pat.ref == ref)
Thierry FOURNIER503bb092015-08-19 08:35:43 +02002560 return &pat->pat.data;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01002561
2562 return NULL;
Thierry FOURNIER55d0b102014-01-15 11:25:26 +01002563}
2564
Willy Tarreaua5bbaaf2020-10-30 16:03:50 +01002565/* This function compares two pat_ref** on their unique_id, and returns -1/0/1
2566 * depending on their order (suitable for sorting).
2567 */
Carl Henrik Lundef91ac192020-02-27 16:45:50 +01002568static int cmp_pat_ref(const void *_a, const void *_b)
2569{
2570 struct pat_ref * const *a = _a;
2571 struct pat_ref * const *b = _b;
2572
2573 if ((*a)->unique_id < (*b)->unique_id)
2574 return -1;
2575 else if ((*a)->unique_id > (*b)->unique_id)
2576 return 1;
2577 return 0;
2578}
2579
Willy Tarreaua5bbaaf2020-10-30 16:03:50 +01002580/* This function finalizes the configuration parsing. It sets all the
2581 * automatic ids.
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002582 */
Carl Henrik Lundef91ac192020-02-27 16:45:50 +01002583int pattern_finalize_config(void)
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002584{
Tim Duesterhusb584b442020-03-17 21:08:24 +01002585 size_t len = 0;
2586 size_t unassigned_pos = 0;
Carl Henrik Lundef91ac192020-02-27 16:45:50 +01002587 int next_unique_id = 0;
Tim Duesterhusb584b442020-03-17 21:08:24 +01002588 size_t i, j;
Carl Henrik Lundef91ac192020-02-27 16:45:50 +01002589 struct pat_ref *ref, **arr;
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002590 struct list pr = LIST_HEAD_INIT(pr);
2591
Willy Tarreau52bf8392020-03-08 00:42:37 +01002592 pat_lru_seed = ha_random();
Willy Tarreauf3045d22015-04-29 16:24:50 +02002593
Carl Henrik Lundef91ac192020-02-27 16:45:50 +01002594 /* Count pat_refs with user defined unique_id and totalt count */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002595 list_for_each_entry(ref, &pattern_reference, list) {
Carl Henrik Lundef91ac192020-02-27 16:45:50 +01002596 len++;
2597 if (ref->unique_id != -1)
2598 unassigned_pos++;
2599 }
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002600
Tim Duesterhusb584b442020-03-17 21:08:24 +01002601 if (len == 0) {
2602 return 0;
2603 }
2604
Carl Henrik Lundef91ac192020-02-27 16:45:50 +01002605 arr = calloc(len, sizeof(*arr));
2606 if (arr == NULL) {
2607 ha_alert("Out of memory error.\n");
2608 return ERR_ALERT | ERR_FATAL;
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002609 }
2610
Carl Henrik Lundef91ac192020-02-27 16:45:50 +01002611 i = 0;
2612 j = unassigned_pos;
2613 list_for_each_entry(ref, &pattern_reference, list) {
2614 if (ref->unique_id != -1)
2615 arr[i++] = ref;
2616 else
2617 arr[j++] = ref;
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002618 }
2619
Carl Henrik Lundef91ac192020-02-27 16:45:50 +01002620 /* Sort first segment of array with user-defined unique ids for
2621 * fast lookup when generating unique ids
2622 */
2623 qsort(arr, unassigned_pos, sizeof(*arr), cmp_pat_ref);
2624
2625 /* Assign unique ids to the rest of the elements */
2626 for (i = unassigned_pos; i < len; i++) {
2627 do {
2628 arr[i]->unique_id = next_unique_id++;
2629 } while (bsearch(&arr[i], arr, unassigned_pos, sizeof(*arr), cmp_pat_ref));
2630 }
2631
2632 /* Sort complete array */
2633 qsort(arr, len, sizeof(*arr), cmp_pat_ref);
2634
2635 /* Convert back to linked list */
2636 for (i = 0; i < len; i++)
2637 LIST_ADDQ(&pr, &arr[i]->list);
2638
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002639 /* swap root */
2640 LIST_ADD(&pr, &pattern_reference);
2641 LIST_DEL(&pr);
Carl Henrik Lundef91ac192020-02-27 16:45:50 +01002642
2643 free(arr);
2644 return 0;
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002645}
Willy Tarreau403bfbb2019-10-23 06:59:31 +02002646
2647static int pattern_per_thread_lru_alloc()
2648{
2649 if (!global.tune.pattern_cache)
2650 return 1;
2651 pat_lru_tree = lru64_new(global.tune.pattern_cache);
2652 return !!pat_lru_tree;
2653}
2654
2655static void pattern_per_thread_lru_free()
2656{
2657 lru64_destroy(pat_lru_tree);
2658}
2659
2660REGISTER_PER_THREAD_ALLOC(pattern_per_thread_lru_alloc);
2661REGISTER_PER_THREAD_FREE(pattern_per_thread_lru_free);