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