blob: 006899e32a951f730973d35c8029127664d67320 [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
Willy Tarreauc92ddbc2012-04-27 22:10:57 +0200108/* This describes one ACL pattern, which might be a single value or a tree of
109 * values. All patterns for a single ACL expression are linked together. Some
110 * of them might have a type (eg: IP). Right now, the types are shared with
111 * the samples, though it is possible that in the future this will change to
112 * accommodate for other types (eg: meth, regex). Unsigned and constant types
113 * are preferred when there is a doubt.
114 */
Willy Tarreaua84d3742007-05-07 00:36:48 +0200115struct acl_pattern {
116 struct list list; /* chaining */
Willy Tarreauc92ddbc2012-04-27 22:10:57 +0200117 int type; /* type of the ACL pattern (SMP_T_*) */
Willy Tarreaua84d3742007-05-07 00:36:48 +0200118 union {
119 int i; /* integer value */
Willy Tarreauae8b7962007-06-09 23:10:04 +0200120 struct {
121 signed long long min, max;
122 int min_set :1;
123 int max_set :1;
124 } range; /* integer range */
Willy Tarreaua67fad92007-05-08 19:50:09 +0200125 struct {
126 struct in_addr addr;
127 struct in_addr mask;
128 } ipv4; /* IPv4 address */
Willy Tarreauceb4ac92012-04-28 00:41:46 +0200129 struct {
130 struct in6_addr addr;
131 unsigned char mask; /* number of bits */
132 } ipv6; /* IPv6 address/mask */
Willy Tarreaua84d3742007-05-07 00:36:48 +0200133 struct acl_time time; /* valid hours and days */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +0100134 unsigned int group_mask;
Willy Tarreaue56cda92010-05-11 23:25:05 +0200135 struct eb_root *tree; /* tree storing all values if any */
Willy Tarreaua84d3742007-05-07 00:36:48 +0200136 } val; /* direct value */
137 union {
138 void *ptr; /* any data */
139 char *str; /* any string */
Hiroaki Nakamura70351322013-01-13 15:00:42 +0900140 regex *reg; /* a compiled regex */
Willy Tarreaua84d3742007-05-07 00:36:48 +0200141 } ptr; /* indirect values, allocated */
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +0200142 void(*freeptrbuf)(void *ptr); /* a destructor able to free objects from the ptr */
Willy Tarreaua84d3742007-05-07 00:36:48 +0200143 int len; /* data length when required */
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200144 int flags; /* expr or pattern flags. */
Willy Tarreaua84d3742007-05-07 00:36:48 +0200145};
146
Willy Tarreaua84d3742007-05-07 00:36:48 +0200147/* some dummy declarations to silent the compiler */
148struct proxy;
149struct session;
150
Willy Tarreauae8b7962007-06-09 23:10:04 +0200151/*
Willy Tarreauc92ddbc2012-04-27 22:10:57 +0200152 * ACL keyword: Associates keywords with parsers, methods to retrieve the value and testers.
153 */
154/*
Willy Tarreauae8b7962007-06-09 23:10:04 +0200155 * NOTE:
156 * The 'parse' function is called to parse words in the configuration. It must
157 * return the number of valid words read. 0 = error. The 'opaque' argument may
158 * be used by functions which need to maintain a context between consecutive
159 * values. It is initialized to zero before the first call, and passed along
160 * successive calls.
161 */
162
Willy Tarreau97be1452007-06-10 11:47:14 +0200163struct acl_expr;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200164struct acl_keyword {
165 const char *kw;
Willy Tarreau8ed669b2013-01-11 15:49:37 +0100166 char *fetch_kw;
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200167 int (*parse)(const char **text, struct acl_pattern *pattern, int *opaque, char **err);
Willy Tarreau37406352012-04-23 16:16:37 +0200168 int (*match)(struct sample *smp, struct acl_pattern *pattern);
Willy Tarreau61612d42012-04-19 18:42:05 +0200169 /* must be after the config params */
Willy Tarreau8ed669b2013-01-11 15:49:37 +0100170 struct sample_fetch *smp; /* the sample fetch we depend on */
Willy Tarreaua84d3742007-05-07 00:36:48 +0200171};
172
173/*
174 * A keyword list. It is a NULL-terminated array of keywords. It embeds a
175 * struct list in order to be linked to other lists, allowing it to easily
176 * be declared where it is needed, and linked without duplicating data nor
177 * allocating memory.
178 */
179struct acl_kw_list {
180 struct list list;
181 struct acl_keyword kw[VAR_ARRAY];
182};
183
184/*
185 * Description of an ACL expression.
Willy Tarreaud76a98a2013-03-31 18:34:33 +0200186 * The expression is part of a list. It contains pointers to the keyword, the
187 * parse and match functions which default to the keyword's, the sample fetch
188 * descriptor which also defaults to the keyword's, and a list or tree of
189 * patterns to test against. The structure is organized so that the hot parts
190 * are grouped together in order to optimize caching.
Willy Tarreaua84d3742007-05-07 00:36:48 +0200191 */
192struct acl_expr {
Willy Tarreaud76a98a2013-03-31 18:34:33 +0200193 int (*parse)(const char **text, struct acl_pattern *pattern, int *opaque, char **err);
194 int (*match)(struct sample *smp, struct acl_pattern *pattern);
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100195 struct sample_expr *smp; /* the sample expression we depend on */
Willy Tarreaud76a98a2013-03-31 18:34:33 +0200196 struct list patterns; /* list of acl_patterns */
Willy Tarreau438b0f32010-05-10 22:29:06 +0200197 struct eb_root pattern_tree; /* may be used for lookup in large datasets */
Willy Tarreaud76a98a2013-03-31 18:34:33 +0200198 struct list list; /* chaining */
Willy Tarreau93fddf12013-03-31 22:59:32 +0200199 const char *kw; /* points to the ACL kw's name or fetch's name (must not free) */
Willy Tarreaua84d3742007-05-07 00:36:48 +0200200};
201
Willy Tarreauc92ddbc2012-04-27 22:10:57 +0200202/* The acl will be linked to from the proxy where it is declared */
Willy Tarreaua84d3742007-05-07 00:36:48 +0200203struct acl {
204 struct list list; /* chaining */
205 char *name; /* acl name */
206 struct list expr; /* list of acl_exprs */
207 int cache_idx; /* ACL index in cache */
Willy Tarreaua91d0a52013-03-25 08:12:18 +0100208 unsigned int use; /* or'ed bit mask of all acl_expr's SMP_USE_* */
209 unsigned int val; /* or'ed bit mask of all acl_expr's SMP_VAL_* */
Willy Tarreaua84d3742007-05-07 00:36:48 +0200210};
211
212/* the condition will be linked to from an action in a proxy */
213struct acl_term {
214 struct list list; /* chaining */
215 struct acl *acl; /* acl pointed to by this term */
216 int neg; /* 1 if the ACL result must be negated */
217};
218
219struct acl_term_suite {
220 struct list list; /* chaining of term suites */
221 struct list terms; /* list of acl_terms */
222};
223
224struct acl_cond {
225 struct list list; /* Some specific tests may use multiple conditions */
226 struct list suites; /* list of acl_term_suites */
227 int pol; /* polarity: ACL_COND_IF / ACL_COND_UNLESS */
Willy Tarreaua91d0a52013-03-25 08:12:18 +0100228 unsigned int use; /* or'ed bit mask of all suites's SMP_USE_* */
229 unsigned int val; /* or'ed bit mask of all suites's SMP_VAL_* */
Willy Tarreau88922352009-10-04 22:02:50 +0200230 const char *file; /* config file where the condition is declared */
Willy Tarreaua9802632008-07-25 19:13:19 +0200231 int line; /* line in the config file where the condition is declared */
Willy Tarreaua84d3742007-05-07 00:36:48 +0200232};
233
234
235#endif /* _TYPES_ACL_H */
236
237/*
238 * Local variables:
239 * c-indent-level: 8
240 * c-basic-offset: 8
241 * End:
242 */