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 | |
| 19 | #include <common/chunk.h> |
| 20 | #include <common/compat.h> |
| 21 | #include <common/config.h> |
| 22 | #include <common/debug.h> |
| 23 | #include <common/http.h> |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 24 | #include <common/initcall.h> |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 25 | #include <common/memory.h> |
| 26 | #include <common/standard.h> |
| 27 | #include <common/version.h> |
| 28 | |
| 29 | #include <types/capture.h> |
| 30 | #include <types/global.h> |
| 31 | |
| 32 | #include <proto/acl.h> |
| 33 | #include <proto/arg.h> |
Willy Tarreau | 61c112a | 2018-10-02 16:43:32 +0200 | [diff] [blame] | 34 | #include <proto/http_rules.h> |
Willy Tarreau | 3381022 | 2019-06-12 17:44:02 +0200 | [diff] [blame] | 35 | #include <proto/http_htx.h> |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 36 | #include <proto/log.h> |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 37 | #include <proto/http_ana.h> |
Willy Tarreau | 0f9cd7b | 2019-01-31 19:02:43 +0100 | [diff] [blame] | 38 | #include <proto/stream_interface.h> |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 39 | |
| 40 | |
| 41 | /* This function executes one of the set-{method,path,query,uri} actions. It |
| 42 | * builds a string in the trash from the specified format string. It finds |
| 43 | * the action to be performed in <http.action>, previously filled by function |
| 44 | * parse_set_req_line(). The replacement action is excuted by the function |
| 45 | * http_action_set_req_line(). It always returns ACT_RET_CONT. If an error |
| 46 | * occurs the action is canceled, but the rule processing continue. |
| 47 | */ |
| 48 | static enum act_return http_action_set_req_line(struct act_rule *rule, struct proxy *px, |
| 49 | struct session *sess, struct stream *s, int flags) |
| 50 | { |
| 51 | struct buffer *replace; |
| 52 | enum act_return ret = ACT_RET_ERR; |
| 53 | |
| 54 | replace = alloc_trash_chunk(); |
| 55 | if (!replace) |
| 56 | goto leave; |
| 57 | |
| 58 | /* If we have to create a query string, prepare a '?'. */ |
| 59 | if (rule->arg.http.action == 2) |
| 60 | replace->area[replace->data++] = '?'; |
| 61 | replace->data += build_logline(s, replace->area + replace->data, |
| 62 | replace->size - replace->data, |
| 63 | &rule->arg.http.logfmt); |
| 64 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 65 | http_req_replace_stline(rule->arg.http.action, replace->area, |
| 66 | replace->data, px, s); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 67 | |
| 68 | ret = ACT_RET_CONT; |
| 69 | |
| 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 | { |
| 144 | enum act_return ret = ACT_RET_ERR; |
| 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 | |
| 172 | ret = ACT_RET_CONT; |
| 173 | |
| 174 | leave: |
| 175 | free_trash_chunk(output); |
| 176 | free_trash_chunk(replace); |
| 177 | return ret; |
| 178 | } |
| 179 | |
Willy Tarreau | 262c3f1 | 2019-12-17 06:52:51 +0100 | [diff] [blame] | 180 | /* parse a "replace-uri" or "replace-path" http-request action. |
Willy Tarreau | 3381022 | 2019-06-12 17:44:02 +0200 | [diff] [blame] | 181 | * This action takes 2 arguments (a regex and a replacement format string). |
Willy Tarreau | 262c3f1 | 2019-12-17 06:52:51 +0100 | [diff] [blame] | 182 | * 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] | 183 | * p[1] to store the compiled regex, and arg->act.p[2..3] to store the log-format |
| 184 | * list head. It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error. |
| 185 | */ |
| 186 | static enum act_parse_ret parse_replace_uri(const char **args, int *orig_arg, struct proxy *px, |
| 187 | struct act_rule *rule, char **err) |
| 188 | { |
| 189 | int cur_arg = *orig_arg; |
| 190 | char *error = NULL; |
| 191 | |
| 192 | rule->action = ACT_CUSTOM; |
Willy Tarreau | 262c3f1 | 2019-12-17 06:52:51 +0100 | [diff] [blame] | 193 | if (strcmp(args[cur_arg-1], "replace-path") == 0) |
| 194 | rule->arg.act.p[0] = (void *)1; // replace-path |
| 195 | else |
| 196 | rule->arg.act.p[0] = (void *)3; // replace-uri |
| 197 | |
Willy Tarreau | 3381022 | 2019-06-12 17:44:02 +0200 | [diff] [blame] | 198 | rule->action_ptr = http_action_replace_uri; |
| 199 | |
| 200 | if (!*args[cur_arg] || !*args[cur_arg+1] || |
| 201 | (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) { |
| 202 | memprintf(err, "expects exactly 2 arguments <match-regex> and <replace-format>"); |
| 203 | return ACT_RET_PRS_ERR; |
| 204 | } |
| 205 | |
| 206 | if (!(rule->arg.act.p[1] = regex_comp(args[cur_arg], 1, 1, &error))) { |
| 207 | memprintf(err, "failed to parse the regex : %s", error); |
| 208 | free(error); |
| 209 | return ACT_RET_PRS_ERR; |
| 210 | } |
| 211 | |
| 212 | LIST_INIT((struct list *)&rule->arg.act.p[2]); |
| 213 | px->conf.args.ctx = ARGC_HRQ; |
| 214 | if (!parse_logformat_string(args[cur_arg + 1], px, (struct list *)&rule->arg.act.p[2], LOG_OPT_HTTP, |
| 215 | (px->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, err)) { |
| 216 | return ACT_RET_PRS_ERR; |
| 217 | } |
| 218 | |
| 219 | (*orig_arg) += 2; |
| 220 | return ACT_RET_PRS_OK; |
| 221 | } |
| 222 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 223 | /* This function is just a compliant action wrapper for "set-status". */ |
| 224 | static enum act_return action_http_set_status(struct act_rule *rule, struct proxy *px, |
| 225 | struct session *sess, struct stream *s, int flags) |
| 226 | { |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 227 | 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] | 228 | return ACT_RET_CONT; |
| 229 | } |
| 230 | |
| 231 | /* parse set-status action: |
| 232 | * This action accepts a single argument of type int representing |
| 233 | * an http status code. It returns ACT_RET_PRS_OK on success, |
| 234 | * ACT_RET_PRS_ERR on error. |
| 235 | */ |
| 236 | static enum act_parse_ret parse_http_set_status(const char **args, int *orig_arg, struct proxy *px, |
| 237 | struct act_rule *rule, char **err) |
| 238 | { |
| 239 | char *error; |
| 240 | |
| 241 | rule->action = ACT_CUSTOM; |
| 242 | rule->action_ptr = action_http_set_status; |
| 243 | |
| 244 | /* Check if an argument is available */ |
| 245 | if (!*args[*orig_arg]) { |
| 246 | memprintf(err, "expects 1 argument: <status>; or 3 arguments: <status> reason <fmt>"); |
| 247 | return ACT_RET_PRS_ERR; |
| 248 | } |
| 249 | |
| 250 | /* convert status code as integer */ |
| 251 | rule->arg.status.code = strtol(args[*orig_arg], &error, 10); |
| 252 | if (*error != '\0' || rule->arg.status.code < 100 || rule->arg.status.code > 999) { |
| 253 | memprintf(err, "expects an integer status code between 100 and 999"); |
| 254 | return ACT_RET_PRS_ERR; |
| 255 | } |
| 256 | |
| 257 | (*orig_arg)++; |
| 258 | |
| 259 | /* set custom reason string */ |
| 260 | rule->arg.status.reason = NULL; // If null, we use the default reason for the status code. |
| 261 | if (*args[*orig_arg] && strcmp(args[*orig_arg], "reason") == 0 && |
| 262 | (*args[*orig_arg + 1] && strcmp(args[*orig_arg + 1], "if") != 0 && strcmp(args[*orig_arg + 1], "unless") != 0)) { |
| 263 | (*orig_arg)++; |
| 264 | rule->arg.status.reason = strdup(args[*orig_arg]); |
| 265 | (*orig_arg)++; |
| 266 | } |
| 267 | |
| 268 | return ACT_RET_PRS_OK; |
| 269 | } |
| 270 | |
| 271 | /* This function executes the "reject" HTTP action. It clears the request and |
| 272 | * response buffer without sending any response. It can be useful as an HTTP |
| 273 | * alternative to the silent-drop action to defend against DoS attacks, and may |
| 274 | * also be used with HTTP/2 to close a connection instead of just a stream. |
| 275 | * The txn status is unchanged, indicating no response was sent. The termination |
Christopher Faulet | 8f1aa77 | 2019-07-04 11:27:15 +0200 | [diff] [blame] | 276 | * flags will indicate "PR". It always returns ACT_RET_DONE. |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 277 | */ |
| 278 | static enum act_return http_action_reject(struct act_rule *rule, struct proxy *px, |
| 279 | struct session *sess, struct stream *s, int flags) |
| 280 | { |
Willy Tarreau | 0f9cd7b | 2019-01-31 19:02:43 +0100 | [diff] [blame] | 281 | si_must_kill_conn(chn_prod(&s->req)); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 282 | channel_abort(&s->req); |
| 283 | channel_abort(&s->res); |
| 284 | s->req.analysers = 0; |
| 285 | s->res.analysers = 0; |
| 286 | |
Olivier Houchard | a798bf5 | 2019-03-08 18:52:00 +0100 | [diff] [blame] | 287 | _HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1); |
| 288 | _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 289 | if (sess->listener && sess->listener->counters) |
Olivier Houchard | a798bf5 | 2019-03-08 18:52:00 +0100 | [diff] [blame] | 290 | _HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 291 | |
| 292 | if (!(s->flags & SF_ERR_MASK)) |
| 293 | s->flags |= SF_ERR_PRXCOND; |
| 294 | if (!(s->flags & SF_FINST_MASK)) |
| 295 | s->flags |= SF_FINST_R; |
| 296 | |
Christopher Faulet | 8f1aa77 | 2019-07-04 11:27:15 +0200 | [diff] [blame] | 297 | return ACT_RET_DONE; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | /* parse the "reject" action: |
| 301 | * This action takes no argument and returns ACT_RET_PRS_OK on success, |
| 302 | * ACT_RET_PRS_ERR on error. |
| 303 | */ |
| 304 | static enum act_parse_ret parse_http_action_reject(const char **args, int *orig_arg, struct proxy *px, |
| 305 | struct act_rule *rule, char **err) |
| 306 | { |
| 307 | rule->action = ACT_CUSTOM; |
| 308 | rule->action_ptr = http_action_reject; |
| 309 | return ACT_RET_PRS_OK; |
| 310 | } |
| 311 | |
Olivier Houchard | 602bf7d | 2019-05-10 13:59:15 +0200 | [diff] [blame] | 312 | /* This function executes the "disable-l7-retry" HTTP action. |
| 313 | * It disables L7 retries (all retry except for a connection failure). This |
| 314 | * can be useful for example to avoid retrying on POST requests. |
| 315 | * It just removes the L7 retry flag on the stream_interface, and always |
| 316 | * return ACT_RET_CONT; |
| 317 | */ |
| 318 | static enum act_return http_req_disable_l7_retry(struct act_rule *rule, struct proxy *px, |
| 319 | struct session *sess, struct stream *s, int flags) |
| 320 | { |
| 321 | struct stream_interface *si = &s->si[1]; |
| 322 | |
| 323 | /* In theory, the SI_FL_L7_RETRY flags isn't set at this point, but |
| 324 | * let's be future-proof and remove it anyway. |
| 325 | */ |
| 326 | si->flags &= ~SI_FL_L7_RETRY; |
| 327 | si->flags |= SI_FL_D_L7_RETRY; |
| 328 | return ACT_RET_CONT; |
| 329 | } |
| 330 | |
| 331 | /* parse the "disable-l7-retry" action: |
| 332 | * This action takes no argument and returns ACT_RET_PRS_OK on success, |
| 333 | * ACT_RET_PRS_ERR on error. |
| 334 | */ |
| 335 | static enum act_parse_ret parse_http_req_disable_l7_retry(const char **args, |
| 336 | int *orig_args, struct proxy *px, |
| 337 | struct act_rule *rule, char **err) |
| 338 | { |
| 339 | rule->action = ACT_CUSTOM; |
| 340 | rule->action_ptr = http_req_disable_l7_retry; |
| 341 | return ACT_RET_PRS_OK; |
| 342 | } |
| 343 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 344 | /* This function executes the "capture" action. It executes a fetch expression, |
| 345 | * turns the result into a string and puts it in a capture slot. It always |
| 346 | * returns 1. If an error occurs the action is cancelled, but the rule |
| 347 | * processing continues. |
| 348 | */ |
| 349 | static enum act_return http_action_req_capture(struct act_rule *rule, struct proxy *px, |
| 350 | struct session *sess, struct stream *s, int flags) |
| 351 | { |
| 352 | struct sample *key; |
| 353 | struct cap_hdr *h = rule->arg.cap.hdr; |
| 354 | char **cap = s->req_cap; |
| 355 | int len; |
| 356 | |
| 357 | key = sample_fetch_as_type(s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.cap.expr, SMP_T_STR); |
| 358 | if (!key) |
| 359 | return ACT_RET_CONT; |
| 360 | |
| 361 | if (cap[h->index] == NULL) |
| 362 | cap[h->index] = pool_alloc(h->pool); |
| 363 | |
| 364 | if (cap[h->index] == NULL) /* no more capture memory */ |
| 365 | return ACT_RET_CONT; |
| 366 | |
| 367 | len = key->data.u.str.data; |
| 368 | if (len > h->len) |
| 369 | len = h->len; |
| 370 | |
| 371 | memcpy(cap[h->index], key->data.u.str.area, len); |
| 372 | cap[h->index][len] = 0; |
| 373 | return ACT_RET_CONT; |
| 374 | } |
| 375 | |
| 376 | /* This function executes the "capture" action and store the result in a |
| 377 | * capture slot if exists. It executes a fetch expression, turns the result |
| 378 | * into a string and puts it in a capture slot. It always returns 1. If an |
| 379 | * error occurs the action is cancelled, but the rule processing continues. |
| 380 | */ |
| 381 | static enum act_return http_action_req_capture_by_id(struct act_rule *rule, struct proxy *px, |
| 382 | struct session *sess, struct stream *s, int flags) |
| 383 | { |
| 384 | struct sample *key; |
| 385 | struct cap_hdr *h; |
| 386 | char **cap = s->req_cap; |
| 387 | struct proxy *fe = strm_fe(s); |
| 388 | int len; |
| 389 | int i; |
| 390 | |
| 391 | /* Look for the original configuration. */ |
| 392 | for (h = fe->req_cap, i = fe->nb_req_cap - 1; |
| 393 | h != NULL && i != rule->arg.capid.idx ; |
| 394 | i--, h = h->next); |
| 395 | if (!h) |
| 396 | return ACT_RET_CONT; |
| 397 | |
| 398 | key = sample_fetch_as_type(s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.capid.expr, SMP_T_STR); |
| 399 | if (!key) |
| 400 | return ACT_RET_CONT; |
| 401 | |
| 402 | if (cap[h->index] == NULL) |
| 403 | cap[h->index] = pool_alloc(h->pool); |
| 404 | |
| 405 | if (cap[h->index] == NULL) /* no more capture memory */ |
| 406 | return ACT_RET_CONT; |
| 407 | |
| 408 | len = key->data.u.str.data; |
| 409 | if (len > h->len) |
| 410 | len = h->len; |
| 411 | |
| 412 | memcpy(cap[h->index], key->data.u.str.area, len); |
| 413 | cap[h->index][len] = 0; |
| 414 | return ACT_RET_CONT; |
| 415 | } |
| 416 | |
| 417 | /* Check an "http-request capture" action. |
| 418 | * |
| 419 | * The function returns 1 in success case, otherwise, it returns 0 and err is |
| 420 | * filled. |
| 421 | */ |
| 422 | static int check_http_req_capture(struct act_rule *rule, struct proxy *px, char **err) |
| 423 | { |
| 424 | if (rule->action_ptr != http_action_req_capture_by_id) |
| 425 | return 1; |
| 426 | |
| 427 | if (rule->arg.capid.idx >= px->nb_req_cap) { |
| 428 | memprintf(err, "unable to find capture id '%d' referenced by http-request capture rule", |
| 429 | rule->arg.capid.idx); |
| 430 | return 0; |
| 431 | } |
| 432 | |
| 433 | return 1; |
| 434 | } |
| 435 | |
| 436 | /* parse an "http-request capture" action. It takes a single argument which is |
| 437 | * a sample fetch expression. It stores the expression into arg->act.p[0] and |
| 438 | * the allocated hdr_cap struct or the preallocated "id" into arg->act.p[1]. |
| 439 | * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error. |
| 440 | */ |
| 441 | static enum act_parse_ret parse_http_req_capture(const char **args, int *orig_arg, struct proxy *px, |
| 442 | struct act_rule *rule, char **err) |
| 443 | { |
| 444 | struct sample_expr *expr; |
| 445 | struct cap_hdr *hdr; |
| 446 | int cur_arg; |
| 447 | int len = 0; |
| 448 | |
| 449 | for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++) |
| 450 | if (strcmp(args[cur_arg], "if") == 0 || |
| 451 | strcmp(args[cur_arg], "unless") == 0) |
| 452 | break; |
| 453 | |
| 454 | if (cur_arg < *orig_arg + 3) { |
| 455 | memprintf(err, "expects <expression> [ 'len' <length> | id <idx> ]"); |
| 456 | return ACT_RET_PRS_ERR; |
| 457 | } |
| 458 | |
| 459 | cur_arg = *orig_arg; |
| 460 | expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args); |
| 461 | if (!expr) |
| 462 | return ACT_RET_PRS_ERR; |
| 463 | |
| 464 | if (!(expr->fetch->val & SMP_VAL_FE_HRQ_HDR)) { |
| 465 | memprintf(err, |
| 466 | "fetch method '%s' extracts information from '%s', none of which is available here", |
| 467 | args[cur_arg-1], sample_src_names(expr->fetch->use)); |
| 468 | free(expr); |
| 469 | return ACT_RET_PRS_ERR; |
| 470 | } |
| 471 | |
| 472 | if (!args[cur_arg] || !*args[cur_arg]) { |
| 473 | memprintf(err, "expects 'len or 'id'"); |
| 474 | free(expr); |
| 475 | return ACT_RET_PRS_ERR; |
| 476 | } |
| 477 | |
| 478 | if (strcmp(args[cur_arg], "len") == 0) { |
| 479 | cur_arg++; |
| 480 | |
| 481 | if (!(px->cap & PR_CAP_FE)) { |
| 482 | memprintf(err, "proxy '%s' has no frontend capability", px->id); |
| 483 | return ACT_RET_PRS_ERR; |
| 484 | } |
| 485 | |
| 486 | px->conf.args.ctx = ARGC_CAP; |
| 487 | |
| 488 | if (!args[cur_arg]) { |
| 489 | memprintf(err, "missing length value"); |
| 490 | free(expr); |
| 491 | return ACT_RET_PRS_ERR; |
| 492 | } |
| 493 | /* we copy the table name for now, it will be resolved later */ |
| 494 | len = atoi(args[cur_arg]); |
| 495 | if (len <= 0) { |
| 496 | memprintf(err, "length must be > 0"); |
| 497 | free(expr); |
| 498 | return ACT_RET_PRS_ERR; |
| 499 | } |
| 500 | cur_arg++; |
| 501 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 502 | hdr = calloc(1, sizeof(*hdr)); |
| 503 | hdr->next = px->req_cap; |
| 504 | hdr->name = NULL; /* not a header capture */ |
| 505 | hdr->namelen = 0; |
| 506 | hdr->len = len; |
| 507 | hdr->pool = create_pool("caphdr", hdr->len + 1, MEM_F_SHARED); |
| 508 | hdr->index = px->nb_req_cap++; |
| 509 | |
| 510 | px->req_cap = hdr; |
| 511 | px->to_log |= LW_REQHDR; |
| 512 | |
| 513 | rule->action = ACT_CUSTOM; |
| 514 | rule->action_ptr = http_action_req_capture; |
| 515 | rule->arg.cap.expr = expr; |
| 516 | rule->arg.cap.hdr = hdr; |
| 517 | } |
| 518 | |
| 519 | else if (strcmp(args[cur_arg], "id") == 0) { |
| 520 | int id; |
| 521 | char *error; |
| 522 | |
| 523 | cur_arg++; |
| 524 | |
| 525 | if (!args[cur_arg]) { |
| 526 | memprintf(err, "missing id value"); |
| 527 | free(expr); |
| 528 | return ACT_RET_PRS_ERR; |
| 529 | } |
| 530 | |
| 531 | id = strtol(args[cur_arg], &error, 10); |
| 532 | if (*error != '\0') { |
| 533 | memprintf(err, "cannot parse id '%s'", args[cur_arg]); |
| 534 | free(expr); |
| 535 | return ACT_RET_PRS_ERR; |
| 536 | } |
| 537 | cur_arg++; |
| 538 | |
| 539 | px->conf.args.ctx = ARGC_CAP; |
| 540 | |
| 541 | rule->action = ACT_CUSTOM; |
| 542 | rule->action_ptr = http_action_req_capture_by_id; |
| 543 | rule->check_ptr = check_http_req_capture; |
| 544 | rule->arg.capid.expr = expr; |
| 545 | rule->arg.capid.idx = id; |
| 546 | } |
| 547 | |
| 548 | else { |
| 549 | memprintf(err, "expects 'len' or 'id', found '%s'", args[cur_arg]); |
| 550 | free(expr); |
| 551 | return ACT_RET_PRS_ERR; |
| 552 | } |
| 553 | |
| 554 | *orig_arg = cur_arg; |
| 555 | return ACT_RET_PRS_OK; |
| 556 | } |
| 557 | |
| 558 | /* This function executes the "capture" action and store the result in a |
| 559 | * capture slot if exists. It executes a fetch expression, turns the result |
| 560 | * into a string and puts it in a capture slot. It always returns 1. If an |
| 561 | * error occurs the action is cancelled, but the rule processing continues. |
| 562 | */ |
| 563 | static enum act_return http_action_res_capture_by_id(struct act_rule *rule, struct proxy *px, |
| 564 | struct session *sess, struct stream *s, int flags) |
| 565 | { |
| 566 | struct sample *key; |
| 567 | struct cap_hdr *h; |
| 568 | char **cap = s->res_cap; |
| 569 | struct proxy *fe = strm_fe(s); |
| 570 | int len; |
| 571 | int i; |
| 572 | |
| 573 | /* Look for the original configuration. */ |
| 574 | for (h = fe->rsp_cap, i = fe->nb_rsp_cap - 1; |
| 575 | h != NULL && i != rule->arg.capid.idx ; |
| 576 | i--, h = h->next); |
| 577 | if (!h) |
| 578 | return ACT_RET_CONT; |
| 579 | |
| 580 | key = sample_fetch_as_type(s->be, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL, rule->arg.capid.expr, SMP_T_STR); |
| 581 | if (!key) |
| 582 | return ACT_RET_CONT; |
| 583 | |
| 584 | if (cap[h->index] == NULL) |
| 585 | cap[h->index] = pool_alloc(h->pool); |
| 586 | |
| 587 | if (cap[h->index] == NULL) /* no more capture memory */ |
| 588 | return ACT_RET_CONT; |
| 589 | |
| 590 | len = key->data.u.str.data; |
| 591 | if (len > h->len) |
| 592 | len = h->len; |
| 593 | |
| 594 | memcpy(cap[h->index], key->data.u.str.area, len); |
| 595 | cap[h->index][len] = 0; |
| 596 | return ACT_RET_CONT; |
| 597 | } |
| 598 | |
| 599 | /* Check an "http-response capture" action. |
| 600 | * |
| 601 | * The function returns 1 in success case, otherwise, it returns 0 and err is |
| 602 | * filled. |
| 603 | */ |
| 604 | static int check_http_res_capture(struct act_rule *rule, struct proxy *px, char **err) |
| 605 | { |
| 606 | if (rule->action_ptr != http_action_res_capture_by_id) |
| 607 | return 1; |
| 608 | |
| 609 | if (rule->arg.capid.idx >= px->nb_rsp_cap) { |
| 610 | memprintf(err, "unable to find capture id '%d' referenced by http-response capture rule", |
| 611 | rule->arg.capid.idx); |
| 612 | return 0; |
| 613 | } |
| 614 | |
| 615 | return 1; |
| 616 | } |
| 617 | |
| 618 | /* parse an "http-response capture" action. It takes a single argument which is |
| 619 | * a sample fetch expression. It stores the expression into arg->act.p[0] and |
| 620 | * the allocated hdr_cap struct od the preallocated id into arg->act.p[1]. |
| 621 | * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error. |
| 622 | */ |
| 623 | static enum act_parse_ret parse_http_res_capture(const char **args, int *orig_arg, struct proxy *px, |
| 624 | struct act_rule *rule, char **err) |
| 625 | { |
| 626 | struct sample_expr *expr; |
| 627 | int cur_arg; |
| 628 | int id; |
| 629 | char *error; |
| 630 | |
| 631 | for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++) |
| 632 | if (strcmp(args[cur_arg], "if") == 0 || |
| 633 | strcmp(args[cur_arg], "unless") == 0) |
| 634 | break; |
| 635 | |
| 636 | if (cur_arg < *orig_arg + 3) { |
| 637 | memprintf(err, "expects <expression> id <idx>"); |
| 638 | return ACT_RET_PRS_ERR; |
| 639 | } |
| 640 | |
| 641 | cur_arg = *orig_arg; |
| 642 | expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args); |
| 643 | if (!expr) |
| 644 | return ACT_RET_PRS_ERR; |
| 645 | |
| 646 | if (!(expr->fetch->val & SMP_VAL_FE_HRS_HDR)) { |
| 647 | memprintf(err, |
| 648 | "fetch method '%s' extracts information from '%s', none of which is available here", |
| 649 | args[cur_arg-1], sample_src_names(expr->fetch->use)); |
| 650 | free(expr); |
| 651 | return ACT_RET_PRS_ERR; |
| 652 | } |
| 653 | |
| 654 | if (!args[cur_arg] || !*args[cur_arg]) { |
| 655 | memprintf(err, "expects 'id'"); |
| 656 | free(expr); |
| 657 | return ACT_RET_PRS_ERR; |
| 658 | } |
| 659 | |
| 660 | if (strcmp(args[cur_arg], "id") != 0) { |
| 661 | memprintf(err, "expects 'id', found '%s'", args[cur_arg]); |
| 662 | free(expr); |
| 663 | return ACT_RET_PRS_ERR; |
| 664 | } |
| 665 | |
| 666 | cur_arg++; |
| 667 | |
| 668 | if (!args[cur_arg]) { |
| 669 | memprintf(err, "missing id value"); |
| 670 | free(expr); |
| 671 | return ACT_RET_PRS_ERR; |
| 672 | } |
| 673 | |
| 674 | id = strtol(args[cur_arg], &error, 10); |
| 675 | if (*error != '\0') { |
| 676 | memprintf(err, "cannot parse id '%s'", args[cur_arg]); |
| 677 | free(expr); |
| 678 | return ACT_RET_PRS_ERR; |
| 679 | } |
| 680 | cur_arg++; |
| 681 | |
| 682 | px->conf.args.ctx = ARGC_CAP; |
| 683 | |
| 684 | rule->action = ACT_CUSTOM; |
| 685 | rule->action_ptr = http_action_res_capture_by_id; |
| 686 | rule->check_ptr = check_http_res_capture; |
| 687 | rule->arg.capid.expr = expr; |
| 688 | rule->arg.capid.idx = id; |
| 689 | |
| 690 | *orig_arg = cur_arg; |
| 691 | return ACT_RET_PRS_OK; |
| 692 | } |
| 693 | |
| 694 | /************************************************************************/ |
| 695 | /* All supported http-request action keywords must be declared here. */ |
| 696 | /************************************************************************/ |
| 697 | |
| 698 | static struct action_kw_list http_req_actions = { |
| 699 | .kw = { |
| 700 | { "capture", parse_http_req_capture }, |
| 701 | { "reject", parse_http_action_reject }, |
Olivier Houchard | 602bf7d | 2019-05-10 13:59:15 +0200 | [diff] [blame] | 702 | { "disable-l7-retry", parse_http_req_disable_l7_retry }, |
Willy Tarreau | 262c3f1 | 2019-12-17 06:52:51 +0100 | [diff] [blame] | 703 | { "replace-path", parse_replace_uri }, |
Willy Tarreau | 3381022 | 2019-06-12 17:44:02 +0200 | [diff] [blame] | 704 | { "replace-uri", parse_replace_uri }, |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 705 | { "set-method", parse_set_req_line }, |
| 706 | { "set-path", parse_set_req_line }, |
| 707 | { "set-query", parse_set_req_line }, |
| 708 | { "set-uri", parse_set_req_line }, |
| 709 | { NULL, NULL } |
| 710 | } |
| 711 | }; |
| 712 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 713 | INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_actions); |
| 714 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 715 | static struct action_kw_list http_res_actions = { |
| 716 | .kw = { |
| 717 | { "capture", parse_http_res_capture }, |
| 718 | { "set-status", parse_http_set_status }, |
| 719 | { NULL, NULL } |
| 720 | } |
| 721 | }; |
| 722 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 723 | INITCALL1(STG_REGISTER, http_res_keywords_register, &http_res_actions); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 724 | |
| 725 | /* |
| 726 | * Local variables: |
| 727 | * c-indent-level: 8 |
| 728 | * c-basic-offset: 8 |
| 729 | * End: |
| 730 | */ |