blob: 4a53072024e16e79f2b310bf332e2d988290e1ab [file] [log] [blame]
Willy Tarreaua84d3742007-05-07 00:36:48 +02001/*
2 * ACL management functions.
3 *
Willy Tarreaud4c33c82013-01-07 21:59:07 +01004 * Copyright 2000-2013 Willy Tarreau <w@1wt.eu>
Willy Tarreaua84d3742007-05-07 00:36:48 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
Willy Tarreauae8b7962007-06-09 23:10:04 +020013#include <ctype.h>
Willy Tarreaua84d3742007-05-07 00:36:48 +020014#include <stdio.h>
15#include <string.h>
16
17#include <common/config.h>
18#include <common/mini-clist.h>
19#include <common/standard.h>
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +010020#include <common/uri_auth.h>
Willy Tarreaua84d3742007-05-07 00:36:48 +020021
Willy Tarreau2b5285d2010-05-09 23:45:24 +020022#include <types/global.h>
23
Willy Tarreaua84d3742007-05-07 00:36:48 +020024#include <proto/acl.h>
Willy Tarreau34db1082012-04-19 17:16:54 +020025#include <proto/arg.h>
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010026#include <proto/auth.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020027#include <proto/channel.h>
Willy Tarreau404e8ab2009-07-26 19:40:40 +020028#include <proto/log.h>
Willy Tarreau0b1cd942010-05-16 22:18:27 +020029#include <proto/proxy.h>
Willy Tarreau8ed669b2013-01-11 15:49:37 +010030#include <proto/sample.h>
Willy Tarreaud28c3532012-04-19 19:28:33 +020031#include <proto/stick_table.h>
Willy Tarreaua84d3742007-05-07 00:36:48 +020032
Willy Tarreauc4262962010-05-10 23:42:40 +020033#include <ebsttree.h>
34
Willy Tarreaua84d3742007-05-07 00:36:48 +020035/* List head of all known ACL keywords */
36static struct acl_kw_list acl_keywords = {
37 .list = LIST_HEAD_INIT(acl_keywords.list)
38};
39
Willy Tarreau5adeda12013-03-31 22:13:34 +020040static char *acl_match_names[ACL_MATCH_NUM] = {
41 [ACL_MATCH_FOUND] = "found",
42 [ACL_MATCH_BOOL] = "bool",
43 [ACL_MATCH_INT] = "int",
44 [ACL_MATCH_IP] = "ip",
45 [ACL_MATCH_BIN] = "bin",
46 [ACL_MATCH_LEN] = "len",
47 [ACL_MATCH_STR] = "str",
48 [ACL_MATCH_BEG] = "beg",
49 [ACL_MATCH_SUB] = "sub",
50 [ACL_MATCH_DIR] = "dir",
51 [ACL_MATCH_DOM] = "dom",
52 [ACL_MATCH_END] = "end",
53 [ACL_MATCH_REG] = "reg",
54};
55
56static int (*acl_parse_fcts[ACL_MATCH_NUM])(const char **, struct acl_pattern *, int *, char **) = {
57 [ACL_MATCH_FOUND] = acl_parse_nothing,
58 [ACL_MATCH_BOOL] = acl_parse_nothing,
59 [ACL_MATCH_INT] = acl_parse_int,
60 [ACL_MATCH_IP] = acl_parse_ip,
61 [ACL_MATCH_BIN] = acl_parse_bin,
62 [ACL_MATCH_LEN] = acl_parse_int,
63 [ACL_MATCH_STR] = acl_parse_str,
64 [ACL_MATCH_BEG] = acl_parse_str,
65 [ACL_MATCH_SUB] = acl_parse_str,
66 [ACL_MATCH_DIR] = acl_parse_str,
67 [ACL_MATCH_DOM] = acl_parse_str,
68 [ACL_MATCH_END] = acl_parse_str,
69 [ACL_MATCH_REG] = acl_parse_reg,
70};
71
72static int (*acl_match_fcts[ACL_MATCH_NUM])(struct sample *, struct acl_pattern *) = {
73 [ACL_MATCH_FOUND] = NULL,
74 [ACL_MATCH_BOOL] = acl_match_nothing,
75 [ACL_MATCH_INT] = acl_match_int,
76 [ACL_MATCH_IP] = acl_match_ip,
77 [ACL_MATCH_BIN] = acl_match_bin,
78 [ACL_MATCH_LEN] = acl_match_len,
79 [ACL_MATCH_STR] = acl_match_str,
80 [ACL_MATCH_BEG] = acl_match_beg,
81 [ACL_MATCH_SUB] = acl_match_sub,
82 [ACL_MATCH_DIR] = acl_match_dir,
83 [ACL_MATCH_DOM] = acl_match_dom,
84 [ACL_MATCH_END] = acl_match_end,
85 [ACL_MATCH_REG] = acl_match_reg,
86};
87
88/* return the ACL_MATCH_* index for match name "name", or < 0 if not found */
89static int acl_find_match_name(const char *name)
90{
91 int i;
92
93 for (i = 0; i < ACL_MATCH_NUM; i++)
94 if (strcmp(name, acl_match_names[i]) == 0)
95 return i;
96 return -1;
97}
Willy Tarreaua84d3742007-05-07 00:36:48 +020098
Willy Tarreaua5909832007-06-17 20:40:25 +020099/*
Willy Tarreau58393e12008-07-20 10:39:22 +0200100 * These functions are exported and may be used by any other component.
101 */
102
103/* ignore the current line */
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200104int acl_parse_nothing(const char **text, struct acl_pattern *pattern, int *opaque, char **err)
Willy Tarreaua5909832007-06-17 20:40:25 +0200105{
Willy Tarreau58393e12008-07-20 10:39:22 +0200106 return 1;
107}
108
Willy Tarreaua5909832007-06-17 20:40:25 +0200109/* always return false */
Willy Tarreau37406352012-04-23 16:16:37 +0200110int acl_match_nothing(struct sample *smp, struct acl_pattern *pattern)
Willy Tarreaua5909832007-06-17 20:40:25 +0200111{
Willy Tarreau11382812008-07-09 16:18:21 +0200112 return ACL_PAT_FAIL;
Willy Tarreaua5909832007-06-17 20:40:25 +0200113}
114
115
Willy Tarreaua84d3742007-05-07 00:36:48 +0200116/* NB: For two strings to be identical, it is required that their lengths match */
Willy Tarreau37406352012-04-23 16:16:37 +0200117int acl_match_str(struct sample *smp, struct acl_pattern *pattern)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200118{
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200119 int icase;
120
Willy Tarreauf853c462012-04-23 18:53:56 +0200121 if (pattern->len != smp->data.str.len)
Willy Tarreau11382812008-07-09 16:18:21 +0200122 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200123
124 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
Willy Tarreauf853c462012-04-23 18:53:56 +0200125 if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) == 0) ||
126 (!icase && strncmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) == 0))
Willy Tarreau11382812008-07-09 16:18:21 +0200127 return ACL_PAT_PASS;
128 return ACL_PAT_FAIL;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200129}
130
Emeric Brun07ca4962012-10-17 13:38:19 +0200131/* NB: For two binaries buf to be identical, it is required that their lengths match */
132int acl_match_bin(struct sample *smp, struct acl_pattern *pattern)
133{
134 if (pattern->len != smp->data.str.len)
135 return ACL_PAT_FAIL;
136
137 if (memcmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) == 0)
138 return ACL_PAT_PASS;
139 return ACL_PAT_FAIL;
140}
141
Willy Tarreauc4262962010-05-10 23:42:40 +0200142/* Lookup a string in the expression's pattern tree. The node is returned if it
143 * exists, otherwise NULL.
144 */
Willy Tarreau37406352012-04-23 16:16:37 +0200145static void *acl_lookup_str(struct sample *smp, struct acl_expr *expr)
Willy Tarreauc4262962010-05-10 23:42:40 +0200146{
147 /* data are stored in a tree */
148 struct ebmb_node *node;
149 char prev;
150
151 /* we may have to force a trailing zero on the test pattern */
Willy Tarreauf853c462012-04-23 18:53:56 +0200152 prev = smp->data.str.str[smp->data.str.len];
Willy Tarreauc4262962010-05-10 23:42:40 +0200153 if (prev)
Willy Tarreauf853c462012-04-23 18:53:56 +0200154 smp->data.str.str[smp->data.str.len] = '\0';
155 node = ebst_lookup(&expr->pattern_tree, smp->data.str.str);
Willy Tarreauc4262962010-05-10 23:42:40 +0200156 if (prev)
Willy Tarreauf853c462012-04-23 18:53:56 +0200157 smp->data.str.str[smp->data.str.len] = prev;
Willy Tarreauc4262962010-05-10 23:42:40 +0200158 return node;
159}
160
Willy Tarreau21e5b0e2012-04-23 19:25:44 +0200161/* Executes a regex. It temporarily changes the data to add a trailing zero,
162 * and restores the previous character when leaving.
Willy Tarreauf3d25982007-05-08 22:45:09 +0200163 */
Willy Tarreau37406352012-04-23 16:16:37 +0200164int acl_match_reg(struct sample *smp, struct acl_pattern *pattern)
Willy Tarreauf3d25982007-05-08 22:45:09 +0200165{
166 char old_char;
167 int ret;
168
Willy Tarreauf853c462012-04-23 18:53:56 +0200169 old_char = smp->data.str.str[smp->data.str.len];
170 smp->data.str.str[smp->data.str.len] = 0;
Willy Tarreauf3d25982007-05-08 22:45:09 +0200171
Hiroaki Nakamura70351322013-01-13 15:00:42 +0900172 if (regex_exec(pattern->ptr.reg, smp->data.str.str, smp->data.str.len) == 0)
Willy Tarreau11382812008-07-09 16:18:21 +0200173 ret = ACL_PAT_PASS;
Willy Tarreauf3d25982007-05-08 22:45:09 +0200174 else
Willy Tarreau11382812008-07-09 16:18:21 +0200175 ret = ACL_PAT_FAIL;
Willy Tarreauf3d25982007-05-08 22:45:09 +0200176
Willy Tarreauf853c462012-04-23 18:53:56 +0200177 smp->data.str.str[smp->data.str.len] = old_char;
Willy Tarreauf3d25982007-05-08 22:45:09 +0200178 return ret;
179}
180
Willy Tarreaua84d3742007-05-07 00:36:48 +0200181/* Checks that the pattern matches the beginning of the tested string. */
Willy Tarreau37406352012-04-23 16:16:37 +0200182int acl_match_beg(struct sample *smp, struct acl_pattern *pattern)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200183{
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200184 int icase;
185
Willy Tarreauf853c462012-04-23 18:53:56 +0200186 if (pattern->len > smp->data.str.len)
Willy Tarreau11382812008-07-09 16:18:21 +0200187 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200188
189 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
Willy Tarreauf853c462012-04-23 18:53:56 +0200190 if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str, pattern->len) != 0) ||
191 (!icase && strncmp(pattern->ptr.str, smp->data.str.str, pattern->len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +0200192 return ACL_PAT_FAIL;
193 return ACL_PAT_PASS;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200194}
195
196/* Checks that the pattern matches the end of the tested string. */
Willy Tarreau37406352012-04-23 16:16:37 +0200197int acl_match_end(struct sample *smp, struct acl_pattern *pattern)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200198{
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200199 int icase;
200
Willy Tarreauf853c462012-04-23 18:53:56 +0200201 if (pattern->len > smp->data.str.len)
Willy Tarreau11382812008-07-09 16:18:21 +0200202 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200203 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
Willy Tarreauf853c462012-04-23 18:53:56 +0200204 if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str + smp->data.str.len - pattern->len, pattern->len) != 0) ||
205 (!icase && strncmp(pattern->ptr.str, smp->data.str.str + smp->data.str.len - pattern->len, pattern->len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +0200206 return ACL_PAT_FAIL;
207 return ACL_PAT_PASS;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200208}
209
210/* Checks that the pattern is included inside the tested string.
211 * NB: Suboptimal, should be rewritten using a Boyer-Moore method.
212 */
Willy Tarreau37406352012-04-23 16:16:37 +0200213int acl_match_sub(struct sample *smp, struct acl_pattern *pattern)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200214{
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200215 int icase;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200216 char *end;
217 char *c;
218
Willy Tarreauf853c462012-04-23 18:53:56 +0200219 if (pattern->len > smp->data.str.len)
Willy Tarreau11382812008-07-09 16:18:21 +0200220 return ACL_PAT_FAIL;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200221
Willy Tarreauf853c462012-04-23 18:53:56 +0200222 end = smp->data.str.str + smp->data.str.len - pattern->len;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200223 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
224 if (icase) {
Willy Tarreauf853c462012-04-23 18:53:56 +0200225 for (c = smp->data.str.str; c <= end; c++) {
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200226 if (tolower(*c) != tolower(*pattern->ptr.str))
227 continue;
228 if (strncasecmp(pattern->ptr.str, c, pattern->len) == 0)
Willy Tarreau11382812008-07-09 16:18:21 +0200229 return ACL_PAT_PASS;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200230 }
231 } else {
Willy Tarreauf853c462012-04-23 18:53:56 +0200232 for (c = smp->data.str.str; c <= end; c++) {
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200233 if (*c != *pattern->ptr.str)
234 continue;
235 if (strncmp(pattern->ptr.str, c, pattern->len) == 0)
Willy Tarreau11382812008-07-09 16:18:21 +0200236 return ACL_PAT_PASS;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200237 }
Willy Tarreaua84d3742007-05-07 00:36:48 +0200238 }
Willy Tarreau11382812008-07-09 16:18:21 +0200239 return ACL_PAT_FAIL;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200240}
241
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200242/* Background: Fast way to find a zero byte in a word
243 * http://graphics.stanford.edu/~seander/bithacks.html#ZeroInWord
244 * hasZeroByte = (v - 0x01010101UL) & ~v & 0x80808080UL;
245 *
246 * To look for 4 different byte values, xor the word with those bytes and
247 * then check for zero bytes:
248 *
249 * v = (((unsigned char)c * 0x1010101U) ^ delimiter)
250 * where <delimiter> is the 4 byte values to look for (as an uint)
251 * and <c> is the character that is being tested
252 */
253static inline unsigned int is_delimiter(unsigned char c, unsigned int mask)
254{
255 mask ^= (c * 0x01010101); /* propagate the char to all 4 bytes */
256 return (mask - 0x01010101) & ~mask & 0x80808080U;
257}
258
259static inline unsigned int make_4delim(unsigned char d1, unsigned char d2, unsigned char d3, unsigned char d4)
260{
261 return d1 << 24 | d2 << 16 | d3 << 8 | d4;
262}
263
Willy Tarreaua84d3742007-05-07 00:36:48 +0200264/* This one is used by other real functions. It checks that the pattern is
265 * included inside the tested string, but enclosed between the specified
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200266 * delimiters or at the beginning or end of the string. The delimiters are
267 * provided as an unsigned int made by make_4delim() and match up to 4 different
268 * delimiters. Delimiters are stripped at the beginning and end of the pattern.
Willy Tarreaua84d3742007-05-07 00:36:48 +0200269 */
Willy Tarreau37406352012-04-23 16:16:37 +0200270static int match_word(struct sample *smp, struct acl_pattern *pattern, unsigned int delimiters)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200271{
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200272 int may_match, icase;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200273 char *c, *end;
274 char *ps;
275 int pl;
276
277 pl = pattern->len;
278 ps = pattern->ptr.str;
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200279
280 while (pl > 0 && is_delimiter(*ps, delimiters)) {
Willy Tarreaua84d3742007-05-07 00:36:48 +0200281 pl--;
282 ps++;
283 }
284
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200285 while (pl > 0 && is_delimiter(ps[pl - 1], delimiters))
Willy Tarreaua84d3742007-05-07 00:36:48 +0200286 pl--;
287
Willy Tarreauf853c462012-04-23 18:53:56 +0200288 if (pl > smp->data.str.len)
Willy Tarreau11382812008-07-09 16:18:21 +0200289 return ACL_PAT_FAIL;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200290
291 may_match = 1;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200292 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
Willy Tarreauf853c462012-04-23 18:53:56 +0200293 end = smp->data.str.str + smp->data.str.len - pl;
294 for (c = smp->data.str.str; c <= end; c++) {
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200295 if (is_delimiter(*c, delimiters)) {
Willy Tarreaua84d3742007-05-07 00:36:48 +0200296 may_match = 1;
297 continue;
298 }
Willy Tarreaua84d3742007-05-07 00:36:48 +0200299
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200300 if (!may_match)
301 continue;
302
303 if (icase) {
304 if ((tolower(*c) == tolower(*ps)) &&
305 (strncasecmp(ps, c, pl) == 0) &&
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200306 (c == end || is_delimiter(c[pl], delimiters)))
Willy Tarreau11382812008-07-09 16:18:21 +0200307 return ACL_PAT_PASS;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200308 } else {
309 if ((*c == *ps) &&
310 (strncmp(ps, c, pl) == 0) &&
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200311 (c == end || is_delimiter(c[pl], delimiters)))
Willy Tarreau11382812008-07-09 16:18:21 +0200312 return ACL_PAT_PASS;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200313 }
Willy Tarreaua84d3742007-05-07 00:36:48 +0200314 may_match = 0;
315 }
Willy Tarreau11382812008-07-09 16:18:21 +0200316 return ACL_PAT_FAIL;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200317}
318
319/* Checks that the pattern is included inside the tested string, but enclosed
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200320 * between the delimiters '?' or '/' or at the beginning or end of the string.
321 * Delimiters at the beginning or end of the pattern are ignored.
Willy Tarreaua84d3742007-05-07 00:36:48 +0200322 */
Willy Tarreau37406352012-04-23 16:16:37 +0200323int acl_match_dir(struct sample *smp, struct acl_pattern *pattern)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200324{
Willy Tarreau37406352012-04-23 16:16:37 +0200325 return match_word(smp, pattern, make_4delim('/', '?', '?', '?'));
Willy Tarreaua84d3742007-05-07 00:36:48 +0200326}
327
328/* Checks that the pattern is included inside the tested string, but enclosed
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200329 * between the delmiters '/', '?', '.' or ":" or at the beginning or end of
330 * the string. Delimiters at the beginning or end of the pattern are ignored.
Willy Tarreaua84d3742007-05-07 00:36:48 +0200331 */
Willy Tarreau37406352012-04-23 16:16:37 +0200332int acl_match_dom(struct sample *smp, struct acl_pattern *pattern)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200333{
Willy Tarreau37406352012-04-23 16:16:37 +0200334 return match_word(smp, pattern, make_4delim('/', '?', '.', ':'));
Willy Tarreaua84d3742007-05-07 00:36:48 +0200335}
336
337/* Checks that the integer in <test> is included between min and max */
Willy Tarreau37406352012-04-23 16:16:37 +0200338int acl_match_int(struct sample *smp, struct acl_pattern *pattern)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200339{
Willy Tarreauf853c462012-04-23 18:53:56 +0200340 if ((!pattern->val.range.min_set || pattern->val.range.min <= smp->data.uint) &&
341 (!pattern->val.range.max_set || smp->data.uint <= pattern->val.range.max))
Willy Tarreau11382812008-07-09 16:18:21 +0200342 return ACL_PAT_PASS;
343 return ACL_PAT_FAIL;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200344}
345
Willy Tarreau0e698542011-09-16 08:32:32 +0200346/* Checks that the length of the pattern in <test> is included between min and max */
Willy Tarreau37406352012-04-23 16:16:37 +0200347int acl_match_len(struct sample *smp, struct acl_pattern *pattern)
Willy Tarreau0e698542011-09-16 08:32:32 +0200348{
Willy Tarreauf853c462012-04-23 18:53:56 +0200349 if ((!pattern->val.range.min_set || pattern->val.range.min <= smp->data.str.len) &&
350 (!pattern->val.range.max_set || smp->data.str.len <= pattern->val.range.max))
Willy Tarreau0e698542011-09-16 08:32:32 +0200351 return ACL_PAT_PASS;
352 return ACL_PAT_FAIL;
353}
354
Willy Tarreau37406352012-04-23 16:16:37 +0200355int acl_match_ip(struct sample *smp, struct acl_pattern *pattern)
Willy Tarreaua67fad92007-05-08 19:50:09 +0200356{
Willy Tarreauceb4ac92012-04-28 00:41:46 +0200357 unsigned int v4; /* in network byte order */
358 struct in6_addr *v6;
359 int bits, pos;
360 struct in6_addr tmp6;
Willy Tarreaua67fad92007-05-08 19:50:09 +0200361
Willy Tarreauceb4ac92012-04-28 00:41:46 +0200362 if (pattern->type == SMP_T_IPV4) {
363 if (smp->type == SMP_T_IPV4) {
364 v4 = smp->data.ipv4.s_addr;
365 }
366 else if (smp->type == SMP_T_IPV6) {
367 /* v4 match on a V6 sample. We want to check at least for
368 * the following forms :
369 * - ::ffff:ip:v4 (ipv4 mapped)
370 * - ::0000:ip:v4 (old ipv4 mapped)
371 * - 2002:ip:v4:: (6to4)
372 */
373 if (*(uint32_t*)&smp->data.ipv6.s6_addr[0] == 0 &&
374 *(uint32_t*)&smp->data.ipv6.s6_addr[4] == 0 &&
375 (*(uint32_t*)&smp->data.ipv6.s6_addr[8] == 0 ||
376 *(uint32_t*)&smp->data.ipv6.s6_addr[8] == htonl(0xFFFF))) {
377 v4 = *(uint32_t*)&smp->data.ipv6.s6_addr[12];
378 }
379 else if (*(uint16_t*)&smp->data.ipv6.s6_addr[0] == htons(0x2002)) {
380 v4 = htonl((ntohs(*(uint16_t*)&smp->data.ipv6.s6_addr[2]) << 16) +
381 ntohs(*(uint16_t*)&smp->data.ipv6.s6_addr[4]));
382 }
383 else
384 return ACL_PAT_FAIL;
385 }
386 else
387 return ACL_PAT_FAIL;
Willy Tarreaua67fad92007-05-08 19:50:09 +0200388
Willy Tarreauceb4ac92012-04-28 00:41:46 +0200389 if (((v4 ^ pattern->val.ipv4.addr.s_addr) & pattern->val.ipv4.mask.s_addr) == 0)
390 return ACL_PAT_PASS;
391 else
392 return ACL_PAT_FAIL;
393 }
394 else if (pattern->type == SMP_T_IPV6) {
395 if (smp->type == SMP_T_IPV4) {
396 /* Convert the IPv4 sample address to IPv4 with the
397 * mapping method using the ::ffff: prefix.
398 */
399 memset(&tmp6, 0, 10);
400 *(uint16_t*)&tmp6.s6_addr[10] = htons(0xffff);
401 *(uint32_t*)&tmp6.s6_addr[12] = smp->data.ipv4.s_addr;
402 v6 = &tmp6;
403 }
404 else if (smp->type == SMP_T_IPV6) {
405 v6 = &smp->data.ipv6;
406 }
407 else {
408 return ACL_PAT_FAIL;
409 }
410
411 bits = pattern->val.ipv6.mask;
412 for (pos = 0; bits > 0; pos += 4, bits -= 32) {
413 v4 = *(uint32_t*)&v6->s6_addr[pos] ^ *(uint32_t*)&pattern->val.ipv6.addr.s6_addr[pos];
414 if (bits < 32)
Cyril Bonté4c01beb2012-10-23 21:28:31 +0200415 v4 &= htonl((~0U) << (32-bits));
Willy Tarreauceb4ac92012-04-28 00:41:46 +0200416 if (v4)
417 return ACL_PAT_FAIL;
418 }
Willy Tarreau11382812008-07-09 16:18:21 +0200419 return ACL_PAT_PASS;
Willy Tarreauceb4ac92012-04-28 00:41:46 +0200420 }
Willy Tarreau11382812008-07-09 16:18:21 +0200421 return ACL_PAT_FAIL;
Willy Tarreaua67fad92007-05-08 19:50:09 +0200422}
423
Willy Tarreaub337b532010-05-13 20:03:41 +0200424/* Lookup an IPv4 address in the expression's pattern tree using the longest
425 * match method. The node is returned if it exists, otherwise NULL.
426 */
Willy Tarreau37406352012-04-23 16:16:37 +0200427static void *acl_lookup_ip(struct sample *smp, struct acl_expr *expr)
Willy Tarreaub337b532010-05-13 20:03:41 +0200428{
429 struct in_addr *s;
430
Willy Tarreauf853c462012-04-23 18:53:56 +0200431 if (smp->type != SMP_T_IPV4)
Willy Tarreaub337b532010-05-13 20:03:41 +0200432 return ACL_PAT_FAIL;
433
Willy Tarreauf853c462012-04-23 18:53:56 +0200434 s = &smp->data.ipv4;
Willy Tarreaub337b532010-05-13 20:03:41 +0200435 return ebmb_lookup_longest(&expr->pattern_tree, &s->s_addr);
436}
437
Willy Tarreaua84d3742007-05-07 00:36:48 +0200438/* Parse a string. It is allocated and duplicated. */
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200439int acl_parse_str(const char **text, struct acl_pattern *pattern, int *opaque, char **err)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200440{
441 int len;
442
Willy Tarreauae8b7962007-06-09 23:10:04 +0200443 len = strlen(*text);
Willy Tarreauc92ddbc2012-04-27 22:10:57 +0200444 pattern->type = SMP_T_CSTR;
Willy Tarreauc4262962010-05-10 23:42:40 +0200445
446 if (pattern->flags & ACL_PAT_F_TREE_OK) {
447 /* we're allowed to put the data in a tree whose root is pointed
448 * to by val.tree.
449 */
450 struct ebmb_node *node;
451
452 node = calloc(1, sizeof(*node) + len + 1);
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200453 if (!node) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200454 memprintf(err, "out of memory while loading string pattern");
Willy Tarreauc4262962010-05-10 23:42:40 +0200455 return 0;
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200456 }
Willy Tarreauc4262962010-05-10 23:42:40 +0200457 memcpy(node->key, *text, len + 1);
458 if (ebst_insert(pattern->val.tree, node) != node)
459 free(node); /* was a duplicate */
460 pattern->flags |= ACL_PAT_F_TREE; /* this pattern now contains a tree */
461 return 1;
462 }
463
Willy Tarreauae8b7962007-06-09 23:10:04 +0200464 pattern->ptr.str = strdup(*text);
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200465 if (!pattern->ptr.str) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200466 memprintf(err, "out of memory while loading string pattern");
Willy Tarreaua84d3742007-05-07 00:36:48 +0200467 return 0;
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200468 }
Willy Tarreaua84d3742007-05-07 00:36:48 +0200469 pattern->len = len;
470 return 1;
471}
472
Emeric Brun07ca4962012-10-17 13:38:19 +0200473/* Parse a binary written in hexa. It is allocated. */
474int acl_parse_bin(const char **text, struct acl_pattern *pattern, int *opaque, char **err)
475{
476 int len;
477 const char *p = *text;
478 int i,j;
479
480 len = strlen(p);
481 if (len%2) {
482 memprintf(err, "an even number of hex digit is expected");
483 return 0;
484 }
485
486 pattern->type = SMP_T_CBIN;
487 pattern->len = len >> 1;
488 pattern->ptr.str = malloc(pattern->len);
489 if (!pattern->ptr.str) {
490 memprintf(err, "out of memory while loading string pattern");
491 return 0;
492 }
493
494 i = j = 0;
495 while (j < pattern->len) {
496 if (!ishex(p[i++]))
497 goto bad_input;
498 if (!ishex(p[i++]))
499 goto bad_input;
500 pattern->ptr.str[j++] = (hex2i(p[i-2]) << 4) + hex2i(p[i-1]);
501 }
502 return 1;
503
504bad_input:
505 memprintf(err, "an hex digit is expected (found '%c')", p[i-1]);
506 free(pattern->ptr.str);
507 return 0;
508}
509
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +0100510/* Parse and concatenate all further strings into one. */
511int
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200512acl_parse_strcat(const char **text, struct acl_pattern *pattern, int *opaque, char **err)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +0100513{
514
515 int len = 0, i;
516 char *s;
517
518 for (i = 0; *text[i]; i++)
519 len += strlen(text[i])+1;
520
Willy Tarreauc92ddbc2012-04-27 22:10:57 +0200521 pattern->type = SMP_T_CSTR;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +0100522 pattern->ptr.str = s = calloc(1, len);
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200523 if (!pattern->ptr.str) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200524 memprintf(err, "out of memory while loading pattern");
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +0100525 return 0;
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200526 }
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +0100527
528 for (i = 0; *text[i]; i++)
529 s += sprintf(s, i?" %s":"%s", text[i]);
530
531 pattern->len = len;
532
533 return i;
534}
535
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +0200536/* Free data allocated by acl_parse_reg */
Willy Tarreau37406352012-04-23 16:16:37 +0200537static void acl_free_reg(void *ptr)
538{
Hiroaki Nakamura70351322013-01-13 15:00:42 +0900539 regex_free(ptr);
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +0200540}
541
Willy Tarreauf3d25982007-05-08 22:45:09 +0200542/* Parse a regex. It is allocated. */
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200543int acl_parse_reg(const char **text, struct acl_pattern *pattern, int *opaque, char **err)
Willy Tarreauf3d25982007-05-08 22:45:09 +0200544{
Hiroaki Nakamura70351322013-01-13 15:00:42 +0900545 regex *preg;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200546 int icase;
Willy Tarreauf3d25982007-05-08 22:45:09 +0200547
Hiroaki Nakamura70351322013-01-13 15:00:42 +0900548 preg = calloc(1, sizeof(*preg));
Willy Tarreauf3d25982007-05-08 22:45:09 +0200549
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200550 if (!preg) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200551 memprintf(err, "out of memory while loading pattern");
Willy Tarreauf3d25982007-05-08 22:45:09 +0200552 return 0;
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200553 }
Willy Tarreauf3d25982007-05-08 22:45:09 +0200554
Hiroaki Nakamura70351322013-01-13 15:00:42 +0900555#ifdef USE_PCRE_JIT
556 icase = (pattern->flags & ACL_PAT_F_IGNORE_CASE) ? PCRE_CASELESS : 0;
557 preg->reg = pcre_compile(*text, PCRE_NO_AUTO_CAPTURE | icase, NULL, NULL,
558 NULL);
559 if (!preg->reg) {
560 free(preg);
561 memprintf(err, "regex '%s' is invalid", *text);
562 return 0;
563 }
564
565 preg->extra = pcre_study(preg->reg, PCRE_STUDY_JIT_COMPILE, NULL);
566 if (!preg->extra) {
567 pcre_free(preg->reg);
568 free(preg);
569 memprintf(err, "failed to compile regex '%s'", *text);
570 return 0;
571 }
572#else
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200573 icase = (pattern->flags & ACL_PAT_F_IGNORE_CASE) ? REG_ICASE : 0;
574 if (regcomp(preg, *text, REG_EXTENDED | REG_NOSUB | icase) != 0) {
Willy Tarreauf3d25982007-05-08 22:45:09 +0200575 free(preg);
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200576 memprintf(err, "regex '%s' is invalid", *text);
Willy Tarreauf3d25982007-05-08 22:45:09 +0200577 return 0;
578 }
Hiroaki Nakamura70351322013-01-13 15:00:42 +0900579#endif
Willy Tarreauf3d25982007-05-08 22:45:09 +0200580
581 pattern->ptr.reg = preg;
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +0200582 pattern->freeptrbuf = &acl_free_reg;
Willy Tarreauf3d25982007-05-08 22:45:09 +0200583 return 1;
584}
585
Willy Tarreauae8b7962007-06-09 23:10:04 +0200586/* Parse a range of positive integers delimited by either ':' or '-'. If only
587 * one integer is read, it is set as both min and max. An operator may be
588 * specified as the prefix, among this list of 5 :
589 *
590 * 0:eq, 1:gt, 2:ge, 3:lt, 4:le
591 *
592 * The default operator is "eq". It supports range matching. Ranges are
593 * rejected for other operators. The operator may be changed at any time.
594 * The operator is stored in the 'opaque' argument.
595 *
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200596 * If err is non-NULL, an error message will be returned there on errors and
597 * the caller will have to free it.
598 *
Willy Tarreaua84d3742007-05-07 00:36:48 +0200599 */
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200600int acl_parse_int(const char **text, struct acl_pattern *pattern, int *opaque, char **err)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200601{
Willy Tarreauae8b7962007-06-09 23:10:04 +0200602 signed long long i;
603 unsigned int j, last, skip = 0;
604 const char *ptr = *text;
605
Willy Tarreauc92ddbc2012-04-27 22:10:57 +0200606 pattern->type = SMP_T_UINT;
Willy Tarreau8f8e6452007-06-17 21:51:38 +0200607 while (!isdigit((unsigned char)*ptr)) {
Willy Tarreau1c7cc5b2010-07-18 10:46:33 +0200608 switch (get_std_op(ptr)) {
609 case STD_OP_EQ: *opaque = 0; break;
610 case STD_OP_GT: *opaque = 1; break;
611 case STD_OP_GE: *opaque = 2; break;
612 case STD_OP_LT: *opaque = 3; break;
613 case STD_OP_LE: *opaque = 4; break;
614 default:
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200615 memprintf(err, "'%s' is neither a number nor a supported operator", ptr);
Willy Tarreauae8b7962007-06-09 23:10:04 +0200616 return 0;
Willy Tarreau1c7cc5b2010-07-18 10:46:33 +0200617 }
Willy Tarreauae8b7962007-06-09 23:10:04 +0200618
619 skip++;
620 ptr = text[skip];
621 }
Willy Tarreaua84d3742007-05-07 00:36:48 +0200622
623 last = i = 0;
624 while (1) {
Willy Tarreauae8b7962007-06-09 23:10:04 +0200625 j = *ptr++;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200626 if ((j == '-' || j == ':') && !last) {
627 last++;
628 pattern->val.range.min = i;
629 i = 0;
630 continue;
631 }
632 j -= '0';
633 if (j > 9)
634 // also catches the terminating zero
635 break;
636 i *= 10;
637 i += j;
638 }
Willy Tarreauae8b7962007-06-09 23:10:04 +0200639
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200640 if (last && *opaque >= 1 && *opaque <= 4) {
Willy Tarreauae8b7962007-06-09 23:10:04 +0200641 /* having a range with a min or a max is absurd */
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200642 memprintf(err, "integer range '%s' specified with a comparison operator", text[skip]);
Willy Tarreauae8b7962007-06-09 23:10:04 +0200643 return 0;
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200644 }
Willy Tarreauae8b7962007-06-09 23:10:04 +0200645
Willy Tarreaua84d3742007-05-07 00:36:48 +0200646 if (!last)
647 pattern->val.range.min = i;
648 pattern->val.range.max = i;
Willy Tarreauae8b7962007-06-09 23:10:04 +0200649
650 switch (*opaque) {
651 case 0: /* eq */
652 pattern->val.range.min_set = 1;
653 pattern->val.range.max_set = 1;
654 break;
655 case 1: /* gt */
656 pattern->val.range.min++; /* gt = ge + 1 */
657 case 2: /* ge */
658 pattern->val.range.min_set = 1;
659 pattern->val.range.max_set = 0;
660 break;
661 case 3: /* lt */
662 pattern->val.range.max--; /* lt = le - 1 */
663 case 4: /* le */
664 pattern->val.range.min_set = 0;
665 pattern->val.range.max_set = 1;
666 break;
667 }
668 return skip + 1;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200669}
670
Willy Tarreau4a26d2f2008-07-15 16:05:33 +0200671/* Parse a range of positive 2-component versions delimited by either ':' or
672 * '-'. The version consists in a major and a minor, both of which must be
673 * smaller than 65536, because internally they will be represented as a 32-bit
674 * integer.
675 * If only one version is read, it is set as both min and max. Just like for
676 * pure integers, an operator may be specified as the prefix, among this list
677 * of 5 :
678 *
679 * 0:eq, 1:gt, 2:ge, 3:lt, 4:le
680 *
681 * The default operator is "eq". It supports range matching. Ranges are
682 * rejected for other operators. The operator may be changed at any time.
683 * The operator is stored in the 'opaque' argument. This allows constructs
684 * such as the following one :
685 *
686 * acl obsolete_ssl ssl_req_proto lt 3
687 * acl unsupported_ssl ssl_req_proto gt 3.1
688 * acl valid_ssl ssl_req_proto 3.0-3.1
689 *
690 */
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200691int acl_parse_dotted_ver(const char **text, struct acl_pattern *pattern, int *opaque, char **err)
Willy Tarreau4a26d2f2008-07-15 16:05:33 +0200692{
693 signed long long i;
694 unsigned int j, last, skip = 0;
695 const char *ptr = *text;
696
697
698 while (!isdigit((unsigned char)*ptr)) {
Willy Tarreau1c7cc5b2010-07-18 10:46:33 +0200699 switch (get_std_op(ptr)) {
700 case STD_OP_EQ: *opaque = 0; break;
701 case STD_OP_GT: *opaque = 1; break;
702 case STD_OP_GE: *opaque = 2; break;
703 case STD_OP_LT: *opaque = 3; break;
704 case STD_OP_LE: *opaque = 4; break;
705 default:
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200706 memprintf(err, "'%s' is neither a number nor a supported operator", ptr);
Willy Tarreau4a26d2f2008-07-15 16:05:33 +0200707 return 0;
Willy Tarreau1c7cc5b2010-07-18 10:46:33 +0200708 }
Willy Tarreau4a26d2f2008-07-15 16:05:33 +0200709
710 skip++;
711 ptr = text[skip];
712 }
713
714 last = i = 0;
715 while (1) {
716 j = *ptr++;
717 if (j == '.') {
718 /* minor part */
719 if (i >= 65536)
720 return 0;
721 i <<= 16;
722 continue;
723 }
724 if ((j == '-' || j == ':') && !last) {
725 last++;
726 if (i < 65536)
727 i <<= 16;
728 pattern->val.range.min = i;
729 i = 0;
730 continue;
731 }
732 j -= '0';
733 if (j > 9)
734 // also catches the terminating zero
735 break;
736 i = (i & 0xFFFF0000) + (i & 0xFFFF) * 10;
737 i += j;
738 }
739
740 /* if we only got a major version, let's shift it now */
741 if (i < 65536)
742 i <<= 16;
743
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200744 if (last && *opaque >= 1 && *opaque <= 4) {
Willy Tarreau4a26d2f2008-07-15 16:05:33 +0200745 /* having a range with a min or a max is absurd */
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200746 memprintf(err, "version range '%s' specified with a comparison operator", text[skip]);
Willy Tarreau4a26d2f2008-07-15 16:05:33 +0200747 return 0;
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200748 }
Willy Tarreau4a26d2f2008-07-15 16:05:33 +0200749
750 if (!last)
751 pattern->val.range.min = i;
752 pattern->val.range.max = i;
753
754 switch (*opaque) {
755 case 0: /* eq */
756 pattern->val.range.min_set = 1;
757 pattern->val.range.max_set = 1;
758 break;
759 case 1: /* gt */
760 pattern->val.range.min++; /* gt = ge + 1 */
761 case 2: /* ge */
762 pattern->val.range.min_set = 1;
763 pattern->val.range.max_set = 0;
764 break;
765 case 3: /* lt */
766 pattern->val.range.max--; /* lt = le - 1 */
767 case 4: /* le */
768 pattern->val.range.min_set = 0;
769 pattern->val.range.max_set = 1;
770 break;
771 }
772 return skip + 1;
773}
774
Willy Tarreaua67fad92007-05-08 19:50:09 +0200775/* Parse an IP address and an optional mask in the form addr[/mask].
776 * The addr may either be an IPv4 address or a hostname. The mask
777 * may either be a dotted mask or a number of bits. Returns 1 if OK,
Willy Tarreauc92ddbc2012-04-27 22:10:57 +0200778 * otherwise 0. NOTE: IP address patterns are typed (IPV4/IPV6).
Willy Tarreaua67fad92007-05-08 19:50:09 +0200779 */
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200780int acl_parse_ip(const char **text, struct acl_pattern *pattern, int *opaque, char **err)
Willy Tarreaua67fad92007-05-08 19:50:09 +0200781{
Willy Tarreaub337b532010-05-13 20:03:41 +0200782 struct eb_root *tree = NULL;
783 if (pattern->flags & ACL_PAT_F_TREE_OK)
784 tree = pattern->val.tree;
785
786 if (str2net(*text, &pattern->val.ipv4.addr, &pattern->val.ipv4.mask)) {
787 unsigned int mask = ntohl(pattern->val.ipv4.mask.s_addr);
788 struct ebmb_node *node;
789 /* check if the mask is contiguous so that we can insert the
790 * network into the tree. A continuous mask has only ones on
791 * the left. This means that this mask + its lower bit added
792 * once again is null.
793 */
Willy Tarreauceb4ac92012-04-28 00:41:46 +0200794 pattern->type = SMP_T_IPV4;
Willy Tarreaub337b532010-05-13 20:03:41 +0200795 if (mask + (mask & -mask) == 0 && tree) {
796 mask = mask ? 33 - flsnz(mask & -mask) : 0; /* equals cidr value */
797 /* FIXME: insert <addr>/<mask> into the tree here */
798 node = calloc(1, sizeof(*node) + 4); /* reserve 4 bytes for IPv4 address */
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200799 if (!node) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200800 memprintf(err, "out of memory while loading IPv4 pattern");
Willy Tarreaub337b532010-05-13 20:03:41 +0200801 return 0;
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200802 }
Willy Tarreaub337b532010-05-13 20:03:41 +0200803 memcpy(node->key, &pattern->val.ipv4.addr, 4); /* network byte order */
804 node->node.pfx = mask;
805 if (ebmb_insert_prefix(tree, node, 4) != node)
806 free(node); /* was a duplicate */
807 pattern->flags |= ACL_PAT_F_TREE;
808 return 1;
809 }
Willy Tarreauae8b7962007-06-09 23:10:04 +0200810 return 1;
Willy Tarreaub337b532010-05-13 20:03:41 +0200811 }
Willy Tarreauceb4ac92012-04-28 00:41:46 +0200812 else if (str62net(*text, &pattern->val.ipv6.addr, &pattern->val.ipv6.mask)) {
813 /* no tree support right now */
814 pattern->type = SMP_T_IPV6;
815 return 1;
816 }
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200817 else {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200818 memprintf(err, "'%s' is not a valid IPv4 or IPv6 address", *text);
Willy Tarreauae8b7962007-06-09 23:10:04 +0200819 return 0;
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200820 }
Willy Tarreaua67fad92007-05-08 19:50:09 +0200821}
822
Willy Tarreaua84d3742007-05-07 00:36:48 +0200823/*
824 * Registers the ACL keyword list <kwl> as a list of valid keywords for next
825 * parsing sessions.
826 */
827void acl_register_keywords(struct acl_kw_list *kwl)
828{
829 LIST_ADDQ(&acl_keywords.list, &kwl->list);
830}
831
832/*
833 * Unregisters the ACL keyword list <kwl> from the list of valid keywords.
834 */
835void acl_unregister_keywords(struct acl_kw_list *kwl)
836{
837 LIST_DEL(&kwl->list);
838 LIST_INIT(&kwl->list);
839}
840
841/* Return a pointer to the ACL <name> within the list starting at <head>, or
842 * NULL if not found.
843 */
844struct acl *find_acl_by_name(const char *name, struct list *head)
845{
846 struct acl *acl;
847 list_for_each_entry(acl, head, list) {
848 if (strcmp(acl->name, name) == 0)
849 return acl;
850 }
851 return NULL;
852}
853
854/* Return a pointer to the ACL keyword <kw>, or NULL if not found. Note that if
855 * <kw> contains an opening parenthesis, only the left part of it is checked.
856 */
857struct acl_keyword *find_acl_kw(const char *kw)
858{
859 int index;
860 const char *kwend;
861 struct acl_kw_list *kwl;
862
863 kwend = strchr(kw, '(');
864 if (!kwend)
865 kwend = kw + strlen(kw);
866
867 list_for_each_entry(kwl, &acl_keywords.list, list) {
868 for (index = 0; kwl->kw[index].kw != NULL; index++) {
869 if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) &&
870 kwl->kw[index].kw[kwend-kw] == 0)
871 return &kwl->kw[index];
872 }
873 }
874 return NULL;
875}
876
Willy Tarreaudfd7fca2011-03-09 07:27:02 +0100877/* NB: does nothing if <pat> is NULL */
Willy Tarreaua84d3742007-05-07 00:36:48 +0200878static void free_pattern(struct acl_pattern *pat)
879{
Willy Tarreaudfd7fca2011-03-09 07:27:02 +0100880 if (!pat)
881 return;
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +0200882
883 if (pat->ptr.ptr) {
884 if (pat->freeptrbuf)
885 pat->freeptrbuf(pat->ptr.ptr);
886
Willy Tarreaua84d3742007-05-07 00:36:48 +0200887 free(pat->ptr.ptr);
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +0200888 }
889
Willy Tarreaua84d3742007-05-07 00:36:48 +0200890 free(pat);
891}
892
893static void free_pattern_list(struct list *head)
894{
895 struct acl_pattern *pat, *tmp;
896 list_for_each_entry_safe(pat, tmp, head, list)
897 free_pattern(pat);
898}
899
Willy Tarreaue56cda92010-05-11 23:25:05 +0200900static void free_pattern_tree(struct eb_root *root)
901{
902 struct eb_node *node, *next;
903 node = eb_first(root);
904 while (node) {
905 next = eb_next(node);
906 free(node);
907 node = next;
908 }
909}
910
Willy Tarreaua84d3742007-05-07 00:36:48 +0200911static struct acl_expr *prune_acl_expr(struct acl_expr *expr)
912{
Willy Tarreau34db1082012-04-19 17:16:54 +0200913 struct arg *arg;
914
Willy Tarreaua84d3742007-05-07 00:36:48 +0200915 free_pattern_list(&expr->patterns);
Willy Tarreaue56cda92010-05-11 23:25:05 +0200916 free_pattern_tree(&expr->pattern_tree);
Willy Tarreaua84d3742007-05-07 00:36:48 +0200917 LIST_INIT(&expr->patterns);
Willy Tarreau34db1082012-04-19 17:16:54 +0200918
919 for (arg = expr->args; arg; arg++) {
920 if (arg->type == ARGT_STOP)
921 break;
Willy Tarreau496aa012012-06-01 10:38:29 +0200922 if (arg->type == ARGT_STR || arg->unresolved) {
Willy Tarreau34db1082012-04-19 17:16:54 +0200923 free(arg->data.str.str);
924 arg->data.str.str = NULL;
Willy Tarreau496aa012012-06-01 10:38:29 +0200925 arg->unresolved = 0;
Willy Tarreau34db1082012-04-19 17:16:54 +0200926 }
Willy Tarreau34db1082012-04-19 17:16:54 +0200927 }
928
Willy Tarreau2e845be2012-10-19 19:49:09 +0200929 if (expr->args != empty_arg_list)
930 free(expr->args);
Willy Tarreaua84d3742007-05-07 00:36:48 +0200931 return expr;
932}
933
Willy Tarreau08ad0b32012-04-27 17:25:24 +0200934
935/* Reads patterns from a file. If <err_msg> is non-NULL, an error message will
936 * be returned there on errors and the caller will have to free it.
937 */
Willy Tarreaud76a98a2013-03-31 18:34:33 +0200938static int acl_read_patterns_from_file(struct acl_expr *expr,
939 const char *filename, int patflags,
940 char **err)
Willy Tarreau2b5285d2010-05-09 23:45:24 +0200941{
942 FILE *file;
943 char *c;
944 const char *args[2];
945 struct acl_pattern *pattern;
946 int opaque;
Willy Tarreau6a8097f2011-02-26 15:14:15 +0100947 int ret = 0;
Willy Tarreau08ad0b32012-04-27 17:25:24 +0200948 int line = 0;
Willy Tarreau2b5285d2010-05-09 23:45:24 +0200949
950 file = fopen(filename, "r");
Willy Tarreau08ad0b32012-04-27 17:25:24 +0200951 if (!file) {
952 memprintf(err, "failed to open pattern file <%s>", filename);
Willy Tarreau2b5285d2010-05-09 23:45:24 +0200953 return 0;
Willy Tarreau08ad0b32012-04-27 17:25:24 +0200954 }
Willy Tarreau2b5285d2010-05-09 23:45:24 +0200955
956 /* now parse all patterns. The file may contain only one pattern per
957 * line. If the line contains spaces, they will be part of the pattern.
958 * The pattern stops at the first CR, LF or EOF encountered.
959 */
960 opaque = 0;
Willy Tarreaue56cda92010-05-11 23:25:05 +0200961 pattern = NULL;
Willy Tarreau2b5285d2010-05-09 23:45:24 +0200962 args[1] = "";
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100963 while (fgets(trash.str, trash.size, file) != NULL) {
Willy Tarreau08ad0b32012-04-27 17:25:24 +0200964 line++;
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100965 c = trash.str;
Willy Tarreau58215a02010-05-13 22:07:43 +0200966
967 /* ignore lines beginning with a dash */
968 if (*c == '#')
969 continue;
970
971 /* strip leading spaces and tabs */
972 while (*c == ' ' || *c == '\t')
973 c++;
974
Willy Tarreau58215a02010-05-13 22:07:43 +0200975
976 args[0] = c;
Willy Tarreau2b5285d2010-05-09 23:45:24 +0200977 while (*c && *c != '\n' && *c != '\r')
978 c++;
979 *c = 0;
980
Willy Tarreau51091962011-01-03 21:04:10 +0100981 /* empty lines are ignored too */
982 if (c == args[0])
983 continue;
984
Willy Tarreaue56cda92010-05-11 23:25:05 +0200985 /* we keep the previous pattern along iterations as long as it's not used */
986 if (!pattern)
987 pattern = (struct acl_pattern *)malloc(sizeof(*pattern));
Willy Tarreau08ad0b32012-04-27 17:25:24 +0200988 if (!pattern) {
989 memprintf(err, "out of memory when loading patterns from file <%s>", filename);
Willy Tarreau2b5285d2010-05-09 23:45:24 +0200990 goto out_close;
Willy Tarreau08ad0b32012-04-27 17:25:24 +0200991 }
Willy Tarreaue56cda92010-05-11 23:25:05 +0200992
993 memset(pattern, 0, sizeof(*pattern));
Willy Tarreau2b5285d2010-05-09 23:45:24 +0200994 pattern->flags = patflags;
995
Willy Tarreaue0db1e82013-01-04 16:31:47 +0100996 if (!(pattern->flags & ACL_PAT_F_IGNORE_CASE) &&
Willy Tarreaud76a98a2013-03-31 18:34:33 +0200997 (expr->match == acl_match_str || expr->match == acl_match_ip)) {
Willy Tarreaue56cda92010-05-11 23:25:05 +0200998 /* we pre-set the data pointer to the tree's head so that functions
999 * which are able to insert in a tree know where to do that.
1000 */
1001 pattern->flags |= ACL_PAT_F_TREE_OK;
1002 pattern->val.tree = &expr->pattern_tree;
1003 }
1004
Willy Tarreauc92ddbc2012-04-27 22:10:57 +02001005 pattern->type = SMP_TYPES; /* unspecified type by default */
Willy Tarreaud76a98a2013-03-31 18:34:33 +02001006 if (!expr->parse(args, pattern, &opaque, err))
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001007 goto out_free_pattern;
Willy Tarreaue56cda92010-05-11 23:25:05 +02001008
1009 /* if the parser did not feed the tree, let's chain the pattern to the list */
1010 if (!(pattern->flags & ACL_PAT_F_TREE)) {
1011 LIST_ADDQ(&expr->patterns, &pattern->list);
1012 pattern = NULL; /* get a new one */
1013 }
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001014 }
Willy Tarreau6a8097f2011-02-26 15:14:15 +01001015
1016 ret = 1; /* success */
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001017
1018 out_free_pattern:
1019 free_pattern(pattern);
1020 out_close:
1021 fclose(file);
Willy Tarreau6a8097f2011-02-26 15:14:15 +01001022 return ret;
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001023}
1024
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001025/* Parse an ACL expression starting at <args>[0], and return it. If <err> is
1026 * not NULL, it will be filled with a pointer to an error message in case of
1027 * error. This pointer must be freeable or NULL.
1028 *
Willy Tarreaua84d3742007-05-07 00:36:48 +02001029 * Right now, the only accepted syntax is :
1030 * <subject> [<value>...]
1031 */
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001032struct acl_expr *parse_acl_expr(const char **args, char **err)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001033{
1034 __label__ out_return, out_free_expr, out_free_pattern;
1035 struct acl_expr *expr;
1036 struct acl_keyword *aclkw;
1037 struct acl_pattern *pattern;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02001038 int opaque, patflags;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001039 const char *arg;
Willy Tarreaubef91e72013-03-31 23:14:46 +02001040 struct sample_fetch *smp = NULL;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001041
Willy Tarreaubef91e72013-03-31 23:14:46 +02001042 /* First, we lookd for an ACL keyword. And if we don't find one, then
1043 * we look for a sample fetch keyword.
1044 */
Willy Tarreaua84d3742007-05-07 00:36:48 +02001045 aclkw = find_acl_kw(args[0]);
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001046 if (!aclkw || !aclkw->parse) {
Willy Tarreaubef91e72013-03-31 23:14:46 +02001047 const char *kwend;
1048
1049 kwend = strchr(args[0], '(');
1050 if (!kwend)
1051 kwend = args[0] + strlen(args[0]);
1052 smp = find_sample_fetch(args[0], kwend - args[0]);
1053
1054 if (!smp) {
1055 memprintf(err, "unknown ACL or sample keyword '%s'", *args);
1056 goto out_return;
1057 }
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001058 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001059
1060 expr = (struct acl_expr *)calloc(1, sizeof(*expr));
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001061 if (!expr) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001062 memprintf(err, "out of memory when parsing ACL expression");
Willy Tarreaua84d3742007-05-07 00:36:48 +02001063 goto out_return;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001064 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001065
Willy Tarreaubef91e72013-03-31 23:14:46 +02001066 expr->kw = aclkw ? aclkw->kw : smp->kw;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001067 LIST_INIT(&expr->patterns);
Willy Tarreaue56cda92010-05-11 23:25:05 +02001068 expr->pattern_tree = EB_ROOT_UNIQUE;
Willy Tarreaubef91e72013-03-31 23:14:46 +02001069 expr->parse = aclkw ? aclkw->parse : NULL;
1070 expr->match = aclkw ? aclkw->match : NULL;
Willy Tarreau2e845be2012-10-19 19:49:09 +02001071 expr->args = empty_arg_list;
Willy Tarreaubef91e72013-03-31 23:14:46 +02001072 expr->smp = aclkw ? aclkw->smp : smp;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001073
1074 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
1091 * missing.
1092 */
Willy Tarreaud76a98a2013-03-31 18:34:33 +02001093 nbargs = make_arg_list(arg, end - arg, expr->smp->arg_mask, &expr->args,
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001094 err, NULL, NULL);
1095 if (nbargs < 0) {
1096 /* note that make_arg_list will have set <err> here */
Willy Tarreau93fddf12013-03-31 22:59:32 +02001097 memprintf(err, "in argument to '%s', %s", expr->kw, *err);
Willy Tarreau61612d42012-04-19 18:42:05 +02001098 goto out_free_expr;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001099 }
Willy Tarreauae52f062012-04-26 12:13:35 +02001100
Willy Tarreau2e845be2012-10-19 19:49:09 +02001101 if (!expr->args)
1102 expr->args = empty_arg_list;
1103
Willy Tarreaud76a98a2013-03-31 18:34:33 +02001104 if (expr->smp->val_args && !expr->smp->val_args(expr->args, err)) {
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001105 /* invalid keyword argument, error must have been
1106 * set by val_args().
1107 */
Willy Tarreau93fddf12013-03-31 22:59:32 +02001108 memprintf(err, "in argument to '%s', %s", expr->kw, *err);
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001109 goto out_free_expr;
1110 }
Willy Tarreau61612d42012-04-19 18:42:05 +02001111 }
Willy Tarreaud76a98a2013-03-31 18:34:33 +02001112 else if (ARGM(expr->smp->arg_mask) == 1) {
1113 int type = (expr->smp->arg_mask >> 4) & 15;
Willy Tarreaufc2c1fd2012-04-19 23:35:54 +02001114
1115 /* If a proxy is noted as a mandatory argument, we'll fake
1116 * an empty one so that acl_find_targets() resolves it as
1117 * the current one later.
1118 */
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001119 if (type != ARGT_FE && type != ARGT_BE && type != ARGT_TAB) {
Willy Tarreau93fddf12013-03-31 22:59:32 +02001120 memprintf(err, "ACL keyword '%s' expects %d arguments", expr->kw, ARGM(expr->smp->arg_mask));
Willy Tarreaufc2c1fd2012-04-19 23:35:54 +02001121 goto out_free_expr;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001122 }
Willy Tarreaufc2c1fd2012-04-19 23:35:54 +02001123
1124 /* Build an arg list containing the type as an empty string
1125 * and the usual STOP.
1126 */
1127 expr->args = calloc(2, sizeof(*expr->args));
1128 expr->args[0].type = type;
Willy Tarreaue3a46112012-06-15 08:02:34 +02001129 expr->args[0].unresolved = 1;
Willy Tarreaufc2c1fd2012-04-19 23:35:54 +02001130 expr->args[0].data.str.str = strdup("");
1131 expr->args[0].data.str.len = 1;
1132 expr->args[0].data.str.len = 0;
1133 expr->args[1].type = ARGT_STOP;
1134 }
Willy Tarreaud76a98a2013-03-31 18:34:33 +02001135 else if (ARGM(expr->smp->arg_mask)) {
Willy Tarreau61612d42012-04-19 18:42:05 +02001136 /* there were some mandatory arguments */
Willy Tarreau93fddf12013-03-31 22:59:32 +02001137 memprintf(err, "ACL keyword '%s' expects %d arguments", expr->kw, ARGM(expr->smp->arg_mask));
Willy Tarreaua84d3742007-05-07 00:36:48 +02001138 goto out_free_expr;
Willy Tarreau61612d42012-04-19 18:42:05 +02001139 }
1140 }
1141 else {
1142 if (arg) {
1143 /* no argument expected */
Willy Tarreau93fddf12013-03-31 22:59:32 +02001144 memprintf(err, "ACL keyword '%s' takes no argument", expr->kw);
Willy Tarreau61612d42012-04-19 18:42:05 +02001145 goto out_free_expr;
1146 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001147 }
1148
Willy Tarreaua84d3742007-05-07 00:36:48 +02001149 args++;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02001150
1151 /* check for options before patterns. Supported options are :
1152 * -i : ignore case for all patterns by default
1153 * -f : read patterns from those files
Willy Tarreau5adeda12013-03-31 22:13:34 +02001154 * -m : force matching method (must be used before -f)
Willy Tarreauc8d7c962007-06-17 08:20:33 +02001155 * -- : everything after this is not an option
1156 */
1157 patflags = 0;
1158 while (**args == '-') {
1159 if ((*args)[1] == 'i')
1160 patflags |= ACL_PAT_F_IGNORE_CASE;
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001161 else if ((*args)[1] == 'f') {
Willy Tarreaubef91e72013-03-31 23:14:46 +02001162 if (!expr->parse) {
1163 memprintf(err, "matching method must be specified first (using '-m') when using a sample fetch ('%s')", expr->kw);
1164 goto out_free_expr;
1165 }
1166
Willy Tarreaud76a98a2013-03-31 18:34:33 +02001167 if (!acl_read_patterns_from_file(expr, args[1], patflags | ACL_PAT_F_FROM_FILE, err))
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001168 goto out_free_expr;
Willy Tarreau5adeda12013-03-31 22:13:34 +02001169 args++;
1170 }
1171 else if ((*args)[1] == 'm') {
1172 int idx;
1173
1174 if (!LIST_ISEMPTY(&expr->patterns) || !eb_is_empty(&expr->pattern_tree)) {
1175 memprintf(err, "'-m' must only be specified before patterns and files in parsing ACL expression");
1176 goto out_free_expr;
1177 }
1178
1179 idx = acl_find_match_name(args[1]);
1180 if (idx < 0) {
1181 memprintf(err, "unknown matching method '%s' when parsing ACL expression", args[1]);
1182 goto out_free_expr;
1183 }
1184
1185 /* Note: -m found is always valid, bool/int are compatible, str/bin/reg/len are compatible */
1186 if (idx == ACL_MATCH_FOUND || /* -m found */
1187 ((idx == ACL_MATCH_BOOL || idx == ACL_MATCH_INT) && /* -m bool/int */
1188 (expr->smp->out_type == SMP_T_BOOL ||
1189 expr->smp->out_type == SMP_T_UINT ||
1190 expr->smp->out_type == SMP_T_SINT)) ||
1191 (idx == ACL_MATCH_IP && /* -m ip */
1192 (expr->smp->out_type == SMP_T_IPV4 ||
1193 expr->smp->out_type == SMP_T_IPV6)) ||
1194 ((idx == ACL_MATCH_BIN || idx == ACL_MATCH_LEN || idx == ACL_MATCH_STR ||
1195 idx == ACL_MATCH_BEG || idx == ACL_MATCH_SUB || idx == ACL_MATCH_DIR ||
1196 idx == ACL_MATCH_DOM || idx == ACL_MATCH_END || idx == ACL_MATCH_REG) && /* strings */
1197 (expr->smp->out_type == SMP_T_STR ||
1198 expr->smp->out_type == SMP_T_BIN ||
1199 expr->smp->out_type == SMP_T_CSTR ||
1200 expr->smp->out_type == SMP_T_CBIN))) {
1201 expr->parse = acl_parse_fcts[idx];
1202 expr->match = acl_match_fcts[idx];
1203 }
1204 else {
Willy Tarreau93fddf12013-03-31 22:59:32 +02001205 memprintf(err, "matching method '%s' cannot be used with fetch keyword '%s'", args[1], expr->kw);
Willy Tarreau5adeda12013-03-31 22:13:34 +02001206 goto out_free_expr;
1207 }
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001208 args++;
1209 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +02001210 else if ((*args)[1] == '-') {
1211 args++;
1212 break;
1213 }
1214 else
1215 break;
1216 args++;
1217 }
1218
Willy Tarreaubef91e72013-03-31 23:14:46 +02001219 if (!expr->parse) {
1220 memprintf(err, "matching method must be specified first (using '-m') when using a sample fetch ('%s')", expr->kw);
1221 goto out_free_expr;
1222 }
1223
Willy Tarreauc8d7c962007-06-17 08:20:33 +02001224 /* now parse all patterns */
Willy Tarreauae8b7962007-06-09 23:10:04 +02001225 opaque = 0;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001226 while (**args) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02001227 int ret;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001228 pattern = (struct acl_pattern *)calloc(1, sizeof(*pattern));
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001229 if (!pattern) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001230 memprintf(err, "out of memory when parsing ACL pattern");
Willy Tarreaua84d3742007-05-07 00:36:48 +02001231 goto out_free_expr;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001232 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +02001233 pattern->flags = patflags;
1234
Willy Tarreauc92ddbc2012-04-27 22:10:57 +02001235 pattern->type = SMP_TYPES; /* unspecified type */
Willy Tarreaud76a98a2013-03-31 18:34:33 +02001236 ret = expr->parse(args, pattern, &opaque, err);
Willy Tarreau7dcb6482012-04-27 17:52:25 +02001237 if (!ret)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001238 goto out_free_pattern;
Willy Tarreau7dcb6482012-04-27 17:52:25 +02001239
Willy Tarreaua84d3742007-05-07 00:36:48 +02001240 LIST_ADDQ(&expr->patterns, &pattern->list);
Willy Tarreauae8b7962007-06-09 23:10:04 +02001241 args += ret;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001242 }
1243
1244 return expr;
1245
1246 out_free_pattern:
1247 free_pattern(pattern);
1248 out_free_expr:
1249 prune_acl_expr(expr);
1250 free(expr);
1251 out_return:
1252 return NULL;
1253}
1254
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02001255/* Purge everything in the acl <acl>, then return <acl>. */
1256struct acl *prune_acl(struct acl *acl) {
1257
1258 struct acl_expr *expr, *exprb;
1259
1260 free(acl->name);
1261
1262 list_for_each_entry_safe(expr, exprb, &acl->expr, list) {
1263 LIST_DEL(&expr->list);
1264 prune_acl_expr(expr);
1265 free(expr);
1266 }
1267
1268 return acl;
1269}
1270
Willy Tarreaua84d3742007-05-07 00:36:48 +02001271/* Parse an ACL with the name starting at <args>[0], and with a list of already
1272 * known ACLs in <acl>. If the ACL was not in the list, it will be added.
Willy Tarreau2a56c5e2010-03-15 16:13:29 +01001273 * A pointer to that ACL is returned. If the ACL has an empty name, then it's
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001274 * an anonymous one and it won't be merged with any other one. If <err> is not
1275 * NULL, it will be filled with an appropriate error. This pointer must be
1276 * freeable or NULL.
Willy Tarreaua84d3742007-05-07 00:36:48 +02001277 *
1278 * args syntax: <aclname> <acl_expr>
1279 */
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001280struct acl *parse_acl(const char **args, struct list *known_acl, char **err)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001281{
1282 __label__ out_return, out_free_acl_expr, out_free_name;
1283 struct acl *cur_acl;
1284 struct acl_expr *acl_expr;
1285 char *name;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001286 const char *pos;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001287
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001288 if (**args && (pos = invalid_char(*args))) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001289 memprintf(err, "invalid character in ACL name : '%c'", *pos);
Willy Tarreau2e74c3f2007-12-02 18:45:09 +01001290 goto out_return;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001291 }
Willy Tarreau2e74c3f2007-12-02 18:45:09 +01001292
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001293 acl_expr = parse_acl_expr(args + 1, err);
1294 if (!acl_expr) {
1295 /* parse_acl_expr will have filled <err> here */
Willy Tarreaua84d3742007-05-07 00:36:48 +02001296 goto out_return;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001297 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001298
Willy Tarreau404e8ab2009-07-26 19:40:40 +02001299 /* Check for args beginning with an opening parenthesis just after the
1300 * subject, as this is almost certainly a typo. Right now we can only
1301 * emit a warning, so let's do so.
1302 */
Krzysztof Piotr Oledzki4cdd8312009-10-05 00:23:35 +02001303 if (!strchr(args[1], '(') && *args[2] == '(')
Willy Tarreau404e8ab2009-07-26 19:40:40 +02001304 Warning("parsing acl '%s' :\n"
1305 " matching '%s' for pattern '%s' is likely a mistake and probably\n"
1306 " not what you want. Maybe you need to remove the extraneous space before '('.\n"
1307 " If you are really sure this is not an error, please insert '--' between the\n"
1308 " match and the pattern to make this warning message disappear.\n",
1309 args[0], args[1], args[2]);
1310
Willy Tarreau2a56c5e2010-03-15 16:13:29 +01001311 if (*args[0])
1312 cur_acl = find_acl_by_name(args[0], known_acl);
1313 else
1314 cur_acl = NULL;
1315
Willy Tarreaua84d3742007-05-07 00:36:48 +02001316 if (!cur_acl) {
1317 name = strdup(args[0]);
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001318 if (!name) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001319 memprintf(err, "out of memory when parsing ACL");
Willy Tarreaua84d3742007-05-07 00:36:48 +02001320 goto out_free_acl_expr;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001321 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001322 cur_acl = (struct acl *)calloc(1, sizeof(*cur_acl));
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001323 if (cur_acl == NULL) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001324 memprintf(err, "out of memory when parsing ACL");
Willy Tarreaua84d3742007-05-07 00:36:48 +02001325 goto out_free_name;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001326 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001327
1328 LIST_INIT(&cur_acl->expr);
1329 LIST_ADDQ(known_acl, &cur_acl->list);
1330 cur_acl->name = name;
1331 }
1332
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001333 /* We want to know what features the ACL needs (typically HTTP parsing),
1334 * and where it may be used. If an ACL relies on multiple matches, it is
1335 * OK if at least one of them may match in the context where it is used.
1336 */
Willy Tarreaud76a98a2013-03-31 18:34:33 +02001337 cur_acl->use |= acl_expr->smp->use;
1338 cur_acl->val |= acl_expr->smp->val;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001339 LIST_ADDQ(&cur_acl->expr, &acl_expr->list);
1340 return cur_acl;
1341
1342 out_free_name:
1343 free(name);
1344 out_free_acl_expr:
1345 prune_acl_expr(acl_expr);
1346 free(acl_expr);
1347 out_return:
1348 return NULL;
1349}
1350
Willy Tarreau16fbe822007-06-17 11:54:31 +02001351/* Some useful ACLs provided by default. Only those used are allocated. */
1352
1353const struct {
1354 const char *name;
1355 const char *expr[4]; /* put enough for longest expression */
1356} default_acl_list[] = {
Willy Tarreau58393e12008-07-20 10:39:22 +02001357 { .name = "TRUE", .expr = {"always_true",""}},
1358 { .name = "FALSE", .expr = {"always_false",""}},
Willy Tarreau16fbe822007-06-17 11:54:31 +02001359 { .name = "LOCALHOST", .expr = {"src","127.0.0.1/8",""}},
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001360 { .name = "HTTP", .expr = {"req_proto_http",""}},
Willy Tarreau16fbe822007-06-17 11:54:31 +02001361 { .name = "HTTP_1.0", .expr = {"req_ver","1.0",""}},
1362 { .name = "HTTP_1.1", .expr = {"req_ver","1.1",""}},
1363 { .name = "METH_CONNECT", .expr = {"method","CONNECT",""}},
1364 { .name = "METH_GET", .expr = {"method","GET","HEAD",""}},
1365 { .name = "METH_HEAD", .expr = {"method","HEAD",""}},
1366 { .name = "METH_OPTIONS", .expr = {"method","OPTIONS",""}},
1367 { .name = "METH_POST", .expr = {"method","POST",""}},
1368 { .name = "METH_TRACE", .expr = {"method","TRACE",""}},
1369 { .name = "HTTP_URL_ABS", .expr = {"url_reg","^[^/:]*://",""}},
1370 { .name = "HTTP_URL_SLASH", .expr = {"url_beg","/",""}},
1371 { .name = "HTTP_URL_STAR", .expr = {"url","*",""}},
1372 { .name = "HTTP_CONTENT", .expr = {"hdr_val(content-length)","gt","0",""}},
Emeric Brunbede3d02009-06-30 17:54:00 +02001373 { .name = "RDP_COOKIE", .expr = {"req_rdp_cookie_cnt","gt","0",""}},
Willy Tarreauc6317702008-07-20 09:29:50 +02001374 { .name = "REQ_CONTENT", .expr = {"req_len","gt","0",""}},
Willy Tarreaub6fb4202008-07-20 11:18:28 +02001375 { .name = "WAIT_END", .expr = {"wait_end",""}},
Willy Tarreau16fbe822007-06-17 11:54:31 +02001376 { .name = NULL, .expr = {""}}
1377};
1378
1379/* Find a default ACL from the default_acl list, compile it and return it.
1380 * If the ACL is not found, NULL is returned. In theory, it cannot fail,
1381 * except when default ACLs are broken, in which case it will return NULL.
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001382 * If <known_acl> is not NULL, the ACL will be queued at its tail. If <err> is
1383 * not NULL, it will be filled with an error message if an error occurs. This
1384 * pointer must be freeable or NULL.
Willy Tarreau16fbe822007-06-17 11:54:31 +02001385 */
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001386struct acl *find_acl_default(const char *acl_name, struct list *known_acl, char **err)
Willy Tarreau16fbe822007-06-17 11:54:31 +02001387{
1388 __label__ out_return, out_free_acl_expr, out_free_name;
1389 struct acl *cur_acl;
1390 struct acl_expr *acl_expr;
1391 char *name;
1392 int index;
1393
1394 for (index = 0; default_acl_list[index].name != NULL; index++) {
1395 if (strcmp(acl_name, default_acl_list[index].name) == 0)
1396 break;
1397 }
1398
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001399 if (default_acl_list[index].name == NULL) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001400 memprintf(err, "no such ACL : '%s'", acl_name);
Willy Tarreau16fbe822007-06-17 11:54:31 +02001401 return NULL;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001402 }
Willy Tarreau16fbe822007-06-17 11:54:31 +02001403
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001404 acl_expr = parse_acl_expr((const char **)default_acl_list[index].expr, err);
1405 if (!acl_expr) {
1406 /* parse_acl_expr must have filled err here */
Willy Tarreau16fbe822007-06-17 11:54:31 +02001407 goto out_return;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001408 }
Willy Tarreau16fbe822007-06-17 11:54:31 +02001409
1410 name = strdup(acl_name);
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001411 if (!name) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001412 memprintf(err, "out of memory when building default ACL '%s'", acl_name);
Willy Tarreau16fbe822007-06-17 11:54:31 +02001413 goto out_free_acl_expr;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001414 }
1415
Willy Tarreau16fbe822007-06-17 11:54:31 +02001416 cur_acl = (struct acl *)calloc(1, sizeof(*cur_acl));
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001417 if (cur_acl == NULL) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001418 memprintf(err, "out of memory when building default ACL '%s'", acl_name);
Willy Tarreau16fbe822007-06-17 11:54:31 +02001419 goto out_free_name;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001420 }
Willy Tarreau16fbe822007-06-17 11:54:31 +02001421
1422 cur_acl->name = name;
Willy Tarreaud76a98a2013-03-31 18:34:33 +02001423 cur_acl->use |= acl_expr->smp->use;
1424 cur_acl->val |= acl_expr->smp->val;
Willy Tarreau16fbe822007-06-17 11:54:31 +02001425 LIST_INIT(&cur_acl->expr);
1426 LIST_ADDQ(&cur_acl->expr, &acl_expr->list);
1427 if (known_acl)
1428 LIST_ADDQ(known_acl, &cur_acl->list);
1429
1430 return cur_acl;
1431
1432 out_free_name:
1433 free(name);
1434 out_free_acl_expr:
1435 prune_acl_expr(acl_expr);
1436 free(acl_expr);
1437 out_return:
1438 return NULL;
1439}
Willy Tarreaua84d3742007-05-07 00:36:48 +02001440
1441/* Purge everything in the acl_cond <cond>, then return <cond>. */
1442struct acl_cond *prune_acl_cond(struct acl_cond *cond)
1443{
1444 struct acl_term_suite *suite, *tmp_suite;
1445 struct acl_term *term, *tmp_term;
1446
1447 /* iterate through all term suites and free all terms and all suites */
1448 list_for_each_entry_safe(suite, tmp_suite, &cond->suites, list) {
1449 list_for_each_entry_safe(term, tmp_term, &suite->terms, list)
1450 free(term);
1451 free(suite);
1452 }
1453 return cond;
1454}
1455
1456/* Parse an ACL condition starting at <args>[0], relying on a list of already
1457 * known ACLs passed in <known_acl>. The new condition is returned (or NULL in
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001458 * case of low memory). Supports multiple conditions separated by "or". If
1459 * <err> is not NULL, it will be filled with a pointer to an error message in
1460 * case of error, that the caller is responsible for freeing. The initial
1461 * location must either be freeable or NULL.
Willy Tarreaua84d3742007-05-07 00:36:48 +02001462 */
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001463struct acl_cond *parse_acl_cond(const char **args, struct list *known_acl, int pol, char **err)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001464{
1465 __label__ out_return, out_free_suite, out_free_term;
Willy Tarreau74b98a82007-06-16 19:35:18 +02001466 int arg, neg;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001467 const char *word;
1468 struct acl *cur_acl;
1469 struct acl_term *cur_term;
1470 struct acl_term_suite *cur_suite;
1471 struct acl_cond *cond;
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001472 unsigned int suite_val;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001473
1474 cond = (struct acl_cond *)calloc(1, sizeof(*cond));
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001475 if (cond == NULL) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001476 memprintf(err, "out of memory when parsing condition");
Willy Tarreaua84d3742007-05-07 00:36:48 +02001477 goto out_return;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001478 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001479
1480 LIST_INIT(&cond->list);
1481 LIST_INIT(&cond->suites);
1482 cond->pol = pol;
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001483 cond->val = 0;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001484
1485 cur_suite = NULL;
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001486 suite_val = ~0U;
Willy Tarreau74b98a82007-06-16 19:35:18 +02001487 neg = 0;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001488 for (arg = 0; *args[arg]; arg++) {
1489 word = args[arg];
1490
1491 /* remove as many exclamation marks as we can */
1492 while (*word == '!') {
1493 neg = !neg;
1494 word++;
1495 }
1496
1497 /* an empty word is allowed because we cannot force the user to
1498 * always think about not leaving exclamation marks alone.
1499 */
1500 if (!*word)
1501 continue;
1502
Willy Tarreau16fbe822007-06-17 11:54:31 +02001503 if (strcasecmp(word, "or") == 0 || strcmp(word, "||") == 0) {
Willy Tarreaua84d3742007-05-07 00:36:48 +02001504 /* new term suite */
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001505 cond->val |= suite_val;
1506 suite_val = ~0U;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001507 cur_suite = NULL;
1508 neg = 0;
1509 continue;
1510 }
1511
Willy Tarreau95fa4692010-02-01 13:05:50 +01001512 if (strcmp(word, "{") == 0) {
1513 /* we may have a complete ACL expression between two braces,
1514 * find the last one.
1515 */
1516 int arg_end = arg + 1;
1517 const char **args_new;
1518
1519 while (*args[arg_end] && strcmp(args[arg_end], "}") != 0)
1520 arg_end++;
1521
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001522 if (!*args[arg_end]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001523 memprintf(err, "missing closing '}' in condition");
Willy Tarreau95fa4692010-02-01 13:05:50 +01001524 goto out_free_suite;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001525 }
Willy Tarreau95fa4692010-02-01 13:05:50 +01001526
1527 args_new = calloc(1, (arg_end - arg + 1) * sizeof(*args_new));
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001528 if (!args_new) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001529 memprintf(err, "out of memory when parsing condition");
Willy Tarreau95fa4692010-02-01 13:05:50 +01001530 goto out_free_suite;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001531 }
Willy Tarreau95fa4692010-02-01 13:05:50 +01001532
Willy Tarreau2a56c5e2010-03-15 16:13:29 +01001533 args_new[0] = "";
Willy Tarreau95fa4692010-02-01 13:05:50 +01001534 memcpy(args_new + 1, args + arg + 1, (arg_end - arg) * sizeof(*args_new));
1535 args_new[arg_end - arg] = "";
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001536 cur_acl = parse_acl(args_new, known_acl, err);
Willy Tarreau95fa4692010-02-01 13:05:50 +01001537 free(args_new);
1538
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001539 if (!cur_acl) {
1540 /* note that parse_acl() must have filled <err> here */
Willy Tarreau16fbe822007-06-17 11:54:31 +02001541 goto out_free_suite;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001542 }
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001543 word = args[arg + 1];
Willy Tarreau95fa4692010-02-01 13:05:50 +01001544 arg = arg_end;
1545 }
1546 else {
1547 /* search for <word> in the known ACL names. If we do not find
1548 * it, let's look for it in the default ACLs, and if found, add
1549 * it to the list of ACLs of this proxy. This makes it possible
1550 * to override them.
1551 */
1552 cur_acl = find_acl_by_name(word, known_acl);
1553 if (cur_acl == NULL) {
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001554 cur_acl = find_acl_default(word, known_acl, err);
1555 if (cur_acl == NULL) {
1556 /* note that find_acl_default() must have filled <err> here */
Willy Tarreau95fa4692010-02-01 13:05:50 +01001557 goto out_free_suite;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001558 }
Willy Tarreau95fa4692010-02-01 13:05:50 +01001559 }
Willy Tarreau16fbe822007-06-17 11:54:31 +02001560 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001561
1562 cur_term = (struct acl_term *)calloc(1, sizeof(*cur_term));
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001563 if (cur_term == NULL) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001564 memprintf(err, "out of memory when parsing condition");
Willy Tarreaua84d3742007-05-07 00:36:48 +02001565 goto out_free_suite;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001566 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001567
1568 cur_term->acl = cur_acl;
1569 cur_term->neg = neg;
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001570
1571 /* Here it is a bit complex. The acl_term_suite is a conjunction
1572 * of many terms. It may only be used if all of its terms are
1573 * usable at the same time. So the suite's validity domain is an
1574 * AND between all ACL keywords' ones. But, the global condition
1575 * is valid if at least one term suite is OK. So it's an OR between
1576 * all of their validity domains. We could emit a warning as soon
1577 * as suite_val is null because it means that the last ACL is not
1578 * compatible with the previous ones. Let's remain simple for now.
1579 */
1580 cond->use |= cur_acl->use;
1581 suite_val &= cur_acl->val;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001582
1583 if (!cur_suite) {
1584 cur_suite = (struct acl_term_suite *)calloc(1, sizeof(*cur_suite));
Willy Tarreauf678b7f2013-01-24 00:25:39 +01001585 if (cur_suite == NULL) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001586 memprintf(err, "out of memory when parsing condition");
Willy Tarreaua84d3742007-05-07 00:36:48 +02001587 goto out_free_term;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001588 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001589 LIST_INIT(&cur_suite->terms);
1590 LIST_ADDQ(&cond->suites, &cur_suite->list);
1591 }
1592 LIST_ADDQ(&cur_suite->terms, &cur_term->list);
Willy Tarreau74b98a82007-06-16 19:35:18 +02001593 neg = 0;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001594 }
1595
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001596 cond->val |= suite_val;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001597 return cond;
1598
1599 out_free_term:
1600 free(cur_term);
1601 out_free_suite:
1602 prune_acl_cond(cond);
1603 free(cond);
1604 out_return:
1605 return NULL;
1606}
1607
Willy Tarreau2bbba412010-01-28 16:48:33 +01001608/* Builds an ACL condition starting at the if/unless keyword. The complete
1609 * condition is returned. NULL is returned in case of error or if the first
1610 * word is neither "if" nor "unless". It automatically sets the file name and
Willy Tarreau25320b22013-03-24 07:22:08 +01001611 * the line number in the condition for better error reporting, and sets the
1612 * HTTP intiailization requirements in the proxy. If <err> is not NULL, it will
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001613 * be filled with a pointer to an error message in case of error, that the
1614 * caller is responsible for freeing. The initial location must either be
1615 * freeable or NULL.
Willy Tarreau2bbba412010-01-28 16:48:33 +01001616 */
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001617struct 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 +01001618{
1619 int pol = ACL_COND_NONE;
1620 struct acl_cond *cond = NULL;
1621
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001622 if (err)
1623 *err = NULL;
1624
Willy Tarreau2bbba412010-01-28 16:48:33 +01001625 if (!strcmp(*args, "if")) {
1626 pol = ACL_COND_IF;
1627 args++;
1628 }
1629 else if (!strcmp(*args, "unless")) {
1630 pol = ACL_COND_UNLESS;
1631 args++;
1632 }
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001633 else {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001634 memprintf(err, "conditions must start with either 'if' or 'unless'");
Willy Tarreau2bbba412010-01-28 16:48:33 +01001635 return NULL;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001636 }
Willy Tarreau2bbba412010-01-28 16:48:33 +01001637
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001638 cond = parse_acl_cond(args, &px->acl, pol, err);
1639 if (!cond) {
1640 /* note that parse_acl_cond must have filled <err> here */
Willy Tarreau2bbba412010-01-28 16:48:33 +01001641 return NULL;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001642 }
Willy Tarreau2bbba412010-01-28 16:48:33 +01001643
1644 cond->file = file;
1645 cond->line = line;
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001646 px->http_needed |= !!(cond->use & SMP_USE_HTTP_ANY);
Willy Tarreau2bbba412010-01-28 16:48:33 +01001647 return cond;
1648}
1649
Willy Tarreau11382812008-07-09 16:18:21 +02001650/* Execute condition <cond> and return either ACL_PAT_FAIL, ACL_PAT_MISS or
Willy Tarreaub6866442008-07-14 23:54:42 +02001651 * ACL_PAT_PASS depending on the test results. ACL_PAT_MISS may only be
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001652 * returned if <opt> does not contain SMP_OPT_FINAL, indicating that incomplete
Willy Tarreau7a777ed2012-04-26 11:44:02 +02001653 * data is being examined. The function automatically sets SMP_OPT_ITERATE.
Willy Tarreaub6866442008-07-14 23:54:42 +02001654 * This function only computes the condition, it does not apply the polarity
1655 * required by IF/UNLESS, it's up to the caller to do this using something like
1656 * this :
Willy Tarreau11382812008-07-09 16:18:21 +02001657 *
1658 * res = acl_pass(res);
Willy Tarreaub6866442008-07-14 23:54:42 +02001659 * if (res == ACL_PAT_MISS)
1660 * return 0;
Willy Tarreau11382812008-07-09 16:18:21 +02001661 * if (cond->pol == ACL_COND_UNLESS)
1662 * res = !res;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001663 */
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001664int 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 +02001665{
1666 __label__ fetch_next;
1667 struct acl_term_suite *suite;
1668 struct acl_term *term;
1669 struct acl_expr *expr;
1670 struct acl *acl;
1671 struct acl_pattern *pattern;
Willy Tarreau37406352012-04-23 16:16:37 +02001672 struct sample smp;
Willy Tarreau11382812008-07-09 16:18:21 +02001673 int acl_res, suite_res, cond_res;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001674
Willy Tarreau7a777ed2012-04-26 11:44:02 +02001675 /* ACLs are iterated over all values, so let's always set the flag to
1676 * indicate this to the fetch functions.
1677 */
1678 opt |= SMP_OPT_ITERATE;
1679
Willy Tarreau11382812008-07-09 16:18:21 +02001680 /* We're doing a logical OR between conditions so we initialize to FAIL.
1681 * The MISS status is propagated down from the suites.
1682 */
Willy Tarreaua84d3742007-05-07 00:36:48 +02001683 cond_res = ACL_PAT_FAIL;
1684 list_for_each_entry(suite, &cond->suites, list) {
Willy Tarreau11382812008-07-09 16:18:21 +02001685 /* Evaluate condition suite <suite>. We stop at the first term
1686 * which returns ACL_PAT_FAIL. The MISS status is still propagated
1687 * in case of uncertainty in the result.
Willy Tarreaua84d3742007-05-07 00:36:48 +02001688 */
1689
1690 /* we're doing a logical AND between terms, so we must set the
1691 * initial value to PASS.
1692 */
1693 suite_res = ACL_PAT_PASS;
1694 list_for_each_entry(term, &suite->terms, list) {
1695 acl = term->acl;
1696
1697 /* FIXME: use cache !
1698 * check acl->cache_idx for this.
1699 */
1700
1701 /* ACL result not cached. Let's scan all the expressions
1702 * and use the first one to match.
1703 */
1704 acl_res = ACL_PAT_FAIL;
1705 list_for_each_entry(expr, &acl->expr, list) {
Willy Tarreaud41f8d82007-06-10 10:06:18 +02001706 /* we need to reset context and flags */
Willy Tarreau37406352012-04-23 16:16:37 +02001707 memset(&smp, 0, sizeof(smp));
Willy Tarreaua84d3742007-05-07 00:36:48 +02001708 fetch_next:
Willy Tarreaud76a98a2013-03-31 18:34:33 +02001709 if (!expr->smp->process(px, l4, l7, opt, expr->args, &smp)) {
Willy Tarreaub6866442008-07-14 23:54:42 +02001710 /* maybe we could not fetch because of missing data */
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001711 if (smp.flags & SMP_F_MAY_CHANGE && !(opt & SMP_OPT_FINAL))
Willy Tarreaub6866442008-07-14 23:54:42 +02001712 acl_res |= ACL_PAT_MISS;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001713 continue;
Willy Tarreaub6866442008-07-14 23:54:42 +02001714 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001715
Willy Tarreau197e10a2012-04-23 19:18:42 +02001716 if (smp.type == SMP_T_BOOL) {
1717 if (smp.data.uint)
Willy Tarreaua79534f2008-07-20 10:13:37 +02001718 acl_res |= ACL_PAT_PASS;
1719 else
1720 acl_res |= ACL_PAT_FAIL;
1721 }
Willy Tarreau5adeda12013-03-31 22:13:34 +02001722 else if (!expr->match) {
1723 /* just check for existence */
1724 acl_res |= ACL_PAT_PASS;
1725 }
Willy Tarreaua79534f2008-07-20 10:13:37 +02001726 else {
Willy Tarreau020534d2010-05-16 21:45:45 +02001727 if (!eb_is_empty(&expr->pattern_tree)) {
Willy Tarreauc4262962010-05-10 23:42:40 +02001728 /* a tree is present, let's check what type it is */
Willy Tarreaud76a98a2013-03-31 18:34:33 +02001729 if (expr->match == acl_match_str)
Willy Tarreau37406352012-04-23 16:16:37 +02001730 acl_res |= acl_lookup_str(&smp, expr) ? ACL_PAT_PASS : ACL_PAT_FAIL;
Willy Tarreaud76a98a2013-03-31 18:34:33 +02001731 else if (expr->match == acl_match_ip)
Willy Tarreau37406352012-04-23 16:16:37 +02001732 acl_res |= acl_lookup_ip(&smp, expr) ? ACL_PAT_PASS : ACL_PAT_FAIL;
Willy Tarreauc4262962010-05-10 23:42:40 +02001733 }
1734
Willy Tarreaua79534f2008-07-20 10:13:37 +02001735 /* call the match() function for all tests on this value */
1736 list_for_each_entry(pattern, &expr->patterns, list) {
Willy Tarreaua79534f2008-07-20 10:13:37 +02001737 if (acl_res == ACL_PAT_PASS)
1738 break;
Willy Tarreaud76a98a2013-03-31 18:34:33 +02001739 acl_res |= expr->match(&smp, pattern);
Willy Tarreaua79534f2008-07-20 10:13:37 +02001740 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001741 }
1742 /*
Willy Tarreau11382812008-07-09 16:18:21 +02001743 * OK now acl_res holds the result of this expression
1744 * as one of ACL_PAT_FAIL, ACL_PAT_MISS or ACL_PAT_PASS.
Willy Tarreaua84d3742007-05-07 00:36:48 +02001745 *
Willy Tarreau11382812008-07-09 16:18:21 +02001746 * Then if (!MISS) we can cache the result, and put
Willy Tarreau37406352012-04-23 16:16:37 +02001747 * (smp.flags & SMP_F_VOLATILE) in the cache flags.
Willy Tarreaua84d3742007-05-07 00:36:48 +02001748 *
1749 * FIXME: implement cache.
1750 *
1751 */
1752
Willy Tarreau11382812008-07-09 16:18:21 +02001753 /* we're ORing these terms, so a single PASS is enough */
1754 if (acl_res == ACL_PAT_PASS)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001755 break;
1756
Willy Tarreau37406352012-04-23 16:16:37 +02001757 if (smp.flags & SMP_F_NOT_LAST)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001758 goto fetch_next;
Willy Tarreaub6866442008-07-14 23:54:42 +02001759
1760 /* sometimes we know the fetched data is subject to change
1761 * later and give another chance for a new match (eg: request
1762 * size, time, ...)
1763 */
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001764 if (smp.flags & SMP_F_MAY_CHANGE && !(opt & SMP_OPT_FINAL))
Willy Tarreaub6866442008-07-14 23:54:42 +02001765 acl_res |= ACL_PAT_MISS;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001766 }
1767 /*
1768 * Here we have the result of an ACL (cached or not).
1769 * ACLs are combined, negated or not, to form conditions.
1770 */
1771
Willy Tarreaua84d3742007-05-07 00:36:48 +02001772 if (term->neg)
Willy Tarreau11382812008-07-09 16:18:21 +02001773 acl_res = acl_neg(acl_res);
Willy Tarreaua84d3742007-05-07 00:36:48 +02001774
1775 suite_res &= acl_res;
Willy Tarreau11382812008-07-09 16:18:21 +02001776
1777 /* we're ANDing these terms, so a single FAIL is enough */
1778 if (suite_res == ACL_PAT_FAIL)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001779 break;
1780 }
1781 cond_res |= suite_res;
Willy Tarreau11382812008-07-09 16:18:21 +02001782
1783 /* we're ORing these terms, so a single PASS is enough */
1784 if (cond_res == ACL_PAT_PASS)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001785 break;
1786 }
Willy Tarreau11382812008-07-09 16:18:21 +02001787 return cond_res;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001788}
1789
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001790/* Returns a pointer to the first ACL conflicting with usage at place <where>
1791 * which is one of the SMP_VAL_* bits indicating a check place, or NULL if
1792 * no conflict is found. Only full conflicts are detected (ACL is not usable).
1793 * Use the next function to check for useless keywords.
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001794 */
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001795const struct acl *acl_cond_conflicts(const struct acl_cond *cond, unsigned int where)
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001796{
1797 struct acl_term_suite *suite;
1798 struct acl_term *term;
1799 struct acl *acl;
1800
1801 list_for_each_entry(suite, &cond->suites, list) {
1802 list_for_each_entry(term, &suite->terms, list) {
1803 acl = term->acl;
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001804 if (!(acl->val & where))
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001805 return acl;
1806 }
1807 }
1808 return NULL;
1809}
1810
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001811/* Returns a pointer to the first ACL and its first keyword to conflict with
1812 * usage at place <where> which is one of the SMP_VAL_* bits indicating a check
1813 * place. Returns true if a conflict is found, with <acl> and <kw> set (if non
1814 * null), or false if not conflict is found. The first useless keyword is
1815 * returned.
1816 */
Willy Tarreau93fddf12013-03-31 22:59:32 +02001817int 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 +01001818{
1819 struct acl_term_suite *suite;
1820 struct acl_term *term;
1821 struct acl_expr *expr;
1822
1823 list_for_each_entry(suite, &cond->suites, list) {
1824 list_for_each_entry(term, &suite->terms, list) {
1825 list_for_each_entry(expr, &term->acl->expr, list) {
Willy Tarreaud76a98a2013-03-31 18:34:33 +02001826 if (!(expr->smp->val & where)) {
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001827 if (acl)
1828 *acl = term->acl;
1829 if (kw)
1830 *kw = expr->kw;
1831 return 1;
1832 }
1833 }
1834 }
1835 }
1836 return 0;
1837}
1838
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001839/*
1840 * Find targets for userlist and groups in acl. Function returns the number
1841 * of errors or OK if everything is fine.
1842 */
1843int
1844acl_find_targets(struct proxy *p)
1845{
1846
1847 struct acl *acl;
1848 struct acl_expr *expr;
1849 struct acl_pattern *pattern;
1850 struct userlist *ul;
Willy Tarreau63364ee2012-04-19 19:11:13 +02001851 struct arg *arg;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001852 int cfgerr = 0;
1853
1854 list_for_each_entry(acl, &p->acl, list) {
1855 list_for_each_entry(expr, &acl->expr, list) {
Willy Tarreau2e845be2012-10-19 19:49:09 +02001856 for (arg = expr->args; arg && arg->type != ARGT_STOP; arg++) {
1857 if (!arg->unresolved)
Willy Tarreau496aa012012-06-01 10:38:29 +02001858 continue;
Willy Tarreau63364ee2012-04-19 19:11:13 +02001859 else if (arg->type == ARGT_SRV) {
1860 struct proxy *px;
1861 struct server *srv;
1862 char *pname, *sname;
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001863
Willy Tarreau7d1df412012-11-23 23:47:36 +01001864 if (!arg->data.str.len) {
Willy Tarreau63364ee2012-04-19 19:11:13 +02001865 Alert("proxy %s: acl '%s' %s(): missing server name.\n",
Willy Tarreau93fddf12013-03-31 22:59:32 +02001866 p->id, acl->name, expr->kw);
Willy Tarreau63364ee2012-04-19 19:11:13 +02001867 cfgerr++;
1868 continue;
1869 }
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001870
Willy Tarreau7d1df412012-11-23 23:47:36 +01001871 pname = arg->data.str.str;
Willy Tarreau63364ee2012-04-19 19:11:13 +02001872 sname = strrchr(pname, '/');
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001873
Willy Tarreau63364ee2012-04-19 19:11:13 +02001874 if (sname)
1875 *sname++ = '\0';
1876 else {
1877 sname = pname;
1878 pname = NULL;
1879 }
1880
1881 px = p;
1882 if (pname) {
1883 px = findproxy(pname, PR_CAP_BE);
1884 if (!px) {
1885 Alert("proxy %s: acl '%s' %s(): unable to find proxy '%s'.\n",
Willy Tarreau93fddf12013-03-31 22:59:32 +02001886 p->id, acl->name, expr->kw, pname);
Willy Tarreau63364ee2012-04-19 19:11:13 +02001887 cfgerr++;
1888 continue;
1889 }
1890 }
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001891
Willy Tarreau63364ee2012-04-19 19:11:13 +02001892 srv = findserver(px, sname);
1893 if (!srv) {
1894 Alert("proxy %s: acl '%s' %s(): unable to find server '%s'.\n",
Willy Tarreau93fddf12013-03-31 22:59:32 +02001895 p->id, acl->name, expr->kw, sname);
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001896 cfgerr++;
1897 continue;
1898 }
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001899
Willy Tarreau7d1df412012-11-23 23:47:36 +01001900 free(arg->data.str.str);
1901 arg->data.str.str = NULL;
Willy Tarreau496aa012012-06-01 10:38:29 +02001902 arg->unresolved = 0;
Willy Tarreau7d1df412012-11-23 23:47:36 +01001903 arg->data.srv = srv;
Willy Tarreaud28c3532012-04-19 19:28:33 +02001904 }
1905 else if (arg->type == ARGT_FE) {
Willy Tarreaufc2c1fd2012-04-19 23:35:54 +02001906 struct proxy *prx = p;
1907 char *pname = p->id;
Willy Tarreaud28c3532012-04-19 19:28:33 +02001908
Willy Tarreau7d1df412012-11-23 23:47:36 +01001909 if (arg->data.str.len) {
1910 pname = arg->data.str.str;
Willy Tarreaufc2c1fd2012-04-19 23:35:54 +02001911 prx = findproxy(pname, PR_CAP_FE);
Willy Tarreaud28c3532012-04-19 19:28:33 +02001912 }
1913
Willy Tarreaud28c3532012-04-19 19:28:33 +02001914 if (!prx) {
1915 Alert("proxy %s: acl '%s' %s(): unable to find frontend '%s'.\n",
Willy Tarreau93fddf12013-03-31 22:59:32 +02001916 p->id, acl->name, expr->kw, pname);
Willy Tarreaud28c3532012-04-19 19:28:33 +02001917 cfgerr++;
1918 continue;
1919 }
1920
Willy Tarreaufc2c1fd2012-04-19 23:35:54 +02001921 if (!(prx->cap & PR_CAP_FE)) {
1922 Alert("proxy %s: acl '%s' %s(): proxy '%s' has no frontend capability.\n",
Willy Tarreau93fddf12013-03-31 22:59:32 +02001923 p->id, acl->name, expr->kw, pname);
Willy Tarreaufc2c1fd2012-04-19 23:35:54 +02001924 cfgerr++;
1925 continue;
1926 }
1927
Willy Tarreau7d1df412012-11-23 23:47:36 +01001928 free(arg->data.str.str);
1929 arg->data.str.str = NULL;
Willy Tarreau496aa012012-06-01 10:38:29 +02001930 arg->unresolved = 0;
Willy Tarreau7d1df412012-11-23 23:47:36 +01001931 arg->data.prx = prx;
Willy Tarreaud28c3532012-04-19 19:28:33 +02001932 }
1933 else if (arg->type == ARGT_BE) {
Willy Tarreaufc2c1fd2012-04-19 23:35:54 +02001934 struct proxy *prx = p;
1935 char *pname = p->id;
Willy Tarreaud28c3532012-04-19 19:28:33 +02001936
Willy Tarreau7d1df412012-11-23 23:47:36 +01001937 if (arg->data.str.len) {
1938 pname = arg->data.str.str;
Willy Tarreaufc2c1fd2012-04-19 23:35:54 +02001939 prx = findproxy(pname, PR_CAP_BE);
Willy Tarreaud28c3532012-04-19 19:28:33 +02001940 }
1941
Willy Tarreaud28c3532012-04-19 19:28:33 +02001942 if (!prx) {
1943 Alert("proxy %s: acl '%s' %s(): unable to find backend '%s'.\n",
Willy Tarreau93fddf12013-03-31 22:59:32 +02001944 p->id, acl->name, expr->kw, pname);
Willy Tarreaud28c3532012-04-19 19:28:33 +02001945 cfgerr++;
1946 continue;
1947 }
1948
Willy Tarreaufc2c1fd2012-04-19 23:35:54 +02001949 if (!(prx->cap & PR_CAP_BE)) {
1950 Alert("proxy %s: acl '%s' %s(): proxy '%s' has no backend capability.\n",
Willy Tarreau93fddf12013-03-31 22:59:32 +02001951 p->id, acl->name, expr->kw, pname);
Willy Tarreaufc2c1fd2012-04-19 23:35:54 +02001952 cfgerr++;
1953 continue;
1954 }
1955
Willy Tarreau7d1df412012-11-23 23:47:36 +01001956 free(arg->data.str.str);
1957 arg->data.str.str = NULL;
Willy Tarreau496aa012012-06-01 10:38:29 +02001958 arg->unresolved = 0;
Willy Tarreau7d1df412012-11-23 23:47:36 +01001959 arg->data.prx = prx;
Willy Tarreaud28c3532012-04-19 19:28:33 +02001960 }
1961 else if (arg->type == ARGT_TAB) {
Willy Tarreaufc2c1fd2012-04-19 23:35:54 +02001962 struct proxy *prx = p;
1963 char *pname = p->id;
Willy Tarreaud28c3532012-04-19 19:28:33 +02001964
Willy Tarreau7d1df412012-11-23 23:47:36 +01001965 if (arg->data.str.len) {
1966 pname = arg->data.str.str;
Willy Tarreaufc2c1fd2012-04-19 23:35:54 +02001967 prx = find_stktable(pname);
Willy Tarreaud28c3532012-04-19 19:28:33 +02001968 }
1969
Willy Tarreaud28c3532012-04-19 19:28:33 +02001970 if (!prx) {
1971 Alert("proxy %s: acl '%s' %s(): unable to find table '%s'.\n",
Willy Tarreau93fddf12013-03-31 22:59:32 +02001972 p->id, acl->name, expr->kw, pname);
Willy Tarreaud28c3532012-04-19 19:28:33 +02001973 cfgerr++;
1974 continue;
1975 }
1976
Willy Tarreaufc2c1fd2012-04-19 23:35:54 +02001977
1978 if (!prx->table.size) {
1979 Alert("proxy %s: acl '%s' %s(): no table in proxy '%s'.\n",
Willy Tarreau93fddf12013-03-31 22:59:32 +02001980 p->id, acl->name, expr->kw, pname);
Willy Tarreaufc2c1fd2012-04-19 23:35:54 +02001981 cfgerr++;
1982 continue;
1983 }
1984
Willy Tarreau7d1df412012-11-23 23:47:36 +01001985 free(arg->data.str.str);
1986 arg->data.str.str = NULL;
Willy Tarreau496aa012012-06-01 10:38:29 +02001987 arg->unresolved = 0;
Willy Tarreau7d1df412012-11-23 23:47:36 +01001988 arg->data.prx = prx;
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001989 }
Willy Tarreau63364ee2012-04-19 19:11:13 +02001990 else if (arg->type == ARGT_USR) {
Willy Tarreau7d1df412012-11-23 23:47:36 +01001991 if (!arg->data.str.len) {
Willy Tarreau63364ee2012-04-19 19:11:13 +02001992 Alert("proxy %s: acl '%s' %s(): missing userlist name.\n",
Willy Tarreau93fddf12013-03-31 22:59:32 +02001993 p->id, acl->name, expr->kw);
Willy Tarreau63364ee2012-04-19 19:11:13 +02001994 cfgerr++;
1995 continue;
1996 }
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001997
Willy Tarreau63364ee2012-04-19 19:11:13 +02001998 if (p->uri_auth && p->uri_auth->userlist &&
Willy Tarreau7d1df412012-11-23 23:47:36 +01001999 !strcmp(p->uri_auth->userlist->name, arg->data.str.str))
Willy Tarreau63364ee2012-04-19 19:11:13 +02002000 ul = p->uri_auth->userlist;
2001 else
Willy Tarreau7d1df412012-11-23 23:47:36 +01002002 ul = auth_find_userlist(arg->data.str.str);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01002003
Willy Tarreau63364ee2012-04-19 19:11:13 +02002004 if (!ul) {
2005 Alert("proxy %s: acl '%s' %s(%s): unable to find userlist.\n",
Willy Tarreau93fddf12013-03-31 22:59:32 +02002006 p->id, acl->name, expr->kw, arg->data.str.str);
Willy Tarreau63364ee2012-04-19 19:11:13 +02002007 cfgerr++;
2008 continue;
2009 }
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01002010
Willy Tarreau7d1df412012-11-23 23:47:36 +01002011 free(arg->data.str.str);
2012 arg->data.str.str = NULL;
Willy Tarreau496aa012012-06-01 10:38:29 +02002013 arg->unresolved = 0;
Willy Tarreau7d1df412012-11-23 23:47:36 +01002014 arg->data.usr = ul;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01002015 }
Willy Tarreau63364ee2012-04-19 19:11:13 +02002016 } /* end of args processing */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01002017
Willy Tarreau46b39d02012-05-10 23:40:14 +02002018 /* don't try to resolve groups if we're not certain of having
2019 * resolved userlists first.
2020 */
2021 if (cfgerr)
2022 break;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01002023
Willy Tarreau93fddf12013-03-31 22:59:32 +02002024 if (!strcmp(expr->kw, "http_auth_group")) {
Willy Tarreau63364ee2012-04-19 19:11:13 +02002025 /* note: argument resolved above thanks to ARGT_USR */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01002026
2027 if (LIST_ISEMPTY(&expr->patterns)) {
2028 Alert("proxy %s: acl %s %s(): no groups specified.\n",
Willy Tarreau93fddf12013-03-31 22:59:32 +02002029 p->id, acl->name, expr->kw);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01002030 cfgerr++;
2031 continue;
2032 }
2033
2034 list_for_each_entry(pattern, &expr->patterns, list) {
Willy Tarreau7d1df412012-11-23 23:47:36 +01002035 /* this keyword only has one argument */
Willy Tarreau34db1082012-04-19 17:16:54 +02002036 pattern->val.group_mask = auth_resolve_groups(expr->args->data.usr, pattern->ptr.str);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01002037
2038 free(pattern->ptr.str);
2039 pattern->ptr.str = NULL;
2040 pattern->len = 0;
2041
2042 if (!pattern->val.group_mask) {
2043 Alert("proxy %s: acl %s %s(): invalid group(s).\n",
Willy Tarreau93fddf12013-03-31 22:59:32 +02002044 p->id, acl->name, expr->kw);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01002045 cfgerr++;
2046 continue;
2047 }
2048 }
2049 }
2050 }
2051 }
2052
2053 return cfgerr;
2054}
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02002055
Willy Tarreau8ed669b2013-01-11 15:49:37 +01002056/* initializes ACLs by resolving the sample fetch names they rely upon.
2057 * Returns 0 on success, otherwise an error.
2058 */
2059int init_acl()
2060{
2061 int err = 0;
2062 int index;
2063 const char *name;
2064 struct acl_kw_list *kwl;
2065 struct sample_fetch *smp;
2066
2067 list_for_each_entry(kwl, &acl_keywords.list, list) {
2068 for (index = 0; kwl->kw[index].kw != NULL; index++) {
2069 name = kwl->kw[index].fetch_kw;
2070 if (!name)
2071 name = kwl->kw[index].kw;
2072
2073 smp = find_sample_fetch(name, strlen(name));
2074 if (!smp) {
2075 Alert("Critical internal error: ACL keyword '%s' relies on sample fetch '%s' which was not registered!\n",
2076 kwl->kw[index].kw, name);
2077 err++;
2078 continue;
2079 }
2080 kwl->kw[index].smp = smp;
2081 }
2082 }
2083 return err;
2084}
Willy Tarreaud4c33c82013-01-07 21:59:07 +01002085
Willy Tarreaua84d3742007-05-07 00:36:48 +02002086/************************************************************************/
Willy Tarreaud4c33c82013-01-07 21:59:07 +01002087/* All supported sample fetch functions must be declared here */
Willy Tarreaua84d3742007-05-07 00:36:48 +02002088/************************************************************************/
2089
Willy Tarreaud4c33c82013-01-07 21:59:07 +01002090/* force TRUE to be returned at the fetch level */
2091static int
2092smp_fetch_true(struct proxy *px, struct session *s, void *l7, unsigned int opt,
2093 const struct arg *args, struct sample *smp)
2094{
2095 smp->type = SMP_T_BOOL;
2096 smp->data.uint = 1;
2097 return 1;
2098}
2099
2100/* force FALSE to be returned at the fetch level */
2101static int
2102smp_fetch_false(struct proxy *px, struct session *s, void *l7, unsigned int opt,
2103 const struct arg *args, struct sample *smp)
2104{
2105 smp->type = SMP_T_BOOL;
2106 smp->data.uint = 0;
2107 return 1;
2108}
2109
2110
2111/************************************************************************/
2112/* All supported sample and ACL keywords must be declared here. */
2113/************************************************************************/
2114
2115/* Note: must not be declared <const> as its list will be overwritten.
2116 * Note: fetches that may return multiple types must be declared as the lowest
2117 * common denominator, the type that can be casted into all other ones. For
2118 * instance IPv4/IPv6 must be declared IPv4.
2119 */
2120static struct sample_fetch_kw_list smp_kws = {{ },{
2121 { "always_false", smp_fetch_false, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
2122 { "always_true", smp_fetch_true, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
2123 { /* END */ },
2124}};
2125
2126
Willy Tarreau61612d42012-04-19 18:42:05 +02002127/* Note: must not be declared <const> as its list will be overwritten.
2128 * Please take care of keeping this list alphabetically sorted.
2129 */
Willy Tarreaua84d3742007-05-07 00:36:48 +02002130static struct acl_kw_list acl_kws = {{ },{
Willy Tarreaud86e29d2013-03-25 08:21:05 +01002131 { "always_false", NULL, acl_parse_nothing, acl_match_nothing },
2132 { "always_true", NULL, acl_parse_nothing, acl_match_nothing },
Willy Tarreaud4c33c82013-01-07 21:59:07 +01002133 { /* END */ },
Willy Tarreaua84d3742007-05-07 00:36:48 +02002134}};
2135
2136
2137__attribute__((constructor))
2138static void __acl_init(void)
2139{
Willy Tarreaud4c33c82013-01-07 21:59:07 +01002140 sample_register_fetches(&smp_kws);
Willy Tarreaua84d3742007-05-07 00:36:48 +02002141 acl_register_keywords(&acl_kws);
2142}
2143
2144
2145/*
2146 * Local variables:
2147 * c-indent-level: 8
2148 * c-basic-offset: 8
2149 * End:
2150 */