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