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 | |
Willy Tarreau | c92ddbc | 2012-04-27 22:10:57 +0200 | [diff] [blame] | 108 | /* This describes one ACL pattern, which might be a single value or a tree of |
| 109 | * values. All patterns for a single ACL expression are linked together. Some |
| 110 | * of them might have a type (eg: IP). Right now, the types are shared with |
| 111 | * the samples, though it is possible that in the future this will change to |
| 112 | * accommodate for other types (eg: meth, regex). Unsigned and constant types |
| 113 | * are preferred when there is a doubt. |
| 114 | */ |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 115 | struct acl_pattern { |
| 116 | struct list list; /* chaining */ |
Willy Tarreau | c92ddbc | 2012-04-27 22:10:57 +0200 | [diff] [blame] | 117 | int type; /* type of the ACL pattern (SMP_T_*) */ |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 118 | union { |
| 119 | int i; /* integer value */ |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 120 | struct { |
| 121 | signed long long min, max; |
| 122 | int min_set :1; |
| 123 | int max_set :1; |
| 124 | } range; /* integer range */ |
Willy Tarreau | a67fad9 | 2007-05-08 19:50:09 +0200 | [diff] [blame] | 125 | struct { |
| 126 | struct in_addr addr; |
| 127 | struct in_addr mask; |
| 128 | } ipv4; /* IPv4 address */ |
Willy Tarreau | ceb4ac9 | 2012-04-28 00:41:46 +0200 | [diff] [blame] | 129 | struct { |
| 130 | struct in6_addr addr; |
| 131 | unsigned char mask; /* number of bits */ |
| 132 | } ipv6; /* IPv6 address/mask */ |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 133 | struct acl_time time; /* valid hours and days */ |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 134 | unsigned int group_mask; |
Willy Tarreau | e56cda9 | 2010-05-11 23:25:05 +0200 | [diff] [blame] | 135 | struct eb_root *tree; /* tree storing all values if any */ |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 136 | } val; /* direct value */ |
| 137 | union { |
| 138 | void *ptr; /* any data */ |
| 139 | char *str; /* any string */ |
Hiroaki Nakamura | 7035132 | 2013-01-13 15:00:42 +0900 | [diff] [blame] | 140 | regex *reg; /* a compiled regex */ |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 141 | } ptr; /* indirect values, allocated */ |
Krzysztof Piotr Oledzki | 8001d61 | 2008-05-31 13:53:23 +0200 | [diff] [blame] | 142 | 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] | 143 | int len; /* data length when required */ |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 144 | int flags; /* expr or pattern flags. */ |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 145 | }; |
| 146 | |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 147 | /* some dummy declarations to silent the compiler */ |
| 148 | struct proxy; |
| 149 | struct session; |
| 150 | |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 151 | /* |
Willy Tarreau | c92ddbc | 2012-04-27 22:10:57 +0200 | [diff] [blame] | 152 | * ACL keyword: Associates keywords with parsers, methods to retrieve the value and testers. |
| 153 | */ |
| 154 | /* |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 155 | * NOTE: |
| 156 | * The 'parse' function is called to parse words in the configuration. It must |
| 157 | * return the number of valid words read. 0 = error. The 'opaque' argument may |
| 158 | * be used by functions which need to maintain a context between consecutive |
| 159 | * values. It is initialized to zero before the first call, and passed along |
| 160 | * successive calls. |
| 161 | */ |
| 162 | |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 163 | struct acl_expr; |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 164 | struct acl_keyword { |
| 165 | const char *kw; |
Willy Tarreau | 8ed669b | 2013-01-11 15:49:37 +0100 | [diff] [blame] | 166 | char *fetch_kw; |
Willy Tarreau | 7dcb648 | 2012-04-27 17:52:25 +0200 | [diff] [blame] | 167 | int (*parse)(const char **text, struct acl_pattern *pattern, int *opaque, char **err); |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 168 | int (*match)(struct sample *smp, struct acl_pattern *pattern); |
Willy Tarreau | 61612d4 | 2012-04-19 18:42:05 +0200 | [diff] [blame] | 169 | /* must be after the config params */ |
Willy Tarreau | 8ed669b | 2013-01-11 15:49:37 +0100 | [diff] [blame] | 170 | struct sample_fetch *smp; /* the sample fetch we depend on */ |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 171 | }; |
| 172 | |
| 173 | /* |
| 174 | * A keyword list. It is a NULL-terminated array of keywords. It embeds a |
| 175 | * struct list in order to be linked to other lists, allowing it to easily |
| 176 | * be declared where it is needed, and linked without duplicating data nor |
| 177 | * allocating memory. |
| 178 | */ |
| 179 | struct acl_kw_list { |
| 180 | struct list list; |
| 181 | struct acl_keyword kw[VAR_ARRAY]; |
| 182 | }; |
| 183 | |
| 184 | /* |
| 185 | * Description of an ACL expression. |
Willy Tarreau | d76a98a | 2013-03-31 18:34:33 +0200 | [diff] [blame] | 186 | * The expression is part of a list. It contains pointers to the keyword, the |
| 187 | * parse and match functions which default to the keyword's, the sample fetch |
| 188 | * descriptor which also defaults to the keyword's, and a list or tree of |
| 189 | * patterns to test against. The structure is organized so that the hot parts |
| 190 | * are grouped together in order to optimize caching. |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 191 | */ |
| 192 | struct acl_expr { |
Willy Tarreau | d76a98a | 2013-03-31 18:34:33 +0200 | [diff] [blame] | 193 | int (*parse)(const char **text, struct acl_pattern *pattern, int *opaque, char **err); |
| 194 | int (*match)(struct sample *smp, struct acl_pattern *pattern); |
| 195 | struct arg *args; /* optional fetch argument list (eg: header or cookie name) */ |
| 196 | struct sample_fetch *smp; /* the sample fetch we depend on */ |
| 197 | struct list patterns; /* list of acl_patterns */ |
Willy Tarreau | 438b0f3 | 2010-05-10 22:29:06 +0200 | [diff] [blame] | 198 | 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] | 199 | struct list list; /* chaining */ |
Willy Tarreau | 93fddf1 | 2013-03-31 22:59:32 +0200 | [diff] [blame] | 200 | 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] | 201 | }; |
| 202 | |
Willy Tarreau | c92ddbc | 2012-04-27 22:10:57 +0200 | [diff] [blame] | 203 | /* 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] | 204 | struct acl { |
| 205 | struct list list; /* chaining */ |
| 206 | char *name; /* acl name */ |
| 207 | struct list expr; /* list of acl_exprs */ |
| 208 | int cache_idx; /* ACL index in cache */ |
Willy Tarreau | a91d0a5 | 2013-03-25 08:12:18 +0100 | [diff] [blame] | 209 | unsigned int use; /* or'ed bit mask of all acl_expr's SMP_USE_* */ |
| 210 | 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] | 211 | }; |
| 212 | |
| 213 | /* the condition will be linked to from an action in a proxy */ |
| 214 | struct acl_term { |
| 215 | struct list list; /* chaining */ |
| 216 | struct acl *acl; /* acl pointed to by this term */ |
| 217 | int neg; /* 1 if the ACL result must be negated */ |
| 218 | }; |
| 219 | |
| 220 | struct acl_term_suite { |
| 221 | struct list list; /* chaining of term suites */ |
| 222 | struct list terms; /* list of acl_terms */ |
| 223 | }; |
| 224 | |
| 225 | struct acl_cond { |
| 226 | struct list list; /* Some specific tests may use multiple conditions */ |
| 227 | struct list suites; /* list of acl_term_suites */ |
| 228 | int pol; /* polarity: ACL_COND_IF / ACL_COND_UNLESS */ |
Willy Tarreau | a91d0a5 | 2013-03-25 08:12:18 +0100 | [diff] [blame] | 229 | unsigned int use; /* or'ed bit mask of all suites's SMP_USE_* */ |
| 230 | 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] | 231 | const char *file; /* config file where the condition is declared */ |
Willy Tarreau | a980263 | 2008-07-25 19:13:19 +0200 | [diff] [blame] | 232 | int line; /* line in the config file where the condition is declared */ |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 233 | }; |
| 234 | |
| 235 | |
| 236 | #endif /* _TYPES_ACL_H */ |
| 237 | |
| 238 | /* |
| 239 | * Local variables: |
| 240 | * c-indent-level: 8 |
| 241 | * c-basic-offset: 8 |
| 242 | * End: |
| 243 | */ |