blob: 30fdcc6c1f8c4bb728a71f0eb9c3f966342aa61a [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 FOURNIER580c32c2014-01-24 10:58:12 +010043int (*pat_parse_fcts[PAT_MATCH_NUM])(const char *, struct pattern *, enum pat_usage, 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
Willy Tarreau0cba6072013-11-28 22:21:02 +010059enum pat_match_res (*pat_match_fcts[PAT_MATCH_NUM])(struct sample *, struct pattern *) = {
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010060 [PAT_MATCH_FOUND] = NULL,
61 [PAT_MATCH_BOOL] = pat_match_nothing,
62 [PAT_MATCH_INT] = pat_match_int,
63 [PAT_MATCH_IP] = pat_match_ip,
64 [PAT_MATCH_BIN] = pat_match_bin,
65 [PAT_MATCH_LEN] = pat_match_len,
66 [PAT_MATCH_STR] = pat_match_str,
67 [PAT_MATCH_BEG] = pat_match_beg,
68 [PAT_MATCH_SUB] = pat_match_sub,
69 [PAT_MATCH_DIR] = pat_match_dir,
70 [PAT_MATCH_DOM] = pat_match_dom,
71 [PAT_MATCH_END] = pat_match_end,
72 [PAT_MATCH_REG] = pat_match_reg,
Thierry FOURNIERed66c292013-11-28 11:05:19 +010073};
74
Thierry FOURNIERe3ded592013-12-06 15:36:54 +010075/* Just used for checking configuration compatibility */
76int pat_match_types[PAT_MATCH_NUM] = {
77 [PAT_MATCH_FOUND] = SMP_T_UINT,
78 [PAT_MATCH_BOOL] = SMP_T_UINT,
79 [PAT_MATCH_INT] = SMP_T_UINT,
80 [PAT_MATCH_IP] = SMP_T_ADDR,
81 [PAT_MATCH_BIN] = SMP_T_CBIN,
82 [PAT_MATCH_LEN] = SMP_T_CSTR,
83 [PAT_MATCH_STR] = SMP_T_CSTR,
84 [PAT_MATCH_BEG] = SMP_T_CSTR,
85 [PAT_MATCH_SUB] = SMP_T_CSTR,
86 [PAT_MATCH_DIR] = SMP_T_CSTR,
87 [PAT_MATCH_DOM] = SMP_T_CSTR,
88 [PAT_MATCH_END] = SMP_T_CSTR,
89 [PAT_MATCH_REG] = SMP_T_CSTR,
90};
91
Thierry FOURNIERed66c292013-11-28 11:05:19 +010092/*
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +010093 *
94 * The following functions are not exported and are used by internals process
95 * of pattern matching
96 *
Thierry FOURNIERed66c292013-11-28 11:05:19 +010097 */
98
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +010099/* Lookup an IPv4 address in the expression's pattern tree using the longest
100 * match method. The node is returned if it exists, otherwise NULL.
101 */
102static void *pat_lookup_ip(struct sample *smp, struct pattern_expr *expr)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100103{
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100104 struct in_addr *s;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100105
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100106 if (smp->type != SMP_T_IPV4)
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100107 return PAT_NOMATCH;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100108
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100109 s = &smp->data.ipv4;
110 return ebmb_lookup_longest(&expr->pattern_tree, &s->s_addr);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100111}
112
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100113/* Free data allocated by pat_parse_reg */
114static void pat_free_reg(void *ptr)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100115{
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100116 regex_free(ptr);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100117}
118
119/* Lookup a string in the expression's pattern tree. The node is returned if it
120 * exists, otherwise NULL.
121 */
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100122static void *pat_lookup_str(struct sample *smp, struct pattern_expr *expr)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100123{
124 /* data are stored in a tree */
125 struct ebmb_node *node;
126 char prev;
127
128 /* we may have to force a trailing zero on the test pattern */
129 prev = smp->data.str.str[smp->data.str.len];
130 if (prev)
131 smp->data.str.str[smp->data.str.len] = '\0';
132 node = ebst_lookup(&expr->pattern_tree, smp->data.str.str);
133 if (prev)
134 smp->data.str.str[smp->data.str.len] = prev;
135 return node;
136}
137
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100138/* Background: Fast way to find a zero byte in a word
139 * http://graphics.stanford.edu/~seander/bithacks.html#ZeroInWord
140 * hasZeroByte = (v - 0x01010101UL) & ~v & 0x80808080UL;
141 *
142 * To look for 4 different byte values, xor the word with those bytes and
143 * then check for zero bytes:
144 *
145 * v = (((unsigned char)c * 0x1010101U) ^ delimiter)
146 * where <delimiter> is the 4 byte values to look for (as an uint)
147 * and <c> is the character that is being tested
148 */
149static inline unsigned int is_delimiter(unsigned char c, unsigned int mask)
150{
151 mask ^= (c * 0x01010101); /* propagate the char to all 4 bytes */
152 return (mask - 0x01010101) & ~mask & 0x80808080U;
153}
154
155static inline unsigned int make_4delim(unsigned char d1, unsigned char d2, unsigned char d3, unsigned char d4)
156{
157 return d1 << 24 | d2 << 16 | d3 << 8 | d4;
158}
159
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100160
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100161/*
162 *
163 * These functions are exported and may be used by any other component.
164 *
165 * The following functions are used for parsing pattern matching
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100166 * input value. The <text> contain the string to be parsed. <pattern>
167 * must be a preallocated pattern. The pat_parse_* functions fill this
168 * structure with the parsed value. <usage> can be PAT_U_COMPILE or
169 * PAT_U_LOOKUP. If the value PAT_U_COMPILE is used memory is allocated
170 * for filling the pattern. If the value PAT_U_LOOKUP is set, the parser
171 * use "trash" or return pointers to the input strings. In both cases,
172 * the caller must use the value PAT_U_LOOKUP with caution. <err> is
173 * filled with an error message built with memprintf() function.
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100174 *
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100175 * In succes case, the pat_parse_* function return 1. If the function
176 * fail, it returns 0 and <err> is filled.
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100177 *
178 */
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100179
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100180/* ignore the current line */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100181int pat_parse_nothing(const char *text, struct pattern *pattern, enum pat_usage usage, char **err)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100182{
183 return 1;
184}
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100185
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100186/* Parse a string. It is allocated and duplicated. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100187int pat_parse_str(const char *text, struct pattern *pattern, enum pat_usage usage, char **err)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100188{
189 pattern->type = SMP_T_CSTR;
190 pattern->expect_type = SMP_T_CSTR;
191 if (usage == PAT_U_COMPILE) {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100192 pattern->ptr.str = strdup(text);
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100193 if (!pattern->ptr.str) {
194 memprintf(err, "out of memory while loading string pattern");
195 return 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100196 }
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100197 }
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100198 else
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100199 pattern->ptr.str = (char *)text;
200 pattern->len = strlen(text);
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100201 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100202}
203
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100204/* Parse a binary written in hexa. It is allocated. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100205int pat_parse_bin(const char *text, struct pattern *pattern, enum pat_usage usage, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100206{
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100207 struct chunk *trash;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100208
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100209 pattern->type = SMP_T_CBIN;
210 pattern->expect_type = SMP_T_CBIN;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100211
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100212 if (usage == PAT_U_COMPILE)
213 /* If the parse_binary fails, it returns 0. In succes case, it returns
214 * the length of the arsed binary content. The functions pat_parse_*
215 * must return 0 if fail and the number of elements eated from **text
216 * if not fail. In succes case, this function eat always 1 elements.
217 * The double operator "!" converts the range "1-n" to "1".
218 */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100219 return !!parse_binary(text, &pattern->ptr.str, &pattern->len, err);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100220
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100221 trash = get_trash_chunk();
222 pattern->len = trash->size;
223 pattern->ptr.str = trash->str;
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100224 return !!parse_binary(text, &pattern->ptr.str, &pattern->len, err);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100225}
226
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100227/* Parse a regex. It is allocated. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100228int pat_parse_reg(const char *text, struct pattern *pattern, enum pat_usage usage, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100229{
Thierry FOURNIER799c0422013-12-06 20:36:20 +0100230 struct my_regex *preg;
Thierry FOURNIER0b2fe4a2013-12-06 20:33:50 +0100231 struct chunk *trash;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100232
Thierry FOURNIER0b2fe4a2013-12-06 20:33:50 +0100233 if (usage == PAT_U_COMPILE) {
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100234
Thierry FOURNIER0b2fe4a2013-12-06 20:33:50 +0100235 preg = calloc(1, sizeof(*preg));
236 if (!preg) {
237 memprintf(err, "out of memory while loading pattern");
238 return 0;
239 }
240
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100241 if (!regex_comp(text, preg, !(pattern->flags & PAT_F_IGNORE_CASE), 0, err)) {
Thierry FOURNIER0b2fe4a2013-12-06 20:33:50 +0100242 free(preg);
243 return 0;
244 }
245 pattern->freeptrbuf = &pat_free_reg;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100246 }
Thierry FOURNIER0b2fe4a2013-12-06 20:33:50 +0100247 else {
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100248
Thierry FOURNIER0b2fe4a2013-12-06 20:33:50 +0100249 trash = get_trash_chunk();
250 if (trash->size < sizeof(*preg)) {
251 memprintf(err, "no space avalaible in the buffer. expect %d, provides %d",
252 (int)sizeof(*preg), trash->size);
253 return 0;
254 }
255
256 preg = (struct my_regex *)trash->str;
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100257 preg->regstr = (char *)text;
Thierry FOURNIER0b2fe4a2013-12-06 20:33:50 +0100258 pattern->freeptrbuf = NULL;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100259 }
260
261 pattern->ptr.reg = preg;
Thierry FOURNIERcc0e0b32013-12-06 16:56:40 +0100262 pattern->expect_type = SMP_T_CSTR;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100263 return 1;
264}
265
266/* Parse a range of positive integers delimited by either ':' or '-'. If only
267 * one integer is read, it is set as both min and max. An operator may be
268 * specified as the prefix, among this list of 5 :
269 *
270 * 0:eq, 1:gt, 2:ge, 3:lt, 4:le
271 *
272 * The default operator is "eq". It supports range matching. Ranges are
273 * rejected for other operators. The operator may be changed at any time.
274 * The operator is stored in the 'opaque' argument.
275 *
276 * If err is non-NULL, an error message will be returned there on errors and
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100277 * the caller will have to free it. The function returns zero on error, and
278 * non-zero on success.
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100279 *
280 */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100281int pat_parse_int(const char *text, struct pattern *pattern, enum pat_usage usage, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100282{
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100283 const char *ptr = text;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100284
285 pattern->type = SMP_T_UINT;
Thierry FOURNIERcc0e0b32013-12-06 16:56:40 +0100286 pattern->expect_type = SMP_T_UINT;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100287
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100288 /* Empty string is not valid */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100289 if (!*text)
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100290 goto not_valid_range;
291
292 /* Search ':' or '-' separator. */
293 while (*ptr != '\0' && *ptr != ':' && *ptr != '-')
294 ptr++;
295
296 /* If separator not found. */
297 if (!*ptr) {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100298 if (strl2llrc(text, ptr - text, &pattern->val.range.min) != 0) {
299 memprintf(err, "'%s' is not a number", text);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100300 return 0;
301 }
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100302 pattern->val.range.max = pattern->val.range.min;
303 pattern->val.range.min_set = 1;
304 pattern->val.range.max_set = 1;
305 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100306 }
307
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100308 /* If the separator is the first character. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100309 if (ptr == text && *(ptr + 1) != '\0') {
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100310 if (strl2llrc(ptr + 1, strlen(ptr + 1), &pattern->val.range.max) != 0)
311 goto not_valid_range;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100312
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100313 pattern->val.range.min_set = 0;
314 pattern->val.range.max_set = 1;
315 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100316 }
317
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100318 /* If separator is the last character. */
319 if (*(ptr + 1) == '\0') {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100320 if (strl2llrc(text, ptr - text, &pattern->val.range.min) != 0)
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100321 goto not_valid_range;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100322
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100323 pattern->val.range.min_set = 1;
324 pattern->val.range.max_set = 0;
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100325 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100326 }
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100327
328 /* Else, parse two numbers. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100329 if (strl2llrc(text, ptr - text, &pattern->val.range.min) != 0)
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100330 goto not_valid_range;
331
332 if (strl2llrc(ptr + 1, strlen(ptr + 1), &pattern->val.range.max) != 0)
333 goto not_valid_range;
334
335 if (pattern->val.range.min > pattern->val.range.max)
336 goto not_valid_range;
337
338 pattern->val.range.min_set = 1;
339 pattern->val.range.max_set = 1;
340 return 1;
341
342 not_valid_range:
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100343 memprintf(err, "'%s' is not a valid number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100344 return 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100345}
346
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100347int pat_parse_len(const char *text, struct pattern *pattern, enum pat_usage usage, char **err)
Thierry FOURNIERcc0e0b32013-12-06 16:56:40 +0100348{
349 int ret;
350
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100351 ret = pat_parse_int(text, pattern, usage, err);
Thierry FOURNIERcc0e0b32013-12-06 16:56:40 +0100352 pattern->expect_type = SMP_T_CSTR;
353 return ret;
354}
355
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100356/* Parse a range of positive 2-component versions delimited by either ':' or
357 * '-'. The version consists in a major and a minor, both of which must be
358 * smaller than 65536, because internally they will be represented as a 32-bit
359 * integer.
360 * If only one version is read, it is set as both min and max. Just like for
361 * pure integers, an operator may be specified as the prefix, among this list
362 * of 5 :
363 *
364 * 0:eq, 1:gt, 2:ge, 3:lt, 4:le
365 *
366 * The default operator is "eq". It supports range matching. Ranges are
367 * rejected for other operators. The operator may be changed at any time.
368 * The operator is stored in the 'opaque' argument. This allows constructs
369 * such as the following one :
370 *
371 * acl obsolete_ssl ssl_req_proto lt 3
372 * acl unsupported_ssl ssl_req_proto gt 3.1
373 * acl valid_ssl ssl_req_proto 3.0-3.1
374 *
375 */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100376int pat_parse_dotted_ver(const char *text, struct pattern *pattern, enum pat_usage usage, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100377{
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100378 const char *ptr = text;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100379
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100380 pattern->type = SMP_T_UINT;
381 pattern->expect_type = SMP_T_UINT;
382
383 /* Search ':' or '-' separator. */
384 while (*ptr != '\0' && *ptr != ':' && *ptr != '-')
385 ptr++;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100386
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100387 /* If separator not found. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100388 if (*ptr == '\0' && ptr > text) {
389 if (strl2llrc_dotted(text, ptr-text, &pattern->val.range.min) != 0) {
390 memprintf(err, "'%s' is not a dotted number", text);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100391 return 0;
392 }
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100393 pattern->val.range.max = pattern->val.range.min;
394 pattern->val.range.min_set = 1;
395 pattern->val.range.max_set = 1;
396 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100397 }
398
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100399 /* If the separator is the first character. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100400 if (ptr == text && *(ptr+1) != '\0') {
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100401 if (strl2llrc_dotted(ptr+1, strlen(ptr+1), &pattern->val.range.max) != 0) {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100402 memprintf(err, "'%s' is not a valid dotted number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100403 return 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100404 }
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100405 pattern->val.range.min_set = 0;
406 pattern->val.range.max_set = 1;
407 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100408 }
409
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100410 /* If separator is the last character. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100411 if (ptr == &text[strlen(text)-1]) {
412 if (strl2llrc_dotted(text, ptr-text, &pattern->val.range.min) != 0) {
413 memprintf(err, "'%s' is not a valid dotted number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100414 return 0;
415 }
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100416 pattern->val.range.min_set = 1;
417 pattern->val.range.max_set = 0;
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100418 return 1;
419 }
420
421 /* Else, parse two numbers. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100422 if (strl2llrc_dotted(text, ptr-text, &pattern->val.range.min) != 0) {
423 memprintf(err, "'%s' is not a valid dotted number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100424 return 0;
425 }
426 if (strl2llrc_dotted(ptr+1, strlen(ptr+1), &pattern->val.range.max) != 0) {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100427 memprintf(err, "'%s' is not a valid dotted number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100428 return 0;
429 }
430 if (pattern->val.range.min > pattern->val.range.max) {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100431 memprintf(err, "'%s' is not a valid dotted number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100432 return 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100433 }
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100434 pattern->val.range.min_set = 1;
435 pattern->val.range.max_set = 1;
436 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100437}
438
439/* Parse an IP address and an optional mask in the form addr[/mask].
440 * The addr may either be an IPv4 address or a hostname. The mask
441 * may either be a dotted mask or a number of bits. Returns 1 if OK,
442 * otherwise 0. NOTE: IP address patterns are typed (IPV4/IPV6).
443 */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100444int pat_parse_ip(const char *text, struct pattern *pattern, enum pat_usage usage, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100445{
Thierry FOURNIERcc0e0b32013-12-06 16:56:40 +0100446 pattern->expect_type = SMP_T_ADDR;
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100447 if (str2net(text, &pattern->val.ipv4.addr, &pattern->val.ipv4.mask)) {
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100448 pattern->type = SMP_T_IPV4;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100449 return 1;
450 }
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100451 else if (str62net(text, &pattern->val.ipv6.addr, &pattern->val.ipv6.mask)) {
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100452 /* no tree support right now */
453 pattern->type = SMP_T_IPV6;
454 return 1;
455 }
456 else {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100457 memprintf(err, "'%s' is not a valid IPv4 or IPv6 address", text);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100458 return 0;
459 }
460}
461
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100462/*
463 *
464 * These functions are exported and may be used by any other component.
465 *
466 * This fucntion just take a sample <smp> and check if this sample match
467 * with the pattern <pattern>. This fucntion return just PAT_MATCH or
468 * PAT_NOMATCH.
469 *
470 */
471
472/* always return false */
473enum pat_match_res pat_match_nothing(struct sample *smp, struct pattern *pattern)
474{
475 return PAT_NOMATCH;
476}
477
478
479/* NB: For two strings to be identical, it is required that their lengths match */
480enum pat_match_res pat_match_str(struct sample *smp, struct pattern *pattern)
481{
482 int icase;
483
484 if (pattern->len != smp->data.str.len)
485 return PAT_NOMATCH;
486
487 icase = pattern->flags & PAT_F_IGNORE_CASE;
488 if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) == 0) ||
489 (!icase && strncmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) == 0))
490 return PAT_MATCH;
491 return PAT_NOMATCH;
492}
493
494/* NB: For two binaries buf to be identical, it is required that their lengths match */
495enum pat_match_res pat_match_bin(struct sample *smp, struct pattern *pattern)
496{
497 if (pattern->len != smp->data.str.len)
498 return PAT_NOMATCH;
499
500 if (memcmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) == 0)
501 return PAT_MATCH;
502 return PAT_NOMATCH;
503}
504
505/* Executes a regex. It temporarily changes the data to add a trailing zero,
506 * and restores the previous character when leaving.
507 */
508enum pat_match_res pat_match_reg(struct sample *smp, struct pattern *pattern)
509{
510 if (regex_exec(pattern->ptr.reg, smp->data.str.str, smp->data.str.len) == 0)
511 return PAT_MATCH;
512 return PAT_NOMATCH;
513}
514
515/* Checks that the pattern matches the beginning of the tested string. */
516enum pat_match_res pat_match_beg(struct sample *smp, struct pattern *pattern)
517{
518 int icase;
519
520 if (pattern->len > smp->data.str.len)
521 return PAT_NOMATCH;
522
523 icase = pattern->flags & PAT_F_IGNORE_CASE;
524 if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str, pattern->len) != 0) ||
525 (!icase && strncmp(pattern->ptr.str, smp->data.str.str, pattern->len) != 0))
526 return PAT_NOMATCH;
527 return PAT_MATCH;
528}
529
530/* Checks that the pattern matches the end of the tested string. */
531enum pat_match_res pat_match_end(struct sample *smp, struct pattern *pattern)
532{
533 int icase;
534
535 if (pattern->len > smp->data.str.len)
536 return PAT_NOMATCH;
537 icase = pattern->flags & PAT_F_IGNORE_CASE;
538 if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str + smp->data.str.len - pattern->len, pattern->len) != 0) ||
539 (!icase && strncmp(pattern->ptr.str, smp->data.str.str + smp->data.str.len - pattern->len, pattern->len) != 0))
540 return PAT_NOMATCH;
541 return PAT_MATCH;
542}
543
544/* Checks that the pattern is included inside the tested string.
545 * NB: Suboptimal, should be rewritten using a Boyer-Moore method.
546 */
547enum pat_match_res pat_match_sub(struct sample *smp, struct pattern *pattern)
548{
549 int icase;
550 char *end;
551 char *c;
552
553 if (pattern->len > smp->data.str.len)
554 return PAT_NOMATCH;
555
556 end = smp->data.str.str + smp->data.str.len - pattern->len;
557 icase = pattern->flags & PAT_F_IGNORE_CASE;
558 if (icase) {
559 for (c = smp->data.str.str; c <= end; c++) {
560 if (tolower(*c) != tolower(*pattern->ptr.str))
561 continue;
562 if (strncasecmp(pattern->ptr.str, c, pattern->len) == 0)
563 return PAT_MATCH;
564 }
565 } else {
566 for (c = smp->data.str.str; c <= end; c++) {
567 if (*c != *pattern->ptr.str)
568 continue;
569 if (strncmp(pattern->ptr.str, c, pattern->len) == 0)
570 return PAT_MATCH;
571 }
572 }
573 return PAT_NOMATCH;
574}
575
576/* This one is used by other real functions. It checks that the pattern is
577 * included inside the tested string, but enclosed between the specified
578 * delimiters or at the beginning or end of the string. The delimiters are
579 * provided as an unsigned int made by make_4delim() and match up to 4 different
580 * delimiters. Delimiters are stripped at the beginning and end of the pattern.
581 */
582static int match_word(struct sample *smp, struct pattern *pattern, unsigned int delimiters)
583{
584 int may_match, icase;
585 char *c, *end;
586 char *ps;
587 int pl;
588
589 pl = pattern->len;
590 ps = pattern->ptr.str;
591
592 while (pl > 0 && is_delimiter(*ps, delimiters)) {
593 pl--;
594 ps++;
595 }
596
597 while (pl > 0 && is_delimiter(ps[pl - 1], delimiters))
598 pl--;
599
600 if (pl > smp->data.str.len)
601 return PAT_NOMATCH;
602
603 may_match = 1;
604 icase = pattern->flags & PAT_F_IGNORE_CASE;
605 end = smp->data.str.str + smp->data.str.len - pl;
606 for (c = smp->data.str.str; c <= end; c++) {
607 if (is_delimiter(*c, delimiters)) {
608 may_match = 1;
609 continue;
610 }
611
612 if (!may_match)
613 continue;
614
615 if (icase) {
616 if ((tolower(*c) == tolower(*ps)) &&
617 (strncasecmp(ps, c, pl) == 0) &&
618 (c == end || is_delimiter(c[pl], delimiters)))
619 return PAT_MATCH;
620 } else {
621 if ((*c == *ps) &&
622 (strncmp(ps, c, pl) == 0) &&
623 (c == end || is_delimiter(c[pl], delimiters)))
624 return PAT_MATCH;
625 }
626 may_match = 0;
627 }
628 return PAT_NOMATCH;
629}
630
631/* Checks that the pattern is included inside the tested string, but enclosed
632 * between the delimiters '?' or '/' or at the beginning or end of the string.
633 * Delimiters at the beginning or end of the pattern are ignored.
634 */
635enum pat_match_res pat_match_dir(struct sample *smp, struct pattern *pattern)
636{
637 return match_word(smp, pattern, make_4delim('/', '?', '?', '?'));
638}
639
640/* Checks that the pattern is included inside the tested string, but enclosed
641 * between the delmiters '/', '?', '.' or ":" or at the beginning or end of
642 * the string. Delimiters at the beginning or end of the pattern are ignored.
643 */
644enum pat_match_res pat_match_dom(struct sample *smp, struct pattern *pattern)
645{
646 return match_word(smp, pattern, make_4delim('/', '?', '.', ':'));
647}
648
649/* Checks that the integer in <test> is included between min and max */
650enum pat_match_res pat_match_int(struct sample *smp, struct pattern *pattern)
651{
652 if ((!pattern->val.range.min_set || pattern->val.range.min <= smp->data.uint) &&
653 (!pattern->val.range.max_set || smp->data.uint <= pattern->val.range.max))
654 return PAT_MATCH;
655 return PAT_NOMATCH;
656}
657
658/* Checks that the length of the pattern in <test> is included between min and max */
659enum pat_match_res pat_match_len(struct sample *smp, struct pattern *pattern)
660{
661 if ((!pattern->val.range.min_set || pattern->val.range.min <= smp->data.str.len) &&
662 (!pattern->val.range.max_set || smp->data.str.len <= pattern->val.range.max))
663 return PAT_MATCH;
664 return PAT_NOMATCH;
665}
666
667enum pat_match_res pat_match_ip(struct sample *smp, struct pattern *pattern)
668{
669 unsigned int v4; /* in network byte order */
670 struct in6_addr *v6;
671 int bits, pos;
672 struct in6_addr tmp6;
673
674 if (pattern->type == SMP_T_IPV4) {
675 if (smp->type == SMP_T_IPV4) {
676 v4 = smp->data.ipv4.s_addr;
677 }
678 else if (smp->type == SMP_T_IPV6) {
679 /* v4 match on a V6 sample. We want to check at least for
680 * the following forms :
681 * - ::ffff:ip:v4 (ipv4 mapped)
682 * - ::0000:ip:v4 (old ipv4 mapped)
683 * - 2002:ip:v4:: (6to4)
684 */
685 if (*(uint32_t*)&smp->data.ipv6.s6_addr[0] == 0 &&
686 *(uint32_t*)&smp->data.ipv6.s6_addr[4] == 0 &&
687 (*(uint32_t*)&smp->data.ipv6.s6_addr[8] == 0 ||
688 *(uint32_t*)&smp->data.ipv6.s6_addr[8] == htonl(0xFFFF))) {
689 v4 = *(uint32_t*)&smp->data.ipv6.s6_addr[12];
690 }
691 else if (*(uint16_t*)&smp->data.ipv6.s6_addr[0] == htons(0x2002)) {
692 v4 = htonl((ntohs(*(uint16_t*)&smp->data.ipv6.s6_addr[2]) << 16) +
693 ntohs(*(uint16_t*)&smp->data.ipv6.s6_addr[4]));
694 }
695 else
696 return PAT_NOMATCH;
697 }
698 else
699 return PAT_NOMATCH;
700
701 if (((v4 ^ pattern->val.ipv4.addr.s_addr) & pattern->val.ipv4.mask.s_addr) == 0)
702 return PAT_MATCH;
703 else
704 return PAT_NOMATCH;
705 }
706 else if (pattern->type == SMP_T_IPV6) {
707 if (smp->type == SMP_T_IPV4) {
708 /* Convert the IPv4 sample address to IPv4 with the
709 * mapping method using the ::ffff: prefix.
710 */
711 memset(&tmp6, 0, 10);
712 *(uint16_t*)&tmp6.s6_addr[10] = htons(0xffff);
713 *(uint32_t*)&tmp6.s6_addr[12] = smp->data.ipv4.s_addr;
714 v6 = &tmp6;
715 }
716 else if (smp->type == SMP_T_IPV6) {
717 v6 = &smp->data.ipv6;
718 }
719 else {
720 return PAT_NOMATCH;
721 }
722
723 bits = pattern->val.ipv6.mask;
724 for (pos = 0; bits > 0; pos += 4, bits -= 32) {
725 v4 = *(uint32_t*)&v6->s6_addr[pos] ^ *(uint32_t*)&pattern->val.ipv6.addr.s6_addr[pos];
726 if (bits < 32)
727 v4 &= htonl((~0U) << (32-bits));
728 if (v4)
729 return PAT_NOMATCH;
730 }
731 return PAT_MATCH;
732 }
733 return PAT_NOMATCH;
734}
735
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100736/* NB: does nothing if <pat> is NULL */
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100737void pattern_free(struct pattern *pat)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100738{
739 if (!pat)
740 return;
741
742 if (pat->ptr.ptr) {
743 if (pat->freeptrbuf)
744 pat->freeptrbuf(pat->ptr.ptr);
745
746 free(pat->ptr.ptr);
747 }
748
Thierry FOURNIERc64de3f2013-12-10 15:08:39 +0100749 free(pat->smp);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100750 free(pat);
751}
752
753void free_pattern_list(struct list *head)
754{
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100755 struct pattern *pat, *tmp;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100756 list_for_each_entry_safe(pat, tmp, head, list)
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100757 pattern_free(pat);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100758}
759
760void free_pattern_tree(struct eb_root *root)
761{
762 struct eb_node *node, *next;
Thierry FOURNIER3ce88c72013-12-09 11:29:46 +0100763 struct pat_idx_elt *elt;
764
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100765 node = eb_first(root);
766 while (node) {
767 next = eb_next(node);
768 eb_delete(node);
Thierry FOURNIER3ce88c72013-12-09 11:29:46 +0100769 elt = container_of(node, struct pat_idx_elt, node);
Thierry FOURNIERc64de3f2013-12-10 15:08:39 +0100770 free(elt->smp);
Thierry FOURNIER3ce88c72013-12-09 11:29:46 +0100771 free(elt);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100772 node = next;
773 }
774}
775
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100776void pattern_prune_expr(struct pattern_expr *expr)
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +0100777{
778 free_pattern_list(&expr->patterns);
779 free_pattern_tree(&expr->pattern_tree);
780 LIST_INIT(&expr->patterns);
781}
782
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100783void pattern_init_expr(struct pattern_expr *expr)
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +0100784{
785 LIST_INIT(&expr->patterns);
786 expr->pattern_tree = EB_ROOT_UNIQUE;
787}
788
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100789/* return 1 if the process is ok
790 * return -1 if the parser fail. The err message is filled.
791 * return -2 if out of memory
792 */
Thierry FOURNIER972028f2014-01-23 17:53:31 +0100793int pattern_register(struct pattern_expr *expr, const char *arg,
794 struct sample_storage *smp,
795 struct pattern **pattern,
796 int patflags, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100797{
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100798 unsigned int mask = 0;
799 struct pat_idx_elt *node;
800 int len;
801 int ret;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100802
Thierry FOURNIER972028f2014-01-23 17:53:31 +0100803 /* we keep the previous pattern along iterations as long as it's not used */
804 if (!*pattern)
805 *pattern = (struct pattern *)malloc(sizeof(**pattern));
806 if (!*pattern) {
807 memprintf(err, "out of memory while loading pattern");
808 return 0;
809 }
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100810
Thierry FOURNIER972028f2014-01-23 17:53:31 +0100811 memset(*pattern, 0, sizeof(**pattern));
812 (*pattern)->flags = patflags;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100813
Thierry FOURNIER972028f2014-01-23 17:53:31 +0100814 ret = expr->parse(arg, *pattern, PAT_U_COMPILE, err);
815 if (!ret)
816 return 0;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100817
Thierry FOURNIER972028f2014-01-23 17:53:31 +0100818 if (expr->match == pat_match_str) {
819 /* SMP_T_CSTR tree indexation.
820 * The match "pat_match_str()" can use trees.
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100821 */
Thierry FOURNIER972028f2014-01-23 17:53:31 +0100822 if ((*pattern)->flags & PAT_F_IGNORE_CASE) {
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100823 /* If the flag PAT_F_IGNORE_CASE is set, we cannot use trees */
Thierry FOURNIER972028f2014-01-23 17:53:31 +0100824 goto just_chain_the_pattern;
825 }
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100826
Thierry FOURNIER972028f2014-01-23 17:53:31 +0100827 /* Process the key len */
828 len = strlen((*pattern)->ptr.str) + 1;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100829
Thierry FOURNIER972028f2014-01-23 17:53:31 +0100830 /* node memory allocation */
831 node = calloc(1, sizeof(*node) + len);
832 if (!node) {
833 memprintf(err, "out of memory while loading pattern");
834 return 0;
835 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100836
Thierry FOURNIER972028f2014-01-23 17:53:31 +0100837 /* copy the pointer to sample associated to this node */
838 node->smp = smp;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100839
Thierry FOURNIER972028f2014-01-23 17:53:31 +0100840 /* copy the string */
841 memcpy(node->node.key, (*pattern)->ptr.str, len);
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100842
Thierry FOURNIER972028f2014-01-23 17:53:31 +0100843 /* the "map_parser_str()" function always duplicate string information */
844 free((*pattern)->ptr.str);
845 (*pattern)->ptr.str = NULL;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100846
Thierry FOURNIER972028f2014-01-23 17:53:31 +0100847 /* we pre-set the data pointer to the tree's head so that functions
848 * which are able to insert in a tree know where to do that.
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100849 *
Thierry FOURNIER972028f2014-01-23 17:53:31 +0100850 * because "val" is an "union", the previous data are crushed.
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100851 */
Thierry FOURNIER972028f2014-01-23 17:53:31 +0100852 (*pattern)->flags |= PAT_F_TREE;
853 (*pattern)->val.tree = &expr->pattern_tree;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100854
Thierry FOURNIER972028f2014-01-23 17:53:31 +0100855 /* index the new node */
856 if (ebst_insert((*pattern)->val.tree, &node->node) != &node->node)
857 free(node); /* was a duplicate */
858 }
859 else if (expr->match == pat_match_ip) {
860 /* SMP_T_IPV4 tree indexation
861 * The match "pat_match_ip()" can use tree.
862 */
863 if ((*pattern)->type != SMP_T_IPV4) {
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100864 /* Only IPv4 can be indexed */
Thierry FOURNIER972028f2014-01-23 17:53:31 +0100865 goto just_chain_the_pattern;
866 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100867
Thierry FOURNIER972028f2014-01-23 17:53:31 +0100868 /* in IPv4 case, check if the mask is contiguous so that we can
869 * insert the network into the tree. A continuous mask has only
870 * ones on the left. This means that this mask + its lower bit
871 * added once again is null.
872 */
873 mask = ntohl((*pattern)->val.ipv4.mask.s_addr);
874 if (mask + (mask & -mask) != 0)
875 goto just_chain_the_pattern;
876 mask = mask ? 33 - flsnz(mask & -mask) : 0; /* equals cidr value */
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100877
Thierry FOURNIER972028f2014-01-23 17:53:31 +0100878 /* node memory allocation */
879 node = calloc(1, sizeof(*node) + 4);
880 if (!node) {
881 memprintf(err, "out of memory while loading pattern");
882 return 0;
883 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100884
Thierry FOURNIER972028f2014-01-23 17:53:31 +0100885 /* copy the pointer to sample associated to this node */
886 node->smp = smp;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100887
Thierry FOURNIER972028f2014-01-23 17:53:31 +0100888 /* FIXME: insert <addr>/<mask> into the tree here */
889 memcpy(node->node.key, &(*pattern)->val.ipv4.addr, 4); /* network byte order */
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100890
Thierry FOURNIER972028f2014-01-23 17:53:31 +0100891 /* we pre-set the data pointer to the tree's head so that functions
892 * which are able to insert in a tree know where to do that.
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100893 *
Thierry FOURNIER972028f2014-01-23 17:53:31 +0100894 * because "val" is an "union", the previous data are crushed.
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100895 */
Thierry FOURNIER972028f2014-01-23 17:53:31 +0100896 (*pattern)->flags |= PAT_F_TREE;
897 (*pattern)->val.tree = &expr->pattern_tree;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100898
Thierry FOURNIER972028f2014-01-23 17:53:31 +0100899 /* Index the new node
900 * FIXME: insert <addr>/<mask> into the tree here
901 */
902 node->node.node.pfx = mask;
903 if (ebmb_insert_prefix((*pattern)->val.tree, &node->node, 4) != &node->node)
904 free(node); /* was a duplicate */
905 }
906 else {
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100907just_chain_the_pattern:
Thierry FOURNIER972028f2014-01-23 17:53:31 +0100908 /* if the parser did not feed the tree, let's chain the pattern to the list */
909 LIST_ADDQ(&expr->patterns, &(*pattern)->list);
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100910
Thierry FOURNIER972028f2014-01-23 17:53:31 +0100911 /* copy the pointer to sample associated to this node */
912 (*pattern)->smp = smp;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100913
Thierry FOURNIER972028f2014-01-23 17:53:31 +0100914 /* get a new one */
915 *pattern = NULL;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100916 }
917
918 return 1;
919}
920
921/* Reads patterns from a file. If <err_msg> is non-NULL, an error message will
922 * be returned there on errors and the caller will have to free it.
923 */
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100924int pattern_read_from_file(struct pattern_expr *expr,
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100925 const char *filename, int patflags,
926 char **err)
927{
928 FILE *file;
929 char *c;
930 char *arg;
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100931 struct pattern *pattern;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100932 int ret = 0;
933 int line = 0;
934 int code;
935
936 file = fopen(filename, "r");
937 if (!file) {
938 memprintf(err, "failed to open pattern file <%s>", filename);
939 return 0;
940 }
941
942 /* now parse all patterns. The file may contain only one pattern per
943 * line. If the line contains spaces, they will be part of the pattern.
944 * The pattern stops at the first CR, LF or EOF encountered.
945 */
946 pattern = NULL;
947 while (fgets(trash.str, trash.size, file) != NULL) {
948 line++;
949 c = trash.str;
950
951 /* ignore lines beginning with a dash */
952 if (*c == '#')
953 continue;
954
955 /* strip leading spaces and tabs */
956 while (*c == ' ' || *c == '\t')
957 c++;
958
959
960 arg = c;
961 while (*c && *c != '\n' && *c != '\r')
962 c++;
963 *c = 0;
964
965 /* empty lines are ignored too */
966 if (c == arg)
967 continue;
968
Thierry FOURNIER972028f2014-01-23 17:53:31 +0100969 code = pattern_register(expr, arg, NULL, &pattern, patflags, err);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100970 if (code == -2) {
971 memprintf(err, "out of memory when loading patterns from file <%s>", filename);
972 goto out_close;
973 }
974 else if (code < 0) {
975 memprintf(err, "%s when loading patterns from file <%s>", *err, filename);
976 goto out_free_pattern;
977 }
978 }
979
980 ret = 1; /* success */
981
982 out_free_pattern:
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100983 pattern_free(pattern);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100984 out_close:
985 fclose(file);
986 return ret;
987}
988
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100989/* This function matches a sample <smp> against a set of patterns presented in
990 * pattern expression <expr>. Upon success, if <sample> is not NULL, it is fed
991 * with the pointer associated with the matching pattern. This function returns
992 * PAT_NOMATCH or PAT_MATCH.
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100993 */
Willy Tarreau0cba6072013-11-28 22:21:02 +0100994enum pat_match_res pattern_exec_match(struct pattern_expr *expr, struct sample *smp,
Thierry FOURNIER76090642013-12-10 15:03:38 +0100995 struct sample_storage **sample,
996 struct pattern **pat, struct pat_idx_elt **idx_elt)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100997{
Willy Tarreau0cba6072013-11-28 22:21:02 +0100998 enum pat_match_res pat_res = PAT_NOMATCH;
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100999 struct pattern *pattern;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001000 struct ebmb_node *node = NULL;
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01001001 struct pat_idx_elt *elt;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001002
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01001003 if (expr->match == pat_match_nothing) {
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001004 if (smp->data.uint)
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01001005 pat_res |= PAT_MATCH;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001006 else
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01001007 pat_res |= PAT_NOMATCH;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001008 }
1009 else if (!expr->match) {
1010 /* just check for existence */
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01001011 pat_res |= PAT_MATCH;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001012 }
1013 else {
1014 if (!eb_is_empty(&expr->pattern_tree)) {
1015 /* a tree is present, let's check what type it is */
Thierry FOURNIERe3ded592013-12-06 15:36:54 +01001016 if (expr->match == pat_match_str) {
1017 if (sample_convert(smp, SMP_T_STR))
1018 node = pat_lookup_str(smp, expr);
1019 }
1020 else if (expr->match == pat_match_ip) {
1021 if (sample_convert(smp, SMP_T_IPV4))
1022 node = pat_lookup_ip(smp, expr);
1023 }
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001024 if (node) {
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01001025 pat_res |= PAT_MATCH;
1026 elt = ebmb_entry(node, struct pat_idx_elt, node);
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001027 if (sample)
1028 *sample = elt->smp;
Thierry FOURNIER76090642013-12-10 15:03:38 +01001029 if (idx_elt)
1030 *idx_elt = elt;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001031 }
1032 }
1033
1034 /* call the match() function for all tests on this value */
1035 list_for_each_entry(pattern, &expr->patterns, list) {
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01001036 if (pat_res == PAT_MATCH)
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001037 break;
Thierry FOURNIERe3ded592013-12-06 15:36:54 +01001038 if (sample_convert(smp, pattern->expect_type))
1039 pat_res |= expr->match(smp, pattern);
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001040 if (sample)
1041 *sample = pattern->smp;
Thierry FOURNIER76090642013-12-10 15:03:38 +01001042 if (pat)
1043 *pat = pattern;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001044 }
1045 }
1046
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01001047 return pat_res;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001048}
1049
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001050/* This function browse the pattern expr <expr> to lookup the key <key>. On
1051 * error it returns 0. On success, it returns 1 and fills either <pat_elt>
1052 * or <idx_elt> with the respectively matched pointers, and the other one with
1053 * NULL. Pointers are not set if they're passed as NULL.
1054 */
1055int pattern_lookup(const char *key, struct pattern_expr *expr,
1056 struct pattern **pat_elt, struct pat_idx_elt **idx_elt, char **err)
1057{
1058 struct pattern pattern;
1059 struct pattern *pat;
1060 struct ebmb_node *node;
1061 struct pat_idx_elt *elt;
Willy Tarreau668ae532013-12-15 16:42:26 +01001062 unsigned int mask = 0;
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001063
1064 /* no real pattern */
1065 if (!expr->match || expr->match == pat_match_nothing)
1066 return 0;
1067
1068 /* build lookup pattern */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +01001069 if (!expr->parse(key, &pattern, PAT_U_LOOKUP, NULL))
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001070 return 0;
1071
1072 pat = NULL;
1073 elt = NULL;
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001074
Thierry FOURNIERc64de3f2013-12-10 15:08:39 +01001075 /* Try to look up the tree first. IPv6 is not indexed */
1076 if (!eb_is_empty(&expr->pattern_tree) && pattern.type != SMP_T_IPV6) {
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001077 /* Check the pattern type */
1078 if (pattern.type != SMP_T_STR &&
1079 pattern.type != SMP_T_CSTR &&
1080 pattern.type != SMP_T_IPV4) {
1081 memprintf(err, "Unexpected pattern type.");
1082 return 0;
1083 }
1084
1085 /* Convert mask. If the mask is not contiguous, ignore the lookup
1086 * in the tree, and browse the list.
1087 */
1088 if (expr->match == pat_match_ip) {
1089 mask = ntohl(pattern.val.ipv4.mask.s_addr);
1090 if (mask + (mask & -mask) != 0)
1091 goto browse_list;
1092 mask = mask ? 33 - flsnz(mask & -mask) : 0; /* equals cidr value */
1093 }
1094
1095 /* browse each node of the tree, and check string */
1096 if (expr->match == pat_match_str) {
1097 for (node = ebmb_first(&expr->pattern_tree);
1098 node;
1099 node = ebmb_next(node)) {
1100 elt = container_of(node, struct pat_idx_elt, node);
1101 if (strcmp(pattern.ptr.str, (char *)elt->node.key) == 0)
1102 goto found;
1103 }
1104 }
1105 else if (expr->match == pat_match_ip) {
1106 for (node = ebmb_first(&expr->pattern_tree);
1107 node;
1108 node = ebmb_next(node)) {
1109 elt = container_of(node, struct pat_idx_elt, node);
1110 if (elt->node.node.pfx == mask &&
1111 memcmp(&pattern.val.ipv4.addr.s_addr, elt->node.key, 4) == 0)
1112 goto found;
1113 }
1114 }
1115 }
1116
1117browse_list:
Thierry FOURNIERc64de3f2013-12-10 15:08:39 +01001118 elt = NULL;
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001119 if (expr->parse == pat_parse_int ||
1120 expr->parse == pat_parse_len) {
1121 list_for_each_entry(pat, &expr->patterns, list) {
1122 if (pat->flags & PAT_F_TREE)
1123 continue;
1124 if (pattern.val.range.min_set != pat->val.range.min_set)
1125 continue;
1126 if (pattern.val.range.max_set != pat->val.range.max_set)
1127 continue;
1128 if (pattern.val.range.min_set &&
1129 pattern.val.range.min != pat->val.range.min)
1130 continue;
1131 if (pattern.val.range.max_set &&
1132 pattern.val.range.max != pat->val.range.max)
1133 continue;
1134 goto found;
1135 }
1136 }
1137 else if (expr->parse == pat_parse_ip) {
1138 list_for_each_entry(pat, &expr->patterns, list) {
1139 if (pat->flags & PAT_F_TREE)
1140 continue;
1141 if (pattern.type != pat->type)
1142 continue;
1143 if (pattern.type == SMP_T_IPV4 &&
1144 memcmp(&pattern.val.ipv4.addr, &pat->val.ipv4.addr, sizeof(pat->val.ipv4.addr)) != 0)
1145 continue;
1146 if (pattern.type == SMP_T_IPV4 &&
1147 memcmp(&pattern.val.ipv4.mask, &pat->val.ipv4.mask, sizeof(pat->val.ipv4.addr)) != 0)
1148 continue;
1149 if (pattern.type == SMP_T_IPV6 &&
1150 memcmp(&pattern.val.ipv6.addr, &pat->val.ipv6.addr, sizeof(pat->val.ipv6.addr)) != 0)
1151 continue;
1152 if (pattern.type == SMP_T_IPV6 &&
1153 pattern.val.ipv6.mask != pat->val.ipv6.mask)
1154 continue;
1155 goto found;
1156 }
1157 }
1158 else if (expr->parse == pat_parse_str) {
1159 list_for_each_entry(pat, &expr->patterns, list) {
1160 if (pat->flags & PAT_F_TREE)
1161 continue;
1162 if (pattern.len != pat->len)
1163 continue;
Thierry FOURNIER35249cb2014-01-14 13:38:40 +01001164 if (pat->flags & PAT_F_IGNORE_CASE) {
1165 if (strncasecmp(pattern.ptr.str, pat->ptr.str, pat->len) != 0)
1166 continue;
1167 }
1168 else {
1169 if (strncmp(pattern.ptr.str, pat->ptr.str, pat->len) != 0)
1170 continue;
1171 }
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001172 goto found;
1173 }
1174 }
1175 else if (expr->parse == pat_parse_bin) {
1176 list_for_each_entry(pat, &expr->patterns, list) {
1177 if (pat->flags & PAT_F_TREE)
1178 continue;
1179 if (pattern.len != pat->len)
1180 continue;
1181 if (memcmp(pattern.ptr.ptr, pat->ptr.ptr, pat->len) != 0)
1182 continue;
1183 goto found;
1184 }
1185 }
1186 else if (expr->parse == pat_parse_reg) {
1187 list_for_each_entry(pat, &expr->patterns, list) {
1188 if (pat->flags & PAT_F_TREE)
1189 continue;
Thierry FOURNIER35249cb2014-01-14 13:38:40 +01001190 if (pat->flags & PAT_F_IGNORE_CASE) {
1191 if (strcasecmp(pattern.ptr.reg->regstr, pat->ptr.reg->regstr) != 0)
1192 continue;
1193 }
1194 else {
1195 if (strcmp(pattern.ptr.reg->regstr, pat->ptr.reg->regstr) != 0)
1196 continue;
1197 }
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001198 goto found;
1199 }
1200 }
1201
1202 /* if we get there, we didn't find the pattern */
1203 return 0;
1204found:
1205 if (idx_elt)
1206 *idx_elt = elt;
1207
1208 if (pat_elt)
1209 *pat_elt = pat;
1210
1211 return 1;
1212}