blob: 51acfe743edd714f98eedd68f4a74b8338b5e612 [file] [log] [blame]
Willy Tarreaua84d3742007-05-07 00:36:48 +02001/*
2 include/types/acl.h
3 This file provides structures and types for ACLs.
4
Willy Tarreau11382812008-07-09 16:18:21 +02005 Copyright (C) 2000-2008 Willy Tarreau - w@1wt.eu
Willy Tarreaua84d3742007-05-07 00:36:48 +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*/
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
29#include <types/proxy.h>
30#include <types/session.h>
31
32
Willy Tarreau11382812008-07-09 16:18:21 +020033/* Pattern matching function result.
34 *
35 * We're using a 3-state matching system :
36 * - PASS : at least one pattern already matches
37 * - MISS : some data is missing to decide if some rules may finally match.
38 * - FAIL : no mattern may ever match
39 *
40 * We assign values 0, 1 and 3 to FAIL, MISS and PASS respectively, so that we
41 * can make use of standard arithmetics for the truth tables below :
42 *
43 * x | !x x&y | F(0) | M(1) | P(3) x|y | F(0) | M(1) | P(3)
44 * ------+----- -----+------+------+----- -----+------+------+-----
45 * F(0) | P(3) F(0)| F(0) | F(0) | F(0) F(0)| F(0) | M(1) | P(3)
46 * M(1) | M(1) M(1)| F(0) | M(1) | M(1) M(1)| M(1) | M(1) | P(3)
47 * P(3) | F(0) P(3)| F(0) | M(1) | P(3) P(3)| P(3) | P(3) | P(3)
48 *
49 * neg(x) = (3 >> x) and(x,y) = (x & y) or(x,y) = (x | y)
50 *
51 */
52
Willy Tarreaua84d3742007-05-07 00:36:48 +020053enum {
54 ACL_PAT_FAIL = 0, /* test failed */
Willy Tarreau11382812008-07-09 16:18:21 +020055 ACL_PAT_MISS = 1, /* test may pass with more info */
56 ACL_PAT_PASS = 3, /* test passed */
Willy Tarreaua84d3742007-05-07 00:36:48 +020057};
58
59/* Condition polarity. It makes it easier for any option to choose between
60 * IF/UNLESS if it can store that information within the condition itself.
Willy Tarreau11382812008-07-09 16:18:21 +020061 * Those should be interpreted as "IF/UNLESS result == PASS".
Willy Tarreaua84d3742007-05-07 00:36:48 +020062 */
63enum {
64 ACL_COND_NONE, /* no polarity set yet */
65 ACL_COND_IF, /* positive condition (after 'if') */
66 ACL_COND_UNLESS, /* negative condition (after 'unless') */
67};
68
69/* possible flags for intermediate test values. The flags are maintained
70 * across consecutive fetches for a same entry (eg: parse all req lines).
71 */
72enum {
73 ACL_TEST_F_READ_ONLY = 1 << 0, /* test data are read-only */
74 ACL_TEST_F_MUST_FREE = 1 << 1, /* test data must be freed after end of evaluation */
75 ACL_TEST_F_VOL_TEST = 1 << 2, /* result must not survive longer than the test (eg: time) */
76 ACL_TEST_F_VOL_HDR = 1 << 3, /* result sensitive to changes in headers */
77 ACL_TEST_F_VOL_1ST = 1 << 4, /* result sensitive to changes in first line (eg: URI) */
78 ACL_TEST_F_VOL_TXN = 1 << 5, /* result sensitive to new transaction (eg: persist) */
79 ACL_TEST_F_VOL_SESS = 1 << 6, /* result sensitive to new session (eg: IP) */
80 ACL_TEST_F_VOLATILE = (1<<2)|(1<<3)|(1<<4)|(1<<5)|(1<<6),
Willy Tarreaub6866442008-07-14 23:54:42 +020081 ACL_TEST_F_FETCH_MORE = 1 << 7, /* if test does not match, retry with next entry (for multi-match) */
82 ACL_TEST_F_MAY_CHANGE = 1 << 8, /* if test does not match, retry later (eg: request size) */
Willy Tarreaua84d3742007-05-07 00:36:48 +020083};
84
Willy Tarreaub6866442008-07-14 23:54:42 +020085/* ACLs can be evaluated on requests and on responses, and on partial or complete data */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020086enum {
87 ACL_DIR_REQ = 0, /* ACL evaluated on request */
Willy Tarreaub6866442008-07-14 23:54:42 +020088 ACL_DIR_RTR = (1 << 0), /* ACL evaluated on response */
89 ACL_DIR_MASK = (ACL_DIR_REQ | ACL_DIR_RTR),
90 ACL_PARTIAL = (1 << 1), /* partial data, return MISS if data are missing */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020091};
92
Willy Tarreauc8d7c962007-06-17 08:20:33 +020093/* possible flags for expressions or patterns */
94enum {
95 ACL_PAT_F_IGNORE_CASE = 1 << 0, /* ignore case */
96 ACL_PAT_F_FROM_FILE = 1 << 1, /* pattern comes from a file */
97};
98
Willy Tarreaua84d3742007-05-07 00:36:48 +020099/* How to store a time range and the valid days in 29 bits */
100struct acl_time {
101 int dow:7; /* 1 bit per day of week: 0-6 */
102 int h1:5, m1:6; /* 0..24:0..60. Use 0:0 for all day. */
103 int h2:5, m2:6; /* 0..24:0..60. Use 24:0 for all day. */
104};
105
106/* The acl will be linked to from the proxy where it is declared */
107struct acl_pattern {
108 struct list list; /* chaining */
109 union {
110 int i; /* integer value */
Willy Tarreauae8b7962007-06-09 23:10:04 +0200111 struct {
112 signed long long min, max;
113 int min_set :1;
114 int max_set :1;
115 } range; /* integer range */
Willy Tarreaua67fad92007-05-08 19:50:09 +0200116 struct {
117 struct in_addr addr;
118 struct in_addr mask;
119 } ipv4; /* IPv4 address */
Willy Tarreaua84d3742007-05-07 00:36:48 +0200120 struct acl_time time; /* valid hours and days */
121 } val; /* direct value */
122 union {
123 void *ptr; /* any data */
124 char *str; /* any string */
125 regex_t *reg; /* a compiled regex */
126 } ptr; /* indirect values, allocated */
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +0200127 void(*freeptrbuf)(void *ptr); /* a destructor able to free objects from the ptr */
Willy Tarreaua84d3742007-05-07 00:36:48 +0200128 int len; /* data length when required */
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200129 int flags; /* expr or pattern flags. */
Willy Tarreaua84d3742007-05-07 00:36:48 +0200130};
131
132/* The structure exchanged between an acl_fetch_* function responsible for
133 * retrieving a value, and an acl_match_* function responsible for testing it.
134 */
135struct acl_test {
Willy Tarreau33a7e692007-06-10 19:45:56 +0200136 int i; /* integer value */
137 char *ptr; /* pointer to beginning of value */
138 int len; /* length of value at ptr, otherwise ignored */
139 int flags; /* ACL_TEST_F_* set to 0 on first call */
140 union { /* fetch_* functions context for any purpose */
141 void *p; /* any pointer */
142 int i; /* any integer */
143 long long ll; /* any long long or smaller */
144 double d; /* any float or double */
145 void *a[8]; /* any array of up to 8 pointers */
Willy Tarreaua84d3742007-05-07 00:36:48 +0200146 } ctx;
147};
148
149
150/*
151 * ACL keyword: Associates keywords with parsers, methods to retrieve the value and testers.
152 */
153
154/* some dummy declarations to silent the compiler */
155struct proxy;
156struct session;
157
Willy Tarreauae8b7962007-06-09 23:10:04 +0200158/*
159 * NOTE:
160 * The 'parse' function is called to parse words in the configuration. It must
161 * return the number of valid words read. 0 = error. The 'opaque' argument may
162 * be used by functions which need to maintain a context between consecutive
163 * values. It is initialized to zero before the first call, and passed along
164 * successive calls.
165 */
166
Willy Tarreau97be1452007-06-10 11:47:14 +0200167struct acl_expr;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200168struct acl_keyword {
169 const char *kw;
Willy Tarreauae8b7962007-06-09 23:10:04 +0200170 int (*parse)(const char **text, struct acl_pattern *pattern, int *opaque);
Willy Tarreau97be1452007-06-10 11:47:14 +0200171 int (*fetch)(struct proxy *px, struct session *l4, void *l7, int dir,
172 struct acl_expr *expr, struct acl_test *test);
Willy Tarreaua84d3742007-05-07 00:36:48 +0200173 int (*match)(struct acl_test *test, struct acl_pattern *pattern);
174 int use_cnt;
175};
176
177/*
178 * A keyword list. It is a NULL-terminated array of keywords. It embeds a
179 * struct list in order to be linked to other lists, allowing it to easily
180 * be declared where it is needed, and linked without duplicating data nor
181 * allocating memory.
182 */
183struct acl_kw_list {
184 struct list list;
185 struct acl_keyword kw[VAR_ARRAY];
186};
187
188/*
189 * Description of an ACL expression.
190 * It contains a subject and a set of patterns to test against it.
191 * - the function get() is called to retrieve the subject from the
192 * current session or transaction and build a test.
193 * - the function test() is called to evaluate the test based on the
194 * available patterns and return ACL_PAT_*
195 * Both of those functions are available through the keyword.
196 */
197struct acl_expr {
198 struct list list; /* chaining */
199 struct acl_keyword *kw; /* back-reference to the keyword */
200 union { /* optional argument of the subject (eg: header or cookie name) */
201 char *str;
202 } arg;
Willy Tarreaubb768912007-06-10 11:17:01 +0200203 int arg_len; /* optional argument length */
Willy Tarreaua84d3742007-05-07 00:36:48 +0200204 struct list patterns; /* list of acl_patterns */
205};
206
207struct acl {
208 struct list list; /* chaining */
209 char *name; /* acl name */
210 struct list expr; /* list of acl_exprs */
211 int cache_idx; /* ACL index in cache */
212};
213
214/* the condition will be linked to from an action in a proxy */
215struct acl_term {
216 struct list list; /* chaining */
217 struct acl *acl; /* acl pointed to by this term */
218 int neg; /* 1 if the ACL result must be negated */
219};
220
221struct acl_term_suite {
222 struct list list; /* chaining of term suites */
223 struct list terms; /* list of acl_terms */
224};
225
226struct acl_cond {
227 struct list list; /* Some specific tests may use multiple conditions */
228 struct list suites; /* list of acl_term_suites */
229 int pol; /* polarity: ACL_COND_IF / ACL_COND_UNLESS */
230};
231
232
233#endif /* _TYPES_ACL_H */
234
235/*
236 * Local variables:
237 * c-indent-level: 8
238 * c-basic-offset: 8
239 * End:
240 */