blob: b4cb8e9ea60ba642c2a5ede069eec54ebb024898 [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 */
151static struct pattern static_pattern;
152
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100153/* This is the root of the list of all pattern_ref avalaibles. */
154struct list pattern_reference = LIST_HEAD_INIT(pattern_reference);
155
Willy Tarreauf3045d22015-04-29 16:24:50 +0200156static struct lru64_head *pat_lru_tree;
157static unsigned long long pat_lru_seed;
158
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100159/*
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100160 *
161 * The following functions are not exported and are used by internals process
162 * of pattern matching
163 *
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100164 */
165
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100166/* Background: Fast way to find a zero byte in a word
167 * http://graphics.stanford.edu/~seander/bithacks.html#ZeroInWord
168 * hasZeroByte = (v - 0x01010101UL) & ~v & 0x80808080UL;
169 *
170 * To look for 4 different byte values, xor the word with those bytes and
171 * then check for zero bytes:
172 *
173 * v = (((unsigned char)c * 0x1010101U) ^ delimiter)
174 * where <delimiter> is the 4 byte values to look for (as an uint)
175 * and <c> is the character that is being tested
176 */
177static inline unsigned int is_delimiter(unsigned char c, unsigned int mask)
178{
179 mask ^= (c * 0x01010101); /* propagate the char to all 4 bytes */
180 return (mask - 0x01010101) & ~mask & 0x80808080U;
181}
182
183static inline unsigned int make_4delim(unsigned char d1, unsigned char d2, unsigned char d3, unsigned char d4)
184{
185 return d1 << 24 | d2 << 16 | d3 << 8 | d4;
186}
187
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100188
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100189/*
190 *
191 * These functions are exported and may be used by any other component.
192 *
Willy Tarreau5def8ef2014-08-29 15:19:33 +0200193 * The following functions are used for parsing pattern matching input value.
194 * The <text> contain the string to be parsed. <pattern> must be a preallocated
195 * pattern. The pat_parse_* functions fill this structure with the parsed value.
196 * <err> is filled with an error message built with memprintf() function. It is
197 * allowed to use a trash as a temporary storage for the returned pattern, as
198 * the next call after these functions will be pat_idx_*.
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100199 *
Willy Tarreau5def8ef2014-08-29 15:19:33 +0200200 * In success case, the pat_parse_* function returns 1. If the function
201 * fails, it returns 0 and <err> is filled.
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100202 */
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100203
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100204/* ignore the current line */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200205int pat_parse_nothing(const char *text, struct pattern *pattern, int mflags, char **err)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100206{
207 return 1;
208}
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100209
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100210/* Parse a string. It is allocated and duplicated. */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200211int pat_parse_str(const char *text, struct pattern *pattern, int mflags, char **err)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100212{
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100213 pattern->type = SMP_T_STR;
Thierry FOURNIERedc15c32013-12-13 15:36:59 +0100214 pattern->ptr.str = (char *)text;
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100215 pattern->len = strlen(text);
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100216 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100217}
218
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100219/* Parse a binary written in hexa. It is allocated. */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200220int pat_parse_bin(const char *text, struct pattern *pattern, int mflags, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100221{
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100222 struct chunk *trash;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100223
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100224 pattern->type = SMP_T_BIN;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100225 trash = get_trash_chunk();
226 pattern->len = trash->size;
227 pattern->ptr.str = trash->str;
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100228 return !!parse_binary(text, &pattern->ptr.str, &pattern->len, err);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100229}
230
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100231/* Parse a regex. It is allocated. */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200232int pat_parse_reg(const char *text, struct pattern *pattern, int mflags, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100233{
Thierry FOURNIER0b6d15f2014-01-29 19:35:16 +0100234 pattern->ptr.str = (char *)text;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100235 return 1;
236}
237
238/* Parse a range of positive integers delimited by either ':' or '-'. If only
239 * one integer is read, it is set as both min and max. An operator may be
240 * specified as the prefix, among this list of 5 :
241 *
242 * 0:eq, 1:gt, 2:ge, 3:lt, 4:le
243 *
244 * The default operator is "eq". It supports range matching. Ranges are
245 * rejected for other operators. The operator may be changed at any time.
246 * The operator is stored in the 'opaque' argument.
247 *
248 * If err is non-NULL, an error message will be returned there on errors and
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100249 * the caller will have to free it. The function returns zero on error, and
250 * non-zero on success.
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100251 *
252 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200253int pat_parse_int(const char *text, struct pattern *pattern, int mflags, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100254{
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100255 const char *ptr = text;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100256
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200257 pattern->type = SMP_T_SINT;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100258
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100259 /* Empty string is not valid */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100260 if (!*text)
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100261 goto not_valid_range;
262
263 /* Search ':' or '-' separator. */
264 while (*ptr != '\0' && *ptr != ':' && *ptr != '-')
265 ptr++;
266
267 /* If separator not found. */
268 if (!*ptr) {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100269 if (strl2llrc(text, ptr - text, &pattern->val.range.min) != 0) {
270 memprintf(err, "'%s' is not a number", text);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100271 return 0;
272 }
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100273 pattern->val.range.max = pattern->val.range.min;
274 pattern->val.range.min_set = 1;
275 pattern->val.range.max_set = 1;
276 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100277 }
278
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100279 /* If the separator is the first character. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100280 if (ptr == text && *(ptr + 1) != '\0') {
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100281 if (strl2llrc(ptr + 1, strlen(ptr + 1), &pattern->val.range.max) != 0)
282 goto not_valid_range;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100283
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100284 pattern->val.range.min_set = 0;
285 pattern->val.range.max_set = 1;
286 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100287 }
288
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100289 /* If separator is the last character. */
290 if (*(ptr + 1) == '\0') {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100291 if (strl2llrc(text, ptr - text, &pattern->val.range.min) != 0)
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100292 goto not_valid_range;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100293
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100294 pattern->val.range.min_set = 1;
295 pattern->val.range.max_set = 0;
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100296 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100297 }
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100298
299 /* Else, parse two numbers. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100300 if (strl2llrc(text, ptr - text, &pattern->val.range.min) != 0)
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100301 goto not_valid_range;
302
303 if (strl2llrc(ptr + 1, strlen(ptr + 1), &pattern->val.range.max) != 0)
304 goto not_valid_range;
305
306 if (pattern->val.range.min > pattern->val.range.max)
307 goto not_valid_range;
308
309 pattern->val.range.min_set = 1;
310 pattern->val.range.max_set = 1;
311 return 1;
312
313 not_valid_range:
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100314 memprintf(err, "'%s' is not a valid number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100315 return 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100316}
317
318/* Parse a range of positive 2-component versions delimited by either ':' or
319 * '-'. The version consists in a major and a minor, both of which must be
320 * smaller than 65536, because internally they will be represented as a 32-bit
321 * integer.
322 * If only one version is read, it is set as both min and max. Just like for
323 * pure integers, an operator may be specified as the prefix, among this list
324 * of 5 :
325 *
326 * 0:eq, 1:gt, 2:ge, 3:lt, 4:le
327 *
328 * The default operator is "eq". It supports range matching. Ranges are
329 * rejected for other operators. The operator may be changed at any time.
330 * The operator is stored in the 'opaque' argument. This allows constructs
331 * such as the following one :
332 *
333 * acl obsolete_ssl ssl_req_proto lt 3
334 * acl unsupported_ssl ssl_req_proto gt 3.1
335 * acl valid_ssl ssl_req_proto 3.0-3.1
336 *
337 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200338int pat_parse_dotted_ver(const char *text, struct pattern *pattern, int mflags, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100339{
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100340 const char *ptr = text;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100341
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200342 pattern->type = SMP_T_SINT;
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100343
344 /* Search ':' or '-' separator. */
345 while (*ptr != '\0' && *ptr != ':' && *ptr != '-')
346 ptr++;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100347
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100348 /* If separator not found. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100349 if (*ptr == '\0' && ptr > text) {
350 if (strl2llrc_dotted(text, ptr-text, &pattern->val.range.min) != 0) {
351 memprintf(err, "'%s' is not a dotted number", text);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100352 return 0;
353 }
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100354 pattern->val.range.max = pattern->val.range.min;
355 pattern->val.range.min_set = 1;
356 pattern->val.range.max_set = 1;
357 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100358 }
359
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100360 /* If the separator is the first character. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100361 if (ptr == text && *(ptr+1) != '\0') {
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100362 if (strl2llrc_dotted(ptr+1, strlen(ptr+1), &pattern->val.range.max) != 0) {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100363 memprintf(err, "'%s' is not a valid dotted number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100364 return 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100365 }
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100366 pattern->val.range.min_set = 0;
367 pattern->val.range.max_set = 1;
368 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100369 }
370
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100371 /* If separator is the last character. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100372 if (ptr == &text[strlen(text)-1]) {
373 if (strl2llrc_dotted(text, ptr-text, &pattern->val.range.min) != 0) {
374 memprintf(err, "'%s' is not a valid dotted number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100375 return 0;
376 }
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100377 pattern->val.range.min_set = 1;
378 pattern->val.range.max_set = 0;
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100379 return 1;
380 }
381
382 /* Else, parse two numbers. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100383 if (strl2llrc_dotted(text, ptr-text, &pattern->val.range.min) != 0) {
384 memprintf(err, "'%s' is not a valid dotted number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100385 return 0;
386 }
387 if (strl2llrc_dotted(ptr+1, strlen(ptr+1), &pattern->val.range.max) != 0) {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100388 memprintf(err, "'%s' is not a valid dotted number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100389 return 0;
390 }
391 if (pattern->val.range.min > pattern->val.range.max) {
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;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100394 }
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100395 pattern->val.range.min_set = 1;
396 pattern->val.range.max_set = 1;
397 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100398}
399
400/* Parse an IP address and an optional mask in the form addr[/mask].
401 * The addr may either be an IPv4 address or a hostname. The mask
402 * may either be a dotted mask or a number of bits. Returns 1 if OK,
403 * otherwise 0. NOTE: IP address patterns are typed (IPV4/IPV6).
404 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200405int pat_parse_ip(const char *text, struct pattern *pattern, int mflags, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100406{
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200407 if (str2net(text, !(mflags & PAT_MF_NO_DNS) && (global.mode & MODE_STARTING),
Thierry FOURNIERfc7ac7b2014-02-11 15:23:04 +0100408 &pattern->val.ipv4.addr, &pattern->val.ipv4.mask)) {
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100409 pattern->type = SMP_T_IPV4;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100410 return 1;
411 }
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100412 else if (str62net(text, &pattern->val.ipv6.addr, &pattern->val.ipv6.mask)) {
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100413 pattern->type = SMP_T_IPV6;
414 return 1;
415 }
416 else {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100417 memprintf(err, "'%s' is not a valid IPv4 or IPv6 address", text);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100418 return 0;
419 }
420}
421
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100422/*
423 *
424 * These functions are exported and may be used by any other component.
425 *
426 * This fucntion just take a sample <smp> and check if this sample match
427 * with the pattern <pattern>. This fucntion return just PAT_MATCH or
428 * PAT_NOMATCH.
429 *
430 */
431
432/* always return false */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100433struct pattern *pat_match_nothing(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100434{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200435 if (smp->data.u.sint) {
Thierry FOURNIERe5978bf2014-03-17 19:53:10 +0100436 if (fill) {
Thierry FOURNIER503bb092015-08-19 08:35:43 +0200437 static_pattern.data = NULL;
Thierry FOURNIERe5978bf2014-03-17 19:53:10 +0100438 static_pattern.ref = NULL;
Thierry FOURNIERe5978bf2014-03-17 19:53:10 +0100439 static_pattern.type = 0;
440 static_pattern.ptr.str = NULL;
441 }
442 return &static_pattern;
443 }
444 else
445 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100446}
447
448
449/* NB: For two strings to be identical, it is required that their lengths match */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100450struct pattern *pat_match_str(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100451{
452 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100453 struct ebmb_node *node;
454 char prev;
455 struct pattern_tree *elt;
456 struct pattern_list *lst;
457 struct pattern *pattern;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200458 struct pattern *ret = NULL;
459 struct lru64 *lru = NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100460
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100461 /* Lookup a string in the expression's pattern tree. */
462 if (!eb_is_empty(&expr->pattern_tree)) {
463 /* we may have to force a trailing zero on the test pattern */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200464 prev = smp->data.u.str.str[smp->data.u.str.len];
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100465 if (prev)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200466 smp->data.u.str.str[smp->data.u.str.len] = '\0';
467 node = ebst_lookup(&expr->pattern_tree, smp->data.u.str.str);
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100468 if (prev)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200469 smp->data.u.str.str[smp->data.u.str.len] = prev;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100470
471 if (node) {
472 if (fill) {
473 elt = ebmb_entry(node, struct pattern_tree, node);
Thierry FOURNIER503bb092015-08-19 08:35:43 +0200474 static_pattern.data = elt->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +0100475 static_pattern.ref = elt->ref;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200476 static_pattern.sflags = PAT_SF_TREE;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100477 static_pattern.type = SMP_T_STR;
478 static_pattern.ptr.str = (char *)elt->node.key;
479 }
480 return &static_pattern;
481 }
482 }
483
484 /* look in the list */
Willy Tarreauf3045d22015-04-29 16:24:50 +0200485 if (pat_lru_tree) {
Willy Tarreauaee93142015-05-04 17:18:42 +0200486 unsigned long long seed = pat_lru_seed ^ (long)expr;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200487
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200488 lru = lru64_get(XXH64(smp->data.u.str.str, smp->data.u.str.len, seed),
Willy Tarreauf3045d22015-04-29 16:24:50 +0200489 pat_lru_tree, expr, expr->revision);
490 if (lru && lru->domain)
491 return lru->data;
492 }
493
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100494 list_for_each_entry(lst, &expr->patterns, list) {
495 pattern = &lst->pat;
496
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200497 if (pattern->len != smp->data.u.str.len)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100498 continue;
499
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200500 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200501 if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.str.str, smp->data.u.str.len) == 0) ||
502 (!icase && strncmp(pattern->ptr.str, smp->data.u.str.str, smp->data.u.str.len) == 0)) {
Willy Tarreauf3045d22015-04-29 16:24:50 +0200503 ret = pattern;
504 break;
505 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100506 }
507
Willy Tarreauf3045d22015-04-29 16:24:50 +0200508 if (lru)
Christopher Fauletf90ac552015-06-09 17:06:17 +0200509 lru64_commit(lru, ret, expr, expr->revision, NULL);
Willy Tarreauf3045d22015-04-29 16:24:50 +0200510
511 return ret;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100512}
513
514/* NB: For two binaries buf to be identical, it is required that their lengths match */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100515struct pattern *pat_match_bin(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100516{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100517 struct pattern_list *lst;
518 struct pattern *pattern;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200519 struct pattern *ret = NULL;
520 struct lru64 *lru = NULL;
521
522 if (pat_lru_tree) {
Willy Tarreauaee93142015-05-04 17:18:42 +0200523 unsigned long long seed = pat_lru_seed ^ (long)expr;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100524
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200525 lru = lru64_get(XXH64(smp->data.u.str.str, smp->data.u.str.len, seed),
Willy Tarreauf3045d22015-04-29 16:24:50 +0200526 pat_lru_tree, expr, expr->revision);
527 if (lru && lru->domain)
528 return lru->data;
529 }
530
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100531 list_for_each_entry(lst, &expr->patterns, list) {
532 pattern = &lst->pat;
533
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200534 if (pattern->len != smp->data.u.str.len)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100535 continue;
536
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200537 if (memcmp(pattern->ptr.str, smp->data.u.str.str, smp->data.u.str.len) == 0) {
Willy Tarreauf3045d22015-04-29 16:24:50 +0200538 ret = pattern;
539 break;
540 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100541 }
542
Willy Tarreauf3045d22015-04-29 16:24:50 +0200543 if (lru)
Christopher Fauletf90ac552015-06-09 17:06:17 +0200544 lru64_commit(lru, ret, expr, expr->revision, NULL);
Willy Tarreauf3045d22015-04-29 16:24:50 +0200545
546 return ret;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100547}
548
549/* Executes a regex. It temporarily changes the data to add a trailing zero,
Thierry Fournier8feaa662016-02-10 22:55:20 +0100550 * and restores the previous character when leaving. This function fills
551 * a matching array.
552 */
553struct pattern *pat_match_regm(struct sample *smp, struct pattern_expr *expr, int fill)
554{
555 struct pattern_list *lst;
556 struct pattern *pattern;
557 struct pattern *ret = NULL;
558
559 list_for_each_entry(lst, &expr->patterns, list) {
560 pattern = &lst->pat;
561
562 if (regex_exec_match2(pattern->ptr.reg, smp->data.u.str.str, smp->data.u.str.len,
563 MAX_MATCH, pmatch, 0)) {
564 ret = pattern;
565 smp->ctx.a[0] = pmatch;
566 break;
567 }
568 }
569
570 return ret;
571}
572
573/* Executes a regex. It temporarily changes the data to add a trailing zero,
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100574 * and restores the previous character when leaving.
575 */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100576struct pattern *pat_match_reg(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100577{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100578 struct pattern_list *lst;
579 struct pattern *pattern;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200580 struct pattern *ret = NULL;
581 struct lru64 *lru = NULL;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100582
Willy Tarreauf3045d22015-04-29 16:24:50 +0200583 if (pat_lru_tree) {
Willy Tarreauaee93142015-05-04 17:18:42 +0200584 unsigned long long seed = pat_lru_seed ^ (long)expr;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200585
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200586 lru = lru64_get(XXH64(smp->data.u.str.str, smp->data.u.str.len, seed),
Willy Tarreauf3045d22015-04-29 16:24:50 +0200587 pat_lru_tree, expr, expr->revision);
588 if (lru && lru->domain)
589 return lru->data;
590 }
591
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100592 list_for_each_entry(lst, &expr->patterns, list) {
593 pattern = &lst->pat;
594
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200595 if (regex_exec2(pattern->ptr.reg, smp->data.u.str.str, smp->data.u.str.len)) {
Willy Tarreauf3045d22015-04-29 16:24:50 +0200596 ret = pattern;
597 break;
598 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100599 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200600
601 if (lru)
Christopher Fauletf90ac552015-06-09 17:06:17 +0200602 lru64_commit(lru, ret, expr, expr->revision, NULL);
Willy Tarreauf3045d22015-04-29 16:24:50 +0200603
604 return ret;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100605}
606
607/* Checks that the pattern matches the beginning of the tested string. */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100608struct pattern *pat_match_beg(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100609{
610 int icase;
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +0200611 struct ebmb_node *node;
612 char prev;
613 struct pattern_tree *elt;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100614 struct pattern_list *lst;
615 struct pattern *pattern;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200616 struct pattern *ret = NULL;
617 struct lru64 *lru = NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100618
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +0200619 /* Lookup a string in the expression's pattern tree. */
620 if (!eb_is_empty(&expr->pattern_tree)) {
621 /* we may have to force a trailing zero on the test pattern */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200622 prev = smp->data.u.str.str[smp->data.u.str.len];
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +0200623 if (prev)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200624 smp->data.u.str.str[smp->data.u.str.len] = '\0';
625 node = ebmb_lookup_longest(&expr->pattern_tree, smp->data.u.str.str);
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +0200626 if (prev)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200627 smp->data.u.str.str[smp->data.u.str.len] = prev;
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +0200628
629 if (node) {
630 if (fill) {
631 elt = ebmb_entry(node, struct pattern_tree, node);
Thierry FOURNIER503bb092015-08-19 08:35:43 +0200632 static_pattern.data = elt->data;
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +0200633 static_pattern.ref = elt->ref;
634 static_pattern.sflags = PAT_SF_TREE;
635 static_pattern.type = SMP_T_STR;
636 static_pattern.ptr.str = (char *)elt->node.key;
637 }
638 return &static_pattern;
639 }
640 }
641
642 /* look in the list */
Willy Tarreauf3045d22015-04-29 16:24:50 +0200643 if (pat_lru_tree) {
Willy Tarreauaee93142015-05-04 17:18:42 +0200644 unsigned long long seed = pat_lru_seed ^ (long)expr;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200645
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200646 lru = lru64_get(XXH64(smp->data.u.str.str, smp->data.u.str.len, seed),
Willy Tarreauf3045d22015-04-29 16:24:50 +0200647 pat_lru_tree, expr, expr->revision);
648 if (lru && lru->domain)
649 return lru->data;
650 }
651
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100652 list_for_each_entry(lst, &expr->patterns, list) {
653 pattern = &lst->pat;
654
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200655 if (pattern->len > smp->data.u.str.len)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100656 continue;
657
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200658 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200659 if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.str.str, pattern->len) != 0) ||
660 (!icase && strncmp(pattern->ptr.str, smp->data.u.str.str, pattern->len) != 0))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100661 continue;
662
Willy Tarreauf3045d22015-04-29 16:24:50 +0200663 ret = pattern;
664 break;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100665 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200666
667 if (lru)
Christopher Fauletf90ac552015-06-09 17:06:17 +0200668 lru64_commit(lru, ret, expr, expr->revision, NULL);
Willy Tarreauf3045d22015-04-29 16:24:50 +0200669
670 return ret;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100671}
672
673/* Checks that the pattern matches the end of the tested string. */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100674struct pattern *pat_match_end(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100675{
676 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100677 struct pattern_list *lst;
678 struct pattern *pattern;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200679 struct pattern *ret = NULL;
680 struct lru64 *lru = NULL;
681
682 if (pat_lru_tree) {
Willy Tarreauaee93142015-05-04 17:18:42 +0200683 unsigned long long seed = pat_lru_seed ^ (long)expr;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200684
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200685 lru = lru64_get(XXH64(smp->data.u.str.str, smp->data.u.str.len, seed),
Willy Tarreauf3045d22015-04-29 16:24:50 +0200686 pat_lru_tree, expr, expr->revision);
687 if (lru && lru->domain)
688 return lru->data;
689 }
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100690
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 + smp->data.u.str.len - pattern->len, pattern->len) != 0) ||
699 (!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 +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
706 if (lru)
Christopher Fauletf90ac552015-06-09 17:06:17 +0200707 lru64_commit(lru, ret, expr, expr->revision, NULL);
Willy Tarreauf3045d22015-04-29 16:24:50 +0200708
709 return ret;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100710}
711
712/* Checks that the pattern is included inside the tested string.
713 * NB: Suboptimal, should be rewritten using a Boyer-Moore method.
714 */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100715struct pattern *pat_match_sub(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100716{
717 int icase;
718 char *end;
719 char *c;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100720 struct pattern_list *lst;
721 struct pattern *pattern;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200722 struct pattern *ret = NULL;
723 struct lru64 *lru = NULL;
724
725 if (pat_lru_tree) {
Willy Tarreauaee93142015-05-04 17:18:42 +0200726 unsigned long long seed = pat_lru_seed ^ (long)expr;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200727
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200728 lru = lru64_get(XXH64(smp->data.u.str.str, smp->data.u.str.len, seed),
Willy Tarreauf3045d22015-04-29 16:24:50 +0200729 pat_lru_tree, expr, expr->revision);
730 if (lru && lru->domain)
731 return lru->data;
732 }
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100733
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100734 list_for_each_entry(lst, &expr->patterns, list) {
735 pattern = &lst->pat;
736
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200737 if (pattern->len > smp->data.u.str.len)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100738 continue;
739
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200740 end = smp->data.u.str.str + smp->data.u.str.len - pattern->len;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200741 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100742 if (icase) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200743 for (c = smp->data.u.str.str; c <= end; c++) {
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100744 if (tolower(*c) != tolower(*pattern->ptr.str))
745 continue;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200746 if (strncasecmp(pattern->ptr.str, c, pattern->len) == 0) {
747 ret = pattern;
748 goto leave;
749 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100750 }
751 } else {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200752 for (c = smp->data.u.str.str; c <= end; c++) {
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100753 if (*c != *pattern->ptr.str)
754 continue;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200755 if (strncmp(pattern->ptr.str, c, pattern->len) == 0) {
756 ret = pattern;
757 goto leave;
758 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100759 }
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100760 }
761 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200762 leave:
763 if (lru)
Christopher Fauletf90ac552015-06-09 17:06:17 +0200764 lru64_commit(lru, ret, expr, expr->revision, NULL);
Willy Tarreauf3045d22015-04-29 16:24:50 +0200765
766 return ret;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100767}
768
769/* This one is used by other real functions. It checks that the pattern is
770 * included inside the tested string, but enclosed between the specified
771 * delimiters or at the beginning or end of the string. The delimiters are
772 * provided as an unsigned int made by make_4delim() and match up to 4 different
773 * delimiters. Delimiters are stripped at the beginning and end of the pattern.
774 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200775static int match_word(struct sample *smp, struct pattern *pattern, int mflags, unsigned int delimiters)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100776{
777 int may_match, icase;
778 char *c, *end;
779 char *ps;
780 int pl;
781
782 pl = pattern->len;
783 ps = pattern->ptr.str;
784
785 while (pl > 0 && is_delimiter(*ps, delimiters)) {
786 pl--;
787 ps++;
788 }
789
790 while (pl > 0 && is_delimiter(ps[pl - 1], delimiters))
791 pl--;
792
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200793 if (pl > smp->data.u.str.len)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100794 return PAT_NOMATCH;
795
796 may_match = 1;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200797 icase = mflags & PAT_MF_IGNORE_CASE;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200798 end = smp->data.u.str.str + smp->data.u.str.len - pl;
799 for (c = smp->data.u.str.str; c <= end; c++) {
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100800 if (is_delimiter(*c, delimiters)) {
801 may_match = 1;
802 continue;
803 }
804
805 if (!may_match)
806 continue;
807
808 if (icase) {
809 if ((tolower(*c) == tolower(*ps)) &&
810 (strncasecmp(ps, c, pl) == 0) &&
811 (c == end || is_delimiter(c[pl], delimiters)))
812 return PAT_MATCH;
813 } else {
814 if ((*c == *ps) &&
815 (strncmp(ps, c, pl) == 0) &&
816 (c == end || is_delimiter(c[pl], delimiters)))
817 return PAT_MATCH;
818 }
819 may_match = 0;
820 }
821 return PAT_NOMATCH;
822}
823
824/* Checks that the pattern is included inside the tested string, but enclosed
825 * between the delimiters '?' or '/' or at the beginning or end of the string.
826 * Delimiters at the beginning or end of the pattern are ignored.
827 */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100828struct pattern *pat_match_dir(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100829{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100830 struct pattern_list *lst;
831 struct pattern *pattern;
832
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100833 list_for_each_entry(lst, &expr->patterns, list) {
834 pattern = &lst->pat;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200835 if (match_word(smp, pattern, expr->mflags, make_4delim('/', '?', '?', '?')))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100836 return pattern;
837 }
838 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100839}
840
841/* Checks that the pattern is included inside the tested string, but enclosed
842 * between the delmiters '/', '?', '.' or ":" or at the beginning or end of
843 * the string. Delimiters at the beginning or end of the pattern are ignored.
844 */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100845struct pattern *pat_match_dom(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100846{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100847 struct pattern_list *lst;
848 struct pattern *pattern;
849
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100850 list_for_each_entry(lst, &expr->patterns, list) {
851 pattern = &lst->pat;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200852 if (match_word(smp, pattern, expr->mflags, make_4delim('/', '?', '.', ':')))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100853 return pattern;
854 }
855 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100856}
857
858/* Checks that the integer in <test> is included between min and max */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100859struct pattern *pat_match_int(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100860{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100861 struct pattern_list *lst;
862 struct pattern *pattern;
863
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100864 list_for_each_entry(lst, &expr->patterns, list) {
865 pattern = &lst->pat;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200866 if ((!pattern->val.range.min_set || pattern->val.range.min <= smp->data.u.sint) &&
867 (!pattern->val.range.max_set || smp->data.u.sint <= pattern->val.range.max))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100868 return pattern;
869 }
870 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100871}
872
873/* Checks that the length of the pattern in <test> is included between min and max */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100874struct pattern *pat_match_len(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100875{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100876 struct pattern_list *lst;
877 struct pattern *pattern;
878
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100879 list_for_each_entry(lst, &expr->patterns, list) {
880 pattern = &lst->pat;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200881 if ((!pattern->val.range.min_set || pattern->val.range.min <= smp->data.u.str.len) &&
882 (!pattern->val.range.max_set || smp->data.u.str.len <= pattern->val.range.max))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100883 return pattern;
884 }
885 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100886}
887
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100888struct pattern *pat_match_ip(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100889{
890 unsigned int v4; /* in network byte order */
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100891 struct in6_addr tmp6;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100892 struct in_addr *s;
893 struct ebmb_node *node;
894 struct pattern_tree *elt;
895 struct pattern_list *lst;
896 struct pattern *pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100897
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100898 /* The input sample is IPv4. Try to match in the trees. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200899 if (smp->data.type == SMP_T_IPV4) {
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100900 /* Lookup an IPv4 address in the expression's pattern tree using
901 * the longest match method.
902 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200903 s = &smp->data.u.ipv4;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100904 node = ebmb_lookup_longest(&expr->pattern_tree, &s->s_addr);
905 if (node) {
906 if (fill) {
907 elt = ebmb_entry(node, struct pattern_tree, node);
Thierry FOURNIER503bb092015-08-19 08:35:43 +0200908 static_pattern.data = elt->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +0100909 static_pattern.ref = elt->ref;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200910 static_pattern.sflags = PAT_SF_TREE;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100911 static_pattern.type = SMP_T_IPV4;
912 memcpy(&static_pattern.val.ipv4.addr.s_addr, elt->node.key, 4);
913 if (!cidr2dotted(elt->node.node.pfx, &static_pattern.val.ipv4.mask))
914 return NULL;
915 }
916 return &static_pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100917 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100918
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100919 /* The IPv4 sample dont match the IPv4 tree. Convert the IPv4
920 * sample address to IPv6 with the mapping method using the ::ffff:
921 * prefix, and try to lookup in the IPv6 tree.
922 */
923 memset(&tmp6, 0, 10);
924 *(uint16_t*)&tmp6.s6_addr[10] = htons(0xffff);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200925 *(uint32_t*)&tmp6.s6_addr[12] = smp->data.u.ipv4.s_addr;
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100926 node = ebmb_lookup_longest(&expr->pattern_tree_2, &tmp6);
927 if (node) {
928 if (fill) {
929 elt = ebmb_entry(node, struct pattern_tree, node);
Thierry FOURNIER503bb092015-08-19 08:35:43 +0200930 static_pattern.data = elt->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +0100931 static_pattern.ref = elt->ref;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200932 static_pattern.sflags = PAT_SF_TREE;
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100933 static_pattern.type = SMP_T_IPV6;
934 memcpy(&static_pattern.val.ipv6.addr, elt->node.key, 16);
935 static_pattern.val.ipv6.mask = elt->node.node.pfx;
936 }
937 return &static_pattern;
938 }
939 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100940
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100941 /* The input sample is IPv6. Try to match in the trees. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200942 if (smp->data.type == SMP_T_IPV6) {
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100943 /* Lookup an IPv6 address in the expression's pattern tree using
944 * the longest match method.
945 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200946 node = ebmb_lookup_longest(&expr->pattern_tree_2, &smp->data.u.ipv6);
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100947 if (node) {
948 if (fill) {
949 elt = ebmb_entry(node, struct pattern_tree, node);
Thierry FOURNIER503bb092015-08-19 08:35:43 +0200950 static_pattern.data = elt->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +0100951 static_pattern.ref = elt->ref;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200952 static_pattern.sflags = PAT_SF_TREE;
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100953 static_pattern.type = SMP_T_IPV6;
954 memcpy(&static_pattern.val.ipv6.addr, elt->node.key, 16);
955 static_pattern.val.ipv6.mask = elt->node.node.pfx;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100956 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100957 return &static_pattern;
958 }
959
960 /* Try to convert 6 to 4 when the start of the ipv6 address match the
961 * following forms :
962 * - ::ffff:ip:v4 (ipv4 mapped)
963 * - ::0000:ip:v4 (old ipv4 mapped)
964 * - 2002:ip:v4:: (6to4)
965 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200966 if ((*(uint32_t*)&smp->data.u.ipv6.s6_addr[0] == 0 &&
967 *(uint32_t*)&smp->data.u.ipv6.s6_addr[4] == 0 &&
968 (*(uint32_t*)&smp->data.u.ipv6.s6_addr[8] == 0 ||
969 *(uint32_t*)&smp->data.u.ipv6.s6_addr[8] == htonl(0xFFFF))) ||
970 *(uint16_t*)&smp->data.u.ipv6.s6_addr[0] == htons(0x2002)) {
971 if (*(uint32_t*)&smp->data.u.ipv6.s6_addr[0] == 0)
972 v4 = *(uint32_t*)&smp->data.u.ipv6.s6_addr[12];
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100973 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200974 v4 = htonl((ntohs(*(uint16_t*)&smp->data.u.ipv6.s6_addr[2]) << 16) +
975 ntohs(*(uint16_t*)&smp->data.u.ipv6.s6_addr[4]));
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100976
977 /* Lookup an IPv4 address in the expression's pattern tree using the longest
978 * match method.
979 */
980 node = ebmb_lookup_longest(&expr->pattern_tree, &v4);
981 if (node) {
982 if (fill) {
983 elt = ebmb_entry(node, struct pattern_tree, node);
Thierry FOURNIER503bb092015-08-19 08:35:43 +0200984 static_pattern.data = elt->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +0100985 static_pattern.ref = elt->ref;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200986 static_pattern.sflags = PAT_SF_TREE;
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100987 static_pattern.type = SMP_T_IPV4;
988 memcpy(&static_pattern.val.ipv4.addr.s_addr, elt->node.key, 4);
989 if (!cidr2dotted(elt->node.node.pfx, &static_pattern.val.ipv4.mask))
990 return NULL;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100991 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100992 return &static_pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100993 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100994 }
995 }
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100996
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100997 /* Lookup in the list. the list contain only IPv4 patterns */
998 list_for_each_entry(lst, &expr->patterns, list) {
999 pattern = &lst->pat;
1000
1001 /* The input sample is IPv4, use it as is. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001002 if (smp->data.type == SMP_T_IPV4) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001003 v4 = smp->data.u.ipv4.s_addr;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +01001004 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001005 else if (smp->data.type == SMP_T_IPV6) {
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001006 /* v4 match on a V6 sample. We want to check at least for
1007 * the following forms :
1008 * - ::ffff:ip:v4 (ipv4 mapped)
1009 * - ::0000:ip:v4 (old ipv4 mapped)
1010 * - 2002:ip:v4:: (6to4)
1011 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001012 if (*(uint32_t*)&smp->data.u.ipv6.s6_addr[0] == 0 &&
1013 *(uint32_t*)&smp->data.u.ipv6.s6_addr[4] == 0 &&
1014 (*(uint32_t*)&smp->data.u.ipv6.s6_addr[8] == 0 ||
1015 *(uint32_t*)&smp->data.u.ipv6.s6_addr[8] == htonl(0xFFFF))) {
1016 v4 = *(uint32_t*)&smp->data.u.ipv6.s6_addr[12];
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01001017 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001018 else if (*(uint16_t*)&smp->data.u.ipv6.s6_addr[0] == htons(0x2002)) {
1019 v4 = htonl((ntohs(*(uint16_t*)&smp->data.u.ipv6.s6_addr[2]) << 16) +
1020 ntohs(*(uint16_t*)&smp->data.u.ipv6.s6_addr[4]));
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01001021 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001022 else
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01001023 continue;
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001024 }
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +01001025
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001026 /* Check if the input sample match the current pattern. */
1027 if (((v4 ^ pattern->val.ipv4.addr.s_addr) & pattern->val.ipv4.mask.s_addr) == 0)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01001028 return pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +01001029 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01001030 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +01001031}
1032
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001033void free_pattern_tree(struct eb_root *root)
1034{
1035 struct eb_node *node, *next;
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +01001036 struct pattern_tree *elt;
Thierry FOURNIER3ce88c72013-12-09 11:29:46 +01001037
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001038 node = eb_first(root);
1039 while (node) {
1040 next = eb_next(node);
1041 eb_delete(node);
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +01001042 elt = container_of(node, struct pattern_tree, node);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001043 free(elt->data);
Thierry FOURNIER3ce88c72013-12-09 11:29:46 +01001044 free(elt);
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001045 node = next;
1046 }
1047}
1048
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01001049void pat_prune_val(struct pattern_expr *expr)
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +01001050{
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01001051 struct pattern_list *pat, *tmp;
1052
1053 list_for_each_entry_safe(pat, tmp, &expr->patterns, list) {
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001054 free(pat->pat.data);
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01001055 free(pat);
1056 }
1057
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +01001058 free_pattern_tree(&expr->pattern_tree);
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001059 free_pattern_tree(&expr->pattern_tree_2);
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +01001060 LIST_INIT(&expr->patterns);
1061}
1062
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01001063void pat_prune_ptr(struct pattern_expr *expr)
1064{
1065 struct pattern_list *pat, *tmp;
1066
1067 list_for_each_entry_safe(pat, tmp, &expr->patterns, list) {
1068 free(pat->pat.ptr.ptr);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001069 free(pat->pat.data);
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01001070 free(pat);
1071 }
1072
1073 free_pattern_tree(&expr->pattern_tree);
1074 free_pattern_tree(&expr->pattern_tree_2);
1075 LIST_INIT(&expr->patterns);
1076}
1077
1078void pat_prune_reg(struct pattern_expr *expr)
1079{
1080 struct pattern_list *pat, *tmp;
1081
1082 list_for_each_entry_safe(pat, tmp, &expr->patterns, list) {
1083 regex_free(pat->pat.ptr.ptr);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001084 free(pat->pat.data);
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01001085 free(pat);
1086 }
1087
1088 free_pattern_tree(&expr->pattern_tree);
1089 free_pattern_tree(&expr->pattern_tree_2);
1090 LIST_INIT(&expr->patterns);
1091}
1092
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001093/*
1094 *
1095 * The following functions are used for the pattern indexation
1096 *
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001097 */
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001098
1099int pat_idx_list_val(struct pattern_expr *expr, struct pattern *pat, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001100{
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001101 struct pattern_list *patl;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001102
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001103 /* allocate pattern */
1104 patl = calloc(1, sizeof(*patl));
1105 if (!patl) {
1106 memprintf(err, "out of memory while indexing pattern");
Thierry FOURNIER972028f2014-01-23 17:53:31 +01001107 return 0;
1108 }
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001109
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001110 /* duplicate pattern */
1111 memcpy(&patl->pat, pat, sizeof(*pat));
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001112
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001113 /* chain pattern in the expression */
1114 LIST_ADDQ(&expr->patterns, &patl->list);
Willy Tarreau72f073b2015-04-29 17:53:47 +02001115 expr->revision = rdtsc();
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001116
1117 /* that's ok */
1118 return 1;
1119}
1120
1121int pat_idx_list_ptr(struct pattern_expr *expr, struct pattern *pat, char **err)
1122{
1123 struct pattern_list *patl;
1124
1125 /* allocate pattern */
1126 patl = calloc(1, sizeof(*patl));
Thierry FOURNIER8aa83842015-02-06 17:50:55 +01001127 if (!patl) {
1128 memprintf(err, "out of memory while indexing pattern");
Thierry FOURNIER972028f2014-01-23 17:53:31 +01001129 return 0;
Thierry FOURNIER8aa83842015-02-06 17:50:55 +01001130 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001131
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001132 /* duplicate pattern */
1133 memcpy(&patl->pat, pat, sizeof(*pat));
1134 patl->pat.ptr.ptr = malloc(patl->pat.len);
1135 if (!patl->pat.ptr.ptr) {
1136 free(patl);
1137 memprintf(err, "out of memory while indexing pattern");
1138 return 0;
1139 }
1140 memcpy(patl->pat.ptr.ptr, pat->ptr.ptr, pat->len);
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001141
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001142 /* chain pattern in the expression */
1143 LIST_ADDQ(&expr->patterns, &patl->list);
Willy Tarreau72f073b2015-04-29 17:53:47 +02001144 expr->revision = rdtsc();
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001145
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001146 /* that's ok */
1147 return 1;
1148}
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001149
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001150int pat_idx_list_str(struct pattern_expr *expr, struct pattern *pat, char **err)
1151{
1152 struct pattern_list *patl;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001153
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001154 /* allocate pattern */
1155 patl = calloc(1, sizeof(*patl));
1156 if (!patl) {
1157 memprintf(err, "out of memory while indexing pattern");
1158 return 0;
1159 }
1160
1161 /* duplicate pattern */
1162 memcpy(&patl->pat, pat, sizeof(*pat));
1163 patl->pat.ptr.str = malloc(patl->pat.len + 1);
1164 if (!patl->pat.ptr.str) {
1165 free(patl);
1166 memprintf(err, "out of memory while indexing pattern");
1167 return 0;
1168 }
1169 memcpy(patl->pat.ptr.ptr, pat->ptr.ptr, pat->len);
1170 patl->pat.ptr.str[patl->pat.len] = '\0';
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001171
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001172 /* chain pattern in the expression */
1173 LIST_ADDQ(&expr->patterns, &patl->list);
Willy Tarreau72f073b2015-04-29 17:53:47 +02001174 expr->revision = rdtsc();
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001175
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001176 /* that's ok */
1177 return 1;
1178}
1179
Thierry Fournier8feaa662016-02-10 22:55:20 +01001180int pat_idx_list_reg_cap(struct pattern_expr *expr, struct pattern *pat, int cap, char **err)
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001181{
1182 struct pattern_list *patl;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001183
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001184 /* allocate pattern */
1185 patl = calloc(1, sizeof(*patl));
1186 if (!patl) {
1187 memprintf(err, "out of memory while indexing pattern");
1188 return 0;
Thierry FOURNIER972028f2014-01-23 17:53:31 +01001189 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001190
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001191 /* duplicate pattern */
1192 memcpy(&patl->pat, pat, sizeof(*pat));
1193
1194 /* allocate regex */
1195 patl->pat.ptr.reg = calloc(1, sizeof(*patl->pat.ptr.reg));
1196 if (!patl->pat.ptr.reg) {
1197 free(patl);
1198 memprintf(err, "out of memory while indexing pattern");
1199 return 0;
1200 }
1201
1202 /* compile regex */
Thierry Fournier8feaa662016-02-10 22:55:20 +01001203 if (!regex_comp(pat->ptr.str, patl->pat.ptr.reg,
1204 !(expr->mflags & PAT_MF_IGNORE_CASE), cap, err)) {
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001205 free(patl->pat.ptr.reg);
Dirkjan Bussink07fcaaa2014-04-28 22:57:16 +00001206 free(patl);
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001207 return 0;
1208 }
1209
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001210 /* chain pattern in the expression */
1211 LIST_ADDQ(&expr->patterns, &patl->list);
Willy Tarreau72f073b2015-04-29 17:53:47 +02001212 expr->revision = rdtsc();
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001213
1214 /* that's ok */
1215 return 1;
1216}
1217
Thierry Fournier8feaa662016-02-10 22:55:20 +01001218int pat_idx_list_reg(struct pattern_expr *expr, struct pattern *pat, char **err)
1219{
1220 return pat_idx_list_reg_cap(expr, pat, 0, err);
1221}
1222
1223int pat_idx_list_regm(struct pattern_expr *expr, struct pattern *pat, char **err)
1224{
1225 return pat_idx_list_reg_cap(expr, pat, 1, err);
1226}
1227
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001228int pat_idx_tree_ip(struct pattern_expr *expr, struct pattern *pat, char **err)
1229{
1230 unsigned int mask;
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +01001231 struct pattern_tree *node;
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001232
1233 /* Only IPv4 can be indexed */
1234 if (pat->type == SMP_T_IPV4) {
Thierry FOURNIER972028f2014-01-23 17:53:31 +01001235 /* in IPv4 case, check if the mask is contiguous so that we can
1236 * insert the network into the tree. A continuous mask has only
1237 * ones on the left. This means that this mask + its lower bit
1238 * added once again is null.
1239 */
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001240 mask = ntohl(pat->val.ipv4.mask.s_addr);
1241 if (mask + (mask & -mask) == 0) {
1242 mask = mask ? 33 - flsnz(mask & -mask) : 0; /* equals cidr value */
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001243
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001244 /* node memory allocation */
1245 node = calloc(1, sizeof(*node) + 4);
1246 if (!node) {
1247 memprintf(err, "out of memory while loading pattern");
1248 return 0;
1249 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001250
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001251 /* copy the pointer to sample associated to this node */
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001252 node->data = pat->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01001253 node->ref = pat->ref;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001254
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001255 /* FIXME: insert <addr>/<mask> into the tree here */
1256 memcpy(node->node.key, &pat->val.ipv4.addr, 4); /* network byte order */
1257 node->node.node.pfx = mask;
Thierry FOURNIER31db4ae2014-01-30 00:27:15 +01001258
1259 /* Insert the entry. */
1260 ebmb_insert_prefix(&expr->pattern_tree, &node->node, 4);
Willy Tarreau72f073b2015-04-29 17:53:47 +02001261 expr->revision = rdtsc();
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001262
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001263 /* that's ok */
1264 return 1;
1265 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001266 else {
1267 /* If the mask is not contiguous, just add the pattern to the list */
1268 return pat_idx_list_val(expr, pat, err);
1269 }
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001270 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001271 else if (pat->type == SMP_T_IPV6) {
1272 /* IPv6 also can be indexed */
1273 node = calloc(1, sizeof(*node) + 16);
1274 if (!node) {
1275 memprintf(err, "out of memory while loading pattern");
1276 return 0;
1277 }
1278
1279 /* copy the pointer to sample associated to this node */
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001280 node->data = pat->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01001281 node->ref = pat->ref;
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001282
1283 /* FIXME: insert <addr>/<mask> into the tree here */
1284 memcpy(node->node.key, &pat->val.ipv6.addr, 16); /* network byte order */
1285 node->node.node.pfx = pat->val.ipv6.mask;
Thierry FOURNIER31db4ae2014-01-30 00:27:15 +01001286
1287 /* Insert the entry. */
1288 ebmb_insert_prefix(&expr->pattern_tree_2, &node->node, 16);
Willy Tarreau72f073b2015-04-29 17:53:47 +02001289 expr->revision = rdtsc();
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001290
1291 /* that's ok */
1292 return 1;
1293 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001294
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001295 return 0;
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001296}
1297
1298int pat_idx_tree_str(struct pattern_expr *expr, struct pattern *pat, char **err)
1299{
1300 int len;
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +01001301 struct pattern_tree *node;
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001302
1303 /* Only string can be indexed */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001304 if (pat->type != SMP_T_STR) {
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001305 memprintf(err, "internal error: string expected, but the type is '%s'",
1306 smp_to_type[pat->type]);
1307 return 0;
Thierry FOURNIER972028f2014-01-23 17:53:31 +01001308 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001309
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001310 /* If the flag PAT_F_IGNORE_CASE is set, we cannot use trees */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02001311 if (expr->mflags & PAT_MF_IGNORE_CASE)
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001312 return pat_idx_list_str(expr, pat, err);
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001313
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001314 /* Process the key len */
1315 len = strlen(pat->ptr.str) + 1;
1316
1317 /* node memory allocation */
1318 node = calloc(1, sizeof(*node) + len);
1319 if (!node) {
1320 memprintf(err, "out of memory while loading pattern");
1321 return 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001322 }
1323
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001324 /* copy the pointer to sample associated to this node */
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001325 node->data = pat->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01001326 node->ref = pat->ref;
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001327
1328 /* copy the string */
1329 memcpy(node->node.key, pat->ptr.str, len);
1330
1331 /* index the new node */
Thierry FOURNIER31db4ae2014-01-30 00:27:15 +01001332 ebst_insert(&expr->pattern_tree, &node->node);
Willy Tarreau72f073b2015-04-29 17:53:47 +02001333 expr->revision = rdtsc();
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001334
1335 /* that's ok */
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001336 return 1;
1337}
1338
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +02001339int pat_idx_tree_pfx(struct pattern_expr *expr, struct pattern *pat, char **err)
1340{
1341 int len;
1342 struct pattern_tree *node;
1343
1344 /* Only string can be indexed */
1345 if (pat->type != SMP_T_STR) {
1346 memprintf(err, "internal error: string expected, but the type is '%s'",
1347 smp_to_type[pat->type]);
1348 return 0;
1349 }
1350
1351 /* If the flag PAT_F_IGNORE_CASE is set, we cannot use trees */
1352 if (expr->mflags & PAT_MF_IGNORE_CASE)
1353 return pat_idx_list_str(expr, pat, err);
1354
1355 /* Process the key len */
1356 len = strlen(pat->ptr.str);
1357
1358 /* node memory allocation */
1359 node = calloc(1, sizeof(*node) + len + 1);
1360 if (!node) {
1361 memprintf(err, "out of memory while loading pattern");
1362 return 0;
1363 }
1364
1365 /* copy the pointer to sample associated to this node */
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001366 node->data = pat->data;
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +02001367 node->ref = pat->ref;
1368
1369 /* copy the string and the trailing zero */
1370 memcpy(node->node.key, pat->ptr.str, len + 1);
1371 node->node.node.pfx = len * 8;
1372
1373 /* index the new node */
1374 ebmb_insert_prefix(&expr->pattern_tree, &node->node, len);
Willy Tarreau72f073b2015-04-29 17:53:47 +02001375 expr->revision = rdtsc();
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +02001376
1377 /* that's ok */
1378 return 1;
1379}
1380
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001381void pat_del_list_val(struct pattern_expr *expr, struct pat_ref_elt *ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001382{
1383 struct pattern_list *pat;
1384 struct pattern_list *safe;
1385
1386 list_for_each_entry_safe(pat, safe, &expr->patterns, list) {
1387 /* Check equality. */
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001388 if (pat->pat.ref != ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001389 continue;
1390
1391 /* Delete and free entry. */
1392 LIST_DEL(&pat->list);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001393 free(pat->pat.data);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001394 free(pat);
1395 }
Willy Tarreau72f073b2015-04-29 17:53:47 +02001396 expr->revision = rdtsc();
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001397}
1398
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001399void pat_del_tree_ip(struct pattern_expr *expr, struct pat_ref_elt *ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001400{
1401 struct ebmb_node *node, *next_node;
1402 struct pattern_tree *elt;
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001403
1404 /* browse each node of the tree for IPv4 addresses. */
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001405 for (node = ebmb_first(&expr->pattern_tree), next_node = node ? ebmb_next(node) : NULL;
1406 node;
1407 node = next_node, next_node = node ? ebmb_next(node) : NULL) {
1408 /* Extract container of the tree node. */
1409 elt = container_of(node, struct pattern_tree, node);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001410
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001411 /* Check equality. */
1412 if (elt->ref != ref)
1413 continue;
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001414
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001415 /* Delete and free entry. */
1416 ebmb_delete(node);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001417 free(elt->data);
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001418 free(elt);
1419 }
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001420
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001421 /* Browse each node of the list for IPv4 addresses. */
1422 pat_del_list_val(expr, ref);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001423
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001424 /* browse each node of the tree for IPv6 addresses. */
1425 for (node = ebmb_first(&expr->pattern_tree_2), next_node = node ? ebmb_next(node) : NULL;
1426 node;
1427 node = next_node, next_node = node ? ebmb_next(node) : NULL) {
1428 /* Extract container of the tree node. */
1429 elt = container_of(node, struct pattern_tree, node);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001430
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001431 /* Check equality. */
1432 if (elt->ref != ref)
1433 continue;
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001434
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001435 /* Delete and free entry. */
1436 ebmb_delete(node);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001437 free(elt->data);
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001438 free(elt);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001439 }
Willy Tarreau72f073b2015-04-29 17:53:47 +02001440 expr->revision = rdtsc();
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001441}
1442
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001443void pat_del_list_ptr(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);
1455 free(pat->pat.ptr.ptr);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001456 free(pat->pat.data);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001457 free(pat);
1458 }
Willy Tarreau72f073b2015-04-29 17:53:47 +02001459 expr->revision = rdtsc();
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001460}
1461
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001462void pat_del_tree_str(struct pattern_expr *expr, struct pat_ref_elt *ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001463{
1464 struct ebmb_node *node, *next_node;
1465 struct pattern_tree *elt;
1466
Thierry FOURNIER73bc2852015-02-06 17:53:54 +01001467 /* If the flag PAT_F_IGNORE_CASE is set, we cannot use trees */
1468 if (expr->mflags & PAT_MF_IGNORE_CASE)
1469 return pat_del_list_ptr(expr, ref);
1470
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001471 /* browse each node of the tree. */
1472 for (node = ebmb_first(&expr->pattern_tree), next_node = node ? ebmb_next(node) : NULL;
1473 node;
1474 node = next_node, next_node = node ? ebmb_next(node) : NULL) {
1475 /* Extract container of the tree node. */
1476 elt = container_of(node, struct pattern_tree, node);
1477
1478 /* Check equality. */
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001479 if (elt->ref != ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001480 continue;
1481
1482 /* Delete and free entry. */
1483 ebmb_delete(node);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001484 free(elt->data);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001485 free(elt);
1486 }
Willy Tarreau72f073b2015-04-29 17:53:47 +02001487 expr->revision = rdtsc();
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001488}
1489
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001490void pat_del_list_reg(struct pattern_expr *expr, struct pat_ref_elt *ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001491{
1492 struct pattern_list *pat;
1493 struct pattern_list *safe;
1494
1495 list_for_each_entry_safe(pat, safe, &expr->patterns, list) {
1496 /* Check equality. */
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001497 if (pat->pat.ref != ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001498 continue;
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001499
1500 /* Delete and free entry. */
1501 LIST_DEL(&pat->list);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001502 regex_free(pat->pat.ptr.ptr);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001503 free(pat->pat.data);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001504 free(pat);
1505 }
Willy Tarreau72f073b2015-04-29 17:53:47 +02001506 expr->revision = rdtsc();
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001507}
1508
1509void pattern_init_expr(struct pattern_expr *expr)
1510{
1511 LIST_INIT(&expr->patterns);
Willy Tarreau72f073b2015-04-29 17:53:47 +02001512 expr->revision = 0;
Thierry FOURNIER31db4ae2014-01-30 00:27:15 +01001513 expr->pattern_tree = EB_ROOT;
1514 expr->pattern_tree_2 = EB_ROOT;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001515}
1516
1517void pattern_init_head(struct pattern_head *head)
1518{
1519 LIST_INIT(&head->head);
1520}
1521
1522/* The following functions are relative to the management of the reference
1523 * lists. These lists are used to store the original pattern and associated
1524 * value as string form.
1525 *
1526 * This is used with modifiable ACL and MAPS
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001527 *
1528 * The pattern reference are stored with two identifiers: the unique_id and
1529 * the reference.
1530 *
1531 * The reference identify a file. Each file with the same name point to the
1532 * same reference. We can register many times one file. If the file is modified,
1533 * all his dependencies are also modified. The reference can be used with map or
1534 * acl.
1535 *
1536 * The unique_id identify inline acl. The unique id is unique for each acl.
1537 * You cannot force the same id in the configuration file, because this repoort
1538 * an error.
1539 *
1540 * A particular case appears if the filename is a number. In this case, the
1541 * unique_id is set with the number represented by the filename and the
1542 * reference is also set. This method prevent double unique_id.
1543 *
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001544 */
1545
1546/* This function lookup for reference. If the reference is found, they return
1547 * pointer to the struct pat_ref, else return NULL.
1548 */
1549struct pat_ref *pat_ref_lookup(const char *reference)
1550{
1551 struct pat_ref *ref;
1552
1553 list_for_each_entry(ref, &pattern_reference, list)
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001554 if (ref->reference && strcmp(reference, ref->reference) == 0)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001555 return ref;
1556 return NULL;
1557}
1558
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001559/* This function lookup for unique id. If the reference is found, they return
1560 * pointer to the struct pat_ref, else return NULL.
1561 */
1562struct pat_ref *pat_ref_lookupid(int unique_id)
1563{
1564 struct pat_ref *ref;
1565
1566 list_for_each_entry(ref, &pattern_reference, list)
1567 if (ref->unique_id == unique_id)
1568 return ref;
1569 return NULL;
1570}
1571
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001572/* This function remove all pattern matching the pointer <refelt> from
1573 * the the reference and from each expr member of the reference. This
1574 * function returns 1 if the deletion is done and return 0 is the entry
1575 * is not found.
1576 */
1577int pat_ref_delete_by_id(struct pat_ref *ref, struct pat_ref_elt *refelt)
1578{
1579 struct pattern_expr *expr;
1580 struct pat_ref_elt *elt, *safe;
1581
1582 /* delete pattern from reference */
1583 list_for_each_entry_safe(elt, safe, &ref->head, list) {
1584 if (elt == refelt) {
peter caiaede6dd2015-10-07 00:07:43 -07001585 list_for_each_entry(expr, &ref->pat, list)
1586 pattern_delete(expr, elt);
1587
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001588 LIST_DEL(&elt->list);
1589 free(elt->sample);
1590 free(elt->pattern);
1591 free(elt);
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001592 return 1;
1593 }
1594 }
1595 return 0;
1596}
1597
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001598/* This function remove all pattern match <key> from the the reference
1599 * and from each expr member of the reference. This fucntion returns 1
1600 * if the deletion is done and return 0 is the entry is not found.
1601 */
1602int pat_ref_delete(struct pat_ref *ref, const char *key)
1603{
1604 struct pattern_expr *expr;
1605 struct pat_ref_elt *elt, *safe;
1606 int found = 0;
1607
1608 /* delete pattern from reference */
1609 list_for_each_entry_safe(elt, safe, &ref->head, list) {
1610 if (strcmp(key, elt->pattern) == 0) {
Dirkjan Bussink07fcaaa2014-04-28 22:57:16 +00001611 list_for_each_entry(expr, &ref->pat, list)
1612 pattern_delete(expr, elt);
1613
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001614 LIST_DEL(&elt->list);
1615 free(elt->sample);
1616 free(elt->pattern);
1617 free(elt);
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001618
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001619 found = 1;
1620 }
1621 }
1622
1623 if (!found)
1624 return 0;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001625 return 1;
1626}
1627
Baptiste Assmann953f74d2014-04-25 16:57:03 +02001628/*
1629 * find and return an element <elt> matching <key> in a reference <ref>
1630 * return NULL if not found
1631 */
1632struct pat_ref_elt *pat_ref_find_elt(struct pat_ref *ref, const char *key)
1633{
1634 struct pat_ref_elt *elt;
1635
1636 list_for_each_entry(elt, &ref->head, list) {
1637 if (strcmp(key, elt->pattern) == 0)
1638 return elt;
1639 }
1640
1641 return NULL;
1642}
1643
1644
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001645 /* This function modify the sample of the first pattern that match the <key>. */
1646static inline int pat_ref_set_elt(struct pat_ref *ref, struct pat_ref_elt *elt,
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001647 const char *value, char **err)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001648{
1649 struct pattern_expr *expr;
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001650 struct sample_data **data;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001651 char *sample;
Thierry FOURNIER12ba0c22015-08-14 00:02:11 +02001652 struct sample_data test;
Thierry FOURNIER149e0fe2014-01-29 19:35:06 +01001653
1654 /* Try all needed converters. */
1655 list_for_each_entry(expr, &ref->pat, list) {
1656 if (!expr->pat_head->parse_smp)
1657 continue;
1658
1659 if (!expr->pat_head->parse_smp(value, &test)) {
1660 memprintf(err, "unable to parse '%s'", value);
1661 return 0;
1662 }
1663 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001664
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001665 /* Modify pattern from reference. */
1666 sample = strdup(value);
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001667 if (!sample) {
1668 memprintf(err, "out of memory error");
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001669 return 0;
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001670 }
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001671 free(elt->sample);
1672 elt->sample = sample;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001673
Thierry FOURNIER149e0fe2014-01-29 19:35:06 +01001674 /* Load sample in each reference. All the conversion are tested
1675 * below, normally these calls dosn't fail.
1676 */
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001677 list_for_each_entry(expr, &ref->pat, list) {
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001678 if (!expr->pat_head->parse_smp)
1679 continue;
1680
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001681 data = pattern_find_smp(expr, elt);
1682 if (data && *data && !expr->pat_head->parse_smp(sample, *data))
1683 *data = NULL;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001684 }
1685
Thierry FOURNIER149e0fe2014-01-29 19:35:06 +01001686 return 1;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001687}
1688
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001689/* This function modify the sample of the first pattern that match the <key>. */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001690int 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 +01001691{
1692 struct pat_ref_elt *elt;
1693
1694 /* Look for pattern in the reference. */
1695 list_for_each_entry(elt, &ref->head, list) {
1696 if (elt == refelt) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001697 if (!pat_ref_set_elt(ref, elt, value, err))
1698 return 0;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001699 return 1;
1700 }
1701 }
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001702
1703 memprintf(err, "key or pattern not found");
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001704 return 0;
1705}
1706
1707/* This function modify the sample of the first pattern that match the <key>. */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001708int pat_ref_set(struct pat_ref *ref, const char *key, const char *value, char **err)
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001709{
1710 struct pat_ref_elt *elt;
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001711 int found = 0;
1712 char *_merr;
1713 char **merr;
1714
1715 if (err) {
1716 merr = &_merr;
1717 *merr = NULL;
1718 }
1719 else
1720 merr = NULL;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001721
1722 /* Look for pattern in the reference. */
1723 list_for_each_entry(elt, &ref->head, list) {
1724 if (strcmp(key, elt->pattern) == 0) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001725 if (!pat_ref_set_elt(ref, elt, value, merr)) {
1726 if (!found)
1727 *err = *merr;
1728 else {
1729 memprintf(err, "%s, %s", *err, *merr);
1730 free(*merr);
1731 *merr = NULL;
1732 }
1733 }
1734 found = 1;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001735 }
1736 }
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001737
1738 if (!found) {
1739 memprintf(err, "entry not found");
1740 return 0;
1741 }
1742 return 1;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001743}
1744
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001745/* This function create new reference. <ref> is the reference name.
1746 * <flags> are PAT_REF_*. /!\ The reference is not checked, and must
1747 * be unique. The user must check the reference with "pat_ref_lookup()"
1748 * before calling this function. If the fucntion fail, it return NULL,
1749 * else return new struct pat_ref.
1750 */
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001751struct pat_ref *pat_ref_new(const char *reference, const char *display, unsigned int flags)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001752{
1753 struct pat_ref *ref;
1754
1755 ref = malloc(sizeof(*ref));
1756 if (!ref)
1757 return NULL;
1758
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001759 if (display) {
1760 ref->display = strdup(display);
1761 if (!ref->display) {
1762 free(ref);
1763 return NULL;
1764 }
1765 }
1766 else
1767 ref->display = NULL;
1768
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001769 ref->reference = strdup(reference);
1770 if (!ref->reference) {
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001771 free(ref->display);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001772 free(ref);
1773 return NULL;
1774 }
1775
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001776 ref->flags = flags;
1777 ref->unique_id = -1;
1778
1779 LIST_INIT(&ref->head);
1780 LIST_INIT(&ref->pat);
1781
1782 LIST_ADDQ(&pattern_reference, &ref->list);
1783
1784 return ref;
1785}
1786
1787/* This function create new reference. <unique_id> is the unique id. If
1788 * the value of <unique_id> is -1, the unique id is calculated later.
1789 * <flags> are PAT_REF_*. /!\ The reference is not checked, and must
1790 * be unique. The user must check the reference with "pat_ref_lookup()"
1791 * or pat_ref_lookupid before calling this function. If the function
1792 * fail, it return NULL, else return new struct pat_ref.
1793 */
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001794struct pat_ref *pat_ref_newid(int unique_id, const char *display, unsigned int flags)
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001795{
1796 struct pat_ref *ref;
1797
1798 ref = malloc(sizeof(*ref));
1799 if (!ref)
1800 return NULL;
1801
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001802 if (display) {
1803 ref->display = strdup(display);
1804 if (!ref->display) {
1805 free(ref);
1806 return NULL;
1807 }
1808 }
1809 else
1810 ref->display = NULL;
1811
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001812 ref->reference = NULL;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001813 ref->flags = flags;
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001814 ref->unique_id = unique_id;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001815 LIST_INIT(&ref->head);
1816 LIST_INIT(&ref->pat);
1817
1818 LIST_ADDQ(&pattern_reference, &ref->list);
1819
1820 return ref;
1821}
1822
1823/* This function adds entry to <ref>. It can failed with memory error.
1824 * If the function fails, it returns 0.
1825 */
1826int pat_ref_append(struct pat_ref *ref, char *pattern, char *sample, int line)
1827{
1828 struct pat_ref_elt *elt;
1829
1830 elt = malloc(sizeof(*elt));
1831 if (!elt)
1832 return 0;
1833
1834 elt->line = line;
1835
1836 elt->pattern = strdup(pattern);
1837 if (!elt->pattern) {
1838 free(elt);
1839 return 0;
1840 }
1841
1842 if (sample) {
1843 elt->sample = strdup(sample);
1844 if (!elt->sample) {
1845 free(elt->pattern);
1846 free(elt);
1847 return 0;
1848 }
1849 }
1850 else
1851 elt->sample = NULL;
1852
1853 LIST_ADDQ(&ref->head, &elt->list);
1854
1855 return 1;
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001856}
1857
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001858/* This function create sample found in <elt>, parse the pattern also
1859 * found in <elt> and insert it in <expr>. The function copy <patflags>
1860 * in <expr>. If the function fails, it returns0 and <err> is filled.
1861 * In succes case, the function returns 1.
1862 */
1863static inline
1864int pat_ref_push(struct pat_ref_elt *elt, struct pattern_expr *expr,
1865 int patflags, char **err)
1866{
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001867 struct sample_data *data;
Thierry FOURNIERd25c8422014-01-28 15:34:35 +01001868 struct pattern pattern;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001869
1870 /* Create sample */
1871 if (elt->sample && expr->pat_head->parse_smp) {
1872 /* New sample. */
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001873 data = malloc(sizeof(*data));
1874 if (!data)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001875 return 0;
1876
1877 /* Parse value. */
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001878 if (!expr->pat_head->parse_smp(elt->sample, data)) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001879 memprintf(err, "unable to parse '%s'", elt->sample);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001880 free(data);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001881 return 0;
1882 }
1883
1884 }
1885 else
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001886 data = NULL;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001887
Thierry FOURNIERd25c8422014-01-28 15:34:35 +01001888 /* initialise pattern */
1889 memset(&pattern, 0, sizeof(pattern));
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001890 pattern.data = data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01001891 pattern.ref = elt;
Thierry FOURNIERd25c8422014-01-28 15:34:35 +01001892
1893 /* parse pattern */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02001894 if (!expr->pat_head->parse(elt->pattern, &pattern, expr->mflags, err)) {
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001895 free(data);
Thierry FOURNIERd25c8422014-01-28 15:34:35 +01001896 return 0;
1897 }
1898
1899 /* index pattern */
1900 if (!expr->pat_head->index(expr, &pattern, err)) {
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001901 free(data);
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001902 return 0;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001903 }
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001904
1905 return 1;
1906}
1907
Thierry FOURNIER31db4ae2014-01-30 00:27:15 +01001908/* This function adds entry to <ref>. It can failed with memory error. The new
1909 * entry is added at all the pattern_expr registered in this reference. The
1910 * function stop on the first error encountered. It returns 0 and err is
1911 * filled. If an error is encountered, the complete add operation is cancelled.
1912 * If the insertion is a success the function returns 1.
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001913 */
1914int pat_ref_add(struct pat_ref *ref,
1915 const char *pattern, const char *sample,
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02001916 char **err)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001917{
1918 struct pat_ref_elt *elt;
1919 struct pattern_expr *expr;
1920
1921 elt = malloc(sizeof(*elt));
1922 if (!elt) {
1923 memprintf(err, "out of memory error");
1924 return 0;
1925 }
1926
1927 elt->line = -1;
1928
1929 elt->pattern = strdup(pattern);
1930 if (!elt->pattern) {
1931 free(elt);
1932 memprintf(err, "out of memory error");
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 memprintf(err, "out of memory error");
1942 return 0;
1943 }
1944 }
1945 else
1946 elt->sample = NULL;
1947
1948 LIST_ADDQ(&ref->head, &elt->list);
1949
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001950 list_for_each_entry(expr, &ref->pat, list) {
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02001951 if (!pat_ref_push(elt, expr, 0, err)) {
Thierry FOURNIER31db4ae2014-01-30 00:27:15 +01001952 /* If the insertion fails, try to delete all the added entries. */
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001953 pat_ref_delete_by_id(ref, elt);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001954 return 0;
1955 }
1956 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001957 return 1;
1958}
1959
Thierry FOURNIER46006bd2014-03-21 21:45:15 +01001960/* This function prune <ref>, replace all reference by the references
1961 * of <replace>, and reindex all the news values.
1962 *
1963 * The pattern are loaded in best effort and the errors are ignored,
1964 * but writed in the logs.
1965 */
1966void pat_ref_reload(struct pat_ref *ref, struct pat_ref *replace)
1967{
1968 struct pattern_expr *expr;
1969 struct pat_ref_elt *elt;
1970 char *err = NULL;
1971
1972 pat_ref_prune(ref);
1973
1974 LIST_ADD(&replace->head, &ref->head);
1975 LIST_DEL(&replace->head);
1976
1977 list_for_each_entry(elt, &ref->head, list) {
1978 list_for_each_entry(expr, &ref->pat, list) {
1979 if (!pat_ref_push(elt, expr, 0, &err)) {
1980 send_log(NULL, LOG_NOTICE, "%s", err);
1981 free(err);
1982 err = NULL;
1983 }
1984 }
1985 }
1986}
1987
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001988/* This function prune all entries of <ref>. This function
1989 * prune the associated pattern_expr.
1990 */
1991void pat_ref_prune(struct pat_ref *ref)
1992{
1993 struct pat_ref_elt *elt, *safe;
1994 struct pattern_expr *expr;
1995
1996 list_for_each_entry_safe(elt, safe, &ref->head, list) {
1997 LIST_DEL(&elt->list);
1998 free(elt->pattern);
1999 free(elt->sample);
2000 free(elt);
2001 }
2002
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002003 list_for_each_entry(expr, &ref->pat, list)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002004 expr->pat_head->prune(expr);
2005}
2006
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002007/* This function lookup for existing reference <ref> in pattern_head <head>. */
2008struct pattern_expr *pattern_lookup_expr(struct pattern_head *head, struct pat_ref *ref)
2009{
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002010 struct pattern_expr_list *expr;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002011
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002012 list_for_each_entry(expr, &head->head, list)
2013 if (expr->expr->ref == ref)
2014 return expr->expr;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002015 return NULL;
2016}
2017
2018/* This function create new pattern_expr associated to the reference <ref>.
2019 * <ref> can be NULL. If an error is occured, the function returns NULL and
2020 * <err> is filled. Otherwise, the function returns new pattern_expr linked
2021 * with <head> and <ref>.
Thierry FOURNIER315ec422014-11-24 11:14:42 +01002022 *
2023 * The returned value can be a alredy filled pattern list, in this case the
2024 * flag <reuse> is set.
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002025 */
Thierry FOURNIER315ec422014-11-24 11:14:42 +01002026struct pattern_expr *pattern_new_expr(struct pattern_head *head, struct pat_ref *ref,
2027 char **err, int *reuse)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002028{
2029 struct pattern_expr *expr;
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002030 struct pattern_expr_list *list;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002031
Thierry FOURNIER315ec422014-11-24 11:14:42 +01002032 if (reuse)
2033 *reuse = 0;
2034
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002035 /* Memory and initialization of the chain element. */
2036 list = malloc(sizeof(*list));
2037 if (!list) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002038 memprintf(err, "out of memory");
2039 return NULL;
2040 }
2041
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002042 /* Look for existing similar expr. No that only the index, parse and
2043 * parse_smp function must be identical for having similar pattern.
2044 * The other function depends of theses first.
2045 */
2046 if (ref) {
2047 list_for_each_entry(expr, &ref->pat, list)
2048 if (expr->pat_head->index == head->index &&
2049 expr->pat_head->parse == head->parse &&
2050 expr->pat_head->parse_smp == head->parse_smp)
2051 break;
2052 if (&expr->list == &ref->pat)
2053 expr = NULL;
2054 }
2055 else
2056 expr = NULL;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002057
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002058 /* If no similar expr was found, we create new expr. */
2059 if (!expr) {
2060 /* Get a lot of memory for the expr struct. */
2061 expr = malloc(sizeof(*expr));
2062 if (!expr) {
2063 memprintf(err, "out of memory");
2064 return NULL;
2065 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002066
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002067 /* Initialize this new expr. */
2068 pattern_init_expr(expr);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002069
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002070 /* This new pattern expression reference one of his heads. */
2071 expr->pat_head = head;
2072
2073 /* Link with ref, or to self to facilitate LIST_DEL() */
2074 if (ref)
2075 LIST_ADDQ(&ref->pat, &expr->list);
2076 else
2077 LIST_INIT(&expr->list);
2078
2079 expr->ref = ref;
2080
2081 /* We must free this pattern if it is no more used. */
2082 list->do_free = 1;
2083 }
2084 else {
2085 /* If the pattern used already exists, it is already linked
2086 * with ref and we must not free it.
2087 */
2088 list->do_free = 0;
Thierry FOURNIER315ec422014-11-24 11:14:42 +01002089 if (reuse)
2090 *reuse = 1;
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002091 }
2092
2093 /* The new list element reference the pattern_expr. */
2094 list->expr = expr;
2095
2096 /* Link the list element with the pattern_head. */
2097 LIST_ADDQ(&head->head, &list->list);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002098 return expr;
2099}
2100
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002101/* Reads patterns from a file. If <err_msg> is non-NULL, an error message will
2102 * be returned there on errors and the caller will have to free it.
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002103 *
2104 * The file contains one key + value per line. Lines which start with '#' are
2105 * ignored, just like empty lines. Leading tabs/spaces are stripped. The key is
2106 * then the first "word" (series of non-space/tabs characters), and the value is
2107 * what follows this series of space/tab till the end of the line excluding
2108 * trailing spaces/tabs.
2109 *
2110 * Example :
2111 *
2112 * # this is a comment and is ignored
2113 * 62.212.114.60 1wt.eu \n
2114 * <-><-----------><---><----><---->
2115 * | | | | `--- trailing spaces ignored
2116 * | | | `-------- value
2117 * | | `--------------- middle spaces ignored
2118 * | `------------------------ key
2119 * `-------------------------------- leading spaces ignored
2120 *
2121 * Return non-zero in case of succes, otherwise 0.
2122 */
2123int pat_ref_read_from_file_smp(struct pat_ref *ref, const char *filename, char **err)
2124{
2125 FILE *file;
2126 char *c;
2127 int ret = 0;
2128 int line = 0;
2129 char *key_beg;
2130 char *key_end;
2131 char *value_beg;
2132 char *value_end;
2133
2134 file = fopen(filename, "r");
2135 if (!file) {
2136 memprintf(err, "failed to open pattern file <%s>", filename);
2137 return 0;
2138 }
2139
2140 /* now parse all patterns. The file may contain only one pattern
2141 * followed by one value per line. The start spaces, separator spaces
2142 * and and spaces are stripped. Each can contain comment started by '#'
2143 */
2144 while (fgets(trash.str, trash.size, file) != NULL) {
2145 line++;
2146 c = trash.str;
2147
2148 /* ignore lines beginning with a dash */
2149 if (*c == '#')
2150 continue;
2151
2152 /* strip leading spaces and tabs */
2153 while (*c == ' ' || *c == '\t')
2154 c++;
2155
2156 /* empty lines are ignored too */
2157 if (*c == '\0' || *c == '\r' || *c == '\n')
2158 continue;
2159
2160 /* look for the end of the key */
2161 key_beg = c;
2162 while (*c && *c != ' ' && *c != '\t' && *c != '\n' && *c != '\r')
2163 c++;
2164
2165 key_end = c;
2166
2167 /* strip middle spaces and tabs */
2168 while (*c == ' ' || *c == '\t')
2169 c++;
2170
2171 /* look for the end of the value, it is the end of the line */
2172 value_beg = c;
2173 while (*c && *c != '\n' && *c != '\r')
2174 c++;
2175 value_end = c;
2176
2177 /* trim possibly trailing spaces and tabs */
2178 while (value_end > value_beg && (value_end[-1] == ' ' || value_end[-1] == '\t'))
2179 value_end--;
2180
2181 /* set final \0 and check entries */
2182 *key_end = '\0';
2183 *value_end = '\0';
2184
2185 /* insert values */
2186 if (!pat_ref_append(ref, key_beg, value_beg, line)) {
2187 memprintf(err, "out of memory");
2188 goto out_close;
2189 }
2190 }
2191
2192 /* succes */
2193 ret = 1;
2194
2195 out_close:
2196 fclose(file);
2197 return ret;
2198}
2199
2200/* Reads patterns from a file. If <err_msg> is non-NULL, an error message will
2201 * be returned there on errors and the caller will have to free it.
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002202 */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002203int pat_ref_read_from_file(struct pat_ref *ref, const char *filename, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002204{
2205 FILE *file;
2206 char *c;
2207 char *arg;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002208 int ret = 0;
2209 int line = 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002210
2211 file = fopen(filename, "r");
2212 if (!file) {
2213 memprintf(err, "failed to open pattern file <%s>", filename);
2214 return 0;
2215 }
2216
2217 /* now parse all patterns. The file may contain only one pattern per
2218 * line. If the line contains spaces, they will be part of the pattern.
2219 * The pattern stops at the first CR, LF or EOF encountered.
2220 */
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002221 while (fgets(trash.str, trash.size, file) != NULL) {
2222 line++;
2223 c = trash.str;
2224
2225 /* ignore lines beginning with a dash */
2226 if (*c == '#')
2227 continue;
2228
2229 /* strip leading spaces and tabs */
2230 while (*c == ' ' || *c == '\t')
2231 c++;
2232
2233
2234 arg = c;
2235 while (*c && *c != '\n' && *c != '\r')
2236 c++;
2237 *c = 0;
2238
2239 /* empty lines are ignored too */
2240 if (c == arg)
2241 continue;
2242
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002243 if (!pat_ref_append(ref, arg, NULL, line)) {
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002244 memprintf(err, "out of memory when loading patterns from file <%s>", filename);
2245 goto out_close;
2246 }
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002247 }
2248
2249 ret = 1; /* success */
2250
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002251 out_close:
2252 fclose(file);
2253 return ret;
2254}
2255
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002256int pattern_read_from_file(struct pattern_head *head, unsigned int refflags,
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002257 const char *filename, int patflags, int load_smp,
Thierry FOURNIER94580c92014-02-11 14:36:45 +01002258 char **err, const char *file, int line)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002259{
2260 struct pat_ref *ref;
2261 struct pattern_expr *expr;
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002262 struct pat_ref_elt *elt;
Willy Tarreau4deaf392014-11-26 13:17:03 +01002263 int reuse = 0;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002264
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002265 /* Lookup for the existing reference. */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002266 ref = pat_ref_lookup(filename);
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002267
2268 /* If the reference doesn't exists, create it and load associated file. */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002269 if (!ref) {
Thierry FOURNIER94580c92014-02-11 14:36:45 +01002270 chunk_printf(&trash,
2271 "pattern loaded from file '%s' used by %s at file '%s' line %d",
2272 filename, refflags & PAT_REF_MAP ? "map" : "acl", file, line);
2273
2274 ref = pat_ref_new(filename, trash.str, refflags);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002275 if (!ref) {
2276 memprintf(err, "out of memory");
2277 return 0;
2278 }
2279
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002280 if (load_smp) {
Thierry FOURNIERc0bd9102014-01-29 12:32:58 +01002281 ref->flags |= PAT_REF_SMP;
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002282 if (!pat_ref_read_from_file_smp(ref, filename, err))
2283 return 0;
2284 }
2285 else {
2286 if (!pat_ref_read_from_file(ref, filename, err))
2287 return 0;
2288 }
2289 }
2290 else {
Thierry FOURNIERc0bd9102014-01-29 12:32:58 +01002291 /* The reference already exists, check the map compatibility. */
2292
2293 /* If the load require samples and the flag PAT_REF_SMP is not set,
2294 * the reference doesn't contain sample, and cannot be used.
2295 */
2296 if (load_smp) {
2297 if (!(ref->flags & PAT_REF_SMP)) {
2298 memprintf(err, "The file \"%s\" is already used as one column file "
2299 "and cannot be used by as two column file.",
2300 filename);
2301 return 0;
2302 }
2303 }
2304 else {
2305 /* The load doesn't require samples. If the flag PAT_REF_SMP is
2306 * set, the reference contains a sample, and cannot be used.
2307 */
2308 if (ref->flags & PAT_REF_SMP) {
2309 memprintf(err, "The file \"%s\" is already used as two column file "
2310 "and cannot be used by as one column file.",
2311 filename);
2312 return 0;
2313 }
2314 }
2315
Thierry FOURNIER94580c92014-02-11 14:36:45 +01002316 /* Extends display */
2317 chunk_printf(&trash, "%s", ref->display);
2318 chunk_appendf(&trash, ", by %s at file '%s' line %d",
2319 refflags & PAT_REF_MAP ? "map" : "acl", file, line);
2320 free(ref->display);
2321 ref->display = strdup(trash.str);
2322 if (!ref->display) {
2323 memprintf(err, "out of memory");
2324 return 0;
2325 }
2326
Thierry FOURNIERc0bd9102014-01-29 12:32:58 +01002327 /* Merge flags. */
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002328 ref->flags |= refflags;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002329 }
2330
2331 /* Now, we can loading patterns from the reference. */
2332
2333 /* Lookup for existing reference in the head. If the reference
2334 * doesn't exists, create it.
2335 */
2336 expr = pattern_lookup_expr(head, ref);
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02002337 if (!expr || (expr->mflags != patflags)) {
Thierry FOURNIER315ec422014-11-24 11:14:42 +01002338 expr = pattern_new_expr(head, ref, err, &reuse);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002339 if (!expr)
2340 return 0;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02002341 expr->mflags = patflags;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002342 }
2343
Thierry FOURNIER315ec422014-11-24 11:14:42 +01002344 /* The returned expression may be not empty, because the function
2345 * "pattern_new_expr" lookup for similar pattern list and can
2346 * reuse a already filled pattern list. In this case, we can not
2347 * reload the patterns.
2348 */
2349 if (reuse)
2350 return 1;
2351
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002352 /* Load reference content in the pattern expression. */
2353 list_for_each_entry(elt, &ref->head, list) {
2354 if (!pat_ref_push(elt, expr, patflags, err)) {
2355 if (elt->line > 0)
2356 memprintf(err, "%s at line %d of file '%s'",
2357 *err, elt->line, filename);
2358 return 0;
2359 }
2360 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002361
2362 return 1;
2363}
2364
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01002365/* This function executes a pattern match on a sample. It applies pattern <expr>
2366 * to sample <smp>. The function returns NULL if the sample dont match. It returns
2367 * non-null if the sample match. If <fill> is true and the sample match, the
2368 * function returns the matched pattern. In many cases, this pattern can be a
2369 * static buffer.
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002370 */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002371struct pattern *pattern_exec_match(struct pattern_head *head, struct sample *smp, int fill)
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002372{
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002373 struct pattern_expr_list *list;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002374 struct pattern *pat;
2375
2376 if (!head->match) {
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01002377 if (fill) {
Thierry FOURNIER503bb092015-08-19 08:35:43 +02002378 static_pattern.data = NULL;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01002379 static_pattern.ref = NULL;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02002380 static_pattern.sflags = 0;
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002381 static_pattern.type = SMP_T_SINT;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01002382 static_pattern.val.i = 1;
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01002383 }
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01002384 return &static_pattern;
2385 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002386
Thierry FOURNIER5d344082014-01-27 14:19:53 +01002387 /* convert input to string */
2388 if (!sample_convert(smp, head->expect_type))
2389 return NULL;
2390
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002391 list_for_each_entry(list, &head->head, list) {
2392 pat = head->match(smp, list->expr, fill);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002393 if (pat)
2394 return pat;
2395 }
2396 return NULL;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002397}
2398
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01002399/* This function prune the pattern expression. */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002400void pattern_prune(struct pattern_head *head)
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01002401{
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002402 struct pattern_expr_list *list, *safe;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002403
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002404 list_for_each_entry_safe(list, safe, &head->head, list) {
2405 LIST_DEL(&list->list);
2406 if (list->do_free) {
2407 LIST_DEL(&list->expr->list);
2408 head->prune(list->expr);
2409 free(list->expr);
2410 }
2411 free(list);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002412 }
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01002413}
2414
Thierry FOURNIER55d0b102014-01-15 11:25:26 +01002415/* This function lookup for a pattern matching the <key> and return a
2416 * pointer to a pointer of the sample stoarge. If the <key> dont match,
2417 * the function returns NULL. If the key cannot be parsed, the function
2418 * fill <err>.
2419 */
Thierry FOURNIER12ba0c22015-08-14 00:02:11 +02002420struct sample_data **pattern_find_smp(struct pattern_expr *expr, struct pat_ref_elt *ref)
Thierry FOURNIER55d0b102014-01-15 11:25:26 +01002421{
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01002422 struct ebmb_node *node;
2423 struct pattern_tree *elt;
2424 struct pattern_list *pat;
Thierry FOURNIER55d0b102014-01-15 11:25:26 +01002425
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01002426 for (node = ebmb_first(&expr->pattern_tree);
2427 node;
2428 node = ebmb_next(node)) {
2429 elt = container_of(node, struct pattern_tree, node);
2430 if (elt->ref == ref)
Thierry FOURNIER503bb092015-08-19 08:35:43 +02002431 return &elt->data;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01002432 }
2433
2434 for (node = ebmb_first(&expr->pattern_tree_2);
2435 node;
2436 node = ebmb_next(node)) {
2437 elt = container_of(node, struct pattern_tree, node);
2438 if (elt->ref == ref)
Thierry FOURNIER503bb092015-08-19 08:35:43 +02002439 return &elt->data;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01002440 }
2441
2442 list_for_each_entry(pat, &expr->patterns, list)
2443 if (pat->pat.ref == ref)
Thierry FOURNIER503bb092015-08-19 08:35:43 +02002444 return &pat->pat.data;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01002445
2446 return NULL;
Thierry FOURNIER55d0b102014-01-15 11:25:26 +01002447}
2448
Thierry FOURNIERb1136502014-01-15 11:38:49 +01002449/* This function search all the pattern matching the <key> and delete it.
2450 * If the parsing of the input key fails, the function returns 0 and the
2451 * <err> is filled, else return 1;
2452 */
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01002453int pattern_delete(struct pattern_expr *expr, struct pat_ref_elt *ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01002454{
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01002455 expr->pat_head->delete(expr, ref);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01002456 return 1;
2457}
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002458
2459/* This function finalize the configuration parsing. Its set all the
2460 * automatic ids
2461 */
2462void pattern_finalize_config(void)
2463{
2464 int i = 0;
2465 struct pat_ref *ref, *ref2, *ref3;
2466 struct list pr = LIST_HEAD_INIT(pr);
2467
Willy Tarreauf3045d22015-04-29 16:24:50 +02002468 pat_lru_seed = random();
2469 if (global.tune.pattern_cache)
2470 pat_lru_tree = lru64_new(global.tune.pattern_cache);
2471
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002472 list_for_each_entry(ref, &pattern_reference, list) {
2473 if (ref->unique_id == -1) {
2474 /* Look for the first free id. */
2475 while (1) {
2476 list_for_each_entry(ref2, &pattern_reference, list) {
2477 if (ref2->unique_id == i) {
2478 i++;
2479 break;
2480 }
2481 }
Willy Tarreau3b786962014-04-26 12:37:25 +02002482 if (&ref2->list == &pattern_reference)
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002483 break;
2484 }
2485
2486 /* Uses the unique id and increment it for the next entry. */
2487 ref->unique_id = i;
2488 i++;
2489 }
2490 }
2491
2492 /* This sort the reference list by id. */
2493 list_for_each_entry_safe(ref, ref2, &pattern_reference, list) {
2494 LIST_DEL(&ref->list);
2495 list_for_each_entry(ref3, &pr, list) {
2496 if (ref->unique_id < ref3->unique_id) {
2497 LIST_ADDQ(&ref3->list, &ref->list);
2498 break;
2499 }
2500 }
2501 if (&ref3->list == &pr)
2502 LIST_ADDQ(&pr, &ref->list);
2503 }
2504
2505 /* swap root */
2506 LIST_ADD(&pr, &pattern_reference);
2507 LIST_DEL(&pr);
2508}