blob: 2af4d471a48aa579ffa913c70fa41a661f592b05 [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 Duesterhusd2bedcc2021-04-15 21:45:57 +0200235 }
236
237 switch (err) {
238 case URI_NORMALIZER_ERR_NONE:
239 break;
240 case URI_NORMALIZER_ERR_INTERNAL_ERROR:
241 ret = ACT_RET_ERR;
242 break;
243 case URI_NORMALIZER_ERR_INVALID_INPUT:
244 ret = ACT_RET_INV;
245 break;
246 case URI_NORMALIZER_ERR_ALLOC:
247 goto fail_alloc;
248 }
249
250 leave:
251 free_trash_chunk(replace);
252 return ret;
253
254 fail_alloc:
255 if (!(s->flags & SF_ERR_MASK))
256 s->flags |= SF_ERR_RESOURCE;
257 ret = ACT_RET_ERR;
258 goto leave;
259
260 fail_rewrite:
261 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_rewrites, 1);
262 if (s->flags & SF_BE_ASSIGNED)
263 _HA_ATOMIC_ADD(&s->be->be_counters.failed_rewrites, 1);
264 if (sess->listener && sess->listener->counters)
265 _HA_ATOMIC_ADD(&sess->listener->counters->failed_rewrites, 1);
266 if (objt_server(s->target))
267 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_rewrites, 1);
268
269 if (!(s->txn->req.flags & HTTP_MSGF_SOFT_RW)) {
270 ret = ACT_RET_ERR;
271 if (!(s->flags & SF_ERR_MASK))
272 s->flags |= SF_ERR_PRXCOND;
273 }
274 goto leave;
275}
276
277/* Parses the http-request normalize-uri action. It expects a single <normalizer>
278 * argument, corresponding too a value in `enum act_normalize_uri`.
279 *
280 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
281 */
282static enum act_parse_ret parse_http_normalize_uri(const char **args, int *orig_arg, struct proxy *px,
283 struct act_rule *rule, char **err)
284{
285 int cur_arg = *orig_arg;
286
287 rule->action_ptr = http_action_normalize_uri;
288 rule->release_ptr = NULL;
289
290 if (!*args[cur_arg]) {
291 memprintf(err, "missing argument <normalizer>");
292 return ACT_RET_PRS_ERR;
293 }
294
Tim Duesterhusd371e992021-04-15 21:45:58 +0200295 if (strcmp(args[cur_arg], "merge-slashes") == 0) {
296 cur_arg++;
Tim Duesterhusd2bedcc2021-04-15 21:45:57 +0200297
Tim Duesterhusd371e992021-04-15 21:45:58 +0200298 rule->action = ACT_NORMALIZE_URI_MERGE_SLASHES;
Tim Duesterhusd2bedcc2021-04-15 21:45:57 +0200299 }
300 else {
301 memprintf(err, "unknown normalizer '%s'", args[cur_arg]);
302 return ACT_RET_PRS_ERR;
303 }
304
305 *orig_arg = cur_arg;
306 return ACT_RET_PRS_OK;
307}
308
Willy Tarreau33810222019-06-12 17:44:02 +0200309/* This function executes a replace-uri action. It finds its arguments in
Christopher Faulet96bff762019-12-17 13:46:18 +0100310 * <rule>.arg.http. It builds a string in the trash from the format string
Willy Tarreau33810222019-06-12 17:44:02 +0200311 * previously filled by function parse_replace_uri() and will execute the regex
Christopher Faulet96bff762019-12-17 13:46:18 +0100312 * in <http.re> to replace the URI. It uses the format string present in
Christopher Faulet2c22a692019-12-18 15:39:56 +0100313 * <http.fmt>. The component to act on (path/uri) is taken from <.action> which
Christopher Faulet96bff762019-12-17 13:46:18 +0100314 * contains 1 for the path or 3 for the URI (values used by
315 * http_req_replace_stline()). On success, it returns ACT_RET_CONT. If an error
316 * occurs while soft rewrites are enabled, the action is canceled, but the rule
317 * processing continue. Otherwsize ACT_RET_ERR is returned.
Willy Tarreau33810222019-06-12 17:44:02 +0200318 */
319static enum act_return http_action_replace_uri(struct act_rule *rule, struct proxy *px,
320 struct session *sess, struct stream *s, int flags)
321{
Christopher Faulet13403762019-12-13 09:01:57 +0100322 enum act_return ret = ACT_RET_CONT;
Willy Tarreau33810222019-06-12 17:44:02 +0200323 struct buffer *replace, *output;
324 struct ist uri;
325 int len;
326
327 replace = alloc_trash_chunk();
328 output = alloc_trash_chunk();
329 if (!replace || !output)
Christopher Faulete00d06c2019-12-16 17:18:42 +0100330 goto fail_alloc;
Christopher Faulet12c28b62019-07-15 16:30:24 +0200331 uri = htx_sl_req_uri(http_get_stline(htxbuf(&s->req.buf)));
Willy Tarreau262c3f12019-12-17 06:52:51 +0100332
Christopher Faulet1fa0cc12020-09-02 11:10:38 +0200333 if (rule->action == 1) // replace-path
334 uri = iststop(http_get_path(uri), '?');
Christopher Faulet312294f2020-09-02 17:17:44 +0200335 else if (rule->action == 4) // replace-pathq
336 uri = http_get_path(uri);
Willy Tarreau262c3f12019-12-17 06:52:51 +0100337
Christopher Faulet96bff762019-12-17 13:46:18 +0100338 if (!regex_exec_match2(rule->arg.http.re, uri.ptr, uri.len, MAX_MATCH, pmatch, 0))
Willy Tarreau33810222019-06-12 17:44:02 +0200339 goto leave;
340
Christopher Faulet96bff762019-12-17 13:46:18 +0100341 replace->data = build_logline(s, replace->area, replace->size, &rule->arg.http.fmt);
Willy Tarreau33810222019-06-12 17:44:02 +0200342
343 /* note: uri.ptr doesn't need to be zero-terminated because it will
344 * only be used to pick pmatch references.
345 */
346 len = exp_replace(output->area, output->size, uri.ptr, replace->area, pmatch);
347 if (len == -1)
Christopher Faulete00d06c2019-12-16 17:18:42 +0100348 goto fail_rewrite;
Willy Tarreau33810222019-06-12 17:44:02 +0200349
Christopher Faulet2c22a692019-12-18 15:39:56 +0100350 if (http_req_replace_stline(rule->action, output->area, len, px, s) == -1)
Christopher Faulete00d06c2019-12-16 17:18:42 +0100351 goto fail_rewrite;
Willy Tarreau33810222019-06-12 17:44:02 +0200352
Christopher Faulete00d06c2019-12-16 17:18:42 +0100353 leave:
Willy Tarreau33810222019-06-12 17:44:02 +0200354 free_trash_chunk(output);
355 free_trash_chunk(replace);
356 return ret;
Christopher Faulete00d06c2019-12-16 17:18:42 +0100357
358 fail_alloc:
359 if (!(s->flags & SF_ERR_MASK))
360 s->flags |= SF_ERR_RESOURCE;
361 ret = ACT_RET_ERR;
362 goto leave;
363
364 fail_rewrite:
Willy Tarreau4781b152021-04-06 13:53:36 +0200365 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_rewrites);
Christopher Faulete00d06c2019-12-16 17:18:42 +0100366 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200367 _HA_ATOMIC_INC(&s->be->be_counters.failed_rewrites);
William Lallemand36119de2021-03-08 15:26:48 +0100368 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200369 _HA_ATOMIC_INC(&sess->listener->counters->failed_rewrites);
Christopher Faulete00d06c2019-12-16 17:18:42 +0100370 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +0200371 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_rewrites);
Christopher Faulete00d06c2019-12-16 17:18:42 +0100372
Christopher Faulet333bf8c2020-01-22 14:38:05 +0100373 if (!(s->txn->req.flags & HTTP_MSGF_SOFT_RW)) {
Christopher Faulete00d06c2019-12-16 17:18:42 +0100374 ret = ACT_RET_ERR;
Christopher Faulet333bf8c2020-01-22 14:38:05 +0100375 if (!(s->flags & SF_ERR_MASK))
376 s->flags |= SF_ERR_PRXCOND;
377 }
Christopher Faulete00d06c2019-12-16 17:18:42 +0100378 goto leave;
Willy Tarreau33810222019-06-12 17:44:02 +0200379}
380
Christopher Faulet312294f2020-09-02 17:17:44 +0200381/* parse a "replace-uri", "replace-path" or "replace-pathq"
382 * http-request action.
Willy Tarreau33810222019-06-12 17:44:02 +0200383 * This action takes 2 arguments (a regex and a replacement format string).
Christopher Faulet2c22a692019-12-18 15:39:56 +0100384 * The resulting rule makes use of <.action> to store the action (1/3 for now),
Christopher Faulet96bff762019-12-17 13:46:18 +0100385 * <http.re> to store the compiled regex, and <http.fmt> to store the log-format
Willy Tarreau33810222019-06-12 17:44:02 +0200386 * list head. It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
387 */
388static enum act_parse_ret parse_replace_uri(const char **args, int *orig_arg, struct proxy *px,
389 struct act_rule *rule, char **err)
390{
391 int cur_arg = *orig_arg;
392 char *error = NULL;
393
Christopher Faulet312294f2020-09-02 17:17:44 +0200394 switch (args[0][8]) {
395 case 'p':
396 if (args[0][12] == 'q')
397 rule->action = 4; // replace-pathq, same as set-pathq
398 else
399 rule->action = 1; // replace-path, same as set-path
400 break;
401 case 'u':
Christopher Faulet2c22a692019-12-18 15:39:56 +0100402 rule->action = 3; // replace-uri, same as set-uri
Christopher Faulet312294f2020-09-02 17:17:44 +0200403 break;
404 default:
405 memprintf(err, "internal error: unhandled action '%s'", args[0]);
406 return ACT_RET_PRS_ERR;
407 }
Willy Tarreau262c3f12019-12-17 06:52:51 +0100408
Willy Tarreau33810222019-06-12 17:44:02 +0200409 rule->action_ptr = http_action_replace_uri;
Christopher Faulet2eb53962020-01-14 14:47:34 +0100410 rule->release_ptr = release_http_action;
Willy Tarreau33810222019-06-12 17:44:02 +0200411
412 if (!*args[cur_arg] || !*args[cur_arg+1] ||
413 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
414 memprintf(err, "expects exactly 2 arguments <match-regex> and <replace-format>");
415 return ACT_RET_PRS_ERR;
416 }
417
Christopher Faulet96bff762019-12-17 13:46:18 +0100418 if (!(rule->arg.http.re = regex_comp(args[cur_arg], 1, 1, &error))) {
Willy Tarreau33810222019-06-12 17:44:02 +0200419 memprintf(err, "failed to parse the regex : %s", error);
420 free(error);
421 return ACT_RET_PRS_ERR;
422 }
423
Christopher Faulet96bff762019-12-17 13:46:18 +0100424 LIST_INIT(&rule->arg.http.fmt);
Willy Tarreau33810222019-06-12 17:44:02 +0200425 px->conf.args.ctx = ARGC_HRQ;
Christopher Faulet96bff762019-12-17 13:46:18 +0100426 if (!parse_logformat_string(args[cur_arg + 1], px, &rule->arg.http.fmt, LOG_OPT_HTTP,
Willy Tarreau33810222019-06-12 17:44:02 +0200427 (px->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, err)) {
Christopher Faulet1337b322020-01-14 14:50:55 +0100428 regex_free(rule->arg.http.re);
Willy Tarreau33810222019-06-12 17:44:02 +0200429 return ACT_RET_PRS_ERR;
430 }
431
432 (*orig_arg) += 2;
433 return ACT_RET_PRS_OK;
434}
435
Willy Tarreau79e57332018-10-02 16:01:16 +0200436/* This function is just a compliant action wrapper for "set-status". */
437static enum act_return action_http_set_status(struct act_rule *rule, struct proxy *px,
438 struct session *sess, struct stream *s, int flags)
439{
Christopher Faulet96bff762019-12-17 13:46:18 +0100440 if (http_res_set_status(rule->arg.http.i, rule->arg.http.str, s) == -1) {
Willy Tarreau4781b152021-04-06 13:53:36 +0200441 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_rewrites);
Christopher Faulete00d06c2019-12-16 17:18:42 +0100442 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200443 _HA_ATOMIC_INC(&s->be->be_counters.failed_rewrites);
William Lallemand36119de2021-03-08 15:26:48 +0100444 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200445 _HA_ATOMIC_INC(&sess->listener->counters->failed_rewrites);
Christopher Faulete00d06c2019-12-16 17:18:42 +0100446 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +0200447 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_rewrites);
Christopher Faulete00d06c2019-12-16 17:18:42 +0100448
Christopher Faulet333bf8c2020-01-22 14:38:05 +0100449 if (!(s->txn->req.flags & HTTP_MSGF_SOFT_RW)) {
Christopher Faulet333bf8c2020-01-22 14:38:05 +0100450 if (!(s->flags & SF_ERR_MASK))
451 s->flags |= SF_ERR_PRXCOND;
Christopher Faulet692a6c22020-02-07 10:22:31 +0100452 return ACT_RET_ERR;
Christopher Faulet333bf8c2020-01-22 14:38:05 +0100453 }
Christopher Faulete00d06c2019-12-16 17:18:42 +0100454 }
455
Willy Tarreau79e57332018-10-02 16:01:16 +0200456 return ACT_RET_CONT;
457}
458
459/* parse set-status action:
460 * This action accepts a single argument of type int representing
461 * an http status code. It returns ACT_RET_PRS_OK on success,
462 * ACT_RET_PRS_ERR on error.
463 */
464static enum act_parse_ret parse_http_set_status(const char **args, int *orig_arg, struct proxy *px,
465 struct act_rule *rule, char **err)
466{
467 char *error;
468
469 rule->action = ACT_CUSTOM;
470 rule->action_ptr = action_http_set_status;
Christopher Faulet2eb53962020-01-14 14:47:34 +0100471 rule->release_ptr = release_http_action;
Willy Tarreau79e57332018-10-02 16:01:16 +0200472
473 /* Check if an argument is available */
474 if (!*args[*orig_arg]) {
475 memprintf(err, "expects 1 argument: <status>; or 3 arguments: <status> reason <fmt>");
476 return ACT_RET_PRS_ERR;
477 }
478
479 /* convert status code as integer */
Christopher Faulet96bff762019-12-17 13:46:18 +0100480 rule->arg.http.i = strtol(args[*orig_arg], &error, 10);
481 if (*error != '\0' || rule->arg.http.i < 100 || rule->arg.http.i > 999) {
Willy Tarreau79e57332018-10-02 16:01:16 +0200482 memprintf(err, "expects an integer status code between 100 and 999");
483 return ACT_RET_PRS_ERR;
484 }
485
486 (*orig_arg)++;
487
488 /* set custom reason string */
Christopher Faulet96bff762019-12-17 13:46:18 +0100489 rule->arg.http.str = ist(NULL); // If null, we use the default reason for the status code.
Willy Tarreau79e57332018-10-02 16:01:16 +0200490 if (*args[*orig_arg] && strcmp(args[*orig_arg], "reason") == 0 &&
491 (*args[*orig_arg + 1] && strcmp(args[*orig_arg + 1], "if") != 0 && strcmp(args[*orig_arg + 1], "unless") != 0)) {
492 (*orig_arg)++;
Christopher Faulet96bff762019-12-17 13:46:18 +0100493 rule->arg.http.str.ptr = strdup(args[*orig_arg]);
494 rule->arg.http.str.len = strlen(rule->arg.http.str.ptr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200495 (*orig_arg)++;
496 }
497
Christopher Fauletc20b3712020-01-27 15:51:56 +0100498 LIST_INIT(&rule->arg.http.fmt);
Willy Tarreau79e57332018-10-02 16:01:16 +0200499 return ACT_RET_PRS_OK;
500}
501
502/* This function executes the "reject" HTTP action. It clears the request and
503 * response buffer without sending any response. It can be useful as an HTTP
504 * alternative to the silent-drop action to defend against DoS attacks, and may
505 * also be used with HTTP/2 to close a connection instead of just a stream.
506 * The txn status is unchanged, indicating no response was sent. The termination
Christopher Faulet90d22a82020-03-06 11:18:39 +0100507 * flags will indicate "PR". It always returns ACT_RET_ABRT.
Willy Tarreau79e57332018-10-02 16:01:16 +0200508 */
509static enum act_return http_action_reject(struct act_rule *rule, struct proxy *px,
510 struct session *sess, struct stream *s, int flags)
511{
Willy Tarreau0f9cd7b2019-01-31 19:02:43 +0100512 si_must_kill_conn(chn_prod(&s->req));
Willy Tarreau79e57332018-10-02 16:01:16 +0200513 channel_abort(&s->req);
514 channel_abort(&s->res);
Christopher Fauletd4a824e2020-03-06 15:07:09 +0100515 s->req.analysers &= AN_REQ_FLT_END;
516 s->res.analysers &= AN_RES_FLT_END;
Willy Tarreau79e57332018-10-02 16:01:16 +0200517
Willy Tarreau4781b152021-04-06 13:53:36 +0200518 _HA_ATOMIC_INC(&s->be->be_counters.denied_req);
519 _HA_ATOMIC_INC(&sess->fe->fe_counters.denied_req);
Willy Tarreau79e57332018-10-02 16:01:16 +0200520 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200521 _HA_ATOMIC_INC(&sess->listener->counters->denied_req);
Willy Tarreau79e57332018-10-02 16:01:16 +0200522
523 if (!(s->flags & SF_ERR_MASK))
524 s->flags |= SF_ERR_PRXCOND;
525 if (!(s->flags & SF_FINST_MASK))
526 s->flags |= SF_FINST_R;
527
Christopher Faulet90d22a82020-03-06 11:18:39 +0100528 return ACT_RET_ABRT;
Willy Tarreau79e57332018-10-02 16:01:16 +0200529}
530
531/* parse the "reject" action:
532 * This action takes no argument and returns ACT_RET_PRS_OK on success,
533 * ACT_RET_PRS_ERR on error.
534 */
535static enum act_parse_ret parse_http_action_reject(const char **args, int *orig_arg, struct proxy *px,
536 struct act_rule *rule, char **err)
537{
538 rule->action = ACT_CUSTOM;
539 rule->action_ptr = http_action_reject;
540 return ACT_RET_PRS_OK;
541}
542
Olivier Houchard602bf7d2019-05-10 13:59:15 +0200543/* This function executes the "disable-l7-retry" HTTP action.
544 * It disables L7 retries (all retry except for a connection failure). This
545 * can be useful for example to avoid retrying on POST requests.
546 * It just removes the L7 retry flag on the stream_interface, and always
547 * return ACT_RET_CONT;
548 */
549static enum act_return http_req_disable_l7_retry(struct act_rule *rule, struct proxy *px,
550 struct session *sess, struct stream *s, int flags)
551{
552 struct stream_interface *si = &s->si[1];
553
554 /* In theory, the SI_FL_L7_RETRY flags isn't set at this point, but
555 * let's be future-proof and remove it anyway.
556 */
557 si->flags &= ~SI_FL_L7_RETRY;
558 si->flags |= SI_FL_D_L7_RETRY;
559 return ACT_RET_CONT;
560}
561
562/* parse the "disable-l7-retry" action:
563 * This action takes no argument and returns ACT_RET_PRS_OK on success,
564 * ACT_RET_PRS_ERR on error.
565 */
566static enum act_parse_ret parse_http_req_disable_l7_retry(const char **args,
567 int *orig_args, struct proxy *px,
568 struct act_rule *rule, char **err)
569{
570 rule->action = ACT_CUSTOM;
571 rule->action_ptr = http_req_disable_l7_retry;
572 return ACT_RET_PRS_OK;
573}
574
Willy Tarreau79e57332018-10-02 16:01:16 +0200575/* This function executes the "capture" action. It executes a fetch expression,
576 * turns the result into a string and puts it in a capture slot. It always
577 * returns 1. If an error occurs the action is cancelled, but the rule
578 * processing continues.
579 */
580static enum act_return http_action_req_capture(struct act_rule *rule, struct proxy *px,
581 struct session *sess, struct stream *s, int flags)
582{
583 struct sample *key;
584 struct cap_hdr *h = rule->arg.cap.hdr;
585 char **cap = s->req_cap;
586 int len;
587
588 key = sample_fetch_as_type(s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.cap.expr, SMP_T_STR);
589 if (!key)
590 return ACT_RET_CONT;
591
592 if (cap[h->index] == NULL)
593 cap[h->index] = pool_alloc(h->pool);
594
595 if (cap[h->index] == NULL) /* no more capture memory */
596 return ACT_RET_CONT;
597
598 len = key->data.u.str.data;
599 if (len > h->len)
600 len = h->len;
601
602 memcpy(cap[h->index], key->data.u.str.area, len);
603 cap[h->index][len] = 0;
604 return ACT_RET_CONT;
605}
606
607/* This function executes the "capture" action and store the result in a
608 * capture slot if exists. It executes a fetch expression, turns the result
609 * into a string and puts it in a capture slot. It always returns 1. If an
610 * error occurs the action is cancelled, but the rule processing continues.
611 */
612static enum act_return http_action_req_capture_by_id(struct act_rule *rule, struct proxy *px,
613 struct session *sess, struct stream *s, int flags)
614{
615 struct sample *key;
616 struct cap_hdr *h;
617 char **cap = s->req_cap;
618 struct proxy *fe = strm_fe(s);
619 int len;
620 int i;
621
622 /* Look for the original configuration. */
623 for (h = fe->req_cap, i = fe->nb_req_cap - 1;
624 h != NULL && i != rule->arg.capid.idx ;
625 i--, h = h->next);
626 if (!h)
627 return ACT_RET_CONT;
628
629 key = sample_fetch_as_type(s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.capid.expr, SMP_T_STR);
630 if (!key)
631 return ACT_RET_CONT;
632
633 if (cap[h->index] == NULL)
634 cap[h->index] = pool_alloc(h->pool);
635
636 if (cap[h->index] == NULL) /* no more capture memory */
637 return ACT_RET_CONT;
638
639 len = key->data.u.str.data;
640 if (len > h->len)
641 len = h->len;
642
643 memcpy(cap[h->index], key->data.u.str.area, len);
644 cap[h->index][len] = 0;
645 return ACT_RET_CONT;
646}
647
648/* Check an "http-request capture" action.
649 *
650 * The function returns 1 in success case, otherwise, it returns 0 and err is
651 * filled.
652 */
653static int check_http_req_capture(struct act_rule *rule, struct proxy *px, char **err)
654{
655 if (rule->action_ptr != http_action_req_capture_by_id)
656 return 1;
657
Baptiste Assmann19a69b32020-01-16 14:34:22 +0100658 /* capture slots can only be declared in frontends, so we can't check their
659 * existence in backends at configuration parsing step
660 */
661 if (px->cap & PR_CAP_FE && rule->arg.capid.idx >= px->nb_req_cap) {
Willy Tarreau79e57332018-10-02 16:01:16 +0200662 memprintf(err, "unable to find capture id '%d' referenced by http-request capture rule",
663 rule->arg.capid.idx);
664 return 0;
665 }
666
667 return 1;
668}
669
Christopher Faulet2eb53962020-01-14 14:47:34 +0100670/* Release memory allocate by an http capture action */
671static void release_http_capture(struct act_rule *rule)
672{
673 if (rule->action_ptr == http_action_req_capture)
674 release_sample_expr(rule->arg.cap.expr);
675 else
676 release_sample_expr(rule->arg.capid.expr);
677}
678
Willy Tarreau79e57332018-10-02 16:01:16 +0200679/* parse an "http-request capture" action. It takes a single argument which is
680 * a sample fetch expression. It stores the expression into arg->act.p[0] and
681 * the allocated hdr_cap struct or the preallocated "id" into arg->act.p[1].
682 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
683 */
684static enum act_parse_ret parse_http_req_capture(const char **args, int *orig_arg, struct proxy *px,
685 struct act_rule *rule, char **err)
686{
687 struct sample_expr *expr;
688 struct cap_hdr *hdr;
689 int cur_arg;
690 int len = 0;
691
692 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
693 if (strcmp(args[cur_arg], "if") == 0 ||
694 strcmp(args[cur_arg], "unless") == 0)
695 break;
696
697 if (cur_arg < *orig_arg + 3) {
698 memprintf(err, "expects <expression> [ 'len' <length> | id <idx> ]");
699 return ACT_RET_PRS_ERR;
700 }
701
702 cur_arg = *orig_arg;
Willy Tarreaue3b57bf2020-02-14 16:50:14 +0100703 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 +0200704 if (!expr)
705 return ACT_RET_PRS_ERR;
706
707 if (!(expr->fetch->val & SMP_VAL_FE_HRQ_HDR)) {
708 memprintf(err,
709 "fetch method '%s' extracts information from '%s', none of which is available here",
710 args[cur_arg-1], sample_src_names(expr->fetch->use));
Christopher Faulet1337b322020-01-14 14:50:55 +0100711 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200712 return ACT_RET_PRS_ERR;
713 }
714
715 if (!args[cur_arg] || !*args[cur_arg]) {
716 memprintf(err, "expects 'len or 'id'");
Christopher Faulet1337b322020-01-14 14:50:55 +0100717 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200718 return ACT_RET_PRS_ERR;
719 }
720
721 if (strcmp(args[cur_arg], "len") == 0) {
722 cur_arg++;
723
724 if (!(px->cap & PR_CAP_FE)) {
725 memprintf(err, "proxy '%s' has no frontend capability", px->id);
Christopher Faulet1337b322020-01-14 14:50:55 +0100726 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200727 return ACT_RET_PRS_ERR;
728 }
729
730 px->conf.args.ctx = ARGC_CAP;
731
732 if (!args[cur_arg]) {
733 memprintf(err, "missing length value");
Christopher Faulet1337b322020-01-14 14:50:55 +0100734 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200735 return ACT_RET_PRS_ERR;
736 }
737 /* we copy the table name for now, it will be resolved later */
738 len = atoi(args[cur_arg]);
739 if (len <= 0) {
740 memprintf(err, "length must be > 0");
Christopher Faulet1337b322020-01-14 14:50:55 +0100741 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200742 return ACT_RET_PRS_ERR;
743 }
744 cur_arg++;
745
Willy Tarreau79e57332018-10-02 16:01:16 +0200746 hdr = calloc(1, sizeof(*hdr));
747 hdr->next = px->req_cap;
748 hdr->name = NULL; /* not a header capture */
749 hdr->namelen = 0;
750 hdr->len = len;
751 hdr->pool = create_pool("caphdr", hdr->len + 1, MEM_F_SHARED);
752 hdr->index = px->nb_req_cap++;
753
754 px->req_cap = hdr;
755 px->to_log |= LW_REQHDR;
756
757 rule->action = ACT_CUSTOM;
758 rule->action_ptr = http_action_req_capture;
Christopher Faulet2eb53962020-01-14 14:47:34 +0100759 rule->release_ptr = release_http_capture;
Willy Tarreau79e57332018-10-02 16:01:16 +0200760 rule->arg.cap.expr = expr;
761 rule->arg.cap.hdr = hdr;
762 }
763
764 else if (strcmp(args[cur_arg], "id") == 0) {
765 int id;
766 char *error;
767
768 cur_arg++;
769
770 if (!args[cur_arg]) {
771 memprintf(err, "missing id value");
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 id = strtol(args[cur_arg], &error, 10);
777 if (*error != '\0') {
778 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
Christopher Faulet1337b322020-01-14 14:50:55 +0100779 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200780 return ACT_RET_PRS_ERR;
781 }
782 cur_arg++;
783
784 px->conf.args.ctx = ARGC_CAP;
785
786 rule->action = ACT_CUSTOM;
787 rule->action_ptr = http_action_req_capture_by_id;
788 rule->check_ptr = check_http_req_capture;
Christopher Faulet2eb53962020-01-14 14:47:34 +0100789 rule->release_ptr = release_http_capture;
Willy Tarreau79e57332018-10-02 16:01:16 +0200790 rule->arg.capid.expr = expr;
791 rule->arg.capid.idx = id;
792 }
793
794 else {
795 memprintf(err, "expects 'len' or 'id', found '%s'", args[cur_arg]);
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
800 *orig_arg = cur_arg;
801 return ACT_RET_PRS_OK;
802}
803
804/* This function executes the "capture" action and store the result in a
805 * capture slot if exists. It executes a fetch expression, turns the result
806 * into a string and puts it in a capture slot. It always returns 1. If an
807 * error occurs the action is cancelled, but the rule processing continues.
808 */
809static enum act_return http_action_res_capture_by_id(struct act_rule *rule, struct proxy *px,
810 struct session *sess, struct stream *s, int flags)
811{
812 struct sample *key;
813 struct cap_hdr *h;
814 char **cap = s->res_cap;
815 struct proxy *fe = strm_fe(s);
816 int len;
817 int i;
818
819 /* Look for the original configuration. */
820 for (h = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
821 h != NULL && i != rule->arg.capid.idx ;
822 i--, h = h->next);
823 if (!h)
824 return ACT_RET_CONT;
825
826 key = sample_fetch_as_type(s->be, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL, rule->arg.capid.expr, SMP_T_STR);
827 if (!key)
828 return ACT_RET_CONT;
829
830 if (cap[h->index] == NULL)
831 cap[h->index] = pool_alloc(h->pool);
832
833 if (cap[h->index] == NULL) /* no more capture memory */
834 return ACT_RET_CONT;
835
836 len = key->data.u.str.data;
837 if (len > h->len)
838 len = h->len;
839
840 memcpy(cap[h->index], key->data.u.str.area, len);
841 cap[h->index][len] = 0;
842 return ACT_RET_CONT;
843}
844
845/* Check an "http-response capture" action.
846 *
847 * The function returns 1 in success case, otherwise, it returns 0 and err is
848 * filled.
849 */
850static int check_http_res_capture(struct act_rule *rule, struct proxy *px, char **err)
851{
852 if (rule->action_ptr != http_action_res_capture_by_id)
853 return 1;
854
Tim Duesterhusf3f4aa02020-07-03 13:43:42 +0200855 /* capture slots can only be declared in frontends, so we can't check their
856 * existence in backends at configuration parsing step
857 */
858 if (px->cap & PR_CAP_FE && rule->arg.capid.idx >= px->nb_rsp_cap) {
Willy Tarreau79e57332018-10-02 16:01:16 +0200859 memprintf(err, "unable to find capture id '%d' referenced by http-response capture rule",
860 rule->arg.capid.idx);
861 return 0;
862 }
863
864 return 1;
865}
866
867/* parse an "http-response capture" action. It takes a single argument which is
868 * a sample fetch expression. It stores the expression into arg->act.p[0] and
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -0700869 * the allocated hdr_cap struct of the preallocated id into arg->act.p[1].
Willy Tarreau79e57332018-10-02 16:01:16 +0200870 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
871 */
872static enum act_parse_ret parse_http_res_capture(const char **args, int *orig_arg, struct proxy *px,
873 struct act_rule *rule, char **err)
874{
875 struct sample_expr *expr;
876 int cur_arg;
877 int id;
878 char *error;
879
880 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
881 if (strcmp(args[cur_arg], "if") == 0 ||
882 strcmp(args[cur_arg], "unless") == 0)
883 break;
884
885 if (cur_arg < *orig_arg + 3) {
886 memprintf(err, "expects <expression> id <idx>");
887 return ACT_RET_PRS_ERR;
888 }
889
890 cur_arg = *orig_arg;
Willy Tarreaue3b57bf2020-02-14 16:50:14 +0100891 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 +0200892 if (!expr)
893 return ACT_RET_PRS_ERR;
894
895 if (!(expr->fetch->val & SMP_VAL_FE_HRS_HDR)) {
896 memprintf(err,
897 "fetch method '%s' extracts information from '%s', none of which is available here",
898 args[cur_arg-1], sample_src_names(expr->fetch->use));
Christopher Faulet1337b322020-01-14 14:50:55 +0100899 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200900 return ACT_RET_PRS_ERR;
901 }
902
903 if (!args[cur_arg] || !*args[cur_arg]) {
904 memprintf(err, "expects 'id'");
Christopher Faulet1337b322020-01-14 14:50:55 +0100905 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200906 return ACT_RET_PRS_ERR;
907 }
908
909 if (strcmp(args[cur_arg], "id") != 0) {
910 memprintf(err, "expects 'id', found '%s'", args[cur_arg]);
Christopher Faulet1337b322020-01-14 14:50:55 +0100911 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200912 return ACT_RET_PRS_ERR;
913 }
914
915 cur_arg++;
916
917 if (!args[cur_arg]) {
918 memprintf(err, "missing id value");
Christopher Faulet1337b322020-01-14 14:50:55 +0100919 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200920 return ACT_RET_PRS_ERR;
921 }
922
923 id = strtol(args[cur_arg], &error, 10);
924 if (*error != '\0') {
925 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
Christopher Faulet1337b322020-01-14 14:50:55 +0100926 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200927 return ACT_RET_PRS_ERR;
928 }
929 cur_arg++;
930
931 px->conf.args.ctx = ARGC_CAP;
932
933 rule->action = ACT_CUSTOM;
934 rule->action_ptr = http_action_res_capture_by_id;
935 rule->check_ptr = check_http_res_capture;
Christopher Faulet2eb53962020-01-14 14:47:34 +0100936 rule->release_ptr = release_http_capture;
Willy Tarreau79e57332018-10-02 16:01:16 +0200937 rule->arg.capid.expr = expr;
938 rule->arg.capid.idx = id;
939
940 *orig_arg = cur_arg;
941 return ACT_RET_PRS_OK;
942}
943
Christopher Faulet81e20172019-12-12 16:40:30 +0100944/* Parse a "allow" action for a request or a response rule. It takes no argument. It
945 * returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
946 */
947static enum act_parse_ret parse_http_allow(const char **args, int *orig_arg, struct proxy *px,
948 struct act_rule *rule, char **err)
949{
950 rule->action = ACT_ACTION_ALLOW;
Christopher Faulet245cf792019-12-18 14:58:12 +0100951 rule->flags |= ACT_FLAG_FINAL;
Christopher Faulet81e20172019-12-12 16:40:30 +0100952 return ACT_RET_PRS_OK;
953}
954
Christopher Faulete0fca292020-01-13 21:49:03 +0100955/* Parse "deny" or "tarpit" actions for a request rule or "deny" action for a
Christopher Faulet5cb513a2020-05-13 17:56:56 +0200956 * response rule. It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on
957 * error. It relies on http_parse_http_reply() to set
958 * <.arg.http_reply>.
Christopher Faulet81e20172019-12-12 16:40:30 +0100959 */
Christopher Faulete0fca292020-01-13 21:49:03 +0100960static enum act_parse_ret parse_http_deny(const char **args, int *orig_arg, struct proxy *px,
961 struct act_rule *rule, char **err)
Christopher Faulet81e20172019-12-12 16:40:30 +0100962{
Christopher Faulet5cb513a2020-05-13 17:56:56 +0200963 int default_status;
964 int cur_arg, arg = 0;
Christopher Faulet81e20172019-12-12 16:40:30 +0100965
966 cur_arg = *orig_arg;
Christopher Faulete0fca292020-01-13 21:49:03 +0100967 if (rule->from == ACT_F_HTTP_REQ) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100968 if (strcmp(args[cur_arg - 1], "tarpit") == 0) {
Christopher Faulete0fca292020-01-13 21:49:03 +0100969 rule->action = ACT_HTTP_REQ_TARPIT;
Christopher Faulet5cb513a2020-05-13 17:56:56 +0200970 default_status = 500;
Christopher Faulet81e20172019-12-12 16:40:30 +0100971 }
Christopher Faulete0fca292020-01-13 21:49:03 +0100972 else {
973 rule->action = ACT_ACTION_DENY;
Christopher Faulet5cb513a2020-05-13 17:56:56 +0200974 default_status = 403;
Christopher Faulet81e20172019-12-12 16:40:30 +0100975 }
Christopher Faulet81e20172019-12-12 16:40:30 +0100976 }
Christopher Faulete0fca292020-01-13 21:49:03 +0100977 else {
Christopher Faulet554c0eb2020-01-14 12:00:28 +0100978 rule->action = ACT_ACTION_DENY;
Christopher Faulet5cb513a2020-05-13 17:56:56 +0200979 default_status = 502;
Christopher Faulete0fca292020-01-13 21:49:03 +0100980 }
Christopher Faulet040c8cd2020-01-13 16:43:45 +0100981
Christopher Faulet5cb513a2020-05-13 17:56:56 +0200982 /* If no args or only a deny_status specified, fallback on the legacy
983 * mode and use default error files despite the fact that
984 * default-errorfiles is not used. Otherwise, parse an http reply.
985 */
Christopher Faulet040c8cd2020-01-13 16:43:45 +0100986
Christopher Faulet5cb513a2020-05-13 17:56:56 +0200987 /* Prepare parsing of log-format strings */
988 px->conf.args.ctx = ((rule->from == ACT_F_HTTP_REQ) ? ARGC_HRQ : ARGC_HRS);
Christopher Faulet554c0eb2020-01-14 12:00:28 +0100989
Christopher Faulet9467f182020-06-30 09:32:01 +0200990 if (!*(args[cur_arg]) || strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
Christopher Faulet5cb513a2020-05-13 17:56:56 +0200991 rule->arg.http_reply = http_parse_http_reply((const char *[]){"default-errorfiles", ""}, &arg, px, default_status, err);
992 goto end;
Christopher Faulet554c0eb2020-01-14 12:00:28 +0100993 }
Christopher Faulet5cb513a2020-05-13 17:56:56 +0200994
995 if (strcmp(args[cur_arg], "deny_status") == 0) {
Christopher Faulet9467f182020-06-30 09:32:01 +0200996 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 +0200997 rule->arg.http_reply = http_parse_http_reply((const char *[]){"status", args[cur_arg+1], "default-errorfiles", ""},
998 &arg, px, default_status, err);
999 *orig_arg += 2;
1000 goto end;
Christopher Faulet554c0eb2020-01-14 12:00:28 +01001001 }
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001002 args[cur_arg] += 5; /* skip "deny_" for the parsing */
Christopher Faulet554c0eb2020-01-14 12:00:28 +01001003 }
1004
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001005 rule->arg.http_reply = http_parse_http_reply(args, orig_arg, px, default_status, err);
Christopher Faulet554c0eb2020-01-14 12:00:28 +01001006
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001007 end:
1008 if (!rule->arg.http_reply)
1009 return ACT_RET_PRS_ERR;
1010
1011 rule->flags |= ACT_FLAG_FINAL;
1012 rule->check_ptr = check_act_http_reply;
1013 rule->release_ptr = release_act_http_reply;
Christopher Faulet81e20172019-12-12 16:40:30 +01001014 return ACT_RET_PRS_OK;
1015}
1016
Christopher Fauletb3048832020-05-27 15:26:43 +02001017
1018/* This function executes a auth action. It builds an 401/407 HTX message using
1019 * the corresponding proxy's error message. On success, it returns
1020 * ACT_RET_ABRT. If an error occurs ACT_RET_ERR is returned.
1021 */
1022static enum act_return http_action_auth(struct act_rule *rule, struct proxy *px,
1023 struct session *sess, struct stream *s, int flags)
1024{
1025 struct channel *req = &s->req;
1026 struct channel *res = &s->res;
1027 struct htx *htx = htx_from_buf(&res->buf);
1028 struct http_reply *reply;
1029 const char *auth_realm;
1030 struct http_hdr_ctx ctx;
1031 struct ist hdr;
1032
1033 /* Auth might be performed on regular http-req rules as well as on stats */
1034 auth_realm = rule->arg.http.str.ptr;
1035 if (!auth_realm) {
1036 if (px->uri_auth && s->current_rule_list == &px->uri_auth->http_req_rules)
1037 auth_realm = STATS_DEFAULT_REALM;
1038 else
1039 auth_realm = px->id;
1040 }
1041
1042 if (!(s->txn->flags & TX_USE_PX_CONN)) {
1043 s->txn->status = 401;
1044 hdr = ist("WWW-Authenticate");
1045 }
1046 else {
1047 s->txn->status = 407;
1048 hdr = ist("Proxy-Authenticate");
1049 }
1050 reply = http_error_message(s);
1051 channel_htx_truncate(res, htx);
1052
1053 if (chunk_printf(&trash, "Basic realm=\"%s\"", auth_realm) == -1)
1054 goto fail;
1055
1056 /* Write the generic 40x message */
1057 if (http_reply_to_htx(s, htx, reply) == -1)
1058 goto fail;
1059
1060 /* Remove all existing occurrences of the XXX-Authenticate header */
1061 ctx.blk = NULL;
1062 while (http_find_header(htx, hdr, &ctx, 1))
1063 http_remove_header(htx, &ctx);
1064
1065 /* Now a the right XXX-Authenticate header */
1066 if (!http_add_header(htx, hdr, ist2(b_orig(&trash), b_data(&trash))))
1067 goto fail;
1068
1069 /* Finally forward the reply */
1070 htx_to_buf(htx, &res->buf);
1071 if (!http_forward_proxy_resp(s, 1))
1072 goto fail;
1073
1074 /* Note: Only eval on the request */
1075 s->logs.tv_request = now;
1076 req->analysers &= AN_REQ_FLT_END;
1077
1078 if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Willy Tarreau4781b152021-04-06 13:53:36 +02001079 _HA_ATOMIC_INC(&s->sess->fe->fe_counters.intercepted_req);
Christopher Fauletb3048832020-05-27 15:26:43 +02001080
1081 if (!(s->flags & SF_ERR_MASK))
1082 s->flags |= SF_ERR_LOCAL;
1083 if (!(s->flags & SF_FINST_MASK))
1084 s->flags |= SF_FINST_R;
1085
1086 stream_inc_http_err_ctr(s);
1087 return ACT_RET_ABRT;
1088
1089 fail:
1090 /* If an error occurred, remove the incomplete HTTP response from the
1091 * buffer */
1092 channel_htx_truncate(res, htx);
1093 return ACT_RET_ERR;
1094}
1095
Christopher Faulet81e20172019-12-12 16:40:30 +01001096/* Parse a "auth" action. It may take 2 optional arguments to define a "realm"
1097 * parameter. It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
1098 */
1099static enum act_parse_ret parse_http_auth(const char **args, int *orig_arg, struct proxy *px,
1100 struct act_rule *rule, char **err)
1101{
1102 int cur_arg;
1103
Christopher Fauletb3048832020-05-27 15:26:43 +02001104 rule->action = ACT_CUSTOM;
Christopher Faulet245cf792019-12-18 14:58:12 +01001105 rule->flags |= ACT_FLAG_FINAL;
Christopher Fauletb3048832020-05-27 15:26:43 +02001106 rule->action_ptr = http_action_auth;
Christopher Faulet2eb53962020-01-14 14:47:34 +01001107 rule->release_ptr = release_http_action;
Christopher Faulet81e20172019-12-12 16:40:30 +01001108
1109 cur_arg = *orig_arg;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001110 if (strcmp(args[cur_arg], "realm") == 0) {
Christopher Faulet81e20172019-12-12 16:40:30 +01001111 cur_arg++;
1112 if (!*args[cur_arg]) {
1113 memprintf(err, "missing realm value.\n");
1114 return ACT_RET_PRS_ERR;
1115 }
Christopher Faulet96bff762019-12-17 13:46:18 +01001116 rule->arg.http.str.ptr = strdup(args[cur_arg]);
1117 rule->arg.http.str.len = strlen(rule->arg.http.str.ptr);
Christopher Faulet81e20172019-12-12 16:40:30 +01001118 cur_arg++;
1119 }
1120
Christopher Fauletc20b3712020-01-27 15:51:56 +01001121 LIST_INIT(&rule->arg.http.fmt);
Christopher Faulet81e20172019-12-12 16:40:30 +01001122 *orig_arg = cur_arg;
1123 return ACT_RET_PRS_OK;
1124}
1125
1126/* Parse a "set-nice" action. It takes the nice value as argument. It returns
1127 * ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
1128 */
1129static enum act_parse_ret parse_http_set_nice(const char **args, int *orig_arg, struct proxy *px,
1130 struct act_rule *rule, char **err)
1131{
1132 int cur_arg;
1133
1134 rule->action = ACT_HTTP_SET_NICE;
1135
1136 cur_arg = *orig_arg;
1137 if (!*args[cur_arg]) {
1138 memprintf(err, "expects exactly 1 argument (integer value)");
1139 return ACT_RET_PRS_ERR;
1140 }
Christopher Faulet96bff762019-12-17 13:46:18 +01001141 rule->arg.http.i = atoi(args[cur_arg]);
1142 if (rule->arg.http.i < -1024)
1143 rule->arg.http.i = -1024;
1144 else if (rule->arg.http.i > 1024)
1145 rule->arg.http.i = 1024;
Christopher Faulet81e20172019-12-12 16:40:30 +01001146
Christopher Fauletc20b3712020-01-27 15:51:56 +01001147 LIST_INIT(&rule->arg.http.fmt);
Christopher Faulet81e20172019-12-12 16:40:30 +01001148 *orig_arg = cur_arg + 1;
1149 return ACT_RET_PRS_OK;
1150}
1151
1152/* Parse a "set-tos" action. It takes the TOS value as argument. It returns
1153 * ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
1154 */
1155static enum act_parse_ret parse_http_set_tos(const char **args, int *orig_arg, struct proxy *px,
1156 struct act_rule *rule, char **err)
1157{
1158#ifdef IP_TOS
1159 char *endp;
1160 int cur_arg;
1161
1162 rule->action = ACT_HTTP_SET_TOS;
1163
1164 cur_arg = *orig_arg;
1165 if (!*args[cur_arg]) {
1166 memprintf(err, "expects exactly 1 argument (integer/hex value)");
1167 return ACT_RET_PRS_ERR;
1168 }
Christopher Faulet96bff762019-12-17 13:46:18 +01001169 rule->arg.http.i = strtol(args[cur_arg], &endp, 0);
Christopher Faulet81e20172019-12-12 16:40:30 +01001170 if (endp && *endp != '\0') {
1171 memprintf(err, "invalid character starting at '%s' (integer/hex value expected)", endp);
1172 return ACT_RET_PRS_ERR;
1173 }
1174
Christopher Fauletc20b3712020-01-27 15:51:56 +01001175 LIST_INIT(&rule->arg.http.fmt);
Christopher Faulet81e20172019-12-12 16:40:30 +01001176 *orig_arg = cur_arg + 1;
1177 return ACT_RET_PRS_OK;
1178#else
1179 memprintf(err, "not supported on this platform (IP_TOS undefined)");
1180 return ACT_RET_PRS_ERR;
1181#endif
1182}
1183
1184/* Parse a "set-mark" action. It takes the MARK value as argument. It returns
1185 * ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
1186 */
1187static enum act_parse_ret parse_http_set_mark(const char **args, int *orig_arg, struct proxy *px,
1188 struct act_rule *rule, char **err)
1189{
1190#ifdef SO_MARK
1191 char *endp;
1192 int cur_arg;
1193
1194 rule->action = ACT_HTTP_SET_MARK;
1195
1196 cur_arg = *orig_arg;
1197 if (!*args[cur_arg]) {
1198 memprintf(err, "expects exactly 1 argument (integer/hex value)");
1199 return ACT_RET_PRS_ERR;
1200 }
Christopher Faulet96bff762019-12-17 13:46:18 +01001201 rule->arg.http.i = strtoul(args[cur_arg], &endp, 0);
Christopher Faulet81e20172019-12-12 16:40:30 +01001202 if (endp && *endp != '\0') {
1203 memprintf(err, "invalid character starting at '%s' (integer/hex value expected)", endp);
1204 return ACT_RET_PRS_ERR;
1205 }
1206
Christopher Fauletc20b3712020-01-27 15:51:56 +01001207 LIST_INIT(&rule->arg.http.fmt);
Christopher Faulet81e20172019-12-12 16:40:30 +01001208 *orig_arg = cur_arg + 1;
1209 global.last_checks |= LSTCHK_NETADM;
1210 return ACT_RET_PRS_OK;
1211#else
1212 memprintf(err, "not supported on this platform (SO_MARK undefined)");
1213 return ACT_RET_PRS_ERR;
1214#endif
1215}
1216
1217/* Parse a "set-log-level" action. It takes the level value as argument. It
1218 * returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
1219 */
1220static enum act_parse_ret parse_http_set_log_level(const char **args, int *orig_arg, struct proxy *px,
1221 struct act_rule *rule, char **err)
1222{
1223 int cur_arg;
1224
1225 rule->action = ACT_HTTP_SET_LOGL;
1226
1227 cur_arg = *orig_arg;
1228 if (!*args[cur_arg]) {
1229 bad_log_level:
1230 memprintf(err, "expects exactly 1 argument (log level name or 'silent')");
1231 return ACT_RET_PRS_ERR;
1232 }
1233 if (strcmp(args[cur_arg], "silent") == 0)
Christopher Faulet96bff762019-12-17 13:46:18 +01001234 rule->arg.http.i = -1;
1235 else if ((rule->arg.http.i = get_log_level(args[cur_arg]) + 1) == 0)
Christopher Faulet81e20172019-12-12 16:40:30 +01001236 goto bad_log_level;
1237
Christopher Fauletc20b3712020-01-27 15:51:56 +01001238 LIST_INIT(&rule->arg.http.fmt);
Christopher Faulet81e20172019-12-12 16:40:30 +01001239 *orig_arg = cur_arg + 1;
1240 return ACT_RET_PRS_OK;
1241}
1242
Christopher Faulet91b3ec12020-01-17 22:30:06 +01001243/* This function executes a early-hint action. It adds an HTTP Early Hint HTTP
1244 * 103 response header with <.arg.http.str> name and with a value built
1245 * according to <.arg.http.fmt> log line format. If it is the first early-hint
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001246 * rule of series, the 103 response start-line is added first. At the end, if
Christopher Faulet91b3ec12020-01-17 22:30:06 +01001247 * the next rule is not an early-hint rule or if it is the last rule, the EOH
1248 * block is added to terminate the response. On success, it returns
1249 * ACT_RET_CONT. If an error occurs while soft rewrites are enabled, the action
1250 * is canceled, but the rule processing continue. Otherwsize ACT_RET_ERR is
1251 * returned.
1252 */
1253static enum act_return http_action_early_hint(struct act_rule *rule, struct proxy *px,
1254 struct session *sess, struct stream *s, int flags)
1255{
1256 struct act_rule *prev_rule, *next_rule;
1257 struct channel *res = &s->res;
1258 struct htx *htx = htx_from_buf(&res->buf);
1259 struct buffer *value = alloc_trash_chunk();
1260 enum act_return ret = ACT_RET_CONT;
1261
1262 if (!(s->txn->req.flags & HTTP_MSGF_VER_11))
1263 goto leave;
1264
1265 if (!value) {
1266 if (!(s->flags & SF_ERR_MASK))
1267 s->flags |= SF_ERR_RESOURCE;
1268 goto error;
1269 }
1270
1271 /* get previous and next rules */
1272 prev_rule = LIST_PREV(&rule->list, typeof(rule), list);
1273 next_rule = LIST_NEXT(&rule->list, typeof(rule), list);
1274
1275 /* if no previous rule or previous rule is not early-hint, start a new response. Otherwise,
1276 * continue to add link to a previously started response */
1277 if (&prev_rule->list == s->current_rule_list || prev_rule->action_ptr != http_action_early_hint) {
1278 struct htx_sl *sl;
1279 unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|
1280 HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
1281
1282 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
1283 ist("HTTP/1.1"), ist("103"), ist("Early Hints"));
1284 if (!sl)
1285 goto error;
1286 sl->info.res.status = 103;
1287 }
1288
1289 /* Add the HTTP Early Hint HTTP 103 response heade */
1290 value->data = build_logline(s, b_tail(value), b_room(value), &rule->arg.http.fmt);
1291 if (!htx_add_header(htx, rule->arg.http.str, ist2(b_head(value), b_data(value))))
1292 goto error;
1293
1294 /* if it is the last rule or the next one is not an early-hint, terminate the current
1295 * response. */
1296 if (&next_rule->list == s->current_rule_list || next_rule->action_ptr != http_action_early_hint) {
Christopher Faulet91b3ec12020-01-17 22:30:06 +01001297 if (!htx_add_endof(htx, HTX_BLK_EOH)) {
1298 /* If an error occurred during an Early-hint rule,
1299 * remove the incomplete HTTP 103 response from the
1300 * buffer */
1301 goto error;
1302 }
1303
Christopher Fauleta72a7e42020-01-28 09:28:11 +01001304 if (!http_forward_proxy_resp(s, 0))
1305 goto error;
Christopher Faulet91b3ec12020-01-17 22:30:06 +01001306 }
1307
1308 leave:
1309 free_trash_chunk(value);
1310 return ret;
1311
1312 error:
1313 /* If an error occurred during an Early-hint rule, remove the incomplete
1314 * HTTP 103 response from the buffer */
1315 channel_htx_truncate(res, htx);
1316 ret = ACT_RET_ERR;
1317 goto leave;
1318}
1319
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001320/* This function executes a set-header or add-header actions. It builds a string
1321 * in the trash from the specified format string. It finds the action to be
1322 * performed in <.action>, previously filled by function parse_set_header(). The
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001323 * replacement action is executed by the function http_action_set_header(). On
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001324 * success, it returns ACT_RET_CONT. If an error occurs while soft rewrites are
1325 * enabled, the action is canceled, but the rule processing continue. Otherwsize
1326 * ACT_RET_ERR is returned.
1327 */
1328static enum act_return http_action_set_header(struct act_rule *rule, struct proxy *px,
1329 struct session *sess, struct stream *s, int flags)
1330{
Christopher Faulet91e31d82020-01-24 15:37:13 +01001331 struct http_msg *msg = ((rule->from == ACT_F_HTTP_REQ) ? &s->txn->req : &s->txn->rsp);
1332 struct htx *htx = htxbuf(&msg->chn->buf);
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001333 enum act_return ret = ACT_RET_CONT;
1334 struct buffer *replace;
1335 struct http_hdr_ctx ctx;
1336 struct ist n, v;
1337
1338 replace = alloc_trash_chunk();
1339 if (!replace)
1340 goto fail_alloc;
1341
1342 replace->data = build_logline(s, replace->area, replace->size, &rule->arg.http.fmt);
1343 n = rule->arg.http.str;
1344 v = ist2(replace->area, replace->data);
1345
1346 if (rule->action == 0) { // set-header
1347 /* remove all occurrences of the header */
1348 ctx.blk = NULL;
1349 while (http_find_header(htx, n, &ctx, 1))
1350 http_remove_header(htx, &ctx);
1351 }
1352
1353 /* Now add header */
1354 if (!http_add_header(htx, n, v))
1355 goto fail_rewrite;
1356
1357 leave:
1358 free_trash_chunk(replace);
1359 return ret;
1360
1361 fail_alloc:
1362 if (!(s->flags & SF_ERR_MASK))
1363 s->flags |= SF_ERR_RESOURCE;
1364 ret = ACT_RET_ERR;
1365 goto leave;
1366
1367 fail_rewrite:
Willy Tarreau4781b152021-04-06 13:53:36 +02001368 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_rewrites);
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001369 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +02001370 _HA_ATOMIC_INC(&s->be->be_counters.failed_rewrites);
William Lallemand36119de2021-03-08 15:26:48 +01001371 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001372 _HA_ATOMIC_INC(&sess->listener->counters->failed_rewrites);
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001373 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001374 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_rewrites);
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001375
Christopher Faulet333bf8c2020-01-22 14:38:05 +01001376 if (!(msg->flags & HTTP_MSGF_SOFT_RW)) {
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001377 ret = ACT_RET_ERR;
Christopher Faulet333bf8c2020-01-22 14:38:05 +01001378 if (!(s->flags & SF_ERR_MASK))
1379 s->flags |= SF_ERR_PRXCOND;
1380 }
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001381 goto leave;
1382}
1383
Christopher Faulet81e20172019-12-12 16:40:30 +01001384/* Parse a "set-header", "add-header" or "early-hint" actions. It takes an
1385 * header name and a log-format string as arguments. It returns ACT_RET_PRS_OK
1386 * on success, ACT_RET_PRS_ERR on error.
1387 *
1388 * Note: same function is used for the request and the response. However
1389 * "early-hint" rules are only supported for request rules.
1390 */
1391static enum act_parse_ret parse_http_set_header(const char **args, int *orig_arg, struct proxy *px,
1392 struct act_rule *rule, char **err)
1393{
Christopher Faulet81e20172019-12-12 16:40:30 +01001394 int cap, cur_arg;
1395
Christopher Faulet91b3ec12020-01-17 22:30:06 +01001396 if (args[*orig_arg-1][0] == 'e') {
1397 rule->action = ACT_CUSTOM;
1398 rule->action_ptr = http_action_early_hint;
1399 }
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001400 else {
1401 if (args[*orig_arg-1][0] == 's')
1402 rule->action = 0; // set-header
1403 else
1404 rule->action = 1; // add-header
1405 rule->action_ptr = http_action_set_header;
1406 }
Christopher Faulet2eb53962020-01-14 14:47:34 +01001407 rule->release_ptr = release_http_action;
Christopher Faulet81e20172019-12-12 16:40:30 +01001408
1409 cur_arg = *orig_arg;
1410 if (!*args[cur_arg] || !*args[cur_arg+1]) {
1411 memprintf(err, "expects exactly 2 arguments");
1412 return ACT_RET_PRS_ERR;
1413 }
1414
Christopher Faulet81e20172019-12-12 16:40:30 +01001415
Christopher Faulet96bff762019-12-17 13:46:18 +01001416 rule->arg.http.str.ptr = strdup(args[cur_arg]);
1417 rule->arg.http.str.len = strlen(rule->arg.http.str.ptr);
1418 LIST_INIT(&rule->arg.http.fmt);
Christopher Faulet81e20172019-12-12 16:40:30 +01001419
1420 if (rule->from == ACT_F_HTTP_REQ) {
1421 px->conf.args.ctx = ARGC_HRQ;
1422 cap = (px->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR;
1423 }
1424 else{
1425 px->conf.args.ctx = ARGC_HRS;
1426 cap = (px->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR;
1427 }
1428
1429 cur_arg++;
Christopher Faulet1337b322020-01-14 14:50:55 +01001430 if (!parse_logformat_string(args[cur_arg], px, &rule->arg.http.fmt, LOG_OPT_HTTP, cap, err)) {
Tim Duesterhused526372020-03-05 17:56:33 +01001431 istfree(&rule->arg.http.str);
Christopher Faulet81e20172019-12-12 16:40:30 +01001432 return ACT_RET_PRS_ERR;
Christopher Faulet1337b322020-01-14 14:50:55 +01001433 }
Christopher Faulet81e20172019-12-12 16:40:30 +01001434
1435 free(px->conf.lfs_file);
1436 px->conf.lfs_file = strdup(px->conf.args.file);
1437 px->conf.lfs_line = px->conf.args.line;
1438
1439 *orig_arg = cur_arg + 1;
1440 return ACT_RET_PRS_OK;
1441}
1442
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001443/* This function executes a replace-header or replace-value actions. It
1444 * builds a string in the trash from the specified format string. It finds
1445 * the action to be performed in <.action>, previously filled by function
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001446 * parse_replace_header(). The replacement action is executed by the function
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001447 * http_action_replace_header(). On success, it returns ACT_RET_CONT. If an error
1448 * occurs while soft rewrites are enabled, the action is canceled, but the rule
1449 * processing continue. Otherwsize ACT_RET_ERR is returned.
1450 */
1451static enum act_return http_action_replace_header(struct act_rule *rule, struct proxy *px,
1452 struct session *sess, struct stream *s, int flags)
1453{
Christopher Faulet91e31d82020-01-24 15:37:13 +01001454 struct http_msg *msg = ((rule->from == ACT_F_HTTP_REQ) ? &s->txn->req : &s->txn->rsp);
1455 struct htx *htx = htxbuf(&msg->chn->buf);
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001456 enum act_return ret = ACT_RET_CONT;
1457 struct buffer *replace;
1458 int r;
1459
1460 replace = alloc_trash_chunk();
1461 if (!replace)
1462 goto fail_alloc;
1463
1464 replace->data = build_logline(s, replace->area, replace->size, &rule->arg.http.fmt);
1465
1466 r = http_replace_hdrs(s, htx, rule->arg.http.str, replace->area, rule->arg.http.re, (rule->action == 0));
1467 if (r == -1)
1468 goto fail_rewrite;
1469
1470 leave:
1471 free_trash_chunk(replace);
1472 return ret;
1473
1474 fail_alloc:
1475 if (!(s->flags & SF_ERR_MASK))
1476 s->flags |= SF_ERR_RESOURCE;
1477 ret = ACT_RET_ERR;
1478 goto leave;
1479
1480 fail_rewrite:
Willy Tarreau4781b152021-04-06 13:53:36 +02001481 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_rewrites);
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001482 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +02001483 _HA_ATOMIC_INC(&s->be->be_counters.failed_rewrites);
William Lallemand36119de2021-03-08 15:26:48 +01001484 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001485 _HA_ATOMIC_INC(&sess->listener->counters->failed_rewrites);
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001486 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001487 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_rewrites);
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001488
Christopher Faulet333bf8c2020-01-22 14:38:05 +01001489 if (!(msg->flags & HTTP_MSGF_SOFT_RW)) {
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001490 ret = ACT_RET_ERR;
Christopher Faulet333bf8c2020-01-22 14:38:05 +01001491 if (!(s->flags & SF_ERR_MASK))
1492 s->flags |= SF_ERR_PRXCOND;
1493 }
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001494 goto leave;
1495}
1496
Christopher Faulet81e20172019-12-12 16:40:30 +01001497/* Parse a "replace-header" or "replace-value" actions. It takes an header name,
1498 * a regex and replacement string as arguments. It returns ACT_RET_PRS_OK on
1499 * success, ACT_RET_PRS_ERR on error.
1500 */
1501static enum act_parse_ret parse_http_replace_header(const char **args, int *orig_arg, struct proxy *px,
1502 struct act_rule *rule, char **err)
1503{
1504 int cap, cur_arg;
1505
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001506 if (args[*orig_arg-1][8] == 'h')
1507 rule->action = 0; // replace-header
1508 else
1509 rule->action = 1; // replace-value
1510 rule->action_ptr = http_action_replace_header;
Christopher Faulet2eb53962020-01-14 14:47:34 +01001511 rule->release_ptr = release_http_action;
Christopher Faulet81e20172019-12-12 16:40:30 +01001512
1513 cur_arg = *orig_arg;
1514 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2]) {
1515 memprintf(err, "expects exactly 3 arguments");
1516 return ACT_RET_PRS_ERR;
1517 }
1518
Christopher Faulet96bff762019-12-17 13:46:18 +01001519 rule->arg.http.str.ptr = strdup(args[cur_arg]);
1520 rule->arg.http.str.len = strlen(rule->arg.http.str.ptr);
1521 LIST_INIT(&rule->arg.http.fmt);
Christopher Faulet81e20172019-12-12 16:40:30 +01001522
1523 cur_arg++;
Christopher Faulet1337b322020-01-14 14:50:55 +01001524 if (!(rule->arg.http.re = regex_comp(args[cur_arg], 1, 1, err))) {
Tim Duesterhused526372020-03-05 17:56:33 +01001525 istfree(&rule->arg.http.str);
Christopher Faulet81e20172019-12-12 16:40:30 +01001526 return ACT_RET_PRS_ERR;
Christopher Faulet1337b322020-01-14 14:50:55 +01001527 }
Christopher Faulet81e20172019-12-12 16:40:30 +01001528
1529 if (rule->from == ACT_F_HTTP_REQ) {
1530 px->conf.args.ctx = ARGC_HRQ;
1531 cap = (px->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR;
1532 }
1533 else{
1534 px->conf.args.ctx = ARGC_HRS;
1535 cap = (px->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR;
1536 }
1537
1538 cur_arg++;
Christopher Faulet1337b322020-01-14 14:50:55 +01001539 if (!parse_logformat_string(args[cur_arg], px, &rule->arg.http.fmt, LOG_OPT_HTTP, cap, err)) {
Tim Duesterhused526372020-03-05 17:56:33 +01001540 istfree(&rule->arg.http.str);
Christopher Faulet1337b322020-01-14 14:50:55 +01001541 regex_free(rule->arg.http.re);
Christopher Faulet81e20172019-12-12 16:40:30 +01001542 return ACT_RET_PRS_ERR;
Christopher Faulet1337b322020-01-14 14:50:55 +01001543 }
Christopher Faulet81e20172019-12-12 16:40:30 +01001544
1545 free(px->conf.lfs_file);
1546 px->conf.lfs_file = strdup(px->conf.args.file);
1547 px->conf.lfs_line = px->conf.args.line;
1548
1549 *orig_arg = cur_arg + 1;
1550 return ACT_RET_PRS_OK;
1551}
1552
Maciej Zdebebdd4c52020-11-20 13:58:48 +00001553/* This function executes a del-header action with selected matching mode for
1554 * header name. It finds the matching method to be performed in <.action>, previously
1555 * filled by function parse_http_del_header(). On success, it returns ACT_RET_CONT.
1556 * Otherwise ACT_RET_ERR is returned.
1557 */
1558static enum act_return http_action_del_header(struct act_rule *rule, struct proxy *px,
1559 struct session *sess, struct stream *s, int flags)
1560{
1561 struct http_hdr_ctx ctx;
1562 struct http_msg *msg = ((rule->from == ACT_F_HTTP_REQ) ? &s->txn->req : &s->txn->rsp);
1563 struct htx *htx = htxbuf(&msg->chn->buf);
1564 enum act_return ret = ACT_RET_CONT;
1565
1566 /* remove all occurrences of the header */
1567 ctx.blk = NULL;
1568 switch (rule->action) {
1569 case PAT_MATCH_STR:
1570 while (http_find_header(htx, rule->arg.http.str, &ctx, 1))
1571 http_remove_header(htx, &ctx);
1572 break;
1573 case PAT_MATCH_BEG:
1574 while (http_find_pfx_header(htx, rule->arg.http.str, &ctx, 1))
1575 http_remove_header(htx, &ctx);
1576 break;
1577 case PAT_MATCH_END:
1578 while (http_find_sfx_header(htx, rule->arg.http.str, &ctx, 1))
1579 http_remove_header(htx, &ctx);
1580 break;
1581 case PAT_MATCH_SUB:
1582 while (http_find_sub_header(htx, rule->arg.http.str, &ctx, 1))
1583 http_remove_header(htx, &ctx);
1584 break;
1585 case PAT_MATCH_REG:
1586 while (http_match_header(htx, rule->arg.http.re, &ctx, 1))
1587 http_remove_header(htx, &ctx);
1588 break;
1589 default:
1590 return ACT_RET_ERR;
1591 }
1592 return ret;
1593}
1594
1595/* Parse a "del-header" action. It takes string as a required argument,
1596 * optional flag (currently only -m) and optional matching method of input string
1597 * with header name to be deleted. Default matching method is exact match (-m str).
1598 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Christopher Faulet81e20172019-12-12 16:40:30 +01001599 */
1600static enum act_parse_ret parse_http_del_header(const char **args, int *orig_arg, struct proxy *px,
1601 struct act_rule *rule, char **err)
1602{
1603 int cur_arg;
Maciej Zdebebdd4c52020-11-20 13:58:48 +00001604 int pat_idx;
Christopher Faulet81e20172019-12-12 16:40:30 +01001605
Maciej Zdebebdd4c52020-11-20 13:58:48 +00001606 /* set exact matching (-m str) as default */
1607 rule->action = PAT_MATCH_STR;
1608 rule->action_ptr = http_action_del_header;
Christopher Faulet2eb53962020-01-14 14:47:34 +01001609 rule->release_ptr = release_http_action;
Christopher Faulet81e20172019-12-12 16:40:30 +01001610
1611 cur_arg = *orig_arg;
1612 if (!*args[cur_arg]) {
Maciej Zdebebdd4c52020-11-20 13:58:48 +00001613 memprintf(err, "expects at least 1 argument");
Christopher Faulet81e20172019-12-12 16:40:30 +01001614 return ACT_RET_PRS_ERR;
1615 }
1616
Christopher Faulet96bff762019-12-17 13:46:18 +01001617 rule->arg.http.str.ptr = strdup(args[cur_arg]);
1618 rule->arg.http.str.len = strlen(rule->arg.http.str.ptr);
Christopher Faulet81e20172019-12-12 16:40:30 +01001619 px->conf.args.ctx = (rule->from == ACT_F_HTTP_REQ ? ARGC_HRQ : ARGC_HRS);
1620
Maciej Zdeb6dee9962020-11-23 16:03:09 +00001621 LIST_INIT(&rule->arg.http.fmt);
Maciej Zdebebdd4c52020-11-20 13:58:48 +00001622 if (strcmp(args[cur_arg+1], "-m") == 0) {
1623 cur_arg++;
1624 if (!*args[cur_arg+1]) {
1625 memprintf(err, "-m flag expects exactly 1 argument");
1626 return ACT_RET_PRS_ERR;
1627 }
1628
1629 cur_arg++;
1630 pat_idx = pat_find_match_name(args[cur_arg]);
1631 switch (pat_idx) {
1632 case PAT_MATCH_REG:
1633 if (!(rule->arg.http.re = regex_comp(rule->arg.http.str.ptr, 1, 1, err)))
1634 return ACT_RET_PRS_ERR;
1635 /* fall through */
1636 case PAT_MATCH_STR:
1637 case PAT_MATCH_BEG:
1638 case PAT_MATCH_END:
1639 case PAT_MATCH_SUB:
1640 rule->action = pat_idx;
1641 break;
1642 default:
1643 memprintf(err, "-m with unsupported matching method '%s'", args[cur_arg]);
1644 return ACT_RET_PRS_ERR;
1645 }
1646 }
1647
Christopher Faulet81e20172019-12-12 16:40:30 +01001648 *orig_arg = cur_arg + 1;
1649 return ACT_RET_PRS_OK;
1650}
1651
Christopher Faulet2eb53962020-01-14 14:47:34 +01001652/* Release memory allocated by an http redirect action. */
1653static void release_http_redir(struct act_rule *rule)
1654{
1655 struct logformat_node *lf, *lfb;
1656 struct redirect_rule *redir;
1657
1658 redir = rule->arg.redir;
1659 LIST_DEL(&redir->list);
1660 if (redir->cond) {
1661 prune_acl_cond(redir->cond);
1662 free(redir->cond);
1663 }
1664 free(redir->rdr_str);
1665 free(redir->cookie_str);
1666 list_for_each_entry_safe(lf, lfb, &redir->rdr_fmt, list) {
1667 LIST_DEL(&lf->list);
1668 free(lf);
1669 }
1670 free(redir);
1671}
1672
Christopher Faulet81e20172019-12-12 16:40:30 +01001673/* Parse a "redirect" action. It returns ACT_RET_PRS_OK on success,
1674 * ACT_RET_PRS_ERR on error.
1675 */
1676static enum act_parse_ret parse_http_redirect(const char **args, int *orig_arg, struct proxy *px,
1677 struct act_rule *rule, char **err)
1678{
1679 struct redirect_rule *redir;
1680 int dir, cur_arg;
1681
1682 rule->action = ACT_HTTP_REDIR;
Christopher Faulet245cf792019-12-18 14:58:12 +01001683 rule->flags |= ACT_FLAG_FINAL;
Christopher Faulet2eb53962020-01-14 14:47:34 +01001684 rule->release_ptr = release_http_redir;
Christopher Faulet81e20172019-12-12 16:40:30 +01001685
1686 cur_arg = *orig_arg;
1687
1688 dir = (rule->from == ACT_F_HTTP_REQ ? 0 : 1);
1689 if ((redir = http_parse_redirect_rule(px->conf.args.file, px->conf.args.line, px, &args[cur_arg], err, 1, dir)) == NULL)
1690 return ACT_RET_PRS_ERR;
1691
1692 rule->arg.redir = redir;
1693 rule->cond = redir->cond;
1694 redir->cond = NULL;
1695
1696 /* skip all arguments */
1697 while (*args[cur_arg])
1698 cur_arg++;
1699
1700 *orig_arg = cur_arg;
1701 return ACT_RET_PRS_OK;
1702}
1703
Christopher Faulet046cf442019-12-17 15:45:23 +01001704/* This function executes a add-acl, del-acl, set-map or del-map actions. On
1705 * success, it returns ACT_RET_CONT. Otherwsize ACT_RET_ERR is returned.
1706 */
1707static enum act_return http_action_set_map(struct act_rule *rule, struct proxy *px,
1708 struct session *sess, struct stream *s, int flags)
1709{
1710 struct pat_ref *ref;
1711 struct buffer *key = NULL, *value = NULL;
1712 enum act_return ret = ACT_RET_CONT;
1713
1714 /* collect reference */
1715 ref = pat_ref_lookup(rule->arg.map.ref);
1716 if (!ref)
1717 goto leave;
1718
1719 /* allocate key */
1720 key = alloc_trash_chunk();
1721 if (!key)
1722 goto fail_alloc;
1723
1724 /* collect key */
1725 key->data = build_logline(s, key->area, key->size, &rule->arg.map.key);
1726 key->area[key->data] = '\0';
1727
1728 switch (rule->action) {
1729 case 0: // add-acl
1730 /* add entry only if it does not already exist */
1731 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
1732 if (pat_ref_find_elt(ref, key->area) == NULL)
1733 pat_ref_add(ref, key->area, NULL, NULL);
1734 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
1735 break;
1736
1737 case 1: // set-map
1738 /* allocate value */
1739 value = alloc_trash_chunk();
1740 if (!value)
1741 goto fail_alloc;
1742
1743 /* collect value */
1744 value->data = build_logline(s, value->area, value->size, &rule->arg.map.value);
1745 value->area[value->data] = '\0';
1746
1747 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
1748 if (pat_ref_find_elt(ref, key->area) != NULL) {
1749 /* update entry if it exists */
1750 pat_ref_set(ref, key->area, value->area, NULL);
1751 }
1752 else {
1753 /* insert a new entry */
1754 pat_ref_add(ref, key->area, value->area, NULL);
1755 }
1756 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
1757 break;
1758
1759 case 2: // del-acl
1760 case 3: // del-map
1761 /* returned code: 1=ok, 0=ko */
1762 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
1763 pat_ref_delete(ref, key->area);
1764 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
1765 break;
1766
1767 default:
1768 ret = ACT_RET_ERR;
1769 }
1770
1771
1772 leave:
1773 free_trash_chunk(key);
1774 free_trash_chunk(value);
1775 return ret;
1776
1777 fail_alloc:
1778 if (!(s->flags & SF_ERR_MASK))
1779 s->flags |= SF_ERR_RESOURCE;
1780 ret = ACT_RET_ERR;
1781 goto leave;
1782}
1783
Christopher Faulet2eb53962020-01-14 14:47:34 +01001784/* Release memory allocated by an http map/acl action. */
1785static void release_http_map(struct act_rule *rule)
1786{
1787 struct logformat_node *lf, *lfb;
1788
1789 free(rule->arg.map.ref);
1790 list_for_each_entry_safe(lf, lfb, &rule->arg.map.key, list) {
1791 LIST_DEL(&lf->list);
1792 release_sample_expr(lf->expr);
1793 free(lf->arg);
1794 free(lf);
1795 }
1796 if (rule->action == 1) {
1797 list_for_each_entry_safe(lf, lfb, &rule->arg.map.value, list) {
1798 LIST_DEL(&lf->list);
1799 release_sample_expr(lf->expr);
1800 free(lf->arg);
1801 free(lf);
1802 }
1803 }
1804}
1805
Christopher Faulet81e20172019-12-12 16:40:30 +01001806/* Parse a "add-acl", "del-acl", "set-map" or "del-map" actions. It takes one or
Christopher Faulet046cf442019-12-17 15:45:23 +01001807 * two log-format string as argument depending on the action. The action is
1808 * stored in <.action> as an int (0=add-acl, 1=set-map, 2=del-acl,
1809 * 3=del-map). It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Christopher Faulet81e20172019-12-12 16:40:30 +01001810 */
1811static enum act_parse_ret parse_http_set_map(const char **args, int *orig_arg, struct proxy *px,
1812 struct act_rule *rule, char **err)
1813{
1814 int cap, cur_arg;
1815
Christopher Faulet046cf442019-12-17 15:45:23 +01001816 if (args[*orig_arg-1][0] == 'a') // add-acl
1817 rule->action = 0;
1818 else if (args[*orig_arg-1][0] == 's') // set-map
1819 rule->action = 1;
1820 else if (args[*orig_arg-1][4] == 'a') // del-acl
1821 rule->action = 2;
1822 else if (args[*orig_arg-1][4] == 'm') // del-map
1823 rule->action = 3;
1824 else {
1825 memprintf(err, "internal error: unhandled action '%s'", args[0]);
1826 return ACT_RET_PRS_ERR;
1827 }
1828 rule->action_ptr = http_action_set_map;
Christopher Faulet2eb53962020-01-14 14:47:34 +01001829 rule->release_ptr = release_http_map;
Christopher Faulet81e20172019-12-12 16:40:30 +01001830
1831 cur_arg = *orig_arg;
Christopher Faulet046cf442019-12-17 15:45:23 +01001832 if (rule->action == 1 && (!*args[cur_arg] || !*args[cur_arg+1])) {
1833 /* 2 args for set-map */
Christopher Faulet81e20172019-12-12 16:40:30 +01001834 memprintf(err, "expects exactly 2 arguments");
1835 return ACT_RET_PRS_ERR;
1836 }
1837 else if (!*args[cur_arg]) {
Christopher Faulet046cf442019-12-17 15:45:23 +01001838 /* only one arg for other actions */
Christopher Faulet81e20172019-12-12 16:40:30 +01001839 memprintf(err, "expects exactly 1 arguments");
1840 return ACT_RET_PRS_ERR;
1841 }
1842
1843 /*
1844 * '+ 8' for 'set-map(' (same for del-map)
1845 * '- 9' for 'set-map(' + trailing ')' (same for del-map)
1846 */
1847 rule->arg.map.ref = my_strndup(args[cur_arg-1] + 8, strlen(args[cur_arg-1]) - 9);
1848
1849 if (rule->from == ACT_F_HTTP_REQ) {
1850 px->conf.args.ctx = ARGC_HRQ;
1851 cap = (px->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR;
1852 }
1853 else{
1854 px->conf.args.ctx = ARGC_HRS;
1855 cap = (px->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR;
1856 }
1857
1858 /* key pattern */
1859 LIST_INIT(&rule->arg.map.key);
Christopher Faulet1337b322020-01-14 14:50:55 +01001860 if (!parse_logformat_string(args[cur_arg], px, &rule->arg.map.key, LOG_OPT_HTTP, cap, err)) {
1861 free(rule->arg.map.ref);
Christopher Faulet81e20172019-12-12 16:40:30 +01001862 return ACT_RET_PRS_ERR;
Christopher Faulet1337b322020-01-14 14:50:55 +01001863 }
Christopher Faulet81e20172019-12-12 16:40:30 +01001864
Christopher Faulet046cf442019-12-17 15:45:23 +01001865 if (rule->action == 1) {
Christopher Faulet81e20172019-12-12 16:40:30 +01001866 /* value pattern for set-map only */
1867 cur_arg++;
1868 LIST_INIT(&rule->arg.map.value);
Christopher Faulet1337b322020-01-14 14:50:55 +01001869 if (!parse_logformat_string(args[cur_arg], px, &rule->arg.map.value, LOG_OPT_HTTP, cap, err)) {
1870 free(rule->arg.map.ref);
Christopher Faulet81e20172019-12-12 16:40:30 +01001871 return ACT_RET_PRS_ERR;
Christopher Faulet1337b322020-01-14 14:50:55 +01001872 }
Christopher Faulet81e20172019-12-12 16:40:30 +01001873 }
1874
1875 free(px->conf.lfs_file);
1876 px->conf.lfs_file = strdup(px->conf.args.file);
1877 px->conf.lfs_line = px->conf.args.line;
1878
1879 *orig_arg = cur_arg + 1;
1880 return ACT_RET_PRS_OK;
1881}
1882
Christopher Fauletac98d812019-12-18 09:20:16 +01001883/* This function executes a track-sc* actions. On success, it returns
1884 * ACT_RET_CONT. Otherwsize ACT_RET_ERR is returned.
1885 */
1886static enum act_return http_action_track_sc(struct act_rule *rule, struct proxy *px,
1887 struct session *sess, struct stream *s, int flags)
1888{
1889 struct stktable *t;
1890 struct stksess *ts;
1891 struct stktable_key *key;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001892 void *ptr1, *ptr2, *ptr3, *ptr4, *ptr5, *ptr6;
Christopher Fauletac98d812019-12-18 09:20:16 +01001893 int opt;
1894
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001895 ptr1 = ptr2 = ptr3 = ptr4 = ptr5 = ptr6 = NULL;
Christopher Fauletac98d812019-12-18 09:20:16 +01001896 opt = ((rule->from == ACT_F_HTTP_REQ) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES) | SMP_OPT_FINAL;
1897
1898 t = rule->arg.trk_ctr.table.t;
Emeric Brun362d25e2021-03-10 16:58:03 +01001899
1900 if (stkctr_entry(&s->stkctr[rule->action]))
1901 goto end;
1902
Christopher Fauletac98d812019-12-18 09:20:16 +01001903 key = stktable_fetch_key(t, s->be, sess, s, opt, rule->arg.trk_ctr.expr, NULL);
1904
1905 if (!key)
1906 goto end;
1907 ts = stktable_get_entry(t, key);
1908 if (!ts)
1909 goto end;
1910
1911 stream_track_stkctr(&s->stkctr[rule->action], t, ts);
1912
1913 /* let's count a new HTTP request as it's the first time we do it */
1914 ptr1 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
1915 ptr2 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
1916
1917 /* When the client triggers a 4xx from the server, it's most often due
1918 * to a missing object or permission. These events should be tracked
1919 * because if they happen often, it may indicate a brute force or a
1920 * vulnerability scan. Normally this is done when receiving the response
1921 * but here we're tracking after this ought to have been done so we have
1922 * to do it on purpose.
1923 */
1924 if (rule->from == ACT_F_HTTP_RES && (unsigned)(s->txn->status - 400) < 100) {
1925 ptr3 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_CNT);
1926 ptr4 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_RATE);
1927 }
1928
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001929 if (rule->from == ACT_F_HTTP_RES && (unsigned)(s->txn->status - 500) < 100 &&
1930 s->txn->status != 501 && s->txn->status != 505) {
1931 ptr5 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_FAIL_CNT);
1932 ptr6 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_FAIL_RATE);
1933 }
1934
1935 if (ptr1 || ptr2 || ptr3 || ptr4 || ptr5 || ptr6) {
Christopher Fauletac98d812019-12-18 09:20:16 +01001936 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
1937
1938 if (ptr1)
1939 stktable_data_cast(ptr1, http_req_cnt)++;
1940 if (ptr2)
1941 update_freq_ctr_period(&stktable_data_cast(ptr2, http_req_rate),
1942 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
1943 if (ptr3)
1944 stktable_data_cast(ptr3, http_err_cnt)++;
1945 if (ptr4)
1946 update_freq_ctr_period(&stktable_data_cast(ptr4, http_err_rate),
1947 t->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1);
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001948 if (ptr5)
1949 stktable_data_cast(ptr5, http_fail_cnt)++;
1950 if (ptr6)
1951 update_freq_ctr_period(&stktable_data_cast(ptr6, http_fail_rate),
1952 t->data_arg[STKTABLE_DT_HTTP_FAIL_RATE].u, 1);
Christopher Fauletac98d812019-12-18 09:20:16 +01001953
1954 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
1955
1956 /* If data was modified, we need to touch to re-schedule sync */
1957 stktable_touch_local(t, ts, 0);
1958 }
1959
1960 stkctr_set_flags(&s->stkctr[rule->action], STKCTR_TRACK_CONTENT);
1961 if (sess->fe != s->be)
1962 stkctr_set_flags(&s->stkctr[rule->action], STKCTR_TRACK_BACKEND);
1963
1964 end:
1965 return ACT_RET_CONT;
1966}
Christopher Faulet81e20172019-12-12 16:40:30 +01001967
Christopher Faulet2eb53962020-01-14 14:47:34 +01001968static void release_http_track_sc(struct act_rule *rule)
1969{
1970 release_sample_expr(rule->arg.trk_ctr.expr);
1971}
1972
Christopher Faulet81e20172019-12-12 16:40:30 +01001973/* Parse a "track-sc*" actions. It returns ACT_RET_PRS_OK on success,
1974 * ACT_RET_PRS_ERR on error.
1975 */
1976static enum act_parse_ret parse_http_track_sc(const char **args, int *orig_arg, struct proxy *px,
1977 struct act_rule *rule, char **err)
1978{
1979 struct sample_expr *expr;
1980 unsigned int where;
1981 unsigned int tsc_num;
1982 const char *tsc_num_str;
1983 int cur_arg;
1984
1985 tsc_num_str = &args[*orig_arg-1][8];
1986 if (cfg_parse_track_sc_num(&tsc_num, tsc_num_str, tsc_num_str + strlen(tsc_num_str), err) == -1)
1987 return ACT_RET_PRS_ERR;
1988
1989 cur_arg = *orig_arg;
1990 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line,
Willy Tarreaue3b57bf2020-02-14 16:50:14 +01001991 err, &px->conf.args, NULL);
Christopher Faulet81e20172019-12-12 16:40:30 +01001992 if (!expr)
1993 return ACT_RET_PRS_ERR;
1994
1995 where = 0;
1996 if (px->cap & PR_CAP_FE)
1997 where |= (rule->from == ACT_F_HTTP_REQ ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_FE_HRS_HDR);
1998 if (px->cap & PR_CAP_BE)
1999 where |= (rule->from == ACT_F_HTTP_REQ ? SMP_VAL_BE_HRQ_HDR : SMP_VAL_BE_HRS_HDR);
2000
2001 if (!(expr->fetch->val & where)) {
2002 memprintf(err, "fetch method '%s' extracts information from '%s', none of which is available here",
2003 args[cur_arg-1], sample_src_names(expr->fetch->use));
Christopher Faulet1337b322020-01-14 14:50:55 +01002004 release_sample_expr(expr);
Christopher Faulet81e20172019-12-12 16:40:30 +01002005 return ACT_RET_PRS_ERR;
2006 }
2007
2008 if (strcmp(args[cur_arg], "table") == 0) {
2009 cur_arg++;
2010 if (!*args[cur_arg]) {
2011 memprintf(err, "missing table name");
Christopher Faulet1337b322020-01-14 14:50:55 +01002012 release_sample_expr(expr);
Christopher Faulet81e20172019-12-12 16:40:30 +01002013 return ACT_RET_PRS_ERR;
2014 }
2015
2016 /* we copy the table name for now, it will be resolved later */
2017 rule->arg.trk_ctr.table.n = strdup(args[cur_arg]);
2018 cur_arg++;
2019 }
2020
Christopher Fauletac98d812019-12-18 09:20:16 +01002021 rule->action = tsc_num;
Christopher Faulet81e20172019-12-12 16:40:30 +01002022 rule->arg.trk_ctr.expr = expr;
Christopher Fauletac98d812019-12-18 09:20:16 +01002023 rule->action_ptr = http_action_track_sc;
Christopher Faulet2eb53962020-01-14 14:47:34 +01002024 rule->release_ptr = release_http_track_sc;
Christopher Faulet81e20172019-12-12 16:40:30 +01002025 rule->check_ptr = check_trk_action;
2026
2027 *orig_arg = cur_arg;
2028 return ACT_RET_PRS_OK;
2029}
2030
Amaury Denoyelle8d228232020-12-10 13:43:54 +01002031static enum act_return action_timeout_set_stream_timeout(struct act_rule *rule,
2032 struct proxy *px,
2033 struct session *sess,
2034 struct stream *s,
2035 int flags)
2036{
2037 struct sample *key;
2038
2039 if (rule->arg.timeout.expr) {
2040 key = sample_fetch_as_type(px, sess, s, SMP_OPT_FINAL, rule->arg.timeout.expr, SMP_T_SINT);
2041 if (!key)
2042 return ACT_RET_CONT;
2043
2044 stream_set_timeout(s, rule->arg.timeout.type, MS_TO_TICKS(key->data.u.sint));
2045 }
2046 else {
2047 stream_set_timeout(s, rule->arg.timeout.type, MS_TO_TICKS(rule->arg.timeout.value));
2048 }
2049
2050 return ACT_RET_CONT;
2051}
2052
2053/* Parse a "set-timeout" action. Returns ACT_RET_PRS_ERR if parsing error.
2054 */
2055static enum act_parse_ret parse_http_set_timeout(const char **args,
2056 int *orig_arg,
2057 struct proxy *px,
2058 struct act_rule *rule, char **err)
2059{
2060 int cur_arg;
2061
2062 rule->action = ACT_CUSTOM;
2063 rule->action_ptr = action_timeout_set_stream_timeout;
2064 rule->release_ptr = release_timeout_action;
2065
2066 cur_arg = *orig_arg;
2067 if (!*args[cur_arg] || !*args[cur_arg + 1]) {
2068 memprintf(err, "expects exactly 2 arguments");
2069 return ACT_RET_PRS_ERR;
2070 }
2071
2072 if (!(px->cap & PR_CAP_BE)) {
2073 memprintf(err, "proxy '%s' has no backend capability", px->id);
2074 return ACT_RET_PRS_ERR;
2075 }
2076
2077 if (cfg_parse_rule_set_timeout(args, cur_arg,
2078 &rule->arg.timeout.value,
2079 &rule->arg.timeout.type,
2080 &rule->arg.timeout.expr,
2081 err,
2082 px->conf.args.file,
2083 px->conf.args.line, &px->conf.args) == -1) {
2084 return ACT_RET_PRS_ERR;
2085 }
2086
2087 *orig_arg = cur_arg + 2;
2088
2089 return ACT_RET_PRS_OK;
2090}
2091
Christopher Faulet46f95542019-12-20 10:07:22 +01002092/* This function executes a strict-mode actions. On success, it always returns
2093 * ACT_RET_CONT
2094 */
2095static enum act_return http_action_strict_mode(struct act_rule *rule, struct proxy *px,
2096 struct session *sess, struct stream *s, int flags)
2097{
2098 struct http_msg *msg = ((rule->from == ACT_F_HTTP_REQ) ? &s->txn->req : &s->txn->rsp);
2099
2100 if (rule->action == 0) // strict-mode on
2101 msg->flags &= ~HTTP_MSGF_SOFT_RW;
2102 else // strict-mode off
2103 msg->flags |= HTTP_MSGF_SOFT_RW;
2104 return ACT_RET_CONT;
2105}
2106
2107/* Parse a "strict-mode" action. It returns ACT_RET_PRS_OK on success,
2108 * ACT_RET_PRS_ERR on error.
2109 */
2110static enum act_parse_ret parse_http_strict_mode(const char **args, int *orig_arg, struct proxy *px,
2111 struct act_rule *rule, char **err)
2112{
2113 int cur_arg;
2114
Christopher Faulet46f95542019-12-20 10:07:22 +01002115 cur_arg = *orig_arg;
2116 if (!*args[cur_arg]) {
2117 memprintf(err, "expects exactly 1 arguments");
2118 return ACT_RET_PRS_ERR;
2119 }
2120
2121 if (strcasecmp(args[cur_arg], "on") == 0)
2122 rule->action = 0; // strict-mode on
2123 else if (strcasecmp(args[cur_arg], "off") == 0)
2124 rule->action = 1; // strict-mode off
2125 else {
2126 memprintf(err, "Unexpected value '%s'. Only 'on' and 'off' are supported", args[cur_arg]);
2127 return ACT_RET_PRS_ERR;
2128 }
2129 rule->action_ptr = http_action_strict_mode;
2130
2131 *orig_arg = cur_arg + 1;
2132 return ACT_RET_PRS_OK;
2133}
2134
Christopher Faulet24231ab2020-01-24 17:44:23 +01002135/* This function executes a return action. It builds an HTX message from an
2136 * errorfile, an raw file or a log-format string, depending on <.action>
2137 * value. On success, it returns ACT_RET_ABRT. If an error occurs ACT_RET_ERR is
2138 * returned.
2139 */
2140static enum act_return http_action_return(struct act_rule *rule, struct proxy *px,
2141 struct session *sess, struct stream *s, int flags)
2142{
2143 struct channel *req = &s->req;
Christopher Faulet24231ab2020-01-24 17:44:23 +01002144
Christopher Faulet2d36df22021-02-19 11:41:01 +01002145 s->txn->status = rule->arg.http_reply->status;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02002146 if (http_reply_message(s, rule->arg.http_reply) == -1)
2147 return ACT_RET_ERR;
Christopher Faulet24231ab2020-01-24 17:44:23 +01002148
Christopher Faulet24231ab2020-01-24 17:44:23 +01002149 if (rule->from == ACT_F_HTTP_REQ) {
2150 /* let's log the request time */
2151 s->logs.tv_request = now;
2152 req->analysers &= AN_REQ_FLT_END;
2153
2154 if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Willy Tarreau4781b152021-04-06 13:53:36 +02002155 _HA_ATOMIC_INC(&s->sess->fe->fe_counters.intercepted_req);
Christopher Faulet24231ab2020-01-24 17:44:23 +01002156 }
2157
2158 if (!(s->flags & SF_ERR_MASK))
2159 s->flags |= SF_ERR_LOCAL;
2160 if (!(s->flags & SF_FINST_MASK))
2161 s->flags |= ((rule->from == ACT_F_HTTP_REQ) ? SF_FINST_R : SF_FINST_H);
2162
Christopher Faulet0e2ad612020-05-13 16:38:37 +02002163 return ACT_RET_ABRT;
Christopher Faulet24231ab2020-01-24 17:44:23 +01002164}
2165
Christopher Faulet24231ab2020-01-24 17:44:23 +01002166/* Parse a "return" action. It returns ACT_RET_PRS_OK on success,
Christopher Faulet47e791e2020-05-13 14:36:55 +02002167 * ACT_RET_PRS_ERR on error. It relies on http_parse_http_reply() to set
2168 * <.arg.http_reply>.
Christopher Faulet24231ab2020-01-24 17:44:23 +01002169 */
2170static enum act_parse_ret parse_http_return(const char **args, int *orig_arg, struct proxy *px,
2171 struct act_rule *rule, char **err)
2172{
Christopher Faulet47e791e2020-05-13 14:36:55 +02002173 /* Prepare parsing of log-format strings */
2174 px->conf.args.ctx = ((rule->from == ACT_F_HTTP_REQ) ? ARGC_HRQ : ARGC_HRS);
2175 rule->arg.http_reply = http_parse_http_reply(args, orig_arg, px, 200, err);
2176 if (!rule->arg.http_reply)
2177 return ACT_RET_PRS_ERR;
Christopher Faulet24231ab2020-01-24 17:44:23 +01002178
Christopher Fauletba946bf2020-05-13 08:50:07 +02002179 rule->flags |= ACT_FLAG_FINAL;
Christopher Faulet5ff0c642020-05-12 18:33:37 +02002180 rule->action = ACT_CUSTOM;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002181 rule->check_ptr = check_act_http_reply;
Christopher Faulet24231ab2020-01-24 17:44:23 +01002182 rule->action_ptr = http_action_return;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002183 rule->release_ptr = release_act_http_reply;
Christopher Faulet24231ab2020-01-24 17:44:23 +01002184 return ACT_RET_PRS_OK;
Christopher Faulet24231ab2020-01-24 17:44:23 +01002185}
2186
Christopher Faulet021a8e42021-03-29 10:46:38 +02002187
2188
2189/* This function executes a wait-for-body action. It waits for the message
2190 * payload for a max configured time (.arg.p[0]) and eventually for only first
2191 * <arg.p[1]> bytes (0 means no limit). It relies on http_wait_for_msg_body()
2192 * function. it returns ACT_RET_CONT when conditions are met to stop to wait.
2193 * Otherwise ACT_RET_YIELD is returned to wait for more data. ACT_RET_INV is
2194 * returned if a parsing error is raised by lower level and ACT_RET_ERR if an
2195 * internal error occured. Finally ACT_RET_ABRT is returned when a timeout
2196 * occured.
2197 */
2198static enum act_return http_action_wait_for_body(struct act_rule *rule, struct proxy *px,
2199 struct session *sess, struct stream *s, int flags)
2200{
2201 struct channel *chn = ((rule->from == ACT_F_HTTP_REQ) ? &s->req : &s->res);
2202 unsigned int time = (uintptr_t)rule->arg.act.p[0];
2203 unsigned int bytes = (uintptr_t)rule->arg.act.p[1];
2204
2205 switch (http_wait_for_msg_body(s, chn, time, bytes)) {
2206 case HTTP_RULE_RES_CONT:
2207 return ACT_RET_CONT;
2208 case HTTP_RULE_RES_YIELD:
2209 return ACT_RET_YIELD;
2210 case HTTP_RULE_RES_BADREQ:
2211 return ACT_RET_INV;
2212 case HTTP_RULE_RES_ERROR:
2213 return ACT_RET_ERR;
2214 case HTTP_RULE_RES_ABRT:
2215 return ACT_RET_ABRT;
2216 default:
2217 return ACT_RET_ERR;
2218 }
2219}
2220
2221/* Parse a "wait-for-body" action. It returns ACT_RET_PRS_OK on success,
2222 * ACT_RET_PRS_ERR on error.
2223 */
2224static enum act_parse_ret parse_http_wait_for_body(const char **args, int *orig_arg, struct proxy *px,
2225 struct act_rule *rule, char **err)
2226{
2227 int cur_arg;
2228 unsigned int time, bytes;
2229 const char *res;
2230
2231 cur_arg = *orig_arg;
2232 if (!*args[cur_arg]) {
2233 memprintf(err, "expects time <time> [ at-least <bytes> ]");
2234 return ACT_RET_PRS_ERR;
2235 }
2236
2237 time = UINT_MAX; /* To be sure it is set */
2238 bytes = 0; /* Default value, wait all the body */
2239 while (*(args[cur_arg])) {
2240 if (strcmp(args[cur_arg], "time") == 0) {
2241 if (!*args[cur_arg + 1]) {
2242 memprintf(err, "missing argument for '%s'", args[cur_arg]);
2243 return ACT_RET_PRS_ERR;
2244 }
2245 res = parse_time_err(args[cur_arg+1], &time, TIME_UNIT_MS);
2246 if (res == PARSE_TIME_OVER) {
2247 memprintf(err, "time overflow (maximum value is 2147483647 ms or ~24.8 days)");
2248 return ACT_RET_PRS_ERR;
2249 }
2250 if (res == PARSE_TIME_UNDER) {
2251 memprintf(err, "time underflow (minimum non-null value is 1 ms)");
2252 return ACT_RET_PRS_ERR;
2253 }
2254 if (res) {
2255 memprintf(err, "unexpected character '%c'", *res);
2256 return ACT_RET_PRS_ERR;
2257 }
2258 cur_arg++;
2259 }
2260 else if (strcmp(args[cur_arg], "at-least") == 0) {
2261 if (!*args[cur_arg + 1]) {
2262 memprintf(err, "missing argument for '%s'", args[cur_arg]);
2263 return ACT_RET_PRS_ERR;
2264 }
2265 res = parse_size_err(args[cur_arg+1], &bytes);
2266 if (res) {
2267 memprintf(err, "unexpected character '%c'", *res);
2268 return ACT_RET_PRS_ERR;
2269 }
2270 cur_arg++;
2271 }
2272 else
2273 break;
2274 cur_arg++;
2275 }
2276
2277 if (time == UINT_MAX) {
2278 memprintf(err, "expects time <time> [ at-least <bytes> ]");
2279 return ACT_RET_PRS_ERR;
2280 }
2281
2282 rule->arg.act.p[0] = (void *)(uintptr_t)time;
2283 rule->arg.act.p[1] = (void *)(uintptr_t)bytes;
2284
2285 *orig_arg = cur_arg;
2286
2287 rule->action = ACT_CUSTOM;
2288 rule->action_ptr = http_action_wait_for_body;
2289 return ACT_RET_PRS_OK;
2290}
2291
Willy Tarreau79e57332018-10-02 16:01:16 +02002292/************************************************************************/
2293/* All supported http-request action keywords must be declared here. */
2294/************************************************************************/
2295
2296static struct action_kw_list http_req_actions = {
2297 .kw = {
Christopher Faulet81e20172019-12-12 16:40:30 +01002298 { "add-acl", parse_http_set_map, 1 },
2299 { "add-header", parse_http_set_header, 0 },
2300 { "allow", parse_http_allow, 0 },
2301 { "auth", parse_http_auth, 0 },
2302 { "capture", parse_http_req_capture, 0 },
2303 { "del-acl", parse_http_set_map, 1 },
2304 { "del-header", parse_http_del_header, 0 },
2305 { "del-map", parse_http_set_map, 1 },
Christopher Faulete0fca292020-01-13 21:49:03 +01002306 { "deny", parse_http_deny, 0 },
Christopher Faulet81e20172019-12-12 16:40:30 +01002307 { "disable-l7-retry", parse_http_req_disable_l7_retry, 0 },
2308 { "early-hint", parse_http_set_header, 0 },
Tim Duesterhusd2bedcc2021-04-15 21:45:57 +02002309 { "normalize-uri", parse_http_normalize_uri, 0 },
Christopher Faulet81e20172019-12-12 16:40:30 +01002310 { "redirect", parse_http_redirect, 0 },
2311 { "reject", parse_http_action_reject, 0 },
2312 { "replace-header", parse_http_replace_header, 0 },
2313 { "replace-path", parse_replace_uri, 0 },
Christopher Faulet312294f2020-09-02 17:17:44 +02002314 { "replace-pathq", parse_replace_uri, 0 },
Christopher Faulet81e20172019-12-12 16:40:30 +01002315 { "replace-uri", parse_replace_uri, 0 },
2316 { "replace-value", parse_http_replace_header, 0 },
Christopher Faulet24231ab2020-01-24 17:44:23 +01002317 { "return", parse_http_return, 0 },
Christopher Faulet81e20172019-12-12 16:40:30 +01002318 { "set-header", parse_http_set_header, 0 },
2319 { "set-log-level", parse_http_set_log_level, 0 },
2320 { "set-map", parse_http_set_map, 1 },
2321 { "set-method", parse_set_req_line, 0 },
2322 { "set-mark", parse_http_set_mark, 0 },
2323 { "set-nice", parse_http_set_nice, 0 },
2324 { "set-path", parse_set_req_line, 0 },
Christopher Faulet312294f2020-09-02 17:17:44 +02002325 { "set-pathq", parse_set_req_line, 0 },
Christopher Faulet81e20172019-12-12 16:40:30 +01002326 { "set-query", parse_set_req_line, 0 },
2327 { "set-tos", parse_http_set_tos, 0 },
2328 { "set-uri", parse_set_req_line, 0 },
Christopher Faulet46f95542019-12-20 10:07:22 +01002329 { "strict-mode", parse_http_strict_mode, 0 },
Christopher Faulete0fca292020-01-13 21:49:03 +01002330 { "tarpit", parse_http_deny, 0 },
Christopher Faulet81e20172019-12-12 16:40:30 +01002331 { "track-sc", parse_http_track_sc, 1 },
Amaury Denoyelle8d228232020-12-10 13:43:54 +01002332 { "set-timeout", parse_http_set_timeout, 0 },
Christopher Faulet021a8e42021-03-29 10:46:38 +02002333 { "wait-for-body", parse_http_wait_for_body, 0 },
Willy Tarreau79e57332018-10-02 16:01:16 +02002334 { NULL, NULL }
2335 }
2336};
2337
Willy Tarreau0108d902018-11-25 19:14:37 +01002338INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_actions);
2339
Willy Tarreau79e57332018-10-02 16:01:16 +02002340static struct action_kw_list http_res_actions = {
2341 .kw = {
Christopher Faulet81e20172019-12-12 16:40:30 +01002342 { "add-acl", parse_http_set_map, 1 },
2343 { "add-header", parse_http_set_header, 0 },
2344 { "allow", parse_http_allow, 0 },
2345 { "capture", parse_http_res_capture, 0 },
2346 { "del-acl", parse_http_set_map, 1 },
2347 { "del-header", parse_http_del_header, 0 },
2348 { "del-map", parse_http_set_map, 1 },
Christopher Faulete0fca292020-01-13 21:49:03 +01002349 { "deny", parse_http_deny, 0 },
Christopher Faulet81e20172019-12-12 16:40:30 +01002350 { "redirect", parse_http_redirect, 0 },
2351 { "replace-header", parse_http_replace_header, 0 },
2352 { "replace-value", parse_http_replace_header, 0 },
Christopher Faulet24231ab2020-01-24 17:44:23 +01002353 { "return", parse_http_return, 0 },
Christopher Faulet81e20172019-12-12 16:40:30 +01002354 { "set-header", parse_http_set_header, 0 },
2355 { "set-log-level", parse_http_set_log_level, 0 },
2356 { "set-map", parse_http_set_map, 1 },
2357 { "set-mark", parse_http_set_mark, 0 },
2358 { "set-nice", parse_http_set_nice, 0 },
2359 { "set-status", parse_http_set_status, 0 },
2360 { "set-tos", parse_http_set_tos, 0 },
Christopher Faulet46f95542019-12-20 10:07:22 +01002361 { "strict-mode", parse_http_strict_mode, 0 },
Christopher Faulet81e20172019-12-12 16:40:30 +01002362 { "track-sc", parse_http_track_sc, 1 },
Christopher Faulet021a8e42021-03-29 10:46:38 +02002363 { "wait-for-body", parse_http_wait_for_body, 0 },
Willy Tarreau79e57332018-10-02 16:01:16 +02002364 { NULL, NULL }
2365 }
2366};
2367
Willy Tarreau0108d902018-11-25 19:14:37 +01002368INITCALL1(STG_REGISTER, http_res_keywords_register, &http_res_actions);
Willy Tarreau79e57332018-10-02 16:01:16 +02002369
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002370static struct action_kw_list http_after_res_actions = {
2371 .kw = {
2372 { "add-header", parse_http_set_header, 0 },
2373 { "allow", parse_http_allow, 0 },
2374 { "del-header", parse_http_del_header, 0 },
2375 { "replace-header", parse_http_replace_header, 0 },
2376 { "replace-value", parse_http_replace_header, 0 },
2377 { "set-header", parse_http_set_header, 0 },
2378 { "set-status", parse_http_set_status, 0 },
2379 { "strict-mode", parse_http_strict_mode, 0 },
2380 { NULL, NULL }
2381 }
2382};
2383
2384INITCALL1(STG_REGISTER, http_after_res_keywords_register, &http_after_res_actions);
2385
Willy Tarreau79e57332018-10-02 16:01:16 +02002386/*
2387 * Local variables:
2388 * c-indent-level: 8
2389 * c-basic-offset: 8
2390 * End:
2391 */