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 | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 697 | if (strcmp(ptr, "eq") == 0) *opaque = 0; |
| 698 | else if (strcmp(ptr, "gt") == 0) *opaque = 1; |
| 699 | else if (strcmp(ptr, "ge") == 0) *opaque = 2; |
| 700 | else if (strcmp(ptr, "lt") == 0) *opaque = 3; |
| 701 | else if (strcmp(ptr, "le") == 0) *opaque = 4; |
| 702 | else |
| 703 | return 0; |
| 704 | |
| 705 | skip++; |
| 706 | ptr = text[skip]; |
| 707 | } |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 708 | |
| 709 | last = i = 0; |
| 710 | while (1) { |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 711 | j = *ptr++; |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 712 | if ((j == '-' || j == ':') && !last) { |
| 713 | last++; |
| 714 | pattern->val.range.min = i; |
| 715 | i = 0; |
| 716 | continue; |
| 717 | } |
| 718 | j -= '0'; |
| 719 | if (j > 9) |
| 720 | // also catches the terminating zero |
| 721 | break; |
| 722 | i *= 10; |
| 723 | i += j; |
| 724 | } |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 725 | |
| 726 | if (last && *opaque >= 1 && *opaque <= 4) |
| 727 | /* having a range with a min or a max is absurd */ |
| 728 | return 0; |
| 729 | |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 730 | if (!last) |
| 731 | pattern->val.range.min = i; |
| 732 | pattern->val.range.max = i; |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 733 | |
| 734 | switch (*opaque) { |
| 735 | case 0: /* eq */ |
| 736 | pattern->val.range.min_set = 1; |
| 737 | pattern->val.range.max_set = 1; |
| 738 | break; |
| 739 | case 1: /* gt */ |
| 740 | pattern->val.range.min++; /* gt = ge + 1 */ |
| 741 | case 2: /* ge */ |
| 742 | pattern->val.range.min_set = 1; |
| 743 | pattern->val.range.max_set = 0; |
| 744 | break; |
| 745 | case 3: /* lt */ |
| 746 | pattern->val.range.max--; /* lt = le - 1 */ |
| 747 | case 4: /* le */ |
| 748 | pattern->val.range.min_set = 0; |
| 749 | pattern->val.range.max_set = 1; |
| 750 | break; |
| 751 | } |
| 752 | return skip + 1; |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 753 | } |
| 754 | |
Willy Tarreau | 4a26d2f | 2008-07-15 16:05:33 +0200 | [diff] [blame] | 755 | /* Parse a range of positive 2-component versions delimited by either ':' or |
| 756 | * '-'. The version consists in a major and a minor, both of which must be |
| 757 | * smaller than 65536, because internally they will be represented as a 32-bit |
| 758 | * integer. |
| 759 | * If only one version is read, it is set as both min and max. Just like for |
| 760 | * pure integers, an operator may be specified as the prefix, among this list |
| 761 | * of 5 : |
| 762 | * |
| 763 | * 0:eq, 1:gt, 2:ge, 3:lt, 4:le |
| 764 | * |
| 765 | * The default operator is "eq". It supports range matching. Ranges are |
| 766 | * rejected for other operators. The operator may be changed at any time. |
| 767 | * The operator is stored in the 'opaque' argument. This allows constructs |
| 768 | * such as the following one : |
| 769 | * |
| 770 | * acl obsolete_ssl ssl_req_proto lt 3 |
| 771 | * acl unsupported_ssl ssl_req_proto gt 3.1 |
| 772 | * acl valid_ssl ssl_req_proto 3.0-3.1 |
| 773 | * |
| 774 | */ |
| 775 | int acl_parse_dotted_ver(const char **text, struct acl_pattern *pattern, int *opaque) |
| 776 | { |
| 777 | signed long long i; |
| 778 | unsigned int j, last, skip = 0; |
| 779 | const char *ptr = *text; |
| 780 | |
| 781 | |
| 782 | while (!isdigit((unsigned char)*ptr)) { |
| 783 | if (strcmp(ptr, "eq") == 0) *opaque = 0; |
| 784 | else if (strcmp(ptr, "gt") == 0) *opaque = 1; |
| 785 | else if (strcmp(ptr, "ge") == 0) *opaque = 2; |
| 786 | else if (strcmp(ptr, "lt") == 0) *opaque = 3; |
| 787 | else if (strcmp(ptr, "le") == 0) *opaque = 4; |
| 788 | else |
| 789 | return 0; |
| 790 | |
| 791 | skip++; |
| 792 | ptr = text[skip]; |
| 793 | } |
| 794 | |
| 795 | last = i = 0; |
| 796 | while (1) { |
| 797 | j = *ptr++; |
| 798 | if (j == '.') { |
| 799 | /* minor part */ |
| 800 | if (i >= 65536) |
| 801 | return 0; |
| 802 | i <<= 16; |
| 803 | continue; |
| 804 | } |
| 805 | if ((j == '-' || j == ':') && !last) { |
| 806 | last++; |
| 807 | if (i < 65536) |
| 808 | i <<= 16; |
| 809 | pattern->val.range.min = i; |
| 810 | i = 0; |
| 811 | continue; |
| 812 | } |
| 813 | j -= '0'; |
| 814 | if (j > 9) |
| 815 | // also catches the terminating zero |
| 816 | break; |
| 817 | i = (i & 0xFFFF0000) + (i & 0xFFFF) * 10; |
| 818 | i += j; |
| 819 | } |
| 820 | |
| 821 | /* if we only got a major version, let's shift it now */ |
| 822 | if (i < 65536) |
| 823 | i <<= 16; |
| 824 | |
| 825 | if (last && *opaque >= 1 && *opaque <= 4) |
| 826 | /* having a range with a min or a max is absurd */ |
| 827 | return 0; |
| 828 | |
| 829 | if (!last) |
| 830 | pattern->val.range.min = i; |
| 831 | pattern->val.range.max = i; |
| 832 | |
| 833 | switch (*opaque) { |
| 834 | case 0: /* eq */ |
| 835 | pattern->val.range.min_set = 1; |
| 836 | pattern->val.range.max_set = 1; |
| 837 | break; |
| 838 | case 1: /* gt */ |
| 839 | pattern->val.range.min++; /* gt = ge + 1 */ |
| 840 | case 2: /* ge */ |
| 841 | pattern->val.range.min_set = 1; |
| 842 | pattern->val.range.max_set = 0; |
| 843 | break; |
| 844 | case 3: /* lt */ |
| 845 | pattern->val.range.max--; /* lt = le - 1 */ |
| 846 | case 4: /* le */ |
| 847 | pattern->val.range.min_set = 0; |
| 848 | pattern->val.range.max_set = 1; |
| 849 | break; |
| 850 | } |
| 851 | return skip + 1; |
| 852 | } |
| 853 | |
Willy Tarreau | a67fad9 | 2007-05-08 19:50:09 +0200 | [diff] [blame] | 854 | /* Parse an IP address and an optional mask in the form addr[/mask]. |
| 855 | * The addr may either be an IPv4 address or a hostname. The mask |
| 856 | * may either be a dotted mask or a number of bits. Returns 1 if OK, |
| 857 | * otherwise 0. |
| 858 | */ |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 859 | 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] | 860 | { |
Willy Tarreau | b337b53 | 2010-05-13 20:03:41 +0200 | [diff] [blame] | 861 | struct eb_root *tree = NULL; |
| 862 | if (pattern->flags & ACL_PAT_F_TREE_OK) |
| 863 | tree = pattern->val.tree; |
| 864 | |
| 865 | if (str2net(*text, &pattern->val.ipv4.addr, &pattern->val.ipv4.mask)) { |
| 866 | unsigned int mask = ntohl(pattern->val.ipv4.mask.s_addr); |
| 867 | struct ebmb_node *node; |
| 868 | /* check if the mask is contiguous so that we can insert the |
| 869 | * network into the tree. A continuous mask has only ones on |
| 870 | * the left. This means that this mask + its lower bit added |
| 871 | * once again is null. |
| 872 | */ |
| 873 | if (mask + (mask & -mask) == 0 && tree) { |
| 874 | mask = mask ? 33 - flsnz(mask & -mask) : 0; /* equals cidr value */ |
| 875 | /* FIXME: insert <addr>/<mask> into the tree here */ |
| 876 | node = calloc(1, sizeof(*node) + 4); /* reserve 4 bytes for IPv4 address */ |
| 877 | if (!node) |
| 878 | return 0; |
| 879 | memcpy(node->key, &pattern->val.ipv4.addr, 4); /* network byte order */ |
| 880 | node->node.pfx = mask; |
| 881 | if (ebmb_insert_prefix(tree, node, 4) != node) |
| 882 | free(node); /* was a duplicate */ |
| 883 | pattern->flags |= ACL_PAT_F_TREE; |
| 884 | return 1; |
| 885 | } |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 886 | return 1; |
Willy Tarreau | b337b53 | 2010-05-13 20:03:41 +0200 | [diff] [blame] | 887 | } |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 888 | else |
| 889 | return 0; |
Willy Tarreau | a67fad9 | 2007-05-08 19:50:09 +0200 | [diff] [blame] | 890 | } |
| 891 | |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 892 | /* |
| 893 | * Registers the ACL keyword list <kwl> as a list of valid keywords for next |
| 894 | * parsing sessions. |
| 895 | */ |
| 896 | void acl_register_keywords(struct acl_kw_list *kwl) |
| 897 | { |
| 898 | LIST_ADDQ(&acl_keywords.list, &kwl->list); |
| 899 | } |
| 900 | |
| 901 | /* |
| 902 | * Unregisters the ACL keyword list <kwl> from the list of valid keywords. |
| 903 | */ |
| 904 | void acl_unregister_keywords(struct acl_kw_list *kwl) |
| 905 | { |
| 906 | LIST_DEL(&kwl->list); |
| 907 | LIST_INIT(&kwl->list); |
| 908 | } |
| 909 | |
| 910 | /* Return a pointer to the ACL <name> within the list starting at <head>, or |
| 911 | * NULL if not found. |
| 912 | */ |
| 913 | struct acl *find_acl_by_name(const char *name, struct list *head) |
| 914 | { |
| 915 | struct acl *acl; |
| 916 | list_for_each_entry(acl, head, list) { |
| 917 | if (strcmp(acl->name, name) == 0) |
| 918 | return acl; |
| 919 | } |
| 920 | return NULL; |
| 921 | } |
| 922 | |
| 923 | /* Return a pointer to the ACL keyword <kw>, or NULL if not found. Note that if |
| 924 | * <kw> contains an opening parenthesis, only the left part of it is checked. |
| 925 | */ |
| 926 | struct acl_keyword *find_acl_kw(const char *kw) |
| 927 | { |
| 928 | int index; |
| 929 | const char *kwend; |
| 930 | struct acl_kw_list *kwl; |
| 931 | |
| 932 | kwend = strchr(kw, '('); |
| 933 | if (!kwend) |
| 934 | kwend = kw + strlen(kw); |
| 935 | |
| 936 | list_for_each_entry(kwl, &acl_keywords.list, list) { |
| 937 | for (index = 0; kwl->kw[index].kw != NULL; index++) { |
| 938 | if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) && |
| 939 | kwl->kw[index].kw[kwend-kw] == 0) |
| 940 | return &kwl->kw[index]; |
| 941 | } |
| 942 | } |
| 943 | return NULL; |
| 944 | } |
| 945 | |
| 946 | static void free_pattern(struct acl_pattern *pat) |
| 947 | { |
Krzysztof Piotr Oledzki | 8001d61 | 2008-05-31 13:53:23 +0200 | [diff] [blame] | 948 | |
| 949 | if (pat->ptr.ptr) { |
| 950 | if (pat->freeptrbuf) |
| 951 | pat->freeptrbuf(pat->ptr.ptr); |
| 952 | |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 953 | free(pat->ptr.ptr); |
Krzysztof Piotr Oledzki | 8001d61 | 2008-05-31 13:53:23 +0200 | [diff] [blame] | 954 | } |
| 955 | |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 956 | free(pat); |
| 957 | } |
| 958 | |
| 959 | static void free_pattern_list(struct list *head) |
| 960 | { |
| 961 | struct acl_pattern *pat, *tmp; |
| 962 | list_for_each_entry_safe(pat, tmp, head, list) |
| 963 | free_pattern(pat); |
| 964 | } |
| 965 | |
Willy Tarreau | e56cda9 | 2010-05-11 23:25:05 +0200 | [diff] [blame] | 966 | static void free_pattern_tree(struct eb_root *root) |
| 967 | { |
| 968 | struct eb_node *node, *next; |
| 969 | node = eb_first(root); |
| 970 | while (node) { |
| 971 | next = eb_next(node); |
| 972 | free(node); |
| 973 | node = next; |
| 974 | } |
| 975 | } |
| 976 | |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 977 | static struct acl_expr *prune_acl_expr(struct acl_expr *expr) |
| 978 | { |
| 979 | free_pattern_list(&expr->patterns); |
Willy Tarreau | e56cda9 | 2010-05-11 23:25:05 +0200 | [diff] [blame] | 980 | free_pattern_tree(&expr->pattern_tree); |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 981 | LIST_INIT(&expr->patterns); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 982 | if (expr->arg_len && expr->arg.str) |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 983 | free(expr->arg.str); |
| 984 | expr->kw->use_cnt--; |
| 985 | return expr; |
| 986 | } |
| 987 | |
Willy Tarreau | 2b5285d | 2010-05-09 23:45:24 +0200 | [diff] [blame] | 988 | static int acl_read_patterns_from_file( struct acl_keyword *aclkw, |
| 989 | struct acl_expr *expr, |
| 990 | const char *filename, int patflags) |
| 991 | { |
| 992 | FILE *file; |
| 993 | char *c; |
| 994 | const char *args[2]; |
| 995 | struct acl_pattern *pattern; |
| 996 | int opaque; |
| 997 | |
| 998 | file = fopen(filename, "r"); |
| 999 | if (!file) |
| 1000 | return 0; |
| 1001 | |
| 1002 | /* now parse all patterns. The file may contain only one pattern per |
| 1003 | * line. If the line contains spaces, they will be part of the pattern. |
| 1004 | * The pattern stops at the first CR, LF or EOF encountered. |
| 1005 | */ |
| 1006 | opaque = 0; |
Willy Tarreau | e56cda9 | 2010-05-11 23:25:05 +0200 | [diff] [blame] | 1007 | pattern = NULL; |
Willy Tarreau | 2b5285d | 2010-05-09 23:45:24 +0200 | [diff] [blame] | 1008 | args[1] = ""; |
| 1009 | while (fgets(trash, sizeof(trash), file) != NULL) { |
| 1010 | |
| 1011 | c = trash; |
Willy Tarreau | 58215a0 | 2010-05-13 22:07:43 +0200 | [diff] [blame] | 1012 | |
| 1013 | /* ignore lines beginning with a dash */ |
| 1014 | if (*c == '#') |
| 1015 | continue; |
| 1016 | |
| 1017 | /* strip leading spaces and tabs */ |
| 1018 | while (*c == ' ' || *c == '\t') |
| 1019 | c++; |
| 1020 | |
| 1021 | /* empty lines are ignored too */ |
| 1022 | if (!*c) |
| 1023 | continue; |
| 1024 | |
| 1025 | args[0] = c; |
Willy Tarreau | 2b5285d | 2010-05-09 23:45:24 +0200 | [diff] [blame] | 1026 | while (*c && *c != '\n' && *c != '\r') |
| 1027 | c++; |
| 1028 | *c = 0; |
| 1029 | |
Willy Tarreau | e56cda9 | 2010-05-11 23:25:05 +0200 | [diff] [blame] | 1030 | /* we keep the previous pattern along iterations as long as it's not used */ |
| 1031 | if (!pattern) |
| 1032 | pattern = (struct acl_pattern *)malloc(sizeof(*pattern)); |
Willy Tarreau | 2b5285d | 2010-05-09 23:45:24 +0200 | [diff] [blame] | 1033 | if (!pattern) |
| 1034 | goto out_close; |
Willy Tarreau | e56cda9 | 2010-05-11 23:25:05 +0200 | [diff] [blame] | 1035 | |
| 1036 | memset(pattern, 0, sizeof(*pattern)); |
Willy Tarreau | 2b5285d | 2010-05-09 23:45:24 +0200 | [diff] [blame] | 1037 | pattern->flags = patflags; |
| 1038 | |
Willy Tarreau | e56cda9 | 2010-05-11 23:25:05 +0200 | [diff] [blame] | 1039 | if ((aclkw->requires & ACL_MAY_LOOKUP) && !(pattern->flags & ACL_PAT_F_IGNORE_CASE)) { |
| 1040 | /* we pre-set the data pointer to the tree's head so that functions |
| 1041 | * which are able to insert in a tree know where to do that. |
| 1042 | */ |
| 1043 | pattern->flags |= ACL_PAT_F_TREE_OK; |
| 1044 | pattern->val.tree = &expr->pattern_tree; |
| 1045 | } |
| 1046 | |
Willy Tarreau | 2b5285d | 2010-05-09 23:45:24 +0200 | [diff] [blame] | 1047 | if (!aclkw->parse(args, pattern, &opaque)) |
| 1048 | goto out_free_pattern; |
Willy Tarreau | e56cda9 | 2010-05-11 23:25:05 +0200 | [diff] [blame] | 1049 | |
| 1050 | /* if the parser did not feed the tree, let's chain the pattern to the list */ |
| 1051 | if (!(pattern->flags & ACL_PAT_F_TREE)) { |
| 1052 | LIST_ADDQ(&expr->patterns, &pattern->list); |
| 1053 | pattern = NULL; /* get a new one */ |
| 1054 | } |
Willy Tarreau | 2b5285d | 2010-05-09 23:45:24 +0200 | [diff] [blame] | 1055 | } |
Willy Tarreau | e56cda9 | 2010-05-11 23:25:05 +0200 | [diff] [blame] | 1056 | if (pattern) |
| 1057 | free_pattern(pattern); |
Willy Tarreau | 2b5285d | 2010-05-09 23:45:24 +0200 | [diff] [blame] | 1058 | return 1; |
| 1059 | |
| 1060 | out_free_pattern: |
| 1061 | free_pattern(pattern); |
| 1062 | out_close: |
| 1063 | fclose(file); |
| 1064 | return 0; |
| 1065 | } |
| 1066 | |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 1067 | /* Parse an ACL expression starting at <args>[0], and return it. |
| 1068 | * Right now, the only accepted syntax is : |
| 1069 | * <subject> [<value>...] |
| 1070 | */ |
| 1071 | struct acl_expr *parse_acl_expr(const char **args) |
| 1072 | { |
| 1073 | __label__ out_return, out_free_expr, out_free_pattern; |
| 1074 | struct acl_expr *expr; |
| 1075 | struct acl_keyword *aclkw; |
| 1076 | struct acl_pattern *pattern; |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 1077 | int opaque, patflags; |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 1078 | const char *arg; |
| 1079 | |
| 1080 | aclkw = find_acl_kw(args[0]); |
| 1081 | if (!aclkw || !aclkw->parse) |
| 1082 | goto out_return; |
| 1083 | |
| 1084 | expr = (struct acl_expr *)calloc(1, sizeof(*expr)); |
| 1085 | if (!expr) |
| 1086 | goto out_return; |
| 1087 | |
| 1088 | expr->kw = aclkw; |
| 1089 | aclkw->use_cnt++; |
| 1090 | LIST_INIT(&expr->patterns); |
Willy Tarreau | e56cda9 | 2010-05-11 23:25:05 +0200 | [diff] [blame] | 1091 | expr->pattern_tree = EB_ROOT_UNIQUE; |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 1092 | expr->arg.str = NULL; |
Willy Tarreau | bb76891 | 2007-06-10 11:17:01 +0200 | [diff] [blame] | 1093 | expr->arg_len = 0; |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 1094 | |
| 1095 | arg = strchr(args[0], '('); |
| 1096 | if (arg != NULL) { |
| 1097 | char *end, *arg2; |
| 1098 | /* there is an argument in the form "subject(arg)" */ |
| 1099 | arg++; |
| 1100 | end = strchr(arg, ')'); |
| 1101 | if (!end) |
| 1102 | goto out_free_expr; |
Willy Tarreau | ac778f5 | 2010-01-26 19:02:46 +0100 | [diff] [blame] | 1103 | arg2 = my_strndup(arg, end - arg); |
Willy Tarreau | 1edb144 | 2010-01-27 20:13:38 +0100 | [diff] [blame] | 1104 | if (!arg2) |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 1105 | goto out_free_expr; |
Willy Tarreau | bb76891 | 2007-06-10 11:17:01 +0200 | [diff] [blame] | 1106 | expr->arg_len = end - arg; |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 1107 | expr->arg.str = arg2; |
| 1108 | } |
| 1109 | |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 1110 | args++; |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 1111 | |
| 1112 | /* check for options before patterns. Supported options are : |
| 1113 | * -i : ignore case for all patterns by default |
| 1114 | * -f : read patterns from those files |
| 1115 | * -- : everything after this is not an option |
| 1116 | */ |
| 1117 | patflags = 0; |
| 1118 | while (**args == '-') { |
| 1119 | if ((*args)[1] == 'i') |
| 1120 | patflags |= ACL_PAT_F_IGNORE_CASE; |
Willy Tarreau | 2b5285d | 2010-05-09 23:45:24 +0200 | [diff] [blame] | 1121 | else if ((*args)[1] == 'f') { |
| 1122 | if (!acl_read_patterns_from_file(aclkw, expr, args[1], patflags | ACL_PAT_F_FROM_FILE)) |
| 1123 | goto out_free_expr; |
| 1124 | args++; |
| 1125 | } |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 1126 | else if ((*args)[1] == '-') { |
| 1127 | args++; |
| 1128 | break; |
| 1129 | } |
| 1130 | else |
| 1131 | break; |
| 1132 | args++; |
| 1133 | } |
| 1134 | |
| 1135 | /* now parse all patterns */ |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 1136 | opaque = 0; |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 1137 | while (**args) { |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 1138 | int ret; |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 1139 | pattern = (struct acl_pattern *)calloc(1, sizeof(*pattern)); |
| 1140 | if (!pattern) |
| 1141 | goto out_free_expr; |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 1142 | pattern->flags = patflags; |
| 1143 | |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 1144 | ret = aclkw->parse(args, pattern, &opaque); |
| 1145 | if (!ret) |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 1146 | goto out_free_pattern; |
| 1147 | LIST_ADDQ(&expr->patterns, &pattern->list); |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 1148 | args += ret; |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 1149 | } |
| 1150 | |
| 1151 | return expr; |
| 1152 | |
| 1153 | out_free_pattern: |
| 1154 | free_pattern(pattern); |
| 1155 | out_free_expr: |
| 1156 | prune_acl_expr(expr); |
| 1157 | free(expr); |
| 1158 | out_return: |
| 1159 | return NULL; |
| 1160 | } |
| 1161 | |
Krzysztof Piotr Oledzki | a643baf | 2008-05-29 23:53:44 +0200 | [diff] [blame] | 1162 | /* Purge everything in the acl <acl>, then return <acl>. */ |
| 1163 | struct acl *prune_acl(struct acl *acl) { |
| 1164 | |
| 1165 | struct acl_expr *expr, *exprb; |
| 1166 | |
| 1167 | free(acl->name); |
| 1168 | |
| 1169 | list_for_each_entry_safe(expr, exprb, &acl->expr, list) { |
| 1170 | LIST_DEL(&expr->list); |
| 1171 | prune_acl_expr(expr); |
| 1172 | free(expr); |
| 1173 | } |
| 1174 | |
| 1175 | return acl; |
| 1176 | } |
| 1177 | |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 1178 | /* Parse an ACL with the name starting at <args>[0], and with a list of already |
| 1179 | * 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] | 1180 | * A pointer to that ACL is returned. If the ACL has an empty name, then it's |
| 1181 | * 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] | 1182 | * |
| 1183 | * args syntax: <aclname> <acl_expr> |
| 1184 | */ |
| 1185 | struct acl *parse_acl(const char **args, struct list *known_acl) |
| 1186 | { |
| 1187 | __label__ out_return, out_free_acl_expr, out_free_name; |
| 1188 | struct acl *cur_acl; |
| 1189 | struct acl_expr *acl_expr; |
| 1190 | char *name; |
| 1191 | |
Willy Tarreau | 2a56c5e | 2010-03-15 16:13:29 +0100 | [diff] [blame] | 1192 | if (**args && invalid_char(*args)) |
Willy Tarreau | 2e74c3f | 2007-12-02 18:45:09 +0100 | [diff] [blame] | 1193 | goto out_return; |
| 1194 | |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 1195 | acl_expr = parse_acl_expr(args + 1); |
| 1196 | if (!acl_expr) |
| 1197 | goto out_return; |
| 1198 | |
Willy Tarreau | 404e8ab | 2009-07-26 19:40:40 +0200 | [diff] [blame] | 1199 | /* Check for args beginning with an opening parenthesis just after the |
| 1200 | * subject, as this is almost certainly a typo. Right now we can only |
| 1201 | * emit a warning, so let's do so. |
| 1202 | */ |
Krzysztof Piotr Oledzki | 4cdd831 | 2009-10-05 00:23:35 +0200 | [diff] [blame] | 1203 | if (!strchr(args[1], '(') && *args[2] == '(') |
Willy Tarreau | 404e8ab | 2009-07-26 19:40:40 +0200 | [diff] [blame] | 1204 | Warning("parsing acl '%s' :\n" |
| 1205 | " matching '%s' for pattern '%s' is likely a mistake and probably\n" |
| 1206 | " not what you want. Maybe you need to remove the extraneous space before '('.\n" |
| 1207 | " If you are really sure this is not an error, please insert '--' between the\n" |
| 1208 | " match and the pattern to make this warning message disappear.\n", |
| 1209 | args[0], args[1], args[2]); |
| 1210 | |
Willy Tarreau | 2a56c5e | 2010-03-15 16:13:29 +0100 | [diff] [blame] | 1211 | if (*args[0]) |
| 1212 | cur_acl = find_acl_by_name(args[0], known_acl); |
| 1213 | else |
| 1214 | cur_acl = NULL; |
| 1215 | |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 1216 | if (!cur_acl) { |
| 1217 | name = strdup(args[0]); |
| 1218 | if (!name) |
| 1219 | goto out_free_acl_expr; |
| 1220 | cur_acl = (struct acl *)calloc(1, sizeof(*cur_acl)); |
| 1221 | if (cur_acl == NULL) |
| 1222 | goto out_free_name; |
| 1223 | |
| 1224 | LIST_INIT(&cur_acl->expr); |
| 1225 | LIST_ADDQ(known_acl, &cur_acl->list); |
| 1226 | cur_acl->name = name; |
| 1227 | } |
| 1228 | |
Willy Tarreau | a980263 | 2008-07-25 19:13:19 +0200 | [diff] [blame] | 1229 | cur_acl->requires |= acl_expr->kw->requires; |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 1230 | LIST_ADDQ(&cur_acl->expr, &acl_expr->list); |
| 1231 | return cur_acl; |
| 1232 | |
| 1233 | out_free_name: |
| 1234 | free(name); |
| 1235 | out_free_acl_expr: |
| 1236 | prune_acl_expr(acl_expr); |
| 1237 | free(acl_expr); |
| 1238 | out_return: |
| 1239 | return NULL; |
| 1240 | } |
| 1241 | |
Willy Tarreau | 16fbe82 | 2007-06-17 11:54:31 +0200 | [diff] [blame] | 1242 | /* Some useful ACLs provided by default. Only those used are allocated. */ |
| 1243 | |
| 1244 | const struct { |
| 1245 | const char *name; |
| 1246 | const char *expr[4]; /* put enough for longest expression */ |
| 1247 | } default_acl_list[] = { |
Willy Tarreau | 58393e1 | 2008-07-20 10:39:22 +0200 | [diff] [blame] | 1248 | { .name = "TRUE", .expr = {"always_true",""}}, |
| 1249 | { .name = "FALSE", .expr = {"always_false",""}}, |
Willy Tarreau | 16fbe82 | 2007-06-17 11:54:31 +0200 | [diff] [blame] | 1250 | { .name = "LOCALHOST", .expr = {"src","127.0.0.1/8",""}}, |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1251 | { .name = "HTTP", .expr = {"req_proto_http",""}}, |
Willy Tarreau | 16fbe82 | 2007-06-17 11:54:31 +0200 | [diff] [blame] | 1252 | { .name = "HTTP_1.0", .expr = {"req_ver","1.0",""}}, |
| 1253 | { .name = "HTTP_1.1", .expr = {"req_ver","1.1",""}}, |
| 1254 | { .name = "METH_CONNECT", .expr = {"method","CONNECT",""}}, |
| 1255 | { .name = "METH_GET", .expr = {"method","GET","HEAD",""}}, |
| 1256 | { .name = "METH_HEAD", .expr = {"method","HEAD",""}}, |
| 1257 | { .name = "METH_OPTIONS", .expr = {"method","OPTIONS",""}}, |
| 1258 | { .name = "METH_POST", .expr = {"method","POST",""}}, |
| 1259 | { .name = "METH_TRACE", .expr = {"method","TRACE",""}}, |
| 1260 | { .name = "HTTP_URL_ABS", .expr = {"url_reg","^[^/:]*://",""}}, |
| 1261 | { .name = "HTTP_URL_SLASH", .expr = {"url_beg","/",""}}, |
| 1262 | { .name = "HTTP_URL_STAR", .expr = {"url","*",""}}, |
| 1263 | { .name = "HTTP_CONTENT", .expr = {"hdr_val(content-length)","gt","0",""}}, |
Emeric Brun | bede3d0 | 2009-06-30 17:54:00 +0200 | [diff] [blame] | 1264 | { .name = "RDP_COOKIE", .expr = {"req_rdp_cookie_cnt","gt","0",""}}, |
Willy Tarreau | c631770 | 2008-07-20 09:29:50 +0200 | [diff] [blame] | 1265 | { .name = "REQ_CONTENT", .expr = {"req_len","gt","0",""}}, |
Willy Tarreau | b6fb420 | 2008-07-20 11:18:28 +0200 | [diff] [blame] | 1266 | { .name = "WAIT_END", .expr = {"wait_end",""}}, |
Willy Tarreau | 16fbe82 | 2007-06-17 11:54:31 +0200 | [diff] [blame] | 1267 | { .name = NULL, .expr = {""}} |
| 1268 | }; |
| 1269 | |
| 1270 | /* Find a default ACL from the default_acl list, compile it and return it. |
| 1271 | * If the ACL is not found, NULL is returned. In theory, it cannot fail, |
| 1272 | * except when default ACLs are broken, in which case it will return NULL. |
| 1273 | * If <known_acl> is not NULL, the ACL will be queued at its tail. |
| 1274 | */ |
| 1275 | struct acl *find_acl_default(const char *acl_name, struct list *known_acl) |
| 1276 | { |
| 1277 | __label__ out_return, out_free_acl_expr, out_free_name; |
| 1278 | struct acl *cur_acl; |
| 1279 | struct acl_expr *acl_expr; |
| 1280 | char *name; |
| 1281 | int index; |
| 1282 | |
| 1283 | for (index = 0; default_acl_list[index].name != NULL; index++) { |
| 1284 | if (strcmp(acl_name, default_acl_list[index].name) == 0) |
| 1285 | break; |
| 1286 | } |
| 1287 | |
| 1288 | if (default_acl_list[index].name == NULL) |
| 1289 | return NULL; |
| 1290 | |
| 1291 | acl_expr = parse_acl_expr((const char **)default_acl_list[index].expr); |
| 1292 | if (!acl_expr) |
| 1293 | goto out_return; |
| 1294 | |
| 1295 | name = strdup(acl_name); |
| 1296 | if (!name) |
| 1297 | goto out_free_acl_expr; |
| 1298 | cur_acl = (struct acl *)calloc(1, sizeof(*cur_acl)); |
| 1299 | if (cur_acl == NULL) |
| 1300 | goto out_free_name; |
| 1301 | |
| 1302 | cur_acl->name = name; |
Willy Tarreau | a55b7dc | 2009-07-12 09:21:30 +0200 | [diff] [blame] | 1303 | cur_acl->requires |= acl_expr->kw->requires; |
Willy Tarreau | 16fbe82 | 2007-06-17 11:54:31 +0200 | [diff] [blame] | 1304 | LIST_INIT(&cur_acl->expr); |
| 1305 | LIST_ADDQ(&cur_acl->expr, &acl_expr->list); |
| 1306 | if (known_acl) |
| 1307 | LIST_ADDQ(known_acl, &cur_acl->list); |
| 1308 | |
| 1309 | return cur_acl; |
| 1310 | |
| 1311 | out_free_name: |
| 1312 | free(name); |
| 1313 | out_free_acl_expr: |
| 1314 | prune_acl_expr(acl_expr); |
| 1315 | free(acl_expr); |
| 1316 | out_return: |
| 1317 | return NULL; |
| 1318 | } |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 1319 | |
| 1320 | /* Purge everything in the acl_cond <cond>, then return <cond>. */ |
| 1321 | struct acl_cond *prune_acl_cond(struct acl_cond *cond) |
| 1322 | { |
| 1323 | struct acl_term_suite *suite, *tmp_suite; |
| 1324 | struct acl_term *term, *tmp_term; |
| 1325 | |
| 1326 | /* iterate through all term suites and free all terms and all suites */ |
| 1327 | list_for_each_entry_safe(suite, tmp_suite, &cond->suites, list) { |
| 1328 | list_for_each_entry_safe(term, tmp_term, &suite->terms, list) |
| 1329 | free(term); |
| 1330 | free(suite); |
| 1331 | } |
| 1332 | return cond; |
| 1333 | } |
| 1334 | |
| 1335 | /* Parse an ACL condition starting at <args>[0], relying on a list of already |
| 1336 | * known ACLs passed in <known_acl>. The new condition is returned (or NULL in |
| 1337 | * case of low memory). Supports multiple conditions separated by "or". |
| 1338 | */ |
| 1339 | struct acl_cond *parse_acl_cond(const char **args, struct list *known_acl, int pol) |
| 1340 | { |
| 1341 | __label__ out_return, out_free_suite, out_free_term; |
Willy Tarreau | 74b98a8 | 2007-06-16 19:35:18 +0200 | [diff] [blame] | 1342 | int arg, neg; |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 1343 | const char *word; |
| 1344 | struct acl *cur_acl; |
| 1345 | struct acl_term *cur_term; |
| 1346 | struct acl_term_suite *cur_suite; |
| 1347 | struct acl_cond *cond; |
| 1348 | |
| 1349 | cond = (struct acl_cond *)calloc(1, sizeof(*cond)); |
| 1350 | if (cond == NULL) |
| 1351 | goto out_return; |
| 1352 | |
| 1353 | LIST_INIT(&cond->list); |
| 1354 | LIST_INIT(&cond->suites); |
| 1355 | cond->pol = pol; |
| 1356 | |
| 1357 | cur_suite = NULL; |
Willy Tarreau | 74b98a8 | 2007-06-16 19:35:18 +0200 | [diff] [blame] | 1358 | neg = 0; |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 1359 | for (arg = 0; *args[arg]; arg++) { |
| 1360 | word = args[arg]; |
| 1361 | |
| 1362 | /* remove as many exclamation marks as we can */ |
| 1363 | while (*word == '!') { |
| 1364 | neg = !neg; |
| 1365 | word++; |
| 1366 | } |
| 1367 | |
| 1368 | /* an empty word is allowed because we cannot force the user to |
| 1369 | * always think about not leaving exclamation marks alone. |
| 1370 | */ |
| 1371 | if (!*word) |
| 1372 | continue; |
| 1373 | |
Willy Tarreau | 16fbe82 | 2007-06-17 11:54:31 +0200 | [diff] [blame] | 1374 | if (strcasecmp(word, "or") == 0 || strcmp(word, "||") == 0) { |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 1375 | /* new term suite */ |
| 1376 | cur_suite = NULL; |
| 1377 | neg = 0; |
| 1378 | continue; |
| 1379 | } |
| 1380 | |
Willy Tarreau | 95fa469 | 2010-02-01 13:05:50 +0100 | [diff] [blame] | 1381 | if (strcmp(word, "{") == 0) { |
| 1382 | /* we may have a complete ACL expression between two braces, |
| 1383 | * find the last one. |
| 1384 | */ |
| 1385 | int arg_end = arg + 1; |
| 1386 | const char **args_new; |
| 1387 | |
| 1388 | while (*args[arg_end] && strcmp(args[arg_end], "}") != 0) |
| 1389 | arg_end++; |
| 1390 | |
| 1391 | if (!*args[arg_end]) |
| 1392 | goto out_free_suite; |
| 1393 | |
| 1394 | args_new = calloc(1, (arg_end - arg + 1) * sizeof(*args_new)); |
| 1395 | if (!args_new) |
| 1396 | goto out_free_suite; |
| 1397 | |
Willy Tarreau | 2a56c5e | 2010-03-15 16:13:29 +0100 | [diff] [blame] | 1398 | args_new[0] = ""; |
Willy Tarreau | 95fa469 | 2010-02-01 13:05:50 +0100 | [diff] [blame] | 1399 | memcpy(args_new + 1, args + arg + 1, (arg_end - arg) * sizeof(*args_new)); |
| 1400 | args_new[arg_end - arg] = ""; |
| 1401 | cur_acl = parse_acl(args_new, known_acl); |
| 1402 | free(args_new); |
| 1403 | |
| 1404 | if (!cur_acl) |
Willy Tarreau | 16fbe82 | 2007-06-17 11:54:31 +0200 | [diff] [blame] | 1405 | goto out_free_suite; |
Willy Tarreau | 95fa469 | 2010-02-01 13:05:50 +0100 | [diff] [blame] | 1406 | arg = arg_end; |
| 1407 | } |
| 1408 | else { |
| 1409 | /* search for <word> in the known ACL names. If we do not find |
| 1410 | * it, let's look for it in the default ACLs, and if found, add |
| 1411 | * it to the list of ACLs of this proxy. This makes it possible |
| 1412 | * to override them. |
| 1413 | */ |
| 1414 | cur_acl = find_acl_by_name(word, known_acl); |
| 1415 | if (cur_acl == NULL) { |
| 1416 | cur_acl = find_acl_default(word, known_acl); |
| 1417 | if (cur_acl == NULL) |
| 1418 | goto out_free_suite; |
| 1419 | } |
Willy Tarreau | 16fbe82 | 2007-06-17 11:54:31 +0200 | [diff] [blame] | 1420 | } |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 1421 | |
| 1422 | cur_term = (struct acl_term *)calloc(1, sizeof(*cur_term)); |
| 1423 | if (cur_term == NULL) |
| 1424 | goto out_free_suite; |
| 1425 | |
| 1426 | cur_term->acl = cur_acl; |
| 1427 | cur_term->neg = neg; |
Willy Tarreau | a980263 | 2008-07-25 19:13:19 +0200 | [diff] [blame] | 1428 | cond->requires |= cur_acl->requires; |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 1429 | |
| 1430 | if (!cur_suite) { |
| 1431 | cur_suite = (struct acl_term_suite *)calloc(1, sizeof(*cur_suite)); |
| 1432 | if (cur_term == NULL) |
| 1433 | goto out_free_term; |
| 1434 | LIST_INIT(&cur_suite->terms); |
| 1435 | LIST_ADDQ(&cond->suites, &cur_suite->list); |
| 1436 | } |
| 1437 | LIST_ADDQ(&cur_suite->terms, &cur_term->list); |
Willy Tarreau | 74b98a8 | 2007-06-16 19:35:18 +0200 | [diff] [blame] | 1438 | neg = 0; |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 1439 | } |
| 1440 | |
| 1441 | return cond; |
| 1442 | |
| 1443 | out_free_term: |
| 1444 | free(cur_term); |
| 1445 | out_free_suite: |
| 1446 | prune_acl_cond(cond); |
| 1447 | free(cond); |
| 1448 | out_return: |
| 1449 | return NULL; |
| 1450 | } |
| 1451 | |
Willy Tarreau | 2bbba41 | 2010-01-28 16:48:33 +0100 | [diff] [blame] | 1452 | /* Builds an ACL condition starting at the if/unless keyword. The complete |
| 1453 | * condition is returned. NULL is returned in case of error or if the first |
| 1454 | * word is neither "if" nor "unless". It automatically sets the file name and |
| 1455 | * the line number in the condition for better error reporting, and adds the |
| 1456 | * ACL requirements to the proxy's acl_requires. |
| 1457 | */ |
| 1458 | struct acl_cond *build_acl_cond(const char *file, int line, struct proxy *px, const char **args) |
| 1459 | { |
| 1460 | int pol = ACL_COND_NONE; |
| 1461 | struct acl_cond *cond = NULL; |
| 1462 | |
| 1463 | if (!strcmp(*args, "if")) { |
| 1464 | pol = ACL_COND_IF; |
| 1465 | args++; |
| 1466 | } |
| 1467 | else if (!strcmp(*args, "unless")) { |
| 1468 | pol = ACL_COND_UNLESS; |
| 1469 | args++; |
| 1470 | } |
| 1471 | else |
| 1472 | return NULL; |
| 1473 | |
| 1474 | cond = parse_acl_cond(args, &px->acl, pol); |
| 1475 | if (!cond) |
| 1476 | return NULL; |
| 1477 | |
| 1478 | cond->file = file; |
| 1479 | cond->line = line; |
| 1480 | px->acl_requires |= cond->requires; |
| 1481 | |
| 1482 | return cond; |
| 1483 | } |
| 1484 | |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 1485 | /* 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] | 1486 | * ACL_PAT_PASS depending on the test results. ACL_PAT_MISS may only be |
| 1487 | * returned if <dir> contains ACL_PARTIAL, indicating that incomplete data |
| 1488 | * is being examined. |
| 1489 | * This function only computes the condition, it does not apply the polarity |
| 1490 | * required by IF/UNLESS, it's up to the caller to do this using something like |
| 1491 | * this : |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 1492 | * |
| 1493 | * res = acl_pass(res); |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1494 | * if (res == ACL_PAT_MISS) |
| 1495 | * return 0; |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 1496 | * if (cond->pol == ACL_COND_UNLESS) |
| 1497 | * res = !res; |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 1498 | */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 1499 | 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] | 1500 | { |
| 1501 | __label__ fetch_next; |
| 1502 | struct acl_term_suite *suite; |
| 1503 | struct acl_term *term; |
| 1504 | struct acl_expr *expr; |
| 1505 | struct acl *acl; |
| 1506 | struct acl_pattern *pattern; |
| 1507 | struct acl_test test; |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 1508 | int acl_res, suite_res, cond_res; |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 1509 | |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 1510 | /* We're doing a logical OR between conditions so we initialize to FAIL. |
| 1511 | * The MISS status is propagated down from the suites. |
| 1512 | */ |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 1513 | cond_res = ACL_PAT_FAIL; |
| 1514 | list_for_each_entry(suite, &cond->suites, list) { |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 1515 | /* Evaluate condition suite <suite>. We stop at the first term |
| 1516 | * which returns ACL_PAT_FAIL. The MISS status is still propagated |
| 1517 | * in case of uncertainty in the result. |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 1518 | */ |
| 1519 | |
| 1520 | /* we're doing a logical AND between terms, so we must set the |
| 1521 | * initial value to PASS. |
| 1522 | */ |
| 1523 | suite_res = ACL_PAT_PASS; |
| 1524 | list_for_each_entry(term, &suite->terms, list) { |
| 1525 | acl = term->acl; |
| 1526 | |
| 1527 | /* FIXME: use cache ! |
| 1528 | * check acl->cache_idx for this. |
| 1529 | */ |
| 1530 | |
| 1531 | /* ACL result not cached. Let's scan all the expressions |
| 1532 | * and use the first one to match. |
| 1533 | */ |
| 1534 | acl_res = ACL_PAT_FAIL; |
| 1535 | list_for_each_entry(expr, &acl->expr, list) { |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 1536 | /* we need to reset context and flags */ |
| 1537 | memset(&test, 0, sizeof(test)); |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 1538 | fetch_next: |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1539 | if (!expr->kw->fetch(px, l4, l7, dir, expr, &test)) { |
| 1540 | /* maybe we could not fetch because of missing data */ |
| 1541 | if (test.flags & ACL_TEST_F_MAY_CHANGE && dir & ACL_PARTIAL) |
| 1542 | acl_res |= ACL_PAT_MISS; |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 1543 | continue; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1544 | } |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 1545 | |
Willy Tarreau | a79534f | 2008-07-20 10:13:37 +0200 | [diff] [blame] | 1546 | if (test.flags & ACL_TEST_F_RES_SET) { |
| 1547 | if (test.flags & ACL_TEST_F_RES_PASS) |
| 1548 | acl_res |= ACL_PAT_PASS; |
| 1549 | else |
| 1550 | acl_res |= ACL_PAT_FAIL; |
| 1551 | } |
| 1552 | else { |
Willy Tarreau | 020534d | 2010-05-16 21:45:45 +0200 | [diff] [blame] | 1553 | if (!eb_is_empty(&expr->pattern_tree)) { |
Willy Tarreau | c426296 | 2010-05-10 23:42:40 +0200 | [diff] [blame] | 1554 | /* a tree is present, let's check what type it is */ |
| 1555 | if (expr->kw->match == acl_match_str) |
| 1556 | 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] | 1557 | else if (expr->kw->match == acl_match_ip) |
| 1558 | 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] | 1559 | } |
| 1560 | |
Willy Tarreau | a79534f | 2008-07-20 10:13:37 +0200 | [diff] [blame] | 1561 | /* call the match() function for all tests on this value */ |
| 1562 | list_for_each_entry(pattern, &expr->patterns, list) { |
Willy Tarreau | a79534f | 2008-07-20 10:13:37 +0200 | [diff] [blame] | 1563 | if (acl_res == ACL_PAT_PASS) |
| 1564 | break; |
Willy Tarreau | e56cda9 | 2010-05-11 23:25:05 +0200 | [diff] [blame] | 1565 | acl_res |= expr->kw->match(&test, pattern); |
Willy Tarreau | a79534f | 2008-07-20 10:13:37 +0200 | [diff] [blame] | 1566 | } |
Krzysztof Piotr Oledzki | d7528e5 | 2010-01-29 17:55:53 +0100 | [diff] [blame] | 1567 | |
Willy Tarreau | e56cda9 | 2010-05-11 23:25:05 +0200 | [diff] [blame] | 1568 | if ((test.flags & ACL_TEST_F_NULL_MATCH) && |
Willy Tarreau | 020534d | 2010-05-16 21:45:45 +0200 | [diff] [blame] | 1569 | LIST_ISEMPTY(&expr->patterns) && eb_is_empty(&expr->pattern_tree)) |
Krzysztof Piotr Oledzki | d7528e5 | 2010-01-29 17:55:53 +0100 | [diff] [blame] | 1570 | acl_res |= expr->kw->match(&test, NULL); |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 1571 | } |
| 1572 | /* |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 1573 | * OK now acl_res holds the result of this expression |
| 1574 | * 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] | 1575 | * |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 1576 | * Then if (!MISS) we can cache the result, and put |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 1577 | * (test.flags & ACL_TEST_F_VOLATILE) in the cache flags. |
| 1578 | * |
| 1579 | * FIXME: implement cache. |
| 1580 | * |
| 1581 | */ |
| 1582 | |
| 1583 | /* now we may have some cleanup to do */ |
| 1584 | if (test.flags & ACL_TEST_F_MUST_FREE) { |
| 1585 | free(test.ptr); |
| 1586 | test.len = 0; |
| 1587 | } |
| 1588 | |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 1589 | /* we're ORing these terms, so a single PASS is enough */ |
| 1590 | if (acl_res == ACL_PAT_PASS) |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 1591 | break; |
| 1592 | |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 1593 | if (test.flags & ACL_TEST_F_FETCH_MORE) |
| 1594 | goto fetch_next; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1595 | |
| 1596 | /* sometimes we know the fetched data is subject to change |
| 1597 | * later and give another chance for a new match (eg: request |
| 1598 | * size, time, ...) |
| 1599 | */ |
| 1600 | if (test.flags & ACL_TEST_F_MAY_CHANGE && dir & ACL_PARTIAL) |
| 1601 | acl_res |= ACL_PAT_MISS; |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 1602 | } |
| 1603 | /* |
| 1604 | * Here we have the result of an ACL (cached or not). |
| 1605 | * ACLs are combined, negated or not, to form conditions. |
| 1606 | */ |
| 1607 | |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 1608 | if (term->neg) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 1609 | acl_res = acl_neg(acl_res); |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 1610 | |
| 1611 | suite_res &= acl_res; |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 1612 | |
| 1613 | /* we're ANDing these terms, so a single FAIL is enough */ |
| 1614 | if (suite_res == ACL_PAT_FAIL) |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 1615 | break; |
| 1616 | } |
| 1617 | cond_res |= suite_res; |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 1618 | |
| 1619 | /* we're ORing these terms, so a single PASS is enough */ |
| 1620 | if (cond_res == ACL_PAT_PASS) |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 1621 | break; |
| 1622 | } |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 1623 | return cond_res; |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 1624 | } |
| 1625 | |
| 1626 | |
Willy Tarreau | dd64f8d | 2008-07-27 22:02:32 +0200 | [diff] [blame] | 1627 | /* Reports a pointer to the first ACL used in condition <cond> which requires |
| 1628 | * at least one of the USE_FLAGS in <require>. Returns NULL if none matches. |
| 1629 | * The construct is almost the same as for acl_exec_cond() since we're walking |
| 1630 | * down the ACL tree as well. It is important that the tree is really walked |
| 1631 | * through and never cached, because that way, this function can be used as a |
| 1632 | * late check. |
| 1633 | */ |
Willy Tarreau | f1e98b8 | 2010-01-28 17:59:39 +0100 | [diff] [blame] | 1634 | 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] | 1635 | { |
| 1636 | struct acl_term_suite *suite; |
| 1637 | struct acl_term *term; |
| 1638 | struct acl *acl; |
| 1639 | |
| 1640 | list_for_each_entry(suite, &cond->suites, list) { |
| 1641 | list_for_each_entry(term, &suite->terms, list) { |
| 1642 | acl = term->acl; |
| 1643 | if (acl->requires & require) |
| 1644 | return acl; |
| 1645 | } |
| 1646 | } |
| 1647 | return NULL; |
| 1648 | } |
| 1649 | |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1650 | /* |
| 1651 | * Find targets for userlist and groups in acl. Function returns the number |
| 1652 | * of errors or OK if everything is fine. |
| 1653 | */ |
| 1654 | int |
| 1655 | acl_find_targets(struct proxy *p) |
| 1656 | { |
| 1657 | |
| 1658 | struct acl *acl; |
| 1659 | struct acl_expr *expr; |
| 1660 | struct acl_pattern *pattern; |
| 1661 | struct userlist *ul; |
| 1662 | int cfgerr = 0; |
| 1663 | |
| 1664 | list_for_each_entry(acl, &p->acl, list) { |
| 1665 | list_for_each_entry(expr, &acl->expr, list) { |
Willy Tarreau | 0b1cd94 | 2010-05-16 22:18:27 +0200 | [diff] [blame] | 1666 | if (strcmp(expr->kw->kw, "srv_is_up") == 0) { |
| 1667 | struct proxy *px; |
| 1668 | struct server *srv; |
| 1669 | char *pname, *sname; |
| 1670 | |
| 1671 | if (!expr->arg.str || !*expr->arg.str) { |
| 1672 | Alert("proxy %s: acl %s %s(): missing server name.\n", |
| 1673 | p->id, acl->name, expr->kw->kw); |
| 1674 | cfgerr++; |
| 1675 | continue; |
| 1676 | } |
| 1677 | |
| 1678 | pname = expr->arg.str; |
| 1679 | sname = strrchr(pname, '/'); |
| 1680 | |
| 1681 | if (sname) |
| 1682 | *sname++ = '\0'; |
| 1683 | else { |
| 1684 | sname = pname; |
| 1685 | pname = NULL; |
| 1686 | } |
| 1687 | |
| 1688 | px = p; |
| 1689 | if (pname) { |
| 1690 | px = findproxy(pname, PR_CAP_BE); |
| 1691 | if (!px) { |
| 1692 | Alert("proxy %s: acl %s %s(): unable to find proxy '%s'.\n", |
| 1693 | p->id, acl->name, expr->kw->kw, pname); |
| 1694 | cfgerr++; |
| 1695 | continue; |
| 1696 | } |
| 1697 | } |
| 1698 | |
| 1699 | srv = findserver(px, sname); |
| 1700 | if (!srv) { |
| 1701 | Alert("proxy %s: acl %s %s(): unable to find server '%s'.\n", |
| 1702 | p->id, acl->name, expr->kw->kw, sname); |
| 1703 | cfgerr++; |
| 1704 | continue; |
| 1705 | } |
| 1706 | |
| 1707 | free(expr->arg.str); |
| 1708 | expr->arg_len = 0; |
| 1709 | expr->arg.srv = srv; |
| 1710 | continue; |
| 1711 | } |
| 1712 | |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1713 | if (strstr(expr->kw->kw, "http_auth") == expr->kw->kw) { |
| 1714 | |
| 1715 | if (!expr->arg.str || !*expr->arg.str) { |
| 1716 | Alert("proxy %s: acl %s %s(): missing userlist name.\n", |
| 1717 | p->id, acl->name, expr->kw->kw); |
| 1718 | cfgerr++; |
| 1719 | continue; |
| 1720 | } |
| 1721 | |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 1722 | if (p->uri_auth && p->uri_auth->userlist && |
| 1723 | !strcmp(p->uri_auth->userlist->name, expr->arg.str)) |
| 1724 | ul = p->uri_auth->userlist; |
| 1725 | else |
| 1726 | ul = auth_find_userlist(expr->arg.str); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1727 | |
| 1728 | if (!ul) { |
| 1729 | Alert("proxy %s: acl %s %s(%s): unable to find userlist.\n", |
| 1730 | p->id, acl->name, expr->kw->kw, expr->arg.str); |
| 1731 | cfgerr++; |
| 1732 | continue; |
| 1733 | } |
| 1734 | |
| 1735 | expr->arg_len = 0; |
| 1736 | expr->arg.ul = ul; |
| 1737 | } |
| 1738 | |
| 1739 | |
| 1740 | if (!strcmp(expr->kw->kw, "http_auth_group")) { |
| 1741 | |
| 1742 | if (LIST_ISEMPTY(&expr->patterns)) { |
| 1743 | Alert("proxy %s: acl %s %s(): no groups specified.\n", |
| 1744 | p->id, acl->name, expr->kw->kw); |
| 1745 | cfgerr++; |
| 1746 | continue; |
| 1747 | } |
| 1748 | |
| 1749 | list_for_each_entry(pattern, &expr->patterns, list) { |
| 1750 | pattern->val.group_mask = auth_resolve_groups(expr->arg.ul, pattern->ptr.str); |
| 1751 | |
| 1752 | free(pattern->ptr.str); |
| 1753 | pattern->ptr.str = NULL; |
| 1754 | pattern->len = 0; |
| 1755 | |
| 1756 | if (!pattern->val.group_mask) { |
| 1757 | Alert("proxy %s: acl %s %s(): invalid group(s).\n", |
| 1758 | p->id, acl->name, expr->kw->kw); |
| 1759 | cfgerr++; |
| 1760 | continue; |
| 1761 | } |
| 1762 | } |
| 1763 | } |
| 1764 | } |
| 1765 | } |
| 1766 | |
| 1767 | return cfgerr; |
| 1768 | } |
Willy Tarreau | dd64f8d | 2008-07-27 22:02:32 +0200 | [diff] [blame] | 1769 | |
Willy Tarreau | a84d374 | 2007-05-07 00:36:48 +0200 | [diff] [blame] | 1770 | /************************************************************************/ |
| 1771 | /* All supported keywords must be declared here. */ |
| 1772 | /************************************************************************/ |
| 1773 | |
| 1774 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 1775 | static struct acl_kw_list acl_kws = {{ },{ |
Willy Tarreau | 44b90cc | 2010-05-24 20:27:29 +0200 | [diff] [blame] | 1776 | { "always_true", acl_parse_nothing, acl_fetch_true, acl_match_nothing, ACL_USE_NOTHING }, |
| 1777 | { "always_false", acl_parse_nothing, acl_fetch_false, acl_match_nothing, ACL_USE_NOTHING }, |
| 1778 | { "wait_end", acl_parse_nothing, acl_fetch_wait_end, acl_match_nothing, ACL_USE_NOTHING }, |
| 1779 | { "req_len", acl_parse_int, acl_fetch_req_len, acl_match_int, ACL_USE_L6REQ_VOLATILE }, |
| 1780 | { "req_ssl_ver", acl_parse_dotted_ver, acl_fetch_req_ssl_ver, acl_match_int, ACL_USE_L6REQ_VOLATILE }, |
| 1781 | { "req_rdp_cookie", acl_parse_str, acl_fetch_rdp_cookie, acl_match_str, ACL_USE_L6REQ_VOLATILE|ACL_MAY_LOOKUP }, |
| 1782 | { "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] | 1783 | #if 0 |
| 1784 | { "time", acl_parse_time, acl_fetch_time, acl_match_time }, |
| 1785 | #endif |
| 1786 | { NULL, NULL, NULL, NULL } |
| 1787 | }}; |
| 1788 | |
| 1789 | |
| 1790 | __attribute__((constructor)) |
| 1791 | static void __acl_init(void) |
| 1792 | { |
| 1793 | acl_register_keywords(&acl_kws); |
| 1794 | } |
| 1795 | |
| 1796 | |
| 1797 | /* |
| 1798 | * Local variables: |
| 1799 | * c-indent-level: 8 |
| 1800 | * c-basic-offset: 8 |
| 1801 | * End: |
| 1802 | */ |