blob: 3ac8f0361b9dd7910ac17f3f1e43b5a33d028170 [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 FOURNIER7148ce62013-12-06 19:06:43 +010043int (*pat_parse_fcts[PAT_MATCH_NUM])(const char **, struct pattern *, 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 FOURNIER7148ce62013-12-06 19:06:43 +010097int pat_parse_nothing(const char **text, struct pattern *pattern, 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 FOURNIER7148ce62013-12-06 19:06:43 +0100422int pat_parse_str(const char **text, struct pattern *pattern, 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 FOURNIERed66c292013-11-28 11:05:19 +0100426 pattern->ptr.str = strdup(*text);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100427 if (!pattern->ptr.str) {
428 memprintf(err, "out of memory while loading string pattern");
429 return 0;
430 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100431 pattern->len = strlen(*text);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100432 return 1;
433}
434
435/* Parse a binary written in hexa. It is allocated. */
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100436int pat_parse_bin(const char **text, struct pattern *pattern, int *opaque, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100437{
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100438 pattern->type = SMP_T_CBIN;
Thierry FOURNIERcc0e0b32013-12-06 16:56:40 +0100439 pattern->expect_type = SMP_T_CBIN;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100440
Willy Tarreau126d4062013-12-03 17:50:47 +0100441 return parse_binary(*text, &pattern->ptr.str, &pattern->len, err);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100442}
443
444/* Parse and concatenate all further strings into one. */
445int
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100446pat_parse_strcat(const char **text, struct pattern *pattern, int *opaque, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100447{
448
449 int len = 0, i;
450 char *s;
451
452 for (i = 0; *text[i]; i++)
453 len += strlen(text[i])+1;
454
455 pattern->type = SMP_T_CSTR;
456 pattern->ptr.str = s = calloc(1, len);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100457 if (!pattern->ptr.str) {
458 memprintf(err, "out of memory while loading pattern");
459 return 0;
460 }
461
462 for (i = 0; *text[i]; i++)
463 s += sprintf(s, i?" %s":"%s", text[i]);
464
465 pattern->len = len;
466
467 return i;
468}
469
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100470/* Free data allocated by pat_parse_reg */
471static void pat_free_reg(void *ptr)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100472{
473 regex_free(ptr);
474}
475
476/* Parse a regex. It is allocated. */
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100477int pat_parse_reg(const char **text, struct pattern *pattern, int *opaque, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100478{
Thierry FOURNIER799c0422013-12-06 20:36:20 +0100479 struct my_regex *preg;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100480
481 preg = calloc(1, sizeof(*preg));
482
483 if (!preg) {
484 memprintf(err, "out of memory while loading pattern");
485 return 0;
486 }
487
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100488 if (!regex_comp(*text, preg, !(pattern->flags & PAT_F_IGNORE_CASE), 0, err)) {
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100489 free(preg);
490 return 0;
491 }
492
493 pattern->ptr.reg = preg;
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100494 pattern->freeptrbuf = &pat_free_reg;
Thierry FOURNIERcc0e0b32013-12-06 16:56:40 +0100495 pattern->expect_type = SMP_T_CSTR;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100496 return 1;
497}
498
499/* Parse a range of positive integers delimited by either ':' or '-'. If only
500 * one integer is read, it is set as both min and max. An operator may be
501 * specified as the prefix, among this list of 5 :
502 *
503 * 0:eq, 1:gt, 2:ge, 3:lt, 4:le
504 *
505 * The default operator is "eq". It supports range matching. Ranges are
506 * rejected for other operators. The operator may be changed at any time.
507 * The operator is stored in the 'opaque' argument.
508 *
509 * If err is non-NULL, an error message will be returned there on errors and
510 * the caller will have to free it.
511 *
512 */
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100513int pat_parse_int(const char **text, struct pattern *pattern, int *opaque, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100514{
515 signed long long i;
516 unsigned int j, last, skip = 0;
517 const char *ptr = *text;
518
519 pattern->type = SMP_T_UINT;
Thierry FOURNIERcc0e0b32013-12-06 16:56:40 +0100520 pattern->expect_type = SMP_T_UINT;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100521
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100522 while (!isdigit((unsigned char)*ptr)) {
523 switch (get_std_op(ptr)) {
524 case STD_OP_EQ: *opaque = 0; break;
525 case STD_OP_GT: *opaque = 1; break;
526 case STD_OP_GE: *opaque = 2; break;
527 case STD_OP_LT: *opaque = 3; break;
528 case STD_OP_LE: *opaque = 4; break;
529 default:
530 memprintf(err, "'%s' is neither a number nor a supported operator", ptr);
531 return 0;
532 }
533
534 skip++;
535 ptr = text[skip];
536 }
537
538 last = i = 0;
539 while (1) {
540 j = *ptr++;
541 if ((j == '-' || j == ':') && !last) {
542 last++;
543 pattern->val.range.min = i;
544 i = 0;
545 continue;
546 }
547 j -= '0';
548 if (j > 9)
549 // also catches the terminating zero
550 break;
551 i *= 10;
552 i += j;
553 }
554
555 if (last && *opaque >= 1 && *opaque <= 4) {
556 /* having a range with a min or a max is absurd */
557 memprintf(err, "integer range '%s' specified with a comparison operator", text[skip]);
558 return 0;
559 }
560
561 if (!last)
562 pattern->val.range.min = i;
563 pattern->val.range.max = i;
564
565 switch (*opaque) {
566 case 0: /* eq */
567 pattern->val.range.min_set = 1;
568 pattern->val.range.max_set = 1;
569 break;
570 case 1: /* gt */
571 pattern->val.range.min++; /* gt = ge + 1 */
572 case 2: /* ge */
573 pattern->val.range.min_set = 1;
574 pattern->val.range.max_set = 0;
575 break;
576 case 3: /* lt */
577 pattern->val.range.max--; /* lt = le - 1 */
578 case 4: /* le */
579 pattern->val.range.min_set = 0;
580 pattern->val.range.max_set = 1;
581 break;
582 }
583 return skip + 1;
584}
585
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100586int pat_parse_len(const char **text, struct pattern *pattern, int *opaque, char **err)
Thierry FOURNIERcc0e0b32013-12-06 16:56:40 +0100587{
588 int ret;
589
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100590 ret = pat_parse_int(text, pattern, opaque, err);
Thierry FOURNIERcc0e0b32013-12-06 16:56:40 +0100591 pattern->expect_type = SMP_T_CSTR;
592 return ret;
593}
594
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100595/* Parse a range of positive 2-component versions delimited by either ':' or
596 * '-'. The version consists in a major and a minor, both of which must be
597 * smaller than 65536, because internally they will be represented as a 32-bit
598 * integer.
599 * If only one version is read, it is set as both min and max. Just like for
600 * pure integers, an operator may be specified as the prefix, among this list
601 * of 5 :
602 *
603 * 0:eq, 1:gt, 2:ge, 3:lt, 4:le
604 *
605 * The default operator is "eq". It supports range matching. Ranges are
606 * rejected for other operators. The operator may be changed at any time.
607 * The operator is stored in the 'opaque' argument. This allows constructs
608 * such as the following one :
609 *
610 * acl obsolete_ssl ssl_req_proto lt 3
611 * acl unsupported_ssl ssl_req_proto gt 3.1
612 * acl valid_ssl ssl_req_proto 3.0-3.1
613 *
614 */
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100615int pat_parse_dotted_ver(const char **text, struct pattern *pattern, int *opaque, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100616{
617 signed long long i;
618 unsigned int j, last, skip = 0;
619 const char *ptr = *text;
620
621
622 while (!isdigit((unsigned char)*ptr)) {
623 switch (get_std_op(ptr)) {
624 case STD_OP_EQ: *opaque = 0; break;
625 case STD_OP_GT: *opaque = 1; break;
626 case STD_OP_GE: *opaque = 2; break;
627 case STD_OP_LT: *opaque = 3; break;
628 case STD_OP_LE: *opaque = 4; break;
629 default:
630 memprintf(err, "'%s' is neither a number nor a supported operator", ptr);
631 return 0;
632 }
633
634 skip++;
635 ptr = text[skip];
636 }
637
638 last = i = 0;
639 while (1) {
640 j = *ptr++;
641 if (j == '.') {
642 /* minor part */
643 if (i >= 65536)
644 return 0;
645 i <<= 16;
646 continue;
647 }
648 if ((j == '-' || j == ':') && !last) {
649 last++;
650 if (i < 65536)
651 i <<= 16;
652 pattern->val.range.min = i;
653 i = 0;
654 continue;
655 }
656 j -= '0';
657 if (j > 9)
658 // also catches the terminating zero
659 break;
660 i = (i & 0xFFFF0000) + (i & 0xFFFF) * 10;
661 i += j;
662 }
663
664 /* if we only got a major version, let's shift it now */
665 if (i < 65536)
666 i <<= 16;
667
668 if (last && *opaque >= 1 && *opaque <= 4) {
669 /* having a range with a min or a max is absurd */
670 memprintf(err, "version range '%s' specified with a comparison operator", text[skip]);
671 return 0;
672 }
673
Thierry FOURNIERcc0e0b32013-12-06 16:56:40 +0100674 pattern->expect_type = SMP_T_CSTR;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100675
676 if (!last)
677 pattern->val.range.min = i;
678 pattern->val.range.max = i;
679
680 switch (*opaque) {
681 case 0: /* eq */
682 pattern->val.range.min_set = 1;
683 pattern->val.range.max_set = 1;
684 break;
685 case 1: /* gt */
686 pattern->val.range.min++; /* gt = ge + 1 */
687 case 2: /* ge */
688 pattern->val.range.min_set = 1;
689 pattern->val.range.max_set = 0;
690 break;
691 case 3: /* lt */
692 pattern->val.range.max--; /* lt = le - 1 */
693 case 4: /* le */
694 pattern->val.range.min_set = 0;
695 pattern->val.range.max_set = 1;
696 break;
697 }
698 return skip + 1;
699}
700
701/* Parse an IP address and an optional mask in the form addr[/mask].
702 * The addr may either be an IPv4 address or a hostname. The mask
703 * may either be a dotted mask or a number of bits. Returns 1 if OK,
704 * otherwise 0. NOTE: IP address patterns are typed (IPV4/IPV6).
705 */
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100706int pat_parse_ip(const char **text, struct pattern *pattern, int *opaque, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100707{
Thierry FOURNIERcc0e0b32013-12-06 16:56:40 +0100708 pattern->expect_type = SMP_T_ADDR;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100709 if (str2net(*text, &pattern->val.ipv4.addr, &pattern->val.ipv4.mask)) {
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100710 pattern->type = SMP_T_IPV4;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100711 return 1;
712 }
713 else if (str62net(*text, &pattern->val.ipv6.addr, &pattern->val.ipv6.mask)) {
714 /* no tree support right now */
715 pattern->type = SMP_T_IPV6;
716 return 1;
717 }
718 else {
719 memprintf(err, "'%s' is not a valid IPv4 or IPv6 address", *text);
720 return 0;
721 }
722}
723
724/* NB: does nothing if <pat> is NULL */
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100725void pattern_free(struct pattern *pat)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100726{
727 if (!pat)
728 return;
729
730 if (pat->ptr.ptr) {
731 if (pat->freeptrbuf)
732 pat->freeptrbuf(pat->ptr.ptr);
733
734 free(pat->ptr.ptr);
735 }
736
737 free(pat);
738}
739
740void free_pattern_list(struct list *head)
741{
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100742 struct pattern *pat, *tmp;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100743 list_for_each_entry_safe(pat, tmp, head, list)
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100744 pattern_free(pat);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100745}
746
747void free_pattern_tree(struct eb_root *root)
748{
749 struct eb_node *node, *next;
Thierry FOURNIER3ce88c72013-12-09 11:29:46 +0100750 struct pat_idx_elt *elt;
751
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100752 node = eb_first(root);
753 while (node) {
754 next = eb_next(node);
755 eb_delete(node);
Thierry FOURNIER3ce88c72013-12-09 11:29:46 +0100756 elt = container_of(node, struct pat_idx_elt, node);
757 free(elt);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100758 node = next;
759 }
760}
761
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100762void pattern_prune_expr(struct pattern_expr *expr)
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +0100763{
764 free_pattern_list(&expr->patterns);
765 free_pattern_tree(&expr->pattern_tree);
766 LIST_INIT(&expr->patterns);
767}
768
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100769void pattern_init_expr(struct pattern_expr *expr)
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +0100770{
771 LIST_INIT(&expr->patterns);
772 expr->pattern_tree = EB_ROOT_UNIQUE;
773}
774
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100775/* return 1 if the process is ok
776 * return -1 if the parser fail. The err message is filled.
777 * return -2 if out of memory
778 */
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100779int pattern_register(struct pattern_expr *expr, const char **args,
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100780 struct sample_storage *smp,
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100781 struct pattern **pattern,
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100782 int patflags, char **err)
783{
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100784 int opaque = 0;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100785 unsigned int mask = 0;
786 struct pat_idx_elt *node;
787 int len;
788 int ret;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100789
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100790 /* eat args */
791 while (**args) {
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100792
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100793 /* we keep the previous pattern along iterations as long as it's not used */
794 if (!*pattern)
795 *pattern = (struct pattern *)malloc(sizeof(**pattern));
796 if (!*pattern) {
797 memprintf(err, "out of memory while loading pattern");
798 return 0;
799 }
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100800
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100801 memset(*pattern, 0, sizeof(**pattern));
802 (*pattern)->flags = patflags;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100803
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100804 ret = expr->parse(args, *pattern, &opaque, err);
805 if (!ret)
806 return 0;
807
808 /* each parser return the number of args eated */
809 args += ret;
810
811 /*
812 *
813 * SMP_T_CSTR tree indexation
814 *
815 * The match "pat_match_str()" can use tree.
816 *
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100817 */
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100818 if (expr->match == pat_match_str) {
819
820 /* If the flag PAT_F_IGNORE_CASE is set, we cannot use trees */
821 if ((*pattern)->flags & PAT_F_IGNORE_CASE)
822 goto just_chain_the_pattern;
823
824 /* Process the key len */
825 len = strlen((*pattern)->ptr.str) + 1;
826
827 /* node memory allocation */
828 node = calloc(1, sizeof(*node) + len);
829 if (!node) {
830 memprintf(err, "out of memory while loading pattern");
831 return 0;
832 }
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100833
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100834 /* copy the pointer to sample associated to this node */
835 node->smp = smp;
836
837 /* copy the string */
838 memcpy(node->node.key, (*pattern)->ptr.str, len);
839
840 /* the "map_parser_str()" function always duplicate string information */
841 free((*pattern)->ptr.str);
842
843 /* we pre-set the data pointer to the tree's head so that functions
844 * which are able to insert in a tree know where to do that.
845 *
846 * because "val" is an "union", the previous data are crushed.
847 */
848 (*pattern)->flags |= PAT_F_TREE;
849 (*pattern)->val.tree = &expr->pattern_tree;
850
851 /* index the new node */
852 if (ebst_insert((*pattern)->val.tree, &node->node) != &node->node)
853 free(node); /* was a duplicate */
854 }
855
856 /*
857 *
858 * SMP_T_IPV4 tree indexation
859 *
860 * The match "pat_match_ip()" can use tree.
861 *
862 */
863 else if (expr->match == pat_match_ip) {
864
865 /* Only IPv4 can be indexed */
866 if ((*pattern)->type != SMP_T_IPV4)
867 goto just_chain_the_pattern;
868
869 /* in IPv4 case, check if the mask is contiguous so that we can
870 * insert the network into the tree. A continuous mask has only
871 * ones on the left. This means that this mask + its lower bit
872 * added once again is null.
873 */
874 mask = ntohl((*pattern)->val.ipv4.mask.s_addr);
875 if (mask + (mask & -mask) != 0)
876 goto just_chain_the_pattern;
877 mask = mask ? 33 - flsnz(mask & -mask) : 0; /* equals cidr value */
878
879 /* node memory allocation */
880 node = calloc(1, sizeof(*node) + 4);
881 if (!node) {
882 memprintf(err, "out of memory while loading pattern");
883 return 0;
884 }
885
886 /* copy the pointer to sample associated to this node */
887 node->smp = smp;
888
889 /* FIXME: insert <addr>/<mask> into the tree here */
890 memcpy(node->node.key, &(*pattern)->val.ipv4.addr, 4); /* network byte order */
891
892 /* we pre-set the data pointer to the tree's head so that functions
893 * which are able to insert in a tree know where to do that.
894 *
895 * because "val" is an "union", the previous data are crushed.
896 */
897 (*pattern)->flags |= PAT_F_TREE;
898 (*pattern)->val.tree = &expr->pattern_tree;
899
900 /* Index the new node
901 * FIXME: insert <addr>/<mask> into the tree here
902 */
903 node->node.node.pfx = mask;
904 if (ebmb_insert_prefix((*pattern)->val.tree, &node->node, 4) != &node->node)
905 free(node); /* was a duplicate */
906 }
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100907
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100908 /*
909 *
910 * if the parser did not feed the tree, let's chain the pattern to the list
911 *
912 */
913 else {
914
915just_chain_the_pattern:
916
917 LIST_ADDQ(&expr->patterns, &(*pattern)->list);
918
919 /* copy the pointer to sample associated to this node */
920 (*pattern)->smp = smp;
921
922 /* get a new one */
923 *pattern = NULL;
924 }
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100925 }
926
927 return 1;
928}
929
930/* Reads patterns from a file. If <err_msg> is non-NULL, an error message will
931 * be returned there on errors and the caller will have to free it.
932 */
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100933int pattern_read_from_file(struct pattern_expr *expr,
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100934 const char *filename, int patflags,
935 char **err)
936{
937 FILE *file;
938 char *c;
939 char *arg;
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100940 struct pattern *pattern;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100941 int ret = 0;
942 int line = 0;
943 int code;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100944 const char *args[2];
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100945
946 file = fopen(filename, "r");
947 if (!file) {
948 memprintf(err, "failed to open pattern file <%s>", filename);
949 return 0;
950 }
951
952 /* now parse all patterns. The file may contain only one pattern per
953 * line. If the line contains spaces, they will be part of the pattern.
954 * The pattern stops at the first CR, LF or EOF encountered.
955 */
956 pattern = NULL;
957 while (fgets(trash.str, trash.size, file) != NULL) {
958 line++;
959 c = trash.str;
960
961 /* ignore lines beginning with a dash */
962 if (*c == '#')
963 continue;
964
965 /* strip leading spaces and tabs */
966 while (*c == ' ' || *c == '\t')
967 c++;
968
969
970 arg = c;
971 while (*c && *c != '\n' && *c != '\r')
972 c++;
973 *c = 0;
974
975 /* empty lines are ignored too */
976 if (c == arg)
977 continue;
978
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100979 args[0] = arg;
980 args[1] = "";
981
982 code = pattern_register(expr, args, NULL, &pattern, patflags, err);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100983 if (code == -2) {
984 memprintf(err, "out of memory when loading patterns from file <%s>", filename);
985 goto out_close;
986 }
987 else if (code < 0) {
988 memprintf(err, "%s when loading patterns from file <%s>", *err, filename);
989 goto out_free_pattern;
990 }
991 }
992
993 ret = 1; /* success */
994
995 out_free_pattern:
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100996 pattern_free(pattern);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100997 out_close:
998 fclose(file);
999 return ret;
1000}
1001
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01001002/* This function matches a sample <smp> against a set of patterns presented in
1003 * pattern expression <expr>. Upon success, if <sample> is not NULL, it is fed
1004 * with the pointer associated with the matching pattern. This function returns
1005 * PAT_NOMATCH or PAT_MATCH.
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001006 */
Willy Tarreau0cba6072013-11-28 22:21:02 +01001007enum pat_match_res pattern_exec_match(struct pattern_expr *expr, struct sample *smp,
1008 struct sample_storage **sample)
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001009{
Willy Tarreau0cba6072013-11-28 22:21:02 +01001010 enum pat_match_res pat_res = PAT_NOMATCH;
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01001011 struct pattern *pattern;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001012 struct ebmb_node *node = NULL;
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01001013 struct pat_idx_elt *elt;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001014
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01001015 if (expr->match == pat_match_nothing) {
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001016 if (smp->data.uint)
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01001017 pat_res |= PAT_MATCH;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001018 else
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01001019 pat_res |= PAT_NOMATCH;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001020 }
1021 else if (!expr->match) {
1022 /* just check for existence */
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01001023 pat_res |= PAT_MATCH;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001024 }
1025 else {
1026 if (!eb_is_empty(&expr->pattern_tree)) {
1027 /* a tree is present, let's check what type it is */
Thierry FOURNIERe3ded592013-12-06 15:36:54 +01001028 if (expr->match == pat_match_str) {
1029 if (sample_convert(smp, SMP_T_STR))
1030 node = pat_lookup_str(smp, expr);
1031 }
1032 else if (expr->match == pat_match_ip) {
1033 if (sample_convert(smp, SMP_T_IPV4))
1034 node = pat_lookup_ip(smp, expr);
1035 }
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001036 if (node) {
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01001037 pat_res |= PAT_MATCH;
1038 elt = ebmb_entry(node, struct pat_idx_elt, node);
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001039 if (sample)
1040 *sample = elt->smp;
1041 }
1042 }
1043
1044 /* call the match() function for all tests on this value */
1045 list_for_each_entry(pattern, &expr->patterns, list) {
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01001046 if (pat_res == PAT_MATCH)
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001047 break;
Thierry FOURNIERe3ded592013-12-06 15:36:54 +01001048 if (sample_convert(smp, pattern->expect_type))
1049 pat_res |= expr->match(smp, pattern);
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001050 if (sample)
1051 *sample = pattern->smp;
1052 }
1053 }
1054
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01001055 return pat_res;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001056}
1057