blob: db63dfe4528961a1b5336440a4423d08ce8d314d [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 Tarreau37406352012-04-23 16:16:37 +020031#include <types/pattern.h>
Willy Tarreaua84d3742007-05-07 00:36:48 +020032#include <types/proxy.h>
Willy Tarreau0b1cd942010-05-16 22:18:27 +020033#include <types/server.h>
Willy Tarreaua84d3742007-05-07 00:36:48 +020034#include <types/session.h>
35
Willy Tarreau438b0f32010-05-10 22:29:06 +020036#include <ebmbtree.h>
Willy Tarreaua84d3742007-05-07 00:36:48 +020037
Willy Tarreau11382812008-07-09 16:18:21 +020038/* 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 Tarreaua84d3742007-05-07 00:36:48 +020058enum {
59 ACL_PAT_FAIL = 0, /* test failed */
Willy Tarreau11382812008-07-09 16:18:21 +020060 ACL_PAT_MISS = 1, /* test may pass with more info */
61 ACL_PAT_PASS = 3, /* test passed */
Willy Tarreaua84d3742007-05-07 00:36:48 +020062};
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 Tarreau11382812008-07-09 16:18:21 +020066 * Those should be interpreted as "IF/UNLESS result == PASS".
Willy Tarreaua84d3742007-05-07 00:36:48 +020067 */
68enum {
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 Tarreaub6866442008-07-14 23:54:42 +020074/* ACLs can be evaluated on requests and on responses, and on partial or complete data */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020075enum {
76 ACL_DIR_REQ = 0, /* ACL evaluated on request */
Willy Tarreaub6866442008-07-14 23:54:42 +020077 ACL_DIR_RTR = (1 << 0), /* ACL evaluated on response */
78 ACL_DIR_MASK = (ACL_DIR_REQ | ACL_DIR_RTR),
79 ACL_PARTIAL = (1 << 1), /* partial data, return MISS if data are missing */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020080};
81
Willy Tarreauc8d7c962007-06-17 08:20:33 +020082/* possible flags for expressions or patterns */
83enum {
84 ACL_PAT_F_IGNORE_CASE = 1 << 0, /* ignore case */
85 ACL_PAT_F_FROM_FILE = 1 << 1, /* pattern comes from a file */
Willy Tarreau438b0f32010-05-10 22:29:06 +020086 ACL_PAT_F_TREE_OK = 1 << 2, /* the pattern parser is allowed to build a tree */
87 ACL_PAT_F_TREE = 1 << 3, /* some patterns are arranged in a tree */
Willy Tarreauc8d7c962007-06-17 08:20:33 +020088};
89
Willy Tarreaua9802632008-07-25 19:13:19 +020090/* what capabilities an ACL uses. These flags are set during parsing, which
91 * allows for flexible ACLs typed by their contents.
92 */
93enum {
Willy Tarreau0ceba5a2008-07-25 19:31:03 +020094 ACL_USE_NOTHING = 0, /* no need for anything beyond internal information */
Willy Tarreaua9802632008-07-25 19:13:19 +020095 ACL_USE_TCP4_PERMANENT = 1 << 0, /* unchanged TCPv4 data (eg: source IP) */
96 ACL_USE_TCP4_CACHEABLE = 1 << 1, /* cacheable TCPv4 data (eg: src conns) */
97 ACL_USE_TCP4_VOLATILE = 1 << 2, /* volatile TCPv4 data (eg: RTT) */
98 ACL_USE_TCP4_ANY = (ACL_USE_TCP4_PERMANENT | ACL_USE_TCP4_CACHEABLE | ACL_USE_TCP4_VOLATILE),
99
100 ACL_USE_TCP6_PERMANENT = 1 << 3, /* unchanged TCPv6 data (eg: source IP) */
101 ACL_USE_TCP6_CACHEABLE = 1 << 4, /* cacheable TCPv6 data (eg: src conns) */
102 ACL_USE_TCP6_VOLATILE = 1 << 5, /* volatile TCPv6 data (eg: RTT) */
103 ACL_USE_TCP6_ANY = (ACL_USE_TCP6_PERMANENT | ACL_USE_TCP6_CACHEABLE | ACL_USE_TCP6_VOLATILE),
104
105 ACL_USE_TCP_PERMANENT = 1 << 6, /* unchanged TCPv4/v6 data (eg: source IP) */
106 ACL_USE_TCP_CACHEABLE = 1 << 7, /* cacheable TCPv4/v6 data (eg: src conns) */
107 ACL_USE_TCP_VOLATILE = 1 << 8, /* volatile TCPv4/v6 data (eg: RTT) */
108 ACL_USE_TCP_ANY = (ACL_USE_TCP_PERMANENT | ACL_USE_TCP_CACHEABLE | ACL_USE_TCP_VOLATILE),
109
Willy Tarreau06457872010-05-23 12:24:38 +0200110 ACL_USE_L6REQ_PERMANENT = 1 << 9, /* unchanged layer6 request data */
111 ACL_USE_L6REQ_CACHEABLE = 1 << 10, /* cacheable layer6 request data (eg: length) */
112 ACL_USE_L6REQ_VOLATILE = 1 << 11, /* volatile layer6 request data (eg: contents) */
113 ACL_USE_L6REQ_ANY = (ACL_USE_L6REQ_PERMANENT | ACL_USE_L6REQ_CACHEABLE | ACL_USE_L6REQ_VOLATILE),
Willy Tarreaua9802632008-07-25 19:13:19 +0200114
Willy Tarreau06457872010-05-23 12:24:38 +0200115 ACL_USE_L6RTR_PERMANENT = 1 << 12, /* unchanged layer6 response data */
116 ACL_USE_L6RTR_CACHEABLE = 1 << 13, /* cacheable layer6 response data (eg: length) */
117 ACL_USE_L6RTR_VOLATILE = 1 << 14, /* volatile layer6 response data (eg: contents) */
118 ACL_USE_L6RTR_ANY = (ACL_USE_L6RTR_PERMANENT | ACL_USE_L6RTR_CACHEABLE | ACL_USE_L6RTR_VOLATILE),
Willy Tarreaua9802632008-07-25 19:13:19 +0200119
120 ACL_USE_L7REQ_PERMANENT = 1 << 15, /* unchanged layer7 request data (eg: method) */
121 ACL_USE_L7REQ_CACHEABLE = 1 << 16, /* cacheable layer7 request data (eg: content-length) */
122 ACL_USE_L7REQ_VOLATILE = 1 << 17, /* volatile layer7 request data (eg: cookie) */
123 ACL_USE_L7REQ_ANY = (ACL_USE_L7REQ_PERMANENT | ACL_USE_L7REQ_CACHEABLE | ACL_USE_L7REQ_VOLATILE),
124
125 ACL_USE_L7RTR_PERMANENT = 1 << 18, /* unchanged layer7 response data (eg: status) */
126 ACL_USE_L7RTR_CACHEABLE = 1 << 19, /* cacheable layer7 response data (eg: content-length) */
127 ACL_USE_L7RTR_VOLATILE = 1 << 20, /* volatile layer7 response data (eg: cookie) */
128 ACL_USE_L7RTR_ANY = (ACL_USE_L7RTR_PERMANENT | ACL_USE_L7RTR_CACHEABLE | ACL_USE_L7RTR_VOLATILE),
129
130 /* those ones are used for ambiguous "hdr_xxx" verbs */
131 ACL_USE_HDR_CACHEABLE = 1 << 21, /* cacheable request or response header (eg: content-length) */
132 ACL_USE_HDR_VOLATILE = 1 << 22, /* volatile request or response header (eg: cookie) */
133 ACL_USE_HDR_ANY = (ACL_USE_HDR_CACHEABLE | ACL_USE_HDR_VOLATILE),
134
Willy Tarreau930bd6b2011-02-23 15:17:24 +0100135 /* This one indicates that we need an internal parameter known in the response only */
136 ACL_USE_RTR_INTERNAL = 1 << 23, /* volatile response information */
137
Willy Tarreaua9802632008-07-25 19:13:19 +0200138 /* information which remains during response */
139 ACL_USE_REQ_PERMANENT = (ACL_USE_TCP4_PERMANENT | ACL_USE_TCP6_PERMANENT | ACL_USE_TCP_PERMANENT |
Willy Tarreau06457872010-05-23 12:24:38 +0200140 ACL_USE_L6REQ_PERMANENT | ACL_USE_L7REQ_PERMANENT),
Willy Tarreaua9802632008-07-25 19:13:19 +0200141 ACL_USE_REQ_CACHEABLE = (ACL_USE_TCP4_CACHEABLE | ACL_USE_TCP6_CACHEABLE | ACL_USE_TCP_CACHEABLE |
Willy Tarreau06457872010-05-23 12:24:38 +0200142 ACL_USE_L6REQ_CACHEABLE | ACL_USE_L7REQ_CACHEABLE | ACL_USE_HDR_CACHEABLE),
Willy Tarreaua9802632008-07-25 19:13:19 +0200143
144 /* information which does not remain during response */
145 ACL_USE_REQ_VOLATILE = (ACL_USE_TCP4_VOLATILE | ACL_USE_TCP6_VOLATILE | ACL_USE_TCP_VOLATILE |
Willy Tarreau06457872010-05-23 12:24:38 +0200146 ACL_USE_L6REQ_VOLATILE | ACL_USE_L7REQ_VOLATILE),
Willy Tarreaua9802632008-07-25 19:13:19 +0200147
Willy Tarreau06457872010-05-23 12:24:38 +0200148 /* any type of layer 6 contents information (random data available in a buffer) */
149 ACL_USE_L6_ANY = (ACL_USE_L6REQ_ANY | ACL_USE_L6RTR_ANY),
Willy Tarreaua9802632008-07-25 19:13:19 +0200150
151 /* any type of layer 7 information */
152 ACL_USE_L7_ANY = (ACL_USE_L7REQ_ANY | ACL_USE_L7RTR_ANY | ACL_USE_HDR_ANY),
153
154 /* any type of response information */
Willy Tarreau930bd6b2011-02-23 15:17:24 +0100155 ACL_USE_RTR_ANY = (ACL_USE_L6RTR_ANY | ACL_USE_L7RTR_ANY | ACL_USE_RTR_INTERNAL),
Willy Tarreau438b0f32010-05-10 22:29:06 +0200156
157 /* some flags indicating if a keyword supports exact pattern matching,
158 * so that patterns may be arranged in lookup trees. Let's put those
159 * flags at the end to leave some space for the other ones above.
160 */
161 ACL_MAY_LOOKUP = 1 << 31, /* exact pattern lookup */
Willy Tarreaua9802632008-07-25 19:13:19 +0200162};
163
164/* filtering hooks */
165enum {
166 /* hooks on the request path */
167 ACL_HOOK_REQ_FE_TCP = 0,
168 ACL_HOOK_REQ_FE_TCP_CONTENT,
169 ACL_HOOK_REQ_FE_HTTP_IN,
170 ACL_HOOK_REQ_FE_SWITCH,
171 ACL_HOOK_REQ_BE_TCP_CONTENT,
172 ACL_HOOK_REQ_BE_HTTP_IN,
173 ACL_HOOK_REQ_BE_SWITCH,
174 ACL_HOOK_REQ_FE_HTTP_OUT,
175 ACL_HOOK_REQ_BE_HTTP_OUT,
176 /* hooks on the response path */
177 ACL_HOOK_RTR_BE_TCP_CONTENT,
178 ACL_HOOK_RTR_BE_HTTP_IN,
179 ACL_HOOK_RTR_FE_TCP_CONTENT,
180 ACL_HOOK_RTR_FE_HTTP_IN,
181 ACL_HOOK_RTR_BE_HTTP_OUT,
182 ACL_HOOK_RTR_FE_HTTP_OUT,
183};
184
Willy Tarreaua84d3742007-05-07 00:36:48 +0200185/* How to store a time range and the valid days in 29 bits */
186struct acl_time {
187 int dow:7; /* 1 bit per day of week: 0-6 */
188 int h1:5, m1:6; /* 0..24:0..60. Use 0:0 for all day. */
189 int h2:5, m2:6; /* 0..24:0..60. Use 24:0 for all day. */
190};
191
192/* The acl will be linked to from the proxy where it is declared */
193struct acl_pattern {
194 struct list list; /* chaining */
195 union {
196 int i; /* integer value */
Willy Tarreauae8b7962007-06-09 23:10:04 +0200197 struct {
198 signed long long min, max;
199 int min_set :1;
200 int max_set :1;
201 } range; /* integer range */
Willy Tarreaua67fad92007-05-08 19:50:09 +0200202 struct {
203 struct in_addr addr;
204 struct in_addr mask;
205 } ipv4; /* IPv4 address */
Willy Tarreaua84d3742007-05-07 00:36:48 +0200206 struct acl_time time; /* valid hours and days */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +0100207 unsigned int group_mask;
Willy Tarreaue56cda92010-05-11 23:25:05 +0200208 struct eb_root *tree; /* tree storing all values if any */
Willy Tarreaua84d3742007-05-07 00:36:48 +0200209 } val; /* direct value */
210 union {
211 void *ptr; /* any data */
212 char *str; /* any string */
213 regex_t *reg; /* a compiled regex */
214 } ptr; /* indirect values, allocated */
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +0200215 void(*freeptrbuf)(void *ptr); /* a destructor able to free objects from the ptr */
Willy Tarreaua84d3742007-05-07 00:36:48 +0200216 int len; /* data length when required */
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200217 int flags; /* expr or pattern flags. */
Willy Tarreaua84d3742007-05-07 00:36:48 +0200218};
219
Willy Tarreaua84d3742007-05-07 00:36:48 +0200220/*
221 * ACL keyword: Associates keywords with parsers, methods to retrieve the value and testers.
222 */
223
224/* some dummy declarations to silent the compiler */
225struct proxy;
226struct session;
227
Willy Tarreauae8b7962007-06-09 23:10:04 +0200228/*
229 * NOTE:
230 * The 'parse' function is called to parse words in the configuration. It must
231 * return the number of valid words read. 0 = error. The 'opaque' argument may
232 * be used by functions which need to maintain a context between consecutive
233 * values. It is initialized to zero before the first call, and passed along
234 * successive calls.
235 */
236
Willy Tarreau97be1452007-06-10 11:47:14 +0200237struct acl_expr;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200238struct acl_keyword {
239 const char *kw;
Willy Tarreauae8b7962007-06-09 23:10:04 +0200240 int (*parse)(const char **text, struct acl_pattern *pattern, int *opaque);
Willy Tarreau97be1452007-06-10 11:47:14 +0200241 int (*fetch)(struct proxy *px, struct session *l4, void *l7, int dir,
Willy Tarreau37406352012-04-23 16:16:37 +0200242 struct acl_expr *expr, struct sample *smp);
243 int (*match)(struct sample *smp, struct acl_pattern *pattern);
Willy Tarreaua9802632008-07-25 19:13:19 +0200244 unsigned int requires; /* bit mask of all ACL_USE_* required to evaluate this keyword */
Willy Tarreau61612d42012-04-19 18:42:05 +0200245 int arg_mask; /* mask describing up to 7 arg types */
246 /* must be after the config params */
Willy Tarreaua84d3742007-05-07 00:36:48 +0200247 int use_cnt;
248};
249
250/*
251 * A keyword list. It is a NULL-terminated array of keywords. It embeds a
252 * struct list in order to be linked to other lists, allowing it to easily
253 * be declared where it is needed, and linked without duplicating data nor
254 * allocating memory.
255 */
256struct acl_kw_list {
257 struct list list;
258 struct acl_keyword kw[VAR_ARRAY];
259};
260
261/*
262 * Description of an ACL expression.
263 * It contains a subject and a set of patterns to test against it.
264 * - the function get() is called to retrieve the subject from the
265 * current session or transaction and build a test.
266 * - the function test() is called to evaluate the test based on the
267 * available patterns and return ACL_PAT_*
268 * Both of those functions are available through the keyword.
269 */
270struct acl_expr {
271 struct list list; /* chaining */
272 struct acl_keyword *kw; /* back-reference to the keyword */
Willy Tarreau34db1082012-04-19 17:16:54 +0200273 struct arg *args; /* optional argument list (eg: header or cookie name) */
Willy Tarreaua84d3742007-05-07 00:36:48 +0200274 struct list patterns; /* list of acl_patterns */
Willy Tarreau438b0f32010-05-10 22:29:06 +0200275 struct eb_root pattern_tree; /* may be used for lookup in large datasets */
Willy Tarreaua84d3742007-05-07 00:36:48 +0200276};
277
278struct acl {
279 struct list list; /* chaining */
280 char *name; /* acl name */
281 struct list expr; /* list of acl_exprs */
282 int cache_idx; /* ACL index in cache */
Willy Tarreaua9802632008-07-25 19:13:19 +0200283 unsigned int requires; /* or'ed bit mask of all acl_expr's ACL_USE_* */
Willy Tarreaua84d3742007-05-07 00:36:48 +0200284};
285
286/* the condition will be linked to from an action in a proxy */
287struct acl_term {
288 struct list list; /* chaining */
289 struct acl *acl; /* acl pointed to by this term */
290 int neg; /* 1 if the ACL result must be negated */
291};
292
293struct acl_term_suite {
294 struct list list; /* chaining of term suites */
295 struct list terms; /* list of acl_terms */
296};
297
298struct acl_cond {
299 struct list list; /* Some specific tests may use multiple conditions */
300 struct list suites; /* list of acl_term_suites */
301 int pol; /* polarity: ACL_COND_IF / ACL_COND_UNLESS */
Willy Tarreaua9802632008-07-25 19:13:19 +0200302 unsigned int requires; /* or'ed bit mask of all acl's ACL_USE_* */
Willy Tarreau88922352009-10-04 22:02:50 +0200303 const char *file; /* config file where the condition is declared */
Willy Tarreaua9802632008-07-25 19:13:19 +0200304 int line; /* line in the config file where the condition is declared */
Willy Tarreaua84d3742007-05-07 00:36:48 +0200305};
306
307
308#endif /* _TYPES_ACL_H */
309
310/*
311 * Local variables:
312 * c-indent-level: 8
313 * c-basic-offset: 8
314 * End:
315 */