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 | 0b2fe4a | 2013-12-06 20:33:50 +0100 | [diff] [blame] | 43 | int (*pat_parse_fcts[PAT_MATCH_NUM])(const char **, struct pattern *, enum pat_usage, int *, 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 | /* |
| 93 | * These functions are exported and may be used by any other component. |
| 94 | */ |
| 95 | |
| 96 | /* ignore the current line */ |
Thierry FOURNIER | 0b2fe4a | 2013-12-06 20:33:50 +0100 | [diff] [blame] | 97 | int pat_parse_nothing(const char **text, struct pattern *pattern, enum pat_usage usage, int *opaque, char **err) |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 98 | { |
| 99 | return 1; |
| 100 | } |
| 101 | |
| 102 | /* always return false */ |
Willy Tarreau | 0cba607 | 2013-11-28 22:21:02 +0100 | [diff] [blame] | 103 | enum pat_match_res pat_match_nothing(struct sample *smp, struct pattern *pattern) |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 104 | { |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 105 | return PAT_NOMATCH; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | |
| 109 | /* NB: For two strings to be identical, it is required that their lengths match */ |
Willy Tarreau | 0cba607 | 2013-11-28 22:21:02 +0100 | [diff] [blame] | 110 | enum pat_match_res pat_match_str(struct sample *smp, struct pattern *pattern) |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 111 | { |
| 112 | int icase; |
| 113 | |
| 114 | if (pattern->len != smp->data.str.len) |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 115 | return PAT_NOMATCH; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 116 | |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 117 | icase = pattern->flags & PAT_F_IGNORE_CASE; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 118 | 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 FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 120 | return PAT_MATCH; |
| 121 | return PAT_NOMATCH; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | /* NB: For two binaries buf to be identical, it is required that their lengths match */ |
Willy Tarreau | 0cba607 | 2013-11-28 22:21:02 +0100 | [diff] [blame] | 125 | enum pat_match_res pat_match_bin(struct sample *smp, struct pattern *pattern) |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 126 | { |
| 127 | if (pattern->len != smp->data.str.len) |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 128 | return PAT_NOMATCH; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 129 | |
| 130 | if (memcmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) == 0) |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 131 | return PAT_MATCH; |
| 132 | return PAT_NOMATCH; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | /* Lookup a string in the expression's pattern tree. The node is returned if it |
| 136 | * exists, otherwise NULL. |
| 137 | */ |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 138 | static void *pat_lookup_str(struct sample *smp, struct pattern_expr *expr) |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 139 | { |
| 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 Tarreau | 0cba607 | 2013-11-28 22:21:02 +0100 | [diff] [blame] | 157 | enum pat_match_res pat_match_reg(struct sample *smp, struct pattern *pattern) |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 158 | { |
| 159 | if (regex_exec(pattern->ptr.reg, smp->data.str.str, smp->data.str.len) == 0) |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 160 | return PAT_MATCH; |
| 161 | return PAT_NOMATCH; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | /* Checks that the pattern matches the beginning of the tested string. */ |
Willy Tarreau | 0cba607 | 2013-11-28 22:21:02 +0100 | [diff] [blame] | 165 | enum pat_match_res pat_match_beg(struct sample *smp, struct pattern *pattern) |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 166 | { |
| 167 | int icase; |
| 168 | |
| 169 | if (pattern->len > smp->data.str.len) |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 170 | return PAT_NOMATCH; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 171 | |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 172 | icase = pattern->flags & PAT_F_IGNORE_CASE; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 173 | 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 FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 175 | return PAT_NOMATCH; |
| 176 | return PAT_MATCH; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | /* Checks that the pattern matches the end of the tested string. */ |
Willy Tarreau | 0cba607 | 2013-11-28 22:21:02 +0100 | [diff] [blame] | 180 | enum pat_match_res pat_match_end(struct sample *smp, struct pattern *pattern) |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 181 | { |
| 182 | int icase; |
| 183 | |
| 184 | if (pattern->len > smp->data.str.len) |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 185 | return PAT_NOMATCH; |
| 186 | icase = pattern->flags & PAT_F_IGNORE_CASE; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 187 | 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 FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 189 | return PAT_NOMATCH; |
| 190 | return PAT_MATCH; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 191 | } |
| 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 Tarreau | 0cba607 | 2013-11-28 22:21:02 +0100 | [diff] [blame] | 196 | enum pat_match_res pat_match_sub(struct sample *smp, struct pattern *pattern) |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 197 | { |
| 198 | int icase; |
| 199 | char *end; |
| 200 | char *c; |
| 201 | |
| 202 | if (pattern->len > smp->data.str.len) |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 203 | return PAT_NOMATCH; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 204 | |
| 205 | end = smp->data.str.str + smp->data.str.len - pattern->len; |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 206 | icase = pattern->flags & PAT_F_IGNORE_CASE; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 207 | 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 FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 212 | return PAT_MATCH; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 213 | } |
| 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 FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 219 | return PAT_MATCH; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 220 | } |
| 221 | } |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 222 | return PAT_NOMATCH; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 223 | } |
| 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 | */ |
| 236 | static 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 | |
| 242 | static 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 FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 253 | static int match_word(struct sample *smp, struct pattern *pattern, unsigned int delimiters) |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 254 | { |
| 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 FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 272 | return PAT_NOMATCH; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 273 | |
| 274 | may_match = 1; |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 275 | icase = pattern->flags & PAT_F_IGNORE_CASE; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 276 | 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 FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 290 | return PAT_MATCH; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 291 | } else { |
| 292 | if ((*c == *ps) && |
| 293 | (strncmp(ps, c, pl) == 0) && |
| 294 | (c == end || is_delimiter(c[pl], delimiters))) |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 295 | return PAT_MATCH; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 296 | } |
| 297 | may_match = 0; |
| 298 | } |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 299 | return PAT_NOMATCH; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 300 | } |
| 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 Tarreau | 0cba607 | 2013-11-28 22:21:02 +0100 | [diff] [blame] | 306 | enum pat_match_res pat_match_dir(struct sample *smp, struct pattern *pattern) |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 307 | { |
| 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 Tarreau | 0cba607 | 2013-11-28 22:21:02 +0100 | [diff] [blame] | 315 | enum pat_match_res pat_match_dom(struct sample *smp, struct pattern *pattern) |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 316 | { |
| 317 | return match_word(smp, pattern, make_4delim('/', '?', '.', ':')); |
| 318 | } |
| 319 | |
| 320 | /* Checks that the integer in <test> is included between min and max */ |
Willy Tarreau | 0cba607 | 2013-11-28 22:21:02 +0100 | [diff] [blame] | 321 | enum pat_match_res pat_match_int(struct sample *smp, struct pattern *pattern) |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 322 | { |
| 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 FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 325 | return PAT_MATCH; |
| 326 | return PAT_NOMATCH; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | /* Checks that the length of the pattern in <test> is included between min and max */ |
Willy Tarreau | 0cba607 | 2013-11-28 22:21:02 +0100 | [diff] [blame] | 330 | enum pat_match_res pat_match_len(struct sample *smp, struct pattern *pattern) |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 331 | { |
| 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 FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 334 | return PAT_MATCH; |
| 335 | return PAT_NOMATCH; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 336 | } |
| 337 | |
Willy Tarreau | 0cba607 | 2013-11-28 22:21:02 +0100 | [diff] [blame] | 338 | enum pat_match_res pat_match_ip(struct sample *smp, struct pattern *pattern) |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 339 | { |
| 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 FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 367 | return PAT_NOMATCH; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 368 | } |
| 369 | else |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 370 | return PAT_NOMATCH; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 371 | |
| 372 | if (((v4 ^ pattern->val.ipv4.addr.s_addr) & pattern->val.ipv4.mask.s_addr) == 0) |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 373 | return PAT_MATCH; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 374 | else |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 375 | return PAT_NOMATCH; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 376 | } |
| 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 FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 391 | return PAT_NOMATCH; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 392 | } |
| 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 FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 400 | return PAT_NOMATCH; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 401 | } |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 402 | return PAT_MATCH; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 403 | } |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 404 | return PAT_NOMATCH; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 405 | } |
| 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 FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 410 | static void *pat_lookup_ip(struct sample *smp, struct pattern_expr *expr) |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 411 | { |
| 412 | struct in_addr *s; |
| 413 | |
| 414 | if (smp->type != SMP_T_IPV4) |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 415 | return PAT_NOMATCH; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 416 | |
| 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 FOURNIER | 0b2fe4a | 2013-12-06 20:33:50 +0100 | [diff] [blame] | 422 | int pat_parse_str(const char **text, struct pattern *pattern, enum pat_usage usage, int *opaque, char **err) |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 423 | { |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 424 | pattern->type = SMP_T_CSTR; |
Thierry FOURNIER | cc0e0b3 | 2013-12-06 16:56:40 +0100 | [diff] [blame] | 425 | pattern->expect_type = SMP_T_CSTR; |
Thierry FOURNIER | 0b2fe4a | 2013-12-06 20:33:50 +0100 | [diff] [blame] | 426 | 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 FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 432 | } |
Thierry FOURNIER | 0b2fe4a | 2013-12-06 20:33:50 +0100 | [diff] [blame] | 433 | else |
| 434 | pattern->ptr.str = (char *)*text; |
Thierry FOURNIER | 7148ce6 | 2013-12-06 19:06:43 +0100 | [diff] [blame] | 435 | pattern->len = strlen(*text); |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 436 | return 1; |
| 437 | } |
| 438 | |
| 439 | /* Parse a binary written in hexa. It is allocated. */ |
Thierry FOURNIER | 0b2fe4a | 2013-12-06 20:33:50 +0100 | [diff] [blame] | 440 | int pat_parse_bin(const char **text, struct pattern *pattern, enum pat_usage usage, int *opaque, char **err) |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 441 | { |
Thierry FOURNIER | 0b2fe4a | 2013-12-06 20:33:50 +0100 | [diff] [blame] | 442 | struct chunk *trash; |
| 443 | |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 444 | pattern->type = SMP_T_CBIN; |
Thierry FOURNIER | cc0e0b3 | 2013-12-06 16:56:40 +0100 | [diff] [blame] | 445 | pattern->expect_type = SMP_T_CBIN; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 446 | |
Thierry FOURNIER | 0b2fe4a | 2013-12-06 20:33:50 +0100 | [diff] [blame] | 447 | 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 Tarreau | 126d406 | 2013-12-03 17:50:47 +0100 | [diff] [blame] | 453 | return parse_binary(*text, &pattern->ptr.str, &pattern->len, err); |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | /* Parse and concatenate all further strings into one. */ |
| 457 | int |
Thierry FOURNIER | 0b2fe4a | 2013-12-06 20:33:50 +0100 | [diff] [blame] | 458 | pat_parse_strcat(const char **text, struct pattern *pattern, enum pat_usage usage, int *opaque, char **err) |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 459 | { |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 460 | int len = 0, i; |
| 461 | char *s; |
Thierry FOURNIER | 0b2fe4a | 2013-12-06 20:33:50 +0100 | [diff] [blame] | 462 | struct chunk *trash; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 463 | |
| 464 | for (i = 0; *text[i]; i++) |
| 465 | len += strlen(text[i])+1; |
| 466 | |
| 467 | pattern->type = SMP_T_CSTR; |
Thierry FOURNIER | 0b2fe4a | 2013-12-06 20:33:50 +0100 | [diff] [blame] | 468 | 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 FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 483 | } |
| 484 | |
Thierry FOURNIER | 0b2fe4a | 2013-12-06 20:33:50 +0100 | [diff] [blame] | 485 | s = pattern->ptr.str; |
| 486 | |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 487 | 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 FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 495 | /* Free data allocated by pat_parse_reg */ |
| 496 | static void pat_free_reg(void *ptr) |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 497 | { |
| 498 | regex_free(ptr); |
| 499 | } |
| 500 | |
| 501 | /* Parse a regex. It is allocated. */ |
Thierry FOURNIER | 0b2fe4a | 2013-12-06 20:33:50 +0100 | [diff] [blame] | 502 | int pat_parse_reg(const char **text, struct pattern *pattern, enum pat_usage usage, int *opaque, char **err) |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 503 | { |
Thierry FOURNIER | 799c042 | 2013-12-06 20:36:20 +0100 | [diff] [blame] | 504 | struct my_regex *preg; |
Thierry FOURNIER | 0b2fe4a | 2013-12-06 20:33:50 +0100 | [diff] [blame] | 505 | struct chunk *trash; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 506 | |
Thierry FOURNIER | 0b2fe4a | 2013-12-06 20:33:50 +0100 | [diff] [blame] | 507 | if (usage == PAT_U_COMPILE) { |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 508 | |
Thierry FOURNIER | 0b2fe4a | 2013-12-06 20:33:50 +0100 | [diff] [blame] | 509 | 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 FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 520 | } |
Thierry FOURNIER | 0b2fe4a | 2013-12-06 20:33:50 +0100 | [diff] [blame] | 521 | else { |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 522 | |
Thierry FOURNIER | 0b2fe4a | 2013-12-06 20:33:50 +0100 | [diff] [blame] | 523 | 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 FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 533 | } |
| 534 | |
| 535 | pattern->ptr.reg = preg; |
Thierry FOURNIER | cc0e0b3 | 2013-12-06 16:56:40 +0100 | [diff] [blame] | 536 | pattern->expect_type = SMP_T_CSTR; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 537 | 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 FOURNIER | 0b2fe4a | 2013-12-06 20:33:50 +0100 | [diff] [blame] | 554 | int pat_parse_int(const char **text, struct pattern *pattern, enum pat_usage usage, int *opaque, char **err) |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 555 | { |
| 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 FOURNIER | cc0e0b3 | 2013-12-06 16:56:40 +0100 | [diff] [blame] | 561 | pattern->expect_type = SMP_T_UINT; |
Thierry FOURNIER | 7148ce6 | 2013-12-06 19:06:43 +0100 | [diff] [blame] | 562 | |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 563 | 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 FOURNIER | 0b2fe4a | 2013-12-06 20:33:50 +0100 | [diff] [blame] | 627 | int pat_parse_len(const char **text, struct pattern *pattern, enum pat_usage usage, int *opaque, char **err) |
Thierry FOURNIER | cc0e0b3 | 2013-12-06 16:56:40 +0100 | [diff] [blame] | 628 | { |
| 629 | int ret; |
| 630 | |
Thierry FOURNIER | 0b2fe4a | 2013-12-06 20:33:50 +0100 | [diff] [blame] | 631 | ret = pat_parse_int(text, pattern, usage, opaque, err); |
Thierry FOURNIER | cc0e0b3 | 2013-12-06 16:56:40 +0100 | [diff] [blame] | 632 | pattern->expect_type = SMP_T_CSTR; |
| 633 | return ret; |
| 634 | } |
| 635 | |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 636 | /* 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 FOURNIER | 0b2fe4a | 2013-12-06 20:33:50 +0100 | [diff] [blame] | 656 | int pat_parse_dotted_ver(const char **text, struct pattern *pattern, enum pat_usage usage, int *opaque, char **err) |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 657 | { |
| 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 FOURNIER | cc0e0b3 | 2013-12-06 16:56:40 +0100 | [diff] [blame] | 715 | pattern->expect_type = SMP_T_CSTR; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 716 | |
| 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 FOURNIER | 0b2fe4a | 2013-12-06 20:33:50 +0100 | [diff] [blame] | 747 | int pat_parse_ip(const char **text, struct pattern *pattern, enum pat_usage usage, int *opaque, char **err) |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 748 | { |
Thierry FOURNIER | cc0e0b3 | 2013-12-06 16:56:40 +0100 | [diff] [blame] | 749 | pattern->expect_type = SMP_T_ADDR; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 750 | if (str2net(*text, &pattern->val.ipv4.addr, &pattern->val.ipv4.mask)) { |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 751 | pattern->type = SMP_T_IPV4; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 752 | 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 FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 766 | void pattern_free(struct pattern *pat) |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 767 | { |
| 768 | if (!pat) |
| 769 | return; |
| 770 | |
| 771 | if (pat->ptr.ptr) { |
| 772 | if (pat->freeptrbuf) |
| 773 | pat->freeptrbuf(pat->ptr.ptr); |
| 774 | |
| 775 | free(pat->ptr.ptr); |
| 776 | } |
| 777 | |
Thierry FOURNIER | c64de3f | 2013-12-10 15:08:39 +0100 | [diff] [blame] | 778 | free(pat->smp); |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 779 | free(pat); |
| 780 | } |
| 781 | |
| 782 | void free_pattern_list(struct list *head) |
| 783 | { |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 784 | struct pattern *pat, *tmp; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 785 | list_for_each_entry_safe(pat, tmp, head, list) |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 786 | pattern_free(pat); |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 787 | } |
| 788 | |
| 789 | void free_pattern_tree(struct eb_root *root) |
| 790 | { |
| 791 | struct eb_node *node, *next; |
Thierry FOURNIER | 3ce88c7 | 2013-12-09 11:29:46 +0100 | [diff] [blame] | 792 | struct pat_idx_elt *elt; |
| 793 | |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 794 | node = eb_first(root); |
| 795 | while (node) { |
| 796 | next = eb_next(node); |
| 797 | eb_delete(node); |
Thierry FOURNIER | 3ce88c7 | 2013-12-09 11:29:46 +0100 | [diff] [blame] | 798 | elt = container_of(node, struct pat_idx_elt, node); |
Thierry FOURNIER | c64de3f | 2013-12-10 15:08:39 +0100 | [diff] [blame] | 799 | free(elt->smp); |
Thierry FOURNIER | 3ce88c7 | 2013-12-09 11:29:46 +0100 | [diff] [blame] | 800 | free(elt); |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 801 | node = next; |
| 802 | } |
| 803 | } |
| 804 | |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 805 | void pattern_prune_expr(struct pattern_expr *expr) |
Thierry FOURNIER | d163e1c | 2013-11-28 11:41:23 +0100 | [diff] [blame] | 806 | { |
| 807 | free_pattern_list(&expr->patterns); |
| 808 | free_pattern_tree(&expr->pattern_tree); |
| 809 | LIST_INIT(&expr->patterns); |
| 810 | } |
| 811 | |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 812 | void pattern_init_expr(struct pattern_expr *expr) |
Thierry FOURNIER | d163e1c | 2013-11-28 11:41:23 +0100 | [diff] [blame] | 813 | { |
| 814 | LIST_INIT(&expr->patterns); |
| 815 | expr->pattern_tree = EB_ROOT_UNIQUE; |
| 816 | } |
| 817 | |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 818 | /* return 1 if the process is ok |
| 819 | * return -1 if the parser fail. The err message is filled. |
| 820 | * return -2 if out of memory |
| 821 | */ |
Thierry FOURNIER | 7148ce6 | 2013-12-06 19:06:43 +0100 | [diff] [blame] | 822 | int pattern_register(struct pattern_expr *expr, const char **args, |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 823 | struct sample_storage *smp, |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 824 | struct pattern **pattern, |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 825 | int patflags, char **err) |
| 826 | { |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 827 | int opaque = 0; |
Thierry FOURNIER | 7148ce6 | 2013-12-06 19:06:43 +0100 | [diff] [blame] | 828 | unsigned int mask = 0; |
| 829 | struct pat_idx_elt *node; |
| 830 | int len; |
| 831 | int ret; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 832 | |
Thierry FOURNIER | 7148ce6 | 2013-12-06 19:06:43 +0100 | [diff] [blame] | 833 | /* eat args */ |
| 834 | while (**args) { |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 835 | |
Thierry FOURNIER | 7148ce6 | 2013-12-06 19:06:43 +0100 | [diff] [blame] | 836 | /* we keep the previous pattern along iterations as long as it's not used */ |
| 837 | if (!*pattern) |
| 838 | *pattern = (struct pattern *)malloc(sizeof(**pattern)); |
| 839 | if (!*pattern) { |
| 840 | memprintf(err, "out of memory while loading pattern"); |
| 841 | return 0; |
| 842 | } |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 843 | |
Thierry FOURNIER | 7148ce6 | 2013-12-06 19:06:43 +0100 | [diff] [blame] | 844 | memset(*pattern, 0, sizeof(**pattern)); |
| 845 | (*pattern)->flags = patflags; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 846 | |
Thierry FOURNIER | 0b2fe4a | 2013-12-06 20:33:50 +0100 | [diff] [blame] | 847 | ret = expr->parse(args, *pattern, PAT_U_COMPILE, &opaque, err); |
Thierry FOURNIER | 7148ce6 | 2013-12-06 19:06:43 +0100 | [diff] [blame] | 848 | if (!ret) |
| 849 | return 0; |
| 850 | |
| 851 | /* each parser return the number of args eated */ |
| 852 | args += ret; |
| 853 | |
| 854 | /* |
| 855 | * |
| 856 | * SMP_T_CSTR tree indexation |
| 857 | * |
| 858 | * The match "pat_match_str()" can use tree. |
| 859 | * |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 860 | */ |
Thierry FOURNIER | 7148ce6 | 2013-12-06 19:06:43 +0100 | [diff] [blame] | 861 | if (expr->match == pat_match_str) { |
| 862 | |
| 863 | /* If the flag PAT_F_IGNORE_CASE is set, we cannot use trees */ |
| 864 | if ((*pattern)->flags & PAT_F_IGNORE_CASE) |
| 865 | goto just_chain_the_pattern; |
| 866 | |
| 867 | /* Process the key len */ |
| 868 | len = strlen((*pattern)->ptr.str) + 1; |
| 869 | |
| 870 | /* node memory allocation */ |
| 871 | node = calloc(1, sizeof(*node) + len); |
| 872 | if (!node) { |
| 873 | memprintf(err, "out of memory while loading pattern"); |
| 874 | return 0; |
| 875 | } |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 876 | |
Thierry FOURNIER | 7148ce6 | 2013-12-06 19:06:43 +0100 | [diff] [blame] | 877 | /* copy the pointer to sample associated to this node */ |
| 878 | node->smp = smp; |
| 879 | |
| 880 | /* copy the string */ |
| 881 | memcpy(node->node.key, (*pattern)->ptr.str, len); |
| 882 | |
| 883 | /* the "map_parser_str()" function always duplicate string information */ |
| 884 | free((*pattern)->ptr.str); |
| 885 | |
| 886 | /* we pre-set the data pointer to the tree's head so that functions |
| 887 | * which are able to insert in a tree know where to do that. |
| 888 | * |
| 889 | * because "val" is an "union", the previous data are crushed. |
| 890 | */ |
| 891 | (*pattern)->flags |= PAT_F_TREE; |
| 892 | (*pattern)->val.tree = &expr->pattern_tree; |
| 893 | |
| 894 | /* index the new node */ |
| 895 | if (ebst_insert((*pattern)->val.tree, &node->node) != &node->node) |
| 896 | free(node); /* was a duplicate */ |
| 897 | } |
| 898 | |
| 899 | /* |
| 900 | * |
| 901 | * SMP_T_IPV4 tree indexation |
| 902 | * |
| 903 | * The match "pat_match_ip()" can use tree. |
| 904 | * |
| 905 | */ |
| 906 | else if (expr->match == pat_match_ip) { |
| 907 | |
| 908 | /* Only IPv4 can be indexed */ |
| 909 | if ((*pattern)->type != SMP_T_IPV4) |
| 910 | goto just_chain_the_pattern; |
| 911 | |
| 912 | /* in IPv4 case, check if the mask is contiguous so that we can |
| 913 | * insert the network into the tree. A continuous mask has only |
| 914 | * ones on the left. This means that this mask + its lower bit |
| 915 | * added once again is null. |
| 916 | */ |
| 917 | mask = ntohl((*pattern)->val.ipv4.mask.s_addr); |
| 918 | if (mask + (mask & -mask) != 0) |
| 919 | goto just_chain_the_pattern; |
| 920 | mask = mask ? 33 - flsnz(mask & -mask) : 0; /* equals cidr value */ |
| 921 | |
| 922 | /* node memory allocation */ |
| 923 | node = calloc(1, sizeof(*node) + 4); |
| 924 | if (!node) { |
| 925 | memprintf(err, "out of memory while loading pattern"); |
| 926 | return 0; |
| 927 | } |
| 928 | |
| 929 | /* copy the pointer to sample associated to this node */ |
| 930 | node->smp = smp; |
| 931 | |
| 932 | /* FIXME: insert <addr>/<mask> into the tree here */ |
| 933 | memcpy(node->node.key, &(*pattern)->val.ipv4.addr, 4); /* network byte order */ |
| 934 | |
| 935 | /* we pre-set the data pointer to the tree's head so that functions |
| 936 | * which are able to insert in a tree know where to do that. |
| 937 | * |
| 938 | * because "val" is an "union", the previous data are crushed. |
| 939 | */ |
| 940 | (*pattern)->flags |= PAT_F_TREE; |
| 941 | (*pattern)->val.tree = &expr->pattern_tree; |
| 942 | |
| 943 | /* Index the new node |
| 944 | * FIXME: insert <addr>/<mask> into the tree here |
| 945 | */ |
| 946 | node->node.node.pfx = mask; |
| 947 | if (ebmb_insert_prefix((*pattern)->val.tree, &node->node, 4) != &node->node) |
| 948 | free(node); /* was a duplicate */ |
| 949 | } |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 950 | |
Thierry FOURNIER | 7148ce6 | 2013-12-06 19:06:43 +0100 | [diff] [blame] | 951 | /* |
| 952 | * |
| 953 | * if the parser did not feed the tree, let's chain the pattern to the list |
| 954 | * |
| 955 | */ |
| 956 | else { |
| 957 | |
| 958 | just_chain_the_pattern: |
| 959 | |
| 960 | LIST_ADDQ(&expr->patterns, &(*pattern)->list); |
| 961 | |
| 962 | /* copy the pointer to sample associated to this node */ |
| 963 | (*pattern)->smp = smp; |
| 964 | |
| 965 | /* get a new one */ |
| 966 | *pattern = NULL; |
| 967 | } |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 968 | } |
| 969 | |
| 970 | return 1; |
| 971 | } |
| 972 | |
| 973 | /* Reads patterns from a file. If <err_msg> is non-NULL, an error message will |
| 974 | * be returned there on errors and the caller will have to free it. |
| 975 | */ |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 976 | int pattern_read_from_file(struct pattern_expr *expr, |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 977 | const char *filename, int patflags, |
| 978 | char **err) |
| 979 | { |
| 980 | FILE *file; |
| 981 | char *c; |
| 982 | char *arg; |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 983 | struct pattern *pattern; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 984 | int ret = 0; |
| 985 | int line = 0; |
| 986 | int code; |
Thierry FOURNIER | 7148ce6 | 2013-12-06 19:06:43 +0100 | [diff] [blame] | 987 | const char *args[2]; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 988 | |
| 989 | file = fopen(filename, "r"); |
| 990 | if (!file) { |
| 991 | memprintf(err, "failed to open pattern file <%s>", filename); |
| 992 | return 0; |
| 993 | } |
| 994 | |
| 995 | /* now parse all patterns. The file may contain only one pattern per |
| 996 | * line. If the line contains spaces, they will be part of the pattern. |
| 997 | * The pattern stops at the first CR, LF or EOF encountered. |
| 998 | */ |
| 999 | pattern = NULL; |
| 1000 | while (fgets(trash.str, trash.size, file) != NULL) { |
| 1001 | line++; |
| 1002 | c = trash.str; |
| 1003 | |
| 1004 | /* ignore lines beginning with a dash */ |
| 1005 | if (*c == '#') |
| 1006 | continue; |
| 1007 | |
| 1008 | /* strip leading spaces and tabs */ |
| 1009 | while (*c == ' ' || *c == '\t') |
| 1010 | c++; |
| 1011 | |
| 1012 | |
| 1013 | arg = c; |
| 1014 | while (*c && *c != '\n' && *c != '\r') |
| 1015 | c++; |
| 1016 | *c = 0; |
| 1017 | |
| 1018 | /* empty lines are ignored too */ |
| 1019 | if (c == arg) |
| 1020 | continue; |
| 1021 | |
Thierry FOURNIER | 7148ce6 | 2013-12-06 19:06:43 +0100 | [diff] [blame] | 1022 | args[0] = arg; |
| 1023 | args[1] = ""; |
| 1024 | |
| 1025 | code = pattern_register(expr, args, NULL, &pattern, patflags, err); |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 1026 | if (code == -2) { |
| 1027 | memprintf(err, "out of memory when loading patterns from file <%s>", filename); |
| 1028 | goto out_close; |
| 1029 | } |
| 1030 | else if (code < 0) { |
| 1031 | memprintf(err, "%s when loading patterns from file <%s>", *err, filename); |
| 1032 | goto out_free_pattern; |
| 1033 | } |
| 1034 | } |
| 1035 | |
| 1036 | ret = 1; /* success */ |
| 1037 | |
| 1038 | out_free_pattern: |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 1039 | pattern_free(pattern); |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 1040 | out_close: |
| 1041 | fclose(file); |
| 1042 | return ret; |
| 1043 | } |
| 1044 | |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 1045 | /* This function matches a sample <smp> against a set of patterns presented in |
| 1046 | * pattern expression <expr>. Upon success, if <sample> is not NULL, it is fed |
| 1047 | * with the pointer associated with the matching pattern. This function returns |
| 1048 | * PAT_NOMATCH or PAT_MATCH. |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 1049 | */ |
Willy Tarreau | 0cba607 | 2013-11-28 22:21:02 +0100 | [diff] [blame] | 1050 | 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] | 1051 | struct sample_storage **sample, |
| 1052 | struct pattern **pat, struct pat_idx_elt **idx_elt) |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 1053 | { |
Willy Tarreau | 0cba607 | 2013-11-28 22:21:02 +0100 | [diff] [blame] | 1054 | enum pat_match_res pat_res = PAT_NOMATCH; |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 1055 | struct pattern *pattern; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 1056 | struct ebmb_node *node = NULL; |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 1057 | struct pat_idx_elt *elt; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 1058 | |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 1059 | if (expr->match == pat_match_nothing) { |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 1060 | if (smp->data.uint) |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 1061 | pat_res |= PAT_MATCH; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 1062 | else |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 1063 | pat_res |= PAT_NOMATCH; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 1064 | } |
| 1065 | else if (!expr->match) { |
| 1066 | /* just check for existence */ |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 1067 | pat_res |= PAT_MATCH; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 1068 | } |
| 1069 | else { |
| 1070 | if (!eb_is_empty(&expr->pattern_tree)) { |
| 1071 | /* a tree is present, let's check what type it is */ |
Thierry FOURNIER | e3ded59 | 2013-12-06 15:36:54 +0100 | [diff] [blame] | 1072 | if (expr->match == pat_match_str) { |
| 1073 | if (sample_convert(smp, SMP_T_STR)) |
| 1074 | node = pat_lookup_str(smp, expr); |
| 1075 | } |
| 1076 | else if (expr->match == pat_match_ip) { |
| 1077 | if (sample_convert(smp, SMP_T_IPV4)) |
| 1078 | node = pat_lookup_ip(smp, expr); |
| 1079 | } |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 1080 | if (node) { |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 1081 | pat_res |= PAT_MATCH; |
| 1082 | elt = ebmb_entry(node, struct pat_idx_elt, node); |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 1083 | if (sample) |
| 1084 | *sample = elt->smp; |
Thierry FOURNIER | 7609064 | 2013-12-10 15:03:38 +0100 | [diff] [blame] | 1085 | if (idx_elt) |
| 1086 | *idx_elt = elt; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 1087 | } |
| 1088 | } |
| 1089 | |
| 1090 | /* call the match() function for all tests on this value */ |
| 1091 | list_for_each_entry(pattern, &expr->patterns, list) { |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 1092 | if (pat_res == PAT_MATCH) |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 1093 | break; |
Thierry FOURNIER | e3ded59 | 2013-12-06 15:36:54 +0100 | [diff] [blame] | 1094 | if (sample_convert(smp, pattern->expect_type)) |
| 1095 | pat_res |= expr->match(smp, pattern); |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 1096 | if (sample) |
| 1097 | *sample = pattern->smp; |
Thierry FOURNIER | 7609064 | 2013-12-10 15:03:38 +0100 | [diff] [blame] | 1098 | if (pat) |
| 1099 | *pat = pattern; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 1100 | } |
| 1101 | } |
| 1102 | |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 1103 | return pat_res; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 1104 | } |
| 1105 | |
Thierry FOURNIER | 01cdcd4 | 2013-12-10 15:08:01 +0100 | [diff] [blame] | 1106 | /* This function browse the pattern expr <expr> to lookup the key <key>. On |
| 1107 | * error it returns 0. On success, it returns 1 and fills either <pat_elt> |
| 1108 | * or <idx_elt> with the respectively matched pointers, and the other one with |
| 1109 | * NULL. Pointers are not set if they're passed as NULL. |
| 1110 | */ |
| 1111 | int pattern_lookup(const char *key, struct pattern_expr *expr, |
| 1112 | struct pattern **pat_elt, struct pat_idx_elt **idx_elt, char **err) |
| 1113 | { |
| 1114 | struct pattern pattern; |
| 1115 | struct pattern *pat; |
| 1116 | struct ebmb_node *node; |
| 1117 | struct pat_idx_elt *elt; |
| 1118 | const char *args[2]; |
| 1119 | int opaque = 0; |
| 1120 | unsigned int mask; |
| 1121 | |
| 1122 | /* no real pattern */ |
| 1123 | if (!expr->match || expr->match == pat_match_nothing) |
| 1124 | return 0; |
| 1125 | |
| 1126 | /* build lookup pattern */ |
| 1127 | args[0] = key; |
| 1128 | args[1] = ""; |
| 1129 | if (!expr->parse(args, &pattern, PAT_U_LOOKUP, &opaque, NULL)) |
| 1130 | return 0; |
| 1131 | |
| 1132 | pat = NULL; |
| 1133 | elt = NULL; |
Thierry FOURNIER | 01cdcd4 | 2013-12-10 15:08:01 +0100 | [diff] [blame] | 1134 | |
Thierry FOURNIER | c64de3f | 2013-12-10 15:08:39 +0100 | [diff] [blame] | 1135 | /* Try to look up the tree first. IPv6 is not indexed */ |
| 1136 | if (!eb_is_empty(&expr->pattern_tree) && pattern.type != SMP_T_IPV6) { |
Thierry FOURNIER | 01cdcd4 | 2013-12-10 15:08:01 +0100 | [diff] [blame] | 1137 | /* Check the pattern type */ |
| 1138 | if (pattern.type != SMP_T_STR && |
| 1139 | pattern.type != SMP_T_CSTR && |
| 1140 | pattern.type != SMP_T_IPV4) { |
| 1141 | memprintf(err, "Unexpected pattern type."); |
| 1142 | return 0; |
| 1143 | } |
| 1144 | |
| 1145 | /* Convert mask. If the mask is not contiguous, ignore the lookup |
| 1146 | * in the tree, and browse the list. |
| 1147 | */ |
| 1148 | if (expr->match == pat_match_ip) { |
| 1149 | mask = ntohl(pattern.val.ipv4.mask.s_addr); |
| 1150 | if (mask + (mask & -mask) != 0) |
| 1151 | goto browse_list; |
| 1152 | mask = mask ? 33 - flsnz(mask & -mask) : 0; /* equals cidr value */ |
| 1153 | } |
| 1154 | |
| 1155 | /* browse each node of the tree, and check string */ |
| 1156 | if (expr->match == pat_match_str) { |
| 1157 | for (node = ebmb_first(&expr->pattern_tree); |
| 1158 | node; |
| 1159 | node = ebmb_next(node)) { |
| 1160 | elt = container_of(node, struct pat_idx_elt, node); |
| 1161 | if (strcmp(pattern.ptr.str, (char *)elt->node.key) == 0) |
| 1162 | goto found; |
| 1163 | } |
| 1164 | } |
| 1165 | else if (expr->match == pat_match_ip) { |
| 1166 | for (node = ebmb_first(&expr->pattern_tree); |
| 1167 | node; |
| 1168 | node = ebmb_next(node)) { |
| 1169 | elt = container_of(node, struct pat_idx_elt, node); |
| 1170 | if (elt->node.node.pfx == mask && |
| 1171 | memcmp(&pattern.val.ipv4.addr.s_addr, elt->node.key, 4) == 0) |
| 1172 | goto found; |
| 1173 | } |
| 1174 | } |
| 1175 | } |
| 1176 | |
| 1177 | browse_list: |
Thierry FOURNIER | c64de3f | 2013-12-10 15:08:39 +0100 | [diff] [blame] | 1178 | elt = NULL; |
Thierry FOURNIER | 01cdcd4 | 2013-12-10 15:08:01 +0100 | [diff] [blame] | 1179 | if (expr->parse == pat_parse_int || |
| 1180 | expr->parse == pat_parse_len) { |
| 1181 | list_for_each_entry(pat, &expr->patterns, list) { |
| 1182 | if (pat->flags & PAT_F_TREE) |
| 1183 | continue; |
| 1184 | if (pattern.val.range.min_set != pat->val.range.min_set) |
| 1185 | continue; |
| 1186 | if (pattern.val.range.max_set != pat->val.range.max_set) |
| 1187 | continue; |
| 1188 | if (pattern.val.range.min_set && |
| 1189 | pattern.val.range.min != pat->val.range.min) |
| 1190 | continue; |
| 1191 | if (pattern.val.range.max_set && |
| 1192 | pattern.val.range.max != pat->val.range.max) |
| 1193 | continue; |
| 1194 | goto found; |
| 1195 | } |
| 1196 | } |
| 1197 | else if (expr->parse == pat_parse_ip) { |
| 1198 | list_for_each_entry(pat, &expr->patterns, list) { |
| 1199 | if (pat->flags & PAT_F_TREE) |
| 1200 | continue; |
| 1201 | if (pattern.type != pat->type) |
| 1202 | continue; |
| 1203 | if (pattern.type == SMP_T_IPV4 && |
| 1204 | memcmp(&pattern.val.ipv4.addr, &pat->val.ipv4.addr, sizeof(pat->val.ipv4.addr)) != 0) |
| 1205 | continue; |
| 1206 | if (pattern.type == SMP_T_IPV4 && |
| 1207 | memcmp(&pattern.val.ipv4.mask, &pat->val.ipv4.mask, sizeof(pat->val.ipv4.addr)) != 0) |
| 1208 | continue; |
| 1209 | if (pattern.type == SMP_T_IPV6 && |
| 1210 | memcmp(&pattern.val.ipv6.addr, &pat->val.ipv6.addr, sizeof(pat->val.ipv6.addr)) != 0) |
| 1211 | continue; |
| 1212 | if (pattern.type == SMP_T_IPV6 && |
| 1213 | pattern.val.ipv6.mask != pat->val.ipv6.mask) |
| 1214 | continue; |
| 1215 | goto found; |
| 1216 | } |
| 1217 | } |
| 1218 | else if (expr->parse == pat_parse_str) { |
| 1219 | list_for_each_entry(pat, &expr->patterns, list) { |
| 1220 | if (pat->flags & PAT_F_TREE) |
| 1221 | continue; |
| 1222 | if (pattern.len != pat->len) |
| 1223 | continue; |
| 1224 | if ((pat->flags & PAT_F_IGNORE_CASE) && |
| 1225 | strncasecmp(pattern.ptr.str, pat->ptr.str, pat->len) != 0) |
| 1226 | continue; |
| 1227 | if (strncmp(pattern.ptr.str, pat->ptr.str, pat->len) != 0) |
| 1228 | continue; |
| 1229 | goto found; |
| 1230 | } |
| 1231 | } |
| 1232 | else if (expr->parse == pat_parse_bin) { |
| 1233 | list_for_each_entry(pat, &expr->patterns, list) { |
| 1234 | if (pat->flags & PAT_F_TREE) |
| 1235 | continue; |
| 1236 | if (pattern.len != pat->len) |
| 1237 | continue; |
| 1238 | if (memcmp(pattern.ptr.ptr, pat->ptr.ptr, pat->len) != 0) |
| 1239 | continue; |
| 1240 | goto found; |
| 1241 | } |
| 1242 | } |
| 1243 | else if (expr->parse == pat_parse_reg) { |
| 1244 | list_for_each_entry(pat, &expr->patterns, list) { |
| 1245 | if (pat->flags & PAT_F_TREE) |
| 1246 | continue; |
| 1247 | if ((pat->flags & PAT_F_IGNORE_CASE) && |
| 1248 | strcasecmp(pattern.ptr.reg->regstr, pat->ptr.reg->regstr) != 0) |
| 1249 | continue; |
| 1250 | if (strcmp(pattern.ptr.reg->regstr, pat->ptr.reg->regstr) != 0) |
| 1251 | continue; |
| 1252 | goto found; |
| 1253 | } |
| 1254 | } |
| 1255 | |
| 1256 | /* if we get there, we didn't find the pattern */ |
| 1257 | return 0; |
| 1258 | found: |
| 1259 | if (idx_elt) |
| 1260 | *idx_elt = elt; |
| 1261 | |
| 1262 | if (pat_elt) |
| 1263 | *pat_elt = pat; |
| 1264 | |
| 1265 | return 1; |
| 1266 | } |