blob: f88c856f2689f9b4d99eefab2bfe11b215e9c9aa [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{
Christopher Fauletc68a7fa2022-07-05 16:24:15 +02001459 struct act_rule *next_rule;
Christopher Faulet91b3ec12020-01-17 22:30:06 +01001460 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
Christopher Fauletc68a7fa2022-07-05 16:24:15 +02001474 /* if there is no pending 103 response, start a new response. Otherwise,
1475 * continue to add link to a previously started response
1476 */
1477 if (s->txn->status != 103) {
Christopher Faulet91b3ec12020-01-17 22:30:06 +01001478 struct htx_sl *sl;
1479 unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|
1480 HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
1481
1482 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
1483 ist("HTTP/1.1"), ist("103"), ist("Early Hints"));
1484 if (!sl)
1485 goto error;
1486 sl->info.res.status = 103;
Christopher Fauletc68a7fa2022-07-05 16:24:15 +02001487 s->txn->status = 103;
Christopher Faulet91b3ec12020-01-17 22:30:06 +01001488 }
1489
1490 /* Add the HTTP Early Hint HTTP 103 response heade */
1491 value->data = build_logline(s, b_tail(value), b_room(value), &rule->arg.http.fmt);
1492 if (!htx_add_header(htx, rule->arg.http.str, ist2(b_head(value), b_data(value))))
1493 goto error;
1494
Christopher Fauletc68a7fa2022-07-05 16:24:15 +02001495 /* if it is the last rule or the next one is not an early-hint or an
1496 * conditional early-hint, terminate the current response.
1497 */
1498 next_rule = LIST_NEXT(&rule->list, typeof(rule), list);
1499 if (&next_rule->list == s->current_rule_list || next_rule->action_ptr != http_action_early_hint || next_rule->cond) {
1500 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet91b3ec12020-01-17 22:30:06 +01001501 goto error;
Christopher Fauleta72a7e42020-01-28 09:28:11 +01001502 if (!http_forward_proxy_resp(s, 0))
1503 goto error;
Christopher Fauletc68a7fa2022-07-05 16:24:15 +02001504 s->txn->status = 0;
Christopher Faulet91b3ec12020-01-17 22:30:06 +01001505 }
1506
1507 leave:
1508 free_trash_chunk(value);
1509 return ret;
1510
1511 error:
1512 /* If an error occurred during an Early-hint rule, remove the incomplete
1513 * HTTP 103 response from the buffer */
1514 channel_htx_truncate(res, htx);
1515 ret = ACT_RET_ERR;
Christopher Fauletc68a7fa2022-07-05 16:24:15 +02001516 s->txn->status = 0;
Christopher Faulet91b3ec12020-01-17 22:30:06 +01001517 goto leave;
1518}
1519
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001520/* This function executes a set-header or add-header actions. It builds a string
1521 * in the trash from the specified format string. It finds the action to be
1522 * performed in <.action>, previously filled by function parse_set_header(). The
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001523 * replacement action is executed by the function http_action_set_header(). On
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001524 * success, it returns ACT_RET_CONT. If an error occurs while soft rewrites are
1525 * enabled, the action is canceled, but the rule processing continue. Otherwsize
1526 * ACT_RET_ERR is returned.
1527 */
1528static enum act_return http_action_set_header(struct act_rule *rule, struct proxy *px,
1529 struct session *sess, struct stream *s, int flags)
1530{
Christopher Faulet91e31d82020-01-24 15:37:13 +01001531 struct http_msg *msg = ((rule->from == ACT_F_HTTP_REQ) ? &s->txn->req : &s->txn->rsp);
1532 struct htx *htx = htxbuf(&msg->chn->buf);
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001533 enum act_return ret = ACT_RET_CONT;
1534 struct buffer *replace;
1535 struct http_hdr_ctx ctx;
1536 struct ist n, v;
1537
1538 replace = alloc_trash_chunk();
1539 if (!replace)
1540 goto fail_alloc;
1541
1542 replace->data = build_logline(s, replace->area, replace->size, &rule->arg.http.fmt);
1543 n = rule->arg.http.str;
1544 v = ist2(replace->area, replace->data);
1545
1546 if (rule->action == 0) { // set-header
1547 /* remove all occurrences of the header */
1548 ctx.blk = NULL;
1549 while (http_find_header(htx, n, &ctx, 1))
1550 http_remove_header(htx, &ctx);
1551 }
1552
1553 /* Now add header */
1554 if (!http_add_header(htx, n, v))
1555 goto fail_rewrite;
1556
1557 leave:
1558 free_trash_chunk(replace);
1559 return ret;
1560
1561 fail_alloc:
1562 if (!(s->flags & SF_ERR_MASK))
1563 s->flags |= SF_ERR_RESOURCE;
1564 ret = ACT_RET_ERR;
1565 goto leave;
1566
1567 fail_rewrite:
Willy Tarreau4781b152021-04-06 13:53:36 +02001568 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_rewrites);
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001569 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +02001570 _HA_ATOMIC_INC(&s->be->be_counters.failed_rewrites);
William Lallemand36119de2021-03-08 15:26:48 +01001571 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001572 _HA_ATOMIC_INC(&sess->listener->counters->failed_rewrites);
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001573 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001574 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_rewrites);
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001575
Christopher Faulet333bf8c2020-01-22 14:38:05 +01001576 if (!(msg->flags & HTTP_MSGF_SOFT_RW)) {
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001577 ret = ACT_RET_ERR;
Christopher Faulet333bf8c2020-01-22 14:38:05 +01001578 if (!(s->flags & SF_ERR_MASK))
1579 s->flags |= SF_ERR_PRXCOND;
1580 }
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001581 goto leave;
1582}
1583
Christopher Faulet81e20172019-12-12 16:40:30 +01001584/* Parse a "set-header", "add-header" or "early-hint" actions. It takes an
1585 * header name and a log-format string as arguments. It returns ACT_RET_PRS_OK
1586 * on success, ACT_RET_PRS_ERR on error.
1587 *
1588 * Note: same function is used for the request and the response. However
1589 * "early-hint" rules are only supported for request rules.
1590 */
1591static enum act_parse_ret parse_http_set_header(const char **args, int *orig_arg, struct proxy *px,
1592 struct act_rule *rule, char **err)
1593{
Christopher Faulet5f802b32021-10-13 17:22:17 +02001594 int cap = 0, cur_arg;
Christopher Faulet81e20172019-12-12 16:40:30 +01001595
Christopher Faulet91b3ec12020-01-17 22:30:06 +01001596 if (args[*orig_arg-1][0] == 'e') {
1597 rule->action = ACT_CUSTOM;
1598 rule->action_ptr = http_action_early_hint;
1599 }
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001600 else {
1601 if (args[*orig_arg-1][0] == 's')
1602 rule->action = 0; // set-header
1603 else
1604 rule->action = 1; // add-header
1605 rule->action_ptr = http_action_set_header;
1606 }
Christopher Faulet2eb53962020-01-14 14:47:34 +01001607 rule->release_ptr = release_http_action;
Christopher Faulet81e20172019-12-12 16:40:30 +01001608
1609 cur_arg = *orig_arg;
1610 if (!*args[cur_arg] || !*args[cur_arg+1]) {
1611 memprintf(err, "expects exactly 2 arguments");
1612 return ACT_RET_PRS_ERR;
1613 }
1614
Christopher Faulet81e20172019-12-12 16:40:30 +01001615
Christopher Faulet96bff762019-12-17 13:46:18 +01001616 rule->arg.http.str.ptr = strdup(args[cur_arg]);
1617 rule->arg.http.str.len = strlen(rule->arg.http.str.ptr);
1618 LIST_INIT(&rule->arg.http.fmt);
Christopher Faulet81e20172019-12-12 16:40:30 +01001619
1620 if (rule->from == ACT_F_HTTP_REQ) {
1621 px->conf.args.ctx = ARGC_HRQ;
Christopher Faulet5f802b32021-10-13 17:22:17 +02001622 if (px->cap & PR_CAP_FE)
1623 cap |= SMP_VAL_FE_HRQ_HDR;
1624 if (px->cap & PR_CAP_BE)
1625 cap |= SMP_VAL_BE_HRQ_HDR;
Christopher Faulet81e20172019-12-12 16:40:30 +01001626 }
1627 else{
1628 px->conf.args.ctx = ARGC_HRS;
Christopher Faulet5f802b32021-10-13 17:22:17 +02001629 if (px->cap & PR_CAP_FE)
1630 cap |= SMP_VAL_FE_HRS_HDR;
1631 if (px->cap & PR_CAP_BE)
1632 cap |= SMP_VAL_BE_HRS_HDR;
Christopher Faulet81e20172019-12-12 16:40:30 +01001633 }
1634
1635 cur_arg++;
Christopher Faulet1337b322020-01-14 14:50:55 +01001636 if (!parse_logformat_string(args[cur_arg], px, &rule->arg.http.fmt, LOG_OPT_HTTP, cap, err)) {
Tim Duesterhused526372020-03-05 17:56:33 +01001637 istfree(&rule->arg.http.str);
Christopher Faulet81e20172019-12-12 16:40:30 +01001638 return ACT_RET_PRS_ERR;
Christopher Faulet1337b322020-01-14 14:50:55 +01001639 }
Christopher Faulet81e20172019-12-12 16:40:30 +01001640
1641 free(px->conf.lfs_file);
1642 px->conf.lfs_file = strdup(px->conf.args.file);
1643 px->conf.lfs_line = px->conf.args.line;
1644
1645 *orig_arg = cur_arg + 1;
1646 return ACT_RET_PRS_OK;
1647}
1648
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001649/* This function executes a replace-header or replace-value actions. It
1650 * builds a string in the trash from the specified format string. It finds
1651 * the action to be performed in <.action>, previously filled by function
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001652 * parse_replace_header(). The replacement action is executed by the function
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001653 * http_action_replace_header(). On success, it returns ACT_RET_CONT. If an error
1654 * occurs while soft rewrites are enabled, the action is canceled, but the rule
1655 * processing continue. Otherwsize ACT_RET_ERR is returned.
1656 */
1657static enum act_return http_action_replace_header(struct act_rule *rule, struct proxy *px,
1658 struct session *sess, struct stream *s, int flags)
1659{
Christopher Faulet91e31d82020-01-24 15:37:13 +01001660 struct http_msg *msg = ((rule->from == ACT_F_HTTP_REQ) ? &s->txn->req : &s->txn->rsp);
1661 struct htx *htx = htxbuf(&msg->chn->buf);
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001662 enum act_return ret = ACT_RET_CONT;
1663 struct buffer *replace;
1664 int r;
1665
1666 replace = alloc_trash_chunk();
1667 if (!replace)
1668 goto fail_alloc;
1669
1670 replace->data = build_logline(s, replace->area, replace->size, &rule->arg.http.fmt);
1671
1672 r = http_replace_hdrs(s, htx, rule->arg.http.str, replace->area, rule->arg.http.re, (rule->action == 0));
1673 if (r == -1)
1674 goto fail_rewrite;
1675
1676 leave:
1677 free_trash_chunk(replace);
1678 return ret;
1679
1680 fail_alloc:
1681 if (!(s->flags & SF_ERR_MASK))
1682 s->flags |= SF_ERR_RESOURCE;
1683 ret = ACT_RET_ERR;
1684 goto leave;
1685
1686 fail_rewrite:
Willy Tarreau4781b152021-04-06 13:53:36 +02001687 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_rewrites);
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001688 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +02001689 _HA_ATOMIC_INC(&s->be->be_counters.failed_rewrites);
William Lallemand36119de2021-03-08 15:26:48 +01001690 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001691 _HA_ATOMIC_INC(&sess->listener->counters->failed_rewrites);
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001692 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001693 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_rewrites);
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001694
Christopher Faulet333bf8c2020-01-22 14:38:05 +01001695 if (!(msg->flags & HTTP_MSGF_SOFT_RW)) {
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001696 ret = ACT_RET_ERR;
Christopher Faulet333bf8c2020-01-22 14:38:05 +01001697 if (!(s->flags & SF_ERR_MASK))
1698 s->flags |= SF_ERR_PRXCOND;
1699 }
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001700 goto leave;
1701}
1702
Christopher Faulet81e20172019-12-12 16:40:30 +01001703/* Parse a "replace-header" or "replace-value" actions. It takes an header name,
1704 * a regex and replacement string as arguments. It returns ACT_RET_PRS_OK on
1705 * success, ACT_RET_PRS_ERR on error.
1706 */
1707static enum act_parse_ret parse_http_replace_header(const char **args, int *orig_arg, struct proxy *px,
1708 struct act_rule *rule, char **err)
1709{
Christopher Faulet5f802b32021-10-13 17:22:17 +02001710 int cap = 0, cur_arg;
Christopher Faulet81e20172019-12-12 16:40:30 +01001711
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001712 if (args[*orig_arg-1][8] == 'h')
1713 rule->action = 0; // replace-header
1714 else
1715 rule->action = 1; // replace-value
1716 rule->action_ptr = http_action_replace_header;
Christopher Faulet2eb53962020-01-14 14:47:34 +01001717 rule->release_ptr = release_http_action;
Christopher Faulet81e20172019-12-12 16:40:30 +01001718
1719 cur_arg = *orig_arg;
1720 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2]) {
1721 memprintf(err, "expects exactly 3 arguments");
1722 return ACT_RET_PRS_ERR;
1723 }
1724
Christopher Faulet96bff762019-12-17 13:46:18 +01001725 rule->arg.http.str.ptr = strdup(args[cur_arg]);
1726 rule->arg.http.str.len = strlen(rule->arg.http.str.ptr);
1727 LIST_INIT(&rule->arg.http.fmt);
Christopher Faulet81e20172019-12-12 16:40:30 +01001728
1729 cur_arg++;
Christopher Faulet1337b322020-01-14 14:50:55 +01001730 if (!(rule->arg.http.re = regex_comp(args[cur_arg], 1, 1, err))) {
Tim Duesterhused526372020-03-05 17:56:33 +01001731 istfree(&rule->arg.http.str);
Christopher Faulet81e20172019-12-12 16:40:30 +01001732 return ACT_RET_PRS_ERR;
Christopher Faulet1337b322020-01-14 14:50:55 +01001733 }
Christopher Faulet81e20172019-12-12 16:40:30 +01001734
1735 if (rule->from == ACT_F_HTTP_REQ) {
1736 px->conf.args.ctx = ARGC_HRQ;
Christopher Faulet5f802b32021-10-13 17:22:17 +02001737 if (px->cap & PR_CAP_FE)
1738 cap |= SMP_VAL_FE_HRQ_HDR;
1739 if (px->cap & PR_CAP_BE)
1740 cap |= SMP_VAL_BE_HRQ_HDR;
Christopher Faulet81e20172019-12-12 16:40:30 +01001741 }
1742 else{
1743 px->conf.args.ctx = ARGC_HRS;
Christopher Faulet5f802b32021-10-13 17:22:17 +02001744 if (px->cap & PR_CAP_FE)
1745 cap |= SMP_VAL_FE_HRS_HDR;
1746 if (px->cap & PR_CAP_BE)
1747 cap |= SMP_VAL_BE_HRS_HDR;
Christopher Faulet81e20172019-12-12 16:40:30 +01001748 }
1749
1750 cur_arg++;
Christopher Faulet1337b322020-01-14 14:50:55 +01001751 if (!parse_logformat_string(args[cur_arg], px, &rule->arg.http.fmt, LOG_OPT_HTTP, cap, err)) {
Tim Duesterhused526372020-03-05 17:56:33 +01001752 istfree(&rule->arg.http.str);
Christopher Faulet1337b322020-01-14 14:50:55 +01001753 regex_free(rule->arg.http.re);
Christopher Faulet81e20172019-12-12 16:40:30 +01001754 return ACT_RET_PRS_ERR;
Christopher Faulet1337b322020-01-14 14:50:55 +01001755 }
Christopher Faulet81e20172019-12-12 16:40:30 +01001756
1757 free(px->conf.lfs_file);
1758 px->conf.lfs_file = strdup(px->conf.args.file);
1759 px->conf.lfs_line = px->conf.args.line;
1760
1761 *orig_arg = cur_arg + 1;
1762 return ACT_RET_PRS_OK;
1763}
1764
Maciej Zdebebdd4c52020-11-20 13:58:48 +00001765/* This function executes a del-header action with selected matching mode for
1766 * header name. It finds the matching method to be performed in <.action>, previously
1767 * filled by function parse_http_del_header(). On success, it returns ACT_RET_CONT.
1768 * Otherwise ACT_RET_ERR is returned.
1769 */
1770static enum act_return http_action_del_header(struct act_rule *rule, struct proxy *px,
1771 struct session *sess, struct stream *s, int flags)
1772{
1773 struct http_hdr_ctx ctx;
1774 struct http_msg *msg = ((rule->from == ACT_F_HTTP_REQ) ? &s->txn->req : &s->txn->rsp);
1775 struct htx *htx = htxbuf(&msg->chn->buf);
1776 enum act_return ret = ACT_RET_CONT;
1777
1778 /* remove all occurrences of the header */
1779 ctx.blk = NULL;
1780 switch (rule->action) {
1781 case PAT_MATCH_STR:
1782 while (http_find_header(htx, rule->arg.http.str, &ctx, 1))
1783 http_remove_header(htx, &ctx);
1784 break;
1785 case PAT_MATCH_BEG:
1786 while (http_find_pfx_header(htx, rule->arg.http.str, &ctx, 1))
1787 http_remove_header(htx, &ctx);
1788 break;
1789 case PAT_MATCH_END:
1790 while (http_find_sfx_header(htx, rule->arg.http.str, &ctx, 1))
1791 http_remove_header(htx, &ctx);
1792 break;
1793 case PAT_MATCH_SUB:
1794 while (http_find_sub_header(htx, rule->arg.http.str, &ctx, 1))
1795 http_remove_header(htx, &ctx);
1796 break;
1797 case PAT_MATCH_REG:
1798 while (http_match_header(htx, rule->arg.http.re, &ctx, 1))
1799 http_remove_header(htx, &ctx);
1800 break;
1801 default:
1802 return ACT_RET_ERR;
1803 }
1804 return ret;
1805}
1806
1807/* Parse a "del-header" action. It takes string as a required argument,
1808 * optional flag (currently only -m) and optional matching method of input string
1809 * with header name to be deleted. Default matching method is exact match (-m str).
1810 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Christopher Faulet81e20172019-12-12 16:40:30 +01001811 */
1812static enum act_parse_ret parse_http_del_header(const char **args, int *orig_arg, struct proxy *px,
1813 struct act_rule *rule, char **err)
1814{
1815 int cur_arg;
Maciej Zdebebdd4c52020-11-20 13:58:48 +00001816 int pat_idx;
Christopher Faulet81e20172019-12-12 16:40:30 +01001817
Maciej Zdebebdd4c52020-11-20 13:58:48 +00001818 /* set exact matching (-m str) as default */
1819 rule->action = PAT_MATCH_STR;
1820 rule->action_ptr = http_action_del_header;
Christopher Faulet2eb53962020-01-14 14:47:34 +01001821 rule->release_ptr = release_http_action;
Christopher Faulet81e20172019-12-12 16:40:30 +01001822
1823 cur_arg = *orig_arg;
1824 if (!*args[cur_arg]) {
Maciej Zdebebdd4c52020-11-20 13:58:48 +00001825 memprintf(err, "expects at least 1 argument");
Christopher Faulet81e20172019-12-12 16:40:30 +01001826 return ACT_RET_PRS_ERR;
1827 }
1828
Christopher Faulet96bff762019-12-17 13:46:18 +01001829 rule->arg.http.str.ptr = strdup(args[cur_arg]);
1830 rule->arg.http.str.len = strlen(rule->arg.http.str.ptr);
Christopher Faulet81e20172019-12-12 16:40:30 +01001831 px->conf.args.ctx = (rule->from == ACT_F_HTTP_REQ ? ARGC_HRQ : ARGC_HRS);
1832
Maciej Zdeb6dee9962020-11-23 16:03:09 +00001833 LIST_INIT(&rule->arg.http.fmt);
Maciej Zdebebdd4c52020-11-20 13:58:48 +00001834 if (strcmp(args[cur_arg+1], "-m") == 0) {
1835 cur_arg++;
1836 if (!*args[cur_arg+1]) {
1837 memprintf(err, "-m flag expects exactly 1 argument");
1838 return ACT_RET_PRS_ERR;
1839 }
1840
1841 cur_arg++;
1842 pat_idx = pat_find_match_name(args[cur_arg]);
1843 switch (pat_idx) {
1844 case PAT_MATCH_REG:
1845 if (!(rule->arg.http.re = regex_comp(rule->arg.http.str.ptr, 1, 1, err)))
1846 return ACT_RET_PRS_ERR;
1847 /* fall through */
1848 case PAT_MATCH_STR:
1849 case PAT_MATCH_BEG:
1850 case PAT_MATCH_END:
1851 case PAT_MATCH_SUB:
1852 rule->action = pat_idx;
1853 break;
1854 default:
1855 memprintf(err, "-m with unsupported matching method '%s'", args[cur_arg]);
1856 return ACT_RET_PRS_ERR;
1857 }
1858 }
1859
Christopher Faulet81e20172019-12-12 16:40:30 +01001860 *orig_arg = cur_arg + 1;
1861 return ACT_RET_PRS_OK;
1862}
1863
Christopher Faulet2eb53962020-01-14 14:47:34 +01001864/* Release memory allocated by an http redirect action. */
1865static void release_http_redir(struct act_rule *rule)
1866{
1867 struct logformat_node *lf, *lfb;
1868 struct redirect_rule *redir;
1869
1870 redir = rule->arg.redir;
Willy Tarreau2b718102021-04-21 07:32:39 +02001871 LIST_DELETE(&redir->list);
Christopher Faulet2eb53962020-01-14 14:47:34 +01001872 if (redir->cond) {
1873 prune_acl_cond(redir->cond);
1874 free(redir->cond);
1875 }
1876 free(redir->rdr_str);
1877 free(redir->cookie_str);
1878 list_for_each_entry_safe(lf, lfb, &redir->rdr_fmt, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001879 LIST_DELETE(&lf->list);
Christopher Faulet2eb53962020-01-14 14:47:34 +01001880 free(lf);
1881 }
1882 free(redir);
1883}
1884
Christopher Faulet81e20172019-12-12 16:40:30 +01001885/* Parse a "redirect" action. It returns ACT_RET_PRS_OK on success,
1886 * ACT_RET_PRS_ERR on error.
1887 */
1888static enum act_parse_ret parse_http_redirect(const char **args, int *orig_arg, struct proxy *px,
1889 struct act_rule *rule, char **err)
1890{
1891 struct redirect_rule *redir;
1892 int dir, cur_arg;
1893
1894 rule->action = ACT_HTTP_REDIR;
Christopher Faulet245cf792019-12-18 14:58:12 +01001895 rule->flags |= ACT_FLAG_FINAL;
Christopher Faulet2eb53962020-01-14 14:47:34 +01001896 rule->release_ptr = release_http_redir;
Christopher Faulet81e20172019-12-12 16:40:30 +01001897
1898 cur_arg = *orig_arg;
1899
1900 dir = (rule->from == ACT_F_HTTP_REQ ? 0 : 1);
1901 if ((redir = http_parse_redirect_rule(px->conf.args.file, px->conf.args.line, px, &args[cur_arg], err, 1, dir)) == NULL)
1902 return ACT_RET_PRS_ERR;
1903
1904 rule->arg.redir = redir;
1905 rule->cond = redir->cond;
1906 redir->cond = NULL;
1907
1908 /* skip all arguments */
1909 while (*args[cur_arg])
1910 cur_arg++;
1911
1912 *orig_arg = cur_arg;
1913 return ACT_RET_PRS_OK;
1914}
1915
Christopher Faulet046cf442019-12-17 15:45:23 +01001916/* This function executes a add-acl, del-acl, set-map or del-map actions. On
1917 * success, it returns ACT_RET_CONT. Otherwsize ACT_RET_ERR is returned.
1918 */
1919static enum act_return http_action_set_map(struct act_rule *rule, struct proxy *px,
1920 struct session *sess, struct stream *s, int flags)
1921{
1922 struct pat_ref *ref;
1923 struct buffer *key = NULL, *value = NULL;
1924 enum act_return ret = ACT_RET_CONT;
1925
1926 /* collect reference */
1927 ref = pat_ref_lookup(rule->arg.map.ref);
1928 if (!ref)
1929 goto leave;
1930
1931 /* allocate key */
1932 key = alloc_trash_chunk();
1933 if (!key)
1934 goto fail_alloc;
1935
1936 /* collect key */
1937 key->data = build_logline(s, key->area, key->size, &rule->arg.map.key);
1938 key->area[key->data] = '\0';
1939
1940 switch (rule->action) {
1941 case 0: // add-acl
1942 /* add entry only if it does not already exist */
1943 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
1944 if (pat_ref_find_elt(ref, key->area) == NULL)
1945 pat_ref_add(ref, key->area, NULL, NULL);
1946 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
1947 break;
1948
1949 case 1: // set-map
1950 /* allocate value */
1951 value = alloc_trash_chunk();
1952 if (!value)
1953 goto fail_alloc;
1954
1955 /* collect value */
1956 value->data = build_logline(s, value->area, value->size, &rule->arg.map.value);
1957 value->area[value->data] = '\0';
1958
1959 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
1960 if (pat_ref_find_elt(ref, key->area) != NULL) {
1961 /* update entry if it exists */
1962 pat_ref_set(ref, key->area, value->area, NULL);
1963 }
1964 else {
1965 /* insert a new entry */
1966 pat_ref_add(ref, key->area, value->area, NULL);
1967 }
1968 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
1969 break;
1970
1971 case 2: // del-acl
1972 case 3: // del-map
1973 /* returned code: 1=ok, 0=ko */
1974 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
1975 pat_ref_delete(ref, key->area);
1976 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
1977 break;
1978
1979 default:
1980 ret = ACT_RET_ERR;
1981 }
1982
1983
1984 leave:
1985 free_trash_chunk(key);
1986 free_trash_chunk(value);
1987 return ret;
1988
1989 fail_alloc:
1990 if (!(s->flags & SF_ERR_MASK))
1991 s->flags |= SF_ERR_RESOURCE;
1992 ret = ACT_RET_ERR;
1993 goto leave;
1994}
1995
Christopher Faulet2eb53962020-01-14 14:47:34 +01001996/* Release memory allocated by an http map/acl action. */
1997static void release_http_map(struct act_rule *rule)
1998{
1999 struct logformat_node *lf, *lfb;
2000
2001 free(rule->arg.map.ref);
2002 list_for_each_entry_safe(lf, lfb, &rule->arg.map.key, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02002003 LIST_DELETE(&lf->list);
Christopher Faulet2eb53962020-01-14 14:47:34 +01002004 release_sample_expr(lf->expr);
2005 free(lf->arg);
2006 free(lf);
2007 }
2008 if (rule->action == 1) {
2009 list_for_each_entry_safe(lf, lfb, &rule->arg.map.value, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02002010 LIST_DELETE(&lf->list);
Christopher Faulet2eb53962020-01-14 14:47:34 +01002011 release_sample_expr(lf->expr);
2012 free(lf->arg);
2013 free(lf);
2014 }
2015 }
2016}
2017
Christopher Faulet81e20172019-12-12 16:40:30 +01002018/* Parse a "add-acl", "del-acl", "set-map" or "del-map" actions. It takes one or
Christopher Faulet046cf442019-12-17 15:45:23 +01002019 * two log-format string as argument depending on the action. The action is
2020 * stored in <.action> as an int (0=add-acl, 1=set-map, 2=del-acl,
2021 * 3=del-map). It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Christopher Faulet81e20172019-12-12 16:40:30 +01002022 */
2023static enum act_parse_ret parse_http_set_map(const char **args, int *orig_arg, struct proxy *px,
2024 struct act_rule *rule, char **err)
2025{
Christopher Faulet5f802b32021-10-13 17:22:17 +02002026 int cap = 0, cur_arg;
Christopher Faulet81e20172019-12-12 16:40:30 +01002027
Christopher Faulet046cf442019-12-17 15:45:23 +01002028 if (args[*orig_arg-1][0] == 'a') // add-acl
2029 rule->action = 0;
2030 else if (args[*orig_arg-1][0] == 's') // set-map
2031 rule->action = 1;
2032 else if (args[*orig_arg-1][4] == 'a') // del-acl
2033 rule->action = 2;
2034 else if (args[*orig_arg-1][4] == 'm') // del-map
2035 rule->action = 3;
2036 else {
2037 memprintf(err, "internal error: unhandled action '%s'", args[0]);
2038 return ACT_RET_PRS_ERR;
2039 }
2040 rule->action_ptr = http_action_set_map;
Christopher Faulet2eb53962020-01-14 14:47:34 +01002041 rule->release_ptr = release_http_map;
Christopher Faulet81e20172019-12-12 16:40:30 +01002042
2043 cur_arg = *orig_arg;
Christopher Faulet046cf442019-12-17 15:45:23 +01002044 if (rule->action == 1 && (!*args[cur_arg] || !*args[cur_arg+1])) {
2045 /* 2 args for set-map */
Christopher Faulet81e20172019-12-12 16:40:30 +01002046 memprintf(err, "expects exactly 2 arguments");
2047 return ACT_RET_PRS_ERR;
2048 }
2049 else if (!*args[cur_arg]) {
Christopher Faulet046cf442019-12-17 15:45:23 +01002050 /* only one arg for other actions */
Christopher Faulet81e20172019-12-12 16:40:30 +01002051 memprintf(err, "expects exactly 1 arguments");
2052 return ACT_RET_PRS_ERR;
2053 }
2054
2055 /*
2056 * '+ 8' for 'set-map(' (same for del-map)
2057 * '- 9' for 'set-map(' + trailing ')' (same for del-map)
2058 */
2059 rule->arg.map.ref = my_strndup(args[cur_arg-1] + 8, strlen(args[cur_arg-1]) - 9);
2060
2061 if (rule->from == ACT_F_HTTP_REQ) {
2062 px->conf.args.ctx = ARGC_HRQ;
Christopher Faulet5f802b32021-10-13 17:22:17 +02002063 if (px->cap & PR_CAP_FE)
2064 cap |= SMP_VAL_FE_HRQ_HDR;
2065 if (px->cap & PR_CAP_BE)
2066 cap |= SMP_VAL_BE_HRQ_HDR;
Christopher Faulet81e20172019-12-12 16:40:30 +01002067 }
2068 else{
2069 px->conf.args.ctx = ARGC_HRS;
Christopher Faulet5f802b32021-10-13 17:22:17 +02002070 if (px->cap & PR_CAP_FE)
2071 cap |= SMP_VAL_FE_HRS_HDR;
2072 if (px->cap & PR_CAP_BE)
2073 cap |= SMP_VAL_BE_HRS_HDR;
Christopher Faulet81e20172019-12-12 16:40:30 +01002074 }
2075
2076 /* key pattern */
2077 LIST_INIT(&rule->arg.map.key);
Christopher Faulet1337b322020-01-14 14:50:55 +01002078 if (!parse_logformat_string(args[cur_arg], px, &rule->arg.map.key, LOG_OPT_HTTP, cap, err)) {
2079 free(rule->arg.map.ref);
Christopher Faulet81e20172019-12-12 16:40:30 +01002080 return ACT_RET_PRS_ERR;
Christopher Faulet1337b322020-01-14 14:50:55 +01002081 }
Christopher Faulet81e20172019-12-12 16:40:30 +01002082
Christopher Faulet046cf442019-12-17 15:45:23 +01002083 if (rule->action == 1) {
Christopher Faulet81e20172019-12-12 16:40:30 +01002084 /* value pattern for set-map only */
2085 cur_arg++;
2086 LIST_INIT(&rule->arg.map.value);
Christopher Faulet1337b322020-01-14 14:50:55 +01002087 if (!parse_logformat_string(args[cur_arg], px, &rule->arg.map.value, LOG_OPT_HTTP, cap, err)) {
2088 free(rule->arg.map.ref);
Christopher Faulet81e20172019-12-12 16:40:30 +01002089 return ACT_RET_PRS_ERR;
Christopher Faulet1337b322020-01-14 14:50:55 +01002090 }
Christopher Faulet81e20172019-12-12 16:40:30 +01002091 }
2092
2093 free(px->conf.lfs_file);
2094 px->conf.lfs_file = strdup(px->conf.args.file);
2095 px->conf.lfs_line = px->conf.args.line;
2096
2097 *orig_arg = cur_arg + 1;
2098 return ACT_RET_PRS_OK;
2099}
2100
Christopher Fauletac98d812019-12-18 09:20:16 +01002101/* This function executes a track-sc* actions. On success, it returns
2102 * ACT_RET_CONT. Otherwsize ACT_RET_ERR is returned.
2103 */
2104static enum act_return http_action_track_sc(struct act_rule *rule, struct proxy *px,
2105 struct session *sess, struct stream *s, int flags)
2106{
2107 struct stktable *t;
2108 struct stksess *ts;
2109 struct stktable_key *key;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002110 void *ptr1, *ptr2, *ptr3, *ptr4, *ptr5, *ptr6;
Christopher Fauletac98d812019-12-18 09:20:16 +01002111 int opt;
2112
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002113 ptr1 = ptr2 = ptr3 = ptr4 = ptr5 = ptr6 = NULL;
Christopher Fauletac98d812019-12-18 09:20:16 +01002114 opt = ((rule->from == ACT_F_HTTP_REQ) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES) | SMP_OPT_FINAL;
2115
2116 t = rule->arg.trk_ctr.table.t;
Emeric Brun362d25e2021-03-10 16:58:03 +01002117
2118 if (stkctr_entry(&s->stkctr[rule->action]))
2119 goto end;
2120
Christopher Fauletac98d812019-12-18 09:20:16 +01002121 key = stktable_fetch_key(t, s->be, sess, s, opt, rule->arg.trk_ctr.expr, NULL);
2122
2123 if (!key)
2124 goto end;
2125 ts = stktable_get_entry(t, key);
2126 if (!ts)
2127 goto end;
2128
2129 stream_track_stkctr(&s->stkctr[rule->action], t, ts);
2130
2131 /* let's count a new HTTP request as it's the first time we do it */
2132 ptr1 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
2133 ptr2 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
2134
2135 /* When the client triggers a 4xx from the server, it's most often due
2136 * to a missing object or permission. These events should be tracked
2137 * because if they happen often, it may indicate a brute force or a
2138 * vulnerability scan. Normally this is done when receiving the response
2139 * but here we're tracking after this ought to have been done so we have
2140 * to do it on purpose.
2141 */
2142 if (rule->from == ACT_F_HTTP_RES && (unsigned)(s->txn->status - 400) < 100) {
2143 ptr3 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_CNT);
2144 ptr4 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_RATE);
2145 }
2146
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002147 if (rule->from == ACT_F_HTTP_RES && (unsigned)(s->txn->status - 500) < 100 &&
2148 s->txn->status != 501 && s->txn->status != 505) {
2149 ptr5 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_FAIL_CNT);
2150 ptr6 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_FAIL_RATE);
2151 }
2152
2153 if (ptr1 || ptr2 || ptr3 || ptr4 || ptr5 || ptr6) {
Christopher Fauletac98d812019-12-18 09:20:16 +01002154 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
2155
2156 if (ptr1)
2157 stktable_data_cast(ptr1, http_req_cnt)++;
2158 if (ptr2)
2159 update_freq_ctr_period(&stktable_data_cast(ptr2, http_req_rate),
2160 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
2161 if (ptr3)
2162 stktable_data_cast(ptr3, http_err_cnt)++;
2163 if (ptr4)
2164 update_freq_ctr_period(&stktable_data_cast(ptr4, http_err_rate),
2165 t->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1);
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002166 if (ptr5)
2167 stktable_data_cast(ptr5, http_fail_cnt)++;
2168 if (ptr6)
2169 update_freq_ctr_period(&stktable_data_cast(ptr6, http_fail_rate),
2170 t->data_arg[STKTABLE_DT_HTTP_FAIL_RATE].u, 1);
Christopher Fauletac98d812019-12-18 09:20:16 +01002171
2172 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
2173
2174 /* If data was modified, we need to touch to re-schedule sync */
2175 stktable_touch_local(t, ts, 0);
2176 }
2177
2178 stkctr_set_flags(&s->stkctr[rule->action], STKCTR_TRACK_CONTENT);
2179 if (sess->fe != s->be)
2180 stkctr_set_flags(&s->stkctr[rule->action], STKCTR_TRACK_BACKEND);
2181
2182 end:
2183 return ACT_RET_CONT;
2184}
Christopher Faulet81e20172019-12-12 16:40:30 +01002185
Christopher Faulet2eb53962020-01-14 14:47:34 +01002186static void release_http_track_sc(struct act_rule *rule)
2187{
2188 release_sample_expr(rule->arg.trk_ctr.expr);
2189}
2190
Christopher Faulet81e20172019-12-12 16:40:30 +01002191/* Parse a "track-sc*" actions. It returns ACT_RET_PRS_OK on success,
2192 * ACT_RET_PRS_ERR on error.
2193 */
2194static enum act_parse_ret parse_http_track_sc(const char **args, int *orig_arg, struct proxy *px,
2195 struct act_rule *rule, char **err)
2196{
2197 struct sample_expr *expr;
2198 unsigned int where;
2199 unsigned int tsc_num;
2200 const char *tsc_num_str;
2201 int cur_arg;
2202
2203 tsc_num_str = &args[*orig_arg-1][8];
2204 if (cfg_parse_track_sc_num(&tsc_num, tsc_num_str, tsc_num_str + strlen(tsc_num_str), err) == -1)
2205 return ACT_RET_PRS_ERR;
2206
2207 cur_arg = *orig_arg;
2208 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line,
Willy Tarreaue3b57bf2020-02-14 16:50:14 +01002209 err, &px->conf.args, NULL);
Christopher Faulet81e20172019-12-12 16:40:30 +01002210 if (!expr)
2211 return ACT_RET_PRS_ERR;
2212
2213 where = 0;
2214 if (px->cap & PR_CAP_FE)
2215 where |= (rule->from == ACT_F_HTTP_REQ ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_FE_HRS_HDR);
2216 if (px->cap & PR_CAP_BE)
2217 where |= (rule->from == ACT_F_HTTP_REQ ? SMP_VAL_BE_HRQ_HDR : SMP_VAL_BE_HRS_HDR);
2218
2219 if (!(expr->fetch->val & where)) {
2220 memprintf(err, "fetch method '%s' extracts information from '%s', none of which is available here",
2221 args[cur_arg-1], sample_src_names(expr->fetch->use));
Christopher Faulet1337b322020-01-14 14:50:55 +01002222 release_sample_expr(expr);
Christopher Faulet81e20172019-12-12 16:40:30 +01002223 return ACT_RET_PRS_ERR;
2224 }
2225
2226 if (strcmp(args[cur_arg], "table") == 0) {
2227 cur_arg++;
2228 if (!*args[cur_arg]) {
2229 memprintf(err, "missing table name");
Christopher Faulet1337b322020-01-14 14:50:55 +01002230 release_sample_expr(expr);
Christopher Faulet81e20172019-12-12 16:40:30 +01002231 return ACT_RET_PRS_ERR;
2232 }
2233
2234 /* we copy the table name for now, it will be resolved later */
2235 rule->arg.trk_ctr.table.n = strdup(args[cur_arg]);
2236 cur_arg++;
2237 }
2238
Christopher Fauletac98d812019-12-18 09:20:16 +01002239 rule->action = tsc_num;
Christopher Faulet81e20172019-12-12 16:40:30 +01002240 rule->arg.trk_ctr.expr = expr;
Christopher Fauletac98d812019-12-18 09:20:16 +01002241 rule->action_ptr = http_action_track_sc;
Christopher Faulet2eb53962020-01-14 14:47:34 +01002242 rule->release_ptr = release_http_track_sc;
Christopher Faulet81e20172019-12-12 16:40:30 +01002243 rule->check_ptr = check_trk_action;
2244
2245 *orig_arg = cur_arg;
2246 return ACT_RET_PRS_OK;
2247}
2248
Amaury Denoyelle8d228232020-12-10 13:43:54 +01002249static enum act_return action_timeout_set_stream_timeout(struct act_rule *rule,
2250 struct proxy *px,
2251 struct session *sess,
2252 struct stream *s,
2253 int flags)
2254{
2255 struct sample *key;
2256
2257 if (rule->arg.timeout.expr) {
2258 key = sample_fetch_as_type(px, sess, s, SMP_OPT_FINAL, rule->arg.timeout.expr, SMP_T_SINT);
2259 if (!key)
2260 return ACT_RET_CONT;
2261
2262 stream_set_timeout(s, rule->arg.timeout.type, MS_TO_TICKS(key->data.u.sint));
2263 }
2264 else {
2265 stream_set_timeout(s, rule->arg.timeout.type, MS_TO_TICKS(rule->arg.timeout.value));
2266 }
2267
2268 return ACT_RET_CONT;
2269}
2270
2271/* Parse a "set-timeout" action. Returns ACT_RET_PRS_ERR if parsing error.
2272 */
2273static enum act_parse_ret parse_http_set_timeout(const char **args,
2274 int *orig_arg,
2275 struct proxy *px,
2276 struct act_rule *rule, char **err)
2277{
2278 int cur_arg;
2279
2280 rule->action = ACT_CUSTOM;
2281 rule->action_ptr = action_timeout_set_stream_timeout;
2282 rule->release_ptr = release_timeout_action;
2283
2284 cur_arg = *orig_arg;
2285 if (!*args[cur_arg] || !*args[cur_arg + 1]) {
2286 memprintf(err, "expects exactly 2 arguments");
2287 return ACT_RET_PRS_ERR;
2288 }
2289
2290 if (!(px->cap & PR_CAP_BE)) {
2291 memprintf(err, "proxy '%s' has no backend capability", px->id);
2292 return ACT_RET_PRS_ERR;
2293 }
2294
2295 if (cfg_parse_rule_set_timeout(args, cur_arg,
2296 &rule->arg.timeout.value,
2297 &rule->arg.timeout.type,
2298 &rule->arg.timeout.expr,
2299 err,
2300 px->conf.args.file,
2301 px->conf.args.line, &px->conf.args) == -1) {
2302 return ACT_RET_PRS_ERR;
2303 }
2304
2305 *orig_arg = cur_arg + 2;
2306
2307 return ACT_RET_PRS_OK;
2308}
2309
Christopher Faulet46f95542019-12-20 10:07:22 +01002310/* This function executes a strict-mode actions. On success, it always returns
2311 * ACT_RET_CONT
2312 */
2313static enum act_return http_action_strict_mode(struct act_rule *rule, struct proxy *px,
2314 struct session *sess, struct stream *s, int flags)
2315{
2316 struct http_msg *msg = ((rule->from == ACT_F_HTTP_REQ) ? &s->txn->req : &s->txn->rsp);
2317
2318 if (rule->action == 0) // strict-mode on
2319 msg->flags &= ~HTTP_MSGF_SOFT_RW;
2320 else // strict-mode off
2321 msg->flags |= HTTP_MSGF_SOFT_RW;
2322 return ACT_RET_CONT;
2323}
2324
2325/* Parse a "strict-mode" action. It returns ACT_RET_PRS_OK on success,
2326 * ACT_RET_PRS_ERR on error.
2327 */
2328static enum act_parse_ret parse_http_strict_mode(const char **args, int *orig_arg, struct proxy *px,
2329 struct act_rule *rule, char **err)
2330{
2331 int cur_arg;
2332
Christopher Faulet46f95542019-12-20 10:07:22 +01002333 cur_arg = *orig_arg;
2334 if (!*args[cur_arg]) {
2335 memprintf(err, "expects exactly 1 arguments");
2336 return ACT_RET_PRS_ERR;
2337 }
2338
2339 if (strcasecmp(args[cur_arg], "on") == 0)
2340 rule->action = 0; // strict-mode on
2341 else if (strcasecmp(args[cur_arg], "off") == 0)
2342 rule->action = 1; // strict-mode off
2343 else {
2344 memprintf(err, "Unexpected value '%s'. Only 'on' and 'off' are supported", args[cur_arg]);
2345 return ACT_RET_PRS_ERR;
2346 }
2347 rule->action_ptr = http_action_strict_mode;
2348
2349 *orig_arg = cur_arg + 1;
2350 return ACT_RET_PRS_OK;
2351}
2352
Christopher Faulet24231ab2020-01-24 17:44:23 +01002353/* This function executes a return action. It builds an HTX message from an
2354 * errorfile, an raw file or a log-format string, depending on <.action>
2355 * value. On success, it returns ACT_RET_ABRT. If an error occurs ACT_RET_ERR is
2356 * returned.
2357 */
2358static enum act_return http_action_return(struct act_rule *rule, struct proxy *px,
2359 struct session *sess, struct stream *s, int flags)
2360{
2361 struct channel *req = &s->req;
Christopher Faulet24231ab2020-01-24 17:44:23 +01002362
Christopher Faulet2d36df22021-02-19 11:41:01 +01002363 s->txn->status = rule->arg.http_reply->status;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02002364 if (http_reply_message(s, rule->arg.http_reply) == -1)
2365 return ACT_RET_ERR;
Christopher Faulet24231ab2020-01-24 17:44:23 +01002366
Christopher Faulet24231ab2020-01-24 17:44:23 +01002367 if (rule->from == ACT_F_HTTP_REQ) {
2368 /* let's log the request time */
2369 s->logs.tv_request = now;
2370 req->analysers &= AN_REQ_FLT_END;
2371
2372 if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Willy Tarreau4781b152021-04-06 13:53:36 +02002373 _HA_ATOMIC_INC(&s->sess->fe->fe_counters.intercepted_req);
Christopher Faulet24231ab2020-01-24 17:44:23 +01002374 }
2375
2376 if (!(s->flags & SF_ERR_MASK))
2377 s->flags |= SF_ERR_LOCAL;
2378 if (!(s->flags & SF_FINST_MASK))
2379 s->flags |= ((rule->from == ACT_F_HTTP_REQ) ? SF_FINST_R : SF_FINST_H);
2380
Christopher Faulet0e2ad612020-05-13 16:38:37 +02002381 return ACT_RET_ABRT;
Christopher Faulet24231ab2020-01-24 17:44:23 +01002382}
2383
Christopher Faulet24231ab2020-01-24 17:44:23 +01002384/* Parse a "return" action. It returns ACT_RET_PRS_OK on success,
Christopher Faulet47e791e2020-05-13 14:36:55 +02002385 * ACT_RET_PRS_ERR on error. It relies on http_parse_http_reply() to set
2386 * <.arg.http_reply>.
Christopher Faulet24231ab2020-01-24 17:44:23 +01002387 */
2388static enum act_parse_ret parse_http_return(const char **args, int *orig_arg, struct proxy *px,
2389 struct act_rule *rule, char **err)
2390{
Christopher Faulet47e791e2020-05-13 14:36:55 +02002391 /* Prepare parsing of log-format strings */
2392 px->conf.args.ctx = ((rule->from == ACT_F_HTTP_REQ) ? ARGC_HRQ : ARGC_HRS);
2393 rule->arg.http_reply = http_parse_http_reply(args, orig_arg, px, 200, err);
2394 if (!rule->arg.http_reply)
2395 return ACT_RET_PRS_ERR;
Christopher Faulet24231ab2020-01-24 17:44:23 +01002396
Christopher Fauletba946bf2020-05-13 08:50:07 +02002397 rule->flags |= ACT_FLAG_FINAL;
Christopher Faulet5ff0c642020-05-12 18:33:37 +02002398 rule->action = ACT_CUSTOM;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002399 rule->check_ptr = check_act_http_reply;
Christopher Faulet24231ab2020-01-24 17:44:23 +01002400 rule->action_ptr = http_action_return;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002401 rule->release_ptr = release_act_http_reply;
Christopher Faulet24231ab2020-01-24 17:44:23 +01002402 return ACT_RET_PRS_OK;
Christopher Faulet24231ab2020-01-24 17:44:23 +01002403}
2404
Christopher Faulet021a8e42021-03-29 10:46:38 +02002405
2406
2407/* This function executes a wait-for-body action. It waits for the message
2408 * payload for a max configured time (.arg.p[0]) and eventually for only first
2409 * <arg.p[1]> bytes (0 means no limit). It relies on http_wait_for_msg_body()
2410 * function. it returns ACT_RET_CONT when conditions are met to stop to wait.
2411 * Otherwise ACT_RET_YIELD is returned to wait for more data. ACT_RET_INV is
2412 * returned if a parsing error is raised by lower level and ACT_RET_ERR if an
Ilya Shipitsinb2be9a12021-04-24 13:25:42 +05002413 * internal error occurred. Finally ACT_RET_ABRT is returned when a timeout
2414 * occurred.
Christopher Faulet021a8e42021-03-29 10:46:38 +02002415 */
2416static enum act_return http_action_wait_for_body(struct act_rule *rule, struct proxy *px,
2417 struct session *sess, struct stream *s, int flags)
2418{
2419 struct channel *chn = ((rule->from == ACT_F_HTTP_REQ) ? &s->req : &s->res);
2420 unsigned int time = (uintptr_t)rule->arg.act.p[0];
2421 unsigned int bytes = (uintptr_t)rule->arg.act.p[1];
2422
2423 switch (http_wait_for_msg_body(s, chn, time, bytes)) {
2424 case HTTP_RULE_RES_CONT:
2425 return ACT_RET_CONT;
2426 case HTTP_RULE_RES_YIELD:
2427 return ACT_RET_YIELD;
2428 case HTTP_RULE_RES_BADREQ:
2429 return ACT_RET_INV;
2430 case HTTP_RULE_RES_ERROR:
2431 return ACT_RET_ERR;
2432 case HTTP_RULE_RES_ABRT:
2433 return ACT_RET_ABRT;
2434 default:
2435 return ACT_RET_ERR;
2436 }
2437}
2438
2439/* Parse a "wait-for-body" action. It returns ACT_RET_PRS_OK on success,
2440 * ACT_RET_PRS_ERR on error.
2441 */
2442static enum act_parse_ret parse_http_wait_for_body(const char **args, int *orig_arg, struct proxy *px,
2443 struct act_rule *rule, char **err)
2444{
2445 int cur_arg;
2446 unsigned int time, bytes;
2447 const char *res;
2448
2449 cur_arg = *orig_arg;
2450 if (!*args[cur_arg]) {
2451 memprintf(err, "expects time <time> [ at-least <bytes> ]");
2452 return ACT_RET_PRS_ERR;
2453 }
2454
2455 time = UINT_MAX; /* To be sure it is set */
2456 bytes = 0; /* Default value, wait all the body */
2457 while (*(args[cur_arg])) {
2458 if (strcmp(args[cur_arg], "time") == 0) {
2459 if (!*args[cur_arg + 1]) {
2460 memprintf(err, "missing argument for '%s'", args[cur_arg]);
2461 return ACT_RET_PRS_ERR;
2462 }
2463 res = parse_time_err(args[cur_arg+1], &time, TIME_UNIT_MS);
2464 if (res == PARSE_TIME_OVER) {
2465 memprintf(err, "time overflow (maximum value is 2147483647 ms or ~24.8 days)");
2466 return ACT_RET_PRS_ERR;
2467 }
2468 if (res == PARSE_TIME_UNDER) {
2469 memprintf(err, "time underflow (minimum non-null value is 1 ms)");
2470 return ACT_RET_PRS_ERR;
2471 }
2472 if (res) {
2473 memprintf(err, "unexpected character '%c'", *res);
2474 return ACT_RET_PRS_ERR;
2475 }
2476 cur_arg++;
2477 }
2478 else if (strcmp(args[cur_arg], "at-least") == 0) {
2479 if (!*args[cur_arg + 1]) {
2480 memprintf(err, "missing argument for '%s'", args[cur_arg]);
2481 return ACT_RET_PRS_ERR;
2482 }
2483 res = parse_size_err(args[cur_arg+1], &bytes);
2484 if (res) {
2485 memprintf(err, "unexpected character '%c'", *res);
2486 return ACT_RET_PRS_ERR;
2487 }
2488 cur_arg++;
2489 }
2490 else
2491 break;
2492 cur_arg++;
2493 }
2494
2495 if (time == UINT_MAX) {
2496 memprintf(err, "expects time <time> [ at-least <bytes> ]");
2497 return ACT_RET_PRS_ERR;
2498 }
2499
2500 rule->arg.act.p[0] = (void *)(uintptr_t)time;
2501 rule->arg.act.p[1] = (void *)(uintptr_t)bytes;
2502
2503 *orig_arg = cur_arg;
2504
2505 rule->action = ACT_CUSTOM;
2506 rule->action_ptr = http_action_wait_for_body;
2507 return ACT_RET_PRS_OK;
2508}
2509
Willy Tarreau79e57332018-10-02 16:01:16 +02002510/************************************************************************/
2511/* All supported http-request action keywords must be declared here. */
2512/************************************************************************/
2513
2514static struct action_kw_list http_req_actions = {
2515 .kw = {
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02002516 { "add-acl", parse_http_set_map, KWF_MATCH_PREFIX },
Christopher Faulet81e20172019-12-12 16:40:30 +01002517 { "add-header", parse_http_set_header, 0 },
2518 { "allow", parse_http_allow, 0 },
2519 { "auth", parse_http_auth, 0 },
2520 { "capture", parse_http_req_capture, 0 },
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02002521 { "del-acl", parse_http_set_map, KWF_MATCH_PREFIX },
Christopher Faulet81e20172019-12-12 16:40:30 +01002522 { "del-header", parse_http_del_header, 0 },
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02002523 { "del-map", parse_http_set_map, KWF_MATCH_PREFIX },
Christopher Faulete0fca292020-01-13 21:49:03 +01002524 { "deny", parse_http_deny, 0 },
Christopher Faulet81e20172019-12-12 16:40:30 +01002525 { "disable-l7-retry", parse_http_req_disable_l7_retry, 0 },
2526 { "early-hint", parse_http_set_header, 0 },
Amaury Denoyellea9e639a2021-05-06 15:50:12 +02002527 { "normalize-uri", parse_http_normalize_uri, KWF_EXPERIMENTAL },
Christopher Faulet81e20172019-12-12 16:40:30 +01002528 { "redirect", parse_http_redirect, 0 },
2529 { "reject", parse_http_action_reject, 0 },
2530 { "replace-header", parse_http_replace_header, 0 },
2531 { "replace-path", parse_replace_uri, 0 },
Christopher Faulet312294f2020-09-02 17:17:44 +02002532 { "replace-pathq", parse_replace_uri, 0 },
Christopher Faulet81e20172019-12-12 16:40:30 +01002533 { "replace-uri", parse_replace_uri, 0 },
2534 { "replace-value", parse_http_replace_header, 0 },
Christopher Faulet24231ab2020-01-24 17:44:23 +01002535 { "return", parse_http_return, 0 },
Christopher Faulet81e20172019-12-12 16:40:30 +01002536 { "set-header", parse_http_set_header, 0 },
2537 { "set-log-level", parse_http_set_log_level, 0 },
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02002538 { "set-map", parse_http_set_map, KWF_MATCH_PREFIX },
Christopher Faulet81e20172019-12-12 16:40:30 +01002539 { "set-method", parse_set_req_line, 0 },
2540 { "set-mark", parse_http_set_mark, 0 },
2541 { "set-nice", parse_http_set_nice, 0 },
2542 { "set-path", parse_set_req_line, 0 },
Christopher Faulet312294f2020-09-02 17:17:44 +02002543 { "set-pathq", parse_set_req_line, 0 },
Christopher Faulet81e20172019-12-12 16:40:30 +01002544 { "set-query", parse_set_req_line, 0 },
2545 { "set-tos", parse_http_set_tos, 0 },
2546 { "set-uri", parse_set_req_line, 0 },
Christopher Faulet46f95542019-12-20 10:07:22 +01002547 { "strict-mode", parse_http_strict_mode, 0 },
Christopher Faulete0fca292020-01-13 21:49:03 +01002548 { "tarpit", parse_http_deny, 0 },
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02002549 { "track-sc", parse_http_track_sc, KWF_MATCH_PREFIX },
Amaury Denoyelle8d228232020-12-10 13:43:54 +01002550 { "set-timeout", parse_http_set_timeout, 0 },
Christopher Faulet021a8e42021-03-29 10:46:38 +02002551 { "wait-for-body", parse_http_wait_for_body, 0 },
Willy Tarreau79e57332018-10-02 16:01:16 +02002552 { NULL, NULL }
2553 }
2554};
2555
Willy Tarreau0108d902018-11-25 19:14:37 +01002556INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_actions);
2557
Willy Tarreau79e57332018-10-02 16:01:16 +02002558static struct action_kw_list http_res_actions = {
2559 .kw = {
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02002560 { "add-acl", parse_http_set_map, KWF_MATCH_PREFIX },
Christopher Faulet81e20172019-12-12 16:40:30 +01002561 { "add-header", parse_http_set_header, 0 },
2562 { "allow", parse_http_allow, 0 },
2563 { "capture", parse_http_res_capture, 0 },
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02002564 { "del-acl", parse_http_set_map, KWF_MATCH_PREFIX },
Christopher Faulet81e20172019-12-12 16:40:30 +01002565 { "del-header", parse_http_del_header, 0 },
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02002566 { "del-map", parse_http_set_map, KWF_MATCH_PREFIX },
Christopher Faulete0fca292020-01-13 21:49:03 +01002567 { "deny", parse_http_deny, 0 },
Christopher Faulet81e20172019-12-12 16:40:30 +01002568 { "redirect", parse_http_redirect, 0 },
2569 { "replace-header", parse_http_replace_header, 0 },
2570 { "replace-value", parse_http_replace_header, 0 },
Christopher Faulet24231ab2020-01-24 17:44:23 +01002571 { "return", parse_http_return, 0 },
Christopher Faulet81e20172019-12-12 16:40:30 +01002572 { "set-header", parse_http_set_header, 0 },
2573 { "set-log-level", parse_http_set_log_level, 0 },
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02002574 { "set-map", parse_http_set_map, KWF_MATCH_PREFIX },
Christopher Faulet81e20172019-12-12 16:40:30 +01002575 { "set-mark", parse_http_set_mark, 0 },
2576 { "set-nice", parse_http_set_nice, 0 },
2577 { "set-status", parse_http_set_status, 0 },
2578 { "set-tos", parse_http_set_tos, 0 },
Christopher Faulet46f95542019-12-20 10:07:22 +01002579 { "strict-mode", parse_http_strict_mode, 0 },
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02002580 { "track-sc", parse_http_track_sc, KWF_MATCH_PREFIX },
Christopher Faulet021a8e42021-03-29 10:46:38 +02002581 { "wait-for-body", parse_http_wait_for_body, 0 },
Willy Tarreau79e57332018-10-02 16:01:16 +02002582 { NULL, NULL }
2583 }
2584};
2585
Willy Tarreau0108d902018-11-25 19:14:37 +01002586INITCALL1(STG_REGISTER, http_res_keywords_register, &http_res_actions);
Willy Tarreau79e57332018-10-02 16:01:16 +02002587
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002588static struct action_kw_list http_after_res_actions = {
2589 .kw = {
2590 { "add-header", parse_http_set_header, 0 },
2591 { "allow", parse_http_allow, 0 },
2592 { "del-header", parse_http_del_header, 0 },
2593 { "replace-header", parse_http_replace_header, 0 },
2594 { "replace-value", parse_http_replace_header, 0 },
2595 { "set-header", parse_http_set_header, 0 },
2596 { "set-status", parse_http_set_status, 0 },
2597 { "strict-mode", parse_http_strict_mode, 0 },
2598 { NULL, NULL }
2599 }
2600};
2601
2602INITCALL1(STG_REGISTER, http_after_res_keywords_register, &http_after_res_actions);
2603
Willy Tarreau79e57332018-10-02 16:01:16 +02002604/*
2605 * Local variables:
2606 * c-indent-level: 8
2607 * c-basic-offset: 8
2608 * End:
2609 */