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