blob: ca24693df5b282d0b0e0ee2c806f7beccd2bf340 [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
Thierry FOURNIER319e4952013-11-22 17:25:35 +010040char *acl_match_names[ACL_MATCH_NUM] = {
Willy Tarreau5adeda12013-03-31 22:13:34 +020041 [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
Thierry FOURNIERdd69a042013-11-22 19:14:42 +010056int (*acl_parse_fcts[ACL_MATCH_NUM])(const char **, struct acl_pattern *, struct sample_storage *, int *, char **) = {
Willy Tarreau5adeda12013-03-31 22:13:34 +020057 [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
Thierry FOURNIER319e4952013-11-22 17:25:35 +010072int (*acl_match_fcts[ACL_MATCH_NUM])(struct sample *, struct acl_pattern *) = {
Willy Tarreau5adeda12013-03-31 22:13:34 +020073 [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 */
Thierry FOURNIERdd69a042013-11-22 19:14:42 +0100104int acl_parse_nothing(const char **text, struct acl_pattern *pattern, struct sample_storage *smp, int *opaque, char **err)
Willy Tarreaua5909832007-06-17 20:40:25 +0200105{
Willy Tarreau58393e12008-07-20 10:39:22 +0200106 return 1;
107}
108
Willy Tarreaua5909832007-06-17 20:40:25 +0200109/* always return false */
Willy Tarreau37406352012-04-23 16:16:37 +0200110int acl_match_nothing(struct sample *smp, struct acl_pattern *pattern)
Willy Tarreaua5909832007-06-17 20:40:25 +0200111{
Willy Tarreau11382812008-07-09 16:18:21 +0200112 return ACL_PAT_FAIL;
Willy Tarreaua5909832007-06-17 20:40:25 +0200113}
114
115
Willy Tarreaua84d3742007-05-07 00:36:48 +0200116/* NB: For two strings to be identical, it is required that their lengths match */
Willy Tarreau37406352012-04-23 16:16:37 +0200117int acl_match_str(struct sample *smp, struct acl_pattern *pattern)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200118{
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200119 int icase;
120
Willy Tarreauf853c462012-04-23 18:53:56 +0200121 if (pattern->len != smp->data.str.len)
Willy Tarreau11382812008-07-09 16:18:21 +0200122 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200123
124 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
Willy Tarreauf853c462012-04-23 18:53:56 +0200125 if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) == 0) ||
126 (!icase && strncmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) == 0))
Willy Tarreau11382812008-07-09 16:18:21 +0200127 return ACL_PAT_PASS;
128 return ACL_PAT_FAIL;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200129}
130
Emeric Brun07ca4962012-10-17 13:38:19 +0200131/* NB: For two binaries buf to be identical, it is required that their lengths match */
132int acl_match_bin(struct sample *smp, struct acl_pattern *pattern)
133{
134 if (pattern->len != smp->data.str.len)
135 return ACL_PAT_FAIL;
136
137 if (memcmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) == 0)
138 return ACL_PAT_PASS;
139 return ACL_PAT_FAIL;
140}
141
Willy Tarreauc4262962010-05-10 23:42:40 +0200142/* Lookup a string in the expression's pattern tree. The node is returned if it
143 * exists, otherwise NULL.
144 */
Willy Tarreau37406352012-04-23 16:16:37 +0200145static void *acl_lookup_str(struct sample *smp, struct acl_expr *expr)
Willy Tarreauc4262962010-05-10 23:42:40 +0200146{
147 /* data are stored in a tree */
148 struct ebmb_node *node;
149 char prev;
150
151 /* we may have to force a trailing zero on the test pattern */
Willy Tarreauf853c462012-04-23 18:53:56 +0200152 prev = smp->data.str.str[smp->data.str.len];
Willy Tarreauc4262962010-05-10 23:42:40 +0200153 if (prev)
Willy Tarreauf853c462012-04-23 18:53:56 +0200154 smp->data.str.str[smp->data.str.len] = '\0';
155 node = ebst_lookup(&expr->pattern_tree, smp->data.str.str);
Willy Tarreauc4262962010-05-10 23:42:40 +0200156 if (prev)
Willy Tarreauf853c462012-04-23 18:53:56 +0200157 smp->data.str.str[smp->data.str.len] = prev;
Willy Tarreauc4262962010-05-10 23:42:40 +0200158 return node;
159}
160
Willy Tarreau21e5b0e2012-04-23 19:25:44 +0200161/* Executes a regex. It temporarily changes the data to add a trailing zero,
162 * and restores the previous character when leaving.
Willy Tarreauf3d25982007-05-08 22:45:09 +0200163 */
Willy Tarreau37406352012-04-23 16:16:37 +0200164int acl_match_reg(struct sample *smp, struct acl_pattern *pattern)
Willy Tarreauf3d25982007-05-08 22:45:09 +0200165{
Hiroaki Nakamura70351322013-01-13 15:00:42 +0900166 if (regex_exec(pattern->ptr.reg, smp->data.str.str, smp->data.str.len) == 0)
Thierry FOURNIERef37a662013-10-15 13:41:44 +0200167 return ACL_PAT_PASS;
168 return ACL_PAT_FAIL;
Willy Tarreauf3d25982007-05-08 22:45:09 +0200169}
170
Willy Tarreaua84d3742007-05-07 00:36:48 +0200171/* Checks that the pattern matches the beginning of the tested string. */
Willy Tarreau37406352012-04-23 16:16:37 +0200172int acl_match_beg(struct sample *smp, struct acl_pattern *pattern)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200173{
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200174 int icase;
175
Willy Tarreauf853c462012-04-23 18:53:56 +0200176 if (pattern->len > smp->data.str.len)
Willy Tarreau11382812008-07-09 16:18:21 +0200177 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200178
179 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
Willy Tarreauf853c462012-04-23 18:53:56 +0200180 if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str, pattern->len) != 0) ||
181 (!icase && strncmp(pattern->ptr.str, smp->data.str.str, pattern->len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +0200182 return ACL_PAT_FAIL;
183 return ACL_PAT_PASS;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200184}
185
186/* Checks that the pattern matches the end of the tested string. */
Willy Tarreau37406352012-04-23 16:16:37 +0200187int acl_match_end(struct sample *smp, struct acl_pattern *pattern)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200188{
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200189 int icase;
190
Willy Tarreauf853c462012-04-23 18:53:56 +0200191 if (pattern->len > smp->data.str.len)
Willy Tarreau11382812008-07-09 16:18:21 +0200192 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200193 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
Willy Tarreauf853c462012-04-23 18:53:56 +0200194 if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str + smp->data.str.len - pattern->len, pattern->len) != 0) ||
195 (!icase && strncmp(pattern->ptr.str, smp->data.str.str + smp->data.str.len - pattern->len, pattern->len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +0200196 return ACL_PAT_FAIL;
197 return ACL_PAT_PASS;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200198}
199
200/* Checks that the pattern is included inside the tested string.
201 * NB: Suboptimal, should be rewritten using a Boyer-Moore method.
202 */
Willy Tarreau37406352012-04-23 16:16:37 +0200203int acl_match_sub(struct sample *smp, struct acl_pattern *pattern)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200204{
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200205 int icase;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200206 char *end;
207 char *c;
208
Willy Tarreauf853c462012-04-23 18:53:56 +0200209 if (pattern->len > smp->data.str.len)
Willy Tarreau11382812008-07-09 16:18:21 +0200210 return ACL_PAT_FAIL;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200211
Willy Tarreauf853c462012-04-23 18:53:56 +0200212 end = smp->data.str.str + smp->data.str.len - pattern->len;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200213 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
214 if (icase) {
Willy Tarreauf853c462012-04-23 18:53:56 +0200215 for (c = smp->data.str.str; c <= end; c++) {
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200216 if (tolower(*c) != tolower(*pattern->ptr.str))
217 continue;
218 if (strncasecmp(pattern->ptr.str, c, pattern->len) == 0)
Willy Tarreau11382812008-07-09 16:18:21 +0200219 return ACL_PAT_PASS;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200220 }
221 } else {
Willy Tarreauf853c462012-04-23 18:53:56 +0200222 for (c = smp->data.str.str; c <= end; c++) {
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200223 if (*c != *pattern->ptr.str)
224 continue;
225 if (strncmp(pattern->ptr.str, c, pattern->len) == 0)
Willy Tarreau11382812008-07-09 16:18:21 +0200226 return ACL_PAT_PASS;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200227 }
Willy Tarreaua84d3742007-05-07 00:36:48 +0200228 }
Willy Tarreau11382812008-07-09 16:18:21 +0200229 return ACL_PAT_FAIL;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200230}
231
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200232/* Background: Fast way to find a zero byte in a word
233 * http://graphics.stanford.edu/~seander/bithacks.html#ZeroInWord
234 * hasZeroByte = (v - 0x01010101UL) & ~v & 0x80808080UL;
235 *
236 * To look for 4 different byte values, xor the word with those bytes and
237 * then check for zero bytes:
238 *
239 * v = (((unsigned char)c * 0x1010101U) ^ delimiter)
240 * where <delimiter> is the 4 byte values to look for (as an uint)
241 * and <c> is the character that is being tested
242 */
243static inline unsigned int is_delimiter(unsigned char c, unsigned int mask)
244{
245 mask ^= (c * 0x01010101); /* propagate the char to all 4 bytes */
246 return (mask - 0x01010101) & ~mask & 0x80808080U;
247}
248
249static inline unsigned int make_4delim(unsigned char d1, unsigned char d2, unsigned char d3, unsigned char d4)
250{
251 return d1 << 24 | d2 << 16 | d3 << 8 | d4;
252}
253
Willy Tarreaua84d3742007-05-07 00:36:48 +0200254/* This one is used by other real functions. It checks that the pattern is
255 * included inside the tested string, but enclosed between the specified
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200256 * delimiters or at the beginning or end of the string. The delimiters are
257 * provided as an unsigned int made by make_4delim() and match up to 4 different
258 * delimiters. Delimiters are stripped at the beginning and end of the pattern.
Willy Tarreaua84d3742007-05-07 00:36:48 +0200259 */
Willy Tarreau37406352012-04-23 16:16:37 +0200260static int match_word(struct sample *smp, struct acl_pattern *pattern, unsigned int delimiters)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200261{
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200262 int may_match, icase;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200263 char *c, *end;
264 char *ps;
265 int pl;
266
267 pl = pattern->len;
268 ps = pattern->ptr.str;
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200269
270 while (pl > 0 && is_delimiter(*ps, delimiters)) {
Willy Tarreaua84d3742007-05-07 00:36:48 +0200271 pl--;
272 ps++;
273 }
274
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200275 while (pl > 0 && is_delimiter(ps[pl - 1], delimiters))
Willy Tarreaua84d3742007-05-07 00:36:48 +0200276 pl--;
277
Willy Tarreauf853c462012-04-23 18:53:56 +0200278 if (pl > smp->data.str.len)
Willy Tarreau11382812008-07-09 16:18:21 +0200279 return ACL_PAT_FAIL;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200280
281 may_match = 1;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200282 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
Willy Tarreauf853c462012-04-23 18:53:56 +0200283 end = smp->data.str.str + smp->data.str.len - pl;
284 for (c = smp->data.str.str; c <= end; c++) {
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200285 if (is_delimiter(*c, delimiters)) {
Willy Tarreaua84d3742007-05-07 00:36:48 +0200286 may_match = 1;
287 continue;
288 }
Willy Tarreaua84d3742007-05-07 00:36:48 +0200289
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200290 if (!may_match)
291 continue;
292
293 if (icase) {
294 if ((tolower(*c) == tolower(*ps)) &&
295 (strncasecmp(ps, c, pl) == 0) &&
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200296 (c == end || is_delimiter(c[pl], delimiters)))
Willy Tarreau11382812008-07-09 16:18:21 +0200297 return ACL_PAT_PASS;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200298 } else {
299 if ((*c == *ps) &&
300 (strncmp(ps, c, pl) == 0) &&
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200301 (c == end || is_delimiter(c[pl], delimiters)))
Willy Tarreau11382812008-07-09 16:18:21 +0200302 return ACL_PAT_PASS;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200303 }
Willy Tarreaua84d3742007-05-07 00:36:48 +0200304 may_match = 0;
305 }
Willy Tarreau11382812008-07-09 16:18:21 +0200306 return ACL_PAT_FAIL;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200307}
308
309/* Checks that the pattern is included inside the tested string, but enclosed
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200310 * between the delimiters '?' or '/' or at the beginning or end of the string.
311 * Delimiters at the beginning or end of the pattern are ignored.
Willy Tarreaua84d3742007-05-07 00:36:48 +0200312 */
Willy Tarreau37406352012-04-23 16:16:37 +0200313int acl_match_dir(struct sample *smp, struct acl_pattern *pattern)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200314{
Willy Tarreau37406352012-04-23 16:16:37 +0200315 return match_word(smp, pattern, make_4delim('/', '?', '?', '?'));
Willy Tarreaua84d3742007-05-07 00:36:48 +0200316}
317
318/* Checks that the pattern is included inside the tested string, but enclosed
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200319 * between the delmiters '/', '?', '.' or ":" or at the beginning or end of
320 * the string. Delimiters at the beginning or end of the pattern are ignored.
Willy Tarreaua84d3742007-05-07 00:36:48 +0200321 */
Willy Tarreau37406352012-04-23 16:16:37 +0200322int acl_match_dom(struct sample *smp, struct acl_pattern *pattern)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200323{
Willy Tarreau37406352012-04-23 16:16:37 +0200324 return match_word(smp, pattern, make_4delim('/', '?', '.', ':'));
Willy Tarreaua84d3742007-05-07 00:36:48 +0200325}
326
327/* Checks that the integer in <test> is included between min and max */
Willy Tarreau37406352012-04-23 16:16:37 +0200328int acl_match_int(struct sample *smp, struct acl_pattern *pattern)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200329{
Willy Tarreauf853c462012-04-23 18:53:56 +0200330 if ((!pattern->val.range.min_set || pattern->val.range.min <= smp->data.uint) &&
331 (!pattern->val.range.max_set || smp->data.uint <= pattern->val.range.max))
Willy Tarreau11382812008-07-09 16:18:21 +0200332 return ACL_PAT_PASS;
333 return ACL_PAT_FAIL;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200334}
335
Willy Tarreau0e698542011-09-16 08:32:32 +0200336/* Checks that the length of the pattern in <test> is included between min and max */
Willy Tarreau37406352012-04-23 16:16:37 +0200337int acl_match_len(struct sample *smp, struct acl_pattern *pattern)
Willy Tarreau0e698542011-09-16 08:32:32 +0200338{
Willy Tarreauf853c462012-04-23 18:53:56 +0200339 if ((!pattern->val.range.min_set || pattern->val.range.min <= smp->data.str.len) &&
340 (!pattern->val.range.max_set || smp->data.str.len <= pattern->val.range.max))
Willy Tarreau0e698542011-09-16 08:32:32 +0200341 return ACL_PAT_PASS;
342 return ACL_PAT_FAIL;
343}
344
Willy Tarreau37406352012-04-23 16:16:37 +0200345int acl_match_ip(struct sample *smp, struct acl_pattern *pattern)
Willy Tarreaua67fad92007-05-08 19:50:09 +0200346{
Willy Tarreauceb4ac92012-04-28 00:41:46 +0200347 unsigned int v4; /* in network byte order */
348 struct in6_addr *v6;
349 int bits, pos;
350 struct in6_addr tmp6;
Willy Tarreaua67fad92007-05-08 19:50:09 +0200351
Willy Tarreauceb4ac92012-04-28 00:41:46 +0200352 if (pattern->type == SMP_T_IPV4) {
353 if (smp->type == SMP_T_IPV4) {
354 v4 = smp->data.ipv4.s_addr;
355 }
356 else if (smp->type == SMP_T_IPV6) {
357 /* v4 match on a V6 sample. We want to check at least for
358 * the following forms :
359 * - ::ffff:ip:v4 (ipv4 mapped)
360 * - ::0000:ip:v4 (old ipv4 mapped)
361 * - 2002:ip:v4:: (6to4)
362 */
363 if (*(uint32_t*)&smp->data.ipv6.s6_addr[0] == 0 &&
364 *(uint32_t*)&smp->data.ipv6.s6_addr[4] == 0 &&
365 (*(uint32_t*)&smp->data.ipv6.s6_addr[8] == 0 ||
366 *(uint32_t*)&smp->data.ipv6.s6_addr[8] == htonl(0xFFFF))) {
367 v4 = *(uint32_t*)&smp->data.ipv6.s6_addr[12];
368 }
369 else if (*(uint16_t*)&smp->data.ipv6.s6_addr[0] == htons(0x2002)) {
370 v4 = htonl((ntohs(*(uint16_t*)&smp->data.ipv6.s6_addr[2]) << 16) +
371 ntohs(*(uint16_t*)&smp->data.ipv6.s6_addr[4]));
372 }
373 else
374 return ACL_PAT_FAIL;
375 }
376 else
377 return ACL_PAT_FAIL;
Willy Tarreaua67fad92007-05-08 19:50:09 +0200378
Willy Tarreauceb4ac92012-04-28 00:41:46 +0200379 if (((v4 ^ pattern->val.ipv4.addr.s_addr) & pattern->val.ipv4.mask.s_addr) == 0)
380 return ACL_PAT_PASS;
381 else
382 return ACL_PAT_FAIL;
383 }
384 else if (pattern->type == SMP_T_IPV6) {
385 if (smp->type == SMP_T_IPV4) {
386 /* Convert the IPv4 sample address to IPv4 with the
387 * mapping method using the ::ffff: prefix.
388 */
389 memset(&tmp6, 0, 10);
390 *(uint16_t*)&tmp6.s6_addr[10] = htons(0xffff);
391 *(uint32_t*)&tmp6.s6_addr[12] = smp->data.ipv4.s_addr;
392 v6 = &tmp6;
393 }
394 else if (smp->type == SMP_T_IPV6) {
395 v6 = &smp->data.ipv6;
396 }
397 else {
398 return ACL_PAT_FAIL;
399 }
400
401 bits = pattern->val.ipv6.mask;
402 for (pos = 0; bits > 0; pos += 4, bits -= 32) {
403 v4 = *(uint32_t*)&v6->s6_addr[pos] ^ *(uint32_t*)&pattern->val.ipv6.addr.s6_addr[pos];
404 if (bits < 32)
Cyril Bonté4c01beb2012-10-23 21:28:31 +0200405 v4 &= htonl((~0U) << (32-bits));
Willy Tarreauceb4ac92012-04-28 00:41:46 +0200406 if (v4)
407 return ACL_PAT_FAIL;
408 }
Willy Tarreau11382812008-07-09 16:18:21 +0200409 return ACL_PAT_PASS;
Willy Tarreauceb4ac92012-04-28 00:41:46 +0200410 }
Willy Tarreau11382812008-07-09 16:18:21 +0200411 return ACL_PAT_FAIL;
Willy Tarreaua67fad92007-05-08 19:50:09 +0200412}
413
Willy Tarreaub337b532010-05-13 20:03:41 +0200414/* Lookup an IPv4 address in the expression's pattern tree using the longest
415 * match method. The node is returned if it exists, otherwise NULL.
416 */
Willy Tarreau37406352012-04-23 16:16:37 +0200417static void *acl_lookup_ip(struct sample *smp, struct acl_expr *expr)
Willy Tarreaub337b532010-05-13 20:03:41 +0200418{
419 struct in_addr *s;
420
Willy Tarreauf853c462012-04-23 18:53:56 +0200421 if (smp->type != SMP_T_IPV4)
Willy Tarreaub337b532010-05-13 20:03:41 +0200422 return ACL_PAT_FAIL;
423
Willy Tarreauf853c462012-04-23 18:53:56 +0200424 s = &smp->data.ipv4;
Willy Tarreaub337b532010-05-13 20:03:41 +0200425 return ebmb_lookup_longest(&expr->pattern_tree, &s->s_addr);
426}
427
Willy Tarreaua84d3742007-05-07 00:36:48 +0200428/* Parse a string. It is allocated and duplicated. */
Thierry FOURNIERdd69a042013-11-22 19:14:42 +0100429int acl_parse_str(const char **text, struct acl_pattern *pattern, struct sample_storage *smp, int *opaque, char **err)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200430{
431 int len;
432
Willy Tarreauae8b7962007-06-09 23:10:04 +0200433 len = strlen(*text);
Willy Tarreauc92ddbc2012-04-27 22:10:57 +0200434 pattern->type = SMP_T_CSTR;
Willy Tarreauc4262962010-05-10 23:42:40 +0200435
436 if (pattern->flags & ACL_PAT_F_TREE_OK) {
437 /* we're allowed to put the data in a tree whose root is pointed
438 * to by val.tree.
439 */
Thierry FOURNIERdd69a042013-11-22 19:14:42 +0100440 struct acl_idx_elt *node;
Willy Tarreauc4262962010-05-10 23:42:40 +0200441
442 node = calloc(1, sizeof(*node) + len + 1);
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200443 if (!node) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200444 memprintf(err, "out of memory while loading string pattern");
Willy Tarreauc4262962010-05-10 23:42:40 +0200445 return 0;
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200446 }
Thierry FOURNIERdd69a042013-11-22 19:14:42 +0100447 node->smp = smp;
448 memcpy(node->node.key, *text, len + 1);
449 if (ebst_insert(pattern->val.tree, &node->node) != &node->node)
Willy Tarreauc4262962010-05-10 23:42:40 +0200450 free(node); /* was a duplicate */
451 pattern->flags |= ACL_PAT_F_TREE; /* this pattern now contains a tree */
452 return 1;
453 }
454
Willy Tarreauae8b7962007-06-09 23:10:04 +0200455 pattern->ptr.str = strdup(*text);
Thierry FOURNIERdd69a042013-11-22 19:14:42 +0100456 pattern->smp = smp;
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200457 if (!pattern->ptr.str) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200458 memprintf(err, "out of memory while loading string pattern");
Willy Tarreaua84d3742007-05-07 00:36:48 +0200459 return 0;
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200460 }
Willy Tarreaua84d3742007-05-07 00:36:48 +0200461 pattern->len = len;
462 return 1;
463}
464
Emeric Brun07ca4962012-10-17 13:38:19 +0200465/* Parse a binary written in hexa. It is allocated. */
Thierry FOURNIERdd69a042013-11-22 19:14:42 +0100466int acl_parse_bin(const char **text, struct acl_pattern *pattern, struct sample_storage *smp, int *opaque, char **err)
Emeric Brun07ca4962012-10-17 13:38:19 +0200467{
468 int len;
469 const char *p = *text;
470 int i,j;
471
472 len = strlen(p);
473 if (len%2) {
474 memprintf(err, "an even number of hex digit is expected");
475 return 0;
476 }
477
478 pattern->type = SMP_T_CBIN;
479 pattern->len = len >> 1;
480 pattern->ptr.str = malloc(pattern->len);
Thierry FOURNIERdd69a042013-11-22 19:14:42 +0100481 pattern->smp = smp;
Emeric Brun07ca4962012-10-17 13:38:19 +0200482 if (!pattern->ptr.str) {
483 memprintf(err, "out of memory while loading string pattern");
484 return 0;
485 }
486
487 i = j = 0;
488 while (j < pattern->len) {
489 if (!ishex(p[i++]))
490 goto bad_input;
491 if (!ishex(p[i++]))
492 goto bad_input;
493 pattern->ptr.str[j++] = (hex2i(p[i-2]) << 4) + hex2i(p[i-1]);
494 }
495 return 1;
496
497bad_input:
498 memprintf(err, "an hex digit is expected (found '%c')", p[i-1]);
499 free(pattern->ptr.str);
500 return 0;
501}
502
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +0100503/* Parse and concatenate all further strings into one. */
504int
Thierry FOURNIERdd69a042013-11-22 19:14:42 +0100505acl_parse_strcat(const char **text, struct acl_pattern *pattern, struct sample_storage *smp, int *opaque, char **err)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +0100506{
507
508 int len = 0, i;
509 char *s;
510
511 for (i = 0; *text[i]; i++)
512 len += strlen(text[i])+1;
513
Willy Tarreauc92ddbc2012-04-27 22:10:57 +0200514 pattern->type = SMP_T_CSTR;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +0100515 pattern->ptr.str = s = calloc(1, len);
Thierry FOURNIERdd69a042013-11-22 19:14:42 +0100516 pattern->smp = smp;
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200517 if (!pattern->ptr.str) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200518 memprintf(err, "out of memory while loading pattern");
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +0100519 return 0;
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200520 }
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +0100521
522 for (i = 0; *text[i]; i++)
523 s += sprintf(s, i?" %s":"%s", text[i]);
524
525 pattern->len = len;
526
527 return i;
528}
529
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +0200530/* Free data allocated by acl_parse_reg */
Willy Tarreau37406352012-04-23 16:16:37 +0200531static void acl_free_reg(void *ptr)
532{
Hiroaki Nakamura70351322013-01-13 15:00:42 +0900533 regex_free(ptr);
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +0200534}
535
Willy Tarreauf3d25982007-05-08 22:45:09 +0200536/* Parse a regex. It is allocated. */
Thierry FOURNIERdd69a042013-11-22 19:14:42 +0100537int acl_parse_reg(const char **text, struct acl_pattern *pattern, struct sample_storage *smp, int *opaque, char **err)
Willy Tarreauf3d25982007-05-08 22:45:09 +0200538{
Hiroaki Nakamura70351322013-01-13 15:00:42 +0900539 regex *preg;
Willy Tarreauf3d25982007-05-08 22:45:09 +0200540
Hiroaki Nakamura70351322013-01-13 15:00:42 +0900541 preg = calloc(1, sizeof(*preg));
Willy Tarreauf3d25982007-05-08 22:45:09 +0200542
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200543 if (!preg) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200544 memprintf(err, "out of memory while loading pattern");
Willy Tarreauf3d25982007-05-08 22:45:09 +0200545 return 0;
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200546 }
Willy Tarreauf3d25982007-05-08 22:45:09 +0200547
Thierry FOURNIERed5a4ae2013-10-14 14:07:36 +0200548 if (!regex_comp(*text, preg, !(pattern->flags & ACL_PAT_F_IGNORE_CASE), 0, err)) {
Hiroaki Nakamura70351322013-01-13 15:00:42 +0900549 free(preg);
Hiroaki Nakamura70351322013-01-13 15:00:42 +0900550 return 0;
551 }
552
Willy Tarreauf3d25982007-05-08 22:45:09 +0200553 pattern->ptr.reg = preg;
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +0200554 pattern->freeptrbuf = &acl_free_reg;
Thierry FOURNIERdd69a042013-11-22 19:14:42 +0100555 pattern->smp = smp;
Willy Tarreauf3d25982007-05-08 22:45:09 +0200556 return 1;
557}
558
Willy Tarreauae8b7962007-06-09 23:10:04 +0200559/* Parse a range of positive integers delimited by either ':' or '-'. If only
560 * one integer is read, it is set as both min and max. An operator may be
561 * specified as the prefix, among this list of 5 :
562 *
563 * 0:eq, 1:gt, 2:ge, 3:lt, 4:le
564 *
565 * The default operator is "eq". It supports range matching. Ranges are
566 * rejected for other operators. The operator may be changed at any time.
567 * The operator is stored in the 'opaque' argument.
568 *
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200569 * If err is non-NULL, an error message will be returned there on errors and
570 * the caller will have to free it.
571 *
Willy Tarreaua84d3742007-05-07 00:36:48 +0200572 */
Thierry FOURNIERdd69a042013-11-22 19:14:42 +0100573int acl_parse_int(const char **text, struct acl_pattern *pattern, struct sample_storage *smp, int *opaque, char **err)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200574{
Willy Tarreauae8b7962007-06-09 23:10:04 +0200575 signed long long i;
576 unsigned int j, last, skip = 0;
577 const char *ptr = *text;
578
Willy Tarreauc92ddbc2012-04-27 22:10:57 +0200579 pattern->type = SMP_T_UINT;
Thierry FOURNIERdd69a042013-11-22 19:14:42 +0100580 pattern->smp = smp;
Willy Tarreau8f8e6452007-06-17 21:51:38 +0200581 while (!isdigit((unsigned char)*ptr)) {
Willy Tarreau1c7cc5b2010-07-18 10:46:33 +0200582 switch (get_std_op(ptr)) {
583 case STD_OP_EQ: *opaque = 0; break;
584 case STD_OP_GT: *opaque = 1; break;
585 case STD_OP_GE: *opaque = 2; break;
586 case STD_OP_LT: *opaque = 3; break;
587 case STD_OP_LE: *opaque = 4; break;
588 default:
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200589 memprintf(err, "'%s' is neither a number nor a supported operator", ptr);
Willy Tarreauae8b7962007-06-09 23:10:04 +0200590 return 0;
Willy Tarreau1c7cc5b2010-07-18 10:46:33 +0200591 }
Willy Tarreauae8b7962007-06-09 23:10:04 +0200592
593 skip++;
594 ptr = text[skip];
595 }
Willy Tarreaua84d3742007-05-07 00:36:48 +0200596
597 last = i = 0;
598 while (1) {
Willy Tarreauae8b7962007-06-09 23:10:04 +0200599 j = *ptr++;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200600 if ((j == '-' || j == ':') && !last) {
601 last++;
602 pattern->val.range.min = i;
603 i = 0;
604 continue;
605 }
606 j -= '0';
607 if (j > 9)
608 // also catches the terminating zero
609 break;
610 i *= 10;
611 i += j;
612 }
Willy Tarreauae8b7962007-06-09 23:10:04 +0200613
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200614 if (last && *opaque >= 1 && *opaque <= 4) {
Willy Tarreauae8b7962007-06-09 23:10:04 +0200615 /* having a range with a min or a max is absurd */
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200616 memprintf(err, "integer range '%s' specified with a comparison operator", text[skip]);
Willy Tarreauae8b7962007-06-09 23:10:04 +0200617 return 0;
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200618 }
Willy Tarreauae8b7962007-06-09 23:10:04 +0200619
Willy Tarreaua84d3742007-05-07 00:36:48 +0200620 if (!last)
621 pattern->val.range.min = i;
622 pattern->val.range.max = i;
Willy Tarreauae8b7962007-06-09 23:10:04 +0200623
624 switch (*opaque) {
625 case 0: /* eq */
626 pattern->val.range.min_set = 1;
627 pattern->val.range.max_set = 1;
628 break;
629 case 1: /* gt */
630 pattern->val.range.min++; /* gt = ge + 1 */
631 case 2: /* ge */
632 pattern->val.range.min_set = 1;
633 pattern->val.range.max_set = 0;
634 break;
635 case 3: /* lt */
636 pattern->val.range.max--; /* lt = le - 1 */
637 case 4: /* le */
638 pattern->val.range.min_set = 0;
639 pattern->val.range.max_set = 1;
640 break;
641 }
642 return skip + 1;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200643}
644
Willy Tarreau4a26d2f2008-07-15 16:05:33 +0200645/* Parse a range of positive 2-component versions delimited by either ':' or
646 * '-'. The version consists in a major and a minor, both of which must be
647 * smaller than 65536, because internally they will be represented as a 32-bit
648 * integer.
649 * If only one version is read, it is set as both min and max. Just like for
650 * pure integers, an operator may be specified as the prefix, among this list
651 * of 5 :
652 *
653 * 0:eq, 1:gt, 2:ge, 3:lt, 4:le
654 *
655 * The default operator is "eq". It supports range matching. Ranges are
656 * rejected for other operators. The operator may be changed at any time.
657 * The operator is stored in the 'opaque' argument. This allows constructs
658 * such as the following one :
659 *
660 * acl obsolete_ssl ssl_req_proto lt 3
661 * acl unsupported_ssl ssl_req_proto gt 3.1
662 * acl valid_ssl ssl_req_proto 3.0-3.1
663 *
664 */
Thierry FOURNIERdd69a042013-11-22 19:14:42 +0100665int acl_parse_dotted_ver(const char **text, struct acl_pattern *pattern, struct sample_storage *smp, int *opaque, char **err)
Willy Tarreau4a26d2f2008-07-15 16:05:33 +0200666{
667 signed long long i;
668 unsigned int j, last, skip = 0;
669 const char *ptr = *text;
670
671
672 while (!isdigit((unsigned char)*ptr)) {
Willy Tarreau1c7cc5b2010-07-18 10:46:33 +0200673 switch (get_std_op(ptr)) {
674 case STD_OP_EQ: *opaque = 0; break;
675 case STD_OP_GT: *opaque = 1; break;
676 case STD_OP_GE: *opaque = 2; break;
677 case STD_OP_LT: *opaque = 3; break;
678 case STD_OP_LE: *opaque = 4; break;
679 default:
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200680 memprintf(err, "'%s' is neither a number nor a supported operator", ptr);
Willy Tarreau4a26d2f2008-07-15 16:05:33 +0200681 return 0;
Willy Tarreau1c7cc5b2010-07-18 10:46:33 +0200682 }
Willy Tarreau4a26d2f2008-07-15 16:05:33 +0200683
684 skip++;
685 ptr = text[skip];
686 }
687
688 last = i = 0;
689 while (1) {
690 j = *ptr++;
691 if (j == '.') {
692 /* minor part */
693 if (i >= 65536)
694 return 0;
695 i <<= 16;
696 continue;
697 }
698 if ((j == '-' || j == ':') && !last) {
699 last++;
700 if (i < 65536)
701 i <<= 16;
702 pattern->val.range.min = i;
703 i = 0;
704 continue;
705 }
706 j -= '0';
707 if (j > 9)
708 // also catches the terminating zero
709 break;
710 i = (i & 0xFFFF0000) + (i & 0xFFFF) * 10;
711 i += j;
712 }
713
714 /* if we only got a major version, let's shift it now */
715 if (i < 65536)
716 i <<= 16;
717
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200718 if (last && *opaque >= 1 && *opaque <= 4) {
Willy Tarreau4a26d2f2008-07-15 16:05:33 +0200719 /* having a range with a min or a max is absurd */
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200720 memprintf(err, "version range '%s' specified with a comparison operator", text[skip]);
Willy Tarreau4a26d2f2008-07-15 16:05:33 +0200721 return 0;
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200722 }
Willy Tarreau4a26d2f2008-07-15 16:05:33 +0200723
Thierry FOURNIERdd69a042013-11-22 19:14:42 +0100724 pattern->smp = smp;
725
Willy Tarreau4a26d2f2008-07-15 16:05:33 +0200726 if (!last)
727 pattern->val.range.min = i;
728 pattern->val.range.max = i;
729
730 switch (*opaque) {
731 case 0: /* eq */
732 pattern->val.range.min_set = 1;
733 pattern->val.range.max_set = 1;
734 break;
735 case 1: /* gt */
736 pattern->val.range.min++; /* gt = ge + 1 */
737 case 2: /* ge */
738 pattern->val.range.min_set = 1;
739 pattern->val.range.max_set = 0;
740 break;
741 case 3: /* lt */
742 pattern->val.range.max--; /* lt = le - 1 */
743 case 4: /* le */
744 pattern->val.range.min_set = 0;
745 pattern->val.range.max_set = 1;
746 break;
747 }
748 return skip + 1;
749}
750
Willy Tarreaua67fad92007-05-08 19:50:09 +0200751/* Parse an IP address and an optional mask in the form addr[/mask].
752 * The addr may either be an IPv4 address or a hostname. The mask
753 * may either be a dotted mask or a number of bits. Returns 1 if OK,
Willy Tarreauc92ddbc2012-04-27 22:10:57 +0200754 * otherwise 0. NOTE: IP address patterns are typed (IPV4/IPV6).
Willy Tarreaua67fad92007-05-08 19:50:09 +0200755 */
Thierry FOURNIERdd69a042013-11-22 19:14:42 +0100756int acl_parse_ip(const char **text, struct acl_pattern *pattern, struct sample_storage *smp, int *opaque, char **err)
Willy Tarreaua67fad92007-05-08 19:50:09 +0200757{
Willy Tarreaub337b532010-05-13 20:03:41 +0200758 struct eb_root *tree = NULL;
759 if (pattern->flags & ACL_PAT_F_TREE_OK)
760 tree = pattern->val.tree;
761
762 if (str2net(*text, &pattern->val.ipv4.addr, &pattern->val.ipv4.mask)) {
763 unsigned int mask = ntohl(pattern->val.ipv4.mask.s_addr);
Thierry FOURNIERdd69a042013-11-22 19:14:42 +0100764 struct acl_idx_elt *node;
Willy Tarreaub337b532010-05-13 20:03:41 +0200765 /* check if the mask is contiguous so that we can insert the
766 * network into the tree. A continuous mask has only ones on
767 * the left. This means that this mask + its lower bit added
768 * once again is null.
769 */
Willy Tarreauceb4ac92012-04-28 00:41:46 +0200770 pattern->type = SMP_T_IPV4;
Willy Tarreaub337b532010-05-13 20:03:41 +0200771 if (mask + (mask & -mask) == 0 && tree) {
772 mask = mask ? 33 - flsnz(mask & -mask) : 0; /* equals cidr value */
773 /* FIXME: insert <addr>/<mask> into the tree here */
774 node = calloc(1, sizeof(*node) + 4); /* reserve 4 bytes for IPv4 address */
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200775 if (!node) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200776 memprintf(err, "out of memory while loading IPv4 pattern");
Willy Tarreaub337b532010-05-13 20:03:41 +0200777 return 0;
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200778 }
Thierry FOURNIERdd69a042013-11-22 19:14:42 +0100779 node->smp = smp;
780 memcpy(node->node.key, &pattern->val.ipv4.addr, 4); /* network byte order */
781 node->node.node.pfx = mask;
782 if (ebmb_insert_prefix(tree, &node->node, 4) != &node->node)
Willy Tarreaub337b532010-05-13 20:03:41 +0200783 free(node); /* was a duplicate */
784 pattern->flags |= ACL_PAT_F_TREE;
785 return 1;
786 }
Willy Tarreauae8b7962007-06-09 23:10:04 +0200787 return 1;
Willy Tarreaub337b532010-05-13 20:03:41 +0200788 }
Willy Tarreauceb4ac92012-04-28 00:41:46 +0200789 else if (str62net(*text, &pattern->val.ipv6.addr, &pattern->val.ipv6.mask)) {
790 /* no tree support right now */
791 pattern->type = SMP_T_IPV6;
792 return 1;
793 }
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200794 else {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200795 memprintf(err, "'%s' is not a valid IPv4 or IPv6 address", *text);
Willy Tarreauae8b7962007-06-09 23:10:04 +0200796 return 0;
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200797 }
Willy Tarreaua67fad92007-05-08 19:50:09 +0200798}
799
Willy Tarreaua84d3742007-05-07 00:36:48 +0200800/*
801 * Registers the ACL keyword list <kwl> as a list of valid keywords for next
802 * parsing sessions.
803 */
804void acl_register_keywords(struct acl_kw_list *kwl)
805{
806 LIST_ADDQ(&acl_keywords.list, &kwl->list);
807}
808
809/*
810 * Unregisters the ACL keyword list <kwl> from the list of valid keywords.
811 */
812void acl_unregister_keywords(struct acl_kw_list *kwl)
813{
814 LIST_DEL(&kwl->list);
815 LIST_INIT(&kwl->list);
816}
817
818/* Return a pointer to the ACL <name> within the list starting at <head>, or
819 * NULL if not found.
820 */
821struct acl *find_acl_by_name(const char *name, struct list *head)
822{
823 struct acl *acl;
824 list_for_each_entry(acl, head, list) {
825 if (strcmp(acl->name, name) == 0)
826 return acl;
827 }
828 return NULL;
829}
830
831/* Return a pointer to the ACL keyword <kw>, or NULL if not found. Note that if
832 * <kw> contains an opening parenthesis, only the left part of it is checked.
833 */
834struct acl_keyword *find_acl_kw(const char *kw)
835{
836 int index;
837 const char *kwend;
838 struct acl_kw_list *kwl;
839
840 kwend = strchr(kw, '(');
841 if (!kwend)
842 kwend = kw + strlen(kw);
843
844 list_for_each_entry(kwl, &acl_keywords.list, list) {
845 for (index = 0; kwl->kw[index].kw != NULL; index++) {
846 if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) &&
847 kwl->kw[index].kw[kwend-kw] == 0)
848 return &kwl->kw[index];
849 }
850 }
851 return NULL;
852}
853
Willy Tarreaudfd7fca2011-03-09 07:27:02 +0100854/* NB: does nothing if <pat> is NULL */
Willy Tarreaua84d3742007-05-07 00:36:48 +0200855static void free_pattern(struct acl_pattern *pat)
856{
Willy Tarreaudfd7fca2011-03-09 07:27:02 +0100857 if (!pat)
858 return;
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +0200859
860 if (pat->ptr.ptr) {
861 if (pat->freeptrbuf)
862 pat->freeptrbuf(pat->ptr.ptr);
863
Willy Tarreaua84d3742007-05-07 00:36:48 +0200864 free(pat->ptr.ptr);
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +0200865 }
866
Willy Tarreaua84d3742007-05-07 00:36:48 +0200867 free(pat);
868}
869
870static void free_pattern_list(struct list *head)
871{
872 struct acl_pattern *pat, *tmp;
873 list_for_each_entry_safe(pat, tmp, head, list)
874 free_pattern(pat);
875}
876
Willy Tarreaue56cda92010-05-11 23:25:05 +0200877static void free_pattern_tree(struct eb_root *root)
878{
879 struct eb_node *node, *next;
880 node = eb_first(root);
881 while (node) {
882 next = eb_next(node);
Willy Tarreau60eccc12013-11-14 16:00:12 +0100883 eb_delete(node);
Willy Tarreaue56cda92010-05-11 23:25:05 +0200884 free(node);
885 node = next;
886 }
887}
888
Willy Tarreaua84d3742007-05-07 00:36:48 +0200889static struct acl_expr *prune_acl_expr(struct acl_expr *expr)
890{
Willy Tarreau34db1082012-04-19 17:16:54 +0200891 struct arg *arg;
892
Willy Tarreaua84d3742007-05-07 00:36:48 +0200893 free_pattern_list(&expr->patterns);
Willy Tarreaue56cda92010-05-11 23:25:05 +0200894 free_pattern_tree(&expr->pattern_tree);
Willy Tarreaua84d3742007-05-07 00:36:48 +0200895 LIST_INIT(&expr->patterns);
Willy Tarreau34db1082012-04-19 17:16:54 +0200896
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100897 for (arg = expr->smp->arg_p; arg; arg++) {
Willy Tarreau34db1082012-04-19 17:16:54 +0200898 if (arg->type == ARGT_STOP)
899 break;
Willy Tarreau496aa012012-06-01 10:38:29 +0200900 if (arg->type == ARGT_STR || arg->unresolved) {
Willy Tarreau34db1082012-04-19 17:16:54 +0200901 free(arg->data.str.str);
902 arg->data.str.str = NULL;
Willy Tarreau496aa012012-06-01 10:38:29 +0200903 arg->unresolved = 0;
Willy Tarreau34db1082012-04-19 17:16:54 +0200904 }
Willy Tarreau34db1082012-04-19 17:16:54 +0200905 }
906
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100907 if (expr->smp->arg_p != empty_arg_list)
908 free(expr->smp->arg_p);
Willy Tarreaua84d3742007-05-07 00:36:48 +0200909 return expr;
910}
911
Thierry FOURNIER3a103c52013-11-22 17:33:27 +0100912/* return 1 if the process is ok
913 * return -1 if the parser fail. The err message is filled.
914 * return -2 if out of memory
915 */
916int acl_register_pattern(struct acl_expr *expr, char *text,
Thierry FOURNIERdd69a042013-11-22 19:14:42 +0100917 struct sample_storage *smp,
Thierry FOURNIER3a103c52013-11-22 17:33:27 +0100918 struct acl_pattern **pattern,
919 int patflags, char **err)
920{
921 const char *args[2];
922 int opaque = 0;
923
924 args[0] = text;
925 args[1] = "";
926
927 /* we keep the previous pattern along iterations as long as it's not used */
928 if (!*pattern)
929 *pattern = (struct acl_pattern *)malloc(sizeof(**pattern));
930 if (!*pattern)
931 return -1;
932
933 memset(*pattern, 0, sizeof(**pattern));
934 (*pattern)->flags = patflags;
935
936 if (!((*pattern)->flags & ACL_PAT_F_IGNORE_CASE) &&
937 (expr->match == acl_match_str || expr->match == acl_match_ip)) {
938 /* we pre-set the data pointer to the tree's head so that functions
939 * which are able to insert in a tree know where to do that.
940 */
941 (*pattern)->flags |= ACL_PAT_F_TREE_OK;
942 (*pattern)->val.tree = &expr->pattern_tree;
943 }
944
945 (*pattern)->type = SMP_TYPES; /* unspecified type by default */
Thierry FOURNIERdd69a042013-11-22 19:14:42 +0100946 if (!expr->parse(args, *pattern, smp, &opaque, err))
Thierry FOURNIER3a103c52013-11-22 17:33:27 +0100947 return -1;
948
949 /* if the parser did not feed the tree, let's chain the pattern to the list */
950 if (!((*pattern)->flags & ACL_PAT_F_TREE)) {
951 LIST_ADDQ(&expr->patterns, &(*pattern)->list);
952 *pattern = NULL; /* get a new one */
953 }
954
955 return 1;
956}
Willy Tarreau08ad0b32012-04-27 17:25:24 +0200957
958/* Reads patterns from a file. If <err_msg> is non-NULL, an error message will
959 * be returned there on errors and the caller will have to free it.
960 */
Willy Tarreaud76a98a2013-03-31 18:34:33 +0200961static int acl_read_patterns_from_file(struct acl_expr *expr,
962 const char *filename, int patflags,
963 char **err)
Willy Tarreau2b5285d2010-05-09 23:45:24 +0200964{
965 FILE *file;
966 char *c;
Thierry FOURNIER3a103c52013-11-22 17:33:27 +0100967 char *arg;
Willy Tarreau2b5285d2010-05-09 23:45:24 +0200968 struct acl_pattern *pattern;
Willy Tarreau6a8097f2011-02-26 15:14:15 +0100969 int ret = 0;
Willy Tarreau08ad0b32012-04-27 17:25:24 +0200970 int line = 0;
Thierry FOURNIER3a103c52013-11-22 17:33:27 +0100971 int code;
Willy Tarreau2b5285d2010-05-09 23:45:24 +0200972
973 file = fopen(filename, "r");
Willy Tarreau08ad0b32012-04-27 17:25:24 +0200974 if (!file) {
975 memprintf(err, "failed to open pattern file <%s>", filename);
Willy Tarreau2b5285d2010-05-09 23:45:24 +0200976 return 0;
Willy Tarreau08ad0b32012-04-27 17:25:24 +0200977 }
Willy Tarreau2b5285d2010-05-09 23:45:24 +0200978
979 /* now parse all patterns. The file may contain only one pattern per
980 * line. If the line contains spaces, they will be part of the pattern.
981 * The pattern stops at the first CR, LF or EOF encountered.
982 */
Willy Tarreaue56cda92010-05-11 23:25:05 +0200983 pattern = NULL;
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100984 while (fgets(trash.str, trash.size, file) != NULL) {
Willy Tarreau08ad0b32012-04-27 17:25:24 +0200985 line++;
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100986 c = trash.str;
Willy Tarreau58215a02010-05-13 22:07:43 +0200987
988 /* ignore lines beginning with a dash */
989 if (*c == '#')
990 continue;
991
992 /* strip leading spaces and tabs */
993 while (*c == ' ' || *c == '\t')
994 c++;
995
Willy Tarreau58215a02010-05-13 22:07:43 +0200996
Thierry FOURNIER3a103c52013-11-22 17:33:27 +0100997 arg = c;
Willy Tarreau2b5285d2010-05-09 23:45:24 +0200998 while (*c && *c != '\n' && *c != '\r')
999 c++;
1000 *c = 0;
1001
Willy Tarreau51091962011-01-03 21:04:10 +01001002 /* empty lines are ignored too */
Thierry FOURNIER3a103c52013-11-22 17:33:27 +01001003 if (c == arg)
Willy Tarreau51091962011-01-03 21:04:10 +01001004 continue;
1005
Thierry FOURNIERdd69a042013-11-22 19:14:42 +01001006 code = acl_register_pattern(expr, arg, NULL, &pattern, patflags, err);
Thierry FOURNIER3a103c52013-11-22 17:33:27 +01001007 if (code == -2) {
Willy Tarreau08ad0b32012-04-27 17:25:24 +02001008 memprintf(err, "out of memory when loading patterns from file <%s>", filename);
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001009 goto out_close;
Willy Tarreau08ad0b32012-04-27 17:25:24 +02001010 }
Thierry FOURNIER3a103c52013-11-22 17:33:27 +01001011 else if (code < 0) {
1012 memprintf(err, "%s when loading patterns from file <%s>", *err, filename);
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001013 goto out_free_pattern;
Willy Tarreaue56cda92010-05-11 23:25:05 +02001014 }
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001015 }
Willy Tarreau6a8097f2011-02-26 15:14:15 +01001016
1017 ret = 1; /* success */
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001018
1019 out_free_pattern:
1020 free_pattern(pattern);
1021 out_close:
1022 fclose(file);
Willy Tarreau6a8097f2011-02-26 15:14:15 +01001023 return ret;
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001024}
1025
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001026/* Parse an ACL expression starting at <args>[0], and return it. If <err> is
1027 * not NULL, it will be filled with a pointer to an error message in case of
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001028 * error. This pointer must be freeable or NULL. <al> is an arg_list serving
1029 * as a list head to report missing dependencies.
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001030 *
Willy Tarreaua84d3742007-05-07 00:36:48 +02001031 * Right now, the only accepted syntax is :
1032 * <subject> [<value>...]
1033 */
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001034struct acl_expr *parse_acl_expr(const char **args, char **err, struct arg_list *al)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001035{
1036 __label__ out_return, out_free_expr, out_free_pattern;
1037 struct acl_expr *expr;
1038 struct acl_keyword *aclkw;
1039 struct acl_pattern *pattern;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02001040 int opaque, patflags;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001041 const char *arg;
Thierry FOURNIER348971e2013-11-21 10:50:10 +01001042 struct sample_expr *smp = NULL;
1043 const char *p;
1044 int idx = 0;
1045 char *ckw = NULL;
1046 const char *begw;
1047 const char *endw;
1048 unsigned long prev_type;
1049 int cur_type;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001050
Willy Tarreaubef91e72013-03-31 23:14:46 +02001051 /* First, we lookd for an ACL keyword. And if we don't find one, then
1052 * we look for a sample fetch keyword.
1053 */
Willy Tarreaua84d3742007-05-07 00:36:48 +02001054 aclkw = find_acl_kw(args[0]);
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001055 if (!aclkw || !aclkw->parse) {
Willy Tarreaubef91e72013-03-31 23:14:46 +02001056
Thierry FOURNIER348971e2013-11-21 10:50:10 +01001057 smp = sample_parse_expr((char **)args, &idx, trash.str, trash.size, al);
Willy Tarreaubef91e72013-03-31 23:14:46 +02001058
1059 if (!smp) {
Thierry FOURNIER348971e2013-11-21 10:50:10 +01001060 memprintf(err, "unknown ACL or sample keyword '%s': %s", *args, trash.str);
Willy Tarreaubef91e72013-03-31 23:14:46 +02001061 goto out_return;
1062 }
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001063 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001064
1065 expr = (struct acl_expr *)calloc(1, sizeof(*expr));
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001066 if (!expr) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001067 memprintf(err, "out of memory when parsing ACL expression");
Willy Tarreaua84d3742007-05-07 00:36:48 +02001068 goto out_return;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001069 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001070
Thierry FOURNIER348971e2013-11-21 10:50:10 +01001071 expr->kw = aclkw ? aclkw->kw : smp->fetch->kw;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001072 LIST_INIT(&expr->patterns);
Willy Tarreaue56cda92010-05-11 23:25:05 +02001073 expr->pattern_tree = EB_ROOT_UNIQUE;
Willy Tarreaubef91e72013-03-31 23:14:46 +02001074 expr->parse = aclkw ? aclkw->parse : NULL;
1075 expr->match = aclkw ? aclkw->match : NULL;
Thierry FOURNIER348971e2013-11-21 10:50:10 +01001076 expr->smp = aclkw ? NULL : smp;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001077
Willy Tarreau9987ea92013-06-11 21:09:06 +02001078 if (!expr->parse) {
1079 /* some types can be automatically converted */
1080
Thierry FOURNIER348971e2013-11-21 10:50:10 +01001081 switch (expr->smp ? expr->smp->fetch->out_type : aclkw->smp->out_type) {
Willy Tarreau9987ea92013-06-11 21:09:06 +02001082 case SMP_T_BOOL:
1083 expr->parse = acl_parse_fcts[ACL_MATCH_BOOL];
1084 expr->match = acl_match_fcts[ACL_MATCH_BOOL];
1085 break;
1086 case SMP_T_SINT:
1087 case SMP_T_UINT:
1088 expr->parse = acl_parse_fcts[ACL_MATCH_INT];
1089 expr->match = acl_match_fcts[ACL_MATCH_INT];
1090 break;
1091 case SMP_T_IPV4:
1092 case SMP_T_IPV6:
1093 expr->parse = acl_parse_fcts[ACL_MATCH_IP];
1094 expr->match = acl_match_fcts[ACL_MATCH_IP];
1095 break;
1096 }
1097 }
1098
Thierry FOURNIER348971e2013-11-21 10:50:10 +01001099 /* now parse the rest of acl only if "find_acl_kw" match */
1100 if (aclkw) {
Willy Tarreau34db1082012-04-19 17:16:54 +02001101
Thierry FOURNIER348971e2013-11-21 10:50:10 +01001102 /* build new sample expression */
1103 expr->smp = calloc(1, sizeof(struct sample_expr));
1104 if (!expr->smp) {
1105 memprintf(err, "out of memory when parsing ACL expression");
1106 goto out_return;
1107 }
1108 LIST_INIT(&(expr->smp->conv_exprs));
1109 expr->smp->fetch = aclkw->smp;
1110 expr->smp->arg_p = empty_arg_list;
Willy Tarreau34db1082012-04-19 17:16:54 +02001111
Thierry FOURNIER348971e2013-11-21 10:50:10 +01001112 /* look for the begining of the subject arguments */
1113 p = strchr(args[0], ',');
1114 arg = strchr(args[0], '(');
1115 if (p && arg && p < arg)
1116 arg = NULL;
1117
1118 if (expr->smp->fetch->arg_mask) {
1119 int nbargs = 0;
1120 char *end;
1121
1122 if (arg != NULL) {
1123 /* there are 0 or more arguments in the form "subject(arg[,arg]*)" */
1124 arg++;
1125 end = strchr(arg, ')');
1126 if (!end) {
1127 memprintf(err, "missing closing ')' after arguments to ACL keyword '%s'", expr->kw);
1128 goto out_free_expr;
1129 }
1130
1131 /* Parse the arguments. Note that currently we have no way to
1132 * report parsing errors, hence the NULL in the error pointers.
1133 * An error is also reported if some mandatory arguments are
1134 * missing. We prepare the args list to report unresolved
1135 * dependencies.
1136 */
1137 al->ctx = ARGC_ACL;
1138 al->kw = expr->kw;
1139 al->conv = NULL;
1140 nbargs = make_arg_list(arg, end - arg, expr->smp->fetch->arg_mask, &expr->smp->arg_p,
1141 err, NULL, NULL, al);
1142 if (nbargs < 0) {
1143 /* note that make_arg_list will have set <err> here */
1144 memprintf(err, "in argument to '%s', %s", expr->kw, *err);
1145 goto out_free_expr;
1146 }
1147
1148 if (!expr->smp->arg_p)
1149 expr->smp->arg_p = empty_arg_list;
1150
1151 if (expr->smp->fetch->val_args && !expr->smp->fetch->val_args(expr->smp->arg_p, err)) {
1152 /* invalid keyword argument, error must have been
1153 * set by val_args().
1154 */
1155 memprintf(err, "in argument to '%s', %s", expr->kw, *err);
1156 goto out_free_expr;
1157 }
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001158 }
Thierry FOURNIER348971e2013-11-21 10:50:10 +01001159 else if (ARGM(expr->smp->fetch->arg_mask) == 1) {
1160 int type = (expr->smp->fetch->arg_mask >> 4) & 15;
Willy Tarreauae52f062012-04-26 12:13:35 +02001161
Thierry FOURNIER348971e2013-11-21 10:50:10 +01001162 /* If a proxy is noted as a mandatory argument, we'll fake
1163 * an empty one so that acl_find_targets() resolves it as
1164 * the current one later.
1165 */
1166 if (type != ARGT_FE && type != ARGT_BE && type != ARGT_TAB) {
1167 memprintf(err, "ACL keyword '%s' expects %d arguments", expr->kw, ARGM(expr->smp->fetch->arg_mask));
1168 goto out_free_expr;
1169 }
Willy Tarreau2e845be2012-10-19 19:49:09 +02001170
Thierry FOURNIER348971e2013-11-21 10:50:10 +01001171 /* Build an arg list containing the type as an empty string
1172 * and the usual STOP.
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001173 */
Thierry FOURNIER348971e2013-11-21 10:50:10 +01001174 expr->smp->arg_p = calloc(2, sizeof(*expr->smp->arg_p));
1175 expr->smp->arg_p[0].type = type;
1176 expr->smp->arg_p[0].unresolved = 1;
1177 expr->smp->arg_p[0].data.str.str = strdup("");
1178 expr->smp->arg_p[0].data.str.size = 1;
1179 expr->smp->arg_p[0].data.str.len = 0;
1180
1181 al->ctx = ARGC_ACL;
1182 al->kw = expr->kw;
1183 al->conv = NULL;
1184 arg_list_add(al, &expr->smp->arg_p[0], 0);
1185
1186 expr->smp->arg_p[1].type = ARGT_STOP;
1187 }
1188 else if (ARGM(expr->smp->fetch->arg_mask)) {
1189 /* there were some mandatory arguments */
1190 memprintf(err, "ACL keyword '%s' expects %d arguments", expr->kw, ARGM(expr->smp->fetch->arg_mask));
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001191 goto out_free_expr;
1192 }
Willy Tarreau61612d42012-04-19 18:42:05 +02001193 }
Thierry FOURNIER348971e2013-11-21 10:50:10 +01001194 else {
1195 if (arg ) {
1196 /* no argument expected */
1197 memprintf(err, "ACL keyword '%s' takes no argument", expr->kw);
Willy Tarreaufc2c1fd2012-04-19 23:35:54 +02001198 goto out_free_expr;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001199 }
Thierry FOURNIER348971e2013-11-21 10:50:10 +01001200 }
Willy Tarreau9ca69362013-10-22 19:10:06 +02001201
Thierry FOURNIER348971e2013-11-21 10:50:10 +01001202 /* Now process the converters if any. We have two supported syntaxes
1203 * for the converters, which can be combined :
1204 * - comma-delimited list of converters just after the keyword and args ;
1205 * - one converter per keyword
1206 * The combination allows to have each keyword being a comma-delimited
1207 * series of converters.
1208 *
1209 * We want to process the former first, then the latter. For this we start
1210 * from the beginning of the supposed place in the exiting conv chain, which
1211 * starts at the last comma (endt).
1212 */
Willy Tarreauf75d0082013-04-07 21:20:44 +02001213
Thierry FOURNIER348971e2013-11-21 10:50:10 +01001214 /* look for the begining of the converters list */
1215 arg = strchr(args[0], ',');
Willy Tarreau61612d42012-04-19 18:42:05 +02001216 if (arg) {
Thierry FOURNIER348971e2013-11-21 10:50:10 +01001217 prev_type = expr->smp->fetch->out_type;
1218 while (1) {
1219 struct sample_conv *conv;
1220 struct sample_conv_expr *conv_expr;
1221
1222 if (*arg == ')') /* skip last closing parenthesis */
1223 arg++;
1224
1225 if (*arg && *arg != ',') {
1226 if (ckw)
1227 memprintf(err, "ACL keyword '%s' : missing comma after conv keyword '%s'.",
1228 expr->kw, ckw);
1229 else
1230 memprintf(err, "ACL keyword '%s' : missing comma after fetch keyword.",
1231 expr->kw);
1232 goto out_free_expr;
1233 }
1234
1235 while (*arg == ',') /* then trailing commas */
1236 arg++;
1237
1238 begw = arg; /* start of conv keyword */
1239
1240 if (!*begw)
1241 /* none ? end of converters */
1242 break;
1243
1244 for (endw = begw; *endw && *endw != '(' && *endw != ','; endw++);
1245
1246 free(ckw);
1247 ckw = my_strndup(begw, endw - begw);
1248
1249 conv = find_sample_conv(begw, endw - begw);
1250 if (!conv) {
1251 /* Unknown converter method */
1252 memprintf(err, "ACL keyword '%s' : unknown conv method '%s'.",
1253 expr->kw, ckw);
1254 goto out_free_expr;
1255 }
1256
1257 arg = endw;
1258 if (*arg == '(') {
1259 /* look for the end of this term */
1260 while (*arg && *arg != ')')
1261 arg++;
1262 if (*arg != ')') {
1263 memprintf(err, "ACL keyword '%s' : syntax error: missing ')' after conv keyword '%s'.",
1264 expr->kw, ckw);
1265 goto out_free_expr;
1266 }
1267 }
1268
1269 if (conv->in_type >= SMP_TYPES || conv->out_type >= SMP_TYPES) {
1270 memprintf(err, "ACL keyword '%s' : returns type of conv method '%s' is unknown.",
1271 expr->kw, ckw);
1272 goto out_free_expr;
1273 }
1274
1275 /* If impossible type conversion */
1276 if (!sample_casts[prev_type][conv->in_type]) {
1277 memprintf(err, "ACL keyword '%s' : conv method '%s' cannot be applied.",
1278 expr->kw, ckw);
1279 goto out_free_expr;
1280 }
1281
1282 prev_type = conv->out_type;
1283 conv_expr = calloc(1, sizeof(struct sample_conv_expr));
1284 if (!conv_expr)
1285 goto out_free_expr;
1286
1287 LIST_ADDQ(&(expr->smp->conv_exprs), &(conv_expr->list));
1288 conv_expr->conv = conv;
1289
1290 if (arg != endw) {
1291 char *err_msg = NULL;
1292 int err_arg;
1293
1294 if (!conv->arg_mask) {
1295 memprintf(err, "ACL keyword '%s' : conv method '%s' does not support any args.",
1296 expr->kw, ckw);
1297 goto out_free_expr;
1298 }
1299
1300 al->kw = expr->smp->fetch->kw;
1301 al->conv = conv_expr->conv->kw;
1302 if (make_arg_list(endw + 1, arg - endw - 1, conv->arg_mask, &conv_expr->arg_p, &err_msg, NULL, &err_arg, al) < 0) {
1303 memprintf(err, "ACL keyword '%s' : invalid arg %d in conv method '%s' : %s.",
1304 expr->kw, err_arg+1, ckw, err_msg);
1305 free(err_msg);
1306 goto out_free_expr;
1307 }
1308
1309 if (!conv_expr->arg_p)
1310 conv_expr->arg_p = empty_arg_list;
1311
Thierry FOURNIER9c1d67e2013-11-21 13:37:41 +01001312 if (conv->val_args && !conv->val_args(conv_expr->arg_p, conv, &err_msg)) {
Thierry FOURNIER348971e2013-11-21 10:50:10 +01001313 memprintf(err, "ACL keyword '%s' : invalid args in conv method '%s' : %s.",
1314 expr->kw, ckw, err_msg);
1315 free(err_msg);
1316 goto out_free_expr;
1317 }
1318 }
1319 else if (ARGM(conv->arg_mask)) {
1320 memprintf(err, "ACL keyword '%s' : missing args for conv method '%s'.",
1321 expr->kw, ckw);
1322 goto out_free_expr;
1323 }
1324 }
Willy Tarreau61612d42012-04-19 18:42:05 +02001325 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001326 }
1327
Willy Tarreau3c3dfd52013-11-04 18:09:12 +01001328 /* Additional check to protect against common mistakes */
Thierry FOURNIER348971e2013-11-21 10:50:10 +01001329 cur_type = smp_expr_output_type(expr->smp);
1330 if (expr->parse && cur_type != SMP_T_BOOL && !*args[1]) {
Willy Tarreau3c3dfd52013-11-04 18:09:12 +01001331 Warning("parsing acl keyword '%s' :\n"
1332 " no pattern to match against were provided, so this ACL will never match.\n"
1333 " If this is what you intended, please add '--' to get rid of this warning.\n"
1334 " If you intended to match only for existence, please use '-m found'.\n"
1335 " If you wanted to force an int to match as a bool, please use '-m bool'.\n"
1336 "\n",
1337 args[0]);
1338 }
1339
Willy Tarreaua84d3742007-05-07 00:36:48 +02001340 args++;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02001341
1342 /* check for options before patterns. Supported options are :
1343 * -i : ignore case for all patterns by default
1344 * -f : read patterns from those files
Willy Tarreau5adeda12013-03-31 22:13:34 +02001345 * -m : force matching method (must be used before -f)
Willy Tarreauc8d7c962007-06-17 08:20:33 +02001346 * -- : everything after this is not an option
1347 */
1348 patflags = 0;
1349 while (**args == '-') {
1350 if ((*args)[1] == 'i')
1351 patflags |= ACL_PAT_F_IGNORE_CASE;
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001352 else if ((*args)[1] == 'f') {
Willy Tarreaubef91e72013-03-31 23:14:46 +02001353 if (!expr->parse) {
Willy Tarreau9987ea92013-06-11 21:09:06 +02001354 memprintf(err, "matching method must be specified first (using '-m') when using a sample fetch of this type ('%s')", expr->kw);
Willy Tarreaubef91e72013-03-31 23:14:46 +02001355 goto out_free_expr;
1356 }
1357
Willy Tarreaud76a98a2013-03-31 18:34:33 +02001358 if (!acl_read_patterns_from_file(expr, args[1], patflags | ACL_PAT_F_FROM_FILE, err))
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001359 goto out_free_expr;
Willy Tarreau5adeda12013-03-31 22:13:34 +02001360 args++;
1361 }
1362 else if ((*args)[1] == 'm') {
1363 int idx;
1364
1365 if (!LIST_ISEMPTY(&expr->patterns) || !eb_is_empty(&expr->pattern_tree)) {
1366 memprintf(err, "'-m' must only be specified before patterns and files in parsing ACL expression");
1367 goto out_free_expr;
1368 }
1369
1370 idx = acl_find_match_name(args[1]);
1371 if (idx < 0) {
1372 memprintf(err, "unknown matching method '%s' when parsing ACL expression", args[1]);
1373 goto out_free_expr;
1374 }
1375
1376 /* Note: -m found is always valid, bool/int are compatible, str/bin/reg/len are compatible */
1377 if (idx == ACL_MATCH_FOUND || /* -m found */
1378 ((idx == ACL_MATCH_BOOL || idx == ACL_MATCH_INT) && /* -m bool/int */
Thierry FOURNIER348971e2013-11-21 10:50:10 +01001379 (cur_type == SMP_T_BOOL ||
1380 cur_type == SMP_T_UINT ||
1381 cur_type == SMP_T_SINT)) ||
Willy Tarreau5adeda12013-03-31 22:13:34 +02001382 (idx == ACL_MATCH_IP && /* -m ip */
Thierry FOURNIER348971e2013-11-21 10:50:10 +01001383 (cur_type == SMP_T_IPV4 ||
1384 cur_type == SMP_T_IPV6)) ||
Willy Tarreau5adeda12013-03-31 22:13:34 +02001385 ((idx == ACL_MATCH_BIN || idx == ACL_MATCH_LEN || idx == ACL_MATCH_STR ||
1386 idx == ACL_MATCH_BEG || idx == ACL_MATCH_SUB || idx == ACL_MATCH_DIR ||
1387 idx == ACL_MATCH_DOM || idx == ACL_MATCH_END || idx == ACL_MATCH_REG) && /* strings */
Thierry FOURNIER348971e2013-11-21 10:50:10 +01001388 (cur_type == SMP_T_STR ||
1389 cur_type == SMP_T_BIN ||
1390 cur_type == SMP_T_CSTR ||
1391 cur_type == SMP_T_CBIN))) {
Willy Tarreau5adeda12013-03-31 22:13:34 +02001392 expr->parse = acl_parse_fcts[idx];
1393 expr->match = acl_match_fcts[idx];
1394 }
1395 else {
Willy Tarreau93fddf12013-03-31 22:59:32 +02001396 memprintf(err, "matching method '%s' cannot be used with fetch keyword '%s'", args[1], expr->kw);
Willy Tarreau5adeda12013-03-31 22:13:34 +02001397 goto out_free_expr;
1398 }
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001399 args++;
1400 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +02001401 else if ((*args)[1] == '-') {
1402 args++;
1403 break;
1404 }
1405 else
1406 break;
1407 args++;
1408 }
1409
Willy Tarreaubef91e72013-03-31 23:14:46 +02001410 if (!expr->parse) {
Willy Tarreau9987ea92013-06-11 21:09:06 +02001411 memprintf(err, "matching method must be specified first (using '-m') when using a sample fetch of this type ('%s')", expr->kw);
Willy Tarreaubef91e72013-03-31 23:14:46 +02001412 goto out_free_expr;
1413 }
1414
Willy Tarreauc8d7c962007-06-17 08:20:33 +02001415 /* now parse all patterns */
Willy Tarreauae8b7962007-06-09 23:10:04 +02001416 opaque = 0;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001417 while (**args) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02001418 int ret;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001419 pattern = (struct acl_pattern *)calloc(1, sizeof(*pattern));
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001420 if (!pattern) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001421 memprintf(err, "out of memory when parsing ACL pattern");
Willy Tarreaua84d3742007-05-07 00:36:48 +02001422 goto out_free_expr;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001423 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +02001424 pattern->flags = patflags;
1425
Willy Tarreauc92ddbc2012-04-27 22:10:57 +02001426 pattern->type = SMP_TYPES; /* unspecified type */
Thierry FOURNIERdd69a042013-11-22 19:14:42 +01001427 ret = expr->parse(args, pattern, NULL, &opaque, err);
Willy Tarreau7dcb6482012-04-27 17:52:25 +02001428 if (!ret)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001429 goto out_free_pattern;
Willy Tarreau7dcb6482012-04-27 17:52:25 +02001430
Willy Tarreaua84d3742007-05-07 00:36:48 +02001431 LIST_ADDQ(&expr->patterns, &pattern->list);
Willy Tarreauae8b7962007-06-09 23:10:04 +02001432 args += ret;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001433 }
1434
1435 return expr;
1436
1437 out_free_pattern:
1438 free_pattern(pattern);
1439 out_free_expr:
1440 prune_acl_expr(expr);
1441 free(expr);
Thierry FOURNIER348971e2013-11-21 10:50:10 +01001442 free(ckw);
Willy Tarreaua84d3742007-05-07 00:36:48 +02001443 out_return:
1444 return NULL;
1445}
1446
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02001447/* Purge everything in the acl <acl>, then return <acl>. */
1448struct acl *prune_acl(struct acl *acl) {
1449
1450 struct acl_expr *expr, *exprb;
1451
1452 free(acl->name);
1453
1454 list_for_each_entry_safe(expr, exprb, &acl->expr, list) {
1455 LIST_DEL(&expr->list);
1456 prune_acl_expr(expr);
1457 free(expr);
1458 }
1459
1460 return acl;
1461}
1462
Willy Tarreaua84d3742007-05-07 00:36:48 +02001463/* Parse an ACL with the name starting at <args>[0], and with a list of already
1464 * known ACLs in <acl>. If the ACL was not in the list, it will be added.
Willy Tarreau2a56c5e2010-03-15 16:13:29 +01001465 * A pointer to that ACL is returned. If the ACL has an empty name, then it's
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001466 * an anonymous one and it won't be merged with any other one. If <err> is not
1467 * NULL, it will be filled with an appropriate error. This pointer must be
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001468 * freeable or NULL. <al> is the arg_list serving as a head for unresolved
1469 * dependencies.
Willy Tarreaua84d3742007-05-07 00:36:48 +02001470 *
1471 * args syntax: <aclname> <acl_expr>
1472 */
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001473struct acl *parse_acl(const char **args, struct list *known_acl, char **err, struct arg_list *al)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001474{
1475 __label__ out_return, out_free_acl_expr, out_free_name;
1476 struct acl *cur_acl;
1477 struct acl_expr *acl_expr;
1478 char *name;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001479 const char *pos;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001480
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001481 if (**args && (pos = invalid_char(*args))) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001482 memprintf(err, "invalid character in ACL name : '%c'", *pos);
Willy Tarreau2e74c3f2007-12-02 18:45:09 +01001483 goto out_return;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001484 }
Willy Tarreau2e74c3f2007-12-02 18:45:09 +01001485
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001486 acl_expr = parse_acl_expr(args + 1, err, al);
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001487 if (!acl_expr) {
1488 /* parse_acl_expr will have filled <err> here */
Willy Tarreaua84d3742007-05-07 00:36:48 +02001489 goto out_return;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001490 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001491
Willy Tarreau404e8ab2009-07-26 19:40:40 +02001492 /* Check for args beginning with an opening parenthesis just after the
1493 * subject, as this is almost certainly a typo. Right now we can only
1494 * emit a warning, so let's do so.
1495 */
Krzysztof Piotr Oledzki4cdd8312009-10-05 00:23:35 +02001496 if (!strchr(args[1], '(') && *args[2] == '(')
Willy Tarreau404e8ab2009-07-26 19:40:40 +02001497 Warning("parsing acl '%s' :\n"
1498 " matching '%s' for pattern '%s' is likely a mistake and probably\n"
1499 " not what you want. Maybe you need to remove the extraneous space before '('.\n"
1500 " If you are really sure this is not an error, please insert '--' between the\n"
1501 " match and the pattern to make this warning message disappear.\n",
1502 args[0], args[1], args[2]);
1503
Willy Tarreau2a56c5e2010-03-15 16:13:29 +01001504 if (*args[0])
1505 cur_acl = find_acl_by_name(args[0], known_acl);
1506 else
1507 cur_acl = NULL;
1508
Willy Tarreaua84d3742007-05-07 00:36:48 +02001509 if (!cur_acl) {
1510 name = strdup(args[0]);
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001511 if (!name) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001512 memprintf(err, "out of memory when parsing ACL");
Willy Tarreaua84d3742007-05-07 00:36:48 +02001513 goto out_free_acl_expr;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001514 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001515 cur_acl = (struct acl *)calloc(1, sizeof(*cur_acl));
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001516 if (cur_acl == NULL) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001517 memprintf(err, "out of memory when parsing ACL");
Willy Tarreaua84d3742007-05-07 00:36:48 +02001518 goto out_free_name;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001519 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001520
1521 LIST_INIT(&cur_acl->expr);
1522 LIST_ADDQ(known_acl, &cur_acl->list);
1523 cur_acl->name = name;
1524 }
1525
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001526 /* We want to know what features the ACL needs (typically HTTP parsing),
1527 * and where it may be used. If an ACL relies on multiple matches, it is
1528 * OK if at least one of them may match in the context where it is used.
1529 */
Thierry FOURNIER348971e2013-11-21 10:50:10 +01001530 cur_acl->use |= acl_expr->smp->fetch->use;
1531 cur_acl->val |= acl_expr->smp->fetch->val;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001532 LIST_ADDQ(&cur_acl->expr, &acl_expr->list);
1533 return cur_acl;
1534
1535 out_free_name:
1536 free(name);
1537 out_free_acl_expr:
1538 prune_acl_expr(acl_expr);
1539 free(acl_expr);
1540 out_return:
1541 return NULL;
1542}
1543
Willy Tarreau16fbe822007-06-17 11:54:31 +02001544/* Some useful ACLs provided by default. Only those used are allocated. */
1545
1546const struct {
1547 const char *name;
1548 const char *expr[4]; /* put enough for longest expression */
1549} default_acl_list[] = {
Willy Tarreau58393e12008-07-20 10:39:22 +02001550 { .name = "TRUE", .expr = {"always_true",""}},
1551 { .name = "FALSE", .expr = {"always_false",""}},
Willy Tarreau16fbe822007-06-17 11:54:31 +02001552 { .name = "LOCALHOST", .expr = {"src","127.0.0.1/8",""}},
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001553 { .name = "HTTP", .expr = {"req_proto_http",""}},
Willy Tarreau16fbe822007-06-17 11:54:31 +02001554 { .name = "HTTP_1.0", .expr = {"req_ver","1.0",""}},
1555 { .name = "HTTP_1.1", .expr = {"req_ver","1.1",""}},
1556 { .name = "METH_CONNECT", .expr = {"method","CONNECT",""}},
1557 { .name = "METH_GET", .expr = {"method","GET","HEAD",""}},
1558 { .name = "METH_HEAD", .expr = {"method","HEAD",""}},
1559 { .name = "METH_OPTIONS", .expr = {"method","OPTIONS",""}},
1560 { .name = "METH_POST", .expr = {"method","POST",""}},
1561 { .name = "METH_TRACE", .expr = {"method","TRACE",""}},
1562 { .name = "HTTP_URL_ABS", .expr = {"url_reg","^[^/:]*://",""}},
1563 { .name = "HTTP_URL_SLASH", .expr = {"url_beg","/",""}},
1564 { .name = "HTTP_URL_STAR", .expr = {"url","*",""}},
1565 { .name = "HTTP_CONTENT", .expr = {"hdr_val(content-length)","gt","0",""}},
Emeric Brunbede3d02009-06-30 17:54:00 +02001566 { .name = "RDP_COOKIE", .expr = {"req_rdp_cookie_cnt","gt","0",""}},
Willy Tarreauc6317702008-07-20 09:29:50 +02001567 { .name = "REQ_CONTENT", .expr = {"req_len","gt","0",""}},
Willy Tarreaub6fb4202008-07-20 11:18:28 +02001568 { .name = "WAIT_END", .expr = {"wait_end",""}},
Willy Tarreau16fbe822007-06-17 11:54:31 +02001569 { .name = NULL, .expr = {""}}
1570};
1571
1572/* Find a default ACL from the default_acl list, compile it and return it.
1573 * If the ACL is not found, NULL is returned. In theory, it cannot fail,
1574 * except when default ACLs are broken, in which case it will return NULL.
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001575 * If <known_acl> is not NULL, the ACL will be queued at its tail. If <err> is
1576 * not NULL, it will be filled with an error message if an error occurs. This
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001577 * pointer must be freeable or NULL. <al> is an arg_list serving as a list head
1578 * to report missing dependencies.
Willy Tarreau16fbe822007-06-17 11:54:31 +02001579 */
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001580static struct acl *find_acl_default(const char *acl_name, struct list *known_acl,
1581 char **err, struct arg_list *al)
Willy Tarreau16fbe822007-06-17 11:54:31 +02001582{
1583 __label__ out_return, out_free_acl_expr, out_free_name;
1584 struct acl *cur_acl;
1585 struct acl_expr *acl_expr;
1586 char *name;
1587 int index;
1588
1589 for (index = 0; default_acl_list[index].name != NULL; index++) {
1590 if (strcmp(acl_name, default_acl_list[index].name) == 0)
1591 break;
1592 }
1593
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001594 if (default_acl_list[index].name == NULL) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001595 memprintf(err, "no such ACL : '%s'", acl_name);
Willy Tarreau16fbe822007-06-17 11:54:31 +02001596 return NULL;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001597 }
Willy Tarreau16fbe822007-06-17 11:54:31 +02001598
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001599 acl_expr = parse_acl_expr((const char **)default_acl_list[index].expr, err, al);
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001600 if (!acl_expr) {
1601 /* parse_acl_expr must have filled err here */
Willy Tarreau16fbe822007-06-17 11:54:31 +02001602 goto out_return;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001603 }
Willy Tarreau16fbe822007-06-17 11:54:31 +02001604
1605 name = strdup(acl_name);
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001606 if (!name) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001607 memprintf(err, "out of memory when building default ACL '%s'", acl_name);
Willy Tarreau16fbe822007-06-17 11:54:31 +02001608 goto out_free_acl_expr;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001609 }
1610
Willy Tarreau16fbe822007-06-17 11:54:31 +02001611 cur_acl = (struct acl *)calloc(1, sizeof(*cur_acl));
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001612 if (cur_acl == NULL) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001613 memprintf(err, "out of memory when building default ACL '%s'", acl_name);
Willy Tarreau16fbe822007-06-17 11:54:31 +02001614 goto out_free_name;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001615 }
Willy Tarreau16fbe822007-06-17 11:54:31 +02001616
1617 cur_acl->name = name;
Thierry FOURNIER348971e2013-11-21 10:50:10 +01001618 cur_acl->use |= acl_expr->smp->fetch->use;
1619 cur_acl->val |= acl_expr->smp->fetch->val;
Willy Tarreau16fbe822007-06-17 11:54:31 +02001620 LIST_INIT(&cur_acl->expr);
1621 LIST_ADDQ(&cur_acl->expr, &acl_expr->list);
1622 if (known_acl)
1623 LIST_ADDQ(known_acl, &cur_acl->list);
1624
1625 return cur_acl;
1626
1627 out_free_name:
1628 free(name);
1629 out_free_acl_expr:
1630 prune_acl_expr(acl_expr);
1631 free(acl_expr);
1632 out_return:
1633 return NULL;
1634}
Willy Tarreaua84d3742007-05-07 00:36:48 +02001635
1636/* Purge everything in the acl_cond <cond>, then return <cond>. */
1637struct acl_cond *prune_acl_cond(struct acl_cond *cond)
1638{
1639 struct acl_term_suite *suite, *tmp_suite;
1640 struct acl_term *term, *tmp_term;
1641
1642 /* iterate through all term suites and free all terms and all suites */
1643 list_for_each_entry_safe(suite, tmp_suite, &cond->suites, list) {
1644 list_for_each_entry_safe(term, tmp_term, &suite->terms, list)
1645 free(term);
1646 free(suite);
1647 }
1648 return cond;
1649}
1650
1651/* Parse an ACL condition starting at <args>[0], relying on a list of already
1652 * known ACLs passed in <known_acl>. The new condition is returned (or NULL in
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001653 * case of low memory). Supports multiple conditions separated by "or". If
1654 * <err> is not NULL, it will be filled with a pointer to an error message in
1655 * case of error, that the caller is responsible for freeing. The initial
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001656 * location must either be freeable or NULL. The list <al> serves as a list head
1657 * for unresolved dependencies.
Willy Tarreaua84d3742007-05-07 00:36:48 +02001658 */
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001659struct acl_cond *parse_acl_cond(const char **args, struct list *known_acl,
1660 int pol, char **err, struct arg_list *al)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001661{
1662 __label__ out_return, out_free_suite, out_free_term;
Willy Tarreau74b98a82007-06-16 19:35:18 +02001663 int arg, neg;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001664 const char *word;
1665 struct acl *cur_acl;
1666 struct acl_term *cur_term;
1667 struct acl_term_suite *cur_suite;
1668 struct acl_cond *cond;
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001669 unsigned int suite_val;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001670
1671 cond = (struct acl_cond *)calloc(1, sizeof(*cond));
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001672 if (cond == NULL) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001673 memprintf(err, "out of memory when parsing condition");
Willy Tarreaua84d3742007-05-07 00:36:48 +02001674 goto out_return;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001675 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001676
1677 LIST_INIT(&cond->list);
1678 LIST_INIT(&cond->suites);
1679 cond->pol = pol;
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001680 cond->val = 0;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001681
1682 cur_suite = NULL;
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001683 suite_val = ~0U;
Willy Tarreau74b98a82007-06-16 19:35:18 +02001684 neg = 0;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001685 for (arg = 0; *args[arg]; arg++) {
1686 word = args[arg];
1687
1688 /* remove as many exclamation marks as we can */
1689 while (*word == '!') {
1690 neg = !neg;
1691 word++;
1692 }
1693
1694 /* an empty word is allowed because we cannot force the user to
1695 * always think about not leaving exclamation marks alone.
1696 */
1697 if (!*word)
1698 continue;
1699
Willy Tarreau16fbe822007-06-17 11:54:31 +02001700 if (strcasecmp(word, "or") == 0 || strcmp(word, "||") == 0) {
Willy Tarreaua84d3742007-05-07 00:36:48 +02001701 /* new term suite */
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001702 cond->val |= suite_val;
1703 suite_val = ~0U;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001704 cur_suite = NULL;
1705 neg = 0;
1706 continue;
1707 }
1708
Willy Tarreau95fa4692010-02-01 13:05:50 +01001709 if (strcmp(word, "{") == 0) {
1710 /* we may have a complete ACL expression between two braces,
1711 * find the last one.
1712 */
1713 int arg_end = arg + 1;
1714 const char **args_new;
1715
1716 while (*args[arg_end] && strcmp(args[arg_end], "}") != 0)
1717 arg_end++;
1718
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001719 if (!*args[arg_end]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001720 memprintf(err, "missing closing '}' in condition");
Willy Tarreau95fa4692010-02-01 13:05:50 +01001721 goto out_free_suite;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001722 }
Willy Tarreau95fa4692010-02-01 13:05:50 +01001723
1724 args_new = calloc(1, (arg_end - arg + 1) * sizeof(*args_new));
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001725 if (!args_new) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001726 memprintf(err, "out of memory when parsing condition");
Willy Tarreau95fa4692010-02-01 13:05:50 +01001727 goto out_free_suite;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001728 }
Willy Tarreau95fa4692010-02-01 13:05:50 +01001729
Willy Tarreau2a56c5e2010-03-15 16:13:29 +01001730 args_new[0] = "";
Willy Tarreau95fa4692010-02-01 13:05:50 +01001731 memcpy(args_new + 1, args + arg + 1, (arg_end - arg) * sizeof(*args_new));
1732 args_new[arg_end - arg] = "";
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001733 cur_acl = parse_acl(args_new, known_acl, err, al);
Willy Tarreau95fa4692010-02-01 13:05:50 +01001734 free(args_new);
1735
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001736 if (!cur_acl) {
1737 /* note that parse_acl() must have filled <err> here */
Willy Tarreau16fbe822007-06-17 11:54:31 +02001738 goto out_free_suite;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001739 }
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001740 word = args[arg + 1];
Willy Tarreau95fa4692010-02-01 13:05:50 +01001741 arg = arg_end;
1742 }
1743 else {
1744 /* search for <word> in the known ACL names. If we do not find
1745 * it, let's look for it in the default ACLs, and if found, add
1746 * it to the list of ACLs of this proxy. This makes it possible
1747 * to override them.
1748 */
1749 cur_acl = find_acl_by_name(word, known_acl);
1750 if (cur_acl == NULL) {
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001751 cur_acl = find_acl_default(word, known_acl, err, al);
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001752 if (cur_acl == NULL) {
1753 /* note that find_acl_default() must have filled <err> here */
Willy Tarreau95fa4692010-02-01 13:05:50 +01001754 goto out_free_suite;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001755 }
Willy Tarreau95fa4692010-02-01 13:05:50 +01001756 }
Willy Tarreau16fbe822007-06-17 11:54:31 +02001757 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001758
1759 cur_term = (struct acl_term *)calloc(1, sizeof(*cur_term));
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001760 if (cur_term == NULL) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001761 memprintf(err, "out of memory when parsing condition");
Willy Tarreaua84d3742007-05-07 00:36:48 +02001762 goto out_free_suite;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001763 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001764
1765 cur_term->acl = cur_acl;
1766 cur_term->neg = neg;
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001767
1768 /* Here it is a bit complex. The acl_term_suite is a conjunction
1769 * of many terms. It may only be used if all of its terms are
1770 * usable at the same time. So the suite's validity domain is an
1771 * AND between all ACL keywords' ones. But, the global condition
1772 * is valid if at least one term suite is OK. So it's an OR between
1773 * all of their validity domains. We could emit a warning as soon
1774 * as suite_val is null because it means that the last ACL is not
1775 * compatible with the previous ones. Let's remain simple for now.
1776 */
1777 cond->use |= cur_acl->use;
1778 suite_val &= cur_acl->val;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001779
1780 if (!cur_suite) {
1781 cur_suite = (struct acl_term_suite *)calloc(1, sizeof(*cur_suite));
Willy Tarreauf678b7f2013-01-24 00:25:39 +01001782 if (cur_suite == NULL) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001783 memprintf(err, "out of memory when parsing condition");
Willy Tarreaua84d3742007-05-07 00:36:48 +02001784 goto out_free_term;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001785 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001786 LIST_INIT(&cur_suite->terms);
1787 LIST_ADDQ(&cond->suites, &cur_suite->list);
1788 }
1789 LIST_ADDQ(&cur_suite->terms, &cur_term->list);
Willy Tarreau74b98a82007-06-16 19:35:18 +02001790 neg = 0;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001791 }
1792
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001793 cond->val |= suite_val;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001794 return cond;
1795
1796 out_free_term:
1797 free(cur_term);
1798 out_free_suite:
1799 prune_acl_cond(cond);
1800 free(cond);
1801 out_return:
1802 return NULL;
1803}
1804
Willy Tarreau2bbba412010-01-28 16:48:33 +01001805/* Builds an ACL condition starting at the if/unless keyword. The complete
1806 * condition is returned. NULL is returned in case of error or if the first
1807 * word is neither "if" nor "unless". It automatically sets the file name and
Willy Tarreau25320b22013-03-24 07:22:08 +01001808 * the line number in the condition for better error reporting, and sets the
1809 * HTTP intiailization requirements in the proxy. If <err> is not NULL, it will
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001810 * be filled with a pointer to an error message in case of error, that the
1811 * caller is responsible for freeing. The initial location must either be
1812 * freeable or NULL.
Willy Tarreau2bbba412010-01-28 16:48:33 +01001813 */
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001814struct 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 +01001815{
1816 int pol = ACL_COND_NONE;
1817 struct acl_cond *cond = NULL;
1818
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001819 if (err)
1820 *err = NULL;
1821
Willy Tarreau2bbba412010-01-28 16:48:33 +01001822 if (!strcmp(*args, "if")) {
1823 pol = ACL_COND_IF;
1824 args++;
1825 }
1826 else if (!strcmp(*args, "unless")) {
1827 pol = ACL_COND_UNLESS;
1828 args++;
1829 }
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001830 else {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001831 memprintf(err, "conditions must start with either 'if' or 'unless'");
Willy Tarreau2bbba412010-01-28 16:48:33 +01001832 return NULL;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001833 }
Willy Tarreau2bbba412010-01-28 16:48:33 +01001834
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001835 cond = parse_acl_cond(args, &px->acl, pol, err, &px->conf.args);
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001836 if (!cond) {
1837 /* note that parse_acl_cond must have filled <err> here */
Willy Tarreau2bbba412010-01-28 16:48:33 +01001838 return NULL;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001839 }
Willy Tarreau2bbba412010-01-28 16:48:33 +01001840
1841 cond->file = file;
1842 cond->line = line;
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001843 px->http_needed |= !!(cond->use & SMP_USE_HTTP_ANY);
Willy Tarreau2bbba412010-01-28 16:48:33 +01001844 return cond;
1845}
1846
Thierry FOURNIERdd69a042013-11-22 19:14:42 +01001847/* This function execute the match part of the acl. It's applying
1848 * acl <expr> on sample <smp>. <sample> is filled only if the pointer
1849 * is not NULL. The function return ACL_PAT_FAIL, ACL_PAT_MISS or
1850 * ACL_PAT_PASS
Thierry FOURNIER29d47b82013-11-22 18:03:42 +01001851 */
Thierry FOURNIERdd69a042013-11-22 19:14:42 +01001852inline int acl_exec_match(struct acl_expr *expr, struct sample *smp,
1853 struct sample_storage **sample)
Thierry FOURNIER29d47b82013-11-22 18:03:42 +01001854{
1855 int acl_res = ACL_PAT_FAIL;
1856 struct acl_pattern *pattern;
Thierry FOURNIERdd69a042013-11-22 19:14:42 +01001857 struct ebmb_node *node = NULL;
1858 struct acl_idx_elt *elt;
Thierry FOURNIER29d47b82013-11-22 18:03:42 +01001859
1860 if (expr->match == acl_match_nothing) {
1861 if (smp->data.uint)
1862 acl_res |= ACL_PAT_PASS;
1863 else
1864 acl_res |= ACL_PAT_FAIL;
1865 }
1866 else if (!expr->match) {
1867 /* just check for existence */
1868 acl_res |= ACL_PAT_PASS;
1869 }
1870 else {
1871 if (!eb_is_empty(&expr->pattern_tree)) {
1872 /* a tree is present, let's check what type it is */
1873 if (expr->match == acl_match_str)
Thierry FOURNIERdd69a042013-11-22 19:14:42 +01001874 node = acl_lookup_str(smp, expr);
Thierry FOURNIER29d47b82013-11-22 18:03:42 +01001875 else if (expr->match == acl_match_ip)
Thierry FOURNIERdd69a042013-11-22 19:14:42 +01001876 node = acl_lookup_ip(smp, expr);
1877 if (node) {
1878 acl_res |= ACL_PAT_PASS;
1879 elt = ebmb_entry(node, struct acl_idx_elt, node);
1880 if (sample)
1881 *sample = elt->smp;
1882 }
Thierry FOURNIER29d47b82013-11-22 18:03:42 +01001883 }
1884
1885 /* call the match() function for all tests on this value */
1886 list_for_each_entry(pattern, &expr->patterns, list) {
1887 if (acl_res == ACL_PAT_PASS)
1888 break;
1889 acl_res |= expr->match(smp, pattern);
Thierry FOURNIERdd69a042013-11-22 19:14:42 +01001890 if (sample)
1891 *sample = pattern->smp;
Thierry FOURNIER29d47b82013-11-22 18:03:42 +01001892 }
1893 }
1894
1895 return acl_res;
1896}
1897
Willy Tarreau11382812008-07-09 16:18:21 +02001898/* Execute condition <cond> and return either ACL_PAT_FAIL, ACL_PAT_MISS or
Willy Tarreaub6866442008-07-14 23:54:42 +02001899 * ACL_PAT_PASS depending on the test results. ACL_PAT_MISS may only be
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001900 * returned if <opt> does not contain SMP_OPT_FINAL, indicating that incomplete
Willy Tarreau7a777ed2012-04-26 11:44:02 +02001901 * data is being examined. The function automatically sets SMP_OPT_ITERATE.
Willy Tarreaub6866442008-07-14 23:54:42 +02001902 * This function only computes the condition, it does not apply the polarity
1903 * required by IF/UNLESS, it's up to the caller to do this using something like
1904 * this :
Willy Tarreau11382812008-07-09 16:18:21 +02001905 *
1906 * res = acl_pass(res);
Willy Tarreaub6866442008-07-14 23:54:42 +02001907 * if (res == ACL_PAT_MISS)
1908 * return 0;
Willy Tarreau11382812008-07-09 16:18:21 +02001909 * if (cond->pol == ACL_COND_UNLESS)
1910 * res = !res;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001911 */
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001912int 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 +02001913{
1914 __label__ fetch_next;
1915 struct acl_term_suite *suite;
1916 struct acl_term *term;
1917 struct acl_expr *expr;
1918 struct acl *acl;
Willy Tarreau37406352012-04-23 16:16:37 +02001919 struct sample smp;
Willy Tarreau11382812008-07-09 16:18:21 +02001920 int acl_res, suite_res, cond_res;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001921
Willy Tarreau7a777ed2012-04-26 11:44:02 +02001922 /* ACLs are iterated over all values, so let's always set the flag to
1923 * indicate this to the fetch functions.
1924 */
1925 opt |= SMP_OPT_ITERATE;
1926
Willy Tarreau11382812008-07-09 16:18:21 +02001927 /* We're doing a logical OR between conditions so we initialize to FAIL.
1928 * The MISS status is propagated down from the suites.
1929 */
Willy Tarreaua84d3742007-05-07 00:36:48 +02001930 cond_res = ACL_PAT_FAIL;
1931 list_for_each_entry(suite, &cond->suites, list) {
Willy Tarreau11382812008-07-09 16:18:21 +02001932 /* Evaluate condition suite <suite>. We stop at the first term
1933 * which returns ACL_PAT_FAIL. The MISS status is still propagated
1934 * in case of uncertainty in the result.
Willy Tarreaua84d3742007-05-07 00:36:48 +02001935 */
1936
1937 /* we're doing a logical AND between terms, so we must set the
1938 * initial value to PASS.
1939 */
1940 suite_res = ACL_PAT_PASS;
1941 list_for_each_entry(term, &suite->terms, list) {
1942 acl = term->acl;
1943
1944 /* FIXME: use cache !
1945 * check acl->cache_idx for this.
1946 */
1947
1948 /* ACL result not cached. Let's scan all the expressions
1949 * and use the first one to match.
1950 */
1951 acl_res = ACL_PAT_FAIL;
1952 list_for_each_entry(expr, &acl->expr, list) {
Willy Tarreaud41f8d82007-06-10 10:06:18 +02001953 /* we need to reset context and flags */
Willy Tarreau37406352012-04-23 16:16:37 +02001954 memset(&smp, 0, sizeof(smp));
Willy Tarreaua84d3742007-05-07 00:36:48 +02001955 fetch_next:
Thierry FOURNIER348971e2013-11-21 10:50:10 +01001956 if (!sample_process(px, l4, l7, opt, expr->smp, &smp)) {
Willy Tarreaub6866442008-07-14 23:54:42 +02001957 /* maybe we could not fetch because of missing data */
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001958 if (smp.flags & SMP_F_MAY_CHANGE && !(opt & SMP_OPT_FINAL))
Willy Tarreaub6866442008-07-14 23:54:42 +02001959 acl_res |= ACL_PAT_MISS;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001960 continue;
Willy Tarreaub6866442008-07-14 23:54:42 +02001961 }
Willy Tarreauc4262962010-05-10 23:42:40 +02001962
Thierry FOURNIERdd69a042013-11-22 19:14:42 +01001963 acl_res |= acl_exec_match(expr, &smp, NULL);
Willy Tarreaua84d3742007-05-07 00:36:48 +02001964 /*
Willy Tarreau11382812008-07-09 16:18:21 +02001965 * OK now acl_res holds the result of this expression
1966 * as one of ACL_PAT_FAIL, ACL_PAT_MISS or ACL_PAT_PASS.
Willy Tarreaua84d3742007-05-07 00:36:48 +02001967 *
Willy Tarreau11382812008-07-09 16:18:21 +02001968 * Then if (!MISS) we can cache the result, and put
Willy Tarreau37406352012-04-23 16:16:37 +02001969 * (smp.flags & SMP_F_VOLATILE) in the cache flags.
Willy Tarreaua84d3742007-05-07 00:36:48 +02001970 *
1971 * FIXME: implement cache.
1972 *
1973 */
1974
Willy Tarreau11382812008-07-09 16:18:21 +02001975 /* we're ORing these terms, so a single PASS is enough */
1976 if (acl_res == ACL_PAT_PASS)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001977 break;
1978
Willy Tarreau37406352012-04-23 16:16:37 +02001979 if (smp.flags & SMP_F_NOT_LAST)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001980 goto fetch_next;
Willy Tarreaub6866442008-07-14 23:54:42 +02001981
1982 /* sometimes we know the fetched data is subject to change
1983 * later and give another chance for a new match (eg: request
1984 * size, time, ...)
1985 */
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001986 if (smp.flags & SMP_F_MAY_CHANGE && !(opt & SMP_OPT_FINAL))
Willy Tarreaub6866442008-07-14 23:54:42 +02001987 acl_res |= ACL_PAT_MISS;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001988 }
1989 /*
1990 * Here we have the result of an ACL (cached or not).
1991 * ACLs are combined, negated or not, to form conditions.
1992 */
1993
Willy Tarreaua84d3742007-05-07 00:36:48 +02001994 if (term->neg)
Willy Tarreau11382812008-07-09 16:18:21 +02001995 acl_res = acl_neg(acl_res);
Willy Tarreaua84d3742007-05-07 00:36:48 +02001996
1997 suite_res &= acl_res;
Willy Tarreau11382812008-07-09 16:18:21 +02001998
Willy Tarreau79c412b2013-10-30 19:30:32 +01001999 /* we're ANDing these terms, so a single FAIL or MISS is enough */
2000 if (suite_res != ACL_PAT_PASS)
Willy Tarreaua84d3742007-05-07 00:36:48 +02002001 break;
2002 }
2003 cond_res |= suite_res;
Willy Tarreau11382812008-07-09 16:18:21 +02002004
2005 /* we're ORing these terms, so a single PASS is enough */
2006 if (cond_res == ACL_PAT_PASS)
Willy Tarreaua84d3742007-05-07 00:36:48 +02002007 break;
2008 }
Willy Tarreau11382812008-07-09 16:18:21 +02002009 return cond_res;
Willy Tarreaua84d3742007-05-07 00:36:48 +02002010}
2011
Willy Tarreaua91d0a52013-03-25 08:12:18 +01002012/* Returns a pointer to the first ACL conflicting with usage at place <where>
2013 * which is one of the SMP_VAL_* bits indicating a check place, or NULL if
2014 * no conflict is found. Only full conflicts are detected (ACL is not usable).
2015 * Use the next function to check for useless keywords.
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02002016 */
Willy Tarreaua91d0a52013-03-25 08:12:18 +01002017const struct acl *acl_cond_conflicts(const struct acl_cond *cond, unsigned int where)
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02002018{
2019 struct acl_term_suite *suite;
2020 struct acl_term *term;
2021 struct acl *acl;
2022
2023 list_for_each_entry(suite, &cond->suites, list) {
2024 list_for_each_entry(term, &suite->terms, list) {
2025 acl = term->acl;
Willy Tarreaua91d0a52013-03-25 08:12:18 +01002026 if (!(acl->val & where))
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02002027 return acl;
2028 }
2029 }
2030 return NULL;
2031}
2032
Willy Tarreaua91d0a52013-03-25 08:12:18 +01002033/* Returns a pointer to the first ACL and its first keyword to conflict with
2034 * usage at place <where> which is one of the SMP_VAL_* bits indicating a check
2035 * place. Returns true if a conflict is found, with <acl> and <kw> set (if non
2036 * null), or false if not conflict is found. The first useless keyword is
2037 * returned.
2038 */
Willy Tarreau93fddf12013-03-31 22:59:32 +02002039int 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 +01002040{
2041 struct acl_term_suite *suite;
2042 struct acl_term *term;
2043 struct acl_expr *expr;
2044
2045 list_for_each_entry(suite, &cond->suites, list) {
2046 list_for_each_entry(term, &suite->terms, list) {
2047 list_for_each_entry(expr, &term->acl->expr, list) {
Thierry FOURNIER348971e2013-11-21 10:50:10 +01002048 if (!(expr->smp->fetch->val & where)) {
Willy Tarreaua91d0a52013-03-25 08:12:18 +01002049 if (acl)
2050 *acl = term->acl;
2051 if (kw)
2052 *kw = expr->kw;
2053 return 1;
2054 }
2055 }
2056 }
2057 }
2058 return 0;
2059}
2060
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01002061/*
2062 * Find targets for userlist and groups in acl. Function returns the number
Willy Tarreaua4312fa2013-04-02 16:34:32 +02002063 * of errors or OK if everything is fine. It must be called only once sample
2064 * fetch arguments have been resolved (after smp_resolve_args()).
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01002065 */
Willy Tarreaua4312fa2013-04-02 16:34:32 +02002066int acl_find_targets(struct proxy *p)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01002067{
2068
2069 struct acl *acl;
2070 struct acl_expr *expr;
2071 struct acl_pattern *pattern;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01002072 int cfgerr = 0;
2073
2074 list_for_each_entry(acl, &p->acl, list) {
2075 list_for_each_entry(expr, &acl->expr, list) {
Willy Tarreaua4312fa2013-04-02 16:34:32 +02002076 if (!strcmp(expr->kw, "http_auth_group")) {
2077 /* Note: the ARGT_USR argument may only have been resolved earlier
2078 * by smp_resolve_args().
2079 */
Thierry FOURNIER348971e2013-11-21 10:50:10 +01002080 if (expr->smp->arg_p->unresolved) {
Willy Tarreaua4312fa2013-04-02 16:34:32 +02002081 Alert("Internal bug in proxy %s: %sacl %s %s() makes use of unresolved userlist '%s'. Please report this.\n",
Thierry FOURNIER348971e2013-11-21 10:50:10 +01002082 p->id, *acl->name ? "" : "anonymous ", acl->name, expr->kw, expr->smp->arg_p->data.str.str);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02002083 cfgerr++;
Willy Tarreau496aa012012-06-01 10:38:29 +02002084 continue;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01002085 }
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01002086
2087 if (LIST_ISEMPTY(&expr->patterns)) {
2088 Alert("proxy %s: acl %s %s(): no groups specified.\n",
Willy Tarreau93fddf12013-03-31 22:59:32 +02002089 p->id, acl->name, expr->kw);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01002090 cfgerr++;
2091 continue;
2092 }
2093
2094 list_for_each_entry(pattern, &expr->patterns, list) {
Willy Tarreau7d1df412012-11-23 23:47:36 +01002095 /* this keyword only has one argument */
Thierry FOURNIER348971e2013-11-21 10:50:10 +01002096 pattern->val.group_mask = auth_resolve_groups(expr->smp->arg_p->data.usr, pattern->ptr.str);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01002097
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01002098 if (!pattern->val.group_mask) {
Willy Tarreaua4312fa2013-04-02 16:34:32 +02002099 Alert("proxy %s: acl %s %s(): invalid group '%s'.\n",
2100 p->id, acl->name, expr->kw, pattern->ptr.str);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01002101 cfgerr++;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01002102 }
Willy Tarreaua4312fa2013-04-02 16:34:32 +02002103 free(pattern->ptr.str);
2104 pattern->ptr.str = NULL;
2105 pattern->len = 0;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01002106 }
2107 }
2108 }
2109 }
2110
2111 return cfgerr;
2112}
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02002113
Willy Tarreau8ed669b2013-01-11 15:49:37 +01002114/* initializes ACLs by resolving the sample fetch names they rely upon.
2115 * Returns 0 on success, otherwise an error.
2116 */
2117int init_acl()
2118{
2119 int err = 0;
2120 int index;
2121 const char *name;
2122 struct acl_kw_list *kwl;
2123 struct sample_fetch *smp;
2124
2125 list_for_each_entry(kwl, &acl_keywords.list, list) {
2126 for (index = 0; kwl->kw[index].kw != NULL; index++) {
2127 name = kwl->kw[index].fetch_kw;
2128 if (!name)
2129 name = kwl->kw[index].kw;
2130
2131 smp = find_sample_fetch(name, strlen(name));
2132 if (!smp) {
2133 Alert("Critical internal error: ACL keyword '%s' relies on sample fetch '%s' which was not registered!\n",
2134 kwl->kw[index].kw, name);
2135 err++;
2136 continue;
2137 }
2138 kwl->kw[index].smp = smp;
2139 }
2140 }
2141 return err;
2142}
Willy Tarreaud4c33c82013-01-07 21:59:07 +01002143
Willy Tarreaua84d3742007-05-07 00:36:48 +02002144/************************************************************************/
Willy Tarreaud4c33c82013-01-07 21:59:07 +01002145/* All supported sample and ACL keywords must be declared here. */
2146/************************************************************************/
2147
2148/* Note: must not be declared <const> as its list will be overwritten.
Willy Tarreau61612d42012-04-19 18:42:05 +02002149 * Please take care of keeping this list alphabetically sorted.
2150 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02002151static struct acl_kw_list acl_kws = {ILH, {
Willy Tarreaud4c33c82013-01-07 21:59:07 +01002152 { /* END */ },
Willy Tarreaua84d3742007-05-07 00:36:48 +02002153}};
2154
Willy Tarreaua84d3742007-05-07 00:36:48 +02002155__attribute__((constructor))
2156static void __acl_init(void)
2157{
Willy Tarreaua84d3742007-05-07 00:36:48 +02002158 acl_register_keywords(&acl_kws);
2159}
2160
2161
2162/*
2163 * Local variables:
2164 * c-indent-level: 8
2165 * c-basic-offset: 8
2166 * End:
2167 */