blob: 33f00ed2bf8992e594a5b9273ed74cc92246d36b [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
Emeric Brun38e71762010-09-23 17:59:18 +0200110
111static int
112acl_fetch_ssl_hello_type(struct proxy *px, struct session *l4, void *l7, int dir,
113 struct acl_expr *expr, struct acl_test *test)
114{
115 int hs_len;
116 int hs_type, bleft;
117 struct buffer *b;
118 const unsigned char *data;
119
120 if (!l4)
121 goto not_ssl_hello;
122
123 b = ((dir & ACL_DIR_MASK) == ACL_DIR_RTR) ? l4->rep : l4->req;
124
125 bleft = b->l;
126 data = (const unsigned char *)b->w;
127
128 if (!bleft)
129 goto too_short;
130
131 if ((*data >= 0x14 && *data <= 0x17) || (*data == 0xFF)) {
132 /* SSLv3 header format */
133 if (bleft < 9)
134 goto too_short;
135
136 /* ssl version 3 */
137 if ((data[1] << 16) + data[2] < 0x00030000)
138 goto not_ssl_hello;
139
140 /* ssl message len must present handshake type and len */
141 if ((data[3] << 8) + data[4] < 4)
142 goto not_ssl_hello;
143
144 /* format introduced with SSLv3 */
145
146 hs_type = (int)data[5];
147 hs_len = ( data[6] << 16 ) + ( data[7] << 8 ) + data[8];
148
149 /* not a full handshake */
150 if (bleft < (9 + hs_len))
151 goto too_short;
152
153 }
154 else {
155 goto not_ssl_hello;
156 }
157
158 test->i = hs_type;
159 test->flags = ACL_TEST_F_VOLATILE;
160
161 return 1;
162
163 too_short:
164 test->flags = ACL_TEST_F_MAY_CHANGE;
165
166 not_ssl_hello:
167
168 return 0;
169}
170
Willy Tarreau44b90cc2010-05-24 20:27:29 +0200171/* Return the version of the SSL protocol in the request. It supports both
172 * SSLv3 (TLSv1) header format for any message, and SSLv2 header format for
173 * the hello message. The SSLv3 format is described in RFC 2246 p49, and the
174 * SSLv2 format is described here, and completed p67 of RFC 2246 :
175 * http://wp.netscape.com/eng/security/SSL_2.html
176 *
177 * Note: this decoder only works with non-wrapping data.
178 */
179static int
180acl_fetch_req_ssl_ver(struct proxy *px, struct session *l4, void *l7, int dir,
181 struct acl_expr *expr, struct acl_test *test)
182{
183 int version, bleft, msg_len;
184 const unsigned char *data;
185
186 if (!l4 || !l4->req)
187 return 0;
188
189 msg_len = 0;
190 bleft = l4->req->l;
191 if (!bleft)
192 goto too_short;
193
194 data = (const unsigned char *)l4->req->w;
195 if ((*data >= 0x14 && *data <= 0x17) || (*data == 0xFF)) {
196 /* SSLv3 header format */
197 if (bleft < 5)
198 goto too_short;
199
200 version = (data[1] << 16) + data[2]; /* version: major, minor */
201 msg_len = (data[3] << 8) + data[4]; /* record length */
202
203 /* format introduced with SSLv3 */
204 if (version < 0x00030000)
205 goto not_ssl;
206
207 /* message length between 1 and 2^14 + 2048 */
208 if (msg_len < 1 || msg_len > ((1<<14) + 2048))
209 goto not_ssl;
210
211 bleft -= 5; data += 5;
212 } else {
213 /* SSLv2 header format, only supported for hello (msg type 1) */
214 int rlen, plen, cilen, silen, chlen;
215
216 if (*data & 0x80) {
217 if (bleft < 3)
218 goto too_short;
219 /* short header format : 15 bits for length */
220 rlen = ((data[0] & 0x7F) << 8) | data[1];
221 plen = 0;
222 bleft -= 2; data += 2;
223 } else {
224 if (bleft < 4)
225 goto too_short;
226 /* long header format : 14 bits for length + pad length */
227 rlen = ((data[0] & 0x3F) << 8) | data[1];
228 plen = data[2];
229 bleft -= 3; data += 2;
230 }
231
232 if (*data != 0x01)
233 goto not_ssl;
234 bleft--; data++;
235
236 if (bleft < 8)
237 goto too_short;
238 version = (data[0] << 16) + data[1]; /* version: major, minor */
239 cilen = (data[2] << 8) + data[3]; /* cipher len, multiple of 3 */
240 silen = (data[4] << 8) + data[5]; /* session_id_len: 0 or 16 */
241 chlen = (data[6] << 8) + data[7]; /* 16<=challenge length<=32 */
242
243 bleft -= 8; data += 8;
244 if (cilen % 3 != 0)
245 goto not_ssl;
246 if (silen && silen != 16)
247 goto not_ssl;
248 if (chlen < 16 || chlen > 32)
249 goto not_ssl;
250 if (rlen != 9 + cilen + silen + chlen)
251 goto not_ssl;
252
253 /* focus on the remaining data length */
254 msg_len = cilen + silen + chlen + plen;
255 }
256 /* We could recursively check that the buffer ends exactly on an SSL
257 * fragment boundary and that a possible next segment is still SSL,
258 * but that's a bit pointless. However, we could still check that
259 * all the part of the request which fits in a buffer is already
260 * there.
261 */
262 if (msg_len > buffer_max_len(l4->req) + l4->req->data - l4->req->w)
263 msg_len = buffer_max_len(l4->req) + l4->req->data - l4->req->w;
264
265 if (bleft < msg_len)
266 goto too_short;
267
268 /* OK that's enough. We have at least the whole message, and we have
269 * the protocol version.
270 */
271 test->i = version;
272 test->flags = ACL_TEST_F_VOLATILE;
273 return 1;
274
275 too_short:
276 test->flags = ACL_TEST_F_MAY_CHANGE;
277 not_ssl:
278 return 0;
279}
280
281/* Fetch the RDP cookie identified in the expression.
282 * Note: this decoder only works with non-wrapping data.
283 */
284int
285acl_fetch_rdp_cookie(struct proxy *px, struct session *l4, void *l7, int dir,
286 struct acl_expr *expr, struct acl_test *test)
287{
288 int bleft;
289 const unsigned char *data;
290
291 if (!l4 || !l4->req)
292 return 0;
293
294 test->flags = 0;
295
296 bleft = l4->req->l;
297 if (bleft <= 11)
298 goto too_short;
299
300 data = (const unsigned char *)l4->req->w + 11;
301 bleft -= 11;
302
303 if (bleft <= 7)
304 goto too_short;
305
306 if (strncasecmp((const char *)data, "Cookie:", 7) != 0)
307 goto not_cookie;
308
309 data += 7;
310 bleft -= 7;
311
312 while (bleft > 0 && *data == ' ') {
313 data++;
314 bleft--;
315 }
316
317 if (expr->arg_len) {
318
319 if (bleft <= expr->arg_len)
320 goto too_short;
321
322 if ((data[expr->arg_len] != '=') ||
323 strncasecmp(expr->arg.str, (const char *)data, expr->arg_len) != 0)
324 goto not_cookie;
325
326 data += expr->arg_len + 1;
327 bleft -= expr->arg_len + 1;
328 } else {
329 while (bleft > 0 && *data != '=') {
330 if (*data == '\r' || *data == '\n')
331 goto not_cookie;
332 data++;
333 bleft--;
334 }
335
336 if (bleft < 1)
337 goto too_short;
338
339 if (*data != '=')
340 goto not_cookie;
341
342 data++;
343 bleft--;
344 }
345
346 /* data points to cookie value */
347 test->ptr = (char *)data;
348 test->len = 0;
349
350 while (bleft > 0 && *data != '\r') {
351 data++;
352 bleft--;
353 }
354
355 if (bleft < 2)
356 goto too_short;
357
358 if (data[0] != '\r' || data[1] != '\n')
359 goto not_cookie;
360
361 test->len = (char *)data - test->ptr;
362 test->flags = ACL_TEST_F_VOLATILE;
363 return 1;
364
365 too_short:
366 test->flags = ACL_TEST_F_MAY_CHANGE;
367 not_cookie:
368 return 0;
369}
370
371static int
372acl_fetch_rdp_cookie_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
373 struct acl_expr *expr, struct acl_test *test)
374{
375 int ret;
376
377 ret = acl_fetch_rdp_cookie(px, l4, l7, dir, expr, test);
378
379 test->ptr = NULL;
380 test->len = 0;
381
382 if (test->flags & ACL_TEST_F_MAY_CHANGE)
383 return 0;
384
385 test->flags = ACL_TEST_F_VOLATILE;
386 test->i = ret;
387
388 return 1;
389}
390
Willy Tarreau58393e12008-07-20 10:39:22 +0200391
392/*
393 * These functions are exported and may be used by any other component.
394 */
395
396/* ignore the current line */
397int acl_parse_nothing(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreaua5909832007-06-17 20:40:25 +0200398{
Willy Tarreau58393e12008-07-20 10:39:22 +0200399 return 1;
400}
401
402/* always fake a data retrieval */
403int acl_fetch_nothing(struct proxy *px, struct session *l4, void *l7, int dir,
404 struct acl_expr *expr, struct acl_test *test)
405{
406 return 1;
Willy Tarreaua5909832007-06-17 20:40:25 +0200407}
408
409/* always return false */
Willy Tarreau58393e12008-07-20 10:39:22 +0200410int acl_match_nothing(struct acl_test *test, struct acl_pattern *pattern)
Willy Tarreaua5909832007-06-17 20:40:25 +0200411{
Willy Tarreau11382812008-07-09 16:18:21 +0200412 return ACL_PAT_FAIL;
Willy Tarreaua5909832007-06-17 20:40:25 +0200413}
414
415
Willy Tarreaua84d3742007-05-07 00:36:48 +0200416/* NB: For two strings to be identical, it is required that their lengths match */
417int acl_match_str(struct acl_test *test, struct acl_pattern *pattern)
418{
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200419 int icase;
420
Willy Tarreaua84d3742007-05-07 00:36:48 +0200421 if (pattern->len != test->len)
Willy Tarreau11382812008-07-09 16:18:21 +0200422 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200423
424 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
425 if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) == 0) ||
426 (!icase && strncmp(pattern->ptr.str, test->ptr, test->len) == 0))
Willy Tarreau11382812008-07-09 16:18:21 +0200427 return ACL_PAT_PASS;
428 return ACL_PAT_FAIL;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200429}
430
Willy Tarreauc4262962010-05-10 23:42:40 +0200431/* Lookup a string in the expression's pattern tree. The node is returned if it
432 * exists, otherwise NULL.
433 */
434void *acl_lookup_str(struct acl_test *test, struct acl_expr *expr)
435{
436 /* data are stored in a tree */
437 struct ebmb_node *node;
438 char prev;
439
440 /* we may have to force a trailing zero on the test pattern */
441 prev = test->ptr[test->len];
442 if (prev)
443 test->ptr[test->len] = '\0';
444 node = ebst_lookup(&expr->pattern_tree, test->ptr);
445 if (prev)
446 test->ptr[test->len] = prev;
447 return node;
448}
449
Willy Tarreauf3d25982007-05-08 22:45:09 +0200450/* Executes a regex. It needs to change the data. If it is marked READ_ONLY
451 * then it will be allocated and duplicated in place so that others may use
452 * it later on. Note that this is embarrassing because we always try to avoid
453 * allocating memory at run time.
454 */
455int acl_match_reg(struct acl_test *test, struct acl_pattern *pattern)
456{
457 char old_char;
458 int ret;
459
460 if (unlikely(test->flags & ACL_TEST_F_READ_ONLY)) {
461 char *new_str;
462
463 new_str = calloc(1, test->len + 1);
464 if (!new_str)
Willy Tarreau11382812008-07-09 16:18:21 +0200465 return ACL_PAT_FAIL;
Willy Tarreauf3d25982007-05-08 22:45:09 +0200466
467 memcpy(new_str, test->ptr, test->len);
468 new_str[test->len] = 0;
469 if (test->flags & ACL_TEST_F_MUST_FREE)
470 free(test->ptr);
471 test->ptr = new_str;
472 test->flags |= ACL_TEST_F_MUST_FREE;
473 test->flags &= ~ACL_TEST_F_READ_ONLY;
474 }
475
476 old_char = test->ptr[test->len];
477 test->ptr[test->len] = 0;
478
479 if (regexec(pattern->ptr.reg, test->ptr, 0, NULL, 0) == 0)
Willy Tarreau11382812008-07-09 16:18:21 +0200480 ret = ACL_PAT_PASS;
Willy Tarreauf3d25982007-05-08 22:45:09 +0200481 else
Willy Tarreau11382812008-07-09 16:18:21 +0200482 ret = ACL_PAT_FAIL;
Willy Tarreauf3d25982007-05-08 22:45:09 +0200483
484 test->ptr[test->len] = old_char;
485 return ret;
486}
487
Willy Tarreaua84d3742007-05-07 00:36:48 +0200488/* Checks that the pattern matches the beginning of the tested string. */
489int acl_match_beg(struct acl_test *test, struct acl_pattern *pattern)
490{
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200491 int icase;
492
Willy Tarreaua84d3742007-05-07 00:36:48 +0200493 if (pattern->len > test->len)
Willy Tarreau11382812008-07-09 16:18:21 +0200494 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200495
496 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
497 if ((icase && strncasecmp(pattern->ptr.str, test->ptr, pattern->len) != 0) ||
498 (!icase && strncmp(pattern->ptr.str, test->ptr, pattern->len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +0200499 return ACL_PAT_FAIL;
500 return ACL_PAT_PASS;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200501}
502
503/* Checks that the pattern matches the end of the tested string. */
504int acl_match_end(struct acl_test *test, struct acl_pattern *pattern)
505{
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200506 int icase;
507
Willy Tarreaua84d3742007-05-07 00:36:48 +0200508 if (pattern->len > test->len)
Willy Tarreau11382812008-07-09 16:18:21 +0200509 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200510 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
511 if ((icase && strncasecmp(pattern->ptr.str, test->ptr + test->len - pattern->len, pattern->len) != 0) ||
512 (!icase && strncmp(pattern->ptr.str, test->ptr + test->len - pattern->len, pattern->len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +0200513 return ACL_PAT_FAIL;
514 return ACL_PAT_PASS;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200515}
516
517/* Checks that the pattern is included inside the tested string.
518 * NB: Suboptimal, should be rewritten using a Boyer-Moore method.
519 */
520int acl_match_sub(struct acl_test *test, struct acl_pattern *pattern)
521{
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200522 int icase;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200523 char *end;
524 char *c;
525
526 if (pattern->len > test->len)
Willy Tarreau11382812008-07-09 16:18:21 +0200527 return ACL_PAT_FAIL;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200528
529 end = test->ptr + test->len - pattern->len;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200530 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
531 if (icase) {
532 for (c = test->ptr; c <= end; c++) {
533 if (tolower(*c) != tolower(*pattern->ptr.str))
534 continue;
535 if (strncasecmp(pattern->ptr.str, c, pattern->len) == 0)
Willy Tarreau11382812008-07-09 16:18:21 +0200536 return ACL_PAT_PASS;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200537 }
538 } else {
539 for (c = test->ptr; c <= end; c++) {
540 if (*c != *pattern->ptr.str)
541 continue;
542 if (strncmp(pattern->ptr.str, c, pattern->len) == 0)
Willy Tarreau11382812008-07-09 16:18:21 +0200543 return ACL_PAT_PASS;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200544 }
Willy Tarreaua84d3742007-05-07 00:36:48 +0200545 }
Willy Tarreau11382812008-07-09 16:18:21 +0200546 return ACL_PAT_FAIL;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200547}
548
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200549/* Background: Fast way to find a zero byte in a word
550 * http://graphics.stanford.edu/~seander/bithacks.html#ZeroInWord
551 * hasZeroByte = (v - 0x01010101UL) & ~v & 0x80808080UL;
552 *
553 * To look for 4 different byte values, xor the word with those bytes and
554 * then check for zero bytes:
555 *
556 * v = (((unsigned char)c * 0x1010101U) ^ delimiter)
557 * where <delimiter> is the 4 byte values to look for (as an uint)
558 * and <c> is the character that is being tested
559 */
560static inline unsigned int is_delimiter(unsigned char c, unsigned int mask)
561{
562 mask ^= (c * 0x01010101); /* propagate the char to all 4 bytes */
563 return (mask - 0x01010101) & ~mask & 0x80808080U;
564}
565
566static inline unsigned int make_4delim(unsigned char d1, unsigned char d2, unsigned char d3, unsigned char d4)
567{
568 return d1 << 24 | d2 << 16 | d3 << 8 | d4;
569}
570
Willy Tarreaua84d3742007-05-07 00:36:48 +0200571/* This one is used by other real functions. It checks that the pattern is
572 * included inside the tested string, but enclosed between the specified
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200573 * delimiters or at the beginning or end of the string. The delimiters are
574 * provided as an unsigned int made by make_4delim() and match up to 4 different
575 * delimiters. Delimiters are stripped at the beginning and end of the pattern.
Willy Tarreaua84d3742007-05-07 00:36:48 +0200576 */
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200577static int match_word(struct acl_test *test, struct acl_pattern *pattern, unsigned int delimiters)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200578{
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200579 int may_match, icase;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200580 char *c, *end;
581 char *ps;
582 int pl;
583
584 pl = pattern->len;
585 ps = pattern->ptr.str;
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200586
587 while (pl > 0 && is_delimiter(*ps, delimiters)) {
Willy Tarreaua84d3742007-05-07 00:36:48 +0200588 pl--;
589 ps++;
590 }
591
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200592 while (pl > 0 && is_delimiter(ps[pl - 1], delimiters))
Willy Tarreaua84d3742007-05-07 00:36:48 +0200593 pl--;
594
595 if (pl > test->len)
Willy Tarreau11382812008-07-09 16:18:21 +0200596 return ACL_PAT_FAIL;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200597
598 may_match = 1;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200599 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200600 end = test->ptr + test->len - pl;
601 for (c = test->ptr; c <= end; c++) {
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200602 if (is_delimiter(*c, delimiters)) {
Willy Tarreaua84d3742007-05-07 00:36:48 +0200603 may_match = 1;
604 continue;
605 }
Willy Tarreaua84d3742007-05-07 00:36:48 +0200606
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200607 if (!may_match)
608 continue;
609
610 if (icase) {
611 if ((tolower(*c) == tolower(*ps)) &&
612 (strncasecmp(ps, c, pl) == 0) &&
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200613 (c == end || is_delimiter(c[pl], delimiters)))
Willy Tarreau11382812008-07-09 16:18:21 +0200614 return ACL_PAT_PASS;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200615 } else {
616 if ((*c == *ps) &&
617 (strncmp(ps, c, pl) == 0) &&
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200618 (c == end || is_delimiter(c[pl], delimiters)))
Willy Tarreau11382812008-07-09 16:18:21 +0200619 return ACL_PAT_PASS;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200620 }
Willy Tarreaua84d3742007-05-07 00:36:48 +0200621 may_match = 0;
622 }
Willy Tarreau11382812008-07-09 16:18:21 +0200623 return ACL_PAT_FAIL;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200624}
625
626/* Checks that the pattern is included inside the tested string, but enclosed
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200627 * between the delimiters '?' or '/' or at the beginning or end of the string.
628 * Delimiters at the beginning or end of the pattern are ignored.
Willy Tarreaua84d3742007-05-07 00:36:48 +0200629 */
630int acl_match_dir(struct acl_test *test, struct acl_pattern *pattern)
631{
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200632 return match_word(test, pattern, make_4delim('/', '?', '?', '?'));
Willy Tarreaua84d3742007-05-07 00:36:48 +0200633}
634
635/* Checks that the pattern is included inside the tested string, but enclosed
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200636 * between the delmiters '/', '?', '.' or ":" or at the beginning or end of
637 * the string. Delimiters at the beginning or end of the pattern are ignored.
Willy Tarreaua84d3742007-05-07 00:36:48 +0200638 */
639int acl_match_dom(struct acl_test *test, struct acl_pattern *pattern)
640{
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200641 return match_word(test, pattern, make_4delim('/', '?', '.', ':'));
Willy Tarreaua84d3742007-05-07 00:36:48 +0200642}
643
644/* Checks that the integer in <test> is included between min and max */
Willy Tarreauae8b7962007-06-09 23:10:04 +0200645int acl_match_int(struct acl_test *test, struct acl_pattern *pattern)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200646{
Willy Tarreauae8b7962007-06-09 23:10:04 +0200647 if ((!pattern->val.range.min_set || pattern->val.range.min <= test->i) &&
648 (!pattern->val.range.max_set || test->i <= pattern->val.range.max))
Willy Tarreau11382812008-07-09 16:18:21 +0200649 return ACL_PAT_PASS;
650 return ACL_PAT_FAIL;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200651}
652
Willy Tarreaua67fad92007-05-08 19:50:09 +0200653int acl_match_ip(struct acl_test *test, struct acl_pattern *pattern)
654{
655 struct in_addr *s;
656
657 if (test->i != AF_INET)
Willy Tarreau11382812008-07-09 16:18:21 +0200658 return ACL_PAT_FAIL;
Willy Tarreaua67fad92007-05-08 19:50:09 +0200659
660 s = (void *)test->ptr;
661 if (((s->s_addr ^ pattern->val.ipv4.addr.s_addr) & pattern->val.ipv4.mask.s_addr) == 0)
Willy Tarreau11382812008-07-09 16:18:21 +0200662 return ACL_PAT_PASS;
663 return ACL_PAT_FAIL;
Willy Tarreaua67fad92007-05-08 19:50:09 +0200664}
665
Willy Tarreaub337b532010-05-13 20:03:41 +0200666/* Lookup an IPv4 address in the expression's pattern tree using the longest
667 * match method. The node is returned if it exists, otherwise NULL.
668 */
669void *acl_lookup_ip(struct acl_test *test, struct acl_expr *expr)
670{
671 struct in_addr *s;
672
673 if (test->i != AF_INET)
674 return ACL_PAT_FAIL;
675
676 s = (void *)test->ptr;
677
678 return ebmb_lookup_longest(&expr->pattern_tree, &s->s_addr);
679}
680
Willy Tarreaua84d3742007-05-07 00:36:48 +0200681/* Parse a string. It is allocated and duplicated. */
Willy Tarreauae8b7962007-06-09 23:10:04 +0200682int acl_parse_str(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200683{
684 int len;
685
Willy Tarreauae8b7962007-06-09 23:10:04 +0200686 len = strlen(*text);
Willy Tarreauc4262962010-05-10 23:42:40 +0200687
688 if (pattern->flags & ACL_PAT_F_TREE_OK) {
689 /* we're allowed to put the data in a tree whose root is pointed
690 * to by val.tree.
691 */
692 struct ebmb_node *node;
693
694 node = calloc(1, sizeof(*node) + len + 1);
695 if (!node)
696 return 0;
697 memcpy(node->key, *text, len + 1);
698 if (ebst_insert(pattern->val.tree, node) != node)
699 free(node); /* was a duplicate */
700 pattern->flags |= ACL_PAT_F_TREE; /* this pattern now contains a tree */
701 return 1;
702 }
703
Willy Tarreauae8b7962007-06-09 23:10:04 +0200704 pattern->ptr.str = strdup(*text);
Willy Tarreaua84d3742007-05-07 00:36:48 +0200705 if (!pattern->ptr.str)
706 return 0;
707 pattern->len = len;
708 return 1;
709}
710
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +0100711/* Parse and concatenate all further strings into one. */
712int
713acl_parse_strcat(const char **text, struct acl_pattern *pattern, int *opaque)
714{
715
716 int len = 0, i;
717 char *s;
718
719 for (i = 0; *text[i]; i++)
720 len += strlen(text[i])+1;
721
722 pattern->ptr.str = s = calloc(1, len);
723 if (!pattern->ptr.str)
724 return 0;
725
726 for (i = 0; *text[i]; i++)
727 s += sprintf(s, i?" %s":"%s", text[i]);
728
729 pattern->len = len;
730
731 return i;
732}
733
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +0200734/* Free data allocated by acl_parse_reg */
735static void acl_free_reg(void *ptr) {
736
737 regfree((regex_t *)ptr);
738}
739
Willy Tarreauf3d25982007-05-08 22:45:09 +0200740/* Parse a regex. It is allocated. */
Willy Tarreauae8b7962007-06-09 23:10:04 +0200741int acl_parse_reg(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreauf3d25982007-05-08 22:45:09 +0200742{
743 regex_t *preg;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200744 int icase;
Willy Tarreauf3d25982007-05-08 22:45:09 +0200745
746 preg = calloc(1, sizeof(regex_t));
747
748 if (!preg)
749 return 0;
750
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200751 icase = (pattern->flags & ACL_PAT_F_IGNORE_CASE) ? REG_ICASE : 0;
752 if (regcomp(preg, *text, REG_EXTENDED | REG_NOSUB | icase) != 0) {
Willy Tarreauf3d25982007-05-08 22:45:09 +0200753 free(preg);
754 return 0;
755 }
756
757 pattern->ptr.reg = preg;
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +0200758 pattern->freeptrbuf = &acl_free_reg;
Willy Tarreauf3d25982007-05-08 22:45:09 +0200759 return 1;
760}
761
Willy Tarreauae8b7962007-06-09 23:10:04 +0200762/* Parse a range of positive integers delimited by either ':' or '-'. If only
763 * one integer is read, it is set as both min and max. An operator may be
764 * specified as the prefix, among this list of 5 :
765 *
766 * 0:eq, 1:gt, 2:ge, 3:lt, 4:le
767 *
768 * The default operator is "eq". It supports range matching. Ranges are
769 * rejected for other operators. The operator may be changed at any time.
770 * The operator is stored in the 'opaque' argument.
771 *
Willy Tarreaua84d3742007-05-07 00:36:48 +0200772 */
Willy Tarreauae8b7962007-06-09 23:10:04 +0200773int acl_parse_int(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200774{
Willy Tarreauae8b7962007-06-09 23:10:04 +0200775 signed long long i;
776 unsigned int j, last, skip = 0;
777 const char *ptr = *text;
778
779
Willy Tarreau8f8e6452007-06-17 21:51:38 +0200780 while (!isdigit((unsigned char)*ptr)) {
Willy Tarreau1c7cc5b2010-07-18 10:46:33 +0200781 switch (get_std_op(ptr)) {
782 case STD_OP_EQ: *opaque = 0; break;
783 case STD_OP_GT: *opaque = 1; break;
784 case STD_OP_GE: *opaque = 2; break;
785 case STD_OP_LT: *opaque = 3; break;
786 case STD_OP_LE: *opaque = 4; break;
787 default:
Willy Tarreauae8b7962007-06-09 23:10:04 +0200788 return 0;
Willy Tarreau1c7cc5b2010-07-18 10:46:33 +0200789 }
Willy Tarreauae8b7962007-06-09 23:10:04 +0200790
791 skip++;
792 ptr = text[skip];
793 }
Willy Tarreaua84d3742007-05-07 00:36:48 +0200794
795 last = i = 0;
796 while (1) {
Willy Tarreauae8b7962007-06-09 23:10:04 +0200797 j = *ptr++;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200798 if ((j == '-' || j == ':') && !last) {
799 last++;
800 pattern->val.range.min = i;
801 i = 0;
802 continue;
803 }
804 j -= '0';
805 if (j > 9)
806 // also catches the terminating zero
807 break;
808 i *= 10;
809 i += j;
810 }
Willy Tarreauae8b7962007-06-09 23:10:04 +0200811
812 if (last && *opaque >= 1 && *opaque <= 4)
813 /* having a range with a min or a max is absurd */
814 return 0;
815
Willy Tarreaua84d3742007-05-07 00:36:48 +0200816 if (!last)
817 pattern->val.range.min = i;
818 pattern->val.range.max = i;
Willy Tarreauae8b7962007-06-09 23:10:04 +0200819
820 switch (*opaque) {
821 case 0: /* eq */
822 pattern->val.range.min_set = 1;
823 pattern->val.range.max_set = 1;
824 break;
825 case 1: /* gt */
826 pattern->val.range.min++; /* gt = ge + 1 */
827 case 2: /* ge */
828 pattern->val.range.min_set = 1;
829 pattern->val.range.max_set = 0;
830 break;
831 case 3: /* lt */
832 pattern->val.range.max--; /* lt = le - 1 */
833 case 4: /* le */
834 pattern->val.range.min_set = 0;
835 pattern->val.range.max_set = 1;
836 break;
837 }
838 return skip + 1;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200839}
840
Willy Tarreau4a26d2f2008-07-15 16:05:33 +0200841/* Parse a range of positive 2-component versions delimited by either ':' or
842 * '-'. The version consists in a major and a minor, both of which must be
843 * smaller than 65536, because internally they will be represented as a 32-bit
844 * integer.
845 * If only one version is read, it is set as both min and max. Just like for
846 * pure integers, an operator may be specified as the prefix, among this list
847 * of 5 :
848 *
849 * 0:eq, 1:gt, 2:ge, 3:lt, 4:le
850 *
851 * The default operator is "eq". It supports range matching. Ranges are
852 * rejected for other operators. The operator may be changed at any time.
853 * The operator is stored in the 'opaque' argument. This allows constructs
854 * such as the following one :
855 *
856 * acl obsolete_ssl ssl_req_proto lt 3
857 * acl unsupported_ssl ssl_req_proto gt 3.1
858 * acl valid_ssl ssl_req_proto 3.0-3.1
859 *
860 */
861int acl_parse_dotted_ver(const char **text, struct acl_pattern *pattern, int *opaque)
862{
863 signed long long i;
864 unsigned int j, last, skip = 0;
865 const char *ptr = *text;
866
867
868 while (!isdigit((unsigned char)*ptr)) {
Willy Tarreau1c7cc5b2010-07-18 10:46:33 +0200869 switch (get_std_op(ptr)) {
870 case STD_OP_EQ: *opaque = 0; break;
871 case STD_OP_GT: *opaque = 1; break;
872 case STD_OP_GE: *opaque = 2; break;
873 case STD_OP_LT: *opaque = 3; break;
874 case STD_OP_LE: *opaque = 4; break;
875 default:
Willy Tarreau4a26d2f2008-07-15 16:05:33 +0200876 return 0;
Willy Tarreau1c7cc5b2010-07-18 10:46:33 +0200877 }
Willy Tarreau4a26d2f2008-07-15 16:05:33 +0200878
879 skip++;
880 ptr = text[skip];
881 }
882
883 last = i = 0;
884 while (1) {
885 j = *ptr++;
886 if (j == '.') {
887 /* minor part */
888 if (i >= 65536)
889 return 0;
890 i <<= 16;
891 continue;
892 }
893 if ((j == '-' || j == ':') && !last) {
894 last++;
895 if (i < 65536)
896 i <<= 16;
897 pattern->val.range.min = i;
898 i = 0;
899 continue;
900 }
901 j -= '0';
902 if (j > 9)
903 // also catches the terminating zero
904 break;
905 i = (i & 0xFFFF0000) + (i & 0xFFFF) * 10;
906 i += j;
907 }
908
909 /* if we only got a major version, let's shift it now */
910 if (i < 65536)
911 i <<= 16;
912
913 if (last && *opaque >= 1 && *opaque <= 4)
914 /* having a range with a min or a max is absurd */
915 return 0;
916
917 if (!last)
918 pattern->val.range.min = i;
919 pattern->val.range.max = i;
920
921 switch (*opaque) {
922 case 0: /* eq */
923 pattern->val.range.min_set = 1;
924 pattern->val.range.max_set = 1;
925 break;
926 case 1: /* gt */
927 pattern->val.range.min++; /* gt = ge + 1 */
928 case 2: /* ge */
929 pattern->val.range.min_set = 1;
930 pattern->val.range.max_set = 0;
931 break;
932 case 3: /* lt */
933 pattern->val.range.max--; /* lt = le - 1 */
934 case 4: /* le */
935 pattern->val.range.min_set = 0;
936 pattern->val.range.max_set = 1;
937 break;
938 }
939 return skip + 1;
940}
941
Willy Tarreaua67fad92007-05-08 19:50:09 +0200942/* Parse an IP address and an optional mask in the form addr[/mask].
943 * The addr may either be an IPv4 address or a hostname. The mask
944 * may either be a dotted mask or a number of bits. Returns 1 if OK,
945 * otherwise 0.
946 */
Willy Tarreauae8b7962007-06-09 23:10:04 +0200947int acl_parse_ip(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreaua67fad92007-05-08 19:50:09 +0200948{
Willy Tarreaub337b532010-05-13 20:03:41 +0200949 struct eb_root *tree = NULL;
950 if (pattern->flags & ACL_PAT_F_TREE_OK)
951 tree = pattern->val.tree;
952
953 if (str2net(*text, &pattern->val.ipv4.addr, &pattern->val.ipv4.mask)) {
954 unsigned int mask = ntohl(pattern->val.ipv4.mask.s_addr);
955 struct ebmb_node *node;
956 /* check if the mask is contiguous so that we can insert the
957 * network into the tree. A continuous mask has only ones on
958 * the left. This means that this mask + its lower bit added
959 * once again is null.
960 */
961 if (mask + (mask & -mask) == 0 && tree) {
962 mask = mask ? 33 - flsnz(mask & -mask) : 0; /* equals cidr value */
963 /* FIXME: insert <addr>/<mask> into the tree here */
964 node = calloc(1, sizeof(*node) + 4); /* reserve 4 bytes for IPv4 address */
965 if (!node)
966 return 0;
967 memcpy(node->key, &pattern->val.ipv4.addr, 4); /* network byte order */
968 node->node.pfx = mask;
969 if (ebmb_insert_prefix(tree, node, 4) != node)
970 free(node); /* was a duplicate */
971 pattern->flags |= ACL_PAT_F_TREE;
972 return 1;
973 }
Willy Tarreauae8b7962007-06-09 23:10:04 +0200974 return 1;
Willy Tarreaub337b532010-05-13 20:03:41 +0200975 }
Willy Tarreauae8b7962007-06-09 23:10:04 +0200976 else
977 return 0;
Willy Tarreaua67fad92007-05-08 19:50:09 +0200978}
979
Willy Tarreaua84d3742007-05-07 00:36:48 +0200980/*
981 * Registers the ACL keyword list <kwl> as a list of valid keywords for next
982 * parsing sessions.
983 */
984void acl_register_keywords(struct acl_kw_list *kwl)
985{
986 LIST_ADDQ(&acl_keywords.list, &kwl->list);
987}
988
989/*
990 * Unregisters the ACL keyword list <kwl> from the list of valid keywords.
991 */
992void acl_unregister_keywords(struct acl_kw_list *kwl)
993{
994 LIST_DEL(&kwl->list);
995 LIST_INIT(&kwl->list);
996}
997
998/* Return a pointer to the ACL <name> within the list starting at <head>, or
999 * NULL if not found.
1000 */
1001struct acl *find_acl_by_name(const char *name, struct list *head)
1002{
1003 struct acl *acl;
1004 list_for_each_entry(acl, head, list) {
1005 if (strcmp(acl->name, name) == 0)
1006 return acl;
1007 }
1008 return NULL;
1009}
1010
1011/* Return a pointer to the ACL keyword <kw>, or NULL if not found. Note that if
1012 * <kw> contains an opening parenthesis, only the left part of it is checked.
1013 */
1014struct acl_keyword *find_acl_kw(const char *kw)
1015{
1016 int index;
1017 const char *kwend;
1018 struct acl_kw_list *kwl;
1019
1020 kwend = strchr(kw, '(');
1021 if (!kwend)
1022 kwend = kw + strlen(kw);
1023
1024 list_for_each_entry(kwl, &acl_keywords.list, list) {
1025 for (index = 0; kwl->kw[index].kw != NULL; index++) {
1026 if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) &&
1027 kwl->kw[index].kw[kwend-kw] == 0)
1028 return &kwl->kw[index];
1029 }
1030 }
1031 return NULL;
1032}
1033
Willy Tarreaudfd7fca2011-03-09 07:27:02 +01001034/* NB: does nothing if <pat> is NULL */
Willy Tarreaua84d3742007-05-07 00:36:48 +02001035static void free_pattern(struct acl_pattern *pat)
1036{
Willy Tarreaudfd7fca2011-03-09 07:27:02 +01001037 if (!pat)
1038 return;
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02001039
1040 if (pat->ptr.ptr) {
1041 if (pat->freeptrbuf)
1042 pat->freeptrbuf(pat->ptr.ptr);
1043
Willy Tarreaua84d3742007-05-07 00:36:48 +02001044 free(pat->ptr.ptr);
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02001045 }
1046
Willy Tarreaua84d3742007-05-07 00:36:48 +02001047 free(pat);
1048}
1049
1050static void free_pattern_list(struct list *head)
1051{
1052 struct acl_pattern *pat, *tmp;
1053 list_for_each_entry_safe(pat, tmp, head, list)
1054 free_pattern(pat);
1055}
1056
Willy Tarreaue56cda92010-05-11 23:25:05 +02001057static void free_pattern_tree(struct eb_root *root)
1058{
1059 struct eb_node *node, *next;
1060 node = eb_first(root);
1061 while (node) {
1062 next = eb_next(node);
1063 free(node);
1064 node = next;
1065 }
1066}
1067
Willy Tarreaua84d3742007-05-07 00:36:48 +02001068static struct acl_expr *prune_acl_expr(struct acl_expr *expr)
1069{
1070 free_pattern_list(&expr->patterns);
Willy Tarreaue56cda92010-05-11 23:25:05 +02001071 free_pattern_tree(&expr->pattern_tree);
Willy Tarreaua84d3742007-05-07 00:36:48 +02001072 LIST_INIT(&expr->patterns);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001073 if (expr->arg_len && expr->arg.str)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001074 free(expr->arg.str);
1075 expr->kw->use_cnt--;
1076 return expr;
1077}
1078
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001079static int acl_read_patterns_from_file( struct acl_keyword *aclkw,
1080 struct acl_expr *expr,
1081 const char *filename, int patflags)
1082{
1083 FILE *file;
1084 char *c;
1085 const char *args[2];
1086 struct acl_pattern *pattern;
1087 int opaque;
Willy Tarreau6a8097f2011-02-26 15:14:15 +01001088 int ret = 0;
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001089
1090 file = fopen(filename, "r");
1091 if (!file)
1092 return 0;
1093
1094 /* now parse all patterns. The file may contain only one pattern per
1095 * line. If the line contains spaces, they will be part of the pattern.
1096 * The pattern stops at the first CR, LF or EOF encountered.
1097 */
1098 opaque = 0;
Willy Tarreaue56cda92010-05-11 23:25:05 +02001099 pattern = NULL;
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001100 args[1] = "";
1101 while (fgets(trash, sizeof(trash), file) != NULL) {
1102
1103 c = trash;
Willy Tarreau58215a02010-05-13 22:07:43 +02001104
1105 /* ignore lines beginning with a dash */
1106 if (*c == '#')
1107 continue;
1108
1109 /* strip leading spaces and tabs */
1110 while (*c == ' ' || *c == '\t')
1111 c++;
1112
Willy Tarreau58215a02010-05-13 22:07:43 +02001113
1114 args[0] = c;
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001115 while (*c && *c != '\n' && *c != '\r')
1116 c++;
1117 *c = 0;
1118
Willy Tarreau51091962011-01-03 21:04:10 +01001119 /* empty lines are ignored too */
1120 if (c == args[0])
1121 continue;
1122
Willy Tarreaue56cda92010-05-11 23:25:05 +02001123 /* we keep the previous pattern along iterations as long as it's not used */
1124 if (!pattern)
1125 pattern = (struct acl_pattern *)malloc(sizeof(*pattern));
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001126 if (!pattern)
1127 goto out_close;
Willy Tarreaue56cda92010-05-11 23:25:05 +02001128
1129 memset(pattern, 0, sizeof(*pattern));
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001130 pattern->flags = patflags;
1131
Willy Tarreaue56cda92010-05-11 23:25:05 +02001132 if ((aclkw->requires & ACL_MAY_LOOKUP) && !(pattern->flags & ACL_PAT_F_IGNORE_CASE)) {
1133 /* we pre-set the data pointer to the tree's head so that functions
1134 * which are able to insert in a tree know where to do that.
1135 */
1136 pattern->flags |= ACL_PAT_F_TREE_OK;
1137 pattern->val.tree = &expr->pattern_tree;
1138 }
1139
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001140 if (!aclkw->parse(args, pattern, &opaque))
1141 goto out_free_pattern;
Willy Tarreaue56cda92010-05-11 23:25:05 +02001142
1143 /* if the parser did not feed the tree, let's chain the pattern to the list */
1144 if (!(pattern->flags & ACL_PAT_F_TREE)) {
1145 LIST_ADDQ(&expr->patterns, &pattern->list);
1146 pattern = NULL; /* get a new one */
1147 }
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001148 }
Willy Tarreau6a8097f2011-02-26 15:14:15 +01001149
1150 ret = 1; /* success */
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001151
1152 out_free_pattern:
1153 free_pattern(pattern);
1154 out_close:
1155 fclose(file);
Willy Tarreau6a8097f2011-02-26 15:14:15 +01001156 return ret;
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001157}
1158
Willy Tarreaua84d3742007-05-07 00:36:48 +02001159/* Parse an ACL expression starting at <args>[0], and return it.
1160 * Right now, the only accepted syntax is :
1161 * <subject> [<value>...]
1162 */
1163struct acl_expr *parse_acl_expr(const char **args)
1164{
1165 __label__ out_return, out_free_expr, out_free_pattern;
1166 struct acl_expr *expr;
1167 struct acl_keyword *aclkw;
1168 struct acl_pattern *pattern;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02001169 int opaque, patflags;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001170 const char *arg;
1171
1172 aclkw = find_acl_kw(args[0]);
1173 if (!aclkw || !aclkw->parse)
1174 goto out_return;
1175
1176 expr = (struct acl_expr *)calloc(1, sizeof(*expr));
1177 if (!expr)
1178 goto out_return;
1179
1180 expr->kw = aclkw;
1181 aclkw->use_cnt++;
1182 LIST_INIT(&expr->patterns);
Willy Tarreaue56cda92010-05-11 23:25:05 +02001183 expr->pattern_tree = EB_ROOT_UNIQUE;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001184 expr->arg.str = NULL;
Willy Tarreaubb768912007-06-10 11:17:01 +02001185 expr->arg_len = 0;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001186
1187 arg = strchr(args[0], '(');
1188 if (arg != NULL) {
1189 char *end, *arg2;
1190 /* there is an argument in the form "subject(arg)" */
1191 arg++;
1192 end = strchr(arg, ')');
1193 if (!end)
1194 goto out_free_expr;
Willy Tarreauac778f52010-01-26 19:02:46 +01001195 arg2 = my_strndup(arg, end - arg);
Willy Tarreau1edb1442010-01-27 20:13:38 +01001196 if (!arg2)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001197 goto out_free_expr;
Willy Tarreaubb768912007-06-10 11:17:01 +02001198 expr->arg_len = end - arg;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001199 expr->arg.str = arg2;
1200 }
1201
Willy Tarreaua84d3742007-05-07 00:36:48 +02001202 args++;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02001203
1204 /* check for options before patterns. Supported options are :
1205 * -i : ignore case for all patterns by default
1206 * -f : read patterns from those files
1207 * -- : everything after this is not an option
1208 */
1209 patflags = 0;
1210 while (**args == '-') {
1211 if ((*args)[1] == 'i')
1212 patflags |= ACL_PAT_F_IGNORE_CASE;
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001213 else if ((*args)[1] == 'f') {
1214 if (!acl_read_patterns_from_file(aclkw, expr, args[1], patflags | ACL_PAT_F_FROM_FILE))
1215 goto out_free_expr;
1216 args++;
1217 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +02001218 else if ((*args)[1] == '-') {
1219 args++;
1220 break;
1221 }
1222 else
1223 break;
1224 args++;
1225 }
1226
1227 /* now parse all patterns */
Willy Tarreauae8b7962007-06-09 23:10:04 +02001228 opaque = 0;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001229 while (**args) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02001230 int ret;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001231 pattern = (struct acl_pattern *)calloc(1, sizeof(*pattern));
1232 if (!pattern)
1233 goto out_free_expr;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02001234 pattern->flags = patflags;
1235
Willy Tarreauae8b7962007-06-09 23:10:04 +02001236 ret = aclkw->parse(args, pattern, &opaque);
1237 if (!ret)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001238 goto out_free_pattern;
1239 LIST_ADDQ(&expr->patterns, &pattern->list);
Willy Tarreauae8b7962007-06-09 23:10:04 +02001240 args += ret;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001241 }
1242
1243 return expr;
1244
1245 out_free_pattern:
1246 free_pattern(pattern);
1247 out_free_expr:
1248 prune_acl_expr(expr);
1249 free(expr);
1250 out_return:
1251 return NULL;
1252}
1253
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02001254/* Purge everything in the acl <acl>, then return <acl>. */
1255struct acl *prune_acl(struct acl *acl) {
1256
1257 struct acl_expr *expr, *exprb;
1258
1259 free(acl->name);
1260
1261 list_for_each_entry_safe(expr, exprb, &acl->expr, list) {
1262 LIST_DEL(&expr->list);
1263 prune_acl_expr(expr);
1264 free(expr);
1265 }
1266
1267 return acl;
1268}
1269
Willy Tarreaua84d3742007-05-07 00:36:48 +02001270/* Parse an ACL with the name starting at <args>[0], and with a list of already
1271 * known ACLs in <acl>. If the ACL was not in the list, it will be added.
Willy Tarreau2a56c5e2010-03-15 16:13:29 +01001272 * A pointer to that ACL is returned. If the ACL has an empty name, then it's
1273 * an anonymous one and it won't be merged with any other one.
Willy Tarreaua84d3742007-05-07 00:36:48 +02001274 *
1275 * args syntax: <aclname> <acl_expr>
1276 */
1277struct acl *parse_acl(const char **args, struct list *known_acl)
1278{
1279 __label__ out_return, out_free_acl_expr, out_free_name;
1280 struct acl *cur_acl;
1281 struct acl_expr *acl_expr;
1282 char *name;
1283
Willy Tarreau2a56c5e2010-03-15 16:13:29 +01001284 if (**args && invalid_char(*args))
Willy Tarreau2e74c3f2007-12-02 18:45:09 +01001285 goto out_return;
1286
Willy Tarreaua84d3742007-05-07 00:36:48 +02001287 acl_expr = parse_acl_expr(args + 1);
1288 if (!acl_expr)
1289 goto out_return;
1290
Willy Tarreau404e8ab2009-07-26 19:40:40 +02001291 /* Check for args beginning with an opening parenthesis just after the
1292 * subject, as this is almost certainly a typo. Right now we can only
1293 * emit a warning, so let's do so.
1294 */
Krzysztof Piotr Oledzki4cdd8312009-10-05 00:23:35 +02001295 if (!strchr(args[1], '(') && *args[2] == '(')
Willy Tarreau404e8ab2009-07-26 19:40:40 +02001296 Warning("parsing acl '%s' :\n"
1297 " matching '%s' for pattern '%s' is likely a mistake and probably\n"
1298 " not what you want. Maybe you need to remove the extraneous space before '('.\n"
1299 " If you are really sure this is not an error, please insert '--' between the\n"
1300 " match and the pattern to make this warning message disappear.\n",
1301 args[0], args[1], args[2]);
1302
Willy Tarreau2a56c5e2010-03-15 16:13:29 +01001303 if (*args[0])
1304 cur_acl = find_acl_by_name(args[0], known_acl);
1305 else
1306 cur_acl = NULL;
1307
Willy Tarreaua84d3742007-05-07 00:36:48 +02001308 if (!cur_acl) {
1309 name = strdup(args[0]);
1310 if (!name)
1311 goto out_free_acl_expr;
1312 cur_acl = (struct acl *)calloc(1, sizeof(*cur_acl));
1313 if (cur_acl == NULL)
1314 goto out_free_name;
1315
1316 LIST_INIT(&cur_acl->expr);
1317 LIST_ADDQ(known_acl, &cur_acl->list);
1318 cur_acl->name = name;
1319 }
1320
Willy Tarreaua9802632008-07-25 19:13:19 +02001321 cur_acl->requires |= acl_expr->kw->requires;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001322 LIST_ADDQ(&cur_acl->expr, &acl_expr->list);
1323 return cur_acl;
1324
1325 out_free_name:
1326 free(name);
1327 out_free_acl_expr:
1328 prune_acl_expr(acl_expr);
1329 free(acl_expr);
1330 out_return:
1331 return NULL;
1332}
1333
Willy Tarreau16fbe822007-06-17 11:54:31 +02001334/* Some useful ACLs provided by default. Only those used are allocated. */
1335
1336const struct {
1337 const char *name;
1338 const char *expr[4]; /* put enough for longest expression */
1339} default_acl_list[] = {
Willy Tarreau58393e12008-07-20 10:39:22 +02001340 { .name = "TRUE", .expr = {"always_true",""}},
1341 { .name = "FALSE", .expr = {"always_false",""}},
Willy Tarreau16fbe822007-06-17 11:54:31 +02001342 { .name = "LOCALHOST", .expr = {"src","127.0.0.1/8",""}},
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001343 { .name = "HTTP", .expr = {"req_proto_http",""}},
Willy Tarreau16fbe822007-06-17 11:54:31 +02001344 { .name = "HTTP_1.0", .expr = {"req_ver","1.0",""}},
1345 { .name = "HTTP_1.1", .expr = {"req_ver","1.1",""}},
1346 { .name = "METH_CONNECT", .expr = {"method","CONNECT",""}},
1347 { .name = "METH_GET", .expr = {"method","GET","HEAD",""}},
1348 { .name = "METH_HEAD", .expr = {"method","HEAD",""}},
1349 { .name = "METH_OPTIONS", .expr = {"method","OPTIONS",""}},
1350 { .name = "METH_POST", .expr = {"method","POST",""}},
1351 { .name = "METH_TRACE", .expr = {"method","TRACE",""}},
1352 { .name = "HTTP_URL_ABS", .expr = {"url_reg","^[^/:]*://",""}},
1353 { .name = "HTTP_URL_SLASH", .expr = {"url_beg","/",""}},
1354 { .name = "HTTP_URL_STAR", .expr = {"url","*",""}},
1355 { .name = "HTTP_CONTENT", .expr = {"hdr_val(content-length)","gt","0",""}},
Emeric Brunbede3d02009-06-30 17:54:00 +02001356 { .name = "RDP_COOKIE", .expr = {"req_rdp_cookie_cnt","gt","0",""}},
Willy Tarreauc6317702008-07-20 09:29:50 +02001357 { .name = "REQ_CONTENT", .expr = {"req_len","gt","0",""}},
Willy Tarreaub6fb4202008-07-20 11:18:28 +02001358 { .name = "WAIT_END", .expr = {"wait_end",""}},
Willy Tarreau16fbe822007-06-17 11:54:31 +02001359 { .name = NULL, .expr = {""}}
1360};
1361
1362/* Find a default ACL from the default_acl list, compile it and return it.
1363 * If the ACL is not found, NULL is returned. In theory, it cannot fail,
1364 * except when default ACLs are broken, in which case it will return NULL.
1365 * If <known_acl> is not NULL, the ACL will be queued at its tail.
1366 */
1367struct acl *find_acl_default(const char *acl_name, struct list *known_acl)
1368{
1369 __label__ out_return, out_free_acl_expr, out_free_name;
1370 struct acl *cur_acl;
1371 struct acl_expr *acl_expr;
1372 char *name;
1373 int index;
1374
1375 for (index = 0; default_acl_list[index].name != NULL; index++) {
1376 if (strcmp(acl_name, default_acl_list[index].name) == 0)
1377 break;
1378 }
1379
1380 if (default_acl_list[index].name == NULL)
1381 return NULL;
1382
1383 acl_expr = parse_acl_expr((const char **)default_acl_list[index].expr);
1384 if (!acl_expr)
1385 goto out_return;
1386
1387 name = strdup(acl_name);
1388 if (!name)
1389 goto out_free_acl_expr;
1390 cur_acl = (struct acl *)calloc(1, sizeof(*cur_acl));
1391 if (cur_acl == NULL)
1392 goto out_free_name;
1393
1394 cur_acl->name = name;
Willy Tarreaua55b7dc2009-07-12 09:21:30 +02001395 cur_acl->requires |= acl_expr->kw->requires;
Willy Tarreau16fbe822007-06-17 11:54:31 +02001396 LIST_INIT(&cur_acl->expr);
1397 LIST_ADDQ(&cur_acl->expr, &acl_expr->list);
1398 if (known_acl)
1399 LIST_ADDQ(known_acl, &cur_acl->list);
1400
1401 return cur_acl;
1402
1403 out_free_name:
1404 free(name);
1405 out_free_acl_expr:
1406 prune_acl_expr(acl_expr);
1407 free(acl_expr);
1408 out_return:
1409 return NULL;
1410}
Willy Tarreaua84d3742007-05-07 00:36:48 +02001411
1412/* Purge everything in the acl_cond <cond>, then return <cond>. */
1413struct acl_cond *prune_acl_cond(struct acl_cond *cond)
1414{
1415 struct acl_term_suite *suite, *tmp_suite;
1416 struct acl_term *term, *tmp_term;
1417
1418 /* iterate through all term suites and free all terms and all suites */
1419 list_for_each_entry_safe(suite, tmp_suite, &cond->suites, list) {
1420 list_for_each_entry_safe(term, tmp_term, &suite->terms, list)
1421 free(term);
1422 free(suite);
1423 }
1424 return cond;
1425}
1426
1427/* Parse an ACL condition starting at <args>[0], relying on a list of already
1428 * known ACLs passed in <known_acl>. The new condition is returned (or NULL in
1429 * case of low memory). Supports multiple conditions separated by "or".
1430 */
1431struct acl_cond *parse_acl_cond(const char **args, struct list *known_acl, int pol)
1432{
1433 __label__ out_return, out_free_suite, out_free_term;
Willy Tarreau74b98a82007-06-16 19:35:18 +02001434 int arg, neg;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001435 const char *word;
1436 struct acl *cur_acl;
1437 struct acl_term *cur_term;
1438 struct acl_term_suite *cur_suite;
1439 struct acl_cond *cond;
1440
1441 cond = (struct acl_cond *)calloc(1, sizeof(*cond));
1442 if (cond == NULL)
1443 goto out_return;
1444
1445 LIST_INIT(&cond->list);
1446 LIST_INIT(&cond->suites);
1447 cond->pol = pol;
1448
1449 cur_suite = NULL;
Willy Tarreau74b98a82007-06-16 19:35:18 +02001450 neg = 0;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001451 for (arg = 0; *args[arg]; arg++) {
1452 word = args[arg];
1453
1454 /* remove as many exclamation marks as we can */
1455 while (*word == '!') {
1456 neg = !neg;
1457 word++;
1458 }
1459
1460 /* an empty word is allowed because we cannot force the user to
1461 * always think about not leaving exclamation marks alone.
1462 */
1463 if (!*word)
1464 continue;
1465
Willy Tarreau16fbe822007-06-17 11:54:31 +02001466 if (strcasecmp(word, "or") == 0 || strcmp(word, "||") == 0) {
Willy Tarreaua84d3742007-05-07 00:36:48 +02001467 /* new term suite */
1468 cur_suite = NULL;
1469 neg = 0;
1470 continue;
1471 }
1472
Willy Tarreau95fa4692010-02-01 13:05:50 +01001473 if (strcmp(word, "{") == 0) {
1474 /* we may have a complete ACL expression between two braces,
1475 * find the last one.
1476 */
1477 int arg_end = arg + 1;
1478 const char **args_new;
1479
1480 while (*args[arg_end] && strcmp(args[arg_end], "}") != 0)
1481 arg_end++;
1482
1483 if (!*args[arg_end])
1484 goto out_free_suite;
1485
1486 args_new = calloc(1, (arg_end - arg + 1) * sizeof(*args_new));
1487 if (!args_new)
1488 goto out_free_suite;
1489
Willy Tarreau2a56c5e2010-03-15 16:13:29 +01001490 args_new[0] = "";
Willy Tarreau95fa4692010-02-01 13:05:50 +01001491 memcpy(args_new + 1, args + arg + 1, (arg_end - arg) * sizeof(*args_new));
1492 args_new[arg_end - arg] = "";
1493 cur_acl = parse_acl(args_new, known_acl);
1494 free(args_new);
1495
1496 if (!cur_acl)
Willy Tarreau16fbe822007-06-17 11:54:31 +02001497 goto out_free_suite;
Willy Tarreau95fa4692010-02-01 13:05:50 +01001498 arg = arg_end;
1499 }
1500 else {
1501 /* search for <word> in the known ACL names. If we do not find
1502 * it, let's look for it in the default ACLs, and if found, add
1503 * it to the list of ACLs of this proxy. This makes it possible
1504 * to override them.
1505 */
1506 cur_acl = find_acl_by_name(word, known_acl);
1507 if (cur_acl == NULL) {
1508 cur_acl = find_acl_default(word, known_acl);
1509 if (cur_acl == NULL)
1510 goto out_free_suite;
1511 }
Willy Tarreau16fbe822007-06-17 11:54:31 +02001512 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001513
1514 cur_term = (struct acl_term *)calloc(1, sizeof(*cur_term));
1515 if (cur_term == NULL)
1516 goto out_free_suite;
1517
1518 cur_term->acl = cur_acl;
1519 cur_term->neg = neg;
Willy Tarreaua9802632008-07-25 19:13:19 +02001520 cond->requires |= cur_acl->requires;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001521
1522 if (!cur_suite) {
1523 cur_suite = (struct acl_term_suite *)calloc(1, sizeof(*cur_suite));
1524 if (cur_term == NULL)
1525 goto out_free_term;
1526 LIST_INIT(&cur_suite->terms);
1527 LIST_ADDQ(&cond->suites, &cur_suite->list);
1528 }
1529 LIST_ADDQ(&cur_suite->terms, &cur_term->list);
Willy Tarreau74b98a82007-06-16 19:35:18 +02001530 neg = 0;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001531 }
1532
1533 return cond;
1534
1535 out_free_term:
1536 free(cur_term);
1537 out_free_suite:
1538 prune_acl_cond(cond);
1539 free(cond);
1540 out_return:
1541 return NULL;
1542}
1543
Willy Tarreau2bbba412010-01-28 16:48:33 +01001544/* Builds an ACL condition starting at the if/unless keyword. The complete
1545 * condition is returned. NULL is returned in case of error or if the first
1546 * word is neither "if" nor "unless". It automatically sets the file name and
1547 * the line number in the condition for better error reporting, and adds the
1548 * ACL requirements to the proxy's acl_requires.
1549 */
1550struct acl_cond *build_acl_cond(const char *file, int line, struct proxy *px, const char **args)
1551{
1552 int pol = ACL_COND_NONE;
1553 struct acl_cond *cond = NULL;
1554
1555 if (!strcmp(*args, "if")) {
1556 pol = ACL_COND_IF;
1557 args++;
1558 }
1559 else if (!strcmp(*args, "unless")) {
1560 pol = ACL_COND_UNLESS;
1561 args++;
1562 }
1563 else
1564 return NULL;
1565
1566 cond = parse_acl_cond(args, &px->acl, pol);
1567 if (!cond)
1568 return NULL;
1569
1570 cond->file = file;
1571 cond->line = line;
1572 px->acl_requires |= cond->requires;
1573
1574 return cond;
1575}
1576
Willy Tarreau11382812008-07-09 16:18:21 +02001577/* Execute condition <cond> and return either ACL_PAT_FAIL, ACL_PAT_MISS or
Willy Tarreaub6866442008-07-14 23:54:42 +02001578 * ACL_PAT_PASS depending on the test results. ACL_PAT_MISS may only be
1579 * returned if <dir> contains ACL_PARTIAL, indicating that incomplete data
1580 * is being examined.
1581 * This function only computes the condition, it does not apply the polarity
1582 * required by IF/UNLESS, it's up to the caller to do this using something like
1583 * this :
Willy Tarreau11382812008-07-09 16:18:21 +02001584 *
1585 * res = acl_pass(res);
Willy Tarreaub6866442008-07-14 23:54:42 +02001586 * if (res == ACL_PAT_MISS)
1587 * return 0;
Willy Tarreau11382812008-07-09 16:18:21 +02001588 * if (cond->pol == ACL_COND_UNLESS)
1589 * res = !res;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001590 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02001591int acl_exec_cond(struct acl_cond *cond, struct proxy *px, struct session *l4, void *l7, int dir)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001592{
1593 __label__ fetch_next;
1594 struct acl_term_suite *suite;
1595 struct acl_term *term;
1596 struct acl_expr *expr;
1597 struct acl *acl;
1598 struct acl_pattern *pattern;
1599 struct acl_test test;
Willy Tarreau11382812008-07-09 16:18:21 +02001600 int acl_res, suite_res, cond_res;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001601
Willy Tarreau11382812008-07-09 16:18:21 +02001602 /* We're doing a logical OR between conditions so we initialize to FAIL.
1603 * The MISS status is propagated down from the suites.
1604 */
Willy Tarreaua84d3742007-05-07 00:36:48 +02001605 cond_res = ACL_PAT_FAIL;
1606 list_for_each_entry(suite, &cond->suites, list) {
Willy Tarreau11382812008-07-09 16:18:21 +02001607 /* Evaluate condition suite <suite>. We stop at the first term
1608 * which returns ACL_PAT_FAIL. The MISS status is still propagated
1609 * in case of uncertainty in the result.
Willy Tarreaua84d3742007-05-07 00:36:48 +02001610 */
1611
1612 /* we're doing a logical AND between terms, so we must set the
1613 * initial value to PASS.
1614 */
1615 suite_res = ACL_PAT_PASS;
1616 list_for_each_entry(term, &suite->terms, list) {
1617 acl = term->acl;
1618
1619 /* FIXME: use cache !
1620 * check acl->cache_idx for this.
1621 */
1622
1623 /* ACL result not cached. Let's scan all the expressions
1624 * and use the first one to match.
1625 */
1626 acl_res = ACL_PAT_FAIL;
1627 list_for_each_entry(expr, &acl->expr, list) {
Willy Tarreaud41f8d82007-06-10 10:06:18 +02001628 /* we need to reset context and flags */
1629 memset(&test, 0, sizeof(test));
Willy Tarreaua84d3742007-05-07 00:36:48 +02001630 fetch_next:
Willy Tarreaub6866442008-07-14 23:54:42 +02001631 if (!expr->kw->fetch(px, l4, l7, dir, expr, &test)) {
1632 /* maybe we could not fetch because of missing data */
1633 if (test.flags & ACL_TEST_F_MAY_CHANGE && dir & ACL_PARTIAL)
1634 acl_res |= ACL_PAT_MISS;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001635 continue;
Willy Tarreaub6866442008-07-14 23:54:42 +02001636 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001637
Willy Tarreaua79534f2008-07-20 10:13:37 +02001638 if (test.flags & ACL_TEST_F_RES_SET) {
1639 if (test.flags & ACL_TEST_F_RES_PASS)
1640 acl_res |= ACL_PAT_PASS;
1641 else
1642 acl_res |= ACL_PAT_FAIL;
1643 }
1644 else {
Willy Tarreau020534d2010-05-16 21:45:45 +02001645 if (!eb_is_empty(&expr->pattern_tree)) {
Willy Tarreauc4262962010-05-10 23:42:40 +02001646 /* a tree is present, let's check what type it is */
1647 if (expr->kw->match == acl_match_str)
1648 acl_res |= acl_lookup_str(&test, expr) ? ACL_PAT_PASS : ACL_PAT_FAIL;
Willy Tarreaub337b532010-05-13 20:03:41 +02001649 else if (expr->kw->match == acl_match_ip)
1650 acl_res |= acl_lookup_ip(&test, expr) ? ACL_PAT_PASS : ACL_PAT_FAIL;
Willy Tarreauc4262962010-05-10 23:42:40 +02001651 }
1652
Willy Tarreaua79534f2008-07-20 10:13:37 +02001653 /* call the match() function for all tests on this value */
1654 list_for_each_entry(pattern, &expr->patterns, list) {
Willy Tarreaua79534f2008-07-20 10:13:37 +02001655 if (acl_res == ACL_PAT_PASS)
1656 break;
Willy Tarreaue56cda92010-05-11 23:25:05 +02001657 acl_res |= expr->kw->match(&test, pattern);
Willy Tarreaua79534f2008-07-20 10:13:37 +02001658 }
Krzysztof Piotr Oledzkid7528e52010-01-29 17:55:53 +01001659
Willy Tarreaue56cda92010-05-11 23:25:05 +02001660 if ((test.flags & ACL_TEST_F_NULL_MATCH) &&
Willy Tarreau020534d2010-05-16 21:45:45 +02001661 LIST_ISEMPTY(&expr->patterns) && eb_is_empty(&expr->pattern_tree))
Krzysztof Piotr Oledzkid7528e52010-01-29 17:55:53 +01001662 acl_res |= expr->kw->match(&test, NULL);
Willy Tarreaua84d3742007-05-07 00:36:48 +02001663 }
1664 /*
Willy Tarreau11382812008-07-09 16:18:21 +02001665 * OK now acl_res holds the result of this expression
1666 * as one of ACL_PAT_FAIL, ACL_PAT_MISS or ACL_PAT_PASS.
Willy Tarreaua84d3742007-05-07 00:36:48 +02001667 *
Willy Tarreau11382812008-07-09 16:18:21 +02001668 * Then if (!MISS) we can cache the result, and put
Willy Tarreaua84d3742007-05-07 00:36:48 +02001669 * (test.flags & ACL_TEST_F_VOLATILE) in the cache flags.
1670 *
1671 * FIXME: implement cache.
1672 *
1673 */
1674
1675 /* now we may have some cleanup to do */
1676 if (test.flags & ACL_TEST_F_MUST_FREE) {
1677 free(test.ptr);
1678 test.len = 0;
1679 }
1680
Willy Tarreau11382812008-07-09 16:18:21 +02001681 /* we're ORing these terms, so a single PASS is enough */
1682 if (acl_res == ACL_PAT_PASS)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001683 break;
1684
Willy Tarreaua84d3742007-05-07 00:36:48 +02001685 if (test.flags & ACL_TEST_F_FETCH_MORE)
1686 goto fetch_next;
Willy Tarreaub6866442008-07-14 23:54:42 +02001687
1688 /* sometimes we know the fetched data is subject to change
1689 * later and give another chance for a new match (eg: request
1690 * size, time, ...)
1691 */
1692 if (test.flags & ACL_TEST_F_MAY_CHANGE && dir & ACL_PARTIAL)
1693 acl_res |= ACL_PAT_MISS;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001694 }
1695 /*
1696 * Here we have the result of an ACL (cached or not).
1697 * ACLs are combined, negated or not, to form conditions.
1698 */
1699
Willy Tarreaua84d3742007-05-07 00:36:48 +02001700 if (term->neg)
Willy Tarreau11382812008-07-09 16:18:21 +02001701 acl_res = acl_neg(acl_res);
Willy Tarreaua84d3742007-05-07 00:36:48 +02001702
1703 suite_res &= acl_res;
Willy Tarreau11382812008-07-09 16:18:21 +02001704
1705 /* we're ANDing these terms, so a single FAIL is enough */
1706 if (suite_res == ACL_PAT_FAIL)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001707 break;
1708 }
1709 cond_res |= suite_res;
Willy Tarreau11382812008-07-09 16:18:21 +02001710
1711 /* we're ORing these terms, so a single PASS is enough */
1712 if (cond_res == ACL_PAT_PASS)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001713 break;
1714 }
Willy Tarreau11382812008-07-09 16:18:21 +02001715 return cond_res;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001716}
1717
1718
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001719/* Reports a pointer to the first ACL used in condition <cond> which requires
1720 * at least one of the USE_FLAGS in <require>. Returns NULL if none matches.
1721 * The construct is almost the same as for acl_exec_cond() since we're walking
1722 * down the ACL tree as well. It is important that the tree is really walked
1723 * through and never cached, because that way, this function can be used as a
1724 * late check.
1725 */
Willy Tarreauf1e98b82010-01-28 17:59:39 +01001726struct acl *cond_find_require(const struct acl_cond *cond, unsigned int require)
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001727{
1728 struct acl_term_suite *suite;
1729 struct acl_term *term;
1730 struct acl *acl;
1731
1732 list_for_each_entry(suite, &cond->suites, list) {
1733 list_for_each_entry(term, &suite->terms, list) {
1734 acl = term->acl;
1735 if (acl->requires & require)
1736 return acl;
1737 }
1738 }
1739 return NULL;
1740}
1741
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001742/*
1743 * Find targets for userlist and groups in acl. Function returns the number
1744 * of errors or OK if everything is fine.
1745 */
1746int
1747acl_find_targets(struct proxy *p)
1748{
1749
1750 struct acl *acl;
1751 struct acl_expr *expr;
1752 struct acl_pattern *pattern;
1753 struct userlist *ul;
1754 int cfgerr = 0;
1755
1756 list_for_each_entry(acl, &p->acl, list) {
1757 list_for_each_entry(expr, &acl->expr, list) {
Hervé COMMOWICKdaa824e2011-08-05 12:09:44 +02001758 if (strcmp(expr->kw->kw, "srv_is_up") == 0 ||
1759 strcmp(expr->kw->kw, "srv_conn") == 0) {
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001760 struct proxy *px;
1761 struct server *srv;
1762 char *pname, *sname;
1763
1764 if (!expr->arg.str || !*expr->arg.str) {
1765 Alert("proxy %s: acl %s %s(): missing server name.\n",
1766 p->id, acl->name, expr->kw->kw);
1767 cfgerr++;
1768 continue;
1769 }
1770
1771 pname = expr->arg.str;
1772 sname = strrchr(pname, '/');
1773
1774 if (sname)
1775 *sname++ = '\0';
1776 else {
1777 sname = pname;
1778 pname = NULL;
1779 }
1780
1781 px = p;
1782 if (pname) {
1783 px = findproxy(pname, PR_CAP_BE);
1784 if (!px) {
1785 Alert("proxy %s: acl %s %s(): unable to find proxy '%s'.\n",
1786 p->id, acl->name, expr->kw->kw, pname);
1787 cfgerr++;
1788 continue;
1789 }
1790 }
1791
1792 srv = findserver(px, sname);
1793 if (!srv) {
1794 Alert("proxy %s: acl %s %s(): unable to find server '%s'.\n",
1795 p->id, acl->name, expr->kw->kw, sname);
1796 cfgerr++;
1797 continue;
1798 }
1799
1800 free(expr->arg.str);
1801 expr->arg_len = 0;
1802 expr->arg.srv = srv;
1803 continue;
1804 }
1805
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001806 if (strstr(expr->kw->kw, "http_auth") == expr->kw->kw) {
1807
1808 if (!expr->arg.str || !*expr->arg.str) {
1809 Alert("proxy %s: acl %s %s(): missing userlist name.\n",
1810 p->id, acl->name, expr->kw->kw);
1811 cfgerr++;
1812 continue;
1813 }
1814
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01001815 if (p->uri_auth && p->uri_auth->userlist &&
1816 !strcmp(p->uri_auth->userlist->name, expr->arg.str))
1817 ul = p->uri_auth->userlist;
1818 else
1819 ul = auth_find_userlist(expr->arg.str);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001820
1821 if (!ul) {
1822 Alert("proxy %s: acl %s %s(%s): unable to find userlist.\n",
1823 p->id, acl->name, expr->kw->kw, expr->arg.str);
1824 cfgerr++;
1825 continue;
1826 }
1827
1828 expr->arg_len = 0;
1829 expr->arg.ul = ul;
1830 }
1831
1832
1833 if (!strcmp(expr->kw->kw, "http_auth_group")) {
1834
1835 if (LIST_ISEMPTY(&expr->patterns)) {
1836 Alert("proxy %s: acl %s %s(): no groups specified.\n",
1837 p->id, acl->name, expr->kw->kw);
1838 cfgerr++;
1839 continue;
1840 }
1841
1842 list_for_each_entry(pattern, &expr->patterns, list) {
1843 pattern->val.group_mask = auth_resolve_groups(expr->arg.ul, pattern->ptr.str);
1844
1845 free(pattern->ptr.str);
1846 pattern->ptr.str = NULL;
1847 pattern->len = 0;
1848
1849 if (!pattern->val.group_mask) {
1850 Alert("proxy %s: acl %s %s(): invalid group(s).\n",
1851 p->id, acl->name, expr->kw->kw);
1852 cfgerr++;
1853 continue;
1854 }
1855 }
1856 }
1857 }
1858 }
1859
1860 return cfgerr;
1861}
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001862
Willy Tarreaua84d3742007-05-07 00:36:48 +02001863/************************************************************************/
1864/* All supported keywords must be declared here. */
1865/************************************************************************/
1866
1867/* Note: must not be declared <const> as its list will be overwritten */
1868static struct acl_kw_list acl_kws = {{ },{
Emeric Brun38e71762010-09-23 17:59:18 +02001869 { "always_true", acl_parse_nothing, acl_fetch_true, acl_match_nothing, ACL_USE_NOTHING },
1870 { "always_false", acl_parse_nothing, acl_fetch_false, acl_match_nothing, ACL_USE_NOTHING },
1871 { "wait_end", acl_parse_nothing, acl_fetch_wait_end, acl_match_nothing, ACL_USE_NOTHING },
1872 { "req_len", acl_parse_int, acl_fetch_req_len, acl_match_int, ACL_USE_L6REQ_VOLATILE },
1873 { "req_ssl_hello_type", acl_parse_int, acl_fetch_ssl_hello_type, acl_match_int, ACL_USE_L6REQ_VOLATILE },
1874 { "rep_ssl_hello_type", acl_parse_int, acl_fetch_ssl_hello_type, acl_match_int, ACL_USE_L6RTR_VOLATILE },
1875 { "req_ssl_ver", acl_parse_dotted_ver, acl_fetch_req_ssl_ver, acl_match_int, ACL_USE_L6REQ_VOLATILE },
1876 { "req_rdp_cookie", acl_parse_str, acl_fetch_rdp_cookie, acl_match_str, ACL_USE_L6REQ_VOLATILE|ACL_MAY_LOOKUP },
1877 { "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 +02001878#if 0
1879 { "time", acl_parse_time, acl_fetch_time, acl_match_time },
1880#endif
1881 { NULL, NULL, NULL, NULL }
1882}};
1883
1884
1885__attribute__((constructor))
1886static void __acl_init(void)
1887{
1888 acl_register_keywords(&acl_kws);
1889}
1890
1891
1892/*
1893 * Local variables:
1894 * c-indent-level: 8
1895 * c-basic-offset: 8
1896 * End:
1897 */