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