blob: 80a6a4520c6164e78afdcd286c2ca7f1c632c484 [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
Willy Tarreaub7bfcb32021-08-31 08:13:25 +0200199/* This function initializes a variables list head */
200void vars_init_head(struct vars *vars, enum vars_scope scope)
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200201{
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
Willy Tarreauf1cb0eb2021-09-07 11:37:37 +0200350/* This function tries to create variable <name> in scope <scope> and store
351 * sample <smp> as its value. The stream and session are extracted from <smp>,
352 * and the stream may be NULL when scope is SCOPE_SESS. In case there wouldn't
353 * be enough memory to store the sample while the variable was already created,
354 * it would be changed to a bool (which is memory-less).
Willy Tarreau7978c5c2021-09-07 14:24:07 +0200355 *
356 * Flags is a bitfield that may contain one of the following flags:
357 * - VF_UPDATEONLY: if the scope is SCOPE_PROC, the variable may only be
358 * updated but not created.
Willy Tarreau4994b572021-09-08 11:38:25 +0200359 * - VF_CREATEONLY: do nothing if the variable already exists (success).
Willy Tarreau7978c5c2021-09-07 14:24:07 +0200360 *
Willy Tarreauf1cb0eb2021-09-07 11:37:37 +0200361 * It returns 0 on failure, non-zero on success.
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200362 */
Willy Tarreau7978c5c2021-09-07 14:24:07 +0200363static int var_set(const char *name, enum vars_scope scope, struct sample *smp, uint flags)
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200364{
Willy Tarreauf1cb0eb2021-09-07 11:37:37 +0200365 struct vars *vars;
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200366 struct var *var;
Willy Tarreauf1cb0eb2021-09-07 11:37:37 +0200367 int ret = 0;
368
369 vars = get_vars(smp->sess, smp->strm, scope);
370 if (!vars || vars->scope != scope)
371 return 0;
372
373 HA_RWLOCK_WRLOCK(VARS_LOCK, &vars->rwlock);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200374
375 /* Look for existing variable name. */
376 var = var_get(vars, name);
377
378 if (var) {
Willy Tarreau4994b572021-09-08 11:38:25 +0200379 if (flags & VF_CREATEONLY) {
380 ret = 1;
381 goto unlock;
382 }
383
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200384 /* free its used memory. */
385 if (var->data.type == SMP_T_STR ||
386 var->data.type == SMP_T_BIN) {
Willy Tarreau5b52b002021-02-26 21:19:53 +0100387 ha_free(&var->data.u.str.area);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200388 var_accounting_diff(vars, smp->sess, smp->strm,
389 -var->data.u.str.data);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200390 }
Christopher Fauletd02210c2017-07-24 16:24:39 +0200391 else if (var->data.type == SMP_T_METH && var->data.u.meth.meth == HTTP_METH_OTHER) {
Willy Tarreau5b52b002021-02-26 21:19:53 +0100392 ha_free(&var->data.u.meth.str.area);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200393 var_accounting_diff(vars, smp->sess, smp->strm,
394 -var->data.u.meth.str.data);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200395 }
396 } else {
Willy Tarreau7978c5c2021-09-07 14:24:07 +0200397 /* creation permitted for proc ? */
398 if (flags & VF_UPDATEONLY && scope == SCOPE_PROC)
399 goto unlock;
400
Joseph Herlant07676892018-11-15 09:19:50 -0800401 /* Check memory available. */
Willy Tarreau6204cd92016-03-10 16:33:04 +0100402 if (!var_accounting_add(vars, smp->sess, smp->strm, sizeof(struct var)))
Willy Tarreauf1cb0eb2021-09-07 11:37:37 +0200403 goto unlock;
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200404
405 /* Create new entry. */
Willy Tarreaubafbe012017-11-24 17:34:44 +0100406 var = pool_alloc(var_pool);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200407 if (!var)
Willy Tarreauf1cb0eb2021-09-07 11:37:37 +0200408 goto unlock;
Willy Tarreau2b718102021-04-21 07:32:39 +0200409 LIST_APPEND(&vars->head, &var->l);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200410 var->name = name;
411 }
412
413 /* Set type. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200414 var->data.type = smp->data.type;
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200415
416 /* Copy data. If the data needs memory, the function can fail. */
417 switch (var->data.type) {
418 case SMP_T_BOOL:
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200419 case SMP_T_SINT:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200420 var->data.u.sint = smp->data.u.sint;
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200421 break;
422 case SMP_T_IPV4:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200423 var->data.u.ipv4 = smp->data.u.ipv4;
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200424 break;
425 case SMP_T_IPV6:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200426 var->data.u.ipv6 = smp->data.u.ipv6;
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200427 break;
428 case SMP_T_STR:
429 case SMP_T_BIN:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200430 if (!var_accounting_add(vars, smp->sess, smp->strm, smp->data.u.str.data)) {
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200431 var->data.type = SMP_T_BOOL; /* This type doesn't use additional memory. */
Willy Tarreauf1cb0eb2021-09-07 11:37:37 +0200432 goto unlock;
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200433 }
Willy Tarreauf1cb0eb2021-09-07 11:37:37 +0200434
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200435 var->data.u.str.area = malloc(smp->data.u.str.data);
436 if (!var->data.u.str.area) {
437 var_accounting_diff(vars, smp->sess, smp->strm,
438 -smp->data.u.str.data);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200439 var->data.type = SMP_T_BOOL; /* This type doesn't use additional memory. */
Willy Tarreauf1cb0eb2021-09-07 11:37:37 +0200440 goto unlock;
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200441 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200442 var->data.u.str.data = smp->data.u.str.data;
443 memcpy(var->data.u.str.area, smp->data.u.str.area,
444 var->data.u.str.data);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200445 break;
446 case SMP_T_METH:
Christopher Fauletd02210c2017-07-24 16:24:39 +0200447 var->data.u.meth.meth = smp->data.u.meth.meth;
448 if (smp->data.u.meth.meth != HTTP_METH_OTHER)
449 break;
450
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200451 if (!var_accounting_add(vars, smp->sess, smp->strm, smp->data.u.meth.str.data)) {
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200452 var->data.type = SMP_T_BOOL; /* This type doesn't use additional memory. */
Willy Tarreauf1cb0eb2021-09-07 11:37:37 +0200453 goto unlock;
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200454 }
Willy Tarreauf1cb0eb2021-09-07 11:37:37 +0200455
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200456 var->data.u.meth.str.area = malloc(smp->data.u.meth.str.data);
457 if (!var->data.u.meth.str.area) {
458 var_accounting_diff(vars, smp->sess, smp->strm,
459 -smp->data.u.meth.str.data);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200460 var->data.type = SMP_T_BOOL; /* This type doesn't use additional memory. */
Willy Tarreauf1cb0eb2021-09-07 11:37:37 +0200461 goto unlock;
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200462 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200463 var->data.u.meth.str.data = smp->data.u.meth.str.data;
464 var->data.u.meth.str.size = smp->data.u.meth.str.data;
465 memcpy(var->data.u.meth.str.area, smp->data.u.meth.str.area,
466 var->data.u.meth.str.data);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200467 break;
468 }
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200469
Willy Tarreauf1cb0eb2021-09-07 11:37:37 +0200470 /* OK, now done */
471 ret = 1;
472 unlock:
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100473 HA_RWLOCK_WRUNLOCK(VARS_LOCK, &vars->rwlock);
Christopher Faulete95f2c32017-07-24 16:30:34 +0200474 return ret;
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200475}
476
Willy Tarreaud378eb82021-09-07 11:44:41 +0200477/* This unsets variable <name> from scope <scope>, using the session and stream
478 * found in <smp>. Note that stream may be null for SCOPE_SESS. Returns 0 if
479 * the scope was not found otherwise 1.
480 */
481static int var_unset(const char *name, enum vars_scope scope, struct sample *smp)
Christopher Faulet85d79c92016-11-09 16:54:56 +0100482{
483 struct vars *vars;
484 struct var *var;
485 unsigned int size = 0;
486
Willy Tarreauf37b1402019-06-04 16:27:36 +0200487 vars = get_vars(smp->sess, smp->strm, scope);
488 if (!vars || vars->scope != scope)
Christopher Faulet85d79c92016-11-09 16:54:56 +0100489 return 0;
490
491 /* Look for existing variable name. */
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100492 HA_RWLOCK_WRLOCK(VARS_LOCK, &vars->rwlock);
Christopher Faulet85d79c92016-11-09 16:54:56 +0100493 var = var_get(vars, name);
494 if (var) {
495 size = var_clear(var);
496 var_accounting_diff(vars, smp->sess, smp->strm, -size);
497 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100498 HA_RWLOCK_WRUNLOCK(VARS_LOCK, &vars->rwlock);
Christopher Faulet85d79c92016-11-09 16:54:56 +0100499 return 1;
500}
501
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200502/* Returns 0 if fails, else returns 1. */
503static int smp_conv_store(const struct arg *args, struct sample *smp, void *private)
504{
Willy Tarreau7978c5c2021-09-07 14:24:07 +0200505 return var_set(args[0].data.var.name, args[0].data.var.scope, smp, 0);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200506}
507
Christopher Faulet85d79c92016-11-09 16:54:56 +0100508/* Returns 0 if fails, else returns 1. */
509static int smp_conv_clear(const struct arg *args, struct sample *smp, void *private)
510{
Willy Tarreaud378eb82021-09-07 11:44:41 +0200511 return var_unset(args[0].data.var.name, args[0].data.var.scope, smp);
Christopher Faulet85d79c92016-11-09 16:54:56 +0100512}
513
Joseph Herlant07676892018-11-15 09:19:50 -0800514/* This functions check an argument entry and fill it with a variable
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200515 * type. The argumen must be a string. If the variable lookup fails,
Joseph Herlant07676892018-11-15 09:19:50 -0800516 * the function returns 0 and fill <err>, otherwise it returns 1.
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200517 */
518int vars_check_arg(struct arg *arg, char **err)
519{
520 char *name;
521 enum vars_scope scope;
522
523 /* Check arg type. */
524 if (arg->type != ARGT_STR) {
525 memprintf(err, "unexpected argument type");
526 return 0;
527 }
528
529 /* Register new variable name. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200530 name = register_name(arg->data.str.area, arg->data.str.data, &scope,
531 1,
532 err);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200533 if (!name)
534 return 0;
535
Tim Duesterhusa6cc7e82019-05-13 10:53:29 +0200536 /* properly destroy the chunk */
537 chunk_destroy(&arg->data.str);
538
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200539 /* Use the global variable name pointer. */
540 arg->type = ARGT_VAR;
541 arg->data.var.name = name;
542 arg->data.var.scope = scope;
543 return 1;
544}
545
Christopher Faulet09c9df22016-10-31 11:05:37 +0100546/* This function store a sample in a variable if it was already defined.
Tim Duesterhusb4fac1e2020-05-19 13:49:40 +0200547 * Returns zero on failure and non-zero otherwise. The variable not being
548 * defined is treated as a failure.
Christopher Faulet09c9df22016-10-31 11:05:37 +0100549 */
Tim Duesterhusb4fac1e2020-05-19 13:49:40 +0200550int vars_set_by_name_ifexist(const char *name, size_t len, struct sample *smp)
Christopher Faulet09c9df22016-10-31 11:05:37 +0100551{
552 enum vars_scope scope;
553
554 /* Resolve name and scope. */
555 name = register_name(name, len, &scope, 0, NULL);
556 if (!name)
Tim Duesterhusb4fac1e2020-05-19 13:49:40 +0200557 return 0;
Christopher Faulet09c9df22016-10-31 11:05:37 +0100558
Willy Tarreau7978c5c2021-09-07 14:24:07 +0200559 return var_set(name, scope, smp, VF_UPDATEONLY);
Christopher Faulet09c9df22016-10-31 11:05:37 +0100560}
561
562
Thierry FOURNIERc365d992015-06-09 12:27:17 +0200563/* This function store a sample in a variable.
Tim Duesterhusb4fac1e2020-05-19 13:49:40 +0200564 * Returns zero on failure and non-zero otherwise.
Thierry FOURNIERc365d992015-06-09 12:27:17 +0200565 */
Tim Duesterhusb4fac1e2020-05-19 13:49:40 +0200566int vars_set_by_name(const char *name, size_t len, struct sample *smp)
Thierry FOURNIERc365d992015-06-09 12:27:17 +0200567{
568 enum vars_scope scope;
569
570 /* Resolve name and scope. */
Christopher Faulet09c9df22016-10-31 11:05:37 +0100571 name = register_name(name, len, &scope, 1, NULL);
Thierry FOURNIERc365d992015-06-09 12:27:17 +0200572 if (!name)
Tim Duesterhusb4fac1e2020-05-19 13:49:40 +0200573 return 0;
Thierry FOURNIERc365d992015-06-09 12:27:17 +0200574
Willy Tarreau7978c5c2021-09-07 14:24:07 +0200575 return var_set(name, scope, smp, 0);
Thierry FOURNIERc365d992015-06-09 12:27:17 +0200576}
577
Christopher Faulet85d79c92016-11-09 16:54:56 +0100578/* This function unset a variable if it was already defined.
Tim Duesterhusb4fac1e2020-05-19 13:49:40 +0200579 * Returns zero on failure and non-zero otherwise.
Christopher Faulet85d79c92016-11-09 16:54:56 +0100580 */
Tim Duesterhusb4fac1e2020-05-19 13:49:40 +0200581int vars_unset_by_name_ifexist(const char *name, size_t len, struct sample *smp)
Christopher Faulet85d79c92016-11-09 16:54:56 +0100582{
583 enum vars_scope scope;
584
585 /* Resolve name and scope. */
586 name = register_name(name, len, &scope, 0, NULL);
587 if (!name)
Tim Duesterhusb4fac1e2020-05-19 13:49:40 +0200588 return 0;
Christopher Faulet85d79c92016-11-09 16:54:56 +0100589
Willy Tarreaud378eb82021-09-07 11:44:41 +0200590 return var_unset(name, scope, smp);
Christopher Faulet85d79c92016-11-09 16:54:56 +0100591}
592
593
Willy Tarreaube7e00d2021-09-03 11:40:58 +0200594/* This retrieves variable <name> from variables <vars>, and if found,
595 * duplicates the result into sample <smp>. smp_dup() is used in order to
596 * release the variables lock ASAP (so a pre-allocated chunk is obtained
597 * via get_trash_shunk()). The variables' lock is used for reads.
598 *
Willy Tarreaue352b9d2021-09-03 11:52:38 +0200599 * The function returns 0 if the variable was not found and no default
600 * value was provided in <def>, otherwise 1 with the sample filled.
601 * Default values are always returned as strings.
Willy Tarreaube7e00d2021-09-03 11:40:58 +0200602 */
Willy Tarreaue352b9d2021-09-03 11:52:38 +0200603static int var_to_smp(struct vars *vars, const char *name, struct sample *smp, const struct buffer *def)
Willy Tarreaube7e00d2021-09-03 11:40:58 +0200604{
605 struct var *var;
606
607 /* Get the variable entry. */
608 HA_RWLOCK_RDLOCK(VARS_LOCK, &vars->rwlock);
609 var = var_get(vars, name);
610 if (!var) {
Willy Tarreaue352b9d2021-09-03 11:52:38 +0200611 if (!def) {
612 HA_RWLOCK_RDUNLOCK(VARS_LOCK, &vars->rwlock);
613 return 0;
614 }
615
616 /* not found but we have a default value */
617 smp->data.type = SMP_T_STR;
618 smp->data.u.str = *def;
Willy Tarreaube7e00d2021-09-03 11:40:58 +0200619 }
Willy Tarreaue352b9d2021-09-03 11:52:38 +0200620 else
621 smp->data = var->data;
Willy Tarreaube7e00d2021-09-03 11:40:58 +0200622
623 /* Copy sample. */
Willy Tarreaube7e00d2021-09-03 11:40:58 +0200624 smp_dup(smp);
625
626 HA_RWLOCK_RDUNLOCK(VARS_LOCK, &vars->rwlock);
627 return 1;
628}
629
Dragan Dosen14518f22021-02-22 17:20:01 +0100630/* This function fills a sample with the variable content.
631 *
632 * Keep in mind that a sample content is duplicated by using smp_dup()
633 * and it therefore uses a pre-allocated trash chunk as returned by
634 * get_trash_chunk().
635 *
Willy Tarreaue352b9d2021-09-03 11:52:38 +0200636 * If the variable is not valid in this scope, 0 is always returned.
637 * If the variable is valid but not found, either the default value
638 * <def> is returned if not NULL, or zero is returned.
639 *
Dragan Dosen14518f22021-02-22 17:20:01 +0100640 * Returns 1 if the sample is filled, otherwise it returns 0.
Thierry FOURNIERc365d992015-06-09 12:27:17 +0200641 */
Willy Tarreaue352b9d2021-09-03 11:52:38 +0200642int vars_get_by_name(const char *name, size_t len, struct sample *smp, const struct buffer *def)
Thierry FOURNIERc365d992015-06-09 12:27:17 +0200643{
644 struct vars *vars;
Thierry FOURNIERc365d992015-06-09 12:27:17 +0200645 enum vars_scope scope;
646
647 /* Resolve name and scope. */
Willy Tarreau89f6ded2021-05-13 13:30:12 +0200648 name = register_name(name, len, &scope, 0, NULL);
Thierry FOURNIERc365d992015-06-09 12:27:17 +0200649 if (!name)
650 return 0;
651
652 /* Select "vars" pool according with the scope. */
Willy Tarreauf37b1402019-06-04 16:27:36 +0200653 vars = get_vars(smp->sess, smp->strm, scope);
654 if (!vars || vars->scope != scope)
Thierry FOURNIERc365d992015-06-09 12:27:17 +0200655 return 0;
Thierry FOURNIERc365d992015-06-09 12:27:17 +0200656
Willy Tarreaue352b9d2021-09-03 11:52:38 +0200657
658 return var_to_smp(vars, name, smp, def);
Thierry FOURNIERc365d992015-06-09 12:27:17 +0200659}
660
Dragan Dosen14518f22021-02-22 17:20:01 +0100661/* This function fills a sample with the content of the variable described
662 * by <var_desc>.
663 *
664 * Keep in mind that a sample content is duplicated by using smp_dup()
665 * and it therefore uses a pre-allocated trash chunk as returned by
666 * get_trash_chunk().
667 *
Willy Tarreaue352b9d2021-09-03 11:52:38 +0200668 * If the variable is not valid in this scope, 0 is always returned.
669 * If the variable is valid but not found, either the default value
670 * <def> is returned if not NULL, or zero is returned.
671 *
Dragan Dosen14518f22021-02-22 17:20:01 +0100672 * Returns 1 if the sample is filled, otherwise it returns 0.
Thierry FOURNIERfd77e052015-07-07 21:20:42 +0200673 */
Willy Tarreaue352b9d2021-09-03 11:52:38 +0200674int vars_get_by_desc(const struct var_desc *var_desc, struct sample *smp, const struct buffer *def)
Thierry FOURNIERfd77e052015-07-07 21:20:42 +0200675{
676 struct vars *vars;
Thierry FOURNIERfd77e052015-07-07 21:20:42 +0200677
678 /* Select "vars" pool according with the scope. */
Willy Tarreauf37b1402019-06-04 16:27:36 +0200679 vars = get_vars(smp->sess, smp->strm, var_desc->scope);
Thierry FOURNIERfd77e052015-07-07 21:20:42 +0200680
Joseph Herlant07676892018-11-15 09:19:50 -0800681 /* Check if the scope is available a this point of processing. */
Willy Tarreauf37b1402019-06-04 16:27:36 +0200682 if (!vars || vars->scope != var_desc->scope)
Thierry FOURNIERfd77e052015-07-07 21:20:42 +0200683 return 0;
684
Willy Tarreaue352b9d2021-09-03 11:52:38 +0200685 return var_to_smp(vars, var_desc->name, smp, def);
Thierry FOURNIERfd77e052015-07-07 21:20:42 +0200686}
687
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +0200688/* Always returns ACT_RET_CONT even if an error occurs. */
689static enum act_return action_store(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +0200690 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200691{
Willy Tarreau9a621ae2021-09-02 21:00:38 +0200692 struct buffer *fmtstr = NULL;
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200693 struct sample smp;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +0200694 int dir;
695
696 switch (rule->from) {
Willy Tarreau620408f2016-10-21 16:37:51 +0200697 case ACT_F_TCP_REQ_SES: dir = SMP_OPT_DIR_REQ; break;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +0200698 case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break;
699 case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break;
700 case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break;
701 case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break;
Gaetan Rivet0c39ecc2020-02-24 17:34:11 +0100702 case ACT_F_TCP_CHK: dir = SMP_OPT_DIR_REQ; break;
Willy Tarreau01d580a2021-03-26 11:11:34 +0100703 case ACT_F_CFG_PARSER: dir = SMP_OPT_DIR_REQ; break; /* not used anyway */
Willy Tarreau2f836de2021-03-26 15:36:44 +0100704 case ACT_F_CLI_PARSER: dir = SMP_OPT_DIR_REQ; break; /* not used anyway */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +0200705 default:
706 send_log(px, LOG_ERR, "Vars: internal error while execute action store.");
707 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))
Christopher Faulet767a84b2017-11-24 16:50:31 +0100708 ha_alert("Vars: internal error while execute action store.\n");
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +0200709 return ACT_RET_CONT;
710 }
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200711
712 /* Process the expression. */
713 memset(&smp, 0, sizeof(smp));
Willy Tarreau9a621ae2021-09-02 21:00:38 +0200714
715 if (!LIST_ISEMPTY(&rule->arg.vars.fmt)) {
716 /* a format-string is used */
717
718 fmtstr = alloc_trash_chunk();
719 if (!fmtstr) {
720 send_log(px, LOG_ERR, "Vars: memory allocation failure while processing store rule.");
721 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))
722 ha_alert("Vars: memory allocation failure while processing store rule.\n");
723 return ACT_RET_CONT;
724 }
725
726 /* execute the log-format expression */
727 fmtstr->data = sess_build_logline(sess, s, fmtstr->area, fmtstr->size, &rule->arg.vars.fmt);
728
729 /* convert it to a sample of type string as it's what the vars
730 * API consumes, and store it.
731 */
732 smp_set_owner(&smp, px, sess, s, 0);
733 smp.data.type = SMP_T_STR;
734 smp.data.u.str = *fmtstr;
Willy Tarreau7978c5c2021-09-07 14:24:07 +0200735 var_set(rule->arg.vars.name, rule->arg.vars.scope, &smp, 0);
Willy Tarreau9a621ae2021-09-02 21:00:38 +0200736 }
737 else {
738 /* an expression is used */
739 if (!sample_process(px, sess, s, dir|SMP_OPT_FINAL,
740 rule->arg.vars.expr, &smp))
741 return ACT_RET_CONT;
742 }
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200743
744 /* Store the sample, and ignore errors. */
Willy Tarreau7978c5c2021-09-07 14:24:07 +0200745 var_set(rule->arg.vars.name, rule->arg.vars.scope, &smp, 0);
Willy Tarreau9a621ae2021-09-02 21:00:38 +0200746 free_trash_chunk(fmtstr);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +0200747 return ACT_RET_CONT;
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200748}
749
Christopher Faulet85d79c92016-11-09 16:54:56 +0100750/* Always returns ACT_RET_CONT even if an error occurs. */
751static enum act_return action_clear(struct act_rule *rule, struct proxy *px,
752 struct session *sess, struct stream *s, int flags)
753{
754 struct sample smp;
755
756 memset(&smp, 0, sizeof(smp));
757 smp_set_owner(&smp, px, sess, s, SMP_OPT_FINAL);
758
759 /* Clear the variable using the sample context, and ignore errors. */
Willy Tarreaud378eb82021-09-07 11:44:41 +0200760 var_unset(rule->arg.vars.name, rule->arg.vars.scope, &smp);
Christopher Faulet85d79c92016-11-09 16:54:56 +0100761 return ACT_RET_CONT;
762}
763
Tim Duesterhus01a0ce32020-06-14 17:27:36 +0200764static void release_store_rule(struct act_rule *rule)
765{
Willy Tarreau9a621ae2021-09-02 21:00:38 +0200766 struct logformat_node *lf, *lfb;
Willy Tarreauc77bad22021-09-03 10:58:07 +0200767
768 list_for_each_entry_safe(lf, lfb, &rule->arg.vars.fmt, list) {
Willy Tarreau9a621ae2021-09-02 21:00:38 +0200769 LIST_DELETE(&lf->list);
770 release_sample_expr(lf->expr);
771 free(lf->arg);
772 free(lf);
773 }
774
Tim Duesterhus01a0ce32020-06-14 17:27:36 +0200775 release_sample_expr(rule->arg.vars.expr);
776}
777
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200778/* This two function checks the variable name and replace the
779 * configuration string name by the global string name. its
780 * the same string, but the global pointer can be easy to
Willy Tarreaue352b9d2021-09-03 11:52:38 +0200781 * compare. They return non-zero on success, zero on failure.
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200782 *
783 * The first function checks a sample-fetch and the second
784 * checks a converter.
785 */
786static int smp_check_var(struct arg *args, char **err)
787{
788 return vars_check_arg(&args[0], err);
789}
790
791static int conv_check_var(struct arg *args, struct sample_conv *conv,
792 const char *file, int line, char **err_msg)
793{
794 return vars_check_arg(&args[0], err_msg);
795}
796
797/* This function is a common parser for using variables. It understands
798 * the format:
799 *
Willy Tarreau9a621ae2021-09-02 21:00:38 +0200800 * set-var-fmt(<variable-name>) <format-string>
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200801 * set-var(<variable-name>) <expression>
Willy Tarreau4b7531f2019-06-04 16:43:29 +0200802 * unset-var(<variable-name>)
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200803 *
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +0200804 * It returns ACT_RET_PRS_ERR if fails and <err> is filled with an error
805 * message. Otherwise, it returns ACT_RET_PRS_OK and the variable <expr>
Willy Tarreau13d2ba22021-03-26 11:38:08 +0100806 * is filled with the pointer to the expression to execute. The proxy is
807 * only used to retrieve the ->conf entries.
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200808 */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +0200809static enum act_parse_ret parse_store(const char **args, int *arg, struct proxy *px,
810 struct act_rule *rule, char **err)
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200811{
812 const char *var_name = args[*arg-1];
813 int var_len;
Thierry FOURNIER48a9cd12015-07-28 19:00:28 +0200814 const char *kw_name;
Willy Tarreau9a621ae2021-09-02 21:00:38 +0200815 int flags, set_var = 0; /* 0=unset-var, 1=set-var, 2=set-var-fmt */
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200816
Willy Tarreau9a621ae2021-09-02 21:00:38 +0200817 if (strncmp(var_name, "set-var-fmt", 11) == 0) {
818 var_name += 11;
819 set_var = 2;
820 }
821 else if (strncmp(var_name, "set-var", 7) == 0) {
Christopher Faulet85d79c92016-11-09 16:54:56 +0100822 var_name += 7;
823 set_var = 1;
824 }
Willy Tarreau28192102021-09-02 18:46:22 +0200825 else if (strncmp(var_name, "unset-var", 9) == 0) {
Christopher Faulet85d79c92016-11-09 16:54:56 +0100826 var_name += 9;
827 set_var = 0;
828 }
829
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200830 if (*var_name != '(') {
Willy Tarreau9a621ae2021-09-02 21:00:38 +0200831 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 +0100832 args[*arg-1]);
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +0200833 return ACT_RET_PRS_ERR;
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200834 }
835 var_name++; /* jump the '(' */
836 var_len = strlen(var_name);
837 var_len--; /* remove the ')' */
838 if (var_name[var_len] != ')') {
Willy Tarreau9a621ae2021-09-02 21:00:38 +0200839 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 +0100840 args[*arg-1]);
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +0200841 return ACT_RET_PRS_ERR;
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200842 }
843
Willy Tarreau9a621ae2021-09-02 21:00:38 +0200844 LIST_INIT(&rule->arg.vars.fmt);
Christopher Faulet09c9df22016-10-31 11:05:37 +0100845 rule->arg.vars.name = register_name(var_name, var_len, &rule->arg.vars.scope, 1, err);
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +0200846 if (!rule->arg.vars.name)
847 return ACT_RET_PRS_ERR;
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200848
Christopher Faulet85d79c92016-11-09 16:54:56 +0100849 /* There is no fetch method when variable is unset. Just set the right
850 * action and return. */
851 if (!set_var) {
Christopher Faulet85d79c92016-11-09 16:54:56 +0100852 rule->action = ACT_CUSTOM;
853 rule->action_ptr = action_clear;
Tim Duesterhus01a0ce32020-06-14 17:27:36 +0200854 rule->release_ptr = release_store_rule;
Christopher Faulet85d79c92016-11-09 16:54:56 +0100855 return ACT_RET_PRS_OK;
856 }
857
Thierry FOURNIER48a9cd12015-07-28 19:00:28 +0200858 kw_name = args[*arg-1];
859
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +0200860 switch (rule->from) {
Willy Tarreau843096d2021-09-02 19:03:07 +0200861 case ACT_F_TCP_REQ_SES:
862 flags = SMP_VAL_FE_SES_ACC;
Willy Tarreau54b96d92021-09-02 19:46:08 +0200863 px->conf.args.ctx = ARGC_TSE;
Willy Tarreau843096d2021-09-02 19:03:07 +0200864 break;
865 case ACT_F_TCP_REQ_CNT:
866 flags = (px->cap & PR_CAP_FE) ? SMP_VAL_FE_REQ_CNT : SMP_VAL_BE_REQ_CNT;
Willy Tarreau54b96d92021-09-02 19:46:08 +0200867 px->conf.args.ctx = ARGC_TRQ;
Willy Tarreau843096d2021-09-02 19:03:07 +0200868 break;
869 case ACT_F_TCP_RES_CNT:
870 flags = (px->cap & PR_CAP_FE) ? SMP_VAL_FE_RES_CNT : SMP_VAL_BE_RES_CNT;
Willy Tarreau54b96d92021-09-02 19:46:08 +0200871 px->conf.args.ctx = ARGC_TRS;
Willy Tarreau843096d2021-09-02 19:03:07 +0200872 break;
873 case ACT_F_HTTP_REQ:
874 flags = (px->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR;
Willy Tarreau54b96d92021-09-02 19:46:08 +0200875 px->conf.args.ctx = ARGC_HRQ;
Willy Tarreau843096d2021-09-02 19:03:07 +0200876 break;
877 case ACT_F_HTTP_RES:
878 flags = (px->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR;
Willy Tarreau54b96d92021-09-02 19:46:08 +0200879 px->conf.args.ctx = ARGC_HRS;
Willy Tarreau843096d2021-09-02 19:03:07 +0200880 break;
881 case ACT_F_TCP_CHK:
882 flags = SMP_VAL_BE_CHK_RUL;
Willy Tarreau54b96d92021-09-02 19:46:08 +0200883 px->conf.args.ctx = ARGC_TCK;
Willy Tarreau843096d2021-09-02 19:03:07 +0200884 break;
885 case ACT_F_CFG_PARSER:
886 flags = SMP_VAL_CFG_PARSER;
Willy Tarreau54b96d92021-09-02 19:46:08 +0200887 px->conf.args.ctx = ARGC_CFG;
Willy Tarreau843096d2021-09-02 19:03:07 +0200888 break;
889 case ACT_F_CLI_PARSER:
890 flags = SMP_VAL_CLI_PARSER;
Willy Tarreau54b96d92021-09-02 19:46:08 +0200891 px->conf.args.ctx = ARGC_CLI;
Willy Tarreau843096d2021-09-02 19:03:07 +0200892 break;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +0200893 default:
894 memprintf(err,
895 "internal error, unexpected rule->from=%d, please report this bug!",
896 rule->from);
Thierry FOURNIERafa80492015-08-19 09:04:15 +0200897 return ACT_RET_PRS_ERR;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +0200898 }
Willy Tarreau54b96d92021-09-02 19:46:08 +0200899
Willy Tarreau9a621ae2021-09-02 21:00:38 +0200900 if (set_var == 2) { /* set-var-fmt */
901 if (!parse_logformat_string(args[*arg], px, &rule->arg.vars.fmt, 0, flags, err))
902 return ACT_RET_PRS_ERR;
Willy Tarreau54b96d92021-09-02 19:46:08 +0200903
Willy Tarreau9a621ae2021-09-02 21:00:38 +0200904 (*arg)++;
905
906 /* for late error reporting */
907 free(px->conf.lfs_file);
908 px->conf.lfs_file = strdup(px->conf.args.file);
909 px->conf.lfs_line = px->conf.args.line;
910 } else {
911 /* set-var */
912 rule->arg.vars.expr = sample_parse_expr((char **)args, arg, px->conf.args.file,
913 px->conf.args.line, err, &px->conf.args, NULL);
914 if (!rule->arg.vars.expr)
915 return ACT_RET_PRS_ERR;
916
917 if (!(rule->arg.vars.expr->fetch->val & flags)) {
918 memprintf(err,
919 "fetch method '%s' extracts information from '%s', none of which is available here",
920 kw_name, sample_src_names(rule->arg.vars.expr->fetch->use));
921 free(rule->arg.vars.expr);
922 return ACT_RET_PRS_ERR;
923 }
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +0200924 }
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200925
Thierry FOURNIER42148732015-09-02 17:17:33 +0200926 rule->action = ACT_CUSTOM;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +0200927 rule->action_ptr = action_store;
Tim Duesterhus01a0ce32020-06-14 17:27:36 +0200928 rule->release_ptr = release_store_rule;
Thierry FOURNIERafa80492015-08-19 09:04:15 +0200929 return ACT_RET_PRS_OK;
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200930}
931
Willy Tarreau13d2ba22021-03-26 11:38:08 +0100932
933/* parses a global "set-var" directive. It will create a temporary rule and
934 * expression that are parsed, processed, and released on the fly so that we
935 * respect the real set-var syntax. These directives take the following format:
936 * set-var <name> <expression>
Willy Tarreau753d4db2021-09-03 09:02:47 +0200937 * set-var-fmt <name> <fmt>
Willy Tarreau13d2ba22021-03-26 11:38:08 +0100938 * Note that parse_store() expects "set-var(name) <expression>" so we have to
939 * temporarily replace the keyword here.
940 */
941static int vars_parse_global_set_var(char **args, int section_type, struct proxy *curpx,
942 const struct proxy *defpx, const char *file, int line,
943 char **err)
944{
945 struct proxy px = {
Willy Tarreau9c204332021-09-03 08:19:43 +0200946 .id = "CFG",
Willy Tarreau13d2ba22021-03-26 11:38:08 +0100947 .conf.args.file = file,
948 .conf.args.line = line,
949 };
950 struct act_rule rule = {
951 .arg.vars.scope = SCOPE_PROC,
952 .from = ACT_F_CFG_PARSER,
953 };
Willy Tarreau753d4db2021-09-03 09:02:47 +0200954 enum obj_type objt = OBJ_TYPE_NONE;
955 struct session *sess = NULL;
Willy Tarreau13d2ba22021-03-26 11:38:08 +0100956 enum act_parse_ret p_ret;
957 char *old_arg1;
958 char *tmp_arg1;
959 int arg = 2; // variable name
960 int ret = -1;
Willy Tarreau753d4db2021-09-03 09:02:47 +0200961 int use_fmt = 0;
Willy Tarreau13d2ba22021-03-26 11:38:08 +0100962
963 LIST_INIT(&px.conf.args.list);
964
Willy Tarreau753d4db2021-09-03 09:02:47 +0200965 use_fmt = strcmp(args[0], "set-var-fmt") == 0;
966
Willy Tarreau13d2ba22021-03-26 11:38:08 +0100967 if (!*args[1] || !*args[2]) {
Willy Tarreau753d4db2021-09-03 09:02:47 +0200968 if (use_fmt)
969 memprintf(err, "'%s' requires a process-wide variable name ('proc.<name>') and a format string.", args[0]);
970 else
971 memprintf(err, "'%s' requires a process-wide variable name ('proc.<name>') and a sample expression.", args[0]);
Willy Tarreau13d2ba22021-03-26 11:38:08 +0100972 goto end;
973 }
974
975 tmp_arg1 = NULL;
Willy Tarreau753d4db2021-09-03 09:02:47 +0200976 if (!memprintf(&tmp_arg1, "set-var%s(%s)", use_fmt ? "-fmt" : "", args[1]))
Willy Tarreau13d2ba22021-03-26 11:38:08 +0100977 goto end;
978
979 /* parse_store() will always return a message in <err> on error */
980 old_arg1 = args[1]; args[1] = tmp_arg1;
981 p_ret = parse_store((const char **)args, &arg, &px, &rule, err);
982 free(args[1]); args[1] = old_arg1;
983
984 if (p_ret != ACT_RET_PRS_OK)
985 goto end;
986
987 if (rule.arg.vars.scope != SCOPE_PROC) {
988 memprintf(err, "'%s': cannot set variable '%s', only scope 'proc' is permitted in the global section.", args[0], args[1]);
989 goto end;
990 }
991
992 if (smp_resolve_args(&px, err) != 0) {
993 release_sample_expr(rule.arg.vars.expr);
994 indent_msg(err, 2);
995 goto end;
996 }
997
Willy Tarreau753d4db2021-09-03 09:02:47 +0200998 if (use_fmt && !(sess = session_new(&px, NULL, &objt))) {
999 release_sample_expr(rule.arg.vars.expr);
1000 memprintf(err, "'%s': out of memory when trying to set variable '%s' in the global section.", args[0], args[1]);
1001 goto end;
1002 }
1003
1004 action_store(&rule, &px, sess, NULL, 0);
Willy Tarreau13d2ba22021-03-26 11:38:08 +01001005 release_sample_expr(rule.arg.vars.expr);
Willy Tarreau753d4db2021-09-03 09:02:47 +02001006 if (sess)
1007 session_free(sess);
Willy Tarreau13d2ba22021-03-26 11:38:08 +01001008
1009 ret = 0;
1010 end:
1011 return ret;
1012}
1013
Willy Tarreauc35eb382021-03-26 14:51:31 +01001014/* parse CLI's "get var <name>" */
1015static int vars_parse_cli_get_var(char **args, char *payload, struct appctx *appctx, void *private)
1016{
1017 struct vars *vars;
Willy Tarreau374edc72021-04-01 17:01:43 +02001018 struct sample smp = { };
Willy Tarreauc35eb382021-03-26 14:51:31 +01001019 int i;
1020
1021 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
1022 return 1;
1023
1024 if (!*args[2])
1025 return cli_err(appctx, "Missing process-wide variable identifier.\n");
1026
1027 vars = get_vars(NULL, NULL, SCOPE_PROC);
1028 if (!vars || vars->scope != SCOPE_PROC)
1029 return 0;
1030
Willy Tarreaue352b9d2021-09-03 11:52:38 +02001031 if (!vars_get_by_name(args[2], strlen(args[2]), &smp, NULL))
Willy Tarreauc35eb382021-03-26 14:51:31 +01001032 return cli_err(appctx, "Variable not found.\n");
1033
1034 /* the sample returned by vars_get_by_name() is allocated into a trash
1035 * chunk so we have no constraint to manipulate it.
1036 */
1037 chunk_printf(&trash, "%s: type=%s value=", args[2], smp_to_type[smp.data.type]);
1038
1039 if (!sample_casts[smp.data.type][SMP_T_STR] ||
1040 !sample_casts[smp.data.type][SMP_T_STR](&smp)) {
1041 chunk_appendf(&trash, "(undisplayable)");
1042 } else {
1043 /* Display the displayable chars*. */
1044 b_putchr(&trash, '<');
1045 for (i = 0; i < smp.data.u.str.data; i++) {
1046 if (isprint((unsigned char)smp.data.u.str.area[i]))
1047 b_putchr(&trash, smp.data.u.str.area[i]);
1048 else
1049 b_putchr(&trash, '.');
1050 }
1051 b_putchr(&trash, '>');
1052 b_putchr(&trash, 0);
1053 }
1054 return cli_msg(appctx, LOG_INFO, trash.area);
1055}
1056
Willy Tarreaue93bff42021-09-03 09:47:37 +02001057/* parse CLI's "set var <name>". It accepts:
1058 * - set var <name> <expression>
1059 * - set var <name> expr <expression>
1060 * - set var <name> fmt <format>
1061 */
Willy Tarreaub8bd1ee2021-03-26 15:19:50 +01001062static int vars_parse_cli_set_var(char **args, char *payload, struct appctx *appctx, void *private)
1063{
1064 struct proxy px = {
1065 .id = "CLI",
1066 .conf.args.file = "CLI",
1067 .conf.args.line = 0,
1068 };
1069 struct act_rule rule = {
1070 .arg.vars.scope = SCOPE_PROC,
1071 .from = ACT_F_CLI_PARSER,
1072 };
Willy Tarreaue93bff42021-09-03 09:47:37 +02001073 enum obj_type objt = OBJ_TYPE_NONE;
1074 struct session *sess = NULL;
Willy Tarreaub8bd1ee2021-03-26 15:19:50 +01001075 enum act_parse_ret p_ret;
Willy Tarreaue93bff42021-09-03 09:47:37 +02001076 const char *tmp_args[3];
1077 int tmp_arg;
1078 char *tmp_act;
Willy Tarreaub8bd1ee2021-03-26 15:19:50 +01001079 char *err = NULL;
Willy Tarreaub8bd1ee2021-03-26 15:19:50 +01001080 int nberr;
Willy Tarreaue93bff42021-09-03 09:47:37 +02001081 int use_fmt = 0;
Willy Tarreaub8bd1ee2021-03-26 15:19:50 +01001082
1083 LIST_INIT(&px.conf.args.list);
1084
1085 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
1086 return 1;
1087
Willy Tarreaue93bff42021-09-03 09:47:37 +02001088 if (!*args[2])
1089 return cli_err(appctx, "Missing process-wide variable identifier.\n");
Willy Tarreaub8bd1ee2021-03-26 15:19:50 +01001090
Willy Tarreaue93bff42021-09-03 09:47:37 +02001091 if (!*args[3])
1092 return cli_err(appctx, "Missing either 'expr', 'fmt' or expression.\n");
1093
1094 if (*args[4]) {
1095 /* this is the long format */
1096 if (strcmp(args[3], "fmt") == 0)
1097 use_fmt = 1;
1098 else if (strcmp(args[3], "expr") != 0) {
1099 memprintf(&err, "'%s %s': arg type must be either 'expr' or 'fmt' but got '%s'.", args[0], args[1], args[3]);
1100 goto fail;
1101 }
1102 }
1103
1104 tmp_act = NULL;
1105 if (!memprintf(&tmp_act, "set-var%s(%s)", use_fmt ? "-fmt" : "", args[2])) {
Willy Tarreaub8bd1ee2021-03-26 15:19:50 +01001106 memprintf(&err, "memory allocation error.");
1107 goto fail;
1108 }
1109
1110 /* parse_store() will always return a message in <err> on error */
Willy Tarreaue93bff42021-09-03 09:47:37 +02001111 tmp_args[0] = tmp_act;
1112 tmp_args[1] = (*args[4]) ? args[4] : args[3];
1113 tmp_args[2] = "";
1114 tmp_arg = 1; // must point to the first arg after the action
1115 p_ret = parse_store(tmp_args, &tmp_arg, &px, &rule, &err);
1116 free(tmp_act);
Willy Tarreaub8bd1ee2021-03-26 15:19:50 +01001117
1118 if (p_ret != ACT_RET_PRS_OK)
1119 goto fail;
1120
1121 if (rule.arg.vars.scope != SCOPE_PROC) {
Willy Tarreauc767eeb2021-09-03 10:23:26 +02001122 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 +01001123 goto fail;
1124 }
1125
1126 err = NULL;
1127 nberr = smp_resolve_args(&px, &err);
1128 if (nberr) {
1129 release_sample_expr(rule.arg.vars.expr);
1130 indent_msg(&err, 2);
1131 goto fail;
1132 }
1133
Willy Tarreaue93bff42021-09-03 09:47:37 +02001134 if (use_fmt && !(sess = session_new(&px, NULL, &objt))) {
1135 release_sample_expr(rule.arg.vars.expr);
1136 memprintf(&err, "memory allocation error.");
1137 goto fail;
1138 }
1139
1140 action_store(&rule, &px, sess, NULL, 0);
Willy Tarreaub8bd1ee2021-03-26 15:19:50 +01001141 release_sample_expr(rule.arg.vars.expr);
Willy Tarreaue93bff42021-09-03 09:47:37 +02001142 if (sess)
1143 session_free(sess);
1144
Willy Tarreaub8bd1ee2021-03-26 15:19:50 +01001145 appctx->st0 = CLI_ST_PROMPT;
1146 return 0;
1147 fail:
1148 return cli_dynerr(appctx, err);
1149}
1150
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02001151static int vars_max_size(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01001152 const struct proxy *defpx, const char *file, int line,
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02001153 char **err, unsigned int *limit)
1154{
1155 char *error;
1156
1157 *limit = strtol(args[1], &error, 10);
1158 if (*error != 0) {
1159 memprintf(err, "%s: '%s' is an invalid size", args[0], args[1]);
1160 return -1;
1161 }
1162 return 0;
1163}
1164
1165static int vars_max_size_global(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01001166 const struct proxy *defpx, const char *file, int line,
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02001167 char **err)
1168{
1169 return vars_max_size(args, section_type, curpx, defpx, file, line, err, &var_global_limit);
1170}
1171
Christopher Fauletff2613e2016-11-09 11:36:17 +01001172static int vars_max_size_proc(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01001173 const struct proxy *defpx, const char *file, int line,
Christopher Fauletff2613e2016-11-09 11:36:17 +01001174 char **err)
1175{
1176 return vars_max_size(args, section_type, curpx, defpx, file, line, err, &var_proc_limit);
1177}
1178
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02001179static int vars_max_size_sess(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01001180 const struct proxy *defpx, const char *file, int line,
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02001181 char **err)
1182{
1183 return vars_max_size(args, section_type, curpx, defpx, file, line, err, &var_sess_limit);
1184}
1185
1186static int vars_max_size_txn(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01001187 const struct proxy *defpx, const char *file, int line,
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02001188 char **err)
1189{
1190 return vars_max_size(args, section_type, curpx, defpx, file, line, err, &var_txn_limit);
1191}
1192
1193static int vars_max_size_reqres(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01001194 const struct proxy *defpx, const char *file, int line,
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02001195 char **err)
1196{
1197 return vars_max_size(args, section_type, curpx, defpx, file, line, err, &var_reqres_limit);
1198}
1199
Gaetan Rivet13a50432020-02-21 18:13:44 +01001200static int vars_max_size_check(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01001201 const struct proxy *defpx, const char *file, int line,
Gaetan Rivet13a50432020-02-21 18:13:44 +01001202 char **err)
1203{
1204 return vars_max_size(args, section_type, curpx, defpx, file, line, err, &var_check_limit);
1205}
1206
Tim Duesterhusbbdd5b82020-07-04 11:53:25 +02001207static void vars_deinit()
1208{
1209 while (var_names_nb-- > 0)
1210 free(var_names[var_names_nb]);
1211 free(var_names);
1212}
1213
1214REGISTER_POST_DEINIT(vars_deinit);
1215
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02001216static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
1217
Willy Tarreau54496a62021-09-03 12:00:13 +02001218 { "var", smp_fetch_var, ARG2(1,STR,STR), smp_check_var, SMP_T_STR, SMP_USE_CONST },
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02001219 { /* END */ },
1220}};
1221
Willy Tarreau0108d902018-11-25 19:14:37 +01001222INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);
1223
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02001224static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Christopher Faulet85d79c92016-11-09 16:54:56 +01001225 { "set-var", smp_conv_store, ARG1(1,STR), conv_check_var, SMP_T_ANY, SMP_T_ANY },
1226 { "unset-var", smp_conv_clear, ARG1(1,STR), conv_check_var, SMP_T_ANY, SMP_T_ANY },
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02001227 { /* END */ },
1228}};
1229
Willy Tarreau0108d902018-11-25 19:14:37 +01001230INITCALL1(STG_REGISTER, sample_register_convs, &sample_conv_kws);
1231
Willy Tarreau620408f2016-10-21 16:37:51 +02001232static struct action_kw_list tcp_req_sess_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 },
Willy Tarreau620408f2016-10-21 16:37:51 +02001236 { /* END */ }
1237}};
1238
Willy Tarreau0108d902018-11-25 19:14:37 +01001239INITCALL1(STG_REGISTER, tcp_req_sess_keywords_register, &tcp_req_sess_kws);
1240
Willy Tarreau620408f2016-10-21 16:37:51 +02001241static struct action_kw_list tcp_req_cont_kws = { { }, {
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 },
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02001245 { /* END */ }
1246}};
1247
Willy Tarreau0108d902018-11-25 19:14:37 +01001248INITCALL1(STG_REGISTER, tcp_req_cont_keywords_register, &tcp_req_cont_kws);
1249
Thierry FOURNIER36481b82015-08-19 09:01:53 +02001250static struct action_kw_list tcp_res_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, tcp_res_cont_keywords_register, &tcp_res_kws);
1258
Gaetan Rivet707b52f2020-02-21 18:14:59 +01001259static struct action_kw_list tcp_check_kws = {ILH, {
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 },
Gaetan Rivet707b52f2020-02-21 18:14:59 +01001263 { /* END */ }
1264}};
1265
1266INITCALL1(STG_REGISTER, tcp_check_keywords_register, &tcp_check_kws);
1267
Thierry FOURNIER36481b82015-08-19 09:01:53 +02001268static struct action_kw_list http_req_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 },
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02001272 { /* END */ }
1273}};
1274
Willy Tarreau0108d902018-11-25 19:14:37 +01001275INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_kws);
1276
Thierry FOURNIER36481b82015-08-19 09:01:53 +02001277static struct action_kw_list http_res_kws = { { }, {
Willy Tarreau9a621ae2021-09-02 21:00:38 +02001278 { "set-var-fmt", parse_store, KWF_MATCH_PREFIX },
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02001279 { "set-var", parse_store, KWF_MATCH_PREFIX },
1280 { "unset-var", parse_store, KWF_MATCH_PREFIX },
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02001281 { /* END */ }
1282}};
1283
Willy Tarreau0108d902018-11-25 19:14:37 +01001284INITCALL1(STG_REGISTER, http_res_keywords_register, &http_res_kws);
1285
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01001286static struct action_kw_list http_after_res_kws = { { }, {
Willy Tarreau9a621ae2021-09-02 21:00:38 +02001287 { "set-var-fmt", parse_store, KWF_MATCH_PREFIX },
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02001288 { "set-var", parse_store, KWF_MATCH_PREFIX },
1289 { "unset-var", parse_store, KWF_MATCH_PREFIX },
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01001290 { /* END */ }
1291}};
1292
1293INITCALL1(STG_REGISTER, http_after_res_keywords_register, &http_after_res_kws);
1294
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02001295static struct cfg_kw_list cfg_kws = {{ },{
Willy Tarreau13d2ba22021-03-26 11:38:08 +01001296 { CFG_GLOBAL, "set-var", vars_parse_global_set_var },
Willy Tarreau753d4db2021-09-03 09:02:47 +02001297 { CFG_GLOBAL, "set-var-fmt", vars_parse_global_set_var },
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02001298 { CFG_GLOBAL, "tune.vars.global-max-size", vars_max_size_global },
Christopher Fauletff2613e2016-11-09 11:36:17 +01001299 { CFG_GLOBAL, "tune.vars.proc-max-size", vars_max_size_proc },
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02001300 { CFG_GLOBAL, "tune.vars.sess-max-size", vars_max_size_sess },
1301 { CFG_GLOBAL, "tune.vars.txn-max-size", vars_max_size_txn },
1302 { CFG_GLOBAL, "tune.vars.reqres-max-size", vars_max_size_reqres },
Gaetan Rivet13a50432020-02-21 18:13:44 +01001303 { CFG_GLOBAL, "tune.vars.check-max-size", vars_max_size_check },
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02001304 { /* END */ }
1305}};
1306
Willy Tarreau0108d902018-11-25 19:14:37 +01001307INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
Willy Tarreauc35eb382021-03-26 14:51:31 +01001308
1309
1310/* register cli keywords */
1311static struct cli_kw_list cli_kws = {{ },{
Willy Tarreaub205bfd2021-05-07 11:38:37 +02001312 { { "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 +02001313 { { "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 +01001314 { { NULL }, NULL, NULL, NULL }
1315}};
1316INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);