blob: be752b069feb18aa3b549729e7ef2c2eacb75a87 [file] [log] [blame]
Willy Tarreaua84d3742007-05-07 00:36:48 +02001/*
2 * ACL management functions.
3 *
Willy Tarreau11382812008-07-09 16:18:21 +02004 * Copyright 2000-2008 Willy Tarreau <w@1wt.eu>
Willy Tarreaua84d3742007-05-07 00:36:48 +02005 *
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 Tarreauae8b7962007-06-09 23:10:04 +020013#include <ctype.h>
Willy Tarreaua84d3742007-05-07 00:36:48 +020014#include <stdio.h>
15#include <string.h>
16
17#include <common/config.h>
18#include <common/mini-clist.h>
19#include <common/standard.h>
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +010020#include <common/uri_auth.h>
Willy Tarreaua84d3742007-05-07 00:36:48 +020021
22#include <proto/acl.h>
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010023#include <proto/auth.h>
Willy Tarreau404e8ab2009-07-26 19:40:40 +020024#include <proto/log.h>
Willy Tarreaua84d3742007-05-07 00:36:48 +020025
Willy Tarreaua9802632008-07-25 19:13:19 +020026/* The capabilities of filtering hooks describe the type of information
27 * available to each of them.
28 */
29const unsigned int filt_cap[] = {
30 [ACL_HOOK_REQ_FE_TCP] = ACL_USE_TCP4_ANY|ACL_USE_TCP6_ANY|ACL_USE_TCP_ANY,
31 [ACL_HOOK_REQ_FE_TCP_CONTENT] = ACL_USE_TCP4_ANY|ACL_USE_TCP6_ANY|ACL_USE_TCP_ANY|ACL_USE_L4REQ_ANY,
32 [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,
33 [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,
34 [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,
35 [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,
36 [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,
37 [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,
38 [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,
39
40 [ACL_HOOK_RTR_BE_TCP_CONTENT] = ACL_USE_REQ_PERMANENT|ACL_USE_REQ_CACHEABLE|ACL_USE_L4RTR_ANY,
41 [ACL_HOOK_RTR_BE_HTTP_IN] = ACL_USE_REQ_PERMANENT|ACL_USE_REQ_CACHEABLE|ACL_USE_L4RTR_ANY|ACL_USE_L7RTR_ANY,
42 [ACL_HOOK_RTR_FE_TCP_CONTENT] = ACL_USE_REQ_PERMANENT|ACL_USE_REQ_CACHEABLE|ACL_USE_L4RTR_ANY|ACL_USE_L7RTR_ANY,
43 [ACL_HOOK_RTR_FE_HTTP_IN] = ACL_USE_REQ_PERMANENT|ACL_USE_REQ_CACHEABLE|ACL_USE_L4RTR_ANY|ACL_USE_L7RTR_ANY,
44 [ACL_HOOK_RTR_BE_HTTP_OUT] = ACL_USE_REQ_PERMANENT|ACL_USE_REQ_CACHEABLE|ACL_USE_L4RTR_ANY|ACL_USE_L7RTR_ANY,
45 [ACL_HOOK_RTR_FE_HTTP_OUT] = ACL_USE_REQ_PERMANENT|ACL_USE_REQ_CACHEABLE|ACL_USE_L4RTR_ANY|ACL_USE_L7RTR_ANY,
46};
47
Willy Tarreaua84d3742007-05-07 00:36:48 +020048/* List head of all known ACL keywords */
49static struct acl_kw_list acl_keywords = {
50 .list = LIST_HEAD_INIT(acl_keywords.list)
51};
52
53
Willy Tarreaua5909832007-06-17 20:40:25 +020054/*
55 * These functions are only used for debugging complex configurations.
Willy Tarreaua84d3742007-05-07 00:36:48 +020056 */
Willy Tarreaua5909832007-06-17 20:40:25 +020057
Willy Tarreau58393e12008-07-20 10:39:22 +020058/* force TRUE to be returned at the fetch level */
Willy Tarreaua5909832007-06-17 20:40:25 +020059static int
Willy Tarreau58393e12008-07-20 10:39:22 +020060acl_fetch_true(struct proxy *px, struct session *l4, void *l7, int dir,
61 struct acl_expr *expr, struct acl_test *test)
Willy Tarreaua5909832007-06-17 20:40:25 +020062{
Willy Tarreau58393e12008-07-20 10:39:22 +020063 test->flags |= ACL_TEST_F_SET_RES_PASS;
Willy Tarreaua5909832007-06-17 20:40:25 +020064 return 1;
65}
66
Willy Tarreaub6fb4202008-07-20 11:18:28 +020067/* wait for more data as long as possible, then return TRUE. This should be
68 * used with content inspection.
69 */
70static int
71acl_fetch_wait_end(struct proxy *px, struct session *l4, void *l7, int dir,
72 struct acl_expr *expr, struct acl_test *test)
73{
74 if (dir & ACL_PARTIAL) {
75 test->flags |= ACL_TEST_F_MAY_CHANGE;
76 return 0;
77 }
78 test->flags |= ACL_TEST_F_SET_RES_PASS;
79 return 1;
80}
81
Willy Tarreau58393e12008-07-20 10:39:22 +020082/* force FALSE to be returned at the fetch level */
Willy Tarreaua5909832007-06-17 20:40:25 +020083static int
Willy Tarreau58393e12008-07-20 10:39:22 +020084acl_fetch_false(struct proxy *px, struct session *l4, void *l7, int dir,
85 struct acl_expr *expr, struct acl_test *test)
Willy Tarreaua84d3742007-05-07 00:36:48 +020086{
Willy Tarreau58393e12008-07-20 10:39:22 +020087 test->flags |= ACL_TEST_F_SET_RES_FAIL;
Willy Tarreaua84d3742007-05-07 00:36:48 +020088 return 1;
89}
90
Willy Tarreau58393e12008-07-20 10:39:22 +020091
92/*
93 * These functions are exported and may be used by any other component.
94 */
95
96/* ignore the current line */
97int acl_parse_nothing(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreaua5909832007-06-17 20:40:25 +020098{
Willy Tarreau58393e12008-07-20 10:39:22 +020099 return 1;
100}
101
102/* always fake a data retrieval */
103int acl_fetch_nothing(struct proxy *px, struct session *l4, void *l7, int dir,
104 struct acl_expr *expr, struct acl_test *test)
105{
106 return 1;
Willy Tarreaua5909832007-06-17 20:40:25 +0200107}
108
109/* always return false */
Willy Tarreau58393e12008-07-20 10:39:22 +0200110int acl_match_nothing(struct acl_test *test, struct acl_pattern *pattern)
Willy Tarreaua5909832007-06-17 20:40:25 +0200111{
Willy Tarreau11382812008-07-09 16:18:21 +0200112 return ACL_PAT_FAIL;
Willy Tarreaua5909832007-06-17 20:40:25 +0200113}
114
115
Willy Tarreaua84d3742007-05-07 00:36:48 +0200116/* NB: For two strings to be identical, it is required that their lengths match */
117int acl_match_str(struct acl_test *test, struct acl_pattern *pattern)
118{
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200119 int icase;
120
Willy Tarreaua84d3742007-05-07 00:36:48 +0200121 if (pattern->len != test->len)
Willy Tarreau11382812008-07-09 16:18:21 +0200122 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200123
124 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
125 if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) == 0) ||
126 (!icase && strncmp(pattern->ptr.str, test->ptr, test->len) == 0))
Willy Tarreau11382812008-07-09 16:18:21 +0200127 return ACL_PAT_PASS;
128 return ACL_PAT_FAIL;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200129}
130
Willy Tarreauf3d25982007-05-08 22:45:09 +0200131/* Executes a regex. It needs to change the data. If it is marked READ_ONLY
132 * then it will be allocated and duplicated in place so that others may use
133 * it later on. Note that this is embarrassing because we always try to avoid
134 * allocating memory at run time.
135 */
136int acl_match_reg(struct acl_test *test, struct acl_pattern *pattern)
137{
138 char old_char;
139 int ret;
140
141 if (unlikely(test->flags & ACL_TEST_F_READ_ONLY)) {
142 char *new_str;
143
144 new_str = calloc(1, test->len + 1);
145 if (!new_str)
Willy Tarreau11382812008-07-09 16:18:21 +0200146 return ACL_PAT_FAIL;
Willy Tarreauf3d25982007-05-08 22:45:09 +0200147
148 memcpy(new_str, test->ptr, test->len);
149 new_str[test->len] = 0;
150 if (test->flags & ACL_TEST_F_MUST_FREE)
151 free(test->ptr);
152 test->ptr = new_str;
153 test->flags |= ACL_TEST_F_MUST_FREE;
154 test->flags &= ~ACL_TEST_F_READ_ONLY;
155 }
156
157 old_char = test->ptr[test->len];
158 test->ptr[test->len] = 0;
159
160 if (regexec(pattern->ptr.reg, test->ptr, 0, NULL, 0) == 0)
Willy Tarreau11382812008-07-09 16:18:21 +0200161 ret = ACL_PAT_PASS;
Willy Tarreauf3d25982007-05-08 22:45:09 +0200162 else
Willy Tarreau11382812008-07-09 16:18:21 +0200163 ret = ACL_PAT_FAIL;
Willy Tarreauf3d25982007-05-08 22:45:09 +0200164
165 test->ptr[test->len] = old_char;
166 return ret;
167}
168
Willy Tarreaua84d3742007-05-07 00:36:48 +0200169/* Checks that the pattern matches the beginning of the tested string. */
170int acl_match_beg(struct acl_test *test, struct acl_pattern *pattern)
171{
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200172 int icase;
173
Willy Tarreaua84d3742007-05-07 00:36:48 +0200174 if (pattern->len > test->len)
Willy Tarreau11382812008-07-09 16:18:21 +0200175 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200176
177 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
178 if ((icase && strncasecmp(pattern->ptr.str, test->ptr, pattern->len) != 0) ||
179 (!icase && strncmp(pattern->ptr.str, test->ptr, pattern->len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +0200180 return ACL_PAT_FAIL;
181 return ACL_PAT_PASS;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200182}
183
184/* Checks that the pattern matches the end of the tested string. */
185int acl_match_end(struct acl_test *test, struct acl_pattern *pattern)
186{
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200187 int icase;
188
Willy Tarreaua84d3742007-05-07 00:36:48 +0200189 if (pattern->len > test->len)
Willy Tarreau11382812008-07-09 16:18:21 +0200190 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200191 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
192 if ((icase && strncasecmp(pattern->ptr.str, test->ptr + test->len - pattern->len, pattern->len) != 0) ||
193 (!icase && strncmp(pattern->ptr.str, test->ptr + test->len - pattern->len, pattern->len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +0200194 return ACL_PAT_FAIL;
195 return ACL_PAT_PASS;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200196}
197
198/* Checks that the pattern is included inside the tested string.
199 * NB: Suboptimal, should be rewritten using a Boyer-Moore method.
200 */
201int acl_match_sub(struct acl_test *test, struct acl_pattern *pattern)
202{
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200203 int icase;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200204 char *end;
205 char *c;
206
207 if (pattern->len > test->len)
Willy Tarreau11382812008-07-09 16:18:21 +0200208 return ACL_PAT_FAIL;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200209
210 end = test->ptr + test->len - pattern->len;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200211 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
212 if (icase) {
213 for (c = test->ptr; c <= end; c++) {
214 if (tolower(*c) != tolower(*pattern->ptr.str))
215 continue;
216 if (strncasecmp(pattern->ptr.str, c, pattern->len) == 0)
Willy Tarreau11382812008-07-09 16:18:21 +0200217 return ACL_PAT_PASS;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200218 }
219 } else {
220 for (c = test->ptr; c <= end; c++) {
221 if (*c != *pattern->ptr.str)
222 continue;
223 if (strncmp(pattern->ptr.str, c, pattern->len) == 0)
Willy Tarreau11382812008-07-09 16:18:21 +0200224 return ACL_PAT_PASS;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200225 }
Willy Tarreaua84d3742007-05-07 00:36:48 +0200226 }
Willy Tarreau11382812008-07-09 16:18:21 +0200227 return ACL_PAT_FAIL;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200228}
229
230/* This one is used by other real functions. It checks that the pattern is
231 * included inside the tested string, but enclosed between the specified
232 * delimitor, or a '/' or a '?' or at the beginning or end of the string.
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200233 * The delimitor is stripped at the beginning or end of the pattern.
Willy Tarreaua84d3742007-05-07 00:36:48 +0200234 */
235static int match_word(struct acl_test *test, struct acl_pattern *pattern, char delim)
236{
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200237 int may_match, icase;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200238 char *c, *end;
239 char *ps;
240 int pl;
241
242 pl = pattern->len;
243 ps = pattern->ptr.str;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200244 while (pl > 0 && (*ps == delim || *ps == '/' || *ps == '?')) {
Willy Tarreaua84d3742007-05-07 00:36:48 +0200245 pl--;
246 ps++;
247 }
248
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200249 while (pl > 0 &&
250 (ps[pl - 1] == delim || ps[pl - 1] == '/' || ps[pl - 1] == '?'))
Willy Tarreaua84d3742007-05-07 00:36:48 +0200251 pl--;
252
253 if (pl > test->len)
Willy Tarreau11382812008-07-09 16:18:21 +0200254 return ACL_PAT_FAIL;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200255
256 may_match = 1;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200257 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200258 end = test->ptr + test->len - pl;
259 for (c = test->ptr; c <= end; c++) {
260 if (*c == '/' || *c == delim || *c == '?') {
261 may_match = 1;
262 continue;
263 }
Willy Tarreaua84d3742007-05-07 00:36:48 +0200264
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200265 if (!may_match)
266 continue;
267
268 if (icase) {
269 if ((tolower(*c) == tolower(*ps)) &&
270 (strncasecmp(ps, c, pl) == 0) &&
271 (c == end || c[pl] == '/' || c[pl] == delim || c[pl] == '?'))
Willy Tarreau11382812008-07-09 16:18:21 +0200272 return ACL_PAT_PASS;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200273 } else {
274 if ((*c == *ps) &&
275 (strncmp(ps, c, pl) == 0) &&
276 (c == end || c[pl] == '/' || c[pl] == delim || c[pl] == '?'))
Willy Tarreau11382812008-07-09 16:18:21 +0200277 return ACL_PAT_PASS;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200278 }
Willy Tarreaua84d3742007-05-07 00:36:48 +0200279 may_match = 0;
280 }
Willy Tarreau11382812008-07-09 16:18:21 +0200281 return ACL_PAT_FAIL;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200282}
283
284/* Checks that the pattern is included inside the tested string, but enclosed
285 * between slashes or at the beginning or end of the string. Slashes at the
286 * beginning or end of the pattern are ignored.
287 */
288int acl_match_dir(struct acl_test *test, struct acl_pattern *pattern)
289{
290 return match_word(test, pattern, '/');
291}
292
293/* Checks that the pattern is included inside the tested string, but enclosed
294 * between dots or at the beginning or end of the string. Dots at the beginning
295 * or end of the pattern are ignored.
296 */
297int acl_match_dom(struct acl_test *test, struct acl_pattern *pattern)
298{
299 return match_word(test, pattern, '.');
300}
301
302/* Checks that the integer in <test> is included between min and max */
Willy Tarreauae8b7962007-06-09 23:10:04 +0200303int acl_match_int(struct acl_test *test, struct acl_pattern *pattern)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200304{
Willy Tarreauae8b7962007-06-09 23:10:04 +0200305 if ((!pattern->val.range.min_set || pattern->val.range.min <= test->i) &&
306 (!pattern->val.range.max_set || test->i <= pattern->val.range.max))
Willy Tarreau11382812008-07-09 16:18:21 +0200307 return ACL_PAT_PASS;
308 return ACL_PAT_FAIL;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200309}
310
Willy Tarreaua67fad92007-05-08 19:50:09 +0200311int acl_match_ip(struct acl_test *test, struct acl_pattern *pattern)
312{
313 struct in_addr *s;
314
315 if (test->i != AF_INET)
Willy Tarreau11382812008-07-09 16:18:21 +0200316 return ACL_PAT_FAIL;
Willy Tarreaua67fad92007-05-08 19:50:09 +0200317
318 s = (void *)test->ptr;
319 if (((s->s_addr ^ pattern->val.ipv4.addr.s_addr) & pattern->val.ipv4.mask.s_addr) == 0)
Willy Tarreau11382812008-07-09 16:18:21 +0200320 return ACL_PAT_PASS;
321 return ACL_PAT_FAIL;
Willy Tarreaua67fad92007-05-08 19:50:09 +0200322}
323
Willy Tarreaua84d3742007-05-07 00:36:48 +0200324/* Parse a string. It is allocated and duplicated. */
Willy Tarreauae8b7962007-06-09 23:10:04 +0200325int acl_parse_str(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200326{
327 int len;
328
Willy Tarreauae8b7962007-06-09 23:10:04 +0200329 len = strlen(*text);
330 pattern->ptr.str = strdup(*text);
Willy Tarreaua84d3742007-05-07 00:36:48 +0200331 if (!pattern->ptr.str)
332 return 0;
333 pattern->len = len;
334 return 1;
335}
336
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +0100337/* Parse and concatenate all further strings into one. */
338int
339acl_parse_strcat(const char **text, struct acl_pattern *pattern, int *opaque)
340{
341
342 int len = 0, i;
343 char *s;
344
345 for (i = 0; *text[i]; i++)
346 len += strlen(text[i])+1;
347
348 pattern->ptr.str = s = calloc(1, len);
349 if (!pattern->ptr.str)
350 return 0;
351
352 for (i = 0; *text[i]; i++)
353 s += sprintf(s, i?" %s":"%s", text[i]);
354
355 pattern->len = len;
356
357 return i;
358}
359
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +0200360/* Free data allocated by acl_parse_reg */
361static void acl_free_reg(void *ptr) {
362
363 regfree((regex_t *)ptr);
364}
365
Willy Tarreauf3d25982007-05-08 22:45:09 +0200366/* Parse a regex. It is allocated. */
Willy Tarreauae8b7962007-06-09 23:10:04 +0200367int acl_parse_reg(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreauf3d25982007-05-08 22:45:09 +0200368{
369 regex_t *preg;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200370 int icase;
Willy Tarreauf3d25982007-05-08 22:45:09 +0200371
372 preg = calloc(1, sizeof(regex_t));
373
374 if (!preg)
375 return 0;
376
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200377 icase = (pattern->flags & ACL_PAT_F_IGNORE_CASE) ? REG_ICASE : 0;
378 if (regcomp(preg, *text, REG_EXTENDED | REG_NOSUB | icase) != 0) {
Willy Tarreauf3d25982007-05-08 22:45:09 +0200379 free(preg);
380 return 0;
381 }
382
383 pattern->ptr.reg = preg;
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +0200384 pattern->freeptrbuf = &acl_free_reg;
Willy Tarreauf3d25982007-05-08 22:45:09 +0200385 return 1;
386}
387
Willy Tarreauae8b7962007-06-09 23:10:04 +0200388/* Parse a range of positive integers delimited by either ':' or '-'. If only
389 * one integer is read, it is set as both min and max. An operator may be
390 * specified as the prefix, among this list of 5 :
391 *
392 * 0:eq, 1:gt, 2:ge, 3:lt, 4:le
393 *
394 * The default operator is "eq". It supports range matching. Ranges are
395 * rejected for other operators. The operator may be changed at any time.
396 * The operator is stored in the 'opaque' argument.
397 *
Willy Tarreaua84d3742007-05-07 00:36:48 +0200398 */
Willy Tarreauae8b7962007-06-09 23:10:04 +0200399int acl_parse_int(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200400{
Willy Tarreauae8b7962007-06-09 23:10:04 +0200401 signed long long i;
402 unsigned int j, last, skip = 0;
403 const char *ptr = *text;
404
405
Willy Tarreau8f8e6452007-06-17 21:51:38 +0200406 while (!isdigit((unsigned char)*ptr)) {
Willy Tarreauae8b7962007-06-09 23:10:04 +0200407 if (strcmp(ptr, "eq") == 0) *opaque = 0;
408 else if (strcmp(ptr, "gt") == 0) *opaque = 1;
409 else if (strcmp(ptr, "ge") == 0) *opaque = 2;
410 else if (strcmp(ptr, "lt") == 0) *opaque = 3;
411 else if (strcmp(ptr, "le") == 0) *opaque = 4;
412 else
413 return 0;
414
415 skip++;
416 ptr = text[skip];
417 }
Willy Tarreaua84d3742007-05-07 00:36:48 +0200418
419 last = i = 0;
420 while (1) {
Willy Tarreauae8b7962007-06-09 23:10:04 +0200421 j = *ptr++;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200422 if ((j == '-' || j == ':') && !last) {
423 last++;
424 pattern->val.range.min = i;
425 i = 0;
426 continue;
427 }
428 j -= '0';
429 if (j > 9)
430 // also catches the terminating zero
431 break;
432 i *= 10;
433 i += j;
434 }
Willy Tarreauae8b7962007-06-09 23:10:04 +0200435
436 if (last && *opaque >= 1 && *opaque <= 4)
437 /* having a range with a min or a max is absurd */
438 return 0;
439
Willy Tarreaua84d3742007-05-07 00:36:48 +0200440 if (!last)
441 pattern->val.range.min = i;
442 pattern->val.range.max = i;
Willy Tarreauae8b7962007-06-09 23:10:04 +0200443
444 switch (*opaque) {
445 case 0: /* eq */
446 pattern->val.range.min_set = 1;
447 pattern->val.range.max_set = 1;
448 break;
449 case 1: /* gt */
450 pattern->val.range.min++; /* gt = ge + 1 */
451 case 2: /* ge */
452 pattern->val.range.min_set = 1;
453 pattern->val.range.max_set = 0;
454 break;
455 case 3: /* lt */
456 pattern->val.range.max--; /* lt = le - 1 */
457 case 4: /* le */
458 pattern->val.range.min_set = 0;
459 pattern->val.range.max_set = 1;
460 break;
461 }
462 return skip + 1;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200463}
464
Willy Tarreau4a26d2f2008-07-15 16:05:33 +0200465/* Parse a range of positive 2-component versions delimited by either ':' or
466 * '-'. The version consists in a major and a minor, both of which must be
467 * smaller than 65536, because internally they will be represented as a 32-bit
468 * integer.
469 * If only one version is read, it is set as both min and max. Just like for
470 * pure integers, an operator may be specified as the prefix, among this list
471 * of 5 :
472 *
473 * 0:eq, 1:gt, 2:ge, 3:lt, 4:le
474 *
475 * The default operator is "eq". It supports range matching. Ranges are
476 * rejected for other operators. The operator may be changed at any time.
477 * The operator is stored in the 'opaque' argument. This allows constructs
478 * such as the following one :
479 *
480 * acl obsolete_ssl ssl_req_proto lt 3
481 * acl unsupported_ssl ssl_req_proto gt 3.1
482 * acl valid_ssl ssl_req_proto 3.0-3.1
483 *
484 */
485int acl_parse_dotted_ver(const char **text, struct acl_pattern *pattern, int *opaque)
486{
487 signed long long i;
488 unsigned int j, last, skip = 0;
489 const char *ptr = *text;
490
491
492 while (!isdigit((unsigned char)*ptr)) {
493 if (strcmp(ptr, "eq") == 0) *opaque = 0;
494 else if (strcmp(ptr, "gt") == 0) *opaque = 1;
495 else if (strcmp(ptr, "ge") == 0) *opaque = 2;
496 else if (strcmp(ptr, "lt") == 0) *opaque = 3;
497 else if (strcmp(ptr, "le") == 0) *opaque = 4;
498 else
499 return 0;
500
501 skip++;
502 ptr = text[skip];
503 }
504
505 last = i = 0;
506 while (1) {
507 j = *ptr++;
508 if (j == '.') {
509 /* minor part */
510 if (i >= 65536)
511 return 0;
512 i <<= 16;
513 continue;
514 }
515 if ((j == '-' || j == ':') && !last) {
516 last++;
517 if (i < 65536)
518 i <<= 16;
519 pattern->val.range.min = i;
520 i = 0;
521 continue;
522 }
523 j -= '0';
524 if (j > 9)
525 // also catches the terminating zero
526 break;
527 i = (i & 0xFFFF0000) + (i & 0xFFFF) * 10;
528 i += j;
529 }
530
531 /* if we only got a major version, let's shift it now */
532 if (i < 65536)
533 i <<= 16;
534
535 if (last && *opaque >= 1 && *opaque <= 4)
536 /* having a range with a min or a max is absurd */
537 return 0;
538
539 if (!last)
540 pattern->val.range.min = i;
541 pattern->val.range.max = i;
542
543 switch (*opaque) {
544 case 0: /* eq */
545 pattern->val.range.min_set = 1;
546 pattern->val.range.max_set = 1;
547 break;
548 case 1: /* gt */
549 pattern->val.range.min++; /* gt = ge + 1 */
550 case 2: /* ge */
551 pattern->val.range.min_set = 1;
552 pattern->val.range.max_set = 0;
553 break;
554 case 3: /* lt */
555 pattern->val.range.max--; /* lt = le - 1 */
556 case 4: /* le */
557 pattern->val.range.min_set = 0;
558 pattern->val.range.max_set = 1;
559 break;
560 }
561 return skip + 1;
562}
563
Willy Tarreaua67fad92007-05-08 19:50:09 +0200564/* Parse an IP address and an optional mask in the form addr[/mask].
565 * The addr may either be an IPv4 address or a hostname. The mask
566 * may either be a dotted mask or a number of bits. Returns 1 if OK,
567 * otherwise 0.
568 */
Willy Tarreauae8b7962007-06-09 23:10:04 +0200569int acl_parse_ip(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreaua67fad92007-05-08 19:50:09 +0200570{
Willy Tarreauae8b7962007-06-09 23:10:04 +0200571 if (str2net(*text, &pattern->val.ipv4.addr, &pattern->val.ipv4.mask))
572 return 1;
573 else
574 return 0;
Willy Tarreaua67fad92007-05-08 19:50:09 +0200575}
576
Willy Tarreaua84d3742007-05-07 00:36:48 +0200577/*
578 * Registers the ACL keyword list <kwl> as a list of valid keywords for next
579 * parsing sessions.
580 */
581void acl_register_keywords(struct acl_kw_list *kwl)
582{
583 LIST_ADDQ(&acl_keywords.list, &kwl->list);
584}
585
586/*
587 * Unregisters the ACL keyword list <kwl> from the list of valid keywords.
588 */
589void acl_unregister_keywords(struct acl_kw_list *kwl)
590{
591 LIST_DEL(&kwl->list);
592 LIST_INIT(&kwl->list);
593}
594
595/* Return a pointer to the ACL <name> within the list starting at <head>, or
596 * NULL if not found.
597 */
598struct acl *find_acl_by_name(const char *name, struct list *head)
599{
600 struct acl *acl;
601 list_for_each_entry(acl, head, list) {
602 if (strcmp(acl->name, name) == 0)
603 return acl;
604 }
605 return NULL;
606}
607
608/* Return a pointer to the ACL keyword <kw>, or NULL if not found. Note that if
609 * <kw> contains an opening parenthesis, only the left part of it is checked.
610 */
611struct acl_keyword *find_acl_kw(const char *kw)
612{
613 int index;
614 const char *kwend;
615 struct acl_kw_list *kwl;
616
617 kwend = strchr(kw, '(');
618 if (!kwend)
619 kwend = kw + strlen(kw);
620
621 list_for_each_entry(kwl, &acl_keywords.list, list) {
622 for (index = 0; kwl->kw[index].kw != NULL; index++) {
623 if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) &&
624 kwl->kw[index].kw[kwend-kw] == 0)
625 return &kwl->kw[index];
626 }
627 }
628 return NULL;
629}
630
631static void free_pattern(struct acl_pattern *pat)
632{
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +0200633
634 if (pat->ptr.ptr) {
635 if (pat->freeptrbuf)
636 pat->freeptrbuf(pat->ptr.ptr);
637
Willy Tarreaua84d3742007-05-07 00:36:48 +0200638 free(pat->ptr.ptr);
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +0200639 }
640
Willy Tarreaua84d3742007-05-07 00:36:48 +0200641 free(pat);
642}
643
644static void free_pattern_list(struct list *head)
645{
646 struct acl_pattern *pat, *tmp;
647 list_for_each_entry_safe(pat, tmp, head, list)
648 free_pattern(pat);
649}
650
651static struct acl_expr *prune_acl_expr(struct acl_expr *expr)
652{
653 free_pattern_list(&expr->patterns);
654 LIST_INIT(&expr->patterns);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +0100655 if (expr->arg_len && expr->arg.str)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200656 free(expr->arg.str);
657 expr->kw->use_cnt--;
658 return expr;
659}
660
661/* Parse an ACL expression starting at <args>[0], and return it.
662 * Right now, the only accepted syntax is :
663 * <subject> [<value>...]
664 */
665struct acl_expr *parse_acl_expr(const char **args)
666{
667 __label__ out_return, out_free_expr, out_free_pattern;
668 struct acl_expr *expr;
669 struct acl_keyword *aclkw;
670 struct acl_pattern *pattern;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200671 int opaque, patflags;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200672 const char *arg;
673
674 aclkw = find_acl_kw(args[0]);
675 if (!aclkw || !aclkw->parse)
676 goto out_return;
677
678 expr = (struct acl_expr *)calloc(1, sizeof(*expr));
679 if (!expr)
680 goto out_return;
681
682 expr->kw = aclkw;
683 aclkw->use_cnt++;
684 LIST_INIT(&expr->patterns);
685 expr->arg.str = NULL;
Willy Tarreaubb768912007-06-10 11:17:01 +0200686 expr->arg_len = 0;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200687
688 arg = strchr(args[0], '(');
689 if (arg != NULL) {
690 char *end, *arg2;
691 /* there is an argument in the form "subject(arg)" */
692 arg++;
693 end = strchr(arg, ')');
694 if (!end)
695 goto out_free_expr;
Willy Tarreauac778f52010-01-26 19:02:46 +0100696 arg2 = my_strndup(arg, end - arg);
Willy Tarreau1edb1442010-01-27 20:13:38 +0100697 if (!arg2)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200698 goto out_free_expr;
Willy Tarreaubb768912007-06-10 11:17:01 +0200699 expr->arg_len = end - arg;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200700 expr->arg.str = arg2;
701 }
702
Willy Tarreaua84d3742007-05-07 00:36:48 +0200703 args++;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200704
705 /* check for options before patterns. Supported options are :
706 * -i : ignore case for all patterns by default
707 * -f : read patterns from those files
708 * -- : everything after this is not an option
709 */
710 patflags = 0;
711 while (**args == '-') {
712 if ((*args)[1] == 'i')
713 patflags |= ACL_PAT_F_IGNORE_CASE;
714 else if ((*args)[1] == 'f')
715 patflags |= ACL_PAT_F_FROM_FILE;
716 else if ((*args)[1] == '-') {
717 args++;
718 break;
719 }
720 else
721 break;
722 args++;
723 }
724
725 /* now parse all patterns */
Willy Tarreauae8b7962007-06-09 23:10:04 +0200726 opaque = 0;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200727 while (**args) {
Willy Tarreauae8b7962007-06-09 23:10:04 +0200728 int ret;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200729 pattern = (struct acl_pattern *)calloc(1, sizeof(*pattern));
730 if (!pattern)
731 goto out_free_expr;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200732 pattern->flags = patflags;
733
Willy Tarreauae8b7962007-06-09 23:10:04 +0200734 ret = aclkw->parse(args, pattern, &opaque);
735 if (!ret)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200736 goto out_free_pattern;
737 LIST_ADDQ(&expr->patterns, &pattern->list);
Willy Tarreauae8b7962007-06-09 23:10:04 +0200738 args += ret;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200739 }
740
741 return expr;
742
743 out_free_pattern:
744 free_pattern(pattern);
745 out_free_expr:
746 prune_acl_expr(expr);
747 free(expr);
748 out_return:
749 return NULL;
750}
751
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +0200752/* Purge everything in the acl <acl>, then return <acl>. */
753struct acl *prune_acl(struct acl *acl) {
754
755 struct acl_expr *expr, *exprb;
756
757 free(acl->name);
758
759 list_for_each_entry_safe(expr, exprb, &acl->expr, list) {
760 LIST_DEL(&expr->list);
761 prune_acl_expr(expr);
762 free(expr);
763 }
764
765 return acl;
766}
767
Willy Tarreaua84d3742007-05-07 00:36:48 +0200768/* Parse an ACL with the name starting at <args>[0], and with a list of already
769 * known ACLs in <acl>. If the ACL was not in the list, it will be added.
Willy Tarreau2a56c5e2010-03-15 16:13:29 +0100770 * A pointer to that ACL is returned. If the ACL has an empty name, then it's
771 * an anonymous one and it won't be merged with any other one.
Willy Tarreaua84d3742007-05-07 00:36:48 +0200772 *
773 * args syntax: <aclname> <acl_expr>
774 */
775struct acl *parse_acl(const char **args, struct list *known_acl)
776{
777 __label__ out_return, out_free_acl_expr, out_free_name;
778 struct acl *cur_acl;
779 struct acl_expr *acl_expr;
780 char *name;
781
Willy Tarreau2a56c5e2010-03-15 16:13:29 +0100782 if (**args && invalid_char(*args))
Willy Tarreau2e74c3f2007-12-02 18:45:09 +0100783 goto out_return;
784
Willy Tarreaua84d3742007-05-07 00:36:48 +0200785 acl_expr = parse_acl_expr(args + 1);
786 if (!acl_expr)
787 goto out_return;
788
Willy Tarreau404e8ab2009-07-26 19:40:40 +0200789 /* Check for args beginning with an opening parenthesis just after the
790 * subject, as this is almost certainly a typo. Right now we can only
791 * emit a warning, so let's do so.
792 */
Krzysztof Piotr Oledzki4cdd8312009-10-05 00:23:35 +0200793 if (!strchr(args[1], '(') && *args[2] == '(')
Willy Tarreau404e8ab2009-07-26 19:40:40 +0200794 Warning("parsing acl '%s' :\n"
795 " matching '%s' for pattern '%s' is likely a mistake and probably\n"
796 " not what you want. Maybe you need to remove the extraneous space before '('.\n"
797 " If you are really sure this is not an error, please insert '--' between the\n"
798 " match and the pattern to make this warning message disappear.\n",
799 args[0], args[1], args[2]);
800
Willy Tarreau2a56c5e2010-03-15 16:13:29 +0100801 if (*args[0])
802 cur_acl = find_acl_by_name(args[0], known_acl);
803 else
804 cur_acl = NULL;
805
Willy Tarreaua84d3742007-05-07 00:36:48 +0200806 if (!cur_acl) {
807 name = strdup(args[0]);
808 if (!name)
809 goto out_free_acl_expr;
810 cur_acl = (struct acl *)calloc(1, sizeof(*cur_acl));
811 if (cur_acl == NULL)
812 goto out_free_name;
813
814 LIST_INIT(&cur_acl->expr);
815 LIST_ADDQ(known_acl, &cur_acl->list);
816 cur_acl->name = name;
817 }
818
Willy Tarreaua9802632008-07-25 19:13:19 +0200819 cur_acl->requires |= acl_expr->kw->requires;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200820 LIST_ADDQ(&cur_acl->expr, &acl_expr->list);
821 return cur_acl;
822
823 out_free_name:
824 free(name);
825 out_free_acl_expr:
826 prune_acl_expr(acl_expr);
827 free(acl_expr);
828 out_return:
829 return NULL;
830}
831
Willy Tarreau16fbe822007-06-17 11:54:31 +0200832/* Some useful ACLs provided by default. Only those used are allocated. */
833
834const struct {
835 const char *name;
836 const char *expr[4]; /* put enough for longest expression */
837} default_acl_list[] = {
Willy Tarreau58393e12008-07-20 10:39:22 +0200838 { .name = "TRUE", .expr = {"always_true",""}},
839 { .name = "FALSE", .expr = {"always_false",""}},
Willy Tarreau16fbe822007-06-17 11:54:31 +0200840 { .name = "LOCALHOST", .expr = {"src","127.0.0.1/8",""}},
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200841 { .name = "HTTP", .expr = {"req_proto_http",""}},
Willy Tarreau16fbe822007-06-17 11:54:31 +0200842 { .name = "HTTP_1.0", .expr = {"req_ver","1.0",""}},
843 { .name = "HTTP_1.1", .expr = {"req_ver","1.1",""}},
844 { .name = "METH_CONNECT", .expr = {"method","CONNECT",""}},
845 { .name = "METH_GET", .expr = {"method","GET","HEAD",""}},
846 { .name = "METH_HEAD", .expr = {"method","HEAD",""}},
847 { .name = "METH_OPTIONS", .expr = {"method","OPTIONS",""}},
848 { .name = "METH_POST", .expr = {"method","POST",""}},
849 { .name = "METH_TRACE", .expr = {"method","TRACE",""}},
850 { .name = "HTTP_URL_ABS", .expr = {"url_reg","^[^/:]*://",""}},
851 { .name = "HTTP_URL_SLASH", .expr = {"url_beg","/",""}},
852 { .name = "HTTP_URL_STAR", .expr = {"url","*",""}},
853 { .name = "HTTP_CONTENT", .expr = {"hdr_val(content-length)","gt","0",""}},
Emeric Brunbede3d02009-06-30 17:54:00 +0200854 { .name = "RDP_COOKIE", .expr = {"req_rdp_cookie_cnt","gt","0",""}},
Willy Tarreauc6317702008-07-20 09:29:50 +0200855 { .name = "REQ_CONTENT", .expr = {"req_len","gt","0",""}},
Willy Tarreaub6fb4202008-07-20 11:18:28 +0200856 { .name = "WAIT_END", .expr = {"wait_end",""}},
Willy Tarreau16fbe822007-06-17 11:54:31 +0200857 { .name = NULL, .expr = {""}}
858};
859
860/* Find a default ACL from the default_acl list, compile it and return it.
861 * If the ACL is not found, NULL is returned. In theory, it cannot fail,
862 * except when default ACLs are broken, in which case it will return NULL.
863 * If <known_acl> is not NULL, the ACL will be queued at its tail.
864 */
865struct acl *find_acl_default(const char *acl_name, struct list *known_acl)
866{
867 __label__ out_return, out_free_acl_expr, out_free_name;
868 struct acl *cur_acl;
869 struct acl_expr *acl_expr;
870 char *name;
871 int index;
872
873 for (index = 0; default_acl_list[index].name != NULL; index++) {
874 if (strcmp(acl_name, default_acl_list[index].name) == 0)
875 break;
876 }
877
878 if (default_acl_list[index].name == NULL)
879 return NULL;
880
881 acl_expr = parse_acl_expr((const char **)default_acl_list[index].expr);
882 if (!acl_expr)
883 goto out_return;
884
885 name = strdup(acl_name);
886 if (!name)
887 goto out_free_acl_expr;
888 cur_acl = (struct acl *)calloc(1, sizeof(*cur_acl));
889 if (cur_acl == NULL)
890 goto out_free_name;
891
892 cur_acl->name = name;
Willy Tarreaua55b7dc2009-07-12 09:21:30 +0200893 cur_acl->requires |= acl_expr->kw->requires;
Willy Tarreau16fbe822007-06-17 11:54:31 +0200894 LIST_INIT(&cur_acl->expr);
895 LIST_ADDQ(&cur_acl->expr, &acl_expr->list);
896 if (known_acl)
897 LIST_ADDQ(known_acl, &cur_acl->list);
898
899 return cur_acl;
900
901 out_free_name:
902 free(name);
903 out_free_acl_expr:
904 prune_acl_expr(acl_expr);
905 free(acl_expr);
906 out_return:
907 return NULL;
908}
Willy Tarreaua84d3742007-05-07 00:36:48 +0200909
910/* Purge everything in the acl_cond <cond>, then return <cond>. */
911struct acl_cond *prune_acl_cond(struct acl_cond *cond)
912{
913 struct acl_term_suite *suite, *tmp_suite;
914 struct acl_term *term, *tmp_term;
915
916 /* iterate through all term suites and free all terms and all suites */
917 list_for_each_entry_safe(suite, tmp_suite, &cond->suites, list) {
918 list_for_each_entry_safe(term, tmp_term, &suite->terms, list)
919 free(term);
920 free(suite);
921 }
922 return cond;
923}
924
925/* Parse an ACL condition starting at <args>[0], relying on a list of already
926 * known ACLs passed in <known_acl>. The new condition is returned (or NULL in
927 * case of low memory). Supports multiple conditions separated by "or".
928 */
929struct acl_cond *parse_acl_cond(const char **args, struct list *known_acl, int pol)
930{
931 __label__ out_return, out_free_suite, out_free_term;
Willy Tarreau74b98a82007-06-16 19:35:18 +0200932 int arg, neg;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200933 const char *word;
934 struct acl *cur_acl;
935 struct acl_term *cur_term;
936 struct acl_term_suite *cur_suite;
937 struct acl_cond *cond;
938
939 cond = (struct acl_cond *)calloc(1, sizeof(*cond));
940 if (cond == NULL)
941 goto out_return;
942
943 LIST_INIT(&cond->list);
944 LIST_INIT(&cond->suites);
945 cond->pol = pol;
946
947 cur_suite = NULL;
Willy Tarreau74b98a82007-06-16 19:35:18 +0200948 neg = 0;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200949 for (arg = 0; *args[arg]; arg++) {
950 word = args[arg];
951
952 /* remove as many exclamation marks as we can */
953 while (*word == '!') {
954 neg = !neg;
955 word++;
956 }
957
958 /* an empty word is allowed because we cannot force the user to
959 * always think about not leaving exclamation marks alone.
960 */
961 if (!*word)
962 continue;
963
Willy Tarreau16fbe822007-06-17 11:54:31 +0200964 if (strcasecmp(word, "or") == 0 || strcmp(word, "||") == 0) {
Willy Tarreaua84d3742007-05-07 00:36:48 +0200965 /* new term suite */
966 cur_suite = NULL;
967 neg = 0;
968 continue;
969 }
970
Willy Tarreau95fa4692010-02-01 13:05:50 +0100971 if (strcmp(word, "{") == 0) {
972 /* we may have a complete ACL expression between two braces,
973 * find the last one.
974 */
975 int arg_end = arg + 1;
976 const char **args_new;
977
978 while (*args[arg_end] && strcmp(args[arg_end], "}") != 0)
979 arg_end++;
980
981 if (!*args[arg_end])
982 goto out_free_suite;
983
984 args_new = calloc(1, (arg_end - arg + 1) * sizeof(*args_new));
985 if (!args_new)
986 goto out_free_suite;
987
Willy Tarreau2a56c5e2010-03-15 16:13:29 +0100988 args_new[0] = "";
Willy Tarreau95fa4692010-02-01 13:05:50 +0100989 memcpy(args_new + 1, args + arg + 1, (arg_end - arg) * sizeof(*args_new));
990 args_new[arg_end - arg] = "";
991 cur_acl = parse_acl(args_new, known_acl);
992 free(args_new);
993
994 if (!cur_acl)
Willy Tarreau16fbe822007-06-17 11:54:31 +0200995 goto out_free_suite;
Willy Tarreau95fa4692010-02-01 13:05:50 +0100996 arg = arg_end;
997 }
998 else {
999 /* search for <word> in the known ACL names. If we do not find
1000 * it, let's look for it in the default ACLs, and if found, add
1001 * it to the list of ACLs of this proxy. This makes it possible
1002 * to override them.
1003 */
1004 cur_acl = find_acl_by_name(word, known_acl);
1005 if (cur_acl == NULL) {
1006 cur_acl = find_acl_default(word, known_acl);
1007 if (cur_acl == NULL)
1008 goto out_free_suite;
1009 }
Willy Tarreau16fbe822007-06-17 11:54:31 +02001010 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001011
1012 cur_term = (struct acl_term *)calloc(1, sizeof(*cur_term));
1013 if (cur_term == NULL)
1014 goto out_free_suite;
1015
1016 cur_term->acl = cur_acl;
1017 cur_term->neg = neg;
Willy Tarreaua9802632008-07-25 19:13:19 +02001018 cond->requires |= cur_acl->requires;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001019
1020 if (!cur_suite) {
1021 cur_suite = (struct acl_term_suite *)calloc(1, sizeof(*cur_suite));
1022 if (cur_term == NULL)
1023 goto out_free_term;
1024 LIST_INIT(&cur_suite->terms);
1025 LIST_ADDQ(&cond->suites, &cur_suite->list);
1026 }
1027 LIST_ADDQ(&cur_suite->terms, &cur_term->list);
Willy Tarreau74b98a82007-06-16 19:35:18 +02001028 neg = 0;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001029 }
1030
1031 return cond;
1032
1033 out_free_term:
1034 free(cur_term);
1035 out_free_suite:
1036 prune_acl_cond(cond);
1037 free(cond);
1038 out_return:
1039 return NULL;
1040}
1041
Willy Tarreau2bbba412010-01-28 16:48:33 +01001042/* Builds an ACL condition starting at the if/unless keyword. The complete
1043 * condition is returned. NULL is returned in case of error or if the first
1044 * word is neither "if" nor "unless". It automatically sets the file name and
1045 * the line number in the condition for better error reporting, and adds the
1046 * ACL requirements to the proxy's acl_requires.
1047 */
1048struct acl_cond *build_acl_cond(const char *file, int line, struct proxy *px, const char **args)
1049{
1050 int pol = ACL_COND_NONE;
1051 struct acl_cond *cond = NULL;
1052
1053 if (!strcmp(*args, "if")) {
1054 pol = ACL_COND_IF;
1055 args++;
1056 }
1057 else if (!strcmp(*args, "unless")) {
1058 pol = ACL_COND_UNLESS;
1059 args++;
1060 }
1061 else
1062 return NULL;
1063
1064 cond = parse_acl_cond(args, &px->acl, pol);
1065 if (!cond)
1066 return NULL;
1067
1068 cond->file = file;
1069 cond->line = line;
1070 px->acl_requires |= cond->requires;
1071
1072 return cond;
1073}
1074
Willy Tarreau11382812008-07-09 16:18:21 +02001075/* Execute condition <cond> and return either ACL_PAT_FAIL, ACL_PAT_MISS or
Willy Tarreaub6866442008-07-14 23:54:42 +02001076 * ACL_PAT_PASS depending on the test results. ACL_PAT_MISS may only be
1077 * returned if <dir> contains ACL_PARTIAL, indicating that incomplete data
1078 * is being examined.
1079 * This function only computes the condition, it does not apply the polarity
1080 * required by IF/UNLESS, it's up to the caller to do this using something like
1081 * this :
Willy Tarreau11382812008-07-09 16:18:21 +02001082 *
1083 * res = acl_pass(res);
Willy Tarreaub6866442008-07-14 23:54:42 +02001084 * if (res == ACL_PAT_MISS)
1085 * return 0;
Willy Tarreau11382812008-07-09 16:18:21 +02001086 * if (cond->pol == ACL_COND_UNLESS)
1087 * res = !res;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001088 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02001089int acl_exec_cond(struct acl_cond *cond, struct proxy *px, struct session *l4, void *l7, int dir)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001090{
1091 __label__ fetch_next;
1092 struct acl_term_suite *suite;
1093 struct acl_term *term;
1094 struct acl_expr *expr;
1095 struct acl *acl;
1096 struct acl_pattern *pattern;
1097 struct acl_test test;
Willy Tarreau11382812008-07-09 16:18:21 +02001098 int acl_res, suite_res, cond_res;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001099
Willy Tarreau11382812008-07-09 16:18:21 +02001100 /* We're doing a logical OR between conditions so we initialize to FAIL.
1101 * The MISS status is propagated down from the suites.
1102 */
Willy Tarreaua84d3742007-05-07 00:36:48 +02001103 cond_res = ACL_PAT_FAIL;
1104 list_for_each_entry(suite, &cond->suites, list) {
Willy Tarreau11382812008-07-09 16:18:21 +02001105 /* Evaluate condition suite <suite>. We stop at the first term
1106 * which returns ACL_PAT_FAIL. The MISS status is still propagated
1107 * in case of uncertainty in the result.
Willy Tarreaua84d3742007-05-07 00:36:48 +02001108 */
1109
1110 /* we're doing a logical AND between terms, so we must set the
1111 * initial value to PASS.
1112 */
1113 suite_res = ACL_PAT_PASS;
1114 list_for_each_entry(term, &suite->terms, list) {
1115 acl = term->acl;
1116
1117 /* FIXME: use cache !
1118 * check acl->cache_idx for this.
1119 */
1120
1121 /* ACL result not cached. Let's scan all the expressions
1122 * and use the first one to match.
1123 */
1124 acl_res = ACL_PAT_FAIL;
1125 list_for_each_entry(expr, &acl->expr, list) {
Willy Tarreaud41f8d82007-06-10 10:06:18 +02001126 /* we need to reset context and flags */
1127 memset(&test, 0, sizeof(test));
Willy Tarreaua84d3742007-05-07 00:36:48 +02001128 fetch_next:
Willy Tarreaub6866442008-07-14 23:54:42 +02001129 if (!expr->kw->fetch(px, l4, l7, dir, expr, &test)) {
1130 /* maybe we could not fetch because of missing data */
1131 if (test.flags & ACL_TEST_F_MAY_CHANGE && dir & ACL_PARTIAL)
1132 acl_res |= ACL_PAT_MISS;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001133 continue;
Willy Tarreaub6866442008-07-14 23:54:42 +02001134 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001135
Willy Tarreaua79534f2008-07-20 10:13:37 +02001136 if (test.flags & ACL_TEST_F_RES_SET) {
1137 if (test.flags & ACL_TEST_F_RES_PASS)
1138 acl_res |= ACL_PAT_PASS;
1139 else
1140 acl_res |= ACL_PAT_FAIL;
1141 }
1142 else {
1143 /* call the match() function for all tests on this value */
1144 list_for_each_entry(pattern, &expr->patterns, list) {
1145 acl_res |= expr->kw->match(&test, pattern);
1146 if (acl_res == ACL_PAT_PASS)
1147 break;
1148 }
Krzysztof Piotr Oledzkid7528e52010-01-29 17:55:53 +01001149
1150 if ((test.flags & ACL_TEST_F_NULL_MATCH) && LIST_ISEMPTY(&expr->patterns))
1151 acl_res |= expr->kw->match(&test, NULL);
Willy Tarreaua84d3742007-05-07 00:36:48 +02001152 }
1153 /*
Willy Tarreau11382812008-07-09 16:18:21 +02001154 * OK now acl_res holds the result of this expression
1155 * as one of ACL_PAT_FAIL, ACL_PAT_MISS or ACL_PAT_PASS.
Willy Tarreaua84d3742007-05-07 00:36:48 +02001156 *
Willy Tarreau11382812008-07-09 16:18:21 +02001157 * Then if (!MISS) we can cache the result, and put
Willy Tarreaua84d3742007-05-07 00:36:48 +02001158 * (test.flags & ACL_TEST_F_VOLATILE) in the cache flags.
1159 *
1160 * FIXME: implement cache.
1161 *
1162 */
1163
1164 /* now we may have some cleanup to do */
1165 if (test.flags & ACL_TEST_F_MUST_FREE) {
1166 free(test.ptr);
1167 test.len = 0;
1168 }
1169
Willy Tarreau11382812008-07-09 16:18:21 +02001170 /* we're ORing these terms, so a single PASS is enough */
1171 if (acl_res == ACL_PAT_PASS)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001172 break;
1173
Willy Tarreaua84d3742007-05-07 00:36:48 +02001174 if (test.flags & ACL_TEST_F_FETCH_MORE)
1175 goto fetch_next;
Willy Tarreaub6866442008-07-14 23:54:42 +02001176
1177 /* sometimes we know the fetched data is subject to change
1178 * later and give another chance for a new match (eg: request
1179 * size, time, ...)
1180 */
1181 if (test.flags & ACL_TEST_F_MAY_CHANGE && dir & ACL_PARTIAL)
1182 acl_res |= ACL_PAT_MISS;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001183 }
1184 /*
1185 * Here we have the result of an ACL (cached or not).
1186 * ACLs are combined, negated or not, to form conditions.
1187 */
1188
Willy Tarreaua84d3742007-05-07 00:36:48 +02001189 if (term->neg)
Willy Tarreau11382812008-07-09 16:18:21 +02001190 acl_res = acl_neg(acl_res);
Willy Tarreaua84d3742007-05-07 00:36:48 +02001191
1192 suite_res &= acl_res;
Willy Tarreau11382812008-07-09 16:18:21 +02001193
1194 /* we're ANDing these terms, so a single FAIL is enough */
1195 if (suite_res == ACL_PAT_FAIL)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001196 break;
1197 }
1198 cond_res |= suite_res;
Willy Tarreau11382812008-07-09 16:18:21 +02001199
1200 /* we're ORing these terms, so a single PASS is enough */
1201 if (cond_res == ACL_PAT_PASS)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001202 break;
1203 }
Willy Tarreau11382812008-07-09 16:18:21 +02001204 return cond_res;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001205}
1206
1207
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001208/* Reports a pointer to the first ACL used in condition <cond> which requires
1209 * at least one of the USE_FLAGS in <require>. Returns NULL if none matches.
1210 * The construct is almost the same as for acl_exec_cond() since we're walking
1211 * down the ACL tree as well. It is important that the tree is really walked
1212 * through and never cached, because that way, this function can be used as a
1213 * late check.
1214 */
Willy Tarreauf1e98b82010-01-28 17:59:39 +01001215struct acl *cond_find_require(const struct acl_cond *cond, unsigned int require)
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001216{
1217 struct acl_term_suite *suite;
1218 struct acl_term *term;
1219 struct acl *acl;
1220
1221 list_for_each_entry(suite, &cond->suites, list) {
1222 list_for_each_entry(term, &suite->terms, list) {
1223 acl = term->acl;
1224 if (acl->requires & require)
1225 return acl;
1226 }
1227 }
1228 return NULL;
1229}
1230
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001231/*
1232 * Find targets for userlist and groups in acl. Function returns the number
1233 * of errors or OK if everything is fine.
1234 */
1235int
1236acl_find_targets(struct proxy *p)
1237{
1238
1239 struct acl *acl;
1240 struct acl_expr *expr;
1241 struct acl_pattern *pattern;
1242 struct userlist *ul;
1243 int cfgerr = 0;
1244
1245 list_for_each_entry(acl, &p->acl, list) {
1246 list_for_each_entry(expr, &acl->expr, list) {
1247 if (strstr(expr->kw->kw, "http_auth") == expr->kw->kw) {
1248
1249 if (!expr->arg.str || !*expr->arg.str) {
1250 Alert("proxy %s: acl %s %s(): missing userlist name.\n",
1251 p->id, acl->name, expr->kw->kw);
1252 cfgerr++;
1253 continue;
1254 }
1255
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01001256 if (p->uri_auth && p->uri_auth->userlist &&
1257 !strcmp(p->uri_auth->userlist->name, expr->arg.str))
1258 ul = p->uri_auth->userlist;
1259 else
1260 ul = auth_find_userlist(expr->arg.str);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001261
1262 if (!ul) {
1263 Alert("proxy %s: acl %s %s(%s): unable to find userlist.\n",
1264 p->id, acl->name, expr->kw->kw, expr->arg.str);
1265 cfgerr++;
1266 continue;
1267 }
1268
1269 expr->arg_len = 0;
1270 expr->arg.ul = ul;
1271 }
1272
1273
1274 if (!strcmp(expr->kw->kw, "http_auth_group")) {
1275
1276 if (LIST_ISEMPTY(&expr->patterns)) {
1277 Alert("proxy %s: acl %s %s(): no groups specified.\n",
1278 p->id, acl->name, expr->kw->kw);
1279 cfgerr++;
1280 continue;
1281 }
1282
1283 list_for_each_entry(pattern, &expr->patterns, list) {
1284 pattern->val.group_mask = auth_resolve_groups(expr->arg.ul, pattern->ptr.str);
1285
1286 free(pattern->ptr.str);
1287 pattern->ptr.str = NULL;
1288 pattern->len = 0;
1289
1290 if (!pattern->val.group_mask) {
1291 Alert("proxy %s: acl %s %s(): invalid group(s).\n",
1292 p->id, acl->name, expr->kw->kw);
1293 cfgerr++;
1294 continue;
1295 }
1296 }
1297 }
1298 }
1299 }
1300
1301 return cfgerr;
1302}
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001303
Willy Tarreaua84d3742007-05-07 00:36:48 +02001304/************************************************************************/
1305/* All supported keywords must be declared here. */
1306/************************************************************************/
1307
1308/* Note: must not be declared <const> as its list will be overwritten */
1309static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02001310 { "always_true", acl_parse_nothing, acl_fetch_true, acl_match_nothing, ACL_USE_NOTHING },
1311 { "always_false", acl_parse_nothing, acl_fetch_false, acl_match_nothing, ACL_USE_NOTHING },
1312 { "wait_end", acl_parse_nothing, acl_fetch_wait_end, acl_match_nothing, ACL_USE_NOTHING },
Willy Tarreaua84d3742007-05-07 00:36:48 +02001313#if 0
1314 { "time", acl_parse_time, acl_fetch_time, acl_match_time },
1315#endif
1316 { NULL, NULL, NULL, NULL }
1317}};
1318
1319
1320__attribute__((constructor))
1321static void __acl_init(void)
1322{
1323 acl_register_keywords(&acl_kws);
1324}
1325
1326
1327/*
1328 * Local variables:
1329 * c-indent-level: 8
1330 * c-basic-offset: 8
1331 * End:
1332 */