blob: 1e8e1bd30c387485d8a09e63c72f59766e2f3f86 [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>
Thierry FOURNIER4834bc72015-06-06 19:29:07 +020021
Thierry FOURNIER4834bc72015-06-06 19:29:07 +020022
23/* This contains a pool of struct vars */
Willy Tarreau8ceae722018-11-26 11:58:30 +010024DECLARE_STATIC_POOL(var_pool, "vars", sizeof(struct var));
Thierry FOURNIER4834bc72015-06-06 19:29:07 +020025
Willy Tarreaucfc4f242021-05-08 11:41:28 +020026/* list of variables for the process scope. */
27struct vars proc_vars THREAD_ALIGNED(64);
28
Thierry FOURNIER4834bc72015-06-06 19:29:07 +020029/* This array contain all the names of all the HAProxy vars.
30 * This permits to identify two variables name with
31 * only one pointer. It permits to not using strdup() for
32 * each variable name used during the runtime.
33 */
34static char **var_names = NULL;
35static int var_names_nb = 0;
36
37/* This array of int contains the system limits per context. */
38static unsigned int var_global_limit = 0;
39static unsigned int var_global_size = 0;
Christopher Fauletff2613e2016-11-09 11:36:17 +010040static unsigned int var_proc_limit = 0;
Thierry FOURNIER4834bc72015-06-06 19:29:07 +020041static unsigned int var_sess_limit = 0;
42static unsigned int var_txn_limit = 0;
43static unsigned int var_reqres_limit = 0;
Gaetan Rivet13a50432020-02-21 18:13:44 +010044static unsigned int var_check_limit = 0;
Thierry FOURNIER4834bc72015-06-06 19:29:07 +020045
Willy Tarreau86abe442018-11-25 20:12:18 +010046__decl_rwlock(var_names_rwlock);
Christopher Faulete95f2c32017-07-24 16:30:34 +020047
Willy Tarreauf37b1402019-06-04 16:27:36 +020048/* returns the struct vars pointer for a session, stream and scope, or NULL if
49 * it does not exist.
50 */
51static inline struct vars *get_vars(struct session *sess, struct stream *strm, enum vars_scope scope)
52{
53 switch (scope) {
54 case SCOPE_PROC:
Willy Tarreaucfc4f242021-05-08 11:41:28 +020055 return &proc_vars;
Willy Tarreauf37b1402019-06-04 16:27:36 +020056 case SCOPE_SESS:
Willy Tarreaua07d61b2021-03-26 11:27:59 +010057 return sess ? &sess->vars : NULL;
Gaetan Rivet13a50432020-02-21 18:13:44 +010058 case SCOPE_CHECK: {
Christopher Fauletc4439f72021-06-02 11:48:42 +020059 struct check *check = sess ? objt_check(sess->origin) : NULL;
Gaetan Rivet13a50432020-02-21 18:13:44 +010060
Christopher Faulet0fca7ed2020-04-21 11:53:32 +020061 return check ? &check->vars : NULL;
Gaetan Rivet13a50432020-02-21 18:13:44 +010062 }
Willy Tarreauf37b1402019-06-04 16:27:36 +020063 case SCOPE_TXN:
64 return strm ? &strm->vars_txn : NULL;
65 case SCOPE_REQ:
66 case SCOPE_RES:
67 default:
68 return strm ? &strm->vars_reqres : NULL;
69 }
70}
71
Willy Tarreau72330982015-06-19 11:21:56 +020072/* This function adds or remove memory size from the accounting. The inner
73 * pointers may be null when setting the outer ones only.
74 */
Miroslav Zagorac6deab792020-12-09 16:34:29 +010075void var_accounting_diff(struct vars *vars, struct session *sess, struct stream *strm, int size)
Thierry FOURNIER4834bc72015-06-06 19:29:07 +020076{
77 switch (vars->scope) {
78 case SCOPE_REQ:
79 case SCOPE_RES:
Willy Tarreauf37b1402019-06-04 16:27:36 +020080 if (strm)
81 _HA_ATOMIC_ADD(&strm->vars_reqres.size, size);
Willy Tarreau6204cd92016-03-10 16:33:04 +010082 /* fall through */
Thierry FOURNIER4834bc72015-06-06 19:29:07 +020083 case SCOPE_TXN:
Willy Tarreauf37b1402019-06-04 16:27:36 +020084 if (strm)
85 _HA_ATOMIC_ADD(&strm->vars_txn.size, size);
Gaetan Rivet13a50432020-02-21 18:13:44 +010086 goto scope_sess;
87 case SCOPE_CHECK: {
Christopher Faulet0fca7ed2020-04-21 11:53:32 +020088 struct check *check = objt_check(sess->origin);
Gaetan Rivet13a50432020-02-21 18:13:44 +010089
Christopher Faulet0fca7ed2020-04-21 11:53:32 +020090 if (check)
91 _HA_ATOMIC_ADD(&check->vars.size, size);
Gaetan Rivet13a50432020-02-21 18:13:44 +010092 }
Willy Tarreau6204cd92016-03-10 16:33:04 +010093 /* fall through */
Gaetan Rivet13a50432020-02-21 18:13:44 +010094scope_sess:
Thierry FOURNIER4834bc72015-06-06 19:29:07 +020095 case SCOPE_SESS:
Olivier Houchard25ad13f2019-03-08 18:55:38 +010096 _HA_ATOMIC_ADD(&sess->vars.size, size);
Christopher Fauletff2613e2016-11-09 11:36:17 +010097 /* fall through */
98 case SCOPE_PROC:
Willy Tarreaucfc4f242021-05-08 11:41:28 +020099 _HA_ATOMIC_ADD(&proc_vars.size, size);
Olivier Houchard25ad13f2019-03-08 18:55:38 +0100100 _HA_ATOMIC_ADD(&var_global_size, size);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200101 }
102}
103
104/* This function returns 1 if the <size> is available in the var
Joseph Herlant07676892018-11-15 09:19:50 -0800105 * pool <vars>, otherwise returns 0. If the space is available,
Willy Tarreau72330982015-06-19 11:21:56 +0200106 * the size is reserved. The inner pointers may be null when setting
Willy Tarreau6204cd92016-03-10 16:33:04 +0100107 * the outer ones only. The accounting uses either <sess> or <strm>
108 * depending on the scope. <strm> may be NULL when no stream is known
109 * and only the session exists (eg: tcp-request connection).
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200110 */
Willy Tarreau6204cd92016-03-10 16:33:04 +0100111static int var_accounting_add(struct vars *vars, struct session *sess, struct stream *strm, int size)
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200112{
113 switch (vars->scope) {
114 case SCOPE_REQ:
115 case SCOPE_RES:
Willy Tarreauf37b1402019-06-04 16:27:36 +0200116 if (var_reqres_limit && strm && strm->vars_reqres.size + size > var_reqres_limit)
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200117 return 0;
Willy Tarreau6204cd92016-03-10 16:33:04 +0100118 /* fall through */
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200119 case SCOPE_TXN:
Willy Tarreauf37b1402019-06-04 16:27:36 +0200120 if (var_txn_limit && strm && strm->vars_txn.size + size > var_txn_limit)
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200121 return 0;
Gaetan Rivet13a50432020-02-21 18:13:44 +0100122 goto scope_sess;
123 case SCOPE_CHECK: {
Christopher Faulet0fca7ed2020-04-21 11:53:32 +0200124 struct check *check = objt_check(sess->origin);
Gaetan Rivet13a50432020-02-21 18:13:44 +0100125
Christopher Faulet0fca7ed2020-04-21 11:53:32 +0200126 if (var_check_limit && check && check->vars.size + size > var_check_limit)
Gaetan Rivet13a50432020-02-21 18:13:44 +0100127 return 0;
128 }
Willy Tarreau6204cd92016-03-10 16:33:04 +0100129 /* fall through */
Gaetan Rivet13a50432020-02-21 18:13:44 +0100130scope_sess:
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200131 case SCOPE_SESS:
Willy Tarreau6204cd92016-03-10 16:33:04 +0100132 if (var_sess_limit && sess->vars.size + size > var_sess_limit)
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200133 return 0;
Christopher Fauletff2613e2016-11-09 11:36:17 +0100134 /* fall through */
135 case SCOPE_PROC:
Willy Tarreaucfc4f242021-05-08 11:41:28 +0200136 if (var_proc_limit && proc_vars.size + size > var_proc_limit)
Christopher Fauletff2613e2016-11-09 11:36:17 +0100137 return 0;
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200138 if (var_global_limit && var_global_size + size > var_global_limit)
139 return 0;
140 }
Willy Tarreau6204cd92016-03-10 16:33:04 +0100141 var_accounting_diff(vars, sess, strm, size);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200142 return 1;
143}
144
Christopher Faulet85d79c92016-11-09 16:54:56 +0100145/* This fnuction remove a variable from the list and free memory it used */
146unsigned int var_clear(struct var *var)
147{
148 unsigned int size = 0;
149
150 if (var->data.type == SMP_T_STR || var->data.type == SMP_T_BIN) {
Willy Tarreau5b52b002021-02-26 21:19:53 +0100151 ha_free(&var->data.u.str.area);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200152 size += var->data.u.str.data;
Christopher Faulet85d79c92016-11-09 16:54:56 +0100153 }
Christopher Fauletd02210c2017-07-24 16:24:39 +0200154 else if (var->data.type == SMP_T_METH && var->data.u.meth.meth == HTTP_METH_OTHER) {
Willy Tarreau5b52b002021-02-26 21:19:53 +0100155 ha_free(&var->data.u.meth.str.area);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200156 size += var->data.u.meth.str.data;
Christopher Faulet85d79c92016-11-09 16:54:56 +0100157 }
Willy Tarreau2b718102021-04-21 07:32:39 +0200158 LIST_DELETE(&var->l);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100159 pool_free(var_pool, var);
Christopher Faulet85d79c92016-11-09 16:54:56 +0100160 size += sizeof(struct var);
161 return size;
162}
163
Joseph Herlant07676892018-11-15 09:19:50 -0800164/* This function free all the memory used by all the variables
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200165 * in the list.
166 */
Willy Tarreau6204cd92016-03-10 16:33:04 +0100167void vars_prune(struct vars *vars, struct session *sess, struct stream *strm)
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200168{
169 struct var *var, *tmp;
Willy Tarreau72330982015-06-19 11:21:56 +0200170 unsigned int size = 0;
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200171
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100172 HA_RWLOCK_WRLOCK(VARS_LOCK, &vars->rwlock);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200173 list_for_each_entry_safe(var, tmp, &vars->head, l) {
Christopher Faulet85d79c92016-11-09 16:54:56 +0100174 size += var_clear(var);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200175 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100176 HA_RWLOCK_WRUNLOCK(VARS_LOCK, &vars->rwlock);
Willy Tarreau6204cd92016-03-10 16:33:04 +0100177 var_accounting_diff(vars, sess, strm, -size);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200178}
179
Willy Tarreauebcd4842015-06-19 11:59:02 +0200180/* This function frees all the memory used by all the session variables in the
181 * list starting at <vars>.
182 */
183void vars_prune_per_sess(struct vars *vars)
184{
185 struct var *var, *tmp;
186 unsigned int size = 0;
187
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100188 HA_RWLOCK_WRLOCK(VARS_LOCK, &vars->rwlock);
Willy Tarreauebcd4842015-06-19 11:59:02 +0200189 list_for_each_entry_safe(var, tmp, &vars->head, l) {
Christopher Faulet85d79c92016-11-09 16:54:56 +0100190 size += var_clear(var);
Willy Tarreauebcd4842015-06-19 11:59:02 +0200191 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100192 HA_RWLOCK_WRUNLOCK(VARS_LOCK, &vars->rwlock);
Christopher Faulete95f2c32017-07-24 16:30:34 +0200193
Olivier Houchard25ad13f2019-03-08 18:55:38 +0100194 _HA_ATOMIC_SUB(&vars->size, size);
Willy Tarreaucfc4f242021-05-08 11:41:28 +0200195 _HA_ATOMIC_SUB(&proc_vars.size, size);
Olivier Houchard25ad13f2019-03-08 18:55:38 +0100196 _HA_ATOMIC_SUB(&var_global_size, size);
Willy Tarreauebcd4842015-06-19 11:59:02 +0200197}
198
Ilya Shipitsin46a030c2020-07-05 16:36:08 +0500199/* This function init a list of variables. */
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200200void vars_init(struct vars *vars, enum vars_scope scope)
201{
202 LIST_INIT(&vars->head);
203 vars->scope = scope;
204 vars->size = 0;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100205 HA_RWLOCK_INIT(&vars->rwlock);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200206}
207
208/* This function declares a new variable name. It returns a pointer
209 * on the string identifying the name. This function assures that
210 * the same name exists only once.
211 *
212 * This function check if the variable name is acceptable.
213 *
214 * The function returns NULL if an error occurs, and <err> is filled.
215 * In this case, the HAProxy must be stopped because the structs are
216 * left inconsistent. Otherwise, it returns the pointer on the global
217 * name.
218 */
Christopher Faulet09c9df22016-10-31 11:05:37 +0100219static char *register_name(const char *name, int len, enum vars_scope *scope,
220 int alloc, char **err)
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200221{
222 int i;
Hubert Verstraete831962e2016-06-28 22:44:26 +0200223 char **var_names2;
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200224 const char *tmp;
Christopher Faulete95f2c32017-07-24 16:30:34 +0200225 char *res = NULL;
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200226
227 /* Check length. */
228 if (len == 0) {
229 memprintf(err, "Empty variable name cannot be accepted");
Christopher Fauleteb3e2762017-12-08 09:17:39 +0100230 return res;
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200231 }
232
233 /* Check scope. */
Christopher Fauletff2613e2016-11-09 11:36:17 +0100234 if (len > 5 && strncmp(name, "proc.", 5) == 0) {
235 name += 5;
236 len -= 5;
237 *scope = SCOPE_PROC;
238 }
239 else if (len > 5 && strncmp(name, "sess.", 5) == 0) {
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200240 name += 5;
241 len -= 5;
242 *scope = SCOPE_SESS;
243 }
244 else if (len > 4 && strncmp(name, "txn.", 4) == 0) {
245 name += 4;
246 len -= 4;
247 *scope = SCOPE_TXN;
248 }
249 else if (len > 4 && strncmp(name, "req.", 4) == 0) {
250 name += 4;
251 len -= 4;
252 *scope = SCOPE_REQ;
253 }
254 else if (len > 4 && strncmp(name, "res.", 4) == 0) {
255 name += 4;
256 len -= 4;
257 *scope = SCOPE_RES;
258 }
Gaetan Rivet13a50432020-02-21 18:13:44 +0100259 else if (len > 6 && strncmp(name, "check.", 6) == 0) {
260 name += 6;
261 len -= 6;
262 *scope = SCOPE_CHECK;
263 }
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200264 else {
Willy Tarreau1402fef2021-09-03 10:12:55 +0200265 memprintf(err, "invalid variable name '%.*s'. A variable name must be start by its scope. "
266 "The scope can be 'proc', 'sess', 'txn', 'req', 'res' or 'check'", len, name);
Christopher Fauleteb3e2762017-12-08 09:17:39 +0100267 return res;
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200268 }
269
Christopher Faulete95f2c32017-07-24 16:30:34 +0200270 if (alloc)
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100271 HA_RWLOCK_WRLOCK(VARS_LOCK, &var_names_rwlock);
Christopher Faulete95f2c32017-07-24 16:30:34 +0200272 else
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100273 HA_RWLOCK_RDLOCK(VARS_LOCK, &var_names_rwlock);
Christopher Faulete95f2c32017-07-24 16:30:34 +0200274
275
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200276 /* Look for existing variable name. */
277 for (i = 0; i < var_names_nb; i++)
Christopher Faulete95f2c32017-07-24 16:30:34 +0200278 if (strncmp(var_names[i], name, len) == 0 && var_names[i][len] == '\0') {
279 res = var_names[i];
280 goto end;
281 }
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200282
Christopher Faulete95f2c32017-07-24 16:30:34 +0200283 if (!alloc) {
284 res = NULL;
285 goto end;
286 }
Christopher Faulet09c9df22016-10-31 11:05:37 +0100287
Hubert Verstraete831962e2016-06-28 22:44:26 +0200288 /* Store variable name. If realloc fails, var_names remains valid */
289 var_names2 = realloc(var_names, (var_names_nb + 1) * sizeof(*var_names));
290 if (!var_names2) {
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200291 memprintf(err, "out of memory error");
Christopher Faulete95f2c32017-07-24 16:30:34 +0200292 res = NULL;
293 goto end;
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200294 }
Hubert Verstraete831962e2016-06-28 22:44:26 +0200295 var_names_nb++;
296 var_names = var_names2;
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200297 var_names[var_names_nb - 1] = malloc(len + 1);
298 if (!var_names[var_names_nb - 1]) {
299 memprintf(err, "out of memory error");
Christopher Faulete95f2c32017-07-24 16:30:34 +0200300 res = NULL;
301 goto end;
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200302 }
303 memcpy(var_names[var_names_nb - 1], name, len);
304 var_names[var_names_nb - 1][len] = '\0';
305
306 /* Check variable name syntax. */
307 tmp = var_names[var_names_nb - 1];
308 while (*tmp) {
Willy Tarreau90807112020-02-25 08:16:33 +0100309 if (!isalnum((unsigned char)*tmp) && *tmp != '_' && *tmp != '.') {
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200310 memprintf(err, "invalid syntax at char '%s'", tmp);
Christopher Faulete95f2c32017-07-24 16:30:34 +0200311 res = NULL;
312 goto end;
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200313 }
314 tmp++;
315 }
Christopher Faulete95f2c32017-07-24 16:30:34 +0200316 res = var_names[var_names_nb - 1];
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200317
Christopher Faulete95f2c32017-07-24 16:30:34 +0200318 end:
319 if (alloc)
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100320 HA_RWLOCK_WRUNLOCK(VARS_LOCK, &var_names_rwlock);
Christopher Faulete95f2c32017-07-24 16:30:34 +0200321 else
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100322 HA_RWLOCK_RDUNLOCK(VARS_LOCK, &var_names_rwlock);
Christopher Faulete95f2c32017-07-24 16:30:34 +0200323
324 return res;
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200325}
326
327/* This function returns an existing variable or returns NULL. */
328static inline struct var *var_get(struct vars *vars, const char *name)
329{
330 struct var *var;
331
332 list_for_each_entry(var, &vars->head, l)
333 if (var->name == name)
334 return var;
335 return NULL;
336}
337
338/* Returns 0 if fails, else returns 1. */
339static int smp_fetch_var(const struct arg *args, struct sample *smp, const char *kw, void *private)
340{
341 const struct var_desc *var_desc = &args[0].data.var;
Willy Tarreau54496a62021-09-03 12:00:13 +0200342 const struct buffer *def = NULL;
Christopher Faulete95f2c32017-07-24 16:30:34 +0200343
Willy Tarreau54496a62021-09-03 12:00:13 +0200344 if (args[1].type == ARGT_STR)
345 def = &args[1].data.str;
346
347 return vars_get_by_desc(var_desc, smp, def);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200348}
349
350/* This function search in the <head> a variable with the same
351 * pointer value that the <name>. If the variable doesn't exists,
352 * create it. The function stores a copy of smp> if the variable.
353 * It returns 0 if fails, else returns 1.
354 */
Willy Tarreau6204cd92016-03-10 16:33:04 +0100355static int sample_store(struct vars *vars, const char *name, struct sample *smp)
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200356{
357 struct var *var;
358
359 /* Look for existing variable name. */
360 var = var_get(vars, name);
361
362 if (var) {
363 /* free its used memory. */
364 if (var->data.type == SMP_T_STR ||
365 var->data.type == SMP_T_BIN) {
Willy Tarreau5b52b002021-02-26 21:19:53 +0100366 ha_free(&var->data.u.str.area);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200367 var_accounting_diff(vars, smp->sess, smp->strm,
368 -var->data.u.str.data);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200369 }
Christopher Fauletd02210c2017-07-24 16:24:39 +0200370 else if (var->data.type == SMP_T_METH && var->data.u.meth.meth == HTTP_METH_OTHER) {
Willy Tarreau5b52b002021-02-26 21:19:53 +0100371 ha_free(&var->data.u.meth.str.area);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200372 var_accounting_diff(vars, smp->sess, smp->strm,
373 -var->data.u.meth.str.data);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200374 }
375 } else {
376
Joseph Herlant07676892018-11-15 09:19:50 -0800377 /* Check memory available. */
Willy Tarreau6204cd92016-03-10 16:33:04 +0100378 if (!var_accounting_add(vars, smp->sess, smp->strm, sizeof(struct var)))
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200379 return 0;
380
381 /* Create new entry. */
Willy Tarreaubafbe012017-11-24 17:34:44 +0100382 var = pool_alloc(var_pool);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200383 if (!var)
384 return 0;
Willy Tarreau2b718102021-04-21 07:32:39 +0200385 LIST_APPEND(&vars->head, &var->l);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200386 var->name = name;
387 }
388
389 /* Set type. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200390 var->data.type = smp->data.type;
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200391
392 /* Copy data. If the data needs memory, the function can fail. */
393 switch (var->data.type) {
394 case SMP_T_BOOL:
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200395 case SMP_T_SINT:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200396 var->data.u.sint = smp->data.u.sint;
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200397 break;
398 case SMP_T_IPV4:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200399 var->data.u.ipv4 = smp->data.u.ipv4;
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200400 break;
401 case SMP_T_IPV6:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200402 var->data.u.ipv6 = smp->data.u.ipv6;
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200403 break;
404 case SMP_T_STR:
405 case SMP_T_BIN:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200406 if (!var_accounting_add(vars, smp->sess, smp->strm, smp->data.u.str.data)) {
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200407 var->data.type = SMP_T_BOOL; /* This type doesn't use additional memory. */
408 return 0;
409 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200410 var->data.u.str.area = malloc(smp->data.u.str.data);
411 if (!var->data.u.str.area) {
412 var_accounting_diff(vars, smp->sess, smp->strm,
413 -smp->data.u.str.data);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200414 var->data.type = SMP_T_BOOL; /* This type doesn't use additional memory. */
415 return 0;
416 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200417 var->data.u.str.data = smp->data.u.str.data;
418 memcpy(var->data.u.str.area, smp->data.u.str.area,
419 var->data.u.str.data);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200420 break;
421 case SMP_T_METH:
Christopher Fauletd02210c2017-07-24 16:24:39 +0200422 var->data.u.meth.meth = smp->data.u.meth.meth;
423 if (smp->data.u.meth.meth != HTTP_METH_OTHER)
424 break;
425
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200426 if (!var_accounting_add(vars, smp->sess, smp->strm, smp->data.u.meth.str.data)) {
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200427 var->data.type = SMP_T_BOOL; /* This type doesn't use additional memory. */
428 return 0;
429 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200430 var->data.u.meth.str.area = malloc(smp->data.u.meth.str.data);
431 if (!var->data.u.meth.str.area) {
432 var_accounting_diff(vars, smp->sess, smp->strm,
433 -smp->data.u.meth.str.data);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200434 var->data.type = SMP_T_BOOL; /* This type doesn't use additional memory. */
435 return 0;
436 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200437 var->data.u.meth.str.data = smp->data.u.meth.str.data;
438 var->data.u.meth.str.size = smp->data.u.meth.str.data;
439 memcpy(var->data.u.meth.str.area, smp->data.u.meth.str.area,
440 var->data.u.meth.str.data);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200441 break;
442 }
443 return 1;
444}
445
Willy Tarreau620408f2016-10-21 16:37:51 +0200446/* Returns 0 if fails, else returns 1. Note that stream may be null for SCOPE_SESS. */
Willy Tarreau6204cd92016-03-10 16:33:04 +0100447static inline int sample_store_stream(const char *name, enum vars_scope scope, struct sample *smp)
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200448{
449 struct vars *vars;
Christopher Faulete95f2c32017-07-24 16:30:34 +0200450 int ret;
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200451
Willy Tarreauf37b1402019-06-04 16:27:36 +0200452 vars = get_vars(smp->sess, smp->strm, scope);
453 if (!vars || vars->scope != scope)
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200454 return 0;
Christopher Faulete95f2c32017-07-24 16:30:34 +0200455
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100456 HA_RWLOCK_WRLOCK(VARS_LOCK, &vars->rwlock);
Christopher Faulete95f2c32017-07-24 16:30:34 +0200457 ret = sample_store(vars, name, smp);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100458 HA_RWLOCK_WRUNLOCK(VARS_LOCK, &vars->rwlock);
Christopher Faulete95f2c32017-07-24 16:30:34 +0200459 return ret;
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200460}
461
Christopher Faulet85d79c92016-11-09 16:54:56 +0100462/* Returns 0 if fails, else returns 1. Note that stream may be null for SCOPE_SESS. */
463static inline int sample_clear_stream(const char *name, enum vars_scope scope, struct sample *smp)
464{
465 struct vars *vars;
466 struct var *var;
467 unsigned int size = 0;
468
Willy Tarreauf37b1402019-06-04 16:27:36 +0200469 vars = get_vars(smp->sess, smp->strm, scope);
470 if (!vars || vars->scope != scope)
Christopher Faulet85d79c92016-11-09 16:54:56 +0100471 return 0;
472
473 /* Look for existing variable name. */
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100474 HA_RWLOCK_WRLOCK(VARS_LOCK, &vars->rwlock);
Christopher Faulet85d79c92016-11-09 16:54:56 +0100475 var = var_get(vars, name);
476 if (var) {
477 size = var_clear(var);
478 var_accounting_diff(vars, smp->sess, smp->strm, -size);
479 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100480 HA_RWLOCK_WRUNLOCK(VARS_LOCK, &vars->rwlock);
Christopher Faulet85d79c92016-11-09 16:54:56 +0100481 return 1;
482}
483
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200484/* Returns 0 if fails, else returns 1. */
485static int smp_conv_store(const struct arg *args, struct sample *smp, void *private)
486{
Christopher Faulet0099a8c2016-11-09 16:15:32 +0100487 return sample_store_stream(args[0].data.var.name, args[0].data.var.scope, smp);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200488}
489
Christopher Faulet85d79c92016-11-09 16:54:56 +0100490/* Returns 0 if fails, else returns 1. */
491static int smp_conv_clear(const struct arg *args, struct sample *smp, void *private)
492{
493 return sample_clear_stream(args[0].data.var.name, args[0].data.var.scope, smp);
494}
495
Joseph Herlant07676892018-11-15 09:19:50 -0800496/* This functions check an argument entry and fill it with a variable
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200497 * type. The argumen must be a string. If the variable lookup fails,
Joseph Herlant07676892018-11-15 09:19:50 -0800498 * the function returns 0 and fill <err>, otherwise it returns 1.
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200499 */
500int vars_check_arg(struct arg *arg, char **err)
501{
502 char *name;
503 enum vars_scope scope;
504
505 /* Check arg type. */
506 if (arg->type != ARGT_STR) {
507 memprintf(err, "unexpected argument type");
508 return 0;
509 }
510
511 /* Register new variable name. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200512 name = register_name(arg->data.str.area, arg->data.str.data, &scope,
513 1,
514 err);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200515 if (!name)
516 return 0;
517
Tim Duesterhusa6cc7e82019-05-13 10:53:29 +0200518 /* properly destroy the chunk */
519 chunk_destroy(&arg->data.str);
520
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200521 /* Use the global variable name pointer. */
522 arg->type = ARGT_VAR;
523 arg->data.var.name = name;
524 arg->data.var.scope = scope;
525 return 1;
526}
527
Christopher Faulet09c9df22016-10-31 11:05:37 +0100528/* This function store a sample in a variable if it was already defined.
Tim Duesterhusb4fac1e2020-05-19 13:49:40 +0200529 * Returns zero on failure and non-zero otherwise. The variable not being
530 * defined is treated as a failure.
Christopher Faulet09c9df22016-10-31 11:05:37 +0100531 */
Tim Duesterhusb4fac1e2020-05-19 13:49:40 +0200532int vars_set_by_name_ifexist(const char *name, size_t len, struct sample *smp)
Christopher Faulet09c9df22016-10-31 11:05:37 +0100533{
534 enum vars_scope scope;
535
536 /* Resolve name and scope. */
537 name = register_name(name, len, &scope, 0, NULL);
538 if (!name)
Tim Duesterhusb4fac1e2020-05-19 13:49:40 +0200539 return 0;
Christopher Faulet09c9df22016-10-31 11:05:37 +0100540
Tim Duesterhusb4fac1e2020-05-19 13:49:40 +0200541 return sample_store_stream(name, scope, smp);
Christopher Faulet09c9df22016-10-31 11:05:37 +0100542}
543
544
Thierry FOURNIERc365d992015-06-09 12:27:17 +0200545/* This function store a sample in a variable.
Tim Duesterhusb4fac1e2020-05-19 13:49:40 +0200546 * Returns zero on failure and non-zero otherwise.
Thierry FOURNIERc365d992015-06-09 12:27:17 +0200547 */
Tim Duesterhusb4fac1e2020-05-19 13:49:40 +0200548int vars_set_by_name(const char *name, size_t len, struct sample *smp)
Thierry FOURNIERc365d992015-06-09 12:27:17 +0200549{
550 enum vars_scope scope;
551
552 /* Resolve name and scope. */
Christopher Faulet09c9df22016-10-31 11:05:37 +0100553 name = register_name(name, len, &scope, 1, NULL);
Thierry FOURNIERc365d992015-06-09 12:27:17 +0200554 if (!name)
Tim Duesterhusb4fac1e2020-05-19 13:49:40 +0200555 return 0;
Thierry FOURNIERc365d992015-06-09 12:27:17 +0200556
Tim Duesterhusb4fac1e2020-05-19 13:49:40 +0200557 return sample_store_stream(name, scope, smp);
Thierry FOURNIERc365d992015-06-09 12:27:17 +0200558}
559
Christopher Faulet85d79c92016-11-09 16:54:56 +0100560/* This function unset a variable if it was already defined.
Tim Duesterhusb4fac1e2020-05-19 13:49:40 +0200561 * Returns zero on failure and non-zero otherwise.
Christopher Faulet85d79c92016-11-09 16:54:56 +0100562 */
Tim Duesterhusb4fac1e2020-05-19 13:49:40 +0200563int vars_unset_by_name_ifexist(const char *name, size_t len, struct sample *smp)
Christopher Faulet85d79c92016-11-09 16:54:56 +0100564{
565 enum vars_scope scope;
566
567 /* Resolve name and scope. */
568 name = register_name(name, len, &scope, 0, NULL);
569 if (!name)
Tim Duesterhusb4fac1e2020-05-19 13:49:40 +0200570 return 0;
Christopher Faulet85d79c92016-11-09 16:54:56 +0100571
Tim Duesterhusb4fac1e2020-05-19 13:49:40 +0200572 return sample_clear_stream(name, scope, smp);
Christopher Faulet85d79c92016-11-09 16:54:56 +0100573}
574
575
Willy Tarreaube7e00d2021-09-03 11:40:58 +0200576/* This retrieves variable <name> from variables <vars>, and if found,
577 * duplicates the result into sample <smp>. smp_dup() is used in order to
578 * release the variables lock ASAP (so a pre-allocated chunk is obtained
579 * via get_trash_shunk()). The variables' lock is used for reads.
580 *
Willy Tarreaue352b9d2021-09-03 11:52:38 +0200581 * The function returns 0 if the variable was not found and no default
582 * value was provided in <def>, otherwise 1 with the sample filled.
583 * Default values are always returned as strings.
Willy Tarreaube7e00d2021-09-03 11:40:58 +0200584 */
Willy Tarreaue352b9d2021-09-03 11:52:38 +0200585static int var_to_smp(struct vars *vars, const char *name, struct sample *smp, const struct buffer *def)
Willy Tarreaube7e00d2021-09-03 11:40:58 +0200586{
587 struct var *var;
588
589 /* Get the variable entry. */
590 HA_RWLOCK_RDLOCK(VARS_LOCK, &vars->rwlock);
591 var = var_get(vars, name);
592 if (!var) {
Willy Tarreaue352b9d2021-09-03 11:52:38 +0200593 if (!def) {
594 HA_RWLOCK_RDUNLOCK(VARS_LOCK, &vars->rwlock);
595 return 0;
596 }
597
598 /* not found but we have a default value */
599 smp->data.type = SMP_T_STR;
600 smp->data.u.str = *def;
Willy Tarreaube7e00d2021-09-03 11:40:58 +0200601 }
Willy Tarreaue352b9d2021-09-03 11:52:38 +0200602 else
603 smp->data = var->data;
Willy Tarreaube7e00d2021-09-03 11:40:58 +0200604
605 /* Copy sample. */
Willy Tarreaube7e00d2021-09-03 11:40:58 +0200606 smp_dup(smp);
607
608 HA_RWLOCK_RDUNLOCK(VARS_LOCK, &vars->rwlock);
609 return 1;
610}
611
Dragan Dosen14518f22021-02-22 17:20:01 +0100612/* This function fills a sample with the variable content.
613 *
614 * Keep in mind that a sample content is duplicated by using smp_dup()
615 * and it therefore uses a pre-allocated trash chunk as returned by
616 * get_trash_chunk().
617 *
Willy Tarreaue352b9d2021-09-03 11:52:38 +0200618 * If the variable is not valid in this scope, 0 is always returned.
619 * If the variable is valid but not found, either the default value
620 * <def> is returned if not NULL, or zero is returned.
621 *
Dragan Dosen14518f22021-02-22 17:20:01 +0100622 * Returns 1 if the sample is filled, otherwise it returns 0.
Thierry FOURNIERc365d992015-06-09 12:27:17 +0200623 */
Willy Tarreaue352b9d2021-09-03 11:52:38 +0200624int vars_get_by_name(const char *name, size_t len, struct sample *smp, const struct buffer *def)
Thierry FOURNIERc365d992015-06-09 12:27:17 +0200625{
626 struct vars *vars;
Thierry FOURNIERc365d992015-06-09 12:27:17 +0200627 enum vars_scope scope;
628
629 /* Resolve name and scope. */
Willy Tarreau89f6ded2021-05-13 13:30:12 +0200630 name = register_name(name, len, &scope, 0, NULL);
Thierry FOURNIERc365d992015-06-09 12:27:17 +0200631 if (!name)
632 return 0;
633
634 /* Select "vars" pool according with the scope. */
Willy Tarreauf37b1402019-06-04 16:27:36 +0200635 vars = get_vars(smp->sess, smp->strm, scope);
636 if (!vars || vars->scope != scope)
Thierry FOURNIERc365d992015-06-09 12:27:17 +0200637 return 0;
Thierry FOURNIERc365d992015-06-09 12:27:17 +0200638
Willy Tarreaue352b9d2021-09-03 11:52:38 +0200639
640 return var_to_smp(vars, name, smp, def);
Thierry FOURNIERc365d992015-06-09 12:27:17 +0200641}
642
Dragan Dosen14518f22021-02-22 17:20:01 +0100643/* This function fills a sample with the content of the variable described
644 * by <var_desc>.
645 *
646 * Keep in mind that a sample content is duplicated by using smp_dup()
647 * and it therefore uses a pre-allocated trash chunk as returned by
648 * get_trash_chunk().
649 *
Willy Tarreaue352b9d2021-09-03 11:52:38 +0200650 * If the variable is not valid in this scope, 0 is always returned.
651 * If the variable is valid but not found, either the default value
652 * <def> is returned if not NULL, or zero is returned.
653 *
Dragan Dosen14518f22021-02-22 17:20:01 +0100654 * Returns 1 if the sample is filled, otherwise it returns 0.
Thierry FOURNIERfd77e052015-07-07 21:20:42 +0200655 */
Willy Tarreaue352b9d2021-09-03 11:52:38 +0200656int vars_get_by_desc(const struct var_desc *var_desc, struct sample *smp, const struct buffer *def)
Thierry FOURNIERfd77e052015-07-07 21:20:42 +0200657{
658 struct vars *vars;
Thierry FOURNIERfd77e052015-07-07 21:20:42 +0200659
660 /* Select "vars" pool according with the scope. */
Willy Tarreauf37b1402019-06-04 16:27:36 +0200661 vars = get_vars(smp->sess, smp->strm, var_desc->scope);
Thierry FOURNIERfd77e052015-07-07 21:20:42 +0200662
Joseph Herlant07676892018-11-15 09:19:50 -0800663 /* Check if the scope is available a this point of processing. */
Willy Tarreauf37b1402019-06-04 16:27:36 +0200664 if (!vars || vars->scope != var_desc->scope)
Thierry FOURNIERfd77e052015-07-07 21:20:42 +0200665 return 0;
666
Willy Tarreaue352b9d2021-09-03 11:52:38 +0200667 return var_to_smp(vars, var_desc->name, smp, def);
Thierry FOURNIERfd77e052015-07-07 21:20:42 +0200668}
669
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +0200670/* Always returns ACT_RET_CONT even if an error occurs. */
671static enum act_return action_store(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +0200672 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200673{
Willy Tarreau9a621ae2021-09-02 21:00:38 +0200674 struct buffer *fmtstr = NULL;
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200675 struct sample smp;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +0200676 int dir;
677
678 switch (rule->from) {
Willy Tarreau620408f2016-10-21 16:37:51 +0200679 case ACT_F_TCP_REQ_SES: dir = SMP_OPT_DIR_REQ; break;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +0200680 case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break;
681 case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break;
682 case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break;
683 case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break;
Gaetan Rivet0c39ecc2020-02-24 17:34:11 +0100684 case ACT_F_TCP_CHK: dir = SMP_OPT_DIR_REQ; break;
Willy Tarreau01d580a2021-03-26 11:11:34 +0100685 case ACT_F_CFG_PARSER: dir = SMP_OPT_DIR_REQ; break; /* not used anyway */
Willy Tarreau2f836de2021-03-26 15:36:44 +0100686 case ACT_F_CLI_PARSER: dir = SMP_OPT_DIR_REQ; break; /* not used anyway */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +0200687 default:
688 send_log(px, LOG_ERR, "Vars: internal error while execute action store.");
689 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))
Christopher Faulet767a84b2017-11-24 16:50:31 +0100690 ha_alert("Vars: internal error while execute action store.\n");
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +0200691 return ACT_RET_CONT;
692 }
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200693
694 /* Process the expression. */
695 memset(&smp, 0, sizeof(smp));
Willy Tarreau9a621ae2021-09-02 21:00:38 +0200696
697 if (!LIST_ISEMPTY(&rule->arg.vars.fmt)) {
698 /* a format-string is used */
699
700 fmtstr = alloc_trash_chunk();
701 if (!fmtstr) {
702 send_log(px, LOG_ERR, "Vars: memory allocation failure while processing store rule.");
703 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))
704 ha_alert("Vars: memory allocation failure while processing store rule.\n");
705 return ACT_RET_CONT;
706 }
707
708 /* execute the log-format expression */
709 fmtstr->data = sess_build_logline(sess, s, fmtstr->area, fmtstr->size, &rule->arg.vars.fmt);
710
711 /* convert it to a sample of type string as it's what the vars
712 * API consumes, and store it.
713 */
714 smp_set_owner(&smp, px, sess, s, 0);
715 smp.data.type = SMP_T_STR;
716 smp.data.u.str = *fmtstr;
717 sample_store_stream(rule->arg.vars.name, rule->arg.vars.scope, &smp);
718 }
719 else {
720 /* an expression is used */
721 if (!sample_process(px, sess, s, dir|SMP_OPT_FINAL,
722 rule->arg.vars.expr, &smp))
723 return ACT_RET_CONT;
724 }
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200725
726 /* Store the sample, and ignore errors. */
Willy Tarreau6204cd92016-03-10 16:33:04 +0100727 sample_store_stream(rule->arg.vars.name, rule->arg.vars.scope, &smp);
Willy Tarreau9a621ae2021-09-02 21:00:38 +0200728 free_trash_chunk(fmtstr);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +0200729 return ACT_RET_CONT;
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200730}
731
Christopher Faulet85d79c92016-11-09 16:54:56 +0100732/* Always returns ACT_RET_CONT even if an error occurs. */
733static enum act_return action_clear(struct act_rule *rule, struct proxy *px,
734 struct session *sess, struct stream *s, int flags)
735{
736 struct sample smp;
737
738 memset(&smp, 0, sizeof(smp));
739 smp_set_owner(&smp, px, sess, s, SMP_OPT_FINAL);
740
741 /* Clear the variable using the sample context, and ignore errors. */
742 sample_clear_stream(rule->arg.vars.name, rule->arg.vars.scope, &smp);
743 return ACT_RET_CONT;
744}
745
Tim Duesterhus01a0ce32020-06-14 17:27:36 +0200746static void release_store_rule(struct act_rule *rule)
747{
Willy Tarreau9a621ae2021-09-02 21:00:38 +0200748 struct logformat_node *lf, *lfb;
Willy Tarreauc77bad22021-09-03 10:58:07 +0200749
750 list_for_each_entry_safe(lf, lfb, &rule->arg.vars.fmt, list) {
Willy Tarreau9a621ae2021-09-02 21:00:38 +0200751 LIST_DELETE(&lf->list);
752 release_sample_expr(lf->expr);
753 free(lf->arg);
754 free(lf);
755 }
756
Tim Duesterhus01a0ce32020-06-14 17:27:36 +0200757 release_sample_expr(rule->arg.vars.expr);
758}
759
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200760/* This two function checks the variable name and replace the
761 * configuration string name by the global string name. its
762 * the same string, but the global pointer can be easy to
Willy Tarreaue352b9d2021-09-03 11:52:38 +0200763 * compare. They return non-zero on success, zero on failure.
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200764 *
765 * The first function checks a sample-fetch and the second
766 * checks a converter.
767 */
768static int smp_check_var(struct arg *args, char **err)
769{
770 return vars_check_arg(&args[0], err);
771}
772
773static int conv_check_var(struct arg *args, struct sample_conv *conv,
774 const char *file, int line, char **err_msg)
775{
776 return vars_check_arg(&args[0], err_msg);
777}
778
779/* This function is a common parser for using variables. It understands
780 * the format:
781 *
Willy Tarreau9a621ae2021-09-02 21:00:38 +0200782 * set-var-fmt(<variable-name>) <format-string>
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200783 * set-var(<variable-name>) <expression>
Willy Tarreau4b7531f2019-06-04 16:43:29 +0200784 * unset-var(<variable-name>)
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200785 *
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +0200786 * It returns ACT_RET_PRS_ERR if fails and <err> is filled with an error
787 * message. Otherwise, it returns ACT_RET_PRS_OK and the variable <expr>
Willy Tarreau13d2ba22021-03-26 11:38:08 +0100788 * is filled with the pointer to the expression to execute. The proxy is
789 * only used to retrieve the ->conf entries.
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200790 */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +0200791static enum act_parse_ret parse_store(const char **args, int *arg, struct proxy *px,
792 struct act_rule *rule, char **err)
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200793{
794 const char *var_name = args[*arg-1];
795 int var_len;
Thierry FOURNIER48a9cd12015-07-28 19:00:28 +0200796 const char *kw_name;
Willy Tarreau9a621ae2021-09-02 21:00:38 +0200797 int flags, set_var = 0; /* 0=unset-var, 1=set-var, 2=set-var-fmt */
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200798
Willy Tarreau9a621ae2021-09-02 21:00:38 +0200799 if (strncmp(var_name, "set-var-fmt", 11) == 0) {
800 var_name += 11;
801 set_var = 2;
802 }
803 else if (strncmp(var_name, "set-var", 7) == 0) {
Christopher Faulet85d79c92016-11-09 16:54:56 +0100804 var_name += 7;
805 set_var = 1;
806 }
Willy Tarreau28192102021-09-02 18:46:22 +0200807 else if (strncmp(var_name, "unset-var", 9) == 0) {
Christopher Faulet85d79c92016-11-09 16:54:56 +0100808 var_name += 9;
809 set_var = 0;
810 }
811
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200812 if (*var_name != '(') {
Willy Tarreau9a621ae2021-09-02 21:00:38 +0200813 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 +0100814 args[*arg-1]);
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +0200815 return ACT_RET_PRS_ERR;
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200816 }
817 var_name++; /* jump the '(' */
818 var_len = strlen(var_name);
819 var_len--; /* remove the ')' */
820 if (var_name[var_len] != ')') {
Willy Tarreau9a621ae2021-09-02 21:00:38 +0200821 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 +0100822 args[*arg-1]);
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +0200823 return ACT_RET_PRS_ERR;
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200824 }
825
Willy Tarreau9a621ae2021-09-02 21:00:38 +0200826 LIST_INIT(&rule->arg.vars.fmt);
Christopher Faulet09c9df22016-10-31 11:05:37 +0100827 rule->arg.vars.name = register_name(var_name, var_len, &rule->arg.vars.scope, 1, err);
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +0200828 if (!rule->arg.vars.name)
829 return ACT_RET_PRS_ERR;
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200830
Christopher Faulet85d79c92016-11-09 16:54:56 +0100831 /* There is no fetch method when variable is unset. Just set the right
832 * action and return. */
833 if (!set_var) {
Christopher Faulet85d79c92016-11-09 16:54:56 +0100834 rule->action = ACT_CUSTOM;
835 rule->action_ptr = action_clear;
Tim Duesterhus01a0ce32020-06-14 17:27:36 +0200836 rule->release_ptr = release_store_rule;
Christopher Faulet85d79c92016-11-09 16:54:56 +0100837 return ACT_RET_PRS_OK;
838 }
839
Thierry FOURNIER48a9cd12015-07-28 19:00:28 +0200840 kw_name = args[*arg-1];
841
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +0200842 switch (rule->from) {
Willy Tarreau843096d2021-09-02 19:03:07 +0200843 case ACT_F_TCP_REQ_SES:
844 flags = SMP_VAL_FE_SES_ACC;
Willy Tarreau54b96d92021-09-02 19:46:08 +0200845 px->conf.args.ctx = ARGC_TSE;
Willy Tarreau843096d2021-09-02 19:03:07 +0200846 break;
847 case ACT_F_TCP_REQ_CNT:
848 flags = (px->cap & PR_CAP_FE) ? SMP_VAL_FE_REQ_CNT : SMP_VAL_BE_REQ_CNT;
Willy Tarreau54b96d92021-09-02 19:46:08 +0200849 px->conf.args.ctx = ARGC_TRQ;
Willy Tarreau843096d2021-09-02 19:03:07 +0200850 break;
851 case ACT_F_TCP_RES_CNT:
852 flags = (px->cap & PR_CAP_FE) ? SMP_VAL_FE_RES_CNT : SMP_VAL_BE_RES_CNT;
Willy Tarreau54b96d92021-09-02 19:46:08 +0200853 px->conf.args.ctx = ARGC_TRS;
Willy Tarreau843096d2021-09-02 19:03:07 +0200854 break;
855 case ACT_F_HTTP_REQ:
856 flags = (px->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR;
Willy Tarreau54b96d92021-09-02 19:46:08 +0200857 px->conf.args.ctx = ARGC_HRQ;
Willy Tarreau843096d2021-09-02 19:03:07 +0200858 break;
859 case ACT_F_HTTP_RES:
860 flags = (px->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR;
Willy Tarreau54b96d92021-09-02 19:46:08 +0200861 px->conf.args.ctx = ARGC_HRS;
Willy Tarreau843096d2021-09-02 19:03:07 +0200862 break;
863 case ACT_F_TCP_CHK:
864 flags = SMP_VAL_BE_CHK_RUL;
Willy Tarreau54b96d92021-09-02 19:46:08 +0200865 px->conf.args.ctx = ARGC_TCK;
Willy Tarreau843096d2021-09-02 19:03:07 +0200866 break;
867 case ACT_F_CFG_PARSER:
868 flags = SMP_VAL_CFG_PARSER;
Willy Tarreau54b96d92021-09-02 19:46:08 +0200869 px->conf.args.ctx = ARGC_CFG;
Willy Tarreau843096d2021-09-02 19:03:07 +0200870 break;
871 case ACT_F_CLI_PARSER:
872 flags = SMP_VAL_CLI_PARSER;
Willy Tarreau54b96d92021-09-02 19:46:08 +0200873 px->conf.args.ctx = ARGC_CLI;
Willy Tarreau843096d2021-09-02 19:03:07 +0200874 break;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +0200875 default:
876 memprintf(err,
877 "internal error, unexpected rule->from=%d, please report this bug!",
878 rule->from);
Thierry FOURNIERafa80492015-08-19 09:04:15 +0200879 return ACT_RET_PRS_ERR;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +0200880 }
Willy Tarreau54b96d92021-09-02 19:46:08 +0200881
Willy Tarreau9a621ae2021-09-02 21:00:38 +0200882 if (set_var == 2) { /* set-var-fmt */
883 if (!parse_logformat_string(args[*arg], px, &rule->arg.vars.fmt, 0, flags, err))
884 return ACT_RET_PRS_ERR;
Willy Tarreau54b96d92021-09-02 19:46:08 +0200885
Willy Tarreau9a621ae2021-09-02 21:00:38 +0200886 (*arg)++;
887
888 /* for late error reporting */
889 free(px->conf.lfs_file);
890 px->conf.lfs_file = strdup(px->conf.args.file);
891 px->conf.lfs_line = px->conf.args.line;
892 } else {
893 /* set-var */
894 rule->arg.vars.expr = sample_parse_expr((char **)args, arg, px->conf.args.file,
895 px->conf.args.line, err, &px->conf.args, NULL);
896 if (!rule->arg.vars.expr)
897 return ACT_RET_PRS_ERR;
898
899 if (!(rule->arg.vars.expr->fetch->val & flags)) {
900 memprintf(err,
901 "fetch method '%s' extracts information from '%s', none of which is available here",
902 kw_name, sample_src_names(rule->arg.vars.expr->fetch->use));
903 free(rule->arg.vars.expr);
904 return ACT_RET_PRS_ERR;
905 }
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +0200906 }
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200907
Thierry FOURNIER42148732015-09-02 17:17:33 +0200908 rule->action = ACT_CUSTOM;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +0200909 rule->action_ptr = action_store;
Tim Duesterhus01a0ce32020-06-14 17:27:36 +0200910 rule->release_ptr = release_store_rule;
Thierry FOURNIERafa80492015-08-19 09:04:15 +0200911 return ACT_RET_PRS_OK;
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200912}
913
Willy Tarreau13d2ba22021-03-26 11:38:08 +0100914
915/* parses a global "set-var" directive. It will create a temporary rule and
916 * expression that are parsed, processed, and released on the fly so that we
917 * respect the real set-var syntax. These directives take the following format:
918 * set-var <name> <expression>
Willy Tarreau753d4db2021-09-03 09:02:47 +0200919 * set-var-fmt <name> <fmt>
Willy Tarreau13d2ba22021-03-26 11:38:08 +0100920 * Note that parse_store() expects "set-var(name) <expression>" so we have to
921 * temporarily replace the keyword here.
922 */
923static int vars_parse_global_set_var(char **args, int section_type, struct proxy *curpx,
924 const struct proxy *defpx, const char *file, int line,
925 char **err)
926{
927 struct proxy px = {
Willy Tarreau9c204332021-09-03 08:19:43 +0200928 .id = "CFG",
Willy Tarreau13d2ba22021-03-26 11:38:08 +0100929 .conf.args.file = file,
930 .conf.args.line = line,
931 };
932 struct act_rule rule = {
933 .arg.vars.scope = SCOPE_PROC,
934 .from = ACT_F_CFG_PARSER,
935 };
Willy Tarreau753d4db2021-09-03 09:02:47 +0200936 enum obj_type objt = OBJ_TYPE_NONE;
937 struct session *sess = NULL;
Willy Tarreau13d2ba22021-03-26 11:38:08 +0100938 enum act_parse_ret p_ret;
939 char *old_arg1;
940 char *tmp_arg1;
941 int arg = 2; // variable name
942 int ret = -1;
Willy Tarreau753d4db2021-09-03 09:02:47 +0200943 int use_fmt = 0;
Willy Tarreau13d2ba22021-03-26 11:38:08 +0100944
945 LIST_INIT(&px.conf.args.list);
946
Willy Tarreau753d4db2021-09-03 09:02:47 +0200947 use_fmt = strcmp(args[0], "set-var-fmt") == 0;
948
Willy Tarreau13d2ba22021-03-26 11:38:08 +0100949 if (!*args[1] || !*args[2]) {
Willy Tarreau753d4db2021-09-03 09:02:47 +0200950 if (use_fmt)
951 memprintf(err, "'%s' requires a process-wide variable name ('proc.<name>') and a format string.", args[0]);
952 else
953 memprintf(err, "'%s' requires a process-wide variable name ('proc.<name>') and a sample expression.", args[0]);
Willy Tarreau13d2ba22021-03-26 11:38:08 +0100954 goto end;
955 }
956
957 tmp_arg1 = NULL;
Willy Tarreau753d4db2021-09-03 09:02:47 +0200958 if (!memprintf(&tmp_arg1, "set-var%s(%s)", use_fmt ? "-fmt" : "", args[1]))
Willy Tarreau13d2ba22021-03-26 11:38:08 +0100959 goto end;
960
961 /* parse_store() will always return a message in <err> on error */
962 old_arg1 = args[1]; args[1] = tmp_arg1;
963 p_ret = parse_store((const char **)args, &arg, &px, &rule, err);
964 free(args[1]); args[1] = old_arg1;
965
966 if (p_ret != ACT_RET_PRS_OK)
967 goto end;
968
969 if (rule.arg.vars.scope != SCOPE_PROC) {
970 memprintf(err, "'%s': cannot set variable '%s', only scope 'proc' is permitted in the global section.", args[0], args[1]);
971 goto end;
972 }
973
974 if (smp_resolve_args(&px, err) != 0) {
975 release_sample_expr(rule.arg.vars.expr);
976 indent_msg(err, 2);
977 goto end;
978 }
979
Willy Tarreau753d4db2021-09-03 09:02:47 +0200980 if (use_fmt && !(sess = session_new(&px, NULL, &objt))) {
981 release_sample_expr(rule.arg.vars.expr);
982 memprintf(err, "'%s': out of memory when trying to set variable '%s' in the global section.", args[0], args[1]);
983 goto end;
984 }
985
986 action_store(&rule, &px, sess, NULL, 0);
Willy Tarreau13d2ba22021-03-26 11:38:08 +0100987 release_sample_expr(rule.arg.vars.expr);
Willy Tarreau753d4db2021-09-03 09:02:47 +0200988 if (sess)
989 session_free(sess);
Willy Tarreau13d2ba22021-03-26 11:38:08 +0100990
991 ret = 0;
992 end:
993 return ret;
994}
995
Willy Tarreauc35eb382021-03-26 14:51:31 +0100996/* parse CLI's "get var <name>" */
997static int vars_parse_cli_get_var(char **args, char *payload, struct appctx *appctx, void *private)
998{
999 struct vars *vars;
Willy Tarreau374edc72021-04-01 17:01:43 +02001000 struct sample smp = { };
Willy Tarreauc35eb382021-03-26 14:51:31 +01001001 int i;
1002
1003 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
1004 return 1;
1005
1006 if (!*args[2])
1007 return cli_err(appctx, "Missing process-wide variable identifier.\n");
1008
1009 vars = get_vars(NULL, NULL, SCOPE_PROC);
1010 if (!vars || vars->scope != SCOPE_PROC)
1011 return 0;
1012
Willy Tarreaue352b9d2021-09-03 11:52:38 +02001013 if (!vars_get_by_name(args[2], strlen(args[2]), &smp, NULL))
Willy Tarreauc35eb382021-03-26 14:51:31 +01001014 return cli_err(appctx, "Variable not found.\n");
1015
1016 /* the sample returned by vars_get_by_name() is allocated into a trash
1017 * chunk so we have no constraint to manipulate it.
1018 */
1019 chunk_printf(&trash, "%s: type=%s value=", args[2], smp_to_type[smp.data.type]);
1020
1021 if (!sample_casts[smp.data.type][SMP_T_STR] ||
1022 !sample_casts[smp.data.type][SMP_T_STR](&smp)) {
1023 chunk_appendf(&trash, "(undisplayable)");
1024 } else {
1025 /* Display the displayable chars*. */
1026 b_putchr(&trash, '<');
1027 for (i = 0; i < smp.data.u.str.data; i++) {
1028 if (isprint((unsigned char)smp.data.u.str.area[i]))
1029 b_putchr(&trash, smp.data.u.str.area[i]);
1030 else
1031 b_putchr(&trash, '.');
1032 }
1033 b_putchr(&trash, '>');
1034 b_putchr(&trash, 0);
1035 }
1036 return cli_msg(appctx, LOG_INFO, trash.area);
1037}
1038
Willy Tarreaue93bff42021-09-03 09:47:37 +02001039/* parse CLI's "set var <name>". It accepts:
1040 * - set var <name> <expression>
1041 * - set var <name> expr <expression>
1042 * - set var <name> fmt <format>
1043 */
Willy Tarreaub8bd1ee2021-03-26 15:19:50 +01001044static int vars_parse_cli_set_var(char **args, char *payload, struct appctx *appctx, void *private)
1045{
1046 struct proxy px = {
1047 .id = "CLI",
1048 .conf.args.file = "CLI",
1049 .conf.args.line = 0,
1050 };
1051 struct act_rule rule = {
1052 .arg.vars.scope = SCOPE_PROC,
1053 .from = ACT_F_CLI_PARSER,
1054 };
Willy Tarreaue93bff42021-09-03 09:47:37 +02001055 enum obj_type objt = OBJ_TYPE_NONE;
1056 struct session *sess = NULL;
Willy Tarreaub8bd1ee2021-03-26 15:19:50 +01001057 enum act_parse_ret p_ret;
Willy Tarreaue93bff42021-09-03 09:47:37 +02001058 const char *tmp_args[3];
1059 int tmp_arg;
1060 char *tmp_act;
Willy Tarreaub8bd1ee2021-03-26 15:19:50 +01001061 char *err = NULL;
Willy Tarreaub8bd1ee2021-03-26 15:19:50 +01001062 int nberr;
Willy Tarreaue93bff42021-09-03 09:47:37 +02001063 int use_fmt = 0;
Willy Tarreaub8bd1ee2021-03-26 15:19:50 +01001064
1065 LIST_INIT(&px.conf.args.list);
1066
1067 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
1068 return 1;
1069
Willy Tarreaue93bff42021-09-03 09:47:37 +02001070 if (!*args[2])
1071 return cli_err(appctx, "Missing process-wide variable identifier.\n");
Willy Tarreaub8bd1ee2021-03-26 15:19:50 +01001072
Willy Tarreaue93bff42021-09-03 09:47:37 +02001073 if (!*args[3])
1074 return cli_err(appctx, "Missing either 'expr', 'fmt' or expression.\n");
1075
1076 if (*args[4]) {
1077 /* this is the long format */
1078 if (strcmp(args[3], "fmt") == 0)
1079 use_fmt = 1;
1080 else if (strcmp(args[3], "expr") != 0) {
1081 memprintf(&err, "'%s %s': arg type must be either 'expr' or 'fmt' but got '%s'.", args[0], args[1], args[3]);
1082 goto fail;
1083 }
1084 }
1085
1086 tmp_act = NULL;
1087 if (!memprintf(&tmp_act, "set-var%s(%s)", use_fmt ? "-fmt" : "", args[2])) {
Willy Tarreaub8bd1ee2021-03-26 15:19:50 +01001088 memprintf(&err, "memory allocation error.");
1089 goto fail;
1090 }
1091
1092 /* parse_store() will always return a message in <err> on error */
Willy Tarreaue93bff42021-09-03 09:47:37 +02001093 tmp_args[0] = tmp_act;
1094 tmp_args[1] = (*args[4]) ? args[4] : args[3];
1095 tmp_args[2] = "";
1096 tmp_arg = 1; // must point to the first arg after the action
1097 p_ret = parse_store(tmp_args, &tmp_arg, &px, &rule, &err);
1098 free(tmp_act);
Willy Tarreaub8bd1ee2021-03-26 15:19:50 +01001099
1100 if (p_ret != ACT_RET_PRS_OK)
1101 goto fail;
1102
1103 if (rule.arg.vars.scope != SCOPE_PROC) {
Willy Tarreauc767eeb2021-09-03 10:23:26 +02001104 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 +01001105 goto fail;
1106 }
1107
1108 err = NULL;
1109 nberr = smp_resolve_args(&px, &err);
1110 if (nberr) {
1111 release_sample_expr(rule.arg.vars.expr);
1112 indent_msg(&err, 2);
1113 goto fail;
1114 }
1115
Willy Tarreaue93bff42021-09-03 09:47:37 +02001116 if (use_fmt && !(sess = session_new(&px, NULL, &objt))) {
1117 release_sample_expr(rule.arg.vars.expr);
1118 memprintf(&err, "memory allocation error.");
1119 goto fail;
1120 }
1121
1122 action_store(&rule, &px, sess, NULL, 0);
Willy Tarreaub8bd1ee2021-03-26 15:19:50 +01001123 release_sample_expr(rule.arg.vars.expr);
Willy Tarreaue93bff42021-09-03 09:47:37 +02001124 if (sess)
1125 session_free(sess);
1126
Willy Tarreaub8bd1ee2021-03-26 15:19:50 +01001127 appctx->st0 = CLI_ST_PROMPT;
1128 return 0;
1129 fail:
1130 return cli_dynerr(appctx, err);
1131}
1132
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02001133static int vars_max_size(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01001134 const struct proxy *defpx, const char *file, int line,
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02001135 char **err, unsigned int *limit)
1136{
1137 char *error;
1138
1139 *limit = strtol(args[1], &error, 10);
1140 if (*error != 0) {
1141 memprintf(err, "%s: '%s' is an invalid size", args[0], args[1]);
1142 return -1;
1143 }
1144 return 0;
1145}
1146
1147static int vars_max_size_global(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01001148 const struct proxy *defpx, const char *file, int line,
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02001149 char **err)
1150{
1151 return vars_max_size(args, section_type, curpx, defpx, file, line, err, &var_global_limit);
1152}
1153
Christopher Fauletff2613e2016-11-09 11:36:17 +01001154static int vars_max_size_proc(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01001155 const struct proxy *defpx, const char *file, int line,
Christopher Fauletff2613e2016-11-09 11:36:17 +01001156 char **err)
1157{
1158 return vars_max_size(args, section_type, curpx, defpx, file, line, err, &var_proc_limit);
1159}
1160
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02001161static int vars_max_size_sess(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01001162 const struct proxy *defpx, const char *file, int line,
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02001163 char **err)
1164{
1165 return vars_max_size(args, section_type, curpx, defpx, file, line, err, &var_sess_limit);
1166}
1167
1168static int vars_max_size_txn(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01001169 const struct proxy *defpx, const char *file, int line,
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02001170 char **err)
1171{
1172 return vars_max_size(args, section_type, curpx, defpx, file, line, err, &var_txn_limit);
1173}
1174
1175static int vars_max_size_reqres(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01001176 const struct proxy *defpx, const char *file, int line,
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02001177 char **err)
1178{
1179 return vars_max_size(args, section_type, curpx, defpx, file, line, err, &var_reqres_limit);
1180}
1181
Gaetan Rivet13a50432020-02-21 18:13:44 +01001182static int vars_max_size_check(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01001183 const struct proxy *defpx, const char *file, int line,
Gaetan Rivet13a50432020-02-21 18:13:44 +01001184 char **err)
1185{
1186 return vars_max_size(args, section_type, curpx, defpx, file, line, err, &var_check_limit);
1187}
1188
Tim Duesterhusbbdd5b82020-07-04 11:53:25 +02001189static void vars_deinit()
1190{
1191 while (var_names_nb-- > 0)
1192 free(var_names[var_names_nb]);
1193 free(var_names);
1194}
1195
1196REGISTER_POST_DEINIT(vars_deinit);
1197
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02001198static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
1199
Willy Tarreau54496a62021-09-03 12:00:13 +02001200 { "var", smp_fetch_var, ARG2(1,STR,STR), smp_check_var, SMP_T_STR, SMP_USE_CONST },
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02001201 { /* END */ },
1202}};
1203
Willy Tarreau0108d902018-11-25 19:14:37 +01001204INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);
1205
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02001206static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Christopher Faulet85d79c92016-11-09 16:54:56 +01001207 { "set-var", smp_conv_store, ARG1(1,STR), conv_check_var, SMP_T_ANY, SMP_T_ANY },
1208 { "unset-var", smp_conv_clear, ARG1(1,STR), conv_check_var, SMP_T_ANY, SMP_T_ANY },
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02001209 { /* END */ },
1210}};
1211
Willy Tarreau0108d902018-11-25 19:14:37 +01001212INITCALL1(STG_REGISTER, sample_register_convs, &sample_conv_kws);
1213
Willy Tarreau620408f2016-10-21 16:37:51 +02001214static struct action_kw_list tcp_req_sess_kws = { { }, {
Willy Tarreau9a621ae2021-09-02 21:00:38 +02001215 { "set-var-fmt", parse_store, KWF_MATCH_PREFIX },
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02001216 { "set-var", parse_store, KWF_MATCH_PREFIX },
1217 { "unset-var", parse_store, KWF_MATCH_PREFIX },
Willy Tarreau620408f2016-10-21 16:37:51 +02001218 { /* END */ }
1219}};
1220
Willy Tarreau0108d902018-11-25 19:14:37 +01001221INITCALL1(STG_REGISTER, tcp_req_sess_keywords_register, &tcp_req_sess_kws);
1222
Willy Tarreau620408f2016-10-21 16:37:51 +02001223static struct action_kw_list tcp_req_cont_kws = { { }, {
Willy Tarreau9a621ae2021-09-02 21:00:38 +02001224 { "set-var-fmt", parse_store, KWF_MATCH_PREFIX },
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02001225 { "set-var", parse_store, KWF_MATCH_PREFIX },
1226 { "unset-var", parse_store, KWF_MATCH_PREFIX },
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02001227 { /* END */ }
1228}};
1229
Willy Tarreau0108d902018-11-25 19:14:37 +01001230INITCALL1(STG_REGISTER, tcp_req_cont_keywords_register, &tcp_req_cont_kws);
1231
Thierry FOURNIER36481b82015-08-19 09:01:53 +02001232static struct action_kw_list tcp_res_kws = { { }, {
Willy Tarreau9a621ae2021-09-02 21:00:38 +02001233 { "set-var-fmt", parse_store, KWF_MATCH_PREFIX },
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02001234 { "set-var", parse_store, KWF_MATCH_PREFIX },
1235 { "unset-var", parse_store, KWF_MATCH_PREFIX },
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02001236 { /* END */ }
1237}};
1238
Willy Tarreau0108d902018-11-25 19:14:37 +01001239INITCALL1(STG_REGISTER, tcp_res_cont_keywords_register, &tcp_res_kws);
1240
Gaetan Rivet707b52f2020-02-21 18:14:59 +01001241static struct action_kw_list tcp_check_kws = {ILH, {
Willy Tarreau9a621ae2021-09-02 21:00:38 +02001242 { "set-var-fmt", parse_store, KWF_MATCH_PREFIX },
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02001243 { "set-var", parse_store, KWF_MATCH_PREFIX },
1244 { "unset-var", parse_store, KWF_MATCH_PREFIX },
Gaetan Rivet707b52f2020-02-21 18:14:59 +01001245 { /* END */ }
1246}};
1247
1248INITCALL1(STG_REGISTER, tcp_check_keywords_register, &tcp_check_kws);
1249
Thierry FOURNIER36481b82015-08-19 09:01:53 +02001250static struct action_kw_list http_req_kws = { { }, {
Willy Tarreau9a621ae2021-09-02 21:00:38 +02001251 { "set-var-fmt", parse_store, KWF_MATCH_PREFIX },
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02001252 { "set-var", parse_store, KWF_MATCH_PREFIX },
1253 { "unset-var", parse_store, KWF_MATCH_PREFIX },
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02001254 { /* END */ }
1255}};
1256
Willy Tarreau0108d902018-11-25 19:14:37 +01001257INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_kws);
1258
Thierry FOURNIER36481b82015-08-19 09:01:53 +02001259static struct action_kw_list http_res_kws = { { }, {
Willy Tarreau9a621ae2021-09-02 21:00:38 +02001260 { "set-var-fmt", parse_store, KWF_MATCH_PREFIX },
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02001261 { "set-var", parse_store, KWF_MATCH_PREFIX },
1262 { "unset-var", parse_store, KWF_MATCH_PREFIX },
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02001263 { /* END */ }
1264}};
1265
Willy Tarreau0108d902018-11-25 19:14:37 +01001266INITCALL1(STG_REGISTER, http_res_keywords_register, &http_res_kws);
1267
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01001268static struct action_kw_list http_after_res_kws = { { }, {
Willy Tarreau9a621ae2021-09-02 21:00:38 +02001269 { "set-var-fmt", parse_store, KWF_MATCH_PREFIX },
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02001270 { "set-var", parse_store, KWF_MATCH_PREFIX },
1271 { "unset-var", parse_store, KWF_MATCH_PREFIX },
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01001272 { /* END */ }
1273}};
1274
1275INITCALL1(STG_REGISTER, http_after_res_keywords_register, &http_after_res_kws);
1276
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02001277static struct cfg_kw_list cfg_kws = {{ },{
Willy Tarreau13d2ba22021-03-26 11:38:08 +01001278 { CFG_GLOBAL, "set-var", vars_parse_global_set_var },
Willy Tarreau753d4db2021-09-03 09:02:47 +02001279 { CFG_GLOBAL, "set-var-fmt", vars_parse_global_set_var },
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02001280 { CFG_GLOBAL, "tune.vars.global-max-size", vars_max_size_global },
Christopher Fauletff2613e2016-11-09 11:36:17 +01001281 { CFG_GLOBAL, "tune.vars.proc-max-size", vars_max_size_proc },
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02001282 { CFG_GLOBAL, "tune.vars.sess-max-size", vars_max_size_sess },
1283 { CFG_GLOBAL, "tune.vars.txn-max-size", vars_max_size_txn },
1284 { CFG_GLOBAL, "tune.vars.reqres-max-size", vars_max_size_reqres },
Gaetan Rivet13a50432020-02-21 18:13:44 +01001285 { CFG_GLOBAL, "tune.vars.check-max-size", vars_max_size_check },
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02001286 { /* END */ }
1287}};
1288
Willy Tarreau0108d902018-11-25 19:14:37 +01001289INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
Willy Tarreauc35eb382021-03-26 14:51:31 +01001290
1291
1292/* register cli keywords */
1293static struct cli_kw_list cli_kws = {{ },{
Willy Tarreaub205bfd2021-05-07 11:38:37 +02001294 { { "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 +02001295 { { "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 +01001296 { { NULL }, NULL, NULL, NULL }
1297}};
1298INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);