blob: df2bbe41b90c3989ae8c4216d98ef9a4598520ef [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) {
Willy Tarreau2b718102021-04-21 07:32:39 +020054 LIST_DELETE(&lf->list);
Christopher Faulet2eb53962020-01-14 14:47:34 +010055 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 Duesterhus5be6ab22021-04-17 11:21:10 +0200218 case ACT_NORMALIZE_URI_PATH_MERGE_SLASHES: {
Tim Duesterhusd371e992021-04-15 21:45:58 +0200219 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 }
Maximilian Maderff3bb8b2021-04-21 00:22:50 +0200235 case ACT_NORMALIZE_URI_PATH_STRIP_DOT: {
236 const struct ist path = http_get_path(uri);
237 struct ist newpath = ist2(replace->area, replace->size);
238
239 if (!isttest(path))
240 goto leave;
241
242 err = uri_normalizer_path_dot(iststop(path, '?'), &newpath);
243
244 if (err != URI_NORMALIZER_ERR_NONE)
245 break;
246
247 if (!http_replace_req_path(htx, newpath, 0))
248 goto fail_rewrite;
249
250 break;
251 }
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200252 case ACT_NORMALIZE_URI_PATH_STRIP_DOTDOT:
253 case ACT_NORMALIZE_URI_PATH_STRIP_DOTDOT_FULL: {
Tim Duesterhus9982fc22021-04-15 21:45:59 +0200254 const struct ist path = http_get_path(uri);
255 struct ist newpath = ist2(replace->area, replace->size);
256
257 if (!isttest(path))
258 goto leave;
259
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200260 err = uri_normalizer_path_dotdot(iststop(path, '?'), rule->action == ACT_NORMALIZE_URI_PATH_STRIP_DOTDOT_FULL, &newpath);
Tim Duesterhus9982fc22021-04-15 21:45:59 +0200261
262 if (err != URI_NORMALIZER_ERR_NONE)
263 break;
264
265 if (!http_replace_req_path(htx, newpath, 0))
266 goto fail_rewrite;
267
268 break;
269 }
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200270 case ACT_NORMALIZE_URI_QUERY_SORT_BY_NAME: {
Tim Duesterhusd7b89be2021-04-15 21:46:01 +0200271 const struct ist path = http_get_path(uri);
272 struct ist newquery = ist2(replace->area, replace->size);
273
274 if (!isttest(path))
275 goto leave;
276
277 err = uri_normalizer_query_sort(istfind(path, '?'), '&', &newquery);
278
279 if (err != URI_NORMALIZER_ERR_NONE)
280 break;
281
282 if (!http_replace_req_query(htx, newquery))
283 goto fail_rewrite;
284
285 break;
286 }
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200287 case ACT_NORMALIZE_URI_PERCENT_TO_UPPERCASE:
288 case ACT_NORMALIZE_URI_PERCENT_TO_UPPERCASE_STRICT: {
Tim Duesterhusa4071932021-04-15 21:46:02 +0200289 const struct ist path = http_get_path(uri);
290 struct ist newpath = ist2(replace->area, replace->size);
291
292 if (!isttest(path))
293 goto leave;
294
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200295 err = uri_normalizer_percent_upper(path, rule->action == ACT_NORMALIZE_URI_PERCENT_TO_UPPERCASE_STRICT, &newpath);
Tim Duesterhusa4071932021-04-15 21:46:02 +0200296
297 if (err != URI_NORMALIZER_ERR_NONE)
298 break;
299
300 if (!http_replace_req_path(htx, newpath, 1))
301 goto fail_rewrite;
302
303 break;
304 }
Tim Duesterhusd2bedcc2021-04-15 21:45:57 +0200305 }
306
307 switch (err) {
308 case URI_NORMALIZER_ERR_NONE:
309 break;
310 case URI_NORMALIZER_ERR_INTERNAL_ERROR:
311 ret = ACT_RET_ERR;
312 break;
313 case URI_NORMALIZER_ERR_INVALID_INPUT:
314 ret = ACT_RET_INV;
315 break;
316 case URI_NORMALIZER_ERR_ALLOC:
317 goto fail_alloc;
318 }
319
320 leave:
321 free_trash_chunk(replace);
322 return ret;
323
324 fail_alloc:
325 if (!(s->flags & SF_ERR_MASK))
326 s->flags |= SF_ERR_RESOURCE;
327 ret = ACT_RET_ERR;
328 goto leave;
329
330 fail_rewrite:
331 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_rewrites, 1);
332 if (s->flags & SF_BE_ASSIGNED)
333 _HA_ATOMIC_ADD(&s->be->be_counters.failed_rewrites, 1);
334 if (sess->listener && sess->listener->counters)
335 _HA_ATOMIC_ADD(&sess->listener->counters->failed_rewrites, 1);
336 if (objt_server(s->target))
337 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_rewrites, 1);
338
339 if (!(s->txn->req.flags & HTTP_MSGF_SOFT_RW)) {
340 ret = ACT_RET_ERR;
341 if (!(s->flags & SF_ERR_MASK))
342 s->flags |= SF_ERR_PRXCOND;
343 }
344 goto leave;
345}
346
347/* Parses the http-request normalize-uri action. It expects a single <normalizer>
348 * argument, corresponding too a value in `enum act_normalize_uri`.
349 *
350 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
351 */
352static enum act_parse_ret parse_http_normalize_uri(const char **args, int *orig_arg, struct proxy *px,
353 struct act_rule *rule, char **err)
354{
355 int cur_arg = *orig_arg;
356
357 rule->action_ptr = http_action_normalize_uri;
358 rule->release_ptr = NULL;
359
360 if (!*args[cur_arg]) {
361 memprintf(err, "missing argument <normalizer>");
362 return ACT_RET_PRS_ERR;
363 }
364
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200365 if (strcmp(args[cur_arg], "path-merge-slashes") == 0) {
Tim Duesterhusd371e992021-04-15 21:45:58 +0200366 cur_arg++;
Tim Duesterhusd2bedcc2021-04-15 21:45:57 +0200367
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200368 rule->action = ACT_NORMALIZE_URI_PATH_MERGE_SLASHES;
Tim Duesterhusd2bedcc2021-04-15 21:45:57 +0200369 }
Maximilian Maderff3bb8b2021-04-21 00:22:50 +0200370 else if (strcmp(args[cur_arg], "path-strip-dot") == 0) {
371 cur_arg++;
372
373 rule->action = ACT_NORMALIZE_URI_PATH_STRIP_DOT;
374 }
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200375 else if (strcmp(args[cur_arg], "path-strip-dotdot") == 0) {
Tim Duesterhus9982fc22021-04-15 21:45:59 +0200376 cur_arg++;
377
Tim Duesterhus560e1a62021-04-15 21:46:00 +0200378 if (strcmp(args[cur_arg], "full") == 0) {
379 cur_arg++;
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200380 rule->action = ACT_NORMALIZE_URI_PATH_STRIP_DOTDOT_FULL;
Tim Duesterhus560e1a62021-04-15 21:46:00 +0200381 }
382 else if (!*args[cur_arg]) {
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200383 rule->action = ACT_NORMALIZE_URI_PATH_STRIP_DOTDOT;
Tim Duesterhus560e1a62021-04-15 21:46:00 +0200384 }
385 else if (strcmp(args[cur_arg], "if") != 0 && strcmp(args[cur_arg], "unless") != 0) {
386 memprintf(err, "unknown argument '%s' for 'dotdot' normalizer", args[cur_arg]);
387 return ACT_RET_PRS_ERR;
388 }
Tim Duesterhus9982fc22021-04-15 21:45:59 +0200389 }
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200390 else if (strcmp(args[cur_arg], "query-sort-by-name") == 0) {
Tim Duesterhusd7b89be2021-04-15 21:46:01 +0200391 cur_arg++;
392
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200393 rule->action = ACT_NORMALIZE_URI_QUERY_SORT_BY_NAME;
Tim Duesterhusd7b89be2021-04-15 21:46:01 +0200394 }
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200395 else if (strcmp(args[cur_arg], "percent-to-uppercase") == 0) {
Tim Duesterhusa4071932021-04-15 21:46:02 +0200396 cur_arg++;
397
398 if (strcmp(args[cur_arg], "strict") == 0) {
399 cur_arg++;
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200400 rule->action = ACT_NORMALIZE_URI_PERCENT_TO_UPPERCASE_STRICT;
Tim Duesterhusa4071932021-04-15 21:46:02 +0200401 }
402 else if (!*args[cur_arg]) {
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200403 rule->action = ACT_NORMALIZE_URI_PERCENT_TO_UPPERCASE;
Tim Duesterhusa4071932021-04-15 21:46:02 +0200404 }
405 else if (strcmp(args[cur_arg], "if") != 0 && strcmp(args[cur_arg], "unless") != 0) {
406 memprintf(err, "unknown argument '%s' for 'percent-upper' normalizer", args[cur_arg]);
407 return ACT_RET_PRS_ERR;
408 }
409 }
Tim Duesterhusd2bedcc2021-04-15 21:45:57 +0200410 else {
411 memprintf(err, "unknown normalizer '%s'", args[cur_arg]);
412 return ACT_RET_PRS_ERR;
413 }
414
415 *orig_arg = cur_arg;
416 return ACT_RET_PRS_OK;
417}
418
Willy Tarreau33810222019-06-12 17:44:02 +0200419/* This function executes a replace-uri action. It finds its arguments in
Christopher Faulet96bff762019-12-17 13:46:18 +0100420 * <rule>.arg.http. It builds a string in the trash from the format string
Willy Tarreau33810222019-06-12 17:44:02 +0200421 * previously filled by function parse_replace_uri() and will execute the regex
Christopher Faulet96bff762019-12-17 13:46:18 +0100422 * in <http.re> to replace the URI. It uses the format string present in
Christopher Faulet2c22a692019-12-18 15:39:56 +0100423 * <http.fmt>. The component to act on (path/uri) is taken from <.action> which
Christopher Faulet96bff762019-12-17 13:46:18 +0100424 * contains 1 for the path or 3 for the URI (values used by
425 * http_req_replace_stline()). On success, it returns ACT_RET_CONT. If an error
426 * occurs while soft rewrites are enabled, the action is canceled, but the rule
427 * processing continue. Otherwsize ACT_RET_ERR is returned.
Willy Tarreau33810222019-06-12 17:44:02 +0200428 */
429static enum act_return http_action_replace_uri(struct act_rule *rule, struct proxy *px,
430 struct session *sess, struct stream *s, int flags)
431{
Christopher Faulet13403762019-12-13 09:01:57 +0100432 enum act_return ret = ACT_RET_CONT;
Willy Tarreau33810222019-06-12 17:44:02 +0200433 struct buffer *replace, *output;
434 struct ist uri;
435 int len;
436
437 replace = alloc_trash_chunk();
438 output = alloc_trash_chunk();
439 if (!replace || !output)
Christopher Faulete00d06c2019-12-16 17:18:42 +0100440 goto fail_alloc;
Christopher Faulet12c28b62019-07-15 16:30:24 +0200441 uri = htx_sl_req_uri(http_get_stline(htxbuf(&s->req.buf)));
Willy Tarreau262c3f12019-12-17 06:52:51 +0100442
Christopher Faulet1fa0cc12020-09-02 11:10:38 +0200443 if (rule->action == 1) // replace-path
444 uri = iststop(http_get_path(uri), '?');
Christopher Faulet312294f2020-09-02 17:17:44 +0200445 else if (rule->action == 4) // replace-pathq
446 uri = http_get_path(uri);
Willy Tarreau262c3f12019-12-17 06:52:51 +0100447
Christopher Faulet96bff762019-12-17 13:46:18 +0100448 if (!regex_exec_match2(rule->arg.http.re, uri.ptr, uri.len, MAX_MATCH, pmatch, 0))
Willy Tarreau33810222019-06-12 17:44:02 +0200449 goto leave;
450
Christopher Faulet96bff762019-12-17 13:46:18 +0100451 replace->data = build_logline(s, replace->area, replace->size, &rule->arg.http.fmt);
Willy Tarreau33810222019-06-12 17:44:02 +0200452
453 /* note: uri.ptr doesn't need to be zero-terminated because it will
454 * only be used to pick pmatch references.
455 */
456 len = exp_replace(output->area, output->size, uri.ptr, replace->area, pmatch);
457 if (len == -1)
Christopher Faulete00d06c2019-12-16 17:18:42 +0100458 goto fail_rewrite;
Willy Tarreau33810222019-06-12 17:44:02 +0200459
Christopher Faulet2c22a692019-12-18 15:39:56 +0100460 if (http_req_replace_stline(rule->action, output->area, len, px, s) == -1)
Christopher Faulete00d06c2019-12-16 17:18:42 +0100461 goto fail_rewrite;
Willy Tarreau33810222019-06-12 17:44:02 +0200462
Christopher Faulete00d06c2019-12-16 17:18:42 +0100463 leave:
Willy Tarreau33810222019-06-12 17:44:02 +0200464 free_trash_chunk(output);
465 free_trash_chunk(replace);
466 return ret;
Christopher Faulete00d06c2019-12-16 17:18:42 +0100467
468 fail_alloc:
469 if (!(s->flags & SF_ERR_MASK))
470 s->flags |= SF_ERR_RESOURCE;
471 ret = ACT_RET_ERR;
472 goto leave;
473
474 fail_rewrite:
Willy Tarreau4781b152021-04-06 13:53:36 +0200475 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_rewrites);
Christopher Faulete00d06c2019-12-16 17:18:42 +0100476 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200477 _HA_ATOMIC_INC(&s->be->be_counters.failed_rewrites);
William Lallemand36119de2021-03-08 15:26:48 +0100478 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200479 _HA_ATOMIC_INC(&sess->listener->counters->failed_rewrites);
Christopher Faulete00d06c2019-12-16 17:18:42 +0100480 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +0200481 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_rewrites);
Christopher Faulete00d06c2019-12-16 17:18:42 +0100482
Christopher Faulet333bf8c2020-01-22 14:38:05 +0100483 if (!(s->txn->req.flags & HTTP_MSGF_SOFT_RW)) {
Christopher Faulete00d06c2019-12-16 17:18:42 +0100484 ret = ACT_RET_ERR;
Christopher Faulet333bf8c2020-01-22 14:38:05 +0100485 if (!(s->flags & SF_ERR_MASK))
486 s->flags |= SF_ERR_PRXCOND;
487 }
Christopher Faulete00d06c2019-12-16 17:18:42 +0100488 goto leave;
Willy Tarreau33810222019-06-12 17:44:02 +0200489}
490
Christopher Faulet312294f2020-09-02 17:17:44 +0200491/* parse a "replace-uri", "replace-path" or "replace-pathq"
492 * http-request action.
Willy Tarreau33810222019-06-12 17:44:02 +0200493 * This action takes 2 arguments (a regex and a replacement format string).
Christopher Faulet2c22a692019-12-18 15:39:56 +0100494 * The resulting rule makes use of <.action> to store the action (1/3 for now),
Christopher Faulet96bff762019-12-17 13:46:18 +0100495 * <http.re> to store the compiled regex, and <http.fmt> to store the log-format
Willy Tarreau33810222019-06-12 17:44:02 +0200496 * list head. It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
497 */
498static enum act_parse_ret parse_replace_uri(const char **args, int *orig_arg, struct proxy *px,
499 struct act_rule *rule, char **err)
500{
501 int cur_arg = *orig_arg;
502 char *error = NULL;
503
Christopher Faulet312294f2020-09-02 17:17:44 +0200504 switch (args[0][8]) {
505 case 'p':
506 if (args[0][12] == 'q')
507 rule->action = 4; // replace-pathq, same as set-pathq
508 else
509 rule->action = 1; // replace-path, same as set-path
510 break;
511 case 'u':
Christopher Faulet2c22a692019-12-18 15:39:56 +0100512 rule->action = 3; // replace-uri, same as set-uri
Christopher Faulet312294f2020-09-02 17:17:44 +0200513 break;
514 default:
515 memprintf(err, "internal error: unhandled action '%s'", args[0]);
516 return ACT_RET_PRS_ERR;
517 }
Willy Tarreau262c3f12019-12-17 06:52:51 +0100518
Willy Tarreau33810222019-06-12 17:44:02 +0200519 rule->action_ptr = http_action_replace_uri;
Christopher Faulet2eb53962020-01-14 14:47:34 +0100520 rule->release_ptr = release_http_action;
Willy Tarreau33810222019-06-12 17:44:02 +0200521
522 if (!*args[cur_arg] || !*args[cur_arg+1] ||
523 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
524 memprintf(err, "expects exactly 2 arguments <match-regex> and <replace-format>");
525 return ACT_RET_PRS_ERR;
526 }
527
Christopher Faulet96bff762019-12-17 13:46:18 +0100528 if (!(rule->arg.http.re = regex_comp(args[cur_arg], 1, 1, &error))) {
Willy Tarreau33810222019-06-12 17:44:02 +0200529 memprintf(err, "failed to parse the regex : %s", error);
530 free(error);
531 return ACT_RET_PRS_ERR;
532 }
533
Christopher Faulet96bff762019-12-17 13:46:18 +0100534 LIST_INIT(&rule->arg.http.fmt);
Willy Tarreau33810222019-06-12 17:44:02 +0200535 px->conf.args.ctx = ARGC_HRQ;
Christopher Faulet96bff762019-12-17 13:46:18 +0100536 if (!parse_logformat_string(args[cur_arg + 1], px, &rule->arg.http.fmt, LOG_OPT_HTTP,
Willy Tarreau33810222019-06-12 17:44:02 +0200537 (px->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, err)) {
Christopher Faulet1337b322020-01-14 14:50:55 +0100538 regex_free(rule->arg.http.re);
Willy Tarreau33810222019-06-12 17:44:02 +0200539 return ACT_RET_PRS_ERR;
540 }
541
542 (*orig_arg) += 2;
543 return ACT_RET_PRS_OK;
544}
545
Willy Tarreau79e57332018-10-02 16:01:16 +0200546/* This function is just a compliant action wrapper for "set-status". */
547static enum act_return action_http_set_status(struct act_rule *rule, struct proxy *px,
548 struct session *sess, struct stream *s, int flags)
549{
Christopher Faulet96bff762019-12-17 13:46:18 +0100550 if (http_res_set_status(rule->arg.http.i, rule->arg.http.str, s) == -1) {
Willy Tarreau4781b152021-04-06 13:53:36 +0200551 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_rewrites);
Christopher Faulete00d06c2019-12-16 17:18:42 +0100552 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200553 _HA_ATOMIC_INC(&s->be->be_counters.failed_rewrites);
William Lallemand36119de2021-03-08 15:26:48 +0100554 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200555 _HA_ATOMIC_INC(&sess->listener->counters->failed_rewrites);
Christopher Faulete00d06c2019-12-16 17:18:42 +0100556 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +0200557 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_rewrites);
Christopher Faulete00d06c2019-12-16 17:18:42 +0100558
Christopher Faulet333bf8c2020-01-22 14:38:05 +0100559 if (!(s->txn->req.flags & HTTP_MSGF_SOFT_RW)) {
Christopher Faulet333bf8c2020-01-22 14:38:05 +0100560 if (!(s->flags & SF_ERR_MASK))
561 s->flags |= SF_ERR_PRXCOND;
Christopher Faulet692a6c22020-02-07 10:22:31 +0100562 return ACT_RET_ERR;
Christopher Faulet333bf8c2020-01-22 14:38:05 +0100563 }
Christopher Faulete00d06c2019-12-16 17:18:42 +0100564 }
565
Willy Tarreau79e57332018-10-02 16:01:16 +0200566 return ACT_RET_CONT;
567}
568
569/* parse set-status action:
570 * This action accepts a single argument of type int representing
571 * an http status code. It returns ACT_RET_PRS_OK on success,
572 * ACT_RET_PRS_ERR on error.
573 */
574static enum act_parse_ret parse_http_set_status(const char **args, int *orig_arg, struct proxy *px,
575 struct act_rule *rule, char **err)
576{
577 char *error;
578
579 rule->action = ACT_CUSTOM;
580 rule->action_ptr = action_http_set_status;
Christopher Faulet2eb53962020-01-14 14:47:34 +0100581 rule->release_ptr = release_http_action;
Willy Tarreau79e57332018-10-02 16:01:16 +0200582
583 /* Check if an argument is available */
584 if (!*args[*orig_arg]) {
585 memprintf(err, "expects 1 argument: <status>; or 3 arguments: <status> reason <fmt>");
586 return ACT_RET_PRS_ERR;
587 }
588
589 /* convert status code as integer */
Christopher Faulet96bff762019-12-17 13:46:18 +0100590 rule->arg.http.i = strtol(args[*orig_arg], &error, 10);
591 if (*error != '\0' || rule->arg.http.i < 100 || rule->arg.http.i > 999) {
Willy Tarreau79e57332018-10-02 16:01:16 +0200592 memprintf(err, "expects an integer status code between 100 and 999");
593 return ACT_RET_PRS_ERR;
594 }
595
596 (*orig_arg)++;
597
598 /* set custom reason string */
Christopher Faulet96bff762019-12-17 13:46:18 +0100599 rule->arg.http.str = ist(NULL); // If null, we use the default reason for the status code.
Willy Tarreau79e57332018-10-02 16:01:16 +0200600 if (*args[*orig_arg] && strcmp(args[*orig_arg], "reason") == 0 &&
601 (*args[*orig_arg + 1] && strcmp(args[*orig_arg + 1], "if") != 0 && strcmp(args[*orig_arg + 1], "unless") != 0)) {
602 (*orig_arg)++;
Christopher Faulet96bff762019-12-17 13:46:18 +0100603 rule->arg.http.str.ptr = strdup(args[*orig_arg]);
604 rule->arg.http.str.len = strlen(rule->arg.http.str.ptr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200605 (*orig_arg)++;
606 }
607
Christopher Fauletc20b3712020-01-27 15:51:56 +0100608 LIST_INIT(&rule->arg.http.fmt);
Willy Tarreau79e57332018-10-02 16:01:16 +0200609 return ACT_RET_PRS_OK;
610}
611
612/* This function executes the "reject" HTTP action. It clears the request and
613 * response buffer without sending any response. It can be useful as an HTTP
614 * alternative to the silent-drop action to defend against DoS attacks, and may
615 * also be used with HTTP/2 to close a connection instead of just a stream.
616 * The txn status is unchanged, indicating no response was sent. The termination
Christopher Faulet90d22a82020-03-06 11:18:39 +0100617 * flags will indicate "PR". It always returns ACT_RET_ABRT.
Willy Tarreau79e57332018-10-02 16:01:16 +0200618 */
619static enum act_return http_action_reject(struct act_rule *rule, struct proxy *px,
620 struct session *sess, struct stream *s, int flags)
621{
Willy Tarreau0f9cd7b2019-01-31 19:02:43 +0100622 si_must_kill_conn(chn_prod(&s->req));
Willy Tarreau79e57332018-10-02 16:01:16 +0200623 channel_abort(&s->req);
624 channel_abort(&s->res);
Christopher Fauletd4a824e2020-03-06 15:07:09 +0100625 s->req.analysers &= AN_REQ_FLT_END;
626 s->res.analysers &= AN_RES_FLT_END;
Willy Tarreau79e57332018-10-02 16:01:16 +0200627
Willy Tarreau4781b152021-04-06 13:53:36 +0200628 _HA_ATOMIC_INC(&s->be->be_counters.denied_req);
629 _HA_ATOMIC_INC(&sess->fe->fe_counters.denied_req);
Willy Tarreau79e57332018-10-02 16:01:16 +0200630 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200631 _HA_ATOMIC_INC(&sess->listener->counters->denied_req);
Willy Tarreau79e57332018-10-02 16:01:16 +0200632
633 if (!(s->flags & SF_ERR_MASK))
634 s->flags |= SF_ERR_PRXCOND;
635 if (!(s->flags & SF_FINST_MASK))
636 s->flags |= SF_FINST_R;
637
Christopher Faulet90d22a82020-03-06 11:18:39 +0100638 return ACT_RET_ABRT;
Willy Tarreau79e57332018-10-02 16:01:16 +0200639}
640
641/* parse the "reject" action:
642 * This action takes no argument and returns ACT_RET_PRS_OK on success,
643 * ACT_RET_PRS_ERR on error.
644 */
645static enum act_parse_ret parse_http_action_reject(const char **args, int *orig_arg, struct proxy *px,
646 struct act_rule *rule, char **err)
647{
648 rule->action = ACT_CUSTOM;
649 rule->action_ptr = http_action_reject;
650 return ACT_RET_PRS_OK;
651}
652
Olivier Houchard602bf7d2019-05-10 13:59:15 +0200653/* This function executes the "disable-l7-retry" HTTP action.
654 * It disables L7 retries (all retry except for a connection failure). This
655 * can be useful for example to avoid retrying on POST requests.
656 * It just removes the L7 retry flag on the stream_interface, and always
657 * return ACT_RET_CONT;
658 */
659static enum act_return http_req_disable_l7_retry(struct act_rule *rule, struct proxy *px,
660 struct session *sess, struct stream *s, int flags)
661{
662 struct stream_interface *si = &s->si[1];
663
664 /* In theory, the SI_FL_L7_RETRY flags isn't set at this point, but
665 * let's be future-proof and remove it anyway.
666 */
667 si->flags &= ~SI_FL_L7_RETRY;
668 si->flags |= SI_FL_D_L7_RETRY;
669 return ACT_RET_CONT;
670}
671
672/* parse the "disable-l7-retry" action:
673 * This action takes no argument and returns ACT_RET_PRS_OK on success,
674 * ACT_RET_PRS_ERR on error.
675 */
676static enum act_parse_ret parse_http_req_disable_l7_retry(const char **args,
677 int *orig_args, struct proxy *px,
678 struct act_rule *rule, char **err)
679{
680 rule->action = ACT_CUSTOM;
681 rule->action_ptr = http_req_disable_l7_retry;
682 return ACT_RET_PRS_OK;
683}
684
Willy Tarreau79e57332018-10-02 16:01:16 +0200685/* This function executes the "capture" action. It executes a fetch expression,
686 * turns the result into a string and puts it in a capture slot. It always
687 * returns 1. If an error occurs the action is cancelled, but the rule
688 * processing continues.
689 */
690static enum act_return http_action_req_capture(struct act_rule *rule, struct proxy *px,
691 struct session *sess, struct stream *s, int flags)
692{
693 struct sample *key;
694 struct cap_hdr *h = rule->arg.cap.hdr;
695 char **cap = s->req_cap;
696 int len;
697
698 key = sample_fetch_as_type(s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.cap.expr, SMP_T_STR);
699 if (!key)
700 return ACT_RET_CONT;
701
702 if (cap[h->index] == NULL)
703 cap[h->index] = pool_alloc(h->pool);
704
705 if (cap[h->index] == NULL) /* no more capture memory */
706 return ACT_RET_CONT;
707
708 len = key->data.u.str.data;
709 if (len > h->len)
710 len = h->len;
711
712 memcpy(cap[h->index], key->data.u.str.area, len);
713 cap[h->index][len] = 0;
714 return ACT_RET_CONT;
715}
716
717/* This function executes the "capture" action and store the result in a
718 * capture slot if exists. It executes a fetch expression, turns the result
719 * into a string and puts it in a capture slot. It always returns 1. If an
720 * error occurs the action is cancelled, but the rule processing continues.
721 */
722static enum act_return http_action_req_capture_by_id(struct act_rule *rule, struct proxy *px,
723 struct session *sess, struct stream *s, int flags)
724{
725 struct sample *key;
726 struct cap_hdr *h;
727 char **cap = s->req_cap;
728 struct proxy *fe = strm_fe(s);
729 int len;
730 int i;
731
732 /* Look for the original configuration. */
733 for (h = fe->req_cap, i = fe->nb_req_cap - 1;
734 h != NULL && i != rule->arg.capid.idx ;
735 i--, h = h->next);
736 if (!h)
737 return ACT_RET_CONT;
738
739 key = sample_fetch_as_type(s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.capid.expr, SMP_T_STR);
740 if (!key)
741 return ACT_RET_CONT;
742
743 if (cap[h->index] == NULL)
744 cap[h->index] = pool_alloc(h->pool);
745
746 if (cap[h->index] == NULL) /* no more capture memory */
747 return ACT_RET_CONT;
748
749 len = key->data.u.str.data;
750 if (len > h->len)
751 len = h->len;
752
753 memcpy(cap[h->index], key->data.u.str.area, len);
754 cap[h->index][len] = 0;
755 return ACT_RET_CONT;
756}
757
758/* Check an "http-request capture" action.
759 *
760 * The function returns 1 in success case, otherwise, it returns 0 and err is
761 * filled.
762 */
763static int check_http_req_capture(struct act_rule *rule, struct proxy *px, char **err)
764{
765 if (rule->action_ptr != http_action_req_capture_by_id)
766 return 1;
767
Baptiste Assmann19a69b32020-01-16 14:34:22 +0100768 /* capture slots can only be declared in frontends, so we can't check their
769 * existence in backends at configuration parsing step
770 */
771 if (px->cap & PR_CAP_FE && rule->arg.capid.idx >= px->nb_req_cap) {
Willy Tarreau79e57332018-10-02 16:01:16 +0200772 memprintf(err, "unable to find capture id '%d' referenced by http-request capture rule",
773 rule->arg.capid.idx);
774 return 0;
775 }
776
777 return 1;
778}
779
Christopher Faulet2eb53962020-01-14 14:47:34 +0100780/* Release memory allocate by an http capture action */
781static void release_http_capture(struct act_rule *rule)
782{
783 if (rule->action_ptr == http_action_req_capture)
784 release_sample_expr(rule->arg.cap.expr);
785 else
786 release_sample_expr(rule->arg.capid.expr);
787}
788
Willy Tarreau79e57332018-10-02 16:01:16 +0200789/* parse an "http-request capture" action. It takes a single argument which is
790 * a sample fetch expression. It stores the expression into arg->act.p[0] and
791 * the allocated hdr_cap struct or the preallocated "id" into arg->act.p[1].
792 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
793 */
794static enum act_parse_ret parse_http_req_capture(const char **args, int *orig_arg, struct proxy *px,
795 struct act_rule *rule, char **err)
796{
797 struct sample_expr *expr;
798 struct cap_hdr *hdr;
799 int cur_arg;
800 int len = 0;
801
802 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
803 if (strcmp(args[cur_arg], "if") == 0 ||
804 strcmp(args[cur_arg], "unless") == 0)
805 break;
806
807 if (cur_arg < *orig_arg + 3) {
808 memprintf(err, "expects <expression> [ 'len' <length> | id <idx> ]");
809 return ACT_RET_PRS_ERR;
810 }
811
812 cur_arg = *orig_arg;
Willy Tarreaue3b57bf2020-02-14 16:50:14 +0100813 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 +0200814 if (!expr)
815 return ACT_RET_PRS_ERR;
816
817 if (!(expr->fetch->val & SMP_VAL_FE_HRQ_HDR)) {
818 memprintf(err,
819 "fetch method '%s' extracts information from '%s', none of which is available here",
820 args[cur_arg-1], sample_src_names(expr->fetch->use));
Christopher Faulet1337b322020-01-14 14:50:55 +0100821 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200822 return ACT_RET_PRS_ERR;
823 }
824
825 if (!args[cur_arg] || !*args[cur_arg]) {
826 memprintf(err, "expects 'len or 'id'");
Christopher Faulet1337b322020-01-14 14:50:55 +0100827 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200828 return ACT_RET_PRS_ERR;
829 }
830
831 if (strcmp(args[cur_arg], "len") == 0) {
832 cur_arg++;
833
834 if (!(px->cap & PR_CAP_FE)) {
835 memprintf(err, "proxy '%s' has no frontend capability", px->id);
Christopher Faulet1337b322020-01-14 14:50:55 +0100836 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200837 return ACT_RET_PRS_ERR;
838 }
839
840 px->conf.args.ctx = ARGC_CAP;
841
842 if (!args[cur_arg]) {
843 memprintf(err, "missing length value");
Christopher Faulet1337b322020-01-14 14:50:55 +0100844 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200845 return ACT_RET_PRS_ERR;
846 }
847 /* we copy the table name for now, it will be resolved later */
848 len = atoi(args[cur_arg]);
849 if (len <= 0) {
850 memprintf(err, "length must be > 0");
Christopher Faulet1337b322020-01-14 14:50:55 +0100851 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200852 return ACT_RET_PRS_ERR;
853 }
854 cur_arg++;
855
Willy Tarreau79e57332018-10-02 16:01:16 +0200856 hdr = calloc(1, sizeof(*hdr));
857 hdr->next = px->req_cap;
858 hdr->name = NULL; /* not a header capture */
859 hdr->namelen = 0;
860 hdr->len = len;
861 hdr->pool = create_pool("caphdr", hdr->len + 1, MEM_F_SHARED);
862 hdr->index = px->nb_req_cap++;
863
864 px->req_cap = hdr;
865 px->to_log |= LW_REQHDR;
866
867 rule->action = ACT_CUSTOM;
868 rule->action_ptr = http_action_req_capture;
Christopher Faulet2eb53962020-01-14 14:47:34 +0100869 rule->release_ptr = release_http_capture;
Willy Tarreau79e57332018-10-02 16:01:16 +0200870 rule->arg.cap.expr = expr;
871 rule->arg.cap.hdr = hdr;
872 }
873
874 else if (strcmp(args[cur_arg], "id") == 0) {
875 int id;
876 char *error;
877
878 cur_arg++;
879
880 if (!args[cur_arg]) {
881 memprintf(err, "missing id value");
Christopher Faulet1337b322020-01-14 14:50:55 +0100882 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200883 return ACT_RET_PRS_ERR;
884 }
885
886 id = strtol(args[cur_arg], &error, 10);
887 if (*error != '\0') {
888 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
Christopher Faulet1337b322020-01-14 14:50:55 +0100889 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200890 return ACT_RET_PRS_ERR;
891 }
892 cur_arg++;
893
894 px->conf.args.ctx = ARGC_CAP;
895
896 rule->action = ACT_CUSTOM;
897 rule->action_ptr = http_action_req_capture_by_id;
898 rule->check_ptr = check_http_req_capture;
Christopher Faulet2eb53962020-01-14 14:47:34 +0100899 rule->release_ptr = release_http_capture;
Willy Tarreau79e57332018-10-02 16:01:16 +0200900 rule->arg.capid.expr = expr;
901 rule->arg.capid.idx = id;
902 }
903
904 else {
905 memprintf(err, "expects 'len' or 'id', found '%s'", args[cur_arg]);
Christopher Faulet1337b322020-01-14 14:50:55 +0100906 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200907 return ACT_RET_PRS_ERR;
908 }
909
910 *orig_arg = cur_arg;
911 return ACT_RET_PRS_OK;
912}
913
914/* This function executes the "capture" action and store the result in a
915 * capture slot if exists. It executes a fetch expression, turns the result
916 * into a string and puts it in a capture slot. It always returns 1. If an
917 * error occurs the action is cancelled, but the rule processing continues.
918 */
919static enum act_return http_action_res_capture_by_id(struct act_rule *rule, struct proxy *px,
920 struct session *sess, struct stream *s, int flags)
921{
922 struct sample *key;
923 struct cap_hdr *h;
924 char **cap = s->res_cap;
925 struct proxy *fe = strm_fe(s);
926 int len;
927 int i;
928
929 /* Look for the original configuration. */
930 for (h = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
931 h != NULL && i != rule->arg.capid.idx ;
932 i--, h = h->next);
933 if (!h)
934 return ACT_RET_CONT;
935
936 key = sample_fetch_as_type(s->be, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL, rule->arg.capid.expr, SMP_T_STR);
937 if (!key)
938 return ACT_RET_CONT;
939
940 if (cap[h->index] == NULL)
941 cap[h->index] = pool_alloc(h->pool);
942
943 if (cap[h->index] == NULL) /* no more capture memory */
944 return ACT_RET_CONT;
945
946 len = key->data.u.str.data;
947 if (len > h->len)
948 len = h->len;
949
950 memcpy(cap[h->index], key->data.u.str.area, len);
951 cap[h->index][len] = 0;
952 return ACT_RET_CONT;
953}
954
955/* Check an "http-response capture" action.
956 *
957 * The function returns 1 in success case, otherwise, it returns 0 and err is
958 * filled.
959 */
960static int check_http_res_capture(struct act_rule *rule, struct proxy *px, char **err)
961{
962 if (rule->action_ptr != http_action_res_capture_by_id)
963 return 1;
964
Tim Duesterhusf3f4aa02020-07-03 13:43:42 +0200965 /* capture slots can only be declared in frontends, so we can't check their
966 * existence in backends at configuration parsing step
967 */
968 if (px->cap & PR_CAP_FE && rule->arg.capid.idx >= px->nb_rsp_cap) {
Willy Tarreau79e57332018-10-02 16:01:16 +0200969 memprintf(err, "unable to find capture id '%d' referenced by http-response capture rule",
970 rule->arg.capid.idx);
971 return 0;
972 }
973
974 return 1;
975}
976
977/* parse an "http-response capture" action. It takes a single argument which is
978 * a sample fetch expression. It stores the expression into arg->act.p[0] and
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -0700979 * the allocated hdr_cap struct of the preallocated id into arg->act.p[1].
Willy Tarreau79e57332018-10-02 16:01:16 +0200980 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
981 */
982static enum act_parse_ret parse_http_res_capture(const char **args, int *orig_arg, struct proxy *px,
983 struct act_rule *rule, char **err)
984{
985 struct sample_expr *expr;
986 int cur_arg;
987 int id;
988 char *error;
989
990 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
991 if (strcmp(args[cur_arg], "if") == 0 ||
992 strcmp(args[cur_arg], "unless") == 0)
993 break;
994
995 if (cur_arg < *orig_arg + 3) {
996 memprintf(err, "expects <expression> id <idx>");
997 return ACT_RET_PRS_ERR;
998 }
999
1000 cur_arg = *orig_arg;
Willy Tarreaue3b57bf2020-02-14 16:50:14 +01001001 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 +02001002 if (!expr)
1003 return ACT_RET_PRS_ERR;
1004
1005 if (!(expr->fetch->val & SMP_VAL_FE_HRS_HDR)) {
1006 memprintf(err,
1007 "fetch method '%s' extracts information from '%s', none of which is available here",
1008 args[cur_arg-1], sample_src_names(expr->fetch->use));
Christopher Faulet1337b322020-01-14 14:50:55 +01001009 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +02001010 return ACT_RET_PRS_ERR;
1011 }
1012
1013 if (!args[cur_arg] || !*args[cur_arg]) {
1014 memprintf(err, "expects 'id'");
Christopher Faulet1337b322020-01-14 14:50:55 +01001015 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +02001016 return ACT_RET_PRS_ERR;
1017 }
1018
1019 if (strcmp(args[cur_arg], "id") != 0) {
1020 memprintf(err, "expects 'id', found '%s'", args[cur_arg]);
Christopher Faulet1337b322020-01-14 14:50:55 +01001021 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +02001022 return ACT_RET_PRS_ERR;
1023 }
1024
1025 cur_arg++;
1026
1027 if (!args[cur_arg]) {
1028 memprintf(err, "missing id value");
Christopher Faulet1337b322020-01-14 14:50:55 +01001029 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +02001030 return ACT_RET_PRS_ERR;
1031 }
1032
1033 id = strtol(args[cur_arg], &error, 10);
1034 if (*error != '\0') {
1035 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
Christopher Faulet1337b322020-01-14 14:50:55 +01001036 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +02001037 return ACT_RET_PRS_ERR;
1038 }
1039 cur_arg++;
1040
1041 px->conf.args.ctx = ARGC_CAP;
1042
1043 rule->action = ACT_CUSTOM;
1044 rule->action_ptr = http_action_res_capture_by_id;
1045 rule->check_ptr = check_http_res_capture;
Christopher Faulet2eb53962020-01-14 14:47:34 +01001046 rule->release_ptr = release_http_capture;
Willy Tarreau79e57332018-10-02 16:01:16 +02001047 rule->arg.capid.expr = expr;
1048 rule->arg.capid.idx = id;
1049
1050 *orig_arg = cur_arg;
1051 return ACT_RET_PRS_OK;
1052}
1053
Christopher Faulet81e20172019-12-12 16:40:30 +01001054/* Parse a "allow" action for a request or a response rule. It takes no argument. It
1055 * returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
1056 */
1057static enum act_parse_ret parse_http_allow(const char **args, int *orig_arg, struct proxy *px,
1058 struct act_rule *rule, char **err)
1059{
1060 rule->action = ACT_ACTION_ALLOW;
Christopher Faulet245cf792019-12-18 14:58:12 +01001061 rule->flags |= ACT_FLAG_FINAL;
Christopher Faulet81e20172019-12-12 16:40:30 +01001062 return ACT_RET_PRS_OK;
1063}
1064
Christopher Faulete0fca292020-01-13 21:49:03 +01001065/* Parse "deny" or "tarpit" actions for a request rule or "deny" action for a
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001066 * response rule. It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on
1067 * error. It relies on http_parse_http_reply() to set
1068 * <.arg.http_reply>.
Christopher Faulet81e20172019-12-12 16:40:30 +01001069 */
Christopher Faulete0fca292020-01-13 21:49:03 +01001070static enum act_parse_ret parse_http_deny(const char **args, int *orig_arg, struct proxy *px,
1071 struct act_rule *rule, char **err)
Christopher Faulet81e20172019-12-12 16:40:30 +01001072{
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001073 int default_status;
1074 int cur_arg, arg = 0;
Christopher Faulet81e20172019-12-12 16:40:30 +01001075
1076 cur_arg = *orig_arg;
Christopher Faulete0fca292020-01-13 21:49:03 +01001077 if (rule->from == ACT_F_HTTP_REQ) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001078 if (strcmp(args[cur_arg - 1], "tarpit") == 0) {
Christopher Faulete0fca292020-01-13 21:49:03 +01001079 rule->action = ACT_HTTP_REQ_TARPIT;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001080 default_status = 500;
Christopher Faulet81e20172019-12-12 16:40:30 +01001081 }
Christopher Faulete0fca292020-01-13 21:49:03 +01001082 else {
1083 rule->action = ACT_ACTION_DENY;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001084 default_status = 403;
Christopher Faulet81e20172019-12-12 16:40:30 +01001085 }
Christopher Faulet81e20172019-12-12 16:40:30 +01001086 }
Christopher Faulete0fca292020-01-13 21:49:03 +01001087 else {
Christopher Faulet554c0eb2020-01-14 12:00:28 +01001088 rule->action = ACT_ACTION_DENY;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001089 default_status = 502;
Christopher Faulete0fca292020-01-13 21:49:03 +01001090 }
Christopher Faulet040c8cd2020-01-13 16:43:45 +01001091
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001092 /* If no args or only a deny_status specified, fallback on the legacy
1093 * mode and use default error files despite the fact that
1094 * default-errorfiles is not used. Otherwise, parse an http reply.
1095 */
Christopher Faulet040c8cd2020-01-13 16:43:45 +01001096
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001097 /* Prepare parsing of log-format strings */
1098 px->conf.args.ctx = ((rule->from == ACT_F_HTTP_REQ) ? ARGC_HRQ : ARGC_HRS);
Christopher Faulet554c0eb2020-01-14 12:00:28 +01001099
Christopher Faulet9467f182020-06-30 09:32:01 +02001100 if (!*(args[cur_arg]) || strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001101 rule->arg.http_reply = http_parse_http_reply((const char *[]){"default-errorfiles", ""}, &arg, px, default_status, err);
1102 goto end;
Christopher Faulet554c0eb2020-01-14 12:00:28 +01001103 }
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001104
1105 if (strcmp(args[cur_arg], "deny_status") == 0) {
Christopher Faulet9467f182020-06-30 09:32:01 +02001106 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 +02001107 rule->arg.http_reply = http_parse_http_reply((const char *[]){"status", args[cur_arg+1], "default-errorfiles", ""},
1108 &arg, px, default_status, err);
1109 *orig_arg += 2;
1110 goto end;
Christopher Faulet554c0eb2020-01-14 12:00:28 +01001111 }
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001112 args[cur_arg] += 5; /* skip "deny_" for the parsing */
Christopher Faulet554c0eb2020-01-14 12:00:28 +01001113 }
1114
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001115 rule->arg.http_reply = http_parse_http_reply(args, orig_arg, px, default_status, err);
Christopher Faulet554c0eb2020-01-14 12:00:28 +01001116
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001117 end:
1118 if (!rule->arg.http_reply)
1119 return ACT_RET_PRS_ERR;
1120
1121 rule->flags |= ACT_FLAG_FINAL;
1122 rule->check_ptr = check_act_http_reply;
1123 rule->release_ptr = release_act_http_reply;
Christopher Faulet81e20172019-12-12 16:40:30 +01001124 return ACT_RET_PRS_OK;
1125}
1126
Christopher Fauletb3048832020-05-27 15:26:43 +02001127
1128/* This function executes a auth action. It builds an 401/407 HTX message using
1129 * the corresponding proxy's error message. On success, it returns
1130 * ACT_RET_ABRT. If an error occurs ACT_RET_ERR is returned.
1131 */
1132static enum act_return http_action_auth(struct act_rule *rule, struct proxy *px,
1133 struct session *sess, struct stream *s, int flags)
1134{
1135 struct channel *req = &s->req;
1136 struct channel *res = &s->res;
1137 struct htx *htx = htx_from_buf(&res->buf);
1138 struct http_reply *reply;
1139 const char *auth_realm;
1140 struct http_hdr_ctx ctx;
1141 struct ist hdr;
1142
1143 /* Auth might be performed on regular http-req rules as well as on stats */
1144 auth_realm = rule->arg.http.str.ptr;
1145 if (!auth_realm) {
1146 if (px->uri_auth && s->current_rule_list == &px->uri_auth->http_req_rules)
1147 auth_realm = STATS_DEFAULT_REALM;
1148 else
1149 auth_realm = px->id;
1150 }
1151
1152 if (!(s->txn->flags & TX_USE_PX_CONN)) {
1153 s->txn->status = 401;
1154 hdr = ist("WWW-Authenticate");
1155 }
1156 else {
1157 s->txn->status = 407;
1158 hdr = ist("Proxy-Authenticate");
1159 }
1160 reply = http_error_message(s);
1161 channel_htx_truncate(res, htx);
1162
1163 if (chunk_printf(&trash, "Basic realm=\"%s\"", auth_realm) == -1)
1164 goto fail;
1165
1166 /* Write the generic 40x message */
1167 if (http_reply_to_htx(s, htx, reply) == -1)
1168 goto fail;
1169
1170 /* Remove all existing occurrences of the XXX-Authenticate header */
1171 ctx.blk = NULL;
1172 while (http_find_header(htx, hdr, &ctx, 1))
1173 http_remove_header(htx, &ctx);
1174
1175 /* Now a the right XXX-Authenticate header */
1176 if (!http_add_header(htx, hdr, ist2(b_orig(&trash), b_data(&trash))))
1177 goto fail;
1178
1179 /* Finally forward the reply */
1180 htx_to_buf(htx, &res->buf);
1181 if (!http_forward_proxy_resp(s, 1))
1182 goto fail;
1183
1184 /* Note: Only eval on the request */
1185 s->logs.tv_request = now;
1186 req->analysers &= AN_REQ_FLT_END;
1187
1188 if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Willy Tarreau4781b152021-04-06 13:53:36 +02001189 _HA_ATOMIC_INC(&s->sess->fe->fe_counters.intercepted_req);
Christopher Fauletb3048832020-05-27 15:26:43 +02001190
1191 if (!(s->flags & SF_ERR_MASK))
1192 s->flags |= SF_ERR_LOCAL;
1193 if (!(s->flags & SF_FINST_MASK))
1194 s->flags |= SF_FINST_R;
1195
1196 stream_inc_http_err_ctr(s);
1197 return ACT_RET_ABRT;
1198
1199 fail:
1200 /* If an error occurred, remove the incomplete HTTP response from the
1201 * buffer */
1202 channel_htx_truncate(res, htx);
1203 return ACT_RET_ERR;
1204}
1205
Christopher Faulet81e20172019-12-12 16:40:30 +01001206/* Parse a "auth" action. It may take 2 optional arguments to define a "realm"
1207 * parameter. It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
1208 */
1209static enum act_parse_ret parse_http_auth(const char **args, int *orig_arg, struct proxy *px,
1210 struct act_rule *rule, char **err)
1211{
1212 int cur_arg;
1213
Christopher Fauletb3048832020-05-27 15:26:43 +02001214 rule->action = ACT_CUSTOM;
Christopher Faulet245cf792019-12-18 14:58:12 +01001215 rule->flags |= ACT_FLAG_FINAL;
Christopher Fauletb3048832020-05-27 15:26:43 +02001216 rule->action_ptr = http_action_auth;
Christopher Faulet2eb53962020-01-14 14:47:34 +01001217 rule->release_ptr = release_http_action;
Christopher Faulet81e20172019-12-12 16:40:30 +01001218
1219 cur_arg = *orig_arg;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001220 if (strcmp(args[cur_arg], "realm") == 0) {
Christopher Faulet81e20172019-12-12 16:40:30 +01001221 cur_arg++;
1222 if (!*args[cur_arg]) {
1223 memprintf(err, "missing realm value.\n");
1224 return ACT_RET_PRS_ERR;
1225 }
Christopher Faulet96bff762019-12-17 13:46:18 +01001226 rule->arg.http.str.ptr = strdup(args[cur_arg]);
1227 rule->arg.http.str.len = strlen(rule->arg.http.str.ptr);
Christopher Faulet81e20172019-12-12 16:40:30 +01001228 cur_arg++;
1229 }
1230
Christopher Fauletc20b3712020-01-27 15:51:56 +01001231 LIST_INIT(&rule->arg.http.fmt);
Christopher Faulet81e20172019-12-12 16:40:30 +01001232 *orig_arg = cur_arg;
1233 return ACT_RET_PRS_OK;
1234}
1235
1236/* Parse a "set-nice" action. It takes the nice value as argument. It returns
1237 * ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
1238 */
1239static enum act_parse_ret parse_http_set_nice(const char **args, int *orig_arg, struct proxy *px,
1240 struct act_rule *rule, char **err)
1241{
1242 int cur_arg;
1243
1244 rule->action = ACT_HTTP_SET_NICE;
1245
1246 cur_arg = *orig_arg;
1247 if (!*args[cur_arg]) {
1248 memprintf(err, "expects exactly 1 argument (integer value)");
1249 return ACT_RET_PRS_ERR;
1250 }
Christopher Faulet96bff762019-12-17 13:46:18 +01001251 rule->arg.http.i = atoi(args[cur_arg]);
1252 if (rule->arg.http.i < -1024)
1253 rule->arg.http.i = -1024;
1254 else if (rule->arg.http.i > 1024)
1255 rule->arg.http.i = 1024;
Christopher Faulet81e20172019-12-12 16:40:30 +01001256
Christopher Fauletc20b3712020-01-27 15:51:56 +01001257 LIST_INIT(&rule->arg.http.fmt);
Christopher Faulet81e20172019-12-12 16:40:30 +01001258 *orig_arg = cur_arg + 1;
1259 return ACT_RET_PRS_OK;
1260}
1261
1262/* Parse a "set-tos" action. It takes the TOS value as argument. It returns
1263 * ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
1264 */
1265static enum act_parse_ret parse_http_set_tos(const char **args, int *orig_arg, struct proxy *px,
1266 struct act_rule *rule, char **err)
1267{
1268#ifdef IP_TOS
1269 char *endp;
1270 int cur_arg;
1271
1272 rule->action = ACT_HTTP_SET_TOS;
1273
1274 cur_arg = *orig_arg;
1275 if (!*args[cur_arg]) {
1276 memprintf(err, "expects exactly 1 argument (integer/hex value)");
1277 return ACT_RET_PRS_ERR;
1278 }
Christopher Faulet96bff762019-12-17 13:46:18 +01001279 rule->arg.http.i = strtol(args[cur_arg], &endp, 0);
Christopher Faulet81e20172019-12-12 16:40:30 +01001280 if (endp && *endp != '\0') {
1281 memprintf(err, "invalid character starting at '%s' (integer/hex value expected)", endp);
1282 return ACT_RET_PRS_ERR;
1283 }
1284
Christopher Fauletc20b3712020-01-27 15:51:56 +01001285 LIST_INIT(&rule->arg.http.fmt);
Christopher Faulet81e20172019-12-12 16:40:30 +01001286 *orig_arg = cur_arg + 1;
1287 return ACT_RET_PRS_OK;
1288#else
1289 memprintf(err, "not supported on this platform (IP_TOS undefined)");
1290 return ACT_RET_PRS_ERR;
1291#endif
1292}
1293
1294/* Parse a "set-mark" action. It takes the MARK value as argument. It returns
1295 * ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
1296 */
1297static enum act_parse_ret parse_http_set_mark(const char **args, int *orig_arg, struct proxy *px,
1298 struct act_rule *rule, char **err)
1299{
1300#ifdef SO_MARK
1301 char *endp;
1302 int cur_arg;
1303
1304 rule->action = ACT_HTTP_SET_MARK;
1305
1306 cur_arg = *orig_arg;
1307 if (!*args[cur_arg]) {
1308 memprintf(err, "expects exactly 1 argument (integer/hex value)");
1309 return ACT_RET_PRS_ERR;
1310 }
Christopher Faulet96bff762019-12-17 13:46:18 +01001311 rule->arg.http.i = strtoul(args[cur_arg], &endp, 0);
Christopher Faulet81e20172019-12-12 16:40:30 +01001312 if (endp && *endp != '\0') {
1313 memprintf(err, "invalid character starting at '%s' (integer/hex value expected)", endp);
1314 return ACT_RET_PRS_ERR;
1315 }
1316
Christopher Fauletc20b3712020-01-27 15:51:56 +01001317 LIST_INIT(&rule->arg.http.fmt);
Christopher Faulet81e20172019-12-12 16:40:30 +01001318 *orig_arg = cur_arg + 1;
1319 global.last_checks |= LSTCHK_NETADM;
1320 return ACT_RET_PRS_OK;
1321#else
1322 memprintf(err, "not supported on this platform (SO_MARK undefined)");
1323 return ACT_RET_PRS_ERR;
1324#endif
1325}
1326
1327/* Parse a "set-log-level" action. It takes the level value as argument. It
1328 * returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
1329 */
1330static enum act_parse_ret parse_http_set_log_level(const char **args, int *orig_arg, struct proxy *px,
1331 struct act_rule *rule, char **err)
1332{
1333 int cur_arg;
1334
1335 rule->action = ACT_HTTP_SET_LOGL;
1336
1337 cur_arg = *orig_arg;
1338 if (!*args[cur_arg]) {
1339 bad_log_level:
1340 memprintf(err, "expects exactly 1 argument (log level name or 'silent')");
1341 return ACT_RET_PRS_ERR;
1342 }
1343 if (strcmp(args[cur_arg], "silent") == 0)
Christopher Faulet96bff762019-12-17 13:46:18 +01001344 rule->arg.http.i = -1;
1345 else if ((rule->arg.http.i = get_log_level(args[cur_arg]) + 1) == 0)
Christopher Faulet81e20172019-12-12 16:40:30 +01001346 goto bad_log_level;
1347
Christopher Fauletc20b3712020-01-27 15:51:56 +01001348 LIST_INIT(&rule->arg.http.fmt);
Christopher Faulet81e20172019-12-12 16:40:30 +01001349 *orig_arg = cur_arg + 1;
1350 return ACT_RET_PRS_OK;
1351}
1352
Christopher Faulet91b3ec12020-01-17 22:30:06 +01001353/* This function executes a early-hint action. It adds an HTTP Early Hint HTTP
1354 * 103 response header with <.arg.http.str> name and with a value built
1355 * according to <.arg.http.fmt> log line format. If it is the first early-hint
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001356 * rule of series, the 103 response start-line is added first. At the end, if
Christopher Faulet91b3ec12020-01-17 22:30:06 +01001357 * the next rule is not an early-hint rule or if it is the last rule, the EOH
1358 * block is added to terminate the response. On success, it returns
1359 * ACT_RET_CONT. If an error occurs while soft rewrites are enabled, the action
1360 * is canceled, but the rule processing continue. Otherwsize ACT_RET_ERR is
1361 * returned.
1362 */
1363static enum act_return http_action_early_hint(struct act_rule *rule, struct proxy *px,
1364 struct session *sess, struct stream *s, int flags)
1365{
1366 struct act_rule *prev_rule, *next_rule;
1367 struct channel *res = &s->res;
1368 struct htx *htx = htx_from_buf(&res->buf);
1369 struct buffer *value = alloc_trash_chunk();
1370 enum act_return ret = ACT_RET_CONT;
1371
1372 if (!(s->txn->req.flags & HTTP_MSGF_VER_11))
1373 goto leave;
1374
1375 if (!value) {
1376 if (!(s->flags & SF_ERR_MASK))
1377 s->flags |= SF_ERR_RESOURCE;
1378 goto error;
1379 }
1380
1381 /* get previous and next rules */
1382 prev_rule = LIST_PREV(&rule->list, typeof(rule), list);
1383 next_rule = LIST_NEXT(&rule->list, typeof(rule), list);
1384
1385 /* if no previous rule or previous rule is not early-hint, start a new response. Otherwise,
1386 * continue to add link to a previously started response */
1387 if (&prev_rule->list == s->current_rule_list || prev_rule->action_ptr != http_action_early_hint) {
1388 struct htx_sl *sl;
1389 unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|
1390 HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
1391
1392 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
1393 ist("HTTP/1.1"), ist("103"), ist("Early Hints"));
1394 if (!sl)
1395 goto error;
1396 sl->info.res.status = 103;
1397 }
1398
1399 /* Add the HTTP Early Hint HTTP 103 response heade */
1400 value->data = build_logline(s, b_tail(value), b_room(value), &rule->arg.http.fmt);
1401 if (!htx_add_header(htx, rule->arg.http.str, ist2(b_head(value), b_data(value))))
1402 goto error;
1403
1404 /* if it is the last rule or the next one is not an early-hint, terminate the current
1405 * response. */
1406 if (&next_rule->list == s->current_rule_list || next_rule->action_ptr != http_action_early_hint) {
Christopher Faulet91b3ec12020-01-17 22:30:06 +01001407 if (!htx_add_endof(htx, HTX_BLK_EOH)) {
1408 /* If an error occurred during an Early-hint rule,
1409 * remove the incomplete HTTP 103 response from the
1410 * buffer */
1411 goto error;
1412 }
1413
Christopher Fauleta72a7e42020-01-28 09:28:11 +01001414 if (!http_forward_proxy_resp(s, 0))
1415 goto error;
Christopher Faulet91b3ec12020-01-17 22:30:06 +01001416 }
1417
1418 leave:
1419 free_trash_chunk(value);
1420 return ret;
1421
1422 error:
1423 /* If an error occurred during an Early-hint rule, remove the incomplete
1424 * HTTP 103 response from the buffer */
1425 channel_htx_truncate(res, htx);
1426 ret = ACT_RET_ERR;
1427 goto leave;
1428}
1429
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001430/* This function executes a set-header or add-header actions. It builds a string
1431 * in the trash from the specified format string. It finds the action to be
1432 * performed in <.action>, previously filled by function parse_set_header(). The
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001433 * replacement action is executed by the function http_action_set_header(). On
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001434 * success, it returns ACT_RET_CONT. If an error occurs while soft rewrites are
1435 * enabled, the action is canceled, but the rule processing continue. Otherwsize
1436 * ACT_RET_ERR is returned.
1437 */
1438static enum act_return http_action_set_header(struct act_rule *rule, struct proxy *px,
1439 struct session *sess, struct stream *s, int flags)
1440{
Christopher Faulet91e31d82020-01-24 15:37:13 +01001441 struct http_msg *msg = ((rule->from == ACT_F_HTTP_REQ) ? &s->txn->req : &s->txn->rsp);
1442 struct htx *htx = htxbuf(&msg->chn->buf);
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001443 enum act_return ret = ACT_RET_CONT;
1444 struct buffer *replace;
1445 struct http_hdr_ctx ctx;
1446 struct ist n, v;
1447
1448 replace = alloc_trash_chunk();
1449 if (!replace)
1450 goto fail_alloc;
1451
1452 replace->data = build_logline(s, replace->area, replace->size, &rule->arg.http.fmt);
1453 n = rule->arg.http.str;
1454 v = ist2(replace->area, replace->data);
1455
1456 if (rule->action == 0) { // set-header
1457 /* remove all occurrences of the header */
1458 ctx.blk = NULL;
1459 while (http_find_header(htx, n, &ctx, 1))
1460 http_remove_header(htx, &ctx);
1461 }
1462
1463 /* Now add header */
1464 if (!http_add_header(htx, n, v))
1465 goto fail_rewrite;
1466
1467 leave:
1468 free_trash_chunk(replace);
1469 return ret;
1470
1471 fail_alloc:
1472 if (!(s->flags & SF_ERR_MASK))
1473 s->flags |= SF_ERR_RESOURCE;
1474 ret = ACT_RET_ERR;
1475 goto leave;
1476
1477 fail_rewrite:
Willy Tarreau4781b152021-04-06 13:53:36 +02001478 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_rewrites);
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001479 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +02001480 _HA_ATOMIC_INC(&s->be->be_counters.failed_rewrites);
William Lallemand36119de2021-03-08 15:26:48 +01001481 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001482 _HA_ATOMIC_INC(&sess->listener->counters->failed_rewrites);
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001483 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001484 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_rewrites);
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001485
Christopher Faulet333bf8c2020-01-22 14:38:05 +01001486 if (!(msg->flags & HTTP_MSGF_SOFT_RW)) {
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001487 ret = ACT_RET_ERR;
Christopher Faulet333bf8c2020-01-22 14:38:05 +01001488 if (!(s->flags & SF_ERR_MASK))
1489 s->flags |= SF_ERR_PRXCOND;
1490 }
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001491 goto leave;
1492}
1493
Christopher Faulet81e20172019-12-12 16:40:30 +01001494/* Parse a "set-header", "add-header" or "early-hint" actions. It takes an
1495 * header name and a log-format string as arguments. It returns ACT_RET_PRS_OK
1496 * on success, ACT_RET_PRS_ERR on error.
1497 *
1498 * Note: same function is used for the request and the response. However
1499 * "early-hint" rules are only supported for request rules.
1500 */
1501static enum act_parse_ret parse_http_set_header(const char **args, int *orig_arg, struct proxy *px,
1502 struct act_rule *rule, char **err)
1503{
Christopher Faulet81e20172019-12-12 16:40:30 +01001504 int cap, cur_arg;
1505
Christopher Faulet91b3ec12020-01-17 22:30:06 +01001506 if (args[*orig_arg-1][0] == 'e') {
1507 rule->action = ACT_CUSTOM;
1508 rule->action_ptr = http_action_early_hint;
1509 }
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001510 else {
1511 if (args[*orig_arg-1][0] == 's')
1512 rule->action = 0; // set-header
1513 else
1514 rule->action = 1; // add-header
1515 rule->action_ptr = http_action_set_header;
1516 }
Christopher Faulet2eb53962020-01-14 14:47:34 +01001517 rule->release_ptr = release_http_action;
Christopher Faulet81e20172019-12-12 16:40:30 +01001518
1519 cur_arg = *orig_arg;
1520 if (!*args[cur_arg] || !*args[cur_arg+1]) {
1521 memprintf(err, "expects exactly 2 arguments");
1522 return ACT_RET_PRS_ERR;
1523 }
1524
Christopher Faulet81e20172019-12-12 16:40:30 +01001525
Christopher Faulet96bff762019-12-17 13:46:18 +01001526 rule->arg.http.str.ptr = strdup(args[cur_arg]);
1527 rule->arg.http.str.len = strlen(rule->arg.http.str.ptr);
1528 LIST_INIT(&rule->arg.http.fmt);
Christopher Faulet81e20172019-12-12 16:40:30 +01001529
1530 if (rule->from == ACT_F_HTTP_REQ) {
1531 px->conf.args.ctx = ARGC_HRQ;
1532 cap = (px->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR;
1533 }
1534 else{
1535 px->conf.args.ctx = ARGC_HRS;
1536 cap = (px->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR;
1537 }
1538
1539 cur_arg++;
Christopher Faulet1337b322020-01-14 14:50:55 +01001540 if (!parse_logformat_string(args[cur_arg], px, &rule->arg.http.fmt, LOG_OPT_HTTP, cap, err)) {
Tim Duesterhused526372020-03-05 17:56:33 +01001541 istfree(&rule->arg.http.str);
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
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001553/* This function executes a replace-header or replace-value actions. It
1554 * builds a string in the trash from the specified format string. It finds
1555 * the action to be performed in <.action>, previously filled by function
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001556 * parse_replace_header(). The replacement action is executed by the function
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001557 * http_action_replace_header(). On success, it returns ACT_RET_CONT. If an error
1558 * occurs while soft rewrites are enabled, the action is canceled, but the rule
1559 * processing continue. Otherwsize ACT_RET_ERR is returned.
1560 */
1561static enum act_return http_action_replace_header(struct act_rule *rule, struct proxy *px,
1562 struct session *sess, struct stream *s, int flags)
1563{
Christopher Faulet91e31d82020-01-24 15:37:13 +01001564 struct http_msg *msg = ((rule->from == ACT_F_HTTP_REQ) ? &s->txn->req : &s->txn->rsp);
1565 struct htx *htx = htxbuf(&msg->chn->buf);
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001566 enum act_return ret = ACT_RET_CONT;
1567 struct buffer *replace;
1568 int r;
1569
1570 replace = alloc_trash_chunk();
1571 if (!replace)
1572 goto fail_alloc;
1573
1574 replace->data = build_logline(s, replace->area, replace->size, &rule->arg.http.fmt);
1575
1576 r = http_replace_hdrs(s, htx, rule->arg.http.str, replace->area, rule->arg.http.re, (rule->action == 0));
1577 if (r == -1)
1578 goto fail_rewrite;
1579
1580 leave:
1581 free_trash_chunk(replace);
1582 return ret;
1583
1584 fail_alloc:
1585 if (!(s->flags & SF_ERR_MASK))
1586 s->flags |= SF_ERR_RESOURCE;
1587 ret = ACT_RET_ERR;
1588 goto leave;
1589
1590 fail_rewrite:
Willy Tarreau4781b152021-04-06 13:53:36 +02001591 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_rewrites);
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001592 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +02001593 _HA_ATOMIC_INC(&s->be->be_counters.failed_rewrites);
William Lallemand36119de2021-03-08 15:26:48 +01001594 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001595 _HA_ATOMIC_INC(&sess->listener->counters->failed_rewrites);
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001596 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001597 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_rewrites);
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001598
Christopher Faulet333bf8c2020-01-22 14:38:05 +01001599 if (!(msg->flags & HTTP_MSGF_SOFT_RW)) {
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001600 ret = ACT_RET_ERR;
Christopher Faulet333bf8c2020-01-22 14:38:05 +01001601 if (!(s->flags & SF_ERR_MASK))
1602 s->flags |= SF_ERR_PRXCOND;
1603 }
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001604 goto leave;
1605}
1606
Christopher Faulet81e20172019-12-12 16:40:30 +01001607/* Parse a "replace-header" or "replace-value" actions. It takes an header name,
1608 * a regex and replacement string as arguments. It returns ACT_RET_PRS_OK on
1609 * success, ACT_RET_PRS_ERR on error.
1610 */
1611static enum act_parse_ret parse_http_replace_header(const char **args, int *orig_arg, struct proxy *px,
1612 struct act_rule *rule, char **err)
1613{
1614 int cap, cur_arg;
1615
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001616 if (args[*orig_arg-1][8] == 'h')
1617 rule->action = 0; // replace-header
1618 else
1619 rule->action = 1; // replace-value
1620 rule->action_ptr = http_action_replace_header;
Christopher Faulet2eb53962020-01-14 14:47:34 +01001621 rule->release_ptr = release_http_action;
Christopher Faulet81e20172019-12-12 16:40:30 +01001622
1623 cur_arg = *orig_arg;
1624 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2]) {
1625 memprintf(err, "expects exactly 3 arguments");
1626 return ACT_RET_PRS_ERR;
1627 }
1628
Christopher Faulet96bff762019-12-17 13:46:18 +01001629 rule->arg.http.str.ptr = strdup(args[cur_arg]);
1630 rule->arg.http.str.len = strlen(rule->arg.http.str.ptr);
1631 LIST_INIT(&rule->arg.http.fmt);
Christopher Faulet81e20172019-12-12 16:40:30 +01001632
1633 cur_arg++;
Christopher Faulet1337b322020-01-14 14:50:55 +01001634 if (!(rule->arg.http.re = regex_comp(args[cur_arg], 1, 1, err))) {
Tim Duesterhused526372020-03-05 17:56:33 +01001635 istfree(&rule->arg.http.str);
Christopher Faulet81e20172019-12-12 16:40:30 +01001636 return ACT_RET_PRS_ERR;
Christopher Faulet1337b322020-01-14 14:50:55 +01001637 }
Christopher Faulet81e20172019-12-12 16:40:30 +01001638
1639 if (rule->from == ACT_F_HTTP_REQ) {
1640 px->conf.args.ctx = ARGC_HRQ;
1641 cap = (px->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR;
1642 }
1643 else{
1644 px->conf.args.ctx = ARGC_HRS;
1645 cap = (px->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR;
1646 }
1647
1648 cur_arg++;
Christopher Faulet1337b322020-01-14 14:50:55 +01001649 if (!parse_logformat_string(args[cur_arg], px, &rule->arg.http.fmt, LOG_OPT_HTTP, cap, err)) {
Tim Duesterhused526372020-03-05 17:56:33 +01001650 istfree(&rule->arg.http.str);
Christopher Faulet1337b322020-01-14 14:50:55 +01001651 regex_free(rule->arg.http.re);
Christopher Faulet81e20172019-12-12 16:40:30 +01001652 return ACT_RET_PRS_ERR;
Christopher Faulet1337b322020-01-14 14:50:55 +01001653 }
Christopher Faulet81e20172019-12-12 16:40:30 +01001654
1655 free(px->conf.lfs_file);
1656 px->conf.lfs_file = strdup(px->conf.args.file);
1657 px->conf.lfs_line = px->conf.args.line;
1658
1659 *orig_arg = cur_arg + 1;
1660 return ACT_RET_PRS_OK;
1661}
1662
Maciej Zdebebdd4c52020-11-20 13:58:48 +00001663/* This function executes a del-header action with selected matching mode for
1664 * header name. It finds the matching method to be performed in <.action>, previously
1665 * filled by function parse_http_del_header(). On success, it returns ACT_RET_CONT.
1666 * Otherwise ACT_RET_ERR is returned.
1667 */
1668static enum act_return http_action_del_header(struct act_rule *rule, struct proxy *px,
1669 struct session *sess, struct stream *s, int flags)
1670{
1671 struct http_hdr_ctx ctx;
1672 struct http_msg *msg = ((rule->from == ACT_F_HTTP_REQ) ? &s->txn->req : &s->txn->rsp);
1673 struct htx *htx = htxbuf(&msg->chn->buf);
1674 enum act_return ret = ACT_RET_CONT;
1675
1676 /* remove all occurrences of the header */
1677 ctx.blk = NULL;
1678 switch (rule->action) {
1679 case PAT_MATCH_STR:
1680 while (http_find_header(htx, rule->arg.http.str, &ctx, 1))
1681 http_remove_header(htx, &ctx);
1682 break;
1683 case PAT_MATCH_BEG:
1684 while (http_find_pfx_header(htx, rule->arg.http.str, &ctx, 1))
1685 http_remove_header(htx, &ctx);
1686 break;
1687 case PAT_MATCH_END:
1688 while (http_find_sfx_header(htx, rule->arg.http.str, &ctx, 1))
1689 http_remove_header(htx, &ctx);
1690 break;
1691 case PAT_MATCH_SUB:
1692 while (http_find_sub_header(htx, rule->arg.http.str, &ctx, 1))
1693 http_remove_header(htx, &ctx);
1694 break;
1695 case PAT_MATCH_REG:
1696 while (http_match_header(htx, rule->arg.http.re, &ctx, 1))
1697 http_remove_header(htx, &ctx);
1698 break;
1699 default:
1700 return ACT_RET_ERR;
1701 }
1702 return ret;
1703}
1704
1705/* Parse a "del-header" action. It takes string as a required argument,
1706 * optional flag (currently only -m) and optional matching method of input string
1707 * with header name to be deleted. Default matching method is exact match (-m str).
1708 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Christopher Faulet81e20172019-12-12 16:40:30 +01001709 */
1710static enum act_parse_ret parse_http_del_header(const char **args, int *orig_arg, struct proxy *px,
1711 struct act_rule *rule, char **err)
1712{
1713 int cur_arg;
Maciej Zdebebdd4c52020-11-20 13:58:48 +00001714 int pat_idx;
Christopher Faulet81e20172019-12-12 16:40:30 +01001715
Maciej Zdebebdd4c52020-11-20 13:58:48 +00001716 /* set exact matching (-m str) as default */
1717 rule->action = PAT_MATCH_STR;
1718 rule->action_ptr = http_action_del_header;
Christopher Faulet2eb53962020-01-14 14:47:34 +01001719 rule->release_ptr = release_http_action;
Christopher Faulet81e20172019-12-12 16:40:30 +01001720
1721 cur_arg = *orig_arg;
1722 if (!*args[cur_arg]) {
Maciej Zdebebdd4c52020-11-20 13:58:48 +00001723 memprintf(err, "expects at least 1 argument");
Christopher Faulet81e20172019-12-12 16:40:30 +01001724 return ACT_RET_PRS_ERR;
1725 }
1726
Christopher Faulet96bff762019-12-17 13:46:18 +01001727 rule->arg.http.str.ptr = strdup(args[cur_arg]);
1728 rule->arg.http.str.len = strlen(rule->arg.http.str.ptr);
Christopher Faulet81e20172019-12-12 16:40:30 +01001729 px->conf.args.ctx = (rule->from == ACT_F_HTTP_REQ ? ARGC_HRQ : ARGC_HRS);
1730
Maciej Zdeb6dee9962020-11-23 16:03:09 +00001731 LIST_INIT(&rule->arg.http.fmt);
Maciej Zdebebdd4c52020-11-20 13:58:48 +00001732 if (strcmp(args[cur_arg+1], "-m") == 0) {
1733 cur_arg++;
1734 if (!*args[cur_arg+1]) {
1735 memprintf(err, "-m flag expects exactly 1 argument");
1736 return ACT_RET_PRS_ERR;
1737 }
1738
1739 cur_arg++;
1740 pat_idx = pat_find_match_name(args[cur_arg]);
1741 switch (pat_idx) {
1742 case PAT_MATCH_REG:
1743 if (!(rule->arg.http.re = regex_comp(rule->arg.http.str.ptr, 1, 1, err)))
1744 return ACT_RET_PRS_ERR;
1745 /* fall through */
1746 case PAT_MATCH_STR:
1747 case PAT_MATCH_BEG:
1748 case PAT_MATCH_END:
1749 case PAT_MATCH_SUB:
1750 rule->action = pat_idx;
1751 break;
1752 default:
1753 memprintf(err, "-m with unsupported matching method '%s'", args[cur_arg]);
1754 return ACT_RET_PRS_ERR;
1755 }
1756 }
1757
Christopher Faulet81e20172019-12-12 16:40:30 +01001758 *orig_arg = cur_arg + 1;
1759 return ACT_RET_PRS_OK;
1760}
1761
Christopher Faulet2eb53962020-01-14 14:47:34 +01001762/* Release memory allocated by an http redirect action. */
1763static void release_http_redir(struct act_rule *rule)
1764{
1765 struct logformat_node *lf, *lfb;
1766 struct redirect_rule *redir;
1767
1768 redir = rule->arg.redir;
Willy Tarreau2b718102021-04-21 07:32:39 +02001769 LIST_DELETE(&redir->list);
Christopher Faulet2eb53962020-01-14 14:47:34 +01001770 if (redir->cond) {
1771 prune_acl_cond(redir->cond);
1772 free(redir->cond);
1773 }
1774 free(redir->rdr_str);
1775 free(redir->cookie_str);
1776 list_for_each_entry_safe(lf, lfb, &redir->rdr_fmt, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001777 LIST_DELETE(&lf->list);
Christopher Faulet2eb53962020-01-14 14:47:34 +01001778 free(lf);
1779 }
1780 free(redir);
1781}
1782
Christopher Faulet81e20172019-12-12 16:40:30 +01001783/* Parse a "redirect" action. It returns ACT_RET_PRS_OK on success,
1784 * ACT_RET_PRS_ERR on error.
1785 */
1786static enum act_parse_ret parse_http_redirect(const char **args, int *orig_arg, struct proxy *px,
1787 struct act_rule *rule, char **err)
1788{
1789 struct redirect_rule *redir;
1790 int dir, cur_arg;
1791
1792 rule->action = ACT_HTTP_REDIR;
Christopher Faulet245cf792019-12-18 14:58:12 +01001793 rule->flags |= ACT_FLAG_FINAL;
Christopher Faulet2eb53962020-01-14 14:47:34 +01001794 rule->release_ptr = release_http_redir;
Christopher Faulet81e20172019-12-12 16:40:30 +01001795
1796 cur_arg = *orig_arg;
1797
1798 dir = (rule->from == ACT_F_HTTP_REQ ? 0 : 1);
1799 if ((redir = http_parse_redirect_rule(px->conf.args.file, px->conf.args.line, px, &args[cur_arg], err, 1, dir)) == NULL)
1800 return ACT_RET_PRS_ERR;
1801
1802 rule->arg.redir = redir;
1803 rule->cond = redir->cond;
1804 redir->cond = NULL;
1805
1806 /* skip all arguments */
1807 while (*args[cur_arg])
1808 cur_arg++;
1809
1810 *orig_arg = cur_arg;
1811 return ACT_RET_PRS_OK;
1812}
1813
Christopher Faulet046cf442019-12-17 15:45:23 +01001814/* This function executes a add-acl, del-acl, set-map or del-map actions. On
1815 * success, it returns ACT_RET_CONT. Otherwsize ACT_RET_ERR is returned.
1816 */
1817static enum act_return http_action_set_map(struct act_rule *rule, struct proxy *px,
1818 struct session *sess, struct stream *s, int flags)
1819{
1820 struct pat_ref *ref;
1821 struct buffer *key = NULL, *value = NULL;
1822 enum act_return ret = ACT_RET_CONT;
1823
1824 /* collect reference */
1825 ref = pat_ref_lookup(rule->arg.map.ref);
1826 if (!ref)
1827 goto leave;
1828
1829 /* allocate key */
1830 key = alloc_trash_chunk();
1831 if (!key)
1832 goto fail_alloc;
1833
1834 /* collect key */
1835 key->data = build_logline(s, key->area, key->size, &rule->arg.map.key);
1836 key->area[key->data] = '\0';
1837
1838 switch (rule->action) {
1839 case 0: // add-acl
1840 /* add entry only if it does not already exist */
1841 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
1842 if (pat_ref_find_elt(ref, key->area) == NULL)
1843 pat_ref_add(ref, key->area, NULL, NULL);
1844 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
1845 break;
1846
1847 case 1: // set-map
1848 /* allocate value */
1849 value = alloc_trash_chunk();
1850 if (!value)
1851 goto fail_alloc;
1852
1853 /* collect value */
1854 value->data = build_logline(s, value->area, value->size, &rule->arg.map.value);
1855 value->area[value->data] = '\0';
1856
1857 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
1858 if (pat_ref_find_elt(ref, key->area) != NULL) {
1859 /* update entry if it exists */
1860 pat_ref_set(ref, key->area, value->area, NULL);
1861 }
1862 else {
1863 /* insert a new entry */
1864 pat_ref_add(ref, key->area, value->area, NULL);
1865 }
1866 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
1867 break;
1868
1869 case 2: // del-acl
1870 case 3: // del-map
1871 /* returned code: 1=ok, 0=ko */
1872 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
1873 pat_ref_delete(ref, key->area);
1874 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
1875 break;
1876
1877 default:
1878 ret = ACT_RET_ERR;
1879 }
1880
1881
1882 leave:
1883 free_trash_chunk(key);
1884 free_trash_chunk(value);
1885 return ret;
1886
1887 fail_alloc:
1888 if (!(s->flags & SF_ERR_MASK))
1889 s->flags |= SF_ERR_RESOURCE;
1890 ret = ACT_RET_ERR;
1891 goto leave;
1892}
1893
Christopher Faulet2eb53962020-01-14 14:47:34 +01001894/* Release memory allocated by an http map/acl action. */
1895static void release_http_map(struct act_rule *rule)
1896{
1897 struct logformat_node *lf, *lfb;
1898
1899 free(rule->arg.map.ref);
1900 list_for_each_entry_safe(lf, lfb, &rule->arg.map.key, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001901 LIST_DELETE(&lf->list);
Christopher Faulet2eb53962020-01-14 14:47:34 +01001902 release_sample_expr(lf->expr);
1903 free(lf->arg);
1904 free(lf);
1905 }
1906 if (rule->action == 1) {
1907 list_for_each_entry_safe(lf, lfb, &rule->arg.map.value, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001908 LIST_DELETE(&lf->list);
Christopher Faulet2eb53962020-01-14 14:47:34 +01001909 release_sample_expr(lf->expr);
1910 free(lf->arg);
1911 free(lf);
1912 }
1913 }
1914}
1915
Christopher Faulet81e20172019-12-12 16:40:30 +01001916/* Parse a "add-acl", "del-acl", "set-map" or "del-map" actions. It takes one or
Christopher Faulet046cf442019-12-17 15:45:23 +01001917 * two log-format string as argument depending on the action. The action is
1918 * stored in <.action> as an int (0=add-acl, 1=set-map, 2=del-acl,
1919 * 3=del-map). It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Christopher Faulet81e20172019-12-12 16:40:30 +01001920 */
1921static enum act_parse_ret parse_http_set_map(const char **args, int *orig_arg, struct proxy *px,
1922 struct act_rule *rule, char **err)
1923{
1924 int cap, cur_arg;
1925
Christopher Faulet046cf442019-12-17 15:45:23 +01001926 if (args[*orig_arg-1][0] == 'a') // add-acl
1927 rule->action = 0;
1928 else if (args[*orig_arg-1][0] == 's') // set-map
1929 rule->action = 1;
1930 else if (args[*orig_arg-1][4] == 'a') // del-acl
1931 rule->action = 2;
1932 else if (args[*orig_arg-1][4] == 'm') // del-map
1933 rule->action = 3;
1934 else {
1935 memprintf(err, "internal error: unhandled action '%s'", args[0]);
1936 return ACT_RET_PRS_ERR;
1937 }
1938 rule->action_ptr = http_action_set_map;
Christopher Faulet2eb53962020-01-14 14:47:34 +01001939 rule->release_ptr = release_http_map;
Christopher Faulet81e20172019-12-12 16:40:30 +01001940
1941 cur_arg = *orig_arg;
Christopher Faulet046cf442019-12-17 15:45:23 +01001942 if (rule->action == 1 && (!*args[cur_arg] || !*args[cur_arg+1])) {
1943 /* 2 args for set-map */
Christopher Faulet81e20172019-12-12 16:40:30 +01001944 memprintf(err, "expects exactly 2 arguments");
1945 return ACT_RET_PRS_ERR;
1946 }
1947 else if (!*args[cur_arg]) {
Christopher Faulet046cf442019-12-17 15:45:23 +01001948 /* only one arg for other actions */
Christopher Faulet81e20172019-12-12 16:40:30 +01001949 memprintf(err, "expects exactly 1 arguments");
1950 return ACT_RET_PRS_ERR;
1951 }
1952
1953 /*
1954 * '+ 8' for 'set-map(' (same for del-map)
1955 * '- 9' for 'set-map(' + trailing ')' (same for del-map)
1956 */
1957 rule->arg.map.ref = my_strndup(args[cur_arg-1] + 8, strlen(args[cur_arg-1]) - 9);
1958
1959 if (rule->from == ACT_F_HTTP_REQ) {
1960 px->conf.args.ctx = ARGC_HRQ;
1961 cap = (px->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR;
1962 }
1963 else{
1964 px->conf.args.ctx = ARGC_HRS;
1965 cap = (px->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR;
1966 }
1967
1968 /* key pattern */
1969 LIST_INIT(&rule->arg.map.key);
Christopher Faulet1337b322020-01-14 14:50:55 +01001970 if (!parse_logformat_string(args[cur_arg], px, &rule->arg.map.key, LOG_OPT_HTTP, cap, err)) {
1971 free(rule->arg.map.ref);
Christopher Faulet81e20172019-12-12 16:40:30 +01001972 return ACT_RET_PRS_ERR;
Christopher Faulet1337b322020-01-14 14:50:55 +01001973 }
Christopher Faulet81e20172019-12-12 16:40:30 +01001974
Christopher Faulet046cf442019-12-17 15:45:23 +01001975 if (rule->action == 1) {
Christopher Faulet81e20172019-12-12 16:40:30 +01001976 /* value pattern for set-map only */
1977 cur_arg++;
1978 LIST_INIT(&rule->arg.map.value);
Christopher Faulet1337b322020-01-14 14:50:55 +01001979 if (!parse_logformat_string(args[cur_arg], px, &rule->arg.map.value, LOG_OPT_HTTP, cap, err)) {
1980 free(rule->arg.map.ref);
Christopher Faulet81e20172019-12-12 16:40:30 +01001981 return ACT_RET_PRS_ERR;
Christopher Faulet1337b322020-01-14 14:50:55 +01001982 }
Christopher Faulet81e20172019-12-12 16:40:30 +01001983 }
1984
1985 free(px->conf.lfs_file);
1986 px->conf.lfs_file = strdup(px->conf.args.file);
1987 px->conf.lfs_line = px->conf.args.line;
1988
1989 *orig_arg = cur_arg + 1;
1990 return ACT_RET_PRS_OK;
1991}
1992
Christopher Fauletac98d812019-12-18 09:20:16 +01001993/* This function executes a track-sc* actions. On success, it returns
1994 * ACT_RET_CONT. Otherwsize ACT_RET_ERR is returned.
1995 */
1996static enum act_return http_action_track_sc(struct act_rule *rule, struct proxy *px,
1997 struct session *sess, struct stream *s, int flags)
1998{
1999 struct stktable *t;
2000 struct stksess *ts;
2001 struct stktable_key *key;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002002 void *ptr1, *ptr2, *ptr3, *ptr4, *ptr5, *ptr6;
Christopher Fauletac98d812019-12-18 09:20:16 +01002003 int opt;
2004
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002005 ptr1 = ptr2 = ptr3 = ptr4 = ptr5 = ptr6 = NULL;
Christopher Fauletac98d812019-12-18 09:20:16 +01002006 opt = ((rule->from == ACT_F_HTTP_REQ) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES) | SMP_OPT_FINAL;
2007
2008 t = rule->arg.trk_ctr.table.t;
Emeric Brun362d25e2021-03-10 16:58:03 +01002009
2010 if (stkctr_entry(&s->stkctr[rule->action]))
2011 goto end;
2012
Christopher Fauletac98d812019-12-18 09:20:16 +01002013 key = stktable_fetch_key(t, s->be, sess, s, opt, rule->arg.trk_ctr.expr, NULL);
2014
2015 if (!key)
2016 goto end;
2017 ts = stktable_get_entry(t, key);
2018 if (!ts)
2019 goto end;
2020
2021 stream_track_stkctr(&s->stkctr[rule->action], t, ts);
2022
2023 /* let's count a new HTTP request as it's the first time we do it */
2024 ptr1 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
2025 ptr2 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
2026
2027 /* When the client triggers a 4xx from the server, it's most often due
2028 * to a missing object or permission. These events should be tracked
2029 * because if they happen often, it may indicate a brute force or a
2030 * vulnerability scan. Normally this is done when receiving the response
2031 * but here we're tracking after this ought to have been done so we have
2032 * to do it on purpose.
2033 */
2034 if (rule->from == ACT_F_HTTP_RES && (unsigned)(s->txn->status - 400) < 100) {
2035 ptr3 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_CNT);
2036 ptr4 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_RATE);
2037 }
2038
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002039 if (rule->from == ACT_F_HTTP_RES && (unsigned)(s->txn->status - 500) < 100 &&
2040 s->txn->status != 501 && s->txn->status != 505) {
2041 ptr5 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_FAIL_CNT);
2042 ptr6 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_FAIL_RATE);
2043 }
2044
2045 if (ptr1 || ptr2 || ptr3 || ptr4 || ptr5 || ptr6) {
Christopher Fauletac98d812019-12-18 09:20:16 +01002046 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
2047
2048 if (ptr1)
2049 stktable_data_cast(ptr1, http_req_cnt)++;
2050 if (ptr2)
2051 update_freq_ctr_period(&stktable_data_cast(ptr2, http_req_rate),
2052 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
2053 if (ptr3)
2054 stktable_data_cast(ptr3, http_err_cnt)++;
2055 if (ptr4)
2056 update_freq_ctr_period(&stktable_data_cast(ptr4, http_err_rate),
2057 t->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1);
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002058 if (ptr5)
2059 stktable_data_cast(ptr5, http_fail_cnt)++;
2060 if (ptr6)
2061 update_freq_ctr_period(&stktable_data_cast(ptr6, http_fail_rate),
2062 t->data_arg[STKTABLE_DT_HTTP_FAIL_RATE].u, 1);
Christopher Fauletac98d812019-12-18 09:20:16 +01002063
2064 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
2065
2066 /* If data was modified, we need to touch to re-schedule sync */
2067 stktable_touch_local(t, ts, 0);
2068 }
2069
2070 stkctr_set_flags(&s->stkctr[rule->action], STKCTR_TRACK_CONTENT);
2071 if (sess->fe != s->be)
2072 stkctr_set_flags(&s->stkctr[rule->action], STKCTR_TRACK_BACKEND);
2073
2074 end:
2075 return ACT_RET_CONT;
2076}
Christopher Faulet81e20172019-12-12 16:40:30 +01002077
Christopher Faulet2eb53962020-01-14 14:47:34 +01002078static void release_http_track_sc(struct act_rule *rule)
2079{
2080 release_sample_expr(rule->arg.trk_ctr.expr);
2081}
2082
Christopher Faulet81e20172019-12-12 16:40:30 +01002083/* Parse a "track-sc*" actions. It returns ACT_RET_PRS_OK on success,
2084 * ACT_RET_PRS_ERR on error.
2085 */
2086static enum act_parse_ret parse_http_track_sc(const char **args, int *orig_arg, struct proxy *px,
2087 struct act_rule *rule, char **err)
2088{
2089 struct sample_expr *expr;
2090 unsigned int where;
2091 unsigned int tsc_num;
2092 const char *tsc_num_str;
2093 int cur_arg;
2094
2095 tsc_num_str = &args[*orig_arg-1][8];
2096 if (cfg_parse_track_sc_num(&tsc_num, tsc_num_str, tsc_num_str + strlen(tsc_num_str), err) == -1)
2097 return ACT_RET_PRS_ERR;
2098
2099 cur_arg = *orig_arg;
2100 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line,
Willy Tarreaue3b57bf2020-02-14 16:50:14 +01002101 err, &px->conf.args, NULL);
Christopher Faulet81e20172019-12-12 16:40:30 +01002102 if (!expr)
2103 return ACT_RET_PRS_ERR;
2104
2105 where = 0;
2106 if (px->cap & PR_CAP_FE)
2107 where |= (rule->from == ACT_F_HTTP_REQ ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_FE_HRS_HDR);
2108 if (px->cap & PR_CAP_BE)
2109 where |= (rule->from == ACT_F_HTTP_REQ ? SMP_VAL_BE_HRQ_HDR : SMP_VAL_BE_HRS_HDR);
2110
2111 if (!(expr->fetch->val & where)) {
2112 memprintf(err, "fetch method '%s' extracts information from '%s', none of which is available here",
2113 args[cur_arg-1], sample_src_names(expr->fetch->use));
Christopher Faulet1337b322020-01-14 14:50:55 +01002114 release_sample_expr(expr);
Christopher Faulet81e20172019-12-12 16:40:30 +01002115 return ACT_RET_PRS_ERR;
2116 }
2117
2118 if (strcmp(args[cur_arg], "table") == 0) {
2119 cur_arg++;
2120 if (!*args[cur_arg]) {
2121 memprintf(err, "missing table name");
Christopher Faulet1337b322020-01-14 14:50:55 +01002122 release_sample_expr(expr);
Christopher Faulet81e20172019-12-12 16:40:30 +01002123 return ACT_RET_PRS_ERR;
2124 }
2125
2126 /* we copy the table name for now, it will be resolved later */
2127 rule->arg.trk_ctr.table.n = strdup(args[cur_arg]);
2128 cur_arg++;
2129 }
2130
Christopher Fauletac98d812019-12-18 09:20:16 +01002131 rule->action = tsc_num;
Christopher Faulet81e20172019-12-12 16:40:30 +01002132 rule->arg.trk_ctr.expr = expr;
Christopher Fauletac98d812019-12-18 09:20:16 +01002133 rule->action_ptr = http_action_track_sc;
Christopher Faulet2eb53962020-01-14 14:47:34 +01002134 rule->release_ptr = release_http_track_sc;
Christopher Faulet81e20172019-12-12 16:40:30 +01002135 rule->check_ptr = check_trk_action;
2136
2137 *orig_arg = cur_arg;
2138 return ACT_RET_PRS_OK;
2139}
2140
Amaury Denoyelle8d228232020-12-10 13:43:54 +01002141static enum act_return action_timeout_set_stream_timeout(struct act_rule *rule,
2142 struct proxy *px,
2143 struct session *sess,
2144 struct stream *s,
2145 int flags)
2146{
2147 struct sample *key;
2148
2149 if (rule->arg.timeout.expr) {
2150 key = sample_fetch_as_type(px, sess, s, SMP_OPT_FINAL, rule->arg.timeout.expr, SMP_T_SINT);
2151 if (!key)
2152 return ACT_RET_CONT;
2153
2154 stream_set_timeout(s, rule->arg.timeout.type, MS_TO_TICKS(key->data.u.sint));
2155 }
2156 else {
2157 stream_set_timeout(s, rule->arg.timeout.type, MS_TO_TICKS(rule->arg.timeout.value));
2158 }
2159
2160 return ACT_RET_CONT;
2161}
2162
2163/* Parse a "set-timeout" action. Returns ACT_RET_PRS_ERR if parsing error.
2164 */
2165static enum act_parse_ret parse_http_set_timeout(const char **args,
2166 int *orig_arg,
2167 struct proxy *px,
2168 struct act_rule *rule, char **err)
2169{
2170 int cur_arg;
2171
2172 rule->action = ACT_CUSTOM;
2173 rule->action_ptr = action_timeout_set_stream_timeout;
2174 rule->release_ptr = release_timeout_action;
2175
2176 cur_arg = *orig_arg;
2177 if (!*args[cur_arg] || !*args[cur_arg + 1]) {
2178 memprintf(err, "expects exactly 2 arguments");
2179 return ACT_RET_PRS_ERR;
2180 }
2181
2182 if (!(px->cap & PR_CAP_BE)) {
2183 memprintf(err, "proxy '%s' has no backend capability", px->id);
2184 return ACT_RET_PRS_ERR;
2185 }
2186
2187 if (cfg_parse_rule_set_timeout(args, cur_arg,
2188 &rule->arg.timeout.value,
2189 &rule->arg.timeout.type,
2190 &rule->arg.timeout.expr,
2191 err,
2192 px->conf.args.file,
2193 px->conf.args.line, &px->conf.args) == -1) {
2194 return ACT_RET_PRS_ERR;
2195 }
2196
2197 *orig_arg = cur_arg + 2;
2198
2199 return ACT_RET_PRS_OK;
2200}
2201
Christopher Faulet46f95542019-12-20 10:07:22 +01002202/* This function executes a strict-mode actions. On success, it always returns
2203 * ACT_RET_CONT
2204 */
2205static enum act_return http_action_strict_mode(struct act_rule *rule, struct proxy *px,
2206 struct session *sess, struct stream *s, int flags)
2207{
2208 struct http_msg *msg = ((rule->from == ACT_F_HTTP_REQ) ? &s->txn->req : &s->txn->rsp);
2209
2210 if (rule->action == 0) // strict-mode on
2211 msg->flags &= ~HTTP_MSGF_SOFT_RW;
2212 else // strict-mode off
2213 msg->flags |= HTTP_MSGF_SOFT_RW;
2214 return ACT_RET_CONT;
2215}
2216
2217/* Parse a "strict-mode" action. It returns ACT_RET_PRS_OK on success,
2218 * ACT_RET_PRS_ERR on error.
2219 */
2220static enum act_parse_ret parse_http_strict_mode(const char **args, int *orig_arg, struct proxy *px,
2221 struct act_rule *rule, char **err)
2222{
2223 int cur_arg;
2224
Christopher Faulet46f95542019-12-20 10:07:22 +01002225 cur_arg = *orig_arg;
2226 if (!*args[cur_arg]) {
2227 memprintf(err, "expects exactly 1 arguments");
2228 return ACT_RET_PRS_ERR;
2229 }
2230
2231 if (strcasecmp(args[cur_arg], "on") == 0)
2232 rule->action = 0; // strict-mode on
2233 else if (strcasecmp(args[cur_arg], "off") == 0)
2234 rule->action = 1; // strict-mode off
2235 else {
2236 memprintf(err, "Unexpected value '%s'. Only 'on' and 'off' are supported", args[cur_arg]);
2237 return ACT_RET_PRS_ERR;
2238 }
2239 rule->action_ptr = http_action_strict_mode;
2240
2241 *orig_arg = cur_arg + 1;
2242 return ACT_RET_PRS_OK;
2243}
2244
Christopher Faulet24231ab2020-01-24 17:44:23 +01002245/* This function executes a return action. It builds an HTX message from an
2246 * errorfile, an raw file or a log-format string, depending on <.action>
2247 * value. On success, it returns ACT_RET_ABRT. If an error occurs ACT_RET_ERR is
2248 * returned.
2249 */
2250static enum act_return http_action_return(struct act_rule *rule, struct proxy *px,
2251 struct session *sess, struct stream *s, int flags)
2252{
2253 struct channel *req = &s->req;
Christopher Faulet24231ab2020-01-24 17:44:23 +01002254
Christopher Faulet2d36df22021-02-19 11:41:01 +01002255 s->txn->status = rule->arg.http_reply->status;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02002256 if (http_reply_message(s, rule->arg.http_reply) == -1)
2257 return ACT_RET_ERR;
Christopher Faulet24231ab2020-01-24 17:44:23 +01002258
Christopher Faulet24231ab2020-01-24 17:44:23 +01002259 if (rule->from == ACT_F_HTTP_REQ) {
2260 /* let's log the request time */
2261 s->logs.tv_request = now;
2262 req->analysers &= AN_REQ_FLT_END;
2263
2264 if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Willy Tarreau4781b152021-04-06 13:53:36 +02002265 _HA_ATOMIC_INC(&s->sess->fe->fe_counters.intercepted_req);
Christopher Faulet24231ab2020-01-24 17:44:23 +01002266 }
2267
2268 if (!(s->flags & SF_ERR_MASK))
2269 s->flags |= SF_ERR_LOCAL;
2270 if (!(s->flags & SF_FINST_MASK))
2271 s->flags |= ((rule->from == ACT_F_HTTP_REQ) ? SF_FINST_R : SF_FINST_H);
2272
Christopher Faulet0e2ad612020-05-13 16:38:37 +02002273 return ACT_RET_ABRT;
Christopher Faulet24231ab2020-01-24 17:44:23 +01002274}
2275
Christopher Faulet24231ab2020-01-24 17:44:23 +01002276/* Parse a "return" action. It returns ACT_RET_PRS_OK on success,
Christopher Faulet47e791e2020-05-13 14:36:55 +02002277 * ACT_RET_PRS_ERR on error. It relies on http_parse_http_reply() to set
2278 * <.arg.http_reply>.
Christopher Faulet24231ab2020-01-24 17:44:23 +01002279 */
2280static enum act_parse_ret parse_http_return(const char **args, int *orig_arg, struct proxy *px,
2281 struct act_rule *rule, char **err)
2282{
Christopher Faulet47e791e2020-05-13 14:36:55 +02002283 /* Prepare parsing of log-format strings */
2284 px->conf.args.ctx = ((rule->from == ACT_F_HTTP_REQ) ? ARGC_HRQ : ARGC_HRS);
2285 rule->arg.http_reply = http_parse_http_reply(args, orig_arg, px, 200, err);
2286 if (!rule->arg.http_reply)
2287 return ACT_RET_PRS_ERR;
Christopher Faulet24231ab2020-01-24 17:44:23 +01002288
Christopher Fauletba946bf2020-05-13 08:50:07 +02002289 rule->flags |= ACT_FLAG_FINAL;
Christopher Faulet5ff0c642020-05-12 18:33:37 +02002290 rule->action = ACT_CUSTOM;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002291 rule->check_ptr = check_act_http_reply;
Christopher Faulet24231ab2020-01-24 17:44:23 +01002292 rule->action_ptr = http_action_return;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002293 rule->release_ptr = release_act_http_reply;
Christopher Faulet24231ab2020-01-24 17:44:23 +01002294 return ACT_RET_PRS_OK;
Christopher Faulet24231ab2020-01-24 17:44:23 +01002295}
2296
Christopher Faulet021a8e42021-03-29 10:46:38 +02002297
2298
2299/* This function executes a wait-for-body action. It waits for the message
2300 * payload for a max configured time (.arg.p[0]) and eventually for only first
2301 * <arg.p[1]> bytes (0 means no limit). It relies on http_wait_for_msg_body()
2302 * function. it returns ACT_RET_CONT when conditions are met to stop to wait.
2303 * Otherwise ACT_RET_YIELD is returned to wait for more data. ACT_RET_INV is
2304 * returned if a parsing error is raised by lower level and ACT_RET_ERR if an
2305 * internal error occured. Finally ACT_RET_ABRT is returned when a timeout
2306 * occured.
2307 */
2308static enum act_return http_action_wait_for_body(struct act_rule *rule, struct proxy *px,
2309 struct session *sess, struct stream *s, int flags)
2310{
2311 struct channel *chn = ((rule->from == ACT_F_HTTP_REQ) ? &s->req : &s->res);
2312 unsigned int time = (uintptr_t)rule->arg.act.p[0];
2313 unsigned int bytes = (uintptr_t)rule->arg.act.p[1];
2314
2315 switch (http_wait_for_msg_body(s, chn, time, bytes)) {
2316 case HTTP_RULE_RES_CONT:
2317 return ACT_RET_CONT;
2318 case HTTP_RULE_RES_YIELD:
2319 return ACT_RET_YIELD;
2320 case HTTP_RULE_RES_BADREQ:
2321 return ACT_RET_INV;
2322 case HTTP_RULE_RES_ERROR:
2323 return ACT_RET_ERR;
2324 case HTTP_RULE_RES_ABRT:
2325 return ACT_RET_ABRT;
2326 default:
2327 return ACT_RET_ERR;
2328 }
2329}
2330
2331/* Parse a "wait-for-body" action. It returns ACT_RET_PRS_OK on success,
2332 * ACT_RET_PRS_ERR on error.
2333 */
2334static enum act_parse_ret parse_http_wait_for_body(const char **args, int *orig_arg, struct proxy *px,
2335 struct act_rule *rule, char **err)
2336{
2337 int cur_arg;
2338 unsigned int time, bytes;
2339 const char *res;
2340
2341 cur_arg = *orig_arg;
2342 if (!*args[cur_arg]) {
2343 memprintf(err, "expects time <time> [ at-least <bytes> ]");
2344 return ACT_RET_PRS_ERR;
2345 }
2346
2347 time = UINT_MAX; /* To be sure it is set */
2348 bytes = 0; /* Default value, wait all the body */
2349 while (*(args[cur_arg])) {
2350 if (strcmp(args[cur_arg], "time") == 0) {
2351 if (!*args[cur_arg + 1]) {
2352 memprintf(err, "missing argument for '%s'", args[cur_arg]);
2353 return ACT_RET_PRS_ERR;
2354 }
2355 res = parse_time_err(args[cur_arg+1], &time, TIME_UNIT_MS);
2356 if (res == PARSE_TIME_OVER) {
2357 memprintf(err, "time overflow (maximum value is 2147483647 ms or ~24.8 days)");
2358 return ACT_RET_PRS_ERR;
2359 }
2360 if (res == PARSE_TIME_UNDER) {
2361 memprintf(err, "time underflow (minimum non-null value is 1 ms)");
2362 return ACT_RET_PRS_ERR;
2363 }
2364 if (res) {
2365 memprintf(err, "unexpected character '%c'", *res);
2366 return ACT_RET_PRS_ERR;
2367 }
2368 cur_arg++;
2369 }
2370 else if (strcmp(args[cur_arg], "at-least") == 0) {
2371 if (!*args[cur_arg + 1]) {
2372 memprintf(err, "missing argument for '%s'", args[cur_arg]);
2373 return ACT_RET_PRS_ERR;
2374 }
2375 res = parse_size_err(args[cur_arg+1], &bytes);
2376 if (res) {
2377 memprintf(err, "unexpected character '%c'", *res);
2378 return ACT_RET_PRS_ERR;
2379 }
2380 cur_arg++;
2381 }
2382 else
2383 break;
2384 cur_arg++;
2385 }
2386
2387 if (time == UINT_MAX) {
2388 memprintf(err, "expects time <time> [ at-least <bytes> ]");
2389 return ACT_RET_PRS_ERR;
2390 }
2391
2392 rule->arg.act.p[0] = (void *)(uintptr_t)time;
2393 rule->arg.act.p[1] = (void *)(uintptr_t)bytes;
2394
2395 *orig_arg = cur_arg;
2396
2397 rule->action = ACT_CUSTOM;
2398 rule->action_ptr = http_action_wait_for_body;
2399 return ACT_RET_PRS_OK;
2400}
2401
Willy Tarreau79e57332018-10-02 16:01:16 +02002402/************************************************************************/
2403/* All supported http-request action keywords must be declared here. */
2404/************************************************************************/
2405
2406static struct action_kw_list http_req_actions = {
2407 .kw = {
Christopher Faulet81e20172019-12-12 16:40:30 +01002408 { "add-acl", parse_http_set_map, 1 },
2409 { "add-header", parse_http_set_header, 0 },
2410 { "allow", parse_http_allow, 0 },
2411 { "auth", parse_http_auth, 0 },
2412 { "capture", parse_http_req_capture, 0 },
2413 { "del-acl", parse_http_set_map, 1 },
2414 { "del-header", parse_http_del_header, 0 },
2415 { "del-map", parse_http_set_map, 1 },
Christopher Faulete0fca292020-01-13 21:49:03 +01002416 { "deny", parse_http_deny, 0 },
Christopher Faulet81e20172019-12-12 16:40:30 +01002417 { "disable-l7-retry", parse_http_req_disable_l7_retry, 0 },
2418 { "early-hint", parse_http_set_header, 0 },
Tim Duesterhusd2bedcc2021-04-15 21:45:57 +02002419 { "normalize-uri", parse_http_normalize_uri, 0 },
Christopher Faulet81e20172019-12-12 16:40:30 +01002420 { "redirect", parse_http_redirect, 0 },
2421 { "reject", parse_http_action_reject, 0 },
2422 { "replace-header", parse_http_replace_header, 0 },
2423 { "replace-path", parse_replace_uri, 0 },
Christopher Faulet312294f2020-09-02 17:17:44 +02002424 { "replace-pathq", parse_replace_uri, 0 },
Christopher Faulet81e20172019-12-12 16:40:30 +01002425 { "replace-uri", parse_replace_uri, 0 },
2426 { "replace-value", parse_http_replace_header, 0 },
Christopher Faulet24231ab2020-01-24 17:44:23 +01002427 { "return", parse_http_return, 0 },
Christopher Faulet81e20172019-12-12 16:40:30 +01002428 { "set-header", parse_http_set_header, 0 },
2429 { "set-log-level", parse_http_set_log_level, 0 },
2430 { "set-map", parse_http_set_map, 1 },
2431 { "set-method", parse_set_req_line, 0 },
2432 { "set-mark", parse_http_set_mark, 0 },
2433 { "set-nice", parse_http_set_nice, 0 },
2434 { "set-path", parse_set_req_line, 0 },
Christopher Faulet312294f2020-09-02 17:17:44 +02002435 { "set-pathq", parse_set_req_line, 0 },
Christopher Faulet81e20172019-12-12 16:40:30 +01002436 { "set-query", parse_set_req_line, 0 },
2437 { "set-tos", parse_http_set_tos, 0 },
2438 { "set-uri", parse_set_req_line, 0 },
Christopher Faulet46f95542019-12-20 10:07:22 +01002439 { "strict-mode", parse_http_strict_mode, 0 },
Christopher Faulete0fca292020-01-13 21:49:03 +01002440 { "tarpit", parse_http_deny, 0 },
Christopher Faulet81e20172019-12-12 16:40:30 +01002441 { "track-sc", parse_http_track_sc, 1 },
Amaury Denoyelle8d228232020-12-10 13:43:54 +01002442 { "set-timeout", parse_http_set_timeout, 0 },
Christopher Faulet021a8e42021-03-29 10:46:38 +02002443 { "wait-for-body", parse_http_wait_for_body, 0 },
Willy Tarreau79e57332018-10-02 16:01:16 +02002444 { NULL, NULL }
2445 }
2446};
2447
Willy Tarreau0108d902018-11-25 19:14:37 +01002448INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_actions);
2449
Willy Tarreau79e57332018-10-02 16:01:16 +02002450static struct action_kw_list http_res_actions = {
2451 .kw = {
Christopher Faulet81e20172019-12-12 16:40:30 +01002452 { "add-acl", parse_http_set_map, 1 },
2453 { "add-header", parse_http_set_header, 0 },
2454 { "allow", parse_http_allow, 0 },
2455 { "capture", parse_http_res_capture, 0 },
2456 { "del-acl", parse_http_set_map, 1 },
2457 { "del-header", parse_http_del_header, 0 },
2458 { "del-map", parse_http_set_map, 1 },
Christopher Faulete0fca292020-01-13 21:49:03 +01002459 { "deny", parse_http_deny, 0 },
Christopher Faulet81e20172019-12-12 16:40:30 +01002460 { "redirect", parse_http_redirect, 0 },
2461 { "replace-header", parse_http_replace_header, 0 },
2462 { "replace-value", parse_http_replace_header, 0 },
Christopher Faulet24231ab2020-01-24 17:44:23 +01002463 { "return", parse_http_return, 0 },
Christopher Faulet81e20172019-12-12 16:40:30 +01002464 { "set-header", parse_http_set_header, 0 },
2465 { "set-log-level", parse_http_set_log_level, 0 },
2466 { "set-map", parse_http_set_map, 1 },
2467 { "set-mark", parse_http_set_mark, 0 },
2468 { "set-nice", parse_http_set_nice, 0 },
2469 { "set-status", parse_http_set_status, 0 },
2470 { "set-tos", parse_http_set_tos, 0 },
Christopher Faulet46f95542019-12-20 10:07:22 +01002471 { "strict-mode", parse_http_strict_mode, 0 },
Christopher Faulet81e20172019-12-12 16:40:30 +01002472 { "track-sc", parse_http_track_sc, 1 },
Christopher Faulet021a8e42021-03-29 10:46:38 +02002473 { "wait-for-body", parse_http_wait_for_body, 0 },
Willy Tarreau79e57332018-10-02 16:01:16 +02002474 { NULL, NULL }
2475 }
2476};
2477
Willy Tarreau0108d902018-11-25 19:14:37 +01002478INITCALL1(STG_REGISTER, http_res_keywords_register, &http_res_actions);
Willy Tarreau79e57332018-10-02 16:01:16 +02002479
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002480static struct action_kw_list http_after_res_actions = {
2481 .kw = {
2482 { "add-header", parse_http_set_header, 0 },
2483 { "allow", parse_http_allow, 0 },
2484 { "del-header", parse_http_del_header, 0 },
2485 { "replace-header", parse_http_replace_header, 0 },
2486 { "replace-value", parse_http_replace_header, 0 },
2487 { "set-header", parse_http_set_header, 0 },
2488 { "set-status", parse_http_set_status, 0 },
2489 { "strict-mode", parse_http_strict_mode, 0 },
2490 { NULL, NULL }
2491 }
2492};
2493
2494INITCALL1(STG_REGISTER, http_after_res_keywords_register, &http_after_res_actions);
2495
Willy Tarreau79e57332018-10-02 16:01:16 +02002496/*
2497 * Local variables:
2498 * c-indent-level: 8
2499 * c-basic-offset: 8
2500 * End:
2501 */