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