blob: bdf0b081061bb2619234ef6929f69d93ca799687 [file] [log] [blame]
Willy Tarreaua84d3742007-05-07 00:36:48 +02001/*
2 * ACL management functions.
3 *
4 * Copyright 2000-2007 Willy Tarreau <w@1wt.eu>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
Willy 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
33/* This one always returns 1 because its only purpose is to check that the
34 * value is present, which is already checked by getval().
35 */
36int acl_match_pst(struct acl_test *test, struct acl_pattern *pattern)
37{
38 return 1;
39}
40
41/* NB: For two strings to be identical, it is required that their lengths match */
42int acl_match_str(struct acl_test *test, struct acl_pattern *pattern)
43{
Willy Tarreauc8d7c962007-06-17 08:20:33 +020044 int icase;
45
Willy Tarreaua84d3742007-05-07 00:36:48 +020046 if (pattern->len != test->len)
47 return 0;
Willy Tarreauc8d7c962007-06-17 08:20:33 +020048
49 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
50 if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) == 0) ||
51 (!icase && strncmp(pattern->ptr.str, test->ptr, test->len) == 0))
Willy Tarreaua84d3742007-05-07 00:36:48 +020052 return 1;
53 return 0;
54}
55
Willy Tarreauf3d25982007-05-08 22:45:09 +020056/* Executes a regex. It needs to change the data. If it is marked READ_ONLY
57 * then it will be allocated and duplicated in place so that others may use
58 * it later on. Note that this is embarrassing because we always try to avoid
59 * allocating memory at run time.
60 */
61int acl_match_reg(struct acl_test *test, struct acl_pattern *pattern)
62{
63 char old_char;
64 int ret;
65
66 if (unlikely(test->flags & ACL_TEST_F_READ_ONLY)) {
67 char *new_str;
68
69 new_str = calloc(1, test->len + 1);
70 if (!new_str)
71 return 0;
72
73 memcpy(new_str, test->ptr, test->len);
74 new_str[test->len] = 0;
75 if (test->flags & ACL_TEST_F_MUST_FREE)
76 free(test->ptr);
77 test->ptr = new_str;
78 test->flags |= ACL_TEST_F_MUST_FREE;
79 test->flags &= ~ACL_TEST_F_READ_ONLY;
80 }
81
82 old_char = test->ptr[test->len];
83 test->ptr[test->len] = 0;
84
85 if (regexec(pattern->ptr.reg, test->ptr, 0, NULL, 0) == 0)
86 ret = 1;
87 else
88 ret = 0;
89
90 test->ptr[test->len] = old_char;
91 return ret;
92}
93
Willy Tarreaua84d3742007-05-07 00:36:48 +020094/* Checks that the pattern matches the beginning of the tested string. */
95int acl_match_beg(struct acl_test *test, struct acl_pattern *pattern)
96{
Willy Tarreauc8d7c962007-06-17 08:20:33 +020097 int icase;
98
Willy Tarreaua84d3742007-05-07 00:36:48 +020099 if (pattern->len > test->len)
100 return 0;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200101
102 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
103 if ((icase && strncasecmp(pattern->ptr.str, test->ptr, pattern->len) != 0) ||
104 (!icase && strncmp(pattern->ptr.str, test->ptr, pattern->len) != 0))
Willy Tarreaua84d3742007-05-07 00:36:48 +0200105 return 0;
106 return 1;
107}
108
109/* Checks that the pattern matches the end of the tested string. */
110int acl_match_end(struct acl_test *test, struct acl_pattern *pattern)
111{
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200112 int icase;
113
Willy Tarreaua84d3742007-05-07 00:36:48 +0200114 if (pattern->len > test->len)
115 return 0;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200116 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
117 if ((icase && strncasecmp(pattern->ptr.str, test->ptr + test->len - pattern->len, pattern->len) != 0) ||
118 (!icase && strncmp(pattern->ptr.str, test->ptr + test->len - pattern->len, pattern->len) != 0))
Willy Tarreaua84d3742007-05-07 00:36:48 +0200119 return 0;
120 return 1;
121}
122
123/* Checks that the pattern is included inside the tested string.
124 * NB: Suboptimal, should be rewritten using a Boyer-Moore method.
125 */
126int acl_match_sub(struct acl_test *test, struct acl_pattern *pattern)
127{
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200128 int icase;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200129 char *end;
130 char *c;
131
132 if (pattern->len > test->len)
133 return 0;
134
135 end = test->ptr + test->len - pattern->len;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200136 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
137 if (icase) {
138 for (c = test->ptr; c <= end; c++) {
139 if (tolower(*c) != tolower(*pattern->ptr.str))
140 continue;
141 if (strncasecmp(pattern->ptr.str, c, pattern->len) == 0)
142 return 1;
143 }
144 } else {
145 for (c = test->ptr; c <= end; c++) {
146 if (*c != *pattern->ptr.str)
147 continue;
148 if (strncmp(pattern->ptr.str, c, pattern->len) == 0)
149 return 1;
150 }
Willy Tarreaua84d3742007-05-07 00:36:48 +0200151 }
152 return 0;
153}
154
155/* This one is used by other real functions. It checks that the pattern is
156 * included inside the tested string, but enclosed between the specified
157 * delimitor, or a '/' or a '?' or at the beginning or end of the string.
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200158 * The delimitor is stripped at the beginning or end of the pattern.
Willy Tarreaua84d3742007-05-07 00:36:48 +0200159 */
160static int match_word(struct acl_test *test, struct acl_pattern *pattern, char delim)
161{
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200162 int may_match, icase;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200163 char *c, *end;
164 char *ps;
165 int pl;
166
167 pl = pattern->len;
168 ps = pattern->ptr.str;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200169 while (pl > 0 && (*ps == delim || *ps == '/' || *ps == '?')) {
Willy Tarreaua84d3742007-05-07 00:36:48 +0200170 pl--;
171 ps++;
172 }
173
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200174 while (pl > 0 &&
175 (ps[pl - 1] == delim || ps[pl - 1] == '/' || ps[pl - 1] == '?'))
Willy Tarreaua84d3742007-05-07 00:36:48 +0200176 pl--;
177
178 if (pl > test->len)
179 return 0;
180
181 may_match = 1;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200182 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200183 end = test->ptr + test->len - pl;
184 for (c = test->ptr; c <= end; c++) {
185 if (*c == '/' || *c == delim || *c == '?') {
186 may_match = 1;
187 continue;
188 }
Willy Tarreaua84d3742007-05-07 00:36:48 +0200189
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200190 if (!may_match)
191 continue;
192
193 if (icase) {
194 if ((tolower(*c) == tolower(*ps)) &&
195 (strncasecmp(ps, c, pl) == 0) &&
196 (c == end || c[pl] == '/' || c[pl] == delim || c[pl] == '?'))
197 return 1;
198 } else {
199 if ((*c == *ps) &&
200 (strncmp(ps, c, pl) == 0) &&
201 (c == end || c[pl] == '/' || c[pl] == delim || c[pl] == '?'))
202 return 1;
203 }
Willy Tarreaua84d3742007-05-07 00:36:48 +0200204 may_match = 0;
205 }
206 return 0;
207}
208
209/* Checks that the pattern is included inside the tested string, but enclosed
210 * between slashes or at the beginning or end of the string. Slashes at the
211 * beginning or end of the pattern are ignored.
212 */
213int acl_match_dir(struct acl_test *test, struct acl_pattern *pattern)
214{
215 return match_word(test, pattern, '/');
216}
217
218/* Checks that the pattern is included inside the tested string, but enclosed
219 * between dots or at the beginning or end of the string. Dots at the beginning
220 * or end of the pattern are ignored.
221 */
222int acl_match_dom(struct acl_test *test, struct acl_pattern *pattern)
223{
224 return match_word(test, pattern, '.');
225}
226
227/* Checks that the integer in <test> is included between min and max */
Willy Tarreauae8b7962007-06-09 23:10:04 +0200228int acl_match_int(struct acl_test *test, struct acl_pattern *pattern)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200229{
Willy Tarreauae8b7962007-06-09 23:10:04 +0200230 if ((!pattern->val.range.min_set || pattern->val.range.min <= test->i) &&
231 (!pattern->val.range.max_set || test->i <= pattern->val.range.max))
Willy Tarreaua84d3742007-05-07 00:36:48 +0200232 return 1;
233 return 0;
234}
235
Willy Tarreaua67fad92007-05-08 19:50:09 +0200236int acl_match_ip(struct acl_test *test, struct acl_pattern *pattern)
237{
238 struct in_addr *s;
239
240 if (test->i != AF_INET)
241 return 0;
242
243 s = (void *)test->ptr;
244 if (((s->s_addr ^ pattern->val.ipv4.addr.s_addr) & pattern->val.ipv4.mask.s_addr) == 0)
245 return 1;
246 return 0;
247}
248
Willy Tarreaua84d3742007-05-07 00:36:48 +0200249/* Parse a string. It is allocated and duplicated. */
Willy Tarreauae8b7962007-06-09 23:10:04 +0200250int acl_parse_str(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200251{
252 int len;
253
Willy Tarreauae8b7962007-06-09 23:10:04 +0200254 len = strlen(*text);
255 pattern->ptr.str = strdup(*text);
Willy Tarreaua84d3742007-05-07 00:36:48 +0200256 if (!pattern->ptr.str)
257 return 0;
258 pattern->len = len;
259 return 1;
260}
261
Willy Tarreauf3d25982007-05-08 22:45:09 +0200262/* Parse a regex. It is allocated. */
Willy Tarreauae8b7962007-06-09 23:10:04 +0200263int acl_parse_reg(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreauf3d25982007-05-08 22:45:09 +0200264{
265 regex_t *preg;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200266 int icase;
Willy Tarreauf3d25982007-05-08 22:45:09 +0200267
268 preg = calloc(1, sizeof(regex_t));
269
270 if (!preg)
271 return 0;
272
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200273 icase = (pattern->flags & ACL_PAT_F_IGNORE_CASE) ? REG_ICASE : 0;
274 if (regcomp(preg, *text, REG_EXTENDED | REG_NOSUB | icase) != 0) {
Willy Tarreauf3d25982007-05-08 22:45:09 +0200275 free(preg);
276 return 0;
277 }
278
279 pattern->ptr.reg = preg;
280 return 1;
281}
282
Willy Tarreauae8b7962007-06-09 23:10:04 +0200283/* Parse a range of positive integers delimited by either ':' or '-'. If only
284 * one integer is read, it is set as both min and max. An operator may be
285 * specified as the prefix, among this list of 5 :
286 *
287 * 0:eq, 1:gt, 2:ge, 3:lt, 4:le
288 *
289 * The default operator is "eq". It supports range matching. Ranges are
290 * rejected for other operators. The operator may be changed at any time.
291 * The operator is stored in the 'opaque' argument.
292 *
Willy Tarreaua84d3742007-05-07 00:36:48 +0200293 */
Willy Tarreauae8b7962007-06-09 23:10:04 +0200294int acl_parse_int(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200295{
Willy Tarreauae8b7962007-06-09 23:10:04 +0200296 signed long long i;
297 unsigned int j, last, skip = 0;
298 const char *ptr = *text;
299
300
301 while (!isdigit(*ptr)) {
302 if (strcmp(ptr, "eq") == 0) *opaque = 0;
303 else if (strcmp(ptr, "gt") == 0) *opaque = 1;
304 else if (strcmp(ptr, "ge") == 0) *opaque = 2;
305 else if (strcmp(ptr, "lt") == 0) *opaque = 3;
306 else if (strcmp(ptr, "le") == 0) *opaque = 4;
307 else
308 return 0;
309
310 skip++;
311 ptr = text[skip];
312 }
Willy Tarreaua84d3742007-05-07 00:36:48 +0200313
314 last = i = 0;
315 while (1) {
Willy Tarreauae8b7962007-06-09 23:10:04 +0200316 j = *ptr++;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200317 if ((j == '-' || j == ':') && !last) {
318 last++;
319 pattern->val.range.min = i;
320 i = 0;
321 continue;
322 }
323 j -= '0';
324 if (j > 9)
325 // also catches the terminating zero
326 break;
327 i *= 10;
328 i += j;
329 }
Willy Tarreauae8b7962007-06-09 23:10:04 +0200330
331 if (last && *opaque >= 1 && *opaque <= 4)
332 /* having a range with a min or a max is absurd */
333 return 0;
334
Willy Tarreaua84d3742007-05-07 00:36:48 +0200335 if (!last)
336 pattern->val.range.min = i;
337 pattern->val.range.max = i;
Willy Tarreauae8b7962007-06-09 23:10:04 +0200338
339 switch (*opaque) {
340 case 0: /* eq */
341 pattern->val.range.min_set = 1;
342 pattern->val.range.max_set = 1;
343 break;
344 case 1: /* gt */
345 pattern->val.range.min++; /* gt = ge + 1 */
346 case 2: /* ge */
347 pattern->val.range.min_set = 1;
348 pattern->val.range.max_set = 0;
349 break;
350 case 3: /* lt */
351 pattern->val.range.max--; /* lt = le - 1 */
352 case 4: /* le */
353 pattern->val.range.min_set = 0;
354 pattern->val.range.max_set = 1;
355 break;
356 }
357 return skip + 1;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200358}
359
Willy Tarreaua67fad92007-05-08 19:50:09 +0200360/* Parse an IP address and an optional mask in the form addr[/mask].
361 * The addr may either be an IPv4 address or a hostname. The mask
362 * may either be a dotted mask or a number of bits. Returns 1 if OK,
363 * otherwise 0.
364 */
Willy Tarreauae8b7962007-06-09 23:10:04 +0200365int acl_parse_ip(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreaua67fad92007-05-08 19:50:09 +0200366{
Willy Tarreauae8b7962007-06-09 23:10:04 +0200367 if (str2net(*text, &pattern->val.ipv4.addr, &pattern->val.ipv4.mask))
368 return 1;
369 else
370 return 0;
Willy Tarreaua67fad92007-05-08 19:50:09 +0200371}
372
Willy Tarreaua84d3742007-05-07 00:36:48 +0200373/*
374 * Registers the ACL keyword list <kwl> as a list of valid keywords for next
375 * parsing sessions.
376 */
377void acl_register_keywords(struct acl_kw_list *kwl)
378{
379 LIST_ADDQ(&acl_keywords.list, &kwl->list);
380}
381
382/*
383 * Unregisters the ACL keyword list <kwl> from the list of valid keywords.
384 */
385void acl_unregister_keywords(struct acl_kw_list *kwl)
386{
387 LIST_DEL(&kwl->list);
388 LIST_INIT(&kwl->list);
389}
390
391/* Return a pointer to the ACL <name> within the list starting at <head>, or
392 * NULL if not found.
393 */
394struct acl *find_acl_by_name(const char *name, struct list *head)
395{
396 struct acl *acl;
397 list_for_each_entry(acl, head, list) {
398 if (strcmp(acl->name, name) == 0)
399 return acl;
400 }
401 return NULL;
402}
403
404/* Return a pointer to the ACL keyword <kw>, or NULL if not found. Note that if
405 * <kw> contains an opening parenthesis, only the left part of it is checked.
406 */
407struct acl_keyword *find_acl_kw(const char *kw)
408{
409 int index;
410 const char *kwend;
411 struct acl_kw_list *kwl;
412
413 kwend = strchr(kw, '(');
414 if (!kwend)
415 kwend = kw + strlen(kw);
416
417 list_for_each_entry(kwl, &acl_keywords.list, list) {
418 for (index = 0; kwl->kw[index].kw != NULL; index++) {
419 if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) &&
420 kwl->kw[index].kw[kwend-kw] == 0)
421 return &kwl->kw[index];
422 }
423 }
424 return NULL;
425}
426
427static void free_pattern(struct acl_pattern *pat)
428{
429 if (pat->ptr.ptr)
430 free(pat->ptr.ptr);
431 free(pat);
432}
433
434static void free_pattern_list(struct list *head)
435{
436 struct acl_pattern *pat, *tmp;
437 list_for_each_entry_safe(pat, tmp, head, list)
438 free_pattern(pat);
439}
440
441static struct acl_expr *prune_acl_expr(struct acl_expr *expr)
442{
443 free_pattern_list(&expr->patterns);
444 LIST_INIT(&expr->patterns);
445 if (expr->arg.str)
446 free(expr->arg.str);
447 expr->kw->use_cnt--;
448 return expr;
449}
450
451/* Parse an ACL expression starting at <args>[0], and return it.
452 * Right now, the only accepted syntax is :
453 * <subject> [<value>...]
454 */
455struct acl_expr *parse_acl_expr(const char **args)
456{
457 __label__ out_return, out_free_expr, out_free_pattern;
458 struct acl_expr *expr;
459 struct acl_keyword *aclkw;
460 struct acl_pattern *pattern;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200461 int opaque, patflags;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200462 const char *arg;
463
464 aclkw = find_acl_kw(args[0]);
465 if (!aclkw || !aclkw->parse)
466 goto out_return;
467
468 expr = (struct acl_expr *)calloc(1, sizeof(*expr));
469 if (!expr)
470 goto out_return;
471
472 expr->kw = aclkw;
473 aclkw->use_cnt++;
474 LIST_INIT(&expr->patterns);
475 expr->arg.str = NULL;
Willy Tarreaubb768912007-06-10 11:17:01 +0200476 expr->arg_len = 0;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200477
478 arg = strchr(args[0], '(');
479 if (arg != NULL) {
480 char *end, *arg2;
481 /* there is an argument in the form "subject(arg)" */
482 arg++;
483 end = strchr(arg, ')');
484 if (!end)
485 goto out_free_expr;
486 arg2 = (char *)calloc(1, end - arg + 1);
487 if (!arg2)
488 goto out_free_expr;
489 memcpy(arg2, arg, end - arg);
490 arg2[end-arg] = '\0';
Willy Tarreaubb768912007-06-10 11:17:01 +0200491 expr->arg_len = end - arg;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200492 expr->arg.str = arg2;
493 }
494
Willy Tarreaua84d3742007-05-07 00:36:48 +0200495 args++;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200496
497 /* check for options before patterns. Supported options are :
498 * -i : ignore case for all patterns by default
499 * -f : read patterns from those files
500 * -- : everything after this is not an option
501 */
502 patflags = 0;
503 while (**args == '-') {
504 if ((*args)[1] == 'i')
505 patflags |= ACL_PAT_F_IGNORE_CASE;
506 else if ((*args)[1] == 'f')
507 patflags |= ACL_PAT_F_FROM_FILE;
508 else if ((*args)[1] == '-') {
509 args++;
510 break;
511 }
512 else
513 break;
514 args++;
515 }
516
517 /* now parse all patterns */
Willy Tarreauae8b7962007-06-09 23:10:04 +0200518 opaque = 0;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200519 while (**args) {
Willy Tarreauae8b7962007-06-09 23:10:04 +0200520 int ret;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200521 pattern = (struct acl_pattern *)calloc(1, sizeof(*pattern));
522 if (!pattern)
523 goto out_free_expr;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200524 pattern->flags = patflags;
525
Willy Tarreauae8b7962007-06-09 23:10:04 +0200526 ret = aclkw->parse(args, pattern, &opaque);
527 if (!ret)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200528 goto out_free_pattern;
529 LIST_ADDQ(&expr->patterns, &pattern->list);
Willy Tarreauae8b7962007-06-09 23:10:04 +0200530 args += ret;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200531 }
532
533 return expr;
534
535 out_free_pattern:
536 free_pattern(pattern);
537 out_free_expr:
538 prune_acl_expr(expr);
539 free(expr);
540 out_return:
541 return NULL;
542}
543
544/* Parse an ACL with the name starting at <args>[0], and with a list of already
545 * known ACLs in <acl>. If the ACL was not in the list, it will be added.
546 * A pointer to that ACL is returned.
547 *
548 * args syntax: <aclname> <acl_expr>
549 */
550struct acl *parse_acl(const char **args, struct list *known_acl)
551{
552 __label__ out_return, out_free_acl_expr, out_free_name;
553 struct acl *cur_acl;
554 struct acl_expr *acl_expr;
555 char *name;
556
557 acl_expr = parse_acl_expr(args + 1);
558 if (!acl_expr)
559 goto out_return;
560
561 cur_acl = find_acl_by_name(args[0], known_acl);
562 if (!cur_acl) {
563 name = strdup(args[0]);
564 if (!name)
565 goto out_free_acl_expr;
566 cur_acl = (struct acl *)calloc(1, sizeof(*cur_acl));
567 if (cur_acl == NULL)
568 goto out_free_name;
569
570 LIST_INIT(&cur_acl->expr);
571 LIST_ADDQ(known_acl, &cur_acl->list);
572 cur_acl->name = name;
573 }
574
575 LIST_ADDQ(&cur_acl->expr, &acl_expr->list);
576 return cur_acl;
577
578 out_free_name:
579 free(name);
580 out_free_acl_expr:
581 prune_acl_expr(acl_expr);
582 free(acl_expr);
583 out_return:
584 return NULL;
585}
586
Willy Tarreau16fbe822007-06-17 11:54:31 +0200587/* Some useful ACLs provided by default. Only those used are allocated. */
588
589const struct {
590 const char *name;
591 const char *expr[4]; /* put enough for longest expression */
592} default_acl_list[] = {
593 { .name = "LOCALHOST", .expr = {"src","127.0.0.1/8",""}},
594 { .name = "HTTP_1.0", .expr = {"req_ver","1.0",""}},
595 { .name = "HTTP_1.1", .expr = {"req_ver","1.1",""}},
596 { .name = "METH_CONNECT", .expr = {"method","CONNECT",""}},
597 { .name = "METH_GET", .expr = {"method","GET","HEAD",""}},
598 { .name = "METH_HEAD", .expr = {"method","HEAD",""}},
599 { .name = "METH_OPTIONS", .expr = {"method","OPTIONS",""}},
600 { .name = "METH_POST", .expr = {"method","POST",""}},
601 { .name = "METH_TRACE", .expr = {"method","TRACE",""}},
602 { .name = "HTTP_URL_ABS", .expr = {"url_reg","^[^/:]*://",""}},
603 { .name = "HTTP_URL_SLASH", .expr = {"url_beg","/",""}},
604 { .name = "HTTP_URL_STAR", .expr = {"url","*",""}},
605 { .name = "HTTP_CONTENT", .expr = {"hdr_val(content-length)","gt","0",""}},
606 { .name = NULL, .expr = {""}}
607};
608
609/* Find a default ACL from the default_acl list, compile it and return it.
610 * If the ACL is not found, NULL is returned. In theory, it cannot fail,
611 * except when default ACLs are broken, in which case it will return NULL.
612 * If <known_acl> is not NULL, the ACL will be queued at its tail.
613 */
614struct acl *find_acl_default(const char *acl_name, struct list *known_acl)
615{
616 __label__ out_return, out_free_acl_expr, out_free_name;
617 struct acl *cur_acl;
618 struct acl_expr *acl_expr;
619 char *name;
620 int index;
621
622 for (index = 0; default_acl_list[index].name != NULL; index++) {
623 if (strcmp(acl_name, default_acl_list[index].name) == 0)
624 break;
625 }
626
627 if (default_acl_list[index].name == NULL)
628 return NULL;
629
630 acl_expr = parse_acl_expr((const char **)default_acl_list[index].expr);
631 if (!acl_expr)
632 goto out_return;
633
634 name = strdup(acl_name);
635 if (!name)
636 goto out_free_acl_expr;
637 cur_acl = (struct acl *)calloc(1, sizeof(*cur_acl));
638 if (cur_acl == NULL)
639 goto out_free_name;
640
641 cur_acl->name = name;
642 LIST_INIT(&cur_acl->expr);
643 LIST_ADDQ(&cur_acl->expr, &acl_expr->list);
644 if (known_acl)
645 LIST_ADDQ(known_acl, &cur_acl->list);
646
647 return cur_acl;
648
649 out_free_name:
650 free(name);
651 out_free_acl_expr:
652 prune_acl_expr(acl_expr);
653 free(acl_expr);
654 out_return:
655 return NULL;
656}
Willy Tarreaua84d3742007-05-07 00:36:48 +0200657
658/* Purge everything in the acl_cond <cond>, then return <cond>. */
659struct acl_cond *prune_acl_cond(struct acl_cond *cond)
660{
661 struct acl_term_suite *suite, *tmp_suite;
662 struct acl_term *term, *tmp_term;
663
664 /* iterate through all term suites and free all terms and all suites */
665 list_for_each_entry_safe(suite, tmp_suite, &cond->suites, list) {
666 list_for_each_entry_safe(term, tmp_term, &suite->terms, list)
667 free(term);
668 free(suite);
669 }
670 return cond;
671}
672
673/* Parse an ACL condition starting at <args>[0], relying on a list of already
674 * known ACLs passed in <known_acl>. The new condition is returned (or NULL in
675 * case of low memory). Supports multiple conditions separated by "or".
676 */
677struct acl_cond *parse_acl_cond(const char **args, struct list *known_acl, int pol)
678{
679 __label__ out_return, out_free_suite, out_free_term;
Willy Tarreau74b98a82007-06-16 19:35:18 +0200680 int arg, neg;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200681 const char *word;
682 struct acl *cur_acl;
683 struct acl_term *cur_term;
684 struct acl_term_suite *cur_suite;
685 struct acl_cond *cond;
686
687 cond = (struct acl_cond *)calloc(1, sizeof(*cond));
688 if (cond == NULL)
689 goto out_return;
690
691 LIST_INIT(&cond->list);
692 LIST_INIT(&cond->suites);
693 cond->pol = pol;
694
695 cur_suite = NULL;
Willy Tarreau74b98a82007-06-16 19:35:18 +0200696 neg = 0;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200697 for (arg = 0; *args[arg]; arg++) {
698 word = args[arg];
699
700 /* remove as many exclamation marks as we can */
701 while (*word == '!') {
702 neg = !neg;
703 word++;
704 }
705
706 /* an empty word is allowed because we cannot force the user to
707 * always think about not leaving exclamation marks alone.
708 */
709 if (!*word)
710 continue;
711
Willy Tarreau16fbe822007-06-17 11:54:31 +0200712 if (strcasecmp(word, "or") == 0 || strcmp(word, "||") == 0) {
Willy Tarreaua84d3742007-05-07 00:36:48 +0200713 /* new term suite */
714 cur_suite = NULL;
715 neg = 0;
716 continue;
717 }
718
Willy Tarreau16fbe822007-06-17 11:54:31 +0200719 /* search for <word> in the known ACL names. If we do not find
720 * it, let's look for it in the default ACLs, and if found, add
721 * it to the list of ACLs of this proxy. This makes it possible
722 * to override them.
723 */
Willy Tarreaua84d3742007-05-07 00:36:48 +0200724 cur_acl = find_acl_by_name(word, known_acl);
Willy Tarreau16fbe822007-06-17 11:54:31 +0200725 if (cur_acl == NULL) {
726 cur_acl = find_acl_default(word, known_acl);
727 if (cur_acl == NULL)
728 goto out_free_suite;
729 }
Willy Tarreaua84d3742007-05-07 00:36:48 +0200730
731 cur_term = (struct acl_term *)calloc(1, sizeof(*cur_term));
732 if (cur_term == NULL)
733 goto out_free_suite;
734
735 cur_term->acl = cur_acl;
736 cur_term->neg = neg;
737
738 if (!cur_suite) {
739 cur_suite = (struct acl_term_suite *)calloc(1, sizeof(*cur_suite));
740 if (cur_term == NULL)
741 goto out_free_term;
742 LIST_INIT(&cur_suite->terms);
743 LIST_ADDQ(&cond->suites, &cur_suite->list);
744 }
745 LIST_ADDQ(&cur_suite->terms, &cur_term->list);
Willy Tarreau74b98a82007-06-16 19:35:18 +0200746 neg = 0;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200747 }
748
749 return cond;
750
751 out_free_term:
752 free(cur_term);
753 out_free_suite:
754 prune_acl_cond(cond);
755 free(cond);
756 out_return:
757 return NULL;
758}
759
760/* Execute condition <cond> and return 0 if test fails or 1 if test succeeds.
761 * This function only computes the condition, it does not apply the polarity
762 * required by IF/UNLESS, it's up to the caller to do this.
763 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +0200764int acl_exec_cond(struct acl_cond *cond, struct proxy *px, struct session *l4, void *l7, int dir)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200765{
766 __label__ fetch_next;
767 struct acl_term_suite *suite;
768 struct acl_term *term;
769 struct acl_expr *expr;
770 struct acl *acl;
771 struct acl_pattern *pattern;
772 struct acl_test test;
773 int acl_res, pat_res, suite_res, cond_res;
774
775 /* we're doing a logical OR between conditions so we initialize to FAIL */
776 cond_res = ACL_PAT_FAIL;
777 list_for_each_entry(suite, &cond->suites, list) {
778 /* evaluate condition suite <suite>. We stop at the first term
779 * which does not return ACL_PAT_PASS.
780 */
781
782 /* we're doing a logical AND between terms, so we must set the
783 * initial value to PASS.
784 */
785 suite_res = ACL_PAT_PASS;
786 list_for_each_entry(term, &suite->terms, list) {
787 acl = term->acl;
788
789 /* FIXME: use cache !
790 * check acl->cache_idx for this.
791 */
792
793 /* ACL result not cached. Let's scan all the expressions
794 * and use the first one to match.
795 */
796 acl_res = ACL_PAT_FAIL;
797 list_for_each_entry(expr, &acl->expr, list) {
Willy Tarreaud41f8d82007-06-10 10:06:18 +0200798 /* we need to reset context and flags */
799 memset(&test, 0, sizeof(test));
Willy Tarreaua84d3742007-05-07 00:36:48 +0200800 fetch_next:
Willy Tarreau97be1452007-06-10 11:47:14 +0200801 if (!expr->kw->fetch(px, l4, l7, dir, expr, &test))
Willy Tarreaua84d3742007-05-07 00:36:48 +0200802 continue;
803
804 /* apply all tests to this value */
805 list_for_each_entry(pattern, &expr->patterns, list) {
806 pat_res = expr->kw->match(&test, pattern);
807
808 if (pat_res & ACL_PAT_MISS) {
809 /* there is at least one test which might be worth retrying later. */
810 acl_res |= ACL_PAT_MISS;
811 continue;
812 } else if (pat_res & ACL_PAT_PASS) {
813 /* we found one ! */
814 acl_res |= ACL_PAT_PASS;
815 break;
816 }
817 }
818 /*
Willy Tarreau74b98a82007-06-16 19:35:18 +0200819 * OK now we have the result of this expression in acl_res.
Willy Tarreaua84d3742007-05-07 00:36:48 +0200820 * - we have the PASS bit set if at least one pattern matched ;
821 * - we have the MISS bit set if at least one pattern may match
822 * later so that we should not cache a failure ;
823 *
824 * Then if (PASS || !MISS) we can cache the result, and put
825 * (test.flags & ACL_TEST_F_VOLATILE) in the cache flags.
826 *
827 * FIXME: implement cache.
828 *
829 */
830
831 /* now we may have some cleanup to do */
832 if (test.flags & ACL_TEST_F_MUST_FREE) {
833 free(test.ptr);
834 test.len = 0;
835 }
836
837 if (acl_res & ACL_PAT_PASS)
838 break;
839
840 /* prepare to test another expression */
841 acl_res = ACL_PAT_FAIL;
842
843 if (test.flags & ACL_TEST_F_FETCH_MORE)
844 goto fetch_next;
845 }
846 /*
847 * Here we have the result of an ACL (cached or not).
848 * ACLs are combined, negated or not, to form conditions.
849 */
850
851 acl_res &= ACL_PAT_PASS;
852 if (term->neg)
853 acl_res ^= ACL_PAT_PASS;
854
855 suite_res &= acl_res;
856 if (!(suite_res & ACL_PAT_PASS))
857 break;
858 }
859 cond_res |= suite_res;
860 if (cond_res & ACL_PAT_PASS)
861 break;
862 }
863
864 return (cond_res & ACL_PAT_PASS) ? 1 : 0;
865}
866
867
868/************************************************************************/
869/* All supported keywords must be declared here. */
870/************************************************************************/
871
872/* Note: must not be declared <const> as its list will be overwritten */
873static struct acl_kw_list acl_kws = {{ },{
874#if 0
875 { "time", acl_parse_time, acl_fetch_time, acl_match_time },
876#endif
877 { NULL, NULL, NULL, NULL }
878}};
879
880
881__attribute__((constructor))
882static void __acl_init(void)
883{
884 acl_register_keywords(&acl_kws);
885}
886
887
888/*
889 * Local variables:
890 * c-indent-level: 8
891 * c-basic-offset: 8
892 * End:
893 */