blob: 9f487a309b55e2808184b950c37cbe3f21cad82c [file] [log] [blame]
Willy Tarreaua84d3742007-05-07 00:36:48 +02001/*
2 * ACL management functions.
3 *
Willy Tarreaud4c33c82013-01-07 21:59:07 +01004 * Copyright 2000-2013 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
Willy Tarreau2b5285d2010-05-09 23:45:24 +020022#include <types/global.h>
23
Willy Tarreaua84d3742007-05-07 00:36:48 +020024#include <proto/acl.h>
Willy Tarreau34db1082012-04-19 17:16:54 +020025#include <proto/arg.h>
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010026#include <proto/auth.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020027#include <proto/channel.h>
Willy Tarreau404e8ab2009-07-26 19:40:40 +020028#include <proto/log.h>
Willy Tarreau0b1cd942010-05-16 22:18:27 +020029#include <proto/proxy.h>
Willy Tarreau8ed669b2013-01-11 15:49:37 +010030#include <proto/sample.h>
Willy Tarreaud28c3532012-04-19 19:28:33 +020031#include <proto/stick_table.h>
Willy Tarreaua84d3742007-05-07 00:36:48 +020032
Willy Tarreauc4262962010-05-10 23:42:40 +020033#include <ebsttree.h>
34
Willy Tarreaua84d3742007-05-07 00:36:48 +020035/* List head of all known ACL keywords */
36static struct acl_kw_list acl_keywords = {
37 .list = LIST_HEAD_INIT(acl_keywords.list)
38};
39
Willy Tarreau5adeda12013-03-31 22:13:34 +020040static char *acl_match_names[ACL_MATCH_NUM] = {
41 [ACL_MATCH_FOUND] = "found",
42 [ACL_MATCH_BOOL] = "bool",
43 [ACL_MATCH_INT] = "int",
44 [ACL_MATCH_IP] = "ip",
45 [ACL_MATCH_BIN] = "bin",
46 [ACL_MATCH_LEN] = "len",
47 [ACL_MATCH_STR] = "str",
48 [ACL_MATCH_BEG] = "beg",
49 [ACL_MATCH_SUB] = "sub",
50 [ACL_MATCH_DIR] = "dir",
51 [ACL_MATCH_DOM] = "dom",
52 [ACL_MATCH_END] = "end",
53 [ACL_MATCH_REG] = "reg",
54};
55
56static int (*acl_parse_fcts[ACL_MATCH_NUM])(const char **, struct acl_pattern *, int *, char **) = {
57 [ACL_MATCH_FOUND] = acl_parse_nothing,
58 [ACL_MATCH_BOOL] = acl_parse_nothing,
59 [ACL_MATCH_INT] = acl_parse_int,
60 [ACL_MATCH_IP] = acl_parse_ip,
61 [ACL_MATCH_BIN] = acl_parse_bin,
62 [ACL_MATCH_LEN] = acl_parse_int,
63 [ACL_MATCH_STR] = acl_parse_str,
64 [ACL_MATCH_BEG] = acl_parse_str,
65 [ACL_MATCH_SUB] = acl_parse_str,
66 [ACL_MATCH_DIR] = acl_parse_str,
67 [ACL_MATCH_DOM] = acl_parse_str,
68 [ACL_MATCH_END] = acl_parse_str,
69 [ACL_MATCH_REG] = acl_parse_reg,
70};
71
72static int (*acl_match_fcts[ACL_MATCH_NUM])(struct sample *, struct acl_pattern *) = {
73 [ACL_MATCH_FOUND] = NULL,
74 [ACL_MATCH_BOOL] = acl_match_nothing,
75 [ACL_MATCH_INT] = acl_match_int,
76 [ACL_MATCH_IP] = acl_match_ip,
77 [ACL_MATCH_BIN] = acl_match_bin,
78 [ACL_MATCH_LEN] = acl_match_len,
79 [ACL_MATCH_STR] = acl_match_str,
80 [ACL_MATCH_BEG] = acl_match_beg,
81 [ACL_MATCH_SUB] = acl_match_sub,
82 [ACL_MATCH_DIR] = acl_match_dir,
83 [ACL_MATCH_DOM] = acl_match_dom,
84 [ACL_MATCH_END] = acl_match_end,
85 [ACL_MATCH_REG] = acl_match_reg,
86};
87
88/* return the ACL_MATCH_* index for match name "name", or < 0 if not found */
89static int acl_find_match_name(const char *name)
90{
91 int i;
92
93 for (i = 0; i < ACL_MATCH_NUM; i++)
94 if (strcmp(name, acl_match_names[i]) == 0)
95 return i;
96 return -1;
97}
Willy Tarreaua84d3742007-05-07 00:36:48 +020098
Willy Tarreaua5909832007-06-17 20:40:25 +020099/*
Willy Tarreau58393e12008-07-20 10:39:22 +0200100 * These functions are exported and may be used by any other component.
101 */
102
103/* ignore the current line */
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200104int acl_parse_nothing(const char **text, struct acl_pattern *pattern, int *opaque, char **err)
Willy Tarreaua5909832007-06-17 20:40:25 +0200105{
Willy Tarreau58393e12008-07-20 10:39:22 +0200106 return 1;
107}
108
Willy Tarreaua5909832007-06-17 20:40:25 +0200109/* always return false */
Willy Tarreau37406352012-04-23 16:16:37 +0200110int acl_match_nothing(struct sample *smp, 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 */
Willy Tarreau37406352012-04-23 16:16:37 +0200117int acl_match_str(struct sample *smp, struct acl_pattern *pattern)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200118{
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200119 int icase;
120
Willy Tarreauf853c462012-04-23 18:53:56 +0200121 if (pattern->len != smp->data.str.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;
Willy Tarreauf853c462012-04-23 18:53:56 +0200125 if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) == 0) ||
126 (!icase && strncmp(pattern->ptr.str, smp->data.str.str, smp->data.str.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
Emeric Brun07ca4962012-10-17 13:38:19 +0200131/* NB: For two binaries buf to be identical, it is required that their lengths match */
132int acl_match_bin(struct sample *smp, struct acl_pattern *pattern)
133{
134 if (pattern->len != smp->data.str.len)
135 return ACL_PAT_FAIL;
136
137 if (memcmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) == 0)
138 return ACL_PAT_PASS;
139 return ACL_PAT_FAIL;
140}
141
Willy Tarreauc4262962010-05-10 23:42:40 +0200142/* Lookup a string in the expression's pattern tree. The node is returned if it
143 * exists, otherwise NULL.
144 */
Willy Tarreau37406352012-04-23 16:16:37 +0200145static void *acl_lookup_str(struct sample *smp, struct acl_expr *expr)
Willy Tarreauc4262962010-05-10 23:42:40 +0200146{
147 /* data are stored in a tree */
148 struct ebmb_node *node;
149 char prev;
150
151 /* we may have to force a trailing zero on the test pattern */
Willy Tarreauf853c462012-04-23 18:53:56 +0200152 prev = smp->data.str.str[smp->data.str.len];
Willy Tarreauc4262962010-05-10 23:42:40 +0200153 if (prev)
Willy Tarreauf853c462012-04-23 18:53:56 +0200154 smp->data.str.str[smp->data.str.len] = '\0';
155 node = ebst_lookup(&expr->pattern_tree, smp->data.str.str);
Willy Tarreauc4262962010-05-10 23:42:40 +0200156 if (prev)
Willy Tarreauf853c462012-04-23 18:53:56 +0200157 smp->data.str.str[smp->data.str.len] = prev;
Willy Tarreauc4262962010-05-10 23:42:40 +0200158 return node;
159}
160
Willy Tarreau21e5b0e2012-04-23 19:25:44 +0200161/* Executes a regex. It temporarily changes the data to add a trailing zero,
162 * and restores the previous character when leaving.
Willy Tarreauf3d25982007-05-08 22:45:09 +0200163 */
Willy Tarreau37406352012-04-23 16:16:37 +0200164int acl_match_reg(struct sample *smp, struct acl_pattern *pattern)
Willy Tarreauf3d25982007-05-08 22:45:09 +0200165{
Hiroaki Nakamura70351322013-01-13 15:00:42 +0900166 if (regex_exec(pattern->ptr.reg, smp->data.str.str, smp->data.str.len) == 0)
Thierry FOURNIERef37a662013-10-15 13:41:44 +0200167 return ACL_PAT_PASS;
168 return ACL_PAT_FAIL;
Willy Tarreauf3d25982007-05-08 22:45:09 +0200169}
170
Willy Tarreaua84d3742007-05-07 00:36:48 +0200171/* Checks that the pattern matches the beginning of the tested string. */
Willy Tarreau37406352012-04-23 16:16:37 +0200172int acl_match_beg(struct sample *smp, struct acl_pattern *pattern)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200173{
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200174 int icase;
175
Willy Tarreauf853c462012-04-23 18:53:56 +0200176 if (pattern->len > smp->data.str.len)
Willy Tarreau11382812008-07-09 16:18:21 +0200177 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200178
179 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
Willy Tarreauf853c462012-04-23 18:53:56 +0200180 if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str, pattern->len) != 0) ||
181 (!icase && strncmp(pattern->ptr.str, smp->data.str.str, pattern->len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +0200182 return ACL_PAT_FAIL;
183 return ACL_PAT_PASS;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200184}
185
186/* Checks that the pattern matches the end of the tested string. */
Willy Tarreau37406352012-04-23 16:16:37 +0200187int acl_match_end(struct sample *smp, struct acl_pattern *pattern)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200188{
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200189 int icase;
190
Willy Tarreauf853c462012-04-23 18:53:56 +0200191 if (pattern->len > smp->data.str.len)
Willy Tarreau11382812008-07-09 16:18:21 +0200192 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200193 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
Willy Tarreauf853c462012-04-23 18:53:56 +0200194 if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str + smp->data.str.len - pattern->len, pattern->len) != 0) ||
195 (!icase && strncmp(pattern->ptr.str, smp->data.str.str + smp->data.str.len - pattern->len, pattern->len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +0200196 return ACL_PAT_FAIL;
197 return ACL_PAT_PASS;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200198}
199
200/* Checks that the pattern is included inside the tested string.
201 * NB: Suboptimal, should be rewritten using a Boyer-Moore method.
202 */
Willy Tarreau37406352012-04-23 16:16:37 +0200203int acl_match_sub(struct sample *smp, struct acl_pattern *pattern)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200204{
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200205 int icase;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200206 char *end;
207 char *c;
208
Willy Tarreauf853c462012-04-23 18:53:56 +0200209 if (pattern->len > smp->data.str.len)
Willy Tarreau11382812008-07-09 16:18:21 +0200210 return ACL_PAT_FAIL;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200211
Willy Tarreauf853c462012-04-23 18:53:56 +0200212 end = smp->data.str.str + smp->data.str.len - pattern->len;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200213 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
214 if (icase) {
Willy Tarreauf853c462012-04-23 18:53:56 +0200215 for (c = smp->data.str.str; c <= end; c++) {
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200216 if (tolower(*c) != tolower(*pattern->ptr.str))
217 continue;
218 if (strncasecmp(pattern->ptr.str, c, pattern->len) == 0)
Willy Tarreau11382812008-07-09 16:18:21 +0200219 return ACL_PAT_PASS;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200220 }
221 } else {
Willy Tarreauf853c462012-04-23 18:53:56 +0200222 for (c = smp->data.str.str; c <= end; c++) {
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200223 if (*c != *pattern->ptr.str)
224 continue;
225 if (strncmp(pattern->ptr.str, c, pattern->len) == 0)
Willy Tarreau11382812008-07-09 16:18:21 +0200226 return ACL_PAT_PASS;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200227 }
Willy Tarreaua84d3742007-05-07 00:36:48 +0200228 }
Willy Tarreau11382812008-07-09 16:18:21 +0200229 return ACL_PAT_FAIL;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200230}
231
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200232/* Background: Fast way to find a zero byte in a word
233 * http://graphics.stanford.edu/~seander/bithacks.html#ZeroInWord
234 * hasZeroByte = (v - 0x01010101UL) & ~v & 0x80808080UL;
235 *
236 * To look for 4 different byte values, xor the word with those bytes and
237 * then check for zero bytes:
238 *
239 * v = (((unsigned char)c * 0x1010101U) ^ delimiter)
240 * where <delimiter> is the 4 byte values to look for (as an uint)
241 * and <c> is the character that is being tested
242 */
243static inline unsigned int is_delimiter(unsigned char c, unsigned int mask)
244{
245 mask ^= (c * 0x01010101); /* propagate the char to all 4 bytes */
246 return (mask - 0x01010101) & ~mask & 0x80808080U;
247}
248
249static inline unsigned int make_4delim(unsigned char d1, unsigned char d2, unsigned char d3, unsigned char d4)
250{
251 return d1 << 24 | d2 << 16 | d3 << 8 | d4;
252}
253
Willy Tarreaua84d3742007-05-07 00:36:48 +0200254/* This one is used by other real functions. It checks that the pattern is
255 * included inside the tested string, but enclosed between the specified
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200256 * delimiters or at the beginning or end of the string. The delimiters are
257 * provided as an unsigned int made by make_4delim() and match up to 4 different
258 * delimiters. Delimiters are stripped at the beginning and end of the pattern.
Willy Tarreaua84d3742007-05-07 00:36:48 +0200259 */
Willy Tarreau37406352012-04-23 16:16:37 +0200260static int match_word(struct sample *smp, struct acl_pattern *pattern, unsigned int delimiters)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200261{
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200262 int may_match, icase;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200263 char *c, *end;
264 char *ps;
265 int pl;
266
267 pl = pattern->len;
268 ps = pattern->ptr.str;
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200269
270 while (pl > 0 && is_delimiter(*ps, delimiters)) {
Willy Tarreaua84d3742007-05-07 00:36:48 +0200271 pl--;
272 ps++;
273 }
274
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200275 while (pl > 0 && is_delimiter(ps[pl - 1], delimiters))
Willy Tarreaua84d3742007-05-07 00:36:48 +0200276 pl--;
277
Willy Tarreauf853c462012-04-23 18:53:56 +0200278 if (pl > smp->data.str.len)
Willy Tarreau11382812008-07-09 16:18:21 +0200279 return ACL_PAT_FAIL;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200280
281 may_match = 1;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200282 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
Willy Tarreauf853c462012-04-23 18:53:56 +0200283 end = smp->data.str.str + smp->data.str.len - pl;
284 for (c = smp->data.str.str; c <= end; c++) {
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200285 if (is_delimiter(*c, delimiters)) {
Willy Tarreaua84d3742007-05-07 00:36:48 +0200286 may_match = 1;
287 continue;
288 }
Willy Tarreaua84d3742007-05-07 00:36:48 +0200289
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200290 if (!may_match)
291 continue;
292
293 if (icase) {
294 if ((tolower(*c) == tolower(*ps)) &&
295 (strncasecmp(ps, c, pl) == 0) &&
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200296 (c == end || is_delimiter(c[pl], delimiters)))
Willy Tarreau11382812008-07-09 16:18:21 +0200297 return ACL_PAT_PASS;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200298 } else {
299 if ((*c == *ps) &&
300 (strncmp(ps, c, pl) == 0) &&
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200301 (c == end || is_delimiter(c[pl], delimiters)))
Willy Tarreau11382812008-07-09 16:18:21 +0200302 return ACL_PAT_PASS;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200303 }
Willy Tarreaua84d3742007-05-07 00:36:48 +0200304 may_match = 0;
305 }
Willy Tarreau11382812008-07-09 16:18:21 +0200306 return ACL_PAT_FAIL;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200307}
308
309/* Checks that the pattern is included inside the tested string, but enclosed
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200310 * between the delimiters '?' or '/' or at the beginning or end of the string.
311 * Delimiters at the beginning or end of the pattern are ignored.
Willy Tarreaua84d3742007-05-07 00:36:48 +0200312 */
Willy Tarreau37406352012-04-23 16:16:37 +0200313int acl_match_dir(struct sample *smp, struct acl_pattern *pattern)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200314{
Willy Tarreau37406352012-04-23 16:16:37 +0200315 return match_word(smp, pattern, make_4delim('/', '?', '?', '?'));
Willy Tarreaua84d3742007-05-07 00:36:48 +0200316}
317
318/* Checks that the pattern is included inside the tested string, but enclosed
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200319 * between the delmiters '/', '?', '.' or ":" or at the beginning or end of
320 * the string. Delimiters at the beginning or end of the pattern are ignored.
Willy Tarreaua84d3742007-05-07 00:36:48 +0200321 */
Willy Tarreau37406352012-04-23 16:16:37 +0200322int acl_match_dom(struct sample *smp, struct acl_pattern *pattern)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200323{
Willy Tarreau37406352012-04-23 16:16:37 +0200324 return match_word(smp, pattern, make_4delim('/', '?', '.', ':'));
Willy Tarreaua84d3742007-05-07 00:36:48 +0200325}
326
327/* Checks that the integer in <test> is included between min and max */
Willy Tarreau37406352012-04-23 16:16:37 +0200328int acl_match_int(struct sample *smp, struct acl_pattern *pattern)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200329{
Willy Tarreauf853c462012-04-23 18:53:56 +0200330 if ((!pattern->val.range.min_set || pattern->val.range.min <= smp->data.uint) &&
331 (!pattern->val.range.max_set || smp->data.uint <= pattern->val.range.max))
Willy Tarreau11382812008-07-09 16:18:21 +0200332 return ACL_PAT_PASS;
333 return ACL_PAT_FAIL;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200334}
335
Willy Tarreau0e698542011-09-16 08:32:32 +0200336/* Checks that the length of the pattern in <test> is included between min and max */
Willy Tarreau37406352012-04-23 16:16:37 +0200337int acl_match_len(struct sample *smp, struct acl_pattern *pattern)
Willy Tarreau0e698542011-09-16 08:32:32 +0200338{
Willy Tarreauf853c462012-04-23 18:53:56 +0200339 if ((!pattern->val.range.min_set || pattern->val.range.min <= smp->data.str.len) &&
340 (!pattern->val.range.max_set || smp->data.str.len <= pattern->val.range.max))
Willy Tarreau0e698542011-09-16 08:32:32 +0200341 return ACL_PAT_PASS;
342 return ACL_PAT_FAIL;
343}
344
Willy Tarreau37406352012-04-23 16:16:37 +0200345int acl_match_ip(struct sample *smp, struct acl_pattern *pattern)
Willy Tarreaua67fad92007-05-08 19:50:09 +0200346{
Willy Tarreauceb4ac92012-04-28 00:41:46 +0200347 unsigned int v4; /* in network byte order */
348 struct in6_addr *v6;
349 int bits, pos;
350 struct in6_addr tmp6;
Willy Tarreaua67fad92007-05-08 19:50:09 +0200351
Willy Tarreauceb4ac92012-04-28 00:41:46 +0200352 if (pattern->type == SMP_T_IPV4) {
353 if (smp->type == SMP_T_IPV4) {
354 v4 = smp->data.ipv4.s_addr;
355 }
356 else if (smp->type == SMP_T_IPV6) {
357 /* v4 match on a V6 sample. We want to check at least for
358 * the following forms :
359 * - ::ffff:ip:v4 (ipv4 mapped)
360 * - ::0000:ip:v4 (old ipv4 mapped)
361 * - 2002:ip:v4:: (6to4)
362 */
363 if (*(uint32_t*)&smp->data.ipv6.s6_addr[0] == 0 &&
364 *(uint32_t*)&smp->data.ipv6.s6_addr[4] == 0 &&
365 (*(uint32_t*)&smp->data.ipv6.s6_addr[8] == 0 ||
366 *(uint32_t*)&smp->data.ipv6.s6_addr[8] == htonl(0xFFFF))) {
367 v4 = *(uint32_t*)&smp->data.ipv6.s6_addr[12];
368 }
369 else if (*(uint16_t*)&smp->data.ipv6.s6_addr[0] == htons(0x2002)) {
370 v4 = htonl((ntohs(*(uint16_t*)&smp->data.ipv6.s6_addr[2]) << 16) +
371 ntohs(*(uint16_t*)&smp->data.ipv6.s6_addr[4]));
372 }
373 else
374 return ACL_PAT_FAIL;
375 }
376 else
377 return ACL_PAT_FAIL;
Willy Tarreaua67fad92007-05-08 19:50:09 +0200378
Willy Tarreauceb4ac92012-04-28 00:41:46 +0200379 if (((v4 ^ pattern->val.ipv4.addr.s_addr) & pattern->val.ipv4.mask.s_addr) == 0)
380 return ACL_PAT_PASS;
381 else
382 return ACL_PAT_FAIL;
383 }
384 else if (pattern->type == SMP_T_IPV6) {
385 if (smp->type == SMP_T_IPV4) {
386 /* Convert the IPv4 sample address to IPv4 with the
387 * mapping method using the ::ffff: prefix.
388 */
389 memset(&tmp6, 0, 10);
390 *(uint16_t*)&tmp6.s6_addr[10] = htons(0xffff);
391 *(uint32_t*)&tmp6.s6_addr[12] = smp->data.ipv4.s_addr;
392 v6 = &tmp6;
393 }
394 else if (smp->type == SMP_T_IPV6) {
395 v6 = &smp->data.ipv6;
396 }
397 else {
398 return ACL_PAT_FAIL;
399 }
400
401 bits = pattern->val.ipv6.mask;
402 for (pos = 0; bits > 0; pos += 4, bits -= 32) {
403 v4 = *(uint32_t*)&v6->s6_addr[pos] ^ *(uint32_t*)&pattern->val.ipv6.addr.s6_addr[pos];
404 if (bits < 32)
Cyril Bonté4c01beb2012-10-23 21:28:31 +0200405 v4 &= htonl((~0U) << (32-bits));
Willy Tarreauceb4ac92012-04-28 00:41:46 +0200406 if (v4)
407 return ACL_PAT_FAIL;
408 }
Willy Tarreau11382812008-07-09 16:18:21 +0200409 return ACL_PAT_PASS;
Willy Tarreauceb4ac92012-04-28 00:41:46 +0200410 }
Willy Tarreau11382812008-07-09 16:18:21 +0200411 return ACL_PAT_FAIL;
Willy Tarreaua67fad92007-05-08 19:50:09 +0200412}
413
Willy Tarreaub337b532010-05-13 20:03:41 +0200414/* Lookup an IPv4 address in the expression's pattern tree using the longest
415 * match method. The node is returned if it exists, otherwise NULL.
416 */
Willy Tarreau37406352012-04-23 16:16:37 +0200417static void *acl_lookup_ip(struct sample *smp, struct acl_expr *expr)
Willy Tarreaub337b532010-05-13 20:03:41 +0200418{
419 struct in_addr *s;
420
Willy Tarreauf853c462012-04-23 18:53:56 +0200421 if (smp->type != SMP_T_IPV4)
Willy Tarreaub337b532010-05-13 20:03:41 +0200422 return ACL_PAT_FAIL;
423
Willy Tarreauf853c462012-04-23 18:53:56 +0200424 s = &smp->data.ipv4;
Willy Tarreaub337b532010-05-13 20:03:41 +0200425 return ebmb_lookup_longest(&expr->pattern_tree, &s->s_addr);
426}
427
Willy Tarreaua84d3742007-05-07 00:36:48 +0200428/* Parse a string. It is allocated and duplicated. */
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200429int acl_parse_str(const char **text, struct acl_pattern *pattern, int *opaque, char **err)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200430{
431 int len;
432
Willy Tarreauae8b7962007-06-09 23:10:04 +0200433 len = strlen(*text);
Willy Tarreauc92ddbc2012-04-27 22:10:57 +0200434 pattern->type = SMP_T_CSTR;
Willy Tarreauc4262962010-05-10 23:42:40 +0200435
436 if (pattern->flags & ACL_PAT_F_TREE_OK) {
437 /* we're allowed to put the data in a tree whose root is pointed
438 * to by val.tree.
439 */
440 struct ebmb_node *node;
441
442 node = calloc(1, sizeof(*node) + len + 1);
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200443 if (!node) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200444 memprintf(err, "out of memory while loading string pattern");
Willy Tarreauc4262962010-05-10 23:42:40 +0200445 return 0;
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200446 }
Willy Tarreauc4262962010-05-10 23:42:40 +0200447 memcpy(node->key, *text, len + 1);
448 if (ebst_insert(pattern->val.tree, node) != node)
449 free(node); /* was a duplicate */
450 pattern->flags |= ACL_PAT_F_TREE; /* this pattern now contains a tree */
451 return 1;
452 }
453
Willy Tarreauae8b7962007-06-09 23:10:04 +0200454 pattern->ptr.str = strdup(*text);
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200455 if (!pattern->ptr.str) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200456 memprintf(err, "out of memory while loading string pattern");
Willy Tarreaua84d3742007-05-07 00:36:48 +0200457 return 0;
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200458 }
Willy Tarreaua84d3742007-05-07 00:36:48 +0200459 pattern->len = len;
460 return 1;
461}
462
Emeric Brun07ca4962012-10-17 13:38:19 +0200463/* Parse a binary written in hexa. It is allocated. */
464int acl_parse_bin(const char **text, struct acl_pattern *pattern, int *opaque, char **err)
465{
466 int len;
467 const char *p = *text;
468 int i,j;
469
470 len = strlen(p);
471 if (len%2) {
472 memprintf(err, "an even number of hex digit is expected");
473 return 0;
474 }
475
476 pattern->type = SMP_T_CBIN;
477 pattern->len = len >> 1;
478 pattern->ptr.str = malloc(pattern->len);
479 if (!pattern->ptr.str) {
480 memprintf(err, "out of memory while loading string pattern");
481 return 0;
482 }
483
484 i = j = 0;
485 while (j < pattern->len) {
486 if (!ishex(p[i++]))
487 goto bad_input;
488 if (!ishex(p[i++]))
489 goto bad_input;
490 pattern->ptr.str[j++] = (hex2i(p[i-2]) << 4) + hex2i(p[i-1]);
491 }
492 return 1;
493
494bad_input:
495 memprintf(err, "an hex digit is expected (found '%c')", p[i-1]);
496 free(pattern->ptr.str);
497 return 0;
498}
499
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +0100500/* Parse and concatenate all further strings into one. */
501int
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200502acl_parse_strcat(const char **text, struct acl_pattern *pattern, int *opaque, char **err)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +0100503{
504
505 int len = 0, i;
506 char *s;
507
508 for (i = 0; *text[i]; i++)
509 len += strlen(text[i])+1;
510
Willy Tarreauc92ddbc2012-04-27 22:10:57 +0200511 pattern->type = SMP_T_CSTR;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +0100512 pattern->ptr.str = s = calloc(1, len);
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200513 if (!pattern->ptr.str) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200514 memprintf(err, "out of memory while loading pattern");
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +0100515 return 0;
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200516 }
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +0100517
518 for (i = 0; *text[i]; i++)
519 s += sprintf(s, i?" %s":"%s", text[i]);
520
521 pattern->len = len;
522
523 return i;
524}
525
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +0200526/* Free data allocated by acl_parse_reg */
Willy Tarreau37406352012-04-23 16:16:37 +0200527static void acl_free_reg(void *ptr)
528{
Hiroaki Nakamura70351322013-01-13 15:00:42 +0900529 regex_free(ptr);
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +0200530}
531
Willy Tarreauf3d25982007-05-08 22:45:09 +0200532/* Parse a regex. It is allocated. */
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200533int acl_parse_reg(const char **text, struct acl_pattern *pattern, int *opaque, char **err)
Willy Tarreauf3d25982007-05-08 22:45:09 +0200534{
Hiroaki Nakamura70351322013-01-13 15:00:42 +0900535 regex *preg;
Willy Tarreauf3d25982007-05-08 22:45:09 +0200536
Hiroaki Nakamura70351322013-01-13 15:00:42 +0900537 preg = calloc(1, sizeof(*preg));
Willy Tarreauf3d25982007-05-08 22:45:09 +0200538
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200539 if (!preg) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200540 memprintf(err, "out of memory while loading pattern");
Willy Tarreauf3d25982007-05-08 22:45:09 +0200541 return 0;
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200542 }
Willy Tarreauf3d25982007-05-08 22:45:09 +0200543
Thierry FOURNIERed5a4ae2013-10-14 14:07:36 +0200544 if (!regex_comp(*text, preg, !(pattern->flags & ACL_PAT_F_IGNORE_CASE), 0, err)) {
Hiroaki Nakamura70351322013-01-13 15:00:42 +0900545 free(preg);
Hiroaki Nakamura70351322013-01-13 15:00:42 +0900546 return 0;
547 }
548
Willy Tarreauf3d25982007-05-08 22:45:09 +0200549 pattern->ptr.reg = preg;
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +0200550 pattern->freeptrbuf = &acl_free_reg;
Willy Tarreauf3d25982007-05-08 22:45:09 +0200551 return 1;
552}
553
Willy Tarreauae8b7962007-06-09 23:10:04 +0200554/* Parse a range of positive integers delimited by either ':' or '-'. If only
555 * one integer is read, it is set as both min and max. An operator may be
556 * specified as the prefix, among this list of 5 :
557 *
558 * 0:eq, 1:gt, 2:ge, 3:lt, 4:le
559 *
560 * The default operator is "eq". It supports range matching. Ranges are
561 * rejected for other operators. The operator may be changed at any time.
562 * The operator is stored in the 'opaque' argument.
563 *
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200564 * If err is non-NULL, an error message will be returned there on errors and
565 * the caller will have to free it.
566 *
Willy Tarreaua84d3742007-05-07 00:36:48 +0200567 */
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200568int acl_parse_int(const char **text, struct acl_pattern *pattern, int *opaque, char **err)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200569{
Willy Tarreauae8b7962007-06-09 23:10:04 +0200570 signed long long i;
571 unsigned int j, last, skip = 0;
572 const char *ptr = *text;
573
Willy Tarreauc92ddbc2012-04-27 22:10:57 +0200574 pattern->type = SMP_T_UINT;
Willy Tarreau8f8e6452007-06-17 21:51:38 +0200575 while (!isdigit((unsigned char)*ptr)) {
Willy Tarreau1c7cc5b2010-07-18 10:46:33 +0200576 switch (get_std_op(ptr)) {
577 case STD_OP_EQ: *opaque = 0; break;
578 case STD_OP_GT: *opaque = 1; break;
579 case STD_OP_GE: *opaque = 2; break;
580 case STD_OP_LT: *opaque = 3; break;
581 case STD_OP_LE: *opaque = 4; break;
582 default:
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200583 memprintf(err, "'%s' is neither a number nor a supported operator", ptr);
Willy Tarreauae8b7962007-06-09 23:10:04 +0200584 return 0;
Willy Tarreau1c7cc5b2010-07-18 10:46:33 +0200585 }
Willy Tarreauae8b7962007-06-09 23:10:04 +0200586
587 skip++;
588 ptr = text[skip];
589 }
Willy Tarreaua84d3742007-05-07 00:36:48 +0200590
591 last = i = 0;
592 while (1) {
Willy Tarreauae8b7962007-06-09 23:10:04 +0200593 j = *ptr++;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200594 if ((j == '-' || j == ':') && !last) {
595 last++;
596 pattern->val.range.min = i;
597 i = 0;
598 continue;
599 }
600 j -= '0';
601 if (j > 9)
602 // also catches the terminating zero
603 break;
604 i *= 10;
605 i += j;
606 }
Willy Tarreauae8b7962007-06-09 23:10:04 +0200607
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200608 if (last && *opaque >= 1 && *opaque <= 4) {
Willy Tarreauae8b7962007-06-09 23:10:04 +0200609 /* having a range with a min or a max is absurd */
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200610 memprintf(err, "integer range '%s' specified with a comparison operator", text[skip]);
Willy Tarreauae8b7962007-06-09 23:10:04 +0200611 return 0;
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200612 }
Willy Tarreauae8b7962007-06-09 23:10:04 +0200613
Willy Tarreaua84d3742007-05-07 00:36:48 +0200614 if (!last)
615 pattern->val.range.min = i;
616 pattern->val.range.max = i;
Willy Tarreauae8b7962007-06-09 23:10:04 +0200617
618 switch (*opaque) {
619 case 0: /* eq */
620 pattern->val.range.min_set = 1;
621 pattern->val.range.max_set = 1;
622 break;
623 case 1: /* gt */
624 pattern->val.range.min++; /* gt = ge + 1 */
625 case 2: /* ge */
626 pattern->val.range.min_set = 1;
627 pattern->val.range.max_set = 0;
628 break;
629 case 3: /* lt */
630 pattern->val.range.max--; /* lt = le - 1 */
631 case 4: /* le */
632 pattern->val.range.min_set = 0;
633 pattern->val.range.max_set = 1;
634 break;
635 }
636 return skip + 1;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200637}
638
Willy Tarreau4a26d2f2008-07-15 16:05:33 +0200639/* Parse a range of positive 2-component versions delimited by either ':' or
640 * '-'. The version consists in a major and a minor, both of which must be
641 * smaller than 65536, because internally they will be represented as a 32-bit
642 * integer.
643 * If only one version is read, it is set as both min and max. Just like for
644 * pure integers, an operator may be specified as the prefix, among this list
645 * of 5 :
646 *
647 * 0:eq, 1:gt, 2:ge, 3:lt, 4:le
648 *
649 * The default operator is "eq". It supports range matching. Ranges are
650 * rejected for other operators. The operator may be changed at any time.
651 * The operator is stored in the 'opaque' argument. This allows constructs
652 * such as the following one :
653 *
654 * acl obsolete_ssl ssl_req_proto lt 3
655 * acl unsupported_ssl ssl_req_proto gt 3.1
656 * acl valid_ssl ssl_req_proto 3.0-3.1
657 *
658 */
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200659int acl_parse_dotted_ver(const char **text, struct acl_pattern *pattern, int *opaque, char **err)
Willy Tarreau4a26d2f2008-07-15 16:05:33 +0200660{
661 signed long long i;
662 unsigned int j, last, skip = 0;
663 const char *ptr = *text;
664
665
666 while (!isdigit((unsigned char)*ptr)) {
Willy Tarreau1c7cc5b2010-07-18 10:46:33 +0200667 switch (get_std_op(ptr)) {
668 case STD_OP_EQ: *opaque = 0; break;
669 case STD_OP_GT: *opaque = 1; break;
670 case STD_OP_GE: *opaque = 2; break;
671 case STD_OP_LT: *opaque = 3; break;
672 case STD_OP_LE: *opaque = 4; break;
673 default:
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200674 memprintf(err, "'%s' is neither a number nor a supported operator", ptr);
Willy Tarreau4a26d2f2008-07-15 16:05:33 +0200675 return 0;
Willy Tarreau1c7cc5b2010-07-18 10:46:33 +0200676 }
Willy Tarreau4a26d2f2008-07-15 16:05:33 +0200677
678 skip++;
679 ptr = text[skip];
680 }
681
682 last = i = 0;
683 while (1) {
684 j = *ptr++;
685 if (j == '.') {
686 /* minor part */
687 if (i >= 65536)
688 return 0;
689 i <<= 16;
690 continue;
691 }
692 if ((j == '-' || j == ':') && !last) {
693 last++;
694 if (i < 65536)
695 i <<= 16;
696 pattern->val.range.min = i;
697 i = 0;
698 continue;
699 }
700 j -= '0';
701 if (j > 9)
702 // also catches the terminating zero
703 break;
704 i = (i & 0xFFFF0000) + (i & 0xFFFF) * 10;
705 i += j;
706 }
707
708 /* if we only got a major version, let's shift it now */
709 if (i < 65536)
710 i <<= 16;
711
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200712 if (last && *opaque >= 1 && *opaque <= 4) {
Willy Tarreau4a26d2f2008-07-15 16:05:33 +0200713 /* having a range with a min or a max is absurd */
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200714 memprintf(err, "version range '%s' specified with a comparison operator", text[skip]);
Willy Tarreau4a26d2f2008-07-15 16:05:33 +0200715 return 0;
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200716 }
Willy Tarreau4a26d2f2008-07-15 16:05:33 +0200717
718 if (!last)
719 pattern->val.range.min = i;
720 pattern->val.range.max = i;
721
722 switch (*opaque) {
723 case 0: /* eq */
724 pattern->val.range.min_set = 1;
725 pattern->val.range.max_set = 1;
726 break;
727 case 1: /* gt */
728 pattern->val.range.min++; /* gt = ge + 1 */
729 case 2: /* ge */
730 pattern->val.range.min_set = 1;
731 pattern->val.range.max_set = 0;
732 break;
733 case 3: /* lt */
734 pattern->val.range.max--; /* lt = le - 1 */
735 case 4: /* le */
736 pattern->val.range.min_set = 0;
737 pattern->val.range.max_set = 1;
738 break;
739 }
740 return skip + 1;
741}
742
Willy Tarreaua67fad92007-05-08 19:50:09 +0200743/* Parse an IP address and an optional mask in the form addr[/mask].
744 * The addr may either be an IPv4 address or a hostname. The mask
745 * may either be a dotted mask or a number of bits. Returns 1 if OK,
Willy Tarreauc92ddbc2012-04-27 22:10:57 +0200746 * otherwise 0. NOTE: IP address patterns are typed (IPV4/IPV6).
Willy Tarreaua67fad92007-05-08 19:50:09 +0200747 */
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200748int acl_parse_ip(const char **text, struct acl_pattern *pattern, int *opaque, char **err)
Willy Tarreaua67fad92007-05-08 19:50:09 +0200749{
Willy Tarreaub337b532010-05-13 20:03:41 +0200750 struct eb_root *tree = NULL;
751 if (pattern->flags & ACL_PAT_F_TREE_OK)
752 tree = pattern->val.tree;
753
754 if (str2net(*text, &pattern->val.ipv4.addr, &pattern->val.ipv4.mask)) {
755 unsigned int mask = ntohl(pattern->val.ipv4.mask.s_addr);
756 struct ebmb_node *node;
757 /* check if the mask is contiguous so that we can insert the
758 * network into the tree. A continuous mask has only ones on
759 * the left. This means that this mask + its lower bit added
760 * once again is null.
761 */
Willy Tarreauceb4ac92012-04-28 00:41:46 +0200762 pattern->type = SMP_T_IPV4;
Willy Tarreaub337b532010-05-13 20:03:41 +0200763 if (mask + (mask & -mask) == 0 && tree) {
764 mask = mask ? 33 - flsnz(mask & -mask) : 0; /* equals cidr value */
765 /* FIXME: insert <addr>/<mask> into the tree here */
766 node = calloc(1, sizeof(*node) + 4); /* reserve 4 bytes for IPv4 address */
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200767 if (!node) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200768 memprintf(err, "out of memory while loading IPv4 pattern");
Willy Tarreaub337b532010-05-13 20:03:41 +0200769 return 0;
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200770 }
Willy Tarreaub337b532010-05-13 20:03:41 +0200771 memcpy(node->key, &pattern->val.ipv4.addr, 4); /* network byte order */
772 node->node.pfx = mask;
773 if (ebmb_insert_prefix(tree, node, 4) != node)
774 free(node); /* was a duplicate */
775 pattern->flags |= ACL_PAT_F_TREE;
776 return 1;
777 }
Willy Tarreauae8b7962007-06-09 23:10:04 +0200778 return 1;
Willy Tarreaub337b532010-05-13 20:03:41 +0200779 }
Willy Tarreauceb4ac92012-04-28 00:41:46 +0200780 else if (str62net(*text, &pattern->val.ipv6.addr, &pattern->val.ipv6.mask)) {
781 /* no tree support right now */
782 pattern->type = SMP_T_IPV6;
783 return 1;
784 }
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200785 else {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200786 memprintf(err, "'%s' is not a valid IPv4 or IPv6 address", *text);
Willy Tarreauae8b7962007-06-09 23:10:04 +0200787 return 0;
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200788 }
Willy Tarreaua67fad92007-05-08 19:50:09 +0200789}
790
Willy Tarreaua84d3742007-05-07 00:36:48 +0200791/*
792 * Registers the ACL keyword list <kwl> as a list of valid keywords for next
793 * parsing sessions.
794 */
795void acl_register_keywords(struct acl_kw_list *kwl)
796{
797 LIST_ADDQ(&acl_keywords.list, &kwl->list);
798}
799
800/*
801 * Unregisters the ACL keyword list <kwl> from the list of valid keywords.
802 */
803void acl_unregister_keywords(struct acl_kw_list *kwl)
804{
805 LIST_DEL(&kwl->list);
806 LIST_INIT(&kwl->list);
807}
808
809/* Return a pointer to the ACL <name> within the list starting at <head>, or
810 * NULL if not found.
811 */
812struct acl *find_acl_by_name(const char *name, struct list *head)
813{
814 struct acl *acl;
815 list_for_each_entry(acl, head, list) {
816 if (strcmp(acl->name, name) == 0)
817 return acl;
818 }
819 return NULL;
820}
821
822/* Return a pointer to the ACL keyword <kw>, or NULL if not found. Note that if
823 * <kw> contains an opening parenthesis, only the left part of it is checked.
824 */
825struct acl_keyword *find_acl_kw(const char *kw)
826{
827 int index;
828 const char *kwend;
829 struct acl_kw_list *kwl;
830
831 kwend = strchr(kw, '(');
832 if (!kwend)
833 kwend = kw + strlen(kw);
834
835 list_for_each_entry(kwl, &acl_keywords.list, list) {
836 for (index = 0; kwl->kw[index].kw != NULL; index++) {
837 if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) &&
838 kwl->kw[index].kw[kwend-kw] == 0)
839 return &kwl->kw[index];
840 }
841 }
842 return NULL;
843}
844
Willy Tarreaudfd7fca2011-03-09 07:27:02 +0100845/* NB: does nothing if <pat> is NULL */
Willy Tarreaua84d3742007-05-07 00:36:48 +0200846static void free_pattern(struct acl_pattern *pat)
847{
Willy Tarreaudfd7fca2011-03-09 07:27:02 +0100848 if (!pat)
849 return;
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +0200850
851 if (pat->ptr.ptr) {
852 if (pat->freeptrbuf)
853 pat->freeptrbuf(pat->ptr.ptr);
854
Willy Tarreaua84d3742007-05-07 00:36:48 +0200855 free(pat->ptr.ptr);
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +0200856 }
857
Willy Tarreaua84d3742007-05-07 00:36:48 +0200858 free(pat);
859}
860
861static void free_pattern_list(struct list *head)
862{
863 struct acl_pattern *pat, *tmp;
864 list_for_each_entry_safe(pat, tmp, head, list)
865 free_pattern(pat);
866}
867
Willy Tarreaue56cda92010-05-11 23:25:05 +0200868static void free_pattern_tree(struct eb_root *root)
869{
870 struct eb_node *node, *next;
871 node = eb_first(root);
872 while (node) {
873 next = eb_next(node);
Willy Tarreau60eccc12013-11-14 16:00:12 +0100874 eb_delete(node);
Willy Tarreaue56cda92010-05-11 23:25:05 +0200875 free(node);
876 node = next;
877 }
878}
879
Willy Tarreaua84d3742007-05-07 00:36:48 +0200880static struct acl_expr *prune_acl_expr(struct acl_expr *expr)
881{
Willy Tarreau34db1082012-04-19 17:16:54 +0200882 struct arg *arg;
883
Willy Tarreaua84d3742007-05-07 00:36:48 +0200884 free_pattern_list(&expr->patterns);
Willy Tarreaue56cda92010-05-11 23:25:05 +0200885 free_pattern_tree(&expr->pattern_tree);
Willy Tarreaua84d3742007-05-07 00:36:48 +0200886 LIST_INIT(&expr->patterns);
Willy Tarreau34db1082012-04-19 17:16:54 +0200887
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100888 for (arg = expr->smp->arg_p; arg; arg++) {
Willy Tarreau34db1082012-04-19 17:16:54 +0200889 if (arg->type == ARGT_STOP)
890 break;
Willy Tarreau496aa012012-06-01 10:38:29 +0200891 if (arg->type == ARGT_STR || arg->unresolved) {
Willy Tarreau34db1082012-04-19 17:16:54 +0200892 free(arg->data.str.str);
893 arg->data.str.str = NULL;
Willy Tarreau496aa012012-06-01 10:38:29 +0200894 arg->unresolved = 0;
Willy Tarreau34db1082012-04-19 17:16:54 +0200895 }
Willy Tarreau34db1082012-04-19 17:16:54 +0200896 }
897
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100898 if (expr->smp->arg_p != empty_arg_list)
899 free(expr->smp->arg_p);
Willy Tarreaua84d3742007-05-07 00:36:48 +0200900 return expr;
901}
902
Willy Tarreau08ad0b32012-04-27 17:25:24 +0200903
904/* Reads patterns from a file. If <err_msg> is non-NULL, an error message will
905 * be returned there on errors and the caller will have to free it.
906 */
Willy Tarreaud76a98a2013-03-31 18:34:33 +0200907static int acl_read_patterns_from_file(struct acl_expr *expr,
908 const char *filename, int patflags,
909 char **err)
Willy Tarreau2b5285d2010-05-09 23:45:24 +0200910{
911 FILE *file;
912 char *c;
913 const char *args[2];
914 struct acl_pattern *pattern;
915 int opaque;
Willy Tarreau6a8097f2011-02-26 15:14:15 +0100916 int ret = 0;
Willy Tarreau08ad0b32012-04-27 17:25:24 +0200917 int line = 0;
Willy Tarreau2b5285d2010-05-09 23:45:24 +0200918
919 file = fopen(filename, "r");
Willy Tarreau08ad0b32012-04-27 17:25:24 +0200920 if (!file) {
921 memprintf(err, "failed to open pattern file <%s>", filename);
Willy Tarreau2b5285d2010-05-09 23:45:24 +0200922 return 0;
Willy Tarreau08ad0b32012-04-27 17:25:24 +0200923 }
Willy Tarreau2b5285d2010-05-09 23:45:24 +0200924
925 /* now parse all patterns. The file may contain only one pattern per
926 * line. If the line contains spaces, they will be part of the pattern.
927 * The pattern stops at the first CR, LF or EOF encountered.
928 */
929 opaque = 0;
Willy Tarreaue56cda92010-05-11 23:25:05 +0200930 pattern = NULL;
Willy Tarreau2b5285d2010-05-09 23:45:24 +0200931 args[1] = "";
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100932 while (fgets(trash.str, trash.size, file) != NULL) {
Willy Tarreau08ad0b32012-04-27 17:25:24 +0200933 line++;
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100934 c = trash.str;
Willy Tarreau58215a02010-05-13 22:07:43 +0200935
936 /* ignore lines beginning with a dash */
937 if (*c == '#')
938 continue;
939
940 /* strip leading spaces and tabs */
941 while (*c == ' ' || *c == '\t')
942 c++;
943
Willy Tarreau58215a02010-05-13 22:07:43 +0200944
945 args[0] = c;
Willy Tarreau2b5285d2010-05-09 23:45:24 +0200946 while (*c && *c != '\n' && *c != '\r')
947 c++;
948 *c = 0;
949
Willy Tarreau51091962011-01-03 21:04:10 +0100950 /* empty lines are ignored too */
951 if (c == args[0])
952 continue;
953
Willy Tarreaue56cda92010-05-11 23:25:05 +0200954 /* we keep the previous pattern along iterations as long as it's not used */
955 if (!pattern)
956 pattern = (struct acl_pattern *)malloc(sizeof(*pattern));
Willy Tarreau08ad0b32012-04-27 17:25:24 +0200957 if (!pattern) {
958 memprintf(err, "out of memory when loading patterns from file <%s>", filename);
Willy Tarreau2b5285d2010-05-09 23:45:24 +0200959 goto out_close;
Willy Tarreau08ad0b32012-04-27 17:25:24 +0200960 }
Willy Tarreaue56cda92010-05-11 23:25:05 +0200961
962 memset(pattern, 0, sizeof(*pattern));
Willy Tarreau2b5285d2010-05-09 23:45:24 +0200963 pattern->flags = patflags;
964
Willy Tarreaue0db1e82013-01-04 16:31:47 +0100965 if (!(pattern->flags & ACL_PAT_F_IGNORE_CASE) &&
Willy Tarreaud76a98a2013-03-31 18:34:33 +0200966 (expr->match == acl_match_str || expr->match == acl_match_ip)) {
Willy Tarreaue56cda92010-05-11 23:25:05 +0200967 /* we pre-set the data pointer to the tree's head so that functions
968 * which are able to insert in a tree know where to do that.
969 */
970 pattern->flags |= ACL_PAT_F_TREE_OK;
971 pattern->val.tree = &expr->pattern_tree;
972 }
973
Willy Tarreauc92ddbc2012-04-27 22:10:57 +0200974 pattern->type = SMP_TYPES; /* unspecified type by default */
Willy Tarreaud76a98a2013-03-31 18:34:33 +0200975 if (!expr->parse(args, pattern, &opaque, err))
Willy Tarreau2b5285d2010-05-09 23:45:24 +0200976 goto out_free_pattern;
Willy Tarreaue56cda92010-05-11 23:25:05 +0200977
978 /* if the parser did not feed the tree, let's chain the pattern to the list */
979 if (!(pattern->flags & ACL_PAT_F_TREE)) {
980 LIST_ADDQ(&expr->patterns, &pattern->list);
981 pattern = NULL; /* get a new one */
982 }
Willy Tarreau2b5285d2010-05-09 23:45:24 +0200983 }
Willy Tarreau6a8097f2011-02-26 15:14:15 +0100984
985 ret = 1; /* success */
Willy Tarreau2b5285d2010-05-09 23:45:24 +0200986
987 out_free_pattern:
988 free_pattern(pattern);
989 out_close:
990 fclose(file);
Willy Tarreau6a8097f2011-02-26 15:14:15 +0100991 return ret;
Willy Tarreau2b5285d2010-05-09 23:45:24 +0200992}
993
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200994/* Parse an ACL expression starting at <args>[0], and return it. If <err> is
995 * not NULL, it will be filled with a pointer to an error message in case of
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200996 * error. This pointer must be freeable or NULL. <al> is an arg_list serving
997 * as a list head to report missing dependencies.
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200998 *
Willy Tarreaua84d3742007-05-07 00:36:48 +0200999 * Right now, the only accepted syntax is :
1000 * <subject> [<value>...]
1001 */
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001002struct acl_expr *parse_acl_expr(const char **args, char **err, struct arg_list *al)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001003{
1004 __label__ out_return, out_free_expr, out_free_pattern;
1005 struct acl_expr *expr;
1006 struct acl_keyword *aclkw;
1007 struct acl_pattern *pattern;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02001008 int opaque, patflags;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001009 const char *arg;
Thierry FOURNIER348971e2013-11-21 10:50:10 +01001010 struct sample_expr *smp = NULL;
1011 const char *p;
1012 int idx = 0;
1013 char *ckw = NULL;
1014 const char *begw;
1015 const char *endw;
1016 unsigned long prev_type;
1017 int cur_type;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001018
Willy Tarreaubef91e72013-03-31 23:14:46 +02001019 /* First, we lookd for an ACL keyword. And if we don't find one, then
1020 * we look for a sample fetch keyword.
1021 */
Willy Tarreaua84d3742007-05-07 00:36:48 +02001022 aclkw = find_acl_kw(args[0]);
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001023 if (!aclkw || !aclkw->parse) {
Willy Tarreaubef91e72013-03-31 23:14:46 +02001024
Thierry FOURNIER348971e2013-11-21 10:50:10 +01001025 smp = sample_parse_expr((char **)args, &idx, trash.str, trash.size, al);
Willy Tarreaubef91e72013-03-31 23:14:46 +02001026
1027 if (!smp) {
Thierry FOURNIER348971e2013-11-21 10:50:10 +01001028 memprintf(err, "unknown ACL or sample keyword '%s': %s", *args, trash.str);
Willy Tarreaubef91e72013-03-31 23:14:46 +02001029 goto out_return;
1030 }
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001031 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001032
1033 expr = (struct acl_expr *)calloc(1, sizeof(*expr));
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001034 if (!expr) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001035 memprintf(err, "out of memory when parsing ACL expression");
Willy Tarreaua84d3742007-05-07 00:36:48 +02001036 goto out_return;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001037 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001038
Thierry FOURNIER348971e2013-11-21 10:50:10 +01001039 expr->kw = aclkw ? aclkw->kw : smp->fetch->kw;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001040 LIST_INIT(&expr->patterns);
Willy Tarreaue56cda92010-05-11 23:25:05 +02001041 expr->pattern_tree = EB_ROOT_UNIQUE;
Willy Tarreaubef91e72013-03-31 23:14:46 +02001042 expr->parse = aclkw ? aclkw->parse : NULL;
1043 expr->match = aclkw ? aclkw->match : NULL;
Thierry FOURNIER348971e2013-11-21 10:50:10 +01001044 expr->smp = aclkw ? NULL : smp;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001045
Willy Tarreau9987ea92013-06-11 21:09:06 +02001046 if (!expr->parse) {
1047 /* some types can be automatically converted */
1048
Thierry FOURNIER348971e2013-11-21 10:50:10 +01001049 switch (expr->smp ? expr->smp->fetch->out_type : aclkw->smp->out_type) {
Willy Tarreau9987ea92013-06-11 21:09:06 +02001050 case SMP_T_BOOL:
1051 expr->parse = acl_parse_fcts[ACL_MATCH_BOOL];
1052 expr->match = acl_match_fcts[ACL_MATCH_BOOL];
1053 break;
1054 case SMP_T_SINT:
1055 case SMP_T_UINT:
1056 expr->parse = acl_parse_fcts[ACL_MATCH_INT];
1057 expr->match = acl_match_fcts[ACL_MATCH_INT];
1058 break;
1059 case SMP_T_IPV4:
1060 case SMP_T_IPV6:
1061 expr->parse = acl_parse_fcts[ACL_MATCH_IP];
1062 expr->match = acl_match_fcts[ACL_MATCH_IP];
1063 break;
1064 }
1065 }
1066
Thierry FOURNIER348971e2013-11-21 10:50:10 +01001067 /* now parse the rest of acl only if "find_acl_kw" match */
1068 if (aclkw) {
Willy Tarreau34db1082012-04-19 17:16:54 +02001069
Thierry FOURNIER348971e2013-11-21 10:50:10 +01001070 /* build new sample expression */
1071 expr->smp = calloc(1, sizeof(struct sample_expr));
1072 if (!expr->smp) {
1073 memprintf(err, "out of memory when parsing ACL expression");
1074 goto out_return;
1075 }
1076 LIST_INIT(&(expr->smp->conv_exprs));
1077 expr->smp->fetch = aclkw->smp;
1078 expr->smp->arg_p = empty_arg_list;
Willy Tarreau34db1082012-04-19 17:16:54 +02001079
Thierry FOURNIER348971e2013-11-21 10:50:10 +01001080 /* look for the begining of the subject arguments */
1081 p = strchr(args[0], ',');
1082 arg = strchr(args[0], '(');
1083 if (p && arg && p < arg)
1084 arg = NULL;
1085
1086 if (expr->smp->fetch->arg_mask) {
1087 int nbargs = 0;
1088 char *end;
1089
1090 if (arg != NULL) {
1091 /* there are 0 or more arguments in the form "subject(arg[,arg]*)" */
1092 arg++;
1093 end = strchr(arg, ')');
1094 if (!end) {
1095 memprintf(err, "missing closing ')' after arguments to ACL keyword '%s'", expr->kw);
1096 goto out_free_expr;
1097 }
1098
1099 /* Parse the arguments. Note that currently we have no way to
1100 * report parsing errors, hence the NULL in the error pointers.
1101 * An error is also reported if some mandatory arguments are
1102 * missing. We prepare the args list to report unresolved
1103 * dependencies.
1104 */
1105 al->ctx = ARGC_ACL;
1106 al->kw = expr->kw;
1107 al->conv = NULL;
1108 nbargs = make_arg_list(arg, end - arg, expr->smp->fetch->arg_mask, &expr->smp->arg_p,
1109 err, NULL, NULL, al);
1110 if (nbargs < 0) {
1111 /* note that make_arg_list will have set <err> here */
1112 memprintf(err, "in argument to '%s', %s", expr->kw, *err);
1113 goto out_free_expr;
1114 }
1115
1116 if (!expr->smp->arg_p)
1117 expr->smp->arg_p = empty_arg_list;
1118
1119 if (expr->smp->fetch->val_args && !expr->smp->fetch->val_args(expr->smp->arg_p, err)) {
1120 /* invalid keyword argument, error must have been
1121 * set by val_args().
1122 */
1123 memprintf(err, "in argument to '%s', %s", expr->kw, *err);
1124 goto out_free_expr;
1125 }
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001126 }
Thierry FOURNIER348971e2013-11-21 10:50:10 +01001127 else if (ARGM(expr->smp->fetch->arg_mask) == 1) {
1128 int type = (expr->smp->fetch->arg_mask >> 4) & 15;
Willy Tarreauae52f062012-04-26 12:13:35 +02001129
Thierry FOURNIER348971e2013-11-21 10:50:10 +01001130 /* If a proxy is noted as a mandatory argument, we'll fake
1131 * an empty one so that acl_find_targets() resolves it as
1132 * the current one later.
1133 */
1134 if (type != ARGT_FE && type != ARGT_BE && type != ARGT_TAB) {
1135 memprintf(err, "ACL keyword '%s' expects %d arguments", expr->kw, ARGM(expr->smp->fetch->arg_mask));
1136 goto out_free_expr;
1137 }
Willy Tarreau2e845be2012-10-19 19:49:09 +02001138
Thierry FOURNIER348971e2013-11-21 10:50:10 +01001139 /* Build an arg list containing the type as an empty string
1140 * and the usual STOP.
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001141 */
Thierry FOURNIER348971e2013-11-21 10:50:10 +01001142 expr->smp->arg_p = calloc(2, sizeof(*expr->smp->arg_p));
1143 expr->smp->arg_p[0].type = type;
1144 expr->smp->arg_p[0].unresolved = 1;
1145 expr->smp->arg_p[0].data.str.str = strdup("");
1146 expr->smp->arg_p[0].data.str.size = 1;
1147 expr->smp->arg_p[0].data.str.len = 0;
1148
1149 al->ctx = ARGC_ACL;
1150 al->kw = expr->kw;
1151 al->conv = NULL;
1152 arg_list_add(al, &expr->smp->arg_p[0], 0);
1153
1154 expr->smp->arg_p[1].type = ARGT_STOP;
1155 }
1156 else if (ARGM(expr->smp->fetch->arg_mask)) {
1157 /* there were some mandatory arguments */
1158 memprintf(err, "ACL keyword '%s' expects %d arguments", expr->kw, ARGM(expr->smp->fetch->arg_mask));
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001159 goto out_free_expr;
1160 }
Willy Tarreau61612d42012-04-19 18:42:05 +02001161 }
Thierry FOURNIER348971e2013-11-21 10:50:10 +01001162 else {
1163 if (arg ) {
1164 /* no argument expected */
1165 memprintf(err, "ACL keyword '%s' takes no argument", expr->kw);
Willy Tarreaufc2c1fd2012-04-19 23:35:54 +02001166 goto out_free_expr;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001167 }
Thierry FOURNIER348971e2013-11-21 10:50:10 +01001168 }
Willy Tarreau9ca69362013-10-22 19:10:06 +02001169
Thierry FOURNIER348971e2013-11-21 10:50:10 +01001170 /* Now process the converters if any. We have two supported syntaxes
1171 * for the converters, which can be combined :
1172 * - comma-delimited list of converters just after the keyword and args ;
1173 * - one converter per keyword
1174 * The combination allows to have each keyword being a comma-delimited
1175 * series of converters.
1176 *
1177 * We want to process the former first, then the latter. For this we start
1178 * from the beginning of the supposed place in the exiting conv chain, which
1179 * starts at the last comma (endt).
1180 */
Willy Tarreauf75d0082013-04-07 21:20:44 +02001181
Thierry FOURNIER348971e2013-11-21 10:50:10 +01001182 /* look for the begining of the converters list */
1183 arg = strchr(args[0], ',');
Willy Tarreau61612d42012-04-19 18:42:05 +02001184 if (arg) {
Thierry FOURNIER348971e2013-11-21 10:50:10 +01001185 prev_type = expr->smp->fetch->out_type;
1186 while (1) {
1187 struct sample_conv *conv;
1188 struct sample_conv_expr *conv_expr;
1189
1190 if (*arg == ')') /* skip last closing parenthesis */
1191 arg++;
1192
1193 if (*arg && *arg != ',') {
1194 if (ckw)
1195 memprintf(err, "ACL keyword '%s' : missing comma after conv keyword '%s'.",
1196 expr->kw, ckw);
1197 else
1198 memprintf(err, "ACL keyword '%s' : missing comma after fetch keyword.",
1199 expr->kw);
1200 goto out_free_expr;
1201 }
1202
1203 while (*arg == ',') /* then trailing commas */
1204 arg++;
1205
1206 begw = arg; /* start of conv keyword */
1207
1208 if (!*begw)
1209 /* none ? end of converters */
1210 break;
1211
1212 for (endw = begw; *endw && *endw != '(' && *endw != ','; endw++);
1213
1214 free(ckw);
1215 ckw = my_strndup(begw, endw - begw);
1216
1217 conv = find_sample_conv(begw, endw - begw);
1218 if (!conv) {
1219 /* Unknown converter method */
1220 memprintf(err, "ACL keyword '%s' : unknown conv method '%s'.",
1221 expr->kw, ckw);
1222 goto out_free_expr;
1223 }
1224
1225 arg = endw;
1226 if (*arg == '(') {
1227 /* look for the end of this term */
1228 while (*arg && *arg != ')')
1229 arg++;
1230 if (*arg != ')') {
1231 memprintf(err, "ACL keyword '%s' : syntax error: missing ')' after conv keyword '%s'.",
1232 expr->kw, ckw);
1233 goto out_free_expr;
1234 }
1235 }
1236
1237 if (conv->in_type >= SMP_TYPES || conv->out_type >= SMP_TYPES) {
1238 memprintf(err, "ACL keyword '%s' : returns type of conv method '%s' is unknown.",
1239 expr->kw, ckw);
1240 goto out_free_expr;
1241 }
1242
1243 /* If impossible type conversion */
1244 if (!sample_casts[prev_type][conv->in_type]) {
1245 memprintf(err, "ACL keyword '%s' : conv method '%s' cannot be applied.",
1246 expr->kw, ckw);
1247 goto out_free_expr;
1248 }
1249
1250 prev_type = conv->out_type;
1251 conv_expr = calloc(1, sizeof(struct sample_conv_expr));
1252 if (!conv_expr)
1253 goto out_free_expr;
1254
1255 LIST_ADDQ(&(expr->smp->conv_exprs), &(conv_expr->list));
1256 conv_expr->conv = conv;
1257
1258 if (arg != endw) {
1259 char *err_msg = NULL;
1260 int err_arg;
1261
1262 if (!conv->arg_mask) {
1263 memprintf(err, "ACL keyword '%s' : conv method '%s' does not support any args.",
1264 expr->kw, ckw);
1265 goto out_free_expr;
1266 }
1267
1268 al->kw = expr->smp->fetch->kw;
1269 al->conv = conv_expr->conv->kw;
1270 if (make_arg_list(endw + 1, arg - endw - 1, conv->arg_mask, &conv_expr->arg_p, &err_msg, NULL, &err_arg, al) < 0) {
1271 memprintf(err, "ACL keyword '%s' : invalid arg %d in conv method '%s' : %s.",
1272 expr->kw, err_arg+1, ckw, err_msg);
1273 free(err_msg);
1274 goto out_free_expr;
1275 }
1276
1277 if (!conv_expr->arg_p)
1278 conv_expr->arg_p = empty_arg_list;
1279
1280 if (conv->val_args && !conv->val_args(conv_expr->arg_p, &err_msg)) {
1281 memprintf(err, "ACL keyword '%s' : invalid args in conv method '%s' : %s.",
1282 expr->kw, ckw, err_msg);
1283 free(err_msg);
1284 goto out_free_expr;
1285 }
1286 }
1287 else if (ARGM(conv->arg_mask)) {
1288 memprintf(err, "ACL keyword '%s' : missing args for conv method '%s'.",
1289 expr->kw, ckw);
1290 goto out_free_expr;
1291 }
1292 }
Willy Tarreau61612d42012-04-19 18:42:05 +02001293 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001294 }
1295
Willy Tarreau3c3dfd52013-11-04 18:09:12 +01001296 /* Additional check to protect against common mistakes */
Thierry FOURNIER348971e2013-11-21 10:50:10 +01001297 cur_type = smp_expr_output_type(expr->smp);
1298 if (expr->parse && cur_type != SMP_T_BOOL && !*args[1]) {
Willy Tarreau3c3dfd52013-11-04 18:09:12 +01001299 Warning("parsing acl keyword '%s' :\n"
1300 " no pattern to match against were provided, so this ACL will never match.\n"
1301 " If this is what you intended, please add '--' to get rid of this warning.\n"
1302 " If you intended to match only for existence, please use '-m found'.\n"
1303 " If you wanted to force an int to match as a bool, please use '-m bool'.\n"
1304 "\n",
1305 args[0]);
1306 }
1307
Willy Tarreaua84d3742007-05-07 00:36:48 +02001308 args++;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02001309
1310 /* check for options before patterns. Supported options are :
1311 * -i : ignore case for all patterns by default
1312 * -f : read patterns from those files
Willy Tarreau5adeda12013-03-31 22:13:34 +02001313 * -m : force matching method (must be used before -f)
Willy Tarreauc8d7c962007-06-17 08:20:33 +02001314 * -- : everything after this is not an option
1315 */
1316 patflags = 0;
1317 while (**args == '-') {
1318 if ((*args)[1] == 'i')
1319 patflags |= ACL_PAT_F_IGNORE_CASE;
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001320 else if ((*args)[1] == 'f') {
Willy Tarreaubef91e72013-03-31 23:14:46 +02001321 if (!expr->parse) {
Willy Tarreau9987ea92013-06-11 21:09:06 +02001322 memprintf(err, "matching method must be specified first (using '-m') when using a sample fetch of this type ('%s')", expr->kw);
Willy Tarreaubef91e72013-03-31 23:14:46 +02001323 goto out_free_expr;
1324 }
1325
Willy Tarreaud76a98a2013-03-31 18:34:33 +02001326 if (!acl_read_patterns_from_file(expr, args[1], patflags | ACL_PAT_F_FROM_FILE, err))
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001327 goto out_free_expr;
Willy Tarreau5adeda12013-03-31 22:13:34 +02001328 args++;
1329 }
1330 else if ((*args)[1] == 'm') {
1331 int idx;
1332
1333 if (!LIST_ISEMPTY(&expr->patterns) || !eb_is_empty(&expr->pattern_tree)) {
1334 memprintf(err, "'-m' must only be specified before patterns and files in parsing ACL expression");
1335 goto out_free_expr;
1336 }
1337
1338 idx = acl_find_match_name(args[1]);
1339 if (idx < 0) {
1340 memprintf(err, "unknown matching method '%s' when parsing ACL expression", args[1]);
1341 goto out_free_expr;
1342 }
1343
1344 /* Note: -m found is always valid, bool/int are compatible, str/bin/reg/len are compatible */
1345 if (idx == ACL_MATCH_FOUND || /* -m found */
1346 ((idx == ACL_MATCH_BOOL || idx == ACL_MATCH_INT) && /* -m bool/int */
Thierry FOURNIER348971e2013-11-21 10:50:10 +01001347 (cur_type == SMP_T_BOOL ||
1348 cur_type == SMP_T_UINT ||
1349 cur_type == SMP_T_SINT)) ||
Willy Tarreau5adeda12013-03-31 22:13:34 +02001350 (idx == ACL_MATCH_IP && /* -m ip */
Thierry FOURNIER348971e2013-11-21 10:50:10 +01001351 (cur_type == SMP_T_IPV4 ||
1352 cur_type == SMP_T_IPV6)) ||
Willy Tarreau5adeda12013-03-31 22:13:34 +02001353 ((idx == ACL_MATCH_BIN || idx == ACL_MATCH_LEN || idx == ACL_MATCH_STR ||
1354 idx == ACL_MATCH_BEG || idx == ACL_MATCH_SUB || idx == ACL_MATCH_DIR ||
1355 idx == ACL_MATCH_DOM || idx == ACL_MATCH_END || idx == ACL_MATCH_REG) && /* strings */
Thierry FOURNIER348971e2013-11-21 10:50:10 +01001356 (cur_type == SMP_T_STR ||
1357 cur_type == SMP_T_BIN ||
1358 cur_type == SMP_T_CSTR ||
1359 cur_type == SMP_T_CBIN))) {
Willy Tarreau5adeda12013-03-31 22:13:34 +02001360 expr->parse = acl_parse_fcts[idx];
1361 expr->match = acl_match_fcts[idx];
1362 }
1363 else {
Willy Tarreau93fddf12013-03-31 22:59:32 +02001364 memprintf(err, "matching method '%s' cannot be used with fetch keyword '%s'", args[1], expr->kw);
Willy Tarreau5adeda12013-03-31 22:13:34 +02001365 goto out_free_expr;
1366 }
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001367 args++;
1368 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +02001369 else if ((*args)[1] == '-') {
1370 args++;
1371 break;
1372 }
1373 else
1374 break;
1375 args++;
1376 }
1377
Willy Tarreaubef91e72013-03-31 23:14:46 +02001378 if (!expr->parse) {
Willy Tarreau9987ea92013-06-11 21:09:06 +02001379 memprintf(err, "matching method must be specified first (using '-m') when using a sample fetch of this type ('%s')", expr->kw);
Willy Tarreaubef91e72013-03-31 23:14:46 +02001380 goto out_free_expr;
1381 }
1382
Willy Tarreauc8d7c962007-06-17 08:20:33 +02001383 /* now parse all patterns */
Willy Tarreauae8b7962007-06-09 23:10:04 +02001384 opaque = 0;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001385 while (**args) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02001386 int ret;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001387 pattern = (struct acl_pattern *)calloc(1, sizeof(*pattern));
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001388 if (!pattern) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001389 memprintf(err, "out of memory when parsing ACL pattern");
Willy Tarreaua84d3742007-05-07 00:36:48 +02001390 goto out_free_expr;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001391 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +02001392 pattern->flags = patflags;
1393
Willy Tarreauc92ddbc2012-04-27 22:10:57 +02001394 pattern->type = SMP_TYPES; /* unspecified type */
Willy Tarreaud76a98a2013-03-31 18:34:33 +02001395 ret = expr->parse(args, pattern, &opaque, err);
Willy Tarreau7dcb6482012-04-27 17:52:25 +02001396 if (!ret)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001397 goto out_free_pattern;
Willy Tarreau7dcb6482012-04-27 17:52:25 +02001398
Willy Tarreaua84d3742007-05-07 00:36:48 +02001399 LIST_ADDQ(&expr->patterns, &pattern->list);
Willy Tarreauae8b7962007-06-09 23:10:04 +02001400 args += ret;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001401 }
1402
1403 return expr;
1404
1405 out_free_pattern:
1406 free_pattern(pattern);
1407 out_free_expr:
1408 prune_acl_expr(expr);
1409 free(expr);
Thierry FOURNIER348971e2013-11-21 10:50:10 +01001410 free(ckw);
Willy Tarreaua84d3742007-05-07 00:36:48 +02001411 out_return:
1412 return NULL;
1413}
1414
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02001415/* Purge everything in the acl <acl>, then return <acl>. */
1416struct acl *prune_acl(struct acl *acl) {
1417
1418 struct acl_expr *expr, *exprb;
1419
1420 free(acl->name);
1421
1422 list_for_each_entry_safe(expr, exprb, &acl->expr, list) {
1423 LIST_DEL(&expr->list);
1424 prune_acl_expr(expr);
1425 free(expr);
1426 }
1427
1428 return acl;
1429}
1430
Willy Tarreaua84d3742007-05-07 00:36:48 +02001431/* Parse an ACL with the name starting at <args>[0], and with a list of already
1432 * known ACLs in <acl>. If the ACL was not in the list, it will be added.
Willy Tarreau2a56c5e2010-03-15 16:13:29 +01001433 * A pointer to that ACL is returned. If the ACL has an empty name, then it's
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001434 * an anonymous one and it won't be merged with any other one. If <err> is not
1435 * NULL, it will be filled with an appropriate error. This pointer must be
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001436 * freeable or NULL. <al> is the arg_list serving as a head for unresolved
1437 * dependencies.
Willy Tarreaua84d3742007-05-07 00:36:48 +02001438 *
1439 * args syntax: <aclname> <acl_expr>
1440 */
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001441struct acl *parse_acl(const char **args, struct list *known_acl, char **err, struct arg_list *al)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001442{
1443 __label__ out_return, out_free_acl_expr, out_free_name;
1444 struct acl *cur_acl;
1445 struct acl_expr *acl_expr;
1446 char *name;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001447 const char *pos;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001448
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001449 if (**args && (pos = invalid_char(*args))) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001450 memprintf(err, "invalid character in ACL name : '%c'", *pos);
Willy Tarreau2e74c3f2007-12-02 18:45:09 +01001451 goto out_return;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001452 }
Willy Tarreau2e74c3f2007-12-02 18:45:09 +01001453
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001454 acl_expr = parse_acl_expr(args + 1, err, al);
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001455 if (!acl_expr) {
1456 /* parse_acl_expr will have filled <err> here */
Willy Tarreaua84d3742007-05-07 00:36:48 +02001457 goto out_return;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001458 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001459
Willy Tarreau404e8ab2009-07-26 19:40:40 +02001460 /* Check for args beginning with an opening parenthesis just after the
1461 * subject, as this is almost certainly a typo. Right now we can only
1462 * emit a warning, so let's do so.
1463 */
Krzysztof Piotr Oledzki4cdd8312009-10-05 00:23:35 +02001464 if (!strchr(args[1], '(') && *args[2] == '(')
Willy Tarreau404e8ab2009-07-26 19:40:40 +02001465 Warning("parsing acl '%s' :\n"
1466 " matching '%s' for pattern '%s' is likely a mistake and probably\n"
1467 " not what you want. Maybe you need to remove the extraneous space before '('.\n"
1468 " If you are really sure this is not an error, please insert '--' between the\n"
1469 " match and the pattern to make this warning message disappear.\n",
1470 args[0], args[1], args[2]);
1471
Willy Tarreau2a56c5e2010-03-15 16:13:29 +01001472 if (*args[0])
1473 cur_acl = find_acl_by_name(args[0], known_acl);
1474 else
1475 cur_acl = NULL;
1476
Willy Tarreaua84d3742007-05-07 00:36:48 +02001477 if (!cur_acl) {
1478 name = strdup(args[0]);
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001479 if (!name) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001480 memprintf(err, "out of memory when parsing ACL");
Willy Tarreaua84d3742007-05-07 00:36:48 +02001481 goto out_free_acl_expr;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001482 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001483 cur_acl = (struct acl *)calloc(1, sizeof(*cur_acl));
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001484 if (cur_acl == NULL) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001485 memprintf(err, "out of memory when parsing ACL");
Willy Tarreaua84d3742007-05-07 00:36:48 +02001486 goto out_free_name;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001487 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001488
1489 LIST_INIT(&cur_acl->expr);
1490 LIST_ADDQ(known_acl, &cur_acl->list);
1491 cur_acl->name = name;
1492 }
1493
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001494 /* We want to know what features the ACL needs (typically HTTP parsing),
1495 * and where it may be used. If an ACL relies on multiple matches, it is
1496 * OK if at least one of them may match in the context where it is used.
1497 */
Thierry FOURNIER348971e2013-11-21 10:50:10 +01001498 cur_acl->use |= acl_expr->smp->fetch->use;
1499 cur_acl->val |= acl_expr->smp->fetch->val;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001500 LIST_ADDQ(&cur_acl->expr, &acl_expr->list);
1501 return cur_acl;
1502
1503 out_free_name:
1504 free(name);
1505 out_free_acl_expr:
1506 prune_acl_expr(acl_expr);
1507 free(acl_expr);
1508 out_return:
1509 return NULL;
1510}
1511
Willy Tarreau16fbe822007-06-17 11:54:31 +02001512/* Some useful ACLs provided by default. Only those used are allocated. */
1513
1514const struct {
1515 const char *name;
1516 const char *expr[4]; /* put enough for longest expression */
1517} default_acl_list[] = {
Willy Tarreau58393e12008-07-20 10:39:22 +02001518 { .name = "TRUE", .expr = {"always_true",""}},
1519 { .name = "FALSE", .expr = {"always_false",""}},
Willy Tarreau16fbe822007-06-17 11:54:31 +02001520 { .name = "LOCALHOST", .expr = {"src","127.0.0.1/8",""}},
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001521 { .name = "HTTP", .expr = {"req_proto_http",""}},
Willy Tarreau16fbe822007-06-17 11:54:31 +02001522 { .name = "HTTP_1.0", .expr = {"req_ver","1.0",""}},
1523 { .name = "HTTP_1.1", .expr = {"req_ver","1.1",""}},
1524 { .name = "METH_CONNECT", .expr = {"method","CONNECT",""}},
1525 { .name = "METH_GET", .expr = {"method","GET","HEAD",""}},
1526 { .name = "METH_HEAD", .expr = {"method","HEAD",""}},
1527 { .name = "METH_OPTIONS", .expr = {"method","OPTIONS",""}},
1528 { .name = "METH_POST", .expr = {"method","POST",""}},
1529 { .name = "METH_TRACE", .expr = {"method","TRACE",""}},
1530 { .name = "HTTP_URL_ABS", .expr = {"url_reg","^[^/:]*://",""}},
1531 { .name = "HTTP_URL_SLASH", .expr = {"url_beg","/",""}},
1532 { .name = "HTTP_URL_STAR", .expr = {"url","*",""}},
1533 { .name = "HTTP_CONTENT", .expr = {"hdr_val(content-length)","gt","0",""}},
Emeric Brunbede3d02009-06-30 17:54:00 +02001534 { .name = "RDP_COOKIE", .expr = {"req_rdp_cookie_cnt","gt","0",""}},
Willy Tarreauc6317702008-07-20 09:29:50 +02001535 { .name = "REQ_CONTENT", .expr = {"req_len","gt","0",""}},
Willy Tarreaub6fb4202008-07-20 11:18:28 +02001536 { .name = "WAIT_END", .expr = {"wait_end",""}},
Willy Tarreau16fbe822007-06-17 11:54:31 +02001537 { .name = NULL, .expr = {""}}
1538};
1539
1540/* Find a default ACL from the default_acl list, compile it and return it.
1541 * If the ACL is not found, NULL is returned. In theory, it cannot fail,
1542 * except when default ACLs are broken, in which case it will return NULL.
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001543 * If <known_acl> is not NULL, the ACL will be queued at its tail. If <err> is
1544 * not NULL, it will be filled with an error message if an error occurs. This
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001545 * pointer must be freeable or NULL. <al> is an arg_list serving as a list head
1546 * to report missing dependencies.
Willy Tarreau16fbe822007-06-17 11:54:31 +02001547 */
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001548static struct acl *find_acl_default(const char *acl_name, struct list *known_acl,
1549 char **err, struct arg_list *al)
Willy Tarreau16fbe822007-06-17 11:54:31 +02001550{
1551 __label__ out_return, out_free_acl_expr, out_free_name;
1552 struct acl *cur_acl;
1553 struct acl_expr *acl_expr;
1554 char *name;
1555 int index;
1556
1557 for (index = 0; default_acl_list[index].name != NULL; index++) {
1558 if (strcmp(acl_name, default_acl_list[index].name) == 0)
1559 break;
1560 }
1561
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001562 if (default_acl_list[index].name == NULL) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001563 memprintf(err, "no such ACL : '%s'", acl_name);
Willy Tarreau16fbe822007-06-17 11:54:31 +02001564 return NULL;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001565 }
Willy Tarreau16fbe822007-06-17 11:54:31 +02001566
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001567 acl_expr = parse_acl_expr((const char **)default_acl_list[index].expr, err, al);
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001568 if (!acl_expr) {
1569 /* parse_acl_expr must have filled err here */
Willy Tarreau16fbe822007-06-17 11:54:31 +02001570 goto out_return;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001571 }
Willy Tarreau16fbe822007-06-17 11:54:31 +02001572
1573 name = strdup(acl_name);
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001574 if (!name) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001575 memprintf(err, "out of memory when building default ACL '%s'", acl_name);
Willy Tarreau16fbe822007-06-17 11:54:31 +02001576 goto out_free_acl_expr;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001577 }
1578
Willy Tarreau16fbe822007-06-17 11:54:31 +02001579 cur_acl = (struct acl *)calloc(1, sizeof(*cur_acl));
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001580 if (cur_acl == NULL) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001581 memprintf(err, "out of memory when building default ACL '%s'", acl_name);
Willy Tarreau16fbe822007-06-17 11:54:31 +02001582 goto out_free_name;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001583 }
Willy Tarreau16fbe822007-06-17 11:54:31 +02001584
1585 cur_acl->name = name;
Thierry FOURNIER348971e2013-11-21 10:50:10 +01001586 cur_acl->use |= acl_expr->smp->fetch->use;
1587 cur_acl->val |= acl_expr->smp->fetch->val;
Willy Tarreau16fbe822007-06-17 11:54:31 +02001588 LIST_INIT(&cur_acl->expr);
1589 LIST_ADDQ(&cur_acl->expr, &acl_expr->list);
1590 if (known_acl)
1591 LIST_ADDQ(known_acl, &cur_acl->list);
1592
1593 return cur_acl;
1594
1595 out_free_name:
1596 free(name);
1597 out_free_acl_expr:
1598 prune_acl_expr(acl_expr);
1599 free(acl_expr);
1600 out_return:
1601 return NULL;
1602}
Willy Tarreaua84d3742007-05-07 00:36:48 +02001603
1604/* Purge everything in the acl_cond <cond>, then return <cond>. */
1605struct acl_cond *prune_acl_cond(struct acl_cond *cond)
1606{
1607 struct acl_term_suite *suite, *tmp_suite;
1608 struct acl_term *term, *tmp_term;
1609
1610 /* iterate through all term suites and free all terms and all suites */
1611 list_for_each_entry_safe(suite, tmp_suite, &cond->suites, list) {
1612 list_for_each_entry_safe(term, tmp_term, &suite->terms, list)
1613 free(term);
1614 free(suite);
1615 }
1616 return cond;
1617}
1618
1619/* Parse an ACL condition starting at <args>[0], relying on a list of already
1620 * known ACLs passed in <known_acl>. The new condition is returned (or NULL in
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001621 * case of low memory). Supports multiple conditions separated by "or". If
1622 * <err> is not NULL, it will be filled with a pointer to an error message in
1623 * case of error, that the caller is responsible for freeing. The initial
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001624 * location must either be freeable or NULL. The list <al> serves as a list head
1625 * for unresolved dependencies.
Willy Tarreaua84d3742007-05-07 00:36:48 +02001626 */
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001627struct acl_cond *parse_acl_cond(const char **args, struct list *known_acl,
1628 int pol, char **err, struct arg_list *al)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001629{
1630 __label__ out_return, out_free_suite, out_free_term;
Willy Tarreau74b98a82007-06-16 19:35:18 +02001631 int arg, neg;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001632 const char *word;
1633 struct acl *cur_acl;
1634 struct acl_term *cur_term;
1635 struct acl_term_suite *cur_suite;
1636 struct acl_cond *cond;
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001637 unsigned int suite_val;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001638
1639 cond = (struct acl_cond *)calloc(1, sizeof(*cond));
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001640 if (cond == NULL) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001641 memprintf(err, "out of memory when parsing condition");
Willy Tarreaua84d3742007-05-07 00:36:48 +02001642 goto out_return;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001643 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001644
1645 LIST_INIT(&cond->list);
1646 LIST_INIT(&cond->suites);
1647 cond->pol = pol;
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001648 cond->val = 0;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001649
1650 cur_suite = NULL;
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001651 suite_val = ~0U;
Willy Tarreau74b98a82007-06-16 19:35:18 +02001652 neg = 0;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001653 for (arg = 0; *args[arg]; arg++) {
1654 word = args[arg];
1655
1656 /* remove as many exclamation marks as we can */
1657 while (*word == '!') {
1658 neg = !neg;
1659 word++;
1660 }
1661
1662 /* an empty word is allowed because we cannot force the user to
1663 * always think about not leaving exclamation marks alone.
1664 */
1665 if (!*word)
1666 continue;
1667
Willy Tarreau16fbe822007-06-17 11:54:31 +02001668 if (strcasecmp(word, "or") == 0 || strcmp(word, "||") == 0) {
Willy Tarreaua84d3742007-05-07 00:36:48 +02001669 /* new term suite */
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001670 cond->val |= suite_val;
1671 suite_val = ~0U;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001672 cur_suite = NULL;
1673 neg = 0;
1674 continue;
1675 }
1676
Willy Tarreau95fa4692010-02-01 13:05:50 +01001677 if (strcmp(word, "{") == 0) {
1678 /* we may have a complete ACL expression between two braces,
1679 * find the last one.
1680 */
1681 int arg_end = arg + 1;
1682 const char **args_new;
1683
1684 while (*args[arg_end] && strcmp(args[arg_end], "}") != 0)
1685 arg_end++;
1686
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001687 if (!*args[arg_end]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001688 memprintf(err, "missing closing '}' in condition");
Willy Tarreau95fa4692010-02-01 13:05:50 +01001689 goto out_free_suite;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001690 }
Willy Tarreau95fa4692010-02-01 13:05:50 +01001691
1692 args_new = calloc(1, (arg_end - arg + 1) * sizeof(*args_new));
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001693 if (!args_new) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001694 memprintf(err, "out of memory when parsing condition");
Willy Tarreau95fa4692010-02-01 13:05:50 +01001695 goto out_free_suite;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001696 }
Willy Tarreau95fa4692010-02-01 13:05:50 +01001697
Willy Tarreau2a56c5e2010-03-15 16:13:29 +01001698 args_new[0] = "";
Willy Tarreau95fa4692010-02-01 13:05:50 +01001699 memcpy(args_new + 1, args + arg + 1, (arg_end - arg) * sizeof(*args_new));
1700 args_new[arg_end - arg] = "";
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001701 cur_acl = parse_acl(args_new, known_acl, err, al);
Willy Tarreau95fa4692010-02-01 13:05:50 +01001702 free(args_new);
1703
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001704 if (!cur_acl) {
1705 /* note that parse_acl() must have filled <err> here */
Willy Tarreau16fbe822007-06-17 11:54:31 +02001706 goto out_free_suite;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001707 }
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001708 word = args[arg + 1];
Willy Tarreau95fa4692010-02-01 13:05:50 +01001709 arg = arg_end;
1710 }
1711 else {
1712 /* search for <word> in the known ACL names. If we do not find
1713 * it, let's look for it in the default ACLs, and if found, add
1714 * it to the list of ACLs of this proxy. This makes it possible
1715 * to override them.
1716 */
1717 cur_acl = find_acl_by_name(word, known_acl);
1718 if (cur_acl == NULL) {
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001719 cur_acl = find_acl_default(word, known_acl, err, al);
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001720 if (cur_acl == NULL) {
1721 /* note that find_acl_default() must have filled <err> here */
Willy Tarreau95fa4692010-02-01 13:05:50 +01001722 goto out_free_suite;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001723 }
Willy Tarreau95fa4692010-02-01 13:05:50 +01001724 }
Willy Tarreau16fbe822007-06-17 11:54:31 +02001725 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001726
1727 cur_term = (struct acl_term *)calloc(1, sizeof(*cur_term));
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001728 if (cur_term == NULL) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001729 memprintf(err, "out of memory when parsing condition");
Willy Tarreaua84d3742007-05-07 00:36:48 +02001730 goto out_free_suite;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001731 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001732
1733 cur_term->acl = cur_acl;
1734 cur_term->neg = neg;
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001735
1736 /* Here it is a bit complex. The acl_term_suite is a conjunction
1737 * of many terms. It may only be used if all of its terms are
1738 * usable at the same time. So the suite's validity domain is an
1739 * AND between all ACL keywords' ones. But, the global condition
1740 * is valid if at least one term suite is OK. So it's an OR between
1741 * all of their validity domains. We could emit a warning as soon
1742 * as suite_val is null because it means that the last ACL is not
1743 * compatible with the previous ones. Let's remain simple for now.
1744 */
1745 cond->use |= cur_acl->use;
1746 suite_val &= cur_acl->val;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001747
1748 if (!cur_suite) {
1749 cur_suite = (struct acl_term_suite *)calloc(1, sizeof(*cur_suite));
Willy Tarreauf678b7f2013-01-24 00:25:39 +01001750 if (cur_suite == NULL) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001751 memprintf(err, "out of memory when parsing condition");
Willy Tarreaua84d3742007-05-07 00:36:48 +02001752 goto out_free_term;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001753 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001754 LIST_INIT(&cur_suite->terms);
1755 LIST_ADDQ(&cond->suites, &cur_suite->list);
1756 }
1757 LIST_ADDQ(&cur_suite->terms, &cur_term->list);
Willy Tarreau74b98a82007-06-16 19:35:18 +02001758 neg = 0;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001759 }
1760
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001761 cond->val |= suite_val;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001762 return cond;
1763
1764 out_free_term:
1765 free(cur_term);
1766 out_free_suite:
1767 prune_acl_cond(cond);
1768 free(cond);
1769 out_return:
1770 return NULL;
1771}
1772
Willy Tarreau2bbba412010-01-28 16:48:33 +01001773/* Builds an ACL condition starting at the if/unless keyword. The complete
1774 * condition is returned. NULL is returned in case of error or if the first
1775 * word is neither "if" nor "unless". It automatically sets the file name and
Willy Tarreau25320b22013-03-24 07:22:08 +01001776 * the line number in the condition for better error reporting, and sets the
1777 * HTTP intiailization requirements in the proxy. If <err> is not NULL, it will
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001778 * be filled with a pointer to an error message in case of error, that the
1779 * caller is responsible for freeing. The initial location must either be
1780 * freeable or NULL.
Willy Tarreau2bbba412010-01-28 16:48:33 +01001781 */
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001782struct acl_cond *build_acl_cond(const char *file, int line, struct proxy *px, const char **args, char **err)
Willy Tarreau2bbba412010-01-28 16:48:33 +01001783{
1784 int pol = ACL_COND_NONE;
1785 struct acl_cond *cond = NULL;
1786
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001787 if (err)
1788 *err = NULL;
1789
Willy Tarreau2bbba412010-01-28 16:48:33 +01001790 if (!strcmp(*args, "if")) {
1791 pol = ACL_COND_IF;
1792 args++;
1793 }
1794 else if (!strcmp(*args, "unless")) {
1795 pol = ACL_COND_UNLESS;
1796 args++;
1797 }
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001798 else {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001799 memprintf(err, "conditions must start with either 'if' or 'unless'");
Willy Tarreau2bbba412010-01-28 16:48:33 +01001800 return NULL;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001801 }
Willy Tarreau2bbba412010-01-28 16:48:33 +01001802
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001803 cond = parse_acl_cond(args, &px->acl, pol, err, &px->conf.args);
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001804 if (!cond) {
1805 /* note that parse_acl_cond must have filled <err> here */
Willy Tarreau2bbba412010-01-28 16:48:33 +01001806 return NULL;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001807 }
Willy Tarreau2bbba412010-01-28 16:48:33 +01001808
1809 cond->file = file;
1810 cond->line = line;
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001811 px->http_needed |= !!(cond->use & SMP_USE_HTTP_ANY);
Willy Tarreau2bbba412010-01-28 16:48:33 +01001812 return cond;
1813}
1814
Willy Tarreau11382812008-07-09 16:18:21 +02001815/* Execute condition <cond> and return either ACL_PAT_FAIL, ACL_PAT_MISS or
Willy Tarreaub6866442008-07-14 23:54:42 +02001816 * ACL_PAT_PASS depending on the test results. ACL_PAT_MISS may only be
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001817 * returned if <opt> does not contain SMP_OPT_FINAL, indicating that incomplete
Willy Tarreau7a777ed2012-04-26 11:44:02 +02001818 * data is being examined. The function automatically sets SMP_OPT_ITERATE.
Willy Tarreaub6866442008-07-14 23:54:42 +02001819 * This function only computes the condition, it does not apply the polarity
1820 * required by IF/UNLESS, it's up to the caller to do this using something like
1821 * this :
Willy Tarreau11382812008-07-09 16:18:21 +02001822 *
1823 * res = acl_pass(res);
Willy Tarreaub6866442008-07-14 23:54:42 +02001824 * if (res == ACL_PAT_MISS)
1825 * return 0;
Willy Tarreau11382812008-07-09 16:18:21 +02001826 * if (cond->pol == ACL_COND_UNLESS)
1827 * res = !res;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001828 */
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001829int acl_exec_cond(struct acl_cond *cond, struct proxy *px, struct session *l4, void *l7, unsigned int opt)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001830{
1831 __label__ fetch_next;
1832 struct acl_term_suite *suite;
1833 struct acl_term *term;
1834 struct acl_expr *expr;
1835 struct acl *acl;
1836 struct acl_pattern *pattern;
Willy Tarreau37406352012-04-23 16:16:37 +02001837 struct sample smp;
Willy Tarreau11382812008-07-09 16:18:21 +02001838 int acl_res, suite_res, cond_res;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001839
Willy Tarreau7a777ed2012-04-26 11:44:02 +02001840 /* ACLs are iterated over all values, so let's always set the flag to
1841 * indicate this to the fetch functions.
1842 */
1843 opt |= SMP_OPT_ITERATE;
1844
Willy Tarreau11382812008-07-09 16:18:21 +02001845 /* We're doing a logical OR between conditions so we initialize to FAIL.
1846 * The MISS status is propagated down from the suites.
1847 */
Willy Tarreaua84d3742007-05-07 00:36:48 +02001848 cond_res = ACL_PAT_FAIL;
1849 list_for_each_entry(suite, &cond->suites, list) {
Willy Tarreau11382812008-07-09 16:18:21 +02001850 /* Evaluate condition suite <suite>. We stop at the first term
1851 * which returns ACL_PAT_FAIL. The MISS status is still propagated
1852 * in case of uncertainty in the result.
Willy Tarreaua84d3742007-05-07 00:36:48 +02001853 */
1854
1855 /* we're doing a logical AND between terms, so we must set the
1856 * initial value to PASS.
1857 */
1858 suite_res = ACL_PAT_PASS;
1859 list_for_each_entry(term, &suite->terms, list) {
1860 acl = term->acl;
1861
1862 /* FIXME: use cache !
1863 * check acl->cache_idx for this.
1864 */
1865
1866 /* ACL result not cached. Let's scan all the expressions
1867 * and use the first one to match.
1868 */
1869 acl_res = ACL_PAT_FAIL;
1870 list_for_each_entry(expr, &acl->expr, list) {
Willy Tarreaud41f8d82007-06-10 10:06:18 +02001871 /* we need to reset context and flags */
Willy Tarreau37406352012-04-23 16:16:37 +02001872 memset(&smp, 0, sizeof(smp));
Willy Tarreaua84d3742007-05-07 00:36:48 +02001873 fetch_next:
Thierry FOURNIER348971e2013-11-21 10:50:10 +01001874 if (!sample_process(px, l4, l7, opt, expr->smp, &smp)) {
Willy Tarreaub6866442008-07-14 23:54:42 +02001875 /* maybe we could not fetch because of missing data */
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001876 if (smp.flags & SMP_F_MAY_CHANGE && !(opt & SMP_OPT_FINAL))
Willy Tarreaub6866442008-07-14 23:54:42 +02001877 acl_res |= ACL_PAT_MISS;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001878 continue;
Willy Tarreaub6866442008-07-14 23:54:42 +02001879 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001880
Willy Tarreau24b2c762013-06-12 21:50:19 +02001881 if (expr->match == acl_match_nothing) {
Willy Tarreau197e10a2012-04-23 19:18:42 +02001882 if (smp.data.uint)
Willy Tarreaua79534f2008-07-20 10:13:37 +02001883 acl_res |= ACL_PAT_PASS;
1884 else
1885 acl_res |= ACL_PAT_FAIL;
1886 }
Willy Tarreau5adeda12013-03-31 22:13:34 +02001887 else if (!expr->match) {
1888 /* just check for existence */
1889 acl_res |= ACL_PAT_PASS;
1890 }
Willy Tarreaua79534f2008-07-20 10:13:37 +02001891 else {
Willy Tarreau020534d2010-05-16 21:45:45 +02001892 if (!eb_is_empty(&expr->pattern_tree)) {
Willy Tarreauc4262962010-05-10 23:42:40 +02001893 /* a tree is present, let's check what type it is */
Willy Tarreaud76a98a2013-03-31 18:34:33 +02001894 if (expr->match == acl_match_str)
Willy Tarreau37406352012-04-23 16:16:37 +02001895 acl_res |= acl_lookup_str(&smp, expr) ? ACL_PAT_PASS : ACL_PAT_FAIL;
Willy Tarreaud76a98a2013-03-31 18:34:33 +02001896 else if (expr->match == acl_match_ip)
Willy Tarreau37406352012-04-23 16:16:37 +02001897 acl_res |= acl_lookup_ip(&smp, expr) ? ACL_PAT_PASS : ACL_PAT_FAIL;
Willy Tarreauc4262962010-05-10 23:42:40 +02001898 }
1899
Willy Tarreaua79534f2008-07-20 10:13:37 +02001900 /* call the match() function for all tests on this value */
1901 list_for_each_entry(pattern, &expr->patterns, list) {
Willy Tarreaua79534f2008-07-20 10:13:37 +02001902 if (acl_res == ACL_PAT_PASS)
1903 break;
Willy Tarreaud76a98a2013-03-31 18:34:33 +02001904 acl_res |= expr->match(&smp, pattern);
Willy Tarreaua79534f2008-07-20 10:13:37 +02001905 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001906 }
1907 /*
Willy Tarreau11382812008-07-09 16:18:21 +02001908 * OK now acl_res holds the result of this expression
1909 * as one of ACL_PAT_FAIL, ACL_PAT_MISS or ACL_PAT_PASS.
Willy Tarreaua84d3742007-05-07 00:36:48 +02001910 *
Willy Tarreau11382812008-07-09 16:18:21 +02001911 * Then if (!MISS) we can cache the result, and put
Willy Tarreau37406352012-04-23 16:16:37 +02001912 * (smp.flags & SMP_F_VOLATILE) in the cache flags.
Willy Tarreaua84d3742007-05-07 00:36:48 +02001913 *
1914 * FIXME: implement cache.
1915 *
1916 */
1917
Willy Tarreau11382812008-07-09 16:18:21 +02001918 /* we're ORing these terms, so a single PASS is enough */
1919 if (acl_res == ACL_PAT_PASS)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001920 break;
1921
Willy Tarreau37406352012-04-23 16:16:37 +02001922 if (smp.flags & SMP_F_NOT_LAST)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001923 goto fetch_next;
Willy Tarreaub6866442008-07-14 23:54:42 +02001924
1925 /* sometimes we know the fetched data is subject to change
1926 * later and give another chance for a new match (eg: request
1927 * size, time, ...)
1928 */
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001929 if (smp.flags & SMP_F_MAY_CHANGE && !(opt & SMP_OPT_FINAL))
Willy Tarreaub6866442008-07-14 23:54:42 +02001930 acl_res |= ACL_PAT_MISS;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001931 }
1932 /*
1933 * Here we have the result of an ACL (cached or not).
1934 * ACLs are combined, negated or not, to form conditions.
1935 */
1936
Willy Tarreaua84d3742007-05-07 00:36:48 +02001937 if (term->neg)
Willy Tarreau11382812008-07-09 16:18:21 +02001938 acl_res = acl_neg(acl_res);
Willy Tarreaua84d3742007-05-07 00:36:48 +02001939
1940 suite_res &= acl_res;
Willy Tarreau11382812008-07-09 16:18:21 +02001941
Willy Tarreau79c412b2013-10-30 19:30:32 +01001942 /* we're ANDing these terms, so a single FAIL or MISS is enough */
1943 if (suite_res != ACL_PAT_PASS)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001944 break;
1945 }
1946 cond_res |= suite_res;
Willy Tarreau11382812008-07-09 16:18:21 +02001947
1948 /* we're ORing these terms, so a single PASS is enough */
1949 if (cond_res == ACL_PAT_PASS)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001950 break;
1951 }
Willy Tarreau11382812008-07-09 16:18:21 +02001952 return cond_res;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001953}
1954
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001955/* Returns a pointer to the first ACL conflicting with usage at place <where>
1956 * which is one of the SMP_VAL_* bits indicating a check place, or NULL if
1957 * no conflict is found. Only full conflicts are detected (ACL is not usable).
1958 * Use the next function to check for useless keywords.
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001959 */
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001960const struct acl *acl_cond_conflicts(const struct acl_cond *cond, unsigned int where)
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001961{
1962 struct acl_term_suite *suite;
1963 struct acl_term *term;
1964 struct acl *acl;
1965
1966 list_for_each_entry(suite, &cond->suites, list) {
1967 list_for_each_entry(term, &suite->terms, list) {
1968 acl = term->acl;
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001969 if (!(acl->val & where))
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001970 return acl;
1971 }
1972 }
1973 return NULL;
1974}
1975
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001976/* Returns a pointer to the first ACL and its first keyword to conflict with
1977 * usage at place <where> which is one of the SMP_VAL_* bits indicating a check
1978 * place. Returns true if a conflict is found, with <acl> and <kw> set (if non
1979 * null), or false if not conflict is found. The first useless keyword is
1980 * returned.
1981 */
Willy Tarreau93fddf12013-03-31 22:59:32 +02001982int acl_cond_kw_conflicts(const struct acl_cond *cond, unsigned int where, struct acl const **acl, char const **kw)
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001983{
1984 struct acl_term_suite *suite;
1985 struct acl_term *term;
1986 struct acl_expr *expr;
1987
1988 list_for_each_entry(suite, &cond->suites, list) {
1989 list_for_each_entry(term, &suite->terms, list) {
1990 list_for_each_entry(expr, &term->acl->expr, list) {
Thierry FOURNIER348971e2013-11-21 10:50:10 +01001991 if (!(expr->smp->fetch->val & where)) {
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001992 if (acl)
1993 *acl = term->acl;
1994 if (kw)
1995 *kw = expr->kw;
1996 return 1;
1997 }
1998 }
1999 }
2000 }
2001 return 0;
2002}
2003
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01002004/*
2005 * Find targets for userlist and groups in acl. Function returns the number
Willy Tarreaua4312fa2013-04-02 16:34:32 +02002006 * of errors or OK if everything is fine. It must be called only once sample
2007 * fetch arguments have been resolved (after smp_resolve_args()).
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01002008 */
Willy Tarreaua4312fa2013-04-02 16:34:32 +02002009int acl_find_targets(struct proxy *p)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01002010{
2011
2012 struct acl *acl;
2013 struct acl_expr *expr;
2014 struct acl_pattern *pattern;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01002015 int cfgerr = 0;
2016
2017 list_for_each_entry(acl, &p->acl, list) {
2018 list_for_each_entry(expr, &acl->expr, list) {
Willy Tarreaua4312fa2013-04-02 16:34:32 +02002019 if (!strcmp(expr->kw, "http_auth_group")) {
2020 /* Note: the ARGT_USR argument may only have been resolved earlier
2021 * by smp_resolve_args().
2022 */
Thierry FOURNIER348971e2013-11-21 10:50:10 +01002023 if (expr->smp->arg_p->unresolved) {
Willy Tarreaua4312fa2013-04-02 16:34:32 +02002024 Alert("Internal bug in proxy %s: %sacl %s %s() makes use of unresolved userlist '%s'. Please report this.\n",
Thierry FOURNIER348971e2013-11-21 10:50:10 +01002025 p->id, *acl->name ? "" : "anonymous ", acl->name, expr->kw, expr->smp->arg_p->data.str.str);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02002026 cfgerr++;
Willy Tarreau496aa012012-06-01 10:38:29 +02002027 continue;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01002028 }
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01002029
2030 if (LIST_ISEMPTY(&expr->patterns)) {
2031 Alert("proxy %s: acl %s %s(): no groups specified.\n",
Willy Tarreau93fddf12013-03-31 22:59:32 +02002032 p->id, acl->name, expr->kw);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01002033 cfgerr++;
2034 continue;
2035 }
2036
2037 list_for_each_entry(pattern, &expr->patterns, list) {
Willy Tarreau7d1df412012-11-23 23:47:36 +01002038 /* this keyword only has one argument */
Thierry FOURNIER348971e2013-11-21 10:50:10 +01002039 pattern->val.group_mask = auth_resolve_groups(expr->smp->arg_p->data.usr, pattern->ptr.str);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01002040
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01002041 if (!pattern->val.group_mask) {
Willy Tarreaua4312fa2013-04-02 16:34:32 +02002042 Alert("proxy %s: acl %s %s(): invalid group '%s'.\n",
2043 p->id, acl->name, expr->kw, pattern->ptr.str);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01002044 cfgerr++;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01002045 }
Willy Tarreaua4312fa2013-04-02 16:34:32 +02002046 free(pattern->ptr.str);
2047 pattern->ptr.str = NULL;
2048 pattern->len = 0;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01002049 }
2050 }
2051 }
2052 }
2053
2054 return cfgerr;
2055}
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02002056
Willy Tarreau8ed669b2013-01-11 15:49:37 +01002057/* initializes ACLs by resolving the sample fetch names they rely upon.
2058 * Returns 0 on success, otherwise an error.
2059 */
2060int init_acl()
2061{
2062 int err = 0;
2063 int index;
2064 const char *name;
2065 struct acl_kw_list *kwl;
2066 struct sample_fetch *smp;
2067
2068 list_for_each_entry(kwl, &acl_keywords.list, list) {
2069 for (index = 0; kwl->kw[index].kw != NULL; index++) {
2070 name = kwl->kw[index].fetch_kw;
2071 if (!name)
2072 name = kwl->kw[index].kw;
2073
2074 smp = find_sample_fetch(name, strlen(name));
2075 if (!smp) {
2076 Alert("Critical internal error: ACL keyword '%s' relies on sample fetch '%s' which was not registered!\n",
2077 kwl->kw[index].kw, name);
2078 err++;
2079 continue;
2080 }
2081 kwl->kw[index].smp = smp;
2082 }
2083 }
2084 return err;
2085}
Willy Tarreaud4c33c82013-01-07 21:59:07 +01002086
Willy Tarreaua84d3742007-05-07 00:36:48 +02002087/************************************************************************/
Willy Tarreaud4c33c82013-01-07 21:59:07 +01002088/* All supported sample and ACL keywords must be declared here. */
2089/************************************************************************/
2090
2091/* Note: must not be declared <const> as its list will be overwritten.
Willy Tarreau61612d42012-04-19 18:42:05 +02002092 * Please take care of keeping this list alphabetically sorted.
2093 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02002094static struct acl_kw_list acl_kws = {ILH, {
Willy Tarreaud4c33c82013-01-07 21:59:07 +01002095 { /* END */ },
Willy Tarreaua84d3742007-05-07 00:36:48 +02002096}};
2097
Willy Tarreaua84d3742007-05-07 00:36:48 +02002098__attribute__((constructor))
2099static void __acl_init(void)
2100{
Willy Tarreaua84d3742007-05-07 00:36:48 +02002101 acl_register_keywords(&acl_kws);
2102}
2103
2104
2105/*
2106 * Local variables:
2107 * c-indent-level: 8
2108 * c-basic-offset: 8
2109 * End:
2110 */