blob: 845e1de1e981a408bcfaff9dc541439829c08f45 [file] [log] [blame]
Willy Tarreaua84d3742007-05-07 00:36:48 +02001/*
2 * ACL management functions.
3 *
Willy Tarreaud4c33c82013-01-07 21:59:07 +01004 * Copyright 2000-2013 Willy Tarreau <w@1wt.eu>
Willy Tarreaua84d3742007-05-07 00:36:48 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
Willy Tarreauae8b7962007-06-09 23:10:04 +020013#include <ctype.h>
Willy Tarreaua84d3742007-05-07 00:36:48 +020014#include <stdio.h>
15#include <string.h>
16
17#include <common/config.h>
18#include <common/mini-clist.h>
19#include <common/standard.h>
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +010020#include <common/uri_auth.h>
Willy Tarreaua84d3742007-05-07 00:36:48 +020021
Willy Tarreau2b5285d2010-05-09 23:45:24 +020022#include <types/global.h>
23
Willy Tarreaua84d3742007-05-07 00:36:48 +020024#include <proto/acl.h>
Willy Tarreau34db1082012-04-19 17:16:54 +020025#include <proto/arg.h>
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010026#include <proto/auth.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020027#include <proto/channel.h>
Willy Tarreau404e8ab2009-07-26 19:40:40 +020028#include <proto/log.h>
Willy Tarreau0b1cd942010-05-16 22:18:27 +020029#include <proto/proxy.h>
Willy Tarreau8ed669b2013-01-11 15:49:37 +010030#include <proto/sample.h>
Willy Tarreaud28c3532012-04-19 19:28:33 +020031#include <proto/stick_table.h>
Willy Tarreaua84d3742007-05-07 00:36:48 +020032
Willy Tarreauc4262962010-05-10 23:42:40 +020033#include <ebsttree.h>
34
Willy Tarreaua84d3742007-05-07 00:36:48 +020035/* List head of all known ACL keywords */
36static struct acl_kw_list acl_keywords = {
37 .list = LIST_HEAD_INIT(acl_keywords.list)
38};
39
Willy Tarreau5adeda12013-03-31 22:13:34 +020040static char *acl_match_names[ACL_MATCH_NUM] = {
41 [ACL_MATCH_FOUND] = "found",
42 [ACL_MATCH_BOOL] = "bool",
43 [ACL_MATCH_INT] = "int",
44 [ACL_MATCH_IP] = "ip",
45 [ACL_MATCH_BIN] = "bin",
46 [ACL_MATCH_LEN] = "len",
47 [ACL_MATCH_STR] = "str",
48 [ACL_MATCH_BEG] = "beg",
49 [ACL_MATCH_SUB] = "sub",
50 [ACL_MATCH_DIR] = "dir",
51 [ACL_MATCH_DOM] = "dom",
52 [ACL_MATCH_END] = "end",
53 [ACL_MATCH_REG] = "reg",
54};
55
56static int (*acl_parse_fcts[ACL_MATCH_NUM])(const char **, struct acl_pattern *, int *, char **) = {
57 [ACL_MATCH_FOUND] = acl_parse_nothing,
58 [ACL_MATCH_BOOL] = acl_parse_nothing,
59 [ACL_MATCH_INT] = acl_parse_int,
60 [ACL_MATCH_IP] = acl_parse_ip,
61 [ACL_MATCH_BIN] = acl_parse_bin,
62 [ACL_MATCH_LEN] = acl_parse_int,
63 [ACL_MATCH_STR] = acl_parse_str,
64 [ACL_MATCH_BEG] = acl_parse_str,
65 [ACL_MATCH_SUB] = acl_parse_str,
66 [ACL_MATCH_DIR] = acl_parse_str,
67 [ACL_MATCH_DOM] = acl_parse_str,
68 [ACL_MATCH_END] = acl_parse_str,
69 [ACL_MATCH_REG] = acl_parse_reg,
70};
71
72static int (*acl_match_fcts[ACL_MATCH_NUM])(struct sample *, struct acl_pattern *) = {
73 [ACL_MATCH_FOUND] = NULL,
74 [ACL_MATCH_BOOL] = acl_match_nothing,
75 [ACL_MATCH_INT] = acl_match_int,
76 [ACL_MATCH_IP] = acl_match_ip,
77 [ACL_MATCH_BIN] = acl_match_bin,
78 [ACL_MATCH_LEN] = acl_match_len,
79 [ACL_MATCH_STR] = acl_match_str,
80 [ACL_MATCH_BEG] = acl_match_beg,
81 [ACL_MATCH_SUB] = acl_match_sub,
82 [ACL_MATCH_DIR] = acl_match_dir,
83 [ACL_MATCH_DOM] = acl_match_dom,
84 [ACL_MATCH_END] = acl_match_end,
85 [ACL_MATCH_REG] = acl_match_reg,
86};
87
88/* return the ACL_MATCH_* index for match name "name", or < 0 if not found */
89static int acl_find_match_name(const char *name)
90{
91 int i;
92
93 for (i = 0; i < ACL_MATCH_NUM; i++)
94 if (strcmp(name, acl_match_names[i]) == 0)
95 return i;
96 return -1;
97}
Willy Tarreaua84d3742007-05-07 00:36:48 +020098
Willy Tarreaua5909832007-06-17 20:40:25 +020099/*
Willy Tarreau58393e12008-07-20 10:39:22 +0200100 * These functions are exported and may be used by any other component.
101 */
102
103/* ignore the current line */
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200104int acl_parse_nothing(const char **text, struct acl_pattern *pattern, int *opaque, char **err)
Willy Tarreaua5909832007-06-17 20:40:25 +0200105{
Willy Tarreau58393e12008-07-20 10:39:22 +0200106 return 1;
107}
108
Willy Tarreaua5909832007-06-17 20:40:25 +0200109/* always return false */
Willy Tarreau37406352012-04-23 16:16:37 +0200110int acl_match_nothing(struct sample *smp, struct acl_pattern *pattern)
Willy Tarreaua5909832007-06-17 20:40:25 +0200111{
Willy Tarreau11382812008-07-09 16:18:21 +0200112 return ACL_PAT_FAIL;
Willy Tarreaua5909832007-06-17 20:40:25 +0200113}
114
115
Willy Tarreaua84d3742007-05-07 00:36:48 +0200116/* NB: For two strings to be identical, it is required that their lengths match */
Willy Tarreau37406352012-04-23 16:16:37 +0200117int acl_match_str(struct sample *smp, struct acl_pattern *pattern)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200118{
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200119 int icase;
120
Willy Tarreauf853c462012-04-23 18:53:56 +0200121 if (pattern->len != smp->data.str.len)
Willy Tarreau11382812008-07-09 16:18:21 +0200122 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200123
124 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
Willy Tarreauf853c462012-04-23 18:53:56 +0200125 if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) == 0) ||
126 (!icase && strncmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) == 0))
Willy Tarreau11382812008-07-09 16:18:21 +0200127 return ACL_PAT_PASS;
128 return ACL_PAT_FAIL;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200129}
130
Emeric Brun07ca4962012-10-17 13:38:19 +0200131/* NB: For two binaries buf to be identical, it is required that their lengths match */
132int acl_match_bin(struct sample *smp, struct acl_pattern *pattern)
133{
134 if (pattern->len != smp->data.str.len)
135 return ACL_PAT_FAIL;
136
137 if (memcmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) == 0)
138 return ACL_PAT_PASS;
139 return ACL_PAT_FAIL;
140}
141
Willy Tarreauc4262962010-05-10 23:42:40 +0200142/* Lookup a string in the expression's pattern tree. The node is returned if it
143 * exists, otherwise NULL.
144 */
Willy Tarreau37406352012-04-23 16:16:37 +0200145static void *acl_lookup_str(struct sample *smp, struct acl_expr *expr)
Willy Tarreauc4262962010-05-10 23:42:40 +0200146{
147 /* data are stored in a tree */
148 struct ebmb_node *node;
149 char prev;
150
151 /* we may have to force a trailing zero on the test pattern */
Willy Tarreauf853c462012-04-23 18:53:56 +0200152 prev = smp->data.str.str[smp->data.str.len];
Willy Tarreauc4262962010-05-10 23:42:40 +0200153 if (prev)
Willy Tarreauf853c462012-04-23 18:53:56 +0200154 smp->data.str.str[smp->data.str.len] = '\0';
155 node = ebst_lookup(&expr->pattern_tree, smp->data.str.str);
Willy Tarreauc4262962010-05-10 23:42:40 +0200156 if (prev)
Willy Tarreauf853c462012-04-23 18:53:56 +0200157 smp->data.str.str[smp->data.str.len] = prev;
Willy Tarreauc4262962010-05-10 23:42:40 +0200158 return node;
159}
160
Willy Tarreau21e5b0e2012-04-23 19:25:44 +0200161/* Executes a regex. It temporarily changes the data to add a trailing zero,
162 * and restores the previous character when leaving.
Willy Tarreauf3d25982007-05-08 22:45:09 +0200163 */
Willy Tarreau37406352012-04-23 16:16:37 +0200164int acl_match_reg(struct sample *smp, struct acl_pattern *pattern)
Willy Tarreauf3d25982007-05-08 22:45:09 +0200165{
166 char old_char;
167 int ret;
168
Willy Tarreauf853c462012-04-23 18:53:56 +0200169 old_char = smp->data.str.str[smp->data.str.len];
170 smp->data.str.str[smp->data.str.len] = 0;
Willy Tarreauf3d25982007-05-08 22:45:09 +0200171
Hiroaki Nakamura70351322013-01-13 15:00:42 +0900172 if (regex_exec(pattern->ptr.reg, smp->data.str.str, smp->data.str.len) == 0)
Willy Tarreau11382812008-07-09 16:18:21 +0200173 ret = ACL_PAT_PASS;
Willy Tarreauf3d25982007-05-08 22:45:09 +0200174 else
Willy Tarreau11382812008-07-09 16:18:21 +0200175 ret = ACL_PAT_FAIL;
Willy Tarreauf3d25982007-05-08 22:45:09 +0200176
Willy Tarreauf853c462012-04-23 18:53:56 +0200177 smp->data.str.str[smp->data.str.len] = old_char;
Willy Tarreauf3d25982007-05-08 22:45:09 +0200178 return ret;
179}
180
Willy Tarreaua84d3742007-05-07 00:36:48 +0200181/* Checks that the pattern matches the beginning of the tested string. */
Willy Tarreau37406352012-04-23 16:16:37 +0200182int acl_match_beg(struct sample *smp, struct acl_pattern *pattern)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200183{
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200184 int icase;
185
Willy Tarreauf853c462012-04-23 18:53:56 +0200186 if (pattern->len > smp->data.str.len)
Willy Tarreau11382812008-07-09 16:18:21 +0200187 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200188
189 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
Willy Tarreauf853c462012-04-23 18:53:56 +0200190 if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str, pattern->len) != 0) ||
191 (!icase && strncmp(pattern->ptr.str, smp->data.str.str, pattern->len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +0200192 return ACL_PAT_FAIL;
193 return ACL_PAT_PASS;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200194}
195
196/* Checks that the pattern matches the end of the tested string. */
Willy Tarreau37406352012-04-23 16:16:37 +0200197int acl_match_end(struct sample *smp, struct acl_pattern *pattern)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200198{
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200199 int icase;
200
Willy Tarreauf853c462012-04-23 18:53:56 +0200201 if (pattern->len > smp->data.str.len)
Willy Tarreau11382812008-07-09 16:18:21 +0200202 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200203 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
Willy Tarreauf853c462012-04-23 18:53:56 +0200204 if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str + smp->data.str.len - pattern->len, pattern->len) != 0) ||
205 (!icase && strncmp(pattern->ptr.str, smp->data.str.str + smp->data.str.len - pattern->len, pattern->len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +0200206 return ACL_PAT_FAIL;
207 return ACL_PAT_PASS;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200208}
209
210/* Checks that the pattern is included inside the tested string.
211 * NB: Suboptimal, should be rewritten using a Boyer-Moore method.
212 */
Willy Tarreau37406352012-04-23 16:16:37 +0200213int acl_match_sub(struct sample *smp, struct acl_pattern *pattern)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200214{
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200215 int icase;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200216 char *end;
217 char *c;
218
Willy Tarreauf853c462012-04-23 18:53:56 +0200219 if (pattern->len > smp->data.str.len)
Willy Tarreau11382812008-07-09 16:18:21 +0200220 return ACL_PAT_FAIL;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200221
Willy Tarreauf853c462012-04-23 18:53:56 +0200222 end = smp->data.str.str + smp->data.str.len - pattern->len;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200223 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
224 if (icase) {
Willy Tarreauf853c462012-04-23 18:53:56 +0200225 for (c = smp->data.str.str; c <= end; c++) {
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200226 if (tolower(*c) != tolower(*pattern->ptr.str))
227 continue;
228 if (strncasecmp(pattern->ptr.str, c, pattern->len) == 0)
Willy Tarreau11382812008-07-09 16:18:21 +0200229 return ACL_PAT_PASS;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200230 }
231 } else {
Willy Tarreauf853c462012-04-23 18:53:56 +0200232 for (c = smp->data.str.str; c <= end; c++) {
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200233 if (*c != *pattern->ptr.str)
234 continue;
235 if (strncmp(pattern->ptr.str, c, pattern->len) == 0)
Willy Tarreau11382812008-07-09 16:18:21 +0200236 return ACL_PAT_PASS;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200237 }
Willy Tarreaua84d3742007-05-07 00:36:48 +0200238 }
Willy Tarreau11382812008-07-09 16:18:21 +0200239 return ACL_PAT_FAIL;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200240}
241
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200242/* Background: Fast way to find a zero byte in a word
243 * http://graphics.stanford.edu/~seander/bithacks.html#ZeroInWord
244 * hasZeroByte = (v - 0x01010101UL) & ~v & 0x80808080UL;
245 *
246 * To look for 4 different byte values, xor the word with those bytes and
247 * then check for zero bytes:
248 *
249 * v = (((unsigned char)c * 0x1010101U) ^ delimiter)
250 * where <delimiter> is the 4 byte values to look for (as an uint)
251 * and <c> is the character that is being tested
252 */
253static inline unsigned int is_delimiter(unsigned char c, unsigned int mask)
254{
255 mask ^= (c * 0x01010101); /* propagate the char to all 4 bytes */
256 return (mask - 0x01010101) & ~mask & 0x80808080U;
257}
258
259static inline unsigned int make_4delim(unsigned char d1, unsigned char d2, unsigned char d3, unsigned char d4)
260{
261 return d1 << 24 | d2 << 16 | d3 << 8 | d4;
262}
263
Willy Tarreaua84d3742007-05-07 00:36:48 +0200264/* This one is used by other real functions. It checks that the pattern is
265 * included inside the tested string, but enclosed between the specified
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200266 * delimiters or at the beginning or end of the string. The delimiters are
267 * provided as an unsigned int made by make_4delim() and match up to 4 different
268 * delimiters. Delimiters are stripped at the beginning and end of the pattern.
Willy Tarreaua84d3742007-05-07 00:36:48 +0200269 */
Willy Tarreau37406352012-04-23 16:16:37 +0200270static int match_word(struct sample *smp, struct acl_pattern *pattern, unsigned int delimiters)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200271{
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200272 int may_match, icase;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200273 char *c, *end;
274 char *ps;
275 int pl;
276
277 pl = pattern->len;
278 ps = pattern->ptr.str;
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200279
280 while (pl > 0 && is_delimiter(*ps, delimiters)) {
Willy Tarreaua84d3742007-05-07 00:36:48 +0200281 pl--;
282 ps++;
283 }
284
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200285 while (pl > 0 && is_delimiter(ps[pl - 1], delimiters))
Willy Tarreaua84d3742007-05-07 00:36:48 +0200286 pl--;
287
Willy Tarreauf853c462012-04-23 18:53:56 +0200288 if (pl > smp->data.str.len)
Willy Tarreau11382812008-07-09 16:18:21 +0200289 return ACL_PAT_FAIL;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200290
291 may_match = 1;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200292 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
Willy Tarreauf853c462012-04-23 18:53:56 +0200293 end = smp->data.str.str + smp->data.str.len - pl;
294 for (c = smp->data.str.str; c <= end; c++) {
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200295 if (is_delimiter(*c, delimiters)) {
Willy Tarreaua84d3742007-05-07 00:36:48 +0200296 may_match = 1;
297 continue;
298 }
Willy Tarreaua84d3742007-05-07 00:36:48 +0200299
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200300 if (!may_match)
301 continue;
302
303 if (icase) {
304 if ((tolower(*c) == tolower(*ps)) &&
305 (strncasecmp(ps, c, pl) == 0) &&
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200306 (c == end || is_delimiter(c[pl], delimiters)))
Willy Tarreau11382812008-07-09 16:18:21 +0200307 return ACL_PAT_PASS;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200308 } else {
309 if ((*c == *ps) &&
310 (strncmp(ps, c, pl) == 0) &&
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200311 (c == end || is_delimiter(c[pl], delimiters)))
Willy Tarreau11382812008-07-09 16:18:21 +0200312 return ACL_PAT_PASS;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200313 }
Willy Tarreaua84d3742007-05-07 00:36:48 +0200314 may_match = 0;
315 }
Willy Tarreau11382812008-07-09 16:18:21 +0200316 return ACL_PAT_FAIL;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200317}
318
319/* Checks that the pattern is included inside the tested string, but enclosed
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200320 * between the delimiters '?' or '/' or at the beginning or end of the string.
321 * Delimiters at the beginning or end of the pattern are ignored.
Willy Tarreaua84d3742007-05-07 00:36:48 +0200322 */
Willy Tarreau37406352012-04-23 16:16:37 +0200323int acl_match_dir(struct sample *smp, struct acl_pattern *pattern)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200324{
Willy Tarreau37406352012-04-23 16:16:37 +0200325 return match_word(smp, pattern, make_4delim('/', '?', '?', '?'));
Willy Tarreaua84d3742007-05-07 00:36:48 +0200326}
327
328/* Checks that the pattern is included inside the tested string, but enclosed
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200329 * between the delmiters '/', '?', '.' or ":" or at the beginning or end of
330 * the string. Delimiters at the beginning or end of the pattern are ignored.
Willy Tarreaua84d3742007-05-07 00:36:48 +0200331 */
Willy Tarreau37406352012-04-23 16:16:37 +0200332int acl_match_dom(struct sample *smp, struct acl_pattern *pattern)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200333{
Willy Tarreau37406352012-04-23 16:16:37 +0200334 return match_word(smp, pattern, make_4delim('/', '?', '.', ':'));
Willy Tarreaua84d3742007-05-07 00:36:48 +0200335}
336
337/* Checks that the integer in <test> is included between min and max */
Willy Tarreau37406352012-04-23 16:16:37 +0200338int acl_match_int(struct sample *smp, struct acl_pattern *pattern)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200339{
Willy Tarreauf853c462012-04-23 18:53:56 +0200340 if ((!pattern->val.range.min_set || pattern->val.range.min <= smp->data.uint) &&
341 (!pattern->val.range.max_set || smp->data.uint <= pattern->val.range.max))
Willy Tarreau11382812008-07-09 16:18:21 +0200342 return ACL_PAT_PASS;
343 return ACL_PAT_FAIL;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200344}
345
Willy Tarreau0e698542011-09-16 08:32:32 +0200346/* Checks that the length of the pattern in <test> is included between min and max */
Willy Tarreau37406352012-04-23 16:16:37 +0200347int acl_match_len(struct sample *smp, struct acl_pattern *pattern)
Willy Tarreau0e698542011-09-16 08:32:32 +0200348{
Willy Tarreauf853c462012-04-23 18:53:56 +0200349 if ((!pattern->val.range.min_set || pattern->val.range.min <= smp->data.str.len) &&
350 (!pattern->val.range.max_set || smp->data.str.len <= pattern->val.range.max))
Willy Tarreau0e698542011-09-16 08:32:32 +0200351 return ACL_PAT_PASS;
352 return ACL_PAT_FAIL;
353}
354
Willy Tarreau37406352012-04-23 16:16:37 +0200355int acl_match_ip(struct sample *smp, struct acl_pattern *pattern)
Willy Tarreaua67fad92007-05-08 19:50:09 +0200356{
Willy Tarreauceb4ac92012-04-28 00:41:46 +0200357 unsigned int v4; /* in network byte order */
358 struct in6_addr *v6;
359 int bits, pos;
360 struct in6_addr tmp6;
Willy Tarreaua67fad92007-05-08 19:50:09 +0200361
Willy Tarreauceb4ac92012-04-28 00:41:46 +0200362 if (pattern->type == SMP_T_IPV4) {
363 if (smp->type == SMP_T_IPV4) {
364 v4 = smp->data.ipv4.s_addr;
365 }
366 else if (smp->type == SMP_T_IPV6) {
367 /* v4 match on a V6 sample. We want to check at least for
368 * the following forms :
369 * - ::ffff:ip:v4 (ipv4 mapped)
370 * - ::0000:ip:v4 (old ipv4 mapped)
371 * - 2002:ip:v4:: (6to4)
372 */
373 if (*(uint32_t*)&smp->data.ipv6.s6_addr[0] == 0 &&
374 *(uint32_t*)&smp->data.ipv6.s6_addr[4] == 0 &&
375 (*(uint32_t*)&smp->data.ipv6.s6_addr[8] == 0 ||
376 *(uint32_t*)&smp->data.ipv6.s6_addr[8] == htonl(0xFFFF))) {
377 v4 = *(uint32_t*)&smp->data.ipv6.s6_addr[12];
378 }
379 else if (*(uint16_t*)&smp->data.ipv6.s6_addr[0] == htons(0x2002)) {
380 v4 = htonl((ntohs(*(uint16_t*)&smp->data.ipv6.s6_addr[2]) << 16) +
381 ntohs(*(uint16_t*)&smp->data.ipv6.s6_addr[4]));
382 }
383 else
384 return ACL_PAT_FAIL;
385 }
386 else
387 return ACL_PAT_FAIL;
Willy Tarreaua67fad92007-05-08 19:50:09 +0200388
Willy Tarreauceb4ac92012-04-28 00:41:46 +0200389 if (((v4 ^ pattern->val.ipv4.addr.s_addr) & pattern->val.ipv4.mask.s_addr) == 0)
390 return ACL_PAT_PASS;
391 else
392 return ACL_PAT_FAIL;
393 }
394 else if (pattern->type == SMP_T_IPV6) {
395 if (smp->type == SMP_T_IPV4) {
396 /* Convert the IPv4 sample address to IPv4 with the
397 * mapping method using the ::ffff: prefix.
398 */
399 memset(&tmp6, 0, 10);
400 *(uint16_t*)&tmp6.s6_addr[10] = htons(0xffff);
401 *(uint32_t*)&tmp6.s6_addr[12] = smp->data.ipv4.s_addr;
402 v6 = &tmp6;
403 }
404 else if (smp->type == SMP_T_IPV6) {
405 v6 = &smp->data.ipv6;
406 }
407 else {
408 return ACL_PAT_FAIL;
409 }
410
411 bits = pattern->val.ipv6.mask;
412 for (pos = 0; bits > 0; pos += 4, bits -= 32) {
413 v4 = *(uint32_t*)&v6->s6_addr[pos] ^ *(uint32_t*)&pattern->val.ipv6.addr.s6_addr[pos];
414 if (bits < 32)
Cyril Bonté4c01beb2012-10-23 21:28:31 +0200415 v4 &= htonl((~0U) << (32-bits));
Willy Tarreauceb4ac92012-04-28 00:41:46 +0200416 if (v4)
417 return ACL_PAT_FAIL;
418 }
Willy Tarreau11382812008-07-09 16:18:21 +0200419 return ACL_PAT_PASS;
Willy Tarreauceb4ac92012-04-28 00:41:46 +0200420 }
Willy Tarreau11382812008-07-09 16:18:21 +0200421 return ACL_PAT_FAIL;
Willy Tarreaua67fad92007-05-08 19:50:09 +0200422}
423
Willy Tarreaub337b532010-05-13 20:03:41 +0200424/* Lookup an IPv4 address in the expression's pattern tree using the longest
425 * match method. The node is returned if it exists, otherwise NULL.
426 */
Willy Tarreau37406352012-04-23 16:16:37 +0200427static void *acl_lookup_ip(struct sample *smp, struct acl_expr *expr)
Willy Tarreaub337b532010-05-13 20:03:41 +0200428{
429 struct in_addr *s;
430
Willy Tarreauf853c462012-04-23 18:53:56 +0200431 if (smp->type != SMP_T_IPV4)
Willy Tarreaub337b532010-05-13 20:03:41 +0200432 return ACL_PAT_FAIL;
433
Willy Tarreauf853c462012-04-23 18:53:56 +0200434 s = &smp->data.ipv4;
Willy Tarreaub337b532010-05-13 20:03:41 +0200435 return ebmb_lookup_longest(&expr->pattern_tree, &s->s_addr);
436}
437
Willy Tarreaua84d3742007-05-07 00:36:48 +0200438/* Parse a string. It is allocated and duplicated. */
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200439int acl_parse_str(const char **text, struct acl_pattern *pattern, int *opaque, char **err)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200440{
441 int len;
442
Willy Tarreauae8b7962007-06-09 23:10:04 +0200443 len = strlen(*text);
Willy Tarreauc92ddbc2012-04-27 22:10:57 +0200444 pattern->type = SMP_T_CSTR;
Willy Tarreauc4262962010-05-10 23:42:40 +0200445
446 if (pattern->flags & ACL_PAT_F_TREE_OK) {
447 /* we're allowed to put the data in a tree whose root is pointed
448 * to by val.tree.
449 */
450 struct ebmb_node *node;
451
452 node = calloc(1, sizeof(*node) + len + 1);
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200453 if (!node) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200454 memprintf(err, "out of memory while loading string pattern");
Willy Tarreauc4262962010-05-10 23:42:40 +0200455 return 0;
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200456 }
Willy Tarreauc4262962010-05-10 23:42:40 +0200457 memcpy(node->key, *text, len + 1);
458 if (ebst_insert(pattern->val.tree, node) != node)
459 free(node); /* was a duplicate */
460 pattern->flags |= ACL_PAT_F_TREE; /* this pattern now contains a tree */
461 return 1;
462 }
463
Willy Tarreauae8b7962007-06-09 23:10:04 +0200464 pattern->ptr.str = strdup(*text);
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200465 if (!pattern->ptr.str) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200466 memprintf(err, "out of memory while loading string pattern");
Willy Tarreaua84d3742007-05-07 00:36:48 +0200467 return 0;
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200468 }
Willy Tarreaua84d3742007-05-07 00:36:48 +0200469 pattern->len = len;
470 return 1;
471}
472
Emeric Brun07ca4962012-10-17 13:38:19 +0200473/* Parse a binary written in hexa. It is allocated. */
474int acl_parse_bin(const char **text, struct acl_pattern *pattern, int *opaque, char **err)
475{
476 int len;
477 const char *p = *text;
478 int i,j;
479
480 len = strlen(p);
481 if (len%2) {
482 memprintf(err, "an even number of hex digit is expected");
483 return 0;
484 }
485
486 pattern->type = SMP_T_CBIN;
487 pattern->len = len >> 1;
488 pattern->ptr.str = malloc(pattern->len);
489 if (!pattern->ptr.str) {
490 memprintf(err, "out of memory while loading string pattern");
491 return 0;
492 }
493
494 i = j = 0;
495 while (j < pattern->len) {
496 if (!ishex(p[i++]))
497 goto bad_input;
498 if (!ishex(p[i++]))
499 goto bad_input;
500 pattern->ptr.str[j++] = (hex2i(p[i-2]) << 4) + hex2i(p[i-1]);
501 }
502 return 1;
503
504bad_input:
505 memprintf(err, "an hex digit is expected (found '%c')", p[i-1]);
506 free(pattern->ptr.str);
507 return 0;
508}
509
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +0100510/* Parse and concatenate all further strings into one. */
511int
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200512acl_parse_strcat(const char **text, struct acl_pattern *pattern, int *opaque, char **err)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +0100513{
514
515 int len = 0, i;
516 char *s;
517
518 for (i = 0; *text[i]; i++)
519 len += strlen(text[i])+1;
520
Willy Tarreauc92ddbc2012-04-27 22:10:57 +0200521 pattern->type = SMP_T_CSTR;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +0100522 pattern->ptr.str = s = calloc(1, len);
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200523 if (!pattern->ptr.str) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200524 memprintf(err, "out of memory while loading pattern");
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +0100525 return 0;
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200526 }
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +0100527
528 for (i = 0; *text[i]; i++)
529 s += sprintf(s, i?" %s":"%s", text[i]);
530
531 pattern->len = len;
532
533 return i;
534}
535
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +0200536/* Free data allocated by acl_parse_reg */
Willy Tarreau37406352012-04-23 16:16:37 +0200537static void acl_free_reg(void *ptr)
538{
Hiroaki Nakamura70351322013-01-13 15:00:42 +0900539 regex_free(ptr);
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +0200540}
541
Willy Tarreauf3d25982007-05-08 22:45:09 +0200542/* Parse a regex. It is allocated. */
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200543int acl_parse_reg(const char **text, struct acl_pattern *pattern, int *opaque, char **err)
Willy Tarreauf3d25982007-05-08 22:45:09 +0200544{
Hiroaki Nakamura70351322013-01-13 15:00:42 +0900545 regex *preg;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200546 int icase;
Hiroaki Nakamurae3cf2222013-04-11 08:17:37 +0200547#ifdef USE_PCRE_JIT
548 const char *error;
549 int erroffset;
550#endif
Willy Tarreauf3d25982007-05-08 22:45:09 +0200551
Hiroaki Nakamura70351322013-01-13 15:00:42 +0900552 preg = calloc(1, sizeof(*preg));
Willy Tarreauf3d25982007-05-08 22:45:09 +0200553
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200554 if (!preg) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200555 memprintf(err, "out of memory while loading pattern");
Willy Tarreauf3d25982007-05-08 22:45:09 +0200556 return 0;
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200557 }
Willy Tarreauf3d25982007-05-08 22:45:09 +0200558
Hiroaki Nakamura70351322013-01-13 15:00:42 +0900559#ifdef USE_PCRE_JIT
560 icase = (pattern->flags & ACL_PAT_F_IGNORE_CASE) ? PCRE_CASELESS : 0;
Hiroaki Nakamurae3cf2222013-04-11 08:17:37 +0200561 preg->reg = pcre_compile(*text, PCRE_NO_AUTO_CAPTURE | icase, &error, &erroffset,
Hiroaki Nakamura70351322013-01-13 15:00:42 +0900562 NULL);
563 if (!preg->reg) {
564 free(preg);
Hiroaki Nakamurae3cf2222013-04-11 08:17:37 +0200565 memprintf(err, "regex '%s' is invalid (error=%s, erroffset=%d)", *text, error, erroffset);
Hiroaki Nakamura70351322013-01-13 15:00:42 +0900566 return 0;
567 }
568
Hiroaki Nakamurae3cf2222013-04-11 08:17:37 +0200569 preg->extra = pcre_study(preg->reg, PCRE_STUDY_JIT_COMPILE, &error);
Hiroaki Nakamura70351322013-01-13 15:00:42 +0900570 if (!preg->extra) {
571 pcre_free(preg->reg);
572 free(preg);
Hiroaki Nakamurae3cf2222013-04-11 08:17:37 +0200573 memprintf(err, "failed to compile regex '%s' (error=%s)", *text, error);
Hiroaki Nakamura70351322013-01-13 15:00:42 +0900574 return 0;
575 }
576#else
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200577 icase = (pattern->flags & ACL_PAT_F_IGNORE_CASE) ? REG_ICASE : 0;
578 if (regcomp(preg, *text, REG_EXTENDED | REG_NOSUB | icase) != 0) {
Willy Tarreauf3d25982007-05-08 22:45:09 +0200579 free(preg);
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200580 memprintf(err, "regex '%s' is invalid", *text);
Willy Tarreauf3d25982007-05-08 22:45:09 +0200581 return 0;
582 }
Hiroaki Nakamura70351322013-01-13 15:00:42 +0900583#endif
Willy Tarreauf3d25982007-05-08 22:45:09 +0200584
585 pattern->ptr.reg = preg;
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +0200586 pattern->freeptrbuf = &acl_free_reg;
Willy Tarreauf3d25982007-05-08 22:45:09 +0200587 return 1;
588}
589
Willy Tarreauae8b7962007-06-09 23:10:04 +0200590/* Parse a range of positive integers delimited by either ':' or '-'. If only
591 * one integer is read, it is set as both min and max. An operator may be
592 * specified as the prefix, among this list of 5 :
593 *
594 * 0:eq, 1:gt, 2:ge, 3:lt, 4:le
595 *
596 * The default operator is "eq". It supports range matching. Ranges are
597 * rejected for other operators. The operator may be changed at any time.
598 * The operator is stored in the 'opaque' argument.
599 *
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200600 * If err is non-NULL, an error message will be returned there on errors and
601 * the caller will have to free it.
602 *
Willy Tarreaua84d3742007-05-07 00:36:48 +0200603 */
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200604int acl_parse_int(const char **text, struct acl_pattern *pattern, int *opaque, char **err)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200605{
Willy Tarreauae8b7962007-06-09 23:10:04 +0200606 signed long long i;
607 unsigned int j, last, skip = 0;
608 const char *ptr = *text;
609
Willy Tarreauc92ddbc2012-04-27 22:10:57 +0200610 pattern->type = SMP_T_UINT;
Willy Tarreau8f8e6452007-06-17 21:51:38 +0200611 while (!isdigit((unsigned char)*ptr)) {
Willy Tarreau1c7cc5b2010-07-18 10:46:33 +0200612 switch (get_std_op(ptr)) {
613 case STD_OP_EQ: *opaque = 0; break;
614 case STD_OP_GT: *opaque = 1; break;
615 case STD_OP_GE: *opaque = 2; break;
616 case STD_OP_LT: *opaque = 3; break;
617 case STD_OP_LE: *opaque = 4; break;
618 default:
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200619 memprintf(err, "'%s' is neither a number nor a supported operator", ptr);
Willy Tarreauae8b7962007-06-09 23:10:04 +0200620 return 0;
Willy Tarreau1c7cc5b2010-07-18 10:46:33 +0200621 }
Willy Tarreauae8b7962007-06-09 23:10:04 +0200622
623 skip++;
624 ptr = text[skip];
625 }
Willy Tarreaua84d3742007-05-07 00:36:48 +0200626
627 last = i = 0;
628 while (1) {
Willy Tarreauae8b7962007-06-09 23:10:04 +0200629 j = *ptr++;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200630 if ((j == '-' || j == ':') && !last) {
631 last++;
632 pattern->val.range.min = i;
633 i = 0;
634 continue;
635 }
636 j -= '0';
637 if (j > 9)
638 // also catches the terminating zero
639 break;
640 i *= 10;
641 i += j;
642 }
Willy Tarreauae8b7962007-06-09 23:10:04 +0200643
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200644 if (last && *opaque >= 1 && *opaque <= 4) {
Willy Tarreauae8b7962007-06-09 23:10:04 +0200645 /* having a range with a min or a max is absurd */
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200646 memprintf(err, "integer range '%s' specified with a comparison operator", text[skip]);
Willy Tarreauae8b7962007-06-09 23:10:04 +0200647 return 0;
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200648 }
Willy Tarreauae8b7962007-06-09 23:10:04 +0200649
Willy Tarreaua84d3742007-05-07 00:36:48 +0200650 if (!last)
651 pattern->val.range.min = i;
652 pattern->val.range.max = i;
Willy Tarreauae8b7962007-06-09 23:10:04 +0200653
654 switch (*opaque) {
655 case 0: /* eq */
656 pattern->val.range.min_set = 1;
657 pattern->val.range.max_set = 1;
658 break;
659 case 1: /* gt */
660 pattern->val.range.min++; /* gt = ge + 1 */
661 case 2: /* ge */
662 pattern->val.range.min_set = 1;
663 pattern->val.range.max_set = 0;
664 break;
665 case 3: /* lt */
666 pattern->val.range.max--; /* lt = le - 1 */
667 case 4: /* le */
668 pattern->val.range.min_set = 0;
669 pattern->val.range.max_set = 1;
670 break;
671 }
672 return skip + 1;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200673}
674
Willy Tarreau4a26d2f2008-07-15 16:05:33 +0200675/* Parse a range of positive 2-component versions delimited by either ':' or
676 * '-'. The version consists in a major and a minor, both of which must be
677 * smaller than 65536, because internally they will be represented as a 32-bit
678 * integer.
679 * If only one version is read, it is set as both min and max. Just like for
680 * pure integers, an operator may be specified as the prefix, among this list
681 * of 5 :
682 *
683 * 0:eq, 1:gt, 2:ge, 3:lt, 4:le
684 *
685 * The default operator is "eq". It supports range matching. Ranges are
686 * rejected for other operators. The operator may be changed at any time.
687 * The operator is stored in the 'opaque' argument. This allows constructs
688 * such as the following one :
689 *
690 * acl obsolete_ssl ssl_req_proto lt 3
691 * acl unsupported_ssl ssl_req_proto gt 3.1
692 * acl valid_ssl ssl_req_proto 3.0-3.1
693 *
694 */
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200695int acl_parse_dotted_ver(const char **text, struct acl_pattern *pattern, int *opaque, char **err)
Willy Tarreau4a26d2f2008-07-15 16:05:33 +0200696{
697 signed long long i;
698 unsigned int j, last, skip = 0;
699 const char *ptr = *text;
700
701
702 while (!isdigit((unsigned char)*ptr)) {
Willy Tarreau1c7cc5b2010-07-18 10:46:33 +0200703 switch (get_std_op(ptr)) {
704 case STD_OP_EQ: *opaque = 0; break;
705 case STD_OP_GT: *opaque = 1; break;
706 case STD_OP_GE: *opaque = 2; break;
707 case STD_OP_LT: *opaque = 3; break;
708 case STD_OP_LE: *opaque = 4; break;
709 default:
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200710 memprintf(err, "'%s' is neither a number nor a supported operator", ptr);
Willy Tarreau4a26d2f2008-07-15 16:05:33 +0200711 return 0;
Willy Tarreau1c7cc5b2010-07-18 10:46:33 +0200712 }
Willy Tarreau4a26d2f2008-07-15 16:05:33 +0200713
714 skip++;
715 ptr = text[skip];
716 }
717
718 last = i = 0;
719 while (1) {
720 j = *ptr++;
721 if (j == '.') {
722 /* minor part */
723 if (i >= 65536)
724 return 0;
725 i <<= 16;
726 continue;
727 }
728 if ((j == '-' || j == ':') && !last) {
729 last++;
730 if (i < 65536)
731 i <<= 16;
732 pattern->val.range.min = i;
733 i = 0;
734 continue;
735 }
736 j -= '0';
737 if (j > 9)
738 // also catches the terminating zero
739 break;
740 i = (i & 0xFFFF0000) + (i & 0xFFFF) * 10;
741 i += j;
742 }
743
744 /* if we only got a major version, let's shift it now */
745 if (i < 65536)
746 i <<= 16;
747
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200748 if (last && *opaque >= 1 && *opaque <= 4) {
Willy Tarreau4a26d2f2008-07-15 16:05:33 +0200749 /* having a range with a min or a max is absurd */
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200750 memprintf(err, "version range '%s' specified with a comparison operator", text[skip]);
Willy Tarreau4a26d2f2008-07-15 16:05:33 +0200751 return 0;
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200752 }
Willy Tarreau4a26d2f2008-07-15 16:05:33 +0200753
754 if (!last)
755 pattern->val.range.min = i;
756 pattern->val.range.max = i;
757
758 switch (*opaque) {
759 case 0: /* eq */
760 pattern->val.range.min_set = 1;
761 pattern->val.range.max_set = 1;
762 break;
763 case 1: /* gt */
764 pattern->val.range.min++; /* gt = ge + 1 */
765 case 2: /* ge */
766 pattern->val.range.min_set = 1;
767 pattern->val.range.max_set = 0;
768 break;
769 case 3: /* lt */
770 pattern->val.range.max--; /* lt = le - 1 */
771 case 4: /* le */
772 pattern->val.range.min_set = 0;
773 pattern->val.range.max_set = 1;
774 break;
775 }
776 return skip + 1;
777}
778
Willy Tarreaua67fad92007-05-08 19:50:09 +0200779/* Parse an IP address and an optional mask in the form addr[/mask].
780 * The addr may either be an IPv4 address or a hostname. The mask
781 * may either be a dotted mask or a number of bits. Returns 1 if OK,
Willy Tarreauc92ddbc2012-04-27 22:10:57 +0200782 * otherwise 0. NOTE: IP address patterns are typed (IPV4/IPV6).
Willy Tarreaua67fad92007-05-08 19:50:09 +0200783 */
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200784int acl_parse_ip(const char **text, struct acl_pattern *pattern, int *opaque, char **err)
Willy Tarreaua67fad92007-05-08 19:50:09 +0200785{
Willy Tarreaub337b532010-05-13 20:03:41 +0200786 struct eb_root *tree = NULL;
787 if (pattern->flags & ACL_PAT_F_TREE_OK)
788 tree = pattern->val.tree;
789
790 if (str2net(*text, &pattern->val.ipv4.addr, &pattern->val.ipv4.mask)) {
791 unsigned int mask = ntohl(pattern->val.ipv4.mask.s_addr);
792 struct ebmb_node *node;
793 /* check if the mask is contiguous so that we can insert the
794 * network into the tree. A continuous mask has only ones on
795 * the left. This means that this mask + its lower bit added
796 * once again is null.
797 */
Willy Tarreauceb4ac92012-04-28 00:41:46 +0200798 pattern->type = SMP_T_IPV4;
Willy Tarreaub337b532010-05-13 20:03:41 +0200799 if (mask + (mask & -mask) == 0 && tree) {
800 mask = mask ? 33 - flsnz(mask & -mask) : 0; /* equals cidr value */
801 /* FIXME: insert <addr>/<mask> into the tree here */
802 node = calloc(1, sizeof(*node) + 4); /* reserve 4 bytes for IPv4 address */
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200803 if (!node) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200804 memprintf(err, "out of memory while loading IPv4 pattern");
Willy Tarreaub337b532010-05-13 20:03:41 +0200805 return 0;
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200806 }
Willy Tarreaub337b532010-05-13 20:03:41 +0200807 memcpy(node->key, &pattern->val.ipv4.addr, 4); /* network byte order */
808 node->node.pfx = mask;
809 if (ebmb_insert_prefix(tree, node, 4) != node)
810 free(node); /* was a duplicate */
811 pattern->flags |= ACL_PAT_F_TREE;
812 return 1;
813 }
Willy Tarreauae8b7962007-06-09 23:10:04 +0200814 return 1;
Willy Tarreaub337b532010-05-13 20:03:41 +0200815 }
Willy Tarreauceb4ac92012-04-28 00:41:46 +0200816 else if (str62net(*text, &pattern->val.ipv6.addr, &pattern->val.ipv6.mask)) {
817 /* no tree support right now */
818 pattern->type = SMP_T_IPV6;
819 return 1;
820 }
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200821 else {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200822 memprintf(err, "'%s' is not a valid IPv4 or IPv6 address", *text);
Willy Tarreauae8b7962007-06-09 23:10:04 +0200823 return 0;
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200824 }
Willy Tarreaua67fad92007-05-08 19:50:09 +0200825}
826
Willy Tarreaua84d3742007-05-07 00:36:48 +0200827/*
828 * Registers the ACL keyword list <kwl> as a list of valid keywords for next
829 * parsing sessions.
830 */
831void acl_register_keywords(struct acl_kw_list *kwl)
832{
833 LIST_ADDQ(&acl_keywords.list, &kwl->list);
834}
835
836/*
837 * Unregisters the ACL keyword list <kwl> from the list of valid keywords.
838 */
839void acl_unregister_keywords(struct acl_kw_list *kwl)
840{
841 LIST_DEL(&kwl->list);
842 LIST_INIT(&kwl->list);
843}
844
845/* Return a pointer to the ACL <name> within the list starting at <head>, or
846 * NULL if not found.
847 */
848struct acl *find_acl_by_name(const char *name, struct list *head)
849{
850 struct acl *acl;
851 list_for_each_entry(acl, head, list) {
852 if (strcmp(acl->name, name) == 0)
853 return acl;
854 }
855 return NULL;
856}
857
858/* Return a pointer to the ACL keyword <kw>, or NULL if not found. Note that if
859 * <kw> contains an opening parenthesis, only the left part of it is checked.
860 */
861struct acl_keyword *find_acl_kw(const char *kw)
862{
863 int index;
864 const char *kwend;
865 struct acl_kw_list *kwl;
866
867 kwend = strchr(kw, '(');
868 if (!kwend)
869 kwend = kw + strlen(kw);
870
871 list_for_each_entry(kwl, &acl_keywords.list, list) {
872 for (index = 0; kwl->kw[index].kw != NULL; index++) {
873 if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) &&
874 kwl->kw[index].kw[kwend-kw] == 0)
875 return &kwl->kw[index];
876 }
877 }
878 return NULL;
879}
880
Willy Tarreaudfd7fca2011-03-09 07:27:02 +0100881/* NB: does nothing if <pat> is NULL */
Willy Tarreaua84d3742007-05-07 00:36:48 +0200882static void free_pattern(struct acl_pattern *pat)
883{
Willy Tarreaudfd7fca2011-03-09 07:27:02 +0100884 if (!pat)
885 return;
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +0200886
887 if (pat->ptr.ptr) {
888 if (pat->freeptrbuf)
889 pat->freeptrbuf(pat->ptr.ptr);
890
Willy Tarreaua84d3742007-05-07 00:36:48 +0200891 free(pat->ptr.ptr);
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +0200892 }
893
Willy Tarreaua84d3742007-05-07 00:36:48 +0200894 free(pat);
895}
896
897static void free_pattern_list(struct list *head)
898{
899 struct acl_pattern *pat, *tmp;
900 list_for_each_entry_safe(pat, tmp, head, list)
901 free_pattern(pat);
902}
903
Willy Tarreaue56cda92010-05-11 23:25:05 +0200904static void free_pattern_tree(struct eb_root *root)
905{
906 struct eb_node *node, *next;
907 node = eb_first(root);
908 while (node) {
909 next = eb_next(node);
910 free(node);
911 node = next;
912 }
913}
914
Willy Tarreaua84d3742007-05-07 00:36:48 +0200915static struct acl_expr *prune_acl_expr(struct acl_expr *expr)
916{
Willy Tarreau34db1082012-04-19 17:16:54 +0200917 struct arg *arg;
918
Willy Tarreaua84d3742007-05-07 00:36:48 +0200919 free_pattern_list(&expr->patterns);
Willy Tarreaue56cda92010-05-11 23:25:05 +0200920 free_pattern_tree(&expr->pattern_tree);
Willy Tarreaua84d3742007-05-07 00:36:48 +0200921 LIST_INIT(&expr->patterns);
Willy Tarreau34db1082012-04-19 17:16:54 +0200922
923 for (arg = expr->args; arg; arg++) {
924 if (arg->type == ARGT_STOP)
925 break;
Willy Tarreau496aa012012-06-01 10:38:29 +0200926 if (arg->type == ARGT_STR || arg->unresolved) {
Willy Tarreau34db1082012-04-19 17:16:54 +0200927 free(arg->data.str.str);
928 arg->data.str.str = NULL;
Willy Tarreau496aa012012-06-01 10:38:29 +0200929 arg->unresolved = 0;
Willy Tarreau34db1082012-04-19 17:16:54 +0200930 }
Willy Tarreau34db1082012-04-19 17:16:54 +0200931 }
932
Willy Tarreau2e845be2012-10-19 19:49:09 +0200933 if (expr->args != empty_arg_list)
934 free(expr->args);
Willy Tarreaua84d3742007-05-07 00:36:48 +0200935 return expr;
936}
937
Willy Tarreau08ad0b32012-04-27 17:25:24 +0200938
939/* Reads patterns from a file. If <err_msg> is non-NULL, an error message will
940 * be returned there on errors and the caller will have to free it.
941 */
Willy Tarreaud76a98a2013-03-31 18:34:33 +0200942static int acl_read_patterns_from_file(struct acl_expr *expr,
943 const char *filename, int patflags,
944 char **err)
Willy Tarreau2b5285d2010-05-09 23:45:24 +0200945{
946 FILE *file;
947 char *c;
948 const char *args[2];
949 struct acl_pattern *pattern;
950 int opaque;
Willy Tarreau6a8097f2011-02-26 15:14:15 +0100951 int ret = 0;
Willy Tarreau08ad0b32012-04-27 17:25:24 +0200952 int line = 0;
Willy Tarreau2b5285d2010-05-09 23:45:24 +0200953
954 file = fopen(filename, "r");
Willy Tarreau08ad0b32012-04-27 17:25:24 +0200955 if (!file) {
956 memprintf(err, "failed to open pattern file <%s>", filename);
Willy Tarreau2b5285d2010-05-09 23:45:24 +0200957 return 0;
Willy Tarreau08ad0b32012-04-27 17:25:24 +0200958 }
Willy Tarreau2b5285d2010-05-09 23:45:24 +0200959
960 /* now parse all patterns. The file may contain only one pattern per
961 * line. If the line contains spaces, they will be part of the pattern.
962 * The pattern stops at the first CR, LF or EOF encountered.
963 */
964 opaque = 0;
Willy Tarreaue56cda92010-05-11 23:25:05 +0200965 pattern = NULL;
Willy Tarreau2b5285d2010-05-09 23:45:24 +0200966 args[1] = "";
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100967 while (fgets(trash.str, trash.size, file) != NULL) {
Willy Tarreau08ad0b32012-04-27 17:25:24 +0200968 line++;
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100969 c = trash.str;
Willy Tarreau58215a02010-05-13 22:07:43 +0200970
971 /* ignore lines beginning with a dash */
972 if (*c == '#')
973 continue;
974
975 /* strip leading spaces and tabs */
976 while (*c == ' ' || *c == '\t')
977 c++;
978
Willy Tarreau58215a02010-05-13 22:07:43 +0200979
980 args[0] = c;
Willy Tarreau2b5285d2010-05-09 23:45:24 +0200981 while (*c && *c != '\n' && *c != '\r')
982 c++;
983 *c = 0;
984
Willy Tarreau51091962011-01-03 21:04:10 +0100985 /* empty lines are ignored too */
986 if (c == args[0])
987 continue;
988
Willy Tarreaue56cda92010-05-11 23:25:05 +0200989 /* we keep the previous pattern along iterations as long as it's not used */
990 if (!pattern)
991 pattern = (struct acl_pattern *)malloc(sizeof(*pattern));
Willy Tarreau08ad0b32012-04-27 17:25:24 +0200992 if (!pattern) {
993 memprintf(err, "out of memory when loading patterns from file <%s>", filename);
Willy Tarreau2b5285d2010-05-09 23:45:24 +0200994 goto out_close;
Willy Tarreau08ad0b32012-04-27 17:25:24 +0200995 }
Willy Tarreaue56cda92010-05-11 23:25:05 +0200996
997 memset(pattern, 0, sizeof(*pattern));
Willy Tarreau2b5285d2010-05-09 23:45:24 +0200998 pattern->flags = patflags;
999
Willy Tarreaue0db1e82013-01-04 16:31:47 +01001000 if (!(pattern->flags & ACL_PAT_F_IGNORE_CASE) &&
Willy Tarreaud76a98a2013-03-31 18:34:33 +02001001 (expr->match == acl_match_str || expr->match == acl_match_ip)) {
Willy Tarreaue56cda92010-05-11 23:25:05 +02001002 /* we pre-set the data pointer to the tree's head so that functions
1003 * which are able to insert in a tree know where to do that.
1004 */
1005 pattern->flags |= ACL_PAT_F_TREE_OK;
1006 pattern->val.tree = &expr->pattern_tree;
1007 }
1008
Willy Tarreauc92ddbc2012-04-27 22:10:57 +02001009 pattern->type = SMP_TYPES; /* unspecified type by default */
Willy Tarreaud76a98a2013-03-31 18:34:33 +02001010 if (!expr->parse(args, pattern, &opaque, err))
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001011 goto out_free_pattern;
Willy Tarreaue56cda92010-05-11 23:25:05 +02001012
1013 /* if the parser did not feed the tree, let's chain the pattern to the list */
1014 if (!(pattern->flags & ACL_PAT_F_TREE)) {
1015 LIST_ADDQ(&expr->patterns, &pattern->list);
1016 pattern = NULL; /* get a new one */
1017 }
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001018 }
Willy Tarreau6a8097f2011-02-26 15:14:15 +01001019
1020 ret = 1; /* success */
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001021
1022 out_free_pattern:
1023 free_pattern(pattern);
1024 out_close:
1025 fclose(file);
Willy Tarreau6a8097f2011-02-26 15:14:15 +01001026 return ret;
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001027}
1028
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001029/* Parse an ACL expression starting at <args>[0], and return it. If <err> is
1030 * not NULL, it will be filled with a pointer to an error message in case of
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001031 * error. This pointer must be freeable or NULL. <al> is an arg_list serving
1032 * as a list head to report missing dependencies.
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001033 *
Willy Tarreaua84d3742007-05-07 00:36:48 +02001034 * Right now, the only accepted syntax is :
1035 * <subject> [<value>...]
1036 */
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001037struct acl_expr *parse_acl_expr(const char **args, char **err, struct arg_list *al)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001038{
1039 __label__ out_return, out_free_expr, out_free_pattern;
1040 struct acl_expr *expr;
1041 struct acl_keyword *aclkw;
1042 struct acl_pattern *pattern;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02001043 int opaque, patflags;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001044 const char *arg;
Willy Tarreaubef91e72013-03-31 23:14:46 +02001045 struct sample_fetch *smp = NULL;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001046
Willy Tarreaubef91e72013-03-31 23:14:46 +02001047 /* First, we lookd for an ACL keyword. And if we don't find one, then
1048 * we look for a sample fetch keyword.
1049 */
Willy Tarreaua84d3742007-05-07 00:36:48 +02001050 aclkw = find_acl_kw(args[0]);
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001051 if (!aclkw || !aclkw->parse) {
Willy Tarreaubef91e72013-03-31 23:14:46 +02001052 const char *kwend;
1053
1054 kwend = strchr(args[0], '(');
1055 if (!kwend)
1056 kwend = args[0] + strlen(args[0]);
1057 smp = find_sample_fetch(args[0], kwend - args[0]);
1058
1059 if (!smp) {
1060 memprintf(err, "unknown ACL or sample keyword '%s'", *args);
1061 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
Willy Tarreaubef91e72013-03-31 23:14:46 +02001071 expr->kw = aclkw ? aclkw->kw : smp->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;
Willy Tarreau2e845be2012-10-19 19:49:09 +02001076 expr->args = empty_arg_list;
Willy Tarreaubef91e72013-03-31 23:14:46 +02001077 expr->smp = aclkw ? aclkw->smp : smp;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001078
Willy Tarreau9987ea92013-06-11 21:09:06 +02001079 if (!expr->parse) {
1080 /* some types can be automatically converted */
1081
1082 switch (expr->smp->out_type) {
1083 case SMP_T_BOOL:
1084 expr->parse = acl_parse_fcts[ACL_MATCH_BOOL];
1085 expr->match = acl_match_fcts[ACL_MATCH_BOOL];
1086 break;
1087 case SMP_T_SINT:
1088 case SMP_T_UINT:
1089 expr->parse = acl_parse_fcts[ACL_MATCH_INT];
1090 expr->match = acl_match_fcts[ACL_MATCH_INT];
1091 break;
1092 case SMP_T_IPV4:
1093 case SMP_T_IPV6:
1094 expr->parse = acl_parse_fcts[ACL_MATCH_IP];
1095 expr->match = acl_match_fcts[ACL_MATCH_IP];
1096 break;
1097 }
1098 }
1099
Willy Tarreaua84d3742007-05-07 00:36:48 +02001100 arg = strchr(args[0], '(');
Willy Tarreaud76a98a2013-03-31 18:34:33 +02001101 if (expr->smp->arg_mask) {
Willy Tarreau61612d42012-04-19 18:42:05 +02001102 int nbargs = 0;
Willy Tarreau34db1082012-04-19 17:16:54 +02001103 char *end;
Willy Tarreau34db1082012-04-19 17:16:54 +02001104
Willy Tarreau61612d42012-04-19 18:42:05 +02001105 if (arg != NULL) {
1106 /* there are 0 or more arguments in the form "subject(arg[,arg]*)" */
1107 arg++;
1108 end = strchr(arg, ')');
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001109 if (!end) {
Willy Tarreau93fddf12013-03-31 22:59:32 +02001110 memprintf(err, "missing closing ')' after arguments to ACL keyword '%s'", expr->kw);
Willy Tarreau61612d42012-04-19 18:42:05 +02001111 goto out_free_expr;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001112 }
Willy Tarreau34db1082012-04-19 17:16:54 +02001113
Willy Tarreau61612d42012-04-19 18:42:05 +02001114 /* Parse the arguments. Note that currently we have no way to
1115 * report parsing errors, hence the NULL in the error pointers.
1116 * An error is also reported if some mandatory arguments are
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001117 * missing. We prepare the args list to report unresolved
1118 * dependencies.
Willy Tarreau61612d42012-04-19 18:42:05 +02001119 */
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001120 al->ctx = ARGC_ACL;
1121 al->kw = expr->kw;
1122 al->conv = NULL;
Willy Tarreaud76a98a2013-03-31 18:34:33 +02001123 nbargs = make_arg_list(arg, end - arg, expr->smp->arg_mask, &expr->args,
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001124 err, NULL, NULL, al);
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001125 if (nbargs < 0) {
1126 /* note that make_arg_list will have set <err> here */
Willy Tarreau93fddf12013-03-31 22:59:32 +02001127 memprintf(err, "in argument to '%s', %s", expr->kw, *err);
Willy Tarreau61612d42012-04-19 18:42:05 +02001128 goto out_free_expr;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001129 }
Willy Tarreauae52f062012-04-26 12:13:35 +02001130
Willy Tarreau2e845be2012-10-19 19:49:09 +02001131 if (!expr->args)
1132 expr->args = empty_arg_list;
1133
Willy Tarreaud76a98a2013-03-31 18:34:33 +02001134 if (expr->smp->val_args && !expr->smp->val_args(expr->args, err)) {
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001135 /* invalid keyword argument, error must have been
1136 * set by val_args().
1137 */
Willy Tarreau93fddf12013-03-31 22:59:32 +02001138 memprintf(err, "in argument to '%s', %s", expr->kw, *err);
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001139 goto out_free_expr;
1140 }
Willy Tarreau61612d42012-04-19 18:42:05 +02001141 }
Willy Tarreaud76a98a2013-03-31 18:34:33 +02001142 else if (ARGM(expr->smp->arg_mask) == 1) {
1143 int type = (expr->smp->arg_mask >> 4) & 15;
Willy Tarreaufc2c1fd2012-04-19 23:35:54 +02001144
1145 /* If a proxy is noted as a mandatory argument, we'll fake
1146 * an empty one so that acl_find_targets() resolves it as
1147 * the current one later.
1148 */
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001149 if (type != ARGT_FE && type != ARGT_BE && type != ARGT_TAB) {
Willy Tarreau93fddf12013-03-31 22:59:32 +02001150 memprintf(err, "ACL keyword '%s' expects %d arguments", expr->kw, ARGM(expr->smp->arg_mask));
Willy Tarreaufc2c1fd2012-04-19 23:35:54 +02001151 goto out_free_expr;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001152 }
Willy Tarreaufc2c1fd2012-04-19 23:35:54 +02001153
1154 /* Build an arg list containing the type as an empty string
1155 * and the usual STOP.
1156 */
1157 expr->args = calloc(2, sizeof(*expr->args));
1158 expr->args[0].type = type;
Willy Tarreaue3a46112012-06-15 08:02:34 +02001159 expr->args[0].unresolved = 1;
Willy Tarreaufc2c1fd2012-04-19 23:35:54 +02001160 expr->args[0].data.str.str = strdup("");
1161 expr->args[0].data.str.len = 1;
1162 expr->args[0].data.str.len = 0;
Willy Tarreauf75d0082013-04-07 21:20:44 +02001163 arg_list_add(al, &expr->args[0], 0);
1164
Willy Tarreaufc2c1fd2012-04-19 23:35:54 +02001165 expr->args[1].type = ARGT_STOP;
1166 }
Willy Tarreaud76a98a2013-03-31 18:34:33 +02001167 else if (ARGM(expr->smp->arg_mask)) {
Willy Tarreau61612d42012-04-19 18:42:05 +02001168 /* there were some mandatory arguments */
Willy Tarreau93fddf12013-03-31 22:59:32 +02001169 memprintf(err, "ACL keyword '%s' expects %d arguments", expr->kw, ARGM(expr->smp->arg_mask));
Willy Tarreaua84d3742007-05-07 00:36:48 +02001170 goto out_free_expr;
Willy Tarreau61612d42012-04-19 18:42:05 +02001171 }
1172 }
1173 else {
1174 if (arg) {
1175 /* no argument expected */
Willy Tarreau93fddf12013-03-31 22:59:32 +02001176 memprintf(err, "ACL keyword '%s' takes no argument", expr->kw);
Willy Tarreau61612d42012-04-19 18:42:05 +02001177 goto out_free_expr;
1178 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001179 }
1180
Willy Tarreaua84d3742007-05-07 00:36:48 +02001181 args++;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02001182
1183 /* check for options before patterns. Supported options are :
1184 * -i : ignore case for all patterns by default
1185 * -f : read patterns from those files
Willy Tarreau5adeda12013-03-31 22:13:34 +02001186 * -m : force matching method (must be used before -f)
Willy Tarreauc8d7c962007-06-17 08:20:33 +02001187 * -- : everything after this is not an option
1188 */
1189 patflags = 0;
1190 while (**args == '-') {
1191 if ((*args)[1] == 'i')
1192 patflags |= ACL_PAT_F_IGNORE_CASE;
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001193 else if ((*args)[1] == 'f') {
Willy Tarreaubef91e72013-03-31 23:14:46 +02001194 if (!expr->parse) {
Willy Tarreau9987ea92013-06-11 21:09:06 +02001195 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 +02001196 goto out_free_expr;
1197 }
1198
Willy Tarreaud76a98a2013-03-31 18:34:33 +02001199 if (!acl_read_patterns_from_file(expr, args[1], patflags | ACL_PAT_F_FROM_FILE, err))
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001200 goto out_free_expr;
Willy Tarreau5adeda12013-03-31 22:13:34 +02001201 args++;
1202 }
1203 else if ((*args)[1] == 'm') {
1204 int idx;
1205
1206 if (!LIST_ISEMPTY(&expr->patterns) || !eb_is_empty(&expr->pattern_tree)) {
1207 memprintf(err, "'-m' must only be specified before patterns and files in parsing ACL expression");
1208 goto out_free_expr;
1209 }
1210
1211 idx = acl_find_match_name(args[1]);
1212 if (idx < 0) {
1213 memprintf(err, "unknown matching method '%s' when parsing ACL expression", args[1]);
1214 goto out_free_expr;
1215 }
1216
1217 /* Note: -m found is always valid, bool/int are compatible, str/bin/reg/len are compatible */
1218 if (idx == ACL_MATCH_FOUND || /* -m found */
1219 ((idx == ACL_MATCH_BOOL || idx == ACL_MATCH_INT) && /* -m bool/int */
1220 (expr->smp->out_type == SMP_T_BOOL ||
1221 expr->smp->out_type == SMP_T_UINT ||
1222 expr->smp->out_type == SMP_T_SINT)) ||
1223 (idx == ACL_MATCH_IP && /* -m ip */
1224 (expr->smp->out_type == SMP_T_IPV4 ||
1225 expr->smp->out_type == SMP_T_IPV6)) ||
1226 ((idx == ACL_MATCH_BIN || idx == ACL_MATCH_LEN || idx == ACL_MATCH_STR ||
1227 idx == ACL_MATCH_BEG || idx == ACL_MATCH_SUB || idx == ACL_MATCH_DIR ||
1228 idx == ACL_MATCH_DOM || idx == ACL_MATCH_END || idx == ACL_MATCH_REG) && /* strings */
1229 (expr->smp->out_type == SMP_T_STR ||
1230 expr->smp->out_type == SMP_T_BIN ||
1231 expr->smp->out_type == SMP_T_CSTR ||
1232 expr->smp->out_type == SMP_T_CBIN))) {
1233 expr->parse = acl_parse_fcts[idx];
1234 expr->match = acl_match_fcts[idx];
1235 }
1236 else {
Willy Tarreau93fddf12013-03-31 22:59:32 +02001237 memprintf(err, "matching method '%s' cannot be used with fetch keyword '%s'", args[1], expr->kw);
Willy Tarreau5adeda12013-03-31 22:13:34 +02001238 goto out_free_expr;
1239 }
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001240 args++;
1241 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +02001242 else if ((*args)[1] == '-') {
1243 args++;
1244 break;
1245 }
1246 else
1247 break;
1248 args++;
1249 }
1250
Willy Tarreaubef91e72013-03-31 23:14:46 +02001251 if (!expr->parse) {
Willy Tarreau9987ea92013-06-11 21:09:06 +02001252 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 +02001253 goto out_free_expr;
1254 }
1255
Willy Tarreauc8d7c962007-06-17 08:20:33 +02001256 /* now parse all patterns */
Willy Tarreauae8b7962007-06-09 23:10:04 +02001257 opaque = 0;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001258 while (**args) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02001259 int ret;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001260 pattern = (struct acl_pattern *)calloc(1, sizeof(*pattern));
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001261 if (!pattern) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001262 memprintf(err, "out of memory when parsing ACL pattern");
Willy Tarreaua84d3742007-05-07 00:36:48 +02001263 goto out_free_expr;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001264 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +02001265 pattern->flags = patflags;
1266
Willy Tarreauc92ddbc2012-04-27 22:10:57 +02001267 pattern->type = SMP_TYPES; /* unspecified type */
Willy Tarreaud76a98a2013-03-31 18:34:33 +02001268 ret = expr->parse(args, pattern, &opaque, err);
Willy Tarreau7dcb6482012-04-27 17:52:25 +02001269 if (!ret)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001270 goto out_free_pattern;
Willy Tarreau7dcb6482012-04-27 17:52:25 +02001271
Willy Tarreaua84d3742007-05-07 00:36:48 +02001272 LIST_ADDQ(&expr->patterns, &pattern->list);
Willy Tarreauae8b7962007-06-09 23:10:04 +02001273 args += ret;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001274 }
1275
1276 return expr;
1277
1278 out_free_pattern:
1279 free_pattern(pattern);
1280 out_free_expr:
1281 prune_acl_expr(expr);
1282 free(expr);
1283 out_return:
1284 return NULL;
1285}
1286
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02001287/* Purge everything in the acl <acl>, then return <acl>. */
1288struct acl *prune_acl(struct acl *acl) {
1289
1290 struct acl_expr *expr, *exprb;
1291
1292 free(acl->name);
1293
1294 list_for_each_entry_safe(expr, exprb, &acl->expr, list) {
1295 LIST_DEL(&expr->list);
1296 prune_acl_expr(expr);
1297 free(expr);
1298 }
1299
1300 return acl;
1301}
1302
Willy Tarreaua84d3742007-05-07 00:36:48 +02001303/* Parse an ACL with the name starting at <args>[0], and with a list of already
1304 * known ACLs in <acl>. If the ACL was not in the list, it will be added.
Willy Tarreau2a56c5e2010-03-15 16:13:29 +01001305 * A pointer to that ACL is returned. If the ACL has an empty name, then it's
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001306 * an anonymous one and it won't be merged with any other one. If <err> is not
1307 * NULL, it will be filled with an appropriate error. This pointer must be
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001308 * freeable or NULL. <al> is the arg_list serving as a head for unresolved
1309 * dependencies.
Willy Tarreaua84d3742007-05-07 00:36:48 +02001310 *
1311 * args syntax: <aclname> <acl_expr>
1312 */
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001313struct acl *parse_acl(const char **args, struct list *known_acl, char **err, struct arg_list *al)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001314{
1315 __label__ out_return, out_free_acl_expr, out_free_name;
1316 struct acl *cur_acl;
1317 struct acl_expr *acl_expr;
1318 char *name;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001319 const char *pos;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001320
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001321 if (**args && (pos = invalid_char(*args))) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001322 memprintf(err, "invalid character in ACL name : '%c'", *pos);
Willy Tarreau2e74c3f2007-12-02 18:45:09 +01001323 goto out_return;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001324 }
Willy Tarreau2e74c3f2007-12-02 18:45:09 +01001325
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001326 acl_expr = parse_acl_expr(args + 1, err, al);
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001327 if (!acl_expr) {
1328 /* parse_acl_expr will have filled <err> here */
Willy Tarreaua84d3742007-05-07 00:36:48 +02001329 goto out_return;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001330 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001331
Willy Tarreau404e8ab2009-07-26 19:40:40 +02001332 /* Check for args beginning with an opening parenthesis just after the
1333 * subject, as this is almost certainly a typo. Right now we can only
1334 * emit a warning, so let's do so.
1335 */
Krzysztof Piotr Oledzki4cdd8312009-10-05 00:23:35 +02001336 if (!strchr(args[1], '(') && *args[2] == '(')
Willy Tarreau404e8ab2009-07-26 19:40:40 +02001337 Warning("parsing acl '%s' :\n"
1338 " matching '%s' for pattern '%s' is likely a mistake and probably\n"
1339 " not what you want. Maybe you need to remove the extraneous space before '('.\n"
1340 " If you are really sure this is not an error, please insert '--' between the\n"
1341 " match and the pattern to make this warning message disappear.\n",
1342 args[0], args[1], args[2]);
1343
Willy Tarreau2a56c5e2010-03-15 16:13:29 +01001344 if (*args[0])
1345 cur_acl = find_acl_by_name(args[0], known_acl);
1346 else
1347 cur_acl = NULL;
1348
Willy Tarreaua84d3742007-05-07 00:36:48 +02001349 if (!cur_acl) {
1350 name = strdup(args[0]);
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001351 if (!name) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001352 memprintf(err, "out of memory when parsing ACL");
Willy Tarreaua84d3742007-05-07 00:36:48 +02001353 goto out_free_acl_expr;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001354 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001355 cur_acl = (struct acl *)calloc(1, sizeof(*cur_acl));
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001356 if (cur_acl == NULL) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001357 memprintf(err, "out of memory when parsing ACL");
Willy Tarreaua84d3742007-05-07 00:36:48 +02001358 goto out_free_name;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001359 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001360
1361 LIST_INIT(&cur_acl->expr);
1362 LIST_ADDQ(known_acl, &cur_acl->list);
1363 cur_acl->name = name;
1364 }
1365
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001366 /* We want to know what features the ACL needs (typically HTTP parsing),
1367 * and where it may be used. If an ACL relies on multiple matches, it is
1368 * OK if at least one of them may match in the context where it is used.
1369 */
Willy Tarreaud76a98a2013-03-31 18:34:33 +02001370 cur_acl->use |= acl_expr->smp->use;
1371 cur_acl->val |= acl_expr->smp->val;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001372 LIST_ADDQ(&cur_acl->expr, &acl_expr->list);
1373 return cur_acl;
1374
1375 out_free_name:
1376 free(name);
1377 out_free_acl_expr:
1378 prune_acl_expr(acl_expr);
1379 free(acl_expr);
1380 out_return:
1381 return NULL;
1382}
1383
Willy Tarreau16fbe822007-06-17 11:54:31 +02001384/* Some useful ACLs provided by default. Only those used are allocated. */
1385
1386const struct {
1387 const char *name;
1388 const char *expr[4]; /* put enough for longest expression */
1389} default_acl_list[] = {
Willy Tarreau58393e12008-07-20 10:39:22 +02001390 { .name = "TRUE", .expr = {"always_true",""}},
1391 { .name = "FALSE", .expr = {"always_false",""}},
Willy Tarreau16fbe822007-06-17 11:54:31 +02001392 { .name = "LOCALHOST", .expr = {"src","127.0.0.1/8",""}},
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001393 { .name = "HTTP", .expr = {"req_proto_http",""}},
Willy Tarreau16fbe822007-06-17 11:54:31 +02001394 { .name = "HTTP_1.0", .expr = {"req_ver","1.0",""}},
1395 { .name = "HTTP_1.1", .expr = {"req_ver","1.1",""}},
1396 { .name = "METH_CONNECT", .expr = {"method","CONNECT",""}},
1397 { .name = "METH_GET", .expr = {"method","GET","HEAD",""}},
1398 { .name = "METH_HEAD", .expr = {"method","HEAD",""}},
1399 { .name = "METH_OPTIONS", .expr = {"method","OPTIONS",""}},
1400 { .name = "METH_POST", .expr = {"method","POST",""}},
1401 { .name = "METH_TRACE", .expr = {"method","TRACE",""}},
1402 { .name = "HTTP_URL_ABS", .expr = {"url_reg","^[^/:]*://",""}},
1403 { .name = "HTTP_URL_SLASH", .expr = {"url_beg","/",""}},
1404 { .name = "HTTP_URL_STAR", .expr = {"url","*",""}},
1405 { .name = "HTTP_CONTENT", .expr = {"hdr_val(content-length)","gt","0",""}},
Emeric Brunbede3d02009-06-30 17:54:00 +02001406 { .name = "RDP_COOKIE", .expr = {"req_rdp_cookie_cnt","gt","0",""}},
Willy Tarreauc6317702008-07-20 09:29:50 +02001407 { .name = "REQ_CONTENT", .expr = {"req_len","gt","0",""}},
Willy Tarreaub6fb4202008-07-20 11:18:28 +02001408 { .name = "WAIT_END", .expr = {"wait_end",""}},
Willy Tarreau16fbe822007-06-17 11:54:31 +02001409 { .name = NULL, .expr = {""}}
1410};
1411
1412/* Find a default ACL from the default_acl list, compile it and return it.
1413 * If the ACL is not found, NULL is returned. In theory, it cannot fail,
1414 * except when default ACLs are broken, in which case it will return NULL.
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001415 * If <known_acl> is not NULL, the ACL will be queued at its tail. If <err> is
1416 * not NULL, it will be filled with an error message if an error occurs. This
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001417 * pointer must be freeable or NULL. <al> is an arg_list serving as a list head
1418 * to report missing dependencies.
Willy Tarreau16fbe822007-06-17 11:54:31 +02001419 */
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001420static struct acl *find_acl_default(const char *acl_name, struct list *known_acl,
1421 char **err, struct arg_list *al)
Willy Tarreau16fbe822007-06-17 11:54:31 +02001422{
1423 __label__ out_return, out_free_acl_expr, out_free_name;
1424 struct acl *cur_acl;
1425 struct acl_expr *acl_expr;
1426 char *name;
1427 int index;
1428
1429 for (index = 0; default_acl_list[index].name != NULL; index++) {
1430 if (strcmp(acl_name, default_acl_list[index].name) == 0)
1431 break;
1432 }
1433
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001434 if (default_acl_list[index].name == NULL) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001435 memprintf(err, "no such ACL : '%s'", acl_name);
Willy Tarreau16fbe822007-06-17 11:54:31 +02001436 return NULL;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001437 }
Willy Tarreau16fbe822007-06-17 11:54:31 +02001438
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001439 acl_expr = parse_acl_expr((const char **)default_acl_list[index].expr, err, al);
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001440 if (!acl_expr) {
1441 /* parse_acl_expr must have filled err here */
Willy Tarreau16fbe822007-06-17 11:54:31 +02001442 goto out_return;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001443 }
Willy Tarreau16fbe822007-06-17 11:54:31 +02001444
1445 name = strdup(acl_name);
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001446 if (!name) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001447 memprintf(err, "out of memory when building default ACL '%s'", acl_name);
Willy Tarreau16fbe822007-06-17 11:54:31 +02001448 goto out_free_acl_expr;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001449 }
1450
Willy Tarreau16fbe822007-06-17 11:54:31 +02001451 cur_acl = (struct acl *)calloc(1, sizeof(*cur_acl));
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001452 if (cur_acl == NULL) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001453 memprintf(err, "out of memory when building default ACL '%s'", acl_name);
Willy Tarreau16fbe822007-06-17 11:54:31 +02001454 goto out_free_name;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001455 }
Willy Tarreau16fbe822007-06-17 11:54:31 +02001456
1457 cur_acl->name = name;
Willy Tarreaud76a98a2013-03-31 18:34:33 +02001458 cur_acl->use |= acl_expr->smp->use;
1459 cur_acl->val |= acl_expr->smp->val;
Willy Tarreau16fbe822007-06-17 11:54:31 +02001460 LIST_INIT(&cur_acl->expr);
1461 LIST_ADDQ(&cur_acl->expr, &acl_expr->list);
1462 if (known_acl)
1463 LIST_ADDQ(known_acl, &cur_acl->list);
1464
1465 return cur_acl;
1466
1467 out_free_name:
1468 free(name);
1469 out_free_acl_expr:
1470 prune_acl_expr(acl_expr);
1471 free(acl_expr);
1472 out_return:
1473 return NULL;
1474}
Willy Tarreaua84d3742007-05-07 00:36:48 +02001475
1476/* Purge everything in the acl_cond <cond>, then return <cond>. */
1477struct acl_cond *prune_acl_cond(struct acl_cond *cond)
1478{
1479 struct acl_term_suite *suite, *tmp_suite;
1480 struct acl_term *term, *tmp_term;
1481
1482 /* iterate through all term suites and free all terms and all suites */
1483 list_for_each_entry_safe(suite, tmp_suite, &cond->suites, list) {
1484 list_for_each_entry_safe(term, tmp_term, &suite->terms, list)
1485 free(term);
1486 free(suite);
1487 }
1488 return cond;
1489}
1490
1491/* Parse an ACL condition starting at <args>[0], relying on a list of already
1492 * known ACLs passed in <known_acl>. The new condition is returned (or NULL in
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001493 * case of low memory). Supports multiple conditions separated by "or". If
1494 * <err> is not NULL, it will be filled with a pointer to an error message in
1495 * case of error, that the caller is responsible for freeing. The initial
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001496 * location must either be freeable or NULL. The list <al> serves as a list head
1497 * for unresolved dependencies.
Willy Tarreaua84d3742007-05-07 00:36:48 +02001498 */
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001499struct acl_cond *parse_acl_cond(const char **args, struct list *known_acl,
1500 int pol, char **err, struct arg_list *al)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001501{
1502 __label__ out_return, out_free_suite, out_free_term;
Willy Tarreau74b98a82007-06-16 19:35:18 +02001503 int arg, neg;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001504 const char *word;
1505 struct acl *cur_acl;
1506 struct acl_term *cur_term;
1507 struct acl_term_suite *cur_suite;
1508 struct acl_cond *cond;
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001509 unsigned int suite_val;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001510
1511 cond = (struct acl_cond *)calloc(1, sizeof(*cond));
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001512 if (cond == NULL) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001513 memprintf(err, "out of memory when parsing condition");
Willy Tarreaua84d3742007-05-07 00:36:48 +02001514 goto out_return;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001515 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001516
1517 LIST_INIT(&cond->list);
1518 LIST_INIT(&cond->suites);
1519 cond->pol = pol;
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001520 cond->val = 0;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001521
1522 cur_suite = NULL;
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001523 suite_val = ~0U;
Willy Tarreau74b98a82007-06-16 19:35:18 +02001524 neg = 0;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001525 for (arg = 0; *args[arg]; arg++) {
1526 word = args[arg];
1527
1528 /* remove as many exclamation marks as we can */
1529 while (*word == '!') {
1530 neg = !neg;
1531 word++;
1532 }
1533
1534 /* an empty word is allowed because we cannot force the user to
1535 * always think about not leaving exclamation marks alone.
1536 */
1537 if (!*word)
1538 continue;
1539
Willy Tarreau16fbe822007-06-17 11:54:31 +02001540 if (strcasecmp(word, "or") == 0 || strcmp(word, "||") == 0) {
Willy Tarreaua84d3742007-05-07 00:36:48 +02001541 /* new term suite */
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001542 cond->val |= suite_val;
1543 suite_val = ~0U;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001544 cur_suite = NULL;
1545 neg = 0;
1546 continue;
1547 }
1548
Willy Tarreau95fa4692010-02-01 13:05:50 +01001549 if (strcmp(word, "{") == 0) {
1550 /* we may have a complete ACL expression between two braces,
1551 * find the last one.
1552 */
1553 int arg_end = arg + 1;
1554 const char **args_new;
1555
1556 while (*args[arg_end] && strcmp(args[arg_end], "}") != 0)
1557 arg_end++;
1558
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001559 if (!*args[arg_end]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001560 memprintf(err, "missing closing '}' in condition");
Willy Tarreau95fa4692010-02-01 13:05:50 +01001561 goto out_free_suite;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001562 }
Willy Tarreau95fa4692010-02-01 13:05:50 +01001563
1564 args_new = calloc(1, (arg_end - arg + 1) * sizeof(*args_new));
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001565 if (!args_new) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001566 memprintf(err, "out of memory when parsing condition");
Willy Tarreau95fa4692010-02-01 13:05:50 +01001567 goto out_free_suite;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001568 }
Willy Tarreau95fa4692010-02-01 13:05:50 +01001569
Willy Tarreau2a56c5e2010-03-15 16:13:29 +01001570 args_new[0] = "";
Willy Tarreau95fa4692010-02-01 13:05:50 +01001571 memcpy(args_new + 1, args + arg + 1, (arg_end - arg) * sizeof(*args_new));
1572 args_new[arg_end - arg] = "";
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001573 cur_acl = parse_acl(args_new, known_acl, err, al);
Willy Tarreau95fa4692010-02-01 13:05:50 +01001574 free(args_new);
1575
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001576 if (!cur_acl) {
1577 /* note that parse_acl() must have filled <err> here */
Willy Tarreau16fbe822007-06-17 11:54:31 +02001578 goto out_free_suite;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001579 }
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001580 word = args[arg + 1];
Willy Tarreau95fa4692010-02-01 13:05:50 +01001581 arg = arg_end;
1582 }
1583 else {
1584 /* search for <word> in the known ACL names. If we do not find
1585 * it, let's look for it in the default ACLs, and if found, add
1586 * it to the list of ACLs of this proxy. This makes it possible
1587 * to override them.
1588 */
1589 cur_acl = find_acl_by_name(word, known_acl);
1590 if (cur_acl == NULL) {
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001591 cur_acl = find_acl_default(word, known_acl, err, al);
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001592 if (cur_acl == NULL) {
1593 /* note that find_acl_default() must have filled <err> here */
Willy Tarreau95fa4692010-02-01 13:05:50 +01001594 goto out_free_suite;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001595 }
Willy Tarreau95fa4692010-02-01 13:05:50 +01001596 }
Willy Tarreau16fbe822007-06-17 11:54:31 +02001597 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001598
1599 cur_term = (struct acl_term *)calloc(1, sizeof(*cur_term));
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001600 if (cur_term == NULL) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001601 memprintf(err, "out of memory when parsing condition");
Willy Tarreaua84d3742007-05-07 00:36:48 +02001602 goto out_free_suite;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001603 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001604
1605 cur_term->acl = cur_acl;
1606 cur_term->neg = neg;
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001607
1608 /* Here it is a bit complex. The acl_term_suite is a conjunction
1609 * of many terms. It may only be used if all of its terms are
1610 * usable at the same time. So the suite's validity domain is an
1611 * AND between all ACL keywords' ones. But, the global condition
1612 * is valid if at least one term suite is OK. So it's an OR between
1613 * all of their validity domains. We could emit a warning as soon
1614 * as suite_val is null because it means that the last ACL is not
1615 * compatible with the previous ones. Let's remain simple for now.
1616 */
1617 cond->use |= cur_acl->use;
1618 suite_val &= cur_acl->val;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001619
1620 if (!cur_suite) {
1621 cur_suite = (struct acl_term_suite *)calloc(1, sizeof(*cur_suite));
Willy Tarreauf678b7f2013-01-24 00:25:39 +01001622 if (cur_suite == NULL) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001623 memprintf(err, "out of memory when parsing condition");
Willy Tarreaua84d3742007-05-07 00:36:48 +02001624 goto out_free_term;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001625 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001626 LIST_INIT(&cur_suite->terms);
1627 LIST_ADDQ(&cond->suites, &cur_suite->list);
1628 }
1629 LIST_ADDQ(&cur_suite->terms, &cur_term->list);
Willy Tarreau74b98a82007-06-16 19:35:18 +02001630 neg = 0;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001631 }
1632
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001633 cond->val |= suite_val;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001634 return cond;
1635
1636 out_free_term:
1637 free(cur_term);
1638 out_free_suite:
1639 prune_acl_cond(cond);
1640 free(cond);
1641 out_return:
1642 return NULL;
1643}
1644
Willy Tarreau2bbba412010-01-28 16:48:33 +01001645/* Builds an ACL condition starting at the if/unless keyword. The complete
1646 * condition is returned. NULL is returned in case of error or if the first
1647 * word is neither "if" nor "unless". It automatically sets the file name and
Willy Tarreau25320b22013-03-24 07:22:08 +01001648 * the line number in the condition for better error reporting, and sets the
1649 * HTTP intiailization requirements in the proxy. If <err> is not NULL, it will
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001650 * be filled with a pointer to an error message in case of error, that the
1651 * caller is responsible for freeing. The initial location must either be
1652 * freeable or NULL.
Willy Tarreau2bbba412010-01-28 16:48:33 +01001653 */
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001654struct 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 +01001655{
1656 int pol = ACL_COND_NONE;
1657 struct acl_cond *cond = NULL;
1658
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001659 if (err)
1660 *err = NULL;
1661
Willy Tarreau2bbba412010-01-28 16:48:33 +01001662 if (!strcmp(*args, "if")) {
1663 pol = ACL_COND_IF;
1664 args++;
1665 }
1666 else if (!strcmp(*args, "unless")) {
1667 pol = ACL_COND_UNLESS;
1668 args++;
1669 }
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001670 else {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001671 memprintf(err, "conditions must start with either 'if' or 'unless'");
Willy Tarreau2bbba412010-01-28 16:48:33 +01001672 return NULL;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001673 }
Willy Tarreau2bbba412010-01-28 16:48:33 +01001674
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001675 cond = parse_acl_cond(args, &px->acl, pol, err, &px->conf.args);
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001676 if (!cond) {
1677 /* note that parse_acl_cond must have filled <err> here */
Willy Tarreau2bbba412010-01-28 16:48:33 +01001678 return NULL;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001679 }
Willy Tarreau2bbba412010-01-28 16:48:33 +01001680
1681 cond->file = file;
1682 cond->line = line;
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001683 px->http_needed |= !!(cond->use & SMP_USE_HTTP_ANY);
Willy Tarreau2bbba412010-01-28 16:48:33 +01001684 return cond;
1685}
1686
Willy Tarreau11382812008-07-09 16:18:21 +02001687/* Execute condition <cond> and return either ACL_PAT_FAIL, ACL_PAT_MISS or
Willy Tarreaub6866442008-07-14 23:54:42 +02001688 * ACL_PAT_PASS depending on the test results. ACL_PAT_MISS may only be
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001689 * returned if <opt> does not contain SMP_OPT_FINAL, indicating that incomplete
Willy Tarreau7a777ed2012-04-26 11:44:02 +02001690 * data is being examined. The function automatically sets SMP_OPT_ITERATE.
Willy Tarreaub6866442008-07-14 23:54:42 +02001691 * This function only computes the condition, it does not apply the polarity
1692 * required by IF/UNLESS, it's up to the caller to do this using something like
1693 * this :
Willy Tarreau11382812008-07-09 16:18:21 +02001694 *
1695 * res = acl_pass(res);
Willy Tarreaub6866442008-07-14 23:54:42 +02001696 * if (res == ACL_PAT_MISS)
1697 * return 0;
Willy Tarreau11382812008-07-09 16:18:21 +02001698 * if (cond->pol == ACL_COND_UNLESS)
1699 * res = !res;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001700 */
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001701int 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 +02001702{
1703 __label__ fetch_next;
1704 struct acl_term_suite *suite;
1705 struct acl_term *term;
1706 struct acl_expr *expr;
1707 struct acl *acl;
1708 struct acl_pattern *pattern;
Willy Tarreau37406352012-04-23 16:16:37 +02001709 struct sample smp;
Willy Tarreau11382812008-07-09 16:18:21 +02001710 int acl_res, suite_res, cond_res;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001711
Willy Tarreau7a777ed2012-04-26 11:44:02 +02001712 /* ACLs are iterated over all values, so let's always set the flag to
1713 * indicate this to the fetch functions.
1714 */
1715 opt |= SMP_OPT_ITERATE;
1716
Willy Tarreau11382812008-07-09 16:18:21 +02001717 /* We're doing a logical OR between conditions so we initialize to FAIL.
1718 * The MISS status is propagated down from the suites.
1719 */
Willy Tarreaua84d3742007-05-07 00:36:48 +02001720 cond_res = ACL_PAT_FAIL;
1721 list_for_each_entry(suite, &cond->suites, list) {
Willy Tarreau11382812008-07-09 16:18:21 +02001722 /* Evaluate condition suite <suite>. We stop at the first term
1723 * which returns ACL_PAT_FAIL. The MISS status is still propagated
1724 * in case of uncertainty in the result.
Willy Tarreaua84d3742007-05-07 00:36:48 +02001725 */
1726
1727 /* we're doing a logical AND between terms, so we must set the
1728 * initial value to PASS.
1729 */
1730 suite_res = ACL_PAT_PASS;
1731 list_for_each_entry(term, &suite->terms, list) {
1732 acl = term->acl;
1733
1734 /* FIXME: use cache !
1735 * check acl->cache_idx for this.
1736 */
1737
1738 /* ACL result not cached. Let's scan all the expressions
1739 * and use the first one to match.
1740 */
1741 acl_res = ACL_PAT_FAIL;
1742 list_for_each_entry(expr, &acl->expr, list) {
Willy Tarreaud41f8d82007-06-10 10:06:18 +02001743 /* we need to reset context and flags */
Willy Tarreau37406352012-04-23 16:16:37 +02001744 memset(&smp, 0, sizeof(smp));
Willy Tarreaua84d3742007-05-07 00:36:48 +02001745 fetch_next:
Willy Tarreaud76a98a2013-03-31 18:34:33 +02001746 if (!expr->smp->process(px, l4, l7, opt, expr->args, &smp)) {
Willy Tarreaub6866442008-07-14 23:54:42 +02001747 /* maybe we could not fetch because of missing data */
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001748 if (smp.flags & SMP_F_MAY_CHANGE && !(opt & SMP_OPT_FINAL))
Willy Tarreaub6866442008-07-14 23:54:42 +02001749 acl_res |= ACL_PAT_MISS;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001750 continue;
Willy Tarreaub6866442008-07-14 23:54:42 +02001751 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001752
Willy Tarreau24b2c762013-06-12 21:50:19 +02001753 if (expr->match == acl_match_nothing) {
Willy Tarreau197e10a2012-04-23 19:18:42 +02001754 if (smp.data.uint)
Willy Tarreaua79534f2008-07-20 10:13:37 +02001755 acl_res |= ACL_PAT_PASS;
1756 else
1757 acl_res |= ACL_PAT_FAIL;
1758 }
Willy Tarreau5adeda12013-03-31 22:13:34 +02001759 else if (!expr->match) {
1760 /* just check for existence */
1761 acl_res |= ACL_PAT_PASS;
1762 }
Willy Tarreaua79534f2008-07-20 10:13:37 +02001763 else {
Willy Tarreau020534d2010-05-16 21:45:45 +02001764 if (!eb_is_empty(&expr->pattern_tree)) {
Willy Tarreauc4262962010-05-10 23:42:40 +02001765 /* a tree is present, let's check what type it is */
Willy Tarreaud76a98a2013-03-31 18:34:33 +02001766 if (expr->match == acl_match_str)
Willy Tarreau37406352012-04-23 16:16:37 +02001767 acl_res |= acl_lookup_str(&smp, expr) ? ACL_PAT_PASS : ACL_PAT_FAIL;
Willy Tarreaud76a98a2013-03-31 18:34:33 +02001768 else if (expr->match == acl_match_ip)
Willy Tarreau37406352012-04-23 16:16:37 +02001769 acl_res |= acl_lookup_ip(&smp, expr) ? ACL_PAT_PASS : ACL_PAT_FAIL;
Willy Tarreauc4262962010-05-10 23:42:40 +02001770 }
1771
Willy Tarreaua79534f2008-07-20 10:13:37 +02001772 /* call the match() function for all tests on this value */
1773 list_for_each_entry(pattern, &expr->patterns, list) {
Willy Tarreaua79534f2008-07-20 10:13:37 +02001774 if (acl_res == ACL_PAT_PASS)
1775 break;
Willy Tarreaud76a98a2013-03-31 18:34:33 +02001776 acl_res |= expr->match(&smp, pattern);
Willy Tarreaua79534f2008-07-20 10:13:37 +02001777 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001778 }
1779 /*
Willy Tarreau11382812008-07-09 16:18:21 +02001780 * OK now acl_res holds the result of this expression
1781 * as one of ACL_PAT_FAIL, ACL_PAT_MISS or ACL_PAT_PASS.
Willy Tarreaua84d3742007-05-07 00:36:48 +02001782 *
Willy Tarreau11382812008-07-09 16:18:21 +02001783 * Then if (!MISS) we can cache the result, and put
Willy Tarreau37406352012-04-23 16:16:37 +02001784 * (smp.flags & SMP_F_VOLATILE) in the cache flags.
Willy Tarreaua84d3742007-05-07 00:36:48 +02001785 *
1786 * FIXME: implement cache.
1787 *
1788 */
1789
Willy Tarreau11382812008-07-09 16:18:21 +02001790 /* we're ORing these terms, so a single PASS is enough */
1791 if (acl_res == ACL_PAT_PASS)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001792 break;
1793
Willy Tarreau37406352012-04-23 16:16:37 +02001794 if (smp.flags & SMP_F_NOT_LAST)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001795 goto fetch_next;
Willy Tarreaub6866442008-07-14 23:54:42 +02001796
1797 /* sometimes we know the fetched data is subject to change
1798 * later and give another chance for a new match (eg: request
1799 * size, time, ...)
1800 */
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001801 if (smp.flags & SMP_F_MAY_CHANGE && !(opt & SMP_OPT_FINAL))
Willy Tarreaub6866442008-07-14 23:54:42 +02001802 acl_res |= ACL_PAT_MISS;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001803 }
1804 /*
1805 * Here we have the result of an ACL (cached or not).
1806 * ACLs are combined, negated or not, to form conditions.
1807 */
1808
Willy Tarreaua84d3742007-05-07 00:36:48 +02001809 if (term->neg)
Willy Tarreau11382812008-07-09 16:18:21 +02001810 acl_res = acl_neg(acl_res);
Willy Tarreaua84d3742007-05-07 00:36:48 +02001811
1812 suite_res &= acl_res;
Willy Tarreau11382812008-07-09 16:18:21 +02001813
1814 /* we're ANDing these terms, so a single FAIL is enough */
1815 if (suite_res == ACL_PAT_FAIL)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001816 break;
1817 }
1818 cond_res |= suite_res;
Willy Tarreau11382812008-07-09 16:18:21 +02001819
1820 /* we're ORing these terms, so a single PASS is enough */
1821 if (cond_res == ACL_PAT_PASS)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001822 break;
1823 }
Willy Tarreau11382812008-07-09 16:18:21 +02001824 return cond_res;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001825}
1826
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001827/* Returns a pointer to the first ACL conflicting with usage at place <where>
1828 * which is one of the SMP_VAL_* bits indicating a check place, or NULL if
1829 * no conflict is found. Only full conflicts are detected (ACL is not usable).
1830 * Use the next function to check for useless keywords.
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001831 */
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001832const struct acl *acl_cond_conflicts(const struct acl_cond *cond, unsigned int where)
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001833{
1834 struct acl_term_suite *suite;
1835 struct acl_term *term;
1836 struct acl *acl;
1837
1838 list_for_each_entry(suite, &cond->suites, list) {
1839 list_for_each_entry(term, &suite->terms, list) {
1840 acl = term->acl;
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001841 if (!(acl->val & where))
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001842 return acl;
1843 }
1844 }
1845 return NULL;
1846}
1847
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001848/* Returns a pointer to the first ACL and its first keyword to conflict with
1849 * usage at place <where> which is one of the SMP_VAL_* bits indicating a check
1850 * place. Returns true if a conflict is found, with <acl> and <kw> set (if non
1851 * null), or false if not conflict is found. The first useless keyword is
1852 * returned.
1853 */
Willy Tarreau93fddf12013-03-31 22:59:32 +02001854int 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 +01001855{
1856 struct acl_term_suite *suite;
1857 struct acl_term *term;
1858 struct acl_expr *expr;
1859
1860 list_for_each_entry(suite, &cond->suites, list) {
1861 list_for_each_entry(term, &suite->terms, list) {
1862 list_for_each_entry(expr, &term->acl->expr, list) {
Willy Tarreaud76a98a2013-03-31 18:34:33 +02001863 if (!(expr->smp->val & where)) {
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001864 if (acl)
1865 *acl = term->acl;
1866 if (kw)
1867 *kw = expr->kw;
1868 return 1;
1869 }
1870 }
1871 }
1872 }
1873 return 0;
1874}
1875
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001876/*
1877 * Find targets for userlist and groups in acl. Function returns the number
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001878 * of errors or OK if everything is fine. It must be called only once sample
1879 * fetch arguments have been resolved (after smp_resolve_args()).
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001880 */
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001881int acl_find_targets(struct proxy *p)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001882{
1883
1884 struct acl *acl;
1885 struct acl_expr *expr;
1886 struct acl_pattern *pattern;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001887 int cfgerr = 0;
1888
1889 list_for_each_entry(acl, &p->acl, list) {
1890 list_for_each_entry(expr, &acl->expr, list) {
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001891 if (!strcmp(expr->kw, "http_auth_group")) {
1892 /* Note: the ARGT_USR argument may only have been resolved earlier
1893 * by smp_resolve_args().
1894 */
1895 if (expr->args->unresolved) {
1896 Alert("Internal bug in proxy %s: %sacl %s %s() makes use of unresolved userlist '%s'. Please report this.\n",
1897 p->id, *acl->name ? "" : "anonymous ", acl->name, expr->kw, expr->args->data.str.str);
1898 cfgerr++;
Willy Tarreau496aa012012-06-01 10:38:29 +02001899 continue;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001900 }
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001901
1902 if (LIST_ISEMPTY(&expr->patterns)) {
1903 Alert("proxy %s: acl %s %s(): no groups specified.\n",
Willy Tarreau93fddf12013-03-31 22:59:32 +02001904 p->id, acl->name, expr->kw);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001905 cfgerr++;
1906 continue;
1907 }
1908
1909 list_for_each_entry(pattern, &expr->patterns, list) {
Willy Tarreau7d1df412012-11-23 23:47:36 +01001910 /* this keyword only has one argument */
Willy Tarreau34db1082012-04-19 17:16:54 +02001911 pattern->val.group_mask = auth_resolve_groups(expr->args->data.usr, pattern->ptr.str);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001912
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001913 if (!pattern->val.group_mask) {
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001914 Alert("proxy %s: acl %s %s(): invalid group '%s'.\n",
1915 p->id, acl->name, expr->kw, pattern->ptr.str);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001916 cfgerr++;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001917 }
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001918 free(pattern->ptr.str);
1919 pattern->ptr.str = NULL;
1920 pattern->len = 0;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001921 }
1922 }
1923 }
1924 }
1925
1926 return cfgerr;
1927}
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001928
Willy Tarreau8ed669b2013-01-11 15:49:37 +01001929/* initializes ACLs by resolving the sample fetch names they rely upon.
1930 * Returns 0 on success, otherwise an error.
1931 */
1932int init_acl()
1933{
1934 int err = 0;
1935 int index;
1936 const char *name;
1937 struct acl_kw_list *kwl;
1938 struct sample_fetch *smp;
1939
1940 list_for_each_entry(kwl, &acl_keywords.list, list) {
1941 for (index = 0; kwl->kw[index].kw != NULL; index++) {
1942 name = kwl->kw[index].fetch_kw;
1943 if (!name)
1944 name = kwl->kw[index].kw;
1945
1946 smp = find_sample_fetch(name, strlen(name));
1947 if (!smp) {
1948 Alert("Critical internal error: ACL keyword '%s' relies on sample fetch '%s' which was not registered!\n",
1949 kwl->kw[index].kw, name);
1950 err++;
1951 continue;
1952 }
1953 kwl->kw[index].smp = smp;
1954 }
1955 }
1956 return err;
1957}
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001958
Willy Tarreaua84d3742007-05-07 00:36:48 +02001959/************************************************************************/
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001960/* All supported sample fetch functions must be declared here */
Willy Tarreaua84d3742007-05-07 00:36:48 +02001961/************************************************************************/
1962
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001963/* force TRUE to be returned at the fetch level */
1964static int
1965smp_fetch_true(struct proxy *px, struct session *s, void *l7, unsigned int opt,
1966 const struct arg *args, struct sample *smp)
1967{
1968 smp->type = SMP_T_BOOL;
1969 smp->data.uint = 1;
1970 return 1;
1971}
1972
1973/* force FALSE to be returned at the fetch level */
1974static int
1975smp_fetch_false(struct proxy *px, struct session *s, void *l7, unsigned int opt,
1976 const struct arg *args, struct sample *smp)
1977{
1978 smp->type = SMP_T_BOOL;
1979 smp->data.uint = 0;
1980 return 1;
1981}
1982
Willy Tarreau595ec542013-06-12 21:34:28 +02001983/* retrieve environment variable $1 as a string */
1984static int
1985smp_fetch_env(struct proxy *px, struct session *s, void *l7, unsigned int opt,
1986 const struct arg *args, struct sample *smp)
1987{
1988 char *env;
1989
1990 if (!args || args[0].type != ARGT_STR)
1991 return 0;
1992
1993 env = getenv(args[0].data.str.str);
1994 if (!env)
1995 return 0;
1996
1997 smp->type = SMP_T_CSTR;
1998 smp->data.str.str = env;
1999 smp->data.str.len = strlen(env);
2000 return 1;
2001}
2002
Willy Tarreaud4c33c82013-01-07 21:59:07 +01002003
2004/************************************************************************/
2005/* All supported sample and ACL keywords must be declared here. */
2006/************************************************************************/
2007
2008/* Note: must not be declared <const> as its list will be overwritten.
2009 * Note: fetches that may return multiple types must be declared as the lowest
2010 * common denominator, the type that can be casted into all other ones. For
2011 * instance IPv4/IPv6 must be declared IPv4.
2012 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02002013static struct sample_fetch_kw_list smp_kws = {ILH, {
Willy Tarreau595ec542013-06-12 21:34:28 +02002014 { "always_false", smp_fetch_false, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
2015 { "always_true", smp_fetch_true, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
2016 { "env", smp_fetch_env, ARG1(1,STR), NULL, SMP_T_CSTR, SMP_USE_INTRN },
Willy Tarreaud4c33c82013-01-07 21:59:07 +01002017 { /* END */ },
2018}};
2019
2020
Willy Tarreau61612d42012-04-19 18:42:05 +02002021/* Note: must not be declared <const> as its list will be overwritten.
2022 * Please take care of keeping this list alphabetically sorted.
2023 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02002024static struct acl_kw_list acl_kws = {ILH, {
Willy Tarreaud4c33c82013-01-07 21:59:07 +01002025 { /* END */ },
Willy Tarreaua84d3742007-05-07 00:36:48 +02002026}};
2027
2028
2029__attribute__((constructor))
2030static void __acl_init(void)
2031{
Willy Tarreaud4c33c82013-01-07 21:59:07 +01002032 sample_register_fetches(&smp_kws);
Willy Tarreaua84d3742007-05-07 00:36:48 +02002033 acl_register_keywords(&acl_kws);
2034}
2035
2036
2037/*
2038 * Local variables:
2039 * c-indent-level: 8
2040 * c-basic-offset: 8
2041 * End:
2042 */