blob: 5613512ce0f873c2323e4004f9413afc040fee88 [file] [log] [blame]
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001/*
2 * Pattern management functions.
3 *
4 * Copyright 2000-2013 Willy Tarreau <w@1wt.eu>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <ctype.h>
14#include <stdio.h>
Jerome Magninb8bd6d72020-01-17 18:01:20 +010015#include <errno.h>
Thierry FOURNIERed66c292013-11-28 11:05:19 +010016
Willy Tarreaub2551052020-06-09 09:07:15 +020017#include <import/ebsttree.h>
18#include <import/lru.h>
19#include <import/xxhash.h>
20
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020021#include <haproxy/api.h>
Willy Tarreauf268ee82020-06-04 17:05:57 +020022#include <haproxy/global.h>
Willy Tarreauaeed4a82020-06-04 22:01:04 +020023#include <haproxy/log.h>
Willy Tarreau6131d6a2020-06-02 16:48:09 +020024#include <haproxy/net_helper.h>
Willy Tarreau225a90a2020-06-04 15:06:28 +020025#include <haproxy/pattern.h>
Willy Tarreau7cd8b6e2020-06-02 17:32:26 +020026#include <haproxy/regex.h>
Willy Tarreaue6ce10b2020-06-04 15:33:47 +020027#include <haproxy/sample.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020028#include <haproxy/tools.h>
Thierry FOURNIERed66c292013-11-28 11:05:19 +010029
Thierry FOURNIERed66c292013-11-28 11:05:19 +010030
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010031char *pat_match_names[PAT_MATCH_NUM] = {
32 [PAT_MATCH_FOUND] = "found",
33 [PAT_MATCH_BOOL] = "bool",
34 [PAT_MATCH_INT] = "int",
35 [PAT_MATCH_IP] = "ip",
36 [PAT_MATCH_BIN] = "bin",
37 [PAT_MATCH_LEN] = "len",
38 [PAT_MATCH_STR] = "str",
39 [PAT_MATCH_BEG] = "beg",
40 [PAT_MATCH_SUB] = "sub",
41 [PAT_MATCH_DIR] = "dir",
42 [PAT_MATCH_DOM] = "dom",
43 [PAT_MATCH_END] = "end",
44 [PAT_MATCH_REG] = "reg",
Thierry Fournier8feaa662016-02-10 22:55:20 +010045 [PAT_MATCH_REGM] = "regm",
Thierry FOURNIERed66c292013-11-28 11:05:19 +010046};
47
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +020048int (*pat_parse_fcts[PAT_MATCH_NUM])(const char *, struct pattern *, int, char **) = {
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010049 [PAT_MATCH_FOUND] = pat_parse_nothing,
50 [PAT_MATCH_BOOL] = pat_parse_nothing,
51 [PAT_MATCH_INT] = pat_parse_int,
52 [PAT_MATCH_IP] = pat_parse_ip,
53 [PAT_MATCH_BIN] = pat_parse_bin,
Thierry FOURNIER5d344082014-01-27 14:19:53 +010054 [PAT_MATCH_LEN] = pat_parse_int,
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010055 [PAT_MATCH_STR] = pat_parse_str,
56 [PAT_MATCH_BEG] = pat_parse_str,
57 [PAT_MATCH_SUB] = pat_parse_str,
58 [PAT_MATCH_DIR] = pat_parse_str,
59 [PAT_MATCH_DOM] = pat_parse_str,
60 [PAT_MATCH_END] = pat_parse_str,
61 [PAT_MATCH_REG] = pat_parse_reg,
Thierry Fournier8feaa662016-02-10 22:55:20 +010062 [PAT_MATCH_REGM] = pat_parse_reg,
Thierry FOURNIERed66c292013-11-28 11:05:19 +010063};
64
Thierry FOURNIERb9b08462013-12-13 15:12:32 +010065int (*pat_index_fcts[PAT_MATCH_NUM])(struct pattern_expr *, struct pattern *, char **) = {
66 [PAT_MATCH_FOUND] = pat_idx_list_val,
67 [PAT_MATCH_BOOL] = pat_idx_list_val,
68 [PAT_MATCH_INT] = pat_idx_list_val,
69 [PAT_MATCH_IP] = pat_idx_tree_ip,
70 [PAT_MATCH_BIN] = pat_idx_list_ptr,
71 [PAT_MATCH_LEN] = pat_idx_list_val,
72 [PAT_MATCH_STR] = pat_idx_tree_str,
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +020073 [PAT_MATCH_BEG] = pat_idx_tree_pfx,
Thierry FOURNIERb9b08462013-12-13 15:12:32 +010074 [PAT_MATCH_SUB] = pat_idx_list_str,
75 [PAT_MATCH_DIR] = pat_idx_list_str,
76 [PAT_MATCH_DOM] = pat_idx_list_str,
77 [PAT_MATCH_END] = pat_idx_list_str,
78 [PAT_MATCH_REG] = pat_idx_list_reg,
Thierry Fournier8feaa662016-02-10 22:55:20 +010079 [PAT_MATCH_REGM] = pat_idx_list_regm,
Thierry FOURNIERb9b08462013-12-13 15:12:32 +010080};
81
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +010082void (*pat_prune_fcts[PAT_MATCH_NUM])(struct pattern_expr *) = {
Willy Tarreau6d8a6892020-11-02 19:26:02 +010083 [PAT_MATCH_FOUND] = pat_prune_gen,
84 [PAT_MATCH_BOOL] = pat_prune_gen,
85 [PAT_MATCH_INT] = pat_prune_gen,
86 [PAT_MATCH_IP] = pat_prune_gen,
87 [PAT_MATCH_BIN] = pat_prune_gen,
88 [PAT_MATCH_LEN] = pat_prune_gen,
89 [PAT_MATCH_STR] = pat_prune_gen,
90 [PAT_MATCH_BEG] = pat_prune_gen,
91 [PAT_MATCH_SUB] = pat_prune_gen,
92 [PAT_MATCH_DIR] = pat_prune_gen,
93 [PAT_MATCH_DOM] = pat_prune_gen,
94 [PAT_MATCH_END] = pat_prune_gen,
95 [PAT_MATCH_REG] = pat_prune_gen,
96 [PAT_MATCH_REGM] = pat_prune_gen,
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +010097};
98
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010099struct pattern *(*pat_match_fcts[PAT_MATCH_NUM])(struct sample *, struct pattern_expr *, int) = {
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100100 [PAT_MATCH_FOUND] = NULL,
101 [PAT_MATCH_BOOL] = pat_match_nothing,
102 [PAT_MATCH_INT] = pat_match_int,
103 [PAT_MATCH_IP] = pat_match_ip,
104 [PAT_MATCH_BIN] = pat_match_bin,
105 [PAT_MATCH_LEN] = pat_match_len,
106 [PAT_MATCH_STR] = pat_match_str,
107 [PAT_MATCH_BEG] = pat_match_beg,
108 [PAT_MATCH_SUB] = pat_match_sub,
109 [PAT_MATCH_DIR] = pat_match_dir,
110 [PAT_MATCH_DOM] = pat_match_dom,
111 [PAT_MATCH_END] = pat_match_end,
112 [PAT_MATCH_REG] = pat_match_reg,
Thierry Fournier8feaa662016-02-10 22:55:20 +0100113 [PAT_MATCH_REGM] = pat_match_regm,
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100114};
115
Thierry FOURNIERe3ded592013-12-06 15:36:54 +0100116/* Just used for checking configuration compatibility */
117int pat_match_types[PAT_MATCH_NUM] = {
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200118 [PAT_MATCH_FOUND] = SMP_T_SINT,
119 [PAT_MATCH_BOOL] = SMP_T_SINT,
120 [PAT_MATCH_INT] = SMP_T_SINT,
Thierry FOURNIERe3ded592013-12-06 15:36:54 +0100121 [PAT_MATCH_IP] = SMP_T_ADDR,
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100122 [PAT_MATCH_BIN] = SMP_T_BIN,
123 [PAT_MATCH_LEN] = SMP_T_STR,
124 [PAT_MATCH_STR] = SMP_T_STR,
125 [PAT_MATCH_BEG] = SMP_T_STR,
126 [PAT_MATCH_SUB] = SMP_T_STR,
127 [PAT_MATCH_DIR] = SMP_T_STR,
128 [PAT_MATCH_DOM] = SMP_T_STR,
129 [PAT_MATCH_END] = SMP_T_STR,
130 [PAT_MATCH_REG] = SMP_T_STR,
Thierry Fournier8feaa662016-02-10 22:55:20 +0100131 [PAT_MATCH_REGM] = SMP_T_STR,
Thierry FOURNIERe3ded592013-12-06 15:36:54 +0100132};
133
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +0100134/* this struct is used to return information */
Emeric Brunb5997f72017-07-03 11:34:05 +0200135static THREAD_LOCAL struct pattern static_pattern;
136static THREAD_LOCAL struct sample_data static_sample_data;
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +0100137
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100138/* This is the root of the list of all pattern_ref avalaibles. */
139struct list pattern_reference = LIST_HEAD_INIT(pattern_reference);
140
Willy Tarreau403bfbb2019-10-23 06:59:31 +0200141static THREAD_LOCAL struct lru64_head *pat_lru_tree;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200142static unsigned long long pat_lru_seed;
143
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100144/*
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100145 *
146 * The following functions are not exported and are used by internals process
147 * of pattern matching
148 *
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100149 */
150
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100151/* Background: Fast way to find a zero byte in a word
152 * http://graphics.stanford.edu/~seander/bithacks.html#ZeroInWord
153 * hasZeroByte = (v - 0x01010101UL) & ~v & 0x80808080UL;
154 *
155 * To look for 4 different byte values, xor the word with those bytes and
156 * then check for zero bytes:
157 *
158 * v = (((unsigned char)c * 0x1010101U) ^ delimiter)
159 * where <delimiter> is the 4 byte values to look for (as an uint)
160 * and <c> is the character that is being tested
161 */
162static inline unsigned int is_delimiter(unsigned char c, unsigned int mask)
163{
164 mask ^= (c * 0x01010101); /* propagate the char to all 4 bytes */
165 return (mask - 0x01010101) & ~mask & 0x80808080U;
166}
167
168static inline unsigned int make_4delim(unsigned char d1, unsigned char d2, unsigned char d3, unsigned char d4)
169{
170 return d1 << 24 | d2 << 16 | d3 << 8 | d4;
171}
172
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100173
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100174/*
175 *
176 * These functions are exported and may be used by any other component.
177 *
Willy Tarreau5def8ef2014-08-29 15:19:33 +0200178 * The following functions are used for parsing pattern matching input value.
179 * The <text> contain the string to be parsed. <pattern> must be a preallocated
180 * pattern. The pat_parse_* functions fill this structure with the parsed value.
181 * <err> is filled with an error message built with memprintf() function. It is
182 * allowed to use a trash as a temporary storage for the returned pattern, as
183 * the next call after these functions will be pat_idx_*.
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100184 *
Willy Tarreau5def8ef2014-08-29 15:19:33 +0200185 * In success case, the pat_parse_* function returns 1. If the function
186 * fails, it returns 0 and <err> is filled.
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100187 */
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100188
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100189/* ignore the current line */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200190int pat_parse_nothing(const char *text, struct pattern *pattern, int mflags, char **err)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100191{
192 return 1;
193}
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100194
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100195/* Parse a string. It is allocated and duplicated. */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200196int pat_parse_str(const char *text, struct pattern *pattern, int mflags, char **err)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100197{
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100198 pattern->type = SMP_T_STR;
Thierry FOURNIERedc15c32013-12-13 15:36:59 +0100199 pattern->ptr.str = (char *)text;
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100200 pattern->len = strlen(text);
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100201 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100202}
203
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100204/* Parse a binary written in hexa. It is allocated. */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200205int pat_parse_bin(const char *text, struct pattern *pattern, int mflags, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100206{
Willy Tarreau83061a82018-07-13 11:56:34 +0200207 struct buffer *trash;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100208
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100209 pattern->type = SMP_T_BIN;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100210 trash = get_trash_chunk();
211 pattern->len = trash->size;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200212 pattern->ptr.str = trash->area;
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100213 return !!parse_binary(text, &pattern->ptr.str, &pattern->len, err);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100214}
215
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100216/* Parse a regex. It is allocated. */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200217int pat_parse_reg(const char *text, struct pattern *pattern, int mflags, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100218{
Thierry FOURNIER0b6d15f2014-01-29 19:35:16 +0100219 pattern->ptr.str = (char *)text;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100220 return 1;
221}
222
223/* Parse a range of positive integers delimited by either ':' or '-'. If only
224 * one integer is read, it is set as both min and max. An operator may be
225 * specified as the prefix, among this list of 5 :
226 *
227 * 0:eq, 1:gt, 2:ge, 3:lt, 4:le
228 *
229 * The default operator is "eq". It supports range matching. Ranges are
230 * rejected for other operators. The operator may be changed at any time.
231 * The operator is stored in the 'opaque' argument.
232 *
233 * If err is non-NULL, an error message will be returned there on errors and
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100234 * the caller will have to free it. The function returns zero on error, and
235 * non-zero on success.
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100236 *
237 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200238int pat_parse_int(const char *text, struct pattern *pattern, int mflags, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100239{
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100240 const char *ptr = text;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100241
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200242 pattern->type = SMP_T_SINT;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100243
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100244 /* Empty string is not valid */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100245 if (!*text)
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100246 goto not_valid_range;
247
248 /* Search ':' or '-' separator. */
249 while (*ptr != '\0' && *ptr != ':' && *ptr != '-')
250 ptr++;
251
252 /* If separator not found. */
253 if (!*ptr) {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100254 if (strl2llrc(text, ptr - text, &pattern->val.range.min) != 0) {
255 memprintf(err, "'%s' is not a number", text);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100256 return 0;
257 }
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100258 pattern->val.range.max = pattern->val.range.min;
259 pattern->val.range.min_set = 1;
260 pattern->val.range.max_set = 1;
261 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100262 }
263
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100264 /* If the separator is the first character. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100265 if (ptr == text && *(ptr + 1) != '\0') {
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100266 if (strl2llrc(ptr + 1, strlen(ptr + 1), &pattern->val.range.max) != 0)
267 goto not_valid_range;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100268
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100269 pattern->val.range.min_set = 0;
270 pattern->val.range.max_set = 1;
271 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100272 }
273
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100274 /* If separator is the last character. */
275 if (*(ptr + 1) == '\0') {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100276 if (strl2llrc(text, ptr - text, &pattern->val.range.min) != 0)
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100277 goto not_valid_range;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100278
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100279 pattern->val.range.min_set = 1;
280 pattern->val.range.max_set = 0;
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100281 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100282 }
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100283
284 /* Else, parse two numbers. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100285 if (strl2llrc(text, ptr - text, &pattern->val.range.min) != 0)
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100286 goto not_valid_range;
287
288 if (strl2llrc(ptr + 1, strlen(ptr + 1), &pattern->val.range.max) != 0)
289 goto not_valid_range;
290
291 if (pattern->val.range.min > pattern->val.range.max)
292 goto not_valid_range;
293
294 pattern->val.range.min_set = 1;
295 pattern->val.range.max_set = 1;
296 return 1;
297
298 not_valid_range:
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100299 memprintf(err, "'%s' is not a valid number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100300 return 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100301}
302
303/* Parse a range of positive 2-component versions delimited by either ':' or
304 * '-'. The version consists in a major and a minor, both of which must be
305 * smaller than 65536, because internally they will be represented as a 32-bit
306 * integer.
307 * If only one version is read, it is set as both min and max. Just like for
308 * pure integers, an operator may be specified as the prefix, among this list
309 * of 5 :
310 *
311 * 0:eq, 1:gt, 2:ge, 3:lt, 4:le
312 *
313 * The default operator is "eq". It supports range matching. Ranges are
314 * rejected for other operators. The operator may be changed at any time.
315 * The operator is stored in the 'opaque' argument. This allows constructs
316 * such as the following one :
317 *
318 * acl obsolete_ssl ssl_req_proto lt 3
319 * acl unsupported_ssl ssl_req_proto gt 3.1
320 * acl valid_ssl ssl_req_proto 3.0-3.1
321 *
322 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200323int pat_parse_dotted_ver(const char *text, struct pattern *pattern, int mflags, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100324{
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100325 const char *ptr = text;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100326
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200327 pattern->type = SMP_T_SINT;
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100328
329 /* Search ':' or '-' separator. */
330 while (*ptr != '\0' && *ptr != ':' && *ptr != '-')
331 ptr++;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100332
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100333 /* If separator not found. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100334 if (*ptr == '\0' && ptr > text) {
335 if (strl2llrc_dotted(text, ptr-text, &pattern->val.range.min) != 0) {
336 memprintf(err, "'%s' is not a dotted number", text);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100337 return 0;
338 }
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100339 pattern->val.range.max = pattern->val.range.min;
340 pattern->val.range.min_set = 1;
341 pattern->val.range.max_set = 1;
342 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100343 }
344
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100345 /* If the separator is the first character. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100346 if (ptr == text && *(ptr+1) != '\0') {
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100347 if (strl2llrc_dotted(ptr+1, strlen(ptr+1), &pattern->val.range.max) != 0) {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100348 memprintf(err, "'%s' is not a valid dotted number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100349 return 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100350 }
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100351 pattern->val.range.min_set = 0;
352 pattern->val.range.max_set = 1;
353 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100354 }
355
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100356 /* If separator is the last character. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100357 if (ptr == &text[strlen(text)-1]) {
358 if (strl2llrc_dotted(text, ptr-text, &pattern->val.range.min) != 0) {
359 memprintf(err, "'%s' is not a valid dotted number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100360 return 0;
361 }
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100362 pattern->val.range.min_set = 1;
363 pattern->val.range.max_set = 0;
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100364 return 1;
365 }
366
367 /* Else, parse two numbers. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100368 if (strl2llrc_dotted(text, ptr-text, &pattern->val.range.min) != 0) {
369 memprintf(err, "'%s' is not a valid dotted number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100370 return 0;
371 }
372 if (strl2llrc_dotted(ptr+1, strlen(ptr+1), &pattern->val.range.max) != 0) {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100373 memprintf(err, "'%s' is not a valid dotted number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100374 return 0;
375 }
376 if (pattern->val.range.min > pattern->val.range.max) {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100377 memprintf(err, "'%s' is not a valid dotted number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100378 return 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100379 }
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100380 pattern->val.range.min_set = 1;
381 pattern->val.range.max_set = 1;
382 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100383}
384
385/* Parse an IP address and an optional mask in the form addr[/mask].
386 * The addr may either be an IPv4 address or a hostname. The mask
387 * may either be a dotted mask or a number of bits. Returns 1 if OK,
388 * otherwise 0. NOTE: IP address patterns are typed (IPV4/IPV6).
389 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200390int pat_parse_ip(const char *text, struct pattern *pattern, int mflags, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100391{
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200392 if (str2net(text, !(mflags & PAT_MF_NO_DNS) && (global.mode & MODE_STARTING),
Thierry FOURNIERfc7ac7b2014-02-11 15:23:04 +0100393 &pattern->val.ipv4.addr, &pattern->val.ipv4.mask)) {
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100394 pattern->type = SMP_T_IPV4;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100395 return 1;
396 }
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100397 else if (str62net(text, &pattern->val.ipv6.addr, &pattern->val.ipv6.mask)) {
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100398 pattern->type = SMP_T_IPV6;
399 return 1;
400 }
401 else {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100402 memprintf(err, "'%s' is not a valid IPv4 or IPv6 address", text);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100403 return 0;
404 }
405}
406
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100407/*
408 *
409 * These functions are exported and may be used by any other component.
410 *
Joseph Herlant4189d672018-11-15 10:22:31 -0800411 * This function just takes a sample <smp> and checks if this sample matches
412 * with the pattern <pattern>. This function returns only PAT_MATCH or
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100413 * PAT_NOMATCH.
414 *
415 */
416
417/* always return false */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100418struct pattern *pat_match_nothing(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100419{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200420 if (smp->data.u.sint) {
Thierry FOURNIERe5978bf2014-03-17 19:53:10 +0100421 if (fill) {
Thierry FOURNIER503bb092015-08-19 08:35:43 +0200422 static_pattern.data = NULL;
Thierry FOURNIERe5978bf2014-03-17 19:53:10 +0100423 static_pattern.ref = NULL;
Thierry FOURNIERe5978bf2014-03-17 19:53:10 +0100424 static_pattern.type = 0;
425 static_pattern.ptr.str = NULL;
426 }
427 return &static_pattern;
428 }
429 else
430 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100431}
432
433
Joseph Herlant4189d672018-11-15 10:22:31 -0800434/* NB: For two strings to be identical, it is required that their length match */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100435struct pattern *pat_match_str(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100436{
437 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100438 struct ebmb_node *node;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100439 struct pattern_tree *elt;
440 struct pattern_list *lst;
441 struct pattern *pattern;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200442 struct pattern *ret = NULL;
443 struct lru64 *lru = NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100444
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100445 /* Lookup a string in the expression's pattern tree. */
446 if (!eb_is_empty(&expr->pattern_tree)) {
Christopher Fauletb4cf7ab2020-06-30 18:52:32 +0200447 char prev = 0;
448
449 if (smp->data.u.str.data < smp->data.u.str.size) {
450 /* we may have to force a trailing zero on the test pattern and
451 * the buffer is large enough to accommodate it.
452 */
453 prev = smp->data.u.str.area[smp->data.u.str.data];
454 if (prev)
455 smp->data.u.str.area[smp->data.u.str.data] = '\0';
456 }
457 else {
458 /* Otherwise, the sample is duplicated. A trailing zero
459 * is automatically added to the string.
460 */
461 if (!smp_dup(smp))
462 return NULL;
463 }
464
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200465 node = ebst_lookup(&expr->pattern_tree, smp->data.u.str.area);
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100466 if (prev)
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200467 smp->data.u.str.area[smp->data.u.str.data] = prev;
Willy Tarreauc93da692020-10-29 09:41:34 +0100468
469 while (node) {
470 elt = ebmb_entry(node, struct pattern_tree, node);
471 if (elt->ref->gen_id != expr->ref->curr_gen) {
472 node = ebmb_next(node);
473 continue;
474 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100475 if (fill) {
Thierry FOURNIER503bb092015-08-19 08:35:43 +0200476 static_pattern.data = elt->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +0100477 static_pattern.ref = elt->ref;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200478 static_pattern.sflags = PAT_SF_TREE;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100479 static_pattern.type = SMP_T_STR;
480 static_pattern.ptr.str = (char *)elt->node.key;
481 }
482 return &static_pattern;
483 }
484 }
485
486 /* look in the list */
Willy Tarreauf3045d22015-04-29 16:24:50 +0200487 if (pat_lru_tree) {
Willy Tarreauaee93142015-05-04 17:18:42 +0200488 unsigned long long seed = pat_lru_seed ^ (long)expr;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200489
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200490 lru = lru64_get(XXH64(smp->data.u.str.area, smp->data.u.str.data, seed),
Willy Tarreau3ee0de12020-11-02 15:26:51 +0100491 pat_lru_tree, expr, expr->ref->revision);
Willy Tarreau403bfbb2019-10-23 06:59:31 +0200492 if (lru && lru->domain) {
Emeric Brunb5997f72017-07-03 11:34:05 +0200493 ret = lru->data;
Emeric Brunb5997f72017-07-03 11:34:05 +0200494 return ret;
495 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200496 }
497
Emeric Brunb5997f72017-07-03 11:34:05 +0200498
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100499 list_for_each_entry(lst, &expr->patterns, list) {
500 pattern = &lst->pat;
501
Willy Tarreauc93da692020-10-29 09:41:34 +0100502 if (pattern->ref->gen_id != expr->ref->curr_gen)
503 continue;
504
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200505 if (pattern->len != smp->data.u.str.data)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100506 continue;
507
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200508 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200509 if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.str.area, smp->data.u.str.data) == 0) ||
510 (!icase && strncmp(pattern->ptr.str, smp->data.u.str.area, smp->data.u.str.data) == 0)) {
Willy Tarreauf3045d22015-04-29 16:24:50 +0200511 ret = pattern;
512 break;
513 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100514 }
515
Willy Tarreau403bfbb2019-10-23 06:59:31 +0200516 if (lru)
Willy Tarreau3ee0de12020-11-02 15:26:51 +0100517 lru64_commit(lru, ret, expr, expr->ref->revision, NULL);
Willy Tarreauf3045d22015-04-29 16:24:50 +0200518
519 return ret;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100520}
521
522/* NB: For two binaries buf to be identical, it is required that their lengths match */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100523struct pattern *pat_match_bin(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100524{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100525 struct pattern_list *lst;
526 struct pattern *pattern;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200527 struct pattern *ret = NULL;
528 struct lru64 *lru = NULL;
529
530 if (pat_lru_tree) {
Willy Tarreauaee93142015-05-04 17:18:42 +0200531 unsigned long long seed = pat_lru_seed ^ (long)expr;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100532
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200533 lru = lru64_get(XXH64(smp->data.u.str.area, smp->data.u.str.data, seed),
Willy Tarreau3ee0de12020-11-02 15:26:51 +0100534 pat_lru_tree, expr, expr->ref->revision);
Willy Tarreau403bfbb2019-10-23 06:59:31 +0200535 if (lru && lru->domain) {
Emeric Brunb5997f72017-07-03 11:34:05 +0200536 ret = lru->data;
Emeric Brunb5997f72017-07-03 11:34:05 +0200537 return ret;
538 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200539 }
540
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100541 list_for_each_entry(lst, &expr->patterns, list) {
542 pattern = &lst->pat;
543
Willy Tarreauc93da692020-10-29 09:41:34 +0100544 if (pattern->ref->gen_id != expr->ref->curr_gen)
545 continue;
546
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200547 if (pattern->len != smp->data.u.str.data)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100548 continue;
549
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200550 if (memcmp(pattern->ptr.str, smp->data.u.str.area, smp->data.u.str.data) == 0) {
Willy Tarreauf3045d22015-04-29 16:24:50 +0200551 ret = pattern;
552 break;
553 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100554 }
555
Willy Tarreau403bfbb2019-10-23 06:59:31 +0200556 if (lru)
Willy Tarreau3ee0de12020-11-02 15:26:51 +0100557 lru64_commit(lru, ret, expr, expr->ref->revision, NULL);
Willy Tarreauf3045d22015-04-29 16:24:50 +0200558
559 return ret;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100560}
561
562/* Executes a regex. It temporarily changes the data to add a trailing zero,
Thierry Fournier8feaa662016-02-10 22:55:20 +0100563 * and restores the previous character when leaving. This function fills
564 * a matching array.
565 */
566struct pattern *pat_match_regm(struct sample *smp, struct pattern_expr *expr, int fill)
567{
568 struct pattern_list *lst;
569 struct pattern *pattern;
570 struct pattern *ret = NULL;
571
572 list_for_each_entry(lst, &expr->patterns, list) {
573 pattern = &lst->pat;
574
Willy Tarreauc93da692020-10-29 09:41:34 +0100575 if (pattern->ref->gen_id != expr->ref->curr_gen)
576 continue;
577
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200578 if (regex_exec_match2(pattern->ptr.reg, smp->data.u.str.area, smp->data.u.str.data,
Thierry Fournier8feaa662016-02-10 22:55:20 +0100579 MAX_MATCH, pmatch, 0)) {
580 ret = pattern;
581 smp->ctx.a[0] = pmatch;
582 break;
583 }
584 }
585
586 return ret;
587}
588
589/* Executes a regex. It temporarily changes the data to add a trailing zero,
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100590 * and restores the previous character when leaving.
591 */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100592struct pattern *pat_match_reg(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100593{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100594 struct pattern_list *lst;
595 struct pattern *pattern;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200596 struct pattern *ret = NULL;
597 struct lru64 *lru = NULL;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100598
Willy Tarreauf3045d22015-04-29 16:24:50 +0200599 if (pat_lru_tree) {
Willy Tarreauaee93142015-05-04 17:18:42 +0200600 unsigned long long seed = pat_lru_seed ^ (long)expr;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200601
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200602 lru = lru64_get(XXH64(smp->data.u.str.area, smp->data.u.str.data, seed),
Willy Tarreau3ee0de12020-11-02 15:26:51 +0100603 pat_lru_tree, expr, expr->ref->revision);
Willy Tarreau403bfbb2019-10-23 06:59:31 +0200604 if (lru && lru->domain) {
Emeric Brunb5997f72017-07-03 11:34:05 +0200605 ret = lru->data;
Emeric Brunb5997f72017-07-03 11:34:05 +0200606 return ret;
607 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200608 }
609
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100610 list_for_each_entry(lst, &expr->patterns, list) {
611 pattern = &lst->pat;
612
Willy Tarreauc93da692020-10-29 09:41:34 +0100613 if (pattern->ref->gen_id != expr->ref->curr_gen)
614 continue;
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
Willy Tarreauc93da692020-10-29 09:41:34 +0100664 while (node) {
665 elt = ebmb_entry(node, struct pattern_tree, node);
666 if (elt->ref->gen_id != expr->ref->curr_gen) {
667 node = ebmb_next(node);
668 continue;
669 }
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +0200670 if (fill) {
Thierry FOURNIER503bb092015-08-19 08:35:43 +0200671 static_pattern.data = elt->data;
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +0200672 static_pattern.ref = elt->ref;
673 static_pattern.sflags = PAT_SF_TREE;
674 static_pattern.type = SMP_T_STR;
675 static_pattern.ptr.str = (char *)elt->node.key;
676 }
677 return &static_pattern;
678 }
679 }
680
681 /* look in the list */
Willy Tarreauf3045d22015-04-29 16:24:50 +0200682 if (pat_lru_tree) {
Willy Tarreauaee93142015-05-04 17:18:42 +0200683 unsigned long long seed = pat_lru_seed ^ (long)expr;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200684
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200685 lru = lru64_get(XXH64(smp->data.u.str.area, smp->data.u.str.data, seed),
Willy Tarreau3ee0de12020-11-02 15:26:51 +0100686 pat_lru_tree, expr, expr->ref->revision);
Willy Tarreau403bfbb2019-10-23 06:59:31 +0200687 if (lru && lru->domain) {
Emeric Brunb5997f72017-07-03 11:34:05 +0200688 ret = lru->data;
Emeric Brunb5997f72017-07-03 11:34:05 +0200689 return ret;
690 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200691 }
692
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100693 list_for_each_entry(lst, &expr->patterns, list) {
694 pattern = &lst->pat;
695
Willy Tarreauc93da692020-10-29 09:41:34 +0100696 if (pattern->ref->gen_id != expr->ref->curr_gen)
697 continue;
698
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200699 if (pattern->len > smp->data.u.str.data)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100700 continue;
701
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200702 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200703 if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.str.area, pattern->len) != 0) ||
704 (!icase && strncmp(pattern->ptr.str, smp->data.u.str.area, pattern->len) != 0))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100705 continue;
706
Willy Tarreauf3045d22015-04-29 16:24:50 +0200707 ret = pattern;
708 break;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100709 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200710
Willy Tarreau403bfbb2019-10-23 06:59:31 +0200711 if (lru)
Willy Tarreau3ee0de12020-11-02 15:26:51 +0100712 lru64_commit(lru, ret, expr, expr->ref->revision, NULL);
Willy Tarreauf3045d22015-04-29 16:24:50 +0200713
714 return ret;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100715}
716
717/* Checks that the pattern matches the end of the tested string. */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100718struct pattern *pat_match_end(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100719{
720 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100721 struct pattern_list *lst;
722 struct pattern *pattern;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200723 struct pattern *ret = NULL;
724 struct lru64 *lru = NULL;
725
726 if (pat_lru_tree) {
Willy Tarreauaee93142015-05-04 17:18:42 +0200727 unsigned long long seed = pat_lru_seed ^ (long)expr;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200728
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200729 lru = lru64_get(XXH64(smp->data.u.str.area, smp->data.u.str.data, seed),
Willy Tarreau3ee0de12020-11-02 15:26:51 +0100730 pat_lru_tree, expr, expr->ref->revision);
Willy Tarreau403bfbb2019-10-23 06:59:31 +0200731 if (lru && lru->domain) {
Emeric Brunb5997f72017-07-03 11:34:05 +0200732 ret = lru->data;
Emeric Brunb5997f72017-07-03 11:34:05 +0200733 return ret;
734 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200735 }
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100736
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100737 list_for_each_entry(lst, &expr->patterns, list) {
738 pattern = &lst->pat;
739
Willy Tarreauc93da692020-10-29 09:41:34 +0100740 if (pattern->ref->gen_id != expr->ref->curr_gen)
741 continue;
742
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200743 if (pattern->len > smp->data.u.str.data)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100744 continue;
745
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200746 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200747 if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.str.area + smp->data.u.str.data - pattern->len, pattern->len) != 0) ||
748 (!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 +0100749 continue;
750
Willy Tarreauf3045d22015-04-29 16:24:50 +0200751 ret = pattern;
752 break;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100753 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200754
Willy Tarreau403bfbb2019-10-23 06:59:31 +0200755 if (lru)
Willy Tarreau3ee0de12020-11-02 15:26:51 +0100756 lru64_commit(lru, ret, expr, expr->ref->revision, NULL);
Willy Tarreauf3045d22015-04-29 16:24:50 +0200757
758 return ret;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100759}
760
761/* Checks that the pattern is included inside the tested string.
762 * NB: Suboptimal, should be rewritten using a Boyer-Moore method.
763 */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100764struct pattern *pat_match_sub(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100765{
766 int icase;
767 char *end;
768 char *c;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100769 struct pattern_list *lst;
770 struct pattern *pattern;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200771 struct pattern *ret = NULL;
772 struct lru64 *lru = NULL;
773
774 if (pat_lru_tree) {
Willy Tarreauaee93142015-05-04 17:18:42 +0200775 unsigned long long seed = pat_lru_seed ^ (long)expr;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200776
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200777 lru = lru64_get(XXH64(smp->data.u.str.area, smp->data.u.str.data, seed),
Willy Tarreau3ee0de12020-11-02 15:26:51 +0100778 pat_lru_tree, expr, expr->ref->revision);
Willy Tarreau403bfbb2019-10-23 06:59:31 +0200779 if (lru && lru->domain) {
Emeric Brunb5997f72017-07-03 11:34:05 +0200780 ret = lru->data;
Emeric Brunb5997f72017-07-03 11:34:05 +0200781 return ret;
782 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200783 }
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100784
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100785 list_for_each_entry(lst, &expr->patterns, list) {
786 pattern = &lst->pat;
787
Willy Tarreauc93da692020-10-29 09:41:34 +0100788 if (pattern->ref->gen_id != expr->ref->curr_gen)
789 continue;
790
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200791 if (pattern->len > smp->data.u.str.data)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100792 continue;
793
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200794 end = smp->data.u.str.area + smp->data.u.str.data - pattern->len;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200795 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100796 if (icase) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200797 for (c = smp->data.u.str.area; c <= end; c++) {
Willy Tarreauf278eec2020-07-05 21:46:32 +0200798 if (tolower((unsigned char)*c) != tolower((unsigned char)*pattern->ptr.str))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100799 continue;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200800 if (strncasecmp(pattern->ptr.str, c, pattern->len) == 0) {
801 ret = pattern;
802 goto leave;
803 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100804 }
805 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200806 for (c = smp->data.u.str.area; c <= end; c++) {
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100807 if (*c != *pattern->ptr.str)
808 continue;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200809 if (strncmp(pattern->ptr.str, c, pattern->len) == 0) {
810 ret = pattern;
811 goto leave;
812 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100813 }
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100814 }
815 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200816 leave:
Willy Tarreau403bfbb2019-10-23 06:59:31 +0200817 if (lru)
Willy Tarreau3ee0de12020-11-02 15:26:51 +0100818 lru64_commit(lru, ret, expr, expr->ref->revision, NULL);
Willy Tarreauf3045d22015-04-29 16:24:50 +0200819
820 return ret;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100821}
822
823/* This one is used by other real functions. It checks that the pattern is
824 * included inside the tested string, but enclosed between the specified
825 * delimiters or at the beginning or end of the string. The delimiters are
826 * provided as an unsigned int made by make_4delim() and match up to 4 different
827 * delimiters. Delimiters are stripped at the beginning and end of the pattern.
828 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200829static int match_word(struct sample *smp, struct pattern *pattern, int mflags, unsigned int delimiters)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100830{
831 int may_match, icase;
832 char *c, *end;
833 char *ps;
834 int pl;
835
836 pl = pattern->len;
837 ps = pattern->ptr.str;
838
839 while (pl > 0 && is_delimiter(*ps, delimiters)) {
840 pl--;
841 ps++;
842 }
843
844 while (pl > 0 && is_delimiter(ps[pl - 1], delimiters))
845 pl--;
846
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200847 if (pl > smp->data.u.str.data)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100848 return PAT_NOMATCH;
849
850 may_match = 1;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200851 icase = mflags & PAT_MF_IGNORE_CASE;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200852 end = smp->data.u.str.area + smp->data.u.str.data - pl;
853 for (c = smp->data.u.str.area; c <= end; c++) {
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100854 if (is_delimiter(*c, delimiters)) {
855 may_match = 1;
856 continue;
857 }
858
859 if (!may_match)
860 continue;
861
862 if (icase) {
Willy Tarreauf278eec2020-07-05 21:46:32 +0200863 if ((tolower((unsigned char)*c) == tolower((unsigned char)*ps)) &&
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100864 (strncasecmp(ps, c, pl) == 0) &&
865 (c == end || is_delimiter(c[pl], delimiters)))
866 return PAT_MATCH;
867 } else {
868 if ((*c == *ps) &&
869 (strncmp(ps, c, pl) == 0) &&
870 (c == end || is_delimiter(c[pl], delimiters)))
871 return PAT_MATCH;
872 }
873 may_match = 0;
874 }
875 return PAT_NOMATCH;
876}
877
878/* Checks that the pattern is included inside the tested string, but enclosed
879 * between the delimiters '?' or '/' or at the beginning or end of the string.
880 * Delimiters at the beginning or end of the pattern are ignored.
881 */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100882struct pattern *pat_match_dir(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100883{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100884 struct pattern_list *lst;
885 struct pattern *pattern;
886
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100887 list_for_each_entry(lst, &expr->patterns, list) {
888 pattern = &lst->pat;
Willy Tarreauc93da692020-10-29 09:41:34 +0100889
890 if (pattern->ref->gen_id != expr->ref->curr_gen)
891 continue;
892
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 pattern is included inside the tested string, but enclosed
900 * between the delmiters '/', '?', '.' or ":" or at the beginning or end of
901 * the string. Delimiters at the beginning or end of the pattern are ignored.
902 */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100903struct pattern *pat_match_dom(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100904{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100905 struct pattern_list *lst;
906 struct pattern *pattern;
907
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100908 list_for_each_entry(lst, &expr->patterns, list) {
909 pattern = &lst->pat;
Willy Tarreauc93da692020-10-29 09:41:34 +0100910
911 if (pattern->ref->gen_id != expr->ref->curr_gen)
912 continue;
913
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200914 if (match_word(smp, pattern, expr->mflags, make_4delim('/', '?', '.', ':')))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100915 return pattern;
916 }
917 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100918}
919
920/* Checks that the integer in <test> is included between min and max */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100921struct pattern *pat_match_int(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100922{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100923 struct pattern_list *lst;
924 struct pattern *pattern;
925
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100926 list_for_each_entry(lst, &expr->patterns, list) {
927 pattern = &lst->pat;
Willy Tarreauc93da692020-10-29 09:41:34 +0100928
929 if (pattern->ref->gen_id != expr->ref->curr_gen)
930 continue;
931
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200932 if ((!pattern->val.range.min_set || pattern->val.range.min <= smp->data.u.sint) &&
933 (!pattern->val.range.max_set || smp->data.u.sint <= pattern->val.range.max))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100934 return pattern;
935 }
936 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100937}
938
939/* Checks that the length of the pattern in <test> is included between min and max */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100940struct pattern *pat_match_len(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100941{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100942 struct pattern_list *lst;
943 struct pattern *pattern;
944
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100945 list_for_each_entry(lst, &expr->patterns, list) {
946 pattern = &lst->pat;
Willy Tarreauc93da692020-10-29 09:41:34 +0100947
948 if (pattern->ref->gen_id != expr->ref->curr_gen)
949 continue;
950
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200951 if ((!pattern->val.range.min_set || pattern->val.range.min <= smp->data.u.str.data) &&
952 (!pattern->val.range.max_set || smp->data.u.str.data <= pattern->val.range.max))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100953 return pattern;
954 }
955 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100956}
957
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100958struct pattern *pat_match_ip(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100959{
960 unsigned int v4; /* in network byte order */
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100961 struct in6_addr tmp6;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100962 struct in_addr *s;
963 struct ebmb_node *node;
964 struct pattern_tree *elt;
965 struct pattern_list *lst;
966 struct pattern *pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100967
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100968 /* The input sample is IPv4. Try to match in the trees. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200969 if (smp->data.type == SMP_T_IPV4) {
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100970 /* Lookup an IPv4 address in the expression's pattern tree using
971 * the longest match method.
972 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200973 s = &smp->data.u.ipv4;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100974 node = ebmb_lookup_longest(&expr->pattern_tree, &s->s_addr);
Willy Tarreauc93da692020-10-29 09:41:34 +0100975 while (node) {
976 elt = ebmb_entry(node, struct pattern_tree, node);
977 if (elt->ref->gen_id != expr->ref->curr_gen) {
978 node = ebmb_next(node);
979 continue;
980 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100981 if (fill) {
Thierry FOURNIER503bb092015-08-19 08:35:43 +0200982 static_pattern.data = elt->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +0100983 static_pattern.ref = elt->ref;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200984 static_pattern.sflags = PAT_SF_TREE;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100985 static_pattern.type = SMP_T_IPV4;
Willy Tarreau296cfd12020-02-25 09:58:41 +0100986 static_pattern.val.ipv4.addr.s_addr = read_u32(elt->node.key);
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100987 if (!cidr2dotted(elt->node.node.pfx, &static_pattern.val.ipv4.mask))
988 return NULL;
989 }
990 return &static_pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100991 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100992
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100993 /* The IPv4 sample dont match the IPv4 tree. Convert the IPv4
994 * sample address to IPv6 with the mapping method using the ::ffff:
995 * prefix, and try to lookup in the IPv6 tree.
996 */
997 memset(&tmp6, 0, 10);
Willy Tarreau296cfd12020-02-25 09:58:41 +0100998 write_u16(&tmp6.s6_addr[10], htons(0xffff));
999 write_u32(&tmp6.s6_addr[12], smp->data.u.ipv4.s_addr);
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001000 node = ebmb_lookup_longest(&expr->pattern_tree_2, &tmp6);
Willy Tarreauc93da692020-10-29 09:41:34 +01001001 while (node) {
1002 elt = ebmb_entry(node, struct pattern_tree, node);
1003 if (elt->ref->gen_id != expr->ref->curr_gen) {
1004 node = ebmb_next(node);
1005 continue;
1006 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001007 if (fill) {
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001008 static_pattern.data = elt->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01001009 static_pattern.ref = elt->ref;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02001010 static_pattern.sflags = PAT_SF_TREE;
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001011 static_pattern.type = SMP_T_IPV6;
Willy Tarreau296cfd12020-02-25 09:58:41 +01001012 memcpy(&static_pattern.val.ipv6.addr, elt->node.key, 16);
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001013 static_pattern.val.ipv6.mask = elt->node.node.pfx;
1014 }
1015 return &static_pattern;
1016 }
1017 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01001018
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001019 /* The input sample is IPv6. Try to match in the trees. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001020 if (smp->data.type == SMP_T_IPV6) {
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001021 /* Lookup an IPv6 address in the expression's pattern tree using
1022 * the longest match method.
1023 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001024 node = ebmb_lookup_longest(&expr->pattern_tree_2, &smp->data.u.ipv6);
Willy Tarreauc93da692020-10-29 09:41:34 +01001025 while (node) {
1026 elt = ebmb_entry(node, struct pattern_tree, node);
1027 if (elt->ref->gen_id != expr->ref->curr_gen) {
1028 node = ebmb_next(node);
1029 continue;
1030 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001031 if (fill) {
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001032 static_pattern.data = elt->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01001033 static_pattern.ref = elt->ref;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02001034 static_pattern.sflags = PAT_SF_TREE;
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001035 static_pattern.type = SMP_T_IPV6;
Willy Tarreau296cfd12020-02-25 09:58:41 +01001036 memcpy(&static_pattern.val.ipv6.addr, elt->node.key, 16);
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001037 static_pattern.val.ipv6.mask = elt->node.node.pfx;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +01001038 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001039 return &static_pattern;
1040 }
1041
1042 /* Try to convert 6 to 4 when the start of the ipv6 address match the
1043 * following forms :
1044 * - ::ffff:ip:v4 (ipv4 mapped)
1045 * - ::0000:ip:v4 (old ipv4 mapped)
1046 * - 2002:ip:v4:: (6to4)
1047 */
Willy Tarreau296cfd12020-02-25 09:58:41 +01001048 if ((read_u64(&smp->data.u.ipv6.s6_addr[0]) == 0 &&
1049 (read_u32(&smp->data.u.ipv6.s6_addr[8]) == 0 ||
1050 read_u32(&smp->data.u.ipv6.s6_addr[8]) == htonl(0xFFFF))) ||
1051 read_u16(&smp->data.u.ipv6.s6_addr[0]) == htons(0x2002)) {
1052 if (read_u32(&smp->data.u.ipv6.s6_addr[0]) == 0)
1053 v4 = read_u32(&smp->data.u.ipv6.s6_addr[12]);
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001054 else
Willy Tarreau296cfd12020-02-25 09:58:41 +01001055 v4 = htonl((ntohs(read_u16(&smp->data.u.ipv6.s6_addr[2])) << 16) +
1056 ntohs(read_u16(&smp->data.u.ipv6.s6_addr[4])));
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001057
1058 /* Lookup an IPv4 address in the expression's pattern tree using the longest
1059 * match method.
1060 */
1061 node = ebmb_lookup_longest(&expr->pattern_tree, &v4);
Willy Tarreauc93da692020-10-29 09:41:34 +01001062 while (node) {
1063 elt = ebmb_entry(node, struct pattern_tree, node);
1064 if (elt->ref->gen_id != expr->ref->curr_gen) {
1065 node = ebmb_next(node);
1066 continue;
1067 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001068 if (fill) {
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001069 static_pattern.data = elt->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01001070 static_pattern.ref = elt->ref;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02001071 static_pattern.sflags = PAT_SF_TREE;
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001072 static_pattern.type = SMP_T_IPV4;
Willy Tarreau296cfd12020-02-25 09:58:41 +01001073 static_pattern.val.ipv4.addr.s_addr = read_u32(elt->node.key);
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001074 if (!cidr2dotted(elt->node.node.pfx, &static_pattern.val.ipv4.mask))
1075 return NULL;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01001076 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001077 return &static_pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +01001078 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001079 }
1080 }
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +01001081
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001082 /* Lookup in the list. the list contain only IPv4 patterns */
1083 list_for_each_entry(lst, &expr->patterns, list) {
1084 pattern = &lst->pat;
1085
Willy Tarreauc93da692020-10-29 09:41:34 +01001086 if (pattern->ref->gen_id != expr->ref->curr_gen)
1087 continue;
1088
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001089 /* The input sample is IPv4, use it as is. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001090 if (smp->data.type == SMP_T_IPV4) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001091 v4 = smp->data.u.ipv4.s_addr;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +01001092 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001093 else if (smp->data.type == SMP_T_IPV6) {
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001094 /* v4 match on a V6 sample. We want to check at least for
1095 * the following forms :
1096 * - ::ffff:ip:v4 (ipv4 mapped)
1097 * - ::0000:ip:v4 (old ipv4 mapped)
1098 * - 2002:ip:v4:: (6to4)
1099 */
Willy Tarreau296cfd12020-02-25 09:58:41 +01001100 if (read_u64(&smp->data.u.ipv6.s6_addr[0]) == 0 &&
1101 (read_u32(&smp->data.u.ipv6.s6_addr[8]) == 0 ||
1102 read_u32(&smp->data.u.ipv6.s6_addr[8]) == htonl(0xFFFF))) {
1103 v4 = read_u32(&smp->data.u.ipv6.s6_addr[12]);
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01001104 }
Willy Tarreau296cfd12020-02-25 09:58:41 +01001105 else if (read_u16(&smp->data.u.ipv6.s6_addr[0]) == htons(0x2002)) {
1106 v4 = htonl((ntohs(read_u16(&smp->data.u.ipv6.s6_addr[2])) << 16) +
1107 ntohs(read_u16(&smp->data.u.ipv6.s6_addr[4])));
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01001108 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001109 else
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01001110 continue;
Andreas Seltenreichf0653192016-03-03 20:08:35 +01001111 } else {
1112 /* impossible */
1113 continue;
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001114 }
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +01001115
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001116 /* Check if the input sample match the current pattern. */
1117 if (((v4 ^ pattern->val.ipv4.addr.s_addr) & pattern->val.ipv4.mask.s_addr) == 0)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01001118 return pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +01001119 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01001120 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +01001121}
1122
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001123void free_pattern_tree(struct eb_root *root)
1124{
1125 struct eb_node *node, *next;
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +01001126 struct pattern_tree *elt;
Thierry FOURNIER3ce88c72013-12-09 11:29:46 +01001127
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001128 node = eb_first(root);
1129 while (node) {
1130 next = eb_next(node);
1131 eb_delete(node);
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +01001132 elt = container_of(node, struct pattern_tree, node);
Willy Tarreau4bdd0a12020-11-02 12:10:48 +01001133 LIST_DEL(&elt->from_ref);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001134 free(elt->data);
Thierry FOURNIER3ce88c72013-12-09 11:29:46 +01001135 free(elt);
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001136 node = next;
1137 }
1138}
1139
Willy Tarreau6d8a6892020-11-02 19:26:02 +01001140void pat_prune_gen(struct pattern_expr *expr)
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +01001141{
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01001142 struct pattern_list *pat, *tmp;
1143
1144 list_for_each_entry_safe(pat, tmp, &expr->patterns, list) {
Christopher Faulet6cfc8512020-09-09 16:09:44 +02001145 LIST_DEL(&pat->list);
Willy Tarreau4bdd0a12020-11-02 12:10:48 +01001146 LIST_DEL(&pat->from_ref);
Willy Tarreau6d8a6892020-11-02 19:26:02 +01001147 if (pat->pat.sflags & PAT_SF_REGFREE)
1148 regex_free(pat->pat.ptr.ptr);
1149 else
1150 free(pat->pat.ptr.ptr);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001151 free(pat->pat.data);
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01001152 free(pat);
1153 }
1154
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +01001155 free_pattern_tree(&expr->pattern_tree);
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001156 free_pattern_tree(&expr->pattern_tree_2);
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +01001157 LIST_INIT(&expr->patterns);
Willy Tarreau3ee0de12020-11-02 15:26:51 +01001158 expr->ref->revision = rdtsc();
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +01001159}
1160
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001161/*
1162 *
1163 * The following functions are used for the pattern indexation
1164 *
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001165 */
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001166
1167int pat_idx_list_val(struct pattern_expr *expr, struct pattern *pat, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001168{
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001169 struct pattern_list *patl;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001170
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001171 /* allocate pattern */
1172 patl = calloc(1, sizeof(*patl));
1173 if (!patl) {
1174 memprintf(err, "out of memory while indexing pattern");
Thierry FOURNIER972028f2014-01-23 17:53:31 +01001175 return 0;
1176 }
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001177
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001178 /* duplicate pattern */
1179 memcpy(&patl->pat, pat, sizeof(*pat));
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001180
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001181 /* chain pattern in the expression */
1182 LIST_ADDQ(&expr->patterns, &patl->list);
Willy Tarreau4bdd0a12020-11-02 12:10:48 +01001183 /* and from the reference */
1184 LIST_ADDQ(&pat->ref->list_head, &patl->from_ref);
Willy Tarreau3ee0de12020-11-02 15:26:51 +01001185 expr->ref->revision = rdtsc();
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001186
1187 /* that's ok */
1188 return 1;
1189}
1190
1191int pat_idx_list_ptr(struct pattern_expr *expr, struct pattern *pat, char **err)
1192{
1193 struct pattern_list *patl;
1194
1195 /* allocate pattern */
1196 patl = calloc(1, sizeof(*patl));
Thierry FOURNIER8aa83842015-02-06 17:50:55 +01001197 if (!patl) {
1198 memprintf(err, "out of memory while indexing pattern");
Thierry FOURNIER972028f2014-01-23 17:53:31 +01001199 return 0;
Thierry FOURNIER8aa83842015-02-06 17:50:55 +01001200 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001201
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001202 /* duplicate pattern */
1203 memcpy(&patl->pat, pat, sizeof(*pat));
1204 patl->pat.ptr.ptr = malloc(patl->pat.len);
1205 if (!patl->pat.ptr.ptr) {
1206 free(patl);
1207 memprintf(err, "out of memory while indexing pattern");
1208 return 0;
1209 }
1210 memcpy(patl->pat.ptr.ptr, pat->ptr.ptr, pat->len);
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001211
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001212 /* chain pattern in the expression */
1213 LIST_ADDQ(&expr->patterns, &patl->list);
Willy Tarreau4bdd0a12020-11-02 12:10:48 +01001214 /* and from the reference */
1215 LIST_ADDQ(&pat->ref->list_head, &patl->from_ref);
Willy Tarreau3ee0de12020-11-02 15:26:51 +01001216 expr->ref->revision = rdtsc();
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001217
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001218 /* that's ok */
1219 return 1;
1220}
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001221
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001222int pat_idx_list_str(struct pattern_expr *expr, struct pattern *pat, char **err)
1223{
1224 struct pattern_list *patl;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001225
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001226 /* allocate pattern */
1227 patl = calloc(1, sizeof(*patl));
1228 if (!patl) {
1229 memprintf(err, "out of memory while indexing pattern");
1230 return 0;
1231 }
1232
1233 /* duplicate pattern */
1234 memcpy(&patl->pat, pat, sizeof(*pat));
1235 patl->pat.ptr.str = malloc(patl->pat.len + 1);
1236 if (!patl->pat.ptr.str) {
1237 free(patl);
1238 memprintf(err, "out of memory while indexing pattern");
1239 return 0;
1240 }
1241 memcpy(patl->pat.ptr.ptr, pat->ptr.ptr, pat->len);
1242 patl->pat.ptr.str[patl->pat.len] = '\0';
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001243
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001244 /* chain pattern in the expression */
1245 LIST_ADDQ(&expr->patterns, &patl->list);
Willy Tarreau4bdd0a12020-11-02 12:10:48 +01001246 /* and from the reference */
1247 LIST_ADDQ(&pat->ref->list_head, &patl->from_ref);
Willy Tarreau3ee0de12020-11-02 15:26:51 +01001248 expr->ref->revision = rdtsc();
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001249
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001250 /* that's ok */
1251 return 1;
1252}
1253
Thierry Fournier8feaa662016-02-10 22:55:20 +01001254int pat_idx_list_reg_cap(struct pattern_expr *expr, struct pattern *pat, int cap, char **err)
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001255{
1256 struct pattern_list *patl;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001257
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001258 /* allocate pattern */
1259 patl = calloc(1, sizeof(*patl));
1260 if (!patl) {
1261 memprintf(err, "out of memory while indexing pattern");
1262 return 0;
Thierry FOURNIER972028f2014-01-23 17:53:31 +01001263 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001264
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001265 /* duplicate pattern */
1266 memcpy(&patl->pat, pat, sizeof(*pat));
1267
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001268 /* compile regex */
Willy Tarreau9b5c8bb2020-11-02 19:16:23 +01001269 patl->pat.sflags |= PAT_SF_REGFREE;
Dragan Dosen26743032019-04-30 15:54:36 +02001270 if (!(patl->pat.ptr.reg = regex_comp(pat->ptr.str, !(expr->mflags & PAT_MF_IGNORE_CASE),
1271 cap, err))) {
Dirkjan Bussink07fcaaa2014-04-28 22:57:16 +00001272 free(patl);
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001273 return 0;
1274 }
1275
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001276 /* chain pattern in the expression */
1277 LIST_ADDQ(&expr->patterns, &patl->list);
Willy Tarreau4bdd0a12020-11-02 12:10:48 +01001278 /* and from the reference */
1279 LIST_ADDQ(&pat->ref->list_head, &patl->from_ref);
Willy Tarreau3ee0de12020-11-02 15:26:51 +01001280 expr->ref->revision = rdtsc();
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001281
1282 /* that's ok */
1283 return 1;
1284}
1285
Thierry Fournier8feaa662016-02-10 22:55:20 +01001286int pat_idx_list_reg(struct pattern_expr *expr, struct pattern *pat, char **err)
1287{
1288 return pat_idx_list_reg_cap(expr, pat, 0, err);
1289}
1290
1291int pat_idx_list_regm(struct pattern_expr *expr, struct pattern *pat, char **err)
1292{
1293 return pat_idx_list_reg_cap(expr, pat, 1, err);
1294}
1295
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001296int pat_idx_tree_ip(struct pattern_expr *expr, struct pattern *pat, char **err)
1297{
1298 unsigned int mask;
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +01001299 struct pattern_tree *node;
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001300
1301 /* Only IPv4 can be indexed */
1302 if (pat->type == SMP_T_IPV4) {
Thierry FOURNIER972028f2014-01-23 17:53:31 +01001303 /* in IPv4 case, check if the mask is contiguous so that we can
1304 * insert the network into the tree. A continuous mask has only
1305 * ones on the left. This means that this mask + its lower bit
1306 * added once again is null.
1307 */
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001308 mask = ntohl(pat->val.ipv4.mask.s_addr);
1309 if (mask + (mask & -mask) == 0) {
1310 mask = mask ? 33 - flsnz(mask & -mask) : 0; /* equals cidr value */
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001311
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001312 /* node memory allocation */
1313 node = calloc(1, sizeof(*node) + 4);
1314 if (!node) {
1315 memprintf(err, "out of memory while loading pattern");
1316 return 0;
1317 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001318
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001319 /* copy the pointer to sample associated to this node */
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001320 node->data = pat->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01001321 node->ref = pat->ref;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001322
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001323 /* FIXME: insert <addr>/<mask> into the tree here */
1324 memcpy(node->node.key, &pat->val.ipv4.addr, 4); /* network byte order */
1325 node->node.node.pfx = mask;
Thierry FOURNIER31db4ae2014-01-30 00:27:15 +01001326
1327 /* Insert the entry. */
1328 ebmb_insert_prefix(&expr->pattern_tree, &node->node, 4);
Willy Tarreau4bdd0a12020-11-02 12:10:48 +01001329 LIST_ADDQ(&pat->ref->tree_head, &node->from_ref);
Willy Tarreau3ee0de12020-11-02 15:26:51 +01001330 expr->ref->revision = rdtsc();
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001331
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001332 /* that's ok */
1333 return 1;
1334 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001335 else {
1336 /* If the mask is not contiguous, just add the pattern to the list */
1337 return pat_idx_list_val(expr, pat, err);
1338 }
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001339 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001340 else if (pat->type == SMP_T_IPV6) {
1341 /* IPv6 also can be indexed */
1342 node = calloc(1, sizeof(*node) + 16);
1343 if (!node) {
1344 memprintf(err, "out of memory while loading pattern");
1345 return 0;
1346 }
1347
1348 /* copy the pointer to sample associated to this node */
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001349 node->data = pat->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01001350 node->ref = pat->ref;
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001351
1352 /* FIXME: insert <addr>/<mask> into the tree here */
1353 memcpy(node->node.key, &pat->val.ipv6.addr, 16); /* network byte order */
1354 node->node.node.pfx = pat->val.ipv6.mask;
Thierry FOURNIER31db4ae2014-01-30 00:27:15 +01001355
1356 /* Insert the entry. */
1357 ebmb_insert_prefix(&expr->pattern_tree_2, &node->node, 16);
Willy Tarreau4bdd0a12020-11-02 12:10:48 +01001358 LIST_ADDQ(&pat->ref->tree_head, &node->from_ref);
Willy Tarreau3ee0de12020-11-02 15:26:51 +01001359 expr->ref->revision = rdtsc();
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001360
1361 /* that's ok */
1362 return 1;
1363 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001364
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001365 return 0;
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001366}
1367
1368int pat_idx_tree_str(struct pattern_expr *expr, struct pattern *pat, char **err)
1369{
1370 int len;
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +01001371 struct pattern_tree *node;
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001372
1373 /* Only string can be indexed */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001374 if (pat->type != SMP_T_STR) {
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001375 memprintf(err, "internal error: string expected, but the type is '%s'",
1376 smp_to_type[pat->type]);
1377 return 0;
Thierry FOURNIER972028f2014-01-23 17:53:31 +01001378 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001379
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001380 /* If the flag PAT_F_IGNORE_CASE is set, we cannot use trees */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02001381 if (expr->mflags & PAT_MF_IGNORE_CASE)
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001382 return pat_idx_list_str(expr, pat, err);
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001383
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001384 /* Process the key len */
1385 len = strlen(pat->ptr.str) + 1;
1386
1387 /* node memory allocation */
1388 node = calloc(1, sizeof(*node) + len);
1389 if (!node) {
1390 memprintf(err, "out of memory while loading pattern");
1391 return 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001392 }
1393
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001394 /* copy the pointer to sample associated to this node */
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001395 node->data = pat->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01001396 node->ref = pat->ref;
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001397
1398 /* copy the string */
1399 memcpy(node->node.key, pat->ptr.str, len);
1400
1401 /* index the new node */
Thierry FOURNIER31db4ae2014-01-30 00:27:15 +01001402 ebst_insert(&expr->pattern_tree, &node->node);
Willy Tarreau4bdd0a12020-11-02 12:10:48 +01001403 LIST_ADDQ(&pat->ref->tree_head, &node->from_ref);
Willy Tarreau3ee0de12020-11-02 15:26:51 +01001404 expr->ref->revision = rdtsc();
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001405
1406 /* that's ok */
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001407 return 1;
1408}
1409
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +02001410int pat_idx_tree_pfx(struct pattern_expr *expr, struct pattern *pat, char **err)
1411{
1412 int len;
1413 struct pattern_tree *node;
1414
1415 /* Only string can be indexed */
1416 if (pat->type != SMP_T_STR) {
1417 memprintf(err, "internal error: string expected, but the type is '%s'",
1418 smp_to_type[pat->type]);
1419 return 0;
1420 }
1421
1422 /* If the flag PAT_F_IGNORE_CASE is set, we cannot use trees */
1423 if (expr->mflags & PAT_MF_IGNORE_CASE)
1424 return pat_idx_list_str(expr, pat, err);
1425
1426 /* Process the key len */
1427 len = strlen(pat->ptr.str);
1428
1429 /* node memory allocation */
1430 node = calloc(1, sizeof(*node) + len + 1);
1431 if (!node) {
1432 memprintf(err, "out of memory while loading pattern");
1433 return 0;
1434 }
1435
1436 /* copy the pointer to sample associated to this node */
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001437 node->data = pat->data;
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +02001438 node->ref = pat->ref;
1439
1440 /* copy the string and the trailing zero */
1441 memcpy(node->node.key, pat->ptr.str, len + 1);
1442 node->node.node.pfx = len * 8;
1443
1444 /* index the new node */
1445 ebmb_insert_prefix(&expr->pattern_tree, &node->node, len);
Willy Tarreau4bdd0a12020-11-02 12:10:48 +01001446 LIST_ADDQ(&pat->ref->tree_head, &node->from_ref);
Willy Tarreau3ee0de12020-11-02 15:26:51 +01001447 expr->ref->revision = rdtsc();
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +02001448
1449 /* that's ok */
1450 return 1;
1451}
1452
Willy Tarreauf1c08922020-11-02 19:53:16 +01001453/* Deletes all patterns from reference <elt>. Note that all of their
Willy Tarreau78777ea2020-11-02 13:55:22 +01001454 * expressions must be locked, and the pattern lock must be held as well.
1455 */
Willy Tarreauf1c08922020-11-02 19:53:16 +01001456void pat_delete_gen(struct pat_ref *ref, struct pat_ref_elt *elt)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001457{
Willy Tarreauf1c08922020-11-02 19:53:16 +01001458 struct pattern_tree *tree, *tree_bck;
1459 struct pattern_list *pat, *pat_bck;
1460
1461 /* delete all known tree nodes. They are all allocated inline */
1462 list_for_each_entry_safe(tree, tree_bck, &elt->tree_head, from_ref) {
1463 BUG_ON(tree->ref != elt);
1464
1465 ebmb_delete(&tree->node);
1466 LIST_DEL(&tree->from_ref);
1467 free(tree->data);
1468 free(tree);
1469 }
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001470
Willy Tarreauf1c08922020-11-02 19:53:16 +01001471 /* delete all list nodes and free their pattern entries (str/reg) */
1472 list_for_each_entry_safe(pat, pat_bck, &elt->list_head, from_ref) {
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001473 /* Check equality. */
Willy Tarreau78777ea2020-11-02 13:55:22 +01001474 BUG_ON(pat->pat.ref != elt);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001475
1476 /* Delete and free entry. */
1477 LIST_DEL(&pat->list);
Willy Tarreau4bdd0a12020-11-02 12:10:48 +01001478 LIST_DEL(&pat->from_ref);
Willy Tarreau6d8a6892020-11-02 19:26:02 +01001479 if (pat->pat.sflags & PAT_SF_REGFREE)
1480 regex_free(pat->pat.ptr.reg);
1481 else
1482 free(pat->pat.ptr.ptr);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001483 free(pat->pat.data);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001484 free(pat);
1485 }
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001486
Willy Tarreauf1c08922020-11-02 19:53:16 +01001487 /* update revision number to refresh the cache */
1488 ref->revision = rdtsc();
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001489}
1490
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001491void pattern_init_expr(struct pattern_expr *expr)
1492{
1493 LIST_INIT(&expr->patterns);
Thierry FOURNIER31db4ae2014-01-30 00:27:15 +01001494 expr->pattern_tree = EB_ROOT;
1495 expr->pattern_tree_2 = EB_ROOT;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001496}
1497
1498void pattern_init_head(struct pattern_head *head)
1499{
1500 LIST_INIT(&head->head);
1501}
1502
1503/* The following functions are relative to the management of the reference
1504 * lists. These lists are used to store the original pattern and associated
1505 * value as string form.
1506 *
1507 * This is used with modifiable ACL and MAPS
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001508 *
1509 * The pattern reference are stored with two identifiers: the unique_id and
1510 * the reference.
1511 *
1512 * The reference identify a file. Each file with the same name point to the
1513 * same reference. We can register many times one file. If the file is modified,
1514 * all his dependencies are also modified. The reference can be used with map or
1515 * acl.
1516 *
1517 * The unique_id identify inline acl. The unique id is unique for each acl.
1518 * You cannot force the same id in the configuration file, because this repoort
1519 * an error.
1520 *
1521 * A particular case appears if the filename is a number. In this case, the
1522 * unique_id is set with the number represented by the filename and the
1523 * reference is also set. This method prevent double unique_id.
1524 *
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001525 */
1526
Willy Tarreaua5bbaaf2020-10-30 16:03:50 +01001527/* This function looks up a reference by name. If the reference is found, a
1528 * pointer to the struct pat_ref is returned, otherwise NULL is returned.
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001529 */
1530struct pat_ref *pat_ref_lookup(const char *reference)
1531{
1532 struct pat_ref *ref;
1533
1534 list_for_each_entry(ref, &pattern_reference, list)
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001535 if (ref->reference && strcmp(reference, ref->reference) == 0)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001536 return ref;
1537 return NULL;
1538}
1539
Willy Tarreaua5bbaaf2020-10-30 16:03:50 +01001540/* This function looks up a reference's unique id. If the reference is found, a
1541 * pointer to the struct pat_ref is returned, otherwise NULL is returned.
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001542 */
1543struct pat_ref *pat_ref_lookupid(int unique_id)
1544{
1545 struct pat_ref *ref;
1546
1547 list_for_each_entry(ref, &pattern_reference, list)
1548 if (ref->unique_id == unique_id)
1549 return ref;
1550 return NULL;
1551}
1552
Willy Tarreau1fd52f72020-11-02 17:30:17 +01001553/* This function removes from the pattern reference <ref> all the patterns
1554 * attached to the reference element <elt>, and the element itself. The
1555 * reference must be locked.
1556 */
1557void pat_ref_delete_by_ptr(struct pat_ref *ref, struct pat_ref_elt *elt)
1558{
1559 struct pattern_expr *expr;
1560 struct bref *bref, *back;
1561
1562 /*
1563 * we have to unlink all watchers from this reference pattern. We must
1564 * not relink them if this elt was the last one in the list.
1565 */
1566 list_for_each_entry_safe(bref, back, &elt->back_refs, users) {
1567 LIST_DEL(&bref->users);
1568 LIST_INIT(&bref->users);
1569 if (elt->list.n != &ref->head)
1570 LIST_ADDQ(&LIST_ELEM(elt->list.n, typeof(elt), list)->back_refs, &bref->users);
1571 bref->ref = elt->list.n;
1572 }
1573
1574 /* delete all entries from all expressions for this pattern */
1575 list_for_each_entry(expr, &ref->pat, list)
1576 HA_RWLOCK_WRLOCK(PATEXP_LOCK, &expr->lock);
1577
1578 pat_delete_gen(ref, elt);
1579
1580 list_for_each_entry(expr, &ref->pat, list)
1581 HA_RWLOCK_WRUNLOCK(PATEXP_LOCK, &expr->lock);
1582
1583 LIST_DEL(&elt->list);
1584 free(elt->sample);
1585 free(elt->pattern);
1586 free(elt);
1587}
1588
Willy Tarreaua5bbaaf2020-10-30 16:03:50 +01001589/* This function removes all the patterns matching the pointer <refelt> from
1590 * the reference and from each expr member of this reference. This function
1591 * returns 1 if the entry was found and deleted, otherwise zero.
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001592 */
1593int pat_ref_delete_by_id(struct pat_ref *ref, struct pat_ref_elt *refelt)
1594{
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001595 struct pat_ref_elt *elt, *safe;
1596
1597 /* delete pattern from reference */
1598 list_for_each_entry_safe(elt, safe, &ref->head, list) {
1599 if (elt == refelt) {
Willy Tarreau1fd52f72020-11-02 17:30:17 +01001600 pat_ref_delete_by_ptr(ref, elt);
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001601 return 1;
1602 }
1603 }
1604 return 0;
1605}
1606
Willy Tarreau1fd52f72020-11-02 17:30:17 +01001607/* This function removes all patterns matching <key> from the reference
Joseph Herlant4189d672018-11-15 10:22:31 -08001608 * and from each expr member of the reference. This function returns 1
Willy Tarreau1fd52f72020-11-02 17:30:17 +01001609 * if the deletion is done and returns 0 is the entry is not found.
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001610 */
1611int pat_ref_delete(struct pat_ref *ref, const char *key)
1612{
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001613 struct pat_ref_elt *elt, *safe;
1614 int found = 0;
1615
1616 /* delete pattern from reference */
1617 list_for_each_entry_safe(elt, safe, &ref->head, list) {
1618 if (strcmp(key, elt->pattern) == 0) {
Willy Tarreau1fd52f72020-11-02 17:30:17 +01001619 pat_ref_delete_by_ptr(ref, elt);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001620 found = 1;
1621 }
1622 }
1623
Willy Tarreau1fd52f72020-11-02 17:30:17 +01001624 return found;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001625}
1626
Baptiste Assmann953f74d2014-04-25 16:57:03 +02001627/*
1628 * find and return an element <elt> matching <key> in a reference <ref>
1629 * return NULL if not found
1630 */
1631struct pat_ref_elt *pat_ref_find_elt(struct pat_ref *ref, const char *key)
1632{
1633 struct pat_ref_elt *elt;
1634
1635 list_for_each_entry(elt, &ref->head, list) {
1636 if (strcmp(key, elt->pattern) == 0)
1637 return elt;
1638 }
1639
1640 return NULL;
1641}
1642
1643
Willy Tarreaua5bbaaf2020-10-30 16:03:50 +01001644/* This function modifies the sample of pat_ref_elt <elt> in all expressions
1645 * found under <ref> to become <value>. It is assumed that the caller has
1646 * already verified that <elt> belongs to <ref>.
1647 */
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001648static inline int pat_ref_set_elt(struct pat_ref *ref, struct pat_ref_elt *elt,
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001649 const char *value, char **err)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001650{
1651 struct pattern_expr *expr;
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001652 struct sample_data **data;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001653 char *sample;
Thierry FOURNIER12ba0c22015-08-14 00:02:11 +02001654 struct sample_data test;
Thierry FOURNIER149e0fe2014-01-29 19:35:06 +01001655
1656 /* Try all needed converters. */
1657 list_for_each_entry(expr, &ref->pat, list) {
1658 if (!expr->pat_head->parse_smp)
1659 continue;
1660
1661 if (!expr->pat_head->parse_smp(value, &test)) {
1662 memprintf(err, "unable to parse '%s'", value);
1663 return 0;
1664 }
1665 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001666
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001667 /* Modify pattern from reference. */
1668 sample = strdup(value);
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001669 if (!sample) {
1670 memprintf(err, "out of memory error");
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001671 return 0;
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001672 }
Willy Tarreaua5bbaaf2020-10-30 16:03:50 +01001673 /* Load sample in each reference. All the conversions are tested
1674 * below, normally these calls don't fail.
Thierry FOURNIER149e0fe2014-01-29 19:35:06 +01001675 */
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001676 list_for_each_entry(expr, &ref->pat, list) {
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001677 if (!expr->pat_head->parse_smp)
1678 continue;
1679
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001680 HA_RWLOCK_WRLOCK(PATEXP_LOCK, &expr->lock);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001681 data = pattern_find_smp(expr, elt);
1682 if (data && *data && !expr->pat_head->parse_smp(sample, *data))
1683 *data = NULL;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001684 HA_RWLOCK_WRUNLOCK(PATEXP_LOCK, &expr->lock);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001685 }
1686
Emeric Brunb5997f72017-07-03 11:34:05 +02001687 /* free old sample only when all exprs are updated */
1688 free(elt->sample);
1689 elt->sample = sample;
1690
1691
Thierry FOURNIER149e0fe2014-01-29 19:35:06 +01001692 return 1;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001693}
1694
Willy Tarreaua5bbaaf2020-10-30 16:03:50 +01001695/* This function modifies the sample of pat_ref_elt <refelt> in all expressions
1696 * found under <ref> to become <value>, after checking that <refelt> really
1697 * belongs to <ref>.
1698 */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001699int 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 +01001700{
1701 struct pat_ref_elt *elt;
1702
1703 /* Look for pattern in the reference. */
1704 list_for_each_entry(elt, &ref->head, list) {
1705 if (elt == refelt) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001706 if (!pat_ref_set_elt(ref, elt, value, err))
1707 return 0;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001708 return 1;
1709 }
1710 }
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001711
1712 memprintf(err, "key or pattern not found");
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001713 return 0;
1714}
1715
Willy Tarreaua5bbaaf2020-10-30 16:03:50 +01001716/* This function modifies to <value> the sample of all patterns matching <key>
1717 * under <ref>.
1718 */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001719int pat_ref_set(struct pat_ref *ref, const char *key, const char *value, char **err)
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001720{
1721 struct pat_ref_elt *elt;
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001722 int found = 0;
1723 char *_merr;
1724 char **merr;
1725
1726 if (err) {
1727 merr = &_merr;
1728 *merr = NULL;
1729 }
1730 else
1731 merr = NULL;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001732
1733 /* Look for pattern in the reference. */
1734 list_for_each_entry(elt, &ref->head, list) {
1735 if (strcmp(key, elt->pattern) == 0) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001736 if (!pat_ref_set_elt(ref, elt, value, merr)) {
William Lallemand579fb252018-06-11 10:53:46 +02001737 if (err && merr) {
1738 if (!found) {
1739 *err = *merr;
1740 } else {
1741 memprintf(err, "%s, %s", *err, *merr);
1742 free(*merr);
1743 *merr = NULL;
1744 }
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001745 }
1746 }
1747 found = 1;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001748 }
1749 }
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001750
1751 if (!found) {
1752 memprintf(err, "entry not found");
1753 return 0;
1754 }
1755 return 1;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001756}
1757
Joseph Herlant4189d672018-11-15 10:22:31 -08001758/* This function creates a new reference. <ref> is the reference name.
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001759 * <flags> are PAT_REF_*. /!\ The reference is not checked, and must
1760 * be unique. The user must check the reference with "pat_ref_lookup()"
Willy Tarreaua5bbaaf2020-10-30 16:03:50 +01001761 * before calling this function. If the function fails, it returns NULL,
1762 * otherwise it returns the new struct pat_ref.
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001763 */
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001764struct pat_ref *pat_ref_new(const char *reference, const char *display, unsigned int flags)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001765{
1766 struct pat_ref *ref;
1767
Willy Tarreau8135d9b2020-10-30 15:35:11 +01001768 ref = calloc(1, sizeof(*ref));
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001769 if (!ref)
1770 return NULL;
1771
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001772 if (display) {
1773 ref->display = strdup(display);
1774 if (!ref->display) {
1775 free(ref);
1776 return NULL;
1777 }
1778 }
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001779
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001780 ref->reference = strdup(reference);
1781 if (!ref->reference) {
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001782 free(ref->display);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001783 free(ref);
1784 return NULL;
1785 }
1786
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001787 ref->flags = flags;
1788 ref->unique_id = -1;
Willy Tarreau3ee0de12020-11-02 15:26:51 +01001789 ref->revision = 0;
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001790
1791 LIST_INIT(&ref->head);
1792 LIST_INIT(&ref->pat);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001793 HA_SPIN_INIT(&ref->lock);
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001794 LIST_ADDQ(&pattern_reference, &ref->list);
1795
1796 return ref;
1797}
1798
Willy Tarreaua5bbaaf2020-10-30 16:03:50 +01001799/* This function creates a new reference. <unique_id> is the unique id. If
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001800 * the value of <unique_id> is -1, the unique id is calculated later.
1801 * <flags> are PAT_REF_*. /!\ The reference is not checked, and must
1802 * be unique. The user must check the reference with "pat_ref_lookup()"
1803 * or pat_ref_lookupid before calling this function. If the function
Willy Tarreaua5bbaaf2020-10-30 16:03:50 +01001804 * fails, it returns NULL, otherwise it returns the new struct pat_ref.
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001805 */
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001806struct pat_ref *pat_ref_newid(int unique_id, const char *display, unsigned int flags)
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001807{
1808 struct pat_ref *ref;
1809
Willy Tarreau8135d9b2020-10-30 15:35:11 +01001810 ref = calloc(1, sizeof(*ref));
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001811 if (!ref)
1812 return NULL;
1813
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001814 if (display) {
1815 ref->display = strdup(display);
1816 if (!ref->display) {
1817 free(ref);
1818 return NULL;
1819 }
1820 }
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001821
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001822 ref->reference = NULL;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001823 ref->flags = flags;
Willy Tarreau29947742020-10-28 11:43:49 +01001824 ref->curr_gen = 0;
1825 ref->next_gen = 0;
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001826 ref->unique_id = unique_id;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001827 LIST_INIT(&ref->head);
1828 LIST_INIT(&ref->pat);
Aurélien Nephtali564d15a2018-04-19 16:56:07 +02001829 HA_SPIN_INIT(&ref->lock);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001830 LIST_ADDQ(&pattern_reference, &ref->list);
1831
1832 return ref;
1833}
1834
Willy Tarreauf4edb722020-10-28 10:52:46 +01001835/* This function adds entry to <ref>. It can fail on memory error. It returns
1836 * the newly added element on success, or NULL on failure. The PATREF_LOCK on
Willy Tarreau29947742020-10-28 11:43:49 +01001837 * <ref> must be held. It sets the newly created pattern's generation number
1838 * to the same value as the reference's.
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001839 */
Willy Tarreauf4edb722020-10-28 10:52:46 +01001840struct 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 +01001841{
1842 struct pat_ref_elt *elt;
1843
Willy Tarreau8135d9b2020-10-30 15:35:11 +01001844 elt = calloc(1, sizeof(*elt));
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001845 if (!elt)
Willy Tarreauf4edb722020-10-28 10:52:46 +01001846 goto fail;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001847
Willy Tarreau29947742020-10-28 11:43:49 +01001848 elt->gen_id = ref->curr_gen;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001849 elt->line = line;
1850
1851 elt->pattern = strdup(pattern);
Willy Tarreauf4edb722020-10-28 10:52:46 +01001852 if (!elt->pattern)
1853 goto fail;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001854
1855 if (sample) {
1856 elt->sample = strdup(sample);
Willy Tarreauf4edb722020-10-28 10:52:46 +01001857 if (!elt->sample)
1858 goto fail;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001859 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001860
Emeric Brun8d85aa42017-06-29 15:40:33 +02001861 LIST_INIT(&elt->back_refs);
Willy Tarreau4bdd0a12020-11-02 12:10:48 +01001862 LIST_INIT(&elt->list_head);
1863 LIST_INIT(&elt->tree_head);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001864 LIST_ADDQ(&ref->head, &elt->list);
Willy Tarreauf4edb722020-10-28 10:52:46 +01001865 return elt;
1866 fail:
1867 if (elt)
1868 free(elt->pattern);
1869 free(elt);
1870 return NULL;
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001871}
1872
Willy Tarreaua5bbaaf2020-10-30 16:03:50 +01001873/* This function creates sample found in <elt>, parses the pattern also
1874 * found in <elt> and inserts it in <expr>. The function copies <patflags>
1875 * into <expr>. If the function fails, it returns 0 and <err> is filled.
Ilya Shipitsin47d17182020-06-21 21:42:57 +05001876 * In success case, the function returns 1.
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001877 */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001878int pat_ref_push(struct pat_ref_elt *elt, struct pattern_expr *expr,
1879 int patflags, char **err)
1880{
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001881 struct sample_data *data;
Thierry FOURNIERd25c8422014-01-28 15:34:35 +01001882 struct pattern pattern;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001883
1884 /* Create sample */
1885 if (elt->sample && expr->pat_head->parse_smp) {
1886 /* New sample. */
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001887 data = malloc(sizeof(*data));
1888 if (!data)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001889 return 0;
1890
1891 /* Parse value. */
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001892 if (!expr->pat_head->parse_smp(elt->sample, data)) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001893 memprintf(err, "unable to parse '%s'", elt->sample);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001894 free(data);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001895 return 0;
1896 }
1897
1898 }
1899 else
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001900 data = NULL;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001901
Thierry FOURNIERd25c8422014-01-28 15:34:35 +01001902 /* initialise pattern */
1903 memset(&pattern, 0, sizeof(pattern));
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001904 pattern.data = data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01001905 pattern.ref = elt;
Thierry FOURNIERd25c8422014-01-28 15:34:35 +01001906
1907 /* parse pattern */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02001908 if (!expr->pat_head->parse(elt->pattern, &pattern, expr->mflags, err)) {
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001909 free(data);
Thierry FOURNIERd25c8422014-01-28 15:34:35 +01001910 return 0;
1911 }
1912
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001913 HA_RWLOCK_WRLOCK(PATEXP_LOCK, &expr->lock);
Thierry FOURNIERd25c8422014-01-28 15:34:35 +01001914 /* index pattern */
1915 if (!expr->pat_head->index(expr, &pattern, err)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001916 HA_RWLOCK_WRUNLOCK(PATEXP_LOCK, &expr->lock);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001917 free(data);
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001918 return 0;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001919 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001920 HA_RWLOCK_WRUNLOCK(PATEXP_LOCK, &expr->lock);
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001921
1922 return 1;
1923}
1924
Willy Tarreau0439e5e2020-10-28 18:45:45 +01001925/* This function tries to commit entry <elt> into <ref>. The new entry must
1926 * have already been inserted using pat_ref_append(), and its generation number
1927 * may have been adjusted as it will not be changed. <err> must point to a NULL
1928 * pointer. The PATREF lock on <ref> must be held. All the pattern_expr for
1929 * this reference will be updated (parsing, indexing). On success, non-zero is
1930 * returned. On failure, all the operation is rolled back (the element is
1931 * deleted from all expressions and is freed), zero is returned and the error
1932 * pointer <err> may have been updated (and the caller must free it). Failure
1933 * causes include memory allocation, parsing error or indexing error.
1934 */
1935int pat_ref_commit(struct pat_ref *ref, struct pat_ref_elt *elt, char **err)
1936{
1937 struct pattern_expr *expr;
1938
1939 list_for_each_entry(expr, &ref->pat, list) {
1940 if (!pat_ref_push(elt, expr, 0, err)) {
1941 pat_ref_delete_by_ptr(ref, elt);
1942 return 0;
1943 }
1944 }
1945 return 1;
1946}
1947
Willy Tarreau1a6857b2020-10-29 09:21:43 +01001948/* Loads <pattern>:<sample> into <ref> for generation <gen>. <sample> may be
1949 * NULL if none exists (e.g. ACL). If not needed, the generation number should
1950 * be set to ref->curr_gen. The error pointer must initially point to NULL. The
1951 * new entry will be propagated to all use places, involving allocation, parsing
1952 * and indexing. On error (parsing, allocation), the operation will be rolled
1953 * back, an error may be reported, and NULL will be reported. On success, the
1954 * freshly allocated element will be returned. The PATREF lock on <ref> must be
1955 * held during the operation.
1956 */
1957struct pat_ref_elt *pat_ref_load(struct pat_ref *ref, unsigned int gen,
1958 const char *pattern, const char *sample,
1959 int line, char **err)
1960{
1961 struct pat_ref_elt *elt;
1962
1963 elt = pat_ref_append(ref, pattern, sample, line);
1964 if (elt) {
1965 elt->gen_id = gen;
1966 if (!pat_ref_commit(ref, elt, err))
1967 elt = NULL;
1968 } else
1969 memprintf(err, "out of memory error");
1970
1971 return elt;
1972}
1973
Willy Tarreau6a174072020-10-28 10:58:05 +01001974/* This function adds entry to <ref>. It can fail on memory error. The new
Thierry FOURNIER31db4ae2014-01-30 00:27:15 +01001975 * entry is added at all the pattern_expr registered in this reference. The
Willy Tarreau6a174072020-10-28 10:58:05 +01001976 * function stops on the first error encountered. It returns 0 and <err> is
Thierry FOURNIER31db4ae2014-01-30 00:27:15 +01001977 * filled. If an error is encountered, the complete add operation is cancelled.
1978 * If the insertion is a success the function returns 1.
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001979 */
1980int pat_ref_add(struct pat_ref *ref,
1981 const char *pattern, const char *sample,
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02001982 char **err)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001983{
Willy Tarreau1a6857b2020-10-29 09:21:43 +01001984 return !!pat_ref_load(ref, ref->curr_gen, pattern, sample, -1, err);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001985}
1986
Joseph Herlant4189d672018-11-15 10:22:31 -08001987/* This function prunes <ref>, replaces all references by the references
1988 * of <replace>, and reindexes all the news values.
Thierry FOURNIER46006bd2014-03-21 21:45:15 +01001989 *
Joseph Herlant4189d672018-11-15 10:22:31 -08001990 * The patterns are loaded in best effort and the errors are ignored,
1991 * but written in the logs.
Thierry FOURNIER46006bd2014-03-21 21:45:15 +01001992 */
1993void pat_ref_reload(struct pat_ref *ref, struct pat_ref *replace)
1994{
1995 struct pattern_expr *expr;
Emeric Brunb5997f72017-07-03 11:34:05 +02001996 struct pat_ref_elt *elt, *safe;
1997 struct bref *bref, *back;
Emeric Brunb5997f72017-07-03 11:34:05 +02001998 struct pattern pattern;
Thierry FOURNIER46006bd2014-03-21 21:45:15 +01001999
Thierry FOURNIER46006bd2014-03-21 21:45:15 +01002000
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002001 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Emeric Brunb5997f72017-07-03 11:34:05 +02002002 list_for_each_entry(expr, &ref->pat, list) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002003 HA_RWLOCK_WRLOCK(PATEXP_LOCK, &expr->lock);
Emeric Brunb5997f72017-07-03 11:34:05 +02002004 }
2005
2006 /* all expr are locked, we can safely remove all pat_ref */
2007 list_for_each_entry_safe(elt, safe, &ref->head, list) {
2008 list_for_each_entry_safe(bref, back, &elt->back_refs, users) {
Willy Tarreaud4164dc2020-10-27 18:55:20 +01002009 /* we have to unlink all watchers. */
2010 LIST_DEL_INIT(&bref->users);
2011 bref->ref = NULL;
Emeric Brunb5997f72017-07-03 11:34:05 +02002012 }
2013 LIST_DEL(&elt->list);
Willy Tarreau4bdd0a12020-11-02 12:10:48 +01002014 LIST_DEL(&elt->list_head);
2015 LIST_DEL(&elt->tree_head);
Emeric Brunb5997f72017-07-03 11:34:05 +02002016 free(elt->pattern);
2017 free(elt->sample);
2018 free(elt);
2019 }
2020
2021 /* switch pat_ret_elt lists */
Thierry FOURNIER46006bd2014-03-21 21:45:15 +01002022 LIST_ADD(&replace->head, &ref->head);
2023 LIST_DEL(&replace->head);
2024
Emeric Brunb5997f72017-07-03 11:34:05 +02002025 list_for_each_entry(expr, &ref->pat, list) {
2026 expr->pat_head->prune(expr);
2027 list_for_each_entry(elt, &ref->head, list) {
Dragan Dosenf1474792018-09-18 20:18:09 +02002028 char *err = NULL;
2029 struct sample_data *data = NULL;
2030
Emeric Brunb5997f72017-07-03 11:34:05 +02002031 /* Create sample */
2032 if (elt->sample && expr->pat_head->parse_smp) {
2033 /* New sample. */
2034 data = malloc(sizeof(*data));
2035 if (!data)
2036 continue;
2037
2038 /* Parse value. */
2039 if (!expr->pat_head->parse_smp(elt->sample, data)) {
2040 memprintf(&err, "unable to parse '%s'", elt->sample);
2041 send_log(NULL, LOG_NOTICE, "%s", err);
2042 free(err);
2043 free(data);
2044 continue;
2045 }
2046
2047 }
Emeric Brunb5997f72017-07-03 11:34:05 +02002048
2049 /* initialise pattern */
2050 memset(&pattern, 0, sizeof(pattern));
2051 pattern.data = data;
2052 pattern.ref = elt;
2053
2054 /* parse pattern */
2055 if (!expr->pat_head->parse(elt->pattern, &pattern, expr->mflags, &err)) {
Thierry FOURNIER46006bd2014-03-21 21:45:15 +01002056 send_log(NULL, LOG_NOTICE, "%s", err);
2057 free(err);
Emeric Brunb5997f72017-07-03 11:34:05 +02002058 free(data);
2059 continue;
2060 }
2061
2062 /* index pattern */
2063 if (!expr->pat_head->index(expr, &pattern, &err)) {
2064 send_log(NULL, LOG_NOTICE, "%s", err);
2065 free(err);
2066 free(data);
2067 continue;
Thierry FOURNIER46006bd2014-03-21 21:45:15 +01002068 }
2069 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002070 HA_RWLOCK_WRUNLOCK(PATEXP_LOCK, &expr->lock);
Thierry FOURNIER46006bd2014-03-21 21:45:15 +01002071 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002072 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Willy Tarreau114d6982020-11-03 15:55:35 +01002073
2074#if defined(HA_HAVE_MALLOC_TRIM)
2075 malloc_trim(0);
2076#endif
Thierry FOURNIER46006bd2014-03-21 21:45:15 +01002077}
2078
Willy Tarreaua5bbaaf2020-10-30 16:03:50 +01002079/* This function prunes all entries of <ref>. This function
Willy Tarreaud1d005d2019-12-20 18:22:02 +01002080 * prunes the associated pattern_expr. It may return before the end of
2081 * the list is reached, returning 0, to yield. The caller must call it
2082 * again. Otherwise it returns 1 once done.
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002083 */
Willy Tarreaud1d005d2019-12-20 18:22:02 +01002084int pat_ref_prune(struct pat_ref *ref)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002085{
2086 struct pat_ref_elt *elt, *safe;
2087 struct pattern_expr *expr;
Emeric Brun8d85aa42017-06-29 15:40:33 +02002088 struct bref *bref, *back;
Willy Tarreaud1d005d2019-12-20 18:22:02 +01002089 int loops = 0;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002090
Emeric Brunb5997f72017-07-03 11:34:05 +02002091 list_for_each_entry(expr, &ref->pat, list) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002092 HA_RWLOCK_WRLOCK(PATEXP_LOCK, &expr->lock);
Emeric Brunb5997f72017-07-03 11:34:05 +02002093 expr->pat_head->prune(expr);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002094 HA_RWLOCK_WRUNLOCK(PATEXP_LOCK, &expr->lock);
Willy Tarreaud1d005d2019-12-20 18:22:02 +01002095 loops++;
2096 /* yield often, some lists may be huge, especially those
2097 * having to be freed through free_pattern_tree()
2098 */
2099 if (loops > 10)
2100 return 0;
Emeric Brunb5997f72017-07-03 11:34:05 +02002101 }
2102
2103 /* we trash pat_ref_elt in a second time to ensure that data is
2104 free once there is no ref on it */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002105 list_for_each_entry_safe(elt, safe, &ref->head, list) {
Emeric Brun8d85aa42017-06-29 15:40:33 +02002106 list_for_each_entry_safe(bref, back, &elt->back_refs, users) {
2107 /*
2108 * we have to unlink all watchers. We must not relink them if
2109 * this elt was the last one in the list.
2110 */
2111 LIST_DEL(&bref->users);
2112 LIST_INIT(&bref->users);
2113 if (elt->list.n != &ref->head)
Willy Tarreau49ee3b22019-04-30 11:43:43 +02002114 LIST_ADDQ(&LIST_ELEM(elt->list.n, typeof(elt), list)->back_refs, &bref->users);
Emeric Brun8d85aa42017-06-29 15:40:33 +02002115 bref->ref = elt->list.n;
2116 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002117 LIST_DEL(&elt->list);
2118 free(elt->pattern);
2119 free(elt->sample);
2120 free(elt);
Willy Tarreaud1d005d2019-12-20 18:22:02 +01002121 loops++;
2122 if (loops > 100000)
2123 return 0;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002124 }
Willy Tarreaud1d005d2019-12-20 18:22:02 +01002125 return 1;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002126}
2127
Willy Tarreaua5bbaaf2020-10-30 16:03:50 +01002128/* This function looks up any existing reference <ref> in pattern_head <head>, and
2129 * returns the associated pattern_expr pointer if found, otherwise NULL.
2130 */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002131struct pattern_expr *pattern_lookup_expr(struct pattern_head *head, struct pat_ref *ref)
2132{
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002133 struct pattern_expr_list *expr;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002134
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002135 list_for_each_entry(expr, &head->head, list)
2136 if (expr->expr->ref == ref)
2137 return expr->expr;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002138 return NULL;
2139}
2140
Joseph Herlant4189d672018-11-15 10:22:31 -08002141/* This function creates new pattern_expr associated to the reference <ref>.
2142 * <ref> can be NULL. If an error occurs, the function returns NULL and
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002143 * <err> is filled. Otherwise, the function returns new pattern_expr linked
2144 * with <head> and <ref>.
Thierry FOURNIER315ec422014-11-24 11:14:42 +01002145 *
Joseph Herlant4189d672018-11-15 10:22:31 -08002146 * The returned value can be an already filled pattern list, in this case the
Thierry FOURNIER315ec422014-11-24 11:14:42 +01002147 * flag <reuse> is set.
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002148 */
Thierry FOURNIER315ec422014-11-24 11:14:42 +01002149struct pattern_expr *pattern_new_expr(struct pattern_head *head, struct pat_ref *ref,
Emeric Brun7d27f3c2017-07-03 17:54:23 +02002150 int patflags, char **err, int *reuse)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002151{
2152 struct pattern_expr *expr;
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002153 struct pattern_expr_list *list;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002154
Thierry FOURNIER315ec422014-11-24 11:14:42 +01002155 if (reuse)
2156 *reuse = 0;
2157
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002158 /* Memory and initialization of the chain element. */
Willy Tarreau8135d9b2020-10-30 15:35:11 +01002159 list = calloc(1, sizeof(*list));
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002160 if (!list) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002161 memprintf(err, "out of memory");
2162 return NULL;
2163 }
2164
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002165 /* Look for existing similar expr. No that only the index, parse and
2166 * parse_smp function must be identical for having similar pattern.
Joseph Herlant4189d672018-11-15 10:22:31 -08002167 * The other function depends of these first.
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002168 */
2169 if (ref) {
2170 list_for_each_entry(expr, &ref->pat, list)
2171 if (expr->pat_head->index == head->index &&
2172 expr->pat_head->parse == head->parse &&
Emeric Brun7d27f3c2017-07-03 17:54:23 +02002173 expr->pat_head->parse_smp == head->parse_smp &&
2174 expr->mflags == patflags)
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002175 break;
2176 if (&expr->list == &ref->pat)
2177 expr = NULL;
2178 }
2179 else
2180 expr = NULL;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002181
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002182 /* If no similar expr was found, we create new expr. */
2183 if (!expr) {
2184 /* Get a lot of memory for the expr struct. */
Willy Tarreau8135d9b2020-10-30 15:35:11 +01002185 expr = calloc(1, sizeof(*expr));
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002186 if (!expr) {
Andreas Seltenreiche6e22e82016-03-03 20:20:23 +01002187 free(list);
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002188 memprintf(err, "out of memory");
2189 return NULL;
2190 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002191
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002192 /* Initialize this new expr. */
2193 pattern_init_expr(expr);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002194
Emeric Brun7d27f3c2017-07-03 17:54:23 +02002195 /* Copy the pattern matching and indexing flags. */
2196 expr->mflags = patflags;
2197
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002198 /* This new pattern expression reference one of his heads. */
2199 expr->pat_head = head;
2200
2201 /* Link with ref, or to self to facilitate LIST_DEL() */
2202 if (ref)
2203 LIST_ADDQ(&ref->pat, &expr->list);
2204 else
2205 LIST_INIT(&expr->list);
2206
2207 expr->ref = ref;
2208
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002209 HA_RWLOCK_INIT(&expr->lock);
Emeric Brunb5997f72017-07-03 11:34:05 +02002210
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002211 /* We must free this pattern if it is no more used. */
2212 list->do_free = 1;
2213 }
2214 else {
2215 /* If the pattern used already exists, it is already linked
2216 * with ref and we must not free it.
2217 */
2218 list->do_free = 0;
Thierry FOURNIER315ec422014-11-24 11:14:42 +01002219 if (reuse)
2220 *reuse = 1;
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002221 }
2222
2223 /* The new list element reference the pattern_expr. */
2224 list->expr = expr;
2225
2226 /* Link the list element with the pattern_head. */
2227 LIST_ADDQ(&head->head, &list->list);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002228 return expr;
2229}
2230
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002231/* Reads patterns from a file. If <err_msg> is non-NULL, an error message will
2232 * be returned there on errors and the caller will have to free it.
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002233 *
2234 * The file contains one key + value per line. Lines which start with '#' are
2235 * ignored, just like empty lines. Leading tabs/spaces are stripped. The key is
2236 * then the first "word" (series of non-space/tabs characters), and the value is
2237 * what follows this series of space/tab till the end of the line excluding
2238 * trailing spaces/tabs.
2239 *
2240 * Example :
2241 *
2242 * # this is a comment and is ignored
2243 * 62.212.114.60 1wt.eu \n
2244 * <-><-----------><---><----><---->
2245 * | | | | `--- trailing spaces ignored
2246 * | | | `-------- value
2247 * | | `--------------- middle spaces ignored
2248 * | `------------------------ key
2249 * `-------------------------------- leading spaces ignored
2250 *
Ilya Shipitsin47d17182020-06-21 21:42:57 +05002251 * Return non-zero in case of success, otherwise 0.
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002252 */
2253int pat_ref_read_from_file_smp(struct pat_ref *ref, const char *filename, char **err)
2254{
2255 FILE *file;
2256 char *c;
2257 int ret = 0;
2258 int line = 0;
2259 char *key_beg;
2260 char *key_end;
2261 char *value_beg;
2262 char *value_end;
2263
2264 file = fopen(filename, "r");
2265 if (!file) {
2266 memprintf(err, "failed to open pattern file <%s>", filename);
2267 return 0;
2268 }
2269
2270 /* now parse all patterns. The file may contain only one pattern
2271 * followed by one value per line. The start spaces, separator spaces
2272 * and and spaces are stripped. Each can contain comment started by '#'
2273 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002274 while (fgets(trash.area, trash.size, file) != NULL) {
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002275 line++;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002276 c = trash.area;
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002277
2278 /* ignore lines beginning with a dash */
2279 if (*c == '#')
2280 continue;
2281
2282 /* strip leading spaces and tabs */
2283 while (*c == ' ' || *c == '\t')
2284 c++;
2285
2286 /* empty lines are ignored too */
2287 if (*c == '\0' || *c == '\r' || *c == '\n')
2288 continue;
2289
2290 /* look for the end of the key */
2291 key_beg = c;
2292 while (*c && *c != ' ' && *c != '\t' && *c != '\n' && *c != '\r')
2293 c++;
2294
2295 key_end = c;
2296
2297 /* strip middle spaces and tabs */
2298 while (*c == ' ' || *c == '\t')
2299 c++;
2300
2301 /* look for the end of the value, it is the end of the line */
2302 value_beg = c;
2303 while (*c && *c != '\n' && *c != '\r')
2304 c++;
2305 value_end = c;
2306
2307 /* trim possibly trailing spaces and tabs */
2308 while (value_end > value_beg && (value_end[-1] == ' ' || value_end[-1] == '\t'))
2309 value_end--;
2310
2311 /* set final \0 and check entries */
2312 *key_end = '\0';
2313 *value_end = '\0';
2314
2315 /* insert values */
2316 if (!pat_ref_append(ref, key_beg, value_beg, line)) {
2317 memprintf(err, "out of memory");
2318 goto out_close;
2319 }
2320 }
2321
Jerome Magnin3c79d4b2020-01-17 16:09:33 +01002322 if (ferror(file)) {
2323 memprintf(err, "error encountered while reading <%s> : %s",
2324 filename, strerror(errno));
2325 goto out_close;
2326 }
Ilya Shipitsin47d17182020-06-21 21:42:57 +05002327 /* success */
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002328 ret = 1;
2329
2330 out_close:
2331 fclose(file);
2332 return ret;
2333}
2334
2335/* Reads patterns from a file. If <err_msg> is non-NULL, an error message will
2336 * be returned there on errors and the caller will have to free it.
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002337 */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002338int pat_ref_read_from_file(struct pat_ref *ref, const char *filename, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002339{
2340 FILE *file;
2341 char *c;
2342 char *arg;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002343 int ret = 0;
2344 int line = 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002345
2346 file = fopen(filename, "r");
2347 if (!file) {
2348 memprintf(err, "failed to open pattern file <%s>", filename);
2349 return 0;
2350 }
2351
2352 /* now parse all patterns. The file may contain only one pattern per
2353 * line. If the line contains spaces, they will be part of the pattern.
2354 * The pattern stops at the first CR, LF or EOF encountered.
2355 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002356 while (fgets(trash.area, trash.size, file) != NULL) {
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002357 line++;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002358 c = trash.area;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002359
2360 /* ignore lines beginning with a dash */
2361 if (*c == '#')
2362 continue;
2363
2364 /* strip leading spaces and tabs */
2365 while (*c == ' ' || *c == '\t')
2366 c++;
2367
2368
2369 arg = c;
2370 while (*c && *c != '\n' && *c != '\r')
2371 c++;
2372 *c = 0;
2373
2374 /* empty lines are ignored too */
2375 if (c == arg)
2376 continue;
2377
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002378 if (!pat_ref_append(ref, arg, NULL, line)) {
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002379 memprintf(err, "out of memory when loading patterns from file <%s>", filename);
2380 goto out_close;
2381 }
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002382 }
2383
Jerome Magnin3c79d4b2020-01-17 16:09:33 +01002384 if (ferror(file)) {
2385 memprintf(err, "error encountered while reading <%s> : %s",
2386 filename, strerror(errno));
2387 goto out_close;
2388 }
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002389 ret = 1; /* success */
2390
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002391 out_close:
2392 fclose(file);
2393 return ret;
2394}
2395
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002396int pattern_read_from_file(struct pattern_head *head, unsigned int refflags,
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002397 const char *filename, int patflags, int load_smp,
Thierry FOURNIER94580c92014-02-11 14:36:45 +01002398 char **err, const char *file, int line)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002399{
2400 struct pat_ref *ref;
2401 struct pattern_expr *expr;
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002402 struct pat_ref_elt *elt;
Willy Tarreau4deaf392014-11-26 13:17:03 +01002403 int reuse = 0;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002404
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002405 /* Lookup for the existing reference. */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002406 ref = pat_ref_lookup(filename);
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002407
2408 /* If the reference doesn't exists, create it and load associated file. */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002409 if (!ref) {
Thierry FOURNIER94580c92014-02-11 14:36:45 +01002410 chunk_printf(&trash,
2411 "pattern loaded from file '%s' used by %s at file '%s' line %d",
2412 filename, refflags & PAT_REF_MAP ? "map" : "acl", file, line);
2413
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002414 ref = pat_ref_new(filename, trash.area, refflags);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002415 if (!ref) {
2416 memprintf(err, "out of memory");
2417 return 0;
2418 }
2419
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002420 if (load_smp) {
Thierry FOURNIERc0bd9102014-01-29 12:32:58 +01002421 ref->flags |= PAT_REF_SMP;
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002422 if (!pat_ref_read_from_file_smp(ref, filename, err))
2423 return 0;
2424 }
2425 else {
2426 if (!pat_ref_read_from_file(ref, filename, err))
2427 return 0;
2428 }
2429 }
2430 else {
Thierry FOURNIERc0bd9102014-01-29 12:32:58 +01002431 /* The reference already exists, check the map compatibility. */
2432
2433 /* If the load require samples and the flag PAT_REF_SMP is not set,
2434 * the reference doesn't contain sample, and cannot be used.
2435 */
2436 if (load_smp) {
2437 if (!(ref->flags & PAT_REF_SMP)) {
2438 memprintf(err, "The file \"%s\" is already used as one column file "
2439 "and cannot be used by as two column file.",
2440 filename);
2441 return 0;
2442 }
2443 }
2444 else {
2445 /* The load doesn't require samples. If the flag PAT_REF_SMP is
2446 * set, the reference contains a sample, and cannot be used.
2447 */
2448 if (ref->flags & PAT_REF_SMP) {
2449 memprintf(err, "The file \"%s\" is already used as two column file "
2450 "and cannot be used by as one column file.",
2451 filename);
2452 return 0;
2453 }
2454 }
2455
Thierry FOURNIER94580c92014-02-11 14:36:45 +01002456 /* Extends display */
2457 chunk_printf(&trash, "%s", ref->display);
2458 chunk_appendf(&trash, ", by %s at file '%s' line %d",
2459 refflags & PAT_REF_MAP ? "map" : "acl", file, line);
2460 free(ref->display);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002461 ref->display = strdup(trash.area);
Thierry FOURNIER94580c92014-02-11 14:36:45 +01002462 if (!ref->display) {
2463 memprintf(err, "out of memory");
2464 return 0;
2465 }
2466
Thierry FOURNIERc0bd9102014-01-29 12:32:58 +01002467 /* Merge flags. */
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002468 ref->flags |= refflags;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002469 }
2470
2471 /* Now, we can loading patterns from the reference. */
2472
2473 /* Lookup for existing reference in the head. If the reference
2474 * doesn't exists, create it.
2475 */
2476 expr = pattern_lookup_expr(head, ref);
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02002477 if (!expr || (expr->mflags != patflags)) {
Emeric Brun7d27f3c2017-07-03 17:54:23 +02002478 expr = pattern_new_expr(head, ref, patflags, err, &reuse);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002479 if (!expr)
2480 return 0;
2481 }
2482
Thierry FOURNIER315ec422014-11-24 11:14:42 +01002483 /* The returned expression may be not empty, because the function
2484 * "pattern_new_expr" lookup for similar pattern list and can
2485 * reuse a already filled pattern list. In this case, we can not
2486 * reload the patterns.
2487 */
2488 if (reuse)
2489 return 1;
2490
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002491 /* Load reference content in the pattern expression. */
2492 list_for_each_entry(elt, &ref->head, list) {
2493 if (!pat_ref_push(elt, expr, patflags, err)) {
2494 if (elt->line > 0)
2495 memprintf(err, "%s at line %d of file '%s'",
2496 *err, elt->line, filename);
2497 return 0;
2498 }
2499 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002500
2501 return 1;
2502}
2503
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01002504/* This function executes a pattern match on a sample. It applies pattern <expr>
2505 * to sample <smp>. The function returns NULL if the sample dont match. It returns
2506 * non-null if the sample match. If <fill> is true and the sample match, the
2507 * function returns the matched pattern. In many cases, this pattern can be a
2508 * static buffer.
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002509 */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002510struct pattern *pattern_exec_match(struct pattern_head *head, struct sample *smp, int fill)
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002511{
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002512 struct pattern_expr_list *list;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002513 struct pattern *pat;
2514
2515 if (!head->match) {
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01002516 if (fill) {
Thierry FOURNIER503bb092015-08-19 08:35:43 +02002517 static_pattern.data = NULL;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01002518 static_pattern.ref = NULL;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02002519 static_pattern.sflags = 0;
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002520 static_pattern.type = SMP_T_SINT;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01002521 static_pattern.val.i = 1;
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01002522 }
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01002523 return &static_pattern;
2524 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002525
Thierry FOURNIER5d344082014-01-27 14:19:53 +01002526 /* convert input to string */
2527 if (!sample_convert(smp, head->expect_type))
2528 return NULL;
2529
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002530 list_for_each_entry(list, &head->head, list) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002531 HA_RWLOCK_RDLOCK(PATEXP_LOCK, &list->expr->lock);
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002532 pat = head->match(smp, list->expr, fill);
Emeric Brunb5997f72017-07-03 11:34:05 +02002533 if (pat) {
2534 /* We duplicate the pattern cause it could be modified
2535 by another thread */
2536 if (pat != &static_pattern) {
2537 memcpy(&static_pattern, pat, sizeof(struct pattern));
2538 pat = &static_pattern;
2539 }
2540
2541 /* We also duplicate the sample data for
2542 same reason */
2543 if (pat->data && (pat->data != &static_sample_data)) {
Christopher Faulet09fdf4b2017-11-09 16:14:16 +01002544 switch(pat->data->type) {
Emeric Brunb5997f72017-07-03 11:34:05 +02002545 case SMP_T_STR:
2546 static_sample_data.type = SMP_T_STR;
2547 static_sample_data.u.str = *get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002548 static_sample_data.u.str.data = pat->data->u.str.data;
2549 if (static_sample_data.u.str.data >= static_sample_data.u.str.size)
2550 static_sample_data.u.str.data = static_sample_data.u.str.size - 1;
2551 memcpy(static_sample_data.u.str.area,
Willy Tarreau2fc761e2020-06-11 16:37:35 +02002552 pat->data->u.str.area, static_sample_data.u.str.data);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002553 static_sample_data.u.str.area[static_sample_data.u.str.data] = 0;
Willy Tarreau2fc761e2020-06-11 16:37:35 +02002554 pat->data = &static_sample_data;
2555 break;
2556
Emeric Brunb5997f72017-07-03 11:34:05 +02002557 case SMP_T_IPV4:
2558 case SMP_T_IPV6:
2559 case SMP_T_SINT:
2560 memcpy(&static_sample_data, pat->data, sizeof(struct sample_data));
Willy Tarreau2fc761e2020-06-11 16:37:35 +02002561 pat->data = &static_sample_data;
2562 break;
Emeric Brunb5997f72017-07-03 11:34:05 +02002563 default:
Willy Tarreau2fc761e2020-06-11 16:37:35 +02002564 /* unimplemented pattern type */
Emeric Brunb5997f72017-07-03 11:34:05 +02002565 pat->data = NULL;
Willy Tarreau2fc761e2020-06-11 16:37:35 +02002566 break;
Emeric Brunb5997f72017-07-03 11:34:05 +02002567 }
Emeric Brunb5997f72017-07-03 11:34:05 +02002568 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002569 HA_RWLOCK_RDUNLOCK(PATEXP_LOCK, &list->expr->lock);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002570 return pat;
Emeric Brunb5997f72017-07-03 11:34:05 +02002571 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002572 HA_RWLOCK_RDUNLOCK(PATEXP_LOCK, &list->expr->lock);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002573 }
2574 return NULL;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002575}
2576
Willy Tarreaua5bbaaf2020-10-30 16:03:50 +01002577/* This function prunes the pattern expressions starting at pattern_head <head>. */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002578void pattern_prune(struct pattern_head *head)
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01002579{
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002580 struct pattern_expr_list *list, *safe;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002581
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002582 list_for_each_entry_safe(list, safe, &head->head, list) {
2583 LIST_DEL(&list->list);
2584 if (list->do_free) {
2585 LIST_DEL(&list->expr->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002586 HA_RWLOCK_WRLOCK(PATEXP_LOCK, &list->expr->lock);
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002587 head->prune(list->expr);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002588 HA_RWLOCK_WRUNLOCK(PATEXP_LOCK, &list->expr->lock);
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002589 free(list->expr);
2590 }
2591 free(list);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002592 }
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01002593}
2594
Willy Tarreaua5bbaaf2020-10-30 16:03:50 +01002595/* This function searches occurrences of pattern reference element <ref> in
2596 * expression <expr> and returns a pointer to a pointer of the sample storage.
2597 * If <ref> is not found, NULL is returned.
Thierry FOURNIER55d0b102014-01-15 11:25:26 +01002598 */
Thierry FOURNIER12ba0c22015-08-14 00:02:11 +02002599struct sample_data **pattern_find_smp(struct pattern_expr *expr, struct pat_ref_elt *ref)
Thierry FOURNIER55d0b102014-01-15 11:25:26 +01002600{
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01002601 struct ebmb_node *node;
2602 struct pattern_tree *elt;
2603 struct pattern_list *pat;
Thierry FOURNIER55d0b102014-01-15 11:25:26 +01002604
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01002605 for (node = ebmb_first(&expr->pattern_tree);
2606 node;
2607 node = ebmb_next(node)) {
2608 elt = container_of(node, struct pattern_tree, node);
2609 if (elt->ref == ref)
Thierry FOURNIER503bb092015-08-19 08:35:43 +02002610 return &elt->data;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01002611 }
2612
2613 for (node = ebmb_first(&expr->pattern_tree_2);
2614 node;
2615 node = ebmb_next(node)) {
2616 elt = container_of(node, struct pattern_tree, node);
2617 if (elt->ref == ref)
Thierry FOURNIER503bb092015-08-19 08:35:43 +02002618 return &elt->data;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01002619 }
2620
2621 list_for_each_entry(pat, &expr->patterns, list)
2622 if (pat->pat.ref == ref)
Thierry FOURNIER503bb092015-08-19 08:35:43 +02002623 return &pat->pat.data;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01002624
2625 return NULL;
Thierry FOURNIER55d0b102014-01-15 11:25:26 +01002626}
2627
Willy Tarreaua5bbaaf2020-10-30 16:03:50 +01002628/* This function compares two pat_ref** on their unique_id, and returns -1/0/1
2629 * depending on their order (suitable for sorting).
2630 */
Carl Henrik Lundef91ac192020-02-27 16:45:50 +01002631static int cmp_pat_ref(const void *_a, const void *_b)
2632{
2633 struct pat_ref * const *a = _a;
2634 struct pat_ref * const *b = _b;
2635
2636 if ((*a)->unique_id < (*b)->unique_id)
2637 return -1;
2638 else if ((*a)->unique_id > (*b)->unique_id)
2639 return 1;
2640 return 0;
2641}
2642
Willy Tarreaua5bbaaf2020-10-30 16:03:50 +01002643/* This function finalizes the configuration parsing. It sets all the
2644 * automatic ids.
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002645 */
Carl Henrik Lundef91ac192020-02-27 16:45:50 +01002646int pattern_finalize_config(void)
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002647{
Tim Duesterhusb584b442020-03-17 21:08:24 +01002648 size_t len = 0;
2649 size_t unassigned_pos = 0;
Carl Henrik Lundef91ac192020-02-27 16:45:50 +01002650 int next_unique_id = 0;
Tim Duesterhusb584b442020-03-17 21:08:24 +01002651 size_t i, j;
Carl Henrik Lundef91ac192020-02-27 16:45:50 +01002652 struct pat_ref *ref, **arr;
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002653 struct list pr = LIST_HEAD_INIT(pr);
2654
Willy Tarreau52bf8392020-03-08 00:42:37 +01002655 pat_lru_seed = ha_random();
Willy Tarreauf3045d22015-04-29 16:24:50 +02002656
Carl Henrik Lundef91ac192020-02-27 16:45:50 +01002657 /* Count pat_refs with user defined unique_id and totalt count */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002658 list_for_each_entry(ref, &pattern_reference, list) {
Carl Henrik Lundef91ac192020-02-27 16:45:50 +01002659 len++;
2660 if (ref->unique_id != -1)
2661 unassigned_pos++;
2662 }
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002663
Tim Duesterhusb584b442020-03-17 21:08:24 +01002664 if (len == 0) {
2665 return 0;
2666 }
2667
Carl Henrik Lundef91ac192020-02-27 16:45:50 +01002668 arr = calloc(len, sizeof(*arr));
2669 if (arr == NULL) {
2670 ha_alert("Out of memory error.\n");
2671 return ERR_ALERT | ERR_FATAL;
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002672 }
2673
Carl Henrik Lundef91ac192020-02-27 16:45:50 +01002674 i = 0;
2675 j = unassigned_pos;
2676 list_for_each_entry(ref, &pattern_reference, list) {
2677 if (ref->unique_id != -1)
2678 arr[i++] = ref;
2679 else
2680 arr[j++] = ref;
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002681 }
2682
Carl Henrik Lundef91ac192020-02-27 16:45:50 +01002683 /* Sort first segment of array with user-defined unique ids for
2684 * fast lookup when generating unique ids
2685 */
2686 qsort(arr, unassigned_pos, sizeof(*arr), cmp_pat_ref);
2687
2688 /* Assign unique ids to the rest of the elements */
2689 for (i = unassigned_pos; i < len; i++) {
2690 do {
2691 arr[i]->unique_id = next_unique_id++;
2692 } while (bsearch(&arr[i], arr, unassigned_pos, sizeof(*arr), cmp_pat_ref));
2693 }
2694
2695 /* Sort complete array */
2696 qsort(arr, len, sizeof(*arr), cmp_pat_ref);
2697
2698 /* Convert back to linked list */
2699 for (i = 0; i < len; i++)
2700 LIST_ADDQ(&pr, &arr[i]->list);
2701
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002702 /* swap root */
2703 LIST_ADD(&pr, &pattern_reference);
2704 LIST_DEL(&pr);
Carl Henrik Lundef91ac192020-02-27 16:45:50 +01002705
2706 free(arr);
2707 return 0;
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002708}
Willy Tarreau403bfbb2019-10-23 06:59:31 +02002709
2710static int pattern_per_thread_lru_alloc()
2711{
2712 if (!global.tune.pattern_cache)
2713 return 1;
2714 pat_lru_tree = lru64_new(global.tune.pattern_cache);
2715 return !!pat_lru_tree;
2716}
2717
2718static void pattern_per_thread_lru_free()
2719{
2720 lru64_destroy(pat_lru_tree);
2721}
2722
2723REGISTER_PER_THREAD_ALLOC(pattern_per_thread_lru_alloc);
2724REGISTER_PER_THREAD_FREE(pattern_per_thread_lru_free);