blob: 8380c636fef30393dea6695d3519c5d71e55abae [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 FOURNIER0b2fe4a2013-12-06 20:33:50 +010043int (*pat_parse_fcts[PAT_MATCH_NUM])(const char **, struct pattern *, enum pat_usage, int *, 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/*
93 * These functions are exported and may be used by any other component.
94 */
95
96/* ignore the current line */
Thierry FOURNIER0b2fe4a2013-12-06 20:33:50 +010097int pat_parse_nothing(const char **text, struct pattern *pattern, enum pat_usage usage, int *opaque, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +010098{
99 return 1;
100}
101
102/* always return false */
Willy Tarreau0cba6072013-11-28 22:21:02 +0100103enum pat_match_res pat_match_nothing(struct sample *smp, struct pattern *pattern)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100104{
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100105 return PAT_NOMATCH;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100106}
107
108
109/* NB: For two strings to be identical, it is required that their lengths match */
Willy Tarreau0cba6072013-11-28 22:21:02 +0100110enum pat_match_res pat_match_str(struct sample *smp, struct pattern *pattern)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100111{
112 int icase;
113
114 if (pattern->len != smp->data.str.len)
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100115 return PAT_NOMATCH;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100116
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100117 icase = pattern->flags & PAT_F_IGNORE_CASE;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100118 if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) == 0) ||
119 (!icase && strncmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) == 0))
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100120 return PAT_MATCH;
121 return PAT_NOMATCH;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100122}
123
124/* NB: For two binaries buf to be identical, it is required that their lengths match */
Willy Tarreau0cba6072013-11-28 22:21:02 +0100125enum pat_match_res pat_match_bin(struct sample *smp, struct pattern *pattern)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100126{
127 if (pattern->len != smp->data.str.len)
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100128 return PAT_NOMATCH;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100129
130 if (memcmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) == 0)
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100131 return PAT_MATCH;
132 return PAT_NOMATCH;
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
154/* Executes a regex. It temporarily changes the data to add a trailing zero,
155 * and restores the previous character when leaving.
156 */
Willy Tarreau0cba6072013-11-28 22:21:02 +0100157enum pat_match_res pat_match_reg(struct sample *smp, struct pattern *pattern)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100158{
159 if (regex_exec(pattern->ptr.reg, smp->data.str.str, smp->data.str.len) == 0)
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100160 return PAT_MATCH;
161 return PAT_NOMATCH;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100162}
163
164/* Checks that the pattern matches the beginning of the tested string. */
Willy Tarreau0cba6072013-11-28 22:21:02 +0100165enum pat_match_res pat_match_beg(struct sample *smp, struct pattern *pattern)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100166{
167 int icase;
168
169 if (pattern->len > smp->data.str.len)
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100170 return PAT_NOMATCH;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100171
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100172 icase = pattern->flags & PAT_F_IGNORE_CASE;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100173 if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str, pattern->len) != 0) ||
174 (!icase && strncmp(pattern->ptr.str, smp->data.str.str, pattern->len) != 0))
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100175 return PAT_NOMATCH;
176 return PAT_MATCH;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100177}
178
179/* Checks that the pattern matches the end of the tested string. */
Willy Tarreau0cba6072013-11-28 22:21:02 +0100180enum pat_match_res pat_match_end(struct sample *smp, struct pattern *pattern)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100181{
182 int icase;
183
184 if (pattern->len > smp->data.str.len)
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100185 return PAT_NOMATCH;
186 icase = pattern->flags & PAT_F_IGNORE_CASE;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100187 if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str + smp->data.str.len - pattern->len, pattern->len) != 0) ||
188 (!icase && strncmp(pattern->ptr.str, smp->data.str.str + smp->data.str.len - pattern->len, pattern->len) != 0))
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100189 return PAT_NOMATCH;
190 return PAT_MATCH;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100191}
192
193/* Checks that the pattern is included inside the tested string.
194 * NB: Suboptimal, should be rewritten using a Boyer-Moore method.
195 */
Willy Tarreau0cba6072013-11-28 22:21:02 +0100196enum pat_match_res pat_match_sub(struct sample *smp, struct pattern *pattern)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100197{
198 int icase;
199 char *end;
200 char *c;
201
202 if (pattern->len > smp->data.str.len)
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100203 return PAT_NOMATCH;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100204
205 end = smp->data.str.str + smp->data.str.len - pattern->len;
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100206 icase = pattern->flags & PAT_F_IGNORE_CASE;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100207 if (icase) {
208 for (c = smp->data.str.str; c <= end; c++) {
209 if (tolower(*c) != tolower(*pattern->ptr.str))
210 continue;
211 if (strncasecmp(pattern->ptr.str, c, pattern->len) == 0)
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100212 return PAT_MATCH;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100213 }
214 } else {
215 for (c = smp->data.str.str; c <= end; c++) {
216 if (*c != *pattern->ptr.str)
217 continue;
218 if (strncmp(pattern->ptr.str, c, pattern->len) == 0)
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100219 return PAT_MATCH;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100220 }
221 }
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100222 return PAT_NOMATCH;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100223}
224
225/* Background: Fast way to find a zero byte in a word
226 * http://graphics.stanford.edu/~seander/bithacks.html#ZeroInWord
227 * hasZeroByte = (v - 0x01010101UL) & ~v & 0x80808080UL;
228 *
229 * To look for 4 different byte values, xor the word with those bytes and
230 * then check for zero bytes:
231 *
232 * v = (((unsigned char)c * 0x1010101U) ^ delimiter)
233 * where <delimiter> is the 4 byte values to look for (as an uint)
234 * and <c> is the character that is being tested
235 */
236static inline unsigned int is_delimiter(unsigned char c, unsigned int mask)
237{
238 mask ^= (c * 0x01010101); /* propagate the char to all 4 bytes */
239 return (mask - 0x01010101) & ~mask & 0x80808080U;
240}
241
242static inline unsigned int make_4delim(unsigned char d1, unsigned char d2, unsigned char d3, unsigned char d4)
243{
244 return d1 << 24 | d2 << 16 | d3 << 8 | d4;
245}
246
247/* This one is used by other real functions. It checks that the pattern is
248 * included inside the tested string, but enclosed between the specified
249 * delimiters or at the beginning or end of the string. The delimiters are
250 * provided as an unsigned int made by make_4delim() and match up to 4 different
251 * delimiters. Delimiters are stripped at the beginning and end of the pattern.
252 */
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100253static int match_word(struct sample *smp, struct pattern *pattern, unsigned int delimiters)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100254{
255 int may_match, icase;
256 char *c, *end;
257 char *ps;
258 int pl;
259
260 pl = pattern->len;
261 ps = pattern->ptr.str;
262
263 while (pl > 0 && is_delimiter(*ps, delimiters)) {
264 pl--;
265 ps++;
266 }
267
268 while (pl > 0 && is_delimiter(ps[pl - 1], delimiters))
269 pl--;
270
271 if (pl > smp->data.str.len)
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100272 return PAT_NOMATCH;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100273
274 may_match = 1;
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100275 icase = pattern->flags & PAT_F_IGNORE_CASE;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100276 end = smp->data.str.str + smp->data.str.len - pl;
277 for (c = smp->data.str.str; c <= end; c++) {
278 if (is_delimiter(*c, delimiters)) {
279 may_match = 1;
280 continue;
281 }
282
283 if (!may_match)
284 continue;
285
286 if (icase) {
287 if ((tolower(*c) == tolower(*ps)) &&
288 (strncasecmp(ps, c, pl) == 0) &&
289 (c == end || is_delimiter(c[pl], delimiters)))
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100290 return PAT_MATCH;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100291 } else {
292 if ((*c == *ps) &&
293 (strncmp(ps, c, pl) == 0) &&
294 (c == end || is_delimiter(c[pl], delimiters)))
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100295 return PAT_MATCH;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100296 }
297 may_match = 0;
298 }
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100299 return PAT_NOMATCH;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100300}
301
302/* Checks that the pattern is included inside the tested string, but enclosed
303 * between the delimiters '?' or '/' or at the beginning or end of the string.
304 * Delimiters at the beginning or end of the pattern are ignored.
305 */
Willy Tarreau0cba6072013-11-28 22:21:02 +0100306enum pat_match_res pat_match_dir(struct sample *smp, struct pattern *pattern)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100307{
308 return match_word(smp, pattern, make_4delim('/', '?', '?', '?'));
309}
310
311/* Checks that the pattern is included inside the tested string, but enclosed
312 * between the delmiters '/', '?', '.' or ":" or at the beginning or end of
313 * the string. Delimiters at the beginning or end of the pattern are ignored.
314 */
Willy Tarreau0cba6072013-11-28 22:21:02 +0100315enum pat_match_res pat_match_dom(struct sample *smp, struct pattern *pattern)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100316{
317 return match_word(smp, pattern, make_4delim('/', '?', '.', ':'));
318}
319
320/* Checks that the integer in <test> is included between min and max */
Willy Tarreau0cba6072013-11-28 22:21:02 +0100321enum pat_match_res pat_match_int(struct sample *smp, struct pattern *pattern)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100322{
323 if ((!pattern->val.range.min_set || pattern->val.range.min <= smp->data.uint) &&
324 (!pattern->val.range.max_set || smp->data.uint <= pattern->val.range.max))
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100325 return PAT_MATCH;
326 return PAT_NOMATCH;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100327}
328
329/* Checks that the length of the pattern in <test> is included between min and max */
Willy Tarreau0cba6072013-11-28 22:21:02 +0100330enum pat_match_res pat_match_len(struct sample *smp, struct pattern *pattern)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100331{
332 if ((!pattern->val.range.min_set || pattern->val.range.min <= smp->data.str.len) &&
333 (!pattern->val.range.max_set || smp->data.str.len <= pattern->val.range.max))
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100334 return PAT_MATCH;
335 return PAT_NOMATCH;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100336}
337
Willy Tarreau0cba6072013-11-28 22:21:02 +0100338enum pat_match_res pat_match_ip(struct sample *smp, struct pattern *pattern)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100339{
340 unsigned int v4; /* in network byte order */
341 struct in6_addr *v6;
342 int bits, pos;
343 struct in6_addr tmp6;
344
345 if (pattern->type == SMP_T_IPV4) {
346 if (smp->type == SMP_T_IPV4) {
347 v4 = smp->data.ipv4.s_addr;
348 }
349 else if (smp->type == SMP_T_IPV6) {
350 /* v4 match on a V6 sample. We want to check at least for
351 * the following forms :
352 * - ::ffff:ip:v4 (ipv4 mapped)
353 * - ::0000:ip:v4 (old ipv4 mapped)
354 * - 2002:ip:v4:: (6to4)
355 */
356 if (*(uint32_t*)&smp->data.ipv6.s6_addr[0] == 0 &&
357 *(uint32_t*)&smp->data.ipv6.s6_addr[4] == 0 &&
358 (*(uint32_t*)&smp->data.ipv6.s6_addr[8] == 0 ||
359 *(uint32_t*)&smp->data.ipv6.s6_addr[8] == htonl(0xFFFF))) {
360 v4 = *(uint32_t*)&smp->data.ipv6.s6_addr[12];
361 }
362 else if (*(uint16_t*)&smp->data.ipv6.s6_addr[0] == htons(0x2002)) {
363 v4 = htonl((ntohs(*(uint16_t*)&smp->data.ipv6.s6_addr[2]) << 16) +
364 ntohs(*(uint16_t*)&smp->data.ipv6.s6_addr[4]));
365 }
366 else
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100367 return PAT_NOMATCH;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100368 }
369 else
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100370 return PAT_NOMATCH;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100371
372 if (((v4 ^ pattern->val.ipv4.addr.s_addr) & pattern->val.ipv4.mask.s_addr) == 0)
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100373 return PAT_MATCH;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100374 else
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100375 return PAT_NOMATCH;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100376 }
377 else if (pattern->type == SMP_T_IPV6) {
378 if (smp->type == SMP_T_IPV4) {
379 /* Convert the IPv4 sample address to IPv4 with the
380 * mapping method using the ::ffff: prefix.
381 */
382 memset(&tmp6, 0, 10);
383 *(uint16_t*)&tmp6.s6_addr[10] = htons(0xffff);
384 *(uint32_t*)&tmp6.s6_addr[12] = smp->data.ipv4.s_addr;
385 v6 = &tmp6;
386 }
387 else if (smp->type == SMP_T_IPV6) {
388 v6 = &smp->data.ipv6;
389 }
390 else {
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100391 return PAT_NOMATCH;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100392 }
393
394 bits = pattern->val.ipv6.mask;
395 for (pos = 0; bits > 0; pos += 4, bits -= 32) {
396 v4 = *(uint32_t*)&v6->s6_addr[pos] ^ *(uint32_t*)&pattern->val.ipv6.addr.s6_addr[pos];
397 if (bits < 32)
398 v4 &= htonl((~0U) << (32-bits));
399 if (v4)
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100400 return PAT_NOMATCH;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100401 }
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100402 return PAT_MATCH;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100403 }
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100404 return PAT_NOMATCH;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100405}
406
407/* Lookup an IPv4 address in the expression's pattern tree using the longest
408 * match method. The node is returned if it exists, otherwise NULL.
409 */
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100410static void *pat_lookup_ip(struct sample *smp, struct pattern_expr *expr)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100411{
412 struct in_addr *s;
413
414 if (smp->type != SMP_T_IPV4)
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100415 return PAT_NOMATCH;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100416
417 s = &smp->data.ipv4;
418 return ebmb_lookup_longest(&expr->pattern_tree, &s->s_addr);
419}
420
421/* Parse a string. It is allocated and duplicated. */
Thierry FOURNIER0b2fe4a2013-12-06 20:33:50 +0100422int pat_parse_str(const char **text, struct pattern *pattern, enum pat_usage usage, int *opaque, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100423{
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100424 pattern->type = SMP_T_CSTR;
Thierry FOURNIERcc0e0b32013-12-06 16:56:40 +0100425 pattern->expect_type = SMP_T_CSTR;
Thierry FOURNIER0b2fe4a2013-12-06 20:33:50 +0100426 if (usage == PAT_U_COMPILE) {
427 pattern->ptr.str = strdup(*text);
428 if (!pattern->ptr.str) {
429 memprintf(err, "out of memory while loading string pattern");
430 return 0;
431 }
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100432 }
Thierry FOURNIER0b2fe4a2013-12-06 20:33:50 +0100433 else
434 pattern->ptr.str = (char *)*text;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100435 pattern->len = strlen(*text);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100436 return 1;
437}
438
439/* Parse a binary written in hexa. It is allocated. */
Thierry FOURNIER0b2fe4a2013-12-06 20:33:50 +0100440int pat_parse_bin(const char **text, struct pattern *pattern, enum pat_usage usage, int *opaque, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100441{
Thierry FOURNIER0b2fe4a2013-12-06 20:33:50 +0100442 struct chunk *trash;
443
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100444 pattern->type = SMP_T_CBIN;
Thierry FOURNIERcc0e0b32013-12-06 16:56:40 +0100445 pattern->expect_type = SMP_T_CBIN;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100446
Thierry FOURNIER0b2fe4a2013-12-06 20:33:50 +0100447 if (usage == PAT_U_COMPILE)
448 return parse_binary(*text, &pattern->ptr.str, &pattern->len, err);
449
450 trash = get_trash_chunk();
451 pattern->len = trash->size;
452 pattern->ptr.str = trash->str;
Willy Tarreau126d4062013-12-03 17:50:47 +0100453 return parse_binary(*text, &pattern->ptr.str, &pattern->len, err);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100454}
455
456/* Parse and concatenate all further strings into one. */
457int
Thierry FOURNIER0b2fe4a2013-12-06 20:33:50 +0100458pat_parse_strcat(const char **text, struct pattern *pattern, enum pat_usage usage, int *opaque, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100459{
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100460 int len = 0, i;
461 char *s;
Thierry FOURNIER0b2fe4a2013-12-06 20:33:50 +0100462 struct chunk *trash;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100463
464 for (i = 0; *text[i]; i++)
465 len += strlen(text[i])+1;
466
467 pattern->type = SMP_T_CSTR;
Thierry FOURNIER0b2fe4a2013-12-06 20:33:50 +0100468 if (usage == PAT_U_COMPILE) {
469 pattern->ptr.str = calloc(1, len);
470 if (!pattern->ptr.str) {
471 memprintf(err, "out of memory while loading pattern");
472 return 0;
473 }
474 }
475 else {
476 trash = get_trash_chunk();
477 if (trash->size < len) {
478 memprintf(err, "no space avalaible in the buffer. expect %d, provides %d",
479 len, trash->size);
480 return 0;
481 }
482 pattern->ptr.str = trash->str;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100483 }
484
Thierry FOURNIER0b2fe4a2013-12-06 20:33:50 +0100485 s = pattern->ptr.str;
486
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100487 for (i = 0; *text[i]; i++)
488 s += sprintf(s, i?" %s":"%s", text[i]);
489
490 pattern->len = len;
491
492 return i;
493}
494
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100495/* Free data allocated by pat_parse_reg */
496static void pat_free_reg(void *ptr)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100497{
498 regex_free(ptr);
499}
500
501/* Parse a regex. It is allocated. */
Thierry FOURNIER0b2fe4a2013-12-06 20:33:50 +0100502int pat_parse_reg(const char **text, struct pattern *pattern, enum pat_usage usage, int *opaque, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100503{
Thierry FOURNIER799c0422013-12-06 20:36:20 +0100504 struct my_regex *preg;
Thierry FOURNIER0b2fe4a2013-12-06 20:33:50 +0100505 struct chunk *trash;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100506
Thierry FOURNIER0b2fe4a2013-12-06 20:33:50 +0100507 if (usage == PAT_U_COMPILE) {
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100508
Thierry FOURNIER0b2fe4a2013-12-06 20:33:50 +0100509 preg = calloc(1, sizeof(*preg));
510 if (!preg) {
511 memprintf(err, "out of memory while loading pattern");
512 return 0;
513 }
514
515 if (!regex_comp(*text, preg, !(pattern->flags & PAT_F_IGNORE_CASE), 0, err)) {
516 free(preg);
517 return 0;
518 }
519 pattern->freeptrbuf = &pat_free_reg;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100520 }
Thierry FOURNIER0b2fe4a2013-12-06 20:33:50 +0100521 else {
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100522
Thierry FOURNIER0b2fe4a2013-12-06 20:33:50 +0100523 trash = get_trash_chunk();
524 if (trash->size < sizeof(*preg)) {
525 memprintf(err, "no space avalaible in the buffer. expect %d, provides %d",
526 (int)sizeof(*preg), trash->size);
527 return 0;
528 }
529
530 preg = (struct my_regex *)trash->str;
531 preg->regstr = (char *)*text;
532 pattern->freeptrbuf = NULL;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100533 }
534
535 pattern->ptr.reg = preg;
Thierry FOURNIERcc0e0b32013-12-06 16:56:40 +0100536 pattern->expect_type = SMP_T_CSTR;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100537 return 1;
538}
539
540/* Parse a range of positive integers delimited by either ':' or '-'. If only
541 * one integer is read, it is set as both min and max. An operator may be
542 * specified as the prefix, among this list of 5 :
543 *
544 * 0:eq, 1:gt, 2:ge, 3:lt, 4:le
545 *
546 * The default operator is "eq". It supports range matching. Ranges are
547 * rejected for other operators. The operator may be changed at any time.
548 * The operator is stored in the 'opaque' argument.
549 *
550 * If err is non-NULL, an error message will be returned there on errors and
551 * the caller will have to free it.
552 *
553 */
Thierry FOURNIER0b2fe4a2013-12-06 20:33:50 +0100554int pat_parse_int(const char **text, struct pattern *pattern, enum pat_usage usage, int *opaque, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100555{
556 signed long long i;
557 unsigned int j, last, skip = 0;
558 const char *ptr = *text;
559
560 pattern->type = SMP_T_UINT;
Thierry FOURNIERcc0e0b32013-12-06 16:56:40 +0100561 pattern->expect_type = SMP_T_UINT;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100562
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100563 while (!isdigit((unsigned char)*ptr)) {
564 switch (get_std_op(ptr)) {
565 case STD_OP_EQ: *opaque = 0; break;
566 case STD_OP_GT: *opaque = 1; break;
567 case STD_OP_GE: *opaque = 2; break;
568 case STD_OP_LT: *opaque = 3; break;
569 case STD_OP_LE: *opaque = 4; break;
570 default:
571 memprintf(err, "'%s' is neither a number nor a supported operator", ptr);
572 return 0;
573 }
574
575 skip++;
576 ptr = text[skip];
577 }
578
579 last = i = 0;
580 while (1) {
581 j = *ptr++;
582 if ((j == '-' || j == ':') && !last) {
583 last++;
584 pattern->val.range.min = i;
585 i = 0;
586 continue;
587 }
588 j -= '0';
589 if (j > 9)
590 // also catches the terminating zero
591 break;
592 i *= 10;
593 i += j;
594 }
595
596 if (last && *opaque >= 1 && *opaque <= 4) {
597 /* having a range with a min or a max is absurd */
598 memprintf(err, "integer range '%s' specified with a comparison operator", text[skip]);
599 return 0;
600 }
601
602 if (!last)
603 pattern->val.range.min = i;
604 pattern->val.range.max = i;
605
606 switch (*opaque) {
607 case 0: /* eq */
608 pattern->val.range.min_set = 1;
609 pattern->val.range.max_set = 1;
610 break;
611 case 1: /* gt */
612 pattern->val.range.min++; /* gt = ge + 1 */
613 case 2: /* ge */
614 pattern->val.range.min_set = 1;
615 pattern->val.range.max_set = 0;
616 break;
617 case 3: /* lt */
618 pattern->val.range.max--; /* lt = le - 1 */
619 case 4: /* le */
620 pattern->val.range.min_set = 0;
621 pattern->val.range.max_set = 1;
622 break;
623 }
624 return skip + 1;
625}
626
Thierry FOURNIER0b2fe4a2013-12-06 20:33:50 +0100627int pat_parse_len(const char **text, struct pattern *pattern, enum pat_usage usage, int *opaque, char **err)
Thierry FOURNIERcc0e0b32013-12-06 16:56:40 +0100628{
629 int ret;
630
Thierry FOURNIER0b2fe4a2013-12-06 20:33:50 +0100631 ret = pat_parse_int(text, pattern, usage, opaque, err);
Thierry FOURNIERcc0e0b32013-12-06 16:56:40 +0100632 pattern->expect_type = SMP_T_CSTR;
633 return ret;
634}
635
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100636/* Parse a range of positive 2-component versions delimited by either ':' or
637 * '-'. The version consists in a major and a minor, both of which must be
638 * smaller than 65536, because internally they will be represented as a 32-bit
639 * integer.
640 * If only one version is read, it is set as both min and max. Just like for
641 * pure integers, an operator may be specified as the prefix, among this list
642 * of 5 :
643 *
644 * 0:eq, 1:gt, 2:ge, 3:lt, 4:le
645 *
646 * The default operator is "eq". It supports range matching. Ranges are
647 * rejected for other operators. The operator may be changed at any time.
648 * The operator is stored in the 'opaque' argument. This allows constructs
649 * such as the following one :
650 *
651 * acl obsolete_ssl ssl_req_proto lt 3
652 * acl unsupported_ssl ssl_req_proto gt 3.1
653 * acl valid_ssl ssl_req_proto 3.0-3.1
654 *
655 */
Thierry FOURNIER0b2fe4a2013-12-06 20:33:50 +0100656int pat_parse_dotted_ver(const char **text, struct pattern *pattern, enum pat_usage usage, int *opaque, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100657{
658 signed long long i;
659 unsigned int j, last, skip = 0;
660 const char *ptr = *text;
661
662
663 while (!isdigit((unsigned char)*ptr)) {
664 switch (get_std_op(ptr)) {
665 case STD_OP_EQ: *opaque = 0; break;
666 case STD_OP_GT: *opaque = 1; break;
667 case STD_OP_GE: *opaque = 2; break;
668 case STD_OP_LT: *opaque = 3; break;
669 case STD_OP_LE: *opaque = 4; break;
670 default:
671 memprintf(err, "'%s' is neither a number nor a supported operator", ptr);
672 return 0;
673 }
674
675 skip++;
676 ptr = text[skip];
677 }
678
679 last = i = 0;
680 while (1) {
681 j = *ptr++;
682 if (j == '.') {
683 /* minor part */
684 if (i >= 65536)
685 return 0;
686 i <<= 16;
687 continue;
688 }
689 if ((j == '-' || j == ':') && !last) {
690 last++;
691 if (i < 65536)
692 i <<= 16;
693 pattern->val.range.min = i;
694 i = 0;
695 continue;
696 }
697 j -= '0';
698 if (j > 9)
699 // also catches the terminating zero
700 break;
701 i = (i & 0xFFFF0000) + (i & 0xFFFF) * 10;
702 i += j;
703 }
704
705 /* if we only got a major version, let's shift it now */
706 if (i < 65536)
707 i <<= 16;
708
709 if (last && *opaque >= 1 && *opaque <= 4) {
710 /* having a range with a min or a max is absurd */
711 memprintf(err, "version range '%s' specified with a comparison operator", text[skip]);
712 return 0;
713 }
714
Thierry FOURNIERcc0e0b32013-12-06 16:56:40 +0100715 pattern->expect_type = SMP_T_CSTR;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100716
717 if (!last)
718 pattern->val.range.min = i;
719 pattern->val.range.max = i;
720
721 switch (*opaque) {
722 case 0: /* eq */
723 pattern->val.range.min_set = 1;
724 pattern->val.range.max_set = 1;
725 break;
726 case 1: /* gt */
727 pattern->val.range.min++; /* gt = ge + 1 */
728 case 2: /* ge */
729 pattern->val.range.min_set = 1;
730 pattern->val.range.max_set = 0;
731 break;
732 case 3: /* lt */
733 pattern->val.range.max--; /* lt = le - 1 */
734 case 4: /* le */
735 pattern->val.range.min_set = 0;
736 pattern->val.range.max_set = 1;
737 break;
738 }
739 return skip + 1;
740}
741
742/* Parse an IP address and an optional mask in the form addr[/mask].
743 * The addr may either be an IPv4 address or a hostname. The mask
744 * may either be a dotted mask or a number of bits. Returns 1 if OK,
745 * otherwise 0. NOTE: IP address patterns are typed (IPV4/IPV6).
746 */
Thierry FOURNIER0b2fe4a2013-12-06 20:33:50 +0100747int pat_parse_ip(const char **text, struct pattern *pattern, enum pat_usage usage, int *opaque, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100748{
Thierry FOURNIERcc0e0b32013-12-06 16:56:40 +0100749 pattern->expect_type = SMP_T_ADDR;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100750 if (str2net(*text, &pattern->val.ipv4.addr, &pattern->val.ipv4.mask)) {
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100751 pattern->type = SMP_T_IPV4;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100752 return 1;
753 }
754 else if (str62net(*text, &pattern->val.ipv6.addr, &pattern->val.ipv6.mask)) {
755 /* no tree support right now */
756 pattern->type = SMP_T_IPV6;
757 return 1;
758 }
759 else {
760 memprintf(err, "'%s' is not a valid IPv4 or IPv6 address", *text);
761 return 0;
762 }
763}
764
765/* NB: does nothing if <pat> is NULL */
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100766void pattern_free(struct pattern *pat)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100767{
768 if (!pat)
769 return;
770
771 if (pat->ptr.ptr) {
772 if (pat->freeptrbuf)
773 pat->freeptrbuf(pat->ptr.ptr);
774
775 free(pat->ptr.ptr);
776 }
777
Thierry FOURNIERc64de3f2013-12-10 15:08:39 +0100778 free(pat->smp);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100779 free(pat);
780}
781
782void free_pattern_list(struct list *head)
783{
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100784 struct pattern *pat, *tmp;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100785 list_for_each_entry_safe(pat, tmp, head, list)
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100786 pattern_free(pat);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100787}
788
789void free_pattern_tree(struct eb_root *root)
790{
791 struct eb_node *node, *next;
Thierry FOURNIER3ce88c72013-12-09 11:29:46 +0100792 struct pat_idx_elt *elt;
793
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100794 node = eb_first(root);
795 while (node) {
796 next = eb_next(node);
797 eb_delete(node);
Thierry FOURNIER3ce88c72013-12-09 11:29:46 +0100798 elt = container_of(node, struct pat_idx_elt, node);
Thierry FOURNIERc64de3f2013-12-10 15:08:39 +0100799 free(elt->smp);
Thierry FOURNIER3ce88c72013-12-09 11:29:46 +0100800 free(elt);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100801 node = next;
802 }
803}
804
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100805void pattern_prune_expr(struct pattern_expr *expr)
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +0100806{
807 free_pattern_list(&expr->patterns);
808 free_pattern_tree(&expr->pattern_tree);
809 LIST_INIT(&expr->patterns);
810}
811
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100812void pattern_init_expr(struct pattern_expr *expr)
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +0100813{
814 LIST_INIT(&expr->patterns);
815 expr->pattern_tree = EB_ROOT_UNIQUE;
816}
817
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100818/* return 1 if the process is ok
819 * return -1 if the parser fail. The err message is filled.
820 * return -2 if out of memory
821 */
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100822int pattern_register(struct pattern_expr *expr, const char **args,
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100823 struct sample_storage *smp,
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100824 struct pattern **pattern,
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100825 int patflags, char **err)
826{
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100827 int opaque = 0;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100828 unsigned int mask = 0;
829 struct pat_idx_elt *node;
830 int len;
831 int ret;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100832
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100833 /* eat args */
834 while (**args) {
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100835
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100836 /* we keep the previous pattern along iterations as long as it's not used */
837 if (!*pattern)
838 *pattern = (struct pattern *)malloc(sizeof(**pattern));
839 if (!*pattern) {
840 memprintf(err, "out of memory while loading pattern");
841 return 0;
842 }
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100843
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100844 memset(*pattern, 0, sizeof(**pattern));
845 (*pattern)->flags = patflags;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100846
Thierry FOURNIER0b2fe4a2013-12-06 20:33:50 +0100847 ret = expr->parse(args, *pattern, PAT_U_COMPILE, &opaque, err);
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100848 if (!ret)
849 return 0;
850
851 /* each parser return the number of args eated */
852 args += ret;
853
854 /*
855 *
856 * SMP_T_CSTR tree indexation
857 *
858 * The match "pat_match_str()" can use tree.
859 *
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100860 */
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100861 if (expr->match == pat_match_str) {
862
863 /* If the flag PAT_F_IGNORE_CASE is set, we cannot use trees */
864 if ((*pattern)->flags & PAT_F_IGNORE_CASE)
865 goto just_chain_the_pattern;
866
867 /* Process the key len */
868 len = strlen((*pattern)->ptr.str) + 1;
869
870 /* node memory allocation */
871 node = calloc(1, sizeof(*node) + len);
872 if (!node) {
873 memprintf(err, "out of memory while loading pattern");
874 return 0;
875 }
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100876
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100877 /* copy the pointer to sample associated to this node */
878 node->smp = smp;
879
880 /* copy the string */
881 memcpy(node->node.key, (*pattern)->ptr.str, len);
882
883 /* the "map_parser_str()" function always duplicate string information */
884 free((*pattern)->ptr.str);
Willy Tarreau6762a302013-12-16 10:40:28 +0100885 (*pattern)->ptr.str = NULL;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100886
887 /* we pre-set the data pointer to the tree's head so that functions
888 * which are able to insert in a tree know where to do that.
889 *
890 * because "val" is an "union", the previous data are crushed.
891 */
892 (*pattern)->flags |= PAT_F_TREE;
893 (*pattern)->val.tree = &expr->pattern_tree;
894
895 /* index the new node */
896 if (ebst_insert((*pattern)->val.tree, &node->node) != &node->node)
897 free(node); /* was a duplicate */
898 }
899
900 /*
901 *
902 * SMP_T_IPV4 tree indexation
903 *
904 * The match "pat_match_ip()" can use tree.
905 *
906 */
907 else if (expr->match == pat_match_ip) {
908
909 /* Only IPv4 can be indexed */
910 if ((*pattern)->type != SMP_T_IPV4)
911 goto just_chain_the_pattern;
912
913 /* in IPv4 case, check if the mask is contiguous so that we can
914 * insert the network into the tree. A continuous mask has only
915 * ones on the left. This means that this mask + its lower bit
916 * added once again is null.
917 */
918 mask = ntohl((*pattern)->val.ipv4.mask.s_addr);
919 if (mask + (mask & -mask) != 0)
920 goto just_chain_the_pattern;
921 mask = mask ? 33 - flsnz(mask & -mask) : 0; /* equals cidr value */
922
923 /* node memory allocation */
924 node = calloc(1, sizeof(*node) + 4);
925 if (!node) {
926 memprintf(err, "out of memory while loading pattern");
927 return 0;
928 }
929
930 /* copy the pointer to sample associated to this node */
931 node->smp = smp;
932
933 /* FIXME: insert <addr>/<mask> into the tree here */
934 memcpy(node->node.key, &(*pattern)->val.ipv4.addr, 4); /* network byte order */
935
936 /* we pre-set the data pointer to the tree's head so that functions
937 * which are able to insert in a tree know where to do that.
938 *
939 * because "val" is an "union", the previous data are crushed.
940 */
941 (*pattern)->flags |= PAT_F_TREE;
942 (*pattern)->val.tree = &expr->pattern_tree;
943
944 /* Index the new node
945 * FIXME: insert <addr>/<mask> into the tree here
946 */
947 node->node.node.pfx = mask;
948 if (ebmb_insert_prefix((*pattern)->val.tree, &node->node, 4) != &node->node)
949 free(node); /* was a duplicate */
950 }
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100951
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100952 /*
953 *
954 * if the parser did not feed the tree, let's chain the pattern to the list
955 *
956 */
957 else {
958
959just_chain_the_pattern:
960
961 LIST_ADDQ(&expr->patterns, &(*pattern)->list);
962
963 /* copy the pointer to sample associated to this node */
964 (*pattern)->smp = smp;
965
966 /* get a new one */
967 *pattern = NULL;
968 }
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100969 }
970
971 return 1;
972}
973
974/* Reads patterns from a file. If <err_msg> is non-NULL, an error message will
975 * be returned there on errors and the caller will have to free it.
976 */
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100977int pattern_read_from_file(struct pattern_expr *expr,
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100978 const char *filename, int patflags,
979 char **err)
980{
981 FILE *file;
982 char *c;
983 char *arg;
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100984 struct pattern *pattern;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100985 int ret = 0;
986 int line = 0;
987 int code;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100988 const char *args[2];
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100989
990 file = fopen(filename, "r");
991 if (!file) {
992 memprintf(err, "failed to open pattern file <%s>", filename);
993 return 0;
994 }
995
996 /* now parse all patterns. The file may contain only one pattern per
997 * line. If the line contains spaces, they will be part of the pattern.
998 * The pattern stops at the first CR, LF or EOF encountered.
999 */
1000 pattern = NULL;
1001 while (fgets(trash.str, trash.size, file) != NULL) {
1002 line++;
1003 c = trash.str;
1004
1005 /* ignore lines beginning with a dash */
1006 if (*c == '#')
1007 continue;
1008
1009 /* strip leading spaces and tabs */
1010 while (*c == ' ' || *c == '\t')
1011 c++;
1012
1013
1014 arg = c;
1015 while (*c && *c != '\n' && *c != '\r')
1016 c++;
1017 *c = 0;
1018
1019 /* empty lines are ignored too */
1020 if (c == arg)
1021 continue;
1022
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001023 args[0] = arg;
1024 args[1] = "";
1025
1026 code = pattern_register(expr, args, NULL, &pattern, patflags, err);
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001027 if (code == -2) {
1028 memprintf(err, "out of memory when loading patterns from file <%s>", filename);
1029 goto out_close;
1030 }
1031 else if (code < 0) {
1032 memprintf(err, "%s when loading patterns from file <%s>", *err, filename);
1033 goto out_free_pattern;
1034 }
1035 }
1036
1037 ret = 1; /* success */
1038
1039 out_free_pattern:
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01001040 pattern_free(pattern);
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001041 out_close:
1042 fclose(file);
1043 return ret;
1044}
1045
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01001046/* This function matches a sample <smp> against a set of patterns presented in
1047 * pattern expression <expr>. Upon success, if <sample> is not NULL, it is fed
1048 * with the pointer associated with the matching pattern. This function returns
1049 * PAT_NOMATCH or PAT_MATCH.
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001050 */
Willy Tarreau0cba6072013-11-28 22:21:02 +01001051enum pat_match_res pattern_exec_match(struct pattern_expr *expr, struct sample *smp,
Thierry FOURNIER76090642013-12-10 15:03:38 +01001052 struct sample_storage **sample,
1053 struct pattern **pat, struct pat_idx_elt **idx_elt)
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001054{
Willy Tarreau0cba6072013-11-28 22:21:02 +01001055 enum pat_match_res pat_res = PAT_NOMATCH;
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01001056 struct pattern *pattern;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001057 struct ebmb_node *node = NULL;
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01001058 struct pat_idx_elt *elt;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001059
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01001060 if (expr->match == pat_match_nothing) {
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001061 if (smp->data.uint)
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01001062 pat_res |= PAT_MATCH;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001063 else
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01001064 pat_res |= PAT_NOMATCH;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001065 }
1066 else if (!expr->match) {
1067 /* just check for existence */
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01001068 pat_res |= PAT_MATCH;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001069 }
1070 else {
1071 if (!eb_is_empty(&expr->pattern_tree)) {
1072 /* a tree is present, let's check what type it is */
Thierry FOURNIERe3ded592013-12-06 15:36:54 +01001073 if (expr->match == pat_match_str) {
1074 if (sample_convert(smp, SMP_T_STR))
1075 node = pat_lookup_str(smp, expr);
1076 }
1077 else if (expr->match == pat_match_ip) {
1078 if (sample_convert(smp, SMP_T_IPV4))
1079 node = pat_lookup_ip(smp, expr);
1080 }
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001081 if (node) {
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01001082 pat_res |= PAT_MATCH;
1083 elt = ebmb_entry(node, struct pat_idx_elt, node);
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001084 if (sample)
1085 *sample = elt->smp;
Thierry FOURNIER76090642013-12-10 15:03:38 +01001086 if (idx_elt)
1087 *idx_elt = elt;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001088 }
1089 }
1090
1091 /* call the match() function for all tests on this value */
1092 list_for_each_entry(pattern, &expr->patterns, list) {
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01001093 if (pat_res == PAT_MATCH)
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001094 break;
Thierry FOURNIERe3ded592013-12-06 15:36:54 +01001095 if (sample_convert(smp, pattern->expect_type))
1096 pat_res |= expr->match(smp, pattern);
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001097 if (sample)
1098 *sample = pattern->smp;
Thierry FOURNIER76090642013-12-10 15:03:38 +01001099 if (pat)
1100 *pat = pattern;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001101 }
1102 }
1103
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01001104 return pat_res;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001105}
1106
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001107/* This function browse the pattern expr <expr> to lookup the key <key>. On
1108 * error it returns 0. On success, it returns 1 and fills either <pat_elt>
1109 * or <idx_elt> with the respectively matched pointers, and the other one with
1110 * NULL. Pointers are not set if they're passed as NULL.
1111 */
1112int pattern_lookup(const char *key, struct pattern_expr *expr,
1113 struct pattern **pat_elt, struct pat_idx_elt **idx_elt, char **err)
1114{
1115 struct pattern pattern;
1116 struct pattern *pat;
1117 struct ebmb_node *node;
1118 struct pat_idx_elt *elt;
1119 const char *args[2];
1120 int opaque = 0;
Willy Tarreau668ae532013-12-15 16:42:26 +01001121 unsigned int mask = 0;
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001122
1123 /* no real pattern */
1124 if (!expr->match || expr->match == pat_match_nothing)
1125 return 0;
1126
1127 /* build lookup pattern */
1128 args[0] = key;
1129 args[1] = "";
1130 if (!expr->parse(args, &pattern, PAT_U_LOOKUP, &opaque, NULL))
1131 return 0;
1132
1133 pat = NULL;
1134 elt = NULL;
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001135
Thierry FOURNIERc64de3f2013-12-10 15:08:39 +01001136 /* Try to look up the tree first. IPv6 is not indexed */
1137 if (!eb_is_empty(&expr->pattern_tree) && pattern.type != SMP_T_IPV6) {
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001138 /* Check the pattern type */
1139 if (pattern.type != SMP_T_STR &&
1140 pattern.type != SMP_T_CSTR &&
1141 pattern.type != SMP_T_IPV4) {
1142 memprintf(err, "Unexpected pattern type.");
1143 return 0;
1144 }
1145
1146 /* Convert mask. If the mask is not contiguous, ignore the lookup
1147 * in the tree, and browse the list.
1148 */
1149 if (expr->match == pat_match_ip) {
1150 mask = ntohl(pattern.val.ipv4.mask.s_addr);
1151 if (mask + (mask & -mask) != 0)
1152 goto browse_list;
1153 mask = mask ? 33 - flsnz(mask & -mask) : 0; /* equals cidr value */
1154 }
1155
1156 /* browse each node of the tree, and check string */
1157 if (expr->match == pat_match_str) {
1158 for (node = ebmb_first(&expr->pattern_tree);
1159 node;
1160 node = ebmb_next(node)) {
1161 elt = container_of(node, struct pat_idx_elt, node);
1162 if (strcmp(pattern.ptr.str, (char *)elt->node.key) == 0)
1163 goto found;
1164 }
1165 }
1166 else if (expr->match == pat_match_ip) {
1167 for (node = ebmb_first(&expr->pattern_tree);
1168 node;
1169 node = ebmb_next(node)) {
1170 elt = container_of(node, struct pat_idx_elt, node);
1171 if (elt->node.node.pfx == mask &&
1172 memcmp(&pattern.val.ipv4.addr.s_addr, elt->node.key, 4) == 0)
1173 goto found;
1174 }
1175 }
1176 }
1177
1178browse_list:
Thierry FOURNIERc64de3f2013-12-10 15:08:39 +01001179 elt = NULL;
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001180 if (expr->parse == pat_parse_int ||
1181 expr->parse == pat_parse_len) {
1182 list_for_each_entry(pat, &expr->patterns, list) {
1183 if (pat->flags & PAT_F_TREE)
1184 continue;
1185 if (pattern.val.range.min_set != pat->val.range.min_set)
1186 continue;
1187 if (pattern.val.range.max_set != pat->val.range.max_set)
1188 continue;
1189 if (pattern.val.range.min_set &&
1190 pattern.val.range.min != pat->val.range.min)
1191 continue;
1192 if (pattern.val.range.max_set &&
1193 pattern.val.range.max != pat->val.range.max)
1194 continue;
1195 goto found;
1196 }
1197 }
1198 else if (expr->parse == pat_parse_ip) {
1199 list_for_each_entry(pat, &expr->patterns, list) {
1200 if (pat->flags & PAT_F_TREE)
1201 continue;
1202 if (pattern.type != pat->type)
1203 continue;
1204 if (pattern.type == SMP_T_IPV4 &&
1205 memcmp(&pattern.val.ipv4.addr, &pat->val.ipv4.addr, sizeof(pat->val.ipv4.addr)) != 0)
1206 continue;
1207 if (pattern.type == SMP_T_IPV4 &&
1208 memcmp(&pattern.val.ipv4.mask, &pat->val.ipv4.mask, sizeof(pat->val.ipv4.addr)) != 0)
1209 continue;
1210 if (pattern.type == SMP_T_IPV6 &&
1211 memcmp(&pattern.val.ipv6.addr, &pat->val.ipv6.addr, sizeof(pat->val.ipv6.addr)) != 0)
1212 continue;
1213 if (pattern.type == SMP_T_IPV6 &&
1214 pattern.val.ipv6.mask != pat->val.ipv6.mask)
1215 continue;
1216 goto found;
1217 }
1218 }
1219 else if (expr->parse == pat_parse_str) {
1220 list_for_each_entry(pat, &expr->patterns, list) {
1221 if (pat->flags & PAT_F_TREE)
1222 continue;
1223 if (pattern.len != pat->len)
1224 continue;
1225 if ((pat->flags & PAT_F_IGNORE_CASE) &&
1226 strncasecmp(pattern.ptr.str, pat->ptr.str, pat->len) != 0)
1227 continue;
1228 if (strncmp(pattern.ptr.str, pat->ptr.str, pat->len) != 0)
1229 continue;
1230 goto found;
1231 }
1232 }
1233 else if (expr->parse == pat_parse_bin) {
1234 list_for_each_entry(pat, &expr->patterns, list) {
1235 if (pat->flags & PAT_F_TREE)
1236 continue;
1237 if (pattern.len != pat->len)
1238 continue;
1239 if (memcmp(pattern.ptr.ptr, pat->ptr.ptr, pat->len) != 0)
1240 continue;
1241 goto found;
1242 }
1243 }
1244 else if (expr->parse == pat_parse_reg) {
1245 list_for_each_entry(pat, &expr->patterns, list) {
1246 if (pat->flags & PAT_F_TREE)
1247 continue;
1248 if ((pat->flags & PAT_F_IGNORE_CASE) &&
1249 strcasecmp(pattern.ptr.reg->regstr, pat->ptr.reg->regstr) != 0)
1250 continue;
1251 if (strcmp(pattern.ptr.reg->regstr, pat->ptr.reg->regstr) != 0)
1252 continue;
1253 goto found;
1254 }
1255 }
1256
1257 /* if we get there, we didn't find the pattern */
1258 return 0;
1259found:
1260 if (idx_elt)
1261 *idx_elt = elt;
1262
1263 if (pat_elt)
1264 *pat_elt = pat;
1265
1266 return 1;
1267}