Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 1 | /* |
| 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 FOURNIER | e3ded59 | 2013-12-06 15:36:54 +0100 | [diff] [blame] | 23 | #include <proto/sample.h> |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 24 | |
| 25 | #include <ebsttree.h> |
| 26 | |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 27 | char *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 FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 41 | }; |
| 42 | |
Thierry FOURNIER | 580c32c | 2014-01-24 10:58:12 +0100 | [diff] [blame] | 43 | int (*pat_parse_fcts[PAT_MATCH_NUM])(const char *, struct pattern *, enum pat_usage, char **) = { |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 44 | [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 FOURNIER | cc0e0b3 | 2013-12-06 16:56:40 +0100 | [diff] [blame] | 49 | [PAT_MATCH_LEN] = pat_parse_len, |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 50 | [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 FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 57 | }; |
| 58 | |
Willy Tarreau | 0cba607 | 2013-11-28 22:21:02 +0100 | [diff] [blame] | 59 | enum pat_match_res (*pat_match_fcts[PAT_MATCH_NUM])(struct sample *, struct pattern *) = { |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 60 | [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 FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 73 | }; |
| 74 | |
Thierry FOURNIER | e3ded59 | 2013-12-06 15:36:54 +0100 | [diff] [blame] | 75 | /* Just used for checking configuration compatibility */ |
| 76 | int 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 FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 92 | /* |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 93 | * |
| 94 | * The following functions are not exported and are used by internals process |
| 95 | * of pattern matching |
| 96 | * |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 97 | */ |
| 98 | |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 99 | /* Lookup an IPv4 address in the expression's pattern tree using the longest |
| 100 | * match method. The node is returned if it exists, otherwise NULL. |
| 101 | */ |
| 102 | static void *pat_lookup_ip(struct sample *smp, struct pattern_expr *expr) |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 103 | { |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 104 | struct in_addr *s; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 105 | |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 106 | if (smp->type != SMP_T_IPV4) |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 107 | return PAT_NOMATCH; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 108 | |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 109 | s = &smp->data.ipv4; |
| 110 | return ebmb_lookup_longest(&expr->pattern_tree, &s->s_addr); |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 111 | } |
| 112 | |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 113 | /* Free data allocated by pat_parse_reg */ |
| 114 | static void pat_free_reg(void *ptr) |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 115 | { |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 116 | regex_free(ptr); |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | /* Lookup a string in the expression's pattern tree. The node is returned if it |
| 120 | * exists, otherwise NULL. |
| 121 | */ |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 122 | static void *pat_lookup_str(struct sample *smp, struct pattern_expr *expr) |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 123 | { |
| 124 | /* data are stored in a tree */ |
| 125 | struct ebmb_node *node; |
| 126 | char prev; |
| 127 | |
| 128 | /* we may have to force a trailing zero on the test pattern */ |
| 129 | prev = smp->data.str.str[smp->data.str.len]; |
| 130 | if (prev) |
| 131 | smp->data.str.str[smp->data.str.len] = '\0'; |
| 132 | node = ebst_lookup(&expr->pattern_tree, smp->data.str.str); |
| 133 | if (prev) |
| 134 | smp->data.str.str[smp->data.str.len] = prev; |
| 135 | return node; |
| 136 | } |
| 137 | |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 138 | /* Background: Fast way to find a zero byte in a word |
| 139 | * http://graphics.stanford.edu/~seander/bithacks.html#ZeroInWord |
| 140 | * hasZeroByte = (v - 0x01010101UL) & ~v & 0x80808080UL; |
| 141 | * |
| 142 | * To look for 4 different byte values, xor the word with those bytes and |
| 143 | * then check for zero bytes: |
| 144 | * |
| 145 | * v = (((unsigned char)c * 0x1010101U) ^ delimiter) |
| 146 | * where <delimiter> is the 4 byte values to look for (as an uint) |
| 147 | * and <c> is the character that is being tested |
| 148 | */ |
| 149 | static inline unsigned int is_delimiter(unsigned char c, unsigned int mask) |
| 150 | { |
| 151 | mask ^= (c * 0x01010101); /* propagate the char to all 4 bytes */ |
| 152 | return (mask - 0x01010101) & ~mask & 0x80808080U; |
| 153 | } |
| 154 | |
| 155 | static inline unsigned int make_4delim(unsigned char d1, unsigned char d2, unsigned char d3, unsigned char d4) |
| 156 | { |
| 157 | return d1 << 24 | d2 << 16 | d3 << 8 | d4; |
| 158 | } |
| 159 | |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 160 | |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 161 | /* |
| 162 | * |
| 163 | * These functions are exported and may be used by any other component. |
| 164 | * |
| 165 | * The following functions are used for parsing pattern matching |
Thierry FOURNIER | 580c32c | 2014-01-24 10:58:12 +0100 | [diff] [blame] | 166 | * input value. The <text> contain the string to be parsed. <pattern> |
| 167 | * must be a preallocated pattern. The pat_parse_* functions fill this |
| 168 | * structure with the parsed value. <usage> can be PAT_U_COMPILE or |
| 169 | * PAT_U_LOOKUP. If the value PAT_U_COMPILE is used memory is allocated |
| 170 | * for filling the pattern. If the value PAT_U_LOOKUP is set, the parser |
| 171 | * use "trash" or return pointers to the input strings. In both cases, |
| 172 | * the caller must use the value PAT_U_LOOKUP with caution. <err> is |
| 173 | * filled with an error message built with memprintf() function. |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 174 | * |
Thierry FOURNIER | 580c32c | 2014-01-24 10:58:12 +0100 | [diff] [blame] | 175 | * In succes case, the pat_parse_* function return 1. If the function |
| 176 | * fail, it returns 0 and <err> is filled. |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 177 | * |
| 178 | */ |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 179 | |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 180 | /* ignore the current line */ |
Thierry FOURNIER | 580c32c | 2014-01-24 10:58:12 +0100 | [diff] [blame] | 181 | int pat_parse_nothing(const char *text, struct pattern *pattern, enum pat_usage usage, char **err) |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 182 | { |
| 183 | return 1; |
| 184 | } |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 185 | |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 186 | /* Parse a string. It is allocated and duplicated. */ |
Thierry FOURNIER | 580c32c | 2014-01-24 10:58:12 +0100 | [diff] [blame] | 187 | int pat_parse_str(const char *text, struct pattern *pattern, enum pat_usage usage, char **err) |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 188 | { |
| 189 | pattern->type = SMP_T_CSTR; |
| 190 | pattern->expect_type = SMP_T_CSTR; |
| 191 | if (usage == PAT_U_COMPILE) { |
Thierry FOURNIER | 580c32c | 2014-01-24 10:58:12 +0100 | [diff] [blame] | 192 | pattern->ptr.str = strdup(text); |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 193 | if (!pattern->ptr.str) { |
| 194 | memprintf(err, "out of memory while loading string pattern"); |
| 195 | return 0; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 196 | } |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 197 | } |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 198 | else |
Thierry FOURNIER | 580c32c | 2014-01-24 10:58:12 +0100 | [diff] [blame] | 199 | pattern->ptr.str = (char *)text; |
| 200 | pattern->len = strlen(text); |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 201 | return 1; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 202 | } |
| 203 | |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 204 | /* Parse a binary written in hexa. It is allocated. */ |
Thierry FOURNIER | 580c32c | 2014-01-24 10:58:12 +0100 | [diff] [blame] | 205 | int pat_parse_bin(const char *text, struct pattern *pattern, enum pat_usage usage, char **err) |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 206 | { |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 207 | struct chunk *trash; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 208 | |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 209 | pattern->type = SMP_T_CBIN; |
| 210 | pattern->expect_type = SMP_T_CBIN; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 211 | |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 212 | if (usage == PAT_U_COMPILE) |
| 213 | /* If the parse_binary fails, it returns 0. In succes case, it returns |
| 214 | * the length of the arsed binary content. The functions pat_parse_* |
| 215 | * must return 0 if fail and the number of elements eated from **text |
| 216 | * if not fail. In succes case, this function eat always 1 elements. |
| 217 | * The double operator "!" converts the range "1-n" to "1". |
| 218 | */ |
Thierry FOURNIER | 580c32c | 2014-01-24 10:58:12 +0100 | [diff] [blame] | 219 | return !!parse_binary(text, &pattern->ptr.str, &pattern->len, err); |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 220 | |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 221 | trash = get_trash_chunk(); |
| 222 | pattern->len = trash->size; |
| 223 | pattern->ptr.str = trash->str; |
Thierry FOURNIER | 580c32c | 2014-01-24 10:58:12 +0100 | [diff] [blame] | 224 | return !!parse_binary(text, &pattern->ptr.str, &pattern->len, err); |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 225 | } |
| 226 | |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 227 | /* Parse a regex. It is allocated. */ |
Thierry FOURNIER | 580c32c | 2014-01-24 10:58:12 +0100 | [diff] [blame] | 228 | int pat_parse_reg(const char *text, struct pattern *pattern, enum pat_usage usage, char **err) |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 229 | { |
Thierry FOURNIER | 799c042 | 2013-12-06 20:36:20 +0100 | [diff] [blame] | 230 | struct my_regex *preg; |
Thierry FOURNIER | 0b2fe4a | 2013-12-06 20:33:50 +0100 | [diff] [blame] | 231 | struct chunk *trash; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 232 | |
Thierry FOURNIER | 0b2fe4a | 2013-12-06 20:33:50 +0100 | [diff] [blame] | 233 | if (usage == PAT_U_COMPILE) { |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 234 | |
Thierry FOURNIER | 0b2fe4a | 2013-12-06 20:33:50 +0100 | [diff] [blame] | 235 | preg = calloc(1, sizeof(*preg)); |
| 236 | if (!preg) { |
| 237 | memprintf(err, "out of memory while loading pattern"); |
| 238 | return 0; |
| 239 | } |
| 240 | |
Thierry FOURNIER | 580c32c | 2014-01-24 10:58:12 +0100 | [diff] [blame] | 241 | if (!regex_comp(text, preg, !(pattern->flags & PAT_F_IGNORE_CASE), 0, err)) { |
Thierry FOURNIER | 0b2fe4a | 2013-12-06 20:33:50 +0100 | [diff] [blame] | 242 | free(preg); |
| 243 | return 0; |
| 244 | } |
| 245 | pattern->freeptrbuf = &pat_free_reg; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 246 | } |
Thierry FOURNIER | 0b2fe4a | 2013-12-06 20:33:50 +0100 | [diff] [blame] | 247 | else { |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 248 | |
Thierry FOURNIER | 0b2fe4a | 2013-12-06 20:33:50 +0100 | [diff] [blame] | 249 | trash = get_trash_chunk(); |
| 250 | if (trash->size < sizeof(*preg)) { |
| 251 | memprintf(err, "no space avalaible in the buffer. expect %d, provides %d", |
| 252 | (int)sizeof(*preg), trash->size); |
| 253 | return 0; |
| 254 | } |
| 255 | |
| 256 | preg = (struct my_regex *)trash->str; |
Thierry FOURNIER | 580c32c | 2014-01-24 10:58:12 +0100 | [diff] [blame] | 257 | preg->regstr = (char *)text; |
Thierry FOURNIER | 0b2fe4a | 2013-12-06 20:33:50 +0100 | [diff] [blame] | 258 | pattern->freeptrbuf = NULL; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | pattern->ptr.reg = preg; |
Thierry FOURNIER | cc0e0b3 | 2013-12-06 16:56:40 +0100 | [diff] [blame] | 262 | pattern->expect_type = SMP_T_CSTR; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 263 | return 1; |
| 264 | } |
| 265 | |
| 266 | /* Parse a range of positive integers delimited by either ':' or '-'. If only |
| 267 | * one integer is read, it is set as both min and max. An operator may be |
| 268 | * specified as the prefix, among this list of 5 : |
| 269 | * |
| 270 | * 0:eq, 1:gt, 2:ge, 3:lt, 4:le |
| 271 | * |
| 272 | * The default operator is "eq". It supports range matching. Ranges are |
| 273 | * rejected for other operators. The operator may be changed at any time. |
| 274 | * The operator is stored in the 'opaque' argument. |
| 275 | * |
| 276 | * If err is non-NULL, an error message will be returned there on errors and |
Thierry FOURNIER | 511e947 | 2014-01-23 17:40:34 +0100 | [diff] [blame] | 277 | * the caller will have to free it. The function returns zero on error, and |
| 278 | * non-zero on success. |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 279 | * |
| 280 | */ |
Thierry FOURNIER | 580c32c | 2014-01-24 10:58:12 +0100 | [diff] [blame] | 281 | int pat_parse_int(const char *text, struct pattern *pattern, enum pat_usage usage, char **err) |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 282 | { |
Thierry FOURNIER | 580c32c | 2014-01-24 10:58:12 +0100 | [diff] [blame] | 283 | const char *ptr = text; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 284 | |
| 285 | pattern->type = SMP_T_UINT; |
Thierry FOURNIER | cc0e0b3 | 2013-12-06 16:56:40 +0100 | [diff] [blame] | 286 | pattern->expect_type = SMP_T_UINT; |
Thierry FOURNIER | 7148ce6 | 2013-12-06 19:06:43 +0100 | [diff] [blame] | 287 | |
Thierry FOURNIER | 511e947 | 2014-01-23 17:40:34 +0100 | [diff] [blame] | 288 | /* Empty string is not valid */ |
Thierry FOURNIER | 580c32c | 2014-01-24 10:58:12 +0100 | [diff] [blame] | 289 | if (!*text) |
Thierry FOURNIER | 511e947 | 2014-01-23 17:40:34 +0100 | [diff] [blame] | 290 | goto not_valid_range; |
| 291 | |
| 292 | /* Search ':' or '-' separator. */ |
| 293 | while (*ptr != '\0' && *ptr != ':' && *ptr != '-') |
| 294 | ptr++; |
| 295 | |
| 296 | /* If separator not found. */ |
| 297 | if (!*ptr) { |
Thierry FOURNIER | 580c32c | 2014-01-24 10:58:12 +0100 | [diff] [blame] | 298 | if (strl2llrc(text, ptr - text, &pattern->val.range.min) != 0) { |
| 299 | memprintf(err, "'%s' is not a number", text); |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 300 | return 0; |
| 301 | } |
Thierry FOURNIER | 511e947 | 2014-01-23 17:40:34 +0100 | [diff] [blame] | 302 | pattern->val.range.max = pattern->val.range.min; |
| 303 | pattern->val.range.min_set = 1; |
| 304 | pattern->val.range.max_set = 1; |
| 305 | return 1; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 306 | } |
| 307 | |
Thierry FOURNIER | 511e947 | 2014-01-23 17:40:34 +0100 | [diff] [blame] | 308 | /* If the separator is the first character. */ |
Thierry FOURNIER | 580c32c | 2014-01-24 10:58:12 +0100 | [diff] [blame] | 309 | if (ptr == text && *(ptr + 1) != '\0') { |
Thierry FOURNIER | 511e947 | 2014-01-23 17:40:34 +0100 | [diff] [blame] | 310 | if (strl2llrc(ptr + 1, strlen(ptr + 1), &pattern->val.range.max) != 0) |
| 311 | goto not_valid_range; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 312 | |
Thierry FOURNIER | 511e947 | 2014-01-23 17:40:34 +0100 | [diff] [blame] | 313 | pattern->val.range.min_set = 0; |
| 314 | pattern->val.range.max_set = 1; |
| 315 | return 1; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 316 | } |
| 317 | |
Thierry FOURNIER | 511e947 | 2014-01-23 17:40:34 +0100 | [diff] [blame] | 318 | /* If separator is the last character. */ |
| 319 | if (*(ptr + 1) == '\0') { |
Thierry FOURNIER | 580c32c | 2014-01-24 10:58:12 +0100 | [diff] [blame] | 320 | if (strl2llrc(text, ptr - text, &pattern->val.range.min) != 0) |
Thierry FOURNIER | 511e947 | 2014-01-23 17:40:34 +0100 | [diff] [blame] | 321 | goto not_valid_range; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 322 | |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 323 | pattern->val.range.min_set = 1; |
| 324 | pattern->val.range.max_set = 0; |
Thierry FOURNIER | 511e947 | 2014-01-23 17:40:34 +0100 | [diff] [blame] | 325 | return 1; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 326 | } |
Thierry FOURNIER | 511e947 | 2014-01-23 17:40:34 +0100 | [diff] [blame] | 327 | |
| 328 | /* Else, parse two numbers. */ |
Thierry FOURNIER | 580c32c | 2014-01-24 10:58:12 +0100 | [diff] [blame] | 329 | if (strl2llrc(text, ptr - text, &pattern->val.range.min) != 0) |
Thierry FOURNIER | 511e947 | 2014-01-23 17:40:34 +0100 | [diff] [blame] | 330 | goto not_valid_range; |
| 331 | |
| 332 | if (strl2llrc(ptr + 1, strlen(ptr + 1), &pattern->val.range.max) != 0) |
| 333 | goto not_valid_range; |
| 334 | |
| 335 | if (pattern->val.range.min > pattern->val.range.max) |
| 336 | goto not_valid_range; |
| 337 | |
| 338 | pattern->val.range.min_set = 1; |
| 339 | pattern->val.range.max_set = 1; |
| 340 | return 1; |
| 341 | |
| 342 | not_valid_range: |
Thierry FOURNIER | 580c32c | 2014-01-24 10:58:12 +0100 | [diff] [blame] | 343 | memprintf(err, "'%s' is not a valid number range", text); |
Thierry FOURNIER | 511e947 | 2014-01-23 17:40:34 +0100 | [diff] [blame] | 344 | return 0; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 345 | } |
| 346 | |
Thierry FOURNIER | 580c32c | 2014-01-24 10:58:12 +0100 | [diff] [blame] | 347 | int pat_parse_len(const char *text, struct pattern *pattern, enum pat_usage usage, char **err) |
Thierry FOURNIER | cc0e0b3 | 2013-12-06 16:56:40 +0100 | [diff] [blame] | 348 | { |
| 349 | int ret; |
| 350 | |
Thierry FOURNIER | 580c32c | 2014-01-24 10:58:12 +0100 | [diff] [blame] | 351 | ret = pat_parse_int(text, pattern, usage, err); |
Thierry FOURNIER | cc0e0b3 | 2013-12-06 16:56:40 +0100 | [diff] [blame] | 352 | pattern->expect_type = SMP_T_CSTR; |
| 353 | return ret; |
| 354 | } |
| 355 | |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 356 | /* Parse a range of positive 2-component versions delimited by either ':' or |
| 357 | * '-'. The version consists in a major and a minor, both of which must be |
| 358 | * smaller than 65536, because internally they will be represented as a 32-bit |
| 359 | * integer. |
| 360 | * If only one version is read, it is set as both min and max. Just like for |
| 361 | * pure integers, an operator may be specified as the prefix, among this list |
| 362 | * of 5 : |
| 363 | * |
| 364 | * 0:eq, 1:gt, 2:ge, 3:lt, 4:le |
| 365 | * |
| 366 | * The default operator is "eq". It supports range matching. Ranges are |
| 367 | * rejected for other operators. The operator may be changed at any time. |
| 368 | * The operator is stored in the 'opaque' argument. This allows constructs |
| 369 | * such as the following one : |
| 370 | * |
| 371 | * acl obsolete_ssl ssl_req_proto lt 3 |
| 372 | * acl unsupported_ssl ssl_req_proto gt 3.1 |
| 373 | * acl valid_ssl ssl_req_proto 3.0-3.1 |
| 374 | * |
| 375 | */ |
Thierry FOURNIER | 580c32c | 2014-01-24 10:58:12 +0100 | [diff] [blame] | 376 | int pat_parse_dotted_ver(const char *text, struct pattern *pattern, enum pat_usage usage, char **err) |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 377 | { |
Thierry FOURNIER | 580c32c | 2014-01-24 10:58:12 +0100 | [diff] [blame] | 378 | const char *ptr = text; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 379 | |
Thierry FOURNIER | 511e947 | 2014-01-23 17:40:34 +0100 | [diff] [blame] | 380 | pattern->type = SMP_T_UINT; |
| 381 | pattern->expect_type = SMP_T_UINT; |
| 382 | |
| 383 | /* Search ':' or '-' separator. */ |
| 384 | while (*ptr != '\0' && *ptr != ':' && *ptr != '-') |
| 385 | ptr++; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 386 | |
Thierry FOURNIER | 511e947 | 2014-01-23 17:40:34 +0100 | [diff] [blame] | 387 | /* If separator not found. */ |
Thierry FOURNIER | 580c32c | 2014-01-24 10:58:12 +0100 | [diff] [blame] | 388 | if (*ptr == '\0' && ptr > text) { |
| 389 | if (strl2llrc_dotted(text, ptr-text, &pattern->val.range.min) != 0) { |
| 390 | memprintf(err, "'%s' is not a dotted number", text); |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 391 | return 0; |
| 392 | } |
Thierry FOURNIER | 511e947 | 2014-01-23 17:40:34 +0100 | [diff] [blame] | 393 | pattern->val.range.max = pattern->val.range.min; |
| 394 | pattern->val.range.min_set = 1; |
| 395 | pattern->val.range.max_set = 1; |
| 396 | return 1; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 397 | } |
| 398 | |
Thierry FOURNIER | 511e947 | 2014-01-23 17:40:34 +0100 | [diff] [blame] | 399 | /* If the separator is the first character. */ |
Thierry FOURNIER | 580c32c | 2014-01-24 10:58:12 +0100 | [diff] [blame] | 400 | if (ptr == text && *(ptr+1) != '\0') { |
Thierry FOURNIER | 511e947 | 2014-01-23 17:40:34 +0100 | [diff] [blame] | 401 | if (strl2llrc_dotted(ptr+1, strlen(ptr+1), &pattern->val.range.max) != 0) { |
Thierry FOURNIER | 580c32c | 2014-01-24 10:58:12 +0100 | [diff] [blame] | 402 | memprintf(err, "'%s' is not a valid dotted number range", text); |
Thierry FOURNIER | 511e947 | 2014-01-23 17:40:34 +0100 | [diff] [blame] | 403 | return 0; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 404 | } |
Thierry FOURNIER | 511e947 | 2014-01-23 17:40:34 +0100 | [diff] [blame] | 405 | pattern->val.range.min_set = 0; |
| 406 | pattern->val.range.max_set = 1; |
| 407 | return 1; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 408 | } |
| 409 | |
Thierry FOURNIER | 511e947 | 2014-01-23 17:40:34 +0100 | [diff] [blame] | 410 | /* If separator is the last character. */ |
Thierry FOURNIER | 580c32c | 2014-01-24 10:58:12 +0100 | [diff] [blame] | 411 | if (ptr == &text[strlen(text)-1]) { |
| 412 | if (strl2llrc_dotted(text, ptr-text, &pattern->val.range.min) != 0) { |
| 413 | memprintf(err, "'%s' is not a valid dotted number range", text); |
Thierry FOURNIER | 511e947 | 2014-01-23 17:40:34 +0100 | [diff] [blame] | 414 | return 0; |
| 415 | } |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 416 | pattern->val.range.min_set = 1; |
| 417 | pattern->val.range.max_set = 0; |
Thierry FOURNIER | 511e947 | 2014-01-23 17:40:34 +0100 | [diff] [blame] | 418 | return 1; |
| 419 | } |
| 420 | |
| 421 | /* Else, parse two numbers. */ |
Thierry FOURNIER | 580c32c | 2014-01-24 10:58:12 +0100 | [diff] [blame] | 422 | if (strl2llrc_dotted(text, ptr-text, &pattern->val.range.min) != 0) { |
| 423 | memprintf(err, "'%s' is not a valid dotted number range", text); |
Thierry FOURNIER | 511e947 | 2014-01-23 17:40:34 +0100 | [diff] [blame] | 424 | return 0; |
| 425 | } |
| 426 | if (strl2llrc_dotted(ptr+1, strlen(ptr+1), &pattern->val.range.max) != 0) { |
Thierry FOURNIER | 580c32c | 2014-01-24 10:58:12 +0100 | [diff] [blame] | 427 | memprintf(err, "'%s' is not a valid dotted number range", text); |
Thierry FOURNIER | 511e947 | 2014-01-23 17:40:34 +0100 | [diff] [blame] | 428 | return 0; |
| 429 | } |
| 430 | if (pattern->val.range.min > pattern->val.range.max) { |
Thierry FOURNIER | 580c32c | 2014-01-24 10:58:12 +0100 | [diff] [blame] | 431 | memprintf(err, "'%s' is not a valid dotted number range", text); |
Thierry FOURNIER | 511e947 | 2014-01-23 17:40:34 +0100 | [diff] [blame] | 432 | return 0; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 433 | } |
Thierry FOURNIER | 511e947 | 2014-01-23 17:40:34 +0100 | [diff] [blame] | 434 | pattern->val.range.min_set = 1; |
| 435 | pattern->val.range.max_set = 1; |
| 436 | return 1; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | /* Parse an IP address and an optional mask in the form addr[/mask]. |
| 440 | * The addr may either be an IPv4 address or a hostname. The mask |
| 441 | * may either be a dotted mask or a number of bits. Returns 1 if OK, |
| 442 | * otherwise 0. NOTE: IP address patterns are typed (IPV4/IPV6). |
| 443 | */ |
Thierry FOURNIER | 580c32c | 2014-01-24 10:58:12 +0100 | [diff] [blame] | 444 | int pat_parse_ip(const char *text, struct pattern *pattern, enum pat_usage usage, char **err) |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 445 | { |
Thierry FOURNIER | cc0e0b3 | 2013-12-06 16:56:40 +0100 | [diff] [blame] | 446 | pattern->expect_type = SMP_T_ADDR; |
Thierry FOURNIER | 580c32c | 2014-01-24 10:58:12 +0100 | [diff] [blame] | 447 | if (str2net(text, &pattern->val.ipv4.addr, &pattern->val.ipv4.mask)) { |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 448 | pattern->type = SMP_T_IPV4; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 449 | return 1; |
| 450 | } |
Thierry FOURNIER | 580c32c | 2014-01-24 10:58:12 +0100 | [diff] [blame] | 451 | else if (str62net(text, &pattern->val.ipv6.addr, &pattern->val.ipv6.mask)) { |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 452 | /* no tree support right now */ |
| 453 | pattern->type = SMP_T_IPV6; |
| 454 | return 1; |
| 455 | } |
| 456 | else { |
Thierry FOURNIER | 580c32c | 2014-01-24 10:58:12 +0100 | [diff] [blame] | 457 | memprintf(err, "'%s' is not a valid IPv4 or IPv6 address", text); |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 458 | return 0; |
| 459 | } |
| 460 | } |
| 461 | |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 462 | /* |
| 463 | * |
| 464 | * These functions are exported and may be used by any other component. |
| 465 | * |
| 466 | * This fucntion just take a sample <smp> and check if this sample match |
| 467 | * with the pattern <pattern>. This fucntion return just PAT_MATCH or |
| 468 | * PAT_NOMATCH. |
| 469 | * |
| 470 | */ |
| 471 | |
| 472 | /* always return false */ |
| 473 | enum pat_match_res pat_match_nothing(struct sample *smp, struct pattern *pattern) |
| 474 | { |
| 475 | return PAT_NOMATCH; |
| 476 | } |
| 477 | |
| 478 | |
| 479 | /* NB: For two strings to be identical, it is required that their lengths match */ |
| 480 | enum pat_match_res pat_match_str(struct sample *smp, struct pattern *pattern) |
| 481 | { |
| 482 | int icase; |
| 483 | |
| 484 | if (pattern->len != smp->data.str.len) |
| 485 | return PAT_NOMATCH; |
| 486 | |
| 487 | icase = pattern->flags & PAT_F_IGNORE_CASE; |
| 488 | if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) == 0) || |
| 489 | (!icase && strncmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) == 0)) |
| 490 | return PAT_MATCH; |
| 491 | return PAT_NOMATCH; |
| 492 | } |
| 493 | |
| 494 | /* NB: For two binaries buf to be identical, it is required that their lengths match */ |
| 495 | enum pat_match_res pat_match_bin(struct sample *smp, struct pattern *pattern) |
| 496 | { |
| 497 | if (pattern->len != smp->data.str.len) |
| 498 | return PAT_NOMATCH; |
| 499 | |
| 500 | if (memcmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) == 0) |
| 501 | return PAT_MATCH; |
| 502 | return PAT_NOMATCH; |
| 503 | } |
| 504 | |
| 505 | /* Executes a regex. It temporarily changes the data to add a trailing zero, |
| 506 | * and restores the previous character when leaving. |
| 507 | */ |
| 508 | enum pat_match_res pat_match_reg(struct sample *smp, struct pattern *pattern) |
| 509 | { |
| 510 | if (regex_exec(pattern->ptr.reg, smp->data.str.str, smp->data.str.len) == 0) |
| 511 | return PAT_MATCH; |
| 512 | return PAT_NOMATCH; |
| 513 | } |
| 514 | |
| 515 | /* Checks that the pattern matches the beginning of the tested string. */ |
| 516 | enum pat_match_res pat_match_beg(struct sample *smp, struct pattern *pattern) |
| 517 | { |
| 518 | int icase; |
| 519 | |
| 520 | if (pattern->len > smp->data.str.len) |
| 521 | return PAT_NOMATCH; |
| 522 | |
| 523 | icase = pattern->flags & PAT_F_IGNORE_CASE; |
| 524 | if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str, pattern->len) != 0) || |
| 525 | (!icase && strncmp(pattern->ptr.str, smp->data.str.str, pattern->len) != 0)) |
| 526 | return PAT_NOMATCH; |
| 527 | return PAT_MATCH; |
| 528 | } |
| 529 | |
| 530 | /* Checks that the pattern matches the end of the tested string. */ |
| 531 | enum pat_match_res pat_match_end(struct sample *smp, struct pattern *pattern) |
| 532 | { |
| 533 | int icase; |
| 534 | |
| 535 | if (pattern->len > smp->data.str.len) |
| 536 | return PAT_NOMATCH; |
| 537 | icase = pattern->flags & PAT_F_IGNORE_CASE; |
| 538 | if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str + smp->data.str.len - pattern->len, pattern->len) != 0) || |
| 539 | (!icase && strncmp(pattern->ptr.str, smp->data.str.str + smp->data.str.len - pattern->len, pattern->len) != 0)) |
| 540 | return PAT_NOMATCH; |
| 541 | return PAT_MATCH; |
| 542 | } |
| 543 | |
| 544 | /* Checks that the pattern is included inside the tested string. |
| 545 | * NB: Suboptimal, should be rewritten using a Boyer-Moore method. |
| 546 | */ |
| 547 | enum pat_match_res pat_match_sub(struct sample *smp, struct pattern *pattern) |
| 548 | { |
| 549 | int icase; |
| 550 | char *end; |
| 551 | char *c; |
| 552 | |
| 553 | if (pattern->len > smp->data.str.len) |
| 554 | return PAT_NOMATCH; |
| 555 | |
| 556 | end = smp->data.str.str + smp->data.str.len - pattern->len; |
| 557 | icase = pattern->flags & PAT_F_IGNORE_CASE; |
| 558 | if (icase) { |
| 559 | for (c = smp->data.str.str; c <= end; c++) { |
| 560 | if (tolower(*c) != tolower(*pattern->ptr.str)) |
| 561 | continue; |
| 562 | if (strncasecmp(pattern->ptr.str, c, pattern->len) == 0) |
| 563 | return PAT_MATCH; |
| 564 | } |
| 565 | } else { |
| 566 | for (c = smp->data.str.str; c <= end; c++) { |
| 567 | if (*c != *pattern->ptr.str) |
| 568 | continue; |
| 569 | if (strncmp(pattern->ptr.str, c, pattern->len) == 0) |
| 570 | return PAT_MATCH; |
| 571 | } |
| 572 | } |
| 573 | return PAT_NOMATCH; |
| 574 | } |
| 575 | |
| 576 | /* This one is used by other real functions. It checks that the pattern is |
| 577 | * included inside the tested string, but enclosed between the specified |
| 578 | * delimiters or at the beginning or end of the string. The delimiters are |
| 579 | * provided as an unsigned int made by make_4delim() and match up to 4 different |
| 580 | * delimiters. Delimiters are stripped at the beginning and end of the pattern. |
| 581 | */ |
| 582 | static int match_word(struct sample *smp, struct pattern *pattern, unsigned int delimiters) |
| 583 | { |
| 584 | int may_match, icase; |
| 585 | char *c, *end; |
| 586 | char *ps; |
| 587 | int pl; |
| 588 | |
| 589 | pl = pattern->len; |
| 590 | ps = pattern->ptr.str; |
| 591 | |
| 592 | while (pl > 0 && is_delimiter(*ps, delimiters)) { |
| 593 | pl--; |
| 594 | ps++; |
| 595 | } |
| 596 | |
| 597 | while (pl > 0 && is_delimiter(ps[pl - 1], delimiters)) |
| 598 | pl--; |
| 599 | |
| 600 | if (pl > smp->data.str.len) |
| 601 | return PAT_NOMATCH; |
| 602 | |
| 603 | may_match = 1; |
| 604 | icase = pattern->flags & PAT_F_IGNORE_CASE; |
| 605 | end = smp->data.str.str + smp->data.str.len - pl; |
| 606 | for (c = smp->data.str.str; c <= end; c++) { |
| 607 | if (is_delimiter(*c, delimiters)) { |
| 608 | may_match = 1; |
| 609 | continue; |
| 610 | } |
| 611 | |
| 612 | if (!may_match) |
| 613 | continue; |
| 614 | |
| 615 | if (icase) { |
| 616 | if ((tolower(*c) == tolower(*ps)) && |
| 617 | (strncasecmp(ps, c, pl) == 0) && |
| 618 | (c == end || is_delimiter(c[pl], delimiters))) |
| 619 | return PAT_MATCH; |
| 620 | } else { |
| 621 | if ((*c == *ps) && |
| 622 | (strncmp(ps, c, pl) == 0) && |
| 623 | (c == end || is_delimiter(c[pl], delimiters))) |
| 624 | return PAT_MATCH; |
| 625 | } |
| 626 | may_match = 0; |
| 627 | } |
| 628 | return PAT_NOMATCH; |
| 629 | } |
| 630 | |
| 631 | /* Checks that the pattern is included inside the tested string, but enclosed |
| 632 | * between the delimiters '?' or '/' or at the beginning or end of the string. |
| 633 | * Delimiters at the beginning or end of the pattern are ignored. |
| 634 | */ |
| 635 | enum pat_match_res pat_match_dir(struct sample *smp, struct pattern *pattern) |
| 636 | { |
| 637 | return match_word(smp, pattern, make_4delim('/', '?', '?', '?')); |
| 638 | } |
| 639 | |
| 640 | /* Checks that the pattern is included inside the tested string, but enclosed |
| 641 | * between the delmiters '/', '?', '.' or ":" or at the beginning or end of |
| 642 | * the string. Delimiters at the beginning or end of the pattern are ignored. |
| 643 | */ |
| 644 | enum pat_match_res pat_match_dom(struct sample *smp, struct pattern *pattern) |
| 645 | { |
| 646 | return match_word(smp, pattern, make_4delim('/', '?', '.', ':')); |
| 647 | } |
| 648 | |
| 649 | /* Checks that the integer in <test> is included between min and max */ |
| 650 | enum pat_match_res pat_match_int(struct sample *smp, struct pattern *pattern) |
| 651 | { |
| 652 | if ((!pattern->val.range.min_set || pattern->val.range.min <= smp->data.uint) && |
| 653 | (!pattern->val.range.max_set || smp->data.uint <= pattern->val.range.max)) |
| 654 | return PAT_MATCH; |
| 655 | return PAT_NOMATCH; |
| 656 | } |
| 657 | |
| 658 | /* Checks that the length of the pattern in <test> is included between min and max */ |
| 659 | enum pat_match_res pat_match_len(struct sample *smp, struct pattern *pattern) |
| 660 | { |
| 661 | if ((!pattern->val.range.min_set || pattern->val.range.min <= smp->data.str.len) && |
| 662 | (!pattern->val.range.max_set || smp->data.str.len <= pattern->val.range.max)) |
| 663 | return PAT_MATCH; |
| 664 | return PAT_NOMATCH; |
| 665 | } |
| 666 | |
| 667 | enum pat_match_res pat_match_ip(struct sample *smp, struct pattern *pattern) |
| 668 | { |
| 669 | unsigned int v4; /* in network byte order */ |
| 670 | struct in6_addr *v6; |
| 671 | int bits, pos; |
| 672 | struct in6_addr tmp6; |
| 673 | |
| 674 | if (pattern->type == SMP_T_IPV4) { |
| 675 | if (smp->type == SMP_T_IPV4) { |
| 676 | v4 = smp->data.ipv4.s_addr; |
| 677 | } |
| 678 | else if (smp->type == SMP_T_IPV6) { |
| 679 | /* v4 match on a V6 sample. We want to check at least for |
| 680 | * the following forms : |
| 681 | * - ::ffff:ip:v4 (ipv4 mapped) |
| 682 | * - ::0000:ip:v4 (old ipv4 mapped) |
| 683 | * - 2002:ip:v4:: (6to4) |
| 684 | */ |
| 685 | if (*(uint32_t*)&smp->data.ipv6.s6_addr[0] == 0 && |
| 686 | *(uint32_t*)&smp->data.ipv6.s6_addr[4] == 0 && |
| 687 | (*(uint32_t*)&smp->data.ipv6.s6_addr[8] == 0 || |
| 688 | *(uint32_t*)&smp->data.ipv6.s6_addr[8] == htonl(0xFFFF))) { |
| 689 | v4 = *(uint32_t*)&smp->data.ipv6.s6_addr[12]; |
| 690 | } |
| 691 | else if (*(uint16_t*)&smp->data.ipv6.s6_addr[0] == htons(0x2002)) { |
| 692 | v4 = htonl((ntohs(*(uint16_t*)&smp->data.ipv6.s6_addr[2]) << 16) + |
| 693 | ntohs(*(uint16_t*)&smp->data.ipv6.s6_addr[4])); |
| 694 | } |
| 695 | else |
| 696 | return PAT_NOMATCH; |
| 697 | } |
| 698 | else |
| 699 | return PAT_NOMATCH; |
| 700 | |
| 701 | if (((v4 ^ pattern->val.ipv4.addr.s_addr) & pattern->val.ipv4.mask.s_addr) == 0) |
| 702 | return PAT_MATCH; |
| 703 | else |
| 704 | return PAT_NOMATCH; |
| 705 | } |
| 706 | else if (pattern->type == SMP_T_IPV6) { |
| 707 | if (smp->type == SMP_T_IPV4) { |
| 708 | /* Convert the IPv4 sample address to IPv4 with the |
| 709 | * mapping method using the ::ffff: prefix. |
| 710 | */ |
| 711 | memset(&tmp6, 0, 10); |
| 712 | *(uint16_t*)&tmp6.s6_addr[10] = htons(0xffff); |
| 713 | *(uint32_t*)&tmp6.s6_addr[12] = smp->data.ipv4.s_addr; |
| 714 | v6 = &tmp6; |
| 715 | } |
| 716 | else if (smp->type == SMP_T_IPV6) { |
| 717 | v6 = &smp->data.ipv6; |
| 718 | } |
| 719 | else { |
| 720 | return PAT_NOMATCH; |
| 721 | } |
| 722 | |
| 723 | bits = pattern->val.ipv6.mask; |
| 724 | for (pos = 0; bits > 0; pos += 4, bits -= 32) { |
| 725 | v4 = *(uint32_t*)&v6->s6_addr[pos] ^ *(uint32_t*)&pattern->val.ipv6.addr.s6_addr[pos]; |
| 726 | if (bits < 32) |
| 727 | v4 &= htonl((~0U) << (32-bits)); |
| 728 | if (v4) |
| 729 | return PAT_NOMATCH; |
| 730 | } |
| 731 | return PAT_MATCH; |
| 732 | } |
| 733 | return PAT_NOMATCH; |
| 734 | } |
| 735 | |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 736 | /* NB: does nothing if <pat> is NULL */ |
Thierry FOURNIER | 3ead5b9 | 2013-12-13 12:12:18 +0100 | [diff] [blame^] | 737 | void pattern_free(struct pattern_list *pat) |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 738 | { |
| 739 | if (!pat) |
| 740 | return; |
| 741 | |
Thierry FOURNIER | 3ead5b9 | 2013-12-13 12:12:18 +0100 | [diff] [blame^] | 742 | if (pat->pat.ptr.ptr) { |
| 743 | if (pat->pat.freeptrbuf) |
| 744 | pat->pat.freeptrbuf(pat->pat.ptr.ptr); |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 745 | |
Thierry FOURNIER | 3ead5b9 | 2013-12-13 12:12:18 +0100 | [diff] [blame^] | 746 | free(pat->pat.ptr.ptr); |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 747 | } |
| 748 | |
Thierry FOURNIER | 3ead5b9 | 2013-12-13 12:12:18 +0100 | [diff] [blame^] | 749 | free(pat->pat.smp); |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 750 | free(pat); |
| 751 | } |
| 752 | |
| 753 | void free_pattern_list(struct list *head) |
| 754 | { |
Thierry FOURNIER | 3ead5b9 | 2013-12-13 12:12:18 +0100 | [diff] [blame^] | 755 | struct pattern_list *pat, *tmp; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 756 | list_for_each_entry_safe(pat, tmp, head, list) |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 757 | pattern_free(pat); |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 758 | } |
| 759 | |
| 760 | void free_pattern_tree(struct eb_root *root) |
| 761 | { |
| 762 | struct eb_node *node, *next; |
Thierry FOURNIER | 3ce88c7 | 2013-12-09 11:29:46 +0100 | [diff] [blame] | 763 | struct pat_idx_elt *elt; |
| 764 | |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 765 | node = eb_first(root); |
| 766 | while (node) { |
| 767 | next = eb_next(node); |
| 768 | eb_delete(node); |
Thierry FOURNIER | 3ce88c7 | 2013-12-09 11:29:46 +0100 | [diff] [blame] | 769 | elt = container_of(node, struct pat_idx_elt, node); |
Thierry FOURNIER | c64de3f | 2013-12-10 15:08:39 +0100 | [diff] [blame] | 770 | free(elt->smp); |
Thierry FOURNIER | 3ce88c7 | 2013-12-09 11:29:46 +0100 | [diff] [blame] | 771 | free(elt); |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 772 | node = next; |
| 773 | } |
| 774 | } |
| 775 | |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 776 | void pattern_prune_expr(struct pattern_expr *expr) |
Thierry FOURNIER | d163e1c | 2013-11-28 11:41:23 +0100 | [diff] [blame] | 777 | { |
| 778 | free_pattern_list(&expr->patterns); |
| 779 | free_pattern_tree(&expr->pattern_tree); |
| 780 | LIST_INIT(&expr->patterns); |
| 781 | } |
| 782 | |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 783 | void pattern_init_expr(struct pattern_expr *expr) |
Thierry FOURNIER | d163e1c | 2013-11-28 11:41:23 +0100 | [diff] [blame] | 784 | { |
| 785 | LIST_INIT(&expr->patterns); |
| 786 | expr->pattern_tree = EB_ROOT_UNIQUE; |
| 787 | } |
| 788 | |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 789 | /* return 1 if the process is ok |
| 790 | * return -1 if the parser fail. The err message is filled. |
| 791 | * return -2 if out of memory |
| 792 | */ |
Thierry FOURNIER | 972028f | 2014-01-23 17:53:31 +0100 | [diff] [blame] | 793 | int pattern_register(struct pattern_expr *expr, const char *arg, |
| 794 | struct sample_storage *smp, |
Thierry FOURNIER | 3ead5b9 | 2013-12-13 12:12:18 +0100 | [diff] [blame^] | 795 | struct pattern_list **pattern, |
Thierry FOURNIER | 972028f | 2014-01-23 17:53:31 +0100 | [diff] [blame] | 796 | int patflags, char **err) |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 797 | { |
Thierry FOURNIER | 7148ce6 | 2013-12-06 19:06:43 +0100 | [diff] [blame] | 798 | unsigned int mask = 0; |
| 799 | struct pat_idx_elt *node; |
| 800 | int len; |
| 801 | int ret; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 802 | |
Thierry FOURNIER | 972028f | 2014-01-23 17:53:31 +0100 | [diff] [blame] | 803 | /* we keep the previous pattern along iterations as long as it's not used */ |
| 804 | if (!*pattern) |
Thierry FOURNIER | 3ead5b9 | 2013-12-13 12:12:18 +0100 | [diff] [blame^] | 805 | *pattern = (struct pattern_list *)malloc(sizeof(**pattern)); |
Thierry FOURNIER | 972028f | 2014-01-23 17:53:31 +0100 | [diff] [blame] | 806 | if (!*pattern) { |
| 807 | memprintf(err, "out of memory while loading pattern"); |
| 808 | return 0; |
| 809 | } |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 810 | |
Thierry FOURNIER | 972028f | 2014-01-23 17:53:31 +0100 | [diff] [blame] | 811 | memset(*pattern, 0, sizeof(**pattern)); |
Thierry FOURNIER | 3ead5b9 | 2013-12-13 12:12:18 +0100 | [diff] [blame^] | 812 | (*pattern)->pat.flags = patflags; |
Thierry FOURNIER | 7148ce6 | 2013-12-06 19:06:43 +0100 | [diff] [blame] | 813 | |
Thierry FOURNIER | 3ead5b9 | 2013-12-13 12:12:18 +0100 | [diff] [blame^] | 814 | ret = expr->parse(arg, &(*pattern)->pat, PAT_U_COMPILE, err); |
Thierry FOURNIER | 972028f | 2014-01-23 17:53:31 +0100 | [diff] [blame] | 815 | if (!ret) |
| 816 | return 0; |
Thierry FOURNIER | 7148ce6 | 2013-12-06 19:06:43 +0100 | [diff] [blame] | 817 | |
Thierry FOURNIER | 972028f | 2014-01-23 17:53:31 +0100 | [diff] [blame] | 818 | if (expr->match == pat_match_str) { |
| 819 | /* SMP_T_CSTR tree indexation. |
| 820 | * The match "pat_match_str()" can use trees. |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 821 | */ |
Thierry FOURNIER | 3ead5b9 | 2013-12-13 12:12:18 +0100 | [diff] [blame^] | 822 | if ((*pattern)->pat.flags & PAT_F_IGNORE_CASE) { |
Thierry FOURNIER | 7148ce6 | 2013-12-06 19:06:43 +0100 | [diff] [blame] | 823 | /* If the flag PAT_F_IGNORE_CASE is set, we cannot use trees */ |
Thierry FOURNIER | 972028f | 2014-01-23 17:53:31 +0100 | [diff] [blame] | 824 | goto just_chain_the_pattern; |
| 825 | } |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 826 | |
Thierry FOURNIER | 972028f | 2014-01-23 17:53:31 +0100 | [diff] [blame] | 827 | /* Process the key len */ |
Thierry FOURNIER | 3ead5b9 | 2013-12-13 12:12:18 +0100 | [diff] [blame^] | 828 | len = strlen((*pattern)->pat.ptr.str) + 1; |
Thierry FOURNIER | 7148ce6 | 2013-12-06 19:06:43 +0100 | [diff] [blame] | 829 | |
Thierry FOURNIER | 972028f | 2014-01-23 17:53:31 +0100 | [diff] [blame] | 830 | /* node memory allocation */ |
| 831 | node = calloc(1, sizeof(*node) + len); |
| 832 | if (!node) { |
| 833 | memprintf(err, "out of memory while loading pattern"); |
| 834 | return 0; |
| 835 | } |
Thierry FOURNIER | 7148ce6 | 2013-12-06 19:06:43 +0100 | [diff] [blame] | 836 | |
Thierry FOURNIER | 972028f | 2014-01-23 17:53:31 +0100 | [diff] [blame] | 837 | /* copy the pointer to sample associated to this node */ |
| 838 | node->smp = smp; |
Thierry FOURNIER | 7148ce6 | 2013-12-06 19:06:43 +0100 | [diff] [blame] | 839 | |
Thierry FOURNIER | 972028f | 2014-01-23 17:53:31 +0100 | [diff] [blame] | 840 | /* copy the string */ |
Thierry FOURNIER | 3ead5b9 | 2013-12-13 12:12:18 +0100 | [diff] [blame^] | 841 | memcpy(node->node.key, (*pattern)->pat.ptr.str, len); |
Thierry FOURNIER | 7148ce6 | 2013-12-06 19:06:43 +0100 | [diff] [blame] | 842 | |
Thierry FOURNIER | 972028f | 2014-01-23 17:53:31 +0100 | [diff] [blame] | 843 | /* the "map_parser_str()" function always duplicate string information */ |
Thierry FOURNIER | 3ead5b9 | 2013-12-13 12:12:18 +0100 | [diff] [blame^] | 844 | free((*pattern)->pat.ptr.str); |
| 845 | (*pattern)->pat.ptr.str = NULL; |
Thierry FOURNIER | 7148ce6 | 2013-12-06 19:06:43 +0100 | [diff] [blame] | 846 | |
Thierry FOURNIER | 972028f | 2014-01-23 17:53:31 +0100 | [diff] [blame] | 847 | /* we pre-set the data pointer to the tree's head so that functions |
| 848 | * which are able to insert in a tree know where to do that. |
Thierry FOURNIER | 7148ce6 | 2013-12-06 19:06:43 +0100 | [diff] [blame] | 849 | * |
Thierry FOURNIER | 972028f | 2014-01-23 17:53:31 +0100 | [diff] [blame] | 850 | * because "val" is an "union", the previous data are crushed. |
Thierry FOURNIER | 7148ce6 | 2013-12-06 19:06:43 +0100 | [diff] [blame] | 851 | */ |
Thierry FOURNIER | 3ead5b9 | 2013-12-13 12:12:18 +0100 | [diff] [blame^] | 852 | (*pattern)->pat.flags |= PAT_F_TREE; |
| 853 | (*pattern)->pat.val.tree = &expr->pattern_tree; |
Thierry FOURNIER | 7148ce6 | 2013-12-06 19:06:43 +0100 | [diff] [blame] | 854 | |
Thierry FOURNIER | 972028f | 2014-01-23 17:53:31 +0100 | [diff] [blame] | 855 | /* index the new node */ |
Thierry FOURNIER | 3ead5b9 | 2013-12-13 12:12:18 +0100 | [diff] [blame^] | 856 | if (ebst_insert((*pattern)->pat.val.tree, &node->node) != &node->node) |
Thierry FOURNIER | 972028f | 2014-01-23 17:53:31 +0100 | [diff] [blame] | 857 | free(node); /* was a duplicate */ |
| 858 | } |
| 859 | else if (expr->match == pat_match_ip) { |
| 860 | /* SMP_T_IPV4 tree indexation |
| 861 | * The match "pat_match_ip()" can use tree. |
| 862 | */ |
Thierry FOURNIER | 3ead5b9 | 2013-12-13 12:12:18 +0100 | [diff] [blame^] | 863 | if ((*pattern)->pat.type != SMP_T_IPV4) { |
Thierry FOURNIER | 7148ce6 | 2013-12-06 19:06:43 +0100 | [diff] [blame] | 864 | /* Only IPv4 can be indexed */ |
Thierry FOURNIER | 972028f | 2014-01-23 17:53:31 +0100 | [diff] [blame] | 865 | goto just_chain_the_pattern; |
| 866 | } |
Thierry FOURNIER | 7148ce6 | 2013-12-06 19:06:43 +0100 | [diff] [blame] | 867 | |
Thierry FOURNIER | 972028f | 2014-01-23 17:53:31 +0100 | [diff] [blame] | 868 | /* in IPv4 case, check if the mask is contiguous so that we can |
| 869 | * insert the network into the tree. A continuous mask has only |
| 870 | * ones on the left. This means that this mask + its lower bit |
| 871 | * added once again is null. |
| 872 | */ |
Thierry FOURNIER | 3ead5b9 | 2013-12-13 12:12:18 +0100 | [diff] [blame^] | 873 | mask = ntohl((*pattern)->pat.val.ipv4.mask.s_addr); |
Thierry FOURNIER | 972028f | 2014-01-23 17:53:31 +0100 | [diff] [blame] | 874 | if (mask + (mask & -mask) != 0) |
| 875 | goto just_chain_the_pattern; |
| 876 | mask = mask ? 33 - flsnz(mask & -mask) : 0; /* equals cidr value */ |
Thierry FOURNIER | 7148ce6 | 2013-12-06 19:06:43 +0100 | [diff] [blame] | 877 | |
Thierry FOURNIER | 972028f | 2014-01-23 17:53:31 +0100 | [diff] [blame] | 878 | /* node memory allocation */ |
| 879 | node = calloc(1, sizeof(*node) + 4); |
| 880 | if (!node) { |
| 881 | memprintf(err, "out of memory while loading pattern"); |
| 882 | return 0; |
| 883 | } |
Thierry FOURNIER | 7148ce6 | 2013-12-06 19:06:43 +0100 | [diff] [blame] | 884 | |
Thierry FOURNIER | 972028f | 2014-01-23 17:53:31 +0100 | [diff] [blame] | 885 | /* copy the pointer to sample associated to this node */ |
| 886 | node->smp = smp; |
Thierry FOURNIER | 7148ce6 | 2013-12-06 19:06:43 +0100 | [diff] [blame] | 887 | |
Thierry FOURNIER | 972028f | 2014-01-23 17:53:31 +0100 | [diff] [blame] | 888 | /* FIXME: insert <addr>/<mask> into the tree here */ |
Thierry FOURNIER | 3ead5b9 | 2013-12-13 12:12:18 +0100 | [diff] [blame^] | 889 | memcpy(node->node.key, &(*pattern)->pat.val.ipv4.addr, 4); /* network byte order */ |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 890 | |
Thierry FOURNIER | 972028f | 2014-01-23 17:53:31 +0100 | [diff] [blame] | 891 | /* we pre-set the data pointer to the tree's head so that functions |
| 892 | * which are able to insert in a tree know where to do that. |
Thierry FOURNIER | 7148ce6 | 2013-12-06 19:06:43 +0100 | [diff] [blame] | 893 | * |
Thierry FOURNIER | 972028f | 2014-01-23 17:53:31 +0100 | [diff] [blame] | 894 | * because "val" is an "union", the previous data are crushed. |
Thierry FOURNIER | 7148ce6 | 2013-12-06 19:06:43 +0100 | [diff] [blame] | 895 | */ |
Thierry FOURNIER | 3ead5b9 | 2013-12-13 12:12:18 +0100 | [diff] [blame^] | 896 | (*pattern)->pat.flags |= PAT_F_TREE; |
| 897 | (*pattern)->pat.val.tree = &expr->pattern_tree; |
Thierry FOURNIER | 7148ce6 | 2013-12-06 19:06:43 +0100 | [diff] [blame] | 898 | |
Thierry FOURNIER | 972028f | 2014-01-23 17:53:31 +0100 | [diff] [blame] | 899 | /* Index the new node |
| 900 | * FIXME: insert <addr>/<mask> into the tree here |
| 901 | */ |
| 902 | node->node.node.pfx = mask; |
Thierry FOURNIER | 3ead5b9 | 2013-12-13 12:12:18 +0100 | [diff] [blame^] | 903 | if (ebmb_insert_prefix((*pattern)->pat.val.tree, &node->node, 4) != &node->node) |
Thierry FOURNIER | 972028f | 2014-01-23 17:53:31 +0100 | [diff] [blame] | 904 | free(node); /* was a duplicate */ |
| 905 | } |
| 906 | else { |
Thierry FOURNIER | 7148ce6 | 2013-12-06 19:06:43 +0100 | [diff] [blame] | 907 | just_chain_the_pattern: |
Thierry FOURNIER | 972028f | 2014-01-23 17:53:31 +0100 | [diff] [blame] | 908 | /* if the parser did not feed the tree, let's chain the pattern to the list */ |
| 909 | LIST_ADDQ(&expr->patterns, &(*pattern)->list); |
Thierry FOURNIER | 7148ce6 | 2013-12-06 19:06:43 +0100 | [diff] [blame] | 910 | |
Thierry FOURNIER | 972028f | 2014-01-23 17:53:31 +0100 | [diff] [blame] | 911 | /* copy the pointer to sample associated to this node */ |
Thierry FOURNIER | 3ead5b9 | 2013-12-13 12:12:18 +0100 | [diff] [blame^] | 912 | (*pattern)->pat.smp = smp; |
Thierry FOURNIER | 7148ce6 | 2013-12-06 19:06:43 +0100 | [diff] [blame] | 913 | |
Thierry FOURNIER | 972028f | 2014-01-23 17:53:31 +0100 | [diff] [blame] | 914 | /* get a new one */ |
| 915 | *pattern = NULL; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 916 | } |
| 917 | |
| 918 | return 1; |
| 919 | } |
| 920 | |
| 921 | /* Reads patterns from a file. If <err_msg> is non-NULL, an error message will |
| 922 | * be returned there on errors and the caller will have to free it. |
| 923 | */ |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 924 | int pattern_read_from_file(struct pattern_expr *expr, |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 925 | const char *filename, int patflags, |
| 926 | char **err) |
| 927 | { |
| 928 | FILE *file; |
| 929 | char *c; |
| 930 | char *arg; |
Thierry FOURNIER | 3ead5b9 | 2013-12-13 12:12:18 +0100 | [diff] [blame^] | 931 | struct pattern_list *pattern; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 932 | int ret = 0; |
| 933 | int line = 0; |
| 934 | int code; |
| 935 | |
| 936 | file = fopen(filename, "r"); |
| 937 | if (!file) { |
| 938 | memprintf(err, "failed to open pattern file <%s>", filename); |
| 939 | return 0; |
| 940 | } |
| 941 | |
| 942 | /* now parse all patterns. The file may contain only one pattern per |
| 943 | * line. If the line contains spaces, they will be part of the pattern. |
| 944 | * The pattern stops at the first CR, LF or EOF encountered. |
| 945 | */ |
| 946 | pattern = NULL; |
| 947 | while (fgets(trash.str, trash.size, file) != NULL) { |
| 948 | line++; |
| 949 | c = trash.str; |
| 950 | |
| 951 | /* ignore lines beginning with a dash */ |
| 952 | if (*c == '#') |
| 953 | continue; |
| 954 | |
| 955 | /* strip leading spaces and tabs */ |
| 956 | while (*c == ' ' || *c == '\t') |
| 957 | c++; |
| 958 | |
| 959 | |
| 960 | arg = c; |
| 961 | while (*c && *c != '\n' && *c != '\r') |
| 962 | c++; |
| 963 | *c = 0; |
| 964 | |
| 965 | /* empty lines are ignored too */ |
| 966 | if (c == arg) |
| 967 | continue; |
| 968 | |
Thierry FOURNIER | 972028f | 2014-01-23 17:53:31 +0100 | [diff] [blame] | 969 | code = pattern_register(expr, arg, NULL, &pattern, patflags, err); |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 970 | if (code == -2) { |
| 971 | memprintf(err, "out of memory when loading patterns from file <%s>", filename); |
| 972 | goto out_close; |
| 973 | } |
| 974 | else if (code < 0) { |
| 975 | memprintf(err, "%s when loading patterns from file <%s>", *err, filename); |
| 976 | goto out_free_pattern; |
| 977 | } |
| 978 | } |
| 979 | |
| 980 | ret = 1; /* success */ |
| 981 | |
| 982 | out_free_pattern: |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 983 | pattern_free(pattern); |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 984 | out_close: |
| 985 | fclose(file); |
| 986 | return ret; |
| 987 | } |
| 988 | |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 989 | /* This function matches a sample <smp> against a set of patterns presented in |
| 990 | * pattern expression <expr>. Upon success, if <sample> is not NULL, it is fed |
| 991 | * with the pointer associated with the matching pattern. This function returns |
| 992 | * PAT_NOMATCH or PAT_MATCH. |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 993 | */ |
Willy Tarreau | 0cba607 | 2013-11-28 22:21:02 +0100 | [diff] [blame] | 994 | enum pat_match_res pattern_exec_match(struct pattern_expr *expr, struct sample *smp, |
Thierry FOURNIER | 7609064 | 2013-12-10 15:03:38 +0100 | [diff] [blame] | 995 | struct sample_storage **sample, |
| 996 | struct pattern **pat, struct pat_idx_elt **idx_elt) |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 997 | { |
Willy Tarreau | 0cba607 | 2013-11-28 22:21:02 +0100 | [diff] [blame] | 998 | enum pat_match_res pat_res = PAT_NOMATCH; |
Thierry FOURNIER | 3ead5b9 | 2013-12-13 12:12:18 +0100 | [diff] [blame^] | 999 | struct pattern_list *pattern; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 1000 | struct ebmb_node *node = NULL; |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 1001 | struct pat_idx_elt *elt; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 1002 | |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 1003 | if (expr->match == pat_match_nothing) { |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 1004 | if (smp->data.uint) |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 1005 | pat_res |= PAT_MATCH; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 1006 | else |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 1007 | pat_res |= PAT_NOMATCH; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 1008 | } |
| 1009 | else if (!expr->match) { |
| 1010 | /* just check for existence */ |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 1011 | pat_res |= PAT_MATCH; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 1012 | } |
| 1013 | else { |
| 1014 | if (!eb_is_empty(&expr->pattern_tree)) { |
| 1015 | /* a tree is present, let's check what type it is */ |
Thierry FOURNIER | e3ded59 | 2013-12-06 15:36:54 +0100 | [diff] [blame] | 1016 | if (expr->match == pat_match_str) { |
| 1017 | if (sample_convert(smp, SMP_T_STR)) |
| 1018 | node = pat_lookup_str(smp, expr); |
| 1019 | } |
| 1020 | else if (expr->match == pat_match_ip) { |
| 1021 | if (sample_convert(smp, SMP_T_IPV4)) |
| 1022 | node = pat_lookup_ip(smp, expr); |
| 1023 | } |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 1024 | if (node) { |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 1025 | pat_res |= PAT_MATCH; |
| 1026 | elt = ebmb_entry(node, struct pat_idx_elt, node); |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 1027 | if (sample) |
| 1028 | *sample = elt->smp; |
Thierry FOURNIER | 7609064 | 2013-12-10 15:03:38 +0100 | [diff] [blame] | 1029 | if (idx_elt) |
| 1030 | *idx_elt = elt; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 1031 | } |
| 1032 | } |
| 1033 | |
| 1034 | /* call the match() function for all tests on this value */ |
| 1035 | list_for_each_entry(pattern, &expr->patterns, list) { |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 1036 | if (pat_res == PAT_MATCH) |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 1037 | break; |
Thierry FOURNIER | 3ead5b9 | 2013-12-13 12:12:18 +0100 | [diff] [blame^] | 1038 | if (sample_convert(smp, pattern->pat.expect_type)) |
| 1039 | pat_res |= expr->match(smp, &pattern->pat); |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 1040 | if (sample) |
Thierry FOURNIER | 3ead5b9 | 2013-12-13 12:12:18 +0100 | [diff] [blame^] | 1041 | *sample = pattern->pat.smp; |
Thierry FOURNIER | 7609064 | 2013-12-10 15:03:38 +0100 | [diff] [blame] | 1042 | if (pat) |
Thierry FOURNIER | 3ead5b9 | 2013-12-13 12:12:18 +0100 | [diff] [blame^] | 1043 | *pat = &pattern->pat; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 1044 | } |
| 1045 | } |
| 1046 | |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 1047 | return pat_res; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 1048 | } |
| 1049 | |
Thierry FOURNIER | 01cdcd4 | 2013-12-10 15:08:01 +0100 | [diff] [blame] | 1050 | /* This function browse the pattern expr <expr> to lookup the key <key>. On |
| 1051 | * error it returns 0. On success, it returns 1 and fills either <pat_elt> |
| 1052 | * or <idx_elt> with the respectively matched pointers, and the other one with |
| 1053 | * NULL. Pointers are not set if they're passed as NULL. |
| 1054 | */ |
| 1055 | int pattern_lookup(const char *key, struct pattern_expr *expr, |
Thierry FOURNIER | 3ead5b9 | 2013-12-13 12:12:18 +0100 | [diff] [blame^] | 1056 | struct pattern_list **pat_elt, struct pat_idx_elt **idx_elt, char **err) |
Thierry FOURNIER | 01cdcd4 | 2013-12-10 15:08:01 +0100 | [diff] [blame] | 1057 | { |
| 1058 | struct pattern pattern; |
Thierry FOURNIER | 3ead5b9 | 2013-12-13 12:12:18 +0100 | [diff] [blame^] | 1059 | struct pattern_list *pat; |
Thierry FOURNIER | 01cdcd4 | 2013-12-10 15:08:01 +0100 | [diff] [blame] | 1060 | struct ebmb_node *node; |
| 1061 | struct pat_idx_elt *elt; |
Willy Tarreau | 668ae53 | 2013-12-15 16:42:26 +0100 | [diff] [blame] | 1062 | unsigned int mask = 0; |
Thierry FOURNIER | 01cdcd4 | 2013-12-10 15:08:01 +0100 | [diff] [blame] | 1063 | |
| 1064 | /* no real pattern */ |
| 1065 | if (!expr->match || expr->match == pat_match_nothing) |
| 1066 | return 0; |
| 1067 | |
| 1068 | /* build lookup pattern */ |
Thierry FOURNIER | 580c32c | 2014-01-24 10:58:12 +0100 | [diff] [blame] | 1069 | if (!expr->parse(key, &pattern, PAT_U_LOOKUP, NULL)) |
Thierry FOURNIER | 01cdcd4 | 2013-12-10 15:08:01 +0100 | [diff] [blame] | 1070 | return 0; |
| 1071 | |
| 1072 | pat = NULL; |
| 1073 | elt = NULL; |
Thierry FOURNIER | 01cdcd4 | 2013-12-10 15:08:01 +0100 | [diff] [blame] | 1074 | |
Thierry FOURNIER | c64de3f | 2013-12-10 15:08:39 +0100 | [diff] [blame] | 1075 | /* Try to look up the tree first. IPv6 is not indexed */ |
| 1076 | if (!eb_is_empty(&expr->pattern_tree) && pattern.type != SMP_T_IPV6) { |
Thierry FOURNIER | 01cdcd4 | 2013-12-10 15:08:01 +0100 | [diff] [blame] | 1077 | /* Check the pattern type */ |
| 1078 | if (pattern.type != SMP_T_STR && |
| 1079 | pattern.type != SMP_T_CSTR && |
| 1080 | pattern.type != SMP_T_IPV4) { |
| 1081 | memprintf(err, "Unexpected pattern type."); |
| 1082 | return 0; |
| 1083 | } |
| 1084 | |
| 1085 | /* Convert mask. If the mask is not contiguous, ignore the lookup |
| 1086 | * in the tree, and browse the list. |
| 1087 | */ |
| 1088 | if (expr->match == pat_match_ip) { |
| 1089 | mask = ntohl(pattern.val.ipv4.mask.s_addr); |
| 1090 | if (mask + (mask & -mask) != 0) |
| 1091 | goto browse_list; |
| 1092 | mask = mask ? 33 - flsnz(mask & -mask) : 0; /* equals cidr value */ |
| 1093 | } |
| 1094 | |
| 1095 | /* browse each node of the tree, and check string */ |
| 1096 | if (expr->match == pat_match_str) { |
| 1097 | for (node = ebmb_first(&expr->pattern_tree); |
| 1098 | node; |
| 1099 | node = ebmb_next(node)) { |
| 1100 | elt = container_of(node, struct pat_idx_elt, node); |
| 1101 | if (strcmp(pattern.ptr.str, (char *)elt->node.key) == 0) |
| 1102 | goto found; |
| 1103 | } |
| 1104 | } |
| 1105 | else if (expr->match == pat_match_ip) { |
| 1106 | for (node = ebmb_first(&expr->pattern_tree); |
| 1107 | node; |
| 1108 | node = ebmb_next(node)) { |
| 1109 | elt = container_of(node, struct pat_idx_elt, node); |
| 1110 | if (elt->node.node.pfx == mask && |
| 1111 | memcmp(&pattern.val.ipv4.addr.s_addr, elt->node.key, 4) == 0) |
| 1112 | goto found; |
| 1113 | } |
| 1114 | } |
| 1115 | } |
| 1116 | |
| 1117 | browse_list: |
Thierry FOURNIER | c64de3f | 2013-12-10 15:08:39 +0100 | [diff] [blame] | 1118 | elt = NULL; |
Thierry FOURNIER | 01cdcd4 | 2013-12-10 15:08:01 +0100 | [diff] [blame] | 1119 | if (expr->parse == pat_parse_int || |
| 1120 | expr->parse == pat_parse_len) { |
| 1121 | list_for_each_entry(pat, &expr->patterns, list) { |
Thierry FOURNIER | 3ead5b9 | 2013-12-13 12:12:18 +0100 | [diff] [blame^] | 1122 | if (pat->pat.flags & PAT_F_TREE) |
Thierry FOURNIER | 01cdcd4 | 2013-12-10 15:08:01 +0100 | [diff] [blame] | 1123 | continue; |
Thierry FOURNIER | 3ead5b9 | 2013-12-13 12:12:18 +0100 | [diff] [blame^] | 1124 | if (pattern.val.range.min_set != pat->pat.val.range.min_set) |
Thierry FOURNIER | 01cdcd4 | 2013-12-10 15:08:01 +0100 | [diff] [blame] | 1125 | continue; |
Thierry FOURNIER | 3ead5b9 | 2013-12-13 12:12:18 +0100 | [diff] [blame^] | 1126 | if (pattern.val.range.max_set != pat->pat.val.range.max_set) |
Thierry FOURNIER | 01cdcd4 | 2013-12-10 15:08:01 +0100 | [diff] [blame] | 1127 | continue; |
| 1128 | if (pattern.val.range.min_set && |
Thierry FOURNIER | 3ead5b9 | 2013-12-13 12:12:18 +0100 | [diff] [blame^] | 1129 | pattern.val.range.min != pat->pat.val.range.min) |
Thierry FOURNIER | 01cdcd4 | 2013-12-10 15:08:01 +0100 | [diff] [blame] | 1130 | continue; |
| 1131 | if (pattern.val.range.max_set && |
Thierry FOURNIER | 3ead5b9 | 2013-12-13 12:12:18 +0100 | [diff] [blame^] | 1132 | pattern.val.range.max != pat->pat.val.range.max) |
Thierry FOURNIER | 01cdcd4 | 2013-12-10 15:08:01 +0100 | [diff] [blame] | 1133 | continue; |
| 1134 | goto found; |
| 1135 | } |
| 1136 | } |
| 1137 | else if (expr->parse == pat_parse_ip) { |
| 1138 | list_for_each_entry(pat, &expr->patterns, list) { |
Thierry FOURNIER | 3ead5b9 | 2013-12-13 12:12:18 +0100 | [diff] [blame^] | 1139 | if (pat->pat.flags & PAT_F_TREE) |
Thierry FOURNIER | 01cdcd4 | 2013-12-10 15:08:01 +0100 | [diff] [blame] | 1140 | continue; |
Thierry FOURNIER | 3ead5b9 | 2013-12-13 12:12:18 +0100 | [diff] [blame^] | 1141 | if (pattern.type != pat->pat.type) |
Thierry FOURNIER | 01cdcd4 | 2013-12-10 15:08:01 +0100 | [diff] [blame] | 1142 | continue; |
| 1143 | if (pattern.type == SMP_T_IPV4 && |
Thierry FOURNIER | 3ead5b9 | 2013-12-13 12:12:18 +0100 | [diff] [blame^] | 1144 | memcmp(&pattern.val.ipv4.addr, &pat->pat.val.ipv4.addr, sizeof(pat->pat.val.ipv4.addr)) != 0) |
Thierry FOURNIER | 01cdcd4 | 2013-12-10 15:08:01 +0100 | [diff] [blame] | 1145 | continue; |
| 1146 | if (pattern.type == SMP_T_IPV4 && |
Thierry FOURNIER | 3ead5b9 | 2013-12-13 12:12:18 +0100 | [diff] [blame^] | 1147 | memcmp(&pattern.val.ipv4.mask, &pat->pat.val.ipv4.mask, sizeof(pat->pat.val.ipv4.addr)) != 0) |
Thierry FOURNIER | 01cdcd4 | 2013-12-10 15:08:01 +0100 | [diff] [blame] | 1148 | continue; |
| 1149 | if (pattern.type == SMP_T_IPV6 && |
Thierry FOURNIER | 3ead5b9 | 2013-12-13 12:12:18 +0100 | [diff] [blame^] | 1150 | memcmp(&pattern.val.ipv6.addr, &pat->pat.val.ipv6.addr, sizeof(pat->pat.val.ipv6.addr)) != 0) |
Thierry FOURNIER | 01cdcd4 | 2013-12-10 15:08:01 +0100 | [diff] [blame] | 1151 | continue; |
| 1152 | if (pattern.type == SMP_T_IPV6 && |
Thierry FOURNIER | 3ead5b9 | 2013-12-13 12:12:18 +0100 | [diff] [blame^] | 1153 | pattern.val.ipv6.mask != pat->pat.val.ipv6.mask) |
Thierry FOURNIER | 01cdcd4 | 2013-12-10 15:08:01 +0100 | [diff] [blame] | 1154 | continue; |
| 1155 | goto found; |
| 1156 | } |
| 1157 | } |
| 1158 | else if (expr->parse == pat_parse_str) { |
| 1159 | list_for_each_entry(pat, &expr->patterns, list) { |
Thierry FOURNIER | 3ead5b9 | 2013-12-13 12:12:18 +0100 | [diff] [blame^] | 1160 | if (pat->pat.flags & PAT_F_TREE) |
Thierry FOURNIER | 01cdcd4 | 2013-12-10 15:08:01 +0100 | [diff] [blame] | 1161 | continue; |
Thierry FOURNIER | 3ead5b9 | 2013-12-13 12:12:18 +0100 | [diff] [blame^] | 1162 | if (pattern.len != pat->pat.len) |
Thierry FOURNIER | 01cdcd4 | 2013-12-10 15:08:01 +0100 | [diff] [blame] | 1163 | continue; |
Thierry FOURNIER | 3ead5b9 | 2013-12-13 12:12:18 +0100 | [diff] [blame^] | 1164 | if (pat->pat.flags & PAT_F_IGNORE_CASE) { |
| 1165 | if (strncasecmp(pattern.ptr.str, pat->pat.ptr.str, pat->pat.len) != 0) |
Thierry FOURNIER | 35249cb | 2014-01-14 13:38:40 +0100 | [diff] [blame] | 1166 | continue; |
| 1167 | } |
| 1168 | else { |
Thierry FOURNIER | 3ead5b9 | 2013-12-13 12:12:18 +0100 | [diff] [blame^] | 1169 | if (strncmp(pattern.ptr.str, pat->pat.ptr.str, pat->pat.len) != 0) |
Thierry FOURNIER | 35249cb | 2014-01-14 13:38:40 +0100 | [diff] [blame] | 1170 | continue; |
| 1171 | } |
Thierry FOURNIER | 01cdcd4 | 2013-12-10 15:08:01 +0100 | [diff] [blame] | 1172 | goto found; |
| 1173 | } |
| 1174 | } |
| 1175 | else if (expr->parse == pat_parse_bin) { |
| 1176 | list_for_each_entry(pat, &expr->patterns, list) { |
Thierry FOURNIER | 3ead5b9 | 2013-12-13 12:12:18 +0100 | [diff] [blame^] | 1177 | if (pat->pat.flags & PAT_F_TREE) |
Thierry FOURNIER | 01cdcd4 | 2013-12-10 15:08:01 +0100 | [diff] [blame] | 1178 | continue; |
Thierry FOURNIER | 3ead5b9 | 2013-12-13 12:12:18 +0100 | [diff] [blame^] | 1179 | if (pattern.len != pat->pat.len) |
Thierry FOURNIER | 01cdcd4 | 2013-12-10 15:08:01 +0100 | [diff] [blame] | 1180 | continue; |
Thierry FOURNIER | 3ead5b9 | 2013-12-13 12:12:18 +0100 | [diff] [blame^] | 1181 | if (memcmp(pattern.ptr.ptr, pat->pat.ptr.ptr, pat->pat.len) != 0) |
Thierry FOURNIER | 01cdcd4 | 2013-12-10 15:08:01 +0100 | [diff] [blame] | 1182 | continue; |
| 1183 | goto found; |
| 1184 | } |
| 1185 | } |
| 1186 | else if (expr->parse == pat_parse_reg) { |
| 1187 | list_for_each_entry(pat, &expr->patterns, list) { |
Thierry FOURNIER | 3ead5b9 | 2013-12-13 12:12:18 +0100 | [diff] [blame^] | 1188 | if (pat->pat.flags & PAT_F_TREE) |
Thierry FOURNIER | 01cdcd4 | 2013-12-10 15:08:01 +0100 | [diff] [blame] | 1189 | continue; |
Thierry FOURNIER | 3ead5b9 | 2013-12-13 12:12:18 +0100 | [diff] [blame^] | 1190 | if (pat->pat.flags & PAT_F_IGNORE_CASE) { |
| 1191 | if (strcasecmp(pattern.ptr.reg->regstr, pat->pat.ptr.reg->regstr) != 0) |
Thierry FOURNIER | 35249cb | 2014-01-14 13:38:40 +0100 | [diff] [blame] | 1192 | continue; |
| 1193 | } |
| 1194 | else { |
Thierry FOURNIER | 3ead5b9 | 2013-12-13 12:12:18 +0100 | [diff] [blame^] | 1195 | if (strcmp(pattern.ptr.reg->regstr, pat->pat.ptr.reg->regstr) != 0) |
Thierry FOURNIER | 35249cb | 2014-01-14 13:38:40 +0100 | [diff] [blame] | 1196 | continue; |
| 1197 | } |
Thierry FOURNIER | 01cdcd4 | 2013-12-10 15:08:01 +0100 | [diff] [blame] | 1198 | goto found; |
| 1199 | } |
| 1200 | } |
| 1201 | |
| 1202 | /* if we get there, we didn't find the pattern */ |
| 1203 | return 0; |
| 1204 | found: |
| 1205 | if (idx_elt) |
| 1206 | *idx_elt = elt; |
| 1207 | |
| 1208 | if (pat_elt) |
| 1209 | *pat_elt = pat; |
| 1210 | |
| 1211 | return 1; |
| 1212 | } |