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 | |
Willy Tarreau | b4a163a | 2014-04-02 20:55:23 +0200 | [diff] [blame] | 31 | /* pattern management function arrays */ |
| 32 | extern char *pat_match_names[PAT_MATCH_NUM]; |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 33 | extern int (*pat_parse_fcts[PAT_MATCH_NUM])(const char *, struct pattern *, int, char **); |
Willy Tarreau | b4a163a | 2014-04-02 20:55:23 +0200 | [diff] [blame] | 34 | extern int (*pat_index_fcts[PAT_MATCH_NUM])(struct pattern_expr *, struct pattern *, char **); |
| 35 | extern void (*pat_delete_fcts[PAT_MATCH_NUM])(struct pattern_expr *, struct pat_ref_elt *); |
| 36 | extern void (*pat_prune_fcts[PAT_MATCH_NUM])(struct pattern_expr *); |
| 37 | extern struct pattern *(*pat_match_fcts[PAT_MATCH_NUM])(struct sample *, struct pattern_expr *, int); |
| 38 | extern int pat_match_types[PAT_MATCH_NUM]; |
| 39 | |
Carl Henrik Lunde | f91ac19 | 2020-02-27 16:45:50 +0100 | [diff] [blame] | 40 | int pattern_finalize_config(void); |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 41 | |
Willy Tarreau | 6f8fe31 | 2013-11-28 22:24:25 +0100 | [diff] [blame] | 42 | /* return the PAT_MATCH_* index for match name "name", or < 0 if not found */ |
| 43 | static inline int pat_find_match_name(const char *name) |
| 44 | { |
| 45 | int i; |
| 46 | |
| 47 | for (i = 0; i < PAT_MATCH_NUM; i++) |
| 48 | if (strcmp(name, pat_match_names[i]) == 0) |
| 49 | return i; |
| 50 | return -1; |
| 51 | } |
| 52 | |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 53 | /* This function executes a pattern match on a sample. It applies pattern <expr> |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame^] | 54 | * to sample <smp>. The function returns NULL if the sample don't match. It returns |
Thierry FOURNIER | 1794fdf | 2014-01-17 15:25:13 +0100 | [diff] [blame] | 55 | * non-null if the sample match. If <fill> is true and the sample match, the |
| 56 | * function returns the matched pattern. In many cases, this pattern can be a |
| 57 | * static buffer. |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 58 | */ |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 59 | struct pattern *pattern_exec_match(struct pattern_head *head, struct sample *smp, int fill); |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 60 | |
| 61 | /* |
| 62 | * |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 63 | * The following function gets "pattern", duplicate it and index it in "expr" |
| 64 | * |
| 65 | */ |
| 66 | int pat_idx_list_val(struct pattern_expr *expr, struct pattern *pat, char **err); |
| 67 | int pat_idx_list_ptr(struct pattern_expr *expr, struct pattern *pat, char **err); |
| 68 | int pat_idx_list_str(struct pattern_expr *expr, struct pattern *pat, char **err); |
| 69 | int pat_idx_list_reg(struct pattern_expr *expr, struct pattern *pat, char **err); |
Thierry Fournier | 8feaa66 | 2016-02-10 22:55:20 +0100 | [diff] [blame] | 70 | int pat_idx_list_regm(struct pattern_expr *expr, struct pattern *pat, char **err); |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 71 | int pat_idx_tree_ip(struct pattern_expr *expr, struct pattern *pat, char **err); |
| 72 | int pat_idx_tree_str(struct pattern_expr *expr, struct pattern *pat, char **err); |
Willy Tarreau | b1dd9bf | 2014-05-10 08:53:48 +0200 | [diff] [blame] | 73 | int pat_idx_tree_pfx(struct pattern_expr *expr, struct pattern *pat, char **err); |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 74 | |
| 75 | /* |
| 76 | * |
Thierry FOURNIER | b113650 | 2014-01-15 11:38:49 +0100 | [diff] [blame] | 77 | * The following functions search pattern <pattern> into the pattern |
| 78 | * expression <expr>. If the pattern is found, delete it. This function |
| 79 | * never fails. |
| 80 | * |
| 81 | */ |
Thierry FOURNIER | 7acca4b | 2014-01-28 16:43:36 +0100 | [diff] [blame] | 82 | void pat_del_list_val(struct pattern_expr *expr, struct pat_ref_elt *ref); |
| 83 | void pat_del_tree_ip(struct pattern_expr *expr, struct pat_ref_elt *ref); |
| 84 | void pat_del_list_ptr(struct pattern_expr *expr, struct pat_ref_elt *ref); |
| 85 | void pat_del_tree_str(struct pattern_expr *expr, struct pat_ref_elt *ref); |
| 86 | void pat_del_list_reg(struct pattern_expr *expr, struct pat_ref_elt *ref); |
Thierry FOURNIER | b113650 | 2014-01-15 11:38:49 +0100 | [diff] [blame] | 87 | |
| 88 | /* |
| 89 | * |
Thierry FOURNIER | 6f7203d | 2014-01-14 16:24:51 +0100 | [diff] [blame] | 90 | * The following functions clean all entries of a pattern expression and |
| 91 | * reset the tree and list root. |
| 92 | * |
| 93 | */ |
| 94 | void pat_prune_val(struct pattern_expr *expr); |
| 95 | void pat_prune_ptr(struct pattern_expr *expr); |
| 96 | void pat_prune_reg(struct pattern_expr *expr); |
| 97 | |
| 98 | /* |
| 99 | * |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 100 | * The following functions are general purpose pattern matching functions. |
| 101 | * |
| 102 | */ |
| 103 | |
| 104 | |
| 105 | /* ignore the current line */ |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 106 | int pat_parse_nothing(const char *text, struct pattern *pattern, int mflags, char **err); |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 107 | |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 108 | /* Parse an integer. It is put both in min and max. */ |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 109 | int pat_parse_int(const char *text, struct pattern *pattern, int mflags, char **err); |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 110 | |
| 111 | /* Parse an version. It is put both in min and max. */ |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 112 | int pat_parse_dotted_ver(const char *text, struct pattern *pattern, int mflags, char **err); |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 113 | |
| 114 | /* Parse a range of integers delimited by either ':' or '-'. If only one |
| 115 | * integer is read, it is set as both min and max. |
| 116 | */ |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 117 | int pat_parse_range(const char *text, struct pattern *pattern, int mflags, char **err); |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 118 | |
| 119 | /* Parse a string. It is allocated and duplicated. */ |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 120 | int pat_parse_str(const char *text, struct pattern *pattern, int mflags, char **err); |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 121 | |
| 122 | /* Parse a hexa binary definition. It is allocated and duplicated. */ |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 123 | int pat_parse_bin(const char *text, struct pattern *pattern, int mflags, char **err); |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 124 | |
| 125 | /* Parse a regex. It is allocated. */ |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 126 | int pat_parse_reg(const char *text, struct pattern *pattern, int mflags, char **err); |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 127 | |
| 128 | /* Parse an IP address and an optional mask in the form addr[/mask]. |
| 129 | * The addr may either be an IPv4 address or a hostname. The mask |
| 130 | * may either be a dotted mask or a number of bits. Returns 1 if OK, |
| 131 | * otherwise 0. |
| 132 | */ |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 133 | int pat_parse_ip(const char *text, struct pattern *pattern, int mflags, char **err); |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 134 | |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 135 | /* 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] | 136 | 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] | 137 | |
| 138 | /* 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] | 139 | 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] | 140 | |
| 141 | /* 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] | 142 | 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] | 143 | |
| 144 | /* Checks that the integer in <test> is included between min and max */ |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 145 | 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] | 146 | |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 147 | /* always return false */ |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 148 | 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] | 149 | |
| 150 | /* Checks that the pattern matches the end of the tested string. */ |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 151 | 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] | 152 | |
| 153 | /* Checks that the pattern matches the beginning of the tested string. */ |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 154 | 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] | 155 | |
| 156 | /* Checks that the pattern is included inside the tested string. */ |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 157 | 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] | 158 | |
| 159 | /* Checks that the pattern is included inside the tested string, but enclosed |
| 160 | * between slashes or at the beginning or end of the string. Slashes at the |
| 161 | * beginning or end of the pattern are ignored. |
| 162 | */ |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 163 | 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] | 164 | |
| 165 | /* Checks that the pattern is included inside the tested string, but enclosed |
| 166 | * between dots or at the beginning or end of the string. Dots at the beginning |
| 167 | * or end of the pattern are ignored. |
| 168 | */ |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 169 | 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] | 170 | |
| 171 | /* 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] | 172 | 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] | 173 | |
| 174 | /* Executes a regex. It temporarily changes the data to add a trailing zero, |
| 175 | * and restores the previous character when leaving. |
| 176 | */ |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 177 | struct pattern *pat_match_reg(struct sample *smp, struct pattern_expr *expr, int fill); |
Thierry Fournier | 8feaa66 | 2016-02-10 22:55:20 +0100 | [diff] [blame] | 178 | struct pattern *pat_match_regm(struct sample *smp, struct pattern_expr *expr, int fill); |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 179 | |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 180 | /* |
| 181 | * pattern_ref manipulation. |
| 182 | */ |
| 183 | struct pat_ref *pat_ref_lookup(const char *reference); |
Thierry FOURNIER | af5a29d | 2014-03-11 14:29:22 +0100 | [diff] [blame] | 184 | struct pat_ref *pat_ref_lookupid(int unique_id); |
Thierry FOURNIER | 0d6ba51 | 2014-02-11 03:31:34 +0100 | [diff] [blame] | 185 | struct pat_ref *pat_ref_new(const char *reference, const char *display, unsigned int flags); |
| 186 | struct pat_ref *pat_ref_newid(int unique_id, const char *display, unsigned int flags); |
Baptiste Assmann | 953f74d | 2014-04-25 16:57:03 +0200 | [diff] [blame] | 187 | struct pat_ref_elt *pat_ref_find_elt(struct pat_ref *ref, const char *key); |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 188 | int pat_ref_append(struct pat_ref *ref, char *pattern, char *sample, int line); |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 189 | int pat_ref_add(struct pat_ref *ref, const char *pattern, const char *sample, char **err); |
Thierry FOURNIER | 364cfdf | 2014-01-29 19:08:49 +0100 | [diff] [blame] | 190 | int pat_ref_set(struct pat_ref *ref, const char *pattern, const char *sample, char **err); |
| 191 | int pat_ref_set_by_id(struct pat_ref *ref, struct pat_ref_elt *refelt, const char *value, char **err); |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 192 | int pat_ref_delete(struct pat_ref *ref, const char *key); |
Thierry FOURNIER | 7acca4b | 2014-01-28 16:43:36 +0100 | [diff] [blame] | 193 | int pat_ref_delete_by_id(struct pat_ref *ref, struct pat_ref_elt *refelt); |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 194 | void pat_ref_prune(struct pat_ref *ref); |
| 195 | int pat_ref_load(struct pat_ref *ref, struct pattern_expr *expr, int patflags, int soe, char **err); |
Thierry FOURNIER | 46006bd | 2014-03-21 21:45:15 +0100 | [diff] [blame] | 196 | void pat_ref_reload(struct pat_ref *ref, struct pat_ref *replace); |
| 197 | |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 198 | |
| 199 | /* |
| 200 | * pattern_head manipulation. |
| 201 | */ |
| 202 | void pattern_init_head(struct pattern_head *head); |
| 203 | void pattern_prune(struct pattern_head *head); |
Thierry FOURNIER | 94580c9 | 2014-02-11 14:36:45 +0100 | [diff] [blame] | 204 | int pattern_read_from_file(struct pattern_head *head, unsigned int refflags, const char *filename, int patflags, int load_smp, char **err, const char *file, int line); |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 205 | |
| 206 | /* |
| 207 | * pattern_expr manipulation. |
| 208 | */ |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 209 | void pattern_init_expr(struct pattern_expr *expr); |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 210 | struct pattern_expr *pattern_lookup_expr(struct pattern_head *head, struct pat_ref *ref); |
Thierry FOURNIER | 315ec42 | 2014-11-24 11:14:42 +0100 | [diff] [blame] | 211 | struct pattern_expr *pattern_new_expr(struct pattern_head *head, struct pat_ref *ref, |
Emeric Brun | 7d27f3c | 2017-07-03 17:54:23 +0200 | [diff] [blame] | 212 | int patflags, char **err, int *reuse); |
Thierry FOURNIER | 12ba0c2 | 2015-08-14 00:02:11 +0200 | [diff] [blame] | 213 | struct sample_data **pattern_find_smp(struct pattern_expr *expr, struct pat_ref_elt *elt); |
Thierry FOURNIER | 7acca4b | 2014-01-28 16:43:36 +0100 | [diff] [blame] | 214 | int pattern_delete(struct pattern_expr *expr, struct pat_ref_elt *ref); |
Thierry FOURNIER | 01cdcd4 | 2013-12-10 15:08:01 +0100 | [diff] [blame] | 215 | |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 216 | |
| 217 | #endif |