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