Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1 | /* |
| 2 | * HTTP actions |
| 3 | * |
| 4 | * Copyright 2000-2018 Willy Tarreau <w@1wt.eu> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
| 13 | #include <sys/types.h> |
| 14 | |
| 15 | #include <ctype.h> |
| 16 | #include <string.h> |
| 17 | #include <time.h> |
| 18 | |
Christopher Faulet | 81e2017 | 2019-12-12 16:40:30 +0100 | [diff] [blame] | 19 | #include <common/cfgparse.h> |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 20 | #include <common/chunk.h> |
| 21 | #include <common/compat.h> |
| 22 | #include <common/config.h> |
| 23 | #include <common/debug.h> |
| 24 | #include <common/http.h> |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 25 | #include <common/initcall.h> |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 26 | #include <common/memory.h> |
| 27 | #include <common/standard.h> |
| 28 | #include <common/version.h> |
| 29 | |
| 30 | #include <types/capture.h> |
| 31 | #include <types/global.h> |
| 32 | |
| 33 | #include <proto/acl.h> |
| 34 | #include <proto/arg.h> |
Christopher Faulet | 81e2017 | 2019-12-12 16:40:30 +0100 | [diff] [blame] | 35 | #include <proto/action.h> |
Willy Tarreau | 61c112a | 2018-10-02 16:43:32 +0200 | [diff] [blame] | 36 | #include <proto/http_rules.h> |
Willy Tarreau | 3381022 | 2019-06-12 17:44:02 +0200 | [diff] [blame] | 37 | #include <proto/http_htx.h> |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 38 | #include <proto/log.h> |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 39 | #include <proto/http_ana.h> |
Willy Tarreau | 0f9cd7b | 2019-01-31 19:02:43 +0100 | [diff] [blame] | 40 | #include <proto/stream_interface.h> |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 41 | |
| 42 | |
| 43 | /* This function executes one of the set-{method,path,query,uri} actions. It |
| 44 | * builds a string in the trash from the specified format string. It finds |
| 45 | * the action to be performed in <http.action>, previously filled by function |
| 46 | * parse_set_req_line(). The replacement action is excuted by the function |
| 47 | * http_action_set_req_line(). It always returns ACT_RET_CONT. If an error |
| 48 | * occurs the action is canceled, but the rule processing continue. |
| 49 | */ |
| 50 | static enum act_return http_action_set_req_line(struct act_rule *rule, struct proxy *px, |
| 51 | struct session *sess, struct stream *s, int flags) |
| 52 | { |
| 53 | struct buffer *replace; |
Christopher Faulet | 1340376 | 2019-12-13 09:01:57 +0100 | [diff] [blame] | 54 | enum act_return ret = ACT_RET_CONT; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 55 | |
| 56 | replace = alloc_trash_chunk(); |
| 57 | if (!replace) |
| 58 | goto leave; |
| 59 | |
| 60 | /* If we have to create a query string, prepare a '?'. */ |
| 61 | if (rule->arg.http.action == 2) |
| 62 | replace->area[replace->data++] = '?'; |
| 63 | replace->data += build_logline(s, replace->area + replace->data, |
| 64 | replace->size - replace->data, |
| 65 | &rule->arg.http.logfmt); |
| 66 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 67 | http_req_replace_stline(rule->arg.http.action, replace->area, |
| 68 | replace->data, px, s); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 69 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 70 | leave: |
| 71 | free_trash_chunk(replace); |
| 72 | return ret; |
| 73 | } |
| 74 | |
| 75 | /* parse an http-request action among : |
| 76 | * set-method |
| 77 | * set-path |
| 78 | * set-query |
| 79 | * set-uri |
| 80 | * |
| 81 | * All of them accept a single argument of type string representing a log-format. |
| 82 | * The resulting rule makes use of arg->act.p[0..1] to store the log-format list |
| 83 | * head, and p[2] to store the action as an int (0=method, 1=path, 2=query, 3=uri). |
| 84 | * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error. |
| 85 | */ |
| 86 | static enum act_parse_ret parse_set_req_line(const char **args, int *orig_arg, struct proxy *px, |
| 87 | struct act_rule *rule, char **err) |
| 88 | { |
| 89 | int cur_arg = *orig_arg; |
| 90 | |
| 91 | rule->action = ACT_CUSTOM; |
| 92 | |
| 93 | switch (args[0][4]) { |
| 94 | case 'm' : |
| 95 | rule->arg.http.action = 0; |
| 96 | rule->action_ptr = http_action_set_req_line; |
| 97 | break; |
| 98 | case 'p' : |
| 99 | rule->arg.http.action = 1; |
| 100 | rule->action_ptr = http_action_set_req_line; |
| 101 | break; |
| 102 | case 'q' : |
| 103 | rule->arg.http.action = 2; |
| 104 | rule->action_ptr = http_action_set_req_line; |
| 105 | break; |
| 106 | case 'u' : |
| 107 | rule->arg.http.action = 3; |
| 108 | rule->action_ptr = http_action_set_req_line; |
| 109 | break; |
| 110 | default: |
| 111 | memprintf(err, "internal error: unhandled action '%s'", args[0]); |
| 112 | return ACT_RET_PRS_ERR; |
| 113 | } |
| 114 | |
| 115 | if (!*args[cur_arg] || |
| 116 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 117 | memprintf(err, "expects exactly 1 argument <format>"); |
| 118 | return ACT_RET_PRS_ERR; |
| 119 | } |
| 120 | |
| 121 | LIST_INIT(&rule->arg.http.logfmt); |
| 122 | px->conf.args.ctx = ARGC_HRQ; |
| 123 | if (!parse_logformat_string(args[cur_arg], px, &rule->arg.http.logfmt, LOG_OPT_HTTP, |
| 124 | (px->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, err)) { |
| 125 | return ACT_RET_PRS_ERR; |
| 126 | } |
| 127 | |
| 128 | (*orig_arg)++; |
| 129 | return ACT_RET_PRS_OK; |
| 130 | } |
| 131 | |
Willy Tarreau | 3381022 | 2019-06-12 17:44:02 +0200 | [diff] [blame] | 132 | /* This function executes a replace-uri action. It finds its arguments in |
| 133 | * <rule>.arg.act.p[]. It builds a string in the trash from the format string |
| 134 | * previously filled by function parse_replace_uri() and will execute the regex |
| 135 | * in p[1] to replace the URI. It uses the format string present in act.p[2..3]. |
Willy Tarreau | 262c3f1 | 2019-12-17 06:52:51 +0100 | [diff] [blame] | 136 | * The component to act on (path/uri) is taken from act.p[0] which contains 1 |
| 137 | * for the path or 3 for the URI (values used by http_req_replace_stline()). |
Willy Tarreau | 3381022 | 2019-06-12 17:44:02 +0200 | [diff] [blame] | 138 | * It always returns ACT_RET_CONT. If an error occurs, the action is canceled, |
| 139 | * but the rule processing continues. |
| 140 | */ |
| 141 | static enum act_return http_action_replace_uri(struct act_rule *rule, struct proxy *px, |
| 142 | struct session *sess, struct stream *s, int flags) |
| 143 | { |
Christopher Faulet | 1340376 | 2019-12-13 09:01:57 +0100 | [diff] [blame] | 144 | enum act_return ret = ACT_RET_CONT; |
Willy Tarreau | 3381022 | 2019-06-12 17:44:02 +0200 | [diff] [blame] | 145 | struct buffer *replace, *output; |
| 146 | struct ist uri; |
| 147 | int len; |
| 148 | |
| 149 | replace = alloc_trash_chunk(); |
| 150 | output = alloc_trash_chunk(); |
| 151 | if (!replace || !output) |
| 152 | goto leave; |
Christopher Faulet | 12c28b6 | 2019-07-15 16:30:24 +0200 | [diff] [blame] | 153 | uri = htx_sl_req_uri(http_get_stline(htxbuf(&s->req.buf))); |
Willy Tarreau | 262c3f1 | 2019-12-17 06:52:51 +0100 | [diff] [blame] | 154 | |
| 155 | if (rule->arg.act.p[0] == (void *)1) |
| 156 | uri = http_get_path(uri); // replace path |
| 157 | |
Willy Tarreau | 3381022 | 2019-06-12 17:44:02 +0200 | [diff] [blame] | 158 | if (!regex_exec_match2(rule->arg.act.p[1], uri.ptr, uri.len, MAX_MATCH, pmatch, 0)) |
| 159 | goto leave; |
| 160 | |
| 161 | replace->data = build_logline(s, replace->area, replace->size, (struct list *)&rule->arg.act.p[2]); |
| 162 | |
| 163 | /* note: uri.ptr doesn't need to be zero-terminated because it will |
| 164 | * only be used to pick pmatch references. |
| 165 | */ |
| 166 | len = exp_replace(output->area, output->size, uri.ptr, replace->area, pmatch); |
| 167 | if (len == -1) |
| 168 | goto leave; |
| 169 | |
Willy Tarreau | 262c3f1 | 2019-12-17 06:52:51 +0100 | [diff] [blame] | 170 | http_req_replace_stline((long)rule->arg.act.p[0], output->area, len, px, s); |
Willy Tarreau | 3381022 | 2019-06-12 17:44:02 +0200 | [diff] [blame] | 171 | |
Willy Tarreau | 3381022 | 2019-06-12 17:44:02 +0200 | [diff] [blame] | 172 | leave: |
| 173 | free_trash_chunk(output); |
| 174 | free_trash_chunk(replace); |
| 175 | return ret; |
| 176 | } |
| 177 | |
Willy Tarreau | 262c3f1 | 2019-12-17 06:52:51 +0100 | [diff] [blame] | 178 | /* parse a "replace-uri" or "replace-path" http-request action. |
Willy Tarreau | 3381022 | 2019-06-12 17:44:02 +0200 | [diff] [blame] | 179 | * This action takes 2 arguments (a regex and a replacement format string). |
Willy Tarreau | 262c3f1 | 2019-12-17 06:52:51 +0100 | [diff] [blame] | 180 | * The resulting rule makes use of arg->act.p[0] to store the action (1/3 for now), |
Willy Tarreau | 3381022 | 2019-06-12 17:44:02 +0200 | [diff] [blame] | 181 | * p[1] to store the compiled regex, and arg->act.p[2..3] to store the log-format |
| 182 | * list head. It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error. |
| 183 | */ |
| 184 | static enum act_parse_ret parse_replace_uri(const char **args, int *orig_arg, struct proxy *px, |
| 185 | struct act_rule *rule, char **err) |
| 186 | { |
| 187 | int cur_arg = *orig_arg; |
| 188 | char *error = NULL; |
| 189 | |
| 190 | rule->action = ACT_CUSTOM; |
Willy Tarreau | 262c3f1 | 2019-12-17 06:52:51 +0100 | [diff] [blame] | 191 | if (strcmp(args[cur_arg-1], "replace-path") == 0) |
| 192 | rule->arg.act.p[0] = (void *)1; // replace-path |
| 193 | else |
| 194 | rule->arg.act.p[0] = (void *)3; // replace-uri |
| 195 | |
Willy Tarreau | 3381022 | 2019-06-12 17:44:02 +0200 | [diff] [blame] | 196 | rule->action_ptr = http_action_replace_uri; |
| 197 | |
| 198 | if (!*args[cur_arg] || !*args[cur_arg+1] || |
| 199 | (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) { |
| 200 | memprintf(err, "expects exactly 2 arguments <match-regex> and <replace-format>"); |
| 201 | return ACT_RET_PRS_ERR; |
| 202 | } |
| 203 | |
| 204 | if (!(rule->arg.act.p[1] = regex_comp(args[cur_arg], 1, 1, &error))) { |
| 205 | memprintf(err, "failed to parse the regex : %s", error); |
| 206 | free(error); |
| 207 | return ACT_RET_PRS_ERR; |
| 208 | } |
| 209 | |
| 210 | LIST_INIT((struct list *)&rule->arg.act.p[2]); |
| 211 | px->conf.args.ctx = ARGC_HRQ; |
| 212 | if (!parse_logformat_string(args[cur_arg + 1], px, (struct list *)&rule->arg.act.p[2], LOG_OPT_HTTP, |
| 213 | (px->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, err)) { |
| 214 | return ACT_RET_PRS_ERR; |
| 215 | } |
| 216 | |
| 217 | (*orig_arg) += 2; |
| 218 | return ACT_RET_PRS_OK; |
| 219 | } |
| 220 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 221 | /* This function is just a compliant action wrapper for "set-status". */ |
| 222 | static enum act_return action_http_set_status(struct act_rule *rule, struct proxy *px, |
| 223 | struct session *sess, struct stream *s, int flags) |
| 224 | { |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 225 | http_res_set_status(rule->arg.status.code, rule->arg.status.reason, s); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 226 | return ACT_RET_CONT; |
| 227 | } |
| 228 | |
| 229 | /* parse set-status action: |
| 230 | * This action accepts a single argument of type int representing |
| 231 | * an http status code. It returns ACT_RET_PRS_OK on success, |
| 232 | * ACT_RET_PRS_ERR on error. |
| 233 | */ |
| 234 | static enum act_parse_ret parse_http_set_status(const char **args, int *orig_arg, struct proxy *px, |
| 235 | struct act_rule *rule, char **err) |
| 236 | { |
| 237 | char *error; |
| 238 | |
| 239 | rule->action = ACT_CUSTOM; |
| 240 | rule->action_ptr = action_http_set_status; |
| 241 | |
| 242 | /* Check if an argument is available */ |
| 243 | if (!*args[*orig_arg]) { |
| 244 | memprintf(err, "expects 1 argument: <status>; or 3 arguments: <status> reason <fmt>"); |
| 245 | return ACT_RET_PRS_ERR; |
| 246 | } |
| 247 | |
| 248 | /* convert status code as integer */ |
| 249 | rule->arg.status.code = strtol(args[*orig_arg], &error, 10); |
| 250 | if (*error != '\0' || rule->arg.status.code < 100 || rule->arg.status.code > 999) { |
| 251 | memprintf(err, "expects an integer status code between 100 and 999"); |
| 252 | return ACT_RET_PRS_ERR; |
| 253 | } |
| 254 | |
| 255 | (*orig_arg)++; |
| 256 | |
| 257 | /* set custom reason string */ |
| 258 | rule->arg.status.reason = NULL; // If null, we use the default reason for the status code. |
| 259 | if (*args[*orig_arg] && strcmp(args[*orig_arg], "reason") == 0 && |
| 260 | (*args[*orig_arg + 1] && strcmp(args[*orig_arg + 1], "if") != 0 && strcmp(args[*orig_arg + 1], "unless") != 0)) { |
| 261 | (*orig_arg)++; |
| 262 | rule->arg.status.reason = strdup(args[*orig_arg]); |
| 263 | (*orig_arg)++; |
| 264 | } |
| 265 | |
| 266 | return ACT_RET_PRS_OK; |
| 267 | } |
| 268 | |
| 269 | /* This function executes the "reject" HTTP action. It clears the request and |
| 270 | * response buffer without sending any response. It can be useful as an HTTP |
| 271 | * alternative to the silent-drop action to defend against DoS attacks, and may |
| 272 | * also be used with HTTP/2 to close a connection instead of just a stream. |
| 273 | * The txn status is unchanged, indicating no response was sent. The termination |
Christopher Faulet | 8f1aa77 | 2019-07-04 11:27:15 +0200 | [diff] [blame] | 274 | * flags will indicate "PR". It always returns ACT_RET_DONE. |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 275 | */ |
| 276 | static enum act_return http_action_reject(struct act_rule *rule, struct proxy *px, |
| 277 | struct session *sess, struct stream *s, int flags) |
| 278 | { |
Willy Tarreau | 0f9cd7b | 2019-01-31 19:02:43 +0100 | [diff] [blame] | 279 | si_must_kill_conn(chn_prod(&s->req)); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 280 | channel_abort(&s->req); |
| 281 | channel_abort(&s->res); |
| 282 | s->req.analysers = 0; |
| 283 | s->res.analysers = 0; |
| 284 | |
Olivier Houchard | a798bf5 | 2019-03-08 18:52:00 +0100 | [diff] [blame] | 285 | _HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1); |
| 286 | _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 287 | if (sess->listener && sess->listener->counters) |
Olivier Houchard | a798bf5 | 2019-03-08 18:52:00 +0100 | [diff] [blame] | 288 | _HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 289 | |
| 290 | if (!(s->flags & SF_ERR_MASK)) |
| 291 | s->flags |= SF_ERR_PRXCOND; |
| 292 | if (!(s->flags & SF_FINST_MASK)) |
| 293 | s->flags |= SF_FINST_R; |
| 294 | |
Christopher Faulet | 8f1aa77 | 2019-07-04 11:27:15 +0200 | [diff] [blame] | 295 | return ACT_RET_DONE; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | /* parse the "reject" action: |
| 299 | * This action takes no argument and returns ACT_RET_PRS_OK on success, |
| 300 | * ACT_RET_PRS_ERR on error. |
| 301 | */ |
| 302 | static enum act_parse_ret parse_http_action_reject(const char **args, int *orig_arg, struct proxy *px, |
| 303 | struct act_rule *rule, char **err) |
| 304 | { |
| 305 | rule->action = ACT_CUSTOM; |
| 306 | rule->action_ptr = http_action_reject; |
| 307 | return ACT_RET_PRS_OK; |
| 308 | } |
| 309 | |
Olivier Houchard | 602bf7d | 2019-05-10 13:59:15 +0200 | [diff] [blame] | 310 | /* This function executes the "disable-l7-retry" HTTP action. |
| 311 | * It disables L7 retries (all retry except for a connection failure). This |
| 312 | * can be useful for example to avoid retrying on POST requests. |
| 313 | * It just removes the L7 retry flag on the stream_interface, and always |
| 314 | * return ACT_RET_CONT; |
| 315 | */ |
| 316 | static enum act_return http_req_disable_l7_retry(struct act_rule *rule, struct proxy *px, |
| 317 | struct session *sess, struct stream *s, int flags) |
| 318 | { |
| 319 | struct stream_interface *si = &s->si[1]; |
| 320 | |
| 321 | /* In theory, the SI_FL_L7_RETRY flags isn't set at this point, but |
| 322 | * let's be future-proof and remove it anyway. |
| 323 | */ |
| 324 | si->flags &= ~SI_FL_L7_RETRY; |
| 325 | si->flags |= SI_FL_D_L7_RETRY; |
| 326 | return ACT_RET_CONT; |
| 327 | } |
| 328 | |
| 329 | /* parse the "disable-l7-retry" action: |
| 330 | * This action takes no argument and returns ACT_RET_PRS_OK on success, |
| 331 | * ACT_RET_PRS_ERR on error. |
| 332 | */ |
| 333 | static enum act_parse_ret parse_http_req_disable_l7_retry(const char **args, |
| 334 | int *orig_args, struct proxy *px, |
| 335 | struct act_rule *rule, char **err) |
| 336 | { |
| 337 | rule->action = ACT_CUSTOM; |
| 338 | rule->action_ptr = http_req_disable_l7_retry; |
| 339 | return ACT_RET_PRS_OK; |
| 340 | } |
| 341 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 342 | /* This function executes the "capture" action. It executes a fetch expression, |
| 343 | * turns the result into a string and puts it in a capture slot. It always |
| 344 | * returns 1. If an error occurs the action is cancelled, but the rule |
| 345 | * processing continues. |
| 346 | */ |
| 347 | static enum act_return http_action_req_capture(struct act_rule *rule, struct proxy *px, |
| 348 | struct session *sess, struct stream *s, int flags) |
| 349 | { |
| 350 | struct sample *key; |
| 351 | struct cap_hdr *h = rule->arg.cap.hdr; |
| 352 | char **cap = s->req_cap; |
| 353 | int len; |
| 354 | |
| 355 | key = sample_fetch_as_type(s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.cap.expr, SMP_T_STR); |
| 356 | if (!key) |
| 357 | return ACT_RET_CONT; |
| 358 | |
| 359 | if (cap[h->index] == NULL) |
| 360 | cap[h->index] = pool_alloc(h->pool); |
| 361 | |
| 362 | if (cap[h->index] == NULL) /* no more capture memory */ |
| 363 | return ACT_RET_CONT; |
| 364 | |
| 365 | len = key->data.u.str.data; |
| 366 | if (len > h->len) |
| 367 | len = h->len; |
| 368 | |
| 369 | memcpy(cap[h->index], key->data.u.str.area, len); |
| 370 | cap[h->index][len] = 0; |
| 371 | return ACT_RET_CONT; |
| 372 | } |
| 373 | |
| 374 | /* This function executes the "capture" action and store the result in a |
| 375 | * capture slot if exists. It executes a fetch expression, turns the result |
| 376 | * into a string and puts it in a capture slot. It always returns 1. If an |
| 377 | * error occurs the action is cancelled, but the rule processing continues. |
| 378 | */ |
| 379 | static enum act_return http_action_req_capture_by_id(struct act_rule *rule, struct proxy *px, |
| 380 | struct session *sess, struct stream *s, int flags) |
| 381 | { |
| 382 | struct sample *key; |
| 383 | struct cap_hdr *h; |
| 384 | char **cap = s->req_cap; |
| 385 | struct proxy *fe = strm_fe(s); |
| 386 | int len; |
| 387 | int i; |
| 388 | |
| 389 | /* Look for the original configuration. */ |
| 390 | for (h = fe->req_cap, i = fe->nb_req_cap - 1; |
| 391 | h != NULL && i != rule->arg.capid.idx ; |
| 392 | i--, h = h->next); |
| 393 | if (!h) |
| 394 | return ACT_RET_CONT; |
| 395 | |
| 396 | key = sample_fetch_as_type(s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.capid.expr, SMP_T_STR); |
| 397 | if (!key) |
| 398 | return ACT_RET_CONT; |
| 399 | |
| 400 | if (cap[h->index] == NULL) |
| 401 | cap[h->index] = pool_alloc(h->pool); |
| 402 | |
| 403 | if (cap[h->index] == NULL) /* no more capture memory */ |
| 404 | return ACT_RET_CONT; |
| 405 | |
| 406 | len = key->data.u.str.data; |
| 407 | if (len > h->len) |
| 408 | len = h->len; |
| 409 | |
| 410 | memcpy(cap[h->index], key->data.u.str.area, len); |
| 411 | cap[h->index][len] = 0; |
| 412 | return ACT_RET_CONT; |
| 413 | } |
| 414 | |
| 415 | /* Check an "http-request capture" action. |
| 416 | * |
| 417 | * The function returns 1 in success case, otherwise, it returns 0 and err is |
| 418 | * filled. |
| 419 | */ |
| 420 | static int check_http_req_capture(struct act_rule *rule, struct proxy *px, char **err) |
| 421 | { |
| 422 | if (rule->action_ptr != http_action_req_capture_by_id) |
| 423 | return 1; |
| 424 | |
| 425 | if (rule->arg.capid.idx >= px->nb_req_cap) { |
| 426 | memprintf(err, "unable to find capture id '%d' referenced by http-request capture rule", |
| 427 | rule->arg.capid.idx); |
| 428 | return 0; |
| 429 | } |
| 430 | |
| 431 | return 1; |
| 432 | } |
| 433 | |
| 434 | /* parse an "http-request capture" action. It takes a single argument which is |
| 435 | * a sample fetch expression. It stores the expression into arg->act.p[0] and |
| 436 | * the allocated hdr_cap struct or the preallocated "id" into arg->act.p[1]. |
| 437 | * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error. |
| 438 | */ |
| 439 | static enum act_parse_ret parse_http_req_capture(const char **args, int *orig_arg, struct proxy *px, |
| 440 | struct act_rule *rule, char **err) |
| 441 | { |
| 442 | struct sample_expr *expr; |
| 443 | struct cap_hdr *hdr; |
| 444 | int cur_arg; |
| 445 | int len = 0; |
| 446 | |
| 447 | for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++) |
| 448 | if (strcmp(args[cur_arg], "if") == 0 || |
| 449 | strcmp(args[cur_arg], "unless") == 0) |
| 450 | break; |
| 451 | |
| 452 | if (cur_arg < *orig_arg + 3) { |
| 453 | memprintf(err, "expects <expression> [ 'len' <length> | id <idx> ]"); |
| 454 | return ACT_RET_PRS_ERR; |
| 455 | } |
| 456 | |
| 457 | cur_arg = *orig_arg; |
| 458 | expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args); |
| 459 | if (!expr) |
| 460 | return ACT_RET_PRS_ERR; |
| 461 | |
| 462 | if (!(expr->fetch->val & SMP_VAL_FE_HRQ_HDR)) { |
| 463 | memprintf(err, |
| 464 | "fetch method '%s' extracts information from '%s', none of which is available here", |
| 465 | args[cur_arg-1], sample_src_names(expr->fetch->use)); |
| 466 | free(expr); |
| 467 | return ACT_RET_PRS_ERR; |
| 468 | } |
| 469 | |
| 470 | if (!args[cur_arg] || !*args[cur_arg]) { |
| 471 | memprintf(err, "expects 'len or 'id'"); |
| 472 | free(expr); |
| 473 | return ACT_RET_PRS_ERR; |
| 474 | } |
| 475 | |
| 476 | if (strcmp(args[cur_arg], "len") == 0) { |
| 477 | cur_arg++; |
| 478 | |
| 479 | if (!(px->cap & PR_CAP_FE)) { |
| 480 | memprintf(err, "proxy '%s' has no frontend capability", px->id); |
| 481 | return ACT_RET_PRS_ERR; |
| 482 | } |
| 483 | |
| 484 | px->conf.args.ctx = ARGC_CAP; |
| 485 | |
| 486 | if (!args[cur_arg]) { |
| 487 | memprintf(err, "missing length value"); |
| 488 | free(expr); |
| 489 | return ACT_RET_PRS_ERR; |
| 490 | } |
| 491 | /* we copy the table name for now, it will be resolved later */ |
| 492 | len = atoi(args[cur_arg]); |
| 493 | if (len <= 0) { |
| 494 | memprintf(err, "length must be > 0"); |
| 495 | free(expr); |
| 496 | return ACT_RET_PRS_ERR; |
| 497 | } |
| 498 | cur_arg++; |
| 499 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 500 | hdr = calloc(1, sizeof(*hdr)); |
| 501 | hdr->next = px->req_cap; |
| 502 | hdr->name = NULL; /* not a header capture */ |
| 503 | hdr->namelen = 0; |
| 504 | hdr->len = len; |
| 505 | hdr->pool = create_pool("caphdr", hdr->len + 1, MEM_F_SHARED); |
| 506 | hdr->index = px->nb_req_cap++; |
| 507 | |
| 508 | px->req_cap = hdr; |
| 509 | px->to_log |= LW_REQHDR; |
| 510 | |
| 511 | rule->action = ACT_CUSTOM; |
| 512 | rule->action_ptr = http_action_req_capture; |
| 513 | rule->arg.cap.expr = expr; |
| 514 | rule->arg.cap.hdr = hdr; |
| 515 | } |
| 516 | |
| 517 | else if (strcmp(args[cur_arg], "id") == 0) { |
| 518 | int id; |
| 519 | char *error; |
| 520 | |
| 521 | cur_arg++; |
| 522 | |
| 523 | if (!args[cur_arg]) { |
| 524 | memprintf(err, "missing id value"); |
| 525 | free(expr); |
| 526 | return ACT_RET_PRS_ERR; |
| 527 | } |
| 528 | |
| 529 | id = strtol(args[cur_arg], &error, 10); |
| 530 | if (*error != '\0') { |
| 531 | memprintf(err, "cannot parse id '%s'", args[cur_arg]); |
| 532 | free(expr); |
| 533 | return ACT_RET_PRS_ERR; |
| 534 | } |
| 535 | cur_arg++; |
| 536 | |
| 537 | px->conf.args.ctx = ARGC_CAP; |
| 538 | |
| 539 | rule->action = ACT_CUSTOM; |
| 540 | rule->action_ptr = http_action_req_capture_by_id; |
| 541 | rule->check_ptr = check_http_req_capture; |
| 542 | rule->arg.capid.expr = expr; |
| 543 | rule->arg.capid.idx = id; |
| 544 | } |
| 545 | |
| 546 | else { |
| 547 | memprintf(err, "expects 'len' or 'id', found '%s'", args[cur_arg]); |
| 548 | free(expr); |
| 549 | return ACT_RET_PRS_ERR; |
| 550 | } |
| 551 | |
| 552 | *orig_arg = cur_arg; |
| 553 | return ACT_RET_PRS_OK; |
| 554 | } |
| 555 | |
| 556 | /* This function executes the "capture" action and store the result in a |
| 557 | * capture slot if exists. It executes a fetch expression, turns the result |
| 558 | * into a string and puts it in a capture slot. It always returns 1. If an |
| 559 | * error occurs the action is cancelled, but the rule processing continues. |
| 560 | */ |
| 561 | static enum act_return http_action_res_capture_by_id(struct act_rule *rule, struct proxy *px, |
| 562 | struct session *sess, struct stream *s, int flags) |
| 563 | { |
| 564 | struct sample *key; |
| 565 | struct cap_hdr *h; |
| 566 | char **cap = s->res_cap; |
| 567 | struct proxy *fe = strm_fe(s); |
| 568 | int len; |
| 569 | int i; |
| 570 | |
| 571 | /* Look for the original configuration. */ |
| 572 | for (h = fe->rsp_cap, i = fe->nb_rsp_cap - 1; |
| 573 | h != NULL && i != rule->arg.capid.idx ; |
| 574 | i--, h = h->next); |
| 575 | if (!h) |
| 576 | return ACT_RET_CONT; |
| 577 | |
| 578 | key = sample_fetch_as_type(s->be, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL, rule->arg.capid.expr, SMP_T_STR); |
| 579 | if (!key) |
| 580 | return ACT_RET_CONT; |
| 581 | |
| 582 | if (cap[h->index] == NULL) |
| 583 | cap[h->index] = pool_alloc(h->pool); |
| 584 | |
| 585 | if (cap[h->index] == NULL) /* no more capture memory */ |
| 586 | return ACT_RET_CONT; |
| 587 | |
| 588 | len = key->data.u.str.data; |
| 589 | if (len > h->len) |
| 590 | len = h->len; |
| 591 | |
| 592 | memcpy(cap[h->index], key->data.u.str.area, len); |
| 593 | cap[h->index][len] = 0; |
| 594 | return ACT_RET_CONT; |
| 595 | } |
| 596 | |
| 597 | /* Check an "http-response capture" action. |
| 598 | * |
| 599 | * The function returns 1 in success case, otherwise, it returns 0 and err is |
| 600 | * filled. |
| 601 | */ |
| 602 | static int check_http_res_capture(struct act_rule *rule, struct proxy *px, char **err) |
| 603 | { |
| 604 | if (rule->action_ptr != http_action_res_capture_by_id) |
| 605 | return 1; |
| 606 | |
| 607 | if (rule->arg.capid.idx >= px->nb_rsp_cap) { |
| 608 | memprintf(err, "unable to find capture id '%d' referenced by http-response capture rule", |
| 609 | rule->arg.capid.idx); |
| 610 | return 0; |
| 611 | } |
| 612 | |
| 613 | return 1; |
| 614 | } |
| 615 | |
| 616 | /* parse an "http-response capture" action. It takes a single argument which is |
| 617 | * a sample fetch expression. It stores the expression into arg->act.p[0] and |
| 618 | * the allocated hdr_cap struct od the preallocated id into arg->act.p[1]. |
| 619 | * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error. |
| 620 | */ |
| 621 | static enum act_parse_ret parse_http_res_capture(const char **args, int *orig_arg, struct proxy *px, |
| 622 | struct act_rule *rule, char **err) |
| 623 | { |
| 624 | struct sample_expr *expr; |
| 625 | int cur_arg; |
| 626 | int id; |
| 627 | char *error; |
| 628 | |
| 629 | for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++) |
| 630 | if (strcmp(args[cur_arg], "if") == 0 || |
| 631 | strcmp(args[cur_arg], "unless") == 0) |
| 632 | break; |
| 633 | |
| 634 | if (cur_arg < *orig_arg + 3) { |
| 635 | memprintf(err, "expects <expression> id <idx>"); |
| 636 | return ACT_RET_PRS_ERR; |
| 637 | } |
| 638 | |
| 639 | cur_arg = *orig_arg; |
| 640 | expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args); |
| 641 | if (!expr) |
| 642 | return ACT_RET_PRS_ERR; |
| 643 | |
| 644 | if (!(expr->fetch->val & SMP_VAL_FE_HRS_HDR)) { |
| 645 | memprintf(err, |
| 646 | "fetch method '%s' extracts information from '%s', none of which is available here", |
| 647 | args[cur_arg-1], sample_src_names(expr->fetch->use)); |
| 648 | free(expr); |
| 649 | return ACT_RET_PRS_ERR; |
| 650 | } |
| 651 | |
| 652 | if (!args[cur_arg] || !*args[cur_arg]) { |
| 653 | memprintf(err, "expects 'id'"); |
| 654 | free(expr); |
| 655 | return ACT_RET_PRS_ERR; |
| 656 | } |
| 657 | |
| 658 | if (strcmp(args[cur_arg], "id") != 0) { |
| 659 | memprintf(err, "expects 'id', found '%s'", args[cur_arg]); |
| 660 | free(expr); |
| 661 | return ACT_RET_PRS_ERR; |
| 662 | } |
| 663 | |
| 664 | cur_arg++; |
| 665 | |
| 666 | if (!args[cur_arg]) { |
| 667 | memprintf(err, "missing id value"); |
| 668 | free(expr); |
| 669 | return ACT_RET_PRS_ERR; |
| 670 | } |
| 671 | |
| 672 | id = strtol(args[cur_arg], &error, 10); |
| 673 | if (*error != '\0') { |
| 674 | memprintf(err, "cannot parse id '%s'", args[cur_arg]); |
| 675 | free(expr); |
| 676 | return ACT_RET_PRS_ERR; |
| 677 | } |
| 678 | cur_arg++; |
| 679 | |
| 680 | px->conf.args.ctx = ARGC_CAP; |
| 681 | |
| 682 | rule->action = ACT_CUSTOM; |
| 683 | rule->action_ptr = http_action_res_capture_by_id; |
| 684 | rule->check_ptr = check_http_res_capture; |
| 685 | rule->arg.capid.expr = expr; |
| 686 | rule->arg.capid.idx = id; |
| 687 | |
| 688 | *orig_arg = cur_arg; |
| 689 | return ACT_RET_PRS_OK; |
| 690 | } |
| 691 | |
Christopher Faulet | 81e2017 | 2019-12-12 16:40:30 +0100 | [diff] [blame] | 692 | /* Parse a "allow" action for a request or a response rule. It takes no argument. It |
| 693 | * returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error. |
| 694 | */ |
| 695 | static enum act_parse_ret parse_http_allow(const char **args, int *orig_arg, struct proxy *px, |
| 696 | struct act_rule *rule, char **err) |
| 697 | { |
| 698 | rule->action = ACT_ACTION_ALLOW; |
| 699 | return ACT_RET_PRS_OK; |
| 700 | } |
| 701 | |
| 702 | /* Parse "deny" or "tarpit" actions for a request rule. It may take 2 optional arguments |
| 703 | * to define the status code. It returns ACT_RET_PRS_OK on success, |
| 704 | * ACT_RET_PRS_ERR on error. |
| 705 | */ |
| 706 | static enum act_parse_ret parse_http_req_deny(const char **args, int *orig_arg, struct proxy *px, |
| 707 | struct act_rule *rule, char **err) |
| 708 | { |
| 709 | int code, hc, cur_arg; |
| 710 | |
| 711 | cur_arg = *orig_arg; |
| 712 | if (!strcmp(args[cur_arg-1], "tarpit")) { |
| 713 | rule->action = ACT_HTTP_REQ_TARPIT; |
| 714 | rule->deny_status = HTTP_ERR_500; |
| 715 | } |
| 716 | else { |
| 717 | rule->action = ACT_ACTION_DENY; |
| 718 | rule->deny_status = HTTP_ERR_403; |
| 719 | } |
| 720 | |
| 721 | if (strcmp(args[cur_arg], "deny_status") == 0) { |
| 722 | cur_arg++; |
| 723 | if (!*args[cur_arg]) { |
| 724 | memprintf(err, "missing status code.\n"); |
| 725 | return ACT_RET_PRS_ERR; |
| 726 | } |
| 727 | |
| 728 | code = atol(args[cur_arg]); |
| 729 | cur_arg++; |
| 730 | for (hc = 0; hc < HTTP_ERR_SIZE; hc++) { |
| 731 | if (http_err_codes[hc] == code) { |
| 732 | rule->deny_status = hc; |
| 733 | break; |
| 734 | } |
| 735 | } |
| 736 | if (hc >= HTTP_ERR_SIZE) |
| 737 | memprintf(err, "status code %d not handled, using default code %d", |
| 738 | code, http_err_codes[rule->deny_status]); |
| 739 | } |
| 740 | |
| 741 | *orig_arg = cur_arg; |
| 742 | return ACT_RET_PRS_OK; |
| 743 | } |
| 744 | |
| 745 | /* Parse a "deny" action for a response rule. It takes no argument. It returns |
| 746 | * ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error. |
| 747 | */ |
| 748 | static enum act_parse_ret parse_http_res_deny(const char **args, int *orig_arg, struct proxy *px, |
| 749 | struct act_rule *rule, char **err) |
| 750 | { |
| 751 | rule->action = ACT_ACTION_DENY; |
| 752 | return ACT_RET_PRS_OK; |
| 753 | } |
| 754 | |
| 755 | /* Parse a "auth" action. It may take 2 optional arguments to define a "realm" |
| 756 | * parameter. It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error. |
| 757 | */ |
| 758 | static enum act_parse_ret parse_http_auth(const char **args, int *orig_arg, struct proxy *px, |
| 759 | struct act_rule *rule, char **err) |
| 760 | { |
| 761 | int cur_arg; |
| 762 | |
| 763 | rule->action = ACT_HTTP_REQ_AUTH; |
| 764 | |
| 765 | cur_arg = *orig_arg; |
| 766 | if (!strcmp(args[cur_arg], "realm")) { |
| 767 | cur_arg++; |
| 768 | if (!*args[cur_arg]) { |
| 769 | memprintf(err, "missing realm value.\n"); |
| 770 | return ACT_RET_PRS_ERR; |
| 771 | } |
| 772 | rule->arg.auth.realm = strdup(args[cur_arg]); |
| 773 | cur_arg++; |
| 774 | } |
| 775 | |
| 776 | *orig_arg = cur_arg; |
| 777 | return ACT_RET_PRS_OK; |
| 778 | } |
| 779 | |
| 780 | /* Parse a "set-nice" action. It takes the nice value as argument. It returns |
| 781 | * ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error. |
| 782 | */ |
| 783 | static enum act_parse_ret parse_http_set_nice(const char **args, int *orig_arg, struct proxy *px, |
| 784 | struct act_rule *rule, char **err) |
| 785 | { |
| 786 | int cur_arg; |
| 787 | |
| 788 | rule->action = ACT_HTTP_SET_NICE; |
| 789 | |
| 790 | cur_arg = *orig_arg; |
| 791 | if (!*args[cur_arg]) { |
| 792 | memprintf(err, "expects exactly 1 argument (integer value)"); |
| 793 | return ACT_RET_PRS_ERR; |
| 794 | } |
| 795 | rule->arg.nice = atoi(args[cur_arg]); |
| 796 | if (rule->arg.nice < -1024) |
| 797 | rule->arg.nice = -1024; |
| 798 | else if (rule->arg.nice > 1024) |
| 799 | rule->arg.nice = 1024; |
| 800 | |
| 801 | *orig_arg = cur_arg + 1; |
| 802 | return ACT_RET_PRS_OK; |
| 803 | } |
| 804 | |
| 805 | /* Parse a "set-tos" action. It takes the TOS value as argument. It returns |
| 806 | * ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error. |
| 807 | */ |
| 808 | static enum act_parse_ret parse_http_set_tos(const char **args, int *orig_arg, struct proxy *px, |
| 809 | struct act_rule *rule, char **err) |
| 810 | { |
| 811 | #ifdef IP_TOS |
| 812 | char *endp; |
| 813 | int cur_arg; |
| 814 | |
| 815 | rule->action = ACT_HTTP_SET_TOS; |
| 816 | |
| 817 | cur_arg = *orig_arg; |
| 818 | if (!*args[cur_arg]) { |
| 819 | memprintf(err, "expects exactly 1 argument (integer/hex value)"); |
| 820 | return ACT_RET_PRS_ERR; |
| 821 | } |
| 822 | rule->arg.tos = strtol(args[cur_arg], &endp, 0); |
| 823 | if (endp && *endp != '\0') { |
| 824 | memprintf(err, "invalid character starting at '%s' (integer/hex value expected)", endp); |
| 825 | return ACT_RET_PRS_ERR; |
| 826 | } |
| 827 | |
| 828 | *orig_arg = cur_arg + 1; |
| 829 | return ACT_RET_PRS_OK; |
| 830 | #else |
| 831 | memprintf(err, "not supported on this platform (IP_TOS undefined)"); |
| 832 | return ACT_RET_PRS_ERR; |
| 833 | #endif |
| 834 | } |
| 835 | |
| 836 | /* Parse a "set-mark" action. It takes the MARK value as argument. It returns |
| 837 | * ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error. |
| 838 | */ |
| 839 | static enum act_parse_ret parse_http_set_mark(const char **args, int *orig_arg, struct proxy *px, |
| 840 | struct act_rule *rule, char **err) |
| 841 | { |
| 842 | #ifdef SO_MARK |
| 843 | char *endp; |
| 844 | int cur_arg; |
| 845 | |
| 846 | rule->action = ACT_HTTP_SET_MARK; |
| 847 | |
| 848 | cur_arg = *orig_arg; |
| 849 | if (!*args[cur_arg]) { |
| 850 | memprintf(err, "expects exactly 1 argument (integer/hex value)"); |
| 851 | return ACT_RET_PRS_ERR; |
| 852 | } |
| 853 | rule->arg.mark = strtoul(args[cur_arg], &endp, 0); |
| 854 | if (endp && *endp != '\0') { |
| 855 | memprintf(err, "invalid character starting at '%s' (integer/hex value expected)", endp); |
| 856 | return ACT_RET_PRS_ERR; |
| 857 | } |
| 858 | |
| 859 | *orig_arg = cur_arg + 1; |
| 860 | global.last_checks |= LSTCHK_NETADM; |
| 861 | return ACT_RET_PRS_OK; |
| 862 | #else |
| 863 | memprintf(err, "not supported on this platform (SO_MARK undefined)"); |
| 864 | return ACT_RET_PRS_ERR; |
| 865 | #endif |
| 866 | } |
| 867 | |
| 868 | /* Parse a "set-log-level" action. It takes the level value as argument. It |
| 869 | * returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error. |
| 870 | */ |
| 871 | static enum act_parse_ret parse_http_set_log_level(const char **args, int *orig_arg, struct proxy *px, |
| 872 | struct act_rule *rule, char **err) |
| 873 | { |
| 874 | int cur_arg; |
| 875 | |
| 876 | rule->action = ACT_HTTP_SET_LOGL; |
| 877 | |
| 878 | cur_arg = *orig_arg; |
| 879 | if (!*args[cur_arg]) { |
| 880 | bad_log_level: |
| 881 | memprintf(err, "expects exactly 1 argument (log level name or 'silent')"); |
| 882 | return ACT_RET_PRS_ERR; |
| 883 | } |
| 884 | if (strcmp(args[cur_arg], "silent") == 0) |
| 885 | rule->arg.loglevel = -1; |
| 886 | else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0) |
| 887 | goto bad_log_level; |
| 888 | |
| 889 | *orig_arg = cur_arg + 1; |
| 890 | return ACT_RET_PRS_OK; |
| 891 | } |
| 892 | |
| 893 | /* Parse a "set-header", "add-header" or "early-hint" actions. It takes an |
| 894 | * header name and a log-format string as arguments. It returns ACT_RET_PRS_OK |
| 895 | * on success, ACT_RET_PRS_ERR on error. |
| 896 | * |
| 897 | * Note: same function is used for the request and the response. However |
| 898 | * "early-hint" rules are only supported for request rules. |
| 899 | */ |
| 900 | static enum act_parse_ret parse_http_set_header(const char **args, int *orig_arg, struct proxy *px, |
| 901 | struct act_rule *rule, char **err) |
| 902 | { |
| 903 | char **hdr_name; |
| 904 | int *hdr_name_len; |
| 905 | struct list *fmt; |
| 906 | int cap, cur_arg; |
| 907 | |
| 908 | rule->action = (*args[*orig_arg-1] == 'a' ? ACT_HTTP_ADD_HDR : |
| 909 | *args[*orig_arg-1] == 's' ? ACT_HTTP_SET_HDR : ACT_HTTP_EARLY_HINT); |
| 910 | |
| 911 | cur_arg = *orig_arg; |
| 912 | if (!*args[cur_arg] || !*args[cur_arg+1]) { |
| 913 | memprintf(err, "expects exactly 2 arguments"); |
| 914 | return ACT_RET_PRS_ERR; |
| 915 | } |
| 916 | |
| 917 | hdr_name = (*args[cur_arg-1] == 'e' ? &rule->arg.early_hint.name : &rule->arg.hdr_add.name); |
| 918 | hdr_name_len = (*args[cur_arg-1] == 'e' ? &rule->arg.early_hint.name_len : &rule->arg.hdr_add.name_len); |
| 919 | fmt = (*args[cur_arg-1] == 'e' ? &rule->arg.early_hint.fmt : &rule->arg.hdr_add.fmt); |
| 920 | |
| 921 | *hdr_name = strdup(args[cur_arg]); |
| 922 | *hdr_name_len = strlen(*hdr_name); |
| 923 | LIST_INIT(fmt); |
| 924 | |
| 925 | if (rule->from == ACT_F_HTTP_REQ) { |
| 926 | px->conf.args.ctx = ARGC_HRQ; |
| 927 | cap = (px->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR; |
| 928 | } |
| 929 | else{ |
| 930 | px->conf.args.ctx = ARGC_HRS; |
| 931 | cap = (px->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR; |
| 932 | } |
| 933 | |
| 934 | cur_arg++; |
| 935 | if (!parse_logformat_string(args[cur_arg], px, fmt, LOG_OPT_HTTP, cap, err)) |
| 936 | return ACT_RET_PRS_ERR; |
| 937 | |
| 938 | free(px->conf.lfs_file); |
| 939 | px->conf.lfs_file = strdup(px->conf.args.file); |
| 940 | px->conf.lfs_line = px->conf.args.line; |
| 941 | |
| 942 | *orig_arg = cur_arg + 1; |
| 943 | return ACT_RET_PRS_OK; |
| 944 | } |
| 945 | |
| 946 | /* Parse a "replace-header" or "replace-value" actions. It takes an header name, |
| 947 | * a regex and replacement string as arguments. It returns ACT_RET_PRS_OK on |
| 948 | * success, ACT_RET_PRS_ERR on error. |
| 949 | */ |
| 950 | static enum act_parse_ret parse_http_replace_header(const char **args, int *orig_arg, struct proxy *px, |
| 951 | struct act_rule *rule, char **err) |
| 952 | { |
| 953 | int cap, cur_arg; |
| 954 | |
| 955 | rule->action = args[*orig_arg-1][8] == 'h' ? ACT_HTTP_REPLACE_HDR : ACT_HTTP_REPLACE_VAL; |
| 956 | |
| 957 | cur_arg = *orig_arg; |
| 958 | if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2]) { |
| 959 | memprintf(err, "expects exactly 3 arguments"); |
| 960 | return ACT_RET_PRS_ERR; |
| 961 | } |
| 962 | |
| 963 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 964 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 965 | LIST_INIT(&rule->arg.hdr_add.fmt); |
| 966 | |
| 967 | cur_arg++; |
| 968 | if (!(rule->arg.hdr_add.re = regex_comp(args[cur_arg], 1, 1, err))) |
| 969 | return ACT_RET_PRS_ERR; |
| 970 | |
| 971 | if (rule->from == ACT_F_HTTP_REQ) { |
| 972 | px->conf.args.ctx = ARGC_HRQ; |
| 973 | cap = (px->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR; |
| 974 | } |
| 975 | else{ |
| 976 | px->conf.args.ctx = ARGC_HRS; |
| 977 | cap = (px->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR; |
| 978 | } |
| 979 | |
| 980 | cur_arg++; |
| 981 | if (!parse_logformat_string(args[cur_arg], px, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP, cap, err)) |
| 982 | return ACT_RET_PRS_ERR; |
| 983 | |
| 984 | free(px->conf.lfs_file); |
| 985 | px->conf.lfs_file = strdup(px->conf.args.file); |
| 986 | px->conf.lfs_line = px->conf.args.line; |
| 987 | |
| 988 | *orig_arg = cur_arg + 1; |
| 989 | return ACT_RET_PRS_OK; |
| 990 | } |
| 991 | |
| 992 | /* Parse a "del-header" action. It takes an header name as argument. It returns |
| 993 | * ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error. |
| 994 | */ |
| 995 | static enum act_parse_ret parse_http_del_header(const char **args, int *orig_arg, struct proxy *px, |
| 996 | struct act_rule *rule, char **err) |
| 997 | { |
| 998 | int cur_arg; |
| 999 | |
| 1000 | rule->action = ACT_HTTP_DEL_HDR; |
| 1001 | |
| 1002 | cur_arg = *orig_arg; |
| 1003 | if (!*args[cur_arg]) { |
| 1004 | memprintf(err, "expects exactly 1 arguments"); |
| 1005 | return ACT_RET_PRS_ERR; |
| 1006 | } |
| 1007 | |
| 1008 | rule->arg.hdr_add.name = strdup(args[cur_arg]); |
| 1009 | rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name); |
| 1010 | |
| 1011 | px->conf.args.ctx = (rule->from == ACT_F_HTTP_REQ ? ARGC_HRQ : ARGC_HRS); |
| 1012 | |
| 1013 | *orig_arg = cur_arg + 1; |
| 1014 | return ACT_RET_PRS_OK; |
| 1015 | } |
| 1016 | |
| 1017 | /* Parse a "redirect" action. It returns ACT_RET_PRS_OK on success, |
| 1018 | * ACT_RET_PRS_ERR on error. |
| 1019 | */ |
| 1020 | static enum act_parse_ret parse_http_redirect(const char **args, int *orig_arg, struct proxy *px, |
| 1021 | struct act_rule *rule, char **err) |
| 1022 | { |
| 1023 | struct redirect_rule *redir; |
| 1024 | int dir, cur_arg; |
| 1025 | |
| 1026 | rule->action = ACT_HTTP_REDIR; |
| 1027 | |
| 1028 | cur_arg = *orig_arg; |
| 1029 | |
| 1030 | dir = (rule->from == ACT_F_HTTP_REQ ? 0 : 1); |
| 1031 | if ((redir = http_parse_redirect_rule(px->conf.args.file, px->conf.args.line, px, &args[cur_arg], err, 1, dir)) == NULL) |
| 1032 | return ACT_RET_PRS_ERR; |
| 1033 | |
| 1034 | rule->arg.redir = redir; |
| 1035 | rule->cond = redir->cond; |
| 1036 | redir->cond = NULL; |
| 1037 | |
| 1038 | /* skip all arguments */ |
| 1039 | while (*args[cur_arg]) |
| 1040 | cur_arg++; |
| 1041 | |
| 1042 | *orig_arg = cur_arg; |
| 1043 | return ACT_RET_PRS_OK; |
| 1044 | } |
| 1045 | |
| 1046 | /* Parse a "add-acl", "del-acl", "set-map" or "del-map" actions. It takes one or |
| 1047 | * two log-format string as argument depending on the action. It returns |
| 1048 | * ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error. |
| 1049 | */ |
| 1050 | static enum act_parse_ret parse_http_set_map(const char **args, int *orig_arg, struct proxy *px, |
| 1051 | struct act_rule *rule, char **err) |
| 1052 | { |
| 1053 | int cap, cur_arg; |
| 1054 | |
| 1055 | rule->action = (args[*orig_arg-1][0] == 'a' ? ACT_HTTP_ADD_ACL : |
| 1056 | (args[*orig_arg-1][0] == 's' ? ACT_HTTP_SET_MAP : |
| 1057 | (args[*orig_arg-1][4] == 'a' ? ACT_HTTP_DEL_ACL : ACT_HTTP_DEL_MAP))); |
| 1058 | |
| 1059 | cur_arg = *orig_arg; |
| 1060 | if (rule->action == ACT_HTTP_SET_MAP && (!*args[cur_arg] || !*args[cur_arg+1])) { |
| 1061 | memprintf(err, "expects exactly 2 arguments"); |
| 1062 | return ACT_RET_PRS_ERR; |
| 1063 | } |
| 1064 | else if (!*args[cur_arg]) { |
| 1065 | memprintf(err, "expects exactly 1 arguments"); |
| 1066 | return ACT_RET_PRS_ERR; |
| 1067 | } |
| 1068 | |
| 1069 | /* |
| 1070 | * '+ 8' for 'set-map(' (same for del-map) |
| 1071 | * '- 9' for 'set-map(' + trailing ')' (same for del-map) |
| 1072 | */ |
| 1073 | rule->arg.map.ref = my_strndup(args[cur_arg-1] + 8, strlen(args[cur_arg-1]) - 9); |
| 1074 | |
| 1075 | if (rule->from == ACT_F_HTTP_REQ) { |
| 1076 | px->conf.args.ctx = ARGC_HRQ; |
| 1077 | cap = (px->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR; |
| 1078 | } |
| 1079 | else{ |
| 1080 | px->conf.args.ctx = ARGC_HRS; |
| 1081 | cap = (px->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR; |
| 1082 | } |
| 1083 | |
| 1084 | /* key pattern */ |
| 1085 | LIST_INIT(&rule->arg.map.key); |
| 1086 | if (!parse_logformat_string(args[cur_arg], px, &rule->arg.map.key, LOG_OPT_HTTP, cap, err)) |
| 1087 | return ACT_RET_PRS_ERR; |
| 1088 | |
| 1089 | if (rule->action == ACT_HTTP_SET_MAP) { |
| 1090 | /* value pattern for set-map only */ |
| 1091 | cur_arg++; |
| 1092 | LIST_INIT(&rule->arg.map.value); |
| 1093 | if (!parse_logformat_string(args[cur_arg], px, &rule->arg.map.value, LOG_OPT_HTTP, cap, err)) |
| 1094 | return ACT_RET_PRS_ERR; |
| 1095 | } |
| 1096 | |
| 1097 | free(px->conf.lfs_file); |
| 1098 | px->conf.lfs_file = strdup(px->conf.args.file); |
| 1099 | px->conf.lfs_line = px->conf.args.line; |
| 1100 | |
| 1101 | *orig_arg = cur_arg + 1; |
| 1102 | return ACT_RET_PRS_OK; |
| 1103 | } |
| 1104 | |
| 1105 | |
| 1106 | /* Parse a "track-sc*" actions. It returns ACT_RET_PRS_OK on success, |
| 1107 | * ACT_RET_PRS_ERR on error. |
| 1108 | */ |
| 1109 | static enum act_parse_ret parse_http_track_sc(const char **args, int *orig_arg, struct proxy *px, |
| 1110 | struct act_rule *rule, char **err) |
| 1111 | { |
| 1112 | struct sample_expr *expr; |
| 1113 | unsigned int where; |
| 1114 | unsigned int tsc_num; |
| 1115 | const char *tsc_num_str; |
| 1116 | int cur_arg; |
| 1117 | |
| 1118 | tsc_num_str = &args[*orig_arg-1][8]; |
| 1119 | if (cfg_parse_track_sc_num(&tsc_num, tsc_num_str, tsc_num_str + strlen(tsc_num_str), err) == -1) |
| 1120 | return ACT_RET_PRS_ERR; |
| 1121 | |
| 1122 | cur_arg = *orig_arg; |
| 1123 | expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, |
| 1124 | err, &px->conf.args); |
| 1125 | if (!expr) |
| 1126 | return ACT_RET_PRS_ERR; |
| 1127 | |
| 1128 | where = 0; |
| 1129 | if (px->cap & PR_CAP_FE) |
| 1130 | where |= (rule->from == ACT_F_HTTP_REQ ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_FE_HRS_HDR); |
| 1131 | if (px->cap & PR_CAP_BE) |
| 1132 | where |= (rule->from == ACT_F_HTTP_REQ ? SMP_VAL_BE_HRQ_HDR : SMP_VAL_BE_HRS_HDR); |
| 1133 | |
| 1134 | if (!(expr->fetch->val & where)) { |
| 1135 | memprintf(err, "fetch method '%s' extracts information from '%s', none of which is available here", |
| 1136 | args[cur_arg-1], sample_src_names(expr->fetch->use)); |
| 1137 | return ACT_RET_PRS_ERR; |
| 1138 | } |
| 1139 | |
| 1140 | if (strcmp(args[cur_arg], "table") == 0) { |
| 1141 | cur_arg++; |
| 1142 | if (!*args[cur_arg]) { |
| 1143 | memprintf(err, "missing table name"); |
| 1144 | return ACT_RET_PRS_ERR; |
| 1145 | } |
| 1146 | |
| 1147 | /* we copy the table name for now, it will be resolved later */ |
| 1148 | rule->arg.trk_ctr.table.n = strdup(args[cur_arg]); |
| 1149 | cur_arg++; |
| 1150 | } |
| 1151 | |
| 1152 | rule->arg.trk_ctr.expr = expr; |
| 1153 | rule->action = ACT_ACTION_TRK_SC0 + tsc_num; |
| 1154 | rule->check_ptr = check_trk_action; |
| 1155 | |
| 1156 | *orig_arg = cur_arg; |
| 1157 | return ACT_RET_PRS_OK; |
| 1158 | } |
| 1159 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1160 | /************************************************************************/ |
| 1161 | /* All supported http-request action keywords must be declared here. */ |
| 1162 | /************************************************************************/ |
| 1163 | |
| 1164 | static struct action_kw_list http_req_actions = { |
| 1165 | .kw = { |
Christopher Faulet | 81e2017 | 2019-12-12 16:40:30 +0100 | [diff] [blame] | 1166 | { "add-acl", parse_http_set_map, 1 }, |
| 1167 | { "add-header", parse_http_set_header, 0 }, |
| 1168 | { "allow", parse_http_allow, 0 }, |
| 1169 | { "auth", parse_http_auth, 0 }, |
| 1170 | { "capture", parse_http_req_capture, 0 }, |
| 1171 | { "del-acl", parse_http_set_map, 1 }, |
| 1172 | { "del-header", parse_http_del_header, 0 }, |
| 1173 | { "del-map", parse_http_set_map, 1 }, |
| 1174 | { "deny", parse_http_req_deny, 0 }, |
| 1175 | { "disable-l7-retry", parse_http_req_disable_l7_retry, 0 }, |
| 1176 | { "early-hint", parse_http_set_header, 0 }, |
| 1177 | { "redirect", parse_http_redirect, 0 }, |
| 1178 | { "reject", parse_http_action_reject, 0 }, |
| 1179 | { "replace-header", parse_http_replace_header, 0 }, |
| 1180 | { "replace-path", parse_replace_uri, 0 }, |
| 1181 | { "replace-uri", parse_replace_uri, 0 }, |
| 1182 | { "replace-value", parse_http_replace_header, 0 }, |
| 1183 | { "set-header", parse_http_set_header, 0 }, |
| 1184 | { "set-log-level", parse_http_set_log_level, 0 }, |
| 1185 | { "set-map", parse_http_set_map, 1 }, |
| 1186 | { "set-method", parse_set_req_line, 0 }, |
| 1187 | { "set-mark", parse_http_set_mark, 0 }, |
| 1188 | { "set-nice", parse_http_set_nice, 0 }, |
| 1189 | { "set-path", parse_set_req_line, 0 }, |
| 1190 | { "set-query", parse_set_req_line, 0 }, |
| 1191 | { "set-tos", parse_http_set_tos, 0 }, |
| 1192 | { "set-uri", parse_set_req_line, 0 }, |
| 1193 | { "tarpit", parse_http_req_deny, 0 }, |
| 1194 | { "track-sc", parse_http_track_sc, 1 }, |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1195 | { NULL, NULL } |
| 1196 | } |
| 1197 | }; |
| 1198 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 1199 | INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_actions); |
| 1200 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1201 | static struct action_kw_list http_res_actions = { |
| 1202 | .kw = { |
Christopher Faulet | 81e2017 | 2019-12-12 16:40:30 +0100 | [diff] [blame] | 1203 | { "add-acl", parse_http_set_map, 1 }, |
| 1204 | { "add-header", parse_http_set_header, 0 }, |
| 1205 | { "allow", parse_http_allow, 0 }, |
| 1206 | { "capture", parse_http_res_capture, 0 }, |
| 1207 | { "del-acl", parse_http_set_map, 1 }, |
| 1208 | { "del-header", parse_http_del_header, 0 }, |
| 1209 | { "del-map", parse_http_set_map, 1 }, |
| 1210 | { "deny", parse_http_res_deny, 0 }, |
| 1211 | { "redirect", parse_http_redirect, 0 }, |
| 1212 | { "replace-header", parse_http_replace_header, 0 }, |
| 1213 | { "replace-value", parse_http_replace_header, 0 }, |
| 1214 | { "set-header", parse_http_set_header, 0 }, |
| 1215 | { "set-log-level", parse_http_set_log_level, 0 }, |
| 1216 | { "set-map", parse_http_set_map, 1 }, |
| 1217 | { "set-mark", parse_http_set_mark, 0 }, |
| 1218 | { "set-nice", parse_http_set_nice, 0 }, |
| 1219 | { "set-status", parse_http_set_status, 0 }, |
| 1220 | { "set-tos", parse_http_set_tos, 0 }, |
| 1221 | { "track-sc", parse_http_track_sc, 1 }, |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1222 | { NULL, NULL } |
| 1223 | } |
| 1224 | }; |
| 1225 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 1226 | INITCALL1(STG_REGISTER, http_res_keywords_register, &http_res_actions); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1227 | |
| 1228 | /* |
| 1229 | * Local variables: |
| 1230 | * c-indent-level: 8 |
| 1231 | * c-basic-offset: 8 |
| 1232 | * End: |
| 1233 | */ |