blob: 9a20cc25f9462a07cab0add71349fb9f102f5771 [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>
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010025#include <proto/auth.h>
Willy Tarreau44b90cc2010-05-24 20:27:29 +020026#include <proto/buffers.h>
Willy Tarreau404e8ab2009-07-26 19:40:40 +020027#include <proto/log.h>
Willy Tarreau0b1cd942010-05-16 22:18:27 +020028#include <proto/proxy.h>
Willy Tarreaua84d3742007-05-07 00:36:48 +020029
Willy Tarreauc4262962010-05-10 23:42:40 +020030#include <ebsttree.h>
31
Willy Tarreaua9802632008-07-25 19:13:19 +020032/* The capabilities of filtering hooks describe the type of information
33 * available to each of them.
34 */
35const unsigned int filt_cap[] = {
36 [ACL_HOOK_REQ_FE_TCP] = ACL_USE_TCP4_ANY|ACL_USE_TCP6_ANY|ACL_USE_TCP_ANY,
Willy Tarreau06457872010-05-23 12:24:38 +020037 [ACL_HOOK_REQ_FE_TCP_CONTENT] = ACL_USE_TCP4_ANY|ACL_USE_TCP6_ANY|ACL_USE_TCP_ANY|ACL_USE_L6REQ_ANY,
38 [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,
39 [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,
40 [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,
41 [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,
42 [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,
43 [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,
44 [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 +020045
Willy Tarreau06457872010-05-23 12:24:38 +020046 [ACL_HOOK_RTR_BE_TCP_CONTENT] = ACL_USE_REQ_PERMANENT|ACL_USE_REQ_CACHEABLE|ACL_USE_L6RTR_ANY,
47 [ACL_HOOK_RTR_BE_HTTP_IN] = ACL_USE_REQ_PERMANENT|ACL_USE_REQ_CACHEABLE|ACL_USE_L6RTR_ANY|ACL_USE_L7RTR_ANY,
48 [ACL_HOOK_RTR_FE_TCP_CONTENT] = ACL_USE_REQ_PERMANENT|ACL_USE_REQ_CACHEABLE|ACL_USE_L6RTR_ANY|ACL_USE_L7RTR_ANY,
49 [ACL_HOOK_RTR_FE_HTTP_IN] = ACL_USE_REQ_PERMANENT|ACL_USE_REQ_CACHEABLE|ACL_USE_L6RTR_ANY|ACL_USE_L7RTR_ANY,
50 [ACL_HOOK_RTR_BE_HTTP_OUT] = ACL_USE_REQ_PERMANENT|ACL_USE_REQ_CACHEABLE|ACL_USE_L6RTR_ANY|ACL_USE_L7RTR_ANY,
51 [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 +020052};
53
Willy Tarreaua84d3742007-05-07 00:36:48 +020054/* List head of all known ACL keywords */
55static struct acl_kw_list acl_keywords = {
56 .list = LIST_HEAD_INIT(acl_keywords.list)
57};
58
59
Willy Tarreaua5909832007-06-17 20:40:25 +020060/*
61 * These functions are only used for debugging complex configurations.
Willy Tarreaua84d3742007-05-07 00:36:48 +020062 */
Willy Tarreaua5909832007-06-17 20:40:25 +020063
Willy Tarreau58393e12008-07-20 10:39:22 +020064/* force TRUE to be returned at the fetch level */
Willy Tarreaua5909832007-06-17 20:40:25 +020065static int
Willy Tarreau58393e12008-07-20 10:39:22 +020066acl_fetch_true(struct proxy *px, struct session *l4, void *l7, int dir,
67 struct acl_expr *expr, struct acl_test *test)
Willy Tarreaua5909832007-06-17 20:40:25 +020068{
Willy Tarreau58393e12008-07-20 10:39:22 +020069 test->flags |= ACL_TEST_F_SET_RES_PASS;
Willy Tarreaua5909832007-06-17 20:40:25 +020070 return 1;
71}
72
Willy Tarreaub6fb4202008-07-20 11:18:28 +020073/* wait for more data as long as possible, then return TRUE. This should be
74 * used with content inspection.
75 */
76static int
77acl_fetch_wait_end(struct proxy *px, struct session *l4, void *l7, int dir,
78 struct acl_expr *expr, struct acl_test *test)
79{
80 if (dir & ACL_PARTIAL) {
81 test->flags |= ACL_TEST_F_MAY_CHANGE;
82 return 0;
83 }
84 test->flags |= ACL_TEST_F_SET_RES_PASS;
85 return 1;
86}
87
Willy Tarreau58393e12008-07-20 10:39:22 +020088/* force FALSE to be returned at the fetch level */
Willy Tarreaua5909832007-06-17 20:40:25 +020089static int
Willy Tarreau58393e12008-07-20 10:39:22 +020090acl_fetch_false(struct proxy *px, struct session *l4, void *l7, int dir,
91 struct acl_expr *expr, struct acl_test *test)
Willy Tarreaua84d3742007-05-07 00:36:48 +020092{
Willy Tarreau58393e12008-07-20 10:39:22 +020093 test->flags |= ACL_TEST_F_SET_RES_FAIL;
Willy Tarreaua84d3742007-05-07 00:36:48 +020094 return 1;
95}
96
Willy Tarreau44b90cc2010-05-24 20:27:29 +020097/* return the number of bytes in the request buffer */
98static int
99acl_fetch_req_len(struct proxy *px, struct session *l4, void *l7, int dir,
100 struct acl_expr *expr, struct acl_test *test)
101{
102 if (!l4 || !l4->req)
103 return 0;
104
Willy Tarreaua5e37562011-12-16 17:06:15 +0100105 temp_pattern.data.integer = l4->req->l;
Willy Tarreau44b90cc2010-05-24 20:27:29 +0200106 test->flags = ACL_TEST_F_VOLATILE | ACL_TEST_F_MAY_CHANGE;
107 return 1;
108}
109
Emeric Brun38e71762010-09-23 17:59:18 +0200110
111static int
112acl_fetch_ssl_hello_type(struct proxy *px, struct session *l4, void *l7, int dir,
113 struct acl_expr *expr, struct acl_test *test)
114{
115 int hs_len;
116 int hs_type, bleft;
117 struct buffer *b;
118 const unsigned char *data;
119
120 if (!l4)
121 goto not_ssl_hello;
122
123 b = ((dir & ACL_DIR_MASK) == ACL_DIR_RTR) ? l4->rep : l4->req;
124
125 bleft = b->l;
126 data = (const unsigned char *)b->w;
127
128 if (!bleft)
129 goto too_short;
130
131 if ((*data >= 0x14 && *data <= 0x17) || (*data == 0xFF)) {
132 /* SSLv3 header format */
133 if (bleft < 9)
134 goto too_short;
135
136 /* ssl version 3 */
137 if ((data[1] << 16) + data[2] < 0x00030000)
138 goto not_ssl_hello;
139
140 /* ssl message len must present handshake type and len */
141 if ((data[3] << 8) + data[4] < 4)
142 goto not_ssl_hello;
143
144 /* format introduced with SSLv3 */
145
146 hs_type = (int)data[5];
147 hs_len = ( data[6] << 16 ) + ( data[7] << 8 ) + data[8];
148
149 /* not a full handshake */
150 if (bleft < (9 + hs_len))
151 goto too_short;
152
153 }
154 else {
155 goto not_ssl_hello;
156 }
157
Willy Tarreaua5e37562011-12-16 17:06:15 +0100158 temp_pattern.data.integer = hs_type;
Emeric Brun38e71762010-09-23 17:59:18 +0200159 test->flags = ACL_TEST_F_VOLATILE;
160
161 return 1;
162
163 too_short:
164 test->flags = ACL_TEST_F_MAY_CHANGE;
165
166 not_ssl_hello:
167
168 return 0;
169}
170
Willy Tarreau44b90cc2010-05-24 20:27:29 +0200171/* Return the version of the SSL protocol in the request. It supports both
172 * SSLv3 (TLSv1) header format for any message, and SSLv2 header format for
173 * the hello message. The SSLv3 format is described in RFC 2246 p49, and the
174 * SSLv2 format is described here, and completed p67 of RFC 2246 :
175 * http://wp.netscape.com/eng/security/SSL_2.html
176 *
177 * Note: this decoder only works with non-wrapping data.
178 */
179static int
180acl_fetch_req_ssl_ver(struct proxy *px, struct session *l4, void *l7, int dir,
181 struct acl_expr *expr, struct acl_test *test)
182{
183 int version, bleft, msg_len;
184 const unsigned char *data;
185
186 if (!l4 || !l4->req)
187 return 0;
188
189 msg_len = 0;
190 bleft = l4->req->l;
191 if (!bleft)
192 goto too_short;
193
194 data = (const unsigned char *)l4->req->w;
195 if ((*data >= 0x14 && *data <= 0x17) || (*data == 0xFF)) {
196 /* SSLv3 header format */
197 if (bleft < 5)
198 goto too_short;
199
200 version = (data[1] << 16) + data[2]; /* version: major, minor */
201 msg_len = (data[3] << 8) + data[4]; /* record length */
202
203 /* format introduced with SSLv3 */
204 if (version < 0x00030000)
205 goto not_ssl;
206
207 /* message length between 1 and 2^14 + 2048 */
208 if (msg_len < 1 || msg_len > ((1<<14) + 2048))
209 goto not_ssl;
210
211 bleft -= 5; data += 5;
212 } else {
213 /* SSLv2 header format, only supported for hello (msg type 1) */
214 int rlen, plen, cilen, silen, chlen;
215
216 if (*data & 0x80) {
217 if (bleft < 3)
218 goto too_short;
219 /* short header format : 15 bits for length */
220 rlen = ((data[0] & 0x7F) << 8) | data[1];
221 plen = 0;
222 bleft -= 2; data += 2;
223 } else {
224 if (bleft < 4)
225 goto too_short;
226 /* long header format : 14 bits for length + pad length */
227 rlen = ((data[0] & 0x3F) << 8) | data[1];
228 plen = data[2];
229 bleft -= 3; data += 2;
230 }
231
232 if (*data != 0x01)
233 goto not_ssl;
234 bleft--; data++;
235
236 if (bleft < 8)
237 goto too_short;
238 version = (data[0] << 16) + data[1]; /* version: major, minor */
239 cilen = (data[2] << 8) + data[3]; /* cipher len, multiple of 3 */
240 silen = (data[4] << 8) + data[5]; /* session_id_len: 0 or 16 */
241 chlen = (data[6] << 8) + data[7]; /* 16<=challenge length<=32 */
242
243 bleft -= 8; data += 8;
244 if (cilen % 3 != 0)
245 goto not_ssl;
246 if (silen && silen != 16)
247 goto not_ssl;
248 if (chlen < 16 || chlen > 32)
249 goto not_ssl;
250 if (rlen != 9 + cilen + silen + chlen)
251 goto not_ssl;
252
253 /* focus on the remaining data length */
254 msg_len = cilen + silen + chlen + plen;
255 }
256 /* We could recursively check that the buffer ends exactly on an SSL
257 * fragment boundary and that a possible next segment is still SSL,
258 * but that's a bit pointless. However, we could still check that
259 * all the part of the request which fits in a buffer is already
260 * there.
261 */
262 if (msg_len > buffer_max_len(l4->req) + l4->req->data - l4->req->w)
263 msg_len = buffer_max_len(l4->req) + l4->req->data - l4->req->w;
264
265 if (bleft < msg_len)
266 goto too_short;
267
268 /* OK that's enough. We have at least the whole message, and we have
269 * the protocol version.
270 */
Willy Tarreaua5e37562011-12-16 17:06:15 +0100271 temp_pattern.data.integer = version;
Willy Tarreau44b90cc2010-05-24 20:27:29 +0200272 test->flags = ACL_TEST_F_VOLATILE;
273 return 1;
274
275 too_short:
276 test->flags = ACL_TEST_F_MAY_CHANGE;
277 not_ssl:
278 return 0;
279}
280
Willy Tarreaub6672b52011-12-12 17:23:41 +0100281/* Try to extract the Server Name Indication that may be presented in a TLS
282 * client hello handshake message. The format of the message is the following
283 * (cf RFC5246 + RFC6066) :
284 * TLS frame :
285 * - uint8 type = 0x16 (Handshake)
286 * - uint16 version >= 0x0301 (TLSv1)
287 * - uint16 length (frame length)
288 * - TLS handshake :
289 * - uint8 msg_type = 0x01 (ClientHello)
290 * - uint24 length (handshake message length)
291 * - ClientHello :
292 * - uint16 client_version >= 0x0301 (TLSv1)
293 * - uint8 Random[32]
294 * - SessionID :
295 * - uint8 session_id_len (0..32) (SessionID len in bytes)
296 * - uint8 session_id[session_id_len]
297 * - CipherSuite :
298 * - uint16 cipher_len >= 2 (Cipher length in bytes)
299 * - uint16 ciphers[cipher_len/2]
300 * - CompressionMethod :
301 * - uint8 compression_len >= 1 (# of supported methods)
302 * - uint8 compression_methods[compression_len]
303 * - optional client_extension_len (in bytes)
304 * - optional sequence of ClientHelloExtensions (as many bytes as above):
305 * - uint16 extension_type = 0 for server_name
306 * - uint16 extension_len
307 * - opaque extension_data[extension_len]
308 * - uint16 server_name_list_len (# of bytes here)
309 * - opaque server_names[server_name_list_len bytes]
310 * - uint8 name_type = 0 for host_name
311 * - uint16 name_len
312 * - opaque hostname[name_len bytes]
313 */
314static int
315acl_fetch_ssl_hello_sni(struct proxy *px, struct session *l4, void *l7, int dir,
316 struct acl_expr *expr, struct acl_test *test)
317{
318 int hs_len, ext_len, bleft;
319 struct buffer *b;
320 unsigned char *data;
321
322 if (!l4)
323 goto not_ssl_hello;
324
325 b = ((dir & ACL_DIR_MASK) == ACL_DIR_RTR) ? l4->rep : l4->req;
326
327 bleft = b->l;
328 data = (unsigned char *)b->w;
329
330 /* Check for SSL/TLS Handshake */
331 if (!bleft)
332 goto too_short;
333 if (*data != 0x16)
334 goto not_ssl_hello;
335
336 /* Check for TLSv1 or later (SSL version >= 3.1) */
337 if (bleft < 3)
338 goto too_short;
339 if (data[1] < 0x03 || data[2] < 0x01)
340 goto not_ssl_hello;
341
342 if (bleft < 5)
343 goto too_short;
344 hs_len = (data[3] << 8) + data[4];
345 if (hs_len < 1 + 3 + 2 + 32 + 1 + 2 + 2 + 1 + 1 + 2 + 2)
346 goto not_ssl_hello; /* too short to have an extension */
347
348 data += 5; /* enter TLS handshake */
349 bleft -= 5;
350
351 /* Check for a complete client hello starting at <data> */
352 if (bleft < 1)
353 goto too_short;
354 if (data[0] != 0x01) /* msg_type = Client Hello */
355 goto not_ssl_hello;
356
357 /* Check the Hello's length */
358 if (bleft < 4)
359 goto too_short;
360 hs_len = (data[1] << 16) + (data[2] << 8) + data[3];
361 if (hs_len < 2 + 32 + 1 + 2 + 2 + 1 + 1 + 2 + 2)
362 goto not_ssl_hello; /* too short to have an extension */
363
364 /* We want the full handshake here */
365 if (bleft < hs_len)
366 goto too_short;
367
368 data += 4;
369 /* Start of the ClientHello message */
370 if (data[0] < 0x03 || data[1] < 0x01) /* TLSv1 minimum */
371 goto not_ssl_hello;
372
373 ext_len = data[35];
374 if (ext_len > 32 || ext_len > (hs_len - 35)) /* check for correct session_id len */
375 goto not_ssl_hello;
376
377 /* Jump to cipher suite */
378 hs_len -= 35 + ext_len;
379 data += 35 + ext_len;
380
381 if (hs_len < 4 || /* minimum one cipher */
382 (ext_len = (data[0] << 8) + data[1]) < 2 || /* minimum 2 bytes for a cipher */
383 ext_len > hs_len)
384 goto not_ssl_hello;
385
386 /* Jump to the compression methods */
387 hs_len -= 2 + ext_len;
388 data += 2 + ext_len;
389
390 if (hs_len < 2 || /* minimum one compression method */
391 data[0] < 1 || data[0] > hs_len) /* minimum 1 bytes for a method */
392 goto not_ssl_hello;
393
394 /* Jump to the extensions */
395 hs_len -= 1 + data[0];
396 data += 1 + data[0];
397
398 if (hs_len < 2 || /* minimum one extension list length */
399 (ext_len = (data[0] << 8) + data[1]) > hs_len - 2) /* list too long */
400 goto not_ssl_hello;
401
402 hs_len = ext_len; /* limit ourselves to the extension length */
403 data += 2;
404
405 while (hs_len >= 4) {
406 int ext_type, name_type, srv_len, name_len;
407
408 ext_type = (data[0] << 8) + data[1];
409 ext_len = (data[2] << 8) + data[3];
410
411 if (ext_len > hs_len - 4) /* Extension too long */
412 goto not_ssl_hello;
413
414 if (ext_type == 0) { /* Server name */
415 if (ext_len < 2) /* need one list length */
416 goto not_ssl_hello;
417
418 srv_len = (data[4] << 8) + data[5];
419 if (srv_len < 4 || srv_len > hs_len - 6)
420 goto not_ssl_hello; /* at least 4 bytes per server name */
421
422 name_type = data[6];
423 name_len = (data[7] << 8) + data[8];
424
425 if (name_type == 0) { /* hostname */
Willy Tarreau664092c2011-12-16 19:11:42 +0100426 temp_pattern.data.str.str = data + 9;
427 temp_pattern.data.str.len = name_len;
Willy Tarreaub6672b52011-12-12 17:23:41 +0100428 test->flags = ACL_TEST_F_VOLATILE;
429 //fprintf(stderr, "found SNI : <");
430 //write(2, test->ptr, test->len);
431 //fprintf(stderr, ">\n");
432 return 1;
433 }
434 }
435
436 hs_len -= 4 + ext_len;
437 data += 4 + ext_len;
438 }
439 /* server name not found */
440 goto not_ssl_hello;
441
442 too_short:
443 test->flags = ACL_TEST_F_MAY_CHANGE;
444
445 not_ssl_hello:
446
447 return 0;
448}
449
Willy Tarreau44b90cc2010-05-24 20:27:29 +0200450/* Fetch the RDP cookie identified in the expression.
451 * Note: this decoder only works with non-wrapping data.
452 */
453int
454acl_fetch_rdp_cookie(struct proxy *px, struct session *l4, void *l7, int dir,
455 struct acl_expr *expr, struct acl_test *test)
456{
457 int bleft;
458 const unsigned char *data;
459
460 if (!l4 || !l4->req)
461 return 0;
462
463 test->flags = 0;
464
465 bleft = l4->req->l;
466 if (bleft <= 11)
467 goto too_short;
468
469 data = (const unsigned char *)l4->req->w + 11;
470 bleft -= 11;
471
472 if (bleft <= 7)
473 goto too_short;
474
475 if (strncasecmp((const char *)data, "Cookie:", 7) != 0)
476 goto not_cookie;
477
478 data += 7;
479 bleft -= 7;
480
481 while (bleft > 0 && *data == ' ') {
482 data++;
483 bleft--;
484 }
485
486 if (expr->arg_len) {
487
488 if (bleft <= expr->arg_len)
489 goto too_short;
490
491 if ((data[expr->arg_len] != '=') ||
492 strncasecmp(expr->arg.str, (const char *)data, expr->arg_len) != 0)
493 goto not_cookie;
494
495 data += expr->arg_len + 1;
496 bleft -= expr->arg_len + 1;
497 } else {
498 while (bleft > 0 && *data != '=') {
499 if (*data == '\r' || *data == '\n')
500 goto not_cookie;
501 data++;
502 bleft--;
503 }
504
505 if (bleft < 1)
506 goto too_short;
507
508 if (*data != '=')
509 goto not_cookie;
510
511 data++;
512 bleft--;
513 }
514
515 /* data points to cookie value */
Willy Tarreau664092c2011-12-16 19:11:42 +0100516 temp_pattern.data.str.str = (char *)data;
517 temp_pattern.data.str.len = 0;
Willy Tarreau44b90cc2010-05-24 20:27:29 +0200518
519 while (bleft > 0 && *data != '\r') {
520 data++;
521 bleft--;
522 }
523
524 if (bleft < 2)
525 goto too_short;
526
527 if (data[0] != '\r' || data[1] != '\n')
528 goto not_cookie;
529
Willy Tarreau664092c2011-12-16 19:11:42 +0100530 temp_pattern.data.str.len = (char *)data - temp_pattern.data.str.str;
Willy Tarreau44b90cc2010-05-24 20:27:29 +0200531 test->flags = ACL_TEST_F_VOLATILE;
532 return 1;
533
534 too_short:
535 test->flags = ACL_TEST_F_MAY_CHANGE;
536 not_cookie:
537 return 0;
538}
539
540static int
541acl_fetch_rdp_cookie_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
542 struct acl_expr *expr, struct acl_test *test)
543{
544 int ret;
545
546 ret = acl_fetch_rdp_cookie(px, l4, l7, dir, expr, test);
547
Willy Tarreau664092c2011-12-16 19:11:42 +0100548 temp_pattern.data.str.str = NULL;
549 temp_pattern.data.str.len = 0;
Willy Tarreau44b90cc2010-05-24 20:27:29 +0200550
551 if (test->flags & ACL_TEST_F_MAY_CHANGE)
552 return 0;
553
554 test->flags = ACL_TEST_F_VOLATILE;
Willy Tarreaua5e37562011-12-16 17:06:15 +0100555 temp_pattern.data.integer = ret;
Willy Tarreau44b90cc2010-05-24 20:27:29 +0200556
557 return 1;
558}
559
Willy Tarreau58393e12008-07-20 10:39:22 +0200560
561/*
562 * These functions are exported and may be used by any other component.
563 */
564
565/* ignore the current line */
566int acl_parse_nothing(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreaua5909832007-06-17 20:40:25 +0200567{
Willy Tarreau58393e12008-07-20 10:39:22 +0200568 return 1;
569}
570
571/* always fake a data retrieval */
572int acl_fetch_nothing(struct proxy *px, struct session *l4, void *l7, int dir,
573 struct acl_expr *expr, struct acl_test *test)
574{
575 return 1;
Willy Tarreaua5909832007-06-17 20:40:25 +0200576}
577
578/* always return false */
Willy Tarreau58393e12008-07-20 10:39:22 +0200579int acl_match_nothing(struct acl_test *test, struct acl_pattern *pattern)
Willy Tarreaua5909832007-06-17 20:40:25 +0200580{
Willy Tarreau11382812008-07-09 16:18:21 +0200581 return ACL_PAT_FAIL;
Willy Tarreaua5909832007-06-17 20:40:25 +0200582}
583
584
Willy Tarreaua84d3742007-05-07 00:36:48 +0200585/* NB: For two strings to be identical, it is required that their lengths match */
586int acl_match_str(struct acl_test *test, struct acl_pattern *pattern)
587{
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200588 int icase;
589
Willy Tarreau664092c2011-12-16 19:11:42 +0100590 if (pattern->len != temp_pattern.data.str.len)
Willy Tarreau11382812008-07-09 16:18:21 +0200591 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200592
593 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
Willy Tarreau664092c2011-12-16 19:11:42 +0100594 if ((icase && strncasecmp(pattern->ptr.str, temp_pattern.data.str.str, temp_pattern.data.str.len) == 0) ||
595 (!icase && strncmp(pattern->ptr.str, temp_pattern.data.str.str, temp_pattern.data.str.len) == 0))
Willy Tarreau11382812008-07-09 16:18:21 +0200596 return ACL_PAT_PASS;
597 return ACL_PAT_FAIL;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200598}
599
Willy Tarreauc4262962010-05-10 23:42:40 +0200600/* Lookup a string in the expression's pattern tree. The node is returned if it
601 * exists, otherwise NULL.
602 */
603void *acl_lookup_str(struct acl_test *test, struct acl_expr *expr)
604{
605 /* data are stored in a tree */
606 struct ebmb_node *node;
607 char prev;
608
609 /* we may have to force a trailing zero on the test pattern */
Willy Tarreau664092c2011-12-16 19:11:42 +0100610 prev = temp_pattern.data.str.str[temp_pattern.data.str.len];
Willy Tarreauc4262962010-05-10 23:42:40 +0200611 if (prev)
Willy Tarreau664092c2011-12-16 19:11:42 +0100612 temp_pattern.data.str.str[temp_pattern.data.str.len] = '\0';
613 node = ebst_lookup(&expr->pattern_tree, temp_pattern.data.str.str);
Willy Tarreauc4262962010-05-10 23:42:40 +0200614 if (prev)
Willy Tarreau664092c2011-12-16 19:11:42 +0100615 temp_pattern.data.str.str[temp_pattern.data.str.len] = prev;
Willy Tarreauc4262962010-05-10 23:42:40 +0200616 return node;
617}
618
Willy Tarreauf3d25982007-05-08 22:45:09 +0200619/* Executes a regex. It needs to change the data. If it is marked READ_ONLY
620 * then it will be allocated and duplicated in place so that others may use
621 * it later on. Note that this is embarrassing because we always try to avoid
622 * allocating memory at run time.
623 */
624int acl_match_reg(struct acl_test *test, struct acl_pattern *pattern)
625{
626 char old_char;
627 int ret;
628
629 if (unlikely(test->flags & ACL_TEST_F_READ_ONLY)) {
630 char *new_str;
631
Willy Tarreau664092c2011-12-16 19:11:42 +0100632 new_str = calloc(1, temp_pattern.data.str.len + 1);
Willy Tarreauf3d25982007-05-08 22:45:09 +0200633 if (!new_str)
Willy Tarreau11382812008-07-09 16:18:21 +0200634 return ACL_PAT_FAIL;
Willy Tarreauf3d25982007-05-08 22:45:09 +0200635
Willy Tarreau664092c2011-12-16 19:11:42 +0100636 memcpy(new_str, temp_pattern.data.str.str, temp_pattern.data.str.len);
637 new_str[temp_pattern.data.str.len] = 0;
Willy Tarreauf3d25982007-05-08 22:45:09 +0200638 if (test->flags & ACL_TEST_F_MUST_FREE)
Willy Tarreau664092c2011-12-16 19:11:42 +0100639 free(temp_pattern.data.str.str);
640 temp_pattern.data.str.str = new_str;
Willy Tarreauf3d25982007-05-08 22:45:09 +0200641 test->flags |= ACL_TEST_F_MUST_FREE;
642 test->flags &= ~ACL_TEST_F_READ_ONLY;
643 }
644
Willy Tarreau664092c2011-12-16 19:11:42 +0100645 old_char = temp_pattern.data.str.str[temp_pattern.data.str.len];
646 temp_pattern.data.str.str[temp_pattern.data.str.len] = 0;
Willy Tarreauf3d25982007-05-08 22:45:09 +0200647
Willy Tarreau664092c2011-12-16 19:11:42 +0100648 if (regexec(pattern->ptr.reg, temp_pattern.data.str.str, 0, NULL, 0) == 0)
Willy Tarreau11382812008-07-09 16:18:21 +0200649 ret = ACL_PAT_PASS;
Willy Tarreauf3d25982007-05-08 22:45:09 +0200650 else
Willy Tarreau11382812008-07-09 16:18:21 +0200651 ret = ACL_PAT_FAIL;
Willy Tarreauf3d25982007-05-08 22:45:09 +0200652
Willy Tarreau664092c2011-12-16 19:11:42 +0100653 temp_pattern.data.str.str[temp_pattern.data.str.len] = old_char;
Willy Tarreauf3d25982007-05-08 22:45:09 +0200654 return ret;
655}
656
Willy Tarreaua84d3742007-05-07 00:36:48 +0200657/* Checks that the pattern matches the beginning of the tested string. */
658int acl_match_beg(struct acl_test *test, struct acl_pattern *pattern)
659{
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200660 int icase;
661
Willy Tarreau664092c2011-12-16 19:11:42 +0100662 if (pattern->len > temp_pattern.data.str.len)
Willy Tarreau11382812008-07-09 16:18:21 +0200663 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200664
665 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
Willy Tarreau664092c2011-12-16 19:11:42 +0100666 if ((icase && strncasecmp(pattern->ptr.str, temp_pattern.data.str.str, pattern->len) != 0) ||
667 (!icase && strncmp(pattern->ptr.str, temp_pattern.data.str.str, pattern->len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +0200668 return ACL_PAT_FAIL;
669 return ACL_PAT_PASS;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200670}
671
672/* Checks that the pattern matches the end of the tested string. */
673int acl_match_end(struct acl_test *test, struct acl_pattern *pattern)
674{
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200675 int icase;
676
Willy Tarreau664092c2011-12-16 19:11:42 +0100677 if (pattern->len > temp_pattern.data.str.len)
Willy Tarreau11382812008-07-09 16:18:21 +0200678 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200679 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
Willy Tarreau664092c2011-12-16 19:11:42 +0100680 if ((icase && strncasecmp(pattern->ptr.str, temp_pattern.data.str.str + temp_pattern.data.str.len - pattern->len, pattern->len) != 0) ||
681 (!icase && strncmp(pattern->ptr.str, temp_pattern.data.str.str + temp_pattern.data.str.len - pattern->len, pattern->len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +0200682 return ACL_PAT_FAIL;
683 return ACL_PAT_PASS;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200684}
685
686/* Checks that the pattern is included inside the tested string.
687 * NB: Suboptimal, should be rewritten using a Boyer-Moore method.
688 */
689int acl_match_sub(struct acl_test *test, struct acl_pattern *pattern)
690{
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200691 int icase;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200692 char *end;
693 char *c;
694
Willy Tarreau664092c2011-12-16 19:11:42 +0100695 if (pattern->len > temp_pattern.data.str.len)
Willy Tarreau11382812008-07-09 16:18:21 +0200696 return ACL_PAT_FAIL;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200697
Willy Tarreau664092c2011-12-16 19:11:42 +0100698 end = temp_pattern.data.str.str + temp_pattern.data.str.len - pattern->len;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200699 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
700 if (icase) {
Willy Tarreau664092c2011-12-16 19:11:42 +0100701 for (c = temp_pattern.data.str.str; c <= end; c++) {
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200702 if (tolower(*c) != tolower(*pattern->ptr.str))
703 continue;
704 if (strncasecmp(pattern->ptr.str, c, pattern->len) == 0)
Willy Tarreau11382812008-07-09 16:18:21 +0200705 return ACL_PAT_PASS;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200706 }
707 } else {
Willy Tarreau664092c2011-12-16 19:11:42 +0100708 for (c = temp_pattern.data.str.str; c <= end; c++) {
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200709 if (*c != *pattern->ptr.str)
710 continue;
711 if (strncmp(pattern->ptr.str, c, pattern->len) == 0)
Willy Tarreau11382812008-07-09 16:18:21 +0200712 return ACL_PAT_PASS;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200713 }
Willy Tarreaua84d3742007-05-07 00:36:48 +0200714 }
Willy Tarreau11382812008-07-09 16:18:21 +0200715 return ACL_PAT_FAIL;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200716}
717
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200718/* Background: Fast way to find a zero byte in a word
719 * http://graphics.stanford.edu/~seander/bithacks.html#ZeroInWord
720 * hasZeroByte = (v - 0x01010101UL) & ~v & 0x80808080UL;
721 *
722 * To look for 4 different byte values, xor the word with those bytes and
723 * then check for zero bytes:
724 *
725 * v = (((unsigned char)c * 0x1010101U) ^ delimiter)
726 * where <delimiter> is the 4 byte values to look for (as an uint)
727 * and <c> is the character that is being tested
728 */
729static inline unsigned int is_delimiter(unsigned char c, unsigned int mask)
730{
731 mask ^= (c * 0x01010101); /* propagate the char to all 4 bytes */
732 return (mask - 0x01010101) & ~mask & 0x80808080U;
733}
734
735static inline unsigned int make_4delim(unsigned char d1, unsigned char d2, unsigned char d3, unsigned char d4)
736{
737 return d1 << 24 | d2 << 16 | d3 << 8 | d4;
738}
739
Willy Tarreaua84d3742007-05-07 00:36:48 +0200740/* This one is used by other real functions. It checks that the pattern is
741 * included inside the tested string, but enclosed between the specified
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200742 * delimiters or at the beginning or end of the string. The delimiters are
743 * provided as an unsigned int made by make_4delim() and match up to 4 different
744 * delimiters. Delimiters are stripped at the beginning and end of the pattern.
Willy Tarreaua84d3742007-05-07 00:36:48 +0200745 */
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200746static int match_word(struct acl_test *test, struct acl_pattern *pattern, unsigned int delimiters)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200747{
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200748 int may_match, icase;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200749 char *c, *end;
750 char *ps;
751 int pl;
752
753 pl = pattern->len;
754 ps = pattern->ptr.str;
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200755
756 while (pl > 0 && is_delimiter(*ps, delimiters)) {
Willy Tarreaua84d3742007-05-07 00:36:48 +0200757 pl--;
758 ps++;
759 }
760
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200761 while (pl > 0 && is_delimiter(ps[pl - 1], delimiters))
Willy Tarreaua84d3742007-05-07 00:36:48 +0200762 pl--;
763
Willy Tarreau664092c2011-12-16 19:11:42 +0100764 if (pl > temp_pattern.data.str.len)
Willy Tarreau11382812008-07-09 16:18:21 +0200765 return ACL_PAT_FAIL;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200766
767 may_match = 1;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200768 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
Willy Tarreau664092c2011-12-16 19:11:42 +0100769 end = temp_pattern.data.str.str + temp_pattern.data.str.len - pl;
770 for (c = temp_pattern.data.str.str; c <= end; c++) {
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200771 if (is_delimiter(*c, delimiters)) {
Willy Tarreaua84d3742007-05-07 00:36:48 +0200772 may_match = 1;
773 continue;
774 }
Willy Tarreaua84d3742007-05-07 00:36:48 +0200775
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200776 if (!may_match)
777 continue;
778
779 if (icase) {
780 if ((tolower(*c) == tolower(*ps)) &&
781 (strncasecmp(ps, c, pl) == 0) &&
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200782 (c == end || is_delimiter(c[pl], delimiters)))
Willy Tarreau11382812008-07-09 16:18:21 +0200783 return ACL_PAT_PASS;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200784 } else {
785 if ((*c == *ps) &&
786 (strncmp(ps, c, pl) == 0) &&
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200787 (c == end || is_delimiter(c[pl], delimiters)))
Willy Tarreau11382812008-07-09 16:18:21 +0200788 return ACL_PAT_PASS;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200789 }
Willy Tarreaua84d3742007-05-07 00:36:48 +0200790 may_match = 0;
791 }
Willy Tarreau11382812008-07-09 16:18:21 +0200792 return ACL_PAT_FAIL;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200793}
794
795/* Checks that the pattern is included inside the tested string, but enclosed
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200796 * between the delimiters '?' or '/' or at the beginning or end of the string.
797 * Delimiters at the beginning or end of the pattern are ignored.
Willy Tarreaua84d3742007-05-07 00:36:48 +0200798 */
799int acl_match_dir(struct acl_test *test, struct acl_pattern *pattern)
800{
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200801 return match_word(test, pattern, make_4delim('/', '?', '?', '?'));
Willy Tarreaua84d3742007-05-07 00:36:48 +0200802}
803
804/* Checks that the pattern is included inside the tested string, but enclosed
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200805 * between the delmiters '/', '?', '.' or ":" or at the beginning or end of
806 * the string. Delimiters at the beginning or end of the pattern are ignored.
Willy Tarreaua84d3742007-05-07 00:36:48 +0200807 */
808int acl_match_dom(struct acl_test *test, struct acl_pattern *pattern)
809{
Finn Arne Gangstade8c7ecc2011-09-09 16:09:50 +0200810 return match_word(test, pattern, make_4delim('/', '?', '.', ':'));
Willy Tarreaua84d3742007-05-07 00:36:48 +0200811}
812
813/* Checks that the integer in <test> is included between min and max */
Willy Tarreauae8b7962007-06-09 23:10:04 +0200814int acl_match_int(struct acl_test *test, struct acl_pattern *pattern)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200815{
Willy Tarreaua5e37562011-12-16 17:06:15 +0100816 if ((!pattern->val.range.min_set || pattern->val.range.min <= temp_pattern.data.integer) &&
817 (!pattern->val.range.max_set || temp_pattern.data.integer <= pattern->val.range.max))
Willy Tarreau11382812008-07-09 16:18:21 +0200818 return ACL_PAT_PASS;
819 return ACL_PAT_FAIL;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200820}
821
Willy Tarreau0e698542011-09-16 08:32:32 +0200822/* Checks that the length of the pattern in <test> is included between min and max */
823int acl_match_len(struct acl_test *test, struct acl_pattern *pattern)
824{
Willy Tarreau664092c2011-12-16 19:11:42 +0100825 if ((!pattern->val.range.min_set || pattern->val.range.min <= temp_pattern.data.str.len) &&
826 (!pattern->val.range.max_set || temp_pattern.data.str.len <= pattern->val.range.max))
Willy Tarreau0e698542011-09-16 08:32:32 +0200827 return ACL_PAT_PASS;
828 return ACL_PAT_FAIL;
829}
830
Willy Tarreaua67fad92007-05-08 19:50:09 +0200831int acl_match_ip(struct acl_test *test, struct acl_pattern *pattern)
832{
833 struct in_addr *s;
834
Willy Tarreauf4362b32011-12-16 17:49:52 +0100835 if (temp_pattern.type != PATTERN_TYPE_IP)
Willy Tarreau11382812008-07-09 16:18:21 +0200836 return ACL_PAT_FAIL;
Willy Tarreaua67fad92007-05-08 19:50:09 +0200837
Willy Tarreauf4362b32011-12-16 17:49:52 +0100838 s = &temp_pattern.data.ip;
Willy Tarreaua67fad92007-05-08 19:50:09 +0200839 if (((s->s_addr ^ pattern->val.ipv4.addr.s_addr) & pattern->val.ipv4.mask.s_addr) == 0)
Willy Tarreau11382812008-07-09 16:18:21 +0200840 return ACL_PAT_PASS;
841 return ACL_PAT_FAIL;
Willy Tarreaua67fad92007-05-08 19:50:09 +0200842}
843
Willy Tarreaub337b532010-05-13 20:03:41 +0200844/* Lookup an IPv4 address in the expression's pattern tree using the longest
845 * match method. The node is returned if it exists, otherwise NULL.
846 */
847void *acl_lookup_ip(struct acl_test *test, struct acl_expr *expr)
848{
849 struct in_addr *s;
850
Willy Tarreauf4362b32011-12-16 17:49:52 +0100851 if (temp_pattern.type != PATTERN_TYPE_IP)
Willy Tarreaub337b532010-05-13 20:03:41 +0200852 return ACL_PAT_FAIL;
853
Willy Tarreauf4362b32011-12-16 17:49:52 +0100854 s = &temp_pattern.data.ip;
Willy Tarreaub337b532010-05-13 20:03:41 +0200855 return ebmb_lookup_longest(&expr->pattern_tree, &s->s_addr);
856}
857
Willy Tarreaua84d3742007-05-07 00:36:48 +0200858/* Parse a string. It is allocated and duplicated. */
Willy Tarreauae8b7962007-06-09 23:10:04 +0200859int acl_parse_str(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200860{
861 int len;
862
Willy Tarreauae8b7962007-06-09 23:10:04 +0200863 len = strlen(*text);
Willy Tarreauc4262962010-05-10 23:42:40 +0200864
865 if (pattern->flags & ACL_PAT_F_TREE_OK) {
866 /* we're allowed to put the data in a tree whose root is pointed
867 * to by val.tree.
868 */
869 struct ebmb_node *node;
870
871 node = calloc(1, sizeof(*node) + len + 1);
872 if (!node)
873 return 0;
874 memcpy(node->key, *text, len + 1);
875 if (ebst_insert(pattern->val.tree, node) != node)
876 free(node); /* was a duplicate */
877 pattern->flags |= ACL_PAT_F_TREE; /* this pattern now contains a tree */
878 return 1;
879 }
880
Willy Tarreauae8b7962007-06-09 23:10:04 +0200881 pattern->ptr.str = strdup(*text);
Willy Tarreaua84d3742007-05-07 00:36:48 +0200882 if (!pattern->ptr.str)
883 return 0;
884 pattern->len = len;
885 return 1;
886}
887
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +0100888/* Parse and concatenate all further strings into one. */
889int
890acl_parse_strcat(const char **text, struct acl_pattern *pattern, int *opaque)
891{
892
893 int len = 0, i;
894 char *s;
895
896 for (i = 0; *text[i]; i++)
897 len += strlen(text[i])+1;
898
899 pattern->ptr.str = s = calloc(1, len);
900 if (!pattern->ptr.str)
901 return 0;
902
903 for (i = 0; *text[i]; i++)
904 s += sprintf(s, i?" %s":"%s", text[i]);
905
906 pattern->len = len;
907
908 return i;
909}
910
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +0200911/* Free data allocated by acl_parse_reg */
912static void acl_free_reg(void *ptr) {
913
914 regfree((regex_t *)ptr);
915}
916
Willy Tarreauf3d25982007-05-08 22:45:09 +0200917/* Parse a regex. It is allocated. */
Willy Tarreauae8b7962007-06-09 23:10:04 +0200918int acl_parse_reg(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreauf3d25982007-05-08 22:45:09 +0200919{
920 regex_t *preg;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200921 int icase;
Willy Tarreauf3d25982007-05-08 22:45:09 +0200922
923 preg = calloc(1, sizeof(regex_t));
924
925 if (!preg)
926 return 0;
927
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200928 icase = (pattern->flags & ACL_PAT_F_IGNORE_CASE) ? REG_ICASE : 0;
929 if (regcomp(preg, *text, REG_EXTENDED | REG_NOSUB | icase) != 0) {
Willy Tarreauf3d25982007-05-08 22:45:09 +0200930 free(preg);
931 return 0;
932 }
933
934 pattern->ptr.reg = preg;
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +0200935 pattern->freeptrbuf = &acl_free_reg;
Willy Tarreauf3d25982007-05-08 22:45:09 +0200936 return 1;
937}
938
Willy Tarreauae8b7962007-06-09 23:10:04 +0200939/* Parse a range of positive integers delimited by either ':' or '-'. If only
940 * one integer is read, it is set as both min and max. An operator may be
941 * specified as the prefix, among this list of 5 :
942 *
943 * 0:eq, 1:gt, 2:ge, 3:lt, 4:le
944 *
945 * The default operator is "eq". It supports range matching. Ranges are
946 * rejected for other operators. The operator may be changed at any time.
947 * The operator is stored in the 'opaque' argument.
948 *
Willy Tarreaua84d3742007-05-07 00:36:48 +0200949 */
Willy Tarreauae8b7962007-06-09 23:10:04 +0200950int acl_parse_int(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200951{
Willy Tarreauae8b7962007-06-09 23:10:04 +0200952 signed long long i;
953 unsigned int j, last, skip = 0;
954 const char *ptr = *text;
955
956
Willy Tarreau8f8e6452007-06-17 21:51:38 +0200957 while (!isdigit((unsigned char)*ptr)) {
Willy Tarreau1c7cc5b2010-07-18 10:46:33 +0200958 switch (get_std_op(ptr)) {
959 case STD_OP_EQ: *opaque = 0; break;
960 case STD_OP_GT: *opaque = 1; break;
961 case STD_OP_GE: *opaque = 2; break;
962 case STD_OP_LT: *opaque = 3; break;
963 case STD_OP_LE: *opaque = 4; break;
964 default:
Willy Tarreauae8b7962007-06-09 23:10:04 +0200965 return 0;
Willy Tarreau1c7cc5b2010-07-18 10:46:33 +0200966 }
Willy Tarreauae8b7962007-06-09 23:10:04 +0200967
968 skip++;
969 ptr = text[skip];
970 }
Willy Tarreaua84d3742007-05-07 00:36:48 +0200971
972 last = i = 0;
973 while (1) {
Willy Tarreauae8b7962007-06-09 23:10:04 +0200974 j = *ptr++;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200975 if ((j == '-' || j == ':') && !last) {
976 last++;
977 pattern->val.range.min = i;
978 i = 0;
979 continue;
980 }
981 j -= '0';
982 if (j > 9)
983 // also catches the terminating zero
984 break;
985 i *= 10;
986 i += j;
987 }
Willy Tarreauae8b7962007-06-09 23:10:04 +0200988
989 if (last && *opaque >= 1 && *opaque <= 4)
990 /* having a range with a min or a max is absurd */
991 return 0;
992
Willy Tarreaua84d3742007-05-07 00:36:48 +0200993 if (!last)
994 pattern->val.range.min = i;
995 pattern->val.range.max = i;
Willy Tarreauae8b7962007-06-09 23:10:04 +0200996
997 switch (*opaque) {
998 case 0: /* eq */
999 pattern->val.range.min_set = 1;
1000 pattern->val.range.max_set = 1;
1001 break;
1002 case 1: /* gt */
1003 pattern->val.range.min++; /* gt = ge + 1 */
1004 case 2: /* ge */
1005 pattern->val.range.min_set = 1;
1006 pattern->val.range.max_set = 0;
1007 break;
1008 case 3: /* lt */
1009 pattern->val.range.max--; /* lt = le - 1 */
1010 case 4: /* le */
1011 pattern->val.range.min_set = 0;
1012 pattern->val.range.max_set = 1;
1013 break;
1014 }
1015 return skip + 1;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001016}
1017
Willy Tarreau4a26d2f2008-07-15 16:05:33 +02001018/* Parse a range of positive 2-component versions delimited by either ':' or
1019 * '-'. The version consists in a major and a minor, both of which must be
1020 * smaller than 65536, because internally they will be represented as a 32-bit
1021 * integer.
1022 * If only one version is read, it is set as both min and max. Just like for
1023 * pure integers, an operator may be specified as the prefix, among this list
1024 * of 5 :
1025 *
1026 * 0:eq, 1:gt, 2:ge, 3:lt, 4:le
1027 *
1028 * The default operator is "eq". It supports range matching. Ranges are
1029 * rejected for other operators. The operator may be changed at any time.
1030 * The operator is stored in the 'opaque' argument. This allows constructs
1031 * such as the following one :
1032 *
1033 * acl obsolete_ssl ssl_req_proto lt 3
1034 * acl unsupported_ssl ssl_req_proto gt 3.1
1035 * acl valid_ssl ssl_req_proto 3.0-3.1
1036 *
1037 */
1038int acl_parse_dotted_ver(const char **text, struct acl_pattern *pattern, int *opaque)
1039{
1040 signed long long i;
1041 unsigned int j, last, skip = 0;
1042 const char *ptr = *text;
1043
1044
1045 while (!isdigit((unsigned char)*ptr)) {
Willy Tarreau1c7cc5b2010-07-18 10:46:33 +02001046 switch (get_std_op(ptr)) {
1047 case STD_OP_EQ: *opaque = 0; break;
1048 case STD_OP_GT: *opaque = 1; break;
1049 case STD_OP_GE: *opaque = 2; break;
1050 case STD_OP_LT: *opaque = 3; break;
1051 case STD_OP_LE: *opaque = 4; break;
1052 default:
Willy Tarreau4a26d2f2008-07-15 16:05:33 +02001053 return 0;
Willy Tarreau1c7cc5b2010-07-18 10:46:33 +02001054 }
Willy Tarreau4a26d2f2008-07-15 16:05:33 +02001055
1056 skip++;
1057 ptr = text[skip];
1058 }
1059
1060 last = i = 0;
1061 while (1) {
1062 j = *ptr++;
1063 if (j == '.') {
1064 /* minor part */
1065 if (i >= 65536)
1066 return 0;
1067 i <<= 16;
1068 continue;
1069 }
1070 if ((j == '-' || j == ':') && !last) {
1071 last++;
1072 if (i < 65536)
1073 i <<= 16;
1074 pattern->val.range.min = i;
1075 i = 0;
1076 continue;
1077 }
1078 j -= '0';
1079 if (j > 9)
1080 // also catches the terminating zero
1081 break;
1082 i = (i & 0xFFFF0000) + (i & 0xFFFF) * 10;
1083 i += j;
1084 }
1085
1086 /* if we only got a major version, let's shift it now */
1087 if (i < 65536)
1088 i <<= 16;
1089
1090 if (last && *opaque >= 1 && *opaque <= 4)
1091 /* having a range with a min or a max is absurd */
1092 return 0;
1093
1094 if (!last)
1095 pattern->val.range.min = i;
1096 pattern->val.range.max = i;
1097
1098 switch (*opaque) {
1099 case 0: /* eq */
1100 pattern->val.range.min_set = 1;
1101 pattern->val.range.max_set = 1;
1102 break;
1103 case 1: /* gt */
1104 pattern->val.range.min++; /* gt = ge + 1 */
1105 case 2: /* ge */
1106 pattern->val.range.min_set = 1;
1107 pattern->val.range.max_set = 0;
1108 break;
1109 case 3: /* lt */
1110 pattern->val.range.max--; /* lt = le - 1 */
1111 case 4: /* le */
1112 pattern->val.range.min_set = 0;
1113 pattern->val.range.max_set = 1;
1114 break;
1115 }
1116 return skip + 1;
1117}
1118
Willy Tarreaua67fad92007-05-08 19:50:09 +02001119/* Parse an IP address and an optional mask in the form addr[/mask].
1120 * The addr may either be an IPv4 address or a hostname. The mask
1121 * may either be a dotted mask or a number of bits. Returns 1 if OK,
1122 * otherwise 0.
1123 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02001124int acl_parse_ip(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreaua67fad92007-05-08 19:50:09 +02001125{
Willy Tarreaub337b532010-05-13 20:03:41 +02001126 struct eb_root *tree = NULL;
1127 if (pattern->flags & ACL_PAT_F_TREE_OK)
1128 tree = pattern->val.tree;
1129
1130 if (str2net(*text, &pattern->val.ipv4.addr, &pattern->val.ipv4.mask)) {
1131 unsigned int mask = ntohl(pattern->val.ipv4.mask.s_addr);
1132 struct ebmb_node *node;
1133 /* check if the mask is contiguous so that we can insert the
1134 * network into the tree. A continuous mask has only ones on
1135 * the left. This means that this mask + its lower bit added
1136 * once again is null.
1137 */
1138 if (mask + (mask & -mask) == 0 && tree) {
1139 mask = mask ? 33 - flsnz(mask & -mask) : 0; /* equals cidr value */
1140 /* FIXME: insert <addr>/<mask> into the tree here */
1141 node = calloc(1, sizeof(*node) + 4); /* reserve 4 bytes for IPv4 address */
1142 if (!node)
1143 return 0;
1144 memcpy(node->key, &pattern->val.ipv4.addr, 4); /* network byte order */
1145 node->node.pfx = mask;
1146 if (ebmb_insert_prefix(tree, node, 4) != node)
1147 free(node); /* was a duplicate */
1148 pattern->flags |= ACL_PAT_F_TREE;
1149 return 1;
1150 }
Willy Tarreauae8b7962007-06-09 23:10:04 +02001151 return 1;
Willy Tarreaub337b532010-05-13 20:03:41 +02001152 }
Willy Tarreauae8b7962007-06-09 23:10:04 +02001153 else
1154 return 0;
Willy Tarreaua67fad92007-05-08 19:50:09 +02001155}
1156
Willy Tarreaua84d3742007-05-07 00:36:48 +02001157/*
1158 * Registers the ACL keyword list <kwl> as a list of valid keywords for next
1159 * parsing sessions.
1160 */
1161void acl_register_keywords(struct acl_kw_list *kwl)
1162{
1163 LIST_ADDQ(&acl_keywords.list, &kwl->list);
1164}
1165
1166/*
1167 * Unregisters the ACL keyword list <kwl> from the list of valid keywords.
1168 */
1169void acl_unregister_keywords(struct acl_kw_list *kwl)
1170{
1171 LIST_DEL(&kwl->list);
1172 LIST_INIT(&kwl->list);
1173}
1174
1175/* Return a pointer to the ACL <name> within the list starting at <head>, or
1176 * NULL if not found.
1177 */
1178struct acl *find_acl_by_name(const char *name, struct list *head)
1179{
1180 struct acl *acl;
1181 list_for_each_entry(acl, head, list) {
1182 if (strcmp(acl->name, name) == 0)
1183 return acl;
1184 }
1185 return NULL;
1186}
1187
1188/* Return a pointer to the ACL keyword <kw>, or NULL if not found. Note that if
1189 * <kw> contains an opening parenthesis, only the left part of it is checked.
1190 */
1191struct acl_keyword *find_acl_kw(const char *kw)
1192{
1193 int index;
1194 const char *kwend;
1195 struct acl_kw_list *kwl;
1196
1197 kwend = strchr(kw, '(');
1198 if (!kwend)
1199 kwend = kw + strlen(kw);
1200
1201 list_for_each_entry(kwl, &acl_keywords.list, list) {
1202 for (index = 0; kwl->kw[index].kw != NULL; index++) {
1203 if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) &&
1204 kwl->kw[index].kw[kwend-kw] == 0)
1205 return &kwl->kw[index];
1206 }
1207 }
1208 return NULL;
1209}
1210
Willy Tarreaudfd7fca2011-03-09 07:27:02 +01001211/* NB: does nothing if <pat> is NULL */
Willy Tarreaua84d3742007-05-07 00:36:48 +02001212static void free_pattern(struct acl_pattern *pat)
1213{
Willy Tarreaudfd7fca2011-03-09 07:27:02 +01001214 if (!pat)
1215 return;
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02001216
1217 if (pat->ptr.ptr) {
1218 if (pat->freeptrbuf)
1219 pat->freeptrbuf(pat->ptr.ptr);
1220
Willy Tarreaua84d3742007-05-07 00:36:48 +02001221 free(pat->ptr.ptr);
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +02001222 }
1223
Willy Tarreaua84d3742007-05-07 00:36:48 +02001224 free(pat);
1225}
1226
1227static void free_pattern_list(struct list *head)
1228{
1229 struct acl_pattern *pat, *tmp;
1230 list_for_each_entry_safe(pat, tmp, head, list)
1231 free_pattern(pat);
1232}
1233
Willy Tarreaue56cda92010-05-11 23:25:05 +02001234static void free_pattern_tree(struct eb_root *root)
1235{
1236 struct eb_node *node, *next;
1237 node = eb_first(root);
1238 while (node) {
1239 next = eb_next(node);
1240 free(node);
1241 node = next;
1242 }
1243}
1244
Willy Tarreaua84d3742007-05-07 00:36:48 +02001245static struct acl_expr *prune_acl_expr(struct acl_expr *expr)
1246{
1247 free_pattern_list(&expr->patterns);
Willy Tarreaue56cda92010-05-11 23:25:05 +02001248 free_pattern_tree(&expr->pattern_tree);
Willy Tarreaua84d3742007-05-07 00:36:48 +02001249 LIST_INIT(&expr->patterns);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001250 if (expr->arg_len && expr->arg.str)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001251 free(expr->arg.str);
1252 expr->kw->use_cnt--;
1253 return expr;
1254}
1255
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001256static int acl_read_patterns_from_file( struct acl_keyword *aclkw,
1257 struct acl_expr *expr,
1258 const char *filename, int patflags)
1259{
1260 FILE *file;
1261 char *c;
1262 const char *args[2];
1263 struct acl_pattern *pattern;
1264 int opaque;
Willy Tarreau6a8097f2011-02-26 15:14:15 +01001265 int ret = 0;
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001266
1267 file = fopen(filename, "r");
1268 if (!file)
1269 return 0;
1270
1271 /* now parse all patterns. The file may contain only one pattern per
1272 * line. If the line contains spaces, they will be part of the pattern.
1273 * The pattern stops at the first CR, LF or EOF encountered.
1274 */
1275 opaque = 0;
Willy Tarreaue56cda92010-05-11 23:25:05 +02001276 pattern = NULL;
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001277 args[1] = "";
1278 while (fgets(trash, sizeof(trash), file) != NULL) {
1279
1280 c = trash;
Willy Tarreau58215a02010-05-13 22:07:43 +02001281
1282 /* ignore lines beginning with a dash */
1283 if (*c == '#')
1284 continue;
1285
1286 /* strip leading spaces and tabs */
1287 while (*c == ' ' || *c == '\t')
1288 c++;
1289
Willy Tarreau58215a02010-05-13 22:07:43 +02001290
1291 args[0] = c;
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001292 while (*c && *c != '\n' && *c != '\r')
1293 c++;
1294 *c = 0;
1295
Willy Tarreau51091962011-01-03 21:04:10 +01001296 /* empty lines are ignored too */
1297 if (c == args[0])
1298 continue;
1299
Willy Tarreaue56cda92010-05-11 23:25:05 +02001300 /* we keep the previous pattern along iterations as long as it's not used */
1301 if (!pattern)
1302 pattern = (struct acl_pattern *)malloc(sizeof(*pattern));
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001303 if (!pattern)
1304 goto out_close;
Willy Tarreaue56cda92010-05-11 23:25:05 +02001305
1306 memset(pattern, 0, sizeof(*pattern));
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001307 pattern->flags = patflags;
1308
Willy Tarreaue56cda92010-05-11 23:25:05 +02001309 if ((aclkw->requires & ACL_MAY_LOOKUP) && !(pattern->flags & ACL_PAT_F_IGNORE_CASE)) {
1310 /* we pre-set the data pointer to the tree's head so that functions
1311 * which are able to insert in a tree know where to do that.
1312 */
1313 pattern->flags |= ACL_PAT_F_TREE_OK;
1314 pattern->val.tree = &expr->pattern_tree;
1315 }
1316
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001317 if (!aclkw->parse(args, pattern, &opaque))
1318 goto out_free_pattern;
Willy Tarreaue56cda92010-05-11 23:25:05 +02001319
1320 /* if the parser did not feed the tree, let's chain the pattern to the list */
1321 if (!(pattern->flags & ACL_PAT_F_TREE)) {
1322 LIST_ADDQ(&expr->patterns, &pattern->list);
1323 pattern = NULL; /* get a new one */
1324 }
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001325 }
Willy Tarreau6a8097f2011-02-26 15:14:15 +01001326
1327 ret = 1; /* success */
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001328
1329 out_free_pattern:
1330 free_pattern(pattern);
1331 out_close:
1332 fclose(file);
Willy Tarreau6a8097f2011-02-26 15:14:15 +01001333 return ret;
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001334}
1335
Willy Tarreaua84d3742007-05-07 00:36:48 +02001336/* Parse an ACL expression starting at <args>[0], and return it.
1337 * Right now, the only accepted syntax is :
1338 * <subject> [<value>...]
1339 */
1340struct acl_expr *parse_acl_expr(const char **args)
1341{
1342 __label__ out_return, out_free_expr, out_free_pattern;
1343 struct acl_expr *expr;
1344 struct acl_keyword *aclkw;
1345 struct acl_pattern *pattern;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02001346 int opaque, patflags;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001347 const char *arg;
1348
1349 aclkw = find_acl_kw(args[0]);
1350 if (!aclkw || !aclkw->parse)
1351 goto out_return;
1352
1353 expr = (struct acl_expr *)calloc(1, sizeof(*expr));
1354 if (!expr)
1355 goto out_return;
1356
1357 expr->kw = aclkw;
1358 aclkw->use_cnt++;
1359 LIST_INIT(&expr->patterns);
Willy Tarreaue56cda92010-05-11 23:25:05 +02001360 expr->pattern_tree = EB_ROOT_UNIQUE;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001361 expr->arg.str = NULL;
Willy Tarreaubb768912007-06-10 11:17:01 +02001362 expr->arg_len = 0;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001363
1364 arg = strchr(args[0], '(');
1365 if (arg != NULL) {
1366 char *end, *arg2;
1367 /* there is an argument in the form "subject(arg)" */
1368 arg++;
1369 end = strchr(arg, ')');
1370 if (!end)
1371 goto out_free_expr;
Willy Tarreauac778f52010-01-26 19:02:46 +01001372 arg2 = my_strndup(arg, end - arg);
Willy Tarreau1edb1442010-01-27 20:13:38 +01001373 if (!arg2)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001374 goto out_free_expr;
Willy Tarreaubb768912007-06-10 11:17:01 +02001375 expr->arg_len = end - arg;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001376 expr->arg.str = arg2;
1377 }
1378
Willy Tarreaua84d3742007-05-07 00:36:48 +02001379 args++;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02001380
1381 /* check for options before patterns. Supported options are :
1382 * -i : ignore case for all patterns by default
1383 * -f : read patterns from those files
1384 * -- : everything after this is not an option
1385 */
1386 patflags = 0;
1387 while (**args == '-') {
1388 if ((*args)[1] == 'i')
1389 patflags |= ACL_PAT_F_IGNORE_CASE;
Willy Tarreau2b5285d2010-05-09 23:45:24 +02001390 else if ((*args)[1] == 'f') {
1391 if (!acl_read_patterns_from_file(aclkw, expr, args[1], patflags | ACL_PAT_F_FROM_FILE))
1392 goto out_free_expr;
1393 args++;
1394 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +02001395 else if ((*args)[1] == '-') {
1396 args++;
1397 break;
1398 }
1399 else
1400 break;
1401 args++;
1402 }
1403
1404 /* now parse all patterns */
Willy Tarreauae8b7962007-06-09 23:10:04 +02001405 opaque = 0;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001406 while (**args) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02001407 int ret;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001408 pattern = (struct acl_pattern *)calloc(1, sizeof(*pattern));
1409 if (!pattern)
1410 goto out_free_expr;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02001411 pattern->flags = patflags;
1412
Willy Tarreauae8b7962007-06-09 23:10:04 +02001413 ret = aclkw->parse(args, pattern, &opaque);
1414 if (!ret)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001415 goto out_free_pattern;
1416 LIST_ADDQ(&expr->patterns, &pattern->list);
Willy Tarreauae8b7962007-06-09 23:10:04 +02001417 args += ret;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001418 }
1419
1420 return expr;
1421
1422 out_free_pattern:
1423 free_pattern(pattern);
1424 out_free_expr:
1425 prune_acl_expr(expr);
1426 free(expr);
1427 out_return:
1428 return NULL;
1429}
1430
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +02001431/* Purge everything in the acl <acl>, then return <acl>. */
1432struct acl *prune_acl(struct acl *acl) {
1433
1434 struct acl_expr *expr, *exprb;
1435
1436 free(acl->name);
1437
1438 list_for_each_entry_safe(expr, exprb, &acl->expr, list) {
1439 LIST_DEL(&expr->list);
1440 prune_acl_expr(expr);
1441 free(expr);
1442 }
1443
1444 return acl;
1445}
1446
Willy Tarreaua84d3742007-05-07 00:36:48 +02001447/* Parse an ACL with the name starting at <args>[0], and with a list of already
1448 * known ACLs in <acl>. If the ACL was not in the list, it will be added.
Willy Tarreau2a56c5e2010-03-15 16:13:29 +01001449 * A pointer to that ACL is returned. If the ACL has an empty name, then it's
1450 * an anonymous one and it won't be merged with any other one.
Willy Tarreaua84d3742007-05-07 00:36:48 +02001451 *
1452 * args syntax: <aclname> <acl_expr>
1453 */
1454struct acl *parse_acl(const char **args, struct list *known_acl)
1455{
1456 __label__ out_return, out_free_acl_expr, out_free_name;
1457 struct acl *cur_acl;
1458 struct acl_expr *acl_expr;
1459 char *name;
1460
Willy Tarreau2a56c5e2010-03-15 16:13:29 +01001461 if (**args && invalid_char(*args))
Willy Tarreau2e74c3f2007-12-02 18:45:09 +01001462 goto out_return;
1463
Willy Tarreaua84d3742007-05-07 00:36:48 +02001464 acl_expr = parse_acl_expr(args + 1);
1465 if (!acl_expr)
1466 goto out_return;
1467
Willy Tarreau404e8ab2009-07-26 19:40:40 +02001468 /* Check for args beginning with an opening parenthesis just after the
1469 * subject, as this is almost certainly a typo. Right now we can only
1470 * emit a warning, so let's do so.
1471 */
Krzysztof Piotr Oledzki4cdd8312009-10-05 00:23:35 +02001472 if (!strchr(args[1], '(') && *args[2] == '(')
Willy Tarreau404e8ab2009-07-26 19:40:40 +02001473 Warning("parsing acl '%s' :\n"
1474 " matching '%s' for pattern '%s' is likely a mistake and probably\n"
1475 " not what you want. Maybe you need to remove the extraneous space before '('.\n"
1476 " If you are really sure this is not an error, please insert '--' between the\n"
1477 " match and the pattern to make this warning message disappear.\n",
1478 args[0], args[1], args[2]);
1479
Willy Tarreau2a56c5e2010-03-15 16:13:29 +01001480 if (*args[0])
1481 cur_acl = find_acl_by_name(args[0], known_acl);
1482 else
1483 cur_acl = NULL;
1484
Willy Tarreaua84d3742007-05-07 00:36:48 +02001485 if (!cur_acl) {
1486 name = strdup(args[0]);
1487 if (!name)
1488 goto out_free_acl_expr;
1489 cur_acl = (struct acl *)calloc(1, sizeof(*cur_acl));
1490 if (cur_acl == NULL)
1491 goto out_free_name;
1492
1493 LIST_INIT(&cur_acl->expr);
1494 LIST_ADDQ(known_acl, &cur_acl->list);
1495 cur_acl->name = name;
1496 }
1497
Willy Tarreaua9802632008-07-25 19:13:19 +02001498 cur_acl->requires |= acl_expr->kw->requires;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001499 LIST_ADDQ(&cur_acl->expr, &acl_expr->list);
1500 return cur_acl;
1501
1502 out_free_name:
1503 free(name);
1504 out_free_acl_expr:
1505 prune_acl_expr(acl_expr);
1506 free(acl_expr);
1507 out_return:
1508 return NULL;
1509}
1510
Willy Tarreau16fbe822007-06-17 11:54:31 +02001511/* Some useful ACLs provided by default. Only those used are allocated. */
1512
1513const struct {
1514 const char *name;
1515 const char *expr[4]; /* put enough for longest expression */
1516} default_acl_list[] = {
Willy Tarreau58393e12008-07-20 10:39:22 +02001517 { .name = "TRUE", .expr = {"always_true",""}},
1518 { .name = "FALSE", .expr = {"always_false",""}},
Willy Tarreau16fbe822007-06-17 11:54:31 +02001519 { .name = "LOCALHOST", .expr = {"src","127.0.0.1/8",""}},
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001520 { .name = "HTTP", .expr = {"req_proto_http",""}},
Willy Tarreau16fbe822007-06-17 11:54:31 +02001521 { .name = "HTTP_1.0", .expr = {"req_ver","1.0",""}},
1522 { .name = "HTTP_1.1", .expr = {"req_ver","1.1",""}},
1523 { .name = "METH_CONNECT", .expr = {"method","CONNECT",""}},
1524 { .name = "METH_GET", .expr = {"method","GET","HEAD",""}},
1525 { .name = "METH_HEAD", .expr = {"method","HEAD",""}},
1526 { .name = "METH_OPTIONS", .expr = {"method","OPTIONS",""}},
1527 { .name = "METH_POST", .expr = {"method","POST",""}},
1528 { .name = "METH_TRACE", .expr = {"method","TRACE",""}},
1529 { .name = "HTTP_URL_ABS", .expr = {"url_reg","^[^/:]*://",""}},
1530 { .name = "HTTP_URL_SLASH", .expr = {"url_beg","/",""}},
1531 { .name = "HTTP_URL_STAR", .expr = {"url","*",""}},
1532 { .name = "HTTP_CONTENT", .expr = {"hdr_val(content-length)","gt","0",""}},
Emeric Brunbede3d02009-06-30 17:54:00 +02001533 { .name = "RDP_COOKIE", .expr = {"req_rdp_cookie_cnt","gt","0",""}},
Willy Tarreauc6317702008-07-20 09:29:50 +02001534 { .name = "REQ_CONTENT", .expr = {"req_len","gt","0",""}},
Willy Tarreaub6fb4202008-07-20 11:18:28 +02001535 { .name = "WAIT_END", .expr = {"wait_end",""}},
Willy Tarreau16fbe822007-06-17 11:54:31 +02001536 { .name = NULL, .expr = {""}}
1537};
1538
1539/* Find a default ACL from the default_acl list, compile it and return it.
1540 * If the ACL is not found, NULL is returned. In theory, it cannot fail,
1541 * except when default ACLs are broken, in which case it will return NULL.
1542 * If <known_acl> is not NULL, the ACL will be queued at its tail.
1543 */
1544struct acl *find_acl_default(const char *acl_name, struct list *known_acl)
1545{
1546 __label__ out_return, out_free_acl_expr, out_free_name;
1547 struct acl *cur_acl;
1548 struct acl_expr *acl_expr;
1549 char *name;
1550 int index;
1551
1552 for (index = 0; default_acl_list[index].name != NULL; index++) {
1553 if (strcmp(acl_name, default_acl_list[index].name) == 0)
1554 break;
1555 }
1556
1557 if (default_acl_list[index].name == NULL)
1558 return NULL;
1559
1560 acl_expr = parse_acl_expr((const char **)default_acl_list[index].expr);
1561 if (!acl_expr)
1562 goto out_return;
1563
1564 name = strdup(acl_name);
1565 if (!name)
1566 goto out_free_acl_expr;
1567 cur_acl = (struct acl *)calloc(1, sizeof(*cur_acl));
1568 if (cur_acl == NULL)
1569 goto out_free_name;
1570
1571 cur_acl->name = name;
Willy Tarreaua55b7dc2009-07-12 09:21:30 +02001572 cur_acl->requires |= acl_expr->kw->requires;
Willy Tarreau16fbe822007-06-17 11:54:31 +02001573 LIST_INIT(&cur_acl->expr);
1574 LIST_ADDQ(&cur_acl->expr, &acl_expr->list);
1575 if (known_acl)
1576 LIST_ADDQ(known_acl, &cur_acl->list);
1577
1578 return cur_acl;
1579
1580 out_free_name:
1581 free(name);
1582 out_free_acl_expr:
1583 prune_acl_expr(acl_expr);
1584 free(acl_expr);
1585 out_return:
1586 return NULL;
1587}
Willy Tarreaua84d3742007-05-07 00:36:48 +02001588
1589/* Purge everything in the acl_cond <cond>, then return <cond>. */
1590struct acl_cond *prune_acl_cond(struct acl_cond *cond)
1591{
1592 struct acl_term_suite *suite, *tmp_suite;
1593 struct acl_term *term, *tmp_term;
1594
1595 /* iterate through all term suites and free all terms and all suites */
1596 list_for_each_entry_safe(suite, tmp_suite, &cond->suites, list) {
1597 list_for_each_entry_safe(term, tmp_term, &suite->terms, list)
1598 free(term);
1599 free(suite);
1600 }
1601 return cond;
1602}
1603
1604/* Parse an ACL condition starting at <args>[0], relying on a list of already
1605 * known ACLs passed in <known_acl>. The new condition is returned (or NULL in
1606 * case of low memory). Supports multiple conditions separated by "or".
1607 */
1608struct acl_cond *parse_acl_cond(const char **args, struct list *known_acl, int pol)
1609{
1610 __label__ out_return, out_free_suite, out_free_term;
Willy Tarreau74b98a82007-06-16 19:35:18 +02001611 int arg, neg;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001612 const char *word;
1613 struct acl *cur_acl;
1614 struct acl_term *cur_term;
1615 struct acl_term_suite *cur_suite;
1616 struct acl_cond *cond;
1617
1618 cond = (struct acl_cond *)calloc(1, sizeof(*cond));
1619 if (cond == NULL)
1620 goto out_return;
1621
1622 LIST_INIT(&cond->list);
1623 LIST_INIT(&cond->suites);
1624 cond->pol = pol;
1625
1626 cur_suite = NULL;
Willy Tarreau74b98a82007-06-16 19:35:18 +02001627 neg = 0;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001628 for (arg = 0; *args[arg]; arg++) {
1629 word = args[arg];
1630
1631 /* remove as many exclamation marks as we can */
1632 while (*word == '!') {
1633 neg = !neg;
1634 word++;
1635 }
1636
1637 /* an empty word is allowed because we cannot force the user to
1638 * always think about not leaving exclamation marks alone.
1639 */
1640 if (!*word)
1641 continue;
1642
Willy Tarreau16fbe822007-06-17 11:54:31 +02001643 if (strcasecmp(word, "or") == 0 || strcmp(word, "||") == 0) {
Willy Tarreaua84d3742007-05-07 00:36:48 +02001644 /* new term suite */
1645 cur_suite = NULL;
1646 neg = 0;
1647 continue;
1648 }
1649
Willy Tarreau95fa4692010-02-01 13:05:50 +01001650 if (strcmp(word, "{") == 0) {
1651 /* we may have a complete ACL expression between two braces,
1652 * find the last one.
1653 */
1654 int arg_end = arg + 1;
1655 const char **args_new;
1656
1657 while (*args[arg_end] && strcmp(args[arg_end], "}") != 0)
1658 arg_end++;
1659
1660 if (!*args[arg_end])
1661 goto out_free_suite;
1662
1663 args_new = calloc(1, (arg_end - arg + 1) * sizeof(*args_new));
1664 if (!args_new)
1665 goto out_free_suite;
1666
Willy Tarreau2a56c5e2010-03-15 16:13:29 +01001667 args_new[0] = "";
Willy Tarreau95fa4692010-02-01 13:05:50 +01001668 memcpy(args_new + 1, args + arg + 1, (arg_end - arg) * sizeof(*args_new));
1669 args_new[arg_end - arg] = "";
1670 cur_acl = parse_acl(args_new, known_acl);
1671 free(args_new);
1672
1673 if (!cur_acl)
Willy Tarreau16fbe822007-06-17 11:54:31 +02001674 goto out_free_suite;
Willy Tarreau95fa4692010-02-01 13:05:50 +01001675 arg = arg_end;
1676 }
1677 else {
1678 /* search for <word> in the known ACL names. If we do not find
1679 * it, let's look for it in the default ACLs, and if found, add
1680 * it to the list of ACLs of this proxy. This makes it possible
1681 * to override them.
1682 */
1683 cur_acl = find_acl_by_name(word, known_acl);
1684 if (cur_acl == NULL) {
1685 cur_acl = find_acl_default(word, known_acl);
1686 if (cur_acl == NULL)
1687 goto out_free_suite;
1688 }
Willy Tarreau16fbe822007-06-17 11:54:31 +02001689 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001690
1691 cur_term = (struct acl_term *)calloc(1, sizeof(*cur_term));
1692 if (cur_term == NULL)
1693 goto out_free_suite;
1694
1695 cur_term->acl = cur_acl;
1696 cur_term->neg = neg;
Willy Tarreaua9802632008-07-25 19:13:19 +02001697 cond->requires |= cur_acl->requires;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001698
1699 if (!cur_suite) {
1700 cur_suite = (struct acl_term_suite *)calloc(1, sizeof(*cur_suite));
1701 if (cur_term == NULL)
1702 goto out_free_term;
1703 LIST_INIT(&cur_suite->terms);
1704 LIST_ADDQ(&cond->suites, &cur_suite->list);
1705 }
1706 LIST_ADDQ(&cur_suite->terms, &cur_term->list);
Willy Tarreau74b98a82007-06-16 19:35:18 +02001707 neg = 0;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001708 }
1709
1710 return cond;
1711
1712 out_free_term:
1713 free(cur_term);
1714 out_free_suite:
1715 prune_acl_cond(cond);
1716 free(cond);
1717 out_return:
1718 return NULL;
1719}
1720
Willy Tarreau2bbba412010-01-28 16:48:33 +01001721/* Builds an ACL condition starting at the if/unless keyword. The complete
1722 * condition is returned. NULL is returned in case of error or if the first
1723 * word is neither "if" nor "unless". It automatically sets the file name and
1724 * the line number in the condition for better error reporting, and adds the
1725 * ACL requirements to the proxy's acl_requires.
1726 */
1727struct acl_cond *build_acl_cond(const char *file, int line, struct proxy *px, const char **args)
1728{
1729 int pol = ACL_COND_NONE;
1730 struct acl_cond *cond = NULL;
1731
1732 if (!strcmp(*args, "if")) {
1733 pol = ACL_COND_IF;
1734 args++;
1735 }
1736 else if (!strcmp(*args, "unless")) {
1737 pol = ACL_COND_UNLESS;
1738 args++;
1739 }
1740 else
1741 return NULL;
1742
1743 cond = parse_acl_cond(args, &px->acl, pol);
1744 if (!cond)
1745 return NULL;
1746
1747 cond->file = file;
1748 cond->line = line;
1749 px->acl_requires |= cond->requires;
1750
1751 return cond;
1752}
1753
Willy Tarreau11382812008-07-09 16:18:21 +02001754/* Execute condition <cond> and return either ACL_PAT_FAIL, ACL_PAT_MISS or
Willy Tarreaub6866442008-07-14 23:54:42 +02001755 * ACL_PAT_PASS depending on the test results. ACL_PAT_MISS may only be
1756 * returned if <dir> contains ACL_PARTIAL, indicating that incomplete data
1757 * is being examined.
1758 * This function only computes the condition, it does not apply the polarity
1759 * required by IF/UNLESS, it's up to the caller to do this using something like
1760 * this :
Willy Tarreau11382812008-07-09 16:18:21 +02001761 *
1762 * res = acl_pass(res);
Willy Tarreaub6866442008-07-14 23:54:42 +02001763 * if (res == ACL_PAT_MISS)
1764 * return 0;
Willy Tarreau11382812008-07-09 16:18:21 +02001765 * if (cond->pol == ACL_COND_UNLESS)
1766 * res = !res;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001767 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02001768int acl_exec_cond(struct acl_cond *cond, struct proxy *px, struct session *l4, void *l7, int dir)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001769{
1770 __label__ fetch_next;
1771 struct acl_term_suite *suite;
1772 struct acl_term *term;
1773 struct acl_expr *expr;
1774 struct acl *acl;
1775 struct acl_pattern *pattern;
1776 struct acl_test test;
Willy Tarreau11382812008-07-09 16:18:21 +02001777 int acl_res, suite_res, cond_res;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001778
Willy Tarreau11382812008-07-09 16:18:21 +02001779 /* We're doing a logical OR between conditions so we initialize to FAIL.
1780 * The MISS status is propagated down from the suites.
1781 */
Willy Tarreaua84d3742007-05-07 00:36:48 +02001782 cond_res = ACL_PAT_FAIL;
1783 list_for_each_entry(suite, &cond->suites, list) {
Willy Tarreau11382812008-07-09 16:18:21 +02001784 /* Evaluate condition suite <suite>. We stop at the first term
1785 * which returns ACL_PAT_FAIL. The MISS status is still propagated
1786 * in case of uncertainty in the result.
Willy Tarreaua84d3742007-05-07 00:36:48 +02001787 */
1788
1789 /* we're doing a logical AND between terms, so we must set the
1790 * initial value to PASS.
1791 */
1792 suite_res = ACL_PAT_PASS;
1793 list_for_each_entry(term, &suite->terms, list) {
1794 acl = term->acl;
1795
1796 /* FIXME: use cache !
1797 * check acl->cache_idx for this.
1798 */
1799
1800 /* ACL result not cached. Let's scan all the expressions
1801 * and use the first one to match.
1802 */
1803 acl_res = ACL_PAT_FAIL;
1804 list_for_each_entry(expr, &acl->expr, list) {
Willy Tarreaud41f8d82007-06-10 10:06:18 +02001805 /* we need to reset context and flags */
1806 memset(&test, 0, sizeof(test));
Willy Tarreaua84d3742007-05-07 00:36:48 +02001807 fetch_next:
Willy Tarreaub6866442008-07-14 23:54:42 +02001808 if (!expr->kw->fetch(px, l4, l7, dir, expr, &test)) {
1809 /* maybe we could not fetch because of missing data */
1810 if (test.flags & ACL_TEST_F_MAY_CHANGE && dir & ACL_PARTIAL)
1811 acl_res |= ACL_PAT_MISS;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001812 continue;
Willy Tarreaub6866442008-07-14 23:54:42 +02001813 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001814
Willy Tarreaua79534f2008-07-20 10:13:37 +02001815 if (test.flags & ACL_TEST_F_RES_SET) {
1816 if (test.flags & ACL_TEST_F_RES_PASS)
1817 acl_res |= ACL_PAT_PASS;
1818 else
1819 acl_res |= ACL_PAT_FAIL;
1820 }
1821 else {
Willy Tarreau020534d2010-05-16 21:45:45 +02001822 if (!eb_is_empty(&expr->pattern_tree)) {
Willy Tarreauc4262962010-05-10 23:42:40 +02001823 /* a tree is present, let's check what type it is */
1824 if (expr->kw->match == acl_match_str)
1825 acl_res |= acl_lookup_str(&test, expr) ? ACL_PAT_PASS : ACL_PAT_FAIL;
Willy Tarreaub337b532010-05-13 20:03:41 +02001826 else if (expr->kw->match == acl_match_ip)
1827 acl_res |= acl_lookup_ip(&test, expr) ? ACL_PAT_PASS : ACL_PAT_FAIL;
Willy Tarreauc4262962010-05-10 23:42:40 +02001828 }
1829
Willy Tarreaua79534f2008-07-20 10:13:37 +02001830 /* call the match() function for all tests on this value */
1831 list_for_each_entry(pattern, &expr->patterns, list) {
Willy Tarreaua79534f2008-07-20 10:13:37 +02001832 if (acl_res == ACL_PAT_PASS)
1833 break;
Willy Tarreaue56cda92010-05-11 23:25:05 +02001834 acl_res |= expr->kw->match(&test, pattern);
Willy Tarreaua79534f2008-07-20 10:13:37 +02001835 }
Krzysztof Piotr Oledzkid7528e52010-01-29 17:55:53 +01001836
Willy Tarreaue56cda92010-05-11 23:25:05 +02001837 if ((test.flags & ACL_TEST_F_NULL_MATCH) &&
Willy Tarreau020534d2010-05-16 21:45:45 +02001838 LIST_ISEMPTY(&expr->patterns) && eb_is_empty(&expr->pattern_tree))
Krzysztof Piotr Oledzkid7528e52010-01-29 17:55:53 +01001839 acl_res |= expr->kw->match(&test, NULL);
Willy Tarreaua84d3742007-05-07 00:36:48 +02001840 }
1841 /*
Willy Tarreau11382812008-07-09 16:18:21 +02001842 * OK now acl_res holds the result of this expression
1843 * as one of ACL_PAT_FAIL, ACL_PAT_MISS or ACL_PAT_PASS.
Willy Tarreaua84d3742007-05-07 00:36:48 +02001844 *
Willy Tarreau11382812008-07-09 16:18:21 +02001845 * Then if (!MISS) we can cache the result, and put
Willy Tarreaua84d3742007-05-07 00:36:48 +02001846 * (test.flags & ACL_TEST_F_VOLATILE) in the cache flags.
1847 *
1848 * FIXME: implement cache.
1849 *
1850 */
1851
1852 /* now we may have some cleanup to do */
1853 if (test.flags & ACL_TEST_F_MUST_FREE) {
Willy Tarreau664092c2011-12-16 19:11:42 +01001854 free(temp_pattern.data.str.str);
1855 temp_pattern.data.str.len = 0;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001856 }
1857
Willy Tarreau11382812008-07-09 16:18:21 +02001858 /* we're ORing these terms, so a single PASS is enough */
1859 if (acl_res == ACL_PAT_PASS)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001860 break;
1861
Willy Tarreaua84d3742007-05-07 00:36:48 +02001862 if (test.flags & ACL_TEST_F_FETCH_MORE)
1863 goto fetch_next;
Willy Tarreaub6866442008-07-14 23:54:42 +02001864
1865 /* sometimes we know the fetched data is subject to change
1866 * later and give another chance for a new match (eg: request
1867 * size, time, ...)
1868 */
1869 if (test.flags & ACL_TEST_F_MAY_CHANGE && dir & ACL_PARTIAL)
1870 acl_res |= ACL_PAT_MISS;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001871 }
1872 /*
1873 * Here we have the result of an ACL (cached or not).
1874 * ACLs are combined, negated or not, to form conditions.
1875 */
1876
Willy Tarreaua84d3742007-05-07 00:36:48 +02001877 if (term->neg)
Willy Tarreau11382812008-07-09 16:18:21 +02001878 acl_res = acl_neg(acl_res);
Willy Tarreaua84d3742007-05-07 00:36:48 +02001879
1880 suite_res &= acl_res;
Willy Tarreau11382812008-07-09 16:18:21 +02001881
1882 /* we're ANDing these terms, so a single FAIL is enough */
1883 if (suite_res == ACL_PAT_FAIL)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001884 break;
1885 }
1886 cond_res |= suite_res;
Willy Tarreau11382812008-07-09 16:18:21 +02001887
1888 /* we're ORing these terms, so a single PASS is enough */
1889 if (cond_res == ACL_PAT_PASS)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001890 break;
1891 }
Willy Tarreau11382812008-07-09 16:18:21 +02001892 return cond_res;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001893}
1894
1895
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001896/* Reports a pointer to the first ACL used in condition <cond> which requires
1897 * at least one of the USE_FLAGS in <require>. Returns NULL if none matches.
1898 * The construct is almost the same as for acl_exec_cond() since we're walking
1899 * down the ACL tree as well. It is important that the tree is really walked
1900 * through and never cached, because that way, this function can be used as a
1901 * late check.
1902 */
Willy Tarreauf1e98b82010-01-28 17:59:39 +01001903struct acl *cond_find_require(const struct acl_cond *cond, unsigned int require)
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001904{
1905 struct acl_term_suite *suite;
1906 struct acl_term *term;
1907 struct acl *acl;
1908
1909 list_for_each_entry(suite, &cond->suites, list) {
1910 list_for_each_entry(term, &suite->terms, list) {
1911 acl = term->acl;
1912 if (acl->requires & require)
1913 return acl;
1914 }
1915 }
1916 return NULL;
1917}
1918
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001919/*
1920 * Find targets for userlist and groups in acl. Function returns the number
1921 * of errors or OK if everything is fine.
1922 */
1923int
1924acl_find_targets(struct proxy *p)
1925{
1926
1927 struct acl *acl;
1928 struct acl_expr *expr;
1929 struct acl_pattern *pattern;
1930 struct userlist *ul;
1931 int cfgerr = 0;
1932
1933 list_for_each_entry(acl, &p->acl, list) {
1934 list_for_each_entry(expr, &acl->expr, list) {
Hervé COMMOWICKdaa824e2011-08-05 12:09:44 +02001935 if (strcmp(expr->kw->kw, "srv_is_up") == 0 ||
1936 strcmp(expr->kw->kw, "srv_conn") == 0) {
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001937 struct proxy *px;
1938 struct server *srv;
1939 char *pname, *sname;
1940
1941 if (!expr->arg.str || !*expr->arg.str) {
1942 Alert("proxy %s: acl %s %s(): missing server name.\n",
1943 p->id, acl->name, expr->kw->kw);
1944 cfgerr++;
1945 continue;
1946 }
1947
1948 pname = expr->arg.str;
1949 sname = strrchr(pname, '/');
1950
1951 if (sname)
1952 *sname++ = '\0';
1953 else {
1954 sname = pname;
1955 pname = NULL;
1956 }
1957
1958 px = p;
1959 if (pname) {
1960 px = findproxy(pname, PR_CAP_BE);
1961 if (!px) {
1962 Alert("proxy %s: acl %s %s(): unable to find proxy '%s'.\n",
1963 p->id, acl->name, expr->kw->kw, pname);
1964 cfgerr++;
1965 continue;
1966 }
1967 }
1968
1969 srv = findserver(px, sname);
1970 if (!srv) {
1971 Alert("proxy %s: acl %s %s(): unable to find server '%s'.\n",
1972 p->id, acl->name, expr->kw->kw, sname);
1973 cfgerr++;
1974 continue;
1975 }
1976
1977 free(expr->arg.str);
1978 expr->arg_len = 0;
1979 expr->arg.srv = srv;
1980 continue;
1981 }
1982
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001983 if (strstr(expr->kw->kw, "http_auth") == expr->kw->kw) {
1984
1985 if (!expr->arg.str || !*expr->arg.str) {
1986 Alert("proxy %s: acl %s %s(): missing userlist name.\n",
1987 p->id, acl->name, expr->kw->kw);
1988 cfgerr++;
1989 continue;
1990 }
1991
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01001992 if (p->uri_auth && p->uri_auth->userlist &&
1993 !strcmp(p->uri_auth->userlist->name, expr->arg.str))
1994 ul = p->uri_auth->userlist;
1995 else
1996 ul = auth_find_userlist(expr->arg.str);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001997
1998 if (!ul) {
1999 Alert("proxy %s: acl %s %s(%s): unable to find userlist.\n",
2000 p->id, acl->name, expr->kw->kw, expr->arg.str);
2001 cfgerr++;
2002 continue;
2003 }
2004
2005 expr->arg_len = 0;
2006 expr->arg.ul = ul;
2007 }
2008
2009
2010 if (!strcmp(expr->kw->kw, "http_auth_group")) {
2011
2012 if (LIST_ISEMPTY(&expr->patterns)) {
2013 Alert("proxy %s: acl %s %s(): no groups specified.\n",
2014 p->id, acl->name, expr->kw->kw);
2015 cfgerr++;
2016 continue;
2017 }
2018
2019 list_for_each_entry(pattern, &expr->patterns, list) {
2020 pattern->val.group_mask = auth_resolve_groups(expr->arg.ul, pattern->ptr.str);
2021
2022 free(pattern->ptr.str);
2023 pattern->ptr.str = NULL;
2024 pattern->len = 0;
2025
2026 if (!pattern->val.group_mask) {
2027 Alert("proxy %s: acl %s %s(): invalid group(s).\n",
2028 p->id, acl->name, expr->kw->kw);
2029 cfgerr++;
2030 continue;
2031 }
2032 }
2033 }
2034 }
2035 }
2036
2037 return cfgerr;
2038}
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02002039
Willy Tarreaua84d3742007-05-07 00:36:48 +02002040/************************************************************************/
2041/* All supported keywords must be declared here. */
2042/************************************************************************/
2043
2044/* Note: must not be declared <const> as its list will be overwritten */
2045static struct acl_kw_list acl_kws = {{ },{
Emeric Brun38e71762010-09-23 17:59:18 +02002046 { "always_true", acl_parse_nothing, acl_fetch_true, acl_match_nothing, ACL_USE_NOTHING },
2047 { "always_false", acl_parse_nothing, acl_fetch_false, acl_match_nothing, ACL_USE_NOTHING },
2048 { "wait_end", acl_parse_nothing, acl_fetch_wait_end, acl_match_nothing, ACL_USE_NOTHING },
2049 { "req_len", acl_parse_int, acl_fetch_req_len, acl_match_int, ACL_USE_L6REQ_VOLATILE },
2050 { "req_ssl_hello_type", acl_parse_int, acl_fetch_ssl_hello_type, acl_match_int, ACL_USE_L6REQ_VOLATILE },
2051 { "rep_ssl_hello_type", acl_parse_int, acl_fetch_ssl_hello_type, acl_match_int, ACL_USE_L6RTR_VOLATILE },
2052 { "req_ssl_ver", acl_parse_dotted_ver, acl_fetch_req_ssl_ver, acl_match_int, ACL_USE_L6REQ_VOLATILE },
2053 { "req_rdp_cookie", acl_parse_str, acl_fetch_rdp_cookie, acl_match_str, ACL_USE_L6REQ_VOLATILE|ACL_MAY_LOOKUP },
2054 { "req_rdp_cookie_cnt", acl_parse_int, acl_fetch_rdp_cookie_cnt, acl_match_int, ACL_USE_L6REQ_VOLATILE },
Willy Tarreaub6672b52011-12-12 17:23:41 +01002055 { "req_ssl_sni", acl_parse_str, acl_fetch_ssl_hello_sni, acl_match_str, ACL_USE_L6REQ_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreaua84d3742007-05-07 00:36:48 +02002056#if 0
2057 { "time", acl_parse_time, acl_fetch_time, acl_match_time },
2058#endif
2059 { NULL, NULL, NULL, NULL }
2060}};
2061
2062
2063__attribute__((constructor))
2064static void __acl_init(void)
2065{
2066 acl_register_keywords(&acl_kws);
2067}
2068
2069
2070/*
2071 * Local variables:
2072 * c-indent-level: 8
2073 * c-basic-offset: 8
2074 * End:
2075 */