blob: dc9925d519dc00e0dfe9eed3a84c486079606f15 [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 Tarreaud28c3532012-04-19 19:28:33 +020030#include <proto/stick_table.h>
Willy Tarreaua84d3742007-05-07 00:36:48 +020031
Willy Tarreauc4262962010-05-10 23:42:40 +020032#include <ebsttree.h>
33
Willy Tarreaua9802632008-07-25 19:13:19 +020034/* The capabilities of filtering hooks describe the type of information
35 * available to each of them.
36 */
37const unsigned int filt_cap[] = {
38 [ACL_HOOK_REQ_FE_TCP] = ACL_USE_TCP4_ANY|ACL_USE_TCP6_ANY|ACL_USE_TCP_ANY,
Willy Tarreau06457872010-05-23 12:24:38 +020039 [ACL_HOOK_REQ_FE_TCP_CONTENT] = ACL_USE_TCP4_ANY|ACL_USE_TCP6_ANY|ACL_USE_TCP_ANY|ACL_USE_L6REQ_ANY,
40 [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,
41 [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,
42 [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,
43 [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,
44 [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,
45 [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,
46 [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 +020047
Willy Tarreau06457872010-05-23 12:24:38 +020048 [ACL_HOOK_RTR_BE_TCP_CONTENT] = ACL_USE_REQ_PERMANENT|ACL_USE_REQ_CACHEABLE|ACL_USE_L6RTR_ANY,
49 [ACL_HOOK_RTR_BE_HTTP_IN] = ACL_USE_REQ_PERMANENT|ACL_USE_REQ_CACHEABLE|ACL_USE_L6RTR_ANY|ACL_USE_L7RTR_ANY,
50 [ACL_HOOK_RTR_FE_TCP_CONTENT] = ACL_USE_REQ_PERMANENT|ACL_USE_REQ_CACHEABLE|ACL_USE_L6RTR_ANY|ACL_USE_L7RTR_ANY,
51 [ACL_HOOK_RTR_FE_HTTP_IN] = ACL_USE_REQ_PERMANENT|ACL_USE_REQ_CACHEABLE|ACL_USE_L6RTR_ANY|ACL_USE_L7RTR_ANY,
52 [ACL_HOOK_RTR_BE_HTTP_OUT] = ACL_USE_REQ_PERMANENT|ACL_USE_REQ_CACHEABLE|ACL_USE_L6RTR_ANY|ACL_USE_L7RTR_ANY,
53 [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 +020054};
55
Willy Tarreaua84d3742007-05-07 00:36:48 +020056/* List head of all known ACL keywords */
57static struct acl_kw_list acl_keywords = {
58 .list = LIST_HEAD_INIT(acl_keywords.list)
59};
60
61
Willy Tarreaua5909832007-06-17 20:40:25 +020062/*
63 * These functions are only used for debugging complex configurations.
Willy Tarreaua84d3742007-05-07 00:36:48 +020064 */
Willy Tarreaua5909832007-06-17 20:40:25 +020065
Willy Tarreau58393e12008-07-20 10:39:22 +020066/* force TRUE to be returned at the fetch level */
Willy Tarreaua5909832007-06-17 20:40:25 +020067static int
Willy Tarreau58393e12008-07-20 10:39:22 +020068acl_fetch_true(struct proxy *px, struct session *l4, void *l7, int dir,
69 struct acl_expr *expr, struct acl_test *test)
Willy Tarreaua5909832007-06-17 20:40:25 +020070{
Willy Tarreau58393e12008-07-20 10:39:22 +020071 test->flags |= ACL_TEST_F_SET_RES_PASS;
Willy Tarreaua5909832007-06-17 20:40:25 +020072 return 1;
73}
74
Willy Tarreaub6fb4202008-07-20 11:18:28 +020075/* wait for more data as long as possible, then return TRUE. This should be
76 * used with content inspection.
77 */
78static int
79acl_fetch_wait_end(struct proxy *px, struct session *l4, void *l7, int dir,
80 struct acl_expr *expr, struct acl_test *test)
81{
82 if (dir & ACL_PARTIAL) {
83 test->flags |= ACL_TEST_F_MAY_CHANGE;
84 return 0;
85 }
86 test->flags |= ACL_TEST_F_SET_RES_PASS;
87 return 1;
88}
89
Willy Tarreau58393e12008-07-20 10:39:22 +020090/* force FALSE to be returned at the fetch level */
Willy Tarreaua5909832007-06-17 20:40:25 +020091static int
Willy Tarreau58393e12008-07-20 10:39:22 +020092acl_fetch_false(struct proxy *px, struct session *l4, void *l7, int dir,
93 struct acl_expr *expr, struct acl_test *test)
Willy Tarreaua84d3742007-05-07 00:36:48 +020094{
Willy Tarreau58393e12008-07-20 10:39:22 +020095 test->flags |= ACL_TEST_F_SET_RES_FAIL;
Willy Tarreaua84d3742007-05-07 00:36:48 +020096 return 1;
97}
98
Willy Tarreau44b90cc2010-05-24 20:27:29 +020099/* return the number of bytes in the request buffer */
100static int
101acl_fetch_req_len(struct proxy *px, struct session *l4, void *l7, int dir,
102 struct acl_expr *expr, struct acl_test *test)
103{
104 if (!l4 || !l4->req)
105 return 0;
106
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100107 temp_pattern.data.integer = l4->req->i;
Willy Tarreau44b90cc2010-05-24 20:27:29 +0200108 test->flags = ACL_TEST_F_VOLATILE | ACL_TEST_F_MAY_CHANGE;
109 return 1;
110}
111
Emeric Brun38e71762010-09-23 17:59:18 +0200112
113static int
114acl_fetch_ssl_hello_type(struct proxy *px, struct session *l4, void *l7, int dir,
115 struct acl_expr *expr, struct acl_test *test)
116{
117 int hs_len;
118 int hs_type, bleft;
119 struct buffer *b;
120 const unsigned char *data;
121
122 if (!l4)
123 goto not_ssl_hello;
124
125 b = ((dir & ACL_DIR_MASK) == ACL_DIR_RTR) ? l4->rep : l4->req;
126
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100127 bleft = b->i;
Willy Tarreau89fa7062012-03-02 16:13:16 +0100128 data = (const unsigned char *)b->p;
Emeric Brun38e71762010-09-23 17:59:18 +0200129
130 if (!bleft)
131 goto too_short;
132
133 if ((*data >= 0x14 && *data <= 0x17) || (*data == 0xFF)) {
134 /* SSLv3 header format */
135 if (bleft < 9)
136 goto too_short;
137
138 /* ssl version 3 */
139 if ((data[1] << 16) + data[2] < 0x00030000)
140 goto not_ssl_hello;
141
142 /* ssl message len must present handshake type and len */
143 if ((data[3] << 8) + data[4] < 4)
144 goto not_ssl_hello;
145
146 /* format introduced with SSLv3 */
147
148 hs_type = (int)data[5];
149 hs_len = ( data[6] << 16 ) + ( data[7] << 8 ) + data[8];
150
151 /* not a full handshake */
152 if (bleft < (9 + hs_len))
153 goto too_short;
154
155 }
156 else {
157 goto not_ssl_hello;
158 }
159
Willy Tarreaua5e37562011-12-16 17:06:15 +0100160 temp_pattern.data.integer = hs_type;
Emeric Brun38e71762010-09-23 17:59:18 +0200161 test->flags = ACL_TEST_F_VOLATILE;
162
163 return 1;
164
165 too_short:
166 test->flags = ACL_TEST_F_MAY_CHANGE;
167
168 not_ssl_hello:
169
170 return 0;
171}
172
Willy Tarreau44b90cc2010-05-24 20:27:29 +0200173/* Return the version of the SSL protocol in the request. It supports both
174 * SSLv3 (TLSv1) header format for any message, and SSLv2 header format for
175 * the hello message. The SSLv3 format is described in RFC 2246 p49, and the
176 * SSLv2 format is described here, and completed p67 of RFC 2246 :
177 * http://wp.netscape.com/eng/security/SSL_2.html
178 *
179 * Note: this decoder only works with non-wrapping data.
180 */
181static int
182acl_fetch_req_ssl_ver(struct proxy *px, struct session *l4, void *l7, int dir,
183 struct acl_expr *expr, struct acl_test *test)
184{
185 int version, bleft, msg_len;
186 const unsigned char *data;
187
188 if (!l4 || !l4->req)
189 return 0;
190
191 msg_len = 0;
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100192 bleft = l4->req->i;
Willy Tarreau44b90cc2010-05-24 20:27:29 +0200193 if (!bleft)
194 goto too_short;
195
Willy Tarreau89fa7062012-03-02 16:13:16 +0100196 data = (const unsigned char *)l4->req->p;
Willy Tarreau44b90cc2010-05-24 20:27:29 +0200197 if ((*data >= 0x14 && *data <= 0x17) || (*data == 0xFF)) {
198 /* SSLv3 header format */
199 if (bleft < 5)
200 goto too_short;
201
202 version = (data[1] << 16) + data[2]; /* version: major, minor */
203 msg_len = (data[3] << 8) + data[4]; /* record length */
204
205 /* format introduced with SSLv3 */
206 if (version < 0x00030000)
207 goto not_ssl;
208
209 /* message length between 1 and 2^14 + 2048 */
210 if (msg_len < 1 || msg_len > ((1<<14) + 2048))
211 goto not_ssl;
212
213 bleft -= 5; data += 5;
214 } else {
215 /* SSLv2 header format, only supported for hello (msg type 1) */
216 int rlen, plen, cilen, silen, chlen;
217
218 if (*data & 0x80) {
219 if (bleft < 3)
220 goto too_short;
221 /* short header format : 15 bits for length */
222 rlen = ((data[0] & 0x7F) << 8) | data[1];
223 plen = 0;
224 bleft -= 2; data += 2;
225 } else {
226 if (bleft < 4)
227 goto too_short;
228 /* long header format : 14 bits for length + pad length */
229 rlen = ((data[0] & 0x3F) << 8) | data[1];
230 plen = data[2];
231 bleft -= 3; data += 2;
232 }
233
234 if (*data != 0x01)
235 goto not_ssl;
236 bleft--; data++;
237
238 if (bleft < 8)
239 goto too_short;
240 version = (data[0] << 16) + data[1]; /* version: major, minor */
241 cilen = (data[2] << 8) + data[3]; /* cipher len, multiple of 3 */
242 silen = (data[4] << 8) + data[5]; /* session_id_len: 0 or 16 */
243 chlen = (data[6] << 8) + data[7]; /* 16<=challenge length<=32 */
244
245 bleft -= 8; data += 8;
246 if (cilen % 3 != 0)
247 goto not_ssl;
248 if (silen && silen != 16)
249 goto not_ssl;
250 if (chlen < 16 || chlen > 32)
251 goto not_ssl;
252 if (rlen != 9 + cilen + silen + chlen)
253 goto not_ssl;
254
255 /* focus on the remaining data length */
256 msg_len = cilen + silen + chlen + plen;
257 }
258 /* We could recursively check that the buffer ends exactly on an SSL
259 * fragment boundary and that a possible next segment is still SSL,
260 * but that's a bit pointless. However, we could still check that
261 * all the part of the request which fits in a buffer is already
262 * there.
263 */
Willy Tarreau89fa7062012-03-02 16:13:16 +0100264 if (msg_len > buffer_max_len(l4->req) + l4->req->data - l4->req->p)
265 msg_len = buffer_max_len(l4->req) + l4->req->data - l4->req->p;
Willy Tarreau44b90cc2010-05-24 20:27:29 +0200266
267 if (bleft < msg_len)
268 goto too_short;
269
270 /* OK that's enough. We have at least the whole message, and we have
271 * the protocol version.
272 */
Willy Tarreaua5e37562011-12-16 17:06:15 +0100273 temp_pattern.data.integer = version;
Willy Tarreau44b90cc2010-05-24 20:27:29 +0200274 test->flags = ACL_TEST_F_VOLATILE;
275 return 1;
276
277 too_short:
278 test->flags = ACL_TEST_F_MAY_CHANGE;
279 not_ssl:
280 return 0;
281}
282
Willy Tarreaub6672b52011-12-12 17:23:41 +0100283/* Try to extract the Server Name Indication that may be presented in a TLS
284 * client hello handshake message. The format of the message is the following
285 * (cf RFC5246 + RFC6066) :
286 * TLS frame :
287 * - uint8 type = 0x16 (Handshake)
288 * - uint16 version >= 0x0301 (TLSv1)
289 * - uint16 length (frame length)
290 * - TLS handshake :
291 * - uint8 msg_type = 0x01 (ClientHello)
292 * - uint24 length (handshake message length)
293 * - ClientHello :
294 * - uint16 client_version >= 0x0301 (TLSv1)
Willy Tarreaud017f112012-04-09 09:24:11 +0200295 * - uint8 Random[32] (4 first ones are timestamp)
Willy Tarreaub6672b52011-12-12 17:23:41 +0100296 * - SessionID :
297 * - uint8 session_id_len (0..32) (SessionID len in bytes)
298 * - uint8 session_id[session_id_len]
299 * - CipherSuite :
300 * - uint16 cipher_len >= 2 (Cipher length in bytes)
301 * - uint16 ciphers[cipher_len/2]
302 * - CompressionMethod :
303 * - uint8 compression_len >= 1 (# of supported methods)
304 * - uint8 compression_methods[compression_len]
305 * - optional client_extension_len (in bytes)
306 * - optional sequence of ClientHelloExtensions (as many bytes as above):
307 * - uint16 extension_type = 0 for server_name
308 * - uint16 extension_len
309 * - opaque extension_data[extension_len]
310 * - uint16 server_name_list_len (# of bytes here)
311 * - opaque server_names[server_name_list_len bytes]
312 * - uint8 name_type = 0 for host_name
313 * - uint16 name_len
314 * - opaque hostname[name_len bytes]
315 */
316static int
317acl_fetch_ssl_hello_sni(struct proxy *px, struct session *l4, void *l7, int dir,
318 struct acl_expr *expr, struct acl_test *test)
319{
320 int hs_len, ext_len, bleft;
321 struct buffer *b;
322 unsigned char *data;
323
324 if (!l4)
325 goto not_ssl_hello;
326
327 b = ((dir & ACL_DIR_MASK) == ACL_DIR_RTR) ? l4->rep : l4->req;
328
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100329 bleft = b->i;
Willy Tarreau89fa7062012-03-02 16:13:16 +0100330 data = (unsigned char *)b->p;
Willy Tarreaub6672b52011-12-12 17:23:41 +0100331
332 /* Check for SSL/TLS Handshake */
333 if (!bleft)
334 goto too_short;
335 if (*data != 0x16)
336 goto not_ssl_hello;
337
338 /* Check for TLSv1 or later (SSL version >= 3.1) */
339 if (bleft < 3)
340 goto too_short;
341 if (data[1] < 0x03 || data[2] < 0x01)
342 goto not_ssl_hello;
343
344 if (bleft < 5)
345 goto too_short;
346 hs_len = (data[3] << 8) + data[4];
347 if (hs_len < 1 + 3 + 2 + 32 + 1 + 2 + 2 + 1 + 1 + 2 + 2)
348 goto not_ssl_hello; /* too short to have an extension */
349
350 data += 5; /* enter TLS handshake */
351 bleft -= 5;
352
353 /* Check for a complete client hello starting at <data> */
354 if (bleft < 1)
355 goto too_short;
356 if (data[0] != 0x01) /* msg_type = Client Hello */
357 goto not_ssl_hello;
358
359 /* Check the Hello's length */
360 if (bleft < 4)
361 goto too_short;
362 hs_len = (data[1] << 16) + (data[2] << 8) + data[3];
363 if (hs_len < 2 + 32 + 1 + 2 + 2 + 1 + 1 + 2 + 2)
364 goto not_ssl_hello; /* too short to have an extension */
365
366 /* We want the full handshake here */
367 if (bleft < hs_len)
368 goto too_short;
369
370 data += 4;
371 /* Start of the ClientHello message */
372 if (data[0] < 0x03 || data[1] < 0x01) /* TLSv1 minimum */
373 goto not_ssl_hello;
374
Willy Tarreaud017f112012-04-09 09:24:11 +0200375 ext_len = data[34]; /* session_id_len */
Willy Tarreaub6672b52011-12-12 17:23:41 +0100376 if (ext_len > 32 || ext_len > (hs_len - 35)) /* check for correct session_id len */
377 goto not_ssl_hello;
378
379 /* Jump to cipher suite */
380 hs_len -= 35 + ext_len;
381 data += 35 + ext_len;
382
383 if (hs_len < 4 || /* minimum one cipher */
384 (ext_len = (data[0] << 8) + data[1]) < 2 || /* minimum 2 bytes for a cipher */
385 ext_len > hs_len)
386 goto not_ssl_hello;
387
388 /* Jump to the compression methods */
389 hs_len -= 2 + ext_len;
390 data += 2 + ext_len;
391
392 if (hs_len < 2 || /* minimum one compression method */
393 data[0] < 1 || data[0] > hs_len) /* minimum 1 bytes for a method */
394 goto not_ssl_hello;
395
396 /* Jump to the extensions */
397 hs_len -= 1 + data[0];
398 data += 1 + data[0];
399
400 if (hs_len < 2 || /* minimum one extension list length */
401 (ext_len = (data[0] << 8) + data[1]) > hs_len - 2) /* list too long */
402 goto not_ssl_hello;
403
404 hs_len = ext_len; /* limit ourselves to the extension length */
405 data += 2;
406
407 while (hs_len >= 4) {
408 int ext_type, name_type, srv_len, name_len;
409
410 ext_type = (data[0] << 8) + data[1];
411 ext_len = (data[2] << 8) + data[3];
412
413 if (ext_len > hs_len - 4) /* Extension too long */
414 goto not_ssl_hello;
415
416 if (ext_type == 0) { /* Server name */
417 if (ext_len < 2) /* need one list length */
418 goto not_ssl_hello;
419
420 srv_len = (data[4] << 8) + data[5];
421 if (srv_len < 4 || srv_len > hs_len - 6)
422 goto not_ssl_hello; /* at least 4 bytes per server name */
423
424 name_type = data[6];
425 name_len = (data[7] << 8) + data[8];
426
427 if (name_type == 0) { /* hostname */
Willy Tarreau62e7c712012-03-10 09:05:30 +0100428 temp_pattern.data.str.str = (char *)data + 9;
Willy Tarreau664092c2011-12-16 19:11:42 +0100429 temp_pattern.data.str.len = name_len;
Willy Tarreaub6672b52011-12-12 17:23:41 +0100430 test->flags = ACL_TEST_F_VOLATILE;
Willy Tarreaub6672b52011-12-12 17:23:41 +0100431 return 1;
432 }
433 }
434
435 hs_len -= 4 + ext_len;
436 data += 4 + ext_len;
437 }
438 /* server name not found */
439 goto not_ssl_hello;
440
441 too_short:
442 test->flags = ACL_TEST_F_MAY_CHANGE;
443
444 not_ssl_hello:
445
446 return 0;
447}
448
Willy Tarreau44b90cc2010-05-24 20:27:29 +0200449/* Fetch the RDP cookie identified in the expression.
450 * Note: this decoder only works with non-wrapping data.
Willy Tarreau34db1082012-04-19 17:16:54 +0200451 * Accepts either 0 or 1 argument. Argument is a string (cookie name), other
452 * types will lead to undefined behaviour.
Willy Tarreau44b90cc2010-05-24 20:27:29 +0200453 */
454int
455acl_fetch_rdp_cookie(struct proxy *px, struct session *l4, void *l7, int dir,
456 struct acl_expr *expr, struct acl_test *test)
457{
458 int bleft;
459 const unsigned char *data;
460
461 if (!l4 || !l4->req)
462 return 0;
463
464 test->flags = 0;
465
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100466 bleft = l4->req->i;
Willy Tarreau44b90cc2010-05-24 20:27:29 +0200467 if (bleft <= 11)
468 goto too_short;
469
Willy Tarreau89fa7062012-03-02 16:13:16 +0100470 data = (const unsigned char *)l4->req->p + 11;
Willy Tarreau44b90cc2010-05-24 20:27:29 +0200471 bleft -= 11;
472
473 if (bleft <= 7)
474 goto too_short;
475
476 if (strncasecmp((const char *)data, "Cookie:", 7) != 0)
477 goto not_cookie;
478
479 data += 7;
480 bleft -= 7;
481
482 while (bleft > 0 && *data == ' ') {
483 data++;
484 bleft--;
485 }
486
Willy Tarreau34db1082012-04-19 17:16:54 +0200487 if (expr->args) {
Willy Tarreau44b90cc2010-05-24 20:27:29 +0200488
Willy Tarreau34db1082012-04-19 17:16:54 +0200489 if (bleft <= expr->args->data.str.len)
Willy Tarreau44b90cc2010-05-24 20:27:29 +0200490 goto too_short;
491
Willy Tarreau34db1082012-04-19 17:16:54 +0200492 if ((data[expr->args->data.str.len] != '=') ||
493 strncasecmp(expr->args->data.str.str, (const char *)data, expr->args->data.str.len) != 0)
Willy Tarreau44b90cc2010-05-24 20:27:29 +0200494 goto not_cookie;
495
Willy Tarreau34db1082012-04-19 17:16:54 +0200496 data += expr->args->data.str.len + 1;
497 bleft -= expr->args->data.str.len + 1;
Willy Tarreau44b90cc2010-05-24 20:27:29 +0200498 } else {
499 while (bleft > 0 && *data != '=') {
500 if (*data == '\r' || *data == '\n')
501 goto not_cookie;
502 data++;
503 bleft--;
504 }
505
506 if (bleft < 1)
507 goto too_short;
508
509 if (*data != '=')
510 goto not_cookie;
511
512 data++;
513 bleft--;
514 }
515
516 /* data points to cookie value */
Willy Tarreau664092c2011-12-16 19:11:42 +0100517 temp_pattern.data.str.str = (char *)data;
518 temp_pattern.data.str.len = 0;
Willy Tarreau44b90cc2010-05-24 20:27:29 +0200519
520 while (bleft > 0 && *data != '\r') {
521 data++;
522 bleft--;
523 }
524
525 if (bleft < 2)
526 goto too_short;
527
528 if (data[0] != '\r' || data[1] != '\n')
529 goto not_cookie;
530
Willy Tarreau664092c2011-12-16 19:11:42 +0100531 temp_pattern.data.str.len = (char *)data - temp_pattern.data.str.str;
Willy Tarreau44b90cc2010-05-24 20:27:29 +0200532 test->flags = ACL_TEST_F_VOLATILE;
533 return 1;
534
535 too_short:
536 test->flags = ACL_TEST_F_MAY_CHANGE;
537 not_cookie:
538 return 0;
539}
540
541static int
542acl_fetch_rdp_cookie_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
543 struct acl_expr *expr, struct acl_test *test)
544{
545 int ret;
546
547 ret = acl_fetch_rdp_cookie(px, l4, l7, dir, expr, test);
548
Willy Tarreau664092c2011-12-16 19:11:42 +0100549 temp_pattern.data.str.str = NULL;
550 temp_pattern.data.str.len = 0;
Willy Tarreau44b90cc2010-05-24 20:27:29 +0200551
552 if (test->flags & ACL_TEST_F_MAY_CHANGE)
553 return 0;
554
555 test->flags = ACL_TEST_F_VOLATILE;
Willy Tarreaua5e37562011-12-16 17:06:15 +0100556 temp_pattern.data.integer = ret;
Willy Tarreau44b90cc2010-05-24 20:27:29 +0200557
558 return 1;
559}
560
Willy Tarreau58393e12008-07-20 10:39:22 +0200561
562/*
563 * These functions are exported and may be used by any other component.
564 */
565
566/* ignore the current line */
567int acl_parse_nothing(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreaua5909832007-06-17 20:40:25 +0200568{
Willy Tarreau58393e12008-07-20 10:39:22 +0200569 return 1;
570}
571
572/* always fake a data retrieval */
573int acl_fetch_nothing(struct proxy *px, struct session *l4, void *l7, int dir,
574 struct acl_expr *expr, struct acl_test *test)
575{
576 return 1;
Willy Tarreaua5909832007-06-17 20:40:25 +0200577}
578
579/* always return false */
Willy Tarreau58393e12008-07-20 10:39:22 +0200580int acl_match_nothing(struct acl_test *test, struct acl_pattern *pattern)
Willy Tarreaua5909832007-06-17 20:40:25 +0200581{
Willy Tarreau11382812008-07-09 16:18:21 +0200582 return ACL_PAT_FAIL;
Willy Tarreaua5909832007-06-17 20:40:25 +0200583}
584
585
Willy Tarreaua84d3742007-05-07 00:36:48 +0200586/* NB: For two strings to be identical, it is required that their lengths match */
587int acl_match_str(struct acl_test *test, struct acl_pattern *pattern)
588{
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200589 int icase;
590
Willy Tarreau664092c2011-12-16 19:11:42 +0100591 if (pattern->len != temp_pattern.data.str.len)
Willy Tarreau11382812008-07-09 16:18:21 +0200592 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200593
594 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
Willy Tarreau664092c2011-12-16 19:11:42 +0100595 if ((icase && strncasecmp(pattern->ptr.str, temp_pattern.data.str.str, temp_pattern.data.str.len) == 0) ||
596 (!icase && strncmp(pattern->ptr.str, temp_pattern.data.str.str, temp_pattern.data.str.len) == 0))
Willy Tarreau11382812008-07-09 16:18:21 +0200597 return ACL_PAT_PASS;
598 return ACL_PAT_FAIL;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200599}
600
Willy Tarreauc4262962010-05-10 23:42:40 +0200601/* Lookup a string in the expression's pattern tree. The node is returned if it
602 * exists, otherwise NULL.
603 */
604void *acl_lookup_str(struct acl_test *test, struct acl_expr *expr)
605{
606 /* data are stored in a tree */
607 struct ebmb_node *node;
608 char prev;
609
610 /* we may have to force a trailing zero on the test pattern */
Willy Tarreau664092c2011-12-16 19:11:42 +0100611 prev = temp_pattern.data.str.str[temp_pattern.data.str.len];
Willy Tarreauc4262962010-05-10 23:42:40 +0200612 if (prev)
Willy Tarreau664092c2011-12-16 19:11:42 +0100613 temp_pattern.data.str.str[temp_pattern.data.str.len] = '\0';
614 node = ebst_lookup(&expr->pattern_tree, temp_pattern.data.str.str);
Willy Tarreauc4262962010-05-10 23:42:40 +0200615 if (prev)
Willy Tarreau664092c2011-12-16 19:11:42 +0100616 temp_pattern.data.str.str[temp_pattern.data.str.len] = prev;
Willy Tarreauc4262962010-05-10 23:42:40 +0200617 return node;
618}
619
Willy Tarreauf3d25982007-05-08 22:45:09 +0200620/* Executes a regex. It needs to change the data. If it is marked READ_ONLY
621 * then it will be allocated and duplicated in place so that others may use
622 * it later on. Note that this is embarrassing because we always try to avoid
623 * allocating memory at run time.
624 */
625int acl_match_reg(struct acl_test *test, struct acl_pattern *pattern)
626{
627 char old_char;
628 int ret;
629
630 if (unlikely(test->flags & ACL_TEST_F_READ_ONLY)) {
631 char *new_str;
632
Willy Tarreau664092c2011-12-16 19:11:42 +0100633 new_str = calloc(1, temp_pattern.data.str.len + 1);
Willy Tarreauf3d25982007-05-08 22:45:09 +0200634 if (!new_str)
Willy Tarreau11382812008-07-09 16:18:21 +0200635 return ACL_PAT_FAIL;
Willy Tarreauf3d25982007-05-08 22:45:09 +0200636
Willy Tarreau664092c2011-12-16 19:11:42 +0100637 memcpy(new_str, temp_pattern.data.str.str, temp_pattern.data.str.len);
638 new_str[temp_pattern.data.str.len] = 0;
Willy Tarreauf3d25982007-05-08 22:45:09 +0200639 if (test->flags & ACL_TEST_F_MUST_FREE)
Willy Tarreau664092c2011-12-16 19:11:42 +0100640 free(temp_pattern.data.str.str);
641 temp_pattern.data.str.str = new_str;
Willy Tarreauf3d25982007-05-08 22:45:09 +0200642 test->flags |= ACL_TEST_F_MUST_FREE;
643 test->flags &= ~ACL_TEST_F_READ_ONLY;
644 }
645
Willy Tarreau664092c2011-12-16 19:11:42 +0100646 old_char = temp_pattern.data.str.str[temp_pattern.data.str.len];
647 temp_pattern.data.str.str[temp_pattern.data.str.len] = 0;
Willy Tarreauf3d25982007-05-08 22:45:09 +0200648
Willy Tarreau664092c2011-12-16 19:11:42 +0100649 if (regexec(pattern->ptr.reg, temp_pattern.data.str.str, 0, NULL, 0) == 0)
Willy Tarreau11382812008-07-09 16:18:21 +0200650 ret = ACL_PAT_PASS;
Willy Tarreauf3d25982007-05-08 22:45:09 +0200651 else
Willy Tarreau11382812008-07-09 16:18:21 +0200652 ret = ACL_PAT_FAIL;
Willy Tarreauf3d25982007-05-08 22:45:09 +0200653
Willy Tarreau664092c2011-12-16 19:11:42 +0100654 temp_pattern.data.str.str[temp_pattern.data.str.len] = old_char;
Willy Tarreauf3d25982007-05-08 22:45:09 +0200655 return ret;
656}
657
Willy Tarreaua84d3742007-05-07 00:36:48 +0200658/* Checks that the pattern matches the beginning of the tested string. */
659int acl_match_beg(struct acl_test *test, struct acl_pattern *pattern)
660{
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200661 int icase;
662
Willy Tarreau664092c2011-12-16 19:11:42 +0100663 if (pattern->len > temp_pattern.data.str.len)
Willy Tarreau11382812008-07-09 16:18:21 +0200664 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200665
666 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
Willy Tarreau664092c2011-12-16 19:11:42 +0100667 if ((icase && strncasecmp(pattern->ptr.str, temp_pattern.data.str.str, pattern->len) != 0) ||
668 (!icase && strncmp(pattern->ptr.str, temp_pattern.data.str.str, pattern->len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +0200669 return ACL_PAT_FAIL;
670 return ACL_PAT_PASS;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200671}
672
673/* Checks that the pattern matches the end of the tested string. */
674int acl_match_end(struct acl_test *test, struct acl_pattern *pattern)
675{
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200676 int icase;
677
Willy Tarreau664092c2011-12-16 19:11:42 +0100678 if (pattern->len > temp_pattern.data.str.len)
Willy Tarreau11382812008-07-09 16:18:21 +0200679 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200680 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
Willy Tarreau664092c2011-12-16 19:11:42 +0100681 if ((icase && strncasecmp(pattern->ptr.str, temp_pattern.data.str.str + temp_pattern.data.str.len - pattern->len, pattern->len) != 0) ||
682 (!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 +0200683 return ACL_PAT_FAIL;
684 return ACL_PAT_PASS;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200685}
686
687/* Checks that the pattern is included inside the tested string.
688 * NB: Suboptimal, should be rewritten using a Boyer-Moore method.
689 */
690int acl_match_sub(struct acl_test *test, struct acl_pattern *pattern)
691{
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200692 int icase;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200693 char *end;
694 char *c;
695
Willy Tarreau664092c2011-12-16 19:11:42 +0100696 if (pattern->len > temp_pattern.data.str.len)
Willy Tarreau11382812008-07-09 16:18:21 +0200697 return ACL_PAT_FAIL;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200698
Willy Tarreau664092c2011-12-16 19:11:42 +0100699 end = temp_pattern.data.str.str + temp_pattern.data.str.len - pattern->len;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200700 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
701 if (icase) {
Willy Tarreau664092c2011-12-16 19:11:42 +0100702 for (c = temp_pattern.data.str.str; c <= end; c++) {
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200703 if (tolower(*c) != tolower(*pattern->ptr.str))
704 continue;
705 if (strncasecmp(pattern->ptr.str, c, pattern->len) == 0)
Willy Tarreau11382812008-07-09 16:18:21 +0200706 return ACL_PAT_PASS;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200707 }
708 } else {
Willy Tarreau664092c2011-12-16 19:11:42 +0100709 for (c = temp_pattern.data.str.str; c <= end; c++) {
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200710 if (*c != *pattern->ptr.str)
711 continue;
712 if (strncmp(pattern->ptr.str, c, pattern->len) == 0)
Willy Tarreau11382812008-07-09 16:18:21 +0200713 return ACL_PAT_PASS;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200714 }
Willy Tarreaua84d3742007-05-07 00:36:48 +0200715 }
Willy Tarreau11382812008-07-09 16:18:21 +0200716 return ACL_PAT_FAIL;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200717}
718
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200719/* Background: Fast way to find a zero byte in a word
720 * http://graphics.stanford.edu/~seander/bithacks.html#ZeroInWord
721 * hasZeroByte = (v - 0x01010101UL) & ~v & 0x80808080UL;
722 *
723 * To look for 4 different byte values, xor the word with those bytes and
724 * then check for zero bytes:
725 *
726 * v = (((unsigned char)c * 0x1010101U) ^ delimiter)
727 * where <delimiter> is the 4 byte values to look for (as an uint)
728 * and <c> is the character that is being tested
729 */
730static inline unsigned int is_delimiter(unsigned char c, unsigned int mask)
731{
732 mask ^= (c * 0x01010101); /* propagate the char to all 4 bytes */
733 return (mask - 0x01010101) & ~mask & 0x80808080U;
734}
735
736static inline unsigned int make_4delim(unsigned char d1, unsigned char d2, unsigned char d3, unsigned char d4)
737{
738 return d1 << 24 | d2 << 16 | d3 << 8 | d4;
739}
740
Willy Tarreaua84d3742007-05-07 00:36:48 +0200741/* This one is used by other real functions. It checks that the pattern is
742 * included inside the tested string, but enclosed between the specified
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200743 * delimiters or at the beginning or end of the string. The delimiters are
744 * provided as an unsigned int made by make_4delim() and match up to 4 different
745 * delimiters. Delimiters are stripped at the beginning and end of the pattern.
Willy Tarreaua84d3742007-05-07 00:36:48 +0200746 */
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200747static int match_word(struct acl_test *test, struct acl_pattern *pattern, unsigned int delimiters)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200748{
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200749 int may_match, icase;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200750 char *c, *end;
751 char *ps;
752 int pl;
753
754 pl = pattern->len;
755 ps = pattern->ptr.str;
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200756
757 while (pl > 0 && is_delimiter(*ps, delimiters)) {
Willy Tarreaua84d3742007-05-07 00:36:48 +0200758 pl--;
759 ps++;
760 }
761
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200762 while (pl > 0 && is_delimiter(ps[pl - 1], delimiters))
Willy Tarreaua84d3742007-05-07 00:36:48 +0200763 pl--;
764
Willy Tarreau664092c2011-12-16 19:11:42 +0100765 if (pl > temp_pattern.data.str.len)
Willy Tarreau11382812008-07-09 16:18:21 +0200766 return ACL_PAT_FAIL;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200767
768 may_match = 1;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200769 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
Willy Tarreau664092c2011-12-16 19:11:42 +0100770 end = temp_pattern.data.str.str + temp_pattern.data.str.len - pl;
771 for (c = temp_pattern.data.str.str; c <= end; c++) {
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200772 if (is_delimiter(*c, delimiters)) {
Willy Tarreaua84d3742007-05-07 00:36:48 +0200773 may_match = 1;
774 continue;
775 }
Willy Tarreaua84d3742007-05-07 00:36:48 +0200776
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200777 if (!may_match)
778 continue;
779
780 if (icase) {
781 if ((tolower(*c) == tolower(*ps)) &&
782 (strncasecmp(ps, c, pl) == 0) &&
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200783 (c == end || is_delimiter(c[pl], delimiters)))
Willy Tarreau11382812008-07-09 16:18:21 +0200784 return ACL_PAT_PASS;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200785 } else {
786 if ((*c == *ps) &&
787 (strncmp(ps, c, pl) == 0) &&
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200788 (c == end || is_delimiter(c[pl], delimiters)))
Willy Tarreau11382812008-07-09 16:18:21 +0200789 return ACL_PAT_PASS;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200790 }
Willy Tarreaua84d3742007-05-07 00:36:48 +0200791 may_match = 0;
792 }
Willy Tarreau11382812008-07-09 16:18:21 +0200793 return ACL_PAT_FAIL;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200794}
795
796/* Checks that the pattern is included inside the tested string, but enclosed
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200797 * between the delimiters '?' or '/' or at the beginning or end of the string.
798 * Delimiters at the beginning or end of the pattern are ignored.
Willy Tarreaua84d3742007-05-07 00:36:48 +0200799 */
800int acl_match_dir(struct acl_test *test, struct acl_pattern *pattern)
801{
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200802 return match_word(test, pattern, make_4delim('/', '?', '?', '?'));
Willy Tarreaua84d3742007-05-07 00:36:48 +0200803}
804
805/* Checks that the pattern is included inside the tested string, but enclosed
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200806 * between the delmiters '/', '?', '.' or ":" or at the beginning or end of
807 * the string. Delimiters at the beginning or end of the pattern are ignored.
Willy Tarreaua84d3742007-05-07 00:36:48 +0200808 */
809int acl_match_dom(struct acl_test *test, struct acl_pattern *pattern)
810{
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200811 return match_word(test, pattern, make_4delim('/', '?', '.', ':'));
Willy Tarreaua84d3742007-05-07 00:36:48 +0200812}
813
814/* Checks that the integer in <test> is included between min and max */
Willy Tarreauae8b7962007-06-09 23:10:04 +0200815int acl_match_int(struct acl_test *test, struct acl_pattern *pattern)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200816{
Willy Tarreaua5e37562011-12-16 17:06:15 +0100817 if ((!pattern->val.range.min_set || pattern->val.range.min <= temp_pattern.data.integer) &&
818 (!pattern->val.range.max_set || temp_pattern.data.integer <= pattern->val.range.max))
Willy Tarreau11382812008-07-09 16:18:21 +0200819 return ACL_PAT_PASS;
820 return ACL_PAT_FAIL;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200821}
822
Willy Tarreau0e698542011-09-16 08:32:32 +0200823/* Checks that the length of the pattern in <test> is included between min and max */
824int acl_match_len(struct acl_test *test, struct acl_pattern *pattern)
825{
Willy Tarreau664092c2011-12-16 19:11:42 +0100826 if ((!pattern->val.range.min_set || pattern->val.range.min <= temp_pattern.data.str.len) &&
827 (!pattern->val.range.max_set || temp_pattern.data.str.len <= pattern->val.range.max))
Willy Tarreau0e698542011-09-16 08:32:32 +0200828 return ACL_PAT_PASS;
829 return ACL_PAT_FAIL;
830}
831
Willy Tarreaua67fad92007-05-08 19:50:09 +0200832int acl_match_ip(struct acl_test *test, struct acl_pattern *pattern)
833{
834 struct in_addr *s;
835
Willy Tarreauf4362b32011-12-16 17:49:52 +0100836 if (temp_pattern.type != PATTERN_TYPE_IP)
Willy Tarreau11382812008-07-09 16:18:21 +0200837 return ACL_PAT_FAIL;
Willy Tarreaua67fad92007-05-08 19:50:09 +0200838
Willy Tarreauf4362b32011-12-16 17:49:52 +0100839 s = &temp_pattern.data.ip;
Willy Tarreaua67fad92007-05-08 19:50:09 +0200840 if (((s->s_addr ^ pattern->val.ipv4.addr.s_addr) & pattern->val.ipv4.mask.s_addr) == 0)
Willy Tarreau11382812008-07-09 16:18:21 +0200841 return ACL_PAT_PASS;
842 return ACL_PAT_FAIL;
Willy Tarreaua67fad92007-05-08 19:50:09 +0200843}
844
Willy Tarreaub337b532010-05-13 20:03:41 +0200845/* Lookup an IPv4 address in the expression's pattern tree using the longest
846 * match method. The node is returned if it exists, otherwise NULL.
847 */
848void *acl_lookup_ip(struct acl_test *test, struct acl_expr *expr)
849{
850 struct in_addr *s;
851
Willy Tarreauf4362b32011-12-16 17:49:52 +0100852 if (temp_pattern.type != PATTERN_TYPE_IP)
Willy Tarreaub337b532010-05-13 20:03:41 +0200853 return ACL_PAT_FAIL;
854
Willy Tarreauf4362b32011-12-16 17:49:52 +0100855 s = &temp_pattern.data.ip;
Willy Tarreaub337b532010-05-13 20:03:41 +0200856 return ebmb_lookup_longest(&expr->pattern_tree, &s->s_addr);
857}
858
Willy Tarreaua84d3742007-05-07 00:36:48 +0200859/* Parse a string. It is allocated and duplicated. */
Willy Tarreauae8b7962007-06-09 23:10:04 +0200860int acl_parse_str(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200861{
862 int len;
863
Willy Tarreauae8b7962007-06-09 23:10:04 +0200864 len = strlen(*text);
Willy Tarreauc4262962010-05-10 23:42:40 +0200865
866 if (pattern->flags & ACL_PAT_F_TREE_OK) {
867 /* we're allowed to put the data in a tree whose root is pointed
868 * to by val.tree.
869 */
870 struct ebmb_node *node;
871
872 node = calloc(1, sizeof(*node) + len + 1);
873 if (!node)
874 return 0;
875 memcpy(node->key, *text, len + 1);
876 if (ebst_insert(pattern->val.tree, node) != node)
877 free(node); /* was a duplicate */
878 pattern->flags |= ACL_PAT_F_TREE; /* this pattern now contains a tree */
879 return 1;
880 }
881
Willy Tarreauae8b7962007-06-09 23:10:04 +0200882 pattern->ptr.str = strdup(*text);
Willy Tarreaua84d3742007-05-07 00:36:48 +0200883 if (!pattern->ptr.str)
884 return 0;
885 pattern->len = len;
886 return 1;
887}
888
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +0100889/* Parse and concatenate all further strings into one. */
890int
891acl_parse_strcat(const char **text, struct acl_pattern *pattern, int *opaque)
892{
893
894 int len = 0, i;
895 char *s;
896
897 for (i = 0; *text[i]; i++)
898 len += strlen(text[i])+1;
899
900 pattern->ptr.str = s = calloc(1, len);
901 if (!pattern->ptr.str)
902 return 0;
903
904 for (i = 0; *text[i]; i++)
905 s += sprintf(s, i?" %s":"%s", text[i]);
906
907 pattern->len = len;
908
909 return i;
910}
911
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +0200912/* Free data allocated by acl_parse_reg */
913static void acl_free_reg(void *ptr) {
914
915 regfree((regex_t *)ptr);
916}
917
Willy Tarreauf3d25982007-05-08 22:45:09 +0200918/* Parse a regex. It is allocated. */
Willy Tarreauae8b7962007-06-09 23:10:04 +0200919int acl_parse_reg(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreauf3d25982007-05-08 22:45:09 +0200920{
921 regex_t *preg;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200922 int icase;
Willy Tarreauf3d25982007-05-08 22:45:09 +0200923
924 preg = calloc(1, sizeof(regex_t));
925
926 if (!preg)
927 return 0;
928
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200929 icase = (pattern->flags & ACL_PAT_F_IGNORE_CASE) ? REG_ICASE : 0;
930 if (regcomp(preg, *text, REG_EXTENDED | REG_NOSUB | icase) != 0) {
Willy Tarreauf3d25982007-05-08 22:45:09 +0200931 free(preg);
932 return 0;
933 }
934
935 pattern->ptr.reg = preg;
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +0200936 pattern->freeptrbuf = &acl_free_reg;
Willy Tarreauf3d25982007-05-08 22:45:09 +0200937 return 1;
938}
939
Willy Tarreauae8b7962007-06-09 23:10:04 +0200940/* Parse a range of positive integers delimited by either ':' or '-'. If only
941 * one integer is read, it is set as both min and max. An operator may be
942 * specified as the prefix, among this list of 5 :
943 *
944 * 0:eq, 1:gt, 2:ge, 3:lt, 4:le
945 *
946 * The default operator is "eq". It supports range matching. Ranges are
947 * rejected for other operators. The operator may be changed at any time.
948 * The operator is stored in the 'opaque' argument.
949 *
Willy Tarreaua84d3742007-05-07 00:36:48 +0200950 */
Willy Tarreauae8b7962007-06-09 23:10:04 +0200951int acl_parse_int(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200952{
Willy Tarreauae8b7962007-06-09 23:10:04 +0200953 signed long long i;
954 unsigned int j, last, skip = 0;
955 const char *ptr = *text;
956
957
Willy Tarreau8f8e6452007-06-17 21:51:38 +0200958 while (!isdigit((unsigned char)*ptr)) {
Willy Tarreau1c7cc5b2010-07-18 10:46:33 +0200959 switch (get_std_op(ptr)) {
960 case STD_OP_EQ: *opaque = 0; break;
961 case STD_OP_GT: *opaque = 1; break;
962 case STD_OP_GE: *opaque = 2; break;
963 case STD_OP_LT: *opaque = 3; break;
964 case STD_OP_LE: *opaque = 4; break;
965 default:
Willy Tarreauae8b7962007-06-09 23:10:04 +0200966 return 0;
Willy Tarreau1c7cc5b2010-07-18 10:46:33 +0200967 }
Willy Tarreauae8b7962007-06-09 23:10:04 +0200968
969 skip++;
970 ptr = text[skip];
971 }
Willy Tarreaua84d3742007-05-07 00:36:48 +0200972
973 last = i = 0;
974 while (1) {
Willy Tarreauae8b7962007-06-09 23:10:04 +0200975 j = *ptr++;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200976 if ((j == '-' || j == ':') && !last) {
977 last++;
978 pattern->val.range.min = i;
979 i = 0;
980 continue;
981 }
982 j -= '0';
983 if (j > 9)
984 // also catches the terminating zero
985 break;
986 i *= 10;
987 i += j;
988 }
Willy Tarreauae8b7962007-06-09 23:10:04 +0200989
990 if (last && *opaque >= 1 && *opaque <= 4)
991 /* having a range with a min or a max is absurd */
992 return 0;
993
Willy Tarreaua84d3742007-05-07 00:36:48 +0200994 if (!last)
995 pattern->val.range.min = i;
996 pattern->val.range.max = i;
Willy Tarreauae8b7962007-06-09 23:10:04 +0200997
998 switch (*opaque) {
999 case 0: /* eq */
1000 pattern->val.range.min_set = 1;
1001 pattern->val.range.max_set = 1;
1002 break;
1003 case 1: /* gt */
1004 pattern->val.range.min++; /* gt = ge + 1 */
1005 case 2: /* ge */
1006 pattern->val.range.min_set = 1;
1007 pattern->val.range.max_set = 0;
1008 break;
1009 case 3: /* lt */
1010 pattern->val.range.max--; /* lt = le - 1 */
1011 case 4: /* le */
1012 pattern->val.range.min_set = 0;
1013 pattern->val.range.max_set = 1;
1014 break;
1015 }
1016 return skip + 1;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001017}
1018
Willy Tarreau4a26d2f2008-07-15 16:05:33 +02001019/* Parse a range of positive 2-component versions delimited by either ':' or
1020 * '-'. The version consists in a major and a minor, both of which must be
1021 * smaller than 65536, because internally they will be represented as a 32-bit
1022 * integer.
1023 * If only one version is read, it is set as both min and max. Just like for
1024 * pure integers, an operator may be specified as the prefix, among this list
1025 * of 5 :
1026 *
1027 * 0:eq, 1:gt, 2:ge, 3:lt, 4:le
1028 *
1029 * The default operator is "eq". It supports range matching. Ranges are
1030 * rejected for other operators. The operator may be changed at any time.
1031 * The operator is stored in the 'opaque' argument. This allows constructs
1032 * such as the following one :
1033 *
1034 * acl obsolete_ssl ssl_req_proto lt 3
1035 * acl unsupported_ssl ssl_req_proto gt 3.1
1036 * acl valid_ssl ssl_req_proto 3.0-3.1
1037 *
1038 */
1039int acl_parse_dotted_ver(const char **text, struct acl_pattern *pattern, int *opaque)
1040{
1041 signed long long i;
1042 unsigned int j, last, skip = 0;
1043 const char *ptr = *text;
1044
1045
1046 while (!isdigit((unsigned char)*ptr)) {
Willy Tarreau1c7cc5b2010-07-18 10:46:33 +02001047 switch (get_std_op(ptr)) {
1048 case STD_OP_EQ: *opaque = 0; break;
1049 case STD_OP_GT: *opaque = 1; break;
1050 case STD_OP_GE: *opaque = 2; break;
1051 case STD_OP_LT: *opaque = 3; break;
1052 case STD_OP_LE: *opaque = 4; break;
1053 default:
Willy Tarreau4a26d2f2008-07-15 16:05:33 +02001054 return 0;
Willy Tarreau1c7cc5b2010-07-18 10:46:33 +02001055 }
Willy Tarreau4a26d2f2008-07-15 16:05:33 +02001056
1057 skip++;
1058 ptr = text[skip];
1059 }
1060
1061 last = i = 0;
1062 while (1) {
1063 j = *ptr++;
1064 if (j == '.') {
1065 /* minor part */
1066 if (i >= 65536)
1067 return 0;
1068 i <<= 16;
1069 continue;
1070 }
1071 if ((j == '-' || j == ':') && !last) {
1072 last++;
1073 if (i < 65536)
1074 i <<= 16;
1075 pattern->val.range.min = i;
1076 i = 0;
1077 continue;
1078 }
1079 j -= '0';
1080 if (j > 9)
1081 // also catches the terminating zero
1082 break;
1083 i = (i & 0xFFFF0000) + (i & 0xFFFF) * 10;
1084 i += j;
1085 }
1086
1087 /* if we only got a major version, let's shift it now */
1088 if (i < 65536)
1089 i <<= 16;
1090
1091 if (last && *opaque >= 1 && *opaque <= 4)
1092 /* having a range with a min or a max is absurd */
1093 return 0;
1094
1095 if (!last)
1096 pattern->val.range.min = i;
1097 pattern->val.range.max = i;
1098
1099 switch (*opaque) {
1100 case 0: /* eq */
1101 pattern->val.range.min_set = 1;
1102 pattern->val.range.max_set = 1;
1103 break;
1104 case 1: /* gt */
1105 pattern->val.range.min++; /* gt = ge + 1 */
1106 case 2: /* ge */
1107 pattern->val.range.min_set = 1;
1108 pattern->val.range.max_set = 0;
1109 break;
1110 case 3: /* lt */
1111 pattern->val.range.max--; /* lt = le - 1 */
1112 case 4: /* le */
1113 pattern->val.range.min_set = 0;
1114 pattern->val.range.max_set = 1;
1115 break;
1116 }
1117 return skip + 1;
1118}
1119
Willy Tarreaua67fad92007-05-08 19:50:09 +02001120/* Parse an IP address and an optional mask in the form addr[/mask].
1121 * The addr may either be an IPv4 address or a hostname. The mask
1122 * may either be a dotted mask or a number of bits. Returns 1 if OK,
1123 * otherwise 0.
1124 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02001125int acl_parse_ip(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreaua67fad92007-05-08 19:50:09 +02001126{
Willy Tarreaub337b532010-05-13 20:03:41 +02001127 struct eb_root *tree = NULL;
1128 if (pattern->flags & ACL_PAT_F_TREE_OK)
1129 tree = pattern->val.tree;
1130
1131 if (str2net(*text, &pattern->val.ipv4.addr, &pattern->val.ipv4.mask)) {
1132 unsigned int mask = ntohl(pattern->val.ipv4.mask.s_addr);
1133 struct ebmb_node *node;
1134 /* check if the mask is contiguous so that we can insert the
1135 * network into the tree. A continuous mask has only ones on
1136 * the left. This means that this mask + its lower bit added
1137 * once again is null.
1138 */
1139 if (mask + (mask & -mask) == 0 && tree) {
1140 mask = mask ? 33 - flsnz(mask & -mask) : 0; /* equals cidr value */
1141 /* FIXME: insert <addr>/<mask> into the tree here */
1142 node = calloc(1, sizeof(*node) + 4); /* reserve 4 bytes for IPv4 address */
1143 if (!node)
1144 return 0;
1145 memcpy(node->key, &pattern->val.ipv4.addr, 4); /* network byte order */
1146 node->node.pfx = mask;
1147 if (ebmb_insert_prefix(tree, node, 4) != node)
1148 free(node); /* was a duplicate */
1149 pattern->flags |= ACL_PAT_F_TREE;
1150 return 1;
1151 }
Willy Tarreauae8b7962007-06-09 23:10:04 +02001152 return 1;
Willy Tarreaub337b532010-05-13 20:03:41 +02001153 }
Willy Tarreauae8b7962007-06-09 23:10:04 +02001154 else
1155 return 0;
Willy Tarreaua67fad92007-05-08 19:50:09 +02001156}
1157
Willy Tarreaua84d3742007-05-07 00:36:48 +02001158/*
1159 * Registers the ACL keyword list <kwl> as a list of valid keywords for next
1160 * parsing sessions.
1161 */
1162void acl_register_keywords(struct acl_kw_list *kwl)
1163{
1164 LIST_ADDQ(&acl_keywords.list, &kwl->list);
1165}
1166
1167/*
1168 * Unregisters the ACL keyword list <kwl> from the list of valid keywords.
1169 */
1170void acl_unregister_keywords(struct acl_kw_list *kwl)
1171{
1172 LIST_DEL(&kwl->list);
1173 LIST_INIT(&kwl->list);
1174}
1175
1176/* Return a pointer to the ACL <name> within the list starting at <head>, or
1177 * NULL if not found.
1178 */
1179struct acl *find_acl_by_name(const char *name, struct list *head)
1180{
1181 struct acl *acl;
1182 list_for_each_entry(acl, head, list) {
1183 if (strcmp(acl->name, name) == 0)
1184 return acl;
1185 }
1186 return NULL;
1187}
1188
1189/* Return a pointer to the ACL keyword <kw>, or NULL if not found. Note that if
1190 * <kw> contains an opening parenthesis, only the left part of it is checked.
1191 */
1192struct acl_keyword *find_acl_kw(const char *kw)
1193{
1194 int index;
1195 const char *kwend;
1196 struct acl_kw_list *kwl;
1197
1198 kwend = strchr(kw, '(');
1199 if (!kwend)
1200 kwend = kw + strlen(kw);
1201
1202 list_for_each_entry(kwl, &acl_keywords.list, list) {
1203 for (index = 0; kwl->kw[index].kw != NULL; index++) {
1204 if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) &&
1205 kwl->kw[index].kw[kwend-kw] == 0)
1206 return &kwl->kw[index];
1207 }
1208 }
1209 return NULL;
1210}
1211
Willy Tarreaudfd7fca2011-03-09 07:27:02 +01001212/* NB: does nothing if <pat> is NULL */
Willy Tarreaua84d3742007-05-07 00:36:48 +02001213static void free_pattern(struct acl_pattern *pat)
1214{
Willy Tarreaudfd7fca2011-03-09 07:27:02 +01001215 if (!pat)
1216 return;
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02001217
1218 if (pat->ptr.ptr) {
1219 if (pat->freeptrbuf)
1220 pat->freeptrbuf(pat->ptr.ptr);
1221
Willy Tarreaua84d3742007-05-07 00:36:48 +02001222 free(pat->ptr.ptr);
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02001223 }
1224
Willy Tarreaua84d3742007-05-07 00:36:48 +02001225 free(pat);
1226}
1227
1228static void free_pattern_list(struct list *head)
1229{
1230 struct acl_pattern *pat, *tmp;
1231 list_for_each_entry_safe(pat, tmp, head, list)
1232 free_pattern(pat);
1233}
1234
Willy Tarreaue56cda92010-05-11 23:25:05 +02001235static void free_pattern_tree(struct eb_root *root)
1236{
1237 struct eb_node *node, *next;
1238 node = eb_first(root);
1239 while (node) {
1240 next = eb_next(node);
1241 free(node);
1242 node = next;
1243 }
1244}
1245
Willy Tarreaua84d3742007-05-07 00:36:48 +02001246static struct acl_expr *prune_acl_expr(struct acl_expr *expr)
1247{
Willy Tarreau34db1082012-04-19 17:16:54 +02001248 struct arg *arg;
1249
Willy Tarreaua84d3742007-05-07 00:36:48 +02001250 free_pattern_list(&expr->patterns);
Willy Tarreaue56cda92010-05-11 23:25:05 +02001251 free_pattern_tree(&expr->pattern_tree);
Willy Tarreaua84d3742007-05-07 00:36:48 +02001252 LIST_INIT(&expr->patterns);
Willy Tarreau34db1082012-04-19 17:16:54 +02001253
1254 for (arg = expr->args; arg; arg++) {
1255 if (arg->type == ARGT_STOP)
1256 break;
1257 if (arg->type == ARGT_FE || arg->type == ARGT_BE ||
1258 arg->type == ARGT_TAB || arg->type == ARGT_SRV ||
1259 arg->type == ARGT_USR || arg->type == ARGT_STR) {
1260 free(arg->data.str.str);
1261 arg->data.str.str = NULL;
1262 }
1263 arg++;
1264 }
1265
1266 free(expr->args);
Willy Tarreaua84d3742007-05-07 00:36:48 +02001267 expr->kw->use_cnt--;
1268 return expr;
1269}
1270
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001271static int acl_read_patterns_from_file( struct acl_keyword *aclkw,
1272 struct acl_expr *expr,
1273 const char *filename, int patflags)
1274{
1275 FILE *file;
1276 char *c;
1277 const char *args[2];
1278 struct acl_pattern *pattern;
1279 int opaque;
Willy Tarreau6a8097f2011-02-26 15:14:15 +01001280 int ret = 0;
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001281
1282 file = fopen(filename, "r");
1283 if (!file)
1284 return 0;
1285
1286 /* now parse all patterns. The file may contain only one pattern per
1287 * line. If the line contains spaces, they will be part of the pattern.
1288 * The pattern stops at the first CR, LF or EOF encountered.
1289 */
1290 opaque = 0;
Willy Tarreaue56cda92010-05-11 23:25:05 +02001291 pattern = NULL;
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001292 args[1] = "";
1293 while (fgets(trash, sizeof(trash), file) != NULL) {
1294
1295 c = trash;
Willy Tarreau58215a02010-05-13 22:07:43 +02001296
1297 /* ignore lines beginning with a dash */
1298 if (*c == '#')
1299 continue;
1300
1301 /* strip leading spaces and tabs */
1302 while (*c == ' ' || *c == '\t')
1303 c++;
1304
Willy Tarreau58215a02010-05-13 22:07:43 +02001305
1306 args[0] = c;
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001307 while (*c && *c != '\n' && *c != '\r')
1308 c++;
1309 *c = 0;
1310
Willy Tarreau51091962011-01-03 21:04:10 +01001311 /* empty lines are ignored too */
1312 if (c == args[0])
1313 continue;
1314
Willy Tarreaue56cda92010-05-11 23:25:05 +02001315 /* we keep the previous pattern along iterations as long as it's not used */
1316 if (!pattern)
1317 pattern = (struct acl_pattern *)malloc(sizeof(*pattern));
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001318 if (!pattern)
1319 goto out_close;
Willy Tarreaue56cda92010-05-11 23:25:05 +02001320
1321 memset(pattern, 0, sizeof(*pattern));
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001322 pattern->flags = patflags;
1323
Willy Tarreaue56cda92010-05-11 23:25:05 +02001324 if ((aclkw->requires & ACL_MAY_LOOKUP) && !(pattern->flags & ACL_PAT_F_IGNORE_CASE)) {
1325 /* we pre-set the data pointer to the tree's head so that functions
1326 * which are able to insert in a tree know where to do that.
1327 */
1328 pattern->flags |= ACL_PAT_F_TREE_OK;
1329 pattern->val.tree = &expr->pattern_tree;
1330 }
1331
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001332 if (!aclkw->parse(args, pattern, &opaque))
1333 goto out_free_pattern;
Willy Tarreaue56cda92010-05-11 23:25:05 +02001334
1335 /* if the parser did not feed the tree, let's chain the pattern to the list */
1336 if (!(pattern->flags & ACL_PAT_F_TREE)) {
1337 LIST_ADDQ(&expr->patterns, &pattern->list);
1338 pattern = NULL; /* get a new one */
1339 }
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001340 }
Willy Tarreau6a8097f2011-02-26 15:14:15 +01001341
1342 ret = 1; /* success */
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001343
1344 out_free_pattern:
1345 free_pattern(pattern);
1346 out_close:
1347 fclose(file);
Willy Tarreau6a8097f2011-02-26 15:14:15 +01001348 return ret;
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001349}
1350
Willy Tarreaua84d3742007-05-07 00:36:48 +02001351/* Parse an ACL expression starting at <args>[0], and return it.
1352 * Right now, the only accepted syntax is :
1353 * <subject> [<value>...]
1354 */
1355struct acl_expr *parse_acl_expr(const char **args)
1356{
1357 __label__ out_return, out_free_expr, out_free_pattern;
1358 struct acl_expr *expr;
1359 struct acl_keyword *aclkw;
1360 struct acl_pattern *pattern;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02001361 int opaque, patflags;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001362 const char *arg;
1363
1364 aclkw = find_acl_kw(args[0]);
1365 if (!aclkw || !aclkw->parse)
1366 goto out_return;
1367
1368 expr = (struct acl_expr *)calloc(1, sizeof(*expr));
1369 if (!expr)
1370 goto out_return;
1371
1372 expr->kw = aclkw;
1373 aclkw->use_cnt++;
1374 LIST_INIT(&expr->patterns);
Willy Tarreaue56cda92010-05-11 23:25:05 +02001375 expr->pattern_tree = EB_ROOT_UNIQUE;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001376
1377 arg = strchr(args[0], '(');
Willy Tarreau61612d42012-04-19 18:42:05 +02001378 if (aclkw->arg_mask) {
1379 int nbargs = 0;
Willy Tarreau34db1082012-04-19 17:16:54 +02001380 char *end;
Willy Tarreau34db1082012-04-19 17:16:54 +02001381
Willy Tarreau61612d42012-04-19 18:42:05 +02001382 if (arg != NULL) {
1383 /* there are 0 or more arguments in the form "subject(arg[,arg]*)" */
1384 arg++;
1385 end = strchr(arg, ')');
1386 if (!end)
1387 goto out_free_expr;
Willy Tarreau34db1082012-04-19 17:16:54 +02001388
Willy Tarreau61612d42012-04-19 18:42:05 +02001389 /* Parse the arguments. Note that currently we have no way to
1390 * report parsing errors, hence the NULL in the error pointers.
1391 * An error is also reported if some mandatory arguments are
1392 * missing.
1393 */
1394 nbargs = make_arg_list(arg, end - arg, aclkw->arg_mask, &expr->args,
1395 NULL, NULL, NULL);
1396 if (nbargs < 0)
1397 goto out_free_expr;
1398 }
1399 else if (ARGM(aclkw->arg_mask)) {
1400 /* there were some mandatory arguments */
Willy Tarreaua84d3742007-05-07 00:36:48 +02001401 goto out_free_expr;
Willy Tarreau61612d42012-04-19 18:42:05 +02001402 }
1403 }
1404 else {
1405 if (arg) {
1406 /* no argument expected */
1407 goto out_free_expr;
1408 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001409 }
1410
Willy Tarreaua84d3742007-05-07 00:36:48 +02001411 args++;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02001412
1413 /* check for options before patterns. Supported options are :
1414 * -i : ignore case for all patterns by default
1415 * -f : read patterns from those files
1416 * -- : everything after this is not an option
1417 */
1418 patflags = 0;
1419 while (**args == '-') {
1420 if ((*args)[1] == 'i')
1421 patflags |= ACL_PAT_F_IGNORE_CASE;
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001422 else if ((*args)[1] == 'f') {
1423 if (!acl_read_patterns_from_file(aclkw, expr, args[1], patflags | ACL_PAT_F_FROM_FILE))
1424 goto out_free_expr;
1425 args++;
1426 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +02001427 else if ((*args)[1] == '-') {
1428 args++;
1429 break;
1430 }
1431 else
1432 break;
1433 args++;
1434 }
1435
1436 /* now parse all patterns */
Willy Tarreauae8b7962007-06-09 23:10:04 +02001437 opaque = 0;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001438 while (**args) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02001439 int ret;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001440 pattern = (struct acl_pattern *)calloc(1, sizeof(*pattern));
1441 if (!pattern)
1442 goto out_free_expr;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02001443 pattern->flags = patflags;
1444
Willy Tarreauae8b7962007-06-09 23:10:04 +02001445 ret = aclkw->parse(args, pattern, &opaque);
1446 if (!ret)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001447 goto out_free_pattern;
1448 LIST_ADDQ(&expr->patterns, &pattern->list);
Willy Tarreauae8b7962007-06-09 23:10:04 +02001449 args += ret;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001450 }
1451
1452 return expr;
1453
1454 out_free_pattern:
1455 free_pattern(pattern);
1456 out_free_expr:
1457 prune_acl_expr(expr);
1458 free(expr);
1459 out_return:
1460 return NULL;
1461}
1462
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02001463/* Purge everything in the acl <acl>, then return <acl>. */
1464struct acl *prune_acl(struct acl *acl) {
1465
1466 struct acl_expr *expr, *exprb;
1467
1468 free(acl->name);
1469
1470 list_for_each_entry_safe(expr, exprb, &acl->expr, list) {
1471 LIST_DEL(&expr->list);
1472 prune_acl_expr(expr);
1473 free(expr);
1474 }
1475
1476 return acl;
1477}
1478
Willy Tarreaua84d3742007-05-07 00:36:48 +02001479/* Parse an ACL with the name starting at <args>[0], and with a list of already
1480 * known ACLs in <acl>. If the ACL was not in the list, it will be added.
Willy Tarreau2a56c5e2010-03-15 16:13:29 +01001481 * A pointer to that ACL is returned. If the ACL has an empty name, then it's
1482 * an anonymous one and it won't be merged with any other one.
Willy Tarreaua84d3742007-05-07 00:36:48 +02001483 *
1484 * args syntax: <aclname> <acl_expr>
1485 */
1486struct acl *parse_acl(const char **args, struct list *known_acl)
1487{
1488 __label__ out_return, out_free_acl_expr, out_free_name;
1489 struct acl *cur_acl;
1490 struct acl_expr *acl_expr;
1491 char *name;
1492
Willy Tarreau2a56c5e2010-03-15 16:13:29 +01001493 if (**args && invalid_char(*args))
Willy Tarreau2e74c3f2007-12-02 18:45:09 +01001494 goto out_return;
1495
Willy Tarreaua84d3742007-05-07 00:36:48 +02001496 acl_expr = parse_acl_expr(args + 1);
1497 if (!acl_expr)
1498 goto out_return;
1499
Willy Tarreau404e8ab2009-07-26 19:40:40 +02001500 /* Check for args beginning with an opening parenthesis just after the
1501 * subject, as this is almost certainly a typo. Right now we can only
1502 * emit a warning, so let's do so.
1503 */
Krzysztof Piotr Oledzki4cdd8312009-10-05 00:23:35 +02001504 if (!strchr(args[1], '(') && *args[2] == '(')
Willy Tarreau404e8ab2009-07-26 19:40:40 +02001505 Warning("parsing acl '%s' :\n"
1506 " matching '%s' for pattern '%s' is likely a mistake and probably\n"
1507 " not what you want. Maybe you need to remove the extraneous space before '('.\n"
1508 " If you are really sure this is not an error, please insert '--' between the\n"
1509 " match and the pattern to make this warning message disappear.\n",
1510 args[0], args[1], args[2]);
1511
Willy Tarreau2a56c5e2010-03-15 16:13:29 +01001512 if (*args[0])
1513 cur_acl = find_acl_by_name(args[0], known_acl);
1514 else
1515 cur_acl = NULL;
1516
Willy Tarreaua84d3742007-05-07 00:36:48 +02001517 if (!cur_acl) {
1518 name = strdup(args[0]);
1519 if (!name)
1520 goto out_free_acl_expr;
1521 cur_acl = (struct acl *)calloc(1, sizeof(*cur_acl));
1522 if (cur_acl == NULL)
1523 goto out_free_name;
1524
1525 LIST_INIT(&cur_acl->expr);
1526 LIST_ADDQ(known_acl, &cur_acl->list);
1527 cur_acl->name = name;
1528 }
1529
Willy Tarreaua9802632008-07-25 19:13:19 +02001530 cur_acl->requires |= acl_expr->kw->requires;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001531 LIST_ADDQ(&cur_acl->expr, &acl_expr->list);
1532 return cur_acl;
1533
1534 out_free_name:
1535 free(name);
1536 out_free_acl_expr:
1537 prune_acl_expr(acl_expr);
1538 free(acl_expr);
1539 out_return:
1540 return NULL;
1541}
1542
Willy Tarreau16fbe822007-06-17 11:54:31 +02001543/* Some useful ACLs provided by default. Only those used are allocated. */
1544
1545const struct {
1546 const char *name;
1547 const char *expr[4]; /* put enough for longest expression */
1548} default_acl_list[] = {
Willy Tarreau58393e12008-07-20 10:39:22 +02001549 { .name = "TRUE", .expr = {"always_true",""}},
1550 { .name = "FALSE", .expr = {"always_false",""}},
Willy Tarreau16fbe822007-06-17 11:54:31 +02001551 { .name = "LOCALHOST", .expr = {"src","127.0.0.1/8",""}},
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001552 { .name = "HTTP", .expr = {"req_proto_http",""}},
Willy Tarreau16fbe822007-06-17 11:54:31 +02001553 { .name = "HTTP_1.0", .expr = {"req_ver","1.0",""}},
1554 { .name = "HTTP_1.1", .expr = {"req_ver","1.1",""}},
1555 { .name = "METH_CONNECT", .expr = {"method","CONNECT",""}},
1556 { .name = "METH_GET", .expr = {"method","GET","HEAD",""}},
1557 { .name = "METH_HEAD", .expr = {"method","HEAD",""}},
1558 { .name = "METH_OPTIONS", .expr = {"method","OPTIONS",""}},
1559 { .name = "METH_POST", .expr = {"method","POST",""}},
1560 { .name = "METH_TRACE", .expr = {"method","TRACE",""}},
1561 { .name = "HTTP_URL_ABS", .expr = {"url_reg","^[^/:]*://",""}},
1562 { .name = "HTTP_URL_SLASH", .expr = {"url_beg","/",""}},
1563 { .name = "HTTP_URL_STAR", .expr = {"url","*",""}},
1564 { .name = "HTTP_CONTENT", .expr = {"hdr_val(content-length)","gt","0",""}},
Emeric Brunbede3d02009-06-30 17:54:00 +02001565 { .name = "RDP_COOKIE", .expr = {"req_rdp_cookie_cnt","gt","0",""}},
Willy Tarreauc6317702008-07-20 09:29:50 +02001566 { .name = "REQ_CONTENT", .expr = {"req_len","gt","0",""}},
Willy Tarreaub6fb4202008-07-20 11:18:28 +02001567 { .name = "WAIT_END", .expr = {"wait_end",""}},
Willy Tarreau16fbe822007-06-17 11:54:31 +02001568 { .name = NULL, .expr = {""}}
1569};
1570
1571/* Find a default ACL from the default_acl list, compile it and return it.
1572 * If the ACL is not found, NULL is returned. In theory, it cannot fail,
1573 * except when default ACLs are broken, in which case it will return NULL.
1574 * If <known_acl> is not NULL, the ACL will be queued at its tail.
1575 */
1576struct acl *find_acl_default(const char *acl_name, struct list *known_acl)
1577{
1578 __label__ out_return, out_free_acl_expr, out_free_name;
1579 struct acl *cur_acl;
1580 struct acl_expr *acl_expr;
1581 char *name;
1582 int index;
1583
1584 for (index = 0; default_acl_list[index].name != NULL; index++) {
1585 if (strcmp(acl_name, default_acl_list[index].name) == 0)
1586 break;
1587 }
1588
1589 if (default_acl_list[index].name == NULL)
1590 return NULL;
1591
1592 acl_expr = parse_acl_expr((const char **)default_acl_list[index].expr);
1593 if (!acl_expr)
1594 goto out_return;
1595
1596 name = strdup(acl_name);
1597 if (!name)
1598 goto out_free_acl_expr;
1599 cur_acl = (struct acl *)calloc(1, sizeof(*cur_acl));
1600 if (cur_acl == NULL)
1601 goto out_free_name;
1602
1603 cur_acl->name = name;
Willy Tarreaua55b7dc2009-07-12 09:21:30 +02001604 cur_acl->requires |= acl_expr->kw->requires;
Willy Tarreau16fbe822007-06-17 11:54:31 +02001605 LIST_INIT(&cur_acl->expr);
1606 LIST_ADDQ(&cur_acl->expr, &acl_expr->list);
1607 if (known_acl)
1608 LIST_ADDQ(known_acl, &cur_acl->list);
1609
1610 return cur_acl;
1611
1612 out_free_name:
1613 free(name);
1614 out_free_acl_expr:
1615 prune_acl_expr(acl_expr);
1616 free(acl_expr);
1617 out_return:
1618 return NULL;
1619}
Willy Tarreaua84d3742007-05-07 00:36:48 +02001620
1621/* Purge everything in the acl_cond <cond>, then return <cond>. */
1622struct acl_cond *prune_acl_cond(struct acl_cond *cond)
1623{
1624 struct acl_term_suite *suite, *tmp_suite;
1625 struct acl_term *term, *tmp_term;
1626
1627 /* iterate through all term suites and free all terms and all suites */
1628 list_for_each_entry_safe(suite, tmp_suite, &cond->suites, list) {
1629 list_for_each_entry_safe(term, tmp_term, &suite->terms, list)
1630 free(term);
1631 free(suite);
1632 }
1633 return cond;
1634}
1635
1636/* Parse an ACL condition starting at <args>[0], relying on a list of already
1637 * known ACLs passed in <known_acl>. The new condition is returned (or NULL in
1638 * case of low memory). Supports multiple conditions separated by "or".
1639 */
1640struct acl_cond *parse_acl_cond(const char **args, struct list *known_acl, int pol)
1641{
1642 __label__ out_return, out_free_suite, out_free_term;
Willy Tarreau74b98a82007-06-16 19:35:18 +02001643 int arg, neg;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001644 const char *word;
1645 struct acl *cur_acl;
1646 struct acl_term *cur_term;
1647 struct acl_term_suite *cur_suite;
1648 struct acl_cond *cond;
1649
1650 cond = (struct acl_cond *)calloc(1, sizeof(*cond));
1651 if (cond == NULL)
1652 goto out_return;
1653
1654 LIST_INIT(&cond->list);
1655 LIST_INIT(&cond->suites);
1656 cond->pol = pol;
1657
1658 cur_suite = NULL;
Willy Tarreau74b98a82007-06-16 19:35:18 +02001659 neg = 0;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001660 for (arg = 0; *args[arg]; arg++) {
1661 word = args[arg];
1662
1663 /* remove as many exclamation marks as we can */
1664 while (*word == '!') {
1665 neg = !neg;
1666 word++;
1667 }
1668
1669 /* an empty word is allowed because we cannot force the user to
1670 * always think about not leaving exclamation marks alone.
1671 */
1672 if (!*word)
1673 continue;
1674
Willy Tarreau16fbe822007-06-17 11:54:31 +02001675 if (strcasecmp(word, "or") == 0 || strcmp(word, "||") == 0) {
Willy Tarreaua84d3742007-05-07 00:36:48 +02001676 /* new term suite */
1677 cur_suite = NULL;
1678 neg = 0;
1679 continue;
1680 }
1681
Willy Tarreau95fa4692010-02-01 13:05:50 +01001682 if (strcmp(word, "{") == 0) {
1683 /* we may have a complete ACL expression between two braces,
1684 * find the last one.
1685 */
1686 int arg_end = arg + 1;
1687 const char **args_new;
1688
1689 while (*args[arg_end] && strcmp(args[arg_end], "}") != 0)
1690 arg_end++;
1691
1692 if (!*args[arg_end])
1693 goto out_free_suite;
1694
1695 args_new = calloc(1, (arg_end - arg + 1) * sizeof(*args_new));
1696 if (!args_new)
1697 goto out_free_suite;
1698
Willy Tarreau2a56c5e2010-03-15 16:13:29 +01001699 args_new[0] = "";
Willy Tarreau95fa4692010-02-01 13:05:50 +01001700 memcpy(args_new + 1, args + arg + 1, (arg_end - arg) * sizeof(*args_new));
1701 args_new[arg_end - arg] = "";
1702 cur_acl = parse_acl(args_new, known_acl);
1703 free(args_new);
1704
1705 if (!cur_acl)
Willy Tarreau16fbe822007-06-17 11:54:31 +02001706 goto out_free_suite;
Willy Tarreau95fa4692010-02-01 13:05:50 +01001707 arg = arg_end;
1708 }
1709 else {
1710 /* search for <word> in the known ACL names. If we do not find
1711 * it, let's look for it in the default ACLs, and if found, add
1712 * it to the list of ACLs of this proxy. This makes it possible
1713 * to override them.
1714 */
1715 cur_acl = find_acl_by_name(word, known_acl);
1716 if (cur_acl == NULL) {
1717 cur_acl = find_acl_default(word, known_acl);
1718 if (cur_acl == NULL)
1719 goto out_free_suite;
1720 }
Willy Tarreau16fbe822007-06-17 11:54:31 +02001721 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001722
1723 cur_term = (struct acl_term *)calloc(1, sizeof(*cur_term));
1724 if (cur_term == NULL)
1725 goto out_free_suite;
1726
1727 cur_term->acl = cur_acl;
1728 cur_term->neg = neg;
Willy Tarreaua9802632008-07-25 19:13:19 +02001729 cond->requires |= cur_acl->requires;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001730
1731 if (!cur_suite) {
1732 cur_suite = (struct acl_term_suite *)calloc(1, sizeof(*cur_suite));
1733 if (cur_term == NULL)
1734 goto out_free_term;
1735 LIST_INIT(&cur_suite->terms);
1736 LIST_ADDQ(&cond->suites, &cur_suite->list);
1737 }
1738 LIST_ADDQ(&cur_suite->terms, &cur_term->list);
Willy Tarreau74b98a82007-06-16 19:35:18 +02001739 neg = 0;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001740 }
1741
1742 return cond;
1743
1744 out_free_term:
1745 free(cur_term);
1746 out_free_suite:
1747 prune_acl_cond(cond);
1748 free(cond);
1749 out_return:
1750 return NULL;
1751}
1752
Willy Tarreau2bbba412010-01-28 16:48:33 +01001753/* Builds an ACL condition starting at the if/unless keyword. The complete
1754 * condition is returned. NULL is returned in case of error or if the first
1755 * word is neither "if" nor "unless". It automatically sets the file name and
1756 * the line number in the condition for better error reporting, and adds the
1757 * ACL requirements to the proxy's acl_requires.
1758 */
1759struct acl_cond *build_acl_cond(const char *file, int line, struct proxy *px, const char **args)
1760{
1761 int pol = ACL_COND_NONE;
1762 struct acl_cond *cond = NULL;
1763
1764 if (!strcmp(*args, "if")) {
1765 pol = ACL_COND_IF;
1766 args++;
1767 }
1768 else if (!strcmp(*args, "unless")) {
1769 pol = ACL_COND_UNLESS;
1770 args++;
1771 }
1772 else
1773 return NULL;
1774
1775 cond = parse_acl_cond(args, &px->acl, pol);
1776 if (!cond)
1777 return NULL;
1778
1779 cond->file = file;
1780 cond->line = line;
1781 px->acl_requires |= cond->requires;
1782
1783 return cond;
1784}
1785
Willy Tarreau11382812008-07-09 16:18:21 +02001786/* Execute condition <cond> and return either ACL_PAT_FAIL, ACL_PAT_MISS or
Willy Tarreaub6866442008-07-14 23:54:42 +02001787 * ACL_PAT_PASS depending on the test results. ACL_PAT_MISS may only be
1788 * returned if <dir> contains ACL_PARTIAL, indicating that incomplete data
1789 * is being examined.
1790 * This function only computes the condition, it does not apply the polarity
1791 * required by IF/UNLESS, it's up to the caller to do this using something like
1792 * this :
Willy Tarreau11382812008-07-09 16:18:21 +02001793 *
1794 * res = acl_pass(res);
Willy Tarreaub6866442008-07-14 23:54:42 +02001795 * if (res == ACL_PAT_MISS)
1796 * return 0;
Willy Tarreau11382812008-07-09 16:18:21 +02001797 * if (cond->pol == ACL_COND_UNLESS)
1798 * res = !res;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001799 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02001800int acl_exec_cond(struct acl_cond *cond, struct proxy *px, struct session *l4, void *l7, int dir)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001801{
1802 __label__ fetch_next;
1803 struct acl_term_suite *suite;
1804 struct acl_term *term;
1805 struct acl_expr *expr;
1806 struct acl *acl;
1807 struct acl_pattern *pattern;
1808 struct acl_test test;
Willy Tarreau11382812008-07-09 16:18:21 +02001809 int acl_res, suite_res, cond_res;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001810
Willy Tarreau11382812008-07-09 16:18:21 +02001811 /* We're doing a logical OR between conditions so we initialize to FAIL.
1812 * The MISS status is propagated down from the suites.
1813 */
Willy Tarreaua84d3742007-05-07 00:36:48 +02001814 cond_res = ACL_PAT_FAIL;
1815 list_for_each_entry(suite, &cond->suites, list) {
Willy Tarreau11382812008-07-09 16:18:21 +02001816 /* Evaluate condition suite <suite>. We stop at the first term
1817 * which returns ACL_PAT_FAIL. The MISS status is still propagated
1818 * in case of uncertainty in the result.
Willy Tarreaua84d3742007-05-07 00:36:48 +02001819 */
1820
1821 /* we're doing a logical AND between terms, so we must set the
1822 * initial value to PASS.
1823 */
1824 suite_res = ACL_PAT_PASS;
1825 list_for_each_entry(term, &suite->terms, list) {
1826 acl = term->acl;
1827
1828 /* FIXME: use cache !
1829 * check acl->cache_idx for this.
1830 */
1831
1832 /* ACL result not cached. Let's scan all the expressions
1833 * and use the first one to match.
1834 */
1835 acl_res = ACL_PAT_FAIL;
1836 list_for_each_entry(expr, &acl->expr, list) {
Willy Tarreaud41f8d82007-06-10 10:06:18 +02001837 /* we need to reset context and flags */
1838 memset(&test, 0, sizeof(test));
Willy Tarreaua84d3742007-05-07 00:36:48 +02001839 fetch_next:
Willy Tarreaub6866442008-07-14 23:54:42 +02001840 if (!expr->kw->fetch(px, l4, l7, dir, expr, &test)) {
1841 /* maybe we could not fetch because of missing data */
1842 if (test.flags & ACL_TEST_F_MAY_CHANGE && dir & ACL_PARTIAL)
1843 acl_res |= ACL_PAT_MISS;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001844 continue;
Willy Tarreaub6866442008-07-14 23:54:42 +02001845 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001846
Willy Tarreaua79534f2008-07-20 10:13:37 +02001847 if (test.flags & ACL_TEST_F_RES_SET) {
1848 if (test.flags & ACL_TEST_F_RES_PASS)
1849 acl_res |= ACL_PAT_PASS;
1850 else
1851 acl_res |= ACL_PAT_FAIL;
1852 }
1853 else {
Willy Tarreau020534d2010-05-16 21:45:45 +02001854 if (!eb_is_empty(&expr->pattern_tree)) {
Willy Tarreauc4262962010-05-10 23:42:40 +02001855 /* a tree is present, let's check what type it is */
1856 if (expr->kw->match == acl_match_str)
1857 acl_res |= acl_lookup_str(&test, expr) ? ACL_PAT_PASS : ACL_PAT_FAIL;
Willy Tarreaub337b532010-05-13 20:03:41 +02001858 else if (expr->kw->match == acl_match_ip)
1859 acl_res |= acl_lookup_ip(&test, expr) ? ACL_PAT_PASS : ACL_PAT_FAIL;
Willy Tarreauc4262962010-05-10 23:42:40 +02001860 }
1861
Willy Tarreaua79534f2008-07-20 10:13:37 +02001862 /* call the match() function for all tests on this value */
1863 list_for_each_entry(pattern, &expr->patterns, list) {
Willy Tarreaua79534f2008-07-20 10:13:37 +02001864 if (acl_res == ACL_PAT_PASS)
1865 break;
Willy Tarreaue56cda92010-05-11 23:25:05 +02001866 acl_res |= expr->kw->match(&test, pattern);
Willy Tarreaua79534f2008-07-20 10:13:37 +02001867 }
Krzysztof Piotr Oledzkid7528e52010-01-29 17:55:53 +01001868
Willy Tarreaue56cda92010-05-11 23:25:05 +02001869 if ((test.flags & ACL_TEST_F_NULL_MATCH) &&
Willy Tarreau020534d2010-05-16 21:45:45 +02001870 LIST_ISEMPTY(&expr->patterns) && eb_is_empty(&expr->pattern_tree))
Krzysztof Piotr Oledzkid7528e52010-01-29 17:55:53 +01001871 acl_res |= expr->kw->match(&test, NULL);
Willy Tarreaua84d3742007-05-07 00:36:48 +02001872 }
1873 /*
Willy Tarreau11382812008-07-09 16:18:21 +02001874 * OK now acl_res holds the result of this expression
1875 * as one of ACL_PAT_FAIL, ACL_PAT_MISS or ACL_PAT_PASS.
Willy Tarreaua84d3742007-05-07 00:36:48 +02001876 *
Willy Tarreau11382812008-07-09 16:18:21 +02001877 * Then if (!MISS) we can cache the result, and put
Willy Tarreaua84d3742007-05-07 00:36:48 +02001878 * (test.flags & ACL_TEST_F_VOLATILE) in the cache flags.
1879 *
1880 * FIXME: implement cache.
1881 *
1882 */
1883
1884 /* now we may have some cleanup to do */
1885 if (test.flags & ACL_TEST_F_MUST_FREE) {
Willy Tarreau664092c2011-12-16 19:11:42 +01001886 free(temp_pattern.data.str.str);
1887 temp_pattern.data.str.len = 0;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001888 }
1889
Willy Tarreau11382812008-07-09 16:18:21 +02001890 /* we're ORing these terms, so a single PASS is enough */
1891 if (acl_res == ACL_PAT_PASS)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001892 break;
1893
Willy Tarreaua84d3742007-05-07 00:36:48 +02001894 if (test.flags & ACL_TEST_F_FETCH_MORE)
1895 goto fetch_next;
Willy Tarreaub6866442008-07-14 23:54:42 +02001896
1897 /* sometimes we know the fetched data is subject to change
1898 * later and give another chance for a new match (eg: request
1899 * size, time, ...)
1900 */
1901 if (test.flags & ACL_TEST_F_MAY_CHANGE && dir & ACL_PARTIAL)
1902 acl_res |= ACL_PAT_MISS;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001903 }
1904 /*
1905 * Here we have the result of an ACL (cached or not).
1906 * ACLs are combined, negated or not, to form conditions.
1907 */
1908
Willy Tarreaua84d3742007-05-07 00:36:48 +02001909 if (term->neg)
Willy Tarreau11382812008-07-09 16:18:21 +02001910 acl_res = acl_neg(acl_res);
Willy Tarreaua84d3742007-05-07 00:36:48 +02001911
1912 suite_res &= acl_res;
Willy Tarreau11382812008-07-09 16:18:21 +02001913
1914 /* we're ANDing these terms, so a single FAIL is enough */
1915 if (suite_res == ACL_PAT_FAIL)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001916 break;
1917 }
1918 cond_res |= suite_res;
Willy Tarreau11382812008-07-09 16:18:21 +02001919
1920 /* we're ORing these terms, so a single PASS is enough */
1921 if (cond_res == ACL_PAT_PASS)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001922 break;
1923 }
Willy Tarreau11382812008-07-09 16:18:21 +02001924 return cond_res;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001925}
1926
1927
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001928/* Reports a pointer to the first ACL used in condition <cond> which requires
1929 * at least one of the USE_FLAGS in <require>. Returns NULL if none matches.
1930 * The construct is almost the same as for acl_exec_cond() since we're walking
1931 * down the ACL tree as well. It is important that the tree is really walked
1932 * through and never cached, because that way, this function can be used as a
1933 * late check.
1934 */
Willy Tarreauf1e98b82010-01-28 17:59:39 +01001935struct acl *cond_find_require(const struct acl_cond *cond, unsigned int require)
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001936{
1937 struct acl_term_suite *suite;
1938 struct acl_term *term;
1939 struct acl *acl;
1940
1941 list_for_each_entry(suite, &cond->suites, list) {
1942 list_for_each_entry(term, &suite->terms, list) {
1943 acl = term->acl;
1944 if (acl->requires & require)
1945 return acl;
1946 }
1947 }
1948 return NULL;
1949}
1950
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001951/*
1952 * Find targets for userlist and groups in acl. Function returns the number
1953 * of errors or OK if everything is fine.
1954 */
1955int
1956acl_find_targets(struct proxy *p)
1957{
1958
1959 struct acl *acl;
1960 struct acl_expr *expr;
1961 struct acl_pattern *pattern;
1962 struct userlist *ul;
Willy Tarreau63364ee2012-04-19 19:11:13 +02001963 struct arg *arg;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001964 int cfgerr = 0;
1965
1966 list_for_each_entry(acl, &p->acl, list) {
1967 list_for_each_entry(expr, &acl->expr, list) {
Willy Tarreau63364ee2012-04-19 19:11:13 +02001968 for (arg = expr->args; arg; arg++) {
1969 if (arg->type == ARGT_STOP)
1970 break;
1971 else if (arg->type == ARGT_SRV) {
1972 struct proxy *px;
1973 struct server *srv;
1974 char *pname, *sname;
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001975
Willy Tarreau63364ee2012-04-19 19:11:13 +02001976 if (!expr->args->data.str.len) {
1977 Alert("proxy %s: acl '%s' %s(): missing server name.\n",
1978 p->id, acl->name, expr->kw->kw);
1979 cfgerr++;
1980 continue;
1981 }
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001982
Willy Tarreau63364ee2012-04-19 19:11:13 +02001983 pname = expr->args->data.str.str;
1984 sname = strrchr(pname, '/');
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001985
Willy Tarreau63364ee2012-04-19 19:11:13 +02001986 if (sname)
1987 *sname++ = '\0';
1988 else {
1989 sname = pname;
1990 pname = NULL;
1991 }
1992
1993 px = p;
1994 if (pname) {
1995 px = findproxy(pname, PR_CAP_BE);
1996 if (!px) {
1997 Alert("proxy %s: acl '%s' %s(): unable to find proxy '%s'.\n",
1998 p->id, acl->name, expr->kw->kw, pname);
1999 cfgerr++;
2000 continue;
2001 }
2002 }
Willy Tarreau0b1cd942010-05-16 22:18:27 +02002003
Willy Tarreau63364ee2012-04-19 19:11:13 +02002004 srv = findserver(px, sname);
2005 if (!srv) {
2006 Alert("proxy %s: acl '%s' %s(): unable to find server '%s'.\n",
2007 p->id, acl->name, expr->kw->kw, sname);
Willy Tarreau0b1cd942010-05-16 22:18:27 +02002008 cfgerr++;
2009 continue;
2010 }
Willy Tarreau0b1cd942010-05-16 22:18:27 +02002011
Willy Tarreau63364ee2012-04-19 19:11:13 +02002012 free(expr->args->data.str.str);
2013 expr->args->data.srv = srv;
Willy Tarreaud28c3532012-04-19 19:28:33 +02002014 }
2015 else if (arg->type == ARGT_FE) {
2016 struct proxy *prx;
2017 char *pname;
2018
2019 if (!expr->args->data.str.len) {
2020 Alert("proxy %s: acl '%s' %s(): missing frontend name.\n",
2021 p->id, acl->name, expr->kw->kw);
2022 cfgerr++;
2023 continue;
2024 }
2025
2026 pname = expr->args->data.str.str;
2027 prx = findproxy(pname, PR_CAP_FE);
2028 if (!prx) {
2029 Alert("proxy %s: acl '%s' %s(): unable to find frontend '%s'.\n",
2030 p->id, acl->name, expr->kw->kw, pname);
2031 cfgerr++;
2032 continue;
2033 }
2034
2035 free(expr->args->data.str.str);
2036 expr->args->data.prx = prx;
2037 }
2038 else if (arg->type == ARGT_BE) {
2039 struct proxy *prx;
2040 char *pname;
2041
2042 if (!expr->args->data.str.len) {
2043 Alert("proxy %s: acl '%s' %s(): missing backend name.\n",
2044 p->id, acl->name, expr->kw->kw);
2045 cfgerr++;
2046 continue;
2047 }
2048
2049 pname = expr->args->data.str.str;
2050 prx = findproxy(pname, PR_CAP_BE);
2051 if (!prx) {
2052 Alert("proxy %s: acl '%s' %s(): unable to find backend '%s'.\n",
2053 p->id, acl->name, expr->kw->kw, pname);
2054 cfgerr++;
2055 continue;
2056 }
2057
2058 free(expr->args->data.str.str);
2059 expr->args->data.prx = prx;
2060 }
2061 else if (arg->type == ARGT_TAB) {
2062 struct proxy *prx;
2063 char *pname;
2064
2065 if (!expr->args->data.str.len) {
2066 Alert("proxy %s: acl '%s' %s(): missing table name.\n",
2067 p->id, acl->name, expr->kw->kw);
2068 cfgerr++;
2069 continue;
2070 }
2071
2072 pname = expr->args->data.str.str;
2073 prx = find_stktable(pname);
2074 if (!prx) {
2075 Alert("proxy %s: acl '%s' %s(): unable to find table '%s'.\n",
2076 p->id, acl->name, expr->kw->kw, pname);
2077 cfgerr++;
2078 continue;
2079 }
2080
2081 free(expr->args->data.str.str);
2082 expr->args->data.prx = prx;
Willy Tarreau0b1cd942010-05-16 22:18:27 +02002083 }
Willy Tarreau63364ee2012-04-19 19:11:13 +02002084 else if (arg->type == ARGT_USR) {
2085 if (!expr->args->data.str.len) {
2086 Alert("proxy %s: acl '%s' %s(): missing userlist name.\n",
2087 p->id, acl->name, expr->kw->kw);
2088 cfgerr++;
2089 continue;
2090 }
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01002091
Willy Tarreau63364ee2012-04-19 19:11:13 +02002092 if (p->uri_auth && p->uri_auth->userlist &&
2093 !strcmp(p->uri_auth->userlist->name, expr->args->data.str.str))
2094 ul = p->uri_auth->userlist;
2095 else
2096 ul = auth_find_userlist(expr->args->data.str.str);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01002097
Willy Tarreau63364ee2012-04-19 19:11:13 +02002098 if (!ul) {
2099 Alert("proxy %s: acl '%s' %s(%s): unable to find userlist.\n",
2100 p->id, acl->name, expr->kw->kw, expr->args->data.str.str);
2101 cfgerr++;
2102 continue;
2103 }
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01002104
Willy Tarreau63364ee2012-04-19 19:11:13 +02002105 free(expr->args->data.str.str);
2106 expr->args->data.usr = ul;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01002107 }
Willy Tarreau63364ee2012-04-19 19:11:13 +02002108 } /* end of args processing */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01002109
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01002110
2111 if (!strcmp(expr->kw->kw, "http_auth_group")) {
Willy Tarreau63364ee2012-04-19 19:11:13 +02002112 /* note: argument resolved above thanks to ARGT_USR */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01002113
2114 if (LIST_ISEMPTY(&expr->patterns)) {
2115 Alert("proxy %s: acl %s %s(): no groups specified.\n",
2116 p->id, acl->name, expr->kw->kw);
2117 cfgerr++;
2118 continue;
2119 }
2120
2121 list_for_each_entry(pattern, &expr->patterns, list) {
Willy Tarreau34db1082012-04-19 17:16:54 +02002122 pattern->val.group_mask = auth_resolve_groups(expr->args->data.usr, pattern->ptr.str);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01002123
2124 free(pattern->ptr.str);
2125 pattern->ptr.str = NULL;
2126 pattern->len = 0;
2127
2128 if (!pattern->val.group_mask) {
2129 Alert("proxy %s: acl %s %s(): invalid group(s).\n",
2130 p->id, acl->name, expr->kw->kw);
2131 cfgerr++;
2132 continue;
2133 }
2134 }
2135 }
2136 }
2137 }
2138
2139 return cfgerr;
2140}
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02002141
Willy Tarreaua84d3742007-05-07 00:36:48 +02002142/************************************************************************/
2143/* All supported keywords must be declared here. */
2144/************************************************************************/
2145
Willy Tarreau61612d42012-04-19 18:42:05 +02002146/* Note: must not be declared <const> as its list will be overwritten.
2147 * Please take care of keeping this list alphabetically sorted.
2148 */
Willy Tarreaua84d3742007-05-07 00:36:48 +02002149static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau61612d42012-04-19 18:42:05 +02002150 { "always_false", acl_parse_nothing, acl_fetch_false, acl_match_nothing, ACL_USE_NOTHING, 0 },
2151 { "always_true", acl_parse_nothing, acl_fetch_true, acl_match_nothing, ACL_USE_NOTHING, 0 },
2152 { "rep_ssl_hello_type", acl_parse_int, acl_fetch_ssl_hello_type, acl_match_int, ACL_USE_L6RTR_VOLATILE, 0 },
2153 { "req_len", acl_parse_int, acl_fetch_req_len, acl_match_int, ACL_USE_L6REQ_VOLATILE, 0 },
2154 { "req_rdp_cookie", acl_parse_str, acl_fetch_rdp_cookie, acl_match_str, ACL_USE_L6REQ_VOLATILE|ACL_MAY_LOOKUP, ARG1(0,STR) },
2155 { "req_rdp_cookie_cnt", acl_parse_int, acl_fetch_rdp_cookie_cnt, acl_match_int, ACL_USE_L6REQ_VOLATILE, ARG1(0,STR) },
2156 { "req_ssl_hello_type", acl_parse_int, acl_fetch_ssl_hello_type, acl_match_int, ACL_USE_L6REQ_VOLATILE, 0 },
2157 { "req_ssl_sni", acl_parse_str, acl_fetch_ssl_hello_sni, acl_match_str, ACL_USE_L6REQ_VOLATILE|ACL_MAY_LOOKUP, 0 },
2158 { "req_ssl_ver", acl_parse_dotted_ver, acl_fetch_req_ssl_ver, acl_match_int, ACL_USE_L6REQ_VOLATILE, 0 },
2159 { "wait_end", acl_parse_nothing, acl_fetch_wait_end, acl_match_nothing, ACL_USE_NOTHING, 0 },
Willy Tarreaua84d3742007-05-07 00:36:48 +02002160 { NULL, NULL, NULL, NULL }
2161}};
2162
2163
2164__attribute__((constructor))
2165static void __acl_init(void)
2166{
2167 acl_register_keywords(&acl_kws);
2168}
2169
2170
2171/*
2172 * Local variables:
2173 * c-indent-level: 8
2174 * c-basic-offset: 8
2175 * End:
2176 */