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