blob: c9e897872457b2b26e9396f99f867ab441339a9b [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
778 free(pat);
779}
780
781void free_pattern_list(struct list *head)
782{
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100783 struct pattern *pat, *tmp;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100784 list_for_each_entry_safe(pat, tmp, head, list)
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100785 pattern_free(pat);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100786}
787
788void free_pattern_tree(struct eb_root *root)
789{
790 struct eb_node *node, *next;
Thierry FOURNIER3ce88c72013-12-09 11:29:46 +0100791 struct pat_idx_elt *elt;
792
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100793 node = eb_first(root);
794 while (node) {
795 next = eb_next(node);
796 eb_delete(node);
Thierry FOURNIER3ce88c72013-12-09 11:29:46 +0100797 elt = container_of(node, struct pat_idx_elt, node);
798 free(elt);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100799 node = next;
800 }
801}
802
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100803void pattern_prune_expr(struct pattern_expr *expr)
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +0100804{
805 free_pattern_list(&expr->patterns);
806 free_pattern_tree(&expr->pattern_tree);
807 LIST_INIT(&expr->patterns);
808}
809
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100810void pattern_init_expr(struct pattern_expr *expr)
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +0100811{
812 LIST_INIT(&expr->patterns);
813 expr->pattern_tree = EB_ROOT_UNIQUE;
814}
815
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100816/* return 1 if the process is ok
817 * return -1 if the parser fail. The err message is filled.
818 * return -2 if out of memory
819 */
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100820int pattern_register(struct pattern_expr *expr, const char **args,
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100821 struct sample_storage *smp,
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100822 struct pattern **pattern,
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100823 int patflags, char **err)
824{
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100825 int opaque = 0;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100826 unsigned int mask = 0;
827 struct pat_idx_elt *node;
828 int len;
829 int ret;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100830
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100831 /* eat args */
832 while (**args) {
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100833
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100834 /* we keep the previous pattern along iterations as long as it's not used */
835 if (!*pattern)
836 *pattern = (struct pattern *)malloc(sizeof(**pattern));
837 if (!*pattern) {
838 memprintf(err, "out of memory while loading pattern");
839 return 0;
840 }
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100841
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100842 memset(*pattern, 0, sizeof(**pattern));
843 (*pattern)->flags = patflags;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100844
Thierry FOURNIER0b2fe4a2013-12-06 20:33:50 +0100845 ret = expr->parse(args, *pattern, PAT_U_COMPILE, &opaque, err);
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100846 if (!ret)
847 return 0;
848
849 /* each parser return the number of args eated */
850 args += ret;
851
852 /*
853 *
854 * SMP_T_CSTR tree indexation
855 *
856 * The match "pat_match_str()" can use tree.
857 *
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100858 */
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100859 if (expr->match == pat_match_str) {
860
861 /* If the flag PAT_F_IGNORE_CASE is set, we cannot use trees */
862 if ((*pattern)->flags & PAT_F_IGNORE_CASE)
863 goto just_chain_the_pattern;
864
865 /* Process the key len */
866 len = strlen((*pattern)->ptr.str) + 1;
867
868 /* node memory allocation */
869 node = calloc(1, sizeof(*node) + len);
870 if (!node) {
871 memprintf(err, "out of memory while loading pattern");
872 return 0;
873 }
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100874
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100875 /* copy the pointer to sample associated to this node */
876 node->smp = smp;
877
878 /* copy the string */
879 memcpy(node->node.key, (*pattern)->ptr.str, len);
880
881 /* the "map_parser_str()" function always duplicate string information */
882 free((*pattern)->ptr.str);
883
884 /* we pre-set the data pointer to the tree's head so that functions
885 * which are able to insert in a tree know where to do that.
886 *
887 * because "val" is an "union", the previous data are crushed.
888 */
889 (*pattern)->flags |= PAT_F_TREE;
890 (*pattern)->val.tree = &expr->pattern_tree;
891
892 /* index the new node */
893 if (ebst_insert((*pattern)->val.tree, &node->node) != &node->node)
894 free(node); /* was a duplicate */
895 }
896
897 /*
898 *
899 * SMP_T_IPV4 tree indexation
900 *
901 * The match "pat_match_ip()" can use tree.
902 *
903 */
904 else if (expr->match == pat_match_ip) {
905
906 /* Only IPv4 can be indexed */
907 if ((*pattern)->type != SMP_T_IPV4)
908 goto just_chain_the_pattern;
909
910 /* in IPv4 case, check if the mask is contiguous so that we can
911 * insert the network into the tree. A continuous mask has only
912 * ones on the left. This means that this mask + its lower bit
913 * added once again is null.
914 */
915 mask = ntohl((*pattern)->val.ipv4.mask.s_addr);
916 if (mask + (mask & -mask) != 0)
917 goto just_chain_the_pattern;
918 mask = mask ? 33 - flsnz(mask & -mask) : 0; /* equals cidr value */
919
920 /* node memory allocation */
921 node = calloc(1, sizeof(*node) + 4);
922 if (!node) {
923 memprintf(err, "out of memory while loading pattern");
924 return 0;
925 }
926
927 /* copy the pointer to sample associated to this node */
928 node->smp = smp;
929
930 /* FIXME: insert <addr>/<mask> into the tree here */
931 memcpy(node->node.key, &(*pattern)->val.ipv4.addr, 4); /* network byte order */
932
933 /* we pre-set the data pointer to the tree's head so that functions
934 * which are able to insert in a tree know where to do that.
935 *
936 * because "val" is an "union", the previous data are crushed.
937 */
938 (*pattern)->flags |= PAT_F_TREE;
939 (*pattern)->val.tree = &expr->pattern_tree;
940
941 /* Index the new node
942 * FIXME: insert <addr>/<mask> into the tree here
943 */
944 node->node.node.pfx = mask;
945 if (ebmb_insert_prefix((*pattern)->val.tree, &node->node, 4) != &node->node)
946 free(node); /* was a duplicate */
947 }
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100948
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100949 /*
950 *
951 * if the parser did not feed the tree, let's chain the pattern to the list
952 *
953 */
954 else {
955
956just_chain_the_pattern:
957
958 LIST_ADDQ(&expr->patterns, &(*pattern)->list);
959
960 /* copy the pointer to sample associated to this node */
961 (*pattern)->smp = smp;
962
963 /* get a new one */
964 *pattern = NULL;
965 }
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100966 }
967
968 return 1;
969}
970
971/* Reads patterns from a file. If <err_msg> is non-NULL, an error message will
972 * be returned there on errors and the caller will have to free it.
973 */
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100974int pattern_read_from_file(struct pattern_expr *expr,
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100975 const char *filename, int patflags,
976 char **err)
977{
978 FILE *file;
979 char *c;
980 char *arg;
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100981 struct pattern *pattern;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100982 int ret = 0;
983 int line = 0;
984 int code;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100985 const char *args[2];
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100986
987 file = fopen(filename, "r");
988 if (!file) {
989 memprintf(err, "failed to open pattern file <%s>", filename);
990 return 0;
991 }
992
993 /* now parse all patterns. The file may contain only one pattern per
994 * line. If the line contains spaces, they will be part of the pattern.
995 * The pattern stops at the first CR, LF or EOF encountered.
996 */
997 pattern = NULL;
998 while (fgets(trash.str, trash.size, file) != NULL) {
999 line++;
1000 c = trash.str;
1001
1002 /* ignore lines beginning with a dash */
1003 if (*c == '#')
1004 continue;
1005
1006 /* strip leading spaces and tabs */
1007 while (*c == ' ' || *c == '\t')
1008 c++;
1009
1010
1011 arg = c;
1012 while (*c && *c != '\n' && *c != '\r')
1013 c++;
1014 *c = 0;
1015
1016 /* empty lines are ignored too */
1017 if (c == arg)
1018 continue;
1019
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001020 args[0] = arg;
1021 args[1] = "";
1022
1023 code = pattern_register(expr, args, NULL, &pattern, patflags, err);
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001024 if (code == -2) {
1025 memprintf(err, "out of memory when loading patterns from file <%s>", filename);
1026 goto out_close;
1027 }
1028 else if (code < 0) {
1029 memprintf(err, "%s when loading patterns from file <%s>", *err, filename);
1030 goto out_free_pattern;
1031 }
1032 }
1033
1034 ret = 1; /* success */
1035
1036 out_free_pattern:
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01001037 pattern_free(pattern);
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001038 out_close:
1039 fclose(file);
1040 return ret;
1041}
1042
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01001043/* This function matches a sample <smp> against a set of patterns presented in
1044 * pattern expression <expr>. Upon success, if <sample> is not NULL, it is fed
1045 * with the pointer associated with the matching pattern. This function returns
1046 * PAT_NOMATCH or PAT_MATCH.
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001047 */
Willy Tarreau0cba6072013-11-28 22:21:02 +01001048enum pat_match_res pattern_exec_match(struct pattern_expr *expr, struct sample *smp,
Thierry FOURNIER76090642013-12-10 15:03:38 +01001049 struct sample_storage **sample,
1050 struct pattern **pat, struct pat_idx_elt **idx_elt)
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001051{
Willy Tarreau0cba6072013-11-28 22:21:02 +01001052 enum pat_match_res pat_res = PAT_NOMATCH;
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01001053 struct pattern *pattern;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001054 struct ebmb_node *node = NULL;
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01001055 struct pat_idx_elt *elt;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001056
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01001057 if (expr->match == pat_match_nothing) {
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001058 if (smp->data.uint)
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01001059 pat_res |= PAT_MATCH;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001060 else
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01001061 pat_res |= PAT_NOMATCH;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001062 }
1063 else if (!expr->match) {
1064 /* just check for existence */
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01001065 pat_res |= PAT_MATCH;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001066 }
1067 else {
1068 if (!eb_is_empty(&expr->pattern_tree)) {
1069 /* a tree is present, let's check what type it is */
Thierry FOURNIERe3ded592013-12-06 15:36:54 +01001070 if (expr->match == pat_match_str) {
1071 if (sample_convert(smp, SMP_T_STR))
1072 node = pat_lookup_str(smp, expr);
1073 }
1074 else if (expr->match == pat_match_ip) {
1075 if (sample_convert(smp, SMP_T_IPV4))
1076 node = pat_lookup_ip(smp, expr);
1077 }
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001078 if (node) {
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01001079 pat_res |= PAT_MATCH;
1080 elt = ebmb_entry(node, struct pat_idx_elt, node);
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001081 if (sample)
1082 *sample = elt->smp;
Thierry FOURNIER76090642013-12-10 15:03:38 +01001083 if (idx_elt)
1084 *idx_elt = elt;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001085 }
1086 }
1087
1088 /* call the match() function for all tests on this value */
1089 list_for_each_entry(pattern, &expr->patterns, list) {
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01001090 if (pat_res == PAT_MATCH)
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001091 break;
Thierry FOURNIERe3ded592013-12-06 15:36:54 +01001092 if (sample_convert(smp, pattern->expect_type))
1093 pat_res |= expr->match(smp, pattern);
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001094 if (sample)
1095 *sample = pattern->smp;
Thierry FOURNIER76090642013-12-10 15:03:38 +01001096 if (pat)
1097 *pat = pattern;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001098 }
1099 }
1100
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01001101 return pat_res;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001102}
1103