Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 1 | /* |
| 2 | include/types/acl.h |
| 3 | This file provides structures and types for ACLs. |
| 4 | |
| 5 | Copyright (C) 2000-2007 Willy Tarreau - w@1wt.eu |
| 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 | */ |
| 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 | |
| 29 | #include <types/proxy.h> |
| 30 | #include <types/session.h> |
| 31 | |
| 32 | |
| 33 | /* pattern matching function result */ |
| 34 | enum { |
| 35 | ACL_PAT_FAIL = 0, /* test failed */ |
| 36 | ACL_PAT_PASS = (1 << 0), /* test passed */ |
| 37 | ACL_PAT_MISS = (1 << 1), /* failed because of missing info (do not cache) */ |
| 38 | }; |
| 39 | |
| 40 | /* Condition polarity. It makes it easier for any option to choose between |
| 41 | * IF/UNLESS if it can store that information within the condition itself. |
| 42 | */ |
| 43 | enum { |
| 44 | ACL_COND_NONE, /* no polarity set yet */ |
| 45 | ACL_COND_IF, /* positive condition (after 'if') */ |
| 46 | ACL_COND_UNLESS, /* negative condition (after 'unless') */ |
| 47 | }; |
| 48 | |
| 49 | /* possible flags for intermediate test values. The flags are maintained |
| 50 | * across consecutive fetches for a same entry (eg: parse all req lines). |
| 51 | */ |
| 52 | enum { |
| 53 | ACL_TEST_F_READ_ONLY = 1 << 0, /* test data are read-only */ |
| 54 | ACL_TEST_F_MUST_FREE = 1 << 1, /* test data must be freed after end of evaluation */ |
| 55 | ACL_TEST_F_VOL_TEST = 1 << 2, /* result must not survive longer than the test (eg: time) */ |
| 56 | ACL_TEST_F_VOL_HDR = 1 << 3, /* result sensitive to changes in headers */ |
| 57 | ACL_TEST_F_VOL_1ST = 1 << 4, /* result sensitive to changes in first line (eg: URI) */ |
| 58 | ACL_TEST_F_VOL_TXN = 1 << 5, /* result sensitive to new transaction (eg: persist) */ |
| 59 | ACL_TEST_F_VOL_SESS = 1 << 6, /* result sensitive to new session (eg: IP) */ |
| 60 | ACL_TEST_F_VOLATILE = (1<<2)|(1<<3)|(1<<4)|(1<<5)|(1<<6), |
| 61 | ACL_TEST_F_FETCH_MORE = 1 << 7, /* if test does not match, retry with next entry */ |
| 62 | }; |
| 63 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 64 | /* ACLs can be evaluated on requests and on responses. */ |
| 65 | enum { |
| 66 | ACL_DIR_REQ = 0, /* ACL evaluated on request */ |
| 67 | ACL_DIR_RTR, /* ACL evaluated on response */ |
| 68 | }; |
| 69 | |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 70 | /* possible flags for expressions or patterns */ |
| 71 | enum { |
| 72 | ACL_PAT_F_IGNORE_CASE = 1 << 0, /* ignore case */ |
| 73 | ACL_PAT_F_FROM_FILE = 1 << 1, /* pattern comes from a file */ |
| 74 | }; |
| 75 | |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 76 | /* How to store a time range and the valid days in 29 bits */ |
| 77 | struct acl_time { |
| 78 | int dow:7; /* 1 bit per day of week: 0-6 */ |
| 79 | int h1:5, m1:6; /* 0..24:0..60. Use 0:0 for all day. */ |
| 80 | int h2:5, m2:6; /* 0..24:0..60. Use 24:0 for all day. */ |
| 81 | }; |
| 82 | |
| 83 | /* The acl will be linked to from the proxy where it is declared */ |
| 84 | struct acl_pattern { |
| 85 | struct list list; /* chaining */ |
| 86 | union { |
| 87 | int i; /* integer value */ |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 88 | struct { |
| 89 | signed long long min, max; |
| 90 | int min_set :1; |
| 91 | int max_set :1; |
| 92 | } range; /* integer range */ |
Willy Tarreau | a67fad9 | 2007-05-08 19:50:09 +0200 | [diff] [blame] | 93 | struct { |
| 94 | struct in_addr addr; |
| 95 | struct in_addr mask; |
| 96 | } ipv4; /* IPv4 address */ |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 97 | struct acl_time time; /* valid hours and days */ |
| 98 | } val; /* direct value */ |
| 99 | union { |
| 100 | void *ptr; /* any data */ |
| 101 | char *str; /* any string */ |
| 102 | regex_t *reg; /* a compiled regex */ |
| 103 | } ptr; /* indirect values, allocated */ |
| 104 | int len; /* data length when required */ |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 105 | int flags; /* expr or pattern flags. */ |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 106 | }; |
| 107 | |
| 108 | /* The structure exchanged between an acl_fetch_* function responsible for |
| 109 | * retrieving a value, and an acl_match_* function responsible for testing it. |
| 110 | */ |
| 111 | struct acl_test { |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 112 | int i; /* integer value */ |
| 113 | char *ptr; /* pointer to beginning of value */ |
| 114 | int len; /* length of value at ptr, otherwise ignored */ |
| 115 | int flags; /* ACL_TEST_F_* set to 0 on first call */ |
| 116 | union { /* fetch_* functions context for any purpose */ |
| 117 | void *p; /* any pointer */ |
| 118 | int i; /* any integer */ |
| 119 | long long ll; /* any long long or smaller */ |
| 120 | double d; /* any float or double */ |
| 121 | void *a[8]; /* any array of up to 8 pointers */ |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 122 | } ctx; |
| 123 | }; |
| 124 | |
| 125 | |
| 126 | /* |
| 127 | * ACL keyword: Associates keywords with parsers, methods to retrieve the value and testers. |
| 128 | */ |
| 129 | |
| 130 | /* some dummy declarations to silent the compiler */ |
| 131 | struct proxy; |
| 132 | struct session; |
| 133 | |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 134 | /* |
| 135 | * NOTE: |
| 136 | * The 'parse' function is called to parse words in the configuration. It must |
| 137 | * return the number of valid words read. 0 = error. The 'opaque' argument may |
| 138 | * be used by functions which need to maintain a context between consecutive |
| 139 | * values. It is initialized to zero before the first call, and passed along |
| 140 | * successive calls. |
| 141 | */ |
| 142 | |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 143 | struct acl_expr; |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 144 | struct acl_keyword { |
| 145 | const char *kw; |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 146 | int (*parse)(const char **text, struct acl_pattern *pattern, int *opaque); |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 147 | int (*fetch)(struct proxy *px, struct session *l4, void *l7, int dir, |
| 148 | struct acl_expr *expr, struct acl_test *test); |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 149 | int (*match)(struct acl_test *test, struct acl_pattern *pattern); |
| 150 | int use_cnt; |
| 151 | }; |
| 152 | |
| 153 | /* |
| 154 | * A keyword list. It is a NULL-terminated array of keywords. It embeds a |
| 155 | * struct list in order to be linked to other lists, allowing it to easily |
| 156 | * be declared where it is needed, and linked without duplicating data nor |
| 157 | * allocating memory. |
| 158 | */ |
| 159 | struct acl_kw_list { |
| 160 | struct list list; |
| 161 | struct acl_keyword kw[VAR_ARRAY]; |
| 162 | }; |
| 163 | |
| 164 | /* |
| 165 | * Description of an ACL expression. |
| 166 | * It contains a subject and a set of patterns to test against it. |
| 167 | * - the function get() is called to retrieve the subject from the |
| 168 | * current session or transaction and build a test. |
| 169 | * - the function test() is called to evaluate the test based on the |
| 170 | * available patterns and return ACL_PAT_* |
| 171 | * Both of those functions are available through the keyword. |
| 172 | */ |
| 173 | struct acl_expr { |
| 174 | struct list list; /* chaining */ |
| 175 | struct acl_keyword *kw; /* back-reference to the keyword */ |
| 176 | union { /* optional argument of the subject (eg: header or cookie name) */ |
| 177 | char *str; |
| 178 | } arg; |
Willy Tarreau | bb76891 | 2007-06-10 11:17:01 +0200 | [diff] [blame] | 179 | int arg_len; /* optional argument length */ |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 180 | struct list patterns; /* list of acl_patterns */ |
| 181 | }; |
| 182 | |
| 183 | struct acl { |
| 184 | struct list list; /* chaining */ |
| 185 | char *name; /* acl name */ |
| 186 | struct list expr; /* list of acl_exprs */ |
| 187 | int cache_idx; /* ACL index in cache */ |
| 188 | }; |
| 189 | |
| 190 | /* the condition will be linked to from an action in a proxy */ |
| 191 | struct acl_term { |
| 192 | struct list list; /* chaining */ |
| 193 | struct acl *acl; /* acl pointed to by this term */ |
| 194 | int neg; /* 1 if the ACL result must be negated */ |
| 195 | }; |
| 196 | |
| 197 | struct acl_term_suite { |
| 198 | struct list list; /* chaining of term suites */ |
| 199 | struct list terms; /* list of acl_terms */ |
| 200 | }; |
| 201 | |
| 202 | struct acl_cond { |
| 203 | struct list list; /* Some specific tests may use multiple conditions */ |
| 204 | struct list suites; /* list of acl_term_suites */ |
| 205 | int pol; /* polarity: ACL_COND_IF / ACL_COND_UNLESS */ |
| 206 | }; |
| 207 | |
| 208 | |
| 209 | #endif /* _TYPES_ACL_H */ |
| 210 | |
| 211 | /* |
| 212 | * Local variables: |
| 213 | * c-indent-level: 8 |
| 214 | * c-basic-offset: 8 |
| 215 | * End: |
| 216 | */ |