blob: 1480563c949c35139acf7c03ed747abbc0083e66 [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 Duesterhus560e1a62021-04-15 21:46:00 +0200235 case ACT_NORMALIZE_URI_DOTDOT:
236 case ACT_NORMALIZE_URI_DOTDOT_FULL: {
Tim Duesterhus9982fc22021-04-15 21:45:59 +0200237 const struct ist path = http_get_path(uri);
238 struct ist newpath = ist2(replace->area, replace->size);
239
240 if (!isttest(path))
241 goto leave;
242
Tim Duesterhus560e1a62021-04-15 21:46:00 +0200243 err = uri_normalizer_path_dotdot(iststop(path, '?'), rule->action == ACT_NORMALIZE_URI_DOTDOT_FULL, &newpath);
Tim Duesterhus9982fc22021-04-15 21:45:59 +0200244
245 if (err != URI_NORMALIZER_ERR_NONE)
246 break;
247
248 if (!http_replace_req_path(htx, newpath, 0))
249 goto fail_rewrite;
250
251 break;
252 }
Tim Duesterhusd7b89be2021-04-15 21:46:01 +0200253 case ACT_NORMALIZE_URI_SORT_QUERY: {
254 const struct ist path = http_get_path(uri);
255 struct ist newquery = ist2(replace->area, replace->size);
256
257 if (!isttest(path))
258 goto leave;
259
260 err = uri_normalizer_query_sort(istfind(path, '?'), '&', &newquery);
261
262 if (err != URI_NORMALIZER_ERR_NONE)
263 break;
264
265 if (!http_replace_req_query(htx, newquery))
266 goto fail_rewrite;
267
268 break;
269 }
Tim Duesterhusd2bedcc2021-04-15 21:45:57 +0200270 }
271
272 switch (err) {
273 case URI_NORMALIZER_ERR_NONE:
274 break;
275 case URI_NORMALIZER_ERR_INTERNAL_ERROR:
276 ret = ACT_RET_ERR;
277 break;
278 case URI_NORMALIZER_ERR_INVALID_INPUT:
279 ret = ACT_RET_INV;
280 break;
281 case URI_NORMALIZER_ERR_ALLOC:
282 goto fail_alloc;
283 }
284
285 leave:
286 free_trash_chunk(replace);
287 return ret;
288
289 fail_alloc:
290 if (!(s->flags & SF_ERR_MASK))
291 s->flags |= SF_ERR_RESOURCE;
292 ret = ACT_RET_ERR;
293 goto leave;
294
295 fail_rewrite:
296 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_rewrites, 1);
297 if (s->flags & SF_BE_ASSIGNED)
298 _HA_ATOMIC_ADD(&s->be->be_counters.failed_rewrites, 1);
299 if (sess->listener && sess->listener->counters)
300 _HA_ATOMIC_ADD(&sess->listener->counters->failed_rewrites, 1);
301 if (objt_server(s->target))
302 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_rewrites, 1);
303
304 if (!(s->txn->req.flags & HTTP_MSGF_SOFT_RW)) {
305 ret = ACT_RET_ERR;
306 if (!(s->flags & SF_ERR_MASK))
307 s->flags |= SF_ERR_PRXCOND;
308 }
309 goto leave;
310}
311
312/* Parses the http-request normalize-uri action. It expects a single <normalizer>
313 * argument, corresponding too a value in `enum act_normalize_uri`.
314 *
315 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
316 */
317static enum act_parse_ret parse_http_normalize_uri(const char **args, int *orig_arg, struct proxy *px,
318 struct act_rule *rule, char **err)
319{
320 int cur_arg = *orig_arg;
321
322 rule->action_ptr = http_action_normalize_uri;
323 rule->release_ptr = NULL;
324
325 if (!*args[cur_arg]) {
326 memprintf(err, "missing argument <normalizer>");
327 return ACT_RET_PRS_ERR;
328 }
329
Tim Duesterhusd371e992021-04-15 21:45:58 +0200330 if (strcmp(args[cur_arg], "merge-slashes") == 0) {
331 cur_arg++;
Tim Duesterhusd2bedcc2021-04-15 21:45:57 +0200332
Tim Duesterhusd371e992021-04-15 21:45:58 +0200333 rule->action = ACT_NORMALIZE_URI_MERGE_SLASHES;
Tim Duesterhusd2bedcc2021-04-15 21:45:57 +0200334 }
Tim Duesterhus9982fc22021-04-15 21:45:59 +0200335 else if (strcmp(args[cur_arg], "dotdot") == 0) {
336 cur_arg++;
337
Tim Duesterhus560e1a62021-04-15 21:46:00 +0200338 if (strcmp(args[cur_arg], "full") == 0) {
339 cur_arg++;
340 rule->action = ACT_NORMALIZE_URI_DOTDOT_FULL;
341 }
342 else if (!*args[cur_arg]) {
343 rule->action = ACT_NORMALIZE_URI_DOTDOT;
344 }
345 else if (strcmp(args[cur_arg], "if") != 0 && strcmp(args[cur_arg], "unless") != 0) {
346 memprintf(err, "unknown argument '%s' for 'dotdot' normalizer", args[cur_arg]);
347 return ACT_RET_PRS_ERR;
348 }
Tim Duesterhus9982fc22021-04-15 21:45:59 +0200349 }
Tim Duesterhusd7b89be2021-04-15 21:46:01 +0200350 else if (strcmp(args[cur_arg], "sort-query") == 0) {
351 cur_arg++;
352
353 rule->action = ACT_NORMALIZE_URI_SORT_QUERY;
354 }
Tim Duesterhusd2bedcc2021-04-15 21:45:57 +0200355 else {
356 memprintf(err, "unknown normalizer '%s'", args[cur_arg]);
357 return ACT_RET_PRS_ERR;
358 }
359
360 *orig_arg = cur_arg;
361 return ACT_RET_PRS_OK;
362}
363
Willy Tarreau33810222019-06-12 17:44:02 +0200364/* This function executes a replace-uri action. It finds its arguments in
Christopher Faulet96bff762019-12-17 13:46:18 +0100365 * <rule>.arg.http. It builds a string in the trash from the format string
Willy Tarreau33810222019-06-12 17:44:02 +0200366 * previously filled by function parse_replace_uri() and will execute the regex
Christopher Faulet96bff762019-12-17 13:46:18 +0100367 * in <http.re> to replace the URI. It uses the format string present in
Christopher Faulet2c22a692019-12-18 15:39:56 +0100368 * <http.fmt>. The component to act on (path/uri) is taken from <.action> which
Christopher Faulet96bff762019-12-17 13:46:18 +0100369 * contains 1 for the path or 3 for the URI (values used by
370 * http_req_replace_stline()). On success, it returns ACT_RET_CONT. If an error
371 * occurs while soft rewrites are enabled, the action is canceled, but the rule
372 * processing continue. Otherwsize ACT_RET_ERR is returned.
Willy Tarreau33810222019-06-12 17:44:02 +0200373 */
374static enum act_return http_action_replace_uri(struct act_rule *rule, struct proxy *px,
375 struct session *sess, struct stream *s, int flags)
376{
Christopher Faulet13403762019-12-13 09:01:57 +0100377 enum act_return ret = ACT_RET_CONT;
Willy Tarreau33810222019-06-12 17:44:02 +0200378 struct buffer *replace, *output;
379 struct ist uri;
380 int len;
381
382 replace = alloc_trash_chunk();
383 output = alloc_trash_chunk();
384 if (!replace || !output)
Christopher Faulete00d06c2019-12-16 17:18:42 +0100385 goto fail_alloc;
Christopher Faulet12c28b62019-07-15 16:30:24 +0200386 uri = htx_sl_req_uri(http_get_stline(htxbuf(&s->req.buf)));
Willy Tarreau262c3f12019-12-17 06:52:51 +0100387
Christopher Faulet1fa0cc12020-09-02 11:10:38 +0200388 if (rule->action == 1) // replace-path
389 uri = iststop(http_get_path(uri), '?');
Christopher Faulet312294f2020-09-02 17:17:44 +0200390 else if (rule->action == 4) // replace-pathq
391 uri = http_get_path(uri);
Willy Tarreau262c3f12019-12-17 06:52:51 +0100392
Christopher Faulet96bff762019-12-17 13:46:18 +0100393 if (!regex_exec_match2(rule->arg.http.re, uri.ptr, uri.len, MAX_MATCH, pmatch, 0))
Willy Tarreau33810222019-06-12 17:44:02 +0200394 goto leave;
395
Christopher Faulet96bff762019-12-17 13:46:18 +0100396 replace->data = build_logline(s, replace->area, replace->size, &rule->arg.http.fmt);
Willy Tarreau33810222019-06-12 17:44:02 +0200397
398 /* note: uri.ptr doesn't need to be zero-terminated because it will
399 * only be used to pick pmatch references.
400 */
401 len = exp_replace(output->area, output->size, uri.ptr, replace->area, pmatch);
402 if (len == -1)
Christopher Faulete00d06c2019-12-16 17:18:42 +0100403 goto fail_rewrite;
Willy Tarreau33810222019-06-12 17:44:02 +0200404
Christopher Faulet2c22a692019-12-18 15:39:56 +0100405 if (http_req_replace_stline(rule->action, output->area, len, px, s) == -1)
Christopher Faulete00d06c2019-12-16 17:18:42 +0100406 goto fail_rewrite;
Willy Tarreau33810222019-06-12 17:44:02 +0200407
Christopher Faulete00d06c2019-12-16 17:18:42 +0100408 leave:
Willy Tarreau33810222019-06-12 17:44:02 +0200409 free_trash_chunk(output);
410 free_trash_chunk(replace);
411 return ret;
Christopher Faulete00d06c2019-12-16 17:18:42 +0100412
413 fail_alloc:
414 if (!(s->flags & SF_ERR_MASK))
415 s->flags |= SF_ERR_RESOURCE;
416 ret = ACT_RET_ERR;
417 goto leave;
418
419 fail_rewrite:
Willy Tarreau4781b152021-04-06 13:53:36 +0200420 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_rewrites);
Christopher Faulete00d06c2019-12-16 17:18:42 +0100421 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200422 _HA_ATOMIC_INC(&s->be->be_counters.failed_rewrites);
William Lallemand36119de2021-03-08 15:26:48 +0100423 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200424 _HA_ATOMIC_INC(&sess->listener->counters->failed_rewrites);
Christopher Faulete00d06c2019-12-16 17:18:42 +0100425 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +0200426 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_rewrites);
Christopher Faulete00d06c2019-12-16 17:18:42 +0100427
Christopher Faulet333bf8c2020-01-22 14:38:05 +0100428 if (!(s->txn->req.flags & HTTP_MSGF_SOFT_RW)) {
Christopher Faulete00d06c2019-12-16 17:18:42 +0100429 ret = ACT_RET_ERR;
Christopher Faulet333bf8c2020-01-22 14:38:05 +0100430 if (!(s->flags & SF_ERR_MASK))
431 s->flags |= SF_ERR_PRXCOND;
432 }
Christopher Faulete00d06c2019-12-16 17:18:42 +0100433 goto leave;
Willy Tarreau33810222019-06-12 17:44:02 +0200434}
435
Christopher Faulet312294f2020-09-02 17:17:44 +0200436/* parse a "replace-uri", "replace-path" or "replace-pathq"
437 * http-request action.
Willy Tarreau33810222019-06-12 17:44:02 +0200438 * This action takes 2 arguments (a regex and a replacement format string).
Christopher Faulet2c22a692019-12-18 15:39:56 +0100439 * The resulting rule makes use of <.action> to store the action (1/3 for now),
Christopher Faulet96bff762019-12-17 13:46:18 +0100440 * <http.re> to store the compiled regex, and <http.fmt> to store the log-format
Willy Tarreau33810222019-06-12 17:44:02 +0200441 * list head. It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
442 */
443static enum act_parse_ret parse_replace_uri(const char **args, int *orig_arg, struct proxy *px,
444 struct act_rule *rule, char **err)
445{
446 int cur_arg = *orig_arg;
447 char *error = NULL;
448
Christopher Faulet312294f2020-09-02 17:17:44 +0200449 switch (args[0][8]) {
450 case 'p':
451 if (args[0][12] == 'q')
452 rule->action = 4; // replace-pathq, same as set-pathq
453 else
454 rule->action = 1; // replace-path, same as set-path
455 break;
456 case 'u':
Christopher Faulet2c22a692019-12-18 15:39:56 +0100457 rule->action = 3; // replace-uri, same as set-uri
Christopher Faulet312294f2020-09-02 17:17:44 +0200458 break;
459 default:
460 memprintf(err, "internal error: unhandled action '%s'", args[0]);
461 return ACT_RET_PRS_ERR;
462 }
Willy Tarreau262c3f12019-12-17 06:52:51 +0100463
Willy Tarreau33810222019-06-12 17:44:02 +0200464 rule->action_ptr = http_action_replace_uri;
Christopher Faulet2eb53962020-01-14 14:47:34 +0100465 rule->release_ptr = release_http_action;
Willy Tarreau33810222019-06-12 17:44:02 +0200466
467 if (!*args[cur_arg] || !*args[cur_arg+1] ||
468 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
469 memprintf(err, "expects exactly 2 arguments <match-regex> and <replace-format>");
470 return ACT_RET_PRS_ERR;
471 }
472
Christopher Faulet96bff762019-12-17 13:46:18 +0100473 if (!(rule->arg.http.re = regex_comp(args[cur_arg], 1, 1, &error))) {
Willy Tarreau33810222019-06-12 17:44:02 +0200474 memprintf(err, "failed to parse the regex : %s", error);
475 free(error);
476 return ACT_RET_PRS_ERR;
477 }
478
Christopher Faulet96bff762019-12-17 13:46:18 +0100479 LIST_INIT(&rule->arg.http.fmt);
Willy Tarreau33810222019-06-12 17:44:02 +0200480 px->conf.args.ctx = ARGC_HRQ;
Christopher Faulet96bff762019-12-17 13:46:18 +0100481 if (!parse_logformat_string(args[cur_arg + 1], px, &rule->arg.http.fmt, LOG_OPT_HTTP,
Willy Tarreau33810222019-06-12 17:44:02 +0200482 (px->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, err)) {
Christopher Faulet1337b322020-01-14 14:50:55 +0100483 regex_free(rule->arg.http.re);
Willy Tarreau33810222019-06-12 17:44:02 +0200484 return ACT_RET_PRS_ERR;
485 }
486
487 (*orig_arg) += 2;
488 return ACT_RET_PRS_OK;
489}
490
Willy Tarreau79e57332018-10-02 16:01:16 +0200491/* This function is just a compliant action wrapper for "set-status". */
492static enum act_return action_http_set_status(struct act_rule *rule, struct proxy *px,
493 struct session *sess, struct stream *s, int flags)
494{
Christopher Faulet96bff762019-12-17 13:46:18 +0100495 if (http_res_set_status(rule->arg.http.i, rule->arg.http.str, s) == -1) {
Willy Tarreau4781b152021-04-06 13:53:36 +0200496 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_rewrites);
Christopher Faulete00d06c2019-12-16 17:18:42 +0100497 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200498 _HA_ATOMIC_INC(&s->be->be_counters.failed_rewrites);
William Lallemand36119de2021-03-08 15:26:48 +0100499 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200500 _HA_ATOMIC_INC(&sess->listener->counters->failed_rewrites);
Christopher Faulete00d06c2019-12-16 17:18:42 +0100501 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +0200502 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_rewrites);
Christopher Faulete00d06c2019-12-16 17:18:42 +0100503
Christopher Faulet333bf8c2020-01-22 14:38:05 +0100504 if (!(s->txn->req.flags & HTTP_MSGF_SOFT_RW)) {
Christopher Faulet333bf8c2020-01-22 14:38:05 +0100505 if (!(s->flags & SF_ERR_MASK))
506 s->flags |= SF_ERR_PRXCOND;
Christopher Faulet692a6c22020-02-07 10:22:31 +0100507 return ACT_RET_ERR;
Christopher Faulet333bf8c2020-01-22 14:38:05 +0100508 }
Christopher Faulete00d06c2019-12-16 17:18:42 +0100509 }
510
Willy Tarreau79e57332018-10-02 16:01:16 +0200511 return ACT_RET_CONT;
512}
513
514/* parse set-status action:
515 * This action accepts a single argument of type int representing
516 * an http status code. It returns ACT_RET_PRS_OK on success,
517 * ACT_RET_PRS_ERR on error.
518 */
519static enum act_parse_ret parse_http_set_status(const char **args, int *orig_arg, struct proxy *px,
520 struct act_rule *rule, char **err)
521{
522 char *error;
523
524 rule->action = ACT_CUSTOM;
525 rule->action_ptr = action_http_set_status;
Christopher Faulet2eb53962020-01-14 14:47:34 +0100526 rule->release_ptr = release_http_action;
Willy Tarreau79e57332018-10-02 16:01:16 +0200527
528 /* Check if an argument is available */
529 if (!*args[*orig_arg]) {
530 memprintf(err, "expects 1 argument: <status>; or 3 arguments: <status> reason <fmt>");
531 return ACT_RET_PRS_ERR;
532 }
533
534 /* convert status code as integer */
Christopher Faulet96bff762019-12-17 13:46:18 +0100535 rule->arg.http.i = strtol(args[*orig_arg], &error, 10);
536 if (*error != '\0' || rule->arg.http.i < 100 || rule->arg.http.i > 999) {
Willy Tarreau79e57332018-10-02 16:01:16 +0200537 memprintf(err, "expects an integer status code between 100 and 999");
538 return ACT_RET_PRS_ERR;
539 }
540
541 (*orig_arg)++;
542
543 /* set custom reason string */
Christopher Faulet96bff762019-12-17 13:46:18 +0100544 rule->arg.http.str = ist(NULL); // If null, we use the default reason for the status code.
Willy Tarreau79e57332018-10-02 16:01:16 +0200545 if (*args[*orig_arg] && strcmp(args[*orig_arg], "reason") == 0 &&
546 (*args[*orig_arg + 1] && strcmp(args[*orig_arg + 1], "if") != 0 && strcmp(args[*orig_arg + 1], "unless") != 0)) {
547 (*orig_arg)++;
Christopher Faulet96bff762019-12-17 13:46:18 +0100548 rule->arg.http.str.ptr = strdup(args[*orig_arg]);
549 rule->arg.http.str.len = strlen(rule->arg.http.str.ptr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200550 (*orig_arg)++;
551 }
552
Christopher Fauletc20b3712020-01-27 15:51:56 +0100553 LIST_INIT(&rule->arg.http.fmt);
Willy Tarreau79e57332018-10-02 16:01:16 +0200554 return ACT_RET_PRS_OK;
555}
556
557/* This function executes the "reject" HTTP action. It clears the request and
558 * response buffer without sending any response. It can be useful as an HTTP
559 * alternative to the silent-drop action to defend against DoS attacks, and may
560 * also be used with HTTP/2 to close a connection instead of just a stream.
561 * The txn status is unchanged, indicating no response was sent. The termination
Christopher Faulet90d22a82020-03-06 11:18:39 +0100562 * flags will indicate "PR". It always returns ACT_RET_ABRT.
Willy Tarreau79e57332018-10-02 16:01:16 +0200563 */
564static enum act_return http_action_reject(struct act_rule *rule, struct proxy *px,
565 struct session *sess, struct stream *s, int flags)
566{
Willy Tarreau0f9cd7b2019-01-31 19:02:43 +0100567 si_must_kill_conn(chn_prod(&s->req));
Willy Tarreau79e57332018-10-02 16:01:16 +0200568 channel_abort(&s->req);
569 channel_abort(&s->res);
Christopher Fauletd4a824e2020-03-06 15:07:09 +0100570 s->req.analysers &= AN_REQ_FLT_END;
571 s->res.analysers &= AN_RES_FLT_END;
Willy Tarreau79e57332018-10-02 16:01:16 +0200572
Willy Tarreau4781b152021-04-06 13:53:36 +0200573 _HA_ATOMIC_INC(&s->be->be_counters.denied_req);
574 _HA_ATOMIC_INC(&sess->fe->fe_counters.denied_req);
Willy Tarreau79e57332018-10-02 16:01:16 +0200575 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200576 _HA_ATOMIC_INC(&sess->listener->counters->denied_req);
Willy Tarreau79e57332018-10-02 16:01:16 +0200577
578 if (!(s->flags & SF_ERR_MASK))
579 s->flags |= SF_ERR_PRXCOND;
580 if (!(s->flags & SF_FINST_MASK))
581 s->flags |= SF_FINST_R;
582
Christopher Faulet90d22a82020-03-06 11:18:39 +0100583 return ACT_RET_ABRT;
Willy Tarreau79e57332018-10-02 16:01:16 +0200584}
585
586/* parse the "reject" action:
587 * This action takes no argument and returns ACT_RET_PRS_OK on success,
588 * ACT_RET_PRS_ERR on error.
589 */
590static enum act_parse_ret parse_http_action_reject(const char **args, int *orig_arg, struct proxy *px,
591 struct act_rule *rule, char **err)
592{
593 rule->action = ACT_CUSTOM;
594 rule->action_ptr = http_action_reject;
595 return ACT_RET_PRS_OK;
596}
597
Olivier Houchard602bf7d2019-05-10 13:59:15 +0200598/* This function executes the "disable-l7-retry" HTTP action.
599 * It disables L7 retries (all retry except for a connection failure). This
600 * can be useful for example to avoid retrying on POST requests.
601 * It just removes the L7 retry flag on the stream_interface, and always
602 * return ACT_RET_CONT;
603 */
604static enum act_return http_req_disable_l7_retry(struct act_rule *rule, struct proxy *px,
605 struct session *sess, struct stream *s, int flags)
606{
607 struct stream_interface *si = &s->si[1];
608
609 /* In theory, the SI_FL_L7_RETRY flags isn't set at this point, but
610 * let's be future-proof and remove it anyway.
611 */
612 si->flags &= ~SI_FL_L7_RETRY;
613 si->flags |= SI_FL_D_L7_RETRY;
614 return ACT_RET_CONT;
615}
616
617/* parse the "disable-l7-retry" action:
618 * This action takes no argument and returns ACT_RET_PRS_OK on success,
619 * ACT_RET_PRS_ERR on error.
620 */
621static enum act_parse_ret parse_http_req_disable_l7_retry(const char **args,
622 int *orig_args, struct proxy *px,
623 struct act_rule *rule, char **err)
624{
625 rule->action = ACT_CUSTOM;
626 rule->action_ptr = http_req_disable_l7_retry;
627 return ACT_RET_PRS_OK;
628}
629
Willy Tarreau79e57332018-10-02 16:01:16 +0200630/* This function executes the "capture" action. It executes a fetch expression,
631 * turns the result into a string and puts it in a capture slot. It always
632 * returns 1. If an error occurs the action is cancelled, but the rule
633 * processing continues.
634 */
635static enum act_return http_action_req_capture(struct act_rule *rule, struct proxy *px,
636 struct session *sess, struct stream *s, int flags)
637{
638 struct sample *key;
639 struct cap_hdr *h = rule->arg.cap.hdr;
640 char **cap = s->req_cap;
641 int len;
642
643 key = sample_fetch_as_type(s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.cap.expr, SMP_T_STR);
644 if (!key)
645 return ACT_RET_CONT;
646
647 if (cap[h->index] == NULL)
648 cap[h->index] = pool_alloc(h->pool);
649
650 if (cap[h->index] == NULL) /* no more capture memory */
651 return ACT_RET_CONT;
652
653 len = key->data.u.str.data;
654 if (len > h->len)
655 len = h->len;
656
657 memcpy(cap[h->index], key->data.u.str.area, len);
658 cap[h->index][len] = 0;
659 return ACT_RET_CONT;
660}
661
662/* This function executes the "capture" action and store the result in a
663 * capture slot if exists. It executes a fetch expression, turns the result
664 * into a string and puts it in a capture slot. It always returns 1. If an
665 * error occurs the action is cancelled, but the rule processing continues.
666 */
667static enum act_return http_action_req_capture_by_id(struct act_rule *rule, struct proxy *px,
668 struct session *sess, struct stream *s, int flags)
669{
670 struct sample *key;
671 struct cap_hdr *h;
672 char **cap = s->req_cap;
673 struct proxy *fe = strm_fe(s);
674 int len;
675 int i;
676
677 /* Look for the original configuration. */
678 for (h = fe->req_cap, i = fe->nb_req_cap - 1;
679 h != NULL && i != rule->arg.capid.idx ;
680 i--, h = h->next);
681 if (!h)
682 return ACT_RET_CONT;
683
684 key = sample_fetch_as_type(s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.capid.expr, SMP_T_STR);
685 if (!key)
686 return ACT_RET_CONT;
687
688 if (cap[h->index] == NULL)
689 cap[h->index] = pool_alloc(h->pool);
690
691 if (cap[h->index] == NULL) /* no more capture memory */
692 return ACT_RET_CONT;
693
694 len = key->data.u.str.data;
695 if (len > h->len)
696 len = h->len;
697
698 memcpy(cap[h->index], key->data.u.str.area, len);
699 cap[h->index][len] = 0;
700 return ACT_RET_CONT;
701}
702
703/* Check an "http-request capture" action.
704 *
705 * The function returns 1 in success case, otherwise, it returns 0 and err is
706 * filled.
707 */
708static int check_http_req_capture(struct act_rule *rule, struct proxy *px, char **err)
709{
710 if (rule->action_ptr != http_action_req_capture_by_id)
711 return 1;
712
Baptiste Assmann19a69b32020-01-16 14:34:22 +0100713 /* capture slots can only be declared in frontends, so we can't check their
714 * existence in backends at configuration parsing step
715 */
716 if (px->cap & PR_CAP_FE && rule->arg.capid.idx >= px->nb_req_cap) {
Willy Tarreau79e57332018-10-02 16:01:16 +0200717 memprintf(err, "unable to find capture id '%d' referenced by http-request capture rule",
718 rule->arg.capid.idx);
719 return 0;
720 }
721
722 return 1;
723}
724
Christopher Faulet2eb53962020-01-14 14:47:34 +0100725/* Release memory allocate by an http capture action */
726static void release_http_capture(struct act_rule *rule)
727{
728 if (rule->action_ptr == http_action_req_capture)
729 release_sample_expr(rule->arg.cap.expr);
730 else
731 release_sample_expr(rule->arg.capid.expr);
732}
733
Willy Tarreau79e57332018-10-02 16:01:16 +0200734/* parse an "http-request capture" action. It takes a single argument which is
735 * a sample fetch expression. It stores the expression into arg->act.p[0] and
736 * the allocated hdr_cap struct or the preallocated "id" into arg->act.p[1].
737 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
738 */
739static enum act_parse_ret parse_http_req_capture(const char **args, int *orig_arg, struct proxy *px,
740 struct act_rule *rule, char **err)
741{
742 struct sample_expr *expr;
743 struct cap_hdr *hdr;
744 int cur_arg;
745 int len = 0;
746
747 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
748 if (strcmp(args[cur_arg], "if") == 0 ||
749 strcmp(args[cur_arg], "unless") == 0)
750 break;
751
752 if (cur_arg < *orig_arg + 3) {
753 memprintf(err, "expects <expression> [ 'len' <length> | id <idx> ]");
754 return ACT_RET_PRS_ERR;
755 }
756
757 cur_arg = *orig_arg;
Willy Tarreaue3b57bf2020-02-14 16:50:14 +0100758 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 +0200759 if (!expr)
760 return ACT_RET_PRS_ERR;
761
762 if (!(expr->fetch->val & SMP_VAL_FE_HRQ_HDR)) {
763 memprintf(err,
764 "fetch method '%s' extracts information from '%s', none of which is available here",
765 args[cur_arg-1], sample_src_names(expr->fetch->use));
Christopher Faulet1337b322020-01-14 14:50:55 +0100766 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200767 return ACT_RET_PRS_ERR;
768 }
769
770 if (!args[cur_arg] || !*args[cur_arg]) {
771 memprintf(err, "expects 'len or 'id'");
Christopher Faulet1337b322020-01-14 14:50:55 +0100772 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200773 return ACT_RET_PRS_ERR;
774 }
775
776 if (strcmp(args[cur_arg], "len") == 0) {
777 cur_arg++;
778
779 if (!(px->cap & PR_CAP_FE)) {
780 memprintf(err, "proxy '%s' has no frontend capability", px->id);
Christopher Faulet1337b322020-01-14 14:50:55 +0100781 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200782 return ACT_RET_PRS_ERR;
783 }
784
785 px->conf.args.ctx = ARGC_CAP;
786
787 if (!args[cur_arg]) {
788 memprintf(err, "missing length value");
Christopher Faulet1337b322020-01-14 14:50:55 +0100789 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200790 return ACT_RET_PRS_ERR;
791 }
792 /* we copy the table name for now, it will be resolved later */
793 len = atoi(args[cur_arg]);
794 if (len <= 0) {
795 memprintf(err, "length must be > 0");
Christopher Faulet1337b322020-01-14 14:50:55 +0100796 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200797 return ACT_RET_PRS_ERR;
798 }
799 cur_arg++;
800
Willy Tarreau79e57332018-10-02 16:01:16 +0200801 hdr = calloc(1, sizeof(*hdr));
802 hdr->next = px->req_cap;
803 hdr->name = NULL; /* not a header capture */
804 hdr->namelen = 0;
805 hdr->len = len;
806 hdr->pool = create_pool("caphdr", hdr->len + 1, MEM_F_SHARED);
807 hdr->index = px->nb_req_cap++;
808
809 px->req_cap = hdr;
810 px->to_log |= LW_REQHDR;
811
812 rule->action = ACT_CUSTOM;
813 rule->action_ptr = http_action_req_capture;
Christopher Faulet2eb53962020-01-14 14:47:34 +0100814 rule->release_ptr = release_http_capture;
Willy Tarreau79e57332018-10-02 16:01:16 +0200815 rule->arg.cap.expr = expr;
816 rule->arg.cap.hdr = hdr;
817 }
818
819 else if (strcmp(args[cur_arg], "id") == 0) {
820 int id;
821 char *error;
822
823 cur_arg++;
824
825 if (!args[cur_arg]) {
826 memprintf(err, "missing id value");
Christopher Faulet1337b322020-01-14 14:50:55 +0100827 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200828 return ACT_RET_PRS_ERR;
829 }
830
831 id = strtol(args[cur_arg], &error, 10);
832 if (*error != '\0') {
833 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
Christopher Faulet1337b322020-01-14 14:50:55 +0100834 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200835 return ACT_RET_PRS_ERR;
836 }
837 cur_arg++;
838
839 px->conf.args.ctx = ARGC_CAP;
840
841 rule->action = ACT_CUSTOM;
842 rule->action_ptr = http_action_req_capture_by_id;
843 rule->check_ptr = check_http_req_capture;
Christopher Faulet2eb53962020-01-14 14:47:34 +0100844 rule->release_ptr = release_http_capture;
Willy Tarreau79e57332018-10-02 16:01:16 +0200845 rule->arg.capid.expr = expr;
846 rule->arg.capid.idx = id;
847 }
848
849 else {
850 memprintf(err, "expects 'len' or 'id', found '%s'", args[cur_arg]);
Christopher Faulet1337b322020-01-14 14:50:55 +0100851 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200852 return ACT_RET_PRS_ERR;
853 }
854
855 *orig_arg = cur_arg;
856 return ACT_RET_PRS_OK;
857}
858
859/* This function executes the "capture" action and store the result in a
860 * capture slot if exists. It executes a fetch expression, turns the result
861 * into a string and puts it in a capture slot. It always returns 1. If an
862 * error occurs the action is cancelled, but the rule processing continues.
863 */
864static enum act_return http_action_res_capture_by_id(struct act_rule *rule, struct proxy *px,
865 struct session *sess, struct stream *s, int flags)
866{
867 struct sample *key;
868 struct cap_hdr *h;
869 char **cap = s->res_cap;
870 struct proxy *fe = strm_fe(s);
871 int len;
872 int i;
873
874 /* Look for the original configuration. */
875 for (h = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
876 h != NULL && i != rule->arg.capid.idx ;
877 i--, h = h->next);
878 if (!h)
879 return ACT_RET_CONT;
880
881 key = sample_fetch_as_type(s->be, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL, rule->arg.capid.expr, SMP_T_STR);
882 if (!key)
883 return ACT_RET_CONT;
884
885 if (cap[h->index] == NULL)
886 cap[h->index] = pool_alloc(h->pool);
887
888 if (cap[h->index] == NULL) /* no more capture memory */
889 return ACT_RET_CONT;
890
891 len = key->data.u.str.data;
892 if (len > h->len)
893 len = h->len;
894
895 memcpy(cap[h->index], key->data.u.str.area, len);
896 cap[h->index][len] = 0;
897 return ACT_RET_CONT;
898}
899
900/* Check an "http-response capture" action.
901 *
902 * The function returns 1 in success case, otherwise, it returns 0 and err is
903 * filled.
904 */
905static int check_http_res_capture(struct act_rule *rule, struct proxy *px, char **err)
906{
907 if (rule->action_ptr != http_action_res_capture_by_id)
908 return 1;
909
Tim Duesterhusf3f4aa02020-07-03 13:43:42 +0200910 /* capture slots can only be declared in frontends, so we can't check their
911 * existence in backends at configuration parsing step
912 */
913 if (px->cap & PR_CAP_FE && rule->arg.capid.idx >= px->nb_rsp_cap) {
Willy Tarreau79e57332018-10-02 16:01:16 +0200914 memprintf(err, "unable to find capture id '%d' referenced by http-response capture rule",
915 rule->arg.capid.idx);
916 return 0;
917 }
918
919 return 1;
920}
921
922/* parse an "http-response capture" action. It takes a single argument which is
923 * a sample fetch expression. It stores the expression into arg->act.p[0] and
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -0700924 * the allocated hdr_cap struct of the preallocated id into arg->act.p[1].
Willy Tarreau79e57332018-10-02 16:01:16 +0200925 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
926 */
927static enum act_parse_ret parse_http_res_capture(const char **args, int *orig_arg, struct proxy *px,
928 struct act_rule *rule, char **err)
929{
930 struct sample_expr *expr;
931 int cur_arg;
932 int id;
933 char *error;
934
935 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
936 if (strcmp(args[cur_arg], "if") == 0 ||
937 strcmp(args[cur_arg], "unless") == 0)
938 break;
939
940 if (cur_arg < *orig_arg + 3) {
941 memprintf(err, "expects <expression> id <idx>");
942 return ACT_RET_PRS_ERR;
943 }
944
945 cur_arg = *orig_arg;
Willy Tarreaue3b57bf2020-02-14 16:50:14 +0100946 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 +0200947 if (!expr)
948 return ACT_RET_PRS_ERR;
949
950 if (!(expr->fetch->val & SMP_VAL_FE_HRS_HDR)) {
951 memprintf(err,
952 "fetch method '%s' extracts information from '%s', none of which is available here",
953 args[cur_arg-1], sample_src_names(expr->fetch->use));
Christopher Faulet1337b322020-01-14 14:50:55 +0100954 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200955 return ACT_RET_PRS_ERR;
956 }
957
958 if (!args[cur_arg] || !*args[cur_arg]) {
959 memprintf(err, "expects 'id'");
Christopher Faulet1337b322020-01-14 14:50:55 +0100960 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200961 return ACT_RET_PRS_ERR;
962 }
963
964 if (strcmp(args[cur_arg], "id") != 0) {
965 memprintf(err, "expects 'id', found '%s'", args[cur_arg]);
Christopher Faulet1337b322020-01-14 14:50:55 +0100966 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200967 return ACT_RET_PRS_ERR;
968 }
969
970 cur_arg++;
971
972 if (!args[cur_arg]) {
973 memprintf(err, "missing id value");
Christopher Faulet1337b322020-01-14 14:50:55 +0100974 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200975 return ACT_RET_PRS_ERR;
976 }
977
978 id = strtol(args[cur_arg], &error, 10);
979 if (*error != '\0') {
980 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
Christopher Faulet1337b322020-01-14 14:50:55 +0100981 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200982 return ACT_RET_PRS_ERR;
983 }
984 cur_arg++;
985
986 px->conf.args.ctx = ARGC_CAP;
987
988 rule->action = ACT_CUSTOM;
989 rule->action_ptr = http_action_res_capture_by_id;
990 rule->check_ptr = check_http_res_capture;
Christopher Faulet2eb53962020-01-14 14:47:34 +0100991 rule->release_ptr = release_http_capture;
Willy Tarreau79e57332018-10-02 16:01:16 +0200992 rule->arg.capid.expr = expr;
993 rule->arg.capid.idx = id;
994
995 *orig_arg = cur_arg;
996 return ACT_RET_PRS_OK;
997}
998
Christopher Faulet81e20172019-12-12 16:40:30 +0100999/* Parse a "allow" action for a request or a response rule. It takes no argument. It
1000 * returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
1001 */
1002static enum act_parse_ret parse_http_allow(const char **args, int *orig_arg, struct proxy *px,
1003 struct act_rule *rule, char **err)
1004{
1005 rule->action = ACT_ACTION_ALLOW;
Christopher Faulet245cf792019-12-18 14:58:12 +01001006 rule->flags |= ACT_FLAG_FINAL;
Christopher Faulet81e20172019-12-12 16:40:30 +01001007 return ACT_RET_PRS_OK;
1008}
1009
Christopher Faulete0fca292020-01-13 21:49:03 +01001010/* Parse "deny" or "tarpit" actions for a request rule or "deny" action for a
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001011 * response rule. It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on
1012 * error. It relies on http_parse_http_reply() to set
1013 * <.arg.http_reply>.
Christopher Faulet81e20172019-12-12 16:40:30 +01001014 */
Christopher Faulete0fca292020-01-13 21:49:03 +01001015static enum act_parse_ret parse_http_deny(const char **args, int *orig_arg, struct proxy *px,
1016 struct act_rule *rule, char **err)
Christopher Faulet81e20172019-12-12 16:40:30 +01001017{
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001018 int default_status;
1019 int cur_arg, arg = 0;
Christopher Faulet81e20172019-12-12 16:40:30 +01001020
1021 cur_arg = *orig_arg;
Christopher Faulete0fca292020-01-13 21:49:03 +01001022 if (rule->from == ACT_F_HTTP_REQ) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001023 if (strcmp(args[cur_arg - 1], "tarpit") == 0) {
Christopher Faulete0fca292020-01-13 21:49:03 +01001024 rule->action = ACT_HTTP_REQ_TARPIT;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001025 default_status = 500;
Christopher Faulet81e20172019-12-12 16:40:30 +01001026 }
Christopher Faulete0fca292020-01-13 21:49:03 +01001027 else {
1028 rule->action = ACT_ACTION_DENY;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001029 default_status = 403;
Christopher Faulet81e20172019-12-12 16:40:30 +01001030 }
Christopher Faulet81e20172019-12-12 16:40:30 +01001031 }
Christopher Faulete0fca292020-01-13 21:49:03 +01001032 else {
Christopher Faulet554c0eb2020-01-14 12:00:28 +01001033 rule->action = ACT_ACTION_DENY;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001034 default_status = 502;
Christopher Faulete0fca292020-01-13 21:49:03 +01001035 }
Christopher Faulet040c8cd2020-01-13 16:43:45 +01001036
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001037 /* If no args or only a deny_status specified, fallback on the legacy
1038 * mode and use default error files despite the fact that
1039 * default-errorfiles is not used. Otherwise, parse an http reply.
1040 */
Christopher Faulet040c8cd2020-01-13 16:43:45 +01001041
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001042 /* Prepare parsing of log-format strings */
1043 px->conf.args.ctx = ((rule->from == ACT_F_HTTP_REQ) ? ARGC_HRQ : ARGC_HRS);
Christopher Faulet554c0eb2020-01-14 12:00:28 +01001044
Christopher Faulet9467f182020-06-30 09:32:01 +02001045 if (!*(args[cur_arg]) || strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001046 rule->arg.http_reply = http_parse_http_reply((const char *[]){"default-errorfiles", ""}, &arg, px, default_status, err);
1047 goto end;
Christopher Faulet554c0eb2020-01-14 12:00:28 +01001048 }
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001049
1050 if (strcmp(args[cur_arg], "deny_status") == 0) {
Christopher Faulet9467f182020-06-30 09:32:01 +02001051 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 +02001052 rule->arg.http_reply = http_parse_http_reply((const char *[]){"status", args[cur_arg+1], "default-errorfiles", ""},
1053 &arg, px, default_status, err);
1054 *orig_arg += 2;
1055 goto end;
Christopher Faulet554c0eb2020-01-14 12:00:28 +01001056 }
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001057 args[cur_arg] += 5; /* skip "deny_" for the parsing */
Christopher Faulet554c0eb2020-01-14 12:00:28 +01001058 }
1059
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001060 rule->arg.http_reply = http_parse_http_reply(args, orig_arg, px, default_status, err);
Christopher Faulet554c0eb2020-01-14 12:00:28 +01001061
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001062 end:
1063 if (!rule->arg.http_reply)
1064 return ACT_RET_PRS_ERR;
1065
1066 rule->flags |= ACT_FLAG_FINAL;
1067 rule->check_ptr = check_act_http_reply;
1068 rule->release_ptr = release_act_http_reply;
Christopher Faulet81e20172019-12-12 16:40:30 +01001069 return ACT_RET_PRS_OK;
1070}
1071
Christopher Fauletb3048832020-05-27 15:26:43 +02001072
1073/* This function executes a auth action. It builds an 401/407 HTX message using
1074 * the corresponding proxy's error message. On success, it returns
1075 * ACT_RET_ABRT. If an error occurs ACT_RET_ERR is returned.
1076 */
1077static enum act_return http_action_auth(struct act_rule *rule, struct proxy *px,
1078 struct session *sess, struct stream *s, int flags)
1079{
1080 struct channel *req = &s->req;
1081 struct channel *res = &s->res;
1082 struct htx *htx = htx_from_buf(&res->buf);
1083 struct http_reply *reply;
1084 const char *auth_realm;
1085 struct http_hdr_ctx ctx;
1086 struct ist hdr;
1087
1088 /* Auth might be performed on regular http-req rules as well as on stats */
1089 auth_realm = rule->arg.http.str.ptr;
1090 if (!auth_realm) {
1091 if (px->uri_auth && s->current_rule_list == &px->uri_auth->http_req_rules)
1092 auth_realm = STATS_DEFAULT_REALM;
1093 else
1094 auth_realm = px->id;
1095 }
1096
1097 if (!(s->txn->flags & TX_USE_PX_CONN)) {
1098 s->txn->status = 401;
1099 hdr = ist("WWW-Authenticate");
1100 }
1101 else {
1102 s->txn->status = 407;
1103 hdr = ist("Proxy-Authenticate");
1104 }
1105 reply = http_error_message(s);
1106 channel_htx_truncate(res, htx);
1107
1108 if (chunk_printf(&trash, "Basic realm=\"%s\"", auth_realm) == -1)
1109 goto fail;
1110
1111 /* Write the generic 40x message */
1112 if (http_reply_to_htx(s, htx, reply) == -1)
1113 goto fail;
1114
1115 /* Remove all existing occurrences of the XXX-Authenticate header */
1116 ctx.blk = NULL;
1117 while (http_find_header(htx, hdr, &ctx, 1))
1118 http_remove_header(htx, &ctx);
1119
1120 /* Now a the right XXX-Authenticate header */
1121 if (!http_add_header(htx, hdr, ist2(b_orig(&trash), b_data(&trash))))
1122 goto fail;
1123
1124 /* Finally forward the reply */
1125 htx_to_buf(htx, &res->buf);
1126 if (!http_forward_proxy_resp(s, 1))
1127 goto fail;
1128
1129 /* Note: Only eval on the request */
1130 s->logs.tv_request = now;
1131 req->analysers &= AN_REQ_FLT_END;
1132
1133 if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Willy Tarreau4781b152021-04-06 13:53:36 +02001134 _HA_ATOMIC_INC(&s->sess->fe->fe_counters.intercepted_req);
Christopher Fauletb3048832020-05-27 15:26:43 +02001135
1136 if (!(s->flags & SF_ERR_MASK))
1137 s->flags |= SF_ERR_LOCAL;
1138 if (!(s->flags & SF_FINST_MASK))
1139 s->flags |= SF_FINST_R;
1140
1141 stream_inc_http_err_ctr(s);
1142 return ACT_RET_ABRT;
1143
1144 fail:
1145 /* If an error occurred, remove the incomplete HTTP response from the
1146 * buffer */
1147 channel_htx_truncate(res, htx);
1148 return ACT_RET_ERR;
1149}
1150
Christopher Faulet81e20172019-12-12 16:40:30 +01001151/* Parse a "auth" action. It may take 2 optional arguments to define a "realm"
1152 * parameter. It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
1153 */
1154static enum act_parse_ret parse_http_auth(const char **args, int *orig_arg, struct proxy *px,
1155 struct act_rule *rule, char **err)
1156{
1157 int cur_arg;
1158
Christopher Fauletb3048832020-05-27 15:26:43 +02001159 rule->action = ACT_CUSTOM;
Christopher Faulet245cf792019-12-18 14:58:12 +01001160 rule->flags |= ACT_FLAG_FINAL;
Christopher Fauletb3048832020-05-27 15:26:43 +02001161 rule->action_ptr = http_action_auth;
Christopher Faulet2eb53962020-01-14 14:47:34 +01001162 rule->release_ptr = release_http_action;
Christopher Faulet81e20172019-12-12 16:40:30 +01001163
1164 cur_arg = *orig_arg;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001165 if (strcmp(args[cur_arg], "realm") == 0) {
Christopher Faulet81e20172019-12-12 16:40:30 +01001166 cur_arg++;
1167 if (!*args[cur_arg]) {
1168 memprintf(err, "missing realm value.\n");
1169 return ACT_RET_PRS_ERR;
1170 }
Christopher Faulet96bff762019-12-17 13:46:18 +01001171 rule->arg.http.str.ptr = strdup(args[cur_arg]);
1172 rule->arg.http.str.len = strlen(rule->arg.http.str.ptr);
Christopher Faulet81e20172019-12-12 16:40:30 +01001173 cur_arg++;
1174 }
1175
Christopher Fauletc20b3712020-01-27 15:51:56 +01001176 LIST_INIT(&rule->arg.http.fmt);
Christopher Faulet81e20172019-12-12 16:40:30 +01001177 *orig_arg = cur_arg;
1178 return ACT_RET_PRS_OK;
1179}
1180
1181/* Parse a "set-nice" action. It takes the nice value as argument. It returns
1182 * ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
1183 */
1184static enum act_parse_ret parse_http_set_nice(const char **args, int *orig_arg, struct proxy *px,
1185 struct act_rule *rule, char **err)
1186{
1187 int cur_arg;
1188
1189 rule->action = ACT_HTTP_SET_NICE;
1190
1191 cur_arg = *orig_arg;
1192 if (!*args[cur_arg]) {
1193 memprintf(err, "expects exactly 1 argument (integer value)");
1194 return ACT_RET_PRS_ERR;
1195 }
Christopher Faulet96bff762019-12-17 13:46:18 +01001196 rule->arg.http.i = atoi(args[cur_arg]);
1197 if (rule->arg.http.i < -1024)
1198 rule->arg.http.i = -1024;
1199 else if (rule->arg.http.i > 1024)
1200 rule->arg.http.i = 1024;
Christopher Faulet81e20172019-12-12 16:40:30 +01001201
Christopher Fauletc20b3712020-01-27 15:51:56 +01001202 LIST_INIT(&rule->arg.http.fmt);
Christopher Faulet81e20172019-12-12 16:40:30 +01001203 *orig_arg = cur_arg + 1;
1204 return ACT_RET_PRS_OK;
1205}
1206
1207/* Parse a "set-tos" action. It takes the TOS value as argument. It returns
1208 * ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
1209 */
1210static enum act_parse_ret parse_http_set_tos(const char **args, int *orig_arg, struct proxy *px,
1211 struct act_rule *rule, char **err)
1212{
1213#ifdef IP_TOS
1214 char *endp;
1215 int cur_arg;
1216
1217 rule->action = ACT_HTTP_SET_TOS;
1218
1219 cur_arg = *orig_arg;
1220 if (!*args[cur_arg]) {
1221 memprintf(err, "expects exactly 1 argument (integer/hex value)");
1222 return ACT_RET_PRS_ERR;
1223 }
Christopher Faulet96bff762019-12-17 13:46:18 +01001224 rule->arg.http.i = strtol(args[cur_arg], &endp, 0);
Christopher Faulet81e20172019-12-12 16:40:30 +01001225 if (endp && *endp != '\0') {
1226 memprintf(err, "invalid character starting at '%s' (integer/hex value expected)", endp);
1227 return ACT_RET_PRS_ERR;
1228 }
1229
Christopher Fauletc20b3712020-01-27 15:51:56 +01001230 LIST_INIT(&rule->arg.http.fmt);
Christopher Faulet81e20172019-12-12 16:40:30 +01001231 *orig_arg = cur_arg + 1;
1232 return ACT_RET_PRS_OK;
1233#else
1234 memprintf(err, "not supported on this platform (IP_TOS undefined)");
1235 return ACT_RET_PRS_ERR;
1236#endif
1237}
1238
1239/* Parse a "set-mark" action. It takes the MARK value as argument. It returns
1240 * ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
1241 */
1242static enum act_parse_ret parse_http_set_mark(const char **args, int *orig_arg, struct proxy *px,
1243 struct act_rule *rule, char **err)
1244{
1245#ifdef SO_MARK
1246 char *endp;
1247 int cur_arg;
1248
1249 rule->action = ACT_HTTP_SET_MARK;
1250
1251 cur_arg = *orig_arg;
1252 if (!*args[cur_arg]) {
1253 memprintf(err, "expects exactly 1 argument (integer/hex value)");
1254 return ACT_RET_PRS_ERR;
1255 }
Christopher Faulet96bff762019-12-17 13:46:18 +01001256 rule->arg.http.i = strtoul(args[cur_arg], &endp, 0);
Christopher Faulet81e20172019-12-12 16:40:30 +01001257 if (endp && *endp != '\0') {
1258 memprintf(err, "invalid character starting at '%s' (integer/hex value expected)", endp);
1259 return ACT_RET_PRS_ERR;
1260 }
1261
Christopher Fauletc20b3712020-01-27 15:51:56 +01001262 LIST_INIT(&rule->arg.http.fmt);
Christopher Faulet81e20172019-12-12 16:40:30 +01001263 *orig_arg = cur_arg + 1;
1264 global.last_checks |= LSTCHK_NETADM;
1265 return ACT_RET_PRS_OK;
1266#else
1267 memprintf(err, "not supported on this platform (SO_MARK undefined)");
1268 return ACT_RET_PRS_ERR;
1269#endif
1270}
1271
1272/* Parse a "set-log-level" action. It takes the level value as argument. It
1273 * returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
1274 */
1275static enum act_parse_ret parse_http_set_log_level(const char **args, int *orig_arg, struct proxy *px,
1276 struct act_rule *rule, char **err)
1277{
1278 int cur_arg;
1279
1280 rule->action = ACT_HTTP_SET_LOGL;
1281
1282 cur_arg = *orig_arg;
1283 if (!*args[cur_arg]) {
1284 bad_log_level:
1285 memprintf(err, "expects exactly 1 argument (log level name or 'silent')");
1286 return ACT_RET_PRS_ERR;
1287 }
1288 if (strcmp(args[cur_arg], "silent") == 0)
Christopher Faulet96bff762019-12-17 13:46:18 +01001289 rule->arg.http.i = -1;
1290 else if ((rule->arg.http.i = get_log_level(args[cur_arg]) + 1) == 0)
Christopher Faulet81e20172019-12-12 16:40:30 +01001291 goto bad_log_level;
1292
Christopher Fauletc20b3712020-01-27 15:51:56 +01001293 LIST_INIT(&rule->arg.http.fmt);
Christopher Faulet81e20172019-12-12 16:40:30 +01001294 *orig_arg = cur_arg + 1;
1295 return ACT_RET_PRS_OK;
1296}
1297
Christopher Faulet91b3ec12020-01-17 22:30:06 +01001298/* This function executes a early-hint action. It adds an HTTP Early Hint HTTP
1299 * 103 response header with <.arg.http.str> name and with a value built
1300 * according to <.arg.http.fmt> log line format. If it is the first early-hint
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001301 * rule of series, the 103 response start-line is added first. At the end, if
Christopher Faulet91b3ec12020-01-17 22:30:06 +01001302 * the next rule is not an early-hint rule or if it is the last rule, the EOH
1303 * block is added to terminate the response. On success, it returns
1304 * ACT_RET_CONT. If an error occurs while soft rewrites are enabled, the action
1305 * is canceled, but the rule processing continue. Otherwsize ACT_RET_ERR is
1306 * returned.
1307 */
1308static enum act_return http_action_early_hint(struct act_rule *rule, struct proxy *px,
1309 struct session *sess, struct stream *s, int flags)
1310{
1311 struct act_rule *prev_rule, *next_rule;
1312 struct channel *res = &s->res;
1313 struct htx *htx = htx_from_buf(&res->buf);
1314 struct buffer *value = alloc_trash_chunk();
1315 enum act_return ret = ACT_RET_CONT;
1316
1317 if (!(s->txn->req.flags & HTTP_MSGF_VER_11))
1318 goto leave;
1319
1320 if (!value) {
1321 if (!(s->flags & SF_ERR_MASK))
1322 s->flags |= SF_ERR_RESOURCE;
1323 goto error;
1324 }
1325
1326 /* get previous and next rules */
1327 prev_rule = LIST_PREV(&rule->list, typeof(rule), list);
1328 next_rule = LIST_NEXT(&rule->list, typeof(rule), list);
1329
1330 /* if no previous rule or previous rule is not early-hint, start a new response. Otherwise,
1331 * continue to add link to a previously started response */
1332 if (&prev_rule->list == s->current_rule_list || prev_rule->action_ptr != http_action_early_hint) {
1333 struct htx_sl *sl;
1334 unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|
1335 HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
1336
1337 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
1338 ist("HTTP/1.1"), ist("103"), ist("Early Hints"));
1339 if (!sl)
1340 goto error;
1341 sl->info.res.status = 103;
1342 }
1343
1344 /* Add the HTTP Early Hint HTTP 103 response heade */
1345 value->data = build_logline(s, b_tail(value), b_room(value), &rule->arg.http.fmt);
1346 if (!htx_add_header(htx, rule->arg.http.str, ist2(b_head(value), b_data(value))))
1347 goto error;
1348
1349 /* if it is the last rule or the next one is not an early-hint, terminate the current
1350 * response. */
1351 if (&next_rule->list == s->current_rule_list || next_rule->action_ptr != http_action_early_hint) {
Christopher Faulet91b3ec12020-01-17 22:30:06 +01001352 if (!htx_add_endof(htx, HTX_BLK_EOH)) {
1353 /* If an error occurred during an Early-hint rule,
1354 * remove the incomplete HTTP 103 response from the
1355 * buffer */
1356 goto error;
1357 }
1358
Christopher Fauleta72a7e42020-01-28 09:28:11 +01001359 if (!http_forward_proxy_resp(s, 0))
1360 goto error;
Christopher Faulet91b3ec12020-01-17 22:30:06 +01001361 }
1362
1363 leave:
1364 free_trash_chunk(value);
1365 return ret;
1366
1367 error:
1368 /* If an error occurred during an Early-hint rule, remove the incomplete
1369 * HTTP 103 response from the buffer */
1370 channel_htx_truncate(res, htx);
1371 ret = ACT_RET_ERR;
1372 goto leave;
1373}
1374
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001375/* This function executes a set-header or add-header actions. It builds a string
1376 * in the trash from the specified format string. It finds the action to be
1377 * performed in <.action>, previously filled by function parse_set_header(). The
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001378 * replacement action is executed by the function http_action_set_header(). On
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001379 * success, it returns ACT_RET_CONT. If an error occurs while soft rewrites are
1380 * enabled, the action is canceled, but the rule processing continue. Otherwsize
1381 * ACT_RET_ERR is returned.
1382 */
1383static enum act_return http_action_set_header(struct act_rule *rule, struct proxy *px,
1384 struct session *sess, struct stream *s, int flags)
1385{
Christopher Faulet91e31d82020-01-24 15:37:13 +01001386 struct http_msg *msg = ((rule->from == ACT_F_HTTP_REQ) ? &s->txn->req : &s->txn->rsp);
1387 struct htx *htx = htxbuf(&msg->chn->buf);
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001388 enum act_return ret = ACT_RET_CONT;
1389 struct buffer *replace;
1390 struct http_hdr_ctx ctx;
1391 struct ist n, v;
1392
1393 replace = alloc_trash_chunk();
1394 if (!replace)
1395 goto fail_alloc;
1396
1397 replace->data = build_logline(s, replace->area, replace->size, &rule->arg.http.fmt);
1398 n = rule->arg.http.str;
1399 v = ist2(replace->area, replace->data);
1400
1401 if (rule->action == 0) { // set-header
1402 /* remove all occurrences of the header */
1403 ctx.blk = NULL;
1404 while (http_find_header(htx, n, &ctx, 1))
1405 http_remove_header(htx, &ctx);
1406 }
1407
1408 /* Now add header */
1409 if (!http_add_header(htx, n, v))
1410 goto fail_rewrite;
1411
1412 leave:
1413 free_trash_chunk(replace);
1414 return ret;
1415
1416 fail_alloc:
1417 if (!(s->flags & SF_ERR_MASK))
1418 s->flags |= SF_ERR_RESOURCE;
1419 ret = ACT_RET_ERR;
1420 goto leave;
1421
1422 fail_rewrite:
Willy Tarreau4781b152021-04-06 13:53:36 +02001423 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_rewrites);
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001424 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +02001425 _HA_ATOMIC_INC(&s->be->be_counters.failed_rewrites);
William Lallemand36119de2021-03-08 15:26:48 +01001426 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001427 _HA_ATOMIC_INC(&sess->listener->counters->failed_rewrites);
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001428 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001429 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_rewrites);
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001430
Christopher Faulet333bf8c2020-01-22 14:38:05 +01001431 if (!(msg->flags & HTTP_MSGF_SOFT_RW)) {
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001432 ret = ACT_RET_ERR;
Christopher Faulet333bf8c2020-01-22 14:38:05 +01001433 if (!(s->flags & SF_ERR_MASK))
1434 s->flags |= SF_ERR_PRXCOND;
1435 }
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001436 goto leave;
1437}
1438
Christopher Faulet81e20172019-12-12 16:40:30 +01001439/* Parse a "set-header", "add-header" or "early-hint" actions. It takes an
1440 * header name and a log-format string as arguments. It returns ACT_RET_PRS_OK
1441 * on success, ACT_RET_PRS_ERR on error.
1442 *
1443 * Note: same function is used for the request and the response. However
1444 * "early-hint" rules are only supported for request rules.
1445 */
1446static enum act_parse_ret parse_http_set_header(const char **args, int *orig_arg, struct proxy *px,
1447 struct act_rule *rule, char **err)
1448{
Christopher Faulet81e20172019-12-12 16:40:30 +01001449 int cap, cur_arg;
1450
Christopher Faulet91b3ec12020-01-17 22:30:06 +01001451 if (args[*orig_arg-1][0] == 'e') {
1452 rule->action = ACT_CUSTOM;
1453 rule->action_ptr = http_action_early_hint;
1454 }
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001455 else {
1456 if (args[*orig_arg-1][0] == 's')
1457 rule->action = 0; // set-header
1458 else
1459 rule->action = 1; // add-header
1460 rule->action_ptr = http_action_set_header;
1461 }
Christopher Faulet2eb53962020-01-14 14:47:34 +01001462 rule->release_ptr = release_http_action;
Christopher Faulet81e20172019-12-12 16:40:30 +01001463
1464 cur_arg = *orig_arg;
1465 if (!*args[cur_arg] || !*args[cur_arg+1]) {
1466 memprintf(err, "expects exactly 2 arguments");
1467 return ACT_RET_PRS_ERR;
1468 }
1469
Christopher Faulet81e20172019-12-12 16:40:30 +01001470
Christopher Faulet96bff762019-12-17 13:46:18 +01001471 rule->arg.http.str.ptr = strdup(args[cur_arg]);
1472 rule->arg.http.str.len = strlen(rule->arg.http.str.ptr);
1473 LIST_INIT(&rule->arg.http.fmt);
Christopher Faulet81e20172019-12-12 16:40:30 +01001474
1475 if (rule->from == ACT_F_HTTP_REQ) {
1476 px->conf.args.ctx = ARGC_HRQ;
1477 cap = (px->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR;
1478 }
1479 else{
1480 px->conf.args.ctx = ARGC_HRS;
1481 cap = (px->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR;
1482 }
1483
1484 cur_arg++;
Christopher Faulet1337b322020-01-14 14:50:55 +01001485 if (!parse_logformat_string(args[cur_arg], px, &rule->arg.http.fmt, LOG_OPT_HTTP, cap, err)) {
Tim Duesterhused526372020-03-05 17:56:33 +01001486 istfree(&rule->arg.http.str);
Christopher Faulet81e20172019-12-12 16:40:30 +01001487 return ACT_RET_PRS_ERR;
Christopher Faulet1337b322020-01-14 14:50:55 +01001488 }
Christopher Faulet81e20172019-12-12 16:40:30 +01001489
1490 free(px->conf.lfs_file);
1491 px->conf.lfs_file = strdup(px->conf.args.file);
1492 px->conf.lfs_line = px->conf.args.line;
1493
1494 *orig_arg = cur_arg + 1;
1495 return ACT_RET_PRS_OK;
1496}
1497
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001498/* This function executes a replace-header or replace-value actions. It
1499 * builds a string in the trash from the specified format string. It finds
1500 * the action to be performed in <.action>, previously filled by function
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001501 * parse_replace_header(). The replacement action is executed by the function
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001502 * http_action_replace_header(). On success, it returns ACT_RET_CONT. If an error
1503 * occurs while soft rewrites are enabled, the action is canceled, but the rule
1504 * processing continue. Otherwsize ACT_RET_ERR is returned.
1505 */
1506static enum act_return http_action_replace_header(struct act_rule *rule, struct proxy *px,
1507 struct session *sess, struct stream *s, int flags)
1508{
Christopher Faulet91e31d82020-01-24 15:37:13 +01001509 struct http_msg *msg = ((rule->from == ACT_F_HTTP_REQ) ? &s->txn->req : &s->txn->rsp);
1510 struct htx *htx = htxbuf(&msg->chn->buf);
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001511 enum act_return ret = ACT_RET_CONT;
1512 struct buffer *replace;
1513 int r;
1514
1515 replace = alloc_trash_chunk();
1516 if (!replace)
1517 goto fail_alloc;
1518
1519 replace->data = build_logline(s, replace->area, replace->size, &rule->arg.http.fmt);
1520
1521 r = http_replace_hdrs(s, htx, rule->arg.http.str, replace->area, rule->arg.http.re, (rule->action == 0));
1522 if (r == -1)
1523 goto fail_rewrite;
1524
1525 leave:
1526 free_trash_chunk(replace);
1527 return ret;
1528
1529 fail_alloc:
1530 if (!(s->flags & SF_ERR_MASK))
1531 s->flags |= SF_ERR_RESOURCE;
1532 ret = ACT_RET_ERR;
1533 goto leave;
1534
1535 fail_rewrite:
Willy Tarreau4781b152021-04-06 13:53:36 +02001536 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_rewrites);
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001537 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +02001538 _HA_ATOMIC_INC(&s->be->be_counters.failed_rewrites);
William Lallemand36119de2021-03-08 15:26:48 +01001539 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001540 _HA_ATOMIC_INC(&sess->listener->counters->failed_rewrites);
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001541 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001542 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_rewrites);
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001543
Christopher Faulet333bf8c2020-01-22 14:38:05 +01001544 if (!(msg->flags & HTTP_MSGF_SOFT_RW)) {
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001545 ret = ACT_RET_ERR;
Christopher Faulet333bf8c2020-01-22 14:38:05 +01001546 if (!(s->flags & SF_ERR_MASK))
1547 s->flags |= SF_ERR_PRXCOND;
1548 }
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001549 goto leave;
1550}
1551
Christopher Faulet81e20172019-12-12 16:40:30 +01001552/* Parse a "replace-header" or "replace-value" actions. It takes an header name,
1553 * a regex and replacement string as arguments. It returns ACT_RET_PRS_OK on
1554 * success, ACT_RET_PRS_ERR on error.
1555 */
1556static enum act_parse_ret parse_http_replace_header(const char **args, int *orig_arg, struct proxy *px,
1557 struct act_rule *rule, char **err)
1558{
1559 int cap, cur_arg;
1560
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001561 if (args[*orig_arg-1][8] == 'h')
1562 rule->action = 0; // replace-header
1563 else
1564 rule->action = 1; // replace-value
1565 rule->action_ptr = http_action_replace_header;
Christopher Faulet2eb53962020-01-14 14:47:34 +01001566 rule->release_ptr = release_http_action;
Christopher Faulet81e20172019-12-12 16:40:30 +01001567
1568 cur_arg = *orig_arg;
1569 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2]) {
1570 memprintf(err, "expects exactly 3 arguments");
1571 return ACT_RET_PRS_ERR;
1572 }
1573
Christopher Faulet96bff762019-12-17 13:46:18 +01001574 rule->arg.http.str.ptr = strdup(args[cur_arg]);
1575 rule->arg.http.str.len = strlen(rule->arg.http.str.ptr);
1576 LIST_INIT(&rule->arg.http.fmt);
Christopher Faulet81e20172019-12-12 16:40:30 +01001577
1578 cur_arg++;
Christopher Faulet1337b322020-01-14 14:50:55 +01001579 if (!(rule->arg.http.re = regex_comp(args[cur_arg], 1, 1, err))) {
Tim Duesterhused526372020-03-05 17:56:33 +01001580 istfree(&rule->arg.http.str);
Christopher Faulet81e20172019-12-12 16:40:30 +01001581 return ACT_RET_PRS_ERR;
Christopher Faulet1337b322020-01-14 14:50:55 +01001582 }
Christopher Faulet81e20172019-12-12 16:40:30 +01001583
1584 if (rule->from == ACT_F_HTTP_REQ) {
1585 px->conf.args.ctx = ARGC_HRQ;
1586 cap = (px->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR;
1587 }
1588 else{
1589 px->conf.args.ctx = ARGC_HRS;
1590 cap = (px->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR;
1591 }
1592
1593 cur_arg++;
Christopher Faulet1337b322020-01-14 14:50:55 +01001594 if (!parse_logformat_string(args[cur_arg], px, &rule->arg.http.fmt, LOG_OPT_HTTP, cap, err)) {
Tim Duesterhused526372020-03-05 17:56:33 +01001595 istfree(&rule->arg.http.str);
Christopher Faulet1337b322020-01-14 14:50:55 +01001596 regex_free(rule->arg.http.re);
Christopher Faulet81e20172019-12-12 16:40:30 +01001597 return ACT_RET_PRS_ERR;
Christopher Faulet1337b322020-01-14 14:50:55 +01001598 }
Christopher Faulet81e20172019-12-12 16:40:30 +01001599
1600 free(px->conf.lfs_file);
1601 px->conf.lfs_file = strdup(px->conf.args.file);
1602 px->conf.lfs_line = px->conf.args.line;
1603
1604 *orig_arg = cur_arg + 1;
1605 return ACT_RET_PRS_OK;
1606}
1607
Maciej Zdebebdd4c52020-11-20 13:58:48 +00001608/* This function executes a del-header action with selected matching mode for
1609 * header name. It finds the matching method to be performed in <.action>, previously
1610 * filled by function parse_http_del_header(). On success, it returns ACT_RET_CONT.
1611 * Otherwise ACT_RET_ERR is returned.
1612 */
1613static enum act_return http_action_del_header(struct act_rule *rule, struct proxy *px,
1614 struct session *sess, struct stream *s, int flags)
1615{
1616 struct http_hdr_ctx ctx;
1617 struct http_msg *msg = ((rule->from == ACT_F_HTTP_REQ) ? &s->txn->req : &s->txn->rsp);
1618 struct htx *htx = htxbuf(&msg->chn->buf);
1619 enum act_return ret = ACT_RET_CONT;
1620
1621 /* remove all occurrences of the header */
1622 ctx.blk = NULL;
1623 switch (rule->action) {
1624 case PAT_MATCH_STR:
1625 while (http_find_header(htx, rule->arg.http.str, &ctx, 1))
1626 http_remove_header(htx, &ctx);
1627 break;
1628 case PAT_MATCH_BEG:
1629 while (http_find_pfx_header(htx, rule->arg.http.str, &ctx, 1))
1630 http_remove_header(htx, &ctx);
1631 break;
1632 case PAT_MATCH_END:
1633 while (http_find_sfx_header(htx, rule->arg.http.str, &ctx, 1))
1634 http_remove_header(htx, &ctx);
1635 break;
1636 case PAT_MATCH_SUB:
1637 while (http_find_sub_header(htx, rule->arg.http.str, &ctx, 1))
1638 http_remove_header(htx, &ctx);
1639 break;
1640 case PAT_MATCH_REG:
1641 while (http_match_header(htx, rule->arg.http.re, &ctx, 1))
1642 http_remove_header(htx, &ctx);
1643 break;
1644 default:
1645 return ACT_RET_ERR;
1646 }
1647 return ret;
1648}
1649
1650/* Parse a "del-header" action. It takes string as a required argument,
1651 * optional flag (currently only -m) and optional matching method of input string
1652 * with header name to be deleted. Default matching method is exact match (-m str).
1653 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Christopher Faulet81e20172019-12-12 16:40:30 +01001654 */
1655static enum act_parse_ret parse_http_del_header(const char **args, int *orig_arg, struct proxy *px,
1656 struct act_rule *rule, char **err)
1657{
1658 int cur_arg;
Maciej Zdebebdd4c52020-11-20 13:58:48 +00001659 int pat_idx;
Christopher Faulet81e20172019-12-12 16:40:30 +01001660
Maciej Zdebebdd4c52020-11-20 13:58:48 +00001661 /* set exact matching (-m str) as default */
1662 rule->action = PAT_MATCH_STR;
1663 rule->action_ptr = http_action_del_header;
Christopher Faulet2eb53962020-01-14 14:47:34 +01001664 rule->release_ptr = release_http_action;
Christopher Faulet81e20172019-12-12 16:40:30 +01001665
1666 cur_arg = *orig_arg;
1667 if (!*args[cur_arg]) {
Maciej Zdebebdd4c52020-11-20 13:58:48 +00001668 memprintf(err, "expects at least 1 argument");
Christopher Faulet81e20172019-12-12 16:40:30 +01001669 return ACT_RET_PRS_ERR;
1670 }
1671
Christopher Faulet96bff762019-12-17 13:46:18 +01001672 rule->arg.http.str.ptr = strdup(args[cur_arg]);
1673 rule->arg.http.str.len = strlen(rule->arg.http.str.ptr);
Christopher Faulet81e20172019-12-12 16:40:30 +01001674 px->conf.args.ctx = (rule->from == ACT_F_HTTP_REQ ? ARGC_HRQ : ARGC_HRS);
1675
Maciej Zdeb6dee9962020-11-23 16:03:09 +00001676 LIST_INIT(&rule->arg.http.fmt);
Maciej Zdebebdd4c52020-11-20 13:58:48 +00001677 if (strcmp(args[cur_arg+1], "-m") == 0) {
1678 cur_arg++;
1679 if (!*args[cur_arg+1]) {
1680 memprintf(err, "-m flag expects exactly 1 argument");
1681 return ACT_RET_PRS_ERR;
1682 }
1683
1684 cur_arg++;
1685 pat_idx = pat_find_match_name(args[cur_arg]);
1686 switch (pat_idx) {
1687 case PAT_MATCH_REG:
1688 if (!(rule->arg.http.re = regex_comp(rule->arg.http.str.ptr, 1, 1, err)))
1689 return ACT_RET_PRS_ERR;
1690 /* fall through */
1691 case PAT_MATCH_STR:
1692 case PAT_MATCH_BEG:
1693 case PAT_MATCH_END:
1694 case PAT_MATCH_SUB:
1695 rule->action = pat_idx;
1696 break;
1697 default:
1698 memprintf(err, "-m with unsupported matching method '%s'", args[cur_arg]);
1699 return ACT_RET_PRS_ERR;
1700 }
1701 }
1702
Christopher Faulet81e20172019-12-12 16:40:30 +01001703 *orig_arg = cur_arg + 1;
1704 return ACT_RET_PRS_OK;
1705}
1706
Christopher Faulet2eb53962020-01-14 14:47:34 +01001707/* Release memory allocated by an http redirect action. */
1708static void release_http_redir(struct act_rule *rule)
1709{
1710 struct logformat_node *lf, *lfb;
1711 struct redirect_rule *redir;
1712
1713 redir = rule->arg.redir;
1714 LIST_DEL(&redir->list);
1715 if (redir->cond) {
1716 prune_acl_cond(redir->cond);
1717 free(redir->cond);
1718 }
1719 free(redir->rdr_str);
1720 free(redir->cookie_str);
1721 list_for_each_entry_safe(lf, lfb, &redir->rdr_fmt, list) {
1722 LIST_DEL(&lf->list);
1723 free(lf);
1724 }
1725 free(redir);
1726}
1727
Christopher Faulet81e20172019-12-12 16:40:30 +01001728/* Parse a "redirect" action. It returns ACT_RET_PRS_OK on success,
1729 * ACT_RET_PRS_ERR on error.
1730 */
1731static enum act_parse_ret parse_http_redirect(const char **args, int *orig_arg, struct proxy *px,
1732 struct act_rule *rule, char **err)
1733{
1734 struct redirect_rule *redir;
1735 int dir, cur_arg;
1736
1737 rule->action = ACT_HTTP_REDIR;
Christopher Faulet245cf792019-12-18 14:58:12 +01001738 rule->flags |= ACT_FLAG_FINAL;
Christopher Faulet2eb53962020-01-14 14:47:34 +01001739 rule->release_ptr = release_http_redir;
Christopher Faulet81e20172019-12-12 16:40:30 +01001740
1741 cur_arg = *orig_arg;
1742
1743 dir = (rule->from == ACT_F_HTTP_REQ ? 0 : 1);
1744 if ((redir = http_parse_redirect_rule(px->conf.args.file, px->conf.args.line, px, &args[cur_arg], err, 1, dir)) == NULL)
1745 return ACT_RET_PRS_ERR;
1746
1747 rule->arg.redir = redir;
1748 rule->cond = redir->cond;
1749 redir->cond = NULL;
1750
1751 /* skip all arguments */
1752 while (*args[cur_arg])
1753 cur_arg++;
1754
1755 *orig_arg = cur_arg;
1756 return ACT_RET_PRS_OK;
1757}
1758
Christopher Faulet046cf442019-12-17 15:45:23 +01001759/* This function executes a add-acl, del-acl, set-map or del-map actions. On
1760 * success, it returns ACT_RET_CONT. Otherwsize ACT_RET_ERR is returned.
1761 */
1762static enum act_return http_action_set_map(struct act_rule *rule, struct proxy *px,
1763 struct session *sess, struct stream *s, int flags)
1764{
1765 struct pat_ref *ref;
1766 struct buffer *key = NULL, *value = NULL;
1767 enum act_return ret = ACT_RET_CONT;
1768
1769 /* collect reference */
1770 ref = pat_ref_lookup(rule->arg.map.ref);
1771 if (!ref)
1772 goto leave;
1773
1774 /* allocate key */
1775 key = alloc_trash_chunk();
1776 if (!key)
1777 goto fail_alloc;
1778
1779 /* collect key */
1780 key->data = build_logline(s, key->area, key->size, &rule->arg.map.key);
1781 key->area[key->data] = '\0';
1782
1783 switch (rule->action) {
1784 case 0: // add-acl
1785 /* add entry only if it does not already exist */
1786 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
1787 if (pat_ref_find_elt(ref, key->area) == NULL)
1788 pat_ref_add(ref, key->area, NULL, NULL);
1789 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
1790 break;
1791
1792 case 1: // set-map
1793 /* allocate value */
1794 value = alloc_trash_chunk();
1795 if (!value)
1796 goto fail_alloc;
1797
1798 /* collect value */
1799 value->data = build_logline(s, value->area, value->size, &rule->arg.map.value);
1800 value->area[value->data] = '\0';
1801
1802 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
1803 if (pat_ref_find_elt(ref, key->area) != NULL) {
1804 /* update entry if it exists */
1805 pat_ref_set(ref, key->area, value->area, NULL);
1806 }
1807 else {
1808 /* insert a new entry */
1809 pat_ref_add(ref, key->area, value->area, NULL);
1810 }
1811 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
1812 break;
1813
1814 case 2: // del-acl
1815 case 3: // del-map
1816 /* returned code: 1=ok, 0=ko */
1817 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
1818 pat_ref_delete(ref, key->area);
1819 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
1820 break;
1821
1822 default:
1823 ret = ACT_RET_ERR;
1824 }
1825
1826
1827 leave:
1828 free_trash_chunk(key);
1829 free_trash_chunk(value);
1830 return ret;
1831
1832 fail_alloc:
1833 if (!(s->flags & SF_ERR_MASK))
1834 s->flags |= SF_ERR_RESOURCE;
1835 ret = ACT_RET_ERR;
1836 goto leave;
1837}
1838
Christopher Faulet2eb53962020-01-14 14:47:34 +01001839/* Release memory allocated by an http map/acl action. */
1840static void release_http_map(struct act_rule *rule)
1841{
1842 struct logformat_node *lf, *lfb;
1843
1844 free(rule->arg.map.ref);
1845 list_for_each_entry_safe(lf, lfb, &rule->arg.map.key, list) {
1846 LIST_DEL(&lf->list);
1847 release_sample_expr(lf->expr);
1848 free(lf->arg);
1849 free(lf);
1850 }
1851 if (rule->action == 1) {
1852 list_for_each_entry_safe(lf, lfb, &rule->arg.map.value, list) {
1853 LIST_DEL(&lf->list);
1854 release_sample_expr(lf->expr);
1855 free(lf->arg);
1856 free(lf);
1857 }
1858 }
1859}
1860
Christopher Faulet81e20172019-12-12 16:40:30 +01001861/* Parse a "add-acl", "del-acl", "set-map" or "del-map" actions. It takes one or
Christopher Faulet046cf442019-12-17 15:45:23 +01001862 * two log-format string as argument depending on the action. The action is
1863 * stored in <.action> as an int (0=add-acl, 1=set-map, 2=del-acl,
1864 * 3=del-map). It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Christopher Faulet81e20172019-12-12 16:40:30 +01001865 */
1866static enum act_parse_ret parse_http_set_map(const char **args, int *orig_arg, struct proxy *px,
1867 struct act_rule *rule, char **err)
1868{
1869 int cap, cur_arg;
1870
Christopher Faulet046cf442019-12-17 15:45:23 +01001871 if (args[*orig_arg-1][0] == 'a') // add-acl
1872 rule->action = 0;
1873 else if (args[*orig_arg-1][0] == 's') // set-map
1874 rule->action = 1;
1875 else if (args[*orig_arg-1][4] == 'a') // del-acl
1876 rule->action = 2;
1877 else if (args[*orig_arg-1][4] == 'm') // del-map
1878 rule->action = 3;
1879 else {
1880 memprintf(err, "internal error: unhandled action '%s'", args[0]);
1881 return ACT_RET_PRS_ERR;
1882 }
1883 rule->action_ptr = http_action_set_map;
Christopher Faulet2eb53962020-01-14 14:47:34 +01001884 rule->release_ptr = release_http_map;
Christopher Faulet81e20172019-12-12 16:40:30 +01001885
1886 cur_arg = *orig_arg;
Christopher Faulet046cf442019-12-17 15:45:23 +01001887 if (rule->action == 1 && (!*args[cur_arg] || !*args[cur_arg+1])) {
1888 /* 2 args for set-map */
Christopher Faulet81e20172019-12-12 16:40:30 +01001889 memprintf(err, "expects exactly 2 arguments");
1890 return ACT_RET_PRS_ERR;
1891 }
1892 else if (!*args[cur_arg]) {
Christopher Faulet046cf442019-12-17 15:45:23 +01001893 /* only one arg for other actions */
Christopher Faulet81e20172019-12-12 16:40:30 +01001894 memprintf(err, "expects exactly 1 arguments");
1895 return ACT_RET_PRS_ERR;
1896 }
1897
1898 /*
1899 * '+ 8' for 'set-map(' (same for del-map)
1900 * '- 9' for 'set-map(' + trailing ')' (same for del-map)
1901 */
1902 rule->arg.map.ref = my_strndup(args[cur_arg-1] + 8, strlen(args[cur_arg-1]) - 9);
1903
1904 if (rule->from == ACT_F_HTTP_REQ) {
1905 px->conf.args.ctx = ARGC_HRQ;
1906 cap = (px->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR;
1907 }
1908 else{
1909 px->conf.args.ctx = ARGC_HRS;
1910 cap = (px->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR;
1911 }
1912
1913 /* key pattern */
1914 LIST_INIT(&rule->arg.map.key);
Christopher Faulet1337b322020-01-14 14:50:55 +01001915 if (!parse_logformat_string(args[cur_arg], px, &rule->arg.map.key, LOG_OPT_HTTP, cap, err)) {
1916 free(rule->arg.map.ref);
Christopher Faulet81e20172019-12-12 16:40:30 +01001917 return ACT_RET_PRS_ERR;
Christopher Faulet1337b322020-01-14 14:50:55 +01001918 }
Christopher Faulet81e20172019-12-12 16:40:30 +01001919
Christopher Faulet046cf442019-12-17 15:45:23 +01001920 if (rule->action == 1) {
Christopher Faulet81e20172019-12-12 16:40:30 +01001921 /* value pattern for set-map only */
1922 cur_arg++;
1923 LIST_INIT(&rule->arg.map.value);
Christopher Faulet1337b322020-01-14 14:50:55 +01001924 if (!parse_logformat_string(args[cur_arg], px, &rule->arg.map.value, LOG_OPT_HTTP, cap, err)) {
1925 free(rule->arg.map.ref);
Christopher Faulet81e20172019-12-12 16:40:30 +01001926 return ACT_RET_PRS_ERR;
Christopher Faulet1337b322020-01-14 14:50:55 +01001927 }
Christopher Faulet81e20172019-12-12 16:40:30 +01001928 }
1929
1930 free(px->conf.lfs_file);
1931 px->conf.lfs_file = strdup(px->conf.args.file);
1932 px->conf.lfs_line = px->conf.args.line;
1933
1934 *orig_arg = cur_arg + 1;
1935 return ACT_RET_PRS_OK;
1936}
1937
Christopher Fauletac98d812019-12-18 09:20:16 +01001938/* This function executes a track-sc* actions. On success, it returns
1939 * ACT_RET_CONT. Otherwsize ACT_RET_ERR is returned.
1940 */
1941static enum act_return http_action_track_sc(struct act_rule *rule, struct proxy *px,
1942 struct session *sess, struct stream *s, int flags)
1943{
1944 struct stktable *t;
1945 struct stksess *ts;
1946 struct stktable_key *key;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001947 void *ptr1, *ptr2, *ptr3, *ptr4, *ptr5, *ptr6;
Christopher Fauletac98d812019-12-18 09:20:16 +01001948 int opt;
1949
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001950 ptr1 = ptr2 = ptr3 = ptr4 = ptr5 = ptr6 = NULL;
Christopher Fauletac98d812019-12-18 09:20:16 +01001951 opt = ((rule->from == ACT_F_HTTP_REQ) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES) | SMP_OPT_FINAL;
1952
1953 t = rule->arg.trk_ctr.table.t;
Emeric Brun362d25e2021-03-10 16:58:03 +01001954
1955 if (stkctr_entry(&s->stkctr[rule->action]))
1956 goto end;
1957
Christopher Fauletac98d812019-12-18 09:20:16 +01001958 key = stktable_fetch_key(t, s->be, sess, s, opt, rule->arg.trk_ctr.expr, NULL);
1959
1960 if (!key)
1961 goto end;
1962 ts = stktable_get_entry(t, key);
1963 if (!ts)
1964 goto end;
1965
1966 stream_track_stkctr(&s->stkctr[rule->action], t, ts);
1967
1968 /* let's count a new HTTP request as it's the first time we do it */
1969 ptr1 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
1970 ptr2 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
1971
1972 /* When the client triggers a 4xx from the server, it's most often due
1973 * to a missing object or permission. These events should be tracked
1974 * because if they happen often, it may indicate a brute force or a
1975 * vulnerability scan. Normally this is done when receiving the response
1976 * but here we're tracking after this ought to have been done so we have
1977 * to do it on purpose.
1978 */
1979 if (rule->from == ACT_F_HTTP_RES && (unsigned)(s->txn->status - 400) < 100) {
1980 ptr3 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_CNT);
1981 ptr4 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_RATE);
1982 }
1983
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001984 if (rule->from == ACT_F_HTTP_RES && (unsigned)(s->txn->status - 500) < 100 &&
1985 s->txn->status != 501 && s->txn->status != 505) {
1986 ptr5 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_FAIL_CNT);
1987 ptr6 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_FAIL_RATE);
1988 }
1989
1990 if (ptr1 || ptr2 || ptr3 || ptr4 || ptr5 || ptr6) {
Christopher Fauletac98d812019-12-18 09:20:16 +01001991 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
1992
1993 if (ptr1)
1994 stktable_data_cast(ptr1, http_req_cnt)++;
1995 if (ptr2)
1996 update_freq_ctr_period(&stktable_data_cast(ptr2, http_req_rate),
1997 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
1998 if (ptr3)
1999 stktable_data_cast(ptr3, http_err_cnt)++;
2000 if (ptr4)
2001 update_freq_ctr_period(&stktable_data_cast(ptr4, http_err_rate),
2002 t->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1);
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002003 if (ptr5)
2004 stktable_data_cast(ptr5, http_fail_cnt)++;
2005 if (ptr6)
2006 update_freq_ctr_period(&stktable_data_cast(ptr6, http_fail_rate),
2007 t->data_arg[STKTABLE_DT_HTTP_FAIL_RATE].u, 1);
Christopher Fauletac98d812019-12-18 09:20:16 +01002008
2009 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
2010
2011 /* If data was modified, we need to touch to re-schedule sync */
2012 stktable_touch_local(t, ts, 0);
2013 }
2014
2015 stkctr_set_flags(&s->stkctr[rule->action], STKCTR_TRACK_CONTENT);
2016 if (sess->fe != s->be)
2017 stkctr_set_flags(&s->stkctr[rule->action], STKCTR_TRACK_BACKEND);
2018
2019 end:
2020 return ACT_RET_CONT;
2021}
Christopher Faulet81e20172019-12-12 16:40:30 +01002022
Christopher Faulet2eb53962020-01-14 14:47:34 +01002023static void release_http_track_sc(struct act_rule *rule)
2024{
2025 release_sample_expr(rule->arg.trk_ctr.expr);
2026}
2027
Christopher Faulet81e20172019-12-12 16:40:30 +01002028/* Parse a "track-sc*" actions. It returns ACT_RET_PRS_OK on success,
2029 * ACT_RET_PRS_ERR on error.
2030 */
2031static enum act_parse_ret parse_http_track_sc(const char **args, int *orig_arg, struct proxy *px,
2032 struct act_rule *rule, char **err)
2033{
2034 struct sample_expr *expr;
2035 unsigned int where;
2036 unsigned int tsc_num;
2037 const char *tsc_num_str;
2038 int cur_arg;
2039
2040 tsc_num_str = &args[*orig_arg-1][8];
2041 if (cfg_parse_track_sc_num(&tsc_num, tsc_num_str, tsc_num_str + strlen(tsc_num_str), err) == -1)
2042 return ACT_RET_PRS_ERR;
2043
2044 cur_arg = *orig_arg;
2045 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line,
Willy Tarreaue3b57bf2020-02-14 16:50:14 +01002046 err, &px->conf.args, NULL);
Christopher Faulet81e20172019-12-12 16:40:30 +01002047 if (!expr)
2048 return ACT_RET_PRS_ERR;
2049
2050 where = 0;
2051 if (px->cap & PR_CAP_FE)
2052 where |= (rule->from == ACT_F_HTTP_REQ ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_FE_HRS_HDR);
2053 if (px->cap & PR_CAP_BE)
2054 where |= (rule->from == ACT_F_HTTP_REQ ? SMP_VAL_BE_HRQ_HDR : SMP_VAL_BE_HRS_HDR);
2055
2056 if (!(expr->fetch->val & where)) {
2057 memprintf(err, "fetch method '%s' extracts information from '%s', none of which is available here",
2058 args[cur_arg-1], sample_src_names(expr->fetch->use));
Christopher Faulet1337b322020-01-14 14:50:55 +01002059 release_sample_expr(expr);
Christopher Faulet81e20172019-12-12 16:40:30 +01002060 return ACT_RET_PRS_ERR;
2061 }
2062
2063 if (strcmp(args[cur_arg], "table") == 0) {
2064 cur_arg++;
2065 if (!*args[cur_arg]) {
2066 memprintf(err, "missing table name");
Christopher Faulet1337b322020-01-14 14:50:55 +01002067 release_sample_expr(expr);
Christopher Faulet81e20172019-12-12 16:40:30 +01002068 return ACT_RET_PRS_ERR;
2069 }
2070
2071 /* we copy the table name for now, it will be resolved later */
2072 rule->arg.trk_ctr.table.n = strdup(args[cur_arg]);
2073 cur_arg++;
2074 }
2075
Christopher Fauletac98d812019-12-18 09:20:16 +01002076 rule->action = tsc_num;
Christopher Faulet81e20172019-12-12 16:40:30 +01002077 rule->arg.trk_ctr.expr = expr;
Christopher Fauletac98d812019-12-18 09:20:16 +01002078 rule->action_ptr = http_action_track_sc;
Christopher Faulet2eb53962020-01-14 14:47:34 +01002079 rule->release_ptr = release_http_track_sc;
Christopher Faulet81e20172019-12-12 16:40:30 +01002080 rule->check_ptr = check_trk_action;
2081
2082 *orig_arg = cur_arg;
2083 return ACT_RET_PRS_OK;
2084}
2085
Amaury Denoyelle8d228232020-12-10 13:43:54 +01002086static enum act_return action_timeout_set_stream_timeout(struct act_rule *rule,
2087 struct proxy *px,
2088 struct session *sess,
2089 struct stream *s,
2090 int flags)
2091{
2092 struct sample *key;
2093
2094 if (rule->arg.timeout.expr) {
2095 key = sample_fetch_as_type(px, sess, s, SMP_OPT_FINAL, rule->arg.timeout.expr, SMP_T_SINT);
2096 if (!key)
2097 return ACT_RET_CONT;
2098
2099 stream_set_timeout(s, rule->arg.timeout.type, MS_TO_TICKS(key->data.u.sint));
2100 }
2101 else {
2102 stream_set_timeout(s, rule->arg.timeout.type, MS_TO_TICKS(rule->arg.timeout.value));
2103 }
2104
2105 return ACT_RET_CONT;
2106}
2107
2108/* Parse a "set-timeout" action. Returns ACT_RET_PRS_ERR if parsing error.
2109 */
2110static enum act_parse_ret parse_http_set_timeout(const char **args,
2111 int *orig_arg,
2112 struct proxy *px,
2113 struct act_rule *rule, char **err)
2114{
2115 int cur_arg;
2116
2117 rule->action = ACT_CUSTOM;
2118 rule->action_ptr = action_timeout_set_stream_timeout;
2119 rule->release_ptr = release_timeout_action;
2120
2121 cur_arg = *orig_arg;
2122 if (!*args[cur_arg] || !*args[cur_arg + 1]) {
2123 memprintf(err, "expects exactly 2 arguments");
2124 return ACT_RET_PRS_ERR;
2125 }
2126
2127 if (!(px->cap & PR_CAP_BE)) {
2128 memprintf(err, "proxy '%s' has no backend capability", px->id);
2129 return ACT_RET_PRS_ERR;
2130 }
2131
2132 if (cfg_parse_rule_set_timeout(args, cur_arg,
2133 &rule->arg.timeout.value,
2134 &rule->arg.timeout.type,
2135 &rule->arg.timeout.expr,
2136 err,
2137 px->conf.args.file,
2138 px->conf.args.line, &px->conf.args) == -1) {
2139 return ACT_RET_PRS_ERR;
2140 }
2141
2142 *orig_arg = cur_arg + 2;
2143
2144 return ACT_RET_PRS_OK;
2145}
2146
Christopher Faulet46f95542019-12-20 10:07:22 +01002147/* This function executes a strict-mode actions. On success, it always returns
2148 * ACT_RET_CONT
2149 */
2150static enum act_return http_action_strict_mode(struct act_rule *rule, struct proxy *px,
2151 struct session *sess, struct stream *s, int flags)
2152{
2153 struct http_msg *msg = ((rule->from == ACT_F_HTTP_REQ) ? &s->txn->req : &s->txn->rsp);
2154
2155 if (rule->action == 0) // strict-mode on
2156 msg->flags &= ~HTTP_MSGF_SOFT_RW;
2157 else // strict-mode off
2158 msg->flags |= HTTP_MSGF_SOFT_RW;
2159 return ACT_RET_CONT;
2160}
2161
2162/* Parse a "strict-mode" action. It returns ACT_RET_PRS_OK on success,
2163 * ACT_RET_PRS_ERR on error.
2164 */
2165static enum act_parse_ret parse_http_strict_mode(const char **args, int *orig_arg, struct proxy *px,
2166 struct act_rule *rule, char **err)
2167{
2168 int cur_arg;
2169
Christopher Faulet46f95542019-12-20 10:07:22 +01002170 cur_arg = *orig_arg;
2171 if (!*args[cur_arg]) {
2172 memprintf(err, "expects exactly 1 arguments");
2173 return ACT_RET_PRS_ERR;
2174 }
2175
2176 if (strcasecmp(args[cur_arg], "on") == 0)
2177 rule->action = 0; // strict-mode on
2178 else if (strcasecmp(args[cur_arg], "off") == 0)
2179 rule->action = 1; // strict-mode off
2180 else {
2181 memprintf(err, "Unexpected value '%s'. Only 'on' and 'off' are supported", args[cur_arg]);
2182 return ACT_RET_PRS_ERR;
2183 }
2184 rule->action_ptr = http_action_strict_mode;
2185
2186 *orig_arg = cur_arg + 1;
2187 return ACT_RET_PRS_OK;
2188}
2189
Christopher Faulet24231ab2020-01-24 17:44:23 +01002190/* This function executes a return action. It builds an HTX message from an
2191 * errorfile, an raw file or a log-format string, depending on <.action>
2192 * value. On success, it returns ACT_RET_ABRT. If an error occurs ACT_RET_ERR is
2193 * returned.
2194 */
2195static enum act_return http_action_return(struct act_rule *rule, struct proxy *px,
2196 struct session *sess, struct stream *s, int flags)
2197{
2198 struct channel *req = &s->req;
Christopher Faulet24231ab2020-01-24 17:44:23 +01002199
Christopher Faulet2d36df22021-02-19 11:41:01 +01002200 s->txn->status = rule->arg.http_reply->status;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02002201 if (http_reply_message(s, rule->arg.http_reply) == -1)
2202 return ACT_RET_ERR;
Christopher Faulet24231ab2020-01-24 17:44:23 +01002203
Christopher Faulet24231ab2020-01-24 17:44:23 +01002204 if (rule->from == ACT_F_HTTP_REQ) {
2205 /* let's log the request time */
2206 s->logs.tv_request = now;
2207 req->analysers &= AN_REQ_FLT_END;
2208
2209 if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Willy Tarreau4781b152021-04-06 13:53:36 +02002210 _HA_ATOMIC_INC(&s->sess->fe->fe_counters.intercepted_req);
Christopher Faulet24231ab2020-01-24 17:44:23 +01002211 }
2212
2213 if (!(s->flags & SF_ERR_MASK))
2214 s->flags |= SF_ERR_LOCAL;
2215 if (!(s->flags & SF_FINST_MASK))
2216 s->flags |= ((rule->from == ACT_F_HTTP_REQ) ? SF_FINST_R : SF_FINST_H);
2217
Christopher Faulet0e2ad612020-05-13 16:38:37 +02002218 return ACT_RET_ABRT;
Christopher Faulet24231ab2020-01-24 17:44:23 +01002219}
2220
Christopher Faulet24231ab2020-01-24 17:44:23 +01002221/* Parse a "return" action. It returns ACT_RET_PRS_OK on success,
Christopher Faulet47e791e2020-05-13 14:36:55 +02002222 * ACT_RET_PRS_ERR on error. It relies on http_parse_http_reply() to set
2223 * <.arg.http_reply>.
Christopher Faulet24231ab2020-01-24 17:44:23 +01002224 */
2225static enum act_parse_ret parse_http_return(const char **args, int *orig_arg, struct proxy *px,
2226 struct act_rule *rule, char **err)
2227{
Christopher Faulet47e791e2020-05-13 14:36:55 +02002228 /* Prepare parsing of log-format strings */
2229 px->conf.args.ctx = ((rule->from == ACT_F_HTTP_REQ) ? ARGC_HRQ : ARGC_HRS);
2230 rule->arg.http_reply = http_parse_http_reply(args, orig_arg, px, 200, err);
2231 if (!rule->arg.http_reply)
2232 return ACT_RET_PRS_ERR;
Christopher Faulet24231ab2020-01-24 17:44:23 +01002233
Christopher Fauletba946bf2020-05-13 08:50:07 +02002234 rule->flags |= ACT_FLAG_FINAL;
Christopher Faulet5ff0c642020-05-12 18:33:37 +02002235 rule->action = ACT_CUSTOM;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002236 rule->check_ptr = check_act_http_reply;
Christopher Faulet24231ab2020-01-24 17:44:23 +01002237 rule->action_ptr = http_action_return;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002238 rule->release_ptr = release_act_http_reply;
Christopher Faulet24231ab2020-01-24 17:44:23 +01002239 return ACT_RET_PRS_OK;
Christopher Faulet24231ab2020-01-24 17:44:23 +01002240}
2241
Christopher Faulet021a8e42021-03-29 10:46:38 +02002242
2243
2244/* This function executes a wait-for-body action. It waits for the message
2245 * payload for a max configured time (.arg.p[0]) and eventually for only first
2246 * <arg.p[1]> bytes (0 means no limit). It relies on http_wait_for_msg_body()
2247 * function. it returns ACT_RET_CONT when conditions are met to stop to wait.
2248 * Otherwise ACT_RET_YIELD is returned to wait for more data. ACT_RET_INV is
2249 * returned if a parsing error is raised by lower level and ACT_RET_ERR if an
2250 * internal error occured. Finally ACT_RET_ABRT is returned when a timeout
2251 * occured.
2252 */
2253static enum act_return http_action_wait_for_body(struct act_rule *rule, struct proxy *px,
2254 struct session *sess, struct stream *s, int flags)
2255{
2256 struct channel *chn = ((rule->from == ACT_F_HTTP_REQ) ? &s->req : &s->res);
2257 unsigned int time = (uintptr_t)rule->arg.act.p[0];
2258 unsigned int bytes = (uintptr_t)rule->arg.act.p[1];
2259
2260 switch (http_wait_for_msg_body(s, chn, time, bytes)) {
2261 case HTTP_RULE_RES_CONT:
2262 return ACT_RET_CONT;
2263 case HTTP_RULE_RES_YIELD:
2264 return ACT_RET_YIELD;
2265 case HTTP_RULE_RES_BADREQ:
2266 return ACT_RET_INV;
2267 case HTTP_RULE_RES_ERROR:
2268 return ACT_RET_ERR;
2269 case HTTP_RULE_RES_ABRT:
2270 return ACT_RET_ABRT;
2271 default:
2272 return ACT_RET_ERR;
2273 }
2274}
2275
2276/* Parse a "wait-for-body" action. It returns ACT_RET_PRS_OK on success,
2277 * ACT_RET_PRS_ERR on error.
2278 */
2279static enum act_parse_ret parse_http_wait_for_body(const char **args, int *orig_arg, struct proxy *px,
2280 struct act_rule *rule, char **err)
2281{
2282 int cur_arg;
2283 unsigned int time, bytes;
2284 const char *res;
2285
2286 cur_arg = *orig_arg;
2287 if (!*args[cur_arg]) {
2288 memprintf(err, "expects time <time> [ at-least <bytes> ]");
2289 return ACT_RET_PRS_ERR;
2290 }
2291
2292 time = UINT_MAX; /* To be sure it is set */
2293 bytes = 0; /* Default value, wait all the body */
2294 while (*(args[cur_arg])) {
2295 if (strcmp(args[cur_arg], "time") == 0) {
2296 if (!*args[cur_arg + 1]) {
2297 memprintf(err, "missing argument for '%s'", args[cur_arg]);
2298 return ACT_RET_PRS_ERR;
2299 }
2300 res = parse_time_err(args[cur_arg+1], &time, TIME_UNIT_MS);
2301 if (res == PARSE_TIME_OVER) {
2302 memprintf(err, "time overflow (maximum value is 2147483647 ms or ~24.8 days)");
2303 return ACT_RET_PRS_ERR;
2304 }
2305 if (res == PARSE_TIME_UNDER) {
2306 memprintf(err, "time underflow (minimum non-null value is 1 ms)");
2307 return ACT_RET_PRS_ERR;
2308 }
2309 if (res) {
2310 memprintf(err, "unexpected character '%c'", *res);
2311 return ACT_RET_PRS_ERR;
2312 }
2313 cur_arg++;
2314 }
2315 else if (strcmp(args[cur_arg], "at-least") == 0) {
2316 if (!*args[cur_arg + 1]) {
2317 memprintf(err, "missing argument for '%s'", args[cur_arg]);
2318 return ACT_RET_PRS_ERR;
2319 }
2320 res = parse_size_err(args[cur_arg+1], &bytes);
2321 if (res) {
2322 memprintf(err, "unexpected character '%c'", *res);
2323 return ACT_RET_PRS_ERR;
2324 }
2325 cur_arg++;
2326 }
2327 else
2328 break;
2329 cur_arg++;
2330 }
2331
2332 if (time == UINT_MAX) {
2333 memprintf(err, "expects time <time> [ at-least <bytes> ]");
2334 return ACT_RET_PRS_ERR;
2335 }
2336
2337 rule->arg.act.p[0] = (void *)(uintptr_t)time;
2338 rule->arg.act.p[1] = (void *)(uintptr_t)bytes;
2339
2340 *orig_arg = cur_arg;
2341
2342 rule->action = ACT_CUSTOM;
2343 rule->action_ptr = http_action_wait_for_body;
2344 return ACT_RET_PRS_OK;
2345}
2346
Willy Tarreau79e57332018-10-02 16:01:16 +02002347/************************************************************************/
2348/* All supported http-request action keywords must be declared here. */
2349/************************************************************************/
2350
2351static struct action_kw_list http_req_actions = {
2352 .kw = {
Christopher Faulet81e20172019-12-12 16:40:30 +01002353 { "add-acl", parse_http_set_map, 1 },
2354 { "add-header", parse_http_set_header, 0 },
2355 { "allow", parse_http_allow, 0 },
2356 { "auth", parse_http_auth, 0 },
2357 { "capture", parse_http_req_capture, 0 },
2358 { "del-acl", parse_http_set_map, 1 },
2359 { "del-header", parse_http_del_header, 0 },
2360 { "del-map", parse_http_set_map, 1 },
Christopher Faulete0fca292020-01-13 21:49:03 +01002361 { "deny", parse_http_deny, 0 },
Christopher Faulet81e20172019-12-12 16:40:30 +01002362 { "disable-l7-retry", parse_http_req_disable_l7_retry, 0 },
2363 { "early-hint", parse_http_set_header, 0 },
Tim Duesterhusd2bedcc2021-04-15 21:45:57 +02002364 { "normalize-uri", parse_http_normalize_uri, 0 },
Christopher Faulet81e20172019-12-12 16:40:30 +01002365 { "redirect", parse_http_redirect, 0 },
2366 { "reject", parse_http_action_reject, 0 },
2367 { "replace-header", parse_http_replace_header, 0 },
2368 { "replace-path", parse_replace_uri, 0 },
Christopher Faulet312294f2020-09-02 17:17:44 +02002369 { "replace-pathq", parse_replace_uri, 0 },
Christopher Faulet81e20172019-12-12 16:40:30 +01002370 { "replace-uri", parse_replace_uri, 0 },
2371 { "replace-value", parse_http_replace_header, 0 },
Christopher Faulet24231ab2020-01-24 17:44:23 +01002372 { "return", parse_http_return, 0 },
Christopher Faulet81e20172019-12-12 16:40:30 +01002373 { "set-header", parse_http_set_header, 0 },
2374 { "set-log-level", parse_http_set_log_level, 0 },
2375 { "set-map", parse_http_set_map, 1 },
2376 { "set-method", parse_set_req_line, 0 },
2377 { "set-mark", parse_http_set_mark, 0 },
2378 { "set-nice", parse_http_set_nice, 0 },
2379 { "set-path", parse_set_req_line, 0 },
Christopher Faulet312294f2020-09-02 17:17:44 +02002380 { "set-pathq", parse_set_req_line, 0 },
Christopher Faulet81e20172019-12-12 16:40:30 +01002381 { "set-query", parse_set_req_line, 0 },
2382 { "set-tos", parse_http_set_tos, 0 },
2383 { "set-uri", parse_set_req_line, 0 },
Christopher Faulet46f95542019-12-20 10:07:22 +01002384 { "strict-mode", parse_http_strict_mode, 0 },
Christopher Faulete0fca292020-01-13 21:49:03 +01002385 { "tarpit", parse_http_deny, 0 },
Christopher Faulet81e20172019-12-12 16:40:30 +01002386 { "track-sc", parse_http_track_sc, 1 },
Amaury Denoyelle8d228232020-12-10 13:43:54 +01002387 { "set-timeout", parse_http_set_timeout, 0 },
Christopher Faulet021a8e42021-03-29 10:46:38 +02002388 { "wait-for-body", parse_http_wait_for_body, 0 },
Willy Tarreau79e57332018-10-02 16:01:16 +02002389 { NULL, NULL }
2390 }
2391};
2392
Willy Tarreau0108d902018-11-25 19:14:37 +01002393INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_actions);
2394
Willy Tarreau79e57332018-10-02 16:01:16 +02002395static struct action_kw_list http_res_actions = {
2396 .kw = {
Christopher Faulet81e20172019-12-12 16:40:30 +01002397 { "add-acl", parse_http_set_map, 1 },
2398 { "add-header", parse_http_set_header, 0 },
2399 { "allow", parse_http_allow, 0 },
2400 { "capture", parse_http_res_capture, 0 },
2401 { "del-acl", parse_http_set_map, 1 },
2402 { "del-header", parse_http_del_header, 0 },
2403 { "del-map", parse_http_set_map, 1 },
Christopher Faulete0fca292020-01-13 21:49:03 +01002404 { "deny", parse_http_deny, 0 },
Christopher Faulet81e20172019-12-12 16:40:30 +01002405 { "redirect", parse_http_redirect, 0 },
2406 { "replace-header", parse_http_replace_header, 0 },
2407 { "replace-value", parse_http_replace_header, 0 },
Christopher Faulet24231ab2020-01-24 17:44:23 +01002408 { "return", parse_http_return, 0 },
Christopher Faulet81e20172019-12-12 16:40:30 +01002409 { "set-header", parse_http_set_header, 0 },
2410 { "set-log-level", parse_http_set_log_level, 0 },
2411 { "set-map", parse_http_set_map, 1 },
2412 { "set-mark", parse_http_set_mark, 0 },
2413 { "set-nice", parse_http_set_nice, 0 },
2414 { "set-status", parse_http_set_status, 0 },
2415 { "set-tos", parse_http_set_tos, 0 },
Christopher Faulet46f95542019-12-20 10:07:22 +01002416 { "strict-mode", parse_http_strict_mode, 0 },
Christopher Faulet81e20172019-12-12 16:40:30 +01002417 { "track-sc", parse_http_track_sc, 1 },
Christopher Faulet021a8e42021-03-29 10:46:38 +02002418 { "wait-for-body", parse_http_wait_for_body, 0 },
Willy Tarreau79e57332018-10-02 16:01:16 +02002419 { NULL, NULL }
2420 }
2421};
2422
Willy Tarreau0108d902018-11-25 19:14:37 +01002423INITCALL1(STG_REGISTER, http_res_keywords_register, &http_res_actions);
Willy Tarreau79e57332018-10-02 16:01:16 +02002424
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002425static struct action_kw_list http_after_res_actions = {
2426 .kw = {
2427 { "add-header", parse_http_set_header, 0 },
2428 { "allow", parse_http_allow, 0 },
2429 { "del-header", parse_http_del_header, 0 },
2430 { "replace-header", parse_http_replace_header, 0 },
2431 { "replace-value", parse_http_replace_header, 0 },
2432 { "set-header", parse_http_set_header, 0 },
2433 { "set-status", parse_http_set_status, 0 },
2434 { "strict-mode", parse_http_strict_mode, 0 },
2435 { NULL, NULL }
2436 }
2437};
2438
2439INITCALL1(STG_REGISTER, http_after_res_keywords_register, &http_after_res_actions);
2440
Willy Tarreau79e57332018-10-02 16:01:16 +02002441/*
2442 * Local variables:
2443 * c-indent-level: 8
2444 * c-basic-offset: 8
2445 * End:
2446 */