blob: f220394d9606492e76c852c986a66aa5abd5af9c [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
Willy Tarreauc4262962010-05-10 23:42:40 +0200495/* Lookup a string in the expression's pattern tree. The node is returned if it
496 * exists, otherwise NULL.
497 */
Willy Tarreau37406352012-04-23 16:16:37 +0200498static void *acl_lookup_str(struct sample *smp, struct acl_expr *expr)
Willy Tarreauc4262962010-05-10 23:42:40 +0200499{
500 /* data are stored in a tree */
501 struct ebmb_node *node;
502 char prev;
503
504 /* we may have to force a trailing zero on the test pattern */
Willy Tarreauf853c462012-04-23 18:53:56 +0200505 prev = smp->data.str.str[smp->data.str.len];
Willy Tarreauc4262962010-05-10 23:42:40 +0200506 if (prev)
Willy Tarreauf853c462012-04-23 18:53:56 +0200507 smp->data.str.str[smp->data.str.len] = '\0';
508 node = ebst_lookup(&expr->pattern_tree, smp->data.str.str);
Willy Tarreauc4262962010-05-10 23:42:40 +0200509 if (prev)
Willy Tarreauf853c462012-04-23 18:53:56 +0200510 smp->data.str.str[smp->data.str.len] = prev;
Willy Tarreauc4262962010-05-10 23:42:40 +0200511 return node;
512}
513
Willy Tarreau21e5b0e2012-04-23 19:25:44 +0200514/* Executes a regex. It temporarily changes the data to add a trailing zero,
515 * and restores the previous character when leaving.
Willy Tarreauf3d25982007-05-08 22:45:09 +0200516 */
Willy Tarreau37406352012-04-23 16:16:37 +0200517int acl_match_reg(struct sample *smp, struct acl_pattern *pattern)
Willy Tarreauf3d25982007-05-08 22:45:09 +0200518{
519 char old_char;
520 int ret;
521
Willy Tarreauf853c462012-04-23 18:53:56 +0200522 old_char = smp->data.str.str[smp->data.str.len];
523 smp->data.str.str[smp->data.str.len] = 0;
Willy Tarreauf3d25982007-05-08 22:45:09 +0200524
Willy Tarreauf853c462012-04-23 18:53:56 +0200525 if (regexec(pattern->ptr.reg, smp->data.str.str, 0, NULL, 0) == 0)
Willy Tarreau11382812008-07-09 16:18:21 +0200526 ret = ACL_PAT_PASS;
Willy Tarreauf3d25982007-05-08 22:45:09 +0200527 else
Willy Tarreau11382812008-07-09 16:18:21 +0200528 ret = ACL_PAT_FAIL;
Willy Tarreauf3d25982007-05-08 22:45:09 +0200529
Willy Tarreauf853c462012-04-23 18:53:56 +0200530 smp->data.str.str[smp->data.str.len] = old_char;
Willy Tarreauf3d25982007-05-08 22:45:09 +0200531 return ret;
532}
533
Willy Tarreaua84d3742007-05-07 00:36:48 +0200534/* Checks that the pattern matches the beginning of the tested string. */
Willy Tarreau37406352012-04-23 16:16:37 +0200535int acl_match_beg(struct sample *smp, struct acl_pattern *pattern)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200536{
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200537 int icase;
538
Willy Tarreauf853c462012-04-23 18:53:56 +0200539 if (pattern->len > smp->data.str.len)
Willy Tarreau11382812008-07-09 16:18:21 +0200540 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200541
542 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
Willy Tarreauf853c462012-04-23 18:53:56 +0200543 if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str, pattern->len) != 0) ||
544 (!icase && strncmp(pattern->ptr.str, smp->data.str.str, pattern->len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +0200545 return ACL_PAT_FAIL;
546 return ACL_PAT_PASS;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200547}
548
549/* Checks that the pattern matches the end of the tested string. */
Willy Tarreau37406352012-04-23 16:16:37 +0200550int acl_match_end(struct sample *smp, struct acl_pattern *pattern)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200551{
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200552 int icase;
553
Willy Tarreauf853c462012-04-23 18:53:56 +0200554 if (pattern->len > smp->data.str.len)
Willy Tarreau11382812008-07-09 16:18:21 +0200555 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200556 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
Willy Tarreauf853c462012-04-23 18:53:56 +0200557 if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str + smp->data.str.len - pattern->len, pattern->len) != 0) ||
558 (!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 +0200559 return ACL_PAT_FAIL;
560 return ACL_PAT_PASS;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200561}
562
563/* Checks that the pattern is included inside the tested string.
564 * NB: Suboptimal, should be rewritten using a Boyer-Moore method.
565 */
Willy Tarreau37406352012-04-23 16:16:37 +0200566int acl_match_sub(struct sample *smp, struct acl_pattern *pattern)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200567{
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200568 int icase;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200569 char *end;
570 char *c;
571
Willy Tarreauf853c462012-04-23 18:53:56 +0200572 if (pattern->len > smp->data.str.len)
Willy Tarreau11382812008-07-09 16:18:21 +0200573 return ACL_PAT_FAIL;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200574
Willy Tarreauf853c462012-04-23 18:53:56 +0200575 end = smp->data.str.str + smp->data.str.len - pattern->len;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200576 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
577 if (icase) {
Willy Tarreauf853c462012-04-23 18:53:56 +0200578 for (c = smp->data.str.str; c <= end; c++) {
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200579 if (tolower(*c) != tolower(*pattern->ptr.str))
580 continue;
581 if (strncasecmp(pattern->ptr.str, c, pattern->len) == 0)
Willy Tarreau11382812008-07-09 16:18:21 +0200582 return ACL_PAT_PASS;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200583 }
584 } else {
Willy Tarreauf853c462012-04-23 18:53:56 +0200585 for (c = smp->data.str.str; c <= end; c++) {
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200586 if (*c != *pattern->ptr.str)
587 continue;
588 if (strncmp(pattern->ptr.str, c, pattern->len) == 0)
Willy Tarreau11382812008-07-09 16:18:21 +0200589 return ACL_PAT_PASS;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200590 }
Willy Tarreaua84d3742007-05-07 00:36:48 +0200591 }
Willy Tarreau11382812008-07-09 16:18:21 +0200592 return ACL_PAT_FAIL;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200593}
594
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200595/* Background: Fast way to find a zero byte in a word
596 * http://graphics.stanford.edu/~seander/bithacks.html#ZeroInWord
597 * hasZeroByte = (v - 0x01010101UL) & ~v & 0x80808080UL;
598 *
599 * To look for 4 different byte values, xor the word with those bytes and
600 * then check for zero bytes:
601 *
602 * v = (((unsigned char)c * 0x1010101U) ^ delimiter)
603 * where <delimiter> is the 4 byte values to look for (as an uint)
604 * and <c> is the character that is being tested
605 */
606static inline unsigned int is_delimiter(unsigned char c, unsigned int mask)
607{
608 mask ^= (c * 0x01010101); /* propagate the char to all 4 bytes */
609 return (mask - 0x01010101) & ~mask & 0x80808080U;
610}
611
612static inline unsigned int make_4delim(unsigned char d1, unsigned char d2, unsigned char d3, unsigned char d4)
613{
614 return d1 << 24 | d2 << 16 | d3 << 8 | d4;
615}
616
Willy Tarreaua84d3742007-05-07 00:36:48 +0200617/* This one is used by other real functions. It checks that the pattern is
618 * included inside the tested string, but enclosed between the specified
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200619 * delimiters or at the beginning or end of the string. The delimiters are
620 * provided as an unsigned int made by make_4delim() and match up to 4 different
621 * delimiters. Delimiters are stripped at the beginning and end of the pattern.
Willy Tarreaua84d3742007-05-07 00:36:48 +0200622 */
Willy Tarreau37406352012-04-23 16:16:37 +0200623static int match_word(struct sample *smp, struct acl_pattern *pattern, unsigned int delimiters)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200624{
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200625 int may_match, icase;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200626 char *c, *end;
627 char *ps;
628 int pl;
629
630 pl = pattern->len;
631 ps = pattern->ptr.str;
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200632
633 while (pl > 0 && is_delimiter(*ps, delimiters)) {
Willy Tarreaua84d3742007-05-07 00:36:48 +0200634 pl--;
635 ps++;
636 }
637
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200638 while (pl > 0 && is_delimiter(ps[pl - 1], delimiters))
Willy Tarreaua84d3742007-05-07 00:36:48 +0200639 pl--;
640
Willy Tarreauf853c462012-04-23 18:53:56 +0200641 if (pl > smp->data.str.len)
Willy Tarreau11382812008-07-09 16:18:21 +0200642 return ACL_PAT_FAIL;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200643
644 may_match = 1;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200645 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
Willy Tarreauf853c462012-04-23 18:53:56 +0200646 end = smp->data.str.str + smp->data.str.len - pl;
647 for (c = smp->data.str.str; c <= end; c++) {
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200648 if (is_delimiter(*c, delimiters)) {
Willy Tarreaua84d3742007-05-07 00:36:48 +0200649 may_match = 1;
650 continue;
651 }
Willy Tarreaua84d3742007-05-07 00:36:48 +0200652
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200653 if (!may_match)
654 continue;
655
656 if (icase) {
657 if ((tolower(*c) == tolower(*ps)) &&
658 (strncasecmp(ps, c, pl) == 0) &&
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200659 (c == end || is_delimiter(c[pl], delimiters)))
Willy Tarreau11382812008-07-09 16:18:21 +0200660 return ACL_PAT_PASS;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200661 } else {
662 if ((*c == *ps) &&
663 (strncmp(ps, c, pl) == 0) &&
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200664 (c == end || is_delimiter(c[pl], delimiters)))
Willy Tarreau11382812008-07-09 16:18:21 +0200665 return ACL_PAT_PASS;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200666 }
Willy Tarreaua84d3742007-05-07 00:36:48 +0200667 may_match = 0;
668 }
Willy Tarreau11382812008-07-09 16:18:21 +0200669 return ACL_PAT_FAIL;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200670}
671
672/* Checks that the pattern is included inside the tested string, but enclosed
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200673 * between the delimiters '?' or '/' or at the beginning or end of the string.
674 * Delimiters at the beginning or end of the pattern are ignored.
Willy Tarreaua84d3742007-05-07 00:36:48 +0200675 */
Willy Tarreau37406352012-04-23 16:16:37 +0200676int acl_match_dir(struct sample *smp, struct acl_pattern *pattern)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200677{
Willy Tarreau37406352012-04-23 16:16:37 +0200678 return match_word(smp, pattern, make_4delim('/', '?', '?', '?'));
Willy Tarreaua84d3742007-05-07 00:36:48 +0200679}
680
681/* Checks that the pattern is included inside the tested string, but enclosed
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200682 * between the delmiters '/', '?', '.' or ":" or at the beginning or end of
683 * the string. Delimiters at the beginning or end of the pattern are ignored.
Willy Tarreaua84d3742007-05-07 00:36:48 +0200684 */
Willy Tarreau37406352012-04-23 16:16:37 +0200685int acl_match_dom(struct sample *smp, struct acl_pattern *pattern)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200686{
Willy Tarreau37406352012-04-23 16:16:37 +0200687 return match_word(smp, pattern, make_4delim('/', '?', '.', ':'));
Willy Tarreaua84d3742007-05-07 00:36:48 +0200688}
689
690/* Checks that the integer in <test> is included between min and max */
Willy Tarreau37406352012-04-23 16:16:37 +0200691int acl_match_int(struct sample *smp, struct acl_pattern *pattern)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200692{
Willy Tarreauf853c462012-04-23 18:53:56 +0200693 if ((!pattern->val.range.min_set || pattern->val.range.min <= smp->data.uint) &&
694 (!pattern->val.range.max_set || smp->data.uint <= pattern->val.range.max))
Willy Tarreau11382812008-07-09 16:18:21 +0200695 return ACL_PAT_PASS;
696 return ACL_PAT_FAIL;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200697}
698
Willy Tarreau0e698542011-09-16 08:32:32 +0200699/* Checks that the length of the pattern in <test> is included between min and max */
Willy Tarreau37406352012-04-23 16:16:37 +0200700int acl_match_len(struct sample *smp, struct acl_pattern *pattern)
Willy Tarreau0e698542011-09-16 08:32:32 +0200701{
Willy Tarreauf853c462012-04-23 18:53:56 +0200702 if ((!pattern->val.range.min_set || pattern->val.range.min <= smp->data.str.len) &&
703 (!pattern->val.range.max_set || smp->data.str.len <= pattern->val.range.max))
Willy Tarreau0e698542011-09-16 08:32:32 +0200704 return ACL_PAT_PASS;
705 return ACL_PAT_FAIL;
706}
707
Willy Tarreau37406352012-04-23 16:16:37 +0200708int acl_match_ip(struct sample *smp, struct acl_pattern *pattern)
Willy Tarreaua67fad92007-05-08 19:50:09 +0200709{
Willy Tarreauceb4ac92012-04-28 00:41:46 +0200710 unsigned int v4; /* in network byte order */
711 struct in6_addr *v6;
712 int bits, pos;
713 struct in6_addr tmp6;
Willy Tarreaua67fad92007-05-08 19:50:09 +0200714
Willy Tarreauceb4ac92012-04-28 00:41:46 +0200715 if (pattern->type == SMP_T_IPV4) {
716 if (smp->type == SMP_T_IPV4) {
717 v4 = smp->data.ipv4.s_addr;
718 }
719 else if (smp->type == SMP_T_IPV6) {
720 /* v4 match on a V6 sample. We want to check at least for
721 * the following forms :
722 * - ::ffff:ip:v4 (ipv4 mapped)
723 * - ::0000:ip:v4 (old ipv4 mapped)
724 * - 2002:ip:v4:: (6to4)
725 */
726 if (*(uint32_t*)&smp->data.ipv6.s6_addr[0] == 0 &&
727 *(uint32_t*)&smp->data.ipv6.s6_addr[4] == 0 &&
728 (*(uint32_t*)&smp->data.ipv6.s6_addr[8] == 0 ||
729 *(uint32_t*)&smp->data.ipv6.s6_addr[8] == htonl(0xFFFF))) {
730 v4 = *(uint32_t*)&smp->data.ipv6.s6_addr[12];
731 }
732 else if (*(uint16_t*)&smp->data.ipv6.s6_addr[0] == htons(0x2002)) {
733 v4 = htonl((ntohs(*(uint16_t*)&smp->data.ipv6.s6_addr[2]) << 16) +
734 ntohs(*(uint16_t*)&smp->data.ipv6.s6_addr[4]));
735 }
736 else
737 return ACL_PAT_FAIL;
738 }
739 else
740 return ACL_PAT_FAIL;
Willy Tarreaua67fad92007-05-08 19:50:09 +0200741
Willy Tarreauceb4ac92012-04-28 00:41:46 +0200742 if (((v4 ^ pattern->val.ipv4.addr.s_addr) & pattern->val.ipv4.mask.s_addr) == 0)
743 return ACL_PAT_PASS;
744 else
745 return ACL_PAT_FAIL;
746 }
747 else if (pattern->type == SMP_T_IPV6) {
748 if (smp->type == SMP_T_IPV4) {
749 /* Convert the IPv4 sample address to IPv4 with the
750 * mapping method using the ::ffff: prefix.
751 */
752 memset(&tmp6, 0, 10);
753 *(uint16_t*)&tmp6.s6_addr[10] = htons(0xffff);
754 *(uint32_t*)&tmp6.s6_addr[12] = smp->data.ipv4.s_addr;
755 v6 = &tmp6;
756 }
757 else if (smp->type == SMP_T_IPV6) {
758 v6 = &smp->data.ipv6;
759 }
760 else {
761 return ACL_PAT_FAIL;
762 }
763
764 bits = pattern->val.ipv6.mask;
765 for (pos = 0; bits > 0; pos += 4, bits -= 32) {
766 v4 = *(uint32_t*)&v6->s6_addr[pos] ^ *(uint32_t*)&pattern->val.ipv6.addr.s6_addr[pos];
767 if (bits < 32)
768 v4 &= (~0U) << (32-bits);
769 if (v4)
770 return ACL_PAT_FAIL;
771 }
Willy Tarreau11382812008-07-09 16:18:21 +0200772 return ACL_PAT_PASS;
Willy Tarreauceb4ac92012-04-28 00:41:46 +0200773 }
Willy Tarreau11382812008-07-09 16:18:21 +0200774 return ACL_PAT_FAIL;
Willy Tarreaua67fad92007-05-08 19:50:09 +0200775}
776
Willy Tarreaub337b532010-05-13 20:03:41 +0200777/* Lookup an IPv4 address in the expression's pattern tree using the longest
778 * match method. The node is returned if it exists, otherwise NULL.
779 */
Willy Tarreau37406352012-04-23 16:16:37 +0200780static void *acl_lookup_ip(struct sample *smp, struct acl_expr *expr)
Willy Tarreaub337b532010-05-13 20:03:41 +0200781{
782 struct in_addr *s;
783
Willy Tarreauf853c462012-04-23 18:53:56 +0200784 if (smp->type != SMP_T_IPV4)
Willy Tarreaub337b532010-05-13 20:03:41 +0200785 return ACL_PAT_FAIL;
786
Willy Tarreauf853c462012-04-23 18:53:56 +0200787 s = &smp->data.ipv4;
Willy Tarreaub337b532010-05-13 20:03:41 +0200788 return ebmb_lookup_longest(&expr->pattern_tree, &s->s_addr);
789}
790
Willy Tarreaua84d3742007-05-07 00:36:48 +0200791/* Parse a string. It is allocated and duplicated. */
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200792int acl_parse_str(const char **text, struct acl_pattern *pattern, int *opaque, char **err)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200793{
794 int len;
795
Willy Tarreauae8b7962007-06-09 23:10:04 +0200796 len = strlen(*text);
Willy Tarreauc92ddbc2012-04-27 22:10:57 +0200797 pattern->type = SMP_T_CSTR;
Willy Tarreauc4262962010-05-10 23:42:40 +0200798
799 if (pattern->flags & ACL_PAT_F_TREE_OK) {
800 /* we're allowed to put the data in a tree whose root is pointed
801 * to by val.tree.
802 */
803 struct ebmb_node *node;
804
805 node = calloc(1, sizeof(*node) + len + 1);
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200806 if (!node) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200807 memprintf(err, "out of memory while loading string pattern");
Willy Tarreauc4262962010-05-10 23:42:40 +0200808 return 0;
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200809 }
Willy Tarreauc4262962010-05-10 23:42:40 +0200810 memcpy(node->key, *text, len + 1);
811 if (ebst_insert(pattern->val.tree, node) != node)
812 free(node); /* was a duplicate */
813 pattern->flags |= ACL_PAT_F_TREE; /* this pattern now contains a tree */
814 return 1;
815 }
816
Willy Tarreauae8b7962007-06-09 23:10:04 +0200817 pattern->ptr.str = strdup(*text);
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200818 if (!pattern->ptr.str) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200819 memprintf(err, "out of memory while loading string pattern");
Willy Tarreaua84d3742007-05-07 00:36:48 +0200820 return 0;
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200821 }
Willy Tarreaua84d3742007-05-07 00:36:48 +0200822 pattern->len = len;
823 return 1;
824}
825
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +0100826/* Parse and concatenate all further strings into one. */
827int
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200828acl_parse_strcat(const char **text, struct acl_pattern *pattern, int *opaque, char **err)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +0100829{
830
831 int len = 0, i;
832 char *s;
833
834 for (i = 0; *text[i]; i++)
835 len += strlen(text[i])+1;
836
Willy Tarreauc92ddbc2012-04-27 22:10:57 +0200837 pattern->type = SMP_T_CSTR;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +0100838 pattern->ptr.str = s = calloc(1, len);
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200839 if (!pattern->ptr.str) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200840 memprintf(err, "out of memory while loading pattern");
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +0100841 return 0;
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200842 }
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +0100843
844 for (i = 0; *text[i]; i++)
845 s += sprintf(s, i?" %s":"%s", text[i]);
846
847 pattern->len = len;
848
849 return i;
850}
851
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +0200852/* Free data allocated by acl_parse_reg */
Willy Tarreau37406352012-04-23 16:16:37 +0200853static void acl_free_reg(void *ptr)
854{
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +0200855 regfree((regex_t *)ptr);
856}
857
Willy Tarreauf3d25982007-05-08 22:45:09 +0200858/* Parse a regex. It is allocated. */
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200859int acl_parse_reg(const char **text, struct acl_pattern *pattern, int *opaque, char **err)
Willy Tarreauf3d25982007-05-08 22:45:09 +0200860{
861 regex_t *preg;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200862 int icase;
Willy Tarreauf3d25982007-05-08 22:45:09 +0200863
864 preg = calloc(1, sizeof(regex_t));
865
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200866 if (!preg) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200867 memprintf(err, "out of memory while loading pattern");
Willy Tarreauf3d25982007-05-08 22:45:09 +0200868 return 0;
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200869 }
Willy Tarreauf3d25982007-05-08 22:45:09 +0200870
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200871 icase = (pattern->flags & ACL_PAT_F_IGNORE_CASE) ? REG_ICASE : 0;
872 if (regcomp(preg, *text, REG_EXTENDED | REG_NOSUB | icase) != 0) {
Willy Tarreauf3d25982007-05-08 22:45:09 +0200873 free(preg);
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200874 memprintf(err, "regex '%s' is invalid", *text);
Willy Tarreauf3d25982007-05-08 22:45:09 +0200875 return 0;
876 }
877
878 pattern->ptr.reg = preg;
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +0200879 pattern->freeptrbuf = &acl_free_reg;
Willy Tarreauf3d25982007-05-08 22:45:09 +0200880 return 1;
881}
882
Willy Tarreauae8b7962007-06-09 23:10:04 +0200883/* Parse a range of positive integers delimited by either ':' or '-'. If only
884 * one integer is read, it is set as both min and max. An operator may be
885 * specified as the prefix, among this list of 5 :
886 *
887 * 0:eq, 1:gt, 2:ge, 3:lt, 4:le
888 *
889 * The default operator is "eq". It supports range matching. Ranges are
890 * rejected for other operators. The operator may be changed at any time.
891 * The operator is stored in the 'opaque' argument.
892 *
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200893 * If err is non-NULL, an error message will be returned there on errors and
894 * the caller will have to free it.
895 *
Willy Tarreaua84d3742007-05-07 00:36:48 +0200896 */
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200897int acl_parse_int(const char **text, struct acl_pattern *pattern, int *opaque, char **err)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200898{
Willy Tarreauae8b7962007-06-09 23:10:04 +0200899 signed long long i;
900 unsigned int j, last, skip = 0;
901 const char *ptr = *text;
902
Willy Tarreauc92ddbc2012-04-27 22:10:57 +0200903 pattern->type = SMP_T_UINT;
Willy Tarreau8f8e6452007-06-17 21:51:38 +0200904 while (!isdigit((unsigned char)*ptr)) {
Willy Tarreau1c7cc5b2010-07-18 10:46:33 +0200905 switch (get_std_op(ptr)) {
906 case STD_OP_EQ: *opaque = 0; break;
907 case STD_OP_GT: *opaque = 1; break;
908 case STD_OP_GE: *opaque = 2; break;
909 case STD_OP_LT: *opaque = 3; break;
910 case STD_OP_LE: *opaque = 4; break;
911 default:
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200912 memprintf(err, "'%s' is neither a number nor a supported operator", ptr);
Willy Tarreauae8b7962007-06-09 23:10:04 +0200913 return 0;
Willy Tarreau1c7cc5b2010-07-18 10:46:33 +0200914 }
Willy Tarreauae8b7962007-06-09 23:10:04 +0200915
916 skip++;
917 ptr = text[skip];
918 }
Willy Tarreaua84d3742007-05-07 00:36:48 +0200919
920 last = i = 0;
921 while (1) {
Willy Tarreauae8b7962007-06-09 23:10:04 +0200922 j = *ptr++;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200923 if ((j == '-' || j == ':') && !last) {
924 last++;
925 pattern->val.range.min = i;
926 i = 0;
927 continue;
928 }
929 j -= '0';
930 if (j > 9)
931 // also catches the terminating zero
932 break;
933 i *= 10;
934 i += j;
935 }
Willy Tarreauae8b7962007-06-09 23:10:04 +0200936
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200937 if (last && *opaque >= 1 && *opaque <= 4) {
Willy Tarreauae8b7962007-06-09 23:10:04 +0200938 /* having a range with a min or a max is absurd */
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200939 memprintf(err, "integer range '%s' specified with a comparison operator", text[skip]);
Willy Tarreauae8b7962007-06-09 23:10:04 +0200940 return 0;
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200941 }
Willy Tarreauae8b7962007-06-09 23:10:04 +0200942
Willy Tarreaua84d3742007-05-07 00:36:48 +0200943 if (!last)
944 pattern->val.range.min = i;
945 pattern->val.range.max = i;
Willy Tarreauae8b7962007-06-09 23:10:04 +0200946
947 switch (*opaque) {
948 case 0: /* eq */
949 pattern->val.range.min_set = 1;
950 pattern->val.range.max_set = 1;
951 break;
952 case 1: /* gt */
953 pattern->val.range.min++; /* gt = ge + 1 */
954 case 2: /* ge */
955 pattern->val.range.min_set = 1;
956 pattern->val.range.max_set = 0;
957 break;
958 case 3: /* lt */
959 pattern->val.range.max--; /* lt = le - 1 */
960 case 4: /* le */
961 pattern->val.range.min_set = 0;
962 pattern->val.range.max_set = 1;
963 break;
964 }
965 return skip + 1;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200966}
967
Willy Tarreau4a26d2f2008-07-15 16:05:33 +0200968/* Parse a range of positive 2-component versions delimited by either ':' or
969 * '-'. The version consists in a major and a minor, both of which must be
970 * smaller than 65536, because internally they will be represented as a 32-bit
971 * integer.
972 * If only one version is read, it is set as both min and max. Just like for
973 * pure integers, an operator may be specified as the prefix, among this list
974 * of 5 :
975 *
976 * 0:eq, 1:gt, 2:ge, 3:lt, 4:le
977 *
978 * The default operator is "eq". It supports range matching. Ranges are
979 * rejected for other operators. The operator may be changed at any time.
980 * The operator is stored in the 'opaque' argument. This allows constructs
981 * such as the following one :
982 *
983 * acl obsolete_ssl ssl_req_proto lt 3
984 * acl unsupported_ssl ssl_req_proto gt 3.1
985 * acl valid_ssl ssl_req_proto 3.0-3.1
986 *
987 */
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200988int acl_parse_dotted_ver(const char **text, struct acl_pattern *pattern, int *opaque, char **err)
Willy Tarreau4a26d2f2008-07-15 16:05:33 +0200989{
990 signed long long i;
991 unsigned int j, last, skip = 0;
992 const char *ptr = *text;
993
994
995 while (!isdigit((unsigned char)*ptr)) {
Willy Tarreau1c7cc5b2010-07-18 10:46:33 +0200996 switch (get_std_op(ptr)) {
997 case STD_OP_EQ: *opaque = 0; break;
998 case STD_OP_GT: *opaque = 1; break;
999 case STD_OP_GE: *opaque = 2; break;
1000 case STD_OP_LT: *opaque = 3; break;
1001 case STD_OP_LE: *opaque = 4; break;
1002 default:
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001003 memprintf(err, "'%s' is neither a number nor a supported operator", ptr);
Willy Tarreau4a26d2f2008-07-15 16:05:33 +02001004 return 0;
Willy Tarreau1c7cc5b2010-07-18 10:46:33 +02001005 }
Willy Tarreau4a26d2f2008-07-15 16:05:33 +02001006
1007 skip++;
1008 ptr = text[skip];
1009 }
1010
1011 last = i = 0;
1012 while (1) {
1013 j = *ptr++;
1014 if (j == '.') {
1015 /* minor part */
1016 if (i >= 65536)
1017 return 0;
1018 i <<= 16;
1019 continue;
1020 }
1021 if ((j == '-' || j == ':') && !last) {
1022 last++;
1023 if (i < 65536)
1024 i <<= 16;
1025 pattern->val.range.min = i;
1026 i = 0;
1027 continue;
1028 }
1029 j -= '0';
1030 if (j > 9)
1031 // also catches the terminating zero
1032 break;
1033 i = (i & 0xFFFF0000) + (i & 0xFFFF) * 10;
1034 i += j;
1035 }
1036
1037 /* if we only got a major version, let's shift it now */
1038 if (i < 65536)
1039 i <<= 16;
1040
Willy Tarreau7dcb6482012-04-27 17:52:25 +02001041 if (last && *opaque >= 1 && *opaque <= 4) {
Willy Tarreau4a26d2f2008-07-15 16:05:33 +02001042 /* having a range with a min or a max is absurd */
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001043 memprintf(err, "version range '%s' specified with a comparison operator", text[skip]);
Willy Tarreau4a26d2f2008-07-15 16:05:33 +02001044 return 0;
Willy Tarreau7dcb6482012-04-27 17:52:25 +02001045 }
Willy Tarreau4a26d2f2008-07-15 16:05:33 +02001046
1047 if (!last)
1048 pattern->val.range.min = i;
1049 pattern->val.range.max = i;
1050
1051 switch (*opaque) {
1052 case 0: /* eq */
1053 pattern->val.range.min_set = 1;
1054 pattern->val.range.max_set = 1;
1055 break;
1056 case 1: /* gt */
1057 pattern->val.range.min++; /* gt = ge + 1 */
1058 case 2: /* ge */
1059 pattern->val.range.min_set = 1;
1060 pattern->val.range.max_set = 0;
1061 break;
1062 case 3: /* lt */
1063 pattern->val.range.max--; /* lt = le - 1 */
1064 case 4: /* le */
1065 pattern->val.range.min_set = 0;
1066 pattern->val.range.max_set = 1;
1067 break;
1068 }
1069 return skip + 1;
1070}
1071
Willy Tarreaua67fad92007-05-08 19:50:09 +02001072/* Parse an IP address and an optional mask in the form addr[/mask].
1073 * The addr may either be an IPv4 address or a hostname. The mask
1074 * may either be a dotted mask or a number of bits. Returns 1 if OK,
Willy Tarreauc92ddbc2012-04-27 22:10:57 +02001075 * otherwise 0. NOTE: IP address patterns are typed (IPV4/IPV6).
Willy Tarreaua67fad92007-05-08 19:50:09 +02001076 */
Willy Tarreau7dcb6482012-04-27 17:52:25 +02001077int acl_parse_ip(const char **text, struct acl_pattern *pattern, int *opaque, char **err)
Willy Tarreaua67fad92007-05-08 19:50:09 +02001078{
Willy Tarreaub337b532010-05-13 20:03:41 +02001079 struct eb_root *tree = NULL;
1080 if (pattern->flags & ACL_PAT_F_TREE_OK)
1081 tree = pattern->val.tree;
1082
1083 if (str2net(*text, &pattern->val.ipv4.addr, &pattern->val.ipv4.mask)) {
1084 unsigned int mask = ntohl(pattern->val.ipv4.mask.s_addr);
1085 struct ebmb_node *node;
1086 /* check if the mask is contiguous so that we can insert the
1087 * network into the tree. A continuous mask has only ones on
1088 * the left. This means that this mask + its lower bit added
1089 * once again is null.
1090 */
Willy Tarreauceb4ac92012-04-28 00:41:46 +02001091 pattern->type = SMP_T_IPV4;
Willy Tarreaub337b532010-05-13 20:03:41 +02001092 if (mask + (mask & -mask) == 0 && tree) {
1093 mask = mask ? 33 - flsnz(mask & -mask) : 0; /* equals cidr value */
1094 /* FIXME: insert <addr>/<mask> into the tree here */
1095 node = calloc(1, sizeof(*node) + 4); /* reserve 4 bytes for IPv4 address */
Willy Tarreau7dcb6482012-04-27 17:52:25 +02001096 if (!node) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001097 memprintf(err, "out of memory while loading IPv4 pattern");
Willy Tarreaub337b532010-05-13 20:03:41 +02001098 return 0;
Willy Tarreau7dcb6482012-04-27 17:52:25 +02001099 }
Willy Tarreaub337b532010-05-13 20:03:41 +02001100 memcpy(node->key, &pattern->val.ipv4.addr, 4); /* network byte order */
1101 node->node.pfx = mask;
1102 if (ebmb_insert_prefix(tree, node, 4) != node)
1103 free(node); /* was a duplicate */
1104 pattern->flags |= ACL_PAT_F_TREE;
1105 return 1;
1106 }
Willy Tarreauae8b7962007-06-09 23:10:04 +02001107 return 1;
Willy Tarreaub337b532010-05-13 20:03:41 +02001108 }
Willy Tarreauceb4ac92012-04-28 00:41:46 +02001109 else if (str62net(*text, &pattern->val.ipv6.addr, &pattern->val.ipv6.mask)) {
1110 /* no tree support right now */
1111 pattern->type = SMP_T_IPV6;
1112 return 1;
1113 }
Willy Tarreau7dcb6482012-04-27 17:52:25 +02001114 else {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001115 memprintf(err, "'%s' is not a valid IPv4 or IPv6 address", *text);
Willy Tarreauae8b7962007-06-09 23:10:04 +02001116 return 0;
Willy Tarreau7dcb6482012-04-27 17:52:25 +02001117 }
Willy Tarreaua67fad92007-05-08 19:50:09 +02001118}
1119
Willy Tarreaua84d3742007-05-07 00:36:48 +02001120/*
1121 * Registers the ACL keyword list <kwl> as a list of valid keywords for next
1122 * parsing sessions.
1123 */
1124void acl_register_keywords(struct acl_kw_list *kwl)
1125{
1126 LIST_ADDQ(&acl_keywords.list, &kwl->list);
1127}
1128
1129/*
1130 * Unregisters the ACL keyword list <kwl> from the list of valid keywords.
1131 */
1132void acl_unregister_keywords(struct acl_kw_list *kwl)
1133{
1134 LIST_DEL(&kwl->list);
1135 LIST_INIT(&kwl->list);
1136}
1137
1138/* Return a pointer to the ACL <name> within the list starting at <head>, or
1139 * NULL if not found.
1140 */
1141struct acl *find_acl_by_name(const char *name, struct list *head)
1142{
1143 struct acl *acl;
1144 list_for_each_entry(acl, head, list) {
1145 if (strcmp(acl->name, name) == 0)
1146 return acl;
1147 }
1148 return NULL;
1149}
1150
1151/* Return a pointer to the ACL keyword <kw>, or NULL if not found. Note that if
1152 * <kw> contains an opening parenthesis, only the left part of it is checked.
1153 */
1154struct acl_keyword *find_acl_kw(const char *kw)
1155{
1156 int index;
1157 const char *kwend;
1158 struct acl_kw_list *kwl;
1159
1160 kwend = strchr(kw, '(');
1161 if (!kwend)
1162 kwend = kw + strlen(kw);
1163
1164 list_for_each_entry(kwl, &acl_keywords.list, list) {
1165 for (index = 0; kwl->kw[index].kw != NULL; index++) {
1166 if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) &&
1167 kwl->kw[index].kw[kwend-kw] == 0)
1168 return &kwl->kw[index];
1169 }
1170 }
1171 return NULL;
1172}
1173
Willy Tarreaudfd7fca2011-03-09 07:27:02 +01001174/* NB: does nothing if <pat> is NULL */
Willy Tarreaua84d3742007-05-07 00:36:48 +02001175static void free_pattern(struct acl_pattern *pat)
1176{
Willy Tarreaudfd7fca2011-03-09 07:27:02 +01001177 if (!pat)
1178 return;
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02001179
1180 if (pat->ptr.ptr) {
1181 if (pat->freeptrbuf)
1182 pat->freeptrbuf(pat->ptr.ptr);
1183
Willy Tarreaua84d3742007-05-07 00:36:48 +02001184 free(pat->ptr.ptr);
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02001185 }
1186
Willy Tarreaua84d3742007-05-07 00:36:48 +02001187 free(pat);
1188}
1189
1190static void free_pattern_list(struct list *head)
1191{
1192 struct acl_pattern *pat, *tmp;
1193 list_for_each_entry_safe(pat, tmp, head, list)
1194 free_pattern(pat);
1195}
1196
Willy Tarreaue56cda92010-05-11 23:25:05 +02001197static void free_pattern_tree(struct eb_root *root)
1198{
1199 struct eb_node *node, *next;
1200 node = eb_first(root);
1201 while (node) {
1202 next = eb_next(node);
1203 free(node);
1204 node = next;
1205 }
1206}
1207
Willy Tarreaua84d3742007-05-07 00:36:48 +02001208static struct acl_expr *prune_acl_expr(struct acl_expr *expr)
1209{
Willy Tarreau34db1082012-04-19 17:16:54 +02001210 struct arg *arg;
1211
Willy Tarreaua84d3742007-05-07 00:36:48 +02001212 free_pattern_list(&expr->patterns);
Willy Tarreaue56cda92010-05-11 23:25:05 +02001213 free_pattern_tree(&expr->pattern_tree);
Willy Tarreaua84d3742007-05-07 00:36:48 +02001214 LIST_INIT(&expr->patterns);
Willy Tarreau34db1082012-04-19 17:16:54 +02001215
1216 for (arg = expr->args; arg; arg++) {
1217 if (arg->type == ARGT_STOP)
1218 break;
Willy Tarreau496aa012012-06-01 10:38:29 +02001219 if (arg->type == ARGT_STR || arg->unresolved) {
Willy Tarreau34db1082012-04-19 17:16:54 +02001220 free(arg->data.str.str);
1221 arg->data.str.str = NULL;
Willy Tarreau496aa012012-06-01 10:38:29 +02001222 arg->unresolved = 0;
Willy Tarreau34db1082012-04-19 17:16:54 +02001223 }
1224 arg++;
1225 }
1226
Willy Tarreau2e845be2012-10-19 19:49:09 +02001227 if (expr->args != empty_arg_list)
1228 free(expr->args);
Willy Tarreaua84d3742007-05-07 00:36:48 +02001229 expr->kw->use_cnt--;
1230 return expr;
1231}
1232
Willy Tarreau08ad0b32012-04-27 17:25:24 +02001233
1234/* Reads patterns from a file. If <err_msg> is non-NULL, an error message will
1235 * be returned there on errors and the caller will have to free it.
1236 */
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001237static int acl_read_patterns_from_file( struct acl_keyword *aclkw,
1238 struct acl_expr *expr,
Willy Tarreau08ad0b32012-04-27 17:25:24 +02001239 const char *filename, int patflags,
1240 char **err)
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001241{
1242 FILE *file;
1243 char *c;
1244 const char *args[2];
1245 struct acl_pattern *pattern;
1246 int opaque;
Willy Tarreau6a8097f2011-02-26 15:14:15 +01001247 int ret = 0;
Willy Tarreau08ad0b32012-04-27 17:25:24 +02001248 int line = 0;
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001249
1250 file = fopen(filename, "r");
Willy Tarreau08ad0b32012-04-27 17:25:24 +02001251 if (!file) {
1252 memprintf(err, "failed to open pattern file <%s>", filename);
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001253 return 0;
Willy Tarreau08ad0b32012-04-27 17:25:24 +02001254 }
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001255
1256 /* now parse all patterns. The file may contain only one pattern per
1257 * line. If the line contains spaces, they will be part of the pattern.
1258 * The pattern stops at the first CR, LF or EOF encountered.
1259 */
1260 opaque = 0;
Willy Tarreaue56cda92010-05-11 23:25:05 +02001261 pattern = NULL;
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001262 args[1] = "";
David du Colombier7af46052012-05-16 14:16:48 +02001263 while (fgets(trash, trashlen, file) != NULL) {
Willy Tarreau08ad0b32012-04-27 17:25:24 +02001264 line++;
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001265 c = trash;
Willy Tarreau58215a02010-05-13 22:07:43 +02001266
1267 /* ignore lines beginning with a dash */
1268 if (*c == '#')
1269 continue;
1270
1271 /* strip leading spaces and tabs */
1272 while (*c == ' ' || *c == '\t')
1273 c++;
1274
Willy Tarreau58215a02010-05-13 22:07:43 +02001275
1276 args[0] = c;
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001277 while (*c && *c != '\n' && *c != '\r')
1278 c++;
1279 *c = 0;
1280
Willy Tarreau51091962011-01-03 21:04:10 +01001281 /* empty lines are ignored too */
1282 if (c == args[0])
1283 continue;
1284
Willy Tarreaue56cda92010-05-11 23:25:05 +02001285 /* we keep the previous pattern along iterations as long as it's not used */
1286 if (!pattern)
1287 pattern = (struct acl_pattern *)malloc(sizeof(*pattern));
Willy Tarreau08ad0b32012-04-27 17:25:24 +02001288 if (!pattern) {
1289 memprintf(err, "out of memory when loading patterns from file <%s>", filename);
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001290 goto out_close;
Willy Tarreau08ad0b32012-04-27 17:25:24 +02001291 }
Willy Tarreaue56cda92010-05-11 23:25:05 +02001292
1293 memset(pattern, 0, sizeof(*pattern));
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001294 pattern->flags = patflags;
1295
Willy Tarreaue56cda92010-05-11 23:25:05 +02001296 if ((aclkw->requires & ACL_MAY_LOOKUP) && !(pattern->flags & ACL_PAT_F_IGNORE_CASE)) {
1297 /* we pre-set the data pointer to the tree's head so that functions
1298 * which are able to insert in a tree know where to do that.
1299 */
1300 pattern->flags |= ACL_PAT_F_TREE_OK;
1301 pattern->val.tree = &expr->pattern_tree;
1302 }
1303
Willy Tarreauc92ddbc2012-04-27 22:10:57 +02001304 pattern->type = SMP_TYPES; /* unspecified type by default */
Willy Tarreau7dcb6482012-04-27 17:52:25 +02001305 if (!aclkw->parse(args, pattern, &opaque, err))
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001306 goto out_free_pattern;
Willy Tarreaue56cda92010-05-11 23:25:05 +02001307
1308 /* if the parser did not feed the tree, let's chain the pattern to the list */
1309 if (!(pattern->flags & ACL_PAT_F_TREE)) {
1310 LIST_ADDQ(&expr->patterns, &pattern->list);
1311 pattern = NULL; /* get a new one */
1312 }
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001313 }
Willy Tarreau6a8097f2011-02-26 15:14:15 +01001314
1315 ret = 1; /* success */
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001316
1317 out_free_pattern:
1318 free_pattern(pattern);
1319 out_close:
1320 fclose(file);
Willy Tarreau6a8097f2011-02-26 15:14:15 +01001321 return ret;
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001322}
1323
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001324/* Parse an ACL expression starting at <args>[0], and return it. If <err> is
1325 * not NULL, it will be filled with a pointer to an error message in case of
1326 * error. This pointer must be freeable or NULL.
1327 *
Willy Tarreaua84d3742007-05-07 00:36:48 +02001328 * Right now, the only accepted syntax is :
1329 * <subject> [<value>...]
1330 */
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001331struct acl_expr *parse_acl_expr(const char **args, char **err)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001332{
1333 __label__ out_return, out_free_expr, out_free_pattern;
1334 struct acl_expr *expr;
1335 struct acl_keyword *aclkw;
1336 struct acl_pattern *pattern;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02001337 int opaque, patflags;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001338 const char *arg;
1339
1340 aclkw = find_acl_kw(args[0]);
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001341 if (!aclkw || !aclkw->parse) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001342 memprintf(err, "unknown ACL keyword '%s'", *args);
Willy Tarreaua84d3742007-05-07 00:36:48 +02001343 goto out_return;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001344 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001345
1346 expr = (struct acl_expr *)calloc(1, sizeof(*expr));
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001347 if (!expr) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001348 memprintf(err, "out of memory when parsing ACL expression");
Willy Tarreaua84d3742007-05-07 00:36:48 +02001349 goto out_return;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001350 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001351
1352 expr->kw = aclkw;
1353 aclkw->use_cnt++;
1354 LIST_INIT(&expr->patterns);
Willy Tarreaue56cda92010-05-11 23:25:05 +02001355 expr->pattern_tree = EB_ROOT_UNIQUE;
Willy Tarreau2e845be2012-10-19 19:49:09 +02001356 expr->args = empty_arg_list;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001357
1358 arg = strchr(args[0], '(');
Willy Tarreau61612d42012-04-19 18:42:05 +02001359 if (aclkw->arg_mask) {
1360 int nbargs = 0;
Willy Tarreau34db1082012-04-19 17:16:54 +02001361 char *end;
Willy Tarreau34db1082012-04-19 17:16:54 +02001362
Willy Tarreau61612d42012-04-19 18:42:05 +02001363 if (arg != NULL) {
1364 /* there are 0 or more arguments in the form "subject(arg[,arg]*)" */
1365 arg++;
1366 end = strchr(arg, ')');
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001367 if (!end) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001368 memprintf(err, "missing closing ')' after arguments to ACL keyword '%s'", aclkw->kw);
Willy Tarreau61612d42012-04-19 18:42:05 +02001369 goto out_free_expr;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001370 }
Willy Tarreau34db1082012-04-19 17:16:54 +02001371
Willy Tarreau61612d42012-04-19 18:42:05 +02001372 /* Parse the arguments. Note that currently we have no way to
1373 * report parsing errors, hence the NULL in the error pointers.
1374 * An error is also reported if some mandatory arguments are
1375 * missing.
1376 */
1377 nbargs = make_arg_list(arg, end - arg, aclkw->arg_mask, &expr->args,
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001378 err, NULL, NULL);
1379 if (nbargs < 0) {
1380 /* note that make_arg_list will have set <err> here */
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001381 memprintf(err, "in argument to '%s', %s", aclkw->kw, *err);
Willy Tarreau61612d42012-04-19 18:42:05 +02001382 goto out_free_expr;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001383 }
Willy Tarreauae52f062012-04-26 12:13:35 +02001384
Willy Tarreau2e845be2012-10-19 19:49:09 +02001385 if (!expr->args)
1386 expr->args = empty_arg_list;
1387
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001388 if (aclkw->val_args && !aclkw->val_args(expr->args, err)) {
1389 /* invalid keyword argument, error must have been
1390 * set by val_args().
1391 */
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001392 memprintf(err, "in argument to '%s', %s", aclkw->kw, *err);
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001393 goto out_free_expr;
1394 }
Willy Tarreau61612d42012-04-19 18:42:05 +02001395 }
Willy Tarreaufc2c1fd2012-04-19 23:35:54 +02001396 else if (ARGM(aclkw->arg_mask) == 1) {
1397 int type = (aclkw->arg_mask >> 4) & 15;
1398
1399 /* If a proxy is noted as a mandatory argument, we'll fake
1400 * an empty one so that acl_find_targets() resolves it as
1401 * the current one later.
1402 */
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001403 if (type != ARGT_FE && type != ARGT_BE && type != ARGT_TAB) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001404 memprintf(err, "ACL keyword '%s' expects %d arguments", aclkw->kw, ARGM(aclkw->arg_mask));
Willy Tarreaufc2c1fd2012-04-19 23:35:54 +02001405 goto out_free_expr;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001406 }
Willy Tarreaufc2c1fd2012-04-19 23:35:54 +02001407
1408 /* Build an arg list containing the type as an empty string
1409 * and the usual STOP.
1410 */
1411 expr->args = calloc(2, sizeof(*expr->args));
1412 expr->args[0].type = type;
Willy Tarreaue3a46112012-06-15 08:02:34 +02001413 expr->args[0].unresolved = 1;
Willy Tarreaufc2c1fd2012-04-19 23:35:54 +02001414 expr->args[0].data.str.str = strdup("");
1415 expr->args[0].data.str.len = 1;
1416 expr->args[0].data.str.len = 0;
1417 expr->args[1].type = ARGT_STOP;
1418 }
Willy Tarreau61612d42012-04-19 18:42:05 +02001419 else if (ARGM(aclkw->arg_mask)) {
1420 /* there were some mandatory arguments */
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001421 memprintf(err, "ACL keyword '%s' expects %d arguments", aclkw->kw, ARGM(aclkw->arg_mask));
Willy Tarreaua84d3742007-05-07 00:36:48 +02001422 goto out_free_expr;
Willy Tarreau61612d42012-04-19 18:42:05 +02001423 }
1424 }
1425 else {
1426 if (arg) {
1427 /* no argument expected */
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001428 memprintf(err, "ACL keyword '%s' takes no argument", aclkw->kw);
Willy Tarreau61612d42012-04-19 18:42:05 +02001429 goto out_free_expr;
1430 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001431 }
1432
Willy Tarreaua84d3742007-05-07 00:36:48 +02001433 args++;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02001434
1435 /* check for options before patterns. Supported options are :
1436 * -i : ignore case for all patterns by default
1437 * -f : read patterns from those files
1438 * -- : everything after this is not an option
1439 */
1440 patflags = 0;
1441 while (**args == '-') {
1442 if ((*args)[1] == 'i')
1443 patflags |= ACL_PAT_F_IGNORE_CASE;
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001444 else if ((*args)[1] == 'f') {
Willy Tarreau08ad0b32012-04-27 17:25:24 +02001445 if (!acl_read_patterns_from_file(aclkw, expr, args[1], patflags | ACL_PAT_F_FROM_FILE, err))
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001446 goto out_free_expr;
1447 args++;
1448 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +02001449 else if ((*args)[1] == '-') {
1450 args++;
1451 break;
1452 }
1453 else
1454 break;
1455 args++;
1456 }
1457
1458 /* now parse all patterns */
Willy Tarreauae8b7962007-06-09 23:10:04 +02001459 opaque = 0;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001460 while (**args) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02001461 int ret;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001462 pattern = (struct acl_pattern *)calloc(1, sizeof(*pattern));
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001463 if (!pattern) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001464 memprintf(err, "out of memory when parsing ACL pattern");
Willy Tarreaua84d3742007-05-07 00:36:48 +02001465 goto out_free_expr;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001466 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +02001467 pattern->flags = patflags;
1468
Willy Tarreauc92ddbc2012-04-27 22:10:57 +02001469 pattern->type = SMP_TYPES; /* unspecified type */
Willy Tarreau7dcb6482012-04-27 17:52:25 +02001470 ret = aclkw->parse(args, pattern, &opaque, err);
1471 if (!ret)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001472 goto out_free_pattern;
Willy Tarreau7dcb6482012-04-27 17:52:25 +02001473
Willy Tarreaua84d3742007-05-07 00:36:48 +02001474 LIST_ADDQ(&expr->patterns, &pattern->list);
Willy Tarreauae8b7962007-06-09 23:10:04 +02001475 args += ret;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001476 }
1477
1478 return expr;
1479
1480 out_free_pattern:
1481 free_pattern(pattern);
1482 out_free_expr:
1483 prune_acl_expr(expr);
1484 free(expr);
1485 out_return:
1486 return NULL;
1487}
1488
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02001489/* Purge everything in the acl <acl>, then return <acl>. */
1490struct acl *prune_acl(struct acl *acl) {
1491
1492 struct acl_expr *expr, *exprb;
1493
1494 free(acl->name);
1495
1496 list_for_each_entry_safe(expr, exprb, &acl->expr, list) {
1497 LIST_DEL(&expr->list);
1498 prune_acl_expr(expr);
1499 free(expr);
1500 }
1501
1502 return acl;
1503}
1504
Willy Tarreaua84d3742007-05-07 00:36:48 +02001505/* Parse an ACL with the name starting at <args>[0], and with a list of already
1506 * known ACLs in <acl>. If the ACL was not in the list, it will be added.
Willy Tarreau2a56c5e2010-03-15 16:13:29 +01001507 * A pointer to that ACL is returned. If the ACL has an empty name, then it's
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001508 * an anonymous one and it won't be merged with any other one. If <err> is not
1509 * NULL, it will be filled with an appropriate error. This pointer must be
1510 * freeable or NULL.
Willy Tarreaua84d3742007-05-07 00:36:48 +02001511 *
1512 * args syntax: <aclname> <acl_expr>
1513 */
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001514struct acl *parse_acl(const char **args, struct list *known_acl, char **err)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001515{
1516 __label__ out_return, out_free_acl_expr, out_free_name;
1517 struct acl *cur_acl;
1518 struct acl_expr *acl_expr;
1519 char *name;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001520 const char *pos;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001521
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001522 if (**args && (pos = invalid_char(*args))) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001523 memprintf(err, "invalid character in ACL name : '%c'", *pos);
Willy Tarreau2e74c3f2007-12-02 18:45:09 +01001524 goto out_return;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001525 }
Willy Tarreau2e74c3f2007-12-02 18:45:09 +01001526
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001527 acl_expr = parse_acl_expr(args + 1, err);
1528 if (!acl_expr) {
1529 /* parse_acl_expr will have filled <err> here */
Willy Tarreaua84d3742007-05-07 00:36:48 +02001530 goto out_return;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001531 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001532
Willy Tarreau404e8ab2009-07-26 19:40:40 +02001533 /* Check for args beginning with an opening parenthesis just after the
1534 * subject, as this is almost certainly a typo. Right now we can only
1535 * emit a warning, so let's do so.
1536 */
Krzysztof Piotr Oledzki4cdd8312009-10-05 00:23:35 +02001537 if (!strchr(args[1], '(') && *args[2] == '(')
Willy Tarreau404e8ab2009-07-26 19:40:40 +02001538 Warning("parsing acl '%s' :\n"
1539 " matching '%s' for pattern '%s' is likely a mistake and probably\n"
1540 " not what you want. Maybe you need to remove the extraneous space before '('.\n"
1541 " If you are really sure this is not an error, please insert '--' between the\n"
1542 " match and the pattern to make this warning message disappear.\n",
1543 args[0], args[1], args[2]);
1544
Willy Tarreau2a56c5e2010-03-15 16:13:29 +01001545 if (*args[0])
1546 cur_acl = find_acl_by_name(args[0], known_acl);
1547 else
1548 cur_acl = NULL;
1549
Willy Tarreaua84d3742007-05-07 00:36:48 +02001550 if (!cur_acl) {
1551 name = strdup(args[0]);
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001552 if (!name) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001553 memprintf(err, "out of memory when parsing ACL");
Willy Tarreaua84d3742007-05-07 00:36:48 +02001554 goto out_free_acl_expr;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001555 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001556 cur_acl = (struct acl *)calloc(1, sizeof(*cur_acl));
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001557 if (cur_acl == NULL) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001558 memprintf(err, "out of memory when parsing ACL");
Willy Tarreaua84d3742007-05-07 00:36:48 +02001559 goto out_free_name;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001560 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001561
1562 LIST_INIT(&cur_acl->expr);
1563 LIST_ADDQ(known_acl, &cur_acl->list);
1564 cur_acl->name = name;
1565 }
1566
Willy Tarreaua9802632008-07-25 19:13:19 +02001567 cur_acl->requires |= acl_expr->kw->requires;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001568 LIST_ADDQ(&cur_acl->expr, &acl_expr->list);
1569 return cur_acl;
1570
1571 out_free_name:
1572 free(name);
1573 out_free_acl_expr:
1574 prune_acl_expr(acl_expr);
1575 free(acl_expr);
1576 out_return:
1577 return NULL;
1578}
1579
Willy Tarreau16fbe822007-06-17 11:54:31 +02001580/* Some useful ACLs provided by default. Only those used are allocated. */
1581
1582const struct {
1583 const char *name;
1584 const char *expr[4]; /* put enough for longest expression */
1585} default_acl_list[] = {
Willy Tarreau58393e12008-07-20 10:39:22 +02001586 { .name = "TRUE", .expr = {"always_true",""}},
1587 { .name = "FALSE", .expr = {"always_false",""}},
Willy Tarreau16fbe822007-06-17 11:54:31 +02001588 { .name = "LOCALHOST", .expr = {"src","127.0.0.1/8",""}},
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001589 { .name = "HTTP", .expr = {"req_proto_http",""}},
Willy Tarreau16fbe822007-06-17 11:54:31 +02001590 { .name = "HTTP_1.0", .expr = {"req_ver","1.0",""}},
1591 { .name = "HTTP_1.1", .expr = {"req_ver","1.1",""}},
1592 { .name = "METH_CONNECT", .expr = {"method","CONNECT",""}},
1593 { .name = "METH_GET", .expr = {"method","GET","HEAD",""}},
1594 { .name = "METH_HEAD", .expr = {"method","HEAD",""}},
1595 { .name = "METH_OPTIONS", .expr = {"method","OPTIONS",""}},
1596 { .name = "METH_POST", .expr = {"method","POST",""}},
1597 { .name = "METH_TRACE", .expr = {"method","TRACE",""}},
1598 { .name = "HTTP_URL_ABS", .expr = {"url_reg","^[^/:]*://",""}},
1599 { .name = "HTTP_URL_SLASH", .expr = {"url_beg","/",""}},
1600 { .name = "HTTP_URL_STAR", .expr = {"url","*",""}},
1601 { .name = "HTTP_CONTENT", .expr = {"hdr_val(content-length)","gt","0",""}},
Emeric Brunbede3d02009-06-30 17:54:00 +02001602 { .name = "RDP_COOKIE", .expr = {"req_rdp_cookie_cnt","gt","0",""}},
Willy Tarreauc6317702008-07-20 09:29:50 +02001603 { .name = "REQ_CONTENT", .expr = {"req_len","gt","0",""}},
Willy Tarreaub6fb4202008-07-20 11:18:28 +02001604 { .name = "WAIT_END", .expr = {"wait_end",""}},
Willy Tarreau16fbe822007-06-17 11:54:31 +02001605 { .name = NULL, .expr = {""}}
1606};
1607
1608/* Find a default ACL from the default_acl list, compile it and return it.
1609 * If the ACL is not found, NULL is returned. In theory, it cannot fail,
1610 * except when default ACLs are broken, in which case it will return NULL.
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001611 * If <known_acl> is not NULL, the ACL will be queued at its tail. If <err> is
1612 * not NULL, it will be filled with an error message if an error occurs. This
1613 * pointer must be freeable or NULL.
Willy Tarreau16fbe822007-06-17 11:54:31 +02001614 */
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001615struct acl *find_acl_default(const char *acl_name, struct list *known_acl, char **err)
Willy Tarreau16fbe822007-06-17 11:54:31 +02001616{
1617 __label__ out_return, out_free_acl_expr, out_free_name;
1618 struct acl *cur_acl;
1619 struct acl_expr *acl_expr;
1620 char *name;
1621 int index;
1622
1623 for (index = 0; default_acl_list[index].name != NULL; index++) {
1624 if (strcmp(acl_name, default_acl_list[index].name) == 0)
1625 break;
1626 }
1627
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001628 if (default_acl_list[index].name == NULL) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001629 memprintf(err, "no such ACL : '%s'", acl_name);
Willy Tarreau16fbe822007-06-17 11:54:31 +02001630 return NULL;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001631 }
Willy Tarreau16fbe822007-06-17 11:54:31 +02001632
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001633 acl_expr = parse_acl_expr((const char **)default_acl_list[index].expr, err);
1634 if (!acl_expr) {
1635 /* parse_acl_expr must have filled err here */
Willy Tarreau16fbe822007-06-17 11:54:31 +02001636 goto out_return;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001637 }
Willy Tarreau16fbe822007-06-17 11:54:31 +02001638
1639 name = strdup(acl_name);
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001640 if (!name) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001641 memprintf(err, "out of memory when building default ACL '%s'", acl_name);
Willy Tarreau16fbe822007-06-17 11:54:31 +02001642 goto out_free_acl_expr;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001643 }
1644
Willy Tarreau16fbe822007-06-17 11:54:31 +02001645 cur_acl = (struct acl *)calloc(1, sizeof(*cur_acl));
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001646 if (cur_acl == NULL) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001647 memprintf(err, "out of memory when building default ACL '%s'", acl_name);
Willy Tarreau16fbe822007-06-17 11:54:31 +02001648 goto out_free_name;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001649 }
Willy Tarreau16fbe822007-06-17 11:54:31 +02001650
1651 cur_acl->name = name;
Willy Tarreaua55b7dc2009-07-12 09:21:30 +02001652 cur_acl->requires |= acl_expr->kw->requires;
Willy Tarreau16fbe822007-06-17 11:54:31 +02001653 LIST_INIT(&cur_acl->expr);
1654 LIST_ADDQ(&cur_acl->expr, &acl_expr->list);
1655 if (known_acl)
1656 LIST_ADDQ(known_acl, &cur_acl->list);
1657
1658 return cur_acl;
1659
1660 out_free_name:
1661 free(name);
1662 out_free_acl_expr:
1663 prune_acl_expr(acl_expr);
1664 free(acl_expr);
1665 out_return:
1666 return NULL;
1667}
Willy Tarreaua84d3742007-05-07 00:36:48 +02001668
1669/* Purge everything in the acl_cond <cond>, then return <cond>. */
1670struct acl_cond *prune_acl_cond(struct acl_cond *cond)
1671{
1672 struct acl_term_suite *suite, *tmp_suite;
1673 struct acl_term *term, *tmp_term;
1674
1675 /* iterate through all term suites and free all terms and all suites */
1676 list_for_each_entry_safe(suite, tmp_suite, &cond->suites, list) {
1677 list_for_each_entry_safe(term, tmp_term, &suite->terms, list)
1678 free(term);
1679 free(suite);
1680 }
1681 return cond;
1682}
1683
1684/* Parse an ACL condition starting at <args>[0], relying on a list of already
1685 * known ACLs passed in <known_acl>. The new condition is returned (or NULL in
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001686 * case of low memory). Supports multiple conditions separated by "or". If
1687 * <err> is not NULL, it will be filled with a pointer to an error message in
1688 * case of error, that the caller is responsible for freeing. The initial
1689 * location must either be freeable or NULL.
Willy Tarreaua84d3742007-05-07 00:36:48 +02001690 */
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001691struct acl_cond *parse_acl_cond(const char **args, struct list *known_acl, int pol, char **err)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001692{
1693 __label__ out_return, out_free_suite, out_free_term;
Willy Tarreau74b98a82007-06-16 19:35:18 +02001694 int arg, neg;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001695 const char *word;
1696 struct acl *cur_acl;
1697 struct acl_term *cur_term;
1698 struct acl_term_suite *cur_suite;
1699 struct acl_cond *cond;
1700
1701 cond = (struct acl_cond *)calloc(1, sizeof(*cond));
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001702 if (cond == NULL) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001703 memprintf(err, "out of memory when parsing condition");
Willy Tarreaua84d3742007-05-07 00:36:48 +02001704 goto out_return;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001705 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001706
1707 LIST_INIT(&cond->list);
1708 LIST_INIT(&cond->suites);
1709 cond->pol = pol;
1710
1711 cur_suite = NULL;
Willy Tarreau74b98a82007-06-16 19:35:18 +02001712 neg = 0;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001713 for (arg = 0; *args[arg]; arg++) {
1714 word = args[arg];
1715
1716 /* remove as many exclamation marks as we can */
1717 while (*word == '!') {
1718 neg = !neg;
1719 word++;
1720 }
1721
1722 /* an empty word is allowed because we cannot force the user to
1723 * always think about not leaving exclamation marks alone.
1724 */
1725 if (!*word)
1726 continue;
1727
Willy Tarreau16fbe822007-06-17 11:54:31 +02001728 if (strcasecmp(word, "or") == 0 || strcmp(word, "||") == 0) {
Willy Tarreaua84d3742007-05-07 00:36:48 +02001729 /* new term suite */
1730 cur_suite = NULL;
1731 neg = 0;
1732 continue;
1733 }
1734
Willy Tarreau95fa4692010-02-01 13:05:50 +01001735 if (strcmp(word, "{") == 0) {
1736 /* we may have a complete ACL expression between two braces,
1737 * find the last one.
1738 */
1739 int arg_end = arg + 1;
1740 const char **args_new;
1741
1742 while (*args[arg_end] && strcmp(args[arg_end], "}") != 0)
1743 arg_end++;
1744
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001745 if (!*args[arg_end]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001746 memprintf(err, "missing closing '}' in condition");
Willy Tarreau95fa4692010-02-01 13:05:50 +01001747 goto out_free_suite;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001748 }
Willy Tarreau95fa4692010-02-01 13:05:50 +01001749
1750 args_new = calloc(1, (arg_end - arg + 1) * sizeof(*args_new));
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001751 if (!args_new) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001752 memprintf(err, "out of memory when parsing condition");
Willy Tarreau95fa4692010-02-01 13:05:50 +01001753 goto out_free_suite;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001754 }
Willy Tarreau95fa4692010-02-01 13:05:50 +01001755
Willy Tarreau2a56c5e2010-03-15 16:13:29 +01001756 args_new[0] = "";
Willy Tarreau95fa4692010-02-01 13:05:50 +01001757 memcpy(args_new + 1, args + arg + 1, (arg_end - arg) * sizeof(*args_new));
1758 args_new[arg_end - arg] = "";
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001759 cur_acl = parse_acl(args_new, known_acl, err);
Willy Tarreau95fa4692010-02-01 13:05:50 +01001760 free(args_new);
1761
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001762 if (!cur_acl) {
1763 /* note that parse_acl() must have filled <err> here */
Willy Tarreau16fbe822007-06-17 11:54:31 +02001764 goto out_free_suite;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001765 }
Willy Tarreau95fa4692010-02-01 13:05:50 +01001766 arg = arg_end;
1767 }
1768 else {
1769 /* search for <word> in the known ACL names. If we do not find
1770 * it, let's look for it in the default ACLs, and if found, add
1771 * it to the list of ACLs of this proxy. This makes it possible
1772 * to override them.
1773 */
1774 cur_acl = find_acl_by_name(word, known_acl);
1775 if (cur_acl == NULL) {
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001776 cur_acl = find_acl_default(word, known_acl, err);
1777 if (cur_acl == NULL) {
1778 /* note that find_acl_default() must have filled <err> here */
Willy Tarreau95fa4692010-02-01 13:05:50 +01001779 goto out_free_suite;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001780 }
Willy Tarreau95fa4692010-02-01 13:05:50 +01001781 }
Willy Tarreau16fbe822007-06-17 11:54:31 +02001782 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001783
1784 cur_term = (struct acl_term *)calloc(1, sizeof(*cur_term));
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001785 if (cur_term == NULL) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001786 memprintf(err, "out of memory when parsing condition");
Willy Tarreaua84d3742007-05-07 00:36:48 +02001787 goto out_free_suite;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001788 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001789
1790 cur_term->acl = cur_acl;
1791 cur_term->neg = neg;
Willy Tarreaua9802632008-07-25 19:13:19 +02001792 cond->requires |= cur_acl->requires;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001793
1794 if (!cur_suite) {
1795 cur_suite = (struct acl_term_suite *)calloc(1, sizeof(*cur_suite));
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001796 if (cur_term == NULL) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001797 memprintf(err, "out of memory when parsing condition");
Willy Tarreaua84d3742007-05-07 00:36:48 +02001798 goto out_free_term;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001799 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001800 LIST_INIT(&cur_suite->terms);
1801 LIST_ADDQ(&cond->suites, &cur_suite->list);
1802 }
1803 LIST_ADDQ(&cur_suite->terms, &cur_term->list);
Willy Tarreau74b98a82007-06-16 19:35:18 +02001804 neg = 0;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001805 }
1806
1807 return cond;
1808
1809 out_free_term:
1810 free(cur_term);
1811 out_free_suite:
1812 prune_acl_cond(cond);
1813 free(cond);
1814 out_return:
1815 return NULL;
1816}
1817
Willy Tarreau2bbba412010-01-28 16:48:33 +01001818/* Builds an ACL condition starting at the if/unless keyword. The complete
1819 * condition is returned. NULL is returned in case of error or if the first
1820 * word is neither "if" nor "unless". It automatically sets the file name and
1821 * the line number in the condition for better error reporting, and adds the
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001822 * ACL requirements to the proxy's acl_requires. If <err> is not NULL, it will
1823 * be filled with a pointer to an error message in case of error, that the
1824 * caller is responsible for freeing. The initial location must either be
1825 * freeable or NULL.
Willy Tarreau2bbba412010-01-28 16:48:33 +01001826 */
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001827struct 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 +01001828{
1829 int pol = ACL_COND_NONE;
1830 struct acl_cond *cond = NULL;
1831
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001832 if (err)
1833 *err = NULL;
1834
Willy Tarreau2bbba412010-01-28 16:48:33 +01001835 if (!strcmp(*args, "if")) {
1836 pol = ACL_COND_IF;
1837 args++;
1838 }
1839 else if (!strcmp(*args, "unless")) {
1840 pol = ACL_COND_UNLESS;
1841 args++;
1842 }
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001843 else {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001844 memprintf(err, "conditions must start with either 'if' or 'unless'");
Willy Tarreau2bbba412010-01-28 16:48:33 +01001845 return NULL;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001846 }
Willy Tarreau2bbba412010-01-28 16:48:33 +01001847
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001848 cond = parse_acl_cond(args, &px->acl, pol, err);
1849 if (!cond) {
1850 /* note that parse_acl_cond must have filled <err> here */
Willy Tarreau2bbba412010-01-28 16:48:33 +01001851 return NULL;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001852 }
Willy Tarreau2bbba412010-01-28 16:48:33 +01001853
1854 cond->file = file;
1855 cond->line = line;
1856 px->acl_requires |= cond->requires;
1857
1858 return cond;
1859}
1860
Willy Tarreau11382812008-07-09 16:18:21 +02001861/* Execute condition <cond> and return either ACL_PAT_FAIL, ACL_PAT_MISS or
Willy Tarreaub6866442008-07-14 23:54:42 +02001862 * ACL_PAT_PASS depending on the test results. ACL_PAT_MISS may only be
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001863 * returned if <opt> does not contain SMP_OPT_FINAL, indicating that incomplete
Willy Tarreau7a777ed2012-04-26 11:44:02 +02001864 * data is being examined. The function automatically sets SMP_OPT_ITERATE.
Willy Tarreaub6866442008-07-14 23:54:42 +02001865 * This function only computes the condition, it does not apply the polarity
1866 * required by IF/UNLESS, it's up to the caller to do this using something like
1867 * this :
Willy Tarreau11382812008-07-09 16:18:21 +02001868 *
1869 * res = acl_pass(res);
Willy Tarreaub6866442008-07-14 23:54:42 +02001870 * if (res == ACL_PAT_MISS)
1871 * return 0;
Willy Tarreau11382812008-07-09 16:18:21 +02001872 * if (cond->pol == ACL_COND_UNLESS)
1873 * res = !res;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001874 */
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001875int 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 +02001876{
1877 __label__ fetch_next;
1878 struct acl_term_suite *suite;
1879 struct acl_term *term;
1880 struct acl_expr *expr;
1881 struct acl *acl;
1882 struct acl_pattern *pattern;
Willy Tarreau37406352012-04-23 16:16:37 +02001883 struct sample smp;
Willy Tarreau11382812008-07-09 16:18:21 +02001884 int acl_res, suite_res, cond_res;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001885
Willy Tarreau7a777ed2012-04-26 11:44:02 +02001886 /* ACLs are iterated over all values, so let's always set the flag to
1887 * indicate this to the fetch functions.
1888 */
1889 opt |= SMP_OPT_ITERATE;
1890
Willy Tarreau11382812008-07-09 16:18:21 +02001891 /* We're doing a logical OR between conditions so we initialize to FAIL.
1892 * The MISS status is propagated down from the suites.
1893 */
Willy Tarreaua84d3742007-05-07 00:36:48 +02001894 cond_res = ACL_PAT_FAIL;
1895 list_for_each_entry(suite, &cond->suites, list) {
Willy Tarreau11382812008-07-09 16:18:21 +02001896 /* Evaluate condition suite <suite>. We stop at the first term
1897 * which returns ACL_PAT_FAIL. The MISS status is still propagated
1898 * in case of uncertainty in the result.
Willy Tarreaua84d3742007-05-07 00:36:48 +02001899 */
1900
1901 /* we're doing a logical AND between terms, so we must set the
1902 * initial value to PASS.
1903 */
1904 suite_res = ACL_PAT_PASS;
1905 list_for_each_entry(term, &suite->terms, list) {
1906 acl = term->acl;
1907
1908 /* FIXME: use cache !
1909 * check acl->cache_idx for this.
1910 */
1911
1912 /* ACL result not cached. Let's scan all the expressions
1913 * and use the first one to match.
1914 */
1915 acl_res = ACL_PAT_FAIL;
1916 list_for_each_entry(expr, &acl->expr, list) {
Willy Tarreaud41f8d82007-06-10 10:06:18 +02001917 /* we need to reset context and flags */
Willy Tarreau37406352012-04-23 16:16:37 +02001918 memset(&smp, 0, sizeof(smp));
Willy Tarreaua84d3742007-05-07 00:36:48 +02001919 fetch_next:
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001920 if (!expr->kw->fetch(px, l4, l7, opt, expr->args, &smp)) {
Willy Tarreaub6866442008-07-14 23:54:42 +02001921 /* maybe we could not fetch because of missing data */
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001922 if (smp.flags & SMP_F_MAY_CHANGE && !(opt & SMP_OPT_FINAL))
Willy Tarreaub6866442008-07-14 23:54:42 +02001923 acl_res |= ACL_PAT_MISS;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001924 continue;
Willy Tarreaub6866442008-07-14 23:54:42 +02001925 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001926
Willy Tarreau197e10a2012-04-23 19:18:42 +02001927 if (smp.type == SMP_T_BOOL) {
1928 if (smp.data.uint)
Willy Tarreaua79534f2008-07-20 10:13:37 +02001929 acl_res |= ACL_PAT_PASS;
1930 else
1931 acl_res |= ACL_PAT_FAIL;
1932 }
1933 else {
Willy Tarreau020534d2010-05-16 21:45:45 +02001934 if (!eb_is_empty(&expr->pattern_tree)) {
Willy Tarreauc4262962010-05-10 23:42:40 +02001935 /* a tree is present, let's check what type it is */
1936 if (expr->kw->match == acl_match_str)
Willy Tarreau37406352012-04-23 16:16:37 +02001937 acl_res |= acl_lookup_str(&smp, expr) ? ACL_PAT_PASS : ACL_PAT_FAIL;
Willy Tarreaub337b532010-05-13 20:03:41 +02001938 else if (expr->kw->match == acl_match_ip)
Willy Tarreau37406352012-04-23 16:16:37 +02001939 acl_res |= acl_lookup_ip(&smp, expr) ? ACL_PAT_PASS : ACL_PAT_FAIL;
Willy Tarreauc4262962010-05-10 23:42:40 +02001940 }
1941
Willy Tarreaua79534f2008-07-20 10:13:37 +02001942 /* call the match() function for all tests on this value */
1943 list_for_each_entry(pattern, &expr->patterns, list) {
Willy Tarreaua79534f2008-07-20 10:13:37 +02001944 if (acl_res == ACL_PAT_PASS)
1945 break;
Willy Tarreau37406352012-04-23 16:16:37 +02001946 acl_res |= expr->kw->match(&smp, pattern);
Willy Tarreaua79534f2008-07-20 10:13:37 +02001947 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001948 }
1949 /*
Willy Tarreau11382812008-07-09 16:18:21 +02001950 * OK now acl_res holds the result of this expression
1951 * as one of ACL_PAT_FAIL, ACL_PAT_MISS or ACL_PAT_PASS.
Willy Tarreaua84d3742007-05-07 00:36:48 +02001952 *
Willy Tarreau11382812008-07-09 16:18:21 +02001953 * Then if (!MISS) we can cache the result, and put
Willy Tarreau37406352012-04-23 16:16:37 +02001954 * (smp.flags & SMP_F_VOLATILE) in the cache flags.
Willy Tarreaua84d3742007-05-07 00:36:48 +02001955 *
1956 * FIXME: implement cache.
1957 *
1958 */
1959
Willy Tarreau11382812008-07-09 16:18:21 +02001960 /* we're ORing these terms, so a single PASS is enough */
1961 if (acl_res == ACL_PAT_PASS)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001962 break;
1963
Willy Tarreau37406352012-04-23 16:16:37 +02001964 if (smp.flags & SMP_F_NOT_LAST)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001965 goto fetch_next;
Willy Tarreaub6866442008-07-14 23:54:42 +02001966
1967 /* sometimes we know the fetched data is subject to change
1968 * later and give another chance for a new match (eg: request
1969 * size, time, ...)
1970 */
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001971 if (smp.flags & SMP_F_MAY_CHANGE && !(opt & SMP_OPT_FINAL))
Willy Tarreaub6866442008-07-14 23:54:42 +02001972 acl_res |= ACL_PAT_MISS;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001973 }
1974 /*
1975 * Here we have the result of an ACL (cached or not).
1976 * ACLs are combined, negated or not, to form conditions.
1977 */
1978
Willy Tarreaua84d3742007-05-07 00:36:48 +02001979 if (term->neg)
Willy Tarreau11382812008-07-09 16:18:21 +02001980 acl_res = acl_neg(acl_res);
Willy Tarreaua84d3742007-05-07 00:36:48 +02001981
1982 suite_res &= acl_res;
Willy Tarreau11382812008-07-09 16:18:21 +02001983
1984 /* we're ANDing these terms, so a single FAIL is enough */
1985 if (suite_res == ACL_PAT_FAIL)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001986 break;
1987 }
1988 cond_res |= suite_res;
Willy Tarreau11382812008-07-09 16:18:21 +02001989
1990 /* we're ORing these terms, so a single PASS is enough */
1991 if (cond_res == ACL_PAT_PASS)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001992 break;
1993 }
Willy Tarreau11382812008-07-09 16:18:21 +02001994 return cond_res;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001995}
1996
1997
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001998/* Reports a pointer to the first ACL used in condition <cond> which requires
1999 * at least one of the USE_FLAGS in <require>. Returns NULL if none matches.
2000 * The construct is almost the same as for acl_exec_cond() since we're walking
2001 * down the ACL tree as well. It is important that the tree is really walked
2002 * through and never cached, because that way, this function can be used as a
2003 * late check.
2004 */
Willy Tarreauf1e98b82010-01-28 17:59:39 +01002005struct acl *cond_find_require(const struct acl_cond *cond, unsigned int require)
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02002006{
2007 struct acl_term_suite *suite;
2008 struct acl_term *term;
2009 struct acl *acl;
2010
2011 list_for_each_entry(suite, &cond->suites, list) {
2012 list_for_each_entry(term, &suite->terms, list) {
2013 acl = term->acl;
2014 if (acl->requires & require)
2015 return acl;
2016 }
2017 }
2018 return NULL;
2019}
2020
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01002021/*
2022 * Find targets for userlist and groups in acl. Function returns the number
2023 * of errors or OK if everything is fine.
2024 */
2025int
2026acl_find_targets(struct proxy *p)
2027{
2028
2029 struct acl *acl;
2030 struct acl_expr *expr;
2031 struct acl_pattern *pattern;
2032 struct userlist *ul;
Willy Tarreau63364ee2012-04-19 19:11:13 +02002033 struct arg *arg;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01002034 int cfgerr = 0;
2035
2036 list_for_each_entry(acl, &p->acl, list) {
2037 list_for_each_entry(expr, &acl->expr, list) {
Willy Tarreau2e845be2012-10-19 19:49:09 +02002038 for (arg = expr->args; arg && arg->type != ARGT_STOP; arg++) {
2039 if (!arg->unresolved)
Willy Tarreau496aa012012-06-01 10:38:29 +02002040 continue;
Willy Tarreau63364ee2012-04-19 19:11:13 +02002041 else if (arg->type == ARGT_SRV) {
2042 struct proxy *px;
2043 struct server *srv;
2044 char *pname, *sname;
Willy Tarreau0b1cd942010-05-16 22:18:27 +02002045
Willy Tarreau63364ee2012-04-19 19:11:13 +02002046 if (!expr->args->data.str.len) {
2047 Alert("proxy %s: acl '%s' %s(): missing server name.\n",
2048 p->id, acl->name, expr->kw->kw);
2049 cfgerr++;
2050 continue;
2051 }
Willy Tarreau0b1cd942010-05-16 22:18:27 +02002052
Willy Tarreau63364ee2012-04-19 19:11:13 +02002053 pname = expr->args->data.str.str;
2054 sname = strrchr(pname, '/');
Willy Tarreau0b1cd942010-05-16 22:18:27 +02002055
Willy Tarreau63364ee2012-04-19 19:11:13 +02002056 if (sname)
2057 *sname++ = '\0';
2058 else {
2059 sname = pname;
2060 pname = NULL;
2061 }
2062
2063 px = p;
2064 if (pname) {
2065 px = findproxy(pname, PR_CAP_BE);
2066 if (!px) {
2067 Alert("proxy %s: acl '%s' %s(): unable to find proxy '%s'.\n",
2068 p->id, acl->name, expr->kw->kw, pname);
2069 cfgerr++;
2070 continue;
2071 }
2072 }
Willy Tarreau0b1cd942010-05-16 22:18:27 +02002073
Willy Tarreau63364ee2012-04-19 19:11:13 +02002074 srv = findserver(px, sname);
2075 if (!srv) {
2076 Alert("proxy %s: acl '%s' %s(): unable to find server '%s'.\n",
2077 p->id, acl->name, expr->kw->kw, sname);
Willy Tarreau0b1cd942010-05-16 22:18:27 +02002078 cfgerr++;
2079 continue;
2080 }
Willy Tarreau0b1cd942010-05-16 22:18:27 +02002081
Willy Tarreau63364ee2012-04-19 19:11:13 +02002082 free(expr->args->data.str.str);
Willy Tarreau496aa012012-06-01 10:38:29 +02002083 expr->args->data.str.str = NULL;
2084 arg->unresolved = 0;
Willy Tarreau63364ee2012-04-19 19:11:13 +02002085 expr->args->data.srv = srv;
Willy Tarreaud28c3532012-04-19 19:28:33 +02002086 }
2087 else if (arg->type == ARGT_FE) {
Willy Tarreaufc2c1fd2012-04-19 23:35:54 +02002088 struct proxy *prx = p;
2089 char *pname = p->id;
Willy Tarreaud28c3532012-04-19 19:28:33 +02002090
Willy Tarreaufc2c1fd2012-04-19 23:35:54 +02002091 if (expr->args->data.str.len) {
2092 pname = expr->args->data.str.str;
2093 prx = findproxy(pname, PR_CAP_FE);
Willy Tarreaud28c3532012-04-19 19:28:33 +02002094 }
2095
Willy Tarreaud28c3532012-04-19 19:28:33 +02002096 if (!prx) {
2097 Alert("proxy %s: acl '%s' %s(): unable to find frontend '%s'.\n",
2098 p->id, acl->name, expr->kw->kw, pname);
2099 cfgerr++;
2100 continue;
2101 }
2102
Willy Tarreaufc2c1fd2012-04-19 23:35:54 +02002103 if (!(prx->cap & PR_CAP_FE)) {
2104 Alert("proxy %s: acl '%s' %s(): proxy '%s' has no frontend capability.\n",
2105 p->id, acl->name, expr->kw->kw, pname);
2106 cfgerr++;
2107 continue;
2108 }
2109
Willy Tarreaud28c3532012-04-19 19:28:33 +02002110 free(expr->args->data.str.str);
Willy Tarreau496aa012012-06-01 10:38:29 +02002111 expr->args->data.str.str = NULL;
2112 arg->unresolved = 0;
Willy Tarreaud28c3532012-04-19 19:28:33 +02002113 expr->args->data.prx = prx;
2114 }
2115 else if (arg->type == ARGT_BE) {
Willy Tarreaufc2c1fd2012-04-19 23:35:54 +02002116 struct proxy *prx = p;
2117 char *pname = p->id;
Willy Tarreaud28c3532012-04-19 19:28:33 +02002118
Willy Tarreaufc2c1fd2012-04-19 23:35:54 +02002119 if (expr->args->data.str.len) {
2120 pname = expr->args->data.str.str;
2121 prx = findproxy(pname, PR_CAP_BE);
Willy Tarreaud28c3532012-04-19 19:28:33 +02002122 }
2123
Willy Tarreaud28c3532012-04-19 19:28:33 +02002124 if (!prx) {
2125 Alert("proxy %s: acl '%s' %s(): unable to find backend '%s'.\n",
2126 p->id, acl->name, expr->kw->kw, pname);
2127 cfgerr++;
2128 continue;
2129 }
2130
Willy Tarreaufc2c1fd2012-04-19 23:35:54 +02002131 if (!(prx->cap & PR_CAP_BE)) {
2132 Alert("proxy %s: acl '%s' %s(): proxy '%s' has no backend capability.\n",
2133 p->id, acl->name, expr->kw->kw, pname);
2134 cfgerr++;
2135 continue;
2136 }
2137
Willy Tarreaud28c3532012-04-19 19:28:33 +02002138 free(expr->args->data.str.str);
Willy Tarreau496aa012012-06-01 10:38:29 +02002139 expr->args->data.str.str = NULL;
2140 arg->unresolved = 0;
Willy Tarreaud28c3532012-04-19 19:28:33 +02002141 expr->args->data.prx = prx;
2142 }
2143 else if (arg->type == ARGT_TAB) {
Willy Tarreaufc2c1fd2012-04-19 23:35:54 +02002144 struct proxy *prx = p;
2145 char *pname = p->id;
Willy Tarreaud28c3532012-04-19 19:28:33 +02002146
Willy Tarreaufc2c1fd2012-04-19 23:35:54 +02002147 if (expr->args->data.str.len) {
2148 pname = expr->args->data.str.str;
2149 prx = find_stktable(pname);
Willy Tarreaud28c3532012-04-19 19:28:33 +02002150 }
2151
Willy Tarreaud28c3532012-04-19 19:28:33 +02002152 if (!prx) {
2153 Alert("proxy %s: acl '%s' %s(): unable to find table '%s'.\n",
2154 p->id, acl->name, expr->kw->kw, pname);
2155 cfgerr++;
2156 continue;
2157 }
2158
Willy Tarreaufc2c1fd2012-04-19 23:35:54 +02002159
2160 if (!prx->table.size) {
2161 Alert("proxy %s: acl '%s' %s(): no table in proxy '%s'.\n",
2162 p->id, acl->name, expr->kw->kw, pname);
2163 cfgerr++;
2164 continue;
2165 }
2166
Willy Tarreaud28c3532012-04-19 19:28:33 +02002167 free(expr->args->data.str.str);
Willy Tarreau496aa012012-06-01 10:38:29 +02002168 expr->args->data.str.str = NULL;
2169 arg->unresolved = 0;
Willy Tarreaud28c3532012-04-19 19:28:33 +02002170 expr->args->data.prx = prx;
Willy Tarreau0b1cd942010-05-16 22:18:27 +02002171 }
Willy Tarreau63364ee2012-04-19 19:11:13 +02002172 else if (arg->type == ARGT_USR) {
2173 if (!expr->args->data.str.len) {
2174 Alert("proxy %s: acl '%s' %s(): missing userlist name.\n",
2175 p->id, acl->name, expr->kw->kw);
2176 cfgerr++;
2177 continue;
2178 }
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01002179
Willy Tarreau63364ee2012-04-19 19:11:13 +02002180 if (p->uri_auth && p->uri_auth->userlist &&
2181 !strcmp(p->uri_auth->userlist->name, expr->args->data.str.str))
2182 ul = p->uri_auth->userlist;
2183 else
2184 ul = auth_find_userlist(expr->args->data.str.str);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01002185
Willy Tarreau63364ee2012-04-19 19:11:13 +02002186 if (!ul) {
2187 Alert("proxy %s: acl '%s' %s(%s): unable to find userlist.\n",
2188 p->id, acl->name, expr->kw->kw, expr->args->data.str.str);
2189 cfgerr++;
2190 continue;
2191 }
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01002192
Willy Tarreau63364ee2012-04-19 19:11:13 +02002193 free(expr->args->data.str.str);
Willy Tarreau496aa012012-06-01 10:38:29 +02002194 expr->args->data.str.str = NULL;
2195 arg->unresolved = 0;
Willy Tarreau63364ee2012-04-19 19:11:13 +02002196 expr->args->data.usr = ul;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01002197 }
Willy Tarreau63364ee2012-04-19 19:11:13 +02002198 } /* end of args processing */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01002199
Willy Tarreau46b39d02012-05-10 23:40:14 +02002200 /* don't try to resolve groups if we're not certain of having
2201 * resolved userlists first.
2202 */
2203 if (cfgerr)
2204 break;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01002205
2206 if (!strcmp(expr->kw->kw, "http_auth_group")) {
Willy Tarreau63364ee2012-04-19 19:11:13 +02002207 /* note: argument resolved above thanks to ARGT_USR */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01002208
2209 if (LIST_ISEMPTY(&expr->patterns)) {
2210 Alert("proxy %s: acl %s %s(): no groups specified.\n",
2211 p->id, acl->name, expr->kw->kw);
2212 cfgerr++;
2213 continue;
2214 }
2215
2216 list_for_each_entry(pattern, &expr->patterns, list) {
Willy Tarreau34db1082012-04-19 17:16:54 +02002217 pattern->val.group_mask = auth_resolve_groups(expr->args->data.usr, pattern->ptr.str);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01002218
2219 free(pattern->ptr.str);
2220 pattern->ptr.str = NULL;
2221 pattern->len = 0;
2222
2223 if (!pattern->val.group_mask) {
2224 Alert("proxy %s: acl %s %s(): invalid group(s).\n",
2225 p->id, acl->name, expr->kw->kw);
2226 cfgerr++;
2227 continue;
2228 }
2229 }
2230 }
2231 }
2232 }
2233
2234 return cfgerr;
2235}
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02002236
Willy Tarreaua84d3742007-05-07 00:36:48 +02002237/************************************************************************/
2238/* All supported keywords must be declared here. */
2239/************************************************************************/
2240
Willy Tarreau61612d42012-04-19 18:42:05 +02002241/* Note: must not be declared <const> as its list will be overwritten.
2242 * Please take care of keeping this list alphabetically sorted.
2243 */
Willy Tarreaua84d3742007-05-07 00:36:48 +02002244static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau61612d42012-04-19 18:42:05 +02002245 { "always_false", acl_parse_nothing, acl_fetch_false, acl_match_nothing, ACL_USE_NOTHING, 0 },
2246 { "always_true", acl_parse_nothing, acl_fetch_true, acl_match_nothing, ACL_USE_NOTHING, 0 },
2247 { "rep_ssl_hello_type", acl_parse_int, acl_fetch_ssl_hello_type, acl_match_int, ACL_USE_L6RTR_VOLATILE, 0 },
2248 { "req_len", acl_parse_int, acl_fetch_req_len, acl_match_int, ACL_USE_L6REQ_VOLATILE, 0 },
Willy Tarreau61612d42012-04-19 18:42:05 +02002249 { "req_ssl_hello_type", acl_parse_int, acl_fetch_ssl_hello_type, acl_match_int, ACL_USE_L6REQ_VOLATILE, 0 },
2250 { "req_ssl_sni", acl_parse_str, acl_fetch_ssl_hello_sni, acl_match_str, ACL_USE_L6REQ_VOLATILE|ACL_MAY_LOOKUP, 0 },
2251 { "req_ssl_ver", acl_parse_dotted_ver, acl_fetch_req_ssl_ver, acl_match_int, ACL_USE_L6REQ_VOLATILE, 0 },
2252 { "wait_end", acl_parse_nothing, acl_fetch_wait_end, acl_match_nothing, ACL_USE_NOTHING, 0 },
Willy Tarreaua84d3742007-05-07 00:36:48 +02002253 { NULL, NULL, NULL, NULL }
2254}};
2255
2256
2257__attribute__((constructor))
2258static void __acl_init(void)
2259{
2260 acl_register_keywords(&acl_kws);
2261}
2262
2263
2264/*
2265 * Local variables:
2266 * c-indent-level: 8
2267 * c-basic-offset: 8
2268 * End:
2269 */