blob: 8574a7de011d69b48f2b9f9c4d0d278f92f4ae52 [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;
Christopher Faulet5f802b32021-10-13 17:22:17 +0200157 int cap = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200158
Willy Tarreau79e57332018-10-02 16:01:16 +0200159 switch (args[0][4]) {
160 case 'm' :
Christopher Faulet2c22a692019-12-18 15:39:56 +0100161 rule->action = 0; // set-method
Willy Tarreau79e57332018-10-02 16:01:16 +0200162 break;
163 case 'p' :
Christopher Faulet312294f2020-09-02 17:17:44 +0200164 if (args[0][8] == 'q')
165 rule->action = 4; // set-pathq
166 else
167 rule->action = 1; // set-path
Willy Tarreau79e57332018-10-02 16:01:16 +0200168 break;
169 case 'q' :
Christopher Faulet2c22a692019-12-18 15:39:56 +0100170 rule->action = 2; // set-query
Willy Tarreau79e57332018-10-02 16:01:16 +0200171 break;
172 case 'u' :
Christopher Faulet2c22a692019-12-18 15:39:56 +0100173 rule->action = 3; // set-uri
Willy Tarreau79e57332018-10-02 16:01:16 +0200174 break;
175 default:
176 memprintf(err, "internal error: unhandled action '%s'", args[0]);
177 return ACT_RET_PRS_ERR;
178 }
Christopher Faulet96bff762019-12-17 13:46:18 +0100179 rule->action_ptr = http_action_set_req_line;
Christopher Faulet2eb53962020-01-14 14:47:34 +0100180 rule->release_ptr = release_http_action;
Willy Tarreau79e57332018-10-02 16:01:16 +0200181
182 if (!*args[cur_arg] ||
183 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
184 memprintf(err, "expects exactly 1 argument <format>");
185 return ACT_RET_PRS_ERR;
186 }
187
Christopher Faulet96bff762019-12-17 13:46:18 +0100188 LIST_INIT(&rule->arg.http.fmt);
Willy Tarreau79e57332018-10-02 16:01:16 +0200189 px->conf.args.ctx = ARGC_HRQ;
Christopher Faulet5f802b32021-10-13 17:22:17 +0200190 if (px->cap & PR_CAP_FE)
191 cap |= SMP_VAL_FE_HRQ_HDR;
192 if (px->cap & PR_CAP_BE)
193 cap |= SMP_VAL_BE_HRQ_HDR;
194 if (!parse_logformat_string(args[cur_arg], px, &rule->arg.http.fmt, LOG_OPT_HTTP, cap, err)) {
Willy Tarreau79e57332018-10-02 16:01:16 +0200195 return ACT_RET_PRS_ERR;
196 }
197
198 (*orig_arg)++;
199 return ACT_RET_PRS_OK;
200}
201
Tim Duesterhusd2bedcc2021-04-15 21:45:57 +0200202/* This function executes the http-request normalize-uri action.
203 * `rule->action` is expected to be a value from `enum act_normalize_uri`.
204 *
205 * On success, it returns ACT_RET_CONT. If an error
206 * occurs while soft rewrites are enabled, the action is canceled, but the rule
207 * processing continue. Otherwsize ACT_RET_ERR is returned.
208 */
209static enum act_return http_action_normalize_uri(struct act_rule *rule, struct proxy *px,
210 struct session *sess, struct stream *s, int flags)
211{
212 enum act_return ret = ACT_RET_CONT;
213 struct htx *htx = htxbuf(&s->req.buf);
214 const struct ist uri = htx_sl_req_uri(http_get_stline(htx));
215 struct buffer *replace = alloc_trash_chunk();
216 enum uri_normalizer_err err = URI_NORMALIZER_ERR_INTERNAL_ERROR;
217
218 if (!replace)
219 goto fail_alloc;
220
221 switch ((enum act_normalize_uri) rule->action) {
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200222 case ACT_NORMALIZE_URI_PATH_MERGE_SLASHES: {
Tim Duesterhusd371e992021-04-15 21:45:58 +0200223 const struct ist path = http_get_path(uri);
224 struct ist newpath = ist2(replace->area, replace->size);
225
226 if (!isttest(path))
227 goto leave;
228
229 err = uri_normalizer_path_merge_slashes(iststop(path, '?'), &newpath);
230
231 if (err != URI_NORMALIZER_ERR_NONE)
232 break;
233
234 if (!http_replace_req_path(htx, newpath, 0))
235 goto fail_rewrite;
236
237 break;
238 }
Maximilian Maderff3bb8b2021-04-21 00:22:50 +0200239 case ACT_NORMALIZE_URI_PATH_STRIP_DOT: {
240 const struct ist path = http_get_path(uri);
241 struct ist newpath = ist2(replace->area, replace->size);
242
243 if (!isttest(path))
244 goto leave;
245
246 err = uri_normalizer_path_dot(iststop(path, '?'), &newpath);
247
248 if (err != URI_NORMALIZER_ERR_NONE)
249 break;
250
251 if (!http_replace_req_path(htx, newpath, 0))
252 goto fail_rewrite;
253
254 break;
255 }
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200256 case ACT_NORMALIZE_URI_PATH_STRIP_DOTDOT:
257 case ACT_NORMALIZE_URI_PATH_STRIP_DOTDOT_FULL: {
Tim Duesterhus9982fc22021-04-15 21:45:59 +0200258 const struct ist path = http_get_path(uri);
259 struct ist newpath = ist2(replace->area, replace->size);
260
261 if (!isttest(path))
262 goto leave;
263
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200264 err = uri_normalizer_path_dotdot(iststop(path, '?'), rule->action == ACT_NORMALIZE_URI_PATH_STRIP_DOTDOT_FULL, &newpath);
Tim Duesterhus9982fc22021-04-15 21:45:59 +0200265
266 if (err != URI_NORMALIZER_ERR_NONE)
267 break;
268
269 if (!http_replace_req_path(htx, newpath, 0))
270 goto fail_rewrite;
271
272 break;
273 }
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200274 case ACT_NORMALIZE_URI_QUERY_SORT_BY_NAME: {
Tim Duesterhusd7b89be2021-04-15 21:46:01 +0200275 const struct ist path = http_get_path(uri);
276 struct ist newquery = ist2(replace->area, replace->size);
277
278 if (!isttest(path))
279 goto leave;
280
281 err = uri_normalizer_query_sort(istfind(path, '?'), '&', &newquery);
282
283 if (err != URI_NORMALIZER_ERR_NONE)
284 break;
285
286 if (!http_replace_req_query(htx, newquery))
287 goto fail_rewrite;
288
289 break;
290 }
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200291 case ACT_NORMALIZE_URI_PERCENT_TO_UPPERCASE:
292 case ACT_NORMALIZE_URI_PERCENT_TO_UPPERCASE_STRICT: {
Tim Duesterhusa4071932021-04-15 21:46:02 +0200293 const struct ist path = http_get_path(uri);
294 struct ist newpath = ist2(replace->area, replace->size);
295
296 if (!isttest(path))
297 goto leave;
298
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200299 err = uri_normalizer_percent_upper(path, rule->action == ACT_NORMALIZE_URI_PERCENT_TO_UPPERCASE_STRICT, &newpath);
Tim Duesterhusa4071932021-04-15 21:46:02 +0200300
301 if (err != URI_NORMALIZER_ERR_NONE)
302 break;
303
304 if (!http_replace_req_path(htx, newpath, 1))
305 goto fail_rewrite;
306
307 break;
308 }
Tim Duesterhus2e4a18e2021-04-21 21:20:36 +0200309 case ACT_NORMALIZE_URI_PERCENT_DECODE_UNRESERVED:
310 case ACT_NORMALIZE_URI_PERCENT_DECODE_UNRESERVED_STRICT: {
311 const struct ist path = http_get_path(uri);
312 struct ist newpath = ist2(replace->area, replace->size);
313
314 if (!isttest(path))
315 goto leave;
316
317 err = uri_normalizer_percent_decode_unreserved(path, rule->action == ACT_NORMALIZE_URI_PERCENT_DECODE_UNRESERVED_STRICT, &newpath);
318
319 if (err != URI_NORMALIZER_ERR_NONE)
320 break;
321
322 if (!http_replace_req_path(htx, newpath, 1))
323 goto fail_rewrite;
324
325 break;
326 }
Tim Duesterhusc9e05ab2021-05-10 17:28:25 +0200327 case ACT_NORMALIZE_URI_FRAGMENT_STRIP: {
328 const struct ist path = http_get_path(uri);
329 struct ist newpath = ist2(replace->area, replace->size);
330
331 if (!isttest(path))
332 goto leave;
333
334 err = uri_normalizer_fragment_strip(path, &newpath);
335
336 if (err != URI_NORMALIZER_ERR_NONE)
337 break;
338
339 if (!http_replace_req_path(htx, newpath, 1))
340 goto fail_rewrite;
341
342 break;
343 }
Tim Duesterhusdec1c362021-05-10 17:28:26 +0200344 case ACT_NORMALIZE_URI_FRAGMENT_ENCODE: {
345 const struct ist path = http_get_path(uri);
346 struct ist newpath = ist2(replace->area, replace->size);
347
348 if (!isttest(path))
349 goto leave;
350
351 err = uri_normalizer_fragment_encode(path, &newpath);
352
353 if (err != URI_NORMALIZER_ERR_NONE)
354 break;
355
356 if (!http_replace_req_path(htx, newpath, 1))
357 goto fail_rewrite;
358
359 break;
360 }
Tim Duesterhusd2bedcc2021-04-15 21:45:57 +0200361 }
362
363 switch (err) {
364 case URI_NORMALIZER_ERR_NONE:
365 break;
366 case URI_NORMALIZER_ERR_INTERNAL_ERROR:
367 ret = ACT_RET_ERR;
368 break;
369 case URI_NORMALIZER_ERR_INVALID_INPUT:
370 ret = ACT_RET_INV;
371 break;
372 case URI_NORMALIZER_ERR_ALLOC:
373 goto fail_alloc;
374 }
375
376 leave:
377 free_trash_chunk(replace);
378 return ret;
379
380 fail_alloc:
381 if (!(s->flags & SF_ERR_MASK))
382 s->flags |= SF_ERR_RESOURCE;
383 ret = ACT_RET_ERR;
384 goto leave;
385
386 fail_rewrite:
387 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_rewrites, 1);
388 if (s->flags & SF_BE_ASSIGNED)
389 _HA_ATOMIC_ADD(&s->be->be_counters.failed_rewrites, 1);
390 if (sess->listener && sess->listener->counters)
391 _HA_ATOMIC_ADD(&sess->listener->counters->failed_rewrites, 1);
392 if (objt_server(s->target))
393 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_rewrites, 1);
394
395 if (!(s->txn->req.flags & HTTP_MSGF_SOFT_RW)) {
396 ret = ACT_RET_ERR;
397 if (!(s->flags & SF_ERR_MASK))
398 s->flags |= SF_ERR_PRXCOND;
399 }
400 goto leave;
401}
402
403/* Parses the http-request normalize-uri action. It expects a single <normalizer>
404 * argument, corresponding too a value in `enum act_normalize_uri`.
405 *
406 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
407 */
408static enum act_parse_ret parse_http_normalize_uri(const char **args, int *orig_arg, struct proxy *px,
409 struct act_rule *rule, char **err)
410{
411 int cur_arg = *orig_arg;
412
413 rule->action_ptr = http_action_normalize_uri;
414 rule->release_ptr = NULL;
415
416 if (!*args[cur_arg]) {
417 memprintf(err, "missing argument <normalizer>");
418 return ACT_RET_PRS_ERR;
419 }
420
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200421 if (strcmp(args[cur_arg], "path-merge-slashes") == 0) {
Tim Duesterhusd371e992021-04-15 21:45:58 +0200422 cur_arg++;
Tim Duesterhusd2bedcc2021-04-15 21:45:57 +0200423
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200424 rule->action = ACT_NORMALIZE_URI_PATH_MERGE_SLASHES;
Tim Duesterhusd2bedcc2021-04-15 21:45:57 +0200425 }
Maximilian Maderff3bb8b2021-04-21 00:22:50 +0200426 else if (strcmp(args[cur_arg], "path-strip-dot") == 0) {
427 cur_arg++;
428
429 rule->action = ACT_NORMALIZE_URI_PATH_STRIP_DOT;
430 }
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200431 else if (strcmp(args[cur_arg], "path-strip-dotdot") == 0) {
Tim Duesterhus9982fc22021-04-15 21:45:59 +0200432 cur_arg++;
433
Tim Duesterhus560e1a62021-04-15 21:46:00 +0200434 if (strcmp(args[cur_arg], "full") == 0) {
435 cur_arg++;
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200436 rule->action = ACT_NORMALIZE_URI_PATH_STRIP_DOTDOT_FULL;
Tim Duesterhus560e1a62021-04-15 21:46:00 +0200437 }
438 else if (!*args[cur_arg]) {
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200439 rule->action = ACT_NORMALIZE_URI_PATH_STRIP_DOTDOT;
Tim Duesterhus560e1a62021-04-15 21:46:00 +0200440 }
441 else if (strcmp(args[cur_arg], "if") != 0 && strcmp(args[cur_arg], "unless") != 0) {
Tim Duesterhus2f413132021-05-10 23:21:20 +0200442 memprintf(err, "unknown argument '%s' for 'path-strip-dotdot' normalizer", args[cur_arg]);
Tim Duesterhus560e1a62021-04-15 21:46:00 +0200443 return ACT_RET_PRS_ERR;
444 }
Tim Duesterhus9982fc22021-04-15 21:45:59 +0200445 }
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200446 else if (strcmp(args[cur_arg], "query-sort-by-name") == 0) {
Tim Duesterhusd7b89be2021-04-15 21:46:01 +0200447 cur_arg++;
448
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200449 rule->action = ACT_NORMALIZE_URI_QUERY_SORT_BY_NAME;
Tim Duesterhusd7b89be2021-04-15 21:46:01 +0200450 }
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200451 else if (strcmp(args[cur_arg], "percent-to-uppercase") == 0) {
Tim Duesterhusa4071932021-04-15 21:46:02 +0200452 cur_arg++;
453
454 if (strcmp(args[cur_arg], "strict") == 0) {
455 cur_arg++;
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200456 rule->action = ACT_NORMALIZE_URI_PERCENT_TO_UPPERCASE_STRICT;
Tim Duesterhusa4071932021-04-15 21:46:02 +0200457 }
458 else if (!*args[cur_arg]) {
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200459 rule->action = ACT_NORMALIZE_URI_PERCENT_TO_UPPERCASE;
Tim Duesterhusa4071932021-04-15 21:46:02 +0200460 }
461 else if (strcmp(args[cur_arg], "if") != 0 && strcmp(args[cur_arg], "unless") != 0) {
Tim Duesterhus2f413132021-05-10 23:21:20 +0200462 memprintf(err, "unknown argument '%s' for 'percent-to-uppercase' normalizer", args[cur_arg]);
Tim Duesterhusa4071932021-04-15 21:46:02 +0200463 return ACT_RET_PRS_ERR;
464 }
465 }
Tim Duesterhus2e4a18e2021-04-21 21:20:36 +0200466 else if (strcmp(args[cur_arg], "percent-decode-unreserved") == 0) {
467 cur_arg++;
468
469 if (strcmp(args[cur_arg], "strict") == 0) {
470 cur_arg++;
471 rule->action = ACT_NORMALIZE_URI_PERCENT_DECODE_UNRESERVED_STRICT;
472 }
473 else if (!*args[cur_arg]) {
474 rule->action = ACT_NORMALIZE_URI_PERCENT_DECODE_UNRESERVED;
475 }
476 else if (strcmp(args[cur_arg], "if") != 0 && strcmp(args[cur_arg], "unless") != 0) {
477 memprintf(err, "unknown argument '%s' for 'percent-decode-unreserved' normalizer", args[cur_arg]);
478 return ACT_RET_PRS_ERR;
479 }
480 }
Tim Duesterhusc9e05ab2021-05-10 17:28:25 +0200481 else if (strcmp(args[cur_arg], "fragment-strip") == 0) {
482 cur_arg++;
483
484 rule->action = ACT_NORMALIZE_URI_FRAGMENT_STRIP;
485 }
Tim Duesterhusdec1c362021-05-10 17:28:26 +0200486 else if (strcmp(args[cur_arg], "fragment-encode") == 0) {
487 cur_arg++;
488
489 rule->action = ACT_NORMALIZE_URI_FRAGMENT_ENCODE;
490 }
Tim Duesterhusd2bedcc2021-04-15 21:45:57 +0200491 else {
492 memprintf(err, "unknown normalizer '%s'", args[cur_arg]);
493 return ACT_RET_PRS_ERR;
494 }
495
496 *orig_arg = cur_arg;
497 return ACT_RET_PRS_OK;
498}
499
Willy Tarreau33810222019-06-12 17:44:02 +0200500/* This function executes a replace-uri action. It finds its arguments in
Christopher Faulet96bff762019-12-17 13:46:18 +0100501 * <rule>.arg.http. It builds a string in the trash from the format string
Willy Tarreau33810222019-06-12 17:44:02 +0200502 * previously filled by function parse_replace_uri() and will execute the regex
Christopher Faulet96bff762019-12-17 13:46:18 +0100503 * in <http.re> to replace the URI. It uses the format string present in
Christopher Faulet2c22a692019-12-18 15:39:56 +0100504 * <http.fmt>. The component to act on (path/uri) is taken from <.action> which
Christopher Faulet96bff762019-12-17 13:46:18 +0100505 * contains 1 for the path or 3 for the URI (values used by
506 * http_req_replace_stline()). On success, it returns ACT_RET_CONT. If an error
507 * occurs while soft rewrites are enabled, the action is canceled, but the rule
508 * processing continue. Otherwsize ACT_RET_ERR is returned.
Willy Tarreau33810222019-06-12 17:44:02 +0200509 */
510static enum act_return http_action_replace_uri(struct act_rule *rule, struct proxy *px,
511 struct session *sess, struct stream *s, int flags)
512{
Christopher Faulet13403762019-12-13 09:01:57 +0100513 enum act_return ret = ACT_RET_CONT;
Willy Tarreau33810222019-06-12 17:44:02 +0200514 struct buffer *replace, *output;
515 struct ist uri;
516 int len;
517
518 replace = alloc_trash_chunk();
519 output = alloc_trash_chunk();
520 if (!replace || !output)
Christopher Faulete00d06c2019-12-16 17:18:42 +0100521 goto fail_alloc;
Christopher Faulet12c28b62019-07-15 16:30:24 +0200522 uri = htx_sl_req_uri(http_get_stline(htxbuf(&s->req.buf)));
Willy Tarreau262c3f12019-12-17 06:52:51 +0100523
Christopher Faulet1fa0cc12020-09-02 11:10:38 +0200524 if (rule->action == 1) // replace-path
525 uri = iststop(http_get_path(uri), '?');
Christopher Faulet312294f2020-09-02 17:17:44 +0200526 else if (rule->action == 4) // replace-pathq
527 uri = http_get_path(uri);
Willy Tarreau262c3f12019-12-17 06:52:51 +0100528
Christopher Faulet5d242692022-04-08 10:44:21 +0200529 if (!istlen(uri))
530 goto leave;
531
Christopher Faulet96bff762019-12-17 13:46:18 +0100532 if (!regex_exec_match2(rule->arg.http.re, uri.ptr, uri.len, MAX_MATCH, pmatch, 0))
Willy Tarreau33810222019-06-12 17:44:02 +0200533 goto leave;
534
Christopher Faulet96bff762019-12-17 13:46:18 +0100535 replace->data = build_logline(s, replace->area, replace->size, &rule->arg.http.fmt);
Willy Tarreau33810222019-06-12 17:44:02 +0200536
537 /* note: uri.ptr doesn't need to be zero-terminated because it will
538 * only be used to pick pmatch references.
539 */
540 len = exp_replace(output->area, output->size, uri.ptr, replace->area, pmatch);
541 if (len == -1)
Christopher Faulete00d06c2019-12-16 17:18:42 +0100542 goto fail_rewrite;
Willy Tarreau33810222019-06-12 17:44:02 +0200543
Christopher Faulet2c22a692019-12-18 15:39:56 +0100544 if (http_req_replace_stline(rule->action, output->area, len, px, s) == -1)
Christopher Faulete00d06c2019-12-16 17:18:42 +0100545 goto fail_rewrite;
Willy Tarreau33810222019-06-12 17:44:02 +0200546
Christopher Faulete00d06c2019-12-16 17:18:42 +0100547 leave:
Willy Tarreau33810222019-06-12 17:44:02 +0200548 free_trash_chunk(output);
549 free_trash_chunk(replace);
550 return ret;
Christopher Faulete00d06c2019-12-16 17:18:42 +0100551
552 fail_alloc:
553 if (!(s->flags & SF_ERR_MASK))
554 s->flags |= SF_ERR_RESOURCE;
555 ret = ACT_RET_ERR;
556 goto leave;
557
558 fail_rewrite:
Willy Tarreau4781b152021-04-06 13:53:36 +0200559 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_rewrites);
Christopher Faulete00d06c2019-12-16 17:18:42 +0100560 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200561 _HA_ATOMIC_INC(&s->be->be_counters.failed_rewrites);
William Lallemand36119de2021-03-08 15:26:48 +0100562 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200563 _HA_ATOMIC_INC(&sess->listener->counters->failed_rewrites);
Christopher Faulete00d06c2019-12-16 17:18:42 +0100564 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +0200565 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_rewrites);
Christopher Faulete00d06c2019-12-16 17:18:42 +0100566
Christopher Faulet333bf8c2020-01-22 14:38:05 +0100567 if (!(s->txn->req.flags & HTTP_MSGF_SOFT_RW)) {
Christopher Faulete00d06c2019-12-16 17:18:42 +0100568 ret = ACT_RET_ERR;
Christopher Faulet333bf8c2020-01-22 14:38:05 +0100569 if (!(s->flags & SF_ERR_MASK))
570 s->flags |= SF_ERR_PRXCOND;
571 }
Christopher Faulete00d06c2019-12-16 17:18:42 +0100572 goto leave;
Willy Tarreau33810222019-06-12 17:44:02 +0200573}
574
Christopher Faulet312294f2020-09-02 17:17:44 +0200575/* parse a "replace-uri", "replace-path" or "replace-pathq"
576 * http-request action.
Willy Tarreau33810222019-06-12 17:44:02 +0200577 * This action takes 2 arguments (a regex and a replacement format string).
Christopher Faulet2c22a692019-12-18 15:39:56 +0100578 * The resulting rule makes use of <.action> to store the action (1/3 for now),
Christopher Faulet96bff762019-12-17 13:46:18 +0100579 * <http.re> to store the compiled regex, and <http.fmt> to store the log-format
Willy Tarreau33810222019-06-12 17:44:02 +0200580 * list head. It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
581 */
582static enum act_parse_ret parse_replace_uri(const char **args, int *orig_arg, struct proxy *px,
583 struct act_rule *rule, char **err)
584{
585 int cur_arg = *orig_arg;
Christopher Faulet5f802b32021-10-13 17:22:17 +0200586 int cap = 0;
Willy Tarreau33810222019-06-12 17:44:02 +0200587 char *error = NULL;
588
Christopher Faulet312294f2020-09-02 17:17:44 +0200589 switch (args[0][8]) {
590 case 'p':
591 if (args[0][12] == 'q')
592 rule->action = 4; // replace-pathq, same as set-pathq
593 else
594 rule->action = 1; // replace-path, same as set-path
595 break;
596 case 'u':
Christopher Faulet2c22a692019-12-18 15:39:56 +0100597 rule->action = 3; // replace-uri, same as set-uri
Christopher Faulet312294f2020-09-02 17:17:44 +0200598 break;
599 default:
600 memprintf(err, "internal error: unhandled action '%s'", args[0]);
601 return ACT_RET_PRS_ERR;
602 }
Willy Tarreau262c3f12019-12-17 06:52:51 +0100603
Willy Tarreau33810222019-06-12 17:44:02 +0200604 rule->action_ptr = http_action_replace_uri;
Christopher Faulet2eb53962020-01-14 14:47:34 +0100605 rule->release_ptr = release_http_action;
Willy Tarreau33810222019-06-12 17:44:02 +0200606
607 if (!*args[cur_arg] || !*args[cur_arg+1] ||
608 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
609 memprintf(err, "expects exactly 2 arguments <match-regex> and <replace-format>");
610 return ACT_RET_PRS_ERR;
611 }
612
Christopher Faulet96bff762019-12-17 13:46:18 +0100613 if (!(rule->arg.http.re = regex_comp(args[cur_arg], 1, 1, &error))) {
Willy Tarreau33810222019-06-12 17:44:02 +0200614 memprintf(err, "failed to parse the regex : %s", error);
615 free(error);
616 return ACT_RET_PRS_ERR;
617 }
618
Christopher Faulet96bff762019-12-17 13:46:18 +0100619 LIST_INIT(&rule->arg.http.fmt);
Willy Tarreau33810222019-06-12 17:44:02 +0200620 px->conf.args.ctx = ARGC_HRQ;
Christopher Faulet5f802b32021-10-13 17:22:17 +0200621 if (px->cap & PR_CAP_FE)
622 cap |= SMP_VAL_FE_HRQ_HDR;
623 if (px->cap & PR_CAP_BE)
624 cap |= SMP_VAL_BE_HRQ_HDR;
625 if (!parse_logformat_string(args[cur_arg + 1], px, &rule->arg.http.fmt, LOG_OPT_HTTP, cap, err)) {
Christopher Faulet1337b322020-01-14 14:50:55 +0100626 regex_free(rule->arg.http.re);
Willy Tarreau33810222019-06-12 17:44:02 +0200627 return ACT_RET_PRS_ERR;
628 }
629
630 (*orig_arg) += 2;
631 return ACT_RET_PRS_OK;
632}
633
Willy Tarreau79e57332018-10-02 16:01:16 +0200634/* This function is just a compliant action wrapper for "set-status". */
635static enum act_return action_http_set_status(struct act_rule *rule, struct proxy *px,
636 struct session *sess, struct stream *s, int flags)
637{
Christopher Faulet96bff762019-12-17 13:46:18 +0100638 if (http_res_set_status(rule->arg.http.i, rule->arg.http.str, s) == -1) {
Willy Tarreau4781b152021-04-06 13:53:36 +0200639 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_rewrites);
Christopher Faulete00d06c2019-12-16 17:18:42 +0100640 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200641 _HA_ATOMIC_INC(&s->be->be_counters.failed_rewrites);
William Lallemand36119de2021-03-08 15:26:48 +0100642 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200643 _HA_ATOMIC_INC(&sess->listener->counters->failed_rewrites);
Christopher Faulete00d06c2019-12-16 17:18:42 +0100644 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +0200645 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_rewrites);
Christopher Faulete00d06c2019-12-16 17:18:42 +0100646
Christopher Faulet333bf8c2020-01-22 14:38:05 +0100647 if (!(s->txn->req.flags & HTTP_MSGF_SOFT_RW)) {
Christopher Faulet333bf8c2020-01-22 14:38:05 +0100648 if (!(s->flags & SF_ERR_MASK))
649 s->flags |= SF_ERR_PRXCOND;
Christopher Faulet692a6c22020-02-07 10:22:31 +0100650 return ACT_RET_ERR;
Christopher Faulet333bf8c2020-01-22 14:38:05 +0100651 }
Christopher Faulete00d06c2019-12-16 17:18:42 +0100652 }
653
Willy Tarreau79e57332018-10-02 16:01:16 +0200654 return ACT_RET_CONT;
655}
656
657/* parse set-status action:
658 * This action accepts a single argument of type int representing
659 * an http status code. It returns ACT_RET_PRS_OK on success,
660 * ACT_RET_PRS_ERR on error.
661 */
662static enum act_parse_ret parse_http_set_status(const char **args, int *orig_arg, struct proxy *px,
663 struct act_rule *rule, char **err)
664{
665 char *error;
666
667 rule->action = ACT_CUSTOM;
668 rule->action_ptr = action_http_set_status;
Christopher Faulet2eb53962020-01-14 14:47:34 +0100669 rule->release_ptr = release_http_action;
Willy Tarreau79e57332018-10-02 16:01:16 +0200670
671 /* Check if an argument is available */
672 if (!*args[*orig_arg]) {
673 memprintf(err, "expects 1 argument: <status>; or 3 arguments: <status> reason <fmt>");
674 return ACT_RET_PRS_ERR;
675 }
676
677 /* convert status code as integer */
Christopher Faulet96bff762019-12-17 13:46:18 +0100678 rule->arg.http.i = strtol(args[*orig_arg], &error, 10);
679 if (*error != '\0' || rule->arg.http.i < 100 || rule->arg.http.i > 999) {
Willy Tarreau79e57332018-10-02 16:01:16 +0200680 memprintf(err, "expects an integer status code between 100 and 999");
681 return ACT_RET_PRS_ERR;
682 }
683
684 (*orig_arg)++;
685
686 /* set custom reason string */
Christopher Faulet96bff762019-12-17 13:46:18 +0100687 rule->arg.http.str = ist(NULL); // If null, we use the default reason for the status code.
Willy Tarreau79e57332018-10-02 16:01:16 +0200688 if (*args[*orig_arg] && strcmp(args[*orig_arg], "reason") == 0 &&
689 (*args[*orig_arg + 1] && strcmp(args[*orig_arg + 1], "if") != 0 && strcmp(args[*orig_arg + 1], "unless") != 0)) {
690 (*orig_arg)++;
Christopher Faulet96bff762019-12-17 13:46:18 +0100691 rule->arg.http.str.ptr = strdup(args[*orig_arg]);
692 rule->arg.http.str.len = strlen(rule->arg.http.str.ptr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200693 (*orig_arg)++;
694 }
695
Christopher Fauletc20b3712020-01-27 15:51:56 +0100696 LIST_INIT(&rule->arg.http.fmt);
Willy Tarreau79e57332018-10-02 16:01:16 +0200697 return ACT_RET_PRS_OK;
698}
699
700/* This function executes the "reject" HTTP action. It clears the request and
701 * response buffer without sending any response. It can be useful as an HTTP
702 * alternative to the silent-drop action to defend against DoS attacks, and may
703 * also be used with HTTP/2 to close a connection instead of just a stream.
704 * The txn status is unchanged, indicating no response was sent. The termination
Christopher Faulet90d22a82020-03-06 11:18:39 +0100705 * flags will indicate "PR". It always returns ACT_RET_ABRT.
Willy Tarreau79e57332018-10-02 16:01:16 +0200706 */
707static enum act_return http_action_reject(struct act_rule *rule, struct proxy *px,
708 struct session *sess, struct stream *s, int flags)
709{
Willy Tarreau0f9cd7b2019-01-31 19:02:43 +0100710 si_must_kill_conn(chn_prod(&s->req));
Willy Tarreau79e57332018-10-02 16:01:16 +0200711 channel_abort(&s->req);
712 channel_abort(&s->res);
Christopher Fauletd4a824e2020-03-06 15:07:09 +0100713 s->req.analysers &= AN_REQ_FLT_END;
714 s->res.analysers &= AN_RES_FLT_END;
Willy Tarreau79e57332018-10-02 16:01:16 +0200715
Willy Tarreau4781b152021-04-06 13:53:36 +0200716 _HA_ATOMIC_INC(&s->be->be_counters.denied_req);
717 _HA_ATOMIC_INC(&sess->fe->fe_counters.denied_req);
Willy Tarreau79e57332018-10-02 16:01:16 +0200718 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200719 _HA_ATOMIC_INC(&sess->listener->counters->denied_req);
Willy Tarreau79e57332018-10-02 16:01:16 +0200720
721 if (!(s->flags & SF_ERR_MASK))
722 s->flags |= SF_ERR_PRXCOND;
723 if (!(s->flags & SF_FINST_MASK))
724 s->flags |= SF_FINST_R;
725
Christopher Faulet90d22a82020-03-06 11:18:39 +0100726 return ACT_RET_ABRT;
Willy Tarreau79e57332018-10-02 16:01:16 +0200727}
728
729/* parse the "reject" action:
730 * This action takes no argument and returns ACT_RET_PRS_OK on success,
731 * ACT_RET_PRS_ERR on error.
732 */
733static enum act_parse_ret parse_http_action_reject(const char **args, int *orig_arg, struct proxy *px,
734 struct act_rule *rule, char **err)
735{
736 rule->action = ACT_CUSTOM;
737 rule->action_ptr = http_action_reject;
738 return ACT_RET_PRS_OK;
739}
740
Olivier Houchard602bf7d2019-05-10 13:59:15 +0200741/* This function executes the "disable-l7-retry" HTTP action.
742 * It disables L7 retries (all retry except for a connection failure). This
743 * can be useful for example to avoid retrying on POST requests.
744 * It just removes the L7 retry flag on the stream_interface, and always
745 * return ACT_RET_CONT;
746 */
747static enum act_return http_req_disable_l7_retry(struct act_rule *rule, struct proxy *px,
748 struct session *sess, struct stream *s, int flags)
749{
750 struct stream_interface *si = &s->si[1];
751
752 /* In theory, the SI_FL_L7_RETRY flags isn't set at this point, but
753 * let's be future-proof and remove it anyway.
754 */
755 si->flags &= ~SI_FL_L7_RETRY;
756 si->flags |= SI_FL_D_L7_RETRY;
757 return ACT_RET_CONT;
758}
759
760/* parse the "disable-l7-retry" action:
761 * This action takes no argument and returns ACT_RET_PRS_OK on success,
762 * ACT_RET_PRS_ERR on error.
763 */
764static enum act_parse_ret parse_http_req_disable_l7_retry(const char **args,
765 int *orig_args, struct proxy *px,
766 struct act_rule *rule, char **err)
767{
768 rule->action = ACT_CUSTOM;
769 rule->action_ptr = http_req_disable_l7_retry;
770 return ACT_RET_PRS_OK;
771}
772
Willy Tarreau79e57332018-10-02 16:01:16 +0200773/* This function executes the "capture" action. It executes a fetch expression,
774 * turns the result into a string and puts it in a capture slot. It always
775 * returns 1. If an error occurs the action is cancelled, but the rule
776 * processing continues.
777 */
778static enum act_return http_action_req_capture(struct act_rule *rule, struct proxy *px,
779 struct session *sess, struct stream *s, int flags)
780{
781 struct sample *key;
782 struct cap_hdr *h = rule->arg.cap.hdr;
783 char **cap = s->req_cap;
784 int len;
785
786 key = sample_fetch_as_type(s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.cap.expr, SMP_T_STR);
787 if (!key)
788 return ACT_RET_CONT;
789
790 if (cap[h->index] == NULL)
791 cap[h->index] = pool_alloc(h->pool);
792
793 if (cap[h->index] == NULL) /* no more capture memory */
794 return ACT_RET_CONT;
795
796 len = key->data.u.str.data;
797 if (len > h->len)
798 len = h->len;
799
800 memcpy(cap[h->index], key->data.u.str.area, len);
801 cap[h->index][len] = 0;
802 return ACT_RET_CONT;
803}
804
805/* This function executes the "capture" action and store the result in a
806 * capture slot if exists. It executes a fetch expression, turns the result
807 * into a string and puts it in a capture slot. It always returns 1. If an
808 * error occurs the action is cancelled, but the rule processing continues.
809 */
810static enum act_return http_action_req_capture_by_id(struct act_rule *rule, struct proxy *px,
811 struct session *sess, struct stream *s, int flags)
812{
813 struct sample *key;
814 struct cap_hdr *h;
815 char **cap = s->req_cap;
816 struct proxy *fe = strm_fe(s);
817 int len;
818 int i;
819
820 /* Look for the original configuration. */
821 for (h = fe->req_cap, i = fe->nb_req_cap - 1;
822 h != NULL && i != rule->arg.capid.idx ;
823 i--, h = h->next);
824 if (!h)
825 return ACT_RET_CONT;
826
827 key = sample_fetch_as_type(s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.capid.expr, SMP_T_STR);
828 if (!key)
829 return ACT_RET_CONT;
830
831 if (cap[h->index] == NULL)
832 cap[h->index] = pool_alloc(h->pool);
833
834 if (cap[h->index] == NULL) /* no more capture memory */
835 return ACT_RET_CONT;
836
837 len = key->data.u.str.data;
838 if (len > h->len)
839 len = h->len;
840
841 memcpy(cap[h->index], key->data.u.str.area, len);
842 cap[h->index][len] = 0;
843 return ACT_RET_CONT;
844}
845
846/* Check an "http-request capture" action.
847 *
848 * The function returns 1 in success case, otherwise, it returns 0 and err is
849 * filled.
850 */
851static int check_http_req_capture(struct act_rule *rule, struct proxy *px, char **err)
852{
853 if (rule->action_ptr != http_action_req_capture_by_id)
854 return 1;
855
Baptiste Assmann19a69b32020-01-16 14:34:22 +0100856 /* capture slots can only be declared in frontends, so we can't check their
857 * existence in backends at configuration parsing step
858 */
859 if (px->cap & PR_CAP_FE && rule->arg.capid.idx >= px->nb_req_cap) {
Willy Tarreau79e57332018-10-02 16:01:16 +0200860 memprintf(err, "unable to find capture id '%d' referenced by http-request capture rule",
861 rule->arg.capid.idx);
862 return 0;
863 }
864
865 return 1;
866}
867
Christopher Faulet2eb53962020-01-14 14:47:34 +0100868/* Release memory allocate by an http capture action */
869static void release_http_capture(struct act_rule *rule)
870{
871 if (rule->action_ptr == http_action_req_capture)
872 release_sample_expr(rule->arg.cap.expr);
873 else
874 release_sample_expr(rule->arg.capid.expr);
875}
876
Willy Tarreau79e57332018-10-02 16:01:16 +0200877/* parse an "http-request capture" action. It takes a single argument which is
878 * a sample fetch expression. It stores the expression into arg->act.p[0] and
879 * the allocated hdr_cap struct or the preallocated "id" into arg->act.p[1].
880 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
881 */
882static enum act_parse_ret parse_http_req_capture(const char **args, int *orig_arg, struct proxy *px,
883 struct act_rule *rule, char **err)
884{
885 struct sample_expr *expr;
886 struct cap_hdr *hdr;
887 int cur_arg;
888 int len = 0;
889
890 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
891 if (strcmp(args[cur_arg], "if") == 0 ||
892 strcmp(args[cur_arg], "unless") == 0)
893 break;
894
895 if (cur_arg < *orig_arg + 3) {
896 memprintf(err, "expects <expression> [ 'len' <length> | id <idx> ]");
897 return ACT_RET_PRS_ERR;
898 }
899
900 cur_arg = *orig_arg;
Willy Tarreaue3b57bf2020-02-14 16:50:14 +0100901 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 +0200902 if (!expr)
903 return ACT_RET_PRS_ERR;
904
905 if (!(expr->fetch->val & SMP_VAL_FE_HRQ_HDR)) {
906 memprintf(err,
907 "fetch method '%s' extracts information from '%s', none of which is available here",
908 args[cur_arg-1], sample_src_names(expr->fetch->use));
Christopher Faulet1337b322020-01-14 14:50:55 +0100909 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200910 return ACT_RET_PRS_ERR;
911 }
912
913 if (!args[cur_arg] || !*args[cur_arg]) {
914 memprintf(err, "expects 'len or 'id'");
Christopher Faulet1337b322020-01-14 14:50:55 +0100915 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200916 return ACT_RET_PRS_ERR;
917 }
918
919 if (strcmp(args[cur_arg], "len") == 0) {
920 cur_arg++;
921
922 if (!(px->cap & PR_CAP_FE)) {
923 memprintf(err, "proxy '%s' has no frontend capability", px->id);
Christopher Faulet1337b322020-01-14 14:50:55 +0100924 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200925 return ACT_RET_PRS_ERR;
926 }
927
928 px->conf.args.ctx = ARGC_CAP;
929
930 if (!args[cur_arg]) {
931 memprintf(err, "missing length value");
Christopher Faulet1337b322020-01-14 14:50:55 +0100932 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200933 return ACT_RET_PRS_ERR;
934 }
935 /* we copy the table name for now, it will be resolved later */
936 len = atoi(args[cur_arg]);
937 if (len <= 0) {
938 memprintf(err, "length must be > 0");
Christopher Faulet1337b322020-01-14 14:50:55 +0100939 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200940 return ACT_RET_PRS_ERR;
941 }
942 cur_arg++;
943
Willy Tarreau79e57332018-10-02 16:01:16 +0200944 hdr = calloc(1, sizeof(*hdr));
Remi Tricot-Le Breton737d8b52021-05-12 17:54:17 +0200945 if (!hdr) {
946 memprintf(err, "out of memory");
947 release_sample_expr(expr);
948 return ACT_RET_PRS_ERR;
949 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200950 hdr->next = px->req_cap;
951 hdr->name = NULL; /* not a header capture */
952 hdr->namelen = 0;
953 hdr->len = len;
954 hdr->pool = create_pool("caphdr", hdr->len + 1, MEM_F_SHARED);
955 hdr->index = px->nb_req_cap++;
956
957 px->req_cap = hdr;
958 px->to_log |= LW_REQHDR;
959
960 rule->action = ACT_CUSTOM;
961 rule->action_ptr = http_action_req_capture;
Christopher Faulet2eb53962020-01-14 14:47:34 +0100962 rule->release_ptr = release_http_capture;
Willy Tarreau79e57332018-10-02 16:01:16 +0200963 rule->arg.cap.expr = expr;
964 rule->arg.cap.hdr = hdr;
965 }
966
967 else if (strcmp(args[cur_arg], "id") == 0) {
968 int id;
969 char *error;
970
971 cur_arg++;
972
973 if (!args[cur_arg]) {
974 memprintf(err, "missing id value");
Christopher Faulet1337b322020-01-14 14:50:55 +0100975 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200976 return ACT_RET_PRS_ERR;
977 }
978
979 id = strtol(args[cur_arg], &error, 10);
980 if (*error != '\0') {
981 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
Christopher Faulet1337b322020-01-14 14:50:55 +0100982 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200983 return ACT_RET_PRS_ERR;
984 }
985 cur_arg++;
986
987 px->conf.args.ctx = ARGC_CAP;
988
989 rule->action = ACT_CUSTOM;
990 rule->action_ptr = http_action_req_capture_by_id;
991 rule->check_ptr = check_http_req_capture;
Christopher Faulet2eb53962020-01-14 14:47:34 +0100992 rule->release_ptr = release_http_capture;
Willy Tarreau79e57332018-10-02 16:01:16 +0200993 rule->arg.capid.expr = expr;
994 rule->arg.capid.idx = id;
995 }
996
997 else {
998 memprintf(err, "expects 'len' or 'id', found '%s'", args[cur_arg]);
Christopher Faulet1337b322020-01-14 14:50:55 +0100999 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +02001000 return ACT_RET_PRS_ERR;
1001 }
1002
1003 *orig_arg = cur_arg;
1004 return ACT_RET_PRS_OK;
1005}
1006
1007/* This function executes the "capture" action and store the result in a
1008 * capture slot if exists. It executes a fetch expression, turns the result
1009 * into a string and puts it in a capture slot. It always returns 1. If an
1010 * error occurs the action is cancelled, but the rule processing continues.
1011 */
1012static enum act_return http_action_res_capture_by_id(struct act_rule *rule, struct proxy *px,
1013 struct session *sess, struct stream *s, int flags)
1014{
1015 struct sample *key;
1016 struct cap_hdr *h;
1017 char **cap = s->res_cap;
1018 struct proxy *fe = strm_fe(s);
1019 int len;
1020 int i;
1021
1022 /* Look for the original configuration. */
1023 for (h = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
1024 h != NULL && i != rule->arg.capid.idx ;
1025 i--, h = h->next);
1026 if (!h)
1027 return ACT_RET_CONT;
1028
1029 key = sample_fetch_as_type(s->be, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL, rule->arg.capid.expr, SMP_T_STR);
1030 if (!key)
1031 return ACT_RET_CONT;
1032
1033 if (cap[h->index] == NULL)
1034 cap[h->index] = pool_alloc(h->pool);
1035
1036 if (cap[h->index] == NULL) /* no more capture memory */
1037 return ACT_RET_CONT;
1038
1039 len = key->data.u.str.data;
1040 if (len > h->len)
1041 len = h->len;
1042
1043 memcpy(cap[h->index], key->data.u.str.area, len);
1044 cap[h->index][len] = 0;
1045 return ACT_RET_CONT;
1046}
1047
1048/* Check an "http-response capture" action.
1049 *
1050 * The function returns 1 in success case, otherwise, it returns 0 and err is
1051 * filled.
1052 */
1053static int check_http_res_capture(struct act_rule *rule, struct proxy *px, char **err)
1054{
1055 if (rule->action_ptr != http_action_res_capture_by_id)
1056 return 1;
1057
Tim Duesterhusf3f4aa02020-07-03 13:43:42 +02001058 /* capture slots can only be declared in frontends, so we can't check their
1059 * existence in backends at configuration parsing step
1060 */
1061 if (px->cap & PR_CAP_FE && rule->arg.capid.idx >= px->nb_rsp_cap) {
Willy Tarreau79e57332018-10-02 16:01:16 +02001062 memprintf(err, "unable to find capture id '%d' referenced by http-response capture rule",
1063 rule->arg.capid.idx);
1064 return 0;
1065 }
1066
1067 return 1;
1068}
1069
1070/* parse an "http-response capture" action. It takes a single argument which is
1071 * a sample fetch expression. It stores the expression into arg->act.p[0] and
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -07001072 * the allocated hdr_cap struct of the preallocated id into arg->act.p[1].
Willy Tarreau79e57332018-10-02 16:01:16 +02001073 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
1074 */
1075static enum act_parse_ret parse_http_res_capture(const char **args, int *orig_arg, struct proxy *px,
1076 struct act_rule *rule, char **err)
1077{
1078 struct sample_expr *expr;
1079 int cur_arg;
1080 int id;
1081 char *error;
1082
1083 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
1084 if (strcmp(args[cur_arg], "if") == 0 ||
1085 strcmp(args[cur_arg], "unless") == 0)
1086 break;
1087
1088 if (cur_arg < *orig_arg + 3) {
1089 memprintf(err, "expects <expression> id <idx>");
1090 return ACT_RET_PRS_ERR;
1091 }
1092
1093 cur_arg = *orig_arg;
Willy Tarreaue3b57bf2020-02-14 16:50:14 +01001094 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 +02001095 if (!expr)
1096 return ACT_RET_PRS_ERR;
1097
1098 if (!(expr->fetch->val & SMP_VAL_FE_HRS_HDR)) {
1099 memprintf(err,
1100 "fetch method '%s' extracts information from '%s', none of which is available here",
1101 args[cur_arg-1], sample_src_names(expr->fetch->use));
Christopher Faulet1337b322020-01-14 14:50:55 +01001102 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +02001103 return ACT_RET_PRS_ERR;
1104 }
1105
1106 if (!args[cur_arg] || !*args[cur_arg]) {
1107 memprintf(err, "expects 'id'");
Christopher Faulet1337b322020-01-14 14:50:55 +01001108 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +02001109 return ACT_RET_PRS_ERR;
1110 }
1111
1112 if (strcmp(args[cur_arg], "id") != 0) {
1113 memprintf(err, "expects 'id', found '%s'", args[cur_arg]);
Christopher Faulet1337b322020-01-14 14:50:55 +01001114 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +02001115 return ACT_RET_PRS_ERR;
1116 }
1117
1118 cur_arg++;
1119
1120 if (!args[cur_arg]) {
1121 memprintf(err, "missing id value");
Christopher Faulet1337b322020-01-14 14:50:55 +01001122 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +02001123 return ACT_RET_PRS_ERR;
1124 }
1125
1126 id = strtol(args[cur_arg], &error, 10);
1127 if (*error != '\0') {
1128 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
Christopher Faulet1337b322020-01-14 14:50:55 +01001129 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +02001130 return ACT_RET_PRS_ERR;
1131 }
1132 cur_arg++;
1133
1134 px->conf.args.ctx = ARGC_CAP;
1135
1136 rule->action = ACT_CUSTOM;
1137 rule->action_ptr = http_action_res_capture_by_id;
1138 rule->check_ptr = check_http_res_capture;
Christopher Faulet2eb53962020-01-14 14:47:34 +01001139 rule->release_ptr = release_http_capture;
Willy Tarreau79e57332018-10-02 16:01:16 +02001140 rule->arg.capid.expr = expr;
1141 rule->arg.capid.idx = id;
1142
1143 *orig_arg = cur_arg;
1144 return ACT_RET_PRS_OK;
1145}
1146
Christopher Faulet81e20172019-12-12 16:40:30 +01001147/* Parse a "allow" action for a request or a response rule. It takes no argument. It
1148 * returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
1149 */
1150static enum act_parse_ret parse_http_allow(const char **args, int *orig_arg, struct proxy *px,
1151 struct act_rule *rule, char **err)
1152{
1153 rule->action = ACT_ACTION_ALLOW;
Christopher Faulet245cf792019-12-18 14:58:12 +01001154 rule->flags |= ACT_FLAG_FINAL;
Christopher Faulet81e20172019-12-12 16:40:30 +01001155 return ACT_RET_PRS_OK;
1156}
1157
Christopher Faulete0fca292020-01-13 21:49:03 +01001158/* Parse "deny" or "tarpit" actions for a request rule or "deny" action for a
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001159 * response rule. It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on
1160 * error. It relies on http_parse_http_reply() to set
1161 * <.arg.http_reply>.
Christopher Faulet81e20172019-12-12 16:40:30 +01001162 */
Christopher Faulete0fca292020-01-13 21:49:03 +01001163static enum act_parse_ret parse_http_deny(const char **args, int *orig_arg, struct proxy *px,
1164 struct act_rule *rule, char **err)
Christopher Faulet81e20172019-12-12 16:40:30 +01001165{
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001166 int default_status;
1167 int cur_arg, arg = 0;
Christopher Faulet81e20172019-12-12 16:40:30 +01001168
1169 cur_arg = *orig_arg;
Christopher Faulete0fca292020-01-13 21:49:03 +01001170 if (rule->from == ACT_F_HTTP_REQ) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001171 if (strcmp(args[cur_arg - 1], "tarpit") == 0) {
Christopher Faulete0fca292020-01-13 21:49:03 +01001172 rule->action = ACT_HTTP_REQ_TARPIT;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001173 default_status = 500;
Christopher Faulet81e20172019-12-12 16:40:30 +01001174 }
Christopher Faulete0fca292020-01-13 21:49:03 +01001175 else {
1176 rule->action = ACT_ACTION_DENY;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001177 default_status = 403;
Christopher Faulet81e20172019-12-12 16:40:30 +01001178 }
Christopher Faulet81e20172019-12-12 16:40:30 +01001179 }
Christopher Faulete0fca292020-01-13 21:49:03 +01001180 else {
Christopher Faulet554c0eb2020-01-14 12:00:28 +01001181 rule->action = ACT_ACTION_DENY;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001182 default_status = 502;
Christopher Faulete0fca292020-01-13 21:49:03 +01001183 }
Christopher Faulet040c8cd2020-01-13 16:43:45 +01001184
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001185 /* If no args or only a deny_status specified, fallback on the legacy
1186 * mode and use default error files despite the fact that
1187 * default-errorfiles is not used. Otherwise, parse an http reply.
1188 */
Christopher Faulet040c8cd2020-01-13 16:43:45 +01001189
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001190 /* Prepare parsing of log-format strings */
1191 px->conf.args.ctx = ((rule->from == ACT_F_HTTP_REQ) ? ARGC_HRQ : ARGC_HRS);
Christopher Faulet554c0eb2020-01-14 12:00:28 +01001192
Christopher Faulet9467f182020-06-30 09:32:01 +02001193 if (!*(args[cur_arg]) || strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001194 rule->arg.http_reply = http_parse_http_reply((const char *[]){"default-errorfiles", ""}, &arg, px, default_status, err);
1195 goto end;
Christopher Faulet554c0eb2020-01-14 12:00:28 +01001196 }
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001197
1198 if (strcmp(args[cur_arg], "deny_status") == 0) {
Christopher Faulet9467f182020-06-30 09:32:01 +02001199 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 +02001200 rule->arg.http_reply = http_parse_http_reply((const char *[]){"status", args[cur_arg+1], "default-errorfiles", ""},
1201 &arg, px, default_status, err);
1202 *orig_arg += 2;
1203 goto end;
Christopher Faulet554c0eb2020-01-14 12:00:28 +01001204 }
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001205 args[cur_arg] += 5; /* skip "deny_" for the parsing */
Christopher Faulet554c0eb2020-01-14 12:00:28 +01001206 }
1207
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001208 rule->arg.http_reply = http_parse_http_reply(args, orig_arg, px, default_status, err);
Christopher Faulet554c0eb2020-01-14 12:00:28 +01001209
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001210 end:
1211 if (!rule->arg.http_reply)
1212 return ACT_RET_PRS_ERR;
1213
1214 rule->flags |= ACT_FLAG_FINAL;
1215 rule->check_ptr = check_act_http_reply;
1216 rule->release_ptr = release_act_http_reply;
Christopher Faulet81e20172019-12-12 16:40:30 +01001217 return ACT_RET_PRS_OK;
1218}
1219
Christopher Fauletb3048832020-05-27 15:26:43 +02001220
1221/* This function executes a auth action. It builds an 401/407 HTX message using
1222 * the corresponding proxy's error message. On success, it returns
1223 * ACT_RET_ABRT. If an error occurs ACT_RET_ERR is returned.
1224 */
1225static enum act_return http_action_auth(struct act_rule *rule, struct proxy *px,
1226 struct session *sess, struct stream *s, int flags)
1227{
1228 struct channel *req = &s->req;
1229 struct channel *res = &s->res;
1230 struct htx *htx = htx_from_buf(&res->buf);
1231 struct http_reply *reply;
1232 const char *auth_realm;
1233 struct http_hdr_ctx ctx;
1234 struct ist hdr;
1235
1236 /* Auth might be performed on regular http-req rules as well as on stats */
1237 auth_realm = rule->arg.http.str.ptr;
1238 if (!auth_realm) {
1239 if (px->uri_auth && s->current_rule_list == &px->uri_auth->http_req_rules)
1240 auth_realm = STATS_DEFAULT_REALM;
1241 else
1242 auth_realm = px->id;
1243 }
1244
1245 if (!(s->txn->flags & TX_USE_PX_CONN)) {
1246 s->txn->status = 401;
1247 hdr = ist("WWW-Authenticate");
1248 }
1249 else {
1250 s->txn->status = 407;
1251 hdr = ist("Proxy-Authenticate");
1252 }
1253 reply = http_error_message(s);
1254 channel_htx_truncate(res, htx);
1255
1256 if (chunk_printf(&trash, "Basic realm=\"%s\"", auth_realm) == -1)
1257 goto fail;
1258
1259 /* Write the generic 40x message */
1260 if (http_reply_to_htx(s, htx, reply) == -1)
1261 goto fail;
1262
1263 /* Remove all existing occurrences of the XXX-Authenticate header */
1264 ctx.blk = NULL;
1265 while (http_find_header(htx, hdr, &ctx, 1))
1266 http_remove_header(htx, &ctx);
1267
1268 /* Now a the right XXX-Authenticate header */
1269 if (!http_add_header(htx, hdr, ist2(b_orig(&trash), b_data(&trash))))
1270 goto fail;
1271
1272 /* Finally forward the reply */
1273 htx_to_buf(htx, &res->buf);
1274 if (!http_forward_proxy_resp(s, 1))
1275 goto fail;
1276
1277 /* Note: Only eval on the request */
1278 s->logs.tv_request = now;
1279 req->analysers &= AN_REQ_FLT_END;
1280
1281 if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Willy Tarreau4781b152021-04-06 13:53:36 +02001282 _HA_ATOMIC_INC(&s->sess->fe->fe_counters.intercepted_req);
Christopher Fauletb3048832020-05-27 15:26:43 +02001283
1284 if (!(s->flags & SF_ERR_MASK))
1285 s->flags |= SF_ERR_LOCAL;
1286 if (!(s->flags & SF_FINST_MASK))
1287 s->flags |= SF_FINST_R;
1288
1289 stream_inc_http_err_ctr(s);
1290 return ACT_RET_ABRT;
1291
1292 fail:
1293 /* If an error occurred, remove the incomplete HTTP response from the
1294 * buffer */
1295 channel_htx_truncate(res, htx);
1296 return ACT_RET_ERR;
1297}
1298
Christopher Faulet81e20172019-12-12 16:40:30 +01001299/* Parse a "auth" action. It may take 2 optional arguments to define a "realm"
1300 * parameter. It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
1301 */
1302static enum act_parse_ret parse_http_auth(const char **args, int *orig_arg, struct proxy *px,
1303 struct act_rule *rule, char **err)
1304{
1305 int cur_arg;
1306
Christopher Fauletb3048832020-05-27 15:26:43 +02001307 rule->action = ACT_CUSTOM;
Christopher Faulet245cf792019-12-18 14:58:12 +01001308 rule->flags |= ACT_FLAG_FINAL;
Christopher Fauletb3048832020-05-27 15:26:43 +02001309 rule->action_ptr = http_action_auth;
Christopher Faulet2eb53962020-01-14 14:47:34 +01001310 rule->release_ptr = release_http_action;
Christopher Faulet81e20172019-12-12 16:40:30 +01001311
1312 cur_arg = *orig_arg;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001313 if (strcmp(args[cur_arg], "realm") == 0) {
Christopher Faulet81e20172019-12-12 16:40:30 +01001314 cur_arg++;
1315 if (!*args[cur_arg]) {
1316 memprintf(err, "missing realm value.\n");
1317 return ACT_RET_PRS_ERR;
1318 }
Christopher Faulet96bff762019-12-17 13:46:18 +01001319 rule->arg.http.str.ptr = strdup(args[cur_arg]);
1320 rule->arg.http.str.len = strlen(rule->arg.http.str.ptr);
Christopher Faulet81e20172019-12-12 16:40:30 +01001321 cur_arg++;
1322 }
1323
Christopher Fauletc20b3712020-01-27 15:51:56 +01001324 LIST_INIT(&rule->arg.http.fmt);
Christopher Faulet81e20172019-12-12 16:40:30 +01001325 *orig_arg = cur_arg;
1326 return ACT_RET_PRS_OK;
1327}
1328
1329/* Parse a "set-nice" action. It takes the nice value as argument. It returns
1330 * ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
1331 */
1332static enum act_parse_ret parse_http_set_nice(const char **args, int *orig_arg, struct proxy *px,
1333 struct act_rule *rule, char **err)
1334{
1335 int cur_arg;
1336
1337 rule->action = ACT_HTTP_SET_NICE;
1338
1339 cur_arg = *orig_arg;
1340 if (!*args[cur_arg]) {
1341 memprintf(err, "expects exactly 1 argument (integer value)");
1342 return ACT_RET_PRS_ERR;
1343 }
Christopher Faulet96bff762019-12-17 13:46:18 +01001344 rule->arg.http.i = atoi(args[cur_arg]);
1345 if (rule->arg.http.i < -1024)
1346 rule->arg.http.i = -1024;
1347 else if (rule->arg.http.i > 1024)
1348 rule->arg.http.i = 1024;
Christopher Faulet81e20172019-12-12 16:40:30 +01001349
Christopher Fauletc20b3712020-01-27 15:51:56 +01001350 LIST_INIT(&rule->arg.http.fmt);
Christopher Faulet81e20172019-12-12 16:40:30 +01001351 *orig_arg = cur_arg + 1;
1352 return ACT_RET_PRS_OK;
1353}
1354
1355/* Parse a "set-tos" action. It takes the TOS value as argument. It returns
1356 * ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
1357 */
1358static enum act_parse_ret parse_http_set_tos(const char **args, int *orig_arg, struct proxy *px,
1359 struct act_rule *rule, char **err)
1360{
1361#ifdef IP_TOS
1362 char *endp;
1363 int cur_arg;
1364
1365 rule->action = ACT_HTTP_SET_TOS;
1366
1367 cur_arg = *orig_arg;
1368 if (!*args[cur_arg]) {
1369 memprintf(err, "expects exactly 1 argument (integer/hex value)");
1370 return ACT_RET_PRS_ERR;
1371 }
Christopher Faulet96bff762019-12-17 13:46:18 +01001372 rule->arg.http.i = strtol(args[cur_arg], &endp, 0);
Christopher Faulet81e20172019-12-12 16:40:30 +01001373 if (endp && *endp != '\0') {
1374 memprintf(err, "invalid character starting at '%s' (integer/hex value expected)", endp);
1375 return ACT_RET_PRS_ERR;
1376 }
1377
Christopher Fauletc20b3712020-01-27 15:51:56 +01001378 LIST_INIT(&rule->arg.http.fmt);
Christopher Faulet81e20172019-12-12 16:40:30 +01001379 *orig_arg = cur_arg + 1;
1380 return ACT_RET_PRS_OK;
1381#else
1382 memprintf(err, "not supported on this platform (IP_TOS undefined)");
1383 return ACT_RET_PRS_ERR;
1384#endif
1385}
1386
1387/* Parse a "set-mark" action. It takes the MARK value as argument. It returns
1388 * ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
1389 */
1390static enum act_parse_ret parse_http_set_mark(const char **args, int *orig_arg, struct proxy *px,
1391 struct act_rule *rule, char **err)
1392{
1393#ifdef SO_MARK
1394 char *endp;
1395 int cur_arg;
1396
1397 rule->action = ACT_HTTP_SET_MARK;
1398
1399 cur_arg = *orig_arg;
1400 if (!*args[cur_arg]) {
1401 memprintf(err, "expects exactly 1 argument (integer/hex value)");
1402 return ACT_RET_PRS_ERR;
1403 }
Christopher Faulet96bff762019-12-17 13:46:18 +01001404 rule->arg.http.i = strtoul(args[cur_arg], &endp, 0);
Christopher Faulet81e20172019-12-12 16:40:30 +01001405 if (endp && *endp != '\0') {
1406 memprintf(err, "invalid character starting at '%s' (integer/hex value expected)", endp);
1407 return ACT_RET_PRS_ERR;
1408 }
1409
Christopher Fauletc20b3712020-01-27 15:51:56 +01001410 LIST_INIT(&rule->arg.http.fmt);
Christopher Faulet81e20172019-12-12 16:40:30 +01001411 *orig_arg = cur_arg + 1;
1412 global.last_checks |= LSTCHK_NETADM;
1413 return ACT_RET_PRS_OK;
1414#else
1415 memprintf(err, "not supported on this platform (SO_MARK undefined)");
1416 return ACT_RET_PRS_ERR;
1417#endif
1418}
1419
1420/* Parse a "set-log-level" action. It takes the level value as argument. It
1421 * returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
1422 */
1423static enum act_parse_ret parse_http_set_log_level(const char **args, int *orig_arg, struct proxy *px,
1424 struct act_rule *rule, char **err)
1425{
1426 int cur_arg;
1427
1428 rule->action = ACT_HTTP_SET_LOGL;
1429
1430 cur_arg = *orig_arg;
1431 if (!*args[cur_arg]) {
1432 bad_log_level:
1433 memprintf(err, "expects exactly 1 argument (log level name or 'silent')");
1434 return ACT_RET_PRS_ERR;
1435 }
1436 if (strcmp(args[cur_arg], "silent") == 0)
Christopher Faulet96bff762019-12-17 13:46:18 +01001437 rule->arg.http.i = -1;
1438 else if ((rule->arg.http.i = get_log_level(args[cur_arg]) + 1) == 0)
Christopher Faulet81e20172019-12-12 16:40:30 +01001439 goto bad_log_level;
1440
Christopher Fauletc20b3712020-01-27 15:51:56 +01001441 LIST_INIT(&rule->arg.http.fmt);
Christopher Faulet81e20172019-12-12 16:40:30 +01001442 *orig_arg = cur_arg + 1;
1443 return ACT_RET_PRS_OK;
1444}
1445
Christopher Faulet91b3ec12020-01-17 22:30:06 +01001446/* This function executes a early-hint action. It adds an HTTP Early Hint HTTP
1447 * 103 response header with <.arg.http.str> name and with a value built
1448 * according to <.arg.http.fmt> log line format. If it is the first early-hint
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001449 * rule of series, the 103 response start-line is added first. At the end, if
Christopher Faulet91b3ec12020-01-17 22:30:06 +01001450 * the next rule is not an early-hint rule or if it is the last rule, the EOH
1451 * block is added to terminate the response. On success, it returns
1452 * ACT_RET_CONT. If an error occurs while soft rewrites are enabled, the action
1453 * is canceled, but the rule processing continue. Otherwsize ACT_RET_ERR is
1454 * returned.
1455 */
1456static enum act_return http_action_early_hint(struct act_rule *rule, struct proxy *px,
1457 struct session *sess, struct stream *s, int flags)
1458{
1459 struct act_rule *prev_rule, *next_rule;
1460 struct channel *res = &s->res;
1461 struct htx *htx = htx_from_buf(&res->buf);
1462 struct buffer *value = alloc_trash_chunk();
1463 enum act_return ret = ACT_RET_CONT;
1464
1465 if (!(s->txn->req.flags & HTTP_MSGF_VER_11))
1466 goto leave;
1467
1468 if (!value) {
1469 if (!(s->flags & SF_ERR_MASK))
1470 s->flags |= SF_ERR_RESOURCE;
1471 goto error;
1472 }
1473
1474 /* get previous and next rules */
1475 prev_rule = LIST_PREV(&rule->list, typeof(rule), list);
1476 next_rule = LIST_NEXT(&rule->list, typeof(rule), list);
1477
1478 /* if no previous rule or previous rule is not early-hint, start a new response. Otherwise,
1479 * continue to add link to a previously started response */
1480 if (&prev_rule->list == s->current_rule_list || prev_rule->action_ptr != http_action_early_hint) {
1481 struct htx_sl *sl;
1482 unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|
1483 HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
1484
1485 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
1486 ist("HTTP/1.1"), ist("103"), ist("Early Hints"));
1487 if (!sl)
1488 goto error;
1489 sl->info.res.status = 103;
1490 }
1491
1492 /* Add the HTTP Early Hint HTTP 103 response heade */
1493 value->data = build_logline(s, b_tail(value), b_room(value), &rule->arg.http.fmt);
1494 if (!htx_add_header(htx, rule->arg.http.str, ist2(b_head(value), b_data(value))))
1495 goto error;
1496
1497 /* if it is the last rule or the next one is not an early-hint, terminate the current
1498 * response. */
1499 if (&next_rule->list == s->current_rule_list || next_rule->action_ptr != http_action_early_hint) {
Christopher Faulet91b3ec12020-01-17 22:30:06 +01001500 if (!htx_add_endof(htx, HTX_BLK_EOH)) {
1501 /* If an error occurred during an Early-hint rule,
1502 * remove the incomplete HTTP 103 response from the
1503 * buffer */
1504 goto error;
1505 }
1506
Christopher Fauleta72a7e42020-01-28 09:28:11 +01001507 if (!http_forward_proxy_resp(s, 0))
1508 goto error;
Christopher Faulet91b3ec12020-01-17 22:30:06 +01001509 }
1510
1511 leave:
1512 free_trash_chunk(value);
1513 return ret;
1514
1515 error:
1516 /* If an error occurred during an Early-hint rule, remove the incomplete
1517 * HTTP 103 response from the buffer */
1518 channel_htx_truncate(res, htx);
1519 ret = ACT_RET_ERR;
1520 goto leave;
1521}
1522
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001523/* This function executes a set-header or add-header actions. It builds a string
1524 * in the trash from the specified format string. It finds the action to be
1525 * performed in <.action>, previously filled by function parse_set_header(). The
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001526 * replacement action is executed by the function http_action_set_header(). On
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001527 * success, it returns ACT_RET_CONT. If an error occurs while soft rewrites are
1528 * enabled, the action is canceled, but the rule processing continue. Otherwsize
1529 * ACT_RET_ERR is returned.
1530 */
1531static enum act_return http_action_set_header(struct act_rule *rule, struct proxy *px,
1532 struct session *sess, struct stream *s, int flags)
1533{
Christopher Faulet91e31d82020-01-24 15:37:13 +01001534 struct http_msg *msg = ((rule->from == ACT_F_HTTP_REQ) ? &s->txn->req : &s->txn->rsp);
1535 struct htx *htx = htxbuf(&msg->chn->buf);
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001536 enum act_return ret = ACT_RET_CONT;
1537 struct buffer *replace;
1538 struct http_hdr_ctx ctx;
1539 struct ist n, v;
1540
1541 replace = alloc_trash_chunk();
1542 if (!replace)
1543 goto fail_alloc;
1544
1545 replace->data = build_logline(s, replace->area, replace->size, &rule->arg.http.fmt);
1546 n = rule->arg.http.str;
1547 v = ist2(replace->area, replace->data);
1548
1549 if (rule->action == 0) { // set-header
1550 /* remove all occurrences of the header */
1551 ctx.blk = NULL;
1552 while (http_find_header(htx, n, &ctx, 1))
1553 http_remove_header(htx, &ctx);
1554 }
1555
1556 /* Now add header */
1557 if (!http_add_header(htx, n, v))
1558 goto fail_rewrite;
1559
1560 leave:
1561 free_trash_chunk(replace);
1562 return ret;
1563
1564 fail_alloc:
1565 if (!(s->flags & SF_ERR_MASK))
1566 s->flags |= SF_ERR_RESOURCE;
1567 ret = ACT_RET_ERR;
1568 goto leave;
1569
1570 fail_rewrite:
Willy Tarreau4781b152021-04-06 13:53:36 +02001571 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_rewrites);
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001572 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +02001573 _HA_ATOMIC_INC(&s->be->be_counters.failed_rewrites);
William Lallemand36119de2021-03-08 15:26:48 +01001574 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001575 _HA_ATOMIC_INC(&sess->listener->counters->failed_rewrites);
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001576 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001577 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_rewrites);
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001578
Christopher Faulet333bf8c2020-01-22 14:38:05 +01001579 if (!(msg->flags & HTTP_MSGF_SOFT_RW)) {
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001580 ret = ACT_RET_ERR;
Christopher Faulet333bf8c2020-01-22 14:38:05 +01001581 if (!(s->flags & SF_ERR_MASK))
1582 s->flags |= SF_ERR_PRXCOND;
1583 }
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001584 goto leave;
1585}
1586
Christopher Faulet81e20172019-12-12 16:40:30 +01001587/* Parse a "set-header", "add-header" or "early-hint" actions. It takes an
1588 * header name and a log-format string as arguments. It returns ACT_RET_PRS_OK
1589 * on success, ACT_RET_PRS_ERR on error.
1590 *
1591 * Note: same function is used for the request and the response. However
1592 * "early-hint" rules are only supported for request rules.
1593 */
1594static enum act_parse_ret parse_http_set_header(const char **args, int *orig_arg, struct proxy *px,
1595 struct act_rule *rule, char **err)
1596{
Christopher Faulet5f802b32021-10-13 17:22:17 +02001597 int cap = 0, cur_arg;
Christopher Faulet81e20172019-12-12 16:40:30 +01001598
Christopher Faulet91b3ec12020-01-17 22:30:06 +01001599 if (args[*orig_arg-1][0] == 'e') {
1600 rule->action = ACT_CUSTOM;
1601 rule->action_ptr = http_action_early_hint;
1602 }
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001603 else {
1604 if (args[*orig_arg-1][0] == 's')
1605 rule->action = 0; // set-header
1606 else
1607 rule->action = 1; // add-header
1608 rule->action_ptr = http_action_set_header;
1609 }
Christopher Faulet2eb53962020-01-14 14:47:34 +01001610 rule->release_ptr = release_http_action;
Christopher Faulet81e20172019-12-12 16:40:30 +01001611
1612 cur_arg = *orig_arg;
1613 if (!*args[cur_arg] || !*args[cur_arg+1]) {
1614 memprintf(err, "expects exactly 2 arguments");
1615 return ACT_RET_PRS_ERR;
1616 }
1617
Christopher Faulet81e20172019-12-12 16:40:30 +01001618
Christopher Faulet96bff762019-12-17 13:46:18 +01001619 rule->arg.http.str.ptr = strdup(args[cur_arg]);
1620 rule->arg.http.str.len = strlen(rule->arg.http.str.ptr);
1621 LIST_INIT(&rule->arg.http.fmt);
Christopher Faulet81e20172019-12-12 16:40:30 +01001622
1623 if (rule->from == ACT_F_HTTP_REQ) {
1624 px->conf.args.ctx = ARGC_HRQ;
Christopher Faulet5f802b32021-10-13 17:22:17 +02001625 if (px->cap & PR_CAP_FE)
1626 cap |= SMP_VAL_FE_HRQ_HDR;
1627 if (px->cap & PR_CAP_BE)
1628 cap |= SMP_VAL_BE_HRQ_HDR;
Christopher Faulet81e20172019-12-12 16:40:30 +01001629 }
1630 else{
1631 px->conf.args.ctx = ARGC_HRS;
Christopher Faulet5f802b32021-10-13 17:22:17 +02001632 if (px->cap & PR_CAP_FE)
1633 cap |= SMP_VAL_FE_HRS_HDR;
1634 if (px->cap & PR_CAP_BE)
1635 cap |= SMP_VAL_BE_HRS_HDR;
Christopher Faulet81e20172019-12-12 16:40:30 +01001636 }
1637
1638 cur_arg++;
Christopher Faulet1337b322020-01-14 14:50:55 +01001639 if (!parse_logformat_string(args[cur_arg], px, &rule->arg.http.fmt, LOG_OPT_HTTP, cap, err)) {
Tim Duesterhused526372020-03-05 17:56:33 +01001640 istfree(&rule->arg.http.str);
Christopher Faulet81e20172019-12-12 16:40:30 +01001641 return ACT_RET_PRS_ERR;
Christopher Faulet1337b322020-01-14 14:50:55 +01001642 }
Christopher Faulet81e20172019-12-12 16:40:30 +01001643
1644 free(px->conf.lfs_file);
1645 px->conf.lfs_file = strdup(px->conf.args.file);
1646 px->conf.lfs_line = px->conf.args.line;
1647
1648 *orig_arg = cur_arg + 1;
1649 return ACT_RET_PRS_OK;
1650}
1651
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001652/* This function executes a replace-header or replace-value actions. It
1653 * builds a string in the trash from the specified format string. It finds
1654 * the action to be performed in <.action>, previously filled by function
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001655 * parse_replace_header(). The replacement action is executed by the function
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001656 * http_action_replace_header(). On success, it returns ACT_RET_CONT. If an error
1657 * occurs while soft rewrites are enabled, the action is canceled, but the rule
1658 * processing continue. Otherwsize ACT_RET_ERR is returned.
1659 */
1660static enum act_return http_action_replace_header(struct act_rule *rule, struct proxy *px,
1661 struct session *sess, struct stream *s, int flags)
1662{
Christopher Faulet91e31d82020-01-24 15:37:13 +01001663 struct http_msg *msg = ((rule->from == ACT_F_HTTP_REQ) ? &s->txn->req : &s->txn->rsp);
1664 struct htx *htx = htxbuf(&msg->chn->buf);
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001665 enum act_return ret = ACT_RET_CONT;
1666 struct buffer *replace;
1667 int r;
1668
1669 replace = alloc_trash_chunk();
1670 if (!replace)
1671 goto fail_alloc;
1672
1673 replace->data = build_logline(s, replace->area, replace->size, &rule->arg.http.fmt);
1674
1675 r = http_replace_hdrs(s, htx, rule->arg.http.str, replace->area, rule->arg.http.re, (rule->action == 0));
1676 if (r == -1)
1677 goto fail_rewrite;
1678
1679 leave:
1680 free_trash_chunk(replace);
1681 return ret;
1682
1683 fail_alloc:
1684 if (!(s->flags & SF_ERR_MASK))
1685 s->flags |= SF_ERR_RESOURCE;
1686 ret = ACT_RET_ERR;
1687 goto leave;
1688
1689 fail_rewrite:
Willy Tarreau4781b152021-04-06 13:53:36 +02001690 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_rewrites);
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001691 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +02001692 _HA_ATOMIC_INC(&s->be->be_counters.failed_rewrites);
William Lallemand36119de2021-03-08 15:26:48 +01001693 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001694 _HA_ATOMIC_INC(&sess->listener->counters->failed_rewrites);
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001695 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001696 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_rewrites);
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001697
Christopher Faulet333bf8c2020-01-22 14:38:05 +01001698 if (!(msg->flags & HTTP_MSGF_SOFT_RW)) {
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001699 ret = ACT_RET_ERR;
Christopher Faulet333bf8c2020-01-22 14:38:05 +01001700 if (!(s->flags & SF_ERR_MASK))
1701 s->flags |= SF_ERR_PRXCOND;
1702 }
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001703 goto leave;
1704}
1705
Christopher Faulet81e20172019-12-12 16:40:30 +01001706/* Parse a "replace-header" or "replace-value" actions. It takes an header name,
1707 * a regex and replacement string as arguments. It returns ACT_RET_PRS_OK on
1708 * success, ACT_RET_PRS_ERR on error.
1709 */
1710static enum act_parse_ret parse_http_replace_header(const char **args, int *orig_arg, struct proxy *px,
1711 struct act_rule *rule, char **err)
1712{
Christopher Faulet5f802b32021-10-13 17:22:17 +02001713 int cap = 0, cur_arg;
Christopher Faulet81e20172019-12-12 16:40:30 +01001714
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001715 if (args[*orig_arg-1][8] == 'h')
1716 rule->action = 0; // replace-header
1717 else
1718 rule->action = 1; // replace-value
1719 rule->action_ptr = http_action_replace_header;
Christopher Faulet2eb53962020-01-14 14:47:34 +01001720 rule->release_ptr = release_http_action;
Christopher Faulet81e20172019-12-12 16:40:30 +01001721
1722 cur_arg = *orig_arg;
1723 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2]) {
1724 memprintf(err, "expects exactly 3 arguments");
1725 return ACT_RET_PRS_ERR;
1726 }
1727
Christopher Faulet96bff762019-12-17 13:46:18 +01001728 rule->arg.http.str.ptr = strdup(args[cur_arg]);
1729 rule->arg.http.str.len = strlen(rule->arg.http.str.ptr);
1730 LIST_INIT(&rule->arg.http.fmt);
Christopher Faulet81e20172019-12-12 16:40:30 +01001731
1732 cur_arg++;
Christopher Faulet1337b322020-01-14 14:50:55 +01001733 if (!(rule->arg.http.re = regex_comp(args[cur_arg], 1, 1, err))) {
Tim Duesterhused526372020-03-05 17:56:33 +01001734 istfree(&rule->arg.http.str);
Christopher Faulet81e20172019-12-12 16:40:30 +01001735 return ACT_RET_PRS_ERR;
Christopher Faulet1337b322020-01-14 14:50:55 +01001736 }
Christopher Faulet81e20172019-12-12 16:40:30 +01001737
1738 if (rule->from == ACT_F_HTTP_REQ) {
1739 px->conf.args.ctx = ARGC_HRQ;
Christopher Faulet5f802b32021-10-13 17:22:17 +02001740 if (px->cap & PR_CAP_FE)
1741 cap |= SMP_VAL_FE_HRQ_HDR;
1742 if (px->cap & PR_CAP_BE)
1743 cap |= SMP_VAL_BE_HRQ_HDR;
Christopher Faulet81e20172019-12-12 16:40:30 +01001744 }
1745 else{
1746 px->conf.args.ctx = ARGC_HRS;
Christopher Faulet5f802b32021-10-13 17:22:17 +02001747 if (px->cap & PR_CAP_FE)
1748 cap |= SMP_VAL_FE_HRS_HDR;
1749 if (px->cap & PR_CAP_BE)
1750 cap |= SMP_VAL_BE_HRS_HDR;
Christopher Faulet81e20172019-12-12 16:40:30 +01001751 }
1752
1753 cur_arg++;
Christopher Faulet1337b322020-01-14 14:50:55 +01001754 if (!parse_logformat_string(args[cur_arg], px, &rule->arg.http.fmt, LOG_OPT_HTTP, cap, err)) {
Tim Duesterhused526372020-03-05 17:56:33 +01001755 istfree(&rule->arg.http.str);
Christopher Faulet1337b322020-01-14 14:50:55 +01001756 regex_free(rule->arg.http.re);
Christopher Faulet81e20172019-12-12 16:40:30 +01001757 return ACT_RET_PRS_ERR;
Christopher Faulet1337b322020-01-14 14:50:55 +01001758 }
Christopher Faulet81e20172019-12-12 16:40:30 +01001759
1760 free(px->conf.lfs_file);
1761 px->conf.lfs_file = strdup(px->conf.args.file);
1762 px->conf.lfs_line = px->conf.args.line;
1763
1764 *orig_arg = cur_arg + 1;
1765 return ACT_RET_PRS_OK;
1766}
1767
Maciej Zdebebdd4c52020-11-20 13:58:48 +00001768/* This function executes a del-header action with selected matching mode for
1769 * header name. It finds the matching method to be performed in <.action>, previously
1770 * filled by function parse_http_del_header(). On success, it returns ACT_RET_CONT.
1771 * Otherwise ACT_RET_ERR is returned.
1772 */
1773static enum act_return http_action_del_header(struct act_rule *rule, struct proxy *px,
1774 struct session *sess, struct stream *s, int flags)
1775{
1776 struct http_hdr_ctx ctx;
1777 struct http_msg *msg = ((rule->from == ACT_F_HTTP_REQ) ? &s->txn->req : &s->txn->rsp);
1778 struct htx *htx = htxbuf(&msg->chn->buf);
1779 enum act_return ret = ACT_RET_CONT;
1780
1781 /* remove all occurrences of the header */
1782 ctx.blk = NULL;
1783 switch (rule->action) {
1784 case PAT_MATCH_STR:
1785 while (http_find_header(htx, rule->arg.http.str, &ctx, 1))
1786 http_remove_header(htx, &ctx);
1787 break;
1788 case PAT_MATCH_BEG:
1789 while (http_find_pfx_header(htx, rule->arg.http.str, &ctx, 1))
1790 http_remove_header(htx, &ctx);
1791 break;
1792 case PAT_MATCH_END:
1793 while (http_find_sfx_header(htx, rule->arg.http.str, &ctx, 1))
1794 http_remove_header(htx, &ctx);
1795 break;
1796 case PAT_MATCH_SUB:
1797 while (http_find_sub_header(htx, rule->arg.http.str, &ctx, 1))
1798 http_remove_header(htx, &ctx);
1799 break;
1800 case PAT_MATCH_REG:
1801 while (http_match_header(htx, rule->arg.http.re, &ctx, 1))
1802 http_remove_header(htx, &ctx);
1803 break;
1804 default:
1805 return ACT_RET_ERR;
1806 }
1807 return ret;
1808}
1809
1810/* Parse a "del-header" action. It takes string as a required argument,
1811 * optional flag (currently only -m) and optional matching method of input string
1812 * with header name to be deleted. Default matching method is exact match (-m str).
1813 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Christopher Faulet81e20172019-12-12 16:40:30 +01001814 */
1815static enum act_parse_ret parse_http_del_header(const char **args, int *orig_arg, struct proxy *px,
1816 struct act_rule *rule, char **err)
1817{
1818 int cur_arg;
Maciej Zdebebdd4c52020-11-20 13:58:48 +00001819 int pat_idx;
Christopher Faulet81e20172019-12-12 16:40:30 +01001820
Maciej Zdebebdd4c52020-11-20 13:58:48 +00001821 /* set exact matching (-m str) as default */
1822 rule->action = PAT_MATCH_STR;
1823 rule->action_ptr = http_action_del_header;
Christopher Faulet2eb53962020-01-14 14:47:34 +01001824 rule->release_ptr = release_http_action;
Christopher Faulet81e20172019-12-12 16:40:30 +01001825
1826 cur_arg = *orig_arg;
1827 if (!*args[cur_arg]) {
Maciej Zdebebdd4c52020-11-20 13:58:48 +00001828 memprintf(err, "expects at least 1 argument");
Christopher Faulet81e20172019-12-12 16:40:30 +01001829 return ACT_RET_PRS_ERR;
1830 }
1831
Christopher Faulet96bff762019-12-17 13:46:18 +01001832 rule->arg.http.str.ptr = strdup(args[cur_arg]);
1833 rule->arg.http.str.len = strlen(rule->arg.http.str.ptr);
Christopher Faulet81e20172019-12-12 16:40:30 +01001834 px->conf.args.ctx = (rule->from == ACT_F_HTTP_REQ ? ARGC_HRQ : ARGC_HRS);
1835
Maciej Zdeb6dee9962020-11-23 16:03:09 +00001836 LIST_INIT(&rule->arg.http.fmt);
Maciej Zdebebdd4c52020-11-20 13:58:48 +00001837 if (strcmp(args[cur_arg+1], "-m") == 0) {
1838 cur_arg++;
1839 if (!*args[cur_arg+1]) {
1840 memprintf(err, "-m flag expects exactly 1 argument");
1841 return ACT_RET_PRS_ERR;
1842 }
1843
1844 cur_arg++;
1845 pat_idx = pat_find_match_name(args[cur_arg]);
1846 switch (pat_idx) {
1847 case PAT_MATCH_REG:
1848 if (!(rule->arg.http.re = regex_comp(rule->arg.http.str.ptr, 1, 1, err)))
1849 return ACT_RET_PRS_ERR;
1850 /* fall through */
1851 case PAT_MATCH_STR:
1852 case PAT_MATCH_BEG:
1853 case PAT_MATCH_END:
1854 case PAT_MATCH_SUB:
1855 rule->action = pat_idx;
1856 break;
1857 default:
1858 memprintf(err, "-m with unsupported matching method '%s'", args[cur_arg]);
1859 return ACT_RET_PRS_ERR;
1860 }
1861 }
1862
Christopher Faulet81e20172019-12-12 16:40:30 +01001863 *orig_arg = cur_arg + 1;
1864 return ACT_RET_PRS_OK;
1865}
1866
Christopher Faulet2eb53962020-01-14 14:47:34 +01001867/* Release memory allocated by an http redirect action. */
1868static void release_http_redir(struct act_rule *rule)
1869{
1870 struct logformat_node *lf, *lfb;
1871 struct redirect_rule *redir;
1872
1873 redir = rule->arg.redir;
Willy Tarreau2b718102021-04-21 07:32:39 +02001874 LIST_DELETE(&redir->list);
Christopher Faulet2eb53962020-01-14 14:47:34 +01001875 if (redir->cond) {
1876 prune_acl_cond(redir->cond);
1877 free(redir->cond);
1878 }
1879 free(redir->rdr_str);
1880 free(redir->cookie_str);
1881 list_for_each_entry_safe(lf, lfb, &redir->rdr_fmt, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001882 LIST_DELETE(&lf->list);
Christopher Faulet2eb53962020-01-14 14:47:34 +01001883 free(lf);
1884 }
1885 free(redir);
1886}
1887
Christopher Faulet81e20172019-12-12 16:40:30 +01001888/* Parse a "redirect" action. It returns ACT_RET_PRS_OK on success,
1889 * ACT_RET_PRS_ERR on error.
1890 */
1891static enum act_parse_ret parse_http_redirect(const char **args, int *orig_arg, struct proxy *px,
1892 struct act_rule *rule, char **err)
1893{
1894 struct redirect_rule *redir;
1895 int dir, cur_arg;
1896
1897 rule->action = ACT_HTTP_REDIR;
Christopher Faulet245cf792019-12-18 14:58:12 +01001898 rule->flags |= ACT_FLAG_FINAL;
Christopher Faulet2eb53962020-01-14 14:47:34 +01001899 rule->release_ptr = release_http_redir;
Christopher Faulet81e20172019-12-12 16:40:30 +01001900
1901 cur_arg = *orig_arg;
1902
1903 dir = (rule->from == ACT_F_HTTP_REQ ? 0 : 1);
1904 if ((redir = http_parse_redirect_rule(px->conf.args.file, px->conf.args.line, px, &args[cur_arg], err, 1, dir)) == NULL)
1905 return ACT_RET_PRS_ERR;
1906
1907 rule->arg.redir = redir;
1908 rule->cond = redir->cond;
1909 redir->cond = NULL;
1910
1911 /* skip all arguments */
1912 while (*args[cur_arg])
1913 cur_arg++;
1914
1915 *orig_arg = cur_arg;
1916 return ACT_RET_PRS_OK;
1917}
1918
Christopher Faulet046cf442019-12-17 15:45:23 +01001919/* This function executes a add-acl, del-acl, set-map or del-map actions. On
1920 * success, it returns ACT_RET_CONT. Otherwsize ACT_RET_ERR is returned.
1921 */
1922static enum act_return http_action_set_map(struct act_rule *rule, struct proxy *px,
1923 struct session *sess, struct stream *s, int flags)
1924{
1925 struct pat_ref *ref;
1926 struct buffer *key = NULL, *value = NULL;
1927 enum act_return ret = ACT_RET_CONT;
1928
1929 /* collect reference */
1930 ref = pat_ref_lookup(rule->arg.map.ref);
1931 if (!ref)
1932 goto leave;
1933
1934 /* allocate key */
1935 key = alloc_trash_chunk();
1936 if (!key)
1937 goto fail_alloc;
1938
1939 /* collect key */
1940 key->data = build_logline(s, key->area, key->size, &rule->arg.map.key);
1941 key->area[key->data] = '\0';
1942
1943 switch (rule->action) {
1944 case 0: // add-acl
1945 /* add entry only if it does not already exist */
1946 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
1947 if (pat_ref_find_elt(ref, key->area) == NULL)
1948 pat_ref_add(ref, key->area, NULL, NULL);
1949 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
1950 break;
1951
1952 case 1: // set-map
1953 /* allocate value */
1954 value = alloc_trash_chunk();
1955 if (!value)
1956 goto fail_alloc;
1957
1958 /* collect value */
1959 value->data = build_logline(s, value->area, value->size, &rule->arg.map.value);
1960 value->area[value->data] = '\0';
1961
1962 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
1963 if (pat_ref_find_elt(ref, key->area) != NULL) {
1964 /* update entry if it exists */
1965 pat_ref_set(ref, key->area, value->area, NULL);
1966 }
1967 else {
1968 /* insert a new entry */
1969 pat_ref_add(ref, key->area, value->area, NULL);
1970 }
1971 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
1972 break;
1973
1974 case 2: // del-acl
1975 case 3: // del-map
1976 /* returned code: 1=ok, 0=ko */
1977 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
1978 pat_ref_delete(ref, key->area);
1979 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
1980 break;
1981
1982 default:
1983 ret = ACT_RET_ERR;
1984 }
1985
1986
1987 leave:
1988 free_trash_chunk(key);
1989 free_trash_chunk(value);
1990 return ret;
1991
1992 fail_alloc:
1993 if (!(s->flags & SF_ERR_MASK))
1994 s->flags |= SF_ERR_RESOURCE;
1995 ret = ACT_RET_ERR;
1996 goto leave;
1997}
1998
Christopher Faulet2eb53962020-01-14 14:47:34 +01001999/* Release memory allocated by an http map/acl action. */
2000static void release_http_map(struct act_rule *rule)
2001{
2002 struct logformat_node *lf, *lfb;
2003
2004 free(rule->arg.map.ref);
2005 list_for_each_entry_safe(lf, lfb, &rule->arg.map.key, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02002006 LIST_DELETE(&lf->list);
Christopher Faulet2eb53962020-01-14 14:47:34 +01002007 release_sample_expr(lf->expr);
2008 free(lf->arg);
2009 free(lf);
2010 }
2011 if (rule->action == 1) {
2012 list_for_each_entry_safe(lf, lfb, &rule->arg.map.value, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02002013 LIST_DELETE(&lf->list);
Christopher Faulet2eb53962020-01-14 14:47:34 +01002014 release_sample_expr(lf->expr);
2015 free(lf->arg);
2016 free(lf);
2017 }
2018 }
2019}
2020
Christopher Faulet81e20172019-12-12 16:40:30 +01002021/* Parse a "add-acl", "del-acl", "set-map" or "del-map" actions. It takes one or
Christopher Faulet046cf442019-12-17 15:45:23 +01002022 * two log-format string as argument depending on the action. The action is
2023 * stored in <.action> as an int (0=add-acl, 1=set-map, 2=del-acl,
2024 * 3=del-map). It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Christopher Faulet81e20172019-12-12 16:40:30 +01002025 */
2026static enum act_parse_ret parse_http_set_map(const char **args, int *orig_arg, struct proxy *px,
2027 struct act_rule *rule, char **err)
2028{
Christopher Faulet5f802b32021-10-13 17:22:17 +02002029 int cap = 0, cur_arg;
Christopher Faulet81e20172019-12-12 16:40:30 +01002030
Christopher Faulet046cf442019-12-17 15:45:23 +01002031 if (args[*orig_arg-1][0] == 'a') // add-acl
2032 rule->action = 0;
2033 else if (args[*orig_arg-1][0] == 's') // set-map
2034 rule->action = 1;
2035 else if (args[*orig_arg-1][4] == 'a') // del-acl
2036 rule->action = 2;
2037 else if (args[*orig_arg-1][4] == 'm') // del-map
2038 rule->action = 3;
2039 else {
2040 memprintf(err, "internal error: unhandled action '%s'", args[0]);
2041 return ACT_RET_PRS_ERR;
2042 }
2043 rule->action_ptr = http_action_set_map;
Christopher Faulet2eb53962020-01-14 14:47:34 +01002044 rule->release_ptr = release_http_map;
Christopher Faulet81e20172019-12-12 16:40:30 +01002045
2046 cur_arg = *orig_arg;
Christopher Faulet046cf442019-12-17 15:45:23 +01002047 if (rule->action == 1 && (!*args[cur_arg] || !*args[cur_arg+1])) {
2048 /* 2 args for set-map */
Christopher Faulet81e20172019-12-12 16:40:30 +01002049 memprintf(err, "expects exactly 2 arguments");
2050 return ACT_RET_PRS_ERR;
2051 }
2052 else if (!*args[cur_arg]) {
Christopher Faulet046cf442019-12-17 15:45:23 +01002053 /* only one arg for other actions */
Christopher Faulet81e20172019-12-12 16:40:30 +01002054 memprintf(err, "expects exactly 1 arguments");
2055 return ACT_RET_PRS_ERR;
2056 }
2057
2058 /*
2059 * '+ 8' for 'set-map(' (same for del-map)
2060 * '- 9' for 'set-map(' + trailing ')' (same for del-map)
2061 */
2062 rule->arg.map.ref = my_strndup(args[cur_arg-1] + 8, strlen(args[cur_arg-1]) - 9);
2063
2064 if (rule->from == ACT_F_HTTP_REQ) {
2065 px->conf.args.ctx = ARGC_HRQ;
Christopher Faulet5f802b32021-10-13 17:22:17 +02002066 if (px->cap & PR_CAP_FE)
2067 cap |= SMP_VAL_FE_HRQ_HDR;
2068 if (px->cap & PR_CAP_BE)
2069 cap |= SMP_VAL_BE_HRQ_HDR;
Christopher Faulet81e20172019-12-12 16:40:30 +01002070 }
2071 else{
2072 px->conf.args.ctx = ARGC_HRS;
Christopher Faulet5f802b32021-10-13 17:22:17 +02002073 if (px->cap & PR_CAP_FE)
2074 cap |= SMP_VAL_FE_HRS_HDR;
2075 if (px->cap & PR_CAP_BE)
2076 cap |= SMP_VAL_BE_HRS_HDR;
Christopher Faulet81e20172019-12-12 16:40:30 +01002077 }
2078
2079 /* key pattern */
2080 LIST_INIT(&rule->arg.map.key);
Christopher Faulet1337b322020-01-14 14:50:55 +01002081 if (!parse_logformat_string(args[cur_arg], px, &rule->arg.map.key, LOG_OPT_HTTP, cap, err)) {
2082 free(rule->arg.map.ref);
Christopher Faulet81e20172019-12-12 16:40:30 +01002083 return ACT_RET_PRS_ERR;
Christopher Faulet1337b322020-01-14 14:50:55 +01002084 }
Christopher Faulet81e20172019-12-12 16:40:30 +01002085
Christopher Faulet046cf442019-12-17 15:45:23 +01002086 if (rule->action == 1) {
Christopher Faulet81e20172019-12-12 16:40:30 +01002087 /* value pattern for set-map only */
2088 cur_arg++;
2089 LIST_INIT(&rule->arg.map.value);
Christopher Faulet1337b322020-01-14 14:50:55 +01002090 if (!parse_logformat_string(args[cur_arg], px, &rule->arg.map.value, LOG_OPT_HTTP, cap, err)) {
2091 free(rule->arg.map.ref);
Christopher Faulet81e20172019-12-12 16:40:30 +01002092 return ACT_RET_PRS_ERR;
Christopher Faulet1337b322020-01-14 14:50:55 +01002093 }
Christopher Faulet81e20172019-12-12 16:40:30 +01002094 }
2095
2096 free(px->conf.lfs_file);
2097 px->conf.lfs_file = strdup(px->conf.args.file);
2098 px->conf.lfs_line = px->conf.args.line;
2099
2100 *orig_arg = cur_arg + 1;
2101 return ACT_RET_PRS_OK;
2102}
2103
Christopher Fauletac98d812019-12-18 09:20:16 +01002104/* This function executes a track-sc* actions. On success, it returns
2105 * ACT_RET_CONT. Otherwsize ACT_RET_ERR is returned.
2106 */
2107static enum act_return http_action_track_sc(struct act_rule *rule, struct proxy *px,
2108 struct session *sess, struct stream *s, int flags)
2109{
2110 struct stktable *t;
2111 struct stksess *ts;
2112 struct stktable_key *key;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002113 void *ptr1, *ptr2, *ptr3, *ptr4, *ptr5, *ptr6;
Christopher Fauletac98d812019-12-18 09:20:16 +01002114 int opt;
2115
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002116 ptr1 = ptr2 = ptr3 = ptr4 = ptr5 = ptr6 = NULL;
Christopher Fauletac98d812019-12-18 09:20:16 +01002117 opt = ((rule->from == ACT_F_HTTP_REQ) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES) | SMP_OPT_FINAL;
2118
2119 t = rule->arg.trk_ctr.table.t;
Emeric Brun362d25e2021-03-10 16:58:03 +01002120
2121 if (stkctr_entry(&s->stkctr[rule->action]))
2122 goto end;
2123
Christopher Fauletac98d812019-12-18 09:20:16 +01002124 key = stktable_fetch_key(t, s->be, sess, s, opt, rule->arg.trk_ctr.expr, NULL);
2125
2126 if (!key)
2127 goto end;
2128 ts = stktable_get_entry(t, key);
2129 if (!ts)
2130 goto end;
2131
2132 stream_track_stkctr(&s->stkctr[rule->action], t, ts);
2133
2134 /* let's count a new HTTP request as it's the first time we do it */
2135 ptr1 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
2136 ptr2 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
2137
2138 /* When the client triggers a 4xx from the server, it's most often due
2139 * to a missing object or permission. These events should be tracked
2140 * because if they happen often, it may indicate a brute force or a
2141 * vulnerability scan. Normally this is done when receiving the response
2142 * but here we're tracking after this ought to have been done so we have
2143 * to do it on purpose.
2144 */
2145 if (rule->from == ACT_F_HTTP_RES && (unsigned)(s->txn->status - 400) < 100) {
2146 ptr3 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_CNT);
2147 ptr4 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_RATE);
2148 }
2149
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002150 if (rule->from == ACT_F_HTTP_RES && (unsigned)(s->txn->status - 500) < 100 &&
2151 s->txn->status != 501 && s->txn->status != 505) {
2152 ptr5 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_FAIL_CNT);
2153 ptr6 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_FAIL_RATE);
2154 }
2155
2156 if (ptr1 || ptr2 || ptr3 || ptr4 || ptr5 || ptr6) {
Christopher Fauletac98d812019-12-18 09:20:16 +01002157 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
2158
2159 if (ptr1)
2160 stktable_data_cast(ptr1, http_req_cnt)++;
2161 if (ptr2)
2162 update_freq_ctr_period(&stktable_data_cast(ptr2, http_req_rate),
2163 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
2164 if (ptr3)
2165 stktable_data_cast(ptr3, http_err_cnt)++;
2166 if (ptr4)
2167 update_freq_ctr_period(&stktable_data_cast(ptr4, http_err_rate),
2168 t->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1);
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002169 if (ptr5)
2170 stktable_data_cast(ptr5, http_fail_cnt)++;
2171 if (ptr6)
2172 update_freq_ctr_period(&stktable_data_cast(ptr6, http_fail_rate),
2173 t->data_arg[STKTABLE_DT_HTTP_FAIL_RATE].u, 1);
Christopher Fauletac98d812019-12-18 09:20:16 +01002174
2175 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
2176
2177 /* If data was modified, we need to touch to re-schedule sync */
2178 stktable_touch_local(t, ts, 0);
2179 }
2180
2181 stkctr_set_flags(&s->stkctr[rule->action], STKCTR_TRACK_CONTENT);
2182 if (sess->fe != s->be)
2183 stkctr_set_flags(&s->stkctr[rule->action], STKCTR_TRACK_BACKEND);
2184
2185 end:
2186 return ACT_RET_CONT;
2187}
Christopher Faulet81e20172019-12-12 16:40:30 +01002188
Christopher Faulet2eb53962020-01-14 14:47:34 +01002189static void release_http_track_sc(struct act_rule *rule)
2190{
2191 release_sample_expr(rule->arg.trk_ctr.expr);
2192}
2193
Christopher Faulet81e20172019-12-12 16:40:30 +01002194/* Parse a "track-sc*" actions. It returns ACT_RET_PRS_OK on success,
2195 * ACT_RET_PRS_ERR on error.
2196 */
2197static enum act_parse_ret parse_http_track_sc(const char **args, int *orig_arg, struct proxy *px,
2198 struct act_rule *rule, char **err)
2199{
2200 struct sample_expr *expr;
2201 unsigned int where;
2202 unsigned int tsc_num;
2203 const char *tsc_num_str;
2204 int cur_arg;
2205
2206 tsc_num_str = &args[*orig_arg-1][8];
2207 if (cfg_parse_track_sc_num(&tsc_num, tsc_num_str, tsc_num_str + strlen(tsc_num_str), err) == -1)
2208 return ACT_RET_PRS_ERR;
2209
2210 cur_arg = *orig_arg;
2211 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line,
Willy Tarreaue3b57bf2020-02-14 16:50:14 +01002212 err, &px->conf.args, NULL);
Christopher Faulet81e20172019-12-12 16:40:30 +01002213 if (!expr)
2214 return ACT_RET_PRS_ERR;
2215
2216 where = 0;
2217 if (px->cap & PR_CAP_FE)
2218 where |= (rule->from == ACT_F_HTTP_REQ ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_FE_HRS_HDR);
2219 if (px->cap & PR_CAP_BE)
2220 where |= (rule->from == ACT_F_HTTP_REQ ? SMP_VAL_BE_HRQ_HDR : SMP_VAL_BE_HRS_HDR);
2221
2222 if (!(expr->fetch->val & where)) {
2223 memprintf(err, "fetch method '%s' extracts information from '%s', none of which is available here",
2224 args[cur_arg-1], sample_src_names(expr->fetch->use));
Christopher Faulet1337b322020-01-14 14:50:55 +01002225 release_sample_expr(expr);
Christopher Faulet81e20172019-12-12 16:40:30 +01002226 return ACT_RET_PRS_ERR;
2227 }
2228
2229 if (strcmp(args[cur_arg], "table") == 0) {
2230 cur_arg++;
2231 if (!*args[cur_arg]) {
2232 memprintf(err, "missing table name");
Christopher Faulet1337b322020-01-14 14:50:55 +01002233 release_sample_expr(expr);
Christopher Faulet81e20172019-12-12 16:40:30 +01002234 return ACT_RET_PRS_ERR;
2235 }
2236
2237 /* we copy the table name for now, it will be resolved later */
2238 rule->arg.trk_ctr.table.n = strdup(args[cur_arg]);
2239 cur_arg++;
2240 }
2241
Christopher Fauletac98d812019-12-18 09:20:16 +01002242 rule->action = tsc_num;
Christopher Faulet81e20172019-12-12 16:40:30 +01002243 rule->arg.trk_ctr.expr = expr;
Christopher Fauletac98d812019-12-18 09:20:16 +01002244 rule->action_ptr = http_action_track_sc;
Christopher Faulet2eb53962020-01-14 14:47:34 +01002245 rule->release_ptr = release_http_track_sc;
Christopher Faulet81e20172019-12-12 16:40:30 +01002246 rule->check_ptr = check_trk_action;
2247
2248 *orig_arg = cur_arg;
2249 return ACT_RET_PRS_OK;
2250}
2251
Amaury Denoyelle8d228232020-12-10 13:43:54 +01002252static enum act_return action_timeout_set_stream_timeout(struct act_rule *rule,
2253 struct proxy *px,
2254 struct session *sess,
2255 struct stream *s,
2256 int flags)
2257{
2258 struct sample *key;
2259
2260 if (rule->arg.timeout.expr) {
2261 key = sample_fetch_as_type(px, sess, s, SMP_OPT_FINAL, rule->arg.timeout.expr, SMP_T_SINT);
2262 if (!key)
2263 return ACT_RET_CONT;
2264
2265 stream_set_timeout(s, rule->arg.timeout.type, MS_TO_TICKS(key->data.u.sint));
2266 }
2267 else {
2268 stream_set_timeout(s, rule->arg.timeout.type, MS_TO_TICKS(rule->arg.timeout.value));
2269 }
2270
2271 return ACT_RET_CONT;
2272}
2273
2274/* Parse a "set-timeout" action. Returns ACT_RET_PRS_ERR if parsing error.
2275 */
2276static enum act_parse_ret parse_http_set_timeout(const char **args,
2277 int *orig_arg,
2278 struct proxy *px,
2279 struct act_rule *rule, char **err)
2280{
2281 int cur_arg;
2282
2283 rule->action = ACT_CUSTOM;
2284 rule->action_ptr = action_timeout_set_stream_timeout;
2285 rule->release_ptr = release_timeout_action;
2286
2287 cur_arg = *orig_arg;
2288 if (!*args[cur_arg] || !*args[cur_arg + 1]) {
2289 memprintf(err, "expects exactly 2 arguments");
2290 return ACT_RET_PRS_ERR;
2291 }
2292
2293 if (!(px->cap & PR_CAP_BE)) {
2294 memprintf(err, "proxy '%s' has no backend capability", px->id);
2295 return ACT_RET_PRS_ERR;
2296 }
2297
2298 if (cfg_parse_rule_set_timeout(args, cur_arg,
2299 &rule->arg.timeout.value,
2300 &rule->arg.timeout.type,
2301 &rule->arg.timeout.expr,
2302 err,
2303 px->conf.args.file,
2304 px->conf.args.line, &px->conf.args) == -1) {
2305 return ACT_RET_PRS_ERR;
2306 }
2307
2308 *orig_arg = cur_arg + 2;
2309
2310 return ACT_RET_PRS_OK;
2311}
2312
Christopher Faulet46f95542019-12-20 10:07:22 +01002313/* This function executes a strict-mode actions. On success, it always returns
2314 * ACT_RET_CONT
2315 */
2316static enum act_return http_action_strict_mode(struct act_rule *rule, struct proxy *px,
2317 struct session *sess, struct stream *s, int flags)
2318{
2319 struct http_msg *msg = ((rule->from == ACT_F_HTTP_REQ) ? &s->txn->req : &s->txn->rsp);
2320
2321 if (rule->action == 0) // strict-mode on
2322 msg->flags &= ~HTTP_MSGF_SOFT_RW;
2323 else // strict-mode off
2324 msg->flags |= HTTP_MSGF_SOFT_RW;
2325 return ACT_RET_CONT;
2326}
2327
2328/* Parse a "strict-mode" action. It returns ACT_RET_PRS_OK on success,
2329 * ACT_RET_PRS_ERR on error.
2330 */
2331static enum act_parse_ret parse_http_strict_mode(const char **args, int *orig_arg, struct proxy *px,
2332 struct act_rule *rule, char **err)
2333{
2334 int cur_arg;
2335
Christopher Faulet46f95542019-12-20 10:07:22 +01002336 cur_arg = *orig_arg;
2337 if (!*args[cur_arg]) {
2338 memprintf(err, "expects exactly 1 arguments");
2339 return ACT_RET_PRS_ERR;
2340 }
2341
2342 if (strcasecmp(args[cur_arg], "on") == 0)
2343 rule->action = 0; // strict-mode on
2344 else if (strcasecmp(args[cur_arg], "off") == 0)
2345 rule->action = 1; // strict-mode off
2346 else {
2347 memprintf(err, "Unexpected value '%s'. Only 'on' and 'off' are supported", args[cur_arg]);
2348 return ACT_RET_PRS_ERR;
2349 }
2350 rule->action_ptr = http_action_strict_mode;
2351
2352 *orig_arg = cur_arg + 1;
2353 return ACT_RET_PRS_OK;
2354}
2355
Christopher Faulet24231ab2020-01-24 17:44:23 +01002356/* This function executes a return action. It builds an HTX message from an
2357 * errorfile, an raw file or a log-format string, depending on <.action>
2358 * value. On success, it returns ACT_RET_ABRT. If an error occurs ACT_RET_ERR is
2359 * returned.
2360 */
2361static enum act_return http_action_return(struct act_rule *rule, struct proxy *px,
2362 struct session *sess, struct stream *s, int flags)
2363{
2364 struct channel *req = &s->req;
Christopher Faulet24231ab2020-01-24 17:44:23 +01002365
Christopher Faulet2d36df22021-02-19 11:41:01 +01002366 s->txn->status = rule->arg.http_reply->status;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02002367 if (http_reply_message(s, rule->arg.http_reply) == -1)
2368 return ACT_RET_ERR;
Christopher Faulet24231ab2020-01-24 17:44:23 +01002369
Christopher Faulet24231ab2020-01-24 17:44:23 +01002370 if (rule->from == ACT_F_HTTP_REQ) {
2371 /* let's log the request time */
2372 s->logs.tv_request = now;
2373 req->analysers &= AN_REQ_FLT_END;
2374
2375 if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Willy Tarreau4781b152021-04-06 13:53:36 +02002376 _HA_ATOMIC_INC(&s->sess->fe->fe_counters.intercepted_req);
Christopher Faulet24231ab2020-01-24 17:44:23 +01002377 }
2378
2379 if (!(s->flags & SF_ERR_MASK))
2380 s->flags |= SF_ERR_LOCAL;
2381 if (!(s->flags & SF_FINST_MASK))
2382 s->flags |= ((rule->from == ACT_F_HTTP_REQ) ? SF_FINST_R : SF_FINST_H);
2383
Christopher Faulet0e2ad612020-05-13 16:38:37 +02002384 return ACT_RET_ABRT;
Christopher Faulet24231ab2020-01-24 17:44:23 +01002385}
2386
Christopher Faulet24231ab2020-01-24 17:44:23 +01002387/* Parse a "return" action. It returns ACT_RET_PRS_OK on success,
Christopher Faulet47e791e2020-05-13 14:36:55 +02002388 * ACT_RET_PRS_ERR on error. It relies on http_parse_http_reply() to set
2389 * <.arg.http_reply>.
Christopher Faulet24231ab2020-01-24 17:44:23 +01002390 */
2391static enum act_parse_ret parse_http_return(const char **args, int *orig_arg, struct proxy *px,
2392 struct act_rule *rule, char **err)
2393{
Christopher Faulet47e791e2020-05-13 14:36:55 +02002394 /* Prepare parsing of log-format strings */
2395 px->conf.args.ctx = ((rule->from == ACT_F_HTTP_REQ) ? ARGC_HRQ : ARGC_HRS);
2396 rule->arg.http_reply = http_parse_http_reply(args, orig_arg, px, 200, err);
2397 if (!rule->arg.http_reply)
2398 return ACT_RET_PRS_ERR;
Christopher Faulet24231ab2020-01-24 17:44:23 +01002399
Christopher Fauletba946bf2020-05-13 08:50:07 +02002400 rule->flags |= ACT_FLAG_FINAL;
Christopher Faulet5ff0c642020-05-12 18:33:37 +02002401 rule->action = ACT_CUSTOM;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002402 rule->check_ptr = check_act_http_reply;
Christopher Faulet24231ab2020-01-24 17:44:23 +01002403 rule->action_ptr = http_action_return;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002404 rule->release_ptr = release_act_http_reply;
Christopher Faulet24231ab2020-01-24 17:44:23 +01002405 return ACT_RET_PRS_OK;
Christopher Faulet24231ab2020-01-24 17:44:23 +01002406}
2407
Christopher Faulet021a8e42021-03-29 10:46:38 +02002408
2409
2410/* This function executes a wait-for-body action. It waits for the message
2411 * payload for a max configured time (.arg.p[0]) and eventually for only first
2412 * <arg.p[1]> bytes (0 means no limit). It relies on http_wait_for_msg_body()
2413 * function. it returns ACT_RET_CONT when conditions are met to stop to wait.
2414 * Otherwise ACT_RET_YIELD is returned to wait for more data. ACT_RET_INV is
2415 * returned if a parsing error is raised by lower level and ACT_RET_ERR if an
Ilya Shipitsinb2be9a12021-04-24 13:25:42 +05002416 * internal error occurred. Finally ACT_RET_ABRT is returned when a timeout
2417 * occurred.
Christopher Faulet021a8e42021-03-29 10:46:38 +02002418 */
2419static enum act_return http_action_wait_for_body(struct act_rule *rule, struct proxy *px,
2420 struct session *sess, struct stream *s, int flags)
2421{
2422 struct channel *chn = ((rule->from == ACT_F_HTTP_REQ) ? &s->req : &s->res);
2423 unsigned int time = (uintptr_t)rule->arg.act.p[0];
2424 unsigned int bytes = (uintptr_t)rule->arg.act.p[1];
2425
2426 switch (http_wait_for_msg_body(s, chn, time, bytes)) {
2427 case HTTP_RULE_RES_CONT:
2428 return ACT_RET_CONT;
2429 case HTTP_RULE_RES_YIELD:
2430 return ACT_RET_YIELD;
2431 case HTTP_RULE_RES_BADREQ:
2432 return ACT_RET_INV;
2433 case HTTP_RULE_RES_ERROR:
2434 return ACT_RET_ERR;
2435 case HTTP_RULE_RES_ABRT:
2436 return ACT_RET_ABRT;
2437 default:
2438 return ACT_RET_ERR;
2439 }
2440}
2441
2442/* Parse a "wait-for-body" action. It returns ACT_RET_PRS_OK on success,
2443 * ACT_RET_PRS_ERR on error.
2444 */
2445static enum act_parse_ret parse_http_wait_for_body(const char **args, int *orig_arg, struct proxy *px,
2446 struct act_rule *rule, char **err)
2447{
2448 int cur_arg;
2449 unsigned int time, bytes;
2450 const char *res;
2451
2452 cur_arg = *orig_arg;
2453 if (!*args[cur_arg]) {
2454 memprintf(err, "expects time <time> [ at-least <bytes> ]");
2455 return ACT_RET_PRS_ERR;
2456 }
2457
2458 time = UINT_MAX; /* To be sure it is set */
2459 bytes = 0; /* Default value, wait all the body */
2460 while (*(args[cur_arg])) {
2461 if (strcmp(args[cur_arg], "time") == 0) {
2462 if (!*args[cur_arg + 1]) {
2463 memprintf(err, "missing argument for '%s'", args[cur_arg]);
2464 return ACT_RET_PRS_ERR;
2465 }
2466 res = parse_time_err(args[cur_arg+1], &time, TIME_UNIT_MS);
2467 if (res == PARSE_TIME_OVER) {
2468 memprintf(err, "time overflow (maximum value is 2147483647 ms or ~24.8 days)");
2469 return ACT_RET_PRS_ERR;
2470 }
2471 if (res == PARSE_TIME_UNDER) {
2472 memprintf(err, "time underflow (minimum non-null value is 1 ms)");
2473 return ACT_RET_PRS_ERR;
2474 }
2475 if (res) {
2476 memprintf(err, "unexpected character '%c'", *res);
2477 return ACT_RET_PRS_ERR;
2478 }
2479 cur_arg++;
2480 }
2481 else if (strcmp(args[cur_arg], "at-least") == 0) {
2482 if (!*args[cur_arg + 1]) {
2483 memprintf(err, "missing argument for '%s'", args[cur_arg]);
2484 return ACT_RET_PRS_ERR;
2485 }
2486 res = parse_size_err(args[cur_arg+1], &bytes);
2487 if (res) {
2488 memprintf(err, "unexpected character '%c'", *res);
2489 return ACT_RET_PRS_ERR;
2490 }
2491 cur_arg++;
2492 }
2493 else
2494 break;
2495 cur_arg++;
2496 }
2497
2498 if (time == UINT_MAX) {
2499 memprintf(err, "expects time <time> [ at-least <bytes> ]");
2500 return ACT_RET_PRS_ERR;
2501 }
2502
2503 rule->arg.act.p[0] = (void *)(uintptr_t)time;
2504 rule->arg.act.p[1] = (void *)(uintptr_t)bytes;
2505
2506 *orig_arg = cur_arg;
2507
2508 rule->action = ACT_CUSTOM;
2509 rule->action_ptr = http_action_wait_for_body;
2510 return ACT_RET_PRS_OK;
2511}
2512
Willy Tarreau79e57332018-10-02 16:01:16 +02002513/************************************************************************/
2514/* All supported http-request action keywords must be declared here. */
2515/************************************************************************/
2516
2517static struct action_kw_list http_req_actions = {
2518 .kw = {
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02002519 { "add-acl", parse_http_set_map, KWF_MATCH_PREFIX },
Christopher Faulet81e20172019-12-12 16:40:30 +01002520 { "add-header", parse_http_set_header, 0 },
2521 { "allow", parse_http_allow, 0 },
2522 { "auth", parse_http_auth, 0 },
2523 { "capture", parse_http_req_capture, 0 },
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02002524 { "del-acl", parse_http_set_map, KWF_MATCH_PREFIX },
Christopher Faulet81e20172019-12-12 16:40:30 +01002525 { "del-header", parse_http_del_header, 0 },
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02002526 { "del-map", parse_http_set_map, KWF_MATCH_PREFIX },
Christopher Faulete0fca292020-01-13 21:49:03 +01002527 { "deny", parse_http_deny, 0 },
Christopher Faulet81e20172019-12-12 16:40:30 +01002528 { "disable-l7-retry", parse_http_req_disable_l7_retry, 0 },
2529 { "early-hint", parse_http_set_header, 0 },
Amaury Denoyellea9e639a2021-05-06 15:50:12 +02002530 { "normalize-uri", parse_http_normalize_uri, KWF_EXPERIMENTAL },
Christopher Faulet81e20172019-12-12 16:40:30 +01002531 { "redirect", parse_http_redirect, 0 },
2532 { "reject", parse_http_action_reject, 0 },
2533 { "replace-header", parse_http_replace_header, 0 },
2534 { "replace-path", parse_replace_uri, 0 },
Christopher Faulet312294f2020-09-02 17:17:44 +02002535 { "replace-pathq", parse_replace_uri, 0 },
Christopher Faulet81e20172019-12-12 16:40:30 +01002536 { "replace-uri", parse_replace_uri, 0 },
2537 { "replace-value", parse_http_replace_header, 0 },
Christopher Faulet24231ab2020-01-24 17:44:23 +01002538 { "return", parse_http_return, 0 },
Christopher Faulet81e20172019-12-12 16:40:30 +01002539 { "set-header", parse_http_set_header, 0 },
2540 { "set-log-level", parse_http_set_log_level, 0 },
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02002541 { "set-map", parse_http_set_map, KWF_MATCH_PREFIX },
Christopher Faulet81e20172019-12-12 16:40:30 +01002542 { "set-method", parse_set_req_line, 0 },
2543 { "set-mark", parse_http_set_mark, 0 },
2544 { "set-nice", parse_http_set_nice, 0 },
2545 { "set-path", parse_set_req_line, 0 },
Christopher Faulet312294f2020-09-02 17:17:44 +02002546 { "set-pathq", parse_set_req_line, 0 },
Christopher Faulet81e20172019-12-12 16:40:30 +01002547 { "set-query", parse_set_req_line, 0 },
2548 { "set-tos", parse_http_set_tos, 0 },
2549 { "set-uri", parse_set_req_line, 0 },
Christopher Faulet46f95542019-12-20 10:07:22 +01002550 { "strict-mode", parse_http_strict_mode, 0 },
Christopher Faulete0fca292020-01-13 21:49:03 +01002551 { "tarpit", parse_http_deny, 0 },
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02002552 { "track-sc", parse_http_track_sc, KWF_MATCH_PREFIX },
Amaury Denoyelle8d228232020-12-10 13:43:54 +01002553 { "set-timeout", parse_http_set_timeout, 0 },
Christopher Faulet021a8e42021-03-29 10:46:38 +02002554 { "wait-for-body", parse_http_wait_for_body, 0 },
Willy Tarreau79e57332018-10-02 16:01:16 +02002555 { NULL, NULL }
2556 }
2557};
2558
Willy Tarreau0108d902018-11-25 19:14:37 +01002559INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_actions);
2560
Willy Tarreau79e57332018-10-02 16:01:16 +02002561static struct action_kw_list http_res_actions = {
2562 .kw = {
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02002563 { "add-acl", parse_http_set_map, KWF_MATCH_PREFIX },
Christopher Faulet81e20172019-12-12 16:40:30 +01002564 { "add-header", parse_http_set_header, 0 },
2565 { "allow", parse_http_allow, 0 },
2566 { "capture", parse_http_res_capture, 0 },
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02002567 { "del-acl", parse_http_set_map, KWF_MATCH_PREFIX },
Christopher Faulet81e20172019-12-12 16:40:30 +01002568 { "del-header", parse_http_del_header, 0 },
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02002569 { "del-map", parse_http_set_map, KWF_MATCH_PREFIX },
Christopher Faulete0fca292020-01-13 21:49:03 +01002570 { "deny", parse_http_deny, 0 },
Christopher Faulet81e20172019-12-12 16:40:30 +01002571 { "redirect", parse_http_redirect, 0 },
2572 { "replace-header", parse_http_replace_header, 0 },
2573 { "replace-value", parse_http_replace_header, 0 },
Christopher Faulet24231ab2020-01-24 17:44:23 +01002574 { "return", parse_http_return, 0 },
Christopher Faulet81e20172019-12-12 16:40:30 +01002575 { "set-header", parse_http_set_header, 0 },
2576 { "set-log-level", parse_http_set_log_level, 0 },
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02002577 { "set-map", parse_http_set_map, KWF_MATCH_PREFIX },
Christopher Faulet81e20172019-12-12 16:40:30 +01002578 { "set-mark", parse_http_set_mark, 0 },
2579 { "set-nice", parse_http_set_nice, 0 },
2580 { "set-status", parse_http_set_status, 0 },
2581 { "set-tos", parse_http_set_tos, 0 },
Christopher Faulet46f95542019-12-20 10:07:22 +01002582 { "strict-mode", parse_http_strict_mode, 0 },
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02002583 { "track-sc", parse_http_track_sc, KWF_MATCH_PREFIX },
Christopher Faulet021a8e42021-03-29 10:46:38 +02002584 { "wait-for-body", parse_http_wait_for_body, 0 },
Willy Tarreau79e57332018-10-02 16:01:16 +02002585 { NULL, NULL }
2586 }
2587};
2588
Willy Tarreau0108d902018-11-25 19:14:37 +01002589INITCALL1(STG_REGISTER, http_res_keywords_register, &http_res_actions);
Willy Tarreau79e57332018-10-02 16:01:16 +02002590
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002591static struct action_kw_list http_after_res_actions = {
2592 .kw = {
2593 { "add-header", parse_http_set_header, 0 },
2594 { "allow", parse_http_allow, 0 },
2595 { "del-header", parse_http_del_header, 0 },
2596 { "replace-header", parse_http_replace_header, 0 },
2597 { "replace-value", parse_http_replace_header, 0 },
2598 { "set-header", parse_http_set_header, 0 },
2599 { "set-status", parse_http_set_status, 0 },
2600 { "strict-mode", parse_http_strict_mode, 0 },
2601 { NULL, NULL }
2602 }
2603};
2604
2605INITCALL1(STG_REGISTER, http_after_res_keywords_register, &http_after_res_actions);
2606
Willy Tarreau79e57332018-10-02 16:01:16 +02002607/*
2608 * Local variables:
2609 * c-indent-level: 8
2610 * c-basic-offset: 8
2611 * End:
2612 */