Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 1 | /* |
| 2 | * ACL management functions. |
| 3 | * |
| 4 | * Copyright 2000-2007 Willy Tarreau <w@1wt.eu> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 13 | #include <ctype.h> |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 14 | #include <stdio.h> |
| 15 | #include <string.h> |
| 16 | |
| 17 | #include <common/config.h> |
| 18 | #include <common/mini-clist.h> |
| 19 | #include <common/standard.h> |
| 20 | |
| 21 | #include <proto/acl.h> |
| 22 | |
| 23 | #include <types/acl.h> |
| 24 | #include <types/proxy.h> |
| 25 | #include <types/session.h> |
| 26 | |
| 27 | /* List head of all known ACL keywords */ |
| 28 | static struct acl_kw_list acl_keywords = { |
| 29 | .list = LIST_HEAD_INIT(acl_keywords.list) |
| 30 | }; |
| 31 | |
| 32 | |
Willy Tarreau | a590983 | 2007-06-17 20:40:25 +0200 | [diff] [blame] | 33 | /* |
| 34 | * These functions are only used for debugging complex configurations. |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 35 | */ |
Willy Tarreau | a590983 | 2007-06-17 20:40:25 +0200 | [diff] [blame] | 36 | |
| 37 | /* ignore the current line */ |
| 38 | static int |
| 39 | acl_parse_nothing(const char **text, struct acl_pattern *pattern, int *opaque) |
| 40 | { |
| 41 | return 1; |
| 42 | } |
| 43 | |
| 44 | /* always fake a data retrieval */ |
| 45 | static int |
| 46 | acl_fetch_nothing(struct proxy *px, struct session *l4, void *l7, int dir, |
| 47 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 48 | { |
| 49 | return 1; |
| 50 | } |
| 51 | |
Willy Tarreau | a590983 | 2007-06-17 20:40:25 +0200 | [diff] [blame] | 52 | /* always return true */ |
| 53 | static int |
| 54 | acl_match_true(struct acl_test *test, struct acl_pattern *pattern) |
| 55 | { |
| 56 | return 1; |
| 57 | } |
| 58 | |
| 59 | /* always return false */ |
| 60 | static int |
| 61 | acl_match_false(struct acl_test *test, struct acl_pattern *pattern) |
| 62 | { |
| 63 | return 0; |
| 64 | } |
| 65 | |
| 66 | |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 67 | /* NB: For two strings to be identical, it is required that their lengths match */ |
| 68 | int acl_match_str(struct acl_test *test, struct acl_pattern *pattern) |
| 69 | { |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 70 | int icase; |
| 71 | |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 72 | if (pattern->len != test->len) |
| 73 | return 0; |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 74 | |
| 75 | icase = pattern->flags & ACL_PAT_F_IGNORE_CASE; |
| 76 | if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) == 0) || |
| 77 | (!icase && strncmp(pattern->ptr.str, test->ptr, test->len) == 0)) |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 78 | return 1; |
| 79 | return 0; |
| 80 | } |
| 81 | |
Willy Tarreau | f3d2598 | 2007-05-08 22:45:09 +0200 | [diff] [blame] | 82 | /* Executes a regex. It needs to change the data. If it is marked READ_ONLY |
| 83 | * then it will be allocated and duplicated in place so that others may use |
| 84 | * it later on. Note that this is embarrassing because we always try to avoid |
| 85 | * allocating memory at run time. |
| 86 | */ |
| 87 | int acl_match_reg(struct acl_test *test, struct acl_pattern *pattern) |
| 88 | { |
| 89 | char old_char; |
| 90 | int ret; |
| 91 | |
| 92 | if (unlikely(test->flags & ACL_TEST_F_READ_ONLY)) { |
| 93 | char *new_str; |
| 94 | |
| 95 | new_str = calloc(1, test->len + 1); |
| 96 | if (!new_str) |
| 97 | return 0; |
| 98 | |
| 99 | memcpy(new_str, test->ptr, test->len); |
| 100 | new_str[test->len] = 0; |
| 101 | if (test->flags & ACL_TEST_F_MUST_FREE) |
| 102 | free(test->ptr); |
| 103 | test->ptr = new_str; |
| 104 | test->flags |= ACL_TEST_F_MUST_FREE; |
| 105 | test->flags &= ~ACL_TEST_F_READ_ONLY; |
| 106 | } |
| 107 | |
| 108 | old_char = test->ptr[test->len]; |
| 109 | test->ptr[test->len] = 0; |
| 110 | |
| 111 | if (regexec(pattern->ptr.reg, test->ptr, 0, NULL, 0) == 0) |
| 112 | ret = 1; |
| 113 | else |
| 114 | ret = 0; |
| 115 | |
| 116 | test->ptr[test->len] = old_char; |
| 117 | return ret; |
| 118 | } |
| 119 | |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 120 | /* Checks that the pattern matches the beginning of the tested string. */ |
| 121 | int acl_match_beg(struct acl_test *test, struct acl_pattern *pattern) |
| 122 | { |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 123 | int icase; |
| 124 | |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 125 | if (pattern->len > test->len) |
| 126 | return 0; |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 127 | |
| 128 | icase = pattern->flags & ACL_PAT_F_IGNORE_CASE; |
| 129 | if ((icase && strncasecmp(pattern->ptr.str, test->ptr, pattern->len) != 0) || |
| 130 | (!icase && strncmp(pattern->ptr.str, test->ptr, pattern->len) != 0)) |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 131 | return 0; |
| 132 | return 1; |
| 133 | } |
| 134 | |
| 135 | /* Checks that the pattern matches the end of the tested string. */ |
| 136 | int acl_match_end(struct acl_test *test, struct acl_pattern *pattern) |
| 137 | { |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 138 | int icase; |
| 139 | |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 140 | if (pattern->len > test->len) |
| 141 | return 0; |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 142 | icase = pattern->flags & ACL_PAT_F_IGNORE_CASE; |
| 143 | if ((icase && strncasecmp(pattern->ptr.str, test->ptr + test->len - pattern->len, pattern->len) != 0) || |
| 144 | (!icase && strncmp(pattern->ptr.str, test->ptr + test->len - pattern->len, pattern->len) != 0)) |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 145 | return 0; |
| 146 | return 1; |
| 147 | } |
| 148 | |
| 149 | /* Checks that the pattern is included inside the tested string. |
| 150 | * NB: Suboptimal, should be rewritten using a Boyer-Moore method. |
| 151 | */ |
| 152 | int acl_match_sub(struct acl_test *test, struct acl_pattern *pattern) |
| 153 | { |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 154 | int icase; |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 155 | char *end; |
| 156 | char *c; |
| 157 | |
| 158 | if (pattern->len > test->len) |
| 159 | return 0; |
| 160 | |
| 161 | end = test->ptr + test->len - pattern->len; |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 162 | icase = pattern->flags & ACL_PAT_F_IGNORE_CASE; |
| 163 | if (icase) { |
| 164 | for (c = test->ptr; c <= end; c++) { |
| 165 | if (tolower(*c) != tolower(*pattern->ptr.str)) |
| 166 | continue; |
| 167 | if (strncasecmp(pattern->ptr.str, c, pattern->len) == 0) |
| 168 | return 1; |
| 169 | } |
| 170 | } else { |
| 171 | for (c = test->ptr; c <= end; c++) { |
| 172 | if (*c != *pattern->ptr.str) |
| 173 | continue; |
| 174 | if (strncmp(pattern->ptr.str, c, pattern->len) == 0) |
| 175 | return 1; |
| 176 | } |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 177 | } |
| 178 | return 0; |
| 179 | } |
| 180 | |
| 181 | /* This one is used by other real functions. It checks that the pattern is |
| 182 | * included inside the tested string, but enclosed between the specified |
| 183 | * delimitor, or a '/' or a '?' or at the beginning or end of the string. |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 184 | * The delimitor is stripped at the beginning or end of the pattern. |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 185 | */ |
| 186 | static int match_word(struct acl_test *test, struct acl_pattern *pattern, char delim) |
| 187 | { |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 188 | int may_match, icase; |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 189 | char *c, *end; |
| 190 | char *ps; |
| 191 | int pl; |
| 192 | |
| 193 | pl = pattern->len; |
| 194 | ps = pattern->ptr.str; |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 195 | while (pl > 0 && (*ps == delim || *ps == '/' || *ps == '?')) { |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 196 | pl--; |
| 197 | ps++; |
| 198 | } |
| 199 | |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 200 | while (pl > 0 && |
| 201 | (ps[pl - 1] == delim || ps[pl - 1] == '/' || ps[pl - 1] == '?')) |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 202 | pl--; |
| 203 | |
| 204 | if (pl > test->len) |
| 205 | return 0; |
| 206 | |
| 207 | may_match = 1; |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 208 | icase = pattern->flags & ACL_PAT_F_IGNORE_CASE; |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 209 | end = test->ptr + test->len - pl; |
| 210 | for (c = test->ptr; c <= end; c++) { |
| 211 | if (*c == '/' || *c == delim || *c == '?') { |
| 212 | may_match = 1; |
| 213 | continue; |
| 214 | } |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 215 | |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 216 | if (!may_match) |
| 217 | continue; |
| 218 | |
| 219 | if (icase) { |
| 220 | if ((tolower(*c) == tolower(*ps)) && |
| 221 | (strncasecmp(ps, c, pl) == 0) && |
| 222 | (c == end || c[pl] == '/' || c[pl] == delim || c[pl] == '?')) |
| 223 | return 1; |
| 224 | } else { |
| 225 | if ((*c == *ps) && |
| 226 | (strncmp(ps, c, pl) == 0) && |
| 227 | (c == end || c[pl] == '/' || c[pl] == delim || c[pl] == '?')) |
| 228 | return 1; |
| 229 | } |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 230 | may_match = 0; |
| 231 | } |
| 232 | return 0; |
| 233 | } |
| 234 | |
| 235 | /* Checks that the pattern is included inside the tested string, but enclosed |
| 236 | * between slashes or at the beginning or end of the string. Slashes at the |
| 237 | * beginning or end of the pattern are ignored. |
| 238 | */ |
| 239 | int acl_match_dir(struct acl_test *test, struct acl_pattern *pattern) |
| 240 | { |
| 241 | return match_word(test, pattern, '/'); |
| 242 | } |
| 243 | |
| 244 | /* Checks that the pattern is included inside the tested string, but enclosed |
| 245 | * between dots or at the beginning or end of the string. Dots at the beginning |
| 246 | * or end of the pattern are ignored. |
| 247 | */ |
| 248 | int acl_match_dom(struct acl_test *test, struct acl_pattern *pattern) |
| 249 | { |
| 250 | return match_word(test, pattern, '.'); |
| 251 | } |
| 252 | |
| 253 | /* Checks that the integer in <test> is included between min and max */ |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 254 | int acl_match_int(struct acl_test *test, struct acl_pattern *pattern) |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 255 | { |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 256 | if ((!pattern->val.range.min_set || pattern->val.range.min <= test->i) && |
| 257 | (!pattern->val.range.max_set || test->i <= pattern->val.range.max)) |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 258 | return 1; |
| 259 | return 0; |
| 260 | } |
| 261 | |
Willy Tarreau | a67fad9 | 2007-05-08 19:50:09 +0200 | [diff] [blame] | 262 | int acl_match_ip(struct acl_test *test, struct acl_pattern *pattern) |
| 263 | { |
| 264 | struct in_addr *s; |
| 265 | |
| 266 | if (test->i != AF_INET) |
| 267 | return 0; |
| 268 | |
| 269 | s = (void *)test->ptr; |
| 270 | if (((s->s_addr ^ pattern->val.ipv4.addr.s_addr) & pattern->val.ipv4.mask.s_addr) == 0) |
| 271 | return 1; |
| 272 | return 0; |
| 273 | } |
| 274 | |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 275 | /* Parse a string. It is allocated and duplicated. */ |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 276 | int acl_parse_str(const char **text, struct acl_pattern *pattern, int *opaque) |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 277 | { |
| 278 | int len; |
| 279 | |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 280 | len = strlen(*text); |
| 281 | pattern->ptr.str = strdup(*text); |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 282 | if (!pattern->ptr.str) |
| 283 | return 0; |
| 284 | pattern->len = len; |
| 285 | return 1; |
| 286 | } |
| 287 | |
Willy Tarreau | f3d2598 | 2007-05-08 22:45:09 +0200 | [diff] [blame] | 288 | /* Parse a regex. It is allocated. */ |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 289 | int acl_parse_reg(const char **text, struct acl_pattern *pattern, int *opaque) |
Willy Tarreau | f3d2598 | 2007-05-08 22:45:09 +0200 | [diff] [blame] | 290 | { |
| 291 | regex_t *preg; |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 292 | int icase; |
Willy Tarreau | f3d2598 | 2007-05-08 22:45:09 +0200 | [diff] [blame] | 293 | |
| 294 | preg = calloc(1, sizeof(regex_t)); |
| 295 | |
| 296 | if (!preg) |
| 297 | return 0; |
| 298 | |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 299 | icase = (pattern->flags & ACL_PAT_F_IGNORE_CASE) ? REG_ICASE : 0; |
| 300 | if (regcomp(preg, *text, REG_EXTENDED | REG_NOSUB | icase) != 0) { |
Willy Tarreau | f3d2598 | 2007-05-08 22:45:09 +0200 | [diff] [blame] | 301 | free(preg); |
| 302 | return 0; |
| 303 | } |
| 304 | |
| 305 | pattern->ptr.reg = preg; |
| 306 | return 1; |
| 307 | } |
| 308 | |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 309 | /* Parse a range of positive integers delimited by either ':' or '-'. If only |
| 310 | * one integer is read, it is set as both min and max. An operator may be |
| 311 | * specified as the prefix, among this list of 5 : |
| 312 | * |
| 313 | * 0:eq, 1:gt, 2:ge, 3:lt, 4:le |
| 314 | * |
| 315 | * The default operator is "eq". It supports range matching. Ranges are |
| 316 | * rejected for other operators. The operator may be changed at any time. |
| 317 | * The operator is stored in the 'opaque' argument. |
| 318 | * |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 319 | */ |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 320 | int acl_parse_int(const char **text, struct acl_pattern *pattern, int *opaque) |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 321 | { |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 322 | signed long long i; |
| 323 | unsigned int j, last, skip = 0; |
| 324 | const char *ptr = *text; |
| 325 | |
| 326 | |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 327 | while (!isdigit((unsigned char)*ptr)) { |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 328 | if (strcmp(ptr, "eq") == 0) *opaque = 0; |
| 329 | else if (strcmp(ptr, "gt") == 0) *opaque = 1; |
| 330 | else if (strcmp(ptr, "ge") == 0) *opaque = 2; |
| 331 | else if (strcmp(ptr, "lt") == 0) *opaque = 3; |
| 332 | else if (strcmp(ptr, "le") == 0) *opaque = 4; |
| 333 | else |
| 334 | return 0; |
| 335 | |
| 336 | skip++; |
| 337 | ptr = text[skip]; |
| 338 | } |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 339 | |
| 340 | last = i = 0; |
| 341 | while (1) { |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 342 | j = *ptr++; |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 343 | if ((j == '-' || j == ':') && !last) { |
| 344 | last++; |
| 345 | pattern->val.range.min = i; |
| 346 | i = 0; |
| 347 | continue; |
| 348 | } |
| 349 | j -= '0'; |
| 350 | if (j > 9) |
| 351 | // also catches the terminating zero |
| 352 | break; |
| 353 | i *= 10; |
| 354 | i += j; |
| 355 | } |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 356 | |
| 357 | if (last && *opaque >= 1 && *opaque <= 4) |
| 358 | /* having a range with a min or a max is absurd */ |
| 359 | return 0; |
| 360 | |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 361 | if (!last) |
| 362 | pattern->val.range.min = i; |
| 363 | pattern->val.range.max = i; |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 364 | |
| 365 | switch (*opaque) { |
| 366 | case 0: /* eq */ |
| 367 | pattern->val.range.min_set = 1; |
| 368 | pattern->val.range.max_set = 1; |
| 369 | break; |
| 370 | case 1: /* gt */ |
| 371 | pattern->val.range.min++; /* gt = ge + 1 */ |
| 372 | case 2: /* ge */ |
| 373 | pattern->val.range.min_set = 1; |
| 374 | pattern->val.range.max_set = 0; |
| 375 | break; |
| 376 | case 3: /* lt */ |
| 377 | pattern->val.range.max--; /* lt = le - 1 */ |
| 378 | case 4: /* le */ |
| 379 | pattern->val.range.min_set = 0; |
| 380 | pattern->val.range.max_set = 1; |
| 381 | break; |
| 382 | } |
| 383 | return skip + 1; |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 384 | } |
| 385 | |
Willy Tarreau | a67fad9 | 2007-05-08 19:50:09 +0200 | [diff] [blame] | 386 | /* Parse an IP address and an optional mask in the form addr[/mask]. |
| 387 | * The addr may either be an IPv4 address or a hostname. The mask |
| 388 | * may either be a dotted mask or a number of bits. Returns 1 if OK, |
| 389 | * otherwise 0. |
| 390 | */ |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 391 | int acl_parse_ip(const char **text, struct acl_pattern *pattern, int *opaque) |
Willy Tarreau | a67fad9 | 2007-05-08 19:50:09 +0200 | [diff] [blame] | 392 | { |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 393 | if (str2net(*text, &pattern->val.ipv4.addr, &pattern->val.ipv4.mask)) |
| 394 | return 1; |
| 395 | else |
| 396 | return 0; |
Willy Tarreau | a67fad9 | 2007-05-08 19:50:09 +0200 | [diff] [blame] | 397 | } |
| 398 | |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 399 | /* |
| 400 | * Registers the ACL keyword list <kwl> as a list of valid keywords for next |
| 401 | * parsing sessions. |
| 402 | */ |
| 403 | void acl_register_keywords(struct acl_kw_list *kwl) |
| 404 | { |
| 405 | LIST_ADDQ(&acl_keywords.list, &kwl->list); |
| 406 | } |
| 407 | |
| 408 | /* |
| 409 | * Unregisters the ACL keyword list <kwl> from the list of valid keywords. |
| 410 | */ |
| 411 | void acl_unregister_keywords(struct acl_kw_list *kwl) |
| 412 | { |
| 413 | LIST_DEL(&kwl->list); |
| 414 | LIST_INIT(&kwl->list); |
| 415 | } |
| 416 | |
| 417 | /* Return a pointer to the ACL <name> within the list starting at <head>, or |
| 418 | * NULL if not found. |
| 419 | */ |
| 420 | struct acl *find_acl_by_name(const char *name, struct list *head) |
| 421 | { |
| 422 | struct acl *acl; |
| 423 | list_for_each_entry(acl, head, list) { |
| 424 | if (strcmp(acl->name, name) == 0) |
| 425 | return acl; |
| 426 | } |
| 427 | return NULL; |
| 428 | } |
| 429 | |
| 430 | /* Return a pointer to the ACL keyword <kw>, or NULL if not found. Note that if |
| 431 | * <kw> contains an opening parenthesis, only the left part of it is checked. |
| 432 | */ |
| 433 | struct acl_keyword *find_acl_kw(const char *kw) |
| 434 | { |
| 435 | int index; |
| 436 | const char *kwend; |
| 437 | struct acl_kw_list *kwl; |
| 438 | |
| 439 | kwend = strchr(kw, '('); |
| 440 | if (!kwend) |
| 441 | kwend = kw + strlen(kw); |
| 442 | |
| 443 | list_for_each_entry(kwl, &acl_keywords.list, list) { |
| 444 | for (index = 0; kwl->kw[index].kw != NULL; index++) { |
| 445 | if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) && |
| 446 | kwl->kw[index].kw[kwend-kw] == 0) |
| 447 | return &kwl->kw[index]; |
| 448 | } |
| 449 | } |
| 450 | return NULL; |
| 451 | } |
| 452 | |
| 453 | static void free_pattern(struct acl_pattern *pat) |
| 454 | { |
| 455 | if (pat->ptr.ptr) |
| 456 | free(pat->ptr.ptr); |
| 457 | free(pat); |
| 458 | } |
| 459 | |
| 460 | static void free_pattern_list(struct list *head) |
| 461 | { |
| 462 | struct acl_pattern *pat, *tmp; |
| 463 | list_for_each_entry_safe(pat, tmp, head, list) |
| 464 | free_pattern(pat); |
| 465 | } |
| 466 | |
| 467 | static struct acl_expr *prune_acl_expr(struct acl_expr *expr) |
| 468 | { |
| 469 | free_pattern_list(&expr->patterns); |
| 470 | LIST_INIT(&expr->patterns); |
| 471 | if (expr->arg.str) |
| 472 | free(expr->arg.str); |
| 473 | expr->kw->use_cnt--; |
| 474 | return expr; |
| 475 | } |
| 476 | |
| 477 | /* Parse an ACL expression starting at <args>[0], and return it. |
| 478 | * Right now, the only accepted syntax is : |
| 479 | * <subject> [<value>...] |
| 480 | */ |
| 481 | struct acl_expr *parse_acl_expr(const char **args) |
| 482 | { |
| 483 | __label__ out_return, out_free_expr, out_free_pattern; |
| 484 | struct acl_expr *expr; |
| 485 | struct acl_keyword *aclkw; |
| 486 | struct acl_pattern *pattern; |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 487 | int opaque, patflags; |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 488 | const char *arg; |
| 489 | |
| 490 | aclkw = find_acl_kw(args[0]); |
| 491 | if (!aclkw || !aclkw->parse) |
| 492 | goto out_return; |
| 493 | |
| 494 | expr = (struct acl_expr *)calloc(1, sizeof(*expr)); |
| 495 | if (!expr) |
| 496 | goto out_return; |
| 497 | |
| 498 | expr->kw = aclkw; |
| 499 | aclkw->use_cnt++; |
| 500 | LIST_INIT(&expr->patterns); |
| 501 | expr->arg.str = NULL; |
Willy Tarreau | bb76891 | 2007-06-10 11:17:01 +0200 | [diff] [blame] | 502 | expr->arg_len = 0; |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 503 | |
| 504 | arg = strchr(args[0], '('); |
| 505 | if (arg != NULL) { |
| 506 | char *end, *arg2; |
| 507 | /* there is an argument in the form "subject(arg)" */ |
| 508 | arg++; |
| 509 | end = strchr(arg, ')'); |
| 510 | if (!end) |
| 511 | goto out_free_expr; |
| 512 | arg2 = (char *)calloc(1, end - arg + 1); |
| 513 | if (!arg2) |
| 514 | goto out_free_expr; |
| 515 | memcpy(arg2, arg, end - arg); |
| 516 | arg2[end-arg] = '\0'; |
Willy Tarreau | bb76891 | 2007-06-10 11:17:01 +0200 | [diff] [blame] | 517 | expr->arg_len = end - arg; |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 518 | expr->arg.str = arg2; |
| 519 | } |
| 520 | |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 521 | args++; |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 522 | |
| 523 | /* check for options before patterns. Supported options are : |
| 524 | * -i : ignore case for all patterns by default |
| 525 | * -f : read patterns from those files |
| 526 | * -- : everything after this is not an option |
| 527 | */ |
| 528 | patflags = 0; |
| 529 | while (**args == '-') { |
| 530 | if ((*args)[1] == 'i') |
| 531 | patflags |= ACL_PAT_F_IGNORE_CASE; |
| 532 | else if ((*args)[1] == 'f') |
| 533 | patflags |= ACL_PAT_F_FROM_FILE; |
| 534 | else if ((*args)[1] == '-') { |
| 535 | args++; |
| 536 | break; |
| 537 | } |
| 538 | else |
| 539 | break; |
| 540 | args++; |
| 541 | } |
| 542 | |
| 543 | /* now parse all patterns */ |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 544 | opaque = 0; |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 545 | while (**args) { |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 546 | int ret; |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 547 | pattern = (struct acl_pattern *)calloc(1, sizeof(*pattern)); |
| 548 | if (!pattern) |
| 549 | goto out_free_expr; |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 550 | pattern->flags = patflags; |
| 551 | |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 552 | ret = aclkw->parse(args, pattern, &opaque); |
| 553 | if (!ret) |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 554 | goto out_free_pattern; |
| 555 | LIST_ADDQ(&expr->patterns, &pattern->list); |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 556 | args += ret; |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 557 | } |
| 558 | |
| 559 | return expr; |
| 560 | |
| 561 | out_free_pattern: |
| 562 | free_pattern(pattern); |
| 563 | out_free_expr: |
| 564 | prune_acl_expr(expr); |
| 565 | free(expr); |
| 566 | out_return: |
| 567 | return NULL; |
| 568 | } |
| 569 | |
| 570 | /* Parse an ACL with the name starting at <args>[0], and with a list of already |
| 571 | * known ACLs in <acl>. If the ACL was not in the list, it will be added. |
| 572 | * A pointer to that ACL is returned. |
| 573 | * |
| 574 | * args syntax: <aclname> <acl_expr> |
| 575 | */ |
| 576 | struct acl *parse_acl(const char **args, struct list *known_acl) |
| 577 | { |
| 578 | __label__ out_return, out_free_acl_expr, out_free_name; |
| 579 | struct acl *cur_acl; |
| 580 | struct acl_expr *acl_expr; |
| 581 | char *name; |
| 582 | |
Willy Tarreau | 2e74c3f | 2007-12-02 18:45:09 +0100 | [diff] [blame] | 583 | if (invalid_char(*args)) |
| 584 | goto out_return; |
| 585 | |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 586 | acl_expr = parse_acl_expr(args + 1); |
| 587 | if (!acl_expr) |
| 588 | goto out_return; |
| 589 | |
| 590 | cur_acl = find_acl_by_name(args[0], known_acl); |
| 591 | if (!cur_acl) { |
| 592 | name = strdup(args[0]); |
| 593 | if (!name) |
| 594 | goto out_free_acl_expr; |
| 595 | cur_acl = (struct acl *)calloc(1, sizeof(*cur_acl)); |
| 596 | if (cur_acl == NULL) |
| 597 | goto out_free_name; |
| 598 | |
| 599 | LIST_INIT(&cur_acl->expr); |
| 600 | LIST_ADDQ(known_acl, &cur_acl->list); |
| 601 | cur_acl->name = name; |
| 602 | } |
| 603 | |
| 604 | LIST_ADDQ(&cur_acl->expr, &acl_expr->list); |
| 605 | return cur_acl; |
| 606 | |
| 607 | out_free_name: |
| 608 | free(name); |
| 609 | out_free_acl_expr: |
| 610 | prune_acl_expr(acl_expr); |
| 611 | free(acl_expr); |
| 612 | out_return: |
| 613 | return NULL; |
| 614 | } |
| 615 | |
Willy Tarreau | 16fbe82 | 2007-06-17 11:54:31 +0200 | [diff] [blame] | 616 | /* Some useful ACLs provided by default. Only those used are allocated. */ |
| 617 | |
| 618 | const struct { |
| 619 | const char *name; |
| 620 | const char *expr[4]; /* put enough for longest expression */ |
| 621 | } default_acl_list[] = { |
Willy Tarreau | a590983 | 2007-06-17 20:40:25 +0200 | [diff] [blame] | 622 | { .name = "TRUE", .expr = {"always_true","1",""}}, |
| 623 | { .name = "FALSE", .expr = {"always_false","0",""}}, |
Willy Tarreau | 16fbe82 | 2007-06-17 11:54:31 +0200 | [diff] [blame] | 624 | { .name = "LOCALHOST", .expr = {"src","127.0.0.1/8",""}}, |
| 625 | { .name = "HTTP_1.0", .expr = {"req_ver","1.0",""}}, |
| 626 | { .name = "HTTP_1.1", .expr = {"req_ver","1.1",""}}, |
| 627 | { .name = "METH_CONNECT", .expr = {"method","CONNECT",""}}, |
| 628 | { .name = "METH_GET", .expr = {"method","GET","HEAD",""}}, |
| 629 | { .name = "METH_HEAD", .expr = {"method","HEAD",""}}, |
| 630 | { .name = "METH_OPTIONS", .expr = {"method","OPTIONS",""}}, |
| 631 | { .name = "METH_POST", .expr = {"method","POST",""}}, |
| 632 | { .name = "METH_TRACE", .expr = {"method","TRACE",""}}, |
| 633 | { .name = "HTTP_URL_ABS", .expr = {"url_reg","^[^/:]*://",""}}, |
| 634 | { .name = "HTTP_URL_SLASH", .expr = {"url_beg","/",""}}, |
| 635 | { .name = "HTTP_URL_STAR", .expr = {"url","*",""}}, |
| 636 | { .name = "HTTP_CONTENT", .expr = {"hdr_val(content-length)","gt","0",""}}, |
| 637 | { .name = NULL, .expr = {""}} |
| 638 | }; |
| 639 | |
| 640 | /* Find a default ACL from the default_acl list, compile it and return it. |
| 641 | * If the ACL is not found, NULL is returned. In theory, it cannot fail, |
| 642 | * except when default ACLs are broken, in which case it will return NULL. |
| 643 | * If <known_acl> is not NULL, the ACL will be queued at its tail. |
| 644 | */ |
| 645 | struct acl *find_acl_default(const char *acl_name, struct list *known_acl) |
| 646 | { |
| 647 | __label__ out_return, out_free_acl_expr, out_free_name; |
| 648 | struct acl *cur_acl; |
| 649 | struct acl_expr *acl_expr; |
| 650 | char *name; |
| 651 | int index; |
| 652 | |
| 653 | for (index = 0; default_acl_list[index].name != NULL; index++) { |
| 654 | if (strcmp(acl_name, default_acl_list[index].name) == 0) |
| 655 | break; |
| 656 | } |
| 657 | |
| 658 | if (default_acl_list[index].name == NULL) |
| 659 | return NULL; |
| 660 | |
| 661 | acl_expr = parse_acl_expr((const char **)default_acl_list[index].expr); |
| 662 | if (!acl_expr) |
| 663 | goto out_return; |
| 664 | |
| 665 | name = strdup(acl_name); |
| 666 | if (!name) |
| 667 | goto out_free_acl_expr; |
| 668 | cur_acl = (struct acl *)calloc(1, sizeof(*cur_acl)); |
| 669 | if (cur_acl == NULL) |
| 670 | goto out_free_name; |
| 671 | |
| 672 | cur_acl->name = name; |
| 673 | LIST_INIT(&cur_acl->expr); |
| 674 | LIST_ADDQ(&cur_acl->expr, &acl_expr->list); |
| 675 | if (known_acl) |
| 676 | LIST_ADDQ(known_acl, &cur_acl->list); |
| 677 | |
| 678 | return cur_acl; |
| 679 | |
| 680 | out_free_name: |
| 681 | free(name); |
| 682 | out_free_acl_expr: |
| 683 | prune_acl_expr(acl_expr); |
| 684 | free(acl_expr); |
| 685 | out_return: |
| 686 | return NULL; |
| 687 | } |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 688 | |
| 689 | /* Purge everything in the acl_cond <cond>, then return <cond>. */ |
| 690 | struct acl_cond *prune_acl_cond(struct acl_cond *cond) |
| 691 | { |
| 692 | struct acl_term_suite *suite, *tmp_suite; |
| 693 | struct acl_term *term, *tmp_term; |
| 694 | |
| 695 | /* iterate through all term suites and free all terms and all suites */ |
| 696 | list_for_each_entry_safe(suite, tmp_suite, &cond->suites, list) { |
| 697 | list_for_each_entry_safe(term, tmp_term, &suite->terms, list) |
| 698 | free(term); |
| 699 | free(suite); |
| 700 | } |
| 701 | return cond; |
| 702 | } |
| 703 | |
| 704 | /* Parse an ACL condition starting at <args>[0], relying on a list of already |
| 705 | * known ACLs passed in <known_acl>. The new condition is returned (or NULL in |
| 706 | * case of low memory). Supports multiple conditions separated by "or". |
| 707 | */ |
| 708 | struct acl_cond *parse_acl_cond(const char **args, struct list *known_acl, int pol) |
| 709 | { |
| 710 | __label__ out_return, out_free_suite, out_free_term; |
Willy Tarreau | 74b98a8 | 2007-06-16 19:35:18 +0200 | [diff] [blame] | 711 | int arg, neg; |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 712 | const char *word; |
| 713 | struct acl *cur_acl; |
| 714 | struct acl_term *cur_term; |
| 715 | struct acl_term_suite *cur_suite; |
| 716 | struct acl_cond *cond; |
| 717 | |
| 718 | cond = (struct acl_cond *)calloc(1, sizeof(*cond)); |
| 719 | if (cond == NULL) |
| 720 | goto out_return; |
| 721 | |
| 722 | LIST_INIT(&cond->list); |
| 723 | LIST_INIT(&cond->suites); |
| 724 | cond->pol = pol; |
| 725 | |
| 726 | cur_suite = NULL; |
Willy Tarreau | 74b98a8 | 2007-06-16 19:35:18 +0200 | [diff] [blame] | 727 | neg = 0; |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 728 | for (arg = 0; *args[arg]; arg++) { |
| 729 | word = args[arg]; |
| 730 | |
| 731 | /* remove as many exclamation marks as we can */ |
| 732 | while (*word == '!') { |
| 733 | neg = !neg; |
| 734 | word++; |
| 735 | } |
| 736 | |
| 737 | /* an empty word is allowed because we cannot force the user to |
| 738 | * always think about not leaving exclamation marks alone. |
| 739 | */ |
| 740 | if (!*word) |
| 741 | continue; |
| 742 | |
Willy Tarreau | 16fbe82 | 2007-06-17 11:54:31 +0200 | [diff] [blame] | 743 | if (strcasecmp(word, "or") == 0 || strcmp(word, "||") == 0) { |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 744 | /* new term suite */ |
| 745 | cur_suite = NULL; |
| 746 | neg = 0; |
| 747 | continue; |
| 748 | } |
| 749 | |
Willy Tarreau | 16fbe82 | 2007-06-17 11:54:31 +0200 | [diff] [blame] | 750 | /* search for <word> in the known ACL names. If we do not find |
| 751 | * it, let's look for it in the default ACLs, and if found, add |
| 752 | * it to the list of ACLs of this proxy. This makes it possible |
| 753 | * to override them. |
| 754 | */ |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 755 | cur_acl = find_acl_by_name(word, known_acl); |
Willy Tarreau | 16fbe82 | 2007-06-17 11:54:31 +0200 | [diff] [blame] | 756 | if (cur_acl == NULL) { |
| 757 | cur_acl = find_acl_default(word, known_acl); |
| 758 | if (cur_acl == NULL) |
| 759 | goto out_free_suite; |
| 760 | } |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 761 | |
| 762 | cur_term = (struct acl_term *)calloc(1, sizeof(*cur_term)); |
| 763 | if (cur_term == NULL) |
| 764 | goto out_free_suite; |
| 765 | |
| 766 | cur_term->acl = cur_acl; |
| 767 | cur_term->neg = neg; |
| 768 | |
| 769 | if (!cur_suite) { |
| 770 | cur_suite = (struct acl_term_suite *)calloc(1, sizeof(*cur_suite)); |
| 771 | if (cur_term == NULL) |
| 772 | goto out_free_term; |
| 773 | LIST_INIT(&cur_suite->terms); |
| 774 | LIST_ADDQ(&cond->suites, &cur_suite->list); |
| 775 | } |
| 776 | LIST_ADDQ(&cur_suite->terms, &cur_term->list); |
Willy Tarreau | 74b98a8 | 2007-06-16 19:35:18 +0200 | [diff] [blame] | 777 | neg = 0; |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 778 | } |
| 779 | |
| 780 | return cond; |
| 781 | |
| 782 | out_free_term: |
| 783 | free(cur_term); |
| 784 | out_free_suite: |
| 785 | prune_acl_cond(cond); |
| 786 | free(cond); |
| 787 | out_return: |
| 788 | return NULL; |
| 789 | } |
| 790 | |
| 791 | /* Execute condition <cond> and return 0 if test fails or 1 if test succeeds. |
| 792 | * This function only computes the condition, it does not apply the polarity |
| 793 | * required by IF/UNLESS, it's up to the caller to do this. |
| 794 | */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 795 | int acl_exec_cond(struct acl_cond *cond, struct proxy *px, struct session *l4, void *l7, int dir) |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 796 | { |
| 797 | __label__ fetch_next; |
| 798 | struct acl_term_suite *suite; |
| 799 | struct acl_term *term; |
| 800 | struct acl_expr *expr; |
| 801 | struct acl *acl; |
| 802 | struct acl_pattern *pattern; |
| 803 | struct acl_test test; |
| 804 | int acl_res, pat_res, suite_res, cond_res; |
| 805 | |
| 806 | /* we're doing a logical OR between conditions so we initialize to FAIL */ |
| 807 | cond_res = ACL_PAT_FAIL; |
| 808 | list_for_each_entry(suite, &cond->suites, list) { |
| 809 | /* evaluate condition suite <suite>. We stop at the first term |
| 810 | * which does not return ACL_PAT_PASS. |
| 811 | */ |
| 812 | |
| 813 | /* we're doing a logical AND between terms, so we must set the |
| 814 | * initial value to PASS. |
| 815 | */ |
| 816 | suite_res = ACL_PAT_PASS; |
| 817 | list_for_each_entry(term, &suite->terms, list) { |
| 818 | acl = term->acl; |
| 819 | |
| 820 | /* FIXME: use cache ! |
| 821 | * check acl->cache_idx for this. |
| 822 | */ |
| 823 | |
| 824 | /* ACL result not cached. Let's scan all the expressions |
| 825 | * and use the first one to match. |
| 826 | */ |
| 827 | acl_res = ACL_PAT_FAIL; |
| 828 | list_for_each_entry(expr, &acl->expr, list) { |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 829 | /* we need to reset context and flags */ |
| 830 | memset(&test, 0, sizeof(test)); |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 831 | fetch_next: |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 832 | if (!expr->kw->fetch(px, l4, l7, dir, expr, &test)) |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 833 | continue; |
| 834 | |
| 835 | /* apply all tests to this value */ |
| 836 | list_for_each_entry(pattern, &expr->patterns, list) { |
| 837 | pat_res = expr->kw->match(&test, pattern); |
| 838 | |
| 839 | if (pat_res & ACL_PAT_MISS) { |
| 840 | /* there is at least one test which might be worth retrying later. */ |
| 841 | acl_res |= ACL_PAT_MISS; |
| 842 | continue; |
| 843 | } else if (pat_res & ACL_PAT_PASS) { |
| 844 | /* we found one ! */ |
| 845 | acl_res |= ACL_PAT_PASS; |
| 846 | break; |
| 847 | } |
| 848 | } |
| 849 | /* |
Willy Tarreau | 74b98a8 | 2007-06-16 19:35:18 +0200 | [diff] [blame] | 850 | * OK now we have the result of this expression in acl_res. |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 851 | * - we have the PASS bit set if at least one pattern matched ; |
| 852 | * - we have the MISS bit set if at least one pattern may match |
| 853 | * later so that we should not cache a failure ; |
| 854 | * |
| 855 | * Then if (PASS || !MISS) we can cache the result, and put |
| 856 | * (test.flags & ACL_TEST_F_VOLATILE) in the cache flags. |
| 857 | * |
| 858 | * FIXME: implement cache. |
| 859 | * |
| 860 | */ |
| 861 | |
| 862 | /* now we may have some cleanup to do */ |
| 863 | if (test.flags & ACL_TEST_F_MUST_FREE) { |
| 864 | free(test.ptr); |
| 865 | test.len = 0; |
| 866 | } |
| 867 | |
| 868 | if (acl_res & ACL_PAT_PASS) |
| 869 | break; |
| 870 | |
| 871 | /* prepare to test another expression */ |
| 872 | acl_res = ACL_PAT_FAIL; |
| 873 | |
| 874 | if (test.flags & ACL_TEST_F_FETCH_MORE) |
| 875 | goto fetch_next; |
| 876 | } |
| 877 | /* |
| 878 | * Here we have the result of an ACL (cached or not). |
| 879 | * ACLs are combined, negated or not, to form conditions. |
| 880 | */ |
| 881 | |
| 882 | acl_res &= ACL_PAT_PASS; |
| 883 | if (term->neg) |
| 884 | acl_res ^= ACL_PAT_PASS; |
| 885 | |
| 886 | suite_res &= acl_res; |
| 887 | if (!(suite_res & ACL_PAT_PASS)) |
| 888 | break; |
| 889 | } |
| 890 | cond_res |= suite_res; |
| 891 | if (cond_res & ACL_PAT_PASS) |
| 892 | break; |
| 893 | } |
| 894 | |
| 895 | return (cond_res & ACL_PAT_PASS) ? 1 : 0; |
| 896 | } |
| 897 | |
| 898 | |
| 899 | /************************************************************************/ |
| 900 | /* All supported keywords must be declared here. */ |
| 901 | /************************************************************************/ |
| 902 | |
| 903 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 904 | static struct acl_kw_list acl_kws = {{ },{ |
Willy Tarreau | a590983 | 2007-06-17 20:40:25 +0200 | [diff] [blame] | 905 | { "always_true", acl_parse_nothing, acl_fetch_nothing, acl_match_true }, |
| 906 | { "always_false", acl_parse_nothing, acl_fetch_nothing, acl_match_false }, |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 907 | #if 0 |
| 908 | { "time", acl_parse_time, acl_fetch_time, acl_match_time }, |
| 909 | #endif |
| 910 | { NULL, NULL, NULL, NULL } |
| 911 | }}; |
| 912 | |
| 913 | |
| 914 | __attribute__((constructor)) |
| 915 | static void __acl_init(void) |
| 916 | { |
| 917 | acl_register_keywords(&acl_kws); |
| 918 | } |
| 919 | |
| 920 | |
| 921 | /* |
| 922 | * Local variables: |
| 923 | * c-indent-level: 8 |
| 924 | * c-basic-offset: 8 |
| 925 | * End: |
| 926 | */ |