blob: 5d86a3e1a1f49489ad314838417dfe7f1c7f6044 [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 Tarreau21e5b0e2012-04-23 19:25:44 +0200626/* Executes a regex. It temporarily changes the data to add a trailing zero,
627 * and restores the previous character when leaving.
Willy Tarreauf3d25982007-05-08 22:45:09 +0200628 */
Willy Tarreau37406352012-04-23 16:16:37 +0200629int acl_match_reg(struct sample *smp, struct acl_pattern *pattern)
Willy Tarreauf3d25982007-05-08 22:45:09 +0200630{
631 char old_char;
632 int ret;
633
Willy Tarreauf853c462012-04-23 18:53:56 +0200634 old_char = smp->data.str.str[smp->data.str.len];
635 smp->data.str.str[smp->data.str.len] = 0;
Willy Tarreauf3d25982007-05-08 22:45:09 +0200636
Willy Tarreauf853c462012-04-23 18:53:56 +0200637 if (regexec(pattern->ptr.reg, smp->data.str.str, 0, NULL, 0) == 0)
Willy Tarreau11382812008-07-09 16:18:21 +0200638 ret = ACL_PAT_PASS;
Willy Tarreauf3d25982007-05-08 22:45:09 +0200639 else
Willy Tarreau11382812008-07-09 16:18:21 +0200640 ret = ACL_PAT_FAIL;
Willy Tarreauf3d25982007-05-08 22:45:09 +0200641
Willy Tarreauf853c462012-04-23 18:53:56 +0200642 smp->data.str.str[smp->data.str.len] = old_char;
Willy Tarreauf3d25982007-05-08 22:45:09 +0200643 return ret;
644}
645
Willy Tarreaua84d3742007-05-07 00:36:48 +0200646/* Checks that the pattern matches the beginning of the tested string. */
Willy Tarreau37406352012-04-23 16:16:37 +0200647int acl_match_beg(struct sample *smp, struct acl_pattern *pattern)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200648{
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200649 int icase;
650
Willy Tarreauf853c462012-04-23 18:53:56 +0200651 if (pattern->len > smp->data.str.len)
Willy Tarreau11382812008-07-09 16:18:21 +0200652 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200653
654 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
Willy Tarreauf853c462012-04-23 18:53:56 +0200655 if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str, pattern->len) != 0) ||
656 (!icase && strncmp(pattern->ptr.str, smp->data.str.str, pattern->len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +0200657 return ACL_PAT_FAIL;
658 return ACL_PAT_PASS;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200659}
660
661/* Checks that the pattern matches the end of the tested string. */
Willy Tarreau37406352012-04-23 16:16:37 +0200662int acl_match_end(struct sample *smp, struct acl_pattern *pattern)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200663{
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200664 int icase;
665
Willy Tarreauf853c462012-04-23 18:53:56 +0200666 if (pattern->len > smp->data.str.len)
Willy Tarreau11382812008-07-09 16:18:21 +0200667 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200668 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
Willy Tarreauf853c462012-04-23 18:53:56 +0200669 if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str + smp->data.str.len - pattern->len, pattern->len) != 0) ||
670 (!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 +0200671 return ACL_PAT_FAIL;
672 return ACL_PAT_PASS;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200673}
674
675/* Checks that the pattern is included inside the tested string.
676 * NB: Suboptimal, should be rewritten using a Boyer-Moore method.
677 */
Willy Tarreau37406352012-04-23 16:16:37 +0200678int acl_match_sub(struct sample *smp, struct acl_pattern *pattern)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200679{
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200680 int icase;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200681 char *end;
682 char *c;
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 Tarreaua84d3742007-05-07 00:36:48 +0200686
Willy Tarreauf853c462012-04-23 18:53:56 +0200687 end = smp->data.str.str + smp->data.str.len - pattern->len;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200688 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
689 if (icase) {
Willy Tarreauf853c462012-04-23 18:53:56 +0200690 for (c = smp->data.str.str; c <= end; c++) {
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200691 if (tolower(*c) != tolower(*pattern->ptr.str))
692 continue;
693 if (strncasecmp(pattern->ptr.str, c, pattern->len) == 0)
Willy Tarreau11382812008-07-09 16:18:21 +0200694 return ACL_PAT_PASS;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200695 }
696 } else {
Willy Tarreauf853c462012-04-23 18:53:56 +0200697 for (c = smp->data.str.str; c <= end; c++) {
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200698 if (*c != *pattern->ptr.str)
699 continue;
700 if (strncmp(pattern->ptr.str, c, pattern->len) == 0)
Willy Tarreau11382812008-07-09 16:18:21 +0200701 return ACL_PAT_PASS;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200702 }
Willy Tarreaua84d3742007-05-07 00:36:48 +0200703 }
Willy Tarreau11382812008-07-09 16:18:21 +0200704 return ACL_PAT_FAIL;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200705}
706
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200707/* Background: Fast way to find a zero byte in a word
708 * http://graphics.stanford.edu/~seander/bithacks.html#ZeroInWord
709 * hasZeroByte = (v - 0x01010101UL) & ~v & 0x80808080UL;
710 *
711 * To look for 4 different byte values, xor the word with those bytes and
712 * then check for zero bytes:
713 *
714 * v = (((unsigned char)c * 0x1010101U) ^ delimiter)
715 * where <delimiter> is the 4 byte values to look for (as an uint)
716 * and <c> is the character that is being tested
717 */
718static inline unsigned int is_delimiter(unsigned char c, unsigned int mask)
719{
720 mask ^= (c * 0x01010101); /* propagate the char to all 4 bytes */
721 return (mask - 0x01010101) & ~mask & 0x80808080U;
722}
723
724static inline unsigned int make_4delim(unsigned char d1, unsigned char d2, unsigned char d3, unsigned char d4)
725{
726 return d1 << 24 | d2 << 16 | d3 << 8 | d4;
727}
728
Willy Tarreaua84d3742007-05-07 00:36:48 +0200729/* This one is used by other real functions. It checks that the pattern is
730 * included inside the tested string, but enclosed between the specified
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200731 * delimiters or at the beginning or end of the string. The delimiters are
732 * provided as an unsigned int made by make_4delim() and match up to 4 different
733 * delimiters. Delimiters are stripped at the beginning and end of the pattern.
Willy Tarreaua84d3742007-05-07 00:36:48 +0200734 */
Willy Tarreau37406352012-04-23 16:16:37 +0200735static int match_word(struct sample *smp, struct acl_pattern *pattern, unsigned int delimiters)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200736{
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200737 int may_match, icase;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200738 char *c, *end;
739 char *ps;
740 int pl;
741
742 pl = pattern->len;
743 ps = pattern->ptr.str;
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200744
745 while (pl > 0 && is_delimiter(*ps, delimiters)) {
Willy Tarreaua84d3742007-05-07 00:36:48 +0200746 pl--;
747 ps++;
748 }
749
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200750 while (pl > 0 && is_delimiter(ps[pl - 1], delimiters))
Willy Tarreaua84d3742007-05-07 00:36:48 +0200751 pl--;
752
Willy Tarreauf853c462012-04-23 18:53:56 +0200753 if (pl > smp->data.str.len)
Willy Tarreau11382812008-07-09 16:18:21 +0200754 return ACL_PAT_FAIL;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200755
756 may_match = 1;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200757 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
Willy Tarreauf853c462012-04-23 18:53:56 +0200758 end = smp->data.str.str + smp->data.str.len - pl;
759 for (c = smp->data.str.str; c <= end; c++) {
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200760 if (is_delimiter(*c, delimiters)) {
Willy Tarreaua84d3742007-05-07 00:36:48 +0200761 may_match = 1;
762 continue;
763 }
Willy Tarreaua84d3742007-05-07 00:36:48 +0200764
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200765 if (!may_match)
766 continue;
767
768 if (icase) {
769 if ((tolower(*c) == tolower(*ps)) &&
770 (strncasecmp(ps, c, pl) == 0) &&
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200771 (c == end || is_delimiter(c[pl], delimiters)))
Willy Tarreau11382812008-07-09 16:18:21 +0200772 return ACL_PAT_PASS;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200773 } else {
774 if ((*c == *ps) &&
775 (strncmp(ps, c, pl) == 0) &&
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200776 (c == end || is_delimiter(c[pl], delimiters)))
Willy Tarreau11382812008-07-09 16:18:21 +0200777 return ACL_PAT_PASS;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200778 }
Willy Tarreaua84d3742007-05-07 00:36:48 +0200779 may_match = 0;
780 }
Willy Tarreau11382812008-07-09 16:18:21 +0200781 return ACL_PAT_FAIL;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200782}
783
784/* Checks that the pattern is included inside the tested string, but enclosed
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200785 * between the delimiters '?' or '/' or at the beginning or end of the string.
786 * Delimiters at the beginning or end of the pattern are ignored.
Willy Tarreaua84d3742007-05-07 00:36:48 +0200787 */
Willy Tarreau37406352012-04-23 16:16:37 +0200788int acl_match_dir(struct sample *smp, struct acl_pattern *pattern)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200789{
Willy Tarreau37406352012-04-23 16:16:37 +0200790 return match_word(smp, pattern, make_4delim('/', '?', '?', '?'));
Willy Tarreaua84d3742007-05-07 00:36:48 +0200791}
792
793/* Checks that the pattern is included inside the tested string, but enclosed
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200794 * between the delmiters '/', '?', '.' or ":" or at the beginning or end of
795 * the string. Delimiters at the beginning or end of the pattern are ignored.
Willy Tarreaua84d3742007-05-07 00:36:48 +0200796 */
Willy Tarreau37406352012-04-23 16:16:37 +0200797int acl_match_dom(struct sample *smp, struct acl_pattern *pattern)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200798{
Willy Tarreau37406352012-04-23 16:16:37 +0200799 return match_word(smp, pattern, make_4delim('/', '?', '.', ':'));
Willy Tarreaua84d3742007-05-07 00:36:48 +0200800}
801
802/* Checks that the integer in <test> is included between min and max */
Willy Tarreau37406352012-04-23 16:16:37 +0200803int acl_match_int(struct sample *smp, struct acl_pattern *pattern)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200804{
Willy Tarreauf853c462012-04-23 18:53:56 +0200805 if ((!pattern->val.range.min_set || pattern->val.range.min <= smp->data.uint) &&
806 (!pattern->val.range.max_set || smp->data.uint <= pattern->val.range.max))
Willy Tarreau11382812008-07-09 16:18:21 +0200807 return ACL_PAT_PASS;
808 return ACL_PAT_FAIL;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200809}
810
Willy Tarreau0e698542011-09-16 08:32:32 +0200811/* Checks that the length of the pattern in <test> is included between min and max */
Willy Tarreau37406352012-04-23 16:16:37 +0200812int acl_match_len(struct sample *smp, struct acl_pattern *pattern)
Willy Tarreau0e698542011-09-16 08:32:32 +0200813{
Willy Tarreauf853c462012-04-23 18:53:56 +0200814 if ((!pattern->val.range.min_set || pattern->val.range.min <= smp->data.str.len) &&
815 (!pattern->val.range.max_set || smp->data.str.len <= pattern->val.range.max))
Willy Tarreau0e698542011-09-16 08:32:32 +0200816 return ACL_PAT_PASS;
817 return ACL_PAT_FAIL;
818}
819
Willy Tarreau37406352012-04-23 16:16:37 +0200820int acl_match_ip(struct sample *smp, struct acl_pattern *pattern)
Willy Tarreaua67fad92007-05-08 19:50:09 +0200821{
822 struct in_addr *s;
823
Willy Tarreauf853c462012-04-23 18:53:56 +0200824 if (smp->type != SMP_T_IPV4)
Willy Tarreau11382812008-07-09 16:18:21 +0200825 return ACL_PAT_FAIL;
Willy Tarreaua67fad92007-05-08 19:50:09 +0200826
Willy Tarreauf853c462012-04-23 18:53:56 +0200827 s = &smp->data.ipv4;
Willy Tarreaua67fad92007-05-08 19:50:09 +0200828 if (((s->s_addr ^ pattern->val.ipv4.addr.s_addr) & pattern->val.ipv4.mask.s_addr) == 0)
Willy Tarreau11382812008-07-09 16:18:21 +0200829 return ACL_PAT_PASS;
830 return ACL_PAT_FAIL;
Willy Tarreaua67fad92007-05-08 19:50:09 +0200831}
832
Willy Tarreaub337b532010-05-13 20:03:41 +0200833/* Lookup an IPv4 address in the expression's pattern tree using the longest
834 * match method. The node is returned if it exists, otherwise NULL.
835 */
Willy Tarreau37406352012-04-23 16:16:37 +0200836static void *acl_lookup_ip(struct sample *smp, struct acl_expr *expr)
Willy Tarreaub337b532010-05-13 20:03:41 +0200837{
838 struct in_addr *s;
839
Willy Tarreauf853c462012-04-23 18:53:56 +0200840 if (smp->type != SMP_T_IPV4)
Willy Tarreaub337b532010-05-13 20:03:41 +0200841 return ACL_PAT_FAIL;
842
Willy Tarreauf853c462012-04-23 18:53:56 +0200843 s = &smp->data.ipv4;
Willy Tarreaub337b532010-05-13 20:03:41 +0200844 return ebmb_lookup_longest(&expr->pattern_tree, &s->s_addr);
845}
846
Willy Tarreaua84d3742007-05-07 00:36:48 +0200847/* Parse a string. It is allocated and duplicated. */
Willy Tarreauae8b7962007-06-09 23:10:04 +0200848int acl_parse_str(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200849{
850 int len;
851
Willy Tarreauae8b7962007-06-09 23:10:04 +0200852 len = strlen(*text);
Willy Tarreauc4262962010-05-10 23:42:40 +0200853
854 if (pattern->flags & ACL_PAT_F_TREE_OK) {
855 /* we're allowed to put the data in a tree whose root is pointed
856 * to by val.tree.
857 */
858 struct ebmb_node *node;
859
860 node = calloc(1, sizeof(*node) + len + 1);
861 if (!node)
862 return 0;
863 memcpy(node->key, *text, len + 1);
864 if (ebst_insert(pattern->val.tree, node) != node)
865 free(node); /* was a duplicate */
866 pattern->flags |= ACL_PAT_F_TREE; /* this pattern now contains a tree */
867 return 1;
868 }
869
Willy Tarreauae8b7962007-06-09 23:10:04 +0200870 pattern->ptr.str = strdup(*text);
Willy Tarreaua84d3742007-05-07 00:36:48 +0200871 if (!pattern->ptr.str)
872 return 0;
873 pattern->len = len;
874 return 1;
875}
876
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +0100877/* Parse and concatenate all further strings into one. */
878int
879acl_parse_strcat(const char **text, struct acl_pattern *pattern, int *opaque)
880{
881
882 int len = 0, i;
883 char *s;
884
885 for (i = 0; *text[i]; i++)
886 len += strlen(text[i])+1;
887
888 pattern->ptr.str = s = calloc(1, len);
889 if (!pattern->ptr.str)
890 return 0;
891
892 for (i = 0; *text[i]; i++)
893 s += sprintf(s, i?" %s":"%s", text[i]);
894
895 pattern->len = len;
896
897 return i;
898}
899
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +0200900/* Free data allocated by acl_parse_reg */
Willy Tarreau37406352012-04-23 16:16:37 +0200901static void acl_free_reg(void *ptr)
902{
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +0200903 regfree((regex_t *)ptr);
904}
905
Willy Tarreauf3d25982007-05-08 22:45:09 +0200906/* Parse a regex. It is allocated. */
Willy Tarreauae8b7962007-06-09 23:10:04 +0200907int acl_parse_reg(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreauf3d25982007-05-08 22:45:09 +0200908{
909 regex_t *preg;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200910 int icase;
Willy Tarreauf3d25982007-05-08 22:45:09 +0200911
912 preg = calloc(1, sizeof(regex_t));
913
914 if (!preg)
915 return 0;
916
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200917 icase = (pattern->flags & ACL_PAT_F_IGNORE_CASE) ? REG_ICASE : 0;
918 if (regcomp(preg, *text, REG_EXTENDED | REG_NOSUB | icase) != 0) {
Willy Tarreauf3d25982007-05-08 22:45:09 +0200919 free(preg);
920 return 0;
921 }
922
923 pattern->ptr.reg = preg;
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +0200924 pattern->freeptrbuf = &acl_free_reg;
Willy Tarreauf3d25982007-05-08 22:45:09 +0200925 return 1;
926}
927
Willy Tarreauae8b7962007-06-09 23:10:04 +0200928/* Parse a range of positive integers delimited by either ':' or '-'. If only
929 * one integer is read, it is set as both min and max. An operator may be
930 * specified as the prefix, among this list of 5 :
931 *
932 * 0:eq, 1:gt, 2:ge, 3:lt, 4:le
933 *
934 * The default operator is "eq". It supports range matching. Ranges are
935 * rejected for other operators. The operator may be changed at any time.
936 * The operator is stored in the 'opaque' argument.
937 *
Willy Tarreaua84d3742007-05-07 00:36:48 +0200938 */
Willy Tarreauae8b7962007-06-09 23:10:04 +0200939int acl_parse_int(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200940{
Willy Tarreauae8b7962007-06-09 23:10:04 +0200941 signed long long i;
942 unsigned int j, last, skip = 0;
943 const char *ptr = *text;
944
945
Willy Tarreau8f8e6452007-06-17 21:51:38 +0200946 while (!isdigit((unsigned char)*ptr)) {
Willy Tarreau1c7cc5b2010-07-18 10:46:33 +0200947 switch (get_std_op(ptr)) {
948 case STD_OP_EQ: *opaque = 0; break;
949 case STD_OP_GT: *opaque = 1; break;
950 case STD_OP_GE: *opaque = 2; break;
951 case STD_OP_LT: *opaque = 3; break;
952 case STD_OP_LE: *opaque = 4; break;
953 default:
Willy Tarreauae8b7962007-06-09 23:10:04 +0200954 return 0;
Willy Tarreau1c7cc5b2010-07-18 10:46:33 +0200955 }
Willy Tarreauae8b7962007-06-09 23:10:04 +0200956
957 skip++;
958 ptr = text[skip];
959 }
Willy Tarreaua84d3742007-05-07 00:36:48 +0200960
961 last = i = 0;
962 while (1) {
Willy Tarreauae8b7962007-06-09 23:10:04 +0200963 j = *ptr++;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200964 if ((j == '-' || j == ':') && !last) {
965 last++;
966 pattern->val.range.min = i;
967 i = 0;
968 continue;
969 }
970 j -= '0';
971 if (j > 9)
972 // also catches the terminating zero
973 break;
974 i *= 10;
975 i += j;
976 }
Willy Tarreauae8b7962007-06-09 23:10:04 +0200977
978 if (last && *opaque >= 1 && *opaque <= 4)
979 /* having a range with a min or a max is absurd */
980 return 0;
981
Willy Tarreaua84d3742007-05-07 00:36:48 +0200982 if (!last)
983 pattern->val.range.min = i;
984 pattern->val.range.max = i;
Willy Tarreauae8b7962007-06-09 23:10:04 +0200985
986 switch (*opaque) {
987 case 0: /* eq */
988 pattern->val.range.min_set = 1;
989 pattern->val.range.max_set = 1;
990 break;
991 case 1: /* gt */
992 pattern->val.range.min++; /* gt = ge + 1 */
993 case 2: /* ge */
994 pattern->val.range.min_set = 1;
995 pattern->val.range.max_set = 0;
996 break;
997 case 3: /* lt */
998 pattern->val.range.max--; /* lt = le - 1 */
999 case 4: /* le */
1000 pattern->val.range.min_set = 0;
1001 pattern->val.range.max_set = 1;
1002 break;
1003 }
1004 return skip + 1;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001005}
1006
Willy Tarreau4a26d2f2008-07-15 16:05:33 +02001007/* Parse a range of positive 2-component versions delimited by either ':' or
1008 * '-'. The version consists in a major and a minor, both of which must be
1009 * smaller than 65536, because internally they will be represented as a 32-bit
1010 * integer.
1011 * If only one version is read, it is set as both min and max. Just like for
1012 * pure integers, an operator may be specified as the prefix, among this list
1013 * of 5 :
1014 *
1015 * 0:eq, 1:gt, 2:ge, 3:lt, 4:le
1016 *
1017 * The default operator is "eq". It supports range matching. Ranges are
1018 * rejected for other operators. The operator may be changed at any time.
1019 * The operator is stored in the 'opaque' argument. This allows constructs
1020 * such as the following one :
1021 *
1022 * acl obsolete_ssl ssl_req_proto lt 3
1023 * acl unsupported_ssl ssl_req_proto gt 3.1
1024 * acl valid_ssl ssl_req_proto 3.0-3.1
1025 *
1026 */
1027int acl_parse_dotted_ver(const char **text, struct acl_pattern *pattern, int *opaque)
1028{
1029 signed long long i;
1030 unsigned int j, last, skip = 0;
1031 const char *ptr = *text;
1032
1033
1034 while (!isdigit((unsigned char)*ptr)) {
Willy Tarreau1c7cc5b2010-07-18 10:46:33 +02001035 switch (get_std_op(ptr)) {
1036 case STD_OP_EQ: *opaque = 0; break;
1037 case STD_OP_GT: *opaque = 1; break;
1038 case STD_OP_GE: *opaque = 2; break;
1039 case STD_OP_LT: *opaque = 3; break;
1040 case STD_OP_LE: *opaque = 4; break;
1041 default:
Willy Tarreau4a26d2f2008-07-15 16:05:33 +02001042 return 0;
Willy Tarreau1c7cc5b2010-07-18 10:46:33 +02001043 }
Willy Tarreau4a26d2f2008-07-15 16:05:33 +02001044
1045 skip++;
1046 ptr = text[skip];
1047 }
1048
1049 last = i = 0;
1050 while (1) {
1051 j = *ptr++;
1052 if (j == '.') {
1053 /* minor part */
1054 if (i >= 65536)
1055 return 0;
1056 i <<= 16;
1057 continue;
1058 }
1059 if ((j == '-' || j == ':') && !last) {
1060 last++;
1061 if (i < 65536)
1062 i <<= 16;
1063 pattern->val.range.min = i;
1064 i = 0;
1065 continue;
1066 }
1067 j -= '0';
1068 if (j > 9)
1069 // also catches the terminating zero
1070 break;
1071 i = (i & 0xFFFF0000) + (i & 0xFFFF) * 10;
1072 i += j;
1073 }
1074
1075 /* if we only got a major version, let's shift it now */
1076 if (i < 65536)
1077 i <<= 16;
1078
1079 if (last && *opaque >= 1 && *opaque <= 4)
1080 /* having a range with a min or a max is absurd */
1081 return 0;
1082
1083 if (!last)
1084 pattern->val.range.min = i;
1085 pattern->val.range.max = i;
1086
1087 switch (*opaque) {
1088 case 0: /* eq */
1089 pattern->val.range.min_set = 1;
1090 pattern->val.range.max_set = 1;
1091 break;
1092 case 1: /* gt */
1093 pattern->val.range.min++; /* gt = ge + 1 */
1094 case 2: /* ge */
1095 pattern->val.range.min_set = 1;
1096 pattern->val.range.max_set = 0;
1097 break;
1098 case 3: /* lt */
1099 pattern->val.range.max--; /* lt = le - 1 */
1100 case 4: /* le */
1101 pattern->val.range.min_set = 0;
1102 pattern->val.range.max_set = 1;
1103 break;
1104 }
1105 return skip + 1;
1106}
1107
Willy Tarreaua67fad92007-05-08 19:50:09 +02001108/* Parse an IP address and an optional mask in the form addr[/mask].
1109 * The addr may either be an IPv4 address or a hostname. The mask
1110 * may either be a dotted mask or a number of bits. Returns 1 if OK,
1111 * otherwise 0.
1112 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02001113int acl_parse_ip(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreaua67fad92007-05-08 19:50:09 +02001114{
Willy Tarreaub337b532010-05-13 20:03:41 +02001115 struct eb_root *tree = NULL;
1116 if (pattern->flags & ACL_PAT_F_TREE_OK)
1117 tree = pattern->val.tree;
1118
1119 if (str2net(*text, &pattern->val.ipv4.addr, &pattern->val.ipv4.mask)) {
1120 unsigned int mask = ntohl(pattern->val.ipv4.mask.s_addr);
1121 struct ebmb_node *node;
1122 /* check if the mask is contiguous so that we can insert the
1123 * network into the tree. A continuous mask has only ones on
1124 * the left. This means that this mask + its lower bit added
1125 * once again is null.
1126 */
1127 if (mask + (mask & -mask) == 0 && tree) {
1128 mask = mask ? 33 - flsnz(mask & -mask) : 0; /* equals cidr value */
1129 /* FIXME: insert <addr>/<mask> into the tree here */
1130 node = calloc(1, sizeof(*node) + 4); /* reserve 4 bytes for IPv4 address */
1131 if (!node)
1132 return 0;
1133 memcpy(node->key, &pattern->val.ipv4.addr, 4); /* network byte order */
1134 node->node.pfx = mask;
1135 if (ebmb_insert_prefix(tree, node, 4) != node)
1136 free(node); /* was a duplicate */
1137 pattern->flags |= ACL_PAT_F_TREE;
1138 return 1;
1139 }
Willy Tarreauae8b7962007-06-09 23:10:04 +02001140 return 1;
Willy Tarreaub337b532010-05-13 20:03:41 +02001141 }
Willy Tarreauae8b7962007-06-09 23:10:04 +02001142 else
1143 return 0;
Willy Tarreaua67fad92007-05-08 19:50:09 +02001144}
1145
Willy Tarreaua84d3742007-05-07 00:36:48 +02001146/*
1147 * Registers the ACL keyword list <kwl> as a list of valid keywords for next
1148 * parsing sessions.
1149 */
1150void acl_register_keywords(struct acl_kw_list *kwl)
1151{
1152 LIST_ADDQ(&acl_keywords.list, &kwl->list);
1153}
1154
1155/*
1156 * Unregisters the ACL keyword list <kwl> from the list of valid keywords.
1157 */
1158void acl_unregister_keywords(struct acl_kw_list *kwl)
1159{
1160 LIST_DEL(&kwl->list);
1161 LIST_INIT(&kwl->list);
1162}
1163
1164/* Return a pointer to the ACL <name> within the list starting at <head>, or
1165 * NULL if not found.
1166 */
1167struct acl *find_acl_by_name(const char *name, struct list *head)
1168{
1169 struct acl *acl;
1170 list_for_each_entry(acl, head, list) {
1171 if (strcmp(acl->name, name) == 0)
1172 return acl;
1173 }
1174 return NULL;
1175}
1176
1177/* Return a pointer to the ACL keyword <kw>, or NULL if not found. Note that if
1178 * <kw> contains an opening parenthesis, only the left part of it is checked.
1179 */
1180struct acl_keyword *find_acl_kw(const char *kw)
1181{
1182 int index;
1183 const char *kwend;
1184 struct acl_kw_list *kwl;
1185
1186 kwend = strchr(kw, '(');
1187 if (!kwend)
1188 kwend = kw + strlen(kw);
1189
1190 list_for_each_entry(kwl, &acl_keywords.list, list) {
1191 for (index = 0; kwl->kw[index].kw != NULL; index++) {
1192 if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) &&
1193 kwl->kw[index].kw[kwend-kw] == 0)
1194 return &kwl->kw[index];
1195 }
1196 }
1197 return NULL;
1198}
1199
Willy Tarreaudfd7fca2011-03-09 07:27:02 +01001200/* NB: does nothing if <pat> is NULL */
Willy Tarreaua84d3742007-05-07 00:36:48 +02001201static void free_pattern(struct acl_pattern *pat)
1202{
Willy Tarreaudfd7fca2011-03-09 07:27:02 +01001203 if (!pat)
1204 return;
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02001205
1206 if (pat->ptr.ptr) {
1207 if (pat->freeptrbuf)
1208 pat->freeptrbuf(pat->ptr.ptr);
1209
Willy Tarreaua84d3742007-05-07 00:36:48 +02001210 free(pat->ptr.ptr);
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02001211 }
1212
Willy Tarreaua84d3742007-05-07 00:36:48 +02001213 free(pat);
1214}
1215
1216static void free_pattern_list(struct list *head)
1217{
1218 struct acl_pattern *pat, *tmp;
1219 list_for_each_entry_safe(pat, tmp, head, list)
1220 free_pattern(pat);
1221}
1222
Willy Tarreaue56cda92010-05-11 23:25:05 +02001223static void free_pattern_tree(struct eb_root *root)
1224{
1225 struct eb_node *node, *next;
1226 node = eb_first(root);
1227 while (node) {
1228 next = eb_next(node);
1229 free(node);
1230 node = next;
1231 }
1232}
1233
Willy Tarreaua84d3742007-05-07 00:36:48 +02001234static struct acl_expr *prune_acl_expr(struct acl_expr *expr)
1235{
Willy Tarreau34db1082012-04-19 17:16:54 +02001236 struct arg *arg;
1237
Willy Tarreaua84d3742007-05-07 00:36:48 +02001238 free_pattern_list(&expr->patterns);
Willy Tarreaue56cda92010-05-11 23:25:05 +02001239 free_pattern_tree(&expr->pattern_tree);
Willy Tarreaua84d3742007-05-07 00:36:48 +02001240 LIST_INIT(&expr->patterns);
Willy Tarreau34db1082012-04-19 17:16:54 +02001241
1242 for (arg = expr->args; arg; arg++) {
1243 if (arg->type == ARGT_STOP)
1244 break;
1245 if (arg->type == ARGT_FE || arg->type == ARGT_BE ||
1246 arg->type == ARGT_TAB || arg->type == ARGT_SRV ||
1247 arg->type == ARGT_USR || arg->type == ARGT_STR) {
1248 free(arg->data.str.str);
1249 arg->data.str.str = NULL;
1250 }
1251 arg++;
1252 }
1253
1254 free(expr->args);
Willy Tarreaua84d3742007-05-07 00:36:48 +02001255 expr->kw->use_cnt--;
1256 return expr;
1257}
1258
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001259static int acl_read_patterns_from_file( struct acl_keyword *aclkw,
1260 struct acl_expr *expr,
1261 const char *filename, int patflags)
1262{
1263 FILE *file;
1264 char *c;
1265 const char *args[2];
1266 struct acl_pattern *pattern;
1267 int opaque;
Willy Tarreau6a8097f2011-02-26 15:14:15 +01001268 int ret = 0;
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001269
1270 file = fopen(filename, "r");
1271 if (!file)
1272 return 0;
1273
1274 /* now parse all patterns. The file may contain only one pattern per
1275 * line. If the line contains spaces, they will be part of the pattern.
1276 * The pattern stops at the first CR, LF or EOF encountered.
1277 */
1278 opaque = 0;
Willy Tarreaue56cda92010-05-11 23:25:05 +02001279 pattern = NULL;
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001280 args[1] = "";
1281 while (fgets(trash, sizeof(trash), file) != NULL) {
1282
1283 c = trash;
Willy Tarreau58215a02010-05-13 22:07:43 +02001284
1285 /* ignore lines beginning with a dash */
1286 if (*c == '#')
1287 continue;
1288
1289 /* strip leading spaces and tabs */
1290 while (*c == ' ' || *c == '\t')
1291 c++;
1292
Willy Tarreau58215a02010-05-13 22:07:43 +02001293
1294 args[0] = c;
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001295 while (*c && *c != '\n' && *c != '\r')
1296 c++;
1297 *c = 0;
1298
Willy Tarreau51091962011-01-03 21:04:10 +01001299 /* empty lines are ignored too */
1300 if (c == args[0])
1301 continue;
1302
Willy Tarreaue56cda92010-05-11 23:25:05 +02001303 /* we keep the previous pattern along iterations as long as it's not used */
1304 if (!pattern)
1305 pattern = (struct acl_pattern *)malloc(sizeof(*pattern));
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001306 if (!pattern)
1307 goto out_close;
Willy Tarreaue56cda92010-05-11 23:25:05 +02001308
1309 memset(pattern, 0, sizeof(*pattern));
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001310 pattern->flags = patflags;
1311
Willy Tarreaue56cda92010-05-11 23:25:05 +02001312 if ((aclkw->requires & ACL_MAY_LOOKUP) && !(pattern->flags & ACL_PAT_F_IGNORE_CASE)) {
1313 /* we pre-set the data pointer to the tree's head so that functions
1314 * which are able to insert in a tree know where to do that.
1315 */
1316 pattern->flags |= ACL_PAT_F_TREE_OK;
1317 pattern->val.tree = &expr->pattern_tree;
1318 }
1319
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001320 if (!aclkw->parse(args, pattern, &opaque))
1321 goto out_free_pattern;
Willy Tarreaue56cda92010-05-11 23:25:05 +02001322
1323 /* if the parser did not feed the tree, let's chain the pattern to the list */
1324 if (!(pattern->flags & ACL_PAT_F_TREE)) {
1325 LIST_ADDQ(&expr->patterns, &pattern->list);
1326 pattern = NULL; /* get a new one */
1327 }
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001328 }
Willy Tarreau6a8097f2011-02-26 15:14:15 +01001329
1330 ret = 1; /* success */
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001331
1332 out_free_pattern:
1333 free_pattern(pattern);
1334 out_close:
1335 fclose(file);
Willy Tarreau6a8097f2011-02-26 15:14:15 +01001336 return ret;
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001337}
1338
Willy Tarreaua84d3742007-05-07 00:36:48 +02001339/* Parse an ACL expression starting at <args>[0], and return it.
1340 * Right now, the only accepted syntax is :
1341 * <subject> [<value>...]
1342 */
1343struct acl_expr *parse_acl_expr(const char **args)
1344{
1345 __label__ out_return, out_free_expr, out_free_pattern;
1346 struct acl_expr *expr;
1347 struct acl_keyword *aclkw;
1348 struct acl_pattern *pattern;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02001349 int opaque, patflags;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001350 const char *arg;
1351
1352 aclkw = find_acl_kw(args[0]);
1353 if (!aclkw || !aclkw->parse)
1354 goto out_return;
1355
1356 expr = (struct acl_expr *)calloc(1, sizeof(*expr));
1357 if (!expr)
1358 goto out_return;
1359
1360 expr->kw = aclkw;
1361 aclkw->use_cnt++;
1362 LIST_INIT(&expr->patterns);
Willy Tarreaue56cda92010-05-11 23:25:05 +02001363 expr->pattern_tree = EB_ROOT_UNIQUE;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001364
1365 arg = strchr(args[0], '(');
Willy Tarreau61612d42012-04-19 18:42:05 +02001366 if (aclkw->arg_mask) {
1367 int nbargs = 0;
Willy Tarreau34db1082012-04-19 17:16:54 +02001368 char *end;
Willy Tarreau34db1082012-04-19 17:16:54 +02001369
Willy Tarreau61612d42012-04-19 18:42:05 +02001370 if (arg != NULL) {
1371 /* there are 0 or more arguments in the form "subject(arg[,arg]*)" */
1372 arg++;
1373 end = strchr(arg, ')');
1374 if (!end)
1375 goto out_free_expr;
Willy Tarreau34db1082012-04-19 17:16:54 +02001376
Willy Tarreau61612d42012-04-19 18:42:05 +02001377 /* Parse the arguments. Note that currently we have no way to
1378 * report parsing errors, hence the NULL in the error pointers.
1379 * An error is also reported if some mandatory arguments are
1380 * missing.
1381 */
1382 nbargs = make_arg_list(arg, end - arg, aclkw->arg_mask, &expr->args,
1383 NULL, NULL, NULL);
1384 if (nbargs < 0)
1385 goto out_free_expr;
1386 }
Willy Tarreaufc2c1fd2012-04-19 23:35:54 +02001387 else if (ARGM(aclkw->arg_mask) == 1) {
1388 int type = (aclkw->arg_mask >> 4) & 15;
1389
1390 /* If a proxy is noted as a mandatory argument, we'll fake
1391 * an empty one so that acl_find_targets() resolves it as
1392 * the current one later.
1393 */
1394 if (type != ARGT_FE && type != ARGT_BE && type != ARGT_TAB)
1395 goto out_free_expr;
1396
1397 /* Build an arg list containing the type as an empty string
1398 * and the usual STOP.
1399 */
1400 expr->args = calloc(2, sizeof(*expr->args));
1401 expr->args[0].type = type;
1402 expr->args[0].data.str.str = strdup("");
1403 expr->args[0].data.str.len = 1;
1404 expr->args[0].data.str.len = 0;
1405 expr->args[1].type = ARGT_STOP;
1406 }
Willy Tarreau61612d42012-04-19 18:42:05 +02001407 else if (ARGM(aclkw->arg_mask)) {
1408 /* there were some mandatory arguments */
Willy Tarreaua84d3742007-05-07 00:36:48 +02001409 goto out_free_expr;
Willy Tarreau61612d42012-04-19 18:42:05 +02001410 }
1411 }
1412 else {
1413 if (arg) {
1414 /* no argument expected */
1415 goto out_free_expr;
1416 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001417 }
1418
Willy Tarreaua84d3742007-05-07 00:36:48 +02001419 args++;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02001420
1421 /* check for options before patterns. Supported options are :
1422 * -i : ignore case for all patterns by default
1423 * -f : read patterns from those files
1424 * -- : everything after this is not an option
1425 */
1426 patflags = 0;
1427 while (**args == '-') {
1428 if ((*args)[1] == 'i')
1429 patflags |= ACL_PAT_F_IGNORE_CASE;
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001430 else if ((*args)[1] == 'f') {
1431 if (!acl_read_patterns_from_file(aclkw, expr, args[1], patflags | ACL_PAT_F_FROM_FILE))
1432 goto out_free_expr;
1433 args++;
1434 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +02001435 else if ((*args)[1] == '-') {
1436 args++;
1437 break;
1438 }
1439 else
1440 break;
1441 args++;
1442 }
1443
1444 /* now parse all patterns */
Willy Tarreauae8b7962007-06-09 23:10:04 +02001445 opaque = 0;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001446 while (**args) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02001447 int ret;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001448 pattern = (struct acl_pattern *)calloc(1, sizeof(*pattern));
1449 if (!pattern)
1450 goto out_free_expr;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02001451 pattern->flags = patflags;
1452
Willy Tarreauae8b7962007-06-09 23:10:04 +02001453 ret = aclkw->parse(args, pattern, &opaque);
1454 if (!ret)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001455 goto out_free_pattern;
1456 LIST_ADDQ(&expr->patterns, &pattern->list);
Willy Tarreauae8b7962007-06-09 23:10:04 +02001457 args += ret;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001458 }
1459
1460 return expr;
1461
1462 out_free_pattern:
1463 free_pattern(pattern);
1464 out_free_expr:
1465 prune_acl_expr(expr);
1466 free(expr);
1467 out_return:
1468 return NULL;
1469}
1470
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02001471/* Purge everything in the acl <acl>, then return <acl>. */
1472struct acl *prune_acl(struct acl *acl) {
1473
1474 struct acl_expr *expr, *exprb;
1475
1476 free(acl->name);
1477
1478 list_for_each_entry_safe(expr, exprb, &acl->expr, list) {
1479 LIST_DEL(&expr->list);
1480 prune_acl_expr(expr);
1481 free(expr);
1482 }
1483
1484 return acl;
1485}
1486
Willy Tarreaua84d3742007-05-07 00:36:48 +02001487/* Parse an ACL with the name starting at <args>[0], and with a list of already
1488 * known ACLs in <acl>. If the ACL was not in the list, it will be added.
Willy Tarreau2a56c5e2010-03-15 16:13:29 +01001489 * A pointer to that ACL is returned. If the ACL has an empty name, then it's
1490 * an anonymous one and it won't be merged with any other one.
Willy Tarreaua84d3742007-05-07 00:36:48 +02001491 *
1492 * args syntax: <aclname> <acl_expr>
1493 */
1494struct acl *parse_acl(const char **args, struct list *known_acl)
1495{
1496 __label__ out_return, out_free_acl_expr, out_free_name;
1497 struct acl *cur_acl;
1498 struct acl_expr *acl_expr;
1499 char *name;
1500
Willy Tarreau2a56c5e2010-03-15 16:13:29 +01001501 if (**args && invalid_char(*args))
Willy Tarreau2e74c3f2007-12-02 18:45:09 +01001502 goto out_return;
1503
Willy Tarreaua84d3742007-05-07 00:36:48 +02001504 acl_expr = parse_acl_expr(args + 1);
1505 if (!acl_expr)
1506 goto out_return;
1507
Willy Tarreau404e8ab2009-07-26 19:40:40 +02001508 /* Check for args beginning with an opening parenthesis just after the
1509 * subject, as this is almost certainly a typo. Right now we can only
1510 * emit a warning, so let's do so.
1511 */
Krzysztof Piotr Oledzki4cdd8312009-10-05 00:23:35 +02001512 if (!strchr(args[1], '(') && *args[2] == '(')
Willy Tarreau404e8ab2009-07-26 19:40:40 +02001513 Warning("parsing acl '%s' :\n"
1514 " matching '%s' for pattern '%s' is likely a mistake and probably\n"
1515 " not what you want. Maybe you need to remove the extraneous space before '('.\n"
1516 " If you are really sure this is not an error, please insert '--' between the\n"
1517 " match and the pattern to make this warning message disappear.\n",
1518 args[0], args[1], args[2]);
1519
Willy Tarreau2a56c5e2010-03-15 16:13:29 +01001520 if (*args[0])
1521 cur_acl = find_acl_by_name(args[0], known_acl);
1522 else
1523 cur_acl = NULL;
1524
Willy Tarreaua84d3742007-05-07 00:36:48 +02001525 if (!cur_acl) {
1526 name = strdup(args[0]);
1527 if (!name)
1528 goto out_free_acl_expr;
1529 cur_acl = (struct acl *)calloc(1, sizeof(*cur_acl));
1530 if (cur_acl == NULL)
1531 goto out_free_name;
1532
1533 LIST_INIT(&cur_acl->expr);
1534 LIST_ADDQ(known_acl, &cur_acl->list);
1535 cur_acl->name = name;
1536 }
1537
Willy Tarreaua9802632008-07-25 19:13:19 +02001538 cur_acl->requires |= acl_expr->kw->requires;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001539 LIST_ADDQ(&cur_acl->expr, &acl_expr->list);
1540 return cur_acl;
1541
1542 out_free_name:
1543 free(name);
1544 out_free_acl_expr:
1545 prune_acl_expr(acl_expr);
1546 free(acl_expr);
1547 out_return:
1548 return NULL;
1549}
1550
Willy Tarreau16fbe822007-06-17 11:54:31 +02001551/* Some useful ACLs provided by default. Only those used are allocated. */
1552
1553const struct {
1554 const char *name;
1555 const char *expr[4]; /* put enough for longest expression */
1556} default_acl_list[] = {
Willy Tarreau58393e12008-07-20 10:39:22 +02001557 { .name = "TRUE", .expr = {"always_true",""}},
1558 { .name = "FALSE", .expr = {"always_false",""}},
Willy Tarreau16fbe822007-06-17 11:54:31 +02001559 { .name = "LOCALHOST", .expr = {"src","127.0.0.1/8",""}},
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001560 { .name = "HTTP", .expr = {"req_proto_http",""}},
Willy Tarreau16fbe822007-06-17 11:54:31 +02001561 { .name = "HTTP_1.0", .expr = {"req_ver","1.0",""}},
1562 { .name = "HTTP_1.1", .expr = {"req_ver","1.1",""}},
1563 { .name = "METH_CONNECT", .expr = {"method","CONNECT",""}},
1564 { .name = "METH_GET", .expr = {"method","GET","HEAD",""}},
1565 { .name = "METH_HEAD", .expr = {"method","HEAD",""}},
1566 { .name = "METH_OPTIONS", .expr = {"method","OPTIONS",""}},
1567 { .name = "METH_POST", .expr = {"method","POST",""}},
1568 { .name = "METH_TRACE", .expr = {"method","TRACE",""}},
1569 { .name = "HTTP_URL_ABS", .expr = {"url_reg","^[^/:]*://",""}},
1570 { .name = "HTTP_URL_SLASH", .expr = {"url_beg","/",""}},
1571 { .name = "HTTP_URL_STAR", .expr = {"url","*",""}},
1572 { .name = "HTTP_CONTENT", .expr = {"hdr_val(content-length)","gt","0",""}},
Emeric Brunbede3d02009-06-30 17:54:00 +02001573 { .name = "RDP_COOKIE", .expr = {"req_rdp_cookie_cnt","gt","0",""}},
Willy Tarreauc6317702008-07-20 09:29:50 +02001574 { .name = "REQ_CONTENT", .expr = {"req_len","gt","0",""}},
Willy Tarreaub6fb4202008-07-20 11:18:28 +02001575 { .name = "WAIT_END", .expr = {"wait_end",""}},
Willy Tarreau16fbe822007-06-17 11:54:31 +02001576 { .name = NULL, .expr = {""}}
1577};
1578
1579/* Find a default ACL from the default_acl list, compile it and return it.
1580 * If the ACL is not found, NULL is returned. In theory, it cannot fail,
1581 * except when default ACLs are broken, in which case it will return NULL.
1582 * If <known_acl> is not NULL, the ACL will be queued at its tail.
1583 */
1584struct acl *find_acl_default(const char *acl_name, struct list *known_acl)
1585{
1586 __label__ out_return, out_free_acl_expr, out_free_name;
1587 struct acl *cur_acl;
1588 struct acl_expr *acl_expr;
1589 char *name;
1590 int index;
1591
1592 for (index = 0; default_acl_list[index].name != NULL; index++) {
1593 if (strcmp(acl_name, default_acl_list[index].name) == 0)
1594 break;
1595 }
1596
1597 if (default_acl_list[index].name == NULL)
1598 return NULL;
1599
1600 acl_expr = parse_acl_expr((const char **)default_acl_list[index].expr);
1601 if (!acl_expr)
1602 goto out_return;
1603
1604 name = strdup(acl_name);
1605 if (!name)
1606 goto out_free_acl_expr;
1607 cur_acl = (struct acl *)calloc(1, sizeof(*cur_acl));
1608 if (cur_acl == NULL)
1609 goto out_free_name;
1610
1611 cur_acl->name = name;
Willy Tarreaua55b7dc2009-07-12 09:21:30 +02001612 cur_acl->requires |= acl_expr->kw->requires;
Willy Tarreau16fbe822007-06-17 11:54:31 +02001613 LIST_INIT(&cur_acl->expr);
1614 LIST_ADDQ(&cur_acl->expr, &acl_expr->list);
1615 if (known_acl)
1616 LIST_ADDQ(known_acl, &cur_acl->list);
1617
1618 return cur_acl;
1619
1620 out_free_name:
1621 free(name);
1622 out_free_acl_expr:
1623 prune_acl_expr(acl_expr);
1624 free(acl_expr);
1625 out_return:
1626 return NULL;
1627}
Willy Tarreaua84d3742007-05-07 00:36:48 +02001628
1629/* Purge everything in the acl_cond <cond>, then return <cond>. */
1630struct acl_cond *prune_acl_cond(struct acl_cond *cond)
1631{
1632 struct acl_term_suite *suite, *tmp_suite;
1633 struct acl_term *term, *tmp_term;
1634
1635 /* iterate through all term suites and free all terms and all suites */
1636 list_for_each_entry_safe(suite, tmp_suite, &cond->suites, list) {
1637 list_for_each_entry_safe(term, tmp_term, &suite->terms, list)
1638 free(term);
1639 free(suite);
1640 }
1641 return cond;
1642}
1643
1644/* Parse an ACL condition starting at <args>[0], relying on a list of already
1645 * known ACLs passed in <known_acl>. The new condition is returned (or NULL in
1646 * case of low memory). Supports multiple conditions separated by "or".
1647 */
1648struct acl_cond *parse_acl_cond(const char **args, struct list *known_acl, int pol)
1649{
1650 __label__ out_return, out_free_suite, out_free_term;
Willy Tarreau74b98a82007-06-16 19:35:18 +02001651 int arg, neg;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001652 const char *word;
1653 struct acl *cur_acl;
1654 struct acl_term *cur_term;
1655 struct acl_term_suite *cur_suite;
1656 struct acl_cond *cond;
1657
1658 cond = (struct acl_cond *)calloc(1, sizeof(*cond));
1659 if (cond == NULL)
1660 goto out_return;
1661
1662 LIST_INIT(&cond->list);
1663 LIST_INIT(&cond->suites);
1664 cond->pol = pol;
1665
1666 cur_suite = NULL;
Willy Tarreau74b98a82007-06-16 19:35:18 +02001667 neg = 0;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001668 for (arg = 0; *args[arg]; arg++) {
1669 word = args[arg];
1670
1671 /* remove as many exclamation marks as we can */
1672 while (*word == '!') {
1673 neg = !neg;
1674 word++;
1675 }
1676
1677 /* an empty word is allowed because we cannot force the user to
1678 * always think about not leaving exclamation marks alone.
1679 */
1680 if (!*word)
1681 continue;
1682
Willy Tarreau16fbe822007-06-17 11:54:31 +02001683 if (strcasecmp(word, "or") == 0 || strcmp(word, "||") == 0) {
Willy Tarreaua84d3742007-05-07 00:36:48 +02001684 /* new term suite */
1685 cur_suite = NULL;
1686 neg = 0;
1687 continue;
1688 }
1689
Willy Tarreau95fa4692010-02-01 13:05:50 +01001690 if (strcmp(word, "{") == 0) {
1691 /* we may have a complete ACL expression between two braces,
1692 * find the last one.
1693 */
1694 int arg_end = arg + 1;
1695 const char **args_new;
1696
1697 while (*args[arg_end] && strcmp(args[arg_end], "}") != 0)
1698 arg_end++;
1699
1700 if (!*args[arg_end])
1701 goto out_free_suite;
1702
1703 args_new = calloc(1, (arg_end - arg + 1) * sizeof(*args_new));
1704 if (!args_new)
1705 goto out_free_suite;
1706
Willy Tarreau2a56c5e2010-03-15 16:13:29 +01001707 args_new[0] = "";
Willy Tarreau95fa4692010-02-01 13:05:50 +01001708 memcpy(args_new + 1, args + arg + 1, (arg_end - arg) * sizeof(*args_new));
1709 args_new[arg_end - arg] = "";
1710 cur_acl = parse_acl(args_new, known_acl);
1711 free(args_new);
1712
1713 if (!cur_acl)
Willy Tarreau16fbe822007-06-17 11:54:31 +02001714 goto out_free_suite;
Willy Tarreau95fa4692010-02-01 13:05:50 +01001715 arg = arg_end;
1716 }
1717 else {
1718 /* search for <word> in the known ACL names. If we do not find
1719 * it, let's look for it in the default ACLs, and if found, add
1720 * it to the list of ACLs of this proxy. This makes it possible
1721 * to override them.
1722 */
1723 cur_acl = find_acl_by_name(word, known_acl);
1724 if (cur_acl == NULL) {
1725 cur_acl = find_acl_default(word, known_acl);
1726 if (cur_acl == NULL)
1727 goto out_free_suite;
1728 }
Willy Tarreau16fbe822007-06-17 11:54:31 +02001729 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001730
1731 cur_term = (struct acl_term *)calloc(1, sizeof(*cur_term));
1732 if (cur_term == NULL)
1733 goto out_free_suite;
1734
1735 cur_term->acl = cur_acl;
1736 cur_term->neg = neg;
Willy Tarreaua9802632008-07-25 19:13:19 +02001737 cond->requires |= cur_acl->requires;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001738
1739 if (!cur_suite) {
1740 cur_suite = (struct acl_term_suite *)calloc(1, sizeof(*cur_suite));
1741 if (cur_term == NULL)
1742 goto out_free_term;
1743 LIST_INIT(&cur_suite->terms);
1744 LIST_ADDQ(&cond->suites, &cur_suite->list);
1745 }
1746 LIST_ADDQ(&cur_suite->terms, &cur_term->list);
Willy Tarreau74b98a82007-06-16 19:35:18 +02001747 neg = 0;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001748 }
1749
1750 return cond;
1751
1752 out_free_term:
1753 free(cur_term);
1754 out_free_suite:
1755 prune_acl_cond(cond);
1756 free(cond);
1757 out_return:
1758 return NULL;
1759}
1760
Willy Tarreau2bbba412010-01-28 16:48:33 +01001761/* Builds an ACL condition starting at the if/unless keyword. The complete
1762 * condition is returned. NULL is returned in case of error or if the first
1763 * word is neither "if" nor "unless". It automatically sets the file name and
1764 * the line number in the condition for better error reporting, and adds the
1765 * ACL requirements to the proxy's acl_requires.
1766 */
1767struct acl_cond *build_acl_cond(const char *file, int line, struct proxy *px, const char **args)
1768{
1769 int pol = ACL_COND_NONE;
1770 struct acl_cond *cond = NULL;
1771
1772 if (!strcmp(*args, "if")) {
1773 pol = ACL_COND_IF;
1774 args++;
1775 }
1776 else if (!strcmp(*args, "unless")) {
1777 pol = ACL_COND_UNLESS;
1778 args++;
1779 }
1780 else
1781 return NULL;
1782
1783 cond = parse_acl_cond(args, &px->acl, pol);
1784 if (!cond)
1785 return NULL;
1786
1787 cond->file = file;
1788 cond->line = line;
1789 px->acl_requires |= cond->requires;
1790
1791 return cond;
1792}
1793
Willy Tarreau11382812008-07-09 16:18:21 +02001794/* Execute condition <cond> and return either ACL_PAT_FAIL, ACL_PAT_MISS or
Willy Tarreaub6866442008-07-14 23:54:42 +02001795 * ACL_PAT_PASS depending on the test results. ACL_PAT_MISS may only be
1796 * returned if <dir> contains ACL_PARTIAL, indicating that incomplete data
1797 * is being examined.
1798 * This function only computes the condition, it does not apply the polarity
1799 * required by IF/UNLESS, it's up to the caller to do this using something like
1800 * this :
Willy Tarreau11382812008-07-09 16:18:21 +02001801 *
1802 * res = acl_pass(res);
Willy Tarreaub6866442008-07-14 23:54:42 +02001803 * if (res == ACL_PAT_MISS)
1804 * return 0;
Willy Tarreau11382812008-07-09 16:18:21 +02001805 * if (cond->pol == ACL_COND_UNLESS)
1806 * res = !res;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001807 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02001808int acl_exec_cond(struct acl_cond *cond, struct proxy *px, struct session *l4, void *l7, int dir)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001809{
1810 __label__ fetch_next;
1811 struct acl_term_suite *suite;
1812 struct acl_term *term;
1813 struct acl_expr *expr;
1814 struct acl *acl;
1815 struct acl_pattern *pattern;
Willy Tarreau37406352012-04-23 16:16:37 +02001816 struct sample smp;
Willy Tarreau11382812008-07-09 16:18:21 +02001817 int acl_res, suite_res, cond_res;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001818
Willy Tarreau11382812008-07-09 16:18:21 +02001819 /* We're doing a logical OR between conditions so we initialize to FAIL.
1820 * The MISS status is propagated down from the suites.
1821 */
Willy Tarreaua84d3742007-05-07 00:36:48 +02001822 cond_res = ACL_PAT_FAIL;
1823 list_for_each_entry(suite, &cond->suites, list) {
Willy Tarreau11382812008-07-09 16:18:21 +02001824 /* Evaluate condition suite <suite>. We stop at the first term
1825 * which returns ACL_PAT_FAIL. The MISS status is still propagated
1826 * in case of uncertainty in the result.
Willy Tarreaua84d3742007-05-07 00:36:48 +02001827 */
1828
1829 /* we're doing a logical AND between terms, so we must set the
1830 * initial value to PASS.
1831 */
1832 suite_res = ACL_PAT_PASS;
1833 list_for_each_entry(term, &suite->terms, list) {
1834 acl = term->acl;
1835
1836 /* FIXME: use cache !
1837 * check acl->cache_idx for this.
1838 */
1839
1840 /* ACL result not cached. Let's scan all the expressions
1841 * and use the first one to match.
1842 */
1843 acl_res = ACL_PAT_FAIL;
1844 list_for_each_entry(expr, &acl->expr, list) {
Willy Tarreaud41f8d82007-06-10 10:06:18 +02001845 /* we need to reset context and flags */
Willy Tarreau37406352012-04-23 16:16:37 +02001846 memset(&smp, 0, sizeof(smp));
Willy Tarreaua84d3742007-05-07 00:36:48 +02001847 fetch_next:
Willy Tarreau37406352012-04-23 16:16:37 +02001848 if (!expr->kw->fetch(px, l4, l7, dir, expr, &smp)) {
Willy Tarreaub6866442008-07-14 23:54:42 +02001849 /* maybe we could not fetch because of missing data */
Willy Tarreau37406352012-04-23 16:16:37 +02001850 if (smp.flags & SMP_F_MAY_CHANGE && dir & ACL_PARTIAL)
Willy Tarreaub6866442008-07-14 23:54:42 +02001851 acl_res |= ACL_PAT_MISS;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001852 continue;
Willy Tarreaub6866442008-07-14 23:54:42 +02001853 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001854
Willy Tarreau197e10a2012-04-23 19:18:42 +02001855 if (smp.type == SMP_T_BOOL) {
1856 if (smp.data.uint)
Willy Tarreaua79534f2008-07-20 10:13:37 +02001857 acl_res |= ACL_PAT_PASS;
1858 else
1859 acl_res |= ACL_PAT_FAIL;
1860 }
1861 else {
Willy Tarreau020534d2010-05-16 21:45:45 +02001862 if (!eb_is_empty(&expr->pattern_tree)) {
Willy Tarreauc4262962010-05-10 23:42:40 +02001863 /* a tree is present, let's check what type it is */
1864 if (expr->kw->match == acl_match_str)
Willy Tarreau37406352012-04-23 16:16:37 +02001865 acl_res |= acl_lookup_str(&smp, expr) ? ACL_PAT_PASS : ACL_PAT_FAIL;
Willy Tarreaub337b532010-05-13 20:03:41 +02001866 else if (expr->kw->match == acl_match_ip)
Willy Tarreau37406352012-04-23 16:16:37 +02001867 acl_res |= acl_lookup_ip(&smp, expr) ? ACL_PAT_PASS : ACL_PAT_FAIL;
Willy Tarreauc4262962010-05-10 23:42:40 +02001868 }
1869
Willy Tarreaua79534f2008-07-20 10:13:37 +02001870 /* call the match() function for all tests on this value */
1871 list_for_each_entry(pattern, &expr->patterns, list) {
Willy Tarreaua79534f2008-07-20 10:13:37 +02001872 if (acl_res == ACL_PAT_PASS)
1873 break;
Willy Tarreau37406352012-04-23 16:16:37 +02001874 acl_res |= expr->kw->match(&smp, pattern);
Willy Tarreaua79534f2008-07-20 10:13:37 +02001875 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001876 }
1877 /*
Willy Tarreau11382812008-07-09 16:18:21 +02001878 * OK now acl_res holds the result of this expression
1879 * as one of ACL_PAT_FAIL, ACL_PAT_MISS or ACL_PAT_PASS.
Willy Tarreaua84d3742007-05-07 00:36:48 +02001880 *
Willy Tarreau11382812008-07-09 16:18:21 +02001881 * Then if (!MISS) we can cache the result, and put
Willy Tarreau37406352012-04-23 16:16:37 +02001882 * (smp.flags & SMP_F_VOLATILE) in the cache flags.
Willy Tarreaua84d3742007-05-07 00:36:48 +02001883 *
1884 * FIXME: implement cache.
1885 *
1886 */
1887
Willy Tarreau11382812008-07-09 16:18:21 +02001888 /* we're ORing these terms, so a single PASS is enough */
1889 if (acl_res == ACL_PAT_PASS)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001890 break;
1891
Willy Tarreau37406352012-04-23 16:16:37 +02001892 if (smp.flags & SMP_F_NOT_LAST)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001893 goto fetch_next;
Willy Tarreaub6866442008-07-14 23:54:42 +02001894
1895 /* sometimes we know the fetched data is subject to change
1896 * later and give another chance for a new match (eg: request
1897 * size, time, ...)
1898 */
Willy Tarreau37406352012-04-23 16:16:37 +02001899 if (smp.flags & SMP_F_MAY_CHANGE && dir & ACL_PARTIAL)
Willy Tarreaub6866442008-07-14 23:54:42 +02001900 acl_res |= ACL_PAT_MISS;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001901 }
1902 /*
1903 * Here we have the result of an ACL (cached or not).
1904 * ACLs are combined, negated or not, to form conditions.
1905 */
1906
Willy Tarreaua84d3742007-05-07 00:36:48 +02001907 if (term->neg)
Willy Tarreau11382812008-07-09 16:18:21 +02001908 acl_res = acl_neg(acl_res);
Willy Tarreaua84d3742007-05-07 00:36:48 +02001909
1910 suite_res &= acl_res;
Willy Tarreau11382812008-07-09 16:18:21 +02001911
1912 /* we're ANDing these terms, so a single FAIL is enough */
1913 if (suite_res == ACL_PAT_FAIL)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001914 break;
1915 }
1916 cond_res |= suite_res;
Willy Tarreau11382812008-07-09 16:18:21 +02001917
1918 /* we're ORing these terms, so a single PASS is enough */
1919 if (cond_res == ACL_PAT_PASS)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001920 break;
1921 }
Willy Tarreau11382812008-07-09 16:18:21 +02001922 return cond_res;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001923}
1924
1925
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001926/* Reports a pointer to the first ACL used in condition <cond> which requires
1927 * at least one of the USE_FLAGS in <require>. Returns NULL if none matches.
1928 * The construct is almost the same as for acl_exec_cond() since we're walking
1929 * down the ACL tree as well. It is important that the tree is really walked
1930 * through and never cached, because that way, this function can be used as a
1931 * late check.
1932 */
Willy Tarreauf1e98b82010-01-28 17:59:39 +01001933struct acl *cond_find_require(const struct acl_cond *cond, unsigned int require)
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001934{
1935 struct acl_term_suite *suite;
1936 struct acl_term *term;
1937 struct acl *acl;
1938
1939 list_for_each_entry(suite, &cond->suites, list) {
1940 list_for_each_entry(term, &suite->terms, list) {
1941 acl = term->acl;
1942 if (acl->requires & require)
1943 return acl;
1944 }
1945 }
1946 return NULL;
1947}
1948
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001949/*
1950 * Find targets for userlist and groups in acl. Function returns the number
1951 * of errors or OK if everything is fine.
1952 */
1953int
1954acl_find_targets(struct proxy *p)
1955{
1956
1957 struct acl *acl;
1958 struct acl_expr *expr;
1959 struct acl_pattern *pattern;
1960 struct userlist *ul;
Willy Tarreau63364ee2012-04-19 19:11:13 +02001961 struct arg *arg;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001962 int cfgerr = 0;
1963
1964 list_for_each_entry(acl, &p->acl, list) {
1965 list_for_each_entry(expr, &acl->expr, list) {
Willy Tarreau63364ee2012-04-19 19:11:13 +02001966 for (arg = expr->args; arg; arg++) {
1967 if (arg->type == ARGT_STOP)
1968 break;
1969 else if (arg->type == ARGT_SRV) {
1970 struct proxy *px;
1971 struct server *srv;
1972 char *pname, *sname;
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001973
Willy Tarreau63364ee2012-04-19 19:11:13 +02001974 if (!expr->args->data.str.len) {
1975 Alert("proxy %s: acl '%s' %s(): missing server name.\n",
1976 p->id, acl->name, expr->kw->kw);
1977 cfgerr++;
1978 continue;
1979 }
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001980
Willy Tarreau63364ee2012-04-19 19:11:13 +02001981 pname = expr->args->data.str.str;
1982 sname = strrchr(pname, '/');
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001983
Willy Tarreau63364ee2012-04-19 19:11:13 +02001984 if (sname)
1985 *sname++ = '\0';
1986 else {
1987 sname = pname;
1988 pname = NULL;
1989 }
1990
1991 px = p;
1992 if (pname) {
1993 px = findproxy(pname, PR_CAP_BE);
1994 if (!px) {
1995 Alert("proxy %s: acl '%s' %s(): unable to find proxy '%s'.\n",
1996 p->id, acl->name, expr->kw->kw, pname);
1997 cfgerr++;
1998 continue;
1999 }
2000 }
Willy Tarreau0b1cd942010-05-16 22:18:27 +02002001
Willy Tarreau63364ee2012-04-19 19:11:13 +02002002 srv = findserver(px, sname);
2003 if (!srv) {
2004 Alert("proxy %s: acl '%s' %s(): unable to find server '%s'.\n",
2005 p->id, acl->name, expr->kw->kw, sname);
Willy Tarreau0b1cd942010-05-16 22:18:27 +02002006 cfgerr++;
2007 continue;
2008 }
Willy Tarreau0b1cd942010-05-16 22:18:27 +02002009
Willy Tarreau63364ee2012-04-19 19:11:13 +02002010 free(expr->args->data.str.str);
2011 expr->args->data.srv = srv;
Willy Tarreaud28c3532012-04-19 19:28:33 +02002012 }
2013 else if (arg->type == ARGT_FE) {
Willy Tarreaufc2c1fd2012-04-19 23:35:54 +02002014 struct proxy *prx = p;
2015 char *pname = p->id;
Willy Tarreaud28c3532012-04-19 19:28:33 +02002016
Willy Tarreaufc2c1fd2012-04-19 23:35:54 +02002017 if (expr->args->data.str.len) {
2018 pname = expr->args->data.str.str;
2019 prx = findproxy(pname, PR_CAP_FE);
Willy Tarreaud28c3532012-04-19 19:28:33 +02002020 }
2021
Willy Tarreaud28c3532012-04-19 19:28:33 +02002022 if (!prx) {
2023 Alert("proxy %s: acl '%s' %s(): unable to find frontend '%s'.\n",
2024 p->id, acl->name, expr->kw->kw, pname);
2025 cfgerr++;
2026 continue;
2027 }
2028
Willy Tarreaufc2c1fd2012-04-19 23:35:54 +02002029 if (!(prx->cap & PR_CAP_FE)) {
2030 Alert("proxy %s: acl '%s' %s(): proxy '%s' has no frontend capability.\n",
2031 p->id, acl->name, expr->kw->kw, pname);
2032 cfgerr++;
2033 continue;
2034 }
2035
Willy Tarreaud28c3532012-04-19 19:28:33 +02002036 free(expr->args->data.str.str);
2037 expr->args->data.prx = prx;
2038 }
2039 else if (arg->type == ARGT_BE) {
Willy Tarreaufc2c1fd2012-04-19 23:35:54 +02002040 struct proxy *prx = p;
2041 char *pname = p->id;
Willy Tarreaud28c3532012-04-19 19:28:33 +02002042
Willy Tarreaufc2c1fd2012-04-19 23:35:54 +02002043 if (expr->args->data.str.len) {
2044 pname = expr->args->data.str.str;
2045 prx = findproxy(pname, PR_CAP_BE);
Willy Tarreaud28c3532012-04-19 19:28:33 +02002046 }
2047
Willy Tarreaud28c3532012-04-19 19:28:33 +02002048 if (!prx) {
2049 Alert("proxy %s: acl '%s' %s(): unable to find backend '%s'.\n",
2050 p->id, acl->name, expr->kw->kw, pname);
2051 cfgerr++;
2052 continue;
2053 }
2054
Willy Tarreaufc2c1fd2012-04-19 23:35:54 +02002055 if (!(prx->cap & PR_CAP_BE)) {
2056 Alert("proxy %s: acl '%s' %s(): proxy '%s' has no backend capability.\n",
2057 p->id, acl->name, expr->kw->kw, pname);
2058 cfgerr++;
2059 continue;
2060 }
2061
Willy Tarreaud28c3532012-04-19 19:28:33 +02002062 free(expr->args->data.str.str);
2063 expr->args->data.prx = prx;
2064 }
2065 else if (arg->type == ARGT_TAB) {
Willy Tarreaufc2c1fd2012-04-19 23:35:54 +02002066 struct proxy *prx = p;
2067 char *pname = p->id;
Willy Tarreaud28c3532012-04-19 19:28:33 +02002068
Willy Tarreaufc2c1fd2012-04-19 23:35:54 +02002069 if (expr->args->data.str.len) {
2070 pname = expr->args->data.str.str;
2071 prx = find_stktable(pname);
Willy Tarreaud28c3532012-04-19 19:28:33 +02002072 }
2073
Willy Tarreaud28c3532012-04-19 19:28:33 +02002074 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
Willy Tarreaufc2c1fd2012-04-19 23:35:54 +02002081
2082 if (!prx->table.size) {
2083 Alert("proxy %s: acl '%s' %s(): no table in proxy '%s'.\n",
2084 p->id, acl->name, expr->kw->kw, pname);
2085 cfgerr++;
2086 continue;
2087 }
2088
Willy Tarreaud28c3532012-04-19 19:28:33 +02002089 free(expr->args->data.str.str);
2090 expr->args->data.prx = prx;
Willy Tarreau0b1cd942010-05-16 22:18:27 +02002091 }
Willy Tarreau63364ee2012-04-19 19:11:13 +02002092 else if (arg->type == ARGT_USR) {
2093 if (!expr->args->data.str.len) {
2094 Alert("proxy %s: acl '%s' %s(): missing userlist name.\n",
2095 p->id, acl->name, expr->kw->kw);
2096 cfgerr++;
2097 continue;
2098 }
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01002099
Willy Tarreau63364ee2012-04-19 19:11:13 +02002100 if (p->uri_auth && p->uri_auth->userlist &&
2101 !strcmp(p->uri_auth->userlist->name, expr->args->data.str.str))
2102 ul = p->uri_auth->userlist;
2103 else
2104 ul = auth_find_userlist(expr->args->data.str.str);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01002105
Willy Tarreau63364ee2012-04-19 19:11:13 +02002106 if (!ul) {
2107 Alert("proxy %s: acl '%s' %s(%s): unable to find userlist.\n",
2108 p->id, acl->name, expr->kw->kw, expr->args->data.str.str);
2109 cfgerr++;
2110 continue;
2111 }
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01002112
Willy Tarreau63364ee2012-04-19 19:11:13 +02002113 free(expr->args->data.str.str);
2114 expr->args->data.usr = ul;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01002115 }
Willy Tarreau63364ee2012-04-19 19:11:13 +02002116 } /* end of args processing */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01002117
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01002118
2119 if (!strcmp(expr->kw->kw, "http_auth_group")) {
Willy Tarreau63364ee2012-04-19 19:11:13 +02002120 /* note: argument resolved above thanks to ARGT_USR */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01002121
2122 if (LIST_ISEMPTY(&expr->patterns)) {
2123 Alert("proxy %s: acl %s %s(): no groups specified.\n",
2124 p->id, acl->name, expr->kw->kw);
2125 cfgerr++;
2126 continue;
2127 }
2128
2129 list_for_each_entry(pattern, &expr->patterns, list) {
Willy Tarreau34db1082012-04-19 17:16:54 +02002130 pattern->val.group_mask = auth_resolve_groups(expr->args->data.usr, pattern->ptr.str);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01002131
2132 free(pattern->ptr.str);
2133 pattern->ptr.str = NULL;
2134 pattern->len = 0;
2135
2136 if (!pattern->val.group_mask) {
2137 Alert("proxy %s: acl %s %s(): invalid group(s).\n",
2138 p->id, acl->name, expr->kw->kw);
2139 cfgerr++;
2140 continue;
2141 }
2142 }
2143 }
2144 }
2145 }
2146
2147 return cfgerr;
2148}
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02002149
Willy Tarreaua84d3742007-05-07 00:36:48 +02002150/************************************************************************/
2151/* All supported keywords must be declared here. */
2152/************************************************************************/
2153
Willy Tarreau61612d42012-04-19 18:42:05 +02002154/* Note: must not be declared <const> as its list will be overwritten.
2155 * Please take care of keeping this list alphabetically sorted.
2156 */
Willy Tarreaua84d3742007-05-07 00:36:48 +02002157static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau61612d42012-04-19 18:42:05 +02002158 { "always_false", acl_parse_nothing, acl_fetch_false, acl_match_nothing, ACL_USE_NOTHING, 0 },
2159 { "always_true", acl_parse_nothing, acl_fetch_true, acl_match_nothing, ACL_USE_NOTHING, 0 },
2160 { "rep_ssl_hello_type", acl_parse_int, acl_fetch_ssl_hello_type, acl_match_int, ACL_USE_L6RTR_VOLATILE, 0 },
2161 { "req_len", acl_parse_int, acl_fetch_req_len, acl_match_int, ACL_USE_L6REQ_VOLATILE, 0 },
2162 { "req_rdp_cookie", acl_parse_str, acl_fetch_rdp_cookie, acl_match_str, ACL_USE_L6REQ_VOLATILE|ACL_MAY_LOOKUP, ARG1(0,STR) },
2163 { "req_rdp_cookie_cnt", acl_parse_int, acl_fetch_rdp_cookie_cnt, acl_match_int, ACL_USE_L6REQ_VOLATILE, ARG1(0,STR) },
2164 { "req_ssl_hello_type", acl_parse_int, acl_fetch_ssl_hello_type, acl_match_int, ACL_USE_L6REQ_VOLATILE, 0 },
2165 { "req_ssl_sni", acl_parse_str, acl_fetch_ssl_hello_sni, acl_match_str, ACL_USE_L6REQ_VOLATILE|ACL_MAY_LOOKUP, 0 },
2166 { "req_ssl_ver", acl_parse_dotted_ver, acl_fetch_req_ssl_ver, acl_match_int, ACL_USE_L6REQ_VOLATILE, 0 },
2167 { "wait_end", acl_parse_nothing, acl_fetch_wait_end, acl_match_nothing, ACL_USE_NOTHING, 0 },
Willy Tarreaua84d3742007-05-07 00:36:48 +02002168 { NULL, NULL, NULL, NULL }
2169}};
2170
2171
2172__attribute__((constructor))
2173static void __acl_init(void)
2174{
2175 acl_register_keywords(&acl_kws);
2176}
2177
2178
2179/*
2180 * Local variables:
2181 * c-indent-level: 8
2182 * c-basic-offset: 8
2183 * End:
2184 */