blob: 3e04874f2a7d9b8d7b114eb57f6e48c979e6af87 [file] [log] [blame]
Willy Tarreaua84d3742007-05-07 00:36:48 +02001/*
2 * ACL management functions.
3 *
Willy Tarreaud4c33c82013-01-07 21:59:07 +01004 * Copyright 2000-2013 Willy Tarreau <w@1wt.eu>
Willy Tarreaua84d3742007-05-07 00:36:48 +02005 *
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 Tarreauae8b7962007-06-09 23:10:04 +020013#include <ctype.h>
Willy Tarreaua84d3742007-05-07 00:36:48 +020014#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 Oledzki8c8bd452010-01-29 19:29:32 +010020#include <common/uri_auth.h>
Willy Tarreaua84d3742007-05-07 00:36:48 +020021
Willy Tarreau2b5285d2010-05-09 23:45:24 +020022#include <types/global.h>
23
Willy Tarreaua84d3742007-05-07 00:36:48 +020024#include <proto/acl.h>
Willy Tarreau34db1082012-04-19 17:16:54 +020025#include <proto/arg.h>
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010026#include <proto/auth.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020027#include <proto/channel.h>
Willy Tarreau404e8ab2009-07-26 19:40:40 +020028#include <proto/log.h>
Thierry FOURNIERed66c292013-11-28 11:05:19 +010029#include <proto/pattern.h>
Willy Tarreau0b1cd942010-05-16 22:18:27 +020030#include <proto/proxy.h>
Willy Tarreau8ed669b2013-01-11 15:49:37 +010031#include <proto/sample.h>
Willy Tarreaud28c3532012-04-19 19:28:33 +020032#include <proto/stick_table.h>
Willy Tarreaua84d3742007-05-07 00:36:48 +020033
Willy Tarreauc4262962010-05-10 23:42:40 +020034#include <ebsttree.h>
35
Willy Tarreaua84d3742007-05-07 00:36:48 +020036/* List head of all known ACL keywords */
37static struct acl_kw_list acl_keywords = {
38 .list = LIST_HEAD_INIT(acl_keywords.list)
39};
40
Willy Tarreau0cba6072013-11-28 22:21:02 +010041/* input values are 0 or 3, output is the same */
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +010042static inline enum acl_test_res pat2acl(struct pattern *pat)
Willy Tarreau0cba6072013-11-28 22:21:02 +010043{
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +010044 if (pat)
45 return ACL_TEST_PASS;
46 else
47 return ACL_TEST_FAIL;
Willy Tarreau0cba6072013-11-28 22:21:02 +010048}
49
Willy Tarreaua5909832007-06-17 20:40:25 +020050/*
Willy Tarreaua84d3742007-05-07 00:36:48 +020051 * Registers the ACL keyword list <kwl> as a list of valid keywords for next
52 * parsing sessions.
53 */
54void acl_register_keywords(struct acl_kw_list *kwl)
55{
56 LIST_ADDQ(&acl_keywords.list, &kwl->list);
57}
58
59/*
60 * Unregisters the ACL keyword list <kwl> from the list of valid keywords.
61 */
62void acl_unregister_keywords(struct acl_kw_list *kwl)
63{
64 LIST_DEL(&kwl->list);
65 LIST_INIT(&kwl->list);
66}
67
68/* Return a pointer to the ACL <name> within the list starting at <head>, or
69 * NULL if not found.
70 */
71struct acl *find_acl_by_name(const char *name, struct list *head)
72{
73 struct acl *acl;
74 list_for_each_entry(acl, head, list) {
75 if (strcmp(acl->name, name) == 0)
76 return acl;
77 }
78 return NULL;
79}
80
81/* Return a pointer to the ACL keyword <kw>, or NULL if not found. Note that if
Willy Tarreau4bfa4222013-12-16 22:01:06 +010082 * <kw> contains an opening parenthesis or a comma, only the left part of it is
83 * checked.
Willy Tarreaua84d3742007-05-07 00:36:48 +020084 */
85struct acl_keyword *find_acl_kw(const char *kw)
86{
87 int index;
88 const char *kwend;
89 struct acl_kw_list *kwl;
90
Willy Tarreau4bfa4222013-12-16 22:01:06 +010091 kwend = kw;
92 while (*kwend && *kwend != '(' && *kwend != ',')
93 kwend++;
Willy Tarreaua84d3742007-05-07 00:36:48 +020094
95 list_for_each_entry(kwl, &acl_keywords.list, list) {
96 for (index = 0; kwl->kw[index].kw != NULL; index++) {
97 if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) &&
98 kwl->kw[index].kw[kwend-kw] == 0)
99 return &kwl->kw[index];
100 }
101 }
102 return NULL;
103}
104
Willy Tarreaua84d3742007-05-07 00:36:48 +0200105static struct acl_expr *prune_acl_expr(struct acl_expr *expr)
106{
Willy Tarreau34db1082012-04-19 17:16:54 +0200107 struct arg *arg;
108
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +0100109 pattern_prune(&expr->pat);
Willy Tarreau34db1082012-04-19 17:16:54 +0200110
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100111 for (arg = expr->smp->arg_p; arg; arg++) {
Willy Tarreau34db1082012-04-19 17:16:54 +0200112 if (arg->type == ARGT_STOP)
113 break;
Willy Tarreau496aa012012-06-01 10:38:29 +0200114 if (arg->type == ARGT_STR || arg->unresolved) {
Willy Tarreau34db1082012-04-19 17:16:54 +0200115 free(arg->data.str.str);
116 arg->data.str.str = NULL;
Willy Tarreau496aa012012-06-01 10:38:29 +0200117 arg->unresolved = 0;
Willy Tarreau34db1082012-04-19 17:16:54 +0200118 }
Willy Tarreau34db1082012-04-19 17:16:54 +0200119 }
120
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100121 if (expr->smp->arg_p != empty_arg_list)
122 free(expr->smp->arg_p);
Willy Tarreaua84d3742007-05-07 00:36:48 +0200123 return expr;
124}
125
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200126/* Parse an ACL expression starting at <args>[0], and return it. If <err> is
127 * not NULL, it will be filled with a pointer to an error message in case of
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200128 * error. This pointer must be freeable or NULL. <al> is an arg_list serving
129 * as a list head to report missing dependencies.
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200130 *
Willy Tarreaua84d3742007-05-07 00:36:48 +0200131 * Right now, the only accepted syntax is :
132 * <subject> [<value>...]
133 */
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +0100134struct acl_expr *parse_acl_expr(const char **args, char **err, struct arg_list *al,
135 const char *file, int line)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200136{
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100137 __label__ out_return, out_free_expr;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200138 struct acl_expr *expr;
139 struct acl_keyword *aclkw;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100140 int patflags;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200141 const char *arg;
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100142 struct sample_expr *smp = NULL;
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100143 int idx = 0;
144 char *ckw = NULL;
145 const char *begw;
146 const char *endw;
Willy Tarreau131b4662013-12-13 01:08:36 +0100147 const char *endt;
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100148 unsigned long prev_type;
149 int cur_type;
Willy Tarreau131b4662013-12-13 01:08:36 +0100150 int nbargs;
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100151 int operator = STD_OP_EQ;
152 int op;
153 int contain_colon, have_dot;
154 const char *dot;
155 signed long long value, minor;
156 /* The following buffer contain two numbers, a ':' separator and the final \0. */
157 char buffer[NB_LLMAX_STR + 1 + NB_LLMAX_STR + 1];
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100158 int is_loaded;
Thierry FOURNIER3534d882014-01-20 17:01:44 +0100159 int unique_id;
160 char *error;
161 struct pat_ref *ref;
162 struct pattern_expr *pattern_expr;
Thierry FOURNIER9860c412014-01-29 14:23:29 +0100163 int load_as_map = 0;
Willy Tarreau47bccd32014-08-29 17:36:40 +0200164 int acl_conv_found = 0;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200165
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100166 /* First, we look for an ACL keyword. And if we don't find one, then
167 * we look for a sample fetch expression starting with a sample fetch
168 * keyword.
Willy Tarreaubef91e72013-03-31 23:14:46 +0200169 */
Willy Tarreau131b4662013-12-13 01:08:36 +0100170
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100171 al->ctx = ARGC_ACL; // to report errors while resolving args late
Willy Tarreau131b4662013-12-13 01:08:36 +0100172 al->kw = *args;
173 al->conv = NULL;
174
Willy Tarreaua84d3742007-05-07 00:36:48 +0200175 aclkw = find_acl_kw(args[0]);
Willy Tarreau20490922014-03-17 18:04:27 +0100176 if (aclkw) {
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100177 /* OK we have a real ACL keyword */
Willy Tarreau9987ea92013-06-11 21:09:06 +0200178
Willy Tarreau131b4662013-12-13 01:08:36 +0100179 /* build new sample expression for this ACL */
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100180 smp = calloc(1, sizeof(struct sample_expr));
181 if (!smp) {
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100182 memprintf(err, "out of memory when parsing ACL expression");
183 goto out_return;
184 }
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100185 LIST_INIT(&(smp->conv_exprs));
186 smp->fetch = aclkw->smp;
187 smp->arg_p = empty_arg_list;
Willy Tarreau34db1082012-04-19 17:16:54 +0200188
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100189 /* look for the begining of the subject arguments */
Willy Tarreau131b4662013-12-13 01:08:36 +0100190 for (arg = args[0]; *arg && *arg != '(' && *arg != ','; arg++);
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100191
Willy Tarreau131b4662013-12-13 01:08:36 +0100192 endt = arg;
193 if (*endt == '(') {
194 /* look for the end of this term and skip the opening parenthesis */
195 endt = ++arg;
196 while (*endt && *endt != ')')
197 endt++;
198 if (*endt != ')') {
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100199 memprintf(err, "missing closing ')' after arguments to ACL keyword '%s'", aclkw->kw);
200 goto out_free_smp;
Willy Tarreau131b4662013-12-13 01:08:36 +0100201 }
202 }
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100203
Willy Tarreau131b4662013-12-13 01:08:36 +0100204 /* At this point, we have :
205 * - args[0] : beginning of the keyword
206 * - arg : end of the keyword, first character not part of keyword
207 * nor the opening parenthesis (so first character of args
208 * if present).
209 * - endt : end of the term (=arg or last parenthesis if args are present)
210 */
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100211 nbargs = make_arg_list(arg, endt - arg, smp->fetch->arg_mask, &smp->arg_p,
Willy Tarreau131b4662013-12-13 01:08:36 +0100212 err, NULL, NULL, al);
213 if (nbargs < 0) {
214 /* note that make_arg_list will have set <err> here */
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100215 memprintf(err, "ACL keyword '%s' : %s", aclkw->kw, *err);
216 goto out_free_smp;
Willy Tarreau131b4662013-12-13 01:08:36 +0100217 }
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100218
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100219 if (!smp->arg_p) {
220 smp->arg_p = empty_arg_list;
Willy Tarreau131b4662013-12-13 01:08:36 +0100221 }
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100222 else if (smp->fetch->val_args && !smp->fetch->val_args(smp->arg_p, err)) {
Willy Tarreau131b4662013-12-13 01:08:36 +0100223 /* invalid keyword argument, error must have been
224 * set by val_args().
225 */
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100226 memprintf(err, "in argument to '%s', %s", aclkw->kw, *err);
227 goto out_free_smp;
Willy Tarreau131b4662013-12-13 01:08:36 +0100228 }
229 arg = endt;
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100230
Willy Tarreau131b4662013-12-13 01:08:36 +0100231 /* look for the begining of the converters list. Those directly attached
232 * to the ACL keyword are found just after <arg> which points to the comma.
Willy Tarreau47bccd32014-08-29 17:36:40 +0200233 * If we find any converter, then we don't use the ACL keyword's match
234 * anymore but the one related to the converter's output type, so we'll
235 * kill the ACL kw to avoid any future reference to it (since in fact we'll
236 * only be using it as a sample fetch function).
Willy Tarreau131b4662013-12-13 01:08:36 +0100237 */
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100238 prev_type = smp->fetch->out_type;
Willy Tarreau131b4662013-12-13 01:08:36 +0100239 while (*arg) {
240 struct sample_conv *conv;
241 struct sample_conv_expr *conv_expr;
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100242
Willy Tarreau131b4662013-12-13 01:08:36 +0100243 if (*arg == ')') /* skip last closing parenthesis */
244 arg++;
245
246 if (*arg && *arg != ',') {
247 if (ckw)
248 memprintf(err, "ACL keyword '%s' : missing comma after conv keyword '%s'.",
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100249 aclkw->kw, ckw);
Willy Tarreau131b4662013-12-13 01:08:36 +0100250 else
251 memprintf(err, "ACL keyword '%s' : missing comma after fetch keyword.",
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100252 aclkw->kw);
253 goto out_free_smp;
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200254 }
Willy Tarreauae52f062012-04-26 12:13:35 +0200255
Willy Tarreau131b4662013-12-13 01:08:36 +0100256 while (*arg == ',') /* then trailing commas */
257 arg++;
Willy Tarreau2e845be2012-10-19 19:49:09 +0200258
Willy Tarreau131b4662013-12-13 01:08:36 +0100259 begw = arg; /* start of conv keyword */
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100260
Willy Tarreau131b4662013-12-13 01:08:36 +0100261 if (!*begw)
262 /* none ? end of converters */
263 break;
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100264
Willy Tarreau131b4662013-12-13 01:08:36 +0100265 for (endw = begw; *endw && *endw != '(' && *endw != ','; endw++);
Willy Tarreau9ca69362013-10-22 19:10:06 +0200266
Willy Tarreau131b4662013-12-13 01:08:36 +0100267 free(ckw);
268 ckw = my_strndup(begw, endw - begw);
Willy Tarreauf75d0082013-04-07 21:20:44 +0200269
Willy Tarreau131b4662013-12-13 01:08:36 +0100270 conv = find_sample_conv(begw, endw - begw);
271 if (!conv) {
272 /* Unknown converter method */
273 memprintf(err, "ACL keyword '%s' : unknown conv method '%s'.",
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100274 aclkw->kw, ckw);
275 goto out_free_smp;
Willy Tarreau131b4662013-12-13 01:08:36 +0100276 }
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100277
Willy Tarreau131b4662013-12-13 01:08:36 +0100278 arg = endw;
279 if (*arg == '(') {
280 /* look for the end of this term */
281 while (*arg && *arg != ')')
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100282 arg++;
Willy Tarreau131b4662013-12-13 01:08:36 +0100283 if (*arg != ')') {
284 memprintf(err, "ACL keyword '%s' : syntax error: missing ')' after conv keyword '%s'.",
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100285 aclkw->kw, ckw);
286 goto out_free_smp;
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100287 }
Willy Tarreau131b4662013-12-13 01:08:36 +0100288 }
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100289
Willy Tarreau131b4662013-12-13 01:08:36 +0100290 if (conv->in_type >= SMP_TYPES || conv->out_type >= SMP_TYPES) {
291 memprintf(err, "ACL keyword '%s' : returns type of conv method '%s' is unknown.",
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100292 aclkw->kw, ckw);
293 goto out_free_smp;
Willy Tarreau131b4662013-12-13 01:08:36 +0100294 }
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100295
Willy Tarreau131b4662013-12-13 01:08:36 +0100296 /* If impossible type conversion */
297 if (!sample_casts[prev_type][conv->in_type]) {
298 memprintf(err, "ACL keyword '%s' : conv method '%s' cannot be applied.",
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100299 aclkw->kw, ckw);
300 goto out_free_smp;
Willy Tarreau131b4662013-12-13 01:08:36 +0100301 }
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100302
Willy Tarreau131b4662013-12-13 01:08:36 +0100303 prev_type = conv->out_type;
304 conv_expr = calloc(1, sizeof(struct sample_conv_expr));
305 if (!conv_expr)
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100306 goto out_free_smp;
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100307
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100308 LIST_ADDQ(&(smp->conv_exprs), &(conv_expr->list));
Willy Tarreau131b4662013-12-13 01:08:36 +0100309 conv_expr->conv = conv;
Willy Tarreau47bccd32014-08-29 17:36:40 +0200310 acl_conv_found = 1;
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100311
Willy Tarreau131b4662013-12-13 01:08:36 +0100312 if (arg != endw) {
Willy Tarreau131b4662013-12-13 01:08:36 +0100313 int err_arg;
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100314
Willy Tarreau131b4662013-12-13 01:08:36 +0100315 if (!conv->arg_mask) {
316 memprintf(err, "ACL keyword '%s' : conv method '%s' does not support any args.",
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100317 aclkw->kw, ckw);
318 goto out_free_smp;
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100319 }
320
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100321 al->kw = smp->fetch->kw;
Willy Tarreau131b4662013-12-13 01:08:36 +0100322 al->conv = conv_expr->conv->kw;
Willy Tarreauadaddc22013-12-13 01:30:22 +0100323 if (make_arg_list(endw + 1, arg - endw - 1, conv->arg_mask, &conv_expr->arg_p, err, NULL, &err_arg, al) < 0) {
Willy Tarreau131b4662013-12-13 01:08:36 +0100324 memprintf(err, "ACL keyword '%s' : invalid arg %d in conv method '%s' : %s.",
Willy Tarreauadaddc22013-12-13 01:30:22 +0100325 aclkw->kw, err_arg+1, ckw, *err);
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100326 goto out_free_smp;
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100327 }
328
Willy Tarreau131b4662013-12-13 01:08:36 +0100329 if (!conv_expr->arg_p)
330 conv_expr->arg_p = empty_arg_list;
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100331
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +0100332 if (conv->val_args && !conv->val_args(conv_expr->arg_p, conv, file, line, err)) {
Willy Tarreau131b4662013-12-13 01:08:36 +0100333 memprintf(err, "ACL keyword '%s' : invalid args in conv method '%s' : %s.",
Willy Tarreauadaddc22013-12-13 01:30:22 +0100334 aclkw->kw, ckw, *err);
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100335 goto out_free_smp;
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100336 }
337 }
Willy Tarreau131b4662013-12-13 01:08:36 +0100338 else if (ARGM(conv->arg_mask)) {
339 memprintf(err, "ACL keyword '%s' : missing args for conv method '%s'.",
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100340 aclkw->kw, ckw);
341 goto out_free_smp;
Willy Tarreau131b4662013-12-13 01:08:36 +0100342 }
Willy Tarreau61612d42012-04-19 18:42:05 +0200343 }
Willy Tarreaua84d3742007-05-07 00:36:48 +0200344 }
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100345 else {
346 /* This is not an ACL keyword, so we hope this is a sample fetch
347 * keyword that we're going to transparently use as an ACL. If
348 * so, we retrieve a completely parsed expression with args and
349 * convs already done.
350 */
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +0100351 smp = sample_parse_expr((char **)args, &idx, file, line, err, al);
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100352 if (!smp) {
353 memprintf(err, "%s in ACL expression '%s'", *err, *args);
354 goto out_return;
355 }
Willy Tarreau47bccd32014-08-29 17:36:40 +0200356 prev_type = smp_expr_output_type(smp);
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100357 }
358
Willy Tarreau47bccd32014-08-29 17:36:40 +0200359 /* From now on, prev_type is the expression's output type.
360 * Stop relying on ACL keyword if a converter is used.
361 */
362 if (acl_conv_found)
363 aclkw = NULL;
364
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100365 expr = (struct acl_expr *)calloc(1, sizeof(*expr));
366 if (!expr) {
367 memprintf(err, "out of memory when parsing ACL expression");
368 goto out_return;
369 }
370
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100371 pattern_init_head(&expr->pat);
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100372
373 expr->kw = aclkw ? aclkw->kw : smp->fetch->kw;
374 expr->pat.parse = aclkw ? aclkw->parse : NULL;
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100375 expr->pat.index = aclkw ? aclkw->index : NULL;
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100376 expr->pat.match = aclkw ? aclkw->match : NULL;
Thierry FOURNIERb1136502014-01-15 11:38:49 +0100377 expr->pat.delete = aclkw ? aclkw->delete : NULL;
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +0100378 expr->pat.prune = aclkw ? aclkw->prune : NULL;
Willy Tarreau47bccd32014-08-29 17:36:40 +0200379 expr->pat.expect_type = prev_type;
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100380 expr->smp = smp;
381 smp = NULL;
382
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +0100383 /* Fill NULL pointers with values provided by the pattern.c arrays */
384 if (aclkw) {
385 if (!expr->pat.parse)
386 expr->pat.parse = pat_parse_fcts[aclkw->match_type];
387
388 if (!expr->pat.index)
389 expr->pat.index = pat_index_fcts[aclkw->match_type];
390
391 if (!expr->pat.match)
392 expr->pat.match = pat_match_fcts[aclkw->match_type];
393
394 if (!expr->pat.delete)
395 expr->pat.delete = pat_delete_fcts[aclkw->match_type];
396
397 if (!expr->pat.prune)
398 expr->pat.prune = pat_prune_fcts[aclkw->match_type];
399 }
400
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100401 if (!expr->pat.parse) {
402 /* some types can be automatically converted */
403
Willy Tarreau47bccd32014-08-29 17:36:40 +0200404 switch (prev_type) {
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100405 case SMP_T_BOOL:
406 expr->pat.parse = pat_parse_fcts[PAT_MATCH_BOOL];
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100407 expr->pat.index = pat_index_fcts[PAT_MATCH_BOOL];
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100408 expr->pat.match = pat_match_fcts[PAT_MATCH_BOOL];
Thierry FOURNIERb1136502014-01-15 11:38:49 +0100409 expr->pat.delete = pat_delete_fcts[PAT_MATCH_BOOL];
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +0100410 expr->pat.prune = pat_prune_fcts[PAT_MATCH_BOOL];
Thierry FOURNIER5d344082014-01-27 14:19:53 +0100411 expr->pat.expect_type = pat_match_types[PAT_MATCH_BOOL];
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100412 break;
413 case SMP_T_SINT:
414 case SMP_T_UINT:
415 expr->pat.parse = pat_parse_fcts[PAT_MATCH_INT];
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100416 expr->pat.index = pat_index_fcts[PAT_MATCH_INT];
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100417 expr->pat.match = pat_match_fcts[PAT_MATCH_INT];
Thierry FOURNIERb1136502014-01-15 11:38:49 +0100418 expr->pat.delete = pat_delete_fcts[PAT_MATCH_INT];
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +0100419 expr->pat.prune = pat_prune_fcts[PAT_MATCH_INT];
Thierry FOURNIER5d344082014-01-27 14:19:53 +0100420 expr->pat.expect_type = pat_match_types[PAT_MATCH_INT];
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100421 break;
422 case SMP_T_IPV4:
423 case SMP_T_IPV6:
424 expr->pat.parse = pat_parse_fcts[PAT_MATCH_IP];
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100425 expr->pat.index = pat_index_fcts[PAT_MATCH_IP];
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100426 expr->pat.match = pat_match_fcts[PAT_MATCH_IP];
Thierry FOURNIERb1136502014-01-15 11:38:49 +0100427 expr->pat.delete = pat_delete_fcts[PAT_MATCH_IP];
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +0100428 expr->pat.prune = pat_prune_fcts[PAT_MATCH_IP];
Thierry FOURNIER5d344082014-01-27 14:19:53 +0100429 expr->pat.expect_type = pat_match_types[PAT_MATCH_IP];
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100430 break;
Thierry FOURNIER9fefbd52014-05-11 15:15:00 +0200431 case SMP_T_STR:
432 expr->pat.parse = pat_parse_fcts[PAT_MATCH_STR];
433 expr->pat.index = pat_index_fcts[PAT_MATCH_STR];
434 expr->pat.match = pat_match_fcts[PAT_MATCH_STR];
435 expr->pat.delete = pat_delete_fcts[PAT_MATCH_STR];
436 expr->pat.prune = pat_prune_fcts[PAT_MATCH_STR];
437 expr->pat.expect_type = pat_match_types[PAT_MATCH_STR];
438 break;
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100439 }
440 }
Willy Tarreaua84d3742007-05-07 00:36:48 +0200441
Willy Tarreau3c3dfd52013-11-04 18:09:12 +0100442 /* Additional check to protect against common mistakes */
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100443 cur_type = smp_expr_output_type(expr->smp);
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +0100444 if (expr->pat.parse && cur_type != SMP_T_BOOL && !*args[1]) {
Willy Tarreau3c3dfd52013-11-04 18:09:12 +0100445 Warning("parsing acl keyword '%s' :\n"
446 " no pattern to match against were provided, so this ACL will never match.\n"
447 " If this is what you intended, please add '--' to get rid of this warning.\n"
448 " If you intended to match only for existence, please use '-m found'.\n"
449 " If you wanted to force an int to match as a bool, please use '-m bool'.\n"
450 "\n",
451 args[0]);
452 }
453
Willy Tarreaua84d3742007-05-07 00:36:48 +0200454 args++;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200455
456 /* check for options before patterns. Supported options are :
457 * -i : ignore case for all patterns by default
458 * -f : read patterns from those files
Willy Tarreau5adeda12013-03-31 22:13:34 +0200459 * -m : force matching method (must be used before -f)
Thierry FOURNIER9860c412014-01-29 14:23:29 +0100460 * -M : load the file as map file
Thierry FOURNIER3534d882014-01-20 17:01:44 +0100461 * -u : force the unique id of the acl
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200462 * -- : everything after this is not an option
463 */
464 patflags = 0;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100465 is_loaded = 0;
Thierry FOURNIER3534d882014-01-20 17:01:44 +0100466 unique_id = -1;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200467 while (**args == '-') {
Willy Tarreau2039bba2014-05-11 09:43:46 +0200468 if (strcmp(*args, "-i") == 0)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200469 patflags |= PAT_MF_IGNORE_CASE;
Willy Tarreau2039bba2014-05-11 09:43:46 +0200470 else if (strcmp(*args, "-n") == 0)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200471 patflags |= PAT_MF_NO_DNS;
Willy Tarreau2039bba2014-05-11 09:43:46 +0200472 else if (strcmp(*args, "-u") == 0) {
Thierry FOURNIER3534d882014-01-20 17:01:44 +0100473 unique_id = strtol(args[1], &error, 10);
474 if (*error != '\0') {
475 memprintf(err, "the argument of -u must be an integer");
476 goto out_free_expr;
477 }
478
479 /* Check if this id is really unique. */
480 if (pat_ref_lookupid(unique_id)) {
481 memprintf(err, "the id is already used");
482 goto out_free_expr;
483 }
484
485 args++;
486 }
Willy Tarreau2039bba2014-05-11 09:43:46 +0200487 else if (strcmp(*args, "-f") == 0) {
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +0100488 if (!expr->pat.parse) {
Willy Tarreau9987ea92013-06-11 21:09:06 +0200489 memprintf(err, "matching method must be specified first (using '-m') when using a sample fetch of this type ('%s')", expr->kw);
Willy Tarreaubef91e72013-03-31 23:14:46 +0200490 goto out_free_expr;
491 }
492
Thierry FOURNIER66eb9bf2014-02-11 16:19:46 +0100493 if (!pattern_read_from_file(&expr->pat, PAT_REF_ACL, args[1], patflags, load_as_map, err, file, line))
Willy Tarreau2b5285d2010-05-09 23:45:24 +0200494 goto out_free_expr;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100495 is_loaded = 1;
Willy Tarreau5adeda12013-03-31 22:13:34 +0200496 args++;
497 }
Willy Tarreau2039bba2014-05-11 09:43:46 +0200498 else if (strcmp(*args, "-m") == 0) {
Willy Tarreau5adeda12013-03-31 22:13:34 +0200499 int idx;
500
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100501 if (is_loaded) {
Willy Tarreau5adeda12013-03-31 22:13:34 +0200502 memprintf(err, "'-m' must only be specified before patterns and files in parsing ACL expression");
503 goto out_free_expr;
504 }
505
Willy Tarreau6f8fe312013-11-28 22:24:25 +0100506 idx = pat_find_match_name(args[1]);
Willy Tarreau5adeda12013-03-31 22:13:34 +0200507 if (idx < 0) {
508 memprintf(err, "unknown matching method '%s' when parsing ACL expression", args[1]);
509 goto out_free_expr;
510 }
511
512 /* Note: -m found is always valid, bool/int are compatible, str/bin/reg/len are compatible */
Thierry FOURNIERe3ded592013-12-06 15:36:54 +0100513 if (!sample_casts[cur_type][pat_match_types[idx]]) {
Willy Tarreau93fddf12013-03-31 22:59:32 +0200514 memprintf(err, "matching method '%s' cannot be used with fetch keyword '%s'", args[1], expr->kw);
Willy Tarreau5adeda12013-03-31 22:13:34 +0200515 goto out_free_expr;
516 }
Thierry FOURNIERe3ded592013-12-06 15:36:54 +0100517 expr->pat.parse = pat_parse_fcts[idx];
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100518 expr->pat.index = pat_index_fcts[idx];
Thierry FOURNIERe3ded592013-12-06 15:36:54 +0100519 expr->pat.match = pat_match_fcts[idx];
Thierry FOURNIERb1136502014-01-15 11:38:49 +0100520 expr->pat.delete = pat_delete_fcts[idx];
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +0100521 expr->pat.prune = pat_prune_fcts[idx];
Thierry FOURNIER5d344082014-01-27 14:19:53 +0100522 expr->pat.expect_type = pat_match_types[idx];
Willy Tarreau2b5285d2010-05-09 23:45:24 +0200523 args++;
524 }
Willy Tarreau2039bba2014-05-11 09:43:46 +0200525 else if (strcmp(*args, "-M") == 0) {
Thierry FOURNIER9860c412014-01-29 14:23:29 +0100526 load_as_map = 1;
527 }
Willy Tarreau2039bba2014-05-11 09:43:46 +0200528 else if (strcmp(*args, "--") == 0) {
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200529 args++;
530 break;
531 }
Willy Tarreau2039bba2014-05-11 09:43:46 +0200532 else {
533 memprintf(err, "'%s' is not a valid ACL option. Please use '--' before any pattern beginning with a '-'", args[0]);
534 goto out_free_expr;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200535 break;
Willy Tarreau2039bba2014-05-11 09:43:46 +0200536 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200537 args++;
538 }
539
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +0100540 if (!expr->pat.parse) {
Willy Tarreau9987ea92013-06-11 21:09:06 +0200541 memprintf(err, "matching method must be specified first (using '-m') when using a sample fetch of this type ('%s')", expr->kw);
Willy Tarreaubef91e72013-03-31 23:14:46 +0200542 goto out_free_expr;
543 }
544
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +0100545 /* Create displayed reference */
546 snprintf(trash.str, trash.size, "acl '%s' file '%s' line %d", expr->kw, file, line);
547 trash.str[trash.size - 1] = '\0';
548
Thierry FOURNIER3534d882014-01-20 17:01:44 +0100549 /* Create new patern reference. */
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +0100550 ref = pat_ref_newid(unique_id, trash.str, PAT_REF_ACL);
Thierry FOURNIER3534d882014-01-20 17:01:44 +0100551 if (!ref) {
552 memprintf(err, "memory error");
553 goto out_free_expr;
554 }
555
556 /* Create new pattern expression associated to this reference. */
557 pattern_expr = pattern_new_expr(&expr->pat, ref, err);
558 if (!pattern_expr)
559 goto out_free_expr;
560
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200561 /* Copy the pattern matching and indexing flags. */
562 pattern_expr->mflags = patflags;
563
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200564 /* now parse all patterns */
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100565 while (**args) {
566 arg = *args;
567
568 /* Compatibility layer. Each pattern can parse only one string per pattern,
569 * but the pat_parser_int() and pat_parse_dotted_ver() parsers were need
570 * optionnaly two operators. The first operator is the match method: eq,
571 * le, lt, ge and gt. pat_parse_int() and pat_parse_dotted_ver() functions
572 * can have a compatibility syntax based on ranges:
573 *
574 * pat_parse_int():
575 *
576 * "eq x" -> "x" or "x:x"
577 * "le x" -> ":x"
578 * "lt x" -> ":y" (with y = x - 1)
579 * "ge x" -> "x:"
580 * "gt x" -> "y:" (with y = x + 1)
581 *
582 * pat_parse_dotted_ver():
583 *
584 * "eq x.y" -> "x.y" or "x.y:x.y"
585 * "le x.y" -> ":x.y"
586 * "lt x.y" -> ":w.z" (with w.z = x.y - 1)
587 * "ge x.y" -> "x.y:"
588 * "gt x.y" -> "w.z:" (with w.z = x.y + 1)
589 *
590 * If y is not present, assume that is "0".
591 *
592 * The syntax eq, le, lt, ge and gt are proper to the acl syntax. The
593 * following block of code detect the operator, and rewrite each value
594 * in parsable string.
595 */
596 if (expr->pat.parse == pat_parse_int ||
597 expr->pat.parse == pat_parse_dotted_ver) {
598 /* Check for operator. If the argument is operator, memorise it and
599 * continue to the next argument.
600 */
601 op = get_std_op(arg);
602 if (op != -1) {
603 operator = op;
604 args++;
605 continue;
606 }
607
608 /* Check if the pattern contain ':' or '-' character. */
609 contain_colon = (strchr(arg, ':') || strchr(arg, '-'));
610
611 /* If the pattern contain ':' or '-' character, give it to the parser as is.
612 * If no contain ':' and operator is STD_OP_EQ, give it to the parser as is.
613 * In other case, try to convert the value according with the operator.
614 */
615 if (!contain_colon && operator != STD_OP_EQ) {
616 /* Search '.' separator. */
617 dot = strchr(arg, '.');
618 if (!dot) {
619 have_dot = 0;
620 minor = 0;
621 dot = arg + strlen(arg);
622 }
623 else
624 have_dot = 1;
625
626 /* convert the integer minor part for the pat_parse_dotted_ver() function. */
627 if (expr->pat.parse == pat_parse_dotted_ver && have_dot) {
628 if (strl2llrc(dot+1, strlen(dot+1), &minor) != 0) {
629 memprintf(err, "'%s' is neither a number nor a supported operator", arg);
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100630 goto out_free_expr;
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100631 }
632 if (minor >= 65536) {
633 memprintf(err, "'%s' contains too large a minor value", arg);
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100634 goto out_free_expr;
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100635 }
636 }
637
638 /* convert the integer value for the pat_parse_int() function, and the
639 * integer major part for the pat_parse_dotted_ver() function.
640 */
641 if (strl2llrc(arg, dot - arg, &value) != 0) {
642 memprintf(err, "'%s' is neither a number nor a supported operator", arg);
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100643 goto out_free_expr;
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100644 }
645 if (expr->pat.parse == pat_parse_dotted_ver) {
646 if (value >= 65536) {
647 memprintf(err, "'%s' contains too large a major value", arg);
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100648 goto out_free_expr;
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100649 }
650 value = (value << 16) | (minor & 0xffff);
651 }
652
653 switch (operator) {
654
655 case STD_OP_EQ: /* this case is not possible. */
656 memprintf(err, "internal error");
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100657 goto out_free_expr;
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100658
659 case STD_OP_GT:
660 value++; /* gt = ge + 1 */
661
662 case STD_OP_GE:
663 if (expr->pat.parse == pat_parse_int)
664 snprintf(buffer, NB_LLMAX_STR+NB_LLMAX_STR+2, "%lld:", value);
665 else
666 snprintf(buffer, NB_LLMAX_STR+NB_LLMAX_STR+2, "%lld.%lld:",
667 value >> 16, value & 0xffff);
668 arg = buffer;
669 break;
670
671 case STD_OP_LT:
672 value--; /* lt = le - 1 */
673
674 case STD_OP_LE:
675 if (expr->pat.parse == pat_parse_int)
676 snprintf(buffer, NB_LLMAX_STR+NB_LLMAX_STR+2, ":%lld", value);
677 else
678 snprintf(buffer, NB_LLMAX_STR+NB_LLMAX_STR+2, ":%lld.%lld",
679 value >> 16, value & 0xffff);
680 arg = buffer;
681 break;
682 }
683 }
684 }
685
Thierry FOURNIER3534d882014-01-20 17:01:44 +0100686 /* Add sample to the reference, and try to compile it fior each pattern
687 * using this value.
688 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200689 if (!pat_ref_add(ref, arg, NULL, err))
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100690 goto out_free_expr;
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100691 args++;
692 }
Willy Tarreaua84d3742007-05-07 00:36:48 +0200693
694 return expr;
695
Willy Tarreaua84d3742007-05-07 00:36:48 +0200696 out_free_expr:
697 prune_acl_expr(expr);
698 free(expr);
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100699 free(ckw);
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100700 out_free_smp:
701 free(smp);
Willy Tarreaua84d3742007-05-07 00:36:48 +0200702 out_return:
703 return NULL;
704}
705
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +0200706/* Purge everything in the acl <acl>, then return <acl>. */
707struct acl *prune_acl(struct acl *acl) {
708
709 struct acl_expr *expr, *exprb;
710
711 free(acl->name);
712
713 list_for_each_entry_safe(expr, exprb, &acl->expr, list) {
714 LIST_DEL(&expr->list);
715 prune_acl_expr(expr);
716 free(expr);
717 }
718
719 return acl;
720}
721
Willy Tarreaua84d3742007-05-07 00:36:48 +0200722/* Parse an ACL with the name starting at <args>[0], and with a list of already
723 * known ACLs in <acl>. If the ACL was not in the list, it will be added.
Willy Tarreau2a56c5e2010-03-15 16:13:29 +0100724 * A pointer to that ACL is returned. If the ACL has an empty name, then it's
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200725 * an anonymous one and it won't be merged with any other one. If <err> is not
726 * NULL, it will be filled with an appropriate error. This pointer must be
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200727 * freeable or NULL. <al> is the arg_list serving as a head for unresolved
728 * dependencies.
Willy Tarreaua84d3742007-05-07 00:36:48 +0200729 *
730 * args syntax: <aclname> <acl_expr>
731 */
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +0100732struct acl *parse_acl(const char **args, struct list *known_acl, char **err, struct arg_list *al,
733 const char *file, int line)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200734{
735 __label__ out_return, out_free_acl_expr, out_free_name;
736 struct acl *cur_acl;
737 struct acl_expr *acl_expr;
738 char *name;
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200739 const char *pos;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200740
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200741 if (**args && (pos = invalid_char(*args))) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200742 memprintf(err, "invalid character in ACL name : '%c'", *pos);
Willy Tarreau2e74c3f2007-12-02 18:45:09 +0100743 goto out_return;
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200744 }
Willy Tarreau2e74c3f2007-12-02 18:45:09 +0100745
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +0100746 acl_expr = parse_acl_expr(args + 1, err, al, file, line);
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200747 if (!acl_expr) {
748 /* parse_acl_expr will have filled <err> here */
Willy Tarreaua84d3742007-05-07 00:36:48 +0200749 goto out_return;
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200750 }
Willy Tarreaua84d3742007-05-07 00:36:48 +0200751
Willy Tarreau404e8ab2009-07-26 19:40:40 +0200752 /* Check for args beginning with an opening parenthesis just after the
753 * subject, as this is almost certainly a typo. Right now we can only
754 * emit a warning, so let's do so.
755 */
Krzysztof Piotr Oledzki4cdd8312009-10-05 00:23:35 +0200756 if (!strchr(args[1], '(') && *args[2] == '(')
Willy Tarreau404e8ab2009-07-26 19:40:40 +0200757 Warning("parsing acl '%s' :\n"
758 " matching '%s' for pattern '%s' is likely a mistake and probably\n"
759 " not what you want. Maybe you need to remove the extraneous space before '('.\n"
760 " If you are really sure this is not an error, please insert '--' between the\n"
761 " match and the pattern to make this warning message disappear.\n",
762 args[0], args[1], args[2]);
763
Willy Tarreau2a56c5e2010-03-15 16:13:29 +0100764 if (*args[0])
765 cur_acl = find_acl_by_name(args[0], known_acl);
766 else
767 cur_acl = NULL;
768
Willy Tarreaua84d3742007-05-07 00:36:48 +0200769 if (!cur_acl) {
770 name = strdup(args[0]);
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200771 if (!name) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200772 memprintf(err, "out of memory when parsing ACL");
Willy Tarreaua84d3742007-05-07 00:36:48 +0200773 goto out_free_acl_expr;
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200774 }
Willy Tarreaua84d3742007-05-07 00:36:48 +0200775 cur_acl = (struct acl *)calloc(1, sizeof(*cur_acl));
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200776 if (cur_acl == NULL) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200777 memprintf(err, "out of memory when parsing ACL");
Willy Tarreaua84d3742007-05-07 00:36:48 +0200778 goto out_free_name;
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200779 }
Willy Tarreaua84d3742007-05-07 00:36:48 +0200780
781 LIST_INIT(&cur_acl->expr);
782 LIST_ADDQ(known_acl, &cur_acl->list);
783 cur_acl->name = name;
784 }
785
Willy Tarreaua91d0a52013-03-25 08:12:18 +0100786 /* We want to know what features the ACL needs (typically HTTP parsing),
787 * and where it may be used. If an ACL relies on multiple matches, it is
788 * OK if at least one of them may match in the context where it is used.
789 */
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100790 cur_acl->use |= acl_expr->smp->fetch->use;
791 cur_acl->val |= acl_expr->smp->fetch->val;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200792 LIST_ADDQ(&cur_acl->expr, &acl_expr->list);
793 return cur_acl;
794
795 out_free_name:
796 free(name);
797 out_free_acl_expr:
798 prune_acl_expr(acl_expr);
799 free(acl_expr);
800 out_return:
801 return NULL;
802}
803
Willy Tarreau16fbe822007-06-17 11:54:31 +0200804/* Some useful ACLs provided by default. Only those used are allocated. */
805
806const struct {
807 const char *name;
808 const char *expr[4]; /* put enough for longest expression */
809} default_acl_list[] = {
Willy Tarreau58393e12008-07-20 10:39:22 +0200810 { .name = "TRUE", .expr = {"always_true",""}},
811 { .name = "FALSE", .expr = {"always_false",""}},
Willy Tarreau16fbe822007-06-17 11:54:31 +0200812 { .name = "LOCALHOST", .expr = {"src","127.0.0.1/8",""}},
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200813 { .name = "HTTP", .expr = {"req_proto_http",""}},
Willy Tarreau16fbe822007-06-17 11:54:31 +0200814 { .name = "HTTP_1.0", .expr = {"req_ver","1.0",""}},
815 { .name = "HTTP_1.1", .expr = {"req_ver","1.1",""}},
816 { .name = "METH_CONNECT", .expr = {"method","CONNECT",""}},
817 { .name = "METH_GET", .expr = {"method","GET","HEAD",""}},
818 { .name = "METH_HEAD", .expr = {"method","HEAD",""}},
819 { .name = "METH_OPTIONS", .expr = {"method","OPTIONS",""}},
820 { .name = "METH_POST", .expr = {"method","POST",""}},
821 { .name = "METH_TRACE", .expr = {"method","TRACE",""}},
822 { .name = "HTTP_URL_ABS", .expr = {"url_reg","^[^/:]*://",""}},
823 { .name = "HTTP_URL_SLASH", .expr = {"url_beg","/",""}},
824 { .name = "HTTP_URL_STAR", .expr = {"url","*",""}},
825 { .name = "HTTP_CONTENT", .expr = {"hdr_val(content-length)","gt","0",""}},
Emeric Brunbede3d02009-06-30 17:54:00 +0200826 { .name = "RDP_COOKIE", .expr = {"req_rdp_cookie_cnt","gt","0",""}},
Willy Tarreauc6317702008-07-20 09:29:50 +0200827 { .name = "REQ_CONTENT", .expr = {"req_len","gt","0",""}},
Willy Tarreaub6fb4202008-07-20 11:18:28 +0200828 { .name = "WAIT_END", .expr = {"wait_end",""}},
Willy Tarreau16fbe822007-06-17 11:54:31 +0200829 { .name = NULL, .expr = {""}}
830};
831
832/* Find a default ACL from the default_acl list, compile it and return it.
833 * If the ACL is not found, NULL is returned. In theory, it cannot fail,
834 * except when default ACLs are broken, in which case it will return NULL.
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200835 * If <known_acl> is not NULL, the ACL will be queued at its tail. If <err> is
836 * not NULL, it will be filled with an error message if an error occurs. This
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200837 * pointer must be freeable or NULL. <al> is an arg_list serving as a list head
838 * to report missing dependencies.
Willy Tarreau16fbe822007-06-17 11:54:31 +0200839 */
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200840static struct acl *find_acl_default(const char *acl_name, struct list *known_acl,
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +0100841 char **err, struct arg_list *al,
842 const char *file, int line)
Willy Tarreau16fbe822007-06-17 11:54:31 +0200843{
844 __label__ out_return, out_free_acl_expr, out_free_name;
845 struct acl *cur_acl;
846 struct acl_expr *acl_expr;
847 char *name;
848 int index;
849
850 for (index = 0; default_acl_list[index].name != NULL; index++) {
851 if (strcmp(acl_name, default_acl_list[index].name) == 0)
852 break;
853 }
854
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200855 if (default_acl_list[index].name == NULL) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200856 memprintf(err, "no such ACL : '%s'", acl_name);
Willy Tarreau16fbe822007-06-17 11:54:31 +0200857 return NULL;
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200858 }
Willy Tarreau16fbe822007-06-17 11:54:31 +0200859
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +0100860 acl_expr = parse_acl_expr((const char **)default_acl_list[index].expr, err, al, file, line);
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200861 if (!acl_expr) {
862 /* parse_acl_expr must have filled err here */
Willy Tarreau16fbe822007-06-17 11:54:31 +0200863 goto out_return;
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200864 }
Willy Tarreau16fbe822007-06-17 11:54:31 +0200865
866 name = strdup(acl_name);
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200867 if (!name) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200868 memprintf(err, "out of memory when building default ACL '%s'", acl_name);
Willy Tarreau16fbe822007-06-17 11:54:31 +0200869 goto out_free_acl_expr;
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200870 }
871
Willy Tarreau16fbe822007-06-17 11:54:31 +0200872 cur_acl = (struct acl *)calloc(1, sizeof(*cur_acl));
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200873 if (cur_acl == NULL) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200874 memprintf(err, "out of memory when building default ACL '%s'", acl_name);
Willy Tarreau16fbe822007-06-17 11:54:31 +0200875 goto out_free_name;
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200876 }
Willy Tarreau16fbe822007-06-17 11:54:31 +0200877
878 cur_acl->name = name;
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100879 cur_acl->use |= acl_expr->smp->fetch->use;
880 cur_acl->val |= acl_expr->smp->fetch->val;
Willy Tarreau16fbe822007-06-17 11:54:31 +0200881 LIST_INIT(&cur_acl->expr);
882 LIST_ADDQ(&cur_acl->expr, &acl_expr->list);
883 if (known_acl)
884 LIST_ADDQ(known_acl, &cur_acl->list);
885
886 return cur_acl;
887
888 out_free_name:
889 free(name);
890 out_free_acl_expr:
891 prune_acl_expr(acl_expr);
892 free(acl_expr);
893 out_return:
894 return NULL;
895}
Willy Tarreaua84d3742007-05-07 00:36:48 +0200896
897/* Purge everything in the acl_cond <cond>, then return <cond>. */
898struct acl_cond *prune_acl_cond(struct acl_cond *cond)
899{
900 struct acl_term_suite *suite, *tmp_suite;
901 struct acl_term *term, *tmp_term;
902
903 /* iterate through all term suites and free all terms and all suites */
904 list_for_each_entry_safe(suite, tmp_suite, &cond->suites, list) {
905 list_for_each_entry_safe(term, tmp_term, &suite->terms, list)
906 free(term);
907 free(suite);
908 }
909 return cond;
910}
911
912/* Parse an ACL condition starting at <args>[0], relying on a list of already
913 * known ACLs passed in <known_acl>. The new condition is returned (or NULL in
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200914 * case of low memory). Supports multiple conditions separated by "or". If
915 * <err> is not NULL, it will be filled with a pointer to an error message in
916 * case of error, that the caller is responsible for freeing. The initial
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200917 * location must either be freeable or NULL. The list <al> serves as a list head
918 * for unresolved dependencies.
Willy Tarreaua84d3742007-05-07 00:36:48 +0200919 */
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200920struct acl_cond *parse_acl_cond(const char **args, struct list *known_acl,
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +0100921 enum acl_cond_pol pol, char **err, struct arg_list *al,
922 const char *file, int line)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200923{
924 __label__ out_return, out_free_suite, out_free_term;
Willy Tarreau74b98a82007-06-16 19:35:18 +0200925 int arg, neg;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200926 const char *word;
927 struct acl *cur_acl;
928 struct acl_term *cur_term;
929 struct acl_term_suite *cur_suite;
930 struct acl_cond *cond;
Willy Tarreaua91d0a52013-03-25 08:12:18 +0100931 unsigned int suite_val;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200932
933 cond = (struct acl_cond *)calloc(1, sizeof(*cond));
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200934 if (cond == NULL) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200935 memprintf(err, "out of memory when parsing condition");
Willy Tarreaua84d3742007-05-07 00:36:48 +0200936 goto out_return;
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200937 }
Willy Tarreaua84d3742007-05-07 00:36:48 +0200938
939 LIST_INIT(&cond->list);
940 LIST_INIT(&cond->suites);
941 cond->pol = pol;
Willy Tarreaua91d0a52013-03-25 08:12:18 +0100942 cond->val = 0;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200943
944 cur_suite = NULL;
Willy Tarreaua91d0a52013-03-25 08:12:18 +0100945 suite_val = ~0U;
Willy Tarreau74b98a82007-06-16 19:35:18 +0200946 neg = 0;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200947 for (arg = 0; *args[arg]; arg++) {
948 word = args[arg];
949
950 /* remove as many exclamation marks as we can */
951 while (*word == '!') {
952 neg = !neg;
953 word++;
954 }
955
956 /* an empty word is allowed because we cannot force the user to
957 * always think about not leaving exclamation marks alone.
958 */
959 if (!*word)
960 continue;
961
Willy Tarreau16fbe822007-06-17 11:54:31 +0200962 if (strcasecmp(word, "or") == 0 || strcmp(word, "||") == 0) {
Willy Tarreaua84d3742007-05-07 00:36:48 +0200963 /* new term suite */
Willy Tarreaua91d0a52013-03-25 08:12:18 +0100964 cond->val |= suite_val;
965 suite_val = ~0U;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200966 cur_suite = NULL;
967 neg = 0;
968 continue;
969 }
970
Willy Tarreau95fa4692010-02-01 13:05:50 +0100971 if (strcmp(word, "{") == 0) {
972 /* we may have a complete ACL expression between two braces,
973 * find the last one.
974 */
975 int arg_end = arg + 1;
976 const char **args_new;
977
978 while (*args[arg_end] && strcmp(args[arg_end], "}") != 0)
979 arg_end++;
980
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200981 if (!*args[arg_end]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200982 memprintf(err, "missing closing '}' in condition");
Willy Tarreau95fa4692010-02-01 13:05:50 +0100983 goto out_free_suite;
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200984 }
Willy Tarreau95fa4692010-02-01 13:05:50 +0100985
986 args_new = calloc(1, (arg_end - arg + 1) * sizeof(*args_new));
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200987 if (!args_new) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200988 memprintf(err, "out of memory when parsing condition");
Willy Tarreau95fa4692010-02-01 13:05:50 +0100989 goto out_free_suite;
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200990 }
Willy Tarreau95fa4692010-02-01 13:05:50 +0100991
Willy Tarreau2a56c5e2010-03-15 16:13:29 +0100992 args_new[0] = "";
Willy Tarreau95fa4692010-02-01 13:05:50 +0100993 memcpy(args_new + 1, args + arg + 1, (arg_end - arg) * sizeof(*args_new));
994 args_new[arg_end - arg] = "";
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +0100995 cur_acl = parse_acl(args_new, known_acl, err, al, file, line);
Willy Tarreau95fa4692010-02-01 13:05:50 +0100996 free(args_new);
997
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200998 if (!cur_acl) {
999 /* note that parse_acl() must have filled <err> here */
Willy Tarreau16fbe822007-06-17 11:54:31 +02001000 goto out_free_suite;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001001 }
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001002 word = args[arg + 1];
Willy Tarreau95fa4692010-02-01 13:05:50 +01001003 arg = arg_end;
1004 }
1005 else {
1006 /* search for <word> in the known ACL names. If we do not find
1007 * it, let's look for it in the default ACLs, and if found, add
1008 * it to the list of ACLs of this proxy. This makes it possible
1009 * to override them.
1010 */
1011 cur_acl = find_acl_by_name(word, known_acl);
1012 if (cur_acl == NULL) {
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001013 cur_acl = find_acl_default(word, known_acl, err, al, file, line);
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001014 if (cur_acl == NULL) {
1015 /* note that find_acl_default() must have filled <err> here */
Willy Tarreau95fa4692010-02-01 13:05:50 +01001016 goto out_free_suite;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001017 }
Willy Tarreau95fa4692010-02-01 13:05:50 +01001018 }
Willy Tarreau16fbe822007-06-17 11:54:31 +02001019 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001020
1021 cur_term = (struct acl_term *)calloc(1, sizeof(*cur_term));
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001022 if (cur_term == NULL) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001023 memprintf(err, "out of memory when parsing condition");
Willy Tarreaua84d3742007-05-07 00:36:48 +02001024 goto out_free_suite;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001025 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001026
1027 cur_term->acl = cur_acl;
1028 cur_term->neg = neg;
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001029
1030 /* Here it is a bit complex. The acl_term_suite is a conjunction
1031 * of many terms. It may only be used if all of its terms are
1032 * usable at the same time. So the suite's validity domain is an
1033 * AND between all ACL keywords' ones. But, the global condition
1034 * is valid if at least one term suite is OK. So it's an OR between
1035 * all of their validity domains. We could emit a warning as soon
1036 * as suite_val is null because it means that the last ACL is not
1037 * compatible with the previous ones. Let's remain simple for now.
1038 */
1039 cond->use |= cur_acl->use;
1040 suite_val &= cur_acl->val;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001041
1042 if (!cur_suite) {
1043 cur_suite = (struct acl_term_suite *)calloc(1, sizeof(*cur_suite));
Willy Tarreauf678b7f2013-01-24 00:25:39 +01001044 if (cur_suite == NULL) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001045 memprintf(err, "out of memory when parsing condition");
Willy Tarreaua84d3742007-05-07 00:36:48 +02001046 goto out_free_term;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001047 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001048 LIST_INIT(&cur_suite->terms);
1049 LIST_ADDQ(&cond->suites, &cur_suite->list);
1050 }
1051 LIST_ADDQ(&cur_suite->terms, &cur_term->list);
Willy Tarreau74b98a82007-06-16 19:35:18 +02001052 neg = 0;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001053 }
1054
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001055 cond->val |= suite_val;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001056 return cond;
1057
1058 out_free_term:
1059 free(cur_term);
1060 out_free_suite:
1061 prune_acl_cond(cond);
1062 free(cond);
1063 out_return:
1064 return NULL;
1065}
1066
Willy Tarreau2bbba412010-01-28 16:48:33 +01001067/* Builds an ACL condition starting at the if/unless keyword. The complete
1068 * condition is returned. NULL is returned in case of error or if the first
1069 * word is neither "if" nor "unless". It automatically sets the file name and
Willy Tarreau25320b22013-03-24 07:22:08 +01001070 * the line number in the condition for better error reporting, and sets the
1071 * HTTP intiailization requirements in the proxy. If <err> is not NULL, it will
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001072 * be filled with a pointer to an error message in case of error, that the
1073 * caller is responsible for freeing. The initial location must either be
1074 * freeable or NULL.
Willy Tarreau2bbba412010-01-28 16:48:33 +01001075 */
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001076struct acl_cond *build_acl_cond(const char *file, int line, struct proxy *px, const char **args, char **err)
Willy Tarreau2bbba412010-01-28 16:48:33 +01001077{
Willy Tarreau0cba6072013-11-28 22:21:02 +01001078 enum acl_cond_pol pol = ACL_COND_NONE;
Willy Tarreau2bbba412010-01-28 16:48:33 +01001079 struct acl_cond *cond = NULL;
1080
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001081 if (err)
1082 *err = NULL;
1083
Willy Tarreau2bbba412010-01-28 16:48:33 +01001084 if (!strcmp(*args, "if")) {
1085 pol = ACL_COND_IF;
1086 args++;
1087 }
1088 else if (!strcmp(*args, "unless")) {
1089 pol = ACL_COND_UNLESS;
1090 args++;
1091 }
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001092 else {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001093 memprintf(err, "conditions must start with either 'if' or 'unless'");
Willy Tarreau2bbba412010-01-28 16:48:33 +01001094 return NULL;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001095 }
Willy Tarreau2bbba412010-01-28 16:48:33 +01001096
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001097 cond = parse_acl_cond(args, &px->acl, pol, err, &px->conf.args, file, line);
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001098 if (!cond) {
1099 /* note that parse_acl_cond must have filled <err> here */
Willy Tarreau2bbba412010-01-28 16:48:33 +01001100 return NULL;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001101 }
Willy Tarreau2bbba412010-01-28 16:48:33 +01001102
1103 cond->file = file;
1104 cond->line = line;
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001105 px->http_needed |= !!(cond->use & SMP_USE_HTTP_ANY);
Willy Tarreau2bbba412010-01-28 16:48:33 +01001106 return cond;
1107}
1108
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01001109/* Execute condition <cond> and return either ACL_TEST_FAIL, ACL_TEST_MISS or
1110 * ACL_TEST_PASS depending on the test results. ACL_TEST_MISS may only be
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001111 * returned if <opt> does not contain SMP_OPT_FINAL, indicating that incomplete
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01001112 * data is being examined. The function automatically sets SMP_OPT_ITERATE. This
1113 * function only computes the condition, it does not apply the polarity required
1114 * by IF/UNLESS, it's up to the caller to do this using something like this :
Willy Tarreau11382812008-07-09 16:18:21 +02001115 *
1116 * res = acl_pass(res);
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01001117 * if (res == ACL_TEST_MISS)
Willy Tarreaub6866442008-07-14 23:54:42 +02001118 * return 0;
Willy Tarreau11382812008-07-09 16:18:21 +02001119 * if (cond->pol == ACL_COND_UNLESS)
1120 * res = !res;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001121 */
Willy Tarreau0cba6072013-11-28 22:21:02 +01001122enum acl_test_res acl_exec_cond(struct acl_cond *cond, struct proxy *px, struct session *l4, void *l7, unsigned int opt)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001123{
1124 __label__ fetch_next;
1125 struct acl_term_suite *suite;
1126 struct acl_term *term;
1127 struct acl_expr *expr;
1128 struct acl *acl;
Willy Tarreau37406352012-04-23 16:16:37 +02001129 struct sample smp;
Willy Tarreau0cba6072013-11-28 22:21:02 +01001130 enum acl_test_res acl_res, suite_res, cond_res;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001131
Willy Tarreau7a777ed2012-04-26 11:44:02 +02001132 /* ACLs are iterated over all values, so let's always set the flag to
1133 * indicate this to the fetch functions.
1134 */
1135 opt |= SMP_OPT_ITERATE;
1136
Willy Tarreau11382812008-07-09 16:18:21 +02001137 /* We're doing a logical OR between conditions so we initialize to FAIL.
1138 * The MISS status is propagated down from the suites.
1139 */
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01001140 cond_res = ACL_TEST_FAIL;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001141 list_for_each_entry(suite, &cond->suites, list) {
Willy Tarreau11382812008-07-09 16:18:21 +02001142 /* Evaluate condition suite <suite>. We stop at the first term
Willy Tarreau0cba6072013-11-28 22:21:02 +01001143 * which returns ACL_TEST_FAIL. The MISS status is still propagated
Willy Tarreau11382812008-07-09 16:18:21 +02001144 * in case of uncertainty in the result.
Willy Tarreaua84d3742007-05-07 00:36:48 +02001145 */
1146
1147 /* we're doing a logical AND between terms, so we must set the
1148 * initial value to PASS.
1149 */
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01001150 suite_res = ACL_TEST_PASS;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001151 list_for_each_entry(term, &suite->terms, list) {
1152 acl = term->acl;
1153
1154 /* FIXME: use cache !
1155 * check acl->cache_idx for this.
1156 */
1157
1158 /* ACL result not cached. Let's scan all the expressions
1159 * and use the first one to match.
1160 */
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01001161 acl_res = ACL_TEST_FAIL;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001162 list_for_each_entry(expr, &acl->expr, list) {
Willy Tarreaud41f8d82007-06-10 10:06:18 +02001163 /* we need to reset context and flags */
Willy Tarreau37406352012-04-23 16:16:37 +02001164 memset(&smp, 0, sizeof(smp));
Willy Tarreaua84d3742007-05-07 00:36:48 +02001165 fetch_next:
Thierry FOURNIER348971e2013-11-21 10:50:10 +01001166 if (!sample_process(px, l4, l7, opt, expr->smp, &smp)) {
Willy Tarreaub6866442008-07-14 23:54:42 +02001167 /* maybe we could not fetch because of missing data */
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001168 if (smp.flags & SMP_F_MAY_CHANGE && !(opt & SMP_OPT_FINAL))
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01001169 acl_res |= ACL_TEST_MISS;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001170 continue;
Willy Tarreaub6866442008-07-14 23:54:42 +02001171 }
Willy Tarreauc4262962010-05-10 23:42:40 +02001172
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01001173 acl_res |= pat2acl(pattern_exec_match(&expr->pat, &smp, 0));
Willy Tarreaua84d3742007-05-07 00:36:48 +02001174 /*
Willy Tarreau11382812008-07-09 16:18:21 +02001175 * OK now acl_res holds the result of this expression
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01001176 * as one of ACL_TEST_FAIL, ACL_TEST_MISS or ACL_TEST_PASS.
Willy Tarreaua84d3742007-05-07 00:36:48 +02001177 *
Willy Tarreau11382812008-07-09 16:18:21 +02001178 * Then if (!MISS) we can cache the result, and put
Willy Tarreau37406352012-04-23 16:16:37 +02001179 * (smp.flags & SMP_F_VOLATILE) in the cache flags.
Willy Tarreaua84d3742007-05-07 00:36:48 +02001180 *
1181 * FIXME: implement cache.
1182 *
1183 */
1184
Willy Tarreau11382812008-07-09 16:18:21 +02001185 /* we're ORing these terms, so a single PASS is enough */
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01001186 if (acl_res == ACL_TEST_PASS)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001187 break;
1188
Willy Tarreau37406352012-04-23 16:16:37 +02001189 if (smp.flags & SMP_F_NOT_LAST)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001190 goto fetch_next;
Willy Tarreaub6866442008-07-14 23:54:42 +02001191
1192 /* sometimes we know the fetched data is subject to change
1193 * later and give another chance for a new match (eg: request
1194 * size, time, ...)
1195 */
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001196 if (smp.flags & SMP_F_MAY_CHANGE && !(opt & SMP_OPT_FINAL))
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01001197 acl_res |= ACL_TEST_MISS;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001198 }
1199 /*
1200 * Here we have the result of an ACL (cached or not).
1201 * ACLs are combined, negated or not, to form conditions.
1202 */
1203
Willy Tarreaua84d3742007-05-07 00:36:48 +02001204 if (term->neg)
Willy Tarreau11382812008-07-09 16:18:21 +02001205 acl_res = acl_neg(acl_res);
Willy Tarreaua84d3742007-05-07 00:36:48 +02001206
1207 suite_res &= acl_res;
Willy Tarreau11382812008-07-09 16:18:21 +02001208
Willy Tarreau79c412b2013-10-30 19:30:32 +01001209 /* we're ANDing these terms, so a single FAIL or MISS is enough */
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01001210 if (suite_res != ACL_TEST_PASS)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001211 break;
1212 }
1213 cond_res |= suite_res;
Willy Tarreau11382812008-07-09 16:18:21 +02001214
1215 /* we're ORing these terms, so a single PASS is enough */
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01001216 if (cond_res == ACL_TEST_PASS)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001217 break;
1218 }
Willy Tarreau11382812008-07-09 16:18:21 +02001219 return cond_res;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001220}
1221
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001222/* Returns a pointer to the first ACL conflicting with usage at place <where>
1223 * which is one of the SMP_VAL_* bits indicating a check place, or NULL if
1224 * no conflict is found. Only full conflicts are detected (ACL is not usable).
1225 * Use the next function to check for useless keywords.
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001226 */
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001227const struct acl *acl_cond_conflicts(const struct acl_cond *cond, unsigned int where)
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001228{
1229 struct acl_term_suite *suite;
1230 struct acl_term *term;
1231 struct acl *acl;
1232
1233 list_for_each_entry(suite, &cond->suites, list) {
1234 list_for_each_entry(term, &suite->terms, list) {
1235 acl = term->acl;
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001236 if (!(acl->val & where))
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001237 return acl;
1238 }
1239 }
1240 return NULL;
1241}
1242
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001243/* Returns a pointer to the first ACL and its first keyword to conflict with
1244 * usage at place <where> which is one of the SMP_VAL_* bits indicating a check
1245 * place. Returns true if a conflict is found, with <acl> and <kw> set (if non
1246 * null), or false if not conflict is found. The first useless keyword is
1247 * returned.
1248 */
Willy Tarreau93fddf12013-03-31 22:59:32 +02001249int acl_cond_kw_conflicts(const struct acl_cond *cond, unsigned int where, struct acl const **acl, char const **kw)
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001250{
1251 struct acl_term_suite *suite;
1252 struct acl_term *term;
1253 struct acl_expr *expr;
1254
1255 list_for_each_entry(suite, &cond->suites, list) {
1256 list_for_each_entry(term, &suite->terms, list) {
1257 list_for_each_entry(expr, &term->acl->expr, list) {
Thierry FOURNIER348971e2013-11-21 10:50:10 +01001258 if (!(expr->smp->fetch->val & where)) {
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001259 if (acl)
1260 *acl = term->acl;
1261 if (kw)
1262 *kw = expr->kw;
1263 return 1;
1264 }
1265 }
1266 }
1267 }
1268 return 0;
1269}
1270
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001271/*
1272 * Find targets for userlist and groups in acl. Function returns the number
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001273 * of errors or OK if everything is fine. It must be called only once sample
1274 * fetch arguments have been resolved (after smp_resolve_args()).
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001275 */
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001276int acl_find_targets(struct proxy *p)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001277{
1278
1279 struct acl *acl;
1280 struct acl_expr *expr;
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001281 struct pattern_list *pattern;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001282 int cfgerr = 0;
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001283 struct pattern_expr_list *pexp;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001284
1285 list_for_each_entry(acl, &p->acl, list) {
1286 list_for_each_entry(expr, &acl->expr, list) {
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001287 if (!strcmp(expr->kw, "http_auth_group")) {
1288 /* Note: the ARGT_USR argument may only have been resolved earlier
1289 * by smp_resolve_args().
1290 */
Thierry FOURNIER348971e2013-11-21 10:50:10 +01001291 if (expr->smp->arg_p->unresolved) {
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001292 Alert("Internal bug in proxy %s: %sacl %s %s() makes use of unresolved userlist '%s'. Please report this.\n",
Thierry FOURNIER348971e2013-11-21 10:50:10 +01001293 p->id, *acl->name ? "" : "anonymous ", acl->name, expr->kw, expr->smp->arg_p->data.str.str);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001294 cfgerr++;
Willy Tarreau496aa012012-06-01 10:38:29 +02001295 continue;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001296 }
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001297
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001298 if (LIST_ISEMPTY(&expr->pat.head)) {
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001299 Alert("proxy %s: acl %s %s(): no groups specified.\n",
Willy Tarreau93fddf12013-03-31 22:59:32 +02001300 p->id, acl->name, expr->kw);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001301 cfgerr++;
1302 continue;
1303 }
1304
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01001305 /* For each pattern, check if the group exists. */
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001306 list_for_each_entry(pexp, &expr->pat.head, list) {
1307 if (LIST_ISEMPTY(&pexp->expr->patterns)) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001308 Alert("proxy %s: acl %s %s(): no groups specified.\n",
1309 p->id, acl->name, expr->kw);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001310 cfgerr++;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001311 continue;
1312 }
1313
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001314 list_for_each_entry(pattern, &pexp->expr->patterns, list) {
1315 /* this keyword only has one argument */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001316 if (!check_group(expr->smp->arg_p->data.usr, pattern->pat.ptr.str)) {
1317 Alert("proxy %s: acl %s %s(): invalid group '%s'.\n",
1318 p->id, acl->name, expr->kw, pattern->pat.ptr.str);
1319 cfgerr++;
1320 }
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001321 }
1322 }
1323 }
1324 }
1325 }
1326
1327 return cfgerr;
1328}
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001329
Willy Tarreau8ed669b2013-01-11 15:49:37 +01001330/* initializes ACLs by resolving the sample fetch names they rely upon.
1331 * Returns 0 on success, otherwise an error.
1332 */
1333int init_acl()
1334{
1335 int err = 0;
1336 int index;
1337 const char *name;
1338 struct acl_kw_list *kwl;
1339 struct sample_fetch *smp;
1340
1341 list_for_each_entry(kwl, &acl_keywords.list, list) {
1342 for (index = 0; kwl->kw[index].kw != NULL; index++) {
1343 name = kwl->kw[index].fetch_kw;
1344 if (!name)
1345 name = kwl->kw[index].kw;
1346
1347 smp = find_sample_fetch(name, strlen(name));
1348 if (!smp) {
1349 Alert("Critical internal error: ACL keyword '%s' relies on sample fetch '%s' which was not registered!\n",
1350 kwl->kw[index].kw, name);
1351 err++;
1352 continue;
1353 }
1354 kwl->kw[index].smp = smp;
1355 }
1356 }
1357 return err;
1358}
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001359
Willy Tarreaua84d3742007-05-07 00:36:48 +02001360/************************************************************************/
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001361/* All supported sample and ACL keywords must be declared here. */
1362/************************************************************************/
1363
1364/* Note: must not be declared <const> as its list will be overwritten.
Willy Tarreau61612d42012-04-19 18:42:05 +02001365 * Please take care of keeping this list alphabetically sorted.
1366 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02001367static struct acl_kw_list acl_kws = {ILH, {
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001368 { /* END */ },
Willy Tarreaua84d3742007-05-07 00:36:48 +02001369}};
1370
Willy Tarreaua84d3742007-05-07 00:36:48 +02001371__attribute__((constructor))
1372static void __acl_init(void)
1373{
Willy Tarreaua84d3742007-05-07 00:36:48 +02001374 acl_register_keywords(&acl_kws);
1375}
1376
1377
1378/*
1379 * Local variables:
1380 * c-indent-level: 8
1381 * c-basic-offset: 8
1382 * End:
1383 */