blob: 29ac3350bfd86f553a515f7b0eeaf74b06c23a09 [file] [log] [blame]
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02001#include <ctype.h>
2
3#include <common/cfgparse.h>
4#include <common/mini-clist.h>
5
6#include <types/vars.h>
7
8#include <proto/arg.h>
9#include <proto/proto_http.h>
10#include <proto/proto_tcp.h>
11#include <proto/sample.h>
12#include <proto/stream.h>
Willy Tarreauebcd4842015-06-19 11:59:02 +020013#include <proto/vars.h>
Thierry FOURNIER4834bc72015-06-06 19:29:07 +020014
15/* This contains a pool of struct vars */
16static struct pool_head *var_pool = NULL;
17
18/* This array contain all the names of all the HAProxy vars.
19 * This permits to identify two variables name with
20 * only one pointer. It permits to not using strdup() for
21 * each variable name used during the runtime.
22 */
23static char **var_names = NULL;
24static int var_names_nb = 0;
25
26/* This array of int contains the system limits per context. */
27static unsigned int var_global_limit = 0;
28static unsigned int var_global_size = 0;
Christopher Fauletff2613e2016-11-09 11:36:17 +010029static unsigned int var_proc_limit = 0;
Thierry FOURNIER4834bc72015-06-06 19:29:07 +020030static unsigned int var_sess_limit = 0;
31static unsigned int var_txn_limit = 0;
32static unsigned int var_reqres_limit = 0;
33
Willy Tarreau72330982015-06-19 11:21:56 +020034/* This function adds or remove memory size from the accounting. The inner
35 * pointers may be null when setting the outer ones only.
36 */
Willy Tarreau6204cd92016-03-10 16:33:04 +010037static void var_accounting_diff(struct vars *vars, struct session *sess, struct stream *strm, int size)
Thierry FOURNIER4834bc72015-06-06 19:29:07 +020038{
39 switch (vars->scope) {
40 case SCOPE_REQ:
41 case SCOPE_RES:
Willy Tarreau6204cd92016-03-10 16:33:04 +010042 strm->vars_reqres.size += size;
43 /* fall through */
Thierry FOURNIER4834bc72015-06-06 19:29:07 +020044 case SCOPE_TXN:
Willy Tarreau6204cd92016-03-10 16:33:04 +010045 strm->vars_txn.size += size;
46 /* fall through */
Thierry FOURNIER4834bc72015-06-06 19:29:07 +020047 case SCOPE_SESS:
Willy Tarreau6204cd92016-03-10 16:33:04 +010048 sess->vars.size += size;
Christopher Fauletff2613e2016-11-09 11:36:17 +010049 /* fall through */
50 case SCOPE_PROC:
51 global.vars.size += size;
52 var_global_size += size;
Thierry FOURNIER4834bc72015-06-06 19:29:07 +020053 }
54}
55
56/* This function returns 1 if the <size> is available in the var
57 * pool <vars>, otherwise returns 0. If the space is avalaible,
Willy Tarreau72330982015-06-19 11:21:56 +020058 * the size is reserved. The inner pointers may be null when setting
Willy Tarreau6204cd92016-03-10 16:33:04 +010059 * the outer ones only. The accounting uses either <sess> or <strm>
60 * depending on the scope. <strm> may be NULL when no stream is known
61 * and only the session exists (eg: tcp-request connection).
Thierry FOURNIER4834bc72015-06-06 19:29:07 +020062 */
Willy Tarreau6204cd92016-03-10 16:33:04 +010063static int var_accounting_add(struct vars *vars, struct session *sess, struct stream *strm, int size)
Thierry FOURNIER4834bc72015-06-06 19:29:07 +020064{
65 switch (vars->scope) {
66 case SCOPE_REQ:
67 case SCOPE_RES:
Willy Tarreau6204cd92016-03-10 16:33:04 +010068 if (var_reqres_limit && strm->vars_reqres.size + size > var_reqres_limit)
Thierry FOURNIER4834bc72015-06-06 19:29:07 +020069 return 0;
Willy Tarreau6204cd92016-03-10 16:33:04 +010070 /* fall through */
Thierry FOURNIER4834bc72015-06-06 19:29:07 +020071 case SCOPE_TXN:
Willy Tarreau6204cd92016-03-10 16:33:04 +010072 if (var_txn_limit && strm->vars_txn.size + size > var_txn_limit)
Thierry FOURNIER4834bc72015-06-06 19:29:07 +020073 return 0;
Willy Tarreau6204cd92016-03-10 16:33:04 +010074 /* fall through */
Thierry FOURNIER4834bc72015-06-06 19:29:07 +020075 case SCOPE_SESS:
Willy Tarreau6204cd92016-03-10 16:33:04 +010076 if (var_sess_limit && sess->vars.size + size > var_sess_limit)
Thierry FOURNIER4834bc72015-06-06 19:29:07 +020077 return 0;
Christopher Fauletff2613e2016-11-09 11:36:17 +010078 /* fall through */
79 case SCOPE_PROC:
80 if (var_proc_limit && global.vars.size + size > var_proc_limit)
81 return 0;
Thierry FOURNIER4834bc72015-06-06 19:29:07 +020082 if (var_global_limit && var_global_size + size > var_global_limit)
83 return 0;
84 }
Willy Tarreau6204cd92016-03-10 16:33:04 +010085 var_accounting_diff(vars, sess, strm, size);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +020086 return 1;
87}
88
Christopher Faulet85d79c92016-11-09 16:54:56 +010089/* This fnuction remove a variable from the list and free memory it used */
90unsigned int var_clear(struct var *var)
91{
92 unsigned int size = 0;
93
94 if (var->data.type == SMP_T_STR || var->data.type == SMP_T_BIN) {
95 free(var->data.u.str.str);
96 size += var->data.u.str.len;
97 }
98 else if (var->data.type == SMP_T_METH) {
99 free(var->data.u.meth.str.str);
100 size += var->data.u.meth.str.len;
101 }
102 LIST_DEL(&var->l);
103 pool_free2(var_pool, var);
104 size += sizeof(struct var);
105 return size;
106}
107
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200108/* This function free all the memory used by all the varaibles
109 * in the list.
110 */
Willy Tarreau6204cd92016-03-10 16:33:04 +0100111void vars_prune(struct vars *vars, struct session *sess, struct stream *strm)
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200112{
113 struct var *var, *tmp;
Willy Tarreau72330982015-06-19 11:21:56 +0200114 unsigned int size = 0;
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200115
116 list_for_each_entry_safe(var, tmp, &vars->head, l) {
Christopher Faulet85d79c92016-11-09 16:54:56 +0100117 size += var_clear(var);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200118 }
Willy Tarreau6204cd92016-03-10 16:33:04 +0100119 var_accounting_diff(vars, sess, strm, -size);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200120}
121
Willy Tarreauebcd4842015-06-19 11:59:02 +0200122/* This function frees all the memory used by all the session variables in the
123 * list starting at <vars>.
124 */
125void vars_prune_per_sess(struct vars *vars)
126{
127 struct var *var, *tmp;
128 unsigned int size = 0;
129
130 list_for_each_entry_safe(var, tmp, &vars->head, l) {
Christopher Faulet85d79c92016-11-09 16:54:56 +0100131 size += var_clear(var);
Willy Tarreauebcd4842015-06-19 11:59:02 +0200132 }
Christopher Fauletff2613e2016-11-09 11:36:17 +0100133 vars->size -= size;
134 global.vars.size -= size;
135 var_global_size -= size;
Willy Tarreauebcd4842015-06-19 11:59:02 +0200136}
137
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200138/* This function init a list of variabes. */
139void vars_init(struct vars *vars, enum vars_scope scope)
140{
141 LIST_INIT(&vars->head);
142 vars->scope = scope;
143 vars->size = 0;
144}
145
146/* This function declares a new variable name. It returns a pointer
147 * on the string identifying the name. This function assures that
148 * the same name exists only once.
149 *
150 * This function check if the variable name is acceptable.
151 *
152 * The function returns NULL if an error occurs, and <err> is filled.
153 * In this case, the HAProxy must be stopped because the structs are
154 * left inconsistent. Otherwise, it returns the pointer on the global
155 * name.
156 */
Christopher Faulet09c9df22016-10-31 11:05:37 +0100157static char *register_name(const char *name, int len, enum vars_scope *scope,
158 int alloc, char **err)
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200159{
160 int i;
Hubert Verstraete831962e2016-06-28 22:44:26 +0200161 char **var_names2;
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200162 const char *tmp;
163
164 /* Check length. */
165 if (len == 0) {
166 memprintf(err, "Empty variable name cannot be accepted");
167 return NULL;
168 }
169
170 /* Check scope. */
Christopher Fauletff2613e2016-11-09 11:36:17 +0100171 if (len > 5 && strncmp(name, "proc.", 5) == 0) {
172 name += 5;
173 len -= 5;
174 *scope = SCOPE_PROC;
175 }
176 else if (len > 5 && strncmp(name, "sess.", 5) == 0) {
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200177 name += 5;
178 len -= 5;
179 *scope = SCOPE_SESS;
180 }
181 else if (len > 4 && strncmp(name, "txn.", 4) == 0) {
182 name += 4;
183 len -= 4;
184 *scope = SCOPE_TXN;
185 }
186 else if (len > 4 && strncmp(name, "req.", 4) == 0) {
187 name += 4;
188 len -= 4;
189 *scope = SCOPE_REQ;
190 }
191 else if (len > 4 && strncmp(name, "res.", 4) == 0) {
192 name += 4;
193 len -= 4;
194 *scope = SCOPE_RES;
195 }
196 else {
197 memprintf(err, "invalid variable name '%s'. A variable name must be start by its scope. "
Christopher Fauletff2613e2016-11-09 11:36:17 +0100198 "The scope can be 'proc', 'sess', 'txn', 'req' or 'res'", name);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200199 return NULL;
200 }
201
202 /* Look for existing variable name. */
203 for (i = 0; i < var_names_nb; i++)
204 if (strncmp(var_names[i], name, len) == 0)
205 return var_names[i];
206
Christopher Faulet09c9df22016-10-31 11:05:37 +0100207 if (!alloc)
208 return NULL;
209
Hubert Verstraete831962e2016-06-28 22:44:26 +0200210 /* Store variable name. If realloc fails, var_names remains valid */
211 var_names2 = realloc(var_names, (var_names_nb + 1) * sizeof(*var_names));
212 if (!var_names2) {
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200213 memprintf(err, "out of memory error");
214 return NULL;
215 }
Hubert Verstraete831962e2016-06-28 22:44:26 +0200216 var_names_nb++;
217 var_names = var_names2;
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200218 var_names[var_names_nb - 1] = malloc(len + 1);
219 if (!var_names[var_names_nb - 1]) {
220 memprintf(err, "out of memory error");
221 return NULL;
222 }
223 memcpy(var_names[var_names_nb - 1], name, len);
224 var_names[var_names_nb - 1][len] = '\0';
225
226 /* Check variable name syntax. */
227 tmp = var_names[var_names_nb - 1];
228 while (*tmp) {
Christopher Fauletb71557a2016-10-31 10:49:03 +0100229 if (!isalnum((int)(unsigned char)*tmp) && *tmp != '_' && *tmp != '.') {
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200230 memprintf(err, "invalid syntax at char '%s'", tmp);
231 return NULL;
232 }
233 tmp++;
234 }
235
236 /* Return the result. */
237 return var_names[var_names_nb - 1];
238}
239
240/* This function returns an existing variable or returns NULL. */
241static inline struct var *var_get(struct vars *vars, const char *name)
242{
243 struct var *var;
244
245 list_for_each_entry(var, &vars->head, l)
246 if (var->name == name)
247 return var;
248 return NULL;
249}
250
251/* Returns 0 if fails, else returns 1. */
252static int smp_fetch_var(const struct arg *args, struct sample *smp, const char *kw, void *private)
253{
254 const struct var_desc *var_desc = &args[0].data.var;
255 struct var *var;
256 struct vars *vars;
257
258 /* Check the availibity of the variable. */
259 switch (var_desc->scope) {
Christopher Fauletff2613e2016-11-09 11:36:17 +0100260 case SCOPE_PROC:
261 vars = &global.vars;
262 break;
Willy Tarreau7513d002016-10-21 17:14:35 +0200263 case SCOPE_SESS:
264 vars = &smp->sess->vars;
265 break;
266 case SCOPE_TXN:
267 if (!smp->strm)
268 return 0;
269 vars = &smp->strm->vars_txn;
270 break;
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200271 case SCOPE_REQ:
Thierry FOURNIER0b243fd2015-06-16 23:52:47 +0200272 case SCOPE_RES:
Willy Tarreau7513d002016-10-21 17:14:35 +0200273 default:
274 if (!smp->strm)
275 return 0;
276 vars = &smp->strm->vars_reqres;
277 break;
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200278 }
279 if (vars->scope != var_desc->scope)
280 return 0;
281 var = var_get(vars, var_desc->name);
282
283 /* check for the variable avalaibility */
284 if (!var)
285 return 0;
286
287 /* Copy sample. */
Thierry FOURNIER5cc18d42015-08-19 09:02:36 +0200288 smp->data = var->data;
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200289 smp->flags |= SMP_F_CONST;
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200290 return 1;
291}
292
293/* This function search in the <head> a variable with the same
294 * pointer value that the <name>. If the variable doesn't exists,
295 * create it. The function stores a copy of smp> if the variable.
296 * It returns 0 if fails, else returns 1.
297 */
Willy Tarreau6204cd92016-03-10 16:33:04 +0100298static int sample_store(struct vars *vars, const char *name, struct sample *smp)
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200299{
300 struct var *var;
301
302 /* Look for existing variable name. */
303 var = var_get(vars, name);
304
305 if (var) {
306 /* free its used memory. */
307 if (var->data.type == SMP_T_STR ||
308 var->data.type == SMP_T_BIN) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200309 free(var->data.u.str.str);
Willy Tarreau6204cd92016-03-10 16:33:04 +0100310 var_accounting_diff(vars, smp->sess, smp->strm, -var->data.u.str.len);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200311 }
312 else if (var->data.type == SMP_T_METH) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200313 free(var->data.u.meth.str.str);
Willy Tarreau6204cd92016-03-10 16:33:04 +0100314 var_accounting_diff(vars, smp->sess, smp->strm, -var->data.u.meth.str.len);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200315 }
316 } else {
317
318 /* Check memory avalaible. */
Willy Tarreau6204cd92016-03-10 16:33:04 +0100319 if (!var_accounting_add(vars, smp->sess, smp->strm, sizeof(struct var)))
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200320 return 0;
321
322 /* Create new entry. */
323 var = pool_alloc2(var_pool);
324 if (!var)
325 return 0;
326 LIST_ADDQ(&vars->head, &var->l);
327 var->name = name;
328 }
329
330 /* Set type. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200331 var->data.type = smp->data.type;
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200332
333 /* Copy data. If the data needs memory, the function can fail. */
334 switch (var->data.type) {
335 case SMP_T_BOOL:
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200336 case SMP_T_SINT:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200337 var->data.u.sint = smp->data.u.sint;
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200338 break;
339 case SMP_T_IPV4:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200340 var->data.u.ipv4 = smp->data.u.ipv4;
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200341 break;
342 case SMP_T_IPV6:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200343 var->data.u.ipv6 = smp->data.u.ipv6;
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200344 break;
345 case SMP_T_STR:
346 case SMP_T_BIN:
Willy Tarreau6204cd92016-03-10 16:33:04 +0100347 if (!var_accounting_add(vars, smp->sess, smp->strm, smp->data.u.str.len)) {
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200348 var->data.type = SMP_T_BOOL; /* This type doesn't use additional memory. */
349 return 0;
350 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200351 var->data.u.str.str = malloc(smp->data.u.str.len);
352 if (!var->data.u.str.str) {
Willy Tarreau6204cd92016-03-10 16:33:04 +0100353 var_accounting_diff(vars, smp->sess, smp->strm, -smp->data.u.str.len);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200354 var->data.type = SMP_T_BOOL; /* This type doesn't use additional memory. */
355 return 0;
356 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200357 var->data.u.str.len = smp->data.u.str.len;
358 memcpy(var->data.u.str.str, smp->data.u.str.str, var->data.u.str.len);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200359 break;
360 case SMP_T_METH:
Willy Tarreau6204cd92016-03-10 16:33:04 +0100361 if (!var_accounting_add(vars, smp->sess, smp->strm, smp->data.u.meth.str.len)) {
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200362 var->data.type = SMP_T_BOOL; /* This type doesn't use additional memory. */
363 return 0;
364 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200365 var->data.u.meth.str.str = malloc(smp->data.u.meth.str.len);
366 if (!var->data.u.meth.str.str) {
Willy Tarreau6204cd92016-03-10 16:33:04 +0100367 var_accounting_diff(vars, smp->sess, smp->strm, -smp->data.u.meth.str.len);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200368 var->data.type = SMP_T_BOOL; /* This type doesn't use additional memory. */
369 return 0;
370 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200371 var->data.u.meth.meth = smp->data.u.meth.meth;
372 var->data.u.meth.str.len = smp->data.u.meth.str.len;
373 var->data.u.meth.str.size = smp->data.u.meth.str.len;
374 memcpy(var->data.u.meth.str.str, smp->data.u.meth.str.str, var->data.u.meth.str.len);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200375 break;
376 }
377 return 1;
378}
379
Willy Tarreau620408f2016-10-21 16:37:51 +0200380/* Returns 0 if fails, else returns 1. Note that stream may be null for SCOPE_SESS. */
Willy Tarreau6204cd92016-03-10 16:33:04 +0100381static inline int sample_store_stream(const char *name, enum vars_scope scope, struct sample *smp)
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200382{
383 struct vars *vars;
384
385 switch (scope) {
Christopher Fauletff2613e2016-11-09 11:36:17 +0100386 case SCOPE_PROC: vars = &global.vars; break;
Willy Tarreau6204cd92016-03-10 16:33:04 +0100387 case SCOPE_SESS: vars = &smp->sess->vars; break;
388 case SCOPE_TXN: vars = &smp->strm->vars_txn; break;
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200389 case SCOPE_REQ:
Thierry FOURNIER0b243fd2015-06-16 23:52:47 +0200390 case SCOPE_RES:
Willy Tarreau6204cd92016-03-10 16:33:04 +0100391 default: vars = &smp->strm->vars_reqres; break;
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200392 }
393 if (vars->scope != scope)
394 return 0;
Willy Tarreau6204cd92016-03-10 16:33:04 +0100395 return sample_store(vars, name, smp);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200396}
397
Christopher Faulet85d79c92016-11-09 16:54:56 +0100398/* Returns 0 if fails, else returns 1. Note that stream may be null for SCOPE_SESS. */
399static inline int sample_clear_stream(const char *name, enum vars_scope scope, struct sample *smp)
400{
401 struct vars *vars;
402 struct var *var;
403 unsigned int size = 0;
404
405 switch (scope) {
406 case SCOPE_PROC: vars = &global.vars; break;
407 case SCOPE_SESS: vars = &smp->sess->vars; break;
408 case SCOPE_TXN: vars = &smp->strm->vars_txn; break;
409 case SCOPE_REQ:
410 case SCOPE_RES:
411 default: vars = &smp->strm->vars_reqres; break;
412 }
413 if (vars->scope != scope)
414 return 0;
415
416 /* Look for existing variable name. */
417 var = var_get(vars, name);
418 if (var) {
419 size = var_clear(var);
420 var_accounting_diff(vars, smp->sess, smp->strm, -size);
421 }
422 return 1;
423}
424
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200425/* Returns 0 if fails, else returns 1. */
426static int smp_conv_store(const struct arg *args, struct sample *smp, void *private)
427{
Christopher Faulet0099a8c2016-11-09 16:15:32 +0100428 return sample_store_stream(args[0].data.var.name, args[0].data.var.scope, smp);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200429}
430
Christopher Faulet85d79c92016-11-09 16:54:56 +0100431/* Returns 0 if fails, else returns 1. */
432static int smp_conv_clear(const struct arg *args, struct sample *smp, void *private)
433{
434 return sample_clear_stream(args[0].data.var.name, args[0].data.var.scope, smp);
435}
436
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200437/* This fucntions check an argument entry and fill it with a variable
438 * type. The argumen must be a string. If the variable lookup fails,
439 * the function retuns 0 and fill <err>, otherwise it returns 1.
440 */
441int vars_check_arg(struct arg *arg, char **err)
442{
443 char *name;
444 enum vars_scope scope;
445
446 /* Check arg type. */
447 if (arg->type != ARGT_STR) {
448 memprintf(err, "unexpected argument type");
449 return 0;
450 }
451
452 /* Register new variable name. */
Christopher Faulet09c9df22016-10-31 11:05:37 +0100453 name = register_name(arg->data.str.str, arg->data.str.len, &scope, 1, err);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200454 if (!name)
455 return 0;
456
457 /* Use the global variable name pointer. */
458 arg->type = ARGT_VAR;
459 arg->data.var.name = name;
460 arg->data.var.scope = scope;
461 return 1;
462}
463
Christopher Faulet09c9df22016-10-31 11:05:37 +0100464/* This function store a sample in a variable if it was already defined.
465 * In error case, it fails silently.
466 */
467void vars_set_by_name_ifexist(const char *name, size_t len, struct sample *smp)
468{
469 enum vars_scope scope;
470
471 /* Resolve name and scope. */
472 name = register_name(name, len, &scope, 0, NULL);
473 if (!name)
474 return;
475
476 sample_store_stream(name, scope, smp);
477}
478
479
Thierry FOURNIERc365d992015-06-09 12:27:17 +0200480/* This function store a sample in a variable.
481 * In error case, it fails silently.
482 */
Willy Tarreau6204cd92016-03-10 16:33:04 +0100483void vars_set_by_name(const char *name, size_t len, struct sample *smp)
Thierry FOURNIERc365d992015-06-09 12:27:17 +0200484{
485 enum vars_scope scope;
486
487 /* Resolve name and scope. */
Christopher Faulet09c9df22016-10-31 11:05:37 +0100488 name = register_name(name, len, &scope, 1, NULL);
Thierry FOURNIERc365d992015-06-09 12:27:17 +0200489 if (!name)
490 return;
491
Willy Tarreau6204cd92016-03-10 16:33:04 +0100492 sample_store_stream(name, scope, smp);
Thierry FOURNIERc365d992015-06-09 12:27:17 +0200493}
494
Christopher Faulet85d79c92016-11-09 16:54:56 +0100495/* This function unset a variable if it was already defined.
496 * In error case, it fails silently.
497 */
498void vars_unset_by_name_ifexist(const char *name, size_t len, struct sample *smp)
499{
500 enum vars_scope scope;
501
502 /* Resolve name and scope. */
503 name = register_name(name, len, &scope, 0, NULL);
504 if (!name)
505 return;
506
507 sample_clear_stream(name, scope, smp);
508}
509
510
511/* This function unset a variable.
512 * In error case, it fails silently.
513 */
514void vars_unset_by_name(const char *name, size_t len, struct sample *smp)
515{
516 enum vars_scope scope;
517
518 /* Resolve name and scope. */
519 name = register_name(name, len, &scope, 1, NULL);
520 if (!name)
521 return;
522
523 sample_clear_stream(name, scope, smp);
524}
525
Thierry FOURNIERc365d992015-06-09 12:27:17 +0200526/* this function fills a sample with the
527 * variable content. Returns 1 if the sample
528 * is filled, otherwise it returns 0.
529 */
Willy Tarreau6204cd92016-03-10 16:33:04 +0100530int vars_get_by_name(const char *name, size_t len, struct sample *smp)
Thierry FOURNIERc365d992015-06-09 12:27:17 +0200531{
532 struct vars *vars;
533 struct var *var;
534 enum vars_scope scope;
535
536 /* Resolve name and scope. */
Christopher Faulet09c9df22016-10-31 11:05:37 +0100537 name = register_name(name, len, &scope, 1, NULL);
Thierry FOURNIERc365d992015-06-09 12:27:17 +0200538 if (!name)
539 return 0;
540
541 /* Select "vars" pool according with the scope. */
542 switch (scope) {
Christopher Fauletff2613e2016-11-09 11:36:17 +0100543 case SCOPE_PROC: vars = &global.vars; break;
Willy Tarreau6204cd92016-03-10 16:33:04 +0100544 case SCOPE_SESS: vars = &smp->sess->vars; break;
545 case SCOPE_TXN: vars = &smp->strm->vars_txn; break;
Thierry FOURNIERc365d992015-06-09 12:27:17 +0200546 case SCOPE_REQ:
Thierry FOURNIER0b243fd2015-06-16 23:52:47 +0200547 case SCOPE_RES:
Willy Tarreau6204cd92016-03-10 16:33:04 +0100548 default: vars = &smp->strm->vars_reqres; break;
Thierry FOURNIERc365d992015-06-09 12:27:17 +0200549 }
550
551 /* Check if the scope is avalaible a this point of processing. */
552 if (vars->scope != scope)
553 return 0;
554
555 /* Get the variable entry. */
556 var = var_get(vars, name);
557 if (!var)
558 return 0;
559
560 /* Copy sample. */
Thierry FOURNIER5cc18d42015-08-19 09:02:36 +0200561 smp->data = var->data;
Thierry FOURNIERc365d992015-06-09 12:27:17 +0200562 smp->flags = SMP_F_CONST;
Thierry FOURNIERc365d992015-06-09 12:27:17 +0200563 return 1;
564}
565
Thierry FOURNIERfd77e052015-07-07 21:20:42 +0200566/* this function fills a sample with the
567 * content of the varaible described by <var_desc>. Returns 1
568 * if the sample is filled, otherwise it returns 0.
569 */
Willy Tarreau6204cd92016-03-10 16:33:04 +0100570int vars_get_by_desc(const struct var_desc *var_desc, struct sample *smp)
Thierry FOURNIERfd77e052015-07-07 21:20:42 +0200571{
572 struct vars *vars;
573 struct var *var;
574
575 /* Select "vars" pool according with the scope. */
576 switch (var_desc->scope) {
Christopher Fauletff2613e2016-11-09 11:36:17 +0100577 case SCOPE_PROC: vars = &global.vars; break;
Willy Tarreau6204cd92016-03-10 16:33:04 +0100578 case SCOPE_SESS: vars = &smp->sess->vars; break;
579 case SCOPE_TXN: vars = &smp->strm->vars_txn; break;
Thierry FOURNIERfd77e052015-07-07 21:20:42 +0200580 case SCOPE_REQ:
581 case SCOPE_RES:
Willy Tarreau6204cd92016-03-10 16:33:04 +0100582 default: vars = &smp->strm->vars_reqres; break;
Thierry FOURNIERfd77e052015-07-07 21:20:42 +0200583 }
584
585 /* Check if the scope is avalaible a this point of processing. */
586 if (vars->scope != var_desc->scope)
587 return 0;
588
589 /* Get the variable entry. */
590 var = var_get(vars, var_desc->name);
591 if (!var)
592 return 0;
593
594 /* Copy sample. */
Thierry FOURNIER5cc18d42015-08-19 09:02:36 +0200595 smp->data = var->data;
Thierry FOURNIERfd77e052015-07-07 21:20:42 +0200596 smp->flags = SMP_F_CONST;
Thierry FOURNIERfd77e052015-07-07 21:20:42 +0200597 return 1;
598}
599
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +0200600/* Always returns ACT_RET_CONT even if an error occurs. */
601static enum act_return action_store(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +0200602 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200603{
604 struct sample smp;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +0200605 int dir;
606
607 switch (rule->from) {
Willy Tarreau620408f2016-10-21 16:37:51 +0200608 case ACT_F_TCP_REQ_SES: dir = SMP_OPT_DIR_REQ; break;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +0200609 case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break;
610 case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break;
611 case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break;
612 case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break;
613 default:
614 send_log(px, LOG_ERR, "Vars: internal error while execute action store.");
615 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))
616 Alert("Vars: internal error while execute action store.\n");
617 return ACT_RET_CONT;
618 }
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200619
620 /* Process the expression. */
621 memset(&smp, 0, sizeof(smp));
Willy Tarreau108a8fd2016-10-21 17:13:24 +0200622 if (!sample_process(px, sess, s, dir|SMP_OPT_FINAL,
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +0200623 rule->arg.vars.expr, &smp))
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +0200624 return ACT_RET_CONT;
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200625
626 /* Store the sample, and ignore errors. */
Willy Tarreau6204cd92016-03-10 16:33:04 +0100627 sample_store_stream(rule->arg.vars.name, rule->arg.vars.scope, &smp);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +0200628 return ACT_RET_CONT;
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200629}
630
Christopher Faulet85d79c92016-11-09 16:54:56 +0100631/* Always returns ACT_RET_CONT even if an error occurs. */
632static enum act_return action_clear(struct act_rule *rule, struct proxy *px,
633 struct session *sess, struct stream *s, int flags)
634{
635 struct sample smp;
636
637 memset(&smp, 0, sizeof(smp));
638 smp_set_owner(&smp, px, sess, s, SMP_OPT_FINAL);
639
640 /* Clear the variable using the sample context, and ignore errors. */
641 sample_clear_stream(rule->arg.vars.name, rule->arg.vars.scope, &smp);
642 return ACT_RET_CONT;
643}
644
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200645/* This two function checks the variable name and replace the
646 * configuration string name by the global string name. its
647 * the same string, but the global pointer can be easy to
648 * compare.
649 *
650 * The first function checks a sample-fetch and the second
651 * checks a converter.
652 */
653static int smp_check_var(struct arg *args, char **err)
654{
655 return vars_check_arg(&args[0], err);
656}
657
658static int conv_check_var(struct arg *args, struct sample_conv *conv,
659 const char *file, int line, char **err_msg)
660{
661 return vars_check_arg(&args[0], err_msg);
662}
663
664/* This function is a common parser for using variables. It understands
665 * the format:
666 *
667 * set-var(<variable-name>) <expression>
668 *
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +0200669 * It returns ACT_RET_PRS_ERR if fails and <err> is filled with an error
670 * message. Otherwise, it returns ACT_RET_PRS_OK and the variable <expr>
671 * is filled with the pointer to the expression to execute.
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200672 */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +0200673static enum act_parse_ret parse_store(const char **args, int *arg, struct proxy *px,
674 struct act_rule *rule, char **err)
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200675{
676 const char *var_name = args[*arg-1];
677 int var_len;
Thierry FOURNIER48a9cd12015-07-28 19:00:28 +0200678 const char *kw_name;
Christopher Faulet85d79c92016-11-09 16:54:56 +0100679 int flags, set_var;
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200680
Christopher Faulet85d79c92016-11-09 16:54:56 +0100681 if (!strncmp(var_name, "set-var", 7)) {
682 var_name += 7;
683 set_var = 1;
684 }
685 if (!strncmp(var_name, "unset-var", 9)) {
686 var_name += 9;
687 set_var = 0;
688 }
689
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200690 if (*var_name != '(') {
Christopher Faulet85d79c92016-11-09 16:54:56 +0100691 memprintf(err, "invalid variable '%s'. Expects 'set-var(<var-name>)' or 'unset-var(<var-name>)'",
692 args[*arg-1]);
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +0200693 return ACT_RET_PRS_ERR;
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200694 }
695 var_name++; /* jump the '(' */
696 var_len = strlen(var_name);
697 var_len--; /* remove the ')' */
698 if (var_name[var_len] != ')') {
Christopher Faulet85d79c92016-11-09 16:54:56 +0100699 memprintf(err, "invalid variable '%s'. Expects 'set-var(<var-name>)' or 'unset-var(<var-name>)'",
700 args[*arg-1]);
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +0200701 return ACT_RET_PRS_ERR;
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200702 }
703
Christopher Faulet09c9df22016-10-31 11:05:37 +0100704 rule->arg.vars.name = register_name(var_name, var_len, &rule->arg.vars.scope, 1, err);
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +0200705 if (!rule->arg.vars.name)
706 return ACT_RET_PRS_ERR;
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200707
Christopher Faulet85d79c92016-11-09 16:54:56 +0100708 /* There is no fetch method when variable is unset. Just set the right
709 * action and return. */
710 if (!set_var) {
711 if (*args[*arg]) {
712 memprintf(err, "fetch method not supported");
713 return ACT_RET_PRS_ERR;
714 }
715 rule->action = ACT_CUSTOM;
716 rule->action_ptr = action_clear;
717 return ACT_RET_PRS_OK;
718 }
719
Thierry FOURNIER48a9cd12015-07-28 19:00:28 +0200720 kw_name = args[*arg-1];
721
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +0200722 rule->arg.vars.expr = sample_parse_expr((char **)args, arg, px->conf.args.file,
723 px->conf.args.line, err, &px->conf.args);
724 if (!rule->arg.vars.expr)
Thierry FOURNIERafa80492015-08-19 09:04:15 +0200725 return ACT_RET_PRS_ERR;
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200726
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +0200727 switch (rule->from) {
Willy Tarreau620408f2016-10-21 16:37:51 +0200728 case ACT_F_TCP_REQ_SES: flags = SMP_VAL_FE_SES_ACC; break;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +0200729 case ACT_F_TCP_REQ_CNT: flags = SMP_VAL_FE_REQ_CNT; break;
730 case ACT_F_TCP_RES_CNT: flags = SMP_VAL_BE_RES_CNT; break;
731 case ACT_F_HTTP_REQ: flags = SMP_VAL_FE_HRQ_HDR; break;
732 case ACT_F_HTTP_RES: flags = SMP_VAL_BE_HRS_HDR; break;
733 default:
734 memprintf(err,
735 "internal error, unexpected rule->from=%d, please report this bug!",
736 rule->from);
Thierry FOURNIERafa80492015-08-19 09:04:15 +0200737 return ACT_RET_PRS_ERR;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +0200738 }
739 if (!(rule->arg.vars.expr->fetch->val & flags)) {
740 memprintf(err,
741 "fetch method '%s' extracts information from '%s', none of which is available here",
742 kw_name, sample_src_names(rule->arg.vars.expr->fetch->use));
743 free(rule->arg.vars.expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +0200744 return ACT_RET_PRS_ERR;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +0200745 }
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200746
Thierry FOURNIER42148732015-09-02 17:17:33 +0200747 rule->action = ACT_CUSTOM;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +0200748 rule->action_ptr = action_store;
Thierry FOURNIERafa80492015-08-19 09:04:15 +0200749 return ACT_RET_PRS_OK;
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200750}
751
752static int vars_max_size(char **args, int section_type, struct proxy *curpx,
753 struct proxy *defpx, const char *file, int line,
754 char **err, unsigned int *limit)
755{
756 char *error;
757
758 *limit = strtol(args[1], &error, 10);
759 if (*error != 0) {
760 memprintf(err, "%s: '%s' is an invalid size", args[0], args[1]);
761 return -1;
762 }
763 return 0;
764}
765
766static int vars_max_size_global(char **args, int section_type, struct proxy *curpx,
767 struct proxy *defpx, const char *file, int line,
768 char **err)
769{
770 return vars_max_size(args, section_type, curpx, defpx, file, line, err, &var_global_limit);
771}
772
Christopher Fauletff2613e2016-11-09 11:36:17 +0100773static int vars_max_size_proc(char **args, int section_type, struct proxy *curpx,
774 struct proxy *defpx, const char *file, int line,
775 char **err)
776{
777 return vars_max_size(args, section_type, curpx, defpx, file, line, err, &var_proc_limit);
778}
779
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200780static int vars_max_size_sess(char **args, int section_type, struct proxy *curpx,
781 struct proxy *defpx, const char *file, int line,
782 char **err)
783{
784 return vars_max_size(args, section_type, curpx, defpx, file, line, err, &var_sess_limit);
785}
786
787static int vars_max_size_txn(char **args, int section_type, struct proxy *curpx,
788 struct proxy *defpx, const char *file, int line,
789 char **err)
790{
791 return vars_max_size(args, section_type, curpx, defpx, file, line, err, &var_txn_limit);
792}
793
794static int vars_max_size_reqres(char **args, int section_type, struct proxy *curpx,
795 struct proxy *defpx, const char *file, int line,
796 char **err)
797{
798 return vars_max_size(args, section_type, curpx, defpx, file, line, err, &var_reqres_limit);
799}
800
801static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
802
Christopher Fauletff2613e2016-11-09 11:36:17 +0100803 { "var", smp_fetch_var, ARG1(1,STR), smp_check_var, SMP_T_STR, SMP_USE_L4CLI },
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200804 { /* END */ },
805}};
806
807static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Christopher Faulet85d79c92016-11-09 16:54:56 +0100808 { "set-var", smp_conv_store, ARG1(1,STR), conv_check_var, SMP_T_ANY, SMP_T_ANY },
809 { "unset-var", smp_conv_clear, ARG1(1,STR), conv_check_var, SMP_T_ANY, SMP_T_ANY },
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200810 { /* END */ },
811}};
812
Willy Tarreau620408f2016-10-21 16:37:51 +0200813static struct action_kw_list tcp_req_sess_kws = { { }, {
Christopher Faulet85d79c92016-11-09 16:54:56 +0100814 { "set-var", parse_store, 1 },
815 { "unset-var", parse_store, 1 },
Willy Tarreau620408f2016-10-21 16:37:51 +0200816 { /* END */ }
817}};
818
819static struct action_kw_list tcp_req_cont_kws = { { }, {
Christopher Faulet85d79c92016-11-09 16:54:56 +0100820 { "set-var", parse_store, 1 },
821 { "unset-var", parse_store, 1 },
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200822 { /* END */ }
823}};
824
Thierry FOURNIER36481b82015-08-19 09:01:53 +0200825static struct action_kw_list tcp_res_kws = { { }, {
Christopher Faulet85d79c92016-11-09 16:54:56 +0100826 { "set-var", parse_store, 1 },
827 { "unset-var", parse_store, 1 },
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200828 { /* END */ }
829}};
830
Thierry FOURNIER36481b82015-08-19 09:01:53 +0200831static struct action_kw_list http_req_kws = { { }, {
Christopher Faulet85d79c92016-11-09 16:54:56 +0100832 { "set-var", parse_store, 1 },
833 { "unset-var", parse_store, 1 },
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200834 { /* END */ }
835}};
836
Thierry FOURNIER36481b82015-08-19 09:01:53 +0200837static struct action_kw_list http_res_kws = { { }, {
Christopher Faulet85d79c92016-11-09 16:54:56 +0100838 { "set-var", parse_store, 1 },
839 { "unset-var", parse_store, 1 },
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200840 { /* END */ }
841}};
842
843static struct cfg_kw_list cfg_kws = {{ },{
844 { CFG_GLOBAL, "tune.vars.global-max-size", vars_max_size_global },
Christopher Fauletff2613e2016-11-09 11:36:17 +0100845 { CFG_GLOBAL, "tune.vars.proc-max-size", vars_max_size_proc },
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200846 { CFG_GLOBAL, "tune.vars.sess-max-size", vars_max_size_sess },
847 { CFG_GLOBAL, "tune.vars.txn-max-size", vars_max_size_txn },
848 { CFG_GLOBAL, "tune.vars.reqres-max-size", vars_max_size_reqres },
849 { /* END */ }
850}};
851
852__attribute__((constructor))
853static void __http_protocol_init(void)
854{
855 var_pool = create_pool("vars", sizeof(struct var), MEM_F_SHARED);
856
857 sample_register_fetches(&sample_fetch_keywords);
858 sample_register_convs(&sample_conv_kws);
Willy Tarreau620408f2016-10-21 16:37:51 +0200859 tcp_req_sess_keywords_register(&tcp_req_sess_kws);
860 tcp_req_cont_keywords_register(&tcp_req_cont_kws);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200861 tcp_res_cont_keywords_register(&tcp_res_kws);
862 http_req_keywords_register(&http_req_kws);
863 http_res_keywords_register(&http_res_kws);
864 cfg_register_keywords(&cfg_kws);
865}