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