blob: 55761873690561fd30c2ccf654f6968b5934f77e [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;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200163
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100164 /* First, we look for an ACL keyword. And if we don't find one, then
165 * we look for a sample fetch expression starting with a sample fetch
166 * keyword.
Willy Tarreaubef91e72013-03-31 23:14:46 +0200167 */
Willy Tarreau131b4662013-12-13 01:08:36 +0100168
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100169 al->ctx = ARGC_ACL; // to report errors while resolving args late
Willy Tarreau131b4662013-12-13 01:08:36 +0100170 al->kw = *args;
171 al->conv = NULL;
172
Willy Tarreaua84d3742007-05-07 00:36:48 +0200173 aclkw = find_acl_kw(args[0]);
Willy Tarreau20490922014-03-17 18:04:27 +0100174 if (aclkw) {
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100175 /* OK we have a real ACL keyword */
Willy Tarreau9987ea92013-06-11 21:09:06 +0200176
Willy Tarreau131b4662013-12-13 01:08:36 +0100177 /* build new sample expression for this ACL */
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100178 smp = calloc(1, sizeof(struct sample_expr));
179 if (!smp) {
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100180 memprintf(err, "out of memory when parsing ACL expression");
181 goto out_return;
182 }
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100183 LIST_INIT(&(smp->conv_exprs));
184 smp->fetch = aclkw->smp;
185 smp->arg_p = empty_arg_list;
Willy Tarreau34db1082012-04-19 17:16:54 +0200186
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100187 /* look for the begining of the subject arguments */
Willy Tarreau131b4662013-12-13 01:08:36 +0100188 for (arg = args[0]; *arg && *arg != '(' && *arg != ','; arg++);
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100189
Willy Tarreau131b4662013-12-13 01:08:36 +0100190 endt = arg;
191 if (*endt == '(') {
192 /* look for the end of this term and skip the opening parenthesis */
193 endt = ++arg;
194 while (*endt && *endt != ')')
195 endt++;
196 if (*endt != ')') {
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100197 memprintf(err, "missing closing ')' after arguments to ACL keyword '%s'", aclkw->kw);
198 goto out_free_smp;
Willy Tarreau131b4662013-12-13 01:08:36 +0100199 }
200 }
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100201
Willy Tarreau131b4662013-12-13 01:08:36 +0100202 /* At this point, we have :
203 * - args[0] : beginning of the keyword
204 * - arg : end of the keyword, first character not part of keyword
205 * nor the opening parenthesis (so first character of args
206 * if present).
207 * - endt : end of the term (=arg or last parenthesis if args are present)
208 */
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100209 nbargs = make_arg_list(arg, endt - arg, smp->fetch->arg_mask, &smp->arg_p,
Willy Tarreau131b4662013-12-13 01:08:36 +0100210 err, NULL, NULL, al);
211 if (nbargs < 0) {
212 /* note that make_arg_list will have set <err> here */
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100213 memprintf(err, "ACL keyword '%s' : %s", aclkw->kw, *err);
214 goto out_free_smp;
Willy Tarreau131b4662013-12-13 01:08:36 +0100215 }
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100216
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100217 if (!smp->arg_p) {
218 smp->arg_p = empty_arg_list;
Willy Tarreau131b4662013-12-13 01:08:36 +0100219 }
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100220 else if (smp->fetch->val_args && !smp->fetch->val_args(smp->arg_p, err)) {
Willy Tarreau131b4662013-12-13 01:08:36 +0100221 /* invalid keyword argument, error must have been
222 * set by val_args().
223 */
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100224 memprintf(err, "in argument to '%s', %s", aclkw->kw, *err);
225 goto out_free_smp;
Willy Tarreau131b4662013-12-13 01:08:36 +0100226 }
227 arg = endt;
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100228
Willy Tarreau131b4662013-12-13 01:08:36 +0100229 /* look for the begining of the converters list. Those directly attached
230 * to the ACL keyword are found just after <arg> which points to the comma.
231 */
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100232 prev_type = smp->fetch->out_type;
Willy Tarreau131b4662013-12-13 01:08:36 +0100233 while (*arg) {
234 struct sample_conv *conv;
235 struct sample_conv_expr *conv_expr;
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100236
Willy Tarreau131b4662013-12-13 01:08:36 +0100237 if (*arg == ')') /* skip last closing parenthesis */
238 arg++;
239
240 if (*arg && *arg != ',') {
241 if (ckw)
242 memprintf(err, "ACL keyword '%s' : missing comma after conv keyword '%s'.",
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100243 aclkw->kw, ckw);
Willy Tarreau131b4662013-12-13 01:08:36 +0100244 else
245 memprintf(err, "ACL keyword '%s' : missing comma after fetch keyword.",
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100246 aclkw->kw);
247 goto out_free_smp;
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200248 }
Willy Tarreauae52f062012-04-26 12:13:35 +0200249
Willy Tarreau131b4662013-12-13 01:08:36 +0100250 while (*arg == ',') /* then trailing commas */
251 arg++;
Willy Tarreau2e845be2012-10-19 19:49:09 +0200252
Willy Tarreau131b4662013-12-13 01:08:36 +0100253 begw = arg; /* start of conv keyword */
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100254
Willy Tarreau131b4662013-12-13 01:08:36 +0100255 if (!*begw)
256 /* none ? end of converters */
257 break;
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100258
Willy Tarreau131b4662013-12-13 01:08:36 +0100259 for (endw = begw; *endw && *endw != '(' && *endw != ','; endw++);
Willy Tarreau9ca69362013-10-22 19:10:06 +0200260
Willy Tarreau131b4662013-12-13 01:08:36 +0100261 free(ckw);
262 ckw = my_strndup(begw, endw - begw);
Willy Tarreauf75d0082013-04-07 21:20:44 +0200263
Willy Tarreau131b4662013-12-13 01:08:36 +0100264 conv = find_sample_conv(begw, endw - begw);
265 if (!conv) {
266 /* Unknown converter method */
267 memprintf(err, "ACL keyword '%s' : unknown conv method '%s'.",
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100268 aclkw->kw, ckw);
269 goto out_free_smp;
Willy Tarreau131b4662013-12-13 01:08:36 +0100270 }
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100271
Willy Tarreau131b4662013-12-13 01:08:36 +0100272 arg = endw;
273 if (*arg == '(') {
274 /* look for the end of this term */
275 while (*arg && *arg != ')')
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100276 arg++;
Willy Tarreau131b4662013-12-13 01:08:36 +0100277 if (*arg != ')') {
278 memprintf(err, "ACL keyword '%s' : syntax error: missing ')' after conv keyword '%s'.",
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100279 aclkw->kw, ckw);
280 goto out_free_smp;
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100281 }
Willy Tarreau131b4662013-12-13 01:08:36 +0100282 }
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100283
Willy Tarreau131b4662013-12-13 01:08:36 +0100284 if (conv->in_type >= SMP_TYPES || conv->out_type >= SMP_TYPES) {
285 memprintf(err, "ACL keyword '%s' : returns type of conv method '%s' is unknown.",
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100286 aclkw->kw, ckw);
287 goto out_free_smp;
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 impossible type conversion */
291 if (!sample_casts[prev_type][conv->in_type]) {
292 memprintf(err, "ACL keyword '%s' : conv method '%s' cannot be applied.",
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100293 aclkw->kw, ckw);
294 goto out_free_smp;
Willy Tarreau131b4662013-12-13 01:08:36 +0100295 }
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100296
Willy Tarreau131b4662013-12-13 01:08:36 +0100297 prev_type = conv->out_type;
298 conv_expr = calloc(1, sizeof(struct sample_conv_expr));
299 if (!conv_expr)
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100300 goto out_free_smp;
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100301
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100302 LIST_ADDQ(&(smp->conv_exprs), &(conv_expr->list));
Willy Tarreau131b4662013-12-13 01:08:36 +0100303 conv_expr->conv = conv;
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100304
Willy Tarreau131b4662013-12-13 01:08:36 +0100305 if (arg != endw) {
Willy Tarreau131b4662013-12-13 01:08:36 +0100306 int err_arg;
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100307
Willy Tarreau131b4662013-12-13 01:08:36 +0100308 if (!conv->arg_mask) {
309 memprintf(err, "ACL keyword '%s' : conv method '%s' does not support any args.",
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100310 aclkw->kw, ckw);
311 goto out_free_smp;
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100312 }
313
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100314 al->kw = smp->fetch->kw;
Willy Tarreau131b4662013-12-13 01:08:36 +0100315 al->conv = conv_expr->conv->kw;
Willy Tarreauadaddc22013-12-13 01:30:22 +0100316 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 +0100317 memprintf(err, "ACL keyword '%s' : invalid arg %d in conv method '%s' : %s.",
Willy Tarreauadaddc22013-12-13 01:30:22 +0100318 aclkw->kw, err_arg+1, ckw, *err);
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100319 goto out_free_smp;
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100320 }
321
Willy Tarreau131b4662013-12-13 01:08:36 +0100322 if (!conv_expr->arg_p)
323 conv_expr->arg_p = empty_arg_list;
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100324
Willy Tarreauadaddc22013-12-13 01:30:22 +0100325 if (conv->val_args && !conv->val_args(conv_expr->arg_p, conv, err)) {
Willy Tarreau131b4662013-12-13 01:08:36 +0100326 memprintf(err, "ACL keyword '%s' : invalid args in conv method '%s' : %s.",
Willy Tarreauadaddc22013-12-13 01:30:22 +0100327 aclkw->kw, ckw, *err);
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100328 goto out_free_smp;
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100329 }
330 }
Willy Tarreau131b4662013-12-13 01:08:36 +0100331 else if (ARGM(conv->arg_mask)) {
332 memprintf(err, "ACL keyword '%s' : missing args for conv method '%s'.",
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100333 aclkw->kw, ckw);
334 goto out_free_smp;
Willy Tarreau131b4662013-12-13 01:08:36 +0100335 }
Willy Tarreau61612d42012-04-19 18:42:05 +0200336 }
Willy Tarreaua84d3742007-05-07 00:36:48 +0200337 }
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100338 else {
339 /* This is not an ACL keyword, so we hope this is a sample fetch
340 * keyword that we're going to transparently use as an ACL. If
341 * so, we retrieve a completely parsed expression with args and
342 * convs already done.
343 */
344 smp = sample_parse_expr((char **)args, &idx, err, al);
345 if (!smp) {
346 memprintf(err, "%s in ACL expression '%s'", *err, *args);
347 goto out_return;
348 }
349 }
350
351 expr = (struct acl_expr *)calloc(1, sizeof(*expr));
352 if (!expr) {
353 memprintf(err, "out of memory when parsing ACL expression");
354 goto out_return;
355 }
356
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100357 pattern_init_head(&expr->pat);
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100358
359 expr->kw = aclkw ? aclkw->kw : smp->fetch->kw;
360 expr->pat.parse = aclkw ? aclkw->parse : NULL;
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100361 expr->pat.index = aclkw ? aclkw->index : NULL;
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100362 expr->pat.match = aclkw ? aclkw->match : NULL;
Thierry FOURNIERb1136502014-01-15 11:38:49 +0100363 expr->pat.delete = aclkw ? aclkw->delete : NULL;
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +0100364 expr->pat.prune = aclkw ? aclkw->prune : NULL;
Thierry FOURNIER5d344082014-01-27 14:19:53 +0100365 expr->pat.expect_type = smp->fetch->out_type;
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100366 expr->smp = smp;
367 smp = NULL;
368
369 if (!expr->pat.parse) {
370 /* some types can be automatically converted */
371
372 switch (expr->smp ? expr->smp->fetch->out_type : aclkw->smp->out_type) {
373 case SMP_T_BOOL:
374 expr->pat.parse = pat_parse_fcts[PAT_MATCH_BOOL];
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100375 expr->pat.index = pat_index_fcts[PAT_MATCH_BOOL];
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100376 expr->pat.match = pat_match_fcts[PAT_MATCH_BOOL];
Thierry FOURNIERb1136502014-01-15 11:38:49 +0100377 expr->pat.delete = pat_delete_fcts[PAT_MATCH_BOOL];
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +0100378 expr->pat.prune = pat_prune_fcts[PAT_MATCH_BOOL];
Thierry FOURNIER5d344082014-01-27 14:19:53 +0100379 expr->pat.expect_type = pat_match_types[PAT_MATCH_BOOL];
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100380 break;
381 case SMP_T_SINT:
382 case SMP_T_UINT:
383 expr->pat.parse = pat_parse_fcts[PAT_MATCH_INT];
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100384 expr->pat.index = pat_index_fcts[PAT_MATCH_INT];
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100385 expr->pat.match = pat_match_fcts[PAT_MATCH_INT];
Thierry FOURNIERb1136502014-01-15 11:38:49 +0100386 expr->pat.delete = pat_delete_fcts[PAT_MATCH_INT];
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +0100387 expr->pat.prune = pat_prune_fcts[PAT_MATCH_INT];
Thierry FOURNIER5d344082014-01-27 14:19:53 +0100388 expr->pat.expect_type = pat_match_types[PAT_MATCH_INT];
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100389 break;
390 case SMP_T_IPV4:
391 case SMP_T_IPV6:
392 expr->pat.parse = pat_parse_fcts[PAT_MATCH_IP];
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100393 expr->pat.index = pat_index_fcts[PAT_MATCH_IP];
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100394 expr->pat.match = pat_match_fcts[PAT_MATCH_IP];
Thierry FOURNIERb1136502014-01-15 11:38:49 +0100395 expr->pat.delete = pat_delete_fcts[PAT_MATCH_IP];
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +0100396 expr->pat.prune = pat_prune_fcts[PAT_MATCH_IP];
Thierry FOURNIER5d344082014-01-27 14:19:53 +0100397 expr->pat.expect_type = pat_match_types[PAT_MATCH_IP];
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100398 break;
399 }
400 }
Willy Tarreaua84d3742007-05-07 00:36:48 +0200401
Willy Tarreau3c3dfd52013-11-04 18:09:12 +0100402 /* Additional check to protect against common mistakes */
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100403 cur_type = smp_expr_output_type(expr->smp);
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +0100404 if (expr->pat.parse && cur_type != SMP_T_BOOL && !*args[1]) {
Willy Tarreau3c3dfd52013-11-04 18:09:12 +0100405 Warning("parsing acl keyword '%s' :\n"
406 " no pattern to match against were provided, so this ACL will never match.\n"
407 " If this is what you intended, please add '--' to get rid of this warning.\n"
408 " If you intended to match only for existence, please use '-m found'.\n"
409 " If you wanted to force an int to match as a bool, please use '-m bool'.\n"
410 "\n",
411 args[0]);
412 }
413
Willy Tarreaua84d3742007-05-07 00:36:48 +0200414 args++;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200415
416 /* check for options before patterns. Supported options are :
417 * -i : ignore case for all patterns by default
418 * -f : read patterns from those files
Willy Tarreau5adeda12013-03-31 22:13:34 +0200419 * -m : force matching method (must be used before -f)
Thierry FOURNIER3534d882014-01-20 17:01:44 +0100420 * -u : force the unique id of the acl
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200421 * -- : everything after this is not an option
422 */
423 patflags = 0;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100424 is_loaded = 0;
Thierry FOURNIER3534d882014-01-20 17:01:44 +0100425 unique_id = -1;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200426 while (**args == '-') {
427 if ((*args)[1] == 'i')
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100428 patflags |= PAT_F_IGNORE_CASE;
Thierry FOURNIER3534d882014-01-20 17:01:44 +0100429 else if ((*args)[1] == 'u') {
430 unique_id = strtol(args[1], &error, 10);
431 if (*error != '\0') {
432 memprintf(err, "the argument of -u must be an integer");
433 goto out_free_expr;
434 }
435
436 /* Check if this id is really unique. */
437 if (pat_ref_lookupid(unique_id)) {
438 memprintf(err, "the id is already used");
439 goto out_free_expr;
440 }
441
442 args++;
443 }
Willy Tarreau2b5285d2010-05-09 23:45:24 +0200444 else if ((*args)[1] == 'f') {
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +0100445 if (!expr->pat.parse) {
Willy Tarreau9987ea92013-06-11 21:09:06 +0200446 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 +0200447 goto out_free_expr;
448 }
449
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +0100450 /* Create displayed reference */
451 snprintf(trash.str, trash.size, "acl(s) loaded from file '%s'", args[1]);
452 trash.str[trash.size - 1] = '\0';
453
Thierry FOURNIER39bef452014-01-29 13:29:45 +0100454 if (!pattern_read_from_file(&expr->pat, PAT_REF_ACL, args[1], patflags | PAT_F_FROM_FILE, 0, err, trash.str))
Willy Tarreau2b5285d2010-05-09 23:45:24 +0200455 goto out_free_expr;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100456 is_loaded = 1;
Willy Tarreau5adeda12013-03-31 22:13:34 +0200457 args++;
458 }
459 else if ((*args)[1] == 'm') {
460 int idx;
461
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100462 if (is_loaded) {
Willy Tarreau5adeda12013-03-31 22:13:34 +0200463 memprintf(err, "'-m' must only be specified before patterns and files in parsing ACL expression");
464 goto out_free_expr;
465 }
466
Willy Tarreau6f8fe312013-11-28 22:24:25 +0100467 idx = pat_find_match_name(args[1]);
Willy Tarreau5adeda12013-03-31 22:13:34 +0200468 if (idx < 0) {
469 memprintf(err, "unknown matching method '%s' when parsing ACL expression", args[1]);
470 goto out_free_expr;
471 }
472
473 /* Note: -m found is always valid, bool/int are compatible, str/bin/reg/len are compatible */
Thierry FOURNIERe3ded592013-12-06 15:36:54 +0100474 if (!sample_casts[cur_type][pat_match_types[idx]]) {
Willy Tarreau93fddf12013-03-31 22:59:32 +0200475 memprintf(err, "matching method '%s' cannot be used with fetch keyword '%s'", args[1], expr->kw);
Willy Tarreau5adeda12013-03-31 22:13:34 +0200476 goto out_free_expr;
477 }
Thierry FOURNIERe3ded592013-12-06 15:36:54 +0100478 expr->pat.parse = pat_parse_fcts[idx];
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100479 expr->pat.index = pat_index_fcts[idx];
Thierry FOURNIERe3ded592013-12-06 15:36:54 +0100480 expr->pat.match = pat_match_fcts[idx];
Thierry FOURNIERb1136502014-01-15 11:38:49 +0100481 expr->pat.delete = pat_delete_fcts[idx];
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +0100482 expr->pat.prune = pat_prune_fcts[idx];
Thierry FOURNIER5d344082014-01-27 14:19:53 +0100483 expr->pat.expect_type = pat_match_types[idx];
Willy Tarreau2b5285d2010-05-09 23:45:24 +0200484 args++;
485 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200486 else if ((*args)[1] == '-') {
487 args++;
488 break;
489 }
490 else
491 break;
492 args++;
493 }
494
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +0100495 if (!expr->pat.parse) {
Willy Tarreau9987ea92013-06-11 21:09:06 +0200496 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 +0200497 goto out_free_expr;
498 }
499
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +0100500 /* Create displayed reference */
501 snprintf(trash.str, trash.size, "acl '%s' file '%s' line %d", expr->kw, file, line);
502 trash.str[trash.size - 1] = '\0';
503
Thierry FOURNIER3534d882014-01-20 17:01:44 +0100504 /* Create new patern reference. */
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +0100505 ref = pat_ref_newid(unique_id, trash.str, PAT_REF_ACL);
Thierry FOURNIER3534d882014-01-20 17:01:44 +0100506 if (!ref) {
507 memprintf(err, "memory error");
508 goto out_free_expr;
509 }
510
511 /* Create new pattern expression associated to this reference. */
512 pattern_expr = pattern_new_expr(&expr->pat, ref, err);
513 if (!pattern_expr)
514 goto out_free_expr;
515
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200516 /* now parse all patterns */
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100517 while (**args) {
518 arg = *args;
519
520 /* Compatibility layer. Each pattern can parse only one string per pattern,
521 * but the pat_parser_int() and pat_parse_dotted_ver() parsers were need
522 * optionnaly two operators. The first operator is the match method: eq,
523 * le, lt, ge and gt. pat_parse_int() and pat_parse_dotted_ver() functions
524 * can have a compatibility syntax based on ranges:
525 *
526 * pat_parse_int():
527 *
528 * "eq x" -> "x" or "x:x"
529 * "le x" -> ":x"
530 * "lt x" -> ":y" (with y = x - 1)
531 * "ge x" -> "x:"
532 * "gt x" -> "y:" (with y = x + 1)
533 *
534 * pat_parse_dotted_ver():
535 *
536 * "eq x.y" -> "x.y" or "x.y:x.y"
537 * "le x.y" -> ":x.y"
538 * "lt x.y" -> ":w.z" (with w.z = x.y - 1)
539 * "ge x.y" -> "x.y:"
540 * "gt x.y" -> "w.z:" (with w.z = x.y + 1)
541 *
542 * If y is not present, assume that is "0".
543 *
544 * The syntax eq, le, lt, ge and gt are proper to the acl syntax. The
545 * following block of code detect the operator, and rewrite each value
546 * in parsable string.
547 */
548 if (expr->pat.parse == pat_parse_int ||
549 expr->pat.parse == pat_parse_dotted_ver) {
550 /* Check for operator. If the argument is operator, memorise it and
551 * continue to the next argument.
552 */
553 op = get_std_op(arg);
554 if (op != -1) {
555 operator = op;
556 args++;
557 continue;
558 }
559
560 /* Check if the pattern contain ':' or '-' character. */
561 contain_colon = (strchr(arg, ':') || strchr(arg, '-'));
562
563 /* If the pattern contain ':' or '-' character, give it to the parser as is.
564 * If no contain ':' and operator is STD_OP_EQ, give it to the parser as is.
565 * In other case, try to convert the value according with the operator.
566 */
567 if (!contain_colon && operator != STD_OP_EQ) {
568 /* Search '.' separator. */
569 dot = strchr(arg, '.');
570 if (!dot) {
571 have_dot = 0;
572 minor = 0;
573 dot = arg + strlen(arg);
574 }
575 else
576 have_dot = 1;
577
578 /* convert the integer minor part for the pat_parse_dotted_ver() function. */
579 if (expr->pat.parse == pat_parse_dotted_ver && have_dot) {
580 if (strl2llrc(dot+1, strlen(dot+1), &minor) != 0) {
581 memprintf(err, "'%s' is neither a number nor a supported operator", arg);
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100582 goto out_free_expr;
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100583 }
584 if (minor >= 65536) {
585 memprintf(err, "'%s' contains too large a minor value", arg);
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100586 goto out_free_expr;
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100587 }
588 }
589
590 /* convert the integer value for the pat_parse_int() function, and the
591 * integer major part for the pat_parse_dotted_ver() function.
592 */
593 if (strl2llrc(arg, dot - arg, &value) != 0) {
594 memprintf(err, "'%s' is neither a number nor a supported operator", arg);
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100595 goto out_free_expr;
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100596 }
597 if (expr->pat.parse == pat_parse_dotted_ver) {
598 if (value >= 65536) {
599 memprintf(err, "'%s' contains too large a major value", arg);
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100600 goto out_free_expr;
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100601 }
602 value = (value << 16) | (minor & 0xffff);
603 }
604
605 switch (operator) {
606
607 case STD_OP_EQ: /* this case is not possible. */
608 memprintf(err, "internal error");
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100609 goto out_free_expr;
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100610
611 case STD_OP_GT:
612 value++; /* gt = ge + 1 */
613
614 case STD_OP_GE:
615 if (expr->pat.parse == pat_parse_int)
616 snprintf(buffer, NB_LLMAX_STR+NB_LLMAX_STR+2, "%lld:", value);
617 else
618 snprintf(buffer, NB_LLMAX_STR+NB_LLMAX_STR+2, "%lld.%lld:",
619 value >> 16, value & 0xffff);
620 arg = buffer;
621 break;
622
623 case STD_OP_LT:
624 value--; /* lt = le - 1 */
625
626 case STD_OP_LE:
627 if (expr->pat.parse == pat_parse_int)
628 snprintf(buffer, NB_LLMAX_STR+NB_LLMAX_STR+2, ":%lld", value);
629 else
630 snprintf(buffer, NB_LLMAX_STR+NB_LLMAX_STR+2, ":%lld.%lld",
631 value >> 16, value & 0xffff);
632 arg = buffer;
633 break;
634 }
635 }
636 }
637
Thierry FOURNIER3534d882014-01-20 17:01:44 +0100638 /* Add sample to the reference, and try to compile it fior each pattern
639 * using this value.
640 */
641 if (!pat_ref_add(ref, arg, NULL, err))
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100642 goto out_free_expr;
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100643 args++;
644 }
Willy Tarreaua84d3742007-05-07 00:36:48 +0200645
646 return expr;
647
Willy Tarreaua84d3742007-05-07 00:36:48 +0200648 out_free_expr:
649 prune_acl_expr(expr);
650 free(expr);
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100651 free(ckw);
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100652 out_free_smp:
653 free(smp);
Willy Tarreaua84d3742007-05-07 00:36:48 +0200654 out_return:
655 return NULL;
656}
657
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +0200658/* Purge everything in the acl <acl>, then return <acl>. */
659struct acl *prune_acl(struct acl *acl) {
660
661 struct acl_expr *expr, *exprb;
662
663 free(acl->name);
664
665 list_for_each_entry_safe(expr, exprb, &acl->expr, list) {
666 LIST_DEL(&expr->list);
667 prune_acl_expr(expr);
668 free(expr);
669 }
670
671 return acl;
672}
673
Willy Tarreaua84d3742007-05-07 00:36:48 +0200674/* Parse an ACL with the name starting at <args>[0], and with a list of already
675 * known ACLs in <acl>. If the ACL was not in the list, it will be added.
Willy Tarreau2a56c5e2010-03-15 16:13:29 +0100676 * A pointer to that ACL is returned. If the ACL has an empty name, then it's
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200677 * an anonymous one and it won't be merged with any other one. If <err> is not
678 * NULL, it will be filled with an appropriate error. This pointer must be
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200679 * freeable or NULL. <al> is the arg_list serving as a head for unresolved
680 * dependencies.
Willy Tarreaua84d3742007-05-07 00:36:48 +0200681 *
682 * args syntax: <aclname> <acl_expr>
683 */
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +0100684struct acl *parse_acl(const char **args, struct list *known_acl, char **err, struct arg_list *al,
685 const char *file, int line)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200686{
687 __label__ out_return, out_free_acl_expr, out_free_name;
688 struct acl *cur_acl;
689 struct acl_expr *acl_expr;
690 char *name;
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200691 const char *pos;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200692
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200693 if (**args && (pos = invalid_char(*args))) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200694 memprintf(err, "invalid character in ACL name : '%c'", *pos);
Willy Tarreau2e74c3f2007-12-02 18:45:09 +0100695 goto out_return;
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200696 }
Willy Tarreau2e74c3f2007-12-02 18:45:09 +0100697
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +0100698 acl_expr = parse_acl_expr(args + 1, err, al, file, line);
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200699 if (!acl_expr) {
700 /* parse_acl_expr will have filled <err> here */
Willy Tarreaua84d3742007-05-07 00:36:48 +0200701 goto out_return;
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200702 }
Willy Tarreaua84d3742007-05-07 00:36:48 +0200703
Willy Tarreau404e8ab2009-07-26 19:40:40 +0200704 /* Check for args beginning with an opening parenthesis just after the
705 * subject, as this is almost certainly a typo. Right now we can only
706 * emit a warning, so let's do so.
707 */
Krzysztof Piotr Oledzki4cdd8312009-10-05 00:23:35 +0200708 if (!strchr(args[1], '(') && *args[2] == '(')
Willy Tarreau404e8ab2009-07-26 19:40:40 +0200709 Warning("parsing acl '%s' :\n"
710 " matching '%s' for pattern '%s' is likely a mistake and probably\n"
711 " not what you want. Maybe you need to remove the extraneous space before '('.\n"
712 " If you are really sure this is not an error, please insert '--' between the\n"
713 " match and the pattern to make this warning message disappear.\n",
714 args[0], args[1], args[2]);
715
Willy Tarreau2a56c5e2010-03-15 16:13:29 +0100716 if (*args[0])
717 cur_acl = find_acl_by_name(args[0], known_acl);
718 else
719 cur_acl = NULL;
720
Willy Tarreaua84d3742007-05-07 00:36:48 +0200721 if (!cur_acl) {
722 name = strdup(args[0]);
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200723 if (!name) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200724 memprintf(err, "out of memory when parsing ACL");
Willy Tarreaua84d3742007-05-07 00:36:48 +0200725 goto out_free_acl_expr;
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200726 }
Willy Tarreaua84d3742007-05-07 00:36:48 +0200727 cur_acl = (struct acl *)calloc(1, sizeof(*cur_acl));
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200728 if (cur_acl == NULL) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200729 memprintf(err, "out of memory when parsing ACL");
Willy Tarreaua84d3742007-05-07 00:36:48 +0200730 goto out_free_name;
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200731 }
Willy Tarreaua84d3742007-05-07 00:36:48 +0200732
733 LIST_INIT(&cur_acl->expr);
734 LIST_ADDQ(known_acl, &cur_acl->list);
735 cur_acl->name = name;
736 }
737
Willy Tarreaua91d0a52013-03-25 08:12:18 +0100738 /* We want to know what features the ACL needs (typically HTTP parsing),
739 * and where it may be used. If an ACL relies on multiple matches, it is
740 * OK if at least one of them may match in the context where it is used.
741 */
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100742 cur_acl->use |= acl_expr->smp->fetch->use;
743 cur_acl->val |= acl_expr->smp->fetch->val;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200744 LIST_ADDQ(&cur_acl->expr, &acl_expr->list);
745 return cur_acl;
746
747 out_free_name:
748 free(name);
749 out_free_acl_expr:
750 prune_acl_expr(acl_expr);
751 free(acl_expr);
752 out_return:
753 return NULL;
754}
755
Willy Tarreau16fbe822007-06-17 11:54:31 +0200756/* Some useful ACLs provided by default. Only those used are allocated. */
757
758const struct {
759 const char *name;
760 const char *expr[4]; /* put enough for longest expression */
761} default_acl_list[] = {
Willy Tarreau58393e12008-07-20 10:39:22 +0200762 { .name = "TRUE", .expr = {"always_true",""}},
763 { .name = "FALSE", .expr = {"always_false",""}},
Willy Tarreau16fbe822007-06-17 11:54:31 +0200764 { .name = "LOCALHOST", .expr = {"src","127.0.0.1/8",""}},
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200765 { .name = "HTTP", .expr = {"req_proto_http",""}},
Willy Tarreau16fbe822007-06-17 11:54:31 +0200766 { .name = "HTTP_1.0", .expr = {"req_ver","1.0",""}},
767 { .name = "HTTP_1.1", .expr = {"req_ver","1.1",""}},
768 { .name = "METH_CONNECT", .expr = {"method","CONNECT",""}},
769 { .name = "METH_GET", .expr = {"method","GET","HEAD",""}},
770 { .name = "METH_HEAD", .expr = {"method","HEAD",""}},
771 { .name = "METH_OPTIONS", .expr = {"method","OPTIONS",""}},
772 { .name = "METH_POST", .expr = {"method","POST",""}},
773 { .name = "METH_TRACE", .expr = {"method","TRACE",""}},
774 { .name = "HTTP_URL_ABS", .expr = {"url_reg","^[^/:]*://",""}},
775 { .name = "HTTP_URL_SLASH", .expr = {"url_beg","/",""}},
776 { .name = "HTTP_URL_STAR", .expr = {"url","*",""}},
777 { .name = "HTTP_CONTENT", .expr = {"hdr_val(content-length)","gt","0",""}},
Emeric Brunbede3d02009-06-30 17:54:00 +0200778 { .name = "RDP_COOKIE", .expr = {"req_rdp_cookie_cnt","gt","0",""}},
Willy Tarreauc6317702008-07-20 09:29:50 +0200779 { .name = "REQ_CONTENT", .expr = {"req_len","gt","0",""}},
Willy Tarreaub6fb4202008-07-20 11:18:28 +0200780 { .name = "WAIT_END", .expr = {"wait_end",""}},
Willy Tarreau16fbe822007-06-17 11:54:31 +0200781 { .name = NULL, .expr = {""}}
782};
783
784/* Find a default ACL from the default_acl list, compile it and return it.
785 * If the ACL is not found, NULL is returned. In theory, it cannot fail,
786 * except when default ACLs are broken, in which case it will return NULL.
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200787 * If <known_acl> is not NULL, the ACL will be queued at its tail. If <err> is
788 * not NULL, it will be filled with an error message if an error occurs. This
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200789 * pointer must be freeable or NULL. <al> is an arg_list serving as a list head
790 * to report missing dependencies.
Willy Tarreau16fbe822007-06-17 11:54:31 +0200791 */
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200792static struct acl *find_acl_default(const char *acl_name, struct list *known_acl,
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +0100793 char **err, struct arg_list *al,
794 const char *file, int line)
Willy Tarreau16fbe822007-06-17 11:54:31 +0200795{
796 __label__ out_return, out_free_acl_expr, out_free_name;
797 struct acl *cur_acl;
798 struct acl_expr *acl_expr;
799 char *name;
800 int index;
801
802 for (index = 0; default_acl_list[index].name != NULL; index++) {
803 if (strcmp(acl_name, default_acl_list[index].name) == 0)
804 break;
805 }
806
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200807 if (default_acl_list[index].name == NULL) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200808 memprintf(err, "no such ACL : '%s'", acl_name);
Willy Tarreau16fbe822007-06-17 11:54:31 +0200809 return NULL;
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200810 }
Willy Tarreau16fbe822007-06-17 11:54:31 +0200811
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +0100812 acl_expr = parse_acl_expr((const char **)default_acl_list[index].expr, err, al, file, line);
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200813 if (!acl_expr) {
814 /* parse_acl_expr must have filled err here */
Willy Tarreau16fbe822007-06-17 11:54:31 +0200815 goto out_return;
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200816 }
Willy Tarreau16fbe822007-06-17 11:54:31 +0200817
818 name = strdup(acl_name);
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200819 if (!name) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200820 memprintf(err, "out of memory when building default ACL '%s'", acl_name);
Willy Tarreau16fbe822007-06-17 11:54:31 +0200821 goto out_free_acl_expr;
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200822 }
823
Willy Tarreau16fbe822007-06-17 11:54:31 +0200824 cur_acl = (struct acl *)calloc(1, sizeof(*cur_acl));
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200825 if (cur_acl == NULL) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200826 memprintf(err, "out of memory when building default ACL '%s'", acl_name);
Willy Tarreau16fbe822007-06-17 11:54:31 +0200827 goto out_free_name;
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200828 }
Willy Tarreau16fbe822007-06-17 11:54:31 +0200829
830 cur_acl->name = name;
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100831 cur_acl->use |= acl_expr->smp->fetch->use;
832 cur_acl->val |= acl_expr->smp->fetch->val;
Willy Tarreau16fbe822007-06-17 11:54:31 +0200833 LIST_INIT(&cur_acl->expr);
834 LIST_ADDQ(&cur_acl->expr, &acl_expr->list);
835 if (known_acl)
836 LIST_ADDQ(known_acl, &cur_acl->list);
837
838 return cur_acl;
839
840 out_free_name:
841 free(name);
842 out_free_acl_expr:
843 prune_acl_expr(acl_expr);
844 free(acl_expr);
845 out_return:
846 return NULL;
847}
Willy Tarreaua84d3742007-05-07 00:36:48 +0200848
849/* Purge everything in the acl_cond <cond>, then return <cond>. */
850struct acl_cond *prune_acl_cond(struct acl_cond *cond)
851{
852 struct acl_term_suite *suite, *tmp_suite;
853 struct acl_term *term, *tmp_term;
854
855 /* iterate through all term suites and free all terms and all suites */
856 list_for_each_entry_safe(suite, tmp_suite, &cond->suites, list) {
857 list_for_each_entry_safe(term, tmp_term, &suite->terms, list)
858 free(term);
859 free(suite);
860 }
861 return cond;
862}
863
864/* Parse an ACL condition starting at <args>[0], relying on a list of already
865 * known ACLs passed in <known_acl>. The new condition is returned (or NULL in
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200866 * case of low memory). Supports multiple conditions separated by "or". If
867 * <err> is not NULL, it will be filled with a pointer to an error message in
868 * case of error, that the caller is responsible for freeing. The initial
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200869 * location must either be freeable or NULL. The list <al> serves as a list head
870 * for unresolved dependencies.
Willy Tarreaua84d3742007-05-07 00:36:48 +0200871 */
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200872struct acl_cond *parse_acl_cond(const char **args, struct list *known_acl,
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +0100873 enum acl_cond_pol pol, char **err, struct arg_list *al,
874 const char *file, int line)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200875{
876 __label__ out_return, out_free_suite, out_free_term;
Willy Tarreau74b98a82007-06-16 19:35:18 +0200877 int arg, neg;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200878 const char *word;
879 struct acl *cur_acl;
880 struct acl_term *cur_term;
881 struct acl_term_suite *cur_suite;
882 struct acl_cond *cond;
Willy Tarreaua91d0a52013-03-25 08:12:18 +0100883 unsigned int suite_val;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200884
885 cond = (struct acl_cond *)calloc(1, sizeof(*cond));
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200886 if (cond == NULL) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200887 memprintf(err, "out of memory when parsing condition");
Willy Tarreaua84d3742007-05-07 00:36:48 +0200888 goto out_return;
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200889 }
Willy Tarreaua84d3742007-05-07 00:36:48 +0200890
891 LIST_INIT(&cond->list);
892 LIST_INIT(&cond->suites);
893 cond->pol = pol;
Willy Tarreaua91d0a52013-03-25 08:12:18 +0100894 cond->val = 0;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200895
896 cur_suite = NULL;
Willy Tarreaua91d0a52013-03-25 08:12:18 +0100897 suite_val = ~0U;
Willy Tarreau74b98a82007-06-16 19:35:18 +0200898 neg = 0;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200899 for (arg = 0; *args[arg]; arg++) {
900 word = args[arg];
901
902 /* remove as many exclamation marks as we can */
903 while (*word == '!') {
904 neg = !neg;
905 word++;
906 }
907
908 /* an empty word is allowed because we cannot force the user to
909 * always think about not leaving exclamation marks alone.
910 */
911 if (!*word)
912 continue;
913
Willy Tarreau16fbe822007-06-17 11:54:31 +0200914 if (strcasecmp(word, "or") == 0 || strcmp(word, "||") == 0) {
Willy Tarreaua84d3742007-05-07 00:36:48 +0200915 /* new term suite */
Willy Tarreaua91d0a52013-03-25 08:12:18 +0100916 cond->val |= suite_val;
917 suite_val = ~0U;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200918 cur_suite = NULL;
919 neg = 0;
920 continue;
921 }
922
Willy Tarreau95fa4692010-02-01 13:05:50 +0100923 if (strcmp(word, "{") == 0) {
924 /* we may have a complete ACL expression between two braces,
925 * find the last one.
926 */
927 int arg_end = arg + 1;
928 const char **args_new;
929
930 while (*args[arg_end] && strcmp(args[arg_end], "}") != 0)
931 arg_end++;
932
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200933 if (!*args[arg_end]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200934 memprintf(err, "missing closing '}' in condition");
Willy Tarreau95fa4692010-02-01 13:05:50 +0100935 goto out_free_suite;
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200936 }
Willy Tarreau95fa4692010-02-01 13:05:50 +0100937
938 args_new = calloc(1, (arg_end - arg + 1) * sizeof(*args_new));
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200939 if (!args_new) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200940 memprintf(err, "out of memory when parsing condition");
Willy Tarreau95fa4692010-02-01 13:05:50 +0100941 goto out_free_suite;
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200942 }
Willy Tarreau95fa4692010-02-01 13:05:50 +0100943
Willy Tarreau2a56c5e2010-03-15 16:13:29 +0100944 args_new[0] = "";
Willy Tarreau95fa4692010-02-01 13:05:50 +0100945 memcpy(args_new + 1, args + arg + 1, (arg_end - arg) * sizeof(*args_new));
946 args_new[arg_end - arg] = "";
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +0100947 cur_acl = parse_acl(args_new, known_acl, err, al, file, line);
Willy Tarreau95fa4692010-02-01 13:05:50 +0100948 free(args_new);
949
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200950 if (!cur_acl) {
951 /* note that parse_acl() must have filled <err> here */
Willy Tarreau16fbe822007-06-17 11:54:31 +0200952 goto out_free_suite;
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200953 }
Willy Tarreaua91d0a52013-03-25 08:12:18 +0100954 word = args[arg + 1];
Willy Tarreau95fa4692010-02-01 13:05:50 +0100955 arg = arg_end;
956 }
957 else {
958 /* search for <word> in the known ACL names. If we do not find
959 * it, let's look for it in the default ACLs, and if found, add
960 * it to the list of ACLs of this proxy. This makes it possible
961 * to override them.
962 */
963 cur_acl = find_acl_by_name(word, known_acl);
964 if (cur_acl == NULL) {
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +0100965 cur_acl = find_acl_default(word, known_acl, err, al, file, line);
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200966 if (cur_acl == NULL) {
967 /* note that find_acl_default() must have filled <err> here */
Willy Tarreau95fa4692010-02-01 13:05:50 +0100968 goto out_free_suite;
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200969 }
Willy Tarreau95fa4692010-02-01 13:05:50 +0100970 }
Willy Tarreau16fbe822007-06-17 11:54:31 +0200971 }
Willy Tarreaua84d3742007-05-07 00:36:48 +0200972
973 cur_term = (struct acl_term *)calloc(1, sizeof(*cur_term));
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200974 if (cur_term == NULL) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200975 memprintf(err, "out of memory when parsing condition");
Willy Tarreaua84d3742007-05-07 00:36:48 +0200976 goto out_free_suite;
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200977 }
Willy Tarreaua84d3742007-05-07 00:36:48 +0200978
979 cur_term->acl = cur_acl;
980 cur_term->neg = neg;
Willy Tarreaua91d0a52013-03-25 08:12:18 +0100981
982 /* Here it is a bit complex. The acl_term_suite is a conjunction
983 * of many terms. It may only be used if all of its terms are
984 * usable at the same time. So the suite's validity domain is an
985 * AND between all ACL keywords' ones. But, the global condition
986 * is valid if at least one term suite is OK. So it's an OR between
987 * all of their validity domains. We could emit a warning as soon
988 * as suite_val is null because it means that the last ACL is not
989 * compatible with the previous ones. Let's remain simple for now.
990 */
991 cond->use |= cur_acl->use;
992 suite_val &= cur_acl->val;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200993
994 if (!cur_suite) {
995 cur_suite = (struct acl_term_suite *)calloc(1, sizeof(*cur_suite));
Willy Tarreauf678b7f2013-01-24 00:25:39 +0100996 if (cur_suite == NULL) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200997 memprintf(err, "out of memory when parsing condition");
Willy Tarreaua84d3742007-05-07 00:36:48 +0200998 goto out_free_term;
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200999 }
Willy Tarreaua84d3742007-05-07 00:36:48 +02001000 LIST_INIT(&cur_suite->terms);
1001 LIST_ADDQ(&cond->suites, &cur_suite->list);
1002 }
1003 LIST_ADDQ(&cur_suite->terms, &cur_term->list);
Willy Tarreau74b98a82007-06-16 19:35:18 +02001004 neg = 0;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001005 }
1006
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001007 cond->val |= suite_val;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001008 return cond;
1009
1010 out_free_term:
1011 free(cur_term);
1012 out_free_suite:
1013 prune_acl_cond(cond);
1014 free(cond);
1015 out_return:
1016 return NULL;
1017}
1018
Willy Tarreau2bbba412010-01-28 16:48:33 +01001019/* Builds an ACL condition starting at the if/unless keyword. The complete
1020 * condition is returned. NULL is returned in case of error or if the first
1021 * word is neither "if" nor "unless". It automatically sets the file name and
Willy Tarreau25320b22013-03-24 07:22:08 +01001022 * the line number in the condition for better error reporting, and sets the
1023 * HTTP intiailization requirements in the proxy. If <err> is not NULL, it will
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001024 * be filled with a pointer to an error message in case of error, that the
1025 * caller is responsible for freeing. The initial location must either be
1026 * freeable or NULL.
Willy Tarreau2bbba412010-01-28 16:48:33 +01001027 */
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001028struct 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 +01001029{
Willy Tarreau0cba6072013-11-28 22:21:02 +01001030 enum acl_cond_pol pol = ACL_COND_NONE;
Willy Tarreau2bbba412010-01-28 16:48:33 +01001031 struct acl_cond *cond = NULL;
1032
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001033 if (err)
1034 *err = NULL;
1035
Willy Tarreau2bbba412010-01-28 16:48:33 +01001036 if (!strcmp(*args, "if")) {
1037 pol = ACL_COND_IF;
1038 args++;
1039 }
1040 else if (!strcmp(*args, "unless")) {
1041 pol = ACL_COND_UNLESS;
1042 args++;
1043 }
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001044 else {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02001045 memprintf(err, "conditions must start with either 'if' or 'unless'");
Willy Tarreau2bbba412010-01-28 16:48:33 +01001046 return NULL;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001047 }
Willy Tarreau2bbba412010-01-28 16:48:33 +01001048
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001049 cond = parse_acl_cond(args, &px->acl, pol, err, &px->conf.args, file, line);
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001050 if (!cond) {
1051 /* note that parse_acl_cond must have filled <err> here */
Willy Tarreau2bbba412010-01-28 16:48:33 +01001052 return NULL;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02001053 }
Willy Tarreau2bbba412010-01-28 16:48:33 +01001054
1055 cond->file = file;
1056 cond->line = line;
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001057 px->http_needed |= !!(cond->use & SMP_USE_HTTP_ANY);
Willy Tarreau2bbba412010-01-28 16:48:33 +01001058 return cond;
1059}
1060
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01001061/* Execute condition <cond> and return either ACL_TEST_FAIL, ACL_TEST_MISS or
1062 * ACL_TEST_PASS depending on the test results. ACL_TEST_MISS may only be
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001063 * returned if <opt> does not contain SMP_OPT_FINAL, indicating that incomplete
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01001064 * data is being examined. The function automatically sets SMP_OPT_ITERATE. This
1065 * function only computes the condition, it does not apply the polarity required
1066 * by IF/UNLESS, it's up to the caller to do this using something like this :
Willy Tarreau11382812008-07-09 16:18:21 +02001067 *
1068 * res = acl_pass(res);
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01001069 * if (res == ACL_TEST_MISS)
Willy Tarreaub6866442008-07-14 23:54:42 +02001070 * return 0;
Willy Tarreau11382812008-07-09 16:18:21 +02001071 * if (cond->pol == ACL_COND_UNLESS)
1072 * res = !res;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001073 */
Willy Tarreau0cba6072013-11-28 22:21:02 +01001074enum 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 +02001075{
1076 __label__ fetch_next;
1077 struct acl_term_suite *suite;
1078 struct acl_term *term;
1079 struct acl_expr *expr;
1080 struct acl *acl;
Willy Tarreau37406352012-04-23 16:16:37 +02001081 struct sample smp;
Willy Tarreau0cba6072013-11-28 22:21:02 +01001082 enum acl_test_res acl_res, suite_res, cond_res;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001083
Willy Tarreau7a777ed2012-04-26 11:44:02 +02001084 /* ACLs are iterated over all values, so let's always set the flag to
1085 * indicate this to the fetch functions.
1086 */
1087 opt |= SMP_OPT_ITERATE;
1088
Willy Tarreau11382812008-07-09 16:18:21 +02001089 /* We're doing a logical OR between conditions so we initialize to FAIL.
1090 * The MISS status is propagated down from the suites.
1091 */
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01001092 cond_res = ACL_TEST_FAIL;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001093 list_for_each_entry(suite, &cond->suites, list) {
Willy Tarreau11382812008-07-09 16:18:21 +02001094 /* Evaluate condition suite <suite>. We stop at the first term
Willy Tarreau0cba6072013-11-28 22:21:02 +01001095 * which returns ACL_TEST_FAIL. The MISS status is still propagated
Willy Tarreau11382812008-07-09 16:18:21 +02001096 * in case of uncertainty in the result.
Willy Tarreaua84d3742007-05-07 00:36:48 +02001097 */
1098
1099 /* we're doing a logical AND between terms, so we must set the
1100 * initial value to PASS.
1101 */
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01001102 suite_res = ACL_TEST_PASS;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001103 list_for_each_entry(term, &suite->terms, list) {
1104 acl = term->acl;
1105
1106 /* FIXME: use cache !
1107 * check acl->cache_idx for this.
1108 */
1109
1110 /* ACL result not cached. Let's scan all the expressions
1111 * and use the first one to match.
1112 */
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01001113 acl_res = ACL_TEST_FAIL;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001114 list_for_each_entry(expr, &acl->expr, list) {
Willy Tarreaud41f8d82007-06-10 10:06:18 +02001115 /* we need to reset context and flags */
Willy Tarreau37406352012-04-23 16:16:37 +02001116 memset(&smp, 0, sizeof(smp));
Willy Tarreaua84d3742007-05-07 00:36:48 +02001117 fetch_next:
Thierry FOURNIER348971e2013-11-21 10:50:10 +01001118 if (!sample_process(px, l4, l7, opt, expr->smp, &smp)) {
Willy Tarreaub6866442008-07-14 23:54:42 +02001119 /* maybe we could not fetch because of missing data */
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001120 if (smp.flags & SMP_F_MAY_CHANGE && !(opt & SMP_OPT_FINAL))
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01001121 acl_res |= ACL_TEST_MISS;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001122 continue;
Willy Tarreaub6866442008-07-14 23:54:42 +02001123 }
Willy Tarreauc4262962010-05-10 23:42:40 +02001124
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01001125 acl_res |= pat2acl(pattern_exec_match(&expr->pat, &smp, 0));
Willy Tarreaua84d3742007-05-07 00:36:48 +02001126 /*
Willy Tarreau11382812008-07-09 16:18:21 +02001127 * OK now acl_res holds the result of this expression
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01001128 * as one of ACL_TEST_FAIL, ACL_TEST_MISS or ACL_TEST_PASS.
Willy Tarreaua84d3742007-05-07 00:36:48 +02001129 *
Willy Tarreau11382812008-07-09 16:18:21 +02001130 * Then if (!MISS) we can cache the result, and put
Willy Tarreau37406352012-04-23 16:16:37 +02001131 * (smp.flags & SMP_F_VOLATILE) in the cache flags.
Willy Tarreaua84d3742007-05-07 00:36:48 +02001132 *
1133 * FIXME: implement cache.
1134 *
1135 */
1136
Willy Tarreau11382812008-07-09 16:18:21 +02001137 /* we're ORing these terms, so a single PASS is enough */
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01001138 if (acl_res == ACL_TEST_PASS)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001139 break;
1140
Willy Tarreau37406352012-04-23 16:16:37 +02001141 if (smp.flags & SMP_F_NOT_LAST)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001142 goto fetch_next;
Willy Tarreaub6866442008-07-14 23:54:42 +02001143
1144 /* sometimes we know the fetched data is subject to change
1145 * later and give another chance for a new match (eg: request
1146 * size, time, ...)
1147 */
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001148 if (smp.flags & SMP_F_MAY_CHANGE && !(opt & SMP_OPT_FINAL))
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01001149 acl_res |= ACL_TEST_MISS;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001150 }
1151 /*
1152 * Here we have the result of an ACL (cached or not).
1153 * ACLs are combined, negated or not, to form conditions.
1154 */
1155
Willy Tarreaua84d3742007-05-07 00:36:48 +02001156 if (term->neg)
Willy Tarreau11382812008-07-09 16:18:21 +02001157 acl_res = acl_neg(acl_res);
Willy Tarreaua84d3742007-05-07 00:36:48 +02001158
1159 suite_res &= acl_res;
Willy Tarreau11382812008-07-09 16:18:21 +02001160
Willy Tarreau79c412b2013-10-30 19:30:32 +01001161 /* we're ANDing these terms, so a single FAIL or MISS is enough */
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01001162 if (suite_res != ACL_TEST_PASS)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001163 break;
1164 }
1165 cond_res |= suite_res;
Willy Tarreau11382812008-07-09 16:18:21 +02001166
1167 /* we're ORing these terms, so a single PASS is enough */
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01001168 if (cond_res == ACL_TEST_PASS)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001169 break;
1170 }
Willy Tarreau11382812008-07-09 16:18:21 +02001171 return cond_res;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001172}
1173
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001174/* Returns a pointer to the first ACL conflicting with usage at place <where>
1175 * which is one of the SMP_VAL_* bits indicating a check place, or NULL if
1176 * no conflict is found. Only full conflicts are detected (ACL is not usable).
1177 * Use the next function to check for useless keywords.
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001178 */
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001179const struct acl *acl_cond_conflicts(const struct acl_cond *cond, unsigned int where)
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001180{
1181 struct acl_term_suite *suite;
1182 struct acl_term *term;
1183 struct acl *acl;
1184
1185 list_for_each_entry(suite, &cond->suites, list) {
1186 list_for_each_entry(term, &suite->terms, list) {
1187 acl = term->acl;
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001188 if (!(acl->val & where))
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001189 return acl;
1190 }
1191 }
1192 return NULL;
1193}
1194
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001195/* Returns a pointer to the first ACL and its first keyword to conflict with
1196 * usage at place <where> which is one of the SMP_VAL_* bits indicating a check
1197 * place. Returns true if a conflict is found, with <acl> and <kw> set (if non
1198 * null), or false if not conflict is found. The first useless keyword is
1199 * returned.
1200 */
Willy Tarreau93fddf12013-03-31 22:59:32 +02001201int 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 +01001202{
1203 struct acl_term_suite *suite;
1204 struct acl_term *term;
1205 struct acl_expr *expr;
1206
1207 list_for_each_entry(suite, &cond->suites, list) {
1208 list_for_each_entry(term, &suite->terms, list) {
1209 list_for_each_entry(expr, &term->acl->expr, list) {
Thierry FOURNIER348971e2013-11-21 10:50:10 +01001210 if (!(expr->smp->fetch->val & where)) {
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001211 if (acl)
1212 *acl = term->acl;
1213 if (kw)
1214 *kw = expr->kw;
1215 return 1;
1216 }
1217 }
1218 }
1219 }
1220 return 0;
1221}
1222
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001223/*
1224 * Find targets for userlist and groups in acl. Function returns the number
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001225 * of errors or OK if everything is fine. It must be called only once sample
1226 * fetch arguments have been resolved (after smp_resolve_args()).
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001227 */
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001228int acl_find_targets(struct proxy *p)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001229{
1230
1231 struct acl *acl;
1232 struct acl_expr *expr;
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001233 struct pattern_list *pattern;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001234 int cfgerr = 0;
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001235 struct pattern_expr_list *pexp;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001236
1237 list_for_each_entry(acl, &p->acl, list) {
1238 list_for_each_entry(expr, &acl->expr, list) {
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001239 if (!strcmp(expr->kw, "http_auth_group")) {
1240 /* Note: the ARGT_USR argument may only have been resolved earlier
1241 * by smp_resolve_args().
1242 */
Thierry FOURNIER348971e2013-11-21 10:50:10 +01001243 if (expr->smp->arg_p->unresolved) {
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001244 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 +01001245 p->id, *acl->name ? "" : "anonymous ", acl->name, expr->kw, expr->smp->arg_p->data.str.str);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001246 cfgerr++;
Willy Tarreau496aa012012-06-01 10:38:29 +02001247 continue;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001248 }
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001249
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001250 if (LIST_ISEMPTY(&expr->pat.head)) {
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001251 Alert("proxy %s: acl %s %s(): no groups specified.\n",
Willy Tarreau93fddf12013-03-31 22:59:32 +02001252 p->id, acl->name, expr->kw);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001253 cfgerr++;
1254 continue;
1255 }
1256
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01001257 /* For each pattern, check if the group exists. */
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001258 list_for_each_entry(pexp, &expr->pat.head, list) {
1259 if (LIST_ISEMPTY(&pexp->expr->patterns)) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001260 Alert("proxy %s: acl %s %s(): no groups specified.\n",
1261 p->id, acl->name, expr->kw);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001262 cfgerr++;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001263 continue;
1264 }
1265
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001266 list_for_each_entry(pattern, &pexp->expr->patterns, list) {
1267 /* this keyword only has one argument */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001268 if (!check_group(expr->smp->arg_p->data.usr, pattern->pat.ptr.str)) {
1269 Alert("proxy %s: acl %s %s(): invalid group '%s'.\n",
1270 p->id, acl->name, expr->kw, pattern->pat.ptr.str);
1271 cfgerr++;
1272 }
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001273 }
1274 }
1275 }
1276 }
1277 }
1278
1279 return cfgerr;
1280}
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001281
Willy Tarreau8ed669b2013-01-11 15:49:37 +01001282/* initializes ACLs by resolving the sample fetch names they rely upon.
1283 * Returns 0 on success, otherwise an error.
1284 */
1285int init_acl()
1286{
1287 int err = 0;
1288 int index;
1289 const char *name;
1290 struct acl_kw_list *kwl;
1291 struct sample_fetch *smp;
1292
1293 list_for_each_entry(kwl, &acl_keywords.list, list) {
1294 for (index = 0; kwl->kw[index].kw != NULL; index++) {
1295 name = kwl->kw[index].fetch_kw;
1296 if (!name)
1297 name = kwl->kw[index].kw;
1298
1299 smp = find_sample_fetch(name, strlen(name));
1300 if (!smp) {
1301 Alert("Critical internal error: ACL keyword '%s' relies on sample fetch '%s' which was not registered!\n",
1302 kwl->kw[index].kw, name);
1303 err++;
1304 continue;
1305 }
1306 kwl->kw[index].smp = smp;
1307 }
1308 }
1309 return err;
1310}
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001311
Willy Tarreaua84d3742007-05-07 00:36:48 +02001312/************************************************************************/
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001313/* All supported sample and ACL keywords must be declared here. */
1314/************************************************************************/
1315
1316/* Note: must not be declared <const> as its list will be overwritten.
Willy Tarreau61612d42012-04-19 18:42:05 +02001317 * Please take care of keeping this list alphabetically sorted.
1318 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02001319static struct acl_kw_list acl_kws = {ILH, {
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001320 { /* END */ },
Willy Tarreaua84d3742007-05-07 00:36:48 +02001321}};
1322
Willy Tarreaua84d3742007-05-07 00:36:48 +02001323__attribute__((constructor))
1324static void __acl_init(void)
1325{
Willy Tarreaua84d3742007-05-07 00:36:48 +02001326 acl_register_keywords(&acl_kws);
1327}
1328
1329
1330/*
1331 * Local variables:
1332 * c-indent-level: 8
1333 * c-basic-offset: 8
1334 * End:
1335 */