blob: aab7f41bafe8304f7c01c26b41bdc9591ad8bd2c [file] [log] [blame]
Willy Tarreaua84d3742007-05-07 00:36:48 +02001/*
2 * ACL management functions.
3 *
Willy Tarreau0e698542011-09-16 08:32:32 +02004 * Copyright 2000-2011 Willy Tarreau <w@1wt.eu>
Willy Tarreaua84d3742007-05-07 00:36:48 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
Willy Tarreauae8b7962007-06-09 23:10:04 +020013#include <ctype.h>
Willy Tarreaua84d3742007-05-07 00:36:48 +020014#include <stdio.h>
15#include <string.h>
16
17#include <common/config.h>
18#include <common/mini-clist.h>
19#include <common/standard.h>
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +010020#include <common/uri_auth.h>
Willy Tarreaua84d3742007-05-07 00:36:48 +020021
Willy Tarreau2b5285d2010-05-09 23:45:24 +020022#include <types/global.h>
23
Willy Tarreaua84d3742007-05-07 00:36:48 +020024#include <proto/acl.h>
Willy Tarreau34db1082012-04-19 17:16:54 +020025#include <proto/arg.h>
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010026#include <proto/auth.h>
Willy Tarreau44b90cc2010-05-24 20:27:29 +020027#include <proto/buffers.h>
Willy Tarreau404e8ab2009-07-26 19:40:40 +020028#include <proto/log.h>
Willy Tarreau0b1cd942010-05-16 22:18:27 +020029#include <proto/proxy.h>
Willy Tarreaua84d3742007-05-07 00:36:48 +020030
Willy Tarreauc4262962010-05-10 23:42:40 +020031#include <ebsttree.h>
32
Willy Tarreaua9802632008-07-25 19:13:19 +020033/* The capabilities of filtering hooks describe the type of information
34 * available to each of them.
35 */
36const unsigned int filt_cap[] = {
37 [ACL_HOOK_REQ_FE_TCP] = ACL_USE_TCP4_ANY|ACL_USE_TCP6_ANY|ACL_USE_TCP_ANY,
Willy Tarreau06457872010-05-23 12:24:38 +020038 [ACL_HOOK_REQ_FE_TCP_CONTENT] = ACL_USE_TCP4_ANY|ACL_USE_TCP6_ANY|ACL_USE_TCP_ANY|ACL_USE_L6REQ_ANY,
39 [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,
40 [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,
41 [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,
42 [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,
43 [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,
44 [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,
45 [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 +020046
Willy Tarreau06457872010-05-23 12:24:38 +020047 [ACL_HOOK_RTR_BE_TCP_CONTENT] = ACL_USE_REQ_PERMANENT|ACL_USE_REQ_CACHEABLE|ACL_USE_L6RTR_ANY,
48 [ACL_HOOK_RTR_BE_HTTP_IN] = ACL_USE_REQ_PERMANENT|ACL_USE_REQ_CACHEABLE|ACL_USE_L6RTR_ANY|ACL_USE_L7RTR_ANY,
49 [ACL_HOOK_RTR_FE_TCP_CONTENT] = ACL_USE_REQ_PERMANENT|ACL_USE_REQ_CACHEABLE|ACL_USE_L6RTR_ANY|ACL_USE_L7RTR_ANY,
50 [ACL_HOOK_RTR_FE_HTTP_IN] = ACL_USE_REQ_PERMANENT|ACL_USE_REQ_CACHEABLE|ACL_USE_L6RTR_ANY|ACL_USE_L7RTR_ANY,
51 [ACL_HOOK_RTR_BE_HTTP_OUT] = ACL_USE_REQ_PERMANENT|ACL_USE_REQ_CACHEABLE|ACL_USE_L6RTR_ANY|ACL_USE_L7RTR_ANY,
52 [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 +020053};
54
Willy Tarreaua84d3742007-05-07 00:36:48 +020055/* List head of all known ACL keywords */
56static struct acl_kw_list acl_keywords = {
57 .list = LIST_HEAD_INIT(acl_keywords.list)
58};
59
60
Willy Tarreaua5909832007-06-17 20:40:25 +020061/*
62 * These functions are only used for debugging complex configurations.
Willy Tarreaua84d3742007-05-07 00:36:48 +020063 */
Willy Tarreaua5909832007-06-17 20:40:25 +020064
Willy Tarreau58393e12008-07-20 10:39:22 +020065/* force TRUE to be returned at the fetch level */
Willy Tarreaua5909832007-06-17 20:40:25 +020066static int
Willy Tarreau58393e12008-07-20 10:39:22 +020067acl_fetch_true(struct proxy *px, struct session *l4, void *l7, int dir,
68 struct acl_expr *expr, struct acl_test *test)
Willy Tarreaua5909832007-06-17 20:40:25 +020069{
Willy Tarreau58393e12008-07-20 10:39:22 +020070 test->flags |= ACL_TEST_F_SET_RES_PASS;
Willy Tarreaua5909832007-06-17 20:40:25 +020071 return 1;
72}
73
Willy Tarreaub6fb4202008-07-20 11:18:28 +020074/* wait for more data as long as possible, then return TRUE. This should be
75 * used with content inspection.
76 */
77static int
78acl_fetch_wait_end(struct proxy *px, struct session *l4, void *l7, int dir,
79 struct acl_expr *expr, struct acl_test *test)
80{
81 if (dir & ACL_PARTIAL) {
82 test->flags |= ACL_TEST_F_MAY_CHANGE;
83 return 0;
84 }
85 test->flags |= ACL_TEST_F_SET_RES_PASS;
86 return 1;
87}
88
Willy Tarreau58393e12008-07-20 10:39:22 +020089/* force FALSE to be returned at the fetch level */
Willy Tarreaua5909832007-06-17 20:40:25 +020090static int
Willy Tarreau58393e12008-07-20 10:39:22 +020091acl_fetch_false(struct proxy *px, struct session *l4, void *l7, int dir,
92 struct acl_expr *expr, struct acl_test *test)
Willy Tarreaua84d3742007-05-07 00:36:48 +020093{
Willy Tarreau58393e12008-07-20 10:39:22 +020094 test->flags |= ACL_TEST_F_SET_RES_FAIL;
Willy Tarreaua84d3742007-05-07 00:36:48 +020095 return 1;
96}
97
Willy Tarreau44b90cc2010-05-24 20:27:29 +020098/* return the number of bytes in the request buffer */
99static int
100acl_fetch_req_len(struct proxy *px, struct session *l4, void *l7, int dir,
101 struct acl_expr *expr, struct acl_test *test)
102{
103 if (!l4 || !l4->req)
104 return 0;
105
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100106 temp_pattern.data.integer = l4->req->i;
Willy Tarreau44b90cc2010-05-24 20:27:29 +0200107 test->flags = ACL_TEST_F_VOLATILE | ACL_TEST_F_MAY_CHANGE;
108 return 1;
109}
110
Emeric Brun38e71762010-09-23 17:59:18 +0200111
112static int
113acl_fetch_ssl_hello_type(struct proxy *px, struct session *l4, void *l7, int dir,
114 struct acl_expr *expr, struct acl_test *test)
115{
116 int hs_len;
117 int hs_type, bleft;
118 struct buffer *b;
119 const unsigned char *data;
120
121 if (!l4)
122 goto not_ssl_hello;
123
124 b = ((dir & ACL_DIR_MASK) == ACL_DIR_RTR) ? l4->rep : l4->req;
125
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100126 bleft = b->i;
Willy Tarreau89fa7062012-03-02 16:13:16 +0100127 data = (const unsigned char *)b->p;
Emeric Brun38e71762010-09-23 17:59:18 +0200128
129 if (!bleft)
130 goto too_short;
131
132 if ((*data >= 0x14 && *data <= 0x17) || (*data == 0xFF)) {
133 /* SSLv3 header format */
134 if (bleft < 9)
135 goto too_short;
136
137 /* ssl version 3 */
138 if ((data[1] << 16) + data[2] < 0x00030000)
139 goto not_ssl_hello;
140
141 /* ssl message len must present handshake type and len */
142 if ((data[3] << 8) + data[4] < 4)
143 goto not_ssl_hello;
144
145 /* format introduced with SSLv3 */
146
147 hs_type = (int)data[5];
148 hs_len = ( data[6] << 16 ) + ( data[7] << 8 ) + data[8];
149
150 /* not a full handshake */
151 if (bleft < (9 + hs_len))
152 goto too_short;
153
154 }
155 else {
156 goto not_ssl_hello;
157 }
158
Willy Tarreaua5e37562011-12-16 17:06:15 +0100159 temp_pattern.data.integer = hs_type;
Emeric Brun38e71762010-09-23 17:59:18 +0200160 test->flags = ACL_TEST_F_VOLATILE;
161
162 return 1;
163
164 too_short:
165 test->flags = ACL_TEST_F_MAY_CHANGE;
166
167 not_ssl_hello:
168
169 return 0;
170}
171
Willy Tarreau44b90cc2010-05-24 20:27:29 +0200172/* Return the version of the SSL protocol in the request. It supports both
173 * SSLv3 (TLSv1) header format for any message, and SSLv2 header format for
174 * the hello message. The SSLv3 format is described in RFC 2246 p49, and the
175 * SSLv2 format is described here, and completed p67 of RFC 2246 :
176 * http://wp.netscape.com/eng/security/SSL_2.html
177 *
178 * Note: this decoder only works with non-wrapping data.
179 */
180static int
181acl_fetch_req_ssl_ver(struct proxy *px, struct session *l4, void *l7, int dir,
182 struct acl_expr *expr, struct acl_test *test)
183{
184 int version, bleft, msg_len;
185 const unsigned char *data;
186
187 if (!l4 || !l4->req)
188 return 0;
189
190 msg_len = 0;
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100191 bleft = l4->req->i;
Willy Tarreau44b90cc2010-05-24 20:27:29 +0200192 if (!bleft)
193 goto too_short;
194
Willy Tarreau89fa7062012-03-02 16:13:16 +0100195 data = (const unsigned char *)l4->req->p;
Willy Tarreau44b90cc2010-05-24 20:27:29 +0200196 if ((*data >= 0x14 && *data <= 0x17) || (*data == 0xFF)) {
197 /* SSLv3 header format */
198 if (bleft < 5)
199 goto too_short;
200
201 version = (data[1] << 16) + data[2]; /* version: major, minor */
202 msg_len = (data[3] << 8) + data[4]; /* record length */
203
204 /* format introduced with SSLv3 */
205 if (version < 0x00030000)
206 goto not_ssl;
207
208 /* message length between 1 and 2^14 + 2048 */
209 if (msg_len < 1 || msg_len > ((1<<14) + 2048))
210 goto not_ssl;
211
212 bleft -= 5; data += 5;
213 } else {
214 /* SSLv2 header format, only supported for hello (msg type 1) */
215 int rlen, plen, cilen, silen, chlen;
216
217 if (*data & 0x80) {
218 if (bleft < 3)
219 goto too_short;
220 /* short header format : 15 bits for length */
221 rlen = ((data[0] & 0x7F) << 8) | data[1];
222 plen = 0;
223 bleft -= 2; data += 2;
224 } else {
225 if (bleft < 4)
226 goto too_short;
227 /* long header format : 14 bits for length + pad length */
228 rlen = ((data[0] & 0x3F) << 8) | data[1];
229 plen = data[2];
230 bleft -= 3; data += 2;
231 }
232
233 if (*data != 0x01)
234 goto not_ssl;
235 bleft--; data++;
236
237 if (bleft < 8)
238 goto too_short;
239 version = (data[0] << 16) + data[1]; /* version: major, minor */
240 cilen = (data[2] << 8) + data[3]; /* cipher len, multiple of 3 */
241 silen = (data[4] << 8) + data[5]; /* session_id_len: 0 or 16 */
242 chlen = (data[6] << 8) + data[7]; /* 16<=challenge length<=32 */
243
244 bleft -= 8; data += 8;
245 if (cilen % 3 != 0)
246 goto not_ssl;
247 if (silen && silen != 16)
248 goto not_ssl;
249 if (chlen < 16 || chlen > 32)
250 goto not_ssl;
251 if (rlen != 9 + cilen + silen + chlen)
252 goto not_ssl;
253
254 /* focus on the remaining data length */
255 msg_len = cilen + silen + chlen + plen;
256 }
257 /* We could recursively check that the buffer ends exactly on an SSL
258 * fragment boundary and that a possible next segment is still SSL,
259 * but that's a bit pointless. However, we could still check that
260 * all the part of the request which fits in a buffer is already
261 * there.
262 */
Willy Tarreau89fa7062012-03-02 16:13:16 +0100263 if (msg_len > buffer_max_len(l4->req) + l4->req->data - l4->req->p)
264 msg_len = buffer_max_len(l4->req) + l4->req->data - l4->req->p;
Willy Tarreau44b90cc2010-05-24 20:27:29 +0200265
266 if (bleft < msg_len)
267 goto too_short;
268
269 /* OK that's enough. We have at least the whole message, and we have
270 * the protocol version.
271 */
Willy Tarreaua5e37562011-12-16 17:06:15 +0100272 temp_pattern.data.integer = version;
Willy Tarreau44b90cc2010-05-24 20:27:29 +0200273 test->flags = ACL_TEST_F_VOLATILE;
274 return 1;
275
276 too_short:
277 test->flags = ACL_TEST_F_MAY_CHANGE;
278 not_ssl:
279 return 0;
280}
281
Willy Tarreaub6672b52011-12-12 17:23:41 +0100282/* Try to extract the Server Name Indication that may be presented in a TLS
283 * client hello handshake message. The format of the message is the following
284 * (cf RFC5246 + RFC6066) :
285 * TLS frame :
286 * - uint8 type = 0x16 (Handshake)
287 * - uint16 version >= 0x0301 (TLSv1)
288 * - uint16 length (frame length)
289 * - TLS handshake :
290 * - uint8 msg_type = 0x01 (ClientHello)
291 * - uint24 length (handshake message length)
292 * - ClientHello :
293 * - uint16 client_version >= 0x0301 (TLSv1)
Willy Tarreaud017f112012-04-09 09:24:11 +0200294 * - uint8 Random[32] (4 first ones are timestamp)
Willy Tarreaub6672b52011-12-12 17:23:41 +0100295 * - SessionID :
296 * - uint8 session_id_len (0..32) (SessionID len in bytes)
297 * - uint8 session_id[session_id_len]
298 * - CipherSuite :
299 * - uint16 cipher_len >= 2 (Cipher length in bytes)
300 * - uint16 ciphers[cipher_len/2]
301 * - CompressionMethod :
302 * - uint8 compression_len >= 1 (# of supported methods)
303 * - uint8 compression_methods[compression_len]
304 * - optional client_extension_len (in bytes)
305 * - optional sequence of ClientHelloExtensions (as many bytes as above):
306 * - uint16 extension_type = 0 for server_name
307 * - uint16 extension_len
308 * - opaque extension_data[extension_len]
309 * - uint16 server_name_list_len (# of bytes here)
310 * - opaque server_names[server_name_list_len bytes]
311 * - uint8 name_type = 0 for host_name
312 * - uint16 name_len
313 * - opaque hostname[name_len bytes]
314 */
315static int
316acl_fetch_ssl_hello_sni(struct proxy *px, struct session *l4, void *l7, int dir,
317 struct acl_expr *expr, struct acl_test *test)
318{
319 int hs_len, ext_len, bleft;
320 struct buffer *b;
321 unsigned char *data;
322
323 if (!l4)
324 goto not_ssl_hello;
325
326 b = ((dir & ACL_DIR_MASK) == ACL_DIR_RTR) ? l4->rep : l4->req;
327
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100328 bleft = b->i;
Willy Tarreau89fa7062012-03-02 16:13:16 +0100329 data = (unsigned char *)b->p;
Willy Tarreaub6672b52011-12-12 17:23:41 +0100330
331 /* Check for SSL/TLS Handshake */
332 if (!bleft)
333 goto too_short;
334 if (*data != 0x16)
335 goto not_ssl_hello;
336
337 /* Check for TLSv1 or later (SSL version >= 3.1) */
338 if (bleft < 3)
339 goto too_short;
340 if (data[1] < 0x03 || data[2] < 0x01)
341 goto not_ssl_hello;
342
343 if (bleft < 5)
344 goto too_short;
345 hs_len = (data[3] << 8) + data[4];
346 if (hs_len < 1 + 3 + 2 + 32 + 1 + 2 + 2 + 1 + 1 + 2 + 2)
347 goto not_ssl_hello; /* too short to have an extension */
348
349 data += 5; /* enter TLS handshake */
350 bleft -= 5;
351
352 /* Check for a complete client hello starting at <data> */
353 if (bleft < 1)
354 goto too_short;
355 if (data[0] != 0x01) /* msg_type = Client Hello */
356 goto not_ssl_hello;
357
358 /* Check the Hello's length */
359 if (bleft < 4)
360 goto too_short;
361 hs_len = (data[1] << 16) + (data[2] << 8) + data[3];
362 if (hs_len < 2 + 32 + 1 + 2 + 2 + 1 + 1 + 2 + 2)
363 goto not_ssl_hello; /* too short to have an extension */
364
365 /* We want the full handshake here */
366 if (bleft < hs_len)
367 goto too_short;
368
369 data += 4;
370 /* Start of the ClientHello message */
371 if (data[0] < 0x03 || data[1] < 0x01) /* TLSv1 minimum */
372 goto not_ssl_hello;
373
Willy Tarreaud017f112012-04-09 09:24:11 +0200374 ext_len = data[34]; /* session_id_len */
Willy Tarreaub6672b52011-12-12 17:23:41 +0100375 if (ext_len > 32 || ext_len > (hs_len - 35)) /* check for correct session_id len */
376 goto not_ssl_hello;
377
378 /* Jump to cipher suite */
379 hs_len -= 35 + ext_len;
380 data += 35 + ext_len;
381
382 if (hs_len < 4 || /* minimum one cipher */
383 (ext_len = (data[0] << 8) + data[1]) < 2 || /* minimum 2 bytes for a cipher */
384 ext_len > hs_len)
385 goto not_ssl_hello;
386
387 /* Jump to the compression methods */
388 hs_len -= 2 + ext_len;
389 data += 2 + ext_len;
390
391 if (hs_len < 2 || /* minimum one compression method */
392 data[0] < 1 || data[0] > hs_len) /* minimum 1 bytes for a method */
393 goto not_ssl_hello;
394
395 /* Jump to the extensions */
396 hs_len -= 1 + data[0];
397 data += 1 + data[0];
398
399 if (hs_len < 2 || /* minimum one extension list length */
400 (ext_len = (data[0] << 8) + data[1]) > hs_len - 2) /* list too long */
401 goto not_ssl_hello;
402
403 hs_len = ext_len; /* limit ourselves to the extension length */
404 data += 2;
405
406 while (hs_len >= 4) {
407 int ext_type, name_type, srv_len, name_len;
408
409 ext_type = (data[0] << 8) + data[1];
410 ext_len = (data[2] << 8) + data[3];
411
412 if (ext_len > hs_len - 4) /* Extension too long */
413 goto not_ssl_hello;
414
415 if (ext_type == 0) { /* Server name */
416 if (ext_len < 2) /* need one list length */
417 goto not_ssl_hello;
418
419 srv_len = (data[4] << 8) + data[5];
420 if (srv_len < 4 || srv_len > hs_len - 6)
421 goto not_ssl_hello; /* at least 4 bytes per server name */
422
423 name_type = data[6];
424 name_len = (data[7] << 8) + data[8];
425
426 if (name_type == 0) { /* hostname */
Willy Tarreau62e7c712012-03-10 09:05:30 +0100427 temp_pattern.data.str.str = (char *)data + 9;
Willy Tarreau664092c2011-12-16 19:11:42 +0100428 temp_pattern.data.str.len = name_len;
Willy Tarreaub6672b52011-12-12 17:23:41 +0100429 test->flags = ACL_TEST_F_VOLATILE;
Willy Tarreaub6672b52011-12-12 17:23:41 +0100430 return 1;
431 }
432 }
433
434 hs_len -= 4 + ext_len;
435 data += 4 + ext_len;
436 }
437 /* server name not found */
438 goto not_ssl_hello;
439
440 too_short:
441 test->flags = ACL_TEST_F_MAY_CHANGE;
442
443 not_ssl_hello:
444
445 return 0;
446}
447
Willy Tarreau44b90cc2010-05-24 20:27:29 +0200448/* Fetch the RDP cookie identified in the expression.
449 * Note: this decoder only works with non-wrapping data.
Willy Tarreau34db1082012-04-19 17:16:54 +0200450 * Accepts either 0 or 1 argument. Argument is a string (cookie name), other
451 * types will lead to undefined behaviour.
Willy Tarreau44b90cc2010-05-24 20:27:29 +0200452 */
453int
454acl_fetch_rdp_cookie(struct proxy *px, struct session *l4, void *l7, int dir,
455 struct acl_expr *expr, struct acl_test *test)
456{
457 int bleft;
458 const unsigned char *data;
459
460 if (!l4 || !l4->req)
461 return 0;
462
463 test->flags = 0;
464
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100465 bleft = l4->req->i;
Willy Tarreau44b90cc2010-05-24 20:27:29 +0200466 if (bleft <= 11)
467 goto too_short;
468
Willy Tarreau89fa7062012-03-02 16:13:16 +0100469 data = (const unsigned char *)l4->req->p + 11;
Willy Tarreau44b90cc2010-05-24 20:27:29 +0200470 bleft -= 11;
471
472 if (bleft <= 7)
473 goto too_short;
474
475 if (strncasecmp((const char *)data, "Cookie:", 7) != 0)
476 goto not_cookie;
477
478 data += 7;
479 bleft -= 7;
480
481 while (bleft > 0 && *data == ' ') {
482 data++;
483 bleft--;
484 }
485
Willy Tarreau34db1082012-04-19 17:16:54 +0200486 if (expr->args) {
Willy Tarreau44b90cc2010-05-24 20:27:29 +0200487
Willy Tarreau34db1082012-04-19 17:16:54 +0200488 if (bleft <= expr->args->data.str.len)
Willy Tarreau44b90cc2010-05-24 20:27:29 +0200489 goto too_short;
490
Willy Tarreau34db1082012-04-19 17:16:54 +0200491 if ((data[expr->args->data.str.len] != '=') ||
492 strncasecmp(expr->args->data.str.str, (const char *)data, expr->args->data.str.len) != 0)
Willy Tarreau44b90cc2010-05-24 20:27:29 +0200493 goto not_cookie;
494
Willy Tarreau34db1082012-04-19 17:16:54 +0200495 data += expr->args->data.str.len + 1;
496 bleft -= expr->args->data.str.len + 1;
Willy Tarreau44b90cc2010-05-24 20:27:29 +0200497 } else {
498 while (bleft > 0 && *data != '=') {
499 if (*data == '\r' || *data == '\n')
500 goto not_cookie;
501 data++;
502 bleft--;
503 }
504
505 if (bleft < 1)
506 goto too_short;
507
508 if (*data != '=')
509 goto not_cookie;
510
511 data++;
512 bleft--;
513 }
514
515 /* data points to cookie value */
Willy Tarreau664092c2011-12-16 19:11:42 +0100516 temp_pattern.data.str.str = (char *)data;
517 temp_pattern.data.str.len = 0;
Willy Tarreau44b90cc2010-05-24 20:27:29 +0200518
519 while (bleft > 0 && *data != '\r') {
520 data++;
521 bleft--;
522 }
523
524 if (bleft < 2)
525 goto too_short;
526
527 if (data[0] != '\r' || data[1] != '\n')
528 goto not_cookie;
529
Willy Tarreau664092c2011-12-16 19:11:42 +0100530 temp_pattern.data.str.len = (char *)data - temp_pattern.data.str.str;
Willy Tarreau44b90cc2010-05-24 20:27:29 +0200531 test->flags = ACL_TEST_F_VOLATILE;
532 return 1;
533
534 too_short:
535 test->flags = ACL_TEST_F_MAY_CHANGE;
536 not_cookie:
537 return 0;
538}
539
540static int
541acl_fetch_rdp_cookie_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
542 struct acl_expr *expr, struct acl_test *test)
543{
544 int ret;
545
546 ret = acl_fetch_rdp_cookie(px, l4, l7, dir, expr, test);
547
Willy Tarreau664092c2011-12-16 19:11:42 +0100548 temp_pattern.data.str.str = NULL;
549 temp_pattern.data.str.len = 0;
Willy Tarreau44b90cc2010-05-24 20:27:29 +0200550
551 if (test->flags & ACL_TEST_F_MAY_CHANGE)
552 return 0;
553
554 test->flags = ACL_TEST_F_VOLATILE;
Willy Tarreaua5e37562011-12-16 17:06:15 +0100555 temp_pattern.data.integer = ret;
Willy Tarreau44b90cc2010-05-24 20:27:29 +0200556
557 return 1;
558}
559
Willy Tarreau58393e12008-07-20 10:39:22 +0200560
561/*
562 * These functions are exported and may be used by any other component.
563 */
564
565/* ignore the current line */
566int acl_parse_nothing(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreaua5909832007-06-17 20:40:25 +0200567{
Willy Tarreau58393e12008-07-20 10:39:22 +0200568 return 1;
569}
570
571/* always fake a data retrieval */
572int acl_fetch_nothing(struct proxy *px, struct session *l4, void *l7, int dir,
573 struct acl_expr *expr, struct acl_test *test)
574{
575 return 1;
Willy Tarreaua5909832007-06-17 20:40:25 +0200576}
577
578/* always return false */
Willy Tarreau58393e12008-07-20 10:39:22 +0200579int acl_match_nothing(struct acl_test *test, struct acl_pattern *pattern)
Willy Tarreaua5909832007-06-17 20:40:25 +0200580{
Willy Tarreau11382812008-07-09 16:18:21 +0200581 return ACL_PAT_FAIL;
Willy Tarreaua5909832007-06-17 20:40:25 +0200582}
583
584
Willy Tarreaua84d3742007-05-07 00:36:48 +0200585/* NB: For two strings to be identical, it is required that their lengths match */
586int acl_match_str(struct acl_test *test, struct acl_pattern *pattern)
587{
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200588 int icase;
589
Willy Tarreau664092c2011-12-16 19:11:42 +0100590 if (pattern->len != temp_pattern.data.str.len)
Willy Tarreau11382812008-07-09 16:18:21 +0200591 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200592
593 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
Willy Tarreau664092c2011-12-16 19:11:42 +0100594 if ((icase && strncasecmp(pattern->ptr.str, temp_pattern.data.str.str, temp_pattern.data.str.len) == 0) ||
595 (!icase && strncmp(pattern->ptr.str, temp_pattern.data.str.str, temp_pattern.data.str.len) == 0))
Willy Tarreau11382812008-07-09 16:18:21 +0200596 return ACL_PAT_PASS;
597 return ACL_PAT_FAIL;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200598}
599
Willy Tarreauc4262962010-05-10 23:42:40 +0200600/* Lookup a string in the expression's pattern tree. The node is returned if it
601 * exists, otherwise NULL.
602 */
603void *acl_lookup_str(struct acl_test *test, struct acl_expr *expr)
604{
605 /* data are stored in a tree */
606 struct ebmb_node *node;
607 char prev;
608
609 /* we may have to force a trailing zero on the test pattern */
Willy Tarreau664092c2011-12-16 19:11:42 +0100610 prev = temp_pattern.data.str.str[temp_pattern.data.str.len];
Willy Tarreauc4262962010-05-10 23:42:40 +0200611 if (prev)
Willy Tarreau664092c2011-12-16 19:11:42 +0100612 temp_pattern.data.str.str[temp_pattern.data.str.len] = '\0';
613 node = ebst_lookup(&expr->pattern_tree, temp_pattern.data.str.str);
Willy Tarreauc4262962010-05-10 23:42:40 +0200614 if (prev)
Willy Tarreau664092c2011-12-16 19:11:42 +0100615 temp_pattern.data.str.str[temp_pattern.data.str.len] = prev;
Willy Tarreauc4262962010-05-10 23:42:40 +0200616 return node;
617}
618
Willy Tarreauf3d25982007-05-08 22:45:09 +0200619/* Executes a regex. It needs to change the data. If it is marked READ_ONLY
620 * then it will be allocated and duplicated in place so that others may use
621 * it later on. Note that this is embarrassing because we always try to avoid
622 * allocating memory at run time.
623 */
624int acl_match_reg(struct acl_test *test, struct acl_pattern *pattern)
625{
626 char old_char;
627 int ret;
628
629 if (unlikely(test->flags & ACL_TEST_F_READ_ONLY)) {
630 char *new_str;
631
Willy Tarreau664092c2011-12-16 19:11:42 +0100632 new_str = calloc(1, temp_pattern.data.str.len + 1);
Willy Tarreauf3d25982007-05-08 22:45:09 +0200633 if (!new_str)
Willy Tarreau11382812008-07-09 16:18:21 +0200634 return ACL_PAT_FAIL;
Willy Tarreauf3d25982007-05-08 22:45:09 +0200635
Willy Tarreau664092c2011-12-16 19:11:42 +0100636 memcpy(new_str, temp_pattern.data.str.str, temp_pattern.data.str.len);
637 new_str[temp_pattern.data.str.len] = 0;
Willy Tarreauf3d25982007-05-08 22:45:09 +0200638 if (test->flags & ACL_TEST_F_MUST_FREE)
Willy Tarreau664092c2011-12-16 19:11:42 +0100639 free(temp_pattern.data.str.str);
640 temp_pattern.data.str.str = new_str;
Willy Tarreauf3d25982007-05-08 22:45:09 +0200641 test->flags |= ACL_TEST_F_MUST_FREE;
642 test->flags &= ~ACL_TEST_F_READ_ONLY;
643 }
644
Willy Tarreau664092c2011-12-16 19:11:42 +0100645 old_char = temp_pattern.data.str.str[temp_pattern.data.str.len];
646 temp_pattern.data.str.str[temp_pattern.data.str.len] = 0;
Willy Tarreauf3d25982007-05-08 22:45:09 +0200647
Willy Tarreau664092c2011-12-16 19:11:42 +0100648 if (regexec(pattern->ptr.reg, temp_pattern.data.str.str, 0, NULL, 0) == 0)
Willy Tarreau11382812008-07-09 16:18:21 +0200649 ret = ACL_PAT_PASS;
Willy Tarreauf3d25982007-05-08 22:45:09 +0200650 else
Willy Tarreau11382812008-07-09 16:18:21 +0200651 ret = ACL_PAT_FAIL;
Willy Tarreauf3d25982007-05-08 22:45:09 +0200652
Willy Tarreau664092c2011-12-16 19:11:42 +0100653 temp_pattern.data.str.str[temp_pattern.data.str.len] = old_char;
Willy Tarreauf3d25982007-05-08 22:45:09 +0200654 return ret;
655}
656
Willy Tarreaua84d3742007-05-07 00:36:48 +0200657/* Checks that the pattern matches the beginning of the tested string. */
658int acl_match_beg(struct acl_test *test, struct acl_pattern *pattern)
659{
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200660 int icase;
661
Willy Tarreau664092c2011-12-16 19:11:42 +0100662 if (pattern->len > temp_pattern.data.str.len)
Willy Tarreau11382812008-07-09 16:18:21 +0200663 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200664
665 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
Willy Tarreau664092c2011-12-16 19:11:42 +0100666 if ((icase && strncasecmp(pattern->ptr.str, temp_pattern.data.str.str, pattern->len) != 0) ||
667 (!icase && strncmp(pattern->ptr.str, temp_pattern.data.str.str, pattern->len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +0200668 return ACL_PAT_FAIL;
669 return ACL_PAT_PASS;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200670}
671
672/* Checks that the pattern matches the end of the tested string. */
673int acl_match_end(struct acl_test *test, struct acl_pattern *pattern)
674{
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200675 int icase;
676
Willy Tarreau664092c2011-12-16 19:11:42 +0100677 if (pattern->len > temp_pattern.data.str.len)
Willy Tarreau11382812008-07-09 16:18:21 +0200678 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200679 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
Willy Tarreau664092c2011-12-16 19:11:42 +0100680 if ((icase && strncasecmp(pattern->ptr.str, temp_pattern.data.str.str + temp_pattern.data.str.len - pattern->len, pattern->len) != 0) ||
681 (!icase && strncmp(pattern->ptr.str, temp_pattern.data.str.str + temp_pattern.data.str.len - pattern->len, pattern->len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +0200682 return ACL_PAT_FAIL;
683 return ACL_PAT_PASS;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200684}
685
686/* Checks that the pattern is included inside the tested string.
687 * NB: Suboptimal, should be rewritten using a Boyer-Moore method.
688 */
689int acl_match_sub(struct acl_test *test, struct acl_pattern *pattern)
690{
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200691 int icase;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200692 char *end;
693 char *c;
694
Willy Tarreau664092c2011-12-16 19:11:42 +0100695 if (pattern->len > temp_pattern.data.str.len)
Willy Tarreau11382812008-07-09 16:18:21 +0200696 return ACL_PAT_FAIL;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200697
Willy Tarreau664092c2011-12-16 19:11:42 +0100698 end = temp_pattern.data.str.str + temp_pattern.data.str.len - pattern->len;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200699 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
700 if (icase) {
Willy Tarreau664092c2011-12-16 19:11:42 +0100701 for (c = temp_pattern.data.str.str; c <= end; c++) {
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200702 if (tolower(*c) != tolower(*pattern->ptr.str))
703 continue;
704 if (strncasecmp(pattern->ptr.str, c, pattern->len) == 0)
Willy Tarreau11382812008-07-09 16:18:21 +0200705 return ACL_PAT_PASS;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200706 }
707 } else {
Willy Tarreau664092c2011-12-16 19:11:42 +0100708 for (c = temp_pattern.data.str.str; c <= end; c++) {
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200709 if (*c != *pattern->ptr.str)
710 continue;
711 if (strncmp(pattern->ptr.str, c, pattern->len) == 0)
Willy Tarreau11382812008-07-09 16:18:21 +0200712 return ACL_PAT_PASS;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200713 }
Willy Tarreaua84d3742007-05-07 00:36:48 +0200714 }
Willy Tarreau11382812008-07-09 16:18:21 +0200715 return ACL_PAT_FAIL;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200716}
717
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200718/* Background: Fast way to find a zero byte in a word
719 * http://graphics.stanford.edu/~seander/bithacks.html#ZeroInWord
720 * hasZeroByte = (v - 0x01010101UL) & ~v & 0x80808080UL;
721 *
722 * To look for 4 different byte values, xor the word with those bytes and
723 * then check for zero bytes:
724 *
725 * v = (((unsigned char)c * 0x1010101U) ^ delimiter)
726 * where <delimiter> is the 4 byte values to look for (as an uint)
727 * and <c> is the character that is being tested
728 */
729static inline unsigned int is_delimiter(unsigned char c, unsigned int mask)
730{
731 mask ^= (c * 0x01010101); /* propagate the char to all 4 bytes */
732 return (mask - 0x01010101) & ~mask & 0x80808080U;
733}
734
735static inline unsigned int make_4delim(unsigned char d1, unsigned char d2, unsigned char d3, unsigned char d4)
736{
737 return d1 << 24 | d2 << 16 | d3 << 8 | d4;
738}
739
Willy Tarreaua84d3742007-05-07 00:36:48 +0200740/* This one is used by other real functions. It checks that the pattern is
741 * included inside the tested string, but enclosed between the specified
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200742 * delimiters or at the beginning or end of the string. The delimiters are
743 * provided as an unsigned int made by make_4delim() and match up to 4 different
744 * delimiters. Delimiters are stripped at the beginning and end of the pattern.
Willy Tarreaua84d3742007-05-07 00:36:48 +0200745 */
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200746static int match_word(struct acl_test *test, struct acl_pattern *pattern, unsigned int delimiters)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200747{
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200748 int may_match, icase;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200749 char *c, *end;
750 char *ps;
751 int pl;
752
753 pl = pattern->len;
754 ps = pattern->ptr.str;
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200755
756 while (pl > 0 && is_delimiter(*ps, delimiters)) {
Willy Tarreaua84d3742007-05-07 00:36:48 +0200757 pl--;
758 ps++;
759 }
760
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200761 while (pl > 0 && is_delimiter(ps[pl - 1], delimiters))
Willy Tarreaua84d3742007-05-07 00:36:48 +0200762 pl--;
763
Willy Tarreau664092c2011-12-16 19:11:42 +0100764 if (pl > temp_pattern.data.str.len)
Willy Tarreau11382812008-07-09 16:18:21 +0200765 return ACL_PAT_FAIL;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200766
767 may_match = 1;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200768 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
Willy Tarreau664092c2011-12-16 19:11:42 +0100769 end = temp_pattern.data.str.str + temp_pattern.data.str.len - pl;
770 for (c = temp_pattern.data.str.str; c <= end; c++) {
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200771 if (is_delimiter(*c, delimiters)) {
Willy Tarreaua84d3742007-05-07 00:36:48 +0200772 may_match = 1;
773 continue;
774 }
Willy Tarreaua84d3742007-05-07 00:36:48 +0200775
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200776 if (!may_match)
777 continue;
778
779 if (icase) {
780 if ((tolower(*c) == tolower(*ps)) &&
781 (strncasecmp(ps, c, pl) == 0) &&
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200782 (c == end || is_delimiter(c[pl], delimiters)))
Willy Tarreau11382812008-07-09 16:18:21 +0200783 return ACL_PAT_PASS;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200784 } else {
785 if ((*c == *ps) &&
786 (strncmp(ps, c, pl) == 0) &&
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200787 (c == end || is_delimiter(c[pl], delimiters)))
Willy Tarreau11382812008-07-09 16:18:21 +0200788 return ACL_PAT_PASS;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200789 }
Willy Tarreaua84d3742007-05-07 00:36:48 +0200790 may_match = 0;
791 }
Willy Tarreau11382812008-07-09 16:18:21 +0200792 return ACL_PAT_FAIL;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200793}
794
795/* Checks that the pattern is included inside the tested string, but enclosed
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200796 * between the delimiters '?' or '/' or at the beginning or end of the string.
797 * Delimiters at the beginning or end of the pattern are ignored.
Willy Tarreaua84d3742007-05-07 00:36:48 +0200798 */
799int acl_match_dir(struct acl_test *test, struct acl_pattern *pattern)
800{
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200801 return match_word(test, pattern, make_4delim('/', '?', '?', '?'));
Willy Tarreaua84d3742007-05-07 00:36:48 +0200802}
803
804/* Checks that the pattern is included inside the tested string, but enclosed
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200805 * between the delmiters '/', '?', '.' or ":" or at the beginning or end of
806 * the string. Delimiters at the beginning or end of the pattern are ignored.
Willy Tarreaua84d3742007-05-07 00:36:48 +0200807 */
808int acl_match_dom(struct acl_test *test, struct acl_pattern *pattern)
809{
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200810 return match_word(test, pattern, make_4delim('/', '?', '.', ':'));
Willy Tarreaua84d3742007-05-07 00:36:48 +0200811}
812
813/* Checks that the integer in <test> is included between min and max */
Willy Tarreauae8b7962007-06-09 23:10:04 +0200814int acl_match_int(struct acl_test *test, struct acl_pattern *pattern)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200815{
Willy Tarreaua5e37562011-12-16 17:06:15 +0100816 if ((!pattern->val.range.min_set || pattern->val.range.min <= temp_pattern.data.integer) &&
817 (!pattern->val.range.max_set || temp_pattern.data.integer <= pattern->val.range.max))
Willy Tarreau11382812008-07-09 16:18:21 +0200818 return ACL_PAT_PASS;
819 return ACL_PAT_FAIL;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200820}
821
Willy Tarreau0e698542011-09-16 08:32:32 +0200822/* Checks that the length of the pattern in <test> is included between min and max */
823int acl_match_len(struct acl_test *test, struct acl_pattern *pattern)
824{
Willy Tarreau664092c2011-12-16 19:11:42 +0100825 if ((!pattern->val.range.min_set || pattern->val.range.min <= temp_pattern.data.str.len) &&
826 (!pattern->val.range.max_set || temp_pattern.data.str.len <= pattern->val.range.max))
Willy Tarreau0e698542011-09-16 08:32:32 +0200827 return ACL_PAT_PASS;
828 return ACL_PAT_FAIL;
829}
830
Willy Tarreaua67fad92007-05-08 19:50:09 +0200831int acl_match_ip(struct acl_test *test, struct acl_pattern *pattern)
832{
833 struct in_addr *s;
834
Willy Tarreauf4362b32011-12-16 17:49:52 +0100835 if (temp_pattern.type != PATTERN_TYPE_IP)
Willy Tarreau11382812008-07-09 16:18:21 +0200836 return ACL_PAT_FAIL;
Willy Tarreaua67fad92007-05-08 19:50:09 +0200837
Willy Tarreauf4362b32011-12-16 17:49:52 +0100838 s = &temp_pattern.data.ip;
Willy Tarreaua67fad92007-05-08 19:50:09 +0200839 if (((s->s_addr ^ pattern->val.ipv4.addr.s_addr) & pattern->val.ipv4.mask.s_addr) == 0)
Willy Tarreau11382812008-07-09 16:18:21 +0200840 return ACL_PAT_PASS;
841 return ACL_PAT_FAIL;
Willy Tarreaua67fad92007-05-08 19:50:09 +0200842}
843
Willy Tarreaub337b532010-05-13 20:03:41 +0200844/* Lookup an IPv4 address in the expression's pattern tree using the longest
845 * match method. The node is returned if it exists, otherwise NULL.
846 */
847void *acl_lookup_ip(struct acl_test *test, struct acl_expr *expr)
848{
849 struct in_addr *s;
850
Willy Tarreauf4362b32011-12-16 17:49:52 +0100851 if (temp_pattern.type != PATTERN_TYPE_IP)
Willy Tarreaub337b532010-05-13 20:03:41 +0200852 return ACL_PAT_FAIL;
853
Willy Tarreauf4362b32011-12-16 17:49:52 +0100854 s = &temp_pattern.data.ip;
Willy Tarreaub337b532010-05-13 20:03:41 +0200855 return ebmb_lookup_longest(&expr->pattern_tree, &s->s_addr);
856}
857
Willy Tarreaua84d3742007-05-07 00:36:48 +0200858/* Parse a string. It is allocated and duplicated. */
Willy Tarreauae8b7962007-06-09 23:10:04 +0200859int acl_parse_str(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200860{
861 int len;
862
Willy Tarreauae8b7962007-06-09 23:10:04 +0200863 len = strlen(*text);
Willy Tarreauc4262962010-05-10 23:42:40 +0200864
865 if (pattern->flags & ACL_PAT_F_TREE_OK) {
866 /* we're allowed to put the data in a tree whose root is pointed
867 * to by val.tree.
868 */
869 struct ebmb_node *node;
870
871 node = calloc(1, sizeof(*node) + len + 1);
872 if (!node)
873 return 0;
874 memcpy(node->key, *text, len + 1);
875 if (ebst_insert(pattern->val.tree, node) != node)
876 free(node); /* was a duplicate */
877 pattern->flags |= ACL_PAT_F_TREE; /* this pattern now contains a tree */
878 return 1;
879 }
880
Willy Tarreauae8b7962007-06-09 23:10:04 +0200881 pattern->ptr.str = strdup(*text);
Willy Tarreaua84d3742007-05-07 00:36:48 +0200882 if (!pattern->ptr.str)
883 return 0;
884 pattern->len = len;
885 return 1;
886}
887
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +0100888/* Parse and concatenate all further strings into one. */
889int
890acl_parse_strcat(const char **text, struct acl_pattern *pattern, int *opaque)
891{
892
893 int len = 0, i;
894 char *s;
895
896 for (i = 0; *text[i]; i++)
897 len += strlen(text[i])+1;
898
899 pattern->ptr.str = s = calloc(1, len);
900 if (!pattern->ptr.str)
901 return 0;
902
903 for (i = 0; *text[i]; i++)
904 s += sprintf(s, i?" %s":"%s", text[i]);
905
906 pattern->len = len;
907
908 return i;
909}
910
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +0200911/* Free data allocated by acl_parse_reg */
912static void acl_free_reg(void *ptr) {
913
914 regfree((regex_t *)ptr);
915}
916
Willy Tarreauf3d25982007-05-08 22:45:09 +0200917/* Parse a regex. It is allocated. */
Willy Tarreauae8b7962007-06-09 23:10:04 +0200918int acl_parse_reg(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreauf3d25982007-05-08 22:45:09 +0200919{
920 regex_t *preg;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200921 int icase;
Willy Tarreauf3d25982007-05-08 22:45:09 +0200922
923 preg = calloc(1, sizeof(regex_t));
924
925 if (!preg)
926 return 0;
927
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200928 icase = (pattern->flags & ACL_PAT_F_IGNORE_CASE) ? REG_ICASE : 0;
929 if (regcomp(preg, *text, REG_EXTENDED | REG_NOSUB | icase) != 0) {
Willy Tarreauf3d25982007-05-08 22:45:09 +0200930 free(preg);
931 return 0;
932 }
933
934 pattern->ptr.reg = preg;
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +0200935 pattern->freeptrbuf = &acl_free_reg;
Willy Tarreauf3d25982007-05-08 22:45:09 +0200936 return 1;
937}
938
Willy Tarreauae8b7962007-06-09 23:10:04 +0200939/* Parse a range of positive integers delimited by either ':' or '-'. If only
940 * one integer is read, it is set as both min and max. An operator may be
941 * specified as the prefix, among this list of 5 :
942 *
943 * 0:eq, 1:gt, 2:ge, 3:lt, 4:le
944 *
945 * The default operator is "eq". It supports range matching. Ranges are
946 * rejected for other operators. The operator may be changed at any time.
947 * The operator is stored in the 'opaque' argument.
948 *
Willy Tarreaua84d3742007-05-07 00:36:48 +0200949 */
Willy Tarreauae8b7962007-06-09 23:10:04 +0200950int acl_parse_int(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200951{
Willy Tarreauae8b7962007-06-09 23:10:04 +0200952 signed long long i;
953 unsigned int j, last, skip = 0;
954 const char *ptr = *text;
955
956
Willy Tarreau8f8e6452007-06-17 21:51:38 +0200957 while (!isdigit((unsigned char)*ptr)) {
Willy Tarreau1c7cc5b2010-07-18 10:46:33 +0200958 switch (get_std_op(ptr)) {
959 case STD_OP_EQ: *opaque = 0; break;
960 case STD_OP_GT: *opaque = 1; break;
961 case STD_OP_GE: *opaque = 2; break;
962 case STD_OP_LT: *opaque = 3; break;
963 case STD_OP_LE: *opaque = 4; break;
964 default:
Willy Tarreauae8b7962007-06-09 23:10:04 +0200965 return 0;
Willy Tarreau1c7cc5b2010-07-18 10:46:33 +0200966 }
Willy Tarreauae8b7962007-06-09 23:10:04 +0200967
968 skip++;
969 ptr = text[skip];
970 }
Willy Tarreaua84d3742007-05-07 00:36:48 +0200971
972 last = i = 0;
973 while (1) {
Willy Tarreauae8b7962007-06-09 23:10:04 +0200974 j = *ptr++;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200975 if ((j == '-' || j == ':') && !last) {
976 last++;
977 pattern->val.range.min = i;
978 i = 0;
979 continue;
980 }
981 j -= '0';
982 if (j > 9)
983 // also catches the terminating zero
984 break;
985 i *= 10;
986 i += j;
987 }
Willy Tarreauae8b7962007-06-09 23:10:04 +0200988
989 if (last && *opaque >= 1 && *opaque <= 4)
990 /* having a range with a min or a max is absurd */
991 return 0;
992
Willy Tarreaua84d3742007-05-07 00:36:48 +0200993 if (!last)
994 pattern->val.range.min = i;
995 pattern->val.range.max = i;
Willy Tarreauae8b7962007-06-09 23:10:04 +0200996
997 switch (*opaque) {
998 case 0: /* eq */
999 pattern->val.range.min_set = 1;
1000 pattern->val.range.max_set = 1;
1001 break;
1002 case 1: /* gt */
1003 pattern->val.range.min++; /* gt = ge + 1 */
1004 case 2: /* ge */
1005 pattern->val.range.min_set = 1;
1006 pattern->val.range.max_set = 0;
1007 break;
1008 case 3: /* lt */
1009 pattern->val.range.max--; /* lt = le - 1 */
1010 case 4: /* le */
1011 pattern->val.range.min_set = 0;
1012 pattern->val.range.max_set = 1;
1013 break;
1014 }
1015 return skip + 1;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001016}
1017
Willy Tarreau4a26d2f2008-07-15 16:05:33 +02001018/* Parse a range of positive 2-component versions delimited by either ':' or
1019 * '-'. The version consists in a major and a minor, both of which must be
1020 * smaller than 65536, because internally they will be represented as a 32-bit
1021 * integer.
1022 * If only one version is read, it is set as both min and max. Just like for
1023 * pure integers, an operator may be specified as the prefix, among this list
1024 * of 5 :
1025 *
1026 * 0:eq, 1:gt, 2:ge, 3:lt, 4:le
1027 *
1028 * The default operator is "eq". It supports range matching. Ranges are
1029 * rejected for other operators. The operator may be changed at any time.
1030 * The operator is stored in the 'opaque' argument. This allows constructs
1031 * such as the following one :
1032 *
1033 * acl obsolete_ssl ssl_req_proto lt 3
1034 * acl unsupported_ssl ssl_req_proto gt 3.1
1035 * acl valid_ssl ssl_req_proto 3.0-3.1
1036 *
1037 */
1038int acl_parse_dotted_ver(const char **text, struct acl_pattern *pattern, int *opaque)
1039{
1040 signed long long i;
1041 unsigned int j, last, skip = 0;
1042 const char *ptr = *text;
1043
1044
1045 while (!isdigit((unsigned char)*ptr)) {
Willy Tarreau1c7cc5b2010-07-18 10:46:33 +02001046 switch (get_std_op(ptr)) {
1047 case STD_OP_EQ: *opaque = 0; break;
1048 case STD_OP_GT: *opaque = 1; break;
1049 case STD_OP_GE: *opaque = 2; break;
1050 case STD_OP_LT: *opaque = 3; break;
1051 case STD_OP_LE: *opaque = 4; break;
1052 default:
Willy Tarreau4a26d2f2008-07-15 16:05:33 +02001053 return 0;
Willy Tarreau1c7cc5b2010-07-18 10:46:33 +02001054 }
Willy Tarreau4a26d2f2008-07-15 16:05:33 +02001055
1056 skip++;
1057 ptr = text[skip];
1058 }
1059
1060 last = i = 0;
1061 while (1) {
1062 j = *ptr++;
1063 if (j == '.') {
1064 /* minor part */
1065 if (i >= 65536)
1066 return 0;
1067 i <<= 16;
1068 continue;
1069 }
1070 if ((j == '-' || j == ':') && !last) {
1071 last++;
1072 if (i < 65536)
1073 i <<= 16;
1074 pattern->val.range.min = i;
1075 i = 0;
1076 continue;
1077 }
1078 j -= '0';
1079 if (j > 9)
1080 // also catches the terminating zero
1081 break;
1082 i = (i & 0xFFFF0000) + (i & 0xFFFF) * 10;
1083 i += j;
1084 }
1085
1086 /* if we only got a major version, let's shift it now */
1087 if (i < 65536)
1088 i <<= 16;
1089
1090 if (last && *opaque >= 1 && *opaque <= 4)
1091 /* having a range with a min or a max is absurd */
1092 return 0;
1093
1094 if (!last)
1095 pattern->val.range.min = i;
1096 pattern->val.range.max = i;
1097
1098 switch (*opaque) {
1099 case 0: /* eq */
1100 pattern->val.range.min_set = 1;
1101 pattern->val.range.max_set = 1;
1102 break;
1103 case 1: /* gt */
1104 pattern->val.range.min++; /* gt = ge + 1 */
1105 case 2: /* ge */
1106 pattern->val.range.min_set = 1;
1107 pattern->val.range.max_set = 0;
1108 break;
1109 case 3: /* lt */
1110 pattern->val.range.max--; /* lt = le - 1 */
1111 case 4: /* le */
1112 pattern->val.range.min_set = 0;
1113 pattern->val.range.max_set = 1;
1114 break;
1115 }
1116 return skip + 1;
1117}
1118
Willy Tarreaua67fad92007-05-08 19:50:09 +02001119/* Parse an IP address and an optional mask in the form addr[/mask].
1120 * The addr may either be an IPv4 address or a hostname. The mask
1121 * may either be a dotted mask or a number of bits. Returns 1 if OK,
1122 * otherwise 0.
1123 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02001124int acl_parse_ip(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreaua67fad92007-05-08 19:50:09 +02001125{
Willy Tarreaub337b532010-05-13 20:03:41 +02001126 struct eb_root *tree = NULL;
1127 if (pattern->flags & ACL_PAT_F_TREE_OK)
1128 tree = pattern->val.tree;
1129
1130 if (str2net(*text, &pattern->val.ipv4.addr, &pattern->val.ipv4.mask)) {
1131 unsigned int mask = ntohl(pattern->val.ipv4.mask.s_addr);
1132 struct ebmb_node *node;
1133 /* check if the mask is contiguous so that we can insert the
1134 * network into the tree. A continuous mask has only ones on
1135 * the left. This means that this mask + its lower bit added
1136 * once again is null.
1137 */
1138 if (mask + (mask & -mask) == 0 && tree) {
1139 mask = mask ? 33 - flsnz(mask & -mask) : 0; /* equals cidr value */
1140 /* FIXME: insert <addr>/<mask> into the tree here */
1141 node = calloc(1, sizeof(*node) + 4); /* reserve 4 bytes for IPv4 address */
1142 if (!node)
1143 return 0;
1144 memcpy(node->key, &pattern->val.ipv4.addr, 4); /* network byte order */
1145 node->node.pfx = mask;
1146 if (ebmb_insert_prefix(tree, node, 4) != node)
1147 free(node); /* was a duplicate */
1148 pattern->flags |= ACL_PAT_F_TREE;
1149 return 1;
1150 }
Willy Tarreauae8b7962007-06-09 23:10:04 +02001151 return 1;
Willy Tarreaub337b532010-05-13 20:03:41 +02001152 }
Willy Tarreauae8b7962007-06-09 23:10:04 +02001153 else
1154 return 0;
Willy Tarreaua67fad92007-05-08 19:50:09 +02001155}
1156
Willy Tarreaua84d3742007-05-07 00:36:48 +02001157/*
1158 * Registers the ACL keyword list <kwl> as a list of valid keywords for next
1159 * parsing sessions.
1160 */
1161void acl_register_keywords(struct acl_kw_list *kwl)
1162{
1163 LIST_ADDQ(&acl_keywords.list, &kwl->list);
1164}
1165
1166/*
1167 * Unregisters the ACL keyword list <kwl> from the list of valid keywords.
1168 */
1169void acl_unregister_keywords(struct acl_kw_list *kwl)
1170{
1171 LIST_DEL(&kwl->list);
1172 LIST_INIT(&kwl->list);
1173}
1174
1175/* Return a pointer to the ACL <name> within the list starting at <head>, or
1176 * NULL if not found.
1177 */
1178struct acl *find_acl_by_name(const char *name, struct list *head)
1179{
1180 struct acl *acl;
1181 list_for_each_entry(acl, head, list) {
1182 if (strcmp(acl->name, name) == 0)
1183 return acl;
1184 }
1185 return NULL;
1186}
1187
1188/* Return a pointer to the ACL keyword <kw>, or NULL if not found. Note that if
1189 * <kw> contains an opening parenthesis, only the left part of it is checked.
1190 */
1191struct acl_keyword *find_acl_kw(const char *kw)
1192{
1193 int index;
1194 const char *kwend;
1195 struct acl_kw_list *kwl;
1196
1197 kwend = strchr(kw, '(');
1198 if (!kwend)
1199 kwend = kw + strlen(kw);
1200
1201 list_for_each_entry(kwl, &acl_keywords.list, list) {
1202 for (index = 0; kwl->kw[index].kw != NULL; index++) {
1203 if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) &&
1204 kwl->kw[index].kw[kwend-kw] == 0)
1205 return &kwl->kw[index];
1206 }
1207 }
1208 return NULL;
1209}
1210
Willy Tarreaudfd7fca2011-03-09 07:27:02 +01001211/* NB: does nothing if <pat> is NULL */
Willy Tarreaua84d3742007-05-07 00:36:48 +02001212static void free_pattern(struct acl_pattern *pat)
1213{
Willy Tarreaudfd7fca2011-03-09 07:27:02 +01001214 if (!pat)
1215 return;
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02001216
1217 if (pat->ptr.ptr) {
1218 if (pat->freeptrbuf)
1219 pat->freeptrbuf(pat->ptr.ptr);
1220
Willy Tarreaua84d3742007-05-07 00:36:48 +02001221 free(pat->ptr.ptr);
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02001222 }
1223
Willy Tarreaua84d3742007-05-07 00:36:48 +02001224 free(pat);
1225}
1226
1227static void free_pattern_list(struct list *head)
1228{
1229 struct acl_pattern *pat, *tmp;
1230 list_for_each_entry_safe(pat, tmp, head, list)
1231 free_pattern(pat);
1232}
1233
Willy Tarreaue56cda92010-05-11 23:25:05 +02001234static void free_pattern_tree(struct eb_root *root)
1235{
1236 struct eb_node *node, *next;
1237 node = eb_first(root);
1238 while (node) {
1239 next = eb_next(node);
1240 free(node);
1241 node = next;
1242 }
1243}
1244
Willy Tarreaua84d3742007-05-07 00:36:48 +02001245static struct acl_expr *prune_acl_expr(struct acl_expr *expr)
1246{
Willy Tarreau34db1082012-04-19 17:16:54 +02001247 struct arg *arg;
1248
Willy Tarreaua84d3742007-05-07 00:36:48 +02001249 free_pattern_list(&expr->patterns);
Willy Tarreaue56cda92010-05-11 23:25:05 +02001250 free_pattern_tree(&expr->pattern_tree);
Willy Tarreaua84d3742007-05-07 00:36:48 +02001251 LIST_INIT(&expr->patterns);
Willy Tarreau34db1082012-04-19 17:16:54 +02001252
1253 for (arg = expr->args; arg; arg++) {
1254 if (arg->type == ARGT_STOP)
1255 break;
1256 if (arg->type == ARGT_FE || arg->type == ARGT_BE ||
1257 arg->type == ARGT_TAB || arg->type == ARGT_SRV ||
1258 arg->type == ARGT_USR || arg->type == ARGT_STR) {
1259 free(arg->data.str.str);
1260 arg->data.str.str = NULL;
1261 }
1262 arg++;
1263 }
1264
1265 free(expr->args);
Willy Tarreaua84d3742007-05-07 00:36:48 +02001266 expr->kw->use_cnt--;
1267 return expr;
1268}
1269
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001270static int acl_read_patterns_from_file( struct acl_keyword *aclkw,
1271 struct acl_expr *expr,
1272 const char *filename, int patflags)
1273{
1274 FILE *file;
1275 char *c;
1276 const char *args[2];
1277 struct acl_pattern *pattern;
1278 int opaque;
Willy Tarreau6a8097f2011-02-26 15:14:15 +01001279 int ret = 0;
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001280
1281 file = fopen(filename, "r");
1282 if (!file)
1283 return 0;
1284
1285 /* now parse all patterns. The file may contain only one pattern per
1286 * line. If the line contains spaces, they will be part of the pattern.
1287 * The pattern stops at the first CR, LF or EOF encountered.
1288 */
1289 opaque = 0;
Willy Tarreaue56cda92010-05-11 23:25:05 +02001290 pattern = NULL;
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001291 args[1] = "";
1292 while (fgets(trash, sizeof(trash), file) != NULL) {
1293
1294 c = trash;
Willy Tarreau58215a02010-05-13 22:07:43 +02001295
1296 /* ignore lines beginning with a dash */
1297 if (*c == '#')
1298 continue;
1299
1300 /* strip leading spaces and tabs */
1301 while (*c == ' ' || *c == '\t')
1302 c++;
1303
Willy Tarreau58215a02010-05-13 22:07:43 +02001304
1305 args[0] = c;
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001306 while (*c && *c != '\n' && *c != '\r')
1307 c++;
1308 *c = 0;
1309
Willy Tarreau51091962011-01-03 21:04:10 +01001310 /* empty lines are ignored too */
1311 if (c == args[0])
1312 continue;
1313
Willy Tarreaue56cda92010-05-11 23:25:05 +02001314 /* we keep the previous pattern along iterations as long as it's not used */
1315 if (!pattern)
1316 pattern = (struct acl_pattern *)malloc(sizeof(*pattern));
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001317 if (!pattern)
1318 goto out_close;
Willy Tarreaue56cda92010-05-11 23:25:05 +02001319
1320 memset(pattern, 0, sizeof(*pattern));
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001321 pattern->flags = patflags;
1322
Willy Tarreaue56cda92010-05-11 23:25:05 +02001323 if ((aclkw->requires & ACL_MAY_LOOKUP) && !(pattern->flags & ACL_PAT_F_IGNORE_CASE)) {
1324 /* we pre-set the data pointer to the tree's head so that functions
1325 * which are able to insert in a tree know where to do that.
1326 */
1327 pattern->flags |= ACL_PAT_F_TREE_OK;
1328 pattern->val.tree = &expr->pattern_tree;
1329 }
1330
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001331 if (!aclkw->parse(args, pattern, &opaque))
1332 goto out_free_pattern;
Willy Tarreaue56cda92010-05-11 23:25:05 +02001333
1334 /* if the parser did not feed the tree, let's chain the pattern to the list */
1335 if (!(pattern->flags & ACL_PAT_F_TREE)) {
1336 LIST_ADDQ(&expr->patterns, &pattern->list);
1337 pattern = NULL; /* get a new one */
1338 }
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001339 }
Willy Tarreau6a8097f2011-02-26 15:14:15 +01001340
1341 ret = 1; /* success */
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001342
1343 out_free_pattern:
1344 free_pattern(pattern);
1345 out_close:
1346 fclose(file);
Willy Tarreau6a8097f2011-02-26 15:14:15 +01001347 return ret;
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001348}
1349
Willy Tarreaua84d3742007-05-07 00:36:48 +02001350/* Parse an ACL expression starting at <args>[0], and return it.
1351 * Right now, the only accepted syntax is :
1352 * <subject> [<value>...]
1353 */
1354struct acl_expr *parse_acl_expr(const char **args)
1355{
1356 __label__ out_return, out_free_expr, out_free_pattern;
1357 struct acl_expr *expr;
1358 struct acl_keyword *aclkw;
1359 struct acl_pattern *pattern;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02001360 int opaque, patflags;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001361 const char *arg;
1362
1363 aclkw = find_acl_kw(args[0]);
1364 if (!aclkw || !aclkw->parse)
1365 goto out_return;
1366
1367 expr = (struct acl_expr *)calloc(1, sizeof(*expr));
1368 if (!expr)
1369 goto out_return;
1370
1371 expr->kw = aclkw;
1372 aclkw->use_cnt++;
1373 LIST_INIT(&expr->patterns);
Willy Tarreaue56cda92010-05-11 23:25:05 +02001374 expr->pattern_tree = EB_ROOT_UNIQUE;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001375
1376 arg = strchr(args[0], '(');
Willy Tarreau61612d42012-04-19 18:42:05 +02001377 if (aclkw->arg_mask) {
1378 int nbargs = 0;
Willy Tarreau34db1082012-04-19 17:16:54 +02001379 char *end;
Willy Tarreau34db1082012-04-19 17:16:54 +02001380
Willy Tarreau61612d42012-04-19 18:42:05 +02001381 if (arg != NULL) {
1382 /* there are 0 or more arguments in the form "subject(arg[,arg]*)" */
1383 arg++;
1384 end = strchr(arg, ')');
1385 if (!end)
1386 goto out_free_expr;
Willy Tarreau34db1082012-04-19 17:16:54 +02001387
Willy Tarreau61612d42012-04-19 18:42:05 +02001388 /* Parse the arguments. Note that currently we have no way to
1389 * report parsing errors, hence the NULL in the error pointers.
1390 * An error is also reported if some mandatory arguments are
1391 * missing.
1392 */
1393 nbargs = make_arg_list(arg, end - arg, aclkw->arg_mask, &expr->args,
1394 NULL, NULL, NULL);
1395 if (nbargs < 0)
1396 goto out_free_expr;
1397 }
1398 else if (ARGM(aclkw->arg_mask)) {
1399 /* there were some mandatory arguments */
Willy Tarreaua84d3742007-05-07 00:36:48 +02001400 goto out_free_expr;
Willy Tarreau61612d42012-04-19 18:42:05 +02001401 }
1402 }
1403 else {
1404 if (arg) {
1405 /* no argument expected */
1406 goto out_free_expr;
1407 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001408 }
1409
Willy Tarreaua84d3742007-05-07 00:36:48 +02001410 args++;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02001411
1412 /* check for options before patterns. Supported options are :
1413 * -i : ignore case for all patterns by default
1414 * -f : read patterns from those files
1415 * -- : everything after this is not an option
1416 */
1417 patflags = 0;
1418 while (**args == '-') {
1419 if ((*args)[1] == 'i')
1420 patflags |= ACL_PAT_F_IGNORE_CASE;
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001421 else if ((*args)[1] == 'f') {
1422 if (!acl_read_patterns_from_file(aclkw, expr, args[1], patflags | ACL_PAT_F_FROM_FILE))
1423 goto out_free_expr;
1424 args++;
1425 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +02001426 else if ((*args)[1] == '-') {
1427 args++;
1428 break;
1429 }
1430 else
1431 break;
1432 args++;
1433 }
1434
1435 /* now parse all patterns */
Willy Tarreauae8b7962007-06-09 23:10:04 +02001436 opaque = 0;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001437 while (**args) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02001438 int ret;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001439 pattern = (struct acl_pattern *)calloc(1, sizeof(*pattern));
1440 if (!pattern)
1441 goto out_free_expr;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02001442 pattern->flags = patflags;
1443
Willy Tarreauae8b7962007-06-09 23:10:04 +02001444 ret = aclkw->parse(args, pattern, &opaque);
1445 if (!ret)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001446 goto out_free_pattern;
1447 LIST_ADDQ(&expr->patterns, &pattern->list);
Willy Tarreauae8b7962007-06-09 23:10:04 +02001448 args += ret;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001449 }
1450
1451 return expr;
1452
1453 out_free_pattern:
1454 free_pattern(pattern);
1455 out_free_expr:
1456 prune_acl_expr(expr);
1457 free(expr);
1458 out_return:
1459 return NULL;
1460}
1461
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02001462/* Purge everything in the acl <acl>, then return <acl>. */
1463struct acl *prune_acl(struct acl *acl) {
1464
1465 struct acl_expr *expr, *exprb;
1466
1467 free(acl->name);
1468
1469 list_for_each_entry_safe(expr, exprb, &acl->expr, list) {
1470 LIST_DEL(&expr->list);
1471 prune_acl_expr(expr);
1472 free(expr);
1473 }
1474
1475 return acl;
1476}
1477
Willy Tarreaua84d3742007-05-07 00:36:48 +02001478/* Parse an ACL with the name starting at <args>[0], and with a list of already
1479 * known ACLs in <acl>. If the ACL was not in the list, it will be added.
Willy Tarreau2a56c5e2010-03-15 16:13:29 +01001480 * A pointer to that ACL is returned. If the ACL has an empty name, then it's
1481 * an anonymous one and it won't be merged with any other one.
Willy Tarreaua84d3742007-05-07 00:36:48 +02001482 *
1483 * args syntax: <aclname> <acl_expr>
1484 */
1485struct acl *parse_acl(const char **args, struct list *known_acl)
1486{
1487 __label__ out_return, out_free_acl_expr, out_free_name;
1488 struct acl *cur_acl;
1489 struct acl_expr *acl_expr;
1490 char *name;
1491
Willy Tarreau2a56c5e2010-03-15 16:13:29 +01001492 if (**args && invalid_char(*args))
Willy Tarreau2e74c3f2007-12-02 18:45:09 +01001493 goto out_return;
1494
Willy Tarreaua84d3742007-05-07 00:36:48 +02001495 acl_expr = parse_acl_expr(args + 1);
1496 if (!acl_expr)
1497 goto out_return;
1498
Willy Tarreau404e8ab2009-07-26 19:40:40 +02001499 /* Check for args beginning with an opening parenthesis just after the
1500 * subject, as this is almost certainly a typo. Right now we can only
1501 * emit a warning, so let's do so.
1502 */
Krzysztof Piotr Oledzki4cdd8312009-10-05 00:23:35 +02001503 if (!strchr(args[1], '(') && *args[2] == '(')
Willy Tarreau404e8ab2009-07-26 19:40:40 +02001504 Warning("parsing acl '%s' :\n"
1505 " matching '%s' for pattern '%s' is likely a mistake and probably\n"
1506 " not what you want. Maybe you need to remove the extraneous space before '('.\n"
1507 " If you are really sure this is not an error, please insert '--' between the\n"
1508 " match and the pattern to make this warning message disappear.\n",
1509 args[0], args[1], args[2]);
1510
Willy Tarreau2a56c5e2010-03-15 16:13:29 +01001511 if (*args[0])
1512 cur_acl = find_acl_by_name(args[0], known_acl);
1513 else
1514 cur_acl = NULL;
1515
Willy Tarreaua84d3742007-05-07 00:36:48 +02001516 if (!cur_acl) {
1517 name = strdup(args[0]);
1518 if (!name)
1519 goto out_free_acl_expr;
1520 cur_acl = (struct acl *)calloc(1, sizeof(*cur_acl));
1521 if (cur_acl == NULL)
1522 goto out_free_name;
1523
1524 LIST_INIT(&cur_acl->expr);
1525 LIST_ADDQ(known_acl, &cur_acl->list);
1526 cur_acl->name = name;
1527 }
1528
Willy Tarreaua9802632008-07-25 19:13:19 +02001529 cur_acl->requires |= acl_expr->kw->requires;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001530 LIST_ADDQ(&cur_acl->expr, &acl_expr->list);
1531 return cur_acl;
1532
1533 out_free_name:
1534 free(name);
1535 out_free_acl_expr:
1536 prune_acl_expr(acl_expr);
1537 free(acl_expr);
1538 out_return:
1539 return NULL;
1540}
1541
Willy Tarreau16fbe822007-06-17 11:54:31 +02001542/* Some useful ACLs provided by default. Only those used are allocated. */
1543
1544const struct {
1545 const char *name;
1546 const char *expr[4]; /* put enough for longest expression */
1547} default_acl_list[] = {
Willy Tarreau58393e12008-07-20 10:39:22 +02001548 { .name = "TRUE", .expr = {"always_true",""}},
1549 { .name = "FALSE", .expr = {"always_false",""}},
Willy Tarreau16fbe822007-06-17 11:54:31 +02001550 { .name = "LOCALHOST", .expr = {"src","127.0.0.1/8",""}},
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001551 { .name = "HTTP", .expr = {"req_proto_http",""}},
Willy Tarreau16fbe822007-06-17 11:54:31 +02001552 { .name = "HTTP_1.0", .expr = {"req_ver","1.0",""}},
1553 { .name = "HTTP_1.1", .expr = {"req_ver","1.1",""}},
1554 { .name = "METH_CONNECT", .expr = {"method","CONNECT",""}},
1555 { .name = "METH_GET", .expr = {"method","GET","HEAD",""}},
1556 { .name = "METH_HEAD", .expr = {"method","HEAD",""}},
1557 { .name = "METH_OPTIONS", .expr = {"method","OPTIONS",""}},
1558 { .name = "METH_POST", .expr = {"method","POST",""}},
1559 { .name = "METH_TRACE", .expr = {"method","TRACE",""}},
1560 { .name = "HTTP_URL_ABS", .expr = {"url_reg","^[^/:]*://",""}},
1561 { .name = "HTTP_URL_SLASH", .expr = {"url_beg","/",""}},
1562 { .name = "HTTP_URL_STAR", .expr = {"url","*",""}},
1563 { .name = "HTTP_CONTENT", .expr = {"hdr_val(content-length)","gt","0",""}},
Emeric Brunbede3d02009-06-30 17:54:00 +02001564 { .name = "RDP_COOKIE", .expr = {"req_rdp_cookie_cnt","gt","0",""}},
Willy Tarreauc6317702008-07-20 09:29:50 +02001565 { .name = "REQ_CONTENT", .expr = {"req_len","gt","0",""}},
Willy Tarreaub6fb4202008-07-20 11:18:28 +02001566 { .name = "WAIT_END", .expr = {"wait_end",""}},
Willy Tarreau16fbe822007-06-17 11:54:31 +02001567 { .name = NULL, .expr = {""}}
1568};
1569
1570/* Find a default ACL from the default_acl list, compile it and return it.
1571 * If the ACL is not found, NULL is returned. In theory, it cannot fail,
1572 * except when default ACLs are broken, in which case it will return NULL.
1573 * If <known_acl> is not NULL, the ACL will be queued at its tail.
1574 */
1575struct acl *find_acl_default(const char *acl_name, struct list *known_acl)
1576{
1577 __label__ out_return, out_free_acl_expr, out_free_name;
1578 struct acl *cur_acl;
1579 struct acl_expr *acl_expr;
1580 char *name;
1581 int index;
1582
1583 for (index = 0; default_acl_list[index].name != NULL; index++) {
1584 if (strcmp(acl_name, default_acl_list[index].name) == 0)
1585 break;
1586 }
1587
1588 if (default_acl_list[index].name == NULL)
1589 return NULL;
1590
1591 acl_expr = parse_acl_expr((const char **)default_acl_list[index].expr);
1592 if (!acl_expr)
1593 goto out_return;
1594
1595 name = strdup(acl_name);
1596 if (!name)
1597 goto out_free_acl_expr;
1598 cur_acl = (struct acl *)calloc(1, sizeof(*cur_acl));
1599 if (cur_acl == NULL)
1600 goto out_free_name;
1601
1602 cur_acl->name = name;
Willy Tarreaua55b7dc2009-07-12 09:21:30 +02001603 cur_acl->requires |= acl_expr->kw->requires;
Willy Tarreau16fbe822007-06-17 11:54:31 +02001604 LIST_INIT(&cur_acl->expr);
1605 LIST_ADDQ(&cur_acl->expr, &acl_expr->list);
1606 if (known_acl)
1607 LIST_ADDQ(known_acl, &cur_acl->list);
1608
1609 return cur_acl;
1610
1611 out_free_name:
1612 free(name);
1613 out_free_acl_expr:
1614 prune_acl_expr(acl_expr);
1615 free(acl_expr);
1616 out_return:
1617 return NULL;
1618}
Willy Tarreaua84d3742007-05-07 00:36:48 +02001619
1620/* Purge everything in the acl_cond <cond>, then return <cond>. */
1621struct acl_cond *prune_acl_cond(struct acl_cond *cond)
1622{
1623 struct acl_term_suite *suite, *tmp_suite;
1624 struct acl_term *term, *tmp_term;
1625
1626 /* iterate through all term suites and free all terms and all suites */
1627 list_for_each_entry_safe(suite, tmp_suite, &cond->suites, list) {
1628 list_for_each_entry_safe(term, tmp_term, &suite->terms, list)
1629 free(term);
1630 free(suite);
1631 }
1632 return cond;
1633}
1634
1635/* Parse an ACL condition starting at <args>[0], relying on a list of already
1636 * known ACLs passed in <known_acl>. The new condition is returned (or NULL in
1637 * case of low memory). Supports multiple conditions separated by "or".
1638 */
1639struct acl_cond *parse_acl_cond(const char **args, struct list *known_acl, int pol)
1640{
1641 __label__ out_return, out_free_suite, out_free_term;
Willy Tarreau74b98a82007-06-16 19:35:18 +02001642 int arg, neg;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001643 const char *word;
1644 struct acl *cur_acl;
1645 struct acl_term *cur_term;
1646 struct acl_term_suite *cur_suite;
1647 struct acl_cond *cond;
1648
1649 cond = (struct acl_cond *)calloc(1, sizeof(*cond));
1650 if (cond == NULL)
1651 goto out_return;
1652
1653 LIST_INIT(&cond->list);
1654 LIST_INIT(&cond->suites);
1655 cond->pol = pol;
1656
1657 cur_suite = NULL;
Willy Tarreau74b98a82007-06-16 19:35:18 +02001658 neg = 0;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001659 for (arg = 0; *args[arg]; arg++) {
1660 word = args[arg];
1661
1662 /* remove as many exclamation marks as we can */
1663 while (*word == '!') {
1664 neg = !neg;
1665 word++;
1666 }
1667
1668 /* an empty word is allowed because we cannot force the user to
1669 * always think about not leaving exclamation marks alone.
1670 */
1671 if (!*word)
1672 continue;
1673
Willy Tarreau16fbe822007-06-17 11:54:31 +02001674 if (strcasecmp(word, "or") == 0 || strcmp(word, "||") == 0) {
Willy Tarreaua84d3742007-05-07 00:36:48 +02001675 /* new term suite */
1676 cur_suite = NULL;
1677 neg = 0;
1678 continue;
1679 }
1680
Willy Tarreau95fa4692010-02-01 13:05:50 +01001681 if (strcmp(word, "{") == 0) {
1682 /* we may have a complete ACL expression between two braces,
1683 * find the last one.
1684 */
1685 int arg_end = arg + 1;
1686 const char **args_new;
1687
1688 while (*args[arg_end] && strcmp(args[arg_end], "}") != 0)
1689 arg_end++;
1690
1691 if (!*args[arg_end])
1692 goto out_free_suite;
1693
1694 args_new = calloc(1, (arg_end - arg + 1) * sizeof(*args_new));
1695 if (!args_new)
1696 goto out_free_suite;
1697
Willy Tarreau2a56c5e2010-03-15 16:13:29 +01001698 args_new[0] = "";
Willy Tarreau95fa4692010-02-01 13:05:50 +01001699 memcpy(args_new + 1, args + arg + 1, (arg_end - arg) * sizeof(*args_new));
1700 args_new[arg_end - arg] = "";
1701 cur_acl = parse_acl(args_new, known_acl);
1702 free(args_new);
1703
1704 if (!cur_acl)
Willy Tarreau16fbe822007-06-17 11:54:31 +02001705 goto out_free_suite;
Willy Tarreau95fa4692010-02-01 13:05:50 +01001706 arg = arg_end;
1707 }
1708 else {
1709 /* search for <word> in the known ACL names. If we do not find
1710 * it, let's look for it in the default ACLs, and if found, add
1711 * it to the list of ACLs of this proxy. This makes it possible
1712 * to override them.
1713 */
1714 cur_acl = find_acl_by_name(word, known_acl);
1715 if (cur_acl == NULL) {
1716 cur_acl = find_acl_default(word, known_acl);
1717 if (cur_acl == NULL)
1718 goto out_free_suite;
1719 }
Willy Tarreau16fbe822007-06-17 11:54:31 +02001720 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001721
1722 cur_term = (struct acl_term *)calloc(1, sizeof(*cur_term));
1723 if (cur_term == NULL)
1724 goto out_free_suite;
1725
1726 cur_term->acl = cur_acl;
1727 cur_term->neg = neg;
Willy Tarreaua9802632008-07-25 19:13:19 +02001728 cond->requires |= cur_acl->requires;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001729
1730 if (!cur_suite) {
1731 cur_suite = (struct acl_term_suite *)calloc(1, sizeof(*cur_suite));
1732 if (cur_term == NULL)
1733 goto out_free_term;
1734 LIST_INIT(&cur_suite->terms);
1735 LIST_ADDQ(&cond->suites, &cur_suite->list);
1736 }
1737 LIST_ADDQ(&cur_suite->terms, &cur_term->list);
Willy Tarreau74b98a82007-06-16 19:35:18 +02001738 neg = 0;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001739 }
1740
1741 return cond;
1742
1743 out_free_term:
1744 free(cur_term);
1745 out_free_suite:
1746 prune_acl_cond(cond);
1747 free(cond);
1748 out_return:
1749 return NULL;
1750}
1751
Willy Tarreau2bbba412010-01-28 16:48:33 +01001752/* Builds an ACL condition starting at the if/unless keyword. The complete
1753 * condition is returned. NULL is returned in case of error or if the first
1754 * word is neither "if" nor "unless". It automatically sets the file name and
1755 * the line number in the condition for better error reporting, and adds the
1756 * ACL requirements to the proxy's acl_requires.
1757 */
1758struct acl_cond *build_acl_cond(const char *file, int line, struct proxy *px, const char **args)
1759{
1760 int pol = ACL_COND_NONE;
1761 struct acl_cond *cond = NULL;
1762
1763 if (!strcmp(*args, "if")) {
1764 pol = ACL_COND_IF;
1765 args++;
1766 }
1767 else if (!strcmp(*args, "unless")) {
1768 pol = ACL_COND_UNLESS;
1769 args++;
1770 }
1771 else
1772 return NULL;
1773
1774 cond = parse_acl_cond(args, &px->acl, pol);
1775 if (!cond)
1776 return NULL;
1777
1778 cond->file = file;
1779 cond->line = line;
1780 px->acl_requires |= cond->requires;
1781
1782 return cond;
1783}
1784
Willy Tarreau11382812008-07-09 16:18:21 +02001785/* Execute condition <cond> and return either ACL_PAT_FAIL, ACL_PAT_MISS or
Willy Tarreaub6866442008-07-14 23:54:42 +02001786 * ACL_PAT_PASS depending on the test results. ACL_PAT_MISS may only be
1787 * returned if <dir> contains ACL_PARTIAL, indicating that incomplete data
1788 * is being examined.
1789 * This function only computes the condition, it does not apply the polarity
1790 * required by IF/UNLESS, it's up to the caller to do this using something like
1791 * this :
Willy Tarreau11382812008-07-09 16:18:21 +02001792 *
1793 * res = acl_pass(res);
Willy Tarreaub6866442008-07-14 23:54:42 +02001794 * if (res == ACL_PAT_MISS)
1795 * return 0;
Willy Tarreau11382812008-07-09 16:18:21 +02001796 * if (cond->pol == ACL_COND_UNLESS)
1797 * res = !res;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001798 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02001799int acl_exec_cond(struct acl_cond *cond, struct proxy *px, struct session *l4, void *l7, int dir)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001800{
1801 __label__ fetch_next;
1802 struct acl_term_suite *suite;
1803 struct acl_term *term;
1804 struct acl_expr *expr;
1805 struct acl *acl;
1806 struct acl_pattern *pattern;
1807 struct acl_test test;
Willy Tarreau11382812008-07-09 16:18:21 +02001808 int acl_res, suite_res, cond_res;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001809
Willy Tarreau11382812008-07-09 16:18:21 +02001810 /* We're doing a logical OR between conditions so we initialize to FAIL.
1811 * The MISS status is propagated down from the suites.
1812 */
Willy Tarreaua84d3742007-05-07 00:36:48 +02001813 cond_res = ACL_PAT_FAIL;
1814 list_for_each_entry(suite, &cond->suites, list) {
Willy Tarreau11382812008-07-09 16:18:21 +02001815 /* Evaluate condition suite <suite>. We stop at the first term
1816 * which returns ACL_PAT_FAIL. The MISS status is still propagated
1817 * in case of uncertainty in the result.
Willy Tarreaua84d3742007-05-07 00:36:48 +02001818 */
1819
1820 /* we're doing a logical AND between terms, so we must set the
1821 * initial value to PASS.
1822 */
1823 suite_res = ACL_PAT_PASS;
1824 list_for_each_entry(term, &suite->terms, list) {
1825 acl = term->acl;
1826
1827 /* FIXME: use cache !
1828 * check acl->cache_idx for this.
1829 */
1830
1831 /* ACL result not cached. Let's scan all the expressions
1832 * and use the first one to match.
1833 */
1834 acl_res = ACL_PAT_FAIL;
1835 list_for_each_entry(expr, &acl->expr, list) {
Willy Tarreaud41f8d82007-06-10 10:06:18 +02001836 /* we need to reset context and flags */
1837 memset(&test, 0, sizeof(test));
Willy Tarreaua84d3742007-05-07 00:36:48 +02001838 fetch_next:
Willy Tarreaub6866442008-07-14 23:54:42 +02001839 if (!expr->kw->fetch(px, l4, l7, dir, expr, &test)) {
1840 /* maybe we could not fetch because of missing data */
1841 if (test.flags & ACL_TEST_F_MAY_CHANGE && dir & ACL_PARTIAL)
1842 acl_res |= ACL_PAT_MISS;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001843 continue;
Willy Tarreaub6866442008-07-14 23:54:42 +02001844 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001845
Willy Tarreaua79534f2008-07-20 10:13:37 +02001846 if (test.flags & ACL_TEST_F_RES_SET) {
1847 if (test.flags & ACL_TEST_F_RES_PASS)
1848 acl_res |= ACL_PAT_PASS;
1849 else
1850 acl_res |= ACL_PAT_FAIL;
1851 }
1852 else {
Willy Tarreau020534d2010-05-16 21:45:45 +02001853 if (!eb_is_empty(&expr->pattern_tree)) {
Willy Tarreauc4262962010-05-10 23:42:40 +02001854 /* a tree is present, let's check what type it is */
1855 if (expr->kw->match == acl_match_str)
1856 acl_res |= acl_lookup_str(&test, expr) ? ACL_PAT_PASS : ACL_PAT_FAIL;
Willy Tarreaub337b532010-05-13 20:03:41 +02001857 else if (expr->kw->match == acl_match_ip)
1858 acl_res |= acl_lookup_ip(&test, expr) ? ACL_PAT_PASS : ACL_PAT_FAIL;
Willy Tarreauc4262962010-05-10 23:42:40 +02001859 }
1860
Willy Tarreaua79534f2008-07-20 10:13:37 +02001861 /* call the match() function for all tests on this value */
1862 list_for_each_entry(pattern, &expr->patterns, list) {
Willy Tarreaua79534f2008-07-20 10:13:37 +02001863 if (acl_res == ACL_PAT_PASS)
1864 break;
Willy Tarreaue56cda92010-05-11 23:25:05 +02001865 acl_res |= expr->kw->match(&test, pattern);
Willy Tarreaua79534f2008-07-20 10:13:37 +02001866 }
Krzysztof Piotr Oledzkid7528e52010-01-29 17:55:53 +01001867
Willy Tarreaue56cda92010-05-11 23:25:05 +02001868 if ((test.flags & ACL_TEST_F_NULL_MATCH) &&
Willy Tarreau020534d2010-05-16 21:45:45 +02001869 LIST_ISEMPTY(&expr->patterns) && eb_is_empty(&expr->pattern_tree))
Krzysztof Piotr Oledzkid7528e52010-01-29 17:55:53 +01001870 acl_res |= expr->kw->match(&test, NULL);
Willy Tarreaua84d3742007-05-07 00:36:48 +02001871 }
1872 /*
Willy Tarreau11382812008-07-09 16:18:21 +02001873 * OK now acl_res holds the result of this expression
1874 * as one of ACL_PAT_FAIL, ACL_PAT_MISS or ACL_PAT_PASS.
Willy Tarreaua84d3742007-05-07 00:36:48 +02001875 *
Willy Tarreau11382812008-07-09 16:18:21 +02001876 * Then if (!MISS) we can cache the result, and put
Willy Tarreaua84d3742007-05-07 00:36:48 +02001877 * (test.flags & ACL_TEST_F_VOLATILE) in the cache flags.
1878 *
1879 * FIXME: implement cache.
1880 *
1881 */
1882
1883 /* now we may have some cleanup to do */
1884 if (test.flags & ACL_TEST_F_MUST_FREE) {
Willy Tarreau664092c2011-12-16 19:11:42 +01001885 free(temp_pattern.data.str.str);
1886 temp_pattern.data.str.len = 0;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001887 }
1888
Willy Tarreau11382812008-07-09 16:18:21 +02001889 /* we're ORing these terms, so a single PASS is enough */
1890 if (acl_res == ACL_PAT_PASS)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001891 break;
1892
Willy Tarreaua84d3742007-05-07 00:36:48 +02001893 if (test.flags & ACL_TEST_F_FETCH_MORE)
1894 goto fetch_next;
Willy Tarreaub6866442008-07-14 23:54:42 +02001895
1896 /* sometimes we know the fetched data is subject to change
1897 * later and give another chance for a new match (eg: request
1898 * size, time, ...)
1899 */
1900 if (test.flags & ACL_TEST_F_MAY_CHANGE && dir & ACL_PARTIAL)
1901 acl_res |= ACL_PAT_MISS;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001902 }
1903 /*
1904 * Here we have the result of an ACL (cached or not).
1905 * ACLs are combined, negated or not, to form conditions.
1906 */
1907
Willy Tarreaua84d3742007-05-07 00:36:48 +02001908 if (term->neg)
Willy Tarreau11382812008-07-09 16:18:21 +02001909 acl_res = acl_neg(acl_res);
Willy Tarreaua84d3742007-05-07 00:36:48 +02001910
1911 suite_res &= acl_res;
Willy Tarreau11382812008-07-09 16:18:21 +02001912
1913 /* we're ANDing these terms, so a single FAIL is enough */
1914 if (suite_res == ACL_PAT_FAIL)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001915 break;
1916 }
1917 cond_res |= suite_res;
Willy Tarreau11382812008-07-09 16:18:21 +02001918
1919 /* we're ORing these terms, so a single PASS is enough */
1920 if (cond_res == ACL_PAT_PASS)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001921 break;
1922 }
Willy Tarreau11382812008-07-09 16:18:21 +02001923 return cond_res;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001924}
1925
1926
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001927/* Reports a pointer to the first ACL used in condition <cond> which requires
1928 * at least one of the USE_FLAGS in <require>. Returns NULL if none matches.
1929 * The construct is almost the same as for acl_exec_cond() since we're walking
1930 * down the ACL tree as well. It is important that the tree is really walked
1931 * through and never cached, because that way, this function can be used as a
1932 * late check.
1933 */
Willy Tarreauf1e98b82010-01-28 17:59:39 +01001934struct acl *cond_find_require(const struct acl_cond *cond, unsigned int require)
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001935{
1936 struct acl_term_suite *suite;
1937 struct acl_term *term;
1938 struct acl *acl;
1939
1940 list_for_each_entry(suite, &cond->suites, list) {
1941 list_for_each_entry(term, &suite->terms, list) {
1942 acl = term->acl;
1943 if (acl->requires & require)
1944 return acl;
1945 }
1946 }
1947 return NULL;
1948}
1949
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001950/*
1951 * Find targets for userlist and groups in acl. Function returns the number
1952 * of errors or OK if everything is fine.
1953 */
1954int
1955acl_find_targets(struct proxy *p)
1956{
1957
1958 struct acl *acl;
1959 struct acl_expr *expr;
1960 struct acl_pattern *pattern;
1961 struct userlist *ul;
Willy Tarreau63364ee2012-04-19 19:11:13 +02001962 struct arg *arg;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001963 int cfgerr = 0;
1964
1965 list_for_each_entry(acl, &p->acl, list) {
1966 list_for_each_entry(expr, &acl->expr, list) {
Willy Tarreau63364ee2012-04-19 19:11:13 +02001967 for (arg = expr->args; arg; arg++) {
1968 if (arg->type == ARGT_STOP)
1969 break;
1970 else if (arg->type == ARGT_SRV) {
1971 struct proxy *px;
1972 struct server *srv;
1973 char *pname, *sname;
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001974
Willy Tarreau63364ee2012-04-19 19:11:13 +02001975 if (!expr->args->data.str.len) {
1976 Alert("proxy %s: acl '%s' %s(): missing server name.\n",
1977 p->id, acl->name, expr->kw->kw);
1978 cfgerr++;
1979 continue;
1980 }
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001981
Willy Tarreau63364ee2012-04-19 19:11:13 +02001982 pname = expr->args->data.str.str;
1983 sname = strrchr(pname, '/');
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001984
Willy Tarreau63364ee2012-04-19 19:11:13 +02001985 if (sname)
1986 *sname++ = '\0';
1987 else {
1988 sname = pname;
1989 pname = NULL;
1990 }
1991
1992 px = p;
1993 if (pname) {
1994 px = findproxy(pname, PR_CAP_BE);
1995 if (!px) {
1996 Alert("proxy %s: acl '%s' %s(): unable to find proxy '%s'.\n",
1997 p->id, acl->name, expr->kw->kw, pname);
1998 cfgerr++;
1999 continue;
2000 }
2001 }
Willy Tarreau0b1cd942010-05-16 22:18:27 +02002002
Willy Tarreau63364ee2012-04-19 19:11:13 +02002003 srv = findserver(px, sname);
2004 if (!srv) {
2005 Alert("proxy %s: acl '%s' %s(): unable to find server '%s'.\n",
2006 p->id, acl->name, expr->kw->kw, sname);
Willy Tarreau0b1cd942010-05-16 22:18:27 +02002007 cfgerr++;
2008 continue;
2009 }
Willy Tarreau0b1cd942010-05-16 22:18:27 +02002010
Willy Tarreau63364ee2012-04-19 19:11:13 +02002011 free(expr->args->data.str.str);
2012 expr->args->data.srv = srv;
Willy Tarreau0b1cd942010-05-16 22:18:27 +02002013 continue;
2014 }
Willy Tarreau63364ee2012-04-19 19:11:13 +02002015 else if (arg->type == ARGT_USR) {
2016 if (!expr->args->data.str.len) {
2017 Alert("proxy %s: acl '%s' %s(): missing userlist name.\n",
2018 p->id, acl->name, expr->kw->kw);
2019 cfgerr++;
2020 continue;
2021 }
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01002022
Willy Tarreau63364ee2012-04-19 19:11:13 +02002023 if (p->uri_auth && p->uri_auth->userlist &&
2024 !strcmp(p->uri_auth->userlist->name, expr->args->data.str.str))
2025 ul = p->uri_auth->userlist;
2026 else
2027 ul = auth_find_userlist(expr->args->data.str.str);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01002028
Willy Tarreau63364ee2012-04-19 19:11:13 +02002029 if (!ul) {
2030 Alert("proxy %s: acl '%s' %s(%s): unable to find userlist.\n",
2031 p->id, acl->name, expr->kw->kw, expr->args->data.str.str);
2032 cfgerr++;
2033 continue;
2034 }
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01002035
Willy Tarreau63364ee2012-04-19 19:11:13 +02002036 free(expr->args->data.str.str);
2037 expr->args->data.usr = ul;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01002038 }
Willy Tarreau63364ee2012-04-19 19:11:13 +02002039 } /* end of args processing */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01002040
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01002041
2042 if (!strcmp(expr->kw->kw, "http_auth_group")) {
Willy Tarreau63364ee2012-04-19 19:11:13 +02002043 /* note: argument resolved above thanks to ARGT_USR */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01002044
2045 if (LIST_ISEMPTY(&expr->patterns)) {
2046 Alert("proxy %s: acl %s %s(): no groups specified.\n",
2047 p->id, acl->name, expr->kw->kw);
2048 cfgerr++;
2049 continue;
2050 }
2051
2052 list_for_each_entry(pattern, &expr->patterns, list) {
Willy Tarreau34db1082012-04-19 17:16:54 +02002053 pattern->val.group_mask = auth_resolve_groups(expr->args->data.usr, pattern->ptr.str);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01002054
2055 free(pattern->ptr.str);
2056 pattern->ptr.str = NULL;
2057 pattern->len = 0;
2058
2059 if (!pattern->val.group_mask) {
2060 Alert("proxy %s: acl %s %s(): invalid group(s).\n",
2061 p->id, acl->name, expr->kw->kw);
2062 cfgerr++;
2063 continue;
2064 }
2065 }
2066 }
2067 }
2068 }
2069
2070 return cfgerr;
2071}
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02002072
Willy Tarreaua84d3742007-05-07 00:36:48 +02002073/************************************************************************/
2074/* All supported keywords must be declared here. */
2075/************************************************************************/
2076
Willy Tarreau61612d42012-04-19 18:42:05 +02002077/* Note: must not be declared <const> as its list will be overwritten.
2078 * Please take care of keeping this list alphabetically sorted.
2079 */
Willy Tarreaua84d3742007-05-07 00:36:48 +02002080static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau61612d42012-04-19 18:42:05 +02002081 { "always_false", acl_parse_nothing, acl_fetch_false, acl_match_nothing, ACL_USE_NOTHING, 0 },
2082 { "always_true", acl_parse_nothing, acl_fetch_true, acl_match_nothing, ACL_USE_NOTHING, 0 },
2083 { "rep_ssl_hello_type", acl_parse_int, acl_fetch_ssl_hello_type, acl_match_int, ACL_USE_L6RTR_VOLATILE, 0 },
2084 { "req_len", acl_parse_int, acl_fetch_req_len, acl_match_int, ACL_USE_L6REQ_VOLATILE, 0 },
2085 { "req_rdp_cookie", acl_parse_str, acl_fetch_rdp_cookie, acl_match_str, ACL_USE_L6REQ_VOLATILE|ACL_MAY_LOOKUP, ARG1(0,STR) },
2086 { "req_rdp_cookie_cnt", acl_parse_int, acl_fetch_rdp_cookie_cnt, acl_match_int, ACL_USE_L6REQ_VOLATILE, ARG1(0,STR) },
2087 { "req_ssl_hello_type", acl_parse_int, acl_fetch_ssl_hello_type, acl_match_int, ACL_USE_L6REQ_VOLATILE, 0 },
2088 { "req_ssl_sni", acl_parse_str, acl_fetch_ssl_hello_sni, acl_match_str, ACL_USE_L6REQ_VOLATILE|ACL_MAY_LOOKUP, 0 },
2089 { "req_ssl_ver", acl_parse_dotted_ver, acl_fetch_req_ssl_ver, acl_match_int, ACL_USE_L6REQ_VOLATILE, 0 },
2090 { "wait_end", acl_parse_nothing, acl_fetch_wait_end, acl_match_nothing, ACL_USE_NOTHING, 0 },
Willy Tarreaua84d3742007-05-07 00:36:48 +02002091 { NULL, NULL, NULL, NULL }
2092}};
2093
2094
2095__attribute__((constructor))
2096static void __acl_init(void)
2097{
2098 acl_register_keywords(&acl_kws);
2099}
2100
2101
2102/*
2103 * Local variables:
2104 * c-indent-level: 8
2105 * c-basic-offset: 8
2106 * End:
2107 */