blob: 3fadaf1497c13af348e620063ffe5b34e462ad7e [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 */
42static inline enum acl_test_res pat2acl(enum pat_match_res res)
43{
44 return (enum acl_test_res)res;
45}
46
Willy Tarreaua5909832007-06-17 20:40:25 +020047/*
Willy Tarreaua84d3742007-05-07 00:36:48 +020048 * Registers the ACL keyword list <kwl> as a list of valid keywords for next
49 * parsing sessions.
50 */
51void acl_register_keywords(struct acl_kw_list *kwl)
52{
53 LIST_ADDQ(&acl_keywords.list, &kwl->list);
54}
55
56/*
57 * Unregisters the ACL keyword list <kwl> from the list of valid keywords.
58 */
59void acl_unregister_keywords(struct acl_kw_list *kwl)
60{
61 LIST_DEL(&kwl->list);
62 LIST_INIT(&kwl->list);
63}
64
65/* Return a pointer to the ACL <name> within the list starting at <head>, or
66 * NULL if not found.
67 */
68struct acl *find_acl_by_name(const char *name, struct list *head)
69{
70 struct acl *acl;
71 list_for_each_entry(acl, head, list) {
72 if (strcmp(acl->name, name) == 0)
73 return acl;
74 }
75 return NULL;
76}
77
78/* Return a pointer to the ACL keyword <kw>, or NULL if not found. Note that if
79 * <kw> contains an opening parenthesis, only the left part of it is checked.
80 */
81struct acl_keyword *find_acl_kw(const char *kw)
82{
83 int index;
84 const char *kwend;
85 struct acl_kw_list *kwl;
86
87 kwend = strchr(kw, '(');
88 if (!kwend)
89 kwend = kw + strlen(kw);
90
91 list_for_each_entry(kwl, &acl_keywords.list, list) {
92 for (index = 0; kwl->kw[index].kw != NULL; index++) {
93 if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) &&
94 kwl->kw[index].kw[kwend-kw] == 0)
95 return &kwl->kw[index];
96 }
97 }
98 return NULL;
99}
100
Willy Tarreaua84d3742007-05-07 00:36:48 +0200101static struct acl_expr *prune_acl_expr(struct acl_expr *expr)
102{
Willy Tarreau34db1082012-04-19 17:16:54 +0200103 struct arg *arg;
104
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100105 pattern_prune_expr(&expr->pat);
Willy Tarreau34db1082012-04-19 17:16:54 +0200106
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100107 for (arg = expr->smp->arg_p; arg; arg++) {
Willy Tarreau34db1082012-04-19 17:16:54 +0200108 if (arg->type == ARGT_STOP)
109 break;
Willy Tarreau496aa012012-06-01 10:38:29 +0200110 if (arg->type == ARGT_STR || arg->unresolved) {
Willy Tarreau34db1082012-04-19 17:16:54 +0200111 free(arg->data.str.str);
112 arg->data.str.str = NULL;
Willy Tarreau496aa012012-06-01 10:38:29 +0200113 arg->unresolved = 0;
Willy Tarreau34db1082012-04-19 17:16:54 +0200114 }
Willy Tarreau34db1082012-04-19 17:16:54 +0200115 }
116
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100117 if (expr->smp->arg_p != empty_arg_list)
118 free(expr->smp->arg_p);
Willy Tarreaua84d3742007-05-07 00:36:48 +0200119 return expr;
120}
121
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200122/* Parse an ACL expression starting at <args>[0], and return it. If <err> is
123 * not NULL, it will be filled with a pointer to an error message in case of
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200124 * error. This pointer must be freeable or NULL. <al> is an arg_list serving
125 * as a list head to report missing dependencies.
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200126 *
Willy Tarreaua84d3742007-05-07 00:36:48 +0200127 * Right now, the only accepted syntax is :
128 * <subject> [<value>...]
129 */
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200130struct acl_expr *parse_acl_expr(const char **args, char **err, struct arg_list *al)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200131{
132 __label__ out_return, out_free_expr, out_free_pattern;
133 struct acl_expr *expr;
134 struct acl_keyword *aclkw;
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100135 struct pattern *pattern;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100136 int patflags;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200137 const char *arg;
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100138 struct sample_expr *smp = NULL;
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100139 int idx = 0;
140 char *ckw = NULL;
141 const char *begw;
142 const char *endw;
Willy Tarreau131b4662013-12-13 01:08:36 +0100143 const char *endt;
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100144 unsigned long prev_type;
145 int cur_type;
Willy Tarreau131b4662013-12-13 01:08:36 +0100146 int nbargs;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200147
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100148 /* First, we look for an ACL keyword. And if we don't find one, then
149 * we look for a sample fetch expression starting with a sample fetch
150 * keyword.
Willy Tarreaubef91e72013-03-31 23:14:46 +0200151 */
Willy Tarreau131b4662013-12-13 01:08:36 +0100152
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100153 al->ctx = ARGC_ACL; // to report errors while resolving args late
Willy Tarreau131b4662013-12-13 01:08:36 +0100154 al->kw = *args;
155 al->conv = NULL;
156
Willy Tarreaua84d3742007-05-07 00:36:48 +0200157 aclkw = find_acl_kw(args[0]);
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100158 if (aclkw && aclkw->parse) {
159 /* OK we have a real ACL keyword */
Willy Tarreau9987ea92013-06-11 21:09:06 +0200160
Willy Tarreau131b4662013-12-13 01:08:36 +0100161 /* build new sample expression for this ACL */
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100162 smp = calloc(1, sizeof(struct sample_expr));
163 if (!smp) {
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100164 memprintf(err, "out of memory when parsing ACL expression");
165 goto out_return;
166 }
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100167 LIST_INIT(&(smp->conv_exprs));
168 smp->fetch = aclkw->smp;
169 smp->arg_p = empty_arg_list;
Willy Tarreau34db1082012-04-19 17:16:54 +0200170
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100171 /* look for the begining of the subject arguments */
Willy Tarreau131b4662013-12-13 01:08:36 +0100172 for (arg = args[0]; *arg && *arg != '(' && *arg != ','; arg++);
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100173
Willy Tarreau131b4662013-12-13 01:08:36 +0100174 endt = arg;
175 if (*endt == '(') {
176 /* look for the end of this term and skip the opening parenthesis */
177 endt = ++arg;
178 while (*endt && *endt != ')')
179 endt++;
180 if (*endt != ')') {
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100181 memprintf(err, "missing closing ')' after arguments to ACL keyword '%s'", aclkw->kw);
182 goto out_free_smp;
Willy Tarreau131b4662013-12-13 01:08:36 +0100183 }
184 }
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100185
Willy Tarreau131b4662013-12-13 01:08:36 +0100186 /* At this point, we have :
187 * - args[0] : beginning of the keyword
188 * - arg : end of the keyword, first character not part of keyword
189 * nor the opening parenthesis (so first character of args
190 * if present).
191 * - endt : end of the term (=arg or last parenthesis if args are present)
192 */
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100193 nbargs = make_arg_list(arg, endt - arg, smp->fetch->arg_mask, &smp->arg_p,
Willy Tarreau131b4662013-12-13 01:08:36 +0100194 err, NULL, NULL, al);
195 if (nbargs < 0) {
196 /* note that make_arg_list will have set <err> here */
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100197 memprintf(err, "ACL keyword '%s' : %s", aclkw->kw, *err);
198 goto out_free_smp;
Willy Tarreau131b4662013-12-13 01:08:36 +0100199 }
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100200
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100201 if (!smp->arg_p) {
202 smp->arg_p = empty_arg_list;
Willy Tarreau131b4662013-12-13 01:08:36 +0100203 }
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100204 else if (smp->fetch->val_args && !smp->fetch->val_args(smp->arg_p, err)) {
Willy Tarreau131b4662013-12-13 01:08:36 +0100205 /* invalid keyword argument, error must have been
206 * set by val_args().
207 */
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100208 memprintf(err, "in argument to '%s', %s", aclkw->kw, *err);
209 goto out_free_smp;
Willy Tarreau131b4662013-12-13 01:08:36 +0100210 }
211 arg = endt;
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100212
Willy Tarreau131b4662013-12-13 01:08:36 +0100213 /* look for the begining of the converters list. Those directly attached
214 * to the ACL keyword are found just after <arg> which points to the comma.
215 */
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100216 prev_type = smp->fetch->out_type;
Willy Tarreau131b4662013-12-13 01:08:36 +0100217 while (*arg) {
218 struct sample_conv *conv;
219 struct sample_conv_expr *conv_expr;
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100220
Willy Tarreau131b4662013-12-13 01:08:36 +0100221 if (*arg == ')') /* skip last closing parenthesis */
222 arg++;
223
224 if (*arg && *arg != ',') {
225 if (ckw)
226 memprintf(err, "ACL keyword '%s' : missing comma after conv keyword '%s'.",
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100227 aclkw->kw, ckw);
Willy Tarreau131b4662013-12-13 01:08:36 +0100228 else
229 memprintf(err, "ACL keyword '%s' : missing comma after fetch keyword.",
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100230 aclkw->kw);
231 goto out_free_smp;
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200232 }
Willy Tarreauae52f062012-04-26 12:13:35 +0200233
Willy Tarreau131b4662013-12-13 01:08:36 +0100234 while (*arg == ',') /* then trailing commas */
235 arg++;
Willy Tarreau2e845be2012-10-19 19:49:09 +0200236
Willy Tarreau131b4662013-12-13 01:08:36 +0100237 begw = arg; /* start of conv keyword */
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100238
Willy Tarreau131b4662013-12-13 01:08:36 +0100239 if (!*begw)
240 /* none ? end of converters */
241 break;
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100242
Willy Tarreau131b4662013-12-13 01:08:36 +0100243 for (endw = begw; *endw && *endw != '(' && *endw != ','; endw++);
Willy Tarreau9ca69362013-10-22 19:10:06 +0200244
Willy Tarreau131b4662013-12-13 01:08:36 +0100245 free(ckw);
246 ckw = my_strndup(begw, endw - begw);
Willy Tarreauf75d0082013-04-07 21:20:44 +0200247
Willy Tarreau131b4662013-12-13 01:08:36 +0100248 conv = find_sample_conv(begw, endw - begw);
249 if (!conv) {
250 /* Unknown converter method */
251 memprintf(err, "ACL keyword '%s' : unknown conv method '%s'.",
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100252 aclkw->kw, ckw);
253 goto out_free_smp;
Willy Tarreau131b4662013-12-13 01:08:36 +0100254 }
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100255
Willy Tarreau131b4662013-12-13 01:08:36 +0100256 arg = endw;
257 if (*arg == '(') {
258 /* look for the end of this term */
259 while (*arg && *arg != ')')
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100260 arg++;
Willy Tarreau131b4662013-12-13 01:08:36 +0100261 if (*arg != ')') {
262 memprintf(err, "ACL keyword '%s' : syntax error: missing ')' after conv keyword '%s'.",
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100263 aclkw->kw, ckw);
264 goto out_free_smp;
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100265 }
Willy Tarreau131b4662013-12-13 01:08:36 +0100266 }
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100267
Willy Tarreau131b4662013-12-13 01:08:36 +0100268 if (conv->in_type >= SMP_TYPES || conv->out_type >= SMP_TYPES) {
269 memprintf(err, "ACL keyword '%s' : returns type of conv method '%s' is unknown.",
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100270 aclkw->kw, ckw);
271 goto out_free_smp;
Willy Tarreau131b4662013-12-13 01:08:36 +0100272 }
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100273
Willy Tarreau131b4662013-12-13 01:08:36 +0100274 /* If impossible type conversion */
275 if (!sample_casts[prev_type][conv->in_type]) {
276 memprintf(err, "ACL keyword '%s' : conv method '%s' cannot be applied.",
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100277 aclkw->kw, ckw);
278 goto out_free_smp;
Willy Tarreau131b4662013-12-13 01:08:36 +0100279 }
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100280
Willy Tarreau131b4662013-12-13 01:08:36 +0100281 prev_type = conv->out_type;
282 conv_expr = calloc(1, sizeof(struct sample_conv_expr));
283 if (!conv_expr)
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100284 goto out_free_smp;
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100285
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100286 LIST_ADDQ(&(smp->conv_exprs), &(conv_expr->list));
Willy Tarreau131b4662013-12-13 01:08:36 +0100287 conv_expr->conv = conv;
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100288
Willy Tarreau131b4662013-12-13 01:08:36 +0100289 if (arg != endw) {
290 char *err_msg = NULL;
291 int err_arg;
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100292
Willy Tarreau131b4662013-12-13 01:08:36 +0100293 if (!conv->arg_mask) {
294 memprintf(err, "ACL keyword '%s' : conv method '%s' does not support any args.",
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100295 aclkw->kw, ckw);
296 goto out_free_smp;
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100297 }
298
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100299 al->kw = smp->fetch->kw;
Willy Tarreau131b4662013-12-13 01:08:36 +0100300 al->conv = conv_expr->conv->kw;
301 if (make_arg_list(endw + 1, arg - endw - 1, conv->arg_mask, &conv_expr->arg_p, &err_msg, NULL, &err_arg, al) < 0) {
302 memprintf(err, "ACL keyword '%s' : invalid arg %d in conv method '%s' : %s.",
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100303 aclkw->kw, err_arg+1, ckw, err_msg);
Willy Tarreau131b4662013-12-13 01:08:36 +0100304 free(err_msg);
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100305 goto out_free_smp;
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100306 }
307
Willy Tarreau131b4662013-12-13 01:08:36 +0100308 if (!conv_expr->arg_p)
309 conv_expr->arg_p = empty_arg_list;
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100310
Willy Tarreau131b4662013-12-13 01:08:36 +0100311 if (conv->val_args && !conv->val_args(conv_expr->arg_p, conv, &err_msg)) {
312 memprintf(err, "ACL keyword '%s' : invalid args in conv method '%s' : %s.",
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100313 aclkw->kw, ckw, err_msg);
Willy Tarreau131b4662013-12-13 01:08:36 +0100314 free(err_msg);
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100315 goto out_free_smp;
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100316 }
317 }
Willy Tarreau131b4662013-12-13 01:08:36 +0100318 else if (ARGM(conv->arg_mask)) {
319 memprintf(err, "ACL keyword '%s' : missing args for conv method '%s'.",
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100320 aclkw->kw, ckw);
321 goto out_free_smp;
Willy Tarreau131b4662013-12-13 01:08:36 +0100322 }
Willy Tarreau61612d42012-04-19 18:42:05 +0200323 }
Willy Tarreaua84d3742007-05-07 00:36:48 +0200324 }
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100325 else {
326 /* This is not an ACL keyword, so we hope this is a sample fetch
327 * keyword that we're going to transparently use as an ACL. If
328 * so, we retrieve a completely parsed expression with args and
329 * convs already done.
330 */
331 smp = sample_parse_expr((char **)args, &idx, err, al);
332 if (!smp) {
333 memprintf(err, "%s in ACL expression '%s'", *err, *args);
334 goto out_return;
335 }
336 }
337
338 expr = (struct acl_expr *)calloc(1, sizeof(*expr));
339 if (!expr) {
340 memprintf(err, "out of memory when parsing ACL expression");
341 goto out_return;
342 }
343
344 pattern_init_expr(&expr->pat);
345
346 expr->kw = aclkw ? aclkw->kw : smp->fetch->kw;
347 expr->pat.parse = aclkw ? aclkw->parse : NULL;
348 expr->pat.match = aclkw ? aclkw->match : NULL;
349 expr->smp = smp;
350 smp = NULL;
351
352 if (!expr->pat.parse) {
353 /* some types can be automatically converted */
354
355 switch (expr->smp ? expr->smp->fetch->out_type : aclkw->smp->out_type) {
356 case SMP_T_BOOL:
357 expr->pat.parse = pat_parse_fcts[PAT_MATCH_BOOL];
358 expr->pat.match = pat_match_fcts[PAT_MATCH_BOOL];
359 break;
360 case SMP_T_SINT:
361 case SMP_T_UINT:
362 expr->pat.parse = pat_parse_fcts[PAT_MATCH_INT];
363 expr->pat.match = pat_match_fcts[PAT_MATCH_INT];
364 break;
365 case SMP_T_IPV4:
366 case SMP_T_IPV6:
367 expr->pat.parse = pat_parse_fcts[PAT_MATCH_IP];
368 expr->pat.match = pat_match_fcts[PAT_MATCH_IP];
369 break;
370 }
371 }
Willy Tarreaua84d3742007-05-07 00:36:48 +0200372
Willy Tarreau3c3dfd52013-11-04 18:09:12 +0100373 /* Additional check to protect against common mistakes */
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100374 cur_type = smp_expr_output_type(expr->smp);
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +0100375 if (expr->pat.parse && cur_type != SMP_T_BOOL && !*args[1]) {
Willy Tarreau3c3dfd52013-11-04 18:09:12 +0100376 Warning("parsing acl keyword '%s' :\n"
377 " no pattern to match against were provided, so this ACL will never match.\n"
378 " If this is what you intended, please add '--' to get rid of this warning.\n"
379 " If you intended to match only for existence, please use '-m found'.\n"
380 " If you wanted to force an int to match as a bool, please use '-m bool'.\n"
381 "\n",
382 args[0]);
383 }
384
Willy Tarreaua84d3742007-05-07 00:36:48 +0200385 args++;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200386
387 /* check for options before patterns. Supported options are :
388 * -i : ignore case for all patterns by default
389 * -f : read patterns from those files
Willy Tarreau5adeda12013-03-31 22:13:34 +0200390 * -m : force matching method (must be used before -f)
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200391 * -- : everything after this is not an option
392 */
393 patflags = 0;
394 while (**args == '-') {
395 if ((*args)[1] == 'i')
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100396 patflags |= PAT_F_IGNORE_CASE;
Willy Tarreau2b5285d2010-05-09 23:45:24 +0200397 else if ((*args)[1] == 'f') {
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +0100398 if (!expr->pat.parse) {
Willy Tarreau9987ea92013-06-11 21:09:06 +0200399 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 +0200400 goto out_free_expr;
401 }
402
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100403 if (!pattern_read_from_file(&expr->pat, args[1], patflags | PAT_F_FROM_FILE, err))
Willy Tarreau2b5285d2010-05-09 23:45:24 +0200404 goto out_free_expr;
Willy Tarreau5adeda12013-03-31 22:13:34 +0200405 args++;
406 }
407 else if ((*args)[1] == 'm') {
408 int idx;
409
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +0100410 if (!LIST_ISEMPTY(&expr->pat.patterns) || !eb_is_empty(&expr->pat.pattern_tree)) {
Willy Tarreau5adeda12013-03-31 22:13:34 +0200411 memprintf(err, "'-m' must only be specified before patterns and files in parsing ACL expression");
412 goto out_free_expr;
413 }
414
Willy Tarreau6f8fe312013-11-28 22:24:25 +0100415 idx = pat_find_match_name(args[1]);
Willy Tarreau5adeda12013-03-31 22:13:34 +0200416 if (idx < 0) {
417 memprintf(err, "unknown matching method '%s' when parsing ACL expression", args[1]);
418 goto out_free_expr;
419 }
420
421 /* Note: -m found is always valid, bool/int are compatible, str/bin/reg/len are compatible */
Thierry FOURNIERe3ded592013-12-06 15:36:54 +0100422 if (!sample_casts[cur_type][pat_match_types[idx]]) {
Willy Tarreau93fddf12013-03-31 22:59:32 +0200423 memprintf(err, "matching method '%s' cannot be used with fetch keyword '%s'", args[1], expr->kw);
Willy Tarreau5adeda12013-03-31 22:13:34 +0200424 goto out_free_expr;
425 }
Thierry FOURNIERe3ded592013-12-06 15:36:54 +0100426 expr->pat.parse = pat_parse_fcts[idx];
427 expr->pat.match = pat_match_fcts[idx];
Willy Tarreau2b5285d2010-05-09 23:45:24 +0200428 args++;
429 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200430 else if ((*args)[1] == '-') {
431 args++;
432 break;
433 }
434 else
435 break;
436 args++;
437 }
438
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +0100439 if (!expr->pat.parse) {
Willy Tarreau9987ea92013-06-11 21:09:06 +0200440 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 +0200441 goto out_free_expr;
442 }
443
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200444 /* now parse all patterns */
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100445 pattern = NULL;
446 if (!pattern_register(&expr->pat, args, NULL, &pattern, patflags, err))
447 goto out_free_pattern;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200448
449 return expr;
450
451 out_free_pattern:
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100452 pattern_free(pattern);
Willy Tarreaua84d3742007-05-07 00:36:48 +0200453 out_free_expr:
454 prune_acl_expr(expr);
455 free(expr);
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100456 free(ckw);
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100457 out_free_smp:
458 free(smp);
Willy Tarreaua84d3742007-05-07 00:36:48 +0200459 out_return:
460 return NULL;
461}
462
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +0200463/* Purge everything in the acl <acl>, then return <acl>. */
464struct acl *prune_acl(struct acl *acl) {
465
466 struct acl_expr *expr, *exprb;
467
468 free(acl->name);
469
470 list_for_each_entry_safe(expr, exprb, &acl->expr, list) {
471 LIST_DEL(&expr->list);
472 prune_acl_expr(expr);
473 free(expr);
474 }
475
476 return acl;
477}
478
Willy Tarreaua84d3742007-05-07 00:36:48 +0200479/* Parse an ACL with the name starting at <args>[0], and with a list of already
480 * known ACLs in <acl>. If the ACL was not in the list, it will be added.
Willy Tarreau2a56c5e2010-03-15 16:13:29 +0100481 * A pointer to that ACL is returned. If the ACL has an empty name, then it's
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200482 * an anonymous one and it won't be merged with any other one. If <err> is not
483 * NULL, it will be filled with an appropriate error. This pointer must be
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200484 * freeable or NULL. <al> is the arg_list serving as a head for unresolved
485 * dependencies.
Willy Tarreaua84d3742007-05-07 00:36:48 +0200486 *
487 * args syntax: <aclname> <acl_expr>
488 */
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200489struct acl *parse_acl(const char **args, struct list *known_acl, char **err, struct arg_list *al)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200490{
491 __label__ out_return, out_free_acl_expr, out_free_name;
492 struct acl *cur_acl;
493 struct acl_expr *acl_expr;
494 char *name;
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200495 const char *pos;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200496
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200497 if (**args && (pos = invalid_char(*args))) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200498 memprintf(err, "invalid character in ACL name : '%c'", *pos);
Willy Tarreau2e74c3f2007-12-02 18:45:09 +0100499 goto out_return;
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200500 }
Willy Tarreau2e74c3f2007-12-02 18:45:09 +0100501
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200502 acl_expr = parse_acl_expr(args + 1, err, al);
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200503 if (!acl_expr) {
504 /* parse_acl_expr will have filled <err> here */
Willy Tarreaua84d3742007-05-07 00:36:48 +0200505 goto out_return;
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200506 }
Willy Tarreaua84d3742007-05-07 00:36:48 +0200507
Willy Tarreau404e8ab2009-07-26 19:40:40 +0200508 /* Check for args beginning with an opening parenthesis just after the
509 * subject, as this is almost certainly a typo. Right now we can only
510 * emit a warning, so let's do so.
511 */
Krzysztof Piotr Oledzki4cdd8312009-10-05 00:23:35 +0200512 if (!strchr(args[1], '(') && *args[2] == '(')
Willy Tarreau404e8ab2009-07-26 19:40:40 +0200513 Warning("parsing acl '%s' :\n"
514 " matching '%s' for pattern '%s' is likely a mistake and probably\n"
515 " not what you want. Maybe you need to remove the extraneous space before '('.\n"
516 " If you are really sure this is not an error, please insert '--' between the\n"
517 " match and the pattern to make this warning message disappear.\n",
518 args[0], args[1], args[2]);
519
Willy Tarreau2a56c5e2010-03-15 16:13:29 +0100520 if (*args[0])
521 cur_acl = find_acl_by_name(args[0], known_acl);
522 else
523 cur_acl = NULL;
524
Willy Tarreaua84d3742007-05-07 00:36:48 +0200525 if (!cur_acl) {
526 name = strdup(args[0]);
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200527 if (!name) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200528 memprintf(err, "out of memory when parsing ACL");
Willy Tarreaua84d3742007-05-07 00:36:48 +0200529 goto out_free_acl_expr;
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200530 }
Willy Tarreaua84d3742007-05-07 00:36:48 +0200531 cur_acl = (struct acl *)calloc(1, sizeof(*cur_acl));
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200532 if (cur_acl == NULL) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200533 memprintf(err, "out of memory when parsing ACL");
Willy Tarreaua84d3742007-05-07 00:36:48 +0200534 goto out_free_name;
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200535 }
Willy Tarreaua84d3742007-05-07 00:36:48 +0200536
537 LIST_INIT(&cur_acl->expr);
538 LIST_ADDQ(known_acl, &cur_acl->list);
539 cur_acl->name = name;
540 }
541
Willy Tarreaua91d0a52013-03-25 08:12:18 +0100542 /* We want to know what features the ACL needs (typically HTTP parsing),
543 * and where it may be used. If an ACL relies on multiple matches, it is
544 * OK if at least one of them may match in the context where it is used.
545 */
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100546 cur_acl->use |= acl_expr->smp->fetch->use;
547 cur_acl->val |= acl_expr->smp->fetch->val;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200548 LIST_ADDQ(&cur_acl->expr, &acl_expr->list);
549 return cur_acl;
550
551 out_free_name:
552 free(name);
553 out_free_acl_expr:
554 prune_acl_expr(acl_expr);
555 free(acl_expr);
556 out_return:
557 return NULL;
558}
559
Willy Tarreau16fbe822007-06-17 11:54:31 +0200560/* Some useful ACLs provided by default. Only those used are allocated. */
561
562const struct {
563 const char *name;
564 const char *expr[4]; /* put enough for longest expression */
565} default_acl_list[] = {
Willy Tarreau58393e12008-07-20 10:39:22 +0200566 { .name = "TRUE", .expr = {"always_true",""}},
567 { .name = "FALSE", .expr = {"always_false",""}},
Willy Tarreau16fbe822007-06-17 11:54:31 +0200568 { .name = "LOCALHOST", .expr = {"src","127.0.0.1/8",""}},
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200569 { .name = "HTTP", .expr = {"req_proto_http",""}},
Willy Tarreau16fbe822007-06-17 11:54:31 +0200570 { .name = "HTTP_1.0", .expr = {"req_ver","1.0",""}},
571 { .name = "HTTP_1.1", .expr = {"req_ver","1.1",""}},
572 { .name = "METH_CONNECT", .expr = {"method","CONNECT",""}},
573 { .name = "METH_GET", .expr = {"method","GET","HEAD",""}},
574 { .name = "METH_HEAD", .expr = {"method","HEAD",""}},
575 { .name = "METH_OPTIONS", .expr = {"method","OPTIONS",""}},
576 { .name = "METH_POST", .expr = {"method","POST",""}},
577 { .name = "METH_TRACE", .expr = {"method","TRACE",""}},
578 { .name = "HTTP_URL_ABS", .expr = {"url_reg","^[^/:]*://",""}},
579 { .name = "HTTP_URL_SLASH", .expr = {"url_beg","/",""}},
580 { .name = "HTTP_URL_STAR", .expr = {"url","*",""}},
581 { .name = "HTTP_CONTENT", .expr = {"hdr_val(content-length)","gt","0",""}},
Emeric Brunbede3d02009-06-30 17:54:00 +0200582 { .name = "RDP_COOKIE", .expr = {"req_rdp_cookie_cnt","gt","0",""}},
Willy Tarreauc6317702008-07-20 09:29:50 +0200583 { .name = "REQ_CONTENT", .expr = {"req_len","gt","0",""}},
Willy Tarreaub6fb4202008-07-20 11:18:28 +0200584 { .name = "WAIT_END", .expr = {"wait_end",""}},
Willy Tarreau16fbe822007-06-17 11:54:31 +0200585 { .name = NULL, .expr = {""}}
586};
587
588/* Find a default ACL from the default_acl list, compile it and return it.
589 * If the ACL is not found, NULL is returned. In theory, it cannot fail,
590 * except when default ACLs are broken, in which case it will return NULL.
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200591 * If <known_acl> is not NULL, the ACL will be queued at its tail. If <err> is
592 * not NULL, it will be filled with an error message if an error occurs. This
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200593 * pointer must be freeable or NULL. <al> is an arg_list serving as a list head
594 * to report missing dependencies.
Willy Tarreau16fbe822007-06-17 11:54:31 +0200595 */
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200596static struct acl *find_acl_default(const char *acl_name, struct list *known_acl,
597 char **err, struct arg_list *al)
Willy Tarreau16fbe822007-06-17 11:54:31 +0200598{
599 __label__ out_return, out_free_acl_expr, out_free_name;
600 struct acl *cur_acl;
601 struct acl_expr *acl_expr;
602 char *name;
603 int index;
604
605 for (index = 0; default_acl_list[index].name != NULL; index++) {
606 if (strcmp(acl_name, default_acl_list[index].name) == 0)
607 break;
608 }
609
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200610 if (default_acl_list[index].name == NULL) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200611 memprintf(err, "no such ACL : '%s'", acl_name);
Willy Tarreau16fbe822007-06-17 11:54:31 +0200612 return NULL;
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200613 }
Willy Tarreau16fbe822007-06-17 11:54:31 +0200614
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200615 acl_expr = parse_acl_expr((const char **)default_acl_list[index].expr, err, al);
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200616 if (!acl_expr) {
617 /* parse_acl_expr must have filled err here */
Willy Tarreau16fbe822007-06-17 11:54:31 +0200618 goto out_return;
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200619 }
Willy Tarreau16fbe822007-06-17 11:54:31 +0200620
621 name = strdup(acl_name);
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200622 if (!name) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200623 memprintf(err, "out of memory when building default ACL '%s'", acl_name);
Willy Tarreau16fbe822007-06-17 11:54:31 +0200624 goto out_free_acl_expr;
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200625 }
626
Willy Tarreau16fbe822007-06-17 11:54:31 +0200627 cur_acl = (struct acl *)calloc(1, sizeof(*cur_acl));
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200628 if (cur_acl == NULL) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200629 memprintf(err, "out of memory when building default ACL '%s'", acl_name);
Willy Tarreau16fbe822007-06-17 11:54:31 +0200630 goto out_free_name;
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200631 }
Willy Tarreau16fbe822007-06-17 11:54:31 +0200632
633 cur_acl->name = name;
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100634 cur_acl->use |= acl_expr->smp->fetch->use;
635 cur_acl->val |= acl_expr->smp->fetch->val;
Willy Tarreau16fbe822007-06-17 11:54:31 +0200636 LIST_INIT(&cur_acl->expr);
637 LIST_ADDQ(&cur_acl->expr, &acl_expr->list);
638 if (known_acl)
639 LIST_ADDQ(known_acl, &cur_acl->list);
640
641 return cur_acl;
642
643 out_free_name:
644 free(name);
645 out_free_acl_expr:
646 prune_acl_expr(acl_expr);
647 free(acl_expr);
648 out_return:
649 return NULL;
650}
Willy Tarreaua84d3742007-05-07 00:36:48 +0200651
652/* Purge everything in the acl_cond <cond>, then return <cond>. */
653struct acl_cond *prune_acl_cond(struct acl_cond *cond)
654{
655 struct acl_term_suite *suite, *tmp_suite;
656 struct acl_term *term, *tmp_term;
657
658 /* iterate through all term suites and free all terms and all suites */
659 list_for_each_entry_safe(suite, tmp_suite, &cond->suites, list) {
660 list_for_each_entry_safe(term, tmp_term, &suite->terms, list)
661 free(term);
662 free(suite);
663 }
664 return cond;
665}
666
667/* Parse an ACL condition starting at <args>[0], relying on a list of already
668 * known ACLs passed in <known_acl>. The new condition is returned (or NULL in
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200669 * case of low memory). Supports multiple conditions separated by "or". If
670 * <err> is not NULL, it will be filled with a pointer to an error message in
671 * case of error, that the caller is responsible for freeing. The initial
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200672 * location must either be freeable or NULL. The list <al> serves as a list head
673 * for unresolved dependencies.
Willy Tarreaua84d3742007-05-07 00:36:48 +0200674 */
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200675struct acl_cond *parse_acl_cond(const char **args, struct list *known_acl,
Willy Tarreau0cba6072013-11-28 22:21:02 +0100676 enum acl_cond_pol pol, char **err, struct arg_list *al)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200677{
678 __label__ out_return, out_free_suite, out_free_term;
Willy Tarreau74b98a82007-06-16 19:35:18 +0200679 int arg, neg;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200680 const char *word;
681 struct acl *cur_acl;
682 struct acl_term *cur_term;
683 struct acl_term_suite *cur_suite;
684 struct acl_cond *cond;
Willy Tarreaua91d0a52013-03-25 08:12:18 +0100685 unsigned int suite_val;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200686
687 cond = (struct acl_cond *)calloc(1, sizeof(*cond));
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200688 if (cond == NULL) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200689 memprintf(err, "out of memory when parsing condition");
Willy Tarreaua84d3742007-05-07 00:36:48 +0200690 goto out_return;
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200691 }
Willy Tarreaua84d3742007-05-07 00:36:48 +0200692
693 LIST_INIT(&cond->list);
694 LIST_INIT(&cond->suites);
695 cond->pol = pol;
Willy Tarreaua91d0a52013-03-25 08:12:18 +0100696 cond->val = 0;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200697
698 cur_suite = NULL;
Willy Tarreaua91d0a52013-03-25 08:12:18 +0100699 suite_val = ~0U;
Willy Tarreau74b98a82007-06-16 19:35:18 +0200700 neg = 0;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200701 for (arg = 0; *args[arg]; arg++) {
702 word = args[arg];
703
704 /* remove as many exclamation marks as we can */
705 while (*word == '!') {
706 neg = !neg;
707 word++;
708 }
709
710 /* an empty word is allowed because we cannot force the user to
711 * always think about not leaving exclamation marks alone.
712 */
713 if (!*word)
714 continue;
715
Willy Tarreau16fbe822007-06-17 11:54:31 +0200716 if (strcasecmp(word, "or") == 0 || strcmp(word, "||") == 0) {
Willy Tarreaua84d3742007-05-07 00:36:48 +0200717 /* new term suite */
Willy Tarreaua91d0a52013-03-25 08:12:18 +0100718 cond->val |= suite_val;
719 suite_val = ~0U;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200720 cur_suite = NULL;
721 neg = 0;
722 continue;
723 }
724
Willy Tarreau95fa4692010-02-01 13:05:50 +0100725 if (strcmp(word, "{") == 0) {
726 /* we may have a complete ACL expression between two braces,
727 * find the last one.
728 */
729 int arg_end = arg + 1;
730 const char **args_new;
731
732 while (*args[arg_end] && strcmp(args[arg_end], "}") != 0)
733 arg_end++;
734
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200735 if (!*args[arg_end]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200736 memprintf(err, "missing closing '}' in condition");
Willy Tarreau95fa4692010-02-01 13:05:50 +0100737 goto out_free_suite;
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200738 }
Willy Tarreau95fa4692010-02-01 13:05:50 +0100739
740 args_new = calloc(1, (arg_end - arg + 1) * sizeof(*args_new));
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200741 if (!args_new) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200742 memprintf(err, "out of memory when parsing condition");
Willy Tarreau95fa4692010-02-01 13:05:50 +0100743 goto out_free_suite;
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200744 }
Willy Tarreau95fa4692010-02-01 13:05:50 +0100745
Willy Tarreau2a56c5e2010-03-15 16:13:29 +0100746 args_new[0] = "";
Willy Tarreau95fa4692010-02-01 13:05:50 +0100747 memcpy(args_new + 1, args + arg + 1, (arg_end - arg) * sizeof(*args_new));
748 args_new[arg_end - arg] = "";
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200749 cur_acl = parse_acl(args_new, known_acl, err, al);
Willy Tarreau95fa4692010-02-01 13:05:50 +0100750 free(args_new);
751
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200752 if (!cur_acl) {
753 /* note that parse_acl() must have filled <err> here */
Willy Tarreau16fbe822007-06-17 11:54:31 +0200754 goto out_free_suite;
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200755 }
Willy Tarreaua91d0a52013-03-25 08:12:18 +0100756 word = args[arg + 1];
Willy Tarreau95fa4692010-02-01 13:05:50 +0100757 arg = arg_end;
758 }
759 else {
760 /* search for <word> in the known ACL names. If we do not find
761 * it, let's look for it in the default ACLs, and if found, add
762 * it to the list of ACLs of this proxy. This makes it possible
763 * to override them.
764 */
765 cur_acl = find_acl_by_name(word, known_acl);
766 if (cur_acl == NULL) {
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200767 cur_acl = find_acl_default(word, known_acl, err, al);
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200768 if (cur_acl == NULL) {
769 /* note that find_acl_default() must have filled <err> here */
Willy Tarreau95fa4692010-02-01 13:05:50 +0100770 goto out_free_suite;
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200771 }
Willy Tarreau95fa4692010-02-01 13:05:50 +0100772 }
Willy Tarreau16fbe822007-06-17 11:54:31 +0200773 }
Willy Tarreaua84d3742007-05-07 00:36:48 +0200774
775 cur_term = (struct acl_term *)calloc(1, sizeof(*cur_term));
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200776 if (cur_term == NULL) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200777 memprintf(err, "out of memory when parsing condition");
Willy Tarreaua84d3742007-05-07 00:36:48 +0200778 goto out_free_suite;
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200779 }
Willy Tarreaua84d3742007-05-07 00:36:48 +0200780
781 cur_term->acl = cur_acl;
782 cur_term->neg = neg;
Willy Tarreaua91d0a52013-03-25 08:12:18 +0100783
784 /* Here it is a bit complex. The acl_term_suite is a conjunction
785 * of many terms. It may only be used if all of its terms are
786 * usable at the same time. So the suite's validity domain is an
787 * AND between all ACL keywords' ones. But, the global condition
788 * is valid if at least one term suite is OK. So it's an OR between
789 * all of their validity domains. We could emit a warning as soon
790 * as suite_val is null because it means that the last ACL is not
791 * compatible with the previous ones. Let's remain simple for now.
792 */
793 cond->use |= cur_acl->use;
794 suite_val &= cur_acl->val;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200795
796 if (!cur_suite) {
797 cur_suite = (struct acl_term_suite *)calloc(1, sizeof(*cur_suite));
Willy Tarreauf678b7f2013-01-24 00:25:39 +0100798 if (cur_suite == NULL) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200799 memprintf(err, "out of memory when parsing condition");
Willy Tarreaua84d3742007-05-07 00:36:48 +0200800 goto out_free_term;
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200801 }
Willy Tarreaua84d3742007-05-07 00:36:48 +0200802 LIST_INIT(&cur_suite->terms);
803 LIST_ADDQ(&cond->suites, &cur_suite->list);
804 }
805 LIST_ADDQ(&cur_suite->terms, &cur_term->list);
Willy Tarreau74b98a82007-06-16 19:35:18 +0200806 neg = 0;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200807 }
808
Willy Tarreaua91d0a52013-03-25 08:12:18 +0100809 cond->val |= suite_val;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200810 return cond;
811
812 out_free_term:
813 free(cur_term);
814 out_free_suite:
815 prune_acl_cond(cond);
816 free(cond);
817 out_return:
818 return NULL;
819}
820
Willy Tarreau2bbba412010-01-28 16:48:33 +0100821/* Builds an ACL condition starting at the if/unless keyword. The complete
822 * condition is returned. NULL is returned in case of error or if the first
823 * word is neither "if" nor "unless". It automatically sets the file name and
Willy Tarreau25320b22013-03-24 07:22:08 +0100824 * the line number in the condition for better error reporting, and sets the
825 * HTTP intiailization requirements in the proxy. If <err> is not NULL, it will
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200826 * be filled with a pointer to an error message in case of error, that the
827 * caller is responsible for freeing. The initial location must either be
828 * freeable or NULL.
Willy Tarreau2bbba412010-01-28 16:48:33 +0100829 */
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200830struct 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 +0100831{
Willy Tarreau0cba6072013-11-28 22:21:02 +0100832 enum acl_cond_pol pol = ACL_COND_NONE;
Willy Tarreau2bbba412010-01-28 16:48:33 +0100833 struct acl_cond *cond = NULL;
834
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200835 if (err)
836 *err = NULL;
837
Willy Tarreau2bbba412010-01-28 16:48:33 +0100838 if (!strcmp(*args, "if")) {
839 pol = ACL_COND_IF;
840 args++;
841 }
842 else if (!strcmp(*args, "unless")) {
843 pol = ACL_COND_UNLESS;
844 args++;
845 }
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200846 else {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200847 memprintf(err, "conditions must start with either 'if' or 'unless'");
Willy Tarreau2bbba412010-01-28 16:48:33 +0100848 return NULL;
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200849 }
Willy Tarreau2bbba412010-01-28 16:48:33 +0100850
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200851 cond = parse_acl_cond(args, &px->acl, pol, err, &px->conf.args);
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200852 if (!cond) {
853 /* note that parse_acl_cond must have filled <err> here */
Willy Tarreau2bbba412010-01-28 16:48:33 +0100854 return NULL;
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200855 }
Willy Tarreau2bbba412010-01-28 16:48:33 +0100856
857 cond->file = file;
858 cond->line = line;
Willy Tarreaua91d0a52013-03-25 08:12:18 +0100859 px->http_needed |= !!(cond->use & SMP_USE_HTTP_ANY);
Willy Tarreau2bbba412010-01-28 16:48:33 +0100860 return cond;
861}
862
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100863/* Execute condition <cond> and return either ACL_TEST_FAIL, ACL_TEST_MISS or
864 * ACL_TEST_PASS depending on the test results. ACL_TEST_MISS may only be
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200865 * returned if <opt> does not contain SMP_OPT_FINAL, indicating that incomplete
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100866 * data is being examined. The function automatically sets SMP_OPT_ITERATE. This
867 * function only computes the condition, it does not apply the polarity required
868 * by IF/UNLESS, it's up to the caller to do this using something like this :
Willy Tarreau11382812008-07-09 16:18:21 +0200869 *
870 * res = acl_pass(res);
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100871 * if (res == ACL_TEST_MISS)
Willy Tarreaub6866442008-07-14 23:54:42 +0200872 * return 0;
Willy Tarreau11382812008-07-09 16:18:21 +0200873 * if (cond->pol == ACL_COND_UNLESS)
874 * res = !res;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200875 */
Willy Tarreau0cba6072013-11-28 22:21:02 +0100876enum 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 +0200877{
878 __label__ fetch_next;
879 struct acl_term_suite *suite;
880 struct acl_term *term;
881 struct acl_expr *expr;
882 struct acl *acl;
Willy Tarreau37406352012-04-23 16:16:37 +0200883 struct sample smp;
Willy Tarreau0cba6072013-11-28 22:21:02 +0100884 enum acl_test_res acl_res, suite_res, cond_res;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200885
Willy Tarreau7a777ed2012-04-26 11:44:02 +0200886 /* ACLs are iterated over all values, so let's always set the flag to
887 * indicate this to the fetch functions.
888 */
889 opt |= SMP_OPT_ITERATE;
890
Willy Tarreau11382812008-07-09 16:18:21 +0200891 /* We're doing a logical OR between conditions so we initialize to FAIL.
892 * The MISS status is propagated down from the suites.
893 */
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100894 cond_res = ACL_TEST_FAIL;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200895 list_for_each_entry(suite, &cond->suites, list) {
Willy Tarreau11382812008-07-09 16:18:21 +0200896 /* Evaluate condition suite <suite>. We stop at the first term
Willy Tarreau0cba6072013-11-28 22:21:02 +0100897 * which returns ACL_TEST_FAIL. The MISS status is still propagated
Willy Tarreau11382812008-07-09 16:18:21 +0200898 * in case of uncertainty in the result.
Willy Tarreaua84d3742007-05-07 00:36:48 +0200899 */
900
901 /* we're doing a logical AND between terms, so we must set the
902 * initial value to PASS.
903 */
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100904 suite_res = ACL_TEST_PASS;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200905 list_for_each_entry(term, &suite->terms, list) {
906 acl = term->acl;
907
908 /* FIXME: use cache !
909 * check acl->cache_idx for this.
910 */
911
912 /* ACL result not cached. Let's scan all the expressions
913 * and use the first one to match.
914 */
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100915 acl_res = ACL_TEST_FAIL;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200916 list_for_each_entry(expr, &acl->expr, list) {
Willy Tarreaud41f8d82007-06-10 10:06:18 +0200917 /* we need to reset context and flags */
Willy Tarreau37406352012-04-23 16:16:37 +0200918 memset(&smp, 0, sizeof(smp));
Willy Tarreaua84d3742007-05-07 00:36:48 +0200919 fetch_next:
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100920 if (!sample_process(px, l4, l7, opt, expr->smp, &smp)) {
Willy Tarreaub6866442008-07-14 23:54:42 +0200921 /* maybe we could not fetch because of missing data */
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200922 if (smp.flags & SMP_F_MAY_CHANGE && !(opt & SMP_OPT_FINAL))
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100923 acl_res |= ACL_TEST_MISS;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200924 continue;
Willy Tarreaub6866442008-07-14 23:54:42 +0200925 }
Willy Tarreauc4262962010-05-10 23:42:40 +0200926
Thierry FOURNIER76090642013-12-10 15:03:38 +0100927 acl_res |= pat2acl(pattern_exec_match(&expr->pat, &smp, NULL, NULL, NULL));
Willy Tarreaua84d3742007-05-07 00:36:48 +0200928 /*
Willy Tarreau11382812008-07-09 16:18:21 +0200929 * OK now acl_res holds the result of this expression
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100930 * as one of ACL_TEST_FAIL, ACL_TEST_MISS or ACL_TEST_PASS.
Willy Tarreaua84d3742007-05-07 00:36:48 +0200931 *
Willy Tarreau11382812008-07-09 16:18:21 +0200932 * Then if (!MISS) we can cache the result, and put
Willy Tarreau37406352012-04-23 16:16:37 +0200933 * (smp.flags & SMP_F_VOLATILE) in the cache flags.
Willy Tarreaua84d3742007-05-07 00:36:48 +0200934 *
935 * FIXME: implement cache.
936 *
937 */
938
Willy Tarreau11382812008-07-09 16:18:21 +0200939 /* we're ORing these terms, so a single PASS is enough */
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100940 if (acl_res == ACL_TEST_PASS)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200941 break;
942
Willy Tarreau37406352012-04-23 16:16:37 +0200943 if (smp.flags & SMP_F_NOT_LAST)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200944 goto fetch_next;
Willy Tarreaub6866442008-07-14 23:54:42 +0200945
946 /* sometimes we know the fetched data is subject to change
947 * later and give another chance for a new match (eg: request
948 * size, time, ...)
949 */
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200950 if (smp.flags & SMP_F_MAY_CHANGE && !(opt & SMP_OPT_FINAL))
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100951 acl_res |= ACL_TEST_MISS;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200952 }
953 /*
954 * Here we have the result of an ACL (cached or not).
955 * ACLs are combined, negated or not, to form conditions.
956 */
957
Willy Tarreaua84d3742007-05-07 00:36:48 +0200958 if (term->neg)
Willy Tarreau11382812008-07-09 16:18:21 +0200959 acl_res = acl_neg(acl_res);
Willy Tarreaua84d3742007-05-07 00:36:48 +0200960
961 suite_res &= acl_res;
Willy Tarreau11382812008-07-09 16:18:21 +0200962
Willy Tarreau79c412b2013-10-30 19:30:32 +0100963 /* we're ANDing these terms, so a single FAIL or MISS is enough */
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100964 if (suite_res != ACL_TEST_PASS)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200965 break;
966 }
967 cond_res |= suite_res;
Willy Tarreau11382812008-07-09 16:18:21 +0200968
969 /* we're ORing these terms, so a single PASS is enough */
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100970 if (cond_res == ACL_TEST_PASS)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200971 break;
972 }
Willy Tarreau11382812008-07-09 16:18:21 +0200973 return cond_res;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200974}
975
Willy Tarreaua91d0a52013-03-25 08:12:18 +0100976/* Returns a pointer to the first ACL conflicting with usage at place <where>
977 * which is one of the SMP_VAL_* bits indicating a check place, or NULL if
978 * no conflict is found. Only full conflicts are detected (ACL is not usable).
979 * Use the next function to check for useless keywords.
Willy Tarreaudd64f8d2008-07-27 22:02:32 +0200980 */
Willy Tarreaua91d0a52013-03-25 08:12:18 +0100981const struct acl *acl_cond_conflicts(const struct acl_cond *cond, unsigned int where)
Willy Tarreaudd64f8d2008-07-27 22:02:32 +0200982{
983 struct acl_term_suite *suite;
984 struct acl_term *term;
985 struct acl *acl;
986
987 list_for_each_entry(suite, &cond->suites, list) {
988 list_for_each_entry(term, &suite->terms, list) {
989 acl = term->acl;
Willy Tarreaua91d0a52013-03-25 08:12:18 +0100990 if (!(acl->val & where))
Willy Tarreaudd64f8d2008-07-27 22:02:32 +0200991 return acl;
992 }
993 }
994 return NULL;
995}
996
Willy Tarreaua91d0a52013-03-25 08:12:18 +0100997/* Returns a pointer to the first ACL and its first keyword to conflict with
998 * usage at place <where> which is one of the SMP_VAL_* bits indicating a check
999 * place. Returns true if a conflict is found, with <acl> and <kw> set (if non
1000 * null), or false if not conflict is found. The first useless keyword is
1001 * returned.
1002 */
Willy Tarreau93fddf12013-03-31 22:59:32 +02001003int 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 +01001004{
1005 struct acl_term_suite *suite;
1006 struct acl_term *term;
1007 struct acl_expr *expr;
1008
1009 list_for_each_entry(suite, &cond->suites, list) {
1010 list_for_each_entry(term, &suite->terms, list) {
1011 list_for_each_entry(expr, &term->acl->expr, list) {
Thierry FOURNIER348971e2013-11-21 10:50:10 +01001012 if (!(expr->smp->fetch->val & where)) {
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001013 if (acl)
1014 *acl = term->acl;
1015 if (kw)
1016 *kw = expr->kw;
1017 return 1;
1018 }
1019 }
1020 }
1021 }
1022 return 0;
1023}
1024
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001025/*
1026 * Find targets for userlist and groups in acl. Function returns the number
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001027 * of errors or OK if everything is fine. It must be called only once sample
1028 * fetch arguments have been resolved (after smp_resolve_args()).
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001029 */
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001030int acl_find_targets(struct proxy *p)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001031{
1032
1033 struct acl *acl;
1034 struct acl_expr *expr;
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01001035 struct pattern *pattern;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001036 int cfgerr = 0;
1037
1038 list_for_each_entry(acl, &p->acl, list) {
1039 list_for_each_entry(expr, &acl->expr, list) {
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001040 if (!strcmp(expr->kw, "http_auth_group")) {
1041 /* Note: the ARGT_USR argument may only have been resolved earlier
1042 * by smp_resolve_args().
1043 */
Thierry FOURNIER348971e2013-11-21 10:50:10 +01001044 if (expr->smp->arg_p->unresolved) {
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001045 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 +01001046 p->id, *acl->name ? "" : "anonymous ", acl->name, expr->kw, expr->smp->arg_p->data.str.str);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001047 cfgerr++;
Willy Tarreau496aa012012-06-01 10:38:29 +02001048 continue;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001049 }
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001050
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +01001051 if (LIST_ISEMPTY(&expr->pat.patterns)) {
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001052 Alert("proxy %s: acl %s %s(): no groups specified.\n",
Willy Tarreau93fddf12013-03-31 22:59:32 +02001053 p->id, acl->name, expr->kw);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001054 cfgerr++;
1055 continue;
1056 }
1057
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +01001058 list_for_each_entry(pattern, &expr->pat.patterns, list) {
Willy Tarreau7d1df412012-11-23 23:47:36 +01001059 /* this keyword only has one argument */
Thierry FOURNIER348971e2013-11-21 10:50:10 +01001060 pattern->val.group_mask = auth_resolve_groups(expr->smp->arg_p->data.usr, pattern->ptr.str);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001061
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001062 if (!pattern->val.group_mask) {
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001063 Alert("proxy %s: acl %s %s(): invalid group '%s'.\n",
1064 p->id, acl->name, expr->kw, pattern->ptr.str);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001065 cfgerr++;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001066 }
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001067 free(pattern->ptr.str);
1068 pattern->ptr.str = NULL;
1069 pattern->len = 0;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001070 }
1071 }
1072 }
1073 }
1074
1075 return cfgerr;
1076}
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001077
Willy Tarreau8ed669b2013-01-11 15:49:37 +01001078/* initializes ACLs by resolving the sample fetch names they rely upon.
1079 * Returns 0 on success, otherwise an error.
1080 */
1081int init_acl()
1082{
1083 int err = 0;
1084 int index;
1085 const char *name;
1086 struct acl_kw_list *kwl;
1087 struct sample_fetch *smp;
1088
1089 list_for_each_entry(kwl, &acl_keywords.list, list) {
1090 for (index = 0; kwl->kw[index].kw != NULL; index++) {
1091 name = kwl->kw[index].fetch_kw;
1092 if (!name)
1093 name = kwl->kw[index].kw;
1094
1095 smp = find_sample_fetch(name, strlen(name));
1096 if (!smp) {
1097 Alert("Critical internal error: ACL keyword '%s' relies on sample fetch '%s' which was not registered!\n",
1098 kwl->kw[index].kw, name);
1099 err++;
1100 continue;
1101 }
1102 kwl->kw[index].smp = smp;
1103 }
1104 }
1105 return err;
1106}
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001107
Willy Tarreaua84d3742007-05-07 00:36:48 +02001108/************************************************************************/
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001109/* All supported sample and ACL keywords must be declared here. */
1110/************************************************************************/
1111
1112/* Note: must not be declared <const> as its list will be overwritten.
Willy Tarreau61612d42012-04-19 18:42:05 +02001113 * Please take care of keeping this list alphabetically sorted.
1114 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02001115static struct acl_kw_list acl_kws = {ILH, {
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001116 { /* END */ },
Willy Tarreaua84d3742007-05-07 00:36:48 +02001117}};
1118
Willy Tarreaua84d3742007-05-07 00:36:48 +02001119__attribute__((constructor))
1120static void __acl_init(void)
1121{
Willy Tarreaua84d3742007-05-07 00:36:48 +02001122 acl_register_keywords(&acl_kws);
1123}
1124
1125
1126/*
1127 * Local variables:
1128 * c-indent-level: 8
1129 * c-basic-offset: 8
1130 * End:
1131 */