blob: 507516f31e433490e2b5b06019d6c27297824b27 [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>
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
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100105 temp_pattern.data.integer = l4->req->i;
Willy Tarreau44b90cc2010-05-24 20:27:29 +0200106 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
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100125 bleft = b->i;
Willy Tarreau89fa7062012-03-02 16:13:16 +0100126 data = (const unsigned char *)b->p;
Emeric Brun38e71762010-09-23 17:59:18 +0200127
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
Willy Tarreaua5e37562011-12-16 17:06:15 +0100158 temp_pattern.data.integer = hs_type;
Emeric Brun38e71762010-09-23 17:59:18 +0200159 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;
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100190 bleft = l4->req->i;
Willy Tarreau44b90cc2010-05-24 20:27:29 +0200191 if (!bleft)
192 goto too_short;
193
Willy Tarreau89fa7062012-03-02 16:13:16 +0100194 data = (const unsigned char *)l4->req->p;
Willy Tarreau44b90cc2010-05-24 20:27:29 +0200195 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 */
Willy Tarreau89fa7062012-03-02 16:13:16 +0100262 if (msg_len > buffer_max_len(l4->req) + l4->req->data - l4->req->p)
263 msg_len = buffer_max_len(l4->req) + l4->req->data - l4->req->p;
Willy Tarreau44b90cc2010-05-24 20:27:29 +0200264
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 */
Willy Tarreaua5e37562011-12-16 17:06:15 +0100271 temp_pattern.data.integer = version;
Willy Tarreau44b90cc2010-05-24 20:27:29 +0200272 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
Willy Tarreaub6672b52011-12-12 17:23:41 +0100281/* Try to extract the Server Name Indication that may be presented in a TLS
282 * client hello handshake message. The format of the message is the following
283 * (cf RFC5246 + RFC6066) :
284 * TLS frame :
285 * - uint8 type = 0x16 (Handshake)
286 * - uint16 version >= 0x0301 (TLSv1)
287 * - uint16 length (frame length)
288 * - TLS handshake :
289 * - uint8 msg_type = 0x01 (ClientHello)
290 * - uint24 length (handshake message length)
291 * - ClientHello :
292 * - uint16 client_version >= 0x0301 (TLSv1)
Willy Tarreaud017f112012-04-09 09:24:11 +0200293 * - uint8 Random[32] (4 first ones are timestamp)
Willy Tarreaub6672b52011-12-12 17:23:41 +0100294 * - SessionID :
295 * - uint8 session_id_len (0..32) (SessionID len in bytes)
296 * - uint8 session_id[session_id_len]
297 * - CipherSuite :
298 * - uint16 cipher_len >= 2 (Cipher length in bytes)
299 * - uint16 ciphers[cipher_len/2]
300 * - CompressionMethod :
301 * - uint8 compression_len >= 1 (# of supported methods)
302 * - uint8 compression_methods[compression_len]
303 * - optional client_extension_len (in bytes)
304 * - optional sequence of ClientHelloExtensions (as many bytes as above):
305 * - uint16 extension_type = 0 for server_name
306 * - uint16 extension_len
307 * - opaque extension_data[extension_len]
308 * - uint16 server_name_list_len (# of bytes here)
309 * - opaque server_names[server_name_list_len bytes]
310 * - uint8 name_type = 0 for host_name
311 * - uint16 name_len
312 * - opaque hostname[name_len bytes]
313 */
314static int
315acl_fetch_ssl_hello_sni(struct proxy *px, struct session *l4, void *l7, int dir,
316 struct acl_expr *expr, struct acl_test *test)
317{
318 int hs_len, ext_len, bleft;
319 struct buffer *b;
320 unsigned char *data;
321
322 if (!l4)
323 goto not_ssl_hello;
324
325 b = ((dir & ACL_DIR_MASK) == ACL_DIR_RTR) ? l4->rep : l4->req;
326
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100327 bleft = b->i;
Willy Tarreau89fa7062012-03-02 16:13:16 +0100328 data = (unsigned char *)b->p;
Willy Tarreaub6672b52011-12-12 17:23:41 +0100329
330 /* Check for SSL/TLS Handshake */
331 if (!bleft)
332 goto too_short;
333 if (*data != 0x16)
334 goto not_ssl_hello;
335
336 /* Check for TLSv1 or later (SSL version >= 3.1) */
337 if (bleft < 3)
338 goto too_short;
339 if (data[1] < 0x03 || data[2] < 0x01)
340 goto not_ssl_hello;
341
342 if (bleft < 5)
343 goto too_short;
344 hs_len = (data[3] << 8) + data[4];
345 if (hs_len < 1 + 3 + 2 + 32 + 1 + 2 + 2 + 1 + 1 + 2 + 2)
346 goto not_ssl_hello; /* too short to have an extension */
347
348 data += 5; /* enter TLS handshake */
349 bleft -= 5;
350
351 /* Check for a complete client hello starting at <data> */
352 if (bleft < 1)
353 goto too_short;
354 if (data[0] != 0x01) /* msg_type = Client Hello */
355 goto not_ssl_hello;
356
357 /* Check the Hello's length */
358 if (bleft < 4)
359 goto too_short;
360 hs_len = (data[1] << 16) + (data[2] << 8) + data[3];
361 if (hs_len < 2 + 32 + 1 + 2 + 2 + 1 + 1 + 2 + 2)
362 goto not_ssl_hello; /* too short to have an extension */
363
364 /* We want the full handshake here */
365 if (bleft < hs_len)
366 goto too_short;
367
368 data += 4;
369 /* Start of the ClientHello message */
370 if (data[0] < 0x03 || data[1] < 0x01) /* TLSv1 minimum */
371 goto not_ssl_hello;
372
Willy Tarreaud017f112012-04-09 09:24:11 +0200373 ext_len = data[34]; /* session_id_len */
Willy Tarreaub6672b52011-12-12 17:23:41 +0100374 if (ext_len > 32 || ext_len > (hs_len - 35)) /* check for correct session_id len */
375 goto not_ssl_hello;
376
377 /* Jump to cipher suite */
378 hs_len -= 35 + ext_len;
379 data += 35 + ext_len;
380
381 if (hs_len < 4 || /* minimum one cipher */
382 (ext_len = (data[0] << 8) + data[1]) < 2 || /* minimum 2 bytes for a cipher */
383 ext_len > hs_len)
384 goto not_ssl_hello;
385
386 /* Jump to the compression methods */
387 hs_len -= 2 + ext_len;
388 data += 2 + ext_len;
389
390 if (hs_len < 2 || /* minimum one compression method */
391 data[0] < 1 || data[0] > hs_len) /* minimum 1 bytes for a method */
392 goto not_ssl_hello;
393
394 /* Jump to the extensions */
395 hs_len -= 1 + data[0];
396 data += 1 + data[0];
397
398 if (hs_len < 2 || /* minimum one extension list length */
399 (ext_len = (data[0] << 8) + data[1]) > hs_len - 2) /* list too long */
400 goto not_ssl_hello;
401
402 hs_len = ext_len; /* limit ourselves to the extension length */
403 data += 2;
404
405 while (hs_len >= 4) {
406 int ext_type, name_type, srv_len, name_len;
407
408 ext_type = (data[0] << 8) + data[1];
409 ext_len = (data[2] << 8) + data[3];
410
411 if (ext_len > hs_len - 4) /* Extension too long */
412 goto not_ssl_hello;
413
414 if (ext_type == 0) { /* Server name */
415 if (ext_len < 2) /* need one list length */
416 goto not_ssl_hello;
417
418 srv_len = (data[4] << 8) + data[5];
419 if (srv_len < 4 || srv_len > hs_len - 6)
420 goto not_ssl_hello; /* at least 4 bytes per server name */
421
422 name_type = data[6];
423 name_len = (data[7] << 8) + data[8];
424
425 if (name_type == 0) { /* hostname */
Willy Tarreau62e7c712012-03-10 09:05:30 +0100426 temp_pattern.data.str.str = (char *)data + 9;
Willy Tarreau664092c2011-12-16 19:11:42 +0100427 temp_pattern.data.str.len = name_len;
Willy Tarreaub6672b52011-12-12 17:23:41 +0100428 test->flags = ACL_TEST_F_VOLATILE;
Willy Tarreaub6672b52011-12-12 17:23:41 +0100429 return 1;
430 }
431 }
432
433 hs_len -= 4 + ext_len;
434 data += 4 + ext_len;
435 }
436 /* server name not found */
437 goto not_ssl_hello;
438
439 too_short:
440 test->flags = ACL_TEST_F_MAY_CHANGE;
441
442 not_ssl_hello:
443
444 return 0;
445}
446
Willy Tarreau44b90cc2010-05-24 20:27:29 +0200447/* Fetch the RDP cookie identified in the expression.
448 * Note: this decoder only works with non-wrapping data.
449 */
450int
451acl_fetch_rdp_cookie(struct proxy *px, struct session *l4, void *l7, int dir,
452 struct acl_expr *expr, struct acl_test *test)
453{
454 int bleft;
455 const unsigned char *data;
456
457 if (!l4 || !l4->req)
458 return 0;
459
460 test->flags = 0;
461
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100462 bleft = l4->req->i;
Willy Tarreau44b90cc2010-05-24 20:27:29 +0200463 if (bleft <= 11)
464 goto too_short;
465
Willy Tarreau89fa7062012-03-02 16:13:16 +0100466 data = (const unsigned char *)l4->req->p + 11;
Willy Tarreau44b90cc2010-05-24 20:27:29 +0200467 bleft -= 11;
468
469 if (bleft <= 7)
470 goto too_short;
471
472 if (strncasecmp((const char *)data, "Cookie:", 7) != 0)
473 goto not_cookie;
474
475 data += 7;
476 bleft -= 7;
477
478 while (bleft > 0 && *data == ' ') {
479 data++;
480 bleft--;
481 }
482
483 if (expr->arg_len) {
484
485 if (bleft <= expr->arg_len)
486 goto too_short;
487
488 if ((data[expr->arg_len] != '=') ||
489 strncasecmp(expr->arg.str, (const char *)data, expr->arg_len) != 0)
490 goto not_cookie;
491
492 data += expr->arg_len + 1;
493 bleft -= expr->arg_len + 1;
494 } else {
495 while (bleft > 0 && *data != '=') {
496 if (*data == '\r' || *data == '\n')
497 goto not_cookie;
498 data++;
499 bleft--;
500 }
501
502 if (bleft < 1)
503 goto too_short;
504
505 if (*data != '=')
506 goto not_cookie;
507
508 data++;
509 bleft--;
510 }
511
512 /* data points to cookie value */
Willy Tarreau664092c2011-12-16 19:11:42 +0100513 temp_pattern.data.str.str = (char *)data;
514 temp_pattern.data.str.len = 0;
Willy Tarreau44b90cc2010-05-24 20:27:29 +0200515
516 while (bleft > 0 && *data != '\r') {
517 data++;
518 bleft--;
519 }
520
521 if (bleft < 2)
522 goto too_short;
523
524 if (data[0] != '\r' || data[1] != '\n')
525 goto not_cookie;
526
Willy Tarreau664092c2011-12-16 19:11:42 +0100527 temp_pattern.data.str.len = (char *)data - temp_pattern.data.str.str;
Willy Tarreau44b90cc2010-05-24 20:27:29 +0200528 test->flags = ACL_TEST_F_VOLATILE;
529 return 1;
530
531 too_short:
532 test->flags = ACL_TEST_F_MAY_CHANGE;
533 not_cookie:
534 return 0;
535}
536
537static int
538acl_fetch_rdp_cookie_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
539 struct acl_expr *expr, struct acl_test *test)
540{
541 int ret;
542
543 ret = acl_fetch_rdp_cookie(px, l4, l7, dir, expr, test);
544
Willy Tarreau664092c2011-12-16 19:11:42 +0100545 temp_pattern.data.str.str = NULL;
546 temp_pattern.data.str.len = 0;
Willy Tarreau44b90cc2010-05-24 20:27:29 +0200547
548 if (test->flags & ACL_TEST_F_MAY_CHANGE)
549 return 0;
550
551 test->flags = ACL_TEST_F_VOLATILE;
Willy Tarreaua5e37562011-12-16 17:06:15 +0100552 temp_pattern.data.integer = ret;
Willy Tarreau44b90cc2010-05-24 20:27:29 +0200553
554 return 1;
555}
556
Willy Tarreau58393e12008-07-20 10:39:22 +0200557
558/*
559 * These functions are exported and may be used by any other component.
560 */
561
562/* ignore the current line */
563int acl_parse_nothing(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreaua5909832007-06-17 20:40:25 +0200564{
Willy Tarreau58393e12008-07-20 10:39:22 +0200565 return 1;
566}
567
568/* always fake a data retrieval */
569int acl_fetch_nothing(struct proxy *px, struct session *l4, void *l7, int dir,
570 struct acl_expr *expr, struct acl_test *test)
571{
572 return 1;
Willy Tarreaua5909832007-06-17 20:40:25 +0200573}
574
575/* always return false */
Willy Tarreau58393e12008-07-20 10:39:22 +0200576int acl_match_nothing(struct acl_test *test, struct acl_pattern *pattern)
Willy Tarreaua5909832007-06-17 20:40:25 +0200577{
Willy Tarreau11382812008-07-09 16:18:21 +0200578 return ACL_PAT_FAIL;
Willy Tarreaua5909832007-06-17 20:40:25 +0200579}
580
581
Willy Tarreaua84d3742007-05-07 00:36:48 +0200582/* NB: For two strings to be identical, it is required that their lengths match */
583int acl_match_str(struct acl_test *test, struct acl_pattern *pattern)
584{
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200585 int icase;
586
Willy Tarreau664092c2011-12-16 19:11:42 +0100587 if (pattern->len != temp_pattern.data.str.len)
Willy Tarreau11382812008-07-09 16:18:21 +0200588 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200589
590 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
Willy Tarreau664092c2011-12-16 19:11:42 +0100591 if ((icase && strncasecmp(pattern->ptr.str, temp_pattern.data.str.str, temp_pattern.data.str.len) == 0) ||
592 (!icase && strncmp(pattern->ptr.str, temp_pattern.data.str.str, temp_pattern.data.str.len) == 0))
Willy Tarreau11382812008-07-09 16:18:21 +0200593 return ACL_PAT_PASS;
594 return ACL_PAT_FAIL;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200595}
596
Willy Tarreauc4262962010-05-10 23:42:40 +0200597/* Lookup a string in the expression's pattern tree. The node is returned if it
598 * exists, otherwise NULL.
599 */
600void *acl_lookup_str(struct acl_test *test, struct acl_expr *expr)
601{
602 /* data are stored in a tree */
603 struct ebmb_node *node;
604 char prev;
605
606 /* we may have to force a trailing zero on the test pattern */
Willy Tarreau664092c2011-12-16 19:11:42 +0100607 prev = temp_pattern.data.str.str[temp_pattern.data.str.len];
Willy Tarreauc4262962010-05-10 23:42:40 +0200608 if (prev)
Willy Tarreau664092c2011-12-16 19:11:42 +0100609 temp_pattern.data.str.str[temp_pattern.data.str.len] = '\0';
610 node = ebst_lookup(&expr->pattern_tree, temp_pattern.data.str.str);
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] = prev;
Willy Tarreauc4262962010-05-10 23:42:40 +0200613 return node;
614}
615
Willy Tarreauf3d25982007-05-08 22:45:09 +0200616/* Executes a regex. It needs to change the data. If it is marked READ_ONLY
617 * then it will be allocated and duplicated in place so that others may use
618 * it later on. Note that this is embarrassing because we always try to avoid
619 * allocating memory at run time.
620 */
621int acl_match_reg(struct acl_test *test, struct acl_pattern *pattern)
622{
623 char old_char;
624 int ret;
625
626 if (unlikely(test->flags & ACL_TEST_F_READ_ONLY)) {
627 char *new_str;
628
Willy Tarreau664092c2011-12-16 19:11:42 +0100629 new_str = calloc(1, temp_pattern.data.str.len + 1);
Willy Tarreauf3d25982007-05-08 22:45:09 +0200630 if (!new_str)
Willy Tarreau11382812008-07-09 16:18:21 +0200631 return ACL_PAT_FAIL;
Willy Tarreauf3d25982007-05-08 22:45:09 +0200632
Willy Tarreau664092c2011-12-16 19:11:42 +0100633 memcpy(new_str, temp_pattern.data.str.str, temp_pattern.data.str.len);
634 new_str[temp_pattern.data.str.len] = 0;
Willy Tarreauf3d25982007-05-08 22:45:09 +0200635 if (test->flags & ACL_TEST_F_MUST_FREE)
Willy Tarreau664092c2011-12-16 19:11:42 +0100636 free(temp_pattern.data.str.str);
637 temp_pattern.data.str.str = new_str;
Willy Tarreauf3d25982007-05-08 22:45:09 +0200638 test->flags |= ACL_TEST_F_MUST_FREE;
639 test->flags &= ~ACL_TEST_F_READ_ONLY;
640 }
641
Willy Tarreau664092c2011-12-16 19:11:42 +0100642 old_char = temp_pattern.data.str.str[temp_pattern.data.str.len];
643 temp_pattern.data.str.str[temp_pattern.data.str.len] = 0;
Willy Tarreauf3d25982007-05-08 22:45:09 +0200644
Willy Tarreau664092c2011-12-16 19:11:42 +0100645 if (regexec(pattern->ptr.reg, temp_pattern.data.str.str, 0, NULL, 0) == 0)
Willy Tarreau11382812008-07-09 16:18:21 +0200646 ret = ACL_PAT_PASS;
Willy Tarreauf3d25982007-05-08 22:45:09 +0200647 else
Willy Tarreau11382812008-07-09 16:18:21 +0200648 ret = ACL_PAT_FAIL;
Willy Tarreauf3d25982007-05-08 22:45:09 +0200649
Willy Tarreau664092c2011-12-16 19:11:42 +0100650 temp_pattern.data.str.str[temp_pattern.data.str.len] = old_char;
Willy Tarreauf3d25982007-05-08 22:45:09 +0200651 return ret;
652}
653
Willy Tarreaua84d3742007-05-07 00:36:48 +0200654/* Checks that the pattern matches the beginning of the tested string. */
655int acl_match_beg(struct acl_test *test, struct acl_pattern *pattern)
656{
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200657 int icase;
658
Willy Tarreau664092c2011-12-16 19:11:42 +0100659 if (pattern->len > temp_pattern.data.str.len)
Willy Tarreau11382812008-07-09 16:18:21 +0200660 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200661
662 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
Willy Tarreau664092c2011-12-16 19:11:42 +0100663 if ((icase && strncasecmp(pattern->ptr.str, temp_pattern.data.str.str, pattern->len) != 0) ||
664 (!icase && strncmp(pattern->ptr.str, temp_pattern.data.str.str, pattern->len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +0200665 return ACL_PAT_FAIL;
666 return ACL_PAT_PASS;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200667}
668
669/* Checks that the pattern matches the end of the tested string. */
670int acl_match_end(struct acl_test *test, struct acl_pattern *pattern)
671{
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200672 int icase;
673
Willy Tarreau664092c2011-12-16 19:11:42 +0100674 if (pattern->len > temp_pattern.data.str.len)
Willy Tarreau11382812008-07-09 16:18:21 +0200675 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200676 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
Willy Tarreau664092c2011-12-16 19:11:42 +0100677 if ((icase && strncasecmp(pattern->ptr.str, temp_pattern.data.str.str + temp_pattern.data.str.len - pattern->len, pattern->len) != 0) ||
678 (!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 +0200679 return ACL_PAT_FAIL;
680 return ACL_PAT_PASS;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200681}
682
683/* Checks that the pattern is included inside the tested string.
684 * NB: Suboptimal, should be rewritten using a Boyer-Moore method.
685 */
686int acl_match_sub(struct acl_test *test, struct acl_pattern *pattern)
687{
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200688 int icase;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200689 char *end;
690 char *c;
691
Willy Tarreau664092c2011-12-16 19:11:42 +0100692 if (pattern->len > temp_pattern.data.str.len)
Willy Tarreau11382812008-07-09 16:18:21 +0200693 return ACL_PAT_FAIL;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200694
Willy Tarreau664092c2011-12-16 19:11:42 +0100695 end = temp_pattern.data.str.str + temp_pattern.data.str.len - pattern->len;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200696 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
697 if (icase) {
Willy Tarreau664092c2011-12-16 19:11:42 +0100698 for (c = temp_pattern.data.str.str; c <= end; c++) {
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200699 if (tolower(*c) != tolower(*pattern->ptr.str))
700 continue;
701 if (strncasecmp(pattern->ptr.str, c, pattern->len) == 0)
Willy Tarreau11382812008-07-09 16:18:21 +0200702 return ACL_PAT_PASS;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200703 }
704 } else {
Willy Tarreau664092c2011-12-16 19:11:42 +0100705 for (c = temp_pattern.data.str.str; c <= end; c++) {
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200706 if (*c != *pattern->ptr.str)
707 continue;
708 if (strncmp(pattern->ptr.str, c, pattern->len) == 0)
Willy Tarreau11382812008-07-09 16:18:21 +0200709 return ACL_PAT_PASS;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200710 }
Willy Tarreaua84d3742007-05-07 00:36:48 +0200711 }
Willy Tarreau11382812008-07-09 16:18:21 +0200712 return ACL_PAT_FAIL;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200713}
714
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200715/* Background: Fast way to find a zero byte in a word
716 * http://graphics.stanford.edu/~seander/bithacks.html#ZeroInWord
717 * hasZeroByte = (v - 0x01010101UL) & ~v & 0x80808080UL;
718 *
719 * To look for 4 different byte values, xor the word with those bytes and
720 * then check for zero bytes:
721 *
722 * v = (((unsigned char)c * 0x1010101U) ^ delimiter)
723 * where <delimiter> is the 4 byte values to look for (as an uint)
724 * and <c> is the character that is being tested
725 */
726static inline unsigned int is_delimiter(unsigned char c, unsigned int mask)
727{
728 mask ^= (c * 0x01010101); /* propagate the char to all 4 bytes */
729 return (mask - 0x01010101) & ~mask & 0x80808080U;
730}
731
732static inline unsigned int make_4delim(unsigned char d1, unsigned char d2, unsigned char d3, unsigned char d4)
733{
734 return d1 << 24 | d2 << 16 | d3 << 8 | d4;
735}
736
Willy Tarreaua84d3742007-05-07 00:36:48 +0200737/* This one is used by other real functions. It checks that the pattern is
738 * included inside the tested string, but enclosed between the specified
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200739 * delimiters or at the beginning or end of the string. The delimiters are
740 * provided as an unsigned int made by make_4delim() and match up to 4 different
741 * delimiters. Delimiters are stripped at the beginning and end of the pattern.
Willy Tarreaua84d3742007-05-07 00:36:48 +0200742 */
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200743static int match_word(struct acl_test *test, struct acl_pattern *pattern, unsigned int delimiters)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200744{
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200745 int may_match, icase;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200746 char *c, *end;
747 char *ps;
748 int pl;
749
750 pl = pattern->len;
751 ps = pattern->ptr.str;
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200752
753 while (pl > 0 && is_delimiter(*ps, delimiters)) {
Willy Tarreaua84d3742007-05-07 00:36:48 +0200754 pl--;
755 ps++;
756 }
757
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200758 while (pl > 0 && is_delimiter(ps[pl - 1], delimiters))
Willy Tarreaua84d3742007-05-07 00:36:48 +0200759 pl--;
760
Willy Tarreau664092c2011-12-16 19:11:42 +0100761 if (pl > temp_pattern.data.str.len)
Willy Tarreau11382812008-07-09 16:18:21 +0200762 return ACL_PAT_FAIL;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200763
764 may_match = 1;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200765 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
Willy Tarreau664092c2011-12-16 19:11:42 +0100766 end = temp_pattern.data.str.str + temp_pattern.data.str.len - pl;
767 for (c = temp_pattern.data.str.str; c <= end; c++) {
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200768 if (is_delimiter(*c, delimiters)) {
Willy Tarreaua84d3742007-05-07 00:36:48 +0200769 may_match = 1;
770 continue;
771 }
Willy Tarreaua84d3742007-05-07 00:36:48 +0200772
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200773 if (!may_match)
774 continue;
775
776 if (icase) {
777 if ((tolower(*c) == tolower(*ps)) &&
778 (strncasecmp(ps, c, pl) == 0) &&
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200779 (c == end || is_delimiter(c[pl], delimiters)))
Willy Tarreau11382812008-07-09 16:18:21 +0200780 return ACL_PAT_PASS;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200781 } else {
782 if ((*c == *ps) &&
783 (strncmp(ps, c, pl) == 0) &&
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200784 (c == end || is_delimiter(c[pl], delimiters)))
Willy Tarreau11382812008-07-09 16:18:21 +0200785 return ACL_PAT_PASS;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200786 }
Willy Tarreaua84d3742007-05-07 00:36:48 +0200787 may_match = 0;
788 }
Willy Tarreau11382812008-07-09 16:18:21 +0200789 return ACL_PAT_FAIL;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200790}
791
792/* Checks that the pattern is included inside the tested string, but enclosed
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200793 * between the delimiters '?' or '/' or at the beginning or end of the string.
794 * Delimiters at the beginning or end of the pattern are ignored.
Willy Tarreaua84d3742007-05-07 00:36:48 +0200795 */
796int acl_match_dir(struct acl_test *test, struct acl_pattern *pattern)
797{
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200798 return match_word(test, pattern, make_4delim('/', '?', '?', '?'));
Willy Tarreaua84d3742007-05-07 00:36:48 +0200799}
800
801/* Checks that the pattern is included inside the tested string, but enclosed
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200802 * between the delmiters '/', '?', '.' or ":" or at the beginning or end of
803 * the string. Delimiters at the beginning or end of the pattern are ignored.
Willy Tarreaua84d3742007-05-07 00:36:48 +0200804 */
805int acl_match_dom(struct acl_test *test, struct acl_pattern *pattern)
806{
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200807 return match_word(test, pattern, make_4delim('/', '?', '.', ':'));
Willy Tarreaua84d3742007-05-07 00:36:48 +0200808}
809
810/* Checks that the integer in <test> is included between min and max */
Willy Tarreauae8b7962007-06-09 23:10:04 +0200811int acl_match_int(struct acl_test *test, struct acl_pattern *pattern)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200812{
Willy Tarreaua5e37562011-12-16 17:06:15 +0100813 if ((!pattern->val.range.min_set || pattern->val.range.min <= temp_pattern.data.integer) &&
814 (!pattern->val.range.max_set || temp_pattern.data.integer <= pattern->val.range.max))
Willy Tarreau11382812008-07-09 16:18:21 +0200815 return ACL_PAT_PASS;
816 return ACL_PAT_FAIL;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200817}
818
Willy Tarreau0e698542011-09-16 08:32:32 +0200819/* Checks that the length of the pattern in <test> is included between min and max */
820int acl_match_len(struct acl_test *test, struct acl_pattern *pattern)
821{
Willy Tarreau664092c2011-12-16 19:11:42 +0100822 if ((!pattern->val.range.min_set || pattern->val.range.min <= temp_pattern.data.str.len) &&
823 (!pattern->val.range.max_set || temp_pattern.data.str.len <= pattern->val.range.max))
Willy Tarreau0e698542011-09-16 08:32:32 +0200824 return ACL_PAT_PASS;
825 return ACL_PAT_FAIL;
826}
827
Willy Tarreaua67fad92007-05-08 19:50:09 +0200828int acl_match_ip(struct acl_test *test, struct acl_pattern *pattern)
829{
830 struct in_addr *s;
831
Willy Tarreauf4362b32011-12-16 17:49:52 +0100832 if (temp_pattern.type != PATTERN_TYPE_IP)
Willy Tarreau11382812008-07-09 16:18:21 +0200833 return ACL_PAT_FAIL;
Willy Tarreaua67fad92007-05-08 19:50:09 +0200834
Willy Tarreauf4362b32011-12-16 17:49:52 +0100835 s = &temp_pattern.data.ip;
Willy Tarreaua67fad92007-05-08 19:50:09 +0200836 if (((s->s_addr ^ pattern->val.ipv4.addr.s_addr) & pattern->val.ipv4.mask.s_addr) == 0)
Willy Tarreau11382812008-07-09 16:18:21 +0200837 return ACL_PAT_PASS;
838 return ACL_PAT_FAIL;
Willy Tarreaua67fad92007-05-08 19:50:09 +0200839}
840
Willy Tarreaub337b532010-05-13 20:03:41 +0200841/* Lookup an IPv4 address in the expression's pattern tree using the longest
842 * match method. The node is returned if it exists, otherwise NULL.
843 */
844void *acl_lookup_ip(struct acl_test *test, struct acl_expr *expr)
845{
846 struct in_addr *s;
847
Willy Tarreauf4362b32011-12-16 17:49:52 +0100848 if (temp_pattern.type != PATTERN_TYPE_IP)
Willy Tarreaub337b532010-05-13 20:03:41 +0200849 return ACL_PAT_FAIL;
850
Willy Tarreauf4362b32011-12-16 17:49:52 +0100851 s = &temp_pattern.data.ip;
Willy Tarreaub337b532010-05-13 20:03:41 +0200852 return ebmb_lookup_longest(&expr->pattern_tree, &s->s_addr);
853}
854
Willy Tarreaua84d3742007-05-07 00:36:48 +0200855/* Parse a string. It is allocated and duplicated. */
Willy Tarreauae8b7962007-06-09 23:10:04 +0200856int acl_parse_str(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200857{
858 int len;
859
Willy Tarreauae8b7962007-06-09 23:10:04 +0200860 len = strlen(*text);
Willy Tarreauc4262962010-05-10 23:42:40 +0200861
862 if (pattern->flags & ACL_PAT_F_TREE_OK) {
863 /* we're allowed to put the data in a tree whose root is pointed
864 * to by val.tree.
865 */
866 struct ebmb_node *node;
867
868 node = calloc(1, sizeof(*node) + len + 1);
869 if (!node)
870 return 0;
871 memcpy(node->key, *text, len + 1);
872 if (ebst_insert(pattern->val.tree, node) != node)
873 free(node); /* was a duplicate */
874 pattern->flags |= ACL_PAT_F_TREE; /* this pattern now contains a tree */
875 return 1;
876 }
877
Willy Tarreauae8b7962007-06-09 23:10:04 +0200878 pattern->ptr.str = strdup(*text);
Willy Tarreaua84d3742007-05-07 00:36:48 +0200879 if (!pattern->ptr.str)
880 return 0;
881 pattern->len = len;
882 return 1;
883}
884
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +0100885/* Parse and concatenate all further strings into one. */
886int
887acl_parse_strcat(const char **text, struct acl_pattern *pattern, int *opaque)
888{
889
890 int len = 0, i;
891 char *s;
892
893 for (i = 0; *text[i]; i++)
894 len += strlen(text[i])+1;
895
896 pattern->ptr.str = s = calloc(1, len);
897 if (!pattern->ptr.str)
898 return 0;
899
900 for (i = 0; *text[i]; i++)
901 s += sprintf(s, i?" %s":"%s", text[i]);
902
903 pattern->len = len;
904
905 return i;
906}
907
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +0200908/* Free data allocated by acl_parse_reg */
909static void acl_free_reg(void *ptr) {
910
911 regfree((regex_t *)ptr);
912}
913
Willy Tarreauf3d25982007-05-08 22:45:09 +0200914/* Parse a regex. It is allocated. */
Willy Tarreauae8b7962007-06-09 23:10:04 +0200915int acl_parse_reg(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreauf3d25982007-05-08 22:45:09 +0200916{
917 regex_t *preg;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200918 int icase;
Willy Tarreauf3d25982007-05-08 22:45:09 +0200919
920 preg = calloc(1, sizeof(regex_t));
921
922 if (!preg)
923 return 0;
924
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200925 icase = (pattern->flags & ACL_PAT_F_IGNORE_CASE) ? REG_ICASE : 0;
926 if (regcomp(preg, *text, REG_EXTENDED | REG_NOSUB | icase) != 0) {
Willy Tarreauf3d25982007-05-08 22:45:09 +0200927 free(preg);
928 return 0;
929 }
930
931 pattern->ptr.reg = preg;
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +0200932 pattern->freeptrbuf = &acl_free_reg;
Willy Tarreauf3d25982007-05-08 22:45:09 +0200933 return 1;
934}
935
Willy Tarreauae8b7962007-06-09 23:10:04 +0200936/* Parse a range of positive integers delimited by either ':' or '-'. If only
937 * one integer is read, it is set as both min and max. An operator may be
938 * specified as the prefix, among this list of 5 :
939 *
940 * 0:eq, 1:gt, 2:ge, 3:lt, 4:le
941 *
942 * The default operator is "eq". It supports range matching. Ranges are
943 * rejected for other operators. The operator may be changed at any time.
944 * The operator is stored in the 'opaque' argument.
945 *
Willy Tarreaua84d3742007-05-07 00:36:48 +0200946 */
Willy Tarreauae8b7962007-06-09 23:10:04 +0200947int acl_parse_int(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200948{
Willy Tarreauae8b7962007-06-09 23:10:04 +0200949 signed long long i;
950 unsigned int j, last, skip = 0;
951 const char *ptr = *text;
952
953
Willy Tarreau8f8e6452007-06-17 21:51:38 +0200954 while (!isdigit((unsigned char)*ptr)) {
Willy Tarreau1c7cc5b2010-07-18 10:46:33 +0200955 switch (get_std_op(ptr)) {
956 case STD_OP_EQ: *opaque = 0; break;
957 case STD_OP_GT: *opaque = 1; break;
958 case STD_OP_GE: *opaque = 2; break;
959 case STD_OP_LT: *opaque = 3; break;
960 case STD_OP_LE: *opaque = 4; break;
961 default:
Willy Tarreauae8b7962007-06-09 23:10:04 +0200962 return 0;
Willy Tarreau1c7cc5b2010-07-18 10:46:33 +0200963 }
Willy Tarreauae8b7962007-06-09 23:10:04 +0200964
965 skip++;
966 ptr = text[skip];
967 }
Willy Tarreaua84d3742007-05-07 00:36:48 +0200968
969 last = i = 0;
970 while (1) {
Willy Tarreauae8b7962007-06-09 23:10:04 +0200971 j = *ptr++;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200972 if ((j == '-' || j == ':') && !last) {
973 last++;
974 pattern->val.range.min = i;
975 i = 0;
976 continue;
977 }
978 j -= '0';
979 if (j > 9)
980 // also catches the terminating zero
981 break;
982 i *= 10;
983 i += j;
984 }
Willy Tarreauae8b7962007-06-09 23:10:04 +0200985
986 if (last && *opaque >= 1 && *opaque <= 4)
987 /* having a range with a min or a max is absurd */
988 return 0;
989
Willy Tarreaua84d3742007-05-07 00:36:48 +0200990 if (!last)
991 pattern->val.range.min = i;
992 pattern->val.range.max = i;
Willy Tarreauae8b7962007-06-09 23:10:04 +0200993
994 switch (*opaque) {
995 case 0: /* eq */
996 pattern->val.range.min_set = 1;
997 pattern->val.range.max_set = 1;
998 break;
999 case 1: /* gt */
1000 pattern->val.range.min++; /* gt = ge + 1 */
1001 case 2: /* ge */
1002 pattern->val.range.min_set = 1;
1003 pattern->val.range.max_set = 0;
1004 break;
1005 case 3: /* lt */
1006 pattern->val.range.max--; /* lt = le - 1 */
1007 case 4: /* le */
1008 pattern->val.range.min_set = 0;
1009 pattern->val.range.max_set = 1;
1010 break;
1011 }
1012 return skip + 1;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001013}
1014
Willy Tarreau4a26d2f2008-07-15 16:05:33 +02001015/* Parse a range of positive 2-component versions delimited by either ':' or
1016 * '-'. The version consists in a major and a minor, both of which must be
1017 * smaller than 65536, because internally they will be represented as a 32-bit
1018 * integer.
1019 * If only one version is read, it is set as both min and max. Just like for
1020 * pure integers, an operator may be specified as the prefix, among this list
1021 * of 5 :
1022 *
1023 * 0:eq, 1:gt, 2:ge, 3:lt, 4:le
1024 *
1025 * The default operator is "eq". It supports range matching. Ranges are
1026 * rejected for other operators. The operator may be changed at any time.
1027 * The operator is stored in the 'opaque' argument. This allows constructs
1028 * such as the following one :
1029 *
1030 * acl obsolete_ssl ssl_req_proto lt 3
1031 * acl unsupported_ssl ssl_req_proto gt 3.1
1032 * acl valid_ssl ssl_req_proto 3.0-3.1
1033 *
1034 */
1035int acl_parse_dotted_ver(const char **text, struct acl_pattern *pattern, int *opaque)
1036{
1037 signed long long i;
1038 unsigned int j, last, skip = 0;
1039 const char *ptr = *text;
1040
1041
1042 while (!isdigit((unsigned char)*ptr)) {
Willy Tarreau1c7cc5b2010-07-18 10:46:33 +02001043 switch (get_std_op(ptr)) {
1044 case STD_OP_EQ: *opaque = 0; break;
1045 case STD_OP_GT: *opaque = 1; break;
1046 case STD_OP_GE: *opaque = 2; break;
1047 case STD_OP_LT: *opaque = 3; break;
1048 case STD_OP_LE: *opaque = 4; break;
1049 default:
Willy Tarreau4a26d2f2008-07-15 16:05:33 +02001050 return 0;
Willy Tarreau1c7cc5b2010-07-18 10:46:33 +02001051 }
Willy Tarreau4a26d2f2008-07-15 16:05:33 +02001052
1053 skip++;
1054 ptr = text[skip];
1055 }
1056
1057 last = i = 0;
1058 while (1) {
1059 j = *ptr++;
1060 if (j == '.') {
1061 /* minor part */
1062 if (i >= 65536)
1063 return 0;
1064 i <<= 16;
1065 continue;
1066 }
1067 if ((j == '-' || j == ':') && !last) {
1068 last++;
1069 if (i < 65536)
1070 i <<= 16;
1071 pattern->val.range.min = i;
1072 i = 0;
1073 continue;
1074 }
1075 j -= '0';
1076 if (j > 9)
1077 // also catches the terminating zero
1078 break;
1079 i = (i & 0xFFFF0000) + (i & 0xFFFF) * 10;
1080 i += j;
1081 }
1082
1083 /* if we only got a major version, let's shift it now */
1084 if (i < 65536)
1085 i <<= 16;
1086
1087 if (last && *opaque >= 1 && *opaque <= 4)
1088 /* having a range with a min or a max is absurd */
1089 return 0;
1090
1091 if (!last)
1092 pattern->val.range.min = i;
1093 pattern->val.range.max = i;
1094
1095 switch (*opaque) {
1096 case 0: /* eq */
1097 pattern->val.range.min_set = 1;
1098 pattern->val.range.max_set = 1;
1099 break;
1100 case 1: /* gt */
1101 pattern->val.range.min++; /* gt = ge + 1 */
1102 case 2: /* ge */
1103 pattern->val.range.min_set = 1;
1104 pattern->val.range.max_set = 0;
1105 break;
1106 case 3: /* lt */
1107 pattern->val.range.max--; /* lt = le - 1 */
1108 case 4: /* le */
1109 pattern->val.range.min_set = 0;
1110 pattern->val.range.max_set = 1;
1111 break;
1112 }
1113 return skip + 1;
1114}
1115
Willy Tarreaua67fad92007-05-08 19:50:09 +02001116/* Parse an IP address and an optional mask in the form addr[/mask].
1117 * The addr may either be an IPv4 address or a hostname. The mask
1118 * may either be a dotted mask or a number of bits. Returns 1 if OK,
1119 * otherwise 0.
1120 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02001121int acl_parse_ip(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreaua67fad92007-05-08 19:50:09 +02001122{
Willy Tarreaub337b532010-05-13 20:03:41 +02001123 struct eb_root *tree = NULL;
1124 if (pattern->flags & ACL_PAT_F_TREE_OK)
1125 tree = pattern->val.tree;
1126
1127 if (str2net(*text, &pattern->val.ipv4.addr, &pattern->val.ipv4.mask)) {
1128 unsigned int mask = ntohl(pattern->val.ipv4.mask.s_addr);
1129 struct ebmb_node *node;
1130 /* check if the mask is contiguous so that we can insert the
1131 * network into the tree. A continuous mask has only ones on
1132 * the left. This means that this mask + its lower bit added
1133 * once again is null.
1134 */
1135 if (mask + (mask & -mask) == 0 && tree) {
1136 mask = mask ? 33 - flsnz(mask & -mask) : 0; /* equals cidr value */
1137 /* FIXME: insert <addr>/<mask> into the tree here */
1138 node = calloc(1, sizeof(*node) + 4); /* reserve 4 bytes for IPv4 address */
1139 if (!node)
1140 return 0;
1141 memcpy(node->key, &pattern->val.ipv4.addr, 4); /* network byte order */
1142 node->node.pfx = mask;
1143 if (ebmb_insert_prefix(tree, node, 4) != node)
1144 free(node); /* was a duplicate */
1145 pattern->flags |= ACL_PAT_F_TREE;
1146 return 1;
1147 }
Willy Tarreauae8b7962007-06-09 23:10:04 +02001148 return 1;
Willy Tarreaub337b532010-05-13 20:03:41 +02001149 }
Willy Tarreauae8b7962007-06-09 23:10:04 +02001150 else
1151 return 0;
Willy Tarreaua67fad92007-05-08 19:50:09 +02001152}
1153
Willy Tarreaua84d3742007-05-07 00:36:48 +02001154/*
1155 * Registers the ACL keyword list <kwl> as a list of valid keywords for next
1156 * parsing sessions.
1157 */
1158void acl_register_keywords(struct acl_kw_list *kwl)
1159{
1160 LIST_ADDQ(&acl_keywords.list, &kwl->list);
1161}
1162
1163/*
1164 * Unregisters the ACL keyword list <kwl> from the list of valid keywords.
1165 */
1166void acl_unregister_keywords(struct acl_kw_list *kwl)
1167{
1168 LIST_DEL(&kwl->list);
1169 LIST_INIT(&kwl->list);
1170}
1171
1172/* Return a pointer to the ACL <name> within the list starting at <head>, or
1173 * NULL if not found.
1174 */
1175struct acl *find_acl_by_name(const char *name, struct list *head)
1176{
1177 struct acl *acl;
1178 list_for_each_entry(acl, head, list) {
1179 if (strcmp(acl->name, name) == 0)
1180 return acl;
1181 }
1182 return NULL;
1183}
1184
1185/* Return a pointer to the ACL keyword <kw>, or NULL if not found. Note that if
1186 * <kw> contains an opening parenthesis, only the left part of it is checked.
1187 */
1188struct acl_keyword *find_acl_kw(const char *kw)
1189{
1190 int index;
1191 const char *kwend;
1192 struct acl_kw_list *kwl;
1193
1194 kwend = strchr(kw, '(');
1195 if (!kwend)
1196 kwend = kw + strlen(kw);
1197
1198 list_for_each_entry(kwl, &acl_keywords.list, list) {
1199 for (index = 0; kwl->kw[index].kw != NULL; index++) {
1200 if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) &&
1201 kwl->kw[index].kw[kwend-kw] == 0)
1202 return &kwl->kw[index];
1203 }
1204 }
1205 return NULL;
1206}
1207
Willy Tarreaudfd7fca2011-03-09 07:27:02 +01001208/* NB: does nothing if <pat> is NULL */
Willy Tarreaua84d3742007-05-07 00:36:48 +02001209static void free_pattern(struct acl_pattern *pat)
1210{
Willy Tarreaudfd7fca2011-03-09 07:27:02 +01001211 if (!pat)
1212 return;
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02001213
1214 if (pat->ptr.ptr) {
1215 if (pat->freeptrbuf)
1216 pat->freeptrbuf(pat->ptr.ptr);
1217
Willy Tarreaua84d3742007-05-07 00:36:48 +02001218 free(pat->ptr.ptr);
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02001219 }
1220
Willy Tarreaua84d3742007-05-07 00:36:48 +02001221 free(pat);
1222}
1223
1224static void free_pattern_list(struct list *head)
1225{
1226 struct acl_pattern *pat, *tmp;
1227 list_for_each_entry_safe(pat, tmp, head, list)
1228 free_pattern(pat);
1229}
1230
Willy Tarreaue56cda92010-05-11 23:25:05 +02001231static void free_pattern_tree(struct eb_root *root)
1232{
1233 struct eb_node *node, *next;
1234 node = eb_first(root);
1235 while (node) {
1236 next = eb_next(node);
1237 free(node);
1238 node = next;
1239 }
1240}
1241
Willy Tarreaua84d3742007-05-07 00:36:48 +02001242static struct acl_expr *prune_acl_expr(struct acl_expr *expr)
1243{
1244 free_pattern_list(&expr->patterns);
Willy Tarreaue56cda92010-05-11 23:25:05 +02001245 free_pattern_tree(&expr->pattern_tree);
Willy Tarreaua84d3742007-05-07 00:36:48 +02001246 LIST_INIT(&expr->patterns);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001247 if (expr->arg_len && expr->arg.str)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001248 free(expr->arg.str);
1249 expr->kw->use_cnt--;
1250 return expr;
1251}
1252
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001253static int acl_read_patterns_from_file( struct acl_keyword *aclkw,
1254 struct acl_expr *expr,
1255 const char *filename, int patflags)
1256{
1257 FILE *file;
1258 char *c;
1259 const char *args[2];
1260 struct acl_pattern *pattern;
1261 int opaque;
Willy Tarreau6a8097f2011-02-26 15:14:15 +01001262 int ret = 0;
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001263
1264 file = fopen(filename, "r");
1265 if (!file)
1266 return 0;
1267
1268 /* now parse all patterns. The file may contain only one pattern per
1269 * line. If the line contains spaces, they will be part of the pattern.
1270 * The pattern stops at the first CR, LF or EOF encountered.
1271 */
1272 opaque = 0;
Willy Tarreaue56cda92010-05-11 23:25:05 +02001273 pattern = NULL;
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001274 args[1] = "";
1275 while (fgets(trash, sizeof(trash), file) != NULL) {
1276
1277 c = trash;
Willy Tarreau58215a02010-05-13 22:07:43 +02001278
1279 /* ignore lines beginning with a dash */
1280 if (*c == '#')
1281 continue;
1282
1283 /* strip leading spaces and tabs */
1284 while (*c == ' ' || *c == '\t')
1285 c++;
1286
Willy Tarreau58215a02010-05-13 22:07:43 +02001287
1288 args[0] = c;
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001289 while (*c && *c != '\n' && *c != '\r')
1290 c++;
1291 *c = 0;
1292
Willy Tarreau51091962011-01-03 21:04:10 +01001293 /* empty lines are ignored too */
1294 if (c == args[0])
1295 continue;
1296
Willy Tarreaue56cda92010-05-11 23:25:05 +02001297 /* we keep the previous pattern along iterations as long as it's not used */
1298 if (!pattern)
1299 pattern = (struct acl_pattern *)malloc(sizeof(*pattern));
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001300 if (!pattern)
1301 goto out_close;
Willy Tarreaue56cda92010-05-11 23:25:05 +02001302
1303 memset(pattern, 0, sizeof(*pattern));
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001304 pattern->flags = patflags;
1305
Willy Tarreaue56cda92010-05-11 23:25:05 +02001306 if ((aclkw->requires & ACL_MAY_LOOKUP) && !(pattern->flags & ACL_PAT_F_IGNORE_CASE)) {
1307 /* we pre-set the data pointer to the tree's head so that functions
1308 * which are able to insert in a tree know where to do that.
1309 */
1310 pattern->flags |= ACL_PAT_F_TREE_OK;
1311 pattern->val.tree = &expr->pattern_tree;
1312 }
1313
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001314 if (!aclkw->parse(args, pattern, &opaque))
1315 goto out_free_pattern;
Willy Tarreaue56cda92010-05-11 23:25:05 +02001316
1317 /* if the parser did not feed the tree, let's chain the pattern to the list */
1318 if (!(pattern->flags & ACL_PAT_F_TREE)) {
1319 LIST_ADDQ(&expr->patterns, &pattern->list);
1320 pattern = NULL; /* get a new one */
1321 }
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001322 }
Willy Tarreau6a8097f2011-02-26 15:14:15 +01001323
1324 ret = 1; /* success */
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001325
1326 out_free_pattern:
1327 free_pattern(pattern);
1328 out_close:
1329 fclose(file);
Willy Tarreau6a8097f2011-02-26 15:14:15 +01001330 return ret;
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001331}
1332
Willy Tarreaua84d3742007-05-07 00:36:48 +02001333/* Parse an ACL expression starting at <args>[0], and return it.
1334 * Right now, the only accepted syntax is :
1335 * <subject> [<value>...]
1336 */
1337struct acl_expr *parse_acl_expr(const char **args)
1338{
1339 __label__ out_return, out_free_expr, out_free_pattern;
1340 struct acl_expr *expr;
1341 struct acl_keyword *aclkw;
1342 struct acl_pattern *pattern;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02001343 int opaque, patflags;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001344 const char *arg;
1345
1346 aclkw = find_acl_kw(args[0]);
1347 if (!aclkw || !aclkw->parse)
1348 goto out_return;
1349
1350 expr = (struct acl_expr *)calloc(1, sizeof(*expr));
1351 if (!expr)
1352 goto out_return;
1353
1354 expr->kw = aclkw;
1355 aclkw->use_cnt++;
1356 LIST_INIT(&expr->patterns);
Willy Tarreaue56cda92010-05-11 23:25:05 +02001357 expr->pattern_tree = EB_ROOT_UNIQUE;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001358 expr->arg.str = NULL;
Willy Tarreaubb768912007-06-10 11:17:01 +02001359 expr->arg_len = 0;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001360
1361 arg = strchr(args[0], '(');
1362 if (arg != NULL) {
1363 char *end, *arg2;
1364 /* there is an argument in the form "subject(arg)" */
1365 arg++;
1366 end = strchr(arg, ')');
1367 if (!end)
1368 goto out_free_expr;
Willy Tarreauac778f52010-01-26 19:02:46 +01001369 arg2 = my_strndup(arg, end - arg);
Willy Tarreau1edb1442010-01-27 20:13:38 +01001370 if (!arg2)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001371 goto out_free_expr;
Willy Tarreaubb768912007-06-10 11:17:01 +02001372 expr->arg_len = end - arg;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001373 expr->arg.str = arg2;
1374 }
1375
Willy Tarreaua84d3742007-05-07 00:36:48 +02001376 args++;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02001377
1378 /* check for options before patterns. Supported options are :
1379 * -i : ignore case for all patterns by default
1380 * -f : read patterns from those files
1381 * -- : everything after this is not an option
1382 */
1383 patflags = 0;
1384 while (**args == '-') {
1385 if ((*args)[1] == 'i')
1386 patflags |= ACL_PAT_F_IGNORE_CASE;
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001387 else if ((*args)[1] == 'f') {
1388 if (!acl_read_patterns_from_file(aclkw, expr, args[1], patflags | ACL_PAT_F_FROM_FILE))
1389 goto out_free_expr;
1390 args++;
1391 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +02001392 else if ((*args)[1] == '-') {
1393 args++;
1394 break;
1395 }
1396 else
1397 break;
1398 args++;
1399 }
1400
1401 /* now parse all patterns */
Willy Tarreauae8b7962007-06-09 23:10:04 +02001402 opaque = 0;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001403 while (**args) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02001404 int ret;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001405 pattern = (struct acl_pattern *)calloc(1, sizeof(*pattern));
1406 if (!pattern)
1407 goto out_free_expr;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02001408 pattern->flags = patflags;
1409
Willy Tarreauae8b7962007-06-09 23:10:04 +02001410 ret = aclkw->parse(args, pattern, &opaque);
1411 if (!ret)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001412 goto out_free_pattern;
1413 LIST_ADDQ(&expr->patterns, &pattern->list);
Willy Tarreauae8b7962007-06-09 23:10:04 +02001414 args += ret;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001415 }
1416
1417 return expr;
1418
1419 out_free_pattern:
1420 free_pattern(pattern);
1421 out_free_expr:
1422 prune_acl_expr(expr);
1423 free(expr);
1424 out_return:
1425 return NULL;
1426}
1427
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02001428/* Purge everything in the acl <acl>, then return <acl>. */
1429struct acl *prune_acl(struct acl *acl) {
1430
1431 struct acl_expr *expr, *exprb;
1432
1433 free(acl->name);
1434
1435 list_for_each_entry_safe(expr, exprb, &acl->expr, list) {
1436 LIST_DEL(&expr->list);
1437 prune_acl_expr(expr);
1438 free(expr);
1439 }
1440
1441 return acl;
1442}
1443
Willy Tarreaua84d3742007-05-07 00:36:48 +02001444/* Parse an ACL with the name starting at <args>[0], and with a list of already
1445 * known ACLs in <acl>. If the ACL was not in the list, it will be added.
Willy Tarreau2a56c5e2010-03-15 16:13:29 +01001446 * A pointer to that ACL is returned. If the ACL has an empty name, then it's
1447 * an anonymous one and it won't be merged with any other one.
Willy Tarreaua84d3742007-05-07 00:36:48 +02001448 *
1449 * args syntax: <aclname> <acl_expr>
1450 */
1451struct acl *parse_acl(const char **args, struct list *known_acl)
1452{
1453 __label__ out_return, out_free_acl_expr, out_free_name;
1454 struct acl *cur_acl;
1455 struct acl_expr *acl_expr;
1456 char *name;
1457
Willy Tarreau2a56c5e2010-03-15 16:13:29 +01001458 if (**args && invalid_char(*args))
Willy Tarreau2e74c3f2007-12-02 18:45:09 +01001459 goto out_return;
1460
Willy Tarreaua84d3742007-05-07 00:36:48 +02001461 acl_expr = parse_acl_expr(args + 1);
1462 if (!acl_expr)
1463 goto out_return;
1464
Willy Tarreau404e8ab2009-07-26 19:40:40 +02001465 /* Check for args beginning with an opening parenthesis just after the
1466 * subject, as this is almost certainly a typo. Right now we can only
1467 * emit a warning, so let's do so.
1468 */
Krzysztof Piotr Oledzki4cdd8312009-10-05 00:23:35 +02001469 if (!strchr(args[1], '(') && *args[2] == '(')
Willy Tarreau404e8ab2009-07-26 19:40:40 +02001470 Warning("parsing acl '%s' :\n"
1471 " matching '%s' for pattern '%s' is likely a mistake and probably\n"
1472 " not what you want. Maybe you need to remove the extraneous space before '('.\n"
1473 " If you are really sure this is not an error, please insert '--' between the\n"
1474 " match and the pattern to make this warning message disappear.\n",
1475 args[0], args[1], args[2]);
1476
Willy Tarreau2a56c5e2010-03-15 16:13:29 +01001477 if (*args[0])
1478 cur_acl = find_acl_by_name(args[0], known_acl);
1479 else
1480 cur_acl = NULL;
1481
Willy Tarreaua84d3742007-05-07 00:36:48 +02001482 if (!cur_acl) {
1483 name = strdup(args[0]);
1484 if (!name)
1485 goto out_free_acl_expr;
1486 cur_acl = (struct acl *)calloc(1, sizeof(*cur_acl));
1487 if (cur_acl == NULL)
1488 goto out_free_name;
1489
1490 LIST_INIT(&cur_acl->expr);
1491 LIST_ADDQ(known_acl, &cur_acl->list);
1492 cur_acl->name = name;
1493 }
1494
Willy Tarreaua9802632008-07-25 19:13:19 +02001495 cur_acl->requires |= acl_expr->kw->requires;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001496 LIST_ADDQ(&cur_acl->expr, &acl_expr->list);
1497 return cur_acl;
1498
1499 out_free_name:
1500 free(name);
1501 out_free_acl_expr:
1502 prune_acl_expr(acl_expr);
1503 free(acl_expr);
1504 out_return:
1505 return NULL;
1506}
1507
Willy Tarreau16fbe822007-06-17 11:54:31 +02001508/* Some useful ACLs provided by default. Only those used are allocated. */
1509
1510const struct {
1511 const char *name;
1512 const char *expr[4]; /* put enough for longest expression */
1513} default_acl_list[] = {
Willy Tarreau58393e12008-07-20 10:39:22 +02001514 { .name = "TRUE", .expr = {"always_true",""}},
1515 { .name = "FALSE", .expr = {"always_false",""}},
Willy Tarreau16fbe822007-06-17 11:54:31 +02001516 { .name = "LOCALHOST", .expr = {"src","127.0.0.1/8",""}},
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001517 { .name = "HTTP", .expr = {"req_proto_http",""}},
Willy Tarreau16fbe822007-06-17 11:54:31 +02001518 { .name = "HTTP_1.0", .expr = {"req_ver","1.0",""}},
1519 { .name = "HTTP_1.1", .expr = {"req_ver","1.1",""}},
1520 { .name = "METH_CONNECT", .expr = {"method","CONNECT",""}},
1521 { .name = "METH_GET", .expr = {"method","GET","HEAD",""}},
1522 { .name = "METH_HEAD", .expr = {"method","HEAD",""}},
1523 { .name = "METH_OPTIONS", .expr = {"method","OPTIONS",""}},
1524 { .name = "METH_POST", .expr = {"method","POST",""}},
1525 { .name = "METH_TRACE", .expr = {"method","TRACE",""}},
1526 { .name = "HTTP_URL_ABS", .expr = {"url_reg","^[^/:]*://",""}},
1527 { .name = "HTTP_URL_SLASH", .expr = {"url_beg","/",""}},
1528 { .name = "HTTP_URL_STAR", .expr = {"url","*",""}},
1529 { .name = "HTTP_CONTENT", .expr = {"hdr_val(content-length)","gt","0",""}},
Emeric Brunbede3d02009-06-30 17:54:00 +02001530 { .name = "RDP_COOKIE", .expr = {"req_rdp_cookie_cnt","gt","0",""}},
Willy Tarreauc6317702008-07-20 09:29:50 +02001531 { .name = "REQ_CONTENT", .expr = {"req_len","gt","0",""}},
Willy Tarreaub6fb4202008-07-20 11:18:28 +02001532 { .name = "WAIT_END", .expr = {"wait_end",""}},
Willy Tarreau16fbe822007-06-17 11:54:31 +02001533 { .name = NULL, .expr = {""}}
1534};
1535
1536/* Find a default ACL from the default_acl list, compile it and return it.
1537 * If the ACL is not found, NULL is returned. In theory, it cannot fail,
1538 * except when default ACLs are broken, in which case it will return NULL.
1539 * If <known_acl> is not NULL, the ACL will be queued at its tail.
1540 */
1541struct acl *find_acl_default(const char *acl_name, struct list *known_acl)
1542{
1543 __label__ out_return, out_free_acl_expr, out_free_name;
1544 struct acl *cur_acl;
1545 struct acl_expr *acl_expr;
1546 char *name;
1547 int index;
1548
1549 for (index = 0; default_acl_list[index].name != NULL; index++) {
1550 if (strcmp(acl_name, default_acl_list[index].name) == 0)
1551 break;
1552 }
1553
1554 if (default_acl_list[index].name == NULL)
1555 return NULL;
1556
1557 acl_expr = parse_acl_expr((const char **)default_acl_list[index].expr);
1558 if (!acl_expr)
1559 goto out_return;
1560
1561 name = strdup(acl_name);
1562 if (!name)
1563 goto out_free_acl_expr;
1564 cur_acl = (struct acl *)calloc(1, sizeof(*cur_acl));
1565 if (cur_acl == NULL)
1566 goto out_free_name;
1567
1568 cur_acl->name = name;
Willy Tarreaua55b7dc2009-07-12 09:21:30 +02001569 cur_acl->requires |= acl_expr->kw->requires;
Willy Tarreau16fbe822007-06-17 11:54:31 +02001570 LIST_INIT(&cur_acl->expr);
1571 LIST_ADDQ(&cur_acl->expr, &acl_expr->list);
1572 if (known_acl)
1573 LIST_ADDQ(known_acl, &cur_acl->list);
1574
1575 return cur_acl;
1576
1577 out_free_name:
1578 free(name);
1579 out_free_acl_expr:
1580 prune_acl_expr(acl_expr);
1581 free(acl_expr);
1582 out_return:
1583 return NULL;
1584}
Willy Tarreaua84d3742007-05-07 00:36:48 +02001585
1586/* Purge everything in the acl_cond <cond>, then return <cond>. */
1587struct acl_cond *prune_acl_cond(struct acl_cond *cond)
1588{
1589 struct acl_term_suite *suite, *tmp_suite;
1590 struct acl_term *term, *tmp_term;
1591
1592 /* iterate through all term suites and free all terms and all suites */
1593 list_for_each_entry_safe(suite, tmp_suite, &cond->suites, list) {
1594 list_for_each_entry_safe(term, tmp_term, &suite->terms, list)
1595 free(term);
1596 free(suite);
1597 }
1598 return cond;
1599}
1600
1601/* Parse an ACL condition starting at <args>[0], relying on a list of already
1602 * known ACLs passed in <known_acl>. The new condition is returned (or NULL in
1603 * case of low memory). Supports multiple conditions separated by "or".
1604 */
1605struct acl_cond *parse_acl_cond(const char **args, struct list *known_acl, int pol)
1606{
1607 __label__ out_return, out_free_suite, out_free_term;
Willy Tarreau74b98a82007-06-16 19:35:18 +02001608 int arg, neg;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001609 const char *word;
1610 struct acl *cur_acl;
1611 struct acl_term *cur_term;
1612 struct acl_term_suite *cur_suite;
1613 struct acl_cond *cond;
1614
1615 cond = (struct acl_cond *)calloc(1, sizeof(*cond));
1616 if (cond == NULL)
1617 goto out_return;
1618
1619 LIST_INIT(&cond->list);
1620 LIST_INIT(&cond->suites);
1621 cond->pol = pol;
1622
1623 cur_suite = NULL;
Willy Tarreau74b98a82007-06-16 19:35:18 +02001624 neg = 0;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001625 for (arg = 0; *args[arg]; arg++) {
1626 word = args[arg];
1627
1628 /* remove as many exclamation marks as we can */
1629 while (*word == '!') {
1630 neg = !neg;
1631 word++;
1632 }
1633
1634 /* an empty word is allowed because we cannot force the user to
1635 * always think about not leaving exclamation marks alone.
1636 */
1637 if (!*word)
1638 continue;
1639
Willy Tarreau16fbe822007-06-17 11:54:31 +02001640 if (strcasecmp(word, "or") == 0 || strcmp(word, "||") == 0) {
Willy Tarreaua84d3742007-05-07 00:36:48 +02001641 /* new term suite */
1642 cur_suite = NULL;
1643 neg = 0;
1644 continue;
1645 }
1646
Willy Tarreau95fa4692010-02-01 13:05:50 +01001647 if (strcmp(word, "{") == 0) {
1648 /* we may have a complete ACL expression between two braces,
1649 * find the last one.
1650 */
1651 int arg_end = arg + 1;
1652 const char **args_new;
1653
1654 while (*args[arg_end] && strcmp(args[arg_end], "}") != 0)
1655 arg_end++;
1656
1657 if (!*args[arg_end])
1658 goto out_free_suite;
1659
1660 args_new = calloc(1, (arg_end - arg + 1) * sizeof(*args_new));
1661 if (!args_new)
1662 goto out_free_suite;
1663
Willy Tarreau2a56c5e2010-03-15 16:13:29 +01001664 args_new[0] = "";
Willy Tarreau95fa4692010-02-01 13:05:50 +01001665 memcpy(args_new + 1, args + arg + 1, (arg_end - arg) * sizeof(*args_new));
1666 args_new[arg_end - arg] = "";
1667 cur_acl = parse_acl(args_new, known_acl);
1668 free(args_new);
1669
1670 if (!cur_acl)
Willy Tarreau16fbe822007-06-17 11:54:31 +02001671 goto out_free_suite;
Willy Tarreau95fa4692010-02-01 13:05:50 +01001672 arg = arg_end;
1673 }
1674 else {
1675 /* search for <word> in the known ACL names. If we do not find
1676 * it, let's look for it in the default ACLs, and if found, add
1677 * it to the list of ACLs of this proxy. This makes it possible
1678 * to override them.
1679 */
1680 cur_acl = find_acl_by_name(word, known_acl);
1681 if (cur_acl == NULL) {
1682 cur_acl = find_acl_default(word, known_acl);
1683 if (cur_acl == NULL)
1684 goto out_free_suite;
1685 }
Willy Tarreau16fbe822007-06-17 11:54:31 +02001686 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001687
1688 cur_term = (struct acl_term *)calloc(1, sizeof(*cur_term));
1689 if (cur_term == NULL)
1690 goto out_free_suite;
1691
1692 cur_term->acl = cur_acl;
1693 cur_term->neg = neg;
Willy Tarreaua9802632008-07-25 19:13:19 +02001694 cond->requires |= cur_acl->requires;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001695
1696 if (!cur_suite) {
1697 cur_suite = (struct acl_term_suite *)calloc(1, sizeof(*cur_suite));
1698 if (cur_term == NULL)
1699 goto out_free_term;
1700 LIST_INIT(&cur_suite->terms);
1701 LIST_ADDQ(&cond->suites, &cur_suite->list);
1702 }
1703 LIST_ADDQ(&cur_suite->terms, &cur_term->list);
Willy Tarreau74b98a82007-06-16 19:35:18 +02001704 neg = 0;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001705 }
1706
1707 return cond;
1708
1709 out_free_term:
1710 free(cur_term);
1711 out_free_suite:
1712 prune_acl_cond(cond);
1713 free(cond);
1714 out_return:
1715 return NULL;
1716}
1717
Willy Tarreau2bbba412010-01-28 16:48:33 +01001718/* Builds an ACL condition starting at the if/unless keyword. The complete
1719 * condition is returned. NULL is returned in case of error or if the first
1720 * word is neither "if" nor "unless". It automatically sets the file name and
1721 * the line number in the condition for better error reporting, and adds the
1722 * ACL requirements to the proxy's acl_requires.
1723 */
1724struct acl_cond *build_acl_cond(const char *file, int line, struct proxy *px, const char **args)
1725{
1726 int pol = ACL_COND_NONE;
1727 struct acl_cond *cond = NULL;
1728
1729 if (!strcmp(*args, "if")) {
1730 pol = ACL_COND_IF;
1731 args++;
1732 }
1733 else if (!strcmp(*args, "unless")) {
1734 pol = ACL_COND_UNLESS;
1735 args++;
1736 }
1737 else
1738 return NULL;
1739
1740 cond = parse_acl_cond(args, &px->acl, pol);
1741 if (!cond)
1742 return NULL;
1743
1744 cond->file = file;
1745 cond->line = line;
1746 px->acl_requires |= cond->requires;
1747
1748 return cond;
1749}
1750
Willy Tarreau11382812008-07-09 16:18:21 +02001751/* Execute condition <cond> and return either ACL_PAT_FAIL, ACL_PAT_MISS or
Willy Tarreaub6866442008-07-14 23:54:42 +02001752 * ACL_PAT_PASS depending on the test results. ACL_PAT_MISS may only be
1753 * returned if <dir> contains ACL_PARTIAL, indicating that incomplete data
1754 * is being examined.
1755 * This function only computes the condition, it does not apply the polarity
1756 * required by IF/UNLESS, it's up to the caller to do this using something like
1757 * this :
Willy Tarreau11382812008-07-09 16:18:21 +02001758 *
1759 * res = acl_pass(res);
Willy Tarreaub6866442008-07-14 23:54:42 +02001760 * if (res == ACL_PAT_MISS)
1761 * return 0;
Willy Tarreau11382812008-07-09 16:18:21 +02001762 * if (cond->pol == ACL_COND_UNLESS)
1763 * res = !res;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001764 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02001765int acl_exec_cond(struct acl_cond *cond, struct proxy *px, struct session *l4, void *l7, int dir)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001766{
1767 __label__ fetch_next;
1768 struct acl_term_suite *suite;
1769 struct acl_term *term;
1770 struct acl_expr *expr;
1771 struct acl *acl;
1772 struct acl_pattern *pattern;
1773 struct acl_test test;
Willy Tarreau11382812008-07-09 16:18:21 +02001774 int acl_res, suite_res, cond_res;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001775
Willy Tarreau11382812008-07-09 16:18:21 +02001776 /* We're doing a logical OR between conditions so we initialize to FAIL.
1777 * The MISS status is propagated down from the suites.
1778 */
Willy Tarreaua84d3742007-05-07 00:36:48 +02001779 cond_res = ACL_PAT_FAIL;
1780 list_for_each_entry(suite, &cond->suites, list) {
Willy Tarreau11382812008-07-09 16:18:21 +02001781 /* Evaluate condition suite <suite>. We stop at the first term
1782 * which returns ACL_PAT_FAIL. The MISS status is still propagated
1783 * in case of uncertainty in the result.
Willy Tarreaua84d3742007-05-07 00:36:48 +02001784 */
1785
1786 /* we're doing a logical AND between terms, so we must set the
1787 * initial value to PASS.
1788 */
1789 suite_res = ACL_PAT_PASS;
1790 list_for_each_entry(term, &suite->terms, list) {
1791 acl = term->acl;
1792
1793 /* FIXME: use cache !
1794 * check acl->cache_idx for this.
1795 */
1796
1797 /* ACL result not cached. Let's scan all the expressions
1798 * and use the first one to match.
1799 */
1800 acl_res = ACL_PAT_FAIL;
1801 list_for_each_entry(expr, &acl->expr, list) {
Willy Tarreaud41f8d82007-06-10 10:06:18 +02001802 /* we need to reset context and flags */
1803 memset(&test, 0, sizeof(test));
Willy Tarreaua84d3742007-05-07 00:36:48 +02001804 fetch_next:
Willy Tarreaub6866442008-07-14 23:54:42 +02001805 if (!expr->kw->fetch(px, l4, l7, dir, expr, &test)) {
1806 /* maybe we could not fetch because of missing data */
1807 if (test.flags & ACL_TEST_F_MAY_CHANGE && dir & ACL_PARTIAL)
1808 acl_res |= ACL_PAT_MISS;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001809 continue;
Willy Tarreaub6866442008-07-14 23:54:42 +02001810 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001811
Willy Tarreaua79534f2008-07-20 10:13:37 +02001812 if (test.flags & ACL_TEST_F_RES_SET) {
1813 if (test.flags & ACL_TEST_F_RES_PASS)
1814 acl_res |= ACL_PAT_PASS;
1815 else
1816 acl_res |= ACL_PAT_FAIL;
1817 }
1818 else {
Willy Tarreau020534d2010-05-16 21:45:45 +02001819 if (!eb_is_empty(&expr->pattern_tree)) {
Willy Tarreauc4262962010-05-10 23:42:40 +02001820 /* a tree is present, let's check what type it is */
1821 if (expr->kw->match == acl_match_str)
1822 acl_res |= acl_lookup_str(&test, expr) ? ACL_PAT_PASS : ACL_PAT_FAIL;
Willy Tarreaub337b532010-05-13 20:03:41 +02001823 else if (expr->kw->match == acl_match_ip)
1824 acl_res |= acl_lookup_ip(&test, expr) ? ACL_PAT_PASS : ACL_PAT_FAIL;
Willy Tarreauc4262962010-05-10 23:42:40 +02001825 }
1826
Willy Tarreaua79534f2008-07-20 10:13:37 +02001827 /* call the match() function for all tests on this value */
1828 list_for_each_entry(pattern, &expr->patterns, list) {
Willy Tarreaua79534f2008-07-20 10:13:37 +02001829 if (acl_res == ACL_PAT_PASS)
1830 break;
Willy Tarreaue56cda92010-05-11 23:25:05 +02001831 acl_res |= expr->kw->match(&test, pattern);
Willy Tarreaua79534f2008-07-20 10:13:37 +02001832 }
Krzysztof Piotr Oledzkid7528e52010-01-29 17:55:53 +01001833
Willy Tarreaue56cda92010-05-11 23:25:05 +02001834 if ((test.flags & ACL_TEST_F_NULL_MATCH) &&
Willy Tarreau020534d2010-05-16 21:45:45 +02001835 LIST_ISEMPTY(&expr->patterns) && eb_is_empty(&expr->pattern_tree))
Krzysztof Piotr Oledzkid7528e52010-01-29 17:55:53 +01001836 acl_res |= expr->kw->match(&test, NULL);
Willy Tarreaua84d3742007-05-07 00:36:48 +02001837 }
1838 /*
Willy Tarreau11382812008-07-09 16:18:21 +02001839 * OK now acl_res holds the result of this expression
1840 * as one of ACL_PAT_FAIL, ACL_PAT_MISS or ACL_PAT_PASS.
Willy Tarreaua84d3742007-05-07 00:36:48 +02001841 *
Willy Tarreau11382812008-07-09 16:18:21 +02001842 * Then if (!MISS) we can cache the result, and put
Willy Tarreaua84d3742007-05-07 00:36:48 +02001843 * (test.flags & ACL_TEST_F_VOLATILE) in the cache flags.
1844 *
1845 * FIXME: implement cache.
1846 *
1847 */
1848
1849 /* now we may have some cleanup to do */
1850 if (test.flags & ACL_TEST_F_MUST_FREE) {
Willy Tarreau664092c2011-12-16 19:11:42 +01001851 free(temp_pattern.data.str.str);
1852 temp_pattern.data.str.len = 0;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001853 }
1854
Willy Tarreau11382812008-07-09 16:18:21 +02001855 /* we're ORing these terms, so a single PASS is enough */
1856 if (acl_res == ACL_PAT_PASS)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001857 break;
1858
Willy Tarreaua84d3742007-05-07 00:36:48 +02001859 if (test.flags & ACL_TEST_F_FETCH_MORE)
1860 goto fetch_next;
Willy Tarreaub6866442008-07-14 23:54:42 +02001861
1862 /* sometimes we know the fetched data is subject to change
1863 * later and give another chance for a new match (eg: request
1864 * size, time, ...)
1865 */
1866 if (test.flags & ACL_TEST_F_MAY_CHANGE && dir & ACL_PARTIAL)
1867 acl_res |= ACL_PAT_MISS;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001868 }
1869 /*
1870 * Here we have the result of an ACL (cached or not).
1871 * ACLs are combined, negated or not, to form conditions.
1872 */
1873
Willy Tarreaua84d3742007-05-07 00:36:48 +02001874 if (term->neg)
Willy Tarreau11382812008-07-09 16:18:21 +02001875 acl_res = acl_neg(acl_res);
Willy Tarreaua84d3742007-05-07 00:36:48 +02001876
1877 suite_res &= acl_res;
Willy Tarreau11382812008-07-09 16:18:21 +02001878
1879 /* we're ANDing these terms, so a single FAIL is enough */
1880 if (suite_res == ACL_PAT_FAIL)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001881 break;
1882 }
1883 cond_res |= suite_res;
Willy Tarreau11382812008-07-09 16:18:21 +02001884
1885 /* we're ORing these terms, so a single PASS is enough */
1886 if (cond_res == ACL_PAT_PASS)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001887 break;
1888 }
Willy Tarreau11382812008-07-09 16:18:21 +02001889 return cond_res;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001890}
1891
1892
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001893/* Reports a pointer to the first ACL used in condition <cond> which requires
1894 * at least one of the USE_FLAGS in <require>. Returns NULL if none matches.
1895 * The construct is almost the same as for acl_exec_cond() since we're walking
1896 * down the ACL tree as well. It is important that the tree is really walked
1897 * through and never cached, because that way, this function can be used as a
1898 * late check.
1899 */
Willy Tarreauf1e98b82010-01-28 17:59:39 +01001900struct acl *cond_find_require(const struct acl_cond *cond, unsigned int require)
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001901{
1902 struct acl_term_suite *suite;
1903 struct acl_term *term;
1904 struct acl *acl;
1905
1906 list_for_each_entry(suite, &cond->suites, list) {
1907 list_for_each_entry(term, &suite->terms, list) {
1908 acl = term->acl;
1909 if (acl->requires & require)
1910 return acl;
1911 }
1912 }
1913 return NULL;
1914}
1915
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001916/*
1917 * Find targets for userlist and groups in acl. Function returns the number
1918 * of errors or OK if everything is fine.
1919 */
1920int
1921acl_find_targets(struct proxy *p)
1922{
1923
1924 struct acl *acl;
1925 struct acl_expr *expr;
1926 struct acl_pattern *pattern;
1927 struct userlist *ul;
1928 int cfgerr = 0;
1929
1930 list_for_each_entry(acl, &p->acl, list) {
1931 list_for_each_entry(expr, &acl->expr, list) {
Hervé COMMOWICKdaa824e2011-08-05 12:09:44 +02001932 if (strcmp(expr->kw->kw, "srv_is_up") == 0 ||
1933 strcmp(expr->kw->kw, "srv_conn") == 0) {
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001934 struct proxy *px;
1935 struct server *srv;
1936 char *pname, *sname;
1937
1938 if (!expr->arg.str || !*expr->arg.str) {
1939 Alert("proxy %s: acl %s %s(): missing server name.\n",
1940 p->id, acl->name, expr->kw->kw);
1941 cfgerr++;
1942 continue;
1943 }
1944
1945 pname = expr->arg.str;
1946 sname = strrchr(pname, '/');
1947
1948 if (sname)
1949 *sname++ = '\0';
1950 else {
1951 sname = pname;
1952 pname = NULL;
1953 }
1954
1955 px = p;
1956 if (pname) {
1957 px = findproxy(pname, PR_CAP_BE);
1958 if (!px) {
1959 Alert("proxy %s: acl %s %s(): unable to find proxy '%s'.\n",
1960 p->id, acl->name, expr->kw->kw, pname);
1961 cfgerr++;
1962 continue;
1963 }
1964 }
1965
1966 srv = findserver(px, sname);
1967 if (!srv) {
1968 Alert("proxy %s: acl %s %s(): unable to find server '%s'.\n",
1969 p->id, acl->name, expr->kw->kw, sname);
1970 cfgerr++;
1971 continue;
1972 }
1973
1974 free(expr->arg.str);
1975 expr->arg_len = 0;
1976 expr->arg.srv = srv;
1977 continue;
1978 }
1979
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001980 if (strstr(expr->kw->kw, "http_auth") == expr->kw->kw) {
1981
1982 if (!expr->arg.str || !*expr->arg.str) {
1983 Alert("proxy %s: acl %s %s(): missing userlist name.\n",
1984 p->id, acl->name, expr->kw->kw);
1985 cfgerr++;
1986 continue;
1987 }
1988
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01001989 if (p->uri_auth && p->uri_auth->userlist &&
1990 !strcmp(p->uri_auth->userlist->name, expr->arg.str))
1991 ul = p->uri_auth->userlist;
1992 else
1993 ul = auth_find_userlist(expr->arg.str);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001994
1995 if (!ul) {
1996 Alert("proxy %s: acl %s %s(%s): unable to find userlist.\n",
1997 p->id, acl->name, expr->kw->kw, expr->arg.str);
1998 cfgerr++;
1999 continue;
2000 }
2001
2002 expr->arg_len = 0;
2003 expr->arg.ul = ul;
2004 }
2005
2006
2007 if (!strcmp(expr->kw->kw, "http_auth_group")) {
2008
2009 if (LIST_ISEMPTY(&expr->patterns)) {
2010 Alert("proxy %s: acl %s %s(): no groups specified.\n",
2011 p->id, acl->name, expr->kw->kw);
2012 cfgerr++;
2013 continue;
2014 }
2015
2016 list_for_each_entry(pattern, &expr->patterns, list) {
2017 pattern->val.group_mask = auth_resolve_groups(expr->arg.ul, pattern->ptr.str);
2018
2019 free(pattern->ptr.str);
2020 pattern->ptr.str = NULL;
2021 pattern->len = 0;
2022
2023 if (!pattern->val.group_mask) {
2024 Alert("proxy %s: acl %s %s(): invalid group(s).\n",
2025 p->id, acl->name, expr->kw->kw);
2026 cfgerr++;
2027 continue;
2028 }
2029 }
2030 }
2031 }
2032 }
2033
2034 return cfgerr;
2035}
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02002036
Willy Tarreaua84d3742007-05-07 00:36:48 +02002037/************************************************************************/
2038/* All supported keywords must be declared here. */
2039/************************************************************************/
2040
2041/* Note: must not be declared <const> as its list will be overwritten */
2042static struct acl_kw_list acl_kws = {{ },{
Emeric Brun38e71762010-09-23 17:59:18 +02002043 { "always_true", acl_parse_nothing, acl_fetch_true, acl_match_nothing, ACL_USE_NOTHING },
2044 { "always_false", acl_parse_nothing, acl_fetch_false, acl_match_nothing, ACL_USE_NOTHING },
2045 { "wait_end", acl_parse_nothing, acl_fetch_wait_end, acl_match_nothing, ACL_USE_NOTHING },
2046 { "req_len", acl_parse_int, acl_fetch_req_len, acl_match_int, ACL_USE_L6REQ_VOLATILE },
2047 { "req_ssl_hello_type", acl_parse_int, acl_fetch_ssl_hello_type, acl_match_int, ACL_USE_L6REQ_VOLATILE },
2048 { "rep_ssl_hello_type", acl_parse_int, acl_fetch_ssl_hello_type, acl_match_int, ACL_USE_L6RTR_VOLATILE },
2049 { "req_ssl_ver", acl_parse_dotted_ver, acl_fetch_req_ssl_ver, acl_match_int, ACL_USE_L6REQ_VOLATILE },
2050 { "req_rdp_cookie", acl_parse_str, acl_fetch_rdp_cookie, acl_match_str, ACL_USE_L6REQ_VOLATILE|ACL_MAY_LOOKUP },
2051 { "req_rdp_cookie_cnt", acl_parse_int, acl_fetch_rdp_cookie_cnt, acl_match_int, ACL_USE_L6REQ_VOLATILE },
Willy Tarreaub6672b52011-12-12 17:23:41 +01002052 { "req_ssl_sni", acl_parse_str, acl_fetch_ssl_hello_sni, acl_match_str, ACL_USE_L6REQ_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreaua84d3742007-05-07 00:36:48 +02002053#if 0
2054 { "time", acl_parse_time, acl_fetch_time, acl_match_time },
2055#endif
2056 { NULL, NULL, NULL, NULL }
2057}};
2058
2059
2060__attribute__((constructor))
2061static void __acl_init(void)
2062{
2063 acl_register_keywords(&acl_kws);
2064}
2065
2066
2067/*
2068 * Local variables:
2069 * c-indent-level: 8
2070 * c-basic-offset: 8
2071 * End:
2072 */