Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 1 | #include <ctype.h> |
| 2 | |
Willy Tarreau | 4c7e4b7 | 2020-05-27 12:58:42 +0200 | [diff] [blame] | 3 | #include <haproxy/api.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 4 | #include <haproxy/arg.h> |
Willy Tarreau | c35eb38 | 2021-03-26 14:51:31 +0100 | [diff] [blame] | 5 | #include <haproxy/buf.h> |
Willy Tarreau | 6be7849 | 2020-06-05 00:00:29 +0200 | [diff] [blame] | 6 | #include <haproxy/cfgparse.h> |
Willy Tarreau | 4aa573d | 2020-06-04 18:21:56 +0200 | [diff] [blame] | 7 | #include <haproxy/check.h> |
Willy Tarreau | c35eb38 | 2021-03-26 14:51:31 +0100 | [diff] [blame] | 8 | #include <haproxy/cli.h> |
Willy Tarreau | dfd3de8 | 2020-06-04 23:46:14 +0200 | [diff] [blame] | 9 | #include <haproxy/global.h> |
Willy Tarreau | cd72d8c | 2020-06-02 19:11:26 +0200 | [diff] [blame] | 10 | #include <haproxy/http.h> |
Willy Tarreau | c761f84 | 2020-06-04 11:40:28 +0200 | [diff] [blame] | 11 | #include <haproxy/http_rules.h> |
Willy Tarreau | 853b297 | 2020-05-27 18:01:47 +0200 | [diff] [blame] | 12 | #include <haproxy/list.h> |
Willy Tarreau | 36979d9 | 2020-06-05 17:27:29 +0200 | [diff] [blame] | 13 | #include <haproxy/log.h> |
Willy Tarreau | e6ce10b | 2020-06-04 15:33:47 +0200 | [diff] [blame] | 14 | #include <haproxy/sample.h> |
Willy Tarreau | 753d4db | 2021-09-03 09:02:47 +0200 | [diff] [blame] | 15 | #include <haproxy/session.h> |
Willy Tarreau | dfd3de8 | 2020-06-04 23:46:14 +0200 | [diff] [blame] | 16 | #include <haproxy/stream-t.h> |
Willy Tarreau | 8b550af | 2020-06-04 17:42:48 +0200 | [diff] [blame] | 17 | #include <haproxy/tcp_rules.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 18 | #include <haproxy/tcpcheck.h> |
Willy Tarreau | 67046bf | 2021-05-08 13:56:31 +0200 | [diff] [blame] | 19 | #include <haproxy/tools.h> |
Willy Tarreau | a171892 | 2020-06-04 16:25:31 +0200 | [diff] [blame] | 20 | #include <haproxy/vars.h> |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 21 | |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 22 | |
| 23 | /* This contains a pool of struct vars */ |
Willy Tarreau | 8ceae72 | 2018-11-26 11:58:30 +0100 | [diff] [blame] | 24 | DECLARE_STATIC_POOL(var_pool, "vars", sizeof(struct var)); |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 25 | |
Willy Tarreau | cfc4f24 | 2021-05-08 11:41:28 +0200 | [diff] [blame] | 26 | /* list of variables for the process scope. */ |
| 27 | struct vars proc_vars THREAD_ALIGNED(64); |
| 28 | |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 29 | /* This array of int contains the system limits per context. */ |
| 30 | static unsigned int var_global_limit = 0; |
Christopher Faulet | ff2613e | 2016-11-09 11:36:17 +0100 | [diff] [blame] | 31 | static unsigned int var_proc_limit = 0; |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 32 | static unsigned int var_sess_limit = 0; |
| 33 | static unsigned int var_txn_limit = 0; |
| 34 | static unsigned int var_reqres_limit = 0; |
Gaetan Rivet | 13a5043 | 2020-02-21 18:13:44 +0100 | [diff] [blame] | 35 | static unsigned int var_check_limit = 0; |
Willy Tarreau | 2c897d9 | 2021-08-31 08:48:55 +0200 | [diff] [blame] | 36 | static uint64_t var_name_hash_seed = 0; |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 37 | |
Willy Tarreau | f37b140 | 2019-06-04 16:27:36 +0200 | [diff] [blame] | 38 | /* returns the struct vars pointer for a session, stream and scope, or NULL if |
| 39 | * it does not exist. |
| 40 | */ |
| 41 | static inline struct vars *get_vars(struct session *sess, struct stream *strm, enum vars_scope scope) |
| 42 | { |
| 43 | switch (scope) { |
| 44 | case SCOPE_PROC: |
Willy Tarreau | cfc4f24 | 2021-05-08 11:41:28 +0200 | [diff] [blame] | 45 | return &proc_vars; |
Willy Tarreau | f37b140 | 2019-06-04 16:27:36 +0200 | [diff] [blame] | 46 | case SCOPE_SESS: |
Willy Tarreau | a07d61b | 2021-03-26 11:27:59 +0100 | [diff] [blame] | 47 | return sess ? &sess->vars : NULL; |
Gaetan Rivet | 13a5043 | 2020-02-21 18:13:44 +0100 | [diff] [blame] | 48 | case SCOPE_CHECK: { |
Christopher Faulet | c4439f7 | 2021-06-02 11:48:42 +0200 | [diff] [blame] | 49 | struct check *check = sess ? objt_check(sess->origin) : NULL; |
Gaetan Rivet | 13a5043 | 2020-02-21 18:13:44 +0100 | [diff] [blame] | 50 | |
Christopher Faulet | 0fca7ed | 2020-04-21 11:53:32 +0200 | [diff] [blame] | 51 | return check ? &check->vars : NULL; |
Gaetan Rivet | 13a5043 | 2020-02-21 18:13:44 +0100 | [diff] [blame] | 52 | } |
Willy Tarreau | f37b140 | 2019-06-04 16:27:36 +0200 | [diff] [blame] | 53 | case SCOPE_TXN: |
| 54 | return strm ? &strm->vars_txn : NULL; |
| 55 | case SCOPE_REQ: |
| 56 | case SCOPE_RES: |
| 57 | default: |
| 58 | return strm ? &strm->vars_reqres : NULL; |
| 59 | } |
| 60 | } |
| 61 | |
Willy Tarreau | 7233098 | 2015-06-19 11:21:56 +0200 | [diff] [blame] | 62 | /* This function adds or remove memory size from the accounting. The inner |
| 63 | * pointers may be null when setting the outer ones only. |
| 64 | */ |
Miroslav Zagorac | 6deab79 | 2020-12-09 16:34:29 +0100 | [diff] [blame] | 65 | void var_accounting_diff(struct vars *vars, struct session *sess, struct stream *strm, int size) |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 66 | { |
| 67 | switch (vars->scope) { |
| 68 | case SCOPE_REQ: |
| 69 | case SCOPE_RES: |
Willy Tarreau | 55f8a83 | 2021-09-08 15:51:06 +0200 | [diff] [blame^] | 70 | if (var_reqres_limit && strm) |
Willy Tarreau | f37b140 | 2019-06-04 16:27:36 +0200 | [diff] [blame] | 71 | _HA_ATOMIC_ADD(&strm->vars_reqres.size, size); |
Willy Tarreau | 6204cd9 | 2016-03-10 16:33:04 +0100 | [diff] [blame] | 72 | /* fall through */ |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 73 | case SCOPE_TXN: |
Willy Tarreau | 55f8a83 | 2021-09-08 15:51:06 +0200 | [diff] [blame^] | 74 | if (var_txn_limit && strm) |
Willy Tarreau | f37b140 | 2019-06-04 16:27:36 +0200 | [diff] [blame] | 75 | _HA_ATOMIC_ADD(&strm->vars_txn.size, size); |
Gaetan Rivet | 13a5043 | 2020-02-21 18:13:44 +0100 | [diff] [blame] | 76 | goto scope_sess; |
Willy Tarreau | 55f8a83 | 2021-09-08 15:51:06 +0200 | [diff] [blame^] | 77 | case SCOPE_CHECK: |
| 78 | if (var_check_limit) { |
Christopher Faulet | 0fca7ed | 2020-04-21 11:53:32 +0200 | [diff] [blame] | 79 | struct check *check = objt_check(sess->origin); |
Gaetan Rivet | 13a5043 | 2020-02-21 18:13:44 +0100 | [diff] [blame] | 80 | |
Christopher Faulet | 0fca7ed | 2020-04-21 11:53:32 +0200 | [diff] [blame] | 81 | if (check) |
| 82 | _HA_ATOMIC_ADD(&check->vars.size, size); |
Gaetan Rivet | 13a5043 | 2020-02-21 18:13:44 +0100 | [diff] [blame] | 83 | } |
Willy Tarreau | 6204cd9 | 2016-03-10 16:33:04 +0100 | [diff] [blame] | 84 | /* fall through */ |
Gaetan Rivet | 13a5043 | 2020-02-21 18:13:44 +0100 | [diff] [blame] | 85 | scope_sess: |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 86 | case SCOPE_SESS: |
Willy Tarreau | 55f8a83 | 2021-09-08 15:51:06 +0200 | [diff] [blame^] | 87 | if (var_sess_limit) |
| 88 | _HA_ATOMIC_ADD(&sess->vars.size, size); |
Christopher Faulet | ff2613e | 2016-11-09 11:36:17 +0100 | [diff] [blame] | 89 | /* fall through */ |
| 90 | case SCOPE_PROC: |
Willy Tarreau | 55f8a83 | 2021-09-08 15:51:06 +0200 | [diff] [blame^] | 91 | if (var_proc_limit || var_global_limit) |
| 92 | _HA_ATOMIC_ADD(&proc_vars.size, size); |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 93 | } |
| 94 | } |
| 95 | |
| 96 | /* This function returns 1 if the <size> is available in the var |
Joseph Herlant | 0767689 | 2018-11-15 09:19:50 -0800 | [diff] [blame] | 97 | * pool <vars>, otherwise returns 0. If the space is available, |
Willy Tarreau | 7233098 | 2015-06-19 11:21:56 +0200 | [diff] [blame] | 98 | * the size is reserved. The inner pointers may be null when setting |
Willy Tarreau | 6204cd9 | 2016-03-10 16:33:04 +0100 | [diff] [blame] | 99 | * the outer ones only. The accounting uses either <sess> or <strm> |
| 100 | * depending on the scope. <strm> may be NULL when no stream is known |
| 101 | * and only the session exists (eg: tcp-request connection). |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 102 | */ |
Willy Tarreau | 6204cd9 | 2016-03-10 16:33:04 +0100 | [diff] [blame] | 103 | static int var_accounting_add(struct vars *vars, struct session *sess, struct stream *strm, int size) |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 104 | { |
| 105 | switch (vars->scope) { |
| 106 | case SCOPE_REQ: |
| 107 | case SCOPE_RES: |
Willy Tarreau | f37b140 | 2019-06-04 16:27:36 +0200 | [diff] [blame] | 108 | if (var_reqres_limit && strm && strm->vars_reqres.size + size > var_reqres_limit) |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 109 | return 0; |
Willy Tarreau | 6204cd9 | 2016-03-10 16:33:04 +0100 | [diff] [blame] | 110 | /* fall through */ |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 111 | case SCOPE_TXN: |
Willy Tarreau | f37b140 | 2019-06-04 16:27:36 +0200 | [diff] [blame] | 112 | if (var_txn_limit && strm && strm->vars_txn.size + size > var_txn_limit) |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 113 | return 0; |
Gaetan Rivet | 13a5043 | 2020-02-21 18:13:44 +0100 | [diff] [blame] | 114 | goto scope_sess; |
| 115 | case SCOPE_CHECK: { |
Christopher Faulet | 0fca7ed | 2020-04-21 11:53:32 +0200 | [diff] [blame] | 116 | struct check *check = objt_check(sess->origin); |
Gaetan Rivet | 13a5043 | 2020-02-21 18:13:44 +0100 | [diff] [blame] | 117 | |
Christopher Faulet | 0fca7ed | 2020-04-21 11:53:32 +0200 | [diff] [blame] | 118 | if (var_check_limit && check && check->vars.size + size > var_check_limit) |
Gaetan Rivet | 13a5043 | 2020-02-21 18:13:44 +0100 | [diff] [blame] | 119 | return 0; |
| 120 | } |
Willy Tarreau | 6204cd9 | 2016-03-10 16:33:04 +0100 | [diff] [blame] | 121 | /* fall through */ |
Gaetan Rivet | 13a5043 | 2020-02-21 18:13:44 +0100 | [diff] [blame] | 122 | scope_sess: |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 123 | case SCOPE_SESS: |
Willy Tarreau | 6204cd9 | 2016-03-10 16:33:04 +0100 | [diff] [blame] | 124 | if (var_sess_limit && sess->vars.size + size > var_sess_limit) |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 125 | return 0; |
Christopher Faulet | ff2613e | 2016-11-09 11:36:17 +0100 | [diff] [blame] | 126 | /* fall through */ |
| 127 | case SCOPE_PROC: |
Willy Tarreau | 3b78f2a | 2021-09-08 15:40:58 +0200 | [diff] [blame] | 128 | /* note: scope proc collects all others and is currently identical to the |
| 129 | * global limit. |
| 130 | */ |
Willy Tarreau | cfc4f24 | 2021-05-08 11:41:28 +0200 | [diff] [blame] | 131 | if (var_proc_limit && proc_vars.size + size > var_proc_limit) |
Christopher Faulet | ff2613e | 2016-11-09 11:36:17 +0100 | [diff] [blame] | 132 | return 0; |
Willy Tarreau | 3b78f2a | 2021-09-08 15:40:58 +0200 | [diff] [blame] | 133 | if (var_global_limit && proc_vars.size + size > var_global_limit) |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 134 | return 0; |
| 135 | } |
Willy Tarreau | 6204cd9 | 2016-03-10 16:33:04 +0100 | [diff] [blame] | 136 | var_accounting_diff(vars, sess, strm, size); |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 137 | return 1; |
| 138 | } |
| 139 | |
Willy Tarreau | c1c88f4 | 2021-09-08 15:03:58 +0200 | [diff] [blame] | 140 | /* This function removes a variable from the list and frees the memory it was |
| 141 | * using. If the variable is marked "VF_PERMANENT", the sample_data is only |
| 142 | * reset to SMP_T_ANY unless <force> is non nul. Returns the freed size. |
| 143 | */ |
| 144 | unsigned int var_clear(struct var *var, int force) |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 145 | { |
| 146 | unsigned int size = 0; |
| 147 | |
| 148 | if (var->data.type == SMP_T_STR || var->data.type == SMP_T_BIN) { |
Willy Tarreau | 5b52b00 | 2021-02-26 21:19:53 +0100 | [diff] [blame] | 149 | ha_free(&var->data.u.str.area); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 150 | size += var->data.u.str.data; |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 151 | } |
Christopher Faulet | d02210c | 2017-07-24 16:24:39 +0200 | [diff] [blame] | 152 | else if (var->data.type == SMP_T_METH && var->data.u.meth.meth == HTTP_METH_OTHER) { |
Willy Tarreau | 5b52b00 | 2021-02-26 21:19:53 +0100 | [diff] [blame] | 153 | ha_free(&var->data.u.meth.str.area); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 154 | size += var->data.u.meth.str.data; |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 155 | } |
Willy Tarreau | c1c88f4 | 2021-09-08 15:03:58 +0200 | [diff] [blame] | 156 | /* wipe the sample */ |
| 157 | var->data.type = SMP_T_ANY; |
| 158 | |
| 159 | if (!(var->flags & VF_PERMANENT) || force) { |
| 160 | LIST_DELETE(&var->l); |
| 161 | pool_free(var_pool, var); |
| 162 | size += sizeof(struct var); |
| 163 | } |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 164 | return size; |
| 165 | } |
| 166 | |
Joseph Herlant | 0767689 | 2018-11-15 09:19:50 -0800 | [diff] [blame] | 167 | /* This function free all the memory used by all the variables |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 168 | * in the list. |
| 169 | */ |
Willy Tarreau | 6204cd9 | 2016-03-10 16:33:04 +0100 | [diff] [blame] | 170 | void vars_prune(struct vars *vars, struct session *sess, struct stream *strm) |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 171 | { |
| 172 | struct var *var, *tmp; |
Willy Tarreau | 7233098 | 2015-06-19 11:21:56 +0200 | [diff] [blame] | 173 | unsigned int size = 0; |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 174 | |
Willy Tarreau | dc72fbb | 2021-09-08 15:19:57 +0200 | [diff] [blame] | 175 | vars_wrlock(vars); |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 176 | list_for_each_entry_safe(var, tmp, &vars->head, l) { |
Willy Tarreau | c1c88f4 | 2021-09-08 15:03:58 +0200 | [diff] [blame] | 177 | size += var_clear(var, 1); |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 178 | } |
Willy Tarreau | dc72fbb | 2021-09-08 15:19:57 +0200 | [diff] [blame] | 179 | vars_wrunlock(vars); |
Willy Tarreau | 6204cd9 | 2016-03-10 16:33:04 +0100 | [diff] [blame] | 180 | var_accounting_diff(vars, sess, strm, -size); |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 181 | } |
| 182 | |
Willy Tarreau | ebcd484 | 2015-06-19 11:59:02 +0200 | [diff] [blame] | 183 | /* This function frees all the memory used by all the session variables in the |
| 184 | * list starting at <vars>. |
| 185 | */ |
| 186 | void vars_prune_per_sess(struct vars *vars) |
| 187 | { |
| 188 | struct var *var, *tmp; |
| 189 | unsigned int size = 0; |
| 190 | |
Willy Tarreau | dc72fbb | 2021-09-08 15:19:57 +0200 | [diff] [blame] | 191 | vars_wrlock(vars); |
Willy Tarreau | ebcd484 | 2015-06-19 11:59:02 +0200 | [diff] [blame] | 192 | list_for_each_entry_safe(var, tmp, &vars->head, l) { |
Willy Tarreau | c1c88f4 | 2021-09-08 15:03:58 +0200 | [diff] [blame] | 193 | size += var_clear(var, 1); |
Willy Tarreau | ebcd484 | 2015-06-19 11:59:02 +0200 | [diff] [blame] | 194 | } |
Willy Tarreau | dc72fbb | 2021-09-08 15:19:57 +0200 | [diff] [blame] | 195 | vars_wrunlock(vars); |
Christopher Faulet | e95f2c3 | 2017-07-24 16:30:34 +0200 | [diff] [blame] | 196 | |
Willy Tarreau | 55f8a83 | 2021-09-08 15:51:06 +0200 | [diff] [blame^] | 197 | if (var_sess_limit) |
| 198 | _HA_ATOMIC_SUB(&vars->size, size); |
| 199 | if (var_proc_limit || var_global_limit) |
| 200 | _HA_ATOMIC_SUB(&proc_vars.size, size); |
Willy Tarreau | ebcd484 | 2015-06-19 11:59:02 +0200 | [diff] [blame] | 201 | } |
| 202 | |
Willy Tarreau | b7bfcb3 | 2021-08-31 08:13:25 +0200 | [diff] [blame] | 203 | /* This function initializes a variables list head */ |
| 204 | void vars_init_head(struct vars *vars, enum vars_scope scope) |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 205 | { |
| 206 | LIST_INIT(&vars->head); |
| 207 | vars->scope = scope; |
| 208 | vars->size = 0; |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 209 | HA_RWLOCK_INIT(&vars->rwlock); |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 210 | } |
| 211 | |
Willy Tarreau | 3a4bedc | 2021-08-31 08:51:02 +0200 | [diff] [blame] | 212 | /* This function returns a hash value and a scope for a variable name of a |
| 213 | * specified length. It makes sure that the scope is valid. It returns non-zero |
| 214 | * on success, 0 on failure. Neither hash nor scope may be NULL. |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 215 | */ |
Willy Tarreau | 3a4bedc | 2021-08-31 08:51:02 +0200 | [diff] [blame] | 216 | static int vars_hash_name(const char *name, int len, enum vars_scope *scope, |
| 217 | uint64_t *hash, char **err) |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 218 | { |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 219 | const char *tmp; |
| 220 | |
| 221 | /* Check length. */ |
| 222 | if (len == 0) { |
| 223 | memprintf(err, "Empty variable name cannot be accepted"); |
Willy Tarreau | 3a4bedc | 2021-08-31 08:51:02 +0200 | [diff] [blame] | 224 | return 0; |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | /* Check scope. */ |
Christopher Faulet | ff2613e | 2016-11-09 11:36:17 +0100 | [diff] [blame] | 228 | if (len > 5 && strncmp(name, "proc.", 5) == 0) { |
| 229 | name += 5; |
| 230 | len -= 5; |
| 231 | *scope = SCOPE_PROC; |
| 232 | } |
| 233 | else if (len > 5 && strncmp(name, "sess.", 5) == 0) { |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 234 | name += 5; |
| 235 | len -= 5; |
| 236 | *scope = SCOPE_SESS; |
| 237 | } |
| 238 | else if (len > 4 && strncmp(name, "txn.", 4) == 0) { |
| 239 | name += 4; |
| 240 | len -= 4; |
| 241 | *scope = SCOPE_TXN; |
| 242 | } |
| 243 | else if (len > 4 && strncmp(name, "req.", 4) == 0) { |
| 244 | name += 4; |
| 245 | len -= 4; |
| 246 | *scope = SCOPE_REQ; |
| 247 | } |
| 248 | else if (len > 4 && strncmp(name, "res.", 4) == 0) { |
| 249 | name += 4; |
| 250 | len -= 4; |
| 251 | *scope = SCOPE_RES; |
| 252 | } |
Gaetan Rivet | 13a5043 | 2020-02-21 18:13:44 +0100 | [diff] [blame] | 253 | else if (len > 6 && strncmp(name, "check.", 6) == 0) { |
| 254 | name += 6; |
| 255 | len -= 6; |
| 256 | *scope = SCOPE_CHECK; |
| 257 | } |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 258 | else { |
Willy Tarreau | 1402fef | 2021-09-03 10:12:55 +0200 | [diff] [blame] | 259 | memprintf(err, "invalid variable name '%.*s'. A variable name must be start by its scope. " |
| 260 | "The scope can be 'proc', 'sess', 'txn', 'req', 'res' or 'check'", len, name); |
Willy Tarreau | 3a4bedc | 2021-08-31 08:51:02 +0200 | [diff] [blame] | 261 | return 0; |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 262 | } |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 263 | |
| 264 | /* Check variable name syntax. */ |
Willy Tarreau | 3a4bedc | 2021-08-31 08:51:02 +0200 | [diff] [blame] | 265 | for (tmp = name; tmp < name + len; tmp++) { |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 266 | if (!isalnum((unsigned char)*tmp) && *tmp != '_' && *tmp != '.') { |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 267 | memprintf(err, "invalid syntax at char '%s'", tmp); |
Willy Tarreau | 3a4bedc | 2021-08-31 08:51:02 +0200 | [diff] [blame] | 268 | return 0; |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 269 | } |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 270 | } |
Christopher Faulet | e95f2c3 | 2017-07-24 16:30:34 +0200 | [diff] [blame] | 271 | |
Willy Tarreau | 3a4bedc | 2021-08-31 08:51:02 +0200 | [diff] [blame] | 272 | *hash = XXH3(name, len, var_name_hash_seed); |
| 273 | return 1; |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 274 | } |
| 275 | |
Willy Tarreau | 3a4bedc | 2021-08-31 08:51:02 +0200 | [diff] [blame] | 276 | /* This function returns the variable from the given list that matches |
| 277 | * <name_hash> or returns NULL if not found. It's only a linked list since it |
| 278 | * is not expected to have many variables per scope (a few tens at best). |
| 279 | * The caller is responsible for ensuring that <vars> is properly locked. |
| 280 | */ |
| 281 | static struct var *var_get(struct vars *vars, uint64_t name_hash) |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 282 | { |
| 283 | struct var *var; |
| 284 | |
| 285 | list_for_each_entry(var, &vars->head, l) |
Willy Tarreau | 3a4bedc | 2021-08-31 08:51:02 +0200 | [diff] [blame] | 286 | if (var->name_hash == name_hash) |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 287 | return var; |
| 288 | return NULL; |
| 289 | } |
| 290 | |
| 291 | /* Returns 0 if fails, else returns 1. */ |
| 292 | static int smp_fetch_var(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 293 | { |
| 294 | const struct var_desc *var_desc = &args[0].data.var; |
Willy Tarreau | 54496a6 | 2021-09-03 12:00:13 +0200 | [diff] [blame] | 295 | const struct buffer *def = NULL; |
Christopher Faulet | e95f2c3 | 2017-07-24 16:30:34 +0200 | [diff] [blame] | 296 | |
Willy Tarreau | 54496a6 | 2021-09-03 12:00:13 +0200 | [diff] [blame] | 297 | if (args[1].type == ARGT_STR) |
| 298 | def = &args[1].data.str; |
| 299 | |
| 300 | return vars_get_by_desc(var_desc, smp, def); |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 301 | } |
| 302 | |
Willy Tarreau | 3a4bedc | 2021-08-31 08:51:02 +0200 | [diff] [blame] | 303 | /* This function tries to create a variable whose name hash is <name_hash> in |
| 304 | * scope <scope> and store sample <smp> as its value. |
| 305 | * |
| 306 | * The stream and session are extracted from <smp>, whose stream may be NULL |
| 307 | * when scope is SCOPE_SESS. In case there wouldn't be enough memory to store |
| 308 | * the sample while the variable was already created, it would be changed to |
| 309 | * a bool (which is memory-less). |
Willy Tarreau | 7978c5c | 2021-09-07 14:24:07 +0200 | [diff] [blame] | 310 | * |
| 311 | * Flags is a bitfield that may contain one of the following flags: |
| 312 | * - VF_UPDATEONLY: if the scope is SCOPE_PROC, the variable may only be |
| 313 | * updated but not created. |
Willy Tarreau | 4994b57 | 2021-09-08 11:38:25 +0200 | [diff] [blame] | 314 | * - VF_CREATEONLY: do nothing if the variable already exists (success). |
Willy Tarreau | 3dc6dc3 | 2021-09-08 11:07:32 +0200 | [diff] [blame] | 315 | * - VF_PERMANENT: this flag will be passed to the variable upon creation |
Willy Tarreau | 7978c5c | 2021-09-07 14:24:07 +0200 | [diff] [blame] | 316 | * |
Willy Tarreau | f1cb0eb | 2021-09-07 11:37:37 +0200 | [diff] [blame] | 317 | * It returns 0 on failure, non-zero on success. |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 318 | */ |
Willy Tarreau | 3a4bedc | 2021-08-31 08:51:02 +0200 | [diff] [blame] | 319 | static int var_set(uint64_t name_hash, enum vars_scope scope, struct sample *smp, uint flags) |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 320 | { |
Willy Tarreau | f1cb0eb | 2021-09-07 11:37:37 +0200 | [diff] [blame] | 321 | struct vars *vars; |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 322 | struct var *var; |
Willy Tarreau | f1cb0eb | 2021-09-07 11:37:37 +0200 | [diff] [blame] | 323 | int ret = 0; |
| 324 | |
| 325 | vars = get_vars(smp->sess, smp->strm, scope); |
| 326 | if (!vars || vars->scope != scope) |
| 327 | return 0; |
| 328 | |
Willy Tarreau | dc72fbb | 2021-09-08 15:19:57 +0200 | [diff] [blame] | 329 | vars_wrlock(vars); |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 330 | |
| 331 | /* Look for existing variable name. */ |
Willy Tarreau | 3a4bedc | 2021-08-31 08:51:02 +0200 | [diff] [blame] | 332 | var = var_get(vars, name_hash); |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 333 | |
| 334 | if (var) { |
Willy Tarreau | 4994b57 | 2021-09-08 11:38:25 +0200 | [diff] [blame] | 335 | if (flags & VF_CREATEONLY) { |
| 336 | ret = 1; |
| 337 | goto unlock; |
| 338 | } |
| 339 | |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 340 | /* free its used memory. */ |
| 341 | if (var->data.type == SMP_T_STR || |
| 342 | var->data.type == SMP_T_BIN) { |
Willy Tarreau | 5b52b00 | 2021-02-26 21:19:53 +0100 | [diff] [blame] | 343 | ha_free(&var->data.u.str.area); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 344 | var_accounting_diff(vars, smp->sess, smp->strm, |
| 345 | -var->data.u.str.data); |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 346 | } |
Christopher Faulet | d02210c | 2017-07-24 16:24:39 +0200 | [diff] [blame] | 347 | else if (var->data.type == SMP_T_METH && var->data.u.meth.meth == HTTP_METH_OTHER) { |
Willy Tarreau | 5b52b00 | 2021-02-26 21:19:53 +0100 | [diff] [blame] | 348 | ha_free(&var->data.u.meth.str.area); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 349 | var_accounting_diff(vars, smp->sess, smp->strm, |
| 350 | -var->data.u.meth.str.data); |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 351 | } |
| 352 | } else { |
Willy Tarreau | 7978c5c | 2021-09-07 14:24:07 +0200 | [diff] [blame] | 353 | /* creation permitted for proc ? */ |
| 354 | if (flags & VF_UPDATEONLY && scope == SCOPE_PROC) |
| 355 | goto unlock; |
| 356 | |
Joseph Herlant | 0767689 | 2018-11-15 09:19:50 -0800 | [diff] [blame] | 357 | /* Check memory available. */ |
Willy Tarreau | 6204cd9 | 2016-03-10 16:33:04 +0100 | [diff] [blame] | 358 | if (!var_accounting_add(vars, smp->sess, smp->strm, sizeof(struct var))) |
Willy Tarreau | f1cb0eb | 2021-09-07 11:37:37 +0200 | [diff] [blame] | 359 | goto unlock; |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 360 | |
| 361 | /* Create new entry. */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 362 | var = pool_alloc(var_pool); |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 363 | if (!var) |
Willy Tarreau | f1cb0eb | 2021-09-07 11:37:37 +0200 | [diff] [blame] | 364 | goto unlock; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 365 | LIST_APPEND(&vars->head, &var->l); |
Willy Tarreau | 3a4bedc | 2021-08-31 08:51:02 +0200 | [diff] [blame] | 366 | var->name_hash = name_hash; |
Willy Tarreau | 3dc6dc3 | 2021-09-08 11:07:32 +0200 | [diff] [blame] | 367 | var->flags = flags & VF_PERMANENT; |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | /* Set type. */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 371 | var->data.type = smp->data.type; |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 372 | |
| 373 | /* Copy data. If the data needs memory, the function can fail. */ |
| 374 | switch (var->data.type) { |
| 375 | case SMP_T_BOOL: |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 376 | case SMP_T_SINT: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 377 | var->data.u.sint = smp->data.u.sint; |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 378 | break; |
| 379 | case SMP_T_IPV4: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 380 | var->data.u.ipv4 = smp->data.u.ipv4; |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 381 | break; |
| 382 | case SMP_T_IPV6: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 383 | var->data.u.ipv6 = smp->data.u.ipv6; |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 384 | break; |
| 385 | case SMP_T_STR: |
| 386 | case SMP_T_BIN: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 387 | if (!var_accounting_add(vars, smp->sess, smp->strm, smp->data.u.str.data)) { |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 388 | var->data.type = SMP_T_BOOL; /* This type doesn't use additional memory. */ |
Willy Tarreau | f1cb0eb | 2021-09-07 11:37:37 +0200 | [diff] [blame] | 389 | goto unlock; |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 390 | } |
Willy Tarreau | f1cb0eb | 2021-09-07 11:37:37 +0200 | [diff] [blame] | 391 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 392 | var->data.u.str.area = malloc(smp->data.u.str.data); |
| 393 | if (!var->data.u.str.area) { |
| 394 | var_accounting_diff(vars, smp->sess, smp->strm, |
| 395 | -smp->data.u.str.data); |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 396 | var->data.type = SMP_T_BOOL; /* This type doesn't use additional memory. */ |
Willy Tarreau | f1cb0eb | 2021-09-07 11:37:37 +0200 | [diff] [blame] | 397 | goto unlock; |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 398 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 399 | var->data.u.str.data = smp->data.u.str.data; |
| 400 | memcpy(var->data.u.str.area, smp->data.u.str.area, |
| 401 | var->data.u.str.data); |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 402 | break; |
| 403 | case SMP_T_METH: |
Christopher Faulet | d02210c | 2017-07-24 16:24:39 +0200 | [diff] [blame] | 404 | var->data.u.meth.meth = smp->data.u.meth.meth; |
| 405 | if (smp->data.u.meth.meth != HTTP_METH_OTHER) |
| 406 | break; |
| 407 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 408 | if (!var_accounting_add(vars, smp->sess, smp->strm, smp->data.u.meth.str.data)) { |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 409 | var->data.type = SMP_T_BOOL; /* This type doesn't use additional memory. */ |
Willy Tarreau | f1cb0eb | 2021-09-07 11:37:37 +0200 | [diff] [blame] | 410 | goto unlock; |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 411 | } |
Willy Tarreau | f1cb0eb | 2021-09-07 11:37:37 +0200 | [diff] [blame] | 412 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 413 | var->data.u.meth.str.area = malloc(smp->data.u.meth.str.data); |
| 414 | if (!var->data.u.meth.str.area) { |
| 415 | var_accounting_diff(vars, smp->sess, smp->strm, |
| 416 | -smp->data.u.meth.str.data); |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 417 | var->data.type = SMP_T_BOOL; /* This type doesn't use additional memory. */ |
Willy Tarreau | f1cb0eb | 2021-09-07 11:37:37 +0200 | [diff] [blame] | 418 | goto unlock; |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 419 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 420 | var->data.u.meth.str.data = smp->data.u.meth.str.data; |
| 421 | var->data.u.meth.str.size = smp->data.u.meth.str.data; |
| 422 | memcpy(var->data.u.meth.str.area, smp->data.u.meth.str.area, |
| 423 | var->data.u.meth.str.data); |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 424 | break; |
| 425 | } |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 426 | |
Willy Tarreau | f1cb0eb | 2021-09-07 11:37:37 +0200 | [diff] [blame] | 427 | /* OK, now done */ |
| 428 | ret = 1; |
| 429 | unlock: |
Willy Tarreau | dc72fbb | 2021-09-08 15:19:57 +0200 | [diff] [blame] | 430 | vars_wrunlock(vars); |
Christopher Faulet | e95f2c3 | 2017-07-24 16:30:34 +0200 | [diff] [blame] | 431 | return ret; |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 432 | } |
| 433 | |
Willy Tarreau | 3a4bedc | 2021-08-31 08:51:02 +0200 | [diff] [blame] | 434 | /* Deletes a variable matching name hash <name_hash> and scope <scope> for the |
| 435 | * session and stream found in <smp>. Note that stream may be null for |
| 436 | * SCOPE_SESS. Returns 0 if the scope was not found otherwise 1. |
Willy Tarreau | d378eb8 | 2021-09-07 11:44:41 +0200 | [diff] [blame] | 437 | */ |
Willy Tarreau | 3a4bedc | 2021-08-31 08:51:02 +0200 | [diff] [blame] | 438 | static int var_unset(uint64_t name_hash, enum vars_scope scope, struct sample *smp) |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 439 | { |
| 440 | struct vars *vars; |
| 441 | struct var *var; |
| 442 | unsigned int size = 0; |
| 443 | |
Willy Tarreau | f37b140 | 2019-06-04 16:27:36 +0200 | [diff] [blame] | 444 | vars = get_vars(smp->sess, smp->strm, scope); |
| 445 | if (!vars || vars->scope != scope) |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 446 | return 0; |
| 447 | |
| 448 | /* Look for existing variable name. */ |
Willy Tarreau | dc72fbb | 2021-09-08 15:19:57 +0200 | [diff] [blame] | 449 | vars_wrlock(vars); |
Willy Tarreau | 3a4bedc | 2021-08-31 08:51:02 +0200 | [diff] [blame] | 450 | var = var_get(vars, name_hash); |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 451 | if (var) { |
Willy Tarreau | c1c88f4 | 2021-09-08 15:03:58 +0200 | [diff] [blame] | 452 | size = var_clear(var, 0); |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 453 | var_accounting_diff(vars, smp->sess, smp->strm, -size); |
| 454 | } |
Willy Tarreau | dc72fbb | 2021-09-08 15:19:57 +0200 | [diff] [blame] | 455 | vars_wrunlock(vars); |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 456 | return 1; |
| 457 | } |
| 458 | |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 459 | /* Returns 0 if fails, else returns 1. */ |
| 460 | static int smp_conv_store(const struct arg *args, struct sample *smp, void *private) |
| 461 | { |
Willy Tarreau | 3a4bedc | 2021-08-31 08:51:02 +0200 | [diff] [blame] | 462 | uint64_t seed = var_name_hash_seed; |
| 463 | uint64_t name_hash = XXH3(smp->data.u.str.area, smp->data.u.str.data, seed); |
| 464 | |
| 465 | return var_set(name_hash, args[0].data.var.scope, smp, 0); |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 466 | } |
| 467 | |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 468 | /* Returns 0 if fails, else returns 1. */ |
| 469 | static int smp_conv_clear(const struct arg *args, struct sample *smp, void *private) |
| 470 | { |
Willy Tarreau | 3a4bedc | 2021-08-31 08:51:02 +0200 | [diff] [blame] | 471 | uint64_t seed = var_name_hash_seed; |
| 472 | uint64_t name_hash = XXH3(smp->data.u.str.area, smp->data.u.str.data, seed); |
| 473 | |
| 474 | return var_unset(name_hash, args[0].data.var.scope, smp); |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 475 | } |
| 476 | |
Joseph Herlant | 0767689 | 2018-11-15 09:19:50 -0800 | [diff] [blame] | 477 | /* This functions check an argument entry and fill it with a variable |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 478 | * type. The argumen must be a string. If the variable lookup fails, |
Joseph Herlant | 0767689 | 2018-11-15 09:19:50 -0800 | [diff] [blame] | 479 | * the function returns 0 and fill <err>, otherwise it returns 1. |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 480 | */ |
| 481 | int vars_check_arg(struct arg *arg, char **err) |
| 482 | { |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 483 | enum vars_scope scope; |
Willy Tarreau | df8eeb1 | 2021-09-08 11:07:32 +0200 | [diff] [blame] | 484 | struct sample empty_smp = { }; |
Willy Tarreau | 3a4bedc | 2021-08-31 08:51:02 +0200 | [diff] [blame] | 485 | uint64_t hash; |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 486 | |
| 487 | /* Check arg type. */ |
| 488 | if (arg->type != ARGT_STR) { |
| 489 | memprintf(err, "unexpected argument type"); |
| 490 | return 0; |
| 491 | } |
| 492 | |
| 493 | /* Register new variable name. */ |
Willy Tarreau | 3a4bedc | 2021-08-31 08:51:02 +0200 | [diff] [blame] | 494 | if (!vars_hash_name(arg->data.str.area, arg->data.str.data, &scope, &hash, err)) |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 495 | return 0; |
| 496 | |
Willy Tarreau | 3a4bedc | 2021-08-31 08:51:02 +0200 | [diff] [blame] | 497 | if (scope == SCOPE_PROC && !var_set(hash, scope, &empty_smp, VF_CREATEONLY|VF_PERMANENT)) |
Willy Tarreau | df8eeb1 | 2021-09-08 11:07:32 +0200 | [diff] [blame] | 498 | return 0; |
| 499 | |
Tim Duesterhus | a6cc7e8 | 2019-05-13 10:53:29 +0200 | [diff] [blame] | 500 | /* properly destroy the chunk */ |
| 501 | chunk_destroy(&arg->data.str); |
| 502 | |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 503 | /* Use the global variable name pointer. */ |
| 504 | arg->type = ARGT_VAR; |
Willy Tarreau | 3a4bedc | 2021-08-31 08:51:02 +0200 | [diff] [blame] | 505 | arg->data.var.name_hash = hash; |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 506 | arg->data.var.scope = scope; |
| 507 | return 1; |
| 508 | } |
| 509 | |
Willy Tarreau | 3a4bedc | 2021-08-31 08:51:02 +0200 | [diff] [blame] | 510 | /* This function stores a sample in a variable if it was already defined. |
Tim Duesterhus | b4fac1e | 2020-05-19 13:49:40 +0200 | [diff] [blame] | 511 | * Returns zero on failure and non-zero otherwise. The variable not being |
| 512 | * defined is treated as a failure. |
Christopher Faulet | 09c9df2 | 2016-10-31 11:05:37 +0100 | [diff] [blame] | 513 | */ |
Tim Duesterhus | b4fac1e | 2020-05-19 13:49:40 +0200 | [diff] [blame] | 514 | int vars_set_by_name_ifexist(const char *name, size_t len, struct sample *smp) |
Christopher Faulet | 09c9df2 | 2016-10-31 11:05:37 +0100 | [diff] [blame] | 515 | { |
| 516 | enum vars_scope scope; |
Willy Tarreau | 3a4bedc | 2021-08-31 08:51:02 +0200 | [diff] [blame] | 517 | uint64_t hash; |
Christopher Faulet | 09c9df2 | 2016-10-31 11:05:37 +0100 | [diff] [blame] | 518 | |
| 519 | /* Resolve name and scope. */ |
Willy Tarreau | 3a4bedc | 2021-08-31 08:51:02 +0200 | [diff] [blame] | 520 | if (!vars_hash_name(name, len, &scope, &hash, NULL)) |
Tim Duesterhus | b4fac1e | 2020-05-19 13:49:40 +0200 | [diff] [blame] | 521 | return 0; |
Christopher Faulet | 09c9df2 | 2016-10-31 11:05:37 +0100 | [diff] [blame] | 522 | |
Willy Tarreau | 3a4bedc | 2021-08-31 08:51:02 +0200 | [diff] [blame] | 523 | return var_set(hash, scope, smp, VF_UPDATEONLY); |
Christopher Faulet | 09c9df2 | 2016-10-31 11:05:37 +0100 | [diff] [blame] | 524 | } |
| 525 | |
| 526 | |
Willy Tarreau | 3a4bedc | 2021-08-31 08:51:02 +0200 | [diff] [blame] | 527 | /* This function stores a sample in a variable. |
Tim Duesterhus | b4fac1e | 2020-05-19 13:49:40 +0200 | [diff] [blame] | 528 | * Returns zero on failure and non-zero otherwise. |
Thierry FOURNIER | c365d99 | 2015-06-09 12:27:17 +0200 | [diff] [blame] | 529 | */ |
Tim Duesterhus | b4fac1e | 2020-05-19 13:49:40 +0200 | [diff] [blame] | 530 | int vars_set_by_name(const char *name, size_t len, struct sample *smp) |
Thierry FOURNIER | c365d99 | 2015-06-09 12:27:17 +0200 | [diff] [blame] | 531 | { |
| 532 | enum vars_scope scope; |
Willy Tarreau | 3a4bedc | 2021-08-31 08:51:02 +0200 | [diff] [blame] | 533 | uint64_t hash; |
Thierry FOURNIER | c365d99 | 2015-06-09 12:27:17 +0200 | [diff] [blame] | 534 | |
| 535 | /* Resolve name and scope. */ |
Willy Tarreau | 3a4bedc | 2021-08-31 08:51:02 +0200 | [diff] [blame] | 536 | if (!vars_hash_name(name, len, &scope, &hash, NULL)) |
Tim Duesterhus | b4fac1e | 2020-05-19 13:49:40 +0200 | [diff] [blame] | 537 | return 0; |
Thierry FOURNIER | c365d99 | 2015-06-09 12:27:17 +0200 | [diff] [blame] | 538 | |
Willy Tarreau | 3a4bedc | 2021-08-31 08:51:02 +0200 | [diff] [blame] | 539 | return var_set(hash, scope, smp, 0); |
Thierry FOURNIER | c365d99 | 2015-06-09 12:27:17 +0200 | [diff] [blame] | 540 | } |
| 541 | |
Willy Tarreau | 3a4bedc | 2021-08-31 08:51:02 +0200 | [diff] [blame] | 542 | /* This function unsets a variable if it was already defined. |
Tim Duesterhus | b4fac1e | 2020-05-19 13:49:40 +0200 | [diff] [blame] | 543 | * Returns zero on failure and non-zero otherwise. |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 544 | */ |
Tim Duesterhus | b4fac1e | 2020-05-19 13:49:40 +0200 | [diff] [blame] | 545 | int vars_unset_by_name_ifexist(const char *name, size_t len, struct sample *smp) |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 546 | { |
| 547 | enum vars_scope scope; |
Willy Tarreau | 3a4bedc | 2021-08-31 08:51:02 +0200 | [diff] [blame] | 548 | uint64_t hash; |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 549 | |
| 550 | /* Resolve name and scope. */ |
Willy Tarreau | 3a4bedc | 2021-08-31 08:51:02 +0200 | [diff] [blame] | 551 | if (!vars_hash_name(name, len, &scope, &hash, NULL)) |
Tim Duesterhus | b4fac1e | 2020-05-19 13:49:40 +0200 | [diff] [blame] | 552 | return 0; |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 553 | |
Willy Tarreau | 3a4bedc | 2021-08-31 08:51:02 +0200 | [diff] [blame] | 554 | return var_unset(hash, scope, smp); |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 555 | } |
| 556 | |
| 557 | |
Willy Tarreau | 3a4bedc | 2021-08-31 08:51:02 +0200 | [diff] [blame] | 558 | /* This retrieves variable whose hash matches <name_hash> from variables <vars>, |
| 559 | * and if found and not empty, duplicates the result into sample <smp>. |
| 560 | * smp_dup() is used in order to release the variables lock ASAP (so a pre- |
| 561 | * allocated chunk is obtained via get_trash_shunk()). The variables' lock is |
| 562 | * used for reads. |
Willy Tarreau | be7e00d | 2021-09-03 11:40:58 +0200 | [diff] [blame] | 563 | * |
Willy Tarreau | e352b9d | 2021-09-03 11:52:38 +0200 | [diff] [blame] | 564 | * The function returns 0 if the variable was not found and no default |
| 565 | * value was provided in <def>, otherwise 1 with the sample filled. |
| 566 | * Default values are always returned as strings. |
Willy Tarreau | be7e00d | 2021-09-03 11:40:58 +0200 | [diff] [blame] | 567 | */ |
Willy Tarreau | 3a4bedc | 2021-08-31 08:51:02 +0200 | [diff] [blame] | 568 | static int var_to_smp(struct vars *vars, uint64_t name_hash, struct sample *smp, const struct buffer *def) |
Willy Tarreau | be7e00d | 2021-09-03 11:40:58 +0200 | [diff] [blame] | 569 | { |
| 570 | struct var *var; |
| 571 | |
| 572 | /* Get the variable entry. */ |
Willy Tarreau | dc72fbb | 2021-09-08 15:19:57 +0200 | [diff] [blame] | 573 | vars_rdlock(vars); |
Willy Tarreau | 3a4bedc | 2021-08-31 08:51:02 +0200 | [diff] [blame] | 574 | var = var_get(vars, name_hash); |
Willy Tarreau | 63c3066 | 2021-09-08 13:58:19 +0200 | [diff] [blame] | 575 | if (!var || !var->data.type) { |
Willy Tarreau | e352b9d | 2021-09-03 11:52:38 +0200 | [diff] [blame] | 576 | if (!def) { |
Willy Tarreau | dc72fbb | 2021-09-08 15:19:57 +0200 | [diff] [blame] | 577 | vars_rdunlock(vars); |
Willy Tarreau | e352b9d | 2021-09-03 11:52:38 +0200 | [diff] [blame] | 578 | return 0; |
| 579 | } |
| 580 | |
| 581 | /* not found but we have a default value */ |
| 582 | smp->data.type = SMP_T_STR; |
| 583 | smp->data.u.str = *def; |
Willy Tarreau | be7e00d | 2021-09-03 11:40:58 +0200 | [diff] [blame] | 584 | } |
Willy Tarreau | e352b9d | 2021-09-03 11:52:38 +0200 | [diff] [blame] | 585 | else |
| 586 | smp->data = var->data; |
Willy Tarreau | be7e00d | 2021-09-03 11:40:58 +0200 | [diff] [blame] | 587 | |
| 588 | /* Copy sample. */ |
Willy Tarreau | be7e00d | 2021-09-03 11:40:58 +0200 | [diff] [blame] | 589 | smp_dup(smp); |
| 590 | |
Willy Tarreau | dc72fbb | 2021-09-08 15:19:57 +0200 | [diff] [blame] | 591 | vars_rdunlock(vars); |
Willy Tarreau | be7e00d | 2021-09-03 11:40:58 +0200 | [diff] [blame] | 592 | return 1; |
| 593 | } |
| 594 | |
Dragan Dosen | 14518f2 | 2021-02-22 17:20:01 +0100 | [diff] [blame] | 595 | /* This function fills a sample with the variable content. |
| 596 | * |
| 597 | * Keep in mind that a sample content is duplicated by using smp_dup() |
| 598 | * and it therefore uses a pre-allocated trash chunk as returned by |
| 599 | * get_trash_chunk(). |
| 600 | * |
Willy Tarreau | e352b9d | 2021-09-03 11:52:38 +0200 | [diff] [blame] | 601 | * If the variable is not valid in this scope, 0 is always returned. |
| 602 | * If the variable is valid but not found, either the default value |
| 603 | * <def> is returned if not NULL, or zero is returned. |
| 604 | * |
Dragan Dosen | 14518f2 | 2021-02-22 17:20:01 +0100 | [diff] [blame] | 605 | * Returns 1 if the sample is filled, otherwise it returns 0. |
Thierry FOURNIER | c365d99 | 2015-06-09 12:27:17 +0200 | [diff] [blame] | 606 | */ |
Willy Tarreau | e352b9d | 2021-09-03 11:52:38 +0200 | [diff] [blame] | 607 | int vars_get_by_name(const char *name, size_t len, struct sample *smp, const struct buffer *def) |
Thierry FOURNIER | c365d99 | 2015-06-09 12:27:17 +0200 | [diff] [blame] | 608 | { |
| 609 | struct vars *vars; |
Thierry FOURNIER | c365d99 | 2015-06-09 12:27:17 +0200 | [diff] [blame] | 610 | enum vars_scope scope; |
Willy Tarreau | 3a4bedc | 2021-08-31 08:51:02 +0200 | [diff] [blame] | 611 | uint64_t hash; |
Thierry FOURNIER | c365d99 | 2015-06-09 12:27:17 +0200 | [diff] [blame] | 612 | |
| 613 | /* Resolve name and scope. */ |
Willy Tarreau | 3a4bedc | 2021-08-31 08:51:02 +0200 | [diff] [blame] | 614 | if (!vars_hash_name(name, len, &scope, &hash, NULL)) |
Thierry FOURNIER | c365d99 | 2015-06-09 12:27:17 +0200 | [diff] [blame] | 615 | return 0; |
| 616 | |
| 617 | /* Select "vars" pool according with the scope. */ |
Willy Tarreau | f37b140 | 2019-06-04 16:27:36 +0200 | [diff] [blame] | 618 | vars = get_vars(smp->sess, smp->strm, scope); |
| 619 | if (!vars || vars->scope != scope) |
Thierry FOURNIER | c365d99 | 2015-06-09 12:27:17 +0200 | [diff] [blame] | 620 | return 0; |
Thierry FOURNIER | c365d99 | 2015-06-09 12:27:17 +0200 | [diff] [blame] | 621 | |
Willy Tarreau | 3a4bedc | 2021-08-31 08:51:02 +0200 | [diff] [blame] | 622 | return var_to_smp(vars, hash, smp, def); |
Thierry FOURNIER | c365d99 | 2015-06-09 12:27:17 +0200 | [diff] [blame] | 623 | } |
| 624 | |
Dragan Dosen | 14518f2 | 2021-02-22 17:20:01 +0100 | [diff] [blame] | 625 | /* This function fills a sample with the content of the variable described |
| 626 | * by <var_desc>. |
| 627 | * |
| 628 | * Keep in mind that a sample content is duplicated by using smp_dup() |
| 629 | * and it therefore uses a pre-allocated trash chunk as returned by |
| 630 | * get_trash_chunk(). |
| 631 | * |
Willy Tarreau | e352b9d | 2021-09-03 11:52:38 +0200 | [diff] [blame] | 632 | * If the variable is not valid in this scope, 0 is always returned. |
| 633 | * If the variable is valid but not found, either the default value |
| 634 | * <def> is returned if not NULL, or zero is returned. |
| 635 | * |
Dragan Dosen | 14518f2 | 2021-02-22 17:20:01 +0100 | [diff] [blame] | 636 | * Returns 1 if the sample is filled, otherwise it returns 0. |
Thierry FOURNIER | fd77e05 | 2015-07-07 21:20:42 +0200 | [diff] [blame] | 637 | */ |
Willy Tarreau | e352b9d | 2021-09-03 11:52:38 +0200 | [diff] [blame] | 638 | int vars_get_by_desc(const struct var_desc *var_desc, struct sample *smp, const struct buffer *def) |
Thierry FOURNIER | fd77e05 | 2015-07-07 21:20:42 +0200 | [diff] [blame] | 639 | { |
| 640 | struct vars *vars; |
Thierry FOURNIER | fd77e05 | 2015-07-07 21:20:42 +0200 | [diff] [blame] | 641 | |
| 642 | /* Select "vars" pool according with the scope. */ |
Willy Tarreau | f37b140 | 2019-06-04 16:27:36 +0200 | [diff] [blame] | 643 | vars = get_vars(smp->sess, smp->strm, var_desc->scope); |
Thierry FOURNIER | fd77e05 | 2015-07-07 21:20:42 +0200 | [diff] [blame] | 644 | |
Joseph Herlant | 0767689 | 2018-11-15 09:19:50 -0800 | [diff] [blame] | 645 | /* Check if the scope is available a this point of processing. */ |
Willy Tarreau | f37b140 | 2019-06-04 16:27:36 +0200 | [diff] [blame] | 646 | if (!vars || vars->scope != var_desc->scope) |
Thierry FOURNIER | fd77e05 | 2015-07-07 21:20:42 +0200 | [diff] [blame] | 647 | return 0; |
| 648 | |
Willy Tarreau | 3a4bedc | 2021-08-31 08:51:02 +0200 | [diff] [blame] | 649 | return var_to_smp(vars, var_desc->name_hash, smp, def); |
Thierry FOURNIER | fd77e05 | 2015-07-07 21:20:42 +0200 | [diff] [blame] | 650 | } |
| 651 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 652 | /* Always returns ACT_RET_CONT even if an error occurs. */ |
| 653 | static enum act_return action_store(struct act_rule *rule, struct proxy *px, |
Willy Tarreau | 658b85b | 2015-09-27 10:00:49 +0200 | [diff] [blame] | 654 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 655 | { |
Willy Tarreau | 9a621ae | 2021-09-02 21:00:38 +0200 | [diff] [blame] | 656 | struct buffer *fmtstr = NULL; |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 657 | struct sample smp; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 658 | int dir; |
| 659 | |
| 660 | switch (rule->from) { |
Willy Tarreau | 620408f | 2016-10-21 16:37:51 +0200 | [diff] [blame] | 661 | case ACT_F_TCP_REQ_SES: dir = SMP_OPT_DIR_REQ; break; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 662 | case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break; |
| 663 | case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break; |
| 664 | case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break; |
| 665 | case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break; |
Gaetan Rivet | 0c39ecc | 2020-02-24 17:34:11 +0100 | [diff] [blame] | 666 | case ACT_F_TCP_CHK: dir = SMP_OPT_DIR_REQ; break; |
Willy Tarreau | 01d580a | 2021-03-26 11:11:34 +0100 | [diff] [blame] | 667 | case ACT_F_CFG_PARSER: dir = SMP_OPT_DIR_REQ; break; /* not used anyway */ |
Willy Tarreau | 2f836de | 2021-03-26 15:36:44 +0100 | [diff] [blame] | 668 | case ACT_F_CLI_PARSER: dir = SMP_OPT_DIR_REQ; break; /* not used anyway */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 669 | default: |
| 670 | send_log(px, LOG_ERR, "Vars: internal error while execute action store."); |
| 671 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 672 | ha_alert("Vars: internal error while execute action store.\n"); |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 673 | return ACT_RET_CONT; |
| 674 | } |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 675 | |
| 676 | /* Process the expression. */ |
| 677 | memset(&smp, 0, sizeof(smp)); |
Willy Tarreau | 9a621ae | 2021-09-02 21:00:38 +0200 | [diff] [blame] | 678 | |
| 679 | if (!LIST_ISEMPTY(&rule->arg.vars.fmt)) { |
| 680 | /* a format-string is used */ |
| 681 | |
| 682 | fmtstr = alloc_trash_chunk(); |
| 683 | if (!fmtstr) { |
| 684 | send_log(px, LOG_ERR, "Vars: memory allocation failure while processing store rule."); |
| 685 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) |
| 686 | ha_alert("Vars: memory allocation failure while processing store rule.\n"); |
| 687 | return ACT_RET_CONT; |
| 688 | } |
| 689 | |
| 690 | /* execute the log-format expression */ |
| 691 | fmtstr->data = sess_build_logline(sess, s, fmtstr->area, fmtstr->size, &rule->arg.vars.fmt); |
| 692 | |
| 693 | /* convert it to a sample of type string as it's what the vars |
| 694 | * API consumes, and store it. |
| 695 | */ |
| 696 | smp_set_owner(&smp, px, sess, s, 0); |
| 697 | smp.data.type = SMP_T_STR; |
| 698 | smp.data.u.str = *fmtstr; |
Willy Tarreau | 3a4bedc | 2021-08-31 08:51:02 +0200 | [diff] [blame] | 699 | var_set(rule->arg.vars.name_hash, rule->arg.vars.scope, &smp, 0); |
Willy Tarreau | 9a621ae | 2021-09-02 21:00:38 +0200 | [diff] [blame] | 700 | } |
| 701 | else { |
| 702 | /* an expression is used */ |
| 703 | if (!sample_process(px, sess, s, dir|SMP_OPT_FINAL, |
| 704 | rule->arg.vars.expr, &smp)) |
| 705 | return ACT_RET_CONT; |
| 706 | } |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 707 | |
| 708 | /* Store the sample, and ignore errors. */ |
Willy Tarreau | 3a4bedc | 2021-08-31 08:51:02 +0200 | [diff] [blame] | 709 | var_set(rule->arg.vars.name_hash, rule->arg.vars.scope, &smp, 0); |
Willy Tarreau | 9a621ae | 2021-09-02 21:00:38 +0200 | [diff] [blame] | 710 | free_trash_chunk(fmtstr); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 711 | return ACT_RET_CONT; |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 712 | } |
| 713 | |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 714 | /* Always returns ACT_RET_CONT even if an error occurs. */ |
| 715 | static enum act_return action_clear(struct act_rule *rule, struct proxy *px, |
| 716 | struct session *sess, struct stream *s, int flags) |
| 717 | { |
| 718 | struct sample smp; |
| 719 | |
| 720 | memset(&smp, 0, sizeof(smp)); |
| 721 | smp_set_owner(&smp, px, sess, s, SMP_OPT_FINAL); |
| 722 | |
| 723 | /* Clear the variable using the sample context, and ignore errors. */ |
Willy Tarreau | 3a4bedc | 2021-08-31 08:51:02 +0200 | [diff] [blame] | 724 | var_unset(rule->arg.vars.name_hash, rule->arg.vars.scope, &smp); |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 725 | return ACT_RET_CONT; |
| 726 | } |
| 727 | |
Tim Duesterhus | 01a0ce3 | 2020-06-14 17:27:36 +0200 | [diff] [blame] | 728 | static void release_store_rule(struct act_rule *rule) |
| 729 | { |
Willy Tarreau | 9a621ae | 2021-09-02 21:00:38 +0200 | [diff] [blame] | 730 | struct logformat_node *lf, *lfb; |
Willy Tarreau | c77bad2 | 2021-09-03 10:58:07 +0200 | [diff] [blame] | 731 | |
| 732 | list_for_each_entry_safe(lf, lfb, &rule->arg.vars.fmt, list) { |
Willy Tarreau | 9a621ae | 2021-09-02 21:00:38 +0200 | [diff] [blame] | 733 | LIST_DELETE(&lf->list); |
| 734 | release_sample_expr(lf->expr); |
| 735 | free(lf->arg); |
| 736 | free(lf); |
| 737 | } |
| 738 | |
Tim Duesterhus | 01a0ce3 | 2020-06-14 17:27:36 +0200 | [diff] [blame] | 739 | release_sample_expr(rule->arg.vars.expr); |
| 740 | } |
| 741 | |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 742 | /* This two function checks the variable name and replace the |
| 743 | * configuration string name by the global string name. its |
| 744 | * the same string, but the global pointer can be easy to |
Willy Tarreau | e352b9d | 2021-09-03 11:52:38 +0200 | [diff] [blame] | 745 | * compare. They return non-zero on success, zero on failure. |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 746 | * |
| 747 | * The first function checks a sample-fetch and the second |
| 748 | * checks a converter. |
| 749 | */ |
| 750 | static int smp_check_var(struct arg *args, char **err) |
| 751 | { |
| 752 | return vars_check_arg(&args[0], err); |
| 753 | } |
| 754 | |
| 755 | static int conv_check_var(struct arg *args, struct sample_conv *conv, |
| 756 | const char *file, int line, char **err_msg) |
| 757 | { |
| 758 | return vars_check_arg(&args[0], err_msg); |
| 759 | } |
| 760 | |
| 761 | /* This function is a common parser for using variables. It understands |
| 762 | * the format: |
| 763 | * |
Willy Tarreau | 9a621ae | 2021-09-02 21:00:38 +0200 | [diff] [blame] | 764 | * set-var-fmt(<variable-name>) <format-string> |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 765 | * set-var(<variable-name>) <expression> |
Willy Tarreau | 4b7531f | 2019-06-04 16:43:29 +0200 | [diff] [blame] | 766 | * unset-var(<variable-name>) |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 767 | * |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 768 | * It returns ACT_RET_PRS_ERR if fails and <err> is filled with an error |
| 769 | * message. Otherwise, it returns ACT_RET_PRS_OK and the variable <expr> |
Willy Tarreau | 13d2ba2 | 2021-03-26 11:38:08 +0100 | [diff] [blame] | 770 | * is filled with the pointer to the expression to execute. The proxy is |
| 771 | * only used to retrieve the ->conf entries. |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 772 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 773 | static enum act_parse_ret parse_store(const char **args, int *arg, struct proxy *px, |
| 774 | struct act_rule *rule, char **err) |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 775 | { |
| 776 | const char *var_name = args[*arg-1]; |
| 777 | int var_len; |
Thierry FOURNIER | 48a9cd1 | 2015-07-28 19:00:28 +0200 | [diff] [blame] | 778 | const char *kw_name; |
Willy Tarreau | 9a621ae | 2021-09-02 21:00:38 +0200 | [diff] [blame] | 779 | int flags, set_var = 0; /* 0=unset-var, 1=set-var, 2=set-var-fmt */ |
Willy Tarreau | df8eeb1 | 2021-09-08 11:07:32 +0200 | [diff] [blame] | 780 | struct sample empty_smp = { }; |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 781 | |
Willy Tarreau | 9a621ae | 2021-09-02 21:00:38 +0200 | [diff] [blame] | 782 | if (strncmp(var_name, "set-var-fmt", 11) == 0) { |
| 783 | var_name += 11; |
| 784 | set_var = 2; |
| 785 | } |
| 786 | else if (strncmp(var_name, "set-var", 7) == 0) { |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 787 | var_name += 7; |
| 788 | set_var = 1; |
| 789 | } |
Willy Tarreau | 2819210 | 2021-09-02 18:46:22 +0200 | [diff] [blame] | 790 | else if (strncmp(var_name, "unset-var", 9) == 0) { |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 791 | var_name += 9; |
| 792 | set_var = 0; |
| 793 | } |
| 794 | |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 795 | if (*var_name != '(') { |
Willy Tarreau | 9a621ae | 2021-09-02 21:00:38 +0200 | [diff] [blame] | 796 | memprintf(err, "invalid or incomplete action '%s'. Expects 'set-var(<var-name>)', 'set-var-fmt(<var-name>)' or 'unset-var(<var-name>)'", |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 797 | args[*arg-1]); |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 798 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 799 | } |
| 800 | var_name++; /* jump the '(' */ |
| 801 | var_len = strlen(var_name); |
| 802 | var_len--; /* remove the ')' */ |
| 803 | if (var_name[var_len] != ')') { |
Willy Tarreau | 9a621ae | 2021-09-02 21:00:38 +0200 | [diff] [blame] | 804 | memprintf(err, "incomplete argument after action '%s'. Expects 'set-var(<var-name>)', 'set-var-fmt(<var-name>)' or 'unset-var(<var-name>)'", |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 805 | args[*arg-1]); |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 806 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 807 | } |
| 808 | |
Willy Tarreau | 9a621ae | 2021-09-02 21:00:38 +0200 | [diff] [blame] | 809 | LIST_INIT(&rule->arg.vars.fmt); |
Willy Tarreau | 3a4bedc | 2021-08-31 08:51:02 +0200 | [diff] [blame] | 810 | if (!vars_hash_name(var_name, var_len, &rule->arg.vars.scope, &rule->arg.vars.name_hash, err)) |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 811 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 812 | |
Willy Tarreau | df8eeb1 | 2021-09-08 11:07:32 +0200 | [diff] [blame] | 813 | if (rule->arg.vars.scope == SCOPE_PROC && |
Willy Tarreau | 3a4bedc | 2021-08-31 08:51:02 +0200 | [diff] [blame] | 814 | !var_set(rule->arg.vars.name_hash, rule->arg.vars.scope, &empty_smp, VF_CREATEONLY|VF_PERMANENT)) |
Willy Tarreau | df8eeb1 | 2021-09-08 11:07:32 +0200 | [diff] [blame] | 815 | return 0; |
| 816 | |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 817 | /* There is no fetch method when variable is unset. Just set the right |
| 818 | * action and return. */ |
| 819 | if (!set_var) { |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 820 | rule->action = ACT_CUSTOM; |
| 821 | rule->action_ptr = action_clear; |
Tim Duesterhus | 01a0ce3 | 2020-06-14 17:27:36 +0200 | [diff] [blame] | 822 | rule->release_ptr = release_store_rule; |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 823 | return ACT_RET_PRS_OK; |
| 824 | } |
| 825 | |
Thierry FOURNIER | 48a9cd1 | 2015-07-28 19:00:28 +0200 | [diff] [blame] | 826 | kw_name = args[*arg-1]; |
| 827 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 828 | switch (rule->from) { |
Willy Tarreau | 843096d | 2021-09-02 19:03:07 +0200 | [diff] [blame] | 829 | case ACT_F_TCP_REQ_SES: |
| 830 | flags = SMP_VAL_FE_SES_ACC; |
Willy Tarreau | 54b96d9 | 2021-09-02 19:46:08 +0200 | [diff] [blame] | 831 | px->conf.args.ctx = ARGC_TSE; |
Willy Tarreau | 843096d | 2021-09-02 19:03:07 +0200 | [diff] [blame] | 832 | break; |
| 833 | case ACT_F_TCP_REQ_CNT: |
| 834 | flags = (px->cap & PR_CAP_FE) ? SMP_VAL_FE_REQ_CNT : SMP_VAL_BE_REQ_CNT; |
Willy Tarreau | 54b96d9 | 2021-09-02 19:46:08 +0200 | [diff] [blame] | 835 | px->conf.args.ctx = ARGC_TRQ; |
Willy Tarreau | 843096d | 2021-09-02 19:03:07 +0200 | [diff] [blame] | 836 | break; |
| 837 | case ACT_F_TCP_RES_CNT: |
| 838 | flags = (px->cap & PR_CAP_FE) ? SMP_VAL_FE_RES_CNT : SMP_VAL_BE_RES_CNT; |
Willy Tarreau | 54b96d9 | 2021-09-02 19:46:08 +0200 | [diff] [blame] | 839 | px->conf.args.ctx = ARGC_TRS; |
Willy Tarreau | 843096d | 2021-09-02 19:03:07 +0200 | [diff] [blame] | 840 | break; |
| 841 | case ACT_F_HTTP_REQ: |
| 842 | flags = (px->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR; |
Willy Tarreau | 54b96d9 | 2021-09-02 19:46:08 +0200 | [diff] [blame] | 843 | px->conf.args.ctx = ARGC_HRQ; |
Willy Tarreau | 843096d | 2021-09-02 19:03:07 +0200 | [diff] [blame] | 844 | break; |
| 845 | case ACT_F_HTTP_RES: |
| 846 | flags = (px->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR; |
Willy Tarreau | 54b96d9 | 2021-09-02 19:46:08 +0200 | [diff] [blame] | 847 | px->conf.args.ctx = ARGC_HRS; |
Willy Tarreau | 843096d | 2021-09-02 19:03:07 +0200 | [diff] [blame] | 848 | break; |
| 849 | case ACT_F_TCP_CHK: |
| 850 | flags = SMP_VAL_BE_CHK_RUL; |
Willy Tarreau | 54b96d9 | 2021-09-02 19:46:08 +0200 | [diff] [blame] | 851 | px->conf.args.ctx = ARGC_TCK; |
Willy Tarreau | 843096d | 2021-09-02 19:03:07 +0200 | [diff] [blame] | 852 | break; |
| 853 | case ACT_F_CFG_PARSER: |
| 854 | flags = SMP_VAL_CFG_PARSER; |
Willy Tarreau | 54b96d9 | 2021-09-02 19:46:08 +0200 | [diff] [blame] | 855 | px->conf.args.ctx = ARGC_CFG; |
Willy Tarreau | 843096d | 2021-09-02 19:03:07 +0200 | [diff] [blame] | 856 | break; |
| 857 | case ACT_F_CLI_PARSER: |
| 858 | flags = SMP_VAL_CLI_PARSER; |
Willy Tarreau | 54b96d9 | 2021-09-02 19:46:08 +0200 | [diff] [blame] | 859 | px->conf.args.ctx = ARGC_CLI; |
Willy Tarreau | 843096d | 2021-09-02 19:03:07 +0200 | [diff] [blame] | 860 | break; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 861 | default: |
| 862 | memprintf(err, |
| 863 | "internal error, unexpected rule->from=%d, please report this bug!", |
| 864 | rule->from); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 865 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 866 | } |
Willy Tarreau | 54b96d9 | 2021-09-02 19:46:08 +0200 | [diff] [blame] | 867 | |
Willy Tarreau | 9a621ae | 2021-09-02 21:00:38 +0200 | [diff] [blame] | 868 | if (set_var == 2) { /* set-var-fmt */ |
| 869 | if (!parse_logformat_string(args[*arg], px, &rule->arg.vars.fmt, 0, flags, err)) |
| 870 | return ACT_RET_PRS_ERR; |
Willy Tarreau | 54b96d9 | 2021-09-02 19:46:08 +0200 | [diff] [blame] | 871 | |
Willy Tarreau | 9a621ae | 2021-09-02 21:00:38 +0200 | [diff] [blame] | 872 | (*arg)++; |
| 873 | |
| 874 | /* for late error reporting */ |
| 875 | free(px->conf.lfs_file); |
| 876 | px->conf.lfs_file = strdup(px->conf.args.file); |
| 877 | px->conf.lfs_line = px->conf.args.line; |
| 878 | } else { |
| 879 | /* set-var */ |
| 880 | rule->arg.vars.expr = sample_parse_expr((char **)args, arg, px->conf.args.file, |
| 881 | px->conf.args.line, err, &px->conf.args, NULL); |
| 882 | if (!rule->arg.vars.expr) |
| 883 | return ACT_RET_PRS_ERR; |
| 884 | |
| 885 | if (!(rule->arg.vars.expr->fetch->val & flags)) { |
| 886 | memprintf(err, |
| 887 | "fetch method '%s' extracts information from '%s', none of which is available here", |
| 888 | kw_name, sample_src_names(rule->arg.vars.expr->fetch->use)); |
| 889 | free(rule->arg.vars.expr); |
| 890 | return ACT_RET_PRS_ERR; |
| 891 | } |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 892 | } |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 893 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 894 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 895 | rule->action_ptr = action_store; |
Tim Duesterhus | 01a0ce3 | 2020-06-14 17:27:36 +0200 | [diff] [blame] | 896 | rule->release_ptr = release_store_rule; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 897 | return ACT_RET_PRS_OK; |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 898 | } |
| 899 | |
Willy Tarreau | 13d2ba2 | 2021-03-26 11:38:08 +0100 | [diff] [blame] | 900 | |
| 901 | /* parses a global "set-var" directive. It will create a temporary rule and |
| 902 | * expression that are parsed, processed, and released on the fly so that we |
| 903 | * respect the real set-var syntax. These directives take the following format: |
| 904 | * set-var <name> <expression> |
Willy Tarreau | 753d4db | 2021-09-03 09:02:47 +0200 | [diff] [blame] | 905 | * set-var-fmt <name> <fmt> |
Willy Tarreau | 13d2ba2 | 2021-03-26 11:38:08 +0100 | [diff] [blame] | 906 | * Note that parse_store() expects "set-var(name) <expression>" so we have to |
| 907 | * temporarily replace the keyword here. |
| 908 | */ |
| 909 | static int vars_parse_global_set_var(char **args, int section_type, struct proxy *curpx, |
| 910 | const struct proxy *defpx, const char *file, int line, |
| 911 | char **err) |
| 912 | { |
| 913 | struct proxy px = { |
Willy Tarreau | 9c20433 | 2021-09-03 08:19:43 +0200 | [diff] [blame] | 914 | .id = "CFG", |
Willy Tarreau | 13d2ba2 | 2021-03-26 11:38:08 +0100 | [diff] [blame] | 915 | .conf.args.file = file, |
| 916 | .conf.args.line = line, |
| 917 | }; |
| 918 | struct act_rule rule = { |
| 919 | .arg.vars.scope = SCOPE_PROC, |
| 920 | .from = ACT_F_CFG_PARSER, |
| 921 | }; |
Willy Tarreau | 753d4db | 2021-09-03 09:02:47 +0200 | [diff] [blame] | 922 | enum obj_type objt = OBJ_TYPE_NONE; |
| 923 | struct session *sess = NULL; |
Willy Tarreau | 13d2ba2 | 2021-03-26 11:38:08 +0100 | [diff] [blame] | 924 | enum act_parse_ret p_ret; |
| 925 | char *old_arg1; |
| 926 | char *tmp_arg1; |
| 927 | int arg = 2; // variable name |
| 928 | int ret = -1; |
Willy Tarreau | 753d4db | 2021-09-03 09:02:47 +0200 | [diff] [blame] | 929 | int use_fmt = 0; |
Willy Tarreau | 13d2ba2 | 2021-03-26 11:38:08 +0100 | [diff] [blame] | 930 | |
| 931 | LIST_INIT(&px.conf.args.list); |
| 932 | |
Willy Tarreau | 753d4db | 2021-09-03 09:02:47 +0200 | [diff] [blame] | 933 | use_fmt = strcmp(args[0], "set-var-fmt") == 0; |
| 934 | |
Willy Tarreau | 13d2ba2 | 2021-03-26 11:38:08 +0100 | [diff] [blame] | 935 | if (!*args[1] || !*args[2]) { |
Willy Tarreau | 753d4db | 2021-09-03 09:02:47 +0200 | [diff] [blame] | 936 | if (use_fmt) |
| 937 | memprintf(err, "'%s' requires a process-wide variable name ('proc.<name>') and a format string.", args[0]); |
| 938 | else |
| 939 | memprintf(err, "'%s' requires a process-wide variable name ('proc.<name>') and a sample expression.", args[0]); |
Willy Tarreau | 13d2ba2 | 2021-03-26 11:38:08 +0100 | [diff] [blame] | 940 | goto end; |
| 941 | } |
| 942 | |
| 943 | tmp_arg1 = NULL; |
Willy Tarreau | 753d4db | 2021-09-03 09:02:47 +0200 | [diff] [blame] | 944 | if (!memprintf(&tmp_arg1, "set-var%s(%s)", use_fmt ? "-fmt" : "", args[1])) |
Willy Tarreau | 13d2ba2 | 2021-03-26 11:38:08 +0100 | [diff] [blame] | 945 | goto end; |
| 946 | |
| 947 | /* parse_store() will always return a message in <err> on error */ |
| 948 | old_arg1 = args[1]; args[1] = tmp_arg1; |
| 949 | p_ret = parse_store((const char **)args, &arg, &px, &rule, err); |
| 950 | free(args[1]); args[1] = old_arg1; |
| 951 | |
| 952 | if (p_ret != ACT_RET_PRS_OK) |
| 953 | goto end; |
| 954 | |
| 955 | if (rule.arg.vars.scope != SCOPE_PROC) { |
| 956 | memprintf(err, "'%s': cannot set variable '%s', only scope 'proc' is permitted in the global section.", args[0], args[1]); |
| 957 | goto end; |
| 958 | } |
| 959 | |
| 960 | if (smp_resolve_args(&px, err) != 0) { |
| 961 | release_sample_expr(rule.arg.vars.expr); |
| 962 | indent_msg(err, 2); |
| 963 | goto end; |
| 964 | } |
| 965 | |
Willy Tarreau | 753d4db | 2021-09-03 09:02:47 +0200 | [diff] [blame] | 966 | if (use_fmt && !(sess = session_new(&px, NULL, &objt))) { |
| 967 | release_sample_expr(rule.arg.vars.expr); |
| 968 | memprintf(err, "'%s': out of memory when trying to set variable '%s' in the global section.", args[0], args[1]); |
| 969 | goto end; |
| 970 | } |
| 971 | |
| 972 | action_store(&rule, &px, sess, NULL, 0); |
Willy Tarreau | 13d2ba2 | 2021-03-26 11:38:08 +0100 | [diff] [blame] | 973 | release_sample_expr(rule.arg.vars.expr); |
Willy Tarreau | 753d4db | 2021-09-03 09:02:47 +0200 | [diff] [blame] | 974 | if (sess) |
| 975 | session_free(sess); |
Willy Tarreau | 13d2ba2 | 2021-03-26 11:38:08 +0100 | [diff] [blame] | 976 | |
| 977 | ret = 0; |
| 978 | end: |
| 979 | return ret; |
| 980 | } |
| 981 | |
Willy Tarreau | c35eb38 | 2021-03-26 14:51:31 +0100 | [diff] [blame] | 982 | /* parse CLI's "get var <name>" */ |
| 983 | static int vars_parse_cli_get_var(char **args, char *payload, struct appctx *appctx, void *private) |
| 984 | { |
| 985 | struct vars *vars; |
Willy Tarreau | 374edc7 | 2021-04-01 17:01:43 +0200 | [diff] [blame] | 986 | struct sample smp = { }; |
Willy Tarreau | c35eb38 | 2021-03-26 14:51:31 +0100 | [diff] [blame] | 987 | int i; |
| 988 | |
| 989 | if (!cli_has_level(appctx, ACCESS_LVL_OPER)) |
| 990 | return 1; |
| 991 | |
| 992 | if (!*args[2]) |
| 993 | return cli_err(appctx, "Missing process-wide variable identifier.\n"); |
| 994 | |
| 995 | vars = get_vars(NULL, NULL, SCOPE_PROC); |
| 996 | if (!vars || vars->scope != SCOPE_PROC) |
| 997 | return 0; |
| 998 | |
Willy Tarreau | e352b9d | 2021-09-03 11:52:38 +0200 | [diff] [blame] | 999 | if (!vars_get_by_name(args[2], strlen(args[2]), &smp, NULL)) |
Willy Tarreau | c35eb38 | 2021-03-26 14:51:31 +0100 | [diff] [blame] | 1000 | return cli_err(appctx, "Variable not found.\n"); |
| 1001 | |
| 1002 | /* the sample returned by vars_get_by_name() is allocated into a trash |
| 1003 | * chunk so we have no constraint to manipulate it. |
| 1004 | */ |
| 1005 | chunk_printf(&trash, "%s: type=%s value=", args[2], smp_to_type[smp.data.type]); |
| 1006 | |
| 1007 | if (!sample_casts[smp.data.type][SMP_T_STR] || |
| 1008 | !sample_casts[smp.data.type][SMP_T_STR](&smp)) { |
| 1009 | chunk_appendf(&trash, "(undisplayable)"); |
| 1010 | } else { |
| 1011 | /* Display the displayable chars*. */ |
| 1012 | b_putchr(&trash, '<'); |
| 1013 | for (i = 0; i < smp.data.u.str.data; i++) { |
| 1014 | if (isprint((unsigned char)smp.data.u.str.area[i])) |
| 1015 | b_putchr(&trash, smp.data.u.str.area[i]); |
| 1016 | else |
| 1017 | b_putchr(&trash, '.'); |
| 1018 | } |
| 1019 | b_putchr(&trash, '>'); |
| 1020 | b_putchr(&trash, 0); |
| 1021 | } |
| 1022 | return cli_msg(appctx, LOG_INFO, trash.area); |
| 1023 | } |
| 1024 | |
Willy Tarreau | e93bff4 | 2021-09-03 09:47:37 +0200 | [diff] [blame] | 1025 | /* parse CLI's "set var <name>". It accepts: |
| 1026 | * - set var <name> <expression> |
| 1027 | * - set var <name> expr <expression> |
| 1028 | * - set var <name> fmt <format> |
| 1029 | */ |
Willy Tarreau | b8bd1ee | 2021-03-26 15:19:50 +0100 | [diff] [blame] | 1030 | static int vars_parse_cli_set_var(char **args, char *payload, struct appctx *appctx, void *private) |
| 1031 | { |
| 1032 | struct proxy px = { |
| 1033 | .id = "CLI", |
| 1034 | .conf.args.file = "CLI", |
| 1035 | .conf.args.line = 0, |
| 1036 | }; |
| 1037 | struct act_rule rule = { |
| 1038 | .arg.vars.scope = SCOPE_PROC, |
| 1039 | .from = ACT_F_CLI_PARSER, |
| 1040 | }; |
Willy Tarreau | e93bff4 | 2021-09-03 09:47:37 +0200 | [diff] [blame] | 1041 | enum obj_type objt = OBJ_TYPE_NONE; |
| 1042 | struct session *sess = NULL; |
Willy Tarreau | b8bd1ee | 2021-03-26 15:19:50 +0100 | [diff] [blame] | 1043 | enum act_parse_ret p_ret; |
Willy Tarreau | e93bff4 | 2021-09-03 09:47:37 +0200 | [diff] [blame] | 1044 | const char *tmp_args[3]; |
| 1045 | int tmp_arg; |
| 1046 | char *tmp_act; |
Willy Tarreau | b8bd1ee | 2021-03-26 15:19:50 +0100 | [diff] [blame] | 1047 | char *err = NULL; |
Willy Tarreau | b8bd1ee | 2021-03-26 15:19:50 +0100 | [diff] [blame] | 1048 | int nberr; |
Willy Tarreau | e93bff4 | 2021-09-03 09:47:37 +0200 | [diff] [blame] | 1049 | int use_fmt = 0; |
Willy Tarreau | b8bd1ee | 2021-03-26 15:19:50 +0100 | [diff] [blame] | 1050 | |
| 1051 | LIST_INIT(&px.conf.args.list); |
| 1052 | |
| 1053 | if (!cli_has_level(appctx, ACCESS_LVL_OPER)) |
| 1054 | return 1; |
| 1055 | |
Willy Tarreau | e93bff4 | 2021-09-03 09:47:37 +0200 | [diff] [blame] | 1056 | if (!*args[2]) |
| 1057 | return cli_err(appctx, "Missing process-wide variable identifier.\n"); |
Willy Tarreau | b8bd1ee | 2021-03-26 15:19:50 +0100 | [diff] [blame] | 1058 | |
Willy Tarreau | e93bff4 | 2021-09-03 09:47:37 +0200 | [diff] [blame] | 1059 | if (!*args[3]) |
| 1060 | return cli_err(appctx, "Missing either 'expr', 'fmt' or expression.\n"); |
| 1061 | |
| 1062 | if (*args[4]) { |
| 1063 | /* this is the long format */ |
| 1064 | if (strcmp(args[3], "fmt") == 0) |
| 1065 | use_fmt = 1; |
| 1066 | else if (strcmp(args[3], "expr") != 0) { |
| 1067 | memprintf(&err, "'%s %s': arg type must be either 'expr' or 'fmt' but got '%s'.", args[0], args[1], args[3]); |
| 1068 | goto fail; |
| 1069 | } |
| 1070 | } |
| 1071 | |
| 1072 | tmp_act = NULL; |
| 1073 | if (!memprintf(&tmp_act, "set-var%s(%s)", use_fmt ? "-fmt" : "", args[2])) { |
Willy Tarreau | b8bd1ee | 2021-03-26 15:19:50 +0100 | [diff] [blame] | 1074 | memprintf(&err, "memory allocation error."); |
| 1075 | goto fail; |
| 1076 | } |
| 1077 | |
| 1078 | /* parse_store() will always return a message in <err> on error */ |
Willy Tarreau | e93bff4 | 2021-09-03 09:47:37 +0200 | [diff] [blame] | 1079 | tmp_args[0] = tmp_act; |
| 1080 | tmp_args[1] = (*args[4]) ? args[4] : args[3]; |
| 1081 | tmp_args[2] = ""; |
| 1082 | tmp_arg = 1; // must point to the first arg after the action |
| 1083 | p_ret = parse_store(tmp_args, &tmp_arg, &px, &rule, &err); |
| 1084 | free(tmp_act); |
Willy Tarreau | b8bd1ee | 2021-03-26 15:19:50 +0100 | [diff] [blame] | 1085 | |
| 1086 | if (p_ret != ACT_RET_PRS_OK) |
| 1087 | goto fail; |
| 1088 | |
| 1089 | if (rule.arg.vars.scope != SCOPE_PROC) { |
Willy Tarreau | c767eeb | 2021-09-03 10:23:26 +0200 | [diff] [blame] | 1090 | memprintf(&err, "'%s %s': cannot set variable '%s', only scope 'proc' is permitted here.", args[0], args[1], args[2]); |
Willy Tarreau | b8bd1ee | 2021-03-26 15:19:50 +0100 | [diff] [blame] | 1091 | goto fail; |
| 1092 | } |
| 1093 | |
| 1094 | err = NULL; |
| 1095 | nberr = smp_resolve_args(&px, &err); |
| 1096 | if (nberr) { |
| 1097 | release_sample_expr(rule.arg.vars.expr); |
| 1098 | indent_msg(&err, 2); |
| 1099 | goto fail; |
| 1100 | } |
| 1101 | |
Willy Tarreau | e93bff4 | 2021-09-03 09:47:37 +0200 | [diff] [blame] | 1102 | if (use_fmt && !(sess = session_new(&px, NULL, &objt))) { |
| 1103 | release_sample_expr(rule.arg.vars.expr); |
| 1104 | memprintf(&err, "memory allocation error."); |
| 1105 | goto fail; |
| 1106 | } |
| 1107 | |
| 1108 | action_store(&rule, &px, sess, NULL, 0); |
Willy Tarreau | b8bd1ee | 2021-03-26 15:19:50 +0100 | [diff] [blame] | 1109 | release_sample_expr(rule.arg.vars.expr); |
Willy Tarreau | e93bff4 | 2021-09-03 09:47:37 +0200 | [diff] [blame] | 1110 | if (sess) |
| 1111 | session_free(sess); |
| 1112 | |
Willy Tarreau | b8bd1ee | 2021-03-26 15:19:50 +0100 | [diff] [blame] | 1113 | appctx->st0 = CLI_ST_PROMPT; |
| 1114 | return 0; |
| 1115 | fail: |
| 1116 | return cli_dynerr(appctx, err); |
| 1117 | } |
| 1118 | |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 1119 | static int vars_max_size(char **args, int section_type, struct proxy *curpx, |
Willy Tarreau | 0182516 | 2021-03-09 09:53:46 +0100 | [diff] [blame] | 1120 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 1121 | char **err, unsigned int *limit) |
| 1122 | { |
| 1123 | char *error; |
| 1124 | |
| 1125 | *limit = strtol(args[1], &error, 10); |
| 1126 | if (*error != 0) { |
| 1127 | memprintf(err, "%s: '%s' is an invalid size", args[0], args[1]); |
| 1128 | return -1; |
| 1129 | } |
| 1130 | return 0; |
| 1131 | } |
| 1132 | |
| 1133 | static int vars_max_size_global(char **args, int section_type, struct proxy *curpx, |
Willy Tarreau | 0182516 | 2021-03-09 09:53:46 +0100 | [diff] [blame] | 1134 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 1135 | char **err) |
| 1136 | { |
| 1137 | return vars_max_size(args, section_type, curpx, defpx, file, line, err, &var_global_limit); |
| 1138 | } |
| 1139 | |
Christopher Faulet | ff2613e | 2016-11-09 11:36:17 +0100 | [diff] [blame] | 1140 | static int vars_max_size_proc(char **args, int section_type, struct proxy *curpx, |
Willy Tarreau | 0182516 | 2021-03-09 09:53:46 +0100 | [diff] [blame] | 1141 | const struct proxy *defpx, const char *file, int line, |
Christopher Faulet | ff2613e | 2016-11-09 11:36:17 +0100 | [diff] [blame] | 1142 | char **err) |
| 1143 | { |
| 1144 | return vars_max_size(args, section_type, curpx, defpx, file, line, err, &var_proc_limit); |
| 1145 | } |
| 1146 | |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 1147 | static int vars_max_size_sess(char **args, int section_type, struct proxy *curpx, |
Willy Tarreau | 0182516 | 2021-03-09 09:53:46 +0100 | [diff] [blame] | 1148 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 1149 | char **err) |
| 1150 | { |
| 1151 | return vars_max_size(args, section_type, curpx, defpx, file, line, err, &var_sess_limit); |
| 1152 | } |
| 1153 | |
| 1154 | static int vars_max_size_txn(char **args, int section_type, struct proxy *curpx, |
Willy Tarreau | 0182516 | 2021-03-09 09:53:46 +0100 | [diff] [blame] | 1155 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 1156 | char **err) |
| 1157 | { |
| 1158 | return vars_max_size(args, section_type, curpx, defpx, file, line, err, &var_txn_limit); |
| 1159 | } |
| 1160 | |
| 1161 | static int vars_max_size_reqres(char **args, int section_type, struct proxy *curpx, |
Willy Tarreau | 0182516 | 2021-03-09 09:53:46 +0100 | [diff] [blame] | 1162 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 1163 | char **err) |
| 1164 | { |
| 1165 | return vars_max_size(args, section_type, curpx, defpx, file, line, err, &var_reqres_limit); |
| 1166 | } |
| 1167 | |
Gaetan Rivet | 13a5043 | 2020-02-21 18:13:44 +0100 | [diff] [blame] | 1168 | static int vars_max_size_check(char **args, int section_type, struct proxy *curpx, |
Willy Tarreau | 0182516 | 2021-03-09 09:53:46 +0100 | [diff] [blame] | 1169 | const struct proxy *defpx, const char *file, int line, |
Gaetan Rivet | 13a5043 | 2020-02-21 18:13:44 +0100 | [diff] [blame] | 1170 | char **err) |
| 1171 | { |
| 1172 | return vars_max_size(args, section_type, curpx, defpx, file, line, err, &var_check_limit); |
| 1173 | } |
| 1174 | |
Willy Tarreau | 2c897d9 | 2021-08-31 08:48:55 +0200 | [diff] [blame] | 1175 | /* early boot initialization */ |
| 1176 | static void vars_init() |
| 1177 | { |
| 1178 | var_name_hash_seed = ha_random64(); |
| 1179 | } |
| 1180 | |
| 1181 | INITCALL0(STG_PREPARE, vars_init); |
| 1182 | |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 1183 | static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, { |
| 1184 | |
Willy Tarreau | 54496a6 | 2021-09-03 12:00:13 +0200 | [diff] [blame] | 1185 | { "var", smp_fetch_var, ARG2(1,STR,STR), smp_check_var, SMP_T_STR, SMP_USE_CONST }, |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 1186 | { /* END */ }, |
| 1187 | }}; |
| 1188 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 1189 | INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords); |
| 1190 | |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 1191 | static struct sample_conv_kw_list sample_conv_kws = {ILH, { |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 1192 | { "set-var", smp_conv_store, ARG1(1,STR), conv_check_var, SMP_T_ANY, SMP_T_ANY }, |
| 1193 | { "unset-var", smp_conv_clear, ARG1(1,STR), conv_check_var, SMP_T_ANY, SMP_T_ANY }, |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 1194 | { /* END */ }, |
| 1195 | }}; |
| 1196 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 1197 | INITCALL1(STG_REGISTER, sample_register_convs, &sample_conv_kws); |
| 1198 | |
Willy Tarreau | 620408f | 2016-10-21 16:37:51 +0200 | [diff] [blame] | 1199 | static struct action_kw_list tcp_req_sess_kws = { { }, { |
Willy Tarreau | 9a621ae | 2021-09-02 21:00:38 +0200 | [diff] [blame] | 1200 | { "set-var-fmt", parse_store, KWF_MATCH_PREFIX }, |
Amaury Denoyelle | e4a617c | 2021-05-06 15:33:09 +0200 | [diff] [blame] | 1201 | { "set-var", parse_store, KWF_MATCH_PREFIX }, |
| 1202 | { "unset-var", parse_store, KWF_MATCH_PREFIX }, |
Willy Tarreau | 620408f | 2016-10-21 16:37:51 +0200 | [diff] [blame] | 1203 | { /* END */ } |
| 1204 | }}; |
| 1205 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 1206 | INITCALL1(STG_REGISTER, tcp_req_sess_keywords_register, &tcp_req_sess_kws); |
| 1207 | |
Willy Tarreau | 620408f | 2016-10-21 16:37:51 +0200 | [diff] [blame] | 1208 | static struct action_kw_list tcp_req_cont_kws = { { }, { |
Willy Tarreau | 9a621ae | 2021-09-02 21:00:38 +0200 | [diff] [blame] | 1209 | { "set-var-fmt", parse_store, KWF_MATCH_PREFIX }, |
Amaury Denoyelle | e4a617c | 2021-05-06 15:33:09 +0200 | [diff] [blame] | 1210 | { "set-var", parse_store, KWF_MATCH_PREFIX }, |
| 1211 | { "unset-var", parse_store, KWF_MATCH_PREFIX }, |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 1212 | { /* END */ } |
| 1213 | }}; |
| 1214 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 1215 | INITCALL1(STG_REGISTER, tcp_req_cont_keywords_register, &tcp_req_cont_kws); |
| 1216 | |
Thierry FOURNIER | 36481b8 | 2015-08-19 09:01:53 +0200 | [diff] [blame] | 1217 | static struct action_kw_list tcp_res_kws = { { }, { |
Willy Tarreau | 9a621ae | 2021-09-02 21:00:38 +0200 | [diff] [blame] | 1218 | { "set-var-fmt", parse_store, KWF_MATCH_PREFIX }, |
Amaury Denoyelle | e4a617c | 2021-05-06 15:33:09 +0200 | [diff] [blame] | 1219 | { "set-var", parse_store, KWF_MATCH_PREFIX }, |
| 1220 | { "unset-var", parse_store, KWF_MATCH_PREFIX }, |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 1221 | { /* END */ } |
| 1222 | }}; |
| 1223 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 1224 | INITCALL1(STG_REGISTER, tcp_res_cont_keywords_register, &tcp_res_kws); |
| 1225 | |
Gaetan Rivet | 707b52f | 2020-02-21 18:14:59 +0100 | [diff] [blame] | 1226 | static struct action_kw_list tcp_check_kws = {ILH, { |
Willy Tarreau | 9a621ae | 2021-09-02 21:00:38 +0200 | [diff] [blame] | 1227 | { "set-var-fmt", parse_store, KWF_MATCH_PREFIX }, |
Amaury Denoyelle | e4a617c | 2021-05-06 15:33:09 +0200 | [diff] [blame] | 1228 | { "set-var", parse_store, KWF_MATCH_PREFIX }, |
| 1229 | { "unset-var", parse_store, KWF_MATCH_PREFIX }, |
Gaetan Rivet | 707b52f | 2020-02-21 18:14:59 +0100 | [diff] [blame] | 1230 | { /* END */ } |
| 1231 | }}; |
| 1232 | |
| 1233 | INITCALL1(STG_REGISTER, tcp_check_keywords_register, &tcp_check_kws); |
| 1234 | |
Thierry FOURNIER | 36481b8 | 2015-08-19 09:01:53 +0200 | [diff] [blame] | 1235 | static struct action_kw_list http_req_kws = { { }, { |
Willy Tarreau | 9a621ae | 2021-09-02 21:00:38 +0200 | [diff] [blame] | 1236 | { "set-var-fmt", parse_store, KWF_MATCH_PREFIX }, |
Amaury Denoyelle | e4a617c | 2021-05-06 15:33:09 +0200 | [diff] [blame] | 1237 | { "set-var", parse_store, KWF_MATCH_PREFIX }, |
| 1238 | { "unset-var", parse_store, KWF_MATCH_PREFIX }, |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 1239 | { /* END */ } |
| 1240 | }}; |
| 1241 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 1242 | INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_kws); |
| 1243 | |
Thierry FOURNIER | 36481b8 | 2015-08-19 09:01:53 +0200 | [diff] [blame] | 1244 | static struct action_kw_list http_res_kws = { { }, { |
Willy Tarreau | 9a621ae | 2021-09-02 21:00:38 +0200 | [diff] [blame] | 1245 | { "set-var-fmt", parse_store, KWF_MATCH_PREFIX }, |
Amaury Denoyelle | e4a617c | 2021-05-06 15:33:09 +0200 | [diff] [blame] | 1246 | { "set-var", parse_store, KWF_MATCH_PREFIX }, |
| 1247 | { "unset-var", parse_store, KWF_MATCH_PREFIX }, |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 1248 | { /* END */ } |
| 1249 | }}; |
| 1250 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 1251 | INITCALL1(STG_REGISTER, http_res_keywords_register, &http_res_kws); |
| 1252 | |
Christopher Faulet | 6d0c3df | 2020-01-22 09:26:35 +0100 | [diff] [blame] | 1253 | static struct action_kw_list http_after_res_kws = { { }, { |
Willy Tarreau | 9a621ae | 2021-09-02 21:00:38 +0200 | [diff] [blame] | 1254 | { "set-var-fmt", parse_store, KWF_MATCH_PREFIX }, |
Amaury Denoyelle | e4a617c | 2021-05-06 15:33:09 +0200 | [diff] [blame] | 1255 | { "set-var", parse_store, KWF_MATCH_PREFIX }, |
| 1256 | { "unset-var", parse_store, KWF_MATCH_PREFIX }, |
Christopher Faulet | 6d0c3df | 2020-01-22 09:26:35 +0100 | [diff] [blame] | 1257 | { /* END */ } |
| 1258 | }}; |
| 1259 | |
| 1260 | INITCALL1(STG_REGISTER, http_after_res_keywords_register, &http_after_res_kws); |
| 1261 | |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 1262 | static struct cfg_kw_list cfg_kws = {{ },{ |
Willy Tarreau | 13d2ba2 | 2021-03-26 11:38:08 +0100 | [diff] [blame] | 1263 | { CFG_GLOBAL, "set-var", vars_parse_global_set_var }, |
Willy Tarreau | 753d4db | 2021-09-03 09:02:47 +0200 | [diff] [blame] | 1264 | { CFG_GLOBAL, "set-var-fmt", vars_parse_global_set_var }, |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 1265 | { CFG_GLOBAL, "tune.vars.global-max-size", vars_max_size_global }, |
Christopher Faulet | ff2613e | 2016-11-09 11:36:17 +0100 | [diff] [blame] | 1266 | { CFG_GLOBAL, "tune.vars.proc-max-size", vars_max_size_proc }, |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 1267 | { CFG_GLOBAL, "tune.vars.sess-max-size", vars_max_size_sess }, |
| 1268 | { CFG_GLOBAL, "tune.vars.txn-max-size", vars_max_size_txn }, |
| 1269 | { CFG_GLOBAL, "tune.vars.reqres-max-size", vars_max_size_reqres }, |
Gaetan Rivet | 13a5043 | 2020-02-21 18:13:44 +0100 | [diff] [blame] | 1270 | { CFG_GLOBAL, "tune.vars.check-max-size", vars_max_size_check }, |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 1271 | { /* END */ } |
| 1272 | }}; |
| 1273 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 1274 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |
Willy Tarreau | c35eb38 | 2021-03-26 14:51:31 +0100 | [diff] [blame] | 1275 | |
| 1276 | |
| 1277 | /* register cli keywords */ |
| 1278 | static struct cli_kw_list cli_kws = {{ },{ |
Willy Tarreau | b205bfd | 2021-05-07 11:38:37 +0200 | [diff] [blame] | 1279 | { { "get", "var", NULL }, "get var <name> : retrieve contents of a process-wide variable", vars_parse_cli_get_var, NULL }, |
Willy Tarreau | e93bff4 | 2021-09-03 09:47:37 +0200 | [diff] [blame] | 1280 | { { "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 Tarreau | c35eb38 | 2021-03-26 14:51:31 +0100 | [diff] [blame] | 1281 | { { NULL }, NULL, NULL, NULL } |
| 1282 | }}; |
| 1283 | INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws); |