[TESTS] added test-acl.cfg to test some ACL combinations

various rules constructions can be tested with this test case.
This commit is contained in:
Willy Tarreau 2008-07-09 16:18:21 +02:00
parent a8cfa34a9c
commit 11382813a1
5 changed files with 139 additions and 73 deletions

View File

@ -2,7 +2,7 @@
include/proto/acl.h include/proto/acl.h
This file provides interface definitions for ACL manipulation. 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 This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public modify it under the terms of the GNU Lesser General Public
@ -29,6 +29,19 @@
* FIXME: we need destructor functions too ! * 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 /* Return a pointer to the ACL <name> within the list starting at <head>, or
* NULL if not found. * NULL if not found.
@ -67,9 +80,10 @@ struct acl_cond *prune_acl_cond(struct acl_cond *cond);
*/ */
struct acl_cond *parse_acl_cond(const char **args, struct list *known_acl, int pol); 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. /* Execute condition <cond> and return either ACL_PAT_FAIL, ACL_PAT_MISS or
* This function only computes the condition, it does not apply the polarity * ACL_PAT_PASS depending on the test results. This function only computes the
* required by IF/UNLESS, it's up to the caller to do this. * 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); int acl_exec_cond(struct acl_cond *cond, struct proxy *px, struct session *l4, void *l7, int dir);

View File

@ -2,7 +2,7 @@
include/types/acl.h include/types/acl.h
This file provides structures and types for ACLs. 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 This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public modify it under the terms of the GNU Lesser General Public
@ -30,15 +30,35 @@
#include <types/session.h> #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 { enum {
ACL_PAT_FAIL = 0, /* test failed */ ACL_PAT_FAIL = 0, /* test failed */
ACL_PAT_PASS = (1 << 0), /* test passed */ ACL_PAT_MISS = 1, /* test may pass with more info */
ACL_PAT_MISS = (1 << 1), /* failed because of missing info (do not cache) */ ACL_PAT_PASS = 3, /* test passed */
}; };
/* Condition polarity. It makes it easier for any option to choose between /* Condition polarity. It makes it easier for any option to choose between
* IF/UNLESS if it can store that information within the condition itself. * IF/UNLESS if it can store that information within the condition itself.
* Those should be interpreted as "IF/UNLESS result == PASS".
*/ */
enum { enum {
ACL_COND_NONE, /* no polarity set yet */ ACL_COND_NONE, /* no polarity set yet */

118
src/acl.c
View File

@ -1,7 +1,7 @@
/* /*
* ACL management functions. * 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 * This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License * modify it under the terms of the GNU General Public License
@ -53,14 +53,14 @@ acl_fetch_nothing(struct proxy *px, struct session *l4, void *l7, int dir,
static int static int
acl_match_true(struct acl_test *test, struct acl_pattern *pattern) acl_match_true(struct acl_test *test, struct acl_pattern *pattern)
{ {
return 1; return ACL_PAT_PASS;
} }
/* always return false */ /* always return false */
static int static int
acl_match_false(struct acl_test *test, struct acl_pattern *pattern) acl_match_false(struct acl_test *test, struct acl_pattern *pattern)
{ {
return 0; return ACL_PAT_FAIL;
} }
@ -70,13 +70,13 @@ int acl_match_str(struct acl_test *test, struct acl_pattern *pattern)
int icase; int icase;
if (pattern->len != test->len) if (pattern->len != test->len)
return 0; return ACL_PAT_FAIL;
icase = pattern->flags & ACL_PAT_F_IGNORE_CASE; icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) == 0) || if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) == 0) ||
(!icase && strncmp(pattern->ptr.str, test->ptr, test->len) == 0)) (!icase && strncmp(pattern->ptr.str, test->ptr, test->len) == 0))
return 1; return ACL_PAT_PASS;
return 0; return ACL_PAT_FAIL;
} }
/* Executes a regex. It needs to change the data. If it is marked READ_ONLY /* Executes a regex. It needs to change the data. If it is marked READ_ONLY
@ -94,7 +94,7 @@ int acl_match_reg(struct acl_test *test, struct acl_pattern *pattern)
new_str = calloc(1, test->len + 1); new_str = calloc(1, test->len + 1);
if (!new_str) if (!new_str)
return 0; return ACL_PAT_FAIL;
memcpy(new_str, test->ptr, test->len); memcpy(new_str, test->ptr, test->len);
new_str[test->len] = 0; new_str[test->len] = 0;
@ -109,9 +109,9 @@ int acl_match_reg(struct acl_test *test, struct acl_pattern *pattern)
test->ptr[test->len] = 0; test->ptr[test->len] = 0;
if (regexec(pattern->ptr.reg, test->ptr, 0, NULL, 0) == 0) if (regexec(pattern->ptr.reg, test->ptr, 0, NULL, 0) == 0)
ret = 1; ret = ACL_PAT_PASS;
else else
ret = 0; ret = ACL_PAT_FAIL;
test->ptr[test->len] = old_char; test->ptr[test->len] = old_char;
return ret; return ret;
@ -123,13 +123,13 @@ int acl_match_beg(struct acl_test *test, struct acl_pattern *pattern)
int icase; int icase;
if (pattern->len > test->len) if (pattern->len > test->len)
return 0; return ACL_PAT_FAIL;
icase = pattern->flags & ACL_PAT_F_IGNORE_CASE; icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
if ((icase && strncasecmp(pattern->ptr.str, test->ptr, pattern->len) != 0) || if ((icase && strncasecmp(pattern->ptr.str, test->ptr, pattern->len) != 0) ||
(!icase && strncmp(pattern->ptr.str, test->ptr, pattern->len) != 0)) (!icase && strncmp(pattern->ptr.str, test->ptr, pattern->len) != 0))
return 0; return ACL_PAT_FAIL;
return 1; return ACL_PAT_PASS;
} }
/* Checks that the pattern matches the end of the tested string. */ /* Checks that the pattern matches the end of the tested string. */
@ -138,12 +138,12 @@ int acl_match_end(struct acl_test *test, struct acl_pattern *pattern)
int icase; int icase;
if (pattern->len > test->len) if (pattern->len > test->len)
return 0; return ACL_PAT_FAIL;
icase = pattern->flags & ACL_PAT_F_IGNORE_CASE; icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
if ((icase && strncasecmp(pattern->ptr.str, test->ptr + test->len - pattern->len, pattern->len) != 0) || 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)) (!icase && strncmp(pattern->ptr.str, test->ptr + test->len - pattern->len, pattern->len) != 0))
return 0; return ACL_PAT_FAIL;
return 1; return ACL_PAT_PASS;
} }
/* Checks that the pattern is included inside the tested string. /* Checks that the pattern is included inside the tested string.
@ -156,7 +156,7 @@ int acl_match_sub(struct acl_test *test, struct acl_pattern *pattern)
char *c; char *c;
if (pattern->len > test->len) if (pattern->len > test->len)
return 0; return ACL_PAT_FAIL;
end = test->ptr + test->len - pattern->len; end = test->ptr + test->len - pattern->len;
icase = pattern->flags & ACL_PAT_F_IGNORE_CASE; icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
@ -165,17 +165,17 @@ int acl_match_sub(struct acl_test *test, struct acl_pattern *pattern)
if (tolower(*c) != tolower(*pattern->ptr.str)) if (tolower(*c) != tolower(*pattern->ptr.str))
continue; continue;
if (strncasecmp(pattern->ptr.str, c, pattern->len) == 0) if (strncasecmp(pattern->ptr.str, c, pattern->len) == 0)
return 1; return ACL_PAT_PASS;
} }
} else { } else {
for (c = test->ptr; c <= end; c++) { for (c = test->ptr; c <= end; c++) {
if (*c != *pattern->ptr.str) if (*c != *pattern->ptr.str)
continue; continue;
if (strncmp(pattern->ptr.str, c, pattern->len) == 0) 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 /* This one is used by other real functions. It checks that the pattern is
@ -202,7 +202,7 @@ static int match_word(struct acl_test *test, struct acl_pattern *pattern, char d
pl--; pl--;
if (pl > test->len) if (pl > test->len)
return 0; return ACL_PAT_FAIL;
may_match = 1; may_match = 1;
icase = pattern->flags & ACL_PAT_F_IGNORE_CASE; icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
@ -220,16 +220,16 @@ static int match_word(struct acl_test *test, struct acl_pattern *pattern, char d
if ((tolower(*c) == tolower(*ps)) && if ((tolower(*c) == tolower(*ps)) &&
(strncasecmp(ps, c, pl) == 0) && (strncasecmp(ps, c, pl) == 0) &&
(c == end || c[pl] == '/' || c[pl] == delim || c[pl] == '?')) (c == end || c[pl] == '/' || c[pl] == delim || c[pl] == '?'))
return 1; return ACL_PAT_PASS;
} else { } else {
if ((*c == *ps) && if ((*c == *ps) &&
(strncmp(ps, c, pl) == 0) && (strncmp(ps, c, pl) == 0) &&
(c == end || c[pl] == '/' || c[pl] == delim || c[pl] == '?')) (c == end || c[pl] == '/' || c[pl] == delim || c[pl] == '?'))
return 1; return ACL_PAT_PASS;
} }
may_match = 0; may_match = 0;
} }
return 0; return ACL_PAT_FAIL;
} }
/* Checks that the pattern is included inside the tested string, but enclosed /* Checks that the pattern is included inside the tested string, but enclosed
@ -255,8 +255,8 @@ int acl_match_int(struct acl_test *test, struct acl_pattern *pattern)
{ {
if ((!pattern->val.range.min_set || pattern->val.range.min <= test->i) && if ((!pattern->val.range.min_set || pattern->val.range.min <= test->i) &&
(!pattern->val.range.max_set || test->i <= pattern->val.range.max)) (!pattern->val.range.max_set || test->i <= pattern->val.range.max))
return 1; return ACL_PAT_PASS;
return 0; return ACL_PAT_FAIL;
} }
int acl_match_ip(struct acl_test *test, struct acl_pattern *pattern) int acl_match_ip(struct acl_test *test, struct acl_pattern *pattern)
@ -264,12 +264,12 @@ int acl_match_ip(struct acl_test *test, struct acl_pattern *pattern)
struct in_addr *s; struct in_addr *s;
if (test->i != AF_INET) if (test->i != AF_INET)
return 0; return ACL_PAT_FAIL;
s = (void *)test->ptr; s = (void *)test->ptr;
if (((s->s_addr ^ pattern->val.ipv4.addr.s_addr) & pattern->val.ipv4.mask.s_addr) == 0) if (((s->s_addr ^ pattern->val.ipv4.addr.s_addr) & pattern->val.ipv4.mask.s_addr) == 0)
return 1; return ACL_PAT_PASS;
return 0; return ACL_PAT_FAIL;
} }
/* Parse a string. It is allocated and duplicated. */ /* Parse a string. It is allocated and duplicated. */
@ -817,9 +817,14 @@ struct acl_cond *parse_acl_cond(const char **args, struct list *known_acl, int p
return NULL; return NULL;
} }
/* Execute condition <cond> and return 0 if test fails or 1 if test succeeds. /* Execute condition <cond> and return either ACL_PAT_FAIL, ACL_PAT_MISS or
* This function only computes the condition, it does not apply the polarity * ACL_PAT_PASS depending on the test results. This function only computes the
* required by IF/UNLESS, it's up to the caller to do this. * 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) int acl_exec_cond(struct acl_cond *cond, struct proxy *px, struct session *l4, void *l7, int dir)
{ {
@ -830,13 +835,16 @@ int acl_exec_cond(struct acl_cond *cond, struct proxy *px, struct session *l4, v
struct acl *acl; struct acl *acl;
struct acl_pattern *pattern; struct acl_pattern *pattern;
struct acl_test test; 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; cond_res = ACL_PAT_FAIL;
list_for_each_entry(suite, &cond->suites, list) { list_for_each_entry(suite, &cond->suites, list) {
/* evaluate condition suite <suite>. We stop at the first term /* Evaluate condition suite <suite>. We stop at the first term
* which does not return ACL_PAT_PASS. * 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 /* we're doing a logical AND between terms, so we must set the
@ -863,25 +871,15 @@ int acl_exec_cond(struct acl_cond *cond, struct proxy *px, struct session *l4, v
/* apply all tests to this value */ /* apply all tests to this value */
list_for_each_entry(pattern, &expr->patterns, list) { list_for_each_entry(pattern, &expr->patterns, list) {
pat_res = expr->kw->match(&test, pattern); acl_res |= expr->kw->match(&test, pattern);
if (acl_res == ACL_PAT_PASS)
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;
break; break;
}
} }
/* /*
* OK now we have the result of this expression in acl_res. * OK now acl_res holds the result of this expression
* - we have the PASS bit set if at least one pattern matched ; * as one of ACL_PAT_FAIL, ACL_PAT_MISS or ACL_PAT_PASS.
* - we have the MISS bit set if at least one pattern may match
* later so that we should not cache a failure ;
* *
* 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. * (test.flags & ACL_TEST_F_VOLATILE) in the cache flags.
* *
* FIXME: implement cache. * FIXME: implement cache.
@ -894,12 +892,10 @@ int acl_exec_cond(struct acl_cond *cond, struct proxy *px, struct session *l4, v
test.len = 0; 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; break;
/* prepare to test another expression */
acl_res = ACL_PAT_FAIL;
if (test.flags & ACL_TEST_F_FETCH_MORE) if (test.flags & ACL_TEST_F_FETCH_MORE)
goto fetch_next; goto fetch_next;
} }
@ -908,20 +904,22 @@ int acl_exec_cond(struct acl_cond *cond, struct proxy *px, struct session *l4, v
* ACLs are combined, negated or not, to form conditions. * ACLs are combined, negated or not, to form conditions.
*/ */
acl_res &= ACL_PAT_PASS;
if (term->neg) if (term->neg)
acl_res ^= ACL_PAT_PASS; acl_res = acl_neg(acl_res);
suite_res &= 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; break;
} }
cond_res |= suite_res; 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; break;
} }
return cond_res;
return (cond_res & ACL_PAT_PASS) ? 1 : 0;
} }

