blob: 1ab81197e1a45e0b214773ec699571edad62aa48 [file] [log] [blame]
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02001#include <ctype.h>
2
Willy Tarreau4c7e4b72020-05-27 12:58:42 +02003#include <haproxy/api.h>
Willy Tarreaub2551052020-06-09 09:07:15 +02004#include <haproxy/arg.h>
Willy Tarreauc35eb382021-03-26 14:51:31 +01005#include <haproxy/buf.h>
Willy Tarreau6be78492020-06-05 00:00:29 +02006#include <haproxy/cfgparse.h>
Willy Tarreau4aa573d2020-06-04 18:21:56 +02007#include <haproxy/check.h>
Willy Tarreauc35eb382021-03-26 14:51:31 +01008#include <haproxy/cli.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +02009#include <haproxy/global.h>
Willy Tarreaucd72d8c2020-06-02 19:11:26 +020010#include <haproxy/http.h>
Willy Tarreauc761f842020-06-04 11:40:28 +020011#include <haproxy/http_rules.h>
Willy Tarreau853b2972020-05-27 18:01:47 +020012#include <haproxy/list.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020013#include <haproxy/log.h>
Willy Tarreaue6ce10b2020-06-04 15:33:47 +020014#include <haproxy/sample.h>
Willy Tarreau753d4db2021-09-03 09:02:47 +020015#include <haproxy/session.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020016#include <haproxy/stream-t.h>
Willy Tarreau8b550af2020-06-04 17:42:48 +020017#include <haproxy/tcp_rules.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020018#include <haproxy/tcpcheck.h>
Willy Tarreau67046bf2021-05-08 13:56:31 +020019#include <haproxy/tools.h>
Willy Tarreaua1718922020-06-04 16:25:31 +020020#include <haproxy/vars.h>
Willy Tarreaub555eb12021-10-06 17:11:51 +020021#include <haproxy/xxhash.h>
Thierry FOURNIER4834bc72015-06-06 19:29:07 +020022
Thierry FOURNIER4834bc72015-06-06 19:29:07 +020023
24/* This contains a pool of struct vars */
Willy Tarreau8ceae722018-11-26 11:58:30 +010025DECLARE_STATIC_POOL(var_pool, "vars", sizeof(struct var));
Thierry FOURNIER4834bc72015-06-06 19:29:07 +020026
Willy Tarreaucfc4f242021-05-08 11:41:28 +020027/* list of variables for the process scope. */
28struct vars proc_vars THREAD_ALIGNED(64);
29
Thierry FOURNIER4834bc72015-06-06 19:29:07 +020030/* This array of int contains the system limits per context. */
31static unsigned int var_global_limit = 0;
Christopher Fauletff2613e2016-11-09 11:36:17 +010032static unsigned int var_proc_limit = 0;
Thierry FOURNIER4834bc72015-06-06 19:29:07 +020033static unsigned int var_sess_limit = 0;
34static unsigned int var_txn_limit = 0;
35static unsigned int var_reqres_limit = 0;
Gaetan Rivet13a50432020-02-21 18:13:44 +010036static unsigned int var_check_limit = 0;
Willy Tarreau2c897d92021-08-31 08:48:55 +020037static uint64_t var_name_hash_seed = 0;
Thierry FOURNIER4834bc72015-06-06 19:29:07 +020038
Remi Tricot-Le Breton51899d22021-12-16 17:14:37 +010039/* Structure and array matching set-var conditions to their respective flag
40 * value.
41 */
42struct var_set_condition {
43 const char *cond_str;
44 uint flag;
45};
46
47static struct var_set_condition conditions_array[] = {
48 { "ifexists", VF_COND_IFEXISTS },
49 { "ifnotexists", VF_COND_IFNOTEXISTS },
50 { "ifempty", VF_COND_IFEMPTY },
51 { "ifnotempty", VF_COND_IFNOTEMPTY },
52 { "ifset", VF_COND_IFSET },
53 { "ifnotset", VF_COND_IFNOTSET },
54 { "ifgt", VF_COND_IFGT },
55 { "iflt", VF_COND_IFLT },
56 { NULL, 0 }
57};
58
Willy Tarreauf37b1402019-06-04 16:27:36 +020059/* returns the struct vars pointer for a session, stream and scope, or NULL if
60 * it does not exist.
61 */
62static inline struct vars *get_vars(struct session *sess, struct stream *strm, enum vars_scope scope)
63{
64 switch (scope) {
65 case SCOPE_PROC:
Willy Tarreaucfc4f242021-05-08 11:41:28 +020066 return &proc_vars;
Willy Tarreauf37b1402019-06-04 16:27:36 +020067 case SCOPE_SESS:
Willy Tarreaua07d61b2021-03-26 11:27:59 +010068 return sess ? &sess->vars : NULL;
Gaetan Rivet13a50432020-02-21 18:13:44 +010069 case SCOPE_CHECK: {
Christopher Fauletc4439f72021-06-02 11:48:42 +020070 struct check *check = sess ? objt_check(sess->origin) : NULL;
Gaetan Rivet13a50432020-02-21 18:13:44 +010071
Christopher Faulet0fca7ed2020-04-21 11:53:32 +020072 return check ? &check->vars : NULL;
Gaetan Rivet13a50432020-02-21 18:13:44 +010073 }
Willy Tarreauf37b1402019-06-04 16:27:36 +020074 case SCOPE_TXN:
75 return strm ? &strm->vars_txn : NULL;
76 case SCOPE_REQ:
77 case SCOPE_RES:
78 default:
79 return strm ? &strm->vars_reqres : NULL;
80 }
81}
82
Willy Tarreau72330982015-06-19 11:21:56 +020083/* This function adds or remove memory size from the accounting. The inner
84 * pointers may be null when setting the outer ones only.
85 */
Miroslav Zagorac6deab792020-12-09 16:34:29 +010086void var_accounting_diff(struct vars *vars, struct session *sess, struct stream *strm, int size)
Thierry FOURNIER4834bc72015-06-06 19:29:07 +020087{
88 switch (vars->scope) {
89 case SCOPE_REQ:
90 case SCOPE_RES:
Willy Tarreau55f8a832021-09-08 15:51:06 +020091 if (var_reqres_limit && strm)
Willy Tarreauf37b1402019-06-04 16:27:36 +020092 _HA_ATOMIC_ADD(&strm->vars_reqres.size, size);
Willy Tarreau6204cd92016-03-10 16:33:04 +010093 /* fall through */
Thierry FOURNIER4834bc72015-06-06 19:29:07 +020094 case SCOPE_TXN:
Willy Tarreau55f8a832021-09-08 15:51:06 +020095 if (var_txn_limit && strm)
Willy Tarreauf37b1402019-06-04 16:27:36 +020096 _HA_ATOMIC_ADD(&strm->vars_txn.size, size);
Gaetan Rivet13a50432020-02-21 18:13:44 +010097 goto scope_sess;
Willy Tarreau55f8a832021-09-08 15:51:06 +020098 case SCOPE_CHECK:
99 if (var_check_limit) {
Christopher Faulet0fca7ed2020-04-21 11:53:32 +0200100 struct check *check = objt_check(sess->origin);
Gaetan Rivet13a50432020-02-21 18:13:44 +0100101
Christopher Faulet0fca7ed2020-04-21 11:53:32 +0200102 if (check)
103 _HA_ATOMIC_ADD(&check->vars.size, size);
Gaetan Rivet13a50432020-02-21 18:13:44 +0100104 }
Willy Tarreau6204cd92016-03-10 16:33:04 +0100105 /* fall through */
Gaetan Rivet13a50432020-02-21 18:13:44 +0100106scope_sess:
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200107 case SCOPE_SESS:
Willy Tarreau55f8a832021-09-08 15:51:06 +0200108 if (var_sess_limit)
109 _HA_ATOMIC_ADD(&sess->vars.size, size);
Christopher Fauletff2613e2016-11-09 11:36:17 +0100110 /* fall through */
111 case SCOPE_PROC:
Willy Tarreau55f8a832021-09-08 15:51:06 +0200112 if (var_proc_limit || var_global_limit)
113 _HA_ATOMIC_ADD(&proc_vars.size, size);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200114 }
115}
116
117/* This function returns 1 if the <size> is available in the var
Joseph Herlant07676892018-11-15 09:19:50 -0800118 * pool <vars>, otherwise returns 0. If the space is available,
Willy Tarreau72330982015-06-19 11:21:56 +0200119 * the size is reserved. The inner pointers may be null when setting
Willy Tarreau6204cd92016-03-10 16:33:04 +0100120 * the outer ones only. The accounting uses either <sess> or <strm>
121 * depending on the scope. <strm> may be NULL when no stream is known
122 * and only the session exists (eg: tcp-request connection).
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200123 */
Willy Tarreau6204cd92016-03-10 16:33:04 +0100124static int var_accounting_add(struct vars *vars, struct session *sess, struct stream *strm, int size)
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200125{
126 switch (vars->scope) {
127 case SCOPE_REQ:
128 case SCOPE_RES:
Willy Tarreauf37b1402019-06-04 16:27:36 +0200129 if (var_reqres_limit && strm && strm->vars_reqres.size + size > var_reqres_limit)
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200130 return 0;
Willy Tarreau6204cd92016-03-10 16:33:04 +0100131 /* fall through */
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200132 case SCOPE_TXN:
Willy Tarreauf37b1402019-06-04 16:27:36 +0200133 if (var_txn_limit && strm && strm->vars_txn.size + size > var_txn_limit)
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200134 return 0;
Gaetan Rivet13a50432020-02-21 18:13:44 +0100135 goto scope_sess;
136 case SCOPE_CHECK: {
Christopher Faulet0fca7ed2020-04-21 11:53:32 +0200137 struct check *check = objt_check(sess->origin);
Gaetan Rivet13a50432020-02-21 18:13:44 +0100138
Christopher Faulet0fca7ed2020-04-21 11:53:32 +0200139 if (var_check_limit && check && check->vars.size + size > var_check_limit)
Gaetan Rivet13a50432020-02-21 18:13:44 +0100140 return 0;
141 }
Willy Tarreau6204cd92016-03-10 16:33:04 +0100142 /* fall through */
Gaetan Rivet13a50432020-02-21 18:13:44 +0100143scope_sess:
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200144 case SCOPE_SESS:
Willy Tarreau6204cd92016-03-10 16:33:04 +0100145 if (var_sess_limit && sess->vars.size + size > var_sess_limit)
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200146 return 0;
Christopher Fauletff2613e2016-11-09 11:36:17 +0100147 /* fall through */
148 case SCOPE_PROC:
Willy Tarreau3b78f2a2021-09-08 15:40:58 +0200149 /* note: scope proc collects all others and is currently identical to the
150 * global limit.
151 */
Willy Tarreaucfc4f242021-05-08 11:41:28 +0200152 if (var_proc_limit && proc_vars.size + size > var_proc_limit)
Christopher Fauletff2613e2016-11-09 11:36:17 +0100153 return 0;
Willy Tarreau3b78f2a2021-09-08 15:40:58 +0200154 if (var_global_limit && proc_vars.size + size > var_global_limit)
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200155 return 0;
156 }
Willy Tarreau6204cd92016-03-10 16:33:04 +0100157 var_accounting_diff(vars, sess, strm, size);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200158 return 1;
159}
160
Willy Tarreauc1c88f42021-09-08 15:03:58 +0200161/* This function removes a variable from the list and frees the memory it was
162 * using. If the variable is marked "VF_PERMANENT", the sample_data is only
163 * reset to SMP_T_ANY unless <force> is non nul. Returns the freed size.
164 */
165unsigned int var_clear(struct var *var, int force)
Christopher Faulet85d79c92016-11-09 16:54:56 +0100166{
167 unsigned int size = 0;
168
169 if (var->data.type == SMP_T_STR || var->data.type == SMP_T_BIN) {
Willy Tarreau5b52b002021-02-26 21:19:53 +0100170 ha_free(&var->data.u.str.area);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200171 size += var->data.u.str.data;
Christopher Faulet85d79c92016-11-09 16:54:56 +0100172 }
Christopher Fauletd02210c2017-07-24 16:24:39 +0200173 else if (var->data.type == SMP_T_METH && var->data.u.meth.meth == HTTP_METH_OTHER) {
Willy Tarreau5b52b002021-02-26 21:19:53 +0100174 ha_free(&var->data.u.meth.str.area);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200175 size += var->data.u.meth.str.data;
Christopher Faulet85d79c92016-11-09 16:54:56 +0100176 }
Willy Tarreauc1c88f42021-09-08 15:03:58 +0200177 /* wipe the sample */
178 var->data.type = SMP_T_ANY;
179
180 if (!(var->flags & VF_PERMANENT) || force) {
181 LIST_DELETE(&var->l);
182 pool_free(var_pool, var);
183 size += sizeof(struct var);
184 }
Christopher Faulet85d79c92016-11-09 16:54:56 +0100185 return size;
186}
187
Joseph Herlant07676892018-11-15 09:19:50 -0800188/* This function free all the memory used by all the variables
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200189 * in the list.
190 */
Willy Tarreau6204cd92016-03-10 16:33:04 +0100191void vars_prune(struct vars *vars, struct session *sess, struct stream *strm)
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200192{
193 struct var *var, *tmp;
Willy Tarreau72330982015-06-19 11:21:56 +0200194 unsigned int size = 0;
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200195
Willy Tarreaudc72fbb2021-09-08 15:19:57 +0200196 vars_wrlock(vars);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200197 list_for_each_entry_safe(var, tmp, &vars->head, l) {
Willy Tarreauc1c88f42021-09-08 15:03:58 +0200198 size += var_clear(var, 1);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200199 }
Willy Tarreaudc72fbb2021-09-08 15:19:57 +0200200 vars_wrunlock(vars);
Willy Tarreau6204cd92016-03-10 16:33:04 +0100201 var_accounting_diff(vars, sess, strm, -size);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200202}
203
Willy Tarreauebcd4842015-06-19 11:59:02 +0200204/* This function frees all the memory used by all the session variables in the
205 * list starting at <vars>.
206 */
207void vars_prune_per_sess(struct vars *vars)
208{
209 struct var *var, *tmp;
210 unsigned int size = 0;
211
Willy Tarreaudc72fbb2021-09-08 15:19:57 +0200212 vars_wrlock(vars);
Willy Tarreauebcd4842015-06-19 11:59:02 +0200213 list_for_each_entry_safe(var, tmp, &vars->head, l) {
Willy Tarreauc1c88f42021-09-08 15:03:58 +0200214 size += var_clear(var, 1);
Willy Tarreauebcd4842015-06-19 11:59:02 +0200215 }
Willy Tarreaudc72fbb2021-09-08 15:19:57 +0200216 vars_wrunlock(vars);
Christopher Faulete95f2c32017-07-24 16:30:34 +0200217
Willy Tarreau55f8a832021-09-08 15:51:06 +0200218 if (var_sess_limit)
219 _HA_ATOMIC_SUB(&vars->size, size);
220 if (var_proc_limit || var_global_limit)
221 _HA_ATOMIC_SUB(&proc_vars.size, size);
Willy Tarreauebcd4842015-06-19 11:59:02 +0200222}
223
Willy Tarreaub7bfcb32021-08-31 08:13:25 +0200224/* This function initializes a variables list head */
225void vars_init_head(struct vars *vars, enum vars_scope scope)
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200226{
227 LIST_INIT(&vars->head);
228 vars->scope = scope;
229 vars->size = 0;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100230 HA_RWLOCK_INIT(&vars->rwlock);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200231}
232
Willy Tarreau3a4bedc2021-08-31 08:51:02 +0200233/* This function returns a hash value and a scope for a variable name of a
234 * specified length. It makes sure that the scope is valid. It returns non-zero
235 * on success, 0 on failure. Neither hash nor scope may be NULL.
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200236 */
Willy Tarreau3a4bedc2021-08-31 08:51:02 +0200237static int vars_hash_name(const char *name, int len, enum vars_scope *scope,
238 uint64_t *hash, char **err)
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200239{
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200240 const char *tmp;
241
242 /* Check length. */
243 if (len == 0) {
244 memprintf(err, "Empty variable name cannot be accepted");
Willy Tarreau3a4bedc2021-08-31 08:51:02 +0200245 return 0;
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200246 }
247
248 /* Check scope. */
Christopher Fauletff2613e2016-11-09 11:36:17 +0100249 if (len > 5 && strncmp(name, "proc.", 5) == 0) {
250 name += 5;
251 len -= 5;
252 *scope = SCOPE_PROC;
253 }
254 else if (len > 5 && strncmp(name, "sess.", 5) == 0) {
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200255 name += 5;
256 len -= 5;
257 *scope = SCOPE_SESS;
258 }
259 else if (len > 4 && strncmp(name, "txn.", 4) == 0) {
260 name += 4;
261 len -= 4;
262 *scope = SCOPE_TXN;
263 }
264 else if (len > 4 && strncmp(name, "req.", 4) == 0) {
265 name += 4;
266 len -= 4;
267 *scope = SCOPE_REQ;
268 }
269 else if (len > 4 && strncmp(name, "res.", 4) == 0) {
270 name += 4;
271 len -= 4;
272 *scope = SCOPE_RES;
273 }
Gaetan Rivet13a50432020-02-21 18:13:44 +0100274 else if (len > 6 && strncmp(name, "check.", 6) == 0) {
275 name += 6;
276 len -= 6;
277 *scope = SCOPE_CHECK;
278 }
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200279 else {
Willy Tarreau1402fef2021-09-03 10:12:55 +0200280 memprintf(err, "invalid variable name '%.*s'. A variable name must be start by its scope. "
281 "The scope can be 'proc', 'sess', 'txn', 'req', 'res' or 'check'", len, name);
Willy Tarreau3a4bedc2021-08-31 08:51:02 +0200282 return 0;
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200283 }
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200284
285 /* Check variable name syntax. */
Willy Tarreau3a4bedc2021-08-31 08:51:02 +0200286 for (tmp = name; tmp < name + len; tmp++) {
Willy Tarreau90807112020-02-25 08:16:33 +0100287 if (!isalnum((unsigned char)*tmp) && *tmp != '_' && *tmp != '.') {
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200288 memprintf(err, "invalid syntax at char '%s'", tmp);
Willy Tarreau3a4bedc2021-08-31 08:51:02 +0200289 return 0;
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200290 }
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200291 }
Christopher Faulete95f2c32017-07-24 16:30:34 +0200292
Willy Tarreau3a4bedc2021-08-31 08:51:02 +0200293 *hash = XXH3(name, len, var_name_hash_seed);
294 return 1;
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200295}
296
Willy Tarreau3a4bedc2021-08-31 08:51:02 +0200297/* This function returns the variable from the given list that matches
298 * <name_hash> or returns NULL if not found. It's only a linked list since it
299 * is not expected to have many variables per scope (a few tens at best).
300 * The caller is responsible for ensuring that <vars> is properly locked.
301 */
302static struct var *var_get(struct vars *vars, uint64_t name_hash)
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200303{
304 struct var *var;
305
306 list_for_each_entry(var, &vars->head, l)
Willy Tarreau3a4bedc2021-08-31 08:51:02 +0200307 if (var->name_hash == name_hash)
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200308 return var;
309 return NULL;
310}
311
312/* Returns 0 if fails, else returns 1. */
313static int smp_fetch_var(const struct arg *args, struct sample *smp, const char *kw, void *private)
314{
315 const struct var_desc *var_desc = &args[0].data.var;
Willy Tarreau54496a62021-09-03 12:00:13 +0200316 const struct buffer *def = NULL;
Christopher Faulete95f2c32017-07-24 16:30:34 +0200317
Willy Tarreau54496a62021-09-03 12:00:13 +0200318 if (args[1].type == ARGT_STR)
319 def = &args[1].data.str;
320
321 return vars_get_by_desc(var_desc, smp, def);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200322}
323
Remi Tricot-Le Breton25fccd52021-12-16 17:14:36 +0100324/*
325 * Clear the contents of a variable so that it can be reset directly.
326 * This function is used just before a variable is filled out of a sample's
327 * content.
328 */
329static inline void var_clear_buffer(struct sample *smp, struct vars *vars, struct var *var, int var_type)
330{
331 if (var_type == SMP_T_STR || var_type == SMP_T_BIN) {
332 ha_free(&var->data.u.str.area);
333 var_accounting_diff(vars, smp->sess, smp->strm,
334 -var->data.u.str.data);
335 }
336 else if (var_type == SMP_T_METH && var->data.u.meth.meth == HTTP_METH_OTHER) {
337 ha_free(&var->data.u.meth.str.area);
338 var_accounting_diff(vars, smp->sess, smp->strm,
339 -var->data.u.meth.str.data);
340 }
341}
342
Willy Tarreau3a4bedc2021-08-31 08:51:02 +0200343/* This function tries to create a variable whose name hash is <name_hash> in
344 * scope <scope> and store sample <smp> as its value.
345 *
346 * The stream and session are extracted from <smp>, whose stream may be NULL
347 * when scope is SCOPE_SESS. In case there wouldn't be enough memory to store
348 * the sample while the variable was already created, it would be changed to
349 * a bool (which is memory-less).
Willy Tarreau7978c5c2021-09-07 14:24:07 +0200350 *
351 * Flags is a bitfield that may contain one of the following flags:
Willy Tarreau4994b572021-09-08 11:38:25 +0200352 * - VF_CREATEONLY: do nothing if the variable already exists (success).
Willy Tarreau3dc6dc32021-09-08 11:07:32 +0200353 * - VF_PERMANENT: this flag will be passed to the variable upon creation
Willy Tarreau7978c5c2021-09-07 14:24:07 +0200354 *
Remi Tricot-Le Breton51899d22021-12-16 17:14:37 +0100355 * - VF_COND_IFEXISTS: only set variable if it already exists
356 * - VF_COND_IFNOTEXISTS: only set variable if it did not exist yet
357 * - VF_COND_IFEMPTY: only set variable if sample is empty
358 * - VF_COND_IFNOTEMPTY: only set variable if sample is not empty
359 * - VF_COND_IFSET: only set variable if its type is not SMP_TYPE_ANY
360 * - VF_COND_IFNOTSET: only set variable if its type is ANY
361 * - VF_COND_IFGT: only set variable if its value is greater than the sample's
362 * - VF_COND_IFLT: ony set variable if its value is less than the sample's
363 *
Willy Tarreauf1cb0eb2021-09-07 11:37:37 +0200364 * It returns 0 on failure, non-zero on success.
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200365 */
Willy Tarreau3a4bedc2021-08-31 08:51:02 +0200366static int var_set(uint64_t name_hash, enum vars_scope scope, struct sample *smp, uint flags)
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200367{
Willy Tarreauf1cb0eb2021-09-07 11:37:37 +0200368 struct vars *vars;
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200369 struct var *var;
Willy Tarreauf1cb0eb2021-09-07 11:37:37 +0200370 int ret = 0;
Remi Tricot-Le Breton25fccd52021-12-16 17:14:36 +0100371 int previous_type = SMP_T_ANY;
Willy Tarreauf1cb0eb2021-09-07 11:37:37 +0200372
373 vars = get_vars(smp->sess, smp->strm, scope);
374 if (!vars || vars->scope != scope)
375 return 0;
376
Willy Tarreaudc72fbb2021-09-08 15:19:57 +0200377 vars_wrlock(vars);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200378
379 /* Look for existing variable name. */
Willy Tarreau3a4bedc2021-08-31 08:51:02 +0200380 var = var_get(vars, name_hash);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200381
382 if (var) {
Willy Tarreau4994b572021-09-08 11:38:25 +0200383 if (flags & VF_CREATEONLY) {
384 ret = 1;
385 goto unlock;
386 }
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200387 } else {
Remi Tricot-Le Breton51899d22021-12-16 17:14:37 +0100388 if (flags & VF_COND_IFEXISTS)
Willy Tarreau7978c5c2021-09-07 14:24:07 +0200389 goto unlock;
390
Joseph Herlant07676892018-11-15 09:19:50 -0800391 /* Check memory available. */
Willy Tarreau6204cd92016-03-10 16:33:04 +0100392 if (!var_accounting_add(vars, smp->sess, smp->strm, sizeof(struct var)))
Willy Tarreauf1cb0eb2021-09-07 11:37:37 +0200393 goto unlock;
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200394
395 /* Create new entry. */
Willy Tarreaubafbe012017-11-24 17:34:44 +0100396 var = pool_alloc(var_pool);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200397 if (!var)
Willy Tarreauf1cb0eb2021-09-07 11:37:37 +0200398 goto unlock;
Willy Tarreau2b718102021-04-21 07:32:39 +0200399 LIST_APPEND(&vars->head, &var->l);
Willy Tarreau3a4bedc2021-08-31 08:51:02 +0200400 var->name_hash = name_hash;
Willy Tarreau3dc6dc32021-09-08 11:07:32 +0200401 var->flags = flags & VF_PERMANENT;
Remi Tricot-Le Breton1bd98052021-12-16 17:14:35 +0100402 var->data.type = SMP_T_ANY;
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200403 }
404
405 /* Set type. */
Remi Tricot-Le Breton25fccd52021-12-16 17:14:36 +0100406 previous_type = var->data.type;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200407 var->data.type = smp->data.type;
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200408
409 /* Copy data. If the data needs memory, the function can fail. */
410 switch (var->data.type) {
411 case SMP_T_BOOL:
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200412 case SMP_T_SINT:
Remi Tricot-Le Breton25fccd52021-12-16 17:14:36 +0100413 var_clear_buffer(smp, vars, var, previous_type);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200414 var->data.u.sint = smp->data.u.sint;
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200415 break;
416 case SMP_T_IPV4:
Remi Tricot-Le Breton25fccd52021-12-16 17:14:36 +0100417 var_clear_buffer(smp, vars, var, previous_type);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200418 var->data.u.ipv4 = smp->data.u.ipv4;
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200419 break;
420 case SMP_T_IPV6:
Remi Tricot-Le Breton25fccd52021-12-16 17:14:36 +0100421 var_clear_buffer(smp, vars, var, previous_type);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200422 var->data.u.ipv6 = smp->data.u.ipv6;
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200423 break;
424 case SMP_T_STR:
425 case SMP_T_BIN:
Remi Tricot-Le Breton25fccd52021-12-16 17:14:36 +0100426 var_clear_buffer(smp, vars, var, previous_type);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200427 if (!var_accounting_add(vars, smp->sess, smp->strm, smp->data.u.str.data)) {
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200428 var->data.type = SMP_T_BOOL; /* This type doesn't use additional memory. */
Willy Tarreauf1cb0eb2021-09-07 11:37:37 +0200429 goto unlock;
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200430 }
Willy Tarreauf1cb0eb2021-09-07 11:37:37 +0200431
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200432 var->data.u.str.area = malloc(smp->data.u.str.data);
433 if (!var->data.u.str.area) {
434 var_accounting_diff(vars, smp->sess, smp->strm,
435 -smp->data.u.str.data);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200436 var->data.type = SMP_T_BOOL; /* This type doesn't use additional memory. */
Willy Tarreauf1cb0eb2021-09-07 11:37:37 +0200437 goto unlock;
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200438 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200439 var->data.u.str.data = smp->data.u.str.data;
440 memcpy(var->data.u.str.area, smp->data.u.str.area,
441 var->data.u.str.data);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200442 break;
443 case SMP_T_METH:
Remi Tricot-Le Breton25fccd52021-12-16 17:14:36 +0100444 var_clear_buffer(smp, vars, var, previous_type);
Christopher Fauletd02210c2017-07-24 16:24:39 +0200445 var->data.u.meth.meth = smp->data.u.meth.meth;
446 if (smp->data.u.meth.meth != HTTP_METH_OTHER)
447 break;
448
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200449 if (!var_accounting_add(vars, smp->sess, smp->strm, smp->data.u.meth.str.data)) {
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200450 var->data.type = SMP_T_BOOL; /* This type doesn't use additional memory. */
Willy Tarreauf1cb0eb2021-09-07 11:37:37 +0200451 goto unlock;
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200452 }
Willy Tarreauf1cb0eb2021-09-07 11:37:37 +0200453
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200454 var->data.u.meth.str.area = malloc(smp->data.u.meth.str.data);
455 if (!var->data.u.meth.str.area) {
456 var_accounting_diff(vars, smp->sess, smp->strm,
457 -smp->data.u.meth.str.data);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200458 var->data.type = SMP_T_BOOL; /* This type doesn't use additional memory. */
Willy Tarreauf1cb0eb2021-09-07 11:37:37 +0200459 goto unlock;
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200460 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200461 var->data.u.meth.str.data = smp->data.u.meth.str.data;
462 var->data.u.meth.str.size = smp->data.u.meth.str.data;
463 memcpy(var->data.u.meth.str.area, smp->data.u.meth.str.area,
464 var->data.u.meth.str.data);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200465 break;
466 }
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200467
Willy Tarreauf1cb0eb2021-09-07 11:37:37 +0200468 /* OK, now done */
469 ret = 1;
470 unlock:
Willy Tarreaudc72fbb2021-09-08 15:19:57 +0200471 vars_wrunlock(vars);
Christopher Faulete95f2c32017-07-24 16:30:34 +0200472 return ret;
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200473}
474
Willy Tarreau3a4bedc2021-08-31 08:51:02 +0200475/* Deletes a variable matching name hash <name_hash> and scope <scope> for the
476 * session and stream found in <smp>. Note that stream may be null for
477 * SCOPE_SESS. Returns 0 if the scope was not found otherwise 1.
Willy Tarreaud378eb82021-09-07 11:44:41 +0200478 */
Willy Tarreau3a4bedc2021-08-31 08:51:02 +0200479static int var_unset(uint64_t name_hash, enum vars_scope scope, struct sample *smp)
Christopher Faulet85d79c92016-11-09 16:54:56 +0100480{
481 struct vars *vars;
482 struct var *var;
483 unsigned int size = 0;
484
Willy Tarreauf37b1402019-06-04 16:27:36 +0200485 vars = get_vars(smp->sess, smp->strm, scope);
486 if (!vars || vars->scope != scope)
Christopher Faulet85d79c92016-11-09 16:54:56 +0100487 return 0;
488
489 /* Look for existing variable name. */
Willy Tarreaudc72fbb2021-09-08 15:19:57 +0200490 vars_wrlock(vars);
Willy Tarreau3a4bedc2021-08-31 08:51:02 +0200491 var = var_get(vars, name_hash);
Christopher Faulet85d79c92016-11-09 16:54:56 +0100492 if (var) {
Willy Tarreauc1c88f42021-09-08 15:03:58 +0200493 size = var_clear(var, 0);
Christopher Faulet85d79c92016-11-09 16:54:56 +0100494 var_accounting_diff(vars, smp->sess, smp->strm, -size);
495 }
Willy Tarreaudc72fbb2021-09-08 15:19:57 +0200496 vars_wrunlock(vars);
Christopher Faulet85d79c92016-11-09 16:54:56 +0100497 return 1;
498}
499
Remi Tricot-Le Breton51899d22021-12-16 17:14:37 +0100500
501/*
502 * Convert a string set-var condition into its numerical value.
503 * The corresponding bit is set in the <cond_bitmap> parameter if the
504 * <cond> is known.
505 * Returns 1 in case of success.
506 */
507static int vars_parse_cond_param(const struct buffer *cond, uint *cond_bitmap, char **err)
508{
509 struct var_set_condition *cond_elt = &conditions_array[0];
510
511 /* The conditions array is NULL terminated. */
512 while (cond_elt->cond_str) {
513 if (chunk_strcmp(cond, cond_elt->cond_str) == 0) {
514 *cond_bitmap |= cond_elt->flag;
515 break;
516 }
517 ++cond_elt;
518 }
519
520 if (cond_elt->cond_str == NULL && err)
521 memprintf(err, "unknown condition \"%.*s\"", (int)cond->data, cond->area);
522
523 return cond_elt->cond_str != NULL;
524}
525
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200526/* Returns 0 if fails, else returns 1. */
527static int smp_conv_store(const struct arg *args, struct sample *smp, void *private)
528{
Remi Tricot-Le Breton51899d22021-12-16 17:14:37 +0100529 uint conditions = 0;
530 int cond_idx = 1;
531
532 while (args[cond_idx].type == ARGT_STR) {
533 if (vars_parse_cond_param(&args[cond_idx++].data.str, &conditions, NULL) == 0)
534 break;
535 }
536
537 return var_set(args[0].data.var.name_hash, args[0].data.var.scope, smp, conditions);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200538}
539
Christopher Faulet85d79c92016-11-09 16:54:56 +0100540/* Returns 0 if fails, else returns 1. */
541static int smp_conv_clear(const struct arg *args, struct sample *smp, void *private)
542{
Remi Tricot-Le Bretonbb3e80e2021-11-26 18:08:39 +0100543 return var_unset(args[0].data.var.name_hash, args[0].data.var.scope, smp);
Christopher Faulet85d79c92016-11-09 16:54:56 +0100544}
545
Joseph Herlant07676892018-11-15 09:19:50 -0800546/* This functions check an argument entry and fill it with a variable
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200547 * type. The argumen must be a string. If the variable lookup fails,
Joseph Herlant07676892018-11-15 09:19:50 -0800548 * the function returns 0 and fill <err>, otherwise it returns 1.
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200549 */
550int vars_check_arg(struct arg *arg, char **err)
551{
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200552 enum vars_scope scope;
Willy Tarreaudf8eeb12021-09-08 11:07:32 +0200553 struct sample empty_smp = { };
Willy Tarreau3a4bedc2021-08-31 08:51:02 +0200554 uint64_t hash;
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200555
556 /* Check arg type. */
557 if (arg->type != ARGT_STR) {
558 memprintf(err, "unexpected argument type");
559 return 0;
560 }
561
562 /* Register new variable name. */
Willy Tarreau3a4bedc2021-08-31 08:51:02 +0200563 if (!vars_hash_name(arg->data.str.area, arg->data.str.data, &scope, &hash, err))
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200564 return 0;
565
Willy Tarreau3a4bedc2021-08-31 08:51:02 +0200566 if (scope == SCOPE_PROC && !var_set(hash, scope, &empty_smp, VF_CREATEONLY|VF_PERMANENT))
Willy Tarreaudf8eeb12021-09-08 11:07:32 +0200567 return 0;
568
Tim Duesterhusa6cc7e82019-05-13 10:53:29 +0200569 /* properly destroy the chunk */
570 chunk_destroy(&arg->data.str);
571
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200572 /* Use the global variable name pointer. */
573 arg->type = ARGT_VAR;
Willy Tarreau3a4bedc2021-08-31 08:51:02 +0200574 arg->data.var.name_hash = hash;
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200575 arg->data.var.scope = scope;
576 return 1;
577}
578
Remi Tricot-Le Breton70553012021-12-16 17:14:34 +0100579/* This function stores a sample in a variable unless it is of type "proc" and
580 * not defined yet.
Tim Duesterhusb4fac1e2020-05-19 13:49:40 +0200581 * Returns zero on failure and non-zero otherwise. The variable not being
582 * defined is treated as a failure.
Christopher Faulet09c9df22016-10-31 11:05:37 +0100583 */
Tim Duesterhusb4fac1e2020-05-19 13:49:40 +0200584int vars_set_by_name_ifexist(const char *name, size_t len, struct sample *smp)
Christopher Faulet09c9df22016-10-31 11:05:37 +0100585{
586 enum vars_scope scope;
Willy Tarreau3a4bedc2021-08-31 08:51:02 +0200587 uint64_t hash;
Christopher Faulet09c9df22016-10-31 11:05:37 +0100588
589 /* Resolve name and scope. */
Willy Tarreau3a4bedc2021-08-31 08:51:02 +0200590 if (!vars_hash_name(name, len, &scope, &hash, NULL))
Tim Duesterhusb4fac1e2020-05-19 13:49:40 +0200591 return 0;
Christopher Faulet09c9df22016-10-31 11:05:37 +0100592
Remi Tricot-Le Breton70553012021-12-16 17:14:34 +0100593 /* Variable creation is allowed for all scopes apart from the PROC one. */
Remi Tricot-Le Breton51899d22021-12-16 17:14:37 +0100594 return var_set(hash, scope, smp, (scope == SCOPE_PROC) ? VF_COND_IFEXISTS : 0);
Christopher Faulet09c9df22016-10-31 11:05:37 +0100595}
596
597
Willy Tarreau3a4bedc2021-08-31 08:51:02 +0200598/* This function stores a sample in a variable.
Tim Duesterhusb4fac1e2020-05-19 13:49:40 +0200599 * Returns zero on failure and non-zero otherwise.
Thierry FOURNIERc365d992015-06-09 12:27:17 +0200600 */
Tim Duesterhusb4fac1e2020-05-19 13:49:40 +0200601int vars_set_by_name(const char *name, size_t len, struct sample *smp)
Thierry FOURNIERc365d992015-06-09 12:27:17 +0200602{
603 enum vars_scope scope;
Willy Tarreau3a4bedc2021-08-31 08:51:02 +0200604 uint64_t hash;
Thierry FOURNIERc365d992015-06-09 12:27:17 +0200605
606 /* Resolve name and scope. */
Willy Tarreau3a4bedc2021-08-31 08:51:02 +0200607 if (!vars_hash_name(name, len, &scope, &hash, NULL))
Tim Duesterhusb4fac1e2020-05-19 13:49:40 +0200608 return 0;
Thierry FOURNIERc365d992015-06-09 12:27:17 +0200609
Willy Tarreau3a4bedc2021-08-31 08:51:02 +0200610 return var_set(hash, scope, smp, 0);
Thierry FOURNIERc365d992015-06-09 12:27:17 +0200611}
612
Willy Tarreau3a4bedc2021-08-31 08:51:02 +0200613/* This function unsets a variable if it was already defined.
Tim Duesterhusb4fac1e2020-05-19 13:49:40 +0200614 * Returns zero on failure and non-zero otherwise.
Christopher Faulet85d79c92016-11-09 16:54:56 +0100615 */
Tim Duesterhusb4fac1e2020-05-19 13:49:40 +0200616int vars_unset_by_name_ifexist(const char *name, size_t len, struct sample *smp)
Christopher Faulet85d79c92016-11-09 16:54:56 +0100617{
618 enum vars_scope scope;
Willy Tarreau3a4bedc2021-08-31 08:51:02 +0200619 uint64_t hash;
Christopher Faulet85d79c92016-11-09 16:54:56 +0100620
621 /* Resolve name and scope. */
Willy Tarreau3a4bedc2021-08-31 08:51:02 +0200622 if (!vars_hash_name(name, len, &scope, &hash, NULL))
Tim Duesterhusb4fac1e2020-05-19 13:49:40 +0200623 return 0;
Christopher Faulet85d79c92016-11-09 16:54:56 +0100624
Willy Tarreau3a4bedc2021-08-31 08:51:02 +0200625 return var_unset(hash, scope, smp);
Christopher Faulet85d79c92016-11-09 16:54:56 +0100626}
627
628
Willy Tarreau3a4bedc2021-08-31 08:51:02 +0200629/* This retrieves variable whose hash matches <name_hash> from variables <vars>,
630 * and if found and not empty, duplicates the result into sample <smp>.
631 * smp_dup() is used in order to release the variables lock ASAP (so a pre-
632 * allocated chunk is obtained via get_trash_shunk()). The variables' lock is
633 * used for reads.
Willy Tarreaube7e00d2021-09-03 11:40:58 +0200634 *
Willy Tarreaue352b9d2021-09-03 11:52:38 +0200635 * The function returns 0 if the variable was not found and no default
636 * value was provided in <def>, otherwise 1 with the sample filled.
637 * Default values are always returned as strings.
Willy Tarreaube7e00d2021-09-03 11:40:58 +0200638 */
Willy Tarreau3a4bedc2021-08-31 08:51:02 +0200639static int var_to_smp(struct vars *vars, uint64_t name_hash, struct sample *smp, const struct buffer *def)
Willy Tarreaube7e00d2021-09-03 11:40:58 +0200640{
641 struct var *var;
642
643 /* Get the variable entry. */
Willy Tarreaudc72fbb2021-09-08 15:19:57 +0200644 vars_rdlock(vars);
Willy Tarreau3a4bedc2021-08-31 08:51:02 +0200645 var = var_get(vars, name_hash);
Willy Tarreau63c30662021-09-08 13:58:19 +0200646 if (!var || !var->data.type) {
Willy Tarreaue352b9d2021-09-03 11:52:38 +0200647 if (!def) {
Willy Tarreaudc72fbb2021-09-08 15:19:57 +0200648 vars_rdunlock(vars);
Willy Tarreaue352b9d2021-09-03 11:52:38 +0200649 return 0;
650 }
651
652 /* not found but we have a default value */
653 smp->data.type = SMP_T_STR;
654 smp->data.u.str = *def;
Willy Tarreaube7e00d2021-09-03 11:40:58 +0200655 }
Willy Tarreaue352b9d2021-09-03 11:52:38 +0200656 else
657 smp->data = var->data;
Willy Tarreaube7e00d2021-09-03 11:40:58 +0200658
659 /* Copy sample. */
Willy Tarreaube7e00d2021-09-03 11:40:58 +0200660 smp_dup(smp);
661
Willy Tarreaudc72fbb2021-09-08 15:19:57 +0200662 vars_rdunlock(vars);
Willy Tarreaube7e00d2021-09-03 11:40:58 +0200663 return 1;
664}
665
Dragan Dosen14518f22021-02-22 17:20:01 +0100666/* This function fills a sample with the variable content.
667 *
668 * Keep in mind that a sample content is duplicated by using smp_dup()
669 * and it therefore uses a pre-allocated trash chunk as returned by
670 * get_trash_chunk().
671 *
Willy Tarreaue352b9d2021-09-03 11:52:38 +0200672 * If the variable is not valid in this scope, 0 is always returned.
673 * If the variable is valid but not found, either the default value
674 * <def> is returned if not NULL, or zero is returned.
675 *
Dragan Dosen14518f22021-02-22 17:20:01 +0100676 * Returns 1 if the sample is filled, otherwise it returns 0.
Thierry FOURNIERc365d992015-06-09 12:27:17 +0200677 */
Willy Tarreaue352b9d2021-09-03 11:52:38 +0200678int vars_get_by_name(const char *name, size_t len, struct sample *smp, const struct buffer *def)
Thierry FOURNIERc365d992015-06-09 12:27:17 +0200679{
680 struct vars *vars;
Thierry FOURNIERc365d992015-06-09 12:27:17 +0200681 enum vars_scope scope;
Willy Tarreau3a4bedc2021-08-31 08:51:02 +0200682 uint64_t hash;
Thierry FOURNIERc365d992015-06-09 12:27:17 +0200683
684 /* Resolve name and scope. */
Willy Tarreau3a4bedc2021-08-31 08:51:02 +0200685 if (!vars_hash_name(name, len, &scope, &hash, NULL))
Thierry FOURNIERc365d992015-06-09 12:27:17 +0200686 return 0;
687
688 /* Select "vars" pool according with the scope. */
Willy Tarreauf37b1402019-06-04 16:27:36 +0200689 vars = get_vars(smp->sess, smp->strm, scope);
690 if (!vars || vars->scope != scope)
Thierry FOURNIERc365d992015-06-09 12:27:17 +0200691 return 0;
Thierry FOURNIERc365d992015-06-09 12:27:17 +0200692
Willy Tarreau3a4bedc2021-08-31 08:51:02 +0200693 return var_to_smp(vars, hash, smp, def);
Thierry FOURNIERc365d992015-06-09 12:27:17 +0200694}
695
Dragan Dosen14518f22021-02-22 17:20:01 +0100696/* This function fills a sample with the content of the variable described
697 * by <var_desc>.
698 *
699 * Keep in mind that a sample content is duplicated by using smp_dup()
700 * and it therefore uses a pre-allocated trash chunk as returned by
701 * get_trash_chunk().
702 *
Willy Tarreaue352b9d2021-09-03 11:52:38 +0200703 * If the variable is not valid in this scope, 0 is always returned.
704 * If the variable is valid but not found, either the default value
705 * <def> is returned if not NULL, or zero is returned.
706 *
Dragan Dosen14518f22021-02-22 17:20:01 +0100707 * Returns 1 if the sample is filled, otherwise it returns 0.
Thierry FOURNIERfd77e052015-07-07 21:20:42 +0200708 */
Willy Tarreaue352b9d2021-09-03 11:52:38 +0200709int vars_get_by_desc(const struct var_desc *var_desc, struct sample *smp, const struct buffer *def)
Thierry FOURNIERfd77e052015-07-07 21:20:42 +0200710{
711 struct vars *vars;
Thierry FOURNIERfd77e052015-07-07 21:20:42 +0200712
713 /* Select "vars" pool according with the scope. */
Willy Tarreauf37b1402019-06-04 16:27:36 +0200714 vars = get_vars(smp->sess, smp->strm, var_desc->scope);
Thierry FOURNIERfd77e052015-07-07 21:20:42 +0200715
Joseph Herlant07676892018-11-15 09:19:50 -0800716 /* Check if the scope is available a this point of processing. */
Willy Tarreauf37b1402019-06-04 16:27:36 +0200717 if (!vars || vars->scope != var_desc->scope)
Thierry FOURNIERfd77e052015-07-07 21:20:42 +0200718 return 0;
719
Willy Tarreau3a4bedc2021-08-31 08:51:02 +0200720 return var_to_smp(vars, var_desc->name_hash, smp, def);
Thierry FOURNIERfd77e052015-07-07 21:20:42 +0200721}
722
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +0200723/* Always returns ACT_RET_CONT even if an error occurs. */
724static enum act_return action_store(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +0200725 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200726{
Willy Tarreau9a621ae2021-09-02 21:00:38 +0200727 struct buffer *fmtstr = NULL;
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200728 struct sample smp;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +0200729 int dir;
730
731 switch (rule->from) {
Jaroslaw Rzeszótkoc8637032021-11-02 16:56:05 +0100732 case ACT_F_TCP_REQ_CON: dir = SMP_OPT_DIR_REQ; break;
Willy Tarreau620408f2016-10-21 16:37:51 +0200733 case ACT_F_TCP_REQ_SES: dir = SMP_OPT_DIR_REQ; break;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +0200734 case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break;
735 case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break;
736 case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break;
737 case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break;
Gaetan Rivet0c39ecc2020-02-24 17:34:11 +0100738 case ACT_F_TCP_CHK: dir = SMP_OPT_DIR_REQ; break;
Willy Tarreau01d580a2021-03-26 11:11:34 +0100739 case ACT_F_CFG_PARSER: dir = SMP_OPT_DIR_REQ; break; /* not used anyway */
Willy Tarreau2f836de2021-03-26 15:36:44 +0100740 case ACT_F_CLI_PARSER: dir = SMP_OPT_DIR_REQ; break; /* not used anyway */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +0200741 default:
742 send_log(px, LOG_ERR, "Vars: internal error while execute action store.");
743 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))
Christopher Faulet767a84b2017-11-24 16:50:31 +0100744 ha_alert("Vars: internal error while execute action store.\n");
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +0200745 return ACT_RET_CONT;
746 }
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200747
748 /* Process the expression. */
749 memset(&smp, 0, sizeof(smp));
Willy Tarreau9a621ae2021-09-02 21:00:38 +0200750
751 if (!LIST_ISEMPTY(&rule->arg.vars.fmt)) {
752 /* a format-string is used */
753
754 fmtstr = alloc_trash_chunk();
755 if (!fmtstr) {
756 send_log(px, LOG_ERR, "Vars: memory allocation failure while processing store rule.");
757 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))
758 ha_alert("Vars: memory allocation failure while processing store rule.\n");
759 return ACT_RET_CONT;
760 }
761
762 /* execute the log-format expression */
763 fmtstr->data = sess_build_logline(sess, s, fmtstr->area, fmtstr->size, &rule->arg.vars.fmt);
764
765 /* convert it to a sample of type string as it's what the vars
766 * API consumes, and store it.
767 */
768 smp_set_owner(&smp, px, sess, s, 0);
769 smp.data.type = SMP_T_STR;
770 smp.data.u.str = *fmtstr;
Willy Tarreau3a4bedc2021-08-31 08:51:02 +0200771 var_set(rule->arg.vars.name_hash, rule->arg.vars.scope, &smp, 0);
Willy Tarreau9a621ae2021-09-02 21:00:38 +0200772 }
773 else {
774 /* an expression is used */
775 if (!sample_process(px, sess, s, dir|SMP_OPT_FINAL,
776 rule->arg.vars.expr, &smp))
777 return ACT_RET_CONT;
778 }
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200779
780 /* Store the sample, and ignore errors. */
Willy Tarreau3a4bedc2021-08-31 08:51:02 +0200781 var_set(rule->arg.vars.name_hash, rule->arg.vars.scope, &smp, 0);
Willy Tarreau9a621ae2021-09-02 21:00:38 +0200782 free_trash_chunk(fmtstr);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +0200783 return ACT_RET_CONT;
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200784}
785
Christopher Faulet85d79c92016-11-09 16:54:56 +0100786/* Always returns ACT_RET_CONT even if an error occurs. */
787static enum act_return action_clear(struct act_rule *rule, struct proxy *px,
788 struct session *sess, struct stream *s, int flags)
789{
790 struct sample smp;
791
792 memset(&smp, 0, sizeof(smp));
793 smp_set_owner(&smp, px, sess, s, SMP_OPT_FINAL);
794
795 /* Clear the variable using the sample context, and ignore errors. */
Willy Tarreau3a4bedc2021-08-31 08:51:02 +0200796 var_unset(rule->arg.vars.name_hash, rule->arg.vars.scope, &smp);
Christopher Faulet85d79c92016-11-09 16:54:56 +0100797 return ACT_RET_CONT;
798}
799
Tim Duesterhus01a0ce32020-06-14 17:27:36 +0200800static void release_store_rule(struct act_rule *rule)
801{
Willy Tarreau9a621ae2021-09-02 21:00:38 +0200802 struct logformat_node *lf, *lfb;
Willy Tarreauc77bad22021-09-03 10:58:07 +0200803
804 list_for_each_entry_safe(lf, lfb, &rule->arg.vars.fmt, list) {
Willy Tarreau9a621ae2021-09-02 21:00:38 +0200805 LIST_DELETE(&lf->list);
806 release_sample_expr(lf->expr);
807 free(lf->arg);
808 free(lf);
809 }
810
Tim Duesterhus01a0ce32020-06-14 17:27:36 +0200811 release_sample_expr(rule->arg.vars.expr);
812}
813
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200814/* This two function checks the variable name and replace the
815 * configuration string name by the global string name. its
816 * the same string, but the global pointer can be easy to
Willy Tarreaue352b9d2021-09-03 11:52:38 +0200817 * compare. They return non-zero on success, zero on failure.
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200818 *
819 * The first function checks a sample-fetch and the second
820 * checks a converter.
821 */
822static int smp_check_var(struct arg *args, char **err)
823{
824 return vars_check_arg(&args[0], err);
825}
826
827static int conv_check_var(struct arg *args, struct sample_conv *conv,
828 const char *file, int line, char **err_msg)
829{
Remi Tricot-Le Breton51899d22021-12-16 17:14:37 +0100830 int cond_idx = 1;
831 uint conditions = 0;
832 int retval = vars_check_arg(&args[0], err_msg);
833
834 while (retval && args[cond_idx].type == ARGT_STR)
835 retval = vars_parse_cond_param(&args[cond_idx++].data.str, &conditions, err_msg);
836
837 return retval;
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200838}
839
840/* This function is a common parser for using variables. It understands
841 * the format:
842 *
Remi Tricot-Le Bretonbb6bc952021-12-16 17:14:38 +0100843 * set-var-fmt(<variable-name>[,<cond> ...]) <format-string>
844 * set-var(<variable-name>[,<cond> ...]) <expression>
Willy Tarreau4b7531f2019-06-04 16:43:29 +0200845 * unset-var(<variable-name>)
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200846 *
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +0200847 * It returns ACT_RET_PRS_ERR if fails and <err> is filled with an error
848 * message. Otherwise, it returns ACT_RET_PRS_OK and the variable <expr>
Willy Tarreau13d2ba22021-03-26 11:38:08 +0100849 * is filled with the pointer to the expression to execute. The proxy is
850 * only used to retrieve the ->conf entries.
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200851 */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +0200852static enum act_parse_ret parse_store(const char **args, int *arg, struct proxy *px,
853 struct act_rule *rule, char **err)
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200854{
855 const char *var_name = args[*arg-1];
856 int var_len;
Thierry FOURNIER48a9cd12015-07-28 19:00:28 +0200857 const char *kw_name;
Christopher Faulet7a06ffb2021-10-13 17:22:17 +0200858 int flags = 0, set_var = 0; /* 0=unset-var, 1=set-var, 2=set-var-fmt */
Willy Tarreaudf8eeb12021-09-08 11:07:32 +0200859 struct sample empty_smp = { };
Remi Tricot-Le Bretonbb6bc952021-12-16 17:14:38 +0100860 struct ist condition = IST_NULL;
861 struct ist var = IST_NULL;
862 struct ist varname_ist = IST_NULL;
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200863
Willy Tarreau9a621ae2021-09-02 21:00:38 +0200864 if (strncmp(var_name, "set-var-fmt", 11) == 0) {
865 var_name += 11;
866 set_var = 2;
867 }
868 else if (strncmp(var_name, "set-var", 7) == 0) {
Christopher Faulet85d79c92016-11-09 16:54:56 +0100869 var_name += 7;
870 set_var = 1;
871 }
Willy Tarreau28192102021-09-02 18:46:22 +0200872 else if (strncmp(var_name, "unset-var", 9) == 0) {
Christopher Faulet85d79c92016-11-09 16:54:56 +0100873 var_name += 9;
874 set_var = 0;
875 }
876
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200877 if (*var_name != '(') {
Willy Tarreau9a621ae2021-09-02 21:00:38 +0200878 memprintf(err, "invalid or incomplete action '%s'. Expects 'set-var(<var-name>)', 'set-var-fmt(<var-name>)' or 'unset-var(<var-name>)'",
Christopher Faulet85d79c92016-11-09 16:54:56 +0100879 args[*arg-1]);
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +0200880 return ACT_RET_PRS_ERR;
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200881 }
882 var_name++; /* jump the '(' */
883 var_len = strlen(var_name);
884 var_len--; /* remove the ')' */
885 if (var_name[var_len] != ')') {
Willy Tarreau9a621ae2021-09-02 21:00:38 +0200886 memprintf(err, "incomplete argument after action '%s'. Expects 'set-var(<var-name>)', 'set-var-fmt(<var-name>)' or 'unset-var(<var-name>)'",
Christopher Faulet85d79c92016-11-09 16:54:56 +0100887 args[*arg-1]);
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +0200888 return ACT_RET_PRS_ERR;
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200889 }
890
Remi Tricot-Le Bretonbb6bc952021-12-16 17:14:38 +0100891 /* Parse the optional conditions. */
892 var = ist2(var_name, var_len);
893 varname_ist = istsplit(&var, ',');
894 var_len = istlen(varname_ist);
895
896 condition = istsplit(&var, ',');
897
898 if (istlen(condition) && set_var == 0) {
899 memprintf(err, "unset-var does not expect parameters after the variable name. Only \"set-var\" and \"set-var-fmt\" manage conditions");
900 return ACT_RET_PRS_ERR;
901 }
902
903 while (istlen(condition)) {
904 struct buffer cond = {};
905
906 chunk_initlen(&cond, istptr(condition), 0, istlen(condition));
907 if (vars_parse_cond_param(&cond, &rule->arg.vars.conditions, err) == 0)
908 return ACT_RET_PRS_ERR;
909
910 condition = istsplit(&var, ',');
911 }
912
Willy Tarreau9a621ae2021-09-02 21:00:38 +0200913 LIST_INIT(&rule->arg.vars.fmt);
Willy Tarreau3a4bedc2021-08-31 08:51:02 +0200914 if (!vars_hash_name(var_name, var_len, &rule->arg.vars.scope, &rule->arg.vars.name_hash, err))
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +0200915 return ACT_RET_PRS_ERR;
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200916
Willy Tarreaudf8eeb12021-09-08 11:07:32 +0200917 if (rule->arg.vars.scope == SCOPE_PROC &&
Willy Tarreau3a4bedc2021-08-31 08:51:02 +0200918 !var_set(rule->arg.vars.name_hash, rule->arg.vars.scope, &empty_smp, VF_CREATEONLY|VF_PERMANENT))
Willy Tarreaudf8eeb12021-09-08 11:07:32 +0200919 return 0;
920
Christopher Faulet85d79c92016-11-09 16:54:56 +0100921 /* There is no fetch method when variable is unset. Just set the right
922 * action and return. */
923 if (!set_var) {
Christopher Faulet85d79c92016-11-09 16:54:56 +0100924 rule->action = ACT_CUSTOM;
925 rule->action_ptr = action_clear;
Tim Duesterhus01a0ce32020-06-14 17:27:36 +0200926 rule->release_ptr = release_store_rule;
Christopher Faulet85d79c92016-11-09 16:54:56 +0100927 return ACT_RET_PRS_OK;
928 }
929
Thierry FOURNIER48a9cd12015-07-28 19:00:28 +0200930 kw_name = args[*arg-1];
931
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +0200932 switch (rule->from) {
Jaroslaw Rzeszótkoc8637032021-11-02 16:56:05 +0100933 case ACT_F_TCP_REQ_CON:
934 flags = SMP_VAL_FE_CON_ACC;
935 px->conf.args.ctx = ARGC_TCO;
936 break;
Willy Tarreau843096d2021-09-02 19:03:07 +0200937 case ACT_F_TCP_REQ_SES:
938 flags = SMP_VAL_FE_SES_ACC;
Willy Tarreau54b96d92021-09-02 19:46:08 +0200939 px->conf.args.ctx = ARGC_TSE;
Willy Tarreau843096d2021-09-02 19:03:07 +0200940 break;
941 case ACT_F_TCP_REQ_CNT:
Christopher Faulet7a06ffb2021-10-13 17:22:17 +0200942 if (px->cap & PR_CAP_FE)
943 flags |= SMP_VAL_FE_REQ_CNT;
944 if (px->cap & PR_CAP_BE)
945 flags |= SMP_VAL_BE_REQ_CNT;
Willy Tarreau54b96d92021-09-02 19:46:08 +0200946 px->conf.args.ctx = ARGC_TRQ;
Willy Tarreau843096d2021-09-02 19:03:07 +0200947 break;
948 case ACT_F_TCP_RES_CNT:
Christopher Faulet7a06ffb2021-10-13 17:22:17 +0200949 if (px->cap & PR_CAP_FE)
950 flags |= SMP_VAL_FE_RES_CNT;
951 if (px->cap & PR_CAP_BE)
952 flags |= SMP_VAL_BE_RES_CNT;
Willy Tarreau54b96d92021-09-02 19:46:08 +0200953 px->conf.args.ctx = ARGC_TRS;
Willy Tarreau843096d2021-09-02 19:03:07 +0200954 break;
955 case ACT_F_HTTP_REQ:
Christopher Faulet7a06ffb2021-10-13 17:22:17 +0200956 if (px->cap & PR_CAP_FE)
957 flags |= SMP_VAL_FE_HRQ_HDR;
958 if (px->cap & PR_CAP_BE)
959 flags |= SMP_VAL_BE_HRQ_HDR;
Willy Tarreau54b96d92021-09-02 19:46:08 +0200960 px->conf.args.ctx = ARGC_HRQ;
Willy Tarreau843096d2021-09-02 19:03:07 +0200961 break;
962 case ACT_F_HTTP_RES:
Christopher Faulet7a06ffb2021-10-13 17:22:17 +0200963 if (px->cap & PR_CAP_FE)
964 flags |= SMP_VAL_FE_HRS_HDR;
965 if (px->cap & PR_CAP_BE)
966 flags |= SMP_VAL_BE_HRS_HDR;
Willy Tarreau54b96d92021-09-02 19:46:08 +0200967 px->conf.args.ctx = ARGC_HRS;
Willy Tarreau843096d2021-09-02 19:03:07 +0200968 break;
969 case ACT_F_TCP_CHK:
970 flags = SMP_VAL_BE_CHK_RUL;
Willy Tarreau54b96d92021-09-02 19:46:08 +0200971 px->conf.args.ctx = ARGC_TCK;
Willy Tarreau843096d2021-09-02 19:03:07 +0200972 break;
973 case ACT_F_CFG_PARSER:
974 flags = SMP_VAL_CFG_PARSER;
Willy Tarreau54b96d92021-09-02 19:46:08 +0200975 px->conf.args.ctx = ARGC_CFG;
Willy Tarreau843096d2021-09-02 19:03:07 +0200976 break;
977 case ACT_F_CLI_PARSER:
978 flags = SMP_VAL_CLI_PARSER;
Willy Tarreau54b96d92021-09-02 19:46:08 +0200979 px->conf.args.ctx = ARGC_CLI;
Willy Tarreau843096d2021-09-02 19:03:07 +0200980 break;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +0200981 default:
982 memprintf(err,
983 "internal error, unexpected rule->from=%d, please report this bug!",
984 rule->from);
Thierry FOURNIERafa80492015-08-19 09:04:15 +0200985 return ACT_RET_PRS_ERR;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +0200986 }
Willy Tarreau54b96d92021-09-02 19:46:08 +0200987
Willy Tarreau9a621ae2021-09-02 21:00:38 +0200988 if (set_var == 2) { /* set-var-fmt */
989 if (!parse_logformat_string(args[*arg], px, &rule->arg.vars.fmt, 0, flags, err))
990 return ACT_RET_PRS_ERR;
Willy Tarreau54b96d92021-09-02 19:46:08 +0200991
Willy Tarreau9a621ae2021-09-02 21:00:38 +0200992 (*arg)++;
993
994 /* for late error reporting */
995 free(px->conf.lfs_file);
996 px->conf.lfs_file = strdup(px->conf.args.file);
997 px->conf.lfs_line = px->conf.args.line;
998 } else {
999 /* set-var */
1000 rule->arg.vars.expr = sample_parse_expr((char **)args, arg, px->conf.args.file,
Christopher Faulet6ff7de52021-10-13 15:18:36 +02001001 px->conf.args.line, err, &px->conf.args, NULL);
Willy Tarreau9a621ae2021-09-02 21:00:38 +02001002 if (!rule->arg.vars.expr)
1003 return ACT_RET_PRS_ERR;
1004
1005 if (!(rule->arg.vars.expr->fetch->val & flags)) {
1006 memprintf(err,
1007 "fetch method '%s' extracts information from '%s', none of which is available here",
1008 kw_name, sample_src_names(rule->arg.vars.expr->fetch->use));
1009 free(rule->arg.vars.expr);
1010 return ACT_RET_PRS_ERR;
1011 }
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02001012 }
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02001013
Thierry FOURNIER42148732015-09-02 17:17:33 +02001014 rule->action = ACT_CUSTOM;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02001015 rule->action_ptr = action_store;
Tim Duesterhus01a0ce32020-06-14 17:27:36 +02001016 rule->release_ptr = release_store_rule;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02001017 return ACT_RET_PRS_OK;
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02001018}
1019
Willy Tarreau13d2ba22021-03-26 11:38:08 +01001020
1021/* parses a global "set-var" directive. It will create a temporary rule and
1022 * expression that are parsed, processed, and released on the fly so that we
1023 * respect the real set-var syntax. These directives take the following format:
1024 * set-var <name> <expression>
Willy Tarreau753d4db2021-09-03 09:02:47 +02001025 * set-var-fmt <name> <fmt>
Willy Tarreau13d2ba22021-03-26 11:38:08 +01001026 * Note that parse_store() expects "set-var(name) <expression>" so we have to
1027 * temporarily replace the keyword here.
1028 */
1029static int vars_parse_global_set_var(char **args, int section_type, struct proxy *curpx,
1030 const struct proxy *defpx, const char *file, int line,
1031 char **err)
1032{
1033 struct proxy px = {
Willy Tarreau9c204332021-09-03 08:19:43 +02001034 .id = "CFG",
Willy Tarreau13d2ba22021-03-26 11:38:08 +01001035 .conf.args.file = file,
1036 .conf.args.line = line,
1037 };
1038 struct act_rule rule = {
1039 .arg.vars.scope = SCOPE_PROC,
1040 .from = ACT_F_CFG_PARSER,
Willy Tarreauc9e48682021-10-11 09:13:07 +02001041 .conf.file = (char *)file,
1042 .conf.line = line,
Willy Tarreau13d2ba22021-03-26 11:38:08 +01001043 };
Willy Tarreau753d4db2021-09-03 09:02:47 +02001044 enum obj_type objt = OBJ_TYPE_NONE;
1045 struct session *sess = NULL;
Willy Tarreau13d2ba22021-03-26 11:38:08 +01001046 enum act_parse_ret p_ret;
1047 char *old_arg1;
1048 char *tmp_arg1;
1049 int arg = 2; // variable name
1050 int ret = -1;
Willy Tarreau753d4db2021-09-03 09:02:47 +02001051 int use_fmt = 0;
Willy Tarreau13d2ba22021-03-26 11:38:08 +01001052
1053 LIST_INIT(&px.conf.args.list);
1054
Willy Tarreau753d4db2021-09-03 09:02:47 +02001055 use_fmt = strcmp(args[0], "set-var-fmt") == 0;
1056
Willy Tarreau13d2ba22021-03-26 11:38:08 +01001057 if (!*args[1] || !*args[2]) {
Willy Tarreau753d4db2021-09-03 09:02:47 +02001058 if (use_fmt)
1059 memprintf(err, "'%s' requires a process-wide variable name ('proc.<name>') and a format string.", args[0]);
1060 else
1061 memprintf(err, "'%s' requires a process-wide variable name ('proc.<name>') and a sample expression.", args[0]);
Willy Tarreau13d2ba22021-03-26 11:38:08 +01001062 goto end;
1063 }
1064
1065 tmp_arg1 = NULL;
Willy Tarreau753d4db2021-09-03 09:02:47 +02001066 if (!memprintf(&tmp_arg1, "set-var%s(%s)", use_fmt ? "-fmt" : "", args[1]))
Willy Tarreau13d2ba22021-03-26 11:38:08 +01001067 goto end;
1068
1069 /* parse_store() will always return a message in <err> on error */
1070 old_arg1 = args[1]; args[1] = tmp_arg1;
1071 p_ret = parse_store((const char **)args, &arg, &px, &rule, err);
1072 free(args[1]); args[1] = old_arg1;
1073
1074 if (p_ret != ACT_RET_PRS_OK)
1075 goto end;
1076
1077 if (rule.arg.vars.scope != SCOPE_PROC) {
1078 memprintf(err, "'%s': cannot set variable '%s', only scope 'proc' is permitted in the global section.", args[0], args[1]);
1079 goto end;
1080 }
1081
1082 if (smp_resolve_args(&px, err) != 0) {
1083 release_sample_expr(rule.arg.vars.expr);
1084 indent_msg(err, 2);
1085 goto end;
1086 }
1087
Willy Tarreau753d4db2021-09-03 09:02:47 +02001088 if (use_fmt && !(sess = session_new(&px, NULL, &objt))) {
1089 release_sample_expr(rule.arg.vars.expr);
1090 memprintf(err, "'%s': out of memory when trying to set variable '%s' in the global section.", args[0], args[1]);
1091 goto end;
1092 }
1093
1094 action_store(&rule, &px, sess, NULL, 0);
Willy Tarreau13d2ba22021-03-26 11:38:08 +01001095 release_sample_expr(rule.arg.vars.expr);
Willy Tarreau753d4db2021-09-03 09:02:47 +02001096 if (sess)
1097 session_free(sess);
Willy Tarreau13d2ba22021-03-26 11:38:08 +01001098
1099 ret = 0;
1100 end:
1101 return ret;
1102}
1103
Willy Tarreauc35eb382021-03-26 14:51:31 +01001104/* parse CLI's "get var <name>" */
1105static int vars_parse_cli_get_var(char **args, char *payload, struct appctx *appctx, void *private)
1106{
1107 struct vars *vars;
Willy Tarreau374edc72021-04-01 17:01:43 +02001108 struct sample smp = { };
Willy Tarreauc35eb382021-03-26 14:51:31 +01001109 int i;
1110
1111 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
1112 return 1;
1113
1114 if (!*args[2])
1115 return cli_err(appctx, "Missing process-wide variable identifier.\n");
1116
1117 vars = get_vars(NULL, NULL, SCOPE_PROC);
1118 if (!vars || vars->scope != SCOPE_PROC)
1119 return 0;
1120
Willy Tarreaue352b9d2021-09-03 11:52:38 +02001121 if (!vars_get_by_name(args[2], strlen(args[2]), &smp, NULL))
Willy Tarreauc35eb382021-03-26 14:51:31 +01001122 return cli_err(appctx, "Variable not found.\n");
1123
1124 /* the sample returned by vars_get_by_name() is allocated into a trash
1125 * chunk so we have no constraint to manipulate it.
1126 */
1127 chunk_printf(&trash, "%s: type=%s value=", args[2], smp_to_type[smp.data.type]);
1128
1129 if (!sample_casts[smp.data.type][SMP_T_STR] ||
1130 !sample_casts[smp.data.type][SMP_T_STR](&smp)) {
1131 chunk_appendf(&trash, "(undisplayable)");
1132 } else {
1133 /* Display the displayable chars*. */
1134 b_putchr(&trash, '<');
1135 for (i = 0; i < smp.data.u.str.data; i++) {
1136 if (isprint((unsigned char)smp.data.u.str.area[i]))
1137 b_putchr(&trash, smp.data.u.str.area[i]);
1138 else
1139 b_putchr(&trash, '.');
1140 }
1141 b_putchr(&trash, '>');
1142 b_putchr(&trash, 0);
1143 }
1144 return cli_msg(appctx, LOG_INFO, trash.area);
1145}
1146
Willy Tarreaue93bff42021-09-03 09:47:37 +02001147/* parse CLI's "set var <name>". It accepts:
1148 * - set var <name> <expression>
1149 * - set var <name> expr <expression>
1150 * - set var <name> fmt <format>
1151 */
Willy Tarreaub8bd1ee2021-03-26 15:19:50 +01001152static int vars_parse_cli_set_var(char **args, char *payload, struct appctx *appctx, void *private)
1153{
1154 struct proxy px = {
1155 .id = "CLI",
1156 .conf.args.file = "CLI",
1157 .conf.args.line = 0,
1158 };
1159 struct act_rule rule = {
1160 .arg.vars.scope = SCOPE_PROC,
1161 .from = ACT_F_CLI_PARSER,
Willy Tarreauc9e48682021-10-11 09:13:07 +02001162 .conf.file = "CLI",
1163 .conf.line = 0,
Willy Tarreaub8bd1ee2021-03-26 15:19:50 +01001164 };
Willy Tarreaue93bff42021-09-03 09:47:37 +02001165 enum obj_type objt = OBJ_TYPE_NONE;
1166 struct session *sess = NULL;
Willy Tarreaub8bd1ee2021-03-26 15:19:50 +01001167 enum act_parse_ret p_ret;
Willy Tarreaue93bff42021-09-03 09:47:37 +02001168 const char *tmp_args[3];
1169 int tmp_arg;
1170 char *tmp_act;
Willy Tarreaub8bd1ee2021-03-26 15:19:50 +01001171 char *err = NULL;
Willy Tarreaub8bd1ee2021-03-26 15:19:50 +01001172 int nberr;
Willy Tarreaue93bff42021-09-03 09:47:37 +02001173 int use_fmt = 0;
Willy Tarreaub8bd1ee2021-03-26 15:19:50 +01001174
1175 LIST_INIT(&px.conf.args.list);
1176
1177 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
1178 return 1;
1179
Willy Tarreaue93bff42021-09-03 09:47:37 +02001180 if (!*args[2])
1181 return cli_err(appctx, "Missing process-wide variable identifier.\n");
Willy Tarreaub8bd1ee2021-03-26 15:19:50 +01001182
Willy Tarreaue93bff42021-09-03 09:47:37 +02001183 if (!*args[3])
1184 return cli_err(appctx, "Missing either 'expr', 'fmt' or expression.\n");
1185
1186 if (*args[4]) {
1187 /* this is the long format */
1188 if (strcmp(args[3], "fmt") == 0)
1189 use_fmt = 1;
1190 else if (strcmp(args[3], "expr") != 0) {
1191 memprintf(&err, "'%s %s': arg type must be either 'expr' or 'fmt' but got '%s'.", args[0], args[1], args[3]);
1192 goto fail;
1193 }
1194 }
1195
1196 tmp_act = NULL;
1197 if (!memprintf(&tmp_act, "set-var%s(%s)", use_fmt ? "-fmt" : "", args[2])) {
Willy Tarreaub8bd1ee2021-03-26 15:19:50 +01001198 memprintf(&err, "memory allocation error.");
1199 goto fail;
1200 }
1201
1202 /* parse_store() will always return a message in <err> on error */
Willy Tarreaue93bff42021-09-03 09:47:37 +02001203 tmp_args[0] = tmp_act;
1204 tmp_args[1] = (*args[4]) ? args[4] : args[3];
1205 tmp_args[2] = "";
1206 tmp_arg = 1; // must point to the first arg after the action
1207 p_ret = parse_store(tmp_args, &tmp_arg, &px, &rule, &err);
1208 free(tmp_act);
Willy Tarreaub8bd1ee2021-03-26 15:19:50 +01001209
1210 if (p_ret != ACT_RET_PRS_OK)
1211 goto fail;
1212
1213 if (rule.arg.vars.scope != SCOPE_PROC) {
Willy Tarreauc767eeb2021-09-03 10:23:26 +02001214 memprintf(&err, "'%s %s': cannot set variable '%s', only scope 'proc' is permitted here.", args[0], args[1], args[2]);
Willy Tarreaub8bd1ee2021-03-26 15:19:50 +01001215 goto fail;
1216 }
1217
1218 err = NULL;
1219 nberr = smp_resolve_args(&px, &err);
1220 if (nberr) {
1221 release_sample_expr(rule.arg.vars.expr);
1222 indent_msg(&err, 2);
1223 goto fail;
1224 }
1225
Willy Tarreaue93bff42021-09-03 09:47:37 +02001226 if (use_fmt && !(sess = session_new(&px, NULL, &objt))) {
1227 release_sample_expr(rule.arg.vars.expr);
1228 memprintf(&err, "memory allocation error.");
1229 goto fail;
1230 }
1231
1232 action_store(&rule, &px, sess, NULL, 0);
Willy Tarreaub8bd1ee2021-03-26 15:19:50 +01001233 release_sample_expr(rule.arg.vars.expr);
Willy Tarreaue93bff42021-09-03 09:47:37 +02001234 if (sess)
1235 session_free(sess);
1236
Willy Tarreaub8bd1ee2021-03-26 15:19:50 +01001237 appctx->st0 = CLI_ST_PROMPT;
1238 return 0;
1239 fail:
1240 return cli_dynerr(appctx, err);
1241}
1242
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02001243static int vars_max_size(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01001244 const struct proxy *defpx, const char *file, int line,
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02001245 char **err, unsigned int *limit)
1246{
1247 char *error;
1248
1249 *limit = strtol(args[1], &error, 10);
1250 if (*error != 0) {
1251 memprintf(err, "%s: '%s' is an invalid size", args[0], args[1]);
1252 return -1;
1253 }
1254 return 0;
1255}
1256
1257static int vars_max_size_global(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01001258 const struct proxy *defpx, const char *file, int line,
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02001259 char **err)
1260{
1261 return vars_max_size(args, section_type, curpx, defpx, file, line, err, &var_global_limit);
1262}
1263
Christopher Fauletff2613e2016-11-09 11:36:17 +01001264static int vars_max_size_proc(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01001265 const struct proxy *defpx, const char *file, int line,
Christopher Fauletff2613e2016-11-09 11:36:17 +01001266 char **err)
1267{
1268 return vars_max_size(args, section_type, curpx, defpx, file, line, err, &var_proc_limit);
1269}
1270
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02001271static int vars_max_size_sess(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01001272 const struct proxy *defpx, const char *file, int line,
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02001273 char **err)
1274{
1275 return vars_max_size(args, section_type, curpx, defpx, file, line, err, &var_sess_limit);
1276}
1277
1278static int vars_max_size_txn(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01001279 const struct proxy *defpx, const char *file, int line,
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02001280 char **err)
1281{
1282 return vars_max_size(args, section_type, curpx, defpx, file, line, err, &var_txn_limit);
1283}
1284
1285static int vars_max_size_reqres(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01001286 const struct proxy *defpx, const char *file, int line,
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02001287 char **err)
1288{
1289 return vars_max_size(args, section_type, curpx, defpx, file, line, err, &var_reqres_limit);
1290}
1291
Gaetan Rivet13a50432020-02-21 18:13:44 +01001292static int vars_max_size_check(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01001293 const struct proxy *defpx, const char *file, int line,
Gaetan Rivet13a50432020-02-21 18:13:44 +01001294 char **err)
1295{
1296 return vars_max_size(args, section_type, curpx, defpx, file, line, err, &var_check_limit);
1297}
1298
Willy Tarreau2c897d92021-08-31 08:48:55 +02001299/* early boot initialization */
1300static void vars_init()
1301{
1302 var_name_hash_seed = ha_random64();
1303}
1304
1305INITCALL0(STG_PREPARE, vars_init);
1306
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02001307static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
1308
Willy Tarreau44c5ff62021-11-02 17:08:15 +01001309 { "var", smp_fetch_var, ARG2(1,STR,STR), smp_check_var, SMP_T_ANY, SMP_USE_CONST },
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02001310 { /* END */ },
1311}};
1312
Willy Tarreau0108d902018-11-25 19:14:37 +01001313INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);
1314
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02001315static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Remi Tricot-Le Breton51899d22021-12-16 17:14:37 +01001316 { "set-var", smp_conv_store, ARG5(1,STR,STR,STR,STR,STR), conv_check_var, SMP_T_ANY, SMP_T_ANY },
Christopher Faulet85d79c92016-11-09 16:54:56 +01001317 { "unset-var", smp_conv_clear, ARG1(1,STR), conv_check_var, SMP_T_ANY, SMP_T_ANY },
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02001318 { /* END */ },
1319}};
1320
Willy Tarreau0108d902018-11-25 19:14:37 +01001321INITCALL1(STG_REGISTER, sample_register_convs, &sample_conv_kws);
1322
Jaroslaw Rzeszótkoc8637032021-11-02 16:56:05 +01001323static struct action_kw_list tcp_req_conn_kws = { { }, {
1324 { "set-var-fmt", parse_store, KWF_MATCH_PREFIX },
1325 { "set-var", parse_store, KWF_MATCH_PREFIX },
1326 { "unset-var", parse_store, KWF_MATCH_PREFIX },
1327 { /* END */ }
1328}};
1329
1330INITCALL1(STG_REGISTER, tcp_req_conn_keywords_register, &tcp_req_conn_kws);
1331
Willy Tarreau620408f2016-10-21 16:37:51 +02001332static struct action_kw_list tcp_req_sess_kws = { { }, {
Willy Tarreau9a621ae2021-09-02 21:00:38 +02001333 { "set-var-fmt", parse_store, KWF_MATCH_PREFIX },
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02001334 { "set-var", parse_store, KWF_MATCH_PREFIX },
1335 { "unset-var", parse_store, KWF_MATCH_PREFIX },
Willy Tarreau620408f2016-10-21 16:37:51 +02001336 { /* END */ }
1337}};
1338
Willy Tarreau0108d902018-11-25 19:14:37 +01001339INITCALL1(STG_REGISTER, tcp_req_sess_keywords_register, &tcp_req_sess_kws);
1340
Willy Tarreau620408f2016-10-21 16:37:51 +02001341static struct action_kw_list tcp_req_cont_kws = { { }, {
Willy Tarreau9a621ae2021-09-02 21:00:38 +02001342 { "set-var-fmt", parse_store, KWF_MATCH_PREFIX },
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02001343 { "set-var", parse_store, KWF_MATCH_PREFIX },
1344 { "unset-var", parse_store, KWF_MATCH_PREFIX },
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02001345 { /* END */ }
1346}};
1347
Willy Tarreau0108d902018-11-25 19:14:37 +01001348INITCALL1(STG_REGISTER, tcp_req_cont_keywords_register, &tcp_req_cont_kws);
1349
Thierry FOURNIER36481b82015-08-19 09:01:53 +02001350static struct action_kw_list tcp_res_kws = { { }, {
Willy Tarreau9a621ae2021-09-02 21:00:38 +02001351 { "set-var-fmt", parse_store, KWF_MATCH_PREFIX },
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02001352 { "set-var", parse_store, KWF_MATCH_PREFIX },
1353 { "unset-var", parse_store, KWF_MATCH_PREFIX },
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02001354 { /* END */ }
1355}};
1356
Willy Tarreau0108d902018-11-25 19:14:37 +01001357INITCALL1(STG_REGISTER, tcp_res_cont_keywords_register, &tcp_res_kws);
1358
Gaetan Rivet707b52f2020-02-21 18:14:59 +01001359static struct action_kw_list tcp_check_kws = {ILH, {
Willy Tarreau9a621ae2021-09-02 21:00:38 +02001360 { "set-var-fmt", parse_store, KWF_MATCH_PREFIX },
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02001361 { "set-var", parse_store, KWF_MATCH_PREFIX },
1362 { "unset-var", parse_store, KWF_MATCH_PREFIX },
Gaetan Rivet707b52f2020-02-21 18:14:59 +01001363 { /* END */ }
1364}};
1365
1366INITCALL1(STG_REGISTER, tcp_check_keywords_register, &tcp_check_kws);
1367
Thierry FOURNIER36481b82015-08-19 09:01:53 +02001368static struct action_kw_list http_req_kws = { { }, {
Willy Tarreau9a621ae2021-09-02 21:00:38 +02001369 { "set-var-fmt", parse_store, KWF_MATCH_PREFIX },
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02001370 { "set-var", parse_store, KWF_MATCH_PREFIX },
1371 { "unset-var", parse_store, KWF_MATCH_PREFIX },
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02001372 { /* END */ }
1373}};
1374
Willy Tarreau0108d902018-11-25 19:14:37 +01001375INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_kws);
1376
Thierry FOURNIER36481b82015-08-19 09:01:53 +02001377static struct action_kw_list http_res_kws = { { }, {
Willy Tarreau9a621ae2021-09-02 21:00:38 +02001378 { "set-var-fmt", parse_store, KWF_MATCH_PREFIX },
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02001379 { "set-var", parse_store, KWF_MATCH_PREFIX },
1380 { "unset-var", parse_store, KWF_MATCH_PREFIX },
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02001381 { /* END */ }
1382}};
1383
Willy Tarreau0108d902018-11-25 19:14:37 +01001384INITCALL1(STG_REGISTER, http_res_keywords_register, &http_res_kws);
1385
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01001386static struct action_kw_list http_after_res_kws = { { }, {
Willy Tarreau9a621ae2021-09-02 21:00:38 +02001387 { "set-var-fmt", parse_store, KWF_MATCH_PREFIX },
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02001388 { "set-var", parse_store, KWF_MATCH_PREFIX },
1389 { "unset-var", parse_store, KWF_MATCH_PREFIX },
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01001390 { /* END */ }
1391}};
1392
1393INITCALL1(STG_REGISTER, http_after_res_keywords_register, &http_after_res_kws);
1394
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02001395static struct cfg_kw_list cfg_kws = {{ },{
Willy Tarreau13d2ba22021-03-26 11:38:08 +01001396 { CFG_GLOBAL, "set-var", vars_parse_global_set_var },
Willy Tarreau753d4db2021-09-03 09:02:47 +02001397 { CFG_GLOBAL, "set-var-fmt", vars_parse_global_set_var },
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02001398 { CFG_GLOBAL, "tune.vars.global-max-size", vars_max_size_global },
Christopher Fauletff2613e2016-11-09 11:36:17 +01001399 { CFG_GLOBAL, "tune.vars.proc-max-size", vars_max_size_proc },
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02001400 { CFG_GLOBAL, "tune.vars.sess-max-size", vars_max_size_sess },
1401 { CFG_GLOBAL, "tune.vars.txn-max-size", vars_max_size_txn },
1402 { CFG_GLOBAL, "tune.vars.reqres-max-size", vars_max_size_reqres },
Gaetan Rivet13a50432020-02-21 18:13:44 +01001403 { CFG_GLOBAL, "tune.vars.check-max-size", vars_max_size_check },
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02001404 { /* END */ }
1405}};
1406
Willy Tarreau0108d902018-11-25 19:14:37 +01001407INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
Willy Tarreauc35eb382021-03-26 14:51:31 +01001408
1409
1410/* register cli keywords */
1411static struct cli_kw_list cli_kws = {{ },{
Willy Tarreaub205bfd2021-05-07 11:38:37 +02001412 { { "get", "var", NULL }, "get var <name> : retrieve contents of a process-wide variable", vars_parse_cli_get_var, NULL },
Willy Tarreaue93bff42021-09-03 09:47:37 +02001413 { { "set", "var", NULL }, "set var <name> [fmt|expr] {<fmt>|<expr>}: set variable from an expression or a format", vars_parse_cli_set_var, NULL, NULL, NULL, ACCESS_EXPERIMENTAL },
Willy Tarreauc35eb382021-03-26 14:51:31 +01001414 { { NULL }, NULL, NULL, NULL }
1415}};
1416INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);