Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 1 | /* |
Willy Tarreau | 8892235 | 2009-10-04 22:02:50 +0200 | [diff] [blame] | 2 | * include/types/acl.h |
| 3 | * This file provides structures and types for ACLs. |
| 4 | * |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 5 | * Copyright (C) 2000-2012 Willy Tarreau - w@1wt.eu |
Willy Tarreau | 8892235 | 2009-10-04 22:02:50 +0200 | [diff] [blame] | 6 | * |
| 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 Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 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 | |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 29 | #include <types/arg.h> |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 30 | #include <types/auth.h> |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 31 | #include <types/pattern.h> |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 32 | #include <types/proxy.h> |
Willy Tarreau | 0b1cd94 | 2010-05-16 22:18:27 +0200 | [diff] [blame] | 33 | #include <types/server.h> |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 34 | #include <types/session.h> |
| 35 | |
Willy Tarreau | 438b0f3 | 2010-05-10 22:29:06 +0200 | [diff] [blame] | 36 | #include <ebmbtree.h> |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 37 | |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 38 | /* 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 Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 58 | /* Condition polarity. It makes it easier for any option to choose between |
| 59 | * IF/UNLESS if it can store that information within the condition itself. |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 60 | * Those should be interpreted as "IF/UNLESS result == PASS". |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 61 | */ |
| 62 | enum { |
| 63 | ACL_COND_NONE, /* no polarity set yet */ |
| 64 | ACL_COND_IF, /* positive condition (after 'if') */ |
| 65 | ACL_COND_UNLESS, /* negative condition (after 'unless') */ |
| 66 | }; |
| 67 | |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 68 | /* some dummy declarations to silent the compiler */ |
| 69 | struct proxy; |
| 70 | struct session; |
| 71 | |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 72 | /* |
Willy Tarreau | c92ddbc | 2012-04-27 22:10:57 +0200 | [diff] [blame] | 73 | * ACL keyword: Associates keywords with parsers, methods to retrieve the value and testers. |
| 74 | */ |
| 75 | /* |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 76 | * NOTE: |
| 77 | * The 'parse' function is called to parse words in the configuration. It must |
| 78 | * return the number of valid words read. 0 = error. The 'opaque' argument may |
| 79 | * be used by functions which need to maintain a context between consecutive |
| 80 | * values. It is initialized to zero before the first call, and passed along |
| 81 | * successive calls. |
| 82 | */ |
| 83 | |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 84 | struct acl_expr; |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 85 | struct acl_keyword { |
| 86 | const char *kw; |
Willy Tarreau | 8ed669b | 2013-01-11 15:49:37 +0100 | [diff] [blame] | 87 | char *fetch_kw; |
Thierry FOURNIER | dd69a04 | 2013-11-22 19:14:42 +0100 | [diff] [blame] | 88 | int (*parse)(const char **text, struct acl_pattern *pattern, struct sample_storage *smp, int *opaque, char **err); |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 89 | int (*match)(struct sample *smp, struct acl_pattern *pattern); |
Willy Tarreau | 61612d4 | 2012-04-19 18:42:05 +0200 | [diff] [blame] | 90 | /* must be after the config params */ |
Willy Tarreau | 8ed669b | 2013-01-11 15:49:37 +0100 | [diff] [blame] | 91 | struct sample_fetch *smp; /* the sample fetch we depend on */ |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 92 | }; |
| 93 | |
| 94 | /* |
| 95 | * A keyword list. It is a NULL-terminated array of keywords. It embeds a |
| 96 | * struct list in order to be linked to other lists, allowing it to easily |
| 97 | * be declared where it is needed, and linked without duplicating data nor |
| 98 | * allocating memory. |
| 99 | */ |
| 100 | struct acl_kw_list { |
| 101 | struct list list; |
| 102 | struct acl_keyword kw[VAR_ARRAY]; |
| 103 | }; |
| 104 | |
| 105 | /* |
| 106 | * Description of an ACL expression. |
Willy Tarreau | d76a98a | 2013-03-31 18:34:33 +0200 | [diff] [blame] | 107 | * The expression is part of a list. It contains pointers to the keyword, the |
Thierry FOURNIER | d163e1c | 2013-11-28 11:41:23 +0100 | [diff] [blame^] | 108 | * sample fetch descriptor which defaults to the keyword's, and the associated |
| 109 | * pattern matching. The structure is organized so that the hot parts are |
| 110 | * grouped together in order to optimize caching. |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 111 | */ |
| 112 | struct acl_expr { |
Thierry FOURNIER | 348971e | 2013-11-21 10:50:10 +0100 | [diff] [blame] | 113 | struct sample_expr *smp; /* the sample expression we depend on */ |
Thierry FOURNIER | d163e1c | 2013-11-28 11:41:23 +0100 | [diff] [blame^] | 114 | struct pattern_expr pat; /* the pattern matching expression */ |
Willy Tarreau | d76a98a | 2013-03-31 18:34:33 +0200 | [diff] [blame] | 115 | struct list list; /* chaining */ |
Willy Tarreau | 93fddf1 | 2013-03-31 22:59:32 +0200 | [diff] [blame] | 116 | const char *kw; /* points to the ACL kw's name or fetch's name (must not free) */ |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 117 | }; |
| 118 | |
Willy Tarreau | c92ddbc | 2012-04-27 22:10:57 +0200 | [diff] [blame] | 119 | /* The acl will be linked to from the proxy where it is declared */ |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 120 | struct acl { |
| 121 | struct list list; /* chaining */ |
| 122 | char *name; /* acl name */ |
| 123 | struct list expr; /* list of acl_exprs */ |
| 124 | int cache_idx; /* ACL index in cache */ |
Willy Tarreau | a91d0a5 | 2013-03-25 08:12:18 +0100 | [diff] [blame] | 125 | unsigned int use; /* or'ed bit mask of all acl_expr's SMP_USE_* */ |
| 126 | unsigned int val; /* or'ed bit mask of all acl_expr's SMP_VAL_* */ |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 127 | }; |
| 128 | |
| 129 | /* the condition will be linked to from an action in a proxy */ |
| 130 | struct acl_term { |
| 131 | struct list list; /* chaining */ |
| 132 | struct acl *acl; /* acl pointed to by this term */ |
| 133 | int neg; /* 1 if the ACL result must be negated */ |
| 134 | }; |
| 135 | |
| 136 | struct acl_term_suite { |
| 137 | struct list list; /* chaining of term suites */ |
| 138 | struct list terms; /* list of acl_terms */ |
| 139 | }; |
| 140 | |
| 141 | struct acl_cond { |
| 142 | struct list list; /* Some specific tests may use multiple conditions */ |
| 143 | struct list suites; /* list of acl_term_suites */ |
| 144 | int pol; /* polarity: ACL_COND_IF / ACL_COND_UNLESS */ |
Willy Tarreau | a91d0a5 | 2013-03-25 08:12:18 +0100 | [diff] [blame] | 145 | unsigned int use; /* or'ed bit mask of all suites's SMP_USE_* */ |
| 146 | unsigned int val; /* or'ed bit mask of all suites's SMP_VAL_* */ |
Willy Tarreau | 8892235 | 2009-10-04 22:02:50 +0200 | [diff] [blame] | 147 | const char *file; /* config file where the condition is declared */ |
Willy Tarreau | a980263 | 2008-07-25 19:13:19 +0200 | [diff] [blame] | 148 | int line; /* line in the config file where the condition is declared */ |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 149 | }; |
| 150 | |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 151 | #endif /* _TYPES_ACL_H */ |
| 152 | |
| 153 | /* |
| 154 | * Local variables: |
| 155 | * c-indent-level: 8 |
| 156 | * c-basic-offset: 8 |
| 157 | * End: |
| 158 | */ |