blob: 748180c9e4705bec2a785785933001d97e8c8842 [file] [log] [blame]
Willy Tarreaua84d3742007-05-07 00:36:48 +02001/*
Willy Tarreau88922352009-10-04 22:02:50 +02002 * include/types/acl.h
3 * This file provides structures and types for ACLs.
4 *
Willy Tarreau34db1082012-04-19 17:16:54 +02005 * Copyright (C) 2000-2012 Willy Tarreau - w@1wt.eu
Willy Tarreau88922352009-10-04 22:02:50 +02006 *
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 Tarreaua84d3742007-05-07 00:36:48 +020021
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 Tarreau34db1082012-04-19 17:16:54 +020029#include <types/arg.h>
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010030#include <types/auth.h>
Willy Tarreaua84d3742007-05-07 00:36:48 +020031#include <types/proxy.h>
Willy Tarreau0b1cd942010-05-16 22:18:27 +020032#include <types/server.h>
Willy Tarreaua84d3742007-05-07 00:36:48 +020033#include <types/session.h>
34
Willy Tarreau438b0f32010-05-10 22:29:06 +020035#include <ebmbtree.h>
Willy Tarreaua84d3742007-05-07 00:36:48 +020036
Willy Tarreau11382812008-07-09 16:18:21 +020037/* Pattern matching function result.
38 *
39 * We're using a 3-state matching system :
40 * - PASS : at least one pattern already matches
41 * - MISS : some data is missing to decide if some rules may finally match.
42 * - FAIL : no mattern may ever match
43 *
44 * We assign values 0, 1 and 3 to FAIL, MISS and PASS respectively, so that we
45 * can make use of standard arithmetics for the truth tables below :
46 *
47 * x | !x x&y | F(0) | M(1) | P(3) x|y | F(0) | M(1) | P(3)
48 * ------+----- -----+------+------+----- -----+------+------+-----
49 * F(0) | P(3) F(0)| F(0) | F(0) | F(0) F(0)| F(0) | M(1) | P(3)
50 * M(1) | M(1) M(1)| F(0) | M(1) | M(1) M(1)| M(1) | M(1) | P(3)
51 * P(3) | F(0) P(3)| F(0) | M(1) | P(3) P(3)| P(3) | P(3) | P(3)
52 *
53 * neg(x) = (3 >> x) and(x,y) = (x & y) or(x,y) = (x | y)
54 *
55 */
56
Willy Tarreaua84d3742007-05-07 00:36:48 +020057enum {
58 ACL_PAT_FAIL = 0, /* test failed */
Willy Tarreau11382812008-07-09 16:18:21 +020059 ACL_PAT_MISS = 1, /* test may pass with more info */
60 ACL_PAT_PASS = 3, /* test passed */
Willy Tarreaua84d3742007-05-07 00:36:48 +020061};
62
63/* Condition polarity. It makes it easier for any option to choose between
64 * IF/UNLESS if it can store that information within the condition itself.
Willy Tarreau11382812008-07-09 16:18:21 +020065 * Those should be interpreted as "IF/UNLESS result == PASS".
Willy Tarreaua84d3742007-05-07 00:36:48 +020066 */
67enum {
68 ACL_COND_NONE, /* no polarity set yet */
69 ACL_COND_IF, /* positive condition (after 'if') */
70 ACL_COND_UNLESS, /* negative condition (after 'unless') */
71};
72
73/* possible flags for intermediate test values. The flags are maintained
74 * across consecutive fetches for a same entry (eg: parse all req lines).
75 */
76enum {
77 ACL_TEST_F_READ_ONLY = 1 << 0, /* test data are read-only */
78 ACL_TEST_F_MUST_FREE = 1 << 1, /* test data must be freed after end of evaluation */
79 ACL_TEST_F_VOL_TEST = 1 << 2, /* result must not survive longer than the test (eg: time) */
80 ACL_TEST_F_VOL_HDR = 1 << 3, /* result sensitive to changes in headers */
81 ACL_TEST_F_VOL_1ST = 1 << 4, /* result sensitive to changes in first line (eg: URI) */
82 ACL_TEST_F_VOL_TXN = 1 << 5, /* result sensitive to new transaction (eg: persist) */
83 ACL_TEST_F_VOL_SESS = 1 << 6, /* result sensitive to new session (eg: IP) */
84 ACL_TEST_F_VOLATILE = (1<<2)|(1<<3)|(1<<4)|(1<<5)|(1<<6),
Willy Tarreaub6866442008-07-14 23:54:42 +020085 ACL_TEST_F_FETCH_MORE = 1 << 7, /* if test does not match, retry with next entry (for multi-match) */
86 ACL_TEST_F_MAY_CHANGE = 1 << 8, /* if test does not match, retry later (eg: request size) */
Willy Tarreaua79534f2008-07-20 10:13:37 +020087 ACL_TEST_F_RES_SET = 1 << 9, /* for fetch() function to assign the result without calling match() */
88 ACL_TEST_F_RES_PASS = 1 << 10,/* with SET_RESULT, sets result to PASS (defaults to FAIL) */
89 ACL_TEST_F_SET_RES_PASS = (ACL_TEST_F_RES_SET|ACL_TEST_F_RES_PASS), /* sets result to PASS */
90 ACL_TEST_F_SET_RES_FAIL = (ACL_TEST_F_RES_SET), /* sets result to FAIL */
Krzysztof Piotr Oledzkid7528e52010-01-29 17:55:53 +010091 ACL_TEST_F_NULL_MATCH = 1 << 11,/* call expr->kw->match with NULL pattern if expr->patterns is empty */
Willy Tarreaua84d3742007-05-07 00:36:48 +020092};
93
Willy Tarreaub6866442008-07-14 23:54:42 +020094/* ACLs can be evaluated on requests and on responses, and on partial or complete data */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020095enum {
96 ACL_DIR_REQ = 0, /* ACL evaluated on request */
Willy Tarreaub6866442008-07-14 23:54:42 +020097 ACL_DIR_RTR = (1 << 0), /* ACL evaluated on response */
98 ACL_DIR_MASK = (ACL_DIR_REQ | ACL_DIR_RTR),
99 ACL_PARTIAL = (1 << 1), /* partial data, return MISS if data are missing */
Willy Tarreaud41f8d82007-06-10 10:06:18 +0200100};
101
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200102/* possible flags for expressions or patterns */
103enum {
104 ACL_PAT_F_IGNORE_CASE = 1 << 0, /* ignore case */
105 ACL_PAT_F_FROM_FILE = 1 << 1, /* pattern comes from a file */
Willy Tarreau438b0f32010-05-10 22:29:06 +0200106 ACL_PAT_F_TREE_OK = 1 << 2, /* the pattern parser is allowed to build a tree */
107 ACL_PAT_F_TREE = 1 << 3, /* some patterns are arranged in a tree */
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200108};
109
Willy Tarreaua9802632008-07-25 19:13:19 +0200110/* what capabilities an ACL uses. These flags are set during parsing, which
111 * allows for flexible ACLs typed by their contents.
112 */
113enum {
Willy Tarreau0ceba5a2008-07-25 19:31:03 +0200114 ACL_USE_NOTHING = 0, /* no need for anything beyond internal information */
Willy Tarreaua9802632008-07-25 19:13:19 +0200115 ACL_USE_TCP4_PERMANENT = 1 << 0, /* unchanged TCPv4 data (eg: source IP) */
116 ACL_USE_TCP4_CACHEABLE = 1 << 1, /* cacheable TCPv4 data (eg: src conns) */
117 ACL_USE_TCP4_VOLATILE = 1 << 2, /* volatile TCPv4 data (eg: RTT) */
118 ACL_USE_TCP4_ANY = (ACL_USE_TCP4_PERMANENT | ACL_USE_TCP4_CACHEABLE | ACL_USE_TCP4_VOLATILE),
119
120 ACL_USE_TCP6_PERMANENT = 1 << 3, /* unchanged TCPv6 data (eg: source IP) */
121 ACL_USE_TCP6_CACHEABLE = 1 << 4, /* cacheable TCPv6 data (eg: src conns) */
122 ACL_USE_TCP6_VOLATILE = 1 << 5, /* volatile TCPv6 data (eg: RTT) */
123 ACL_USE_TCP6_ANY = (ACL_USE_TCP6_PERMANENT | ACL_USE_TCP6_CACHEABLE | ACL_USE_TCP6_VOLATILE),
124
125 ACL_USE_TCP_PERMANENT = 1 << 6, /* unchanged TCPv4/v6 data (eg: source IP) */
126 ACL_USE_TCP_CACHEABLE = 1 << 7, /* cacheable TCPv4/v6 data (eg: src conns) */
127 ACL_USE_TCP_VOLATILE = 1 << 8, /* volatile TCPv4/v6 data (eg: RTT) */
128 ACL_USE_TCP_ANY = (ACL_USE_TCP_PERMANENT | ACL_USE_TCP_CACHEABLE | ACL_USE_TCP_VOLATILE),
129
Willy Tarreau06457872010-05-23 12:24:38 +0200130 ACL_USE_L6REQ_PERMANENT = 1 << 9, /* unchanged layer6 request data */
131 ACL_USE_L6REQ_CACHEABLE = 1 << 10, /* cacheable layer6 request data (eg: length) */
132 ACL_USE_L6REQ_VOLATILE = 1 << 11, /* volatile layer6 request data (eg: contents) */
133 ACL_USE_L6REQ_ANY = (ACL_USE_L6REQ_PERMANENT | ACL_USE_L6REQ_CACHEABLE | ACL_USE_L6REQ_VOLATILE),
Willy Tarreaua9802632008-07-25 19:13:19 +0200134
Willy Tarreau06457872010-05-23 12:24:38 +0200135 ACL_USE_L6RTR_PERMANENT = 1 << 12, /* unchanged layer6 response data */
136 ACL_USE_L6RTR_CACHEABLE = 1 << 13, /* cacheable layer6 response data (eg: length) */
137 ACL_USE_L6RTR_VOLATILE = 1 << 14, /* volatile layer6 response data (eg: contents) */
138 ACL_USE_L6RTR_ANY = (ACL_USE_L6RTR_PERMANENT | ACL_USE_L6RTR_CACHEABLE | ACL_USE_L6RTR_VOLATILE),
Willy Tarreaua9802632008-07-25 19:13:19 +0200139
140 ACL_USE_L7REQ_PERMANENT = 1 << 15, /* unchanged layer7 request data (eg: method) */
141 ACL_USE_L7REQ_CACHEABLE = 1 << 16, /* cacheable layer7 request data (eg: content-length) */
142 ACL_USE_L7REQ_VOLATILE = 1 << 17, /* volatile layer7 request data (eg: cookie) */
143 ACL_USE_L7REQ_ANY = (ACL_USE_L7REQ_PERMANENT | ACL_USE_L7REQ_CACHEABLE | ACL_USE_L7REQ_VOLATILE),
144
145 ACL_USE_L7RTR_PERMANENT = 1 << 18, /* unchanged layer7 response data (eg: status) */
146 ACL_USE_L7RTR_CACHEABLE = 1 << 19, /* cacheable layer7 response data (eg: content-length) */
147 ACL_USE_L7RTR_VOLATILE = 1 << 20, /* volatile layer7 response data (eg: cookie) */
148 ACL_USE_L7RTR_ANY = (ACL_USE_L7RTR_PERMANENT | ACL_USE_L7RTR_CACHEABLE | ACL_USE_L7RTR_VOLATILE),
149
150 /* those ones are used for ambiguous "hdr_xxx" verbs */
151 ACL_USE_HDR_CACHEABLE = 1 << 21, /* cacheable request or response header (eg: content-length) */
152 ACL_USE_HDR_VOLATILE = 1 << 22, /* volatile request or response header (eg: cookie) */
153 ACL_USE_HDR_ANY = (ACL_USE_HDR_CACHEABLE | ACL_USE_HDR_VOLATILE),
154
Willy Tarreau930bd6b2011-02-23 15:17:24 +0100155 /* This one indicates that we need an internal parameter known in the response only */
156 ACL_USE_RTR_INTERNAL = 1 << 23, /* volatile response information */
157
Willy Tarreaua9802632008-07-25 19:13:19 +0200158 /* information which remains during response */
159 ACL_USE_REQ_PERMANENT = (ACL_USE_TCP4_PERMANENT | ACL_USE_TCP6_PERMANENT | ACL_USE_TCP_PERMANENT |
Willy Tarreau06457872010-05-23 12:24:38 +0200160 ACL_USE_L6REQ_PERMANENT | ACL_USE_L7REQ_PERMANENT),
Willy Tarreaua9802632008-07-25 19:13:19 +0200161 ACL_USE_REQ_CACHEABLE = (ACL_USE_TCP4_CACHEABLE | ACL_USE_TCP6_CACHEABLE | ACL_USE_TCP_CACHEABLE |
Willy Tarreau06457872010-05-23 12:24:38 +0200162 ACL_USE_L6REQ_CACHEABLE | ACL_USE_L7REQ_CACHEABLE | ACL_USE_HDR_CACHEABLE),
Willy Tarreaua9802632008-07-25 19:13:19 +0200163
164 /* information which does not remain during response */
165 ACL_USE_REQ_VOLATILE = (ACL_USE_TCP4_VOLATILE | ACL_USE_TCP6_VOLATILE | ACL_USE_TCP_VOLATILE |
Willy Tarreau06457872010-05-23 12:24:38 +0200166 ACL_USE_L6REQ_VOLATILE | ACL_USE_L7REQ_VOLATILE),
Willy Tarreaua9802632008-07-25 19:13:19 +0200167
Willy Tarreau06457872010-05-23 12:24:38 +0200168 /* any type of layer 6 contents information (random data available in a buffer) */
169 ACL_USE_L6_ANY = (ACL_USE_L6REQ_ANY | ACL_USE_L6RTR_ANY),
Willy Tarreaua9802632008-07-25 19:13:19 +0200170
171 /* any type of layer 7 information */
172 ACL_USE_L7_ANY = (ACL_USE_L7REQ_ANY | ACL_USE_L7RTR_ANY | ACL_USE_HDR_ANY),
173
174 /* any type of response information */
Willy Tarreau930bd6b2011-02-23 15:17:24 +0100175 ACL_USE_RTR_ANY = (ACL_USE_L6RTR_ANY | ACL_USE_L7RTR_ANY | ACL_USE_RTR_INTERNAL),
Willy Tarreau438b0f32010-05-10 22:29:06 +0200176
177 /* some flags indicating if a keyword supports exact pattern matching,
178 * so that patterns may be arranged in lookup trees. Let's put those
179 * flags at the end to leave some space for the other ones above.
180 */
181 ACL_MAY_LOOKUP = 1 << 31, /* exact pattern lookup */
Willy Tarreaua9802632008-07-25 19:13:19 +0200182};
183
184/* filtering hooks */
185enum {
186 /* hooks on the request path */
187 ACL_HOOK_REQ_FE_TCP = 0,
188 ACL_HOOK_REQ_FE_TCP_CONTENT,
189 ACL_HOOK_REQ_FE_HTTP_IN,
190 ACL_HOOK_REQ_FE_SWITCH,
191 ACL_HOOK_REQ_BE_TCP_CONTENT,
192 ACL_HOOK_REQ_BE_HTTP_IN,
193 ACL_HOOK_REQ_BE_SWITCH,
194 ACL_HOOK_REQ_FE_HTTP_OUT,
195 ACL_HOOK_REQ_BE_HTTP_OUT,
196 /* hooks on the response path */
197 ACL_HOOK_RTR_BE_TCP_CONTENT,
198 ACL_HOOK_RTR_BE_HTTP_IN,
199 ACL_HOOK_RTR_FE_TCP_CONTENT,
200 ACL_HOOK_RTR_FE_HTTP_IN,
201 ACL_HOOK_RTR_BE_HTTP_OUT,
202 ACL_HOOK_RTR_FE_HTTP_OUT,
203};
204
Willy Tarreaua84d3742007-05-07 00:36:48 +0200205/* How to store a time range and the valid days in 29 bits */
206struct acl_time {
207 int dow:7; /* 1 bit per day of week: 0-6 */
208 int h1:5, m1:6; /* 0..24:0..60. Use 0:0 for all day. */
209 int h2:5, m2:6; /* 0..24:0..60. Use 24:0 for all day. */
210};
211
212/* The acl will be linked to from the proxy where it is declared */
213struct acl_pattern {
214 struct list list; /* chaining */
215 union {
216 int i; /* integer value */
Willy Tarreauae8b7962007-06-09 23:10:04 +0200217 struct {
218 signed long long min, max;
219 int min_set :1;
220 int max_set :1;
221 } range; /* integer range */
Willy Tarreaua67fad92007-05-08 19:50:09 +0200222 struct {
223 struct in_addr addr;
224 struct in_addr mask;
225 } ipv4; /* IPv4 address */
Willy Tarreaua84d3742007-05-07 00:36:48 +0200226 struct acl_time time; /* valid hours and days */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +0100227 unsigned int group_mask;
Willy Tarreaue56cda92010-05-11 23:25:05 +0200228 struct eb_root *tree; /* tree storing all values if any */
Willy Tarreaua84d3742007-05-07 00:36:48 +0200229 } val; /* direct value */
230 union {
231 void *ptr; /* any data */
232 char *str; /* any string */
233 regex_t *reg; /* a compiled regex */
234 } ptr; /* indirect values, allocated */
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +0200235 void(*freeptrbuf)(void *ptr); /* a destructor able to free objects from the ptr */
Willy Tarreaua84d3742007-05-07 00:36:48 +0200236 int len; /* data length when required */
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200237 int flags; /* expr or pattern flags. */
Willy Tarreaua84d3742007-05-07 00:36:48 +0200238};
239
240/* The structure exchanged between an acl_fetch_* function responsible for
241 * retrieving a value, and an acl_match_* function responsible for testing it.
242 */
243struct acl_test {
Willy Tarreau33a7e692007-06-10 19:45:56 +0200244 int flags; /* ACL_TEST_F_* set to 0 on first call */
245 union { /* fetch_* functions context for any purpose */
246 void *p; /* any pointer */
247 int i; /* any integer */
248 long long ll; /* any long long or smaller */
249 double d; /* any float or double */
250 void *a[8]; /* any array of up to 8 pointers */
Willy Tarreaua84d3742007-05-07 00:36:48 +0200251 } ctx;
252};
253
254
255/*
256 * ACL keyword: Associates keywords with parsers, methods to retrieve the value and testers.
257 */
258
259/* some dummy declarations to silent the compiler */
260struct proxy;
261struct session;
262
Willy Tarreauae8b7962007-06-09 23:10:04 +0200263/*
264 * NOTE:
265 * The 'parse' function is called to parse words in the configuration. It must
266 * return the number of valid words read. 0 = error. The 'opaque' argument may
267 * be used by functions which need to maintain a context between consecutive
268 * values. It is initialized to zero before the first call, and passed along
269 * successive calls.
270 */
271
Willy Tarreau97be1452007-06-10 11:47:14 +0200272struct acl_expr;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200273struct acl_keyword {
274 const char *kw;
Willy Tarreauae8b7962007-06-09 23:10:04 +0200275 int (*parse)(const char **text, struct acl_pattern *pattern, int *opaque);
Willy Tarreau97be1452007-06-10 11:47:14 +0200276 int (*fetch)(struct proxy *px, struct session *l4, void *l7, int dir,
277 struct acl_expr *expr, struct acl_test *test);
Willy Tarreaua84d3742007-05-07 00:36:48 +0200278 int (*match)(struct acl_test *test, struct acl_pattern *pattern);
Willy Tarreaua9802632008-07-25 19:13:19 +0200279 unsigned int requires; /* bit mask of all ACL_USE_* required to evaluate this keyword */
Willy Tarreau61612d42012-04-19 18:42:05 +0200280 int arg_mask; /* mask describing up to 7 arg types */
281 /* must be after the config params */
Willy Tarreaua84d3742007-05-07 00:36:48 +0200282 int use_cnt;
283};
284
285/*
286 * A keyword list. It is a NULL-terminated array of keywords. It embeds a
287 * struct list in order to be linked to other lists, allowing it to easily
288 * be declared where it is needed, and linked without duplicating data nor
289 * allocating memory.
290 */
291struct acl_kw_list {
292 struct list list;
293 struct acl_keyword kw[VAR_ARRAY];
294};
295
296/*
297 * Description of an ACL expression.
298 * It contains a subject and a set of patterns to test against it.
299 * - the function get() is called to retrieve the subject from the
300 * current session or transaction and build a test.
301 * - the function test() is called to evaluate the test based on the
302 * available patterns and return ACL_PAT_*
303 * Both of those functions are available through the keyword.
304 */
305struct acl_expr {
306 struct list list; /* chaining */
307 struct acl_keyword *kw; /* back-reference to the keyword */
Willy Tarreau34db1082012-04-19 17:16:54 +0200308 struct arg *args; /* optional argument list (eg: header or cookie name) */
Willy Tarreaua84d3742007-05-07 00:36:48 +0200309 struct list patterns; /* list of acl_patterns */
Willy Tarreau438b0f32010-05-10 22:29:06 +0200310 struct eb_root pattern_tree; /* may be used for lookup in large datasets */
Willy Tarreaua84d3742007-05-07 00:36:48 +0200311};
312
313struct acl {
314 struct list list; /* chaining */
315 char *name; /* acl name */
316 struct list expr; /* list of acl_exprs */
317 int cache_idx; /* ACL index in cache */
Willy Tarreaua9802632008-07-25 19:13:19 +0200318 unsigned int requires; /* or'ed bit mask of all acl_expr's ACL_USE_* */
Willy Tarreaua84d3742007-05-07 00:36:48 +0200319};
320
321/* the condition will be linked to from an action in a proxy */
322struct acl_term {
323 struct list list; /* chaining */
324 struct acl *acl; /* acl pointed to by this term */
325 int neg; /* 1 if the ACL result must be negated */
326};
327
328struct acl_term_suite {
329 struct list list; /* chaining of term suites */
330 struct list terms; /* list of acl_terms */
331};
332
333struct acl_cond {
334 struct list list; /* Some specific tests may use multiple conditions */
335 struct list suites; /* list of acl_term_suites */
336 int pol; /* polarity: ACL_COND_IF / ACL_COND_UNLESS */
Willy Tarreaua9802632008-07-25 19:13:19 +0200337 unsigned int requires; /* or'ed bit mask of all acl's ACL_USE_* */
Willy Tarreau88922352009-10-04 22:02:50 +0200338 const char *file; /* config file where the condition is declared */
Willy Tarreaua9802632008-07-25 19:13:19 +0200339 int line; /* line in the config file where the condition is declared */
Willy Tarreaua84d3742007-05-07 00:36:48 +0200340};
341
342
343#endif /* _TYPES_ACL_H */
344
345/*
346 * Local variables:
347 * c-indent-level: 8
348 * c-basic-offset: 8
349 * End:
350 */