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 | |
| 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 | { |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 56 | return ACL_PAT_PASS; |
Willy Tarreau | a590983 | 2007-06-17 20:40:25 +0200 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | /* always return false */ |
| 60 | static int |
| 61 | acl_match_false(struct acl_test *test, struct acl_pattern *pattern) |
| 62 | { |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 63 | return ACL_PAT_FAIL; |
Willy Tarreau | a590983 | 2007-06-17 20:40:25 +0200 | [diff] [blame] | 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) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 73 | return ACL_PAT_FAIL; |
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 | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 78 | return ACL_PAT_PASS; |
| 79 | return ACL_PAT_FAIL; |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 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) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 97 | return ACL_PAT_FAIL; |
Willy Tarreau | f3d2598 | 2007-05-08 22:45:09 +0200 | [diff] [blame] | 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) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 112 | ret = ACL_PAT_PASS; |
Willy Tarreau | f3d2598 | 2007-05-08 22:45:09 +0200 | [diff] [blame] | 113 | else |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 114 | ret = ACL_PAT_FAIL; |
Willy Tarreau | f3d2598 | 2007-05-08 22:45:09 +0200 | [diff] [blame] | 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) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 126 | return ACL_PAT_FAIL; |
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 | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 131 | return ACL_PAT_FAIL; |
| 132 | return ACL_PAT_PASS; |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 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) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 141 | return ACL_PAT_FAIL; |
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 | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 145 | return ACL_PAT_FAIL; |
| 146 | return ACL_PAT_PASS; |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 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) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 159 | return ACL_PAT_FAIL; |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 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) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 168 | return ACL_PAT_PASS; |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 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) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 175 | return ACL_PAT_PASS; |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 176 | } |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 177 | } |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 178 | return ACL_PAT_FAIL; |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 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) |
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 | 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] == '?')) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 223 | return ACL_PAT_PASS; |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 224 | } else { |
| 225 | if ((*c == *ps) && |
| 226 | (strncmp(ps, c, pl) == 0) && |
| 227 | (c == end || c[pl] == '/' || c[pl] == delim || c[pl] == '?')) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 228 | return ACL_PAT_PASS; |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 229 | } |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 230 | may_match = 0; |
| 231 | } |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 232 | return ACL_PAT_FAIL; |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 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 | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 258 | return ACL_PAT_PASS; |
| 259 | return ACL_PAT_FAIL; |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 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) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 267 | return ACL_PAT_FAIL; |
Willy Tarreau | a67fad9 | 2007-05-08 19:50:09 +0200 | [diff] [blame] | 268 | |
| 269 | s = (void *)test->ptr; |
| 270 | 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] | 271 | return ACL_PAT_PASS; |
| 272 | return ACL_PAT_FAIL; |
Willy Tarreau | a67fad9 | 2007-05-08 19:50:09 +0200 | [diff] [blame] | 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 | |
Krzysztof Piotr Oledzki | 8001d61 | 2008-05-31 13:53:23 +0200 | [diff] [blame] | 288 | /* Free data allocated by acl_parse_reg */ |
| 289 | static void acl_free_reg(void *ptr) { |
| 290 | |
| 291 | regfree((regex_t *)ptr); |
| 292 | } |
| 293 | |
Willy Tarreau | f3d2598 | 2007-05-08 22:45:09 +0200 | [diff] [blame] | 294 | /* Parse a regex. It is allocated. */ |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 295 | 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] | 296 | { |
| 297 | regex_t *preg; |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 298 | int icase; |
Willy Tarreau | f3d2598 | 2007-05-08 22:45:09 +0200 | [diff] [blame] | 299 | |
| 300 | preg = calloc(1, sizeof(regex_t)); |
| 301 | |
| 302 | if (!preg) |
| 303 | return 0; |
| 304 | |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 305 | icase = (pattern->flags & ACL_PAT_F_IGNORE_CASE) ? REG_ICASE : 0; |
| 306 | if (regcomp(preg, *text, REG_EXTENDED | REG_NOSUB | icase) != 0) { |
Willy Tarreau | f3d2598 | 2007-05-08 22:45:09 +0200 | [diff] [blame] | 307 | free(preg); |
| 308 | return 0; |
| 309 | } |
| 310 | |
| 311 | pattern->ptr.reg = preg; |
Krzysztof Piotr Oledzki | 8001d61 | 2008-05-31 13:53:23 +0200 | [diff] [blame] | 312 | pattern->freeptrbuf = &acl_free_reg; |
Willy Tarreau | f3d2598 | 2007-05-08 22:45:09 +0200 | [diff] [blame] | 313 | return 1; |
| 314 | } |
| 315 | |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 316 | /* Parse a range of positive integers delimited by either ':' or '-'. If only |
| 317 | * one integer is read, it is set as both min and max. An operator may be |
| 318 | * specified as the prefix, among this list of 5 : |
| 319 | * |
| 320 | * 0:eq, 1:gt, 2:ge, 3:lt, 4:le |
| 321 | * |
| 322 | * The default operator is "eq". It supports range matching. Ranges are |
| 323 | * rejected for other operators. The operator may be changed at any time. |
| 324 | * The operator is stored in the 'opaque' argument. |
| 325 | * |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 326 | */ |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 327 | 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] | 328 | { |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 329 | signed long long i; |
| 330 | unsigned int j, last, skip = 0; |
| 331 | const char *ptr = *text; |
| 332 | |
| 333 | |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 334 | while (!isdigit((unsigned char)*ptr)) { |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 335 | if (strcmp(ptr, "eq") == 0) *opaque = 0; |
| 336 | else if (strcmp(ptr, "gt") == 0) *opaque = 1; |
| 337 | else if (strcmp(ptr, "ge") == 0) *opaque = 2; |
| 338 | else if (strcmp(ptr, "lt") == 0) *opaque = 3; |
| 339 | else if (strcmp(ptr, "le") == 0) *opaque = 4; |
| 340 | else |
| 341 | return 0; |
| 342 | |
| 343 | skip++; |
| 344 | ptr = text[skip]; |
| 345 | } |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 346 | |
| 347 | last = i = 0; |
| 348 | while (1) { |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 349 | j = *ptr++; |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 350 | if ((j == '-' || j == ':') && !last) { |
| 351 | last++; |
| 352 | pattern->val.range.min = i; |
| 353 | i = 0; |
| 354 | continue; |
| 355 | } |
| 356 | j -= '0'; |
| 357 | if (j > 9) |
| 358 | // also catches the terminating zero |
| 359 | break; |
| 360 | i *= 10; |
| 361 | i += j; |
| 362 | } |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 363 | |
| 364 | if (last && *opaque >= 1 && *opaque <= 4) |
| 365 | /* having a range with a min or a max is absurd */ |
| 366 | return 0; |
| 367 | |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 368 | if (!last) |
| 369 | pattern->val.range.min = i; |
| 370 | pattern->val.range.max = i; |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 371 | |
| 372 | switch (*opaque) { |
| 373 | case 0: /* eq */ |
| 374 | pattern->val.range.min_set = 1; |
| 375 | pattern->val.range.max_set = 1; |
| 376 | break; |
| 377 | case 1: /* gt */ |
| 378 | pattern->val.range.min++; /* gt = ge + 1 */ |
| 379 | case 2: /* ge */ |
| 380 | pattern->val.range.min_set = 1; |
| 381 | pattern->val.range.max_set = 0; |
| 382 | break; |
| 383 | case 3: /* lt */ |
| 384 | pattern->val.range.max--; /* lt = le - 1 */ |
| 385 | case 4: /* le */ |
| 386 | pattern->val.range.min_set = 0; |
| 387 | pattern->val.range.max_set = 1; |
| 388 | break; |
| 389 | } |
| 390 | return skip + 1; |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 391 | } |
| 392 | |
Willy Tarreau | 4a26d2f | 2008-07-15 16:05:33 +0200 | [diff] [blame] | 393 | /* Parse a range of positive 2-component versions delimited by either ':' or |
| 394 | * '-'. The version consists in a major and a minor, both of which must be |
| 395 | * smaller than 65536, because internally they will be represented as a 32-bit |
| 396 | * integer. |
| 397 | * If only one version is read, it is set as both min and max. Just like for |
| 398 | * pure integers, an operator may be specified as the prefix, among this list |
| 399 | * of 5 : |
| 400 | * |
| 401 | * 0:eq, 1:gt, 2:ge, 3:lt, 4:le |
| 402 | * |
| 403 | * The default operator is "eq". It supports range matching. Ranges are |
| 404 | * rejected for other operators. The operator may be changed at any time. |
| 405 | * The operator is stored in the 'opaque' argument. This allows constructs |
| 406 | * such as the following one : |
| 407 | * |
| 408 | * acl obsolete_ssl ssl_req_proto lt 3 |
| 409 | * acl unsupported_ssl ssl_req_proto gt 3.1 |
| 410 | * acl valid_ssl ssl_req_proto 3.0-3.1 |
| 411 | * |
| 412 | */ |
| 413 | int acl_parse_dotted_ver(const char **text, struct acl_pattern *pattern, int *opaque) |
| 414 | { |
| 415 | signed long long i; |
| 416 | unsigned int j, last, skip = 0; |
| 417 | const char *ptr = *text; |
| 418 | |
| 419 | |
| 420 | while (!isdigit((unsigned char)*ptr)) { |
| 421 | if (strcmp(ptr, "eq") == 0) *opaque = 0; |
| 422 | else if (strcmp(ptr, "gt") == 0) *opaque = 1; |
| 423 | else if (strcmp(ptr, "ge") == 0) *opaque = 2; |
| 424 | else if (strcmp(ptr, "lt") == 0) *opaque = 3; |
| 425 | else if (strcmp(ptr, "le") == 0) *opaque = 4; |
| 426 | else |
| 427 | return 0; |
| 428 | |
| 429 | skip++; |
| 430 | ptr = text[skip]; |
| 431 | } |
| 432 | |
| 433 | last = i = 0; |
| 434 | while (1) { |
| 435 | j = *ptr++; |
| 436 | if (j == '.') { |
| 437 | /* minor part */ |
| 438 | if (i >= 65536) |
| 439 | return 0; |
| 440 | i <<= 16; |
| 441 | continue; |
| 442 | } |
| 443 | if ((j == '-' || j == ':') && !last) { |
| 444 | last++; |
| 445 | if (i < 65536) |
| 446 | i <<= 16; |
| 447 | pattern->val.range.min = i; |
| 448 | i = 0; |
| 449 | continue; |
| 450 | } |
| 451 | j -= '0'; |
| 452 | if (j > 9) |
| 453 | // also catches the terminating zero |
| 454 | break; |
| 455 | i = (i & 0xFFFF0000) + (i & 0xFFFF) * 10; |
| 456 | i += j; |
| 457 | } |
| 458 | |
| 459 | /* if we only got a major version, let's shift it now */ |
| 460 | if (i < 65536) |
| 461 | i <<= 16; |
| 462 | |
| 463 | if (last && *opaque >= 1 && *opaque <= 4) |
| 464 | /* having a range with a min or a max is absurd */ |
| 465 | return 0; |
| 466 | |
| 467 | if (!last) |
| 468 | pattern->val.range.min = i; |
| 469 | pattern->val.range.max = i; |
| 470 | |
| 471 | switch (*opaque) { |
| 472 | case 0: /* eq */ |
| 473 | pattern->val.range.min_set = 1; |
| 474 | pattern->val.range.max_set = 1; |
| 475 | break; |
| 476 | case 1: /* gt */ |
| 477 | pattern->val.range.min++; /* gt = ge + 1 */ |
| 478 | case 2: /* ge */ |
| 479 | pattern->val.range.min_set = 1; |
| 480 | pattern->val.range.max_set = 0; |
| 481 | break; |
| 482 | case 3: /* lt */ |
| 483 | pattern->val.range.max--; /* lt = le - 1 */ |
| 484 | case 4: /* le */ |
| 485 | pattern->val.range.min_set = 0; |
| 486 | pattern->val.range.max_set = 1; |
| 487 | break; |
| 488 | } |
| 489 | return skip + 1; |
| 490 | } |
| 491 | |
Willy Tarreau | a67fad9 | 2007-05-08 19:50:09 +0200 | [diff] [blame] | 492 | /* Parse an IP address and an optional mask in the form addr[/mask]. |
| 493 | * The addr may either be an IPv4 address or a hostname. The mask |
| 494 | * may either be a dotted mask or a number of bits. Returns 1 if OK, |
| 495 | * otherwise 0. |
| 496 | */ |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 497 | 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] | 498 | { |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 499 | if (str2net(*text, &pattern->val.ipv4.addr, &pattern->val.ipv4.mask)) |
| 500 | return 1; |
| 501 | else |
| 502 | return 0; |
Willy Tarreau | a67fad9 | 2007-05-08 19:50:09 +0200 | [diff] [blame] | 503 | } |
| 504 | |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 505 | /* |
| 506 | * Registers the ACL keyword list <kwl> as a list of valid keywords for next |
| 507 | * parsing sessions. |
| 508 | */ |
| 509 | void acl_register_keywords(struct acl_kw_list *kwl) |
| 510 | { |
| 511 | LIST_ADDQ(&acl_keywords.list, &kwl->list); |
| 512 | } |
| 513 | |
| 514 | /* |
| 515 | * Unregisters the ACL keyword list <kwl> from the list of valid keywords. |
| 516 | */ |
| 517 | void acl_unregister_keywords(struct acl_kw_list *kwl) |
| 518 | { |
| 519 | LIST_DEL(&kwl->list); |
| 520 | LIST_INIT(&kwl->list); |
| 521 | } |
| 522 | |
| 523 | /* Return a pointer to the ACL <name> within the list starting at <head>, or |
| 524 | * NULL if not found. |
| 525 | */ |
| 526 | struct acl *find_acl_by_name(const char *name, struct list *head) |
| 527 | { |
| 528 | struct acl *acl; |
| 529 | list_for_each_entry(acl, head, list) { |
| 530 | if (strcmp(acl->name, name) == 0) |
| 531 | return acl; |
| 532 | } |
| 533 | return NULL; |
| 534 | } |
| 535 | |
| 536 | /* Return a pointer to the ACL keyword <kw>, or NULL if not found. Note that if |
| 537 | * <kw> contains an opening parenthesis, only the left part of it is checked. |
| 538 | */ |
| 539 | struct acl_keyword *find_acl_kw(const char *kw) |
| 540 | { |
| 541 | int index; |
| 542 | const char *kwend; |
| 543 | struct acl_kw_list *kwl; |
| 544 | |
| 545 | kwend = strchr(kw, '('); |
| 546 | if (!kwend) |
| 547 | kwend = kw + strlen(kw); |
| 548 | |
| 549 | list_for_each_entry(kwl, &acl_keywords.list, list) { |
| 550 | for (index = 0; kwl->kw[index].kw != NULL; index++) { |
| 551 | if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) && |
| 552 | kwl->kw[index].kw[kwend-kw] == 0) |
| 553 | return &kwl->kw[index]; |
| 554 | } |
| 555 | } |
| 556 | return NULL; |
| 557 | } |
| 558 | |
| 559 | static void free_pattern(struct acl_pattern *pat) |
| 560 | { |
Krzysztof Piotr Oledzki | 8001d61 | 2008-05-31 13:53:23 +0200 | [diff] [blame] | 561 | |
| 562 | if (pat->ptr.ptr) { |
| 563 | if (pat->freeptrbuf) |
| 564 | pat->freeptrbuf(pat->ptr.ptr); |
| 565 | |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 566 | free(pat->ptr.ptr); |
Krzysztof Piotr Oledzki | 8001d61 | 2008-05-31 13:53:23 +0200 | [diff] [blame] | 567 | } |
| 568 | |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 569 | free(pat); |
| 570 | } |
| 571 | |
| 572 | static void free_pattern_list(struct list *head) |
| 573 | { |
| 574 | struct acl_pattern *pat, *tmp; |
| 575 | list_for_each_entry_safe(pat, tmp, head, list) |
| 576 | free_pattern(pat); |
| 577 | } |
| 578 | |
| 579 | static struct acl_expr *prune_acl_expr(struct acl_expr *expr) |
| 580 | { |
| 581 | free_pattern_list(&expr->patterns); |
| 582 | LIST_INIT(&expr->patterns); |
| 583 | if (expr->arg.str) |
| 584 | free(expr->arg.str); |
| 585 | expr->kw->use_cnt--; |
| 586 | return expr; |
| 587 | } |
| 588 | |
| 589 | /* Parse an ACL expression starting at <args>[0], and return it. |
| 590 | * Right now, the only accepted syntax is : |
| 591 | * <subject> [<value>...] |
| 592 | */ |
| 593 | struct acl_expr *parse_acl_expr(const char **args) |
| 594 | { |
| 595 | __label__ out_return, out_free_expr, out_free_pattern; |
| 596 | struct acl_expr *expr; |
| 597 | struct acl_keyword *aclkw; |
| 598 | struct acl_pattern *pattern; |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 599 | int opaque, patflags; |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 600 | const char *arg; |
| 601 | |
| 602 | aclkw = find_acl_kw(args[0]); |
| 603 | if (!aclkw || !aclkw->parse) |
| 604 | goto out_return; |
| 605 | |
| 606 | expr = (struct acl_expr *)calloc(1, sizeof(*expr)); |
| 607 | if (!expr) |
| 608 | goto out_return; |
| 609 | |
| 610 | expr->kw = aclkw; |
| 611 | aclkw->use_cnt++; |
| 612 | LIST_INIT(&expr->patterns); |
| 613 | expr->arg.str = NULL; |
Willy Tarreau | bb76891 | 2007-06-10 11:17:01 +0200 | [diff] [blame] | 614 | expr->arg_len = 0; |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 615 | |
| 616 | arg = strchr(args[0], '('); |
| 617 | if (arg != NULL) { |
| 618 | char *end, *arg2; |
| 619 | /* there is an argument in the form "subject(arg)" */ |
| 620 | arg++; |
| 621 | end = strchr(arg, ')'); |
| 622 | if (!end) |
| 623 | goto out_free_expr; |
| 624 | arg2 = (char *)calloc(1, end - arg + 1); |
| 625 | if (!arg2) |
| 626 | goto out_free_expr; |
| 627 | memcpy(arg2, arg, end - arg); |
| 628 | arg2[end-arg] = '\0'; |
Willy Tarreau | bb76891 | 2007-06-10 11:17:01 +0200 | [diff] [blame] | 629 | expr->arg_len = end - arg; |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 630 | expr->arg.str = arg2; |
| 631 | } |
| 632 | |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 633 | args++; |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 634 | |
| 635 | /* check for options before patterns. Supported options are : |
| 636 | * -i : ignore case for all patterns by default |
| 637 | * -f : read patterns from those files |
| 638 | * -- : everything after this is not an option |
| 639 | */ |
| 640 | patflags = 0; |
| 641 | while (**args == '-') { |
| 642 | if ((*args)[1] == 'i') |
| 643 | patflags |= ACL_PAT_F_IGNORE_CASE; |
| 644 | else if ((*args)[1] == 'f') |
| 645 | patflags |= ACL_PAT_F_FROM_FILE; |
| 646 | else if ((*args)[1] == '-') { |
| 647 | args++; |
| 648 | break; |
| 649 | } |
| 650 | else |
| 651 | break; |
| 652 | args++; |
| 653 | } |
| 654 | |
| 655 | /* now parse all patterns */ |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 656 | opaque = 0; |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 657 | while (**args) { |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 658 | int ret; |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 659 | pattern = (struct acl_pattern *)calloc(1, sizeof(*pattern)); |
| 660 | if (!pattern) |
| 661 | goto out_free_expr; |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 662 | pattern->flags = patflags; |
| 663 | |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 664 | ret = aclkw->parse(args, pattern, &opaque); |
| 665 | if (!ret) |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 666 | goto out_free_pattern; |
| 667 | LIST_ADDQ(&expr->patterns, &pattern->list); |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 668 | args += ret; |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 669 | } |
| 670 | |
| 671 | return expr; |
| 672 | |
| 673 | out_free_pattern: |
| 674 | free_pattern(pattern); |
| 675 | out_free_expr: |
| 676 | prune_acl_expr(expr); |
| 677 | free(expr); |
| 678 | out_return: |
| 679 | return NULL; |
| 680 | } |
| 681 | |
Krzysztof Piotr Oledzki | a643baf | 2008-05-29 23:53:44 +0200 | [diff] [blame] | 682 | /* Purge everything in the acl <acl>, then return <acl>. */ |
| 683 | struct acl *prune_acl(struct acl *acl) { |
| 684 | |
| 685 | struct acl_expr *expr, *exprb; |
| 686 | |
| 687 | free(acl->name); |
| 688 | |
| 689 | list_for_each_entry_safe(expr, exprb, &acl->expr, list) { |
| 690 | LIST_DEL(&expr->list); |
| 691 | prune_acl_expr(expr); |
| 692 | free(expr); |
| 693 | } |
| 694 | |
| 695 | return acl; |
| 696 | } |
| 697 | |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 698 | /* Parse an ACL with the name starting at <args>[0], and with a list of already |
| 699 | * known ACLs in <acl>. If the ACL was not in the list, it will be added. |
| 700 | * A pointer to that ACL is returned. |
| 701 | * |
| 702 | * args syntax: <aclname> <acl_expr> |
| 703 | */ |
| 704 | struct acl *parse_acl(const char **args, struct list *known_acl) |
| 705 | { |
| 706 | __label__ out_return, out_free_acl_expr, out_free_name; |
| 707 | struct acl *cur_acl; |
| 708 | struct acl_expr *acl_expr; |
| 709 | char *name; |
| 710 | |
Willy Tarreau | 2e74c3f | 2007-12-02 18:45:09 +0100 | [diff] [blame] | 711 | if (invalid_char(*args)) |
| 712 | goto out_return; |
| 713 | |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 714 | acl_expr = parse_acl_expr(args + 1); |
| 715 | if (!acl_expr) |
| 716 | goto out_return; |
| 717 | |
| 718 | cur_acl = find_acl_by_name(args[0], known_acl); |
| 719 | if (!cur_acl) { |
| 720 | name = strdup(args[0]); |
| 721 | if (!name) |
| 722 | goto out_free_acl_expr; |
| 723 | cur_acl = (struct acl *)calloc(1, sizeof(*cur_acl)); |
| 724 | if (cur_acl == NULL) |
| 725 | goto out_free_name; |
| 726 | |
| 727 | LIST_INIT(&cur_acl->expr); |
| 728 | LIST_ADDQ(known_acl, &cur_acl->list); |
| 729 | cur_acl->name = name; |
| 730 | } |
| 731 | |
| 732 | LIST_ADDQ(&cur_acl->expr, &acl_expr->list); |
| 733 | return cur_acl; |
| 734 | |
| 735 | out_free_name: |
| 736 | free(name); |
| 737 | out_free_acl_expr: |
| 738 | prune_acl_expr(acl_expr); |
| 739 | free(acl_expr); |
| 740 | out_return: |
| 741 | return NULL; |
| 742 | } |
| 743 | |
Willy Tarreau | 16fbe82 | 2007-06-17 11:54:31 +0200 | [diff] [blame] | 744 | /* Some useful ACLs provided by default. Only those used are allocated. */ |
| 745 | |
| 746 | const struct { |
| 747 | const char *name; |
| 748 | const char *expr[4]; /* put enough for longest expression */ |
| 749 | } default_acl_list[] = { |
Willy Tarreau | a590983 | 2007-06-17 20:40:25 +0200 | [diff] [blame] | 750 | { .name = "TRUE", .expr = {"always_true","1",""}}, |
| 751 | { .name = "FALSE", .expr = {"always_false","0",""}}, |
Willy Tarreau | 16fbe82 | 2007-06-17 11:54:31 +0200 | [diff] [blame] | 752 | { .name = "LOCALHOST", .expr = {"src","127.0.0.1/8",""}}, |
| 753 | { .name = "HTTP_1.0", .expr = {"req_ver","1.0",""}}, |
| 754 | { .name = "HTTP_1.1", .expr = {"req_ver","1.1",""}}, |
| 755 | { .name = "METH_CONNECT", .expr = {"method","CONNECT",""}}, |
| 756 | { .name = "METH_GET", .expr = {"method","GET","HEAD",""}}, |
| 757 | { .name = "METH_HEAD", .expr = {"method","HEAD",""}}, |
| 758 | { .name = "METH_OPTIONS", .expr = {"method","OPTIONS",""}}, |
| 759 | { .name = "METH_POST", .expr = {"method","POST",""}}, |
| 760 | { .name = "METH_TRACE", .expr = {"method","TRACE",""}}, |
| 761 | { .name = "HTTP_URL_ABS", .expr = {"url_reg","^[^/:]*://",""}}, |
| 762 | { .name = "HTTP_URL_SLASH", .expr = {"url_beg","/",""}}, |
| 763 | { .name = "HTTP_URL_STAR", .expr = {"url","*",""}}, |
| 764 | { .name = "HTTP_CONTENT", .expr = {"hdr_val(content-length)","gt","0",""}}, |
| 765 | { .name = NULL, .expr = {""}} |
| 766 | }; |
| 767 | |
| 768 | /* Find a default ACL from the default_acl list, compile it and return it. |
| 769 | * If the ACL is not found, NULL is returned. In theory, it cannot fail, |
| 770 | * except when default ACLs are broken, in which case it will return NULL. |
| 771 | * If <known_acl> is not NULL, the ACL will be queued at its tail. |
| 772 | */ |
| 773 | struct acl *find_acl_default(const char *acl_name, struct list *known_acl) |
| 774 | { |
| 775 | __label__ out_return, out_free_acl_expr, out_free_name; |
| 776 | struct acl *cur_acl; |
| 777 | struct acl_expr *acl_expr; |
| 778 | char *name; |
| 779 | int index; |
| 780 | |
| 781 | for (index = 0; default_acl_list[index].name != NULL; index++) { |
| 782 | if (strcmp(acl_name, default_acl_list[index].name) == 0) |
| 783 | break; |
| 784 | } |
| 785 | |
| 786 | if (default_acl_list[index].name == NULL) |
| 787 | return NULL; |
| 788 | |
| 789 | acl_expr = parse_acl_expr((const char **)default_acl_list[index].expr); |
| 790 | if (!acl_expr) |
| 791 | goto out_return; |
| 792 | |
| 793 | name = strdup(acl_name); |
| 794 | if (!name) |
| 795 | goto out_free_acl_expr; |
| 796 | cur_acl = (struct acl *)calloc(1, sizeof(*cur_acl)); |
| 797 | if (cur_acl == NULL) |
| 798 | goto out_free_name; |
| 799 | |
| 800 | cur_acl->name = name; |
| 801 | LIST_INIT(&cur_acl->expr); |
| 802 | LIST_ADDQ(&cur_acl->expr, &acl_expr->list); |
| 803 | if (known_acl) |
| 804 | LIST_ADDQ(known_acl, &cur_acl->list); |
| 805 | |
| 806 | return cur_acl; |
| 807 | |
| 808 | out_free_name: |
| 809 | free(name); |
| 810 | out_free_acl_expr: |
| 811 | prune_acl_expr(acl_expr); |
| 812 | free(acl_expr); |
| 813 | out_return: |
| 814 | return NULL; |
| 815 | } |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 816 | |
| 817 | /* Purge everything in the acl_cond <cond>, then return <cond>. */ |
| 818 | struct acl_cond *prune_acl_cond(struct acl_cond *cond) |
| 819 | { |
| 820 | struct acl_term_suite *suite, *tmp_suite; |
| 821 | struct acl_term *term, *tmp_term; |
| 822 | |
| 823 | /* iterate through all term suites and free all terms and all suites */ |
| 824 | list_for_each_entry_safe(suite, tmp_suite, &cond->suites, list) { |
| 825 | list_for_each_entry_safe(term, tmp_term, &suite->terms, list) |
| 826 | free(term); |
| 827 | free(suite); |
| 828 | } |
| 829 | return cond; |
| 830 | } |
| 831 | |
| 832 | /* Parse an ACL condition starting at <args>[0], relying on a list of already |
| 833 | * known ACLs passed in <known_acl>. The new condition is returned (or NULL in |
| 834 | * case of low memory). Supports multiple conditions separated by "or". |
| 835 | */ |
| 836 | struct acl_cond *parse_acl_cond(const char **args, struct list *known_acl, int pol) |
| 837 | { |
| 838 | __label__ out_return, out_free_suite, out_free_term; |
Willy Tarreau | 74b98a8 | 2007-06-16 19:35:18 +0200 | [diff] [blame] | 839 | int arg, neg; |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 840 | const char *word; |
| 841 | struct acl *cur_acl; |
| 842 | struct acl_term *cur_term; |
| 843 | struct acl_term_suite *cur_suite; |
| 844 | struct acl_cond *cond; |
| 845 | |
| 846 | cond = (struct acl_cond *)calloc(1, sizeof(*cond)); |
| 847 | if (cond == NULL) |
| 848 | goto out_return; |
| 849 | |
| 850 | LIST_INIT(&cond->list); |
| 851 | LIST_INIT(&cond->suites); |
| 852 | cond->pol = pol; |
| 853 | |
| 854 | cur_suite = NULL; |
Willy Tarreau | 74b98a8 | 2007-06-16 19:35:18 +0200 | [diff] [blame] | 855 | neg = 0; |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 856 | for (arg = 0; *args[arg]; arg++) { |
| 857 | word = args[arg]; |
| 858 | |
| 859 | /* remove as many exclamation marks as we can */ |
| 860 | while (*word == '!') { |
| 861 | neg = !neg; |
| 862 | word++; |
| 863 | } |
| 864 | |
| 865 | /* an empty word is allowed because we cannot force the user to |
| 866 | * always think about not leaving exclamation marks alone. |
| 867 | */ |
| 868 | if (!*word) |
| 869 | continue; |
| 870 | |
Willy Tarreau | 16fbe82 | 2007-06-17 11:54:31 +0200 | [diff] [blame] | 871 | if (strcasecmp(word, "or") == 0 || strcmp(word, "||") == 0) { |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 872 | /* new term suite */ |
| 873 | cur_suite = NULL; |
| 874 | neg = 0; |
| 875 | continue; |
| 876 | } |
| 877 | |
Willy Tarreau | 16fbe82 | 2007-06-17 11:54:31 +0200 | [diff] [blame] | 878 | /* search for <word> in the known ACL names. If we do not find |
| 879 | * it, let's look for it in the default ACLs, and if found, add |
| 880 | * it to the list of ACLs of this proxy. This makes it possible |
| 881 | * to override them. |
| 882 | */ |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 883 | cur_acl = find_acl_by_name(word, known_acl); |
Willy Tarreau | 16fbe82 | 2007-06-17 11:54:31 +0200 | [diff] [blame] | 884 | if (cur_acl == NULL) { |
| 885 | cur_acl = find_acl_default(word, known_acl); |
| 886 | if (cur_acl == NULL) |
| 887 | goto out_free_suite; |
| 888 | } |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 889 | |
| 890 | cur_term = (struct acl_term *)calloc(1, sizeof(*cur_term)); |
| 891 | if (cur_term == NULL) |
| 892 | goto out_free_suite; |
| 893 | |
| 894 | cur_term->acl = cur_acl; |
| 895 | cur_term->neg = neg; |
| 896 | |
| 897 | if (!cur_suite) { |
| 898 | cur_suite = (struct acl_term_suite *)calloc(1, sizeof(*cur_suite)); |
| 899 | if (cur_term == NULL) |
| 900 | goto out_free_term; |
| 901 | LIST_INIT(&cur_suite->terms); |
| 902 | LIST_ADDQ(&cond->suites, &cur_suite->list); |
| 903 | } |
| 904 | LIST_ADDQ(&cur_suite->terms, &cur_term->list); |
Willy Tarreau | 74b98a8 | 2007-06-16 19:35:18 +0200 | [diff] [blame] | 905 | neg = 0; |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 906 | } |
| 907 | |
| 908 | return cond; |
| 909 | |
| 910 | out_free_term: |
| 911 | free(cur_term); |
| 912 | out_free_suite: |
| 913 | prune_acl_cond(cond); |
| 914 | free(cond); |
| 915 | out_return: |
| 916 | return NULL; |
| 917 | } |
| 918 | |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 919 | /* 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] | 920 | * ACL_PAT_PASS depending on the test results. ACL_PAT_MISS may only be |
| 921 | * returned if <dir> contains ACL_PARTIAL, indicating that incomplete data |
| 922 | * is being examined. |
| 923 | * This function only computes the condition, it does not apply the polarity |
| 924 | * required by IF/UNLESS, it's up to the caller to do this using something like |
| 925 | * this : |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 926 | * |
| 927 | * res = acl_pass(res); |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 928 | * if (res == ACL_PAT_MISS) |
| 929 | * return 0; |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 930 | * if (cond->pol == ACL_COND_UNLESS) |
| 931 | * res = !res; |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 932 | */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 933 | 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] | 934 | { |
| 935 | __label__ fetch_next; |
| 936 | struct acl_term_suite *suite; |
| 937 | struct acl_term *term; |
| 938 | struct acl_expr *expr; |
| 939 | struct acl *acl; |
| 940 | struct acl_pattern *pattern; |
| 941 | struct acl_test test; |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 942 | int acl_res, suite_res, cond_res; |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 943 | |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 944 | /* We're doing a logical OR between conditions so we initialize to FAIL. |
| 945 | * The MISS status is propagated down from the suites. |
| 946 | */ |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 947 | cond_res = ACL_PAT_FAIL; |
| 948 | list_for_each_entry(suite, &cond->suites, list) { |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 949 | /* Evaluate condition suite <suite>. We stop at the first term |
| 950 | * which returns ACL_PAT_FAIL. The MISS status is still propagated |
| 951 | * in case of uncertainty in the result. |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 952 | */ |
| 953 | |
| 954 | /* we're doing a logical AND between terms, so we must set the |
| 955 | * initial value to PASS. |
| 956 | */ |
| 957 | suite_res = ACL_PAT_PASS; |
| 958 | list_for_each_entry(term, &suite->terms, list) { |
| 959 | acl = term->acl; |
| 960 | |
| 961 | /* FIXME: use cache ! |
| 962 | * check acl->cache_idx for this. |
| 963 | */ |
| 964 | |
| 965 | /* ACL result not cached. Let's scan all the expressions |
| 966 | * and use the first one to match. |
| 967 | */ |
| 968 | acl_res = ACL_PAT_FAIL; |
| 969 | list_for_each_entry(expr, &acl->expr, list) { |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 970 | /* we need to reset context and flags */ |
| 971 | memset(&test, 0, sizeof(test)); |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 972 | fetch_next: |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 973 | if (!expr->kw->fetch(px, l4, l7, dir, expr, &test)) { |
| 974 | /* maybe we could not fetch because of missing data */ |
| 975 | if (test.flags & ACL_TEST_F_MAY_CHANGE && dir & ACL_PARTIAL) |
| 976 | acl_res |= ACL_PAT_MISS; |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 977 | continue; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 978 | } |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 979 | |
| 980 | /* apply all tests to this value */ |
| 981 | list_for_each_entry(pattern, &expr->patterns, list) { |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 982 | acl_res |= expr->kw->match(&test, pattern); |
| 983 | if (acl_res == ACL_PAT_PASS) |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 984 | break; |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 985 | } |
| 986 | /* |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 987 | * OK now acl_res holds the result of this expression |
| 988 | * 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] | 989 | * |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 990 | * Then if (!MISS) we can cache the result, and put |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 991 | * (test.flags & ACL_TEST_F_VOLATILE) in the cache flags. |
| 992 | * |
| 993 | * FIXME: implement cache. |
| 994 | * |
| 995 | */ |
| 996 | |
| 997 | /* now we may have some cleanup to do */ |
| 998 | if (test.flags & ACL_TEST_F_MUST_FREE) { |
| 999 | free(test.ptr); |
| 1000 | test.len = 0; |
| 1001 | } |
| 1002 | |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 1003 | /* we're ORing these terms, so a single PASS is enough */ |
| 1004 | if (acl_res == ACL_PAT_PASS) |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 1005 | break; |
| 1006 | |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 1007 | if (test.flags & ACL_TEST_F_FETCH_MORE) |
| 1008 | goto fetch_next; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1009 | |
| 1010 | /* sometimes we know the fetched data is subject to change |
| 1011 | * later and give another chance for a new match (eg: request |
| 1012 | * size, time, ...) |
| 1013 | */ |
| 1014 | if (test.flags & ACL_TEST_F_MAY_CHANGE && dir & ACL_PARTIAL) |
| 1015 | acl_res |= ACL_PAT_MISS; |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 1016 | } |
| 1017 | /* |
| 1018 | * Here we have the result of an ACL (cached or not). |
| 1019 | * ACLs are combined, negated or not, to form conditions. |
| 1020 | */ |
| 1021 | |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 1022 | if (term->neg) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 1023 | acl_res = acl_neg(acl_res); |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 1024 | |
| 1025 | suite_res &= acl_res; |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 1026 | |
| 1027 | /* we're ANDing these terms, so a single FAIL is enough */ |
| 1028 | if (suite_res == ACL_PAT_FAIL) |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 1029 | break; |
| 1030 | } |
| 1031 | cond_res |= suite_res; |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 1032 | |
| 1033 | /* we're ORing these terms, so a single PASS is enough */ |
| 1034 | if (cond_res == ACL_PAT_PASS) |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 1035 | break; |
| 1036 | } |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 1037 | return cond_res; |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 1038 | } |
| 1039 | |
| 1040 | |
| 1041 | /************************************************************************/ |
| 1042 | /* All supported keywords must be declared here. */ |
| 1043 | /************************************************************************/ |
| 1044 | |
| 1045 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 1046 | static struct acl_kw_list acl_kws = {{ },{ |
Willy Tarreau | a590983 | 2007-06-17 20:40:25 +0200 | [diff] [blame] | 1047 | { "always_true", acl_parse_nothing, acl_fetch_nothing, acl_match_true }, |
| 1048 | { "always_false", acl_parse_nothing, acl_fetch_nothing, acl_match_false }, |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 1049 | #if 0 |
| 1050 | { "time", acl_parse_time, acl_fetch_time, acl_match_time }, |
| 1051 | #endif |
| 1052 | { NULL, NULL, NULL, NULL } |
| 1053 | }}; |
| 1054 | |
| 1055 | |
| 1056 | __attribute__((constructor)) |
| 1057 | static void __acl_init(void) |
| 1058 | { |
| 1059 | acl_register_keywords(&acl_kws); |
| 1060 | } |
| 1061 | |
| 1062 | |
| 1063 | /* |
| 1064 | * Local variables: |
| 1065 | * c-indent-level: 8 |
| 1066 | * c-basic-offset: 8 |
| 1067 | * End: |
| 1068 | */ |