blob: 8572bd174cdaa20d96c969cb1ee231f174ea9a47 [file] [log] [blame]
Willy Tarreaua84d3742007-05-07 00:36:48 +02001/*
2 * ACL management functions.
3 *
Willy Tarreau0e698542011-09-16 08:32:32 +02004 * Copyright 2000-2011 Willy Tarreau <w@1wt.eu>
Willy Tarreaua84d3742007-05-07 00:36:48 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
Willy Tarreauae8b7962007-06-09 23:10:04 +020013#include <ctype.h>
Willy Tarreaua84d3742007-05-07 00:36:48 +020014#include <stdio.h>
15#include <string.h>
16
17#include <common/config.h>
18#include <common/mini-clist.h>
19#include <common/standard.h>
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +010020#include <common/uri_auth.h>
Willy Tarreaua84d3742007-05-07 00:36:48 +020021
Willy Tarreau2b5285d2010-05-09 23:45:24 +020022#include <types/global.h>
23
Willy Tarreaua84d3742007-05-07 00:36:48 +020024#include <proto/acl.h>
Willy Tarreau34db1082012-04-19 17:16:54 +020025#include <proto/arg.h>
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010026#include <proto/auth.h>
Willy Tarreau44b90cc2010-05-24 20:27:29 +020027#include <proto/buffers.h>
Willy Tarreau404e8ab2009-07-26 19:40:40 +020028#include <proto/log.h>
Willy Tarreau0b1cd942010-05-16 22:18:27 +020029#include <proto/proxy.h>
Willy Tarreaud28c3532012-04-19 19:28:33 +020030#include <proto/stick_table.h>
Willy Tarreaua84d3742007-05-07 00:36:48 +020031
Willy Tarreauc4262962010-05-10 23:42:40 +020032#include <ebsttree.h>
33
Willy Tarreaua9802632008-07-25 19:13:19 +020034/* The capabilities of filtering hooks describe the type of information
35 * available to each of them.
36 */
37const unsigned int filt_cap[] = {
38 [ACL_HOOK_REQ_FE_TCP] = ACL_USE_TCP4_ANY|ACL_USE_TCP6_ANY|ACL_USE_TCP_ANY,
Willy Tarreau06457872010-05-23 12:24:38 +020039 [ACL_HOOK_REQ_FE_TCP_CONTENT] = ACL_USE_TCP4_ANY|ACL_USE_TCP6_ANY|ACL_USE_TCP_ANY|ACL_USE_L6REQ_ANY,
40 [ACL_HOOK_REQ_FE_HTTP_IN] = ACL_USE_TCP4_ANY|ACL_USE_TCP6_ANY|ACL_USE_TCP_ANY|ACL_USE_L6REQ_ANY|ACL_USE_L7REQ_ANY|ACL_USE_HDR_ANY,
41 [ACL_HOOK_REQ_FE_SWITCH] = ACL_USE_TCP4_ANY|ACL_USE_TCP6_ANY|ACL_USE_TCP_ANY|ACL_USE_L6REQ_ANY|ACL_USE_L7REQ_ANY|ACL_USE_HDR_ANY,
42 [ACL_HOOK_REQ_BE_TCP_CONTENT] = ACL_USE_TCP4_ANY|ACL_USE_TCP6_ANY|ACL_USE_TCP_ANY|ACL_USE_L6REQ_ANY|ACL_USE_L7REQ_ANY|ACL_USE_HDR_ANY,
43 [ACL_HOOK_REQ_BE_HTTP_IN] = ACL_USE_TCP4_ANY|ACL_USE_TCP6_ANY|ACL_USE_TCP_ANY|ACL_USE_L6REQ_ANY|ACL_USE_L7REQ_ANY|ACL_USE_HDR_ANY,
44 [ACL_HOOK_REQ_BE_SWITCH] = ACL_USE_TCP4_ANY|ACL_USE_TCP6_ANY|ACL_USE_TCP_ANY|ACL_USE_L6REQ_ANY|ACL_USE_L7REQ_ANY|ACL_USE_HDR_ANY,
45 [ACL_HOOK_REQ_FE_HTTP_OUT] = ACL_USE_TCP4_ANY|ACL_USE_TCP6_ANY|ACL_USE_TCP_ANY|ACL_USE_L6REQ_ANY|ACL_USE_L7REQ_ANY|ACL_USE_HDR_ANY,
46 [ACL_HOOK_REQ_BE_HTTP_OUT] = ACL_USE_TCP4_ANY|ACL_USE_TCP6_ANY|ACL_USE_TCP_ANY|ACL_USE_L6REQ_ANY|ACL_USE_L7REQ_ANY|ACL_USE_HDR_ANY,
Willy Tarreaua9802632008-07-25 19:13:19 +020047
Willy Tarreau06457872010-05-23 12:24:38 +020048 [ACL_HOOK_RTR_BE_TCP_CONTENT] = ACL_USE_REQ_PERMANENT|ACL_USE_REQ_CACHEABLE|ACL_USE_L6RTR_ANY,
49 [ACL_HOOK_RTR_BE_HTTP_IN] = ACL_USE_REQ_PERMANENT|ACL_USE_REQ_CACHEABLE|ACL_USE_L6RTR_ANY|ACL_USE_L7RTR_ANY,
50 [ACL_HOOK_RTR_FE_TCP_CONTENT] = ACL_USE_REQ_PERMANENT|ACL_USE_REQ_CACHEABLE|ACL_USE_L6RTR_ANY|ACL_USE_L7RTR_ANY,
51 [ACL_HOOK_RTR_FE_HTTP_IN] = ACL_USE_REQ_PERMANENT|ACL_USE_REQ_CACHEABLE|ACL_USE_L6RTR_ANY|ACL_USE_L7RTR_ANY,
52 [ACL_HOOK_RTR_BE_HTTP_OUT] = ACL_USE_REQ_PERMANENT|ACL_USE_REQ_CACHEABLE|ACL_USE_L6RTR_ANY|ACL_USE_L7RTR_ANY,
53 [ACL_HOOK_RTR_FE_HTTP_OUT] = ACL_USE_REQ_PERMANENT|ACL_USE_REQ_CACHEABLE|ACL_USE_L6RTR_ANY|ACL_USE_L7RTR_ANY,
Willy Tarreaua9802632008-07-25 19:13:19 +020054};
55
Willy Tarreaua84d3742007-05-07 00:36:48 +020056/* List head of all known ACL keywords */
57static struct acl_kw_list acl_keywords = {
58 .list = LIST_HEAD_INIT(acl_keywords.list)
59};
60
61
Willy Tarreaua5909832007-06-17 20:40:25 +020062/*
63 * These functions are only used for debugging complex configurations.
Willy Tarreaua84d3742007-05-07 00:36:48 +020064 */
Willy Tarreaua5909832007-06-17 20:40:25 +020065
Willy Tarreau58393e12008-07-20 10:39:22 +020066/* force TRUE to be returned at the fetch level */
Willy Tarreaua5909832007-06-17 20:40:25 +020067static int
Willy 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;
111 smp->data.uint = l4->req->i;
Willy Tarreau37406352012-04-23 16:16:37 +0200112 smp->flags = SMP_F_VOLATILE | SMP_F_MAY_CHANGE;
Willy Tarreau44b90cc2010-05-24 20:27:29 +0200113 return 1;
114}
115
Emeric Brun38e71762010-09-23 17:59:18 +0200116
117static int
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;
123 struct buffer *b;
124 const unsigned char *data;
125
126 if (!l4)
127 goto not_ssl_hello;
128
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200129 b = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? l4->rep : l4->req;
Emeric Brun38e71762010-09-23 17:59:18 +0200130
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100131 bleft = b->i;
Willy Tarreau89fa7062012-03-02 16:13:16 +0100132 data = (const unsigned char *)b->p;
Emeric Brun38e71762010-09-23 17:59:18 +0200133
134 if (!bleft)
135 goto too_short;
136
137 if ((*data >= 0x14 && *data <= 0x17) || (*data == 0xFF)) {
138 /* SSLv3 header format */
139 if (bleft < 9)
140 goto too_short;
141
142 /* ssl version 3 */
143 if ((data[1] << 16) + data[2] < 0x00030000)
144 goto not_ssl_hello;
145
146 /* ssl message len must present handshake type and len */
147 if ((data[3] << 8) + data[4] < 4)
148 goto not_ssl_hello;
149
150 /* format introduced with SSLv3 */
151
152 hs_type = (int)data[5];
153 hs_len = ( data[6] << 16 ) + ( data[7] << 8 ) + data[8];
154
155 /* not a full handshake */
156 if (bleft < (9 + hs_len))
157 goto too_short;
158
159 }
160 else {
161 goto not_ssl_hello;
162 }
163
Willy Tarreauf853c462012-04-23 18:53:56 +0200164 smp->type = SMP_T_UINT;
165 smp->data.uint = hs_type;
Willy Tarreau37406352012-04-23 16:16:37 +0200166 smp->flags = SMP_F_VOLATILE;
Emeric Brun38e71762010-09-23 17:59:18 +0200167
168 return 1;
169
170 too_short:
Willy Tarreau37406352012-04-23 16:16:37 +0200171 smp->flags = SMP_F_MAY_CHANGE;
Emeric Brun38e71762010-09-23 17:59:18 +0200172
173 not_ssl_hello:
174
175 return 0;
176}
177
Willy Tarreau44b90cc2010-05-24 20:27:29 +0200178/* Return the version of the SSL protocol in the request. It supports both
179 * SSLv3 (TLSv1) header format for any message, and SSLv2 header format for
180 * the hello message. The SSLv3 format is described in RFC 2246 p49, and the
181 * SSLv2 format is described here, and completed p67 of RFC 2246 :
182 * http://wp.netscape.com/eng/security/SSL_2.html
183 *
184 * Note: this decoder only works with non-wrapping data.
185 */
186static int
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 Tarreau02d6cfc2012-03-01 18:19:58 +0100197 bleft = l4->req->i;
Willy Tarreau44b90cc2010-05-24 20:27:29 +0200198 if (!bleft)
199 goto too_short;
200
Willy Tarreau89fa7062012-03-02 16:13:16 +0100201 data = (const unsigned char *)l4->req->p;
Willy Tarreau44b90cc2010-05-24 20:27:29 +0200202 if ((*data >= 0x14 && *data <= 0x17) || (*data == 0xFF)) {
203 /* SSLv3 header format */
204 if (bleft < 5)
205 goto too_short;
206
207 version = (data[1] << 16) + data[2]; /* version: major, minor */
208 msg_len = (data[3] << 8) + data[4]; /* record length */
209
210 /* format introduced with SSLv3 */
211 if (version < 0x00030000)
212 goto not_ssl;
213
214 /* message length between 1 and 2^14 + 2048 */
215 if (msg_len < 1 || msg_len > ((1<<14) + 2048))
216 goto not_ssl;
217
218 bleft -= 5; data += 5;
219 } else {
220 /* SSLv2 header format, only supported for hello (msg type 1) */
221 int rlen, plen, cilen, silen, chlen;
222
223 if (*data & 0x80) {
224 if (bleft < 3)
225 goto too_short;
226 /* short header format : 15 bits for length */
227 rlen = ((data[0] & 0x7F) << 8) | data[1];
228 plen = 0;
229 bleft -= 2; data += 2;
230 } else {
231 if (bleft < 4)
232 goto too_short;
233 /* long header format : 14 bits for length + pad length */
234 rlen = ((data[0] & 0x3F) << 8) | data[1];
235 plen = data[2];
236 bleft -= 3; data += 2;
237 }
238
239 if (*data != 0x01)
240 goto not_ssl;
241 bleft--; data++;
242
243 if (bleft < 8)
244 goto too_short;
245 version = (data[0] << 16) + data[1]; /* version: major, minor */
246 cilen = (data[2] << 8) + data[3]; /* cipher len, multiple of 3 */
247 silen = (data[4] << 8) + data[5]; /* session_id_len: 0 or 16 */
248 chlen = (data[6] << 8) + data[7]; /* 16<=challenge length<=32 */
249
250 bleft -= 8; data += 8;
251 if (cilen % 3 != 0)
252 goto not_ssl;
253 if (silen && silen != 16)
254 goto not_ssl;
255 if (chlen < 16 || chlen > 32)
256 goto not_ssl;
257 if (rlen != 9 + cilen + silen + chlen)
258 goto not_ssl;
259
260 /* focus on the remaining data length */
261 msg_len = cilen + silen + chlen + plen;
262 }
263 /* We could recursively check that the buffer ends exactly on an SSL
264 * fragment boundary and that a possible next segment is still SSL,
265 * but that's a bit pointless. However, we could still check that
266 * all the part of the request which fits in a buffer is already
267 * there.
268 */
Willy Tarreau89fa7062012-03-02 16:13:16 +0100269 if (msg_len > buffer_max_len(l4->req) + l4->req->data - l4->req->p)
270 msg_len = buffer_max_len(l4->req) + l4->req->data - l4->req->p;
Willy Tarreau44b90cc2010-05-24 20:27:29 +0200271
272 if (bleft < msg_len)
273 goto too_short;
274
275 /* OK that's enough. We have at least the whole message, and we have
276 * the protocol version.
277 */
Willy Tarreauf853c462012-04-23 18:53:56 +0200278 smp->type = SMP_T_UINT;
279 smp->data.uint = version;
Willy Tarreau37406352012-04-23 16:16:37 +0200280 smp->flags = SMP_F_VOLATILE;
Willy Tarreau44b90cc2010-05-24 20:27:29 +0200281 return 1;
282
283 too_short:
Willy Tarreau37406352012-04-23 16:16:37 +0200284 smp->flags = SMP_F_MAY_CHANGE;
Willy Tarreau44b90cc2010-05-24 20:27:29 +0200285 not_ssl:
286 return 0;
287}
288
Willy Tarreaub6672b52011-12-12 17:23:41 +0100289/* Try to extract the Server Name Indication that may be presented in a TLS
290 * client hello handshake message. The format of the message is the following
291 * (cf RFC5246 + RFC6066) :
292 * TLS frame :
293 * - uint8 type = 0x16 (Handshake)
294 * - uint16 version >= 0x0301 (TLSv1)
295 * - uint16 length (frame length)
296 * - TLS handshake :
297 * - uint8 msg_type = 0x01 (ClientHello)
298 * - uint24 length (handshake message length)
299 * - ClientHello :
300 * - uint16 client_version >= 0x0301 (TLSv1)
Willy Tarreaud017f112012-04-09 09:24:11 +0200301 * - uint8 Random[32] (4 first ones are timestamp)
Willy Tarreaub6672b52011-12-12 17:23:41 +0100302 * - SessionID :
303 * - uint8 session_id_len (0..32) (SessionID len in bytes)
304 * - uint8 session_id[session_id_len]
305 * - CipherSuite :
306 * - uint16 cipher_len >= 2 (Cipher length in bytes)
307 * - uint16 ciphers[cipher_len/2]
308 * - CompressionMethod :
309 * - uint8 compression_len >= 1 (# of supported methods)
310 * - uint8 compression_methods[compression_len]
311 * - optional client_extension_len (in bytes)
312 * - optional sequence of ClientHelloExtensions (as many bytes as above):
313 * - uint16 extension_type = 0 for server_name
314 * - uint16 extension_len
315 * - opaque extension_data[extension_len]
316 * - uint16 server_name_list_len (# of bytes here)
317 * - opaque server_names[server_name_list_len bytes]
318 * - uint8 name_type = 0 for host_name
319 * - uint16 name_len
320 * - opaque hostname[name_len bytes]
321 */
322static int
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;
327 struct buffer *b;
328 unsigned char *data;
329
330 if (!l4)
331 goto not_ssl_hello;
332
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200333 b = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? l4->rep : l4->req;
Willy Tarreaub6672b52011-12-12 17:23:41 +0100334
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100335 bleft = b->i;
Willy Tarreau89fa7062012-03-02 16:13:16 +0100336 data = (unsigned char *)b->p;
Willy Tarreaub6672b52011-12-12 17:23:41 +0100337
338 /* Check for SSL/TLS Handshake */
339 if (!bleft)
340 goto too_short;
341 if (*data != 0x16)
342 goto not_ssl_hello;
343
344 /* Check for TLSv1 or later (SSL version >= 3.1) */
345 if (bleft < 3)
346 goto too_short;
347 if (data[1] < 0x03 || data[2] < 0x01)
348 goto not_ssl_hello;
349
350 if (bleft < 5)
351 goto too_short;
352 hs_len = (data[3] << 8) + data[4];
353 if (hs_len < 1 + 3 + 2 + 32 + 1 + 2 + 2 + 1 + 1 + 2 + 2)
354 goto not_ssl_hello; /* too short to have an extension */
355
356 data += 5; /* enter TLS handshake */
357 bleft -= 5;
358
359 /* Check for a complete client hello starting at <data> */
360 if (bleft < 1)
361 goto too_short;
362 if (data[0] != 0x01) /* msg_type = Client Hello */
363 goto not_ssl_hello;
364
365 /* Check the Hello's length */
366 if (bleft < 4)
367 goto too_short;
368 hs_len = (data[1] << 16) + (data[2] << 8) + data[3];
369 if (hs_len < 2 + 32 + 1 + 2 + 2 + 1 + 1 + 2 + 2)
370 goto not_ssl_hello; /* too short to have an extension */
371
372 /* We want the full handshake here */
373 if (bleft < hs_len)
374 goto too_short;
375
376 data += 4;
377 /* Start of the ClientHello message */
378 if (data[0] < 0x03 || data[1] < 0x01) /* TLSv1 minimum */
379 goto not_ssl_hello;
380
Willy Tarreaud017f112012-04-09 09:24:11 +0200381 ext_len = data[34]; /* session_id_len */
Willy Tarreaub6672b52011-12-12 17:23:41 +0100382 if (ext_len > 32 || ext_len > (hs_len - 35)) /* check for correct session_id len */
383 goto not_ssl_hello;
384
385 /* Jump to cipher suite */
386 hs_len -= 35 + ext_len;
387 data += 35 + ext_len;
388
389 if (hs_len < 4 || /* minimum one cipher */
390 (ext_len = (data[0] << 8) + data[1]) < 2 || /* minimum 2 bytes for a cipher */
391 ext_len > hs_len)
392 goto not_ssl_hello;
393
394 /* Jump to the compression methods */
395 hs_len -= 2 + ext_len;
396 data += 2 + ext_len;
397
398 if (hs_len < 2 || /* minimum one compression method */
399 data[0] < 1 || data[0] > hs_len) /* minimum 1 bytes for a method */
400 goto not_ssl_hello;
401
402 /* Jump to the extensions */
403 hs_len -= 1 + data[0];
404 data += 1 + data[0];
405
406 if (hs_len < 2 || /* minimum one extension list length */
407 (ext_len = (data[0] << 8) + data[1]) > hs_len - 2) /* list too long */
408 goto not_ssl_hello;
409
410 hs_len = ext_len; /* limit ourselves to the extension length */
411 data += 2;
412
413 while (hs_len >= 4) {
414 int ext_type, name_type, srv_len, name_len;
415
416 ext_type = (data[0] << 8) + data[1];
417 ext_len = (data[2] << 8) + data[3];
418
419 if (ext_len > hs_len - 4) /* Extension too long */
420 goto not_ssl_hello;
421
422 if (ext_type == 0) { /* Server name */
423 if (ext_len < 2) /* need one list length */
424 goto not_ssl_hello;
425
426 srv_len = (data[4] << 8) + data[5];
427 if (srv_len < 4 || srv_len > hs_len - 6)
428 goto not_ssl_hello; /* at least 4 bytes per server name */
429
430 name_type = data[6];
431 name_len = (data[7] << 8) + data[8];
432
433 if (name_type == 0) { /* hostname */
Willy Tarreauf853c462012-04-23 18:53:56 +0200434 smp->type = SMP_T_CSTR;
435 smp->data.str.str = (char *)data + 9;
436 smp->data.str.len = name_len;
Willy Tarreau37406352012-04-23 16:16:37 +0200437 smp->flags = SMP_F_VOLATILE;
Willy Tarreaub6672b52011-12-12 17:23:41 +0100438 return 1;
439 }
440 }
441
442 hs_len -= 4 + ext_len;
443 data += 4 + ext_len;
444 }
445 /* server name not found */
446 goto not_ssl_hello;
447
448 too_short:
Willy Tarreau37406352012-04-23 16:16:37 +0200449 smp->flags = SMP_F_MAY_CHANGE;
Willy Tarreaub6672b52011-12-12 17:23:41 +0100450
451 not_ssl_hello:
452
453 return 0;
454}
455
Willy 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) {
807 if (err)
808 memprintf(err, "out of memory while loading string pattern");
Willy Tarreauc4262962010-05-10 23:42:40 +0200809 return 0;
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200810 }
Willy Tarreauc4262962010-05-10 23:42:40 +0200811 memcpy(node->key, *text, len + 1);
812 if (ebst_insert(pattern->val.tree, node) != node)
813 free(node); /* was a duplicate */
814 pattern->flags |= ACL_PAT_F_TREE; /* this pattern now contains a tree */
815 return 1;
816 }
817
Willy Tarreauae8b7962007-06-09 23:10:04 +0200818 pattern->ptr.str = strdup(*text);
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200819 if (!pattern->ptr.str) {
820 if (err)
821 memprintf(err, "out of memory while loading string pattern");
Willy Tarreaua84d3742007-05-07 00:36:48 +0200822 return 0;
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200823 }
Willy Tarreaua84d3742007-05-07 00:36:48 +0200824 pattern->len = len;
825 return 1;
826}
827
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +0100828/* Parse and concatenate all further strings into one. */
829int
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200830acl_parse_strcat(const char **text, struct acl_pattern *pattern, int *opaque, char **err)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +0100831{
832
833 int len = 0, i;
834 char *s;
835
836 for (i = 0; *text[i]; i++)
837 len += strlen(text[i])+1;
838
Willy Tarreauc92ddbc2012-04-27 22:10:57 +0200839 pattern->type = SMP_T_CSTR;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +0100840 pattern->ptr.str = s = calloc(1, len);
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200841 if (!pattern->ptr.str) {
842 if (err)
843 memprintf(err, "out of memory while loading pattern");
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +0100844 return 0;
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200845 }
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +0100846
847 for (i = 0; *text[i]; i++)
848 s += sprintf(s, i?" %s":"%s", text[i]);
849
850 pattern->len = len;
851
852 return i;
853}
854
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +0200855/* Free data allocated by acl_parse_reg */
Willy Tarreau37406352012-04-23 16:16:37 +0200856static void acl_free_reg(void *ptr)
857{
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +0200858 regfree((regex_t *)ptr);
859}
860
Willy Tarreauf3d25982007-05-08 22:45:09 +0200861/* Parse a regex. It is allocated. */
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200862int acl_parse_reg(const char **text, struct acl_pattern *pattern, int *opaque, char **err)
Willy Tarreauf3d25982007-05-08 22:45:09 +0200863{
864 regex_t *preg;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200865 int icase;
Willy Tarreauf3d25982007-05-08 22:45:09 +0200866
867 preg = calloc(1, sizeof(regex_t));
868
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200869 if (!preg) {
870 if (err)
871 memprintf(err, "out of memory while loading pattern");
Willy Tarreauf3d25982007-05-08 22:45:09 +0200872 return 0;
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200873 }
Willy Tarreauf3d25982007-05-08 22:45:09 +0200874
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200875 icase = (pattern->flags & ACL_PAT_F_IGNORE_CASE) ? REG_ICASE : 0;
876 if (regcomp(preg, *text, REG_EXTENDED | REG_NOSUB | icase) != 0) {
Willy Tarreauf3d25982007-05-08 22:45:09 +0200877 free(preg);
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200878 if (err)
879 memprintf(err, "regex '%s' is invalid", *text);
Willy Tarreauf3d25982007-05-08 22:45:09 +0200880 return 0;
881 }
882
883 pattern->ptr.reg = preg;
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +0200884 pattern->freeptrbuf = &acl_free_reg;
Willy Tarreauf3d25982007-05-08 22:45:09 +0200885 return 1;
886}
887
Willy Tarreauae8b7962007-06-09 23:10:04 +0200888/* Parse a range of positive integers delimited by either ':' or '-'. If only
889 * one integer is read, it is set as both min and max. An operator may be
890 * specified as the prefix, among this list of 5 :
891 *
892 * 0:eq, 1:gt, 2:ge, 3:lt, 4:le
893 *
894 * The default operator is "eq". It supports range matching. Ranges are
895 * rejected for other operators. The operator may be changed at any time.
896 * The operator is stored in the 'opaque' argument.
897 *
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200898 * If err is non-NULL, an error message will be returned there on errors and
899 * the caller will have to free it.
900 *
Willy Tarreaua84d3742007-05-07 00:36:48 +0200901 */
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200902int acl_parse_int(const char **text, struct acl_pattern *pattern, int *opaque, char **err)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200903{
Willy Tarreauae8b7962007-06-09 23:10:04 +0200904 signed long long i;
905 unsigned int j, last, skip = 0;
906 const char *ptr = *text;
907
Willy Tarreauc92ddbc2012-04-27 22:10:57 +0200908 pattern->type = SMP_T_UINT;
Willy Tarreau8f8e6452007-06-17 21:51:38 +0200909 while (!isdigit((unsigned char)*ptr)) {
Willy Tarreau1c7cc5b2010-07-18 10:46:33 +0200910 switch (get_std_op(ptr)) {
911 case STD_OP_EQ: *opaque = 0; break;
912 case STD_OP_GT: *opaque = 1; break;
913 case STD_OP_GE: *opaque = 2; break;
914 case STD_OP_LT: *opaque = 3; break;
915 case STD_OP_LE: *opaque = 4; break;
916 default:
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200917 if (err)
918 memprintf(err, "'%s' is neither a number nor a supported operator", ptr);
Willy Tarreauae8b7962007-06-09 23:10:04 +0200919 return 0;
Willy Tarreau1c7cc5b2010-07-18 10:46:33 +0200920 }
Willy Tarreauae8b7962007-06-09 23:10:04 +0200921
922 skip++;
923 ptr = text[skip];
924 }
Willy Tarreaua84d3742007-05-07 00:36:48 +0200925
926 last = i = 0;
927 while (1) {
Willy Tarreauae8b7962007-06-09 23:10:04 +0200928 j = *ptr++;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200929 if ((j == '-' || j == ':') && !last) {
930 last++;
931 pattern->val.range.min = i;
932 i = 0;
933 continue;
934 }
935 j -= '0';
936 if (j > 9)
937 // also catches the terminating zero
938 break;
939 i *= 10;
940 i += j;
941 }
Willy Tarreauae8b7962007-06-09 23:10:04 +0200942
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200943 if (last && *opaque >= 1 && *opaque <= 4) {
Willy Tarreauae8b7962007-06-09 23:10:04 +0200944 /* having a range with a min or a max is absurd */
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200945 if (err)
946 memprintf(err, "integer range '%s' specified with a comparison operator", text[skip]);
Willy Tarreauae8b7962007-06-09 23:10:04 +0200947 return 0;
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200948 }
Willy Tarreauae8b7962007-06-09 23:10:04 +0200949
Willy Tarreaua84d3742007-05-07 00:36:48 +0200950 if (!last)
951 pattern->val.range.min = i;
952 pattern->val.range.max = i;
Willy Tarreauae8b7962007-06-09 23:10:04 +0200953
954 switch (*opaque) {
955 case 0: /* eq */
956 pattern->val.range.min_set = 1;
957 pattern->val.range.max_set = 1;
958 break;
959 case 1: /* gt */
960 pattern->val.range.min++; /* gt = ge + 1 */
961 case 2: /* ge */
962 pattern->val.range.min_set = 1;
963 pattern->val.range.max_set = 0;
964 break;
965 case 3: /* lt */
966 pattern->val.range.max--; /* lt = le - 1 */
967 case 4: /* le */
968 pattern->val.range.min_set = 0;
969 pattern->val.range.max_set = 1;
970 break;
971 }
972 return skip + 1;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200973}
974
Willy Tarreau4a26d2f2008-07-15 16:05:33 +0200975/* Parse a range of positive 2-component versions delimited by either ':' or
976 * '-'. The version consists in a major and a minor, both of which must be
977 * smaller than 65536, because internally they will be represented as a 32-bit
978 * integer.
979 * If only one version is read, it is set as both min and max. Just like for
980 * pure integers, an operator may be specified as the prefix, among this list
981 * of 5 :
982 *
983 * 0:eq, 1:gt, 2:ge, 3:lt, 4:le
984 *
985 * The default operator is "eq". It supports range matching. Ranges are
986 * rejected for other operators. The operator may be changed at any time.
987 * The operator is stored in the 'opaque' argument. This allows constructs
988 * such as the following one :
989 *
990 * acl obsolete_ssl ssl_req_proto lt 3
991 * acl unsupported_ssl ssl_req_proto gt 3.1
992 * acl valid_ssl ssl_req_proto 3.0-3.1
993 *
994 */
Willy Tarreau7dcb6482012-04-27 17:52:25 +0200995int acl_parse_dotted_ver(const char **text, struct acl_pattern *pattern, int *opaque, char **err)
Willy Tarreau4a26d2f2008-07-15 16:05:33 +0200996{
997 signed long long i;
998 unsigned int j, last, skip = 0;
999 const char *ptr = *text;
1000
1001
1002 while (!isdigit((unsigned char)*ptr)) {
Willy Tarreau1c7cc5b2010-07-18 10:46:33 +02001003 switch (get_std_op(ptr)) {
1004 case STD_OP_EQ: *opaque = 0; break;
1005 case STD_OP_GT: *opaque = 1; break;
1006 case STD_OP_GE: *opaque = 2; break;
1007 case STD_OP_LT: *opaque = 3; break;
1008 case STD_OP_LE: *opaque = 4; break;
1009 default:
Willy Tarreau7dcb6482012-04-27 17:52:25 +02001010 if (err)
1011 memprintf(err, "'%s' is neither a number nor a supported operator", ptr);
Willy Tarreau4a26d2f2008-07-15 16:05:33 +02001012 return 0;
Willy Tarreau1c7cc5b2010-07-18 10:46:33 +02001013 }
Willy Tarreau4a26d2f2008-07-15 16:05:33 +02001014
1015 skip++;
1016 ptr = text[skip];
1017 }
1018
1019 last = i = 0;
1020 while (1) {
1021 j = *ptr++;
1022 if (j == '.') {
1023 /* minor part */
1024 if (i >= 65536)
1025 return 0;
1026 i <<= 16;
1027 continue;
1028 }
1029 if ((j == '-' || j == ':') && !last) {
1030 last++;
1031 if (i < 65536)
1032 i <<= 16;
1033 pattern->val.range.min = i;
1034 i = 0;
1035 continue;
1036 }
1037 j -= '0';
1038 if (j > 9)
1039 // also catches the terminating zero
1040 break;
1041 i = (i & 0xFFFF0000) + (i & 0xFFFF) * 10;
1042 i += j;
1043 }
1044
1045 /* if we only got a major version, let's shift it now */
1046 if (i < 65536)
1047 i <<= 16;
1048
Willy Tarreau7dcb6482012-04-27 17:52:25 +02001049 if (last && *opaque >= 1 && *opaque <= 4) {
Willy Tarreau4a26d2f2008-07-15 16:05:33 +02001050 /* having a range with a min or a max is absurd */
Willy Tarreau7dcb6482012-04-27 17:52:25 +02001051 if (err)
1052 memprintf(err, "version range '%s' specified with a comparison operator", text[skip]);
Willy Tarreau4a26d2f2008-07-15 16:05:33 +02001053 return 0;
Willy Tarreau7dcb6482012-04-27 17:52:25 +02001054 }
Willy Tarreau4a26d2f2008-07-15 16:05:33 +02001055
1056 if (!last)
1057 pattern->val.range.min = i;
1058 pattern->val.range.max = i;
1059
1060 switch (*opaque) {
1061 case 0: /* eq */
1062 pattern->val.range.min_set = 1;
1063 pattern->val.range.max_set = 1;
1064 break;
1065 case 1: /* gt */
1066 pattern->val.range.min++; /* gt = ge + 1 */
1067 case 2: /* ge */
1068 pattern->val.range.min_set = 1;
1069 pattern->val.range.max_set = 0;
1070 break;
1071 case 3: /* lt */
1072 pattern->val.range.max--; /* lt = le - 1 */
1073 case 4: /* le */
1074 pattern->val.range.min_set = 0;
1075 pattern->val.range.max_set = 1;
1076 break;
1077 }
1078 return skip + 1;
1079}
1080
Willy Tarreaua67fad92007-05-08 19:50:09 +02001081/* Parse an IP address and an optional mask in the form addr[/mask].
1082 * The addr may either be an IPv4 address or a hostname. The mask
1083 * may either be a dotted mask or a number of bits. Returns 1 if OK,
Willy Tarreauc92ddbc2012-04-27 22:10:57 +02001084 * otherwise 0. NOTE: IP address patterns are typed (IPV4/IPV6).
Willy Tarreaua67fad92007-05-08 19:50:09 +02001085 */
Willy Tarreau7dcb6482012-04-27 17:52:25 +02001086int acl_parse_ip(const char **text, struct acl_pattern *pattern, int *opaque, char **err)
Willy Tarreaua67fad92007-05-08 19:50:09 +02001087{
Willy Tarreaub337b532010-05-13 20:03:41 +02001088 struct eb_root *tree = NULL;
1089 if (pattern->flags & ACL_PAT_F_TREE_OK)
1090 tree = pattern->val.tree;
1091
1092 if (str2net(*text, &pattern->val.ipv4.addr, &pattern->val.ipv4.mask)) {
1093 unsigned int mask = ntohl(pattern->val.ipv4.mask.s_addr);
1094 struct ebmb_node *node;
1095 /* check if the mask is contiguous so that we can insert the
1096 * network into the tree. A continuous mask has only ones on
1097 * the left. This means that this mask + its lower bit added
1098 * once again is null.
1099 */
Willy Tarreauceb4ac92012-04-28 00:41:46 +02001100 pattern->type = SMP_T_IPV4;
Willy Tarreaub337b532010-05-13 20:03:41 +02001101 if (mask + (mask & -mask) == 0 && tree) {
1102 mask = mask ? 33 - flsnz(mask & -mask) : 0; /* equals cidr value */
1103 /* FIXME: insert <addr>/<mask> into the tree here */
1104 node = calloc(1, sizeof(*node) + 4); /* reserve 4 bytes for IPv4 address */
Willy Tarreau7dcb6482012-04-27 17:52:25 +02001105 if (!node) {
1106 if (err)
1107 memprintf(err, "out of memory while loading IPv4 pattern");
Willy Tarreaub337b532010-05-13 20:03:41 +02001108 return 0;
Willy Tarreau7dcb6482012-04-27 17:52:25 +02001109 }
Willy Tarreaub337b532010-05-13 20:03:41 +02001110 memcpy(node->key, &pattern->val.ipv4.addr, 4); /* network byte order */
1111 node->node.pfx = mask;
1112 if (ebmb_insert_prefix(tree, node, 4) != node)
1113 free(node); /* was a duplicate */
1114 pattern->flags |= ACL_PAT_F_TREE;
1115 return 1;
1116 }
Willy Tarreauae8b7962007-06-09 23:10:04 +02001117 return 1;
Willy Tarreaub337b532010-05-13 20:03:41 +02001118 }
Willy Tarreauceb4ac92012-04-28 00:41:46 +02001119 else if (str62net(*text, &pattern->val.ipv6.addr, &pattern->val.ipv6.mask)) {
1120 /* no tree support right now */
1121 pattern->type = SMP_T_IPV6;
1122 return 1;
1123 }
Willy Tarreau7dcb6482012-04-27 17:52:25 +02001124 else {
1125 if (err)
Willy Tarreauceb4ac92012-04-28 00:41:46 +02001126 memprintf(err, "'%s' is not a valid IPv4 or IPv6 address", *text);
Willy Tarreauae8b7962007-06-09 23:10:04 +02001127 return 0;
Willy Tarreau7dcb6482012-04-27 17:52:25 +02001128 }
Willy Tarreaua67fad92007-05-08 19:50:09 +02001129}
1130
Willy Tarreaua84d3742007-05-07 00:36:48 +02001131/*
1132 * Registers the ACL keyword list <kwl> as a list of valid keywords for next
1133 * parsing sessions.
1134 */
1135void acl_register_keywords(struct acl_kw_list *kwl)
1136{
1137 LIST_ADDQ(&acl_keywords.list, &kwl->list);
1138}
1139
1140/*
1141 * Unregisters the ACL keyword list <kwl> from the list of valid keywords.
1142 */
1143void acl_unregister_keywords(struct acl_kw_list *kwl)
1144{
1145 LIST_DEL(&kwl->list);
1146 LIST_INIT(&kwl->list);
1147}
1148
1149/* Return a pointer to the ACL <name> within the list starting at <head>, or
1150 * NULL if not found.
1151 */
1152struct acl *find_acl_by_name(const char *name, struct list *head)
1153{
1154 struct acl *acl;
1155 list_for_each_entry(acl, head, list) {
1156 if (strcmp(acl->name, name) == 0)
1157 return acl;
1158 }
1159 return NULL;
1160}
1161
1162/* Return a pointer to the ACL keyword <kw>, or NULL if not found. Note that if
1163 * <kw> contains an opening parenthesis, only the left part of it is checked.
1164 */
1165struct acl_keyword *find_acl_kw(const char *kw)
1166{
1167 int index;
1168 const char *kwend;
1169 struct acl_kw_list *kwl;
1170
1171 kwend = strchr(kw, '(');
1172 if (!kwend)
1173 kwend = kw + strlen(kw);
1174
1175 list_for_each_entry(kwl, &acl_keywords.list, list) {
1176 for (index = 0; kwl->kw[index].kw != NULL; index++) {
1177 if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) &&
1178 kwl->kw[index].kw[kwend-kw] == 0)
1179 return &kwl->kw[index];
1180 }
1181 }
1182 return NULL;
1183}
1184
Willy Tarreaudfd7fca2011-03-09 07:27:02 +01001185/* NB: does nothing if <pat> is NULL */
Willy Tarreaua84d3742007-05-07 00:36:48 +02001186static void free_pattern(struct acl_pattern *pat)
1187{
Willy Tarreaudfd7fca2011-03-09 07:27:02 +01001188 if (!pat)
1189 return;
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02001190
1191 if (pat->ptr.ptr) {
1192 if (pat->freeptrbuf)
1193 pat->freeptrbuf(pat->ptr.ptr);
1194
Willy Tarreaua84d3742007-05-07 00:36:48 +02001195 free(pat->ptr.ptr);
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02001196 }
1197
Willy Tarreaua84d3742007-05-07 00:36:48 +02001198 free(pat);
1199}
1200
1201static void free_pattern_list(struct list *head)
1202{
1203 struct acl_pattern *pat, *tmp;
1204 list_for_each_entry_safe(pat, tmp, head, list)
1205 free_pattern(pat);
1206}
1207
Willy Tarreaue56cda92010-05-11 23:25:05 +02001208static void free_pattern_tree(struct eb_root *root)
1209{
1210 struct eb_node *node, *next;
1211 node = eb_first(root);
1212 while (node) {
1213 next = eb_next(node);
1214 free(node);
1215 node = next;
1216 }
1217}
1218
Willy Tarreaua84d3742007-05-07 00:36:48 +02001219static struct acl_expr *prune_acl_expr(struct acl_expr *expr)
1220{
Willy Tarreau34db1082012-04-19 17:16:54 +02001221 struct arg *arg;
1222
Willy Tarreaua84d3742007-05-07 00:36:48 +02001223 free_pattern_list(&expr->patterns);
Willy Tarreaue56cda92010-05-11 23:25:05 +02001224 free_pattern_tree(&expr->pattern_tree);
Willy Tarreaua84d3742007-05-07 00:36:48 +02001225 LIST_INIT(&expr->patterns);
Willy Tarreau34db1082012-04-19 17:16:54 +02001226
1227 for (arg = expr->args; arg; arg++) {
1228 if (arg->type == ARGT_STOP)
1229 break;
1230 if (arg->type == ARGT_FE || arg->type == ARGT_BE ||
1231 arg->type == ARGT_TAB || arg->type == ARGT_SRV ||
1232 arg->type == ARGT_USR || arg->type == ARGT_STR) {
1233 free(arg->data.str.str);
1234 arg->data.str.str = NULL;
1235 }
1236 arg++;
1237 }
1238
1239 free(expr->args);
Willy Tarreaua84d3742007-05-07 00:36:48 +02001240 expr->kw->use_cnt--;
1241 return expr;
1242}
1243
Willy Tarreau08ad0b32012-04-27 17:25:24 +02001244
1245/* Reads patterns from a file. If <err_msg> is non-NULL, an error message will
1246 * be returned there on errors and the caller will have to free it.
1247 */
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001248static int acl_read_patterns_from_file( struct acl_keyword *aclkw,
1249 struct acl_expr *expr,
Willy Tarreau08ad0b32012-04-27 17:25:24 +02001250 const char *filename, int patflags,
1251 char **err)
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001252{
1253 FILE *file;
1254 char *c;
1255 const char *args[2];
1256 struct acl_pattern *pattern;
1257 int opaque;
Willy Tarreau6a8097f2011-02-26 15:14:15 +01001258 int ret = 0;
Willy Tarreau08ad0b32012-04-27 17:25:24 +02001259 int line = 0;
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001260
1261 file = fopen(filename, "r");
Willy Tarreau08ad0b32012-04-27 17:25:24 +02001262 if (!file) {
1263 memprintf(err, "failed to open pattern file <%s>", filename);
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001264 return 0;
Willy Tarreau08ad0b32012-04-27 17:25:24 +02001265 }
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001266
1267 /* now parse all patterns. The file may contain only one pattern per
1268 * line. If the line contains spaces, they will be part of the pattern.
1269 * The pattern stops at the first CR, LF or EOF encountered.
1270 */
1271 opaque = 0;
Willy Tarreaue56cda92010-05-11 23:25:05 +02001272 pattern = NULL;
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001273 args[1] = "";
1274 while (fgets(trash, sizeof(trash), file) != NULL) {
Willy Tarreau08ad0b32012-04-27 17:25:24 +02001275 line++;
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001276 c = trash;
Willy Tarreau58215a02010-05-13 22:07:43 +02001277
1278 /* ignore lines beginning with a dash */
1279 if (*c == '#')
1280 continue;
1281
1282 /* strip leading spaces and tabs */
1283 while (*c == ' ' || *c == '\t')
1284 c++;
1285
Willy Tarreau58215a02010-05-13 22:07:43 +02001286
1287 args[0] = c;
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001288 while (*c && *c != '\n' && *c != '\r')
1289 c++;
1290 *c = 0;
1291
Willy Tarreau51091962011-01-03 21:04:10 +01001292 /* empty lines are ignored too */
1293 if (c == args[0])
1294 continue;
1295
Willy Tarreaue56cda92010-05-11 23:25:05 +02001296 /* we keep the previous pattern along iterations as long as it's not used */
1297 if (!pattern)
1298 pattern = (struct acl_pattern *)malloc(sizeof(*pattern));
Willy Tarreau08ad0b32012-04-27 17:25:24 +02001299 if (!pattern) {
1300 memprintf(err, "out of memory when loading patterns from file <%s>", filename);
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001301 goto out_close;
Willy Tarreau08ad0b32012-04-27 17:25:24 +02001302 }
Willy Tarreaue56cda92010-05-11 23:25:05 +02001303
1304 memset(pattern, 0, sizeof(*pattern));
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001305 pattern->flags = patflags;
1306
Willy Tarreaue56cda92010-05-11 23:25:05 +02001307 if ((aclkw->requires & ACL_MAY_LOOKUP) && !(pattern->flags & ACL_PAT_F_IGNORE_CASE)) {
1308 /* we pre-set the data pointer to the tree's head so that functions
1309 * which are able to insert in a tree know where to do that.
1310 */
1311 pattern->flags |= ACL_PAT_F_TREE_OK;
1312 pattern->val.tree = &expr->pattern_tree;
1313 }
1314
Willy Tarreauc92ddbc2012-04-27 22:10:57 +02001315 pattern->type = SMP_TYPES; /* unspecified type by default */
Willy Tarreau7dcb6482012-04-27 17:52:25 +02001316 if (!aclkw->parse(args, pattern, &opaque, err))
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001317 goto out_free_pattern;
Willy Tarreaue56cda92010-05-11 23:25:05 +02001318
1319 /* if the parser did not feed the tree, let's chain the pattern to the list */
1320 if (!(pattern->flags & ACL_PAT_F_TREE)) {
1321 LIST_ADDQ(&expr->patterns, &pattern->list);
1322 pattern = NULL; /* get a new one */
1323 }
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001324 }
Willy Tarreau6a8097f2011-02-26 15:14:15 +01001325
1326 ret = 1; /* success */
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001327
1328 out_free_pattern:
1329 free_pattern(pattern);
1330 out_close:
1331 fclose(file);
Willy Tarreau6a8097f2011-02-26 15:14:15 +01001332 return ret;
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001333}
1334
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001335/* Parse an ACL expression starting at <args>[0], and return it. If <err> is
1336 * not NULL, it will be filled with a pointer to an error message in case of
1337 * error. This pointer must be freeable or NULL.
1338 *
Willy Tarreaua84d3742007-05-07 00:36:48 +02001339 * Right now, the only accepted syntax is :
1340 * <subject> [<value>...]
1341 */
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001342struct acl_expr *parse_acl_expr(const char **args, char **err)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001343{
1344 __label__ out_return, out_free_expr, out_free_pattern;
1345 struct acl_expr *expr;
1346 struct acl_keyword *aclkw;
1347 struct acl_pattern *pattern;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02001348 int opaque, patflags;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001349 const char *arg;
1350
1351 aclkw = find_acl_kw(args[0]);
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001352 if (!aclkw || !aclkw->parse) {
1353 if (err)
1354 memprintf(err, "unknown ACL keyword '%s'", *args);
Willy Tarreaua84d3742007-05-07 00:36:48 +02001355 goto out_return;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001356 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001357
1358 expr = (struct acl_expr *)calloc(1, sizeof(*expr));
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001359 if (!expr) {
1360 if (err)
1361 memprintf(err, "out of memory when parsing ACL expression");
Willy Tarreaua84d3742007-05-07 00:36:48 +02001362 goto out_return;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001363 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001364
1365 expr->kw = aclkw;
1366 aclkw->use_cnt++;
1367 LIST_INIT(&expr->patterns);
Willy Tarreaue56cda92010-05-11 23:25:05 +02001368 expr->pattern_tree = EB_ROOT_UNIQUE;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001369
1370 arg = strchr(args[0], '(');
Willy Tarreau61612d42012-04-19 18:42:05 +02001371 if (aclkw->arg_mask) {
1372 int nbargs = 0;
Willy Tarreau34db1082012-04-19 17:16:54 +02001373 char *end;
Willy Tarreau34db1082012-04-19 17:16:54 +02001374
Willy Tarreau61612d42012-04-19 18:42:05 +02001375 if (arg != NULL) {
1376 /* there are 0 or more arguments in the form "subject(arg[,arg]*)" */
1377 arg++;
1378 end = strchr(arg, ')');
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001379 if (!end) {
1380 if (err)
1381 memprintf(err, "missing closing ')' after arguments to ACL keyword '%s'", aclkw->kw);
Willy Tarreau61612d42012-04-19 18:42:05 +02001382 goto out_free_expr;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001383 }
Willy Tarreau34db1082012-04-19 17:16:54 +02001384
Willy Tarreau61612d42012-04-19 18:42:05 +02001385 /* Parse the arguments. Note that currently we have no way to
1386 * report parsing errors, hence the NULL in the error pointers.
1387 * An error is also reported if some mandatory arguments are
1388 * missing.
1389 */
1390 nbargs = make_arg_list(arg, end - arg, aclkw->arg_mask, &expr->args,
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001391 err, NULL, NULL);
1392 if (nbargs < 0) {
1393 /* note that make_arg_list will have set <err> here */
1394 if (err)
1395 memprintf(err, "in argument to '%s', %s", aclkw->kw, *err);
Willy Tarreau61612d42012-04-19 18:42:05 +02001396 goto out_free_expr;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001397 }
Willy Tarreauae52f062012-04-26 12:13:35 +02001398
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001399 if (aclkw->val_args && !aclkw->val_args(expr->args, err)) {
1400 /* invalid keyword argument, error must have been
1401 * set by val_args().
1402 */
1403 if (err)
1404 memprintf(err, "in argument to '%s', %s", aclkw->kw, *err);
1405 goto out_free_expr;
1406 }
Willy Tarreau61612d42012-04-19 18:42:05 +02001407 }
Willy Tarreaufc2c1fd2012-04-19 23:35:54 +02001408 else if (ARGM(aclkw->arg_mask) == 1) {
1409 int type = (aclkw->arg_mask >> 4) & 15;
1410
1411 /* If a proxy is noted as a mandatory argument, we'll fake
1412 * an empty one so that acl_find_targets() resolves it as
1413 * the current one later.
1414 */
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001415 if (type != ARGT_FE && type != ARGT_BE && type != ARGT_TAB) {
1416 if (err)
1417 memprintf(err, "ACL keyword '%s' expects %d arguments", aclkw->kw, ARGM(aclkw->arg_mask));
Willy Tarreaufc2c1fd2012-04-19 23:35:54 +02001418 goto out_free_expr;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001419 }
Willy Tarreaufc2c1fd2012-04-19 23:35:54 +02001420
1421 /* Build an arg list containing the type as an empty string
1422 * and the usual STOP.
1423 */
1424 expr->args = calloc(2, sizeof(*expr->args));
1425 expr->args[0].type = type;
1426 expr->args[0].data.str.str = strdup("");
1427 expr->args[0].data.str.len = 1;
1428 expr->args[0].data.str.len = 0;
1429 expr->args[1].type = ARGT_STOP;
1430 }
Willy Tarreau61612d42012-04-19 18:42:05 +02001431 else if (ARGM(aclkw->arg_mask)) {
1432 /* there were some mandatory arguments */
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001433 if (err)
1434 memprintf(err, "ACL keyword '%s' expects %d arguments", aclkw->kw, ARGM(aclkw->arg_mask));
Willy Tarreaua84d3742007-05-07 00:36:48 +02001435 goto out_free_expr;
Willy Tarreau61612d42012-04-19 18:42:05 +02001436 }
1437 }
1438 else {
1439 if (arg) {
1440 /* no argument expected */
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001441 if (err)
1442 memprintf(err, "ACL keyword '%s' takes no argument", aclkw->kw);
Willy Tarreau61612d42012-04-19 18:42:05 +02001443 goto out_free_expr;
1444 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001445 }
1446
Willy Tarreaua84d3742007-05-07 00:36:48 +02001447 args++;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02001448
1449 /* check for options before patterns. Supported options are :
1450 * -i : ignore case for all patterns by default
1451 * -f : read patterns from those files
1452 * -- : everything after this is not an option
1453 */
1454 patflags = 0;
1455 while (**args == '-') {
1456 if ((*args)[1] == 'i')
1457 patflags |= ACL_PAT_F_IGNORE_CASE;
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001458 else if ((*args)[1] == 'f') {
Willy Tarreau08ad0b32012-04-27 17:25:24 +02001459 if (!acl_read_patterns_from_file(aclkw, expr, args[1], patflags | ACL_PAT_F_FROM_FILE, err))
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001460 goto out_free_expr;
1461 args++;
1462 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +02001463 else if ((*args)[1] == '-') {
1464 args++;
1465 break;
1466 }
1467 else
1468 break;
1469 args++;
1470 }
1471
1472 /* now parse all patterns */
Willy Tarreauae8b7962007-06-09 23:10:04 +02001473 opaque = 0;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001474 while (**args) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02001475 int ret;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001476 pattern = (struct acl_pattern *)calloc(1, sizeof(*pattern));
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001477 if (!pattern) {
1478 if (err)
1479 memprintf(err, "out of memory when parsing ACL pattern");
Willy Tarreaua84d3742007-05-07 00:36:48 +02001480 goto out_free_expr;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001481 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +02001482 pattern->flags = patflags;
1483
Willy Tarreauc92ddbc2012-04-27 22:10:57 +02001484 pattern->type = SMP_TYPES; /* unspecified type */
Willy Tarreau7dcb6482012-04-27 17:52:25 +02001485 ret = aclkw->parse(args, pattern, &opaque, err);
1486 if (!ret)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001487 goto out_free_pattern;
Willy Tarreau7dcb6482012-04-27 17:52:25 +02001488
Willy Tarreaua84d3742007-05-07 00:36:48 +02001489 LIST_ADDQ(&expr->patterns, &pattern->list);
Willy Tarreauae8b7962007-06-09 23:10:04 +02001490 args += ret;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001491 }
1492
1493 return expr;
1494
1495 out_free_pattern:
1496 free_pattern(pattern);
1497 out_free_expr:
1498 prune_acl_expr(expr);
1499 free(expr);
1500 out_return:
1501 return NULL;
1502}
1503
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02001504/* Purge everything in the acl <acl>, then return <acl>. */
1505struct acl *prune_acl(struct acl *acl) {
1506
1507 struct acl_expr *expr, *exprb;
1508
1509 free(acl->name);
1510
1511 list_for_each_entry_safe(expr, exprb, &acl->expr, list) {
1512 LIST_DEL(&expr->list);
1513 prune_acl_expr(expr);
1514 free(expr);
1515 }
1516
1517 return acl;
1518}
1519
Willy Tarreaua84d3742007-05-07 00:36:48 +02001520/* Parse an ACL with the name starting at <args>[0], and with a list of already
1521 * known ACLs in <acl>. If the ACL was not in the list, it will be added.
Willy Tarreau2a56c5e2010-03-15 16:13:29 +01001522 * A pointer to that ACL is returned. If the ACL has an empty name, then it's
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001523 * an anonymous one and it won't be merged with any other one. If <err> is not
1524 * NULL, it will be filled with an appropriate error. This pointer must be
1525 * freeable or NULL.
Willy Tarreaua84d3742007-05-07 00:36:48 +02001526 *
1527 * args syntax: <aclname> <acl_expr>
1528 */
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001529struct acl *parse_acl(const char **args, struct list *known_acl, char **err)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001530{
1531 __label__ out_return, out_free_acl_expr, out_free_name;
1532 struct acl *cur_acl;
1533 struct acl_expr *acl_expr;
1534 char *name;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001535 const char *pos;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001536
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001537 if (**args && (pos = invalid_char(*args))) {
1538 if (err)
1539 memprintf(err, "invalid character in ACL name : '%c'", *pos);
Willy Tarreau2e74c3f2007-12-02 18:45:09 +01001540 goto out_return;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001541 }
Willy Tarreau2e74c3f2007-12-02 18:45:09 +01001542
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001543 acl_expr = parse_acl_expr(args + 1, err);
1544 if (!acl_expr) {
1545 /* parse_acl_expr will have filled <err> here */
Willy Tarreaua84d3742007-05-07 00:36:48 +02001546 goto out_return;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001547 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001548
Willy Tarreau404e8ab2009-07-26 19:40:40 +02001549 /* Check for args beginning with an opening parenthesis just after the
1550 * subject, as this is almost certainly a typo. Right now we can only
1551 * emit a warning, so let's do so.
1552 */
Krzysztof Piotr Oledzki4cdd8312009-10-05 00:23:35 +02001553 if (!strchr(args[1], '(') && *args[2] == '(')
Willy Tarreau404e8ab2009-07-26 19:40:40 +02001554 Warning("parsing acl '%s' :\n"
1555 " matching '%s' for pattern '%s' is likely a mistake and probably\n"
1556 " not what you want. Maybe you need to remove the extraneous space before '('.\n"
1557 " If you are really sure this is not an error, please insert '--' between the\n"
1558 " match and the pattern to make this warning message disappear.\n",
1559 args[0], args[1], args[2]);
1560
Willy Tarreau2a56c5e2010-03-15 16:13:29 +01001561 if (*args[0])
1562 cur_acl = find_acl_by_name(args[0], known_acl);
1563 else
1564 cur_acl = NULL;
1565
Willy Tarreaua84d3742007-05-07 00:36:48 +02001566 if (!cur_acl) {
1567 name = strdup(args[0]);
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001568 if (!name) {
1569 if (err)
1570 memprintf(err, "out of memory when parsing ACL");
Willy Tarreaua84d3742007-05-07 00:36:48 +02001571 goto out_free_acl_expr;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001572 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001573 cur_acl = (struct acl *)calloc(1, sizeof(*cur_acl));
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001574 if (cur_acl == NULL) {
1575 if (err)
1576 memprintf(err, "out of memory when parsing ACL");
Willy Tarreaua84d3742007-05-07 00:36:48 +02001577 goto out_free_name;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001578 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001579
1580 LIST_INIT(&cur_acl->expr);
1581 LIST_ADDQ(known_acl, &cur_acl->list);
1582 cur_acl->name = name;
1583 }
1584
Willy Tarreaua9802632008-07-25 19:13:19 +02001585 cur_acl->requires |= acl_expr->kw->requires;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001586 LIST_ADDQ(&cur_acl->expr, &acl_expr->list);
1587 return cur_acl;
1588
1589 out_free_name:
1590 free(name);
1591 out_free_acl_expr:
1592 prune_acl_expr(acl_expr);
1593 free(acl_expr);
1594 out_return:
1595 return NULL;
1596}
1597
Willy Tarreau16fbe822007-06-17 11:54:31 +02001598/* Some useful ACLs provided by default. Only those used are allocated. */
1599
1600const struct {
1601 const char *name;
1602 const char *expr[4]; /* put enough for longest expression */
1603} default_acl_list[] = {
Willy Tarreau58393e12008-07-20 10:39:22 +02001604 { .name = "TRUE", .expr = {"always_true",""}},
1605 { .name = "FALSE", .expr = {"always_false",""}},
Willy Tarreau16fbe822007-06-17 11:54:31 +02001606 { .name = "LOCALHOST", .expr = {"src","127.0.0.1/8",""}},
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001607 { .name = "HTTP", .expr = {"req_proto_http",""}},
Willy Tarreau16fbe822007-06-17 11:54:31 +02001608 { .name = "HTTP_1.0", .expr = {"req_ver","1.0",""}},
1609 { .name = "HTTP_1.1", .expr = {"req_ver","1.1",""}},
1610 { .name = "METH_CONNECT", .expr = {"method","CONNECT",""}},
1611 { .name = "METH_GET", .expr = {"method","GET","HEAD",""}},
1612 { .name = "METH_HEAD", .expr = {"method","HEAD",""}},
1613 { .name = "METH_OPTIONS", .expr = {"method","OPTIONS",""}},
1614 { .name = "METH_POST", .expr = {"method","POST",""}},
1615 { .name = "METH_TRACE", .expr = {"method","TRACE",""}},
1616 { .name = "HTTP_URL_ABS", .expr = {"url_reg","^[^/:]*://",""}},
1617 { .name = "HTTP_URL_SLASH", .expr = {"url_beg","/",""}},
1618 { .name = "HTTP_URL_STAR", .expr = {"url","*",""}},
1619 { .name = "HTTP_CONTENT", .expr = {"hdr_val(content-length)","gt","0",""}},
Emeric Brunbede3d02009-06-30 17:54:00 +02001620 { .name = "RDP_COOKIE", .expr = {"req_rdp_cookie_cnt","gt","0",""}},
Willy Tarreauc6317702008-07-20 09:29:50 +02001621 { .name = "REQ_CONTENT", .expr = {"req_len","gt","0",""}},
Willy Tarreaub6fb4202008-07-20 11:18:28 +02001622 { .name = "WAIT_END", .expr = {"wait_end",""}},
Willy Tarreau16fbe822007-06-17 11:54:31 +02001623 { .name = NULL, .expr = {""}}
1624};
1625
1626/* Find a default ACL from the default_acl list, compile it and return it.
1627 * If the ACL is not found, NULL is returned. In theory, it cannot fail,
1628 * except when default ACLs are broken, in which case it will return NULL.
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001629 * If <known_acl> is not NULL, the ACL will be queued at its tail. If <err> is
1630 * not NULL, it will be filled with an error message if an error occurs. This
1631 * pointer must be freeable or NULL.
Willy Tarreau16fbe822007-06-17 11:54:31 +02001632 */
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001633struct acl *find_acl_default(const char *acl_name, struct list *known_acl, char **err)
Willy Tarreau16fbe822007-06-17 11:54:31 +02001634{
1635 __label__ out_return, out_free_acl_expr, out_free_name;
1636 struct acl *cur_acl;
1637 struct acl_expr *acl_expr;
1638 char *name;
1639 int index;
1640
1641 for (index = 0; default_acl_list[index].name != NULL; index++) {
1642 if (strcmp(acl_name, default_acl_list[index].name) == 0)
1643 break;
1644 }
1645
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001646 if (default_acl_list[index].name == NULL) {
1647 if (err)
1648 memprintf(err, "no such ACL : '%s'", acl_name);
Willy Tarreau16fbe822007-06-17 11:54:31 +02001649 return NULL;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001650 }
Willy Tarreau16fbe822007-06-17 11:54:31 +02001651
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001652 acl_expr = parse_acl_expr((const char **)default_acl_list[index].expr, err);
1653 if (!acl_expr) {
1654 /* parse_acl_expr must have filled err here */
Willy Tarreau16fbe822007-06-17 11:54:31 +02001655 goto out_return;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001656 }
Willy Tarreau16fbe822007-06-17 11:54:31 +02001657
1658 name = strdup(acl_name);
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001659 if (!name) {
1660 if (err)
1661 memprintf(err, "out of memory when building default ACL '%s'", acl_name);
Willy Tarreau16fbe822007-06-17 11:54:31 +02001662 goto out_free_acl_expr;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001663 }
1664
Willy Tarreau16fbe822007-06-17 11:54:31 +02001665 cur_acl = (struct acl *)calloc(1, sizeof(*cur_acl));
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001666 if (cur_acl == NULL) {
1667 if (err)
1668 memprintf(err, "out of memory when building default ACL '%s'", acl_name);
Willy Tarreau16fbe822007-06-17 11:54:31 +02001669 goto out_free_name;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001670 }
Willy Tarreau16fbe822007-06-17 11:54:31 +02001671
1672 cur_acl->name = name;
Willy Tarreaua55b7dc2009-07-12 09:21:30 +02001673 cur_acl->requires |= acl_expr->kw->requires;
Willy Tarreau16fbe822007-06-17 11:54:31 +02001674 LIST_INIT(&cur_acl->expr);
1675 LIST_ADDQ(&cur_acl->expr, &acl_expr->list);
1676 if (known_acl)
1677 LIST_ADDQ(known_acl, &cur_acl->list);
1678
1679 return cur_acl;
1680
1681 out_free_name:
1682 free(name);
1683 out_free_acl_expr:
1684 prune_acl_expr(acl_expr);
1685 free(acl_expr);
1686 out_return:
1687 return NULL;
1688}
Willy Tarreaua84d3742007-05-07 00:36:48 +02001689
1690/* Purge everything in the acl_cond <cond>, then return <cond>. */
1691struct acl_cond *prune_acl_cond(struct acl_cond *cond)
1692{
1693 struct acl_term_suite *suite, *tmp_suite;
1694 struct acl_term *term, *tmp_term;
1695
1696 /* iterate through all term suites and free all terms and all suites */
1697 list_for_each_entry_safe(suite, tmp_suite, &cond->suites, list) {
1698 list_for_each_entry_safe(term, tmp_term, &suite->terms, list)
1699 free(term);
1700 free(suite);
1701 }
1702 return cond;
1703}
1704
1705/* Parse an ACL condition starting at <args>[0], relying on a list of already
1706 * known ACLs passed in <known_acl>. The new condition is returned (or NULL in
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001707 * case of low memory). Supports multiple conditions separated by "or". If
1708 * <err> is not NULL, it will be filled with a pointer to an error message in
1709 * case of error, that the caller is responsible for freeing. The initial
1710 * location must either be freeable or NULL.
Willy Tarreaua84d3742007-05-07 00:36:48 +02001711 */
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001712struct acl_cond *parse_acl_cond(const char **args, struct list *known_acl, int pol, char **err)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001713{
1714 __label__ out_return, out_free_suite, out_free_term;
Willy Tarreau74b98a82007-06-16 19:35:18 +02001715 int arg, neg;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001716 const char *word;
1717 struct acl *cur_acl;
1718 struct acl_term *cur_term;
1719 struct acl_term_suite *cur_suite;
1720 struct acl_cond *cond;
1721
1722 cond = (struct acl_cond *)calloc(1, sizeof(*cond));
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001723 if (cond == NULL) {
1724 if (err)
1725 memprintf(err, "out of memory when parsing condition");
Willy Tarreaua84d3742007-05-07 00:36:48 +02001726 goto out_return;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001727 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001728
1729 LIST_INIT(&cond->list);
1730 LIST_INIT(&cond->suites);
1731 cond->pol = pol;
1732
1733 cur_suite = NULL;
Willy Tarreau74b98a82007-06-16 19:35:18 +02001734 neg = 0;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001735 for (arg = 0; *args[arg]; arg++) {
1736 word = args[arg];
1737
1738 /* remove as many exclamation marks as we can */
1739 while (*word == '!') {
1740 neg = !neg;
1741 word++;
1742 }
1743
1744 /* an empty word is allowed because we cannot force the user to
1745 * always think about not leaving exclamation marks alone.
1746 */
1747 if (!*word)
1748 continue;
1749
Willy Tarreau16fbe822007-06-17 11:54:31 +02001750 if (strcasecmp(word, "or") == 0 || strcmp(word, "||") == 0) {
Willy Tarreaua84d3742007-05-07 00:36:48 +02001751 /* new term suite */
1752 cur_suite = NULL;
1753 neg = 0;
1754 continue;
1755 }
1756
Willy Tarreau95fa4692010-02-01 13:05:50 +01001757 if (strcmp(word, "{") == 0) {
1758 /* we may have a complete ACL expression between two braces,
1759 * find the last one.
1760 */
1761 int arg_end = arg + 1;
1762 const char **args_new;
1763
1764 while (*args[arg_end] && strcmp(args[arg_end], "}") != 0)
1765 arg_end++;
1766
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001767 if (!*args[arg_end]) {
1768 if (err)
1769 memprintf(err, "missing closing '}' in condition");
Willy Tarreau95fa4692010-02-01 13:05:50 +01001770 goto out_free_suite;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001771 }
Willy Tarreau95fa4692010-02-01 13:05:50 +01001772
1773 args_new = calloc(1, (arg_end - arg + 1) * sizeof(*args_new));
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001774 if (!args_new) {
1775 if (err)
1776 memprintf(err, "out of memory when parsing condition");
Willy Tarreau95fa4692010-02-01 13:05:50 +01001777 goto out_free_suite;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001778 }
Willy Tarreau95fa4692010-02-01 13:05:50 +01001779
Willy Tarreau2a56c5e2010-03-15 16:13:29 +01001780 args_new[0] = "";
Willy Tarreau95fa4692010-02-01 13:05:50 +01001781 memcpy(args_new + 1, args + arg + 1, (arg_end - arg) * sizeof(*args_new));
1782 args_new[arg_end - arg] = "";
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001783 cur_acl = parse_acl(args_new, known_acl, err);
Willy Tarreau95fa4692010-02-01 13:05:50 +01001784 free(args_new);
1785
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001786 if (!cur_acl) {
1787 /* note that parse_acl() must have filled <err> here */
Willy Tarreau16fbe822007-06-17 11:54:31 +02001788 goto out_free_suite;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001789 }
Willy Tarreau95fa4692010-02-01 13:05:50 +01001790 arg = arg_end;
1791 }
1792 else {
1793 /* search for <word> in the known ACL names. If we do not find
1794 * it, let's look for it in the default ACLs, and if found, add
1795 * it to the list of ACLs of this proxy. This makes it possible
1796 * to override them.
1797 */
1798 cur_acl = find_acl_by_name(word, known_acl);
1799 if (cur_acl == NULL) {
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001800 cur_acl = find_acl_default(word, known_acl, err);
1801 if (cur_acl == NULL) {
1802 /* note that find_acl_default() must have filled <err> here */
Willy Tarreau95fa4692010-02-01 13:05:50 +01001803 goto out_free_suite;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001804 }
Willy Tarreau95fa4692010-02-01 13:05:50 +01001805 }
Willy Tarreau16fbe822007-06-17 11:54:31 +02001806 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001807
1808 cur_term = (struct acl_term *)calloc(1, sizeof(*cur_term));
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001809 if (cur_term == NULL) {
1810 if (err)
1811 memprintf(err, "out of memory when parsing condition");
Willy Tarreaua84d3742007-05-07 00:36:48 +02001812 goto out_free_suite;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001813 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001814
1815 cur_term->acl = cur_acl;
1816 cur_term->neg = neg;
Willy Tarreaua9802632008-07-25 19:13:19 +02001817 cond->requires |= cur_acl->requires;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001818
1819 if (!cur_suite) {
1820 cur_suite = (struct acl_term_suite *)calloc(1, sizeof(*cur_suite));
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001821 if (cur_term == NULL) {
1822 if (err)
1823 memprintf(err, "out of memory when parsing condition");
Willy Tarreaua84d3742007-05-07 00:36:48 +02001824 goto out_free_term;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001825 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001826 LIST_INIT(&cur_suite->terms);
1827 LIST_ADDQ(&cond->suites, &cur_suite->list);
1828 }
1829 LIST_ADDQ(&cur_suite->terms, &cur_term->list);
Willy Tarreau74b98a82007-06-16 19:35:18 +02001830 neg = 0;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001831 }
1832
1833 return cond;
1834
1835 out_free_term:
1836 free(cur_term);
1837 out_free_suite:
1838 prune_acl_cond(cond);
1839 free(cond);
1840 out_return:
1841 return NULL;
1842}
1843
Willy Tarreau2bbba412010-01-28 16:48:33 +01001844/* Builds an ACL condition starting at the if/unless keyword. The complete
1845 * condition is returned. NULL is returned in case of error or if the first
1846 * word is neither "if" nor "unless". It automatically sets the file name and
1847 * the line number in the condition for better error reporting, and adds the
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001848 * ACL requirements to the proxy's acl_requires. If <err> is not NULL, it will
1849 * be filled with a pointer to an error message in case of error, that the
1850 * caller is responsible for freeing. The initial location must either be
1851 * freeable or NULL.
Willy Tarreau2bbba412010-01-28 16:48:33 +01001852 */
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001853struct 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 +01001854{
1855 int pol = ACL_COND_NONE;
1856 struct acl_cond *cond = NULL;
1857
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001858 if (err)
1859 *err = NULL;
1860
Willy Tarreau2bbba412010-01-28 16:48:33 +01001861 if (!strcmp(*args, "if")) {
1862 pol = ACL_COND_IF;
1863 args++;
1864 }
1865 else if (!strcmp(*args, "unless")) {
1866 pol = ACL_COND_UNLESS;
1867 args++;
1868 }
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001869 else {
1870 if (err)
1871 memprintf(err, "conditions must start with either 'if' or 'unless'");
Willy Tarreau2bbba412010-01-28 16:48:33 +01001872 return NULL;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001873 }
Willy Tarreau2bbba412010-01-28 16:48:33 +01001874
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001875 cond = parse_acl_cond(args, &px->acl, pol, err);
1876 if (!cond) {
1877 /* note that parse_acl_cond must have filled <err> here */
Willy Tarreau2bbba412010-01-28 16:48:33 +01001878 return NULL;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001879 }
Willy Tarreau2bbba412010-01-28 16:48:33 +01001880
1881 cond->file = file;
1882 cond->line = line;
1883 px->acl_requires |= cond->requires;
1884
1885 return cond;
1886}
1887
Willy Tarreau11382812008-07-09 16:18:21 +02001888/* Execute condition <cond> and return either ACL_PAT_FAIL, ACL_PAT_MISS or
Willy Tarreaub6866442008-07-14 23:54:42 +02001889 * ACL_PAT_PASS depending on the test results. ACL_PAT_MISS may only be
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001890 * returned if <opt> does not contain SMP_OPT_FINAL, indicating that incomplete
Willy Tarreau7a777ed2012-04-26 11:44:02 +02001891 * data is being examined. The function automatically sets SMP_OPT_ITERATE.
Willy Tarreaub6866442008-07-14 23:54:42 +02001892 * This function only computes the condition, it does not apply the polarity
1893 * required by IF/UNLESS, it's up to the caller to do this using something like
1894 * this :
Willy Tarreau11382812008-07-09 16:18:21 +02001895 *
1896 * res = acl_pass(res);
Willy Tarreaub6866442008-07-14 23:54:42 +02001897 * if (res == ACL_PAT_MISS)
1898 * return 0;
Willy Tarreau11382812008-07-09 16:18:21 +02001899 * if (cond->pol == ACL_COND_UNLESS)
1900 * res = !res;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001901 */
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001902int 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 +02001903{
1904 __label__ fetch_next;
1905 struct acl_term_suite *suite;
1906 struct acl_term *term;
1907 struct acl_expr *expr;
1908 struct acl *acl;
1909 struct acl_pattern *pattern;
Willy Tarreau37406352012-04-23 16:16:37 +02001910 struct sample smp;
Willy Tarreau11382812008-07-09 16:18:21 +02001911 int acl_res, suite_res, cond_res;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001912
Willy Tarreau7a777ed2012-04-26 11:44:02 +02001913 /* ACLs are iterated over all values, so let's always set the flag to
1914 * indicate this to the fetch functions.
1915 */
1916 opt |= SMP_OPT_ITERATE;
1917
Willy Tarreau11382812008-07-09 16:18:21 +02001918 /* We're doing a logical OR between conditions so we initialize to FAIL.
1919 * The MISS status is propagated down from the suites.
1920 */
Willy Tarreaua84d3742007-05-07 00:36:48 +02001921 cond_res = ACL_PAT_FAIL;
1922 list_for_each_entry(suite, &cond->suites, list) {
Willy Tarreau11382812008-07-09 16:18:21 +02001923 /* Evaluate condition suite <suite>. We stop at the first term
1924 * which returns ACL_PAT_FAIL. The MISS status is still propagated
1925 * in case of uncertainty in the result.
Willy Tarreaua84d3742007-05-07 00:36:48 +02001926 */
1927
1928 /* we're doing a logical AND between terms, so we must set the
1929 * initial value to PASS.
1930 */
1931 suite_res = ACL_PAT_PASS;
1932 list_for_each_entry(term, &suite->terms, list) {
1933 acl = term->acl;
1934
1935 /* FIXME: use cache !
1936 * check acl->cache_idx for this.
1937 */
1938
1939 /* ACL result not cached. Let's scan all the expressions
1940 * and use the first one to match.
1941 */
1942 acl_res = ACL_PAT_FAIL;
1943 list_for_each_entry(expr, &acl->expr, list) {
Willy Tarreaud41f8d82007-06-10 10:06:18 +02001944 /* we need to reset context and flags */
Willy Tarreau37406352012-04-23 16:16:37 +02001945 memset(&smp, 0, sizeof(smp));
Willy Tarreaua84d3742007-05-07 00:36:48 +02001946 fetch_next:
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001947 if (!expr->kw->fetch(px, l4, l7, opt, expr->args, &smp)) {
Willy Tarreaub6866442008-07-14 23:54:42 +02001948 /* maybe we could not fetch because of missing data */
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001949 if (smp.flags & SMP_F_MAY_CHANGE && !(opt & SMP_OPT_FINAL))
Willy Tarreaub6866442008-07-14 23:54:42 +02001950 acl_res |= ACL_PAT_MISS;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001951 continue;
Willy Tarreaub6866442008-07-14 23:54:42 +02001952 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001953
Willy Tarreau197e10a2012-04-23 19:18:42 +02001954 if (smp.type == SMP_T_BOOL) {
1955 if (smp.data.uint)
Willy Tarreaua79534f2008-07-20 10:13:37 +02001956 acl_res |= ACL_PAT_PASS;
1957 else
1958 acl_res |= ACL_PAT_FAIL;
1959 }
1960 else {
Willy Tarreau020534d2010-05-16 21:45:45 +02001961 if (!eb_is_empty(&expr->pattern_tree)) {
Willy Tarreauc4262962010-05-10 23:42:40 +02001962 /* a tree is present, let's check what type it is */
1963 if (expr->kw->match == acl_match_str)
Willy Tarreau37406352012-04-23 16:16:37 +02001964 acl_res |= acl_lookup_str(&smp, expr) ? ACL_PAT_PASS : ACL_PAT_FAIL;
Willy Tarreaub337b532010-05-13 20:03:41 +02001965 else if (expr->kw->match == acl_match_ip)
Willy Tarreau37406352012-04-23 16:16:37 +02001966 acl_res |= acl_lookup_ip(&smp, expr) ? ACL_PAT_PASS : ACL_PAT_FAIL;
Willy Tarreauc4262962010-05-10 23:42:40 +02001967 }
1968
Willy Tarreaua79534f2008-07-20 10:13:37 +02001969 /* call the match() function for all tests on this value */
1970 list_for_each_entry(pattern, &expr->patterns, list) {
Willy Tarreaua79534f2008-07-20 10:13:37 +02001971 if (acl_res == ACL_PAT_PASS)
1972 break;
Willy Tarreau37406352012-04-23 16:16:37 +02001973 acl_res |= expr->kw->match(&smp, pattern);
Willy Tarreaua79534f2008-07-20 10:13:37 +02001974 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001975 }
1976 /*
Willy Tarreau11382812008-07-09 16:18:21 +02001977 * OK now acl_res holds the result of this expression
1978 * as one of ACL_PAT_FAIL, ACL_PAT_MISS or ACL_PAT_PASS.
Willy Tarreaua84d3742007-05-07 00:36:48 +02001979 *
Willy Tarreau11382812008-07-09 16:18:21 +02001980 * Then if (!MISS) we can cache the result, and put
Willy Tarreau37406352012-04-23 16:16:37 +02001981 * (smp.flags & SMP_F_VOLATILE) in the cache flags.
Willy Tarreaua84d3742007-05-07 00:36:48 +02001982 *
1983 * FIXME: implement cache.
1984 *
1985 */
1986
Willy Tarreau11382812008-07-09 16:18:21 +02001987 /* we're ORing these terms, so a single PASS is enough */
1988 if (acl_res == ACL_PAT_PASS)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001989 break;
1990
Willy Tarreau37406352012-04-23 16:16:37 +02001991 if (smp.flags & SMP_F_NOT_LAST)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001992 goto fetch_next;
Willy Tarreaub6866442008-07-14 23:54:42 +02001993
1994 /* sometimes we know the fetched data is subject to change
1995 * later and give another chance for a new match (eg: request
1996 * size, time, ...)
1997 */
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001998 if (smp.flags & SMP_F_MAY_CHANGE && !(opt & SMP_OPT_FINAL))
Willy Tarreaub6866442008-07-14 23:54:42 +02001999 acl_res |= ACL_PAT_MISS;
Willy Tarreaua84d3742007-05-07 00:36:48 +02002000 }
2001 /*
2002 * Here we have the result of an ACL (cached or not).
2003 * ACLs are combined, negated or not, to form conditions.
2004 */
2005
Willy Tarreaua84d3742007-05-07 00:36:48 +02002006 if (term->neg)
Willy Tarreau11382812008-07-09 16:18:21 +02002007 acl_res = acl_neg(acl_res);
Willy Tarreaua84d3742007-05-07 00:36:48 +02002008
2009 suite_res &= acl_res;
Willy Tarreau11382812008-07-09 16:18:21 +02002010
2011 /* we're ANDing these terms, so a single FAIL is enough */
2012 if (suite_res == ACL_PAT_FAIL)
Willy Tarreaua84d3742007-05-07 00:36:48 +02002013 break;
2014 }
2015 cond_res |= suite_res;
Willy Tarreau11382812008-07-09 16:18:21 +02002016
2017 /* we're ORing these terms, so a single PASS is enough */
2018 if (cond_res == ACL_PAT_PASS)
Willy Tarreaua84d3742007-05-07 00:36:48 +02002019 break;
2020 }
Willy Tarreau11382812008-07-09 16:18:21 +02002021 return cond_res;
Willy Tarreaua84d3742007-05-07 00:36:48 +02002022}
2023
2024
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02002025/* Reports a pointer to the first ACL used in condition <cond> which requires
2026 * at least one of the USE_FLAGS in <require>. Returns NULL if none matches.
2027 * The construct is almost the same as for acl_exec_cond() since we're walking
2028 * down the ACL tree as well. It is important that the tree is really walked
2029 * through and never cached, because that way, this function can be used as a
2030 * late check.
2031 */
Willy Tarreauf1e98b82010-01-28 17:59:39 +01002032struct acl *cond_find_require(const struct acl_cond *cond, unsigned int require)
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02002033{
2034 struct acl_term_suite *suite;
2035 struct acl_term *term;
2036 struct acl *acl;
2037
2038 list_for_each_entry(suite, &cond->suites, list) {
2039 list_for_each_entry(term, &suite->terms, list) {
2040 acl = term->acl;
2041 if (acl->requires & require)
2042 return acl;
2043 }
2044 }
2045 return NULL;
2046}
2047
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01002048/*
2049 * Find targets for userlist and groups in acl. Function returns the number
2050 * of errors or OK if everything is fine.
2051 */
2052int
2053acl_find_targets(struct proxy *p)
2054{
2055
2056 struct acl *acl;
2057 struct acl_expr *expr;
2058 struct acl_pattern *pattern;
2059 struct userlist *ul;
Willy Tarreau63364ee2012-04-19 19:11:13 +02002060 struct arg *arg;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01002061 int cfgerr = 0;
2062
2063 list_for_each_entry(acl, &p->acl, list) {
2064 list_for_each_entry(expr, &acl->expr, list) {
Willy Tarreau63364ee2012-04-19 19:11:13 +02002065 for (arg = expr->args; arg; arg++) {
2066 if (arg->type == ARGT_STOP)
2067 break;
2068 else if (arg->type == ARGT_SRV) {
2069 struct proxy *px;
2070 struct server *srv;
2071 char *pname, *sname;
Willy Tarreau0b1cd942010-05-16 22:18:27 +02002072
Willy Tarreau63364ee2012-04-19 19:11:13 +02002073 if (!expr->args->data.str.len) {
2074 Alert("proxy %s: acl '%s' %s(): missing server name.\n",
2075 p->id, acl->name, expr->kw->kw);
2076 cfgerr++;
2077 continue;
2078 }
Willy Tarreau0b1cd942010-05-16 22:18:27 +02002079
Willy Tarreau63364ee2012-04-19 19:11:13 +02002080 pname = expr->args->data.str.str;
2081 sname = strrchr(pname, '/');
Willy Tarreau0b1cd942010-05-16 22:18:27 +02002082
Willy Tarreau63364ee2012-04-19 19:11:13 +02002083 if (sname)
2084 *sname++ = '\0';
2085 else {
2086 sname = pname;
2087 pname = NULL;
2088 }
2089
2090 px = p;
2091 if (pname) {
2092 px = findproxy(pname, PR_CAP_BE);
2093 if (!px) {
2094 Alert("proxy %s: acl '%s' %s(): unable to find proxy '%s'.\n",
2095 p->id, acl->name, expr->kw->kw, pname);
2096 cfgerr++;
2097 continue;
2098 }
2099 }
Willy Tarreau0b1cd942010-05-16 22:18:27 +02002100
Willy Tarreau63364ee2012-04-19 19:11:13 +02002101 srv = findserver(px, sname);
2102 if (!srv) {
2103 Alert("proxy %s: acl '%s' %s(): unable to find server '%s'.\n",
2104 p->id, acl->name, expr->kw->kw, sname);
Willy Tarreau0b1cd942010-05-16 22:18:27 +02002105 cfgerr++;
2106 continue;
2107 }
Willy Tarreau0b1cd942010-05-16 22:18:27 +02002108
Willy Tarreau63364ee2012-04-19 19:11:13 +02002109 free(expr->args->data.str.str);
2110 expr->args->data.srv = srv;
Willy Tarreaud28c3532012-04-19 19:28:33 +02002111 }
2112 else if (arg->type == ARGT_FE) {
Willy Tarreaufc2c1fd2012-04-19 23:35:54 +02002113 struct proxy *prx = p;
2114 char *pname = p->id;
Willy Tarreaud28c3532012-04-19 19:28:33 +02002115
Willy Tarreaufc2c1fd2012-04-19 23:35:54 +02002116 if (expr->args->data.str.len) {
2117 pname = expr->args->data.str.str;
2118 prx = findproxy(pname, PR_CAP_FE);
Willy Tarreaud28c3532012-04-19 19:28:33 +02002119 }
2120
Willy Tarreaud28c3532012-04-19 19:28:33 +02002121 if (!prx) {
2122 Alert("proxy %s: acl '%s' %s(): unable to find frontend '%s'.\n",
2123 p->id, acl->name, expr->kw->kw, pname);
2124 cfgerr++;
2125 continue;
2126 }
2127
Willy Tarreaufc2c1fd2012-04-19 23:35:54 +02002128 if (!(prx->cap & PR_CAP_FE)) {
2129 Alert("proxy %s: acl '%s' %s(): proxy '%s' has no frontend capability.\n",
2130 p->id, acl->name, expr->kw->kw, pname);
2131 cfgerr++;
2132 continue;
2133 }
2134
Willy Tarreaud28c3532012-04-19 19:28:33 +02002135 free(expr->args->data.str.str);
2136 expr->args->data.prx = prx;
2137 }
2138 else if (arg->type == ARGT_BE) {
Willy Tarreaufc2c1fd2012-04-19 23:35:54 +02002139 struct proxy *prx = p;
2140 char *pname = p->id;
Willy Tarreaud28c3532012-04-19 19:28:33 +02002141
Willy Tarreaufc2c1fd2012-04-19 23:35:54 +02002142 if (expr->args->data.str.len) {
2143 pname = expr->args->data.str.str;
2144 prx = findproxy(pname, PR_CAP_BE);
Willy Tarreaud28c3532012-04-19 19:28:33 +02002145 }
2146
Willy Tarreaud28c3532012-04-19 19:28:33 +02002147 if (!prx) {
2148 Alert("proxy %s: acl '%s' %s(): unable to find backend '%s'.\n",
2149 p->id, acl->name, expr->kw->kw, pname);
2150 cfgerr++;
2151 continue;
2152 }
2153
Willy Tarreaufc2c1fd2012-04-19 23:35:54 +02002154 if (!(prx->cap & PR_CAP_BE)) {
2155 Alert("proxy %s: acl '%s' %s(): proxy '%s' has no backend capability.\n",
2156 p->id, acl->name, expr->kw->kw, pname);
2157 cfgerr++;
2158 continue;
2159 }
2160
Willy Tarreaud28c3532012-04-19 19:28:33 +02002161 free(expr->args->data.str.str);
2162 expr->args->data.prx = prx;
2163 }
2164 else if (arg->type == ARGT_TAB) {
Willy Tarreaufc2c1fd2012-04-19 23:35:54 +02002165 struct proxy *prx = p;
2166 char *pname = p->id;
Willy Tarreaud28c3532012-04-19 19:28:33 +02002167
Willy Tarreaufc2c1fd2012-04-19 23:35:54 +02002168 if (expr->args->data.str.len) {
2169 pname = expr->args->data.str.str;
2170 prx = find_stktable(pname);
Willy Tarreaud28c3532012-04-19 19:28:33 +02002171 }
2172
Willy Tarreaud28c3532012-04-19 19:28:33 +02002173 if (!prx) {
2174 Alert("proxy %s: acl '%s' %s(): unable to find table '%s'.\n",
2175 p->id, acl->name, expr->kw->kw, pname);
2176 cfgerr++;
2177 continue;
2178 }
2179
Willy Tarreaufc2c1fd2012-04-19 23:35:54 +02002180
2181 if (!prx->table.size) {
2182 Alert("proxy %s: acl '%s' %s(): no table in proxy '%s'.\n",
2183 p->id, acl->name, expr->kw->kw, pname);
2184 cfgerr++;
2185 continue;
2186 }
2187
Willy Tarreaud28c3532012-04-19 19:28:33 +02002188 free(expr->args->data.str.str);
2189 expr->args->data.prx = prx;
Willy Tarreau0b1cd942010-05-16 22:18:27 +02002190 }
Willy Tarreau63364ee2012-04-19 19:11:13 +02002191 else if (arg->type == ARGT_USR) {
2192 if (!expr->args->data.str.len) {
2193 Alert("proxy %s: acl '%s' %s(): missing userlist name.\n",
2194 p->id, acl->name, expr->kw->kw);
2195 cfgerr++;
2196 continue;
2197 }
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01002198
Willy Tarreau63364ee2012-04-19 19:11:13 +02002199 if (p->uri_auth && p->uri_auth->userlist &&
2200 !strcmp(p->uri_auth->userlist->name, expr->args->data.str.str))
2201 ul = p->uri_auth->userlist;
2202 else
2203 ul = auth_find_userlist(expr->args->data.str.str);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01002204
Willy Tarreau63364ee2012-04-19 19:11:13 +02002205 if (!ul) {
2206 Alert("proxy %s: acl '%s' %s(%s): unable to find userlist.\n",
2207 p->id, acl->name, expr->kw->kw, expr->args->data.str.str);
2208 cfgerr++;
2209 continue;
2210 }
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01002211
Willy Tarreau63364ee2012-04-19 19:11:13 +02002212 free(expr->args->data.str.str);
2213 expr->args->data.usr = ul;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01002214 }
Willy Tarreau63364ee2012-04-19 19:11:13 +02002215 } /* end of args processing */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01002216
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01002217
2218 if (!strcmp(expr->kw->kw, "http_auth_group")) {
Willy Tarreau63364ee2012-04-19 19:11:13 +02002219 /* note: argument resolved above thanks to ARGT_USR */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01002220
2221 if (LIST_ISEMPTY(&expr->patterns)) {
2222 Alert("proxy %s: acl %s %s(): no groups specified.\n",
2223 p->id, acl->name, expr->kw->kw);
2224 cfgerr++;
2225 continue;
2226 }
2227
2228 list_for_each_entry(pattern, &expr->patterns, list) {
Willy Tarreau34db1082012-04-19 17:16:54 +02002229 pattern->val.group_mask = auth_resolve_groups(expr->args->data.usr, pattern->ptr.str);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01002230
2231 free(pattern->ptr.str);
2232 pattern->ptr.str = NULL;
2233 pattern->len = 0;
2234
2235 if (!pattern->val.group_mask) {
2236 Alert("proxy %s: acl %s %s(): invalid group(s).\n",
2237 p->id, acl->name, expr->kw->kw);
2238 cfgerr++;
2239 continue;
2240 }
2241 }
2242 }
2243 }
2244 }
2245
2246 return cfgerr;
2247}
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02002248
Willy Tarreaua84d3742007-05-07 00:36:48 +02002249/************************************************************************/
2250/* All supported keywords must be declared here. */
2251/************************************************************************/
2252
Willy Tarreau61612d42012-04-19 18:42:05 +02002253/* Note: must not be declared <const> as its list will be overwritten.
2254 * Please take care of keeping this list alphabetically sorted.
2255 */
Willy Tarreaua84d3742007-05-07 00:36:48 +02002256static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau61612d42012-04-19 18:42:05 +02002257 { "always_false", acl_parse_nothing, acl_fetch_false, acl_match_nothing, ACL_USE_NOTHING, 0 },
2258 { "always_true", acl_parse_nothing, acl_fetch_true, acl_match_nothing, ACL_USE_NOTHING, 0 },
2259 { "rep_ssl_hello_type", acl_parse_int, acl_fetch_ssl_hello_type, acl_match_int, ACL_USE_L6RTR_VOLATILE, 0 },
2260 { "req_len", acl_parse_int, acl_fetch_req_len, acl_match_int, ACL_USE_L6REQ_VOLATILE, 0 },
Willy Tarreau61612d42012-04-19 18:42:05 +02002261 { "req_ssl_hello_type", acl_parse_int, acl_fetch_ssl_hello_type, acl_match_int, ACL_USE_L6REQ_VOLATILE, 0 },
2262 { "req_ssl_sni", acl_parse_str, acl_fetch_ssl_hello_sni, acl_match_str, ACL_USE_L6REQ_VOLATILE|ACL_MAY_LOOKUP, 0 },
2263 { "req_ssl_ver", acl_parse_dotted_ver, acl_fetch_req_ssl_ver, acl_match_int, ACL_USE_L6REQ_VOLATILE, 0 },
2264 { "wait_end", acl_parse_nothing, acl_fetch_wait_end, acl_match_nothing, ACL_USE_NOTHING, 0 },
Willy Tarreaua84d3742007-05-07 00:36:48 +02002265 { NULL, NULL, NULL, NULL }
2266}};
2267
2268
2269__attribute__((constructor))
2270static void __acl_init(void)
2271{
2272 acl_register_keywords(&acl_kws);
2273}
2274
2275
2276/*
2277 * Local variables:
2278 * c-indent-level: 8
2279 * c-basic-offset: 8
2280 * End:
2281 */