blob: c6c7484a3a272fc7f56dc93a2a9f2ede7c73202e [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.
770 * A pointer to that ACL is returned.
771 *
772 * args syntax: <aclname> <acl_expr>
773 */
774struct acl *parse_acl(const char **args, struct list *known_acl)
775{
776 __label__ out_return, out_free_acl_expr, out_free_name;
777 struct acl *cur_acl;
778 struct acl_expr *acl_expr;
779 char *name;
780
Willy Tarreau2e74c3f2007-12-02 18:45:09 +0100781 if (invalid_char(*args))
782 goto out_return;
783
Willy Tarreaua84d3742007-05-07 00:36:48 +0200784 acl_expr = parse_acl_expr(args + 1);
785 if (!acl_expr)
786 goto out_return;
787
Willy Tarreau404e8ab2009-07-26 19:40:40 +0200788 /* Check for args beginning with an opening parenthesis just after the
789 * subject, as this is almost certainly a typo. Right now we can only
790 * emit a warning, so let's do so.
791 */
Krzysztof Piotr Oledzki4cdd8312009-10-05 00:23:35 +0200792 if (!strchr(args[1], '(') && *args[2] == '(')
Willy Tarreau404e8ab2009-07-26 19:40:40 +0200793 Warning("parsing acl '%s' :\n"
794 " matching '%s' for pattern '%s' is likely a mistake and probably\n"
795 " not what you want. Maybe you need to remove the extraneous space before '('.\n"
796 " If you are really sure this is not an error, please insert '--' between the\n"
797 " match and the pattern to make this warning message disappear.\n",
798 args[0], args[1], args[2]);
799
Willy Tarreaua84d3742007-05-07 00:36:48 +0200800 cur_acl = find_acl_by_name(args[0], known_acl);
801 if (!cur_acl) {
802 name = strdup(args[0]);
803 if (!name)
804 goto out_free_acl_expr;
805 cur_acl = (struct acl *)calloc(1, sizeof(*cur_acl));
806 if (cur_acl == NULL)
807 goto out_free_name;
808
809 LIST_INIT(&cur_acl->expr);
810 LIST_ADDQ(known_acl, &cur_acl->list);
811 cur_acl->name = name;
812 }
813
Willy Tarreaua9802632008-07-25 19:13:19 +0200814 cur_acl->requires |= acl_expr->kw->requires;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200815 LIST_ADDQ(&cur_acl->expr, &acl_expr->list);
816 return cur_acl;
817
818 out_free_name:
819 free(name);
820 out_free_acl_expr:
821 prune_acl_expr(acl_expr);
822 free(acl_expr);
823 out_return:
824 return NULL;
825}
826
Willy Tarreau16fbe822007-06-17 11:54:31 +0200827/* Some useful ACLs provided by default. Only those used are allocated. */
828
829const struct {
830 const char *name;
831 const char *expr[4]; /* put enough for longest expression */
832} default_acl_list[] = {
Willy Tarreau58393e12008-07-20 10:39:22 +0200833 { .name = "TRUE", .expr = {"always_true",""}},
834 { .name = "FALSE", .expr = {"always_false",""}},
Willy Tarreau16fbe822007-06-17 11:54:31 +0200835 { .name = "LOCALHOST", .expr = {"src","127.0.0.1/8",""}},
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200836 { .name = "HTTP", .expr = {"req_proto_http",""}},
Willy Tarreau16fbe822007-06-17 11:54:31 +0200837 { .name = "HTTP_1.0", .expr = {"req_ver","1.0",""}},
838 { .name = "HTTP_1.1", .expr = {"req_ver","1.1",""}},
839 { .name = "METH_CONNECT", .expr = {"method","CONNECT",""}},
840 { .name = "METH_GET", .expr = {"method","GET","HEAD",""}},
841 { .name = "METH_HEAD", .expr = {"method","HEAD",""}},
842 { .name = "METH_OPTIONS", .expr = {"method","OPTIONS",""}},
843 { .name = "METH_POST", .expr = {"method","POST",""}},
844 { .name = "METH_TRACE", .expr = {"method","TRACE",""}},
845 { .name = "HTTP_URL_ABS", .expr = {"url_reg","^[^/:]*://",""}},
846 { .name = "HTTP_URL_SLASH", .expr = {"url_beg","/",""}},
847 { .name = "HTTP_URL_STAR", .expr = {"url","*",""}},
848 { .name = "HTTP_CONTENT", .expr = {"hdr_val(content-length)","gt","0",""}},
Emeric Brunbede3d02009-06-30 17:54:00 +0200849 { .name = "RDP_COOKIE", .expr = {"req_rdp_cookie_cnt","gt","0",""}},
Willy Tarreauc6317702008-07-20 09:29:50 +0200850 { .name = "REQ_CONTENT", .expr = {"req_len","gt","0",""}},
Willy Tarreaub6fb4202008-07-20 11:18:28 +0200851 { .name = "WAIT_END", .expr = {"wait_end",""}},
Willy Tarreau16fbe822007-06-17 11:54:31 +0200852 { .name = NULL, .expr = {""}}
853};
854
855/* Find a default ACL from the default_acl list, compile it and return it.
856 * If the ACL is not found, NULL is returned. In theory, it cannot fail,
857 * except when default ACLs are broken, in which case it will return NULL.
858 * If <known_acl> is not NULL, the ACL will be queued at its tail.
859 */
860struct acl *find_acl_default(const char *acl_name, struct list *known_acl)
861{
862 __label__ out_return, out_free_acl_expr, out_free_name;
863 struct acl *cur_acl;
864 struct acl_expr *acl_expr;
865 char *name;
866 int index;
867
868 for (index = 0; default_acl_list[index].name != NULL; index++) {
869 if (strcmp(acl_name, default_acl_list[index].name) == 0)
870 break;
871 }
872
873 if (default_acl_list[index].name == NULL)
874 return NULL;
875
876 acl_expr = parse_acl_expr((const char **)default_acl_list[index].expr);
877 if (!acl_expr)
878 goto out_return;
879
880 name = strdup(acl_name);
881 if (!name)
882 goto out_free_acl_expr;
883 cur_acl = (struct acl *)calloc(1, sizeof(*cur_acl));
884 if (cur_acl == NULL)
885 goto out_free_name;
886
887 cur_acl->name = name;
Willy Tarreaua55b7dc2009-07-12 09:21:30 +0200888 cur_acl->requires |= acl_expr->kw->requires;
Willy Tarreau16fbe822007-06-17 11:54:31 +0200889 LIST_INIT(&cur_acl->expr);
890 LIST_ADDQ(&cur_acl->expr, &acl_expr->list);
891 if (known_acl)
892 LIST_ADDQ(known_acl, &cur_acl->list);
893
894 return cur_acl;
895
896 out_free_name:
897 free(name);
898 out_free_acl_expr:
899 prune_acl_expr(acl_expr);
900 free(acl_expr);
901 out_return:
902 return NULL;
903}
Willy Tarreaua84d3742007-05-07 00:36:48 +0200904
905/* Purge everything in the acl_cond <cond>, then return <cond>. */
906struct acl_cond *prune_acl_cond(struct acl_cond *cond)
907{
908 struct acl_term_suite *suite, *tmp_suite;
909 struct acl_term *term, *tmp_term;
910
911 /* iterate through all term suites and free all terms and all suites */
912 list_for_each_entry_safe(suite, tmp_suite, &cond->suites, list) {
913 list_for_each_entry_safe(term, tmp_term, &suite->terms, list)
914 free(term);
915 free(suite);
916 }
917 return cond;
918}
919
920/* Parse an ACL condition starting at <args>[0], relying on a list of already
921 * known ACLs passed in <known_acl>. The new condition is returned (or NULL in
922 * case of low memory). Supports multiple conditions separated by "or".
923 */
924struct acl_cond *parse_acl_cond(const char **args, struct list *known_acl, int pol)
925{
926 __label__ out_return, out_free_suite, out_free_term;
Willy Tarreau74b98a82007-06-16 19:35:18 +0200927 int arg, neg;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200928 const char *word;
929 struct acl *cur_acl;
930 struct acl_term *cur_term;
931 struct acl_term_suite *cur_suite;
932 struct acl_cond *cond;
933
934 cond = (struct acl_cond *)calloc(1, sizeof(*cond));
935 if (cond == NULL)
936 goto out_return;
937
938 LIST_INIT(&cond->list);
939 LIST_INIT(&cond->suites);
940 cond->pol = pol;
941
942 cur_suite = NULL;
Willy Tarreau74b98a82007-06-16 19:35:18 +0200943 neg = 0;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200944 for (arg = 0; *args[arg]; arg++) {
945 word = args[arg];
946
947 /* remove as many exclamation marks as we can */
948 while (*word == '!') {
949 neg = !neg;
950 word++;
951 }
952
953 /* an empty word is allowed because we cannot force the user to
954 * always think about not leaving exclamation marks alone.
955 */
956 if (!*word)
957 continue;
958
Willy Tarreau16fbe822007-06-17 11:54:31 +0200959 if (strcasecmp(word, "or") == 0 || strcmp(word, "||") == 0) {
Willy Tarreaua84d3742007-05-07 00:36:48 +0200960 /* new term suite */
961 cur_suite = NULL;
962 neg = 0;
963 continue;
964 }
965
Willy Tarreau95fa4692010-02-01 13:05:50 +0100966 if (strcmp(word, "{") == 0) {
967 /* we may have a complete ACL expression between two braces,
968 * find the last one.
969 */
970 int arg_end = arg + 1;
971 const char **args_new;
972
973 while (*args[arg_end] && strcmp(args[arg_end], "}") != 0)
974 arg_end++;
975
976 if (!*args[arg_end])
977 goto out_free_suite;
978
979 args_new = calloc(1, (arg_end - arg + 1) * sizeof(*args_new));
980 if (!args_new)
981 goto out_free_suite;
982
983 args_new[0] = ".noname";
984 memcpy(args_new + 1, args + arg + 1, (arg_end - arg) * sizeof(*args_new));
985 args_new[arg_end - arg] = "";
986 cur_acl = parse_acl(args_new, known_acl);
987 free(args_new);
988
989 if (!cur_acl)
Willy Tarreau16fbe822007-06-17 11:54:31 +0200990 goto out_free_suite;
Willy Tarreau95fa4692010-02-01 13:05:50 +0100991 arg = arg_end;
992 }
993 else {
994 /* search for <word> in the known ACL names. If we do not find
995 * it, let's look for it in the default ACLs, and if found, add
996 * it to the list of ACLs of this proxy. This makes it possible
997 * to override them.
998 */
999 cur_acl = find_acl_by_name(word, known_acl);
1000 if (cur_acl == NULL) {
1001 cur_acl = find_acl_default(word, known_acl);
1002 if (cur_acl == NULL)
1003 goto out_free_suite;
1004 }
Willy Tarreau16fbe822007-06-17 11:54:31 +02001005 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001006
1007 cur_term = (struct acl_term *)calloc(1, sizeof(*cur_term));
1008 if (cur_term == NULL)
1009 goto out_free_suite;
1010
1011 cur_term->acl = cur_acl;
1012 cur_term->neg = neg;
Willy Tarreaua9802632008-07-25 19:13:19 +02001013 cond->requires |= cur_acl->requires;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001014
1015 if (!cur_suite) {
1016 cur_suite = (struct acl_term_suite *)calloc(1, sizeof(*cur_suite));
1017 if (cur_term == NULL)
1018 goto out_free_term;
1019 LIST_INIT(&cur_suite->terms);
1020 LIST_ADDQ(&cond->suites, &cur_suite->list);
1021 }
1022 LIST_ADDQ(&cur_suite->terms, &cur_term->list);
Willy Tarreau74b98a82007-06-16 19:35:18 +02001023 neg = 0;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001024 }
1025
1026 return cond;
1027
1028 out_free_term:
1029 free(cur_term);
1030 out_free_suite:
1031 prune_acl_cond(cond);
1032 free(cond);
1033 out_return:
1034 return NULL;
1035}
1036
Willy Tarreau2bbba412010-01-28 16:48:33 +01001037/* Builds an ACL condition starting at the if/unless keyword. The complete
1038 * condition is returned. NULL is returned in case of error or if the first
1039 * word is neither "if" nor "unless". It automatically sets the file name and
1040 * the line number in the condition for better error reporting, and adds the
1041 * ACL requirements to the proxy's acl_requires.
1042 */
1043struct acl_cond *build_acl_cond(const char *file, int line, struct proxy *px, const char **args)
1044{
1045 int pol = ACL_COND_NONE;
1046 struct acl_cond *cond = NULL;
1047
1048 if (!strcmp(*args, "if")) {
1049 pol = ACL_COND_IF;
1050 args++;
1051 }
1052 else if (!strcmp(*args, "unless")) {
1053 pol = ACL_COND_UNLESS;
1054 args++;
1055 }
1056 else
1057 return NULL;
1058
1059 cond = parse_acl_cond(args, &px->acl, pol);
1060 if (!cond)
1061 return NULL;
1062
1063 cond->file = file;
1064 cond->line = line;
1065 px->acl_requires |= cond->requires;
1066
1067 return cond;
1068}
1069
Willy Tarreau11382812008-07-09 16:18:21 +02001070/* Execute condition <cond> and return either ACL_PAT_FAIL, ACL_PAT_MISS or
Willy Tarreaub6866442008-07-14 23:54:42 +02001071 * ACL_PAT_PASS depending on the test results. ACL_PAT_MISS may only be
1072 * returned if <dir> contains ACL_PARTIAL, indicating that incomplete data
1073 * is being examined.
1074 * This function only computes the condition, it does not apply the polarity
1075 * required by IF/UNLESS, it's up to the caller to do this using something like
1076 * this :
Willy Tarreau11382812008-07-09 16:18:21 +02001077 *
1078 * res = acl_pass(res);
Willy Tarreaub6866442008-07-14 23:54:42 +02001079 * if (res == ACL_PAT_MISS)
1080 * return 0;
Willy Tarreau11382812008-07-09 16:18:21 +02001081 * if (cond->pol == ACL_COND_UNLESS)
1082 * res = !res;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001083 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02001084int acl_exec_cond(struct acl_cond *cond, struct proxy *px, struct session *l4, void *l7, int dir)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001085{
1086 __label__ fetch_next;
1087 struct acl_term_suite *suite;
1088 struct acl_term *term;
1089 struct acl_expr *expr;
1090 struct acl *acl;
1091 struct acl_pattern *pattern;
1092 struct acl_test test;
Willy Tarreau11382812008-07-09 16:18:21 +02001093 int acl_res, suite_res, cond_res;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001094
Willy Tarreau11382812008-07-09 16:18:21 +02001095 /* We're doing a logical OR between conditions so we initialize to FAIL.
1096 * The MISS status is propagated down from the suites.
1097 */
Willy Tarreaua84d3742007-05-07 00:36:48 +02001098 cond_res = ACL_PAT_FAIL;
1099 list_for_each_entry(suite, &cond->suites, list) {
Willy Tarreau11382812008-07-09 16:18:21 +02001100 /* Evaluate condition suite <suite>. We stop at the first term
1101 * which returns ACL_PAT_FAIL. The MISS status is still propagated
1102 * in case of uncertainty in the result.
Willy Tarreaua84d3742007-05-07 00:36:48 +02001103 */
1104
1105 /* we're doing a logical AND between terms, so we must set the
1106 * initial value to PASS.
1107 */
1108 suite_res = ACL_PAT_PASS;
1109 list_for_each_entry(term, &suite->terms, list) {
1110 acl = term->acl;
1111
1112 /* FIXME: use cache !
1113 * check acl->cache_idx for this.
1114 */
1115
1116 /* ACL result not cached. Let's scan all the expressions
1117 * and use the first one to match.
1118 */
1119 acl_res = ACL_PAT_FAIL;
1120 list_for_each_entry(expr, &acl->expr, list) {
Willy Tarreaud41f8d82007-06-10 10:06:18 +02001121 /* we need to reset context and flags */
1122 memset(&test, 0, sizeof(test));
Willy Tarreaua84d3742007-05-07 00:36:48 +02001123 fetch_next:
Willy Tarreaub6866442008-07-14 23:54:42 +02001124 if (!expr->kw->fetch(px, l4, l7, dir, expr, &test)) {
1125 /* maybe we could not fetch because of missing data */
1126 if (test.flags & ACL_TEST_F_MAY_CHANGE && dir & ACL_PARTIAL)
1127 acl_res |= ACL_PAT_MISS;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001128 continue;
Willy Tarreaub6866442008-07-14 23:54:42 +02001129 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001130
Willy Tarreaua79534f2008-07-20 10:13:37 +02001131 if (test.flags & ACL_TEST_F_RES_SET) {
1132 if (test.flags & ACL_TEST_F_RES_PASS)
1133 acl_res |= ACL_PAT_PASS;
1134 else
1135 acl_res |= ACL_PAT_FAIL;
1136 }
1137 else {
1138 /* call the match() function for all tests on this value */
1139 list_for_each_entry(pattern, &expr->patterns, list) {
1140 acl_res |= expr->kw->match(&test, pattern);
1141 if (acl_res == ACL_PAT_PASS)
1142 break;
1143 }
Krzysztof Piotr Oledzkid7528e52010-01-29 17:55:53 +01001144
1145 if ((test.flags & ACL_TEST_F_NULL_MATCH) && LIST_ISEMPTY(&expr->patterns))
1146 acl_res |= expr->kw->match(&test, NULL);
Willy Tarreaua84d3742007-05-07 00:36:48 +02001147 }
1148 /*
Willy Tarreau11382812008-07-09 16:18:21 +02001149 * OK now acl_res holds the result of this expression
1150 * as one of ACL_PAT_FAIL, ACL_PAT_MISS or ACL_PAT_PASS.
Willy Tarreaua84d3742007-05-07 00:36:48 +02001151 *
Willy Tarreau11382812008-07-09 16:18:21 +02001152 * Then if (!MISS) we can cache the result, and put
Willy Tarreaua84d3742007-05-07 00:36:48 +02001153 * (test.flags & ACL_TEST_F_VOLATILE) in the cache flags.
1154 *
1155 * FIXME: implement cache.
1156 *
1157 */
1158
1159 /* now we may have some cleanup to do */
1160 if (test.flags & ACL_TEST_F_MUST_FREE) {
1161 free(test.ptr);
1162 test.len = 0;
1163 }
1164
Willy Tarreau11382812008-07-09 16:18:21 +02001165 /* we're ORing these terms, so a single PASS is enough */
1166 if (acl_res == ACL_PAT_PASS)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001167 break;
1168
Willy Tarreaua84d3742007-05-07 00:36:48 +02001169 if (test.flags & ACL_TEST_F_FETCH_MORE)
1170 goto fetch_next;
Willy Tarreaub6866442008-07-14 23:54:42 +02001171
1172 /* sometimes we know the fetched data is subject to change
1173 * later and give another chance for a new match (eg: request
1174 * size, time, ...)
1175 */
1176 if (test.flags & ACL_TEST_F_MAY_CHANGE && dir & ACL_PARTIAL)
1177 acl_res |= ACL_PAT_MISS;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001178 }
1179 /*
1180 * Here we have the result of an ACL (cached or not).
1181 * ACLs are combined, negated or not, to form conditions.
1182 */
1183
Willy Tarreaua84d3742007-05-07 00:36:48 +02001184 if (term->neg)
Willy Tarreau11382812008-07-09 16:18:21 +02001185 acl_res = acl_neg(acl_res);
Willy Tarreaua84d3742007-05-07 00:36:48 +02001186
1187 suite_res &= acl_res;
Willy Tarreau11382812008-07-09 16:18:21 +02001188
1189 /* we're ANDing these terms, so a single FAIL is enough */
1190 if (suite_res == ACL_PAT_FAIL)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001191 break;
1192 }
1193 cond_res |= suite_res;
Willy Tarreau11382812008-07-09 16:18:21 +02001194
1195 /* we're ORing these terms, so a single PASS is enough */
1196 if (cond_res == ACL_PAT_PASS)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001197 break;
1198 }
Willy Tarreau11382812008-07-09 16:18:21 +02001199 return cond_res;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001200}
1201
1202
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001203/* Reports a pointer to the first ACL used in condition <cond> which requires
1204 * at least one of the USE_FLAGS in <require>. Returns NULL if none matches.
1205 * The construct is almost the same as for acl_exec_cond() since we're walking
1206 * down the ACL tree as well. It is important that the tree is really walked
1207 * through and never cached, because that way, this function can be used as a
1208 * late check.
1209 */
Willy Tarreauf1e98b82010-01-28 17:59:39 +01001210struct acl *cond_find_require(const struct acl_cond *cond, unsigned int require)
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001211{
1212 struct acl_term_suite *suite;
1213 struct acl_term *term;
1214 struct acl *acl;
1215
1216 list_for_each_entry(suite, &cond->suites, list) {
1217 list_for_each_entry(term, &suite->terms, list) {
1218 acl = term->acl;
1219 if (acl->requires & require)
1220 return acl;
1221 }
1222 }
1223 return NULL;
1224}
1225
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001226/*
1227 * Find targets for userlist and groups in acl. Function returns the number
1228 * of errors or OK if everything is fine.
1229 */
1230int
1231acl_find_targets(struct proxy *p)
1232{
1233
1234 struct acl *acl;
1235 struct acl_expr *expr;
1236 struct acl_pattern *pattern;
1237 struct userlist *ul;
1238 int cfgerr = 0;
1239
1240 list_for_each_entry(acl, &p->acl, list) {
1241 list_for_each_entry(expr, &acl->expr, list) {
1242 if (strstr(expr->kw->kw, "http_auth") == expr->kw->kw) {
1243
1244 if (!expr->arg.str || !*expr->arg.str) {
1245 Alert("proxy %s: acl %s %s(): missing userlist name.\n",
1246 p->id, acl->name, expr->kw->kw);
1247 cfgerr++;
1248 continue;
1249 }
1250
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01001251 if (p->uri_auth && p->uri_auth->userlist &&
1252 !strcmp(p->uri_auth->userlist->name, expr->arg.str))
1253 ul = p->uri_auth->userlist;
1254 else
1255 ul = auth_find_userlist(expr->arg.str);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001256
1257 if (!ul) {
1258 Alert("proxy %s: acl %s %s(%s): unable to find userlist.\n",
1259 p->id, acl->name, expr->kw->kw, expr->arg.str);
1260 cfgerr++;
1261 continue;
1262 }
1263
1264 expr->arg_len = 0;
1265 expr->arg.ul = ul;
1266 }
1267
1268
1269 if (!strcmp(expr->kw->kw, "http_auth_group")) {
1270
1271 if (LIST_ISEMPTY(&expr->patterns)) {
1272 Alert("proxy %s: acl %s %s(): no groups specified.\n",
1273 p->id, acl->name, expr->kw->kw);
1274 cfgerr++;
1275 continue;
1276 }
1277
1278 list_for_each_entry(pattern, &expr->patterns, list) {
1279 pattern->val.group_mask = auth_resolve_groups(expr->arg.ul, pattern->ptr.str);
1280
1281 free(pattern->ptr.str);
1282 pattern->ptr.str = NULL;
1283 pattern->len = 0;
1284
1285 if (!pattern->val.group_mask) {
1286 Alert("proxy %s: acl %s %s(): invalid group(s).\n",
1287 p->id, acl->name, expr->kw->kw);
1288 cfgerr++;
1289 continue;
1290 }
1291 }
1292 }
1293 }
1294 }
1295
1296 return cfgerr;
1297}
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001298
Willy Tarreaua84d3742007-05-07 00:36:48 +02001299/************************************************************************/
1300/* All supported keywords must be declared here. */
1301/************************************************************************/
1302
1303/* Note: must not be declared <const> as its list will be overwritten */
1304static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02001305 { "always_true", acl_parse_nothing, acl_fetch_true, acl_match_nothing, ACL_USE_NOTHING },
1306 { "always_false", acl_parse_nothing, acl_fetch_false, acl_match_nothing, ACL_USE_NOTHING },
1307 { "wait_end", acl_parse_nothing, acl_fetch_wait_end, acl_match_nothing, ACL_USE_NOTHING },
Willy Tarreaua84d3742007-05-07 00:36:48 +02001308#if 0
1309 { "time", acl_parse_time, acl_fetch_time, acl_match_time },
1310#endif
1311 { NULL, NULL, NULL, NULL }
1312}};
1313
1314
1315__attribute__((constructor))
1316static void __acl_init(void)
1317{
1318 acl_register_keywords(&acl_kws);
1319}
1320
1321
1322/*
1323 * Local variables:
1324 * c-indent-level: 8
1325 * c-basic-offset: 8
1326 * End:
1327 */