Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 1 | /* |
| 2 | * ACL management functions. |
| 3 | * |
Willy Tarreau | 0e69854 | 2011-09-16 08:32:32 +0200 | [diff] [blame] | 4 | * Copyright 2000-2011 Willy Tarreau <w@1wt.eu> |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 5 | * |
| 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 Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 13 | #include <ctype.h> |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 14 | #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 Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 20 | #include <common/uri_auth.h> |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 21 | |
Willy Tarreau | 2b5285d | 2010-05-09 23:45:24 +0200 | [diff] [blame] | 22 | #include <types/global.h> |
| 23 | |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 24 | #include <proto/acl.h> |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 25 | #include <proto/auth.h> |
Willy Tarreau | 44b90cc | 2010-05-24 20:27:29 +0200 | [diff] [blame] | 26 | #include <proto/buffers.h> |
Willy Tarreau | 404e8ab | 2009-07-26 19:40:40 +0200 | [diff] [blame] | 27 | #include <proto/log.h> |
Willy Tarreau | 0b1cd94 | 2010-05-16 22:18:27 +0200 | [diff] [blame] | 28 | #include <proto/proxy.h> |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 29 | |
Willy Tarreau | c426296 | 2010-05-10 23:42:40 +0200 | [diff] [blame] | 30 | #include <ebsttree.h> |
| 31 | |
Willy Tarreau | a980263 | 2008-07-25 19:13:19 +0200 | [diff] [blame] | 32 | /* The capabilities of filtering hooks describe the type of information |
| 33 | * available to each of them. |
| 34 | */ |
| 35 | const unsigned int filt_cap[] = { |
| 36 | [ACL_HOOK_REQ_FE_TCP] = ACL_USE_TCP4_ANY|ACL_USE_TCP6_ANY|ACL_USE_TCP_ANY, |
Willy Tarreau | 0645787 | 2010-05-23 12:24:38 +0200 | [diff] [blame] | 37 | [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 Tarreau | a980263 | 2008-07-25 19:13:19 +0200 | [diff] [blame] | 45 | |
Willy Tarreau | 0645787 | 2010-05-23 12:24:38 +0200 | [diff] [blame] | 46 | [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 Tarreau | a980263 | 2008-07-25 19:13:19 +0200 | [diff] [blame] | 52 | }; |
| 53 | |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 54 | /* List head of all known ACL keywords */ |
| 55 | static struct acl_kw_list acl_keywords = { |
| 56 | .list = LIST_HEAD_INIT(acl_keywords.list) |
| 57 | }; |
| 58 | |
| 59 | |
Willy Tarreau | a590983 | 2007-06-17 20:40:25 +0200 | [diff] [blame] | 60 | /* |
| 61 | * These functions are only used for debugging complex configurations. |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 62 | */ |
Willy Tarreau | a590983 | 2007-06-17 20:40:25 +0200 | [diff] [blame] | 63 | |
Willy Tarreau | 58393e1 | 2008-07-20 10:39:22 +0200 | [diff] [blame] | 64 | /* force TRUE to be returned at the fetch level */ |
Willy Tarreau | a590983 | 2007-06-17 20:40:25 +0200 | [diff] [blame] | 65 | static int |
Willy Tarreau | 58393e1 | 2008-07-20 10:39:22 +0200 | [diff] [blame] | 66 | acl_fetch_true(struct proxy *px, struct session *l4, void *l7, int dir, |
| 67 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | a590983 | 2007-06-17 20:40:25 +0200 | [diff] [blame] | 68 | { |
Willy Tarreau | 58393e1 | 2008-07-20 10:39:22 +0200 | [diff] [blame] | 69 | test->flags |= ACL_TEST_F_SET_RES_PASS; |
Willy Tarreau | a590983 | 2007-06-17 20:40:25 +0200 | [diff] [blame] | 70 | return 1; |
| 71 | } |
| 72 | |
Willy Tarreau | b6fb420 | 2008-07-20 11:18:28 +0200 | [diff] [blame] | 73 | /* wait for more data as long as possible, then return TRUE. This should be |
| 74 | * used with content inspection. |
| 75 | */ |
| 76 | static int |
| 77 | acl_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 Tarreau | 58393e1 | 2008-07-20 10:39:22 +0200 | [diff] [blame] | 88 | /* force FALSE to be returned at the fetch level */ |
Willy Tarreau | a590983 | 2007-06-17 20:40:25 +0200 | [diff] [blame] | 89 | static int |
Willy Tarreau | 58393e1 | 2008-07-20 10:39:22 +0200 | [diff] [blame] | 90 | acl_fetch_false(struct proxy *px, struct session *l4, void *l7, int dir, |
| 91 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 92 | { |
Willy Tarreau | 58393e1 | 2008-07-20 10:39:22 +0200 | [diff] [blame] | 93 | test->flags |= ACL_TEST_F_SET_RES_FAIL; |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 94 | return 1; |
| 95 | } |
| 96 | |
Willy Tarreau | 44b90cc | 2010-05-24 20:27:29 +0200 | [diff] [blame] | 97 | /* return the number of bytes in the request buffer */ |
| 98 | static int |
| 99 | acl_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 Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 105 | temp_pattern.data.integer = l4->req->i; |
Willy Tarreau | 44b90cc | 2010-05-24 20:27:29 +0200 | [diff] [blame] | 106 | test->flags = ACL_TEST_F_VOLATILE | ACL_TEST_F_MAY_CHANGE; |
| 107 | return 1; |
| 108 | } |
| 109 | |
Emeric Brun | 38e7176 | 2010-09-23 17:59:18 +0200 | [diff] [blame] | 110 | |
| 111 | static int |
| 112 | acl_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 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 125 | bleft = b->i; |
Willy Tarreau | 89fa706 | 2012-03-02 16:13:16 +0100 | [diff] [blame^] | 126 | data = (const unsigned char *)b->p; |
Emeric Brun | 38e7176 | 2010-09-23 17:59:18 +0200 | [diff] [blame] | 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 Tarreau | a5e3756 | 2011-12-16 17:06:15 +0100 | [diff] [blame] | 158 | temp_pattern.data.integer = hs_type; |
Emeric Brun | 38e7176 | 2010-09-23 17:59:18 +0200 | [diff] [blame] | 159 | 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 Tarreau | 44b90cc | 2010-05-24 20:27:29 +0200 | [diff] [blame] | 171 | /* 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 | */ |
| 179 | static int |
| 180 | acl_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; |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 190 | bleft = l4->req->i; |
Willy Tarreau | 44b90cc | 2010-05-24 20:27:29 +0200 | [diff] [blame] | 191 | if (!bleft) |
| 192 | goto too_short; |
| 193 | |
Willy Tarreau | 89fa706 | 2012-03-02 16:13:16 +0100 | [diff] [blame^] | 194 | data = (const unsigned char *)l4->req->p; |
Willy Tarreau | 44b90cc | 2010-05-24 20:27:29 +0200 | [diff] [blame] | 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 | */ |
Willy Tarreau | 89fa706 | 2012-03-02 16:13:16 +0100 | [diff] [blame^] | 262 | if (msg_len > buffer_max_len(l4->req) + l4->req->data - l4->req->p) |
| 263 | msg_len = buffer_max_len(l4->req) + l4->req->data - l4->req->p; |
Willy Tarreau | 44b90cc | 2010-05-24 20:27:29 +0200 | [diff] [blame] | 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 Tarreau | a5e3756 | 2011-12-16 17:06:15 +0100 | [diff] [blame] | 271 | temp_pattern.data.integer = version; |
Willy Tarreau | 44b90cc | 2010-05-24 20:27:29 +0200 | [diff] [blame] | 272 | 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 Tarreau | b6672b5 | 2011-12-12 17:23:41 +0100 | [diff] [blame] | 281 | /* 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) |
Willy Tarreau | d017f11 | 2012-04-09 09:24:11 +0200 | [diff] [blame] | 293 | * - uint8 Random[32] (4 first ones are timestamp) |
Willy Tarreau | b6672b5 | 2011-12-12 17:23:41 +0100 | [diff] [blame] | 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 | */ |
| 314 | static int |
| 315 | acl_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 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 327 | bleft = b->i; |
Willy Tarreau | 89fa706 | 2012-03-02 16:13:16 +0100 | [diff] [blame^] | 328 | data = (unsigned char *)b->p; |
Willy Tarreau | b6672b5 | 2011-12-12 17:23:41 +0100 | [diff] [blame] | 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 | |
Willy Tarreau | d017f11 | 2012-04-09 09:24:11 +0200 | [diff] [blame] | 373 | ext_len = data[34]; /* session_id_len */ |
Willy Tarreau | b6672b5 | 2011-12-12 17:23:41 +0100 | [diff] [blame] | 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 Tarreau | 62e7c71 | 2012-03-10 09:05:30 +0100 | [diff] [blame] | 426 | temp_pattern.data.str.str = (char *)data + 9; |
Willy Tarreau | 664092c | 2011-12-16 19:11:42 +0100 | [diff] [blame] | 427 | temp_pattern.data.str.len = name_len; |
Willy Tarreau | b6672b5 | 2011-12-12 17:23:41 +0100 | [diff] [blame] | 428 | test->flags = ACL_TEST_F_VOLATILE; |
Willy Tarreau | b6672b5 | 2011-12-12 17:23:41 +0100 | [diff] [blame] | 429 | return 1; |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | hs_len -= 4 + ext_len; |
| 434 | data += 4 + ext_len; |
| 435 | } |
| 436 | /* server name not found */ |
| 437 | goto not_ssl_hello; |
| 438 | |
| 439 | too_short: |
| 440 | test->flags = ACL_TEST_F_MAY_CHANGE; |
| 441 | |
| 442 | not_ssl_hello: |
| 443 | |
| 444 | return 0; |
| 445 | } |
| 446 | |
Willy Tarreau | 44b90cc | 2010-05-24 20:27:29 +0200 | [diff] [blame] | 447 | /* Fetch the RDP cookie identified in the expression. |
| 448 | * Note: this decoder only works with non-wrapping data. |
| 449 | */ |
| 450 | int |
| 451 | acl_fetch_rdp_cookie(struct proxy *px, struct session *l4, void *l7, int dir, |
| 452 | struct acl_expr *expr, struct acl_test *test) |
| 453 | { |
| 454 | int bleft; |
| 455 | const unsigned char *data; |
| 456 | |
| 457 | if (!l4 || !l4->req) |
| 458 | return 0; |
| 459 | |
| 460 | test->flags = 0; |
| 461 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 462 | bleft = l4->req->i; |
Willy Tarreau | 44b90cc | 2010-05-24 20:27:29 +0200 | [diff] [blame] | 463 | if (bleft <= 11) |
| 464 | goto too_short; |
| 465 | |
Willy Tarreau | 89fa706 | 2012-03-02 16:13:16 +0100 | [diff] [blame^] | 466 | data = (const unsigned char *)l4->req->p + 11; |
Willy Tarreau | 44b90cc | 2010-05-24 20:27:29 +0200 | [diff] [blame] | 467 | bleft -= 11; |
| 468 | |
| 469 | if (bleft <= 7) |
| 470 | goto too_short; |
| 471 | |
| 472 | if (strncasecmp((const char *)data, "Cookie:", 7) != 0) |
| 473 | goto not_cookie; |
| 474 | |
| 475 | data += 7; |
| 476 | bleft -= 7; |
| 477 | |
| 478 | while (bleft > 0 && *data == ' ') { |
| 479 | data++; |
| 480 | bleft--; |
| 481 | } |
| 482 | |
| 483 | if (expr->arg_len) { |
| 484 | |
| 485 | if (bleft <= expr->arg_len) |
| 486 | goto too_short; |
| 487 | |
| 488 | if ((data[expr->arg_len] != '=') || |
| 489 | strncasecmp(expr->arg.str, (const char *)data, expr->arg_len) != 0) |
| 490 | goto not_cookie; |
| 491 | |
| 492 | data += expr->arg_len + 1; |
| 493 | bleft -= expr->arg_len + 1; |
| 494 | } else { |
| 495 | while (bleft > 0 && *data != '=') { |
| 496 | if (*data == '\r' || *data == '\n') |
| 497 | goto not_cookie; |
| 498 | data++; |
| 499 | bleft--; |
| 500 | } |
| 501 | |
| 502 | if (bleft < 1) |
| 503 | goto too_short; |
| 504 | |
| 505 | if (*data != '=') |
| 506 | goto not_cookie; |
| 507 | |
| 508 | data++; |
| 509 | bleft--; |
| 510 | } |
| 511 | |
| 512 | /* data points to cookie value */ |
Willy Tarreau | 664092c | 2011-12-16 19:11:42 +0100 | [diff] [blame] | 513 | temp_pattern.data.str.str = (char *)data; |
| 514 | temp_pattern.data.str.len = 0; |
Willy Tarreau | 44b90cc | 2010-05-24 20:27:29 +0200 | [diff] [blame] | 515 | |
| 516 | while (bleft > 0 && *data != '\r') { |
| 517 | data++; |
| 518 | bleft--; |
| 519 | } |
| 520 | |
| 521 | if (bleft < 2) |
| 522 | goto too_short; |
| 523 | |
| 524 | if (data[0] != '\r' || data[1] != '\n') |
| 525 | goto not_cookie; |
| 526 | |
Willy Tarreau | 664092c | 2011-12-16 19:11:42 +0100 | [diff] [blame] | 527 | temp_pattern.data.str.len = (char *)data - temp_pattern.data.str.str; |
Willy Tarreau | 44b90cc | 2010-05-24 20:27:29 +0200 | [diff] [blame] | 528 | test->flags = ACL_TEST_F_VOLATILE; |
| 529 | return 1; |
| 530 | |
| 531 | too_short: |
| 532 | test->flags = ACL_TEST_F_MAY_CHANGE; |
| 533 | not_cookie: |
| 534 | return 0; |
| 535 | } |
| 536 | |
| 537 | static int |
| 538 | acl_fetch_rdp_cookie_cnt(struct proxy *px, struct session *l4, void *l7, int dir, |
| 539 | struct acl_expr *expr, struct acl_test *test) |
| 540 | { |
| 541 | int ret; |
| 542 | |
| 543 | ret = acl_fetch_rdp_cookie(px, l4, l7, dir, expr, test); |
| 544 | |
Willy Tarreau | 664092c | 2011-12-16 19:11:42 +0100 | [diff] [blame] | 545 | temp_pattern.data.str.str = NULL; |
| 546 | temp_pattern.data.str.len = 0; |
Willy Tarreau | 44b90cc | 2010-05-24 20:27:29 +0200 | [diff] [blame] | 547 | |
| 548 | if (test->flags & ACL_TEST_F_MAY_CHANGE) |
| 549 | return 0; |
| 550 | |
| 551 | test->flags = ACL_TEST_F_VOLATILE; |
Willy Tarreau | a5e3756 | 2011-12-16 17:06:15 +0100 | [diff] [blame] | 552 | temp_pattern.data.integer = ret; |
Willy Tarreau | 44b90cc | 2010-05-24 20:27:29 +0200 | [diff] [blame] | 553 | |
| 554 | return 1; |
| 555 | } |
| 556 | |
Willy Tarreau | 58393e1 | 2008-07-20 10:39:22 +0200 | [diff] [blame] | 557 | |
| 558 | /* |
| 559 | * These functions are exported and may be used by any other component. |
| 560 | */ |
| 561 | |
| 562 | /* ignore the current line */ |
| 563 | int acl_parse_nothing(const char **text, struct acl_pattern *pattern, int *opaque) |
Willy Tarreau | a590983 | 2007-06-17 20:40:25 +0200 | [diff] [blame] | 564 | { |
Willy Tarreau | 58393e1 | 2008-07-20 10:39:22 +0200 | [diff] [blame] | 565 | return 1; |
| 566 | } |
| 567 | |
| 568 | /* always fake a data retrieval */ |
| 569 | int acl_fetch_nothing(struct proxy *px, struct session *l4, void *l7, int dir, |
| 570 | struct acl_expr *expr, struct acl_test *test) |
| 571 | { |
| 572 | return 1; |
Willy Tarreau | a590983 | 2007-06-17 20:40:25 +0200 | [diff] [blame] | 573 | } |
| 574 | |
| 575 | /* always return false */ |
Willy Tarreau | 58393e1 | 2008-07-20 10:39:22 +0200 | [diff] [blame] | 576 | int acl_match_nothing(struct acl_test *test, struct acl_pattern *pattern) |
Willy Tarreau | a590983 | 2007-06-17 20:40:25 +0200 | [diff] [blame] | 577 | { |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 578 | return ACL_PAT_FAIL; |
Willy Tarreau | a590983 | 2007-06-17 20:40:25 +0200 | [diff] [blame] | 579 | } |
| 580 | |
| 581 | |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 582 | /* NB: For two strings to be identical, it is required that their lengths match */ |
| 583 | int acl_match_str(struct acl_test *test, struct acl_pattern *pattern) |
| 584 | { |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 585 | int icase; |
| 586 | |
Willy Tarreau | 664092c | 2011-12-16 19:11:42 +0100 | [diff] [blame] | 587 | if (pattern->len != temp_pattern.data.str.len) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 588 | return ACL_PAT_FAIL; |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 589 | |
| 590 | icase = pattern->flags & ACL_PAT_F_IGNORE_CASE; |
Willy Tarreau | 664092c | 2011-12-16 19:11:42 +0100 | [diff] [blame] | 591 | if ((icase && strncasecmp(pattern->ptr.str, temp_pattern.data.str.str, temp_pattern.data.str.len) == 0) || |
| 592 | (!icase && strncmp(pattern->ptr.str, temp_pattern.data.str.str, temp_pattern.data.str.len) == 0)) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 593 | return ACL_PAT_PASS; |
| 594 | return ACL_PAT_FAIL; |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 595 | } |
| 596 | |
Willy Tarreau | c426296 | 2010-05-10 23:42:40 +0200 | [diff] [blame] | 597 | /* Lookup a string in the expression's pattern tree. The node is returned if it |
| 598 | * exists, otherwise NULL. |
| 599 | */ |
| 600 | void *acl_lookup_str(struct acl_test *test, struct acl_expr *expr) |
| 601 | { |
| 602 | /* data are stored in a tree */ |
| 603 | struct ebmb_node *node; |
| 604 | char prev; |
| 605 | |
| 606 | /* we may have to force a trailing zero on the test pattern */ |
Willy Tarreau | 664092c | 2011-12-16 19:11:42 +0100 | [diff] [blame] | 607 | prev = temp_pattern.data.str.str[temp_pattern.data.str.len]; |
Willy Tarreau | c426296 | 2010-05-10 23:42:40 +0200 | [diff] [blame] | 608 | if (prev) |
Willy Tarreau | 664092c | 2011-12-16 19:11:42 +0100 | [diff] [blame] | 609 | temp_pattern.data.str.str[temp_pattern.data.str.len] = '\0'; |
| 610 | node = ebst_lookup(&expr->pattern_tree, temp_pattern.data.str.str); |
Willy Tarreau | c426296 | 2010-05-10 23:42:40 +0200 | [diff] [blame] | 611 | if (prev) |
Willy Tarreau | 664092c | 2011-12-16 19:11:42 +0100 | [diff] [blame] | 612 | temp_pattern.data.str.str[temp_pattern.data.str.len] = prev; |
Willy Tarreau | c426296 | 2010-05-10 23:42:40 +0200 | [diff] [blame] | 613 | return node; |
| 614 | } |
| 615 | |
Willy Tarreau | f3d2598 | 2007-05-08 22:45:09 +0200 | [diff] [blame] | 616 | /* Executes a regex. It needs to change the data. If it is marked READ_ONLY |
| 617 | * then it will be allocated and duplicated in place so that others may use |
| 618 | * it later on. Note that this is embarrassing because we always try to avoid |
| 619 | * allocating memory at run time. |
| 620 | */ |
| 621 | int acl_match_reg(struct acl_test *test, struct acl_pattern *pattern) |
| 622 | { |
| 623 | char old_char; |
| 624 | int ret; |
| 625 | |
| 626 | if (unlikely(test->flags & ACL_TEST_F_READ_ONLY)) { |
| 627 | char *new_str; |
| 628 | |
Willy Tarreau | 664092c | 2011-12-16 19:11:42 +0100 | [diff] [blame] | 629 | new_str = calloc(1, temp_pattern.data.str.len + 1); |
Willy Tarreau | f3d2598 | 2007-05-08 22:45:09 +0200 | [diff] [blame] | 630 | if (!new_str) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 631 | return ACL_PAT_FAIL; |
Willy Tarreau | f3d2598 | 2007-05-08 22:45:09 +0200 | [diff] [blame] | 632 | |
Willy Tarreau | 664092c | 2011-12-16 19:11:42 +0100 | [diff] [blame] | 633 | memcpy(new_str, temp_pattern.data.str.str, temp_pattern.data.str.len); |
| 634 | new_str[temp_pattern.data.str.len] = 0; |
Willy Tarreau | f3d2598 | 2007-05-08 22:45:09 +0200 | [diff] [blame] | 635 | if (test->flags & ACL_TEST_F_MUST_FREE) |
Willy Tarreau | 664092c | 2011-12-16 19:11:42 +0100 | [diff] [blame] | 636 | free(temp_pattern.data.str.str); |
| 637 | temp_pattern.data.str.str = new_str; |
Willy Tarreau | f3d2598 | 2007-05-08 22:45:09 +0200 | [diff] [blame] | 638 | test->flags |= ACL_TEST_F_MUST_FREE; |
| 639 | test->flags &= ~ACL_TEST_F_READ_ONLY; |
| 640 | } |
| 641 | |
Willy Tarreau | 664092c | 2011-12-16 19:11:42 +0100 | [diff] [blame] | 642 | old_char = temp_pattern.data.str.str[temp_pattern.data.str.len]; |
| 643 | temp_pattern.data.str.str[temp_pattern.data.str.len] = 0; |
Willy Tarreau | f3d2598 | 2007-05-08 22:45:09 +0200 | [diff] [blame] | 644 | |
Willy Tarreau | 664092c | 2011-12-16 19:11:42 +0100 | [diff] [blame] | 645 | if (regexec(pattern->ptr.reg, temp_pattern.data.str.str, 0, NULL, 0) == 0) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 646 | ret = ACL_PAT_PASS; |
Willy Tarreau | f3d2598 | 2007-05-08 22:45:09 +0200 | [diff] [blame] | 647 | else |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 648 | ret = ACL_PAT_FAIL; |
Willy Tarreau | f3d2598 | 2007-05-08 22:45:09 +0200 | [diff] [blame] | 649 | |
Willy Tarreau | 664092c | 2011-12-16 19:11:42 +0100 | [diff] [blame] | 650 | temp_pattern.data.str.str[temp_pattern.data.str.len] = old_char; |
Willy Tarreau | f3d2598 | 2007-05-08 22:45:09 +0200 | [diff] [blame] | 651 | return ret; |
| 652 | } |
| 653 | |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 654 | /* Checks that the pattern matches the beginning of the tested string. */ |
| 655 | int acl_match_beg(struct acl_test *test, struct acl_pattern *pattern) |
| 656 | { |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 657 | int icase; |
| 658 | |
Willy Tarreau | 664092c | 2011-12-16 19:11:42 +0100 | [diff] [blame] | 659 | if (pattern->len > temp_pattern.data.str.len) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 660 | return ACL_PAT_FAIL; |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 661 | |
| 662 | icase = pattern->flags & ACL_PAT_F_IGNORE_CASE; |
Willy Tarreau | 664092c | 2011-12-16 19:11:42 +0100 | [diff] [blame] | 663 | if ((icase && strncasecmp(pattern->ptr.str, temp_pattern.data.str.str, pattern->len) != 0) || |
| 664 | (!icase && strncmp(pattern->ptr.str, temp_pattern.data.str.str, pattern->len) != 0)) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 665 | return ACL_PAT_FAIL; |
| 666 | return ACL_PAT_PASS; |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 667 | } |
| 668 | |
| 669 | /* Checks that the pattern matches the end of the tested string. */ |
| 670 | int acl_match_end(struct acl_test *test, struct acl_pattern *pattern) |
| 671 | { |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 672 | int icase; |
| 673 | |
Willy Tarreau | 664092c | 2011-12-16 19:11:42 +0100 | [diff] [blame] | 674 | if (pattern->len > temp_pattern.data.str.len) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 675 | return ACL_PAT_FAIL; |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 676 | icase = pattern->flags & ACL_PAT_F_IGNORE_CASE; |
Willy Tarreau | 664092c | 2011-12-16 19:11:42 +0100 | [diff] [blame] | 677 | if ((icase && strncasecmp(pattern->ptr.str, temp_pattern.data.str.str + temp_pattern.data.str.len - pattern->len, pattern->len) != 0) || |
| 678 | (!icase && strncmp(pattern->ptr.str, temp_pattern.data.str.str + temp_pattern.data.str.len - pattern->len, pattern->len) != 0)) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 679 | return ACL_PAT_FAIL; |
| 680 | return ACL_PAT_PASS; |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 681 | } |
| 682 | |
| 683 | /* Checks that the pattern is included inside the tested string. |
| 684 | * NB: Suboptimal, should be rewritten using a Boyer-Moore method. |
| 685 | */ |
| 686 | int acl_match_sub(struct acl_test *test, struct acl_pattern *pattern) |
| 687 | { |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 688 | int icase; |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 689 | char *end; |
| 690 | char *c; |
| 691 | |
Willy Tarreau | 664092c | 2011-12-16 19:11:42 +0100 | [diff] [blame] | 692 | if (pattern->len > temp_pattern.data.str.len) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 693 | return ACL_PAT_FAIL; |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 694 | |
Willy Tarreau | 664092c | 2011-12-16 19:11:42 +0100 | [diff] [blame] | 695 | end = temp_pattern.data.str.str + temp_pattern.data.str.len - pattern->len; |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 696 | icase = pattern->flags & ACL_PAT_F_IGNORE_CASE; |
| 697 | if (icase) { |
Willy Tarreau | 664092c | 2011-12-16 19:11:42 +0100 | [diff] [blame] | 698 | for (c = temp_pattern.data.str.str; c <= end; c++) { |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 699 | if (tolower(*c) != tolower(*pattern->ptr.str)) |
| 700 | continue; |
| 701 | if (strncasecmp(pattern->ptr.str, c, pattern->len) == 0) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 702 | return ACL_PAT_PASS; |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 703 | } |
| 704 | } else { |
Willy Tarreau | 664092c | 2011-12-16 19:11:42 +0100 | [diff] [blame] | 705 | for (c = temp_pattern.data.str.str; c <= end; c++) { |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 706 | if (*c != *pattern->ptr.str) |
| 707 | continue; |
| 708 | if (strncmp(pattern->ptr.str, c, pattern->len) == 0) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 709 | return ACL_PAT_PASS; |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 710 | } |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 711 | } |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 712 | return ACL_PAT_FAIL; |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 713 | } |
| 714 | |
Finn Arne Gangstad | e8c7ecc | 2011-09-09 16:09:50 +0200 | [diff] [blame] | 715 | /* Background: Fast way to find a zero byte in a word |
| 716 | * http://graphics.stanford.edu/~seander/bithacks.html#ZeroInWord |
| 717 | * hasZeroByte = (v - 0x01010101UL) & ~v & 0x80808080UL; |
| 718 | * |
| 719 | * To look for 4 different byte values, xor the word with those bytes and |
| 720 | * then check for zero bytes: |
| 721 | * |
| 722 | * v = (((unsigned char)c * 0x1010101U) ^ delimiter) |
| 723 | * where <delimiter> is the 4 byte values to look for (as an uint) |
| 724 | * and <c> is the character that is being tested |
| 725 | */ |
| 726 | static inline unsigned int is_delimiter(unsigned char c, unsigned int mask) |
| 727 | { |
| 728 | mask ^= (c * 0x01010101); /* propagate the char to all 4 bytes */ |
| 729 | return (mask - 0x01010101) & ~mask & 0x80808080U; |
| 730 | } |
| 731 | |
| 732 | static inline unsigned int make_4delim(unsigned char d1, unsigned char d2, unsigned char d3, unsigned char d4) |
| 733 | { |
| 734 | return d1 << 24 | d2 << 16 | d3 << 8 | d4; |
| 735 | } |
| 736 | |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 737 | /* This one is used by other real functions. It checks that the pattern is |
| 738 | * included inside the tested string, but enclosed between the specified |
Finn Arne Gangstad | e8c7ecc | 2011-09-09 16:09:50 +0200 | [diff] [blame] | 739 | * delimiters or at the beginning or end of the string. The delimiters are |
| 740 | * provided as an unsigned int made by make_4delim() and match up to 4 different |
| 741 | * delimiters. Delimiters are stripped at the beginning and end of the pattern. |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 742 | */ |
Finn Arne Gangstad | e8c7ecc | 2011-09-09 16:09:50 +0200 | [diff] [blame] | 743 | static int match_word(struct acl_test *test, struct acl_pattern *pattern, unsigned int delimiters) |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 744 | { |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 745 | int may_match, icase; |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 746 | char *c, *end; |
| 747 | char *ps; |
| 748 | int pl; |
| 749 | |
| 750 | pl = pattern->len; |
| 751 | ps = pattern->ptr.str; |
Finn Arne Gangstad | e8c7ecc | 2011-09-09 16:09:50 +0200 | [diff] [blame] | 752 | |
| 753 | while (pl > 0 && is_delimiter(*ps, delimiters)) { |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 754 | pl--; |
| 755 | ps++; |
| 756 | } |
| 757 | |
Finn Arne Gangstad | e8c7ecc | 2011-09-09 16:09:50 +0200 | [diff] [blame] | 758 | while (pl > 0 && is_delimiter(ps[pl - 1], delimiters)) |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 759 | pl--; |
| 760 | |
Willy Tarreau | 664092c | 2011-12-16 19:11:42 +0100 | [diff] [blame] | 761 | if (pl > temp_pattern.data.str.len) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 762 | return ACL_PAT_FAIL; |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 763 | |
| 764 | may_match = 1; |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 765 | icase = pattern->flags & ACL_PAT_F_IGNORE_CASE; |
Willy Tarreau | 664092c | 2011-12-16 19:11:42 +0100 | [diff] [blame] | 766 | end = temp_pattern.data.str.str + temp_pattern.data.str.len - pl; |
| 767 | for (c = temp_pattern.data.str.str; c <= end; c++) { |
Finn Arne Gangstad | e8c7ecc | 2011-09-09 16:09:50 +0200 | [diff] [blame] | 768 | if (is_delimiter(*c, delimiters)) { |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 769 | may_match = 1; |
| 770 | continue; |
| 771 | } |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 772 | |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 773 | if (!may_match) |
| 774 | continue; |
| 775 | |
| 776 | if (icase) { |
| 777 | if ((tolower(*c) == tolower(*ps)) && |
| 778 | (strncasecmp(ps, c, pl) == 0) && |
Finn Arne Gangstad | e8c7ecc | 2011-09-09 16:09:50 +0200 | [diff] [blame] | 779 | (c == end || is_delimiter(c[pl], delimiters))) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 780 | return ACL_PAT_PASS; |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 781 | } else { |
| 782 | if ((*c == *ps) && |
| 783 | (strncmp(ps, c, pl) == 0) && |
Finn Arne Gangstad | e8c7ecc | 2011-09-09 16:09:50 +0200 | [diff] [blame] | 784 | (c == end || is_delimiter(c[pl], delimiters))) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 785 | return ACL_PAT_PASS; |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 786 | } |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 787 | may_match = 0; |
| 788 | } |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 789 | return ACL_PAT_FAIL; |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 790 | } |
| 791 | |
| 792 | /* Checks that the pattern is included inside the tested string, but enclosed |
Finn Arne Gangstad | e8c7ecc | 2011-09-09 16:09:50 +0200 | [diff] [blame] | 793 | * between the delimiters '?' or '/' or at the beginning or end of the string. |
| 794 | * Delimiters at the beginning or end of the pattern are ignored. |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 795 | */ |
| 796 | int acl_match_dir(struct acl_test *test, struct acl_pattern *pattern) |
| 797 | { |
Finn Arne Gangstad | e8c7ecc | 2011-09-09 16:09:50 +0200 | [diff] [blame] | 798 | return match_word(test, pattern, make_4delim('/', '?', '?', '?')); |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 799 | } |
| 800 | |
| 801 | /* Checks that the pattern is included inside the tested string, but enclosed |
Finn Arne Gangstad | e8c7ecc | 2011-09-09 16:09:50 +0200 | [diff] [blame] | 802 | * between the delmiters '/', '?', '.' or ":" or at the beginning or end of |
| 803 | * the string. Delimiters at the beginning or end of the pattern are ignored. |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 804 | */ |
| 805 | int acl_match_dom(struct acl_test *test, struct acl_pattern *pattern) |
| 806 | { |
Finn Arne Gangstad | e8c7ecc | 2011-09-09 16:09:50 +0200 | [diff] [blame] | 807 | return match_word(test, pattern, make_4delim('/', '?', '.', ':')); |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 808 | } |
| 809 | |
| 810 | /* Checks that the integer in <test> is included between min and max */ |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 811 | int acl_match_int(struct acl_test *test, struct acl_pattern *pattern) |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 812 | { |
Willy Tarreau | a5e3756 | 2011-12-16 17:06:15 +0100 | [diff] [blame] | 813 | if ((!pattern->val.range.min_set || pattern->val.range.min <= temp_pattern.data.integer) && |
| 814 | (!pattern->val.range.max_set || temp_pattern.data.integer <= pattern->val.range.max)) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 815 | return ACL_PAT_PASS; |
| 816 | return ACL_PAT_FAIL; |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 817 | } |
| 818 | |
Willy Tarreau | 0e69854 | 2011-09-16 08:32:32 +0200 | [diff] [blame] | 819 | /* Checks that the length of the pattern in <test> is included between min and max */ |
| 820 | int acl_match_len(struct acl_test *test, struct acl_pattern *pattern) |
| 821 | { |
Willy Tarreau | 664092c | 2011-12-16 19:11:42 +0100 | [diff] [blame] | 822 | if ((!pattern->val.range.min_set || pattern->val.range.min <= temp_pattern.data.str.len) && |
| 823 | (!pattern->val.range.max_set || temp_pattern.data.str.len <= pattern->val.range.max)) |
Willy Tarreau | 0e69854 | 2011-09-16 08:32:32 +0200 | [diff] [blame] | 824 | return ACL_PAT_PASS; |
| 825 | return ACL_PAT_FAIL; |
| 826 | } |
| 827 | |
Willy Tarreau | a67fad9 | 2007-05-08 19:50:09 +0200 | [diff] [blame] | 828 | int acl_match_ip(struct acl_test *test, struct acl_pattern *pattern) |
| 829 | { |
| 830 | struct in_addr *s; |
| 831 | |
Willy Tarreau | f4362b3 | 2011-12-16 17:49:52 +0100 | [diff] [blame] | 832 | if (temp_pattern.type != PATTERN_TYPE_IP) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 833 | return ACL_PAT_FAIL; |
Willy Tarreau | a67fad9 | 2007-05-08 19:50:09 +0200 | [diff] [blame] | 834 | |
Willy Tarreau | f4362b3 | 2011-12-16 17:49:52 +0100 | [diff] [blame] | 835 | s = &temp_pattern.data.ip; |
Willy Tarreau | a67fad9 | 2007-05-08 19:50:09 +0200 | [diff] [blame] | 836 | if (((s->s_addr ^ pattern->val.ipv4.addr.s_addr) & pattern->val.ipv4.mask.s_addr) == 0) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 837 | return ACL_PAT_PASS; |
| 838 | return ACL_PAT_FAIL; |
Willy Tarreau | a67fad9 | 2007-05-08 19:50:09 +0200 | [diff] [blame] | 839 | } |
| 840 | |
Willy Tarreau | b337b53 | 2010-05-13 20:03:41 +0200 | [diff] [blame] | 841 | /* Lookup an IPv4 address in the expression's pattern tree using the longest |
| 842 | * match method. The node is returned if it exists, otherwise NULL. |
| 843 | */ |
| 844 | void *acl_lookup_ip(struct acl_test *test, struct acl_expr *expr) |
| 845 | { |
| 846 | struct in_addr *s; |
| 847 | |
Willy Tarreau | f4362b3 | 2011-12-16 17:49:52 +0100 | [diff] [blame] | 848 | if (temp_pattern.type != PATTERN_TYPE_IP) |
Willy Tarreau | b337b53 | 2010-05-13 20:03:41 +0200 | [diff] [blame] | 849 | return ACL_PAT_FAIL; |
| 850 | |
Willy Tarreau | f4362b3 | 2011-12-16 17:49:52 +0100 | [diff] [blame] | 851 | s = &temp_pattern.data.ip; |
Willy Tarreau | b337b53 | 2010-05-13 20:03:41 +0200 | [diff] [blame] | 852 | return ebmb_lookup_longest(&expr->pattern_tree, &s->s_addr); |
| 853 | } |
| 854 | |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 855 | /* Parse a string. It is allocated and duplicated. */ |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 856 | int acl_parse_str(const char **text, struct acl_pattern *pattern, int *opaque) |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 857 | { |
| 858 | int len; |
| 859 | |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 860 | len = strlen(*text); |
Willy Tarreau | c426296 | 2010-05-10 23:42:40 +0200 | [diff] [blame] | 861 | |
| 862 | if (pattern->flags & ACL_PAT_F_TREE_OK) { |
| 863 | /* we're allowed to put the data in a tree whose root is pointed |
| 864 | * to by val.tree. |
| 865 | */ |
| 866 | struct ebmb_node *node; |
| 867 | |
| 868 | node = calloc(1, sizeof(*node) + len + 1); |
| 869 | if (!node) |
| 870 | return 0; |
| 871 | memcpy(node->key, *text, len + 1); |
| 872 | if (ebst_insert(pattern->val.tree, node) != node) |
| 873 | free(node); /* was a duplicate */ |
| 874 | pattern->flags |= ACL_PAT_F_TREE; /* this pattern now contains a tree */ |
| 875 | return 1; |
| 876 | } |
| 877 | |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 878 | pattern->ptr.str = strdup(*text); |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 879 | if (!pattern->ptr.str) |
| 880 | return 0; |
| 881 | pattern->len = len; |
| 882 | return 1; |
| 883 | } |
| 884 | |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 885 | /* Parse and concatenate all further strings into one. */ |
| 886 | int |
| 887 | acl_parse_strcat(const char **text, struct acl_pattern *pattern, int *opaque) |
| 888 | { |
| 889 | |
| 890 | int len = 0, i; |
| 891 | char *s; |
| 892 | |
| 893 | for (i = 0; *text[i]; i++) |
| 894 | len += strlen(text[i])+1; |
| 895 | |
| 896 | pattern->ptr.str = s = calloc(1, len); |
| 897 | if (!pattern->ptr.str) |
| 898 | return 0; |
| 899 | |
| 900 | for (i = 0; *text[i]; i++) |
| 901 | s += sprintf(s, i?" %s":"%s", text[i]); |
| 902 | |
| 903 | pattern->len = len; |
| 904 | |
| 905 | return i; |
| 906 | } |
| 907 | |
Krzysztof Piotr Oledzki | 8001d61 | 2008-05-31 13:53:23 +0200 | [diff] [blame] | 908 | /* Free data allocated by acl_parse_reg */ |
| 909 | static void acl_free_reg(void *ptr) { |
| 910 | |
| 911 | regfree((regex_t *)ptr); |
| 912 | } |
| 913 | |
Willy Tarreau | f3d2598 | 2007-05-08 22:45:09 +0200 | [diff] [blame] | 914 | /* Parse a regex. It is allocated. */ |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 915 | int acl_parse_reg(const char **text, struct acl_pattern *pattern, int *opaque) |
Willy Tarreau | f3d2598 | 2007-05-08 22:45:09 +0200 | [diff] [blame] | 916 | { |
| 917 | regex_t *preg; |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 918 | int icase; |
Willy Tarreau | f3d2598 | 2007-05-08 22:45:09 +0200 | [diff] [blame] | 919 | |
| 920 | preg = calloc(1, sizeof(regex_t)); |
| 921 | |
| 922 | if (!preg) |
| 923 | return 0; |
| 924 | |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 925 | icase = (pattern->flags & ACL_PAT_F_IGNORE_CASE) ? REG_ICASE : 0; |
| 926 | if (regcomp(preg, *text, REG_EXTENDED | REG_NOSUB | icase) != 0) { |
Willy Tarreau | f3d2598 | 2007-05-08 22:45:09 +0200 | [diff] [blame] | 927 | free(preg); |
| 928 | return 0; |
| 929 | } |
| 930 | |
| 931 | pattern->ptr.reg = preg; |
Krzysztof Piotr Oledzki | 8001d61 | 2008-05-31 13:53:23 +0200 | [diff] [blame] | 932 | pattern->freeptrbuf = &acl_free_reg; |
Willy Tarreau | f3d2598 | 2007-05-08 22:45:09 +0200 | [diff] [blame] | 933 | return 1; |
| 934 | } |
| 935 | |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 936 | /* Parse a range of positive integers delimited by either ':' or '-'. If only |
| 937 | * one integer is read, it is set as both min and max. An operator may be |
| 938 | * specified as the prefix, among this list of 5 : |
| 939 | * |
| 940 | * 0:eq, 1:gt, 2:ge, 3:lt, 4:le |
| 941 | * |
| 942 | * The default operator is "eq". It supports range matching. Ranges are |
| 943 | * rejected for other operators. The operator may be changed at any time. |
| 944 | * The operator is stored in the 'opaque' argument. |
| 945 | * |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 946 | */ |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 947 | int acl_parse_int(const char **text, struct acl_pattern *pattern, int *opaque) |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 948 | { |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 949 | signed long long i; |
| 950 | unsigned int j, last, skip = 0; |
| 951 | const char *ptr = *text; |
| 952 | |
| 953 | |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 954 | while (!isdigit((unsigned char)*ptr)) { |
Willy Tarreau | 1c7cc5b | 2010-07-18 10:46:33 +0200 | [diff] [blame] | 955 | switch (get_std_op(ptr)) { |
| 956 | case STD_OP_EQ: *opaque = 0; break; |
| 957 | case STD_OP_GT: *opaque = 1; break; |
| 958 | case STD_OP_GE: *opaque = 2; break; |
| 959 | case STD_OP_LT: *opaque = 3; break; |
| 960 | case STD_OP_LE: *opaque = 4; break; |
| 961 | default: |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 962 | return 0; |
Willy Tarreau | 1c7cc5b | 2010-07-18 10:46:33 +0200 | [diff] [blame] | 963 | } |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 964 | |
| 965 | skip++; |
| 966 | ptr = text[skip]; |
| 967 | } |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 968 | |
| 969 | last = i = 0; |
| 970 | while (1) { |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 971 | j = *ptr++; |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 972 | if ((j == '-' || j == ':') && !last) { |
| 973 | last++; |
| 974 | pattern->val.range.min = i; |
| 975 | i = 0; |
| 976 | continue; |
| 977 | } |
| 978 | j -= '0'; |
| 979 | if (j > 9) |
| 980 | // also catches the terminating zero |
| 981 | break; |
| 982 | i *= 10; |
| 983 | i += j; |
| 984 | } |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 985 | |
| 986 | if (last && *opaque >= 1 && *opaque <= 4) |
| 987 | /* having a range with a min or a max is absurd */ |
| 988 | return 0; |
| 989 | |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 990 | if (!last) |
| 991 | pattern->val.range.min = i; |
| 992 | pattern->val.range.max = i; |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 993 | |
| 994 | switch (*opaque) { |
| 995 | case 0: /* eq */ |
| 996 | pattern->val.range.min_set = 1; |
| 997 | pattern->val.range.max_set = 1; |
| 998 | break; |
| 999 | case 1: /* gt */ |
| 1000 | pattern->val.range.min++; /* gt = ge + 1 */ |
| 1001 | case 2: /* ge */ |
| 1002 | pattern->val.range.min_set = 1; |
| 1003 | pattern->val.range.max_set = 0; |
| 1004 | break; |
| 1005 | case 3: /* lt */ |
| 1006 | pattern->val.range.max--; /* lt = le - 1 */ |
| 1007 | case 4: /* le */ |
| 1008 | pattern->val.range.min_set = 0; |
| 1009 | pattern->val.range.max_set = 1; |
| 1010 | break; |
| 1011 | } |
| 1012 | return skip + 1; |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 1013 | } |
| 1014 | |
Willy Tarreau | 4a26d2f | 2008-07-15 16:05:33 +0200 | [diff] [blame] | 1015 | /* Parse a range of positive 2-component versions delimited by either ':' or |
| 1016 | * '-'. The version consists in a major and a minor, both of which must be |
| 1017 | * smaller than 65536, because internally they will be represented as a 32-bit |
| 1018 | * integer. |
| 1019 | * If only one version is read, it is set as both min and max. Just like for |
| 1020 | * pure integers, an operator may be specified as the prefix, among this list |
| 1021 | * of 5 : |
| 1022 | * |
| 1023 | * 0:eq, 1:gt, 2:ge, 3:lt, 4:le |
| 1024 | * |
| 1025 | * The default operator is "eq". It supports range matching. Ranges are |
| 1026 | * rejected for other operators. The operator may be changed at any time. |
| 1027 | * The operator is stored in the 'opaque' argument. This allows constructs |
| 1028 | * such as the following one : |
| 1029 | * |
| 1030 | * acl obsolete_ssl ssl_req_proto lt 3 |
| 1031 | * acl unsupported_ssl ssl_req_proto gt 3.1 |
| 1032 | * acl valid_ssl ssl_req_proto 3.0-3.1 |
| 1033 | * |
| 1034 | */ |
| 1035 | int acl_parse_dotted_ver(const char **text, struct acl_pattern *pattern, int *opaque) |
| 1036 | { |
| 1037 | signed long long i; |
| 1038 | unsigned int j, last, skip = 0; |
| 1039 | const char *ptr = *text; |
| 1040 | |
| 1041 | |
| 1042 | while (!isdigit((unsigned char)*ptr)) { |
Willy Tarreau | 1c7cc5b | 2010-07-18 10:46:33 +0200 | [diff] [blame] | 1043 | switch (get_std_op(ptr)) { |
| 1044 | case STD_OP_EQ: *opaque = 0; break; |
| 1045 | case STD_OP_GT: *opaque = 1; break; |
| 1046 | case STD_OP_GE: *opaque = 2; break; |
| 1047 | case STD_OP_LT: *opaque = 3; break; |
| 1048 | case STD_OP_LE: *opaque = 4; break; |
| 1049 | default: |
Willy Tarreau | 4a26d2f | 2008-07-15 16:05:33 +0200 | [diff] [blame] | 1050 | return 0; |
Willy Tarreau | 1c7cc5b | 2010-07-18 10:46:33 +0200 | [diff] [blame] | 1051 | } |
Willy Tarreau | 4a26d2f | 2008-07-15 16:05:33 +0200 | [diff] [blame] | 1052 | |
| 1053 | skip++; |
| 1054 | ptr = text[skip]; |
| 1055 | } |
| 1056 | |
| 1057 | last = i = 0; |
| 1058 | while (1) { |
| 1059 | j = *ptr++; |
| 1060 | if (j == '.') { |
| 1061 | /* minor part */ |
| 1062 | if (i >= 65536) |
| 1063 | return 0; |
| 1064 | i <<= 16; |
| 1065 | continue; |
| 1066 | } |
| 1067 | if ((j == '-' || j == ':') && !last) { |
| 1068 | last++; |
| 1069 | if (i < 65536) |
| 1070 | i <<= 16; |
| 1071 | pattern->val.range.min = i; |
| 1072 | i = 0; |
| 1073 | continue; |
| 1074 | } |
| 1075 | j -= '0'; |
| 1076 | if (j > 9) |
| 1077 | // also catches the terminating zero |
| 1078 | break; |
| 1079 | i = (i & 0xFFFF0000) + (i & 0xFFFF) * 10; |
| 1080 | i += j; |
| 1081 | } |
| 1082 | |
| 1083 | /* if we only got a major version, let's shift it now */ |
| 1084 | if (i < 65536) |
| 1085 | i <<= 16; |
| 1086 | |
| 1087 | if (last && *opaque >= 1 && *opaque <= 4) |
| 1088 | /* having a range with a min or a max is absurd */ |
| 1089 | return 0; |
| 1090 | |
| 1091 | if (!last) |
| 1092 | pattern->val.range.min = i; |
| 1093 | pattern->val.range.max = i; |
| 1094 | |
| 1095 | switch (*opaque) { |
| 1096 | case 0: /* eq */ |
| 1097 | pattern->val.range.min_set = 1; |
| 1098 | pattern->val.range.max_set = 1; |
| 1099 | break; |
| 1100 | case 1: /* gt */ |
| 1101 | pattern->val.range.min++; /* gt = ge + 1 */ |
| 1102 | case 2: /* ge */ |
| 1103 | pattern->val.range.min_set = 1; |
| 1104 | pattern->val.range.max_set = 0; |
| 1105 | break; |
| 1106 | case 3: /* lt */ |
| 1107 | pattern->val.range.max--; /* lt = le - 1 */ |
| 1108 | case 4: /* le */ |
| 1109 | pattern->val.range.min_set = 0; |
| 1110 | pattern->val.range.max_set = 1; |
| 1111 | break; |
| 1112 | } |
| 1113 | return skip + 1; |
| 1114 | } |
| 1115 | |
Willy Tarreau | a67fad9 | 2007-05-08 19:50:09 +0200 | [diff] [blame] | 1116 | /* Parse an IP address and an optional mask in the form addr[/mask]. |
| 1117 | * The addr may either be an IPv4 address or a hostname. The mask |
| 1118 | * may either be a dotted mask or a number of bits. Returns 1 if OK, |
| 1119 | * otherwise 0. |
| 1120 | */ |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 1121 | int acl_parse_ip(const char **text, struct acl_pattern *pattern, int *opaque) |
Willy Tarreau | a67fad9 | 2007-05-08 19:50:09 +0200 | [diff] [blame] | 1122 | { |
Willy Tarreau | b337b53 | 2010-05-13 20:03:41 +0200 | [diff] [blame] | 1123 | struct eb_root *tree = NULL; |
| 1124 | if (pattern->flags & ACL_PAT_F_TREE_OK) |
| 1125 | tree = pattern->val.tree; |
| 1126 | |
| 1127 | if (str2net(*text, &pattern->val.ipv4.addr, &pattern->val.ipv4.mask)) { |
| 1128 | unsigned int mask = ntohl(pattern->val.ipv4.mask.s_addr); |
| 1129 | struct ebmb_node *node; |
| 1130 | /* check if the mask is contiguous so that we can insert the |
| 1131 | * network into the tree. A continuous mask has only ones on |
| 1132 | * the left. This means that this mask + its lower bit added |
| 1133 | * once again is null. |
| 1134 | */ |
| 1135 | if (mask + (mask & -mask) == 0 && tree) { |
| 1136 | mask = mask ? 33 - flsnz(mask & -mask) : 0; /* equals cidr value */ |
| 1137 | /* FIXME: insert <addr>/<mask> into the tree here */ |
| 1138 | node = calloc(1, sizeof(*node) + 4); /* reserve 4 bytes for IPv4 address */ |
| 1139 | if (!node) |
| 1140 | return 0; |
| 1141 | memcpy(node->key, &pattern->val.ipv4.addr, 4); /* network byte order */ |
| 1142 | node->node.pfx = mask; |
| 1143 | if (ebmb_insert_prefix(tree, node, 4) != node) |
| 1144 | free(node); /* was a duplicate */ |
| 1145 | pattern->flags |= ACL_PAT_F_TREE; |
| 1146 | return 1; |
| 1147 | } |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 1148 | return 1; |
Willy Tarreau | b337b53 | 2010-05-13 20:03:41 +0200 | [diff] [blame] | 1149 | } |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 1150 | else |
| 1151 | return 0; |
Willy Tarreau | a67fad9 | 2007-05-08 19:50:09 +0200 | [diff] [blame] | 1152 | } |
| 1153 | |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 1154 | /* |
| 1155 | * Registers the ACL keyword list <kwl> as a list of valid keywords for next |
| 1156 | * parsing sessions. |
| 1157 | */ |
| 1158 | void acl_register_keywords(struct acl_kw_list *kwl) |
| 1159 | { |
| 1160 | LIST_ADDQ(&acl_keywords.list, &kwl->list); |
| 1161 | } |
| 1162 | |
| 1163 | /* |
| 1164 | * Unregisters the ACL keyword list <kwl> from the list of valid keywords. |
| 1165 | */ |
| 1166 | void acl_unregister_keywords(struct acl_kw_list *kwl) |
| 1167 | { |
| 1168 | LIST_DEL(&kwl->list); |
| 1169 | LIST_INIT(&kwl->list); |
| 1170 | } |
| 1171 | |
| 1172 | /* Return a pointer to the ACL <name> within the list starting at <head>, or |
| 1173 | * NULL if not found. |
| 1174 | */ |
| 1175 | struct acl *find_acl_by_name(const char *name, struct list *head) |
| 1176 | { |
| 1177 | struct acl *acl; |
| 1178 | list_for_each_entry(acl, head, list) { |
| 1179 | if (strcmp(acl->name, name) == 0) |
| 1180 | return acl; |
| 1181 | } |
| 1182 | return NULL; |
| 1183 | } |
| 1184 | |
| 1185 | /* Return a pointer to the ACL keyword <kw>, or NULL if not found. Note that if |
| 1186 | * <kw> contains an opening parenthesis, only the left part of it is checked. |
| 1187 | */ |
| 1188 | struct acl_keyword *find_acl_kw(const char *kw) |
| 1189 | { |
| 1190 | int index; |
| 1191 | const char *kwend; |
| 1192 | struct acl_kw_list *kwl; |
| 1193 | |
| 1194 | kwend = strchr(kw, '('); |
| 1195 | if (!kwend) |
| 1196 | kwend = kw + strlen(kw); |
| 1197 | |
| 1198 | list_for_each_entry(kwl, &acl_keywords.list, list) { |
| 1199 | for (index = 0; kwl->kw[index].kw != NULL; index++) { |
| 1200 | if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) && |
| 1201 | kwl->kw[index].kw[kwend-kw] == 0) |
| 1202 | return &kwl->kw[index]; |
| 1203 | } |
| 1204 | } |
| 1205 | return NULL; |
| 1206 | } |
| 1207 | |
Willy Tarreau | dfd7fca | 2011-03-09 07:27:02 +0100 | [diff] [blame] | 1208 | /* NB: does nothing if <pat> is NULL */ |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 1209 | static void free_pattern(struct acl_pattern *pat) |
| 1210 | { |
Willy Tarreau | dfd7fca | 2011-03-09 07:27:02 +0100 | [diff] [blame] | 1211 | if (!pat) |
| 1212 | return; |
Krzysztof Piotr Oledzki | 8001d61 | 2008-05-31 13:53:23 +0200 | [diff] [blame] | 1213 | |
| 1214 | if (pat->ptr.ptr) { |
| 1215 | if (pat->freeptrbuf) |
| 1216 | pat->freeptrbuf(pat->ptr.ptr); |
| 1217 | |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 1218 | free(pat->ptr.ptr); |
Krzysztof Piotr Oledzki | 8001d61 | 2008-05-31 13:53:23 +0200 | [diff] [blame] | 1219 | } |
| 1220 | |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 1221 | free(pat); |
| 1222 | } |
| 1223 | |
| 1224 | static void free_pattern_list(struct list *head) |
| 1225 | { |
| 1226 | struct acl_pattern *pat, *tmp; |
| 1227 | list_for_each_entry_safe(pat, tmp, head, list) |
| 1228 | free_pattern(pat); |
| 1229 | } |
| 1230 | |
Willy Tarreau | e56cda9 | 2010-05-11 23:25:05 +0200 | [diff] [blame] | 1231 | static void free_pattern_tree(struct eb_root *root) |
| 1232 | { |
| 1233 | struct eb_node *node, *next; |
| 1234 | node = eb_first(root); |
| 1235 | while (node) { |
| 1236 | next = eb_next(node); |
| 1237 | free(node); |
| 1238 | node = next; |
| 1239 | } |
| 1240 | } |
| 1241 | |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 1242 | static struct acl_expr *prune_acl_expr(struct acl_expr *expr) |
| 1243 | { |
| 1244 | free_pattern_list(&expr->patterns); |
Willy Tarreau | e56cda9 | 2010-05-11 23:25:05 +0200 | [diff] [blame] | 1245 | free_pattern_tree(&expr->pattern_tree); |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 1246 | LIST_INIT(&expr->patterns); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1247 | if (expr->arg_len && expr->arg.str) |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 1248 | free(expr->arg.str); |
| 1249 | expr->kw->use_cnt--; |
| 1250 | return expr; |
| 1251 | } |
| 1252 | |
Willy Tarreau | 2b5285d | 2010-05-09 23:45:24 +0200 | [diff] [blame] | 1253 | static int acl_read_patterns_from_file( struct acl_keyword *aclkw, |
| 1254 | struct acl_expr *expr, |
| 1255 | const char *filename, int patflags) |
| 1256 | { |
| 1257 | FILE *file; |
| 1258 | char *c; |
| 1259 | const char *args[2]; |
| 1260 | struct acl_pattern *pattern; |
| 1261 | int opaque; |
Willy Tarreau | 6a8097f | 2011-02-26 15:14:15 +0100 | [diff] [blame] | 1262 | int ret = 0; |
Willy Tarreau | 2b5285d | 2010-05-09 23:45:24 +0200 | [diff] [blame] | 1263 | |
| 1264 | file = fopen(filename, "r"); |
| 1265 | if (!file) |
| 1266 | return 0; |
| 1267 | |
| 1268 | /* now parse all patterns. The file may contain only one pattern per |
| 1269 | * line. If the line contains spaces, they will be part of the pattern. |
| 1270 | * The pattern stops at the first CR, LF or EOF encountered. |
| 1271 | */ |
| 1272 | opaque = 0; |
Willy Tarreau | e56cda9 | 2010-05-11 23:25:05 +0200 | [diff] [blame] | 1273 | pattern = NULL; |
Willy Tarreau | 2b5285d | 2010-05-09 23:45:24 +0200 | [diff] [blame] | 1274 | args[1] = ""; |
| 1275 | while (fgets(trash, sizeof(trash), file) != NULL) { |
| 1276 | |
| 1277 | c = trash; |
Willy Tarreau | 58215a0 | 2010-05-13 22:07:43 +0200 | [diff] [blame] | 1278 | |
| 1279 | /* ignore lines beginning with a dash */ |
| 1280 | if (*c == '#') |
| 1281 | continue; |
| 1282 | |
| 1283 | /* strip leading spaces and tabs */ |
| 1284 | while (*c == ' ' || *c == '\t') |
| 1285 | c++; |
| 1286 | |
Willy Tarreau | 58215a0 | 2010-05-13 22:07:43 +0200 | [diff] [blame] | 1287 | |
| 1288 | args[0] = c; |
Willy Tarreau | 2b5285d | 2010-05-09 23:45:24 +0200 | [diff] [blame] | 1289 | while (*c && *c != '\n' && *c != '\r') |
| 1290 | c++; |
| 1291 | *c = 0; |
| 1292 | |
Willy Tarreau | 5109196 | 2011-01-03 21:04:10 +0100 | [diff] [blame] | 1293 | /* empty lines are ignored too */ |
| 1294 | if (c == args[0]) |
| 1295 | continue; |
| 1296 | |
Willy Tarreau | e56cda9 | 2010-05-11 23:25:05 +0200 | [diff] [blame] | 1297 | /* we keep the previous pattern along iterations as long as it's not used */ |
| 1298 | if (!pattern) |
| 1299 | pattern = (struct acl_pattern *)malloc(sizeof(*pattern)); |
Willy Tarreau | 2b5285d | 2010-05-09 23:45:24 +0200 | [diff] [blame] | 1300 | if (!pattern) |
| 1301 | goto out_close; |
Willy Tarreau | e56cda9 | 2010-05-11 23:25:05 +0200 | [diff] [blame] | 1302 | |
| 1303 | memset(pattern, 0, sizeof(*pattern)); |
Willy Tarreau | 2b5285d | 2010-05-09 23:45:24 +0200 | [diff] [blame] | 1304 | pattern->flags = patflags; |
| 1305 | |
Willy Tarreau | e56cda9 | 2010-05-11 23:25:05 +0200 | [diff] [blame] | 1306 | if ((aclkw->requires & ACL_MAY_LOOKUP) && !(pattern->flags & ACL_PAT_F_IGNORE_CASE)) { |
| 1307 | /* we pre-set the data pointer to the tree's head so that functions |
| 1308 | * which are able to insert in a tree know where to do that. |
| 1309 | */ |
| 1310 | pattern->flags |= ACL_PAT_F_TREE_OK; |
| 1311 | pattern->val.tree = &expr->pattern_tree; |
| 1312 | } |
| 1313 | |
Willy Tarreau | 2b5285d | 2010-05-09 23:45:24 +0200 | [diff] [blame] | 1314 | if (!aclkw->parse(args, pattern, &opaque)) |
| 1315 | goto out_free_pattern; |
Willy Tarreau | e56cda9 | 2010-05-11 23:25:05 +0200 | [diff] [blame] | 1316 | |
| 1317 | /* if the parser did not feed the tree, let's chain the pattern to the list */ |
| 1318 | if (!(pattern->flags & ACL_PAT_F_TREE)) { |
| 1319 | LIST_ADDQ(&expr->patterns, &pattern->list); |
| 1320 | pattern = NULL; /* get a new one */ |
| 1321 | } |
Willy Tarreau | 2b5285d | 2010-05-09 23:45:24 +0200 | [diff] [blame] | 1322 | } |
Willy Tarreau | 6a8097f | 2011-02-26 15:14:15 +0100 | [diff] [blame] | 1323 | |
| 1324 | ret = 1; /* success */ |
Willy Tarreau | 2b5285d | 2010-05-09 23:45:24 +0200 | [diff] [blame] | 1325 | |
| 1326 | out_free_pattern: |
| 1327 | free_pattern(pattern); |
| 1328 | out_close: |
| 1329 | fclose(file); |
Willy Tarreau | 6a8097f | 2011-02-26 15:14:15 +0100 | [diff] [blame] | 1330 | return ret; |
Willy Tarreau | 2b5285d | 2010-05-09 23:45:24 +0200 | [diff] [blame] | 1331 | } |
| 1332 | |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 1333 | /* Parse an ACL expression starting at <args>[0], and return it. |
| 1334 | * Right now, the only accepted syntax is : |
| 1335 | * <subject> [<value>...] |
| 1336 | */ |
| 1337 | struct acl_expr *parse_acl_expr(const char **args) |
| 1338 | { |
| 1339 | __label__ out_return, out_free_expr, out_free_pattern; |
| 1340 | struct acl_expr *expr; |
| 1341 | struct acl_keyword *aclkw; |
| 1342 | struct acl_pattern *pattern; |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 1343 | int opaque, patflags; |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 1344 | const char *arg; |
| 1345 | |
| 1346 | aclkw = find_acl_kw(args[0]); |
| 1347 | if (!aclkw || !aclkw->parse) |
| 1348 | goto out_return; |
| 1349 | |
| 1350 | expr = (struct acl_expr *)calloc(1, sizeof(*expr)); |
| 1351 | if (!expr) |
| 1352 | goto out_return; |
| 1353 | |
| 1354 | expr->kw = aclkw; |
| 1355 | aclkw->use_cnt++; |
| 1356 | LIST_INIT(&expr->patterns); |
Willy Tarreau | e56cda9 | 2010-05-11 23:25:05 +0200 | [diff] [blame] | 1357 | expr->pattern_tree = EB_ROOT_UNIQUE; |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 1358 | expr->arg.str = NULL; |
Willy Tarreau | bb76891 | 2007-06-10 11:17:01 +0200 | [diff] [blame] | 1359 | expr->arg_len = 0; |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 1360 | |
| 1361 | arg = strchr(args[0], '('); |
| 1362 | if (arg != NULL) { |
| 1363 | char *end, *arg2; |
| 1364 | /* there is an argument in the form "subject(arg)" */ |
| 1365 | arg++; |
| 1366 | end = strchr(arg, ')'); |
| 1367 | if (!end) |
| 1368 | goto out_free_expr; |
Willy Tarreau | ac778f5 | 2010-01-26 19:02:46 +0100 | [diff] [blame] | 1369 | arg2 = my_strndup(arg, end - arg); |
Willy Tarreau | 1edb144 | 2010-01-27 20:13:38 +0100 | [diff] [blame] | 1370 | if (!arg2) |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 1371 | goto out_free_expr; |
Willy Tarreau | bb76891 | 2007-06-10 11:17:01 +0200 | [diff] [blame] | 1372 | expr->arg_len = end - arg; |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 1373 | expr->arg.str = arg2; |
| 1374 | } |
| 1375 | |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 1376 | args++; |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 1377 | |
| 1378 | /* check for options before patterns. Supported options are : |
| 1379 | * -i : ignore case for all patterns by default |
| 1380 | * -f : read patterns from those files |
| 1381 | * -- : everything after this is not an option |
| 1382 | */ |
| 1383 | patflags = 0; |
| 1384 | while (**args == '-') { |
| 1385 | if ((*args)[1] == 'i') |
| 1386 | patflags |= ACL_PAT_F_IGNORE_CASE; |
Willy Tarreau | 2b5285d | 2010-05-09 23:45:24 +0200 | [diff] [blame] | 1387 | else if ((*args)[1] == 'f') { |
| 1388 | if (!acl_read_patterns_from_file(aclkw, expr, args[1], patflags | ACL_PAT_F_FROM_FILE)) |
| 1389 | goto out_free_expr; |
| 1390 | args++; |
| 1391 | } |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 1392 | else if ((*args)[1] == '-') { |
| 1393 | args++; |
| 1394 | break; |
| 1395 | } |
| 1396 | else |
| 1397 | break; |
| 1398 | args++; |
| 1399 | } |
| 1400 | |
| 1401 | /* now parse all patterns */ |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 1402 | opaque = 0; |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 1403 | while (**args) { |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 1404 | int ret; |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 1405 | pattern = (struct acl_pattern *)calloc(1, sizeof(*pattern)); |
| 1406 | if (!pattern) |
| 1407 | goto out_free_expr; |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 1408 | pattern->flags = patflags; |
| 1409 | |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 1410 | ret = aclkw->parse(args, pattern, &opaque); |
| 1411 | if (!ret) |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 1412 | goto out_free_pattern; |
| 1413 | LIST_ADDQ(&expr->patterns, &pattern->list); |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 1414 | args += ret; |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 1415 | } |
| 1416 | |
| 1417 | return expr; |
| 1418 | |
| 1419 | out_free_pattern: |
| 1420 | free_pattern(pattern); |
| 1421 | out_free_expr: |
| 1422 | prune_acl_expr(expr); |
| 1423 | free(expr); |
| 1424 | out_return: |
| 1425 | return NULL; |
| 1426 | } |
| 1427 | |
Krzysztof Piotr Oledzki | a643baf | 2008-05-29 23:53:44 +0200 | [diff] [blame] | 1428 | /* Purge everything in the acl <acl>, then return <acl>. */ |
| 1429 | struct acl *prune_acl(struct acl *acl) { |
| 1430 | |
| 1431 | struct acl_expr *expr, *exprb; |
| 1432 | |
| 1433 | free(acl->name); |
| 1434 | |
| 1435 | list_for_each_entry_safe(expr, exprb, &acl->expr, list) { |
| 1436 | LIST_DEL(&expr->list); |
| 1437 | prune_acl_expr(expr); |
| 1438 | free(expr); |
| 1439 | } |
| 1440 | |
| 1441 | return acl; |
| 1442 | } |
| 1443 | |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 1444 | /* Parse an ACL with the name starting at <args>[0], and with a list of already |
| 1445 | * known ACLs in <acl>. If the ACL was not in the list, it will be added. |
Willy Tarreau | 2a56c5e | 2010-03-15 16:13:29 +0100 | [diff] [blame] | 1446 | * A pointer to that ACL is returned. If the ACL has an empty name, then it's |
| 1447 | * an anonymous one and it won't be merged with any other one. |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 1448 | * |
| 1449 | * args syntax: <aclname> <acl_expr> |
| 1450 | */ |
| 1451 | struct acl *parse_acl(const char **args, struct list *known_acl) |
| 1452 | { |
| 1453 | __label__ out_return, out_free_acl_expr, out_free_name; |
| 1454 | struct acl *cur_acl; |
| 1455 | struct acl_expr *acl_expr; |
| 1456 | char *name; |
| 1457 | |
Willy Tarreau | 2a56c5e | 2010-03-15 16:13:29 +0100 | [diff] [blame] | 1458 | if (**args && invalid_char(*args)) |
Willy Tarreau | 2e74c3f | 2007-12-02 18:45:09 +0100 | [diff] [blame] | 1459 | goto out_return; |
| 1460 | |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 1461 | acl_expr = parse_acl_expr(args + 1); |
| 1462 | if (!acl_expr) |
| 1463 | goto out_return; |
| 1464 | |
Willy Tarreau | 404e8ab | 2009-07-26 19:40:40 +0200 | [diff] [blame] | 1465 | /* Check for args beginning with an opening parenthesis just after the |
| 1466 | * subject, as this is almost certainly a typo. Right now we can only |
| 1467 | * emit a warning, so let's do so. |
| 1468 | */ |
Krzysztof Piotr Oledzki | 4cdd831 | 2009-10-05 00:23:35 +0200 | [diff] [blame] | 1469 | if (!strchr(args[1], '(') && *args[2] == '(') |
Willy Tarreau | 404e8ab | 2009-07-26 19:40:40 +0200 | [diff] [blame] | 1470 | Warning("parsing acl '%s' :\n" |
| 1471 | " matching '%s' for pattern '%s' is likely a mistake and probably\n" |
| 1472 | " not what you want. Maybe you need to remove the extraneous space before '('.\n" |
| 1473 | " If you are really sure this is not an error, please insert '--' between the\n" |
| 1474 | " match and the pattern to make this warning message disappear.\n", |
| 1475 | args[0], args[1], args[2]); |
| 1476 | |
Willy Tarreau | 2a56c5e | 2010-03-15 16:13:29 +0100 | [diff] [blame] | 1477 | if (*args[0]) |
| 1478 | cur_acl = find_acl_by_name(args[0], known_acl); |
| 1479 | else |
| 1480 | cur_acl = NULL; |
| 1481 | |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 1482 | if (!cur_acl) { |
| 1483 | name = strdup(args[0]); |
| 1484 | if (!name) |
| 1485 | goto out_free_acl_expr; |
| 1486 | cur_acl = (struct acl *)calloc(1, sizeof(*cur_acl)); |
| 1487 | if (cur_acl == NULL) |
| 1488 | goto out_free_name; |
| 1489 | |
| 1490 | LIST_INIT(&cur_acl->expr); |
| 1491 | LIST_ADDQ(known_acl, &cur_acl->list); |
| 1492 | cur_acl->name = name; |
| 1493 | } |
| 1494 | |
Willy Tarreau | a980263 | 2008-07-25 19:13:19 +0200 | [diff] [blame] | 1495 | cur_acl->requires |= acl_expr->kw->requires; |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 1496 | LIST_ADDQ(&cur_acl->expr, &acl_expr->list); |
| 1497 | return cur_acl; |
| 1498 | |
| 1499 | out_free_name: |
| 1500 | free(name); |
| 1501 | out_free_acl_expr: |
| 1502 | prune_acl_expr(acl_expr); |
| 1503 | free(acl_expr); |
| 1504 | out_return: |
| 1505 | return NULL; |
| 1506 | } |
| 1507 | |
Willy Tarreau | 16fbe82 | 2007-06-17 11:54:31 +0200 | [diff] [blame] | 1508 | /* Some useful ACLs provided by default. Only those used are allocated. */ |
| 1509 | |
| 1510 | const struct { |
| 1511 | const char *name; |
| 1512 | const char *expr[4]; /* put enough for longest expression */ |
| 1513 | } default_acl_list[] = { |
Willy Tarreau | 58393e1 | 2008-07-20 10:39:22 +0200 | [diff] [blame] | 1514 | { .name = "TRUE", .expr = {"always_true",""}}, |
| 1515 | { .name = "FALSE", .expr = {"always_false",""}}, |
Willy Tarreau | 16fbe82 | 2007-06-17 11:54:31 +0200 | [diff] [blame] | 1516 | { .name = "LOCALHOST", .expr = {"src","127.0.0.1/8",""}}, |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1517 | { .name = "HTTP", .expr = {"req_proto_http",""}}, |
Willy Tarreau | 16fbe82 | 2007-06-17 11:54:31 +0200 | [diff] [blame] | 1518 | { .name = "HTTP_1.0", .expr = {"req_ver","1.0",""}}, |
| 1519 | { .name = "HTTP_1.1", .expr = {"req_ver","1.1",""}}, |
| 1520 | { .name = "METH_CONNECT", .expr = {"method","CONNECT",""}}, |
| 1521 | { .name = "METH_GET", .expr = {"method","GET","HEAD",""}}, |
| 1522 | { .name = "METH_HEAD", .expr = {"method","HEAD",""}}, |
| 1523 | { .name = "METH_OPTIONS", .expr = {"method","OPTIONS",""}}, |
| 1524 | { .name = "METH_POST", .expr = {"method","POST",""}}, |
| 1525 | { .name = "METH_TRACE", .expr = {"method","TRACE",""}}, |
| 1526 | { .name = "HTTP_URL_ABS", .expr = {"url_reg","^[^/:]*://",""}}, |
| 1527 | { .name = "HTTP_URL_SLASH", .expr = {"url_beg","/",""}}, |
| 1528 | { .name = "HTTP_URL_STAR", .expr = {"url","*",""}}, |
| 1529 | { .name = "HTTP_CONTENT", .expr = {"hdr_val(content-length)","gt","0",""}}, |
Emeric Brun | bede3d0 | 2009-06-30 17:54:00 +0200 | [diff] [blame] | 1530 | { .name = "RDP_COOKIE", .expr = {"req_rdp_cookie_cnt","gt","0",""}}, |
Willy Tarreau | c631770 | 2008-07-20 09:29:50 +0200 | [diff] [blame] | 1531 | { .name = "REQ_CONTENT", .expr = {"req_len","gt","0",""}}, |
Willy Tarreau | b6fb420 | 2008-07-20 11:18:28 +0200 | [diff] [blame] | 1532 | { .name = "WAIT_END", .expr = {"wait_end",""}}, |
Willy Tarreau | 16fbe82 | 2007-06-17 11:54:31 +0200 | [diff] [blame] | 1533 | { .name = NULL, .expr = {""}} |
| 1534 | }; |
| 1535 | |
| 1536 | /* Find a default ACL from the default_acl list, compile it and return it. |
| 1537 | * If the ACL is not found, NULL is returned. In theory, it cannot fail, |
| 1538 | * except when default ACLs are broken, in which case it will return NULL. |
| 1539 | * If <known_acl> is not NULL, the ACL will be queued at its tail. |
| 1540 | */ |
| 1541 | struct acl *find_acl_default(const char *acl_name, struct list *known_acl) |
| 1542 | { |
| 1543 | __label__ out_return, out_free_acl_expr, out_free_name; |
| 1544 | struct acl *cur_acl; |
| 1545 | struct acl_expr *acl_expr; |
| 1546 | char *name; |
| 1547 | int index; |
| 1548 | |
| 1549 | for (index = 0; default_acl_list[index].name != NULL; index++) { |
| 1550 | if (strcmp(acl_name, default_acl_list[index].name) == 0) |
| 1551 | break; |
| 1552 | } |
| 1553 | |
| 1554 | if (default_acl_list[index].name == NULL) |
| 1555 | return NULL; |
| 1556 | |
| 1557 | acl_expr = parse_acl_expr((const char **)default_acl_list[index].expr); |
| 1558 | if (!acl_expr) |
| 1559 | goto out_return; |
| 1560 | |
| 1561 | name = strdup(acl_name); |
| 1562 | if (!name) |
| 1563 | goto out_free_acl_expr; |
| 1564 | cur_acl = (struct acl *)calloc(1, sizeof(*cur_acl)); |
| 1565 | if (cur_acl == NULL) |
| 1566 | goto out_free_name; |
| 1567 | |
| 1568 | cur_acl->name = name; |
Willy Tarreau | a55b7dc | 2009-07-12 09:21:30 +0200 | [diff] [blame] | 1569 | cur_acl->requires |= acl_expr->kw->requires; |
Willy Tarreau | 16fbe82 | 2007-06-17 11:54:31 +0200 | [diff] [blame] | 1570 | LIST_INIT(&cur_acl->expr); |
| 1571 | LIST_ADDQ(&cur_acl->expr, &acl_expr->list); |
| 1572 | if (known_acl) |
| 1573 | LIST_ADDQ(known_acl, &cur_acl->list); |
| 1574 | |
| 1575 | return cur_acl; |
| 1576 | |
| 1577 | out_free_name: |
| 1578 | free(name); |
| 1579 | out_free_acl_expr: |
| 1580 | prune_acl_expr(acl_expr); |
| 1581 | free(acl_expr); |
| 1582 | out_return: |
| 1583 | return NULL; |
| 1584 | } |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 1585 | |
| 1586 | /* Purge everything in the acl_cond <cond>, then return <cond>. */ |
| 1587 | struct acl_cond *prune_acl_cond(struct acl_cond *cond) |
| 1588 | { |
| 1589 | struct acl_term_suite *suite, *tmp_suite; |
| 1590 | struct acl_term *term, *tmp_term; |
| 1591 | |
| 1592 | /* iterate through all term suites and free all terms and all suites */ |
| 1593 | list_for_each_entry_safe(suite, tmp_suite, &cond->suites, list) { |
| 1594 | list_for_each_entry_safe(term, tmp_term, &suite->terms, list) |
| 1595 | free(term); |
| 1596 | free(suite); |
| 1597 | } |
| 1598 | return cond; |
| 1599 | } |
| 1600 | |
| 1601 | /* Parse an ACL condition starting at <args>[0], relying on a list of already |
| 1602 | * known ACLs passed in <known_acl>. The new condition is returned (or NULL in |
| 1603 | * case of low memory). Supports multiple conditions separated by "or". |
| 1604 | */ |
| 1605 | struct acl_cond *parse_acl_cond(const char **args, struct list *known_acl, int pol) |
| 1606 | { |
| 1607 | __label__ out_return, out_free_suite, out_free_term; |
Willy Tarreau | 74b98a8 | 2007-06-16 19:35:18 +0200 | [diff] [blame] | 1608 | int arg, neg; |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 1609 | const char *word; |
| 1610 | struct acl *cur_acl; |
| 1611 | struct acl_term *cur_term; |
| 1612 | struct acl_term_suite *cur_suite; |
| 1613 | struct acl_cond *cond; |
| 1614 | |
| 1615 | cond = (struct acl_cond *)calloc(1, sizeof(*cond)); |
| 1616 | if (cond == NULL) |
| 1617 | goto out_return; |
| 1618 | |
| 1619 | LIST_INIT(&cond->list); |
| 1620 | LIST_INIT(&cond->suites); |
| 1621 | cond->pol = pol; |
| 1622 | |
| 1623 | cur_suite = NULL; |
Willy Tarreau | 74b98a8 | 2007-06-16 19:35:18 +0200 | [diff] [blame] | 1624 | neg = 0; |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 1625 | for (arg = 0; *args[arg]; arg++) { |
| 1626 | word = args[arg]; |
| 1627 | |
| 1628 | /* remove as many exclamation marks as we can */ |
| 1629 | while (*word == '!') { |
| 1630 | neg = !neg; |
| 1631 | word++; |
| 1632 | } |
| 1633 | |
| 1634 | /* an empty word is allowed because we cannot force the user to |
| 1635 | * always think about not leaving exclamation marks alone. |
| 1636 | */ |
| 1637 | if (!*word) |
| 1638 | continue; |
| 1639 | |
Willy Tarreau | 16fbe82 | 2007-06-17 11:54:31 +0200 | [diff] [blame] | 1640 | if (strcasecmp(word, "or") == 0 || strcmp(word, "||") == 0) { |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 1641 | /* new term suite */ |
| 1642 | cur_suite = NULL; |
| 1643 | neg = 0; |
| 1644 | continue; |
| 1645 | } |
| 1646 | |
Willy Tarreau | 95fa469 | 2010-02-01 13:05:50 +0100 | [diff] [blame] | 1647 | if (strcmp(word, "{") == 0) { |
| 1648 | /* we may have a complete ACL expression between two braces, |
| 1649 | * find the last one. |
| 1650 | */ |
| 1651 | int arg_end = arg + 1; |
| 1652 | const char **args_new; |
| 1653 | |
| 1654 | while (*args[arg_end] && strcmp(args[arg_end], "}") != 0) |
| 1655 | arg_end++; |
| 1656 | |
| 1657 | if (!*args[arg_end]) |
| 1658 | goto out_free_suite; |
| 1659 | |
| 1660 | args_new = calloc(1, (arg_end - arg + 1) * sizeof(*args_new)); |
| 1661 | if (!args_new) |
| 1662 | goto out_free_suite; |
| 1663 | |
Willy Tarreau | 2a56c5e | 2010-03-15 16:13:29 +0100 | [diff] [blame] | 1664 | args_new[0] = ""; |
Willy Tarreau | 95fa469 | 2010-02-01 13:05:50 +0100 | [diff] [blame] | 1665 | memcpy(args_new + 1, args + arg + 1, (arg_end - arg) * sizeof(*args_new)); |
| 1666 | args_new[arg_end - arg] = ""; |
| 1667 | cur_acl = parse_acl(args_new, known_acl); |
| 1668 | free(args_new); |
| 1669 | |
| 1670 | if (!cur_acl) |
Willy Tarreau | 16fbe82 | 2007-06-17 11:54:31 +0200 | [diff] [blame] | 1671 | goto out_free_suite; |
Willy Tarreau | 95fa469 | 2010-02-01 13:05:50 +0100 | [diff] [blame] | 1672 | arg = arg_end; |
| 1673 | } |
| 1674 | else { |
| 1675 | /* search for <word> in the known ACL names. If we do not find |
| 1676 | * it, let's look for it in the default ACLs, and if found, add |
| 1677 | * it to the list of ACLs of this proxy. This makes it possible |
| 1678 | * to override them. |
| 1679 | */ |
| 1680 | cur_acl = find_acl_by_name(word, known_acl); |
| 1681 | if (cur_acl == NULL) { |
| 1682 | cur_acl = find_acl_default(word, known_acl); |
| 1683 | if (cur_acl == NULL) |
| 1684 | goto out_free_suite; |
| 1685 | } |
Willy Tarreau | 16fbe82 | 2007-06-17 11:54:31 +0200 | [diff] [blame] | 1686 | } |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 1687 | |
| 1688 | cur_term = (struct acl_term *)calloc(1, sizeof(*cur_term)); |
| 1689 | if (cur_term == NULL) |
| 1690 | goto out_free_suite; |
| 1691 | |
| 1692 | cur_term->acl = cur_acl; |
| 1693 | cur_term->neg = neg; |
Willy Tarreau | a980263 | 2008-07-25 19:13:19 +0200 | [diff] [blame] | 1694 | cond->requires |= cur_acl->requires; |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 1695 | |
| 1696 | if (!cur_suite) { |
| 1697 | cur_suite = (struct acl_term_suite *)calloc(1, sizeof(*cur_suite)); |
| 1698 | if (cur_term == NULL) |
| 1699 | goto out_free_term; |
| 1700 | LIST_INIT(&cur_suite->terms); |
| 1701 | LIST_ADDQ(&cond->suites, &cur_suite->list); |
| 1702 | } |
| 1703 | LIST_ADDQ(&cur_suite->terms, &cur_term->list); |
Willy Tarreau | 74b98a8 | 2007-06-16 19:35:18 +0200 | [diff] [blame] | 1704 | neg = 0; |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 1705 | } |
| 1706 | |
| 1707 | return cond; |
| 1708 | |
| 1709 | out_free_term: |
| 1710 | free(cur_term); |
| 1711 | out_free_suite: |
| 1712 | prune_acl_cond(cond); |
| 1713 | free(cond); |
| 1714 | out_return: |
| 1715 | return NULL; |
| 1716 | } |
| 1717 | |
Willy Tarreau | 2bbba41 | 2010-01-28 16:48:33 +0100 | [diff] [blame] | 1718 | /* Builds an ACL condition starting at the if/unless keyword. The complete |
| 1719 | * condition is returned. NULL is returned in case of error or if the first |
| 1720 | * word is neither "if" nor "unless". It automatically sets the file name and |
| 1721 | * the line number in the condition for better error reporting, and adds the |
| 1722 | * ACL requirements to the proxy's acl_requires. |
| 1723 | */ |
| 1724 | struct acl_cond *build_acl_cond(const char *file, int line, struct proxy *px, const char **args) |
| 1725 | { |
| 1726 | int pol = ACL_COND_NONE; |
| 1727 | struct acl_cond *cond = NULL; |
| 1728 | |
| 1729 | if (!strcmp(*args, "if")) { |
| 1730 | pol = ACL_COND_IF; |
| 1731 | args++; |
| 1732 | } |
| 1733 | else if (!strcmp(*args, "unless")) { |
| 1734 | pol = ACL_COND_UNLESS; |
| 1735 | args++; |
| 1736 | } |
| 1737 | else |
| 1738 | return NULL; |
| 1739 | |
| 1740 | cond = parse_acl_cond(args, &px->acl, pol); |
| 1741 | if (!cond) |
| 1742 | return NULL; |
| 1743 | |
| 1744 | cond->file = file; |
| 1745 | cond->line = line; |
| 1746 | px->acl_requires |= cond->requires; |
| 1747 | |
| 1748 | return cond; |
| 1749 | } |
| 1750 | |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 1751 | /* Execute condition <cond> and return either ACL_PAT_FAIL, ACL_PAT_MISS or |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1752 | * ACL_PAT_PASS depending on the test results. ACL_PAT_MISS may only be |
| 1753 | * returned if <dir> contains ACL_PARTIAL, indicating that incomplete data |
| 1754 | * is being examined. |
| 1755 | * This function only computes the condition, it does not apply the polarity |
| 1756 | * required by IF/UNLESS, it's up to the caller to do this using something like |
| 1757 | * this : |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 1758 | * |
| 1759 | * res = acl_pass(res); |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1760 | * if (res == ACL_PAT_MISS) |
| 1761 | * return 0; |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 1762 | * if (cond->pol == ACL_COND_UNLESS) |
| 1763 | * res = !res; |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 1764 | */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 1765 | int acl_exec_cond(struct acl_cond *cond, struct proxy *px, struct session *l4, void *l7, int dir) |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 1766 | { |
| 1767 | __label__ fetch_next; |
| 1768 | struct acl_term_suite *suite; |
| 1769 | struct acl_term *term; |
| 1770 | struct acl_expr *expr; |
| 1771 | struct acl *acl; |
| 1772 | struct acl_pattern *pattern; |
| 1773 | struct acl_test test; |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 1774 | int acl_res, suite_res, cond_res; |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 1775 | |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 1776 | /* We're doing a logical OR between conditions so we initialize to FAIL. |
| 1777 | * The MISS status is propagated down from the suites. |
| 1778 | */ |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 1779 | cond_res = ACL_PAT_FAIL; |
| 1780 | list_for_each_entry(suite, &cond->suites, list) { |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 1781 | /* Evaluate condition suite <suite>. We stop at the first term |
| 1782 | * which returns ACL_PAT_FAIL. The MISS status is still propagated |
| 1783 | * in case of uncertainty in the result. |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 1784 | */ |
| 1785 | |
| 1786 | /* we're doing a logical AND between terms, so we must set the |
| 1787 | * initial value to PASS. |
| 1788 | */ |
| 1789 | suite_res = ACL_PAT_PASS; |
| 1790 | list_for_each_entry(term, &suite->terms, list) { |
| 1791 | acl = term->acl; |
| 1792 | |
| 1793 | /* FIXME: use cache ! |
| 1794 | * check acl->cache_idx for this. |
| 1795 | */ |
| 1796 | |
| 1797 | /* ACL result not cached. Let's scan all the expressions |
| 1798 | * and use the first one to match. |
| 1799 | */ |
| 1800 | acl_res = ACL_PAT_FAIL; |
| 1801 | list_for_each_entry(expr, &acl->expr, list) { |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 1802 | /* we need to reset context and flags */ |
| 1803 | memset(&test, 0, sizeof(test)); |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 1804 | fetch_next: |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1805 | if (!expr->kw->fetch(px, l4, l7, dir, expr, &test)) { |
| 1806 | /* maybe we could not fetch because of missing data */ |
| 1807 | if (test.flags & ACL_TEST_F_MAY_CHANGE && dir & ACL_PARTIAL) |
| 1808 | acl_res |= ACL_PAT_MISS; |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 1809 | continue; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1810 | } |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 1811 | |
Willy Tarreau | a79534f | 2008-07-20 10:13:37 +0200 | [diff] [blame] | 1812 | if (test.flags & ACL_TEST_F_RES_SET) { |
| 1813 | if (test.flags & ACL_TEST_F_RES_PASS) |
| 1814 | acl_res |= ACL_PAT_PASS; |
| 1815 | else |
| 1816 | acl_res |= ACL_PAT_FAIL; |
| 1817 | } |
| 1818 | else { |
Willy Tarreau | 020534d | 2010-05-16 21:45:45 +0200 | [diff] [blame] | 1819 | if (!eb_is_empty(&expr->pattern_tree)) { |
Willy Tarreau | c426296 | 2010-05-10 23:42:40 +0200 | [diff] [blame] | 1820 | /* a tree is present, let's check what type it is */ |
| 1821 | if (expr->kw->match == acl_match_str) |
| 1822 | acl_res |= acl_lookup_str(&test, expr) ? ACL_PAT_PASS : ACL_PAT_FAIL; |
Willy Tarreau | b337b53 | 2010-05-13 20:03:41 +0200 | [diff] [blame] | 1823 | else if (expr->kw->match == acl_match_ip) |
| 1824 | acl_res |= acl_lookup_ip(&test, expr) ? ACL_PAT_PASS : ACL_PAT_FAIL; |
Willy Tarreau | c426296 | 2010-05-10 23:42:40 +0200 | [diff] [blame] | 1825 | } |
| 1826 | |
Willy Tarreau | a79534f | 2008-07-20 10:13:37 +0200 | [diff] [blame] | 1827 | /* call the match() function for all tests on this value */ |
| 1828 | list_for_each_entry(pattern, &expr->patterns, list) { |
Willy Tarreau | a79534f | 2008-07-20 10:13:37 +0200 | [diff] [blame] | 1829 | if (acl_res == ACL_PAT_PASS) |
| 1830 | break; |
Willy Tarreau | e56cda9 | 2010-05-11 23:25:05 +0200 | [diff] [blame] | 1831 | acl_res |= expr->kw->match(&test, pattern); |
Willy Tarreau | a79534f | 2008-07-20 10:13:37 +0200 | [diff] [blame] | 1832 | } |
Krzysztof Piotr Oledzki | d7528e5 | 2010-01-29 17:55:53 +0100 | [diff] [blame] | 1833 | |
Willy Tarreau | e56cda9 | 2010-05-11 23:25:05 +0200 | [diff] [blame] | 1834 | if ((test.flags & ACL_TEST_F_NULL_MATCH) && |
Willy Tarreau | 020534d | 2010-05-16 21:45:45 +0200 | [diff] [blame] | 1835 | LIST_ISEMPTY(&expr->patterns) && eb_is_empty(&expr->pattern_tree)) |
Krzysztof Piotr Oledzki | d7528e5 | 2010-01-29 17:55:53 +0100 | [diff] [blame] | 1836 | acl_res |= expr->kw->match(&test, NULL); |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 1837 | } |
| 1838 | /* |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 1839 | * OK now acl_res holds the result of this expression |
| 1840 | * as one of ACL_PAT_FAIL, ACL_PAT_MISS or ACL_PAT_PASS. |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 1841 | * |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 1842 | * Then if (!MISS) we can cache the result, and put |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 1843 | * (test.flags & ACL_TEST_F_VOLATILE) in the cache flags. |
| 1844 | * |
| 1845 | * FIXME: implement cache. |
| 1846 | * |
| 1847 | */ |
| 1848 | |
| 1849 | /* now we may have some cleanup to do */ |
| 1850 | if (test.flags & ACL_TEST_F_MUST_FREE) { |
Willy Tarreau | 664092c | 2011-12-16 19:11:42 +0100 | [diff] [blame] | 1851 | free(temp_pattern.data.str.str); |
| 1852 | temp_pattern.data.str.len = 0; |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 1853 | } |
| 1854 | |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 1855 | /* we're ORing these terms, so a single PASS is enough */ |
| 1856 | if (acl_res == ACL_PAT_PASS) |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 1857 | break; |
| 1858 | |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 1859 | if (test.flags & ACL_TEST_F_FETCH_MORE) |
| 1860 | goto fetch_next; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1861 | |
| 1862 | /* sometimes we know the fetched data is subject to change |
| 1863 | * later and give another chance for a new match (eg: request |
| 1864 | * size, time, ...) |
| 1865 | */ |
| 1866 | if (test.flags & ACL_TEST_F_MAY_CHANGE && dir & ACL_PARTIAL) |
| 1867 | acl_res |= ACL_PAT_MISS; |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 1868 | } |
| 1869 | /* |
| 1870 | * Here we have the result of an ACL (cached or not). |
| 1871 | * ACLs are combined, negated or not, to form conditions. |
| 1872 | */ |
| 1873 | |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 1874 | if (term->neg) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 1875 | acl_res = acl_neg(acl_res); |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 1876 | |
| 1877 | suite_res &= acl_res; |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 1878 | |
| 1879 | /* we're ANDing these terms, so a single FAIL is enough */ |
| 1880 | if (suite_res == ACL_PAT_FAIL) |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 1881 | break; |
| 1882 | } |
| 1883 | cond_res |= suite_res; |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 1884 | |
| 1885 | /* we're ORing these terms, so a single PASS is enough */ |
| 1886 | if (cond_res == ACL_PAT_PASS) |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 1887 | break; |
| 1888 | } |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 1889 | return cond_res; |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 1890 | } |
| 1891 | |
| 1892 | |
Willy Tarreau | dd64f8d | 2008-07-27 22:02:32 +0200 | [diff] [blame] | 1893 | /* Reports a pointer to the first ACL used in condition <cond> which requires |
| 1894 | * at least one of the USE_FLAGS in <require>. Returns NULL if none matches. |
| 1895 | * The construct is almost the same as for acl_exec_cond() since we're walking |
| 1896 | * down the ACL tree as well. It is important that the tree is really walked |
| 1897 | * through and never cached, because that way, this function can be used as a |
| 1898 | * late check. |
| 1899 | */ |
Willy Tarreau | f1e98b8 | 2010-01-28 17:59:39 +0100 | [diff] [blame] | 1900 | struct acl *cond_find_require(const struct acl_cond *cond, unsigned int require) |
Willy Tarreau | dd64f8d | 2008-07-27 22:02:32 +0200 | [diff] [blame] | 1901 | { |
| 1902 | struct acl_term_suite *suite; |
| 1903 | struct acl_term *term; |
| 1904 | struct acl *acl; |
| 1905 | |
| 1906 | list_for_each_entry(suite, &cond->suites, list) { |
| 1907 | list_for_each_entry(term, &suite->terms, list) { |
| 1908 | acl = term->acl; |
| 1909 | if (acl->requires & require) |
| 1910 | return acl; |
| 1911 | } |
| 1912 | } |
| 1913 | return NULL; |
| 1914 | } |
| 1915 | |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1916 | /* |
| 1917 | * Find targets for userlist and groups in acl. Function returns the number |
| 1918 | * of errors or OK if everything is fine. |
| 1919 | */ |
| 1920 | int |
| 1921 | acl_find_targets(struct proxy *p) |
| 1922 | { |
| 1923 | |
| 1924 | struct acl *acl; |
| 1925 | struct acl_expr *expr; |
| 1926 | struct acl_pattern *pattern; |
| 1927 | struct userlist *ul; |
| 1928 | int cfgerr = 0; |
| 1929 | |
| 1930 | list_for_each_entry(acl, &p->acl, list) { |
| 1931 | list_for_each_entry(expr, &acl->expr, list) { |
Hervé COMMOWICK | daa824e | 2011-08-05 12:09:44 +0200 | [diff] [blame] | 1932 | if (strcmp(expr->kw->kw, "srv_is_up") == 0 || |
| 1933 | strcmp(expr->kw->kw, "srv_conn") == 0) { |
Willy Tarreau | 0b1cd94 | 2010-05-16 22:18:27 +0200 | [diff] [blame] | 1934 | struct proxy *px; |
| 1935 | struct server *srv; |
| 1936 | char *pname, *sname; |
| 1937 | |
| 1938 | if (!expr->arg.str || !*expr->arg.str) { |
| 1939 | Alert("proxy %s: acl %s %s(): missing server name.\n", |
| 1940 | p->id, acl->name, expr->kw->kw); |
| 1941 | cfgerr++; |
| 1942 | continue; |
| 1943 | } |
| 1944 | |
| 1945 | pname = expr->arg.str; |
| 1946 | sname = strrchr(pname, '/'); |
| 1947 | |
| 1948 | if (sname) |
| 1949 | *sname++ = '\0'; |
| 1950 | else { |
| 1951 | sname = pname; |
| 1952 | pname = NULL; |
| 1953 | } |
| 1954 | |
| 1955 | px = p; |
| 1956 | if (pname) { |
| 1957 | px = findproxy(pname, PR_CAP_BE); |
| 1958 | if (!px) { |
| 1959 | Alert("proxy %s: acl %s %s(): unable to find proxy '%s'.\n", |
| 1960 | p->id, acl->name, expr->kw->kw, pname); |
| 1961 | cfgerr++; |
| 1962 | continue; |
| 1963 | } |
| 1964 | } |
| 1965 | |
| 1966 | srv = findserver(px, sname); |
| 1967 | if (!srv) { |
| 1968 | Alert("proxy %s: acl %s %s(): unable to find server '%s'.\n", |
| 1969 | p->id, acl->name, expr->kw->kw, sname); |
| 1970 | cfgerr++; |
| 1971 | continue; |
| 1972 | } |
| 1973 | |
| 1974 | free(expr->arg.str); |
| 1975 | expr->arg_len = 0; |
| 1976 | expr->arg.srv = srv; |
| 1977 | continue; |
| 1978 | } |
| 1979 | |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1980 | if (strstr(expr->kw->kw, "http_auth") == expr->kw->kw) { |
| 1981 | |
| 1982 | if (!expr->arg.str || !*expr->arg.str) { |
| 1983 | Alert("proxy %s: acl %s %s(): missing userlist name.\n", |
| 1984 | p->id, acl->name, expr->kw->kw); |
| 1985 | cfgerr++; |
| 1986 | continue; |
| 1987 | } |
| 1988 | |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 1989 | if (p->uri_auth && p->uri_auth->userlist && |
| 1990 | !strcmp(p->uri_auth->userlist->name, expr->arg.str)) |
| 1991 | ul = p->uri_auth->userlist; |
| 1992 | else |
| 1993 | ul = auth_find_userlist(expr->arg.str); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1994 | |
| 1995 | if (!ul) { |
| 1996 | Alert("proxy %s: acl %s %s(%s): unable to find userlist.\n", |
| 1997 | p->id, acl->name, expr->kw->kw, expr->arg.str); |
| 1998 | cfgerr++; |
| 1999 | continue; |
| 2000 | } |
| 2001 | |
| 2002 | expr->arg_len = 0; |
| 2003 | expr->arg.ul = ul; |
| 2004 | } |
| 2005 | |
| 2006 | |
| 2007 | if (!strcmp(expr->kw->kw, "http_auth_group")) { |
| 2008 | |
| 2009 | if (LIST_ISEMPTY(&expr->patterns)) { |
| 2010 | Alert("proxy %s: acl %s %s(): no groups specified.\n", |
| 2011 | p->id, acl->name, expr->kw->kw); |
| 2012 | cfgerr++; |
| 2013 | continue; |
| 2014 | } |
| 2015 | |
| 2016 | list_for_each_entry(pattern, &expr->patterns, list) { |
| 2017 | pattern->val.group_mask = auth_resolve_groups(expr->arg.ul, pattern->ptr.str); |
| 2018 | |
| 2019 | free(pattern->ptr.str); |
| 2020 | pattern->ptr.str = NULL; |
| 2021 | pattern->len = 0; |
| 2022 | |
| 2023 | if (!pattern->val.group_mask) { |
| 2024 | Alert("proxy %s: acl %s %s(): invalid group(s).\n", |
| 2025 | p->id, acl->name, expr->kw->kw); |
| 2026 | cfgerr++; |
| 2027 | continue; |
| 2028 | } |
| 2029 | } |
| 2030 | } |
| 2031 | } |
| 2032 | } |
| 2033 | |
| 2034 | return cfgerr; |
| 2035 | } |
Willy Tarreau | dd64f8d | 2008-07-27 22:02:32 +0200 | [diff] [blame] | 2036 | |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 2037 | /************************************************************************/ |
| 2038 | /* All supported keywords must be declared here. */ |
| 2039 | /************************************************************************/ |
| 2040 | |
| 2041 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 2042 | static struct acl_kw_list acl_kws = {{ },{ |
Emeric Brun | 38e7176 | 2010-09-23 17:59:18 +0200 | [diff] [blame] | 2043 | { "always_true", acl_parse_nothing, acl_fetch_true, acl_match_nothing, ACL_USE_NOTHING }, |
| 2044 | { "always_false", acl_parse_nothing, acl_fetch_false, acl_match_nothing, ACL_USE_NOTHING }, |
| 2045 | { "wait_end", acl_parse_nothing, acl_fetch_wait_end, acl_match_nothing, ACL_USE_NOTHING }, |
| 2046 | { "req_len", acl_parse_int, acl_fetch_req_len, acl_match_int, ACL_USE_L6REQ_VOLATILE }, |
| 2047 | { "req_ssl_hello_type", acl_parse_int, acl_fetch_ssl_hello_type, acl_match_int, ACL_USE_L6REQ_VOLATILE }, |
| 2048 | { "rep_ssl_hello_type", acl_parse_int, acl_fetch_ssl_hello_type, acl_match_int, ACL_USE_L6RTR_VOLATILE }, |
| 2049 | { "req_ssl_ver", acl_parse_dotted_ver, acl_fetch_req_ssl_ver, acl_match_int, ACL_USE_L6REQ_VOLATILE }, |
| 2050 | { "req_rdp_cookie", acl_parse_str, acl_fetch_rdp_cookie, acl_match_str, ACL_USE_L6REQ_VOLATILE|ACL_MAY_LOOKUP }, |
| 2051 | { "req_rdp_cookie_cnt", acl_parse_int, acl_fetch_rdp_cookie_cnt, acl_match_int, ACL_USE_L6REQ_VOLATILE }, |
Willy Tarreau | b6672b5 | 2011-12-12 17:23:41 +0100 | [diff] [blame] | 2052 | { "req_ssl_sni", acl_parse_str, acl_fetch_ssl_hello_sni, acl_match_str, ACL_USE_L6REQ_VOLATILE|ACL_MAY_LOOKUP }, |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 2053 | #if 0 |
| 2054 | { "time", acl_parse_time, acl_fetch_time, acl_match_time }, |
| 2055 | #endif |
| 2056 | { NULL, NULL, NULL, NULL } |
| 2057 | }}; |
| 2058 | |
| 2059 | |
| 2060 | __attribute__((constructor)) |
| 2061 | static void __acl_init(void) |
| 2062 | { |
| 2063 | acl_register_keywords(&acl_kws); |
| 2064 | } |
| 2065 | |
| 2066 | |
| 2067 | /* |
| 2068 | * Local variables: |
| 2069 | * c-indent-level: 8 |
| 2070 | * c-basic-offset: 8 |
| 2071 | * End: |
| 2072 | */ |