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