Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 1 | /* |
| 2 | * include/proto/pattern.h |
| 3 | * This file provides structures and types for pattern matching. |
| 4 | * |
| 5 | * Copyright (C) 2000-2013 Willy Tarreau - w@1wt.eu |
| 6 | * |
| 7 | * This library is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU Lesser General Public |
| 9 | * License as published by the Free Software Foundation, version 2.1 |
| 10 | * exclusively. |
| 11 | * |
| 12 | * This library is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * Lesser General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU Lesser General Public |
| 18 | * License along with this library; if not, write to the Free Software |
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 | */ |
| 21 | |
| 22 | #ifndef _PROTO_PATTERN_H |
| 23 | #define _PROTO_PATTERN_H |
| 24 | |
Willy Tarreau | 6f8fe31 | 2013-11-28 22:24:25 +0100 | [diff] [blame] | 25 | #include <string.h> |
| 26 | |
Willy Tarreau | 0cba607 | 2013-11-28 22:21:02 +0100 | [diff] [blame] | 27 | #include <common/config.h> |
| 28 | #include <common/standard.h> |
| 29 | #include <types/pattern.h> |
| 30 | |
Thierry FOURNIER | 7148ce6 | 2013-12-06 19:06:43 +0100 | [diff] [blame] | 31 | /* parse the <args> with <expr> compliant parser. <pattern> is a context for |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 32 | * the current parsed acl. It must initialized at NULL: |
| 33 | * |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 34 | * struct pattern *pattern = NULL |
| 35 | * pattern_register(..., &pattern, ...); |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 36 | * |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 37 | * patflag are a lot of 'PAT_F_*' flags pattern compatible. see |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 38 | * <types/acl.h>. |
| 39 | * |
Thierry FOURNIER | 7148ce6 | 2013-12-06 19:06:43 +0100 | [diff] [blame] | 40 | * The function returns 1 if the processing is ok, return 0 |
| 41 | * if the parser fails, with <err> message filled. |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 42 | */ |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 43 | int pattern_register(struct pattern_expr *expr, const char *arg, struct sample_storage *smp, int patflags, char **err); |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 44 | |
Willy Tarreau | 6f8fe31 | 2013-11-28 22:24:25 +0100 | [diff] [blame] | 45 | /* return the PAT_MATCH_* index for match name "name", or < 0 if not found */ |
| 46 | static inline int pat_find_match_name(const char *name) |
| 47 | { |
| 48 | int i; |
| 49 | |
| 50 | for (i = 0; i < PAT_MATCH_NUM; i++) |
| 51 | if (strcmp(name, pat_match_names[i]) == 0) |
| 52 | return i; |
| 53 | return -1; |
| 54 | } |
| 55 | |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 56 | /* This function executes a pattern match on a sample. It applies pattern <expr> |
Thierry FOURNIER | 1794fdf | 2014-01-17 15:25:13 +0100 | [diff] [blame] | 57 | * to sample <smp>. The function returns NULL if the sample dont match. It returns |
| 58 | * non-null if the sample match. If <fill> is true and the sample match, the |
| 59 | * function returns the matched pattern. In many cases, this pattern can be a |
| 60 | * static buffer. |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 61 | */ |
Thierry FOURNIER | 1794fdf | 2014-01-17 15:25:13 +0100 | [diff] [blame] | 62 | struct pattern *pattern_exec_match(struct pattern_expr *expr, struct sample *smp, int fill); |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 63 | |
| 64 | /* |
| 65 | * |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 66 | * The following function gets "pattern", duplicate it and index it in "expr" |
| 67 | * |
| 68 | */ |
| 69 | int pat_idx_list_val(struct pattern_expr *expr, struct pattern *pat, char **err); |
| 70 | int pat_idx_list_ptr(struct pattern_expr *expr, struct pattern *pat, char **err); |
| 71 | int pat_idx_list_str(struct pattern_expr *expr, struct pattern *pat, char **err); |
| 72 | int pat_idx_list_reg(struct pattern_expr *expr, struct pattern *pat, char **err); |
| 73 | int pat_idx_tree_ip(struct pattern_expr *expr, struct pattern *pat, char **err); |
| 74 | int pat_idx_tree_str(struct pattern_expr *expr, struct pattern *pat, char **err); |
| 75 | |
| 76 | /* |
| 77 | * |
Thierry FOURNIER | b113650 | 2014-01-15 11:38:49 +0100 | [diff] [blame] | 78 | * The following functions search pattern <pattern> into the pattern |
| 79 | * expression <expr>. If the pattern is found, delete it. This function |
| 80 | * never fails. |
| 81 | * |
| 82 | */ |
| 83 | void pat_del_list_val(struct pattern_expr *expr, struct pattern *pat); |
| 84 | void pat_del_tree_ip(struct pattern_expr *expr, struct pattern *pat); |
| 85 | void pat_del_list_ptr(struct pattern_expr *expr, struct pattern *pat); |
| 86 | void pat_del_tree_str(struct pattern_expr *expr, struct pattern *pat); |
| 87 | void pat_del_list_str(struct pattern_expr *expr, struct pattern *pat); |
| 88 | void pat_del_list_reg(struct pattern_expr *expr, struct pattern *pat); |
| 89 | |
| 90 | /* |
| 91 | * |
Thierry FOURNIER | 6f7203d | 2014-01-14 16:24:51 +0100 | [diff] [blame^] | 92 | * The following functions clean all entries of a pattern expression and |
| 93 | * reset the tree and list root. |
| 94 | * |
| 95 | */ |
| 96 | void pat_prune_val(struct pattern_expr *expr); |
| 97 | void pat_prune_ptr(struct pattern_expr *expr); |
| 98 | void pat_prune_reg(struct pattern_expr *expr); |
| 99 | |
| 100 | /* |
| 101 | * |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 102 | * The following functions are general purpose pattern matching functions. |
| 103 | * |
| 104 | */ |
| 105 | |
| 106 | |
| 107 | /* ignore the current line */ |
Thierry FOURNIER | edc15c3 | 2013-12-13 15:36:59 +0100 | [diff] [blame] | 108 | int pat_parse_nothing(const char *text, struct pattern *pattern, char **err); |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 109 | |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 110 | /* Parse an integer. It is put both in min and max. */ |
Thierry FOURNIER | edc15c3 | 2013-12-13 15:36:59 +0100 | [diff] [blame] | 111 | int pat_parse_int(const char *text, struct pattern *pattern, char **err); |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 112 | |
Thierry FOURNIER | cc0e0b3 | 2013-12-06 16:56:40 +0100 | [diff] [blame] | 113 | /* Parse len like an integer, but specify expected string type */ |
Thierry FOURNIER | edc15c3 | 2013-12-13 15:36:59 +0100 | [diff] [blame] | 114 | int pat_parse_len(const char *text, struct pattern *pattern, char **err); |
Thierry FOURNIER | cc0e0b3 | 2013-12-06 16:56:40 +0100 | [diff] [blame] | 115 | |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 116 | /* Parse an version. It is put both in min and max. */ |
Thierry FOURNIER | edc15c3 | 2013-12-13 15:36:59 +0100 | [diff] [blame] | 117 | 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] | 118 | |
| 119 | /* Parse a range of integers delimited by either ':' or '-'. If only one |
| 120 | * integer is read, it is set as both min and max. |
| 121 | */ |
Thierry FOURNIER | edc15c3 | 2013-12-13 15:36:59 +0100 | [diff] [blame] | 122 | int pat_parse_range(const char *text, struct pattern *pattern, char **err); |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 123 | |
| 124 | /* Parse a string. It is allocated and duplicated. */ |
Thierry FOURNIER | edc15c3 | 2013-12-13 15:36:59 +0100 | [diff] [blame] | 125 | int pat_parse_str(const char *text, struct pattern *pattern, char **err); |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 126 | |
| 127 | /* Parse a hexa binary definition. It is allocated and duplicated. */ |
Thierry FOURNIER | edc15c3 | 2013-12-13 15:36:59 +0100 | [diff] [blame] | 128 | int pat_parse_bin(const char *text, struct pattern *pattern, char **err); |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 129 | |
| 130 | /* Parse a regex. It is allocated. */ |
Thierry FOURNIER | edc15c3 | 2013-12-13 15:36:59 +0100 | [diff] [blame] | 131 | int pat_parse_reg(const char *text, struct pattern *pattern, char **err); |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 132 | |
| 133 | /* Parse an IP address and an optional mask in the form addr[/mask]. |
| 134 | * The addr may either be an IPv4 address or a hostname. The mask |
| 135 | * may either be a dotted mask or a number of bits. Returns 1 if OK, |
| 136 | * otherwise 0. |
| 137 | */ |
Thierry FOURNIER | edc15c3 | 2013-12-13 15:36:59 +0100 | [diff] [blame] | 138 | int pat_parse_ip(const char *text, struct pattern *pattern, char **err); |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 139 | |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 140 | /* NB: For two strings to be identical, it is required that their lengths match */ |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 141 | struct pattern *pat_match_str(struct sample *smp, struct pattern_expr *expr, int fill); |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 142 | |
| 143 | /* NB: For two binary buffers to be identical, it is required that their lengths match */ |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 144 | struct pattern *pat_match_bin(struct sample *smp, struct pattern_expr *expr, int fill); |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 145 | |
| 146 | /* Checks that the length of the pattern in <test> is included between min and max */ |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 147 | struct pattern *pat_match_len(struct sample *smp, struct pattern_expr *expr, int fill); |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 148 | |
| 149 | /* Checks that the integer in <test> is included between min and max */ |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 150 | struct pattern *pat_match_int(struct sample *smp, struct pattern_expr *expr, int fill); |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 151 | |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 152 | /* always return false */ |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 153 | struct pattern *pat_match_nothing(struct sample *smp, struct pattern_expr *expr, int fill); |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 154 | |
| 155 | /* Checks that the pattern matches the end of the tested string. */ |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 156 | struct pattern *pat_match_end(struct sample *smp, struct pattern_expr *expr, int fill); |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 157 | |
| 158 | /* Checks that the pattern matches the beginning of the tested string. */ |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 159 | struct pattern *pat_match_beg(struct sample *smp, struct pattern_expr *expr, int fill); |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 160 | |
| 161 | /* Checks that the pattern is included inside the tested string. */ |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 162 | struct pattern *pat_match_sub(struct sample *smp, struct pattern_expr *expr, int fill); |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 163 | |
| 164 | /* Checks that the pattern is included inside the tested string, but enclosed |
| 165 | * between slashes or at the beginning or end of the string. Slashes at the |
| 166 | * beginning or end of the pattern are ignored. |
| 167 | */ |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 168 | struct pattern *pat_match_dir(struct sample *smp, struct pattern_expr *expr, int fill); |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 169 | |
| 170 | /* Checks that the pattern is included inside the tested string, but enclosed |
| 171 | * between dots or at the beginning or end of the string. Dots at the beginning |
| 172 | * or end of the pattern are ignored. |
| 173 | */ |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 174 | struct pattern *pat_match_dom(struct sample *smp, struct pattern_expr *expr, int fill); |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 175 | |
| 176 | /* Check that the IPv4 address in <test> matches the IP/mask in pattern */ |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 177 | struct pattern *pat_match_ip(struct sample *smp, struct pattern_expr *expr, int fill); |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 178 | |
| 179 | /* Executes a regex. It temporarily changes the data to add a trailing zero, |
| 180 | * and restores the previous character when leaving. |
| 181 | */ |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 182 | struct pattern *pat_match_reg(struct sample *smp, struct pattern_expr *expr, int fill); |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 183 | |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 184 | int pattern_read_from_file(struct pattern_expr *expr, const char *filename, int patflags, char **err); |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 185 | void pattern_init_expr(struct pattern_expr *expr); |
Thierry FOURNIER | e1bcac5 | 2013-12-13 16:09:50 +0100 | [diff] [blame] | 186 | int pattern_lookup(const char *args, struct pattern_expr *expr, struct pattern_list **pat_elt, struct pattern_tree **idx_elt, char **err); |
Thierry FOURNIER | b113650 | 2014-01-15 11:38:49 +0100 | [diff] [blame] | 187 | int pattern_delete(const char *key, struct pattern_expr *expr, char **err); |
Thierry FOURNIER | 6f7203d | 2014-01-14 16:24:51 +0100 | [diff] [blame^] | 188 | void pattern_prune(struct pattern_expr *expr); |
Thierry FOURNIER | 01cdcd4 | 2013-12-10 15:08:01 +0100 | [diff] [blame] | 189 | |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 190 | |
| 191 | #endif |