blob: 89bfdf5a0befcfc7bcbc1267f774e93dd07d6f2f [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 Tarreau9ca69362013-10-22 19:10:06 +02001137
1138 al->ctx = ARGC_ACL;
1139 al->kw = expr->kw;
1140 al->conv = NULL;
Willy Tarreauf75d0082013-04-07 21:20:44 +02001141 arg_list_add(al, &expr->args[0], 0);
1142
Willy Tarreaufc2c1fd2012-04-19 23:35:54 +02001143 expr->args[1].type = ARGT_STOP;
1144 }
Willy Tarreaud76a98a2013-03-31 18:34:33 +02001145 else if (ARGM(expr->smp->arg_mask)) {
Willy Tarreau61612d42012-04-19 18:42:05 +02001146 /* there were some mandatory arguments */
Willy Tarreau93fddf12013-03-31 22:59:32 +02001147 memprintf(err, "ACL keyword '%s' expects %d arguments", expr->kw, ARGM(expr->smp->arg_mask));
Willy Tarreaua84d3742007-05-07 00:36:48 +02001148 goto out_free_expr;
Willy Tarreau61612d42012-04-19 18:42:05 +02001149 }
1150 }
1151 else {
1152 if (arg) {
1153 /* no argument expected */
Willy Tarreau93fddf12013-03-31 22:59:32 +02001154 memprintf(err, "ACL keyword '%s' takes no argument", expr->kw);
Willy Tarreau61612d42012-04-19 18:42:05 +02001155 goto out_free_expr;
1156 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001157 }
1158
Willy Tarreaua84d3742007-05-07 00:36:48 +02001159 args++;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02001160
1161 /* check for options before patterns. Supported options are :
1162 * -i : ignore case for all patterns by default
1163 * -f : read patterns from those files
Willy Tarreau5adeda12013-03-31 22:13:34 +02001164 * -m : force matching method (must be used before -f)
Willy Tarreauc8d7c962007-06-17 08:20:33 +02001165 * -- : everything after this is not an option
1166 */
1167 patflags = 0;
1168 while (**args == '-') {
1169 if ((*args)[1] == 'i')
1170 patflags |= ACL_PAT_F_IGNORE_CASE;
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001171 else if ((*args)[1] == 'f') {
Willy Tarreaubef91e72013-03-31 23:14:46 +02001172 if (!expr->parse) {
Willy Tarreau9987ea92013-06-11 21:09:06 +02001173 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 +02001174 goto out_free_expr;
1175 }
1176
Willy Tarreaud76a98a2013-03-31 18:34:33 +02001177 if (!acl_read_patterns_from_file(expr, args[1], patflags | ACL_PAT_F_FROM_FILE, err))
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001178 goto out_free_expr;
Willy Tarreau5adeda12013-03-31 22:13:34 +02001179 args++;
1180 }
1181 else if ((*args)[1] == 'm') {
1182 int idx;
1183
1184 if (!LIST_ISEMPTY(&expr->patterns) || !eb_is_empty(&expr->pattern_tree)) {
1185 memprintf(err, "'-m' must only be specified before patterns and files in parsing ACL expression");
1186 goto out_free_expr;
1187 }
1188
1189 idx = acl_find_match_name(args[1]);
1190 if (idx < 0) {
1191 memprintf(err, "unknown matching method '%s' when parsing ACL expression", args[1]);
1192 goto out_free_expr;
1193 }
1194
1195 /* Note: -m found is always valid, bool/int are compatible, str/bin/reg/len are compatible */
1196 if (idx == ACL_MATCH_FOUND || /* -m found */
1197 ((idx == ACL_MATCH_BOOL || idx == ACL_MATCH_INT) && /* -m bool/int */
1198 (expr->smp->out_type == SMP_T_BOOL ||
1199 expr->smp->out_type == SMP_T_UINT ||
1200 expr->smp->out_type == SMP_T_SINT)) ||
1201 (idx == ACL_MATCH_IP && /* -m ip */
1202 (expr->smp->out_type == SMP_T_IPV4 ||
1203 expr->smp->out_type == SMP_T_IPV6)) ||
1204 ((idx == ACL_MATCH_BIN || idx == ACL_MATCH_LEN || idx == ACL_MATCH_STR ||
1205 idx == ACL_MATCH_BEG || idx == ACL_MATCH_SUB || idx == ACL_MATCH_DIR ||
1206 idx == ACL_MATCH_DOM || idx == ACL_MATCH_END || idx == ACL_MATCH_REG) && /* strings */
1207 (expr->smp->out_type == SMP_T_STR ||
1208 expr->smp->out_type == SMP_T_BIN ||
1209 expr->smp->out_type == SMP_T_CSTR ||
1210 expr->smp->out_type == SMP_T_CBIN))) {
1211 expr->parse = acl_parse_fcts[idx];
1212 expr->match = acl_match_fcts[idx];
1213 }
1214 else {
Willy Tarreau93fddf12013-03-31 22:59:32 +02001215 memprintf(err, "matching method '%s' cannot be used with fetch keyword '%s'", args[1], expr->kw);
Willy Tarreau5adeda12013-03-31 22:13:34 +02001216 goto out_free_expr;
1217 }
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001218 args++;
1219 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +02001220 else if ((*args)[1] == '-') {
1221 args++;
1222 break;
1223 }
1224 else
1225 break;
1226 args++;
1227 }
1228
Willy Tarreaubef91e72013-03-31 23:14:46 +02001229 if (!expr->parse) {
Willy Tarreau9987ea92013-06-11 21:09:06 +02001230 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 +02001231 goto out_free_expr;
1232 }
1233
Willy Tarreauc8d7c962007-06-17 08:20:33 +02001234 /* now parse all patterns */
Willy Tarreauae8b7962007-06-09 23:10:04 +02001235 opaque = 0;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001236 while (**args) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02001237 int ret;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001238 pattern = (struct acl_pattern *)calloc(1, sizeof(*pattern));
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001239 if (!pattern) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001240 memprintf(err, "out of memory when parsing ACL pattern");
Willy Tarreaua84d3742007-05-07 00:36:48 +02001241 goto out_free_expr;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001242 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +02001243 pattern->flags = patflags;
1244
Willy Tarreauc92ddbc2012-04-27 22:10:57 +02001245 pattern->type = SMP_TYPES; /* unspecified type */
Willy Tarreaud76a98a2013-03-31 18:34:33 +02001246 ret = expr->parse(args, pattern, &opaque, err);
Willy Tarreau7dcb6482012-04-27 17:52:25 +02001247 if (!ret)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001248 goto out_free_pattern;
Willy Tarreau7dcb6482012-04-27 17:52:25 +02001249
Willy Tarreaua84d3742007-05-07 00:36:48 +02001250 LIST_ADDQ(&expr->patterns, &pattern->list);
Willy Tarreauae8b7962007-06-09 23:10:04 +02001251 args += ret;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001252 }
1253
1254 return expr;
1255
1256 out_free_pattern:
1257 free_pattern(pattern);
1258 out_free_expr:
1259 prune_acl_expr(expr);
1260 free(expr);
1261 out_return:
1262 return NULL;
1263}
1264
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02001265/* Purge everything in the acl <acl>, then return <acl>. */
1266struct acl *prune_acl(struct acl *acl) {
1267
1268 struct acl_expr *expr, *exprb;
1269
1270 free(acl->name);
1271
1272 list_for_each_entry_safe(expr, exprb, &acl->expr, list) {
1273 LIST_DEL(&expr->list);
1274 prune_acl_expr(expr);
1275 free(expr);
1276 }
1277
1278 return acl;
1279}
1280
Willy Tarreaua84d3742007-05-07 00:36:48 +02001281/* Parse an ACL with the name starting at <args>[0], and with a list of already
1282 * known ACLs in <acl>. If the ACL was not in the list, it will be added.
Willy Tarreau2a56c5e2010-03-15 16:13:29 +01001283 * A pointer to that ACL is returned. If the ACL has an empty name, then it's
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001284 * an anonymous one and it won't be merged with any other one. If <err> is not
1285 * NULL, it will be filled with an appropriate error. This pointer must be
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001286 * freeable or NULL. <al> is the arg_list serving as a head for unresolved
1287 * dependencies.
Willy Tarreaua84d3742007-05-07 00:36:48 +02001288 *
1289 * args syntax: <aclname> <acl_expr>
1290 */
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001291struct acl *parse_acl(const char **args, struct list *known_acl, char **err, struct arg_list *al)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001292{
1293 __label__ out_return, out_free_acl_expr, out_free_name;
1294 struct acl *cur_acl;
1295 struct acl_expr *acl_expr;
1296 char *name;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001297 const char *pos;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001298
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001299 if (**args && (pos = invalid_char(*args))) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001300 memprintf(err, "invalid character in ACL name : '%c'", *pos);
Willy Tarreau2e74c3f2007-12-02 18:45:09 +01001301 goto out_return;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001302 }
Willy Tarreau2e74c3f2007-12-02 18:45:09 +01001303
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001304 acl_expr = parse_acl_expr(args + 1, err, al);
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001305 if (!acl_expr) {
1306 /* parse_acl_expr will have filled <err> here */
Willy Tarreaua84d3742007-05-07 00:36:48 +02001307 goto out_return;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001308 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001309
Willy Tarreau404e8ab2009-07-26 19:40:40 +02001310 /* Check for args beginning with an opening parenthesis just after the
1311 * subject, as this is almost certainly a typo. Right now we can only
1312 * emit a warning, so let's do so.
1313 */
Krzysztof Piotr Oledzki4cdd8312009-10-05 00:23:35 +02001314 if (!strchr(args[1], '(') && *args[2] == '(')
Willy Tarreau404e8ab2009-07-26 19:40:40 +02001315 Warning("parsing acl '%s' :\n"
1316 " matching '%s' for pattern '%s' is likely a mistake and probably\n"
1317 " not what you want. Maybe you need to remove the extraneous space before '('.\n"
1318 " If you are really sure this is not an error, please insert '--' between the\n"
1319 " match and the pattern to make this warning message disappear.\n",
1320 args[0], args[1], args[2]);
1321
Willy Tarreau2a56c5e2010-03-15 16:13:29 +01001322 if (*args[0])
1323 cur_acl = find_acl_by_name(args[0], known_acl);
1324 else
1325 cur_acl = NULL;
1326
Willy Tarreaua84d3742007-05-07 00:36:48 +02001327 if (!cur_acl) {
1328 name = strdup(args[0]);
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001329 if (!name) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001330 memprintf(err, "out of memory when parsing ACL");
Willy Tarreaua84d3742007-05-07 00:36:48 +02001331 goto out_free_acl_expr;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001332 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001333 cur_acl = (struct acl *)calloc(1, sizeof(*cur_acl));
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001334 if (cur_acl == NULL) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001335 memprintf(err, "out of memory when parsing ACL");
Willy Tarreaua84d3742007-05-07 00:36:48 +02001336 goto out_free_name;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001337 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001338
1339 LIST_INIT(&cur_acl->expr);
1340 LIST_ADDQ(known_acl, &cur_acl->list);
1341 cur_acl->name = name;
1342 }
1343
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001344 /* We want to know what features the ACL needs (typically HTTP parsing),
1345 * and where it may be used. If an ACL relies on multiple matches, it is
1346 * OK if at least one of them may match in the context where it is used.
1347 */
Willy Tarreaud76a98a2013-03-31 18:34:33 +02001348 cur_acl->use |= acl_expr->smp->use;
1349 cur_acl->val |= acl_expr->smp->val;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001350 LIST_ADDQ(&cur_acl->expr, &acl_expr->list);
1351 return cur_acl;
1352
1353 out_free_name:
1354 free(name);
1355 out_free_acl_expr:
1356 prune_acl_expr(acl_expr);
1357 free(acl_expr);
1358 out_return:
1359 return NULL;
1360}
1361
Willy Tarreau16fbe822007-06-17 11:54:31 +02001362/* Some useful ACLs provided by default. Only those used are allocated. */
1363
1364const struct {
1365 const char *name;
1366 const char *expr[4]; /* put enough for longest expression */
1367} default_acl_list[] = {
Willy Tarreau58393e12008-07-20 10:39:22 +02001368 { .name = "TRUE", .expr = {"always_true",""}},
1369 { .name = "FALSE", .expr = {"always_false",""}},
Willy Tarreau16fbe822007-06-17 11:54:31 +02001370 { .name = "LOCALHOST", .expr = {"src","127.0.0.1/8",""}},
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001371 { .name = "HTTP", .expr = {"req_proto_http",""}},
Willy Tarreau16fbe822007-06-17 11:54:31 +02001372 { .name = "HTTP_1.0", .expr = {"req_ver","1.0",""}},
1373 { .name = "HTTP_1.1", .expr = {"req_ver","1.1",""}},
1374 { .name = "METH_CONNECT", .expr = {"method","CONNECT",""}},
1375 { .name = "METH_GET", .expr = {"method","GET","HEAD",""}},
1376 { .name = "METH_HEAD", .expr = {"method","HEAD",""}},
1377 { .name = "METH_OPTIONS", .expr = {"method","OPTIONS",""}},
1378 { .name = "METH_POST", .expr = {"method","POST",""}},
1379 { .name = "METH_TRACE", .expr = {"method","TRACE",""}},
1380 { .name = "HTTP_URL_ABS", .expr = {"url_reg","^[^/:]*://",""}},
1381 { .name = "HTTP_URL_SLASH", .expr = {"url_beg","/",""}},
1382 { .name = "HTTP_URL_STAR", .expr = {"url","*",""}},
1383 { .name = "HTTP_CONTENT", .expr = {"hdr_val(content-length)","gt","0",""}},
Emeric Brunbede3d02009-06-30 17:54:00 +02001384 { .name = "RDP_COOKIE", .expr = {"req_rdp_cookie_cnt","gt","0",""}},
Willy Tarreauc6317702008-07-20 09:29:50 +02001385 { .name = "REQ_CONTENT", .expr = {"req_len","gt","0",""}},
Willy Tarreaub6fb4202008-07-20 11:18:28 +02001386 { .name = "WAIT_END", .expr = {"wait_end",""}},
Willy Tarreau16fbe822007-06-17 11:54:31 +02001387 { .name = NULL, .expr = {""}}
1388};
1389
1390/* Find a default ACL from the default_acl list, compile it and return it.
1391 * If the ACL is not found, NULL is returned. In theory, it cannot fail,
1392 * except when default ACLs are broken, in which case it will return NULL.
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001393 * If <known_acl> is not NULL, the ACL will be queued at its tail. If <err> is
1394 * not NULL, it will be filled with an error message if an error occurs. This
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001395 * pointer must be freeable or NULL. <al> is an arg_list serving as a list head
1396 * to report missing dependencies.
Willy Tarreau16fbe822007-06-17 11:54:31 +02001397 */
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001398static struct acl *find_acl_default(const char *acl_name, struct list *known_acl,
1399 char **err, struct arg_list *al)
Willy Tarreau16fbe822007-06-17 11:54:31 +02001400{
1401 __label__ out_return, out_free_acl_expr, out_free_name;
1402 struct acl *cur_acl;
1403 struct acl_expr *acl_expr;
1404 char *name;
1405 int index;
1406
1407 for (index = 0; default_acl_list[index].name != NULL; index++) {
1408 if (strcmp(acl_name, default_acl_list[index].name) == 0)
1409 break;
1410 }
1411
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001412 if (default_acl_list[index].name == NULL) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001413 memprintf(err, "no such ACL : '%s'", acl_name);
Willy Tarreau16fbe822007-06-17 11:54:31 +02001414 return NULL;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001415 }
Willy Tarreau16fbe822007-06-17 11:54:31 +02001416
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001417 acl_expr = parse_acl_expr((const char **)default_acl_list[index].expr, err, al);
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001418 if (!acl_expr) {
1419 /* parse_acl_expr must have filled err here */
Willy Tarreau16fbe822007-06-17 11:54:31 +02001420 goto out_return;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001421 }
Willy Tarreau16fbe822007-06-17 11:54:31 +02001422
1423 name = strdup(acl_name);
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001424 if (!name) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001425 memprintf(err, "out of memory when building default ACL '%s'", acl_name);
Willy Tarreau16fbe822007-06-17 11:54:31 +02001426 goto out_free_acl_expr;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001427 }
1428
Willy Tarreau16fbe822007-06-17 11:54:31 +02001429 cur_acl = (struct acl *)calloc(1, sizeof(*cur_acl));
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001430 if (cur_acl == NULL) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001431 memprintf(err, "out of memory when building default ACL '%s'", acl_name);
Willy Tarreau16fbe822007-06-17 11:54:31 +02001432 goto out_free_name;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001433 }
Willy Tarreau16fbe822007-06-17 11:54:31 +02001434
1435 cur_acl->name = name;
Willy Tarreaud76a98a2013-03-31 18:34:33 +02001436 cur_acl->use |= acl_expr->smp->use;
1437 cur_acl->val |= acl_expr->smp->val;
Willy Tarreau16fbe822007-06-17 11:54:31 +02001438 LIST_INIT(&cur_acl->expr);
1439 LIST_ADDQ(&cur_acl->expr, &acl_expr->list);
1440 if (known_acl)
1441 LIST_ADDQ(known_acl, &cur_acl->list);
1442
1443 return cur_acl;
1444
1445 out_free_name:
1446 free(name);
1447 out_free_acl_expr:
1448 prune_acl_expr(acl_expr);
1449 free(acl_expr);
1450 out_return:
1451 return NULL;
1452}
Willy Tarreaua84d3742007-05-07 00:36:48 +02001453
1454/* Purge everything in the acl_cond <cond>, then return <cond>. */
1455struct acl_cond *prune_acl_cond(struct acl_cond *cond)
1456{
1457 struct acl_term_suite *suite, *tmp_suite;
1458 struct acl_term *term, *tmp_term;
1459
1460 /* iterate through all term suites and free all terms and all suites */
1461 list_for_each_entry_safe(suite, tmp_suite, &cond->suites, list) {
1462 list_for_each_entry_safe(term, tmp_term, &suite->terms, list)
1463 free(term);
1464 free(suite);
1465 }
1466 return cond;
1467}
1468
1469/* Parse an ACL condition starting at <args>[0], relying on a list of already
1470 * known ACLs passed in <known_acl>. The new condition is returned (or NULL in
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001471 * case of low memory). Supports multiple conditions separated by "or". If
1472 * <err> is not NULL, it will be filled with a pointer to an error message in
1473 * case of error, that the caller is responsible for freeing. The initial
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001474 * location must either be freeable or NULL. The list <al> serves as a list head
1475 * for unresolved dependencies.
Willy Tarreaua84d3742007-05-07 00:36:48 +02001476 */
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001477struct acl_cond *parse_acl_cond(const char **args, struct list *known_acl,
1478 int pol, char **err, struct arg_list *al)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001479{
1480 __label__ out_return, out_free_suite, out_free_term;
Willy Tarreau74b98a82007-06-16 19:35:18 +02001481 int arg, neg;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001482 const char *word;
1483 struct acl *cur_acl;
1484 struct acl_term *cur_term;
1485 struct acl_term_suite *cur_suite;
1486 struct acl_cond *cond;
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001487 unsigned int suite_val;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001488
1489 cond = (struct acl_cond *)calloc(1, sizeof(*cond));
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001490 if (cond == NULL) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001491 memprintf(err, "out of memory when parsing condition");
Willy Tarreaua84d3742007-05-07 00:36:48 +02001492 goto out_return;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001493 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001494
1495 LIST_INIT(&cond->list);
1496 LIST_INIT(&cond->suites);
1497 cond->pol = pol;
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001498 cond->val = 0;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001499
1500 cur_suite = NULL;
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001501 suite_val = ~0U;
Willy Tarreau74b98a82007-06-16 19:35:18 +02001502 neg = 0;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001503 for (arg = 0; *args[arg]; arg++) {
1504 word = args[arg];
1505
1506 /* remove as many exclamation marks as we can */
1507 while (*word == '!') {
1508 neg = !neg;
1509 word++;
1510 }
1511
1512 /* an empty word is allowed because we cannot force the user to
1513 * always think about not leaving exclamation marks alone.
1514 */
1515 if (!*word)
1516 continue;
1517
Willy Tarreau16fbe822007-06-17 11:54:31 +02001518 if (strcasecmp(word, "or") == 0 || strcmp(word, "||") == 0) {
Willy Tarreaua84d3742007-05-07 00:36:48 +02001519 /* new term suite */
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001520 cond->val |= suite_val;
1521 suite_val = ~0U;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001522 cur_suite = NULL;
1523 neg = 0;
1524 continue;
1525 }
1526
Willy Tarreau95fa4692010-02-01 13:05:50 +01001527 if (strcmp(word, "{") == 0) {
1528 /* we may have a complete ACL expression between two braces,
1529 * find the last one.
1530 */
1531 int arg_end = arg + 1;
1532 const char **args_new;
1533
1534 while (*args[arg_end] && strcmp(args[arg_end], "}") != 0)
1535 arg_end++;
1536
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001537 if (!*args[arg_end]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001538 memprintf(err, "missing closing '}' in condition");
Willy Tarreau95fa4692010-02-01 13:05:50 +01001539 goto out_free_suite;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001540 }
Willy Tarreau95fa4692010-02-01 13:05:50 +01001541
1542 args_new = calloc(1, (arg_end - arg + 1) * sizeof(*args_new));
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001543 if (!args_new) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001544 memprintf(err, "out of memory when parsing condition");
Willy Tarreau95fa4692010-02-01 13:05:50 +01001545 goto out_free_suite;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001546 }
Willy Tarreau95fa4692010-02-01 13:05:50 +01001547
Willy Tarreau2a56c5e2010-03-15 16:13:29 +01001548 args_new[0] = "";
Willy Tarreau95fa4692010-02-01 13:05:50 +01001549 memcpy(args_new + 1, args + arg + 1, (arg_end - arg) * sizeof(*args_new));
1550 args_new[arg_end - arg] = "";
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001551 cur_acl = parse_acl(args_new, known_acl, err, al);
Willy Tarreau95fa4692010-02-01 13:05:50 +01001552 free(args_new);
1553
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001554 if (!cur_acl) {
1555 /* note that parse_acl() must have filled <err> here */
Willy Tarreau16fbe822007-06-17 11:54:31 +02001556 goto out_free_suite;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001557 }
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001558 word = args[arg + 1];
Willy Tarreau95fa4692010-02-01 13:05:50 +01001559 arg = arg_end;
1560 }
1561 else {
1562 /* search for <word> in the known ACL names. If we do not find
1563 * it, let's look for it in the default ACLs, and if found, add
1564 * it to the list of ACLs of this proxy. This makes it possible
1565 * to override them.
1566 */
1567 cur_acl = find_acl_by_name(word, known_acl);
1568 if (cur_acl == NULL) {
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001569 cur_acl = find_acl_default(word, known_acl, err, al);
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001570 if (cur_acl == NULL) {
1571 /* note that find_acl_default() must have filled <err> here */
Willy Tarreau95fa4692010-02-01 13:05:50 +01001572 goto out_free_suite;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001573 }
Willy Tarreau95fa4692010-02-01 13:05:50 +01001574 }
Willy Tarreau16fbe822007-06-17 11:54:31 +02001575 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001576
1577 cur_term = (struct acl_term *)calloc(1, sizeof(*cur_term));
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001578 if (cur_term == NULL) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001579 memprintf(err, "out of memory when parsing condition");
Willy Tarreaua84d3742007-05-07 00:36:48 +02001580 goto out_free_suite;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001581 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001582
1583 cur_term->acl = cur_acl;
1584 cur_term->neg = neg;
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001585
1586 /* Here it is a bit complex. The acl_term_suite is a conjunction
1587 * of many terms. It may only be used if all of its terms are
1588 * usable at the same time. So the suite's validity domain is an
1589 * AND between all ACL keywords' ones. But, the global condition
1590 * is valid if at least one term suite is OK. So it's an OR between
1591 * all of their validity domains. We could emit a warning as soon
1592 * as suite_val is null because it means that the last ACL is not
1593 * compatible with the previous ones. Let's remain simple for now.
1594 */
1595 cond->use |= cur_acl->use;
1596 suite_val &= cur_acl->val;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001597
1598 if (!cur_suite) {
1599 cur_suite = (struct acl_term_suite *)calloc(1, sizeof(*cur_suite));
Willy Tarreauf678b7f2013-01-24 00:25:39 +01001600 if (cur_suite == NULL) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001601 memprintf(err, "out of memory when parsing condition");
Willy Tarreaua84d3742007-05-07 00:36:48 +02001602 goto out_free_term;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001603 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001604 LIST_INIT(&cur_suite->terms);
1605 LIST_ADDQ(&cond->suites, &cur_suite->list);
1606 }
1607 LIST_ADDQ(&cur_suite->terms, &cur_term->list);
Willy Tarreau74b98a82007-06-16 19:35:18 +02001608 neg = 0;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001609 }
1610
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001611 cond->val |= suite_val;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001612 return cond;
1613
1614 out_free_term:
1615 free(cur_term);
1616 out_free_suite:
1617 prune_acl_cond(cond);
1618 free(cond);
1619 out_return:
1620 return NULL;
1621}
1622
Willy Tarreau2bbba412010-01-28 16:48:33 +01001623/* Builds an ACL condition starting at the if/unless keyword. The complete
1624 * condition is returned. NULL is returned in case of error or if the first
1625 * word is neither "if" nor "unless". It automatically sets the file name and
Willy Tarreau25320b22013-03-24 07:22:08 +01001626 * the line number in the condition for better error reporting, and sets the
1627 * HTTP intiailization requirements in the proxy. If <err> is not NULL, it will
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001628 * be filled with a pointer to an error message in case of error, that the
1629 * caller is responsible for freeing. The initial location must either be
1630 * freeable or NULL.
Willy Tarreau2bbba412010-01-28 16:48:33 +01001631 */
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001632struct 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 +01001633{
1634 int pol = ACL_COND_NONE;
1635 struct acl_cond *cond = NULL;
1636
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001637 if (err)
1638 *err = NULL;
1639
Willy Tarreau2bbba412010-01-28 16:48:33 +01001640 if (!strcmp(*args, "if")) {
1641 pol = ACL_COND_IF;
1642 args++;
1643 }
1644 else if (!strcmp(*args, "unless")) {
1645 pol = ACL_COND_UNLESS;
1646 args++;
1647 }
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001648 else {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001649 memprintf(err, "conditions must start with either 'if' or 'unless'");
Willy Tarreau2bbba412010-01-28 16:48:33 +01001650 return NULL;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001651 }
Willy Tarreau2bbba412010-01-28 16:48:33 +01001652
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001653 cond = parse_acl_cond(args, &px->acl, pol, err, &px->conf.args);
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001654 if (!cond) {
1655 /* note that parse_acl_cond must have filled <err> here */
Willy Tarreau2bbba412010-01-28 16:48:33 +01001656 return NULL;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001657 }
Willy Tarreau2bbba412010-01-28 16:48:33 +01001658
1659 cond->file = file;
1660 cond->line = line;
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001661 px->http_needed |= !!(cond->use & SMP_USE_HTTP_ANY);
Willy Tarreau2bbba412010-01-28 16:48:33 +01001662 return cond;
1663}
1664
Willy Tarreau11382812008-07-09 16:18:21 +02001665/* Execute condition <cond> and return either ACL_PAT_FAIL, ACL_PAT_MISS or
Willy Tarreaub6866442008-07-14 23:54:42 +02001666 * ACL_PAT_PASS depending on the test results. ACL_PAT_MISS may only be
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001667 * returned if <opt> does not contain SMP_OPT_FINAL, indicating that incomplete
Willy Tarreau7a777ed2012-04-26 11:44:02 +02001668 * data is being examined. The function automatically sets SMP_OPT_ITERATE.
Willy Tarreaub6866442008-07-14 23:54:42 +02001669 * This function only computes the condition, it does not apply the polarity
1670 * required by IF/UNLESS, it's up to the caller to do this using something like
1671 * this :
Willy Tarreau11382812008-07-09 16:18:21 +02001672 *
1673 * res = acl_pass(res);
Willy Tarreaub6866442008-07-14 23:54:42 +02001674 * if (res == ACL_PAT_MISS)
1675 * return 0;
Willy Tarreau11382812008-07-09 16:18:21 +02001676 * if (cond->pol == ACL_COND_UNLESS)
1677 * res = !res;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001678 */
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001679int 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 +02001680{
1681 __label__ fetch_next;
1682 struct acl_term_suite *suite;
1683 struct acl_term *term;
1684 struct acl_expr *expr;
1685 struct acl *acl;
1686 struct acl_pattern *pattern;
Willy Tarreau37406352012-04-23 16:16:37 +02001687 struct sample smp;
Willy Tarreau11382812008-07-09 16:18:21 +02001688 int acl_res, suite_res, cond_res;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001689
Willy Tarreau7a777ed2012-04-26 11:44:02 +02001690 /* ACLs are iterated over all values, so let's always set the flag to
1691 * indicate this to the fetch functions.
1692 */
1693 opt |= SMP_OPT_ITERATE;
1694
Willy Tarreau11382812008-07-09 16:18:21 +02001695 /* We're doing a logical OR between conditions so we initialize to FAIL.
1696 * The MISS status is propagated down from the suites.
1697 */
Willy Tarreaua84d3742007-05-07 00:36:48 +02001698 cond_res = ACL_PAT_FAIL;
1699 list_for_each_entry(suite, &cond->suites, list) {
Willy Tarreau11382812008-07-09 16:18:21 +02001700 /* Evaluate condition suite <suite>. We stop at the first term
1701 * which returns ACL_PAT_FAIL. The MISS status is still propagated
1702 * in case of uncertainty in the result.
Willy Tarreaua84d3742007-05-07 00:36:48 +02001703 */
1704
1705 /* we're doing a logical AND between terms, so we must set the
1706 * initial value to PASS.
1707 */
1708 suite_res = ACL_PAT_PASS;
1709 list_for_each_entry(term, &suite->terms, list) {
1710 acl = term->acl;
1711
1712 /* FIXME: use cache !
1713 * check acl->cache_idx for this.
1714 */
1715
1716 /* ACL result not cached. Let's scan all the expressions
1717 * and use the first one to match.
1718 */
1719 acl_res = ACL_PAT_FAIL;
1720 list_for_each_entry(expr, &acl->expr, list) {
Willy Tarreaud41f8d82007-06-10 10:06:18 +02001721 /* we need to reset context and flags */
Willy Tarreau37406352012-04-23 16:16:37 +02001722 memset(&smp, 0, sizeof(smp));
Willy Tarreaua84d3742007-05-07 00:36:48 +02001723 fetch_next:
Willy Tarreauef38c392013-07-22 16:29:32 +02001724 if (!expr->smp->process(px, l4, l7, opt, expr->args, &smp, expr->smp->kw)) {
Willy Tarreaub6866442008-07-14 23:54:42 +02001725 /* maybe we could not fetch because of missing data */
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001726 if (smp.flags & SMP_F_MAY_CHANGE && !(opt & SMP_OPT_FINAL))
Willy Tarreaub6866442008-07-14 23:54:42 +02001727 acl_res |= ACL_PAT_MISS;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001728 continue;
Willy Tarreaub6866442008-07-14 23:54:42 +02001729 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001730
Willy Tarreau24b2c762013-06-12 21:50:19 +02001731 if (expr->match == acl_match_nothing) {
Willy Tarreau197e10a2012-04-23 19:18:42 +02001732 if (smp.data.uint)
Willy Tarreaua79534f2008-07-20 10:13:37 +02001733 acl_res |= ACL_PAT_PASS;
1734 else
1735 acl_res |= ACL_PAT_FAIL;
1736 }
Willy Tarreau5adeda12013-03-31 22:13:34 +02001737 else if (!expr->match) {
1738 /* just check for existence */
1739 acl_res |= ACL_PAT_PASS;
1740 }
Willy Tarreaua79534f2008-07-20 10:13:37 +02001741 else {
Willy Tarreau020534d2010-05-16 21:45:45 +02001742 if (!eb_is_empty(&expr->pattern_tree)) {
Willy Tarreauc4262962010-05-10 23:42:40 +02001743 /* a tree is present, let's check what type it is */
Willy Tarreaud76a98a2013-03-31 18:34:33 +02001744 if (expr->match == acl_match_str)
Willy Tarreau37406352012-04-23 16:16:37 +02001745 acl_res |= acl_lookup_str(&smp, expr) ? ACL_PAT_PASS : ACL_PAT_FAIL;
Willy Tarreaud76a98a2013-03-31 18:34:33 +02001746 else if (expr->match == acl_match_ip)
Willy Tarreau37406352012-04-23 16:16:37 +02001747 acl_res |= acl_lookup_ip(&smp, expr) ? ACL_PAT_PASS : ACL_PAT_FAIL;
Willy Tarreauc4262962010-05-10 23:42:40 +02001748 }
1749
Willy Tarreaua79534f2008-07-20 10:13:37 +02001750 /* call the match() function for all tests on this value */
1751 list_for_each_entry(pattern, &expr->patterns, list) {
Willy Tarreaua79534f2008-07-20 10:13:37 +02001752 if (acl_res == ACL_PAT_PASS)
1753 break;
Willy Tarreaud76a98a2013-03-31 18:34:33 +02001754 acl_res |= expr->match(&smp, pattern);
Willy Tarreaua79534f2008-07-20 10:13:37 +02001755 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001756 }
1757 /*
Willy Tarreau11382812008-07-09 16:18:21 +02001758 * OK now acl_res holds the result of this expression
1759 * as one of ACL_PAT_FAIL, ACL_PAT_MISS or ACL_PAT_PASS.
Willy Tarreaua84d3742007-05-07 00:36:48 +02001760 *
Willy Tarreau11382812008-07-09 16:18:21 +02001761 * Then if (!MISS) we can cache the result, and put
Willy Tarreau37406352012-04-23 16:16:37 +02001762 * (smp.flags & SMP_F_VOLATILE) in the cache flags.
Willy Tarreaua84d3742007-05-07 00:36:48 +02001763 *
1764 * FIXME: implement cache.
1765 *
1766 */
1767
Willy Tarreau11382812008-07-09 16:18:21 +02001768 /* we're ORing these terms, so a single PASS is enough */
1769 if (acl_res == ACL_PAT_PASS)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001770 break;
1771
Willy Tarreau37406352012-04-23 16:16:37 +02001772 if (smp.flags & SMP_F_NOT_LAST)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001773 goto fetch_next;
Willy Tarreaub6866442008-07-14 23:54:42 +02001774
1775 /* sometimes we know the fetched data is subject to change
1776 * later and give another chance for a new match (eg: request
1777 * size, time, ...)
1778 */
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001779 if (smp.flags & SMP_F_MAY_CHANGE && !(opt & SMP_OPT_FINAL))
Willy Tarreaub6866442008-07-14 23:54:42 +02001780 acl_res |= ACL_PAT_MISS;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001781 }
1782 /*
1783 * Here we have the result of an ACL (cached or not).
1784 * ACLs are combined, negated or not, to form conditions.
1785 */
1786
Willy Tarreaua84d3742007-05-07 00:36:48 +02001787 if (term->neg)
Willy Tarreau11382812008-07-09 16:18:21 +02001788 acl_res = acl_neg(acl_res);
Willy Tarreaua84d3742007-05-07 00:36:48 +02001789
1790 suite_res &= acl_res;
Willy Tarreau11382812008-07-09 16:18:21 +02001791
1792 /* we're ANDing these terms, so a single FAIL is enough */
1793 if (suite_res == ACL_PAT_FAIL)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001794 break;
1795 }
1796 cond_res |= suite_res;
Willy Tarreau11382812008-07-09 16:18:21 +02001797
1798 /* we're ORing these terms, so a single PASS is enough */
1799 if (cond_res == ACL_PAT_PASS)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001800 break;
1801 }
Willy Tarreau11382812008-07-09 16:18:21 +02001802 return cond_res;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001803}
1804
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001805/* Returns a pointer to the first ACL conflicting with usage at place <where>
1806 * which is one of the SMP_VAL_* bits indicating a check place, or NULL if
1807 * no conflict is found. Only full conflicts are detected (ACL is not usable).
1808 * Use the next function to check for useless keywords.
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001809 */
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001810const struct acl *acl_cond_conflicts(const struct acl_cond *cond, unsigned int where)
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001811{
1812 struct acl_term_suite *suite;
1813 struct acl_term *term;
1814 struct acl *acl;
1815
1816 list_for_each_entry(suite, &cond->suites, list) {
1817 list_for_each_entry(term, &suite->terms, list) {
1818 acl = term->acl;
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001819 if (!(acl->val & where))
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001820 return acl;
1821 }
1822 }
1823 return NULL;
1824}
1825
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001826/* Returns a pointer to the first ACL and its first keyword to conflict with
1827 * usage at place <where> which is one of the SMP_VAL_* bits indicating a check
1828 * place. Returns true if a conflict is found, with <acl> and <kw> set (if non
1829 * null), or false if not conflict is found. The first useless keyword is
1830 * returned.
1831 */
Willy Tarreau93fddf12013-03-31 22:59:32 +02001832int 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 +01001833{
1834 struct acl_term_suite *suite;
1835 struct acl_term *term;
1836 struct acl_expr *expr;
1837
1838 list_for_each_entry(suite, &cond->suites, list) {
1839 list_for_each_entry(term, &suite->terms, list) {
1840 list_for_each_entry(expr, &term->acl->expr, list) {
Willy Tarreaud76a98a2013-03-31 18:34:33 +02001841 if (!(expr->smp->val & where)) {
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001842 if (acl)
1843 *acl = term->acl;
1844 if (kw)
1845 *kw = expr->kw;
1846 return 1;
1847 }
1848 }
1849 }
1850 }
1851 return 0;
1852}
1853
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001854/*
1855 * Find targets for userlist and groups in acl. Function returns the number
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001856 * of errors or OK if everything is fine. It must be called only once sample
1857 * fetch arguments have been resolved (after smp_resolve_args()).
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001858 */
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001859int acl_find_targets(struct proxy *p)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001860{
1861
1862 struct acl *acl;
1863 struct acl_expr *expr;
1864 struct acl_pattern *pattern;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001865 int cfgerr = 0;
1866
1867 list_for_each_entry(acl, &p->acl, list) {
1868 list_for_each_entry(expr, &acl->expr, list) {
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001869 if (!strcmp(expr->kw, "http_auth_group")) {
1870 /* Note: the ARGT_USR argument may only have been resolved earlier
1871 * by smp_resolve_args().
1872 */
1873 if (expr->args->unresolved) {
1874 Alert("Internal bug in proxy %s: %sacl %s %s() makes use of unresolved userlist '%s'. Please report this.\n",
1875 p->id, *acl->name ? "" : "anonymous ", acl->name, expr->kw, expr->args->data.str.str);
1876 cfgerr++;
Willy Tarreau496aa012012-06-01 10:38:29 +02001877 continue;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001878 }
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001879
1880 if (LIST_ISEMPTY(&expr->patterns)) {
1881 Alert("proxy %s: acl %s %s(): no groups specified.\n",
Willy Tarreau93fddf12013-03-31 22:59:32 +02001882 p->id, acl->name, expr->kw);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001883 cfgerr++;
1884 continue;
1885 }
1886
1887 list_for_each_entry(pattern, &expr->patterns, list) {
Willy Tarreau7d1df412012-11-23 23:47:36 +01001888 /* this keyword only has one argument */
Willy Tarreau34db1082012-04-19 17:16:54 +02001889 pattern->val.group_mask = auth_resolve_groups(expr->args->data.usr, pattern->ptr.str);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001890
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001891 if (!pattern->val.group_mask) {
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001892 Alert("proxy %s: acl %s %s(): invalid group '%s'.\n",
1893 p->id, acl->name, expr->kw, pattern->ptr.str);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001894 cfgerr++;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001895 }
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001896 free(pattern->ptr.str);
1897 pattern->ptr.str = NULL;
1898 pattern->len = 0;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001899 }
1900 }
1901 }
1902 }
1903
1904 return cfgerr;
1905}
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001906
Willy Tarreau8ed669b2013-01-11 15:49:37 +01001907/* initializes ACLs by resolving the sample fetch names they rely upon.
1908 * Returns 0 on success, otherwise an error.
1909 */
1910int init_acl()
1911{
1912 int err = 0;
1913 int index;
1914 const char *name;
1915 struct acl_kw_list *kwl;
1916 struct sample_fetch *smp;
1917
1918 list_for_each_entry(kwl, &acl_keywords.list, list) {
1919 for (index = 0; kwl->kw[index].kw != NULL; index++) {
1920 name = kwl->kw[index].fetch_kw;
1921 if (!name)
1922 name = kwl->kw[index].kw;
1923
1924 smp = find_sample_fetch(name, strlen(name));
1925 if (!smp) {
1926 Alert("Critical internal error: ACL keyword '%s' relies on sample fetch '%s' which was not registered!\n",
1927 kwl->kw[index].kw, name);
1928 err++;
1929 continue;
1930 }
1931 kwl->kw[index].smp = smp;
1932 }
1933 }
1934 return err;
1935}
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001936
Willy Tarreaua84d3742007-05-07 00:36:48 +02001937/************************************************************************/
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001938/* All supported sample and ACL keywords must be declared here. */
1939/************************************************************************/
1940
1941/* Note: must not be declared <const> as its list will be overwritten.
Willy Tarreau61612d42012-04-19 18:42:05 +02001942 * Please take care of keeping this list alphabetically sorted.
1943 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02001944static struct acl_kw_list acl_kws = {ILH, {
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001945 { /* END */ },
Willy Tarreaua84d3742007-05-07 00:36:48 +02001946}};
1947
Willy Tarreaua84d3742007-05-07 00:36:48 +02001948__attribute__((constructor))
1949static void __acl_init(void)
1950{
Willy Tarreaua84d3742007-05-07 00:36:48 +02001951 acl_register_keywords(&acl_kws);
1952}
1953
1954
1955/*
1956 * Local variables:
1957 * c-indent-level: 8
1958 * c-basic-offset: 8
1959 * End:
1960 */