blob: 322e74b97ddad961406307e7e35142717b53841b [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 Tarreaucd3b0942012-04-27 21:52:18 +020032#include <types/sample.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 Tarreauc8d7c962007-06-17 08:20:33 +020074/* possible flags for expressions or patterns */
75enum {
76 ACL_PAT_F_IGNORE_CASE = 1 << 0, /* ignore case */
77 ACL_PAT_F_FROM_FILE = 1 << 1, /* pattern comes from a file */
Willy Tarreau438b0f32010-05-10 22:29:06 +020078 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 Tarreauc8d7c962007-06-17 08:20:33 +020080};
81
Willy Tarreau5adeda12013-03-31 22:13:34 +020082/* ACL match methods */
83enum {
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 Tarreaua84d3742007-05-07 00:36:48 +0200101/* How to store a time range and the valid days in 29 bits */
102struct 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
Thierry FOURNIERdd69a042013-11-22 19:14:42 +0100108/* This contain each tree indexed entry. This struct permit to associate
109 * "sample" with a tree entry. It is used with maps.
110 */
111struct acl_idx_elt {
112 struct sample_storage *smp;
113 struct ebmb_node node;
114};
115
Willy Tarreauc92ddbc2012-04-27 22:10:57 +0200116/* This describes one ACL pattern, which might be a single value or a tree of
117 * values. All patterns for a single ACL expression are linked together. Some
118 * of them might have a type (eg: IP). Right now, the types are shared with
119 * the samples, though it is possible that in the future this will change to
120 * accommodate for other types (eg: meth, regex). Unsigned and constant types
121 * are preferred when there is a doubt.
122 */
Willy Tarreaua84d3742007-05-07 00:36:48 +0200123struct acl_pattern {
124 struct list list; /* chaining */
Willy Tarreauc92ddbc2012-04-27 22:10:57 +0200125 int type; /* type of the ACL pattern (SMP_T_*) */
Willy Tarreaua84d3742007-05-07 00:36:48 +0200126 union {
127 int i; /* integer value */
Willy Tarreauae8b7962007-06-09 23:10:04 +0200128 struct {
129 signed long long min, max;
130 int min_set :1;
131 int max_set :1;
132 } range; /* integer range */
Willy Tarreaua67fad92007-05-08 19:50:09 +0200133 struct {
134 struct in_addr addr;
135 struct in_addr mask;
136 } ipv4; /* IPv4 address */
Willy Tarreauceb4ac92012-04-28 00:41:46 +0200137 struct {
138 struct in6_addr addr;
139 unsigned char mask; /* number of bits */
140 } ipv6; /* IPv6 address/mask */
Willy Tarreaua84d3742007-05-07 00:36:48 +0200141 struct acl_time time; /* valid hours and days */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +0100142 unsigned int group_mask;
Willy Tarreaue56cda92010-05-11 23:25:05 +0200143 struct eb_root *tree; /* tree storing all values if any */
Willy Tarreaua84d3742007-05-07 00:36:48 +0200144 } val; /* direct value */
145 union {
146 void *ptr; /* any data */
147 char *str; /* any string */
Hiroaki Nakamura70351322013-01-13 15:00:42 +0900148 regex *reg; /* a compiled regex */
Willy Tarreaua84d3742007-05-07 00:36:48 +0200149 } ptr; /* indirect values, allocated */
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +0200150 void(*freeptrbuf)(void *ptr); /* a destructor able to free objects from the ptr */
Willy Tarreaua84d3742007-05-07 00:36:48 +0200151 int len; /* data length when required */
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200152 int flags; /* expr or pattern flags. */
Thierry FOURNIERdd69a042013-11-22 19:14:42 +0100153 struct sample_storage *smp; /* used to store a pointer to sample value associated
154 with the match. It is used with maps */
155
Willy Tarreaua84d3742007-05-07 00:36:48 +0200156};
157
Willy Tarreaua84d3742007-05-07 00:36:48 +0200158/* some dummy declarations to silent the compiler */
159struct proxy;
160struct session;
161
Willy Tarreauae8b7962007-06-09 23:10:04 +0200162/*
Willy Tarreauc92ddbc2012-04-27 22:10:57 +0200163 * ACL keyword: Associates keywords with parsers, methods to retrieve the value and testers.
164 */
165/*
Willy Tarreauae8b7962007-06-09 23:10:04 +0200166 * NOTE:
167 * The 'parse' function is called to parse words in the configuration. It must
168 * return the number of valid words read. 0 = error. The 'opaque' argument may
169 * be used by functions which need to maintain a context between consecutive
170 * values. It is initialized to zero before the first call, and passed along
171 * successive calls.
172 */
173
Willy Tarreau97be1452007-06-10 11:47:14 +0200174struct acl_expr;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200175struct acl_keyword {
176 const char *kw;
Willy Tarreau8ed669b2013-01-11 15:49:37 +0100177 char *fetch_kw;
Thierry FOURNIERdd69a042013-11-22 19:14:42 +0100178 int (*parse)(const char **text, struct acl_pattern *pattern, struct sample_storage *smp, int *opaque, char **err);
Willy Tarreau37406352012-04-23 16:16:37 +0200179 int (*match)(struct sample *smp, struct acl_pattern *pattern);
Willy Tarreau61612d42012-04-19 18:42:05 +0200180 /* must be after the config params */
Willy Tarreau8ed669b2013-01-11 15:49:37 +0100181 struct sample_fetch *smp; /* the sample fetch we depend on */
Willy Tarreaua84d3742007-05-07 00:36:48 +0200182};
183
184/*
185 * A keyword list. It is a NULL-terminated array of keywords. It embeds a
186 * struct list in order to be linked to other lists, allowing it to easily
187 * be declared where it is needed, and linked without duplicating data nor
188 * allocating memory.
189 */
190struct acl_kw_list {
191 struct list list;
192 struct acl_keyword kw[VAR_ARRAY];
193};
194
195/*
196 * Description of an ACL expression.
Willy Tarreaud76a98a2013-03-31 18:34:33 +0200197 * The expression is part of a list. It contains pointers to the keyword, the
198 * parse and match functions which default to the keyword's, the sample fetch
199 * descriptor which also defaults to the keyword's, and a list or tree of
200 * patterns to test against. The structure is organized so that the hot parts
201 * are grouped together in order to optimize caching.
Willy Tarreaua84d3742007-05-07 00:36:48 +0200202 */
203struct acl_expr {
Thierry FOURNIERdd69a042013-11-22 19:14:42 +0100204 int (*parse)(const char **text, struct acl_pattern *pattern, struct sample_storage *smp, int *opaque, char **err);
Willy Tarreaud76a98a2013-03-31 18:34:33 +0200205 int (*match)(struct sample *smp, struct acl_pattern *pattern);
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100206 struct sample_expr *smp; /* the sample expression we depend on */
Willy Tarreaud76a98a2013-03-31 18:34:33 +0200207 struct list patterns; /* list of acl_patterns */
Willy Tarreau438b0f32010-05-10 22:29:06 +0200208 struct eb_root pattern_tree; /* may be used for lookup in large datasets */
Willy Tarreaud76a98a2013-03-31 18:34:33 +0200209 struct list list; /* chaining */
Willy Tarreau93fddf12013-03-31 22:59:32 +0200210 const char *kw; /* points to the ACL kw's name or fetch's name (must not free) */
Willy Tarreaua84d3742007-05-07 00:36:48 +0200211};
212
Willy Tarreauc92ddbc2012-04-27 22:10:57 +0200213/* The acl will be linked to from the proxy where it is declared */
Willy Tarreaua84d3742007-05-07 00:36:48 +0200214struct acl {
215 struct list list; /* chaining */
216 char *name; /* acl name */
217 struct list expr; /* list of acl_exprs */
218 int cache_idx; /* ACL index in cache */
Willy Tarreaua91d0a52013-03-25 08:12:18 +0100219 unsigned int use; /* or'ed bit mask of all acl_expr's SMP_USE_* */
220 unsigned int val; /* or'ed bit mask of all acl_expr's SMP_VAL_* */
Willy Tarreaua84d3742007-05-07 00:36:48 +0200221};
222
223/* the condition will be linked to from an action in a proxy */
224struct acl_term {
225 struct list list; /* chaining */
226 struct acl *acl; /* acl pointed to by this term */
227 int neg; /* 1 if the ACL result must be negated */
228};
229
230struct acl_term_suite {
231 struct list list; /* chaining of term suites */
232 struct list terms; /* list of acl_terms */
233};
234
235struct acl_cond {
236 struct list list; /* Some specific tests may use multiple conditions */
237 struct list suites; /* list of acl_term_suites */
238 int pol; /* polarity: ACL_COND_IF / ACL_COND_UNLESS */
Willy Tarreaua91d0a52013-03-25 08:12:18 +0100239 unsigned int use; /* or'ed bit mask of all suites's SMP_USE_* */
240 unsigned int val; /* or'ed bit mask of all suites's SMP_VAL_* */
Willy Tarreau88922352009-10-04 22:02:50 +0200241 const char *file; /* config file where the condition is declared */
Willy Tarreaua9802632008-07-25 19:13:19 +0200242 int line; /* line in the config file where the condition is declared */
Willy Tarreaua84d3742007-05-07 00:36:48 +0200243};
244
Thierry FOURNIER319e4952013-11-22 17:25:35 +0100245extern char *acl_match_names[ACL_MATCH_NUM];
Thierry FOURNIERdd69a042013-11-22 19:14:42 +0100246extern int (*acl_parse_fcts[ACL_MATCH_NUM])(const char **, struct acl_pattern *, struct sample_storage *, int *, char **);
Thierry FOURNIER319e4952013-11-22 17:25:35 +0100247extern int (*acl_match_fcts[ACL_MATCH_NUM])(struct sample *, struct acl_pattern *);
Willy Tarreaua84d3742007-05-07 00:36:48 +0200248
249#endif /* _TYPES_ACL_H */
250
251/*
252 * Local variables:
253 * c-indent-level: 8
254 * c-basic-offset: 8
255 * End:
256 */