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> |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 4 | #include <common/cfgparse.h> |
Willy Tarreau | cd72d8c | 2020-06-02 19:11:26 +0200 | [diff] [blame] | 5 | #include <haproxy/http.h> |
Willy Tarreau | c761f84 | 2020-06-04 11:40:28 +0200 | [diff] [blame] | 6 | #include <haproxy/http_rules.h> |
Willy Tarreau | 853b297 | 2020-05-27 18:01:47 +0200 | [diff] [blame] | 7 | #include <haproxy/list.h> |
Willy Tarreau | e6ce10b | 2020-06-04 15:33:47 +0200 | [diff] [blame] | 8 | #include <haproxy/sample.h> |
Willy Tarreau | a171892 | 2020-06-04 16:25:31 +0200 | [diff] [blame] | 9 | #include <haproxy/vars.h> |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 10 | |
Willy Tarreau | aa74c4e | 2020-06-04 10:19:23 +0200 | [diff] [blame] | 11 | #include <haproxy/arg.h> |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 12 | #include <proto/http_ana.h> |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 13 | #include <proto/stream.h> |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 14 | #include <proto/tcp_rules.h> |
Gaetan Rivet | 13a5043 | 2020-02-21 18:13:44 +0100 | [diff] [blame] | 15 | #include <proto/checks.h> |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 16 | |
| 17 | /* This contains a pool of struct vars */ |
Willy Tarreau | 8ceae72 | 2018-11-26 11:58:30 +0100 | [diff] [blame] | 18 | DECLARE_STATIC_POOL(var_pool, "vars", sizeof(struct var)); |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 19 | |
| 20 | /* This array contain all the names of all the HAProxy vars. |
| 21 | * This permits to identify two variables name with |
| 22 | * only one pointer. It permits to not using strdup() for |
| 23 | * each variable name used during the runtime. |
| 24 | */ |
| 25 | static char **var_names = NULL; |
| 26 | static int var_names_nb = 0; |
| 27 | |
| 28 | /* This array of int contains the system limits per context. */ |
| 29 | static unsigned int var_global_limit = 0; |
| 30 | static unsigned int var_global_size = 0; |
Christopher Faulet | ff2613e | 2016-11-09 11:36:17 +0100 | [diff] [blame] | 31 | static unsigned int var_proc_limit = 0; |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 32 | static unsigned int var_sess_limit = 0; |
| 33 | static unsigned int var_txn_limit = 0; |
| 34 | static unsigned int var_reqres_limit = 0; |
Gaetan Rivet | 13a5043 | 2020-02-21 18:13:44 +0100 | [diff] [blame] | 35 | static unsigned int var_check_limit = 0; |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 36 | |
Willy Tarreau | 86abe44 | 2018-11-25 20:12:18 +0100 | [diff] [blame] | 37 | __decl_rwlock(var_names_rwlock); |
Christopher Faulet | e95f2c3 | 2017-07-24 16:30:34 +0200 | [diff] [blame] | 38 | |
Willy Tarreau | f37b140 | 2019-06-04 16:27:36 +0200 | [diff] [blame] | 39 | /* returns the struct vars pointer for a session, stream and scope, or NULL if |
| 40 | * it does not exist. |
| 41 | */ |
| 42 | static inline struct vars *get_vars(struct session *sess, struct stream *strm, enum vars_scope scope) |
| 43 | { |
| 44 | switch (scope) { |
| 45 | case SCOPE_PROC: |
| 46 | return &global.vars; |
| 47 | case SCOPE_SESS: |
| 48 | return &sess->vars; |
Gaetan Rivet | 13a5043 | 2020-02-21 18:13:44 +0100 | [diff] [blame] | 49 | case SCOPE_CHECK: { |
Christopher Faulet | 0fca7ed | 2020-04-21 11:53:32 +0200 | [diff] [blame] | 50 | struct check *check = objt_check(sess->origin); |
Gaetan Rivet | 13a5043 | 2020-02-21 18:13:44 +0100 | [diff] [blame] | 51 | |
Christopher Faulet | 0fca7ed | 2020-04-21 11:53:32 +0200 | [diff] [blame] | 52 | return check ? &check->vars : NULL; |
Gaetan Rivet | 13a5043 | 2020-02-21 18:13:44 +0100 | [diff] [blame] | 53 | } |
Willy Tarreau | f37b140 | 2019-06-04 16:27:36 +0200 | [diff] [blame] | 54 | case SCOPE_TXN: |
| 55 | return strm ? &strm->vars_txn : NULL; |
| 56 | case SCOPE_REQ: |
| 57 | case SCOPE_RES: |
| 58 | default: |
| 59 | return strm ? &strm->vars_reqres : NULL; |
| 60 | } |
| 61 | } |
| 62 | |
Willy Tarreau | 7233098 | 2015-06-19 11:21:56 +0200 | [diff] [blame] | 63 | /* This function adds or remove memory size from the accounting. The inner |
| 64 | * pointers may be null when setting the outer ones only. |
| 65 | */ |
Willy Tarreau | 6204cd9 | 2016-03-10 16:33:04 +0100 | [diff] [blame] | 66 | static void var_accounting_diff(struct vars *vars, struct session *sess, struct stream *strm, int size) |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 67 | { |
| 68 | switch (vars->scope) { |
| 69 | case SCOPE_REQ: |
| 70 | case SCOPE_RES: |
Willy Tarreau | f37b140 | 2019-06-04 16:27:36 +0200 | [diff] [blame] | 71 | if (strm) |
| 72 | _HA_ATOMIC_ADD(&strm->vars_reqres.size, size); |
Willy Tarreau | 6204cd9 | 2016-03-10 16:33:04 +0100 | [diff] [blame] | 73 | /* fall through */ |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 74 | case SCOPE_TXN: |
Willy Tarreau | f37b140 | 2019-06-04 16:27:36 +0200 | [diff] [blame] | 75 | if (strm) |
| 76 | _HA_ATOMIC_ADD(&strm->vars_txn.size, size); |
Gaetan Rivet | 13a5043 | 2020-02-21 18:13:44 +0100 | [diff] [blame] | 77 | goto scope_sess; |
| 78 | case SCOPE_CHECK: { |
Christopher Faulet | 0fca7ed | 2020-04-21 11:53:32 +0200 | [diff] [blame] | 79 | struct check *check = objt_check(sess->origin); |
Gaetan Rivet | 13a5043 | 2020-02-21 18:13:44 +0100 | [diff] [blame] | 80 | |
Christopher Faulet | 0fca7ed | 2020-04-21 11:53:32 +0200 | [diff] [blame] | 81 | if (check) |
| 82 | _HA_ATOMIC_ADD(&check->vars.size, size); |
Gaetan Rivet | 13a5043 | 2020-02-21 18:13:44 +0100 | [diff] [blame] | 83 | } |
Willy Tarreau | 6204cd9 | 2016-03-10 16:33:04 +0100 | [diff] [blame] | 84 | /* fall through */ |
Gaetan Rivet | 13a5043 | 2020-02-21 18:13:44 +0100 | [diff] [blame] | 85 | scope_sess: |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 86 | case SCOPE_SESS: |
Olivier Houchard | 25ad13f | 2019-03-08 18:55:38 +0100 | [diff] [blame] | 87 | _HA_ATOMIC_ADD(&sess->vars.size, size); |
Christopher Faulet | ff2613e | 2016-11-09 11:36:17 +0100 | [diff] [blame] | 88 | /* fall through */ |
| 89 | case SCOPE_PROC: |
Olivier Houchard | 25ad13f | 2019-03-08 18:55:38 +0100 | [diff] [blame] | 90 | _HA_ATOMIC_ADD(&global.vars.size, size); |
| 91 | _HA_ATOMIC_ADD(&var_global_size, size); |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 92 | } |
| 93 | } |
| 94 | |
| 95 | /* This function returns 1 if the <size> is available in the var |
Joseph Herlant | 0767689 | 2018-11-15 09:19:50 -0800 | [diff] [blame] | 96 | * pool <vars>, otherwise returns 0. If the space is available, |
Willy Tarreau | 7233098 | 2015-06-19 11:21:56 +0200 | [diff] [blame] | 97 | * the size is reserved. The inner pointers may be null when setting |
Willy Tarreau | 6204cd9 | 2016-03-10 16:33:04 +0100 | [diff] [blame] | 98 | * the outer ones only. The accounting uses either <sess> or <strm> |
| 99 | * depending on the scope. <strm> may be NULL when no stream is known |
| 100 | * and only the session exists (eg: tcp-request connection). |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 101 | */ |
Willy Tarreau | 6204cd9 | 2016-03-10 16:33:04 +0100 | [diff] [blame] | 102 | 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] | 103 | { |
| 104 | switch (vars->scope) { |
| 105 | case SCOPE_REQ: |
| 106 | case SCOPE_RES: |
Willy Tarreau | f37b140 | 2019-06-04 16:27:36 +0200 | [diff] [blame] | 107 | 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] | 108 | return 0; |
Willy Tarreau | 6204cd9 | 2016-03-10 16:33:04 +0100 | [diff] [blame] | 109 | /* fall through */ |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 110 | case SCOPE_TXN: |
Willy Tarreau | f37b140 | 2019-06-04 16:27:36 +0200 | [diff] [blame] | 111 | 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] | 112 | return 0; |
Gaetan Rivet | 13a5043 | 2020-02-21 18:13:44 +0100 | [diff] [blame] | 113 | goto scope_sess; |
| 114 | case SCOPE_CHECK: { |
Christopher Faulet | 0fca7ed | 2020-04-21 11:53:32 +0200 | [diff] [blame] | 115 | struct check *check = objt_check(sess->origin); |
Gaetan Rivet | 13a5043 | 2020-02-21 18:13:44 +0100 | [diff] [blame] | 116 | |
Christopher Faulet | 0fca7ed | 2020-04-21 11:53:32 +0200 | [diff] [blame] | 117 | if (var_check_limit && check && check->vars.size + size > var_check_limit) |
Gaetan Rivet | 13a5043 | 2020-02-21 18:13:44 +0100 | [diff] [blame] | 118 | return 0; |
| 119 | } |
Willy Tarreau | 6204cd9 | 2016-03-10 16:33:04 +0100 | [diff] [blame] | 120 | /* fall through */ |
Gaetan Rivet | 13a5043 | 2020-02-21 18:13:44 +0100 | [diff] [blame] | 121 | scope_sess: |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 122 | case SCOPE_SESS: |
Willy Tarreau | 6204cd9 | 2016-03-10 16:33:04 +0100 | [diff] [blame] | 123 | if (var_sess_limit && sess->vars.size + size > var_sess_limit) |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 124 | return 0; |
Christopher Faulet | ff2613e | 2016-11-09 11:36:17 +0100 | [diff] [blame] | 125 | /* fall through */ |
| 126 | case SCOPE_PROC: |
| 127 | if (var_proc_limit && global.vars.size + size > var_proc_limit) |
| 128 | return 0; |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 129 | if (var_global_limit && var_global_size + size > var_global_limit) |
| 130 | return 0; |
| 131 | } |
Willy Tarreau | 6204cd9 | 2016-03-10 16:33:04 +0100 | [diff] [blame] | 132 | var_accounting_diff(vars, sess, strm, size); |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 133 | return 1; |
| 134 | } |
| 135 | |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 136 | /* This fnuction remove a variable from the list and free memory it used */ |
| 137 | unsigned int var_clear(struct var *var) |
| 138 | { |
| 139 | unsigned int size = 0; |
| 140 | |
| 141 | if (var->data.type == SMP_T_STR || var->data.type == SMP_T_BIN) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 142 | free(var->data.u.str.area); |
| 143 | size += var->data.u.str.data; |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 144 | } |
Christopher Faulet | d02210c | 2017-07-24 16:24:39 +0200 | [diff] [blame] | 145 | else if (var->data.type == SMP_T_METH && var->data.u.meth.meth == HTTP_METH_OTHER) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 146 | free(var->data.u.meth.str.area); |
| 147 | size += var->data.u.meth.str.data; |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 148 | } |
| 149 | LIST_DEL(&var->l); |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 150 | pool_free(var_pool, var); |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 151 | size += sizeof(struct var); |
| 152 | return size; |
| 153 | } |
| 154 | |
Joseph Herlant | 0767689 | 2018-11-15 09:19:50 -0800 | [diff] [blame] | 155 | /* This function free all the memory used by all the variables |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 156 | * in the list. |
| 157 | */ |
Willy Tarreau | 6204cd9 | 2016-03-10 16:33:04 +0100 | [diff] [blame] | 158 | void vars_prune(struct vars *vars, struct session *sess, struct stream *strm) |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 159 | { |
| 160 | struct var *var, *tmp; |
Willy Tarreau | 7233098 | 2015-06-19 11:21:56 +0200 | [diff] [blame] | 161 | unsigned int size = 0; |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 162 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 163 | HA_RWLOCK_WRLOCK(VARS_LOCK, &vars->rwlock); |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 164 | list_for_each_entry_safe(var, tmp, &vars->head, l) { |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 165 | size += var_clear(var); |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 166 | } |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 167 | HA_RWLOCK_WRUNLOCK(VARS_LOCK, &vars->rwlock); |
Willy Tarreau | 6204cd9 | 2016-03-10 16:33:04 +0100 | [diff] [blame] | 168 | var_accounting_diff(vars, sess, strm, -size); |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 169 | } |
| 170 | |
Willy Tarreau | ebcd484 | 2015-06-19 11:59:02 +0200 | [diff] [blame] | 171 | /* This function frees all the memory used by all the session variables in the |
| 172 | * list starting at <vars>. |
| 173 | */ |
| 174 | void vars_prune_per_sess(struct vars *vars) |
| 175 | { |
| 176 | struct var *var, *tmp; |
| 177 | unsigned int size = 0; |
| 178 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 179 | HA_RWLOCK_WRLOCK(VARS_LOCK, &vars->rwlock); |
Willy Tarreau | ebcd484 | 2015-06-19 11:59:02 +0200 | [diff] [blame] | 180 | list_for_each_entry_safe(var, tmp, &vars->head, l) { |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 181 | size += var_clear(var); |
Willy Tarreau | ebcd484 | 2015-06-19 11:59:02 +0200 | [diff] [blame] | 182 | } |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 183 | HA_RWLOCK_WRUNLOCK(VARS_LOCK, &vars->rwlock); |
Christopher Faulet | e95f2c3 | 2017-07-24 16:30:34 +0200 | [diff] [blame] | 184 | |
Olivier Houchard | 25ad13f | 2019-03-08 18:55:38 +0100 | [diff] [blame] | 185 | _HA_ATOMIC_SUB(&vars->size, size); |
| 186 | _HA_ATOMIC_SUB(&global.vars.size, size); |
| 187 | _HA_ATOMIC_SUB(&var_global_size, size); |
Willy Tarreau | ebcd484 | 2015-06-19 11:59:02 +0200 | [diff] [blame] | 188 | } |
| 189 | |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 190 | /* This function init a list of variabes. */ |
| 191 | void vars_init(struct vars *vars, enum vars_scope scope) |
| 192 | { |
| 193 | LIST_INIT(&vars->head); |
| 194 | vars->scope = scope; |
| 195 | vars->size = 0; |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 196 | HA_RWLOCK_INIT(&vars->rwlock); |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | /* This function declares a new variable name. It returns a pointer |
| 200 | * on the string identifying the name. This function assures that |
| 201 | * the same name exists only once. |
| 202 | * |
| 203 | * This function check if the variable name is acceptable. |
| 204 | * |
| 205 | * The function returns NULL if an error occurs, and <err> is filled. |
| 206 | * In this case, the HAProxy must be stopped because the structs are |
| 207 | * left inconsistent. Otherwise, it returns the pointer on the global |
| 208 | * name. |
| 209 | */ |
Christopher Faulet | 09c9df2 | 2016-10-31 11:05:37 +0100 | [diff] [blame] | 210 | static char *register_name(const char *name, int len, enum vars_scope *scope, |
| 211 | int alloc, char **err) |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 212 | { |
| 213 | int i; |
Hubert Verstraete | 831962e | 2016-06-28 22:44:26 +0200 | [diff] [blame] | 214 | char **var_names2; |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 215 | const char *tmp; |
Christopher Faulet | e95f2c3 | 2017-07-24 16:30:34 +0200 | [diff] [blame] | 216 | char *res = NULL; |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 217 | |
| 218 | /* Check length. */ |
| 219 | if (len == 0) { |
| 220 | memprintf(err, "Empty variable name cannot be accepted"); |
Christopher Faulet | eb3e276 | 2017-12-08 09:17:39 +0100 | [diff] [blame] | 221 | return res; |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | /* Check scope. */ |
Christopher Faulet | ff2613e | 2016-11-09 11:36:17 +0100 | [diff] [blame] | 225 | if (len > 5 && strncmp(name, "proc.", 5) == 0) { |
| 226 | name += 5; |
| 227 | len -= 5; |
| 228 | *scope = SCOPE_PROC; |
| 229 | } |
| 230 | else if (len > 5 && strncmp(name, "sess.", 5) == 0) { |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 231 | name += 5; |
| 232 | len -= 5; |
| 233 | *scope = SCOPE_SESS; |
| 234 | } |
| 235 | else if (len > 4 && strncmp(name, "txn.", 4) == 0) { |
| 236 | name += 4; |
| 237 | len -= 4; |
| 238 | *scope = SCOPE_TXN; |
| 239 | } |
| 240 | else if (len > 4 && strncmp(name, "req.", 4) == 0) { |
| 241 | name += 4; |
| 242 | len -= 4; |
| 243 | *scope = SCOPE_REQ; |
| 244 | } |
| 245 | else if (len > 4 && strncmp(name, "res.", 4) == 0) { |
| 246 | name += 4; |
| 247 | len -= 4; |
| 248 | *scope = SCOPE_RES; |
| 249 | } |
Gaetan Rivet | 13a5043 | 2020-02-21 18:13:44 +0100 | [diff] [blame] | 250 | else if (len > 6 && strncmp(name, "check.", 6) == 0) { |
| 251 | name += 6; |
| 252 | len -= 6; |
| 253 | *scope = SCOPE_CHECK; |
| 254 | } |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 255 | else { |
| 256 | memprintf(err, "invalid variable name '%s'. A variable name must be start by its scope. " |
Gaetan Rivet | 13a5043 | 2020-02-21 18:13:44 +0100 | [diff] [blame] | 257 | "The scope can be 'proc', 'sess', 'txn', 'req', 'res' or 'check'", name); |
Christopher Faulet | eb3e276 | 2017-12-08 09:17:39 +0100 | [diff] [blame] | 258 | return res; |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 259 | } |
| 260 | |
Christopher Faulet | e95f2c3 | 2017-07-24 16:30:34 +0200 | [diff] [blame] | 261 | if (alloc) |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 262 | HA_RWLOCK_WRLOCK(VARS_LOCK, &var_names_rwlock); |
Christopher Faulet | e95f2c3 | 2017-07-24 16:30:34 +0200 | [diff] [blame] | 263 | else |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 264 | HA_RWLOCK_RDLOCK(VARS_LOCK, &var_names_rwlock); |
Christopher Faulet | e95f2c3 | 2017-07-24 16:30:34 +0200 | [diff] [blame] | 265 | |
| 266 | |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 267 | /* Look for existing variable name. */ |
| 268 | for (i = 0; i < var_names_nb; i++) |
Christopher Faulet | e95f2c3 | 2017-07-24 16:30:34 +0200 | [diff] [blame] | 269 | if (strncmp(var_names[i], name, len) == 0 && var_names[i][len] == '\0') { |
| 270 | res = var_names[i]; |
| 271 | goto end; |
| 272 | } |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 273 | |
Christopher Faulet | e95f2c3 | 2017-07-24 16:30:34 +0200 | [diff] [blame] | 274 | if (!alloc) { |
| 275 | res = NULL; |
| 276 | goto end; |
| 277 | } |
Christopher Faulet | 09c9df2 | 2016-10-31 11:05:37 +0100 | [diff] [blame] | 278 | |
Hubert Verstraete | 831962e | 2016-06-28 22:44:26 +0200 | [diff] [blame] | 279 | /* Store variable name. If realloc fails, var_names remains valid */ |
| 280 | var_names2 = realloc(var_names, (var_names_nb + 1) * sizeof(*var_names)); |
| 281 | if (!var_names2) { |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 282 | memprintf(err, "out of memory error"); |
Christopher Faulet | e95f2c3 | 2017-07-24 16:30:34 +0200 | [diff] [blame] | 283 | res = NULL; |
| 284 | goto end; |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 285 | } |
Hubert Verstraete | 831962e | 2016-06-28 22:44:26 +0200 | [diff] [blame] | 286 | var_names_nb++; |
| 287 | var_names = var_names2; |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 288 | var_names[var_names_nb - 1] = malloc(len + 1); |
| 289 | if (!var_names[var_names_nb - 1]) { |
| 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 | } |
| 294 | memcpy(var_names[var_names_nb - 1], name, len); |
| 295 | var_names[var_names_nb - 1][len] = '\0'; |
| 296 | |
| 297 | /* Check variable name syntax. */ |
| 298 | tmp = var_names[var_names_nb - 1]; |
| 299 | while (*tmp) { |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 300 | if (!isalnum((unsigned char)*tmp) && *tmp != '_' && *tmp != '.') { |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 301 | memprintf(err, "invalid syntax at char '%s'", tmp); |
Christopher Faulet | e95f2c3 | 2017-07-24 16:30:34 +0200 | [diff] [blame] | 302 | res = NULL; |
| 303 | goto end; |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 304 | } |
| 305 | tmp++; |
| 306 | } |
Christopher Faulet | e95f2c3 | 2017-07-24 16:30:34 +0200 | [diff] [blame] | 307 | res = var_names[var_names_nb - 1]; |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 308 | |
Christopher Faulet | e95f2c3 | 2017-07-24 16:30:34 +0200 | [diff] [blame] | 309 | end: |
| 310 | if (alloc) |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 311 | HA_RWLOCK_WRUNLOCK(VARS_LOCK, &var_names_rwlock); |
Christopher Faulet | e95f2c3 | 2017-07-24 16:30:34 +0200 | [diff] [blame] | 312 | else |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 313 | HA_RWLOCK_RDUNLOCK(VARS_LOCK, &var_names_rwlock); |
Christopher Faulet | e95f2c3 | 2017-07-24 16:30:34 +0200 | [diff] [blame] | 314 | |
| 315 | return res; |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | /* This function returns an existing variable or returns NULL. */ |
| 319 | static inline struct var *var_get(struct vars *vars, const char *name) |
| 320 | { |
| 321 | struct var *var; |
| 322 | |
| 323 | list_for_each_entry(var, &vars->head, l) |
| 324 | if (var->name == name) |
| 325 | return var; |
| 326 | return NULL; |
| 327 | } |
| 328 | |
| 329 | /* Returns 0 if fails, else returns 1. */ |
| 330 | static int smp_fetch_var(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 331 | { |
| 332 | const struct var_desc *var_desc = &args[0].data.var; |
| 333 | struct var *var; |
| 334 | struct vars *vars; |
| 335 | |
| 336 | /* Check the availibity of the variable. */ |
Willy Tarreau | f37b140 | 2019-06-04 16:27:36 +0200 | [diff] [blame] | 337 | vars = get_vars(smp->sess, smp->strm, var_desc->scope); |
| 338 | if (!vars || vars->scope != var_desc->scope) |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 339 | return 0; |
Christopher Faulet | e95f2c3 | 2017-07-24 16:30:34 +0200 | [diff] [blame] | 340 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 341 | HA_RWLOCK_RDLOCK(VARS_LOCK, &vars->rwlock); |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 342 | var = var_get(vars, var_desc->name); |
| 343 | |
| 344 | /* check for the variable avalaibility */ |
Christopher Faulet | e95f2c3 | 2017-07-24 16:30:34 +0200 | [diff] [blame] | 345 | if (!var) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 346 | HA_RWLOCK_RDUNLOCK(VARS_LOCK, &vars->rwlock); |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 347 | return 0; |
Christopher Faulet | e95f2c3 | 2017-07-24 16:30:34 +0200 | [diff] [blame] | 348 | } |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 349 | |
Christopher Faulet | e95f2c3 | 2017-07-24 16:30:34 +0200 | [diff] [blame] | 350 | /* Duplicate the sample data because it could modified by another |
| 351 | * thread */ |
Thierry FOURNIER | 5cc18d4 | 2015-08-19 09:02:36 +0200 | [diff] [blame] | 352 | smp->data = var->data; |
Christopher Faulet | e95f2c3 | 2017-07-24 16:30:34 +0200 | [diff] [blame] | 353 | smp_dup(smp); |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 354 | smp->flags |= SMP_F_CONST; |
Christopher Faulet | e95f2c3 | 2017-07-24 16:30:34 +0200 | [diff] [blame] | 355 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 356 | HA_RWLOCK_RDUNLOCK(VARS_LOCK, &vars->rwlock); |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 357 | return 1; |
| 358 | } |
| 359 | |
| 360 | /* This function search in the <head> a variable with the same |
| 361 | * pointer value that the <name>. If the variable doesn't exists, |
| 362 | * create it. The function stores a copy of smp> if the variable. |
| 363 | * It returns 0 if fails, else returns 1. |
| 364 | */ |
Willy Tarreau | 6204cd9 | 2016-03-10 16:33:04 +0100 | [diff] [blame] | 365 | 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] | 366 | { |
| 367 | struct var *var; |
| 368 | |
| 369 | /* Look for existing variable name. */ |
| 370 | var = var_get(vars, name); |
| 371 | |
| 372 | if (var) { |
| 373 | /* free its used memory. */ |
| 374 | if (var->data.type == SMP_T_STR || |
| 375 | var->data.type == SMP_T_BIN) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 376 | free(var->data.u.str.area); |
| 377 | var_accounting_diff(vars, smp->sess, smp->strm, |
| 378 | -var->data.u.str.data); |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 379 | } |
Christopher Faulet | d02210c | 2017-07-24 16:24:39 +0200 | [diff] [blame] | 380 | else if (var->data.type == SMP_T_METH && var->data.u.meth.meth == HTTP_METH_OTHER) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 381 | free(var->data.u.meth.str.area); |
| 382 | var_accounting_diff(vars, smp->sess, smp->strm, |
| 383 | -var->data.u.meth.str.data); |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 384 | } |
| 385 | } else { |
| 386 | |
Joseph Herlant | 0767689 | 2018-11-15 09:19:50 -0800 | [diff] [blame] | 387 | /* Check memory available. */ |
Willy Tarreau | 6204cd9 | 2016-03-10 16:33:04 +0100 | [diff] [blame] | 388 | if (!var_accounting_add(vars, smp->sess, smp->strm, sizeof(struct var))) |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 389 | return 0; |
| 390 | |
| 391 | /* Create new entry. */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 392 | var = pool_alloc(var_pool); |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 393 | if (!var) |
| 394 | return 0; |
| 395 | LIST_ADDQ(&vars->head, &var->l); |
| 396 | var->name = name; |
| 397 | } |
| 398 | |
| 399 | /* Set type. */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 400 | var->data.type = smp->data.type; |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 401 | |
| 402 | /* Copy data. If the data needs memory, the function can fail. */ |
| 403 | switch (var->data.type) { |
| 404 | case SMP_T_BOOL: |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 405 | case SMP_T_SINT: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 406 | var->data.u.sint = smp->data.u.sint; |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 407 | break; |
| 408 | case SMP_T_IPV4: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 409 | var->data.u.ipv4 = smp->data.u.ipv4; |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 410 | break; |
| 411 | case SMP_T_IPV6: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 412 | var->data.u.ipv6 = smp->data.u.ipv6; |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 413 | break; |
| 414 | case SMP_T_STR: |
| 415 | case SMP_T_BIN: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 416 | 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] | 417 | var->data.type = SMP_T_BOOL; /* This type doesn't use additional memory. */ |
| 418 | return 0; |
| 419 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 420 | var->data.u.str.area = malloc(smp->data.u.str.data); |
| 421 | if (!var->data.u.str.area) { |
| 422 | var_accounting_diff(vars, smp->sess, smp->strm, |
| 423 | -smp->data.u.str.data); |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 424 | var->data.type = SMP_T_BOOL; /* This type doesn't use additional memory. */ |
| 425 | return 0; |
| 426 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 427 | var->data.u.str.data = smp->data.u.str.data; |
| 428 | memcpy(var->data.u.str.area, smp->data.u.str.area, |
| 429 | var->data.u.str.data); |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 430 | break; |
| 431 | case SMP_T_METH: |
Christopher Faulet | d02210c | 2017-07-24 16:24:39 +0200 | [diff] [blame] | 432 | var->data.u.meth.meth = smp->data.u.meth.meth; |
| 433 | if (smp->data.u.meth.meth != HTTP_METH_OTHER) |
| 434 | break; |
| 435 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 436 | 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] | 437 | var->data.type = SMP_T_BOOL; /* This type doesn't use additional memory. */ |
| 438 | return 0; |
| 439 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 440 | var->data.u.meth.str.area = malloc(smp->data.u.meth.str.data); |
| 441 | if (!var->data.u.meth.str.area) { |
| 442 | var_accounting_diff(vars, smp->sess, smp->strm, |
| 443 | -smp->data.u.meth.str.data); |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 444 | var->data.type = SMP_T_BOOL; /* This type doesn't use additional memory. */ |
| 445 | return 0; |
| 446 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 447 | var->data.u.meth.str.data = smp->data.u.meth.str.data; |
| 448 | var->data.u.meth.str.size = smp->data.u.meth.str.data; |
| 449 | memcpy(var->data.u.meth.str.area, smp->data.u.meth.str.area, |
| 450 | var->data.u.meth.str.data); |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 451 | break; |
| 452 | } |
| 453 | return 1; |
| 454 | } |
| 455 | |
Willy Tarreau | 620408f | 2016-10-21 16:37:51 +0200 | [diff] [blame] | 456 | /* 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] | 457 | 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] | 458 | { |
| 459 | struct vars *vars; |
Christopher Faulet | e95f2c3 | 2017-07-24 16:30:34 +0200 | [diff] [blame] | 460 | int ret; |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 461 | |
Willy Tarreau | f37b140 | 2019-06-04 16:27:36 +0200 | [diff] [blame] | 462 | vars = get_vars(smp->sess, smp->strm, scope); |
| 463 | if (!vars || vars->scope != scope) |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 464 | return 0; |
Christopher Faulet | e95f2c3 | 2017-07-24 16:30:34 +0200 | [diff] [blame] | 465 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 466 | HA_RWLOCK_WRLOCK(VARS_LOCK, &vars->rwlock); |
Christopher Faulet | e95f2c3 | 2017-07-24 16:30:34 +0200 | [diff] [blame] | 467 | ret = sample_store(vars, name, smp); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 468 | HA_RWLOCK_WRUNLOCK(VARS_LOCK, &vars->rwlock); |
Christopher Faulet | e95f2c3 | 2017-07-24 16:30:34 +0200 | [diff] [blame] | 469 | return ret; |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 470 | } |
| 471 | |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 472 | /* Returns 0 if fails, else returns 1. Note that stream may be null for SCOPE_SESS. */ |
| 473 | static inline int sample_clear_stream(const char *name, enum vars_scope scope, struct sample *smp) |
| 474 | { |
| 475 | struct vars *vars; |
| 476 | struct var *var; |
| 477 | unsigned int size = 0; |
| 478 | |
Willy Tarreau | f37b140 | 2019-06-04 16:27:36 +0200 | [diff] [blame] | 479 | vars = get_vars(smp->sess, smp->strm, scope); |
| 480 | if (!vars || vars->scope != scope) |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 481 | return 0; |
| 482 | |
| 483 | /* Look for existing variable name. */ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 484 | HA_RWLOCK_WRLOCK(VARS_LOCK, &vars->rwlock); |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 485 | var = var_get(vars, name); |
| 486 | if (var) { |
| 487 | size = var_clear(var); |
| 488 | var_accounting_diff(vars, smp->sess, smp->strm, -size); |
| 489 | } |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 490 | HA_RWLOCK_WRUNLOCK(VARS_LOCK, &vars->rwlock); |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 491 | return 1; |
| 492 | } |
| 493 | |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 494 | /* Returns 0 if fails, else returns 1. */ |
| 495 | static int smp_conv_store(const struct arg *args, struct sample *smp, void *private) |
| 496 | { |
Christopher Faulet | 0099a8c | 2016-11-09 16:15:32 +0100 | [diff] [blame] | 497 | 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] | 498 | } |
| 499 | |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 500 | /* Returns 0 if fails, else returns 1. */ |
| 501 | static int smp_conv_clear(const struct arg *args, struct sample *smp, void *private) |
| 502 | { |
| 503 | return sample_clear_stream(args[0].data.var.name, args[0].data.var.scope, smp); |
| 504 | } |
| 505 | |
Joseph Herlant | 0767689 | 2018-11-15 09:19:50 -0800 | [diff] [blame] | 506 | /* This functions check an argument entry and fill it with a variable |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 507 | * type. The argumen must be a string. If the variable lookup fails, |
Joseph Herlant | 0767689 | 2018-11-15 09:19:50 -0800 | [diff] [blame] | 508 | * the function returns 0 and fill <err>, otherwise it returns 1. |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 509 | */ |
| 510 | int vars_check_arg(struct arg *arg, char **err) |
| 511 | { |
| 512 | char *name; |
| 513 | enum vars_scope scope; |
| 514 | |
| 515 | /* Check arg type. */ |
| 516 | if (arg->type != ARGT_STR) { |
| 517 | memprintf(err, "unexpected argument type"); |
| 518 | return 0; |
| 519 | } |
| 520 | |
| 521 | /* Register new variable name. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 522 | name = register_name(arg->data.str.area, arg->data.str.data, &scope, |
| 523 | 1, |
| 524 | err); |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 525 | if (!name) |
| 526 | return 0; |
| 527 | |
Tim Duesterhus | a6cc7e8 | 2019-05-13 10:53:29 +0200 | [diff] [blame] | 528 | /* properly destroy the chunk */ |
| 529 | chunk_destroy(&arg->data.str); |
| 530 | |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 531 | /* Use the global variable name pointer. */ |
| 532 | arg->type = ARGT_VAR; |
| 533 | arg->data.var.name = name; |
| 534 | arg->data.var.scope = scope; |
| 535 | return 1; |
| 536 | } |
| 537 | |
Christopher Faulet | 09c9df2 | 2016-10-31 11:05:37 +0100 | [diff] [blame] | 538 | /* 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] | 539 | * Returns zero on failure and non-zero otherwise. The variable not being |
| 540 | * defined is treated as a failure. |
Christopher Faulet | 09c9df2 | 2016-10-31 11:05:37 +0100 | [diff] [blame] | 541 | */ |
Tim Duesterhus | b4fac1e | 2020-05-19 13:49:40 +0200 | [diff] [blame] | 542 | 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] | 543 | { |
| 544 | enum vars_scope scope; |
| 545 | |
| 546 | /* Resolve name and scope. */ |
| 547 | name = register_name(name, len, &scope, 0, NULL); |
| 548 | if (!name) |
Tim Duesterhus | b4fac1e | 2020-05-19 13:49:40 +0200 | [diff] [blame] | 549 | return 0; |
Christopher Faulet | 09c9df2 | 2016-10-31 11:05:37 +0100 | [diff] [blame] | 550 | |
Tim Duesterhus | b4fac1e | 2020-05-19 13:49:40 +0200 | [diff] [blame] | 551 | return sample_store_stream(name, scope, smp); |
Christopher Faulet | 09c9df2 | 2016-10-31 11:05:37 +0100 | [diff] [blame] | 552 | } |
| 553 | |
| 554 | |
Thierry FOURNIER | c365d99 | 2015-06-09 12:27:17 +0200 | [diff] [blame] | 555 | /* This function store a sample in a variable. |
Tim Duesterhus | b4fac1e | 2020-05-19 13:49:40 +0200 | [diff] [blame] | 556 | * Returns zero on failure and non-zero otherwise. |
Thierry FOURNIER | c365d99 | 2015-06-09 12:27:17 +0200 | [diff] [blame] | 557 | */ |
Tim Duesterhus | b4fac1e | 2020-05-19 13:49:40 +0200 | [diff] [blame] | 558 | 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] | 559 | { |
| 560 | enum vars_scope scope; |
| 561 | |
| 562 | /* Resolve name and scope. */ |
Christopher Faulet | 09c9df2 | 2016-10-31 11:05:37 +0100 | [diff] [blame] | 563 | name = register_name(name, len, &scope, 1, NULL); |
Thierry FOURNIER | c365d99 | 2015-06-09 12:27:17 +0200 | [diff] [blame] | 564 | if (!name) |
Tim Duesterhus | b4fac1e | 2020-05-19 13:49:40 +0200 | [diff] [blame] | 565 | return 0; |
Thierry FOURNIER | c365d99 | 2015-06-09 12:27:17 +0200 | [diff] [blame] | 566 | |
Tim Duesterhus | b4fac1e | 2020-05-19 13:49:40 +0200 | [diff] [blame] | 567 | return sample_store_stream(name, scope, smp); |
Thierry FOURNIER | c365d99 | 2015-06-09 12:27:17 +0200 | [diff] [blame] | 568 | } |
| 569 | |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 570 | /* This function unset a variable if it was already defined. |
Tim Duesterhus | b4fac1e | 2020-05-19 13:49:40 +0200 | [diff] [blame] | 571 | * Returns zero on failure and non-zero otherwise. |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 572 | */ |
Tim Duesterhus | b4fac1e | 2020-05-19 13:49:40 +0200 | [diff] [blame] | 573 | 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] | 574 | { |
| 575 | enum vars_scope scope; |
| 576 | |
| 577 | /* Resolve name and scope. */ |
| 578 | name = register_name(name, len, &scope, 0, NULL); |
| 579 | if (!name) |
Tim Duesterhus | b4fac1e | 2020-05-19 13:49:40 +0200 | [diff] [blame] | 580 | return 0; |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 581 | |
Tim Duesterhus | b4fac1e | 2020-05-19 13:49:40 +0200 | [diff] [blame] | 582 | return sample_clear_stream(name, scope, smp); |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 583 | } |
| 584 | |
| 585 | |
Thierry FOURNIER | c365d99 | 2015-06-09 12:27:17 +0200 | [diff] [blame] | 586 | /* this function fills a sample with the |
| 587 | * variable content. Returns 1 if the sample |
| 588 | * is filled, otherwise it returns 0. |
| 589 | */ |
Willy Tarreau | 6204cd9 | 2016-03-10 16:33:04 +0100 | [diff] [blame] | 590 | 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] | 591 | { |
| 592 | struct vars *vars; |
| 593 | struct var *var; |
| 594 | enum vars_scope scope; |
| 595 | |
| 596 | /* Resolve name and scope. */ |
Christopher Faulet | 09c9df2 | 2016-10-31 11:05:37 +0100 | [diff] [blame] | 597 | name = register_name(name, len, &scope, 1, NULL); |
Thierry FOURNIER | c365d99 | 2015-06-09 12:27:17 +0200 | [diff] [blame] | 598 | if (!name) |
| 599 | return 0; |
| 600 | |
| 601 | /* Select "vars" pool according with the scope. */ |
Willy Tarreau | f37b140 | 2019-06-04 16:27:36 +0200 | [diff] [blame] | 602 | vars = get_vars(smp->sess, smp->strm, scope); |
| 603 | if (!vars || vars->scope != scope) |
Thierry FOURNIER | c365d99 | 2015-06-09 12:27:17 +0200 | [diff] [blame] | 604 | return 0; |
| 605 | |
| 606 | /* Get the variable entry. */ |
| 607 | var = var_get(vars, name); |
| 608 | if (!var) |
| 609 | return 0; |
| 610 | |
| 611 | /* Copy sample. */ |
Thierry FOURNIER | 5cc18d4 | 2015-08-19 09:02:36 +0200 | [diff] [blame] | 612 | smp->data = var->data; |
Thierry FOURNIER | c365d99 | 2015-06-09 12:27:17 +0200 | [diff] [blame] | 613 | smp->flags = SMP_F_CONST; |
Thierry FOURNIER | c365d99 | 2015-06-09 12:27:17 +0200 | [diff] [blame] | 614 | return 1; |
| 615 | } |
| 616 | |
Thierry FOURNIER | fd77e05 | 2015-07-07 21:20:42 +0200 | [diff] [blame] | 617 | /* this function fills a sample with the |
Joseph Herlant | 0767689 | 2018-11-15 09:19:50 -0800 | [diff] [blame] | 618 | * content of the variable described by <var_desc>. Returns 1 |
Thierry FOURNIER | fd77e05 | 2015-07-07 21:20:42 +0200 | [diff] [blame] | 619 | * if the sample is filled, otherwise it returns 0. |
| 620 | */ |
Willy Tarreau | 6204cd9 | 2016-03-10 16:33:04 +0100 | [diff] [blame] | 621 | 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] | 622 | { |
| 623 | struct vars *vars; |
| 624 | struct var *var; |
| 625 | |
| 626 | /* Select "vars" pool according with the scope. */ |
Willy Tarreau | f37b140 | 2019-06-04 16:27:36 +0200 | [diff] [blame] | 627 | vars = get_vars(smp->sess, smp->strm, var_desc->scope); |
Thierry FOURNIER | fd77e05 | 2015-07-07 21:20:42 +0200 | [diff] [blame] | 628 | |
Joseph Herlant | 0767689 | 2018-11-15 09:19:50 -0800 | [diff] [blame] | 629 | /* Check if the scope is available a this point of processing. */ |
Willy Tarreau | f37b140 | 2019-06-04 16:27:36 +0200 | [diff] [blame] | 630 | if (!vars || vars->scope != var_desc->scope) |
Thierry FOURNIER | fd77e05 | 2015-07-07 21:20:42 +0200 | [diff] [blame] | 631 | return 0; |
| 632 | |
| 633 | /* Get the variable entry. */ |
| 634 | var = var_get(vars, var_desc->name); |
| 635 | if (!var) |
| 636 | return 0; |
| 637 | |
| 638 | /* Copy sample. */ |
Thierry FOURNIER | 5cc18d4 | 2015-08-19 09:02:36 +0200 | [diff] [blame] | 639 | smp->data = var->data; |
Thierry FOURNIER | fd77e05 | 2015-07-07 21:20:42 +0200 | [diff] [blame] | 640 | smp->flags = SMP_F_CONST; |
Thierry FOURNIER | fd77e05 | 2015-07-07 21:20:42 +0200 | [diff] [blame] | 641 | return 1; |
| 642 | } |
| 643 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 644 | /* Always returns ACT_RET_CONT even if an error occurs. */ |
| 645 | 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] | 646 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 647 | { |
| 648 | struct sample smp; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 649 | int dir; |
| 650 | |
| 651 | switch (rule->from) { |
Willy Tarreau | 620408f | 2016-10-21 16:37:51 +0200 | [diff] [blame] | 652 | case ACT_F_TCP_REQ_SES: dir = SMP_OPT_DIR_REQ; break; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 653 | case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break; |
| 654 | case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break; |
| 655 | case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break; |
| 656 | case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break; |
Gaetan Rivet | 0c39ecc | 2020-02-24 17:34:11 +0100 | [diff] [blame] | 657 | case ACT_F_TCP_CHK: dir = SMP_OPT_DIR_REQ; break; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 658 | default: |
| 659 | send_log(px, LOG_ERR, "Vars: internal error while execute action store."); |
| 660 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 661 | ha_alert("Vars: internal error while execute action store.\n"); |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 662 | return ACT_RET_CONT; |
| 663 | } |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 664 | |
| 665 | /* Process the expression. */ |
| 666 | memset(&smp, 0, sizeof(smp)); |
Willy Tarreau | 108a8fd | 2016-10-21 17:13:24 +0200 | [diff] [blame] | 667 | if (!sample_process(px, sess, s, dir|SMP_OPT_FINAL, |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 668 | rule->arg.vars.expr, &smp)) |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 669 | return ACT_RET_CONT; |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 670 | |
| 671 | /* Store the sample, and ignore errors. */ |
Willy Tarreau | 6204cd9 | 2016-03-10 16:33:04 +0100 | [diff] [blame] | 672 | sample_store_stream(rule->arg.vars.name, rule->arg.vars.scope, &smp); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 673 | return ACT_RET_CONT; |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 674 | } |
| 675 | |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 676 | /* Always returns ACT_RET_CONT even if an error occurs. */ |
| 677 | static enum act_return action_clear(struct act_rule *rule, struct proxy *px, |
| 678 | struct session *sess, struct stream *s, int flags) |
| 679 | { |
| 680 | struct sample smp; |
| 681 | |
| 682 | memset(&smp, 0, sizeof(smp)); |
| 683 | smp_set_owner(&smp, px, sess, s, SMP_OPT_FINAL); |
| 684 | |
| 685 | /* Clear the variable using the sample context, and ignore errors. */ |
| 686 | sample_clear_stream(rule->arg.vars.name, rule->arg.vars.scope, &smp); |
| 687 | return ACT_RET_CONT; |
| 688 | } |
| 689 | |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 690 | /* This two function checks the variable name and replace the |
| 691 | * configuration string name by the global string name. its |
| 692 | * the same string, but the global pointer can be easy to |
| 693 | * compare. |
| 694 | * |
| 695 | * The first function checks a sample-fetch and the second |
| 696 | * checks a converter. |
| 697 | */ |
| 698 | static int smp_check_var(struct arg *args, char **err) |
| 699 | { |
| 700 | return vars_check_arg(&args[0], err); |
| 701 | } |
| 702 | |
| 703 | static int conv_check_var(struct arg *args, struct sample_conv *conv, |
| 704 | const char *file, int line, char **err_msg) |
| 705 | { |
| 706 | return vars_check_arg(&args[0], err_msg); |
| 707 | } |
| 708 | |
| 709 | /* This function is a common parser for using variables. It understands |
| 710 | * the format: |
| 711 | * |
| 712 | * set-var(<variable-name>) <expression> |
Willy Tarreau | 4b7531f | 2019-06-04 16:43:29 +0200 | [diff] [blame] | 713 | * unset-var(<variable-name>) |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 714 | * |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 715 | * It returns ACT_RET_PRS_ERR if fails and <err> is filled with an error |
| 716 | * message. Otherwise, it returns ACT_RET_PRS_OK and the variable <expr> |
| 717 | * is filled with the pointer to the expression to execute. |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 718 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 719 | static enum act_parse_ret parse_store(const char **args, int *arg, struct proxy *px, |
| 720 | struct act_rule *rule, char **err) |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 721 | { |
| 722 | const char *var_name = args[*arg-1]; |
| 723 | int var_len; |
Thierry FOURNIER | 48a9cd1 | 2015-07-28 19:00:28 +0200 | [diff] [blame] | 724 | const char *kw_name; |
Willy Tarreau | e365815 | 2016-11-24 21:23:28 +0100 | [diff] [blame] | 725 | int flags, set_var = 0; |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 726 | |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 727 | if (!strncmp(var_name, "set-var", 7)) { |
| 728 | var_name += 7; |
| 729 | set_var = 1; |
| 730 | } |
| 731 | if (!strncmp(var_name, "unset-var", 9)) { |
| 732 | var_name += 9; |
| 733 | set_var = 0; |
| 734 | } |
| 735 | |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 736 | if (*var_name != '(') { |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 737 | memprintf(err, "invalid variable '%s'. Expects 'set-var(<var-name>)' or 'unset-var(<var-name>)'", |
| 738 | args[*arg-1]); |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 739 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 740 | } |
| 741 | var_name++; /* jump the '(' */ |
| 742 | var_len = strlen(var_name); |
| 743 | var_len--; /* remove the ')' */ |
| 744 | if (var_name[var_len] != ')') { |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 745 | memprintf(err, "invalid variable '%s'. Expects 'set-var(<var-name>)' or 'unset-var(<var-name>)'", |
| 746 | args[*arg-1]); |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 747 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 748 | } |
| 749 | |
Christopher Faulet | 09c9df2 | 2016-10-31 11:05:37 +0100 | [diff] [blame] | 750 | 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] | 751 | if (!rule->arg.vars.name) |
| 752 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 753 | |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 754 | /* There is no fetch method when variable is unset. Just set the right |
| 755 | * action and return. */ |
| 756 | if (!set_var) { |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 757 | rule->action = ACT_CUSTOM; |
| 758 | rule->action_ptr = action_clear; |
| 759 | return ACT_RET_PRS_OK; |
| 760 | } |
| 761 | |
Thierry FOURNIER | 48a9cd1 | 2015-07-28 19:00:28 +0200 | [diff] [blame] | 762 | kw_name = args[*arg-1]; |
| 763 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 764 | rule->arg.vars.expr = sample_parse_expr((char **)args, arg, px->conf.args.file, |
Willy Tarreau | e3b57bf | 2020-02-14 16:50:14 +0100 | [diff] [blame] | 765 | px->conf.args.line, err, &px->conf.args, NULL); |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 766 | if (!rule->arg.vars.expr) |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 767 | return ACT_RET_PRS_ERR; |
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 | switch (rule->from) { |
Willy Tarreau | 620408f | 2016-10-21 16:37:51 +0200 | [diff] [blame] | 770 | case ACT_F_TCP_REQ_SES: flags = SMP_VAL_FE_SES_ACC; break; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 771 | case ACT_F_TCP_REQ_CNT: flags = SMP_VAL_FE_REQ_CNT; break; |
| 772 | case ACT_F_TCP_RES_CNT: flags = SMP_VAL_BE_RES_CNT; break; |
| 773 | case ACT_F_HTTP_REQ: flags = SMP_VAL_FE_HRQ_HDR; break; |
| 774 | case ACT_F_HTTP_RES: flags = SMP_VAL_BE_HRS_HDR; break; |
Gaetan Rivet | 707b52f | 2020-02-21 18:14:59 +0100 | [diff] [blame] | 775 | case ACT_F_TCP_CHK: flags = SMP_VAL_BE_CHK_RUL; break; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 776 | default: |
| 777 | memprintf(err, |
| 778 | "internal error, unexpected rule->from=%d, please report this bug!", |
| 779 | rule->from); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 780 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 781 | } |
| 782 | if (!(rule->arg.vars.expr->fetch->val & flags)) { |
| 783 | memprintf(err, |
| 784 | "fetch method '%s' extracts information from '%s', none of which is available here", |
| 785 | kw_name, sample_src_names(rule->arg.vars.expr->fetch->use)); |
| 786 | free(rule->arg.vars.expr); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 787 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 788 | } |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 789 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 790 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 791 | rule->action_ptr = action_store; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 792 | return ACT_RET_PRS_OK; |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 793 | } |
| 794 | |
| 795 | static int vars_max_size(char **args, int section_type, struct proxy *curpx, |
| 796 | struct proxy *defpx, const char *file, int line, |
| 797 | char **err, unsigned int *limit) |
| 798 | { |
| 799 | char *error; |
| 800 | |
| 801 | *limit = strtol(args[1], &error, 10); |
| 802 | if (*error != 0) { |
| 803 | memprintf(err, "%s: '%s' is an invalid size", args[0], args[1]); |
| 804 | return -1; |
| 805 | } |
| 806 | return 0; |
| 807 | } |
| 808 | |
| 809 | static int vars_max_size_global(char **args, int section_type, struct proxy *curpx, |
| 810 | struct proxy *defpx, const char *file, int line, |
| 811 | char **err) |
| 812 | { |
| 813 | return vars_max_size(args, section_type, curpx, defpx, file, line, err, &var_global_limit); |
| 814 | } |
| 815 | |
Christopher Faulet | ff2613e | 2016-11-09 11:36:17 +0100 | [diff] [blame] | 816 | static int vars_max_size_proc(char **args, int section_type, struct proxy *curpx, |
| 817 | struct proxy *defpx, const char *file, int line, |
| 818 | char **err) |
| 819 | { |
| 820 | return vars_max_size(args, section_type, curpx, defpx, file, line, err, &var_proc_limit); |
| 821 | } |
| 822 | |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 823 | static int vars_max_size_sess(char **args, int section_type, struct proxy *curpx, |
| 824 | struct proxy *defpx, const char *file, int line, |
| 825 | char **err) |
| 826 | { |
| 827 | return vars_max_size(args, section_type, curpx, defpx, file, line, err, &var_sess_limit); |
| 828 | } |
| 829 | |
| 830 | static int vars_max_size_txn(char **args, int section_type, struct proxy *curpx, |
| 831 | struct proxy *defpx, const char *file, int line, |
| 832 | char **err) |
| 833 | { |
| 834 | return vars_max_size(args, section_type, curpx, defpx, file, line, err, &var_txn_limit); |
| 835 | } |
| 836 | |
| 837 | static int vars_max_size_reqres(char **args, int section_type, struct proxy *curpx, |
| 838 | struct proxy *defpx, const char *file, int line, |
| 839 | char **err) |
| 840 | { |
| 841 | return vars_max_size(args, section_type, curpx, defpx, file, line, err, &var_reqres_limit); |
| 842 | } |
| 843 | |
Gaetan Rivet | 13a5043 | 2020-02-21 18:13:44 +0100 | [diff] [blame] | 844 | static int vars_max_size_check(char **args, int section_type, struct proxy *curpx, |
| 845 | struct proxy *defpx, const char *file, int line, |
| 846 | char **err) |
| 847 | { |
| 848 | return vars_max_size(args, section_type, curpx, defpx, file, line, err, &var_check_limit); |
| 849 | } |
| 850 | |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 851 | static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, { |
| 852 | |
Christopher Faulet | ff2613e | 2016-11-09 11:36:17 +0100 | [diff] [blame] | 853 | { "var", smp_fetch_var, ARG1(1,STR), smp_check_var, SMP_T_STR, SMP_USE_L4CLI }, |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 854 | { /* END */ }, |
| 855 | }}; |
| 856 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 857 | INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords); |
| 858 | |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 859 | static struct sample_conv_kw_list sample_conv_kws = {ILH, { |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 860 | { "set-var", smp_conv_store, ARG1(1,STR), conv_check_var, SMP_T_ANY, SMP_T_ANY }, |
| 861 | { "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] | 862 | { /* END */ }, |
| 863 | }}; |
| 864 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 865 | INITCALL1(STG_REGISTER, sample_register_convs, &sample_conv_kws); |
| 866 | |
Willy Tarreau | 620408f | 2016-10-21 16:37:51 +0200 | [diff] [blame] | 867 | static struct action_kw_list tcp_req_sess_kws = { { }, { |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 868 | { "set-var", parse_store, 1 }, |
| 869 | { "unset-var", parse_store, 1 }, |
Willy Tarreau | 620408f | 2016-10-21 16:37:51 +0200 | [diff] [blame] | 870 | { /* END */ } |
| 871 | }}; |
| 872 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 873 | INITCALL1(STG_REGISTER, tcp_req_sess_keywords_register, &tcp_req_sess_kws); |
| 874 | |
Willy Tarreau | 620408f | 2016-10-21 16:37:51 +0200 | [diff] [blame] | 875 | static struct action_kw_list tcp_req_cont_kws = { { }, { |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 876 | { "set-var", parse_store, 1 }, |
| 877 | { "unset-var", parse_store, 1 }, |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 878 | { /* END */ } |
| 879 | }}; |
| 880 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 881 | INITCALL1(STG_REGISTER, tcp_req_cont_keywords_register, &tcp_req_cont_kws); |
| 882 | |
Thierry FOURNIER | 36481b8 | 2015-08-19 09:01:53 +0200 | [diff] [blame] | 883 | static struct action_kw_list tcp_res_kws = { { }, { |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 884 | { "set-var", parse_store, 1 }, |
| 885 | { "unset-var", parse_store, 1 }, |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 886 | { /* END */ } |
| 887 | }}; |
| 888 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 889 | INITCALL1(STG_REGISTER, tcp_res_cont_keywords_register, &tcp_res_kws); |
| 890 | |
Gaetan Rivet | 707b52f | 2020-02-21 18:14:59 +0100 | [diff] [blame] | 891 | static struct action_kw_list tcp_check_kws = {ILH, { |
| 892 | { "set-var", parse_store, 1 }, |
| 893 | { "unset-var", parse_store, 1 }, |
| 894 | { /* END */ } |
| 895 | }}; |
| 896 | |
| 897 | INITCALL1(STG_REGISTER, tcp_check_keywords_register, &tcp_check_kws); |
| 898 | |
Thierry FOURNIER | 36481b8 | 2015-08-19 09:01:53 +0200 | [diff] [blame] | 899 | static struct action_kw_list http_req_kws = { { }, { |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 900 | { "set-var", parse_store, 1 }, |
| 901 | { "unset-var", parse_store, 1 }, |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 902 | { /* END */ } |
| 903 | }}; |
| 904 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 905 | INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_kws); |
| 906 | |
Thierry FOURNIER | 36481b8 | 2015-08-19 09:01:53 +0200 | [diff] [blame] | 907 | static struct action_kw_list http_res_kws = { { }, { |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 908 | { "set-var", parse_store, 1 }, |
| 909 | { "unset-var", parse_store, 1 }, |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 910 | { /* END */ } |
| 911 | }}; |
| 912 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 913 | INITCALL1(STG_REGISTER, http_res_keywords_register, &http_res_kws); |
| 914 | |
Christopher Faulet | 6d0c3df | 2020-01-22 09:26:35 +0100 | [diff] [blame] | 915 | static struct action_kw_list http_after_res_kws = { { }, { |
| 916 | { "set-var", parse_store, 1 }, |
| 917 | { "unset-var", parse_store, 1 }, |
| 918 | { /* END */ } |
| 919 | }}; |
| 920 | |
| 921 | INITCALL1(STG_REGISTER, http_after_res_keywords_register, &http_after_res_kws); |
| 922 | |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 923 | static struct cfg_kw_list cfg_kws = {{ },{ |
| 924 | { CFG_GLOBAL, "tune.vars.global-max-size", vars_max_size_global }, |
Christopher Faulet | ff2613e | 2016-11-09 11:36:17 +0100 | [diff] [blame] | 925 | { CFG_GLOBAL, "tune.vars.proc-max-size", vars_max_size_proc }, |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 926 | { CFG_GLOBAL, "tune.vars.sess-max-size", vars_max_size_sess }, |
| 927 | { CFG_GLOBAL, "tune.vars.txn-max-size", vars_max_size_txn }, |
| 928 | { CFG_GLOBAL, "tune.vars.reqres-max-size", vars_max_size_reqres }, |
Gaetan Rivet | 13a5043 | 2020-02-21 18:13:44 +0100 | [diff] [blame] | 929 | { CFG_GLOBAL, "tune.vars.check-max-size", vars_max_size_check }, |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 930 | { /* END */ } |
| 931 | }}; |
| 932 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 933 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |