blob: cba89ab0ef9e698999414c52df1c8fa1f354214c [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 Tarreauc7e42382012-08-24 19:22:53 +020027#include <proto/channel.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 Tarreau32a6f2e2012-04-25 10:13:36 +020068acl_fetch_true(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +020069 const struct arg *args, 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
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020080acl_fetch_wait_end(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +020081 const struct arg *args, struct sample *smp)
Willy Tarreaub6fb4202008-07-20 11:18:28 +020082{
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020083 if (!(opt & SMP_OPT_FINAL)) {
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 Tarreau32a6f2e2012-04-25 10:13:36 +020094acl_fetch_false(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +020095 const struct arg *args, 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
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200104acl_fetch_req_len(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +0200105 const struct arg *args, 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;
Willy Tarreau9b28e032012-10-12 23:49:43 +0200111 smp->data.uint = l4->req->buf->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
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200118acl_fetch_ssl_hello_type(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +0200119 const struct arg *args, struct sample *smp)
Emeric Brun38e71762010-09-23 17:59:18 +0200120{
121 int hs_len;
122 int hs_type, bleft;
Willy Tarreauf332af72012-10-12 23:58:13 +0200123 struct channel *chn;
Emeric Brun38e71762010-09-23 17:59:18 +0200124 const unsigned char *data;
125
126 if (!l4)
127 goto not_ssl_hello;
128
Willy Tarreauf332af72012-10-12 23:58:13 +0200129 chn = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? l4->rep : l4->req;
Emeric Brun38e71762010-09-23 17:59:18 +0200130
Willy Tarreau9b28e032012-10-12 23:49:43 +0200131 bleft = chn->buf->i;
132 data = (const unsigned char *)chn->buf->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
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200187acl_fetch_req_ssl_ver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +0200188 const struct arg *args, 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 Tarreau9b28e032012-10-12 23:49:43 +0200197 bleft = l4->req->buf->i;
Willy Tarreau44b90cc2010-05-24 20:27:29 +0200198 if (!bleft)
199 goto too_short;
200
Willy Tarreau9b28e032012-10-12 23:49:43 +0200201 data = (const unsigned char *)l4->req->buf->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 Tarreau9b28e032012-10-12 23:49:43 +0200269 if (msg_len > buffer_max_len(l4->req) + l4->req->buf->data - l4->req->buf->p)
270 msg_len = buffer_max_len(l4->req) + l4->req->buf->data - l4->req->buf->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
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200323acl_fetch_ssl_hello_sni(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +0200324 const struct arg *args, struct sample *smp)
Willy Tarreaub6672b52011-12-12 17:23:41 +0100325{
326 int hs_len, ext_len, bleft;
Willy Tarreauf332af72012-10-12 23:58:13 +0200327 struct channel *chn;
Willy Tarreaub6672b52011-12-12 17:23:41 +0100328 unsigned char *data;
329
330 if (!l4)
331 goto not_ssl_hello;
332
Willy Tarreauf332af72012-10-12 23:58:13 +0200333 chn = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? l4->rep : l4->req;
Willy Tarreaub6672b52011-12-12 17:23:41 +0100334
Willy Tarreau9b28e032012-10-12 23:49:43 +0200335 bleft = chn->buf->i;
336 data = (unsigned char *)chn->buf->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 Tarreau58393e12008-07-20 10:39:22 +0200456/*
457 * These functions are exported and may be used by any other component.
458 */
459
460/* ignore the current line */
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200461int acl_parse_nothing(const char **text, struct acl_pattern *pattern, int *opaque, char **err)
Willy Tarreaua5909832007-06-17 20:40:25 +0200462{
Willy Tarreau58393e12008-07-20 10:39:22 +0200463 return 1;
464}
465
466/* always fake a data retrieval */
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200467int acl_fetch_nothing(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +0200468 const struct arg *args, struct sample *smp)
Willy Tarreau58393e12008-07-20 10:39:22 +0200469{
470 return 1;
Willy Tarreaua5909832007-06-17 20:40:25 +0200471}
472
473/* always return false */
Willy Tarreau37406352012-04-23 16:16:37 +0200474int acl_match_nothing(struct sample *smp, struct acl_pattern *pattern)
Willy Tarreaua5909832007-06-17 20:40:25 +0200475{
Willy Tarreau11382812008-07-09 16:18:21 +0200476 return ACL_PAT_FAIL;
Willy Tarreaua5909832007-06-17 20:40:25 +0200477}
478
479
Willy Tarreaua84d3742007-05-07 00:36:48 +0200480/* NB: For two strings to be identical, it is required that their lengths match */
Willy Tarreau37406352012-04-23 16:16:37 +0200481int acl_match_str(struct sample *smp, struct acl_pattern *pattern)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200482{
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200483 int icase;
484
Willy Tarreauf853c462012-04-23 18:53:56 +0200485 if (pattern->len != smp->data.str.len)
Willy Tarreau11382812008-07-09 16:18:21 +0200486 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200487
488 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
Willy Tarreauf853c462012-04-23 18:53:56 +0200489 if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) == 0) ||
490 (!icase && strncmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) == 0))
Willy Tarreau11382812008-07-09 16:18:21 +0200491 return ACL_PAT_PASS;
492 return ACL_PAT_FAIL;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200493}
494
Emeric Brun07ca4962012-10-17 13:38:19 +0200495/* NB: For two binaries buf to be identical, it is required that their lengths match */
496int acl_match_bin(struct sample *smp, struct acl_pattern *pattern)
497{
498 if (pattern->len != smp->data.str.len)
499 return ACL_PAT_FAIL;
500
501 if (memcmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) == 0)
502 return ACL_PAT_PASS;
503 return ACL_PAT_FAIL;
504}
505
Willy Tarreauc4262962010-05-10 23:42:40 +0200506/* Lookup a string in the expression's pattern tree. The node is returned if it
507 * exists, otherwise NULL.
508 */
Willy Tarreau37406352012-04-23 16:16:37 +0200509static void *acl_lookup_str(struct sample *smp, struct acl_expr *expr)
Willy Tarreauc4262962010-05-10 23:42:40 +0200510{
511 /* data are stored in a tree */
512 struct ebmb_node *node;
513 char prev;
514
515 /* we may have to force a trailing zero on the test pattern */
Willy Tarreauf853c462012-04-23 18:53:56 +0200516 prev = smp->data.str.str[smp->data.str.len];
Willy Tarreauc4262962010-05-10 23:42:40 +0200517 if (prev)
Willy Tarreauf853c462012-04-23 18:53:56 +0200518 smp->data.str.str[smp->data.str.len] = '\0';
519 node = ebst_lookup(&expr->pattern_tree, smp->data.str.str);
Willy Tarreauc4262962010-05-10 23:42:40 +0200520 if (prev)
Willy Tarreauf853c462012-04-23 18:53:56 +0200521 smp->data.str.str[smp->data.str.len] = prev;
Willy Tarreauc4262962010-05-10 23:42:40 +0200522 return node;
523}
524
Willy Tarreau21e5b0e2012-04-23 19:25:44 +0200525/* Executes a regex. It temporarily changes the data to add a trailing zero,
526 * and restores the previous character when leaving.
Willy Tarreauf3d25982007-05-08 22:45:09 +0200527 */
Willy Tarreau37406352012-04-23 16:16:37 +0200528int acl_match_reg(struct sample *smp, struct acl_pattern *pattern)
Willy Tarreauf3d25982007-05-08 22:45:09 +0200529{
530 char old_char;
531 int ret;
532
Willy Tarreauf853c462012-04-23 18:53:56 +0200533 old_char = smp->data.str.str[smp->data.str.len];
534 smp->data.str.str[smp->data.str.len] = 0;
Willy Tarreauf3d25982007-05-08 22:45:09 +0200535
Willy Tarreauf853c462012-04-23 18:53:56 +0200536 if (regexec(pattern->ptr.reg, smp->data.str.str, 0, NULL, 0) == 0)
Willy Tarreau11382812008-07-09 16:18:21 +0200537 ret = ACL_PAT_PASS;
Willy Tarreauf3d25982007-05-08 22:45:09 +0200538 else
Willy Tarreau11382812008-07-09 16:18:21 +0200539 ret = ACL_PAT_FAIL;
Willy Tarreauf3d25982007-05-08 22:45:09 +0200540
Willy Tarreauf853c462012-04-23 18:53:56 +0200541 smp->data.str.str[smp->data.str.len] = old_char;
Willy Tarreauf3d25982007-05-08 22:45:09 +0200542 return ret;
543}
544
Willy Tarreaua84d3742007-05-07 00:36:48 +0200545/* Checks that the pattern matches the beginning of the tested string. */
Willy Tarreau37406352012-04-23 16:16:37 +0200546int acl_match_beg(struct sample *smp, struct acl_pattern *pattern)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200547{
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200548 int icase;
549
Willy Tarreauf853c462012-04-23 18:53:56 +0200550 if (pattern->len > smp->data.str.len)
Willy Tarreau11382812008-07-09 16:18:21 +0200551 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200552
553 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
Willy Tarreauf853c462012-04-23 18:53:56 +0200554 if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str, pattern->len) != 0) ||
555 (!icase && strncmp(pattern->ptr.str, smp->data.str.str, pattern->len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +0200556 return ACL_PAT_FAIL;
557 return ACL_PAT_PASS;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200558}
559
560/* Checks that the pattern matches the end of the tested string. */
Willy Tarreau37406352012-04-23 16:16:37 +0200561int acl_match_end(struct sample *smp, struct acl_pattern *pattern)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200562{
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200563 int icase;
564
Willy Tarreauf853c462012-04-23 18:53:56 +0200565 if (pattern->len > smp->data.str.len)
Willy Tarreau11382812008-07-09 16:18:21 +0200566 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200567 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
Willy Tarreauf853c462012-04-23 18:53:56 +0200568 if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str + smp->data.str.len - pattern->len, pattern->len) != 0) ||
569 (!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 +0200570 return ACL_PAT_FAIL;
571 return ACL_PAT_PASS;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200572}
573
574/* Checks that the pattern is included inside the tested string.
575 * NB: Suboptimal, should be rewritten using a Boyer-Moore method.
576 */
Willy Tarreau37406352012-04-23 16:16:37 +0200577int acl_match_sub(struct sample *smp, struct acl_pattern *pattern)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200578{
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200579 int icase;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200580 char *end;
581 char *c;
582
Willy Tarreauf853c462012-04-23 18:53:56 +0200583 if (pattern->len > smp->data.str.len)
Willy Tarreau11382812008-07-09 16:18:21 +0200584 return ACL_PAT_FAIL;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200585
Willy Tarreauf853c462012-04-23 18:53:56 +0200586 end = smp->data.str.str + smp->data.str.len - pattern->len;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200587 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
588 if (icase) {
Willy Tarreauf853c462012-04-23 18:53:56 +0200589 for (c = smp->data.str.str; c <= end; c++) {
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200590 if (tolower(*c) != tolower(*pattern->ptr.str))
591 continue;
592 if (strncasecmp(pattern->ptr.str, c, pattern->len) == 0)
Willy Tarreau11382812008-07-09 16:18:21 +0200593 return ACL_PAT_PASS;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200594 }
595 } else {
Willy Tarreauf853c462012-04-23 18:53:56 +0200596 for (c = smp->data.str.str; c <= end; c++) {
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200597 if (*c != *pattern->ptr.str)
598 continue;
599 if (strncmp(pattern->ptr.str, c, pattern->len) == 0)
Willy Tarreau11382812008-07-09 16:18:21 +0200600 return ACL_PAT_PASS;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200601 }
Willy Tarreaua84d3742007-05-07 00:36:48 +0200602 }
Willy Tarreau11382812008-07-09 16:18:21 +0200603 return ACL_PAT_FAIL;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200604}
605
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200606/* Background: Fast way to find a zero byte in a word
607 * http://graphics.stanford.edu/~seander/bithacks.html#ZeroInWord
608 * hasZeroByte = (v - 0x01010101UL) & ~v & 0x80808080UL;
609 *
610 * To look for 4 different byte values, xor the word with those bytes and
611 * then check for zero bytes:
612 *
613 * v = (((unsigned char)c * 0x1010101U) ^ delimiter)
614 * where <delimiter> is the 4 byte values to look for (as an uint)
615 * and <c> is the character that is being tested
616 */
617static inline unsigned int is_delimiter(unsigned char c, unsigned int mask)
618{
619 mask ^= (c * 0x01010101); /* propagate the char to all 4 bytes */
620 return (mask - 0x01010101) & ~mask & 0x80808080U;
621}
622
623static inline unsigned int make_4delim(unsigned char d1, unsigned char d2, unsigned char d3, unsigned char d4)
624{
625 return d1 << 24 | d2 << 16 | d3 << 8 | d4;
626}
627
Willy Tarreaua84d3742007-05-07 00:36:48 +0200628/* This one is used by other real functions. It checks that the pattern is
629 * included inside the tested string, but enclosed between the specified
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200630 * delimiters or at the beginning or end of the string. The delimiters are
631 * provided as an unsigned int made by make_4delim() and match up to 4 different
632 * delimiters. Delimiters are stripped at the beginning and end of the pattern.
Willy Tarreaua84d3742007-05-07 00:36:48 +0200633 */
Willy Tarreau37406352012-04-23 16:16:37 +0200634static int match_word(struct sample *smp, struct acl_pattern *pattern, unsigned int delimiters)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200635{
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200636 int may_match, icase;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200637 char *c, *end;
638 char *ps;
639 int pl;
640
641 pl = pattern->len;
642 ps = pattern->ptr.str;
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200643
644 while (pl > 0 && is_delimiter(*ps, delimiters)) {
Willy Tarreaua84d3742007-05-07 00:36:48 +0200645 pl--;
646 ps++;
647 }
648
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200649 while (pl > 0 && is_delimiter(ps[pl - 1], delimiters))
Willy Tarreaua84d3742007-05-07 00:36:48 +0200650 pl--;
651
Willy Tarreauf853c462012-04-23 18:53:56 +0200652 if (pl > smp->data.str.len)
Willy Tarreau11382812008-07-09 16:18:21 +0200653 return ACL_PAT_FAIL;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200654
655 may_match = 1;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200656 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
Willy Tarreauf853c462012-04-23 18:53:56 +0200657 end = smp->data.str.str + smp->data.str.len - pl;
658 for (c = smp->data.str.str; c <= end; c++) {
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200659 if (is_delimiter(*c, delimiters)) {
Willy Tarreaua84d3742007-05-07 00:36:48 +0200660 may_match = 1;
661 continue;
662 }
Willy Tarreaua84d3742007-05-07 00:36:48 +0200663
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200664 if (!may_match)
665 continue;
666
667 if (icase) {
668 if ((tolower(*c) == tolower(*ps)) &&
669 (strncasecmp(ps, c, pl) == 0) &&
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200670 (c == end || is_delimiter(c[pl], delimiters)))
Willy Tarreau11382812008-07-09 16:18:21 +0200671 return ACL_PAT_PASS;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200672 } else {
673 if ((*c == *ps) &&
674 (strncmp(ps, c, pl) == 0) &&
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200675 (c == end || is_delimiter(c[pl], delimiters)))
Willy Tarreau11382812008-07-09 16:18:21 +0200676 return ACL_PAT_PASS;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200677 }
Willy Tarreaua84d3742007-05-07 00:36:48 +0200678 may_match = 0;
679 }
Willy Tarreau11382812008-07-09 16:18:21 +0200680 return ACL_PAT_FAIL;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200681}
682
683/* Checks that the pattern is included inside the tested string, but enclosed
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200684 * between the delimiters '?' or '/' or at the beginning or end of the string.
685 * Delimiters at the beginning or end of the pattern are ignored.
Willy Tarreaua84d3742007-05-07 00:36:48 +0200686 */
Willy Tarreau37406352012-04-23 16:16:37 +0200687int acl_match_dir(struct sample *smp, struct acl_pattern *pattern)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200688{
Willy Tarreau37406352012-04-23 16:16:37 +0200689 return match_word(smp, pattern, make_4delim('/', '?', '?', '?'));
Willy Tarreaua84d3742007-05-07 00:36:48 +0200690}
691
692/* Checks that the pattern is included inside the tested string, but enclosed
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200693 * between the delmiters '/', '?', '.' or ":" or at the beginning or end of
694 * the string. Delimiters at the beginning or end of the pattern are ignored.
Willy Tarreaua84d3742007-05-07 00:36:48 +0200695 */
Willy Tarreau37406352012-04-23 16:16:37 +0200696int acl_match_dom(struct sample *smp, struct acl_pattern *pattern)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200697{
Willy Tarreau37406352012-04-23 16:16:37 +0200698 return match_word(smp, pattern, make_4delim('/', '?', '.', ':'));
Willy Tarreaua84d3742007-05-07 00:36:48 +0200699}
700
701/* Checks that the integer in <test> is included between min and max */
Willy Tarreau37406352012-04-23 16:16:37 +0200702int acl_match_int(struct sample *smp, struct acl_pattern *pattern)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200703{
Willy Tarreauf853c462012-04-23 18:53:56 +0200704 if ((!pattern->val.range.min_set || pattern->val.range.min <= smp->data.uint) &&
705 (!pattern->val.range.max_set || smp->data.uint <= pattern->val.range.max))
Willy Tarreau11382812008-07-09 16:18:21 +0200706 return ACL_PAT_PASS;
707 return ACL_PAT_FAIL;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200708}
709
Willy Tarreau0e698542011-09-16 08:32:32 +0200710/* Checks that the length of the pattern in <test> is included between min and max */
Willy Tarreau37406352012-04-23 16:16:37 +0200711int acl_match_len(struct sample *smp, struct acl_pattern *pattern)
Willy Tarreau0e698542011-09-16 08:32:32 +0200712{
Willy Tarreauf853c462012-04-23 18:53:56 +0200713 if ((!pattern->val.range.min_set || pattern->val.range.min <= smp->data.str.len) &&
714 (!pattern->val.range.max_set || smp->data.str.len <= pattern->val.range.max))
Willy Tarreau0e698542011-09-16 08:32:32 +0200715 return ACL_PAT_PASS;
716 return ACL_PAT_FAIL;
717}
718
Willy Tarreau37406352012-04-23 16:16:37 +0200719int acl_match_ip(struct sample *smp, struct acl_pattern *pattern)
Willy Tarreaua67fad92007-05-08 19:50:09 +0200720{
Willy Tarreauceb4ac92012-04-28 00:41:46 +0200721 unsigned int v4; /* in network byte order */
722 struct in6_addr *v6;
723 int bits, pos;
724 struct in6_addr tmp6;
Willy Tarreaua67fad92007-05-08 19:50:09 +0200725
Willy Tarreauceb4ac92012-04-28 00:41:46 +0200726 if (pattern->type == SMP_T_IPV4) {
727 if (smp->type == SMP_T_IPV4) {
728 v4 = smp->data.ipv4.s_addr;
729 }
730 else if (smp->type == SMP_T_IPV6) {
731 /* v4 match on a V6 sample. We want to check at least for
732 * the following forms :
733 * - ::ffff:ip:v4 (ipv4 mapped)
734 * - ::0000:ip:v4 (old ipv4 mapped)
735 * - 2002:ip:v4:: (6to4)
736 */
737 if (*(uint32_t*)&smp->data.ipv6.s6_addr[0] == 0 &&
738 *(uint32_t*)&smp->data.ipv6.s6_addr[4] == 0 &&
739 (*(uint32_t*)&smp->data.ipv6.s6_addr[8] == 0 ||
740 *(uint32_t*)&smp->data.ipv6.s6_addr[8] == htonl(0xFFFF))) {
741 v4 = *(uint32_t*)&smp->data.ipv6.s6_addr[12];
742 }
743 else if (*(uint16_t*)&smp->data.ipv6.s6_addr[0] == htons(0x2002)) {
744 v4 = htonl((ntohs(*(uint16_t*)&smp->data.ipv6.s6_addr[2]) << 16) +
745 ntohs(*(uint16_t*)&smp->data.ipv6.s6_addr[4]));
746 }
747 else
748 return ACL_PAT_FAIL;
749 }
750 else
751 return ACL_PAT_FAIL;
Willy Tarreaua67fad92007-05-08 19:50:09 +0200752
Willy Tarreauceb4ac92012-04-28 00:41:46 +0200753 if (((v4 ^ pattern->val.ipv4.addr.s_addr) & pattern->val.ipv4.mask.s_addr) == 0)
754 return ACL_PAT_PASS;
755 else
756 return ACL_PAT_FAIL;
757 }
758 else if (pattern->type == SMP_T_IPV6) {
759 if (smp->type == SMP_T_IPV4) {
760 /* Convert the IPv4 sample address to IPv4 with the
761 * mapping method using the ::ffff: prefix.
762 */
763 memset(&tmp6, 0, 10);
764 *(uint16_t*)&tmp6.s6_addr[10] = htons(0xffff);
765 *(uint32_t*)&tmp6.s6_addr[12] = smp->data.ipv4.s_addr;
766 v6 = &tmp6;
767 }
768 else if (smp->type == SMP_T_IPV6) {
769 v6 = &smp->data.ipv6;
770 }
771 else {
772 return ACL_PAT_FAIL;
773 }
774
775 bits = pattern->val.ipv6.mask;
776 for (pos = 0; bits > 0; pos += 4, bits -= 32) {
777 v4 = *(uint32_t*)&v6->s6_addr[pos] ^ *(uint32_t*)&pattern->val.ipv6.addr.s6_addr[pos];
778 if (bits < 32)
Cyril Bonté4c01beb2012-10-23 21:28:31 +0200779 v4 &= htonl((~0U) << (32-bits));
Willy Tarreauceb4ac92012-04-28 00:41:46 +0200780 if (v4)
781 return ACL_PAT_FAIL;
782 }
Willy Tarreau11382812008-07-09 16:18:21 +0200783 return ACL_PAT_PASS;
Willy Tarreauceb4ac92012-04-28 00:41:46 +0200784 }
Willy Tarreau11382812008-07-09 16:18:21 +0200785 return ACL_PAT_FAIL;
Willy Tarreaua67fad92007-05-08 19:50:09 +0200786}
787
Willy Tarreaub337b532010-05-13 20:03:41 +0200788/* Lookup an IPv4 address in the expression's pattern tree using the longest
789 * match method. The node is returned if it exists, otherwise NULL.
790 */
Willy Tarreau37406352012-04-23 16:16:37 +0200791static void *acl_lookup_ip(struct sample *smp, struct acl_expr *expr)
Willy Tarreaub337b532010-05-13 20:03:41 +0200792{
793 struct in_addr *s;
794
Willy Tarreauf853c462012-04-23 18:53:56 +0200795 if (smp->type != SMP_T_IPV4)
Willy Tarreaub337b532010-05-13 20:03:41 +0200796 return ACL_PAT_FAIL;
797
Willy Tarreauf853c462012-04-23 18:53:56 +0200798 s = &smp->data.ipv4;
Willy Tarreaub337b532010-05-13 20:03:41 +0200799 return ebmb_lookup_longest(&expr->pattern_tree, &s->s_addr);
800}
801
Willy Tarreaua84d3742007-05-07 00:36:48 +0200802/* Parse a string. It is allocated and duplicated. */
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200803int acl_parse_str(const char **text, struct acl_pattern *pattern, int *opaque, char **err)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200804{
805 int len;
806
Willy Tarreauae8b7962007-06-09 23:10:04 +0200807 len = strlen(*text);
Willy Tarreauc92ddbc2012-04-27 22:10:57 +0200808 pattern->type = SMP_T_CSTR;
Willy Tarreauc4262962010-05-10 23:42:40 +0200809
810 if (pattern->flags & ACL_PAT_F_TREE_OK) {
811 /* we're allowed to put the data in a tree whose root is pointed
812 * to by val.tree.
813 */
814 struct ebmb_node *node;
815
816 node = calloc(1, sizeof(*node) + len + 1);
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200817 if (!node) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200818 memprintf(err, "out of memory while loading string pattern");
Willy Tarreauc4262962010-05-10 23:42:40 +0200819 return 0;
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200820 }
Willy Tarreauc4262962010-05-10 23:42:40 +0200821 memcpy(node->key, *text, len + 1);
822 if (ebst_insert(pattern->val.tree, node) != node)
823 free(node); /* was a duplicate */
824 pattern->flags |= ACL_PAT_F_TREE; /* this pattern now contains a tree */
825 return 1;
826 }
827
Willy Tarreauae8b7962007-06-09 23:10:04 +0200828 pattern->ptr.str = strdup(*text);
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200829 if (!pattern->ptr.str) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200830 memprintf(err, "out of memory while loading string pattern");
Willy Tarreaua84d3742007-05-07 00:36:48 +0200831 return 0;
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200832 }
Willy Tarreaua84d3742007-05-07 00:36:48 +0200833 pattern->len = len;
834 return 1;
835}
836
Emeric Brun07ca4962012-10-17 13:38:19 +0200837/* Parse a binary written in hexa. It is allocated. */
838int acl_parse_bin(const char **text, struct acl_pattern *pattern, int *opaque, char **err)
839{
840 int len;
841 const char *p = *text;
842 int i,j;
843
844 len = strlen(p);
845 if (len%2) {
846 memprintf(err, "an even number of hex digit is expected");
847 return 0;
848 }
849
850 pattern->type = SMP_T_CBIN;
851 pattern->len = len >> 1;
852 pattern->ptr.str = malloc(pattern->len);
853 if (!pattern->ptr.str) {
854 memprintf(err, "out of memory while loading string pattern");
855 return 0;
856 }
857
858 i = j = 0;
859 while (j < pattern->len) {
860 if (!ishex(p[i++]))
861 goto bad_input;
862 if (!ishex(p[i++]))
863 goto bad_input;
864 pattern->ptr.str[j++] = (hex2i(p[i-2]) << 4) + hex2i(p[i-1]);
865 }
866 return 1;
867
868bad_input:
869 memprintf(err, "an hex digit is expected (found '%c')", p[i-1]);
870 free(pattern->ptr.str);
871 return 0;
872}
873
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +0100874/* Parse and concatenate all further strings into one. */
875int
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200876acl_parse_strcat(const char **text, struct acl_pattern *pattern, int *opaque, char **err)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +0100877{
878
879 int len = 0, i;
880 char *s;
881
882 for (i = 0; *text[i]; i++)
883 len += strlen(text[i])+1;
884
Willy Tarreauc92ddbc2012-04-27 22:10:57 +0200885 pattern->type = SMP_T_CSTR;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +0100886 pattern->ptr.str = s = calloc(1, len);
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200887 if (!pattern->ptr.str) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200888 memprintf(err, "out of memory while loading pattern");
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +0100889 return 0;
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200890 }
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +0100891
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 Tarreau7dcb6482012-04-27 17:52:25 +0200907int acl_parse_reg(const char **text, struct acl_pattern *pattern, int *opaque, char **err)
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
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200914 if (!preg) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200915 memprintf(err, "out of memory while loading pattern");
Willy Tarreauf3d25982007-05-08 22:45:09 +0200916 return 0;
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200917 }
Willy Tarreauf3d25982007-05-08 22:45:09 +0200918
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200919 icase = (pattern->flags & ACL_PAT_F_IGNORE_CASE) ? REG_ICASE : 0;
920 if (regcomp(preg, *text, REG_EXTENDED | REG_NOSUB | icase) != 0) {
Willy Tarreauf3d25982007-05-08 22:45:09 +0200921 free(preg);
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200922 memprintf(err, "regex '%s' is invalid", *text);
Willy Tarreauf3d25982007-05-08 22:45:09 +0200923 return 0;
924 }
925
926 pattern->ptr.reg = preg;
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +0200927 pattern->freeptrbuf = &acl_free_reg;
Willy Tarreauf3d25982007-05-08 22:45:09 +0200928 return 1;
929}
930
Willy Tarreauae8b7962007-06-09 23:10:04 +0200931/* Parse a range of positive integers delimited by either ':' or '-'. If only
932 * one integer is read, it is set as both min and max. An operator may be
933 * specified as the prefix, among this list of 5 :
934 *
935 * 0:eq, 1:gt, 2:ge, 3:lt, 4:le
936 *
937 * The default operator is "eq". It supports range matching. Ranges are
938 * rejected for other operators. The operator may be changed at any time.
939 * The operator is stored in the 'opaque' argument.
940 *
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200941 * If err is non-NULL, an error message will be returned there on errors and
942 * the caller will have to free it.
943 *
Willy Tarreaua84d3742007-05-07 00:36:48 +0200944 */
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200945int acl_parse_int(const char **text, struct acl_pattern *pattern, int *opaque, char **err)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200946{
Willy Tarreauae8b7962007-06-09 23:10:04 +0200947 signed long long i;
948 unsigned int j, last, skip = 0;
949 const char *ptr = *text;
950
Willy Tarreauc92ddbc2012-04-27 22:10:57 +0200951 pattern->type = SMP_T_UINT;
Willy Tarreau8f8e6452007-06-17 21:51:38 +0200952 while (!isdigit((unsigned char)*ptr)) {
Willy Tarreau1c7cc5b2010-07-18 10:46:33 +0200953 switch (get_std_op(ptr)) {
954 case STD_OP_EQ: *opaque = 0; break;
955 case STD_OP_GT: *opaque = 1; break;
956 case STD_OP_GE: *opaque = 2; break;
957 case STD_OP_LT: *opaque = 3; break;
958 case STD_OP_LE: *opaque = 4; break;
959 default:
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200960 memprintf(err, "'%s' is neither a number nor a supported operator", ptr);
Willy Tarreauae8b7962007-06-09 23:10:04 +0200961 return 0;
Willy Tarreau1c7cc5b2010-07-18 10:46:33 +0200962 }
Willy Tarreauae8b7962007-06-09 23:10:04 +0200963
964 skip++;
965 ptr = text[skip];
966 }
Willy Tarreaua84d3742007-05-07 00:36:48 +0200967
968 last = i = 0;
969 while (1) {
Willy Tarreauae8b7962007-06-09 23:10:04 +0200970 j = *ptr++;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200971 if ((j == '-' || j == ':') && !last) {
972 last++;
973 pattern->val.range.min = i;
974 i = 0;
975 continue;
976 }
977 j -= '0';
978 if (j > 9)
979 // also catches the terminating zero
980 break;
981 i *= 10;
982 i += j;
983 }
Willy Tarreauae8b7962007-06-09 23:10:04 +0200984
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200985 if (last && *opaque >= 1 && *opaque <= 4) {
Willy Tarreauae8b7962007-06-09 23:10:04 +0200986 /* having a range with a min or a max is absurd */
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200987 memprintf(err, "integer range '%s' specified with a comparison operator", text[skip]);
Willy Tarreauae8b7962007-06-09 23:10:04 +0200988 return 0;
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200989 }
Willy Tarreauae8b7962007-06-09 23:10:04 +0200990
Willy Tarreaua84d3742007-05-07 00:36:48 +0200991 if (!last)
992 pattern->val.range.min = i;
993 pattern->val.range.max = i;
Willy Tarreauae8b7962007-06-09 23:10:04 +0200994
995 switch (*opaque) {
996 case 0: /* eq */
997 pattern->val.range.min_set = 1;
998 pattern->val.range.max_set = 1;
999 break;
1000 case 1: /* gt */
1001 pattern->val.range.min++; /* gt = ge + 1 */
1002 case 2: /* ge */
1003 pattern->val.range.min_set = 1;
1004 pattern->val.range.max_set = 0;
1005 break;
1006 case 3: /* lt */
1007 pattern->val.range.max--; /* lt = le - 1 */
1008 case 4: /* le */
1009 pattern->val.range.min_set = 0;
1010 pattern->val.range.max_set = 1;
1011 break;
1012 }
1013 return skip + 1;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001014}
1015
Willy Tarreau4a26d2f2008-07-15 16:05:33 +02001016/* Parse a range of positive 2-component versions delimited by either ':' or
1017 * '-'. The version consists in a major and a minor, both of which must be
1018 * smaller than 65536, because internally they will be represented as a 32-bit
1019 * integer.
1020 * If only one version is read, it is set as both min and max. Just like for
1021 * pure integers, an operator may be specified as the prefix, among this list
1022 * of 5 :
1023 *
1024 * 0:eq, 1:gt, 2:ge, 3:lt, 4:le
1025 *
1026 * The default operator is "eq". It supports range matching. Ranges are
1027 * rejected for other operators. The operator may be changed at any time.
1028 * The operator is stored in the 'opaque' argument. This allows constructs
1029 * such as the following one :
1030 *
1031 * acl obsolete_ssl ssl_req_proto lt 3
1032 * acl unsupported_ssl ssl_req_proto gt 3.1
1033 * acl valid_ssl ssl_req_proto 3.0-3.1
1034 *
1035 */
Willy Tarreau7dcb6482012-04-27 17:52:25 +02001036int acl_parse_dotted_ver(const char **text, struct acl_pattern *pattern, int *opaque, char **err)
Willy Tarreau4a26d2f2008-07-15 16:05:33 +02001037{
1038 signed long long i;
1039 unsigned int j, last, skip = 0;
1040 const char *ptr = *text;
1041
1042
1043 while (!isdigit((unsigned char)*ptr)) {
Willy Tarreau1c7cc5b2010-07-18 10:46:33 +02001044 switch (get_std_op(ptr)) {
1045 case STD_OP_EQ: *opaque = 0; break;
1046 case STD_OP_GT: *opaque = 1; break;
1047 case STD_OP_GE: *opaque = 2; break;
1048 case STD_OP_LT: *opaque = 3; break;
1049 case STD_OP_LE: *opaque = 4; break;
1050 default:
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001051 memprintf(err, "'%s' is neither a number nor a supported operator", ptr);
Willy Tarreau4a26d2f2008-07-15 16:05:33 +02001052 return 0;
Willy Tarreau1c7cc5b2010-07-18 10:46:33 +02001053 }
Willy Tarreau4a26d2f2008-07-15 16:05:33 +02001054
1055 skip++;
1056 ptr = text[skip];
1057 }
1058
1059 last = i = 0;
1060 while (1) {
1061 j = *ptr++;
1062 if (j == '.') {
1063 /* minor part */
1064 if (i >= 65536)
1065 return 0;
1066 i <<= 16;
1067 continue;
1068 }
1069 if ((j == '-' || j == ':') && !last) {
1070 last++;
1071 if (i < 65536)
1072 i <<= 16;
1073 pattern->val.range.min = i;
1074 i = 0;
1075 continue;
1076 }
1077 j -= '0';
1078 if (j > 9)
1079 // also catches the terminating zero
1080 break;
1081 i = (i & 0xFFFF0000) + (i & 0xFFFF) * 10;
1082 i += j;
1083 }
1084
1085 /* if we only got a major version, let's shift it now */
1086 if (i < 65536)
1087 i <<= 16;
1088
Willy Tarreau7dcb6482012-04-27 17:52:25 +02001089 if (last && *opaque >= 1 && *opaque <= 4) {
Willy Tarreau4a26d2f2008-07-15 16:05:33 +02001090 /* having a range with a min or a max is absurd */
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001091 memprintf(err, "version range '%s' specified with a comparison operator", text[skip]);
Willy Tarreau4a26d2f2008-07-15 16:05:33 +02001092 return 0;
Willy Tarreau7dcb6482012-04-27 17:52:25 +02001093 }
Willy Tarreau4a26d2f2008-07-15 16:05:33 +02001094
1095 if (!last)
1096 pattern->val.range.min = i;
1097 pattern->val.range.max = i;
1098
1099 switch (*opaque) {
1100 case 0: /* eq */
1101 pattern->val.range.min_set = 1;
1102 pattern->val.range.max_set = 1;
1103 break;
1104 case 1: /* gt */
1105 pattern->val.range.min++; /* gt = ge + 1 */
1106 case 2: /* ge */
1107 pattern->val.range.min_set = 1;
1108 pattern->val.range.max_set = 0;
1109 break;
1110 case 3: /* lt */
1111 pattern->val.range.max--; /* lt = le - 1 */
1112 case 4: /* le */
1113 pattern->val.range.min_set = 0;
1114 pattern->val.range.max_set = 1;
1115 break;
1116 }
1117 return skip + 1;
1118}
1119
Willy Tarreaua67fad92007-05-08 19:50:09 +02001120/* Parse an IP address and an optional mask in the form addr[/mask].
1121 * The addr may either be an IPv4 address or a hostname. The mask
1122 * may either be a dotted mask or a number of bits. Returns 1 if OK,
Willy Tarreauc92ddbc2012-04-27 22:10:57 +02001123 * otherwise 0. NOTE: IP address patterns are typed (IPV4/IPV6).
Willy Tarreaua67fad92007-05-08 19:50:09 +02001124 */
Willy Tarreau7dcb6482012-04-27 17:52:25 +02001125int acl_parse_ip(const char **text, struct acl_pattern *pattern, int *opaque, char **err)
Willy Tarreaua67fad92007-05-08 19:50:09 +02001126{
Willy Tarreaub337b532010-05-13 20:03:41 +02001127 struct eb_root *tree = NULL;
1128 if (pattern->flags & ACL_PAT_F_TREE_OK)
1129 tree = pattern->val.tree;
1130
1131 if (str2net(*text, &pattern->val.ipv4.addr, &pattern->val.ipv4.mask)) {
1132 unsigned int mask = ntohl(pattern->val.ipv4.mask.s_addr);
1133 struct ebmb_node *node;
1134 /* check if the mask is contiguous so that we can insert the
1135 * network into the tree. A continuous mask has only ones on
1136 * the left. This means that this mask + its lower bit added
1137 * once again is null.
1138 */
Willy Tarreauceb4ac92012-04-28 00:41:46 +02001139 pattern->type = SMP_T_IPV4;
Willy Tarreaub337b532010-05-13 20:03:41 +02001140 if (mask + (mask & -mask) == 0 && tree) {
1141 mask = mask ? 33 - flsnz(mask & -mask) : 0; /* equals cidr value */
1142 /* FIXME: insert <addr>/<mask> into the tree here */
1143 node = calloc(1, sizeof(*node) + 4); /* reserve 4 bytes for IPv4 address */
Willy Tarreau7dcb6482012-04-27 17:52:25 +02001144 if (!node) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001145 memprintf(err, "out of memory while loading IPv4 pattern");
Willy Tarreaub337b532010-05-13 20:03:41 +02001146 return 0;
Willy Tarreau7dcb6482012-04-27 17:52:25 +02001147 }
Willy Tarreaub337b532010-05-13 20:03:41 +02001148 memcpy(node->key, &pattern->val.ipv4.addr, 4); /* network byte order */
1149 node->node.pfx = mask;
1150 if (ebmb_insert_prefix(tree, node, 4) != node)
1151 free(node); /* was a duplicate */
1152 pattern->flags |= ACL_PAT_F_TREE;
1153 return 1;
1154 }
Willy Tarreauae8b7962007-06-09 23:10:04 +02001155 return 1;
Willy Tarreaub337b532010-05-13 20:03:41 +02001156 }
Willy Tarreauceb4ac92012-04-28 00:41:46 +02001157 else if (str62net(*text, &pattern->val.ipv6.addr, &pattern->val.ipv6.mask)) {
1158 /* no tree support right now */
1159 pattern->type = SMP_T_IPV6;
1160 return 1;
1161 }
Willy Tarreau7dcb6482012-04-27 17:52:25 +02001162 else {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001163 memprintf(err, "'%s' is not a valid IPv4 or IPv6 address", *text);
Willy Tarreauae8b7962007-06-09 23:10:04 +02001164 return 0;
Willy Tarreau7dcb6482012-04-27 17:52:25 +02001165 }
Willy Tarreaua67fad92007-05-08 19:50:09 +02001166}
1167
Willy Tarreaua84d3742007-05-07 00:36:48 +02001168/*
1169 * Registers the ACL keyword list <kwl> as a list of valid keywords for next
1170 * parsing sessions.
1171 */
1172void acl_register_keywords(struct acl_kw_list *kwl)
1173{
1174 LIST_ADDQ(&acl_keywords.list, &kwl->list);
1175}
1176
1177/*
1178 * Unregisters the ACL keyword list <kwl> from the list of valid keywords.
1179 */
1180void acl_unregister_keywords(struct acl_kw_list *kwl)
1181{
1182 LIST_DEL(&kwl->list);
1183 LIST_INIT(&kwl->list);
1184}
1185
1186/* Return a pointer to the ACL <name> within the list starting at <head>, or
1187 * NULL if not found.
1188 */
1189struct acl *find_acl_by_name(const char *name, struct list *head)
1190{
1191 struct acl *acl;
1192 list_for_each_entry(acl, head, list) {
1193 if (strcmp(acl->name, name) == 0)
1194 return acl;
1195 }
1196 return NULL;
1197}
1198
1199/* Return a pointer to the ACL keyword <kw>, or NULL if not found. Note that if
1200 * <kw> contains an opening parenthesis, only the left part of it is checked.
1201 */
1202struct acl_keyword *find_acl_kw(const char *kw)
1203{
1204 int index;
1205 const char *kwend;
1206 struct acl_kw_list *kwl;
1207
1208 kwend = strchr(kw, '(');
1209 if (!kwend)
1210 kwend = kw + strlen(kw);
1211
1212 list_for_each_entry(kwl, &acl_keywords.list, list) {
1213 for (index = 0; kwl->kw[index].kw != NULL; index++) {
1214 if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) &&
1215 kwl->kw[index].kw[kwend-kw] == 0)
1216 return &kwl->kw[index];
1217 }
1218 }
1219 return NULL;
1220}
1221
Willy Tarreaudfd7fca2011-03-09 07:27:02 +01001222/* NB: does nothing if <pat> is NULL */
Willy Tarreaua84d3742007-05-07 00:36:48 +02001223static void free_pattern(struct acl_pattern *pat)
1224{
Willy Tarreaudfd7fca2011-03-09 07:27:02 +01001225 if (!pat)
1226 return;
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02001227
1228 if (pat->ptr.ptr) {
1229 if (pat->freeptrbuf)
1230 pat->freeptrbuf(pat->ptr.ptr);
1231
Willy Tarreaua84d3742007-05-07 00:36:48 +02001232 free(pat->ptr.ptr);
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02001233 }
1234
Willy Tarreaua84d3742007-05-07 00:36:48 +02001235 free(pat);
1236}
1237
1238static void free_pattern_list(struct list *head)
1239{
1240 struct acl_pattern *pat, *tmp;
1241 list_for_each_entry_safe(pat, tmp, head, list)
1242 free_pattern(pat);
1243}
1244
Willy Tarreaue56cda92010-05-11 23:25:05 +02001245static void free_pattern_tree(struct eb_root *root)
1246{
1247 struct eb_node *node, *next;
1248 node = eb_first(root);
1249 while (node) {
1250 next = eb_next(node);
1251 free(node);
1252 node = next;
1253 }
1254}
1255
Willy Tarreaua84d3742007-05-07 00:36:48 +02001256static struct acl_expr *prune_acl_expr(struct acl_expr *expr)
1257{
Willy Tarreau34db1082012-04-19 17:16:54 +02001258 struct arg *arg;
1259
Willy Tarreaua84d3742007-05-07 00:36:48 +02001260 free_pattern_list(&expr->patterns);
Willy Tarreaue56cda92010-05-11 23:25:05 +02001261 free_pattern_tree(&expr->pattern_tree);
Willy Tarreaua84d3742007-05-07 00:36:48 +02001262 LIST_INIT(&expr->patterns);
Willy Tarreau34db1082012-04-19 17:16:54 +02001263
1264 for (arg = expr->args; arg; arg++) {
1265 if (arg->type == ARGT_STOP)
1266 break;
Willy Tarreau496aa012012-06-01 10:38:29 +02001267 if (arg->type == ARGT_STR || arg->unresolved) {
Willy Tarreau34db1082012-04-19 17:16:54 +02001268 free(arg->data.str.str);
1269 arg->data.str.str = NULL;
Willy Tarreau496aa012012-06-01 10:38:29 +02001270 arg->unresolved = 0;
Willy Tarreau34db1082012-04-19 17:16:54 +02001271 }
Willy Tarreau34db1082012-04-19 17:16:54 +02001272 }
1273
Willy Tarreau2e845be2012-10-19 19:49:09 +02001274 if (expr->args != empty_arg_list)
1275 free(expr->args);
Willy Tarreaua84d3742007-05-07 00:36:48 +02001276 expr->kw->use_cnt--;
1277 return expr;
1278}
1279
Willy Tarreau08ad0b32012-04-27 17:25:24 +02001280
1281/* Reads patterns from a file. If <err_msg> is non-NULL, an error message will
1282 * be returned there on errors and the caller will have to free it.
1283 */
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001284static int acl_read_patterns_from_file( struct acl_keyword *aclkw,
1285 struct acl_expr *expr,
Willy Tarreau08ad0b32012-04-27 17:25:24 +02001286 const char *filename, int patflags,
1287 char **err)
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001288{
1289 FILE *file;
1290 char *c;
1291 const char *args[2];
1292 struct acl_pattern *pattern;
1293 int opaque;
Willy Tarreau6a8097f2011-02-26 15:14:15 +01001294 int ret = 0;
Willy Tarreau08ad0b32012-04-27 17:25:24 +02001295 int line = 0;
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001296
1297 file = fopen(filename, "r");
Willy Tarreau08ad0b32012-04-27 17:25:24 +02001298 if (!file) {
1299 memprintf(err, "failed to open pattern file <%s>", filename);
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001300 return 0;
Willy Tarreau08ad0b32012-04-27 17:25:24 +02001301 }
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001302
1303 /* now parse all patterns. The file may contain only one pattern per
1304 * line. If the line contains spaces, they will be part of the pattern.
1305 * The pattern stops at the first CR, LF or EOF encountered.
1306 */
1307 opaque = 0;
Willy Tarreaue56cda92010-05-11 23:25:05 +02001308 pattern = NULL;
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001309 args[1] = "";
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001310 while (fgets(trash.str, trash.size, file) != NULL) {
Willy Tarreau08ad0b32012-04-27 17:25:24 +02001311 line++;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001312 c = trash.str;
Willy Tarreau58215a02010-05-13 22:07:43 +02001313
1314 /* ignore lines beginning with a dash */
1315 if (*c == '#')
1316 continue;
1317
1318 /* strip leading spaces and tabs */
1319 while (*c == ' ' || *c == '\t')
1320 c++;
1321
Willy Tarreau58215a02010-05-13 22:07:43 +02001322
1323 args[0] = c;
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001324 while (*c && *c != '\n' && *c != '\r')
1325 c++;
1326 *c = 0;
1327
Willy Tarreau51091962011-01-03 21:04:10 +01001328 /* empty lines are ignored too */
1329 if (c == args[0])
1330 continue;
1331
Willy Tarreaue56cda92010-05-11 23:25:05 +02001332 /* we keep the previous pattern along iterations as long as it's not used */
1333 if (!pattern)
1334 pattern = (struct acl_pattern *)malloc(sizeof(*pattern));
Willy Tarreau08ad0b32012-04-27 17:25:24 +02001335 if (!pattern) {
1336 memprintf(err, "out of memory when loading patterns from file <%s>", filename);
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001337 goto out_close;
Willy Tarreau08ad0b32012-04-27 17:25:24 +02001338 }
Willy Tarreaue56cda92010-05-11 23:25:05 +02001339
1340 memset(pattern, 0, sizeof(*pattern));
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001341 pattern->flags = patflags;
1342
Willy Tarreaue56cda92010-05-11 23:25:05 +02001343 if ((aclkw->requires & ACL_MAY_LOOKUP) && !(pattern->flags & ACL_PAT_F_IGNORE_CASE)) {
1344 /* we pre-set the data pointer to the tree's head so that functions
1345 * which are able to insert in a tree know where to do that.
1346 */
1347 pattern->flags |= ACL_PAT_F_TREE_OK;
1348 pattern->val.tree = &expr->pattern_tree;
1349 }
1350
Willy Tarreauc92ddbc2012-04-27 22:10:57 +02001351 pattern->type = SMP_TYPES; /* unspecified type by default */
Willy Tarreau7dcb6482012-04-27 17:52:25 +02001352 if (!aclkw->parse(args, pattern, &opaque, err))
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001353 goto out_free_pattern;
Willy Tarreaue56cda92010-05-11 23:25:05 +02001354
1355 /* if the parser did not feed the tree, let's chain the pattern to the list */
1356 if (!(pattern->flags & ACL_PAT_F_TREE)) {
1357 LIST_ADDQ(&expr->patterns, &pattern->list);
1358 pattern = NULL; /* get a new one */
1359 }
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001360 }
Willy Tarreau6a8097f2011-02-26 15:14:15 +01001361
1362 ret = 1; /* success */
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001363
1364 out_free_pattern:
1365 free_pattern(pattern);
1366 out_close:
1367 fclose(file);
Willy Tarreau6a8097f2011-02-26 15:14:15 +01001368 return ret;
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001369}
1370
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001371/* Parse an ACL expression starting at <args>[0], and return it. If <err> is
1372 * not NULL, it will be filled with a pointer to an error message in case of
1373 * error. This pointer must be freeable or NULL.
1374 *
Willy Tarreaua84d3742007-05-07 00:36:48 +02001375 * Right now, the only accepted syntax is :
1376 * <subject> [<value>...]
1377 */
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001378struct acl_expr *parse_acl_expr(const char **args, char **err)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001379{
1380 __label__ out_return, out_free_expr, out_free_pattern;
1381 struct acl_expr *expr;
1382 struct acl_keyword *aclkw;
1383 struct acl_pattern *pattern;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02001384 int opaque, patflags;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001385 const char *arg;
1386
1387 aclkw = find_acl_kw(args[0]);
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001388 if (!aclkw || !aclkw->parse) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001389 memprintf(err, "unknown ACL keyword '%s'", *args);
Willy Tarreaua84d3742007-05-07 00:36:48 +02001390 goto out_return;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001391 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001392
1393 expr = (struct acl_expr *)calloc(1, sizeof(*expr));
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001394 if (!expr) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001395 memprintf(err, "out of memory when parsing ACL expression");
Willy Tarreaua84d3742007-05-07 00:36:48 +02001396 goto out_return;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001397 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001398
1399 expr->kw = aclkw;
1400 aclkw->use_cnt++;
1401 LIST_INIT(&expr->patterns);
Willy Tarreaue56cda92010-05-11 23:25:05 +02001402 expr->pattern_tree = EB_ROOT_UNIQUE;
Willy Tarreau2e845be2012-10-19 19:49:09 +02001403 expr->args = empty_arg_list;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001404
1405 arg = strchr(args[0], '(');
Willy Tarreau61612d42012-04-19 18:42:05 +02001406 if (aclkw->arg_mask) {
1407 int nbargs = 0;
Willy Tarreau34db1082012-04-19 17:16:54 +02001408 char *end;
Willy Tarreau34db1082012-04-19 17:16:54 +02001409
Willy Tarreau61612d42012-04-19 18:42:05 +02001410 if (arg != NULL) {
1411 /* there are 0 or more arguments in the form "subject(arg[,arg]*)" */
1412 arg++;
1413 end = strchr(arg, ')');
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001414 if (!end) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001415 memprintf(err, "missing closing ')' after arguments to ACL keyword '%s'", aclkw->kw);
Willy Tarreau61612d42012-04-19 18:42:05 +02001416 goto out_free_expr;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001417 }
Willy Tarreau34db1082012-04-19 17:16:54 +02001418
Willy Tarreau61612d42012-04-19 18:42:05 +02001419 /* Parse the arguments. Note that currently we have no way to
1420 * report parsing errors, hence the NULL in the error pointers.
1421 * An error is also reported if some mandatory arguments are
1422 * missing.
1423 */
1424 nbargs = make_arg_list(arg, end - arg, aclkw->arg_mask, &expr->args,
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001425 err, NULL, NULL);
1426 if (nbargs < 0) {
1427 /* note that make_arg_list will have set <err> here */
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001428 memprintf(err, "in argument to '%s', %s", aclkw->kw, *err);
Willy Tarreau61612d42012-04-19 18:42:05 +02001429 goto out_free_expr;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001430 }
Willy Tarreauae52f062012-04-26 12:13:35 +02001431
Willy Tarreau2e845be2012-10-19 19:49:09 +02001432 if (!expr->args)
1433 expr->args = empty_arg_list;
1434
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001435 if (aclkw->val_args && !aclkw->val_args(expr->args, err)) {
1436 /* invalid keyword argument, error must have been
1437 * set by val_args().
1438 */
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001439 memprintf(err, "in argument to '%s', %s", aclkw->kw, *err);
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001440 goto out_free_expr;
1441 }
Willy Tarreau61612d42012-04-19 18:42:05 +02001442 }
Willy Tarreaufc2c1fd2012-04-19 23:35:54 +02001443 else if (ARGM(aclkw->arg_mask) == 1) {
1444 int type = (aclkw->arg_mask >> 4) & 15;
1445
1446 /* If a proxy is noted as a mandatory argument, we'll fake
1447 * an empty one so that acl_find_targets() resolves it as
1448 * the current one later.
1449 */
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001450 if (type != ARGT_FE && type != ARGT_BE && type != ARGT_TAB) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001451 memprintf(err, "ACL keyword '%s' expects %d arguments", aclkw->kw, ARGM(aclkw->arg_mask));
Willy Tarreaufc2c1fd2012-04-19 23:35:54 +02001452 goto out_free_expr;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001453 }
Willy Tarreaufc2c1fd2012-04-19 23:35:54 +02001454
1455 /* Build an arg list containing the type as an empty string
1456 * and the usual STOP.
1457 */
1458 expr->args = calloc(2, sizeof(*expr->args));
1459 expr->args[0].type = type;
Willy Tarreaue3a46112012-06-15 08:02:34 +02001460 expr->args[0].unresolved = 1;
Willy Tarreaufc2c1fd2012-04-19 23:35:54 +02001461 expr->args[0].data.str.str = strdup("");
1462 expr->args[0].data.str.len = 1;
1463 expr->args[0].data.str.len = 0;
1464 expr->args[1].type = ARGT_STOP;
1465 }
Willy Tarreau61612d42012-04-19 18:42:05 +02001466 else if (ARGM(aclkw->arg_mask)) {
1467 /* there were some mandatory arguments */
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001468 memprintf(err, "ACL keyword '%s' expects %d arguments", aclkw->kw, ARGM(aclkw->arg_mask));
Willy Tarreaua84d3742007-05-07 00:36:48 +02001469 goto out_free_expr;
Willy Tarreau61612d42012-04-19 18:42:05 +02001470 }
1471 }
1472 else {
1473 if (arg) {
1474 /* no argument expected */
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001475 memprintf(err, "ACL keyword '%s' takes no argument", aclkw->kw);
Willy Tarreau61612d42012-04-19 18:42:05 +02001476 goto out_free_expr;
1477 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001478 }
1479
Willy Tarreaua84d3742007-05-07 00:36:48 +02001480 args++;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02001481
1482 /* check for options before patterns. Supported options are :
1483 * -i : ignore case for all patterns by default
1484 * -f : read patterns from those files
1485 * -- : everything after this is not an option
1486 */
1487 patflags = 0;
1488 while (**args == '-') {
1489 if ((*args)[1] == 'i')
1490 patflags |= ACL_PAT_F_IGNORE_CASE;
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001491 else if ((*args)[1] == 'f') {
Willy Tarreau08ad0b32012-04-27 17:25:24 +02001492 if (!acl_read_patterns_from_file(aclkw, expr, args[1], patflags | ACL_PAT_F_FROM_FILE, err))
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001493 goto out_free_expr;
1494 args++;
1495 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +02001496 else if ((*args)[1] == '-') {
1497 args++;
1498 break;
1499 }
1500 else
1501 break;
1502 args++;
1503 }
1504
1505 /* now parse all patterns */
Willy Tarreauae8b7962007-06-09 23:10:04 +02001506 opaque = 0;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001507 while (**args) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02001508 int ret;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001509 pattern = (struct acl_pattern *)calloc(1, sizeof(*pattern));
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001510 if (!pattern) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001511 memprintf(err, "out of memory when parsing ACL pattern");
Willy Tarreaua84d3742007-05-07 00:36:48 +02001512 goto out_free_expr;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001513 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +02001514 pattern->flags = patflags;
1515
Willy Tarreauc92ddbc2012-04-27 22:10:57 +02001516 pattern->type = SMP_TYPES; /* unspecified type */
Willy Tarreau7dcb6482012-04-27 17:52:25 +02001517 ret = aclkw->parse(args, pattern, &opaque, err);
1518 if (!ret)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001519 goto out_free_pattern;
Willy Tarreau7dcb6482012-04-27 17:52:25 +02001520
Willy Tarreaua84d3742007-05-07 00:36:48 +02001521 LIST_ADDQ(&expr->patterns, &pattern->list);
Willy Tarreauae8b7962007-06-09 23:10:04 +02001522 args += ret;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001523 }
1524
1525 return expr;
1526
1527 out_free_pattern:
1528 free_pattern(pattern);
1529 out_free_expr:
1530 prune_acl_expr(expr);
1531 free(expr);
1532 out_return:
1533 return NULL;
1534}
1535
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02001536/* Purge everything in the acl <acl>, then return <acl>. */
1537struct acl *prune_acl(struct acl *acl) {
1538
1539 struct acl_expr *expr, *exprb;
1540
1541 free(acl->name);
1542
1543 list_for_each_entry_safe(expr, exprb, &acl->expr, list) {
1544 LIST_DEL(&expr->list);
1545 prune_acl_expr(expr);
1546 free(expr);
1547 }
1548
1549 return acl;
1550}
1551
Willy Tarreaua84d3742007-05-07 00:36:48 +02001552/* Parse an ACL with the name starting at <args>[0], and with a list of already
1553 * known ACLs in <acl>. If the ACL was not in the list, it will be added.
Willy Tarreau2a56c5e2010-03-15 16:13:29 +01001554 * A pointer to that ACL is returned. If the ACL has an empty name, then it's
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001555 * an anonymous one and it won't be merged with any other one. If <err> is not
1556 * NULL, it will be filled with an appropriate error. This pointer must be
1557 * freeable or NULL.
Willy Tarreaua84d3742007-05-07 00:36:48 +02001558 *
1559 * args syntax: <aclname> <acl_expr>
1560 */
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001561struct acl *parse_acl(const char **args, struct list *known_acl, char **err)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001562{
1563 __label__ out_return, out_free_acl_expr, out_free_name;
1564 struct acl *cur_acl;
1565 struct acl_expr *acl_expr;
1566 char *name;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001567 const char *pos;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001568
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001569 if (**args && (pos = invalid_char(*args))) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001570 memprintf(err, "invalid character in ACL name : '%c'", *pos);
Willy Tarreau2e74c3f2007-12-02 18:45:09 +01001571 goto out_return;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001572 }
Willy Tarreau2e74c3f2007-12-02 18:45:09 +01001573
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001574 acl_expr = parse_acl_expr(args + 1, err);
1575 if (!acl_expr) {
1576 /* parse_acl_expr will have filled <err> here */
Willy Tarreaua84d3742007-05-07 00:36:48 +02001577 goto out_return;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001578 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001579
Willy Tarreau404e8ab2009-07-26 19:40:40 +02001580 /* Check for args beginning with an opening parenthesis just after the
1581 * subject, as this is almost certainly a typo. Right now we can only
1582 * emit a warning, so let's do so.
1583 */
Krzysztof Piotr Oledzki4cdd8312009-10-05 00:23:35 +02001584 if (!strchr(args[1], '(') && *args[2] == '(')
Willy Tarreau404e8ab2009-07-26 19:40:40 +02001585 Warning("parsing acl '%s' :\n"
1586 " matching '%s' for pattern '%s' is likely a mistake and probably\n"
1587 " not what you want. Maybe you need to remove the extraneous space before '('.\n"
1588 " If you are really sure this is not an error, please insert '--' between the\n"
1589 " match and the pattern to make this warning message disappear.\n",
1590 args[0], args[1], args[2]);
1591
Willy Tarreau2a56c5e2010-03-15 16:13:29 +01001592 if (*args[0])
1593 cur_acl = find_acl_by_name(args[0], known_acl);
1594 else
1595 cur_acl = NULL;
1596
Willy Tarreaua84d3742007-05-07 00:36:48 +02001597 if (!cur_acl) {
1598 name = strdup(args[0]);
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001599 if (!name) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001600 memprintf(err, "out of memory when parsing ACL");
Willy Tarreaua84d3742007-05-07 00:36:48 +02001601 goto out_free_acl_expr;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001602 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001603 cur_acl = (struct acl *)calloc(1, sizeof(*cur_acl));
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001604 if (cur_acl == NULL) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001605 memprintf(err, "out of memory when parsing ACL");
Willy Tarreaua84d3742007-05-07 00:36:48 +02001606 goto out_free_name;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001607 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001608
1609 LIST_INIT(&cur_acl->expr);
1610 LIST_ADDQ(known_acl, &cur_acl->list);
1611 cur_acl->name = name;
1612 }
1613
Willy Tarreaua9802632008-07-25 19:13:19 +02001614 cur_acl->requires |= acl_expr->kw->requires;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001615 LIST_ADDQ(&cur_acl->expr, &acl_expr->list);
1616 return cur_acl;
1617
1618 out_free_name:
1619 free(name);
1620 out_free_acl_expr:
1621 prune_acl_expr(acl_expr);
1622 free(acl_expr);
1623 out_return:
1624 return NULL;
1625}
1626
Willy Tarreau16fbe822007-06-17 11:54:31 +02001627/* Some useful ACLs provided by default. Only those used are allocated. */
1628
1629const struct {
1630 const char *name;
1631 const char *expr[4]; /* put enough for longest expression */
1632} default_acl_list[] = {
Willy Tarreau58393e12008-07-20 10:39:22 +02001633 { .name = "TRUE", .expr = {"always_true",""}},
1634 { .name = "FALSE", .expr = {"always_false",""}},
Willy Tarreau16fbe822007-06-17 11:54:31 +02001635 { .name = "LOCALHOST", .expr = {"src","127.0.0.1/8",""}},
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001636 { .name = "HTTP", .expr = {"req_proto_http",""}},
Willy Tarreau16fbe822007-06-17 11:54:31 +02001637 { .name = "HTTP_1.0", .expr = {"req_ver","1.0",""}},
1638 { .name = "HTTP_1.1", .expr = {"req_ver","1.1",""}},
1639 { .name = "METH_CONNECT", .expr = {"method","CONNECT",""}},
1640 { .name = "METH_GET", .expr = {"method","GET","HEAD",""}},
1641 { .name = "METH_HEAD", .expr = {"method","HEAD",""}},
1642 { .name = "METH_OPTIONS", .expr = {"method","OPTIONS",""}},
1643 { .name = "METH_POST", .expr = {"method","POST",""}},
1644 { .name = "METH_TRACE", .expr = {"method","TRACE",""}},
1645 { .name = "HTTP_URL_ABS", .expr = {"url_reg","^[^/:]*://",""}},
1646 { .name = "HTTP_URL_SLASH", .expr = {"url_beg","/",""}},
1647 { .name = "HTTP_URL_STAR", .expr = {"url","*",""}},
1648 { .name = "HTTP_CONTENT", .expr = {"hdr_val(content-length)","gt","0",""}},
Emeric Brunbede3d02009-06-30 17:54:00 +02001649 { .name = "RDP_COOKIE", .expr = {"req_rdp_cookie_cnt","gt","0",""}},
Willy Tarreauc6317702008-07-20 09:29:50 +02001650 { .name = "REQ_CONTENT", .expr = {"req_len","gt","0",""}},
Willy Tarreaub6fb4202008-07-20 11:18:28 +02001651 { .name = "WAIT_END", .expr = {"wait_end",""}},
Willy Tarreau16fbe822007-06-17 11:54:31 +02001652 { .name = NULL, .expr = {""}}
1653};
1654
1655/* Find a default ACL from the default_acl list, compile it and return it.
1656 * If the ACL is not found, NULL is returned. In theory, it cannot fail,
1657 * except when default ACLs are broken, in which case it will return NULL.
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001658 * If <known_acl> is not NULL, the ACL will be queued at its tail. If <err> is
1659 * not NULL, it will be filled with an error message if an error occurs. This
1660 * pointer must be freeable or NULL.
Willy Tarreau16fbe822007-06-17 11:54:31 +02001661 */
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001662struct acl *find_acl_default(const char *acl_name, struct list *known_acl, char **err)
Willy Tarreau16fbe822007-06-17 11:54:31 +02001663{
1664 __label__ out_return, out_free_acl_expr, out_free_name;
1665 struct acl *cur_acl;
1666 struct acl_expr *acl_expr;
1667 char *name;
1668 int index;
1669
1670 for (index = 0; default_acl_list[index].name != NULL; index++) {
1671 if (strcmp(acl_name, default_acl_list[index].name) == 0)
1672 break;
1673 }
1674
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001675 if (default_acl_list[index].name == NULL) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001676 memprintf(err, "no such ACL : '%s'", acl_name);
Willy Tarreau16fbe822007-06-17 11:54:31 +02001677 return NULL;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001678 }
Willy Tarreau16fbe822007-06-17 11:54:31 +02001679
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001680 acl_expr = parse_acl_expr((const char **)default_acl_list[index].expr, err);
1681 if (!acl_expr) {
1682 /* parse_acl_expr must have filled err here */
Willy Tarreau16fbe822007-06-17 11:54:31 +02001683 goto out_return;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001684 }
Willy Tarreau16fbe822007-06-17 11:54:31 +02001685
1686 name = strdup(acl_name);
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001687 if (!name) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001688 memprintf(err, "out of memory when building default ACL '%s'", acl_name);
Willy Tarreau16fbe822007-06-17 11:54:31 +02001689 goto out_free_acl_expr;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001690 }
1691
Willy Tarreau16fbe822007-06-17 11:54:31 +02001692 cur_acl = (struct acl *)calloc(1, sizeof(*cur_acl));
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001693 if (cur_acl == NULL) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001694 memprintf(err, "out of memory when building default ACL '%s'", acl_name);
Willy Tarreau16fbe822007-06-17 11:54:31 +02001695 goto out_free_name;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001696 }
Willy Tarreau16fbe822007-06-17 11:54:31 +02001697
1698 cur_acl->name = name;
Willy Tarreaua55b7dc2009-07-12 09:21:30 +02001699 cur_acl->requires |= acl_expr->kw->requires;
Willy Tarreau16fbe822007-06-17 11:54:31 +02001700 LIST_INIT(&cur_acl->expr);
1701 LIST_ADDQ(&cur_acl->expr, &acl_expr->list);
1702 if (known_acl)
1703 LIST_ADDQ(known_acl, &cur_acl->list);
1704
1705 return cur_acl;
1706
1707 out_free_name:
1708 free(name);
1709 out_free_acl_expr:
1710 prune_acl_expr(acl_expr);
1711 free(acl_expr);
1712 out_return:
1713 return NULL;
1714}
Willy Tarreaua84d3742007-05-07 00:36:48 +02001715
1716/* Purge everything in the acl_cond <cond>, then return <cond>. */
1717struct acl_cond *prune_acl_cond(struct acl_cond *cond)
1718{
1719 struct acl_term_suite *suite, *tmp_suite;
1720 struct acl_term *term, *tmp_term;
1721
1722 /* iterate through all term suites and free all terms and all suites */
1723 list_for_each_entry_safe(suite, tmp_suite, &cond->suites, list) {
1724 list_for_each_entry_safe(term, tmp_term, &suite->terms, list)
1725 free(term);
1726 free(suite);
1727 }
1728 return cond;
1729}
1730
1731/* Parse an ACL condition starting at <args>[0], relying on a list of already
1732 * known ACLs passed in <known_acl>. The new condition is returned (or NULL in
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001733 * case of low memory). Supports multiple conditions separated by "or". If
1734 * <err> is not NULL, it will be filled with a pointer to an error message in
1735 * case of error, that the caller is responsible for freeing. The initial
1736 * location must either be freeable or NULL.
Willy Tarreaua84d3742007-05-07 00:36:48 +02001737 */
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001738struct acl_cond *parse_acl_cond(const char **args, struct list *known_acl, int pol, char **err)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001739{
1740 __label__ out_return, out_free_suite, out_free_term;
Willy Tarreau74b98a82007-06-16 19:35:18 +02001741 int arg, neg;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001742 const char *word;
1743 struct acl *cur_acl;
1744 struct acl_term *cur_term;
1745 struct acl_term_suite *cur_suite;
1746 struct acl_cond *cond;
1747
1748 cond = (struct acl_cond *)calloc(1, sizeof(*cond));
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001749 if (cond == NULL) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001750 memprintf(err, "out of memory when parsing condition");
Willy Tarreaua84d3742007-05-07 00:36:48 +02001751 goto out_return;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001752 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001753
1754 LIST_INIT(&cond->list);
1755 LIST_INIT(&cond->suites);
1756 cond->pol = pol;
1757
1758 cur_suite = NULL;
Willy Tarreau74b98a82007-06-16 19:35:18 +02001759 neg = 0;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001760 for (arg = 0; *args[arg]; arg++) {
1761 word = args[arg];
1762
1763 /* remove as many exclamation marks as we can */
1764 while (*word == '!') {
1765 neg = !neg;
1766 word++;
1767 }
1768
1769 /* an empty word is allowed because we cannot force the user to
1770 * always think about not leaving exclamation marks alone.
1771 */
1772 if (!*word)
1773 continue;
1774
Willy Tarreau16fbe822007-06-17 11:54:31 +02001775 if (strcasecmp(word, "or") == 0 || strcmp(word, "||") == 0) {
Willy Tarreaua84d3742007-05-07 00:36:48 +02001776 /* new term suite */
1777 cur_suite = NULL;
1778 neg = 0;
1779 continue;
1780 }
1781
Willy Tarreau95fa4692010-02-01 13:05:50 +01001782 if (strcmp(word, "{") == 0) {
1783 /* we may have a complete ACL expression between two braces,
1784 * find the last one.
1785 */
1786 int arg_end = arg + 1;
1787 const char **args_new;
1788
1789 while (*args[arg_end] && strcmp(args[arg_end], "}") != 0)
1790 arg_end++;
1791
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001792 if (!*args[arg_end]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001793 memprintf(err, "missing closing '}' in condition");
Willy Tarreau95fa4692010-02-01 13:05:50 +01001794 goto out_free_suite;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001795 }
Willy Tarreau95fa4692010-02-01 13:05:50 +01001796
1797 args_new = calloc(1, (arg_end - arg + 1) * sizeof(*args_new));
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001798 if (!args_new) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001799 memprintf(err, "out of memory when parsing condition");
Willy Tarreau95fa4692010-02-01 13:05:50 +01001800 goto out_free_suite;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001801 }
Willy Tarreau95fa4692010-02-01 13:05:50 +01001802
Willy Tarreau2a56c5e2010-03-15 16:13:29 +01001803 args_new[0] = "";
Willy Tarreau95fa4692010-02-01 13:05:50 +01001804 memcpy(args_new + 1, args + arg + 1, (arg_end - arg) * sizeof(*args_new));
1805 args_new[arg_end - arg] = "";
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001806 cur_acl = parse_acl(args_new, known_acl, err);
Willy Tarreau95fa4692010-02-01 13:05:50 +01001807 free(args_new);
1808
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001809 if (!cur_acl) {
1810 /* note that parse_acl() must have filled <err> here */
Willy Tarreau16fbe822007-06-17 11:54:31 +02001811 goto out_free_suite;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001812 }
Willy Tarreau95fa4692010-02-01 13:05:50 +01001813 arg = arg_end;
1814 }
1815 else {
1816 /* search for <word> in the known ACL names. If we do not find
1817 * it, let's look for it in the default ACLs, and if found, add
1818 * it to the list of ACLs of this proxy. This makes it possible
1819 * to override them.
1820 */
1821 cur_acl = find_acl_by_name(word, known_acl);
1822 if (cur_acl == NULL) {
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001823 cur_acl = find_acl_default(word, known_acl, err);
1824 if (cur_acl == NULL) {
1825 /* note that find_acl_default() must have filled <err> here */
Willy Tarreau95fa4692010-02-01 13:05:50 +01001826 goto out_free_suite;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001827 }
Willy Tarreau95fa4692010-02-01 13:05:50 +01001828 }
Willy Tarreau16fbe822007-06-17 11:54:31 +02001829 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001830
1831 cur_term = (struct acl_term *)calloc(1, sizeof(*cur_term));
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001832 if (cur_term == NULL) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001833 memprintf(err, "out of memory when parsing condition");
Willy Tarreaua84d3742007-05-07 00:36:48 +02001834 goto out_free_suite;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001835 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001836
1837 cur_term->acl = cur_acl;
1838 cur_term->neg = neg;
Willy Tarreaua9802632008-07-25 19:13:19 +02001839 cond->requires |= cur_acl->requires;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001840
1841 if (!cur_suite) {
1842 cur_suite = (struct acl_term_suite *)calloc(1, sizeof(*cur_suite));
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001843 if (cur_term == NULL) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001844 memprintf(err, "out of memory when parsing condition");
Willy Tarreaua84d3742007-05-07 00:36:48 +02001845 goto out_free_term;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001846 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001847 LIST_INIT(&cur_suite->terms);
1848 LIST_ADDQ(&cond->suites, &cur_suite->list);
1849 }
1850 LIST_ADDQ(&cur_suite->terms, &cur_term->list);
Willy Tarreau74b98a82007-06-16 19:35:18 +02001851 neg = 0;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001852 }
1853
1854 return cond;
1855
1856 out_free_term:
1857 free(cur_term);
1858 out_free_suite:
1859 prune_acl_cond(cond);
1860 free(cond);
1861 out_return:
1862 return NULL;
1863}
1864
Willy Tarreau2bbba412010-01-28 16:48:33 +01001865/* Builds an ACL condition starting at the if/unless keyword. The complete
1866 * condition is returned. NULL is returned in case of error or if the first
1867 * word is neither "if" nor "unless". It automatically sets the file name and
1868 * the line number in the condition for better error reporting, and adds the
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001869 * ACL requirements to the proxy's acl_requires. If <err> is not NULL, it will
1870 * be filled with a pointer to an error message in case of error, that the
1871 * caller is responsible for freeing. The initial location must either be
1872 * freeable or NULL.
Willy Tarreau2bbba412010-01-28 16:48:33 +01001873 */
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001874struct acl_cond *build_acl_cond(const char *file, int line, struct proxy *px, const char **args, char **err)
Willy Tarreau2bbba412010-01-28 16:48:33 +01001875{
1876 int pol = ACL_COND_NONE;
1877 struct acl_cond *cond = NULL;
1878
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001879 if (err)
1880 *err = NULL;
1881
Willy Tarreau2bbba412010-01-28 16:48:33 +01001882 if (!strcmp(*args, "if")) {
1883 pol = ACL_COND_IF;
1884 args++;
1885 }
1886 else if (!strcmp(*args, "unless")) {
1887 pol = ACL_COND_UNLESS;
1888 args++;
1889 }
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001890 else {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001891 memprintf(err, "conditions must start with either 'if' or 'unless'");
Willy Tarreau2bbba412010-01-28 16:48:33 +01001892 return NULL;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001893 }
Willy Tarreau2bbba412010-01-28 16:48:33 +01001894
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001895 cond = parse_acl_cond(args, &px->acl, pol, err);
1896 if (!cond) {
1897 /* note that parse_acl_cond must have filled <err> here */
Willy Tarreau2bbba412010-01-28 16:48:33 +01001898 return NULL;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001899 }
Willy Tarreau2bbba412010-01-28 16:48:33 +01001900
1901 cond->file = file;
1902 cond->line = line;
1903 px->acl_requires |= cond->requires;
1904
1905 return cond;
1906}
1907
Willy Tarreau11382812008-07-09 16:18:21 +02001908/* Execute condition <cond> and return either ACL_PAT_FAIL, ACL_PAT_MISS or
Willy Tarreaub6866442008-07-14 23:54:42 +02001909 * ACL_PAT_PASS depending on the test results. ACL_PAT_MISS may only be
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001910 * returned if <opt> does not contain SMP_OPT_FINAL, indicating that incomplete
Willy Tarreau7a777ed2012-04-26 11:44:02 +02001911 * data is being examined. The function automatically sets SMP_OPT_ITERATE.
Willy Tarreaub6866442008-07-14 23:54:42 +02001912 * This function only computes the condition, it does not apply the polarity
1913 * required by IF/UNLESS, it's up to the caller to do this using something like
1914 * this :
Willy Tarreau11382812008-07-09 16:18:21 +02001915 *
1916 * res = acl_pass(res);
Willy Tarreaub6866442008-07-14 23:54:42 +02001917 * if (res == ACL_PAT_MISS)
1918 * return 0;
Willy Tarreau11382812008-07-09 16:18:21 +02001919 * if (cond->pol == ACL_COND_UNLESS)
1920 * res = !res;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001921 */
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001922int acl_exec_cond(struct acl_cond *cond, struct proxy *px, struct session *l4, void *l7, unsigned int opt)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001923{
1924 __label__ fetch_next;
1925 struct acl_term_suite *suite;
1926 struct acl_term *term;
1927 struct acl_expr *expr;
1928 struct acl *acl;
1929 struct acl_pattern *pattern;
Willy Tarreau37406352012-04-23 16:16:37 +02001930 struct sample smp;
Willy Tarreau11382812008-07-09 16:18:21 +02001931 int acl_res, suite_res, cond_res;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001932
Willy Tarreau7a777ed2012-04-26 11:44:02 +02001933 /* ACLs are iterated over all values, so let's always set the flag to
1934 * indicate this to the fetch functions.
1935 */
1936 opt |= SMP_OPT_ITERATE;
1937
Willy Tarreau11382812008-07-09 16:18:21 +02001938 /* We're doing a logical OR between conditions so we initialize to FAIL.
1939 * The MISS status is propagated down from the suites.
1940 */
Willy Tarreaua84d3742007-05-07 00:36:48 +02001941 cond_res = ACL_PAT_FAIL;
1942 list_for_each_entry(suite, &cond->suites, list) {
Willy Tarreau11382812008-07-09 16:18:21 +02001943 /* Evaluate condition suite <suite>. We stop at the first term
1944 * which returns ACL_PAT_FAIL. The MISS status is still propagated
1945 * in case of uncertainty in the result.
Willy Tarreaua84d3742007-05-07 00:36:48 +02001946 */
1947
1948 /* we're doing a logical AND between terms, so we must set the
1949 * initial value to PASS.
1950 */
1951 suite_res = ACL_PAT_PASS;
1952 list_for_each_entry(term, &suite->terms, list) {
1953 acl = term->acl;
1954
1955 /* FIXME: use cache !
1956 * check acl->cache_idx for this.
1957 */
1958
1959 /* ACL result not cached. Let's scan all the expressions
1960 * and use the first one to match.
1961 */
1962 acl_res = ACL_PAT_FAIL;
1963 list_for_each_entry(expr, &acl->expr, list) {
Willy Tarreaud41f8d82007-06-10 10:06:18 +02001964 /* we need to reset context and flags */
Willy Tarreau37406352012-04-23 16:16:37 +02001965 memset(&smp, 0, sizeof(smp));
Willy Tarreaua84d3742007-05-07 00:36:48 +02001966 fetch_next:
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001967 if (!expr->kw->fetch(px, l4, l7, opt, expr->args, &smp)) {
Willy Tarreaub6866442008-07-14 23:54:42 +02001968 /* maybe we could not fetch because of missing data */
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001969 if (smp.flags & SMP_F_MAY_CHANGE && !(opt & SMP_OPT_FINAL))
Willy Tarreaub6866442008-07-14 23:54:42 +02001970 acl_res |= ACL_PAT_MISS;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001971 continue;
Willy Tarreaub6866442008-07-14 23:54:42 +02001972 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001973
Willy Tarreau197e10a2012-04-23 19:18:42 +02001974 if (smp.type == SMP_T_BOOL) {
1975 if (smp.data.uint)
Willy Tarreaua79534f2008-07-20 10:13:37 +02001976 acl_res |= ACL_PAT_PASS;
1977 else
1978 acl_res |= ACL_PAT_FAIL;
1979 }
1980 else {
Willy Tarreau020534d2010-05-16 21:45:45 +02001981 if (!eb_is_empty(&expr->pattern_tree)) {
Willy Tarreauc4262962010-05-10 23:42:40 +02001982 /* a tree is present, let's check what type it is */
1983 if (expr->kw->match == acl_match_str)
Willy Tarreau37406352012-04-23 16:16:37 +02001984 acl_res |= acl_lookup_str(&smp, expr) ? ACL_PAT_PASS : ACL_PAT_FAIL;
Willy Tarreaub337b532010-05-13 20:03:41 +02001985 else if (expr->kw->match == acl_match_ip)
Willy Tarreau37406352012-04-23 16:16:37 +02001986 acl_res |= acl_lookup_ip(&smp, expr) ? ACL_PAT_PASS : ACL_PAT_FAIL;
Willy Tarreauc4262962010-05-10 23:42:40 +02001987 }
1988
Willy Tarreaua79534f2008-07-20 10:13:37 +02001989 /* call the match() function for all tests on this value */
1990 list_for_each_entry(pattern, &expr->patterns, list) {
Willy Tarreaua79534f2008-07-20 10:13:37 +02001991 if (acl_res == ACL_PAT_PASS)
1992 break;
Willy Tarreau37406352012-04-23 16:16:37 +02001993 acl_res |= expr->kw->match(&smp, pattern);
Willy Tarreaua79534f2008-07-20 10:13:37 +02001994 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001995 }
1996 /*
Willy Tarreau11382812008-07-09 16:18:21 +02001997 * OK now acl_res holds the result of this expression
1998 * as one of ACL_PAT_FAIL, ACL_PAT_MISS or ACL_PAT_PASS.
Willy Tarreaua84d3742007-05-07 00:36:48 +02001999 *
Willy Tarreau11382812008-07-09 16:18:21 +02002000 * Then if (!MISS) we can cache the result, and put
Willy Tarreau37406352012-04-23 16:16:37 +02002001 * (smp.flags & SMP_F_VOLATILE) in the cache flags.
Willy Tarreaua84d3742007-05-07 00:36:48 +02002002 *
2003 * FIXME: implement cache.
2004 *
2005 */
2006
Willy Tarreau11382812008-07-09 16:18:21 +02002007 /* we're ORing these terms, so a single PASS is enough */
2008 if (acl_res == ACL_PAT_PASS)
Willy Tarreaua84d3742007-05-07 00:36:48 +02002009 break;
2010
Willy Tarreau37406352012-04-23 16:16:37 +02002011 if (smp.flags & SMP_F_NOT_LAST)
Willy Tarreaua84d3742007-05-07 00:36:48 +02002012 goto fetch_next;
Willy Tarreaub6866442008-07-14 23:54:42 +02002013
2014 /* sometimes we know the fetched data is subject to change
2015 * later and give another chance for a new match (eg: request
2016 * size, time, ...)
2017 */
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002018 if (smp.flags & SMP_F_MAY_CHANGE && !(opt & SMP_OPT_FINAL))
Willy Tarreaub6866442008-07-14 23:54:42 +02002019 acl_res |= ACL_PAT_MISS;
Willy Tarreaua84d3742007-05-07 00:36:48 +02002020 }
2021 /*
2022 * Here we have the result of an ACL (cached or not).
2023 * ACLs are combined, negated or not, to form conditions.
2024 */
2025
Willy Tarreaua84d3742007-05-07 00:36:48 +02002026 if (term->neg)
Willy Tarreau11382812008-07-09 16:18:21 +02002027 acl_res = acl_neg(acl_res);
Willy Tarreaua84d3742007-05-07 00:36:48 +02002028
2029 suite_res &= acl_res;
Willy Tarreau11382812008-07-09 16:18:21 +02002030
2031 /* we're ANDing these terms, so a single FAIL is enough */
2032 if (suite_res == ACL_PAT_FAIL)
Willy Tarreaua84d3742007-05-07 00:36:48 +02002033 break;
2034 }
2035 cond_res |= suite_res;
Willy Tarreau11382812008-07-09 16:18:21 +02002036
2037 /* we're ORing these terms, so a single PASS is enough */
2038 if (cond_res == ACL_PAT_PASS)
Willy Tarreaua84d3742007-05-07 00:36:48 +02002039 break;
2040 }
Willy Tarreau11382812008-07-09 16:18:21 +02002041 return cond_res;
Willy Tarreaua84d3742007-05-07 00:36:48 +02002042}
2043
2044
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02002045/* Reports a pointer to the first ACL used in condition <cond> which requires
2046 * at least one of the USE_FLAGS in <require>. Returns NULL if none matches.
2047 * The construct is almost the same as for acl_exec_cond() since we're walking
2048 * down the ACL tree as well. It is important that the tree is really walked
2049 * through and never cached, because that way, this function can be used as a
2050 * late check.
2051 */
Willy Tarreauf1e98b82010-01-28 17:59:39 +01002052struct acl *cond_find_require(const struct acl_cond *cond, unsigned int require)
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02002053{
2054 struct acl_term_suite *suite;
2055 struct acl_term *term;
2056 struct acl *acl;
2057
2058 list_for_each_entry(suite, &cond->suites, list) {
2059 list_for_each_entry(term, &suite->terms, list) {
2060 acl = term->acl;
2061 if (acl->requires & require)
2062 return acl;
2063 }
2064 }
2065 return NULL;
2066}
2067
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01002068/*
2069 * Find targets for userlist and groups in acl. Function returns the number
2070 * of errors or OK if everything is fine.
2071 */
2072int
2073acl_find_targets(struct proxy *p)
2074{
2075
2076 struct acl *acl;
2077 struct acl_expr *expr;
2078 struct acl_pattern *pattern;
2079 struct userlist *ul;
Willy Tarreau63364ee2012-04-19 19:11:13 +02002080 struct arg *arg;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01002081 int cfgerr = 0;
2082
2083 list_for_each_entry(acl, &p->acl, list) {
2084 list_for_each_entry(expr, &acl->expr, list) {
Willy Tarreau2e845be2012-10-19 19:49:09 +02002085 for (arg = expr->args; arg && arg->type != ARGT_STOP; arg++) {
2086 if (!arg->unresolved)
Willy Tarreau496aa012012-06-01 10:38:29 +02002087 continue;
Willy Tarreau63364ee2012-04-19 19:11:13 +02002088 else if (arg->type == ARGT_SRV) {
2089 struct proxy *px;
2090 struct server *srv;
2091 char *pname, *sname;
Willy Tarreau0b1cd942010-05-16 22:18:27 +02002092
Willy Tarreau7d1df412012-11-23 23:47:36 +01002093 if (!arg->data.str.len) {
Willy Tarreau63364ee2012-04-19 19:11:13 +02002094 Alert("proxy %s: acl '%s' %s(): missing server name.\n",
2095 p->id, acl->name, expr->kw->kw);
2096 cfgerr++;
2097 continue;
2098 }
Willy Tarreau0b1cd942010-05-16 22:18:27 +02002099
Willy Tarreau7d1df412012-11-23 23:47:36 +01002100 pname = arg->data.str.str;
Willy Tarreau63364ee2012-04-19 19:11:13 +02002101 sname = strrchr(pname, '/');
Willy Tarreau0b1cd942010-05-16 22:18:27 +02002102
Willy Tarreau63364ee2012-04-19 19:11:13 +02002103 if (sname)
2104 *sname++ = '\0';
2105 else {
2106 sname = pname;
2107 pname = NULL;
2108 }
2109
2110 px = p;
2111 if (pname) {
2112 px = findproxy(pname, PR_CAP_BE);
2113 if (!px) {
2114 Alert("proxy %s: acl '%s' %s(): unable to find proxy '%s'.\n",
2115 p->id, acl->name, expr->kw->kw, pname);
2116 cfgerr++;
2117 continue;
2118 }
2119 }
Willy Tarreau0b1cd942010-05-16 22:18:27 +02002120
Willy Tarreau63364ee2012-04-19 19:11:13 +02002121 srv = findserver(px, sname);
2122 if (!srv) {
2123 Alert("proxy %s: acl '%s' %s(): unable to find server '%s'.\n",
2124 p->id, acl->name, expr->kw->kw, sname);
Willy Tarreau0b1cd942010-05-16 22:18:27 +02002125 cfgerr++;
2126 continue;
2127 }
Willy Tarreau0b1cd942010-05-16 22:18:27 +02002128
Willy Tarreau7d1df412012-11-23 23:47:36 +01002129 free(arg->data.str.str);
2130 arg->data.str.str = NULL;
Willy Tarreau496aa012012-06-01 10:38:29 +02002131 arg->unresolved = 0;
Willy Tarreau7d1df412012-11-23 23:47:36 +01002132 arg->data.srv = srv;
Willy Tarreaud28c3532012-04-19 19:28:33 +02002133 }
2134 else if (arg->type == ARGT_FE) {
Willy Tarreaufc2c1fd2012-04-19 23:35:54 +02002135 struct proxy *prx = p;
2136 char *pname = p->id;
Willy Tarreaud28c3532012-04-19 19:28:33 +02002137
Willy Tarreau7d1df412012-11-23 23:47:36 +01002138 if (arg->data.str.len) {
2139 pname = arg->data.str.str;
Willy Tarreaufc2c1fd2012-04-19 23:35:54 +02002140 prx = findproxy(pname, PR_CAP_FE);
Willy Tarreaud28c3532012-04-19 19:28:33 +02002141 }
2142
Willy Tarreaud28c3532012-04-19 19:28:33 +02002143 if (!prx) {
2144 Alert("proxy %s: acl '%s' %s(): unable to find frontend '%s'.\n",
2145 p->id, acl->name, expr->kw->kw, pname);
2146 cfgerr++;
2147 continue;
2148 }
2149
Willy Tarreaufc2c1fd2012-04-19 23:35:54 +02002150 if (!(prx->cap & PR_CAP_FE)) {
2151 Alert("proxy %s: acl '%s' %s(): proxy '%s' has no frontend capability.\n",
2152 p->id, acl->name, expr->kw->kw, pname);
2153 cfgerr++;
2154 continue;
2155 }
2156
Willy Tarreau7d1df412012-11-23 23:47:36 +01002157 free(arg->data.str.str);
2158 arg->data.str.str = NULL;
Willy Tarreau496aa012012-06-01 10:38:29 +02002159 arg->unresolved = 0;
Willy Tarreau7d1df412012-11-23 23:47:36 +01002160 arg->data.prx = prx;
Willy Tarreaud28c3532012-04-19 19:28:33 +02002161 }
2162 else if (arg->type == ARGT_BE) {
Willy Tarreaufc2c1fd2012-04-19 23:35:54 +02002163 struct proxy *prx = p;
2164 char *pname = p->id;
Willy Tarreaud28c3532012-04-19 19:28:33 +02002165
Willy Tarreau7d1df412012-11-23 23:47:36 +01002166 if (arg->data.str.len) {
2167 pname = arg->data.str.str;
Willy Tarreaufc2c1fd2012-04-19 23:35:54 +02002168 prx = findproxy(pname, PR_CAP_BE);
Willy Tarreaud28c3532012-04-19 19:28:33 +02002169 }
2170
Willy Tarreaud28c3532012-04-19 19:28:33 +02002171 if (!prx) {
2172 Alert("proxy %s: acl '%s' %s(): unable to find backend '%s'.\n",
2173 p->id, acl->name, expr->kw->kw, pname);
2174 cfgerr++;
2175 continue;
2176 }
2177
Willy Tarreaufc2c1fd2012-04-19 23:35:54 +02002178 if (!(prx->cap & PR_CAP_BE)) {
2179 Alert("proxy %s: acl '%s' %s(): proxy '%s' has no backend capability.\n",
2180 p->id, acl->name, expr->kw->kw, pname);
2181 cfgerr++;
2182 continue;
2183 }
2184
Willy Tarreau7d1df412012-11-23 23:47:36 +01002185 free(arg->data.str.str);
2186 arg->data.str.str = NULL;
Willy Tarreau496aa012012-06-01 10:38:29 +02002187 arg->unresolved = 0;
Willy Tarreau7d1df412012-11-23 23:47:36 +01002188 arg->data.prx = prx;
Willy Tarreaud28c3532012-04-19 19:28:33 +02002189 }
2190 else if (arg->type == ARGT_TAB) {
Willy Tarreaufc2c1fd2012-04-19 23:35:54 +02002191 struct proxy *prx = p;
2192 char *pname = p->id;
Willy Tarreaud28c3532012-04-19 19:28:33 +02002193
Willy Tarreau7d1df412012-11-23 23:47:36 +01002194 if (arg->data.str.len) {
2195 pname = arg->data.str.str;
Willy Tarreaufc2c1fd2012-04-19 23:35:54 +02002196 prx = find_stktable(pname);
Willy Tarreaud28c3532012-04-19 19:28:33 +02002197 }
2198
Willy Tarreaud28c3532012-04-19 19:28:33 +02002199 if (!prx) {
2200 Alert("proxy %s: acl '%s' %s(): unable to find table '%s'.\n",
2201 p->id, acl->name, expr->kw->kw, pname);
2202 cfgerr++;
2203 continue;
2204 }
2205
Willy Tarreaufc2c1fd2012-04-19 23:35:54 +02002206
2207 if (!prx->table.size) {
2208 Alert("proxy %s: acl '%s' %s(): no table in proxy '%s'.\n",
2209 p->id, acl->name, expr->kw->kw, pname);
2210 cfgerr++;
2211 continue;
2212 }
2213
Willy Tarreau7d1df412012-11-23 23:47:36 +01002214 free(arg->data.str.str);
2215 arg->data.str.str = NULL;
Willy Tarreau496aa012012-06-01 10:38:29 +02002216 arg->unresolved = 0;
Willy Tarreau7d1df412012-11-23 23:47:36 +01002217 arg->data.prx = prx;
Willy Tarreau0b1cd942010-05-16 22:18:27 +02002218 }
Willy Tarreau63364ee2012-04-19 19:11:13 +02002219 else if (arg->type == ARGT_USR) {
Willy Tarreau7d1df412012-11-23 23:47:36 +01002220 if (!arg->data.str.len) {
Willy Tarreau63364ee2012-04-19 19:11:13 +02002221 Alert("proxy %s: acl '%s' %s(): missing userlist name.\n",
2222 p->id, acl->name, expr->kw->kw);
2223 cfgerr++;
2224 continue;
2225 }
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01002226
Willy Tarreau63364ee2012-04-19 19:11:13 +02002227 if (p->uri_auth && p->uri_auth->userlist &&
Willy Tarreau7d1df412012-11-23 23:47:36 +01002228 !strcmp(p->uri_auth->userlist->name, arg->data.str.str))
Willy Tarreau63364ee2012-04-19 19:11:13 +02002229 ul = p->uri_auth->userlist;
2230 else
Willy Tarreau7d1df412012-11-23 23:47:36 +01002231 ul = auth_find_userlist(arg->data.str.str);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01002232
Willy Tarreau63364ee2012-04-19 19:11:13 +02002233 if (!ul) {
2234 Alert("proxy %s: acl '%s' %s(%s): unable to find userlist.\n",
Willy Tarreau7d1df412012-11-23 23:47:36 +01002235 p->id, acl->name, expr->kw->kw, arg->data.str.str);
Willy Tarreau63364ee2012-04-19 19:11:13 +02002236 cfgerr++;
2237 continue;
2238 }
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01002239
Willy Tarreau7d1df412012-11-23 23:47:36 +01002240 free(arg->data.str.str);
2241 arg->data.str.str = NULL;
Willy Tarreau496aa012012-06-01 10:38:29 +02002242 arg->unresolved = 0;
Willy Tarreau7d1df412012-11-23 23:47:36 +01002243 arg->data.usr = ul;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01002244 }
Willy Tarreau63364ee2012-04-19 19:11:13 +02002245 } /* end of args processing */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01002246
Willy Tarreau46b39d02012-05-10 23:40:14 +02002247 /* don't try to resolve groups if we're not certain of having
2248 * resolved userlists first.
2249 */
2250 if (cfgerr)
2251 break;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01002252
2253 if (!strcmp(expr->kw->kw, "http_auth_group")) {
Willy Tarreau63364ee2012-04-19 19:11:13 +02002254 /* note: argument resolved above thanks to ARGT_USR */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01002255
2256 if (LIST_ISEMPTY(&expr->patterns)) {
2257 Alert("proxy %s: acl %s %s(): no groups specified.\n",
2258 p->id, acl->name, expr->kw->kw);
2259 cfgerr++;
2260 continue;
2261 }
2262
2263 list_for_each_entry(pattern, &expr->patterns, list) {
Willy Tarreau7d1df412012-11-23 23:47:36 +01002264 /* this keyword only has one argument */
Willy Tarreau34db1082012-04-19 17:16:54 +02002265 pattern->val.group_mask = auth_resolve_groups(expr->args->data.usr, pattern->ptr.str);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01002266
2267 free(pattern->ptr.str);
2268 pattern->ptr.str = NULL;
2269 pattern->len = 0;
2270
2271 if (!pattern->val.group_mask) {
2272 Alert("proxy %s: acl %s %s(): invalid group(s).\n",
2273 p->id, acl->name, expr->kw->kw);
2274 cfgerr++;
2275 continue;
2276 }
2277 }
2278 }
2279 }
2280 }
2281
2282 return cfgerr;
2283}
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02002284
Willy Tarreaua84d3742007-05-07 00:36:48 +02002285/************************************************************************/
2286/* All supported keywords must be declared here. */
2287/************************************************************************/
2288
Willy Tarreau61612d42012-04-19 18:42:05 +02002289/* Note: must not be declared <const> as its list will be overwritten.
2290 * Please take care of keeping this list alphabetically sorted.
2291 */
Willy Tarreaua84d3742007-05-07 00:36:48 +02002292static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau61612d42012-04-19 18:42:05 +02002293 { "always_false", acl_parse_nothing, acl_fetch_false, acl_match_nothing, ACL_USE_NOTHING, 0 },
2294 { "always_true", acl_parse_nothing, acl_fetch_true, acl_match_nothing, ACL_USE_NOTHING, 0 },
2295 { "rep_ssl_hello_type", acl_parse_int, acl_fetch_ssl_hello_type, acl_match_int, ACL_USE_L6RTR_VOLATILE, 0 },
2296 { "req_len", acl_parse_int, acl_fetch_req_len, acl_match_int, ACL_USE_L6REQ_VOLATILE, 0 },
Willy Tarreau61612d42012-04-19 18:42:05 +02002297 { "req_ssl_hello_type", acl_parse_int, acl_fetch_ssl_hello_type, acl_match_int, ACL_USE_L6REQ_VOLATILE, 0 },
2298 { "req_ssl_sni", acl_parse_str, acl_fetch_ssl_hello_sni, acl_match_str, ACL_USE_L6REQ_VOLATILE|ACL_MAY_LOOKUP, 0 },
2299 { "req_ssl_ver", acl_parse_dotted_ver, acl_fetch_req_ssl_ver, acl_match_int, ACL_USE_L6REQ_VOLATILE, 0 },
2300 { "wait_end", acl_parse_nothing, acl_fetch_wait_end, acl_match_nothing, ACL_USE_NOTHING, 0 },
Willy Tarreaua84d3742007-05-07 00:36:48 +02002301 { NULL, NULL, NULL, NULL }
2302}};
2303
2304
2305__attribute__((constructor))
2306static void __acl_init(void)
2307{
2308 acl_register_keywords(&acl_kws);
2309}
2310
2311
2312/*
2313 * Local variables:
2314 * c-indent-level: 8
2315 * c-basic-offset: 8
2316 * End:
2317 */