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 | |
Willy Tarreau | ac13aea | 2020-06-04 10:36:03 +0200 | [diff] [blame] | 25 | #include <haproxy/auth-t.h> |
Willy Tarreau | 4c7e4b7 | 2020-05-27 12:58:42 +0200 | [diff] [blame] | 26 | #include <haproxy/api-t.h> |
Willy Tarreau | 853b297 | 2020-05-27 18:01:47 +0200 | [diff] [blame] | 27 | #include <haproxy/list-t.h> |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 28 | |
Willy Tarreau | aa74c4e | 2020-06-04 10:19:23 +0200 | [diff] [blame] | 29 | #include <haproxy/arg-t.h> |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 30 | #include <types/pattern.h> |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 31 | #include <types/proxy.h> |
Willy Tarreau | 0b1cd94 | 2010-05-16 22:18:27 +0200 | [diff] [blame] | 32 | #include <types/server.h> |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 33 | |
Willy Tarreau | 8d2b777 | 2020-05-27 10:58:19 +0200 | [diff] [blame] | 34 | #include <import/ebmbtree.h> |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 35 | |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 36 | /* ACL test result. |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 37 | * |
| 38 | * We're using a 3-state matching system : |
| 39 | * - PASS : at least one pattern already matches |
| 40 | * - MISS : some data is missing to decide if some rules may finally match. |
| 41 | * - FAIL : no mattern may ever match |
| 42 | * |
| 43 | * We assign values 0, 1 and 3 to FAIL, MISS and PASS respectively, so that we |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 44 | * can make use of standard arithmetic for the truth tables below : |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 45 | * |
| 46 | * x | !x x&y | F(0) | M(1) | P(3) x|y | F(0) | M(1) | P(3) |
| 47 | * ------+----- -----+------+------+----- -----+------+------+----- |
| 48 | * F(0) | P(3) F(0)| F(0) | F(0) | F(0) F(0)| F(0) | M(1) | P(3) |
| 49 | * M(1) | M(1) M(1)| F(0) | M(1) | M(1) M(1)| M(1) | M(1) | P(3) |
| 50 | * P(3) | F(0) P(3)| F(0) | M(1) | P(3) P(3)| P(3) | P(3) | P(3) |
| 51 | * |
| 52 | * neg(x) = (3 >> x) and(x,y) = (x & y) or(x,y) = (x | y) |
| 53 | * |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 54 | * For efficiency, the ACL return flags are directly mapped from the pattern |
| 55 | * match flags. See include/pattern.h for existing values. |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 56 | */ |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 57 | enum acl_test_res { |
| 58 | ACL_TEST_FAIL = 0, /* test failed */ |
| 59 | ACL_TEST_MISS = 1, /* test may pass with more info */ |
| 60 | ACL_TEST_PASS = 3, /* test passed */ |
| 61 | }; |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 62 | |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 63 | /* Condition polarity. It makes it easier for any option to choose between |
| 64 | * IF/UNLESS if it can store that information within the condition itself. |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 65 | * Those should be interpreted as "IF/UNLESS result == PASS". |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 66 | */ |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 67 | enum acl_cond_pol { |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 68 | ACL_COND_NONE, /* no polarity set yet */ |
| 69 | ACL_COND_IF, /* positive condition (after 'if') */ |
| 70 | ACL_COND_UNLESS, /* negative condition (after 'unless') */ |
| 71 | }; |
| 72 | |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 73 | /* some dummy declarations to silent the compiler */ |
| 74 | struct proxy; |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 75 | struct stream; |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 76 | |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 77 | /* |
Willy Tarreau | c92ddbc | 2012-04-27 22:10:57 +0200 | [diff] [blame] | 78 | * ACL keyword: Associates keywords with parsers, methods to retrieve the value and testers. |
| 79 | */ |
| 80 | /* |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 81 | * NOTE: |
| 82 | * The 'parse' function is called to parse words in the configuration. It must |
| 83 | * return the number of valid words read. 0 = error. The 'opaque' argument may |
| 84 | * be used by functions which need to maintain a context between consecutive |
| 85 | * values. It is initialized to zero before the first call, and passed along |
| 86 | * successive calls. |
| 87 | */ |
| 88 | |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 89 | struct acl_expr; |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 90 | struct acl_keyword { |
| 91 | const char *kw; |
Willy Tarreau | 8ed669b | 2013-01-11 15:49:37 +0100 | [diff] [blame] | 92 | char *fetch_kw; |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 93 | int match_type; /* Contain PAT_MATCH_* */ |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 94 | int (*parse)(const char *text, struct pattern *pattern, int flags, char **err); |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 95 | int (*index)(struct pattern_expr *expr, struct pattern *pattern, char **err); |
Thierry FOURNIER | 7acca4b | 2014-01-28 16:43:36 +0100 | [diff] [blame] | 96 | void (*delete)(struct pattern_expr *expr, struct pat_ref_elt *); |
Thierry FOURNIER | 6f7203d | 2014-01-14 16:24:51 +0100 | [diff] [blame] | 97 | void (*prune)(struct pattern_expr *expr); |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 98 | struct pattern *(*match)(struct sample *smp, struct pattern_expr *expr, int fill); |
Willy Tarreau | 61612d4 | 2012-04-19 18:42:05 +0200 | [diff] [blame] | 99 | /* must be after the config params */ |
Willy Tarreau | 8ed669b | 2013-01-11 15:49:37 +0100 | [diff] [blame] | 100 | struct sample_fetch *smp; /* the sample fetch we depend on */ |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 101 | }; |
| 102 | |
| 103 | /* |
| 104 | * A keyword list. It is a NULL-terminated array of keywords. It embeds a |
| 105 | * struct list in order to be linked to other lists, allowing it to easily |
| 106 | * be declared where it is needed, and linked without duplicating data nor |
| 107 | * allocating memory. |
| 108 | */ |
| 109 | struct acl_kw_list { |
| 110 | struct list list; |
| 111 | struct acl_keyword kw[VAR_ARRAY]; |
| 112 | }; |
| 113 | |
| 114 | /* |
| 115 | * Description of an ACL expression. |
Willy Tarreau | d76a98a | 2013-03-31 18:34:33 +0200 | [diff] [blame] | 116 | * 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] | 117 | * sample fetch descriptor which defaults to the keyword's, and the associated |
| 118 | * pattern matching. The structure is organized so that the hot parts are |
| 119 | * grouped together in order to optimize caching. |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 120 | */ |
| 121 | struct acl_expr { |
Thierry FOURNIER | 348971e | 2013-11-21 10:50:10 +0100 | [diff] [blame] | 122 | struct sample_expr *smp; /* the sample expression we depend on */ |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 123 | struct pattern_head pat; /* the pattern matching expression */ |
Willy Tarreau | d76a98a | 2013-03-31 18:34:33 +0200 | [diff] [blame] | 124 | struct list list; /* chaining */ |
Willy Tarreau | 93fddf1 | 2013-03-31 22:59:32 +0200 | [diff] [blame] | 125 | 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] | 126 | }; |
| 127 | |
Willy Tarreau | c92ddbc | 2012-04-27 22:10:57 +0200 | [diff] [blame] | 128 | /* 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] | 129 | struct acl { |
| 130 | struct list list; /* chaining */ |
| 131 | char *name; /* acl name */ |
| 132 | struct list expr; /* list of acl_exprs */ |
| 133 | int cache_idx; /* ACL index in cache */ |
Willy Tarreau | a91d0a5 | 2013-03-25 08:12:18 +0100 | [diff] [blame] | 134 | unsigned int use; /* or'ed bit mask of all acl_expr's SMP_USE_* */ |
| 135 | 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] | 136 | }; |
| 137 | |
| 138 | /* the condition will be linked to from an action in a proxy */ |
| 139 | struct acl_term { |
| 140 | struct list list; /* chaining */ |
| 141 | struct acl *acl; /* acl pointed to by this term */ |
| 142 | int neg; /* 1 if the ACL result must be negated */ |
| 143 | }; |
| 144 | |
| 145 | struct acl_term_suite { |
| 146 | struct list list; /* chaining of term suites */ |
| 147 | struct list terms; /* list of acl_terms */ |
| 148 | }; |
| 149 | |
| 150 | struct acl_cond { |
| 151 | struct list list; /* Some specific tests may use multiple conditions */ |
| 152 | struct list suites; /* list of acl_term_suites */ |
Willy Tarreau | 0cba607 | 2013-11-28 22:21:02 +0100 | [diff] [blame] | 153 | enum acl_cond_pol pol; /* polarity: ACL_COND_IF / ACL_COND_UNLESS */ |
Willy Tarreau | a91d0a5 | 2013-03-25 08:12:18 +0100 | [diff] [blame] | 154 | unsigned int use; /* or'ed bit mask of all suites's SMP_USE_* */ |
| 155 | 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] | 156 | const char *file; /* config file where the condition is declared */ |
Willy Tarreau | a980263 | 2008-07-25 19:13:19 +0200 | [diff] [blame] | 157 | int line; /* line in the config file where the condition is declared */ |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 158 | }; |
| 159 | |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 160 | #endif /* _TYPES_ACL_H */ |
| 161 | |
| 162 | /* |
| 163 | * Local variables: |
| 164 | * c-indent-level: 8 |
| 165 | * c-basic-offset: 8 |
| 166 | * End: |
| 167 | */ |