blob: d1c149f88fd8991e6c713abce2faadf8d01ff96c [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{
166 char old_char;
167 int ret;
168
Willy Tarreauf853c462012-04-23 18:53:56 +0200169 old_char = smp->data.str.str[smp->data.str.len];
170 smp->data.str.str[smp->data.str.len] = 0;
Willy Tarreauf3d25982007-05-08 22:45:09 +0200171
Hiroaki Nakamura70351322013-01-13 15:00:42 +0900172 if (regex_exec(pattern->ptr.reg, smp->data.str.str, smp->data.str.len) == 0)
Willy Tarreau11382812008-07-09 16:18:21 +0200173 ret = ACL_PAT_PASS;
Willy Tarreauf3d25982007-05-08 22:45:09 +0200174 else
Willy Tarreau11382812008-07-09 16:18:21 +0200175 ret = ACL_PAT_FAIL;
Willy Tarreauf3d25982007-05-08 22:45:09 +0200176
Willy Tarreauf853c462012-04-23 18:53:56 +0200177 smp->data.str.str[smp->data.str.len] = old_char;
Willy Tarreauf3d25982007-05-08 22:45:09 +0200178 return ret;
179}
180
Willy Tarreaua84d3742007-05-07 00:36:48 +0200181/* Checks that the pattern matches the beginning of the tested string. */
Willy Tarreau37406352012-04-23 16:16:37 +0200182int acl_match_beg(struct sample *smp, struct acl_pattern *pattern)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200183{
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200184 int icase;
185
Willy Tarreauf853c462012-04-23 18:53:56 +0200186 if (pattern->len > smp->data.str.len)
Willy Tarreau11382812008-07-09 16:18:21 +0200187 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200188
189 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
Willy Tarreauf853c462012-04-23 18:53:56 +0200190 if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str, pattern->len) != 0) ||
191 (!icase && strncmp(pattern->ptr.str, smp->data.str.str, pattern->len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +0200192 return ACL_PAT_FAIL;
193 return ACL_PAT_PASS;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200194}
195
196/* Checks that the pattern matches the end of the tested string. */
Willy Tarreau37406352012-04-23 16:16:37 +0200197int acl_match_end(struct sample *smp, struct acl_pattern *pattern)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200198{
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200199 int icase;
200
Willy Tarreauf853c462012-04-23 18:53:56 +0200201 if (pattern->len > smp->data.str.len)
Willy Tarreau11382812008-07-09 16:18:21 +0200202 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200203 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
Willy Tarreauf853c462012-04-23 18:53:56 +0200204 if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str + smp->data.str.len - pattern->len, pattern->len) != 0) ||
205 (!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 +0200206 return ACL_PAT_FAIL;
207 return ACL_PAT_PASS;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200208}
209
210/* Checks that the pattern is included inside the tested string.
211 * NB: Suboptimal, should be rewritten using a Boyer-Moore method.
212 */
Willy Tarreau37406352012-04-23 16:16:37 +0200213int acl_match_sub(struct sample *smp, struct acl_pattern *pattern)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200214{
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200215 int icase;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200216 char *end;
217 char *c;
218
Willy Tarreauf853c462012-04-23 18:53:56 +0200219 if (pattern->len > smp->data.str.len)
Willy Tarreau11382812008-07-09 16:18:21 +0200220 return ACL_PAT_FAIL;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200221
Willy Tarreauf853c462012-04-23 18:53:56 +0200222 end = smp->data.str.str + smp->data.str.len - pattern->len;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200223 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
224 if (icase) {
Willy Tarreauf853c462012-04-23 18:53:56 +0200225 for (c = smp->data.str.str; c <= end; c++) {
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200226 if (tolower(*c) != tolower(*pattern->ptr.str))
227 continue;
228 if (strncasecmp(pattern->ptr.str, c, pattern->len) == 0)
Willy Tarreau11382812008-07-09 16:18:21 +0200229 return ACL_PAT_PASS;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200230 }
231 } else {
Willy Tarreauf853c462012-04-23 18:53:56 +0200232 for (c = smp->data.str.str; c <= end; c++) {
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200233 if (*c != *pattern->ptr.str)
234 continue;
235 if (strncmp(pattern->ptr.str, c, pattern->len) == 0)
Willy Tarreau11382812008-07-09 16:18:21 +0200236 return ACL_PAT_PASS;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200237 }
Willy Tarreaua84d3742007-05-07 00:36:48 +0200238 }
Willy Tarreau11382812008-07-09 16:18:21 +0200239 return ACL_PAT_FAIL;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200240}
241
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200242/* Background: Fast way to find a zero byte in a word
243 * http://graphics.stanford.edu/~seander/bithacks.html#ZeroInWord
244 * hasZeroByte = (v - 0x01010101UL) & ~v & 0x80808080UL;
245 *
246 * To look for 4 different byte values, xor the word with those bytes and
247 * then check for zero bytes:
248 *
249 * v = (((unsigned char)c * 0x1010101U) ^ delimiter)
250 * where <delimiter> is the 4 byte values to look for (as an uint)
251 * and <c> is the character that is being tested
252 */
253static inline unsigned int is_delimiter(unsigned char c, unsigned int mask)
254{
255 mask ^= (c * 0x01010101); /* propagate the char to all 4 bytes */
256 return (mask - 0x01010101) & ~mask & 0x80808080U;
257}
258
259static inline unsigned int make_4delim(unsigned char d1, unsigned char d2, unsigned char d3, unsigned char d4)
260{
261 return d1 << 24 | d2 << 16 | d3 << 8 | d4;
262}
263
Willy Tarreaua84d3742007-05-07 00:36:48 +0200264/* This one is used by other real functions. It checks that the pattern is
265 * included inside the tested string, but enclosed between the specified
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200266 * delimiters or at the beginning or end of the string. The delimiters are
267 * provided as an unsigned int made by make_4delim() and match up to 4 different
268 * delimiters. Delimiters are stripped at the beginning and end of the pattern.
Willy Tarreaua84d3742007-05-07 00:36:48 +0200269 */
Willy Tarreau37406352012-04-23 16:16:37 +0200270static int match_word(struct sample *smp, struct acl_pattern *pattern, unsigned int delimiters)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200271{
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200272 int may_match, icase;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200273 char *c, *end;
274 char *ps;
275 int pl;
276
277 pl = pattern->len;
278 ps = pattern->ptr.str;
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200279
280 while (pl > 0 && is_delimiter(*ps, delimiters)) {
Willy Tarreaua84d3742007-05-07 00:36:48 +0200281 pl--;
282 ps++;
283 }
284
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200285 while (pl > 0 && is_delimiter(ps[pl - 1], delimiters))
Willy Tarreaua84d3742007-05-07 00:36:48 +0200286 pl--;
287
Willy Tarreauf853c462012-04-23 18:53:56 +0200288 if (pl > smp->data.str.len)
Willy Tarreau11382812008-07-09 16:18:21 +0200289 return ACL_PAT_FAIL;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200290
291 may_match = 1;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200292 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
Willy Tarreauf853c462012-04-23 18:53:56 +0200293 end = smp->data.str.str + smp->data.str.len - pl;
294 for (c = smp->data.str.str; c <= end; c++) {
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200295 if (is_delimiter(*c, delimiters)) {
Willy Tarreaua84d3742007-05-07 00:36:48 +0200296 may_match = 1;
297 continue;
298 }
Willy Tarreaua84d3742007-05-07 00:36:48 +0200299
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200300 if (!may_match)
301 continue;
302
303 if (icase) {
304 if ((tolower(*c) == tolower(*ps)) &&
305 (strncasecmp(ps, c, pl) == 0) &&
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200306 (c == end || is_delimiter(c[pl], delimiters)))
Willy Tarreau11382812008-07-09 16:18:21 +0200307 return ACL_PAT_PASS;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200308 } else {
309 if ((*c == *ps) &&
310 (strncmp(ps, c, pl) == 0) &&
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200311 (c == end || is_delimiter(c[pl], delimiters)))
Willy Tarreau11382812008-07-09 16:18:21 +0200312 return ACL_PAT_PASS;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200313 }
Willy Tarreaua84d3742007-05-07 00:36:48 +0200314 may_match = 0;
315 }
Willy Tarreau11382812008-07-09 16:18:21 +0200316 return ACL_PAT_FAIL;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200317}
318
319/* Checks that the pattern is included inside the tested string, but enclosed
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200320 * between the delimiters '?' or '/' or at the beginning or end of the string.
321 * Delimiters at the beginning or end of the pattern are ignored.
Willy Tarreaua84d3742007-05-07 00:36:48 +0200322 */
Willy Tarreau37406352012-04-23 16:16:37 +0200323int acl_match_dir(struct sample *smp, struct acl_pattern *pattern)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200324{
Willy Tarreau37406352012-04-23 16:16:37 +0200325 return match_word(smp, pattern, make_4delim('/', '?', '?', '?'));
Willy Tarreaua84d3742007-05-07 00:36:48 +0200326}
327
328/* Checks that the pattern is included inside the tested string, but enclosed
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200329 * between the delmiters '/', '?', '.' or ":" or at the beginning or end of
330 * the string. Delimiters at the beginning or end of the pattern are ignored.
Willy Tarreaua84d3742007-05-07 00:36:48 +0200331 */
Willy Tarreau37406352012-04-23 16:16:37 +0200332int acl_match_dom(struct sample *smp, struct acl_pattern *pattern)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200333{
Willy Tarreau37406352012-04-23 16:16:37 +0200334 return match_word(smp, pattern, make_4delim('/', '?', '.', ':'));
Willy Tarreaua84d3742007-05-07 00:36:48 +0200335}
336
337/* Checks that the integer in <test> is included between min and max */
Willy Tarreau37406352012-04-23 16:16:37 +0200338int acl_match_int(struct sample *smp, struct acl_pattern *pattern)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200339{
Willy Tarreauf853c462012-04-23 18:53:56 +0200340 if ((!pattern->val.range.min_set || pattern->val.range.min <= smp->data.uint) &&
341 (!pattern->val.range.max_set || smp->data.uint <= pattern->val.range.max))
Willy Tarreau11382812008-07-09 16:18:21 +0200342 return ACL_PAT_PASS;
343 return ACL_PAT_FAIL;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200344}
345
Willy Tarreau0e698542011-09-16 08:32:32 +0200346/* Checks that the length of the pattern in <test> is included between min and max */
Willy Tarreau37406352012-04-23 16:16:37 +0200347int acl_match_len(struct sample *smp, struct acl_pattern *pattern)
Willy Tarreau0e698542011-09-16 08:32:32 +0200348{
Willy Tarreauf853c462012-04-23 18:53:56 +0200349 if ((!pattern->val.range.min_set || pattern->val.range.min <= smp->data.str.len) &&
350 (!pattern->val.range.max_set || smp->data.str.len <= pattern->val.range.max))
Willy Tarreau0e698542011-09-16 08:32:32 +0200351 return ACL_PAT_PASS;
352 return ACL_PAT_FAIL;
353}
354
Willy Tarreau37406352012-04-23 16:16:37 +0200355int acl_match_ip(struct sample *smp, struct acl_pattern *pattern)
Willy Tarreaua67fad92007-05-08 19:50:09 +0200356{
Willy Tarreauceb4ac92012-04-28 00:41:46 +0200357 unsigned int v4; /* in network byte order */
358 struct in6_addr *v6;
359 int bits, pos;
360 struct in6_addr tmp6;
Willy Tarreaua67fad92007-05-08 19:50:09 +0200361
Willy Tarreauceb4ac92012-04-28 00:41:46 +0200362 if (pattern->type == SMP_T_IPV4) {
363 if (smp->type == SMP_T_IPV4) {
364 v4 = smp->data.ipv4.s_addr;
365 }
366 else if (smp->type == SMP_T_IPV6) {
367 /* v4 match on a V6 sample. We want to check at least for
368 * the following forms :
369 * - ::ffff:ip:v4 (ipv4 mapped)
370 * - ::0000:ip:v4 (old ipv4 mapped)
371 * - 2002:ip:v4:: (6to4)
372 */
373 if (*(uint32_t*)&smp->data.ipv6.s6_addr[0] == 0 &&
374 *(uint32_t*)&smp->data.ipv6.s6_addr[4] == 0 &&
375 (*(uint32_t*)&smp->data.ipv6.s6_addr[8] == 0 ||
376 *(uint32_t*)&smp->data.ipv6.s6_addr[8] == htonl(0xFFFF))) {
377 v4 = *(uint32_t*)&smp->data.ipv6.s6_addr[12];
378 }
379 else if (*(uint16_t*)&smp->data.ipv6.s6_addr[0] == htons(0x2002)) {
380 v4 = htonl((ntohs(*(uint16_t*)&smp->data.ipv6.s6_addr[2]) << 16) +
381 ntohs(*(uint16_t*)&smp->data.ipv6.s6_addr[4]));
382 }
383 else
384 return ACL_PAT_FAIL;
385 }
386 else
387 return ACL_PAT_FAIL;
Willy Tarreaua67fad92007-05-08 19:50:09 +0200388
Willy Tarreauceb4ac92012-04-28 00:41:46 +0200389 if (((v4 ^ pattern->val.ipv4.addr.s_addr) & pattern->val.ipv4.mask.s_addr) == 0)
390 return ACL_PAT_PASS;
391 else
392 return ACL_PAT_FAIL;
393 }
394 else if (pattern->type == SMP_T_IPV6) {
395 if (smp->type == SMP_T_IPV4) {
396 /* Convert the IPv4 sample address to IPv4 with the
397 * mapping method using the ::ffff: prefix.
398 */
399 memset(&tmp6, 0, 10);
400 *(uint16_t*)&tmp6.s6_addr[10] = htons(0xffff);
401 *(uint32_t*)&tmp6.s6_addr[12] = smp->data.ipv4.s_addr;
402 v6 = &tmp6;
403 }
404 else if (smp->type == SMP_T_IPV6) {
405 v6 = &smp->data.ipv6;
406 }
407 else {
408 return ACL_PAT_FAIL;
409 }
410
411 bits = pattern->val.ipv6.mask;
412 for (pos = 0; bits > 0; pos += 4, bits -= 32) {
413 v4 = *(uint32_t*)&v6->s6_addr[pos] ^ *(uint32_t*)&pattern->val.ipv6.addr.s6_addr[pos];
414 if (bits < 32)
Cyril Bonté4c01beb2012-10-23 21:28:31 +0200415 v4 &= htonl((~0U) << (32-bits));
Willy Tarreauceb4ac92012-04-28 00:41:46 +0200416 if (v4)
417 return ACL_PAT_FAIL;
418 }
Willy Tarreau11382812008-07-09 16:18:21 +0200419 return ACL_PAT_PASS;
Willy Tarreauceb4ac92012-04-28 00:41:46 +0200420 }
Willy Tarreau11382812008-07-09 16:18:21 +0200421 return ACL_PAT_FAIL;
Willy Tarreaua67fad92007-05-08 19:50:09 +0200422}
423
Willy Tarreaub337b532010-05-13 20:03:41 +0200424/* Lookup an IPv4 address in the expression's pattern tree using the longest
425 * match method. The node is returned if it exists, otherwise NULL.
426 */
Willy Tarreau37406352012-04-23 16:16:37 +0200427static void *acl_lookup_ip(struct sample *smp, struct acl_expr *expr)
Willy Tarreaub337b532010-05-13 20:03:41 +0200428{
429 struct in_addr *s;
430
Willy Tarreauf853c462012-04-23 18:53:56 +0200431 if (smp->type != SMP_T_IPV4)
Willy Tarreaub337b532010-05-13 20:03:41 +0200432 return ACL_PAT_FAIL;
433
Willy Tarreauf853c462012-04-23 18:53:56 +0200434 s = &smp->data.ipv4;
Willy Tarreaub337b532010-05-13 20:03:41 +0200435 return ebmb_lookup_longest(&expr->pattern_tree, &s->s_addr);
436}
437
Willy Tarreaua84d3742007-05-07 00:36:48 +0200438/* Parse a string. It is allocated and duplicated. */
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200439int acl_parse_str(const char **text, struct acl_pattern *pattern, int *opaque, char **err)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200440{
441 int len;
442
Willy Tarreauae8b7962007-06-09 23:10:04 +0200443 len = strlen(*text);
Willy Tarreauc92ddbc2012-04-27 22:10:57 +0200444 pattern->type = SMP_T_CSTR;
Willy Tarreauc4262962010-05-10 23:42:40 +0200445
446 if (pattern->flags & ACL_PAT_F_TREE_OK) {
447 /* we're allowed to put the data in a tree whose root is pointed
448 * to by val.tree.
449 */
450 struct ebmb_node *node;
451
452 node = calloc(1, sizeof(*node) + len + 1);
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200453 if (!node) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200454 memprintf(err, "out of memory while loading string pattern");
Willy Tarreauc4262962010-05-10 23:42:40 +0200455 return 0;
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200456 }
Willy Tarreauc4262962010-05-10 23:42:40 +0200457 memcpy(node->key, *text, len + 1);
458 if (ebst_insert(pattern->val.tree, node) != node)
459 free(node); /* was a duplicate */
460 pattern->flags |= ACL_PAT_F_TREE; /* this pattern now contains a tree */
461 return 1;
462 }
463
Willy Tarreauae8b7962007-06-09 23:10:04 +0200464 pattern->ptr.str = strdup(*text);
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200465 if (!pattern->ptr.str) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200466 memprintf(err, "out of memory while loading string pattern");
Willy Tarreaua84d3742007-05-07 00:36:48 +0200467 return 0;
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200468 }
Willy Tarreaua84d3742007-05-07 00:36:48 +0200469 pattern->len = len;
470 return 1;
471}
472
Emeric Brun07ca4962012-10-17 13:38:19 +0200473/* Parse a binary written in hexa. It is allocated. */
474int acl_parse_bin(const char **text, struct acl_pattern *pattern, int *opaque, char **err)
475{
476 int len;
477 const char *p = *text;
478 int i,j;
479
480 len = strlen(p);
481 if (len%2) {
482 memprintf(err, "an even number of hex digit is expected");
483 return 0;
484 }
485
486 pattern->type = SMP_T_CBIN;
487 pattern->len = len >> 1;
488 pattern->ptr.str = malloc(pattern->len);
489 if (!pattern->ptr.str) {
490 memprintf(err, "out of memory while loading string pattern");
491 return 0;
492 }
493
494 i = j = 0;
495 while (j < pattern->len) {
496 if (!ishex(p[i++]))
497 goto bad_input;
498 if (!ishex(p[i++]))
499 goto bad_input;
500 pattern->ptr.str[j++] = (hex2i(p[i-2]) << 4) + hex2i(p[i-1]);
501 }
502 return 1;
503
504bad_input:
505 memprintf(err, "an hex digit is expected (found '%c')", p[i-1]);
506 free(pattern->ptr.str);
507 return 0;
508}
509
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +0100510/* Parse and concatenate all further strings into one. */
511int
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200512acl_parse_strcat(const char **text, struct acl_pattern *pattern, int *opaque, char **err)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +0100513{
514
515 int len = 0, i;
516 char *s;
517
518 for (i = 0; *text[i]; i++)
519 len += strlen(text[i])+1;
520
Willy Tarreauc92ddbc2012-04-27 22:10:57 +0200521 pattern->type = SMP_T_CSTR;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +0100522 pattern->ptr.str = s = calloc(1, len);
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200523 if (!pattern->ptr.str) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200524 memprintf(err, "out of memory while loading pattern");
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +0100525 return 0;
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200526 }
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +0100527
528 for (i = 0; *text[i]; i++)
529 s += sprintf(s, i?" %s":"%s", text[i]);
530
531 pattern->len = len;
532
533 return i;
534}
535
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +0200536/* Free data allocated by acl_parse_reg */
Willy Tarreau37406352012-04-23 16:16:37 +0200537static void acl_free_reg(void *ptr)
538{
Hiroaki Nakamura70351322013-01-13 15:00:42 +0900539 regex_free(ptr);
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +0200540}
541
Willy Tarreauf3d25982007-05-08 22:45:09 +0200542/* Parse a regex. It is allocated. */
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200543int acl_parse_reg(const char **text, struct acl_pattern *pattern, int *opaque, char **err)
Willy Tarreauf3d25982007-05-08 22:45:09 +0200544{
Hiroaki Nakamura70351322013-01-13 15:00:42 +0900545 regex *preg;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200546 int icase;
Willy Tarreauf3d25982007-05-08 22:45:09 +0200547
Hiroaki Nakamura70351322013-01-13 15:00:42 +0900548 preg = calloc(1, sizeof(*preg));
Willy Tarreauf3d25982007-05-08 22:45:09 +0200549
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200550 if (!preg) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200551 memprintf(err, "out of memory while loading pattern");
Willy Tarreauf3d25982007-05-08 22:45:09 +0200552 return 0;
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200553 }
Willy Tarreauf3d25982007-05-08 22:45:09 +0200554
Hiroaki Nakamura70351322013-01-13 15:00:42 +0900555#ifdef USE_PCRE_JIT
556 icase = (pattern->flags & ACL_PAT_F_IGNORE_CASE) ? PCRE_CASELESS : 0;
557 preg->reg = pcre_compile(*text, PCRE_NO_AUTO_CAPTURE | icase, NULL, NULL,
558 NULL);
559 if (!preg->reg) {
560 free(preg);
561 memprintf(err, "regex '%s' is invalid", *text);
562 return 0;
563 }
564
565 preg->extra = pcre_study(preg->reg, PCRE_STUDY_JIT_COMPILE, NULL);
566 if (!preg->extra) {
567 pcre_free(preg->reg);
568 free(preg);
569 memprintf(err, "failed to compile regex '%s'", *text);
570 return 0;
571 }
572#else
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200573 icase = (pattern->flags & ACL_PAT_F_IGNORE_CASE) ? REG_ICASE : 0;
574 if (regcomp(preg, *text, REG_EXTENDED | REG_NOSUB | icase) != 0) {
Willy Tarreauf3d25982007-05-08 22:45:09 +0200575 free(preg);
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200576 memprintf(err, "regex '%s' is invalid", *text);
Willy Tarreauf3d25982007-05-08 22:45:09 +0200577 return 0;
578 }
Hiroaki Nakamura70351322013-01-13 15:00:42 +0900579#endif
Willy Tarreauf3d25982007-05-08 22:45:09 +0200580
581 pattern->ptr.reg = preg;
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +0200582 pattern->freeptrbuf = &acl_free_reg;
Willy Tarreauf3d25982007-05-08 22:45:09 +0200583 return 1;
584}
585
Willy Tarreauae8b7962007-06-09 23:10:04 +0200586/* Parse a range of positive integers delimited by either ':' or '-'. If only
587 * one integer is read, it is set as both min and max. An operator may be
588 * specified as the prefix, among this list of 5 :
589 *
590 * 0:eq, 1:gt, 2:ge, 3:lt, 4:le
591 *
592 * The default operator is "eq". It supports range matching. Ranges are
593 * rejected for other operators. The operator may be changed at any time.
594 * The operator is stored in the 'opaque' argument.
595 *
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200596 * If err is non-NULL, an error message will be returned there on errors and
597 * the caller will have to free it.
598 *
Willy Tarreaua84d3742007-05-07 00:36:48 +0200599 */
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200600int acl_parse_int(const char **text, struct acl_pattern *pattern, int *opaque, char **err)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200601{
Willy Tarreauae8b7962007-06-09 23:10:04 +0200602 signed long long i;
603 unsigned int j, last, skip = 0;
604 const char *ptr = *text;
605
Willy Tarreauc92ddbc2012-04-27 22:10:57 +0200606 pattern->type = SMP_T_UINT;
Willy Tarreau8f8e6452007-06-17 21:51:38 +0200607 while (!isdigit((unsigned char)*ptr)) {
Willy Tarreau1c7cc5b2010-07-18 10:46:33 +0200608 switch (get_std_op(ptr)) {
609 case STD_OP_EQ: *opaque = 0; break;
610 case STD_OP_GT: *opaque = 1; break;
611 case STD_OP_GE: *opaque = 2; break;
612 case STD_OP_LT: *opaque = 3; break;
613 case STD_OP_LE: *opaque = 4; break;
614 default:
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200615 memprintf(err, "'%s' is neither a number nor a supported operator", ptr);
Willy Tarreauae8b7962007-06-09 23:10:04 +0200616 return 0;
Willy Tarreau1c7cc5b2010-07-18 10:46:33 +0200617 }
Willy Tarreauae8b7962007-06-09 23:10:04 +0200618
619 skip++;
620 ptr = text[skip];
621 }
Willy Tarreaua84d3742007-05-07 00:36:48 +0200622
623 last = i = 0;
624 while (1) {
Willy Tarreauae8b7962007-06-09 23:10:04 +0200625 j = *ptr++;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200626 if ((j == '-' || j == ':') && !last) {
627 last++;
628 pattern->val.range.min = i;
629 i = 0;
630 continue;
631 }
632 j -= '0';
633 if (j > 9)
634 // also catches the terminating zero
635 break;
636 i *= 10;
637 i += j;
638 }
Willy Tarreauae8b7962007-06-09 23:10:04 +0200639
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200640 if (last && *opaque >= 1 && *opaque <= 4) {
Willy Tarreauae8b7962007-06-09 23:10:04 +0200641 /* having a range with a min or a max is absurd */
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200642 memprintf(err, "integer range '%s' specified with a comparison operator", text[skip]);
Willy Tarreauae8b7962007-06-09 23:10:04 +0200643 return 0;
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200644 }
Willy Tarreauae8b7962007-06-09 23:10:04 +0200645
Willy Tarreaua84d3742007-05-07 00:36:48 +0200646 if (!last)
647 pattern->val.range.min = i;
648 pattern->val.range.max = i;
Willy Tarreauae8b7962007-06-09 23:10:04 +0200649
650 switch (*opaque) {
651 case 0: /* eq */
652 pattern->val.range.min_set = 1;
653 pattern->val.range.max_set = 1;
654 break;
655 case 1: /* gt */
656 pattern->val.range.min++; /* gt = ge + 1 */
657 case 2: /* ge */
658 pattern->val.range.min_set = 1;
659 pattern->val.range.max_set = 0;
660 break;
661 case 3: /* lt */
662 pattern->val.range.max--; /* lt = le - 1 */
663 case 4: /* le */
664 pattern->val.range.min_set = 0;
665 pattern->val.range.max_set = 1;
666 break;
667 }
668 return skip + 1;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200669}
670
Willy Tarreau4a26d2f2008-07-15 16:05:33 +0200671/* Parse a range of positive 2-component versions delimited by either ':' or
672 * '-'. The version consists in a major and a minor, both of which must be
673 * smaller than 65536, because internally they will be represented as a 32-bit
674 * integer.
675 * If only one version is read, it is set as both min and max. Just like for
676 * pure integers, an operator may be specified as the prefix, among this list
677 * of 5 :
678 *
679 * 0:eq, 1:gt, 2:ge, 3:lt, 4:le
680 *
681 * The default operator is "eq". It supports range matching. Ranges are
682 * rejected for other operators. The operator may be changed at any time.
683 * The operator is stored in the 'opaque' argument. This allows constructs
684 * such as the following one :
685 *
686 * acl obsolete_ssl ssl_req_proto lt 3
687 * acl unsupported_ssl ssl_req_proto gt 3.1
688 * acl valid_ssl ssl_req_proto 3.0-3.1
689 *
690 */
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200691int acl_parse_dotted_ver(const char **text, struct acl_pattern *pattern, int *opaque, char **err)
Willy Tarreau4a26d2f2008-07-15 16:05:33 +0200692{
693 signed long long i;
694 unsigned int j, last, skip = 0;
695 const char *ptr = *text;
696
697
698 while (!isdigit((unsigned char)*ptr)) {
Willy Tarreau1c7cc5b2010-07-18 10:46:33 +0200699 switch (get_std_op(ptr)) {
700 case STD_OP_EQ: *opaque = 0; break;
701 case STD_OP_GT: *opaque = 1; break;
702 case STD_OP_GE: *opaque = 2; break;
703 case STD_OP_LT: *opaque = 3; break;
704 case STD_OP_LE: *opaque = 4; break;
705 default:
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200706 memprintf(err, "'%s' is neither a number nor a supported operator", ptr);
Willy Tarreau4a26d2f2008-07-15 16:05:33 +0200707 return 0;
Willy Tarreau1c7cc5b2010-07-18 10:46:33 +0200708 }
Willy Tarreau4a26d2f2008-07-15 16:05:33 +0200709
710 skip++;
711 ptr = text[skip];
712 }
713
714 last = i = 0;
715 while (1) {
716 j = *ptr++;
717 if (j == '.') {
718 /* minor part */
719 if (i >= 65536)
720 return 0;
721 i <<= 16;
722 continue;
723 }
724 if ((j == '-' || j == ':') && !last) {
725 last++;
726 if (i < 65536)
727 i <<= 16;
728 pattern->val.range.min = i;
729 i = 0;
730 continue;
731 }
732 j -= '0';
733 if (j > 9)
734 // also catches the terminating zero
735 break;
736 i = (i & 0xFFFF0000) + (i & 0xFFFF) * 10;
737 i += j;
738 }
739
740 /* if we only got a major version, let's shift it now */
741 if (i < 65536)
742 i <<= 16;
743
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200744 if (last && *opaque >= 1 && *opaque <= 4) {
Willy Tarreau4a26d2f2008-07-15 16:05:33 +0200745 /* having a range with a min or a max is absurd */
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200746 memprintf(err, "version range '%s' specified with a comparison operator", text[skip]);
Willy Tarreau4a26d2f2008-07-15 16:05:33 +0200747 return 0;
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200748 }
Willy Tarreau4a26d2f2008-07-15 16:05:33 +0200749
750 if (!last)
751 pattern->val.range.min = i;
752 pattern->val.range.max = i;
753
754 switch (*opaque) {
755 case 0: /* eq */
756 pattern->val.range.min_set = 1;
757 pattern->val.range.max_set = 1;
758 break;
759 case 1: /* gt */
760 pattern->val.range.min++; /* gt = ge + 1 */
761 case 2: /* ge */
762 pattern->val.range.min_set = 1;
763 pattern->val.range.max_set = 0;
764 break;
765 case 3: /* lt */
766 pattern->val.range.max--; /* lt = le - 1 */
767 case 4: /* le */
768 pattern->val.range.min_set = 0;
769 pattern->val.range.max_set = 1;
770 break;
771 }
772 return skip + 1;
773}
774
Willy Tarreaua67fad92007-05-08 19:50:09 +0200775/* Parse an IP address and an optional mask in the form addr[/mask].
776 * The addr may either be an IPv4 address or a hostname. The mask
777 * may either be a dotted mask or a number of bits. Returns 1 if OK,
Willy Tarreauc92ddbc2012-04-27 22:10:57 +0200778 * otherwise 0. NOTE: IP address patterns are typed (IPV4/IPV6).
Willy Tarreaua67fad92007-05-08 19:50:09 +0200779 */
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200780int acl_parse_ip(const char **text, struct acl_pattern *pattern, int *opaque, char **err)
Willy Tarreaua67fad92007-05-08 19:50:09 +0200781{
Willy Tarreaub337b532010-05-13 20:03:41 +0200782 struct eb_root *tree = NULL;
783 if (pattern->flags & ACL_PAT_F_TREE_OK)
784 tree = pattern->val.tree;
785
786 if (str2net(*text, &pattern->val.ipv4.addr, &pattern->val.ipv4.mask)) {
787 unsigned int mask = ntohl(pattern->val.ipv4.mask.s_addr);
788 struct ebmb_node *node;
789 /* check if the mask is contiguous so that we can insert the
790 * network into the tree. A continuous mask has only ones on
791 * the left. This means that this mask + its lower bit added
792 * once again is null.
793 */
Willy Tarreauceb4ac92012-04-28 00:41:46 +0200794 pattern->type = SMP_T_IPV4;
Willy Tarreaub337b532010-05-13 20:03:41 +0200795 if (mask + (mask & -mask) == 0 && tree) {
796 mask = mask ? 33 - flsnz(mask & -mask) : 0; /* equals cidr value */
797 /* FIXME: insert <addr>/<mask> into the tree here */
798 node = calloc(1, sizeof(*node) + 4); /* reserve 4 bytes for IPv4 address */
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200799 if (!node) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200800 memprintf(err, "out of memory while loading IPv4 pattern");
Willy Tarreaub337b532010-05-13 20:03:41 +0200801 return 0;
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200802 }
Willy Tarreaub337b532010-05-13 20:03:41 +0200803 memcpy(node->key, &pattern->val.ipv4.addr, 4); /* network byte order */
804 node->node.pfx = mask;
805 if (ebmb_insert_prefix(tree, node, 4) != node)
806 free(node); /* was a duplicate */
807 pattern->flags |= ACL_PAT_F_TREE;
808 return 1;
809 }
Willy Tarreauae8b7962007-06-09 23:10:04 +0200810 return 1;
Willy Tarreaub337b532010-05-13 20:03:41 +0200811 }
Willy Tarreauceb4ac92012-04-28 00:41:46 +0200812 else if (str62net(*text, &pattern->val.ipv6.addr, &pattern->val.ipv6.mask)) {
813 /* no tree support right now */
814 pattern->type = SMP_T_IPV6;
815 return 1;
816 }
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200817 else {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200818 memprintf(err, "'%s' is not a valid IPv4 or IPv6 address", *text);
Willy Tarreauae8b7962007-06-09 23:10:04 +0200819 return 0;
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200820 }
Willy Tarreaua67fad92007-05-08 19:50:09 +0200821}
822
Willy Tarreaua84d3742007-05-07 00:36:48 +0200823/*
824 * Registers the ACL keyword list <kwl> as a list of valid keywords for next
825 * parsing sessions.
826 */
827void acl_register_keywords(struct acl_kw_list *kwl)
828{
829 LIST_ADDQ(&acl_keywords.list, &kwl->list);
830}
831
832/*
833 * Unregisters the ACL keyword list <kwl> from the list of valid keywords.
834 */
835void acl_unregister_keywords(struct acl_kw_list *kwl)
836{
837 LIST_DEL(&kwl->list);
838 LIST_INIT(&kwl->list);
839}
840
841/* Return a pointer to the ACL <name> within the list starting at <head>, or
842 * NULL if not found.
843 */
844struct acl *find_acl_by_name(const char *name, struct list *head)
845{
846 struct acl *acl;
847 list_for_each_entry(acl, head, list) {
848 if (strcmp(acl->name, name) == 0)
849 return acl;
850 }
851 return NULL;
852}
853
854/* Return a pointer to the ACL keyword <kw>, or NULL if not found. Note that if
855 * <kw> contains an opening parenthesis, only the left part of it is checked.
856 */
857struct acl_keyword *find_acl_kw(const char *kw)
858{
859 int index;
860 const char *kwend;
861 struct acl_kw_list *kwl;
862
863 kwend = strchr(kw, '(');
864 if (!kwend)
865 kwend = kw + strlen(kw);
866
867 list_for_each_entry(kwl, &acl_keywords.list, list) {
868 for (index = 0; kwl->kw[index].kw != NULL; index++) {
869 if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) &&
870 kwl->kw[index].kw[kwend-kw] == 0)
871 return &kwl->kw[index];
872 }
873 }
874 return NULL;
875}
876
Willy Tarreaudfd7fca2011-03-09 07:27:02 +0100877/* NB: does nothing if <pat> is NULL */
Willy Tarreaua84d3742007-05-07 00:36:48 +0200878static void free_pattern(struct acl_pattern *pat)
879{
Willy Tarreaudfd7fca2011-03-09 07:27:02 +0100880 if (!pat)
881 return;
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +0200882
883 if (pat->ptr.ptr) {
884 if (pat->freeptrbuf)
885 pat->freeptrbuf(pat->ptr.ptr);
886
Willy Tarreaua84d3742007-05-07 00:36:48 +0200887 free(pat->ptr.ptr);
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +0200888 }
889
Willy Tarreaua84d3742007-05-07 00:36:48 +0200890 free(pat);
891}
892
893static void free_pattern_list(struct list *head)
894{
895 struct acl_pattern *pat, *tmp;
896 list_for_each_entry_safe(pat, tmp, head, list)
897 free_pattern(pat);
898}
899
Willy Tarreaue56cda92010-05-11 23:25:05 +0200900static void free_pattern_tree(struct eb_root *root)
901{
902 struct eb_node *node, *next;
903 node = eb_first(root);
904 while (node) {
905 next = eb_next(node);
906 free(node);
907 node = next;
908 }
909}
910
Willy Tarreaua84d3742007-05-07 00:36:48 +0200911static struct acl_expr *prune_acl_expr(struct acl_expr *expr)
912{
Willy Tarreau34db1082012-04-19 17:16:54 +0200913 struct arg *arg;
914
Willy Tarreaua84d3742007-05-07 00:36:48 +0200915 free_pattern_list(&expr->patterns);
Willy Tarreaue56cda92010-05-11 23:25:05 +0200916 free_pattern_tree(&expr->pattern_tree);
Willy Tarreaua84d3742007-05-07 00:36:48 +0200917 LIST_INIT(&expr->patterns);
Willy Tarreau34db1082012-04-19 17:16:54 +0200918
919 for (arg = expr->args; arg; arg++) {
920 if (arg->type == ARGT_STOP)
921 break;
Willy Tarreau496aa012012-06-01 10:38:29 +0200922 if (arg->type == ARGT_STR || arg->unresolved) {
Willy Tarreau34db1082012-04-19 17:16:54 +0200923 free(arg->data.str.str);
924 arg->data.str.str = NULL;
Willy Tarreau496aa012012-06-01 10:38:29 +0200925 arg->unresolved = 0;
Willy Tarreau34db1082012-04-19 17:16:54 +0200926 }
Willy Tarreau34db1082012-04-19 17:16:54 +0200927 }
928
Willy Tarreau2e845be2012-10-19 19:49:09 +0200929 if (expr->args != empty_arg_list)
930 free(expr->args);
Willy Tarreaua84d3742007-05-07 00:36:48 +0200931 return expr;
932}
933
Willy Tarreau08ad0b32012-04-27 17:25:24 +0200934
935/* Reads patterns from a file. If <err_msg> is non-NULL, an error message will
936 * be returned there on errors and the caller will have to free it.
937 */
Willy Tarreaud76a98a2013-03-31 18:34:33 +0200938static int acl_read_patterns_from_file(struct acl_expr *expr,
939 const char *filename, int patflags,
940 char **err)
Willy Tarreau2b5285d2010-05-09 23:45:24 +0200941{
942 FILE *file;
943 char *c;
944 const char *args[2];
945 struct acl_pattern *pattern;
946 int opaque;
Willy Tarreau6a8097f2011-02-26 15:14:15 +0100947 int ret = 0;
Willy Tarreau08ad0b32012-04-27 17:25:24 +0200948 int line = 0;
Willy Tarreau2b5285d2010-05-09 23:45:24 +0200949
950 file = fopen(filename, "r");
Willy Tarreau08ad0b32012-04-27 17:25:24 +0200951 if (!file) {
952 memprintf(err, "failed to open pattern file <%s>", filename);
Willy Tarreau2b5285d2010-05-09 23:45:24 +0200953 return 0;
Willy Tarreau08ad0b32012-04-27 17:25:24 +0200954 }
Willy Tarreau2b5285d2010-05-09 23:45:24 +0200955
956 /* now parse all patterns. The file may contain only one pattern per
957 * line. If the line contains spaces, they will be part of the pattern.
958 * The pattern stops at the first CR, LF or EOF encountered.
959 */
960 opaque = 0;
Willy Tarreaue56cda92010-05-11 23:25:05 +0200961 pattern = NULL;
Willy Tarreau2b5285d2010-05-09 23:45:24 +0200962 args[1] = "";
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100963 while (fgets(trash.str, trash.size, file) != NULL) {
Willy Tarreau08ad0b32012-04-27 17:25:24 +0200964 line++;
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100965 c = trash.str;
Willy Tarreau58215a02010-05-13 22:07:43 +0200966
967 /* ignore lines beginning with a dash */
968 if (*c == '#')
969 continue;
970
971 /* strip leading spaces and tabs */
972 while (*c == ' ' || *c == '\t')
973 c++;
974
Willy Tarreau58215a02010-05-13 22:07:43 +0200975
976 args[0] = c;
Willy Tarreau2b5285d2010-05-09 23:45:24 +0200977 while (*c && *c != '\n' && *c != '\r')
978 c++;
979 *c = 0;
980
Willy Tarreau51091962011-01-03 21:04:10 +0100981 /* empty lines are ignored too */
982 if (c == args[0])
983 continue;
984
Willy Tarreaue56cda92010-05-11 23:25:05 +0200985 /* we keep the previous pattern along iterations as long as it's not used */
986 if (!pattern)
987 pattern = (struct acl_pattern *)malloc(sizeof(*pattern));
Willy Tarreau08ad0b32012-04-27 17:25:24 +0200988 if (!pattern) {
989 memprintf(err, "out of memory when loading patterns from file <%s>", filename);
Willy Tarreau2b5285d2010-05-09 23:45:24 +0200990 goto out_close;
Willy Tarreau08ad0b32012-04-27 17:25:24 +0200991 }
Willy Tarreaue56cda92010-05-11 23:25:05 +0200992
993 memset(pattern, 0, sizeof(*pattern));
Willy Tarreau2b5285d2010-05-09 23:45:24 +0200994 pattern->flags = patflags;
995
Willy Tarreaue0db1e82013-01-04 16:31:47 +0100996 if (!(pattern->flags & ACL_PAT_F_IGNORE_CASE) &&
Willy Tarreaud76a98a2013-03-31 18:34:33 +0200997 (expr->match == acl_match_str || expr->match == acl_match_ip)) {
Willy Tarreaue56cda92010-05-11 23:25:05 +0200998 /* we pre-set the data pointer to the tree's head so that functions
999 * which are able to insert in a tree know where to do that.
1000 */
1001 pattern->flags |= ACL_PAT_F_TREE_OK;
1002 pattern->val.tree = &expr->pattern_tree;
1003 }
1004
Willy Tarreauc92ddbc2012-04-27 22:10:57 +02001005 pattern->type = SMP_TYPES; /* unspecified type by default */
Willy Tarreaud76a98a2013-03-31 18:34:33 +02001006 if (!expr->parse(args, pattern, &opaque, err))
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001007 goto out_free_pattern;
Willy Tarreaue56cda92010-05-11 23:25:05 +02001008
1009 /* if the parser did not feed the tree, let's chain the pattern to the list */
1010 if (!(pattern->flags & ACL_PAT_F_TREE)) {
1011 LIST_ADDQ(&expr->patterns, &pattern->list);
1012 pattern = NULL; /* get a new one */
1013 }
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001014 }
Willy Tarreau6a8097f2011-02-26 15:14:15 +01001015
1016 ret = 1; /* success */
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001017
1018 out_free_pattern:
1019 free_pattern(pattern);
1020 out_close:
1021 fclose(file);
Willy Tarreau6a8097f2011-02-26 15:14:15 +01001022 return ret;
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001023}
1024
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001025/* Parse an ACL expression starting at <args>[0], and return it. If <err> is
1026 * not NULL, it will be filled with a pointer to an error message in case of
1027 * error. This pointer must be freeable or NULL.
1028 *
Willy Tarreaua84d3742007-05-07 00:36:48 +02001029 * Right now, the only accepted syntax is :
1030 * <subject> [<value>...]
1031 */
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001032struct acl_expr *parse_acl_expr(const char **args, char **err)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001033{
1034 __label__ out_return, out_free_expr, out_free_pattern;
1035 struct acl_expr *expr;
1036 struct acl_keyword *aclkw;
1037 struct acl_pattern *pattern;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02001038 int opaque, patflags;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001039 const char *arg;
1040
1041 aclkw = find_acl_kw(args[0]);
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001042 if (!aclkw || !aclkw->parse) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001043 memprintf(err, "unknown ACL keyword '%s'", *args);
Willy Tarreaua84d3742007-05-07 00:36:48 +02001044 goto out_return;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001045 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001046
1047 expr = (struct acl_expr *)calloc(1, sizeof(*expr));
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001048 if (!expr) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001049 memprintf(err, "out of memory when parsing ACL expression");
Willy Tarreaua84d3742007-05-07 00:36:48 +02001050 goto out_return;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001051 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001052
1053 expr->kw = aclkw;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001054 LIST_INIT(&expr->patterns);
Willy Tarreaue56cda92010-05-11 23:25:05 +02001055 expr->pattern_tree = EB_ROOT_UNIQUE;
Willy Tarreaud76a98a2013-03-31 18:34:33 +02001056 expr->parse = aclkw->parse;
1057 expr->match = aclkw->match;
Willy Tarreau2e845be2012-10-19 19:49:09 +02001058 expr->args = empty_arg_list;
Willy Tarreaud76a98a2013-03-31 18:34:33 +02001059 expr->smp = aclkw->smp;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001060
1061 arg = strchr(args[0], '(');
Willy Tarreaud76a98a2013-03-31 18:34:33 +02001062 if (expr->smp->arg_mask) {
Willy Tarreau61612d42012-04-19 18:42:05 +02001063 int nbargs = 0;
Willy Tarreau34db1082012-04-19 17:16:54 +02001064 char *end;
Willy Tarreau34db1082012-04-19 17:16:54 +02001065
Willy Tarreau61612d42012-04-19 18:42:05 +02001066 if (arg != NULL) {
1067 /* there are 0 or more arguments in the form "subject(arg[,arg]*)" */
1068 arg++;
1069 end = strchr(arg, ')');
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001070 if (!end) {
Willy Tarreaud76a98a2013-03-31 18:34:33 +02001071 memprintf(err, "missing closing ')' after arguments to ACL keyword '%s'", expr->kw->kw);
Willy Tarreau61612d42012-04-19 18:42:05 +02001072 goto out_free_expr;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001073 }
Willy Tarreau34db1082012-04-19 17:16:54 +02001074
Willy Tarreau61612d42012-04-19 18:42:05 +02001075 /* Parse the arguments. Note that currently we have no way to
1076 * report parsing errors, hence the NULL in the error pointers.
1077 * An error is also reported if some mandatory arguments are
1078 * missing.
1079 */
Willy Tarreaud76a98a2013-03-31 18:34:33 +02001080 nbargs = make_arg_list(arg, end - arg, expr->smp->arg_mask, &expr->args,
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001081 err, NULL, NULL);
1082 if (nbargs < 0) {
1083 /* note that make_arg_list will have set <err> here */
Willy Tarreaud76a98a2013-03-31 18:34:33 +02001084 memprintf(err, "in argument to '%s', %s", expr->kw->kw, *err);
Willy Tarreau61612d42012-04-19 18:42:05 +02001085 goto out_free_expr;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001086 }
Willy Tarreauae52f062012-04-26 12:13:35 +02001087
Willy Tarreau2e845be2012-10-19 19:49:09 +02001088 if (!expr->args)
1089 expr->args = empty_arg_list;
1090
Willy Tarreaud76a98a2013-03-31 18:34:33 +02001091 if (expr->smp->val_args && !expr->smp->val_args(expr->args, err)) {
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001092 /* invalid keyword argument, error must have been
1093 * set by val_args().
1094 */
Willy Tarreaud76a98a2013-03-31 18:34:33 +02001095 memprintf(err, "in argument to '%s', %s", expr->kw->kw, *err);
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001096 goto out_free_expr;
1097 }
Willy Tarreau61612d42012-04-19 18:42:05 +02001098 }
Willy Tarreaud76a98a2013-03-31 18:34:33 +02001099 else if (ARGM(expr->smp->arg_mask) == 1) {
1100 int type = (expr->smp->arg_mask >> 4) & 15;
Willy Tarreaufc2c1fd2012-04-19 23:35:54 +02001101
1102 /* If a proxy is noted as a mandatory argument, we'll fake
1103 * an empty one so that acl_find_targets() resolves it as
1104 * the current one later.
1105 */
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001106 if (type != ARGT_FE && type != ARGT_BE && type != ARGT_TAB) {
Willy Tarreaud76a98a2013-03-31 18:34:33 +02001107 memprintf(err, "ACL keyword '%s' expects %d arguments", expr->kw->kw, ARGM(expr->smp->arg_mask));
Willy Tarreaufc2c1fd2012-04-19 23:35:54 +02001108 goto out_free_expr;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001109 }
Willy Tarreaufc2c1fd2012-04-19 23:35:54 +02001110
1111 /* Build an arg list containing the type as an empty string
1112 * and the usual STOP.
1113 */
1114 expr->args = calloc(2, sizeof(*expr->args));
1115 expr->args[0].type = type;
Willy Tarreaue3a46112012-06-15 08:02:34 +02001116 expr->args[0].unresolved = 1;
Willy Tarreaufc2c1fd2012-04-19 23:35:54 +02001117 expr->args[0].data.str.str = strdup("");
1118 expr->args[0].data.str.len = 1;
1119 expr->args[0].data.str.len = 0;
1120 expr->args[1].type = ARGT_STOP;
1121 }
Willy Tarreaud76a98a2013-03-31 18:34:33 +02001122 else if (ARGM(expr->smp->arg_mask)) {
Willy Tarreau61612d42012-04-19 18:42:05 +02001123 /* there were some mandatory arguments */
Willy Tarreaud76a98a2013-03-31 18:34:33 +02001124 memprintf(err, "ACL keyword '%s' expects %d arguments", expr->kw->kw, ARGM(expr->smp->arg_mask));
Willy Tarreaua84d3742007-05-07 00:36:48 +02001125 goto out_free_expr;
Willy Tarreau61612d42012-04-19 18:42:05 +02001126 }
1127 }
1128 else {
1129 if (arg) {
1130 /* no argument expected */
Willy Tarreaud76a98a2013-03-31 18:34:33 +02001131 memprintf(err, "ACL keyword '%s' takes no argument", expr->kw->kw);
Willy Tarreau61612d42012-04-19 18:42:05 +02001132 goto out_free_expr;
1133 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001134 }
1135
Willy Tarreaua84d3742007-05-07 00:36:48 +02001136 args++;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02001137
1138 /* check for options before patterns. Supported options are :
1139 * -i : ignore case for all patterns by default
1140 * -f : read patterns from those files
Willy Tarreau5adeda12013-03-31 22:13:34 +02001141 * -m : force matching method (must be used before -f)
Willy Tarreauc8d7c962007-06-17 08:20:33 +02001142 * -- : everything after this is not an option
1143 */
1144 patflags = 0;
1145 while (**args == '-') {
1146 if ((*args)[1] == 'i')
1147 patflags |= ACL_PAT_F_IGNORE_CASE;
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001148 else if ((*args)[1] == 'f') {
Willy Tarreaud76a98a2013-03-31 18:34:33 +02001149 if (!acl_read_patterns_from_file(expr, args[1], patflags | ACL_PAT_F_FROM_FILE, err))
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001150 goto out_free_expr;
Willy Tarreau5adeda12013-03-31 22:13:34 +02001151 args++;
1152 }
1153 else if ((*args)[1] == 'm') {
1154 int idx;
1155
1156 if (!LIST_ISEMPTY(&expr->patterns) || !eb_is_empty(&expr->pattern_tree)) {
1157 memprintf(err, "'-m' must only be specified before patterns and files in parsing ACL expression");
1158 goto out_free_expr;
1159 }
1160
1161 idx = acl_find_match_name(args[1]);
1162 if (idx < 0) {
1163 memprintf(err, "unknown matching method '%s' when parsing ACL expression", args[1]);
1164 goto out_free_expr;
1165 }
1166
1167 /* Note: -m found is always valid, bool/int are compatible, str/bin/reg/len are compatible */
1168 if (idx == ACL_MATCH_FOUND || /* -m found */
1169 ((idx == ACL_MATCH_BOOL || idx == ACL_MATCH_INT) && /* -m bool/int */
1170 (expr->smp->out_type == SMP_T_BOOL ||
1171 expr->smp->out_type == SMP_T_UINT ||
1172 expr->smp->out_type == SMP_T_SINT)) ||
1173 (idx == ACL_MATCH_IP && /* -m ip */
1174 (expr->smp->out_type == SMP_T_IPV4 ||
1175 expr->smp->out_type == SMP_T_IPV6)) ||
1176 ((idx == ACL_MATCH_BIN || idx == ACL_MATCH_LEN || idx == ACL_MATCH_STR ||
1177 idx == ACL_MATCH_BEG || idx == ACL_MATCH_SUB || idx == ACL_MATCH_DIR ||
1178 idx == ACL_MATCH_DOM || idx == ACL_MATCH_END || idx == ACL_MATCH_REG) && /* strings */
1179 (expr->smp->out_type == SMP_T_STR ||
1180 expr->smp->out_type == SMP_T_BIN ||
1181 expr->smp->out_type == SMP_T_CSTR ||
1182 expr->smp->out_type == SMP_T_CBIN))) {
1183 expr->parse = acl_parse_fcts[idx];
1184 expr->match = acl_match_fcts[idx];
1185 }
1186 else {
1187 memprintf(err, "matching method '%s' cannot be used with fetch keyword '%s'", args[1], expr->kw->kw);
1188 goto out_free_expr;
1189 }
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001190 args++;
1191 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +02001192 else if ((*args)[1] == '-') {
1193 args++;
1194 break;
1195 }
1196 else
1197 break;
1198 args++;
1199 }
1200
1201 /* now parse all patterns */
Willy Tarreauae8b7962007-06-09 23:10:04 +02001202 opaque = 0;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001203 while (**args) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02001204 int ret;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001205 pattern = (struct acl_pattern *)calloc(1, sizeof(*pattern));
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001206 if (!pattern) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001207 memprintf(err, "out of memory when parsing ACL pattern");
Willy Tarreaua84d3742007-05-07 00:36:48 +02001208 goto out_free_expr;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001209 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +02001210 pattern->flags = patflags;
1211
Willy Tarreauc92ddbc2012-04-27 22:10:57 +02001212 pattern->type = SMP_TYPES; /* unspecified type */
Willy Tarreaud76a98a2013-03-31 18:34:33 +02001213 ret = expr->parse(args, pattern, &opaque, err);
Willy Tarreau7dcb6482012-04-27 17:52:25 +02001214 if (!ret)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001215 goto out_free_pattern;
Willy Tarreau7dcb6482012-04-27 17:52:25 +02001216
Willy Tarreaua84d3742007-05-07 00:36:48 +02001217 LIST_ADDQ(&expr->patterns, &pattern->list);
Willy Tarreauae8b7962007-06-09 23:10:04 +02001218 args += ret;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001219 }
1220
1221 return expr;
1222
1223 out_free_pattern:
1224 free_pattern(pattern);
1225 out_free_expr:
1226 prune_acl_expr(expr);
1227 free(expr);
1228 out_return:
1229 return NULL;
1230}
1231
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02001232/* Purge everything in the acl <acl>, then return <acl>. */
1233struct acl *prune_acl(struct acl *acl) {
1234
1235 struct acl_expr *expr, *exprb;
1236
1237 free(acl->name);
1238
1239 list_for_each_entry_safe(expr, exprb, &acl->expr, list) {
1240 LIST_DEL(&expr->list);
1241 prune_acl_expr(expr);
1242 free(expr);
1243 }
1244
1245 return acl;
1246}
1247
Willy Tarreaua84d3742007-05-07 00:36:48 +02001248/* Parse an ACL with the name starting at <args>[0], and with a list of already
1249 * known ACLs in <acl>. If the ACL was not in the list, it will be added.
Willy Tarreau2a56c5e2010-03-15 16:13:29 +01001250 * A pointer to that ACL is returned. If the ACL has an empty name, then it's
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001251 * an anonymous one and it won't be merged with any other one. If <err> is not
1252 * NULL, it will be filled with an appropriate error. This pointer must be
1253 * freeable or NULL.
Willy Tarreaua84d3742007-05-07 00:36:48 +02001254 *
1255 * args syntax: <aclname> <acl_expr>
1256 */
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001257struct acl *parse_acl(const char **args, struct list *known_acl, char **err)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001258{
1259 __label__ out_return, out_free_acl_expr, out_free_name;
1260 struct acl *cur_acl;
1261 struct acl_expr *acl_expr;
1262 char *name;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001263 const char *pos;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001264
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001265 if (**args && (pos = invalid_char(*args))) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001266 memprintf(err, "invalid character in ACL name : '%c'", *pos);
Willy Tarreau2e74c3f2007-12-02 18:45:09 +01001267 goto out_return;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001268 }
Willy Tarreau2e74c3f2007-12-02 18:45:09 +01001269
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001270 acl_expr = parse_acl_expr(args + 1, err);
1271 if (!acl_expr) {
1272 /* parse_acl_expr will have filled <err> here */
Willy Tarreaua84d3742007-05-07 00:36:48 +02001273 goto out_return;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001274 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001275
Willy Tarreau404e8ab2009-07-26 19:40:40 +02001276 /* Check for args beginning with an opening parenthesis just after the
1277 * subject, as this is almost certainly a typo. Right now we can only
1278 * emit a warning, so let's do so.
1279 */
Krzysztof Piotr Oledzki4cdd8312009-10-05 00:23:35 +02001280 if (!strchr(args[1], '(') && *args[2] == '(')
Willy Tarreau404e8ab2009-07-26 19:40:40 +02001281 Warning("parsing acl '%s' :\n"
1282 " matching '%s' for pattern '%s' is likely a mistake and probably\n"
1283 " not what you want. Maybe you need to remove the extraneous space before '('.\n"
1284 " If you are really sure this is not an error, please insert '--' between the\n"
1285 " match and the pattern to make this warning message disappear.\n",
1286 args[0], args[1], args[2]);
1287
Willy Tarreau2a56c5e2010-03-15 16:13:29 +01001288 if (*args[0])
1289 cur_acl = find_acl_by_name(args[0], known_acl);
1290 else
1291 cur_acl = NULL;
1292
Willy Tarreaua84d3742007-05-07 00:36:48 +02001293 if (!cur_acl) {
1294 name = strdup(args[0]);
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001295 if (!name) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001296 memprintf(err, "out of memory when parsing ACL");
Willy Tarreaua84d3742007-05-07 00:36:48 +02001297 goto out_free_acl_expr;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001298 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001299 cur_acl = (struct acl *)calloc(1, sizeof(*cur_acl));
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001300 if (cur_acl == NULL) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001301 memprintf(err, "out of memory when parsing ACL");
Willy Tarreaua84d3742007-05-07 00:36:48 +02001302 goto out_free_name;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001303 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001304
1305 LIST_INIT(&cur_acl->expr);
1306 LIST_ADDQ(known_acl, &cur_acl->list);
1307 cur_acl->name = name;
1308 }
1309
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001310 /* We want to know what features the ACL needs (typically HTTP parsing),
1311 * and where it may be used. If an ACL relies on multiple matches, it is
1312 * OK if at least one of them may match in the context where it is used.
1313 */
Willy Tarreaud76a98a2013-03-31 18:34:33 +02001314 cur_acl->use |= acl_expr->smp->use;
1315 cur_acl->val |= acl_expr->smp->val;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001316 LIST_ADDQ(&cur_acl->expr, &acl_expr->list);
1317 return cur_acl;
1318
1319 out_free_name:
1320 free(name);
1321 out_free_acl_expr:
1322 prune_acl_expr(acl_expr);
1323 free(acl_expr);
1324 out_return:
1325 return NULL;
1326}
1327
Willy Tarreau16fbe822007-06-17 11:54:31 +02001328/* Some useful ACLs provided by default. Only those used are allocated. */
1329
1330const struct {
1331 const char *name;
1332 const char *expr[4]; /* put enough for longest expression */
1333} default_acl_list[] = {
Willy Tarreau58393e12008-07-20 10:39:22 +02001334 { .name = "TRUE", .expr = {"always_true",""}},
1335 { .name = "FALSE", .expr = {"always_false",""}},
Willy Tarreau16fbe822007-06-17 11:54:31 +02001336 { .name = "LOCALHOST", .expr = {"src","127.0.0.1/8",""}},
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001337 { .name = "HTTP", .expr = {"req_proto_http",""}},
Willy Tarreau16fbe822007-06-17 11:54:31 +02001338 { .name = "HTTP_1.0", .expr = {"req_ver","1.0",""}},
1339 { .name = "HTTP_1.1", .expr = {"req_ver","1.1",""}},
1340 { .name = "METH_CONNECT", .expr = {"method","CONNECT",""}},
1341 { .name = "METH_GET", .expr = {"method","GET","HEAD",""}},
1342 { .name = "METH_HEAD", .expr = {"method","HEAD",""}},
1343 { .name = "METH_OPTIONS", .expr = {"method","OPTIONS",""}},
1344 { .name = "METH_POST", .expr = {"method","POST",""}},
1345 { .name = "METH_TRACE", .expr = {"method","TRACE",""}},
1346 { .name = "HTTP_URL_ABS", .expr = {"url_reg","^[^/:]*://",""}},
1347 { .name = "HTTP_URL_SLASH", .expr = {"url_beg","/",""}},
1348 { .name = "HTTP_URL_STAR", .expr = {"url","*",""}},
1349 { .name = "HTTP_CONTENT", .expr = {"hdr_val(content-length)","gt","0",""}},
Emeric Brunbede3d02009-06-30 17:54:00 +02001350 { .name = "RDP_COOKIE", .expr = {"req_rdp_cookie_cnt","gt","0",""}},
Willy Tarreauc6317702008-07-20 09:29:50 +02001351 { .name = "REQ_CONTENT", .expr = {"req_len","gt","0",""}},
Willy Tarreaub6fb4202008-07-20 11:18:28 +02001352 { .name = "WAIT_END", .expr = {"wait_end",""}},
Willy Tarreau16fbe822007-06-17 11:54:31 +02001353 { .name = NULL, .expr = {""}}
1354};
1355
1356/* Find a default ACL from the default_acl list, compile it and return it.
1357 * If the ACL is not found, NULL is returned. In theory, it cannot fail,
1358 * except when default ACLs are broken, in which case it will return NULL.
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001359 * If <known_acl> is not NULL, the ACL will be queued at its tail. If <err> is
1360 * not NULL, it will be filled with an error message if an error occurs. This
1361 * pointer must be freeable or NULL.
Willy Tarreau16fbe822007-06-17 11:54:31 +02001362 */
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001363struct acl *find_acl_default(const char *acl_name, struct list *known_acl, char **err)
Willy Tarreau16fbe822007-06-17 11:54:31 +02001364{
1365 __label__ out_return, out_free_acl_expr, out_free_name;
1366 struct acl *cur_acl;
1367 struct acl_expr *acl_expr;
1368 char *name;
1369 int index;
1370
1371 for (index = 0; default_acl_list[index].name != NULL; index++) {
1372 if (strcmp(acl_name, default_acl_list[index].name) == 0)
1373 break;
1374 }
1375
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001376 if (default_acl_list[index].name == NULL) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001377 memprintf(err, "no such ACL : '%s'", acl_name);
Willy Tarreau16fbe822007-06-17 11:54:31 +02001378 return NULL;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001379 }
Willy Tarreau16fbe822007-06-17 11:54:31 +02001380
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001381 acl_expr = parse_acl_expr((const char **)default_acl_list[index].expr, err);
1382 if (!acl_expr) {
1383 /* parse_acl_expr must have filled err here */
Willy Tarreau16fbe822007-06-17 11:54:31 +02001384 goto out_return;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001385 }
Willy Tarreau16fbe822007-06-17 11:54:31 +02001386
1387 name = strdup(acl_name);
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001388 if (!name) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001389 memprintf(err, "out of memory when building default ACL '%s'", acl_name);
Willy Tarreau16fbe822007-06-17 11:54:31 +02001390 goto out_free_acl_expr;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001391 }
1392
Willy Tarreau16fbe822007-06-17 11:54:31 +02001393 cur_acl = (struct acl *)calloc(1, sizeof(*cur_acl));
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001394 if (cur_acl == NULL) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001395 memprintf(err, "out of memory when building default ACL '%s'", acl_name);
Willy Tarreau16fbe822007-06-17 11:54:31 +02001396 goto out_free_name;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001397 }
Willy Tarreau16fbe822007-06-17 11:54:31 +02001398
1399 cur_acl->name = name;
Willy Tarreaud76a98a2013-03-31 18:34:33 +02001400 cur_acl->use |= acl_expr->smp->use;
1401 cur_acl->val |= acl_expr->smp->val;
Willy Tarreau16fbe822007-06-17 11:54:31 +02001402 LIST_INIT(&cur_acl->expr);
1403 LIST_ADDQ(&cur_acl->expr, &acl_expr->list);
1404 if (known_acl)
1405 LIST_ADDQ(known_acl, &cur_acl->list);
1406
1407 return cur_acl;
1408
1409 out_free_name:
1410 free(name);
1411 out_free_acl_expr:
1412 prune_acl_expr(acl_expr);
1413 free(acl_expr);
1414 out_return:
1415 return NULL;
1416}
Willy Tarreaua84d3742007-05-07 00:36:48 +02001417
1418/* Purge everything in the acl_cond <cond>, then return <cond>. */
1419struct acl_cond *prune_acl_cond(struct acl_cond *cond)
1420{
1421 struct acl_term_suite *suite, *tmp_suite;
1422 struct acl_term *term, *tmp_term;
1423
1424 /* iterate through all term suites and free all terms and all suites */
1425 list_for_each_entry_safe(suite, tmp_suite, &cond->suites, list) {
1426 list_for_each_entry_safe(term, tmp_term, &suite->terms, list)
1427 free(term);
1428 free(suite);
1429 }
1430 return cond;
1431}
1432
1433/* Parse an ACL condition starting at <args>[0], relying on a list of already
1434 * known ACLs passed in <known_acl>. The new condition is returned (or NULL in
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001435 * case of low memory). Supports multiple conditions separated by "or". If
1436 * <err> is not NULL, it will be filled with a pointer to an error message in
1437 * case of error, that the caller is responsible for freeing. The initial
1438 * location must either be freeable or NULL.
Willy Tarreaua84d3742007-05-07 00:36:48 +02001439 */
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001440struct acl_cond *parse_acl_cond(const char **args, struct list *known_acl, int pol, char **err)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001441{
1442 __label__ out_return, out_free_suite, out_free_term;
Willy Tarreau74b98a82007-06-16 19:35:18 +02001443 int arg, neg;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001444 const char *word;
1445 struct acl *cur_acl;
1446 struct acl_term *cur_term;
1447 struct acl_term_suite *cur_suite;
1448 struct acl_cond *cond;
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001449 unsigned int suite_val;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001450
1451 cond = (struct acl_cond *)calloc(1, sizeof(*cond));
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001452 if (cond == NULL) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001453 memprintf(err, "out of memory when parsing condition");
Willy Tarreaua84d3742007-05-07 00:36:48 +02001454 goto out_return;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001455 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001456
1457 LIST_INIT(&cond->list);
1458 LIST_INIT(&cond->suites);
1459 cond->pol = pol;
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001460 cond->val = 0;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001461
1462 cur_suite = NULL;
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001463 suite_val = ~0U;
Willy Tarreau74b98a82007-06-16 19:35:18 +02001464 neg = 0;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001465 for (arg = 0; *args[arg]; arg++) {
1466 word = args[arg];
1467
1468 /* remove as many exclamation marks as we can */
1469 while (*word == '!') {
1470 neg = !neg;
1471 word++;
1472 }
1473
1474 /* an empty word is allowed because we cannot force the user to
1475 * always think about not leaving exclamation marks alone.
1476 */
1477 if (!*word)
1478 continue;
1479
Willy Tarreau16fbe822007-06-17 11:54:31 +02001480 if (strcasecmp(word, "or") == 0 || strcmp(word, "||") == 0) {
Willy Tarreaua84d3742007-05-07 00:36:48 +02001481 /* new term suite */
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001482 cond->val |= suite_val;
1483 suite_val = ~0U;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001484 cur_suite = NULL;
1485 neg = 0;
1486 continue;
1487 }
1488
Willy Tarreau95fa4692010-02-01 13:05:50 +01001489 if (strcmp(word, "{") == 0) {
1490 /* we may have a complete ACL expression between two braces,
1491 * find the last one.
1492 */
1493 int arg_end = arg + 1;
1494 const char **args_new;
1495
1496 while (*args[arg_end] && strcmp(args[arg_end], "}") != 0)
1497 arg_end++;
1498
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001499 if (!*args[arg_end]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001500 memprintf(err, "missing closing '}' in condition");
Willy Tarreau95fa4692010-02-01 13:05:50 +01001501 goto out_free_suite;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001502 }
Willy Tarreau95fa4692010-02-01 13:05:50 +01001503
1504 args_new = calloc(1, (arg_end - arg + 1) * sizeof(*args_new));
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001505 if (!args_new) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001506 memprintf(err, "out of memory when parsing condition");
Willy Tarreau95fa4692010-02-01 13:05:50 +01001507 goto out_free_suite;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001508 }
Willy Tarreau95fa4692010-02-01 13:05:50 +01001509
Willy Tarreau2a56c5e2010-03-15 16:13:29 +01001510 args_new[0] = "";
Willy Tarreau95fa4692010-02-01 13:05:50 +01001511 memcpy(args_new + 1, args + arg + 1, (arg_end - arg) * sizeof(*args_new));
1512 args_new[arg_end - arg] = "";
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001513 cur_acl = parse_acl(args_new, known_acl, err);
Willy Tarreau95fa4692010-02-01 13:05:50 +01001514 free(args_new);
1515
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001516 if (!cur_acl) {
1517 /* note that parse_acl() must have filled <err> here */
Willy Tarreau16fbe822007-06-17 11:54:31 +02001518 goto out_free_suite;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001519 }
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001520 word = args[arg + 1];
Willy Tarreau95fa4692010-02-01 13:05:50 +01001521 arg = arg_end;
1522 }
1523 else {
1524 /* search for <word> in the known ACL names. If we do not find
1525 * it, let's look for it in the default ACLs, and if found, add
1526 * it to the list of ACLs of this proxy. This makes it possible
1527 * to override them.
1528 */
1529 cur_acl = find_acl_by_name(word, known_acl);
1530 if (cur_acl == NULL) {
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001531 cur_acl = find_acl_default(word, known_acl, err);
1532 if (cur_acl == NULL) {
1533 /* note that find_acl_default() must have filled <err> here */
Willy Tarreau95fa4692010-02-01 13:05:50 +01001534 goto out_free_suite;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001535 }
Willy Tarreau95fa4692010-02-01 13:05:50 +01001536 }
Willy Tarreau16fbe822007-06-17 11:54:31 +02001537 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001538
1539 cur_term = (struct acl_term *)calloc(1, sizeof(*cur_term));
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001540 if (cur_term == NULL) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001541 memprintf(err, "out of memory when parsing condition");
Willy Tarreaua84d3742007-05-07 00:36:48 +02001542 goto out_free_suite;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001543 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001544
1545 cur_term->acl = cur_acl;
1546 cur_term->neg = neg;
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001547
1548 /* Here it is a bit complex. The acl_term_suite is a conjunction
1549 * of many terms. It may only be used if all of its terms are
1550 * usable at the same time. So the suite's validity domain is an
1551 * AND between all ACL keywords' ones. But, the global condition
1552 * is valid if at least one term suite is OK. So it's an OR between
1553 * all of their validity domains. We could emit a warning as soon
1554 * as suite_val is null because it means that the last ACL is not
1555 * compatible with the previous ones. Let's remain simple for now.
1556 */
1557 cond->use |= cur_acl->use;
1558 suite_val &= cur_acl->val;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001559
1560 if (!cur_suite) {
1561 cur_suite = (struct acl_term_suite *)calloc(1, sizeof(*cur_suite));
Willy Tarreauf678b7f2013-01-24 00:25:39 +01001562 if (cur_suite == NULL) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001563 memprintf(err, "out of memory when parsing condition");
Willy Tarreaua84d3742007-05-07 00:36:48 +02001564 goto out_free_term;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001565 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001566 LIST_INIT(&cur_suite->terms);
1567 LIST_ADDQ(&cond->suites, &cur_suite->list);
1568 }
1569 LIST_ADDQ(&cur_suite->terms, &cur_term->list);
Willy Tarreau74b98a82007-06-16 19:35:18 +02001570 neg = 0;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001571 }
1572
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001573 cond->val |= suite_val;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001574 return cond;
1575
1576 out_free_term:
1577 free(cur_term);
1578 out_free_suite:
1579 prune_acl_cond(cond);
1580 free(cond);
1581 out_return:
1582 return NULL;
1583}
1584
Willy Tarreau2bbba412010-01-28 16:48:33 +01001585/* Builds an ACL condition starting at the if/unless keyword. The complete
1586 * condition is returned. NULL is returned in case of error or if the first
1587 * word is neither "if" nor "unless". It automatically sets the file name and
Willy Tarreau25320b22013-03-24 07:22:08 +01001588 * the line number in the condition for better error reporting, and sets the
1589 * HTTP intiailization requirements in the proxy. If <err> is not NULL, it will
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001590 * be filled with a pointer to an error message in case of error, that the
1591 * caller is responsible for freeing. The initial location must either be
1592 * freeable or NULL.
Willy Tarreau2bbba412010-01-28 16:48:33 +01001593 */
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001594struct 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 +01001595{
1596 int pol = ACL_COND_NONE;
1597 struct acl_cond *cond = NULL;
1598
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001599 if (err)
1600 *err = NULL;
1601
Willy Tarreau2bbba412010-01-28 16:48:33 +01001602 if (!strcmp(*args, "if")) {
1603 pol = ACL_COND_IF;
1604 args++;
1605 }
1606 else if (!strcmp(*args, "unless")) {
1607 pol = ACL_COND_UNLESS;
1608 args++;
1609 }
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001610 else {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001611 memprintf(err, "conditions must start with either 'if' or 'unless'");
Willy Tarreau2bbba412010-01-28 16:48:33 +01001612 return NULL;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001613 }
Willy Tarreau2bbba412010-01-28 16:48:33 +01001614
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001615 cond = parse_acl_cond(args, &px->acl, pol, err);
1616 if (!cond) {
1617 /* note that parse_acl_cond must have filled <err> here */
Willy Tarreau2bbba412010-01-28 16:48:33 +01001618 return NULL;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001619 }
Willy Tarreau2bbba412010-01-28 16:48:33 +01001620
1621 cond->file = file;
1622 cond->line = line;
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001623 px->http_needed |= !!(cond->use & SMP_USE_HTTP_ANY);
Willy Tarreau2bbba412010-01-28 16:48:33 +01001624 return cond;
1625}
1626
Willy Tarreau11382812008-07-09 16:18:21 +02001627/* Execute condition <cond> and return either ACL_PAT_FAIL, ACL_PAT_MISS or
Willy Tarreaub6866442008-07-14 23:54:42 +02001628 * ACL_PAT_PASS depending on the test results. ACL_PAT_MISS may only be
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001629 * returned if <opt> does not contain SMP_OPT_FINAL, indicating that incomplete
Willy Tarreau7a777ed2012-04-26 11:44:02 +02001630 * data is being examined. The function automatically sets SMP_OPT_ITERATE.
Willy Tarreaub6866442008-07-14 23:54:42 +02001631 * This function only computes the condition, it does not apply the polarity
1632 * required by IF/UNLESS, it's up to the caller to do this using something like
1633 * this :
Willy Tarreau11382812008-07-09 16:18:21 +02001634 *
1635 * res = acl_pass(res);
Willy Tarreaub6866442008-07-14 23:54:42 +02001636 * if (res == ACL_PAT_MISS)
1637 * return 0;
Willy Tarreau11382812008-07-09 16:18:21 +02001638 * if (cond->pol == ACL_COND_UNLESS)
1639 * res = !res;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001640 */
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001641int 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 +02001642{
1643 __label__ fetch_next;
1644 struct acl_term_suite *suite;
1645 struct acl_term *term;
1646 struct acl_expr *expr;
1647 struct acl *acl;
1648 struct acl_pattern *pattern;
Willy Tarreau37406352012-04-23 16:16:37 +02001649 struct sample smp;
Willy Tarreau11382812008-07-09 16:18:21 +02001650 int acl_res, suite_res, cond_res;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001651
Willy Tarreau7a777ed2012-04-26 11:44:02 +02001652 /* ACLs are iterated over all values, so let's always set the flag to
1653 * indicate this to the fetch functions.
1654 */
1655 opt |= SMP_OPT_ITERATE;
1656
Willy Tarreau11382812008-07-09 16:18:21 +02001657 /* We're doing a logical OR between conditions so we initialize to FAIL.
1658 * The MISS status is propagated down from the suites.
1659 */
Willy Tarreaua84d3742007-05-07 00:36:48 +02001660 cond_res = ACL_PAT_FAIL;
1661 list_for_each_entry(suite, &cond->suites, list) {
Willy Tarreau11382812008-07-09 16:18:21 +02001662 /* Evaluate condition suite <suite>. We stop at the first term
1663 * which returns ACL_PAT_FAIL. The MISS status is still propagated
1664 * in case of uncertainty in the result.
Willy Tarreaua84d3742007-05-07 00:36:48 +02001665 */
1666
1667 /* we're doing a logical AND between terms, so we must set the
1668 * initial value to PASS.
1669 */
1670 suite_res = ACL_PAT_PASS;
1671 list_for_each_entry(term, &suite->terms, list) {
1672 acl = term->acl;
1673
1674 /* FIXME: use cache !
1675 * check acl->cache_idx for this.
1676 */
1677
1678 /* ACL result not cached. Let's scan all the expressions
1679 * and use the first one to match.
1680 */
1681 acl_res = ACL_PAT_FAIL;
1682 list_for_each_entry(expr, &acl->expr, list) {
Willy Tarreaud41f8d82007-06-10 10:06:18 +02001683 /* we need to reset context and flags */
Willy Tarreau37406352012-04-23 16:16:37 +02001684 memset(&smp, 0, sizeof(smp));
Willy Tarreaua84d3742007-05-07 00:36:48 +02001685 fetch_next:
Willy Tarreaud76a98a2013-03-31 18:34:33 +02001686 if (!expr->smp->process(px, l4, l7, opt, expr->args, &smp)) {
Willy Tarreaub6866442008-07-14 23:54:42 +02001687 /* maybe we could not fetch because of missing data */
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001688 if (smp.flags & SMP_F_MAY_CHANGE && !(opt & SMP_OPT_FINAL))
Willy Tarreaub6866442008-07-14 23:54:42 +02001689 acl_res |= ACL_PAT_MISS;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001690 continue;
Willy Tarreaub6866442008-07-14 23:54:42 +02001691 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001692
Willy Tarreau197e10a2012-04-23 19:18:42 +02001693 if (smp.type == SMP_T_BOOL) {
1694 if (smp.data.uint)
Willy Tarreaua79534f2008-07-20 10:13:37 +02001695 acl_res |= ACL_PAT_PASS;
1696 else
1697 acl_res |= ACL_PAT_FAIL;
1698 }
Willy Tarreau5adeda12013-03-31 22:13:34 +02001699 else if (!expr->match) {
1700 /* just check for existence */
1701 acl_res |= ACL_PAT_PASS;
1702 }
Willy Tarreaua79534f2008-07-20 10:13:37 +02001703 else {
Willy Tarreau020534d2010-05-16 21:45:45 +02001704 if (!eb_is_empty(&expr->pattern_tree)) {
Willy Tarreauc4262962010-05-10 23:42:40 +02001705 /* a tree is present, let's check what type it is */
Willy Tarreaud76a98a2013-03-31 18:34:33 +02001706 if (expr->match == acl_match_str)
Willy Tarreau37406352012-04-23 16:16:37 +02001707 acl_res |= acl_lookup_str(&smp, expr) ? ACL_PAT_PASS : ACL_PAT_FAIL;
Willy Tarreaud76a98a2013-03-31 18:34:33 +02001708 else if (expr->match == acl_match_ip)
Willy Tarreau37406352012-04-23 16:16:37 +02001709 acl_res |= acl_lookup_ip(&smp, expr) ? ACL_PAT_PASS : ACL_PAT_FAIL;
Willy Tarreauc4262962010-05-10 23:42:40 +02001710 }
1711
Willy Tarreaua79534f2008-07-20 10:13:37 +02001712 /* call the match() function for all tests on this value */
1713 list_for_each_entry(pattern, &expr->patterns, list) {
Willy Tarreaua79534f2008-07-20 10:13:37 +02001714 if (acl_res == ACL_PAT_PASS)
1715 break;
Willy Tarreaud76a98a2013-03-31 18:34:33 +02001716 acl_res |= expr->match(&smp, pattern);
Willy Tarreaua79534f2008-07-20 10:13:37 +02001717 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001718 }
1719 /*
Willy Tarreau11382812008-07-09 16:18:21 +02001720 * OK now acl_res holds the result of this expression
1721 * as one of ACL_PAT_FAIL, ACL_PAT_MISS or ACL_PAT_PASS.
Willy Tarreaua84d3742007-05-07 00:36:48 +02001722 *
Willy Tarreau11382812008-07-09 16:18:21 +02001723 * Then if (!MISS) we can cache the result, and put
Willy Tarreau37406352012-04-23 16:16:37 +02001724 * (smp.flags & SMP_F_VOLATILE) in the cache flags.
Willy Tarreaua84d3742007-05-07 00:36:48 +02001725 *
1726 * FIXME: implement cache.
1727 *
1728 */
1729
Willy Tarreau11382812008-07-09 16:18:21 +02001730 /* we're ORing these terms, so a single PASS is enough */
1731 if (acl_res == ACL_PAT_PASS)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001732 break;
1733
Willy Tarreau37406352012-04-23 16:16:37 +02001734 if (smp.flags & SMP_F_NOT_LAST)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001735 goto fetch_next;
Willy Tarreaub6866442008-07-14 23:54:42 +02001736
1737 /* sometimes we know the fetched data is subject to change
1738 * later and give another chance for a new match (eg: request
1739 * size, time, ...)
1740 */
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001741 if (smp.flags & SMP_F_MAY_CHANGE && !(opt & SMP_OPT_FINAL))
Willy Tarreaub6866442008-07-14 23:54:42 +02001742 acl_res |= ACL_PAT_MISS;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001743 }
1744 /*
1745 * Here we have the result of an ACL (cached or not).
1746 * ACLs are combined, negated or not, to form conditions.
1747 */
1748
Willy Tarreaua84d3742007-05-07 00:36:48 +02001749 if (term->neg)
Willy Tarreau11382812008-07-09 16:18:21 +02001750 acl_res = acl_neg(acl_res);
Willy Tarreaua84d3742007-05-07 00:36:48 +02001751
1752 suite_res &= acl_res;
Willy Tarreau11382812008-07-09 16:18:21 +02001753
1754 /* we're ANDing these terms, so a single FAIL is enough */
1755 if (suite_res == ACL_PAT_FAIL)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001756 break;
1757 }
1758 cond_res |= suite_res;
Willy Tarreau11382812008-07-09 16:18:21 +02001759
1760 /* we're ORing these terms, so a single PASS is enough */
1761 if (cond_res == ACL_PAT_PASS)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001762 break;
1763 }
Willy Tarreau11382812008-07-09 16:18:21 +02001764 return cond_res;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001765}
1766
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001767/* Returns a pointer to the first ACL conflicting with usage at place <where>
1768 * which is one of the SMP_VAL_* bits indicating a check place, or NULL if
1769 * no conflict is found. Only full conflicts are detected (ACL is not usable).
1770 * Use the next function to check for useless keywords.
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001771 */
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001772const struct acl *acl_cond_conflicts(const struct acl_cond *cond, unsigned int where)
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001773{
1774 struct acl_term_suite *suite;
1775 struct acl_term *term;
1776 struct acl *acl;
1777
1778 list_for_each_entry(suite, &cond->suites, list) {
1779 list_for_each_entry(term, &suite->terms, list) {
1780 acl = term->acl;
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001781 if (!(acl->val & where))
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001782 return acl;
1783 }
1784 }
1785 return NULL;
1786}
1787
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001788/* Returns a pointer to the first ACL and its first keyword to conflict with
1789 * usage at place <where> which is one of the SMP_VAL_* bits indicating a check
1790 * place. Returns true if a conflict is found, with <acl> and <kw> set (if non
1791 * null), or false if not conflict is found. The first useless keyword is
1792 * returned.
1793 */
1794int acl_cond_kw_conflicts(const struct acl_cond *cond, unsigned int where, struct acl const **acl, struct acl_keyword const **kw)
1795{
1796 struct acl_term_suite *suite;
1797 struct acl_term *term;
1798 struct acl_expr *expr;
1799
1800 list_for_each_entry(suite, &cond->suites, list) {
1801 list_for_each_entry(term, &suite->terms, list) {
1802 list_for_each_entry(expr, &term->acl->expr, list) {
Willy Tarreaud76a98a2013-03-31 18:34:33 +02001803 if (!(expr->smp->val & where)) {
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001804 if (acl)
1805 *acl = term->acl;
1806 if (kw)
1807 *kw = expr->kw;
1808 return 1;
1809 }
1810 }
1811 }
1812 }
1813 return 0;
1814}
1815
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001816/*
1817 * Find targets for userlist and groups in acl. Function returns the number
1818 * of errors or OK if everything is fine.
1819 */
1820int
1821acl_find_targets(struct proxy *p)
1822{
1823
1824 struct acl *acl;
1825 struct acl_expr *expr;
1826 struct acl_pattern *pattern;
1827 struct userlist *ul;
Willy Tarreau63364ee2012-04-19 19:11:13 +02001828 struct arg *arg;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001829 int cfgerr = 0;
1830
1831 list_for_each_entry(acl, &p->acl, list) {
1832 list_for_each_entry(expr, &acl->expr, list) {
Willy Tarreau2e845be2012-10-19 19:49:09 +02001833 for (arg = expr->args; arg && arg->type != ARGT_STOP; arg++) {
1834 if (!arg->unresolved)
Willy Tarreau496aa012012-06-01 10:38:29 +02001835 continue;
Willy Tarreau63364ee2012-04-19 19:11:13 +02001836 else if (arg->type == ARGT_SRV) {
1837 struct proxy *px;
1838 struct server *srv;
1839 char *pname, *sname;
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001840
Willy Tarreau7d1df412012-11-23 23:47:36 +01001841 if (!arg->data.str.len) {
Willy Tarreau63364ee2012-04-19 19:11:13 +02001842 Alert("proxy %s: acl '%s' %s(): missing server name.\n",
1843 p->id, acl->name, expr->kw->kw);
1844 cfgerr++;
1845 continue;
1846 }
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001847
Willy Tarreau7d1df412012-11-23 23:47:36 +01001848 pname = arg->data.str.str;
Willy Tarreau63364ee2012-04-19 19:11:13 +02001849 sname = strrchr(pname, '/');
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001850
Willy Tarreau63364ee2012-04-19 19:11:13 +02001851 if (sname)
1852 *sname++ = '\0';
1853 else {
1854 sname = pname;
1855 pname = NULL;
1856 }
1857
1858 px = p;
1859 if (pname) {
1860 px = findproxy(pname, PR_CAP_BE);
1861 if (!px) {
1862 Alert("proxy %s: acl '%s' %s(): unable to find proxy '%s'.\n",
1863 p->id, acl->name, expr->kw->kw, pname);
1864 cfgerr++;
1865 continue;
1866 }
1867 }
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001868
Willy Tarreau63364ee2012-04-19 19:11:13 +02001869 srv = findserver(px, sname);
1870 if (!srv) {
1871 Alert("proxy %s: acl '%s' %s(): unable to find server '%s'.\n",
1872 p->id, acl->name, expr->kw->kw, sname);
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001873 cfgerr++;
1874 continue;
1875 }
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001876
Willy Tarreau7d1df412012-11-23 23:47:36 +01001877 free(arg->data.str.str);
1878 arg->data.str.str = NULL;
Willy Tarreau496aa012012-06-01 10:38:29 +02001879 arg->unresolved = 0;
Willy Tarreau7d1df412012-11-23 23:47:36 +01001880 arg->data.srv = srv;
Willy Tarreaud28c3532012-04-19 19:28:33 +02001881 }
1882 else if (arg->type == ARGT_FE) {
Willy Tarreaufc2c1fd2012-04-19 23:35:54 +02001883 struct proxy *prx = p;
1884 char *pname = p->id;
Willy Tarreaud28c3532012-04-19 19:28:33 +02001885
Willy Tarreau7d1df412012-11-23 23:47:36 +01001886 if (arg->data.str.len) {
1887 pname = arg->data.str.str;
Willy Tarreaufc2c1fd2012-04-19 23:35:54 +02001888 prx = findproxy(pname, PR_CAP_FE);
Willy Tarreaud28c3532012-04-19 19:28:33 +02001889 }
1890
Willy Tarreaud28c3532012-04-19 19:28:33 +02001891 if (!prx) {
1892 Alert("proxy %s: acl '%s' %s(): unable to find frontend '%s'.\n",
1893 p->id, acl->name, expr->kw->kw, pname);
1894 cfgerr++;
1895 continue;
1896 }
1897
Willy Tarreaufc2c1fd2012-04-19 23:35:54 +02001898 if (!(prx->cap & PR_CAP_FE)) {
1899 Alert("proxy %s: acl '%s' %s(): proxy '%s' has no frontend capability.\n",
1900 p->id, acl->name, expr->kw->kw, pname);
1901 cfgerr++;
1902 continue;
1903 }
1904
Willy Tarreau7d1df412012-11-23 23:47:36 +01001905 free(arg->data.str.str);
1906 arg->data.str.str = NULL;
Willy Tarreau496aa012012-06-01 10:38:29 +02001907 arg->unresolved = 0;
Willy Tarreau7d1df412012-11-23 23:47:36 +01001908 arg->data.prx = prx;
Willy Tarreaud28c3532012-04-19 19:28:33 +02001909 }
1910 else if (arg->type == ARGT_BE) {
Willy Tarreaufc2c1fd2012-04-19 23:35:54 +02001911 struct proxy *prx = p;
1912 char *pname = p->id;
Willy Tarreaud28c3532012-04-19 19:28:33 +02001913
Willy Tarreau7d1df412012-11-23 23:47:36 +01001914 if (arg->data.str.len) {
1915 pname = arg->data.str.str;
Willy Tarreaufc2c1fd2012-04-19 23:35:54 +02001916 prx = findproxy(pname, PR_CAP_BE);
Willy Tarreaud28c3532012-04-19 19:28:33 +02001917 }
1918
Willy Tarreaud28c3532012-04-19 19:28:33 +02001919 if (!prx) {
1920 Alert("proxy %s: acl '%s' %s(): unable to find backend '%s'.\n",
1921 p->id, acl->name, expr->kw->kw, pname);
1922 cfgerr++;
1923 continue;
1924 }
1925
Willy Tarreaufc2c1fd2012-04-19 23:35:54 +02001926 if (!(prx->cap & PR_CAP_BE)) {
1927 Alert("proxy %s: acl '%s' %s(): proxy '%s' has no backend capability.\n",
1928 p->id, acl->name, expr->kw->kw, pname);
1929 cfgerr++;
1930 continue;
1931 }
1932
Willy Tarreau7d1df412012-11-23 23:47:36 +01001933 free(arg->data.str.str);
1934 arg->data.str.str = NULL;
Willy Tarreau496aa012012-06-01 10:38:29 +02001935 arg->unresolved = 0;
Willy Tarreau7d1df412012-11-23 23:47:36 +01001936 arg->data.prx = prx;
Willy Tarreaud28c3532012-04-19 19:28:33 +02001937 }
1938 else if (arg->type == ARGT_TAB) {
Willy Tarreaufc2c1fd2012-04-19 23:35:54 +02001939 struct proxy *prx = p;
1940 char *pname = p->id;
Willy Tarreaud28c3532012-04-19 19:28:33 +02001941
Willy Tarreau7d1df412012-11-23 23:47:36 +01001942 if (arg->data.str.len) {
1943 pname = arg->data.str.str;
Willy Tarreaufc2c1fd2012-04-19 23:35:54 +02001944 prx = find_stktable(pname);
Willy Tarreaud28c3532012-04-19 19:28:33 +02001945 }
1946
Willy Tarreaud28c3532012-04-19 19:28:33 +02001947 if (!prx) {
1948 Alert("proxy %s: acl '%s' %s(): unable to find table '%s'.\n",
1949 p->id, acl->name, expr->kw->kw, pname);
1950 cfgerr++;
1951 continue;
1952 }
1953
Willy Tarreaufc2c1fd2012-04-19 23:35:54 +02001954
1955 if (!prx->table.size) {
1956 Alert("proxy %s: acl '%s' %s(): no table in proxy '%s'.\n",
1957 p->id, acl->name, expr->kw->kw, pname);
1958 cfgerr++;
1959 continue;
1960 }
1961
Willy Tarreau7d1df412012-11-23 23:47:36 +01001962 free(arg->data.str.str);
1963 arg->data.str.str = NULL;
Willy Tarreau496aa012012-06-01 10:38:29 +02001964 arg->unresolved = 0;
Willy Tarreau7d1df412012-11-23 23:47:36 +01001965 arg->data.prx = prx;
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001966 }
Willy Tarreau63364ee2012-04-19 19:11:13 +02001967 else if (arg->type == ARGT_USR) {
Willy Tarreau7d1df412012-11-23 23:47:36 +01001968 if (!arg->data.str.len) {
Willy Tarreau63364ee2012-04-19 19:11:13 +02001969 Alert("proxy %s: acl '%s' %s(): missing userlist name.\n",
1970 p->id, acl->name, expr->kw->kw);
1971 cfgerr++;
1972 continue;
1973 }
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001974
Willy Tarreau63364ee2012-04-19 19:11:13 +02001975 if (p->uri_auth && p->uri_auth->userlist &&
Willy Tarreau7d1df412012-11-23 23:47:36 +01001976 !strcmp(p->uri_auth->userlist->name, arg->data.str.str))
Willy Tarreau63364ee2012-04-19 19:11:13 +02001977 ul = p->uri_auth->userlist;
1978 else
Willy Tarreau7d1df412012-11-23 23:47:36 +01001979 ul = auth_find_userlist(arg->data.str.str);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001980
Willy Tarreau63364ee2012-04-19 19:11:13 +02001981 if (!ul) {
1982 Alert("proxy %s: acl '%s' %s(%s): unable to find userlist.\n",
Willy Tarreau7d1df412012-11-23 23:47:36 +01001983 p->id, acl->name, expr->kw->kw, arg->data.str.str);
Willy Tarreau63364ee2012-04-19 19:11:13 +02001984 cfgerr++;
1985 continue;
1986 }
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001987
Willy Tarreau7d1df412012-11-23 23:47:36 +01001988 free(arg->data.str.str);
1989 arg->data.str.str = NULL;
Willy Tarreau496aa012012-06-01 10:38:29 +02001990 arg->unresolved = 0;
Willy Tarreau7d1df412012-11-23 23:47:36 +01001991 arg->data.usr = ul;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001992 }
Willy Tarreau63364ee2012-04-19 19:11:13 +02001993 } /* end of args processing */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001994
Willy Tarreau46b39d02012-05-10 23:40:14 +02001995 /* don't try to resolve groups if we're not certain of having
1996 * resolved userlists first.
1997 */
1998 if (cfgerr)
1999 break;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01002000
2001 if (!strcmp(expr->kw->kw, "http_auth_group")) {
Willy Tarreau63364ee2012-04-19 19:11:13 +02002002 /* note: argument resolved above thanks to ARGT_USR */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01002003
2004 if (LIST_ISEMPTY(&expr->patterns)) {
2005 Alert("proxy %s: acl %s %s(): no groups specified.\n",
2006 p->id, acl->name, expr->kw->kw);
2007 cfgerr++;
2008 continue;
2009 }
2010
2011 list_for_each_entry(pattern, &expr->patterns, list) {
Willy Tarreau7d1df412012-11-23 23:47:36 +01002012 /* this keyword only has one argument */
Willy Tarreau34db1082012-04-19 17:16:54 +02002013 pattern->val.group_mask = auth_resolve_groups(expr->args->data.usr, pattern->ptr.str);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01002014
2015 free(pattern->ptr.str);
2016 pattern->ptr.str = NULL;
2017 pattern->len = 0;
2018
2019 if (!pattern->val.group_mask) {
2020 Alert("proxy %s: acl %s %s(): invalid group(s).\n",
2021 p->id, acl->name, expr->kw->kw);
2022 cfgerr++;
2023 continue;
2024 }
2025 }
2026 }
2027 }
2028 }
2029
2030 return cfgerr;
2031}
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02002032
Willy Tarreau8ed669b2013-01-11 15:49:37 +01002033/* initializes ACLs by resolving the sample fetch names they rely upon.
2034 * Returns 0 on success, otherwise an error.
2035 */
2036int init_acl()
2037{
2038 int err = 0;
2039 int index;
2040 const char *name;
2041 struct acl_kw_list *kwl;
2042 struct sample_fetch *smp;
2043
2044 list_for_each_entry(kwl, &acl_keywords.list, list) {
2045 for (index = 0; kwl->kw[index].kw != NULL; index++) {
2046 name = kwl->kw[index].fetch_kw;
2047 if (!name)
2048 name = kwl->kw[index].kw;
2049
2050 smp = find_sample_fetch(name, strlen(name));
2051 if (!smp) {
2052 Alert("Critical internal error: ACL keyword '%s' relies on sample fetch '%s' which was not registered!\n",
2053 kwl->kw[index].kw, name);
2054 err++;
2055 continue;
2056 }
2057 kwl->kw[index].smp = smp;
2058 }
2059 }
2060 return err;
2061}
Willy Tarreaud4c33c82013-01-07 21:59:07 +01002062
Willy Tarreaua84d3742007-05-07 00:36:48 +02002063/************************************************************************/
Willy Tarreaud4c33c82013-01-07 21:59:07 +01002064/* All supported sample fetch functions must be declared here */
Willy Tarreaua84d3742007-05-07 00:36:48 +02002065/************************************************************************/
2066
Willy Tarreaud4c33c82013-01-07 21:59:07 +01002067/* force TRUE to be returned at the fetch level */
2068static int
2069smp_fetch_true(struct proxy *px, struct session *s, void *l7, unsigned int opt,
2070 const struct arg *args, struct sample *smp)
2071{
2072 smp->type = SMP_T_BOOL;
2073 smp->data.uint = 1;
2074 return 1;
2075}
2076
2077/* force FALSE to be returned at the fetch level */
2078static int
2079smp_fetch_false(struct proxy *px, struct session *s, void *l7, unsigned int opt,
2080 const struct arg *args, struct sample *smp)
2081{
2082 smp->type = SMP_T_BOOL;
2083 smp->data.uint = 0;
2084 return 1;
2085}
2086
2087
2088/************************************************************************/
2089/* All supported sample and ACL keywords must be declared here. */
2090/************************************************************************/
2091
2092/* Note: must not be declared <const> as its list will be overwritten.
2093 * Note: fetches that may return multiple types must be declared as the lowest
2094 * common denominator, the type that can be casted into all other ones. For
2095 * instance IPv4/IPv6 must be declared IPv4.
2096 */
2097static struct sample_fetch_kw_list smp_kws = {{ },{
2098 { "always_false", smp_fetch_false, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
2099 { "always_true", smp_fetch_true, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
2100 { /* END */ },
2101}};
2102
2103
Willy Tarreau61612d42012-04-19 18:42:05 +02002104/* Note: must not be declared <const> as its list will be overwritten.
2105 * Please take care of keeping this list alphabetically sorted.
2106 */
Willy Tarreaua84d3742007-05-07 00:36:48 +02002107static struct acl_kw_list acl_kws = {{ },{
Willy Tarreaud86e29d2013-03-25 08:21:05 +01002108 { "always_false", NULL, acl_parse_nothing, acl_match_nothing },
2109 { "always_true", NULL, acl_parse_nothing, acl_match_nothing },
Willy Tarreaud4c33c82013-01-07 21:59:07 +01002110 { /* END */ },
Willy Tarreaua84d3742007-05-07 00:36:48 +02002111}};
2112
2113
2114__attribute__((constructor))
2115static void __acl_init(void)
2116{
Willy Tarreaud4c33c82013-01-07 21:59:07 +01002117 sample_register_fetches(&smp_kws);
Willy Tarreaua84d3742007-05-07 00:36:48 +02002118 acl_register_keywords(&acl_kws);
2119}
2120
2121
2122/*
2123 * Local variables:
2124 * c-indent-level: 8
2125 * c-basic-offset: 8
2126 * End:
2127 */