blob: 39ecd9517b290569220663379bbf05046a11afed [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>
15
16#include <common/config.h>
17#include <common/standard.h>
18
19#include <types/global.h>
20#include <types/pattern.h>
21
Thierry FOURNIER46006bd2014-03-21 21:45:15 +010022#include <proto/log.h>
Thierry FOURNIERed66c292013-11-28 11:05:19 +010023#include <proto/pattern.h>
Thierry FOURNIERe3ded592013-12-06 15:36:54 +010024#include <proto/sample.h>
Thierry FOURNIERed66c292013-11-28 11:05:19 +010025
26#include <ebsttree.h>
Willy Tarreauf3045d22015-04-29 16:24:50 +020027#include <import/lru.h>
28#include <import/xxhash.h>
Thierry FOURNIERed66c292013-11-28 11:05:19 +010029
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010030char *pat_match_names[PAT_MATCH_NUM] = {
31 [PAT_MATCH_FOUND] = "found",
32 [PAT_MATCH_BOOL] = "bool",
33 [PAT_MATCH_INT] = "int",
34 [PAT_MATCH_IP] = "ip",
35 [PAT_MATCH_BIN] = "bin",
36 [PAT_MATCH_LEN] = "len",
37 [PAT_MATCH_STR] = "str",
38 [PAT_MATCH_BEG] = "beg",
39 [PAT_MATCH_SUB] = "sub",
40 [PAT_MATCH_DIR] = "dir",
41 [PAT_MATCH_DOM] = "dom",
42 [PAT_MATCH_END] = "end",
43 [PAT_MATCH_REG] = "reg",
Thierry Fournier8feaa662016-02-10 22:55:20 +010044 [PAT_MATCH_REGM] = "regm",
Thierry FOURNIERed66c292013-11-28 11:05:19 +010045};
46
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +020047int (*pat_parse_fcts[PAT_MATCH_NUM])(const char *, struct pattern *, int, char **) = {
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010048 [PAT_MATCH_FOUND] = pat_parse_nothing,
49 [PAT_MATCH_BOOL] = pat_parse_nothing,
50 [PAT_MATCH_INT] = pat_parse_int,
51 [PAT_MATCH_IP] = pat_parse_ip,
52 [PAT_MATCH_BIN] = pat_parse_bin,
Thierry FOURNIER5d344082014-01-27 14:19:53 +010053 [PAT_MATCH_LEN] = pat_parse_int,
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010054 [PAT_MATCH_STR] = pat_parse_str,
55 [PAT_MATCH_BEG] = pat_parse_str,
56 [PAT_MATCH_SUB] = pat_parse_str,
57 [PAT_MATCH_DIR] = pat_parse_str,
58 [PAT_MATCH_DOM] = pat_parse_str,
59 [PAT_MATCH_END] = pat_parse_str,
60 [PAT_MATCH_REG] = pat_parse_reg,
Thierry Fournier8feaa662016-02-10 22:55:20 +010061 [PAT_MATCH_REGM] = pat_parse_reg,
Thierry FOURNIERed66c292013-11-28 11:05:19 +010062};
63
Thierry FOURNIERb9b08462013-12-13 15:12:32 +010064int (*pat_index_fcts[PAT_MATCH_NUM])(struct pattern_expr *, struct pattern *, char **) = {
65 [PAT_MATCH_FOUND] = pat_idx_list_val,
66 [PAT_MATCH_BOOL] = pat_idx_list_val,
67 [PAT_MATCH_INT] = pat_idx_list_val,
68 [PAT_MATCH_IP] = pat_idx_tree_ip,
69 [PAT_MATCH_BIN] = pat_idx_list_ptr,
70 [PAT_MATCH_LEN] = pat_idx_list_val,
71 [PAT_MATCH_STR] = pat_idx_tree_str,
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +020072 [PAT_MATCH_BEG] = pat_idx_tree_pfx,
Thierry FOURNIERb9b08462013-12-13 15:12:32 +010073 [PAT_MATCH_SUB] = pat_idx_list_str,
74 [PAT_MATCH_DIR] = pat_idx_list_str,
75 [PAT_MATCH_DOM] = pat_idx_list_str,
76 [PAT_MATCH_END] = pat_idx_list_str,
77 [PAT_MATCH_REG] = pat_idx_list_reg,
Thierry Fournier8feaa662016-02-10 22:55:20 +010078 [PAT_MATCH_REGM] = pat_idx_list_regm,
Thierry FOURNIERb9b08462013-12-13 15:12:32 +010079};
80
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +010081void (*pat_delete_fcts[PAT_MATCH_NUM])(struct pattern_expr *, struct pat_ref_elt *) = {
Thierry FOURNIERb1136502014-01-15 11:38:49 +010082 [PAT_MATCH_FOUND] = pat_del_list_val,
83 [PAT_MATCH_BOOL] = pat_del_list_val,
84 [PAT_MATCH_INT] = pat_del_list_val,
85 [PAT_MATCH_IP] = pat_del_tree_ip,
86 [PAT_MATCH_BIN] = pat_del_list_ptr,
87 [PAT_MATCH_LEN] = pat_del_list_val,
88 [PAT_MATCH_STR] = pat_del_tree_str,
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +020089 [PAT_MATCH_BEG] = pat_del_tree_str,
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +010090 [PAT_MATCH_SUB] = pat_del_list_ptr,
91 [PAT_MATCH_DIR] = pat_del_list_ptr,
92 [PAT_MATCH_DOM] = pat_del_list_ptr,
93 [PAT_MATCH_END] = pat_del_list_ptr,
Thierry FOURNIERb1136502014-01-15 11:38:49 +010094 [PAT_MATCH_REG] = pat_del_list_reg,
Thierry Fournier8feaa662016-02-10 22:55:20 +010095 [PAT_MATCH_REGM] = pat_del_list_reg,
Thierry FOURNIERb1136502014-01-15 11:38:49 +010096};
97
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +010098void (*pat_prune_fcts[PAT_MATCH_NUM])(struct pattern_expr *) = {
99 [PAT_MATCH_FOUND] = pat_prune_val,
100 [PAT_MATCH_BOOL] = pat_prune_val,
101 [PAT_MATCH_INT] = pat_prune_val,
102 [PAT_MATCH_IP] = pat_prune_val,
103 [PAT_MATCH_BIN] = pat_prune_ptr,
104 [PAT_MATCH_LEN] = pat_prune_val,
105 [PAT_MATCH_STR] = pat_prune_ptr,
106 [PAT_MATCH_BEG] = pat_prune_ptr,
107 [PAT_MATCH_SUB] = pat_prune_ptr,
108 [PAT_MATCH_DIR] = pat_prune_ptr,
109 [PAT_MATCH_DOM] = pat_prune_ptr,
110 [PAT_MATCH_END] = pat_prune_ptr,
111 [PAT_MATCH_REG] = pat_prune_reg,
Thierry Fournier8feaa662016-02-10 22:55:20 +0100112 [PAT_MATCH_REGM] = pat_prune_reg,
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +0100113};
114
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100115struct pattern *(*pat_match_fcts[PAT_MATCH_NUM])(struct sample *, struct pattern_expr *, int) = {
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100116 [PAT_MATCH_FOUND] = NULL,
117 [PAT_MATCH_BOOL] = pat_match_nothing,
118 [PAT_MATCH_INT] = pat_match_int,
119 [PAT_MATCH_IP] = pat_match_ip,
120 [PAT_MATCH_BIN] = pat_match_bin,
121 [PAT_MATCH_LEN] = pat_match_len,
122 [PAT_MATCH_STR] = pat_match_str,
123 [PAT_MATCH_BEG] = pat_match_beg,
124 [PAT_MATCH_SUB] = pat_match_sub,
125 [PAT_MATCH_DIR] = pat_match_dir,
126 [PAT_MATCH_DOM] = pat_match_dom,
127 [PAT_MATCH_END] = pat_match_end,
128 [PAT_MATCH_REG] = pat_match_reg,
Thierry Fournier8feaa662016-02-10 22:55:20 +0100129 [PAT_MATCH_REGM] = pat_match_regm,
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100130};
131
Thierry FOURNIERe3ded592013-12-06 15:36:54 +0100132/* Just used for checking configuration compatibility */
133int pat_match_types[PAT_MATCH_NUM] = {
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200134 [PAT_MATCH_FOUND] = SMP_T_SINT,
135 [PAT_MATCH_BOOL] = SMP_T_SINT,
136 [PAT_MATCH_INT] = SMP_T_SINT,
Thierry FOURNIERe3ded592013-12-06 15:36:54 +0100137 [PAT_MATCH_IP] = SMP_T_ADDR,
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100138 [PAT_MATCH_BIN] = SMP_T_BIN,
139 [PAT_MATCH_LEN] = SMP_T_STR,
140 [PAT_MATCH_STR] = SMP_T_STR,
141 [PAT_MATCH_BEG] = SMP_T_STR,
142 [PAT_MATCH_SUB] = SMP_T_STR,
143 [PAT_MATCH_DIR] = SMP_T_STR,
144 [PAT_MATCH_DOM] = SMP_T_STR,
145 [PAT_MATCH_END] = SMP_T_STR,
146 [PAT_MATCH_REG] = SMP_T_STR,
Thierry Fournier8feaa662016-02-10 22:55:20 +0100147 [PAT_MATCH_REGM] = SMP_T_STR,
Thierry FOURNIERe3ded592013-12-06 15:36:54 +0100148};
149
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +0100150/* this struct is used to return information */
Emeric Brunb5997f72017-07-03 11:34:05 +0200151static THREAD_LOCAL struct pattern static_pattern;
152static THREAD_LOCAL struct sample_data static_sample_data;
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +0100153
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100154/* This is the root of the list of all pattern_ref avalaibles. */
155struct list pattern_reference = LIST_HEAD_INIT(pattern_reference);
156
Willy Tarreauf3045d22015-04-29 16:24:50 +0200157static struct lru64_head *pat_lru_tree;
Emeric Brunb5997f72017-07-03 11:34:05 +0200158#ifdef USE_THREAD
159HA_SPINLOCK_T pat_lru_tree_lock;
160#endif
Willy Tarreauf3045d22015-04-29 16:24:50 +0200161static unsigned long long pat_lru_seed;
162
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100163/*
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100164 *
165 * The following functions are not exported and are used by internals process
166 * of pattern matching
167 *
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100168 */
169
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100170/* Background: Fast way to find a zero byte in a word
171 * http://graphics.stanford.edu/~seander/bithacks.html#ZeroInWord
172 * hasZeroByte = (v - 0x01010101UL) & ~v & 0x80808080UL;
173 *
174 * To look for 4 different byte values, xor the word with those bytes and
175 * then check for zero bytes:
176 *
177 * v = (((unsigned char)c * 0x1010101U) ^ delimiter)
178 * where <delimiter> is the 4 byte values to look for (as an uint)
179 * and <c> is the character that is being tested
180 */
181static inline unsigned int is_delimiter(unsigned char c, unsigned int mask)
182{
183 mask ^= (c * 0x01010101); /* propagate the char to all 4 bytes */
184 return (mask - 0x01010101) & ~mask & 0x80808080U;
185}
186
187static inline unsigned int make_4delim(unsigned char d1, unsigned char d2, unsigned char d3, unsigned char d4)
188{
189 return d1 << 24 | d2 << 16 | d3 << 8 | d4;
190}
191
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100192
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100193/*
194 *
195 * These functions are exported and may be used by any other component.
196 *
Willy Tarreau5def8ef2014-08-29 15:19:33 +0200197 * The following functions are used for parsing pattern matching input value.
198 * The <text> contain the string to be parsed. <pattern> must be a preallocated
199 * pattern. The pat_parse_* functions fill this structure with the parsed value.
200 * <err> is filled with an error message built with memprintf() function. It is
201 * allowed to use a trash as a temporary storage for the returned pattern, as
202 * the next call after these functions will be pat_idx_*.
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100203 *
Willy Tarreau5def8ef2014-08-29 15:19:33 +0200204 * In success case, the pat_parse_* function returns 1. If the function
205 * fails, it returns 0 and <err> is filled.
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100206 */
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100207
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100208/* ignore the current line */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200209int pat_parse_nothing(const char *text, struct pattern *pattern, int mflags, char **err)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100210{
211 return 1;
212}
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100213
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100214/* Parse a string. It is allocated and duplicated. */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200215int pat_parse_str(const char *text, struct pattern *pattern, int mflags, char **err)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100216{
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100217 pattern->type = SMP_T_STR;
Thierry FOURNIERedc15c32013-12-13 15:36:59 +0100218 pattern->ptr.str = (char *)text;
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100219 pattern->len = strlen(text);
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100220 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100221}
222
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100223/* Parse a binary written in hexa. It is allocated. */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200224int pat_parse_bin(const char *text, struct pattern *pattern, int mflags, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100225{
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100226 struct chunk *trash;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100227
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100228 pattern->type = SMP_T_BIN;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100229 trash = get_trash_chunk();
230 pattern->len = trash->size;
231 pattern->ptr.str = trash->str;
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100232 return !!parse_binary(text, &pattern->ptr.str, &pattern->len, err);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100233}
234
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100235/* Parse a regex. It is allocated. */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200236int pat_parse_reg(const char *text, struct pattern *pattern, int mflags, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100237{
Thierry FOURNIER0b6d15f2014-01-29 19:35:16 +0100238 pattern->ptr.str = (char *)text;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100239 return 1;
240}
241
242/* Parse a range of positive integers delimited by either ':' or '-'. If only
243 * one integer is read, it is set as both min and max. An operator may be
244 * specified as the prefix, among this list of 5 :
245 *
246 * 0:eq, 1:gt, 2:ge, 3:lt, 4:le
247 *
248 * The default operator is "eq". It supports range matching. Ranges are
249 * rejected for other operators. The operator may be changed at any time.
250 * The operator is stored in the 'opaque' argument.
251 *
252 * If err is non-NULL, an error message will be returned there on errors and
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100253 * the caller will have to free it. The function returns zero on error, and
254 * non-zero on success.
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100255 *
256 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200257int pat_parse_int(const char *text, struct pattern *pattern, int mflags, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100258{
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100259 const char *ptr = text;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100260
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200261 pattern->type = SMP_T_SINT;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100262
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100263 /* Empty string is not valid */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100264 if (!*text)
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100265 goto not_valid_range;
266
267 /* Search ':' or '-' separator. */
268 while (*ptr != '\0' && *ptr != ':' && *ptr != '-')
269 ptr++;
270
271 /* If separator not found. */
272 if (!*ptr) {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100273 if (strl2llrc(text, ptr - text, &pattern->val.range.min) != 0) {
274 memprintf(err, "'%s' is not a number", text);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100275 return 0;
276 }
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100277 pattern->val.range.max = pattern->val.range.min;
278 pattern->val.range.min_set = 1;
279 pattern->val.range.max_set = 1;
280 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100281 }
282
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100283 /* If the separator is the first character. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100284 if (ptr == text && *(ptr + 1) != '\0') {
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100285 if (strl2llrc(ptr + 1, strlen(ptr + 1), &pattern->val.range.max) != 0)
286 goto not_valid_range;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100287
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100288 pattern->val.range.min_set = 0;
289 pattern->val.range.max_set = 1;
290 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100291 }
292
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100293 /* If separator is the last character. */
294 if (*(ptr + 1) == '\0') {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100295 if (strl2llrc(text, ptr - text, &pattern->val.range.min) != 0)
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100296 goto not_valid_range;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100297
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100298 pattern->val.range.min_set = 1;
299 pattern->val.range.max_set = 0;
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100300 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100301 }
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100302
303 /* Else, parse two numbers. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100304 if (strl2llrc(text, ptr - text, &pattern->val.range.min) != 0)
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100305 goto not_valid_range;
306
307 if (strl2llrc(ptr + 1, strlen(ptr + 1), &pattern->val.range.max) != 0)
308 goto not_valid_range;
309
310 if (pattern->val.range.min > pattern->val.range.max)
311 goto not_valid_range;
312
313 pattern->val.range.min_set = 1;
314 pattern->val.range.max_set = 1;
315 return 1;
316
317 not_valid_range:
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100318 memprintf(err, "'%s' is not a valid number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100319 return 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100320}
321
322/* Parse a range of positive 2-component versions delimited by either ':' or
323 * '-'. The version consists in a major and a minor, both of which must be
324 * smaller than 65536, because internally they will be represented as a 32-bit
325 * integer.
326 * If only one version is read, it is set as both min and max. Just like for
327 * pure integers, an operator may be specified as the prefix, among this list
328 * of 5 :
329 *
330 * 0:eq, 1:gt, 2:ge, 3:lt, 4:le
331 *
332 * The default operator is "eq". It supports range matching. Ranges are
333 * rejected for other operators. The operator may be changed at any time.
334 * The operator is stored in the 'opaque' argument. This allows constructs
335 * such as the following one :
336 *
337 * acl obsolete_ssl ssl_req_proto lt 3
338 * acl unsupported_ssl ssl_req_proto gt 3.1
339 * acl valid_ssl ssl_req_proto 3.0-3.1
340 *
341 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200342int pat_parse_dotted_ver(const char *text, struct pattern *pattern, int mflags, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100343{
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100344 const char *ptr = text;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100345
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200346 pattern->type = SMP_T_SINT;
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100347
348 /* Search ':' or '-' separator. */
349 while (*ptr != '\0' && *ptr != ':' && *ptr != '-')
350 ptr++;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100351
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100352 /* If separator not found. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100353 if (*ptr == '\0' && ptr > text) {
354 if (strl2llrc_dotted(text, ptr-text, &pattern->val.range.min) != 0) {
355 memprintf(err, "'%s' is not a dotted number", text);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100356 return 0;
357 }
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100358 pattern->val.range.max = pattern->val.range.min;
359 pattern->val.range.min_set = 1;
360 pattern->val.range.max_set = 1;
361 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100362 }
363
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100364 /* If the separator is the first character. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100365 if (ptr == text && *(ptr+1) != '\0') {
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100366 if (strl2llrc_dotted(ptr+1, strlen(ptr+1), &pattern->val.range.max) != 0) {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100367 memprintf(err, "'%s' is not a valid dotted number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100368 return 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100369 }
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100370 pattern->val.range.min_set = 0;
371 pattern->val.range.max_set = 1;
372 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100373 }
374
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100375 /* If separator is the last character. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100376 if (ptr == &text[strlen(text)-1]) {
377 if (strl2llrc_dotted(text, ptr-text, &pattern->val.range.min) != 0) {
378 memprintf(err, "'%s' is not a valid dotted number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100379 return 0;
380 }
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100381 pattern->val.range.min_set = 1;
382 pattern->val.range.max_set = 0;
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100383 return 1;
384 }
385
386 /* Else, parse two numbers. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100387 if (strl2llrc_dotted(text, ptr-text, &pattern->val.range.min) != 0) {
388 memprintf(err, "'%s' is not a valid dotted number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100389 return 0;
390 }
391 if (strl2llrc_dotted(ptr+1, strlen(ptr+1), &pattern->val.range.max) != 0) {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100392 memprintf(err, "'%s' is not a valid dotted number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100393 return 0;
394 }
395 if (pattern->val.range.min > pattern->val.range.max) {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100396 memprintf(err, "'%s' is not a valid dotted number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100397 return 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100398 }
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100399 pattern->val.range.min_set = 1;
400 pattern->val.range.max_set = 1;
401 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100402}
403
404/* Parse an IP address and an optional mask in the form addr[/mask].
405 * The addr may either be an IPv4 address or a hostname. The mask
406 * may either be a dotted mask or a number of bits. Returns 1 if OK,
407 * otherwise 0. NOTE: IP address patterns are typed (IPV4/IPV6).
408 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200409int pat_parse_ip(const char *text, struct pattern *pattern, int mflags, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100410{
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200411 if (str2net(text, !(mflags & PAT_MF_NO_DNS) && (global.mode & MODE_STARTING),
Thierry FOURNIERfc7ac7b2014-02-11 15:23:04 +0100412 &pattern->val.ipv4.addr, &pattern->val.ipv4.mask)) {
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100413 pattern->type = SMP_T_IPV4;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100414 return 1;
415 }
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100416 else if (str62net(text, &pattern->val.ipv6.addr, &pattern->val.ipv6.mask)) {
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100417 pattern->type = SMP_T_IPV6;
418 return 1;
419 }
420 else {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100421 memprintf(err, "'%s' is not a valid IPv4 or IPv6 address", text);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100422 return 0;
423 }
424}
425
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100426/*
427 *
428 * These functions are exported and may be used by any other component.
429 *
430 * This fucntion just take a sample <smp> and check if this sample match
431 * with the pattern <pattern>. This fucntion return just PAT_MATCH or
432 * PAT_NOMATCH.
433 *
434 */
435
436/* always return false */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100437struct pattern *pat_match_nothing(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100438{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200439 if (smp->data.u.sint) {
Thierry FOURNIERe5978bf2014-03-17 19:53:10 +0100440 if (fill) {
Thierry FOURNIER503bb092015-08-19 08:35:43 +0200441 static_pattern.data = NULL;
Thierry FOURNIERe5978bf2014-03-17 19:53:10 +0100442 static_pattern.ref = NULL;
Thierry FOURNIERe5978bf2014-03-17 19:53:10 +0100443 static_pattern.type = 0;
444 static_pattern.ptr.str = NULL;
445 }
446 return &static_pattern;
447 }
448 else
449 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100450}
451
452
453/* NB: For two strings to be identical, it is required that their lengths match */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100454struct pattern *pat_match_str(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100455{
456 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100457 struct ebmb_node *node;
458 char prev;
459 struct pattern_tree *elt;
460 struct pattern_list *lst;
461 struct pattern *pattern;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200462 struct pattern *ret = NULL;
463 struct lru64 *lru = NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100464
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100465 /* Lookup a string in the expression's pattern tree. */
466 if (!eb_is_empty(&expr->pattern_tree)) {
467 /* we may have to force a trailing zero on the test pattern */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200468 prev = smp->data.u.str.str[smp->data.u.str.len];
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100469 if (prev)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200470 smp->data.u.str.str[smp->data.u.str.len] = '\0';
471 node = ebst_lookup(&expr->pattern_tree, smp->data.u.str.str);
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100472 if (prev)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200473 smp->data.u.str.str[smp->data.u.str.len] = prev;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100474
475 if (node) {
476 if (fill) {
477 elt = ebmb_entry(node, struct pattern_tree, node);
Thierry FOURNIER503bb092015-08-19 08:35:43 +0200478 static_pattern.data = elt->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +0100479 static_pattern.ref = elt->ref;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200480 static_pattern.sflags = PAT_SF_TREE;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100481 static_pattern.type = SMP_T_STR;
482 static_pattern.ptr.str = (char *)elt->node.key;
483 }
484 return &static_pattern;
485 }
486 }
487
488 /* look in the list */
Willy Tarreauf3045d22015-04-29 16:24:50 +0200489 if (pat_lru_tree) {
Willy Tarreauaee93142015-05-04 17:18:42 +0200490 unsigned long long seed = pat_lru_seed ^ (long)expr;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200491
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100492 HA_SPIN_LOCK(PATLRU_LOCK, &pat_lru_tree_lock);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200493 lru = lru64_get(XXH64(smp->data.u.str.str, smp->data.u.str.len, seed),
Willy Tarreauf3045d22015-04-29 16:24:50 +0200494 pat_lru_tree, expr, expr->revision);
Emeric Brunb5997f72017-07-03 11:34:05 +0200495 if (!lru) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100496 HA_SPIN_UNLOCK(PATLRU_LOCK, &pat_lru_tree_lock);
Emeric Brunb5997f72017-07-03 11:34:05 +0200497 }
498 else if (lru->domain) {
499 ret = lru->data;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100500 HA_SPIN_UNLOCK(PATLRU_LOCK, &pat_lru_tree_lock);
Emeric Brunb5997f72017-07-03 11:34:05 +0200501 return ret;
502 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200503 }
504
Emeric Brunb5997f72017-07-03 11:34:05 +0200505
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100506 list_for_each_entry(lst, &expr->patterns, list) {
507 pattern = &lst->pat;
508
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200509 if (pattern->len != smp->data.u.str.len)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100510 continue;
511
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200512 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200513 if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.str.str, smp->data.u.str.len) == 0) ||
514 (!icase && strncmp(pattern->ptr.str, smp->data.u.str.str, smp->data.u.str.len) == 0)) {
Willy Tarreauf3045d22015-04-29 16:24:50 +0200515 ret = pattern;
516 break;
517 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100518 }
519
Emeric Brunb5997f72017-07-03 11:34:05 +0200520 if (lru) {
521 lru64_commit(lru, ret, expr, expr->revision, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100522 HA_SPIN_UNLOCK(PATLRU_LOCK, &pat_lru_tree_lock);
Emeric Brunb5997f72017-07-03 11:34:05 +0200523 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200524
525 return ret;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100526}
527
528/* NB: For two binaries buf to be identical, it is required that their lengths match */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100529struct pattern *pat_match_bin(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100530{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100531 struct pattern_list *lst;
532 struct pattern *pattern;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200533 struct pattern *ret = NULL;
534 struct lru64 *lru = NULL;
535
536 if (pat_lru_tree) {
Willy Tarreauaee93142015-05-04 17:18:42 +0200537 unsigned long long seed = pat_lru_seed ^ (long)expr;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100538
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100539 HA_SPIN_LOCK(PATLRU_LOCK, &pat_lru_tree_lock);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200540 lru = lru64_get(XXH64(smp->data.u.str.str, smp->data.u.str.len, seed),
Willy Tarreauf3045d22015-04-29 16:24:50 +0200541 pat_lru_tree, expr, expr->revision);
Emeric Brunb5997f72017-07-03 11:34:05 +0200542 if (!lru) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100543 HA_SPIN_UNLOCK(PATLRU_LOCK, &pat_lru_tree_lock);
Emeric Brunb5997f72017-07-03 11:34:05 +0200544 }
545 else if (lru->domain) {
546 ret = lru->data;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100547 HA_SPIN_UNLOCK(PATLRU_LOCK, &pat_lru_tree_lock);
Emeric Brunb5997f72017-07-03 11:34:05 +0200548 return ret;
549 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200550 }
551
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100552 list_for_each_entry(lst, &expr->patterns, list) {
553 pattern = &lst->pat;
554
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200555 if (pattern->len != smp->data.u.str.len)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100556 continue;
557
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200558 if (memcmp(pattern->ptr.str, smp->data.u.str.str, smp->data.u.str.len) == 0) {
Willy Tarreauf3045d22015-04-29 16:24:50 +0200559 ret = pattern;
560 break;
561 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100562 }
563
Emeric Brunb5997f72017-07-03 11:34:05 +0200564 if (lru) {
565 lru64_commit(lru, ret, expr, expr->revision, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100566 HA_SPIN_UNLOCK(PATLRU_LOCK, &pat_lru_tree_lock);
Emeric Brunb5997f72017-07-03 11:34:05 +0200567 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200568
569 return ret;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100570}
571
572/* Executes a regex. It temporarily changes the data to add a trailing zero,
Thierry Fournier8feaa662016-02-10 22:55:20 +0100573 * and restores the previous character when leaving. This function fills
574 * a matching array.
575 */
576struct pattern *pat_match_regm(struct sample *smp, struct pattern_expr *expr, int fill)
577{
578 struct pattern_list *lst;
579 struct pattern *pattern;
580 struct pattern *ret = NULL;
581
582 list_for_each_entry(lst, &expr->patterns, list) {
583 pattern = &lst->pat;
584
585 if (regex_exec_match2(pattern->ptr.reg, smp->data.u.str.str, smp->data.u.str.len,
586 MAX_MATCH, pmatch, 0)) {
587 ret = pattern;
588 smp->ctx.a[0] = pmatch;
589 break;
590 }
591 }
592
593 return ret;
594}
595
596/* Executes a regex. It temporarily changes the data to add a trailing zero,
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100597 * and restores the previous character when leaving.
598 */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100599struct pattern *pat_match_reg(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100600{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100601 struct pattern_list *lst;
602 struct pattern *pattern;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200603 struct pattern *ret = NULL;
604 struct lru64 *lru = NULL;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100605
Willy Tarreauf3045d22015-04-29 16:24:50 +0200606 if (pat_lru_tree) {
Willy Tarreauaee93142015-05-04 17:18:42 +0200607 unsigned long long seed = pat_lru_seed ^ (long)expr;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200608
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100609 HA_SPIN_LOCK(PATLRU_LOCK, &pat_lru_tree_lock);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200610 lru = lru64_get(XXH64(smp->data.u.str.str, smp->data.u.str.len, seed),
Willy Tarreauf3045d22015-04-29 16:24:50 +0200611 pat_lru_tree, expr, expr->revision);
Emeric Brunb5997f72017-07-03 11:34:05 +0200612 if (!lru) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100613 HA_SPIN_UNLOCK(PATLRU_LOCK, &pat_lru_tree_lock);
Emeric Brunb5997f72017-07-03 11:34:05 +0200614 }
615 else if (lru->domain) {
616 ret = lru->data;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100617 HA_SPIN_UNLOCK(PATLRU_LOCK, &pat_lru_tree_lock);
Emeric Brunb5997f72017-07-03 11:34:05 +0200618 return ret;
619 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200620 }
621
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100622 list_for_each_entry(lst, &expr->patterns, list) {
623 pattern = &lst->pat;
624
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200625 if (regex_exec2(pattern->ptr.reg, smp->data.u.str.str, smp->data.u.str.len)) {
Willy Tarreauf3045d22015-04-29 16:24:50 +0200626 ret = pattern;
627 break;
628 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100629 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200630
Emeric Brunb5997f72017-07-03 11:34:05 +0200631 if (lru) {
632 lru64_commit(lru, ret, expr, expr->revision, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100633 HA_SPIN_UNLOCK(PATLRU_LOCK, &pat_lru_tree_lock);
Emeric Brunb5997f72017-07-03 11:34:05 +0200634 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200635
636 return ret;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100637}
638
639/* Checks that the pattern matches the beginning of the tested string. */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100640struct pattern *pat_match_beg(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100641{
642 int icase;
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +0200643 struct ebmb_node *node;
644 char prev;
645 struct pattern_tree *elt;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100646 struct pattern_list *lst;
647 struct pattern *pattern;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200648 struct pattern *ret = NULL;
649 struct lru64 *lru = NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100650
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +0200651 /* Lookup a string in the expression's pattern tree. */
652 if (!eb_is_empty(&expr->pattern_tree)) {
653 /* we may have to force a trailing zero on the test pattern */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200654 prev = smp->data.u.str.str[smp->data.u.str.len];
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +0200655 if (prev)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200656 smp->data.u.str.str[smp->data.u.str.len] = '\0';
657 node = ebmb_lookup_longest(&expr->pattern_tree, smp->data.u.str.str);
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +0200658 if (prev)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200659 smp->data.u.str.str[smp->data.u.str.len] = prev;
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +0200660
661 if (node) {
662 if (fill) {
663 elt = ebmb_entry(node, struct pattern_tree, node);
Thierry FOURNIER503bb092015-08-19 08:35:43 +0200664 static_pattern.data = elt->data;
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +0200665 static_pattern.ref = elt->ref;
666 static_pattern.sflags = PAT_SF_TREE;
667 static_pattern.type = SMP_T_STR;
668 static_pattern.ptr.str = (char *)elt->node.key;
669 }
670 return &static_pattern;
671 }
672 }
673
674 /* look in the list */
Willy Tarreauf3045d22015-04-29 16:24:50 +0200675 if (pat_lru_tree) {
Willy Tarreauaee93142015-05-04 17:18:42 +0200676 unsigned long long seed = pat_lru_seed ^ (long)expr;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200677
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100678 HA_SPIN_LOCK(PATLRU_LOCK, &pat_lru_tree_lock);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200679 lru = lru64_get(XXH64(smp->data.u.str.str, smp->data.u.str.len, seed),
Willy Tarreauf3045d22015-04-29 16:24:50 +0200680 pat_lru_tree, expr, expr->revision);
Emeric Brunb5997f72017-07-03 11:34:05 +0200681 if (!lru) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100682 HA_SPIN_UNLOCK(PATLRU_LOCK, &pat_lru_tree_lock);
Emeric Brunb5997f72017-07-03 11:34:05 +0200683 }
684 else if (lru->domain) {
685 ret = lru->data;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100686 HA_SPIN_UNLOCK(PATLRU_LOCK, &pat_lru_tree_lock);
Emeric Brunb5997f72017-07-03 11:34:05 +0200687 return ret;
688 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200689 }
690
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100691 list_for_each_entry(lst, &expr->patterns, list) {
692 pattern = &lst->pat;
693
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200694 if (pattern->len > smp->data.u.str.len)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100695 continue;
696
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200697 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200698 if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.str.str, pattern->len) != 0) ||
699 (!icase && strncmp(pattern->ptr.str, smp->data.u.str.str, pattern->len) != 0))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100700 continue;
701
Willy Tarreauf3045d22015-04-29 16:24:50 +0200702 ret = pattern;
703 break;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100704 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200705
Emeric Brunb5997f72017-07-03 11:34:05 +0200706 if (lru) {
707 lru64_commit(lru, ret, expr, expr->revision, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100708 HA_SPIN_UNLOCK(PATLRU_LOCK, &pat_lru_tree_lock);
Emeric Brunb5997f72017-07-03 11:34:05 +0200709 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200710
711 return ret;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100712}
713
714/* Checks that the pattern matches the end of the tested string. */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100715struct pattern *pat_match_end(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100716{
717 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100718 struct pattern_list *lst;
719 struct pattern *pattern;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200720 struct pattern *ret = NULL;
721 struct lru64 *lru = NULL;
722
723 if (pat_lru_tree) {
Willy Tarreauaee93142015-05-04 17:18:42 +0200724 unsigned long long seed = pat_lru_seed ^ (long)expr;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200725
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100726 HA_SPIN_LOCK(PATLRU_LOCK, &pat_lru_tree_lock);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200727 lru = lru64_get(XXH64(smp->data.u.str.str, smp->data.u.str.len, seed),
Willy Tarreauf3045d22015-04-29 16:24:50 +0200728 pat_lru_tree, expr, expr->revision);
Emeric Brunb5997f72017-07-03 11:34:05 +0200729 if (!lru) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100730 HA_SPIN_UNLOCK(PATLRU_LOCK, &pat_lru_tree_lock);
Emeric Brunb5997f72017-07-03 11:34:05 +0200731 }
732 else if (lru->domain) {
733 ret = lru->data;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100734 HA_SPIN_UNLOCK(PATLRU_LOCK, &pat_lru_tree_lock);
Emeric Brunb5997f72017-07-03 11:34:05 +0200735 return ret;
736 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200737 }
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100738
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100739 list_for_each_entry(lst, &expr->patterns, list) {
740 pattern = &lst->pat;
741
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200742 if (pattern->len > smp->data.u.str.len)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100743 continue;
744
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200745 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200746 if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.str.str + smp->data.u.str.len - pattern->len, pattern->len) != 0) ||
747 (!icase && strncmp(pattern->ptr.str, smp->data.u.str.str + smp->data.u.str.len - pattern->len, pattern->len) != 0))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100748 continue;
749
Willy Tarreauf3045d22015-04-29 16:24:50 +0200750 ret = pattern;
751 break;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100752 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200753
Emeric Brunb5997f72017-07-03 11:34:05 +0200754 if (lru) {
755 lru64_commit(lru, ret, expr, expr->revision, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100756 HA_SPIN_UNLOCK(PATLRU_LOCK, &pat_lru_tree_lock);
Emeric Brunb5997f72017-07-03 11:34:05 +0200757 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200758
759 return ret;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100760}
761
762/* Checks that the pattern is included inside the tested string.
763 * NB: Suboptimal, should be rewritten using a Boyer-Moore method.
764 */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100765struct pattern *pat_match_sub(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100766{
767 int icase;
768 char *end;
769 char *c;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100770 struct pattern_list *lst;
771 struct pattern *pattern;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200772 struct pattern *ret = NULL;
773 struct lru64 *lru = NULL;
774
775 if (pat_lru_tree) {
Willy Tarreauaee93142015-05-04 17:18:42 +0200776 unsigned long long seed = pat_lru_seed ^ (long)expr;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200777
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100778 HA_SPIN_LOCK(PATLRU_LOCK, &pat_lru_tree_lock);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200779 lru = lru64_get(XXH64(smp->data.u.str.str, smp->data.u.str.len, seed),
Willy Tarreauf3045d22015-04-29 16:24:50 +0200780 pat_lru_tree, expr, expr->revision);
Emeric Brunb5997f72017-07-03 11:34:05 +0200781 if (!lru) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100782 HA_SPIN_UNLOCK(PATLRU_LOCK, &pat_lru_tree_lock);
Emeric Brunb5997f72017-07-03 11:34:05 +0200783 }
784 else if (lru->domain) {
785 ret = lru->data;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100786 HA_SPIN_UNLOCK(PATLRU_LOCK, &pat_lru_tree_lock);
Emeric Brunb5997f72017-07-03 11:34:05 +0200787 return ret;
788 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200789 }
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100790
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100791 list_for_each_entry(lst, &expr->patterns, list) {
792 pattern = &lst->pat;
793
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200794 if (pattern->len > smp->data.u.str.len)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100795 continue;
796
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200797 end = smp->data.u.str.str + smp->data.u.str.len - pattern->len;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200798 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100799 if (icase) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200800 for (c = smp->data.u.str.str; c <= end; c++) {
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100801 if (tolower(*c) != tolower(*pattern->ptr.str))
802 continue;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200803 if (strncasecmp(pattern->ptr.str, c, pattern->len) == 0) {
804 ret = pattern;
805 goto leave;
806 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100807 }
808 } else {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200809 for (c = smp->data.u.str.str; c <= end; c++) {
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100810 if (*c != *pattern->ptr.str)
811 continue;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200812 if (strncmp(pattern->ptr.str, c, pattern->len) == 0) {
813 ret = pattern;
814 goto leave;
815 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100816 }
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100817 }
818 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200819 leave:
Emeric Brunb5997f72017-07-03 11:34:05 +0200820 if (lru) {
821 lru64_commit(lru, ret, expr, expr->revision, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100822 HA_SPIN_UNLOCK(PATLRU_LOCK, &pat_lru_tree_lock);
Emeric Brunb5997f72017-07-03 11:34:05 +0200823 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200824
825 return ret;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100826}
827
828/* This one is used by other real functions. It checks that the pattern is
829 * included inside the tested string, but enclosed between the specified
830 * delimiters or at the beginning or end of the string. The delimiters are
831 * provided as an unsigned int made by make_4delim() and match up to 4 different
832 * delimiters. Delimiters are stripped at the beginning and end of the pattern.
833 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200834static int match_word(struct sample *smp, struct pattern *pattern, int mflags, unsigned int delimiters)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100835{
836 int may_match, icase;
837 char *c, *end;
838 char *ps;
839 int pl;
840
841 pl = pattern->len;
842 ps = pattern->ptr.str;
843
844 while (pl > 0 && is_delimiter(*ps, delimiters)) {
845 pl--;
846 ps++;
847 }
848
849 while (pl > 0 && is_delimiter(ps[pl - 1], delimiters))
850 pl--;
851
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200852 if (pl > smp->data.u.str.len)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100853 return PAT_NOMATCH;
854
855 may_match = 1;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200856 icase = mflags & PAT_MF_IGNORE_CASE;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200857 end = smp->data.u.str.str + smp->data.u.str.len - pl;
858 for (c = smp->data.u.str.str; c <= end; c++) {
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100859 if (is_delimiter(*c, delimiters)) {
860 may_match = 1;
861 continue;
862 }
863
864 if (!may_match)
865 continue;
866
867 if (icase) {
868 if ((tolower(*c) == tolower(*ps)) &&
869 (strncasecmp(ps, c, pl) == 0) &&
870 (c == end || is_delimiter(c[pl], delimiters)))
871 return PAT_MATCH;
872 } else {
873 if ((*c == *ps) &&
874 (strncmp(ps, c, pl) == 0) &&
875 (c == end || is_delimiter(c[pl], delimiters)))
876 return PAT_MATCH;
877 }
878 may_match = 0;
879 }
880 return PAT_NOMATCH;
881}
882
883/* Checks that the pattern is included inside the tested string, but enclosed
884 * between the delimiters '?' or '/' or at the beginning or end of the string.
885 * Delimiters at the beginning or end of the pattern are ignored.
886 */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100887struct pattern *pat_match_dir(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100888{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100889 struct pattern_list *lst;
890 struct pattern *pattern;
891
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100892 list_for_each_entry(lst, &expr->patterns, list) {
893 pattern = &lst->pat;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200894 if (match_word(smp, pattern, expr->mflags, make_4delim('/', '?', '?', '?')))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100895 return pattern;
896 }
897 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100898}
899
900/* Checks that the pattern is included inside the tested string, but enclosed
901 * between the delmiters '/', '?', '.' or ":" or at the beginning or end of
902 * the string. Delimiters at the beginning or end of the pattern are ignored.
903 */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100904struct pattern *pat_match_dom(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100905{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100906 struct pattern_list *lst;
907 struct pattern *pattern;
908
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100909 list_for_each_entry(lst, &expr->patterns, list) {
910 pattern = &lst->pat;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200911 if (match_word(smp, pattern, expr->mflags, make_4delim('/', '?', '.', ':')))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100912 return pattern;
913 }
914 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100915}
916
917/* Checks that the integer in <test> is included between min and max */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100918struct pattern *pat_match_int(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100919{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100920 struct pattern_list *lst;
921 struct pattern *pattern;
922
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100923 list_for_each_entry(lst, &expr->patterns, list) {
924 pattern = &lst->pat;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200925 if ((!pattern->val.range.min_set || pattern->val.range.min <= smp->data.u.sint) &&
926 (!pattern->val.range.max_set || smp->data.u.sint <= pattern->val.range.max))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100927 return pattern;
928 }
929 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100930}
931
932/* Checks that the length of the pattern in <test> is included between min and max */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100933struct pattern *pat_match_len(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100934{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100935 struct pattern_list *lst;
936 struct pattern *pattern;
937
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100938 list_for_each_entry(lst, &expr->patterns, list) {
939 pattern = &lst->pat;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200940 if ((!pattern->val.range.min_set || pattern->val.range.min <= smp->data.u.str.len) &&
941 (!pattern->val.range.max_set || smp->data.u.str.len <= pattern->val.range.max))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100942 return pattern;
943 }
944 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100945}
946
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100947struct pattern *pat_match_ip(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100948{
949 unsigned int v4; /* in network byte order */
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100950 struct in6_addr tmp6;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100951 struct in_addr *s;
952 struct ebmb_node *node;
953 struct pattern_tree *elt;
954 struct pattern_list *lst;
955 struct pattern *pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100956
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100957 /* The input sample is IPv4. Try to match in the trees. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200958 if (smp->data.type == SMP_T_IPV4) {
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100959 /* Lookup an IPv4 address in the expression's pattern tree using
960 * the longest match method.
961 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200962 s = &smp->data.u.ipv4;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100963 node = ebmb_lookup_longest(&expr->pattern_tree, &s->s_addr);
964 if (node) {
965 if (fill) {
966 elt = ebmb_entry(node, struct pattern_tree, node);
Thierry FOURNIER503bb092015-08-19 08:35:43 +0200967 static_pattern.data = elt->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +0100968 static_pattern.ref = elt->ref;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200969 static_pattern.sflags = PAT_SF_TREE;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100970 static_pattern.type = SMP_T_IPV4;
971 memcpy(&static_pattern.val.ipv4.addr.s_addr, elt->node.key, 4);
972 if (!cidr2dotted(elt->node.node.pfx, &static_pattern.val.ipv4.mask))
973 return NULL;
974 }
975 return &static_pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100976 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100977
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100978 /* The IPv4 sample dont match the IPv4 tree. Convert the IPv4
979 * sample address to IPv6 with the mapping method using the ::ffff:
980 * prefix, and try to lookup in the IPv6 tree.
981 */
982 memset(&tmp6, 0, 10);
983 *(uint16_t*)&tmp6.s6_addr[10] = htons(0xffff);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200984 *(uint32_t*)&tmp6.s6_addr[12] = smp->data.u.ipv4.s_addr;
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100985 node = ebmb_lookup_longest(&expr->pattern_tree_2, &tmp6);
986 if (node) {
987 if (fill) {
988 elt = ebmb_entry(node, struct pattern_tree, node);
Thierry FOURNIER503bb092015-08-19 08:35:43 +0200989 static_pattern.data = elt->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +0100990 static_pattern.ref = elt->ref;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200991 static_pattern.sflags = PAT_SF_TREE;
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100992 static_pattern.type = SMP_T_IPV6;
993 memcpy(&static_pattern.val.ipv6.addr, elt->node.key, 16);
994 static_pattern.val.ipv6.mask = elt->node.node.pfx;
995 }
996 return &static_pattern;
997 }
998 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100999
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001000 /* The input sample is IPv6. Try to match in the trees. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001001 if (smp->data.type == SMP_T_IPV6) {
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001002 /* Lookup an IPv6 address in the expression's pattern tree using
1003 * the longest match method.
1004 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001005 node = ebmb_lookup_longest(&expr->pattern_tree_2, &smp->data.u.ipv6);
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001006 if (node) {
1007 if (fill) {
1008 elt = ebmb_entry(node, struct pattern_tree, node);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001009 static_pattern.data = elt->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01001010 static_pattern.ref = elt->ref;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02001011 static_pattern.sflags = PAT_SF_TREE;
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001012 static_pattern.type = SMP_T_IPV6;
1013 memcpy(&static_pattern.val.ipv6.addr, elt->node.key, 16);
1014 static_pattern.val.ipv6.mask = elt->node.node.pfx;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +01001015 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001016 return &static_pattern;
1017 }
1018
1019 /* Try to convert 6 to 4 when the start of the ipv6 address match the
1020 * following forms :
1021 * - ::ffff:ip:v4 (ipv4 mapped)
1022 * - ::0000:ip:v4 (old ipv4 mapped)
1023 * - 2002:ip:v4:: (6to4)
1024 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001025 if ((*(uint32_t*)&smp->data.u.ipv6.s6_addr[0] == 0 &&
1026 *(uint32_t*)&smp->data.u.ipv6.s6_addr[4] == 0 &&
1027 (*(uint32_t*)&smp->data.u.ipv6.s6_addr[8] == 0 ||
1028 *(uint32_t*)&smp->data.u.ipv6.s6_addr[8] == htonl(0xFFFF))) ||
1029 *(uint16_t*)&smp->data.u.ipv6.s6_addr[0] == htons(0x2002)) {
1030 if (*(uint32_t*)&smp->data.u.ipv6.s6_addr[0] == 0)
1031 v4 = *(uint32_t*)&smp->data.u.ipv6.s6_addr[12];
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001032 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001033 v4 = htonl((ntohs(*(uint16_t*)&smp->data.u.ipv6.s6_addr[2]) << 16) +
1034 ntohs(*(uint16_t*)&smp->data.u.ipv6.s6_addr[4]));
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001035
1036 /* Lookup an IPv4 address in the expression's pattern tree using the longest
1037 * match method.
1038 */
1039 node = ebmb_lookup_longest(&expr->pattern_tree, &v4);
1040 if (node) {
1041 if (fill) {
1042 elt = ebmb_entry(node, struct pattern_tree, node);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001043 static_pattern.data = elt->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01001044 static_pattern.ref = elt->ref;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02001045 static_pattern.sflags = PAT_SF_TREE;
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001046 static_pattern.type = SMP_T_IPV4;
1047 memcpy(&static_pattern.val.ipv4.addr.s_addr, elt->node.key, 4);
1048 if (!cidr2dotted(elt->node.node.pfx, &static_pattern.val.ipv4.mask))
1049 return NULL;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01001050 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001051 return &static_pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +01001052 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001053 }
1054 }
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +01001055
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001056 /* Lookup in the list. the list contain only IPv4 patterns */
1057 list_for_each_entry(lst, &expr->patterns, list) {
1058 pattern = &lst->pat;
1059
1060 /* The input sample is IPv4, use it as is. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001061 if (smp->data.type == SMP_T_IPV4) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001062 v4 = smp->data.u.ipv4.s_addr;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +01001063 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001064 else if (smp->data.type == SMP_T_IPV6) {
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001065 /* v4 match on a V6 sample. We want to check at least for
1066 * the following forms :
1067 * - ::ffff:ip:v4 (ipv4 mapped)
1068 * - ::0000:ip:v4 (old ipv4 mapped)
1069 * - 2002:ip:v4:: (6to4)
1070 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001071 if (*(uint32_t*)&smp->data.u.ipv6.s6_addr[0] == 0 &&
1072 *(uint32_t*)&smp->data.u.ipv6.s6_addr[4] == 0 &&
1073 (*(uint32_t*)&smp->data.u.ipv6.s6_addr[8] == 0 ||
1074 *(uint32_t*)&smp->data.u.ipv6.s6_addr[8] == htonl(0xFFFF))) {
1075 v4 = *(uint32_t*)&smp->data.u.ipv6.s6_addr[12];
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01001076 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001077 else if (*(uint16_t*)&smp->data.u.ipv6.s6_addr[0] == htons(0x2002)) {
1078 v4 = htonl((ntohs(*(uint16_t*)&smp->data.u.ipv6.s6_addr[2]) << 16) +
1079 ntohs(*(uint16_t*)&smp->data.u.ipv6.s6_addr[4]));
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01001080 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001081 else
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01001082 continue;
Andreas Seltenreichf0653192016-03-03 20:08:35 +01001083 } else {
1084 /* impossible */
1085 continue;
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001086 }
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +01001087
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001088 /* Check if the input sample match the current pattern. */
1089 if (((v4 ^ pattern->val.ipv4.addr.s_addr) & pattern->val.ipv4.mask.s_addr) == 0)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01001090 return pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +01001091 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01001092 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +01001093}
1094
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001095void free_pattern_tree(struct eb_root *root)
1096{
1097 struct eb_node *node, *next;
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +01001098 struct pattern_tree *elt;
Thierry FOURNIER3ce88c72013-12-09 11:29:46 +01001099
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001100 node = eb_first(root);
1101 while (node) {
1102 next = eb_next(node);
1103 eb_delete(node);
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +01001104 elt = container_of(node, struct pattern_tree, node);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001105 free(elt->data);
Thierry FOURNIER3ce88c72013-12-09 11:29:46 +01001106 free(elt);
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001107 node = next;
1108 }
1109}
1110
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01001111void pat_prune_val(struct pattern_expr *expr)
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +01001112{
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01001113 struct pattern_list *pat, *tmp;
1114
1115 list_for_each_entry_safe(pat, tmp, &expr->patterns, list) {
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001116 free(pat->pat.data);
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01001117 free(pat);
1118 }
1119
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +01001120 free_pattern_tree(&expr->pattern_tree);
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001121 free_pattern_tree(&expr->pattern_tree_2);
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +01001122 LIST_INIT(&expr->patterns);
1123}
1124
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01001125void pat_prune_ptr(struct pattern_expr *expr)
1126{
1127 struct pattern_list *pat, *tmp;
1128
1129 list_for_each_entry_safe(pat, tmp, &expr->patterns, list) {
1130 free(pat->pat.ptr.ptr);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001131 free(pat->pat.data);
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01001132 free(pat);
1133 }
1134
1135 free_pattern_tree(&expr->pattern_tree);
1136 free_pattern_tree(&expr->pattern_tree_2);
1137 LIST_INIT(&expr->patterns);
1138}
1139
1140void pat_prune_reg(struct pattern_expr *expr)
1141{
1142 struct pattern_list *pat, *tmp;
1143
1144 list_for_each_entry_safe(pat, tmp, &expr->patterns, list) {
1145 regex_free(pat->pat.ptr.ptr);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001146 free(pat->pat.data);
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01001147 free(pat);
1148 }
1149
1150 free_pattern_tree(&expr->pattern_tree);
1151 free_pattern_tree(&expr->pattern_tree_2);
1152 LIST_INIT(&expr->patterns);
1153}
1154
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001155/*
1156 *
1157 * The following functions are used for the pattern indexation
1158 *
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001159 */
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001160
1161int pat_idx_list_val(struct pattern_expr *expr, struct pattern *pat, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001162{
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001163 struct pattern_list *patl;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001164
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001165 /* allocate pattern */
1166 patl = calloc(1, sizeof(*patl));
1167 if (!patl) {
1168 memprintf(err, "out of memory while indexing pattern");
Thierry FOURNIER972028f2014-01-23 17:53:31 +01001169 return 0;
1170 }
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001171
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001172 /* duplicate pattern */
1173 memcpy(&patl->pat, pat, sizeof(*pat));
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001174
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001175 /* chain pattern in the expression */
1176 LIST_ADDQ(&expr->patterns, &patl->list);
Willy Tarreau72f073b2015-04-29 17:53:47 +02001177 expr->revision = rdtsc();
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001178
1179 /* that's ok */
1180 return 1;
1181}
1182
1183int pat_idx_list_ptr(struct pattern_expr *expr, struct pattern *pat, char **err)
1184{
1185 struct pattern_list *patl;
1186
1187 /* allocate pattern */
1188 patl = calloc(1, sizeof(*patl));
Thierry FOURNIER8aa83842015-02-06 17:50:55 +01001189 if (!patl) {
1190 memprintf(err, "out of memory while indexing pattern");
Thierry FOURNIER972028f2014-01-23 17:53:31 +01001191 return 0;
Thierry FOURNIER8aa83842015-02-06 17:50:55 +01001192 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001193
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001194 /* duplicate pattern */
1195 memcpy(&patl->pat, pat, sizeof(*pat));
1196 patl->pat.ptr.ptr = malloc(patl->pat.len);
1197 if (!patl->pat.ptr.ptr) {
1198 free(patl);
1199 memprintf(err, "out of memory while indexing pattern");
1200 return 0;
1201 }
1202 memcpy(patl->pat.ptr.ptr, pat->ptr.ptr, pat->len);
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001203
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001204 /* chain pattern in the expression */
1205 LIST_ADDQ(&expr->patterns, &patl->list);
Willy Tarreau72f073b2015-04-29 17:53:47 +02001206 expr->revision = rdtsc();
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001207
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001208 /* that's ok */
1209 return 1;
1210}
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001211
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001212int pat_idx_list_str(struct pattern_expr *expr, struct pattern *pat, char **err)
1213{
1214 struct pattern_list *patl;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001215
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001216 /* allocate pattern */
1217 patl = calloc(1, sizeof(*patl));
1218 if (!patl) {
1219 memprintf(err, "out of memory while indexing pattern");
1220 return 0;
1221 }
1222
1223 /* duplicate pattern */
1224 memcpy(&patl->pat, pat, sizeof(*pat));
1225 patl->pat.ptr.str = malloc(patl->pat.len + 1);
1226 if (!patl->pat.ptr.str) {
1227 free(patl);
1228 memprintf(err, "out of memory while indexing pattern");
1229 return 0;
1230 }
1231 memcpy(patl->pat.ptr.ptr, pat->ptr.ptr, pat->len);
1232 patl->pat.ptr.str[patl->pat.len] = '\0';
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001233
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001234 /* chain pattern in the expression */
1235 LIST_ADDQ(&expr->patterns, &patl->list);
Willy Tarreau72f073b2015-04-29 17:53:47 +02001236 expr->revision = rdtsc();
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001237
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001238 /* that's ok */
1239 return 1;
1240}
1241
Thierry Fournier8feaa662016-02-10 22:55:20 +01001242int pat_idx_list_reg_cap(struct pattern_expr *expr, struct pattern *pat, int cap, char **err)
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001243{
1244 struct pattern_list *patl;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001245
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001246 /* allocate pattern */
1247 patl = calloc(1, sizeof(*patl));
1248 if (!patl) {
1249 memprintf(err, "out of memory while indexing pattern");
1250 return 0;
Thierry FOURNIER972028f2014-01-23 17:53:31 +01001251 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001252
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001253 /* duplicate pattern */
1254 memcpy(&patl->pat, pat, sizeof(*pat));
1255
1256 /* allocate regex */
1257 patl->pat.ptr.reg = calloc(1, sizeof(*patl->pat.ptr.reg));
1258 if (!patl->pat.ptr.reg) {
1259 free(patl);
1260 memprintf(err, "out of memory while indexing pattern");
1261 return 0;
1262 }
1263
1264 /* compile regex */
Thierry Fournier8feaa662016-02-10 22:55:20 +01001265 if (!regex_comp(pat->ptr.str, patl->pat.ptr.reg,
1266 !(expr->mflags & PAT_MF_IGNORE_CASE), cap, err)) {
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001267 free(patl->pat.ptr.reg);
Dirkjan Bussink07fcaaa2014-04-28 22:57:16 +00001268 free(patl);
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001269 return 0;
1270 }
1271
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001272 /* chain pattern in the expression */
1273 LIST_ADDQ(&expr->patterns, &patl->list);
Willy Tarreau72f073b2015-04-29 17:53:47 +02001274 expr->revision = rdtsc();
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001275
1276 /* that's ok */
1277 return 1;
1278}
1279
Thierry Fournier8feaa662016-02-10 22:55:20 +01001280int pat_idx_list_reg(struct pattern_expr *expr, struct pattern *pat, char **err)
1281{
1282 return pat_idx_list_reg_cap(expr, pat, 0, err);
1283}
1284
1285int pat_idx_list_regm(struct pattern_expr *expr, struct pattern *pat, char **err)
1286{
1287 return pat_idx_list_reg_cap(expr, pat, 1, err);
1288}
1289
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001290int pat_idx_tree_ip(struct pattern_expr *expr, struct pattern *pat, char **err)
1291{
1292 unsigned int mask;
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +01001293 struct pattern_tree *node;
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001294
1295 /* Only IPv4 can be indexed */
1296 if (pat->type == SMP_T_IPV4) {
Thierry FOURNIER972028f2014-01-23 17:53:31 +01001297 /* in IPv4 case, check if the mask is contiguous so that we can
1298 * insert the network into the tree. A continuous mask has only
1299 * ones on the left. This means that this mask + its lower bit
1300 * added once again is null.
1301 */
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001302 mask = ntohl(pat->val.ipv4.mask.s_addr);
1303 if (mask + (mask & -mask) == 0) {
1304 mask = mask ? 33 - flsnz(mask & -mask) : 0; /* equals cidr value */
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001305
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001306 /* node memory allocation */
1307 node = calloc(1, sizeof(*node) + 4);
1308 if (!node) {
1309 memprintf(err, "out of memory while loading pattern");
1310 return 0;
1311 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001312
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001313 /* copy the pointer to sample associated to this node */
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001314 node->data = pat->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01001315 node->ref = pat->ref;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001316
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001317 /* FIXME: insert <addr>/<mask> into the tree here */
1318 memcpy(node->node.key, &pat->val.ipv4.addr, 4); /* network byte order */
1319 node->node.node.pfx = mask;
Thierry FOURNIER31db4ae2014-01-30 00:27:15 +01001320
1321 /* Insert the entry. */
1322 ebmb_insert_prefix(&expr->pattern_tree, &node->node, 4);
Willy Tarreau72f073b2015-04-29 17:53:47 +02001323 expr->revision = rdtsc();
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001324
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001325 /* that's ok */
1326 return 1;
1327 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001328 else {
1329 /* If the mask is not contiguous, just add the pattern to the list */
1330 return pat_idx_list_val(expr, pat, err);
1331 }
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001332 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001333 else if (pat->type == SMP_T_IPV6) {
1334 /* IPv6 also can be indexed */
1335 node = calloc(1, sizeof(*node) + 16);
1336 if (!node) {
1337 memprintf(err, "out of memory while loading pattern");
1338 return 0;
1339 }
1340
1341 /* copy the pointer to sample associated to this node */
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001342 node->data = pat->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01001343 node->ref = pat->ref;
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001344
1345 /* FIXME: insert <addr>/<mask> into the tree here */
1346 memcpy(node->node.key, &pat->val.ipv6.addr, 16); /* network byte order */
1347 node->node.node.pfx = pat->val.ipv6.mask;
Thierry FOURNIER31db4ae2014-01-30 00:27:15 +01001348
1349 /* Insert the entry. */
1350 ebmb_insert_prefix(&expr->pattern_tree_2, &node->node, 16);
Willy Tarreau72f073b2015-04-29 17:53:47 +02001351 expr->revision = rdtsc();
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001352
1353 /* that's ok */
1354 return 1;
1355 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001356
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001357 return 0;
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001358}
1359
1360int pat_idx_tree_str(struct pattern_expr *expr, struct pattern *pat, char **err)
1361{
1362 int len;
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +01001363 struct pattern_tree *node;
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001364
1365 /* Only string can be indexed */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001366 if (pat->type != SMP_T_STR) {
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001367 memprintf(err, "internal error: string expected, but the type is '%s'",
1368 smp_to_type[pat->type]);
1369 return 0;
Thierry FOURNIER972028f2014-01-23 17:53:31 +01001370 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001371
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001372 /* If the flag PAT_F_IGNORE_CASE is set, we cannot use trees */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02001373 if (expr->mflags & PAT_MF_IGNORE_CASE)
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001374 return pat_idx_list_str(expr, pat, err);
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001375
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001376 /* Process the key len */
1377 len = strlen(pat->ptr.str) + 1;
1378
1379 /* node memory allocation */
1380 node = calloc(1, sizeof(*node) + len);
1381 if (!node) {
1382 memprintf(err, "out of memory while loading pattern");
1383 return 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001384 }
1385
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001386 /* copy the pointer to sample associated to this node */
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001387 node->data = pat->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01001388 node->ref = pat->ref;
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001389
1390 /* copy the string */
1391 memcpy(node->node.key, pat->ptr.str, len);
1392
1393 /* index the new node */
Thierry FOURNIER31db4ae2014-01-30 00:27:15 +01001394 ebst_insert(&expr->pattern_tree, &node->node);
Willy Tarreau72f073b2015-04-29 17:53:47 +02001395 expr->revision = rdtsc();
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001396
1397 /* that's ok */
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001398 return 1;
1399}
1400
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +02001401int pat_idx_tree_pfx(struct pattern_expr *expr, struct pattern *pat, char **err)
1402{
1403 int len;
1404 struct pattern_tree *node;
1405
1406 /* Only string can be indexed */
1407 if (pat->type != SMP_T_STR) {
1408 memprintf(err, "internal error: string expected, but the type is '%s'",
1409 smp_to_type[pat->type]);
1410 return 0;
1411 }
1412
1413 /* If the flag PAT_F_IGNORE_CASE is set, we cannot use trees */
1414 if (expr->mflags & PAT_MF_IGNORE_CASE)
1415 return pat_idx_list_str(expr, pat, err);
1416
1417 /* Process the key len */
1418 len = strlen(pat->ptr.str);
1419
1420 /* node memory allocation */
1421 node = calloc(1, sizeof(*node) + len + 1);
1422 if (!node) {
1423 memprintf(err, "out of memory while loading pattern");
1424 return 0;
1425 }
1426
1427 /* copy the pointer to sample associated to this node */
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001428 node->data = pat->data;
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +02001429 node->ref = pat->ref;
1430
1431 /* copy the string and the trailing zero */
1432 memcpy(node->node.key, pat->ptr.str, len + 1);
1433 node->node.node.pfx = len * 8;
1434
1435 /* index the new node */
1436 ebmb_insert_prefix(&expr->pattern_tree, &node->node, len);
Willy Tarreau72f073b2015-04-29 17:53:47 +02001437 expr->revision = rdtsc();
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +02001438
1439 /* that's ok */
1440 return 1;
1441}
1442
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001443void pat_del_list_val(struct pattern_expr *expr, struct pat_ref_elt *ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001444{
1445 struct pattern_list *pat;
1446 struct pattern_list *safe;
1447
1448 list_for_each_entry_safe(pat, safe, &expr->patterns, list) {
1449 /* Check equality. */
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001450 if (pat->pat.ref != ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001451 continue;
1452
1453 /* Delete and free entry. */
1454 LIST_DEL(&pat->list);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001455 free(pat->pat.data);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001456 free(pat);
1457 }
Willy Tarreau72f073b2015-04-29 17:53:47 +02001458 expr->revision = rdtsc();
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001459}
1460
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001461void pat_del_tree_ip(struct pattern_expr *expr, struct pat_ref_elt *ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001462{
1463 struct ebmb_node *node, *next_node;
1464 struct pattern_tree *elt;
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001465
1466 /* browse each node of the tree for IPv4 addresses. */
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001467 for (node = ebmb_first(&expr->pattern_tree), next_node = node ? ebmb_next(node) : NULL;
1468 node;
1469 node = next_node, next_node = node ? ebmb_next(node) : NULL) {
1470 /* Extract container of the tree node. */
1471 elt = container_of(node, struct pattern_tree, node);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001472
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001473 /* Check equality. */
1474 if (elt->ref != ref)
1475 continue;
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001476
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001477 /* Delete and free entry. */
1478 ebmb_delete(node);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001479 free(elt->data);
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001480 free(elt);
1481 }
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001482
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001483 /* Browse each node of the list for IPv4 addresses. */
1484 pat_del_list_val(expr, ref);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001485
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001486 /* browse each node of the tree for IPv6 addresses. */
1487 for (node = ebmb_first(&expr->pattern_tree_2), next_node = node ? ebmb_next(node) : NULL;
1488 node;
1489 node = next_node, next_node = node ? ebmb_next(node) : NULL) {
1490 /* Extract container of the tree node. */
1491 elt = container_of(node, struct pattern_tree, node);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001492
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001493 /* Check equality. */
1494 if (elt->ref != ref)
1495 continue;
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001496
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001497 /* Delete and free entry. */
1498 ebmb_delete(node);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001499 free(elt->data);
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001500 free(elt);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001501 }
Willy Tarreau72f073b2015-04-29 17:53:47 +02001502 expr->revision = rdtsc();
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001503}
1504
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001505void pat_del_list_ptr(struct pattern_expr *expr, struct pat_ref_elt *ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001506{
1507 struct pattern_list *pat;
1508 struct pattern_list *safe;
1509
1510 list_for_each_entry_safe(pat, safe, &expr->patterns, list) {
1511 /* Check equality. */
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001512 if (pat->pat.ref != ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001513 continue;
1514
1515 /* Delete and free entry. */
1516 LIST_DEL(&pat->list);
1517 free(pat->pat.ptr.ptr);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001518 free(pat->pat.data);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001519 free(pat);
1520 }
Willy Tarreau72f073b2015-04-29 17:53:47 +02001521 expr->revision = rdtsc();
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001522}
1523
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001524void pat_del_tree_str(struct pattern_expr *expr, struct pat_ref_elt *ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001525{
1526 struct ebmb_node *node, *next_node;
1527 struct pattern_tree *elt;
1528
Thierry FOURNIER73bc2852015-02-06 17:53:54 +01001529 /* If the flag PAT_F_IGNORE_CASE is set, we cannot use trees */
1530 if (expr->mflags & PAT_MF_IGNORE_CASE)
1531 return pat_del_list_ptr(expr, ref);
1532
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001533 /* browse each node of the tree. */
1534 for (node = ebmb_first(&expr->pattern_tree), next_node = node ? ebmb_next(node) : NULL;
1535 node;
1536 node = next_node, next_node = node ? ebmb_next(node) : NULL) {
1537 /* Extract container of the tree node. */
1538 elt = container_of(node, struct pattern_tree, node);
1539
1540 /* Check equality. */
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001541 if (elt->ref != ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001542 continue;
1543
1544 /* Delete and free entry. */
1545 ebmb_delete(node);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001546 free(elt->data);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001547 free(elt);
1548 }
Willy Tarreau72f073b2015-04-29 17:53:47 +02001549 expr->revision = rdtsc();
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001550}
1551
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001552void pat_del_list_reg(struct pattern_expr *expr, struct pat_ref_elt *ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001553{
1554 struct pattern_list *pat;
1555 struct pattern_list *safe;
1556
1557 list_for_each_entry_safe(pat, safe, &expr->patterns, list) {
1558 /* Check equality. */
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001559 if (pat->pat.ref != ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001560 continue;
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001561
1562 /* Delete and free entry. */
1563 LIST_DEL(&pat->list);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001564 regex_free(pat->pat.ptr.ptr);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001565 free(pat->pat.data);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001566 free(pat);
1567 }
Willy Tarreau72f073b2015-04-29 17:53:47 +02001568 expr->revision = rdtsc();
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001569}
1570
1571void pattern_init_expr(struct pattern_expr *expr)
1572{
1573 LIST_INIT(&expr->patterns);
Willy Tarreau72f073b2015-04-29 17:53:47 +02001574 expr->revision = 0;
Thierry FOURNIER31db4ae2014-01-30 00:27:15 +01001575 expr->pattern_tree = EB_ROOT;
1576 expr->pattern_tree_2 = EB_ROOT;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001577}
1578
1579void pattern_init_head(struct pattern_head *head)
1580{
1581 LIST_INIT(&head->head);
1582}
1583
1584/* The following functions are relative to the management of the reference
1585 * lists. These lists are used to store the original pattern and associated
1586 * value as string form.
1587 *
1588 * This is used with modifiable ACL and MAPS
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001589 *
1590 * The pattern reference are stored with two identifiers: the unique_id and
1591 * the reference.
1592 *
1593 * The reference identify a file. Each file with the same name point to the
1594 * same reference. We can register many times one file. If the file is modified,
1595 * all his dependencies are also modified. The reference can be used with map or
1596 * acl.
1597 *
1598 * The unique_id identify inline acl. The unique id is unique for each acl.
1599 * You cannot force the same id in the configuration file, because this repoort
1600 * an error.
1601 *
1602 * A particular case appears if the filename is a number. In this case, the
1603 * unique_id is set with the number represented by the filename and the
1604 * reference is also set. This method prevent double unique_id.
1605 *
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001606 */
1607
1608/* This function lookup for reference. If the reference is found, they return
1609 * pointer to the struct pat_ref, else return NULL.
1610 */
1611struct pat_ref *pat_ref_lookup(const char *reference)
1612{
1613 struct pat_ref *ref;
1614
1615 list_for_each_entry(ref, &pattern_reference, list)
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001616 if (ref->reference && strcmp(reference, ref->reference) == 0)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001617 return ref;
1618 return NULL;
1619}
1620
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001621/* This function lookup for unique id. If the reference is found, they return
1622 * pointer to the struct pat_ref, else return NULL.
1623 */
1624struct pat_ref *pat_ref_lookupid(int unique_id)
1625{
1626 struct pat_ref *ref;
1627
1628 list_for_each_entry(ref, &pattern_reference, list)
1629 if (ref->unique_id == unique_id)
1630 return ref;
1631 return NULL;
1632}
1633
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001634/* This function remove all pattern matching the pointer <refelt> from
1635 * the the reference and from each expr member of the reference. This
1636 * function returns 1 if the deletion is done and return 0 is the entry
1637 * is not found.
1638 */
1639int pat_ref_delete_by_id(struct pat_ref *ref, struct pat_ref_elt *refelt)
1640{
1641 struct pattern_expr *expr;
1642 struct pat_ref_elt *elt, *safe;
Emeric Brun8d85aa42017-06-29 15:40:33 +02001643 struct bref *bref, *back;
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001644
1645 /* delete pattern from reference */
1646 list_for_each_entry_safe(elt, safe, &ref->head, list) {
1647 if (elt == refelt) {
Emeric Brun8d85aa42017-06-29 15:40:33 +02001648 list_for_each_entry_safe(bref, back, &elt->back_refs, users) {
1649 /*
1650 * we have to unlink all watchers. We must not relink them if
1651 * this elt was the last one in the list.
1652 */
1653 LIST_DEL(&bref->users);
1654 LIST_INIT(&bref->users);
1655 if (elt->list.n != &ref->head)
1656 LIST_ADDQ(&LIST_ELEM(elt->list.n, struct stream *, list)->back_refs, &bref->users);
1657 bref->ref = elt->list.n;
1658 }
peter caiaede6dd2015-10-07 00:07:43 -07001659 list_for_each_entry(expr, &ref->pat, list)
1660 pattern_delete(expr, elt);
1661
Emeric Brunb5997f72017-07-03 11:34:05 +02001662 /* pat_ref_elt is trashed once all expr
1663 are cleaned and there is no ref remaining */
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001664 LIST_DEL(&elt->list);
1665 free(elt->sample);
1666 free(elt->pattern);
1667 free(elt);
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001668 return 1;
1669 }
1670 }
1671 return 0;
1672}
1673
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001674/* This function remove all pattern match <key> from the the reference
1675 * and from each expr member of the reference. This fucntion returns 1
1676 * if the deletion is done and return 0 is the entry is not found.
1677 */
1678int pat_ref_delete(struct pat_ref *ref, const char *key)
1679{
1680 struct pattern_expr *expr;
1681 struct pat_ref_elt *elt, *safe;
Emeric Brun8d85aa42017-06-29 15:40:33 +02001682 struct bref *bref, *back;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001683 int found = 0;
1684
1685 /* delete pattern from reference */
1686 list_for_each_entry_safe(elt, safe, &ref->head, list) {
1687 if (strcmp(key, elt->pattern) == 0) {
Emeric Brun8d85aa42017-06-29 15:40:33 +02001688 list_for_each_entry_safe(bref, back, &elt->back_refs, users) {
1689 /*
1690 * we have to unlink all watchers. We must not relink them if
1691 * this elt was the last one in the list.
1692 */
1693 LIST_DEL(&bref->users);
1694 LIST_INIT(&bref->users);
1695 if (elt->list.n != &ref->head)
1696 LIST_ADDQ(&LIST_ELEM(elt->list.n, struct stream *, list)->back_refs, &bref->users);
1697 bref->ref = elt->list.n;
1698 }
Dirkjan Bussink07fcaaa2014-04-28 22:57:16 +00001699 list_for_each_entry(expr, &ref->pat, list)
1700 pattern_delete(expr, elt);
1701
Emeric Brunb5997f72017-07-03 11:34:05 +02001702 /* pat_ref_elt is trashed once all expr
1703 are cleaned and there is no ref remaining */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001704 LIST_DEL(&elt->list);
1705 free(elt->sample);
1706 free(elt->pattern);
1707 free(elt);
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001708
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001709 found = 1;
1710 }
1711 }
1712
1713 if (!found)
1714 return 0;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001715 return 1;
1716}
1717
Baptiste Assmann953f74d2014-04-25 16:57:03 +02001718/*
1719 * find and return an element <elt> matching <key> in a reference <ref>
1720 * return NULL if not found
1721 */
1722struct pat_ref_elt *pat_ref_find_elt(struct pat_ref *ref, const char *key)
1723{
1724 struct pat_ref_elt *elt;
1725
1726 list_for_each_entry(elt, &ref->head, list) {
1727 if (strcmp(key, elt->pattern) == 0)
1728 return elt;
1729 }
1730
1731 return NULL;
1732}
1733
1734
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001735 /* This function modify the sample of the first pattern that match the <key>. */
1736static inline int pat_ref_set_elt(struct pat_ref *ref, struct pat_ref_elt *elt,
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001737 const char *value, char **err)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001738{
1739 struct pattern_expr *expr;
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001740 struct sample_data **data;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001741 char *sample;
Thierry FOURNIER12ba0c22015-08-14 00:02:11 +02001742 struct sample_data test;
Thierry FOURNIER149e0fe2014-01-29 19:35:06 +01001743
1744 /* Try all needed converters. */
1745 list_for_each_entry(expr, &ref->pat, list) {
1746 if (!expr->pat_head->parse_smp)
1747 continue;
1748
1749 if (!expr->pat_head->parse_smp(value, &test)) {
1750 memprintf(err, "unable to parse '%s'", value);
1751 return 0;
1752 }
1753 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001754
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001755 /* Modify pattern from reference. */
1756 sample = strdup(value);
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001757 if (!sample) {
1758 memprintf(err, "out of memory error");
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001759 return 0;
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001760 }
Thierry FOURNIER149e0fe2014-01-29 19:35:06 +01001761 /* Load sample in each reference. All the conversion are tested
1762 * below, normally these calls dosn't fail.
1763 */
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001764 list_for_each_entry(expr, &ref->pat, list) {
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001765 if (!expr->pat_head->parse_smp)
1766 continue;
1767
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001768 HA_RWLOCK_WRLOCK(PATEXP_LOCK, &expr->lock);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001769 data = pattern_find_smp(expr, elt);
1770 if (data && *data && !expr->pat_head->parse_smp(sample, *data))
1771 *data = NULL;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001772 HA_RWLOCK_WRUNLOCK(PATEXP_LOCK, &expr->lock);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001773 }
1774
Emeric Brunb5997f72017-07-03 11:34:05 +02001775 /* free old sample only when all exprs are updated */
1776 free(elt->sample);
1777 elt->sample = sample;
1778
1779
Thierry FOURNIER149e0fe2014-01-29 19:35:06 +01001780 return 1;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001781}
1782
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001783/* This function modify the sample of the first pattern that match the <key>. */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001784int 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 +01001785{
1786 struct pat_ref_elt *elt;
1787
1788 /* Look for pattern in the reference. */
1789 list_for_each_entry(elt, &ref->head, list) {
1790 if (elt == refelt) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001791 if (!pat_ref_set_elt(ref, elt, value, err))
1792 return 0;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001793 return 1;
1794 }
1795 }
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001796
1797 memprintf(err, "key or pattern not found");
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001798 return 0;
1799}
1800
1801/* This function modify the sample of the first pattern that match the <key>. */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001802int pat_ref_set(struct pat_ref *ref, const char *key, const char *value, char **err)
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001803{
1804 struct pat_ref_elt *elt;
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001805 int found = 0;
1806 char *_merr;
1807 char **merr;
1808
1809 if (err) {
1810 merr = &_merr;
1811 *merr = NULL;
1812 }
1813 else
1814 merr = NULL;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001815
1816 /* Look for pattern in the reference. */
1817 list_for_each_entry(elt, &ref->head, list) {
1818 if (strcmp(key, elt->pattern) == 0) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001819 if (!pat_ref_set_elt(ref, elt, value, merr)) {
1820 if (!found)
1821 *err = *merr;
1822 else {
1823 memprintf(err, "%s, %s", *err, *merr);
1824 free(*merr);
1825 *merr = NULL;
1826 }
1827 }
1828 found = 1;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001829 }
1830 }
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001831
1832 if (!found) {
1833 memprintf(err, "entry not found");
1834 return 0;
1835 }
1836 return 1;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001837}
1838
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001839/* This function create new reference. <ref> is the reference name.
1840 * <flags> are PAT_REF_*. /!\ The reference is not checked, and must
1841 * be unique. The user must check the reference with "pat_ref_lookup()"
1842 * before calling this function. If the fucntion fail, it return NULL,
1843 * else return new struct pat_ref.
1844 */
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001845struct pat_ref *pat_ref_new(const char *reference, const char *display, unsigned int flags)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001846{
1847 struct pat_ref *ref;
1848
1849 ref = malloc(sizeof(*ref));
1850 if (!ref)
1851 return NULL;
1852
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001853 if (display) {
1854 ref->display = strdup(display);
1855 if (!ref->display) {
1856 free(ref);
1857 return NULL;
1858 }
1859 }
1860 else
1861 ref->display = NULL;
1862
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001863 ref->reference = strdup(reference);
1864 if (!ref->reference) {
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001865 free(ref->display);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001866 free(ref);
1867 return NULL;
1868 }
1869
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001870 ref->flags = flags;
1871 ref->unique_id = -1;
1872
1873 LIST_INIT(&ref->head);
1874 LIST_INIT(&ref->pat);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001875 HA_SPIN_INIT(&ref->lock);
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001876 LIST_ADDQ(&pattern_reference, &ref->list);
1877
1878 return ref;
1879}
1880
1881/* This function create new reference. <unique_id> is the unique id. If
1882 * the value of <unique_id> is -1, the unique id is calculated later.
1883 * <flags> are PAT_REF_*. /!\ The reference is not checked, and must
1884 * be unique. The user must check the reference with "pat_ref_lookup()"
1885 * or pat_ref_lookupid before calling this function. If the function
1886 * fail, it return NULL, else return new struct pat_ref.
1887 */
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001888struct pat_ref *pat_ref_newid(int unique_id, const char *display, unsigned int flags)
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001889{
1890 struct pat_ref *ref;
1891
1892 ref = malloc(sizeof(*ref));
1893 if (!ref)
1894 return NULL;
1895
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001896 if (display) {
1897 ref->display = strdup(display);
1898 if (!ref->display) {
1899 free(ref);
1900 return NULL;
1901 }
1902 }
1903 else
1904 ref->display = NULL;
1905
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001906 ref->reference = NULL;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001907 ref->flags = flags;
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001908 ref->unique_id = unique_id;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001909 LIST_INIT(&ref->head);
1910 LIST_INIT(&ref->pat);
1911
1912 LIST_ADDQ(&pattern_reference, &ref->list);
1913
1914 return ref;
1915}
1916
1917/* This function adds entry to <ref>. It can failed with memory error.
1918 * If the function fails, it returns 0.
1919 */
1920int pat_ref_append(struct pat_ref *ref, char *pattern, char *sample, int line)
1921{
1922 struct pat_ref_elt *elt;
1923
1924 elt = malloc(sizeof(*elt));
1925 if (!elt)
1926 return 0;
1927
1928 elt->line = line;
1929
1930 elt->pattern = strdup(pattern);
1931 if (!elt->pattern) {
1932 free(elt);
1933 return 0;
1934 }
1935
1936 if (sample) {
1937 elt->sample = strdup(sample);
1938 if (!elt->sample) {
1939 free(elt->pattern);
1940 free(elt);
1941 return 0;
1942 }
1943 }
1944 else
1945 elt->sample = NULL;
1946
Emeric Brun8d85aa42017-06-29 15:40:33 +02001947 LIST_INIT(&elt->back_refs);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001948 LIST_ADDQ(&ref->head, &elt->list);
1949
1950 return 1;
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001951}
1952
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001953/* This function create sample found in <elt>, parse the pattern also
1954 * found in <elt> and insert it in <expr>. The function copy <patflags>
1955 * in <expr>. If the function fails, it returns0 and <err> is filled.
1956 * In succes case, the function returns 1.
1957 */
1958static inline
1959int pat_ref_push(struct pat_ref_elt *elt, struct pattern_expr *expr,
1960 int patflags, char **err)
1961{
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001962 struct sample_data *data;
Thierry FOURNIERd25c8422014-01-28 15:34:35 +01001963 struct pattern pattern;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001964
1965 /* Create sample */
1966 if (elt->sample && expr->pat_head->parse_smp) {
1967 /* New sample. */
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001968 data = malloc(sizeof(*data));
1969 if (!data)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001970 return 0;
1971
1972 /* Parse value. */
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001973 if (!expr->pat_head->parse_smp(elt->sample, data)) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001974 memprintf(err, "unable to parse '%s'", elt->sample);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001975 free(data);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001976 return 0;
1977 }
1978
1979 }
1980 else
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001981 data = NULL;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001982
Thierry FOURNIERd25c8422014-01-28 15:34:35 +01001983 /* initialise pattern */
1984 memset(&pattern, 0, sizeof(pattern));
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001985 pattern.data = data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01001986 pattern.ref = elt;
Thierry FOURNIERd25c8422014-01-28 15:34:35 +01001987
1988 /* parse pattern */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02001989 if (!expr->pat_head->parse(elt->pattern, &pattern, expr->mflags, err)) {
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001990 free(data);
Thierry FOURNIERd25c8422014-01-28 15:34:35 +01001991 return 0;
1992 }
1993
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001994 HA_RWLOCK_WRLOCK(PATEXP_LOCK, &expr->lock);
Thierry FOURNIERd25c8422014-01-28 15:34:35 +01001995 /* index pattern */
1996 if (!expr->pat_head->index(expr, &pattern, err)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001997 HA_RWLOCK_WRUNLOCK(PATEXP_LOCK, &expr->lock);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001998 free(data);
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001999 return 0;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002000 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002001 HA_RWLOCK_WRUNLOCK(PATEXP_LOCK, &expr->lock);
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01002002
2003 return 1;
2004}
2005
Thierry FOURNIER31db4ae2014-01-30 00:27:15 +01002006/* This function adds entry to <ref>. It can failed with memory error. The new
2007 * entry is added at all the pattern_expr registered in this reference. The
2008 * function stop on the first error encountered. It returns 0 and err is
2009 * filled. If an error is encountered, the complete add operation is cancelled.
2010 * If the insertion is a success the function returns 1.
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002011 */
2012int pat_ref_add(struct pat_ref *ref,
2013 const char *pattern, const char *sample,
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02002014 char **err)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002015{
2016 struct pat_ref_elt *elt;
2017 struct pattern_expr *expr;
2018
2019 elt = malloc(sizeof(*elt));
2020 if (!elt) {
2021 memprintf(err, "out of memory error");
2022 return 0;
2023 }
2024
2025 elt->line = -1;
2026
2027 elt->pattern = strdup(pattern);
2028 if (!elt->pattern) {
2029 free(elt);
2030 memprintf(err, "out of memory error");
2031 return 0;
2032 }
2033
2034 if (sample) {
2035 elt->sample = strdup(sample);
2036 if (!elt->sample) {
2037 free(elt->pattern);
2038 free(elt);
2039 memprintf(err, "out of memory error");
2040 return 0;
2041 }
2042 }
2043 else
2044 elt->sample = NULL;
2045
Emeric Brun8d85aa42017-06-29 15:40:33 +02002046 LIST_INIT(&elt->back_refs);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002047 LIST_ADDQ(&ref->head, &elt->list);
2048
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002049 list_for_each_entry(expr, &ref->pat, list) {
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02002050 if (!pat_ref_push(elt, expr, 0, err)) {
Thierry FOURNIER31db4ae2014-01-30 00:27:15 +01002051 /* If the insertion fails, try to delete all the added entries. */
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01002052 pat_ref_delete_by_id(ref, elt);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002053 return 0;
2054 }
2055 }
Emeric Brunb5997f72017-07-03 11:34:05 +02002056
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002057 return 1;
2058}
2059
Thierry FOURNIER46006bd2014-03-21 21:45:15 +01002060/* This function prune <ref>, replace all reference by the references
2061 * of <replace>, and reindex all the news values.
2062 *
2063 * The pattern are loaded in best effort and the errors are ignored,
2064 * but writed in the logs.
2065 */
2066void pat_ref_reload(struct pat_ref *ref, struct pat_ref *replace)
2067{
2068 struct pattern_expr *expr;
Thierry FOURNIER46006bd2014-03-21 21:45:15 +01002069 char *err = NULL;
Emeric Brunb5997f72017-07-03 11:34:05 +02002070 struct pat_ref_elt *elt, *safe;
2071 struct bref *bref, *back;
2072 struct sample_data *data;
2073 struct pattern pattern;
Thierry FOURNIER46006bd2014-03-21 21:45:15 +01002074
Thierry FOURNIER46006bd2014-03-21 21:45:15 +01002075
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002076 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Emeric Brunb5997f72017-07-03 11:34:05 +02002077 list_for_each_entry(expr, &ref->pat, list) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002078 HA_RWLOCK_WRLOCK(PATEXP_LOCK, &expr->lock);
Emeric Brunb5997f72017-07-03 11:34:05 +02002079 }
2080
2081 /* all expr are locked, we can safely remove all pat_ref */
2082 list_for_each_entry_safe(elt, safe, &ref->head, list) {
2083 list_for_each_entry_safe(bref, back, &elt->back_refs, users) {
2084 /*
2085 * we have to unlink all watchers. We must not relink them if
2086 * this elt was the last one in the list.
2087 */
2088 LIST_DEL(&bref->users);
2089 LIST_INIT(&bref->users);
2090 if (elt->list.n != &ref->head)
2091 LIST_ADDQ(&LIST_ELEM(elt->list.n, struct stream *, list)->back_refs, &bref->users);
2092 bref->ref = elt->list.n;
2093 }
2094 LIST_DEL(&elt->list);
2095 free(elt->pattern);
2096 free(elt->sample);
2097 free(elt);
2098 }
2099
2100 /* switch pat_ret_elt lists */
Thierry FOURNIER46006bd2014-03-21 21:45:15 +01002101 LIST_ADD(&replace->head, &ref->head);
2102 LIST_DEL(&replace->head);
2103
Emeric Brunb5997f72017-07-03 11:34:05 +02002104 list_for_each_entry(expr, &ref->pat, list) {
2105 expr->pat_head->prune(expr);
2106 list_for_each_entry(elt, &ref->head, list) {
2107 /* Create sample */
2108 if (elt->sample && expr->pat_head->parse_smp) {
2109 /* New sample. */
2110 data = malloc(sizeof(*data));
2111 if (!data)
2112 continue;
2113
2114 /* Parse value. */
2115 if (!expr->pat_head->parse_smp(elt->sample, data)) {
2116 memprintf(&err, "unable to parse '%s'", elt->sample);
2117 send_log(NULL, LOG_NOTICE, "%s", err);
2118 free(err);
2119 free(data);
2120 continue;
2121 }
2122
2123 }
2124 else
2125 data = NULL;
2126
2127 /* initialise pattern */
2128 memset(&pattern, 0, sizeof(pattern));
2129 pattern.data = data;
2130 pattern.ref = elt;
2131
2132 /* parse pattern */
2133 if (!expr->pat_head->parse(elt->pattern, &pattern, expr->mflags, &err)) {
Thierry FOURNIER46006bd2014-03-21 21:45:15 +01002134 send_log(NULL, LOG_NOTICE, "%s", err);
2135 free(err);
Emeric Brunb5997f72017-07-03 11:34:05 +02002136 free(data);
2137 continue;
2138 }
2139
2140 /* index pattern */
2141 if (!expr->pat_head->index(expr, &pattern, &err)) {
2142 send_log(NULL, LOG_NOTICE, "%s", err);
2143 free(err);
2144 free(data);
2145 continue;
Thierry FOURNIER46006bd2014-03-21 21:45:15 +01002146 }
2147 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002148 HA_RWLOCK_WRUNLOCK(PATEXP_LOCK, &expr->lock);
Thierry FOURNIER46006bd2014-03-21 21:45:15 +01002149 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002150 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER46006bd2014-03-21 21:45:15 +01002151}
2152
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002153/* This function prune all entries of <ref>. This function
2154 * prune the associated pattern_expr.
2155 */
2156void pat_ref_prune(struct pat_ref *ref)
2157{
2158 struct pat_ref_elt *elt, *safe;
2159 struct pattern_expr *expr;
Emeric Brun8d85aa42017-06-29 15:40:33 +02002160 struct bref *bref, *back;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002161
Emeric Brunb5997f72017-07-03 11:34:05 +02002162 list_for_each_entry(expr, &ref->pat, list) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002163 HA_RWLOCK_WRLOCK(PATEXP_LOCK, &expr->lock);
Emeric Brunb5997f72017-07-03 11:34:05 +02002164 expr->pat_head->prune(expr);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002165 HA_RWLOCK_WRUNLOCK(PATEXP_LOCK, &expr->lock);
Emeric Brunb5997f72017-07-03 11:34:05 +02002166 }
2167
2168 /* we trash pat_ref_elt in a second time to ensure that data is
2169 free once there is no ref on it */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002170 list_for_each_entry_safe(elt, safe, &ref->head, list) {
Emeric Brun8d85aa42017-06-29 15:40:33 +02002171 list_for_each_entry_safe(bref, back, &elt->back_refs, users) {
2172 /*
2173 * we have to unlink all watchers. We must not relink them if
2174 * this elt was the last one in the list.
2175 */
2176 LIST_DEL(&bref->users);
2177 LIST_INIT(&bref->users);
2178 if (elt->list.n != &ref->head)
2179 LIST_ADDQ(&LIST_ELEM(elt->list.n, struct stream *, list)->back_refs, &bref->users);
2180 bref->ref = elt->list.n;
2181 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002182 LIST_DEL(&elt->list);
2183 free(elt->pattern);
2184 free(elt->sample);
2185 free(elt);
2186 }
2187
Emeric Brunb5997f72017-07-03 11:34:05 +02002188
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002189}
2190
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002191/* This function lookup for existing reference <ref> in pattern_head <head>. */
2192struct pattern_expr *pattern_lookup_expr(struct pattern_head *head, struct pat_ref *ref)
2193{
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002194 struct pattern_expr_list *expr;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002195
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002196 list_for_each_entry(expr, &head->head, list)
2197 if (expr->expr->ref == ref)
2198 return expr->expr;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002199 return NULL;
2200}
2201
2202/* This function create new pattern_expr associated to the reference <ref>.
2203 * <ref> can be NULL. If an error is occured, the function returns NULL and
2204 * <err> is filled. Otherwise, the function returns new pattern_expr linked
2205 * with <head> and <ref>.
Thierry FOURNIER315ec422014-11-24 11:14:42 +01002206 *
2207 * The returned value can be a alredy filled pattern list, in this case the
2208 * flag <reuse> is set.
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002209 */
Thierry FOURNIER315ec422014-11-24 11:14:42 +01002210struct pattern_expr *pattern_new_expr(struct pattern_head *head, struct pat_ref *ref,
Emeric Brun7d27f3c2017-07-03 17:54:23 +02002211 int patflags, char **err, int *reuse)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002212{
2213 struct pattern_expr *expr;
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002214 struct pattern_expr_list *list;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002215
Thierry FOURNIER315ec422014-11-24 11:14:42 +01002216 if (reuse)
2217 *reuse = 0;
2218
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002219 /* Memory and initialization of the chain element. */
2220 list = malloc(sizeof(*list));
2221 if (!list) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002222 memprintf(err, "out of memory");
2223 return NULL;
2224 }
2225
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002226 /* Look for existing similar expr. No that only the index, parse and
2227 * parse_smp function must be identical for having similar pattern.
2228 * The other function depends of theses first.
2229 */
2230 if (ref) {
2231 list_for_each_entry(expr, &ref->pat, list)
2232 if (expr->pat_head->index == head->index &&
2233 expr->pat_head->parse == head->parse &&
Emeric Brun7d27f3c2017-07-03 17:54:23 +02002234 expr->pat_head->parse_smp == head->parse_smp &&
2235 expr->mflags == patflags)
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002236 break;
2237 if (&expr->list == &ref->pat)
2238 expr = NULL;
2239 }
2240 else
2241 expr = NULL;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002242
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002243 /* If no similar expr was found, we create new expr. */
2244 if (!expr) {
2245 /* Get a lot of memory for the expr struct. */
2246 expr = malloc(sizeof(*expr));
2247 if (!expr) {
Andreas Seltenreiche6e22e82016-03-03 20:20:23 +01002248 free(list);
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002249 memprintf(err, "out of memory");
2250 return NULL;
2251 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002252
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002253 /* Initialize this new expr. */
2254 pattern_init_expr(expr);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002255
Emeric Brun7d27f3c2017-07-03 17:54:23 +02002256 /* Copy the pattern matching and indexing flags. */
2257 expr->mflags = patflags;
2258
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002259 /* This new pattern expression reference one of his heads. */
2260 expr->pat_head = head;
2261
2262 /* Link with ref, or to self to facilitate LIST_DEL() */
2263 if (ref)
2264 LIST_ADDQ(&ref->pat, &expr->list);
2265 else
2266 LIST_INIT(&expr->list);
2267
2268 expr->ref = ref;
2269
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002270 HA_RWLOCK_INIT(&expr->lock);
Emeric Brunb5997f72017-07-03 11:34:05 +02002271
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002272 /* We must free this pattern if it is no more used. */
2273 list->do_free = 1;
2274 }
2275 else {
2276 /* If the pattern used already exists, it is already linked
2277 * with ref and we must not free it.
2278 */
2279 list->do_free = 0;
Thierry FOURNIER315ec422014-11-24 11:14:42 +01002280 if (reuse)
2281 *reuse = 1;
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002282 }
2283
2284 /* The new list element reference the pattern_expr. */
2285 list->expr = expr;
2286
2287 /* Link the list element with the pattern_head. */
2288 LIST_ADDQ(&head->head, &list->list);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002289 return expr;
2290}
2291
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002292/* Reads patterns from a file. If <err_msg> is non-NULL, an error message will
2293 * be returned there on errors and the caller will have to free it.
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002294 *
2295 * The file contains one key + value per line. Lines which start with '#' are
2296 * ignored, just like empty lines. Leading tabs/spaces are stripped. The key is
2297 * then the first "word" (series of non-space/tabs characters), and the value is
2298 * what follows this series of space/tab till the end of the line excluding
2299 * trailing spaces/tabs.
2300 *
2301 * Example :
2302 *
2303 * # this is a comment and is ignored
2304 * 62.212.114.60 1wt.eu \n
2305 * <-><-----------><---><----><---->
2306 * | | | | `--- trailing spaces ignored
2307 * | | | `-------- value
2308 * | | `--------------- middle spaces ignored
2309 * | `------------------------ key
2310 * `-------------------------------- leading spaces ignored
2311 *
2312 * Return non-zero in case of succes, otherwise 0.
2313 */
2314int pat_ref_read_from_file_smp(struct pat_ref *ref, const char *filename, char **err)
2315{
2316 FILE *file;
2317 char *c;
2318 int ret = 0;
2319 int line = 0;
2320 char *key_beg;
2321 char *key_end;
2322 char *value_beg;
2323 char *value_end;
2324
2325 file = fopen(filename, "r");
2326 if (!file) {
2327 memprintf(err, "failed to open pattern file <%s>", filename);
2328 return 0;
2329 }
2330
2331 /* now parse all patterns. The file may contain only one pattern
2332 * followed by one value per line. The start spaces, separator spaces
2333 * and and spaces are stripped. Each can contain comment started by '#'
2334 */
2335 while (fgets(trash.str, trash.size, file) != NULL) {
2336 line++;
2337 c = trash.str;
2338
2339 /* ignore lines beginning with a dash */
2340 if (*c == '#')
2341 continue;
2342
2343 /* strip leading spaces and tabs */
2344 while (*c == ' ' || *c == '\t')
2345 c++;
2346
2347 /* empty lines are ignored too */
2348 if (*c == '\0' || *c == '\r' || *c == '\n')
2349 continue;
2350
2351 /* look for the end of the key */
2352 key_beg = c;
2353 while (*c && *c != ' ' && *c != '\t' && *c != '\n' && *c != '\r')
2354 c++;
2355
2356 key_end = c;
2357
2358 /* strip middle spaces and tabs */
2359 while (*c == ' ' || *c == '\t')
2360 c++;
2361
2362 /* look for the end of the value, it is the end of the line */
2363 value_beg = c;
2364 while (*c && *c != '\n' && *c != '\r')
2365 c++;
2366 value_end = c;
2367
2368 /* trim possibly trailing spaces and tabs */
2369 while (value_end > value_beg && (value_end[-1] == ' ' || value_end[-1] == '\t'))
2370 value_end--;
2371
2372 /* set final \0 and check entries */
2373 *key_end = '\0';
2374 *value_end = '\0';
2375
2376 /* insert values */
2377 if (!pat_ref_append(ref, key_beg, value_beg, line)) {
2378 memprintf(err, "out of memory");
2379 goto out_close;
2380 }
2381 }
2382
2383 /* succes */
2384 ret = 1;
2385
2386 out_close:
2387 fclose(file);
2388 return ret;
2389}
2390
2391/* Reads patterns from a file. If <err_msg> is non-NULL, an error message will
2392 * be returned there on errors and the caller will have to free it.
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002393 */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002394int pat_ref_read_from_file(struct pat_ref *ref, const char *filename, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002395{
2396 FILE *file;
2397 char *c;
2398 char *arg;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002399 int ret = 0;
2400 int line = 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002401
2402 file = fopen(filename, "r");
2403 if (!file) {
2404 memprintf(err, "failed to open pattern file <%s>", filename);
2405 return 0;
2406 }
2407
2408 /* now parse all patterns. The file may contain only one pattern per
2409 * line. If the line contains spaces, they will be part of the pattern.
2410 * The pattern stops at the first CR, LF or EOF encountered.
2411 */
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002412 while (fgets(trash.str, trash.size, file) != NULL) {
2413 line++;
2414 c = trash.str;
2415
2416 /* ignore lines beginning with a dash */
2417 if (*c == '#')
2418 continue;
2419
2420 /* strip leading spaces and tabs */
2421 while (*c == ' ' || *c == '\t')
2422 c++;
2423
2424
2425 arg = c;
2426 while (*c && *c != '\n' && *c != '\r')
2427 c++;
2428 *c = 0;
2429
2430 /* empty lines are ignored too */
2431 if (c == arg)
2432 continue;
2433
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002434 if (!pat_ref_append(ref, arg, NULL, line)) {
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002435 memprintf(err, "out of memory when loading patterns from file <%s>", filename);
2436 goto out_close;
2437 }
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002438 }
2439
2440 ret = 1; /* success */
2441
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002442 out_close:
2443 fclose(file);
2444 return ret;
2445}
2446
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002447int pattern_read_from_file(struct pattern_head *head, unsigned int refflags,
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002448 const char *filename, int patflags, int load_smp,
Thierry FOURNIER94580c92014-02-11 14:36:45 +01002449 char **err, const char *file, int line)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002450{
2451 struct pat_ref *ref;
2452 struct pattern_expr *expr;
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002453 struct pat_ref_elt *elt;
Willy Tarreau4deaf392014-11-26 13:17:03 +01002454 int reuse = 0;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002455
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002456 /* Lookup for the existing reference. */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002457 ref = pat_ref_lookup(filename);
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002458
2459 /* If the reference doesn't exists, create it and load associated file. */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002460 if (!ref) {
Thierry FOURNIER94580c92014-02-11 14:36:45 +01002461 chunk_printf(&trash,
2462 "pattern loaded from file '%s' used by %s at file '%s' line %d",
2463 filename, refflags & PAT_REF_MAP ? "map" : "acl", file, line);
2464
2465 ref = pat_ref_new(filename, trash.str, refflags);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002466 if (!ref) {
2467 memprintf(err, "out of memory");
2468 return 0;
2469 }
2470
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002471 if (load_smp) {
Thierry FOURNIERc0bd9102014-01-29 12:32:58 +01002472 ref->flags |= PAT_REF_SMP;
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002473 if (!pat_ref_read_from_file_smp(ref, filename, err))
2474 return 0;
2475 }
2476 else {
2477 if (!pat_ref_read_from_file(ref, filename, err))
2478 return 0;
2479 }
2480 }
2481 else {
Thierry FOURNIERc0bd9102014-01-29 12:32:58 +01002482 /* The reference already exists, check the map compatibility. */
2483
2484 /* If the load require samples and the flag PAT_REF_SMP is not set,
2485 * the reference doesn't contain sample, and cannot be used.
2486 */
2487 if (load_smp) {
2488 if (!(ref->flags & PAT_REF_SMP)) {
2489 memprintf(err, "The file \"%s\" is already used as one column file "
2490 "and cannot be used by as two column file.",
2491 filename);
2492 return 0;
2493 }
2494 }
2495 else {
2496 /* The load doesn't require samples. If the flag PAT_REF_SMP is
2497 * set, the reference contains a sample, and cannot be used.
2498 */
2499 if (ref->flags & PAT_REF_SMP) {
2500 memprintf(err, "The file \"%s\" is already used as two column file "
2501 "and cannot be used by as one column file.",
2502 filename);
2503 return 0;
2504 }
2505 }
2506
Thierry FOURNIER94580c92014-02-11 14:36:45 +01002507 /* Extends display */
2508 chunk_printf(&trash, "%s", ref->display);
2509 chunk_appendf(&trash, ", by %s at file '%s' line %d",
2510 refflags & PAT_REF_MAP ? "map" : "acl", file, line);
2511 free(ref->display);
2512 ref->display = strdup(trash.str);
2513 if (!ref->display) {
2514 memprintf(err, "out of memory");
2515 return 0;
2516 }
2517
Thierry FOURNIERc0bd9102014-01-29 12:32:58 +01002518 /* Merge flags. */
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002519 ref->flags |= refflags;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002520 }
2521
2522 /* Now, we can loading patterns from the reference. */
2523
2524 /* Lookup for existing reference in the head. If the reference
2525 * doesn't exists, create it.
2526 */
2527 expr = pattern_lookup_expr(head, ref);
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02002528 if (!expr || (expr->mflags != patflags)) {
Emeric Brun7d27f3c2017-07-03 17:54:23 +02002529 expr = pattern_new_expr(head, ref, patflags, err, &reuse);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002530 if (!expr)
2531 return 0;
2532 }
2533
Thierry FOURNIER315ec422014-11-24 11:14:42 +01002534 /* The returned expression may be not empty, because the function
2535 * "pattern_new_expr" lookup for similar pattern list and can
2536 * reuse a already filled pattern list. In this case, we can not
2537 * reload the patterns.
2538 */
2539 if (reuse)
2540 return 1;
2541
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002542 /* Load reference content in the pattern expression. */
2543 list_for_each_entry(elt, &ref->head, list) {
2544 if (!pat_ref_push(elt, expr, patflags, err)) {
2545 if (elt->line > 0)
2546 memprintf(err, "%s at line %d of file '%s'",
2547 *err, elt->line, filename);
2548 return 0;
2549 }
2550 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002551
2552 return 1;
2553}
2554
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01002555/* This function executes a pattern match on a sample. It applies pattern <expr>
2556 * to sample <smp>. The function returns NULL if the sample dont match. It returns
2557 * non-null if the sample match. If <fill> is true and the sample match, the
2558 * function returns the matched pattern. In many cases, this pattern can be a
2559 * static buffer.
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002560 */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002561struct pattern *pattern_exec_match(struct pattern_head *head, struct sample *smp, int fill)
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002562{
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002563 struct pattern_expr_list *list;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002564 struct pattern *pat;
2565
2566 if (!head->match) {
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01002567 if (fill) {
Thierry FOURNIER503bb092015-08-19 08:35:43 +02002568 static_pattern.data = NULL;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01002569 static_pattern.ref = NULL;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02002570 static_pattern.sflags = 0;
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002571 static_pattern.type = SMP_T_SINT;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01002572 static_pattern.val.i = 1;
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01002573 }
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01002574 return &static_pattern;
2575 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002576
Thierry FOURNIER5d344082014-01-27 14:19:53 +01002577 /* convert input to string */
2578 if (!sample_convert(smp, head->expect_type))
2579 return NULL;
2580
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002581 list_for_each_entry(list, &head->head, list) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002582 HA_RWLOCK_RDLOCK(PATEXP_LOCK, &list->expr->lock);
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002583 pat = head->match(smp, list->expr, fill);
Emeric Brunb5997f72017-07-03 11:34:05 +02002584 if (pat) {
2585 /* We duplicate the pattern cause it could be modified
2586 by another thread */
2587 if (pat != &static_pattern) {
2588 memcpy(&static_pattern, pat, sizeof(struct pattern));
2589 pat = &static_pattern;
2590 }
2591
2592 /* We also duplicate the sample data for
2593 same reason */
2594 if (pat->data && (pat->data != &static_sample_data)) {
2595 switch(pat->type) {
2596 case SMP_T_STR:
2597 static_sample_data.type = SMP_T_STR;
2598 static_sample_data.u.str = *get_trash_chunk();
2599 static_sample_data.u.str.len = pat->data->u.str.len;
2600 if (static_sample_data.u.str.len >= static_sample_data.u.str.size)
2601 static_sample_data.u.str.len = static_sample_data.u.str.size - 1;
2602 memcpy(static_sample_data.u.str.str, pat->data->u.str.str, static_sample_data.u.str.len);
2603 static_sample_data.u.str.str[static_sample_data.u.str.len] = 0;
2604 case SMP_T_IPV4:
2605 case SMP_T_IPV6:
2606 case SMP_T_SINT:
2607 memcpy(&static_sample_data, pat->data, sizeof(struct sample_data));
2608 default:
2609 pat->data = NULL;
2610 }
2611 pat->data = &static_sample_data;
2612 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002613 HA_RWLOCK_RDUNLOCK(PATEXP_LOCK, &list->expr->lock);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002614 return pat;
Emeric Brunb5997f72017-07-03 11:34:05 +02002615 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002616 HA_RWLOCK_RDUNLOCK(PATEXP_LOCK, &list->expr->lock);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002617 }
2618 return NULL;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002619}
2620
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01002621/* This function prune the pattern expression. */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002622void pattern_prune(struct pattern_head *head)
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01002623{
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002624 struct pattern_expr_list *list, *safe;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002625
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002626 list_for_each_entry_safe(list, safe, &head->head, list) {
2627 LIST_DEL(&list->list);
2628 if (list->do_free) {
2629 LIST_DEL(&list->expr->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002630 HA_RWLOCK_WRLOCK(PATEXP_LOCK, &list->expr->lock);
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002631 head->prune(list->expr);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002632 HA_RWLOCK_WRUNLOCK(PATEXP_LOCK, &list->expr->lock);
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002633 free(list->expr);
2634 }
2635 free(list);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002636 }
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01002637}
2638
Thierry FOURNIER55d0b102014-01-15 11:25:26 +01002639/* This function lookup for a pattern matching the <key> and return a
2640 * pointer to a pointer of the sample stoarge. If the <key> dont match,
2641 * the function returns NULL. If the key cannot be parsed, the function
2642 * fill <err>.
2643 */
Thierry FOURNIER12ba0c22015-08-14 00:02:11 +02002644struct sample_data **pattern_find_smp(struct pattern_expr *expr, struct pat_ref_elt *ref)
Thierry FOURNIER55d0b102014-01-15 11:25:26 +01002645{
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01002646 struct ebmb_node *node;
2647 struct pattern_tree *elt;
2648 struct pattern_list *pat;
Thierry FOURNIER55d0b102014-01-15 11:25:26 +01002649
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01002650 for (node = ebmb_first(&expr->pattern_tree);
2651 node;
2652 node = ebmb_next(node)) {
2653 elt = container_of(node, struct pattern_tree, node);
2654 if (elt->ref == ref)
Thierry FOURNIER503bb092015-08-19 08:35:43 +02002655 return &elt->data;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01002656 }
2657
2658 for (node = ebmb_first(&expr->pattern_tree_2);
2659 node;
2660 node = ebmb_next(node)) {
2661 elt = container_of(node, struct pattern_tree, node);
2662 if (elt->ref == ref)
Thierry FOURNIER503bb092015-08-19 08:35:43 +02002663 return &elt->data;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01002664 }
2665
2666 list_for_each_entry(pat, &expr->patterns, list)
2667 if (pat->pat.ref == ref)
Thierry FOURNIER503bb092015-08-19 08:35:43 +02002668 return &pat->pat.data;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01002669
2670 return NULL;
Thierry FOURNIER55d0b102014-01-15 11:25:26 +01002671}
2672
Thierry FOURNIERb1136502014-01-15 11:38:49 +01002673/* This function search all the pattern matching the <key> and delete it.
2674 * If the parsing of the input key fails, the function returns 0 and the
2675 * <err> is filled, else return 1;
2676 */
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01002677int pattern_delete(struct pattern_expr *expr, struct pat_ref_elt *ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01002678{
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002679 HA_RWLOCK_WRLOCK(PATEXP_LOCK, &expr->lock);
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01002680 expr->pat_head->delete(expr, ref);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002681 HA_RWLOCK_WRUNLOCK(PATEXP_LOCK, &expr->lock);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01002682 return 1;
2683}
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002684
2685/* This function finalize the configuration parsing. Its set all the
2686 * automatic ids
2687 */
2688void pattern_finalize_config(void)
2689{
2690 int i = 0;
2691 struct pat_ref *ref, *ref2, *ref3;
2692 struct list pr = LIST_HEAD_INIT(pr);
2693
Willy Tarreauf3045d22015-04-29 16:24:50 +02002694 pat_lru_seed = random();
Emeric Brunb5997f72017-07-03 11:34:05 +02002695 if (global.tune.pattern_cache) {
Willy Tarreauf3045d22015-04-29 16:24:50 +02002696 pat_lru_tree = lru64_new(global.tune.pattern_cache);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002697 HA_SPIN_INIT(&pat_lru_tree_lock);
Emeric Brunb5997f72017-07-03 11:34:05 +02002698 }
Willy Tarreauf3045d22015-04-29 16:24:50 +02002699
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002700 list_for_each_entry(ref, &pattern_reference, list) {
2701 if (ref->unique_id == -1) {
2702 /* Look for the first free id. */
2703 while (1) {
2704 list_for_each_entry(ref2, &pattern_reference, list) {
2705 if (ref2->unique_id == i) {
2706 i++;
2707 break;
2708 }
2709 }
Willy Tarreau3b786962014-04-26 12:37:25 +02002710 if (&ref2->list == &pattern_reference)
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002711 break;
2712 }
2713
2714 /* Uses the unique id and increment it for the next entry. */
2715 ref->unique_id = i;
2716 i++;
2717 }
2718 }
2719
2720 /* This sort the reference list by id. */
2721 list_for_each_entry_safe(ref, ref2, &pattern_reference, list) {
2722 LIST_DEL(&ref->list);
2723 list_for_each_entry(ref3, &pr, list) {
2724 if (ref->unique_id < ref3->unique_id) {
2725 LIST_ADDQ(&ref3->list, &ref->list);
2726 break;
2727 }
2728 }
2729 if (&ref3->list == &pr)
2730 LIST_ADDQ(&pr, &ref->list);
2731 }
2732
2733 /* swap root */
2734 LIST_ADD(&pr, &pattern_reference);
2735 LIST_DEL(&pr);
2736}