blob: 9c4ce0ca53dea9eb7caf26a2e6d7f06ed0a00e93 [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,
Willy Tarreau37406352012-04-23 16:16:37 +020069 struct acl_expr *expr, struct sample *smp)
Willy Tarreaua5909832007-06-17 20:40:25 +020070{
Willy Tarreauf853c462012-04-23 18:53:56 +020071 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +020072 smp->data.uint = 1;
Willy Tarreaua5909832007-06-17 20:40:25 +020073 return 1;
74}
75
Willy Tarreaub6fb4202008-07-20 11:18:28 +020076/* wait for more data as long as possible, then return TRUE. This should be
77 * used with content inspection.
78 */
79static int
80acl_fetch_wait_end(struct proxy *px, struct session *l4, void *l7, int dir,
Willy Tarreau37406352012-04-23 16:16:37 +020081 struct acl_expr *expr, struct sample *smp)
Willy Tarreaub6fb4202008-07-20 11:18:28 +020082{
83 if (dir & ACL_PARTIAL) {
Willy Tarreau37406352012-04-23 16:16:37 +020084 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreaub6fb4202008-07-20 11:18:28 +020085 return 0;
86 }
Willy Tarreauf853c462012-04-23 18:53:56 +020087 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +020088 smp->data.uint = 1;
Willy Tarreaub6fb4202008-07-20 11:18:28 +020089 return 1;
90}
91
Willy Tarreau58393e12008-07-20 10:39:22 +020092/* force FALSE to be returned at the fetch level */
Willy Tarreaua5909832007-06-17 20:40:25 +020093static int
Willy Tarreau58393e12008-07-20 10:39:22 +020094acl_fetch_false(struct proxy *px, struct session *l4, void *l7, int dir,
Willy Tarreau37406352012-04-23 16:16:37 +020095 struct acl_expr *expr, struct sample *smp)
Willy Tarreaua84d3742007-05-07 00:36:48 +020096{
Willy Tarreauf853c462012-04-23 18:53:56 +020097 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +020098 smp->data.uint = 0;
Willy Tarreaua84d3742007-05-07 00:36:48 +020099 return 1;
100}
101
Willy Tarreau44b90cc2010-05-24 20:27:29 +0200102/* return the number of bytes in the request buffer */
103static int
104acl_fetch_req_len(struct proxy *px, struct session *l4, void *l7, int dir,
Willy Tarreau37406352012-04-23 16:16:37 +0200105 struct acl_expr *expr, struct sample *smp)
Willy Tarreau44b90cc2010-05-24 20:27:29 +0200106{
107 if (!l4 || !l4->req)
108 return 0;
109
Willy Tarreauf853c462012-04-23 18:53:56 +0200110 smp->type = SMP_T_UINT;
111 smp->data.uint = l4->req->i;
Willy Tarreau37406352012-04-23 16:16:37 +0200112 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
Willy Tarreau44b90cc2010-05-24 20:27:29 +0200113 return 1;
114}
115
Emeric Brun38e71762010-09-23 17:59:18 +0200116
117static int
118acl_fetch_ssl_hello_type(struct proxy *px, struct session *l4, void *l7, int dir,
Willy Tarreau37406352012-04-23 16:16:37 +0200119 struct acl_expr *expr, struct sample *smp)
Emeric Brun38e71762010-09-23 17:59:18 +0200120{
121 int hs_len;
122 int hs_type, bleft;
123 struct buffer *b;
124 const unsigned char *data;
125
126 if (!l4)
127 goto not_ssl_hello;
128
129 b = ((dir & ACL_DIR_MASK) == ACL_DIR_RTR) ? l4->rep : l4->req;
130
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100131 bleft = b->i;
Willy Tarreau89fa7062012-03-02 16:13:16 +0100132 data = (const unsigned char *)b->p;
Emeric Brun38e71762010-09-23 17:59:18 +0200133
134 if (!bleft)
135 goto too_short;
136
137 if ((*data >= 0x14 && *data <= 0x17) || (*data == 0xFF)) {
138 /* SSLv3 header format */
139 if (bleft < 9)
140 goto too_short;
141
142 /* ssl version 3 */
143 if ((data[1] << 16) + data[2] < 0x00030000)
144 goto not_ssl_hello;
145
146 /* ssl message len must present handshake type and len */
147 if ((data[3] << 8) + data[4] < 4)
148 goto not_ssl_hello;
149
150 /* format introduced with SSLv3 */
151
152 hs_type = (int)data[5];
153 hs_len = ( data[6] << 16 ) + ( data[7] << 8 ) + data[8];
154
155 /* not a full handshake */
156 if (bleft < (9 + hs_len))
157 goto too_short;
158
159 }
160 else {
161 goto not_ssl_hello;
162 }
163
Willy Tarreauf853c462012-04-23 18:53:56 +0200164 smp->type = SMP_T_UINT;
165 smp->data.uint = hs_type;
Willy Tarreau37406352012-04-23 16:16:37 +0200166 smp->flags = SMP_F_VOLATILE;
Emeric Brun38e71762010-09-23 17:59:18 +0200167
168 return 1;
169
170 too_short:
Willy Tarreau37406352012-04-23 16:16:37 +0200171 smp->flags = SMP_F_MAY_CHANGE;
Emeric Brun38e71762010-09-23 17:59:18 +0200172
173 not_ssl_hello:
174
175 return 0;
176}
177
Willy Tarreau44b90cc2010-05-24 20:27:29 +0200178/* Return the version of the SSL protocol in the request. It supports both
179 * SSLv3 (TLSv1) header format for any message, and SSLv2 header format for
180 * the hello message. The SSLv3 format is described in RFC 2246 p49, and the
181 * SSLv2 format is described here, and completed p67 of RFC 2246 :
182 * http://wp.netscape.com/eng/security/SSL_2.html
183 *
184 * Note: this decoder only works with non-wrapping data.
185 */
186static int
187acl_fetch_req_ssl_ver(struct proxy *px, struct session *l4, void *l7, int dir,
Willy Tarreau37406352012-04-23 16:16:37 +0200188 struct acl_expr *expr, struct sample *smp)
Willy Tarreau44b90cc2010-05-24 20:27:29 +0200189{
190 int version, bleft, msg_len;
191 const unsigned char *data;
192
193 if (!l4 || !l4->req)
194 return 0;
195
196 msg_len = 0;
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100197 bleft = l4->req->i;
Willy Tarreau44b90cc2010-05-24 20:27:29 +0200198 if (!bleft)
199 goto too_short;
200
Willy Tarreau89fa7062012-03-02 16:13:16 +0100201 data = (const unsigned char *)l4->req->p;
Willy Tarreau44b90cc2010-05-24 20:27:29 +0200202 if ((*data >= 0x14 && *data <= 0x17) || (*data == 0xFF)) {
203 /* SSLv3 header format */
204 if (bleft < 5)
205 goto too_short;
206
207 version = (data[1] << 16) + data[2]; /* version: major, minor */
208 msg_len = (data[3] << 8) + data[4]; /* record length */
209
210 /* format introduced with SSLv3 */
211 if (version < 0x00030000)
212 goto not_ssl;
213
214 /* message length between 1 and 2^14 + 2048 */
215 if (msg_len < 1 || msg_len > ((1<<14) + 2048))
216 goto not_ssl;
217
218 bleft -= 5; data += 5;
219 } else {
220 /* SSLv2 header format, only supported for hello (msg type 1) */
221 int rlen, plen, cilen, silen, chlen;
222
223 if (*data & 0x80) {
224 if (bleft < 3)
225 goto too_short;
226 /* short header format : 15 bits for length */
227 rlen = ((data[0] & 0x7F) << 8) | data[1];
228 plen = 0;
229 bleft -= 2; data += 2;
230 } else {
231 if (bleft < 4)
232 goto too_short;
233 /* long header format : 14 bits for length + pad length */
234 rlen = ((data[0] & 0x3F) << 8) | data[1];
235 plen = data[2];
236 bleft -= 3; data += 2;
237 }
238
239 if (*data != 0x01)
240 goto not_ssl;
241 bleft--; data++;
242
243 if (bleft < 8)
244 goto too_short;
245 version = (data[0] << 16) + data[1]; /* version: major, minor */
246 cilen = (data[2] << 8) + data[3]; /* cipher len, multiple of 3 */
247 silen = (data[4] << 8) + data[5]; /* session_id_len: 0 or 16 */
248 chlen = (data[6] << 8) + data[7]; /* 16<=challenge length<=32 */
249
250 bleft -= 8; data += 8;
251 if (cilen % 3 != 0)
252 goto not_ssl;
253 if (silen && silen != 16)
254 goto not_ssl;
255 if (chlen < 16 || chlen > 32)
256 goto not_ssl;
257 if (rlen != 9 + cilen + silen + chlen)
258 goto not_ssl;
259
260 /* focus on the remaining data length */
261 msg_len = cilen + silen + chlen + plen;
262 }
263 /* We could recursively check that the buffer ends exactly on an SSL
264 * fragment boundary and that a possible next segment is still SSL,
265 * but that's a bit pointless. However, we could still check that
266 * all the part of the request which fits in a buffer is already
267 * there.
268 */
Willy Tarreau89fa7062012-03-02 16:13:16 +0100269 if (msg_len > buffer_max_len(l4->req) + l4->req->data - l4->req->p)
270 msg_len = buffer_max_len(l4->req) + l4->req->data - l4->req->p;
Willy Tarreau44b90cc2010-05-24 20:27:29 +0200271
272 if (bleft < msg_len)
273 goto too_short;
274
275 /* OK that's enough. We have at least the whole message, and we have
276 * the protocol version.
277 */
Willy Tarreauf853c462012-04-23 18:53:56 +0200278 smp->type = SMP_T_UINT;
279 smp->data.uint = version;
Willy Tarreau37406352012-04-23 16:16:37 +0200280 smp->flags = SMP_F_VOLATILE;
Willy Tarreau44b90cc2010-05-24 20:27:29 +0200281 return 1;
282
283 too_short:
Willy Tarreau37406352012-04-23 16:16:37 +0200284 smp->flags = SMP_F_MAY_CHANGE;
Willy Tarreau44b90cc2010-05-24 20:27:29 +0200285 not_ssl:
286 return 0;
287}
288
Willy Tarreaub6672b52011-12-12 17:23:41 +0100289/* Try to extract the Server Name Indication that may be presented in a TLS
290 * client hello handshake message. The format of the message is the following
291 * (cf RFC5246 + RFC6066) :
292 * TLS frame :
293 * - uint8 type = 0x16 (Handshake)
294 * - uint16 version >= 0x0301 (TLSv1)
295 * - uint16 length (frame length)
296 * - TLS handshake :
297 * - uint8 msg_type = 0x01 (ClientHello)
298 * - uint24 length (handshake message length)
299 * - ClientHello :
300 * - uint16 client_version >= 0x0301 (TLSv1)
Willy Tarreaud017f112012-04-09 09:24:11 +0200301 * - uint8 Random[32] (4 first ones are timestamp)
Willy Tarreaub6672b52011-12-12 17:23:41 +0100302 * - SessionID :
303 * - uint8 session_id_len (0..32) (SessionID len in bytes)
304 * - uint8 session_id[session_id_len]
305 * - CipherSuite :
306 * - uint16 cipher_len >= 2 (Cipher length in bytes)
307 * - uint16 ciphers[cipher_len/2]
308 * - CompressionMethod :
309 * - uint8 compression_len >= 1 (# of supported methods)
310 * - uint8 compression_methods[compression_len]
311 * - optional client_extension_len (in bytes)
312 * - optional sequence of ClientHelloExtensions (as many bytes as above):
313 * - uint16 extension_type = 0 for server_name
314 * - uint16 extension_len
315 * - opaque extension_data[extension_len]
316 * - uint16 server_name_list_len (# of bytes here)
317 * - opaque server_names[server_name_list_len bytes]
318 * - uint8 name_type = 0 for host_name
319 * - uint16 name_len
320 * - opaque hostname[name_len bytes]
321 */
322static int
323acl_fetch_ssl_hello_sni(struct proxy *px, struct session *l4, void *l7, int dir,
Willy Tarreau37406352012-04-23 16:16:37 +0200324 struct acl_expr *expr, struct sample *smp)
Willy Tarreaub6672b52011-12-12 17:23:41 +0100325{
326 int hs_len, ext_len, bleft;
327 struct buffer *b;
328 unsigned char *data;
329
330 if (!l4)
331 goto not_ssl_hello;
332
333 b = ((dir & ACL_DIR_MASK) == ACL_DIR_RTR) ? l4->rep : l4->req;
334
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100335 bleft = b->i;
Willy Tarreau89fa7062012-03-02 16:13:16 +0100336 data = (unsigned char *)b->p;
Willy Tarreaub6672b52011-12-12 17:23:41 +0100337
338 /* Check for SSL/TLS Handshake */
339 if (!bleft)
340 goto too_short;
341 if (*data != 0x16)
342 goto not_ssl_hello;
343
344 /* Check for TLSv1 or later (SSL version >= 3.1) */
345 if (bleft < 3)
346 goto too_short;
347 if (data[1] < 0x03 || data[2] < 0x01)
348 goto not_ssl_hello;
349
350 if (bleft < 5)
351 goto too_short;
352 hs_len = (data[3] << 8) + data[4];
353 if (hs_len < 1 + 3 + 2 + 32 + 1 + 2 + 2 + 1 + 1 + 2 + 2)
354 goto not_ssl_hello; /* too short to have an extension */
355
356 data += 5; /* enter TLS handshake */
357 bleft -= 5;
358
359 /* Check for a complete client hello starting at <data> */
360 if (bleft < 1)
361 goto too_short;
362 if (data[0] != 0x01) /* msg_type = Client Hello */
363 goto not_ssl_hello;
364
365 /* Check the Hello's length */
366 if (bleft < 4)
367 goto too_short;
368 hs_len = (data[1] << 16) + (data[2] << 8) + data[3];
369 if (hs_len < 2 + 32 + 1 + 2 + 2 + 1 + 1 + 2 + 2)
370 goto not_ssl_hello; /* too short to have an extension */
371
372 /* We want the full handshake here */
373 if (bleft < hs_len)
374 goto too_short;
375
376 data += 4;
377 /* Start of the ClientHello message */
378 if (data[0] < 0x03 || data[1] < 0x01) /* TLSv1 minimum */
379 goto not_ssl_hello;
380
Willy Tarreaud017f112012-04-09 09:24:11 +0200381 ext_len = data[34]; /* session_id_len */
Willy Tarreaub6672b52011-12-12 17:23:41 +0100382 if (ext_len > 32 || ext_len > (hs_len - 35)) /* check for correct session_id len */
383 goto not_ssl_hello;
384
385 /* Jump to cipher suite */
386 hs_len -= 35 + ext_len;
387 data += 35 + ext_len;
388
389 if (hs_len < 4 || /* minimum one cipher */
390 (ext_len = (data[0] << 8) + data[1]) < 2 || /* minimum 2 bytes for a cipher */
391 ext_len > hs_len)
392 goto not_ssl_hello;
393
394 /* Jump to the compression methods */
395 hs_len -= 2 + ext_len;
396 data += 2 + ext_len;
397
398 if (hs_len < 2 || /* minimum one compression method */
399 data[0] < 1 || data[0] > hs_len) /* minimum 1 bytes for a method */
400 goto not_ssl_hello;
401
402 /* Jump to the extensions */
403 hs_len -= 1 + data[0];
404 data += 1 + data[0];
405
406 if (hs_len < 2 || /* minimum one extension list length */
407 (ext_len = (data[0] << 8) + data[1]) > hs_len - 2) /* list too long */
408 goto not_ssl_hello;
409
410 hs_len = ext_len; /* limit ourselves to the extension length */
411 data += 2;
412
413 while (hs_len >= 4) {
414 int ext_type, name_type, srv_len, name_len;
415
416 ext_type = (data[0] << 8) + data[1];
417 ext_len = (data[2] << 8) + data[3];
418
419 if (ext_len > hs_len - 4) /* Extension too long */
420 goto not_ssl_hello;
421
422 if (ext_type == 0) { /* Server name */
423 if (ext_len < 2) /* need one list length */
424 goto not_ssl_hello;
425
426 srv_len = (data[4] << 8) + data[5];
427 if (srv_len < 4 || srv_len > hs_len - 6)
428 goto not_ssl_hello; /* at least 4 bytes per server name */
429
430 name_type = data[6];
431 name_len = (data[7] << 8) + data[8];
432
433 if (name_type == 0) { /* hostname */
Willy Tarreauf853c462012-04-23 18:53:56 +0200434 smp->type = SMP_T_CSTR;
435 smp->data.str.str = (char *)data + 9;
436 smp->data.str.len = name_len;
Willy Tarreau37406352012-04-23 16:16:37 +0200437 smp->flags = SMP_F_VOLATILE;
Willy Tarreaub6672b52011-12-12 17:23:41 +0100438 return 1;
439 }
440 }
441
442 hs_len -= 4 + ext_len;
443 data += 4 + ext_len;
444 }
445 /* server name not found */
446 goto not_ssl_hello;
447
448 too_short:
Willy Tarreau37406352012-04-23 16:16:37 +0200449 smp->flags = SMP_F_MAY_CHANGE;
Willy Tarreaub6672b52011-12-12 17:23:41 +0100450
451 not_ssl_hello:
452
453 return 0;
454}
455
Willy Tarreau44b90cc2010-05-24 20:27:29 +0200456/* Fetch the RDP cookie identified in the expression.
457 * Note: this decoder only works with non-wrapping data.
Willy Tarreau34db1082012-04-19 17:16:54 +0200458 * Accepts either 0 or 1 argument. Argument is a string (cookie name), other
459 * types will lead to undefined behaviour.
Willy Tarreau44b90cc2010-05-24 20:27:29 +0200460 */
461int
462acl_fetch_rdp_cookie(struct proxy *px, struct session *l4, void *l7, int dir,
Willy Tarreau37406352012-04-23 16:16:37 +0200463 struct acl_expr *expr, struct sample *smp)
Willy Tarreau44b90cc2010-05-24 20:27:29 +0200464{
465 int bleft;
466 const unsigned char *data;
467
468 if (!l4 || !l4->req)
469 return 0;
470
Willy Tarreau37406352012-04-23 16:16:37 +0200471 smp->flags = 0;
Willy Tarreauf853c462012-04-23 18:53:56 +0200472 smp->type = SMP_T_CSTR;
Willy Tarreau44b90cc2010-05-24 20:27:29 +0200473
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100474 bleft = l4->req->i;
Willy Tarreau44b90cc2010-05-24 20:27:29 +0200475 if (bleft <= 11)
476 goto too_short;
477
Willy Tarreau89fa7062012-03-02 16:13:16 +0100478 data = (const unsigned char *)l4->req->p + 11;
Willy Tarreau44b90cc2010-05-24 20:27:29 +0200479 bleft -= 11;
480
481 if (bleft <= 7)
482 goto too_short;
483
484 if (strncasecmp((const char *)data, "Cookie:", 7) != 0)
485 goto not_cookie;
486
487 data += 7;
488 bleft -= 7;
489
490 while (bleft > 0 && *data == ' ') {
491 data++;
492 bleft--;
493 }
494
Willy Tarreau34db1082012-04-19 17:16:54 +0200495 if (expr->args) {
Willy Tarreau44b90cc2010-05-24 20:27:29 +0200496
Willy Tarreau34db1082012-04-19 17:16:54 +0200497 if (bleft <= expr->args->data.str.len)
Willy Tarreau44b90cc2010-05-24 20:27:29 +0200498 goto too_short;
499
Willy Tarreau34db1082012-04-19 17:16:54 +0200500 if ((data[expr->args->data.str.len] != '=') ||
501 strncasecmp(expr->args->data.str.str, (const char *)data, expr->args->data.str.len) != 0)
Willy Tarreau44b90cc2010-05-24 20:27:29 +0200502 goto not_cookie;
503
Willy Tarreau34db1082012-04-19 17:16:54 +0200504 data += expr->args->data.str.len + 1;
505 bleft -= expr->args->data.str.len + 1;
Willy Tarreau44b90cc2010-05-24 20:27:29 +0200506 } else {
507 while (bleft > 0 && *data != '=') {
508 if (*data == '\r' || *data == '\n')
509 goto not_cookie;
510 data++;
511 bleft--;
512 }
513
514 if (bleft < 1)
515 goto too_short;
516
517 if (*data != '=')
518 goto not_cookie;
519
520 data++;
521 bleft--;
522 }
523
524 /* data points to cookie value */
Willy Tarreauf853c462012-04-23 18:53:56 +0200525 smp->data.str.str = (char *)data;
526 smp->data.str.len = 0;
Willy Tarreau44b90cc2010-05-24 20:27:29 +0200527
528 while (bleft > 0 && *data != '\r') {
529 data++;
530 bleft--;
531 }
532
533 if (bleft < 2)
534 goto too_short;
535
536 if (data[0] != '\r' || data[1] != '\n')
537 goto not_cookie;
538
Willy Tarreauf853c462012-04-23 18:53:56 +0200539 smp->data.str.len = (char *)data - smp->data.str.str;
Willy Tarreau37406352012-04-23 16:16:37 +0200540 smp->flags = SMP_F_VOLATILE;
Willy Tarreau44b90cc2010-05-24 20:27:29 +0200541 return 1;
542
543 too_short:
Willy Tarreau37406352012-04-23 16:16:37 +0200544 smp->flags = SMP_F_MAY_CHANGE;
Willy Tarreau44b90cc2010-05-24 20:27:29 +0200545 not_cookie:
546 return 0;
547}
548
549static int
550acl_fetch_rdp_cookie_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
Willy Tarreau37406352012-04-23 16:16:37 +0200551 struct acl_expr *expr, struct sample *smp)
Willy Tarreau44b90cc2010-05-24 20:27:29 +0200552{
553 int ret;
554
Willy Tarreau37406352012-04-23 16:16:37 +0200555 ret = acl_fetch_rdp_cookie(px, l4, l7, dir, expr, smp);
Willy Tarreau44b90cc2010-05-24 20:27:29 +0200556
Willy Tarreau37406352012-04-23 16:16:37 +0200557 if (smp->flags & SMP_F_MAY_CHANGE)
Willy Tarreau44b90cc2010-05-24 20:27:29 +0200558 return 0;
559
Willy Tarreau37406352012-04-23 16:16:37 +0200560 smp->flags = SMP_F_VOLATILE;
Willy Tarreauf853c462012-04-23 18:53:56 +0200561 smp->type = SMP_T_UINT;
562 smp->data.uint = ret;
Willy Tarreau44b90cc2010-05-24 20:27:29 +0200563
564 return 1;
565}
566
Willy Tarreau58393e12008-07-20 10:39:22 +0200567
568/*
569 * These functions are exported and may be used by any other component.
570 */
571
572/* ignore the current line */
573int acl_parse_nothing(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreaua5909832007-06-17 20:40:25 +0200574{
Willy Tarreau58393e12008-07-20 10:39:22 +0200575 return 1;
576}
577
578/* always fake a data retrieval */
579int acl_fetch_nothing(struct proxy *px, struct session *l4, void *l7, int dir,
Willy Tarreau37406352012-04-23 16:16:37 +0200580 struct acl_expr *expr, struct sample *smp)
Willy Tarreau58393e12008-07-20 10:39:22 +0200581{
582 return 1;
Willy Tarreaua5909832007-06-17 20:40:25 +0200583}
584
585/* always return false */
Willy Tarreau37406352012-04-23 16:16:37 +0200586int acl_match_nothing(struct sample *smp, struct acl_pattern *pattern)
Willy Tarreaua5909832007-06-17 20:40:25 +0200587{
Willy Tarreau11382812008-07-09 16:18:21 +0200588 return ACL_PAT_FAIL;
Willy Tarreaua5909832007-06-17 20:40:25 +0200589}
590
591
Willy Tarreaua84d3742007-05-07 00:36:48 +0200592/* NB: For two strings to be identical, it is required that their lengths match */
Willy Tarreau37406352012-04-23 16:16:37 +0200593int acl_match_str(struct sample *smp, struct acl_pattern *pattern)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200594{
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200595 int icase;
596
Willy Tarreauf853c462012-04-23 18:53:56 +0200597 if (pattern->len != smp->data.str.len)
Willy Tarreau11382812008-07-09 16:18:21 +0200598 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200599
600 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
Willy Tarreauf853c462012-04-23 18:53:56 +0200601 if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) == 0) ||
602 (!icase && strncmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) == 0))
Willy Tarreau11382812008-07-09 16:18:21 +0200603 return ACL_PAT_PASS;
604 return ACL_PAT_FAIL;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200605}
606
Willy Tarreauc4262962010-05-10 23:42:40 +0200607/* Lookup a string in the expression's pattern tree. The node is returned if it
608 * exists, otherwise NULL.
609 */
Willy Tarreau37406352012-04-23 16:16:37 +0200610static void *acl_lookup_str(struct sample *smp, struct acl_expr *expr)
Willy Tarreauc4262962010-05-10 23:42:40 +0200611{
612 /* data are stored in a tree */
613 struct ebmb_node *node;
614 char prev;
615
616 /* we may have to force a trailing zero on the test pattern */
Willy Tarreauf853c462012-04-23 18:53:56 +0200617 prev = smp->data.str.str[smp->data.str.len];
Willy Tarreauc4262962010-05-10 23:42:40 +0200618 if (prev)
Willy Tarreauf853c462012-04-23 18:53:56 +0200619 smp->data.str.str[smp->data.str.len] = '\0';
620 node = ebst_lookup(&expr->pattern_tree, smp->data.str.str);
Willy Tarreauc4262962010-05-10 23:42:40 +0200621 if (prev)
Willy Tarreauf853c462012-04-23 18:53:56 +0200622 smp->data.str.str[smp->data.str.len] = prev;
Willy Tarreauc4262962010-05-10 23:42:40 +0200623 return node;
624}
625
Willy Tarreauf3d25982007-05-08 22:45:09 +0200626/* Executes a regex. It needs to change the data. If it is marked READ_ONLY
627 * then it will be allocated and duplicated in place so that others may use
628 * it later on. Note that this is embarrassing because we always try to avoid
629 * allocating memory at run time.
630 */
Willy Tarreau37406352012-04-23 16:16:37 +0200631int acl_match_reg(struct sample *smp, struct acl_pattern *pattern)
Willy Tarreauf3d25982007-05-08 22:45:09 +0200632{
633 char old_char;
634 int ret;
635
Willy Tarreau37406352012-04-23 16:16:37 +0200636 if (unlikely(smp->flags & SMP_F_READ_ONLY)) {
Willy Tarreauf3d25982007-05-08 22:45:09 +0200637 char *new_str;
638
Willy Tarreauf853c462012-04-23 18:53:56 +0200639 new_str = calloc(1, smp->data.str.len + 1);
Willy Tarreauf3d25982007-05-08 22:45:09 +0200640 if (!new_str)
Willy Tarreau11382812008-07-09 16:18:21 +0200641 return ACL_PAT_FAIL;
Willy Tarreauf3d25982007-05-08 22:45:09 +0200642
Willy Tarreauf853c462012-04-23 18:53:56 +0200643 memcpy(new_str, smp->data.str.str, smp->data.str.len);
644 new_str[smp->data.str.len] = 0;
Willy Tarreau37406352012-04-23 16:16:37 +0200645 if (smp->flags & SMP_F_MUST_FREE)
Willy Tarreauf853c462012-04-23 18:53:56 +0200646 free(smp->data.str.str);
647 smp->data.str.str = new_str;
Willy Tarreau37406352012-04-23 16:16:37 +0200648 smp->flags |= SMP_F_MUST_FREE;
649 smp->flags &= ~SMP_F_READ_ONLY;
Willy Tarreauf3d25982007-05-08 22:45:09 +0200650 }
651
Willy Tarreauf853c462012-04-23 18:53:56 +0200652 old_char = smp->data.str.str[smp->data.str.len];
653 smp->data.str.str[smp->data.str.len] = 0;
Willy Tarreauf3d25982007-05-08 22:45:09 +0200654
Willy Tarreauf853c462012-04-23 18:53:56 +0200655 if (regexec(pattern->ptr.reg, smp->data.str.str, 0, NULL, 0) == 0)
Willy Tarreau11382812008-07-09 16:18:21 +0200656 ret = ACL_PAT_PASS;
Willy Tarreauf3d25982007-05-08 22:45:09 +0200657 else
Willy Tarreau11382812008-07-09 16:18:21 +0200658 ret = ACL_PAT_FAIL;
Willy Tarreauf3d25982007-05-08 22:45:09 +0200659
Willy Tarreauf853c462012-04-23 18:53:56 +0200660 smp->data.str.str[smp->data.str.len] = old_char;
Willy Tarreauf3d25982007-05-08 22:45:09 +0200661 return ret;
662}
663
Willy Tarreaua84d3742007-05-07 00:36:48 +0200664/* Checks that the pattern matches the beginning of the tested string. */
Willy Tarreau37406352012-04-23 16:16:37 +0200665int acl_match_beg(struct sample *smp, struct acl_pattern *pattern)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200666{
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200667 int icase;
668
Willy Tarreauf853c462012-04-23 18:53:56 +0200669 if (pattern->len > smp->data.str.len)
Willy Tarreau11382812008-07-09 16:18:21 +0200670 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200671
672 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
Willy Tarreauf853c462012-04-23 18:53:56 +0200673 if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str, pattern->len) != 0) ||
674 (!icase && strncmp(pattern->ptr.str, smp->data.str.str, pattern->len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +0200675 return ACL_PAT_FAIL;
676 return ACL_PAT_PASS;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200677}
678
679/* Checks that the pattern matches the end of the tested string. */
Willy Tarreau37406352012-04-23 16:16:37 +0200680int acl_match_end(struct sample *smp, struct acl_pattern *pattern)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200681{
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200682 int icase;
683
Willy Tarreauf853c462012-04-23 18:53:56 +0200684 if (pattern->len > smp->data.str.len)
Willy Tarreau11382812008-07-09 16:18:21 +0200685 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200686 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
Willy Tarreauf853c462012-04-23 18:53:56 +0200687 if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str + smp->data.str.len - pattern->len, pattern->len) != 0) ||
688 (!icase && strncmp(pattern->ptr.str, smp->data.str.str + smp->data.str.len - pattern->len, pattern->len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +0200689 return ACL_PAT_FAIL;
690 return ACL_PAT_PASS;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200691}
692
693/* Checks that the pattern is included inside the tested string.
694 * NB: Suboptimal, should be rewritten using a Boyer-Moore method.
695 */
Willy Tarreau37406352012-04-23 16:16:37 +0200696int acl_match_sub(struct sample *smp, struct acl_pattern *pattern)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200697{
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200698 int icase;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200699 char *end;
700 char *c;
701
Willy Tarreauf853c462012-04-23 18:53:56 +0200702 if (pattern->len > smp->data.str.len)
Willy Tarreau11382812008-07-09 16:18:21 +0200703 return ACL_PAT_FAIL;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200704
Willy Tarreauf853c462012-04-23 18:53:56 +0200705 end = smp->data.str.str + smp->data.str.len - pattern->len;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200706 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
707 if (icase) {
Willy Tarreauf853c462012-04-23 18:53:56 +0200708 for (c = smp->data.str.str; c <= end; c++) {
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200709 if (tolower(*c) != tolower(*pattern->ptr.str))
710 continue;
711 if (strncasecmp(pattern->ptr.str, c, pattern->len) == 0)
Willy Tarreau11382812008-07-09 16:18:21 +0200712 return ACL_PAT_PASS;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200713 }
714 } else {
Willy Tarreauf853c462012-04-23 18:53:56 +0200715 for (c = smp->data.str.str; c <= end; c++) {
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200716 if (*c != *pattern->ptr.str)
717 continue;
718 if (strncmp(pattern->ptr.str, c, pattern->len) == 0)
Willy Tarreau11382812008-07-09 16:18:21 +0200719 return ACL_PAT_PASS;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200720 }
Willy Tarreaua84d3742007-05-07 00:36:48 +0200721 }
Willy Tarreau11382812008-07-09 16:18:21 +0200722 return ACL_PAT_FAIL;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200723}
724
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200725/* Background: Fast way to find a zero byte in a word
726 * http://graphics.stanford.edu/~seander/bithacks.html#ZeroInWord
727 * hasZeroByte = (v - 0x01010101UL) & ~v & 0x80808080UL;
728 *
729 * To look for 4 different byte values, xor the word with those bytes and
730 * then check for zero bytes:
731 *
732 * v = (((unsigned char)c * 0x1010101U) ^ delimiter)
733 * where <delimiter> is the 4 byte values to look for (as an uint)
734 * and <c> is the character that is being tested
735 */
736static inline unsigned int is_delimiter(unsigned char c, unsigned int mask)
737{
738 mask ^= (c * 0x01010101); /* propagate the char to all 4 bytes */
739 return (mask - 0x01010101) & ~mask & 0x80808080U;
740}
741
742static inline unsigned int make_4delim(unsigned char d1, unsigned char d2, unsigned char d3, unsigned char d4)
743{
744 return d1 << 24 | d2 << 16 | d3 << 8 | d4;
745}
746
Willy Tarreaua84d3742007-05-07 00:36:48 +0200747/* This one is used by other real functions. It checks that the pattern is
748 * included inside the tested string, but enclosed between the specified
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200749 * delimiters or at the beginning or end of the string. The delimiters are
750 * provided as an unsigned int made by make_4delim() and match up to 4 different
751 * delimiters. Delimiters are stripped at the beginning and end of the pattern.
Willy Tarreaua84d3742007-05-07 00:36:48 +0200752 */
Willy Tarreau37406352012-04-23 16:16:37 +0200753static int match_word(struct sample *smp, struct acl_pattern *pattern, unsigned int delimiters)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200754{
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200755 int may_match, icase;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200756 char *c, *end;
757 char *ps;
758 int pl;
759
760 pl = pattern->len;
761 ps = pattern->ptr.str;
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200762
763 while (pl > 0 && is_delimiter(*ps, delimiters)) {
Willy Tarreaua84d3742007-05-07 00:36:48 +0200764 pl--;
765 ps++;
766 }
767
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200768 while (pl > 0 && is_delimiter(ps[pl - 1], delimiters))
Willy Tarreaua84d3742007-05-07 00:36:48 +0200769 pl--;
770
Willy Tarreauf853c462012-04-23 18:53:56 +0200771 if (pl > smp->data.str.len)
Willy Tarreau11382812008-07-09 16:18:21 +0200772 return ACL_PAT_FAIL;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200773
774 may_match = 1;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200775 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
Willy Tarreauf853c462012-04-23 18:53:56 +0200776 end = smp->data.str.str + smp->data.str.len - pl;
777 for (c = smp->data.str.str; c <= end; c++) {
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200778 if (is_delimiter(*c, delimiters)) {
Willy Tarreaua84d3742007-05-07 00:36:48 +0200779 may_match = 1;
780 continue;
781 }
Willy Tarreaua84d3742007-05-07 00:36:48 +0200782
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200783 if (!may_match)
784 continue;
785
786 if (icase) {
787 if ((tolower(*c) == tolower(*ps)) &&
788 (strncasecmp(ps, c, pl) == 0) &&
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200789 (c == end || is_delimiter(c[pl], delimiters)))
Willy Tarreau11382812008-07-09 16:18:21 +0200790 return ACL_PAT_PASS;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200791 } else {
792 if ((*c == *ps) &&
793 (strncmp(ps, c, pl) == 0) &&
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200794 (c == end || is_delimiter(c[pl], delimiters)))
Willy Tarreau11382812008-07-09 16:18:21 +0200795 return ACL_PAT_PASS;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200796 }
Willy Tarreaua84d3742007-05-07 00:36:48 +0200797 may_match = 0;
798 }
Willy Tarreau11382812008-07-09 16:18:21 +0200799 return ACL_PAT_FAIL;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200800}
801
802/* Checks that the pattern is included inside the tested string, but enclosed
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200803 * between the delimiters '?' or '/' or at the beginning or end of the string.
804 * Delimiters at the beginning or end of the pattern are ignored.
Willy Tarreaua84d3742007-05-07 00:36:48 +0200805 */
Willy Tarreau37406352012-04-23 16:16:37 +0200806int acl_match_dir(struct sample *smp, struct acl_pattern *pattern)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200807{
Willy Tarreau37406352012-04-23 16:16:37 +0200808 return match_word(smp, pattern, make_4delim('/', '?', '?', '?'));
Willy Tarreaua84d3742007-05-07 00:36:48 +0200809}
810
811/* Checks that the pattern is included inside the tested string, but enclosed
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200812 * between the delmiters '/', '?', '.' or ":" or at the beginning or end of
813 * the string. Delimiters at the beginning or end of the pattern are ignored.
Willy Tarreaua84d3742007-05-07 00:36:48 +0200814 */
Willy Tarreau37406352012-04-23 16:16:37 +0200815int acl_match_dom(struct sample *smp, struct acl_pattern *pattern)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200816{
Willy Tarreau37406352012-04-23 16:16:37 +0200817 return match_word(smp, pattern, make_4delim('/', '?', '.', ':'));
Willy Tarreaua84d3742007-05-07 00:36:48 +0200818}
819
820/* Checks that the integer in <test> is included between min and max */
Willy Tarreau37406352012-04-23 16:16:37 +0200821int acl_match_int(struct sample *smp, struct acl_pattern *pattern)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200822{
Willy Tarreauf853c462012-04-23 18:53:56 +0200823 if ((!pattern->val.range.min_set || pattern->val.range.min <= smp->data.uint) &&
824 (!pattern->val.range.max_set || smp->data.uint <= pattern->val.range.max))
Willy Tarreau11382812008-07-09 16:18:21 +0200825 return ACL_PAT_PASS;
826 return ACL_PAT_FAIL;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200827}
828
Willy Tarreau0e698542011-09-16 08:32:32 +0200829/* Checks that the length of the pattern in <test> is included between min and max */
Willy Tarreau37406352012-04-23 16:16:37 +0200830int acl_match_len(struct sample *smp, struct acl_pattern *pattern)
Willy Tarreau0e698542011-09-16 08:32:32 +0200831{
Willy Tarreauf853c462012-04-23 18:53:56 +0200832 if ((!pattern->val.range.min_set || pattern->val.range.min <= smp->data.str.len) &&
833 (!pattern->val.range.max_set || smp->data.str.len <= pattern->val.range.max))
Willy Tarreau0e698542011-09-16 08:32:32 +0200834 return ACL_PAT_PASS;
835 return ACL_PAT_FAIL;
836}
837
Willy Tarreau37406352012-04-23 16:16:37 +0200838int acl_match_ip(struct sample *smp, struct acl_pattern *pattern)
Willy Tarreaua67fad92007-05-08 19:50:09 +0200839{
840 struct in_addr *s;
841
Willy Tarreauf853c462012-04-23 18:53:56 +0200842 if (smp->type != SMP_T_IPV4)
Willy Tarreau11382812008-07-09 16:18:21 +0200843 return ACL_PAT_FAIL;
Willy Tarreaua67fad92007-05-08 19:50:09 +0200844
Willy Tarreauf853c462012-04-23 18:53:56 +0200845 s = &smp->data.ipv4;
Willy Tarreaua67fad92007-05-08 19:50:09 +0200846 if (((s->s_addr ^ pattern->val.ipv4.addr.s_addr) & pattern->val.ipv4.mask.s_addr) == 0)
Willy Tarreau11382812008-07-09 16:18:21 +0200847 return ACL_PAT_PASS;
848 return ACL_PAT_FAIL;
Willy Tarreaua67fad92007-05-08 19:50:09 +0200849}
850
Willy Tarreaub337b532010-05-13 20:03:41 +0200851/* Lookup an IPv4 address in the expression's pattern tree using the longest
852 * match method. The node is returned if it exists, otherwise NULL.
853 */
Willy Tarreau37406352012-04-23 16:16:37 +0200854static void *acl_lookup_ip(struct sample *smp, struct acl_expr *expr)
Willy Tarreaub337b532010-05-13 20:03:41 +0200855{
856 struct in_addr *s;
857
Willy Tarreauf853c462012-04-23 18:53:56 +0200858 if (smp->type != SMP_T_IPV4)
Willy Tarreaub337b532010-05-13 20:03:41 +0200859 return ACL_PAT_FAIL;
860
Willy Tarreauf853c462012-04-23 18:53:56 +0200861 s = &smp->data.ipv4;
Willy Tarreaub337b532010-05-13 20:03:41 +0200862 return ebmb_lookup_longest(&expr->pattern_tree, &s->s_addr);
863}
864
Willy Tarreaua84d3742007-05-07 00:36:48 +0200865/* Parse a string. It is allocated and duplicated. */
Willy Tarreauae8b7962007-06-09 23:10:04 +0200866int acl_parse_str(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200867{
868 int len;
869
Willy Tarreauae8b7962007-06-09 23:10:04 +0200870 len = strlen(*text);
Willy Tarreauc4262962010-05-10 23:42:40 +0200871
872 if (pattern->flags & ACL_PAT_F_TREE_OK) {
873 /* we're allowed to put the data in a tree whose root is pointed
874 * to by val.tree.
875 */
876 struct ebmb_node *node;
877
878 node = calloc(1, sizeof(*node) + len + 1);
879 if (!node)
880 return 0;
881 memcpy(node->key, *text, len + 1);
882 if (ebst_insert(pattern->val.tree, node) != node)
883 free(node); /* was a duplicate */
884 pattern->flags |= ACL_PAT_F_TREE; /* this pattern now contains a tree */
885 return 1;
886 }
887
Willy Tarreauae8b7962007-06-09 23:10:04 +0200888 pattern->ptr.str = strdup(*text);
Willy Tarreaua84d3742007-05-07 00:36:48 +0200889 if (!pattern->ptr.str)
890 return 0;
891 pattern->len = len;
892 return 1;
893}
894
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +0100895/* Parse and concatenate all further strings into one. */
896int
897acl_parse_strcat(const char **text, struct acl_pattern *pattern, int *opaque)
898{
899
900 int len = 0, i;
901 char *s;
902
903 for (i = 0; *text[i]; i++)
904 len += strlen(text[i])+1;
905
906 pattern->ptr.str = s = calloc(1, len);
907 if (!pattern->ptr.str)
908 return 0;
909
910 for (i = 0; *text[i]; i++)
911 s += sprintf(s, i?" %s":"%s", text[i]);
912
913 pattern->len = len;
914
915 return i;
916}
917
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +0200918/* Free data allocated by acl_parse_reg */
Willy Tarreau37406352012-04-23 16:16:37 +0200919static void acl_free_reg(void *ptr)
920{
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +0200921 regfree((regex_t *)ptr);
922}
923
Willy Tarreauf3d25982007-05-08 22:45:09 +0200924/* Parse a regex. It is allocated. */
Willy Tarreauae8b7962007-06-09 23:10:04 +0200925int acl_parse_reg(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreauf3d25982007-05-08 22:45:09 +0200926{
927 regex_t *preg;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200928 int icase;
Willy Tarreauf3d25982007-05-08 22:45:09 +0200929
930 preg = calloc(1, sizeof(regex_t));
931
932 if (!preg)
933 return 0;
934
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200935 icase = (pattern->flags & ACL_PAT_F_IGNORE_CASE) ? REG_ICASE : 0;
936 if (regcomp(preg, *text, REG_EXTENDED | REG_NOSUB | icase) != 0) {
Willy Tarreauf3d25982007-05-08 22:45:09 +0200937 free(preg);
938 return 0;
939 }
940
941 pattern->ptr.reg = preg;
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +0200942 pattern->freeptrbuf = &acl_free_reg;
Willy Tarreauf3d25982007-05-08 22:45:09 +0200943 return 1;
944}
945
Willy Tarreauae8b7962007-06-09 23:10:04 +0200946/* Parse a range of positive integers delimited by either ':' or '-'. If only
947 * one integer is read, it is set as both min and max. An operator may be
948 * specified as the prefix, among this list of 5 :
949 *
950 * 0:eq, 1:gt, 2:ge, 3:lt, 4:le
951 *
952 * The default operator is "eq". It supports range matching. Ranges are
953 * rejected for other operators. The operator may be changed at any time.
954 * The operator is stored in the 'opaque' argument.
955 *
Willy Tarreaua84d3742007-05-07 00:36:48 +0200956 */
Willy Tarreauae8b7962007-06-09 23:10:04 +0200957int acl_parse_int(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200958{
Willy Tarreauae8b7962007-06-09 23:10:04 +0200959 signed long long i;
960 unsigned int j, last, skip = 0;
961 const char *ptr = *text;
962
963
Willy Tarreau8f8e6452007-06-17 21:51:38 +0200964 while (!isdigit((unsigned char)*ptr)) {
Willy Tarreau1c7cc5b2010-07-18 10:46:33 +0200965 switch (get_std_op(ptr)) {
966 case STD_OP_EQ: *opaque = 0; break;
967 case STD_OP_GT: *opaque = 1; break;
968 case STD_OP_GE: *opaque = 2; break;
969 case STD_OP_LT: *opaque = 3; break;
970 case STD_OP_LE: *opaque = 4; break;
971 default:
Willy Tarreauae8b7962007-06-09 23:10:04 +0200972 return 0;
Willy Tarreau1c7cc5b2010-07-18 10:46:33 +0200973 }
Willy Tarreauae8b7962007-06-09 23:10:04 +0200974
975 skip++;
976 ptr = text[skip];
977 }
Willy Tarreaua84d3742007-05-07 00:36:48 +0200978
979 last = i = 0;
980 while (1) {
Willy Tarreauae8b7962007-06-09 23:10:04 +0200981 j = *ptr++;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200982 if ((j == '-' || j == ':') && !last) {
983 last++;
984 pattern->val.range.min = i;
985 i = 0;
986 continue;
987 }
988 j -= '0';
989 if (j > 9)
990 // also catches the terminating zero
991 break;
992 i *= 10;
993 i += j;
994 }
Willy Tarreauae8b7962007-06-09 23:10:04 +0200995
996 if (last && *opaque >= 1 && *opaque <= 4)
997 /* having a range with a min or a max is absurd */
998 return 0;
999
Willy Tarreaua84d3742007-05-07 00:36:48 +02001000 if (!last)
1001 pattern->val.range.min = i;
1002 pattern->val.range.max = i;
Willy Tarreauae8b7962007-06-09 23:10:04 +02001003
1004 switch (*opaque) {
1005 case 0: /* eq */
1006 pattern->val.range.min_set = 1;
1007 pattern->val.range.max_set = 1;
1008 break;
1009 case 1: /* gt */
1010 pattern->val.range.min++; /* gt = ge + 1 */
1011 case 2: /* ge */
1012 pattern->val.range.min_set = 1;
1013 pattern->val.range.max_set = 0;
1014 break;
1015 case 3: /* lt */
1016 pattern->val.range.max--; /* lt = le - 1 */
1017 case 4: /* le */
1018 pattern->val.range.min_set = 0;
1019 pattern->val.range.max_set = 1;
1020 break;
1021 }
1022 return skip + 1;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001023}
1024
Willy Tarreau4a26d2f2008-07-15 16:05:33 +02001025/* Parse a range of positive 2-component versions delimited by either ':' or
1026 * '-'. The version consists in a major and a minor, both of which must be
1027 * smaller than 65536, because internally they will be represented as a 32-bit
1028 * integer.
1029 * If only one version is read, it is set as both min and max. Just like for
1030 * pure integers, an operator may be specified as the prefix, among this list
1031 * of 5 :
1032 *
1033 * 0:eq, 1:gt, 2:ge, 3:lt, 4:le
1034 *
1035 * The default operator is "eq". It supports range matching. Ranges are
1036 * rejected for other operators. The operator may be changed at any time.
1037 * The operator is stored in the 'opaque' argument. This allows constructs
1038 * such as the following one :
1039 *
1040 * acl obsolete_ssl ssl_req_proto lt 3
1041 * acl unsupported_ssl ssl_req_proto gt 3.1
1042 * acl valid_ssl ssl_req_proto 3.0-3.1
1043 *
1044 */
1045int acl_parse_dotted_ver(const char **text, struct acl_pattern *pattern, int *opaque)
1046{
1047 signed long long i;
1048 unsigned int j, last, skip = 0;
1049 const char *ptr = *text;
1050
1051
1052 while (!isdigit((unsigned char)*ptr)) {
Willy Tarreau1c7cc5b2010-07-18 10:46:33 +02001053 switch (get_std_op(ptr)) {
1054 case STD_OP_EQ: *opaque = 0; break;
1055 case STD_OP_GT: *opaque = 1; break;
1056 case STD_OP_GE: *opaque = 2; break;
1057 case STD_OP_LT: *opaque = 3; break;
1058 case STD_OP_LE: *opaque = 4; break;
1059 default:
Willy Tarreau4a26d2f2008-07-15 16:05:33 +02001060 return 0;
Willy Tarreau1c7cc5b2010-07-18 10:46:33 +02001061 }
Willy Tarreau4a26d2f2008-07-15 16:05:33 +02001062
1063 skip++;
1064 ptr = text[skip];
1065 }
1066
1067 last = i = 0;
1068 while (1) {
1069 j = *ptr++;
1070 if (j == '.') {
1071 /* minor part */
1072 if (i >= 65536)
1073 return 0;
1074 i <<= 16;
1075 continue;
1076 }
1077 if ((j == '-' || j == ':') && !last) {
1078 last++;
1079 if (i < 65536)
1080 i <<= 16;
1081 pattern->val.range.min = i;
1082 i = 0;
1083 continue;
1084 }
1085 j -= '0';
1086 if (j > 9)
1087 // also catches the terminating zero
1088 break;
1089 i = (i & 0xFFFF0000) + (i & 0xFFFF) * 10;
1090 i += j;
1091 }
1092
1093 /* if we only got a major version, let's shift it now */
1094 if (i < 65536)
1095 i <<= 16;
1096
1097 if (last && *opaque >= 1 && *opaque <= 4)
1098 /* having a range with a min or a max is absurd */
1099 return 0;
1100
1101 if (!last)
1102 pattern->val.range.min = i;
1103 pattern->val.range.max = i;
1104
1105 switch (*opaque) {
1106 case 0: /* eq */
1107 pattern->val.range.min_set = 1;
1108 pattern->val.range.max_set = 1;
1109 break;
1110 case 1: /* gt */
1111 pattern->val.range.min++; /* gt = ge + 1 */
1112 case 2: /* ge */
1113 pattern->val.range.min_set = 1;
1114 pattern->val.range.max_set = 0;
1115 break;
1116 case 3: /* lt */
1117 pattern->val.range.max--; /* lt = le - 1 */
1118 case 4: /* le */
1119 pattern->val.range.min_set = 0;
1120 pattern->val.range.max_set = 1;
1121 break;
1122 }
1123 return skip + 1;
1124}
1125
Willy Tarreaua67fad92007-05-08 19:50:09 +02001126/* Parse an IP address and an optional mask in the form addr[/mask].
1127 * The addr may either be an IPv4 address or a hostname. The mask
1128 * may either be a dotted mask or a number of bits. Returns 1 if OK,
1129 * otherwise 0.
1130 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02001131int acl_parse_ip(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreaua67fad92007-05-08 19:50:09 +02001132{
Willy Tarreaub337b532010-05-13 20:03:41 +02001133 struct eb_root *tree = NULL;
1134 if (pattern->flags & ACL_PAT_F_TREE_OK)
1135 tree = pattern->val.tree;
1136
1137 if (str2net(*text, &pattern->val.ipv4.addr, &pattern->val.ipv4.mask)) {
1138 unsigned int mask = ntohl(pattern->val.ipv4.mask.s_addr);
1139 struct ebmb_node *node;
1140 /* check if the mask is contiguous so that we can insert the
1141 * network into the tree. A continuous mask has only ones on
1142 * the left. This means that this mask + its lower bit added
1143 * once again is null.
1144 */
1145 if (mask + (mask & -mask) == 0 && tree) {
1146 mask = mask ? 33 - flsnz(mask & -mask) : 0; /* equals cidr value */
1147 /* FIXME: insert <addr>/<mask> into the tree here */
1148 node = calloc(1, sizeof(*node) + 4); /* reserve 4 bytes for IPv4 address */
1149 if (!node)
1150 return 0;
1151 memcpy(node->key, &pattern->val.ipv4.addr, 4); /* network byte order */
1152 node->node.pfx = mask;
1153 if (ebmb_insert_prefix(tree, node, 4) != node)
1154 free(node); /* was a duplicate */
1155 pattern->flags |= ACL_PAT_F_TREE;
1156 return 1;
1157 }
Willy Tarreauae8b7962007-06-09 23:10:04 +02001158 return 1;
Willy Tarreaub337b532010-05-13 20:03:41 +02001159 }
Willy Tarreauae8b7962007-06-09 23:10:04 +02001160 else
1161 return 0;
Willy Tarreaua67fad92007-05-08 19:50:09 +02001162}
1163
Willy Tarreaua84d3742007-05-07 00:36:48 +02001164/*
1165 * Registers the ACL keyword list <kwl> as a list of valid keywords for next
1166 * parsing sessions.
1167 */
1168void acl_register_keywords(struct acl_kw_list *kwl)
1169{
1170 LIST_ADDQ(&acl_keywords.list, &kwl->list);
1171}
1172
1173/*
1174 * Unregisters the ACL keyword list <kwl> from the list of valid keywords.
1175 */
1176void acl_unregister_keywords(struct acl_kw_list *kwl)
1177{
1178 LIST_DEL(&kwl->list);
1179 LIST_INIT(&kwl->list);
1180}
1181
1182/* Return a pointer to the ACL <name> within the list starting at <head>, or
1183 * NULL if not found.
1184 */
1185struct acl *find_acl_by_name(const char *name, struct list *head)
1186{
1187 struct acl *acl;
1188 list_for_each_entry(acl, head, list) {
1189 if (strcmp(acl->name, name) == 0)
1190 return acl;
1191 }
1192 return NULL;
1193}
1194
1195/* Return a pointer to the ACL keyword <kw>, or NULL if not found. Note that if
1196 * <kw> contains an opening parenthesis, only the left part of it is checked.
1197 */
1198struct acl_keyword *find_acl_kw(const char *kw)
1199{
1200 int index;
1201 const char *kwend;
1202 struct acl_kw_list *kwl;
1203
1204 kwend = strchr(kw, '(');
1205 if (!kwend)
1206 kwend = kw + strlen(kw);
1207
1208 list_for_each_entry(kwl, &acl_keywords.list, list) {
1209 for (index = 0; kwl->kw[index].kw != NULL; index++) {
1210 if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) &&
1211 kwl->kw[index].kw[kwend-kw] == 0)
1212 return &kwl->kw[index];
1213 }
1214 }
1215 return NULL;
1216}
1217
Willy Tarreaudfd7fca2011-03-09 07:27:02 +01001218/* NB: does nothing if <pat> is NULL */
Willy Tarreaua84d3742007-05-07 00:36:48 +02001219static void free_pattern(struct acl_pattern *pat)
1220{
Willy Tarreaudfd7fca2011-03-09 07:27:02 +01001221 if (!pat)
1222 return;
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02001223
1224 if (pat->ptr.ptr) {
1225 if (pat->freeptrbuf)
1226 pat->freeptrbuf(pat->ptr.ptr);
1227
Willy Tarreaua84d3742007-05-07 00:36:48 +02001228 free(pat->ptr.ptr);
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02001229 }
1230
Willy Tarreaua84d3742007-05-07 00:36:48 +02001231 free(pat);
1232}
1233
1234static void free_pattern_list(struct list *head)
1235{
1236 struct acl_pattern *pat, *tmp;
1237 list_for_each_entry_safe(pat, tmp, head, list)
1238 free_pattern(pat);
1239}
1240
Willy Tarreaue56cda92010-05-11 23:25:05 +02001241static void free_pattern_tree(struct eb_root *root)
1242{
1243 struct eb_node *node, *next;
1244 node = eb_first(root);
1245 while (node) {
1246 next = eb_next(node);
1247 free(node);
1248 node = next;
1249 }
1250}
1251
Willy Tarreaua84d3742007-05-07 00:36:48 +02001252static struct acl_expr *prune_acl_expr(struct acl_expr *expr)
1253{
Willy Tarreau34db1082012-04-19 17:16:54 +02001254 struct arg *arg;
1255
Willy Tarreaua84d3742007-05-07 00:36:48 +02001256 free_pattern_list(&expr->patterns);
Willy Tarreaue56cda92010-05-11 23:25:05 +02001257 free_pattern_tree(&expr->pattern_tree);
Willy Tarreaua84d3742007-05-07 00:36:48 +02001258 LIST_INIT(&expr->patterns);
Willy Tarreau34db1082012-04-19 17:16:54 +02001259
1260 for (arg = expr->args; arg; arg++) {
1261 if (arg->type == ARGT_STOP)
1262 break;
1263 if (arg->type == ARGT_FE || arg->type == ARGT_BE ||
1264 arg->type == ARGT_TAB || arg->type == ARGT_SRV ||
1265 arg->type == ARGT_USR || arg->type == ARGT_STR) {
1266 free(arg->data.str.str);
1267 arg->data.str.str = NULL;
1268 }
1269 arg++;
1270 }
1271
1272 free(expr->args);
Willy Tarreaua84d3742007-05-07 00:36:48 +02001273 expr->kw->use_cnt--;
1274 return expr;
1275}
1276
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001277static int acl_read_patterns_from_file( struct acl_keyword *aclkw,
1278 struct acl_expr *expr,
1279 const char *filename, int patflags)
1280{
1281 FILE *file;
1282 char *c;
1283 const char *args[2];
1284 struct acl_pattern *pattern;
1285 int opaque;
Willy Tarreau6a8097f2011-02-26 15:14:15 +01001286 int ret = 0;
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001287
1288 file = fopen(filename, "r");
1289 if (!file)
1290 return 0;
1291
1292 /* now parse all patterns. The file may contain only one pattern per
1293 * line. If the line contains spaces, they will be part of the pattern.
1294 * The pattern stops at the first CR, LF or EOF encountered.
1295 */
1296 opaque = 0;
Willy Tarreaue56cda92010-05-11 23:25:05 +02001297 pattern = NULL;
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001298 args[1] = "";
1299 while (fgets(trash, sizeof(trash), file) != NULL) {
1300
1301 c = trash;
Willy Tarreau58215a02010-05-13 22:07:43 +02001302
1303 /* ignore lines beginning with a dash */
1304 if (*c == '#')
1305 continue;
1306
1307 /* strip leading spaces and tabs */
1308 while (*c == ' ' || *c == '\t')
1309 c++;
1310
Willy Tarreau58215a02010-05-13 22:07:43 +02001311
1312 args[0] = c;
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001313 while (*c && *c != '\n' && *c != '\r')
1314 c++;
1315 *c = 0;
1316
Willy Tarreau51091962011-01-03 21:04:10 +01001317 /* empty lines are ignored too */
1318 if (c == args[0])
1319 continue;
1320
Willy Tarreaue56cda92010-05-11 23:25:05 +02001321 /* we keep the previous pattern along iterations as long as it's not used */
1322 if (!pattern)
1323 pattern = (struct acl_pattern *)malloc(sizeof(*pattern));
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001324 if (!pattern)
1325 goto out_close;
Willy Tarreaue56cda92010-05-11 23:25:05 +02001326
1327 memset(pattern, 0, sizeof(*pattern));
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001328 pattern->flags = patflags;
1329
Willy Tarreaue56cda92010-05-11 23:25:05 +02001330 if ((aclkw->requires & ACL_MAY_LOOKUP) && !(pattern->flags & ACL_PAT_F_IGNORE_CASE)) {
1331 /* we pre-set the data pointer to the tree's head so that functions
1332 * which are able to insert in a tree know where to do that.
1333 */
1334 pattern->flags |= ACL_PAT_F_TREE_OK;
1335 pattern->val.tree = &expr->pattern_tree;
1336 }
1337
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001338 if (!aclkw->parse(args, pattern, &opaque))
1339 goto out_free_pattern;
Willy Tarreaue56cda92010-05-11 23:25:05 +02001340
1341 /* if the parser did not feed the tree, let's chain the pattern to the list */
1342 if (!(pattern->flags & ACL_PAT_F_TREE)) {
1343 LIST_ADDQ(&expr->patterns, &pattern->list);
1344 pattern = NULL; /* get a new one */
1345 }
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001346 }
Willy Tarreau6a8097f2011-02-26 15:14:15 +01001347
1348 ret = 1; /* success */
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001349
1350 out_free_pattern:
1351 free_pattern(pattern);
1352 out_close:
1353 fclose(file);
Willy Tarreau6a8097f2011-02-26 15:14:15 +01001354 return ret;
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001355}
1356
Willy Tarreaua84d3742007-05-07 00:36:48 +02001357/* Parse an ACL expression starting at <args>[0], and return it.
1358 * Right now, the only accepted syntax is :
1359 * <subject> [<value>...]
1360 */
1361struct acl_expr *parse_acl_expr(const char **args)
1362{
1363 __label__ out_return, out_free_expr, out_free_pattern;
1364 struct acl_expr *expr;
1365 struct acl_keyword *aclkw;
1366 struct acl_pattern *pattern;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02001367 int opaque, patflags;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001368 const char *arg;
1369
1370 aclkw = find_acl_kw(args[0]);
1371 if (!aclkw || !aclkw->parse)
1372 goto out_return;
1373
1374 expr = (struct acl_expr *)calloc(1, sizeof(*expr));
1375 if (!expr)
1376 goto out_return;
1377
1378 expr->kw = aclkw;
1379 aclkw->use_cnt++;
1380 LIST_INIT(&expr->patterns);
Willy Tarreaue56cda92010-05-11 23:25:05 +02001381 expr->pattern_tree = EB_ROOT_UNIQUE;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001382
1383 arg = strchr(args[0], '(');
Willy Tarreau61612d42012-04-19 18:42:05 +02001384 if (aclkw->arg_mask) {
1385 int nbargs = 0;
Willy Tarreau34db1082012-04-19 17:16:54 +02001386 char *end;
Willy Tarreau34db1082012-04-19 17:16:54 +02001387
Willy Tarreau61612d42012-04-19 18:42:05 +02001388 if (arg != NULL) {
1389 /* there are 0 or more arguments in the form "subject(arg[,arg]*)" */
1390 arg++;
1391 end = strchr(arg, ')');
1392 if (!end)
1393 goto out_free_expr;
Willy Tarreau34db1082012-04-19 17:16:54 +02001394
Willy Tarreau61612d42012-04-19 18:42:05 +02001395 /* Parse the arguments. Note that currently we have no way to
1396 * report parsing errors, hence the NULL in the error pointers.
1397 * An error is also reported if some mandatory arguments are
1398 * missing.
1399 */
1400 nbargs = make_arg_list(arg, end - arg, aclkw->arg_mask, &expr->args,
1401 NULL, NULL, NULL);
1402 if (nbargs < 0)
1403 goto out_free_expr;
1404 }
Willy Tarreaufc2c1fd2012-04-19 23:35:54 +02001405 else if (ARGM(aclkw->arg_mask) == 1) {
1406 int type = (aclkw->arg_mask >> 4) & 15;
1407
1408 /* If a proxy is noted as a mandatory argument, we'll fake
1409 * an empty one so that acl_find_targets() resolves it as
1410 * the current one later.
1411 */
1412 if (type != ARGT_FE && type != ARGT_BE && type != ARGT_TAB)
1413 goto out_free_expr;
1414
1415 /* Build an arg list containing the type as an empty string
1416 * and the usual STOP.
1417 */
1418 expr->args = calloc(2, sizeof(*expr->args));
1419 expr->args[0].type = type;
1420 expr->args[0].data.str.str = strdup("");
1421 expr->args[0].data.str.len = 1;
1422 expr->args[0].data.str.len = 0;
1423 expr->args[1].type = ARGT_STOP;
1424 }
Willy Tarreau61612d42012-04-19 18:42:05 +02001425 else if (ARGM(aclkw->arg_mask)) {
1426 /* there were some mandatory arguments */
Willy Tarreaua84d3742007-05-07 00:36:48 +02001427 goto out_free_expr;
Willy Tarreau61612d42012-04-19 18:42:05 +02001428 }
1429 }
1430 else {
1431 if (arg) {
1432 /* no argument expected */
1433 goto out_free_expr;
1434 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001435 }
1436
Willy Tarreaua84d3742007-05-07 00:36:48 +02001437 args++;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02001438
1439 /* check for options before patterns. Supported options are :
1440 * -i : ignore case for all patterns by default
1441 * -f : read patterns from those files
1442 * -- : everything after this is not an option
1443 */
1444 patflags = 0;
1445 while (**args == '-') {
1446 if ((*args)[1] == 'i')
1447 patflags |= ACL_PAT_F_IGNORE_CASE;
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001448 else if ((*args)[1] == 'f') {
1449 if (!acl_read_patterns_from_file(aclkw, expr, args[1], patflags | ACL_PAT_F_FROM_FILE))
1450 goto out_free_expr;
1451 args++;
1452 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +02001453 else if ((*args)[1] == '-') {
1454 args++;
1455 break;
1456 }
1457 else
1458 break;
1459 args++;
1460 }
1461
1462 /* now parse all patterns */
Willy Tarreauae8b7962007-06-09 23:10:04 +02001463 opaque = 0;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001464 while (**args) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02001465 int ret;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001466 pattern = (struct acl_pattern *)calloc(1, sizeof(*pattern));
1467 if (!pattern)
1468 goto out_free_expr;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02001469 pattern->flags = patflags;
1470
Willy Tarreauae8b7962007-06-09 23:10:04 +02001471 ret = aclkw->parse(args, pattern, &opaque);
1472 if (!ret)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001473 goto out_free_pattern;
1474 LIST_ADDQ(&expr->patterns, &pattern->list);
Willy Tarreauae8b7962007-06-09 23:10:04 +02001475 args += ret;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001476 }
1477
1478 return expr;
1479
1480 out_free_pattern:
1481 free_pattern(pattern);
1482 out_free_expr:
1483 prune_acl_expr(expr);
1484 free(expr);
1485 out_return:
1486 return NULL;
1487}
1488
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02001489/* Purge everything in the acl <acl>, then return <acl>. */
1490struct acl *prune_acl(struct acl *acl) {
1491
1492 struct acl_expr *expr, *exprb;
1493
1494 free(acl->name);
1495
1496 list_for_each_entry_safe(expr, exprb, &acl->expr, list) {
1497 LIST_DEL(&expr->list);
1498 prune_acl_expr(expr);
1499 free(expr);
1500 }
1501
1502 return acl;
1503}
1504
Willy Tarreaua84d3742007-05-07 00:36:48 +02001505/* Parse an ACL with the name starting at <args>[0], and with a list of already
1506 * known ACLs in <acl>. If the ACL was not in the list, it will be added.
Willy Tarreau2a56c5e2010-03-15 16:13:29 +01001507 * A pointer to that ACL is returned. If the ACL has an empty name, then it's
1508 * an anonymous one and it won't be merged with any other one.
Willy Tarreaua84d3742007-05-07 00:36:48 +02001509 *
1510 * args syntax: <aclname> <acl_expr>
1511 */
1512struct acl *parse_acl(const char **args, struct list *known_acl)
1513{
1514 __label__ out_return, out_free_acl_expr, out_free_name;
1515 struct acl *cur_acl;
1516 struct acl_expr *acl_expr;
1517 char *name;
1518
Willy Tarreau2a56c5e2010-03-15 16:13:29 +01001519 if (**args && invalid_char(*args))
Willy Tarreau2e74c3f2007-12-02 18:45:09 +01001520 goto out_return;
1521
Willy Tarreaua84d3742007-05-07 00:36:48 +02001522 acl_expr = parse_acl_expr(args + 1);
1523 if (!acl_expr)
1524 goto out_return;
1525
Willy Tarreau404e8ab2009-07-26 19:40:40 +02001526 /* Check for args beginning with an opening parenthesis just after the
1527 * subject, as this is almost certainly a typo. Right now we can only
1528 * emit a warning, so let's do so.
1529 */
Krzysztof Piotr Oledzki4cdd8312009-10-05 00:23:35 +02001530 if (!strchr(args[1], '(') && *args[2] == '(')
Willy Tarreau404e8ab2009-07-26 19:40:40 +02001531 Warning("parsing acl '%s' :\n"
1532 " matching '%s' for pattern '%s' is likely a mistake and probably\n"
1533 " not what you want. Maybe you need to remove the extraneous space before '('.\n"
1534 " If you are really sure this is not an error, please insert '--' between the\n"
1535 " match and the pattern to make this warning message disappear.\n",
1536 args[0], args[1], args[2]);
1537
Willy Tarreau2a56c5e2010-03-15 16:13:29 +01001538 if (*args[0])
1539 cur_acl = find_acl_by_name(args[0], known_acl);
1540 else
1541 cur_acl = NULL;
1542
Willy Tarreaua84d3742007-05-07 00:36:48 +02001543 if (!cur_acl) {
1544 name = strdup(args[0]);
1545 if (!name)
1546 goto out_free_acl_expr;
1547 cur_acl = (struct acl *)calloc(1, sizeof(*cur_acl));
1548 if (cur_acl == NULL)
1549 goto out_free_name;
1550
1551 LIST_INIT(&cur_acl->expr);
1552 LIST_ADDQ(known_acl, &cur_acl->list);
1553 cur_acl->name = name;
1554 }
1555
Willy Tarreaua9802632008-07-25 19:13:19 +02001556 cur_acl->requires |= acl_expr->kw->requires;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001557 LIST_ADDQ(&cur_acl->expr, &acl_expr->list);
1558 return cur_acl;
1559
1560 out_free_name:
1561 free(name);
1562 out_free_acl_expr:
1563 prune_acl_expr(acl_expr);
1564 free(acl_expr);
1565 out_return:
1566 return NULL;
1567}
1568
Willy Tarreau16fbe822007-06-17 11:54:31 +02001569/* Some useful ACLs provided by default. Only those used are allocated. */
1570
1571const struct {
1572 const char *name;
1573 const char *expr[4]; /* put enough for longest expression */
1574} default_acl_list[] = {
Willy Tarreau58393e12008-07-20 10:39:22 +02001575 { .name = "TRUE", .expr = {"always_true",""}},
1576 { .name = "FALSE", .expr = {"always_false",""}},
Willy Tarreau16fbe822007-06-17 11:54:31 +02001577 { .name = "LOCALHOST", .expr = {"src","127.0.0.1/8",""}},
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001578 { .name = "HTTP", .expr = {"req_proto_http",""}},
Willy Tarreau16fbe822007-06-17 11:54:31 +02001579 { .name = "HTTP_1.0", .expr = {"req_ver","1.0",""}},
1580 { .name = "HTTP_1.1", .expr = {"req_ver","1.1",""}},
1581 { .name = "METH_CONNECT", .expr = {"method","CONNECT",""}},
1582 { .name = "METH_GET", .expr = {"method","GET","HEAD",""}},
1583 { .name = "METH_HEAD", .expr = {"method","HEAD",""}},
1584 { .name = "METH_OPTIONS", .expr = {"method","OPTIONS",""}},
1585 { .name = "METH_POST", .expr = {"method","POST",""}},
1586 { .name = "METH_TRACE", .expr = {"method","TRACE",""}},
1587 { .name = "HTTP_URL_ABS", .expr = {"url_reg","^[^/:]*://",""}},
1588 { .name = "HTTP_URL_SLASH", .expr = {"url_beg","/",""}},
1589 { .name = "HTTP_URL_STAR", .expr = {"url","*",""}},
1590 { .name = "HTTP_CONTENT", .expr = {"hdr_val(content-length)","gt","0",""}},
Emeric Brunbede3d02009-06-30 17:54:00 +02001591 { .name = "RDP_COOKIE", .expr = {"req_rdp_cookie_cnt","gt","0",""}},
Willy Tarreauc6317702008-07-20 09:29:50 +02001592 { .name = "REQ_CONTENT", .expr = {"req_len","gt","0",""}},
Willy Tarreaub6fb4202008-07-20 11:18:28 +02001593 { .name = "WAIT_END", .expr = {"wait_end",""}},
Willy Tarreau16fbe822007-06-17 11:54:31 +02001594 { .name = NULL, .expr = {""}}
1595};
1596
1597/* Find a default ACL from the default_acl list, compile it and return it.
1598 * If the ACL is not found, NULL is returned. In theory, it cannot fail,
1599 * except when default ACLs are broken, in which case it will return NULL.
1600 * If <known_acl> is not NULL, the ACL will be queued at its tail.
1601 */
1602struct acl *find_acl_default(const char *acl_name, struct list *known_acl)
1603{
1604 __label__ out_return, out_free_acl_expr, out_free_name;
1605 struct acl *cur_acl;
1606 struct acl_expr *acl_expr;
1607 char *name;
1608 int index;
1609
1610 for (index = 0; default_acl_list[index].name != NULL; index++) {
1611 if (strcmp(acl_name, default_acl_list[index].name) == 0)
1612 break;
1613 }
1614
1615 if (default_acl_list[index].name == NULL)
1616 return NULL;
1617
1618 acl_expr = parse_acl_expr((const char **)default_acl_list[index].expr);
1619 if (!acl_expr)
1620 goto out_return;
1621
1622 name = strdup(acl_name);
1623 if (!name)
1624 goto out_free_acl_expr;
1625 cur_acl = (struct acl *)calloc(1, sizeof(*cur_acl));
1626 if (cur_acl == NULL)
1627 goto out_free_name;
1628
1629 cur_acl->name = name;
Willy Tarreaua55b7dc2009-07-12 09:21:30 +02001630 cur_acl->requires |= acl_expr->kw->requires;
Willy Tarreau16fbe822007-06-17 11:54:31 +02001631 LIST_INIT(&cur_acl->expr);
1632 LIST_ADDQ(&cur_acl->expr, &acl_expr->list);
1633 if (known_acl)
1634 LIST_ADDQ(known_acl, &cur_acl->list);
1635
1636 return cur_acl;
1637
1638 out_free_name:
1639 free(name);
1640 out_free_acl_expr:
1641 prune_acl_expr(acl_expr);
1642 free(acl_expr);
1643 out_return:
1644 return NULL;
1645}
Willy Tarreaua84d3742007-05-07 00:36:48 +02001646
1647/* Purge everything in the acl_cond <cond>, then return <cond>. */
1648struct acl_cond *prune_acl_cond(struct acl_cond *cond)
1649{
1650 struct acl_term_suite *suite, *tmp_suite;
1651 struct acl_term *term, *tmp_term;
1652
1653 /* iterate through all term suites and free all terms and all suites */
1654 list_for_each_entry_safe(suite, tmp_suite, &cond->suites, list) {
1655 list_for_each_entry_safe(term, tmp_term, &suite->terms, list)
1656 free(term);
1657 free(suite);
1658 }
1659 return cond;
1660}
1661
1662/* Parse an ACL condition starting at <args>[0], relying on a list of already
1663 * known ACLs passed in <known_acl>. The new condition is returned (or NULL in
1664 * case of low memory). Supports multiple conditions separated by "or".
1665 */
1666struct acl_cond *parse_acl_cond(const char **args, struct list *known_acl, int pol)
1667{
1668 __label__ out_return, out_free_suite, out_free_term;
Willy Tarreau74b98a82007-06-16 19:35:18 +02001669 int arg, neg;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001670 const char *word;
1671 struct acl *cur_acl;
1672 struct acl_term *cur_term;
1673 struct acl_term_suite *cur_suite;
1674 struct acl_cond *cond;
1675
1676 cond = (struct acl_cond *)calloc(1, sizeof(*cond));
1677 if (cond == NULL)
1678 goto out_return;
1679
1680 LIST_INIT(&cond->list);
1681 LIST_INIT(&cond->suites);
1682 cond->pol = pol;
1683
1684 cur_suite = NULL;
Willy Tarreau74b98a82007-06-16 19:35:18 +02001685 neg = 0;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001686 for (arg = 0; *args[arg]; arg++) {
1687 word = args[arg];
1688
1689 /* remove as many exclamation marks as we can */
1690 while (*word == '!') {
1691 neg = !neg;
1692 word++;
1693 }
1694
1695 /* an empty word is allowed because we cannot force the user to
1696 * always think about not leaving exclamation marks alone.
1697 */
1698 if (!*word)
1699 continue;
1700
Willy Tarreau16fbe822007-06-17 11:54:31 +02001701 if (strcasecmp(word, "or") == 0 || strcmp(word, "||") == 0) {
Willy Tarreaua84d3742007-05-07 00:36:48 +02001702 /* new term suite */
1703 cur_suite = NULL;
1704 neg = 0;
1705 continue;
1706 }
1707
Willy Tarreau95fa4692010-02-01 13:05:50 +01001708 if (strcmp(word, "{") == 0) {
1709 /* we may have a complete ACL expression between two braces,
1710 * find the last one.
1711 */
1712 int arg_end = arg + 1;
1713 const char **args_new;
1714
1715 while (*args[arg_end] && strcmp(args[arg_end], "}") != 0)
1716 arg_end++;
1717
1718 if (!*args[arg_end])
1719 goto out_free_suite;
1720
1721 args_new = calloc(1, (arg_end - arg + 1) * sizeof(*args_new));
1722 if (!args_new)
1723 goto out_free_suite;
1724
Willy Tarreau2a56c5e2010-03-15 16:13:29 +01001725 args_new[0] = "";
Willy Tarreau95fa4692010-02-01 13:05:50 +01001726 memcpy(args_new + 1, args + arg + 1, (arg_end - arg) * sizeof(*args_new));
1727 args_new[arg_end - arg] = "";
1728 cur_acl = parse_acl(args_new, known_acl);
1729 free(args_new);
1730
1731 if (!cur_acl)
Willy Tarreau16fbe822007-06-17 11:54:31 +02001732 goto out_free_suite;
Willy Tarreau95fa4692010-02-01 13:05:50 +01001733 arg = arg_end;
1734 }
1735 else {
1736 /* search for <word> in the known ACL names. If we do not find
1737 * it, let's look for it in the default ACLs, and if found, add
1738 * it to the list of ACLs of this proxy. This makes it possible
1739 * to override them.
1740 */
1741 cur_acl = find_acl_by_name(word, known_acl);
1742 if (cur_acl == NULL) {
1743 cur_acl = find_acl_default(word, known_acl);
1744 if (cur_acl == NULL)
1745 goto out_free_suite;
1746 }
Willy Tarreau16fbe822007-06-17 11:54:31 +02001747 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001748
1749 cur_term = (struct acl_term *)calloc(1, sizeof(*cur_term));
1750 if (cur_term == NULL)
1751 goto out_free_suite;
1752
1753 cur_term->acl = cur_acl;
1754 cur_term->neg = neg;
Willy Tarreaua9802632008-07-25 19:13:19 +02001755 cond->requires |= cur_acl->requires;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001756
1757 if (!cur_suite) {
1758 cur_suite = (struct acl_term_suite *)calloc(1, sizeof(*cur_suite));
1759 if (cur_term == NULL)
1760 goto out_free_term;
1761 LIST_INIT(&cur_suite->terms);
1762 LIST_ADDQ(&cond->suites, &cur_suite->list);
1763 }
1764 LIST_ADDQ(&cur_suite->terms, &cur_term->list);
Willy Tarreau74b98a82007-06-16 19:35:18 +02001765 neg = 0;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001766 }
1767
1768 return cond;
1769
1770 out_free_term:
1771 free(cur_term);
1772 out_free_suite:
1773 prune_acl_cond(cond);
1774 free(cond);
1775 out_return:
1776 return NULL;
1777}
1778
Willy Tarreau2bbba412010-01-28 16:48:33 +01001779/* Builds an ACL condition starting at the if/unless keyword. The complete
1780 * condition is returned. NULL is returned in case of error or if the first
1781 * word is neither "if" nor "unless". It automatically sets the file name and
1782 * the line number in the condition for better error reporting, and adds the
1783 * ACL requirements to the proxy's acl_requires.
1784 */
1785struct acl_cond *build_acl_cond(const char *file, int line, struct proxy *px, const char **args)
1786{
1787 int pol = ACL_COND_NONE;
1788 struct acl_cond *cond = NULL;
1789
1790 if (!strcmp(*args, "if")) {
1791 pol = ACL_COND_IF;
1792 args++;
1793 }
1794 else if (!strcmp(*args, "unless")) {
1795 pol = ACL_COND_UNLESS;
1796 args++;
1797 }
1798 else
1799 return NULL;
1800
1801 cond = parse_acl_cond(args, &px->acl, pol);
1802 if (!cond)
1803 return NULL;
1804
1805 cond->file = file;
1806 cond->line = line;
1807 px->acl_requires |= cond->requires;
1808
1809 return cond;
1810}
1811
Willy Tarreau11382812008-07-09 16:18:21 +02001812/* Execute condition <cond> and return either ACL_PAT_FAIL, ACL_PAT_MISS or
Willy Tarreaub6866442008-07-14 23:54:42 +02001813 * ACL_PAT_PASS depending on the test results. ACL_PAT_MISS may only be
1814 * returned if <dir> contains ACL_PARTIAL, indicating that incomplete data
1815 * is being examined.
1816 * This function only computes the condition, it does not apply the polarity
1817 * required by IF/UNLESS, it's up to the caller to do this using something like
1818 * this :
Willy Tarreau11382812008-07-09 16:18:21 +02001819 *
1820 * res = acl_pass(res);
Willy Tarreaub6866442008-07-14 23:54:42 +02001821 * if (res == ACL_PAT_MISS)
1822 * return 0;
Willy Tarreau11382812008-07-09 16:18:21 +02001823 * if (cond->pol == ACL_COND_UNLESS)
1824 * res = !res;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001825 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02001826int acl_exec_cond(struct acl_cond *cond, struct proxy *px, struct session *l4, void *l7, int dir)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001827{
1828 __label__ fetch_next;
1829 struct acl_term_suite *suite;
1830 struct acl_term *term;
1831 struct acl_expr *expr;
1832 struct acl *acl;
1833 struct acl_pattern *pattern;
Willy Tarreau37406352012-04-23 16:16:37 +02001834 struct sample smp;
Willy Tarreau11382812008-07-09 16:18:21 +02001835 int acl_res, suite_res, cond_res;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001836
Willy Tarreau11382812008-07-09 16:18:21 +02001837 /* We're doing a logical OR between conditions so we initialize to FAIL.
1838 * The MISS status is propagated down from the suites.
1839 */
Willy Tarreaua84d3742007-05-07 00:36:48 +02001840 cond_res = ACL_PAT_FAIL;
1841 list_for_each_entry(suite, &cond->suites, list) {
Willy Tarreau11382812008-07-09 16:18:21 +02001842 /* Evaluate condition suite <suite>. We stop at the first term
1843 * which returns ACL_PAT_FAIL. The MISS status is still propagated
1844 * in case of uncertainty in the result.
Willy Tarreaua84d3742007-05-07 00:36:48 +02001845 */
1846
1847 /* we're doing a logical AND between terms, so we must set the
1848 * initial value to PASS.
1849 */
1850 suite_res = ACL_PAT_PASS;
1851 list_for_each_entry(term, &suite->terms, list) {
1852 acl = term->acl;
1853
1854 /* FIXME: use cache !
1855 * check acl->cache_idx for this.
1856 */
1857
1858 /* ACL result not cached. Let's scan all the expressions
1859 * and use the first one to match.
1860 */
1861 acl_res = ACL_PAT_FAIL;
1862 list_for_each_entry(expr, &acl->expr, list) {
Willy Tarreaud41f8d82007-06-10 10:06:18 +02001863 /* we need to reset context and flags */
Willy Tarreau37406352012-04-23 16:16:37 +02001864 memset(&smp, 0, sizeof(smp));
Willy Tarreaua84d3742007-05-07 00:36:48 +02001865 fetch_next:
Willy Tarreau37406352012-04-23 16:16:37 +02001866 if (!expr->kw->fetch(px, l4, l7, dir, expr, &smp)) {
Willy Tarreaub6866442008-07-14 23:54:42 +02001867 /* maybe we could not fetch because of missing data */
Willy Tarreau37406352012-04-23 16:16:37 +02001868 if (smp.flags & SMP_F_MAY_CHANGE && dir & ACL_PARTIAL)
Willy Tarreaub6866442008-07-14 23:54:42 +02001869 acl_res |= ACL_PAT_MISS;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001870 continue;
Willy Tarreaub6866442008-07-14 23:54:42 +02001871 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001872
Willy Tarreau197e10a2012-04-23 19:18:42 +02001873 if (smp.type == SMP_T_BOOL) {
1874 if (smp.data.uint)
Willy Tarreaua79534f2008-07-20 10:13:37 +02001875 acl_res |= ACL_PAT_PASS;
1876 else
1877 acl_res |= ACL_PAT_FAIL;
1878 }
1879 else {
Willy Tarreau020534d2010-05-16 21:45:45 +02001880 if (!eb_is_empty(&expr->pattern_tree)) {
Willy Tarreauc4262962010-05-10 23:42:40 +02001881 /* a tree is present, let's check what type it is */
1882 if (expr->kw->match == acl_match_str)
Willy Tarreau37406352012-04-23 16:16:37 +02001883 acl_res |= acl_lookup_str(&smp, expr) ? ACL_PAT_PASS : ACL_PAT_FAIL;
Willy Tarreaub337b532010-05-13 20:03:41 +02001884 else if (expr->kw->match == acl_match_ip)
Willy Tarreau37406352012-04-23 16:16:37 +02001885 acl_res |= acl_lookup_ip(&smp, expr) ? ACL_PAT_PASS : ACL_PAT_FAIL;
Willy Tarreauc4262962010-05-10 23:42:40 +02001886 }
1887
Willy Tarreaua79534f2008-07-20 10:13:37 +02001888 /* call the match() function for all tests on this value */
1889 list_for_each_entry(pattern, &expr->patterns, list) {
Willy Tarreaua79534f2008-07-20 10:13:37 +02001890 if (acl_res == ACL_PAT_PASS)
1891 break;
Willy Tarreau37406352012-04-23 16:16:37 +02001892 acl_res |= expr->kw->match(&smp, pattern);
Willy Tarreaua79534f2008-07-20 10:13:37 +02001893 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001894 }
1895 /*
Willy Tarreau11382812008-07-09 16:18:21 +02001896 * OK now acl_res holds the result of this expression
1897 * as one of ACL_PAT_FAIL, ACL_PAT_MISS or ACL_PAT_PASS.
Willy Tarreaua84d3742007-05-07 00:36:48 +02001898 *
Willy Tarreau11382812008-07-09 16:18:21 +02001899 * Then if (!MISS) we can cache the result, and put
Willy Tarreau37406352012-04-23 16:16:37 +02001900 * (smp.flags & SMP_F_VOLATILE) in the cache flags.
Willy Tarreaua84d3742007-05-07 00:36:48 +02001901 *
1902 * FIXME: implement cache.
1903 *
1904 */
1905
1906 /* now we may have some cleanup to do */
Willy Tarreau37406352012-04-23 16:16:37 +02001907 if (smp.flags & SMP_F_MUST_FREE) {
Willy Tarreauf853c462012-04-23 18:53:56 +02001908 free(smp.data.str.str);
1909 smp.data.str.len = 0;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001910 }
1911
Willy Tarreau11382812008-07-09 16:18:21 +02001912 /* we're ORing these terms, so a single PASS is enough */
1913 if (acl_res == ACL_PAT_PASS)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001914 break;
1915
Willy Tarreau37406352012-04-23 16:16:37 +02001916 if (smp.flags & SMP_F_NOT_LAST)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001917 goto fetch_next;
Willy Tarreaub6866442008-07-14 23:54:42 +02001918
1919 /* sometimes we know the fetched data is subject to change
1920 * later and give another chance for a new match (eg: request
1921 * size, time, ...)
1922 */
Willy Tarreau37406352012-04-23 16:16:37 +02001923 if (smp.flags & SMP_F_MAY_CHANGE && dir & ACL_PARTIAL)
Willy Tarreaub6866442008-07-14 23:54:42 +02001924 acl_res |= ACL_PAT_MISS;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001925 }
1926 /*
1927 * Here we have the result of an ACL (cached or not).
1928 * ACLs are combined, negated or not, to form conditions.
1929 */
1930
Willy Tarreaua84d3742007-05-07 00:36:48 +02001931 if (term->neg)
Willy Tarreau11382812008-07-09 16:18:21 +02001932 acl_res = acl_neg(acl_res);
Willy Tarreaua84d3742007-05-07 00:36:48 +02001933
1934 suite_res &= acl_res;
Willy Tarreau11382812008-07-09 16:18:21 +02001935
1936 /* we're ANDing these terms, so a single FAIL is enough */
1937 if (suite_res == ACL_PAT_FAIL)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001938 break;
1939 }
1940 cond_res |= suite_res;
Willy Tarreau11382812008-07-09 16:18:21 +02001941
1942 /* we're ORing these terms, so a single PASS is enough */
1943 if (cond_res == ACL_PAT_PASS)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001944 break;
1945 }
Willy Tarreau11382812008-07-09 16:18:21 +02001946 return cond_res;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001947}
1948
1949
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001950/* Reports a pointer to the first ACL used in condition <cond> which requires
1951 * at least one of the USE_FLAGS in <require>. Returns NULL if none matches.
1952 * The construct is almost the same as for acl_exec_cond() since we're walking
1953 * down the ACL tree as well. It is important that the tree is really walked
1954 * through and never cached, because that way, this function can be used as a
1955 * late check.
1956 */
Willy Tarreauf1e98b82010-01-28 17:59:39 +01001957struct acl *cond_find_require(const struct acl_cond *cond, unsigned int require)
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001958{
1959 struct acl_term_suite *suite;
1960 struct acl_term *term;
1961 struct acl *acl;
1962
1963 list_for_each_entry(suite, &cond->suites, list) {
1964 list_for_each_entry(term, &suite->terms, list) {
1965 acl = term->acl;
1966 if (acl->requires & require)
1967 return acl;
1968 }
1969 }
1970 return NULL;
1971}
1972
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001973/*
1974 * Find targets for userlist and groups in acl. Function returns the number
1975 * of errors or OK if everything is fine.
1976 */
1977int
1978acl_find_targets(struct proxy *p)
1979{
1980
1981 struct acl *acl;
1982 struct acl_expr *expr;
1983 struct acl_pattern *pattern;
1984 struct userlist *ul;
Willy Tarreau63364ee2012-04-19 19:11:13 +02001985 struct arg *arg;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001986 int cfgerr = 0;
1987
1988 list_for_each_entry(acl, &p->acl, list) {
1989 list_for_each_entry(expr, &acl->expr, list) {
Willy Tarreau63364ee2012-04-19 19:11:13 +02001990 for (arg = expr->args; arg; arg++) {
1991 if (arg->type == ARGT_STOP)
1992 break;
1993 else if (arg->type == ARGT_SRV) {
1994 struct proxy *px;
1995 struct server *srv;
1996 char *pname, *sname;
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001997
Willy Tarreau63364ee2012-04-19 19:11:13 +02001998 if (!expr->args->data.str.len) {
1999 Alert("proxy %s: acl '%s' %s(): missing server name.\n",
2000 p->id, acl->name, expr->kw->kw);
2001 cfgerr++;
2002 continue;
2003 }
Willy Tarreau0b1cd942010-05-16 22:18:27 +02002004
Willy Tarreau63364ee2012-04-19 19:11:13 +02002005 pname = expr->args->data.str.str;
2006 sname = strrchr(pname, '/');
Willy Tarreau0b1cd942010-05-16 22:18:27 +02002007
Willy Tarreau63364ee2012-04-19 19:11:13 +02002008 if (sname)
2009 *sname++ = '\0';
2010 else {
2011 sname = pname;
2012 pname = NULL;
2013 }
2014
2015 px = p;
2016 if (pname) {
2017 px = findproxy(pname, PR_CAP_BE);
2018 if (!px) {
2019 Alert("proxy %s: acl '%s' %s(): unable to find proxy '%s'.\n",
2020 p->id, acl->name, expr->kw->kw, pname);
2021 cfgerr++;
2022 continue;
2023 }
2024 }
Willy Tarreau0b1cd942010-05-16 22:18:27 +02002025
Willy Tarreau63364ee2012-04-19 19:11:13 +02002026 srv = findserver(px, sname);
2027 if (!srv) {
2028 Alert("proxy %s: acl '%s' %s(): unable to find server '%s'.\n",
2029 p->id, acl->name, expr->kw->kw, sname);
Willy Tarreau0b1cd942010-05-16 22:18:27 +02002030 cfgerr++;
2031 continue;
2032 }
Willy Tarreau0b1cd942010-05-16 22:18:27 +02002033
Willy Tarreau63364ee2012-04-19 19:11:13 +02002034 free(expr->args->data.str.str);
2035 expr->args->data.srv = srv;
Willy Tarreaud28c3532012-04-19 19:28:33 +02002036 }
2037 else if (arg->type == ARGT_FE) {
Willy Tarreaufc2c1fd2012-04-19 23:35:54 +02002038 struct proxy *prx = p;
2039 char *pname = p->id;
Willy Tarreaud28c3532012-04-19 19:28:33 +02002040
Willy Tarreaufc2c1fd2012-04-19 23:35:54 +02002041 if (expr->args->data.str.len) {
2042 pname = expr->args->data.str.str;
2043 prx = findproxy(pname, PR_CAP_FE);
Willy Tarreaud28c3532012-04-19 19:28:33 +02002044 }
2045
Willy Tarreaud28c3532012-04-19 19:28:33 +02002046 if (!prx) {
2047 Alert("proxy %s: acl '%s' %s(): unable to find frontend '%s'.\n",
2048 p->id, acl->name, expr->kw->kw, pname);
2049 cfgerr++;
2050 continue;
2051 }
2052
Willy Tarreaufc2c1fd2012-04-19 23:35:54 +02002053 if (!(prx->cap & PR_CAP_FE)) {
2054 Alert("proxy %s: acl '%s' %s(): proxy '%s' has no frontend capability.\n",
2055 p->id, acl->name, expr->kw->kw, pname);
2056 cfgerr++;
2057 continue;
2058 }
2059
Willy Tarreaud28c3532012-04-19 19:28:33 +02002060 free(expr->args->data.str.str);
2061 expr->args->data.prx = prx;
2062 }
2063 else if (arg->type == ARGT_BE) {
Willy Tarreaufc2c1fd2012-04-19 23:35:54 +02002064 struct proxy *prx = p;
2065 char *pname = p->id;
Willy Tarreaud28c3532012-04-19 19:28:33 +02002066
Willy Tarreaufc2c1fd2012-04-19 23:35:54 +02002067 if (expr->args->data.str.len) {
2068 pname = expr->args->data.str.str;
2069 prx = findproxy(pname, PR_CAP_BE);
Willy Tarreaud28c3532012-04-19 19:28:33 +02002070 }
2071
Willy Tarreaud28c3532012-04-19 19:28:33 +02002072 if (!prx) {
2073 Alert("proxy %s: acl '%s' %s(): unable to find backend '%s'.\n",
2074 p->id, acl->name, expr->kw->kw, pname);
2075 cfgerr++;
2076 continue;
2077 }
2078
Willy Tarreaufc2c1fd2012-04-19 23:35:54 +02002079 if (!(prx->cap & PR_CAP_BE)) {
2080 Alert("proxy %s: acl '%s' %s(): proxy '%s' has no backend capability.\n",
2081 p->id, acl->name, expr->kw->kw, pname);
2082 cfgerr++;
2083 continue;
2084 }
2085
Willy Tarreaud28c3532012-04-19 19:28:33 +02002086 free(expr->args->data.str.str);
2087 expr->args->data.prx = prx;
2088 }
2089 else if (arg->type == ARGT_TAB) {
Willy Tarreaufc2c1fd2012-04-19 23:35:54 +02002090 struct proxy *prx = p;
2091 char *pname = p->id;
Willy Tarreaud28c3532012-04-19 19:28:33 +02002092
Willy Tarreaufc2c1fd2012-04-19 23:35:54 +02002093 if (expr->args->data.str.len) {
2094 pname = expr->args->data.str.str;
2095 prx = find_stktable(pname);
Willy Tarreaud28c3532012-04-19 19:28:33 +02002096 }
2097
Willy Tarreaud28c3532012-04-19 19:28:33 +02002098 if (!prx) {
2099 Alert("proxy %s: acl '%s' %s(): unable to find table '%s'.\n",
2100 p->id, acl->name, expr->kw->kw, pname);
2101 cfgerr++;
2102 continue;
2103 }
2104
Willy Tarreaufc2c1fd2012-04-19 23:35:54 +02002105
2106 if (!prx->table.size) {
2107 Alert("proxy %s: acl '%s' %s(): no table in proxy '%s'.\n",
2108 p->id, acl->name, expr->kw->kw, pname);
2109 cfgerr++;
2110 continue;
2111 }
2112
Willy Tarreaud28c3532012-04-19 19:28:33 +02002113 free(expr->args->data.str.str);
2114 expr->args->data.prx = prx;
Willy Tarreau0b1cd942010-05-16 22:18:27 +02002115 }
Willy Tarreau63364ee2012-04-19 19:11:13 +02002116 else if (arg->type == ARGT_USR) {
2117 if (!expr->args->data.str.len) {
2118 Alert("proxy %s: acl '%s' %s(): missing userlist name.\n",
2119 p->id, acl->name, expr->kw->kw);
2120 cfgerr++;
2121 continue;
2122 }
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01002123
Willy Tarreau63364ee2012-04-19 19:11:13 +02002124 if (p->uri_auth && p->uri_auth->userlist &&
2125 !strcmp(p->uri_auth->userlist->name, expr->args->data.str.str))
2126 ul = p->uri_auth->userlist;
2127 else
2128 ul = auth_find_userlist(expr->args->data.str.str);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01002129
Willy Tarreau63364ee2012-04-19 19:11:13 +02002130 if (!ul) {
2131 Alert("proxy %s: acl '%s' %s(%s): unable to find userlist.\n",
2132 p->id, acl->name, expr->kw->kw, expr->args->data.str.str);
2133 cfgerr++;
2134 continue;
2135 }
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01002136
Willy Tarreau63364ee2012-04-19 19:11:13 +02002137 free(expr->args->data.str.str);
2138 expr->args->data.usr = ul;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01002139 }
Willy Tarreau63364ee2012-04-19 19:11:13 +02002140 } /* end of args processing */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01002141
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01002142
2143 if (!strcmp(expr->kw->kw, "http_auth_group")) {
Willy Tarreau63364ee2012-04-19 19:11:13 +02002144 /* note: argument resolved above thanks to ARGT_USR */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01002145
2146 if (LIST_ISEMPTY(&expr->patterns)) {
2147 Alert("proxy %s: acl %s %s(): no groups specified.\n",
2148 p->id, acl->name, expr->kw->kw);
2149 cfgerr++;
2150 continue;
2151 }
2152
2153 list_for_each_entry(pattern, &expr->patterns, list) {
Willy Tarreau34db1082012-04-19 17:16:54 +02002154 pattern->val.group_mask = auth_resolve_groups(expr->args->data.usr, pattern->ptr.str);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01002155
2156 free(pattern->ptr.str);
2157 pattern->ptr.str = NULL;
2158 pattern->len = 0;
2159
2160 if (!pattern->val.group_mask) {
2161 Alert("proxy %s: acl %s %s(): invalid group(s).\n",
2162 p->id, acl->name, expr->kw->kw);
2163 cfgerr++;
2164 continue;
2165 }
2166 }
2167 }
2168 }
2169 }
2170
2171 return cfgerr;
2172}
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02002173
Willy Tarreaua84d3742007-05-07 00:36:48 +02002174/************************************************************************/
2175/* All supported keywords must be declared here. */
2176/************************************************************************/
2177
Willy Tarreau61612d42012-04-19 18:42:05 +02002178/* Note: must not be declared <const> as its list will be overwritten.
2179 * Please take care of keeping this list alphabetically sorted.
2180 */
Willy Tarreaua84d3742007-05-07 00:36:48 +02002181static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau61612d42012-04-19 18:42:05 +02002182 { "always_false", acl_parse_nothing, acl_fetch_false, acl_match_nothing, ACL_USE_NOTHING, 0 },
2183 { "always_true", acl_parse_nothing, acl_fetch_true, acl_match_nothing, ACL_USE_NOTHING, 0 },
2184 { "rep_ssl_hello_type", acl_parse_int, acl_fetch_ssl_hello_type, acl_match_int, ACL_USE_L6RTR_VOLATILE, 0 },
2185 { "req_len", acl_parse_int, acl_fetch_req_len, acl_match_int, ACL_USE_L6REQ_VOLATILE, 0 },
2186 { "req_rdp_cookie", acl_parse_str, acl_fetch_rdp_cookie, acl_match_str, ACL_USE_L6REQ_VOLATILE|ACL_MAY_LOOKUP, ARG1(0,STR) },
2187 { "req_rdp_cookie_cnt", acl_parse_int, acl_fetch_rdp_cookie_cnt, acl_match_int, ACL_USE_L6REQ_VOLATILE, ARG1(0,STR) },
2188 { "req_ssl_hello_type", acl_parse_int, acl_fetch_ssl_hello_type, acl_match_int, ACL_USE_L6REQ_VOLATILE, 0 },
2189 { "req_ssl_sni", acl_parse_str, acl_fetch_ssl_hello_sni, acl_match_str, ACL_USE_L6REQ_VOLATILE|ACL_MAY_LOOKUP, 0 },
2190 { "req_ssl_ver", acl_parse_dotted_ver, acl_fetch_req_ssl_ver, acl_match_int, ACL_USE_L6REQ_VOLATILE, 0 },
2191 { "wait_end", acl_parse_nothing, acl_fetch_wait_end, acl_match_nothing, ACL_USE_NOTHING, 0 },
Willy Tarreaua84d3742007-05-07 00:36:48 +02002192 { NULL, NULL, NULL, NULL }
2193}};
2194
2195
2196__attribute__((constructor))
2197static void __acl_init(void)
2198{
2199 acl_register_keywords(&acl_kws);
2200}
2201
2202
2203/*
2204 * Local variables:
2205 * c-indent-level: 8
2206 * c-basic-offset: 8
2207 * End:
2208 */