blob: 927f7d0c430b7e4057e004229860f71079945b24 [file] [log] [blame]
Willy Tarreaua84d3742007-05-07 00:36:48 +02001/*
2 * ACL management functions.
3 *
Willy Tarreau06457872010-05-23 12:24:38 +02004 * Copyright 2000-2010 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>
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010025#include <proto/auth.h>
Willy Tarreau44b90cc2010-05-24 20:27:29 +020026#include <proto/buffers.h>
Willy Tarreau404e8ab2009-07-26 19:40:40 +020027#include <proto/log.h>
Willy Tarreau0b1cd942010-05-16 22:18:27 +020028#include <proto/proxy.h>
Willy Tarreaua84d3742007-05-07 00:36:48 +020029
Willy Tarreauc4262962010-05-10 23:42:40 +020030#include <ebsttree.h>
31
Willy Tarreaua9802632008-07-25 19:13:19 +020032/* The capabilities of filtering hooks describe the type of information
33 * available to each of them.
34 */
35const unsigned int filt_cap[] = {
36 [ACL_HOOK_REQ_FE_TCP] = ACL_USE_TCP4_ANY|ACL_USE_TCP6_ANY|ACL_USE_TCP_ANY,
Willy Tarreau06457872010-05-23 12:24:38 +020037 [ACL_HOOK_REQ_FE_TCP_CONTENT] = ACL_USE_TCP4_ANY|ACL_USE_TCP6_ANY|ACL_USE_TCP_ANY|ACL_USE_L6REQ_ANY,
38 [ACL_HOOK_REQ_FE_HTTP_IN] = ACL_USE_TCP4_ANY|ACL_USE_TCP6_ANY|ACL_USE_TCP_ANY|ACL_USE_L6REQ_ANY|ACL_USE_L7REQ_ANY|ACL_USE_HDR_ANY,
39 [ACL_HOOK_REQ_FE_SWITCH] = ACL_USE_TCP4_ANY|ACL_USE_TCP6_ANY|ACL_USE_TCP_ANY|ACL_USE_L6REQ_ANY|ACL_USE_L7REQ_ANY|ACL_USE_HDR_ANY,
40 [ACL_HOOK_REQ_BE_TCP_CONTENT] = ACL_USE_TCP4_ANY|ACL_USE_TCP6_ANY|ACL_USE_TCP_ANY|ACL_USE_L6REQ_ANY|ACL_USE_L7REQ_ANY|ACL_USE_HDR_ANY,
41 [ACL_HOOK_REQ_BE_HTTP_IN] = ACL_USE_TCP4_ANY|ACL_USE_TCP6_ANY|ACL_USE_TCP_ANY|ACL_USE_L6REQ_ANY|ACL_USE_L7REQ_ANY|ACL_USE_HDR_ANY,
42 [ACL_HOOK_REQ_BE_SWITCH] = ACL_USE_TCP4_ANY|ACL_USE_TCP6_ANY|ACL_USE_TCP_ANY|ACL_USE_L6REQ_ANY|ACL_USE_L7REQ_ANY|ACL_USE_HDR_ANY,
43 [ACL_HOOK_REQ_FE_HTTP_OUT] = ACL_USE_TCP4_ANY|ACL_USE_TCP6_ANY|ACL_USE_TCP_ANY|ACL_USE_L6REQ_ANY|ACL_USE_L7REQ_ANY|ACL_USE_HDR_ANY,
44 [ACL_HOOK_REQ_BE_HTTP_OUT] = ACL_USE_TCP4_ANY|ACL_USE_TCP6_ANY|ACL_USE_TCP_ANY|ACL_USE_L6REQ_ANY|ACL_USE_L7REQ_ANY|ACL_USE_HDR_ANY,
Willy Tarreaua9802632008-07-25 19:13:19 +020045
Willy Tarreau06457872010-05-23 12:24:38 +020046 [ACL_HOOK_RTR_BE_TCP_CONTENT] = ACL_USE_REQ_PERMANENT|ACL_USE_REQ_CACHEABLE|ACL_USE_L6RTR_ANY,
47 [ACL_HOOK_RTR_BE_HTTP_IN] = ACL_USE_REQ_PERMANENT|ACL_USE_REQ_CACHEABLE|ACL_USE_L6RTR_ANY|ACL_USE_L7RTR_ANY,
48 [ACL_HOOK_RTR_FE_TCP_CONTENT] = ACL_USE_REQ_PERMANENT|ACL_USE_REQ_CACHEABLE|ACL_USE_L6RTR_ANY|ACL_USE_L7RTR_ANY,
49 [ACL_HOOK_RTR_FE_HTTP_IN] = ACL_USE_REQ_PERMANENT|ACL_USE_REQ_CACHEABLE|ACL_USE_L6RTR_ANY|ACL_USE_L7RTR_ANY,
50 [ACL_HOOK_RTR_BE_HTTP_OUT] = ACL_USE_REQ_PERMANENT|ACL_USE_REQ_CACHEABLE|ACL_USE_L6RTR_ANY|ACL_USE_L7RTR_ANY,
51 [ACL_HOOK_RTR_FE_HTTP_OUT] = ACL_USE_REQ_PERMANENT|ACL_USE_REQ_CACHEABLE|ACL_USE_L6RTR_ANY|ACL_USE_L7RTR_ANY,
Willy Tarreaua9802632008-07-25 19:13:19 +020052};
53
Willy Tarreaua84d3742007-05-07 00:36:48 +020054/* List head of all known ACL keywords */
55static struct acl_kw_list acl_keywords = {
56 .list = LIST_HEAD_INIT(acl_keywords.list)
57};
58
59
Willy Tarreaua5909832007-06-17 20:40:25 +020060/*
61 * These functions are only used for debugging complex configurations.
Willy Tarreaua84d3742007-05-07 00:36:48 +020062 */
Willy Tarreaua5909832007-06-17 20:40:25 +020063
Willy Tarreau58393e12008-07-20 10:39:22 +020064/* force TRUE to be returned at the fetch level */
Willy Tarreaua5909832007-06-17 20:40:25 +020065static int
Willy Tarreau58393e12008-07-20 10:39:22 +020066acl_fetch_true(struct proxy *px, struct session *l4, void *l7, int dir,
67 struct acl_expr *expr, struct acl_test *test)
Willy Tarreaua5909832007-06-17 20:40:25 +020068{
Willy Tarreau58393e12008-07-20 10:39:22 +020069 test->flags |= ACL_TEST_F_SET_RES_PASS;
Willy Tarreaua5909832007-06-17 20:40:25 +020070 return 1;
71}
72
Willy Tarreaub6fb4202008-07-20 11:18:28 +020073/* wait for more data as long as possible, then return TRUE. This should be
74 * used with content inspection.
75 */
76static int
77acl_fetch_wait_end(struct proxy *px, struct session *l4, void *l7, int dir,
78 struct acl_expr *expr, struct acl_test *test)
79{
80 if (dir & ACL_PARTIAL) {
81 test->flags |= ACL_TEST_F_MAY_CHANGE;
82 return 0;
83 }
84 test->flags |= ACL_TEST_F_SET_RES_PASS;
85 return 1;
86}
87
Willy Tarreau58393e12008-07-20 10:39:22 +020088/* force FALSE to be returned at the fetch level */
Willy Tarreaua5909832007-06-17 20:40:25 +020089static int
Willy Tarreau58393e12008-07-20 10:39:22 +020090acl_fetch_false(struct proxy *px, struct session *l4, void *l7, int dir,
91 struct acl_expr *expr, struct acl_test *test)
Willy Tarreaua84d3742007-05-07 00:36:48 +020092{
Willy Tarreau58393e12008-07-20 10:39:22 +020093 test->flags |= ACL_TEST_F_SET_RES_FAIL;
Willy Tarreaua84d3742007-05-07 00:36:48 +020094 return 1;
95}
96
Willy Tarreau44b90cc2010-05-24 20:27:29 +020097/* return the number of bytes in the request buffer */
98static int
99acl_fetch_req_len(struct proxy *px, struct session *l4, void *l7, int dir,
100 struct acl_expr *expr, struct acl_test *test)
101{
102 if (!l4 || !l4->req)
103 return 0;
104
105 test->i = l4->req->l;
106 test->flags = ACL_TEST_F_VOLATILE | ACL_TEST_F_MAY_CHANGE;
107 return 1;
108}
109
110/* Return the version of the SSL protocol in the request. It supports both
111 * SSLv3 (TLSv1) header format for any message, and SSLv2 header format for
112 * the hello message. The SSLv3 format is described in RFC 2246 p49, and the
113 * SSLv2 format is described here, and completed p67 of RFC 2246 :
114 * http://wp.netscape.com/eng/security/SSL_2.html
115 *
116 * Note: this decoder only works with non-wrapping data.
117 */
118static int
119acl_fetch_req_ssl_ver(struct proxy *px, struct session *l4, void *l7, int dir,
120 struct acl_expr *expr, struct acl_test *test)
121{
122 int version, bleft, msg_len;
123 const unsigned char *data;
124
125 if (!l4 || !l4->req)
126 return 0;
127
128 msg_len = 0;
129 bleft = l4->req->l;
130 if (!bleft)
131 goto too_short;
132
133 data = (const unsigned char *)l4->req->w;
134 if ((*data >= 0x14 && *data <= 0x17) || (*data == 0xFF)) {
135 /* SSLv3 header format */
136 if (bleft < 5)
137 goto too_short;
138
139 version = (data[1] << 16) + data[2]; /* version: major, minor */
140 msg_len = (data[3] << 8) + data[4]; /* record length */
141
142 /* format introduced with SSLv3 */
143 if (version < 0x00030000)
144 goto not_ssl;
145
146 /* message length between 1 and 2^14 + 2048 */
147 if (msg_len < 1 || msg_len > ((1<<14) + 2048))
148 goto not_ssl;
149
150 bleft -= 5; data += 5;
151 } else {
152 /* SSLv2 header format, only supported for hello (msg type 1) */
153 int rlen, plen, cilen, silen, chlen;
154
155 if (*data & 0x80) {
156 if (bleft < 3)
157 goto too_short;
158 /* short header format : 15 bits for length */
159 rlen = ((data[0] & 0x7F) << 8) | data[1];
160 plen = 0;
161 bleft -= 2; data += 2;
162 } else {
163 if (bleft < 4)
164 goto too_short;
165 /* long header format : 14 bits for length + pad length */
166 rlen = ((data[0] & 0x3F) << 8) | data[1];
167 plen = data[2];
168 bleft -= 3; data += 2;
169 }
170
171 if (*data != 0x01)
172 goto not_ssl;
173 bleft--; data++;
174
175 if (bleft < 8)
176 goto too_short;
177 version = (data[0] << 16) + data[1]; /* version: major, minor */
178 cilen = (data[2] << 8) + data[3]; /* cipher len, multiple of 3 */
179 silen = (data[4] << 8) + data[5]; /* session_id_len: 0 or 16 */
180 chlen = (data[6] << 8) + data[7]; /* 16<=challenge length<=32 */
181
182 bleft -= 8; data += 8;
183 if (cilen % 3 != 0)
184 goto not_ssl;
185 if (silen && silen != 16)
186 goto not_ssl;
187 if (chlen < 16 || chlen > 32)
188 goto not_ssl;
189 if (rlen != 9 + cilen + silen + chlen)
190 goto not_ssl;
191
192 /* focus on the remaining data length */
193 msg_len = cilen + silen + chlen + plen;
194 }
195 /* We could recursively check that the buffer ends exactly on an SSL
196 * fragment boundary and that a possible next segment is still SSL,
197 * but that's a bit pointless. However, we could still check that
198 * all the part of the request which fits in a buffer is already
199 * there.
200 */
201 if (msg_len > buffer_max_len(l4->req) + l4->req->data - l4->req->w)
202 msg_len = buffer_max_len(l4->req) + l4->req->data - l4->req->w;
203
204 if (bleft < msg_len)
205 goto too_short;
206
207 /* OK that's enough. We have at least the whole message, and we have
208 * the protocol version.
209 */
210 test->i = version;
211 test->flags = ACL_TEST_F_VOLATILE;
212 return 1;
213
214 too_short:
215 test->flags = ACL_TEST_F_MAY_CHANGE;
216 not_ssl:
217 return 0;
218}
219
220/* Fetch the RDP cookie identified in the expression.
221 * Note: this decoder only works with non-wrapping data.
222 */
223int
224acl_fetch_rdp_cookie(struct proxy *px, struct session *l4, void *l7, int dir,
225 struct acl_expr *expr, struct acl_test *test)
226{
227 int bleft;
228 const unsigned char *data;
229
230 if (!l4 || !l4->req)
231 return 0;
232
233 test->flags = 0;
234
235 bleft = l4->req->l;
236 if (bleft <= 11)
237 goto too_short;
238
239 data = (const unsigned char *)l4->req->w + 11;
240 bleft -= 11;
241
242 if (bleft <= 7)
243 goto too_short;
244
245 if (strncasecmp((const char *)data, "Cookie:", 7) != 0)
246 goto not_cookie;
247
248 data += 7;
249 bleft -= 7;
250
251 while (bleft > 0 && *data == ' ') {
252 data++;
253 bleft--;
254 }
255
256 if (expr->arg_len) {
257
258 if (bleft <= expr->arg_len)
259 goto too_short;
260
261 if ((data[expr->arg_len] != '=') ||
262 strncasecmp(expr->arg.str, (const char *)data, expr->arg_len) != 0)
263 goto not_cookie;
264
265 data += expr->arg_len + 1;
266 bleft -= expr->arg_len + 1;
267 } else {
268 while (bleft > 0 && *data != '=') {
269 if (*data == '\r' || *data == '\n')
270 goto not_cookie;
271 data++;
272 bleft--;
273 }
274
275 if (bleft < 1)
276 goto too_short;
277
278 if (*data != '=')
279 goto not_cookie;
280
281 data++;
282 bleft--;
283 }
284
285 /* data points to cookie value */
286 test->ptr = (char *)data;
287 test->len = 0;
288
289 while (bleft > 0 && *data != '\r') {
290 data++;
291 bleft--;
292 }
293
294 if (bleft < 2)
295 goto too_short;
296
297 if (data[0] != '\r' || data[1] != '\n')
298 goto not_cookie;
299
300 test->len = (char *)data - test->ptr;
301 test->flags = ACL_TEST_F_VOLATILE;
302 return 1;
303
304 too_short:
305 test->flags = ACL_TEST_F_MAY_CHANGE;
306 not_cookie:
307 return 0;
308}
309
310static int
311acl_fetch_rdp_cookie_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
312 struct acl_expr *expr, struct acl_test *test)
313{
314 int ret;
315
316 ret = acl_fetch_rdp_cookie(px, l4, l7, dir, expr, test);
317
318 test->ptr = NULL;
319 test->len = 0;
320
321 if (test->flags & ACL_TEST_F_MAY_CHANGE)
322 return 0;
323
324 test->flags = ACL_TEST_F_VOLATILE;
325 test->i = ret;
326
327 return 1;
328}
329
Willy Tarreau58393e12008-07-20 10:39:22 +0200330
331/*
332 * These functions are exported and may be used by any other component.
333 */
334
335/* ignore the current line */
336int acl_parse_nothing(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreaua5909832007-06-17 20:40:25 +0200337{
Willy Tarreau58393e12008-07-20 10:39:22 +0200338 return 1;
339}
340
341/* always fake a data retrieval */
342int acl_fetch_nothing(struct proxy *px, struct session *l4, void *l7, int dir,
343 struct acl_expr *expr, struct acl_test *test)
344{
345 return 1;
Willy Tarreaua5909832007-06-17 20:40:25 +0200346}
347
348/* always return false */
Willy Tarreau58393e12008-07-20 10:39:22 +0200349int acl_match_nothing(struct acl_test *test, struct acl_pattern *pattern)
Willy Tarreaua5909832007-06-17 20:40:25 +0200350{
Willy Tarreau11382812008-07-09 16:18:21 +0200351 return ACL_PAT_FAIL;
Willy Tarreaua5909832007-06-17 20:40:25 +0200352}
353
354
Willy Tarreaua84d3742007-05-07 00:36:48 +0200355/* NB: For two strings to be identical, it is required that their lengths match */
356int acl_match_str(struct acl_test *test, struct acl_pattern *pattern)
357{
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200358 int icase;
359
Willy Tarreaua84d3742007-05-07 00:36:48 +0200360 if (pattern->len != test->len)
Willy Tarreau11382812008-07-09 16:18:21 +0200361 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200362
363 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
364 if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) == 0) ||
365 (!icase && strncmp(pattern->ptr.str, test->ptr, test->len) == 0))
Willy Tarreau11382812008-07-09 16:18:21 +0200366 return ACL_PAT_PASS;
367 return ACL_PAT_FAIL;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200368}
369
Willy Tarreauc4262962010-05-10 23:42:40 +0200370/* Lookup a string in the expression's pattern tree. The node is returned if it
371 * exists, otherwise NULL.
372 */
373void *acl_lookup_str(struct acl_test *test, struct acl_expr *expr)
374{
375 /* data are stored in a tree */
376 struct ebmb_node *node;
377 char prev;
378
379 /* we may have to force a trailing zero on the test pattern */
380 prev = test->ptr[test->len];
381 if (prev)
382 test->ptr[test->len] = '\0';
383 node = ebst_lookup(&expr->pattern_tree, test->ptr);
384 if (prev)
385 test->ptr[test->len] = prev;
386 return node;
387}
388
Willy Tarreauf3d25982007-05-08 22:45:09 +0200389/* Executes a regex. It needs to change the data. If it is marked READ_ONLY
390 * then it will be allocated and duplicated in place so that others may use
391 * it later on. Note that this is embarrassing because we always try to avoid
392 * allocating memory at run time.
393 */
394int acl_match_reg(struct acl_test *test, struct acl_pattern *pattern)
395{
396 char old_char;
397 int ret;
398
399 if (unlikely(test->flags & ACL_TEST_F_READ_ONLY)) {
400 char *new_str;
401
402 new_str = calloc(1, test->len + 1);
403 if (!new_str)
Willy Tarreau11382812008-07-09 16:18:21 +0200404 return ACL_PAT_FAIL;
Willy Tarreauf3d25982007-05-08 22:45:09 +0200405
406 memcpy(new_str, test->ptr, test->len);
407 new_str[test->len] = 0;
408 if (test->flags & ACL_TEST_F_MUST_FREE)
409 free(test->ptr);
410 test->ptr = new_str;
411 test->flags |= ACL_TEST_F_MUST_FREE;
412 test->flags &= ~ACL_TEST_F_READ_ONLY;
413 }
414
415 old_char = test->ptr[test->len];
416 test->ptr[test->len] = 0;
417
418 if (regexec(pattern->ptr.reg, test->ptr, 0, NULL, 0) == 0)
Willy Tarreau11382812008-07-09 16:18:21 +0200419 ret = ACL_PAT_PASS;
Willy Tarreauf3d25982007-05-08 22:45:09 +0200420 else
Willy Tarreau11382812008-07-09 16:18:21 +0200421 ret = ACL_PAT_FAIL;
Willy Tarreauf3d25982007-05-08 22:45:09 +0200422
423 test->ptr[test->len] = old_char;
424 return ret;
425}
426
Willy Tarreaua84d3742007-05-07 00:36:48 +0200427/* Checks that the pattern matches the beginning of the tested string. */
428int acl_match_beg(struct acl_test *test, struct acl_pattern *pattern)
429{
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200430 int icase;
431
Willy Tarreaua84d3742007-05-07 00:36:48 +0200432 if (pattern->len > test->len)
Willy Tarreau11382812008-07-09 16:18:21 +0200433 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200434
435 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
436 if ((icase && strncasecmp(pattern->ptr.str, test->ptr, pattern->len) != 0) ||
437 (!icase && strncmp(pattern->ptr.str, test->ptr, pattern->len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +0200438 return ACL_PAT_FAIL;
439 return ACL_PAT_PASS;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200440}
441
442/* Checks that the pattern matches the end of the tested string. */
443int acl_match_end(struct acl_test *test, struct acl_pattern *pattern)
444{
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200445 int icase;
446
Willy Tarreaua84d3742007-05-07 00:36:48 +0200447 if (pattern->len > test->len)
Willy Tarreau11382812008-07-09 16:18:21 +0200448 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200449 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
450 if ((icase && strncasecmp(pattern->ptr.str, test->ptr + test->len - pattern->len, pattern->len) != 0) ||
451 (!icase && strncmp(pattern->ptr.str, test->ptr + test->len - pattern->len, pattern->len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +0200452 return ACL_PAT_FAIL;
453 return ACL_PAT_PASS;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200454}
455
456/* Checks that the pattern is included inside the tested string.
457 * NB: Suboptimal, should be rewritten using a Boyer-Moore method.
458 */
459int acl_match_sub(struct acl_test *test, struct acl_pattern *pattern)
460{
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200461 int icase;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200462 char *end;
463 char *c;
464
465 if (pattern->len > test->len)
Willy Tarreau11382812008-07-09 16:18:21 +0200466 return ACL_PAT_FAIL;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200467
468 end = test->ptr + test->len - pattern->len;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200469 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
470 if (icase) {
471 for (c = test->ptr; c <= end; c++) {
472 if (tolower(*c) != tolower(*pattern->ptr.str))
473 continue;
474 if (strncasecmp(pattern->ptr.str, c, pattern->len) == 0)
Willy Tarreau11382812008-07-09 16:18:21 +0200475 return ACL_PAT_PASS;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200476 }
477 } else {
478 for (c = test->ptr; c <= end; c++) {
479 if (*c != *pattern->ptr.str)
480 continue;
481 if (strncmp(pattern->ptr.str, c, pattern->len) == 0)
Willy Tarreau11382812008-07-09 16:18:21 +0200482 return ACL_PAT_PASS;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200483 }
Willy Tarreaua84d3742007-05-07 00:36:48 +0200484 }
Willy Tarreau11382812008-07-09 16:18:21 +0200485 return ACL_PAT_FAIL;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200486}
487
488/* This one is used by other real functions. It checks that the pattern is
489 * included inside the tested string, but enclosed between the specified
490 * delimitor, or a '/' or a '?' or at the beginning or end of the string.
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200491 * The delimitor is stripped at the beginning or end of the pattern.
Willy Tarreaua84d3742007-05-07 00:36:48 +0200492 */
493static int match_word(struct acl_test *test, struct acl_pattern *pattern, char delim)
494{
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200495 int may_match, icase;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200496 char *c, *end;
497 char *ps;
498 int pl;
499
500 pl = pattern->len;
501 ps = pattern->ptr.str;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200502 while (pl > 0 && (*ps == delim || *ps == '/' || *ps == '?')) {
Willy Tarreaua84d3742007-05-07 00:36:48 +0200503 pl--;
504 ps++;
505 }
506
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200507 while (pl > 0 &&
508 (ps[pl - 1] == delim || ps[pl - 1] == '/' || ps[pl - 1] == '?'))
Willy Tarreaua84d3742007-05-07 00:36:48 +0200509 pl--;
510
511 if (pl > test->len)
Willy Tarreau11382812008-07-09 16:18:21 +0200512 return ACL_PAT_FAIL;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200513
514 may_match = 1;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200515 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200516 end = test->ptr + test->len - pl;
517 for (c = test->ptr; c <= end; c++) {
518 if (*c == '/' || *c == delim || *c == '?') {
519 may_match = 1;
520 continue;
521 }
Willy Tarreaua84d3742007-05-07 00:36:48 +0200522
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200523 if (!may_match)
524 continue;
525
526 if (icase) {
527 if ((tolower(*c) == tolower(*ps)) &&
528 (strncasecmp(ps, c, pl) == 0) &&
529 (c == end || c[pl] == '/' || c[pl] == delim || c[pl] == '?'))
Willy Tarreau11382812008-07-09 16:18:21 +0200530 return ACL_PAT_PASS;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200531 } else {
532 if ((*c == *ps) &&
533 (strncmp(ps, c, pl) == 0) &&
534 (c == end || c[pl] == '/' || c[pl] == delim || c[pl] == '?'))
Willy Tarreau11382812008-07-09 16:18:21 +0200535 return ACL_PAT_PASS;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200536 }
Willy Tarreaua84d3742007-05-07 00:36:48 +0200537 may_match = 0;
538 }
Willy Tarreau11382812008-07-09 16:18:21 +0200539 return ACL_PAT_FAIL;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200540}
541
542/* Checks that the pattern is included inside the tested string, but enclosed
543 * between slashes or at the beginning or end of the string. Slashes at the
544 * beginning or end of the pattern are ignored.
545 */
546int acl_match_dir(struct acl_test *test, struct acl_pattern *pattern)
547{
548 return match_word(test, pattern, '/');
549}
550
551/* Checks that the pattern is included inside the tested string, but enclosed
552 * between dots or at the beginning or end of the string. Dots at the beginning
553 * or end of the pattern are ignored.
554 */
555int acl_match_dom(struct acl_test *test, struct acl_pattern *pattern)
556{
557 return match_word(test, pattern, '.');
558}
559
560/* Checks that the integer in <test> is included between min and max */
Willy Tarreauae8b7962007-06-09 23:10:04 +0200561int acl_match_int(struct acl_test *test, struct acl_pattern *pattern)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200562{
Willy Tarreauae8b7962007-06-09 23:10:04 +0200563 if ((!pattern->val.range.min_set || pattern->val.range.min <= test->i) &&
564 (!pattern->val.range.max_set || test->i <= pattern->val.range.max))
Willy Tarreau11382812008-07-09 16:18:21 +0200565 return ACL_PAT_PASS;
566 return ACL_PAT_FAIL;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200567}
568
Willy Tarreaua67fad92007-05-08 19:50:09 +0200569int acl_match_ip(struct acl_test *test, struct acl_pattern *pattern)
570{
571 struct in_addr *s;
572
573 if (test->i != AF_INET)
Willy Tarreau11382812008-07-09 16:18:21 +0200574 return ACL_PAT_FAIL;
Willy Tarreaua67fad92007-05-08 19:50:09 +0200575
576 s = (void *)test->ptr;
577 if (((s->s_addr ^ pattern->val.ipv4.addr.s_addr) & pattern->val.ipv4.mask.s_addr) == 0)
Willy Tarreau11382812008-07-09 16:18:21 +0200578 return ACL_PAT_PASS;
579 return ACL_PAT_FAIL;
Willy Tarreaua67fad92007-05-08 19:50:09 +0200580}
581
Willy Tarreaub337b532010-05-13 20:03:41 +0200582/* Lookup an IPv4 address in the expression's pattern tree using the longest
583 * match method. The node is returned if it exists, otherwise NULL.
584 */
585void *acl_lookup_ip(struct acl_test *test, struct acl_expr *expr)
586{
587 struct in_addr *s;
588
589 if (test->i != AF_INET)
590 return ACL_PAT_FAIL;
591
592 s = (void *)test->ptr;
593
594 return ebmb_lookup_longest(&expr->pattern_tree, &s->s_addr);
595}
596
Willy Tarreaua84d3742007-05-07 00:36:48 +0200597/* Parse a string. It is allocated and duplicated. */
Willy Tarreauae8b7962007-06-09 23:10:04 +0200598int acl_parse_str(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200599{
600 int len;
601
Willy Tarreauae8b7962007-06-09 23:10:04 +0200602 len = strlen(*text);
Willy Tarreauc4262962010-05-10 23:42:40 +0200603
604 if (pattern->flags & ACL_PAT_F_TREE_OK) {
605 /* we're allowed to put the data in a tree whose root is pointed
606 * to by val.tree.
607 */
608 struct ebmb_node *node;
609
610 node = calloc(1, sizeof(*node) + len + 1);
611 if (!node)
612 return 0;
613 memcpy(node->key, *text, len + 1);
614 if (ebst_insert(pattern->val.tree, node) != node)
615 free(node); /* was a duplicate */
616 pattern->flags |= ACL_PAT_F_TREE; /* this pattern now contains a tree */
617 return 1;
618 }
619
Willy Tarreauae8b7962007-06-09 23:10:04 +0200620 pattern->ptr.str = strdup(*text);
Willy Tarreaua84d3742007-05-07 00:36:48 +0200621 if (!pattern->ptr.str)
622 return 0;
623 pattern->len = len;
624 return 1;
625}
626
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +0100627/* Parse and concatenate all further strings into one. */
628int
629acl_parse_strcat(const char **text, struct acl_pattern *pattern, int *opaque)
630{
631
632 int len = 0, i;
633 char *s;
634
635 for (i = 0; *text[i]; i++)
636 len += strlen(text[i])+1;
637
638 pattern->ptr.str = s = calloc(1, len);
639 if (!pattern->ptr.str)
640 return 0;
641
642 for (i = 0; *text[i]; i++)
643 s += sprintf(s, i?" %s":"%s", text[i]);
644
645 pattern->len = len;
646
647 return i;
648}
649
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +0200650/* Free data allocated by acl_parse_reg */
651static void acl_free_reg(void *ptr) {
652
653 regfree((regex_t *)ptr);
654}
655
Willy Tarreauf3d25982007-05-08 22:45:09 +0200656/* Parse a regex. It is allocated. */
Willy Tarreauae8b7962007-06-09 23:10:04 +0200657int acl_parse_reg(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreauf3d25982007-05-08 22:45:09 +0200658{
659 regex_t *preg;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200660 int icase;
Willy Tarreauf3d25982007-05-08 22:45:09 +0200661
662 preg = calloc(1, sizeof(regex_t));
663
664 if (!preg)
665 return 0;
666
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200667 icase = (pattern->flags & ACL_PAT_F_IGNORE_CASE) ? REG_ICASE : 0;
668 if (regcomp(preg, *text, REG_EXTENDED | REG_NOSUB | icase) != 0) {
Willy Tarreauf3d25982007-05-08 22:45:09 +0200669 free(preg);
670 return 0;
671 }
672
673 pattern->ptr.reg = preg;
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +0200674 pattern->freeptrbuf = &acl_free_reg;
Willy Tarreauf3d25982007-05-08 22:45:09 +0200675 return 1;
676}
677
Willy Tarreauae8b7962007-06-09 23:10:04 +0200678/* Parse a range of positive integers delimited by either ':' or '-'. If only
679 * one integer is read, it is set as both min and max. An operator may be
680 * specified as the prefix, among this list of 5 :
681 *
682 * 0:eq, 1:gt, 2:ge, 3:lt, 4:le
683 *
684 * The default operator is "eq". It supports range matching. Ranges are
685 * rejected for other operators. The operator may be changed at any time.
686 * The operator is stored in the 'opaque' argument.
687 *
Willy Tarreaua84d3742007-05-07 00:36:48 +0200688 */
Willy Tarreauae8b7962007-06-09 23:10:04 +0200689int acl_parse_int(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200690{
Willy Tarreauae8b7962007-06-09 23:10:04 +0200691 signed long long i;
692 unsigned int j, last, skip = 0;
693 const char *ptr = *text;
694
695
Willy Tarreau8f8e6452007-06-17 21:51:38 +0200696 while (!isdigit((unsigned char)*ptr)) {
Willy Tarreau1c7cc5b2010-07-18 10:46:33 +0200697 switch (get_std_op(ptr)) {
698 case STD_OP_EQ: *opaque = 0; break;
699 case STD_OP_GT: *opaque = 1; break;
700 case STD_OP_GE: *opaque = 2; break;
701 case STD_OP_LT: *opaque = 3; break;
702 case STD_OP_LE: *opaque = 4; break;
703 default:
Willy Tarreauae8b7962007-06-09 23:10:04 +0200704 return 0;
Willy Tarreau1c7cc5b2010-07-18 10:46:33 +0200705 }
Willy Tarreauae8b7962007-06-09 23:10:04 +0200706
707 skip++;
708 ptr = text[skip];
709 }
Willy Tarreaua84d3742007-05-07 00:36:48 +0200710
711 last = i = 0;
712 while (1) {
Willy Tarreauae8b7962007-06-09 23:10:04 +0200713 j = *ptr++;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200714 if ((j == '-' || j == ':') && !last) {
715 last++;
716 pattern->val.range.min = i;
717 i = 0;
718 continue;
719 }
720 j -= '0';
721 if (j > 9)
722 // also catches the terminating zero
723 break;
724 i *= 10;
725 i += j;
726 }
Willy Tarreauae8b7962007-06-09 23:10:04 +0200727
728 if (last && *opaque >= 1 && *opaque <= 4)
729 /* having a range with a min or a max is absurd */
730 return 0;
731
Willy Tarreaua84d3742007-05-07 00:36:48 +0200732 if (!last)
733 pattern->val.range.min = i;
734 pattern->val.range.max = i;
Willy Tarreauae8b7962007-06-09 23:10:04 +0200735
736 switch (*opaque) {
737 case 0: /* eq */
738 pattern->val.range.min_set = 1;
739 pattern->val.range.max_set = 1;
740 break;
741 case 1: /* gt */
742 pattern->val.range.min++; /* gt = ge + 1 */
743 case 2: /* ge */
744 pattern->val.range.min_set = 1;
745 pattern->val.range.max_set = 0;
746 break;
747 case 3: /* lt */
748 pattern->val.range.max--; /* lt = le - 1 */
749 case 4: /* le */
750 pattern->val.range.min_set = 0;
751 pattern->val.range.max_set = 1;
752 break;
753 }
754 return skip + 1;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200755}
756
Willy Tarreau4a26d2f2008-07-15 16:05:33 +0200757/* Parse a range of positive 2-component versions delimited by either ':' or
758 * '-'. The version consists in a major and a minor, both of which must be
759 * smaller than 65536, because internally they will be represented as a 32-bit
760 * integer.
761 * If only one version is read, it is set as both min and max. Just like for
762 * pure integers, an operator may be specified as the prefix, among this list
763 * of 5 :
764 *
765 * 0:eq, 1:gt, 2:ge, 3:lt, 4:le
766 *
767 * The default operator is "eq". It supports range matching. Ranges are
768 * rejected for other operators. The operator may be changed at any time.
769 * The operator is stored in the 'opaque' argument. This allows constructs
770 * such as the following one :
771 *
772 * acl obsolete_ssl ssl_req_proto lt 3
773 * acl unsupported_ssl ssl_req_proto gt 3.1
774 * acl valid_ssl ssl_req_proto 3.0-3.1
775 *
776 */
777int acl_parse_dotted_ver(const char **text, struct acl_pattern *pattern, int *opaque)
778{
779 signed long long i;
780 unsigned int j, last, skip = 0;
781 const char *ptr = *text;
782
783
784 while (!isdigit((unsigned char)*ptr)) {
Willy Tarreau1c7cc5b2010-07-18 10:46:33 +0200785 switch (get_std_op(ptr)) {
786 case STD_OP_EQ: *opaque = 0; break;
787 case STD_OP_GT: *opaque = 1; break;
788 case STD_OP_GE: *opaque = 2; break;
789 case STD_OP_LT: *opaque = 3; break;
790 case STD_OP_LE: *opaque = 4; break;
791 default:
Willy Tarreau4a26d2f2008-07-15 16:05:33 +0200792 return 0;
Willy Tarreau1c7cc5b2010-07-18 10:46:33 +0200793 }
Willy Tarreau4a26d2f2008-07-15 16:05:33 +0200794
795 skip++;
796 ptr = text[skip];
797 }
798
799 last = i = 0;
800 while (1) {
801 j = *ptr++;
802 if (j == '.') {
803 /* minor part */
804 if (i >= 65536)
805 return 0;
806 i <<= 16;
807 continue;
808 }
809 if ((j == '-' || j == ':') && !last) {
810 last++;
811 if (i < 65536)
812 i <<= 16;
813 pattern->val.range.min = i;
814 i = 0;
815 continue;
816 }
817 j -= '0';
818 if (j > 9)
819 // also catches the terminating zero
820 break;
821 i = (i & 0xFFFF0000) + (i & 0xFFFF) * 10;
822 i += j;
823 }
824
825 /* if we only got a major version, let's shift it now */
826 if (i < 65536)
827 i <<= 16;
828
829 if (last && *opaque >= 1 && *opaque <= 4)
830 /* having a range with a min or a max is absurd */
831 return 0;
832
833 if (!last)
834 pattern->val.range.min = i;
835 pattern->val.range.max = i;
836
837 switch (*opaque) {
838 case 0: /* eq */
839 pattern->val.range.min_set = 1;
840 pattern->val.range.max_set = 1;
841 break;
842 case 1: /* gt */
843 pattern->val.range.min++; /* gt = ge + 1 */
844 case 2: /* ge */
845 pattern->val.range.min_set = 1;
846 pattern->val.range.max_set = 0;
847 break;
848 case 3: /* lt */
849 pattern->val.range.max--; /* lt = le - 1 */
850 case 4: /* le */
851 pattern->val.range.min_set = 0;
852 pattern->val.range.max_set = 1;
853 break;
854 }
855 return skip + 1;
856}
857
Willy Tarreaua67fad92007-05-08 19:50:09 +0200858/* Parse an IP address and an optional mask in the form addr[/mask].
859 * The addr may either be an IPv4 address or a hostname. The mask
860 * may either be a dotted mask or a number of bits. Returns 1 if OK,
861 * otherwise 0.
862 */
Willy Tarreauae8b7962007-06-09 23:10:04 +0200863int acl_parse_ip(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreaua67fad92007-05-08 19:50:09 +0200864{
Willy Tarreaub337b532010-05-13 20:03:41 +0200865 struct eb_root *tree = NULL;
866 if (pattern->flags & ACL_PAT_F_TREE_OK)
867 tree = pattern->val.tree;
868
869 if (str2net(*text, &pattern->val.ipv4.addr, &pattern->val.ipv4.mask)) {
870 unsigned int mask = ntohl(pattern->val.ipv4.mask.s_addr);
871 struct ebmb_node *node;
872 /* check if the mask is contiguous so that we can insert the
873 * network into the tree. A continuous mask has only ones on
874 * the left. This means that this mask + its lower bit added
875 * once again is null.
876 */
877 if (mask + (mask & -mask) == 0 && tree) {
878 mask = mask ? 33 - flsnz(mask & -mask) : 0; /* equals cidr value */
879 /* FIXME: insert <addr>/<mask> into the tree here */
880 node = calloc(1, sizeof(*node) + 4); /* reserve 4 bytes for IPv4 address */
881 if (!node)
882 return 0;
883 memcpy(node->key, &pattern->val.ipv4.addr, 4); /* network byte order */
884 node->node.pfx = mask;
885 if (ebmb_insert_prefix(tree, node, 4) != node)
886 free(node); /* was a duplicate */
887 pattern->flags |= ACL_PAT_F_TREE;
888 return 1;
889 }
Willy Tarreauae8b7962007-06-09 23:10:04 +0200890 return 1;
Willy Tarreaub337b532010-05-13 20:03:41 +0200891 }
Willy Tarreauae8b7962007-06-09 23:10:04 +0200892 else
893 return 0;
Willy Tarreaua67fad92007-05-08 19:50:09 +0200894}
895
Willy Tarreaua84d3742007-05-07 00:36:48 +0200896/*
897 * Registers the ACL keyword list <kwl> as a list of valid keywords for next
898 * parsing sessions.
899 */
900void acl_register_keywords(struct acl_kw_list *kwl)
901{
902 LIST_ADDQ(&acl_keywords.list, &kwl->list);
903}
904
905/*
906 * Unregisters the ACL keyword list <kwl> from the list of valid keywords.
907 */
908void acl_unregister_keywords(struct acl_kw_list *kwl)
909{
910 LIST_DEL(&kwl->list);
911 LIST_INIT(&kwl->list);
912}
913
914/* Return a pointer to the ACL <name> within the list starting at <head>, or
915 * NULL if not found.
916 */
917struct acl *find_acl_by_name(const char *name, struct list *head)
918{
919 struct acl *acl;
920 list_for_each_entry(acl, head, list) {
921 if (strcmp(acl->name, name) == 0)
922 return acl;
923 }
924 return NULL;
925}
926
927/* Return a pointer to the ACL keyword <kw>, or NULL if not found. Note that if
928 * <kw> contains an opening parenthesis, only the left part of it is checked.
929 */
930struct acl_keyword *find_acl_kw(const char *kw)
931{
932 int index;
933 const char *kwend;
934 struct acl_kw_list *kwl;
935
936 kwend = strchr(kw, '(');
937 if (!kwend)
938 kwend = kw + strlen(kw);
939
940 list_for_each_entry(kwl, &acl_keywords.list, list) {
941 for (index = 0; kwl->kw[index].kw != NULL; index++) {
942 if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) &&
943 kwl->kw[index].kw[kwend-kw] == 0)
944 return &kwl->kw[index];
945 }
946 }
947 return NULL;
948}
949
950static void free_pattern(struct acl_pattern *pat)
951{
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +0200952
953 if (pat->ptr.ptr) {
954 if (pat->freeptrbuf)
955 pat->freeptrbuf(pat->ptr.ptr);
956
Willy Tarreaua84d3742007-05-07 00:36:48 +0200957 free(pat->ptr.ptr);
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +0200958 }
959
Willy Tarreaua84d3742007-05-07 00:36:48 +0200960 free(pat);
961}
962
963static void free_pattern_list(struct list *head)
964{
965 struct acl_pattern *pat, *tmp;
966 list_for_each_entry_safe(pat, tmp, head, list)
967 free_pattern(pat);
968}
969
Willy Tarreaue56cda92010-05-11 23:25:05 +0200970static void free_pattern_tree(struct eb_root *root)
971{
972 struct eb_node *node, *next;
973 node = eb_first(root);
974 while (node) {
975 next = eb_next(node);
976 free(node);
977 node = next;
978 }
979}
980
Willy Tarreaua84d3742007-05-07 00:36:48 +0200981static struct acl_expr *prune_acl_expr(struct acl_expr *expr)
982{
983 free_pattern_list(&expr->patterns);
Willy Tarreaue56cda92010-05-11 23:25:05 +0200984 free_pattern_tree(&expr->pattern_tree);
Willy Tarreaua84d3742007-05-07 00:36:48 +0200985 LIST_INIT(&expr->patterns);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +0100986 if (expr->arg_len && expr->arg.str)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200987 free(expr->arg.str);
988 expr->kw->use_cnt--;
989 return expr;
990}
991
Willy Tarreau2b5285d2010-05-09 23:45:24 +0200992static int acl_read_patterns_from_file( struct acl_keyword *aclkw,
993 struct acl_expr *expr,
994 const char *filename, int patflags)
995{
996 FILE *file;
997 char *c;
998 const char *args[2];
999 struct acl_pattern *pattern;
1000 int opaque;
1001
1002 file = fopen(filename, "r");
1003 if (!file)
1004 return 0;
1005
1006 /* now parse all patterns. The file may contain only one pattern per
1007 * line. If the line contains spaces, they will be part of the pattern.
1008 * The pattern stops at the first CR, LF or EOF encountered.
1009 */
1010 opaque = 0;
Willy Tarreaue56cda92010-05-11 23:25:05 +02001011 pattern = NULL;
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001012 args[1] = "";
1013 while (fgets(trash, sizeof(trash), file) != NULL) {
1014
1015 c = trash;
Willy Tarreau58215a02010-05-13 22:07:43 +02001016
1017 /* ignore lines beginning with a dash */
1018 if (*c == '#')
1019 continue;
1020
1021 /* strip leading spaces and tabs */
1022 while (*c == ' ' || *c == '\t')
1023 c++;
1024
1025 /* empty lines are ignored too */
1026 if (!*c)
1027 continue;
1028
1029 args[0] = c;
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001030 while (*c && *c != '\n' && *c != '\r')
1031 c++;
1032 *c = 0;
1033
Willy Tarreaue56cda92010-05-11 23:25:05 +02001034 /* we keep the previous pattern along iterations as long as it's not used */
1035 if (!pattern)
1036 pattern = (struct acl_pattern *)malloc(sizeof(*pattern));
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001037 if (!pattern)
1038 goto out_close;
Willy Tarreaue56cda92010-05-11 23:25:05 +02001039
1040 memset(pattern, 0, sizeof(*pattern));
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001041 pattern->flags = patflags;
1042
Willy Tarreaue56cda92010-05-11 23:25:05 +02001043 if ((aclkw->requires & ACL_MAY_LOOKUP) && !(pattern->flags & ACL_PAT_F_IGNORE_CASE)) {
1044 /* we pre-set the data pointer to the tree's head so that functions
1045 * which are able to insert in a tree know where to do that.
1046 */
1047 pattern->flags |= ACL_PAT_F_TREE_OK;
1048 pattern->val.tree = &expr->pattern_tree;
1049 }
1050
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001051 if (!aclkw->parse(args, pattern, &opaque))
1052 goto out_free_pattern;
Willy Tarreaue56cda92010-05-11 23:25:05 +02001053
1054 /* if the parser did not feed the tree, let's chain the pattern to the list */
1055 if (!(pattern->flags & ACL_PAT_F_TREE)) {
1056 LIST_ADDQ(&expr->patterns, &pattern->list);
1057 pattern = NULL; /* get a new one */
1058 }
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001059 }
Willy Tarreaue56cda92010-05-11 23:25:05 +02001060 if (pattern)
1061 free_pattern(pattern);
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001062 return 1;
1063
1064 out_free_pattern:
1065 free_pattern(pattern);
1066 out_close:
1067 fclose(file);
1068 return 0;
1069}
1070
Willy Tarreaua84d3742007-05-07 00:36:48 +02001071/* Parse an ACL expression starting at <args>[0], and return it.
1072 * Right now, the only accepted syntax is :
1073 * <subject> [<value>...]
1074 */
1075struct acl_expr *parse_acl_expr(const char **args)
1076{
1077 __label__ out_return, out_free_expr, out_free_pattern;
1078 struct acl_expr *expr;
1079 struct acl_keyword *aclkw;
1080 struct acl_pattern *pattern;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02001081 int opaque, patflags;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001082 const char *arg;
1083
1084 aclkw = find_acl_kw(args[0]);
1085 if (!aclkw || !aclkw->parse)
1086 goto out_return;
1087
1088 expr = (struct acl_expr *)calloc(1, sizeof(*expr));
1089 if (!expr)
1090 goto out_return;
1091
1092 expr->kw = aclkw;
1093 aclkw->use_cnt++;
1094 LIST_INIT(&expr->patterns);
Willy Tarreaue56cda92010-05-11 23:25:05 +02001095 expr->pattern_tree = EB_ROOT_UNIQUE;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001096 expr->arg.str = NULL;
Willy Tarreaubb768912007-06-10 11:17:01 +02001097 expr->arg_len = 0;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001098
1099 arg = strchr(args[0], '(');
1100 if (arg != NULL) {
1101 char *end, *arg2;
1102 /* there is an argument in the form "subject(arg)" */
1103 arg++;
1104 end = strchr(arg, ')');
1105 if (!end)
1106 goto out_free_expr;
Willy Tarreauac778f52010-01-26 19:02:46 +01001107 arg2 = my_strndup(arg, end - arg);
Willy Tarreau1edb1442010-01-27 20:13:38 +01001108 if (!arg2)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001109 goto out_free_expr;
Willy Tarreaubb768912007-06-10 11:17:01 +02001110 expr->arg_len = end - arg;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001111 expr->arg.str = arg2;
1112 }
1113
Willy Tarreaua84d3742007-05-07 00:36:48 +02001114 args++;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02001115
1116 /* check for options before patterns. Supported options are :
1117 * -i : ignore case for all patterns by default
1118 * -f : read patterns from those files
1119 * -- : everything after this is not an option
1120 */
1121 patflags = 0;
1122 while (**args == '-') {
1123 if ((*args)[1] == 'i')
1124 patflags |= ACL_PAT_F_IGNORE_CASE;
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001125 else if ((*args)[1] == 'f') {
1126 if (!acl_read_patterns_from_file(aclkw, expr, args[1], patflags | ACL_PAT_F_FROM_FILE))
1127 goto out_free_expr;
1128 args++;
1129 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +02001130 else if ((*args)[1] == '-') {
1131 args++;
1132 break;
1133 }
1134 else
1135 break;
1136 args++;
1137 }
1138
1139 /* now parse all patterns */
Willy Tarreauae8b7962007-06-09 23:10:04 +02001140 opaque = 0;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001141 while (**args) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02001142 int ret;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001143 pattern = (struct acl_pattern *)calloc(1, sizeof(*pattern));
1144 if (!pattern)
1145 goto out_free_expr;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02001146 pattern->flags = patflags;
1147
Willy Tarreauae8b7962007-06-09 23:10:04 +02001148 ret = aclkw->parse(args, pattern, &opaque);
1149 if (!ret)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001150 goto out_free_pattern;
1151 LIST_ADDQ(&expr->patterns, &pattern->list);
Willy Tarreauae8b7962007-06-09 23:10:04 +02001152 args += ret;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001153 }
1154
1155 return expr;
1156
1157 out_free_pattern:
1158 free_pattern(pattern);
1159 out_free_expr:
1160 prune_acl_expr(expr);
1161 free(expr);
1162 out_return:
1163 return NULL;
1164}
1165
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02001166/* Purge everything in the acl <acl>, then return <acl>. */
1167struct acl *prune_acl(struct acl *acl) {
1168
1169 struct acl_expr *expr, *exprb;
1170
1171 free(acl->name);
1172
1173 list_for_each_entry_safe(expr, exprb, &acl->expr, list) {
1174 LIST_DEL(&expr->list);
1175 prune_acl_expr(expr);
1176 free(expr);
1177 }
1178
1179 return acl;
1180}
1181
Willy Tarreaua84d3742007-05-07 00:36:48 +02001182/* Parse an ACL with the name starting at <args>[0], and with a list of already
1183 * known ACLs in <acl>. If the ACL was not in the list, it will be added.
Willy Tarreau2a56c5e2010-03-15 16:13:29 +01001184 * A pointer to that ACL is returned. If the ACL has an empty name, then it's
1185 * an anonymous one and it won't be merged with any other one.
Willy Tarreaua84d3742007-05-07 00:36:48 +02001186 *
1187 * args syntax: <aclname> <acl_expr>
1188 */
1189struct acl *parse_acl(const char **args, struct list *known_acl)
1190{
1191 __label__ out_return, out_free_acl_expr, out_free_name;
1192 struct acl *cur_acl;
1193 struct acl_expr *acl_expr;
1194 char *name;
1195
Willy Tarreau2a56c5e2010-03-15 16:13:29 +01001196 if (**args && invalid_char(*args))
Willy Tarreau2e74c3f2007-12-02 18:45:09 +01001197 goto out_return;
1198
Willy Tarreaua84d3742007-05-07 00:36:48 +02001199 acl_expr = parse_acl_expr(args + 1);
1200 if (!acl_expr)
1201 goto out_return;
1202
Willy Tarreau404e8ab2009-07-26 19:40:40 +02001203 /* Check for args beginning with an opening parenthesis just after the
1204 * subject, as this is almost certainly a typo. Right now we can only
1205 * emit a warning, so let's do so.
1206 */
Krzysztof Piotr Oledzki4cdd8312009-10-05 00:23:35 +02001207 if (!strchr(args[1], '(') && *args[2] == '(')
Willy Tarreau404e8ab2009-07-26 19:40:40 +02001208 Warning("parsing acl '%s' :\n"
1209 " matching '%s' for pattern '%s' is likely a mistake and probably\n"
1210 " not what you want. Maybe you need to remove the extraneous space before '('.\n"
1211 " If you are really sure this is not an error, please insert '--' between the\n"
1212 " match and the pattern to make this warning message disappear.\n",
1213 args[0], args[1], args[2]);
1214
Willy Tarreau2a56c5e2010-03-15 16:13:29 +01001215 if (*args[0])
1216 cur_acl = find_acl_by_name(args[0], known_acl);
1217 else
1218 cur_acl = NULL;
1219
Willy Tarreaua84d3742007-05-07 00:36:48 +02001220 if (!cur_acl) {
1221 name = strdup(args[0]);
1222 if (!name)
1223 goto out_free_acl_expr;
1224 cur_acl = (struct acl *)calloc(1, sizeof(*cur_acl));
1225 if (cur_acl == NULL)
1226 goto out_free_name;
1227
1228 LIST_INIT(&cur_acl->expr);
1229 LIST_ADDQ(known_acl, &cur_acl->list);
1230 cur_acl->name = name;
1231 }
1232
Willy Tarreaua9802632008-07-25 19:13:19 +02001233 cur_acl->requires |= acl_expr->kw->requires;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001234 LIST_ADDQ(&cur_acl->expr, &acl_expr->list);
1235 return cur_acl;
1236
1237 out_free_name:
1238 free(name);
1239 out_free_acl_expr:
1240 prune_acl_expr(acl_expr);
1241 free(acl_expr);
1242 out_return:
1243 return NULL;
1244}
1245
Willy Tarreau16fbe822007-06-17 11:54:31 +02001246/* Some useful ACLs provided by default. Only those used are allocated. */
1247
1248const struct {
1249 const char *name;
1250 const char *expr[4]; /* put enough for longest expression */
1251} default_acl_list[] = {
Willy Tarreau58393e12008-07-20 10:39:22 +02001252 { .name = "TRUE", .expr = {"always_true",""}},
1253 { .name = "FALSE", .expr = {"always_false",""}},
Willy Tarreau16fbe822007-06-17 11:54:31 +02001254 { .name = "LOCALHOST", .expr = {"src","127.0.0.1/8",""}},
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001255 { .name = "HTTP", .expr = {"req_proto_http",""}},
Willy Tarreau16fbe822007-06-17 11:54:31 +02001256 { .name = "HTTP_1.0", .expr = {"req_ver","1.0",""}},
1257 { .name = "HTTP_1.1", .expr = {"req_ver","1.1",""}},
1258 { .name = "METH_CONNECT", .expr = {"method","CONNECT",""}},
1259 { .name = "METH_GET", .expr = {"method","GET","HEAD",""}},
1260 { .name = "METH_HEAD", .expr = {"method","HEAD",""}},
1261 { .name = "METH_OPTIONS", .expr = {"method","OPTIONS",""}},
1262 { .name = "METH_POST", .expr = {"method","POST",""}},
1263 { .name = "METH_TRACE", .expr = {"method","TRACE",""}},
1264 { .name = "HTTP_URL_ABS", .expr = {"url_reg","^[^/:]*://",""}},
1265 { .name = "HTTP_URL_SLASH", .expr = {"url_beg","/",""}},
1266 { .name = "HTTP_URL_STAR", .expr = {"url","*",""}},
1267 { .name = "HTTP_CONTENT", .expr = {"hdr_val(content-length)","gt","0",""}},
Emeric Brunbede3d02009-06-30 17:54:00 +02001268 { .name = "RDP_COOKIE", .expr = {"req_rdp_cookie_cnt","gt","0",""}},
Willy Tarreauc6317702008-07-20 09:29:50 +02001269 { .name = "REQ_CONTENT", .expr = {"req_len","gt","0",""}},
Willy Tarreaub6fb4202008-07-20 11:18:28 +02001270 { .name = "WAIT_END", .expr = {"wait_end",""}},
Willy Tarreau16fbe822007-06-17 11:54:31 +02001271 { .name = NULL, .expr = {""}}
1272};
1273
1274/* Find a default ACL from the default_acl list, compile it and return it.
1275 * If the ACL is not found, NULL is returned. In theory, it cannot fail,
1276 * except when default ACLs are broken, in which case it will return NULL.
1277 * If <known_acl> is not NULL, the ACL will be queued at its tail.
1278 */
1279struct acl *find_acl_default(const char *acl_name, struct list *known_acl)
1280{
1281 __label__ out_return, out_free_acl_expr, out_free_name;
1282 struct acl *cur_acl;
1283 struct acl_expr *acl_expr;
1284 char *name;
1285 int index;
1286
1287 for (index = 0; default_acl_list[index].name != NULL; index++) {
1288 if (strcmp(acl_name, default_acl_list[index].name) == 0)
1289 break;
1290 }
1291
1292 if (default_acl_list[index].name == NULL)
1293 return NULL;
1294
1295 acl_expr = parse_acl_expr((const char **)default_acl_list[index].expr);
1296 if (!acl_expr)
1297 goto out_return;
1298
1299 name = strdup(acl_name);
1300 if (!name)
1301 goto out_free_acl_expr;
1302 cur_acl = (struct acl *)calloc(1, sizeof(*cur_acl));
1303 if (cur_acl == NULL)
1304 goto out_free_name;
1305
1306 cur_acl->name = name;
Willy Tarreaua55b7dc2009-07-12 09:21:30 +02001307 cur_acl->requires |= acl_expr->kw->requires;
Willy Tarreau16fbe822007-06-17 11:54:31 +02001308 LIST_INIT(&cur_acl->expr);
1309 LIST_ADDQ(&cur_acl->expr, &acl_expr->list);
1310 if (known_acl)
1311 LIST_ADDQ(known_acl, &cur_acl->list);
1312
1313 return cur_acl;
1314
1315 out_free_name:
1316 free(name);
1317 out_free_acl_expr:
1318 prune_acl_expr(acl_expr);
1319 free(acl_expr);
1320 out_return:
1321 return NULL;
1322}
Willy Tarreaua84d3742007-05-07 00:36:48 +02001323
1324/* Purge everything in the acl_cond <cond>, then return <cond>. */
1325struct acl_cond *prune_acl_cond(struct acl_cond *cond)
1326{
1327 struct acl_term_suite *suite, *tmp_suite;
1328 struct acl_term *term, *tmp_term;
1329
1330 /* iterate through all term suites and free all terms and all suites */
1331 list_for_each_entry_safe(suite, tmp_suite, &cond->suites, list) {
1332 list_for_each_entry_safe(term, tmp_term, &suite->terms, list)
1333 free(term);
1334 free(suite);
1335 }
1336 return cond;
1337}
1338
1339/* Parse an ACL condition starting at <args>[0], relying on a list of already
1340 * known ACLs passed in <known_acl>. The new condition is returned (or NULL in
1341 * case of low memory). Supports multiple conditions separated by "or".
1342 */
1343struct acl_cond *parse_acl_cond(const char **args, struct list *known_acl, int pol)
1344{
1345 __label__ out_return, out_free_suite, out_free_term;
Willy Tarreau74b98a82007-06-16 19:35:18 +02001346 int arg, neg;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001347 const char *word;
1348 struct acl *cur_acl;
1349 struct acl_term *cur_term;
1350 struct acl_term_suite *cur_suite;
1351 struct acl_cond *cond;
1352
1353 cond = (struct acl_cond *)calloc(1, sizeof(*cond));
1354 if (cond == NULL)
1355 goto out_return;
1356
1357 LIST_INIT(&cond->list);
1358 LIST_INIT(&cond->suites);
1359 cond->pol = pol;
1360
1361 cur_suite = NULL;
Willy Tarreau74b98a82007-06-16 19:35:18 +02001362 neg = 0;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001363 for (arg = 0; *args[arg]; arg++) {
1364 word = args[arg];
1365
1366 /* remove as many exclamation marks as we can */
1367 while (*word == '!') {
1368 neg = !neg;
1369 word++;
1370 }
1371
1372 /* an empty word is allowed because we cannot force the user to
1373 * always think about not leaving exclamation marks alone.
1374 */
1375 if (!*word)
1376 continue;
1377
Willy Tarreau16fbe822007-06-17 11:54:31 +02001378 if (strcasecmp(word, "or") == 0 || strcmp(word, "||") == 0) {
Willy Tarreaua84d3742007-05-07 00:36:48 +02001379 /* new term suite */
1380 cur_suite = NULL;
1381 neg = 0;
1382 continue;
1383 }
1384
Willy Tarreau95fa4692010-02-01 13:05:50 +01001385 if (strcmp(word, "{") == 0) {
1386 /* we may have a complete ACL expression between two braces,
1387 * find the last one.
1388 */
1389 int arg_end = arg + 1;
1390 const char **args_new;
1391
1392 while (*args[arg_end] && strcmp(args[arg_end], "}") != 0)
1393 arg_end++;
1394
1395 if (!*args[arg_end])
1396 goto out_free_suite;
1397
1398 args_new = calloc(1, (arg_end - arg + 1) * sizeof(*args_new));
1399 if (!args_new)
1400 goto out_free_suite;
1401
Willy Tarreau2a56c5e2010-03-15 16:13:29 +01001402 args_new[0] = "";
Willy Tarreau95fa4692010-02-01 13:05:50 +01001403 memcpy(args_new + 1, args + arg + 1, (arg_end - arg) * sizeof(*args_new));
1404 args_new[arg_end - arg] = "";
1405 cur_acl = parse_acl(args_new, known_acl);
1406 free(args_new);
1407
1408 if (!cur_acl)
Willy Tarreau16fbe822007-06-17 11:54:31 +02001409 goto out_free_suite;
Willy Tarreau95fa4692010-02-01 13:05:50 +01001410 arg = arg_end;
1411 }
1412 else {
1413 /* search for <word> in the known ACL names. If we do not find
1414 * it, let's look for it in the default ACLs, and if found, add
1415 * it to the list of ACLs of this proxy. This makes it possible
1416 * to override them.
1417 */
1418 cur_acl = find_acl_by_name(word, known_acl);
1419 if (cur_acl == NULL) {
1420 cur_acl = find_acl_default(word, known_acl);
1421 if (cur_acl == NULL)
1422 goto out_free_suite;
1423 }
Willy Tarreau16fbe822007-06-17 11:54:31 +02001424 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001425
1426 cur_term = (struct acl_term *)calloc(1, sizeof(*cur_term));
1427 if (cur_term == NULL)
1428 goto out_free_suite;
1429
1430 cur_term->acl = cur_acl;
1431 cur_term->neg = neg;
Willy Tarreaua9802632008-07-25 19:13:19 +02001432 cond->requires |= cur_acl->requires;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001433
1434 if (!cur_suite) {
1435 cur_suite = (struct acl_term_suite *)calloc(1, sizeof(*cur_suite));
1436 if (cur_term == NULL)
1437 goto out_free_term;
1438 LIST_INIT(&cur_suite->terms);
1439 LIST_ADDQ(&cond->suites, &cur_suite->list);
1440 }
1441 LIST_ADDQ(&cur_suite->terms, &cur_term->list);
Willy Tarreau74b98a82007-06-16 19:35:18 +02001442 neg = 0;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001443 }
1444
1445 return cond;
1446
1447 out_free_term:
1448 free(cur_term);
1449 out_free_suite:
1450 prune_acl_cond(cond);
1451 free(cond);
1452 out_return:
1453 return NULL;
1454}
1455
Willy Tarreau2bbba412010-01-28 16:48:33 +01001456/* Builds an ACL condition starting at the if/unless keyword. The complete
1457 * condition is returned. NULL is returned in case of error or if the first
1458 * word is neither "if" nor "unless". It automatically sets the file name and
1459 * the line number in the condition for better error reporting, and adds the
1460 * ACL requirements to the proxy's acl_requires.
1461 */
1462struct acl_cond *build_acl_cond(const char *file, int line, struct proxy *px, const char **args)
1463{
1464 int pol = ACL_COND_NONE;
1465 struct acl_cond *cond = NULL;
1466
1467 if (!strcmp(*args, "if")) {
1468 pol = ACL_COND_IF;
1469 args++;
1470 }
1471 else if (!strcmp(*args, "unless")) {
1472 pol = ACL_COND_UNLESS;
1473 args++;
1474 }
1475 else
1476 return NULL;
1477
1478 cond = parse_acl_cond(args, &px->acl, pol);
1479 if (!cond)
1480 return NULL;
1481
1482 cond->file = file;
1483 cond->line = line;
1484 px->acl_requires |= cond->requires;
1485
1486 return cond;
1487}
1488
Willy Tarreau11382812008-07-09 16:18:21 +02001489/* Execute condition <cond> and return either ACL_PAT_FAIL, ACL_PAT_MISS or
Willy Tarreaub6866442008-07-14 23:54:42 +02001490 * ACL_PAT_PASS depending on the test results. ACL_PAT_MISS may only be
1491 * returned if <dir> contains ACL_PARTIAL, indicating that incomplete data
1492 * is being examined.
1493 * This function only computes the condition, it does not apply the polarity
1494 * required by IF/UNLESS, it's up to the caller to do this using something like
1495 * this :
Willy Tarreau11382812008-07-09 16:18:21 +02001496 *
1497 * res = acl_pass(res);
Willy Tarreaub6866442008-07-14 23:54:42 +02001498 * if (res == ACL_PAT_MISS)
1499 * return 0;
Willy Tarreau11382812008-07-09 16:18:21 +02001500 * if (cond->pol == ACL_COND_UNLESS)
1501 * res = !res;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001502 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02001503int acl_exec_cond(struct acl_cond *cond, struct proxy *px, struct session *l4, void *l7, int dir)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001504{
1505 __label__ fetch_next;
1506 struct acl_term_suite *suite;
1507 struct acl_term *term;
1508 struct acl_expr *expr;
1509 struct acl *acl;
1510 struct acl_pattern *pattern;
1511 struct acl_test test;
Willy Tarreau11382812008-07-09 16:18:21 +02001512 int acl_res, suite_res, cond_res;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001513
Willy Tarreau11382812008-07-09 16:18:21 +02001514 /* We're doing a logical OR between conditions so we initialize to FAIL.
1515 * The MISS status is propagated down from the suites.
1516 */
Willy Tarreaua84d3742007-05-07 00:36:48 +02001517 cond_res = ACL_PAT_FAIL;
1518 list_for_each_entry(suite, &cond->suites, list) {
Willy Tarreau11382812008-07-09 16:18:21 +02001519 /* Evaluate condition suite <suite>. We stop at the first term
1520 * which returns ACL_PAT_FAIL. The MISS status is still propagated
1521 * in case of uncertainty in the result.
Willy Tarreaua84d3742007-05-07 00:36:48 +02001522 */
1523
1524 /* we're doing a logical AND between terms, so we must set the
1525 * initial value to PASS.
1526 */
1527 suite_res = ACL_PAT_PASS;
1528 list_for_each_entry(term, &suite->terms, list) {
1529 acl = term->acl;
1530
1531 /* FIXME: use cache !
1532 * check acl->cache_idx for this.
1533 */
1534
1535 /* ACL result not cached. Let's scan all the expressions
1536 * and use the first one to match.
1537 */
1538 acl_res = ACL_PAT_FAIL;
1539 list_for_each_entry(expr, &acl->expr, list) {
Willy Tarreaud41f8d82007-06-10 10:06:18 +02001540 /* we need to reset context and flags */
1541 memset(&test, 0, sizeof(test));
Willy Tarreaua84d3742007-05-07 00:36:48 +02001542 fetch_next:
Willy Tarreaub6866442008-07-14 23:54:42 +02001543 if (!expr->kw->fetch(px, l4, l7, dir, expr, &test)) {
1544 /* maybe we could not fetch because of missing data */
1545 if (test.flags & ACL_TEST_F_MAY_CHANGE && dir & ACL_PARTIAL)
1546 acl_res |= ACL_PAT_MISS;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001547 continue;
Willy Tarreaub6866442008-07-14 23:54:42 +02001548 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001549
Willy Tarreaua79534f2008-07-20 10:13:37 +02001550 if (test.flags & ACL_TEST_F_RES_SET) {
1551 if (test.flags & ACL_TEST_F_RES_PASS)
1552 acl_res |= ACL_PAT_PASS;
1553 else
1554 acl_res |= ACL_PAT_FAIL;
1555 }
1556 else {
Willy Tarreau020534d2010-05-16 21:45:45 +02001557 if (!eb_is_empty(&expr->pattern_tree)) {
Willy Tarreauc4262962010-05-10 23:42:40 +02001558 /* a tree is present, let's check what type it is */
1559 if (expr->kw->match == acl_match_str)
1560 acl_res |= acl_lookup_str(&test, expr) ? ACL_PAT_PASS : ACL_PAT_FAIL;
Willy Tarreaub337b532010-05-13 20:03:41 +02001561 else if (expr->kw->match == acl_match_ip)
1562 acl_res |= acl_lookup_ip(&test, expr) ? ACL_PAT_PASS : ACL_PAT_FAIL;
Willy Tarreauc4262962010-05-10 23:42:40 +02001563 }
1564
Willy Tarreaua79534f2008-07-20 10:13:37 +02001565 /* call the match() function for all tests on this value */
1566 list_for_each_entry(pattern, &expr->patterns, list) {
Willy Tarreaua79534f2008-07-20 10:13:37 +02001567 if (acl_res == ACL_PAT_PASS)
1568 break;
Willy Tarreaue56cda92010-05-11 23:25:05 +02001569 acl_res |= expr->kw->match(&test, pattern);
Willy Tarreaua79534f2008-07-20 10:13:37 +02001570 }
Krzysztof Piotr Oledzkid7528e52010-01-29 17:55:53 +01001571
Willy Tarreaue56cda92010-05-11 23:25:05 +02001572 if ((test.flags & ACL_TEST_F_NULL_MATCH) &&
Willy Tarreau020534d2010-05-16 21:45:45 +02001573 LIST_ISEMPTY(&expr->patterns) && eb_is_empty(&expr->pattern_tree))
Krzysztof Piotr Oledzkid7528e52010-01-29 17:55:53 +01001574 acl_res |= expr->kw->match(&test, NULL);
Willy Tarreaua84d3742007-05-07 00:36:48 +02001575 }
1576 /*
Willy Tarreau11382812008-07-09 16:18:21 +02001577 * OK now acl_res holds the result of this expression
1578 * as one of ACL_PAT_FAIL, ACL_PAT_MISS or ACL_PAT_PASS.
Willy Tarreaua84d3742007-05-07 00:36:48 +02001579 *
Willy Tarreau11382812008-07-09 16:18:21 +02001580 * Then if (!MISS) we can cache the result, and put
Willy Tarreaua84d3742007-05-07 00:36:48 +02001581 * (test.flags & ACL_TEST_F_VOLATILE) in the cache flags.
1582 *
1583 * FIXME: implement cache.
1584 *
1585 */
1586
1587 /* now we may have some cleanup to do */
1588 if (test.flags & ACL_TEST_F_MUST_FREE) {
1589 free(test.ptr);
1590 test.len = 0;
1591 }
1592
Willy Tarreau11382812008-07-09 16:18:21 +02001593 /* we're ORing these terms, so a single PASS is enough */
1594 if (acl_res == ACL_PAT_PASS)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001595 break;
1596
Willy Tarreaua84d3742007-05-07 00:36:48 +02001597 if (test.flags & ACL_TEST_F_FETCH_MORE)
1598 goto fetch_next;
Willy Tarreaub6866442008-07-14 23:54:42 +02001599
1600 /* sometimes we know the fetched data is subject to change
1601 * later and give another chance for a new match (eg: request
1602 * size, time, ...)
1603 */
1604 if (test.flags & ACL_TEST_F_MAY_CHANGE && dir & ACL_PARTIAL)
1605 acl_res |= ACL_PAT_MISS;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001606 }
1607 /*
1608 * Here we have the result of an ACL (cached or not).
1609 * ACLs are combined, negated or not, to form conditions.
1610 */
1611
Willy Tarreaua84d3742007-05-07 00:36:48 +02001612 if (term->neg)
Willy Tarreau11382812008-07-09 16:18:21 +02001613 acl_res = acl_neg(acl_res);
Willy Tarreaua84d3742007-05-07 00:36:48 +02001614
1615 suite_res &= acl_res;
Willy Tarreau11382812008-07-09 16:18:21 +02001616
1617 /* we're ANDing these terms, so a single FAIL is enough */
1618 if (suite_res == ACL_PAT_FAIL)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001619 break;
1620 }
1621 cond_res |= suite_res;
Willy Tarreau11382812008-07-09 16:18:21 +02001622
1623 /* we're ORing these terms, so a single PASS is enough */
1624 if (cond_res == ACL_PAT_PASS)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001625 break;
1626 }
Willy Tarreau11382812008-07-09 16:18:21 +02001627 return cond_res;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001628}
1629
1630
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001631/* Reports a pointer to the first ACL used in condition <cond> which requires
1632 * at least one of the USE_FLAGS in <require>. Returns NULL if none matches.
1633 * The construct is almost the same as for acl_exec_cond() since we're walking
1634 * down the ACL tree as well. It is important that the tree is really walked
1635 * through and never cached, because that way, this function can be used as a
1636 * late check.
1637 */
Willy Tarreauf1e98b82010-01-28 17:59:39 +01001638struct acl *cond_find_require(const struct acl_cond *cond, unsigned int require)
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001639{
1640 struct acl_term_suite *suite;
1641 struct acl_term *term;
1642 struct acl *acl;
1643
1644 list_for_each_entry(suite, &cond->suites, list) {
1645 list_for_each_entry(term, &suite->terms, list) {
1646 acl = term->acl;
1647 if (acl->requires & require)
1648 return acl;
1649 }
1650 }
1651 return NULL;
1652}
1653
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001654/*
1655 * Find targets for userlist and groups in acl. Function returns the number
1656 * of errors or OK if everything is fine.
1657 */
1658int
1659acl_find_targets(struct proxy *p)
1660{
1661
1662 struct acl *acl;
1663 struct acl_expr *expr;
1664 struct acl_pattern *pattern;
1665 struct userlist *ul;
1666 int cfgerr = 0;
1667
1668 list_for_each_entry(acl, &p->acl, list) {
1669 list_for_each_entry(expr, &acl->expr, list) {
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001670 if (strcmp(expr->kw->kw, "srv_is_up") == 0) {
1671 struct proxy *px;
1672 struct server *srv;
1673 char *pname, *sname;
1674
1675 if (!expr->arg.str || !*expr->arg.str) {
1676 Alert("proxy %s: acl %s %s(): missing server name.\n",
1677 p->id, acl->name, expr->kw->kw);
1678 cfgerr++;
1679 continue;
1680 }
1681
1682 pname = expr->arg.str;
1683 sname = strrchr(pname, '/');
1684
1685 if (sname)
1686 *sname++ = '\0';
1687 else {
1688 sname = pname;
1689 pname = NULL;
1690 }
1691
1692 px = p;
1693 if (pname) {
1694 px = findproxy(pname, PR_CAP_BE);
1695 if (!px) {
1696 Alert("proxy %s: acl %s %s(): unable to find proxy '%s'.\n",
1697 p->id, acl->name, expr->kw->kw, pname);
1698 cfgerr++;
1699 continue;
1700 }
1701 }
1702
1703 srv = findserver(px, sname);
1704 if (!srv) {
1705 Alert("proxy %s: acl %s %s(): unable to find server '%s'.\n",
1706 p->id, acl->name, expr->kw->kw, sname);
1707 cfgerr++;
1708 continue;
1709 }
1710
1711 free(expr->arg.str);
1712 expr->arg_len = 0;
1713 expr->arg.srv = srv;
1714 continue;
1715 }
1716
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001717 if (strstr(expr->kw->kw, "http_auth") == expr->kw->kw) {
1718
1719 if (!expr->arg.str || !*expr->arg.str) {
1720 Alert("proxy %s: acl %s %s(): missing userlist name.\n",
1721 p->id, acl->name, expr->kw->kw);
1722 cfgerr++;
1723 continue;
1724 }
1725
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01001726 if (p->uri_auth && p->uri_auth->userlist &&
1727 !strcmp(p->uri_auth->userlist->name, expr->arg.str))
1728 ul = p->uri_auth->userlist;
1729 else
1730 ul = auth_find_userlist(expr->arg.str);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001731
1732 if (!ul) {
1733 Alert("proxy %s: acl %s %s(%s): unable to find userlist.\n",
1734 p->id, acl->name, expr->kw->kw, expr->arg.str);
1735 cfgerr++;
1736 continue;
1737 }
1738
1739 expr->arg_len = 0;
1740 expr->arg.ul = ul;
1741 }
1742
1743
1744 if (!strcmp(expr->kw->kw, "http_auth_group")) {
1745
1746 if (LIST_ISEMPTY(&expr->patterns)) {
1747 Alert("proxy %s: acl %s %s(): no groups specified.\n",
1748 p->id, acl->name, expr->kw->kw);
1749 cfgerr++;
1750 continue;
1751 }
1752
1753 list_for_each_entry(pattern, &expr->patterns, list) {
1754 pattern->val.group_mask = auth_resolve_groups(expr->arg.ul, pattern->ptr.str);
1755
1756 free(pattern->ptr.str);
1757 pattern->ptr.str = NULL;
1758 pattern->len = 0;
1759
1760 if (!pattern->val.group_mask) {
1761 Alert("proxy %s: acl %s %s(): invalid group(s).\n",
1762 p->id, acl->name, expr->kw->kw);
1763 cfgerr++;
1764 continue;
1765 }
1766 }
1767 }
1768 }
1769 }
1770
1771 return cfgerr;
1772}
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001773
Willy Tarreaua84d3742007-05-07 00:36:48 +02001774/************************************************************************/
1775/* All supported keywords must be declared here. */
1776/************************************************************************/
1777
1778/* Note: must not be declared <const> as its list will be overwritten */
1779static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001780 { "always_true", acl_parse_nothing, acl_fetch_true, acl_match_nothing, ACL_USE_NOTHING },
1781 { "always_false", acl_parse_nothing, acl_fetch_false, acl_match_nothing, ACL_USE_NOTHING },
1782 { "wait_end", acl_parse_nothing, acl_fetch_wait_end, acl_match_nothing, ACL_USE_NOTHING },
1783 { "req_len", acl_parse_int, acl_fetch_req_len, acl_match_int, ACL_USE_L6REQ_VOLATILE },
1784 { "req_ssl_ver", acl_parse_dotted_ver, acl_fetch_req_ssl_ver, acl_match_int, ACL_USE_L6REQ_VOLATILE },
1785 { "req_rdp_cookie", acl_parse_str, acl_fetch_rdp_cookie, acl_match_str, ACL_USE_L6REQ_VOLATILE|ACL_MAY_LOOKUP },
1786 { "req_rdp_cookie_cnt", acl_parse_int, acl_fetch_rdp_cookie_cnt, acl_match_int, ACL_USE_L6REQ_VOLATILE },
Willy Tarreaua84d3742007-05-07 00:36:48 +02001787#if 0
1788 { "time", acl_parse_time, acl_fetch_time, acl_match_time },
1789#endif
1790 { NULL, NULL, NULL, NULL }
1791}};
1792
1793
1794__attribute__((constructor))
1795static void __acl_init(void)
1796{
1797 acl_register_keywords(&acl_kws);
1798}
1799
1800
1801/*
1802 * Local variables:
1803 * c-indent-level: 8
1804 * c-basic-offset: 8
1805 * End:
1806 */