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