blob: fa3b7c561ad5295a51d665334986b1f359ab0546 [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,
97 [PAT_MATCH_BIN] = SMP_T_CBIN,
98 [PAT_MATCH_LEN] = SMP_T_CSTR,
99 [PAT_MATCH_STR] = SMP_T_CSTR,
100 [PAT_MATCH_BEG] = SMP_T_CSTR,
101 [PAT_MATCH_SUB] = SMP_T_CSTR,
102 [PAT_MATCH_DIR] = SMP_T_CSTR,
103 [PAT_MATCH_DOM] = SMP_T_CSTR,
104 [PAT_MATCH_END] = SMP_T_CSTR,
105 [PAT_MATCH_REG] = SMP_T_CSTR,
106};
107
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100108/*
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100109 *
110 * The following functions are not exported and are used by internals process
111 * of pattern matching
112 *
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100113 */
114
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100115/* Lookup an IPv4 address in the expression's pattern tree using the longest
116 * match method. The node is returned if it exists, otherwise NULL.
117 */
118static void *pat_lookup_ip(struct sample *smp, struct pattern_expr *expr)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100119{
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100120 struct in_addr *s;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100121
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100122 if (smp->type != SMP_T_IPV4)
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100123 return PAT_NOMATCH;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100124
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100125 s = &smp->data.ipv4;
126 return ebmb_lookup_longest(&expr->pattern_tree, &s->s_addr);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100127}
128
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100129/* Free data allocated by pat_parse_reg */
130static void pat_free_reg(void *ptr)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100131{
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100132 regex_free(ptr);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100133}
134
135/* Lookup a string in the expression's pattern tree. The node is returned if it
136 * exists, otherwise NULL.
137 */
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100138static void *pat_lookup_str(struct sample *smp, struct pattern_expr *expr)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100139{
140 /* data are stored in a tree */
141 struct ebmb_node *node;
142 char prev;
143
144 /* we may have to force a trailing zero on the test pattern */
145 prev = smp->data.str.str[smp->data.str.len];
146 if (prev)
147 smp->data.str.str[smp->data.str.len] = '\0';
148 node = ebst_lookup(&expr->pattern_tree, smp->data.str.str);
149 if (prev)
150 smp->data.str.str[smp->data.str.len] = prev;
151 return node;
152}
153
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100154/* Background: Fast way to find a zero byte in a word
155 * http://graphics.stanford.edu/~seander/bithacks.html#ZeroInWord
156 * hasZeroByte = (v - 0x01010101UL) & ~v & 0x80808080UL;
157 *
158 * To look for 4 different byte values, xor the word with those bytes and
159 * then check for zero bytes:
160 *
161 * v = (((unsigned char)c * 0x1010101U) ^ delimiter)
162 * where <delimiter> is the 4 byte values to look for (as an uint)
163 * and <c> is the character that is being tested
164 */
165static inline unsigned int is_delimiter(unsigned char c, unsigned int mask)
166{
167 mask ^= (c * 0x01010101); /* propagate the char to all 4 bytes */
168 return (mask - 0x01010101) & ~mask & 0x80808080U;
169}
170
171static inline unsigned int make_4delim(unsigned char d1, unsigned char d2, unsigned char d3, unsigned char d4)
172{
173 return d1 << 24 | d2 << 16 | d3 << 8 | d4;
174}
175
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100176
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100177/*
178 *
179 * These functions are exported and may be used by any other component.
180 *
181 * The following functions are used for parsing pattern matching
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100182 * input value. The <text> contain the string to be parsed. <pattern>
183 * must be a preallocated pattern. The pat_parse_* functions fill this
184 * structure with the parsed value. <usage> can be PAT_U_COMPILE or
185 * PAT_U_LOOKUP. If the value PAT_U_COMPILE is used memory is allocated
186 * for filling the pattern. If the value PAT_U_LOOKUP is set, the parser
187 * use "trash" or return pointers to the input strings. In both cases,
188 * the caller must use the value PAT_U_LOOKUP with caution. <err> is
189 * filled with an error message built with memprintf() function.
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100190 *
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100191 * In succes case, the pat_parse_* function return 1. If the function
192 * fail, it returns 0 and <err> is filled.
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100193 *
194 */
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100195
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100196/* ignore the current line */
Thierry FOURNIERedc15c32013-12-13 15:36:59 +0100197int pat_parse_nothing(const char *text, struct pattern *pattern, char **err)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100198{
199 return 1;
200}
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100201
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100202/* Parse a string. It is allocated and duplicated. */
Thierry FOURNIERedc15c32013-12-13 15:36:59 +0100203int pat_parse_str(const char *text, struct pattern *pattern, char **err)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100204{
205 pattern->type = SMP_T_CSTR;
206 pattern->expect_type = SMP_T_CSTR;
Thierry FOURNIERedc15c32013-12-13 15:36:59 +0100207 pattern->ptr.str = (char *)text;
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100208 pattern->len = strlen(text);
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100209 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100210}
211
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100212/* Parse a binary written in hexa. It is allocated. */
Thierry FOURNIERedc15c32013-12-13 15:36:59 +0100213int pat_parse_bin(const char *text, struct pattern *pattern, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100214{
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100215 struct chunk *trash;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100216
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100217 pattern->type = SMP_T_CBIN;
218 pattern->expect_type = SMP_T_CBIN;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100219 trash = get_trash_chunk();
220 pattern->len = trash->size;
221 pattern->ptr.str = trash->str;
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100222 return !!parse_binary(text, &pattern->ptr.str, &pattern->len, err);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100223}
224
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100225/* Parse a regex. It is allocated. */
Thierry FOURNIERedc15c32013-12-13 15:36:59 +0100226int pat_parse_reg(const char *text, struct pattern *pattern, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100227{
Thierry FOURNIER0b2fe4a2013-12-06 20:33:50 +0100228 struct chunk *trash;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100229
Thierry FOURNIERedc15c32013-12-13 15:36:59 +0100230 trash = get_trash_chunk();
231 if (trash->size < sizeof(*pattern->ptr.reg)) {
232 memprintf(err, "no space avalaible in the buffer. expect %d, provides %d",
233 (int)sizeof(*pattern->ptr.reg), trash->size);
234 return 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100235 }
236
Thierry FOURNIERedc15c32013-12-13 15:36:59 +0100237 pattern->ptr.reg = (struct my_regex *)trash->str;
238 pattern->ptr.reg->regstr = (char *)text;
239 pattern->freeptrbuf = NULL;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100240
Thierry FOURNIERcc0e0b32013-12-06 16:56:40 +0100241 pattern->expect_type = SMP_T_CSTR;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100242 return 1;
243}
244
245/* Parse a range of positive integers delimited by either ':' or '-'. If only
246 * one integer is read, it is set as both min and max. An operator may be
247 * specified as the prefix, among this list of 5 :
248 *
249 * 0:eq, 1:gt, 2:ge, 3:lt, 4:le
250 *
251 * The default operator is "eq". It supports range matching. Ranges are
252 * rejected for other operators. The operator may be changed at any time.
253 * The operator is stored in the 'opaque' argument.
254 *
255 * If err is non-NULL, an error message will be returned there on errors and
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100256 * the caller will have to free it. The function returns zero on error, and
257 * non-zero on success.
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100258 *
259 */
Thierry FOURNIERedc15c32013-12-13 15:36:59 +0100260int pat_parse_int(const char *text, struct pattern *pattern, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100261{
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100262 const char *ptr = text;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100263
264 pattern->type = SMP_T_UINT;
Thierry FOURNIERcc0e0b32013-12-06 16:56:40 +0100265 pattern->expect_type = SMP_T_UINT;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100266
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100267 /* Empty string is not valid */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100268 if (!*text)
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100269 goto not_valid_range;
270
271 /* Search ':' or '-' separator. */
272 while (*ptr != '\0' && *ptr != ':' && *ptr != '-')
273 ptr++;
274
275 /* If separator not found. */
276 if (!*ptr) {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100277 if (strl2llrc(text, ptr - text, &pattern->val.range.min) != 0) {
278 memprintf(err, "'%s' is not a number", text);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100279 return 0;
280 }
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100281 pattern->val.range.max = pattern->val.range.min;
282 pattern->val.range.min_set = 1;
283 pattern->val.range.max_set = 1;
284 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100285 }
286
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100287 /* If the separator is the first character. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100288 if (ptr == text && *(ptr + 1) != '\0') {
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100289 if (strl2llrc(ptr + 1, strlen(ptr + 1), &pattern->val.range.max) != 0)
290 goto not_valid_range;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100291
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100292 pattern->val.range.min_set = 0;
293 pattern->val.range.max_set = 1;
294 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100295 }
296
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100297 /* If separator is the last character. */
298 if (*(ptr + 1) == '\0') {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100299 if (strl2llrc(text, ptr - text, &pattern->val.range.min) != 0)
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100300 goto not_valid_range;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100301
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100302 pattern->val.range.min_set = 1;
303 pattern->val.range.max_set = 0;
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100304 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100305 }
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100306
307 /* Else, parse two numbers. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100308 if (strl2llrc(text, ptr - text, &pattern->val.range.min) != 0)
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100309 goto not_valid_range;
310
311 if (strl2llrc(ptr + 1, strlen(ptr + 1), &pattern->val.range.max) != 0)
312 goto not_valid_range;
313
314 if (pattern->val.range.min > pattern->val.range.max)
315 goto not_valid_range;
316
317 pattern->val.range.min_set = 1;
318 pattern->val.range.max_set = 1;
319 return 1;
320
321 not_valid_range:
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100322 memprintf(err, "'%s' is not a valid number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100323 return 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100324}
325
Thierry FOURNIERedc15c32013-12-13 15:36:59 +0100326int pat_parse_len(const char *text, struct pattern *pattern, char **err)
Thierry FOURNIERcc0e0b32013-12-06 16:56:40 +0100327{
328 int ret;
329
Thierry FOURNIERedc15c32013-12-13 15:36:59 +0100330 ret = pat_parse_int(text, pattern, err);
Thierry FOURNIERcc0e0b32013-12-06 16:56:40 +0100331 pattern->expect_type = SMP_T_CSTR;
332 return ret;
333}
334
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100335/* Parse a range of positive 2-component versions delimited by either ':' or
336 * '-'. The version consists in a major and a minor, both of which must be
337 * smaller than 65536, because internally they will be represented as a 32-bit
338 * integer.
339 * If only one version is read, it is set as both min and max. Just like for
340 * pure integers, an operator may be specified as the prefix, among this list
341 * of 5 :
342 *
343 * 0:eq, 1:gt, 2:ge, 3:lt, 4:le
344 *
345 * The default operator is "eq". It supports range matching. Ranges are
346 * rejected for other operators. The operator may be changed at any time.
347 * The operator is stored in the 'opaque' argument. This allows constructs
348 * such as the following one :
349 *
350 * acl obsolete_ssl ssl_req_proto lt 3
351 * acl unsupported_ssl ssl_req_proto gt 3.1
352 * acl valid_ssl ssl_req_proto 3.0-3.1
353 *
354 */
Thierry FOURNIERedc15c32013-12-13 15:36:59 +0100355int pat_parse_dotted_ver(const char *text, struct pattern *pattern, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100356{
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100357 const char *ptr = text;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100358
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100359 pattern->type = SMP_T_UINT;
360 pattern->expect_type = SMP_T_UINT;
361
362 /* Search ':' or '-' separator. */
363 while (*ptr != '\0' && *ptr != ':' && *ptr != '-')
364 ptr++;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100365
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100366 /* If separator not found. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100367 if (*ptr == '\0' && ptr > text) {
368 if (strl2llrc_dotted(text, ptr-text, &pattern->val.range.min) != 0) {
369 memprintf(err, "'%s' is not a dotted number", text);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100370 return 0;
371 }
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100372 pattern->val.range.max = pattern->val.range.min;
373 pattern->val.range.min_set = 1;
374 pattern->val.range.max_set = 1;
375 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100376 }
377
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100378 /* If the separator is the first character. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100379 if (ptr == text && *(ptr+1) != '\0') {
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100380 if (strl2llrc_dotted(ptr+1, strlen(ptr+1), &pattern->val.range.max) != 0) {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100381 memprintf(err, "'%s' is not a valid dotted number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100382 return 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100383 }
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100384 pattern->val.range.min_set = 0;
385 pattern->val.range.max_set = 1;
386 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100387 }
388
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100389 /* If separator is the last character. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100390 if (ptr == &text[strlen(text)-1]) {
391 if (strl2llrc_dotted(text, ptr-text, &pattern->val.range.min) != 0) {
392 memprintf(err, "'%s' is not a valid dotted number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100393 return 0;
394 }
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100395 pattern->val.range.min_set = 1;
396 pattern->val.range.max_set = 0;
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100397 return 1;
398 }
399
400 /* Else, parse two numbers. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100401 if (strl2llrc_dotted(text, ptr-text, &pattern->val.range.min) != 0) {
402 memprintf(err, "'%s' is not a valid dotted number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100403 return 0;
404 }
405 if (strl2llrc_dotted(ptr+1, strlen(ptr+1), &pattern->val.range.max) != 0) {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100406 memprintf(err, "'%s' is not a valid dotted number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100407 return 0;
408 }
409 if (pattern->val.range.min > pattern->val.range.max) {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100410 memprintf(err, "'%s' is not a valid dotted number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100411 return 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100412 }
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100413 pattern->val.range.min_set = 1;
414 pattern->val.range.max_set = 1;
415 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100416}
417
418/* Parse an IP address and an optional mask in the form addr[/mask].
419 * The addr may either be an IPv4 address or a hostname. The mask
420 * may either be a dotted mask or a number of bits. Returns 1 if OK,
421 * otherwise 0. NOTE: IP address patterns are typed (IPV4/IPV6).
422 */
Thierry FOURNIERedc15c32013-12-13 15:36:59 +0100423int pat_parse_ip(const char *text, struct pattern *pattern, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100424{
Thierry FOURNIERcc0e0b32013-12-06 16:56:40 +0100425 pattern->expect_type = SMP_T_ADDR;
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100426 if (str2net(text, &pattern->val.ipv4.addr, &pattern->val.ipv4.mask)) {
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100427 pattern->type = SMP_T_IPV4;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100428 return 1;
429 }
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100430 else if (str62net(text, &pattern->val.ipv6.addr, &pattern->val.ipv6.mask)) {
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100431 /* no tree support right now */
432 pattern->type = SMP_T_IPV6;
433 return 1;
434 }
435 else {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100436 memprintf(err, "'%s' is not a valid IPv4 or IPv6 address", text);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100437 return 0;
438 }
439}
440
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100441/*
442 *
443 * These functions are exported and may be used by any other component.
444 *
445 * This fucntion just take a sample <smp> and check if this sample match
446 * with the pattern <pattern>. This fucntion return just PAT_MATCH or
447 * PAT_NOMATCH.
448 *
449 */
450
451/* always return false */
452enum pat_match_res pat_match_nothing(struct sample *smp, struct pattern *pattern)
453{
454 return PAT_NOMATCH;
455}
456
457
458/* NB: For two strings to be identical, it is required that their lengths match */
459enum pat_match_res pat_match_str(struct sample *smp, struct pattern *pattern)
460{
461 int icase;
462
463 if (pattern->len != smp->data.str.len)
464 return PAT_NOMATCH;
465
466 icase = pattern->flags & PAT_F_IGNORE_CASE;
467 if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) == 0) ||
468 (!icase && strncmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) == 0))
469 return PAT_MATCH;
470 return PAT_NOMATCH;
471}
472
473/* NB: For two binaries buf to be identical, it is required that their lengths match */
474enum pat_match_res pat_match_bin(struct sample *smp, struct pattern *pattern)
475{
476 if (pattern->len != smp->data.str.len)
477 return PAT_NOMATCH;
478
479 if (memcmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) == 0)
480 return PAT_MATCH;
481 return PAT_NOMATCH;
482}
483
484/* Executes a regex. It temporarily changes the data to add a trailing zero,
485 * and restores the previous character when leaving.
486 */
487enum pat_match_res pat_match_reg(struct sample *smp, struct pattern *pattern)
488{
489 if (regex_exec(pattern->ptr.reg, smp->data.str.str, smp->data.str.len) == 0)
490 return PAT_MATCH;
491 return PAT_NOMATCH;
492}
493
494/* Checks that the pattern matches the beginning of the tested string. */
495enum pat_match_res pat_match_beg(struct sample *smp, struct pattern *pattern)
496{
497 int icase;
498
499 if (pattern->len > smp->data.str.len)
500 return PAT_NOMATCH;
501
502 icase = pattern->flags & PAT_F_IGNORE_CASE;
503 if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str, pattern->len) != 0) ||
504 (!icase && strncmp(pattern->ptr.str, smp->data.str.str, pattern->len) != 0))
505 return PAT_NOMATCH;
506 return PAT_MATCH;
507}
508
509/* Checks that the pattern matches the end of the tested string. */
510enum pat_match_res pat_match_end(struct sample *smp, struct pattern *pattern)
511{
512 int icase;
513
514 if (pattern->len > smp->data.str.len)
515 return PAT_NOMATCH;
516 icase = pattern->flags & PAT_F_IGNORE_CASE;
517 if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str + smp->data.str.len - pattern->len, pattern->len) != 0) ||
518 (!icase && strncmp(pattern->ptr.str, smp->data.str.str + smp->data.str.len - pattern->len, pattern->len) != 0))
519 return PAT_NOMATCH;
520 return PAT_MATCH;
521}
522
523/* Checks that the pattern is included inside the tested string.
524 * NB: Suboptimal, should be rewritten using a Boyer-Moore method.
525 */
526enum pat_match_res pat_match_sub(struct sample *smp, struct pattern *pattern)
527{
528 int icase;
529 char *end;
530 char *c;
531
532 if (pattern->len > smp->data.str.len)
533 return PAT_NOMATCH;
534
535 end = smp->data.str.str + smp->data.str.len - pattern->len;
536 icase = pattern->flags & PAT_F_IGNORE_CASE;
537 if (icase) {
538 for (c = smp->data.str.str; c <= end; c++) {
539 if (tolower(*c) != tolower(*pattern->ptr.str))
540 continue;
541 if (strncasecmp(pattern->ptr.str, c, pattern->len) == 0)
542 return PAT_MATCH;
543 }
544 } else {
545 for (c = smp->data.str.str; c <= end; c++) {
546 if (*c != *pattern->ptr.str)
547 continue;
548 if (strncmp(pattern->ptr.str, c, pattern->len) == 0)
549 return PAT_MATCH;
550 }
551 }
552 return PAT_NOMATCH;
553}
554
555/* This one is used by other real functions. It checks that the pattern is
556 * included inside the tested string, but enclosed between the specified
557 * delimiters or at the beginning or end of the string. The delimiters are
558 * provided as an unsigned int made by make_4delim() and match up to 4 different
559 * delimiters. Delimiters are stripped at the beginning and end of the pattern.
560 */
561static int match_word(struct sample *smp, struct pattern *pattern, unsigned int delimiters)
562{
563 int may_match, icase;
564 char *c, *end;
565 char *ps;
566 int pl;
567
568 pl = pattern->len;
569 ps = pattern->ptr.str;
570
571 while (pl > 0 && is_delimiter(*ps, delimiters)) {
572 pl--;
573 ps++;
574 }
575
576 while (pl > 0 && is_delimiter(ps[pl - 1], delimiters))
577 pl--;
578
579 if (pl > smp->data.str.len)
580 return PAT_NOMATCH;
581
582 may_match = 1;
583 icase = pattern->flags & PAT_F_IGNORE_CASE;
584 end = smp->data.str.str + smp->data.str.len - pl;
585 for (c = smp->data.str.str; c <= end; c++) {
586 if (is_delimiter(*c, delimiters)) {
587 may_match = 1;
588 continue;
589 }
590
591 if (!may_match)
592 continue;
593
594 if (icase) {
595 if ((tolower(*c) == tolower(*ps)) &&
596 (strncasecmp(ps, c, pl) == 0) &&
597 (c == end || is_delimiter(c[pl], delimiters)))
598 return PAT_MATCH;
599 } else {
600 if ((*c == *ps) &&
601 (strncmp(ps, c, pl) == 0) &&
602 (c == end || is_delimiter(c[pl], delimiters)))
603 return PAT_MATCH;
604 }
605 may_match = 0;
606 }
607 return PAT_NOMATCH;
608}
609
610/* Checks that the pattern is included inside the tested string, but enclosed
611 * between the delimiters '?' or '/' or at the beginning or end of the string.
612 * Delimiters at the beginning or end of the pattern are ignored.
613 */
614enum pat_match_res pat_match_dir(struct sample *smp, struct pattern *pattern)
615{
616 return match_word(smp, pattern, make_4delim('/', '?', '?', '?'));
617}
618
619/* Checks that the pattern is included inside the tested string, but enclosed
620 * between the delmiters '/', '?', '.' or ":" or at the beginning or end of
621 * the string. Delimiters at the beginning or end of the pattern are ignored.
622 */
623enum pat_match_res pat_match_dom(struct sample *smp, struct pattern *pattern)
624{
625 return match_word(smp, pattern, make_4delim('/', '?', '.', ':'));
626}
627
628/* Checks that the integer in <test> is included between min and max */
629enum pat_match_res pat_match_int(struct sample *smp, struct pattern *pattern)
630{
631 if ((!pattern->val.range.min_set || pattern->val.range.min <= smp->data.uint) &&
632 (!pattern->val.range.max_set || smp->data.uint <= pattern->val.range.max))
633 return PAT_MATCH;
634 return PAT_NOMATCH;
635}
636
637/* Checks that the length of the pattern in <test> is included between min and max */
638enum pat_match_res pat_match_len(struct sample *smp, struct pattern *pattern)
639{
640 if ((!pattern->val.range.min_set || pattern->val.range.min <= smp->data.str.len) &&
641 (!pattern->val.range.max_set || smp->data.str.len <= pattern->val.range.max))
642 return PAT_MATCH;
643 return PAT_NOMATCH;
644}
645
646enum pat_match_res pat_match_ip(struct sample *smp, struct pattern *pattern)
647{
648 unsigned int v4; /* in network byte order */
649 struct in6_addr *v6;
650 int bits, pos;
651 struct in6_addr tmp6;
652
653 if (pattern->type == SMP_T_IPV4) {
654 if (smp->type == SMP_T_IPV4) {
655 v4 = smp->data.ipv4.s_addr;
656 }
657 else if (smp->type == SMP_T_IPV6) {
658 /* v4 match on a V6 sample. We want to check at least for
659 * the following forms :
660 * - ::ffff:ip:v4 (ipv4 mapped)
661 * - ::0000:ip:v4 (old ipv4 mapped)
662 * - 2002:ip:v4:: (6to4)
663 */
664 if (*(uint32_t*)&smp->data.ipv6.s6_addr[0] == 0 &&
665 *(uint32_t*)&smp->data.ipv6.s6_addr[4] == 0 &&
666 (*(uint32_t*)&smp->data.ipv6.s6_addr[8] == 0 ||
667 *(uint32_t*)&smp->data.ipv6.s6_addr[8] == htonl(0xFFFF))) {
668 v4 = *(uint32_t*)&smp->data.ipv6.s6_addr[12];
669 }
670 else if (*(uint16_t*)&smp->data.ipv6.s6_addr[0] == htons(0x2002)) {
671 v4 = htonl((ntohs(*(uint16_t*)&smp->data.ipv6.s6_addr[2]) << 16) +
672 ntohs(*(uint16_t*)&smp->data.ipv6.s6_addr[4]));
673 }
674 else
675 return PAT_NOMATCH;
676 }
677 else
678 return PAT_NOMATCH;
679
680 if (((v4 ^ pattern->val.ipv4.addr.s_addr) & pattern->val.ipv4.mask.s_addr) == 0)
681 return PAT_MATCH;
682 else
683 return PAT_NOMATCH;
684 }
685 else if (pattern->type == SMP_T_IPV6) {
686 if (smp->type == SMP_T_IPV4) {
687 /* Convert the IPv4 sample address to IPv4 with the
688 * mapping method using the ::ffff: prefix.
689 */
690 memset(&tmp6, 0, 10);
691 *(uint16_t*)&tmp6.s6_addr[10] = htons(0xffff);
692 *(uint32_t*)&tmp6.s6_addr[12] = smp->data.ipv4.s_addr;
693 v6 = &tmp6;
694 }
695 else if (smp->type == SMP_T_IPV6) {
696 v6 = &smp->data.ipv6;
697 }
698 else {
699 return PAT_NOMATCH;
700 }
701
702 bits = pattern->val.ipv6.mask;
703 for (pos = 0; bits > 0; pos += 4, bits -= 32) {
704 v4 = *(uint32_t*)&v6->s6_addr[pos] ^ *(uint32_t*)&pattern->val.ipv6.addr.s6_addr[pos];
705 if (bits < 32)
706 v4 &= htonl((~0U) << (32-bits));
707 if (v4)
708 return PAT_NOMATCH;
709 }
710 return PAT_MATCH;
711 }
712 return PAT_NOMATCH;
713}
714
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100715/* NB: does nothing if <pat> is NULL */
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +0100716void pattern_free(struct pattern_list *pat)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100717{
718 if (!pat)
719 return;
720
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +0100721 if (pat->pat.ptr.ptr) {
722 if (pat->pat.freeptrbuf)
723 pat->pat.freeptrbuf(pat->pat.ptr.ptr);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100724
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +0100725 free(pat->pat.ptr.ptr);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100726 }
727
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +0100728 free(pat->pat.smp);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100729 free(pat);
730}
731
732void free_pattern_list(struct list *head)
733{
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +0100734 struct pattern_list *pat, *tmp;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100735 list_for_each_entry_safe(pat, tmp, head, list)
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100736 pattern_free(pat);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100737}
738
739void free_pattern_tree(struct eb_root *root)
740{
741 struct eb_node *node, *next;
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +0100742 struct pattern_tree *elt;
Thierry FOURNIER3ce88c72013-12-09 11:29:46 +0100743
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100744 node = eb_first(root);
745 while (node) {
746 next = eb_next(node);
747 eb_delete(node);
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +0100748 elt = container_of(node, struct pattern_tree, node);
Thierry FOURNIERc64de3f2013-12-10 15:08:39 +0100749 free(elt->smp);
Thierry FOURNIER3ce88c72013-12-09 11:29:46 +0100750 free(elt);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100751 node = next;
752 }
753}
754
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100755void pattern_prune_expr(struct pattern_expr *expr)
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +0100756{
757 free_pattern_list(&expr->patterns);
758 free_pattern_tree(&expr->pattern_tree);
759 LIST_INIT(&expr->patterns);
760}
761
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100762void pattern_init_expr(struct pattern_expr *expr)
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +0100763{
764 LIST_INIT(&expr->patterns);
765 expr->pattern_tree = EB_ROOT_UNIQUE;
766}
767
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100768/*
769 *
770 * The following functions are used for the pattern indexation
771 *
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100772 */
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100773
774int pat_idx_list_val(struct pattern_expr *expr, struct pattern *pat, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100775{
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100776 struct pattern_list *patl;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100777
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100778 /* allocate pattern */
779 patl = calloc(1, sizeof(*patl));
780 if (!patl) {
781 memprintf(err, "out of memory while indexing pattern");
Thierry FOURNIER972028f2014-01-23 17:53:31 +0100782 return 0;
783 }
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100784
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100785 /* duplicate pattern */
786 memcpy(&patl->pat, pat, sizeof(*pat));
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100787
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100788 /* chain pattern in the expression */
789 LIST_ADDQ(&expr->patterns, &patl->list);
790
791 /* that's ok */
792 return 1;
793}
794
795int pat_idx_list_ptr(struct pattern_expr *expr, struct pattern *pat, char **err)
796{
797 struct pattern_list *patl;
798
799 /* allocate pattern */
800 patl = calloc(1, sizeof(*patl));
801 if (!patl)
Thierry FOURNIER972028f2014-01-23 17:53:31 +0100802 return 0;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100803
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100804 /* duplicate pattern */
805 memcpy(&patl->pat, pat, sizeof(*pat));
806 patl->pat.ptr.ptr = malloc(patl->pat.len);
807 if (!patl->pat.ptr.ptr) {
808 free(patl);
809 memprintf(err, "out of memory while indexing pattern");
810 return 0;
811 }
812 memcpy(patl->pat.ptr.ptr, pat->ptr.ptr, pat->len);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100813
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100814 /* chain pattern in the expression */
815 LIST_ADDQ(&expr->patterns, &patl->list);
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100816
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100817 /* that's ok */
818 return 1;
819}
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100820
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100821int pat_idx_list_str(struct pattern_expr *expr, struct pattern *pat, char **err)
822{
823 struct pattern_list *patl;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100824
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100825 /* allocate pattern */
826 patl = calloc(1, sizeof(*patl));
827 if (!patl) {
828 memprintf(err, "out of memory while indexing pattern");
829 return 0;
830 }
831
832 /* duplicate pattern */
833 memcpy(&patl->pat, pat, sizeof(*pat));
834 patl->pat.ptr.str = malloc(patl->pat.len + 1);
835 if (!patl->pat.ptr.str) {
836 free(patl);
837 memprintf(err, "out of memory while indexing pattern");
838 return 0;
839 }
840 memcpy(patl->pat.ptr.ptr, pat->ptr.ptr, pat->len);
841 patl->pat.ptr.str[patl->pat.len] = '\0';
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100842
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100843 /* chain pattern in the expression */
844 LIST_ADDQ(&expr->patterns, &patl->list);
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100845
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100846 /* that's ok */
847 return 1;
848}
849
850int pat_idx_list_reg(struct pattern_expr *expr, struct pattern *pat, char **err)
851{
852 struct pattern_list *patl;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100853
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100854 /* allocate pattern */
855 patl = calloc(1, sizeof(*patl));
856 if (!patl) {
857 memprintf(err, "out of memory while indexing pattern");
858 return 0;
Thierry FOURNIER972028f2014-01-23 17:53:31 +0100859 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100860
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100861 /* duplicate pattern */
862 memcpy(&patl->pat, pat, sizeof(*pat));
863
864 /* allocate regex */
865 patl->pat.ptr.reg = calloc(1, sizeof(*patl->pat.ptr.reg));
866 if (!patl->pat.ptr.reg) {
867 free(patl);
868 memprintf(err, "out of memory while indexing pattern");
869 return 0;
870 }
871
872 /* compile regex */
873 if (!regex_comp(pat->ptr.reg->regstr, patl->pat.ptr.reg, !(patl->pat.flags & PAT_F_IGNORE_CASE), 0, err)) {
874 free(patl);
875 free(patl->pat.ptr.reg);
876 return 0;
877 }
878
879 /* free pattern method */
880 patl->pat.freeptrbuf = &pat_free_reg;
881
882 /* chain pattern in the expression */
883 LIST_ADDQ(&expr->patterns, &patl->list);
884
885 /* that's ok */
886 return 1;
887}
888
889int pat_idx_tree_ip(struct pattern_expr *expr, struct pattern *pat, char **err)
890{
891 unsigned int mask;
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +0100892 struct pattern_tree *node;
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100893
894 /* Only IPv4 can be indexed */
895 if (pat->type == SMP_T_IPV4) {
Thierry FOURNIER972028f2014-01-23 17:53:31 +0100896 /* in IPv4 case, check if the mask is contiguous so that we can
897 * insert the network into the tree. A continuous mask has only
898 * ones on the left. This means that this mask + its lower bit
899 * added once again is null.
900 */
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100901 mask = ntohl(pat->val.ipv4.mask.s_addr);
902 if (mask + (mask & -mask) == 0) {
903 mask = mask ? 33 - flsnz(mask & -mask) : 0; /* equals cidr value */
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100904
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100905 /* node memory allocation */
906 node = calloc(1, sizeof(*node) + 4);
907 if (!node) {
908 memprintf(err, "out of memory while loading pattern");
909 return 0;
910 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100911
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100912 /* copy the pointer to sample associated to this node */
913 node->smp = pat->smp;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100914
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100915 /* FIXME: insert <addr>/<mask> into the tree here */
916 memcpy(node->node.key, &pat->val.ipv4.addr, 4); /* network byte order */
917 node->node.node.pfx = mask;
918 if (ebmb_insert_prefix(&expr->pattern_tree, &node->node, 4) != &node->node)
919 free(node); /* was a duplicate */
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100920
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100921 /* that's ok */
922 return 1;
923 }
924 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100925
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100926 /* If the value cannot be indexed, just add it to the list */
927 return pat_idx_list_val(expr, pat, err);
928}
929
930int pat_idx_tree_str(struct pattern_expr *expr, struct pattern *pat, char **err)
931{
932 int len;
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +0100933 struct pattern_tree *node;
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100934
935 /* Only string can be indexed */
936 if (pat->type != SMP_T_CSTR && pat->type != SMP_T_STR) {
937 memprintf(err, "internal error: string expected, but the type is '%s'",
938 smp_to_type[pat->type]);
939 return 0;
Thierry FOURNIER972028f2014-01-23 17:53:31 +0100940 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100941
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100942 /* If the flag PAT_F_IGNORE_CASE is set, we cannot use trees */
943 if (pat->flags & PAT_F_IGNORE_CASE)
944 return pat_idx_list_str(expr, pat, err);
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100945
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100946 /* Process the key len */
947 len = strlen(pat->ptr.str) + 1;
948
949 /* node memory allocation */
950 node = calloc(1, sizeof(*node) + len);
951 if (!node) {
952 memprintf(err, "out of memory while loading pattern");
953 return 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100954 }
955
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100956 /* copy the pointer to sample associated to this node */
957 node->smp = pat->smp;
958
959 /* copy the string */
960 memcpy(node->node.key, pat->ptr.str, len);
961
962 /* index the new node */
963 if (ebst_insert(&expr->pattern_tree, &node->node) != &node->node)
964 free(node); /* was a duplicate */
965
966 /* that's ok */
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100967 return 1;
968}
969
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100970/* return 1 if the process is ok
971 * return -1 if the parser fail. The err message is filled.
972 * return -2 if out of memory
973 */
974int pattern_register(struct pattern_expr *expr, const char *arg,
975 struct sample_storage *smp,
976 int patflags, char **err)
977{
978 int ret;
979 struct pattern pattern;
980
981 /* initialise pattern */
982 memset(&pattern, 0, sizeof(pattern));
983 pattern.flags = patflags;
984 pattern.smp = smp;
985
986 /* parse pattern */
Thierry FOURNIERedc15c32013-12-13 15:36:59 +0100987 ret = expr->parse(arg, &pattern, err);
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100988 if (!ret)
989 return 0;
990
991 /* index pattern */
992 if (!expr->index(expr, &pattern, err))
993 return 0;
994
995 return 1;
996}
997
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100998/* Reads patterns from a file. If <err_msg> is non-NULL, an error message will
999 * be returned there on errors and the caller will have to free it.
1000 */
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01001001int pattern_read_from_file(struct pattern_expr *expr,
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001002 const char *filename, int patflags,
1003 char **err)
1004{
1005 FILE *file;
1006 char *c;
1007 char *arg;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001008 int ret = 0;
1009 int line = 0;
1010 int code;
1011
1012 file = fopen(filename, "r");
1013 if (!file) {
1014 memprintf(err, "failed to open pattern file <%s>", filename);
1015 return 0;
1016 }
1017
1018 /* now parse all patterns. The file may contain only one pattern per
1019 * line. If the line contains spaces, they will be part of the pattern.
1020 * The pattern stops at the first CR, LF or EOF encountered.
1021 */
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001022 while (fgets(trash.str, trash.size, file) != NULL) {
1023 line++;
1024 c = trash.str;
1025
1026 /* ignore lines beginning with a dash */
1027 if (*c == '#')
1028 continue;
1029
1030 /* strip leading spaces and tabs */
1031 while (*c == ' ' || *c == '\t')
1032 c++;
1033
1034
1035 arg = c;
1036 while (*c && *c != '\n' && *c != '\r')
1037 c++;
1038 *c = 0;
1039
1040 /* empty lines are ignored too */
1041 if (c == arg)
1042 continue;
1043
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001044 code = pattern_register(expr, arg, NULL, patflags, err);
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001045 if (code == -2) {
1046 memprintf(err, "out of memory when loading patterns from file <%s>", filename);
1047 goto out_close;
1048 }
1049 else if (code < 0) {
1050 memprintf(err, "%s when loading patterns from file <%s>", *err, filename);
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001051 goto out_close;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001052 }
1053 }
1054
1055 ret = 1; /* success */
1056
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001057 out_close:
1058 fclose(file);
1059 return ret;
1060}
1061
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01001062/* This function matches a sample <smp> against a set of patterns presented in
1063 * pattern expression <expr>. Upon success, if <sample> is not NULL, it is fed
1064 * with the pointer associated with the matching pattern. This function returns
1065 * PAT_NOMATCH or PAT_MATCH.
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001066 */
Willy Tarreau0cba6072013-11-28 22:21:02 +01001067enum pat_match_res pattern_exec_match(struct pattern_expr *expr, struct sample *smp,
Thierry FOURNIER76090642013-12-10 15:03:38 +01001068 struct sample_storage **sample,
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +01001069 struct pattern **pat, struct pattern_tree **idx_elt)
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001070{
Willy Tarreau0cba6072013-11-28 22:21:02 +01001071 enum pat_match_res pat_res = PAT_NOMATCH;
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001072 struct pattern_list *pattern;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001073 struct ebmb_node *node = NULL;
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +01001074 struct pattern_tree *elt;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001075
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01001076 if (expr->match == pat_match_nothing) {
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001077 if (smp->data.uint)
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01001078 pat_res |= PAT_MATCH;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001079 else
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01001080 pat_res |= PAT_NOMATCH;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001081 }
1082 else if (!expr->match) {
1083 /* just check for existence */
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01001084 pat_res |= PAT_MATCH;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001085 }
1086 else {
1087 if (!eb_is_empty(&expr->pattern_tree)) {
1088 /* a tree is present, let's check what type it is */
Thierry FOURNIERe3ded592013-12-06 15:36:54 +01001089 if (expr->match == pat_match_str) {
1090 if (sample_convert(smp, SMP_T_STR))
1091 node = pat_lookup_str(smp, expr);
1092 }
1093 else if (expr->match == pat_match_ip) {
1094 if (sample_convert(smp, SMP_T_IPV4))
1095 node = pat_lookup_ip(smp, expr);
1096 }
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001097 if (node) {
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01001098 pat_res |= PAT_MATCH;
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +01001099 elt = ebmb_entry(node, struct pattern_tree, node);
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001100 if (sample)
1101 *sample = elt->smp;
Thierry FOURNIER76090642013-12-10 15:03:38 +01001102 if (idx_elt)
1103 *idx_elt = elt;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001104 }
1105 }
1106
1107 /* call the match() function for all tests on this value */
1108 list_for_each_entry(pattern, &expr->patterns, list) {
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01001109 if (pat_res == PAT_MATCH)
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001110 break;
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001111 if (sample_convert(smp, pattern->pat.expect_type))
1112 pat_res |= expr->match(smp, &pattern->pat);
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001113 if (sample)
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001114 *sample = pattern->pat.smp;
Thierry FOURNIER76090642013-12-10 15:03:38 +01001115 if (pat)
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001116 *pat = &pattern->pat;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001117 }
1118 }
1119
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01001120 return pat_res;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001121}
1122
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001123/* This function browse the pattern expr <expr> to lookup the key <key>. On
1124 * error it returns 0. On success, it returns 1 and fills either <pat_elt>
1125 * or <idx_elt> with the respectively matched pointers, and the other one with
1126 * NULL. Pointers are not set if they're passed as NULL.
1127 */
1128int pattern_lookup(const char *key, struct pattern_expr *expr,
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +01001129 struct pattern_list **pat_elt, struct pattern_tree **idx_elt, char **err)
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001130{
1131 struct pattern pattern;
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001132 struct pattern_list *pat;
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001133 struct ebmb_node *node;
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +01001134 struct pattern_tree *elt;
Willy Tarreau668ae532013-12-15 16:42:26 +01001135 unsigned int mask = 0;
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001136
1137 /* no real pattern */
1138 if (!expr->match || expr->match == pat_match_nothing)
1139 return 0;
1140
1141 /* build lookup pattern */
Thierry FOURNIERedc15c32013-12-13 15:36:59 +01001142 if (!expr->parse(key, &pattern, NULL))
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001143 return 0;
1144
1145 pat = NULL;
1146 elt = NULL;
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001147
Thierry FOURNIERc64de3f2013-12-10 15:08:39 +01001148 /* Try to look up the tree first. IPv6 is not indexed */
1149 if (!eb_is_empty(&expr->pattern_tree) && pattern.type != SMP_T_IPV6) {
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001150 /* Check the pattern type */
1151 if (pattern.type != SMP_T_STR &&
1152 pattern.type != SMP_T_CSTR &&
1153 pattern.type != SMP_T_IPV4) {
1154 memprintf(err, "Unexpected pattern type.");
1155 return 0;
1156 }
1157
1158 /* Convert mask. If the mask is not contiguous, ignore the lookup
1159 * in the tree, and browse the list.
1160 */
1161 if (expr->match == pat_match_ip) {
1162 mask = ntohl(pattern.val.ipv4.mask.s_addr);
1163 if (mask + (mask & -mask) != 0)
1164 goto browse_list;
1165 mask = mask ? 33 - flsnz(mask & -mask) : 0; /* equals cidr value */
1166 }
1167
1168 /* browse each node of the tree, and check string */
1169 if (expr->match == pat_match_str) {
1170 for (node = ebmb_first(&expr->pattern_tree);
1171 node;
1172 node = ebmb_next(node)) {
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +01001173 elt = container_of(node, struct pattern_tree, node);
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001174 if (strcmp(pattern.ptr.str, (char *)elt->node.key) == 0)
1175 goto found;
1176 }
1177 }
1178 else if (expr->match == pat_match_ip) {
1179 for (node = ebmb_first(&expr->pattern_tree);
1180 node;
1181 node = ebmb_next(node)) {
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +01001182 elt = container_of(node, struct pattern_tree, node);
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001183 if (elt->node.node.pfx == mask &&
1184 memcmp(&pattern.val.ipv4.addr.s_addr, elt->node.key, 4) == 0)
1185 goto found;
1186 }
1187 }
1188 }
1189
1190browse_list:
Thierry FOURNIERc64de3f2013-12-10 15:08:39 +01001191 elt = NULL;
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001192 if (expr->parse == pat_parse_int ||
1193 expr->parse == pat_parse_len) {
1194 list_for_each_entry(pat, &expr->patterns, list) {
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001195 if (pat->pat.flags & PAT_F_TREE)
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001196 continue;
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001197 if (pattern.val.range.min_set != pat->pat.val.range.min_set)
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001198 continue;
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001199 if (pattern.val.range.max_set != pat->pat.val.range.max_set)
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001200 continue;
1201 if (pattern.val.range.min_set &&
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001202 pattern.val.range.min != pat->pat.val.range.min)
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001203 continue;
1204 if (pattern.val.range.max_set &&
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001205 pattern.val.range.max != pat->pat.val.range.max)
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001206 continue;
1207 goto found;
1208 }
1209 }
1210 else if (expr->parse == pat_parse_ip) {
1211 list_for_each_entry(pat, &expr->patterns, list) {
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001212 if (pat->pat.flags & PAT_F_TREE)
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001213 continue;
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001214 if (pattern.type != pat->pat.type)
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001215 continue;
1216 if (pattern.type == SMP_T_IPV4 &&
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001217 memcmp(&pattern.val.ipv4.addr, &pat->pat.val.ipv4.addr, sizeof(pat->pat.val.ipv4.addr)) != 0)
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001218 continue;
1219 if (pattern.type == SMP_T_IPV4 &&
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001220 memcmp(&pattern.val.ipv4.mask, &pat->pat.val.ipv4.mask, sizeof(pat->pat.val.ipv4.addr)) != 0)
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001221 continue;
1222 if (pattern.type == SMP_T_IPV6 &&
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001223 memcmp(&pattern.val.ipv6.addr, &pat->pat.val.ipv6.addr, sizeof(pat->pat.val.ipv6.addr)) != 0)
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001224 continue;
1225 if (pattern.type == SMP_T_IPV6 &&
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001226 pattern.val.ipv6.mask != pat->pat.val.ipv6.mask)
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001227 continue;
1228 goto found;
1229 }
1230 }
1231 else if (expr->parse == pat_parse_str) {
1232 list_for_each_entry(pat, &expr->patterns, list) {
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001233 if (pat->pat.flags & PAT_F_TREE)
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001234 continue;
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001235 if (pattern.len != pat->pat.len)
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001236 continue;
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001237 if (pat->pat.flags & PAT_F_IGNORE_CASE) {
1238 if (strncasecmp(pattern.ptr.str, pat->pat.ptr.str, pat->pat.len) != 0)
Thierry FOURNIER35249cb2014-01-14 13:38:40 +01001239 continue;
1240 }
1241 else {
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001242 if (strncmp(pattern.ptr.str, pat->pat.ptr.str, pat->pat.len) != 0)
Thierry FOURNIER35249cb2014-01-14 13:38:40 +01001243 continue;
1244 }
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001245 goto found;
1246 }
1247 }
1248 else if (expr->parse == pat_parse_bin) {
1249 list_for_each_entry(pat, &expr->patterns, list) {
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001250 if (pat->pat.flags & PAT_F_TREE)
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001251 continue;
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001252 if (pattern.len != pat->pat.len)
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001253 continue;
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001254 if (memcmp(pattern.ptr.ptr, pat->pat.ptr.ptr, pat->pat.len) != 0)
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001255 continue;
1256 goto found;
1257 }
1258 }
1259 else if (expr->parse == pat_parse_reg) {
1260 list_for_each_entry(pat, &expr->patterns, list) {
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001261 if (pat->pat.flags & PAT_F_TREE)
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001262 continue;
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001263 if (pat->pat.flags & PAT_F_IGNORE_CASE) {
1264 if (strcasecmp(pattern.ptr.reg->regstr, pat->pat.ptr.reg->regstr) != 0)
Thierry FOURNIER35249cb2014-01-14 13:38:40 +01001265 continue;
1266 }
1267 else {
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001268 if (strcmp(pattern.ptr.reg->regstr, pat->pat.ptr.reg->regstr) != 0)
Thierry FOURNIER35249cb2014-01-14 13:38:40 +01001269 continue;
1270 }
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001271 goto found;
1272 }
1273 }
1274
1275 /* if we get there, we didn't find the pattern */
1276 return 0;
1277found:
1278 if (idx_elt)
1279 *idx_elt = elt;
1280
1281 if (pat_elt)
1282 *pat_elt = pat;
1283
1284 return 1;
1285}