blob: 65644cc82e6b71f591a40b1ec414824f228a7a34 [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 Tarreaua84d3742007-05-07 00:36:48 +020082/* How to store a time range and the valid days in 29 bits */
83struct acl_time {
84 int dow:7; /* 1 bit per day of week: 0-6 */
85 int h1:5, m1:6; /* 0..24:0..60. Use 0:0 for all day. */
86 int h2:5, m2:6; /* 0..24:0..60. Use 24:0 for all day. */
87};
88
Willy Tarreauc92ddbc2012-04-27 22:10:57 +020089/* This describes one ACL pattern, which might be a single value or a tree of
90 * values. All patterns for a single ACL expression are linked together. Some
91 * of them might have a type (eg: IP). Right now, the types are shared with
92 * the samples, though it is possible that in the future this will change to
93 * accommodate for other types (eg: meth, regex). Unsigned and constant types
94 * are preferred when there is a doubt.
95 */
Willy Tarreaua84d3742007-05-07 00:36:48 +020096struct acl_pattern {
97 struct list list; /* chaining */
Willy Tarreauc92ddbc2012-04-27 22:10:57 +020098 int type; /* type of the ACL pattern (SMP_T_*) */
Willy Tarreaua84d3742007-05-07 00:36:48 +020099 union {
100 int i; /* integer value */
Willy Tarreauae8b7962007-06-09 23:10:04 +0200101 struct {
102 signed long long min, max;
103 int min_set :1;
104 int max_set :1;
105 } range; /* integer range */
Willy Tarreaua67fad92007-05-08 19:50:09 +0200106 struct {
107 struct in_addr addr;
108 struct in_addr mask;
109 } ipv4; /* IPv4 address */
Willy Tarreauceb4ac92012-04-28 00:41:46 +0200110 struct {
111 struct in6_addr addr;
112 unsigned char mask; /* number of bits */
113 } ipv6; /* IPv6 address/mask */
Willy Tarreaua84d3742007-05-07 00:36:48 +0200114 struct acl_time time; /* valid hours and days */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +0100115 unsigned int group_mask;
Willy Tarreaue56cda92010-05-11 23:25:05 +0200116 struct eb_root *tree; /* tree storing all values if any */
Willy Tarreaua84d3742007-05-07 00:36:48 +0200117 } val; /* direct value */
118 union {
119 void *ptr; /* any data */
120 char *str; /* any string */
Hiroaki Nakamura70351322013-01-13 15:00:42 +0900121 regex *reg; /* a compiled regex */
Willy Tarreaua84d3742007-05-07 00:36:48 +0200122 } ptr; /* indirect values, allocated */
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +0200123 void(*freeptrbuf)(void *ptr); /* a destructor able to free objects from the ptr */
Willy Tarreaua84d3742007-05-07 00:36:48 +0200124 int len; /* data length when required */
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200125 int flags; /* expr or pattern flags. */
Willy Tarreaua84d3742007-05-07 00:36:48 +0200126};
127
Willy Tarreaua84d3742007-05-07 00:36:48 +0200128/* some dummy declarations to silent the compiler */
129struct proxy;
130struct session;
131
Willy Tarreauae8b7962007-06-09 23:10:04 +0200132/*
Willy Tarreauc92ddbc2012-04-27 22:10:57 +0200133 * ACL keyword: Associates keywords with parsers, methods to retrieve the value and testers.
134 */
135/*
Willy Tarreauae8b7962007-06-09 23:10:04 +0200136 * NOTE:
137 * The 'parse' function is called to parse words in the configuration. It must
138 * return the number of valid words read. 0 = error. The 'opaque' argument may
139 * be used by functions which need to maintain a context between consecutive
140 * values. It is initialized to zero before the first call, and passed along
141 * successive calls.
142 */
143
Willy Tarreau97be1452007-06-10 11:47:14 +0200144struct acl_expr;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200145struct acl_keyword {
146 const char *kw;
Willy Tarreau8ed669b2013-01-11 15:49:37 +0100147 char *fetch_kw;
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200148 int (*parse)(const char **text, struct acl_pattern *pattern, int *opaque, char **err);
Willy Tarreau37406352012-04-23 16:16:37 +0200149 int (*match)(struct sample *smp, struct acl_pattern *pattern);
Willy Tarreau61612d42012-04-19 18:42:05 +0200150 /* must be after the config params */
Willy Tarreau8ed669b2013-01-11 15:49:37 +0100151 struct sample_fetch *smp; /* the sample fetch we depend on */
Willy Tarreaua84d3742007-05-07 00:36:48 +0200152 int use_cnt;
153};
154
155/*
156 * A keyword list. It is a NULL-terminated array of keywords. It embeds a
157 * struct list in order to be linked to other lists, allowing it to easily
158 * be declared where it is needed, and linked without duplicating data nor
159 * allocating memory.
160 */
161struct acl_kw_list {
162 struct list list;
163 struct acl_keyword kw[VAR_ARRAY];
164};
165
166/*
167 * Description of an ACL expression.
Willy Tarreaud76a98a2013-03-31 18:34:33 +0200168 * The expression is part of a list. It contains pointers to the keyword, the
169 * parse and match functions which default to the keyword's, the sample fetch
170 * descriptor which also defaults to the keyword's, and a list or tree of
171 * patterns to test against. The structure is organized so that the hot parts
172 * are grouped together in order to optimize caching.
Willy Tarreaua84d3742007-05-07 00:36:48 +0200173 */
174struct acl_expr {
Willy Tarreaud76a98a2013-03-31 18:34:33 +0200175 int (*parse)(const char **text, struct acl_pattern *pattern, int *opaque, char **err);
176 int (*match)(struct sample *smp, struct acl_pattern *pattern);
177 struct arg *args; /* optional fetch argument list (eg: header or cookie name) */
178 struct sample_fetch *smp; /* the sample fetch we depend on */
179 struct list patterns; /* list of acl_patterns */
Willy Tarreau438b0f32010-05-10 22:29:06 +0200180 struct eb_root pattern_tree; /* may be used for lookup in large datasets */
Willy Tarreaud76a98a2013-03-31 18:34:33 +0200181 struct list list; /* chaining */
182 struct acl_keyword *kw; /* back-reference to the keyword */
Willy Tarreaua84d3742007-05-07 00:36:48 +0200183};
184
Willy Tarreauc92ddbc2012-04-27 22:10:57 +0200185/* The acl will be linked to from the proxy where it is declared */
Willy Tarreaua84d3742007-05-07 00:36:48 +0200186struct acl {
187 struct list list; /* chaining */
188 char *name; /* acl name */
189 struct list expr; /* list of acl_exprs */
190 int cache_idx; /* ACL index in cache */
Willy Tarreaua91d0a52013-03-25 08:12:18 +0100191 unsigned int use; /* or'ed bit mask of all acl_expr's SMP_USE_* */
192 unsigned int val; /* or'ed bit mask of all acl_expr's SMP_VAL_* */
Willy Tarreaua84d3742007-05-07 00:36:48 +0200193};
194
195/* the condition will be linked to from an action in a proxy */
196struct acl_term {
197 struct list list; /* chaining */
198 struct acl *acl; /* acl pointed to by this term */
199 int neg; /* 1 if the ACL result must be negated */
200};
201
202struct acl_term_suite {
203 struct list list; /* chaining of term suites */
204 struct list terms; /* list of acl_terms */
205};
206
207struct acl_cond {
208 struct list list; /* Some specific tests may use multiple conditions */
209 struct list suites; /* list of acl_term_suites */
210 int pol; /* polarity: ACL_COND_IF / ACL_COND_UNLESS */
Willy Tarreaua91d0a52013-03-25 08:12:18 +0100211 unsigned int use; /* or'ed bit mask of all suites's SMP_USE_* */
212 unsigned int val; /* or'ed bit mask of all suites's SMP_VAL_* */
Willy Tarreau88922352009-10-04 22:02:50 +0200213 const char *file; /* config file where the condition is declared */
Willy Tarreaua9802632008-07-25 19:13:19 +0200214 int line; /* line in the config file where the condition is declared */
Willy Tarreaua84d3742007-05-07 00:36:48 +0200215};
216
217
218#endif /* _TYPES_ACL_H */
219
220/*
221 * Local variables:
222 * c-indent-level: 8
223 * c-basic-offset: 8
224 * End:
225 */