include/proto/acl.h
This file provides interface definitions for ACL manipulation.
- Copyright (C) 2000-2007 Willy Tarreau - w@1wt.eu
+ Copyright (C) 2000-2008 Willy Tarreau - w@1wt.eu
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
* FIXME: we need destructor functions too !
*/
+/* Negate an acl result. This turns (ACL_PAT_FAIL, ACL_PAT_MISS, ACL_PAT_PASS)
+ * into (ACL_PAT_PASS, ACL_PAT_MISS, ACL_PAT_FAIL).
+ */
+static inline int acl_neg(int res)
+{
+ return (3 >> res);
+}
+
+/* Convert an acl result to a boolean. Only ACL_PAT_PASS returns 1. */
+static inline int acl_pass(int res)
+{
+ return (res >> 1);
+}
/* Return a pointer to the ACL <name> within the list starting at <head>, or
* NULL if not found.
*/
struct acl_cond *parse_acl_cond(const char **args, struct list *known_acl, int pol);
-/* Execute condition <cond> and return 0 if test fails or 1 if test succeeds.
- * This function only computes the condition, it does not apply the polarity
- * required by IF/UNLESS, it's up to the caller to do this.
+/* Execute condition <cond> and return either ACL_PAT_FAIL, ACL_PAT_MISS or
+ * ACL_PAT_PASS depending on the test results. This function only computes the
+ * condition, it does not apply the polarity required by IF/UNLESS, it's up to
+ * the caller to do this.
*/
int acl_exec_cond(struct acl_cond *cond, struct proxy *px, struct session *l4, void *l7, int dir);
include/types/acl.h
This file provides structures and types for ACLs.
- Copyright (C) 2000-2007 Willy Tarreau - w@1wt.eu
+ Copyright (C) 2000-2008 Willy Tarreau - w@1wt.eu
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
#include <types/session.h>
-/* pattern matching function result */
+/* Pattern matching function result.
+ *
+ * We're using a 3-state matching system :
+ * - PASS : at least one pattern already matches
+ * - MISS : some data is missing to decide if some rules may finally match.
+ * - FAIL : no mattern may ever match
+ *
+ * We assign values 0, 1 and 3 to FAIL, MISS and PASS respectively, so that we
+ * can make use of standard arithmetics for the truth tables below :
+ *
+ * x | !x x&y | F(0) | M(1) | P(3) x|y | F(0) | M(1) | P(3)
+ * ------+----- -----+------+------+----- -----+------+------+-----
+ * F(0) | P(3) F(0)| F(0) | F(0) | F(0) F(0)| F(0) | M(1) | P(3)
+ * M(1) | M(1) M(1)| F(0) | M(1) | M(1) M(1)| M(1) | M(1) | P(3)
+ * P(3) | F(0) P(3)| F(0) | M(1) | P(3) P(3)| P(3) | P(3) | P(3)
+ *
+ * neg(x) = (3 >> x) and(x,y) = (x & y) or(x,y) = (x | y)
+ *
+ */
+
enum {
ACL_PAT_FAIL = 0, /* test failed */
- ACL_PAT_PASS = (1 << 0), /* test passed */
- ACL_PAT_MISS = (1 << 1), /* failed because of missing info (do not cache) */
+ ACL_PAT_MISS = 1, /* test may pass with more info */
+ ACL_PAT_PASS = 3, /* test passed */
};
/* Condition polarity. It makes it easier for any option to choose between
* IF/UNLESS if it can store that information within the condition itself.
+ * Those should be interpreted as "IF/UNLESS result == PASS".
*/
enum {
ACL_COND_NONE, /* no polarity set yet */
/*
* ACL management functions.
*
- * Copyright 2000-2007 Willy Tarreau <w@1wt.eu>
+ * Copyright 2000-2008 Willy Tarreau <w@1wt.eu>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
static int
acl_match_true(struct acl_test *test, struct acl_pattern *pattern)
{
- return 1;
+ return ACL_PAT_PASS;
}
/* always return false */
static int
acl_match_false(struct acl_test *test, struct acl_pattern *pattern)
{
- return 0;
+ return ACL_PAT_FAIL;
}
int icase;
if (pattern->len != test->len)
- return 0;
+ return ACL_PAT_FAIL;
icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) == 0) ||
(!icase && strncmp(pattern->ptr.str, test->ptr, test->len) == 0))
- return 1;
- return 0;
+ return ACL_PAT_PASS;
+ return ACL_PAT_FAIL;
}
/* Executes a regex. It needs to change the data. If it is marked READ_ONLY
new_str = calloc(1, test->len + 1);
if (!new_str)
- return 0;
+ return ACL_PAT_FAIL;
memcpy(new_str, test->ptr, test->len);
new_str[test->len] = 0;
test->ptr[test->len] = 0;
if (regexec(pattern->ptr.reg, test->ptr, 0, NULL, 0) == 0)
- ret = 1;
+ ret = ACL_PAT_PASS;
else
- ret = 0;
+ ret = ACL_PAT_FAIL;
test->ptr[test->len] = old_char;
return ret;
int icase;
if (pattern->len > test->len)
- return 0;
+ return ACL_PAT_FAIL;
icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
if ((icase && strncasecmp(pattern->ptr.str, test->ptr, pattern->len) != 0) ||
(!icase && strncmp(pattern->ptr.str, test->ptr, pattern->len) != 0))
- return 0;
- return 1;
+ return ACL_PAT_FAIL;
+ return ACL_PAT_PASS;
}
/* Checks that the pattern matches the end of the tested string. */
int icase;
if (pattern->len > test->len)
- return 0;
+ return ACL_PAT_FAIL;
icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
if ((icase && strncasecmp(pattern->ptr.str, test->ptr + test->len - pattern->len, pattern->len) != 0) ||
(!icase && strncmp(pattern->ptr.str, test->ptr + test->len - pattern->len, pattern->len) != 0))
- return 0;
- return 1;
+ return ACL_PAT_FAIL;
+ return ACL_PAT_PASS;
}
/* Checks that the pattern is included inside the tested string.
char *c;
if (pattern->len > test->len)
- return 0;
+ return ACL_PAT_FAIL;
end = test->ptr + test->len - pattern->len;
icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
if (tolower(*c) != tolower(*pattern->ptr.str))
continue;
if (strncasecmp(pattern->ptr.str, c, pattern->len) == 0)
- return 1;
+ return ACL_PAT_PASS;
}
} else {
for (c = test->ptr; c <= end; c++) {
if (*c != *pattern->ptr.str)
continue;
if (strncmp(pattern->ptr.str, c, pattern->len) == 0)
- return 1;
+ return ACL_PAT_PASS;
}
}
- return 0;
+ return ACL_PAT_FAIL;
}
/* This one is used by other real functions. It checks that the pattern is
pl--;
if (pl > test->len)
- return 0;
+ return ACL_PAT_FAIL;
may_match = 1;
icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
if ((tolower(*c) == tolower(*ps)) &&
(strncasecmp(ps, c, pl) == 0) &&
(c == end || c[pl] == '/' || c[pl] == delim || c[pl] == '?'))
- return 1;
+ return ACL_PAT_PASS;
} else {
if ((*c == *ps) &&
(strncmp(ps, c, pl) == 0) &&
(c == end || c[pl] == '/' || c[pl] == delim || c[pl] == '?'))
- return 1;
+ return ACL_PAT_PASS;
}
may_match = 0;
}
- return 0;
+ return ACL_PAT_FAIL;
}
/* Checks that the pattern is included inside the tested string, but enclosed
{
if ((!pattern->val.range.min_set || pattern->val.range.min <= test->i) &&
(!pattern->val.range.max_set || test->i <= pattern->val.range.max))
- return 1;
- return 0;
+ return ACL_PAT_PASS;
+ return ACL_PAT_FAIL;
}
int acl_match_ip(struct acl_test *test, struct acl_pattern *pattern)
struct in_addr *s;
if (test->i != AF_INET)
- return 0;
+ return ACL_PAT_FAIL;
s = (void *)test->ptr;
if (((s->s_addr ^ pattern->val.ipv4.addr.s_addr) & pattern->val.ipv4.mask.s_addr) == 0)
- return 1;
- return 0;
+ return ACL_PAT_PASS;
+ return ACL_PAT_FAIL;
}
/* Parse a string. It is allocated and duplicated. */
return NULL;
}
-/* Execute condition <cond> and return 0 if test fails or 1 if test succeeds.
- * This function only computes the condition, it does not apply the polarity
- * required by IF/UNLESS, it's up to the caller to do this.
+/* Execute condition <cond> and return either ACL_PAT_FAIL, ACL_PAT_MISS or
+ * ACL_PAT_PASS depending on the test results. This function only computes the
+ * condition, it does not apply the polarity required by IF/UNLESS, it's up to
+ * the caller to do this using something like this :
+ *
+ * res = acl_pass(res);
+ * if (cond->pol == ACL_COND_UNLESS)
+ * res = !res;
*/
int acl_exec_cond(struct acl_cond *cond, struct proxy *px, struct session *l4, void *l7, int dir)
{
struct acl *acl;
struct acl_pattern *pattern;
struct acl_test test;
- int acl_res, pat_res, suite_res, cond_res;
+ int acl_res, suite_res, cond_res;
- /* we're doing a logical OR between conditions so we initialize to FAIL */
+ /* We're doing a logical OR between conditions so we initialize to FAIL.
+ * The MISS status is propagated down from the suites.
+ */
cond_res = ACL_PAT_FAIL;
list_for_each_entry(suite, &cond->suites, list) {
- /* evaluate condition suite <suite>. We stop at the first term
- * which does not return ACL_PAT_PASS.
+ /* Evaluate condition suite <suite>. We stop at the first term
+ * which returns ACL_PAT_FAIL. The MISS status is still propagated
+ * in case of uncertainty in the result.
*/
/* we're doing a logical AND between terms, so we must set the
/* apply all tests to this value */
list_for_each_entry(pattern, &expr->patterns, list) {
- pat_res = expr->kw->match(&test, pattern);
-
- if (pat_res & ACL_PAT_MISS) {
- /* there is at least one test which might be worth retrying later. */
- acl_res |= ACL_PAT_MISS;
- continue;
- } else if (pat_res & ACL_PAT_PASS) {
- /* we found one ! */
- acl_res |= ACL_PAT_PASS;
+ acl_res |= expr->kw->match(&test, pattern);
+ if (acl_res == ACL_PAT_PASS)
break;
- }
}
/*
- * OK now we have the result of this expression in acl_res.
- * - we have the PASS bit set if at least one pattern matched ;
- * - we have the MISS bit set if at least one pattern may match
- * later so that we should not cache a failure ;
+ * OK now acl_res holds the result of this expression
+ * as one of ACL_PAT_FAIL, ACL_PAT_MISS or ACL_PAT_PASS.
*
- * Then if (PASS || !MISS) we can cache the result, and put
+ * Then if (!MISS) we can cache the result, and put
* (test.flags & ACL_TEST_F_VOLATILE) in the cache flags.
*
* FIXME: implement cache.
test.len = 0;
}
- if (acl_res & ACL_PAT_PASS)
+ /* we're ORing these terms, so a single PASS is enough */
+ if (acl_res == ACL_PAT_PASS)
break;
- /* prepare to test another expression */
- acl_res = ACL_PAT_FAIL;
-
if (test.flags & ACL_TEST_F_FETCH_MORE)
goto fetch_next;
}
* ACLs are combined, negated or not, to form conditions.
*/
- acl_res &= ACL_PAT_PASS;
if (term->neg)
- acl_res ^= ACL_PAT_PASS;
+ acl_res = acl_neg(acl_res);
suite_res &= acl_res;
- if (!(suite_res & ACL_PAT_PASS))
+
+ /* we're ANDing these terms, so a single FAIL is enough */
+ if (suite_res == ACL_PAT_FAIL)
break;
}
cond_res |= suite_res;
- if (cond_res & ACL_PAT_PASS)
+
+ /* we're ORing these terms, so a single PASS is enough */
+ if (cond_res == ACL_PAT_PASS)
break;
}
-
- return (cond_res & ACL_PAT_PASS) ? 1 : 0;
+ return cond_res;
}
/* Check if we want to fail this monitor request or not */
list_for_each_entry(cond, &cur_proxy->mon_fail_cond, list) {
int ret = acl_exec_cond(cond, cur_proxy, t, txn, ACL_DIR_REQ);
+
+ ret = acl_pass(ret);
if (cond->pol == ACL_COND_UNLESS)
ret = !ret;
/* first check whether we have some ACLs set to redirect this request */
list_for_each_entry(rule, &cur_proxy->redirect_rules, list) {
int ret = acl_exec_cond(rule->cond, cur_proxy, t, txn, ACL_DIR_REQ);
+
+ ret = acl_pass(ret);
if (rule->cond->pol == ACL_COND_UNLESS)
ret = !ret;
/* first check whether we have some ACLs set to block this request */
list_for_each_entry(cond, &cur_proxy->block_cond, list) {
int ret = acl_exec_cond(cond, cur_proxy, t, txn, ACL_DIR_REQ);
+
+ ret = acl_pass(ret);
if (cond->pol == ACL_COND_UNLESS)
ret = !ret;
int ret;
ret = acl_exec_cond(rule->cond, cur_proxy, t, txn, ACL_DIR_REQ);
+
+ ret = acl_pass(ret);
if (rule->cond->pol == ACL_COND_UNLESS)
ret = !ret;
int icase;
if (test->i != pattern->val.i)
- return 0;
+ return ACL_PAT_FAIL;
if (test->i != HTTP_METH_OTHER)
- return 1;
+ return ACL_PAT_PASS;
/* Other method, we must compare the strings */
if (pattern->len != test->len)
- return 0;
+ return ACL_PAT_FAIL;
icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) != 0) ||
(!icase && strncmp(pattern->ptr.str, test->ptr, test->len) != 0))
- return 0;
- return 1;
+ return ACL_PAT_FAIL;
+ return ACL_PAT_PASS;
}
/* 2. Check on Request/Status Version