blob: 78c3f307d5e0a893455e69585381ebbea7695c56 [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
Willy Tarreau4bfa4222013-12-16 22:01:06 +010079 * <kw> contains an opening parenthesis or a comma, only the left part of it is
80 * checked.
Willy Tarreaua84d3742007-05-07 00:36:48 +020081 */
82struct acl_keyword *find_acl_kw(const char *kw)
83{
84 int index;
85 const char *kwend;
86 struct acl_kw_list *kwl;
87
Willy Tarreau4bfa4222013-12-16 22:01:06 +010088 kwend = kw;
89 while (*kwend && *kwend != '(' && *kwend != ',')
90 kwend++;
Willy Tarreaua84d3742007-05-07 00:36:48 +020091
92 list_for_each_entry(kwl, &acl_keywords.list, list) {
93 for (index = 0; kwl->kw[index].kw != NULL; index++) {
94 if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) &&
95 kwl->kw[index].kw[kwend-kw] == 0)
96 return &kwl->kw[index];
97 }
98 }
99 return NULL;
100}
101
Willy Tarreaua84d3742007-05-07 00:36:48 +0200102static struct acl_expr *prune_acl_expr(struct acl_expr *expr)
103{
Willy Tarreau34db1082012-04-19 17:16:54 +0200104 struct arg *arg;
105
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100106 pattern_prune_expr(&expr->pat);
Willy Tarreau34db1082012-04-19 17:16:54 +0200107
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100108 for (arg = expr->smp->arg_p; arg; arg++) {
Willy Tarreau34db1082012-04-19 17:16:54 +0200109 if (arg->type == ARGT_STOP)
110 break;
Willy Tarreau496aa012012-06-01 10:38:29 +0200111 if (arg->type == ARGT_STR || arg->unresolved) {
Willy Tarreau34db1082012-04-19 17:16:54 +0200112 free(arg->data.str.str);
113 arg->data.str.str = NULL;
Willy Tarreau496aa012012-06-01 10:38:29 +0200114 arg->unresolved = 0;
Willy Tarreau34db1082012-04-19 17:16:54 +0200115 }
Willy Tarreau34db1082012-04-19 17:16:54 +0200116 }
117
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100118 if (expr->smp->arg_p != empty_arg_list)
119 free(expr->smp->arg_p);
Willy Tarreaua84d3742007-05-07 00:36:48 +0200120 return expr;
121}
122
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200123/* Parse an ACL expression starting at <args>[0], and return it. If <err> is
124 * not NULL, it will be filled with a pointer to an error message in case of
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200125 * error. This pointer must be freeable or NULL. <al> is an arg_list serving
126 * as a list head to report missing dependencies.
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200127 *
Willy Tarreaua84d3742007-05-07 00:36:48 +0200128 * Right now, the only accepted syntax is :
129 * <subject> [<value>...]
130 */
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200131struct acl_expr *parse_acl_expr(const char **args, char **err, struct arg_list *al)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200132{
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100133 __label__ out_return, out_free_expr;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200134 struct acl_expr *expr;
135 struct acl_keyword *aclkw;
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;
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100147 int operator = STD_OP_EQ;
148 int op;
149 int contain_colon, have_dot;
150 const char *dot;
151 signed long long value, minor;
152 /* The following buffer contain two numbers, a ':' separator and the final \0. */
153 char buffer[NB_LLMAX_STR + 1 + NB_LLMAX_STR + 1];
Willy Tarreaua84d3742007-05-07 00:36:48 +0200154
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100155 /* First, we look for an ACL keyword. And if we don't find one, then
156 * we look for a sample fetch expression starting with a sample fetch
157 * keyword.
Willy Tarreaubef91e72013-03-31 23:14:46 +0200158 */
Willy Tarreau131b4662013-12-13 01:08:36 +0100159
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100160 al->ctx = ARGC_ACL; // to report errors while resolving args late
Willy Tarreau131b4662013-12-13 01:08:36 +0100161 al->kw = *args;
162 al->conv = NULL;
163
Willy Tarreaua84d3742007-05-07 00:36:48 +0200164 aclkw = find_acl_kw(args[0]);
Willy Tarreau20490922014-03-17 18:04:27 +0100165 if (aclkw) {
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100166 /* OK we have a real ACL keyword */
Willy Tarreau9987ea92013-06-11 21:09:06 +0200167
Willy Tarreau131b4662013-12-13 01:08:36 +0100168 /* build new sample expression for this ACL */
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100169 smp = calloc(1, sizeof(struct sample_expr));
170 if (!smp) {
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100171 memprintf(err, "out of memory when parsing ACL expression");
172 goto out_return;
173 }
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100174 LIST_INIT(&(smp->conv_exprs));
175 smp->fetch = aclkw->smp;
176 smp->arg_p = empty_arg_list;
Willy Tarreau34db1082012-04-19 17:16:54 +0200177
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100178 /* look for the begining of the subject arguments */
Willy Tarreau131b4662013-12-13 01:08:36 +0100179 for (arg = args[0]; *arg && *arg != '(' && *arg != ','; arg++);
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100180
Willy Tarreau131b4662013-12-13 01:08:36 +0100181 endt = arg;
182 if (*endt == '(') {
183 /* look for the end of this term and skip the opening parenthesis */
184 endt = ++arg;
185 while (*endt && *endt != ')')
186 endt++;
187 if (*endt != ')') {
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100188 memprintf(err, "missing closing ')' after arguments to ACL keyword '%s'", aclkw->kw);
189 goto out_free_smp;
Willy Tarreau131b4662013-12-13 01:08:36 +0100190 }
191 }
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100192
Willy Tarreau131b4662013-12-13 01:08:36 +0100193 /* At this point, we have :
194 * - args[0] : beginning of the keyword
195 * - arg : end of the keyword, first character not part of keyword
196 * nor the opening parenthesis (so first character of args
197 * if present).
198 * - endt : end of the term (=arg or last parenthesis if args are present)
199 */
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100200 nbargs = make_arg_list(arg, endt - arg, smp->fetch->arg_mask, &smp->arg_p,
Willy Tarreau131b4662013-12-13 01:08:36 +0100201 err, NULL, NULL, al);
202 if (nbargs < 0) {
203 /* note that make_arg_list will have set <err> here */
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100204 memprintf(err, "ACL keyword '%s' : %s", aclkw->kw, *err);
205 goto out_free_smp;
Willy Tarreau131b4662013-12-13 01:08:36 +0100206 }
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100207
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100208 if (!smp->arg_p) {
209 smp->arg_p = empty_arg_list;
Willy Tarreau131b4662013-12-13 01:08:36 +0100210 }
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100211 else if (smp->fetch->val_args && !smp->fetch->val_args(smp->arg_p, err)) {
Willy Tarreau131b4662013-12-13 01:08:36 +0100212 /* invalid keyword argument, error must have been
213 * set by val_args().
214 */
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100215 memprintf(err, "in argument to '%s', %s", aclkw->kw, *err);
216 goto out_free_smp;
Willy Tarreau131b4662013-12-13 01:08:36 +0100217 }
218 arg = endt;
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100219
Willy Tarreau131b4662013-12-13 01:08:36 +0100220 /* look for the begining of the converters list. Those directly attached
221 * to the ACL keyword are found just after <arg> which points to the comma.
222 */
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100223 prev_type = smp->fetch->out_type;
Willy Tarreau131b4662013-12-13 01:08:36 +0100224 while (*arg) {
225 struct sample_conv *conv;
226 struct sample_conv_expr *conv_expr;
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100227
Willy Tarreau131b4662013-12-13 01:08:36 +0100228 if (*arg == ')') /* skip last closing parenthesis */
229 arg++;
230
231 if (*arg && *arg != ',') {
232 if (ckw)
233 memprintf(err, "ACL keyword '%s' : missing comma after conv keyword '%s'.",
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100234 aclkw->kw, ckw);
Willy Tarreau131b4662013-12-13 01:08:36 +0100235 else
236 memprintf(err, "ACL keyword '%s' : missing comma after fetch keyword.",
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100237 aclkw->kw);
238 goto out_free_smp;
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200239 }
Willy Tarreauae52f062012-04-26 12:13:35 +0200240
Willy Tarreau131b4662013-12-13 01:08:36 +0100241 while (*arg == ',') /* then trailing commas */
242 arg++;
Willy Tarreau2e845be2012-10-19 19:49:09 +0200243
Willy Tarreau131b4662013-12-13 01:08:36 +0100244 begw = arg; /* start of conv keyword */
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100245
Willy Tarreau131b4662013-12-13 01:08:36 +0100246 if (!*begw)
247 /* none ? end of converters */
248 break;
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100249
Willy Tarreau131b4662013-12-13 01:08:36 +0100250 for (endw = begw; *endw && *endw != '(' && *endw != ','; endw++);
Willy Tarreau9ca69362013-10-22 19:10:06 +0200251
Willy Tarreau131b4662013-12-13 01:08:36 +0100252 free(ckw);
253 ckw = my_strndup(begw, endw - begw);
Willy Tarreauf75d0082013-04-07 21:20:44 +0200254
Willy Tarreau131b4662013-12-13 01:08:36 +0100255 conv = find_sample_conv(begw, endw - begw);
256 if (!conv) {
257 /* Unknown converter method */
258 memprintf(err, "ACL keyword '%s' : unknown conv method '%s'.",
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100259 aclkw->kw, ckw);
260 goto out_free_smp;
Willy Tarreau131b4662013-12-13 01:08:36 +0100261 }
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100262
Willy Tarreau131b4662013-12-13 01:08:36 +0100263 arg = endw;
264 if (*arg == '(') {
265 /* look for the end of this term */
266 while (*arg && *arg != ')')
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100267 arg++;
Willy Tarreau131b4662013-12-13 01:08:36 +0100268 if (*arg != ')') {
269 memprintf(err, "ACL keyword '%s' : syntax error: missing ')' after conv keyword '%s'.",
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100270 aclkw->kw, ckw);
271 goto out_free_smp;
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100272 }
Willy Tarreau131b4662013-12-13 01:08:36 +0100273 }
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100274
Willy Tarreau131b4662013-12-13 01:08:36 +0100275 if (conv->in_type >= SMP_TYPES || conv->out_type >= SMP_TYPES) {
276 memprintf(err, "ACL keyword '%s' : returns type of conv method '%s' is unknown.",
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 /* If impossible type conversion */
282 if (!sample_casts[prev_type][conv->in_type]) {
283 memprintf(err, "ACL keyword '%s' : conv method '%s' cannot be applied.",
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100284 aclkw->kw, ckw);
285 goto out_free_smp;
Willy Tarreau131b4662013-12-13 01:08:36 +0100286 }
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100287
Willy Tarreau131b4662013-12-13 01:08:36 +0100288 prev_type = conv->out_type;
289 conv_expr = calloc(1, sizeof(struct sample_conv_expr));
290 if (!conv_expr)
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100291 goto out_free_smp;
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100292
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100293 LIST_ADDQ(&(smp->conv_exprs), &(conv_expr->list));
Willy Tarreau131b4662013-12-13 01:08:36 +0100294 conv_expr->conv = conv;
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100295
Willy Tarreau131b4662013-12-13 01:08:36 +0100296 if (arg != endw) {
Willy Tarreau131b4662013-12-13 01:08:36 +0100297 int err_arg;
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100298
Willy Tarreau131b4662013-12-13 01:08:36 +0100299 if (!conv->arg_mask) {
300 memprintf(err, "ACL keyword '%s' : conv method '%s' does not support any args.",
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100301 aclkw->kw, ckw);
302 goto out_free_smp;
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100303 }
304
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100305 al->kw = smp->fetch->kw;
Willy Tarreau131b4662013-12-13 01:08:36 +0100306 al->conv = conv_expr->conv->kw;
Willy Tarreauadaddc22013-12-13 01:30:22 +0100307 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 +0100308 memprintf(err, "ACL keyword '%s' : invalid arg %d in conv method '%s' : %s.",
Willy Tarreauadaddc22013-12-13 01:30:22 +0100309 aclkw->kw, err_arg+1, ckw, *err);
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100310 goto out_free_smp;
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100311 }
312
Willy Tarreau131b4662013-12-13 01:08:36 +0100313 if (!conv_expr->arg_p)
314 conv_expr->arg_p = empty_arg_list;
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100315
Willy Tarreauadaddc22013-12-13 01:30:22 +0100316 if (conv->val_args && !conv->val_args(conv_expr->arg_p, conv, err)) {
Willy Tarreau131b4662013-12-13 01:08:36 +0100317 memprintf(err, "ACL keyword '%s' : invalid args in conv method '%s' : %s.",
Willy Tarreauadaddc22013-12-13 01:30:22 +0100318 aclkw->kw, 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 else if (ARGM(conv->arg_mask)) {
323 memprintf(err, "ACL keyword '%s' : missing args for conv method '%s'.",
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100324 aclkw->kw, ckw);
325 goto out_free_smp;
Willy Tarreau131b4662013-12-13 01:08:36 +0100326 }
Willy Tarreau61612d42012-04-19 18:42:05 +0200327 }
Willy Tarreaua84d3742007-05-07 00:36:48 +0200328 }
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100329 else {
330 /* This is not an ACL keyword, so we hope this is a sample fetch
331 * keyword that we're going to transparently use as an ACL. If
332 * so, we retrieve a completely parsed expression with args and
333 * convs already done.
334 */
335 smp = sample_parse_expr((char **)args, &idx, err, al);
336 if (!smp) {
337 memprintf(err, "%s in ACL expression '%s'", *err, *args);
338 goto out_return;
339 }
340 }
341
342 expr = (struct acl_expr *)calloc(1, sizeof(*expr));
343 if (!expr) {
344 memprintf(err, "out of memory when parsing ACL expression");
345 goto out_return;
346 }
347
348 pattern_init_expr(&expr->pat);
349
350 expr->kw = aclkw ? aclkw->kw : smp->fetch->kw;
351 expr->pat.parse = aclkw ? aclkw->parse : NULL;
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100352 expr->pat.index = aclkw ? aclkw->index : NULL;
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100353 expr->pat.match = aclkw ? aclkw->match : NULL;
354 expr->smp = smp;
355 smp = NULL;
356
357 if (!expr->pat.parse) {
358 /* some types can be automatically converted */
359
360 switch (expr->smp ? expr->smp->fetch->out_type : aclkw->smp->out_type) {
361 case SMP_T_BOOL:
362 expr->pat.parse = pat_parse_fcts[PAT_MATCH_BOOL];
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100363 expr->pat.index = pat_index_fcts[PAT_MATCH_BOOL];
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100364 expr->pat.match = pat_match_fcts[PAT_MATCH_BOOL];
365 break;
366 case SMP_T_SINT:
367 case SMP_T_UINT:
368 expr->pat.parse = pat_parse_fcts[PAT_MATCH_INT];
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100369 expr->pat.index = pat_index_fcts[PAT_MATCH_INT];
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100370 expr->pat.match = pat_match_fcts[PAT_MATCH_INT];
371 break;
372 case SMP_T_IPV4:
373 case SMP_T_IPV6:
374 expr->pat.parse = pat_parse_fcts[PAT_MATCH_IP];
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100375 expr->pat.index = pat_index_fcts[PAT_MATCH_IP];
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100376 expr->pat.match = pat_match_fcts[PAT_MATCH_IP];
377 break;
378 }
379 }
Willy Tarreaua84d3742007-05-07 00:36:48 +0200380
Willy Tarreau3c3dfd52013-11-04 18:09:12 +0100381 /* Additional check to protect against common mistakes */
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100382 cur_type = smp_expr_output_type(expr->smp);
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +0100383 if (expr->pat.parse && cur_type != SMP_T_BOOL && !*args[1]) {
Willy Tarreau3c3dfd52013-11-04 18:09:12 +0100384 Warning("parsing acl keyword '%s' :\n"
385 " no pattern to match against were provided, so this ACL will never match.\n"
386 " If this is what you intended, please add '--' to get rid of this warning.\n"
387 " If you intended to match only for existence, please use '-m found'.\n"
388 " If you wanted to force an int to match as a bool, please use '-m bool'.\n"
389 "\n",
390 args[0]);
391 }
392
Willy Tarreaua84d3742007-05-07 00:36:48 +0200393 args++;
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200394
395 /* check for options before patterns. Supported options are :
396 * -i : ignore case for all patterns by default
397 * -f : read patterns from those files
Willy Tarreau5adeda12013-03-31 22:13:34 +0200398 * -m : force matching method (must be used before -f)
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200399 * -- : everything after this is not an option
400 */
401 patflags = 0;
402 while (**args == '-') {
403 if ((*args)[1] == 'i')
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100404 patflags |= PAT_F_IGNORE_CASE;
Willy Tarreau2b5285d2010-05-09 23:45:24 +0200405 else if ((*args)[1] == 'f') {
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +0100406 if (!expr->pat.parse) {
Willy Tarreau9987ea92013-06-11 21:09:06 +0200407 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 +0200408 goto out_free_expr;
409 }
410
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100411 if (!pattern_read_from_file(&expr->pat, args[1], patflags | PAT_F_FROM_FILE, err))
Willy Tarreau2b5285d2010-05-09 23:45:24 +0200412 goto out_free_expr;
Willy Tarreau5adeda12013-03-31 22:13:34 +0200413 args++;
414 }
415 else if ((*args)[1] == 'm') {
416 int idx;
417
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +0100418 if (!LIST_ISEMPTY(&expr->pat.patterns) || !eb_is_empty(&expr->pat.pattern_tree)) {
Willy Tarreau5adeda12013-03-31 22:13:34 +0200419 memprintf(err, "'-m' must only be specified before patterns and files in parsing ACL expression");
420 goto out_free_expr;
421 }
422
Willy Tarreau6f8fe312013-11-28 22:24:25 +0100423 idx = pat_find_match_name(args[1]);
Willy Tarreau5adeda12013-03-31 22:13:34 +0200424 if (idx < 0) {
425 memprintf(err, "unknown matching method '%s' when parsing ACL expression", args[1]);
426 goto out_free_expr;
427 }
428
429 /* Note: -m found is always valid, bool/int are compatible, str/bin/reg/len are compatible */
Thierry FOURNIERe3ded592013-12-06 15:36:54 +0100430 if (!sample_casts[cur_type][pat_match_types[idx]]) {
Willy Tarreau93fddf12013-03-31 22:59:32 +0200431 memprintf(err, "matching method '%s' cannot be used with fetch keyword '%s'", args[1], expr->kw);
Willy Tarreau5adeda12013-03-31 22:13:34 +0200432 goto out_free_expr;
433 }
Thierry FOURNIERe3ded592013-12-06 15:36:54 +0100434 expr->pat.parse = pat_parse_fcts[idx];
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100435 expr->pat.index = pat_index_fcts[idx];
Thierry FOURNIERe3ded592013-12-06 15:36:54 +0100436 expr->pat.match = pat_match_fcts[idx];
Willy Tarreau2b5285d2010-05-09 23:45:24 +0200437 args++;
438 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200439 else if ((*args)[1] == '-') {
440 args++;
441 break;
442 }
443 else
444 break;
445 args++;
446 }
447
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +0100448 if (!expr->pat.parse) {
Willy Tarreau9987ea92013-06-11 21:09:06 +0200449 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 +0200450 goto out_free_expr;
451 }
452
Willy Tarreauc8d7c962007-06-17 08:20:33 +0200453 /* now parse all patterns */
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100454 while (**args) {
455 arg = *args;
456
457 /* Compatibility layer. Each pattern can parse only one string per pattern,
458 * but the pat_parser_int() and pat_parse_dotted_ver() parsers were need
459 * optionnaly two operators. The first operator is the match method: eq,
460 * le, lt, ge and gt. pat_parse_int() and pat_parse_dotted_ver() functions
461 * can have a compatibility syntax based on ranges:
462 *
463 * pat_parse_int():
464 *
465 * "eq x" -> "x" or "x:x"
466 * "le x" -> ":x"
467 * "lt x" -> ":y" (with y = x - 1)
468 * "ge x" -> "x:"
469 * "gt x" -> "y:" (with y = x + 1)
470 *
471 * pat_parse_dotted_ver():
472 *
473 * "eq x.y" -> "x.y" or "x.y:x.y"
474 * "le x.y" -> ":x.y"
475 * "lt x.y" -> ":w.z" (with w.z = x.y - 1)
476 * "ge x.y" -> "x.y:"
477 * "gt x.y" -> "w.z:" (with w.z = x.y + 1)
478 *
479 * If y is not present, assume that is "0".
480 *
481 * The syntax eq, le, lt, ge and gt are proper to the acl syntax. The
482 * following block of code detect the operator, and rewrite each value
483 * in parsable string.
484 */
485 if (expr->pat.parse == pat_parse_int ||
486 expr->pat.parse == pat_parse_dotted_ver) {
487 /* Check for operator. If the argument is operator, memorise it and
488 * continue to the next argument.
489 */
490 op = get_std_op(arg);
491 if (op != -1) {
492 operator = op;
493 args++;
494 continue;
495 }
496
497 /* Check if the pattern contain ':' or '-' character. */
498 contain_colon = (strchr(arg, ':') || strchr(arg, '-'));
499
500 /* If the pattern contain ':' or '-' character, give it to the parser as is.
501 * If no contain ':' and operator is STD_OP_EQ, give it to the parser as is.
502 * In other case, try to convert the value according with the operator.
503 */
504 if (!contain_colon && operator != STD_OP_EQ) {
505 /* Search '.' separator. */
506 dot = strchr(arg, '.');
507 if (!dot) {
508 have_dot = 0;
509 minor = 0;
510 dot = arg + strlen(arg);
511 }
512 else
513 have_dot = 1;
514
515 /* convert the integer minor part for the pat_parse_dotted_ver() function. */
516 if (expr->pat.parse == pat_parse_dotted_ver && have_dot) {
517 if (strl2llrc(dot+1, strlen(dot+1), &minor) != 0) {
518 memprintf(err, "'%s' is neither a number nor a supported operator", arg);
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100519 goto out_free_expr;
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100520 }
521 if (minor >= 65536) {
522 memprintf(err, "'%s' contains too large a minor value", arg);
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100523 goto out_free_expr;
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100524 }
525 }
526
527 /* convert the integer value for the pat_parse_int() function, and the
528 * integer major part for the pat_parse_dotted_ver() function.
529 */
530 if (strl2llrc(arg, dot - arg, &value) != 0) {
531 memprintf(err, "'%s' is neither a number nor a supported operator", arg);
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100532 goto out_free_expr;
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100533 }
534 if (expr->pat.parse == pat_parse_dotted_ver) {
535 if (value >= 65536) {
536 memprintf(err, "'%s' contains too large a major value", arg);
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100537 goto out_free_expr;
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100538 }
539 value = (value << 16) | (minor & 0xffff);
540 }
541
542 switch (operator) {
543
544 case STD_OP_EQ: /* this case is not possible. */
545 memprintf(err, "internal error");
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100546 goto out_free_expr;
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100547
548 case STD_OP_GT:
549 value++; /* gt = ge + 1 */
550
551 case STD_OP_GE:
552 if (expr->pat.parse == pat_parse_int)
553 snprintf(buffer, NB_LLMAX_STR+NB_LLMAX_STR+2, "%lld:", value);
554 else
555 snprintf(buffer, NB_LLMAX_STR+NB_LLMAX_STR+2, "%lld.%lld:",
556 value >> 16, value & 0xffff);
557 arg = buffer;
558 break;
559
560 case STD_OP_LT:
561 value--; /* lt = le - 1 */
562
563 case STD_OP_LE:
564 if (expr->pat.parse == pat_parse_int)
565 snprintf(buffer, NB_LLMAX_STR+NB_LLMAX_STR+2, ":%lld", value);
566 else
567 snprintf(buffer, NB_LLMAX_STR+NB_LLMAX_STR+2, ":%lld.%lld",
568 value >> 16, value & 0xffff);
569 arg = buffer;
570 break;
571 }
572 }
573 }
574
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100575 if (!pattern_register(&expr->pat, arg, NULL, patflags, err))
576 goto out_free_expr;
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100577 args++;
578 }
Willy Tarreaua84d3742007-05-07 00:36:48 +0200579
580 return expr;
581
Willy Tarreaua84d3742007-05-07 00:36:48 +0200582 out_free_expr:
583 prune_acl_expr(expr);
584 free(expr);
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100585 free(ckw);
Willy Tarreauc37a3c72013-12-13 01:24:09 +0100586 out_free_smp:
587 free(smp);
Willy Tarreaua84d3742007-05-07 00:36:48 +0200588 out_return:
589 return NULL;
590}
591
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +0200592/* Purge everything in the acl <acl>, then return <acl>. */
593struct acl *prune_acl(struct acl *acl) {
594
595 struct acl_expr *expr, *exprb;
596
597 free(acl->name);
598
599 list_for_each_entry_safe(expr, exprb, &acl->expr, list) {
600 LIST_DEL(&expr->list);
601 prune_acl_expr(expr);
602 free(expr);
603 }
604
605 return acl;
606}
607
Willy Tarreaua84d3742007-05-07 00:36:48 +0200608/* Parse an ACL with the name starting at <args>[0], and with a list of already
609 * known ACLs in <acl>. If the ACL was not in the list, it will be added.
Willy Tarreau2a56c5e2010-03-15 16:13:29 +0100610 * A pointer to that ACL is returned. If the ACL has an empty name, then it's
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200611 * an anonymous one and it won't be merged with any other one. If <err> is not
612 * NULL, it will be filled with an appropriate error. This pointer must be
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200613 * freeable or NULL. <al> is the arg_list serving as a head for unresolved
614 * dependencies.
Willy Tarreaua84d3742007-05-07 00:36:48 +0200615 *
616 * args syntax: <aclname> <acl_expr>
617 */
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200618struct acl *parse_acl(const char **args, struct list *known_acl, char **err, struct arg_list *al)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200619{
620 __label__ out_return, out_free_acl_expr, out_free_name;
621 struct acl *cur_acl;
622 struct acl_expr *acl_expr;
623 char *name;
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200624 const char *pos;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200625
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200626 if (**args && (pos = invalid_char(*args))) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200627 memprintf(err, "invalid character in ACL name : '%c'", *pos);
Willy Tarreau2e74c3f2007-12-02 18:45:09 +0100628 goto out_return;
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200629 }
Willy Tarreau2e74c3f2007-12-02 18:45:09 +0100630
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200631 acl_expr = parse_acl_expr(args + 1, err, al);
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200632 if (!acl_expr) {
633 /* parse_acl_expr will have filled <err> here */
Willy Tarreaua84d3742007-05-07 00:36:48 +0200634 goto out_return;
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200635 }
Willy Tarreaua84d3742007-05-07 00:36:48 +0200636
Willy Tarreau404e8ab2009-07-26 19:40:40 +0200637 /* Check for args beginning with an opening parenthesis just after the
638 * subject, as this is almost certainly a typo. Right now we can only
639 * emit a warning, so let's do so.
640 */
Krzysztof Piotr Oledzki4cdd8312009-10-05 00:23:35 +0200641 if (!strchr(args[1], '(') && *args[2] == '(')
Willy Tarreau404e8ab2009-07-26 19:40:40 +0200642 Warning("parsing acl '%s' :\n"
643 " matching '%s' for pattern '%s' is likely a mistake and probably\n"
644 " not what you want. Maybe you need to remove the extraneous space before '('.\n"
645 " If you are really sure this is not an error, please insert '--' between the\n"
646 " match and the pattern to make this warning message disappear.\n",
647 args[0], args[1], args[2]);
648
Willy Tarreau2a56c5e2010-03-15 16:13:29 +0100649 if (*args[0])
650 cur_acl = find_acl_by_name(args[0], known_acl);
651 else
652 cur_acl = NULL;
653
Willy Tarreaua84d3742007-05-07 00:36:48 +0200654 if (!cur_acl) {
655 name = strdup(args[0]);
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200656 if (!name) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200657 memprintf(err, "out of memory when parsing ACL");
Willy Tarreaua84d3742007-05-07 00:36:48 +0200658 goto out_free_acl_expr;
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200659 }
Willy Tarreaua84d3742007-05-07 00:36:48 +0200660 cur_acl = (struct acl *)calloc(1, sizeof(*cur_acl));
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200661 if (cur_acl == NULL) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200662 memprintf(err, "out of memory when parsing ACL");
Willy Tarreaua84d3742007-05-07 00:36:48 +0200663 goto out_free_name;
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200664 }
Willy Tarreaua84d3742007-05-07 00:36:48 +0200665
666 LIST_INIT(&cur_acl->expr);
667 LIST_ADDQ(known_acl, &cur_acl->list);
668 cur_acl->name = name;
669 }
670
Willy Tarreaua91d0a52013-03-25 08:12:18 +0100671 /* We want to know what features the ACL needs (typically HTTP parsing),
672 * and where it may be used. If an ACL relies on multiple matches, it is
673 * OK if at least one of them may match in the context where it is used.
674 */
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100675 cur_acl->use |= acl_expr->smp->fetch->use;
676 cur_acl->val |= acl_expr->smp->fetch->val;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200677 LIST_ADDQ(&cur_acl->expr, &acl_expr->list);
678 return cur_acl;
679
680 out_free_name:
681 free(name);
682 out_free_acl_expr:
683 prune_acl_expr(acl_expr);
684 free(acl_expr);
685 out_return:
686 return NULL;
687}
688
Willy Tarreau16fbe822007-06-17 11:54:31 +0200689/* Some useful ACLs provided by default. Only those used are allocated. */
690
691const struct {
692 const char *name;
693 const char *expr[4]; /* put enough for longest expression */
694} default_acl_list[] = {
Willy Tarreau58393e12008-07-20 10:39:22 +0200695 { .name = "TRUE", .expr = {"always_true",""}},
696 { .name = "FALSE", .expr = {"always_false",""}},
Willy Tarreau16fbe822007-06-17 11:54:31 +0200697 { .name = "LOCALHOST", .expr = {"src","127.0.0.1/8",""}},
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200698 { .name = "HTTP", .expr = {"req_proto_http",""}},
Willy Tarreau16fbe822007-06-17 11:54:31 +0200699 { .name = "HTTP_1.0", .expr = {"req_ver","1.0",""}},
700 { .name = "HTTP_1.1", .expr = {"req_ver","1.1",""}},
701 { .name = "METH_CONNECT", .expr = {"method","CONNECT",""}},
702 { .name = "METH_GET", .expr = {"method","GET","HEAD",""}},
703 { .name = "METH_HEAD", .expr = {"method","HEAD",""}},
704 { .name = "METH_OPTIONS", .expr = {"method","OPTIONS",""}},
705 { .name = "METH_POST", .expr = {"method","POST",""}},
706 { .name = "METH_TRACE", .expr = {"method","TRACE",""}},
707 { .name = "HTTP_URL_ABS", .expr = {"url_reg","^[^/:]*://",""}},
708 { .name = "HTTP_URL_SLASH", .expr = {"url_beg","/",""}},
709 { .name = "HTTP_URL_STAR", .expr = {"url","*",""}},
710 { .name = "HTTP_CONTENT", .expr = {"hdr_val(content-length)","gt","0",""}},
Emeric Brunbede3d02009-06-30 17:54:00 +0200711 { .name = "RDP_COOKIE", .expr = {"req_rdp_cookie_cnt","gt","0",""}},
Willy Tarreauc6317702008-07-20 09:29:50 +0200712 { .name = "REQ_CONTENT", .expr = {"req_len","gt","0",""}},
Willy Tarreaub6fb4202008-07-20 11:18:28 +0200713 { .name = "WAIT_END", .expr = {"wait_end",""}},
Willy Tarreau16fbe822007-06-17 11:54:31 +0200714 { .name = NULL, .expr = {""}}
715};
716
717/* Find a default ACL from the default_acl list, compile it and return it.
718 * If the ACL is not found, NULL is returned. In theory, it cannot fail,
719 * except when default ACLs are broken, in which case it will return NULL.
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200720 * If <known_acl> is not NULL, the ACL will be queued at its tail. If <err> is
721 * not NULL, it will be filled with an error message if an error occurs. This
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200722 * pointer must be freeable or NULL. <al> is an arg_list serving as a list head
723 * to report missing dependencies.
Willy Tarreau16fbe822007-06-17 11:54:31 +0200724 */
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200725static struct acl *find_acl_default(const char *acl_name, struct list *known_acl,
726 char **err, struct arg_list *al)
Willy Tarreau16fbe822007-06-17 11:54:31 +0200727{
728 __label__ out_return, out_free_acl_expr, out_free_name;
729 struct acl *cur_acl;
730 struct acl_expr *acl_expr;
731 char *name;
732 int index;
733
734 for (index = 0; default_acl_list[index].name != NULL; index++) {
735 if (strcmp(acl_name, default_acl_list[index].name) == 0)
736 break;
737 }
738
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200739 if (default_acl_list[index].name == NULL) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200740 memprintf(err, "no such ACL : '%s'", acl_name);
Willy Tarreau16fbe822007-06-17 11:54:31 +0200741 return NULL;
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200742 }
Willy Tarreau16fbe822007-06-17 11:54:31 +0200743
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200744 acl_expr = parse_acl_expr((const char **)default_acl_list[index].expr, err, al);
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200745 if (!acl_expr) {
746 /* parse_acl_expr must have filled err here */
Willy Tarreau16fbe822007-06-17 11:54:31 +0200747 goto out_return;
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200748 }
Willy Tarreau16fbe822007-06-17 11:54:31 +0200749
750 name = strdup(acl_name);
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200751 if (!name) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200752 memprintf(err, "out of memory when building default ACL '%s'", acl_name);
Willy Tarreau16fbe822007-06-17 11:54:31 +0200753 goto out_free_acl_expr;
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200754 }
755
Willy Tarreau16fbe822007-06-17 11:54:31 +0200756 cur_acl = (struct acl *)calloc(1, sizeof(*cur_acl));
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200757 if (cur_acl == NULL) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200758 memprintf(err, "out of memory when building default ACL '%s'", acl_name);
Willy Tarreau16fbe822007-06-17 11:54:31 +0200759 goto out_free_name;
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200760 }
Willy Tarreau16fbe822007-06-17 11:54:31 +0200761
762 cur_acl->name = name;
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100763 cur_acl->use |= acl_expr->smp->fetch->use;
764 cur_acl->val |= acl_expr->smp->fetch->val;
Willy Tarreau16fbe822007-06-17 11:54:31 +0200765 LIST_INIT(&cur_acl->expr);
766 LIST_ADDQ(&cur_acl->expr, &acl_expr->list);
767 if (known_acl)
768 LIST_ADDQ(known_acl, &cur_acl->list);
769
770 return cur_acl;
771
772 out_free_name:
773 free(name);
774 out_free_acl_expr:
775 prune_acl_expr(acl_expr);
776 free(acl_expr);
777 out_return:
778 return NULL;
779}
Willy Tarreaua84d3742007-05-07 00:36:48 +0200780
781/* Purge everything in the acl_cond <cond>, then return <cond>. */
782struct acl_cond *prune_acl_cond(struct acl_cond *cond)
783{
784 struct acl_term_suite *suite, *tmp_suite;
785 struct acl_term *term, *tmp_term;
786
787 /* iterate through all term suites and free all terms and all suites */
788 list_for_each_entry_safe(suite, tmp_suite, &cond->suites, list) {
789 list_for_each_entry_safe(term, tmp_term, &suite->terms, list)
790 free(term);
791 free(suite);
792 }
793 return cond;
794}
795
796/* Parse an ACL condition starting at <args>[0], relying on a list of already
797 * known ACLs passed in <known_acl>. The new condition is returned (or NULL in
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200798 * case of low memory). Supports multiple conditions separated by "or". If
799 * <err> is not NULL, it will be filled with a pointer to an error message in
800 * case of error, that the caller is responsible for freeing. The initial
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200801 * location must either be freeable or NULL. The list <al> serves as a list head
802 * for unresolved dependencies.
Willy Tarreaua84d3742007-05-07 00:36:48 +0200803 */
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200804struct acl_cond *parse_acl_cond(const char **args, struct list *known_acl,
Willy Tarreau0cba6072013-11-28 22:21:02 +0100805 enum acl_cond_pol pol, char **err, struct arg_list *al)
Willy Tarreaua84d3742007-05-07 00:36:48 +0200806{
807 __label__ out_return, out_free_suite, out_free_term;
Willy Tarreau74b98a82007-06-16 19:35:18 +0200808 int arg, neg;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200809 const char *word;
810 struct acl *cur_acl;
811 struct acl_term *cur_term;
812 struct acl_term_suite *cur_suite;
813 struct acl_cond *cond;
Willy Tarreaua91d0a52013-03-25 08:12:18 +0100814 unsigned int suite_val;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200815
816 cond = (struct acl_cond *)calloc(1, sizeof(*cond));
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200817 if (cond == NULL) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200818 memprintf(err, "out of memory when parsing condition");
Willy Tarreaua84d3742007-05-07 00:36:48 +0200819 goto out_return;
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200820 }
Willy Tarreaua84d3742007-05-07 00:36:48 +0200821
822 LIST_INIT(&cond->list);
823 LIST_INIT(&cond->suites);
824 cond->pol = pol;
Willy Tarreaua91d0a52013-03-25 08:12:18 +0100825 cond->val = 0;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200826
827 cur_suite = NULL;
Willy Tarreaua91d0a52013-03-25 08:12:18 +0100828 suite_val = ~0U;
Willy Tarreau74b98a82007-06-16 19:35:18 +0200829 neg = 0;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200830 for (arg = 0; *args[arg]; arg++) {
831 word = args[arg];
832
833 /* remove as many exclamation marks as we can */
834 while (*word == '!') {
835 neg = !neg;
836 word++;
837 }
838
839 /* an empty word is allowed because we cannot force the user to
840 * always think about not leaving exclamation marks alone.
841 */
842 if (!*word)
843 continue;
844
Willy Tarreau16fbe822007-06-17 11:54:31 +0200845 if (strcasecmp(word, "or") == 0 || strcmp(word, "||") == 0) {
Willy Tarreaua84d3742007-05-07 00:36:48 +0200846 /* new term suite */
Willy Tarreaua91d0a52013-03-25 08:12:18 +0100847 cond->val |= suite_val;
848 suite_val = ~0U;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200849 cur_suite = NULL;
850 neg = 0;
851 continue;
852 }
853
Willy Tarreau95fa4692010-02-01 13:05:50 +0100854 if (strcmp(word, "{") == 0) {
855 /* we may have a complete ACL expression between two braces,
856 * find the last one.
857 */
858 int arg_end = arg + 1;
859 const char **args_new;
860
861 while (*args[arg_end] && strcmp(args[arg_end], "}") != 0)
862 arg_end++;
863
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200864 if (!*args[arg_end]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200865 memprintf(err, "missing closing '}' in condition");
Willy Tarreau95fa4692010-02-01 13:05:50 +0100866 goto out_free_suite;
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200867 }
Willy Tarreau95fa4692010-02-01 13:05:50 +0100868
869 args_new = calloc(1, (arg_end - arg + 1) * sizeof(*args_new));
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200870 if (!args_new) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200871 memprintf(err, "out of memory when parsing condition");
Willy Tarreau95fa4692010-02-01 13:05:50 +0100872 goto out_free_suite;
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200873 }
Willy Tarreau95fa4692010-02-01 13:05:50 +0100874
Willy Tarreau2a56c5e2010-03-15 16:13:29 +0100875 args_new[0] = "";
Willy Tarreau95fa4692010-02-01 13:05:50 +0100876 memcpy(args_new + 1, args + arg + 1, (arg_end - arg) * sizeof(*args_new));
877 args_new[arg_end - arg] = "";
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200878 cur_acl = parse_acl(args_new, known_acl, err, al);
Willy Tarreau95fa4692010-02-01 13:05:50 +0100879 free(args_new);
880
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200881 if (!cur_acl) {
882 /* note that parse_acl() must have filled <err> here */
Willy Tarreau16fbe822007-06-17 11:54:31 +0200883 goto out_free_suite;
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200884 }
Willy Tarreaua91d0a52013-03-25 08:12:18 +0100885 word = args[arg + 1];
Willy Tarreau95fa4692010-02-01 13:05:50 +0100886 arg = arg_end;
887 }
888 else {
889 /* search for <word> in the known ACL names. If we do not find
890 * it, let's look for it in the default ACLs, and if found, add
891 * it to the list of ACLs of this proxy. This makes it possible
892 * to override them.
893 */
894 cur_acl = find_acl_by_name(word, known_acl);
895 if (cur_acl == NULL) {
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200896 cur_acl = find_acl_default(word, known_acl, err, al);
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200897 if (cur_acl == NULL) {
898 /* note that find_acl_default() must have filled <err> here */
Willy Tarreau95fa4692010-02-01 13:05:50 +0100899 goto out_free_suite;
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200900 }
Willy Tarreau95fa4692010-02-01 13:05:50 +0100901 }
Willy Tarreau16fbe822007-06-17 11:54:31 +0200902 }
Willy Tarreaua84d3742007-05-07 00:36:48 +0200903
904 cur_term = (struct acl_term *)calloc(1, sizeof(*cur_term));
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200905 if (cur_term == NULL) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200906 memprintf(err, "out of memory when parsing condition");
Willy Tarreaua84d3742007-05-07 00:36:48 +0200907 goto out_free_suite;
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200908 }
Willy Tarreaua84d3742007-05-07 00:36:48 +0200909
910 cur_term->acl = cur_acl;
911 cur_term->neg = neg;
Willy Tarreaua91d0a52013-03-25 08:12:18 +0100912
913 /* Here it is a bit complex. The acl_term_suite is a conjunction
914 * of many terms. It may only be used if all of its terms are
915 * usable at the same time. So the suite's validity domain is an
916 * AND between all ACL keywords' ones. But, the global condition
917 * is valid if at least one term suite is OK. So it's an OR between
918 * all of their validity domains. We could emit a warning as soon
919 * as suite_val is null because it means that the last ACL is not
920 * compatible with the previous ones. Let's remain simple for now.
921 */
922 cond->use |= cur_acl->use;
923 suite_val &= cur_acl->val;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200924
925 if (!cur_suite) {
926 cur_suite = (struct acl_term_suite *)calloc(1, sizeof(*cur_suite));
Willy Tarreauf678b7f2013-01-24 00:25:39 +0100927 if (cur_suite == NULL) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200928 memprintf(err, "out of memory when parsing condition");
Willy Tarreaua84d3742007-05-07 00:36:48 +0200929 goto out_free_term;
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200930 }
Willy Tarreaua84d3742007-05-07 00:36:48 +0200931 LIST_INIT(&cur_suite->terms);
932 LIST_ADDQ(&cond->suites, &cur_suite->list);
933 }
934 LIST_ADDQ(&cur_suite->terms, &cur_term->list);
Willy Tarreau74b98a82007-06-16 19:35:18 +0200935 neg = 0;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200936 }
937
Willy Tarreaua91d0a52013-03-25 08:12:18 +0100938 cond->val |= suite_val;
Willy Tarreaua84d3742007-05-07 00:36:48 +0200939 return cond;
940
941 out_free_term:
942 free(cur_term);
943 out_free_suite:
944 prune_acl_cond(cond);
945 free(cond);
946 out_return:
947 return NULL;
948}
949
Willy Tarreau2bbba412010-01-28 16:48:33 +0100950/* Builds an ACL condition starting at the if/unless keyword. The complete
951 * condition is returned. NULL is returned in case of error or if the first
952 * word is neither "if" nor "unless". It automatically sets the file name and
Willy Tarreau25320b22013-03-24 07:22:08 +0100953 * the line number in the condition for better error reporting, and sets the
954 * HTTP intiailization requirements in the proxy. If <err> is not NULL, it will
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200955 * be filled with a pointer to an error message in case of error, that the
956 * caller is responsible for freeing. The initial location must either be
957 * freeable or NULL.
Willy Tarreau2bbba412010-01-28 16:48:33 +0100958 */
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200959struct 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 +0100960{
Willy Tarreau0cba6072013-11-28 22:21:02 +0100961 enum acl_cond_pol pol = ACL_COND_NONE;
Willy Tarreau2bbba412010-01-28 16:48:33 +0100962 struct acl_cond *cond = NULL;
963
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200964 if (err)
965 *err = NULL;
966
Willy Tarreau2bbba412010-01-28 16:48:33 +0100967 if (!strcmp(*args, "if")) {
968 pol = ACL_COND_IF;
969 args++;
970 }
971 else if (!strcmp(*args, "unless")) {
972 pol = ACL_COND_UNLESS;
973 args++;
974 }
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200975 else {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200976 memprintf(err, "conditions must start with either 'if' or 'unless'");
Willy Tarreau2bbba412010-01-28 16:48:33 +0100977 return NULL;
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200978 }
Willy Tarreau2bbba412010-01-28 16:48:33 +0100979
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200980 cond = parse_acl_cond(args, &px->acl, pol, err, &px->conf.args);
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200981 if (!cond) {
982 /* note that parse_acl_cond must have filled <err> here */
Willy Tarreau2bbba412010-01-28 16:48:33 +0100983 return NULL;
Willy Tarreaub7451bb2012-04-27 12:38:15 +0200984 }
Willy Tarreau2bbba412010-01-28 16:48:33 +0100985
986 cond->file = file;
987 cond->line = line;
Willy Tarreaua91d0a52013-03-25 08:12:18 +0100988 px->http_needed |= !!(cond->use & SMP_USE_HTTP_ANY);
Willy Tarreau2bbba412010-01-28 16:48:33 +0100989 return cond;
990}
991
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100992/* Execute condition <cond> and return either ACL_TEST_FAIL, ACL_TEST_MISS or
993 * ACL_TEST_PASS depending on the test results. ACL_TEST_MISS may only be
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200994 * returned if <opt> does not contain SMP_OPT_FINAL, indicating that incomplete
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100995 * data is being examined. The function automatically sets SMP_OPT_ITERATE. This
996 * function only computes the condition, it does not apply the polarity required
997 * by IF/UNLESS, it's up to the caller to do this using something like this :
Willy Tarreau11382812008-07-09 16:18:21 +0200998 *
999 * res = acl_pass(res);
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01001000 * if (res == ACL_TEST_MISS)
Willy Tarreaub6866442008-07-14 23:54:42 +02001001 * return 0;
Willy Tarreau11382812008-07-09 16:18:21 +02001002 * if (cond->pol == ACL_COND_UNLESS)
1003 * res = !res;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001004 */
Willy Tarreau0cba6072013-11-28 22:21:02 +01001005enum 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 +02001006{
1007 __label__ fetch_next;
1008 struct acl_term_suite *suite;
1009 struct acl_term *term;
1010 struct acl_expr *expr;
1011 struct acl *acl;
Willy Tarreau37406352012-04-23 16:16:37 +02001012 struct sample smp;
Willy Tarreau0cba6072013-11-28 22:21:02 +01001013 enum acl_test_res acl_res, suite_res, cond_res;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001014
Willy Tarreau7a777ed2012-04-26 11:44:02 +02001015 /* ACLs are iterated over all values, so let's always set the flag to
1016 * indicate this to the fetch functions.
1017 */
1018 opt |= SMP_OPT_ITERATE;
1019
Willy Tarreau11382812008-07-09 16:18:21 +02001020 /* We're doing a logical OR between conditions so we initialize to FAIL.
1021 * The MISS status is propagated down from the suites.
1022 */
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01001023 cond_res = ACL_TEST_FAIL;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001024 list_for_each_entry(suite, &cond->suites, list) {
Willy Tarreau11382812008-07-09 16:18:21 +02001025 /* Evaluate condition suite <suite>. We stop at the first term
Willy Tarreau0cba6072013-11-28 22:21:02 +01001026 * which returns ACL_TEST_FAIL. The MISS status is still propagated
Willy Tarreau11382812008-07-09 16:18:21 +02001027 * in case of uncertainty in the result.
Willy Tarreaua84d3742007-05-07 00:36:48 +02001028 */
1029
1030 /* we're doing a logical AND between terms, so we must set the
1031 * initial value to PASS.
1032 */
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01001033 suite_res = ACL_TEST_PASS;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001034 list_for_each_entry(term, &suite->terms, list) {
1035 acl = term->acl;
1036
1037 /* FIXME: use cache !
1038 * check acl->cache_idx for this.
1039 */
1040
1041 /* ACL result not cached. Let's scan all the expressions
1042 * and use the first one to match.
1043 */
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01001044 acl_res = ACL_TEST_FAIL;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001045 list_for_each_entry(expr, &acl->expr, list) {
Willy Tarreaud41f8d82007-06-10 10:06:18 +02001046 /* we need to reset context and flags */
Willy Tarreau37406352012-04-23 16:16:37 +02001047 memset(&smp, 0, sizeof(smp));
Willy Tarreaua84d3742007-05-07 00:36:48 +02001048 fetch_next:
Thierry FOURNIER348971e2013-11-21 10:50:10 +01001049 if (!sample_process(px, l4, l7, opt, expr->smp, &smp)) {
Willy Tarreaub6866442008-07-14 23:54:42 +02001050 /* maybe we could not fetch because of missing data */
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001051 if (smp.flags & SMP_F_MAY_CHANGE && !(opt & SMP_OPT_FINAL))
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01001052 acl_res |= ACL_TEST_MISS;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001053 continue;
Willy Tarreaub6866442008-07-14 23:54:42 +02001054 }
Willy Tarreauc4262962010-05-10 23:42:40 +02001055
Thierry FOURNIER76090642013-12-10 15:03:38 +01001056 acl_res |= pat2acl(pattern_exec_match(&expr->pat, &smp, NULL, NULL, NULL));
Willy Tarreaua84d3742007-05-07 00:36:48 +02001057 /*
Willy Tarreau11382812008-07-09 16:18:21 +02001058 * OK now acl_res holds the result of this expression
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01001059 * as one of ACL_TEST_FAIL, ACL_TEST_MISS or ACL_TEST_PASS.
Willy Tarreaua84d3742007-05-07 00:36:48 +02001060 *
Willy Tarreau11382812008-07-09 16:18:21 +02001061 * Then if (!MISS) we can cache the result, and put
Willy Tarreau37406352012-04-23 16:16:37 +02001062 * (smp.flags & SMP_F_VOLATILE) in the cache flags.
Willy Tarreaua84d3742007-05-07 00:36:48 +02001063 *
1064 * FIXME: implement cache.
1065 *
1066 */
1067
Willy Tarreau11382812008-07-09 16:18:21 +02001068 /* we're ORing these terms, so a single PASS is enough */
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01001069 if (acl_res == ACL_TEST_PASS)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001070 break;
1071
Willy Tarreau37406352012-04-23 16:16:37 +02001072 if (smp.flags & SMP_F_NOT_LAST)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001073 goto fetch_next;
Willy Tarreaub6866442008-07-14 23:54:42 +02001074
1075 /* sometimes we know the fetched data is subject to change
1076 * later and give another chance for a new match (eg: request
1077 * size, time, ...)
1078 */
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001079 if (smp.flags & SMP_F_MAY_CHANGE && !(opt & SMP_OPT_FINAL))
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01001080 acl_res |= ACL_TEST_MISS;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001081 }
1082 /*
1083 * Here we have the result of an ACL (cached or not).
1084 * ACLs are combined, negated or not, to form conditions.
1085 */
1086
Willy Tarreaua84d3742007-05-07 00:36:48 +02001087 if (term->neg)
Willy Tarreau11382812008-07-09 16:18:21 +02001088 acl_res = acl_neg(acl_res);
Willy Tarreaua84d3742007-05-07 00:36:48 +02001089
1090 suite_res &= acl_res;
Willy Tarreau11382812008-07-09 16:18:21 +02001091
Willy Tarreau79c412b2013-10-30 19:30:32 +01001092 /* we're ANDing these terms, so a single FAIL or MISS is enough */
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01001093 if (suite_res != ACL_TEST_PASS)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001094 break;
1095 }
1096 cond_res |= suite_res;
Willy Tarreau11382812008-07-09 16:18:21 +02001097
1098 /* we're ORing these terms, so a single PASS is enough */
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01001099 if (cond_res == ACL_TEST_PASS)
Willy Tarreaua84d3742007-05-07 00:36:48 +02001100 break;
1101 }
Willy Tarreau11382812008-07-09 16:18:21 +02001102 return cond_res;
Willy Tarreaua84d3742007-05-07 00:36:48 +02001103}
1104
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001105/* Returns a pointer to the first ACL conflicting with usage at place <where>
1106 * which is one of the SMP_VAL_* bits indicating a check place, or NULL if
1107 * no conflict is found. Only full conflicts are detected (ACL is not usable).
1108 * Use the next function to check for useless keywords.
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001109 */
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001110const struct acl *acl_cond_conflicts(const struct acl_cond *cond, unsigned int where)
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001111{
1112 struct acl_term_suite *suite;
1113 struct acl_term *term;
1114 struct acl *acl;
1115
1116 list_for_each_entry(suite, &cond->suites, list) {
1117 list_for_each_entry(term, &suite->terms, list) {
1118 acl = term->acl;
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001119 if (!(acl->val & where))
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001120 return acl;
1121 }
1122 }
1123 return NULL;
1124}
1125
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001126/* Returns a pointer to the first ACL and its first keyword to conflict with
1127 * usage at place <where> which is one of the SMP_VAL_* bits indicating a check
1128 * place. Returns true if a conflict is found, with <acl> and <kw> set (if non
1129 * null), or false if not conflict is found. The first useless keyword is
1130 * returned.
1131 */
Willy Tarreau93fddf12013-03-31 22:59:32 +02001132int 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 +01001133{
1134 struct acl_term_suite *suite;
1135 struct acl_term *term;
1136 struct acl_expr *expr;
1137
1138 list_for_each_entry(suite, &cond->suites, list) {
1139 list_for_each_entry(term, &suite->terms, list) {
1140 list_for_each_entry(expr, &term->acl->expr, list) {
Thierry FOURNIER348971e2013-11-21 10:50:10 +01001141 if (!(expr->smp->fetch->val & where)) {
Willy Tarreaua91d0a52013-03-25 08:12:18 +01001142 if (acl)
1143 *acl = term->acl;
1144 if (kw)
1145 *kw = expr->kw;
1146 return 1;
1147 }
1148 }
1149 }
1150 }
1151 return 0;
1152}
1153
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001154/*
1155 * Find targets for userlist and groups in acl. Function returns the number
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001156 * of errors or OK if everything is fine. It must be called only once sample
1157 * fetch arguments have been resolved (after smp_resolve_args()).
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001158 */
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001159int acl_find_targets(struct proxy *p)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001160{
1161
1162 struct acl *acl;
1163 struct acl_expr *expr;
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001164 struct pattern_list *pattern;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001165 int cfgerr = 0;
1166
1167 list_for_each_entry(acl, &p->acl, list) {
1168 list_for_each_entry(expr, &acl->expr, list) {
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001169 if (!strcmp(expr->kw, "http_auth_group")) {
1170 /* Note: the ARGT_USR argument may only have been resolved earlier
1171 * by smp_resolve_args().
1172 */
Thierry FOURNIER348971e2013-11-21 10:50:10 +01001173 if (expr->smp->arg_p->unresolved) {
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001174 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 +01001175 p->id, *acl->name ? "" : "anonymous ", acl->name, expr->kw, expr->smp->arg_p->data.str.str);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001176 cfgerr++;
Willy Tarreau496aa012012-06-01 10:38:29 +02001177 continue;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001178 }
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001179
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +01001180 if (LIST_ISEMPTY(&expr->pat.patterns)) {
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001181 Alert("proxy %s: acl %s %s(): no groups specified.\n",
Willy Tarreau93fddf12013-03-31 22:59:32 +02001182 p->id, acl->name, expr->kw);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001183 cfgerr++;
1184 continue;
1185 }
1186
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01001187 /* For each pattern, check if the group exists. */
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +01001188 list_for_each_entry(pattern, &expr->pat.patterns, list) {
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001189 if (!check_group(expr->smp->arg_p->data.usr, pattern->pat.ptr.str)) {
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001190 Alert("proxy %s: acl %s %s(): invalid group '%s'.\n",
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001191 p->id, acl->name, expr->kw, pattern->pat.ptr.str);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001192 cfgerr++;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001193 }
1194 }
1195 }
1196 }
1197 }
1198
1199 return cfgerr;
1200}
Willy Tarreaudd64f8d2008-07-27 22:02:32 +02001201
Willy Tarreau8ed669b2013-01-11 15:49:37 +01001202/* initializes ACLs by resolving the sample fetch names they rely upon.
1203 * Returns 0 on success, otherwise an error.
1204 */
1205int init_acl()
1206{
1207 int err = 0;
1208 int index;
1209 const char *name;
1210 struct acl_kw_list *kwl;
1211 struct sample_fetch *smp;
1212
1213 list_for_each_entry(kwl, &acl_keywords.list, list) {
1214 for (index = 0; kwl->kw[index].kw != NULL; index++) {
1215 name = kwl->kw[index].fetch_kw;
1216 if (!name)
1217 name = kwl->kw[index].kw;
1218
1219 smp = find_sample_fetch(name, strlen(name));
1220 if (!smp) {
1221 Alert("Critical internal error: ACL keyword '%s' relies on sample fetch '%s' which was not registered!\n",
1222 kwl->kw[index].kw, name);
1223 err++;
1224 continue;
1225 }
1226 kwl->kw[index].smp = smp;
1227 }
1228 }
1229 return err;
1230}
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001231
Willy Tarreaua84d3742007-05-07 00:36:48 +02001232/************************************************************************/
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001233/* All supported sample and ACL keywords must be declared here. */
1234/************************************************************************/
1235
1236/* Note: must not be declared <const> as its list will be overwritten.
Willy Tarreau61612d42012-04-19 18:42:05 +02001237 * Please take care of keeping this list alphabetically sorted.
1238 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02001239static struct acl_kw_list acl_kws = {ILH, {
Willy Tarreaud4c33c82013-01-07 21:59:07 +01001240 { /* END */ },
Willy Tarreaua84d3742007-05-07 00:36:48 +02001241}};
1242
Willy Tarreaua84d3742007-05-07 00:36:48 +02001243__attribute__((constructor))
1244static void __acl_init(void)
1245{
Willy Tarreaua84d3742007-05-07 00:36:48 +02001246 acl_register_keywords(&acl_kws);
1247}
1248
1249
1250/*
1251 * Local variables:
1252 * c-indent-level: 8
1253 * c-basic-offset: 8
1254 * End:
1255 */