blob: f61362475993e29d1d8f235febc598bd6dc3b34a [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 Duesterhus2e4a18e2021-04-21 21:20:36 +0200305 case ACT_NORMALIZE_URI_PERCENT_DECODE_UNRESERVED:
306 case ACT_NORMALIZE_URI_PERCENT_DECODE_UNRESERVED_STRICT: {
307 const struct ist path = http_get_path(uri);
308 struct ist newpath = ist2(replace->area, replace->size);
309
310 if (!isttest(path))
311 goto leave;
312
313 err = uri_normalizer_percent_decode_unreserved(path, rule->action == ACT_NORMALIZE_URI_PERCENT_DECODE_UNRESERVED_STRICT, &newpath);
314
315 if (err != URI_NORMALIZER_ERR_NONE)
316 break;
317
318 if (!http_replace_req_path(htx, newpath, 1))
319 goto fail_rewrite;
320
321 break;
322 }
Tim Duesterhusc9e05ab2021-05-10 17:28:25 +0200323 case ACT_NORMALIZE_URI_FRAGMENT_STRIP: {
324 const struct ist path = http_get_path(uri);
325 struct ist newpath = ist2(replace->area, replace->size);
326
327 if (!isttest(path))
328 goto leave;
329
330 err = uri_normalizer_fragment_strip(path, &newpath);
331
332 if (err != URI_NORMALIZER_ERR_NONE)
333 break;
334
335 if (!http_replace_req_path(htx, newpath, 1))
336 goto fail_rewrite;
337
338 break;
339 }
Tim Duesterhusdec1c362021-05-10 17:28:26 +0200340 case ACT_NORMALIZE_URI_FRAGMENT_ENCODE: {
341 const struct ist path = http_get_path(uri);
342 struct ist newpath = ist2(replace->area, replace->size);
343
344 if (!isttest(path))
345 goto leave;
346
347 err = uri_normalizer_fragment_encode(path, &newpath);
348
349 if (err != URI_NORMALIZER_ERR_NONE)
350 break;
351
352 if (!http_replace_req_path(htx, newpath, 1))
353 goto fail_rewrite;
354
355 break;
356 }
Tim Duesterhusd2bedcc2021-04-15 21:45:57 +0200357 }
358
359 switch (err) {
360 case URI_NORMALIZER_ERR_NONE:
361 break;
362 case URI_NORMALIZER_ERR_INTERNAL_ERROR:
363 ret = ACT_RET_ERR;
364 break;
365 case URI_NORMALIZER_ERR_INVALID_INPUT:
366 ret = ACT_RET_INV;
367 break;
368 case URI_NORMALIZER_ERR_ALLOC:
369 goto fail_alloc;
370 }
371
372 leave:
373 free_trash_chunk(replace);
374 return ret;
375
376 fail_alloc:
377 if (!(s->flags & SF_ERR_MASK))
378 s->flags |= SF_ERR_RESOURCE;
379 ret = ACT_RET_ERR;
380 goto leave;
381
382 fail_rewrite:
383 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_rewrites, 1);
384 if (s->flags & SF_BE_ASSIGNED)
385 _HA_ATOMIC_ADD(&s->be->be_counters.failed_rewrites, 1);
386 if (sess->listener && sess->listener->counters)
387 _HA_ATOMIC_ADD(&sess->listener->counters->failed_rewrites, 1);
388 if (objt_server(s->target))
389 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_rewrites, 1);
390
391 if (!(s->txn->req.flags & HTTP_MSGF_SOFT_RW)) {
392 ret = ACT_RET_ERR;
393 if (!(s->flags & SF_ERR_MASK))
394 s->flags |= SF_ERR_PRXCOND;
395 }
396 goto leave;
397}
398
399/* Parses the http-request normalize-uri action. It expects a single <normalizer>
400 * argument, corresponding too a value in `enum act_normalize_uri`.
401 *
402 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
403 */
404static enum act_parse_ret parse_http_normalize_uri(const char **args, int *orig_arg, struct proxy *px,
405 struct act_rule *rule, char **err)
406{
407 int cur_arg = *orig_arg;
408
409 rule->action_ptr = http_action_normalize_uri;
410 rule->release_ptr = NULL;
411
412 if (!*args[cur_arg]) {
413 memprintf(err, "missing argument <normalizer>");
414 return ACT_RET_PRS_ERR;
415 }
416
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200417 if (strcmp(args[cur_arg], "path-merge-slashes") == 0) {
Tim Duesterhusd371e992021-04-15 21:45:58 +0200418 cur_arg++;
Tim Duesterhusd2bedcc2021-04-15 21:45:57 +0200419
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200420 rule->action = ACT_NORMALIZE_URI_PATH_MERGE_SLASHES;
Tim Duesterhusd2bedcc2021-04-15 21:45:57 +0200421 }
Maximilian Maderff3bb8b2021-04-21 00:22:50 +0200422 else if (strcmp(args[cur_arg], "path-strip-dot") == 0) {
423 cur_arg++;
424
425 rule->action = ACT_NORMALIZE_URI_PATH_STRIP_DOT;
426 }
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200427 else if (strcmp(args[cur_arg], "path-strip-dotdot") == 0) {
Tim Duesterhus9982fc22021-04-15 21:45:59 +0200428 cur_arg++;
429
Tim Duesterhus560e1a62021-04-15 21:46:00 +0200430 if (strcmp(args[cur_arg], "full") == 0) {
431 cur_arg++;
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200432 rule->action = ACT_NORMALIZE_URI_PATH_STRIP_DOTDOT_FULL;
Tim Duesterhus560e1a62021-04-15 21:46:00 +0200433 }
434 else if (!*args[cur_arg]) {
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200435 rule->action = ACT_NORMALIZE_URI_PATH_STRIP_DOTDOT;
Tim Duesterhus560e1a62021-04-15 21:46:00 +0200436 }
437 else if (strcmp(args[cur_arg], "if") != 0 && strcmp(args[cur_arg], "unless") != 0) {
Tim Duesterhus2f413132021-05-10 23:21:20 +0200438 memprintf(err, "unknown argument '%s' for 'path-strip-dotdot' normalizer", args[cur_arg]);
Tim Duesterhus560e1a62021-04-15 21:46:00 +0200439 return ACT_RET_PRS_ERR;
440 }
Tim Duesterhus9982fc22021-04-15 21:45:59 +0200441 }
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200442 else if (strcmp(args[cur_arg], "query-sort-by-name") == 0) {
Tim Duesterhusd7b89be2021-04-15 21:46:01 +0200443 cur_arg++;
444
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200445 rule->action = ACT_NORMALIZE_URI_QUERY_SORT_BY_NAME;
Tim Duesterhusd7b89be2021-04-15 21:46:01 +0200446 }
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200447 else if (strcmp(args[cur_arg], "percent-to-uppercase") == 0) {
Tim Duesterhusa4071932021-04-15 21:46:02 +0200448 cur_arg++;
449
450 if (strcmp(args[cur_arg], "strict") == 0) {
451 cur_arg++;
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200452 rule->action = ACT_NORMALIZE_URI_PERCENT_TO_UPPERCASE_STRICT;
Tim Duesterhusa4071932021-04-15 21:46:02 +0200453 }
454 else if (!*args[cur_arg]) {
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200455 rule->action = ACT_NORMALIZE_URI_PERCENT_TO_UPPERCASE;
Tim Duesterhusa4071932021-04-15 21:46:02 +0200456 }
457 else if (strcmp(args[cur_arg], "if") != 0 && strcmp(args[cur_arg], "unless") != 0) {
Tim Duesterhus2f413132021-05-10 23:21:20 +0200458 memprintf(err, "unknown argument '%s' for 'percent-to-uppercase' normalizer", args[cur_arg]);
Tim Duesterhusa4071932021-04-15 21:46:02 +0200459 return ACT_RET_PRS_ERR;
460 }
461 }
Tim Duesterhus2e4a18e2021-04-21 21:20:36 +0200462 else if (strcmp(args[cur_arg], "percent-decode-unreserved") == 0) {
463 cur_arg++;
464
465 if (strcmp(args[cur_arg], "strict") == 0) {
466 cur_arg++;
467 rule->action = ACT_NORMALIZE_URI_PERCENT_DECODE_UNRESERVED_STRICT;
468 }
469 else if (!*args[cur_arg]) {
470 rule->action = ACT_NORMALIZE_URI_PERCENT_DECODE_UNRESERVED;
471 }
472 else if (strcmp(args[cur_arg], "if") != 0 && strcmp(args[cur_arg], "unless") != 0) {
473 memprintf(err, "unknown argument '%s' for 'percent-decode-unreserved' normalizer", args[cur_arg]);
474 return ACT_RET_PRS_ERR;
475 }
476 }
Tim Duesterhusc9e05ab2021-05-10 17:28:25 +0200477 else if (strcmp(args[cur_arg], "fragment-strip") == 0) {
478 cur_arg++;
479
480 rule->action = ACT_NORMALIZE_URI_FRAGMENT_STRIP;
481 }
Tim Duesterhusdec1c362021-05-10 17:28:26 +0200482 else if (strcmp(args[cur_arg], "fragment-encode") == 0) {
483 cur_arg++;
484
485 rule->action = ACT_NORMALIZE_URI_FRAGMENT_ENCODE;
486 }
Tim Duesterhusd2bedcc2021-04-15 21:45:57 +0200487 else {
488 memprintf(err, "unknown normalizer '%s'", args[cur_arg]);
489 return ACT_RET_PRS_ERR;
490 }
491
492 *orig_arg = cur_arg;
493 return ACT_RET_PRS_OK;
494}
495
Willy Tarreau33810222019-06-12 17:44:02 +0200496/* This function executes a replace-uri action. It finds its arguments in
Christopher Faulet96bff762019-12-17 13:46:18 +0100497 * <rule>.arg.http. It builds a string in the trash from the format string
Willy Tarreau33810222019-06-12 17:44:02 +0200498 * previously filled by function parse_replace_uri() and will execute the regex
Christopher Faulet96bff762019-12-17 13:46:18 +0100499 * in <http.re> to replace the URI. It uses the format string present in
Christopher Faulet2c22a692019-12-18 15:39:56 +0100500 * <http.fmt>. The component to act on (path/uri) is taken from <.action> which
Christopher Faulet96bff762019-12-17 13:46:18 +0100501 * contains 1 for the path or 3 for the URI (values used by
502 * http_req_replace_stline()). On success, it returns ACT_RET_CONT. If an error
503 * occurs while soft rewrites are enabled, the action is canceled, but the rule
504 * processing continue. Otherwsize ACT_RET_ERR is returned.
Willy Tarreau33810222019-06-12 17:44:02 +0200505 */
506static enum act_return http_action_replace_uri(struct act_rule *rule, struct proxy *px,
507 struct session *sess, struct stream *s, int flags)
508{
Christopher Faulet13403762019-12-13 09:01:57 +0100509 enum act_return ret = ACT_RET_CONT;
Willy Tarreau33810222019-06-12 17:44:02 +0200510 struct buffer *replace, *output;
511 struct ist uri;
512 int len;
513
514 replace = alloc_trash_chunk();
515 output = alloc_trash_chunk();
516 if (!replace || !output)
Christopher Faulete00d06c2019-12-16 17:18:42 +0100517 goto fail_alloc;
Christopher Faulet12c28b62019-07-15 16:30:24 +0200518 uri = htx_sl_req_uri(http_get_stline(htxbuf(&s->req.buf)));
Willy Tarreau262c3f12019-12-17 06:52:51 +0100519
Christopher Faulet1fa0cc12020-09-02 11:10:38 +0200520 if (rule->action == 1) // replace-path
521 uri = iststop(http_get_path(uri), '?');
Christopher Faulet312294f2020-09-02 17:17:44 +0200522 else if (rule->action == 4) // replace-pathq
523 uri = http_get_path(uri);
Willy Tarreau262c3f12019-12-17 06:52:51 +0100524
Christopher Faulet96bff762019-12-17 13:46:18 +0100525 if (!regex_exec_match2(rule->arg.http.re, uri.ptr, uri.len, MAX_MATCH, pmatch, 0))
Willy Tarreau33810222019-06-12 17:44:02 +0200526 goto leave;
527
Christopher Faulet96bff762019-12-17 13:46:18 +0100528 replace->data = build_logline(s, replace->area, replace->size, &rule->arg.http.fmt);
Willy Tarreau33810222019-06-12 17:44:02 +0200529
530 /* note: uri.ptr doesn't need to be zero-terminated because it will
531 * only be used to pick pmatch references.
532 */
533 len = exp_replace(output->area, output->size, uri.ptr, replace->area, pmatch);
534 if (len == -1)
Christopher Faulete00d06c2019-12-16 17:18:42 +0100535 goto fail_rewrite;
Willy Tarreau33810222019-06-12 17:44:02 +0200536
Christopher Faulet2c22a692019-12-18 15:39:56 +0100537 if (http_req_replace_stline(rule->action, output->area, len, px, s) == -1)
Christopher Faulete00d06c2019-12-16 17:18:42 +0100538 goto fail_rewrite;
Willy Tarreau33810222019-06-12 17:44:02 +0200539
Christopher Faulete00d06c2019-12-16 17:18:42 +0100540 leave:
Willy Tarreau33810222019-06-12 17:44:02 +0200541 free_trash_chunk(output);
542 free_trash_chunk(replace);
543 return ret;
Christopher Faulete00d06c2019-12-16 17:18:42 +0100544
545 fail_alloc:
546 if (!(s->flags & SF_ERR_MASK))
547 s->flags |= SF_ERR_RESOURCE;
548 ret = ACT_RET_ERR;
549 goto leave;
550
551 fail_rewrite:
Willy Tarreau4781b152021-04-06 13:53:36 +0200552 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_rewrites);
Christopher Faulete00d06c2019-12-16 17:18:42 +0100553 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200554 _HA_ATOMIC_INC(&s->be->be_counters.failed_rewrites);
William Lallemand36119de2021-03-08 15:26:48 +0100555 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200556 _HA_ATOMIC_INC(&sess->listener->counters->failed_rewrites);
Christopher Faulete00d06c2019-12-16 17:18:42 +0100557 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +0200558 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_rewrites);
Christopher Faulete00d06c2019-12-16 17:18:42 +0100559
Christopher Faulet333bf8c2020-01-22 14:38:05 +0100560 if (!(s->txn->req.flags & HTTP_MSGF_SOFT_RW)) {
Christopher Faulete00d06c2019-12-16 17:18:42 +0100561 ret = ACT_RET_ERR;
Christopher Faulet333bf8c2020-01-22 14:38:05 +0100562 if (!(s->flags & SF_ERR_MASK))
563 s->flags |= SF_ERR_PRXCOND;
564 }
Christopher Faulete00d06c2019-12-16 17:18:42 +0100565 goto leave;
Willy Tarreau33810222019-06-12 17:44:02 +0200566}
567
Christopher Faulet312294f2020-09-02 17:17:44 +0200568/* parse a "replace-uri", "replace-path" or "replace-pathq"
569 * http-request action.
Willy Tarreau33810222019-06-12 17:44:02 +0200570 * This action takes 2 arguments (a regex and a replacement format string).
Christopher Faulet2c22a692019-12-18 15:39:56 +0100571 * The resulting rule makes use of <.action> to store the action (1/3 for now),
Christopher Faulet96bff762019-12-17 13:46:18 +0100572 * <http.re> to store the compiled regex, and <http.fmt> to store the log-format
Willy Tarreau33810222019-06-12 17:44:02 +0200573 * list head. It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
574 */
575static enum act_parse_ret parse_replace_uri(const char **args, int *orig_arg, struct proxy *px,
576 struct act_rule *rule, char **err)
577{
578 int cur_arg = *orig_arg;
579 char *error = NULL;
580
Christopher Faulet312294f2020-09-02 17:17:44 +0200581 switch (args[0][8]) {
582 case 'p':
583 if (args[0][12] == 'q')
584 rule->action = 4; // replace-pathq, same as set-pathq
585 else
586 rule->action = 1; // replace-path, same as set-path
587 break;
588 case 'u':
Christopher Faulet2c22a692019-12-18 15:39:56 +0100589 rule->action = 3; // replace-uri, same as set-uri
Christopher Faulet312294f2020-09-02 17:17:44 +0200590 break;
591 default:
592 memprintf(err, "internal error: unhandled action '%s'", args[0]);
593 return ACT_RET_PRS_ERR;
594 }
Willy Tarreau262c3f12019-12-17 06:52:51 +0100595
Willy Tarreau33810222019-06-12 17:44:02 +0200596 rule->action_ptr = http_action_replace_uri;
Christopher Faulet2eb53962020-01-14 14:47:34 +0100597 rule->release_ptr = release_http_action;
Willy Tarreau33810222019-06-12 17:44:02 +0200598
599 if (!*args[cur_arg] || !*args[cur_arg+1] ||
600 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
601 memprintf(err, "expects exactly 2 arguments <match-regex> and <replace-format>");
602 return ACT_RET_PRS_ERR;
603 }
604
Christopher Faulet96bff762019-12-17 13:46:18 +0100605 if (!(rule->arg.http.re = regex_comp(args[cur_arg], 1, 1, &error))) {
Willy Tarreau33810222019-06-12 17:44:02 +0200606 memprintf(err, "failed to parse the regex : %s", error);
607 free(error);
608 return ACT_RET_PRS_ERR;
609 }
610
Christopher Faulet96bff762019-12-17 13:46:18 +0100611 LIST_INIT(&rule->arg.http.fmt);
Willy Tarreau33810222019-06-12 17:44:02 +0200612 px->conf.args.ctx = ARGC_HRQ;
Christopher Faulet96bff762019-12-17 13:46:18 +0100613 if (!parse_logformat_string(args[cur_arg + 1], px, &rule->arg.http.fmt, LOG_OPT_HTTP,
Willy Tarreau33810222019-06-12 17:44:02 +0200614 (px->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, err)) {
Christopher Faulet1337b322020-01-14 14:50:55 +0100615 regex_free(rule->arg.http.re);
Willy Tarreau33810222019-06-12 17:44:02 +0200616 return ACT_RET_PRS_ERR;
617 }
618
619 (*orig_arg) += 2;
620 return ACT_RET_PRS_OK;
621}
622
Willy Tarreau79e57332018-10-02 16:01:16 +0200623/* This function is just a compliant action wrapper for "set-status". */
624static enum act_return action_http_set_status(struct act_rule *rule, struct proxy *px,
625 struct session *sess, struct stream *s, int flags)
626{
Christopher Faulet96bff762019-12-17 13:46:18 +0100627 if (http_res_set_status(rule->arg.http.i, rule->arg.http.str, s) == -1) {
Willy Tarreau4781b152021-04-06 13:53:36 +0200628 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_rewrites);
Christopher Faulete00d06c2019-12-16 17:18:42 +0100629 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200630 _HA_ATOMIC_INC(&s->be->be_counters.failed_rewrites);
William Lallemand36119de2021-03-08 15:26:48 +0100631 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200632 _HA_ATOMIC_INC(&sess->listener->counters->failed_rewrites);
Christopher Faulete00d06c2019-12-16 17:18:42 +0100633 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +0200634 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_rewrites);
Christopher Faulete00d06c2019-12-16 17:18:42 +0100635
Christopher Faulet333bf8c2020-01-22 14:38:05 +0100636 if (!(s->txn->req.flags & HTTP_MSGF_SOFT_RW)) {
Christopher Faulet333bf8c2020-01-22 14:38:05 +0100637 if (!(s->flags & SF_ERR_MASK))
638 s->flags |= SF_ERR_PRXCOND;
Christopher Faulet692a6c22020-02-07 10:22:31 +0100639 return ACT_RET_ERR;
Christopher Faulet333bf8c2020-01-22 14:38:05 +0100640 }
Christopher Faulete00d06c2019-12-16 17:18:42 +0100641 }
642
Willy Tarreau79e57332018-10-02 16:01:16 +0200643 return ACT_RET_CONT;
644}
645
646/* parse set-status action:
647 * This action accepts a single argument of type int representing
648 * an http status code. It returns ACT_RET_PRS_OK on success,
649 * ACT_RET_PRS_ERR on error.
650 */
651static enum act_parse_ret parse_http_set_status(const char **args, int *orig_arg, struct proxy *px,
652 struct act_rule *rule, char **err)
653{
654 char *error;
655
656 rule->action = ACT_CUSTOM;
657 rule->action_ptr = action_http_set_status;
Christopher Faulet2eb53962020-01-14 14:47:34 +0100658 rule->release_ptr = release_http_action;
Willy Tarreau79e57332018-10-02 16:01:16 +0200659
660 /* Check if an argument is available */
661 if (!*args[*orig_arg]) {
662 memprintf(err, "expects 1 argument: <status>; or 3 arguments: <status> reason <fmt>");
663 return ACT_RET_PRS_ERR;
664 }
665
666 /* convert status code as integer */
Christopher Faulet96bff762019-12-17 13:46:18 +0100667 rule->arg.http.i = strtol(args[*orig_arg], &error, 10);
668 if (*error != '\0' || rule->arg.http.i < 100 || rule->arg.http.i > 999) {
Willy Tarreau79e57332018-10-02 16:01:16 +0200669 memprintf(err, "expects an integer status code between 100 and 999");
670 return ACT_RET_PRS_ERR;
671 }
672
673 (*orig_arg)++;
674
675 /* set custom reason string */
Christopher Faulet96bff762019-12-17 13:46:18 +0100676 rule->arg.http.str = ist(NULL); // If null, we use the default reason for the status code.
Willy Tarreau79e57332018-10-02 16:01:16 +0200677 if (*args[*orig_arg] && strcmp(args[*orig_arg], "reason") == 0 &&
678 (*args[*orig_arg + 1] && strcmp(args[*orig_arg + 1], "if") != 0 && strcmp(args[*orig_arg + 1], "unless") != 0)) {
679 (*orig_arg)++;
Christopher Faulet96bff762019-12-17 13:46:18 +0100680 rule->arg.http.str.ptr = strdup(args[*orig_arg]);
681 rule->arg.http.str.len = strlen(rule->arg.http.str.ptr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200682 (*orig_arg)++;
683 }
684
Christopher Fauletc20b3712020-01-27 15:51:56 +0100685 LIST_INIT(&rule->arg.http.fmt);
Willy Tarreau79e57332018-10-02 16:01:16 +0200686 return ACT_RET_PRS_OK;
687}
688
689/* This function executes the "reject" HTTP action. It clears the request and
690 * response buffer without sending any response. It can be useful as an HTTP
691 * alternative to the silent-drop action to defend against DoS attacks, and may
692 * also be used with HTTP/2 to close a connection instead of just a stream.
693 * The txn status is unchanged, indicating no response was sent. The termination
Christopher Faulet90d22a82020-03-06 11:18:39 +0100694 * flags will indicate "PR". It always returns ACT_RET_ABRT.
Willy Tarreau79e57332018-10-02 16:01:16 +0200695 */
696static enum act_return http_action_reject(struct act_rule *rule, struct proxy *px,
697 struct session *sess, struct stream *s, int flags)
698{
Willy Tarreau0f9cd7b2019-01-31 19:02:43 +0100699 si_must_kill_conn(chn_prod(&s->req));
Willy Tarreau79e57332018-10-02 16:01:16 +0200700 channel_abort(&s->req);
701 channel_abort(&s->res);
Christopher Fauletd4a824e2020-03-06 15:07:09 +0100702 s->req.analysers &= AN_REQ_FLT_END;
703 s->res.analysers &= AN_RES_FLT_END;
Willy Tarreau79e57332018-10-02 16:01:16 +0200704
Willy Tarreau4781b152021-04-06 13:53:36 +0200705 _HA_ATOMIC_INC(&s->be->be_counters.denied_req);
706 _HA_ATOMIC_INC(&sess->fe->fe_counters.denied_req);
Willy Tarreau79e57332018-10-02 16:01:16 +0200707 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200708 _HA_ATOMIC_INC(&sess->listener->counters->denied_req);
Willy Tarreau79e57332018-10-02 16:01:16 +0200709
710 if (!(s->flags & SF_ERR_MASK))
711 s->flags |= SF_ERR_PRXCOND;
712 if (!(s->flags & SF_FINST_MASK))
713 s->flags |= SF_FINST_R;
714
Christopher Faulet90d22a82020-03-06 11:18:39 +0100715 return ACT_RET_ABRT;
Willy Tarreau79e57332018-10-02 16:01:16 +0200716}
717
718/* parse the "reject" action:
719 * This action takes no argument and returns ACT_RET_PRS_OK on success,
720 * ACT_RET_PRS_ERR on error.
721 */
722static enum act_parse_ret parse_http_action_reject(const char **args, int *orig_arg, struct proxy *px,
723 struct act_rule *rule, char **err)
724{
725 rule->action = ACT_CUSTOM;
726 rule->action_ptr = http_action_reject;
727 return ACT_RET_PRS_OK;
728}
729
Olivier Houchard602bf7d2019-05-10 13:59:15 +0200730/* This function executes the "disable-l7-retry" HTTP action.
731 * It disables L7 retries (all retry except for a connection failure). This
732 * can be useful for example to avoid retrying on POST requests.
733 * It just removes the L7 retry flag on the stream_interface, and always
734 * return ACT_RET_CONT;
735 */
736static enum act_return http_req_disable_l7_retry(struct act_rule *rule, struct proxy *px,
737 struct session *sess, struct stream *s, int flags)
738{
739 struct stream_interface *si = &s->si[1];
740
741 /* In theory, the SI_FL_L7_RETRY flags isn't set at this point, but
742 * let's be future-proof and remove it anyway.
743 */
744 si->flags &= ~SI_FL_L7_RETRY;
745 si->flags |= SI_FL_D_L7_RETRY;
746 return ACT_RET_CONT;
747}
748
749/* parse the "disable-l7-retry" action:
750 * This action takes no argument and returns ACT_RET_PRS_OK on success,
751 * ACT_RET_PRS_ERR on error.
752 */
753static enum act_parse_ret parse_http_req_disable_l7_retry(const char **args,
754 int *orig_args, struct proxy *px,
755 struct act_rule *rule, char **err)
756{
757 rule->action = ACT_CUSTOM;
758 rule->action_ptr = http_req_disable_l7_retry;
759 return ACT_RET_PRS_OK;
760}
761
Willy Tarreau79e57332018-10-02 16:01:16 +0200762/* This function executes the "capture" action. It executes a fetch expression,
763 * turns the result into a string and puts it in a capture slot. It always
764 * returns 1. If an error occurs the action is cancelled, but the rule
765 * processing continues.
766 */
767static enum act_return http_action_req_capture(struct act_rule *rule, struct proxy *px,
768 struct session *sess, struct stream *s, int flags)
769{
770 struct sample *key;
771 struct cap_hdr *h = rule->arg.cap.hdr;
772 char **cap = s->req_cap;
773 int len;
774
775 key = sample_fetch_as_type(s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.cap.expr, SMP_T_STR);
776 if (!key)
777 return ACT_RET_CONT;
778
779 if (cap[h->index] == NULL)
780 cap[h->index] = pool_alloc(h->pool);
781
782 if (cap[h->index] == NULL) /* no more capture memory */
783 return ACT_RET_CONT;
784
785 len = key->data.u.str.data;
786 if (len > h->len)
787 len = h->len;
788
789 memcpy(cap[h->index], key->data.u.str.area, len);
790 cap[h->index][len] = 0;
791 return ACT_RET_CONT;
792}
793
794/* This function executes the "capture" action and store the result in a
795 * capture slot if exists. It executes a fetch expression, turns the result
796 * into a string and puts it in a capture slot. It always returns 1. If an
797 * error occurs the action is cancelled, but the rule processing continues.
798 */
799static enum act_return http_action_req_capture_by_id(struct act_rule *rule, struct proxy *px,
800 struct session *sess, struct stream *s, int flags)
801{
802 struct sample *key;
803 struct cap_hdr *h;
804 char **cap = s->req_cap;
805 struct proxy *fe = strm_fe(s);
806 int len;
807 int i;
808
809 /* Look for the original configuration. */
810 for (h = fe->req_cap, i = fe->nb_req_cap - 1;
811 h != NULL && i != rule->arg.capid.idx ;
812 i--, h = h->next);
813 if (!h)
814 return ACT_RET_CONT;
815
816 key = sample_fetch_as_type(s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.capid.expr, SMP_T_STR);
817 if (!key)
818 return ACT_RET_CONT;
819
820 if (cap[h->index] == NULL)
821 cap[h->index] = pool_alloc(h->pool);
822
823 if (cap[h->index] == NULL) /* no more capture memory */
824 return ACT_RET_CONT;
825
826 len = key->data.u.str.data;
827 if (len > h->len)
828 len = h->len;
829
830 memcpy(cap[h->index], key->data.u.str.area, len);
831 cap[h->index][len] = 0;
832 return ACT_RET_CONT;
833}
834
835/* Check an "http-request capture" action.
836 *
837 * The function returns 1 in success case, otherwise, it returns 0 and err is
838 * filled.
839 */
840static int check_http_req_capture(struct act_rule *rule, struct proxy *px, char **err)
841{
842 if (rule->action_ptr != http_action_req_capture_by_id)
843 return 1;
844
Baptiste Assmann19a69b32020-01-16 14:34:22 +0100845 /* capture slots can only be declared in frontends, so we can't check their
846 * existence in backends at configuration parsing step
847 */
848 if (px->cap & PR_CAP_FE && rule->arg.capid.idx >= px->nb_req_cap) {
Willy Tarreau79e57332018-10-02 16:01:16 +0200849 memprintf(err, "unable to find capture id '%d' referenced by http-request capture rule",
850 rule->arg.capid.idx);
851 return 0;
852 }
853
854 return 1;
855}
856
Christopher Faulet2eb53962020-01-14 14:47:34 +0100857/* Release memory allocate by an http capture action */
858static void release_http_capture(struct act_rule *rule)
859{
860 if (rule->action_ptr == http_action_req_capture)
861 release_sample_expr(rule->arg.cap.expr);
862 else
863 release_sample_expr(rule->arg.capid.expr);
864}
865
Willy Tarreau79e57332018-10-02 16:01:16 +0200866/* parse an "http-request capture" action. It takes a single argument which is
867 * a sample fetch expression. It stores the expression into arg->act.p[0] and
868 * the allocated hdr_cap struct or the preallocated "id" into arg->act.p[1].
869 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
870 */
871static enum act_parse_ret parse_http_req_capture(const char **args, int *orig_arg, struct proxy *px,
872 struct act_rule *rule, char **err)
873{
874 struct sample_expr *expr;
875 struct cap_hdr *hdr;
876 int cur_arg;
877 int len = 0;
878
879 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
880 if (strcmp(args[cur_arg], "if") == 0 ||
881 strcmp(args[cur_arg], "unless") == 0)
882 break;
883
884 if (cur_arg < *orig_arg + 3) {
885 memprintf(err, "expects <expression> [ 'len' <length> | id <idx> ]");
886 return ACT_RET_PRS_ERR;
887 }
888
889 cur_arg = *orig_arg;
Willy Tarreaue3b57bf2020-02-14 16:50:14 +0100890 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 +0200891 if (!expr)
892 return ACT_RET_PRS_ERR;
893
894 if (!(expr->fetch->val & SMP_VAL_FE_HRQ_HDR)) {
895 memprintf(err,
896 "fetch method '%s' extracts information from '%s', none of which is available here",
897 args[cur_arg-1], sample_src_names(expr->fetch->use));
Christopher Faulet1337b322020-01-14 14:50:55 +0100898 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200899 return ACT_RET_PRS_ERR;
900 }
901
902 if (!args[cur_arg] || !*args[cur_arg]) {
903 memprintf(err, "expects 'len or 'id'");
Christopher Faulet1337b322020-01-14 14:50:55 +0100904 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200905 return ACT_RET_PRS_ERR;
906 }
907
908 if (strcmp(args[cur_arg], "len") == 0) {
909 cur_arg++;
910
911 if (!(px->cap & PR_CAP_FE)) {
912 memprintf(err, "proxy '%s' has no frontend capability", px->id);
Christopher Faulet1337b322020-01-14 14:50:55 +0100913 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200914 return ACT_RET_PRS_ERR;
915 }
916
917 px->conf.args.ctx = ARGC_CAP;
918
919 if (!args[cur_arg]) {
920 memprintf(err, "missing length value");
Christopher Faulet1337b322020-01-14 14:50:55 +0100921 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200922 return ACT_RET_PRS_ERR;
923 }
924 /* we copy the table name for now, it will be resolved later */
925 len = atoi(args[cur_arg]);
926 if (len <= 0) {
927 memprintf(err, "length must be > 0");
Christopher Faulet1337b322020-01-14 14:50:55 +0100928 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200929 return ACT_RET_PRS_ERR;
930 }
931 cur_arg++;
932
Willy Tarreau79e57332018-10-02 16:01:16 +0200933 hdr = calloc(1, sizeof(*hdr));
934 hdr->next = px->req_cap;
935 hdr->name = NULL; /* not a header capture */
936 hdr->namelen = 0;
937 hdr->len = len;
938 hdr->pool = create_pool("caphdr", hdr->len + 1, MEM_F_SHARED);
939 hdr->index = px->nb_req_cap++;
940
941 px->req_cap = hdr;
942 px->to_log |= LW_REQHDR;
943
944 rule->action = ACT_CUSTOM;
945 rule->action_ptr = http_action_req_capture;
Christopher Faulet2eb53962020-01-14 14:47:34 +0100946 rule->release_ptr = release_http_capture;
Willy Tarreau79e57332018-10-02 16:01:16 +0200947 rule->arg.cap.expr = expr;
948 rule->arg.cap.hdr = hdr;
949 }
950
951 else if (strcmp(args[cur_arg], "id") == 0) {
952 int id;
953 char *error;
954
955 cur_arg++;
956
957 if (!args[cur_arg]) {
958 memprintf(err, "missing id value");
Christopher Faulet1337b322020-01-14 14:50:55 +0100959 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200960 return ACT_RET_PRS_ERR;
961 }
962
963 id = strtol(args[cur_arg], &error, 10);
964 if (*error != '\0') {
965 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
Christopher Faulet1337b322020-01-14 14:50:55 +0100966 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200967 return ACT_RET_PRS_ERR;
968 }
969 cur_arg++;
970
971 px->conf.args.ctx = ARGC_CAP;
972
973 rule->action = ACT_CUSTOM;
974 rule->action_ptr = http_action_req_capture_by_id;
975 rule->check_ptr = check_http_req_capture;
Christopher Faulet2eb53962020-01-14 14:47:34 +0100976 rule->release_ptr = release_http_capture;
Willy Tarreau79e57332018-10-02 16:01:16 +0200977 rule->arg.capid.expr = expr;
978 rule->arg.capid.idx = id;
979 }
980
981 else {
982 memprintf(err, "expects 'len' or 'id', found '%s'", args[cur_arg]);
Christopher Faulet1337b322020-01-14 14:50:55 +0100983 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200984 return ACT_RET_PRS_ERR;
985 }
986
987 *orig_arg = cur_arg;
988 return ACT_RET_PRS_OK;
989}
990
991/* This function executes the "capture" action and store the result in a
992 * capture slot if exists. It executes a fetch expression, turns the result
993 * into a string and puts it in a capture slot. It always returns 1. If an
994 * error occurs the action is cancelled, but the rule processing continues.
995 */
996static enum act_return http_action_res_capture_by_id(struct act_rule *rule, struct proxy *px,
997 struct session *sess, struct stream *s, int flags)
998{
999 struct sample *key;
1000 struct cap_hdr *h;
1001 char **cap = s->res_cap;
1002 struct proxy *fe = strm_fe(s);
1003 int len;
1004 int i;
1005
1006 /* Look for the original configuration. */
1007 for (h = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
1008 h != NULL && i != rule->arg.capid.idx ;
1009 i--, h = h->next);
1010 if (!h)
1011 return ACT_RET_CONT;
1012
1013 key = sample_fetch_as_type(s->be, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL, rule->arg.capid.expr, SMP_T_STR);
1014 if (!key)
1015 return ACT_RET_CONT;
1016
1017 if (cap[h->index] == NULL)
1018 cap[h->index] = pool_alloc(h->pool);
1019
1020 if (cap[h->index] == NULL) /* no more capture memory */
1021 return ACT_RET_CONT;
1022
1023 len = key->data.u.str.data;
1024 if (len > h->len)
1025 len = h->len;
1026
1027 memcpy(cap[h->index], key->data.u.str.area, len);
1028 cap[h->index][len] = 0;
1029 return ACT_RET_CONT;
1030}
1031
1032/* Check an "http-response capture" action.
1033 *
1034 * The function returns 1 in success case, otherwise, it returns 0 and err is
1035 * filled.
1036 */
1037static int check_http_res_capture(struct act_rule *rule, struct proxy *px, char **err)
1038{
1039 if (rule->action_ptr != http_action_res_capture_by_id)
1040 return 1;
1041
Tim Duesterhusf3f4aa02020-07-03 13:43:42 +02001042 /* capture slots can only be declared in frontends, so we can't check their
1043 * existence in backends at configuration parsing step
1044 */
1045 if (px->cap & PR_CAP_FE && rule->arg.capid.idx >= px->nb_rsp_cap) {
Willy Tarreau79e57332018-10-02 16:01:16 +02001046 memprintf(err, "unable to find capture id '%d' referenced by http-response capture rule",
1047 rule->arg.capid.idx);
1048 return 0;
1049 }
1050
1051 return 1;
1052}
1053
1054/* parse an "http-response capture" action. It takes a single argument which is
1055 * a sample fetch expression. It stores the expression into arg->act.p[0] and
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -07001056 * the allocated hdr_cap struct of the preallocated id into arg->act.p[1].
Willy Tarreau79e57332018-10-02 16:01:16 +02001057 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
1058 */
1059static enum act_parse_ret parse_http_res_capture(const char **args, int *orig_arg, struct proxy *px,
1060 struct act_rule *rule, char **err)
1061{
1062 struct sample_expr *expr;
1063 int cur_arg;
1064 int id;
1065 char *error;
1066
1067 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
1068 if (strcmp(args[cur_arg], "if") == 0 ||
1069 strcmp(args[cur_arg], "unless") == 0)
1070 break;
1071
1072 if (cur_arg < *orig_arg + 3) {
1073 memprintf(err, "expects <expression> id <idx>");
1074 return ACT_RET_PRS_ERR;
1075 }
1076
1077 cur_arg = *orig_arg;
Willy Tarreaue3b57bf2020-02-14 16:50:14 +01001078 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 +02001079 if (!expr)
1080 return ACT_RET_PRS_ERR;
1081
1082 if (!(expr->fetch->val & SMP_VAL_FE_HRS_HDR)) {
1083 memprintf(err,
1084 "fetch method '%s' extracts information from '%s', none of which is available here",
1085 args[cur_arg-1], sample_src_names(expr->fetch->use));
Christopher Faulet1337b322020-01-14 14:50:55 +01001086 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +02001087 return ACT_RET_PRS_ERR;
1088 }
1089
1090 if (!args[cur_arg] || !*args[cur_arg]) {
1091 memprintf(err, "expects 'id'");
Christopher Faulet1337b322020-01-14 14:50:55 +01001092 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +02001093 return ACT_RET_PRS_ERR;
1094 }
1095
1096 if (strcmp(args[cur_arg], "id") != 0) {
1097 memprintf(err, "expects 'id', found '%s'", args[cur_arg]);
Christopher Faulet1337b322020-01-14 14:50:55 +01001098 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +02001099 return ACT_RET_PRS_ERR;
1100 }
1101
1102 cur_arg++;
1103
1104 if (!args[cur_arg]) {
1105 memprintf(err, "missing id value");
Christopher Faulet1337b322020-01-14 14:50:55 +01001106 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +02001107 return ACT_RET_PRS_ERR;
1108 }
1109
1110 id = strtol(args[cur_arg], &error, 10);
1111 if (*error != '\0') {
1112 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
Christopher Faulet1337b322020-01-14 14:50:55 +01001113 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +02001114 return ACT_RET_PRS_ERR;
1115 }
1116 cur_arg++;
1117
1118 px->conf.args.ctx = ARGC_CAP;
1119
1120 rule->action = ACT_CUSTOM;
1121 rule->action_ptr = http_action_res_capture_by_id;
1122 rule->check_ptr = check_http_res_capture;
Christopher Faulet2eb53962020-01-14 14:47:34 +01001123 rule->release_ptr = release_http_capture;
Willy Tarreau79e57332018-10-02 16:01:16 +02001124 rule->arg.capid.expr = expr;
1125 rule->arg.capid.idx = id;
1126
1127 *orig_arg = cur_arg;
1128 return ACT_RET_PRS_OK;
1129}
1130
Christopher Faulet81e20172019-12-12 16:40:30 +01001131/* Parse a "allow" action for a request or a response rule. It takes no argument. It
1132 * returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
1133 */
1134static enum act_parse_ret parse_http_allow(const char **args, int *orig_arg, struct proxy *px,
1135 struct act_rule *rule, char **err)
1136{
1137 rule->action = ACT_ACTION_ALLOW;
Christopher Faulet245cf792019-12-18 14:58:12 +01001138 rule->flags |= ACT_FLAG_FINAL;
Christopher Faulet81e20172019-12-12 16:40:30 +01001139 return ACT_RET_PRS_OK;
1140}
1141
Christopher Faulete0fca292020-01-13 21:49:03 +01001142/* Parse "deny" or "tarpit" actions for a request rule or "deny" action for a
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001143 * response rule. It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on
1144 * error. It relies on http_parse_http_reply() to set
1145 * <.arg.http_reply>.
Christopher Faulet81e20172019-12-12 16:40:30 +01001146 */
Christopher Faulete0fca292020-01-13 21:49:03 +01001147static enum act_parse_ret parse_http_deny(const char **args, int *orig_arg, struct proxy *px,
1148 struct act_rule *rule, char **err)
Christopher Faulet81e20172019-12-12 16:40:30 +01001149{
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001150 int default_status;
1151 int cur_arg, arg = 0;
Christopher Faulet81e20172019-12-12 16:40:30 +01001152
1153 cur_arg = *orig_arg;
Christopher Faulete0fca292020-01-13 21:49:03 +01001154 if (rule->from == ACT_F_HTTP_REQ) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001155 if (strcmp(args[cur_arg - 1], "tarpit") == 0) {
Christopher Faulete0fca292020-01-13 21:49:03 +01001156 rule->action = ACT_HTTP_REQ_TARPIT;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001157 default_status = 500;
Christopher Faulet81e20172019-12-12 16:40:30 +01001158 }
Christopher Faulete0fca292020-01-13 21:49:03 +01001159 else {
1160 rule->action = ACT_ACTION_DENY;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001161 default_status = 403;
Christopher Faulet81e20172019-12-12 16:40:30 +01001162 }
Christopher Faulet81e20172019-12-12 16:40:30 +01001163 }
Christopher Faulete0fca292020-01-13 21:49:03 +01001164 else {
Christopher Faulet554c0eb2020-01-14 12:00:28 +01001165 rule->action = ACT_ACTION_DENY;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001166 default_status = 502;
Christopher Faulete0fca292020-01-13 21:49:03 +01001167 }
Christopher Faulet040c8cd2020-01-13 16:43:45 +01001168
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001169 /* If no args or only a deny_status specified, fallback on the legacy
1170 * mode and use default error files despite the fact that
1171 * default-errorfiles is not used. Otherwise, parse an http reply.
1172 */
Christopher Faulet040c8cd2020-01-13 16:43:45 +01001173
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001174 /* Prepare parsing of log-format strings */
1175 px->conf.args.ctx = ((rule->from == ACT_F_HTTP_REQ) ? ARGC_HRQ : ARGC_HRS);
Christopher Faulet554c0eb2020-01-14 12:00:28 +01001176
Christopher Faulet9467f182020-06-30 09:32:01 +02001177 if (!*(args[cur_arg]) || strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001178 rule->arg.http_reply = http_parse_http_reply((const char *[]){"default-errorfiles", ""}, &arg, px, default_status, err);
1179 goto end;
Christopher Faulet554c0eb2020-01-14 12:00:28 +01001180 }
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001181
1182 if (strcmp(args[cur_arg], "deny_status") == 0) {
Christopher Faulet9467f182020-06-30 09:32:01 +02001183 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 +02001184 rule->arg.http_reply = http_parse_http_reply((const char *[]){"status", args[cur_arg+1], "default-errorfiles", ""},
1185 &arg, px, default_status, err);
1186 *orig_arg += 2;
1187 goto end;
Christopher Faulet554c0eb2020-01-14 12:00:28 +01001188 }
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001189 args[cur_arg] += 5; /* skip "deny_" for the parsing */
Christopher Faulet554c0eb2020-01-14 12:00:28 +01001190 }
1191
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001192 rule->arg.http_reply = http_parse_http_reply(args, orig_arg, px, default_status, err);
Christopher Faulet554c0eb2020-01-14 12:00:28 +01001193
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001194 end:
1195 if (!rule->arg.http_reply)
1196 return ACT_RET_PRS_ERR;
1197
1198 rule->flags |= ACT_FLAG_FINAL;
1199 rule->check_ptr = check_act_http_reply;
1200 rule->release_ptr = release_act_http_reply;
Christopher Faulet81e20172019-12-12 16:40:30 +01001201 return ACT_RET_PRS_OK;
1202}
1203
Christopher Fauletb3048832020-05-27 15:26:43 +02001204
1205/* This function executes a auth action. It builds an 401/407 HTX message using
1206 * the corresponding proxy's error message. On success, it returns
1207 * ACT_RET_ABRT. If an error occurs ACT_RET_ERR is returned.
1208 */
1209static enum act_return http_action_auth(struct act_rule *rule, struct proxy *px,
1210 struct session *sess, struct stream *s, int flags)
1211{
1212 struct channel *req = &s->req;
1213 struct channel *res = &s->res;
1214 struct htx *htx = htx_from_buf(&res->buf);
1215 struct http_reply *reply;
1216 const char *auth_realm;
1217 struct http_hdr_ctx ctx;
1218 struct ist hdr;
1219
1220 /* Auth might be performed on regular http-req rules as well as on stats */
1221 auth_realm = rule->arg.http.str.ptr;
1222 if (!auth_realm) {
1223 if (px->uri_auth && s->current_rule_list == &px->uri_auth->http_req_rules)
1224 auth_realm = STATS_DEFAULT_REALM;
1225 else
1226 auth_realm = px->id;
1227 }
1228
1229 if (!(s->txn->flags & TX_USE_PX_CONN)) {
1230 s->txn->status = 401;
1231 hdr = ist("WWW-Authenticate");
1232 }
1233 else {
1234 s->txn->status = 407;
1235 hdr = ist("Proxy-Authenticate");
1236 }
1237 reply = http_error_message(s);
1238 channel_htx_truncate(res, htx);
1239
1240 if (chunk_printf(&trash, "Basic realm=\"%s\"", auth_realm) == -1)
1241 goto fail;
1242
1243 /* Write the generic 40x message */
1244 if (http_reply_to_htx(s, htx, reply) == -1)
1245 goto fail;
1246
1247 /* Remove all existing occurrences of the XXX-Authenticate header */
1248 ctx.blk = NULL;
1249 while (http_find_header(htx, hdr, &ctx, 1))
1250 http_remove_header(htx, &ctx);
1251
1252 /* Now a the right XXX-Authenticate header */
1253 if (!http_add_header(htx, hdr, ist2(b_orig(&trash), b_data(&trash))))
1254 goto fail;
1255
1256 /* Finally forward the reply */
1257 htx_to_buf(htx, &res->buf);
1258 if (!http_forward_proxy_resp(s, 1))
1259 goto fail;
1260
1261 /* Note: Only eval on the request */
1262 s->logs.tv_request = now;
1263 req->analysers &= AN_REQ_FLT_END;
1264
1265 if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Willy Tarreau4781b152021-04-06 13:53:36 +02001266 _HA_ATOMIC_INC(&s->sess->fe->fe_counters.intercepted_req);
Christopher Fauletb3048832020-05-27 15:26:43 +02001267
1268 if (!(s->flags & SF_ERR_MASK))
1269 s->flags |= SF_ERR_LOCAL;
1270 if (!(s->flags & SF_FINST_MASK))
1271 s->flags |= SF_FINST_R;
1272
1273 stream_inc_http_err_ctr(s);
1274 return ACT_RET_ABRT;
1275
1276 fail:
1277 /* If an error occurred, remove the incomplete HTTP response from the
1278 * buffer */
1279 channel_htx_truncate(res, htx);
1280 return ACT_RET_ERR;
1281}
1282
Christopher Faulet81e20172019-12-12 16:40:30 +01001283/* Parse a "auth" action. It may take 2 optional arguments to define a "realm"
1284 * parameter. It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
1285 */
1286static enum act_parse_ret parse_http_auth(const char **args, int *orig_arg, struct proxy *px,
1287 struct act_rule *rule, char **err)
1288{
1289 int cur_arg;
1290
Christopher Fauletb3048832020-05-27 15:26:43 +02001291 rule->action = ACT_CUSTOM;
Christopher Faulet245cf792019-12-18 14:58:12 +01001292 rule->flags |= ACT_FLAG_FINAL;
Christopher Fauletb3048832020-05-27 15:26:43 +02001293 rule->action_ptr = http_action_auth;
Christopher Faulet2eb53962020-01-14 14:47:34 +01001294 rule->release_ptr = release_http_action;
Christopher Faulet81e20172019-12-12 16:40:30 +01001295
1296 cur_arg = *orig_arg;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001297 if (strcmp(args[cur_arg], "realm") == 0) {
Christopher Faulet81e20172019-12-12 16:40:30 +01001298 cur_arg++;
1299 if (!*args[cur_arg]) {
1300 memprintf(err, "missing realm value.\n");
1301 return ACT_RET_PRS_ERR;
1302 }
Christopher Faulet96bff762019-12-17 13:46:18 +01001303 rule->arg.http.str.ptr = strdup(args[cur_arg]);
1304 rule->arg.http.str.len = strlen(rule->arg.http.str.ptr);
Christopher Faulet81e20172019-12-12 16:40:30 +01001305 cur_arg++;
1306 }
1307
Christopher Fauletc20b3712020-01-27 15:51:56 +01001308 LIST_INIT(&rule->arg.http.fmt);
Christopher Faulet81e20172019-12-12 16:40:30 +01001309 *orig_arg = cur_arg;
1310 return ACT_RET_PRS_OK;
1311}
1312
1313/* Parse a "set-nice" action. It takes the nice value as argument. It returns
1314 * ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
1315 */
1316static enum act_parse_ret parse_http_set_nice(const char **args, int *orig_arg, struct proxy *px,
1317 struct act_rule *rule, char **err)
1318{
1319 int cur_arg;
1320
1321 rule->action = ACT_HTTP_SET_NICE;
1322
1323 cur_arg = *orig_arg;
1324 if (!*args[cur_arg]) {
1325 memprintf(err, "expects exactly 1 argument (integer value)");
1326 return ACT_RET_PRS_ERR;
1327 }
Christopher Faulet96bff762019-12-17 13:46:18 +01001328 rule->arg.http.i = atoi(args[cur_arg]);
1329 if (rule->arg.http.i < -1024)
1330 rule->arg.http.i = -1024;
1331 else if (rule->arg.http.i > 1024)
1332 rule->arg.http.i = 1024;
Christopher Faulet81e20172019-12-12 16:40:30 +01001333
Christopher Fauletc20b3712020-01-27 15:51:56 +01001334 LIST_INIT(&rule->arg.http.fmt);
Christopher Faulet81e20172019-12-12 16:40:30 +01001335 *orig_arg = cur_arg + 1;
1336 return ACT_RET_PRS_OK;
1337}
1338
1339/* Parse a "set-tos" action. It takes the TOS value as argument. It returns
1340 * ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
1341 */
1342static enum act_parse_ret parse_http_set_tos(const char **args, int *orig_arg, struct proxy *px,
1343 struct act_rule *rule, char **err)
1344{
1345#ifdef IP_TOS
1346 char *endp;
1347 int cur_arg;
1348
1349 rule->action = ACT_HTTP_SET_TOS;
1350
1351 cur_arg = *orig_arg;
1352 if (!*args[cur_arg]) {
1353 memprintf(err, "expects exactly 1 argument (integer/hex value)");
1354 return ACT_RET_PRS_ERR;
1355 }
Christopher Faulet96bff762019-12-17 13:46:18 +01001356 rule->arg.http.i = strtol(args[cur_arg], &endp, 0);
Christopher Faulet81e20172019-12-12 16:40:30 +01001357 if (endp && *endp != '\0') {
1358 memprintf(err, "invalid character starting at '%s' (integer/hex value expected)", endp);
1359 return ACT_RET_PRS_ERR;
1360 }
1361
Christopher Fauletc20b3712020-01-27 15:51:56 +01001362 LIST_INIT(&rule->arg.http.fmt);
Christopher Faulet81e20172019-12-12 16:40:30 +01001363 *orig_arg = cur_arg + 1;
1364 return ACT_RET_PRS_OK;
1365#else
1366 memprintf(err, "not supported on this platform (IP_TOS undefined)");
1367 return ACT_RET_PRS_ERR;
1368#endif
1369}
1370
1371/* Parse a "set-mark" action. It takes the MARK value as argument. It returns
1372 * ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
1373 */
1374static enum act_parse_ret parse_http_set_mark(const char **args, int *orig_arg, struct proxy *px,
1375 struct act_rule *rule, char **err)
1376{
1377#ifdef SO_MARK
1378 char *endp;
1379 int cur_arg;
1380
1381 rule->action = ACT_HTTP_SET_MARK;
1382
1383 cur_arg = *orig_arg;
1384 if (!*args[cur_arg]) {
1385 memprintf(err, "expects exactly 1 argument (integer/hex value)");
1386 return ACT_RET_PRS_ERR;
1387 }
Christopher Faulet96bff762019-12-17 13:46:18 +01001388 rule->arg.http.i = strtoul(args[cur_arg], &endp, 0);
Christopher Faulet81e20172019-12-12 16:40:30 +01001389 if (endp && *endp != '\0') {
1390 memprintf(err, "invalid character starting at '%s' (integer/hex value expected)", endp);
1391 return ACT_RET_PRS_ERR;
1392 }
1393
Christopher Fauletc20b3712020-01-27 15:51:56 +01001394 LIST_INIT(&rule->arg.http.fmt);
Christopher Faulet81e20172019-12-12 16:40:30 +01001395 *orig_arg = cur_arg + 1;
1396 global.last_checks |= LSTCHK_NETADM;
1397 return ACT_RET_PRS_OK;
1398#else
1399 memprintf(err, "not supported on this platform (SO_MARK undefined)");
1400 return ACT_RET_PRS_ERR;
1401#endif
1402}
1403
1404/* Parse a "set-log-level" action. It takes the level value as argument. It
1405 * returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
1406 */
1407static enum act_parse_ret parse_http_set_log_level(const char **args, int *orig_arg, struct proxy *px,
1408 struct act_rule *rule, char **err)
1409{
1410 int cur_arg;
1411
1412 rule->action = ACT_HTTP_SET_LOGL;
1413
1414 cur_arg = *orig_arg;
1415 if (!*args[cur_arg]) {
1416 bad_log_level:
1417 memprintf(err, "expects exactly 1 argument (log level name or 'silent')");
1418 return ACT_RET_PRS_ERR;
1419 }
1420 if (strcmp(args[cur_arg], "silent") == 0)
Christopher Faulet96bff762019-12-17 13:46:18 +01001421 rule->arg.http.i = -1;
1422 else if ((rule->arg.http.i = get_log_level(args[cur_arg]) + 1) == 0)
Christopher Faulet81e20172019-12-12 16:40:30 +01001423 goto bad_log_level;
1424
Christopher Fauletc20b3712020-01-27 15:51:56 +01001425 LIST_INIT(&rule->arg.http.fmt);
Christopher Faulet81e20172019-12-12 16:40:30 +01001426 *orig_arg = cur_arg + 1;
1427 return ACT_RET_PRS_OK;
1428}
1429
Christopher Faulet91b3ec12020-01-17 22:30:06 +01001430/* This function executes a early-hint action. It adds an HTTP Early Hint HTTP
1431 * 103 response header with <.arg.http.str> name and with a value built
1432 * according to <.arg.http.fmt> log line format. If it is the first early-hint
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001433 * rule of series, the 103 response start-line is added first. At the end, if
Christopher Faulet91b3ec12020-01-17 22:30:06 +01001434 * the next rule is not an early-hint rule or if it is the last rule, the EOH
1435 * block is added to terminate the response. On success, it returns
1436 * ACT_RET_CONT. If an error occurs while soft rewrites are enabled, the action
1437 * is canceled, but the rule processing continue. Otherwsize ACT_RET_ERR is
1438 * returned.
1439 */
1440static enum act_return http_action_early_hint(struct act_rule *rule, struct proxy *px,
1441 struct session *sess, struct stream *s, int flags)
1442{
1443 struct act_rule *prev_rule, *next_rule;
1444 struct channel *res = &s->res;
1445 struct htx *htx = htx_from_buf(&res->buf);
1446 struct buffer *value = alloc_trash_chunk();
1447 enum act_return ret = ACT_RET_CONT;
1448
1449 if (!(s->txn->req.flags & HTTP_MSGF_VER_11))
1450 goto leave;
1451
1452 if (!value) {
1453 if (!(s->flags & SF_ERR_MASK))
1454 s->flags |= SF_ERR_RESOURCE;
1455 goto error;
1456 }
1457
1458 /* get previous and next rules */
1459 prev_rule = LIST_PREV(&rule->list, typeof(rule), list);
1460 next_rule = LIST_NEXT(&rule->list, typeof(rule), list);
1461
1462 /* if no previous rule or previous rule is not early-hint, start a new response. Otherwise,
1463 * continue to add link to a previously started response */
1464 if (&prev_rule->list == s->current_rule_list || prev_rule->action_ptr != http_action_early_hint) {
1465 struct htx_sl *sl;
1466 unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|
1467 HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
1468
1469 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
1470 ist("HTTP/1.1"), ist("103"), ist("Early Hints"));
1471 if (!sl)
1472 goto error;
1473 sl->info.res.status = 103;
1474 }
1475
1476 /* Add the HTTP Early Hint HTTP 103 response heade */
1477 value->data = build_logline(s, b_tail(value), b_room(value), &rule->arg.http.fmt);
1478 if (!htx_add_header(htx, rule->arg.http.str, ist2(b_head(value), b_data(value))))
1479 goto error;
1480
1481 /* if it is the last rule or the next one is not an early-hint, terminate the current
1482 * response. */
1483 if (&next_rule->list == s->current_rule_list || next_rule->action_ptr != http_action_early_hint) {
Christopher Faulet91b3ec12020-01-17 22:30:06 +01001484 if (!htx_add_endof(htx, HTX_BLK_EOH)) {
1485 /* If an error occurred during an Early-hint rule,
1486 * remove the incomplete HTTP 103 response from the
1487 * buffer */
1488 goto error;
1489 }
1490
Christopher Fauleta72a7e42020-01-28 09:28:11 +01001491 if (!http_forward_proxy_resp(s, 0))
1492 goto error;
Christopher Faulet91b3ec12020-01-17 22:30:06 +01001493 }
1494
1495 leave:
1496 free_trash_chunk(value);
1497 return ret;
1498
1499 error:
1500 /* If an error occurred during an Early-hint rule, remove the incomplete
1501 * HTTP 103 response from the buffer */
1502 channel_htx_truncate(res, htx);
1503 ret = ACT_RET_ERR;
1504 goto leave;
1505}
1506
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001507/* This function executes a set-header or add-header actions. It builds a string
1508 * in the trash from the specified format string. It finds the action to be
1509 * performed in <.action>, previously filled by function parse_set_header(). The
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001510 * replacement action is executed by the function http_action_set_header(). On
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001511 * success, it returns ACT_RET_CONT. If an error occurs while soft rewrites are
1512 * enabled, the action is canceled, but the rule processing continue. Otherwsize
1513 * ACT_RET_ERR is returned.
1514 */
1515static enum act_return http_action_set_header(struct act_rule *rule, struct proxy *px,
1516 struct session *sess, struct stream *s, int flags)
1517{
Christopher Faulet91e31d82020-01-24 15:37:13 +01001518 struct http_msg *msg = ((rule->from == ACT_F_HTTP_REQ) ? &s->txn->req : &s->txn->rsp);
1519 struct htx *htx = htxbuf(&msg->chn->buf);
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001520 enum act_return ret = ACT_RET_CONT;
1521 struct buffer *replace;
1522 struct http_hdr_ctx ctx;
1523 struct ist n, v;
1524
1525 replace = alloc_trash_chunk();
1526 if (!replace)
1527 goto fail_alloc;
1528
1529 replace->data = build_logline(s, replace->area, replace->size, &rule->arg.http.fmt);
1530 n = rule->arg.http.str;
1531 v = ist2(replace->area, replace->data);
1532
1533 if (rule->action == 0) { // set-header
1534 /* remove all occurrences of the header */
1535 ctx.blk = NULL;
1536 while (http_find_header(htx, n, &ctx, 1))
1537 http_remove_header(htx, &ctx);
1538 }
1539
1540 /* Now add header */
1541 if (!http_add_header(htx, n, v))
1542 goto fail_rewrite;
1543
1544 leave:
1545 free_trash_chunk(replace);
1546 return ret;
1547
1548 fail_alloc:
1549 if (!(s->flags & SF_ERR_MASK))
1550 s->flags |= SF_ERR_RESOURCE;
1551 ret = ACT_RET_ERR;
1552 goto leave;
1553
1554 fail_rewrite:
Willy Tarreau4781b152021-04-06 13:53:36 +02001555 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_rewrites);
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001556 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +02001557 _HA_ATOMIC_INC(&s->be->be_counters.failed_rewrites);
William Lallemand36119de2021-03-08 15:26:48 +01001558 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001559 _HA_ATOMIC_INC(&sess->listener->counters->failed_rewrites);
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001560 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001561 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_rewrites);
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001562
Christopher Faulet333bf8c2020-01-22 14:38:05 +01001563 if (!(msg->flags & HTTP_MSGF_SOFT_RW)) {
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001564 ret = ACT_RET_ERR;
Christopher Faulet333bf8c2020-01-22 14:38:05 +01001565 if (!(s->flags & SF_ERR_MASK))
1566 s->flags |= SF_ERR_PRXCOND;
1567 }
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001568 goto leave;
1569}
1570
Christopher Faulet81e20172019-12-12 16:40:30 +01001571/* Parse a "set-header", "add-header" or "early-hint" actions. It takes an
1572 * header name and a log-format string as arguments. It returns ACT_RET_PRS_OK
1573 * on success, ACT_RET_PRS_ERR on error.
1574 *
1575 * Note: same function is used for the request and the response. However
1576 * "early-hint" rules are only supported for request rules.
1577 */
1578static enum act_parse_ret parse_http_set_header(const char **args, int *orig_arg, struct proxy *px,
1579 struct act_rule *rule, char **err)
1580{
Christopher Faulet81e20172019-12-12 16:40:30 +01001581 int cap, cur_arg;
1582
Christopher Faulet91b3ec12020-01-17 22:30:06 +01001583 if (args[*orig_arg-1][0] == 'e') {
1584 rule->action = ACT_CUSTOM;
1585 rule->action_ptr = http_action_early_hint;
1586 }
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001587 else {
1588 if (args[*orig_arg-1][0] == 's')
1589 rule->action = 0; // set-header
1590 else
1591 rule->action = 1; // add-header
1592 rule->action_ptr = http_action_set_header;
1593 }
Christopher Faulet2eb53962020-01-14 14:47:34 +01001594 rule->release_ptr = release_http_action;
Christopher Faulet81e20172019-12-12 16:40:30 +01001595
1596 cur_arg = *orig_arg;
1597 if (!*args[cur_arg] || !*args[cur_arg+1]) {
1598 memprintf(err, "expects exactly 2 arguments");
1599 return ACT_RET_PRS_ERR;
1600 }
1601
Christopher Faulet81e20172019-12-12 16:40:30 +01001602
Christopher Faulet96bff762019-12-17 13:46:18 +01001603 rule->arg.http.str.ptr = strdup(args[cur_arg]);
1604 rule->arg.http.str.len = strlen(rule->arg.http.str.ptr);
1605 LIST_INIT(&rule->arg.http.fmt);
Christopher Faulet81e20172019-12-12 16:40:30 +01001606
1607 if (rule->from == ACT_F_HTTP_REQ) {
1608 px->conf.args.ctx = ARGC_HRQ;
1609 cap = (px->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR;
1610 }
1611 else{
1612 px->conf.args.ctx = ARGC_HRS;
1613 cap = (px->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR;
1614 }
1615
1616 cur_arg++;
Christopher Faulet1337b322020-01-14 14:50:55 +01001617 if (!parse_logformat_string(args[cur_arg], px, &rule->arg.http.fmt, LOG_OPT_HTTP, cap, err)) {
Tim Duesterhused526372020-03-05 17:56:33 +01001618 istfree(&rule->arg.http.str);
Christopher Faulet81e20172019-12-12 16:40:30 +01001619 return ACT_RET_PRS_ERR;
Christopher Faulet1337b322020-01-14 14:50:55 +01001620 }
Christopher Faulet81e20172019-12-12 16:40:30 +01001621
1622 free(px->conf.lfs_file);
1623 px->conf.lfs_file = strdup(px->conf.args.file);
1624 px->conf.lfs_line = px->conf.args.line;
1625
1626 *orig_arg = cur_arg + 1;
1627 return ACT_RET_PRS_OK;
1628}
1629
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001630/* This function executes a replace-header or replace-value actions. It
1631 * builds a string in the trash from the specified format string. It finds
1632 * the action to be performed in <.action>, previously filled by function
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001633 * parse_replace_header(). The replacement action is executed by the function
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001634 * http_action_replace_header(). On success, it returns ACT_RET_CONT. If an error
1635 * occurs while soft rewrites are enabled, the action is canceled, but the rule
1636 * processing continue. Otherwsize ACT_RET_ERR is returned.
1637 */
1638static enum act_return http_action_replace_header(struct act_rule *rule, struct proxy *px,
1639 struct session *sess, struct stream *s, int flags)
1640{
Christopher Faulet91e31d82020-01-24 15:37:13 +01001641 struct http_msg *msg = ((rule->from == ACT_F_HTTP_REQ) ? &s->txn->req : &s->txn->rsp);
1642 struct htx *htx = htxbuf(&msg->chn->buf);
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001643 enum act_return ret = ACT_RET_CONT;
1644 struct buffer *replace;
1645 int r;
1646
1647 replace = alloc_trash_chunk();
1648 if (!replace)
1649 goto fail_alloc;
1650
1651 replace->data = build_logline(s, replace->area, replace->size, &rule->arg.http.fmt);
1652
1653 r = http_replace_hdrs(s, htx, rule->arg.http.str, replace->area, rule->arg.http.re, (rule->action == 0));
1654 if (r == -1)
1655 goto fail_rewrite;
1656
1657 leave:
1658 free_trash_chunk(replace);
1659 return ret;
1660
1661 fail_alloc:
1662 if (!(s->flags & SF_ERR_MASK))
1663 s->flags |= SF_ERR_RESOURCE;
1664 ret = ACT_RET_ERR;
1665 goto leave;
1666
1667 fail_rewrite:
Willy Tarreau4781b152021-04-06 13:53:36 +02001668 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_rewrites);
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001669 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +02001670 _HA_ATOMIC_INC(&s->be->be_counters.failed_rewrites);
William Lallemand36119de2021-03-08 15:26:48 +01001671 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001672 _HA_ATOMIC_INC(&sess->listener->counters->failed_rewrites);
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001673 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001674 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_rewrites);
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001675
Christopher Faulet333bf8c2020-01-22 14:38:05 +01001676 if (!(msg->flags & HTTP_MSGF_SOFT_RW)) {
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001677 ret = ACT_RET_ERR;
Christopher Faulet333bf8c2020-01-22 14:38:05 +01001678 if (!(s->flags & SF_ERR_MASK))
1679 s->flags |= SF_ERR_PRXCOND;
1680 }
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001681 goto leave;
1682}
1683
Christopher Faulet81e20172019-12-12 16:40:30 +01001684/* Parse a "replace-header" or "replace-value" actions. It takes an header name,
1685 * a regex and replacement string as arguments. It returns ACT_RET_PRS_OK on
1686 * success, ACT_RET_PRS_ERR on error.
1687 */
1688static enum act_parse_ret parse_http_replace_header(const char **args, int *orig_arg, struct proxy *px,
1689 struct act_rule *rule, char **err)
1690{
1691 int cap, cur_arg;
1692
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001693 if (args[*orig_arg-1][8] == 'h')
1694 rule->action = 0; // replace-header
1695 else
1696 rule->action = 1; // replace-value
1697 rule->action_ptr = http_action_replace_header;
Christopher Faulet2eb53962020-01-14 14:47:34 +01001698 rule->release_ptr = release_http_action;
Christopher Faulet81e20172019-12-12 16:40:30 +01001699
1700 cur_arg = *orig_arg;
1701 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2]) {
1702 memprintf(err, "expects exactly 3 arguments");
1703 return ACT_RET_PRS_ERR;
1704 }
1705
Christopher Faulet96bff762019-12-17 13:46:18 +01001706 rule->arg.http.str.ptr = strdup(args[cur_arg]);
1707 rule->arg.http.str.len = strlen(rule->arg.http.str.ptr);
1708 LIST_INIT(&rule->arg.http.fmt);
Christopher Faulet81e20172019-12-12 16:40:30 +01001709
1710 cur_arg++;
Christopher Faulet1337b322020-01-14 14:50:55 +01001711 if (!(rule->arg.http.re = regex_comp(args[cur_arg], 1, 1, err))) {
Tim Duesterhused526372020-03-05 17:56:33 +01001712 istfree(&rule->arg.http.str);
Christopher Faulet81e20172019-12-12 16:40:30 +01001713 return ACT_RET_PRS_ERR;
Christopher Faulet1337b322020-01-14 14:50:55 +01001714 }
Christopher Faulet81e20172019-12-12 16:40:30 +01001715
1716 if (rule->from == ACT_F_HTTP_REQ) {
1717 px->conf.args.ctx = ARGC_HRQ;
1718 cap = (px->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR;
1719 }
1720 else{
1721 px->conf.args.ctx = ARGC_HRS;
1722 cap = (px->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR;
1723 }
1724
1725 cur_arg++;
Christopher Faulet1337b322020-01-14 14:50:55 +01001726 if (!parse_logformat_string(args[cur_arg], px, &rule->arg.http.fmt, LOG_OPT_HTTP, cap, err)) {
Tim Duesterhused526372020-03-05 17:56:33 +01001727 istfree(&rule->arg.http.str);
Christopher Faulet1337b322020-01-14 14:50:55 +01001728 regex_free(rule->arg.http.re);
Christopher Faulet81e20172019-12-12 16:40:30 +01001729 return ACT_RET_PRS_ERR;
Christopher Faulet1337b322020-01-14 14:50:55 +01001730 }
Christopher Faulet81e20172019-12-12 16:40:30 +01001731
1732 free(px->conf.lfs_file);
1733 px->conf.lfs_file = strdup(px->conf.args.file);
1734 px->conf.lfs_line = px->conf.args.line;
1735
1736 *orig_arg = cur_arg + 1;
1737 return ACT_RET_PRS_OK;
1738}
1739
Maciej Zdebebdd4c52020-11-20 13:58:48 +00001740/* This function executes a del-header action with selected matching mode for
1741 * header name. It finds the matching method to be performed in <.action>, previously
1742 * filled by function parse_http_del_header(). On success, it returns ACT_RET_CONT.
1743 * Otherwise ACT_RET_ERR is returned.
1744 */
1745static enum act_return http_action_del_header(struct act_rule *rule, struct proxy *px,
1746 struct session *sess, struct stream *s, int flags)
1747{
1748 struct http_hdr_ctx ctx;
1749 struct http_msg *msg = ((rule->from == ACT_F_HTTP_REQ) ? &s->txn->req : &s->txn->rsp);
1750 struct htx *htx = htxbuf(&msg->chn->buf);
1751 enum act_return ret = ACT_RET_CONT;
1752
1753 /* remove all occurrences of the header */
1754 ctx.blk = NULL;
1755 switch (rule->action) {
1756 case PAT_MATCH_STR:
1757 while (http_find_header(htx, rule->arg.http.str, &ctx, 1))
1758 http_remove_header(htx, &ctx);
1759 break;
1760 case PAT_MATCH_BEG:
1761 while (http_find_pfx_header(htx, rule->arg.http.str, &ctx, 1))
1762 http_remove_header(htx, &ctx);
1763 break;
1764 case PAT_MATCH_END:
1765 while (http_find_sfx_header(htx, rule->arg.http.str, &ctx, 1))
1766 http_remove_header(htx, &ctx);
1767 break;
1768 case PAT_MATCH_SUB:
1769 while (http_find_sub_header(htx, rule->arg.http.str, &ctx, 1))
1770 http_remove_header(htx, &ctx);
1771 break;
1772 case PAT_MATCH_REG:
1773 while (http_match_header(htx, rule->arg.http.re, &ctx, 1))
1774 http_remove_header(htx, &ctx);
1775 break;
1776 default:
1777 return ACT_RET_ERR;
1778 }
1779 return ret;
1780}
1781
1782/* Parse a "del-header" action. It takes string as a required argument,
1783 * optional flag (currently only -m) and optional matching method of input string
1784 * with header name to be deleted. Default matching method is exact match (-m str).
1785 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Christopher Faulet81e20172019-12-12 16:40:30 +01001786 */
1787static enum act_parse_ret parse_http_del_header(const char **args, int *orig_arg, struct proxy *px,
1788 struct act_rule *rule, char **err)
1789{
1790 int cur_arg;
Maciej Zdebebdd4c52020-11-20 13:58:48 +00001791 int pat_idx;
Christopher Faulet81e20172019-12-12 16:40:30 +01001792
Maciej Zdebebdd4c52020-11-20 13:58:48 +00001793 /* set exact matching (-m str) as default */
1794 rule->action = PAT_MATCH_STR;
1795 rule->action_ptr = http_action_del_header;
Christopher Faulet2eb53962020-01-14 14:47:34 +01001796 rule->release_ptr = release_http_action;
Christopher Faulet81e20172019-12-12 16:40:30 +01001797
1798 cur_arg = *orig_arg;
1799 if (!*args[cur_arg]) {
Maciej Zdebebdd4c52020-11-20 13:58:48 +00001800 memprintf(err, "expects at least 1 argument");
Christopher Faulet81e20172019-12-12 16:40:30 +01001801 return ACT_RET_PRS_ERR;
1802 }
1803
Christopher Faulet96bff762019-12-17 13:46:18 +01001804 rule->arg.http.str.ptr = strdup(args[cur_arg]);
1805 rule->arg.http.str.len = strlen(rule->arg.http.str.ptr);
Christopher Faulet81e20172019-12-12 16:40:30 +01001806 px->conf.args.ctx = (rule->from == ACT_F_HTTP_REQ ? ARGC_HRQ : ARGC_HRS);
1807
Maciej Zdeb6dee9962020-11-23 16:03:09 +00001808 LIST_INIT(&rule->arg.http.fmt);
Maciej Zdebebdd4c52020-11-20 13:58:48 +00001809 if (strcmp(args[cur_arg+1], "-m") == 0) {
1810 cur_arg++;
1811 if (!*args[cur_arg+1]) {
1812 memprintf(err, "-m flag expects exactly 1 argument");
1813 return ACT_RET_PRS_ERR;
1814 }
1815
1816 cur_arg++;
1817 pat_idx = pat_find_match_name(args[cur_arg]);
1818 switch (pat_idx) {
1819 case PAT_MATCH_REG:
1820 if (!(rule->arg.http.re = regex_comp(rule->arg.http.str.ptr, 1, 1, err)))
1821 return ACT_RET_PRS_ERR;
1822 /* fall through */
1823 case PAT_MATCH_STR:
1824 case PAT_MATCH_BEG:
1825 case PAT_MATCH_END:
1826 case PAT_MATCH_SUB:
1827 rule->action = pat_idx;
1828 break;
1829 default:
1830 memprintf(err, "-m with unsupported matching method '%s'", args[cur_arg]);
1831 return ACT_RET_PRS_ERR;
1832 }
1833 }
1834
Christopher Faulet81e20172019-12-12 16:40:30 +01001835 *orig_arg = cur_arg + 1;
1836 return ACT_RET_PRS_OK;
1837}
1838
Christopher Faulet2eb53962020-01-14 14:47:34 +01001839/* Release memory allocated by an http redirect action. */
1840static void release_http_redir(struct act_rule *rule)
1841{
1842 struct logformat_node *lf, *lfb;
1843 struct redirect_rule *redir;
1844
1845 redir = rule->arg.redir;
Willy Tarreau2b718102021-04-21 07:32:39 +02001846 LIST_DELETE(&redir->list);
Christopher Faulet2eb53962020-01-14 14:47:34 +01001847 if (redir->cond) {
1848 prune_acl_cond(redir->cond);
1849 free(redir->cond);
1850 }
1851 free(redir->rdr_str);
1852 free(redir->cookie_str);
1853 list_for_each_entry_safe(lf, lfb, &redir->rdr_fmt, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001854 LIST_DELETE(&lf->list);
Christopher Faulet2eb53962020-01-14 14:47:34 +01001855 free(lf);
1856 }
1857 free(redir);
1858}
1859
Christopher Faulet81e20172019-12-12 16:40:30 +01001860/* Parse a "redirect" action. It returns ACT_RET_PRS_OK on success,
1861 * ACT_RET_PRS_ERR on error.
1862 */
1863static enum act_parse_ret parse_http_redirect(const char **args, int *orig_arg, struct proxy *px,
1864 struct act_rule *rule, char **err)
1865{
1866 struct redirect_rule *redir;
1867 int dir, cur_arg;
1868
1869 rule->action = ACT_HTTP_REDIR;
Christopher Faulet245cf792019-12-18 14:58:12 +01001870 rule->flags |= ACT_FLAG_FINAL;
Christopher Faulet2eb53962020-01-14 14:47:34 +01001871 rule->release_ptr = release_http_redir;
Christopher Faulet81e20172019-12-12 16:40:30 +01001872
1873 cur_arg = *orig_arg;
1874
1875 dir = (rule->from == ACT_F_HTTP_REQ ? 0 : 1);
1876 if ((redir = http_parse_redirect_rule(px->conf.args.file, px->conf.args.line, px, &args[cur_arg], err, 1, dir)) == NULL)
1877 return ACT_RET_PRS_ERR;
1878
1879 rule->arg.redir = redir;
1880 rule->cond = redir->cond;
1881 redir->cond = NULL;
1882
1883 /* skip all arguments */
1884 while (*args[cur_arg])
1885 cur_arg++;
1886
1887 *orig_arg = cur_arg;
1888 return ACT_RET_PRS_OK;
1889}
1890
Christopher Faulet046cf442019-12-17 15:45:23 +01001891/* This function executes a add-acl, del-acl, set-map or del-map actions. On
1892 * success, it returns ACT_RET_CONT. Otherwsize ACT_RET_ERR is returned.
1893 */
1894static enum act_return http_action_set_map(struct act_rule *rule, struct proxy *px,
1895 struct session *sess, struct stream *s, int flags)
1896{
1897 struct pat_ref *ref;
1898 struct buffer *key = NULL, *value = NULL;
1899 enum act_return ret = ACT_RET_CONT;
1900
1901 /* collect reference */
1902 ref = pat_ref_lookup(rule->arg.map.ref);
1903 if (!ref)
1904 goto leave;
1905
1906 /* allocate key */
1907 key = alloc_trash_chunk();
1908 if (!key)
1909 goto fail_alloc;
1910
1911 /* collect key */
1912 key->data = build_logline(s, key->area, key->size, &rule->arg.map.key);
1913 key->area[key->data] = '\0';
1914
1915 switch (rule->action) {
1916 case 0: // add-acl
1917 /* add entry only if it does not already exist */
1918 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
1919 if (pat_ref_find_elt(ref, key->area) == NULL)
1920 pat_ref_add(ref, key->area, NULL, NULL);
1921 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
1922 break;
1923
1924 case 1: // set-map
1925 /* allocate value */
1926 value = alloc_trash_chunk();
1927 if (!value)
1928 goto fail_alloc;
1929
1930 /* collect value */
1931 value->data = build_logline(s, value->area, value->size, &rule->arg.map.value);
1932 value->area[value->data] = '\0';
1933
1934 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
1935 if (pat_ref_find_elt(ref, key->area) != NULL) {
1936 /* update entry if it exists */
1937 pat_ref_set(ref, key->area, value->area, NULL);
1938 }
1939 else {
1940 /* insert a new entry */
1941 pat_ref_add(ref, key->area, value->area, NULL);
1942 }
1943 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
1944 break;
1945
1946 case 2: // del-acl
1947 case 3: // del-map
1948 /* returned code: 1=ok, 0=ko */
1949 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
1950 pat_ref_delete(ref, key->area);
1951 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
1952 break;
1953
1954 default:
1955 ret = ACT_RET_ERR;
1956 }
1957
1958
1959 leave:
1960 free_trash_chunk(key);
1961 free_trash_chunk(value);
1962 return ret;
1963
1964 fail_alloc:
1965 if (!(s->flags & SF_ERR_MASK))
1966 s->flags |= SF_ERR_RESOURCE;
1967 ret = ACT_RET_ERR;
1968 goto leave;
1969}
1970
Christopher Faulet2eb53962020-01-14 14:47:34 +01001971/* Release memory allocated by an http map/acl action. */
1972static void release_http_map(struct act_rule *rule)
1973{
1974 struct logformat_node *lf, *lfb;
1975
1976 free(rule->arg.map.ref);
1977 list_for_each_entry_safe(lf, lfb, &rule->arg.map.key, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001978 LIST_DELETE(&lf->list);
Christopher Faulet2eb53962020-01-14 14:47:34 +01001979 release_sample_expr(lf->expr);
1980 free(lf->arg);
1981 free(lf);
1982 }
1983 if (rule->action == 1) {
1984 list_for_each_entry_safe(lf, lfb, &rule->arg.map.value, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001985 LIST_DELETE(&lf->list);
Christopher Faulet2eb53962020-01-14 14:47:34 +01001986 release_sample_expr(lf->expr);
1987 free(lf->arg);
1988 free(lf);
1989 }
1990 }
1991}
1992
Christopher Faulet81e20172019-12-12 16:40:30 +01001993/* Parse a "add-acl", "del-acl", "set-map" or "del-map" actions. It takes one or
Christopher Faulet046cf442019-12-17 15:45:23 +01001994 * two log-format string as argument depending on the action. The action is
1995 * stored in <.action> as an int (0=add-acl, 1=set-map, 2=del-acl,
1996 * 3=del-map). It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Christopher Faulet81e20172019-12-12 16:40:30 +01001997 */
1998static enum act_parse_ret parse_http_set_map(const char **args, int *orig_arg, struct proxy *px,
1999 struct act_rule *rule, char **err)
2000{
2001 int cap, cur_arg;
2002
Christopher Faulet046cf442019-12-17 15:45:23 +01002003 if (args[*orig_arg-1][0] == 'a') // add-acl
2004 rule->action = 0;
2005 else if (args[*orig_arg-1][0] == 's') // set-map
2006 rule->action = 1;
2007 else if (args[*orig_arg-1][4] == 'a') // del-acl
2008 rule->action = 2;
2009 else if (args[*orig_arg-1][4] == 'm') // del-map
2010 rule->action = 3;
2011 else {
2012 memprintf(err, "internal error: unhandled action '%s'", args[0]);
2013 return ACT_RET_PRS_ERR;
2014 }
2015 rule->action_ptr = http_action_set_map;
Christopher Faulet2eb53962020-01-14 14:47:34 +01002016 rule->release_ptr = release_http_map;
Christopher Faulet81e20172019-12-12 16:40:30 +01002017
2018 cur_arg = *orig_arg;
Christopher Faulet046cf442019-12-17 15:45:23 +01002019 if (rule->action == 1 && (!*args[cur_arg] || !*args[cur_arg+1])) {
2020 /* 2 args for set-map */
Christopher Faulet81e20172019-12-12 16:40:30 +01002021 memprintf(err, "expects exactly 2 arguments");
2022 return ACT_RET_PRS_ERR;
2023 }
2024 else if (!*args[cur_arg]) {
Christopher Faulet046cf442019-12-17 15:45:23 +01002025 /* only one arg for other actions */
Christopher Faulet81e20172019-12-12 16:40:30 +01002026 memprintf(err, "expects exactly 1 arguments");
2027 return ACT_RET_PRS_ERR;
2028 }
2029
2030 /*
2031 * '+ 8' for 'set-map(' (same for del-map)
2032 * '- 9' for 'set-map(' + trailing ')' (same for del-map)
2033 */
2034 rule->arg.map.ref = my_strndup(args[cur_arg-1] + 8, strlen(args[cur_arg-1]) - 9);
2035
2036 if (rule->from == ACT_F_HTTP_REQ) {
2037 px->conf.args.ctx = ARGC_HRQ;
2038 cap = (px->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR;
2039 }
2040 else{
2041 px->conf.args.ctx = ARGC_HRS;
2042 cap = (px->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR;
2043 }
2044
2045 /* key pattern */
2046 LIST_INIT(&rule->arg.map.key);
Christopher Faulet1337b322020-01-14 14:50:55 +01002047 if (!parse_logformat_string(args[cur_arg], px, &rule->arg.map.key, LOG_OPT_HTTP, cap, err)) {
2048 free(rule->arg.map.ref);
Christopher Faulet81e20172019-12-12 16:40:30 +01002049 return ACT_RET_PRS_ERR;
Christopher Faulet1337b322020-01-14 14:50:55 +01002050 }
Christopher Faulet81e20172019-12-12 16:40:30 +01002051
Christopher Faulet046cf442019-12-17 15:45:23 +01002052 if (rule->action == 1) {
Christopher Faulet81e20172019-12-12 16:40:30 +01002053 /* value pattern for set-map only */
2054 cur_arg++;
2055 LIST_INIT(&rule->arg.map.value);
Christopher Faulet1337b322020-01-14 14:50:55 +01002056 if (!parse_logformat_string(args[cur_arg], px, &rule->arg.map.value, LOG_OPT_HTTP, cap, err)) {
2057 free(rule->arg.map.ref);
Christopher Faulet81e20172019-12-12 16:40:30 +01002058 return ACT_RET_PRS_ERR;
Christopher Faulet1337b322020-01-14 14:50:55 +01002059 }
Christopher Faulet81e20172019-12-12 16:40:30 +01002060 }
2061
2062 free(px->conf.lfs_file);
2063 px->conf.lfs_file = strdup(px->conf.args.file);
2064 px->conf.lfs_line = px->conf.args.line;
2065
2066 *orig_arg = cur_arg + 1;
2067 return ACT_RET_PRS_OK;
2068}
2069
Christopher Fauletac98d812019-12-18 09:20:16 +01002070/* This function executes a track-sc* actions. On success, it returns
2071 * ACT_RET_CONT. Otherwsize ACT_RET_ERR is returned.
2072 */
2073static enum act_return http_action_track_sc(struct act_rule *rule, struct proxy *px,
2074 struct session *sess, struct stream *s, int flags)
2075{
2076 struct stktable *t;
2077 struct stksess *ts;
2078 struct stktable_key *key;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002079 void *ptr1, *ptr2, *ptr3, *ptr4, *ptr5, *ptr6;
Christopher Fauletac98d812019-12-18 09:20:16 +01002080 int opt;
2081
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002082 ptr1 = ptr2 = ptr3 = ptr4 = ptr5 = ptr6 = NULL;
Christopher Fauletac98d812019-12-18 09:20:16 +01002083 opt = ((rule->from == ACT_F_HTTP_REQ) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES) | SMP_OPT_FINAL;
2084
2085 t = rule->arg.trk_ctr.table.t;
Emeric Brun362d25e2021-03-10 16:58:03 +01002086
2087 if (stkctr_entry(&s->stkctr[rule->action]))
2088 goto end;
2089
Christopher Fauletac98d812019-12-18 09:20:16 +01002090 key = stktable_fetch_key(t, s->be, sess, s, opt, rule->arg.trk_ctr.expr, NULL);
2091
2092 if (!key)
2093 goto end;
2094 ts = stktable_get_entry(t, key);
2095 if (!ts)
2096 goto end;
2097
2098 stream_track_stkctr(&s->stkctr[rule->action], t, ts);
2099
2100 /* let's count a new HTTP request as it's the first time we do it */
2101 ptr1 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
2102 ptr2 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
2103
2104 /* When the client triggers a 4xx from the server, it's most often due
2105 * to a missing object or permission. These events should be tracked
2106 * because if they happen often, it may indicate a brute force or a
2107 * vulnerability scan. Normally this is done when receiving the response
2108 * but here we're tracking after this ought to have been done so we have
2109 * to do it on purpose.
2110 */
2111 if (rule->from == ACT_F_HTTP_RES && (unsigned)(s->txn->status - 400) < 100) {
2112 ptr3 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_CNT);
2113 ptr4 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_RATE);
2114 }
2115
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002116 if (rule->from == ACT_F_HTTP_RES && (unsigned)(s->txn->status - 500) < 100 &&
2117 s->txn->status != 501 && s->txn->status != 505) {
2118 ptr5 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_FAIL_CNT);
2119 ptr6 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_FAIL_RATE);
2120 }
2121
2122 if (ptr1 || ptr2 || ptr3 || ptr4 || ptr5 || ptr6) {
Christopher Fauletac98d812019-12-18 09:20:16 +01002123 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
2124
2125 if (ptr1)
2126 stktable_data_cast(ptr1, http_req_cnt)++;
2127 if (ptr2)
2128 update_freq_ctr_period(&stktable_data_cast(ptr2, http_req_rate),
2129 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
2130 if (ptr3)
2131 stktable_data_cast(ptr3, http_err_cnt)++;
2132 if (ptr4)
2133 update_freq_ctr_period(&stktable_data_cast(ptr4, http_err_rate),
2134 t->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1);
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002135 if (ptr5)
2136 stktable_data_cast(ptr5, http_fail_cnt)++;
2137 if (ptr6)
2138 update_freq_ctr_period(&stktable_data_cast(ptr6, http_fail_rate),
2139 t->data_arg[STKTABLE_DT_HTTP_FAIL_RATE].u, 1);
Christopher Fauletac98d812019-12-18 09:20:16 +01002140
2141 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
2142
2143 /* If data was modified, we need to touch to re-schedule sync */
2144 stktable_touch_local(t, ts, 0);
2145 }
2146
2147 stkctr_set_flags(&s->stkctr[rule->action], STKCTR_TRACK_CONTENT);
2148 if (sess->fe != s->be)
2149 stkctr_set_flags(&s->stkctr[rule->action], STKCTR_TRACK_BACKEND);
2150
2151 end:
2152 return ACT_RET_CONT;
2153}
Christopher Faulet81e20172019-12-12 16:40:30 +01002154
Christopher Faulet2eb53962020-01-14 14:47:34 +01002155static void release_http_track_sc(struct act_rule *rule)
2156{
2157 release_sample_expr(rule->arg.trk_ctr.expr);
2158}
2159
Christopher Faulet81e20172019-12-12 16:40:30 +01002160/* Parse a "track-sc*" actions. It returns ACT_RET_PRS_OK on success,
2161 * ACT_RET_PRS_ERR on error.
2162 */
2163static enum act_parse_ret parse_http_track_sc(const char **args, int *orig_arg, struct proxy *px,
2164 struct act_rule *rule, char **err)
2165{
2166 struct sample_expr *expr;
2167 unsigned int where;
2168 unsigned int tsc_num;
2169 const char *tsc_num_str;
2170 int cur_arg;
2171
2172 tsc_num_str = &args[*orig_arg-1][8];
2173 if (cfg_parse_track_sc_num(&tsc_num, tsc_num_str, tsc_num_str + strlen(tsc_num_str), err) == -1)
2174 return ACT_RET_PRS_ERR;
2175
2176 cur_arg = *orig_arg;
2177 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line,
Willy Tarreaue3b57bf2020-02-14 16:50:14 +01002178 err, &px->conf.args, NULL);
Christopher Faulet81e20172019-12-12 16:40:30 +01002179 if (!expr)
2180 return ACT_RET_PRS_ERR;
2181
2182 where = 0;
2183 if (px->cap & PR_CAP_FE)
2184 where |= (rule->from == ACT_F_HTTP_REQ ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_FE_HRS_HDR);
2185 if (px->cap & PR_CAP_BE)
2186 where |= (rule->from == ACT_F_HTTP_REQ ? SMP_VAL_BE_HRQ_HDR : SMP_VAL_BE_HRS_HDR);
2187
2188 if (!(expr->fetch->val & where)) {
2189 memprintf(err, "fetch method '%s' extracts information from '%s', none of which is available here",
2190 args[cur_arg-1], sample_src_names(expr->fetch->use));
Christopher Faulet1337b322020-01-14 14:50:55 +01002191 release_sample_expr(expr);
Christopher Faulet81e20172019-12-12 16:40:30 +01002192 return ACT_RET_PRS_ERR;
2193 }
2194
2195 if (strcmp(args[cur_arg], "table") == 0) {
2196 cur_arg++;
2197 if (!*args[cur_arg]) {
2198 memprintf(err, "missing table name");
Christopher Faulet1337b322020-01-14 14:50:55 +01002199 release_sample_expr(expr);
Christopher Faulet81e20172019-12-12 16:40:30 +01002200 return ACT_RET_PRS_ERR;
2201 }
2202
2203 /* we copy the table name for now, it will be resolved later */
2204 rule->arg.trk_ctr.table.n = strdup(args[cur_arg]);
2205 cur_arg++;
2206 }
2207
Christopher Fauletac98d812019-12-18 09:20:16 +01002208 rule->action = tsc_num;
Christopher Faulet81e20172019-12-12 16:40:30 +01002209 rule->arg.trk_ctr.expr = expr;
Christopher Fauletac98d812019-12-18 09:20:16 +01002210 rule->action_ptr = http_action_track_sc;
Christopher Faulet2eb53962020-01-14 14:47:34 +01002211 rule->release_ptr = release_http_track_sc;
Christopher Faulet81e20172019-12-12 16:40:30 +01002212 rule->check_ptr = check_trk_action;
2213
2214 *orig_arg = cur_arg;
2215 return ACT_RET_PRS_OK;
2216}
2217
Amaury Denoyelle8d228232020-12-10 13:43:54 +01002218static enum act_return action_timeout_set_stream_timeout(struct act_rule *rule,
2219 struct proxy *px,
2220 struct session *sess,
2221 struct stream *s,
2222 int flags)
2223{
2224 struct sample *key;
2225
2226 if (rule->arg.timeout.expr) {
2227 key = sample_fetch_as_type(px, sess, s, SMP_OPT_FINAL, rule->arg.timeout.expr, SMP_T_SINT);
2228 if (!key)
2229 return ACT_RET_CONT;
2230
2231 stream_set_timeout(s, rule->arg.timeout.type, MS_TO_TICKS(key->data.u.sint));
2232 }
2233 else {
2234 stream_set_timeout(s, rule->arg.timeout.type, MS_TO_TICKS(rule->arg.timeout.value));
2235 }
2236
2237 return ACT_RET_CONT;
2238}
2239
2240/* Parse a "set-timeout" action. Returns ACT_RET_PRS_ERR if parsing error.
2241 */
2242static enum act_parse_ret parse_http_set_timeout(const char **args,
2243 int *orig_arg,
2244 struct proxy *px,
2245 struct act_rule *rule, char **err)
2246{
2247 int cur_arg;
2248
2249 rule->action = ACT_CUSTOM;
2250 rule->action_ptr = action_timeout_set_stream_timeout;
2251 rule->release_ptr = release_timeout_action;
2252
2253 cur_arg = *orig_arg;
2254 if (!*args[cur_arg] || !*args[cur_arg + 1]) {
2255 memprintf(err, "expects exactly 2 arguments");
2256 return ACT_RET_PRS_ERR;
2257 }
2258
2259 if (!(px->cap & PR_CAP_BE)) {
2260 memprintf(err, "proxy '%s' has no backend capability", px->id);
2261 return ACT_RET_PRS_ERR;
2262 }
2263
2264 if (cfg_parse_rule_set_timeout(args, cur_arg,
2265 &rule->arg.timeout.value,
2266 &rule->arg.timeout.type,
2267 &rule->arg.timeout.expr,
2268 err,
2269 px->conf.args.file,
2270 px->conf.args.line, &px->conf.args) == -1) {
2271 return ACT_RET_PRS_ERR;
2272 }
2273
2274 *orig_arg = cur_arg + 2;
2275
2276 return ACT_RET_PRS_OK;
2277}
2278
Christopher Faulet46f95542019-12-20 10:07:22 +01002279/* This function executes a strict-mode actions. On success, it always returns
2280 * ACT_RET_CONT
2281 */
2282static enum act_return http_action_strict_mode(struct act_rule *rule, struct proxy *px,
2283 struct session *sess, struct stream *s, int flags)
2284{
2285 struct http_msg *msg = ((rule->from == ACT_F_HTTP_REQ) ? &s->txn->req : &s->txn->rsp);
2286
2287 if (rule->action == 0) // strict-mode on
2288 msg->flags &= ~HTTP_MSGF_SOFT_RW;
2289 else // strict-mode off
2290 msg->flags |= HTTP_MSGF_SOFT_RW;
2291 return ACT_RET_CONT;
2292}
2293
2294/* Parse a "strict-mode" action. It returns ACT_RET_PRS_OK on success,
2295 * ACT_RET_PRS_ERR on error.
2296 */
2297static enum act_parse_ret parse_http_strict_mode(const char **args, int *orig_arg, struct proxy *px,
2298 struct act_rule *rule, char **err)
2299{
2300 int cur_arg;
2301
Christopher Faulet46f95542019-12-20 10:07:22 +01002302 cur_arg = *orig_arg;
2303 if (!*args[cur_arg]) {
2304 memprintf(err, "expects exactly 1 arguments");
2305 return ACT_RET_PRS_ERR;
2306 }
2307
2308 if (strcasecmp(args[cur_arg], "on") == 0)
2309 rule->action = 0; // strict-mode on
2310 else if (strcasecmp(args[cur_arg], "off") == 0)
2311 rule->action = 1; // strict-mode off
2312 else {
2313 memprintf(err, "Unexpected value '%s'. Only 'on' and 'off' are supported", args[cur_arg]);
2314 return ACT_RET_PRS_ERR;
2315 }
2316 rule->action_ptr = http_action_strict_mode;
2317
2318 *orig_arg = cur_arg + 1;
2319 return ACT_RET_PRS_OK;
2320}
2321
Christopher Faulet24231ab2020-01-24 17:44:23 +01002322/* This function executes a return action. It builds an HTX message from an
2323 * errorfile, an raw file or a log-format string, depending on <.action>
2324 * value. On success, it returns ACT_RET_ABRT. If an error occurs ACT_RET_ERR is
2325 * returned.
2326 */
2327static enum act_return http_action_return(struct act_rule *rule, struct proxy *px,
2328 struct session *sess, struct stream *s, int flags)
2329{
2330 struct channel *req = &s->req;
Christopher Faulet24231ab2020-01-24 17:44:23 +01002331
Christopher Faulet2d36df22021-02-19 11:41:01 +01002332 s->txn->status = rule->arg.http_reply->status;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02002333 if (http_reply_message(s, rule->arg.http_reply) == -1)
2334 return ACT_RET_ERR;
Christopher Faulet24231ab2020-01-24 17:44:23 +01002335
Christopher Faulet24231ab2020-01-24 17:44:23 +01002336 if (rule->from == ACT_F_HTTP_REQ) {
2337 /* let's log the request time */
2338 s->logs.tv_request = now;
2339 req->analysers &= AN_REQ_FLT_END;
2340
2341 if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Willy Tarreau4781b152021-04-06 13:53:36 +02002342 _HA_ATOMIC_INC(&s->sess->fe->fe_counters.intercepted_req);
Christopher Faulet24231ab2020-01-24 17:44:23 +01002343 }
2344
2345 if (!(s->flags & SF_ERR_MASK))
2346 s->flags |= SF_ERR_LOCAL;
2347 if (!(s->flags & SF_FINST_MASK))
2348 s->flags |= ((rule->from == ACT_F_HTTP_REQ) ? SF_FINST_R : SF_FINST_H);
2349
Christopher Faulet0e2ad612020-05-13 16:38:37 +02002350 return ACT_RET_ABRT;
Christopher Faulet24231ab2020-01-24 17:44:23 +01002351}
2352
Christopher Faulet24231ab2020-01-24 17:44:23 +01002353/* Parse a "return" action. It returns ACT_RET_PRS_OK on success,
Christopher Faulet47e791e2020-05-13 14:36:55 +02002354 * ACT_RET_PRS_ERR on error. It relies on http_parse_http_reply() to set
2355 * <.arg.http_reply>.
Christopher Faulet24231ab2020-01-24 17:44:23 +01002356 */
2357static enum act_parse_ret parse_http_return(const char **args, int *orig_arg, struct proxy *px,
2358 struct act_rule *rule, char **err)
2359{
Christopher Faulet47e791e2020-05-13 14:36:55 +02002360 /* Prepare parsing of log-format strings */
2361 px->conf.args.ctx = ((rule->from == ACT_F_HTTP_REQ) ? ARGC_HRQ : ARGC_HRS);
2362 rule->arg.http_reply = http_parse_http_reply(args, orig_arg, px, 200, err);
2363 if (!rule->arg.http_reply)
2364 return ACT_RET_PRS_ERR;
Christopher Faulet24231ab2020-01-24 17:44:23 +01002365
Christopher Fauletba946bf2020-05-13 08:50:07 +02002366 rule->flags |= ACT_FLAG_FINAL;
Christopher Faulet5ff0c642020-05-12 18:33:37 +02002367 rule->action = ACT_CUSTOM;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002368 rule->check_ptr = check_act_http_reply;
Christopher Faulet24231ab2020-01-24 17:44:23 +01002369 rule->action_ptr = http_action_return;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002370 rule->release_ptr = release_act_http_reply;
Christopher Faulet24231ab2020-01-24 17:44:23 +01002371 return ACT_RET_PRS_OK;
Christopher Faulet24231ab2020-01-24 17:44:23 +01002372}
2373
Christopher Faulet021a8e42021-03-29 10:46:38 +02002374
2375
2376/* This function executes a wait-for-body action. It waits for the message
2377 * payload for a max configured time (.arg.p[0]) and eventually for only first
2378 * <arg.p[1]> bytes (0 means no limit). It relies on http_wait_for_msg_body()
2379 * function. it returns ACT_RET_CONT when conditions are met to stop to wait.
2380 * Otherwise ACT_RET_YIELD is returned to wait for more data. ACT_RET_INV is
2381 * returned if a parsing error is raised by lower level and ACT_RET_ERR if an
Ilya Shipitsinb2be9a12021-04-24 13:25:42 +05002382 * internal error occurred. Finally ACT_RET_ABRT is returned when a timeout
2383 * occurred.
Christopher Faulet021a8e42021-03-29 10:46:38 +02002384 */
2385static enum act_return http_action_wait_for_body(struct act_rule *rule, struct proxy *px,
2386 struct session *sess, struct stream *s, int flags)
2387{
2388 struct channel *chn = ((rule->from == ACT_F_HTTP_REQ) ? &s->req : &s->res);
2389 unsigned int time = (uintptr_t)rule->arg.act.p[0];
2390 unsigned int bytes = (uintptr_t)rule->arg.act.p[1];
2391
2392 switch (http_wait_for_msg_body(s, chn, time, bytes)) {
2393 case HTTP_RULE_RES_CONT:
2394 return ACT_RET_CONT;
2395 case HTTP_RULE_RES_YIELD:
2396 return ACT_RET_YIELD;
2397 case HTTP_RULE_RES_BADREQ:
2398 return ACT_RET_INV;
2399 case HTTP_RULE_RES_ERROR:
2400 return ACT_RET_ERR;
2401 case HTTP_RULE_RES_ABRT:
2402 return ACT_RET_ABRT;
2403 default:
2404 return ACT_RET_ERR;
2405 }
2406}
2407
2408/* Parse a "wait-for-body" action. It returns ACT_RET_PRS_OK on success,
2409 * ACT_RET_PRS_ERR on error.
2410 */
2411static enum act_parse_ret parse_http_wait_for_body(const char **args, int *orig_arg, struct proxy *px,
2412 struct act_rule *rule, char **err)
2413{
2414 int cur_arg;
2415 unsigned int time, bytes;
2416 const char *res;
2417
2418 cur_arg = *orig_arg;
2419 if (!*args[cur_arg]) {
2420 memprintf(err, "expects time <time> [ at-least <bytes> ]");
2421 return ACT_RET_PRS_ERR;
2422 }
2423
2424 time = UINT_MAX; /* To be sure it is set */
2425 bytes = 0; /* Default value, wait all the body */
2426 while (*(args[cur_arg])) {
2427 if (strcmp(args[cur_arg], "time") == 0) {
2428 if (!*args[cur_arg + 1]) {
2429 memprintf(err, "missing argument for '%s'", args[cur_arg]);
2430 return ACT_RET_PRS_ERR;
2431 }
2432 res = parse_time_err(args[cur_arg+1], &time, TIME_UNIT_MS);
2433 if (res == PARSE_TIME_OVER) {
2434 memprintf(err, "time overflow (maximum value is 2147483647 ms or ~24.8 days)");
2435 return ACT_RET_PRS_ERR;
2436 }
2437 if (res == PARSE_TIME_UNDER) {
2438 memprintf(err, "time underflow (minimum non-null value is 1 ms)");
2439 return ACT_RET_PRS_ERR;
2440 }
2441 if (res) {
2442 memprintf(err, "unexpected character '%c'", *res);
2443 return ACT_RET_PRS_ERR;
2444 }
2445 cur_arg++;
2446 }
2447 else if (strcmp(args[cur_arg], "at-least") == 0) {
2448 if (!*args[cur_arg + 1]) {
2449 memprintf(err, "missing argument for '%s'", args[cur_arg]);
2450 return ACT_RET_PRS_ERR;
2451 }
2452 res = parse_size_err(args[cur_arg+1], &bytes);
2453 if (res) {
2454 memprintf(err, "unexpected character '%c'", *res);
2455 return ACT_RET_PRS_ERR;
2456 }
2457 cur_arg++;
2458 }
2459 else
2460 break;
2461 cur_arg++;
2462 }
2463
2464 if (time == UINT_MAX) {
2465 memprintf(err, "expects time <time> [ at-least <bytes> ]");
2466 return ACT_RET_PRS_ERR;
2467 }
2468
2469 rule->arg.act.p[0] = (void *)(uintptr_t)time;
2470 rule->arg.act.p[1] = (void *)(uintptr_t)bytes;
2471
2472 *orig_arg = cur_arg;
2473
2474 rule->action = ACT_CUSTOM;
2475 rule->action_ptr = http_action_wait_for_body;
2476 return ACT_RET_PRS_OK;
2477}
2478
Willy Tarreau79e57332018-10-02 16:01:16 +02002479/************************************************************************/
2480/* All supported http-request action keywords must be declared here. */
2481/************************************************************************/
2482
2483static struct action_kw_list http_req_actions = {
2484 .kw = {
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02002485 { "add-acl", parse_http_set_map, KWF_MATCH_PREFIX },
Christopher Faulet81e20172019-12-12 16:40:30 +01002486 { "add-header", parse_http_set_header, 0 },
2487 { "allow", parse_http_allow, 0 },
2488 { "auth", parse_http_auth, 0 },
2489 { "capture", parse_http_req_capture, 0 },
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02002490 { "del-acl", parse_http_set_map, KWF_MATCH_PREFIX },
Christopher Faulet81e20172019-12-12 16:40:30 +01002491 { "del-header", parse_http_del_header, 0 },
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02002492 { "del-map", parse_http_set_map, KWF_MATCH_PREFIX },
Christopher Faulete0fca292020-01-13 21:49:03 +01002493 { "deny", parse_http_deny, 0 },
Christopher Faulet81e20172019-12-12 16:40:30 +01002494 { "disable-l7-retry", parse_http_req_disable_l7_retry, 0 },
2495 { "early-hint", parse_http_set_header, 0 },
Amaury Denoyellea9e639a2021-05-06 15:50:12 +02002496 { "normalize-uri", parse_http_normalize_uri, KWF_EXPERIMENTAL },
Christopher Faulet81e20172019-12-12 16:40:30 +01002497 { "redirect", parse_http_redirect, 0 },
2498 { "reject", parse_http_action_reject, 0 },
2499 { "replace-header", parse_http_replace_header, 0 },
2500 { "replace-path", parse_replace_uri, 0 },
Christopher Faulet312294f2020-09-02 17:17:44 +02002501 { "replace-pathq", parse_replace_uri, 0 },
Christopher Faulet81e20172019-12-12 16:40:30 +01002502 { "replace-uri", parse_replace_uri, 0 },
2503 { "replace-value", parse_http_replace_header, 0 },
Christopher Faulet24231ab2020-01-24 17:44:23 +01002504 { "return", parse_http_return, 0 },
Christopher Faulet81e20172019-12-12 16:40:30 +01002505 { "set-header", parse_http_set_header, 0 },
2506 { "set-log-level", parse_http_set_log_level, 0 },
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02002507 { "set-map", parse_http_set_map, KWF_MATCH_PREFIX },
Christopher Faulet81e20172019-12-12 16:40:30 +01002508 { "set-method", parse_set_req_line, 0 },
2509 { "set-mark", parse_http_set_mark, 0 },
2510 { "set-nice", parse_http_set_nice, 0 },
2511 { "set-path", parse_set_req_line, 0 },
Christopher Faulet312294f2020-09-02 17:17:44 +02002512 { "set-pathq", parse_set_req_line, 0 },
Christopher Faulet81e20172019-12-12 16:40:30 +01002513 { "set-query", parse_set_req_line, 0 },
2514 { "set-tos", parse_http_set_tos, 0 },
2515 { "set-uri", parse_set_req_line, 0 },
Christopher Faulet46f95542019-12-20 10:07:22 +01002516 { "strict-mode", parse_http_strict_mode, 0 },
Christopher Faulete0fca292020-01-13 21:49:03 +01002517 { "tarpit", parse_http_deny, 0 },
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02002518 { "track-sc", parse_http_track_sc, KWF_MATCH_PREFIX },
Amaury Denoyelle8d228232020-12-10 13:43:54 +01002519 { "set-timeout", parse_http_set_timeout, 0 },
Christopher Faulet021a8e42021-03-29 10:46:38 +02002520 { "wait-for-body", parse_http_wait_for_body, 0 },
Willy Tarreau79e57332018-10-02 16:01:16 +02002521 { NULL, NULL }
2522 }
2523};
2524
Willy Tarreau0108d902018-11-25 19:14:37 +01002525INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_actions);
2526
Willy Tarreau79e57332018-10-02 16:01:16 +02002527static struct action_kw_list http_res_actions = {
2528 .kw = {
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02002529 { "add-acl", parse_http_set_map, KWF_MATCH_PREFIX },
Christopher Faulet81e20172019-12-12 16:40:30 +01002530 { "add-header", parse_http_set_header, 0 },
2531 { "allow", parse_http_allow, 0 },
2532 { "capture", parse_http_res_capture, 0 },
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02002533 { "del-acl", parse_http_set_map, KWF_MATCH_PREFIX },
Christopher Faulet81e20172019-12-12 16:40:30 +01002534 { "del-header", parse_http_del_header, 0 },
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02002535 { "del-map", parse_http_set_map, KWF_MATCH_PREFIX },
Christopher Faulete0fca292020-01-13 21:49:03 +01002536 { "deny", parse_http_deny, 0 },
Christopher Faulet81e20172019-12-12 16:40:30 +01002537 { "redirect", parse_http_redirect, 0 },
2538 { "replace-header", parse_http_replace_header, 0 },
2539 { "replace-value", parse_http_replace_header, 0 },
Christopher Faulet24231ab2020-01-24 17:44:23 +01002540 { "return", parse_http_return, 0 },
Christopher Faulet81e20172019-12-12 16:40:30 +01002541 { "set-header", parse_http_set_header, 0 },
2542 { "set-log-level", parse_http_set_log_level, 0 },
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02002543 { "set-map", parse_http_set_map, KWF_MATCH_PREFIX },
Christopher Faulet81e20172019-12-12 16:40:30 +01002544 { "set-mark", parse_http_set_mark, 0 },
2545 { "set-nice", parse_http_set_nice, 0 },
2546 { "set-status", parse_http_set_status, 0 },
2547 { "set-tos", parse_http_set_tos, 0 },
Christopher Faulet46f95542019-12-20 10:07:22 +01002548 { "strict-mode", parse_http_strict_mode, 0 },
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02002549 { "track-sc", parse_http_track_sc, KWF_MATCH_PREFIX },
Christopher Faulet021a8e42021-03-29 10:46:38 +02002550 { "wait-for-body", parse_http_wait_for_body, 0 },
Willy Tarreau79e57332018-10-02 16:01:16 +02002551 { NULL, NULL }
2552 }
2553};
2554
Willy Tarreau0108d902018-11-25 19:14:37 +01002555INITCALL1(STG_REGISTER, http_res_keywords_register, &http_res_actions);
Willy Tarreau79e57332018-10-02 16:01:16 +02002556
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002557static struct action_kw_list http_after_res_actions = {
2558 .kw = {
2559 { "add-header", parse_http_set_header, 0 },
2560 { "allow", parse_http_allow, 0 },
2561 { "del-header", parse_http_del_header, 0 },
2562 { "replace-header", parse_http_replace_header, 0 },
2563 { "replace-value", parse_http_replace_header, 0 },
2564 { "set-header", parse_http_set_header, 0 },
2565 { "set-status", parse_http_set_status, 0 },
2566 { "strict-mode", parse_http_strict_mode, 0 },
2567 { NULL, NULL }
2568 }
2569};
2570
2571INITCALL1(STG_REGISTER, http_after_res_keywords_register, &http_after_res_actions);
2572
Willy Tarreau79e57332018-10-02 16:01:16 +02002573/*
2574 * Local variables:
2575 * c-indent-level: 8
2576 * c-basic-offset: 8
2577 * End:
2578 */