Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 1 | /* |
| 2 | * ACL management functions. |
| 3 | * |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 4 | * Copyright 2000-2008 Willy Tarreau <w@1wt.eu> |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 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 | |
Willy Tarreau | a980263 | 2008-07-25 19:13:19 +0200 | [diff] [blame] | 23 | /* The capabilities of filtering hooks describe the type of information |
| 24 | * available to each of them. |
| 25 | */ |
| 26 | const unsigned int filt_cap[] = { |
| 27 | [ACL_HOOK_REQ_FE_TCP] = ACL_USE_TCP4_ANY|ACL_USE_TCP6_ANY|ACL_USE_TCP_ANY, |
| 28 | [ACL_HOOK_REQ_FE_TCP_CONTENT] = ACL_USE_TCP4_ANY|ACL_USE_TCP6_ANY|ACL_USE_TCP_ANY|ACL_USE_L4REQ_ANY, |
| 29 | [ACL_HOOK_REQ_FE_HTTP_IN] = ACL_USE_TCP4_ANY|ACL_USE_TCP6_ANY|ACL_USE_TCP_ANY|ACL_USE_L4REQ_ANY|ACL_USE_L7REQ_ANY|ACL_USE_HDR_ANY, |
| 30 | [ACL_HOOK_REQ_FE_SWITCH] = ACL_USE_TCP4_ANY|ACL_USE_TCP6_ANY|ACL_USE_TCP_ANY|ACL_USE_L4REQ_ANY|ACL_USE_L7REQ_ANY|ACL_USE_HDR_ANY, |
| 31 | [ACL_HOOK_REQ_BE_TCP_CONTENT] = ACL_USE_TCP4_ANY|ACL_USE_TCP6_ANY|ACL_USE_TCP_ANY|ACL_USE_L4REQ_ANY|ACL_USE_L7REQ_ANY|ACL_USE_HDR_ANY, |
| 32 | [ACL_HOOK_REQ_BE_HTTP_IN] = ACL_USE_TCP4_ANY|ACL_USE_TCP6_ANY|ACL_USE_TCP_ANY|ACL_USE_L4REQ_ANY|ACL_USE_L7REQ_ANY|ACL_USE_HDR_ANY, |
| 33 | [ACL_HOOK_REQ_BE_SWITCH] = ACL_USE_TCP4_ANY|ACL_USE_TCP6_ANY|ACL_USE_TCP_ANY|ACL_USE_L4REQ_ANY|ACL_USE_L7REQ_ANY|ACL_USE_HDR_ANY, |
| 34 | [ACL_HOOK_REQ_FE_HTTP_OUT] = ACL_USE_TCP4_ANY|ACL_USE_TCP6_ANY|ACL_USE_TCP_ANY|ACL_USE_L4REQ_ANY|ACL_USE_L7REQ_ANY|ACL_USE_HDR_ANY, |
| 35 | [ACL_HOOK_REQ_BE_HTTP_OUT] = ACL_USE_TCP4_ANY|ACL_USE_TCP6_ANY|ACL_USE_TCP_ANY|ACL_USE_L4REQ_ANY|ACL_USE_L7REQ_ANY|ACL_USE_HDR_ANY, |
| 36 | |
| 37 | [ACL_HOOK_RTR_BE_TCP_CONTENT] = ACL_USE_REQ_PERMANENT|ACL_USE_REQ_CACHEABLE|ACL_USE_L4RTR_ANY, |
| 38 | [ACL_HOOK_RTR_BE_HTTP_IN] = ACL_USE_REQ_PERMANENT|ACL_USE_REQ_CACHEABLE|ACL_USE_L4RTR_ANY|ACL_USE_L7RTR_ANY, |
| 39 | [ACL_HOOK_RTR_FE_TCP_CONTENT] = ACL_USE_REQ_PERMANENT|ACL_USE_REQ_CACHEABLE|ACL_USE_L4RTR_ANY|ACL_USE_L7RTR_ANY, |
| 40 | [ACL_HOOK_RTR_FE_HTTP_IN] = ACL_USE_REQ_PERMANENT|ACL_USE_REQ_CACHEABLE|ACL_USE_L4RTR_ANY|ACL_USE_L7RTR_ANY, |
| 41 | [ACL_HOOK_RTR_BE_HTTP_OUT] = ACL_USE_REQ_PERMANENT|ACL_USE_REQ_CACHEABLE|ACL_USE_L4RTR_ANY|ACL_USE_L7RTR_ANY, |
| 42 | [ACL_HOOK_RTR_FE_HTTP_OUT] = ACL_USE_REQ_PERMANENT|ACL_USE_REQ_CACHEABLE|ACL_USE_L4RTR_ANY|ACL_USE_L7RTR_ANY, |
| 43 | }; |
| 44 | |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 45 | /* List head of all known ACL keywords */ |
| 46 | static struct acl_kw_list acl_keywords = { |
| 47 | .list = LIST_HEAD_INIT(acl_keywords.list) |
| 48 | }; |
| 49 | |
| 50 | |
Willy Tarreau | a590983 | 2007-06-17 20:40:25 +0200 | [diff] [blame] | 51 | /* |
| 52 | * These functions are only used for debugging complex configurations. |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 53 | */ |
Willy Tarreau | a590983 | 2007-06-17 20:40:25 +0200 | [diff] [blame] | 54 | |
Willy Tarreau | 58393e1 | 2008-07-20 10:39:22 +0200 | [diff] [blame] | 55 | /* force TRUE to be returned at the fetch level */ |
Willy Tarreau | a590983 | 2007-06-17 20:40:25 +0200 | [diff] [blame] | 56 | static int |
Willy Tarreau | 58393e1 | 2008-07-20 10:39:22 +0200 | [diff] [blame] | 57 | acl_fetch_true(struct proxy *px, struct session *l4, void *l7, int dir, |
| 58 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | a590983 | 2007-06-17 20:40:25 +0200 | [diff] [blame] | 59 | { |
Willy Tarreau | 58393e1 | 2008-07-20 10:39:22 +0200 | [diff] [blame] | 60 | test->flags |= ACL_TEST_F_SET_RES_PASS; |
Willy Tarreau | a590983 | 2007-06-17 20:40:25 +0200 | [diff] [blame] | 61 | return 1; |
| 62 | } |
| 63 | |
Willy Tarreau | b6fb420 | 2008-07-20 11:18:28 +0200 | [diff] [blame] | 64 | /* wait for more data as long as possible, then return TRUE. This should be |
| 65 | * used with content inspection. |
| 66 | */ |
| 67 | static int |
| 68 | acl_fetch_wait_end(struct proxy *px, struct session *l4, void *l7, int dir, |
| 69 | struct acl_expr *expr, struct acl_test *test) |
| 70 | { |
| 71 | if (dir & ACL_PARTIAL) { |
| 72 | test->flags |= ACL_TEST_F_MAY_CHANGE; |
| 73 | return 0; |
| 74 | } |
| 75 | test->flags |= ACL_TEST_F_SET_RES_PASS; |
| 76 | return 1; |
| 77 | } |
| 78 | |
Willy Tarreau | 58393e1 | 2008-07-20 10:39:22 +0200 | [diff] [blame] | 79 | /* force FALSE to be returned at the fetch level */ |
Willy Tarreau | a590983 | 2007-06-17 20:40:25 +0200 | [diff] [blame] | 80 | static int |
Willy Tarreau | 58393e1 | 2008-07-20 10:39:22 +0200 | [diff] [blame] | 81 | acl_fetch_false(struct proxy *px, struct session *l4, void *l7, int dir, |
| 82 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 83 | { |
Willy Tarreau | 58393e1 | 2008-07-20 10:39:22 +0200 | [diff] [blame] | 84 | test->flags |= ACL_TEST_F_SET_RES_FAIL; |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 85 | return 1; |
| 86 | } |
| 87 | |
Willy Tarreau | 58393e1 | 2008-07-20 10:39:22 +0200 | [diff] [blame] | 88 | |
| 89 | /* |
| 90 | * These functions are exported and may be used by any other component. |
| 91 | */ |
| 92 | |
| 93 | /* ignore the current line */ |
| 94 | int acl_parse_nothing(const char **text, struct acl_pattern *pattern, int *opaque) |
Willy Tarreau | a590983 | 2007-06-17 20:40:25 +0200 | [diff] [blame] | 95 | { |
Willy Tarreau | 58393e1 | 2008-07-20 10:39:22 +0200 | [diff] [blame] | 96 | return 1; |
| 97 | } |
| 98 | |
| 99 | /* always fake a data retrieval */ |
| 100 | int acl_fetch_nothing(struct proxy *px, struct session *l4, void *l7, int dir, |
| 101 | struct acl_expr *expr, struct acl_test *test) |
| 102 | { |
| 103 | return 1; |
Willy Tarreau | a590983 | 2007-06-17 20:40:25 +0200 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | /* always return false */ |
Willy Tarreau | 58393e1 | 2008-07-20 10:39:22 +0200 | [diff] [blame] | 107 | int acl_match_nothing(struct acl_test *test, struct acl_pattern *pattern) |
Willy Tarreau | a590983 | 2007-06-17 20:40:25 +0200 | [diff] [blame] | 108 | { |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 109 | return ACL_PAT_FAIL; |
Willy Tarreau | a590983 | 2007-06-17 20:40:25 +0200 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 113 | /* NB: For two strings to be identical, it is required that their lengths match */ |
| 114 | int acl_match_str(struct acl_test *test, struct acl_pattern *pattern) |
| 115 | { |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 116 | int icase; |
| 117 | |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 118 | if (pattern->len != test->len) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 119 | return ACL_PAT_FAIL; |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 120 | |
| 121 | icase = pattern->flags & ACL_PAT_F_IGNORE_CASE; |
| 122 | if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) == 0) || |
| 123 | (!icase && strncmp(pattern->ptr.str, test->ptr, test->len) == 0)) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 124 | return ACL_PAT_PASS; |
| 125 | return ACL_PAT_FAIL; |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 126 | } |
| 127 | |
Willy Tarreau | f3d2598 | 2007-05-08 22:45:09 +0200 | [diff] [blame] | 128 | /* Executes a regex. It needs to change the data. If it is marked READ_ONLY |
| 129 | * then it will be allocated and duplicated in place so that others may use |
| 130 | * it later on. Note that this is embarrassing because we always try to avoid |
| 131 | * allocating memory at run time. |
| 132 | */ |
| 133 | int acl_match_reg(struct acl_test *test, struct acl_pattern *pattern) |
| 134 | { |
| 135 | char old_char; |
| 136 | int ret; |
| 137 | |
| 138 | if (unlikely(test->flags & ACL_TEST_F_READ_ONLY)) { |
| 139 | char *new_str; |
| 140 | |
| 141 | new_str = calloc(1, test->len + 1); |
| 142 | if (!new_str) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 143 | return ACL_PAT_FAIL; |
Willy Tarreau | f3d2598 | 2007-05-08 22:45:09 +0200 | [diff] [blame] | 144 | |
| 145 | memcpy(new_str, test->ptr, test->len); |
| 146 | new_str[test->len] = 0; |
| 147 | if (test->flags & ACL_TEST_F_MUST_FREE) |
| 148 | free(test->ptr); |
| 149 | test->ptr = new_str; |
| 150 | test->flags |= ACL_TEST_F_MUST_FREE; |
| 151 | test->flags &= ~ACL_TEST_F_READ_ONLY; |
| 152 | } |
| 153 | |
| 154 | old_char = test->ptr[test->len]; |
| 155 | test->ptr[test->len] = 0; |
| 156 | |
| 157 | if (regexec(pattern->ptr.reg, test->ptr, 0, NULL, 0) == 0) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 158 | ret = ACL_PAT_PASS; |
Willy Tarreau | f3d2598 | 2007-05-08 22:45:09 +0200 | [diff] [blame] | 159 | else |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 160 | ret = ACL_PAT_FAIL; |
Willy Tarreau | f3d2598 | 2007-05-08 22:45:09 +0200 | [diff] [blame] | 161 | |
| 162 | test->ptr[test->len] = old_char; |
| 163 | return ret; |
| 164 | } |
| 165 | |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 166 | /* Checks that the pattern matches the beginning of the tested string. */ |
| 167 | int acl_match_beg(struct acl_test *test, struct acl_pattern *pattern) |
| 168 | { |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 169 | int icase; |
| 170 | |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 171 | if (pattern->len > test->len) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 172 | return ACL_PAT_FAIL; |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 173 | |
| 174 | icase = pattern->flags & ACL_PAT_F_IGNORE_CASE; |
| 175 | if ((icase && strncasecmp(pattern->ptr.str, test->ptr, pattern->len) != 0) || |
| 176 | (!icase && strncmp(pattern->ptr.str, test->ptr, pattern->len) != 0)) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 177 | return ACL_PAT_FAIL; |
| 178 | return ACL_PAT_PASS; |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | /* Checks that the pattern matches the end of the tested string. */ |
| 182 | int acl_match_end(struct acl_test *test, struct acl_pattern *pattern) |
| 183 | { |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 184 | int icase; |
| 185 | |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 186 | if (pattern->len > test->len) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 187 | return ACL_PAT_FAIL; |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 188 | icase = pattern->flags & ACL_PAT_F_IGNORE_CASE; |
| 189 | if ((icase && strncasecmp(pattern->ptr.str, test->ptr + test->len - pattern->len, pattern->len) != 0) || |
| 190 | (!icase && strncmp(pattern->ptr.str, test->ptr + test->len - pattern->len, pattern->len) != 0)) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 191 | return ACL_PAT_FAIL; |
| 192 | return ACL_PAT_PASS; |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | /* Checks that the pattern is included inside the tested string. |
| 196 | * NB: Suboptimal, should be rewritten using a Boyer-Moore method. |
| 197 | */ |
| 198 | int acl_match_sub(struct acl_test *test, struct acl_pattern *pattern) |
| 199 | { |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 200 | int icase; |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 201 | char *end; |
| 202 | char *c; |
| 203 | |
| 204 | if (pattern->len > test->len) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 205 | return ACL_PAT_FAIL; |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 206 | |
| 207 | end = test->ptr + test->len - pattern->len; |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 208 | icase = pattern->flags & ACL_PAT_F_IGNORE_CASE; |
| 209 | if (icase) { |
| 210 | for (c = test->ptr; c <= end; c++) { |
| 211 | if (tolower(*c) != tolower(*pattern->ptr.str)) |
| 212 | continue; |
| 213 | if (strncasecmp(pattern->ptr.str, c, pattern->len) == 0) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 214 | return ACL_PAT_PASS; |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 215 | } |
| 216 | } else { |
| 217 | for (c = test->ptr; c <= end; c++) { |
| 218 | if (*c != *pattern->ptr.str) |
| 219 | continue; |
| 220 | if (strncmp(pattern->ptr.str, c, pattern->len) == 0) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 221 | return ACL_PAT_PASS; |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 222 | } |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 223 | } |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 224 | return ACL_PAT_FAIL; |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | /* This one is used by other real functions. It checks that the pattern is |
| 228 | * included inside the tested string, but enclosed between the specified |
| 229 | * 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] | 230 | * The delimitor is stripped at the beginning or end of the pattern. |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 231 | */ |
| 232 | static int match_word(struct acl_test *test, struct acl_pattern *pattern, char delim) |
| 233 | { |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 234 | int may_match, icase; |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 235 | char *c, *end; |
| 236 | char *ps; |
| 237 | int pl; |
| 238 | |
| 239 | pl = pattern->len; |
| 240 | ps = pattern->ptr.str; |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 241 | while (pl > 0 && (*ps == delim || *ps == '/' || *ps == '?')) { |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 242 | pl--; |
| 243 | ps++; |
| 244 | } |
| 245 | |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 246 | while (pl > 0 && |
| 247 | (ps[pl - 1] == delim || ps[pl - 1] == '/' || ps[pl - 1] == '?')) |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 248 | pl--; |
| 249 | |
| 250 | if (pl > test->len) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 251 | return ACL_PAT_FAIL; |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 252 | |
| 253 | may_match = 1; |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 254 | icase = pattern->flags & ACL_PAT_F_IGNORE_CASE; |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 255 | end = test->ptr + test->len - pl; |
| 256 | for (c = test->ptr; c <= end; c++) { |
| 257 | if (*c == '/' || *c == delim || *c == '?') { |
| 258 | may_match = 1; |
| 259 | continue; |
| 260 | } |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 261 | |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 262 | if (!may_match) |
| 263 | continue; |
| 264 | |
| 265 | if (icase) { |
| 266 | if ((tolower(*c) == tolower(*ps)) && |
| 267 | (strncasecmp(ps, c, pl) == 0) && |
| 268 | (c == end || c[pl] == '/' || c[pl] == delim || c[pl] == '?')) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 269 | return ACL_PAT_PASS; |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 270 | } else { |
| 271 | if ((*c == *ps) && |
| 272 | (strncmp(ps, c, pl) == 0) && |
| 273 | (c == end || c[pl] == '/' || c[pl] == delim || c[pl] == '?')) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 274 | return ACL_PAT_PASS; |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 275 | } |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 276 | may_match = 0; |
| 277 | } |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 278 | return ACL_PAT_FAIL; |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | /* Checks that the pattern is included inside the tested string, but enclosed |
| 282 | * between slashes or at the beginning or end of the string. Slashes at the |
| 283 | * beginning or end of the pattern are ignored. |
| 284 | */ |
| 285 | int acl_match_dir(struct acl_test *test, struct acl_pattern *pattern) |
| 286 | { |
| 287 | return match_word(test, pattern, '/'); |
| 288 | } |
| 289 | |
| 290 | /* Checks that the pattern is included inside the tested string, but enclosed |
| 291 | * between dots or at the beginning or end of the string. Dots at the beginning |
| 292 | * or end of the pattern are ignored. |
| 293 | */ |
| 294 | int acl_match_dom(struct acl_test *test, struct acl_pattern *pattern) |
| 295 | { |
| 296 | return match_word(test, pattern, '.'); |
| 297 | } |
| 298 | |
| 299 | /* Checks that the integer in <test> is included between min and max */ |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 300 | int acl_match_int(struct acl_test *test, struct acl_pattern *pattern) |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 301 | { |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 302 | if ((!pattern->val.range.min_set || pattern->val.range.min <= test->i) && |
| 303 | (!pattern->val.range.max_set || test->i <= pattern->val.range.max)) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 304 | return ACL_PAT_PASS; |
| 305 | return ACL_PAT_FAIL; |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 306 | } |
| 307 | |
Willy Tarreau | a67fad9 | 2007-05-08 19:50:09 +0200 | [diff] [blame] | 308 | int acl_match_ip(struct acl_test *test, struct acl_pattern *pattern) |
| 309 | { |
| 310 | struct in_addr *s; |
| 311 | |
| 312 | if (test->i != AF_INET) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 313 | return ACL_PAT_FAIL; |
Willy Tarreau | a67fad9 | 2007-05-08 19:50:09 +0200 | [diff] [blame] | 314 | |
| 315 | s = (void *)test->ptr; |
| 316 | if (((s->s_addr ^ pattern->val.ipv4.addr.s_addr) & pattern->val.ipv4.mask.s_addr) == 0) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 317 | return ACL_PAT_PASS; |
| 318 | return ACL_PAT_FAIL; |
Willy Tarreau | a67fad9 | 2007-05-08 19:50:09 +0200 | [diff] [blame] | 319 | } |
| 320 | |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 321 | /* Parse a string. It is allocated and duplicated. */ |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 322 | 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] | 323 | { |
| 324 | int len; |
| 325 | |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 326 | len = strlen(*text); |
| 327 | pattern->ptr.str = strdup(*text); |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 328 | if (!pattern->ptr.str) |
| 329 | return 0; |
| 330 | pattern->len = len; |
| 331 | return 1; |
| 332 | } |
| 333 | |
Krzysztof Piotr Oledzki | 8001d61 | 2008-05-31 13:53:23 +0200 | [diff] [blame] | 334 | /* Free data allocated by acl_parse_reg */ |
| 335 | static void acl_free_reg(void *ptr) { |
| 336 | |
| 337 | regfree((regex_t *)ptr); |
| 338 | } |
| 339 | |
Willy Tarreau | f3d2598 | 2007-05-08 22:45:09 +0200 | [diff] [blame] | 340 | /* Parse a regex. It is allocated. */ |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 341 | 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] | 342 | { |
| 343 | regex_t *preg; |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 344 | int icase; |
Willy Tarreau | f3d2598 | 2007-05-08 22:45:09 +0200 | [diff] [blame] | 345 | |
| 346 | preg = calloc(1, sizeof(regex_t)); |
| 347 | |
| 348 | if (!preg) |
| 349 | return 0; |
| 350 | |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 351 | icase = (pattern->flags & ACL_PAT_F_IGNORE_CASE) ? REG_ICASE : 0; |
| 352 | if (regcomp(preg, *text, REG_EXTENDED | REG_NOSUB | icase) != 0) { |
Willy Tarreau | f3d2598 | 2007-05-08 22:45:09 +0200 | [diff] [blame] | 353 | free(preg); |
| 354 | return 0; |
| 355 | } |
| 356 | |
| 357 | pattern->ptr.reg = preg; |
Krzysztof Piotr Oledzki | 8001d61 | 2008-05-31 13:53:23 +0200 | [diff] [blame] | 358 | pattern->freeptrbuf = &acl_free_reg; |
Willy Tarreau | f3d2598 | 2007-05-08 22:45:09 +0200 | [diff] [blame] | 359 | return 1; |
| 360 | } |
| 361 | |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 362 | /* Parse a range of positive integers delimited by either ':' or '-'. If only |
| 363 | * one integer is read, it is set as both min and max. An operator may be |
| 364 | * specified as the prefix, among this list of 5 : |
| 365 | * |
| 366 | * 0:eq, 1:gt, 2:ge, 3:lt, 4:le |
| 367 | * |
| 368 | * The default operator is "eq". It supports range matching. Ranges are |
| 369 | * rejected for other operators. The operator may be changed at any time. |
| 370 | * The operator is stored in the 'opaque' argument. |
| 371 | * |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 372 | */ |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 373 | 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] | 374 | { |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 375 | signed long long i; |
| 376 | unsigned int j, last, skip = 0; |
| 377 | const char *ptr = *text; |
| 378 | |
| 379 | |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 380 | while (!isdigit((unsigned char)*ptr)) { |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 381 | if (strcmp(ptr, "eq") == 0) *opaque = 0; |
| 382 | else if (strcmp(ptr, "gt") == 0) *opaque = 1; |
| 383 | else if (strcmp(ptr, "ge") == 0) *opaque = 2; |
| 384 | else if (strcmp(ptr, "lt") == 0) *opaque = 3; |
| 385 | else if (strcmp(ptr, "le") == 0) *opaque = 4; |
| 386 | else |
| 387 | return 0; |
| 388 | |
| 389 | skip++; |
| 390 | ptr = text[skip]; |
| 391 | } |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 392 | |
| 393 | last = i = 0; |
| 394 | while (1) { |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 395 | j = *ptr++; |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 396 | if ((j == '-' || j == ':') && !last) { |
| 397 | last++; |
| 398 | pattern->val.range.min = i; |
| 399 | i = 0; |
| 400 | continue; |
| 401 | } |
| 402 | j -= '0'; |
| 403 | if (j > 9) |
| 404 | // also catches the terminating zero |
| 405 | break; |
| 406 | i *= 10; |
| 407 | i += j; |
| 408 | } |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 409 | |
| 410 | if (last && *opaque >= 1 && *opaque <= 4) |
| 411 | /* having a range with a min or a max is absurd */ |
| 412 | return 0; |
| 413 | |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 414 | if (!last) |
| 415 | pattern->val.range.min = i; |
| 416 | pattern->val.range.max = i; |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 417 | |
| 418 | switch (*opaque) { |
| 419 | case 0: /* eq */ |
| 420 | pattern->val.range.min_set = 1; |
| 421 | pattern->val.range.max_set = 1; |
| 422 | break; |
| 423 | case 1: /* gt */ |
| 424 | pattern->val.range.min++; /* gt = ge + 1 */ |
| 425 | case 2: /* ge */ |
| 426 | pattern->val.range.min_set = 1; |
| 427 | pattern->val.range.max_set = 0; |
| 428 | break; |
| 429 | case 3: /* lt */ |
| 430 | pattern->val.range.max--; /* lt = le - 1 */ |
| 431 | case 4: /* le */ |
| 432 | pattern->val.range.min_set = 0; |
| 433 | pattern->val.range.max_set = 1; |
| 434 | break; |
| 435 | } |
| 436 | return skip + 1; |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 437 | } |
| 438 | |
Willy Tarreau | 4a26d2f | 2008-07-15 16:05:33 +0200 | [diff] [blame] | 439 | /* Parse a range of positive 2-component versions delimited by either ':' or |
| 440 | * '-'. The version consists in a major and a minor, both of which must be |
| 441 | * smaller than 65536, because internally they will be represented as a 32-bit |
| 442 | * integer. |
| 443 | * If only one version is read, it is set as both min and max. Just like for |
| 444 | * pure integers, an operator may be specified as the prefix, among this list |
| 445 | * of 5 : |
| 446 | * |
| 447 | * 0:eq, 1:gt, 2:ge, 3:lt, 4:le |
| 448 | * |
| 449 | * The default operator is "eq". It supports range matching. Ranges are |
| 450 | * rejected for other operators. The operator may be changed at any time. |
| 451 | * The operator is stored in the 'opaque' argument. This allows constructs |
| 452 | * such as the following one : |
| 453 | * |
| 454 | * acl obsolete_ssl ssl_req_proto lt 3 |
| 455 | * acl unsupported_ssl ssl_req_proto gt 3.1 |
| 456 | * acl valid_ssl ssl_req_proto 3.0-3.1 |
| 457 | * |
| 458 | */ |
| 459 | int acl_parse_dotted_ver(const char **text, struct acl_pattern *pattern, int *opaque) |
| 460 | { |
| 461 | signed long long i; |
| 462 | unsigned int j, last, skip = 0; |
| 463 | const char *ptr = *text; |
| 464 | |
| 465 | |
| 466 | while (!isdigit((unsigned char)*ptr)) { |
| 467 | if (strcmp(ptr, "eq") == 0) *opaque = 0; |
| 468 | else if (strcmp(ptr, "gt") == 0) *opaque = 1; |
| 469 | else if (strcmp(ptr, "ge") == 0) *opaque = 2; |
| 470 | else if (strcmp(ptr, "lt") == 0) *opaque = 3; |
| 471 | else if (strcmp(ptr, "le") == 0) *opaque = 4; |
| 472 | else |
| 473 | return 0; |
| 474 | |
| 475 | skip++; |
| 476 | ptr = text[skip]; |
| 477 | } |
| 478 | |
| 479 | last = i = 0; |
| 480 | while (1) { |
| 481 | j = *ptr++; |
| 482 | if (j == '.') { |
| 483 | /* minor part */ |
| 484 | if (i >= 65536) |
| 485 | return 0; |
| 486 | i <<= 16; |
| 487 | continue; |
| 488 | } |
| 489 | if ((j == '-' || j == ':') && !last) { |
| 490 | last++; |
| 491 | if (i < 65536) |
| 492 | i <<= 16; |
| 493 | pattern->val.range.min = i; |
| 494 | i = 0; |
| 495 | continue; |
| 496 | } |
| 497 | j -= '0'; |
| 498 | if (j > 9) |
| 499 | // also catches the terminating zero |
| 500 | break; |
| 501 | i = (i & 0xFFFF0000) + (i & 0xFFFF) * 10; |
| 502 | i += j; |
| 503 | } |
| 504 | |
| 505 | /* if we only got a major version, let's shift it now */ |
| 506 | if (i < 65536) |
| 507 | i <<= 16; |
| 508 | |
| 509 | if (last && *opaque >= 1 && *opaque <= 4) |
| 510 | /* having a range with a min or a max is absurd */ |
| 511 | return 0; |
| 512 | |
| 513 | if (!last) |
| 514 | pattern->val.range.min = i; |
| 515 | pattern->val.range.max = i; |
| 516 | |
| 517 | switch (*opaque) { |
| 518 | case 0: /* eq */ |
| 519 | pattern->val.range.min_set = 1; |
| 520 | pattern->val.range.max_set = 1; |
| 521 | break; |
| 522 | case 1: /* gt */ |
| 523 | pattern->val.range.min++; /* gt = ge + 1 */ |
| 524 | case 2: /* ge */ |
| 525 | pattern->val.range.min_set = 1; |
| 526 | pattern->val.range.max_set = 0; |
| 527 | break; |
| 528 | case 3: /* lt */ |
| 529 | pattern->val.range.max--; /* lt = le - 1 */ |
| 530 | case 4: /* le */ |
| 531 | pattern->val.range.min_set = 0; |
| 532 | pattern->val.range.max_set = 1; |
| 533 | break; |
| 534 | } |
| 535 | return skip + 1; |
| 536 | } |
| 537 | |
Willy Tarreau | a67fad9 | 2007-05-08 19:50:09 +0200 | [diff] [blame] | 538 | /* Parse an IP address and an optional mask in the form addr[/mask]. |
| 539 | * The addr may either be an IPv4 address or a hostname. The mask |
| 540 | * may either be a dotted mask or a number of bits. Returns 1 if OK, |
| 541 | * otherwise 0. |
| 542 | */ |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 543 | 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] | 544 | { |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 545 | if (str2net(*text, &pattern->val.ipv4.addr, &pattern->val.ipv4.mask)) |
| 546 | return 1; |
| 547 | else |
| 548 | return 0; |
Willy Tarreau | a67fad9 | 2007-05-08 19:50:09 +0200 | [diff] [blame] | 549 | } |
| 550 | |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 551 | /* |
| 552 | * Registers the ACL keyword list <kwl> as a list of valid keywords for next |
| 553 | * parsing sessions. |
| 554 | */ |
| 555 | void acl_register_keywords(struct acl_kw_list *kwl) |
| 556 | { |
| 557 | LIST_ADDQ(&acl_keywords.list, &kwl->list); |
| 558 | } |
| 559 | |
| 560 | /* |
| 561 | * Unregisters the ACL keyword list <kwl> from the list of valid keywords. |
| 562 | */ |
| 563 | void acl_unregister_keywords(struct acl_kw_list *kwl) |
| 564 | { |
| 565 | LIST_DEL(&kwl->list); |
| 566 | LIST_INIT(&kwl->list); |
| 567 | } |
| 568 | |
| 569 | /* Return a pointer to the ACL <name> within the list starting at <head>, or |
| 570 | * NULL if not found. |
| 571 | */ |
| 572 | struct acl *find_acl_by_name(const char *name, struct list *head) |
| 573 | { |
| 574 | struct acl *acl; |
| 575 | list_for_each_entry(acl, head, list) { |
| 576 | if (strcmp(acl->name, name) == 0) |
| 577 | return acl; |
| 578 | } |
| 579 | return NULL; |
| 580 | } |
| 581 | |
| 582 | /* Return a pointer to the ACL keyword <kw>, or NULL if not found. Note that if |
| 583 | * <kw> contains an opening parenthesis, only the left part of it is checked. |
| 584 | */ |
| 585 | struct acl_keyword *find_acl_kw(const char *kw) |
| 586 | { |
| 587 | int index; |
| 588 | const char *kwend; |
| 589 | struct acl_kw_list *kwl; |
| 590 | |
| 591 | kwend = strchr(kw, '('); |
| 592 | if (!kwend) |
| 593 | kwend = kw + strlen(kw); |
| 594 | |
| 595 | list_for_each_entry(kwl, &acl_keywords.list, list) { |
| 596 | for (index = 0; kwl->kw[index].kw != NULL; index++) { |
| 597 | if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) && |
| 598 | kwl->kw[index].kw[kwend-kw] == 0) |
| 599 | return &kwl->kw[index]; |
| 600 | } |
| 601 | } |
| 602 | return NULL; |
| 603 | } |
| 604 | |
| 605 | static void free_pattern(struct acl_pattern *pat) |
| 606 | { |
Krzysztof Piotr Oledzki | 8001d61 | 2008-05-31 13:53:23 +0200 | [diff] [blame] | 607 | |
| 608 | if (pat->ptr.ptr) { |
| 609 | if (pat->freeptrbuf) |
| 610 | pat->freeptrbuf(pat->ptr.ptr); |
| 611 | |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 612 | free(pat->ptr.ptr); |
Krzysztof Piotr Oledzki | 8001d61 | 2008-05-31 13:53:23 +0200 | [diff] [blame] | 613 | } |
| 614 | |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 615 | free(pat); |
| 616 | } |
| 617 | |
| 618 | static void free_pattern_list(struct list *head) |
| 619 | { |
| 620 | struct acl_pattern *pat, *tmp; |
| 621 | list_for_each_entry_safe(pat, tmp, head, list) |
| 622 | free_pattern(pat); |
| 623 | } |
| 624 | |
| 625 | static struct acl_expr *prune_acl_expr(struct acl_expr *expr) |
| 626 | { |
| 627 | free_pattern_list(&expr->patterns); |
| 628 | LIST_INIT(&expr->patterns); |
| 629 | if (expr->arg.str) |
| 630 | free(expr->arg.str); |
| 631 | expr->kw->use_cnt--; |
| 632 | return expr; |
| 633 | } |
| 634 | |
| 635 | /* Parse an ACL expression starting at <args>[0], and return it. |
| 636 | * Right now, the only accepted syntax is : |
| 637 | * <subject> [<value>...] |
| 638 | */ |
| 639 | struct acl_expr *parse_acl_expr(const char **args) |
| 640 | { |
| 641 | __label__ out_return, out_free_expr, out_free_pattern; |
| 642 | struct acl_expr *expr; |
| 643 | struct acl_keyword *aclkw; |
| 644 | struct acl_pattern *pattern; |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 645 | int opaque, patflags; |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 646 | const char *arg; |
| 647 | |
| 648 | aclkw = find_acl_kw(args[0]); |
| 649 | if (!aclkw || !aclkw->parse) |
| 650 | goto out_return; |
| 651 | |
| 652 | expr = (struct acl_expr *)calloc(1, sizeof(*expr)); |
| 653 | if (!expr) |
| 654 | goto out_return; |
| 655 | |
| 656 | expr->kw = aclkw; |
| 657 | aclkw->use_cnt++; |
| 658 | LIST_INIT(&expr->patterns); |
| 659 | expr->arg.str = NULL; |
Willy Tarreau | bb76891 | 2007-06-10 11:17:01 +0200 | [diff] [blame] | 660 | expr->arg_len = 0; |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 661 | |
| 662 | arg = strchr(args[0], '('); |
| 663 | if (arg != NULL) { |
| 664 | char *end, *arg2; |
| 665 | /* there is an argument in the form "subject(arg)" */ |
| 666 | arg++; |
| 667 | end = strchr(arg, ')'); |
| 668 | if (!end) |
| 669 | goto out_free_expr; |
| 670 | arg2 = (char *)calloc(1, end - arg + 1); |
| 671 | if (!arg2) |
| 672 | goto out_free_expr; |
| 673 | memcpy(arg2, arg, end - arg); |
| 674 | arg2[end-arg] = '\0'; |
Willy Tarreau | bb76891 | 2007-06-10 11:17:01 +0200 | [diff] [blame] | 675 | expr->arg_len = end - arg; |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 676 | expr->arg.str = arg2; |
| 677 | } |
| 678 | |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 679 | args++; |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 680 | |
| 681 | /* check for options before patterns. Supported options are : |
| 682 | * -i : ignore case for all patterns by default |
| 683 | * -f : read patterns from those files |
| 684 | * -- : everything after this is not an option |
| 685 | */ |
| 686 | patflags = 0; |
| 687 | while (**args == '-') { |
| 688 | if ((*args)[1] == 'i') |
| 689 | patflags |= ACL_PAT_F_IGNORE_CASE; |
| 690 | else if ((*args)[1] == 'f') |
| 691 | patflags |= ACL_PAT_F_FROM_FILE; |
| 692 | else if ((*args)[1] == '-') { |
| 693 | args++; |
| 694 | break; |
| 695 | } |
| 696 | else |
| 697 | break; |
| 698 | args++; |
| 699 | } |
| 700 | |
| 701 | /* now parse all patterns */ |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 702 | opaque = 0; |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 703 | while (**args) { |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 704 | int ret; |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 705 | pattern = (struct acl_pattern *)calloc(1, sizeof(*pattern)); |
| 706 | if (!pattern) |
| 707 | goto out_free_expr; |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 708 | pattern->flags = patflags; |
| 709 | |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 710 | ret = aclkw->parse(args, pattern, &opaque); |
| 711 | if (!ret) |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 712 | goto out_free_pattern; |
| 713 | LIST_ADDQ(&expr->patterns, &pattern->list); |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 714 | args += ret; |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 715 | } |
| 716 | |
| 717 | return expr; |
| 718 | |
| 719 | out_free_pattern: |
| 720 | free_pattern(pattern); |
| 721 | out_free_expr: |
| 722 | prune_acl_expr(expr); |
| 723 | free(expr); |
| 724 | out_return: |
| 725 | return NULL; |
| 726 | } |
| 727 | |
Krzysztof Piotr Oledzki | a643baf | 2008-05-29 23:53:44 +0200 | [diff] [blame] | 728 | /* Purge everything in the acl <acl>, then return <acl>. */ |
| 729 | struct acl *prune_acl(struct acl *acl) { |
| 730 | |
| 731 | struct acl_expr *expr, *exprb; |
| 732 | |
| 733 | free(acl->name); |
| 734 | |
| 735 | list_for_each_entry_safe(expr, exprb, &acl->expr, list) { |
| 736 | LIST_DEL(&expr->list); |
| 737 | prune_acl_expr(expr); |
| 738 | free(expr); |
| 739 | } |
| 740 | |
| 741 | return acl; |
| 742 | } |
| 743 | |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 744 | /* Parse an ACL with the name starting at <args>[0], and with a list of already |
| 745 | * known ACLs in <acl>. If the ACL was not in the list, it will be added. |
| 746 | * A pointer to that ACL is returned. |
| 747 | * |
| 748 | * args syntax: <aclname> <acl_expr> |
| 749 | */ |
| 750 | struct acl *parse_acl(const char **args, struct list *known_acl) |
| 751 | { |
| 752 | __label__ out_return, out_free_acl_expr, out_free_name; |
| 753 | struct acl *cur_acl; |
| 754 | struct acl_expr *acl_expr; |
| 755 | char *name; |
| 756 | |
Willy Tarreau | 2e74c3f | 2007-12-02 18:45:09 +0100 | [diff] [blame] | 757 | if (invalid_char(*args)) |
| 758 | goto out_return; |
| 759 | |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 760 | acl_expr = parse_acl_expr(args + 1); |
| 761 | if (!acl_expr) |
| 762 | goto out_return; |
| 763 | |
| 764 | cur_acl = find_acl_by_name(args[0], known_acl); |
| 765 | if (!cur_acl) { |
| 766 | name = strdup(args[0]); |
| 767 | if (!name) |
| 768 | goto out_free_acl_expr; |
| 769 | cur_acl = (struct acl *)calloc(1, sizeof(*cur_acl)); |
| 770 | if (cur_acl == NULL) |
| 771 | goto out_free_name; |
| 772 | |
| 773 | LIST_INIT(&cur_acl->expr); |
| 774 | LIST_ADDQ(known_acl, &cur_acl->list); |
| 775 | cur_acl->name = name; |
| 776 | } |
| 777 | |
Willy Tarreau | a980263 | 2008-07-25 19:13:19 +0200 | [diff] [blame] | 778 | cur_acl->requires |= acl_expr->kw->requires; |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 779 | LIST_ADDQ(&cur_acl->expr, &acl_expr->list); |
| 780 | return cur_acl; |
| 781 | |
| 782 | out_free_name: |
| 783 | free(name); |
| 784 | out_free_acl_expr: |
| 785 | prune_acl_expr(acl_expr); |
| 786 | free(acl_expr); |
| 787 | out_return: |
| 788 | return NULL; |
| 789 | } |
| 790 | |
Willy Tarreau | 16fbe82 | 2007-06-17 11:54:31 +0200 | [diff] [blame] | 791 | /* Some useful ACLs provided by default. Only those used are allocated. */ |
| 792 | |
| 793 | const struct { |
| 794 | const char *name; |
| 795 | const char *expr[4]; /* put enough for longest expression */ |
| 796 | } default_acl_list[] = { |
Willy Tarreau | 58393e1 | 2008-07-20 10:39:22 +0200 | [diff] [blame] | 797 | { .name = "TRUE", .expr = {"always_true",""}}, |
| 798 | { .name = "FALSE", .expr = {"always_false",""}}, |
Willy Tarreau | 16fbe82 | 2007-06-17 11:54:31 +0200 | [diff] [blame] | 799 | { .name = "LOCALHOST", .expr = {"src","127.0.0.1/8",""}}, |
| 800 | { .name = "HTTP_1.0", .expr = {"req_ver","1.0",""}}, |
| 801 | { .name = "HTTP_1.1", .expr = {"req_ver","1.1",""}}, |
| 802 | { .name = "METH_CONNECT", .expr = {"method","CONNECT",""}}, |
| 803 | { .name = "METH_GET", .expr = {"method","GET","HEAD",""}}, |
| 804 | { .name = "METH_HEAD", .expr = {"method","HEAD",""}}, |
| 805 | { .name = "METH_OPTIONS", .expr = {"method","OPTIONS",""}}, |
| 806 | { .name = "METH_POST", .expr = {"method","POST",""}}, |
| 807 | { .name = "METH_TRACE", .expr = {"method","TRACE",""}}, |
| 808 | { .name = "HTTP_URL_ABS", .expr = {"url_reg","^[^/:]*://",""}}, |
| 809 | { .name = "HTTP_URL_SLASH", .expr = {"url_beg","/",""}}, |
| 810 | { .name = "HTTP_URL_STAR", .expr = {"url","*",""}}, |
| 811 | { .name = "HTTP_CONTENT", .expr = {"hdr_val(content-length)","gt","0",""}}, |
Willy Tarreau | c631770 | 2008-07-20 09:29:50 +0200 | [diff] [blame] | 812 | { .name = "REQ_CONTENT", .expr = {"req_len","gt","0",""}}, |
Willy Tarreau | b6fb420 | 2008-07-20 11:18:28 +0200 | [diff] [blame] | 813 | { .name = "WAIT_END", .expr = {"wait_end",""}}, |
Willy Tarreau | 16fbe82 | 2007-06-17 11:54:31 +0200 | [diff] [blame] | 814 | { .name = NULL, .expr = {""}} |
| 815 | }; |
| 816 | |
| 817 | /* Find a default ACL from the default_acl list, compile it and return it. |
| 818 | * If the ACL is not found, NULL is returned. In theory, it cannot fail, |
| 819 | * except when default ACLs are broken, in which case it will return NULL. |
| 820 | * If <known_acl> is not NULL, the ACL will be queued at its tail. |
| 821 | */ |
| 822 | struct acl *find_acl_default(const char *acl_name, struct list *known_acl) |
| 823 | { |
| 824 | __label__ out_return, out_free_acl_expr, out_free_name; |
| 825 | struct acl *cur_acl; |
| 826 | struct acl_expr *acl_expr; |
| 827 | char *name; |
| 828 | int index; |
| 829 | |
| 830 | for (index = 0; default_acl_list[index].name != NULL; index++) { |
| 831 | if (strcmp(acl_name, default_acl_list[index].name) == 0) |
| 832 | break; |
| 833 | } |
| 834 | |
| 835 | if (default_acl_list[index].name == NULL) |
| 836 | return NULL; |
| 837 | |
| 838 | acl_expr = parse_acl_expr((const char **)default_acl_list[index].expr); |
| 839 | if (!acl_expr) |
| 840 | goto out_return; |
| 841 | |
| 842 | name = strdup(acl_name); |
| 843 | if (!name) |
| 844 | goto out_free_acl_expr; |
| 845 | cur_acl = (struct acl *)calloc(1, sizeof(*cur_acl)); |
| 846 | if (cur_acl == NULL) |
| 847 | goto out_free_name; |
| 848 | |
| 849 | cur_acl->name = name; |
| 850 | LIST_INIT(&cur_acl->expr); |
| 851 | LIST_ADDQ(&cur_acl->expr, &acl_expr->list); |
| 852 | if (known_acl) |
| 853 | LIST_ADDQ(known_acl, &cur_acl->list); |
| 854 | |
| 855 | return cur_acl; |
| 856 | |
| 857 | out_free_name: |
| 858 | free(name); |
| 859 | out_free_acl_expr: |
| 860 | prune_acl_expr(acl_expr); |
| 861 | free(acl_expr); |
| 862 | out_return: |
| 863 | return NULL; |
| 864 | } |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 865 | |
| 866 | /* Purge everything in the acl_cond <cond>, then return <cond>. */ |
| 867 | struct acl_cond *prune_acl_cond(struct acl_cond *cond) |
| 868 | { |
| 869 | struct acl_term_suite *suite, *tmp_suite; |
| 870 | struct acl_term *term, *tmp_term; |
| 871 | |
| 872 | /* iterate through all term suites and free all terms and all suites */ |
| 873 | list_for_each_entry_safe(suite, tmp_suite, &cond->suites, list) { |
| 874 | list_for_each_entry_safe(term, tmp_term, &suite->terms, list) |
| 875 | free(term); |
| 876 | free(suite); |
| 877 | } |
| 878 | return cond; |
| 879 | } |
| 880 | |
| 881 | /* Parse an ACL condition starting at <args>[0], relying on a list of already |
| 882 | * known ACLs passed in <known_acl>. The new condition is returned (or NULL in |
| 883 | * case of low memory). Supports multiple conditions separated by "or". |
| 884 | */ |
| 885 | struct acl_cond *parse_acl_cond(const char **args, struct list *known_acl, int pol) |
| 886 | { |
| 887 | __label__ out_return, out_free_suite, out_free_term; |
Willy Tarreau | 74b98a8 | 2007-06-16 19:35:18 +0200 | [diff] [blame] | 888 | int arg, neg; |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 889 | const char *word; |
| 890 | struct acl *cur_acl; |
| 891 | struct acl_term *cur_term; |
| 892 | struct acl_term_suite *cur_suite; |
| 893 | struct acl_cond *cond; |
| 894 | |
| 895 | cond = (struct acl_cond *)calloc(1, sizeof(*cond)); |
| 896 | if (cond == NULL) |
| 897 | goto out_return; |
| 898 | |
| 899 | LIST_INIT(&cond->list); |
| 900 | LIST_INIT(&cond->suites); |
| 901 | cond->pol = pol; |
| 902 | |
| 903 | cur_suite = NULL; |
Willy Tarreau | 74b98a8 | 2007-06-16 19:35:18 +0200 | [diff] [blame] | 904 | neg = 0; |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 905 | for (arg = 0; *args[arg]; arg++) { |
| 906 | word = args[arg]; |
| 907 | |
| 908 | /* remove as many exclamation marks as we can */ |
| 909 | while (*word == '!') { |
| 910 | neg = !neg; |
| 911 | word++; |
| 912 | } |
| 913 | |
| 914 | /* an empty word is allowed because we cannot force the user to |
| 915 | * always think about not leaving exclamation marks alone. |
| 916 | */ |
| 917 | if (!*word) |
| 918 | continue; |
| 919 | |
Willy Tarreau | 16fbe82 | 2007-06-17 11:54:31 +0200 | [diff] [blame] | 920 | if (strcasecmp(word, "or") == 0 || strcmp(word, "||") == 0) { |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 921 | /* new term suite */ |
| 922 | cur_suite = NULL; |
| 923 | neg = 0; |
| 924 | continue; |
| 925 | } |
| 926 | |
Willy Tarreau | 16fbe82 | 2007-06-17 11:54:31 +0200 | [diff] [blame] | 927 | /* search for <word> in the known ACL names. If we do not find |
| 928 | * it, let's look for it in the default ACLs, and if found, add |
| 929 | * it to the list of ACLs of this proxy. This makes it possible |
| 930 | * to override them. |
| 931 | */ |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 932 | cur_acl = find_acl_by_name(word, known_acl); |
Willy Tarreau | 16fbe82 | 2007-06-17 11:54:31 +0200 | [diff] [blame] | 933 | if (cur_acl == NULL) { |
| 934 | cur_acl = find_acl_default(word, known_acl); |
| 935 | if (cur_acl == NULL) |
| 936 | goto out_free_suite; |
| 937 | } |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 938 | |
| 939 | cur_term = (struct acl_term *)calloc(1, sizeof(*cur_term)); |
| 940 | if (cur_term == NULL) |
| 941 | goto out_free_suite; |
| 942 | |
| 943 | cur_term->acl = cur_acl; |
| 944 | cur_term->neg = neg; |
Willy Tarreau | a980263 | 2008-07-25 19:13:19 +0200 | [diff] [blame] | 945 | cond->requires |= cur_acl->requires; |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 946 | |
| 947 | if (!cur_suite) { |
| 948 | cur_suite = (struct acl_term_suite *)calloc(1, sizeof(*cur_suite)); |
| 949 | if (cur_term == NULL) |
| 950 | goto out_free_term; |
| 951 | LIST_INIT(&cur_suite->terms); |
| 952 | LIST_ADDQ(&cond->suites, &cur_suite->list); |
| 953 | } |
| 954 | LIST_ADDQ(&cur_suite->terms, &cur_term->list); |
Willy Tarreau | 74b98a8 | 2007-06-16 19:35:18 +0200 | [diff] [blame] | 955 | neg = 0; |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 956 | } |
| 957 | |
| 958 | return cond; |
| 959 | |
| 960 | out_free_term: |
| 961 | free(cur_term); |
| 962 | out_free_suite: |
| 963 | prune_acl_cond(cond); |
| 964 | free(cond); |
| 965 | out_return: |
| 966 | return NULL; |
| 967 | } |
| 968 | |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 969 | /* Execute condition <cond> and return either ACL_PAT_FAIL, ACL_PAT_MISS or |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 970 | * ACL_PAT_PASS depending on the test results. ACL_PAT_MISS may only be |
| 971 | * returned if <dir> contains ACL_PARTIAL, indicating that incomplete data |
| 972 | * is being examined. |
| 973 | * This function only computes the condition, it does not apply the polarity |
| 974 | * required by IF/UNLESS, it's up to the caller to do this using something like |
| 975 | * this : |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 976 | * |
| 977 | * res = acl_pass(res); |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 978 | * if (res == ACL_PAT_MISS) |
| 979 | * return 0; |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 980 | * if (cond->pol == ACL_COND_UNLESS) |
| 981 | * res = !res; |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 982 | */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 983 | 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] | 984 | { |
| 985 | __label__ fetch_next; |
| 986 | struct acl_term_suite *suite; |
| 987 | struct acl_term *term; |
| 988 | struct acl_expr *expr; |
| 989 | struct acl *acl; |
| 990 | struct acl_pattern *pattern; |
| 991 | struct acl_test test; |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 992 | int acl_res, suite_res, cond_res; |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 993 | |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 994 | /* We're doing a logical OR between conditions so we initialize to FAIL. |
| 995 | * The MISS status is propagated down from the suites. |
| 996 | */ |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 997 | cond_res = ACL_PAT_FAIL; |
| 998 | list_for_each_entry(suite, &cond->suites, list) { |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 999 | /* Evaluate condition suite <suite>. We stop at the first term |
| 1000 | * which returns ACL_PAT_FAIL. The MISS status is still propagated |
| 1001 | * in case of uncertainty in the result. |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 1002 | */ |
| 1003 | |
| 1004 | /* we're doing a logical AND between terms, so we must set the |
| 1005 | * initial value to PASS. |
| 1006 | */ |
| 1007 | suite_res = ACL_PAT_PASS; |
| 1008 | list_for_each_entry(term, &suite->terms, list) { |
| 1009 | acl = term->acl; |
| 1010 | |
| 1011 | /* FIXME: use cache ! |
| 1012 | * check acl->cache_idx for this. |
| 1013 | */ |
| 1014 | |
| 1015 | /* ACL result not cached. Let's scan all the expressions |
| 1016 | * and use the first one to match. |
| 1017 | */ |
| 1018 | acl_res = ACL_PAT_FAIL; |
| 1019 | list_for_each_entry(expr, &acl->expr, list) { |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 1020 | /* we need to reset context and flags */ |
| 1021 | memset(&test, 0, sizeof(test)); |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 1022 | fetch_next: |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1023 | if (!expr->kw->fetch(px, l4, l7, dir, expr, &test)) { |
| 1024 | /* maybe we could not fetch because of missing data */ |
| 1025 | if (test.flags & ACL_TEST_F_MAY_CHANGE && dir & ACL_PARTIAL) |
| 1026 | acl_res |= ACL_PAT_MISS; |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 1027 | continue; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1028 | } |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 1029 | |
Willy Tarreau | a79534f | 2008-07-20 10:13:37 +0200 | [diff] [blame] | 1030 | if (test.flags & ACL_TEST_F_RES_SET) { |
| 1031 | if (test.flags & ACL_TEST_F_RES_PASS) |
| 1032 | acl_res |= ACL_PAT_PASS; |
| 1033 | else |
| 1034 | acl_res |= ACL_PAT_FAIL; |
| 1035 | } |
| 1036 | else { |
| 1037 | /* call the match() function for all tests on this value */ |
| 1038 | list_for_each_entry(pattern, &expr->patterns, list) { |
| 1039 | acl_res |= expr->kw->match(&test, pattern); |
| 1040 | if (acl_res == ACL_PAT_PASS) |
| 1041 | break; |
| 1042 | } |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 1043 | } |
| 1044 | /* |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 1045 | * OK now acl_res holds the result of this expression |
| 1046 | * as one of ACL_PAT_FAIL, ACL_PAT_MISS or ACL_PAT_PASS. |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 1047 | * |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 1048 | * Then if (!MISS) we can cache the result, and put |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 1049 | * (test.flags & ACL_TEST_F_VOLATILE) in the cache flags. |
| 1050 | * |
| 1051 | * FIXME: implement cache. |
| 1052 | * |
| 1053 | */ |
| 1054 | |
| 1055 | /* now we may have some cleanup to do */ |
| 1056 | if (test.flags & ACL_TEST_F_MUST_FREE) { |
| 1057 | free(test.ptr); |
| 1058 | test.len = 0; |
| 1059 | } |
| 1060 | |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 1061 | /* we're ORing these terms, so a single PASS is enough */ |
| 1062 | if (acl_res == ACL_PAT_PASS) |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 1063 | break; |
| 1064 | |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 1065 | if (test.flags & ACL_TEST_F_FETCH_MORE) |
| 1066 | goto fetch_next; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1067 | |
| 1068 | /* sometimes we know the fetched data is subject to change |
| 1069 | * later and give another chance for a new match (eg: request |
| 1070 | * size, time, ...) |
| 1071 | */ |
| 1072 | if (test.flags & ACL_TEST_F_MAY_CHANGE && dir & ACL_PARTIAL) |
| 1073 | acl_res |= ACL_PAT_MISS; |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 1074 | } |
| 1075 | /* |
| 1076 | * Here we have the result of an ACL (cached or not). |
| 1077 | * ACLs are combined, negated or not, to form conditions. |
| 1078 | */ |
| 1079 | |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 1080 | if (term->neg) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 1081 | acl_res = acl_neg(acl_res); |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 1082 | |
| 1083 | suite_res &= acl_res; |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 1084 | |
| 1085 | /* we're ANDing these terms, so a single FAIL is enough */ |
| 1086 | if (suite_res == ACL_PAT_FAIL) |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 1087 | break; |
| 1088 | } |
| 1089 | cond_res |= suite_res; |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 1090 | |
| 1091 | /* we're ORing these terms, so a single PASS is enough */ |
| 1092 | if (cond_res == ACL_PAT_PASS) |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 1093 | break; |
| 1094 | } |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 1095 | return cond_res; |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 1096 | } |
| 1097 | |
| 1098 | |
Willy Tarreau | dd64f8d | 2008-07-27 22:02:32 +0200 | [diff] [blame] | 1099 | /* Reports a pointer to the first ACL used in condition <cond> which requires |
| 1100 | * at least one of the USE_FLAGS in <require>. Returns NULL if none matches. |
| 1101 | * The construct is almost the same as for acl_exec_cond() since we're walking |
| 1102 | * down the ACL tree as well. It is important that the tree is really walked |
| 1103 | * through and never cached, because that way, this function can be used as a |
| 1104 | * late check. |
| 1105 | */ |
| 1106 | struct acl *cond_find_require(struct acl_cond *cond, unsigned int require) |
| 1107 | { |
| 1108 | struct acl_term_suite *suite; |
| 1109 | struct acl_term *term; |
| 1110 | struct acl *acl; |
| 1111 | |
| 1112 | list_for_each_entry(suite, &cond->suites, list) { |
| 1113 | list_for_each_entry(term, &suite->terms, list) { |
| 1114 | acl = term->acl; |
| 1115 | if (acl->requires & require) |
| 1116 | return acl; |
| 1117 | } |
| 1118 | } |
| 1119 | return NULL; |
| 1120 | } |
| 1121 | |
| 1122 | |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 1123 | /************************************************************************/ |
| 1124 | /* All supported keywords must be declared here. */ |
| 1125 | /************************************************************************/ |
| 1126 | |
| 1127 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 1128 | static struct acl_kw_list acl_kws = {{ },{ |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 1129 | { "always_true", acl_parse_nothing, acl_fetch_true, acl_match_nothing, ACL_USE_NOTHING }, |
| 1130 | { "always_false", acl_parse_nothing, acl_fetch_false, acl_match_nothing, ACL_USE_NOTHING }, |
| 1131 | { "wait_end", acl_parse_nothing, acl_fetch_wait_end, acl_match_nothing, ACL_USE_NOTHING }, |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 1132 | #if 0 |
| 1133 | { "time", acl_parse_time, acl_fetch_time, acl_match_time }, |
| 1134 | #endif |
| 1135 | { NULL, NULL, NULL, NULL } |
| 1136 | }}; |
| 1137 | |
| 1138 | |
| 1139 | __attribute__((constructor)) |
| 1140 | static void __acl_init(void) |
| 1141 | { |
| 1142 | acl_register_keywords(&acl_kws); |
| 1143 | } |
| 1144 | |
| 1145 | |
| 1146 | /* |
| 1147 | * Local variables: |
| 1148 | * c-indent-level: 8 |
| 1149 | * c-basic-offset: 8 |
| 1150 | * End: |
| 1151 | */ |