Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 1 | /* |
Willy Tarreau | 8892235 | 2009-10-04 22:02:50 +0200 | [diff] [blame] | 2 | * include/types/acl.h |
| 3 | * This file provides structures and types for ACLs. |
| 4 | * |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 5 | * Copyright (C) 2000-2012 Willy Tarreau - w@1wt.eu |
Willy Tarreau | 8892235 | 2009-10-04 22:02:50 +0200 | [diff] [blame] | 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 | */ |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 21 | |
| 22 | #ifndef _TYPES_ACL_H |
| 23 | #define _TYPES_ACL_H |
| 24 | |
| 25 | #include <common/compat.h> |
| 26 | #include <common/config.h> |
| 27 | #include <common/mini-clist.h> |
| 28 | |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 29 | #include <types/arg.h> |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 30 | #include <types/auth.h> |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 31 | #include <types/proxy.h> |
Willy Tarreau | cd3b094 | 2012-04-27 21:52:18 +0200 | [diff] [blame] | 32 | #include <types/sample.h> |
Willy Tarreau | 0b1cd94 | 2010-05-16 22:18:27 +0200 | [diff] [blame] | 33 | #include <types/server.h> |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 34 | #include <types/session.h> |
| 35 | |
Willy Tarreau | 438b0f3 | 2010-05-10 22:29:06 +0200 | [diff] [blame] | 36 | #include <ebmbtree.h> |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 37 | |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 38 | /* Pattern matching function result. |
| 39 | * |
| 40 | * We're using a 3-state matching system : |
| 41 | * - PASS : at least one pattern already matches |
| 42 | * - MISS : some data is missing to decide if some rules may finally match. |
| 43 | * - FAIL : no mattern may ever match |
| 44 | * |
| 45 | * We assign values 0, 1 and 3 to FAIL, MISS and PASS respectively, so that we |
| 46 | * can make use of standard arithmetics for the truth tables below : |
| 47 | * |
| 48 | * x | !x x&y | F(0) | M(1) | P(3) x|y | F(0) | M(1) | P(3) |
| 49 | * ------+----- -----+------+------+----- -----+------+------+----- |
| 50 | * F(0) | P(3) F(0)| F(0) | F(0) | F(0) F(0)| F(0) | M(1) | P(3) |
| 51 | * M(1) | M(1) M(1)| F(0) | M(1) | M(1) M(1)| M(1) | M(1) | P(3) |
| 52 | * P(3) | F(0) P(3)| F(0) | M(1) | P(3) P(3)| P(3) | P(3) | P(3) |
| 53 | * |
| 54 | * neg(x) = (3 >> x) and(x,y) = (x & y) or(x,y) = (x | y) |
| 55 | * |
| 56 | */ |
| 57 | |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 58 | enum { |
| 59 | ACL_PAT_FAIL = 0, /* test failed */ |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 60 | ACL_PAT_MISS = 1, /* test may pass with more info */ |
| 61 | ACL_PAT_PASS = 3, /* test passed */ |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 62 | }; |
| 63 | |
| 64 | /* Condition polarity. It makes it easier for any option to choose between |
| 65 | * IF/UNLESS if it can store that information within the condition itself. |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 66 | * Those should be interpreted as "IF/UNLESS result == PASS". |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 67 | */ |
| 68 | enum { |
| 69 | ACL_COND_NONE, /* no polarity set yet */ |
| 70 | ACL_COND_IF, /* positive condition (after 'if') */ |
| 71 | ACL_COND_UNLESS, /* negative condition (after 'unless') */ |
| 72 | }; |
| 73 | |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 74 | /* possible flags for expressions or patterns */ |
| 75 | enum { |
| 76 | ACL_PAT_F_IGNORE_CASE = 1 << 0, /* ignore case */ |
| 77 | ACL_PAT_F_FROM_FILE = 1 << 1, /* pattern comes from a file */ |
Willy Tarreau | 438b0f3 | 2010-05-10 22:29:06 +0200 | [diff] [blame] | 78 | ACL_PAT_F_TREE_OK = 1 << 2, /* the pattern parser is allowed to build a tree */ |
| 79 | ACL_PAT_F_TREE = 1 << 3, /* some patterns are arranged in a tree */ |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 80 | }; |
| 81 | |
Willy Tarreau | 5adeda1 | 2013-03-31 22:13:34 +0200 | [diff] [blame] | 82 | /* ACL match methods */ |
| 83 | enum { |
| 84 | ACL_MATCH_FOUND, /* just ensure that fetch found the sample */ |
| 85 | ACL_MATCH_BOOL, /* match fetch's integer value as boolean */ |
| 86 | ACL_MATCH_INT, /* unsigned integer (int) */ |
| 87 | ACL_MATCH_IP, /* IPv4/IPv6 address (IP) */ |
| 88 | ACL_MATCH_BIN, /* hex string (bin) */ |
| 89 | ACL_MATCH_LEN, /* string length (str -> int) */ |
| 90 | ACL_MATCH_STR, /* exact string match (str) */ |
| 91 | ACL_MATCH_BEG, /* beginning of string (str) */ |
| 92 | ACL_MATCH_SUB, /* substring (str) */ |
| 93 | ACL_MATCH_DIR, /* directory-like sub-string (str) */ |
| 94 | ACL_MATCH_DOM, /* domain-like sub-string (str) */ |
| 95 | ACL_MATCH_END, /* end of string (str) */ |
| 96 | ACL_MATCH_REG, /* regex (str -> reg) */ |
| 97 | /* keep this one last */ |
| 98 | ACL_MATCH_NUM |
| 99 | }; |
| 100 | |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 101 | /* How to store a time range and the valid days in 29 bits */ |
| 102 | struct acl_time { |
| 103 | int dow:7; /* 1 bit per day of week: 0-6 */ |
| 104 | int h1:5, m1:6; /* 0..24:0..60. Use 0:0 for all day. */ |
| 105 | int h2:5, m2:6; /* 0..24:0..60. Use 24:0 for all day. */ |
| 106 | }; |
| 107 | |
Thierry FOURNIER | dd69a04 | 2013-11-22 19:14:42 +0100 | [diff] [blame^] | 108 | /* This contain each tree indexed entry. This struct permit to associate |
| 109 | * "sample" with a tree entry. It is used with maps. |
| 110 | */ |
| 111 | struct acl_idx_elt { |
| 112 | struct sample_storage *smp; |
| 113 | struct ebmb_node node; |
| 114 | }; |
| 115 | |
Willy Tarreau | c92ddbc | 2012-04-27 22:10:57 +0200 | [diff] [blame] | 116 | /* This describes one ACL pattern, which might be a single value or a tree of |
| 117 | * values. All patterns for a single ACL expression are linked together. Some |
| 118 | * of them might have a type (eg: IP). Right now, the types are shared with |
| 119 | * the samples, though it is possible that in the future this will change to |
| 120 | * accommodate for other types (eg: meth, regex). Unsigned and constant types |
| 121 | * are preferred when there is a doubt. |
| 122 | */ |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 123 | struct acl_pattern { |
| 124 | struct list list; /* chaining */ |
Willy Tarreau | c92ddbc | 2012-04-27 22:10:57 +0200 | [diff] [blame] | 125 | int type; /* type of the ACL pattern (SMP_T_*) */ |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 126 | union { |
| 127 | int i; /* integer value */ |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 128 | struct { |
| 129 | signed long long min, max; |
| 130 | int min_set :1; |
| 131 | int max_set :1; |
| 132 | } range; /* integer range */ |
Willy Tarreau | a67fad9 | 2007-05-08 19:50:09 +0200 | [diff] [blame] | 133 | struct { |
| 134 | struct in_addr addr; |
| 135 | struct in_addr mask; |
| 136 | } ipv4; /* IPv4 address */ |
Willy Tarreau | ceb4ac9 | 2012-04-28 00:41:46 +0200 | [diff] [blame] | 137 | struct { |
| 138 | struct in6_addr addr; |
| 139 | unsigned char mask; /* number of bits */ |
| 140 | } ipv6; /* IPv6 address/mask */ |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 141 | struct acl_time time; /* valid hours and days */ |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 142 | unsigned int group_mask; |
Willy Tarreau | e56cda9 | 2010-05-11 23:25:05 +0200 | [diff] [blame] | 143 | struct eb_root *tree; /* tree storing all values if any */ |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 144 | } val; /* direct value */ |
| 145 | union { |
| 146 | void *ptr; /* any data */ |
| 147 | char *str; /* any string */ |
Hiroaki Nakamura | 7035132 | 2013-01-13 15:00:42 +0900 | [diff] [blame] | 148 | regex *reg; /* a compiled regex */ |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 149 | } ptr; /* indirect values, allocated */ |
Krzysztof Piotr Oledzki | 8001d61 | 2008-05-31 13:53:23 +0200 | [diff] [blame] | 150 | void(*freeptrbuf)(void *ptr); /* a destructor able to free objects from the ptr */ |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 151 | int len; /* data length when required */ |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 152 | int flags; /* expr or pattern flags. */ |
Thierry FOURNIER | dd69a04 | 2013-11-22 19:14:42 +0100 | [diff] [blame^] | 153 | struct sample_storage *smp; /* used to store a pointer to sample value associated |
| 154 | with the match. It is used with maps */ |
| 155 | |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 156 | }; |
| 157 | |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 158 | /* some dummy declarations to silent the compiler */ |
| 159 | struct proxy; |
| 160 | struct session; |
| 161 | |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 162 | /* |
Willy Tarreau | c92ddbc | 2012-04-27 22:10:57 +0200 | [diff] [blame] | 163 | * ACL keyword: Associates keywords with parsers, methods to retrieve the value and testers. |
| 164 | */ |
| 165 | /* |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 166 | * NOTE: |
| 167 | * The 'parse' function is called to parse words in the configuration. It must |
| 168 | * return the number of valid words read. 0 = error. The 'opaque' argument may |
| 169 | * be used by functions which need to maintain a context between consecutive |
| 170 | * values. It is initialized to zero before the first call, and passed along |
| 171 | * successive calls. |
| 172 | */ |
| 173 | |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 174 | struct acl_expr; |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 175 | struct acl_keyword { |
| 176 | const char *kw; |
Willy Tarreau | 8ed669b | 2013-01-11 15:49:37 +0100 | [diff] [blame] | 177 | char *fetch_kw; |
Thierry FOURNIER | dd69a04 | 2013-11-22 19:14:42 +0100 | [diff] [blame^] | 178 | int (*parse)(const char **text, struct acl_pattern *pattern, struct sample_storage *smp, int *opaque, char **err); |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 179 | int (*match)(struct sample *smp, struct acl_pattern *pattern); |
Willy Tarreau | 61612d4 | 2012-04-19 18:42:05 +0200 | [diff] [blame] | 180 | /* must be after the config params */ |
Willy Tarreau | 8ed669b | 2013-01-11 15:49:37 +0100 | [diff] [blame] | 181 | struct sample_fetch *smp; /* the sample fetch we depend on */ |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 182 | }; |
| 183 | |
| 184 | /* |
| 185 | * A keyword list. It is a NULL-terminated array of keywords. It embeds a |
| 186 | * struct list in order to be linked to other lists, allowing it to easily |
| 187 | * be declared where it is needed, and linked without duplicating data nor |
| 188 | * allocating memory. |
| 189 | */ |
| 190 | struct acl_kw_list { |
| 191 | struct list list; |
| 192 | struct acl_keyword kw[VAR_ARRAY]; |
| 193 | }; |
| 194 | |
| 195 | /* |
| 196 | * Description of an ACL expression. |
Willy Tarreau | d76a98a | 2013-03-31 18:34:33 +0200 | [diff] [blame] | 197 | * The expression is part of a list. It contains pointers to the keyword, the |
| 198 | * parse and match functions which default to the keyword's, the sample fetch |
| 199 | * descriptor which also defaults to the keyword's, and a list or tree of |
| 200 | * patterns to test against. The structure is organized so that the hot parts |
| 201 | * are grouped together in order to optimize caching. |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 202 | */ |
| 203 | struct acl_expr { |
Thierry FOURNIER | dd69a04 | 2013-11-22 19:14:42 +0100 | [diff] [blame^] | 204 | int (*parse)(const char **text, struct acl_pattern *pattern, struct sample_storage *smp, int *opaque, char **err); |
Willy Tarreau | d76a98a | 2013-03-31 18:34:33 +0200 | [diff] [blame] | 205 | int (*match)(struct sample *smp, struct acl_pattern *pattern); |
Thierry FOURNIER | 348971e | 2013-11-21 10:50:10 +0100 | [diff] [blame] | 206 | struct sample_expr *smp; /* the sample expression we depend on */ |
Willy Tarreau | d76a98a | 2013-03-31 18:34:33 +0200 | [diff] [blame] | 207 | struct list patterns; /* list of acl_patterns */ |
Willy Tarreau | 438b0f3 | 2010-05-10 22:29:06 +0200 | [diff] [blame] | 208 | struct eb_root pattern_tree; /* may be used for lookup in large datasets */ |
Willy Tarreau | d76a98a | 2013-03-31 18:34:33 +0200 | [diff] [blame] | 209 | struct list list; /* chaining */ |
Willy Tarreau | 93fddf1 | 2013-03-31 22:59:32 +0200 | [diff] [blame] | 210 | const char *kw; /* points to the ACL kw's name or fetch's name (must not free) */ |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 211 | }; |
| 212 | |
Willy Tarreau | c92ddbc | 2012-04-27 22:10:57 +0200 | [diff] [blame] | 213 | /* The acl will be linked to from the proxy where it is declared */ |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 214 | struct acl { |
| 215 | struct list list; /* chaining */ |
| 216 | char *name; /* acl name */ |
| 217 | struct list expr; /* list of acl_exprs */ |
| 218 | int cache_idx; /* ACL index in cache */ |
Willy Tarreau | a91d0a5 | 2013-03-25 08:12:18 +0100 | [diff] [blame] | 219 | unsigned int use; /* or'ed bit mask of all acl_expr's SMP_USE_* */ |
| 220 | unsigned int val; /* or'ed bit mask of all acl_expr's SMP_VAL_* */ |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 221 | }; |
| 222 | |
| 223 | /* the condition will be linked to from an action in a proxy */ |
| 224 | struct acl_term { |
| 225 | struct list list; /* chaining */ |
| 226 | struct acl *acl; /* acl pointed to by this term */ |
| 227 | int neg; /* 1 if the ACL result must be negated */ |
| 228 | }; |
| 229 | |
| 230 | struct acl_term_suite { |
| 231 | struct list list; /* chaining of term suites */ |
| 232 | struct list terms; /* list of acl_terms */ |
| 233 | }; |
| 234 | |
| 235 | struct acl_cond { |
| 236 | struct list list; /* Some specific tests may use multiple conditions */ |
| 237 | struct list suites; /* list of acl_term_suites */ |
| 238 | int pol; /* polarity: ACL_COND_IF / ACL_COND_UNLESS */ |
Willy Tarreau | a91d0a5 | 2013-03-25 08:12:18 +0100 | [diff] [blame] | 239 | unsigned int use; /* or'ed bit mask of all suites's SMP_USE_* */ |
| 240 | unsigned int val; /* or'ed bit mask of all suites's SMP_VAL_* */ |
Willy Tarreau | 8892235 | 2009-10-04 22:02:50 +0200 | [diff] [blame] | 241 | const char *file; /* config file where the condition is declared */ |
Willy Tarreau | a980263 | 2008-07-25 19:13:19 +0200 | [diff] [blame] | 242 | int line; /* line in the config file where the condition is declared */ |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 243 | }; |
| 244 | |
Thierry FOURNIER | 319e495 | 2013-11-22 17:25:35 +0100 | [diff] [blame] | 245 | extern char *acl_match_names[ACL_MATCH_NUM]; |
Thierry FOURNIER | dd69a04 | 2013-11-22 19:14:42 +0100 | [diff] [blame^] | 246 | extern int (*acl_parse_fcts[ACL_MATCH_NUM])(const char **, struct acl_pattern *, struct sample_storage *, int *, char **); |
Thierry FOURNIER | 319e495 | 2013-11-22 17:25:35 +0100 | [diff] [blame] | 247 | extern int (*acl_match_fcts[ACL_MATCH_NUM])(struct sample *, struct acl_pattern *); |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 248 | |
| 249 | #endif /* _TYPES_ACL_H */ |
| 250 | |
| 251 | /* |
| 252 | * Local variables: |
| 253 | * c-indent-level: 8 |
| 254 | * c-basic-offset: 8 |
| 255 | * End: |
| 256 | */ |