blob: 7dc25fb9b4f37e084c582f468d97f2d3e311bd96 [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 Tarreauf3d25982007-05-08 22:45:09 +0200546
Hiroaki Nakamura70351322013-01-13 15:00:42 +0900547 preg = calloc(1, sizeof(*preg));
Willy Tarreauf3d25982007-05-08 22:45:09 +0200548
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200549 if (!preg) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200550 memprintf(err, "out of memory while loading pattern");
Willy Tarreauf3d25982007-05-08 22:45:09 +0200551 return 0;
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200552 }
Willy Tarreauf3d25982007-05-08 22:45:09 +0200553
Thierry FOURNIERed5a4ae2013-10-14 14:07:36 +0200554 if (!regex_comp(*text, preg, !(pattern->flags & ACL_PAT_F_IGNORE_CASE), 0, err)) {
Hiroaki Nakamura70351322013-01-13 15:00:42 +0900555 free(preg);
Hiroaki Nakamura70351322013-01-13 15:00:42 +0900556 return 0;
557 }
558
Willy Tarreauf3d25982007-05-08 22:45:09 +0200559 pattern->ptr.reg = preg;
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +0200560 pattern->freeptrbuf = &acl_free_reg;
Willy Tarreauf3d25982007-05-08 22:45:09 +0200561 return 1;
562}
563
Willy Tarreauae8b7962007-06-09 23:10:04 +0200564/* Parse a range of positive integers delimited by either ':' or '-'. If only
565 * one integer is read, it is set as both min and max. An operator may be
566 * specified as the prefix, among this list of 5 :
567 *
568 * 0:eq, 1:gt, 2:ge, 3:lt, 4:le
569 *
570 * The default operator is "eq". It supports range matching. Ranges are
571 * rejected for other operators. The operator may be changed at any time.
572 * The operator is stored in the 'opaque' argument.
573 *
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200574 * If err is non-NULL, an error message will be returned there on errors and
575 * the caller will have to free it.
576 *
Willy Tarreaua84d3742007-05-07 00:36:48 +0200577 */
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200578int acl_parse_int(const char **text, struct acl_pattern *pattern, int *opaque, char **err)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200579{
Willy Tarreauae8b7962007-06-09 23:10:04 +0200580 signed long long i;
581 unsigned int j, last, skip = 0;
582 const char *ptr = *text;
583
Willy Tarreauc92ddbc2012-04-27 22:10:57 +0200584 pattern->type = SMP_T_UINT;
Willy Tarreau8f8e6452007-06-17 21:51:38 +0200585 while (!isdigit((unsigned char)*ptr)) {
Willy Tarreau1c7cc5b2010-07-18 10:46:33 +0200586 switch (get_std_op(ptr)) {
587 case STD_OP_EQ: *opaque = 0; break;
588 case STD_OP_GT: *opaque = 1; break;
589 case STD_OP_GE: *opaque = 2; break;
590 case STD_OP_LT: *opaque = 3; break;
591 case STD_OP_LE: *opaque = 4; break;
592 default:
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200593 memprintf(err, "'%s' is neither a number nor a supported operator", ptr);
Willy Tarreauae8b7962007-06-09 23:10:04 +0200594 return 0;
Willy Tarreau1c7cc5b2010-07-18 10:46:33 +0200595 }
Willy Tarreauae8b7962007-06-09 23:10:04 +0200596
597 skip++;
598 ptr = text[skip];
599 }
Willy Tarreaua84d3742007-05-07 00:36:48 +0200600
601 last = i = 0;
602 while (1) {
Willy Tarreauae8b7962007-06-09 23:10:04 +0200603 j = *ptr++;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200604 if ((j == '-' || j == ':') && !last) {
605 last++;
606 pattern->val.range.min = i;
607 i = 0;
608 continue;
609 }
610 j -= '0';
611 if (j > 9)
612 // also catches the terminating zero
613 break;
614 i *= 10;
615 i += j;
616 }
Willy Tarreauae8b7962007-06-09 23:10:04 +0200617
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200618 if (last && *opaque >= 1 && *opaque <= 4) {
Willy Tarreauae8b7962007-06-09 23:10:04 +0200619 /* having a range with a min or a max is absurd */
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200620 memprintf(err, "integer range '%s' specified with a comparison operator", text[skip]);
Willy Tarreauae8b7962007-06-09 23:10:04 +0200621 return 0;
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200622 }
Willy Tarreauae8b7962007-06-09 23:10:04 +0200623
Willy Tarreaua84d3742007-05-07 00:36:48 +0200624 if (!last)
625 pattern->val.range.min = i;
626 pattern->val.range.max = i;
Willy Tarreauae8b7962007-06-09 23:10:04 +0200627
628 switch (*opaque) {
629 case 0: /* eq */
630 pattern->val.range.min_set = 1;
631 pattern->val.range.max_set = 1;
632 break;
633 case 1: /* gt */
634 pattern->val.range.min++; /* gt = ge + 1 */
635 case 2: /* ge */
636 pattern->val.range.min_set = 1;
637 pattern->val.range.max_set = 0;
638 break;
639 case 3: /* lt */
640 pattern->val.range.max--; /* lt = le - 1 */
641 case 4: /* le */
642 pattern->val.range.min_set = 0;
643 pattern->val.range.max_set = 1;
644 break;
645 }
646 return skip + 1;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200647}
648
Willy Tarreau4a26d2f2008-07-15 16:05:33 +0200649/* Parse a range of positive 2-component versions delimited by either ':' or
650 * '-'. The version consists in a major and a minor, both of which must be
651 * smaller than 65536, because internally they will be represented as a 32-bit
652 * integer.
653 * If only one version is read, it is set as both min and max. Just like for
654 * pure integers, an operator may be specified as the prefix, among this list
655 * of 5 :
656 *
657 * 0:eq, 1:gt, 2:ge, 3:lt, 4:le
658 *
659 * The default operator is "eq". It supports range matching. Ranges are
660 * rejected for other operators. The operator may be changed at any time.
661 * The operator is stored in the 'opaque' argument. This allows constructs
662 * such as the following one :
663 *
664 * acl obsolete_ssl ssl_req_proto lt 3
665 * acl unsupported_ssl ssl_req_proto gt 3.1
666 * acl valid_ssl ssl_req_proto 3.0-3.1
667 *
668 */
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200669int acl_parse_dotted_ver(const char **text, struct acl_pattern *pattern, int *opaque, char **err)
Willy Tarreau4a26d2f2008-07-15 16:05:33 +0200670{
671 signed long long i;
672 unsigned int j, last, skip = 0;
673 const char *ptr = *text;
674
675
676 while (!isdigit((unsigned char)*ptr)) {
Willy Tarreau1c7cc5b2010-07-18 10:46:33 +0200677 switch (get_std_op(ptr)) {
678 case STD_OP_EQ: *opaque = 0; break;
679 case STD_OP_GT: *opaque = 1; break;
680 case STD_OP_GE: *opaque = 2; break;
681 case STD_OP_LT: *opaque = 3; break;
682 case STD_OP_LE: *opaque = 4; break;
683 default:
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200684 memprintf(err, "'%s' is neither a number nor a supported operator", ptr);
Willy Tarreau4a26d2f2008-07-15 16:05:33 +0200685 return 0;
Willy Tarreau1c7cc5b2010-07-18 10:46:33 +0200686 }
Willy Tarreau4a26d2f2008-07-15 16:05:33 +0200687
688 skip++;
689 ptr = text[skip];
690 }
691
692 last = i = 0;
693 while (1) {
694 j = *ptr++;
695 if (j == '.') {
696 /* minor part */
697 if (i >= 65536)
698 return 0;
699 i <<= 16;
700 continue;
701 }
702 if ((j == '-' || j == ':') && !last) {
703 last++;
704 if (i < 65536)
705 i <<= 16;
706 pattern->val.range.min = i;
707 i = 0;
708 continue;
709 }
710 j -= '0';
711 if (j > 9)
712 // also catches the terminating zero
713 break;
714 i = (i & 0xFFFF0000) + (i & 0xFFFF) * 10;
715 i += j;
716 }
717
718 /* if we only got a major version, let's shift it now */
719 if (i < 65536)
720 i <<= 16;
721
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200722 if (last && *opaque >= 1 && *opaque <= 4) {
Willy Tarreau4a26d2f2008-07-15 16:05:33 +0200723 /* having a range with a min or a max is absurd */
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200724 memprintf(err, "version range '%s' specified with a comparison operator", text[skip]);
Willy Tarreau4a26d2f2008-07-15 16:05:33 +0200725 return 0;
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200726 }
Willy Tarreau4a26d2f2008-07-15 16:05:33 +0200727
728 if (!last)
729 pattern->val.range.min = i;
730 pattern->val.range.max = i;
731
732 switch (*opaque) {
733 case 0: /* eq */
734 pattern->val.range.min_set = 1;
735 pattern->val.range.max_set = 1;
736 break;
737 case 1: /* gt */
738 pattern->val.range.min++; /* gt = ge + 1 */
739 case 2: /* ge */
740 pattern->val.range.min_set = 1;
741 pattern->val.range.max_set = 0;
742 break;
743 case 3: /* lt */
744 pattern->val.range.max--; /* lt = le - 1 */
745 case 4: /* le */
746 pattern->val.range.min_set = 0;
747 pattern->val.range.max_set = 1;
748 break;
749 }
750 return skip + 1;
751}
752
Willy Tarreaua67fad92007-05-08 19:50:09 +0200753/* Parse an IP address and an optional mask in the form addr[/mask].
754 * The addr may either be an IPv4 address or a hostname. The mask
755 * may either be a dotted mask or a number of bits. Returns 1 if OK,
Willy Tarreauc92ddbc2012-04-27 22:10:57 +0200756 * otherwise 0. NOTE: IP address patterns are typed (IPV4/IPV6).
Willy Tarreaua67fad92007-05-08 19:50:09 +0200757 */
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200758int acl_parse_ip(const char **text, struct acl_pattern *pattern, int *opaque, char **err)
Willy Tarreaua67fad92007-05-08 19:50:09 +0200759{
Willy Tarreaub337b532010-05-13 20:03:41 +0200760 struct eb_root *tree = NULL;
761 if (pattern->flags & ACL_PAT_F_TREE_OK)
762 tree = pattern->val.tree;
763
764 if (str2net(*text, &pattern->val.ipv4.addr, &pattern->val.ipv4.mask)) {
765 unsigned int mask = ntohl(pattern->val.ipv4.mask.s_addr);
766 struct ebmb_node *node;
767 /* check if the mask is contiguous so that we can insert the
768 * network into the tree. A continuous mask has only ones on
769 * the left. This means that this mask + its lower bit added
770 * once again is null.
771 */
Willy Tarreauceb4ac92012-04-28 00:41:46 +0200772 pattern->type = SMP_T_IPV4;
Willy Tarreaub337b532010-05-13 20:03:41 +0200773 if (mask + (mask & -mask) == 0 && tree) {
774 mask = mask ? 33 - flsnz(mask & -mask) : 0; /* equals cidr value */
775 /* FIXME: insert <addr>/<mask> into the tree here */
776 node = calloc(1, sizeof(*node) + 4); /* reserve 4 bytes for IPv4 address */
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200777 if (!node) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200778 memprintf(err, "out of memory while loading IPv4 pattern");
Willy Tarreaub337b532010-05-13 20:03:41 +0200779 return 0;
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200780 }
Willy Tarreaub337b532010-05-13 20:03:41 +0200781 memcpy(node->key, &pattern->val.ipv4.addr, 4); /* network byte order */
782 node->node.pfx = mask;
783 if (ebmb_insert_prefix(tree, node, 4) != node)
784 free(node); /* was a duplicate */
785 pattern->flags |= ACL_PAT_F_TREE;
786 return 1;
787 }
Willy Tarreauae8b7962007-06-09 23:10:04 +0200788 return 1;
Willy Tarreaub337b532010-05-13 20:03:41 +0200789 }
Willy Tarreauceb4ac92012-04-28 00:41:46 +0200790 else if (str62net(*text, &pattern->val.ipv6.addr, &pattern->val.ipv6.mask)) {
791 /* no tree support right now */
792 pattern->type = SMP_T_IPV6;
793 return 1;
794 }
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200795 else {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200796 memprintf(err, "'%s' is not a valid IPv4 or IPv6 address", *text);
Willy Tarreauae8b7962007-06-09 23:10:04 +0200797 return 0;
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200798 }
Willy Tarreaua67fad92007-05-08 19:50:09 +0200799}
800
Willy Tarreaua84d3742007-05-07 00:36:48 +0200801/*
802 * Registers the ACL keyword list <kwl> as a list of valid keywords for next
803 * parsing sessions.
804 */
805void acl_register_keywords(struct acl_kw_list *kwl)
806{
807 LIST_ADDQ(&acl_keywords.list, &kwl->list);
808}
809
810/*
811 * Unregisters the ACL keyword list <kwl> from the list of valid keywords.
812 */
813void acl_unregister_keywords(struct acl_kw_list *kwl)
814{
815 LIST_DEL(&kwl->list);
816 LIST_INIT(&kwl->list);
817}
818
819/* Return a pointer to the ACL <name> within the list starting at <head>, or
820 * NULL if not found.
821 */
822struct acl *find_acl_by_name(const char *name, struct list *head)
823{
824 struct acl *acl;
825 list_for_each_entry(acl, head, list) {
826 if (strcmp(acl->name, name) == 0)
827 return acl;
828 }
829 return NULL;
830}
831
832/* Return a pointer to the ACL keyword <kw>, or NULL if not found. Note that if
833 * <kw> contains an opening parenthesis, only the left part of it is checked.
834 */
835struct acl_keyword *find_acl_kw(const char *kw)
836{
837 int index;
838 const char *kwend;
839 struct acl_kw_list *kwl;
840
841 kwend = strchr(kw, '(');
842 if (!kwend)
843 kwend = kw + strlen(kw);
844
845 list_for_each_entry(kwl, &acl_keywords.list, list) {
846 for (index = 0; kwl->kw[index].kw != NULL; index++) {
847 if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) &&
848 kwl->kw[index].kw[kwend-kw] == 0)
849 return &kwl->kw[index];
850 }
851 }
852 return NULL;
853}
854
Willy Tarreaudfd7fca2011-03-09 07:27:02 +0100855/* NB: does nothing if <pat> is NULL */
Willy Tarreaua84d3742007-05-07 00:36:48 +0200856static void free_pattern(struct acl_pattern *pat)
857{
Willy Tarreaudfd7fca2011-03-09 07:27:02 +0100858 if (!pat)
859 return;
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +0200860
861 if (pat->ptr.ptr) {
862 if (pat->freeptrbuf)
863 pat->freeptrbuf(pat->ptr.ptr);
864
Willy Tarreaua84d3742007-05-07 00:36:48 +0200865 free(pat->ptr.ptr);
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +0200866 }
867
Willy Tarreaua84d3742007-05-07 00:36:48 +0200868 free(pat);
869}
870
871static void free_pattern_list(struct list *head)
872{
873 struct acl_pattern *pat, *tmp;
874 list_for_each_entry_safe(pat, tmp, head, list)
875 free_pattern(pat);
876}
877
Willy Tarreaue56cda92010-05-11 23:25:05 +0200878static void free_pattern_tree(struct eb_root *root)
879{
880 struct eb_node *node, *next;
881 node = eb_first(root);
882 while (node) {
883 next = eb_next(node);
884 free(node);
885 node = next;
886 }
887}
888
Willy Tarreaua84d3742007-05-07 00:36:48 +0200889static struct acl_expr *prune_acl_expr(struct acl_expr *expr)
890{
Willy Tarreau34db1082012-04-19 17:16:54 +0200891 struct arg *arg;
892
Willy Tarreaua84d3742007-05-07 00:36:48 +0200893 free_pattern_list(&expr->patterns);
Willy Tarreaue56cda92010-05-11 23:25:05 +0200894 free_pattern_tree(&expr->pattern_tree);
Willy Tarreaua84d3742007-05-07 00:36:48 +0200895 LIST_INIT(&expr->patterns);
Willy Tarreau34db1082012-04-19 17:16:54 +0200896
897 for (arg = expr->args; arg; arg++) {
898 if (arg->type == ARGT_STOP)
899 break;
Willy Tarreau496aa012012-06-01 10:38:29 +0200900 if (arg->type == ARGT_STR || arg->unresolved) {
Willy Tarreau34db1082012-04-19 17:16:54 +0200901 free(arg->data.str.str);
902 arg->data.str.str = NULL;
Willy Tarreau496aa012012-06-01 10:38:29 +0200903 arg->unresolved = 0;
Willy Tarreau34db1082012-04-19 17:16:54 +0200904 }
Willy Tarreau34db1082012-04-19 17:16:54 +0200905 }
906
Willy Tarreau2e845be2012-10-19 19:49:09 +0200907 if (expr->args != empty_arg_list)
908 free(expr->args);
Willy Tarreaua84d3742007-05-07 00:36:48 +0200909 return expr;
910}
911
Willy Tarreau08ad0b32012-04-27 17:25:24 +0200912
913/* Reads patterns from a file. If <err_msg> is non-NULL, an error message will
914 * be returned there on errors and the caller will have to free it.
915 */
Willy Tarreaud76a98a2013-03-31 18:34:33 +0200916static int acl_read_patterns_from_file(struct acl_expr *expr,
917 const char *filename, int patflags,
918 char **err)
Willy Tarreau2b5285d2010-05-09 23:45:24 +0200919{
920 FILE *file;
921 char *c;
922 const char *args[2];
923 struct acl_pattern *pattern;
924 int opaque;
Willy Tarreau6a8097f2011-02-26 15:14:15 +0100925 int ret = 0;
Willy Tarreau08ad0b32012-04-27 17:25:24 +0200926 int line = 0;
Willy Tarreau2b5285d2010-05-09 23:45:24 +0200927
928 file = fopen(filename, "r");
Willy Tarreau08ad0b32012-04-27 17:25:24 +0200929 if (!file) {
930 memprintf(err, "failed to open pattern file <%s>", filename);
Willy Tarreau2b5285d2010-05-09 23:45:24 +0200931 return 0;
Willy Tarreau08ad0b32012-04-27 17:25:24 +0200932 }
Willy Tarreau2b5285d2010-05-09 23:45:24 +0200933
934 /* now parse all patterns. The file may contain only one pattern per
935 * line. If the line contains spaces, they will be part of the pattern.
936 * The pattern stops at the first CR, LF or EOF encountered.
937 */
938 opaque = 0;
Willy Tarreaue56cda92010-05-11 23:25:05 +0200939 pattern = NULL;
Willy Tarreau2b5285d2010-05-09 23:45:24 +0200940 args[1] = "";
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100941 while (fgets(trash.str, trash.size, file) != NULL) {
Willy Tarreau08ad0b32012-04-27 17:25:24 +0200942 line++;
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100943 c = trash.str;
Willy Tarreau58215a02010-05-13 22:07:43 +0200944
945 /* ignore lines beginning with a dash */
946 if (*c == '#')
947 continue;
948
949 /* strip leading spaces and tabs */
950 while (*c == ' ' || *c == '\t')
951 c++;
952
Willy Tarreau58215a02010-05-13 22:07:43 +0200953
954 args[0] = c;
Willy Tarreau2b5285d2010-05-09 23:45:24 +0200955 while (*c && *c != '\n' && *c != '\r')
956 c++;
957 *c = 0;
958
Willy Tarreau51091962011-01-03 21:04:10 +0100959 /* empty lines are ignored too */
960 if (c == args[0])
961 continue;
962
Willy Tarreaue56cda92010-05-11 23:25:05 +0200963 /* we keep the previous pattern along iterations as long as it's not used */
964 if (!pattern)
965 pattern = (struct acl_pattern *)malloc(sizeof(*pattern));
Willy Tarreau08ad0b32012-04-27 17:25:24 +0200966 if (!pattern) {
967 memprintf(err, "out of memory when loading patterns from file <%s>", filename);
Willy Tarreau2b5285d2010-05-09 23:45:24 +0200968 goto out_close;
Willy Tarreau08ad0b32012-04-27 17:25:24 +0200969 }
Willy Tarreaue56cda92010-05-11 23:25:05 +0200970
971 memset(pattern, 0, sizeof(*pattern));
Willy Tarreau2b5285d2010-05-09 23:45:24 +0200972 pattern->flags = patflags;
973
Willy Tarreaue0db1e82013-01-04 16:31:47 +0100974 if (!(pattern->flags & ACL_PAT_F_IGNORE_CASE) &&
Willy Tarreaud76a98a2013-03-31 18:34:33 +0200975 (expr->match == acl_match_str || expr->match == acl_match_ip)) {
Willy Tarreaue56cda92010-05-11 23:25:05 +0200976 /* we pre-set the data pointer to the tree's head so that functions
977 * which are able to insert in a tree know where to do that.
978 */
979 pattern->flags |= ACL_PAT_F_TREE_OK;
980 pattern->val.tree = &expr->pattern_tree;
981 }
982
Willy Tarreauc92ddbc2012-04-27 22:10:57 +0200983 pattern->type = SMP_TYPES; /* unspecified type by default */
Willy Tarreaud76a98a2013-03-31 18:34:33 +0200984 if (!expr->parse(args, pattern, &opaque, err))
Willy Tarreau2b5285d2010-05-09 23:45:24 +0200985 goto out_free_pattern;
Willy Tarreaue56cda92010-05-11 23:25:05 +0200986
987 /* if the parser did not feed the tree, let's chain the pattern to the list */
988 if (!(pattern->flags & ACL_PAT_F_TREE)) {
989 LIST_ADDQ(&expr->patterns, &pattern->list);
990 pattern = NULL; /* get a new one */
991 }
Willy Tarreau2b5285d2010-05-09 23:45:24 +0200992 }
Willy Tarreau6a8097f2011-02-26 15:14:15 +0100993
994 ret = 1; /* success */
Willy Tarreau2b5285d2010-05-09 23:45:24 +0200995
996 out_free_pattern:
997 free_pattern(pattern);
998 out_close:
999 fclose(file);
Willy Tarreau6a8097f2011-02-26 15:14:15 +01001000 return ret;
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001001}
1002
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001003/* Parse an ACL expression starting at <args>[0], and return it. If <err> is
1004 * not NULL, it will be filled with a pointer to an error message in case of
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001005 * error. This pointer must be freeable or NULL. <al> is an arg_list serving
1006 * as a list head to report missing dependencies.
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001007 *
Willy Tarreaua84d3742007-05-07 00:36:48 +02001008 * Right now, the only accepted syntax is :
1009 * <subject> [<value>...]
1010 */
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001011struct acl_expr *parse_acl_expr(const char **args, char **err, struct arg_list *al)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001012{
1013 __label__ out_return, out_free_expr, out_free_pattern;
1014 struct acl_expr *expr;
1015 struct acl_keyword *aclkw;
1016 struct acl_pattern *pattern;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02001017 int opaque, patflags;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001018 const char *arg;
Willy Tarreaubef91e72013-03-31 23:14:46 +02001019 struct sample_fetch *smp = NULL;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001020
Willy Tarreaubef91e72013-03-31 23:14:46 +02001021 /* First, we lookd for an ACL keyword. And if we don't find one, then
1022 * we look for a sample fetch keyword.
1023 */
Willy Tarreaua84d3742007-05-07 00:36:48 +02001024 aclkw = find_acl_kw(args[0]);
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001025 if (!aclkw || !aclkw->parse) {
Willy Tarreaubef91e72013-03-31 23:14:46 +02001026 const char *kwend;
1027
1028 kwend = strchr(args[0], '(');
1029 if (!kwend)
1030 kwend = args[0] + strlen(args[0]);
1031 smp = find_sample_fetch(args[0], kwend - args[0]);
1032
1033 if (!smp) {
1034 memprintf(err, "unknown ACL or sample keyword '%s'", *args);
1035 goto out_return;
1036 }
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001037 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001038
1039 expr = (struct acl_expr *)calloc(1, sizeof(*expr));
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001040 if (!expr) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001041 memprintf(err, "out of memory when parsing ACL expression");
Willy Tarreaua84d3742007-05-07 00:36:48 +02001042 goto out_return;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001043 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001044
Willy Tarreaubef91e72013-03-31 23:14:46 +02001045 expr->kw = aclkw ? aclkw->kw : smp->kw;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001046 LIST_INIT(&expr->patterns);
Willy Tarreaue56cda92010-05-11 23:25:05 +02001047 expr->pattern_tree = EB_ROOT_UNIQUE;
Willy Tarreaubef91e72013-03-31 23:14:46 +02001048 expr->parse = aclkw ? aclkw->parse : NULL;
1049 expr->match = aclkw ? aclkw->match : NULL;
Willy Tarreau2e845be2012-10-19 19:49:09 +02001050 expr->args = empty_arg_list;
Willy Tarreaubef91e72013-03-31 23:14:46 +02001051 expr->smp = aclkw ? aclkw->smp : smp;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001052
Willy Tarreau9987ea92013-06-11 21:09:06 +02001053 if (!expr->parse) {
1054 /* some types can be automatically converted */
1055
1056 switch (expr->smp->out_type) {
1057 case SMP_T_BOOL:
1058 expr->parse = acl_parse_fcts[ACL_MATCH_BOOL];
1059 expr->match = acl_match_fcts[ACL_MATCH_BOOL];
1060 break;
1061 case SMP_T_SINT:
1062 case SMP_T_UINT:
1063 expr->parse = acl_parse_fcts[ACL_MATCH_INT];
1064 expr->match = acl_match_fcts[ACL_MATCH_INT];
1065 break;
1066 case SMP_T_IPV4:
1067 case SMP_T_IPV6:
1068 expr->parse = acl_parse_fcts[ACL_MATCH_IP];
1069 expr->match = acl_match_fcts[ACL_MATCH_IP];
1070 break;
1071 }
1072 }
1073
Willy Tarreaua84d3742007-05-07 00:36:48 +02001074 arg = strchr(args[0], '(');
Willy Tarreaud76a98a2013-03-31 18:34:33 +02001075 if (expr->smp->arg_mask) {
Willy Tarreau61612d42012-04-19 18:42:05 +02001076 int nbargs = 0;
Willy Tarreau34db1082012-04-19 17:16:54 +02001077 char *end;
Willy Tarreau34db1082012-04-19 17:16:54 +02001078
Willy Tarreau61612d42012-04-19 18:42:05 +02001079 if (arg != NULL) {
1080 /* there are 0 or more arguments in the form "subject(arg[,arg]*)" */
1081 arg++;
1082 end = strchr(arg, ')');
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001083 if (!end) {
Willy Tarreau93fddf12013-03-31 22:59:32 +02001084 memprintf(err, "missing closing ')' after arguments to ACL keyword '%s'", expr->kw);
Willy Tarreau61612d42012-04-19 18:42:05 +02001085 goto out_free_expr;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001086 }
Willy Tarreau34db1082012-04-19 17:16:54 +02001087
Willy Tarreau61612d42012-04-19 18:42:05 +02001088 /* Parse the arguments. Note that currently we have no way to
1089 * report parsing errors, hence the NULL in the error pointers.
1090 * An error is also reported if some mandatory arguments are
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001091 * missing. We prepare the args list to report unresolved
1092 * dependencies.
Willy Tarreau61612d42012-04-19 18:42:05 +02001093 */
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001094 al->ctx = ARGC_ACL;
1095 al->kw = expr->kw;
1096 al->conv = NULL;
Willy Tarreaud76a98a2013-03-31 18:34:33 +02001097 nbargs = make_arg_list(arg, end - arg, expr->smp->arg_mask, &expr->args,
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001098 err, NULL, NULL, al);
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001099 if (nbargs < 0) {
1100 /* note that make_arg_list will have set <err> here */
Willy Tarreau93fddf12013-03-31 22:59:32 +02001101 memprintf(err, "in argument to '%s', %s", expr->kw, *err);
Willy Tarreau61612d42012-04-19 18:42:05 +02001102 goto out_free_expr;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001103 }
Willy Tarreauae52f062012-04-26 12:13:35 +02001104
Willy Tarreau2e845be2012-10-19 19:49:09 +02001105 if (!expr->args)
1106 expr->args = empty_arg_list;
1107
Willy Tarreaud76a98a2013-03-31 18:34:33 +02001108 if (expr->smp->val_args && !expr->smp->val_args(expr->args, err)) {
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001109 /* invalid keyword argument, error must have been
1110 * set by val_args().
1111 */
Willy Tarreau93fddf12013-03-31 22:59:32 +02001112 memprintf(err, "in argument to '%s', %s", expr->kw, *err);
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001113 goto out_free_expr;
1114 }
Willy Tarreau61612d42012-04-19 18:42:05 +02001115 }
Willy Tarreaud76a98a2013-03-31 18:34:33 +02001116 else if (ARGM(expr->smp->arg_mask) == 1) {
1117 int type = (expr->smp->arg_mask >> 4) & 15;
Willy Tarreaufc2c1fd2012-04-19 23:35:54 +02001118
1119 /* If a proxy is noted as a mandatory argument, we'll fake
1120 * an empty one so that acl_find_targets() resolves it as
1121 * the current one later.
1122 */
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001123 if (type != ARGT_FE && type != ARGT_BE && type != ARGT_TAB) {
Willy Tarreau93fddf12013-03-31 22:59:32 +02001124 memprintf(err, "ACL keyword '%s' expects %d arguments", expr->kw, ARGM(expr->smp->arg_mask));
Willy Tarreaufc2c1fd2012-04-19 23:35:54 +02001125 goto out_free_expr;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001126 }
Willy Tarreaufc2c1fd2012-04-19 23:35:54 +02001127
1128 /* Build an arg list containing the type as an empty string
1129 * and the usual STOP.
1130 */
1131 expr->args = calloc(2, sizeof(*expr->args));
1132 expr->args[0].type = type;
Willy Tarreaue3a46112012-06-15 08:02:34 +02001133 expr->args[0].unresolved = 1;
Willy Tarreaufc2c1fd2012-04-19 23:35:54 +02001134 expr->args[0].data.str.str = strdup("");
Willy Tarreau8cc16532013-09-29 11:36:53 +02001135 expr->args[0].data.str.size = 1;
Willy Tarreaufc2c1fd2012-04-19 23:35:54 +02001136 expr->args[0].data.str.len = 0;
Willy Tarreauf75d0082013-04-07 21:20:44 +02001137 arg_list_add(al, &expr->args[0], 0);
1138
Willy Tarreaufc2c1fd2012-04-19 23:35:54 +02001139 expr->args[1].type = ARGT_STOP;
1140 }
Willy Tarreaud76a98a2013-03-31 18:34:33 +02001141 else if (ARGM(expr->smp->arg_mask)) {
Willy Tarreau61612d42012-04-19 18:42:05 +02001142 /* there were some mandatory arguments */
Willy Tarreau93fddf12013-03-31 22:59:32 +02001143 memprintf(err, "ACL keyword '%s' expects %d arguments", expr->kw, ARGM(expr->smp->arg_mask));
Willy Tarreaua84d3742007-05-07 00:36:48 +02001144 goto out_free_expr;
Willy Tarreau61612d42012-04-19 18:42:05 +02001145 }
1146 }
1147 else {
1148 if (arg) {
1149 /* no argument expected */
Willy Tarreau93fddf12013-03-31 22:59:32 +02001150 memprintf(err, "ACL keyword '%s' takes no argument", expr->kw);
Willy Tarreau61612d42012-04-19 18:42:05 +02001151 goto out_free_expr;
1152 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001153 }
1154
Willy Tarreaua84d3742007-05-07 00:36:48 +02001155 args++;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02001156
1157 /* check for options before patterns. Supported options are :
1158 * -i : ignore case for all patterns by default
1159 * -f : read patterns from those files
Willy Tarreau5adeda12013-03-31 22:13:34 +02001160 * -m : force matching method (must be used before -f)
Willy Tarreauc8d7c962007-06-17 08:20:33 +02001161 * -- : everything after this is not an option
1162 */
1163 patflags = 0;
1164 while (**args == '-') {
1165 if ((*args)[1] == 'i')
1166 patflags |= ACL_PAT_F_IGNORE_CASE;
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001167 else if ((*args)[1] == 'f') {
Willy Tarreaubef91e72013-03-31 23:14:46 +02001168 if (!expr->parse) {
Willy Tarreau9987ea92013-06-11 21:09:06 +02001169 memprintf(err, "matching method must be specified first (using '-m') when using a sample fetch of this type ('%s')", expr->kw);
Willy Tarreaubef91e72013-03-31 23:14:46 +02001170 goto out_free_expr;
1171 }
1172
Willy Tarreaud76a98a2013-03-31 18:34:33 +02001173 if (!acl_read_patterns_from_file(expr, args[1], patflags | ACL_PAT_F_FROM_FILE, err))
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001174 goto out_free_expr;
Willy Tarreau5adeda12013-03-31 22:13:34 +02001175 args++;
1176 }
1177 else if ((*args)[1] == 'm') {
1178 int idx;
1179
1180 if (!LIST_ISEMPTY(&expr->patterns) || !eb_is_empty(&expr->pattern_tree)) {
1181 memprintf(err, "'-m' must only be specified before patterns and files in parsing ACL expression");
1182 goto out_free_expr;
1183 }
1184
1185 idx = acl_find_match_name(args[1]);
1186 if (idx < 0) {
1187 memprintf(err, "unknown matching method '%s' when parsing ACL expression", args[1]);
1188 goto out_free_expr;
1189 }
1190
1191 /* Note: -m found is always valid, bool/int are compatible, str/bin/reg/len are compatible */
1192 if (idx == ACL_MATCH_FOUND || /* -m found */
1193 ((idx == ACL_MATCH_BOOL || idx == ACL_MATCH_INT) && /* -m bool/int */
1194 (expr->smp->out_type == SMP_T_BOOL ||
1195 expr->smp->out_type == SMP_T_UINT ||
1196 expr->smp->out_type == SMP_T_SINT)) ||
1197 (idx == ACL_MATCH_IP && /* -m ip */
1198 (expr->smp->out_type == SMP_T_IPV4 ||
1199 expr->smp->out_type == SMP_T_IPV6)) ||
1200 ((idx == ACL_MATCH_BIN || idx == ACL_MATCH_LEN || idx == ACL_MATCH_STR ||
1201 idx == ACL_MATCH_BEG || idx == ACL_MATCH_SUB || idx == ACL_MATCH_DIR ||
1202 idx == ACL_MATCH_DOM || idx == ACL_MATCH_END || idx == ACL_MATCH_REG) && /* strings */
1203 (expr->smp->out_type == SMP_T_STR ||
1204 expr->smp->out_type == SMP_T_BIN ||
1205 expr->smp->out_type == SMP_T_CSTR ||
1206 expr->smp->out_type == SMP_T_CBIN))) {
1207 expr->parse = acl_parse_fcts[idx];
1208 expr->match = acl_match_fcts[idx];
1209 }
1210 else {
Willy Tarreau93fddf12013-03-31 22:59:32 +02001211 memprintf(err, "matching method '%s' cannot be used with fetch keyword '%s'", args[1], expr->kw);
Willy Tarreau5adeda12013-03-31 22:13:34 +02001212 goto out_free_expr;
1213 }
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001214 args++;
1215 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +02001216 else if ((*args)[1] == '-') {
1217 args++;
1218 break;
1219 }
1220 else
1221 break;
1222 args++;
1223 }
1224
Willy Tarreaubef91e72013-03-31 23:14:46 +02001225 if (!expr->parse) {
Willy Tarreau9987ea92013-06-11 21:09:06 +02001226 memprintf(err, "matching method must be specified first (using '-m') when using a sample fetch of this type ('%s')", expr->kw);
Willy Tarreaubef91e72013-03-31 23:14:46 +02001227 goto out_free_expr;
1228 }
1229
Willy Tarreauc8d7c962007-06-17 08:20:33 +02001230 /* now parse all patterns */
Willy Tarreauae8b7962007-06-09 23:10:04 +02001231 opaque = 0;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001232 while (**args) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02001233 int ret;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001234 pattern = (struct acl_pattern *)calloc(1, sizeof(*pattern));
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001235 if (!pattern) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001236 memprintf(err, "out of memory when parsing ACL pattern");
Willy Tarreaua84d3742007-05-07 00:36:48 +02001237 goto out_free_expr;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001238 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +02001239 pattern->flags = patflags;
1240
Willy Tarreauc92ddbc2012-04-27 22:10:57 +02001241 pattern->type = SMP_TYPES; /* unspecified type */
Willy Tarreaud76a98a2013-03-31 18:34:33 +02001242 ret = expr->parse(args, pattern, &opaque, err);
Willy Tarreau7dcb6482012-04-27 17:52:25 +02001243 if (!ret)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001244 goto out_free_pattern;
Willy Tarreau7dcb6482012-04-27 17:52:25 +02001245
Willy Tarreaua84d3742007-05-07 00:36:48 +02001246 LIST_ADDQ(&expr->patterns, &pattern->list);
Willy Tarreauae8b7962007-06-09 23:10:04 +02001247 args += ret;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001248 }
1249
1250 return expr;
1251
1252 out_free_pattern:
1253 free_pattern(pattern);
1254 out_free_expr:
1255 prune_acl_expr(expr);
1256 free(expr);
1257 out_return:
1258 return NULL;
1259}
1260
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02001261/* Purge everything in the acl <acl>, then return <acl>. */
1262struct acl *prune_acl(struct acl *acl) {
1263
1264 struct acl_expr *expr, *exprb;
1265
1266 free(acl->name);
1267
1268 list_for_each_entry_safe(expr, exprb, &acl->expr, list) {
1269 LIST_DEL(&expr->list);
1270 prune_acl_expr(expr);
1271 free(expr);
1272 }
1273
1274 return acl;
1275}
1276
Willy Tarreaua84d3742007-05-07 00:36:48 +02001277/* Parse an ACL with the name starting at <args>[0], and with a list of already
1278 * known ACLs in <acl>. If the ACL was not in the list, it will be added.
Willy Tarreau2a56c5e2010-03-15 16:13:29 +01001279 * A pointer to that ACL is returned. If the ACL has an empty name, then it's
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001280 * an anonymous one and it won't be merged with any other one. If <err> is not
1281 * NULL, it will be filled with an appropriate error. This pointer must be
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001282 * freeable or NULL. <al> is the arg_list serving as a head for unresolved
1283 * dependencies.
Willy Tarreaua84d3742007-05-07 00:36:48 +02001284 *
1285 * args syntax: <aclname> <acl_expr>
1286 */
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001287struct acl *parse_acl(const char **args, struct list *known_acl, char **err, struct arg_list *al)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001288{
1289 __label__ out_return, out_free_acl_expr, out_free_name;
1290 struct acl *cur_acl;
1291 struct acl_expr *acl_expr;
1292 char *name;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001293 const char *pos;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001294
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001295 if (**args && (pos = invalid_char(*args))) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001296 memprintf(err, "invalid character in ACL name : '%c'", *pos);
Willy Tarreau2e74c3f2007-12-02 18:45:09 +01001297 goto out_return;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001298 }
Willy Tarreau2e74c3f2007-12-02 18:45:09 +01001299
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001300 acl_expr = parse_acl_expr(args + 1, err, al);
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001301 if (!acl_expr) {
1302 /* parse_acl_expr will have filled <err> here */
Willy Tarreaua84d3742007-05-07 00:36:48 +02001303 goto out_return;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001304 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001305
Willy Tarreau404e8ab2009-07-26 19:40:40 +02001306 /* Check for args beginning with an opening parenthesis just after the
1307 * subject, as this is almost certainly a typo. Right now we can only
1308 * emit a warning, so let's do so.
1309 */
Krzysztof Piotr Oledzki4cdd8312009-10-05 00:23:35 +02001310 if (!strchr(args[1], '(') && *args[2] == '(')
Willy Tarreau404e8ab2009-07-26 19:40:40 +02001311 Warning("parsing acl '%s' :\n"
1312 " matching '%s' for pattern '%s' is likely a mistake and probably\n"
1313 " not what you want. Maybe you need to remove the extraneous space before '('.\n"
1314 " If you are really sure this is not an error, please insert '--' between the\n"
1315 " match and the pattern to make this warning message disappear.\n",
1316 args[0], args[1], args[2]);
1317
Willy Tarreau2a56c5e2010-03-15 16:13:29 +01001318 if (*args[0])
1319 cur_acl = find_acl_by_name(args[0], known_acl);
1320 else
1321 cur_acl = NULL;
1322
Willy Tarreaua84d3742007-05-07 00:36:48 +02001323 if (!cur_acl) {
1324 name = strdup(args[0]);
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001325 if (!name) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001326 memprintf(err, "out of memory when parsing ACL");
Willy Tarreaua84d3742007-05-07 00:36:48 +02001327 goto out_free_acl_expr;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001328 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001329 cur_acl = (struct acl *)calloc(1, sizeof(*cur_acl));
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001330 if (cur_acl == NULL) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001331 memprintf(err, "out of memory when parsing ACL");
Willy Tarreaua84d3742007-05-07 00:36:48 +02001332 goto out_free_name;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001333 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001334
1335 LIST_INIT(&cur_acl->expr);
1336 LIST_ADDQ(known_acl, &cur_acl->list);
1337 cur_acl->name = name;
1338 }
1339
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001340 /* We want to know what features the ACL needs (typically HTTP parsing),
1341 * and where it may be used. If an ACL relies on multiple matches, it is
1342 * OK if at least one of them may match in the context where it is used.
1343 */
Willy Tarreaud76a98a2013-03-31 18:34:33 +02001344 cur_acl->use |= acl_expr->smp->use;
1345 cur_acl->val |= acl_expr->smp->val;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001346 LIST_ADDQ(&cur_acl->expr, &acl_expr->list);
1347 return cur_acl;
1348
1349 out_free_name:
1350 free(name);
1351 out_free_acl_expr:
1352 prune_acl_expr(acl_expr);
1353 free(acl_expr);
1354 out_return:
1355 return NULL;
1356}
1357
Willy Tarreau16fbe822007-06-17 11:54:31 +02001358/* Some useful ACLs provided by default. Only those used are allocated. */
1359
1360const struct {
1361 const char *name;
1362 const char *expr[4]; /* put enough for longest expression */
1363} default_acl_list[] = {
Willy Tarreau58393e12008-07-20 10:39:22 +02001364 { .name = "TRUE", .expr = {"always_true",""}},
1365 { .name = "FALSE", .expr = {"always_false",""}},
Willy Tarreau16fbe822007-06-17 11:54:31 +02001366 { .name = "LOCALHOST", .expr = {"src","127.0.0.1/8",""}},
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001367 { .name = "HTTP", .expr = {"req_proto_http",""}},
Willy Tarreau16fbe822007-06-17 11:54:31 +02001368 { .name = "HTTP_1.0", .expr = {"req_ver","1.0",""}},
1369 { .name = "HTTP_1.1", .expr = {"req_ver","1.1",""}},
1370 { .name = "METH_CONNECT", .expr = {"method","CONNECT",""}},
1371 { .name = "METH_GET", .expr = {"method","GET","HEAD",""}},
1372 { .name = "METH_HEAD", .expr = {"method","HEAD",""}},
1373 { .name = "METH_OPTIONS", .expr = {"method","OPTIONS",""}},
1374 { .name = "METH_POST", .expr = {"method","POST",""}},
1375 { .name = "METH_TRACE", .expr = {"method","TRACE",""}},
1376 { .name = "HTTP_URL_ABS", .expr = {"url_reg","^[^/:]*://",""}},
1377 { .name = "HTTP_URL_SLASH", .expr = {"url_beg","/",""}},
1378 { .name = "HTTP_URL_STAR", .expr = {"url","*",""}},
1379 { .name = "HTTP_CONTENT", .expr = {"hdr_val(content-length)","gt","0",""}},
Emeric Brunbede3d02009-06-30 17:54:00 +02001380 { .name = "RDP_COOKIE", .expr = {"req_rdp_cookie_cnt","gt","0",""}},
Willy Tarreauc6317702008-07-20 09:29:50 +02001381 { .name = "REQ_CONTENT", .expr = {"req_len","gt","0",""}},
Willy Tarreaub6fb4202008-07-20 11:18:28 +02001382 { .name = "WAIT_END", .expr = {"wait_end",""}},
Willy Tarreau16fbe822007-06-17 11:54:31 +02001383 { .name = NULL, .expr = {""}}
1384};
1385
1386/* Find a default ACL from the default_acl list, compile it and return it.
1387 * If the ACL is not found, NULL is returned. In theory, it cannot fail,
1388 * except when default ACLs are broken, in which case it will return NULL.
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001389 * If <known_acl> is not NULL, the ACL will be queued at its tail. If <err> is
1390 * not NULL, it will be filled with an error message if an error occurs. This
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001391 * pointer must be freeable or NULL. <al> is an arg_list serving as a list head
1392 * to report missing dependencies.
Willy Tarreau16fbe822007-06-17 11:54:31 +02001393 */
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001394static struct acl *find_acl_default(const char *acl_name, struct list *known_acl,
1395 char **err, struct arg_list *al)
Willy Tarreau16fbe822007-06-17 11:54:31 +02001396{
1397 __label__ out_return, out_free_acl_expr, out_free_name;
1398 struct acl *cur_acl;
1399 struct acl_expr *acl_expr;
1400 char *name;
1401 int index;
1402
1403 for (index = 0; default_acl_list[index].name != NULL; index++) {
1404 if (strcmp(acl_name, default_acl_list[index].name) == 0)
1405 break;
1406 }
1407
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001408 if (default_acl_list[index].name == NULL) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001409 memprintf(err, "no such ACL : '%s'", acl_name);
Willy Tarreau16fbe822007-06-17 11:54:31 +02001410 return NULL;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001411 }
Willy Tarreau16fbe822007-06-17 11:54:31 +02001412
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001413 acl_expr = parse_acl_expr((const char **)default_acl_list[index].expr, err, al);
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001414 if (!acl_expr) {
1415 /* parse_acl_expr must have filled err here */
Willy Tarreau16fbe822007-06-17 11:54:31 +02001416 goto out_return;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001417 }
Willy Tarreau16fbe822007-06-17 11:54:31 +02001418
1419 name = strdup(acl_name);
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001420 if (!name) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001421 memprintf(err, "out of memory when building default ACL '%s'", acl_name);
Willy Tarreau16fbe822007-06-17 11:54:31 +02001422 goto out_free_acl_expr;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001423 }
1424
Willy Tarreau16fbe822007-06-17 11:54:31 +02001425 cur_acl = (struct acl *)calloc(1, sizeof(*cur_acl));
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001426 if (cur_acl == NULL) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001427 memprintf(err, "out of memory when building default ACL '%s'", acl_name);
Willy Tarreau16fbe822007-06-17 11:54:31 +02001428 goto out_free_name;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001429 }
Willy Tarreau16fbe822007-06-17 11:54:31 +02001430
1431 cur_acl->name = name;
Willy Tarreaud76a98a2013-03-31 18:34:33 +02001432 cur_acl->use |= acl_expr->smp->use;
1433 cur_acl->val |= acl_expr->smp->val;
Willy Tarreau16fbe822007-06-17 11:54:31 +02001434 LIST_INIT(&cur_acl->expr);
1435 LIST_ADDQ(&cur_acl->expr, &acl_expr->list);
1436 if (known_acl)
1437 LIST_ADDQ(known_acl, &cur_acl->list);
1438
1439 return cur_acl;
1440
1441 out_free_name:
1442 free(name);
1443 out_free_acl_expr:
1444 prune_acl_expr(acl_expr);
1445 free(acl_expr);
1446 out_return:
1447 return NULL;
1448}
Willy Tarreaua84d3742007-05-07 00:36:48 +02001449
1450/* Purge everything in the acl_cond <cond>, then return <cond>. */
1451struct acl_cond *prune_acl_cond(struct acl_cond *cond)
1452{
1453 struct acl_term_suite *suite, *tmp_suite;
1454 struct acl_term *term, *tmp_term;
1455
1456 /* iterate through all term suites and free all terms and all suites */
1457 list_for_each_entry_safe(suite, tmp_suite, &cond->suites, list) {
1458 list_for_each_entry_safe(term, tmp_term, &suite->terms, list)
1459 free(term);
1460 free(suite);
1461 }
1462 return cond;
1463}
1464
1465/* Parse an ACL condition starting at <args>[0], relying on a list of already
1466 * known ACLs passed in <known_acl>. The new condition is returned (or NULL in
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001467 * case of low memory). Supports multiple conditions separated by "or". If
1468 * <err> is not NULL, it will be filled with a pointer to an error message in
1469 * case of error, that the caller is responsible for freeing. The initial
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001470 * location must either be freeable or NULL. The list <al> serves as a list head
1471 * for unresolved dependencies.
Willy Tarreaua84d3742007-05-07 00:36:48 +02001472 */
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001473struct acl_cond *parse_acl_cond(const char **args, struct list *known_acl,
1474 int pol, char **err, struct arg_list *al)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001475{
1476 __label__ out_return, out_free_suite, out_free_term;
Willy Tarreau74b98a82007-06-16 19:35:18 +02001477 int arg, neg;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001478 const char *word;
1479 struct acl *cur_acl;
1480 struct acl_term *cur_term;
1481 struct acl_term_suite *cur_suite;
1482 struct acl_cond *cond;
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001483 unsigned int suite_val;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001484
1485 cond = (struct acl_cond *)calloc(1, sizeof(*cond));
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001486 if (cond == NULL) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001487 memprintf(err, "out of memory when parsing condition");
Willy Tarreaua84d3742007-05-07 00:36:48 +02001488 goto out_return;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001489 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001490
1491 LIST_INIT(&cond->list);
1492 LIST_INIT(&cond->suites);
1493 cond->pol = pol;
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001494 cond->val = 0;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001495
1496 cur_suite = NULL;
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001497 suite_val = ~0U;
Willy Tarreau74b98a82007-06-16 19:35:18 +02001498 neg = 0;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001499 for (arg = 0; *args[arg]; arg++) {
1500 word = args[arg];
1501
1502 /* remove as many exclamation marks as we can */
1503 while (*word == '!') {
1504 neg = !neg;
1505 word++;
1506 }
1507
1508 /* an empty word is allowed because we cannot force the user to
1509 * always think about not leaving exclamation marks alone.
1510 */
1511 if (!*word)
1512 continue;
1513
Willy Tarreau16fbe822007-06-17 11:54:31 +02001514 if (strcasecmp(word, "or") == 0 || strcmp(word, "||") == 0) {
Willy Tarreaua84d3742007-05-07 00:36:48 +02001515 /* new term suite */
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001516 cond->val |= suite_val;
1517 suite_val = ~0U;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001518 cur_suite = NULL;
1519 neg = 0;
1520 continue;
1521 }
1522
Willy Tarreau95fa4692010-02-01 13:05:50 +01001523 if (strcmp(word, "{") == 0) {
1524 /* we may have a complete ACL expression between two braces,
1525 * find the last one.
1526 */
1527 int arg_end = arg + 1;
1528 const char **args_new;
1529
1530 while (*args[arg_end] && strcmp(args[arg_end], "}") != 0)
1531 arg_end++;
1532
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001533 if (!*args[arg_end]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001534 memprintf(err, "missing closing '}' in condition");
Willy Tarreau95fa4692010-02-01 13:05:50 +01001535 goto out_free_suite;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001536 }
Willy Tarreau95fa4692010-02-01 13:05:50 +01001537
1538 args_new = calloc(1, (arg_end - arg + 1) * sizeof(*args_new));
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001539 if (!args_new) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001540 memprintf(err, "out of memory when parsing condition");
Willy Tarreau95fa4692010-02-01 13:05:50 +01001541 goto out_free_suite;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001542 }
Willy Tarreau95fa4692010-02-01 13:05:50 +01001543
Willy Tarreau2a56c5e2010-03-15 16:13:29 +01001544 args_new[0] = "";
Willy Tarreau95fa4692010-02-01 13:05:50 +01001545 memcpy(args_new + 1, args + arg + 1, (arg_end - arg) * sizeof(*args_new));
1546 args_new[arg_end - arg] = "";
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001547 cur_acl = parse_acl(args_new, known_acl, err, al);
Willy Tarreau95fa4692010-02-01 13:05:50 +01001548 free(args_new);
1549
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001550 if (!cur_acl) {
1551 /* note that parse_acl() must have filled <err> here */
Willy Tarreau16fbe822007-06-17 11:54:31 +02001552 goto out_free_suite;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001553 }
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001554 word = args[arg + 1];
Willy Tarreau95fa4692010-02-01 13:05:50 +01001555 arg = arg_end;
1556 }
1557 else {
1558 /* search for <word> in the known ACL names. If we do not find
1559 * it, let's look for it in the default ACLs, and if found, add
1560 * it to the list of ACLs of this proxy. This makes it possible
1561 * to override them.
1562 */
1563 cur_acl = find_acl_by_name(word, known_acl);
1564 if (cur_acl == NULL) {
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001565 cur_acl = find_acl_default(word, known_acl, err, al);
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001566 if (cur_acl == NULL) {
1567 /* note that find_acl_default() must have filled <err> here */
Willy Tarreau95fa4692010-02-01 13:05:50 +01001568 goto out_free_suite;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001569 }
Willy Tarreau95fa4692010-02-01 13:05:50 +01001570 }
Willy Tarreau16fbe822007-06-17 11:54:31 +02001571 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001572
1573 cur_term = (struct acl_term *)calloc(1, sizeof(*cur_term));
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001574 if (cur_term == NULL) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001575 memprintf(err, "out of memory when parsing condition");
Willy Tarreaua84d3742007-05-07 00:36:48 +02001576 goto out_free_suite;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001577 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001578
1579 cur_term->acl = cur_acl;
1580 cur_term->neg = neg;
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001581
1582 /* Here it is a bit complex. The acl_term_suite is a conjunction
1583 * of many terms. It may only be used if all of its terms are
1584 * usable at the same time. So the suite's validity domain is an
1585 * AND between all ACL keywords' ones. But, the global condition
1586 * is valid if at least one term suite is OK. So it's an OR between
1587 * all of their validity domains. We could emit a warning as soon
1588 * as suite_val is null because it means that the last ACL is not
1589 * compatible with the previous ones. Let's remain simple for now.
1590 */
1591 cond->use |= cur_acl->use;
1592 suite_val &= cur_acl->val;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001593
1594 if (!cur_suite) {
1595 cur_suite = (struct acl_term_suite *)calloc(1, sizeof(*cur_suite));
Willy Tarreauf678b7f2013-01-24 00:25:39 +01001596 if (cur_suite == NULL) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001597 memprintf(err, "out of memory when parsing condition");
Willy Tarreaua84d3742007-05-07 00:36:48 +02001598 goto out_free_term;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001599 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001600 LIST_INIT(&cur_suite->terms);
1601 LIST_ADDQ(&cond->suites, &cur_suite->list);
1602 }
1603 LIST_ADDQ(&cur_suite->terms, &cur_term->list);
Willy Tarreau74b98a82007-06-16 19:35:18 +02001604 neg = 0;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001605 }
1606
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001607 cond->val |= suite_val;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001608 return cond;
1609
1610 out_free_term:
1611 free(cur_term);
1612 out_free_suite:
1613 prune_acl_cond(cond);
1614 free(cond);
1615 out_return:
1616 return NULL;
1617}
1618
Willy Tarreau2bbba412010-01-28 16:48:33 +01001619/* Builds an ACL condition starting at the if/unless keyword. The complete
1620 * condition is returned. NULL is returned in case of error or if the first
1621 * word is neither "if" nor "unless". It automatically sets the file name and
Willy Tarreau25320b22013-03-24 07:22:08 +01001622 * the line number in the condition for better error reporting, and sets the
1623 * HTTP intiailization requirements in the proxy. If <err> is not NULL, it will
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001624 * be filled with a pointer to an error message in case of error, that the
1625 * caller is responsible for freeing. The initial location must either be
1626 * freeable or NULL.
Willy Tarreau2bbba412010-01-28 16:48:33 +01001627 */
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001628struct 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 +01001629{
1630 int pol = ACL_COND_NONE;
1631 struct acl_cond *cond = NULL;
1632
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001633 if (err)
1634 *err = NULL;
1635
Willy Tarreau2bbba412010-01-28 16:48:33 +01001636 if (!strcmp(*args, "if")) {
1637 pol = ACL_COND_IF;
1638 args++;
1639 }
1640 else if (!strcmp(*args, "unless")) {
1641 pol = ACL_COND_UNLESS;
1642 args++;
1643 }
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001644 else {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001645 memprintf(err, "conditions must start with either 'if' or 'unless'");
Willy Tarreau2bbba412010-01-28 16:48:33 +01001646 return NULL;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001647 }
Willy Tarreau2bbba412010-01-28 16:48:33 +01001648
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001649 cond = parse_acl_cond(args, &px->acl, pol, err, &px->conf.args);
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001650 if (!cond) {
1651 /* note that parse_acl_cond must have filled <err> here */
Willy Tarreau2bbba412010-01-28 16:48:33 +01001652 return NULL;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001653 }
Willy Tarreau2bbba412010-01-28 16:48:33 +01001654
1655 cond->file = file;
1656 cond->line = line;
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001657 px->http_needed |= !!(cond->use & SMP_USE_HTTP_ANY);
Willy Tarreau2bbba412010-01-28 16:48:33 +01001658 return cond;
1659}
1660
Willy Tarreau11382812008-07-09 16:18:21 +02001661/* Execute condition <cond> and return either ACL_PAT_FAIL, ACL_PAT_MISS or
Willy Tarreaub6866442008-07-14 23:54:42 +02001662 * ACL_PAT_PASS depending on the test results. ACL_PAT_MISS may only be
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001663 * returned if <opt> does not contain SMP_OPT_FINAL, indicating that incomplete
Willy Tarreau7a777ed2012-04-26 11:44:02 +02001664 * data is being examined. The function automatically sets SMP_OPT_ITERATE.
Willy Tarreaub6866442008-07-14 23:54:42 +02001665 * This function only computes the condition, it does not apply the polarity
1666 * required by IF/UNLESS, it's up to the caller to do this using something like
1667 * this :
Willy Tarreau11382812008-07-09 16:18:21 +02001668 *
1669 * res = acl_pass(res);
Willy Tarreaub6866442008-07-14 23:54:42 +02001670 * if (res == ACL_PAT_MISS)
1671 * return 0;
Willy Tarreau11382812008-07-09 16:18:21 +02001672 * if (cond->pol == ACL_COND_UNLESS)
1673 * res = !res;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001674 */
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001675int 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 +02001676{
1677 __label__ fetch_next;
1678 struct acl_term_suite *suite;
1679 struct acl_term *term;
1680 struct acl_expr *expr;
1681 struct acl *acl;
1682 struct acl_pattern *pattern;
Willy Tarreau37406352012-04-23 16:16:37 +02001683 struct sample smp;
Willy Tarreau11382812008-07-09 16:18:21 +02001684 int acl_res, suite_res, cond_res;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001685
Willy Tarreau7a777ed2012-04-26 11:44:02 +02001686 /* ACLs are iterated over all values, so let's always set the flag to
1687 * indicate this to the fetch functions.
1688 */
1689 opt |= SMP_OPT_ITERATE;
1690
Willy Tarreau11382812008-07-09 16:18:21 +02001691 /* We're doing a logical OR between conditions so we initialize to FAIL.
1692 * The MISS status is propagated down from the suites.
1693 */
Willy Tarreaua84d3742007-05-07 00:36:48 +02001694 cond_res = ACL_PAT_FAIL;
1695 list_for_each_entry(suite, &cond->suites, list) {
Willy Tarreau11382812008-07-09 16:18:21 +02001696 /* Evaluate condition suite <suite>. We stop at the first term
1697 * which returns ACL_PAT_FAIL. The MISS status is still propagated
1698 * in case of uncertainty in the result.
Willy Tarreaua84d3742007-05-07 00:36:48 +02001699 */
1700
1701 /* we're doing a logical AND between terms, so we must set the
1702 * initial value to PASS.
1703 */
1704 suite_res = ACL_PAT_PASS;
1705 list_for_each_entry(term, &suite->terms, list) {
1706 acl = term->acl;
1707
1708 /* FIXME: use cache !
1709 * check acl->cache_idx for this.
1710 */
1711
1712 /* ACL result not cached. Let's scan all the expressions
1713 * and use the first one to match.
1714 */
1715 acl_res = ACL_PAT_FAIL;
1716 list_for_each_entry(expr, &acl->expr, list) {
Willy Tarreaud41f8d82007-06-10 10:06:18 +02001717 /* we need to reset context and flags */
Willy Tarreau37406352012-04-23 16:16:37 +02001718 memset(&smp, 0, sizeof(smp));
Willy Tarreaua84d3742007-05-07 00:36:48 +02001719 fetch_next:
Willy Tarreauef38c392013-07-22 16:29:32 +02001720 if (!expr->smp->process(px, l4, l7, opt, expr->args, &smp, expr->smp->kw)) {
Willy Tarreaub6866442008-07-14 23:54:42 +02001721 /* maybe we could not fetch because of missing data */
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001722 if (smp.flags & SMP_F_MAY_CHANGE && !(opt & SMP_OPT_FINAL))
Willy Tarreaub6866442008-07-14 23:54:42 +02001723 acl_res |= ACL_PAT_MISS;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001724 continue;
Willy Tarreaub6866442008-07-14 23:54:42 +02001725 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001726
Willy Tarreau24b2c762013-06-12 21:50:19 +02001727 if (expr->match == acl_match_nothing) {
Willy Tarreau197e10a2012-04-23 19:18:42 +02001728 if (smp.data.uint)
Willy Tarreaua79534f2008-07-20 10:13:37 +02001729 acl_res |= ACL_PAT_PASS;
1730 else
1731 acl_res |= ACL_PAT_FAIL;
1732 }
Willy Tarreau5adeda12013-03-31 22:13:34 +02001733 else if (!expr->match) {
1734 /* just check for existence */
1735 acl_res |= ACL_PAT_PASS;
1736 }
Willy Tarreaua79534f2008-07-20 10:13:37 +02001737 else {
Willy Tarreau020534d2010-05-16 21:45:45 +02001738 if (!eb_is_empty(&expr->pattern_tree)) {
Willy Tarreauc4262962010-05-10 23:42:40 +02001739 /* a tree is present, let's check what type it is */
Willy Tarreaud76a98a2013-03-31 18:34:33 +02001740 if (expr->match == acl_match_str)
Willy Tarreau37406352012-04-23 16:16:37 +02001741 acl_res |= acl_lookup_str(&smp, expr) ? ACL_PAT_PASS : ACL_PAT_FAIL;
Willy Tarreaud76a98a2013-03-31 18:34:33 +02001742 else if (expr->match == acl_match_ip)
Willy Tarreau37406352012-04-23 16:16:37 +02001743 acl_res |= acl_lookup_ip(&smp, expr) ? ACL_PAT_PASS : ACL_PAT_FAIL;
Willy Tarreauc4262962010-05-10 23:42:40 +02001744 }
1745
Willy Tarreaua79534f2008-07-20 10:13:37 +02001746 /* call the match() function for all tests on this value */
1747 list_for_each_entry(pattern, &expr->patterns, list) {
Willy Tarreaua79534f2008-07-20 10:13:37 +02001748 if (acl_res == ACL_PAT_PASS)
1749 break;
Willy Tarreaud76a98a2013-03-31 18:34:33 +02001750 acl_res |= expr->match(&smp, pattern);
Willy Tarreaua79534f2008-07-20 10:13:37 +02001751 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001752 }
1753 /*
Willy Tarreau11382812008-07-09 16:18:21 +02001754 * OK now acl_res holds the result of this expression
1755 * as one of ACL_PAT_FAIL, ACL_PAT_MISS or ACL_PAT_PASS.
Willy Tarreaua84d3742007-05-07 00:36:48 +02001756 *
Willy Tarreau11382812008-07-09 16:18:21 +02001757 * Then if (!MISS) we can cache the result, and put
Willy Tarreau37406352012-04-23 16:16:37 +02001758 * (smp.flags & SMP_F_VOLATILE) in the cache flags.
Willy Tarreaua84d3742007-05-07 00:36:48 +02001759 *
1760 * FIXME: implement cache.
1761 *
1762 */
1763
Willy Tarreau11382812008-07-09 16:18:21 +02001764 /* we're ORing these terms, so a single PASS is enough */
1765 if (acl_res == ACL_PAT_PASS)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001766 break;
1767
Willy Tarreau37406352012-04-23 16:16:37 +02001768 if (smp.flags & SMP_F_NOT_LAST)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001769 goto fetch_next;
Willy Tarreaub6866442008-07-14 23:54:42 +02001770
1771 /* sometimes we know the fetched data is subject to change
1772 * later and give another chance for a new match (eg: request
1773 * size, time, ...)
1774 */
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001775 if (smp.flags & SMP_F_MAY_CHANGE && !(opt & SMP_OPT_FINAL))
Willy Tarreaub6866442008-07-14 23:54:42 +02001776 acl_res |= ACL_PAT_MISS;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001777 }
1778 /*
1779 * Here we have the result of an ACL (cached or not).
1780 * ACLs are combined, negated or not, to form conditions.
1781 */
1782
Willy Tarreaua84d3742007-05-07 00:36:48 +02001783 if (term->neg)
Willy Tarreau11382812008-07-09 16:18:21 +02001784 acl_res = acl_neg(acl_res);
Willy Tarreaua84d3742007-05-07 00:36:48 +02001785
1786 suite_res &= acl_res;
Willy Tarreau11382812008-07-09 16:18:21 +02001787
1788 /* we're ANDing these terms, so a single FAIL is enough */
1789 if (suite_res == ACL_PAT_FAIL)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001790 break;
1791 }
1792 cond_res |= suite_res;
Willy Tarreau11382812008-07-09 16:18:21 +02001793
1794 /* we're ORing these terms, so a single PASS is enough */
1795 if (cond_res == ACL_PAT_PASS)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001796 break;
1797 }
Willy Tarreau11382812008-07-09 16:18:21 +02001798 return cond_res;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001799}
1800
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001801/* Returns a pointer to the first ACL conflicting with usage at place <where>
1802 * which is one of the SMP_VAL_* bits indicating a check place, or NULL if
1803 * no conflict is found. Only full conflicts are detected (ACL is not usable).
1804 * Use the next function to check for useless keywords.
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001805 */
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001806const struct acl *acl_cond_conflicts(const struct acl_cond *cond, unsigned int where)
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001807{
1808 struct acl_term_suite *suite;
1809 struct acl_term *term;
1810 struct acl *acl;
1811
1812 list_for_each_entry(suite, &cond->suites, list) {
1813 list_for_each_entry(term, &suite->terms, list) {
1814 acl = term->acl;
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001815 if (!(acl->val & where))
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001816 return acl;
1817 }
1818 }
1819 return NULL;
1820}
1821
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001822/* Returns a pointer to the first ACL and its first keyword to conflict with
1823 * usage at place <where> which is one of the SMP_VAL_* bits indicating a check
1824 * place. Returns true if a conflict is found, with <acl> and <kw> set (if non
1825 * null), or false if not conflict is found. The first useless keyword is
1826 * returned.
1827 */
Willy Tarreau93fddf12013-03-31 22:59:32 +02001828int acl_cond_kw_conflicts(const struct acl_cond *cond, unsigned int where, struct acl const **acl, char const **kw)
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001829{
1830 struct acl_term_suite *suite;
1831 struct acl_term *term;
1832 struct acl_expr *expr;
1833
1834 list_for_each_entry(suite, &cond->suites, list) {
1835 list_for_each_entry(term, &suite->terms, list) {
1836 list_for_each_entry(expr, &term->acl->expr, list) {
Willy Tarreaud76a98a2013-03-31 18:34:33 +02001837 if (!(expr->smp->val & where)) {
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001838 if (acl)
1839 *acl = term->acl;
1840 if (kw)
1841 *kw = expr->kw;
1842 return 1;
1843 }
1844 }
1845 }
1846 }
1847 return 0;
1848}
1849
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001850/*
1851 * Find targets for userlist and groups in acl. Function returns the number
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001852 * of errors or OK if everything is fine. It must be called only once sample
1853 * fetch arguments have been resolved (after smp_resolve_args()).
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001854 */
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001855int acl_find_targets(struct proxy *p)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001856{
1857
1858 struct acl *acl;
1859 struct acl_expr *expr;
1860 struct acl_pattern *pattern;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001861 int cfgerr = 0;
1862
1863 list_for_each_entry(acl, &p->acl, list) {
1864 list_for_each_entry(expr, &acl->expr, list) {
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001865 if (!strcmp(expr->kw, "http_auth_group")) {
1866 /* Note: the ARGT_USR argument may only have been resolved earlier
1867 * by smp_resolve_args().
1868 */
1869 if (expr->args->unresolved) {
1870 Alert("Internal bug in proxy %s: %sacl %s %s() makes use of unresolved userlist '%s'. Please report this.\n",
1871 p->id, *acl->name ? "" : "anonymous ", acl->name, expr->kw, expr->args->data.str.str);
1872 cfgerr++;
Willy Tarreau496aa012012-06-01 10:38:29 +02001873 continue;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001874 }
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001875
1876 if (LIST_ISEMPTY(&expr->patterns)) {
1877 Alert("proxy %s: acl %s %s(): no groups specified.\n",
Willy Tarreau93fddf12013-03-31 22:59:32 +02001878 p->id, acl->name, expr->kw);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001879 cfgerr++;
1880 continue;
1881 }
1882
1883 list_for_each_entry(pattern, &expr->patterns, list) {
Willy Tarreau7d1df412012-11-23 23:47:36 +01001884 /* this keyword only has one argument */
Willy Tarreau34db1082012-04-19 17:16:54 +02001885 pattern->val.group_mask = auth_resolve_groups(expr->args->data.usr, pattern->ptr.str);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001886
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001887 if (!pattern->val.group_mask) {
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001888 Alert("proxy %s: acl %s %s(): invalid group '%s'.\n",
1889 p->id, acl->name, expr->kw, pattern->ptr.str);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001890 cfgerr++;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001891 }
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001892 free(pattern->ptr.str);
1893 pattern->ptr.str = NULL;
1894 pattern->len = 0;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001895 }
1896 }
1897 }
1898 }
1899
1900 return cfgerr;
1901}
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001902
Willy Tarreau8ed669b2013-01-11 15:49:37 +01001903/* initializes ACLs by resolving the sample fetch names they rely upon.
1904 * Returns 0 on success, otherwise an error.
1905 */
1906int init_acl()
1907{
1908 int err = 0;
1909 int index;
1910 const char *name;
1911 struct acl_kw_list *kwl;
1912 struct sample_fetch *smp;
1913
1914 list_for_each_entry(kwl, &acl_keywords.list, list) {
1915 for (index = 0; kwl->kw[index].kw != NULL; index++) {
1916 name = kwl->kw[index].fetch_kw;
1917 if (!name)
1918 name = kwl->kw[index].kw;
1919
1920 smp = find_sample_fetch(name, strlen(name));
1921 if (!smp) {
1922 Alert("Critical internal error: ACL keyword '%s' relies on sample fetch '%s' which was not registered!\n",
1923 kwl->kw[index].kw, name);
1924 err++;
1925 continue;
1926 }
1927 kwl->kw[index].smp = smp;
1928 }
1929 }
1930 return err;
1931}
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001932
Willy Tarreaua84d3742007-05-07 00:36:48 +02001933/************************************************************************/
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001934/* All supported sample and ACL keywords must be declared here. */
1935/************************************************************************/
1936
1937/* Note: must not be declared <const> as its list will be overwritten.
Willy Tarreau61612d42012-04-19 18:42:05 +02001938 * Please take care of keeping this list alphabetically sorted.
1939 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02001940static struct acl_kw_list acl_kws = {ILH, {
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001941 { /* END */ },
Willy Tarreaua84d3742007-05-07 00:36:48 +02001942}};
1943
Willy Tarreaua84d3742007-05-07 00:36:48 +02001944__attribute__((constructor))
1945static void __acl_init(void)
1946{
Willy Tarreaua84d3742007-05-07 00:36:48 +02001947 acl_register_keywords(&acl_kws);
1948}
1949
1950
1951/*
1952 * Local variables:
1953 * c-indent-level: 8
1954 * c-basic-offset: 8
1955 * End:
1956 */