blob: 5358878f127a90dd1a19861b26dd427b7bedd3fc [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>
Thierry FOURNIERed66c292013-11-28 11:05:19 +010031#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
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010038/* ACL test result.
Willy Tarreau11382812008-07-09 16:18:21 +020039 *
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 *
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010056 * For efficiency, the ACL return flags are directly mapped from the pattern
57 * match flags. See include/pattern.h for existing values.
Willy Tarreau11382812008-07-09 16:18:21 +020058 */
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010059enum acl_test_res {
60 ACL_TEST_FAIL = 0, /* test failed */
61 ACL_TEST_MISS = 1, /* test may pass with more info */
62 ACL_TEST_PASS = 3, /* test passed */
63};
Willy Tarreau11382812008-07-09 16:18:21 +020064
Willy Tarreaua84d3742007-05-07 00:36:48 +020065/* Condition polarity. It makes it easier for any option to choose between
66 * IF/UNLESS if it can store that information within the condition itself.
Willy Tarreau11382812008-07-09 16:18:21 +020067 * Those should be interpreted as "IF/UNLESS result == PASS".
Willy Tarreaua84d3742007-05-07 00:36:48 +020068 */
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010069enum acl_cond_pol {
Willy Tarreaua84d3742007-05-07 00:36:48 +020070 ACL_COND_NONE, /* no polarity set yet */
71 ACL_COND_IF, /* positive condition (after 'if') */
72 ACL_COND_UNLESS, /* negative condition (after 'unless') */
73};
74
Willy Tarreaua84d3742007-05-07 00:36:48 +020075/* some dummy declarations to silent the compiler */
76struct proxy;
77struct session;
78
Willy Tarreauae8b7962007-06-09 23:10:04 +020079/*
Willy Tarreauc92ddbc2012-04-27 22:10:57 +020080 * ACL keyword: Associates keywords with parsers, methods to retrieve the value and testers.
81 */
82/*
Willy Tarreauae8b7962007-06-09 23:10:04 +020083 * NOTE:
84 * The 'parse' function is called to parse words in the configuration. It must
85 * return the number of valid words read. 0 = error. The 'opaque' argument may
86 * be used by functions which need to maintain a context between consecutive
87 * values. It is initialized to zero before the first call, and passed along
88 * successive calls.
89 */
90
Willy Tarreau97be1452007-06-10 11:47:14 +020091struct acl_expr;
Willy Tarreaua84d3742007-05-07 00:36:48 +020092struct acl_keyword {
93 const char *kw;
Willy Tarreau8ed669b2013-01-11 15:49:37 +010094 char *fetch_kw;
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010095 int (*parse)(const char **text, struct pattern *pattern, struct sample_storage *smp, int *opaque, char **err);
96 int (*match)(struct sample *smp, struct pattern *pattern);
Willy Tarreau61612d42012-04-19 18:42:05 +020097 /* must be after the config params */
Willy Tarreau8ed669b2013-01-11 15:49:37 +010098 struct sample_fetch *smp; /* the sample fetch we depend on */
Willy Tarreaua84d3742007-05-07 00:36:48 +020099};
100
101/*
102 * A keyword list. It is a NULL-terminated array of keywords. It embeds a
103 * struct list in order to be linked to other lists, allowing it to easily
104 * be declared where it is needed, and linked without duplicating data nor
105 * allocating memory.
106 */
107struct acl_kw_list {
108 struct list list;
109 struct acl_keyword kw[VAR_ARRAY];
110};
111
112/*
113 * Description of an ACL expression.
Willy Tarreaud76a98a2013-03-31 18:34:33 +0200114 * The expression is part of a list. It contains pointers to the keyword, the
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +0100115 * sample fetch descriptor which defaults to the keyword's, and the associated
116 * pattern matching. The structure is organized so that the hot parts are
117 * grouped together in order to optimize caching.
Willy Tarreaua84d3742007-05-07 00:36:48 +0200118 */
119struct acl_expr {
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100120 struct sample_expr *smp; /* the sample expression we depend on */
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +0100121 struct pattern_expr pat; /* the pattern matching expression */
Willy Tarreaud76a98a2013-03-31 18:34:33 +0200122 struct list list; /* chaining */
Willy Tarreau93fddf12013-03-31 22:59:32 +0200123 const char *kw; /* points to the ACL kw's name or fetch's name (must not free) */
Willy Tarreaua84d3742007-05-07 00:36:48 +0200124};
125
Willy Tarreauc92ddbc2012-04-27 22:10:57 +0200126/* The acl will be linked to from the proxy where it is declared */
Willy Tarreaua84d3742007-05-07 00:36:48 +0200127struct acl {
128 struct list list; /* chaining */
129 char *name; /* acl name */
130 struct list expr; /* list of acl_exprs */
131 int cache_idx; /* ACL index in cache */
Willy Tarreaua91d0a52013-03-25 08:12:18 +0100132 unsigned int use; /* or'ed bit mask of all acl_expr's SMP_USE_* */
133 unsigned int val; /* or'ed bit mask of all acl_expr's SMP_VAL_* */
Willy Tarreaua84d3742007-05-07 00:36:48 +0200134};
135
136/* the condition will be linked to from an action in a proxy */
137struct acl_term {
138 struct list list; /* chaining */
139 struct acl *acl; /* acl pointed to by this term */
140 int neg; /* 1 if the ACL result must be negated */
141};
142
143struct acl_term_suite {
144 struct list list; /* chaining of term suites */
145 struct list terms; /* list of acl_terms */
146};
147
148struct acl_cond {
149 struct list list; /* Some specific tests may use multiple conditions */
150 struct list suites; /* list of acl_term_suites */
151 int pol; /* polarity: ACL_COND_IF / ACL_COND_UNLESS */
Willy Tarreaua91d0a52013-03-25 08:12:18 +0100152 unsigned int use; /* or'ed bit mask of all suites's SMP_USE_* */
153 unsigned int val; /* or'ed bit mask of all suites's SMP_VAL_* */
Willy Tarreau88922352009-10-04 22:02:50 +0200154 const char *file; /* config file where the condition is declared */
Willy Tarreaua9802632008-07-25 19:13:19 +0200155 int line; /* line in the config file where the condition is declared */
Willy Tarreaua84d3742007-05-07 00:36:48 +0200156};
157
Willy Tarreaua84d3742007-05-07 00:36:48 +0200158#endif /* _TYPES_ACL_H */
159
160/*
161 * Local variables:
162 * c-indent-level: 8
163 * c-basic-offset: 8
164 * End:
165 */