View File

@ -1700,6 +1700,8 @@ int process_cli(struct session *t)
/* Check if we want to fail this monitor request or not */ /* Check if we want to fail this monitor request or not */
list_for_each_entry(cond, &cur_proxy->mon_fail_cond, list) { list_for_each_entry(cond, &cur_proxy->mon_fail_cond, list) {
int ret = acl_exec_cond(cond, cur_proxy, t, txn, ACL_DIR_REQ); int ret = acl_exec_cond(cond, cur_proxy, t, txn, ACL_DIR_REQ);
ret = acl_pass(ret);
if (cond->pol == ACL_COND_UNLESS) if (cond->pol == ACL_COND_UNLESS)
ret = !ret; ret = !ret;
@ -1810,6 +1812,8 @@ int process_cli(struct session *t)
/* first check whether we have some ACLs set to redirect this request */ /* first check whether we have some ACLs set to redirect this request */
list_for_each_entry(rule, &cur_proxy->redirect_rules, list) { list_for_each_entry(rule, &cur_proxy->redirect_rules, list) {
int ret = acl_exec_cond(rule->cond, cur_proxy, t, txn, ACL_DIR_REQ); 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) if (rule->cond->pol == ACL_COND_UNLESS)
ret = !ret; ret = !ret;
@ -1890,6 +1894,8 @@ int process_cli(struct session *t)
/* first check whether we have some ACLs set to block this request */ /* first check whether we have some ACLs set to block this request */
list_for_each_entry(cond, &cur_proxy->block_cond, list) { list_for_each_entry(cond, &cur_proxy->block_cond, list) {
int ret = acl_exec_cond(cond, cur_proxy, t, txn, ACL_DIR_REQ); int ret = acl_exec_cond(cond, cur_proxy, t, txn, ACL_DIR_REQ);
ret = acl_pass(ret);
if (cond->pol == ACL_COND_UNLESS) if (cond->pol == ACL_COND_UNLESS)
ret = !ret; ret = !ret;
@ -2004,6 +2010,8 @@ int process_cli(struct session *t)
int ret; int ret;
ret = acl_exec_cond(rule->cond, cur_proxy, t, txn, ACL_DIR_REQ); ret = acl_exec_cond(rule->cond, cur_proxy, t, txn, ACL_DIR_REQ);
ret = acl_pass(ret);
if (rule->cond->pol == ACL_COND_UNLESS) if (rule->cond->pol == ACL_COND_UNLESS)
ret = !ret; ret = !ret;
@ -5215,20 +5223,20 @@ static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern)
int icase; int icase;
if (test->i != pattern->val.i) if (test->i != pattern->val.i)
return 0; return ACL_PAT_FAIL;
if (test->i != HTTP_METH_OTHER) if (test->i != HTTP_METH_OTHER)
return 1; return ACL_PAT_PASS;
/* Other method, we must compare the strings */ /* Other method, we must compare the strings */
if (pattern->len != test->len) if (pattern->len != test->len)
return 0; return ACL_PAT_FAIL;
icase = pattern->flags & ACL_PAT_F_IGNORE_CASE; icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) != 0) || if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) != 0) ||
(!icase && strncmp(pattern->ptr.str, test->ptr, test->len) != 0)) (!icase && strncmp(pattern->ptr.str, test->ptr, test->len) != 0))
return 0; return ACL_PAT_FAIL;
return 1; return ACL_PAT_PASS;
} }
/* 2. Check on Request/Status Version /* 2. Check on Request/Status Version

26
tests/test-acl.cfg Normal file
View File

@ -0,0 +1,26 @@
# This sample configuration tests multiple ACL combinations. It requires
# HAProxy version 1.3.15 minimum.
global
maxconn 100
frontend http-in
bind :8000-8003
mode http
clitimeout 30000
acl p1 dst_port 8001
acl p2 dst_port 8002
acl p3 dst_port 8003
acl d1 dst 127.0.0.1
acl d2 dst 127.0.0.2
acl d3 dst 127.0.0.3
redirect location d1&p2|d2&p1 if d1 p2 or d2 p1
redirect location d1&p1 if d1 p1
redirect location !(d2|d3) unless d2 or d3
redirect location d2&!p1 if d2 !p1
redirect location !d2&p1 if !d2 p1
redirect location !!d2 unless !d2
block if d3