blob: aa6a3d43bcc31aa142b89f3ac14240df63dd8ce4 [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
22#include <proto/pattern.h>
Thierry FOURNIERe3ded592013-12-06 15:36:54 +010023#include <proto/sample.h>
Thierry FOURNIERed66c292013-11-28 11:05:19 +010024
25#include <ebsttree.h>
26
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010027char *pat_match_names[PAT_MATCH_NUM] = {
28 [PAT_MATCH_FOUND] = "found",
29 [PAT_MATCH_BOOL] = "bool",
30 [PAT_MATCH_INT] = "int",
31 [PAT_MATCH_IP] = "ip",
32 [PAT_MATCH_BIN] = "bin",
33 [PAT_MATCH_LEN] = "len",
34 [PAT_MATCH_STR] = "str",
35 [PAT_MATCH_BEG] = "beg",
36 [PAT_MATCH_SUB] = "sub",
37 [PAT_MATCH_DIR] = "dir",
38 [PAT_MATCH_DOM] = "dom",
39 [PAT_MATCH_END] = "end",
40 [PAT_MATCH_REG] = "reg",
Thierry FOURNIERed66c292013-11-28 11:05:19 +010041};
42
Thierry FOURNIERedc15c32013-12-13 15:36:59 +010043int (*pat_parse_fcts[PAT_MATCH_NUM])(const char *, struct pattern *, char **) = {
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010044 [PAT_MATCH_FOUND] = pat_parse_nothing,
45 [PAT_MATCH_BOOL] = pat_parse_nothing,
46 [PAT_MATCH_INT] = pat_parse_int,
47 [PAT_MATCH_IP] = pat_parse_ip,
48 [PAT_MATCH_BIN] = pat_parse_bin,
Thierry FOURNIERcc0e0b32013-12-06 16:56:40 +010049 [PAT_MATCH_LEN] = pat_parse_len,
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010050 [PAT_MATCH_STR] = pat_parse_str,
51 [PAT_MATCH_BEG] = pat_parse_str,
52 [PAT_MATCH_SUB] = pat_parse_str,
53 [PAT_MATCH_DIR] = pat_parse_str,
54 [PAT_MATCH_DOM] = pat_parse_str,
55 [PAT_MATCH_END] = pat_parse_str,
56 [PAT_MATCH_REG] = pat_parse_reg,
Thierry FOURNIERed66c292013-11-28 11:05:19 +010057};
58
Thierry FOURNIERb9b08462013-12-13 15:12:32 +010059int (*pat_index_fcts[PAT_MATCH_NUM])(struct pattern_expr *, struct pattern *, char **) = {
60 [PAT_MATCH_FOUND] = pat_idx_list_val,
61 [PAT_MATCH_BOOL] = pat_idx_list_val,
62 [PAT_MATCH_INT] = pat_idx_list_val,
63 [PAT_MATCH_IP] = pat_idx_tree_ip,
64 [PAT_MATCH_BIN] = pat_idx_list_ptr,
65 [PAT_MATCH_LEN] = pat_idx_list_val,
66 [PAT_MATCH_STR] = pat_idx_tree_str,
67 [PAT_MATCH_BEG] = pat_idx_list_str,
68 [PAT_MATCH_SUB] = pat_idx_list_str,
69 [PAT_MATCH_DIR] = pat_idx_list_str,
70 [PAT_MATCH_DOM] = pat_idx_list_str,
71 [PAT_MATCH_END] = pat_idx_list_str,
72 [PAT_MATCH_REG] = pat_idx_list_reg,
73};
74
Willy Tarreau0cba6072013-11-28 22:21:02 +010075enum pat_match_res (*pat_match_fcts[PAT_MATCH_NUM])(struct sample *, struct pattern *) = {
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010076 [PAT_MATCH_FOUND] = NULL,
77 [PAT_MATCH_BOOL] = pat_match_nothing,
78 [PAT_MATCH_INT] = pat_match_int,
79 [PAT_MATCH_IP] = pat_match_ip,
80 [PAT_MATCH_BIN] = pat_match_bin,
81 [PAT_MATCH_LEN] = pat_match_len,
82 [PAT_MATCH_STR] = pat_match_str,
83 [PAT_MATCH_BEG] = pat_match_beg,
84 [PAT_MATCH_SUB] = pat_match_sub,
85 [PAT_MATCH_DIR] = pat_match_dir,
86 [PAT_MATCH_DOM] = pat_match_dom,
87 [PAT_MATCH_END] = pat_match_end,
88 [PAT_MATCH_REG] = pat_match_reg,
Thierry FOURNIERed66c292013-11-28 11:05:19 +010089};
90
Thierry FOURNIERe3ded592013-12-06 15:36:54 +010091/* Just used for checking configuration compatibility */
92int pat_match_types[PAT_MATCH_NUM] = {
93 [PAT_MATCH_FOUND] = SMP_T_UINT,
94 [PAT_MATCH_BOOL] = SMP_T_UINT,
95 [PAT_MATCH_INT] = SMP_T_UINT,
96 [PAT_MATCH_IP] = SMP_T_ADDR,
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010097 [PAT_MATCH_BIN] = SMP_T_BIN,
98 [PAT_MATCH_LEN] = SMP_T_STR,
99 [PAT_MATCH_STR] = SMP_T_STR,
100 [PAT_MATCH_BEG] = SMP_T_STR,
101 [PAT_MATCH_SUB] = SMP_T_STR,
102 [PAT_MATCH_DIR] = SMP_T_STR,
103 [PAT_MATCH_DOM] = SMP_T_STR,
104 [PAT_MATCH_END] = SMP_T_STR,
105 [PAT_MATCH_REG] = SMP_T_STR,
Thierry FOURNIERe3ded592013-12-06 15:36:54 +0100106};
107
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +0100108/* this struct is used to return information */
109static struct pattern static_pattern;
110
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100111/*
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100112 *
113 * The following functions are not exported and are used by internals process
114 * of pattern matching
115 *
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100116 */
117
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100118/* Lookup an IPv4 address in the expression's pattern tree using the longest
119 * match method. The node is returned if it exists, otherwise NULL.
120 */
121static void *pat_lookup_ip(struct sample *smp, struct pattern_expr *expr)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100122{
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100123 struct in_addr *s;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100124
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100125 if (smp->type != SMP_T_IPV4)
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100126 return PAT_NOMATCH;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100127
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100128 s = &smp->data.ipv4;
129 return ebmb_lookup_longest(&expr->pattern_tree, &s->s_addr);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100130}
131
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100132/* Free data allocated by pat_parse_reg */
133static void pat_free_reg(void *ptr)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100134{
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100135 regex_free(ptr);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100136}
137
138/* Lookup a string in the expression's pattern tree. The node is returned if it
139 * exists, otherwise NULL.
140 */
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100141static void *pat_lookup_str(struct sample *smp, struct pattern_expr *expr)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100142{
143 /* data are stored in a tree */
144 struct ebmb_node *node;
145 char prev;
146
147 /* we may have to force a trailing zero on the test pattern */
148 prev = smp->data.str.str[smp->data.str.len];
149 if (prev)
150 smp->data.str.str[smp->data.str.len] = '\0';
151 node = ebst_lookup(&expr->pattern_tree, smp->data.str.str);
152 if (prev)
153 smp->data.str.str[smp->data.str.len] = prev;
154 return node;
155}
156
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100157/* Background: Fast way to find a zero byte in a word
158 * http://graphics.stanford.edu/~seander/bithacks.html#ZeroInWord
159 * hasZeroByte = (v - 0x01010101UL) & ~v & 0x80808080UL;
160 *
161 * To look for 4 different byte values, xor the word with those bytes and
162 * then check for zero bytes:
163 *
164 * v = (((unsigned char)c * 0x1010101U) ^ delimiter)
165 * where <delimiter> is the 4 byte values to look for (as an uint)
166 * and <c> is the character that is being tested
167 */
168static inline unsigned int is_delimiter(unsigned char c, unsigned int mask)
169{
170 mask ^= (c * 0x01010101); /* propagate the char to all 4 bytes */
171 return (mask - 0x01010101) & ~mask & 0x80808080U;
172}
173
174static inline unsigned int make_4delim(unsigned char d1, unsigned char d2, unsigned char d3, unsigned char d4)
175{
176 return d1 << 24 | d2 << 16 | d3 << 8 | d4;
177}
178
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100179
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100180/*
181 *
182 * These functions are exported and may be used by any other component.
183 *
184 * The following functions are used for parsing pattern matching
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100185 * input value. The <text> contain the string to be parsed. <pattern>
186 * must be a preallocated pattern. The pat_parse_* functions fill this
187 * structure with the parsed value. <usage> can be PAT_U_COMPILE or
188 * PAT_U_LOOKUP. If the value PAT_U_COMPILE is used memory is allocated
189 * for filling the pattern. If the value PAT_U_LOOKUP is set, the parser
190 * use "trash" or return pointers to the input strings. In both cases,
191 * the caller must use the value PAT_U_LOOKUP with caution. <err> is
192 * filled with an error message built with memprintf() function.
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100193 *
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100194 * In succes case, the pat_parse_* function return 1. If the function
195 * fail, it returns 0 and <err> is filled.
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100196 *
197 */
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100198
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100199/* ignore the current line */
Thierry FOURNIERedc15c32013-12-13 15:36:59 +0100200int pat_parse_nothing(const char *text, struct pattern *pattern, char **err)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100201{
202 return 1;
203}
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100204
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100205/* Parse a string. It is allocated and duplicated. */
Thierry FOURNIERedc15c32013-12-13 15:36:59 +0100206int pat_parse_str(const char *text, struct pattern *pattern, char **err)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100207{
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100208 pattern->type = SMP_T_STR;
209 pattern->expect_type = SMP_T_STR;
Thierry FOURNIERedc15c32013-12-13 15:36:59 +0100210 pattern->ptr.str = (char *)text;
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100211 pattern->len = strlen(text);
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100212 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100213}
214
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100215/* Parse a binary written in hexa. It is allocated. */
Thierry FOURNIERedc15c32013-12-13 15:36:59 +0100216int pat_parse_bin(const char *text, struct pattern *pattern, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100217{
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100218 struct chunk *trash;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100219
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100220 pattern->type = SMP_T_BIN;
221 pattern->expect_type = SMP_T_BIN;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100222 trash = get_trash_chunk();
223 pattern->len = trash->size;
224 pattern->ptr.str = trash->str;
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100225 return !!parse_binary(text, &pattern->ptr.str, &pattern->len, err);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100226}
227
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100228/* Parse a regex. It is allocated. */
Thierry FOURNIERedc15c32013-12-13 15:36:59 +0100229int pat_parse_reg(const char *text, struct pattern *pattern, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100230{
Thierry FOURNIER0b2fe4a2013-12-06 20:33:50 +0100231 struct chunk *trash;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100232
Thierry FOURNIERedc15c32013-12-13 15:36:59 +0100233 trash = get_trash_chunk();
234 if (trash->size < sizeof(*pattern->ptr.reg)) {
235 memprintf(err, "no space avalaible in the buffer. expect %d, provides %d",
236 (int)sizeof(*pattern->ptr.reg), trash->size);
237 return 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100238 }
239
Thierry FOURNIERedc15c32013-12-13 15:36:59 +0100240 pattern->ptr.reg = (struct my_regex *)trash->str;
241 pattern->ptr.reg->regstr = (char *)text;
242 pattern->freeptrbuf = NULL;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100243
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100244 pattern->expect_type = SMP_T_STR;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100245 return 1;
246}
247
248/* Parse a range of positive integers delimited by either ':' or '-'. If only
249 * one integer is read, it is set as both min and max. An operator may be
250 * specified as the prefix, among this list of 5 :
251 *
252 * 0:eq, 1:gt, 2:ge, 3:lt, 4:le
253 *
254 * The default operator is "eq". It supports range matching. Ranges are
255 * rejected for other operators. The operator may be changed at any time.
256 * The operator is stored in the 'opaque' argument.
257 *
258 * If err is non-NULL, an error message will be returned there on errors and
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100259 * the caller will have to free it. The function returns zero on error, and
260 * non-zero on success.
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100261 *
262 */
Thierry FOURNIERedc15c32013-12-13 15:36:59 +0100263int pat_parse_int(const char *text, struct pattern *pattern, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100264{
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100265 const char *ptr = text;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100266
267 pattern->type = SMP_T_UINT;
Thierry FOURNIERcc0e0b32013-12-06 16:56:40 +0100268 pattern->expect_type = SMP_T_UINT;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100269
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100270 /* Empty string is not valid */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100271 if (!*text)
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100272 goto not_valid_range;
273
274 /* Search ':' or '-' separator. */
275 while (*ptr != '\0' && *ptr != ':' && *ptr != '-')
276 ptr++;
277
278 /* If separator not found. */
279 if (!*ptr) {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100280 if (strl2llrc(text, ptr - text, &pattern->val.range.min) != 0) {
281 memprintf(err, "'%s' is not a number", text);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100282 return 0;
283 }
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100284 pattern->val.range.max = pattern->val.range.min;
285 pattern->val.range.min_set = 1;
286 pattern->val.range.max_set = 1;
287 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100288 }
289
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100290 /* If the separator is the first character. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100291 if (ptr == text && *(ptr + 1) != '\0') {
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100292 if (strl2llrc(ptr + 1, strlen(ptr + 1), &pattern->val.range.max) != 0)
293 goto not_valid_range;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100294
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100295 pattern->val.range.min_set = 0;
296 pattern->val.range.max_set = 1;
297 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100298 }
299
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100300 /* If separator is the last character. */
301 if (*(ptr + 1) == '\0') {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100302 if (strl2llrc(text, ptr - text, &pattern->val.range.min) != 0)
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100303 goto not_valid_range;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100304
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100305 pattern->val.range.min_set = 1;
306 pattern->val.range.max_set = 0;
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100307 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100308 }
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100309
310 /* Else, parse two numbers. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100311 if (strl2llrc(text, ptr - text, &pattern->val.range.min) != 0)
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100312 goto not_valid_range;
313
314 if (strl2llrc(ptr + 1, strlen(ptr + 1), &pattern->val.range.max) != 0)
315 goto not_valid_range;
316
317 if (pattern->val.range.min > pattern->val.range.max)
318 goto not_valid_range;
319
320 pattern->val.range.min_set = 1;
321 pattern->val.range.max_set = 1;
322 return 1;
323
324 not_valid_range:
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100325 memprintf(err, "'%s' is not a valid number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100326 return 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100327}
328
Thierry FOURNIERedc15c32013-12-13 15:36:59 +0100329int pat_parse_len(const char *text, struct pattern *pattern, char **err)
Thierry FOURNIERcc0e0b32013-12-06 16:56:40 +0100330{
331 int ret;
332
Thierry FOURNIERedc15c32013-12-13 15:36:59 +0100333 ret = pat_parse_int(text, pattern, err);
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100334 pattern->expect_type = SMP_T_STR;
Thierry FOURNIERcc0e0b32013-12-06 16:56:40 +0100335 return ret;
336}
337
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100338/* Parse a range of positive 2-component versions delimited by either ':' or
339 * '-'. The version consists in a major and a minor, both of which must be
340 * smaller than 65536, because internally they will be represented as a 32-bit
341 * integer.
342 * If only one version is read, it is set as both min and max. Just like for
343 * pure integers, an operator may be specified as the prefix, among this list
344 * of 5 :
345 *
346 * 0:eq, 1:gt, 2:ge, 3:lt, 4:le
347 *
348 * The default operator is "eq". It supports range matching. Ranges are
349 * rejected for other operators. The operator may be changed at any time.
350 * The operator is stored in the 'opaque' argument. This allows constructs
351 * such as the following one :
352 *
353 * acl obsolete_ssl ssl_req_proto lt 3
354 * acl unsupported_ssl ssl_req_proto gt 3.1
355 * acl valid_ssl ssl_req_proto 3.0-3.1
356 *
357 */
Thierry FOURNIERedc15c32013-12-13 15:36:59 +0100358int pat_parse_dotted_ver(const char *text, struct pattern *pattern, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100359{
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100360 const char *ptr = text;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100361
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100362 pattern->type = SMP_T_UINT;
363 pattern->expect_type = SMP_T_UINT;
364
365 /* Search ':' or '-' separator. */
366 while (*ptr != '\0' && *ptr != ':' && *ptr != '-')
367 ptr++;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100368
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100369 /* If separator not found. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100370 if (*ptr == '\0' && ptr > text) {
371 if (strl2llrc_dotted(text, ptr-text, &pattern->val.range.min) != 0) {
372 memprintf(err, "'%s' is not a dotted number", text);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100373 return 0;
374 }
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100375 pattern->val.range.max = pattern->val.range.min;
376 pattern->val.range.min_set = 1;
377 pattern->val.range.max_set = 1;
378 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100379 }
380
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100381 /* If the separator is the first character. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100382 if (ptr == text && *(ptr+1) != '\0') {
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100383 if (strl2llrc_dotted(ptr+1, strlen(ptr+1), &pattern->val.range.max) != 0) {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100384 memprintf(err, "'%s' is not a valid dotted number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100385 return 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100386 }
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100387 pattern->val.range.min_set = 0;
388 pattern->val.range.max_set = 1;
389 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100390 }
391
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100392 /* If separator is the last character. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100393 if (ptr == &text[strlen(text)-1]) {
394 if (strl2llrc_dotted(text, ptr-text, &pattern->val.range.min) != 0) {
395 memprintf(err, "'%s' is not a valid dotted number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100396 return 0;
397 }
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100398 pattern->val.range.min_set = 1;
399 pattern->val.range.max_set = 0;
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100400 return 1;
401 }
402
403 /* Else, parse two numbers. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100404 if (strl2llrc_dotted(text, ptr-text, &pattern->val.range.min) != 0) {
405 memprintf(err, "'%s' is not a valid dotted number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100406 return 0;
407 }
408 if (strl2llrc_dotted(ptr+1, strlen(ptr+1), &pattern->val.range.max) != 0) {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100409 memprintf(err, "'%s' is not a valid dotted number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100410 return 0;
411 }
412 if (pattern->val.range.min > pattern->val.range.max) {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100413 memprintf(err, "'%s' is not a valid dotted number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100414 return 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100415 }
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100416 pattern->val.range.min_set = 1;
417 pattern->val.range.max_set = 1;
418 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100419}
420
421/* Parse an IP address and an optional mask in the form addr[/mask].
422 * The addr may either be an IPv4 address or a hostname. The mask
423 * may either be a dotted mask or a number of bits. Returns 1 if OK,
424 * otherwise 0. NOTE: IP address patterns are typed (IPV4/IPV6).
425 */
Thierry FOURNIERedc15c32013-12-13 15:36:59 +0100426int pat_parse_ip(const char *text, struct pattern *pattern, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100427{
Thierry FOURNIERcc0e0b32013-12-06 16:56:40 +0100428 pattern->expect_type = SMP_T_ADDR;
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100429 if (str2net(text, &pattern->val.ipv4.addr, &pattern->val.ipv4.mask)) {
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100430 pattern->type = SMP_T_IPV4;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100431 return 1;
432 }
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100433 else if (str62net(text, &pattern->val.ipv6.addr, &pattern->val.ipv6.mask)) {
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100434 /* no tree support right now */
435 pattern->type = SMP_T_IPV6;
436 return 1;
437 }
438 else {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100439 memprintf(err, "'%s' is not a valid IPv4 or IPv6 address", text);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100440 return 0;
441 }
442}
443
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100444/*
445 *
446 * These functions are exported and may be used by any other component.
447 *
448 * This fucntion just take a sample <smp> and check if this sample match
449 * with the pattern <pattern>. This fucntion return just PAT_MATCH or
450 * PAT_NOMATCH.
451 *
452 */
453
454/* always return false */
455enum pat_match_res pat_match_nothing(struct sample *smp, struct pattern *pattern)
456{
457 return PAT_NOMATCH;
458}
459
460
461/* NB: For two strings to be identical, it is required that their lengths match */
462enum pat_match_res pat_match_str(struct sample *smp, struct pattern *pattern)
463{
464 int icase;
465
466 if (pattern->len != smp->data.str.len)
467 return PAT_NOMATCH;
468
469 icase = pattern->flags & PAT_F_IGNORE_CASE;
470 if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) == 0) ||
471 (!icase && strncmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) == 0))
472 return PAT_MATCH;
473 return PAT_NOMATCH;
474}
475
476/* NB: For two binaries buf to be identical, it is required that their lengths match */
477enum pat_match_res pat_match_bin(struct sample *smp, struct pattern *pattern)
478{
479 if (pattern->len != smp->data.str.len)
480 return PAT_NOMATCH;
481
482 if (memcmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) == 0)
483 return PAT_MATCH;
484 return PAT_NOMATCH;
485}
486
487/* Executes a regex. It temporarily changes the data to add a trailing zero,
488 * and restores the previous character when leaving.
489 */
490enum pat_match_res pat_match_reg(struct sample *smp, struct pattern *pattern)
491{
492 if (regex_exec(pattern->ptr.reg, smp->data.str.str, smp->data.str.len) == 0)
493 return PAT_MATCH;
494 return PAT_NOMATCH;
495}
496
497/* Checks that the pattern matches the beginning of the tested string. */
498enum pat_match_res pat_match_beg(struct sample *smp, struct pattern *pattern)
499{
500 int icase;
501
502 if (pattern->len > smp->data.str.len)
503 return PAT_NOMATCH;
504
505 icase = pattern->flags & PAT_F_IGNORE_CASE;
506 if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str, pattern->len) != 0) ||
507 (!icase && strncmp(pattern->ptr.str, smp->data.str.str, pattern->len) != 0))
508 return PAT_NOMATCH;
509 return PAT_MATCH;
510}
511
512/* Checks that the pattern matches the end of the tested string. */
513enum pat_match_res pat_match_end(struct sample *smp, struct pattern *pattern)
514{
515 int icase;
516
517 if (pattern->len > smp->data.str.len)
518 return PAT_NOMATCH;
519 icase = pattern->flags & PAT_F_IGNORE_CASE;
520 if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str + smp->data.str.len - pattern->len, pattern->len) != 0) ||
521 (!icase && strncmp(pattern->ptr.str, smp->data.str.str + smp->data.str.len - pattern->len, pattern->len) != 0))
522 return PAT_NOMATCH;
523 return PAT_MATCH;
524}
525
526/* Checks that the pattern is included inside the tested string.
527 * NB: Suboptimal, should be rewritten using a Boyer-Moore method.
528 */
529enum pat_match_res pat_match_sub(struct sample *smp, struct pattern *pattern)
530{
531 int icase;
532 char *end;
533 char *c;
534
535 if (pattern->len > smp->data.str.len)
536 return PAT_NOMATCH;
537
538 end = smp->data.str.str + smp->data.str.len - pattern->len;
539 icase = pattern->flags & PAT_F_IGNORE_CASE;
540 if (icase) {
541 for (c = smp->data.str.str; c <= end; c++) {
542 if (tolower(*c) != tolower(*pattern->ptr.str))
543 continue;
544 if (strncasecmp(pattern->ptr.str, c, pattern->len) == 0)
545 return PAT_MATCH;
546 }
547 } else {
548 for (c = smp->data.str.str; c <= end; c++) {
549 if (*c != *pattern->ptr.str)
550 continue;
551 if (strncmp(pattern->ptr.str, c, pattern->len) == 0)
552 return PAT_MATCH;
553 }
554 }
555 return PAT_NOMATCH;
556}
557
558/* This one is used by other real functions. It checks that the pattern is
559 * included inside the tested string, but enclosed between the specified
560 * delimiters or at the beginning or end of the string. The delimiters are
561 * provided as an unsigned int made by make_4delim() and match up to 4 different
562 * delimiters. Delimiters are stripped at the beginning and end of the pattern.
563 */
564static int match_word(struct sample *smp, struct pattern *pattern, unsigned int delimiters)
565{
566 int may_match, icase;
567 char *c, *end;
568 char *ps;
569 int pl;
570
571 pl = pattern->len;
572 ps = pattern->ptr.str;
573
574 while (pl > 0 && is_delimiter(*ps, delimiters)) {
575 pl--;
576 ps++;
577 }
578
579 while (pl > 0 && is_delimiter(ps[pl - 1], delimiters))
580 pl--;
581
582 if (pl > smp->data.str.len)
583 return PAT_NOMATCH;
584
585 may_match = 1;
586 icase = pattern->flags & PAT_F_IGNORE_CASE;
587 end = smp->data.str.str + smp->data.str.len - pl;
588 for (c = smp->data.str.str; c <= end; c++) {
589 if (is_delimiter(*c, delimiters)) {
590 may_match = 1;
591 continue;
592 }
593
594 if (!may_match)
595 continue;
596
597 if (icase) {
598 if ((tolower(*c) == tolower(*ps)) &&
599 (strncasecmp(ps, c, pl) == 0) &&
600 (c == end || is_delimiter(c[pl], delimiters)))
601 return PAT_MATCH;
602 } else {
603 if ((*c == *ps) &&
604 (strncmp(ps, c, pl) == 0) &&
605 (c == end || is_delimiter(c[pl], delimiters)))
606 return PAT_MATCH;
607 }
608 may_match = 0;
609 }
610 return PAT_NOMATCH;
611}
612
613/* Checks that the pattern is included inside the tested string, but enclosed
614 * between the delimiters '?' or '/' or at the beginning or end of the string.
615 * Delimiters at the beginning or end of the pattern are ignored.
616 */
617enum pat_match_res pat_match_dir(struct sample *smp, struct pattern *pattern)
618{
619 return match_word(smp, pattern, make_4delim('/', '?', '?', '?'));
620}
621
622/* Checks that the pattern is included inside the tested string, but enclosed
623 * between the delmiters '/', '?', '.' or ":" or at the beginning or end of
624 * the string. Delimiters at the beginning or end of the pattern are ignored.
625 */
626enum pat_match_res pat_match_dom(struct sample *smp, struct pattern *pattern)
627{
628 return match_word(smp, pattern, make_4delim('/', '?', '.', ':'));
629}
630
631/* Checks that the integer in <test> is included between min and max */
632enum pat_match_res pat_match_int(struct sample *smp, struct pattern *pattern)
633{
634 if ((!pattern->val.range.min_set || pattern->val.range.min <= smp->data.uint) &&
635 (!pattern->val.range.max_set || smp->data.uint <= pattern->val.range.max))
636 return PAT_MATCH;
637 return PAT_NOMATCH;
638}
639
640/* Checks that the length of the pattern in <test> is included between min and max */
641enum pat_match_res pat_match_len(struct sample *smp, struct pattern *pattern)
642{
643 if ((!pattern->val.range.min_set || pattern->val.range.min <= smp->data.str.len) &&
644 (!pattern->val.range.max_set || smp->data.str.len <= pattern->val.range.max))
645 return PAT_MATCH;
646 return PAT_NOMATCH;
647}
648
649enum pat_match_res pat_match_ip(struct sample *smp, struct pattern *pattern)
650{
651 unsigned int v4; /* in network byte order */
652 struct in6_addr *v6;
653 int bits, pos;
654 struct in6_addr tmp6;
655
656 if (pattern->type == SMP_T_IPV4) {
657 if (smp->type == SMP_T_IPV4) {
658 v4 = smp->data.ipv4.s_addr;
659 }
660 else if (smp->type == SMP_T_IPV6) {
661 /* v4 match on a V6 sample. We want to check at least for
662 * the following forms :
663 * - ::ffff:ip:v4 (ipv4 mapped)
664 * - ::0000:ip:v4 (old ipv4 mapped)
665 * - 2002:ip:v4:: (6to4)
666 */
667 if (*(uint32_t*)&smp->data.ipv6.s6_addr[0] == 0 &&
668 *(uint32_t*)&smp->data.ipv6.s6_addr[4] == 0 &&
669 (*(uint32_t*)&smp->data.ipv6.s6_addr[8] == 0 ||
670 *(uint32_t*)&smp->data.ipv6.s6_addr[8] == htonl(0xFFFF))) {
671 v4 = *(uint32_t*)&smp->data.ipv6.s6_addr[12];
672 }
673 else if (*(uint16_t*)&smp->data.ipv6.s6_addr[0] == htons(0x2002)) {
674 v4 = htonl((ntohs(*(uint16_t*)&smp->data.ipv6.s6_addr[2]) << 16) +
675 ntohs(*(uint16_t*)&smp->data.ipv6.s6_addr[4]));
676 }
677 else
678 return PAT_NOMATCH;
679 }
680 else
681 return PAT_NOMATCH;
682
683 if (((v4 ^ pattern->val.ipv4.addr.s_addr) & pattern->val.ipv4.mask.s_addr) == 0)
684 return PAT_MATCH;
685 else
686 return PAT_NOMATCH;
687 }
688 else if (pattern->type == SMP_T_IPV6) {
689 if (smp->type == SMP_T_IPV4) {
690 /* Convert the IPv4 sample address to IPv4 with the
691 * mapping method using the ::ffff: prefix.
692 */
693 memset(&tmp6, 0, 10);
694 *(uint16_t*)&tmp6.s6_addr[10] = htons(0xffff);
695 *(uint32_t*)&tmp6.s6_addr[12] = smp->data.ipv4.s_addr;
696 v6 = &tmp6;
697 }
698 else if (smp->type == SMP_T_IPV6) {
699 v6 = &smp->data.ipv6;
700 }
701 else {
702 return PAT_NOMATCH;
703 }
704
705 bits = pattern->val.ipv6.mask;
706 for (pos = 0; bits > 0; pos += 4, bits -= 32) {
707 v4 = *(uint32_t*)&v6->s6_addr[pos] ^ *(uint32_t*)&pattern->val.ipv6.addr.s6_addr[pos];
708 if (bits < 32)
709 v4 &= htonl((~0U) << (32-bits));
710 if (v4)
711 return PAT_NOMATCH;
712 }
713 return PAT_MATCH;
714 }
715 return PAT_NOMATCH;
716}
717
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100718/* NB: does nothing if <pat> is NULL */
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +0100719void pattern_free(struct pattern_list *pat)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100720{
721 if (!pat)
722 return;
723
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +0100724 if (pat->pat.ptr.ptr) {
725 if (pat->pat.freeptrbuf)
726 pat->pat.freeptrbuf(pat->pat.ptr.ptr);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100727
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +0100728 free(pat->pat.ptr.ptr);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100729 }
730
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +0100731 free(pat->pat.smp);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100732 free(pat);
733}
734
735void free_pattern_list(struct list *head)
736{
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +0100737 struct pattern_list *pat, *tmp;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100738 list_for_each_entry_safe(pat, tmp, head, list)
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100739 pattern_free(pat);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100740}
741
742void free_pattern_tree(struct eb_root *root)
743{
744 struct eb_node *node, *next;
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +0100745 struct pattern_tree *elt;
Thierry FOURNIER3ce88c72013-12-09 11:29:46 +0100746
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100747 node = eb_first(root);
748 while (node) {
749 next = eb_next(node);
750 eb_delete(node);
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +0100751 elt = container_of(node, struct pattern_tree, node);
Thierry FOURNIERc64de3f2013-12-10 15:08:39 +0100752 free(elt->smp);
Thierry FOURNIER3ce88c72013-12-09 11:29:46 +0100753 free(elt);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100754 node = next;
755 }
756}
757
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100758void pattern_prune_expr(struct pattern_expr *expr)
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +0100759{
760 free_pattern_list(&expr->patterns);
761 free_pattern_tree(&expr->pattern_tree);
762 LIST_INIT(&expr->patterns);
763}
764
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100765void pattern_init_expr(struct pattern_expr *expr)
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +0100766{
767 LIST_INIT(&expr->patterns);
768 expr->pattern_tree = EB_ROOT_UNIQUE;
769}
770
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100771/*
772 *
773 * The following functions are used for the pattern indexation
774 *
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100775 */
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100776
777int pat_idx_list_val(struct pattern_expr *expr, struct pattern *pat, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100778{
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100779 struct pattern_list *patl;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100780
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100781 /* allocate pattern */
782 patl = calloc(1, sizeof(*patl));
783 if (!patl) {
784 memprintf(err, "out of memory while indexing pattern");
Thierry FOURNIER972028f2014-01-23 17:53:31 +0100785 return 0;
786 }
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100787
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100788 /* duplicate pattern */
789 memcpy(&patl->pat, pat, sizeof(*pat));
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100790
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100791 /* chain pattern in the expression */
792 LIST_ADDQ(&expr->patterns, &patl->list);
793
794 /* that's ok */
795 return 1;
796}
797
798int pat_idx_list_ptr(struct pattern_expr *expr, struct pattern *pat, char **err)
799{
800 struct pattern_list *patl;
801
802 /* allocate pattern */
803 patl = calloc(1, sizeof(*patl));
804 if (!patl)
Thierry FOURNIER972028f2014-01-23 17:53:31 +0100805 return 0;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100806
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100807 /* duplicate pattern */
808 memcpy(&patl->pat, pat, sizeof(*pat));
809 patl->pat.ptr.ptr = malloc(patl->pat.len);
810 if (!patl->pat.ptr.ptr) {
811 free(patl);
812 memprintf(err, "out of memory while indexing pattern");
813 return 0;
814 }
815 memcpy(patl->pat.ptr.ptr, pat->ptr.ptr, pat->len);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100816
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100817 /* chain pattern in the expression */
818 LIST_ADDQ(&expr->patterns, &patl->list);
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100819
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100820 /* that's ok */
821 return 1;
822}
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100823
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100824int pat_idx_list_str(struct pattern_expr *expr, struct pattern *pat, char **err)
825{
826 struct pattern_list *patl;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100827
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100828 /* allocate pattern */
829 patl = calloc(1, sizeof(*patl));
830 if (!patl) {
831 memprintf(err, "out of memory while indexing pattern");
832 return 0;
833 }
834
835 /* duplicate pattern */
836 memcpy(&patl->pat, pat, sizeof(*pat));
837 patl->pat.ptr.str = malloc(patl->pat.len + 1);
838 if (!patl->pat.ptr.str) {
839 free(patl);
840 memprintf(err, "out of memory while indexing pattern");
841 return 0;
842 }
843 memcpy(patl->pat.ptr.ptr, pat->ptr.ptr, pat->len);
844 patl->pat.ptr.str[patl->pat.len] = '\0';
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100845
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100846 /* chain pattern in the expression */
847 LIST_ADDQ(&expr->patterns, &patl->list);
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100848
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100849 /* that's ok */
850 return 1;
851}
852
853int pat_idx_list_reg(struct pattern_expr *expr, struct pattern *pat, char **err)
854{
855 struct pattern_list *patl;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100856
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100857 /* allocate pattern */
858 patl = calloc(1, sizeof(*patl));
859 if (!patl) {
860 memprintf(err, "out of memory while indexing pattern");
861 return 0;
Thierry FOURNIER972028f2014-01-23 17:53:31 +0100862 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100863
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100864 /* duplicate pattern */
865 memcpy(&patl->pat, pat, sizeof(*pat));
866
867 /* allocate regex */
868 patl->pat.ptr.reg = calloc(1, sizeof(*patl->pat.ptr.reg));
869 if (!patl->pat.ptr.reg) {
870 free(patl);
871 memprintf(err, "out of memory while indexing pattern");
872 return 0;
873 }
874
875 /* compile regex */
876 if (!regex_comp(pat->ptr.reg->regstr, patl->pat.ptr.reg, !(patl->pat.flags & PAT_F_IGNORE_CASE), 0, err)) {
877 free(patl);
878 free(patl->pat.ptr.reg);
879 return 0;
880 }
881
882 /* free pattern method */
883 patl->pat.freeptrbuf = &pat_free_reg;
884
885 /* chain pattern in the expression */
886 LIST_ADDQ(&expr->patterns, &patl->list);
887
888 /* that's ok */
889 return 1;
890}
891
892int pat_idx_tree_ip(struct pattern_expr *expr, struct pattern *pat, char **err)
893{
894 unsigned int mask;
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +0100895 struct pattern_tree *node;
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100896
897 /* Only IPv4 can be indexed */
898 if (pat->type == SMP_T_IPV4) {
Thierry FOURNIER972028f2014-01-23 17:53:31 +0100899 /* in IPv4 case, check if the mask is contiguous so that we can
900 * insert the network into the tree. A continuous mask has only
901 * ones on the left. This means that this mask + its lower bit
902 * added once again is null.
903 */
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100904 mask = ntohl(pat->val.ipv4.mask.s_addr);
905 if (mask + (mask & -mask) == 0) {
906 mask = mask ? 33 - flsnz(mask & -mask) : 0; /* equals cidr value */
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100907
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100908 /* node memory allocation */
909 node = calloc(1, sizeof(*node) + 4);
910 if (!node) {
911 memprintf(err, "out of memory while loading pattern");
912 return 0;
913 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100914
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100915 /* copy the pointer to sample associated to this node */
916 node->smp = pat->smp;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100917
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100918 /* FIXME: insert <addr>/<mask> into the tree here */
919 memcpy(node->node.key, &pat->val.ipv4.addr, 4); /* network byte order */
920 node->node.node.pfx = mask;
921 if (ebmb_insert_prefix(&expr->pattern_tree, &node->node, 4) != &node->node)
922 free(node); /* was a duplicate */
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100923
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100924 /* that's ok */
925 return 1;
926 }
927 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100928
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100929 /* If the value cannot be indexed, just add it to the list */
930 return pat_idx_list_val(expr, pat, err);
931}
932
933int pat_idx_tree_str(struct pattern_expr *expr, struct pattern *pat, char **err)
934{
935 int len;
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +0100936 struct pattern_tree *node;
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100937
938 /* Only string can be indexed */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100939 if (pat->type != SMP_T_STR) {
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100940 memprintf(err, "internal error: string expected, but the type is '%s'",
941 smp_to_type[pat->type]);
942 return 0;
Thierry FOURNIER972028f2014-01-23 17:53:31 +0100943 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100944
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100945 /* If the flag PAT_F_IGNORE_CASE is set, we cannot use trees */
946 if (pat->flags & PAT_F_IGNORE_CASE)
947 return pat_idx_list_str(expr, pat, err);
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100948
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100949 /* Process the key len */
950 len = strlen(pat->ptr.str) + 1;
951
952 /* node memory allocation */
953 node = calloc(1, sizeof(*node) + len);
954 if (!node) {
955 memprintf(err, "out of memory while loading pattern");
956 return 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100957 }
958
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100959 /* copy the pointer to sample associated to this node */
960 node->smp = pat->smp;
961
962 /* copy the string */
963 memcpy(node->node.key, pat->ptr.str, len);
964
965 /* index the new node */
966 if (ebst_insert(&expr->pattern_tree, &node->node) != &node->node)
967 free(node); /* was a duplicate */
968
969 /* that's ok */
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100970 return 1;
971}
972
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100973/* return 1 if the process is ok
974 * return -1 if the parser fail. The err message is filled.
975 * return -2 if out of memory
976 */
977int pattern_register(struct pattern_expr *expr, const char *arg,
978 struct sample_storage *smp,
979 int patflags, char **err)
980{
981 int ret;
982 struct pattern pattern;
983
984 /* initialise pattern */
985 memset(&pattern, 0, sizeof(pattern));
986 pattern.flags = patflags;
987 pattern.smp = smp;
988
989 /* parse pattern */
Thierry FOURNIERedc15c32013-12-13 15:36:59 +0100990 ret = expr->parse(arg, &pattern, err);
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100991 if (!ret)
992 return 0;
993
994 /* index pattern */
995 if (!expr->index(expr, &pattern, err))
996 return 0;
997
998 return 1;
999}
1000
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001001/* Reads patterns from a file. If <err_msg> is non-NULL, an error message will
1002 * be returned there on errors and the caller will have to free it.
1003 */
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01001004int pattern_read_from_file(struct pattern_expr *expr,
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001005 const char *filename, int patflags,
1006 char **err)
1007{
1008 FILE *file;
1009 char *c;
1010 char *arg;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001011 int ret = 0;
1012 int line = 0;
1013 int code;
1014
1015 file = fopen(filename, "r");
1016 if (!file) {
1017 memprintf(err, "failed to open pattern file <%s>", filename);
1018 return 0;
1019 }
1020
1021 /* now parse all patterns. The file may contain only one pattern per
1022 * line. If the line contains spaces, they will be part of the pattern.
1023 * The pattern stops at the first CR, LF or EOF encountered.
1024 */
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001025 while (fgets(trash.str, trash.size, file) != NULL) {
1026 line++;
1027 c = trash.str;
1028
1029 /* ignore lines beginning with a dash */
1030 if (*c == '#')
1031 continue;
1032
1033 /* strip leading spaces and tabs */
1034 while (*c == ' ' || *c == '\t')
1035 c++;
1036
1037
1038 arg = c;
1039 while (*c && *c != '\n' && *c != '\r')
1040 c++;
1041 *c = 0;
1042
1043 /* empty lines are ignored too */
1044 if (c == arg)
1045 continue;
1046
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001047 code = pattern_register(expr, arg, NULL, patflags, err);
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001048 if (code == -2) {
1049 memprintf(err, "out of memory when loading patterns from file <%s>", filename);
1050 goto out_close;
1051 }
1052 else if (code < 0) {
1053 memprintf(err, "%s when loading patterns from file <%s>", *err, filename);
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001054 goto out_close;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001055 }
1056 }
1057
1058 ret = 1; /* success */
1059
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001060 out_close:
1061 fclose(file);
1062 return ret;
1063}
1064
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01001065/* This function executes a pattern match on a sample. It applies pattern <expr>
1066 * to sample <smp>. The function returns NULL if the sample dont match. It returns
1067 * non-null if the sample match. If <fill> is true and the sample match, the
1068 * function returns the matched pattern. In many cases, this pattern can be a
1069 * static buffer.
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001070 */
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01001071struct pattern *pattern_exec_match(struct pattern_expr *expr, struct sample *smp, int fill)
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001072{
Willy Tarreau0cba6072013-11-28 22:21:02 +01001073 enum pat_match_res pat_res = PAT_NOMATCH;
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01001074 struct pattern_list *pattern = NULL;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001075 struct ebmb_node *node = NULL;
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01001076 struct pattern_tree *elt = NULL;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001077
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01001078 if (expr->match == pat_match_nothing) {
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001079 if (smp->data.uint)
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01001080 pat_res |= PAT_MATCH;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001081 else
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01001082 pat_res |= PAT_NOMATCH;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001083 }
1084 else if (!expr->match) {
1085 /* just check for existence */
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01001086 pat_res |= PAT_MATCH;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001087 }
1088 else {
1089 if (!eb_is_empty(&expr->pattern_tree)) {
1090 /* a tree is present, let's check what type it is */
Thierry FOURNIERe3ded592013-12-06 15:36:54 +01001091 if (expr->match == pat_match_str) {
1092 if (sample_convert(smp, SMP_T_STR))
1093 node = pat_lookup_str(smp, expr);
1094 }
1095 else if (expr->match == pat_match_ip) {
1096 if (sample_convert(smp, SMP_T_IPV4))
1097 node = pat_lookup_ip(smp, expr);
1098 }
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001099 if (node) {
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01001100 pat_res |= PAT_MATCH;
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +01001101 elt = ebmb_entry(node, struct pattern_tree, node);
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001102 }
1103 }
1104
1105 /* call the match() function for all tests on this value */
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01001106 if (pat_res != PAT_MATCH) {
1107 list_for_each_entry(pattern, &expr->patterns, list) {
1108 if (sample_convert(smp, pattern->pat.expect_type))
1109 pat_res |= expr->match(smp, &pattern->pat);
1110 if (pat_res == PAT_MATCH)
1111 break;
1112 }
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001113 }
1114 }
1115
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01001116 if (pat_res == PAT_MATCH) {
1117 static_pattern.flags = 0;
1118 if (fill) {
1119 /* fill with boolean */
1120 if (expr->match == NULL ||
1121 expr->match == pat_match_nothing) {
1122 static_pattern.smp = NULL;
1123 static_pattern.type = SMP_T_BOOL;
1124 static_pattern.val.i = 1;
1125 return &static_pattern;
1126 }
1127
1128 /* fill with ipv4 */
1129 if (expr->match == pat_match_ip && elt) {
1130 static_pattern.smp = elt->smp;;
1131 static_pattern.flags |= PAT_F_TREE;
1132 static_pattern.type = SMP_T_IPV4;
1133 memcpy(&static_pattern.val.ipv4.addr, &elt->node.key, 4);
1134 if (!cidr2dotted(elt->node.node.pfx, &static_pattern.val.ipv4.mask))
1135 return NULL;
1136 return &static_pattern;
1137 }
1138
1139 /* fill with string */
1140 if (expr->match == pat_match_str && elt) {
1141 static_pattern.smp = elt->smp;;
1142 static_pattern.flags |= PAT_F_TREE;
1143 static_pattern.type = SMP_T_STR;
1144 static_pattern.ptr.str = (char *)elt->node.key;
1145 return &static_pattern;
1146 }
1147
1148 /* return the pattern */
1149 return &pattern->pat;
1150 }
1151
1152 /* Return uninitialized pattern. The content must not be used by the caller */
1153 return &static_pattern;
1154 }
1155
1156 /* No match */
1157 return NULL;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001158}
1159
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001160/* This function browse the pattern expr <expr> to lookup the key <key>. On
1161 * error it returns 0. On success, it returns 1 and fills either <pat_elt>
1162 * or <idx_elt> with the respectively matched pointers, and the other one with
1163 * NULL. Pointers are not set if they're passed as NULL.
1164 */
1165int pattern_lookup(const char *key, struct pattern_expr *expr,
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +01001166 struct pattern_list **pat_elt, struct pattern_tree **idx_elt, char **err)
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001167{
1168 struct pattern pattern;
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001169 struct pattern_list *pat;
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001170 struct ebmb_node *node;
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +01001171 struct pattern_tree *elt;
Willy Tarreau668ae532013-12-15 16:42:26 +01001172 unsigned int mask = 0;
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001173
1174 /* no real pattern */
1175 if (!expr->match || expr->match == pat_match_nothing)
1176 return 0;
1177
1178 /* build lookup pattern */
Thierry FOURNIERedc15c32013-12-13 15:36:59 +01001179 if (!expr->parse(key, &pattern, NULL))
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001180 return 0;
1181
1182 pat = NULL;
1183 elt = NULL;
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001184
Thierry FOURNIERc64de3f2013-12-10 15:08:39 +01001185 /* Try to look up the tree first. IPv6 is not indexed */
1186 if (!eb_is_empty(&expr->pattern_tree) && pattern.type != SMP_T_IPV6) {
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001187 /* Check the pattern type */
1188 if (pattern.type != SMP_T_STR &&
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001189 pattern.type != SMP_T_IPV4) {
1190 memprintf(err, "Unexpected pattern type.");
1191 return 0;
1192 }
1193
1194 /* Convert mask. If the mask is not contiguous, ignore the lookup
1195 * in the tree, and browse the list.
1196 */
1197 if (expr->match == pat_match_ip) {
1198 mask = ntohl(pattern.val.ipv4.mask.s_addr);
1199 if (mask + (mask & -mask) != 0)
1200 goto browse_list;
1201 mask = mask ? 33 - flsnz(mask & -mask) : 0; /* equals cidr value */
1202 }
1203
1204 /* browse each node of the tree, and check string */
1205 if (expr->match == pat_match_str) {
1206 for (node = ebmb_first(&expr->pattern_tree);
1207 node;
1208 node = ebmb_next(node)) {
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +01001209 elt = container_of(node, struct pattern_tree, node);
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001210 if (strcmp(pattern.ptr.str, (char *)elt->node.key) == 0)
1211 goto found;
1212 }
1213 }
1214 else if (expr->match == pat_match_ip) {
1215 for (node = ebmb_first(&expr->pattern_tree);
1216 node;
1217 node = ebmb_next(node)) {
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +01001218 elt = container_of(node, struct pattern_tree, node);
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001219 if (elt->node.node.pfx == mask &&
1220 memcmp(&pattern.val.ipv4.addr.s_addr, elt->node.key, 4) == 0)
1221 goto found;
1222 }
1223 }
1224 }
1225
1226browse_list:
Thierry FOURNIERc64de3f2013-12-10 15:08:39 +01001227 elt = NULL;
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001228 if (expr->parse == pat_parse_int ||
1229 expr->parse == pat_parse_len) {
1230 list_for_each_entry(pat, &expr->patterns, list) {
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001231 if (pat->pat.flags & PAT_F_TREE)
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001232 continue;
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001233 if (pattern.val.range.min_set != pat->pat.val.range.min_set)
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001234 continue;
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001235 if (pattern.val.range.max_set != pat->pat.val.range.max_set)
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001236 continue;
1237 if (pattern.val.range.min_set &&
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001238 pattern.val.range.min != pat->pat.val.range.min)
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001239 continue;
1240 if (pattern.val.range.max_set &&
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001241 pattern.val.range.max != pat->pat.val.range.max)
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001242 continue;
1243 goto found;
1244 }
1245 }
1246 else if (expr->parse == pat_parse_ip) {
1247 list_for_each_entry(pat, &expr->patterns, list) {
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001248 if (pat->pat.flags & PAT_F_TREE)
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001249 continue;
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001250 if (pattern.type != pat->pat.type)
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001251 continue;
1252 if (pattern.type == SMP_T_IPV4 &&
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001253 memcmp(&pattern.val.ipv4.addr, &pat->pat.val.ipv4.addr, sizeof(pat->pat.val.ipv4.addr)) != 0)
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001254 continue;
1255 if (pattern.type == SMP_T_IPV4 &&
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001256 memcmp(&pattern.val.ipv4.mask, &pat->pat.val.ipv4.mask, sizeof(pat->pat.val.ipv4.addr)) != 0)
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001257 continue;
1258 if (pattern.type == SMP_T_IPV6 &&
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001259 memcmp(&pattern.val.ipv6.addr, &pat->pat.val.ipv6.addr, sizeof(pat->pat.val.ipv6.addr)) != 0)
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001260 continue;
1261 if (pattern.type == SMP_T_IPV6 &&
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001262 pattern.val.ipv6.mask != pat->pat.val.ipv6.mask)
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001263 continue;
1264 goto found;
1265 }
1266 }
1267 else if (expr->parse == pat_parse_str) {
1268 list_for_each_entry(pat, &expr->patterns, list) {
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001269 if (pat->pat.flags & PAT_F_TREE)
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001270 continue;
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001271 if (pattern.len != pat->pat.len)
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001272 continue;
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001273 if (pat->pat.flags & PAT_F_IGNORE_CASE) {
1274 if (strncasecmp(pattern.ptr.str, pat->pat.ptr.str, pat->pat.len) != 0)
Thierry FOURNIER35249cb2014-01-14 13:38:40 +01001275 continue;
1276 }
1277 else {
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001278 if (strncmp(pattern.ptr.str, pat->pat.ptr.str, pat->pat.len) != 0)
Thierry FOURNIER35249cb2014-01-14 13:38:40 +01001279 continue;
1280 }
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001281 goto found;
1282 }
1283 }
1284 else if (expr->parse == pat_parse_bin) {
1285 list_for_each_entry(pat, &expr->patterns, list) {
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001286 if (pat->pat.flags & PAT_F_TREE)
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001287 continue;
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001288 if (pattern.len != pat->pat.len)
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001289 continue;
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001290 if (memcmp(pattern.ptr.ptr, pat->pat.ptr.ptr, pat->pat.len) != 0)
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001291 continue;
1292 goto found;
1293 }
1294 }
1295 else if (expr->parse == pat_parse_reg) {
1296 list_for_each_entry(pat, &expr->patterns, list) {
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001297 if (pat->pat.flags & PAT_F_TREE)
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001298 continue;
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001299 if (pat->pat.flags & PAT_F_IGNORE_CASE) {
1300 if (strcasecmp(pattern.ptr.reg->regstr, pat->pat.ptr.reg->regstr) != 0)
Thierry FOURNIER35249cb2014-01-14 13:38:40 +01001301 continue;
1302 }
1303 else {
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001304 if (strcmp(pattern.ptr.reg->regstr, pat->pat.ptr.reg->regstr) != 0)
Thierry FOURNIER35249cb2014-01-14 13:38:40 +01001305 continue;
1306 }
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001307 goto found;
1308 }
1309 }
1310
1311 /* if we get there, we didn't find the pattern */
1312 return 0;
1313found:
1314 if (idx_elt)
1315 *idx_elt = elt;
1316
1317 if (pat_elt)
1318 *pat_elt = pat;
1319
1320 return 1;
1321}