blob: 4579e4c186b6ebd24e757dba2eee1b70949e6c06 [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 Tarreauae8b7962007-06-09 23:10:04 +0200697 if (strcmp(ptr, "eq") == 0) *opaque = 0;
698 else if (strcmp(ptr, "gt") == 0) *opaque = 1;
699 else if (strcmp(ptr, "ge") == 0) *opaque = 2;
700 else if (strcmp(ptr, "lt") == 0) *opaque = 3;
701 else if (strcmp(ptr, "le") == 0) *opaque = 4;
702 else
703 return 0;
704
705 skip++;
706 ptr = text[skip];
707 }
Willy Tarreaua84d3742007-05-07 00:36:48 +0200708
709 last = i = 0;
710 while (1) {
Willy Tarreauae8b7962007-06-09 23:10:04 +0200711 j = *ptr++;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200712 if ((j == '-' || j == ':') && !last) {
713 last++;
714 pattern->val.range.min = i;
715 i = 0;
716 continue;
717 }
718 j -= '0';
719 if (j > 9)
720 // also catches the terminating zero
721 break;
722 i *= 10;
723 i += j;
724 }
Willy Tarreauae8b7962007-06-09 23:10:04 +0200725
726 if (last && *opaque >= 1 && *opaque <= 4)
727 /* having a range with a min or a max is absurd */
728 return 0;
729
Willy Tarreaua84d3742007-05-07 00:36:48 +0200730 if (!last)
731 pattern->val.range.min = i;
732 pattern->val.range.max = i;
Willy Tarreauae8b7962007-06-09 23:10:04 +0200733
734 switch (*opaque) {
735 case 0: /* eq */
736 pattern->val.range.min_set = 1;
737 pattern->val.range.max_set = 1;
738 break;
739 case 1: /* gt */
740 pattern->val.range.min++; /* gt = ge + 1 */
741 case 2: /* ge */
742 pattern->val.range.min_set = 1;
743 pattern->val.range.max_set = 0;
744 break;
745 case 3: /* lt */
746 pattern->val.range.max--; /* lt = le - 1 */
747 case 4: /* le */
748 pattern->val.range.min_set = 0;
749 pattern->val.range.max_set = 1;
750 break;
751 }
752 return skip + 1;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200753}
754
Willy Tarreau4a26d2f2008-07-15 16:05:33 +0200755/* Parse a range of positive 2-component versions delimited by either ':' or
756 * '-'. The version consists in a major and a minor, both of which must be
757 * smaller than 65536, because internally they will be represented as a 32-bit
758 * integer.
759 * If only one version is read, it is set as both min and max. Just like for
760 * pure integers, an operator may be specified as the prefix, among this list
761 * of 5 :
762 *
763 * 0:eq, 1:gt, 2:ge, 3:lt, 4:le
764 *
765 * The default operator is "eq". It supports range matching. Ranges are
766 * rejected for other operators. The operator may be changed at any time.
767 * The operator is stored in the 'opaque' argument. This allows constructs
768 * such as the following one :
769 *
770 * acl obsolete_ssl ssl_req_proto lt 3
771 * acl unsupported_ssl ssl_req_proto gt 3.1
772 * acl valid_ssl ssl_req_proto 3.0-3.1
773 *
774 */
775int acl_parse_dotted_ver(const char **text, struct acl_pattern *pattern, int *opaque)
776{
777 signed long long i;
778 unsigned int j, last, skip = 0;
779 const char *ptr = *text;
780
781
782 while (!isdigit((unsigned char)*ptr)) {
783 if (strcmp(ptr, "eq") == 0) *opaque = 0;
784 else if (strcmp(ptr, "gt") == 0) *opaque = 1;
785 else if (strcmp(ptr, "ge") == 0) *opaque = 2;
786 else if (strcmp(ptr, "lt") == 0) *opaque = 3;
787 else if (strcmp(ptr, "le") == 0) *opaque = 4;
788 else
789 return 0;
790
791 skip++;
792 ptr = text[skip];
793 }
794
795 last = i = 0;
796 while (1) {
797 j = *ptr++;
798 if (j == '.') {
799 /* minor part */
800 if (i >= 65536)
801 return 0;
802 i <<= 16;
803 continue;
804 }
805 if ((j == '-' || j == ':') && !last) {
806 last++;
807 if (i < 65536)
808 i <<= 16;
809 pattern->val.range.min = i;
810 i = 0;
811 continue;
812 }
813 j -= '0';
814 if (j > 9)
815 // also catches the terminating zero
816 break;
817 i = (i & 0xFFFF0000) + (i & 0xFFFF) * 10;
818 i += j;
819 }
820
821 /* if we only got a major version, let's shift it now */
822 if (i < 65536)
823 i <<= 16;
824
825 if (last && *opaque >= 1 && *opaque <= 4)
826 /* having a range with a min or a max is absurd */
827 return 0;
828
829 if (!last)
830 pattern->val.range.min = i;
831 pattern->val.range.max = i;
832
833 switch (*opaque) {
834 case 0: /* eq */
835 pattern->val.range.min_set = 1;
836 pattern->val.range.max_set = 1;
837 break;
838 case 1: /* gt */
839 pattern->val.range.min++; /* gt = ge + 1 */
840 case 2: /* ge */
841 pattern->val.range.min_set = 1;
842 pattern->val.range.max_set = 0;
843 break;
844 case 3: /* lt */
845 pattern->val.range.max--; /* lt = le - 1 */
846 case 4: /* le */
847 pattern->val.range.min_set = 0;
848 pattern->val.range.max_set = 1;
849 break;
850 }
851 return skip + 1;
852}
853
Willy Tarreaua67fad92007-05-08 19:50:09 +0200854/* Parse an IP address and an optional mask in the form addr[/mask].
855 * The addr may either be an IPv4 address or a hostname. The mask
856 * may either be a dotted mask or a number of bits. Returns 1 if OK,
857 * otherwise 0.
858 */
Willy Tarreauae8b7962007-06-09 23:10:04 +0200859int acl_parse_ip(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreaua67fad92007-05-08 19:50:09 +0200860{
Willy Tarreaub337b532010-05-13 20:03:41 +0200861 struct eb_root *tree = NULL;
862 if (pattern->flags & ACL_PAT_F_TREE_OK)
863 tree = pattern->val.tree;
864
865 if (str2net(*text, &pattern->val.ipv4.addr, &pattern->val.ipv4.mask)) {
866 unsigned int mask = ntohl(pattern->val.ipv4.mask.s_addr);
867 struct ebmb_node *node;
868 /* check if the mask is contiguous so that we can insert the
869 * network into the tree. A continuous mask has only ones on
870 * the left. This means that this mask + its lower bit added
871 * once again is null.
872 */
873 if (mask + (mask & -mask) == 0 && tree) {
874 mask = mask ? 33 - flsnz(mask & -mask) : 0; /* equals cidr value */
875 /* FIXME: insert <addr>/<mask> into the tree here */
876 node = calloc(1, sizeof(*node) + 4); /* reserve 4 bytes for IPv4 address */
877 if (!node)
878 return 0;
879 memcpy(node->key, &pattern->val.ipv4.addr, 4); /* network byte order */
880 node->node.pfx = mask;
881 if (ebmb_insert_prefix(tree, node, 4) != node)
882 free(node); /* was a duplicate */
883 pattern->flags |= ACL_PAT_F_TREE;
884 return 1;
885 }
Willy Tarreauae8b7962007-06-09 23:10:04 +0200886 return 1;
Willy Tarreaub337b532010-05-13 20:03:41 +0200887 }
Willy Tarreauae8b7962007-06-09 23:10:04 +0200888 else
889 return 0;
Willy Tarreaua67fad92007-05-08 19:50:09 +0200890}
891
Willy Tarreaua84d3742007-05-07 00:36:48 +0200892/*
893 * Registers the ACL keyword list <kwl> as a list of valid keywords for next
894 * parsing sessions.
895 */
896void acl_register_keywords(struct acl_kw_list *kwl)
897{
898 LIST_ADDQ(&acl_keywords.list, &kwl->list);
899}
900
901/*
902 * Unregisters the ACL keyword list <kwl> from the list of valid keywords.
903 */
904void acl_unregister_keywords(struct acl_kw_list *kwl)
905{
906 LIST_DEL(&kwl->list);
907 LIST_INIT(&kwl->list);
908}
909
910/* Return a pointer to the ACL <name> within the list starting at <head>, or
911 * NULL if not found.
912 */
913struct acl *find_acl_by_name(const char *name, struct list *head)
914{
915 struct acl *acl;
916 list_for_each_entry(acl, head, list) {
917 if (strcmp(acl->name, name) == 0)
918 return acl;
919 }
920 return NULL;
921}
922
923/* Return a pointer to the ACL keyword <kw>, or NULL if not found. Note that if
924 * <kw> contains an opening parenthesis, only the left part of it is checked.
925 */
926struct acl_keyword *find_acl_kw(const char *kw)
927{
928 int index;
929 const char *kwend;
930 struct acl_kw_list *kwl;
931
932 kwend = strchr(kw, '(');
933 if (!kwend)
934 kwend = kw + strlen(kw);
935
936 list_for_each_entry(kwl, &acl_keywords.list, list) {
937 for (index = 0; kwl->kw[index].kw != NULL; index++) {
938 if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) &&
939 kwl->kw[index].kw[kwend-kw] == 0)
940 return &kwl->kw[index];
941 }
942 }
943 return NULL;
944}
945
946static void free_pattern(struct acl_pattern *pat)
947{
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +0200948
949 if (pat->ptr.ptr) {
950 if (pat->freeptrbuf)
951 pat->freeptrbuf(pat->ptr.ptr);
952
Willy Tarreaua84d3742007-05-07 00:36:48 +0200953 free(pat->ptr.ptr);
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +0200954 }
955
Willy Tarreaua84d3742007-05-07 00:36:48 +0200956 free(pat);
957}
958
959static void free_pattern_list(struct list *head)
960{
961 struct acl_pattern *pat, *tmp;
962 list_for_each_entry_safe(pat, tmp, head, list)
963 free_pattern(pat);
964}
965
Willy Tarreaue56cda92010-05-11 23:25:05 +0200966static void free_pattern_tree(struct eb_root *root)
967{
968 struct eb_node *node, *next;
969 node = eb_first(root);
970 while (node) {
971 next = eb_next(node);
972 free(node);
973 node = next;
974 }
975}
976
Willy Tarreaua84d3742007-05-07 00:36:48 +0200977static struct acl_expr *prune_acl_expr(struct acl_expr *expr)
978{
979 free_pattern_list(&expr->patterns);
Willy Tarreaue56cda92010-05-11 23:25:05 +0200980 free_pattern_tree(&expr->pattern_tree);
Willy Tarreaua84d3742007-05-07 00:36:48 +0200981 LIST_INIT(&expr->patterns);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +0100982 if (expr->arg_len && expr->arg.str)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200983 free(expr->arg.str);
984 expr->kw->use_cnt--;
985 return expr;
986}
987
Willy Tarreau2b5285d2010-05-09 23:45:24 +0200988static int acl_read_patterns_from_file( struct acl_keyword *aclkw,
989 struct acl_expr *expr,
990 const char *filename, int patflags)
991{
992 FILE *file;
993 char *c;
994 const char *args[2];
995 struct acl_pattern *pattern;
996 int opaque;
997
998 file = fopen(filename, "r");
999 if (!file)
1000 return 0;
1001
1002 /* now parse all patterns. The file may contain only one pattern per
1003 * line. If the line contains spaces, they will be part of the pattern.
1004 * The pattern stops at the first CR, LF or EOF encountered.
1005 */
1006 opaque = 0;
Willy Tarreaue56cda92010-05-11 23:25:05 +02001007 pattern = NULL;
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001008 args[1] = "";
1009 while (fgets(trash, sizeof(trash), file) != NULL) {
1010
1011 c = trash;
Willy Tarreau58215a02010-05-13 22:07:43 +02001012
1013 /* ignore lines beginning with a dash */
1014 if (*c == '#')
1015 continue;
1016
1017 /* strip leading spaces and tabs */
1018 while (*c == ' ' || *c == '\t')
1019 c++;
1020
1021 /* empty lines are ignored too */
1022 if (!*c)
1023 continue;
1024
1025 args[0] = c;
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001026 while (*c && *c != '\n' && *c != '\r')
1027 c++;
1028 *c = 0;
1029
Willy Tarreaue56cda92010-05-11 23:25:05 +02001030 /* we keep the previous pattern along iterations as long as it's not used */
1031 if (!pattern)
1032 pattern = (struct acl_pattern *)malloc(sizeof(*pattern));
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001033 if (!pattern)
1034 goto out_close;
Willy Tarreaue56cda92010-05-11 23:25:05 +02001035
1036 memset(pattern, 0, sizeof(*pattern));
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001037 pattern->flags = patflags;
1038
Willy Tarreaue56cda92010-05-11 23:25:05 +02001039 if ((aclkw->requires & ACL_MAY_LOOKUP) && !(pattern->flags & ACL_PAT_F_IGNORE_CASE)) {
1040 /* we pre-set the data pointer to the tree's head so that functions
1041 * which are able to insert in a tree know where to do that.
1042 */
1043 pattern->flags |= ACL_PAT_F_TREE_OK;
1044 pattern->val.tree = &expr->pattern_tree;
1045 }
1046
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001047 if (!aclkw->parse(args, pattern, &opaque))
1048 goto out_free_pattern;
Willy Tarreaue56cda92010-05-11 23:25:05 +02001049
1050 /* if the parser did not feed the tree, let's chain the pattern to the list */
1051 if (!(pattern->flags & ACL_PAT_F_TREE)) {
1052 LIST_ADDQ(&expr->patterns, &pattern->list);
1053 pattern = NULL; /* get a new one */
1054 }
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001055 }
Willy Tarreaue56cda92010-05-11 23:25:05 +02001056 if (pattern)
1057 free_pattern(pattern);
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001058 return 1;
1059
1060 out_free_pattern:
1061 free_pattern(pattern);
1062 out_close:
1063 fclose(file);
1064 return 0;
1065}
1066
Willy Tarreaua84d3742007-05-07 00:36:48 +02001067/* Parse an ACL expression starting at <args>[0], and return it.
1068 * Right now, the only accepted syntax is :
1069 * <subject> [<value>...]
1070 */
1071struct acl_expr *parse_acl_expr(const char **args)
1072{
1073 __label__ out_return, out_free_expr, out_free_pattern;
1074 struct acl_expr *expr;
1075 struct acl_keyword *aclkw;
1076 struct acl_pattern *pattern;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02001077 int opaque, patflags;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001078 const char *arg;
1079
1080 aclkw = find_acl_kw(args[0]);
1081 if (!aclkw || !aclkw->parse)
1082 goto out_return;
1083
1084 expr = (struct acl_expr *)calloc(1, sizeof(*expr));
1085 if (!expr)
1086 goto out_return;
1087
1088 expr->kw = aclkw;
1089 aclkw->use_cnt++;
1090 LIST_INIT(&expr->patterns);
Willy Tarreaue56cda92010-05-11 23:25:05 +02001091 expr->pattern_tree = EB_ROOT_UNIQUE;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001092 expr->arg.str = NULL;
Willy Tarreaubb768912007-06-10 11:17:01 +02001093 expr->arg_len = 0;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001094
1095 arg = strchr(args[0], '(');
1096 if (arg != NULL) {
1097 char *end, *arg2;
1098 /* there is an argument in the form "subject(arg)" */
1099 arg++;
1100 end = strchr(arg, ')');
1101 if (!end)
1102 goto out_free_expr;
Willy Tarreauac778f52010-01-26 19:02:46 +01001103 arg2 = my_strndup(arg, end - arg);
Willy Tarreau1edb1442010-01-27 20:13:38 +01001104 if (!arg2)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001105 goto out_free_expr;
Willy Tarreaubb768912007-06-10 11:17:01 +02001106 expr->arg_len = end - arg;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001107 expr->arg.str = arg2;
1108 }
1109
Willy Tarreaua84d3742007-05-07 00:36:48 +02001110 args++;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02001111
1112 /* check for options before patterns. Supported options are :
1113 * -i : ignore case for all patterns by default
1114 * -f : read patterns from those files
1115 * -- : everything after this is not an option
1116 */
1117 patflags = 0;
1118 while (**args == '-') {
1119 if ((*args)[1] == 'i')
1120 patflags |= ACL_PAT_F_IGNORE_CASE;
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001121 else if ((*args)[1] == 'f') {
1122 if (!acl_read_patterns_from_file(aclkw, expr, args[1], patflags | ACL_PAT_F_FROM_FILE))
1123 goto out_free_expr;
1124 args++;
1125 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +02001126 else if ((*args)[1] == '-') {
1127 args++;
1128 break;
1129 }
1130 else
1131 break;
1132 args++;
1133 }
1134
1135 /* now parse all patterns */
Willy Tarreauae8b7962007-06-09 23:10:04 +02001136 opaque = 0;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001137 while (**args) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02001138 int ret;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001139 pattern = (struct acl_pattern *)calloc(1, sizeof(*pattern));
1140 if (!pattern)
1141 goto out_free_expr;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02001142 pattern->flags = patflags;
1143
Willy Tarreauae8b7962007-06-09 23:10:04 +02001144 ret = aclkw->parse(args, pattern, &opaque);
1145 if (!ret)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001146 goto out_free_pattern;
1147 LIST_ADDQ(&expr->patterns, &pattern->list);
Willy Tarreauae8b7962007-06-09 23:10:04 +02001148 args += ret;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001149 }
1150
1151 return expr;
1152
1153 out_free_pattern:
1154 free_pattern(pattern);
1155 out_free_expr:
1156 prune_acl_expr(expr);
1157 free(expr);
1158 out_return:
1159 return NULL;
1160}
1161
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02001162/* Purge everything in the acl <acl>, then return <acl>. */
1163struct acl *prune_acl(struct acl *acl) {
1164
1165 struct acl_expr *expr, *exprb;
1166
1167 free(acl->name);
1168
1169 list_for_each_entry_safe(expr, exprb, &acl->expr, list) {
1170 LIST_DEL(&expr->list);
1171 prune_acl_expr(expr);
1172 free(expr);
1173 }
1174
1175 return acl;
1176}
1177
Willy Tarreaua84d3742007-05-07 00:36:48 +02001178/* Parse an ACL with the name starting at <args>[0], and with a list of already
1179 * known ACLs in <acl>. If the ACL was not in the list, it will be added.
Willy Tarreau2a56c5e2010-03-15 16:13:29 +01001180 * A pointer to that ACL is returned. If the ACL has an empty name, then it's
1181 * an anonymous one and it won't be merged with any other one.
Willy Tarreaua84d3742007-05-07 00:36:48 +02001182 *
1183 * args syntax: <aclname> <acl_expr>
1184 */
1185struct acl *parse_acl(const char **args, struct list *known_acl)
1186{
1187 __label__ out_return, out_free_acl_expr, out_free_name;
1188 struct acl *cur_acl;
1189 struct acl_expr *acl_expr;
1190 char *name;
1191
Willy Tarreau2a56c5e2010-03-15 16:13:29 +01001192 if (**args && invalid_char(*args))
Willy Tarreau2e74c3f2007-12-02 18:45:09 +01001193 goto out_return;
1194
Willy Tarreaua84d3742007-05-07 00:36:48 +02001195 acl_expr = parse_acl_expr(args + 1);
1196 if (!acl_expr)
1197 goto out_return;
1198
Willy Tarreau404e8ab2009-07-26 19:40:40 +02001199 /* Check for args beginning with an opening parenthesis just after the
1200 * subject, as this is almost certainly a typo. Right now we can only
1201 * emit a warning, so let's do so.
1202 */
Krzysztof Piotr Oledzki4cdd8312009-10-05 00:23:35 +02001203 if (!strchr(args[1], '(') && *args[2] == '(')
Willy Tarreau404e8ab2009-07-26 19:40:40 +02001204 Warning("parsing acl '%s' :\n"
1205 " matching '%s' for pattern '%s' is likely a mistake and probably\n"
1206 " not what you want. Maybe you need to remove the extraneous space before '('.\n"
1207 " If you are really sure this is not an error, please insert '--' between the\n"
1208 " match and the pattern to make this warning message disappear.\n",
1209 args[0], args[1], args[2]);
1210
Willy Tarreau2a56c5e2010-03-15 16:13:29 +01001211 if (*args[0])
1212 cur_acl = find_acl_by_name(args[0], known_acl);
1213 else
1214 cur_acl = NULL;
1215
Willy Tarreaua84d3742007-05-07 00:36:48 +02001216 if (!cur_acl) {
1217 name = strdup(args[0]);
1218 if (!name)
1219 goto out_free_acl_expr;
1220 cur_acl = (struct acl *)calloc(1, sizeof(*cur_acl));
1221 if (cur_acl == NULL)
1222 goto out_free_name;
1223
1224 LIST_INIT(&cur_acl->expr);
1225 LIST_ADDQ(known_acl, &cur_acl->list);
1226 cur_acl->name = name;
1227 }
1228
Willy Tarreaua9802632008-07-25 19:13:19 +02001229 cur_acl->requires |= acl_expr->kw->requires;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001230 LIST_ADDQ(&cur_acl->expr, &acl_expr->list);
1231 return cur_acl;
1232
1233 out_free_name:
1234 free(name);
1235 out_free_acl_expr:
1236 prune_acl_expr(acl_expr);
1237 free(acl_expr);
1238 out_return:
1239 return NULL;
1240}
1241
Willy Tarreau16fbe822007-06-17 11:54:31 +02001242/* Some useful ACLs provided by default. Only those used are allocated. */
1243
1244const struct {
1245 const char *name;
1246 const char *expr[4]; /* put enough for longest expression */
1247} default_acl_list[] = {
Willy Tarreau58393e12008-07-20 10:39:22 +02001248 { .name = "TRUE", .expr = {"always_true",""}},
1249 { .name = "FALSE", .expr = {"always_false",""}},
Willy Tarreau16fbe822007-06-17 11:54:31 +02001250 { .name = "LOCALHOST", .expr = {"src","127.0.0.1/8",""}},
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001251 { .name = "HTTP", .expr = {"req_proto_http",""}},
Willy Tarreau16fbe822007-06-17 11:54:31 +02001252 { .name = "HTTP_1.0", .expr = {"req_ver","1.0",""}},
1253 { .name = "HTTP_1.1", .expr = {"req_ver","1.1",""}},
1254 { .name = "METH_CONNECT", .expr = {"method","CONNECT",""}},
1255 { .name = "METH_GET", .expr = {"method","GET","HEAD",""}},
1256 { .name = "METH_HEAD", .expr = {"method","HEAD",""}},
1257 { .name = "METH_OPTIONS", .expr = {"method","OPTIONS",""}},
1258 { .name = "METH_POST", .expr = {"method","POST",""}},
1259 { .name = "METH_TRACE", .expr = {"method","TRACE",""}},
1260 { .name = "HTTP_URL_ABS", .expr = {"url_reg","^[^/:]*://",""}},
1261 { .name = "HTTP_URL_SLASH", .expr = {"url_beg","/",""}},
1262 { .name = "HTTP_URL_STAR", .expr = {"url","*",""}},
1263 { .name = "HTTP_CONTENT", .expr = {"hdr_val(content-length)","gt","0",""}},
Emeric Brunbede3d02009-06-30 17:54:00 +02001264 { .name = "RDP_COOKIE", .expr = {"req_rdp_cookie_cnt","gt","0",""}},
Willy Tarreauc6317702008-07-20 09:29:50 +02001265 { .name = "REQ_CONTENT", .expr = {"req_len","gt","0",""}},
Willy Tarreaub6fb4202008-07-20 11:18:28 +02001266 { .name = "WAIT_END", .expr = {"wait_end",""}},
Willy Tarreau16fbe822007-06-17 11:54:31 +02001267 { .name = NULL, .expr = {""}}
1268};
1269
1270/* Find a default ACL from the default_acl list, compile it and return it.
1271 * If the ACL is not found, NULL is returned. In theory, it cannot fail,
1272 * except when default ACLs are broken, in which case it will return NULL.
1273 * If <known_acl> is not NULL, the ACL will be queued at its tail.
1274 */
1275struct acl *find_acl_default(const char *acl_name, struct list *known_acl)
1276{
1277 __label__ out_return, out_free_acl_expr, out_free_name;
1278 struct acl *cur_acl;
1279 struct acl_expr *acl_expr;
1280 char *name;
1281 int index;
1282
1283 for (index = 0; default_acl_list[index].name != NULL; index++) {
1284 if (strcmp(acl_name, default_acl_list[index].name) == 0)
1285 break;
1286 }
1287
1288 if (default_acl_list[index].name == NULL)
1289 return NULL;
1290
1291 acl_expr = parse_acl_expr((const char **)default_acl_list[index].expr);
1292 if (!acl_expr)
1293 goto out_return;
1294
1295 name = strdup(acl_name);
1296 if (!name)
1297 goto out_free_acl_expr;
1298 cur_acl = (struct acl *)calloc(1, sizeof(*cur_acl));
1299 if (cur_acl == NULL)
1300 goto out_free_name;
1301
1302 cur_acl->name = name;
Willy Tarreaua55b7dc2009-07-12 09:21:30 +02001303 cur_acl->requires |= acl_expr->kw->requires;
Willy Tarreau16fbe822007-06-17 11:54:31 +02001304 LIST_INIT(&cur_acl->expr);
1305 LIST_ADDQ(&cur_acl->expr, &acl_expr->list);
1306 if (known_acl)
1307 LIST_ADDQ(known_acl, &cur_acl->list);
1308
1309 return cur_acl;
1310
1311 out_free_name:
1312 free(name);
1313 out_free_acl_expr:
1314 prune_acl_expr(acl_expr);
1315 free(acl_expr);
1316 out_return:
1317 return NULL;
1318}
Willy Tarreaua84d3742007-05-07 00:36:48 +02001319
1320/* Purge everything in the acl_cond <cond>, then return <cond>. */
1321struct acl_cond *prune_acl_cond(struct acl_cond *cond)
1322{
1323 struct acl_term_suite *suite, *tmp_suite;
1324 struct acl_term *term, *tmp_term;
1325
1326 /* iterate through all term suites and free all terms and all suites */
1327 list_for_each_entry_safe(suite, tmp_suite, &cond->suites, list) {
1328 list_for_each_entry_safe(term, tmp_term, &suite->terms, list)
1329 free(term);
1330 free(suite);
1331 }
1332 return cond;
1333}
1334
1335/* Parse an ACL condition starting at <args>[0], relying on a list of already
1336 * known ACLs passed in <known_acl>. The new condition is returned (or NULL in
1337 * case of low memory). Supports multiple conditions separated by "or".
1338 */
1339struct acl_cond *parse_acl_cond(const char **args, struct list *known_acl, int pol)
1340{
1341 __label__ out_return, out_free_suite, out_free_term;
Willy Tarreau74b98a82007-06-16 19:35:18 +02001342 int arg, neg;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001343 const char *word;
1344 struct acl *cur_acl;
1345 struct acl_term *cur_term;
1346 struct acl_term_suite *cur_suite;
1347 struct acl_cond *cond;
1348
1349 cond = (struct acl_cond *)calloc(1, sizeof(*cond));
1350 if (cond == NULL)
1351 goto out_return;
1352
1353 LIST_INIT(&cond->list);
1354 LIST_INIT(&cond->suites);
1355 cond->pol = pol;
1356
1357 cur_suite = NULL;
Willy Tarreau74b98a82007-06-16 19:35:18 +02001358 neg = 0;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001359 for (arg = 0; *args[arg]; arg++) {
1360 word = args[arg];
1361
1362 /* remove as many exclamation marks as we can */
1363 while (*word == '!') {
1364 neg = !neg;
1365 word++;
1366 }
1367
1368 /* an empty word is allowed because we cannot force the user to
1369 * always think about not leaving exclamation marks alone.
1370 */
1371 if (!*word)
1372 continue;
1373
Willy Tarreau16fbe822007-06-17 11:54:31 +02001374 if (strcasecmp(word, "or") == 0 || strcmp(word, "||") == 0) {
Willy Tarreaua84d3742007-05-07 00:36:48 +02001375 /* new term suite */
1376 cur_suite = NULL;
1377 neg = 0;
1378 continue;
1379 }
1380
Willy Tarreau95fa4692010-02-01 13:05:50 +01001381 if (strcmp(word, "{") == 0) {
1382 /* we may have a complete ACL expression between two braces,
1383 * find the last one.
1384 */
1385 int arg_end = arg + 1;
1386 const char **args_new;
1387
1388 while (*args[arg_end] && strcmp(args[arg_end], "}") != 0)
1389 arg_end++;
1390
1391 if (!*args[arg_end])
1392 goto out_free_suite;
1393
1394 args_new = calloc(1, (arg_end - arg + 1) * sizeof(*args_new));
1395 if (!args_new)
1396 goto out_free_suite;
1397
Willy Tarreau2a56c5e2010-03-15 16:13:29 +01001398 args_new[0] = "";
Willy Tarreau95fa4692010-02-01 13:05:50 +01001399 memcpy(args_new + 1, args + arg + 1, (arg_end - arg) * sizeof(*args_new));
1400 args_new[arg_end - arg] = "";
1401 cur_acl = parse_acl(args_new, known_acl);
1402 free(args_new);
1403
1404 if (!cur_acl)
Willy Tarreau16fbe822007-06-17 11:54:31 +02001405 goto out_free_suite;
Willy Tarreau95fa4692010-02-01 13:05:50 +01001406 arg = arg_end;
1407 }
1408 else {
1409 /* search for <word> in the known ACL names. If we do not find
1410 * it, let's look for it in the default ACLs, and if found, add
1411 * it to the list of ACLs of this proxy. This makes it possible
1412 * to override them.
1413 */
1414 cur_acl = find_acl_by_name(word, known_acl);
1415 if (cur_acl == NULL) {
1416 cur_acl = find_acl_default(word, known_acl);
1417 if (cur_acl == NULL)
1418 goto out_free_suite;
1419 }
Willy Tarreau16fbe822007-06-17 11:54:31 +02001420 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001421
1422 cur_term = (struct acl_term *)calloc(1, sizeof(*cur_term));
1423 if (cur_term == NULL)
1424 goto out_free_suite;
1425
1426 cur_term->acl = cur_acl;
1427 cur_term->neg = neg;
Willy Tarreaua9802632008-07-25 19:13:19 +02001428 cond->requires |= cur_acl->requires;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001429
1430 if (!cur_suite) {
1431 cur_suite = (struct acl_term_suite *)calloc(1, sizeof(*cur_suite));
1432 if (cur_term == NULL)
1433 goto out_free_term;
1434 LIST_INIT(&cur_suite->terms);
1435 LIST_ADDQ(&cond->suites, &cur_suite->list);
1436 }
1437 LIST_ADDQ(&cur_suite->terms, &cur_term->list);
Willy Tarreau74b98a82007-06-16 19:35:18 +02001438 neg = 0;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001439 }
1440
1441 return cond;
1442
1443 out_free_term:
1444 free(cur_term);
1445 out_free_suite:
1446 prune_acl_cond(cond);
1447 free(cond);
1448 out_return:
1449 return NULL;
1450}
1451
Willy Tarreau2bbba412010-01-28 16:48:33 +01001452/* Builds an ACL condition starting at the if/unless keyword. The complete
1453 * condition is returned. NULL is returned in case of error or if the first
1454 * word is neither "if" nor "unless". It automatically sets the file name and
1455 * the line number in the condition for better error reporting, and adds the
1456 * ACL requirements to the proxy's acl_requires.
1457 */
1458struct acl_cond *build_acl_cond(const char *file, int line, struct proxy *px, const char **args)
1459{
1460 int pol = ACL_COND_NONE;
1461 struct acl_cond *cond = NULL;
1462
1463 if (!strcmp(*args, "if")) {
1464 pol = ACL_COND_IF;
1465 args++;
1466 }
1467 else if (!strcmp(*args, "unless")) {
1468 pol = ACL_COND_UNLESS;
1469 args++;
1470 }
1471 else
1472 return NULL;
1473
1474 cond = parse_acl_cond(args, &px->acl, pol);
1475 if (!cond)
1476 return NULL;
1477
1478 cond->file = file;
1479 cond->line = line;
1480 px->acl_requires |= cond->requires;
1481
1482 return cond;
1483}
1484
Willy Tarreau11382812008-07-09 16:18:21 +02001485/* Execute condition <cond> and return either ACL_PAT_FAIL, ACL_PAT_MISS or
Willy Tarreaub6866442008-07-14 23:54:42 +02001486 * ACL_PAT_PASS depending on the test results. ACL_PAT_MISS may only be
1487 * returned if <dir> contains ACL_PARTIAL, indicating that incomplete data
1488 * is being examined.
1489 * This function only computes the condition, it does not apply the polarity
1490 * required by IF/UNLESS, it's up to the caller to do this using something like
1491 * this :
Willy Tarreau11382812008-07-09 16:18:21 +02001492 *
1493 * res = acl_pass(res);
Willy Tarreaub6866442008-07-14 23:54:42 +02001494 * if (res == ACL_PAT_MISS)
1495 * return 0;
Willy Tarreau11382812008-07-09 16:18:21 +02001496 * if (cond->pol == ACL_COND_UNLESS)
1497 * res = !res;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001498 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02001499int acl_exec_cond(struct acl_cond *cond, struct proxy *px, struct session *l4, void *l7, int dir)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001500{
1501 __label__ fetch_next;
1502 struct acl_term_suite *suite;
1503 struct acl_term *term;
1504 struct acl_expr *expr;
1505 struct acl *acl;
1506 struct acl_pattern *pattern;
1507 struct acl_test test;
Willy Tarreau11382812008-07-09 16:18:21 +02001508 int acl_res, suite_res, cond_res;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001509
Willy Tarreau11382812008-07-09 16:18:21 +02001510 /* We're doing a logical OR between conditions so we initialize to FAIL.
1511 * The MISS status is propagated down from the suites.
1512 */
Willy Tarreaua84d3742007-05-07 00:36:48 +02001513 cond_res = ACL_PAT_FAIL;
1514 list_for_each_entry(suite, &cond->suites, list) {
Willy Tarreau11382812008-07-09 16:18:21 +02001515 /* Evaluate condition suite <suite>. We stop at the first term
1516 * which returns ACL_PAT_FAIL. The MISS status is still propagated
1517 * in case of uncertainty in the result.
Willy Tarreaua84d3742007-05-07 00:36:48 +02001518 */
1519
1520 /* we're doing a logical AND between terms, so we must set the
1521 * initial value to PASS.
1522 */
1523 suite_res = ACL_PAT_PASS;
1524 list_for_each_entry(term, &suite->terms, list) {
1525 acl = term->acl;
1526
1527 /* FIXME: use cache !
1528 * check acl->cache_idx for this.
1529 */
1530
1531 /* ACL result not cached. Let's scan all the expressions
1532 * and use the first one to match.
1533 */
1534 acl_res = ACL_PAT_FAIL;
1535 list_for_each_entry(expr, &acl->expr, list) {
Willy Tarreaud41f8d82007-06-10 10:06:18 +02001536 /* we need to reset context and flags */
1537 memset(&test, 0, sizeof(test));
Willy Tarreaua84d3742007-05-07 00:36:48 +02001538 fetch_next:
Willy Tarreaub6866442008-07-14 23:54:42 +02001539 if (!expr->kw->fetch(px, l4, l7, dir, expr, &test)) {
1540 /* maybe we could not fetch because of missing data */
1541 if (test.flags & ACL_TEST_F_MAY_CHANGE && dir & ACL_PARTIAL)
1542 acl_res |= ACL_PAT_MISS;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001543 continue;
Willy Tarreaub6866442008-07-14 23:54:42 +02001544 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001545
Willy Tarreaua79534f2008-07-20 10:13:37 +02001546 if (test.flags & ACL_TEST_F_RES_SET) {
1547 if (test.flags & ACL_TEST_F_RES_PASS)
1548 acl_res |= ACL_PAT_PASS;
1549 else
1550 acl_res |= ACL_PAT_FAIL;
1551 }
1552 else {
Willy Tarreau020534d2010-05-16 21:45:45 +02001553 if (!eb_is_empty(&expr->pattern_tree)) {
Willy Tarreauc4262962010-05-10 23:42:40 +02001554 /* a tree is present, let's check what type it is */
1555 if (expr->kw->match == acl_match_str)
1556 acl_res |= acl_lookup_str(&test, expr) ? ACL_PAT_PASS : ACL_PAT_FAIL;
Willy Tarreaub337b532010-05-13 20:03:41 +02001557 else if (expr->kw->match == acl_match_ip)
1558 acl_res |= acl_lookup_ip(&test, expr) ? ACL_PAT_PASS : ACL_PAT_FAIL;
Willy Tarreauc4262962010-05-10 23:42:40 +02001559 }
1560
Willy Tarreaua79534f2008-07-20 10:13:37 +02001561 /* call the match() function for all tests on this value */
1562 list_for_each_entry(pattern, &expr->patterns, list) {
Willy Tarreaua79534f2008-07-20 10:13:37 +02001563 if (acl_res == ACL_PAT_PASS)
1564 break;
Willy Tarreaue56cda92010-05-11 23:25:05 +02001565 acl_res |= expr->kw->match(&test, pattern);
Willy Tarreaua79534f2008-07-20 10:13:37 +02001566 }
Krzysztof Piotr Oledzkid7528e52010-01-29 17:55:53 +01001567
Willy Tarreaue56cda92010-05-11 23:25:05 +02001568 if ((test.flags & ACL_TEST_F_NULL_MATCH) &&
Willy Tarreau020534d2010-05-16 21:45:45 +02001569 LIST_ISEMPTY(&expr->patterns) && eb_is_empty(&expr->pattern_tree))
Krzysztof Piotr Oledzkid7528e52010-01-29 17:55:53 +01001570 acl_res |= expr->kw->match(&test, NULL);
Willy Tarreaua84d3742007-05-07 00:36:48 +02001571 }
1572 /*
Willy Tarreau11382812008-07-09 16:18:21 +02001573 * OK now acl_res holds the result of this expression
1574 * as one of ACL_PAT_FAIL, ACL_PAT_MISS or ACL_PAT_PASS.
Willy Tarreaua84d3742007-05-07 00:36:48 +02001575 *
Willy Tarreau11382812008-07-09 16:18:21 +02001576 * Then if (!MISS) we can cache the result, and put
Willy Tarreaua84d3742007-05-07 00:36:48 +02001577 * (test.flags & ACL_TEST_F_VOLATILE) in the cache flags.
1578 *
1579 * FIXME: implement cache.
1580 *
1581 */
1582
1583 /* now we may have some cleanup to do */
1584 if (test.flags & ACL_TEST_F_MUST_FREE) {
1585 free(test.ptr);
1586 test.len = 0;
1587 }
1588
Willy Tarreau11382812008-07-09 16:18:21 +02001589 /* we're ORing these terms, so a single PASS is enough */
1590 if (acl_res == ACL_PAT_PASS)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001591 break;
1592
Willy Tarreaua84d3742007-05-07 00:36:48 +02001593 if (test.flags & ACL_TEST_F_FETCH_MORE)
1594 goto fetch_next;
Willy Tarreaub6866442008-07-14 23:54:42 +02001595
1596 /* sometimes we know the fetched data is subject to change
1597 * later and give another chance for a new match (eg: request
1598 * size, time, ...)
1599 */
1600 if (test.flags & ACL_TEST_F_MAY_CHANGE && dir & ACL_PARTIAL)
1601 acl_res |= ACL_PAT_MISS;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001602 }
1603 /*
1604 * Here we have the result of an ACL (cached or not).
1605 * ACLs are combined, negated or not, to form conditions.
1606 */
1607
Willy Tarreaua84d3742007-05-07 00:36:48 +02001608 if (term->neg)
Willy Tarreau11382812008-07-09 16:18:21 +02001609 acl_res = acl_neg(acl_res);
Willy Tarreaua84d3742007-05-07 00:36:48 +02001610
1611 suite_res &= acl_res;
Willy Tarreau11382812008-07-09 16:18:21 +02001612
1613 /* we're ANDing these terms, so a single FAIL is enough */
1614 if (suite_res == ACL_PAT_FAIL)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001615 break;
1616 }
1617 cond_res |= suite_res;
Willy Tarreau11382812008-07-09 16:18:21 +02001618
1619 /* we're ORing these terms, so a single PASS is enough */
1620 if (cond_res == ACL_PAT_PASS)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001621 break;
1622 }
Willy Tarreau11382812008-07-09 16:18:21 +02001623 return cond_res;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001624}
1625
1626
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001627/* Reports a pointer to the first ACL used in condition <cond> which requires
1628 * at least one of the USE_FLAGS in <require>. Returns NULL if none matches.
1629 * The construct is almost the same as for acl_exec_cond() since we're walking
1630 * down the ACL tree as well. It is important that the tree is really walked
1631 * through and never cached, because that way, this function can be used as a
1632 * late check.
1633 */
Willy Tarreauf1e98b82010-01-28 17:59:39 +01001634struct acl *cond_find_require(const struct acl_cond *cond, unsigned int require)
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001635{
1636 struct acl_term_suite *suite;
1637 struct acl_term *term;
1638 struct acl *acl;
1639
1640 list_for_each_entry(suite, &cond->suites, list) {
1641 list_for_each_entry(term, &suite->terms, list) {
1642 acl = term->acl;
1643 if (acl->requires & require)
1644 return acl;
1645 }
1646 }
1647 return NULL;
1648}
1649
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001650/*
1651 * Find targets for userlist and groups in acl. Function returns the number
1652 * of errors or OK if everything is fine.
1653 */
1654int
1655acl_find_targets(struct proxy *p)
1656{
1657
1658 struct acl *acl;
1659 struct acl_expr *expr;
1660 struct acl_pattern *pattern;
1661 struct userlist *ul;
1662 int cfgerr = 0;
1663
1664 list_for_each_entry(acl, &p->acl, list) {
1665 list_for_each_entry(expr, &acl->expr, list) {
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001666 if (strcmp(expr->kw->kw, "srv_is_up") == 0) {
1667 struct proxy *px;
1668 struct server *srv;
1669 char *pname, *sname;
1670
1671 if (!expr->arg.str || !*expr->arg.str) {
1672 Alert("proxy %s: acl %s %s(): missing server name.\n",
1673 p->id, acl->name, expr->kw->kw);
1674 cfgerr++;
1675 continue;
1676 }
1677
1678 pname = expr->arg.str;
1679 sname = strrchr(pname, '/');
1680
1681 if (sname)
1682 *sname++ = '\0';
1683 else {
1684 sname = pname;
1685 pname = NULL;
1686 }
1687
1688 px = p;
1689 if (pname) {
1690 px = findproxy(pname, PR_CAP_BE);
1691 if (!px) {
1692 Alert("proxy %s: acl %s %s(): unable to find proxy '%s'.\n",
1693 p->id, acl->name, expr->kw->kw, pname);
1694 cfgerr++;
1695 continue;
1696 }
1697 }
1698
1699 srv = findserver(px, sname);
1700 if (!srv) {
1701 Alert("proxy %s: acl %s %s(): unable to find server '%s'.\n",
1702 p->id, acl->name, expr->kw->kw, sname);
1703 cfgerr++;
1704 continue;
1705 }
1706
1707 free(expr->arg.str);
1708 expr->arg_len = 0;
1709 expr->arg.srv = srv;
1710 continue;
1711 }
1712
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001713 if (strstr(expr->kw->kw, "http_auth") == expr->kw->kw) {
1714
1715 if (!expr->arg.str || !*expr->arg.str) {
1716 Alert("proxy %s: acl %s %s(): missing userlist name.\n",
1717 p->id, acl->name, expr->kw->kw);
1718 cfgerr++;
1719 continue;
1720 }
1721
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01001722 if (p->uri_auth && p->uri_auth->userlist &&
1723 !strcmp(p->uri_auth->userlist->name, expr->arg.str))
1724 ul = p->uri_auth->userlist;
1725 else
1726 ul = auth_find_userlist(expr->arg.str);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001727
1728 if (!ul) {
1729 Alert("proxy %s: acl %s %s(%s): unable to find userlist.\n",
1730 p->id, acl->name, expr->kw->kw, expr->arg.str);
1731 cfgerr++;
1732 continue;
1733 }
1734
1735 expr->arg_len = 0;
1736 expr->arg.ul = ul;
1737 }
1738
1739
1740 if (!strcmp(expr->kw->kw, "http_auth_group")) {
1741
1742 if (LIST_ISEMPTY(&expr->patterns)) {
1743 Alert("proxy %s: acl %s %s(): no groups specified.\n",
1744 p->id, acl->name, expr->kw->kw);
1745 cfgerr++;
1746 continue;
1747 }
1748
1749 list_for_each_entry(pattern, &expr->patterns, list) {
1750 pattern->val.group_mask = auth_resolve_groups(expr->arg.ul, pattern->ptr.str);
1751
1752 free(pattern->ptr.str);
1753 pattern->ptr.str = NULL;
1754 pattern->len = 0;
1755
1756 if (!pattern->val.group_mask) {
1757 Alert("proxy %s: acl %s %s(): invalid group(s).\n",
1758 p->id, acl->name, expr->kw->kw);
1759 cfgerr++;
1760 continue;
1761 }
1762 }
1763 }
1764 }
1765 }
1766
1767 return cfgerr;
1768}
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001769
Willy Tarreaua84d3742007-05-07 00:36:48 +02001770/************************************************************************/
1771/* All supported keywords must be declared here. */
1772/************************************************************************/
1773
1774/* Note: must not be declared <const> as its list will be overwritten */
1775static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001776 { "always_true", acl_parse_nothing, acl_fetch_true, acl_match_nothing, ACL_USE_NOTHING },
1777 { "always_false", acl_parse_nothing, acl_fetch_false, acl_match_nothing, ACL_USE_NOTHING },
1778 { "wait_end", acl_parse_nothing, acl_fetch_wait_end, acl_match_nothing, ACL_USE_NOTHING },
1779 { "req_len", acl_parse_int, acl_fetch_req_len, acl_match_int, ACL_USE_L6REQ_VOLATILE },
1780 { "req_ssl_ver", acl_parse_dotted_ver, acl_fetch_req_ssl_ver, acl_match_int, ACL_USE_L6REQ_VOLATILE },
1781 { "req_rdp_cookie", acl_parse_str, acl_fetch_rdp_cookie, acl_match_str, ACL_USE_L6REQ_VOLATILE|ACL_MAY_LOOKUP },
1782 { "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 +02001783#if 0
1784 { "time", acl_parse_time, acl_fetch_time, acl_match_time },
1785#endif
1786 { NULL, NULL, NULL, NULL }
1787}};
1788
1789
1790__attribute__((constructor))
1791static void __acl_init(void)
1792{
1793 acl_register_keywords(&acl_kws);
1794}
1795
1796
1797/*
1798 * Local variables:
1799 * c-indent-level: 8
1800 * c-basic-offset: 8
1801 * End:
1802 */