blob: ddbd4bc2da2ede53e51075ba921b3fe0ea9ba4c6 [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>
20
21#include <proto/acl.h>
22
23#include <types/acl.h>
24#include <types/proxy.h>
25#include <types/session.h>
26
27/* List head of all known ACL keywords */
28static struct acl_kw_list acl_keywords = {
29 .list = LIST_HEAD_INIT(acl_keywords.list)
30};
31
32
Willy Tarreaua5909832007-06-17 20:40:25 +020033/*
34 * These functions are only used for debugging complex configurations.
Willy Tarreaua84d3742007-05-07 00:36:48 +020035 */
Willy Tarreaua5909832007-06-17 20:40:25 +020036
37/* ignore the current line */
38static int
39acl_parse_nothing(const char **text, struct acl_pattern *pattern, int *opaque)
40{
41 return 1;
42}
43
44/* always fake a data retrieval */
45static int
46acl_fetch_nothing(struct proxy *px, struct session *l4, void *l7, int dir,
47 struct acl_expr *expr, struct acl_test *test)
Willy Tarreaua84d3742007-05-07 00:36:48 +020048{
49 return 1;
50}
51
Willy Tarreaua5909832007-06-17 20:40:25 +020052/* always return true */
53static int
54acl_match_true(struct acl_test *test, struct acl_pattern *pattern)
55{
Willy Tarreau11382812008-07-09 16:18:21 +020056 return ACL_PAT_PASS;
Willy Tarreaua5909832007-06-17 20:40:25 +020057}
58
59/* always return false */
60static int
61acl_match_false(struct acl_test *test, struct acl_pattern *pattern)
62{
Willy Tarreau11382812008-07-09 16:18:21 +020063 return ACL_PAT_FAIL;
Willy Tarreaua5909832007-06-17 20:40:25 +020064}
65
66
Willy Tarreaua84d3742007-05-07 00:36:48 +020067/* NB: For two strings to be identical, it is required that their lengths match */
68int acl_match_str(struct acl_test *test, struct acl_pattern *pattern)
69{
Willy Tarreauc8d7c962007-06-17 08:20:33 +020070 int icase;
71
Willy Tarreaua84d3742007-05-07 00:36:48 +020072 if (pattern->len != test->len)
Willy Tarreau11382812008-07-09 16:18:21 +020073 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +020074
75 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
76 if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) == 0) ||
77 (!icase && strncmp(pattern->ptr.str, test->ptr, test->len) == 0))
Willy Tarreau11382812008-07-09 16:18:21 +020078 return ACL_PAT_PASS;
79 return ACL_PAT_FAIL;
Willy Tarreaua84d3742007-05-07 00:36:48 +020080}
81
Willy Tarreauf3d25982007-05-08 22:45:09 +020082/* Executes a regex. It needs to change the data. If it is marked READ_ONLY
83 * then it will be allocated and duplicated in place so that others may use
84 * it later on. Note that this is embarrassing because we always try to avoid
85 * allocating memory at run time.
86 */
87int acl_match_reg(struct acl_test *test, struct acl_pattern *pattern)
88{
89 char old_char;
90 int ret;
91
92 if (unlikely(test->flags & ACL_TEST_F_READ_ONLY)) {
93 char *new_str;
94
95 new_str = calloc(1, test->len + 1);
96 if (!new_str)
Willy Tarreau11382812008-07-09 16:18:21 +020097 return ACL_PAT_FAIL;
Willy Tarreauf3d25982007-05-08 22:45:09 +020098
99 memcpy(new_str, test->ptr, test->len);
100 new_str[test->len] = 0;
101 if (test->flags & ACL_TEST_F_MUST_FREE)
102 free(test->ptr);
103 test->ptr = new_str;
104 test->flags |= ACL_TEST_F_MUST_FREE;
105 test->flags &= ~ACL_TEST_F_READ_ONLY;
106 }
107
108 old_char = test->ptr[test->len];
109 test->ptr[test->len] = 0;
110
111 if (regexec(pattern->ptr.reg, test->ptr, 0, NULL, 0) == 0)
Willy Tarreau11382812008-07-09 16:18:21 +0200112 ret = ACL_PAT_PASS;
Willy Tarreauf3d25982007-05-08 22:45:09 +0200113 else
Willy Tarreau11382812008-07-09 16:18:21 +0200114 ret = ACL_PAT_FAIL;
Willy Tarreauf3d25982007-05-08 22:45:09 +0200115
116 test->ptr[test->len] = old_char;
117 return ret;
118}
119
Willy Tarreaua84d3742007-05-07 00:36:48 +0200120/* Checks that the pattern matches the beginning of the tested string. */
121int acl_match_beg(struct acl_test *test, struct acl_pattern *pattern)
122{
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200123 int icase;
124
Willy Tarreaua84d3742007-05-07 00:36:48 +0200125 if (pattern->len > test->len)
Willy Tarreau11382812008-07-09 16:18:21 +0200126 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200127
128 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
129 if ((icase && strncasecmp(pattern->ptr.str, test->ptr, pattern->len) != 0) ||
130 (!icase && strncmp(pattern->ptr.str, test->ptr, pattern->len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +0200131 return ACL_PAT_FAIL;
132 return ACL_PAT_PASS;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200133}
134
135/* Checks that the pattern matches the end of the tested string. */
136int acl_match_end(struct acl_test *test, struct acl_pattern *pattern)
137{
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200138 int icase;
139
Willy Tarreaua84d3742007-05-07 00:36:48 +0200140 if (pattern->len > test->len)
Willy Tarreau11382812008-07-09 16:18:21 +0200141 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200142 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
143 if ((icase && strncasecmp(pattern->ptr.str, test->ptr + test->len - pattern->len, pattern->len) != 0) ||
144 (!icase && strncmp(pattern->ptr.str, test->ptr + test->len - pattern->len, pattern->len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +0200145 return ACL_PAT_FAIL;
146 return ACL_PAT_PASS;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200147}
148
149/* Checks that the pattern is included inside the tested string.
150 * NB: Suboptimal, should be rewritten using a Boyer-Moore method.
151 */
152int acl_match_sub(struct acl_test *test, struct acl_pattern *pattern)
153{
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200154 int icase;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200155 char *end;
156 char *c;
157
158 if (pattern->len > test->len)
Willy Tarreau11382812008-07-09 16:18:21 +0200159 return ACL_PAT_FAIL;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200160
161 end = test->ptr + test->len - pattern->len;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200162 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
163 if (icase) {
164 for (c = test->ptr; c <= end; c++) {
165 if (tolower(*c) != tolower(*pattern->ptr.str))
166 continue;
167 if (strncasecmp(pattern->ptr.str, c, pattern->len) == 0)
Willy Tarreau11382812008-07-09 16:18:21 +0200168 return ACL_PAT_PASS;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200169 }
170 } else {
171 for (c = test->ptr; c <= end; c++) {
172 if (*c != *pattern->ptr.str)
173 continue;
174 if (strncmp(pattern->ptr.str, c, pattern->len) == 0)
Willy Tarreau11382812008-07-09 16:18:21 +0200175 return ACL_PAT_PASS;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200176 }
Willy Tarreaua84d3742007-05-07 00:36:48 +0200177 }
Willy Tarreau11382812008-07-09 16:18:21 +0200178 return ACL_PAT_FAIL;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200179}
180
181/* This one is used by other real functions. It checks that the pattern is
182 * included inside the tested string, but enclosed between the specified
183 * delimitor, or a '/' or a '?' or at the beginning or end of the string.
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200184 * The delimitor is stripped at the beginning or end of the pattern.
Willy Tarreaua84d3742007-05-07 00:36:48 +0200185 */
186static int match_word(struct acl_test *test, struct acl_pattern *pattern, char delim)
187{
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200188 int may_match, icase;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200189 char *c, *end;
190 char *ps;
191 int pl;
192
193 pl = pattern->len;
194 ps = pattern->ptr.str;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200195 while (pl > 0 && (*ps == delim || *ps == '/' || *ps == '?')) {
Willy Tarreaua84d3742007-05-07 00:36:48 +0200196 pl--;
197 ps++;
198 }
199
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200200 while (pl > 0 &&
201 (ps[pl - 1] == delim || ps[pl - 1] == '/' || ps[pl - 1] == '?'))
Willy Tarreaua84d3742007-05-07 00:36:48 +0200202 pl--;
203
204 if (pl > test->len)
Willy Tarreau11382812008-07-09 16:18:21 +0200205 return ACL_PAT_FAIL;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200206
207 may_match = 1;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200208 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200209 end = test->ptr + test->len - pl;
210 for (c = test->ptr; c <= end; c++) {
211 if (*c == '/' || *c == delim || *c == '?') {
212 may_match = 1;
213 continue;
214 }
Willy Tarreaua84d3742007-05-07 00:36:48 +0200215
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200216 if (!may_match)
217 continue;
218
219 if (icase) {
220 if ((tolower(*c) == tolower(*ps)) &&
221 (strncasecmp(ps, c, pl) == 0) &&
222 (c == end || c[pl] == '/' || c[pl] == delim || c[pl] == '?'))
Willy Tarreau11382812008-07-09 16:18:21 +0200223 return ACL_PAT_PASS;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200224 } else {
225 if ((*c == *ps) &&
226 (strncmp(ps, c, pl) == 0) &&
227 (c == end || c[pl] == '/' || c[pl] == delim || c[pl] == '?'))
Willy Tarreau11382812008-07-09 16:18:21 +0200228 return ACL_PAT_PASS;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200229 }
Willy Tarreaua84d3742007-05-07 00:36:48 +0200230 may_match = 0;
231 }
Willy Tarreau11382812008-07-09 16:18:21 +0200232 return ACL_PAT_FAIL;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200233}
234
235/* Checks that the pattern is included inside the tested string, but enclosed
236 * between slashes or at the beginning or end of the string. Slashes at the
237 * beginning or end of the pattern are ignored.
238 */
239int acl_match_dir(struct acl_test *test, struct acl_pattern *pattern)
240{
241 return match_word(test, pattern, '/');
242}
243
244/* Checks that the pattern is included inside the tested string, but enclosed
245 * between dots or at the beginning or end of the string. Dots at the beginning
246 * or end of the pattern are ignored.
247 */
248int acl_match_dom(struct acl_test *test, struct acl_pattern *pattern)
249{
250 return match_word(test, pattern, '.');
251}
252
253/* Checks that the integer in <test> is included between min and max */
Willy Tarreauae8b7962007-06-09 23:10:04 +0200254int acl_match_int(struct acl_test *test, struct acl_pattern *pattern)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200255{
Willy Tarreauae8b7962007-06-09 23:10:04 +0200256 if ((!pattern->val.range.min_set || pattern->val.range.min <= test->i) &&
257 (!pattern->val.range.max_set || test->i <= pattern->val.range.max))
Willy Tarreau11382812008-07-09 16:18:21 +0200258 return ACL_PAT_PASS;
259 return ACL_PAT_FAIL;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200260}
261
Willy Tarreaua67fad92007-05-08 19:50:09 +0200262int acl_match_ip(struct acl_test *test, struct acl_pattern *pattern)
263{
264 struct in_addr *s;
265
266 if (test->i != AF_INET)
Willy Tarreau11382812008-07-09 16:18:21 +0200267 return ACL_PAT_FAIL;
Willy Tarreaua67fad92007-05-08 19:50:09 +0200268
269 s = (void *)test->ptr;
270 if (((s->s_addr ^ pattern->val.ipv4.addr.s_addr) & pattern->val.ipv4.mask.s_addr) == 0)
Willy Tarreau11382812008-07-09 16:18:21 +0200271 return ACL_PAT_PASS;
272 return ACL_PAT_FAIL;
Willy Tarreaua67fad92007-05-08 19:50:09 +0200273}
274
Willy Tarreaua84d3742007-05-07 00:36:48 +0200275/* Parse a string. It is allocated and duplicated. */
Willy Tarreauae8b7962007-06-09 23:10:04 +0200276int acl_parse_str(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200277{
278 int len;
279
Willy Tarreauae8b7962007-06-09 23:10:04 +0200280 len = strlen(*text);
281 pattern->ptr.str = strdup(*text);
Willy Tarreaua84d3742007-05-07 00:36:48 +0200282 if (!pattern->ptr.str)
283 return 0;
284 pattern->len = len;
285 return 1;
286}
287
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +0200288/* Free data allocated by acl_parse_reg */
289static void acl_free_reg(void *ptr) {
290
291 regfree((regex_t *)ptr);
292}
293
Willy Tarreauf3d25982007-05-08 22:45:09 +0200294/* Parse a regex. It is allocated. */
Willy Tarreauae8b7962007-06-09 23:10:04 +0200295int acl_parse_reg(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreauf3d25982007-05-08 22:45:09 +0200296{
297 regex_t *preg;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200298 int icase;
Willy Tarreauf3d25982007-05-08 22:45:09 +0200299
300 preg = calloc(1, sizeof(regex_t));
301
302 if (!preg)
303 return 0;
304
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200305 icase = (pattern->flags & ACL_PAT_F_IGNORE_CASE) ? REG_ICASE : 0;
306 if (regcomp(preg, *text, REG_EXTENDED | REG_NOSUB | icase) != 0) {
Willy Tarreauf3d25982007-05-08 22:45:09 +0200307 free(preg);
308 return 0;
309 }
310
311 pattern->ptr.reg = preg;
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +0200312 pattern->freeptrbuf = &acl_free_reg;
Willy Tarreauf3d25982007-05-08 22:45:09 +0200313 return 1;
314}
315
Willy Tarreauae8b7962007-06-09 23:10:04 +0200316/* Parse a range of positive integers delimited by either ':' or '-'. If only
317 * one integer is read, it is set as both min and max. An operator may be
318 * specified as the prefix, among this list of 5 :
319 *
320 * 0:eq, 1:gt, 2:ge, 3:lt, 4:le
321 *
322 * The default operator is "eq". It supports range matching. Ranges are
323 * rejected for other operators. The operator may be changed at any time.
324 * The operator is stored in the 'opaque' argument.
325 *
Willy Tarreaua84d3742007-05-07 00:36:48 +0200326 */
Willy Tarreauae8b7962007-06-09 23:10:04 +0200327int acl_parse_int(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200328{
Willy Tarreauae8b7962007-06-09 23:10:04 +0200329 signed long long i;
330 unsigned int j, last, skip = 0;
331 const char *ptr = *text;
332
333
Willy Tarreau8f8e6452007-06-17 21:51:38 +0200334 while (!isdigit((unsigned char)*ptr)) {
Willy Tarreauae8b7962007-06-09 23:10:04 +0200335 if (strcmp(ptr, "eq") == 0) *opaque = 0;
336 else if (strcmp(ptr, "gt") == 0) *opaque = 1;
337 else if (strcmp(ptr, "ge") == 0) *opaque = 2;
338 else if (strcmp(ptr, "lt") == 0) *opaque = 3;
339 else if (strcmp(ptr, "le") == 0) *opaque = 4;
340 else
341 return 0;
342
343 skip++;
344 ptr = text[skip];
345 }
Willy Tarreaua84d3742007-05-07 00:36:48 +0200346
347 last = i = 0;
348 while (1) {
Willy Tarreauae8b7962007-06-09 23:10:04 +0200349 j = *ptr++;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200350 if ((j == '-' || j == ':') && !last) {
351 last++;
352 pattern->val.range.min = i;
353 i = 0;
354 continue;
355 }
356 j -= '0';
357 if (j > 9)
358 // also catches the terminating zero
359 break;
360 i *= 10;
361 i += j;
362 }
Willy Tarreauae8b7962007-06-09 23:10:04 +0200363
364 if (last && *opaque >= 1 && *opaque <= 4)
365 /* having a range with a min or a max is absurd */
366 return 0;
367
Willy Tarreaua84d3742007-05-07 00:36:48 +0200368 if (!last)
369 pattern->val.range.min = i;
370 pattern->val.range.max = i;
Willy Tarreauae8b7962007-06-09 23:10:04 +0200371
372 switch (*opaque) {
373 case 0: /* eq */
374 pattern->val.range.min_set = 1;
375 pattern->val.range.max_set = 1;
376 break;
377 case 1: /* gt */
378 pattern->val.range.min++; /* gt = ge + 1 */
379 case 2: /* ge */
380 pattern->val.range.min_set = 1;
381 pattern->val.range.max_set = 0;
382 break;
383 case 3: /* lt */
384 pattern->val.range.max--; /* lt = le - 1 */
385 case 4: /* le */
386 pattern->val.range.min_set = 0;
387 pattern->val.range.max_set = 1;
388 break;
389 }
390 return skip + 1;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200391}
392
Willy Tarreaua67fad92007-05-08 19:50:09 +0200393/* Parse an IP address and an optional mask in the form addr[/mask].
394 * The addr may either be an IPv4 address or a hostname. The mask
395 * may either be a dotted mask or a number of bits. Returns 1 if OK,
396 * otherwise 0.
397 */
Willy Tarreauae8b7962007-06-09 23:10:04 +0200398int acl_parse_ip(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreaua67fad92007-05-08 19:50:09 +0200399{
Willy Tarreauae8b7962007-06-09 23:10:04 +0200400 if (str2net(*text, &pattern->val.ipv4.addr, &pattern->val.ipv4.mask))
401 return 1;
402 else
403 return 0;
Willy Tarreaua67fad92007-05-08 19:50:09 +0200404}
405
Willy Tarreaua84d3742007-05-07 00:36:48 +0200406/*
407 * Registers the ACL keyword list <kwl> as a list of valid keywords for next
408 * parsing sessions.
409 */
410void acl_register_keywords(struct acl_kw_list *kwl)
411{
412 LIST_ADDQ(&acl_keywords.list, &kwl->list);
413}
414
415/*
416 * Unregisters the ACL keyword list <kwl> from the list of valid keywords.
417 */
418void acl_unregister_keywords(struct acl_kw_list *kwl)
419{
420 LIST_DEL(&kwl->list);
421 LIST_INIT(&kwl->list);
422}
423
424/* Return a pointer to the ACL <name> within the list starting at <head>, or
425 * NULL if not found.
426 */
427struct acl *find_acl_by_name(const char *name, struct list *head)
428{
429 struct acl *acl;
430 list_for_each_entry(acl, head, list) {
431 if (strcmp(acl->name, name) == 0)
432 return acl;
433 }
434 return NULL;
435}
436
437/* Return a pointer to the ACL keyword <kw>, or NULL if not found. Note that if
438 * <kw> contains an opening parenthesis, only the left part of it is checked.
439 */
440struct acl_keyword *find_acl_kw(const char *kw)
441{
442 int index;
443 const char *kwend;
444 struct acl_kw_list *kwl;
445
446 kwend = strchr(kw, '(');
447 if (!kwend)
448 kwend = kw + strlen(kw);
449
450 list_for_each_entry(kwl, &acl_keywords.list, list) {
451 for (index = 0; kwl->kw[index].kw != NULL; index++) {
452 if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) &&
453 kwl->kw[index].kw[kwend-kw] == 0)
454 return &kwl->kw[index];
455 }
456 }
457 return NULL;
458}
459
460static void free_pattern(struct acl_pattern *pat)
461{
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +0200462
463 if (pat->ptr.ptr) {
464 if (pat->freeptrbuf)
465 pat->freeptrbuf(pat->ptr.ptr);
466
Willy Tarreaua84d3742007-05-07 00:36:48 +0200467 free(pat->ptr.ptr);
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +0200468 }
469
Willy Tarreaua84d3742007-05-07 00:36:48 +0200470 free(pat);
471}
472
473static void free_pattern_list(struct list *head)
474{
475 struct acl_pattern *pat, *tmp;
476 list_for_each_entry_safe(pat, tmp, head, list)
477 free_pattern(pat);
478}
479
480static struct acl_expr *prune_acl_expr(struct acl_expr *expr)
481{
482 free_pattern_list(&expr->patterns);
483 LIST_INIT(&expr->patterns);
484 if (expr->arg.str)
485 free(expr->arg.str);
486 expr->kw->use_cnt--;
487 return expr;
488}
489
490/* Parse an ACL expression starting at <args>[0], and return it.
491 * Right now, the only accepted syntax is :
492 * <subject> [<value>...]
493 */
494struct acl_expr *parse_acl_expr(const char **args)
495{
496 __label__ out_return, out_free_expr, out_free_pattern;
497 struct acl_expr *expr;
498 struct acl_keyword *aclkw;
499 struct acl_pattern *pattern;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200500 int opaque, patflags;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200501 const char *arg;
502
503 aclkw = find_acl_kw(args[0]);
504 if (!aclkw || !aclkw->parse)
505 goto out_return;
506
507 expr = (struct acl_expr *)calloc(1, sizeof(*expr));
508 if (!expr)
509 goto out_return;
510
511 expr->kw = aclkw;
512 aclkw->use_cnt++;
513 LIST_INIT(&expr->patterns);
514 expr->arg.str = NULL;
Willy Tarreaubb768912007-06-10 11:17:01 +0200515 expr->arg_len = 0;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200516
517 arg = strchr(args[0], '(');
518 if (arg != NULL) {
519 char *end, *arg2;
520 /* there is an argument in the form "subject(arg)" */
521 arg++;
522 end = strchr(arg, ')');
523 if (!end)
524 goto out_free_expr;
525 arg2 = (char *)calloc(1, end - arg + 1);
526 if (!arg2)
527 goto out_free_expr;
528 memcpy(arg2, arg, end - arg);
529 arg2[end-arg] = '\0';
Willy Tarreaubb768912007-06-10 11:17:01 +0200530 expr->arg_len = end - arg;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200531 expr->arg.str = arg2;
532 }
533
Willy Tarreaua84d3742007-05-07 00:36:48 +0200534 args++;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200535
536 /* check for options before patterns. Supported options are :
537 * -i : ignore case for all patterns by default
538 * -f : read patterns from those files
539 * -- : everything after this is not an option
540 */
541 patflags = 0;
542 while (**args == '-') {
543 if ((*args)[1] == 'i')
544 patflags |= ACL_PAT_F_IGNORE_CASE;
545 else if ((*args)[1] == 'f')
546 patflags |= ACL_PAT_F_FROM_FILE;
547 else if ((*args)[1] == '-') {
548 args++;
549 break;
550 }
551 else
552 break;
553 args++;
554 }
555
556 /* now parse all patterns */
Willy Tarreauae8b7962007-06-09 23:10:04 +0200557 opaque = 0;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200558 while (**args) {
Willy Tarreauae8b7962007-06-09 23:10:04 +0200559 int ret;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200560 pattern = (struct acl_pattern *)calloc(1, sizeof(*pattern));
561 if (!pattern)
562 goto out_free_expr;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200563 pattern->flags = patflags;
564
Willy Tarreauae8b7962007-06-09 23:10:04 +0200565 ret = aclkw->parse(args, pattern, &opaque);
566 if (!ret)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200567 goto out_free_pattern;
568 LIST_ADDQ(&expr->patterns, &pattern->list);
Willy Tarreauae8b7962007-06-09 23:10:04 +0200569 args += ret;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200570 }
571
572 return expr;
573
574 out_free_pattern:
575 free_pattern(pattern);
576 out_free_expr:
577 prune_acl_expr(expr);
578 free(expr);
579 out_return:
580 return NULL;
581}
582
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +0200583/* Purge everything in the acl <acl>, then return <acl>. */
584struct acl *prune_acl(struct acl *acl) {
585
586 struct acl_expr *expr, *exprb;
587
588 free(acl->name);
589
590 list_for_each_entry_safe(expr, exprb, &acl->expr, list) {
591 LIST_DEL(&expr->list);
592 prune_acl_expr(expr);
593 free(expr);
594 }
595
596 return acl;
597}
598
Willy Tarreaua84d3742007-05-07 00:36:48 +0200599/* Parse an ACL with the name starting at <args>[0], and with a list of already
600 * known ACLs in <acl>. If the ACL was not in the list, it will be added.
601 * A pointer to that ACL is returned.
602 *
603 * args syntax: <aclname> <acl_expr>
604 */
605struct acl *parse_acl(const char **args, struct list *known_acl)
606{
607 __label__ out_return, out_free_acl_expr, out_free_name;
608 struct acl *cur_acl;
609 struct acl_expr *acl_expr;
610 char *name;
611
Willy Tarreau2e74c3f2007-12-02 18:45:09 +0100612 if (invalid_char(*args))
613 goto out_return;
614
Willy Tarreaua84d3742007-05-07 00:36:48 +0200615 acl_expr = parse_acl_expr(args + 1);
616 if (!acl_expr)
617 goto out_return;
618
619 cur_acl = find_acl_by_name(args[0], known_acl);
620 if (!cur_acl) {
621 name = strdup(args[0]);
622 if (!name)
623 goto out_free_acl_expr;
624 cur_acl = (struct acl *)calloc(1, sizeof(*cur_acl));
625 if (cur_acl == NULL)
626 goto out_free_name;
627
628 LIST_INIT(&cur_acl->expr);
629 LIST_ADDQ(known_acl, &cur_acl->list);
630 cur_acl->name = name;
631 }
632
633 LIST_ADDQ(&cur_acl->expr, &acl_expr->list);
634 return cur_acl;
635
636 out_free_name:
637 free(name);
638 out_free_acl_expr:
639 prune_acl_expr(acl_expr);
640 free(acl_expr);
641 out_return:
642 return NULL;
643}
644
Willy Tarreau16fbe822007-06-17 11:54:31 +0200645/* Some useful ACLs provided by default. Only those used are allocated. */
646
647const struct {
648 const char *name;
649 const char *expr[4]; /* put enough for longest expression */
650} default_acl_list[] = {
Willy Tarreaua5909832007-06-17 20:40:25 +0200651 { .name = "TRUE", .expr = {"always_true","1",""}},
652 { .name = "FALSE", .expr = {"always_false","0",""}},
Willy Tarreau16fbe822007-06-17 11:54:31 +0200653 { .name = "LOCALHOST", .expr = {"src","127.0.0.1/8",""}},
654 { .name = "HTTP_1.0", .expr = {"req_ver","1.0",""}},
655 { .name = "HTTP_1.1", .expr = {"req_ver","1.1",""}},
656 { .name = "METH_CONNECT", .expr = {"method","CONNECT",""}},
657 { .name = "METH_GET", .expr = {"method","GET","HEAD",""}},
658 { .name = "METH_HEAD", .expr = {"method","HEAD",""}},
659 { .name = "METH_OPTIONS", .expr = {"method","OPTIONS",""}},
660 { .name = "METH_POST", .expr = {"method","POST",""}},
661 { .name = "METH_TRACE", .expr = {"method","TRACE",""}},
662 { .name = "HTTP_URL_ABS", .expr = {"url_reg","^[^/:]*://",""}},
663 { .name = "HTTP_URL_SLASH", .expr = {"url_beg","/",""}},
664 { .name = "HTTP_URL_STAR", .expr = {"url","*",""}},
665 { .name = "HTTP_CONTENT", .expr = {"hdr_val(content-length)","gt","0",""}},
666 { .name = NULL, .expr = {""}}
667};
668
669/* Find a default ACL from the default_acl list, compile it and return it.
670 * If the ACL is not found, NULL is returned. In theory, it cannot fail,
671 * except when default ACLs are broken, in which case it will return NULL.
672 * If <known_acl> is not NULL, the ACL will be queued at its tail.
673 */
674struct acl *find_acl_default(const char *acl_name, struct list *known_acl)
675{
676 __label__ out_return, out_free_acl_expr, out_free_name;
677 struct acl *cur_acl;
678 struct acl_expr *acl_expr;
679 char *name;
680 int index;
681
682 for (index = 0; default_acl_list[index].name != NULL; index++) {
683 if (strcmp(acl_name, default_acl_list[index].name) == 0)
684 break;
685 }
686
687 if (default_acl_list[index].name == NULL)
688 return NULL;
689
690 acl_expr = parse_acl_expr((const char **)default_acl_list[index].expr);
691 if (!acl_expr)
692 goto out_return;
693
694 name = strdup(acl_name);
695 if (!name)
696 goto out_free_acl_expr;
697 cur_acl = (struct acl *)calloc(1, sizeof(*cur_acl));
698 if (cur_acl == NULL)
699 goto out_free_name;
700
701 cur_acl->name = name;
702 LIST_INIT(&cur_acl->expr);
703 LIST_ADDQ(&cur_acl->expr, &acl_expr->list);
704 if (known_acl)
705 LIST_ADDQ(known_acl, &cur_acl->list);
706
707 return cur_acl;
708
709 out_free_name:
710 free(name);
711 out_free_acl_expr:
712 prune_acl_expr(acl_expr);
713 free(acl_expr);
714 out_return:
715 return NULL;
716}
Willy Tarreaua84d3742007-05-07 00:36:48 +0200717
718/* Purge everything in the acl_cond <cond>, then return <cond>. */
719struct acl_cond *prune_acl_cond(struct acl_cond *cond)
720{
721 struct acl_term_suite *suite, *tmp_suite;
722 struct acl_term *term, *tmp_term;
723
724 /* iterate through all term suites and free all terms and all suites */
725 list_for_each_entry_safe(suite, tmp_suite, &cond->suites, list) {
726 list_for_each_entry_safe(term, tmp_term, &suite->terms, list)
727 free(term);
728 free(suite);
729 }
730 return cond;
731}
732
733/* Parse an ACL condition starting at <args>[0], relying on a list of already
734 * known ACLs passed in <known_acl>. The new condition is returned (or NULL in
735 * case of low memory). Supports multiple conditions separated by "or".
736 */
737struct acl_cond *parse_acl_cond(const char **args, struct list *known_acl, int pol)
738{
739 __label__ out_return, out_free_suite, out_free_term;
Willy Tarreau74b98a82007-06-16 19:35:18 +0200740 int arg, neg;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200741 const char *word;
742 struct acl *cur_acl;
743 struct acl_term *cur_term;
744 struct acl_term_suite *cur_suite;
745 struct acl_cond *cond;
746
747 cond = (struct acl_cond *)calloc(1, sizeof(*cond));
748 if (cond == NULL)
749 goto out_return;
750
751 LIST_INIT(&cond->list);
752 LIST_INIT(&cond->suites);
753 cond->pol = pol;
754
755 cur_suite = NULL;
Willy Tarreau74b98a82007-06-16 19:35:18 +0200756 neg = 0;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200757 for (arg = 0; *args[arg]; arg++) {
758 word = args[arg];
759
760 /* remove as many exclamation marks as we can */
761 while (*word == '!') {
762 neg = !neg;
763 word++;
764 }
765
766 /* an empty word is allowed because we cannot force the user to
767 * always think about not leaving exclamation marks alone.
768 */
769 if (!*word)
770 continue;
771
Willy Tarreau16fbe822007-06-17 11:54:31 +0200772 if (strcasecmp(word, "or") == 0 || strcmp(word, "||") == 0) {
Willy Tarreaua84d3742007-05-07 00:36:48 +0200773 /* new term suite */
774 cur_suite = NULL;
775 neg = 0;
776 continue;
777 }
778
Willy Tarreau16fbe822007-06-17 11:54:31 +0200779 /* search for <word> in the known ACL names. If we do not find
780 * it, let's look for it in the default ACLs, and if found, add
781 * it to the list of ACLs of this proxy. This makes it possible
782 * to override them.
783 */
Willy Tarreaua84d3742007-05-07 00:36:48 +0200784 cur_acl = find_acl_by_name(word, known_acl);
Willy Tarreau16fbe822007-06-17 11:54:31 +0200785 if (cur_acl == NULL) {
786 cur_acl = find_acl_default(word, known_acl);
787 if (cur_acl == NULL)
788 goto out_free_suite;
789 }
Willy Tarreaua84d3742007-05-07 00:36:48 +0200790
791 cur_term = (struct acl_term *)calloc(1, sizeof(*cur_term));
792 if (cur_term == NULL)
793 goto out_free_suite;
794
795 cur_term->acl = cur_acl;
796 cur_term->neg = neg;
797
798 if (!cur_suite) {
799 cur_suite = (struct acl_term_suite *)calloc(1, sizeof(*cur_suite));
800 if (cur_term == NULL)
801 goto out_free_term;
802 LIST_INIT(&cur_suite->terms);
803 LIST_ADDQ(&cond->suites, &cur_suite->list);
804 }
805 LIST_ADDQ(&cur_suite->terms, &cur_term->list);
Willy Tarreau74b98a82007-06-16 19:35:18 +0200806 neg = 0;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200807 }
808
809 return cond;
810
811 out_free_term:
812 free(cur_term);
813 out_free_suite:
814 prune_acl_cond(cond);
815 free(cond);
816 out_return:
817 return NULL;
818}
819
Willy Tarreau11382812008-07-09 16:18:21 +0200820/* Execute condition <cond> and return either ACL_PAT_FAIL, ACL_PAT_MISS or
821 * ACL_PAT_PASS depending on the test results. This function only computes the
822 * condition, it does not apply the polarity required by IF/UNLESS, it's up to
823 * the caller to do this using something like this :
824 *
825 * res = acl_pass(res);
826 * if (cond->pol == ACL_COND_UNLESS)
827 * res = !res;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200828 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +0200829int acl_exec_cond(struct acl_cond *cond, struct proxy *px, struct session *l4, void *l7, int dir)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200830{
831 __label__ fetch_next;
832 struct acl_term_suite *suite;
833 struct acl_term *term;
834 struct acl_expr *expr;
835 struct acl *acl;
836 struct acl_pattern *pattern;
837 struct acl_test test;
Willy Tarreau11382812008-07-09 16:18:21 +0200838 int acl_res, suite_res, cond_res;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200839
Willy Tarreau11382812008-07-09 16:18:21 +0200840 /* We're doing a logical OR between conditions so we initialize to FAIL.
841 * The MISS status is propagated down from the suites.
842 */
Willy Tarreaua84d3742007-05-07 00:36:48 +0200843 cond_res = ACL_PAT_FAIL;
844 list_for_each_entry(suite, &cond->suites, list) {
Willy Tarreau11382812008-07-09 16:18:21 +0200845 /* Evaluate condition suite <suite>. We stop at the first term
846 * which returns ACL_PAT_FAIL. The MISS status is still propagated
847 * in case of uncertainty in the result.
Willy Tarreaua84d3742007-05-07 00:36:48 +0200848 */
849
850 /* we're doing a logical AND between terms, so we must set the
851 * initial value to PASS.
852 */
853 suite_res = ACL_PAT_PASS;
854 list_for_each_entry(term, &suite->terms, list) {
855 acl = term->acl;
856
857 /* FIXME: use cache !
858 * check acl->cache_idx for this.
859 */
860
861 /* ACL result not cached. Let's scan all the expressions
862 * and use the first one to match.
863 */
864 acl_res = ACL_PAT_FAIL;
865 list_for_each_entry(expr, &acl->expr, list) {
Willy Tarreaud41f8d82007-06-10 10:06:18 +0200866 /* we need to reset context and flags */
867 memset(&test, 0, sizeof(test));
Willy Tarreaua84d3742007-05-07 00:36:48 +0200868 fetch_next:
Willy Tarreau97be1452007-06-10 11:47:14 +0200869 if (!expr->kw->fetch(px, l4, l7, dir, expr, &test))
Willy Tarreaua84d3742007-05-07 00:36:48 +0200870 continue;
871
872 /* apply all tests to this value */
873 list_for_each_entry(pattern, &expr->patterns, list) {
Willy Tarreau11382812008-07-09 16:18:21 +0200874 acl_res |= expr->kw->match(&test, pattern);
875 if (acl_res == ACL_PAT_PASS)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200876 break;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200877 }
878 /*
Willy Tarreau11382812008-07-09 16:18:21 +0200879 * OK now acl_res holds the result of this expression
880 * as one of ACL_PAT_FAIL, ACL_PAT_MISS or ACL_PAT_PASS.
Willy Tarreaua84d3742007-05-07 00:36:48 +0200881 *
Willy Tarreau11382812008-07-09 16:18:21 +0200882 * Then if (!MISS) we can cache the result, and put
Willy Tarreaua84d3742007-05-07 00:36:48 +0200883 * (test.flags & ACL_TEST_F_VOLATILE) in the cache flags.
884 *
885 * FIXME: implement cache.
886 *
887 */
888
889 /* now we may have some cleanup to do */
890 if (test.flags & ACL_TEST_F_MUST_FREE) {
891 free(test.ptr);
892 test.len = 0;
893 }
894
Willy Tarreau11382812008-07-09 16:18:21 +0200895 /* we're ORing these terms, so a single PASS is enough */
896 if (acl_res == ACL_PAT_PASS)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200897 break;
898
Willy Tarreaua84d3742007-05-07 00:36:48 +0200899 if (test.flags & ACL_TEST_F_FETCH_MORE)
900 goto fetch_next;
901 }
902 /*
903 * Here we have the result of an ACL (cached or not).
904 * ACLs are combined, negated or not, to form conditions.
905 */
906
Willy Tarreaua84d3742007-05-07 00:36:48 +0200907 if (term->neg)
Willy Tarreau11382812008-07-09 16:18:21 +0200908 acl_res = acl_neg(acl_res);
Willy Tarreaua84d3742007-05-07 00:36:48 +0200909
910 suite_res &= acl_res;
Willy Tarreau11382812008-07-09 16:18:21 +0200911
912 /* we're ANDing these terms, so a single FAIL is enough */
913 if (suite_res == ACL_PAT_FAIL)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200914 break;
915 }
916 cond_res |= suite_res;
Willy Tarreau11382812008-07-09 16:18:21 +0200917
918 /* we're ORing these terms, so a single PASS is enough */
919 if (cond_res == ACL_PAT_PASS)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200920 break;
921 }
Willy Tarreau11382812008-07-09 16:18:21 +0200922 return cond_res;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200923}
924
925
926/************************************************************************/
927/* All supported keywords must be declared here. */
928/************************************************************************/
929
930/* Note: must not be declared <const> as its list will be overwritten */
931static struct acl_kw_list acl_kws = {{ },{
Willy Tarreaua5909832007-06-17 20:40:25 +0200932 { "always_true", acl_parse_nothing, acl_fetch_nothing, acl_match_true },
933 { "always_false", acl_parse_nothing, acl_fetch_nothing, acl_match_false },
Willy Tarreaua84d3742007-05-07 00:36:48 +0200934#if 0
935 { "time", acl_parse_time, acl_fetch_time, acl_match_time },
936#endif
937 { NULL, NULL, NULL, NULL }
938}};
939
940
941__attribute__((constructor))
942static void __acl_init(void)
943{
944 acl_register_keywords(&acl_kws);
945}
946
947
948/*
949 * Local variables:
950 * c-indent-level: 8
951 * c-basic-offset: 8
952 * End:
953 */