blob: 7e1829e9db93fa5fb91ecd24105ed6d5f206e2a2 [file] [log] [blame]
Willy Tarreau79e57332018-10-02 16:01:16 +02001/*
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
Willy Tarreaudcc048a2020-06-04 19:11:43 +020019#include <haproxy/acl.h>
Willy Tarreau122eba92020-06-04 10:15:32 +020020#include <haproxy/action.h>
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020021#include <haproxy/api.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020022#include <haproxy/arg.h>
23#include <haproxy/capture-t.h>
Willy Tarreau6be78492020-06-05 00:00:29 +020024#include <haproxy/cfgparse.h>
Willy Tarreauc13ed532020-06-02 10:22:45 +020025#include <haproxy/chunk.h>
Willy Tarreauf268ee82020-06-04 17:05:57 +020026#include <haproxy/global.h>
Willy Tarreaucd72d8c2020-06-02 19:11:26 +020027#include <haproxy/http.h>
Willy Tarreauc2b1ff02020-06-04 21:21:03 +020028#include <haproxy/http_ana.h>
Willy Tarreau87735332020-06-04 09:08:41 +020029#include <haproxy/http_htx.h>
Willy Tarreauc761f842020-06-04 11:40:28 +020030#include <haproxy/http_rules.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020031#include <haproxy/log.h>
Willy Tarreau225a90a2020-06-04 15:06:28 +020032#include <haproxy/pattern.h>
Willy Tarreaud0ef4392020-06-02 09:38:52 +020033#include <haproxy/pool.h>
Willy Tarreau7cd8b6e2020-06-02 17:32:26 +020034#include <haproxy/regex.h>
Willy Tarreaue6ce10b2020-06-04 15:33:47 +020035#include <haproxy/sample.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020036#include <haproxy/stream_interface.h>
Willy Tarreau48fbcae2020-06-03 18:09:46 +020037#include <haproxy/tools.h>
Willy Tarreau8c42b8a2020-06-04 19:27:34 +020038#include <haproxy/uri_auth-t.h>
Tim Duesterhusd2bedcc2021-04-15 21:45:57 +020039#include <haproxy/uri_normalizer.h>
Willy Tarreaud6788052020-05-27 15:59:00 +020040#include <haproxy/version.h>
Willy Tarreau79e57332018-10-02 16:01:16 +020041
Willy Tarreau79e57332018-10-02 16:01:16 +020042
Christopher Faulet2eb53962020-01-14 14:47:34 +010043/* Release memory allocated by most of HTTP actions. Concretly, it releases
44 * <arg.http>.
45 */
46static void release_http_action(struct act_rule *rule)
47{
48 struct logformat_node *lf, *lfb;
49
Tim Duesterhused526372020-03-05 17:56:33 +010050 istfree(&rule->arg.http.str);
Christopher Faulet2eb53962020-01-14 14:47:34 +010051 if (rule->arg.http.re)
52 regex_free(rule->arg.http.re);
53 list_for_each_entry_safe(lf, lfb, &rule->arg.http.fmt, list) {
54 LIST_DEL(&lf->list);
55 release_sample_expr(lf->expr);
56 free(lf->arg);
57 free(lf);
58 }
59}
60
Christopher Faulet5cb513a2020-05-13 17:56:56 +020061/* Release memory allocated by HTTP actions relying on an http reply. Concretly,
62 * it releases <.arg.http_reply>
63 */
64static void release_act_http_reply(struct act_rule *rule)
65{
66 release_http_reply(rule->arg.http_reply);
67 rule->arg.http_reply = NULL;
68}
69
70
71/* Check function for HTTP actions relying on an http reply. The function
72 * returns 1 in success case, otherwise, it returns 0 and err is filled.
73 */
74static int check_act_http_reply(struct act_rule *rule, struct proxy *px, char **err)
75{
76 struct http_reply *reply = rule->arg.http_reply;
77
78 if (!http_check_http_reply(reply, px, err)) {
79 release_act_http_reply(rule);
80 return 0;
81 }
82 return 1;
83}
84
Willy Tarreau79e57332018-10-02 16:01:16 +020085
86/* This function executes one of the set-{method,path,query,uri} actions. It
87 * builds a string in the trash from the specified format string. It finds
Christopher Faulet2c22a692019-12-18 15:39:56 +010088 * the action to be performed in <.action>, previously filled by function
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +050089 * parse_set_req_line(). The replacement action is executed by the function
Christopher Faulete00d06c2019-12-16 17:18:42 +010090 * http_action_set_req_line(). On success, it returns ACT_RET_CONT. If an error
91 * occurs while soft rewrites are enabled, the action is canceled, but the rule
92 * processing continue. Otherwsize ACT_RET_ERR is returned.
Willy Tarreau79e57332018-10-02 16:01:16 +020093 */
94static enum act_return http_action_set_req_line(struct act_rule *rule, struct proxy *px,
95 struct session *sess, struct stream *s, int flags)
96{
97 struct buffer *replace;
Christopher Faulet13403762019-12-13 09:01:57 +010098 enum act_return ret = ACT_RET_CONT;
Willy Tarreau79e57332018-10-02 16:01:16 +020099
100 replace = alloc_trash_chunk();
101 if (!replace)
Christopher Faulete00d06c2019-12-16 17:18:42 +0100102 goto fail_alloc;
Willy Tarreau79e57332018-10-02 16:01:16 +0200103
104 /* If we have to create a query string, prepare a '?'. */
Christopher Faulet2c22a692019-12-18 15:39:56 +0100105 if (rule->action == 2) // set-query
Willy Tarreau79e57332018-10-02 16:01:16 +0200106 replace->area[replace->data++] = '?';
107 replace->data += build_logline(s, replace->area + replace->data,
108 replace->size - replace->data,
Christopher Faulet96bff762019-12-17 13:46:18 +0100109 &rule->arg.http.fmt);
Willy Tarreau79e57332018-10-02 16:01:16 +0200110
Christopher Faulet2c22a692019-12-18 15:39:56 +0100111 if (http_req_replace_stline(rule->action, replace->area, replace->data, px, s) == -1)
Christopher Faulete00d06c2019-12-16 17:18:42 +0100112 goto fail_rewrite;
Willy Tarreau79e57332018-10-02 16:01:16 +0200113
Christopher Faulete00d06c2019-12-16 17:18:42 +0100114 leave:
Willy Tarreau79e57332018-10-02 16:01:16 +0200115 free_trash_chunk(replace);
116 return ret;
Christopher Faulete00d06c2019-12-16 17:18:42 +0100117
118 fail_alloc:
119 if (!(s->flags & SF_ERR_MASK))
120 s->flags |= SF_ERR_RESOURCE;
121 ret = ACT_RET_ERR;
122 goto leave;
123
124 fail_rewrite:
Willy Tarreau4781b152021-04-06 13:53:36 +0200125 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_rewrites);
Christopher Faulete00d06c2019-12-16 17:18:42 +0100126 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200127 _HA_ATOMIC_INC(&s->be->be_counters.failed_rewrites);
William Lallemand36119de2021-03-08 15:26:48 +0100128 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200129 _HA_ATOMIC_INC(&sess->listener->counters->failed_rewrites);
Christopher Faulete00d06c2019-12-16 17:18:42 +0100130 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +0200131 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_rewrites);
Christopher Faulete00d06c2019-12-16 17:18:42 +0100132
Christopher Faulet333bf8c2020-01-22 14:38:05 +0100133 if (!(s->txn->req.flags & HTTP_MSGF_SOFT_RW)) {
Christopher Faulete00d06c2019-12-16 17:18:42 +0100134 ret = ACT_RET_ERR;
Christopher Faulet333bf8c2020-01-22 14:38:05 +0100135 if (!(s->flags & SF_ERR_MASK))
136 s->flags |= SF_ERR_PRXCOND;
137 }
Christopher Faulete00d06c2019-12-16 17:18:42 +0100138 goto leave;
Willy Tarreau79e57332018-10-02 16:01:16 +0200139}
140
141/* parse an http-request action among :
142 * set-method
143 * set-path
Christopher Faulet312294f2020-09-02 17:17:44 +0200144 * set-pathq
Willy Tarreau79e57332018-10-02 16:01:16 +0200145 * set-query
146 * set-uri
147 *
148 * All of them accept a single argument of type string representing a log-format.
Christopher Faulet96bff762019-12-17 13:46:18 +0100149 * The resulting rule makes use of <http.fmt> to store the log-format list head,
Christopher Faulet2c22a692019-12-18 15:39:56 +0100150 * and <.action> to store the action type as an int (0=method, 1=path, 2=query,
151 * 3=uri). It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Willy Tarreau79e57332018-10-02 16:01:16 +0200152 */
153static enum act_parse_ret parse_set_req_line(const char **args, int *orig_arg, struct proxy *px,
154 struct act_rule *rule, char **err)
155{
156 int cur_arg = *orig_arg;
157
Willy Tarreau79e57332018-10-02 16:01:16 +0200158 switch (args[0][4]) {
159 case 'm' :
Christopher Faulet2c22a692019-12-18 15:39:56 +0100160 rule->action = 0; // set-method
Willy Tarreau79e57332018-10-02 16:01:16 +0200161 break;
162 case 'p' :
Christopher Faulet312294f2020-09-02 17:17:44 +0200163 if (args[0][8] == 'q')
164 rule->action = 4; // set-pathq
165 else
166 rule->action = 1; // set-path
Willy Tarreau79e57332018-10-02 16:01:16 +0200167 break;
168 case 'q' :
Christopher Faulet2c22a692019-12-18 15:39:56 +0100169 rule->action = 2; // set-query
Willy Tarreau79e57332018-10-02 16:01:16 +0200170 break;
171 case 'u' :
Christopher Faulet2c22a692019-12-18 15:39:56 +0100172 rule->action = 3; // set-uri
Willy Tarreau79e57332018-10-02 16:01:16 +0200173 break;
174 default:
175 memprintf(err, "internal error: unhandled action '%s'", args[0]);
176 return ACT_RET_PRS_ERR;
177 }
Christopher Faulet96bff762019-12-17 13:46:18 +0100178 rule->action_ptr = http_action_set_req_line;
Christopher Faulet2eb53962020-01-14 14:47:34 +0100179 rule->release_ptr = release_http_action;
Willy Tarreau79e57332018-10-02 16:01:16 +0200180
181 if (!*args[cur_arg] ||
182 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
183 memprintf(err, "expects exactly 1 argument <format>");
184 return ACT_RET_PRS_ERR;
185 }
186
Christopher Faulet96bff762019-12-17 13:46:18 +0100187 LIST_INIT(&rule->arg.http.fmt);
Willy Tarreau79e57332018-10-02 16:01:16 +0200188 px->conf.args.ctx = ARGC_HRQ;
Christopher Faulet96bff762019-12-17 13:46:18 +0100189 if (!parse_logformat_string(args[cur_arg], px, &rule->arg.http.fmt, LOG_OPT_HTTP,
Willy Tarreau79e57332018-10-02 16:01:16 +0200190 (px->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, err)) {
191 return ACT_RET_PRS_ERR;
192 }
193
194 (*orig_arg)++;
195 return ACT_RET_PRS_OK;
196}
197
Tim Duesterhusd2bedcc2021-04-15 21:45:57 +0200198/* This function executes the http-request normalize-uri action.
199 * `rule->action` is expected to be a value from `enum act_normalize_uri`.
200 *
201 * On success, it returns ACT_RET_CONT. If an error
202 * occurs while soft rewrites are enabled, the action is canceled, but the rule
203 * processing continue. Otherwsize ACT_RET_ERR is returned.
204 */
205static enum act_return http_action_normalize_uri(struct act_rule *rule, struct proxy *px,
206 struct session *sess, struct stream *s, int flags)
207{
208 enum act_return ret = ACT_RET_CONT;
209 struct htx *htx = htxbuf(&s->req.buf);
210 const struct ist uri = htx_sl_req_uri(http_get_stline(htx));
211 struct buffer *replace = alloc_trash_chunk();
212 enum uri_normalizer_err err = URI_NORMALIZER_ERR_INTERNAL_ERROR;
213
214 if (!replace)
215 goto fail_alloc;
216
217 switch ((enum act_normalize_uri) rule->action) {
Tim Duesterhusd371e992021-04-15 21:45:58 +0200218 case ACT_NORMALIZE_URI_MERGE_SLASHES: {
219 const struct ist path = http_get_path(uri);
220 struct ist newpath = ist2(replace->area, replace->size);
221
222 if (!isttest(path))
223 goto leave;
224
225 err = uri_normalizer_path_merge_slashes(iststop(path, '?'), &newpath);
226
227 if (err != URI_NORMALIZER_ERR_NONE)
228 break;
229
230 if (!http_replace_req_path(htx, newpath, 0))
231 goto fail_rewrite;
232
233 break;
234 }
Tim Duesterhus9982fc22021-04-15 21:45:59 +0200235 case ACT_NORMALIZE_URI_DOTDOT: {
236 const struct ist path = http_get_path(uri);
237 struct ist newpath = ist2(replace->area, replace->size);
238
239 if (!isttest(path))
240 goto leave;
241
242 err = uri_normalizer_path_dotdot(iststop(path, '?'), &newpath);
243
244 if (err != URI_NORMALIZER_ERR_NONE)
245 break;
246
247 if (!http_replace_req_path(htx, newpath, 0))
248 goto fail_rewrite;
249
250 break;
251 }
Tim Duesterhusd2bedcc2021-04-15 21:45:57 +0200252 }
253
254 switch (err) {
255 case URI_NORMALIZER_ERR_NONE:
256 break;
257 case URI_NORMALIZER_ERR_INTERNAL_ERROR:
258 ret = ACT_RET_ERR;
259 break;
260 case URI_NORMALIZER_ERR_INVALID_INPUT:
261 ret = ACT_RET_INV;
262 break;
263 case URI_NORMALIZER_ERR_ALLOC:
264 goto fail_alloc;
265 }
266
267 leave:
268 free_trash_chunk(replace);
269 return ret;
270
271 fail_alloc:
272 if (!(s->flags & SF_ERR_MASK))
273 s->flags |= SF_ERR_RESOURCE;
274 ret = ACT_RET_ERR;
275 goto leave;
276
277 fail_rewrite:
278 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_rewrites, 1);
279 if (s->flags & SF_BE_ASSIGNED)
280 _HA_ATOMIC_ADD(&s->be->be_counters.failed_rewrites, 1);
281 if (sess->listener && sess->listener->counters)
282 _HA_ATOMIC_ADD(&sess->listener->counters->failed_rewrites, 1);
283 if (objt_server(s->target))
284 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_rewrites, 1);
285
286 if (!(s->txn->req.flags & HTTP_MSGF_SOFT_RW)) {
287 ret = ACT_RET_ERR;
288 if (!(s->flags & SF_ERR_MASK))
289 s->flags |= SF_ERR_PRXCOND;
290 }
291 goto leave;
292}
293
294/* Parses the http-request normalize-uri action. It expects a single <normalizer>
295 * argument, corresponding too a value in `enum act_normalize_uri`.
296 *
297 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
298 */
299static enum act_parse_ret parse_http_normalize_uri(const char **args, int *orig_arg, struct proxy *px,
300 struct act_rule *rule, char **err)
301{
302 int cur_arg = *orig_arg;
303
304 rule->action_ptr = http_action_normalize_uri;
305 rule->release_ptr = NULL;
306
307 if (!*args[cur_arg]) {
308 memprintf(err, "missing argument <normalizer>");
309 return ACT_RET_PRS_ERR;
310 }
311
Tim Duesterhusd371e992021-04-15 21:45:58 +0200312 if (strcmp(args[cur_arg], "merge-slashes") == 0) {
313 cur_arg++;
Tim Duesterhusd2bedcc2021-04-15 21:45:57 +0200314
Tim Duesterhusd371e992021-04-15 21:45:58 +0200315 rule->action = ACT_NORMALIZE_URI_MERGE_SLASHES;
Tim Duesterhusd2bedcc2021-04-15 21:45:57 +0200316 }
Tim Duesterhus9982fc22021-04-15 21:45:59 +0200317 else if (strcmp(args[cur_arg], "dotdot") == 0) {
318 cur_arg++;
319
320 rule->action = ACT_NORMALIZE_URI_DOTDOT;
321 }
Tim Duesterhusd2bedcc2021-04-15 21:45:57 +0200322 else {
323 memprintf(err, "unknown normalizer '%s'", args[cur_arg]);
324 return ACT_RET_PRS_ERR;
325 }
326
327 *orig_arg = cur_arg;
328 return ACT_RET_PRS_OK;
329}
330
Willy Tarreau33810222019-06-12 17:44:02 +0200331/* This function executes a replace-uri action. It finds its arguments in
Christopher Faulet96bff762019-12-17 13:46:18 +0100332 * <rule>.arg.http. It builds a string in the trash from the format string
Willy Tarreau33810222019-06-12 17:44:02 +0200333 * previously filled by function parse_replace_uri() and will execute the regex
Christopher Faulet96bff762019-12-17 13:46:18 +0100334 * in <http.re> to replace the URI. It uses the format string present in
Christopher Faulet2c22a692019-12-18 15:39:56 +0100335 * <http.fmt>. The component to act on (path/uri) is taken from <.action> which
Christopher Faulet96bff762019-12-17 13:46:18 +0100336 * contains 1 for the path or 3 for the URI (values used by
337 * http_req_replace_stline()). On success, it returns ACT_RET_CONT. If an error
338 * occurs while soft rewrites are enabled, the action is canceled, but the rule
339 * processing continue. Otherwsize ACT_RET_ERR is returned.
Willy Tarreau33810222019-06-12 17:44:02 +0200340 */
341static enum act_return http_action_replace_uri(struct act_rule *rule, struct proxy *px,
342 struct session *sess, struct stream *s, int flags)
343{
Christopher Faulet13403762019-12-13 09:01:57 +0100344 enum act_return ret = ACT_RET_CONT;
Willy Tarreau33810222019-06-12 17:44:02 +0200345 struct buffer *replace, *output;
346 struct ist uri;
347 int len;
348
349 replace = alloc_trash_chunk();
350 output = alloc_trash_chunk();
351 if (!replace || !output)
Christopher Faulete00d06c2019-12-16 17:18:42 +0100352 goto fail_alloc;
Christopher Faulet12c28b62019-07-15 16:30:24 +0200353 uri = htx_sl_req_uri(http_get_stline(htxbuf(&s->req.buf)));
Willy Tarreau262c3f12019-12-17 06:52:51 +0100354
Christopher Faulet1fa0cc12020-09-02 11:10:38 +0200355 if (rule->action == 1) // replace-path
356 uri = iststop(http_get_path(uri), '?');
Christopher Faulet312294f2020-09-02 17:17:44 +0200357 else if (rule->action == 4) // replace-pathq
358 uri = http_get_path(uri);
Willy Tarreau262c3f12019-12-17 06:52:51 +0100359
Christopher Faulet96bff762019-12-17 13:46:18 +0100360 if (!regex_exec_match2(rule->arg.http.re, uri.ptr, uri.len, MAX_MATCH, pmatch, 0))
Willy Tarreau33810222019-06-12 17:44:02 +0200361 goto leave;
362
Christopher Faulet96bff762019-12-17 13:46:18 +0100363 replace->data = build_logline(s, replace->area, replace->size, &rule->arg.http.fmt);
Willy Tarreau33810222019-06-12 17:44:02 +0200364
365 /* note: uri.ptr doesn't need to be zero-terminated because it will
366 * only be used to pick pmatch references.
367 */
368 len = exp_replace(output->area, output->size, uri.ptr, replace->area, pmatch);
369 if (len == -1)
Christopher Faulete00d06c2019-12-16 17:18:42 +0100370 goto fail_rewrite;
Willy Tarreau33810222019-06-12 17:44:02 +0200371
Christopher Faulet2c22a692019-12-18 15:39:56 +0100372 if (http_req_replace_stline(rule->action, output->area, len, px, s) == -1)
Christopher Faulete00d06c2019-12-16 17:18:42 +0100373 goto fail_rewrite;
Willy Tarreau33810222019-06-12 17:44:02 +0200374
Christopher Faulete00d06c2019-12-16 17:18:42 +0100375 leave:
Willy Tarreau33810222019-06-12 17:44:02 +0200376 free_trash_chunk(output);
377 free_trash_chunk(replace);
378 return ret;
Christopher Faulete00d06c2019-12-16 17:18:42 +0100379
380 fail_alloc:
381 if (!(s->flags & SF_ERR_MASK))
382 s->flags |= SF_ERR_RESOURCE;
383 ret = ACT_RET_ERR;
384 goto leave;
385
386 fail_rewrite:
Willy Tarreau4781b152021-04-06 13:53:36 +0200387 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_rewrites);
Christopher Faulete00d06c2019-12-16 17:18:42 +0100388 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200389 _HA_ATOMIC_INC(&s->be->be_counters.failed_rewrites);
William Lallemand36119de2021-03-08 15:26:48 +0100390 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200391 _HA_ATOMIC_INC(&sess->listener->counters->failed_rewrites);
Christopher Faulete00d06c2019-12-16 17:18:42 +0100392 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +0200393 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_rewrites);
Christopher Faulete00d06c2019-12-16 17:18:42 +0100394
Christopher Faulet333bf8c2020-01-22 14:38:05 +0100395 if (!(s->txn->req.flags & HTTP_MSGF_SOFT_RW)) {
Christopher Faulete00d06c2019-12-16 17:18:42 +0100396 ret = ACT_RET_ERR;
Christopher Faulet333bf8c2020-01-22 14:38:05 +0100397 if (!(s->flags & SF_ERR_MASK))
398 s->flags |= SF_ERR_PRXCOND;
399 }
Christopher Faulete00d06c2019-12-16 17:18:42 +0100400 goto leave;
Willy Tarreau33810222019-06-12 17:44:02 +0200401}
402
Christopher Faulet312294f2020-09-02 17:17:44 +0200403/* parse a "replace-uri", "replace-path" or "replace-pathq"
404 * http-request action.
Willy Tarreau33810222019-06-12 17:44:02 +0200405 * This action takes 2 arguments (a regex and a replacement format string).
Christopher Faulet2c22a692019-12-18 15:39:56 +0100406 * The resulting rule makes use of <.action> to store the action (1/3 for now),
Christopher Faulet96bff762019-12-17 13:46:18 +0100407 * <http.re> to store the compiled regex, and <http.fmt> to store the log-format
Willy Tarreau33810222019-06-12 17:44:02 +0200408 * list head. It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
409 */
410static enum act_parse_ret parse_replace_uri(const char **args, int *orig_arg, struct proxy *px,
411 struct act_rule *rule, char **err)
412{
413 int cur_arg = *orig_arg;
414 char *error = NULL;
415
Christopher Faulet312294f2020-09-02 17:17:44 +0200416 switch (args[0][8]) {
417 case 'p':
418 if (args[0][12] == 'q')
419 rule->action = 4; // replace-pathq, same as set-pathq
420 else
421 rule->action = 1; // replace-path, same as set-path
422 break;
423 case 'u':
Christopher Faulet2c22a692019-12-18 15:39:56 +0100424 rule->action = 3; // replace-uri, same as set-uri
Christopher Faulet312294f2020-09-02 17:17:44 +0200425 break;
426 default:
427 memprintf(err, "internal error: unhandled action '%s'", args[0]);
428 return ACT_RET_PRS_ERR;
429 }
Willy Tarreau262c3f12019-12-17 06:52:51 +0100430
Willy Tarreau33810222019-06-12 17:44:02 +0200431 rule->action_ptr = http_action_replace_uri;
Christopher Faulet2eb53962020-01-14 14:47:34 +0100432 rule->release_ptr = release_http_action;
Willy Tarreau33810222019-06-12 17:44:02 +0200433
434 if (!*args[cur_arg] || !*args[cur_arg+1] ||
435 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
436 memprintf(err, "expects exactly 2 arguments <match-regex> and <replace-format>");
437 return ACT_RET_PRS_ERR;
438 }
439
Christopher Faulet96bff762019-12-17 13:46:18 +0100440 if (!(rule->arg.http.re = regex_comp(args[cur_arg], 1, 1, &error))) {
Willy Tarreau33810222019-06-12 17:44:02 +0200441 memprintf(err, "failed to parse the regex : %s", error);
442 free(error);
443 return ACT_RET_PRS_ERR;
444 }
445
Christopher Faulet96bff762019-12-17 13:46:18 +0100446 LIST_INIT(&rule->arg.http.fmt);
Willy Tarreau33810222019-06-12 17:44:02 +0200447 px->conf.args.ctx = ARGC_HRQ;
Christopher Faulet96bff762019-12-17 13:46:18 +0100448 if (!parse_logformat_string(args[cur_arg + 1], px, &rule->arg.http.fmt, LOG_OPT_HTTP,
Willy Tarreau33810222019-06-12 17:44:02 +0200449 (px->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, err)) {
Christopher Faulet1337b322020-01-14 14:50:55 +0100450 regex_free(rule->arg.http.re);
Willy Tarreau33810222019-06-12 17:44:02 +0200451 return ACT_RET_PRS_ERR;
452 }
453
454 (*orig_arg) += 2;
455 return ACT_RET_PRS_OK;
456}
457
Willy Tarreau79e57332018-10-02 16:01:16 +0200458/* This function is just a compliant action wrapper for "set-status". */
459static enum act_return action_http_set_status(struct act_rule *rule, struct proxy *px,
460 struct session *sess, struct stream *s, int flags)
461{
Christopher Faulet96bff762019-12-17 13:46:18 +0100462 if (http_res_set_status(rule->arg.http.i, rule->arg.http.str, s) == -1) {
Willy Tarreau4781b152021-04-06 13:53:36 +0200463 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_rewrites);
Christopher Faulete00d06c2019-12-16 17:18:42 +0100464 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200465 _HA_ATOMIC_INC(&s->be->be_counters.failed_rewrites);
William Lallemand36119de2021-03-08 15:26:48 +0100466 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200467 _HA_ATOMIC_INC(&sess->listener->counters->failed_rewrites);
Christopher Faulete00d06c2019-12-16 17:18:42 +0100468 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +0200469 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_rewrites);
Christopher Faulete00d06c2019-12-16 17:18:42 +0100470
Christopher Faulet333bf8c2020-01-22 14:38:05 +0100471 if (!(s->txn->req.flags & HTTP_MSGF_SOFT_RW)) {
Christopher Faulet333bf8c2020-01-22 14:38:05 +0100472 if (!(s->flags & SF_ERR_MASK))
473 s->flags |= SF_ERR_PRXCOND;
Christopher Faulet692a6c22020-02-07 10:22:31 +0100474 return ACT_RET_ERR;
Christopher Faulet333bf8c2020-01-22 14:38:05 +0100475 }
Christopher Faulete00d06c2019-12-16 17:18:42 +0100476 }
477
Willy Tarreau79e57332018-10-02 16:01:16 +0200478 return ACT_RET_CONT;
479}
480
481/* parse set-status action:
482 * This action accepts a single argument of type int representing
483 * an http status code. It returns ACT_RET_PRS_OK on success,
484 * ACT_RET_PRS_ERR on error.
485 */
486static enum act_parse_ret parse_http_set_status(const char **args, int *orig_arg, struct proxy *px,
487 struct act_rule *rule, char **err)
488{
489 char *error;
490
491 rule->action = ACT_CUSTOM;
492 rule->action_ptr = action_http_set_status;
Christopher Faulet2eb53962020-01-14 14:47:34 +0100493 rule->release_ptr = release_http_action;
Willy Tarreau79e57332018-10-02 16:01:16 +0200494
495 /* Check if an argument is available */
496 if (!*args[*orig_arg]) {
497 memprintf(err, "expects 1 argument: <status>; or 3 arguments: <status> reason <fmt>");
498 return ACT_RET_PRS_ERR;
499 }
500
501 /* convert status code as integer */
Christopher Faulet96bff762019-12-17 13:46:18 +0100502 rule->arg.http.i = strtol(args[*orig_arg], &error, 10);
503 if (*error != '\0' || rule->arg.http.i < 100 || rule->arg.http.i > 999) {
Willy Tarreau79e57332018-10-02 16:01:16 +0200504 memprintf(err, "expects an integer status code between 100 and 999");
505 return ACT_RET_PRS_ERR;
506 }
507
508 (*orig_arg)++;
509
510 /* set custom reason string */
Christopher Faulet96bff762019-12-17 13:46:18 +0100511 rule->arg.http.str = ist(NULL); // If null, we use the default reason for the status code.
Willy Tarreau79e57332018-10-02 16:01:16 +0200512 if (*args[*orig_arg] && strcmp(args[*orig_arg], "reason") == 0 &&
513 (*args[*orig_arg + 1] && strcmp(args[*orig_arg + 1], "if") != 0 && strcmp(args[*orig_arg + 1], "unless") != 0)) {
514 (*orig_arg)++;
Christopher Faulet96bff762019-12-17 13:46:18 +0100515 rule->arg.http.str.ptr = strdup(args[*orig_arg]);
516 rule->arg.http.str.len = strlen(rule->arg.http.str.ptr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200517 (*orig_arg)++;
518 }
519
Christopher Fauletc20b3712020-01-27 15:51:56 +0100520 LIST_INIT(&rule->arg.http.fmt);
Willy Tarreau79e57332018-10-02 16:01:16 +0200521 return ACT_RET_PRS_OK;
522}
523
524/* This function executes the "reject" HTTP action. It clears the request and
525 * response buffer without sending any response. It can be useful as an HTTP
526 * alternative to the silent-drop action to defend against DoS attacks, and may
527 * also be used with HTTP/2 to close a connection instead of just a stream.
528 * The txn status is unchanged, indicating no response was sent. The termination
Christopher Faulet90d22a82020-03-06 11:18:39 +0100529 * flags will indicate "PR". It always returns ACT_RET_ABRT.
Willy Tarreau79e57332018-10-02 16:01:16 +0200530 */
531static enum act_return http_action_reject(struct act_rule *rule, struct proxy *px,
532 struct session *sess, struct stream *s, int flags)
533{
Willy Tarreau0f9cd7b2019-01-31 19:02:43 +0100534 si_must_kill_conn(chn_prod(&s->req));
Willy Tarreau79e57332018-10-02 16:01:16 +0200535 channel_abort(&s->req);
536 channel_abort(&s->res);
Christopher Fauletd4a824e2020-03-06 15:07:09 +0100537 s->req.analysers &= AN_REQ_FLT_END;
538 s->res.analysers &= AN_RES_FLT_END;
Willy Tarreau79e57332018-10-02 16:01:16 +0200539
Willy Tarreau4781b152021-04-06 13:53:36 +0200540 _HA_ATOMIC_INC(&s->be->be_counters.denied_req);
541 _HA_ATOMIC_INC(&sess->fe->fe_counters.denied_req);
Willy Tarreau79e57332018-10-02 16:01:16 +0200542 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200543 _HA_ATOMIC_INC(&sess->listener->counters->denied_req);
Willy Tarreau79e57332018-10-02 16:01:16 +0200544
545 if (!(s->flags & SF_ERR_MASK))
546 s->flags |= SF_ERR_PRXCOND;
547 if (!(s->flags & SF_FINST_MASK))
548 s->flags |= SF_FINST_R;
549
Christopher Faulet90d22a82020-03-06 11:18:39 +0100550 return ACT_RET_ABRT;
Willy Tarreau79e57332018-10-02 16:01:16 +0200551}
552
553/* parse the "reject" action:
554 * This action takes no argument and returns ACT_RET_PRS_OK on success,
555 * ACT_RET_PRS_ERR on error.
556 */
557static enum act_parse_ret parse_http_action_reject(const char **args, int *orig_arg, struct proxy *px,
558 struct act_rule *rule, char **err)
559{
560 rule->action = ACT_CUSTOM;
561 rule->action_ptr = http_action_reject;
562 return ACT_RET_PRS_OK;
563}
564
Olivier Houchard602bf7d2019-05-10 13:59:15 +0200565/* This function executes the "disable-l7-retry" HTTP action.
566 * It disables L7 retries (all retry except for a connection failure). This
567 * can be useful for example to avoid retrying on POST requests.
568 * It just removes the L7 retry flag on the stream_interface, and always
569 * return ACT_RET_CONT;
570 */
571static enum act_return http_req_disable_l7_retry(struct act_rule *rule, struct proxy *px,
572 struct session *sess, struct stream *s, int flags)
573{
574 struct stream_interface *si = &s->si[1];
575
576 /* In theory, the SI_FL_L7_RETRY flags isn't set at this point, but
577 * let's be future-proof and remove it anyway.
578 */
579 si->flags &= ~SI_FL_L7_RETRY;
580 si->flags |= SI_FL_D_L7_RETRY;
581 return ACT_RET_CONT;
582}
583
584/* parse the "disable-l7-retry" action:
585 * This action takes no argument and returns ACT_RET_PRS_OK on success,
586 * ACT_RET_PRS_ERR on error.
587 */
588static enum act_parse_ret parse_http_req_disable_l7_retry(const char **args,
589 int *orig_args, struct proxy *px,
590 struct act_rule *rule, char **err)
591{
592 rule->action = ACT_CUSTOM;
593 rule->action_ptr = http_req_disable_l7_retry;
594 return ACT_RET_PRS_OK;
595}
596
Willy Tarreau79e57332018-10-02 16:01:16 +0200597/* This function executes the "capture" action. It executes a fetch expression,
598 * turns the result into a string and puts it in a capture slot. It always
599 * returns 1. If an error occurs the action is cancelled, but the rule
600 * processing continues.
601 */
602static enum act_return http_action_req_capture(struct act_rule *rule, struct proxy *px,
603 struct session *sess, struct stream *s, int flags)
604{
605 struct sample *key;
606 struct cap_hdr *h = rule->arg.cap.hdr;
607 char **cap = s->req_cap;
608 int len;
609
610 key = sample_fetch_as_type(s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.cap.expr, SMP_T_STR);
611 if (!key)
612 return ACT_RET_CONT;
613
614 if (cap[h->index] == NULL)
615 cap[h->index] = pool_alloc(h->pool);
616
617 if (cap[h->index] == NULL) /* no more capture memory */
618 return ACT_RET_CONT;
619
620 len = key->data.u.str.data;
621 if (len > h->len)
622 len = h->len;
623
624 memcpy(cap[h->index], key->data.u.str.area, len);
625 cap[h->index][len] = 0;
626 return ACT_RET_CONT;
627}
628
629/* This function executes the "capture" action and store the result in a
630 * capture slot if exists. It executes a fetch expression, turns the result
631 * into a string and puts it in a capture slot. It always returns 1. If an
632 * error occurs the action is cancelled, but the rule processing continues.
633 */
634static enum act_return http_action_req_capture_by_id(struct act_rule *rule, struct proxy *px,
635 struct session *sess, struct stream *s, int flags)
636{
637 struct sample *key;
638 struct cap_hdr *h;
639 char **cap = s->req_cap;
640 struct proxy *fe = strm_fe(s);
641 int len;
642 int i;
643
644 /* Look for the original configuration. */
645 for (h = fe->req_cap, i = fe->nb_req_cap - 1;
646 h != NULL && i != rule->arg.capid.idx ;
647 i--, h = h->next);
648 if (!h)
649 return ACT_RET_CONT;
650
651 key = sample_fetch_as_type(s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.capid.expr, SMP_T_STR);
652 if (!key)
653 return ACT_RET_CONT;
654
655 if (cap[h->index] == NULL)
656 cap[h->index] = pool_alloc(h->pool);
657
658 if (cap[h->index] == NULL) /* no more capture memory */
659 return ACT_RET_CONT;
660
661 len = key->data.u.str.data;
662 if (len > h->len)
663 len = h->len;
664
665 memcpy(cap[h->index], key->data.u.str.area, len);
666 cap[h->index][len] = 0;
667 return ACT_RET_CONT;
668}
669
670/* Check an "http-request capture" action.
671 *
672 * The function returns 1 in success case, otherwise, it returns 0 and err is
673 * filled.
674 */
675static int check_http_req_capture(struct act_rule *rule, struct proxy *px, char **err)
676{
677 if (rule->action_ptr != http_action_req_capture_by_id)
678 return 1;
679
Baptiste Assmann19a69b32020-01-16 14:34:22 +0100680 /* capture slots can only be declared in frontends, so we can't check their
681 * existence in backends at configuration parsing step
682 */
683 if (px->cap & PR_CAP_FE && rule->arg.capid.idx >= px->nb_req_cap) {
Willy Tarreau79e57332018-10-02 16:01:16 +0200684 memprintf(err, "unable to find capture id '%d' referenced by http-request capture rule",
685 rule->arg.capid.idx);
686 return 0;
687 }
688
689 return 1;
690}
691
Christopher Faulet2eb53962020-01-14 14:47:34 +0100692/* Release memory allocate by an http capture action */
693static void release_http_capture(struct act_rule *rule)
694{
695 if (rule->action_ptr == http_action_req_capture)
696 release_sample_expr(rule->arg.cap.expr);
697 else
698 release_sample_expr(rule->arg.capid.expr);
699}
700
Willy Tarreau79e57332018-10-02 16:01:16 +0200701/* parse an "http-request capture" action. It takes a single argument which is
702 * a sample fetch expression. It stores the expression into arg->act.p[0] and
703 * the allocated hdr_cap struct or the preallocated "id" into arg->act.p[1].
704 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
705 */
706static enum act_parse_ret parse_http_req_capture(const char **args, int *orig_arg, struct proxy *px,
707 struct act_rule *rule, char **err)
708{
709 struct sample_expr *expr;
710 struct cap_hdr *hdr;
711 int cur_arg;
712 int len = 0;
713
714 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
715 if (strcmp(args[cur_arg], "if") == 0 ||
716 strcmp(args[cur_arg], "unless") == 0)
717 break;
718
719 if (cur_arg < *orig_arg + 3) {
720 memprintf(err, "expects <expression> [ 'len' <length> | id <idx> ]");
721 return ACT_RET_PRS_ERR;
722 }
723
724 cur_arg = *orig_arg;
Willy Tarreaue3b57bf2020-02-14 16:50:14 +0100725 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args, NULL);
Willy Tarreau79e57332018-10-02 16:01:16 +0200726 if (!expr)
727 return ACT_RET_PRS_ERR;
728
729 if (!(expr->fetch->val & SMP_VAL_FE_HRQ_HDR)) {
730 memprintf(err,
731 "fetch method '%s' extracts information from '%s', none of which is available here",
732 args[cur_arg-1], sample_src_names(expr->fetch->use));
Christopher Faulet1337b322020-01-14 14:50:55 +0100733 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200734 return ACT_RET_PRS_ERR;
735 }
736
737 if (!args[cur_arg] || !*args[cur_arg]) {
738 memprintf(err, "expects 'len or 'id'");
Christopher Faulet1337b322020-01-14 14:50:55 +0100739 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200740 return ACT_RET_PRS_ERR;
741 }
742
743 if (strcmp(args[cur_arg], "len") == 0) {
744 cur_arg++;
745
746 if (!(px->cap & PR_CAP_FE)) {
747 memprintf(err, "proxy '%s' has no frontend capability", px->id);
Christopher Faulet1337b322020-01-14 14:50:55 +0100748 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200749 return ACT_RET_PRS_ERR;
750 }
751
752 px->conf.args.ctx = ARGC_CAP;
753
754 if (!args[cur_arg]) {
755 memprintf(err, "missing length value");
Christopher Faulet1337b322020-01-14 14:50:55 +0100756 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200757 return ACT_RET_PRS_ERR;
758 }
759 /* we copy the table name for now, it will be resolved later */
760 len = atoi(args[cur_arg]);
761 if (len <= 0) {
762 memprintf(err, "length must be > 0");
Christopher Faulet1337b322020-01-14 14:50:55 +0100763 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200764 return ACT_RET_PRS_ERR;
765 }
766 cur_arg++;
767
Willy Tarreau79e57332018-10-02 16:01:16 +0200768 hdr = calloc(1, sizeof(*hdr));
769 hdr->next = px->req_cap;
770 hdr->name = NULL; /* not a header capture */
771 hdr->namelen = 0;
772 hdr->len = len;
773 hdr->pool = create_pool("caphdr", hdr->len + 1, MEM_F_SHARED);
774 hdr->index = px->nb_req_cap++;
775
776 px->req_cap = hdr;
777 px->to_log |= LW_REQHDR;
778
779 rule->action = ACT_CUSTOM;
780 rule->action_ptr = http_action_req_capture;
Christopher Faulet2eb53962020-01-14 14:47:34 +0100781 rule->release_ptr = release_http_capture;
Willy Tarreau79e57332018-10-02 16:01:16 +0200782 rule->arg.cap.expr = expr;
783 rule->arg.cap.hdr = hdr;
784 }
785
786 else if (strcmp(args[cur_arg], "id") == 0) {
787 int id;
788 char *error;
789
790 cur_arg++;
791
792 if (!args[cur_arg]) {
793 memprintf(err, "missing id value");
Christopher Faulet1337b322020-01-14 14:50:55 +0100794 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200795 return ACT_RET_PRS_ERR;
796 }
797
798 id = strtol(args[cur_arg], &error, 10);
799 if (*error != '\0') {
800 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
Christopher Faulet1337b322020-01-14 14:50:55 +0100801 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200802 return ACT_RET_PRS_ERR;
803 }
804 cur_arg++;
805
806 px->conf.args.ctx = ARGC_CAP;
807
808 rule->action = ACT_CUSTOM;
809 rule->action_ptr = http_action_req_capture_by_id;
810 rule->check_ptr = check_http_req_capture;
Christopher Faulet2eb53962020-01-14 14:47:34 +0100811 rule->release_ptr = release_http_capture;
Willy Tarreau79e57332018-10-02 16:01:16 +0200812 rule->arg.capid.expr = expr;
813 rule->arg.capid.idx = id;
814 }
815
816 else {
817 memprintf(err, "expects 'len' or 'id', found '%s'", args[cur_arg]);
Christopher Faulet1337b322020-01-14 14:50:55 +0100818 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200819 return ACT_RET_PRS_ERR;
820 }
821
822 *orig_arg = cur_arg;
823 return ACT_RET_PRS_OK;
824}
825
826/* This function executes the "capture" action and store the result in a
827 * capture slot if exists. It executes a fetch expression, turns the result
828 * into a string and puts it in a capture slot. It always returns 1. If an
829 * error occurs the action is cancelled, but the rule processing continues.
830 */
831static enum act_return http_action_res_capture_by_id(struct act_rule *rule, struct proxy *px,
832 struct session *sess, struct stream *s, int flags)
833{
834 struct sample *key;
835 struct cap_hdr *h;
836 char **cap = s->res_cap;
837 struct proxy *fe = strm_fe(s);
838 int len;
839 int i;
840
841 /* Look for the original configuration. */
842 for (h = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
843 h != NULL && i != rule->arg.capid.idx ;
844 i--, h = h->next);
845 if (!h)
846 return ACT_RET_CONT;
847
848 key = sample_fetch_as_type(s->be, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL, rule->arg.capid.expr, SMP_T_STR);
849 if (!key)
850 return ACT_RET_CONT;
851
852 if (cap[h->index] == NULL)
853 cap[h->index] = pool_alloc(h->pool);
854
855 if (cap[h->index] == NULL) /* no more capture memory */
856 return ACT_RET_CONT;
857
858 len = key->data.u.str.data;
859 if (len > h->len)
860 len = h->len;
861
862 memcpy(cap[h->index], key->data.u.str.area, len);
863 cap[h->index][len] = 0;
864 return ACT_RET_CONT;
865}
866
867/* Check an "http-response capture" action.
868 *
869 * The function returns 1 in success case, otherwise, it returns 0 and err is
870 * filled.
871 */
872static int check_http_res_capture(struct act_rule *rule, struct proxy *px, char **err)
873{
874 if (rule->action_ptr != http_action_res_capture_by_id)
875 return 1;
876
Tim Duesterhusf3f4aa02020-07-03 13:43:42 +0200877 /* capture slots can only be declared in frontends, so we can't check their
878 * existence in backends at configuration parsing step
879 */
880 if (px->cap & PR_CAP_FE && rule->arg.capid.idx >= px->nb_rsp_cap) {
Willy Tarreau79e57332018-10-02 16:01:16 +0200881 memprintf(err, "unable to find capture id '%d' referenced by http-response capture rule",
882 rule->arg.capid.idx);
883 return 0;
884 }
885
886 return 1;
887}
888
889/* parse an "http-response capture" action. It takes a single argument which is
890 * a sample fetch expression. It stores the expression into arg->act.p[0] and
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -0700891 * the allocated hdr_cap struct of the preallocated id into arg->act.p[1].
Willy Tarreau79e57332018-10-02 16:01:16 +0200892 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
893 */
894static enum act_parse_ret parse_http_res_capture(const char **args, int *orig_arg, struct proxy *px,
895 struct act_rule *rule, char **err)
896{
897 struct sample_expr *expr;
898 int cur_arg;
899 int id;
900 char *error;
901
902 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
903 if (strcmp(args[cur_arg], "if") == 0 ||
904 strcmp(args[cur_arg], "unless") == 0)
905 break;
906
907 if (cur_arg < *orig_arg + 3) {
908 memprintf(err, "expects <expression> id <idx>");
909 return ACT_RET_PRS_ERR;
910 }
911
912 cur_arg = *orig_arg;
Willy Tarreaue3b57bf2020-02-14 16:50:14 +0100913 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args, NULL);
Willy Tarreau79e57332018-10-02 16:01:16 +0200914 if (!expr)
915 return ACT_RET_PRS_ERR;
916
917 if (!(expr->fetch->val & SMP_VAL_FE_HRS_HDR)) {
918 memprintf(err,
919 "fetch method '%s' extracts information from '%s', none of which is available here",
920 args[cur_arg-1], sample_src_names(expr->fetch->use));
Christopher Faulet1337b322020-01-14 14:50:55 +0100921 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200922 return ACT_RET_PRS_ERR;
923 }
924
925 if (!args[cur_arg] || !*args[cur_arg]) {
926 memprintf(err, "expects 'id'");
Christopher Faulet1337b322020-01-14 14:50:55 +0100927 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200928 return ACT_RET_PRS_ERR;
929 }
930
931 if (strcmp(args[cur_arg], "id") != 0) {
932 memprintf(err, "expects 'id', found '%s'", args[cur_arg]);
Christopher Faulet1337b322020-01-14 14:50:55 +0100933 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200934 return ACT_RET_PRS_ERR;
935 }
936
937 cur_arg++;
938
939 if (!args[cur_arg]) {
940 memprintf(err, "missing id value");
Christopher Faulet1337b322020-01-14 14:50:55 +0100941 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200942 return ACT_RET_PRS_ERR;
943 }
944
945 id = strtol(args[cur_arg], &error, 10);
946 if (*error != '\0') {
947 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
Christopher Faulet1337b322020-01-14 14:50:55 +0100948 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200949 return ACT_RET_PRS_ERR;
950 }
951 cur_arg++;
952
953 px->conf.args.ctx = ARGC_CAP;
954
955 rule->action = ACT_CUSTOM;
956 rule->action_ptr = http_action_res_capture_by_id;
957 rule->check_ptr = check_http_res_capture;
Christopher Faulet2eb53962020-01-14 14:47:34 +0100958 rule->release_ptr = release_http_capture;
Willy Tarreau79e57332018-10-02 16:01:16 +0200959 rule->arg.capid.expr = expr;
960 rule->arg.capid.idx = id;
961
962 *orig_arg = cur_arg;
963 return ACT_RET_PRS_OK;
964}
965
Christopher Faulet81e20172019-12-12 16:40:30 +0100966/* Parse a "allow" action for a request or a response rule. It takes no argument. It
967 * returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
968 */
969static enum act_parse_ret parse_http_allow(const char **args, int *orig_arg, struct proxy *px,
970 struct act_rule *rule, char **err)
971{
972 rule->action = ACT_ACTION_ALLOW;
Christopher Faulet245cf792019-12-18 14:58:12 +0100973 rule->flags |= ACT_FLAG_FINAL;
Christopher Faulet81e20172019-12-12 16:40:30 +0100974 return ACT_RET_PRS_OK;
975}
976
Christopher Faulete0fca292020-01-13 21:49:03 +0100977/* Parse "deny" or "tarpit" actions for a request rule or "deny" action for a
Christopher Faulet5cb513a2020-05-13 17:56:56 +0200978 * response rule. It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on
979 * error. It relies on http_parse_http_reply() to set
980 * <.arg.http_reply>.
Christopher Faulet81e20172019-12-12 16:40:30 +0100981 */
Christopher Faulete0fca292020-01-13 21:49:03 +0100982static enum act_parse_ret parse_http_deny(const char **args, int *orig_arg, struct proxy *px,
983 struct act_rule *rule, char **err)
Christopher Faulet81e20172019-12-12 16:40:30 +0100984{
Christopher Faulet5cb513a2020-05-13 17:56:56 +0200985 int default_status;
986 int cur_arg, arg = 0;
Christopher Faulet81e20172019-12-12 16:40:30 +0100987
988 cur_arg = *orig_arg;
Christopher Faulete0fca292020-01-13 21:49:03 +0100989 if (rule->from == ACT_F_HTTP_REQ) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100990 if (strcmp(args[cur_arg - 1], "tarpit") == 0) {
Christopher Faulete0fca292020-01-13 21:49:03 +0100991 rule->action = ACT_HTTP_REQ_TARPIT;
Christopher Faulet5cb513a2020-05-13 17:56:56 +0200992 default_status = 500;
Christopher Faulet81e20172019-12-12 16:40:30 +0100993 }
Christopher Faulete0fca292020-01-13 21:49:03 +0100994 else {
995 rule->action = ACT_ACTION_DENY;
Christopher Faulet5cb513a2020-05-13 17:56:56 +0200996 default_status = 403;
Christopher Faulet81e20172019-12-12 16:40:30 +0100997 }
Christopher Faulet81e20172019-12-12 16:40:30 +0100998 }
Christopher Faulete0fca292020-01-13 21:49:03 +0100999 else {
Christopher Faulet554c0eb2020-01-14 12:00:28 +01001000 rule->action = ACT_ACTION_DENY;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001001 default_status = 502;
Christopher Faulete0fca292020-01-13 21:49:03 +01001002 }
Christopher Faulet040c8cd2020-01-13 16:43:45 +01001003
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001004 /* If no args or only a deny_status specified, fallback on the legacy
1005 * mode and use default error files despite the fact that
1006 * default-errorfiles is not used. Otherwise, parse an http reply.
1007 */
Christopher Faulet040c8cd2020-01-13 16:43:45 +01001008
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001009 /* Prepare parsing of log-format strings */
1010 px->conf.args.ctx = ((rule->from == ACT_F_HTTP_REQ) ? ARGC_HRQ : ARGC_HRS);
Christopher Faulet554c0eb2020-01-14 12:00:28 +01001011
Christopher Faulet9467f182020-06-30 09:32:01 +02001012 if (!*(args[cur_arg]) || strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001013 rule->arg.http_reply = http_parse_http_reply((const char *[]){"default-errorfiles", ""}, &arg, px, default_status, err);
1014 goto end;
Christopher Faulet554c0eb2020-01-14 12:00:28 +01001015 }
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001016
1017 if (strcmp(args[cur_arg], "deny_status") == 0) {
Christopher Faulet9467f182020-06-30 09:32:01 +02001018 if (!*(args[cur_arg+2]) || strcmp(args[cur_arg+2], "if") == 0 || strcmp(args[cur_arg+2], "unless") == 0) {
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001019 rule->arg.http_reply = http_parse_http_reply((const char *[]){"status", args[cur_arg+1], "default-errorfiles", ""},
1020 &arg, px, default_status, err);
1021 *orig_arg += 2;
1022 goto end;
Christopher Faulet554c0eb2020-01-14 12:00:28 +01001023 }
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001024 args[cur_arg] += 5; /* skip "deny_" for the parsing */
Christopher Faulet554c0eb2020-01-14 12:00:28 +01001025 }
1026
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001027 rule->arg.http_reply = http_parse_http_reply(args, orig_arg, px, default_status, err);
Christopher Faulet554c0eb2020-01-14 12:00:28 +01001028
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001029 end:
1030 if (!rule->arg.http_reply)
1031 return ACT_RET_PRS_ERR;
1032
1033 rule->flags |= ACT_FLAG_FINAL;
1034 rule->check_ptr = check_act_http_reply;
1035 rule->release_ptr = release_act_http_reply;
Christopher Faulet81e20172019-12-12 16:40:30 +01001036 return ACT_RET_PRS_OK;
1037}
1038
Christopher Fauletb3048832020-05-27 15:26:43 +02001039
1040/* This function executes a auth action. It builds an 401/407 HTX message using
1041 * the corresponding proxy's error message. On success, it returns
1042 * ACT_RET_ABRT. If an error occurs ACT_RET_ERR is returned.
1043 */
1044static enum act_return http_action_auth(struct act_rule *rule, struct proxy *px,
1045 struct session *sess, struct stream *s, int flags)
1046{
1047 struct channel *req = &s->req;
1048 struct channel *res = &s->res;
1049 struct htx *htx = htx_from_buf(&res->buf);
1050 struct http_reply *reply;
1051 const char *auth_realm;
1052 struct http_hdr_ctx ctx;
1053 struct ist hdr;
1054
1055 /* Auth might be performed on regular http-req rules as well as on stats */
1056 auth_realm = rule->arg.http.str.ptr;
1057 if (!auth_realm) {
1058 if (px->uri_auth && s->current_rule_list == &px->uri_auth->http_req_rules)
1059 auth_realm = STATS_DEFAULT_REALM;
1060 else
1061 auth_realm = px->id;
1062 }
1063
1064 if (!(s->txn->flags & TX_USE_PX_CONN)) {
1065 s->txn->status = 401;
1066 hdr = ist("WWW-Authenticate");
1067 }
1068 else {
1069 s->txn->status = 407;
1070 hdr = ist("Proxy-Authenticate");
1071 }
1072 reply = http_error_message(s);
1073 channel_htx_truncate(res, htx);
1074
1075 if (chunk_printf(&trash, "Basic realm=\"%s\"", auth_realm) == -1)
1076 goto fail;
1077
1078 /* Write the generic 40x message */
1079 if (http_reply_to_htx(s, htx, reply) == -1)
1080 goto fail;
1081
1082 /* Remove all existing occurrences of the XXX-Authenticate header */
1083 ctx.blk = NULL;
1084 while (http_find_header(htx, hdr, &ctx, 1))
1085 http_remove_header(htx, &ctx);
1086
1087 /* Now a the right XXX-Authenticate header */
1088 if (!http_add_header(htx, hdr, ist2(b_orig(&trash), b_data(&trash))))
1089 goto fail;
1090
1091 /* Finally forward the reply */
1092 htx_to_buf(htx, &res->buf);
1093 if (!http_forward_proxy_resp(s, 1))
1094 goto fail;
1095
1096 /* Note: Only eval on the request */
1097 s->logs.tv_request = now;
1098 req->analysers &= AN_REQ_FLT_END;
1099
1100 if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Willy Tarreau4781b152021-04-06 13:53:36 +02001101 _HA_ATOMIC_INC(&s->sess->fe->fe_counters.intercepted_req);
Christopher Fauletb3048832020-05-27 15:26:43 +02001102
1103 if (!(s->flags & SF_ERR_MASK))
1104 s->flags |= SF_ERR_LOCAL;
1105 if (!(s->flags & SF_FINST_MASK))
1106 s->flags |= SF_FINST_R;
1107
1108 stream_inc_http_err_ctr(s);
1109 return ACT_RET_ABRT;
1110
1111 fail:
1112 /* If an error occurred, remove the incomplete HTTP response from the
1113 * buffer */
1114 channel_htx_truncate(res, htx);
1115 return ACT_RET_ERR;
1116}
1117
Christopher Faulet81e20172019-12-12 16:40:30 +01001118/* Parse a "auth" action. It may take 2 optional arguments to define a "realm"
1119 * parameter. It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
1120 */
1121static enum act_parse_ret parse_http_auth(const char **args, int *orig_arg, struct proxy *px,
1122 struct act_rule *rule, char **err)
1123{
1124 int cur_arg;
1125
Christopher Fauletb3048832020-05-27 15:26:43 +02001126 rule->action = ACT_CUSTOM;
Christopher Faulet245cf792019-12-18 14:58:12 +01001127 rule->flags |= ACT_FLAG_FINAL;
Christopher Fauletb3048832020-05-27 15:26:43 +02001128 rule->action_ptr = http_action_auth;
Christopher Faulet2eb53962020-01-14 14:47:34 +01001129 rule->release_ptr = release_http_action;
Christopher Faulet81e20172019-12-12 16:40:30 +01001130
1131 cur_arg = *orig_arg;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001132 if (strcmp(args[cur_arg], "realm") == 0) {
Christopher Faulet81e20172019-12-12 16:40:30 +01001133 cur_arg++;
1134 if (!*args[cur_arg]) {
1135 memprintf(err, "missing realm value.\n");
1136 return ACT_RET_PRS_ERR;
1137 }
Christopher Faulet96bff762019-12-17 13:46:18 +01001138 rule->arg.http.str.ptr = strdup(args[cur_arg]);
1139 rule->arg.http.str.len = strlen(rule->arg.http.str.ptr);
Christopher Faulet81e20172019-12-12 16:40:30 +01001140 cur_arg++;
1141 }
1142
Christopher Fauletc20b3712020-01-27 15:51:56 +01001143 LIST_INIT(&rule->arg.http.fmt);
Christopher Faulet81e20172019-12-12 16:40:30 +01001144 *orig_arg = cur_arg;
1145 return ACT_RET_PRS_OK;
1146}
1147
1148/* Parse a "set-nice" action. It takes the nice value as argument. It returns
1149 * ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
1150 */
1151static enum act_parse_ret parse_http_set_nice(const char **args, int *orig_arg, struct proxy *px,
1152 struct act_rule *rule, char **err)
1153{
1154 int cur_arg;
1155
1156 rule->action = ACT_HTTP_SET_NICE;
1157
1158 cur_arg = *orig_arg;
1159 if (!*args[cur_arg]) {
1160 memprintf(err, "expects exactly 1 argument (integer value)");
1161 return ACT_RET_PRS_ERR;
1162 }
Christopher Faulet96bff762019-12-17 13:46:18 +01001163 rule->arg.http.i = atoi(args[cur_arg]);
1164 if (rule->arg.http.i < -1024)
1165 rule->arg.http.i = -1024;
1166 else if (rule->arg.http.i > 1024)
1167 rule->arg.http.i = 1024;
Christopher Faulet81e20172019-12-12 16:40:30 +01001168
Christopher Fauletc20b3712020-01-27 15:51:56 +01001169 LIST_INIT(&rule->arg.http.fmt);
Christopher Faulet81e20172019-12-12 16:40:30 +01001170 *orig_arg = cur_arg + 1;
1171 return ACT_RET_PRS_OK;
1172}
1173
1174/* Parse a "set-tos" action. It takes the TOS value as argument. It returns
1175 * ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
1176 */
1177static enum act_parse_ret parse_http_set_tos(const char **args, int *orig_arg, struct proxy *px,
1178 struct act_rule *rule, char **err)
1179{
1180#ifdef IP_TOS
1181 char *endp;
1182 int cur_arg;
1183
1184 rule->action = ACT_HTTP_SET_TOS;
1185
1186 cur_arg = *orig_arg;
1187 if (!*args[cur_arg]) {
1188 memprintf(err, "expects exactly 1 argument (integer/hex value)");
1189 return ACT_RET_PRS_ERR;
1190 }
Christopher Faulet96bff762019-12-17 13:46:18 +01001191 rule->arg.http.i = strtol(args[cur_arg], &endp, 0);
Christopher Faulet81e20172019-12-12 16:40:30 +01001192 if (endp && *endp != '\0') {
1193 memprintf(err, "invalid character starting at '%s' (integer/hex value expected)", endp);
1194 return ACT_RET_PRS_ERR;
1195 }
1196
Christopher Fauletc20b3712020-01-27 15:51:56 +01001197 LIST_INIT(&rule->arg.http.fmt);
Christopher Faulet81e20172019-12-12 16:40:30 +01001198 *orig_arg = cur_arg + 1;
1199 return ACT_RET_PRS_OK;
1200#else
1201 memprintf(err, "not supported on this platform (IP_TOS undefined)");
1202 return ACT_RET_PRS_ERR;
1203#endif
1204}
1205
1206/* Parse a "set-mark" action. It takes the MARK value as argument. It returns
1207 * ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
1208 */
1209static enum act_parse_ret parse_http_set_mark(const char **args, int *orig_arg, struct proxy *px,
1210 struct act_rule *rule, char **err)
1211{
1212#ifdef SO_MARK
1213 char *endp;
1214 int cur_arg;
1215
1216 rule->action = ACT_HTTP_SET_MARK;
1217
1218 cur_arg = *orig_arg;
1219 if (!*args[cur_arg]) {
1220 memprintf(err, "expects exactly 1 argument (integer/hex value)");
1221 return ACT_RET_PRS_ERR;
1222 }
Christopher Faulet96bff762019-12-17 13:46:18 +01001223 rule->arg.http.i = strtoul(args[cur_arg], &endp, 0);
Christopher Faulet81e20172019-12-12 16:40:30 +01001224 if (endp && *endp != '\0') {
1225 memprintf(err, "invalid character starting at '%s' (integer/hex value expected)", endp);
1226 return ACT_RET_PRS_ERR;
1227 }
1228
Christopher Fauletc20b3712020-01-27 15:51:56 +01001229 LIST_INIT(&rule->arg.http.fmt);
Christopher Faulet81e20172019-12-12 16:40:30 +01001230 *orig_arg = cur_arg + 1;
1231 global.last_checks |= LSTCHK_NETADM;
1232 return ACT_RET_PRS_OK;
1233#else
1234 memprintf(err, "not supported on this platform (SO_MARK undefined)");
1235 return ACT_RET_PRS_ERR;
1236#endif
1237}
1238
1239/* Parse a "set-log-level" action. It takes the level value as argument. It
1240 * returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
1241 */
1242static enum act_parse_ret parse_http_set_log_level(const char **args, int *orig_arg, struct proxy *px,
1243 struct act_rule *rule, char **err)
1244{
1245 int cur_arg;
1246
1247 rule->action = ACT_HTTP_SET_LOGL;
1248
1249 cur_arg = *orig_arg;
1250 if (!*args[cur_arg]) {
1251 bad_log_level:
1252 memprintf(err, "expects exactly 1 argument (log level name or 'silent')");
1253 return ACT_RET_PRS_ERR;
1254 }
1255 if (strcmp(args[cur_arg], "silent") == 0)
Christopher Faulet96bff762019-12-17 13:46:18 +01001256 rule->arg.http.i = -1;
1257 else if ((rule->arg.http.i = get_log_level(args[cur_arg]) + 1) == 0)
Christopher Faulet81e20172019-12-12 16:40:30 +01001258 goto bad_log_level;
1259
Christopher Fauletc20b3712020-01-27 15:51:56 +01001260 LIST_INIT(&rule->arg.http.fmt);
Christopher Faulet81e20172019-12-12 16:40:30 +01001261 *orig_arg = cur_arg + 1;
1262 return ACT_RET_PRS_OK;
1263}
1264
Christopher Faulet91b3ec12020-01-17 22:30:06 +01001265/* This function executes a early-hint action. It adds an HTTP Early Hint HTTP
1266 * 103 response header with <.arg.http.str> name and with a value built
1267 * according to <.arg.http.fmt> log line format. If it is the first early-hint
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001268 * rule of series, the 103 response start-line is added first. At the end, if
Christopher Faulet91b3ec12020-01-17 22:30:06 +01001269 * the next rule is not an early-hint rule or if it is the last rule, the EOH
1270 * block is added to terminate the response. On success, it returns
1271 * ACT_RET_CONT. If an error occurs while soft rewrites are enabled, the action
1272 * is canceled, but the rule processing continue. Otherwsize ACT_RET_ERR is
1273 * returned.
1274 */
1275static enum act_return http_action_early_hint(struct act_rule *rule, struct proxy *px,
1276 struct session *sess, struct stream *s, int flags)
1277{
1278 struct act_rule *prev_rule, *next_rule;
1279 struct channel *res = &s->res;
1280 struct htx *htx = htx_from_buf(&res->buf);
1281 struct buffer *value = alloc_trash_chunk();
1282 enum act_return ret = ACT_RET_CONT;
1283
1284 if (!(s->txn->req.flags & HTTP_MSGF_VER_11))
1285 goto leave;
1286
1287 if (!value) {
1288 if (!(s->flags & SF_ERR_MASK))
1289 s->flags |= SF_ERR_RESOURCE;
1290 goto error;
1291 }
1292
1293 /* get previous and next rules */
1294 prev_rule = LIST_PREV(&rule->list, typeof(rule), list);
1295 next_rule = LIST_NEXT(&rule->list, typeof(rule), list);
1296
1297 /* if no previous rule or previous rule is not early-hint, start a new response. Otherwise,
1298 * continue to add link to a previously started response */
1299 if (&prev_rule->list == s->current_rule_list || prev_rule->action_ptr != http_action_early_hint) {
1300 struct htx_sl *sl;
1301 unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|
1302 HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
1303
1304 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
1305 ist("HTTP/1.1"), ist("103"), ist("Early Hints"));
1306 if (!sl)
1307 goto error;
1308 sl->info.res.status = 103;
1309 }
1310
1311 /* Add the HTTP Early Hint HTTP 103 response heade */
1312 value->data = build_logline(s, b_tail(value), b_room(value), &rule->arg.http.fmt);
1313 if (!htx_add_header(htx, rule->arg.http.str, ist2(b_head(value), b_data(value))))
1314 goto error;
1315
1316 /* if it is the last rule or the next one is not an early-hint, terminate the current
1317 * response. */
1318 if (&next_rule->list == s->current_rule_list || next_rule->action_ptr != http_action_early_hint) {
Christopher Faulet91b3ec12020-01-17 22:30:06 +01001319 if (!htx_add_endof(htx, HTX_BLK_EOH)) {
1320 /* If an error occurred during an Early-hint rule,
1321 * remove the incomplete HTTP 103 response from the
1322 * buffer */
1323 goto error;
1324 }
1325
Christopher Fauleta72a7e42020-01-28 09:28:11 +01001326 if (!http_forward_proxy_resp(s, 0))
1327 goto error;
Christopher Faulet91b3ec12020-01-17 22:30:06 +01001328 }
1329
1330 leave:
1331 free_trash_chunk(value);
1332 return ret;
1333
1334 error:
1335 /* If an error occurred during an Early-hint rule, remove the incomplete
1336 * HTTP 103 response from the buffer */
1337 channel_htx_truncate(res, htx);
1338 ret = ACT_RET_ERR;
1339 goto leave;
1340}
1341
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001342/* This function executes a set-header or add-header actions. It builds a string
1343 * in the trash from the specified format string. It finds the action to be
1344 * performed in <.action>, previously filled by function parse_set_header(). The
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001345 * replacement action is executed by the function http_action_set_header(). On
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001346 * success, it returns ACT_RET_CONT. If an error occurs while soft rewrites are
1347 * enabled, the action is canceled, but the rule processing continue. Otherwsize
1348 * ACT_RET_ERR is returned.
1349 */
1350static enum act_return http_action_set_header(struct act_rule *rule, struct proxy *px,
1351 struct session *sess, struct stream *s, int flags)
1352{
Christopher Faulet91e31d82020-01-24 15:37:13 +01001353 struct http_msg *msg = ((rule->from == ACT_F_HTTP_REQ) ? &s->txn->req : &s->txn->rsp);
1354 struct htx *htx = htxbuf(&msg->chn->buf);
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001355 enum act_return ret = ACT_RET_CONT;
1356 struct buffer *replace;
1357 struct http_hdr_ctx ctx;
1358 struct ist n, v;
1359
1360 replace = alloc_trash_chunk();
1361 if (!replace)
1362 goto fail_alloc;
1363
1364 replace->data = build_logline(s, replace->area, replace->size, &rule->arg.http.fmt);
1365 n = rule->arg.http.str;
1366 v = ist2(replace->area, replace->data);
1367
1368 if (rule->action == 0) { // set-header
1369 /* remove all occurrences of the header */
1370 ctx.blk = NULL;
1371 while (http_find_header(htx, n, &ctx, 1))
1372 http_remove_header(htx, &ctx);
1373 }
1374
1375 /* Now add header */
1376 if (!http_add_header(htx, n, v))
1377 goto fail_rewrite;
1378
1379 leave:
1380 free_trash_chunk(replace);
1381 return ret;
1382
1383 fail_alloc:
1384 if (!(s->flags & SF_ERR_MASK))
1385 s->flags |= SF_ERR_RESOURCE;
1386 ret = ACT_RET_ERR;
1387 goto leave;
1388
1389 fail_rewrite:
Willy Tarreau4781b152021-04-06 13:53:36 +02001390 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_rewrites);
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001391 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +02001392 _HA_ATOMIC_INC(&s->be->be_counters.failed_rewrites);
William Lallemand36119de2021-03-08 15:26:48 +01001393 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001394 _HA_ATOMIC_INC(&sess->listener->counters->failed_rewrites);
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001395 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001396 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_rewrites);
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001397
Christopher Faulet333bf8c2020-01-22 14:38:05 +01001398 if (!(msg->flags & HTTP_MSGF_SOFT_RW)) {
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001399 ret = ACT_RET_ERR;
Christopher Faulet333bf8c2020-01-22 14:38:05 +01001400 if (!(s->flags & SF_ERR_MASK))
1401 s->flags |= SF_ERR_PRXCOND;
1402 }
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001403 goto leave;
1404}
1405
Christopher Faulet81e20172019-12-12 16:40:30 +01001406/* Parse a "set-header", "add-header" or "early-hint" actions. It takes an
1407 * header name and a log-format string as arguments. It returns ACT_RET_PRS_OK
1408 * on success, ACT_RET_PRS_ERR on error.
1409 *
1410 * Note: same function is used for the request and the response. However
1411 * "early-hint" rules are only supported for request rules.
1412 */
1413static enum act_parse_ret parse_http_set_header(const char **args, int *orig_arg, struct proxy *px,
1414 struct act_rule *rule, char **err)
1415{
Christopher Faulet81e20172019-12-12 16:40:30 +01001416 int cap, cur_arg;
1417
Christopher Faulet91b3ec12020-01-17 22:30:06 +01001418 if (args[*orig_arg-1][0] == 'e') {
1419 rule->action = ACT_CUSTOM;
1420 rule->action_ptr = http_action_early_hint;
1421 }
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001422 else {
1423 if (args[*orig_arg-1][0] == 's')
1424 rule->action = 0; // set-header
1425 else
1426 rule->action = 1; // add-header
1427 rule->action_ptr = http_action_set_header;
1428 }
Christopher Faulet2eb53962020-01-14 14:47:34 +01001429 rule->release_ptr = release_http_action;
Christopher Faulet81e20172019-12-12 16:40:30 +01001430
1431 cur_arg = *orig_arg;
1432 if (!*args[cur_arg] || !*args[cur_arg+1]) {
1433 memprintf(err, "expects exactly 2 arguments");
1434 return ACT_RET_PRS_ERR;
1435 }
1436
Christopher Faulet81e20172019-12-12 16:40:30 +01001437
Christopher Faulet96bff762019-12-17 13:46:18 +01001438 rule->arg.http.str.ptr = strdup(args[cur_arg]);
1439 rule->arg.http.str.len = strlen(rule->arg.http.str.ptr);
1440 LIST_INIT(&rule->arg.http.fmt);
Christopher Faulet81e20172019-12-12 16:40:30 +01001441
1442 if (rule->from == ACT_F_HTTP_REQ) {
1443 px->conf.args.ctx = ARGC_HRQ;
1444 cap = (px->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR;
1445 }
1446 else{
1447 px->conf.args.ctx = ARGC_HRS;
1448 cap = (px->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR;
1449 }
1450
1451 cur_arg++;
Christopher Faulet1337b322020-01-14 14:50:55 +01001452 if (!parse_logformat_string(args[cur_arg], px, &rule->arg.http.fmt, LOG_OPT_HTTP, cap, err)) {
Tim Duesterhused526372020-03-05 17:56:33 +01001453 istfree(&rule->arg.http.str);
Christopher Faulet81e20172019-12-12 16:40:30 +01001454 return ACT_RET_PRS_ERR;
Christopher Faulet1337b322020-01-14 14:50:55 +01001455 }
Christopher Faulet81e20172019-12-12 16:40:30 +01001456
1457 free(px->conf.lfs_file);
1458 px->conf.lfs_file = strdup(px->conf.args.file);
1459 px->conf.lfs_line = px->conf.args.line;
1460
1461 *orig_arg = cur_arg + 1;
1462 return ACT_RET_PRS_OK;
1463}
1464
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001465/* This function executes a replace-header or replace-value actions. It
1466 * builds a string in the trash from the specified format string. It finds
1467 * the action to be performed in <.action>, previously filled by function
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001468 * parse_replace_header(). The replacement action is executed by the function
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001469 * http_action_replace_header(). On success, it returns ACT_RET_CONT. If an error
1470 * occurs while soft rewrites are enabled, the action is canceled, but the rule
1471 * processing continue. Otherwsize ACT_RET_ERR is returned.
1472 */
1473static enum act_return http_action_replace_header(struct act_rule *rule, struct proxy *px,
1474 struct session *sess, struct stream *s, int flags)
1475{
Christopher Faulet91e31d82020-01-24 15:37:13 +01001476 struct http_msg *msg = ((rule->from == ACT_F_HTTP_REQ) ? &s->txn->req : &s->txn->rsp);
1477 struct htx *htx = htxbuf(&msg->chn->buf);
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001478 enum act_return ret = ACT_RET_CONT;
1479 struct buffer *replace;
1480 int r;
1481
1482 replace = alloc_trash_chunk();
1483 if (!replace)
1484 goto fail_alloc;
1485
1486 replace->data = build_logline(s, replace->area, replace->size, &rule->arg.http.fmt);
1487
1488 r = http_replace_hdrs(s, htx, rule->arg.http.str, replace->area, rule->arg.http.re, (rule->action == 0));
1489 if (r == -1)
1490 goto fail_rewrite;
1491
1492 leave:
1493 free_trash_chunk(replace);
1494 return ret;
1495
1496 fail_alloc:
1497 if (!(s->flags & SF_ERR_MASK))
1498 s->flags |= SF_ERR_RESOURCE;
1499 ret = ACT_RET_ERR;
1500 goto leave;
1501
1502 fail_rewrite:
Willy Tarreau4781b152021-04-06 13:53:36 +02001503 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_rewrites);
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001504 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +02001505 _HA_ATOMIC_INC(&s->be->be_counters.failed_rewrites);
William Lallemand36119de2021-03-08 15:26:48 +01001506 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001507 _HA_ATOMIC_INC(&sess->listener->counters->failed_rewrites);
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001508 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001509 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_rewrites);
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001510
Christopher Faulet333bf8c2020-01-22 14:38:05 +01001511 if (!(msg->flags & HTTP_MSGF_SOFT_RW)) {
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001512 ret = ACT_RET_ERR;
Christopher Faulet333bf8c2020-01-22 14:38:05 +01001513 if (!(s->flags & SF_ERR_MASK))
1514 s->flags |= SF_ERR_PRXCOND;
1515 }
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001516 goto leave;
1517}
1518
Christopher Faulet81e20172019-12-12 16:40:30 +01001519/* Parse a "replace-header" or "replace-value" actions. It takes an header name,
1520 * a regex and replacement string as arguments. It returns ACT_RET_PRS_OK on
1521 * success, ACT_RET_PRS_ERR on error.
1522 */
1523static enum act_parse_ret parse_http_replace_header(const char **args, int *orig_arg, struct proxy *px,
1524 struct act_rule *rule, char **err)
1525{
1526 int cap, cur_arg;
1527
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001528 if (args[*orig_arg-1][8] == 'h')
1529 rule->action = 0; // replace-header
1530 else
1531 rule->action = 1; // replace-value
1532 rule->action_ptr = http_action_replace_header;
Christopher Faulet2eb53962020-01-14 14:47:34 +01001533 rule->release_ptr = release_http_action;
Christopher Faulet81e20172019-12-12 16:40:30 +01001534
1535 cur_arg = *orig_arg;
1536 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2]) {
1537 memprintf(err, "expects exactly 3 arguments");
1538 return ACT_RET_PRS_ERR;
1539 }
1540
Christopher Faulet96bff762019-12-17 13:46:18 +01001541 rule->arg.http.str.ptr = strdup(args[cur_arg]);
1542 rule->arg.http.str.len = strlen(rule->arg.http.str.ptr);
1543 LIST_INIT(&rule->arg.http.fmt);
Christopher Faulet81e20172019-12-12 16:40:30 +01001544
1545 cur_arg++;
Christopher Faulet1337b322020-01-14 14:50:55 +01001546 if (!(rule->arg.http.re = regex_comp(args[cur_arg], 1, 1, err))) {
Tim Duesterhused526372020-03-05 17:56:33 +01001547 istfree(&rule->arg.http.str);
Christopher Faulet81e20172019-12-12 16:40:30 +01001548 return ACT_RET_PRS_ERR;
Christopher Faulet1337b322020-01-14 14:50:55 +01001549 }
Christopher Faulet81e20172019-12-12 16:40:30 +01001550
1551 if (rule->from == ACT_F_HTTP_REQ) {
1552 px->conf.args.ctx = ARGC_HRQ;
1553 cap = (px->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR;
1554 }
1555 else{
1556 px->conf.args.ctx = ARGC_HRS;
1557 cap = (px->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR;
1558 }
1559
1560 cur_arg++;
Christopher Faulet1337b322020-01-14 14:50:55 +01001561 if (!parse_logformat_string(args[cur_arg], px, &rule->arg.http.fmt, LOG_OPT_HTTP, cap, err)) {
Tim Duesterhused526372020-03-05 17:56:33 +01001562 istfree(&rule->arg.http.str);
Christopher Faulet1337b322020-01-14 14:50:55 +01001563 regex_free(rule->arg.http.re);
Christopher Faulet81e20172019-12-12 16:40:30 +01001564 return ACT_RET_PRS_ERR;
Christopher Faulet1337b322020-01-14 14:50:55 +01001565 }
Christopher Faulet81e20172019-12-12 16:40:30 +01001566
1567 free(px->conf.lfs_file);
1568 px->conf.lfs_file = strdup(px->conf.args.file);
1569 px->conf.lfs_line = px->conf.args.line;
1570
1571 *orig_arg = cur_arg + 1;
1572 return ACT_RET_PRS_OK;
1573}
1574
Maciej Zdebebdd4c52020-11-20 13:58:48 +00001575/* This function executes a del-header action with selected matching mode for
1576 * header name. It finds the matching method to be performed in <.action>, previously
1577 * filled by function parse_http_del_header(). On success, it returns ACT_RET_CONT.
1578 * Otherwise ACT_RET_ERR is returned.
1579 */
1580static enum act_return http_action_del_header(struct act_rule *rule, struct proxy *px,
1581 struct session *sess, struct stream *s, int flags)
1582{
1583 struct http_hdr_ctx ctx;
1584 struct http_msg *msg = ((rule->from == ACT_F_HTTP_REQ) ? &s->txn->req : &s->txn->rsp);
1585 struct htx *htx = htxbuf(&msg->chn->buf);
1586 enum act_return ret = ACT_RET_CONT;
1587
1588 /* remove all occurrences of the header */
1589 ctx.blk = NULL;
1590 switch (rule->action) {
1591 case PAT_MATCH_STR:
1592 while (http_find_header(htx, rule->arg.http.str, &ctx, 1))
1593 http_remove_header(htx, &ctx);
1594 break;
1595 case PAT_MATCH_BEG:
1596 while (http_find_pfx_header(htx, rule->arg.http.str, &ctx, 1))
1597 http_remove_header(htx, &ctx);
1598 break;
1599 case PAT_MATCH_END:
1600 while (http_find_sfx_header(htx, rule->arg.http.str, &ctx, 1))
1601 http_remove_header(htx, &ctx);
1602 break;
1603 case PAT_MATCH_SUB:
1604 while (http_find_sub_header(htx, rule->arg.http.str, &ctx, 1))
1605 http_remove_header(htx, &ctx);
1606 break;
1607 case PAT_MATCH_REG:
1608 while (http_match_header(htx, rule->arg.http.re, &ctx, 1))
1609 http_remove_header(htx, &ctx);
1610 break;
1611 default:
1612 return ACT_RET_ERR;
1613 }
1614 return ret;
1615}
1616
1617/* Parse a "del-header" action. It takes string as a required argument,
1618 * optional flag (currently only -m) and optional matching method of input string
1619 * with header name to be deleted. Default matching method is exact match (-m str).
1620 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Christopher Faulet81e20172019-12-12 16:40:30 +01001621 */
1622static enum act_parse_ret parse_http_del_header(const char **args, int *orig_arg, struct proxy *px,
1623 struct act_rule *rule, char **err)
1624{
1625 int cur_arg;
Maciej Zdebebdd4c52020-11-20 13:58:48 +00001626 int pat_idx;
Christopher Faulet81e20172019-12-12 16:40:30 +01001627
Maciej Zdebebdd4c52020-11-20 13:58:48 +00001628 /* set exact matching (-m str) as default */
1629 rule->action = PAT_MATCH_STR;
1630 rule->action_ptr = http_action_del_header;
Christopher Faulet2eb53962020-01-14 14:47:34 +01001631 rule->release_ptr = release_http_action;
Christopher Faulet81e20172019-12-12 16:40:30 +01001632
1633 cur_arg = *orig_arg;
1634 if (!*args[cur_arg]) {
Maciej Zdebebdd4c52020-11-20 13:58:48 +00001635 memprintf(err, "expects at least 1 argument");
Christopher Faulet81e20172019-12-12 16:40:30 +01001636 return ACT_RET_PRS_ERR;
1637 }
1638
Christopher Faulet96bff762019-12-17 13:46:18 +01001639 rule->arg.http.str.ptr = strdup(args[cur_arg]);
1640 rule->arg.http.str.len = strlen(rule->arg.http.str.ptr);
Christopher Faulet81e20172019-12-12 16:40:30 +01001641 px->conf.args.ctx = (rule->from == ACT_F_HTTP_REQ ? ARGC_HRQ : ARGC_HRS);
1642
Maciej Zdeb6dee9962020-11-23 16:03:09 +00001643 LIST_INIT(&rule->arg.http.fmt);
Maciej Zdebebdd4c52020-11-20 13:58:48 +00001644 if (strcmp(args[cur_arg+1], "-m") == 0) {
1645 cur_arg++;
1646 if (!*args[cur_arg+1]) {
1647 memprintf(err, "-m flag expects exactly 1 argument");
1648 return ACT_RET_PRS_ERR;
1649 }
1650
1651 cur_arg++;
1652 pat_idx = pat_find_match_name(args[cur_arg]);
1653 switch (pat_idx) {
1654 case PAT_MATCH_REG:
1655 if (!(rule->arg.http.re = regex_comp(rule->arg.http.str.ptr, 1, 1, err)))
1656 return ACT_RET_PRS_ERR;
1657 /* fall through */
1658 case PAT_MATCH_STR:
1659 case PAT_MATCH_BEG:
1660 case PAT_MATCH_END:
1661 case PAT_MATCH_SUB:
1662 rule->action = pat_idx;
1663 break;
1664 default:
1665 memprintf(err, "-m with unsupported matching method '%s'", args[cur_arg]);
1666 return ACT_RET_PRS_ERR;
1667 }
1668 }
1669
Christopher Faulet81e20172019-12-12 16:40:30 +01001670 *orig_arg = cur_arg + 1;
1671 return ACT_RET_PRS_OK;
1672}
1673
Christopher Faulet2eb53962020-01-14 14:47:34 +01001674/* Release memory allocated by an http redirect action. */
1675static void release_http_redir(struct act_rule *rule)
1676{
1677 struct logformat_node *lf, *lfb;
1678 struct redirect_rule *redir;
1679
1680 redir = rule->arg.redir;
1681 LIST_DEL(&redir->list);
1682 if (redir->cond) {
1683 prune_acl_cond(redir->cond);
1684 free(redir->cond);
1685 }
1686 free(redir->rdr_str);
1687 free(redir->cookie_str);
1688 list_for_each_entry_safe(lf, lfb, &redir->rdr_fmt, list) {
1689 LIST_DEL(&lf->list);
1690 free(lf);
1691 }
1692 free(redir);
1693}
1694
Christopher Faulet81e20172019-12-12 16:40:30 +01001695/* Parse a "redirect" action. It returns ACT_RET_PRS_OK on success,
1696 * ACT_RET_PRS_ERR on error.
1697 */
1698static enum act_parse_ret parse_http_redirect(const char **args, int *orig_arg, struct proxy *px,
1699 struct act_rule *rule, char **err)
1700{
1701 struct redirect_rule *redir;
1702 int dir, cur_arg;
1703
1704 rule->action = ACT_HTTP_REDIR;
Christopher Faulet245cf792019-12-18 14:58:12 +01001705 rule->flags |= ACT_FLAG_FINAL;
Christopher Faulet2eb53962020-01-14 14:47:34 +01001706 rule->release_ptr = release_http_redir;
Christopher Faulet81e20172019-12-12 16:40:30 +01001707
1708 cur_arg = *orig_arg;
1709
1710 dir = (rule->from == ACT_F_HTTP_REQ ? 0 : 1);
1711 if ((redir = http_parse_redirect_rule(px->conf.args.file, px->conf.args.line, px, &args[cur_arg], err, 1, dir)) == NULL)
1712 return ACT_RET_PRS_ERR;
1713
1714 rule->arg.redir = redir;
1715 rule->cond = redir->cond;
1716 redir->cond = NULL;
1717
1718 /* skip all arguments */
1719 while (*args[cur_arg])
1720 cur_arg++;
1721
1722 *orig_arg = cur_arg;
1723 return ACT_RET_PRS_OK;
1724}
1725
Christopher Faulet046cf442019-12-17 15:45:23 +01001726/* This function executes a add-acl, del-acl, set-map or del-map actions. On
1727 * success, it returns ACT_RET_CONT. Otherwsize ACT_RET_ERR is returned.
1728 */
1729static enum act_return http_action_set_map(struct act_rule *rule, struct proxy *px,
1730 struct session *sess, struct stream *s, int flags)
1731{
1732 struct pat_ref *ref;
1733 struct buffer *key = NULL, *value = NULL;
1734 enum act_return ret = ACT_RET_CONT;
1735
1736 /* collect reference */
1737 ref = pat_ref_lookup(rule->arg.map.ref);
1738 if (!ref)
1739 goto leave;
1740
1741 /* allocate key */
1742 key = alloc_trash_chunk();
1743 if (!key)
1744 goto fail_alloc;
1745
1746 /* collect key */
1747 key->data = build_logline(s, key->area, key->size, &rule->arg.map.key);
1748 key->area[key->data] = '\0';
1749
1750 switch (rule->action) {
1751 case 0: // add-acl
1752 /* add entry only if it does not already exist */
1753 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
1754 if (pat_ref_find_elt(ref, key->area) == NULL)
1755 pat_ref_add(ref, key->area, NULL, NULL);
1756 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
1757 break;
1758
1759 case 1: // set-map
1760 /* allocate value */
1761 value = alloc_trash_chunk();
1762 if (!value)
1763 goto fail_alloc;
1764
1765 /* collect value */
1766 value->data = build_logline(s, value->area, value->size, &rule->arg.map.value);
1767 value->area[value->data] = '\0';
1768
1769 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
1770 if (pat_ref_find_elt(ref, key->area) != NULL) {
1771 /* update entry if it exists */
1772 pat_ref_set(ref, key->area, value->area, NULL);
1773 }
1774 else {
1775 /* insert a new entry */
1776 pat_ref_add(ref, key->area, value->area, NULL);
1777 }
1778 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
1779 break;
1780
1781 case 2: // del-acl
1782 case 3: // del-map
1783 /* returned code: 1=ok, 0=ko */
1784 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
1785 pat_ref_delete(ref, key->area);
1786 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
1787 break;
1788
1789 default:
1790 ret = ACT_RET_ERR;
1791 }
1792
1793
1794 leave:
1795 free_trash_chunk(key);
1796 free_trash_chunk(value);
1797 return ret;
1798
1799 fail_alloc:
1800 if (!(s->flags & SF_ERR_MASK))
1801 s->flags |= SF_ERR_RESOURCE;
1802 ret = ACT_RET_ERR;
1803 goto leave;
1804}
1805
Christopher Faulet2eb53962020-01-14 14:47:34 +01001806/* Release memory allocated by an http map/acl action. */
1807static void release_http_map(struct act_rule *rule)
1808{
1809 struct logformat_node *lf, *lfb;
1810
1811 free(rule->arg.map.ref);
1812 list_for_each_entry_safe(lf, lfb, &rule->arg.map.key, list) {
1813 LIST_DEL(&lf->list);
1814 release_sample_expr(lf->expr);
1815 free(lf->arg);
1816 free(lf);
1817 }
1818 if (rule->action == 1) {
1819 list_for_each_entry_safe(lf, lfb, &rule->arg.map.value, list) {
1820 LIST_DEL(&lf->list);
1821 release_sample_expr(lf->expr);
1822 free(lf->arg);
1823 free(lf);
1824 }
1825 }
1826}
1827
Christopher Faulet81e20172019-12-12 16:40:30 +01001828/* Parse a "add-acl", "del-acl", "set-map" or "del-map" actions. It takes one or
Christopher Faulet046cf442019-12-17 15:45:23 +01001829 * two log-format string as argument depending on the action. The action is
1830 * stored in <.action> as an int (0=add-acl, 1=set-map, 2=del-acl,
1831 * 3=del-map). It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Christopher Faulet81e20172019-12-12 16:40:30 +01001832 */
1833static enum act_parse_ret parse_http_set_map(const char **args, int *orig_arg, struct proxy *px,
1834 struct act_rule *rule, char **err)
1835{
1836 int cap, cur_arg;
1837
Christopher Faulet046cf442019-12-17 15:45:23 +01001838 if (args[*orig_arg-1][0] == 'a') // add-acl
1839 rule->action = 0;
1840 else if (args[*orig_arg-1][0] == 's') // set-map
1841 rule->action = 1;
1842 else if (args[*orig_arg-1][4] == 'a') // del-acl
1843 rule->action = 2;
1844 else if (args[*orig_arg-1][4] == 'm') // del-map
1845 rule->action = 3;
1846 else {
1847 memprintf(err, "internal error: unhandled action '%s'", args[0]);
1848 return ACT_RET_PRS_ERR;
1849 }
1850 rule->action_ptr = http_action_set_map;
Christopher Faulet2eb53962020-01-14 14:47:34 +01001851 rule->release_ptr = release_http_map;
Christopher Faulet81e20172019-12-12 16:40:30 +01001852
1853 cur_arg = *orig_arg;
Christopher Faulet046cf442019-12-17 15:45:23 +01001854 if (rule->action == 1 && (!*args[cur_arg] || !*args[cur_arg+1])) {
1855 /* 2 args for set-map */
Christopher Faulet81e20172019-12-12 16:40:30 +01001856 memprintf(err, "expects exactly 2 arguments");
1857 return ACT_RET_PRS_ERR;
1858 }
1859 else if (!*args[cur_arg]) {
Christopher Faulet046cf442019-12-17 15:45:23 +01001860 /* only one arg for other actions */
Christopher Faulet81e20172019-12-12 16:40:30 +01001861 memprintf(err, "expects exactly 1 arguments");
1862 return ACT_RET_PRS_ERR;
1863 }
1864
1865 /*
1866 * '+ 8' for 'set-map(' (same for del-map)
1867 * '- 9' for 'set-map(' + trailing ')' (same for del-map)
1868 */
1869 rule->arg.map.ref = my_strndup(args[cur_arg-1] + 8, strlen(args[cur_arg-1]) - 9);
1870
1871 if (rule->from == ACT_F_HTTP_REQ) {
1872 px->conf.args.ctx = ARGC_HRQ;
1873 cap = (px->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR;
1874 }
1875 else{
1876 px->conf.args.ctx = ARGC_HRS;
1877 cap = (px->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR;
1878 }
1879
1880 /* key pattern */
1881 LIST_INIT(&rule->arg.map.key);
Christopher Faulet1337b322020-01-14 14:50:55 +01001882 if (!parse_logformat_string(args[cur_arg], px, &rule->arg.map.key, LOG_OPT_HTTP, cap, err)) {
1883 free(rule->arg.map.ref);
Christopher Faulet81e20172019-12-12 16:40:30 +01001884 return ACT_RET_PRS_ERR;
Christopher Faulet1337b322020-01-14 14:50:55 +01001885 }
Christopher Faulet81e20172019-12-12 16:40:30 +01001886
Christopher Faulet046cf442019-12-17 15:45:23 +01001887 if (rule->action == 1) {
Christopher Faulet81e20172019-12-12 16:40:30 +01001888 /* value pattern for set-map only */
1889 cur_arg++;
1890 LIST_INIT(&rule->arg.map.value);
Christopher Faulet1337b322020-01-14 14:50:55 +01001891 if (!parse_logformat_string(args[cur_arg], px, &rule->arg.map.value, LOG_OPT_HTTP, cap, err)) {
1892 free(rule->arg.map.ref);
Christopher Faulet81e20172019-12-12 16:40:30 +01001893 return ACT_RET_PRS_ERR;
Christopher Faulet1337b322020-01-14 14:50:55 +01001894 }
Christopher Faulet81e20172019-12-12 16:40:30 +01001895 }
1896
1897 free(px->conf.lfs_file);
1898 px->conf.lfs_file = strdup(px->conf.args.file);
1899 px->conf.lfs_line = px->conf.args.line;
1900
1901 *orig_arg = cur_arg + 1;
1902 return ACT_RET_PRS_OK;
1903}
1904
Christopher Fauletac98d812019-12-18 09:20:16 +01001905/* This function executes a track-sc* actions. On success, it returns
1906 * ACT_RET_CONT. Otherwsize ACT_RET_ERR is returned.
1907 */
1908static enum act_return http_action_track_sc(struct act_rule *rule, struct proxy *px,
1909 struct session *sess, struct stream *s, int flags)
1910{
1911 struct stktable *t;
1912 struct stksess *ts;
1913 struct stktable_key *key;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001914 void *ptr1, *ptr2, *ptr3, *ptr4, *ptr5, *ptr6;
Christopher Fauletac98d812019-12-18 09:20:16 +01001915 int opt;
1916
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001917 ptr1 = ptr2 = ptr3 = ptr4 = ptr5 = ptr6 = NULL;
Christopher Fauletac98d812019-12-18 09:20:16 +01001918 opt = ((rule->from == ACT_F_HTTP_REQ) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES) | SMP_OPT_FINAL;
1919
1920 t = rule->arg.trk_ctr.table.t;
Emeric Brun362d25e2021-03-10 16:58:03 +01001921
1922 if (stkctr_entry(&s->stkctr[rule->action]))
1923 goto end;
1924
Christopher Fauletac98d812019-12-18 09:20:16 +01001925 key = stktable_fetch_key(t, s->be, sess, s, opt, rule->arg.trk_ctr.expr, NULL);
1926
1927 if (!key)
1928 goto end;
1929 ts = stktable_get_entry(t, key);
1930 if (!ts)
1931 goto end;
1932
1933 stream_track_stkctr(&s->stkctr[rule->action], t, ts);
1934
1935 /* let's count a new HTTP request as it's the first time we do it */
1936 ptr1 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
1937 ptr2 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
1938
1939 /* When the client triggers a 4xx from the server, it's most often due
1940 * to a missing object or permission. These events should be tracked
1941 * because if they happen often, it may indicate a brute force or a
1942 * vulnerability scan. Normally this is done when receiving the response
1943 * but here we're tracking after this ought to have been done so we have
1944 * to do it on purpose.
1945 */
1946 if (rule->from == ACT_F_HTTP_RES && (unsigned)(s->txn->status - 400) < 100) {
1947 ptr3 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_CNT);
1948 ptr4 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_RATE);
1949 }
1950
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001951 if (rule->from == ACT_F_HTTP_RES && (unsigned)(s->txn->status - 500) < 100 &&
1952 s->txn->status != 501 && s->txn->status != 505) {
1953 ptr5 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_FAIL_CNT);
1954 ptr6 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_FAIL_RATE);
1955 }
1956
1957 if (ptr1 || ptr2 || ptr3 || ptr4 || ptr5 || ptr6) {
Christopher Fauletac98d812019-12-18 09:20:16 +01001958 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
1959
1960 if (ptr1)
1961 stktable_data_cast(ptr1, http_req_cnt)++;
1962 if (ptr2)
1963 update_freq_ctr_period(&stktable_data_cast(ptr2, http_req_rate),
1964 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
1965 if (ptr3)
1966 stktable_data_cast(ptr3, http_err_cnt)++;
1967 if (ptr4)
1968 update_freq_ctr_period(&stktable_data_cast(ptr4, http_err_rate),
1969 t->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1);
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001970 if (ptr5)
1971 stktable_data_cast(ptr5, http_fail_cnt)++;
1972 if (ptr6)
1973 update_freq_ctr_period(&stktable_data_cast(ptr6, http_fail_rate),
1974 t->data_arg[STKTABLE_DT_HTTP_FAIL_RATE].u, 1);
Christopher Fauletac98d812019-12-18 09:20:16 +01001975
1976 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
1977
1978 /* If data was modified, we need to touch to re-schedule sync */
1979 stktable_touch_local(t, ts, 0);
1980 }
1981
1982 stkctr_set_flags(&s->stkctr[rule->action], STKCTR_TRACK_CONTENT);
1983 if (sess->fe != s->be)
1984 stkctr_set_flags(&s->stkctr[rule->action], STKCTR_TRACK_BACKEND);
1985
1986 end:
1987 return ACT_RET_CONT;
1988}
Christopher Faulet81e20172019-12-12 16:40:30 +01001989
Christopher Faulet2eb53962020-01-14 14:47:34 +01001990static void release_http_track_sc(struct act_rule *rule)
1991{
1992 release_sample_expr(rule->arg.trk_ctr.expr);
1993}
1994
Christopher Faulet81e20172019-12-12 16:40:30 +01001995/* Parse a "track-sc*" actions. It returns ACT_RET_PRS_OK on success,
1996 * ACT_RET_PRS_ERR on error.
1997 */
1998static enum act_parse_ret parse_http_track_sc(const char **args, int *orig_arg, struct proxy *px,
1999 struct act_rule *rule, char **err)
2000{
2001 struct sample_expr *expr;
2002 unsigned int where;
2003 unsigned int tsc_num;
2004 const char *tsc_num_str;
2005 int cur_arg;
2006
2007 tsc_num_str = &args[*orig_arg-1][8];
2008 if (cfg_parse_track_sc_num(&tsc_num, tsc_num_str, tsc_num_str + strlen(tsc_num_str), err) == -1)
2009 return ACT_RET_PRS_ERR;
2010
2011 cur_arg = *orig_arg;
2012 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line,
Willy Tarreaue3b57bf2020-02-14 16:50:14 +01002013 err, &px->conf.args, NULL);
Christopher Faulet81e20172019-12-12 16:40:30 +01002014 if (!expr)
2015 return ACT_RET_PRS_ERR;
2016
2017 where = 0;
2018 if (px->cap & PR_CAP_FE)
2019 where |= (rule->from == ACT_F_HTTP_REQ ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_FE_HRS_HDR);
2020 if (px->cap & PR_CAP_BE)
2021 where |= (rule->from == ACT_F_HTTP_REQ ? SMP_VAL_BE_HRQ_HDR : SMP_VAL_BE_HRS_HDR);
2022
2023 if (!(expr->fetch->val & where)) {
2024 memprintf(err, "fetch method '%s' extracts information from '%s', none of which is available here",
2025 args[cur_arg-1], sample_src_names(expr->fetch->use));
Christopher Faulet1337b322020-01-14 14:50:55 +01002026 release_sample_expr(expr);
Christopher Faulet81e20172019-12-12 16:40:30 +01002027 return ACT_RET_PRS_ERR;
2028 }
2029
2030 if (strcmp(args[cur_arg], "table") == 0) {
2031 cur_arg++;
2032 if (!*args[cur_arg]) {
2033 memprintf(err, "missing table name");
Christopher Faulet1337b322020-01-14 14:50:55 +01002034 release_sample_expr(expr);
Christopher Faulet81e20172019-12-12 16:40:30 +01002035 return ACT_RET_PRS_ERR;
2036 }
2037
2038 /* we copy the table name for now, it will be resolved later */
2039 rule->arg.trk_ctr.table.n = strdup(args[cur_arg]);
2040 cur_arg++;
2041 }
2042
Christopher Fauletac98d812019-12-18 09:20:16 +01002043 rule->action = tsc_num;
Christopher Faulet81e20172019-12-12 16:40:30 +01002044 rule->arg.trk_ctr.expr = expr;
Christopher Fauletac98d812019-12-18 09:20:16 +01002045 rule->action_ptr = http_action_track_sc;
Christopher Faulet2eb53962020-01-14 14:47:34 +01002046 rule->release_ptr = release_http_track_sc;
Christopher Faulet81e20172019-12-12 16:40:30 +01002047 rule->check_ptr = check_trk_action;
2048
2049 *orig_arg = cur_arg;
2050 return ACT_RET_PRS_OK;
2051}
2052
Amaury Denoyelle8d228232020-12-10 13:43:54 +01002053static enum act_return action_timeout_set_stream_timeout(struct act_rule *rule,
2054 struct proxy *px,
2055 struct session *sess,
2056 struct stream *s,
2057 int flags)
2058{
2059 struct sample *key;
2060
2061 if (rule->arg.timeout.expr) {
2062 key = sample_fetch_as_type(px, sess, s, SMP_OPT_FINAL, rule->arg.timeout.expr, SMP_T_SINT);
2063 if (!key)
2064 return ACT_RET_CONT;
2065
2066 stream_set_timeout(s, rule->arg.timeout.type, MS_TO_TICKS(key->data.u.sint));
2067 }
2068 else {
2069 stream_set_timeout(s, rule->arg.timeout.type, MS_TO_TICKS(rule->arg.timeout.value));
2070 }
2071
2072 return ACT_RET_CONT;
2073}
2074
2075/* Parse a "set-timeout" action. Returns ACT_RET_PRS_ERR if parsing error.
2076 */
2077static enum act_parse_ret parse_http_set_timeout(const char **args,
2078 int *orig_arg,
2079 struct proxy *px,
2080 struct act_rule *rule, char **err)
2081{
2082 int cur_arg;
2083
2084 rule->action = ACT_CUSTOM;
2085 rule->action_ptr = action_timeout_set_stream_timeout;
2086 rule->release_ptr = release_timeout_action;
2087
2088 cur_arg = *orig_arg;
2089 if (!*args[cur_arg] || !*args[cur_arg + 1]) {
2090 memprintf(err, "expects exactly 2 arguments");
2091 return ACT_RET_PRS_ERR;
2092 }
2093
2094 if (!(px->cap & PR_CAP_BE)) {
2095 memprintf(err, "proxy '%s' has no backend capability", px->id);
2096 return ACT_RET_PRS_ERR;
2097 }
2098
2099 if (cfg_parse_rule_set_timeout(args, cur_arg,
2100 &rule->arg.timeout.value,
2101 &rule->arg.timeout.type,
2102 &rule->arg.timeout.expr,
2103 err,
2104 px->conf.args.file,
2105 px->conf.args.line, &px->conf.args) == -1) {
2106 return ACT_RET_PRS_ERR;
2107 }
2108
2109 *orig_arg = cur_arg + 2;
2110
2111 return ACT_RET_PRS_OK;
2112}
2113
Christopher Faulet46f95542019-12-20 10:07:22 +01002114/* This function executes a strict-mode actions. On success, it always returns
2115 * ACT_RET_CONT
2116 */
2117static enum act_return http_action_strict_mode(struct act_rule *rule, struct proxy *px,
2118 struct session *sess, struct stream *s, int flags)
2119{
2120 struct http_msg *msg = ((rule->from == ACT_F_HTTP_REQ) ? &s->txn->req : &s->txn->rsp);
2121
2122 if (rule->action == 0) // strict-mode on
2123 msg->flags &= ~HTTP_MSGF_SOFT_RW;
2124 else // strict-mode off
2125 msg->flags |= HTTP_MSGF_SOFT_RW;
2126 return ACT_RET_CONT;
2127}
2128
2129/* Parse a "strict-mode" action. It returns ACT_RET_PRS_OK on success,
2130 * ACT_RET_PRS_ERR on error.
2131 */
2132static enum act_parse_ret parse_http_strict_mode(const char **args, int *orig_arg, struct proxy *px,
2133 struct act_rule *rule, char **err)
2134{
2135 int cur_arg;
2136
Christopher Faulet46f95542019-12-20 10:07:22 +01002137 cur_arg = *orig_arg;
2138 if (!*args[cur_arg]) {
2139 memprintf(err, "expects exactly 1 arguments");
2140 return ACT_RET_PRS_ERR;
2141 }
2142
2143 if (strcasecmp(args[cur_arg], "on") == 0)
2144 rule->action = 0; // strict-mode on
2145 else if (strcasecmp(args[cur_arg], "off") == 0)
2146 rule->action = 1; // strict-mode off
2147 else {
2148 memprintf(err, "Unexpected value '%s'. Only 'on' and 'off' are supported", args[cur_arg]);
2149 return ACT_RET_PRS_ERR;
2150 }
2151 rule->action_ptr = http_action_strict_mode;
2152
2153 *orig_arg = cur_arg + 1;
2154 return ACT_RET_PRS_OK;
2155}
2156
Christopher Faulet24231ab2020-01-24 17:44:23 +01002157/* This function executes a return action. It builds an HTX message from an
2158 * errorfile, an raw file or a log-format string, depending on <.action>
2159 * value. On success, it returns ACT_RET_ABRT. If an error occurs ACT_RET_ERR is
2160 * returned.
2161 */
2162static enum act_return http_action_return(struct act_rule *rule, struct proxy *px,
2163 struct session *sess, struct stream *s, int flags)
2164{
2165 struct channel *req = &s->req;
Christopher Faulet24231ab2020-01-24 17:44:23 +01002166
Christopher Faulet2d36df22021-02-19 11:41:01 +01002167 s->txn->status = rule->arg.http_reply->status;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02002168 if (http_reply_message(s, rule->arg.http_reply) == -1)
2169 return ACT_RET_ERR;
Christopher Faulet24231ab2020-01-24 17:44:23 +01002170
Christopher Faulet24231ab2020-01-24 17:44:23 +01002171 if (rule->from == ACT_F_HTTP_REQ) {
2172 /* let's log the request time */
2173 s->logs.tv_request = now;
2174 req->analysers &= AN_REQ_FLT_END;
2175
2176 if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Willy Tarreau4781b152021-04-06 13:53:36 +02002177 _HA_ATOMIC_INC(&s->sess->fe->fe_counters.intercepted_req);
Christopher Faulet24231ab2020-01-24 17:44:23 +01002178 }
2179
2180 if (!(s->flags & SF_ERR_MASK))
2181 s->flags |= SF_ERR_LOCAL;
2182 if (!(s->flags & SF_FINST_MASK))
2183 s->flags |= ((rule->from == ACT_F_HTTP_REQ) ? SF_FINST_R : SF_FINST_H);
2184
Christopher Faulet0e2ad612020-05-13 16:38:37 +02002185 return ACT_RET_ABRT;
Christopher Faulet24231ab2020-01-24 17:44:23 +01002186}
2187
Christopher Faulet24231ab2020-01-24 17:44:23 +01002188/* Parse a "return" action. It returns ACT_RET_PRS_OK on success,
Christopher Faulet47e791e2020-05-13 14:36:55 +02002189 * ACT_RET_PRS_ERR on error. It relies on http_parse_http_reply() to set
2190 * <.arg.http_reply>.
Christopher Faulet24231ab2020-01-24 17:44:23 +01002191 */
2192static enum act_parse_ret parse_http_return(const char **args, int *orig_arg, struct proxy *px,
2193 struct act_rule *rule, char **err)
2194{
Christopher Faulet47e791e2020-05-13 14:36:55 +02002195 /* Prepare parsing of log-format strings */
2196 px->conf.args.ctx = ((rule->from == ACT_F_HTTP_REQ) ? ARGC_HRQ : ARGC_HRS);
2197 rule->arg.http_reply = http_parse_http_reply(args, orig_arg, px, 200, err);
2198 if (!rule->arg.http_reply)
2199 return ACT_RET_PRS_ERR;
Christopher Faulet24231ab2020-01-24 17:44:23 +01002200
Christopher Fauletba946bf2020-05-13 08:50:07 +02002201 rule->flags |= ACT_FLAG_FINAL;
Christopher Faulet5ff0c642020-05-12 18:33:37 +02002202 rule->action = ACT_CUSTOM;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002203 rule->check_ptr = check_act_http_reply;
Christopher Faulet24231ab2020-01-24 17:44:23 +01002204 rule->action_ptr = http_action_return;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002205 rule->release_ptr = release_act_http_reply;
Christopher Faulet24231ab2020-01-24 17:44:23 +01002206 return ACT_RET_PRS_OK;
Christopher Faulet24231ab2020-01-24 17:44:23 +01002207}
2208
Christopher Faulet021a8e42021-03-29 10:46:38 +02002209
2210
2211/* This function executes a wait-for-body action. It waits for the message
2212 * payload for a max configured time (.arg.p[0]) and eventually for only first
2213 * <arg.p[1]> bytes (0 means no limit). It relies on http_wait_for_msg_body()
2214 * function. it returns ACT_RET_CONT when conditions are met to stop to wait.
2215 * Otherwise ACT_RET_YIELD is returned to wait for more data. ACT_RET_INV is
2216 * returned if a parsing error is raised by lower level and ACT_RET_ERR if an
2217 * internal error occured. Finally ACT_RET_ABRT is returned when a timeout
2218 * occured.
2219 */
2220static enum act_return http_action_wait_for_body(struct act_rule *rule, struct proxy *px,
2221 struct session *sess, struct stream *s, int flags)
2222{
2223 struct channel *chn = ((rule->from == ACT_F_HTTP_REQ) ? &s->req : &s->res);
2224 unsigned int time = (uintptr_t)rule->arg.act.p[0];
2225 unsigned int bytes = (uintptr_t)rule->arg.act.p[1];
2226
2227 switch (http_wait_for_msg_body(s, chn, time, bytes)) {
2228 case HTTP_RULE_RES_CONT:
2229 return ACT_RET_CONT;
2230 case HTTP_RULE_RES_YIELD:
2231 return ACT_RET_YIELD;
2232 case HTTP_RULE_RES_BADREQ:
2233 return ACT_RET_INV;
2234 case HTTP_RULE_RES_ERROR:
2235 return ACT_RET_ERR;
2236 case HTTP_RULE_RES_ABRT:
2237 return ACT_RET_ABRT;
2238 default:
2239 return ACT_RET_ERR;
2240 }
2241}
2242
2243/* Parse a "wait-for-body" action. It returns ACT_RET_PRS_OK on success,
2244 * ACT_RET_PRS_ERR on error.
2245 */
2246static enum act_parse_ret parse_http_wait_for_body(const char **args, int *orig_arg, struct proxy *px,
2247 struct act_rule *rule, char **err)
2248{
2249 int cur_arg;
2250 unsigned int time, bytes;
2251 const char *res;
2252
2253 cur_arg = *orig_arg;
2254 if (!*args[cur_arg]) {
2255 memprintf(err, "expects time <time> [ at-least <bytes> ]");
2256 return ACT_RET_PRS_ERR;
2257 }
2258
2259 time = UINT_MAX; /* To be sure it is set */
2260 bytes = 0; /* Default value, wait all the body */
2261 while (*(args[cur_arg])) {
2262 if (strcmp(args[cur_arg], "time") == 0) {
2263 if (!*args[cur_arg + 1]) {
2264 memprintf(err, "missing argument for '%s'", args[cur_arg]);
2265 return ACT_RET_PRS_ERR;
2266 }
2267 res = parse_time_err(args[cur_arg+1], &time, TIME_UNIT_MS);
2268 if (res == PARSE_TIME_OVER) {
2269 memprintf(err, "time overflow (maximum value is 2147483647 ms or ~24.8 days)");
2270 return ACT_RET_PRS_ERR;
2271 }
2272 if (res == PARSE_TIME_UNDER) {
2273 memprintf(err, "time underflow (minimum non-null value is 1 ms)");
2274 return ACT_RET_PRS_ERR;
2275 }
2276 if (res) {
2277 memprintf(err, "unexpected character '%c'", *res);
2278 return ACT_RET_PRS_ERR;
2279 }
2280 cur_arg++;
2281 }
2282 else if (strcmp(args[cur_arg], "at-least") == 0) {
2283 if (!*args[cur_arg + 1]) {
2284 memprintf(err, "missing argument for '%s'", args[cur_arg]);
2285 return ACT_RET_PRS_ERR;
2286 }
2287 res = parse_size_err(args[cur_arg+1], &bytes);
2288 if (res) {
2289 memprintf(err, "unexpected character '%c'", *res);
2290 return ACT_RET_PRS_ERR;
2291 }
2292 cur_arg++;
2293 }
2294 else
2295 break;
2296 cur_arg++;
2297 }
2298
2299 if (time == UINT_MAX) {
2300 memprintf(err, "expects time <time> [ at-least <bytes> ]");
2301 return ACT_RET_PRS_ERR;
2302 }
2303
2304 rule->arg.act.p[0] = (void *)(uintptr_t)time;
2305 rule->arg.act.p[1] = (void *)(uintptr_t)bytes;
2306
2307 *orig_arg = cur_arg;
2308
2309 rule->action = ACT_CUSTOM;
2310 rule->action_ptr = http_action_wait_for_body;
2311 return ACT_RET_PRS_OK;
2312}
2313
Willy Tarreau79e57332018-10-02 16:01:16 +02002314/************************************************************************/
2315/* All supported http-request action keywords must be declared here. */
2316/************************************************************************/
2317
2318static struct action_kw_list http_req_actions = {
2319 .kw = {
Christopher Faulet81e20172019-12-12 16:40:30 +01002320 { "add-acl", parse_http_set_map, 1 },
2321 { "add-header", parse_http_set_header, 0 },
2322 { "allow", parse_http_allow, 0 },
2323 { "auth", parse_http_auth, 0 },
2324 { "capture", parse_http_req_capture, 0 },
2325 { "del-acl", parse_http_set_map, 1 },
2326 { "del-header", parse_http_del_header, 0 },
2327 { "del-map", parse_http_set_map, 1 },
Christopher Faulete0fca292020-01-13 21:49:03 +01002328 { "deny", parse_http_deny, 0 },
Christopher Faulet81e20172019-12-12 16:40:30 +01002329 { "disable-l7-retry", parse_http_req_disable_l7_retry, 0 },
2330 { "early-hint", parse_http_set_header, 0 },
Tim Duesterhusd2bedcc2021-04-15 21:45:57 +02002331 { "normalize-uri", parse_http_normalize_uri, 0 },
Christopher Faulet81e20172019-12-12 16:40:30 +01002332 { "redirect", parse_http_redirect, 0 },
2333 { "reject", parse_http_action_reject, 0 },
2334 { "replace-header", parse_http_replace_header, 0 },
2335 { "replace-path", parse_replace_uri, 0 },
Christopher Faulet312294f2020-09-02 17:17:44 +02002336 { "replace-pathq", parse_replace_uri, 0 },
Christopher Faulet81e20172019-12-12 16:40:30 +01002337 { "replace-uri", parse_replace_uri, 0 },
2338 { "replace-value", parse_http_replace_header, 0 },
Christopher Faulet24231ab2020-01-24 17:44:23 +01002339 { "return", parse_http_return, 0 },
Christopher Faulet81e20172019-12-12 16:40:30 +01002340 { "set-header", parse_http_set_header, 0 },
2341 { "set-log-level", parse_http_set_log_level, 0 },
2342 { "set-map", parse_http_set_map, 1 },
2343 { "set-method", parse_set_req_line, 0 },
2344 { "set-mark", parse_http_set_mark, 0 },
2345 { "set-nice", parse_http_set_nice, 0 },
2346 { "set-path", parse_set_req_line, 0 },
Christopher Faulet312294f2020-09-02 17:17:44 +02002347 { "set-pathq", parse_set_req_line, 0 },
Christopher Faulet81e20172019-12-12 16:40:30 +01002348 { "set-query", parse_set_req_line, 0 },
2349 { "set-tos", parse_http_set_tos, 0 },
2350 { "set-uri", parse_set_req_line, 0 },
Christopher Faulet46f95542019-12-20 10:07:22 +01002351 { "strict-mode", parse_http_strict_mode, 0 },
Christopher Faulete0fca292020-01-13 21:49:03 +01002352 { "tarpit", parse_http_deny, 0 },
Christopher Faulet81e20172019-12-12 16:40:30 +01002353 { "track-sc", parse_http_track_sc, 1 },
Amaury Denoyelle8d228232020-12-10 13:43:54 +01002354 { "set-timeout", parse_http_set_timeout, 0 },
Christopher Faulet021a8e42021-03-29 10:46:38 +02002355 { "wait-for-body", parse_http_wait_for_body, 0 },
Willy Tarreau79e57332018-10-02 16:01:16 +02002356 { NULL, NULL }
2357 }
2358};
2359
Willy Tarreau0108d902018-11-25 19:14:37 +01002360INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_actions);
2361
Willy Tarreau79e57332018-10-02 16:01:16 +02002362static struct action_kw_list http_res_actions = {
2363 .kw = {
Christopher Faulet81e20172019-12-12 16:40:30 +01002364 { "add-acl", parse_http_set_map, 1 },
2365 { "add-header", parse_http_set_header, 0 },
2366 { "allow", parse_http_allow, 0 },
2367 { "capture", parse_http_res_capture, 0 },
2368 { "del-acl", parse_http_set_map, 1 },
2369 { "del-header", parse_http_del_header, 0 },
2370 { "del-map", parse_http_set_map, 1 },
Christopher Faulete0fca292020-01-13 21:49:03 +01002371 { "deny", parse_http_deny, 0 },
Christopher Faulet81e20172019-12-12 16:40:30 +01002372 { "redirect", parse_http_redirect, 0 },
2373 { "replace-header", parse_http_replace_header, 0 },
2374 { "replace-value", parse_http_replace_header, 0 },
Christopher Faulet24231ab2020-01-24 17:44:23 +01002375 { "return", parse_http_return, 0 },
Christopher Faulet81e20172019-12-12 16:40:30 +01002376 { "set-header", parse_http_set_header, 0 },
2377 { "set-log-level", parse_http_set_log_level, 0 },
2378 { "set-map", parse_http_set_map, 1 },
2379 { "set-mark", parse_http_set_mark, 0 },
2380 { "set-nice", parse_http_set_nice, 0 },
2381 { "set-status", parse_http_set_status, 0 },
2382 { "set-tos", parse_http_set_tos, 0 },
Christopher Faulet46f95542019-12-20 10:07:22 +01002383 { "strict-mode", parse_http_strict_mode, 0 },
Christopher Faulet81e20172019-12-12 16:40:30 +01002384 { "track-sc", parse_http_track_sc, 1 },
Christopher Faulet021a8e42021-03-29 10:46:38 +02002385 { "wait-for-body", parse_http_wait_for_body, 0 },
Willy Tarreau79e57332018-10-02 16:01:16 +02002386 { NULL, NULL }
2387 }
2388};
2389
Willy Tarreau0108d902018-11-25 19:14:37 +01002390INITCALL1(STG_REGISTER, http_res_keywords_register, &http_res_actions);
Willy Tarreau79e57332018-10-02 16:01:16 +02002391
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002392static struct action_kw_list http_after_res_actions = {
2393 .kw = {
2394 { "add-header", parse_http_set_header, 0 },
2395 { "allow", parse_http_allow, 0 },
2396 { "del-header", parse_http_del_header, 0 },
2397 { "replace-header", parse_http_replace_header, 0 },
2398 { "replace-value", parse_http_replace_header, 0 },
2399 { "set-header", parse_http_set_header, 0 },
2400 { "set-status", parse_http_set_status, 0 },
2401 { "strict-mode", parse_http_strict_mode, 0 },
2402 { NULL, NULL }
2403 }
2404};
2405
2406INITCALL1(STG_REGISTER, http_after_res_keywords_register, &http_after_res_actions);
2407
Willy Tarreau79e57332018-10-02 16:01:16 +02002408/*
2409 * Local variables:
2410 * c-indent-level: 8
2411 * c-basic-offset: 8
2412 * End:
2413 */