blob: 0aa8a37594b3a15493b3e40a462060edd86516a5 [file] [log] [blame]
Willy Tarreau79e57332018-10-02 16:01:16 +02001/*
2 * HTTP actions
3 *
4 * Copyright 2000-2018 Willy Tarreau <w@1wt.eu>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <sys/types.h>
14
15#include <ctype.h>
16#include <string.h>
17#include <time.h>
18
Willy Tarreaudcc048a2020-06-04 19:11:43 +020019#include <haproxy/acl.h>
Willy Tarreau122eba92020-06-04 10:15:32 +020020#include <haproxy/action.h>
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020021#include <haproxy/api.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020022#include <haproxy/arg.h>
23#include <haproxy/capture-t.h>
Willy Tarreau6be78492020-06-05 00:00:29 +020024#include <haproxy/cfgparse.h>
Willy Tarreauc13ed532020-06-02 10:22:45 +020025#include <haproxy/chunk.h>
Willy Tarreauf268ee82020-06-04 17:05:57 +020026#include <haproxy/global.h>
Willy Tarreaucd72d8c2020-06-02 19:11:26 +020027#include <haproxy/http.h>
Willy Tarreauc2b1ff02020-06-04 21:21:03 +020028#include <haproxy/http_ana.h>
Willy Tarreau87735332020-06-04 09:08:41 +020029#include <haproxy/http_htx.h>
Willy Tarreauc761f842020-06-04 11:40:28 +020030#include <haproxy/http_rules.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020031#include <haproxy/log.h>
Willy Tarreau225a90a2020-06-04 15:06:28 +020032#include <haproxy/pattern.h>
Willy Tarreaud0ef4392020-06-02 09:38:52 +020033#include <haproxy/pool.h>
Willy Tarreau7cd8b6e2020-06-02 17:32:26 +020034#include <haproxy/regex.h>
Willy Tarreaue6ce10b2020-06-04 15:33:47 +020035#include <haproxy/sample.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020036#include <haproxy/stream_interface.h>
Willy Tarreau48fbcae2020-06-03 18:09:46 +020037#include <haproxy/tools.h>
Willy Tarreau8c42b8a2020-06-04 19:27:34 +020038#include <haproxy/uri_auth-t.h>
Tim Duesterhusd2bedcc2021-04-15 21:45:57 +020039#include <haproxy/uri_normalizer.h>
Willy Tarreaud6788052020-05-27 15:59:00 +020040#include <haproxy/version.h>
Willy Tarreau79e57332018-10-02 16:01:16 +020041
Willy Tarreau79e57332018-10-02 16:01:16 +020042
Christopher Faulet2eb53962020-01-14 14:47:34 +010043/* Release memory allocated by most of HTTP actions. Concretly, it releases
44 * <arg.http>.
45 */
46static void release_http_action(struct act_rule *rule)
47{
48 struct logformat_node *lf, *lfb;
49
Tim Duesterhused526372020-03-05 17:56:33 +010050 istfree(&rule->arg.http.str);
Christopher Faulet2eb53962020-01-14 14:47:34 +010051 if (rule->arg.http.re)
52 regex_free(rule->arg.http.re);
53 list_for_each_entry_safe(lf, lfb, &rule->arg.http.fmt, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +020054 LIST_DELETE(&lf->list);
Christopher Faulet2eb53962020-01-14 14:47:34 +010055 release_sample_expr(lf->expr);
56 free(lf->arg);
57 free(lf);
58 }
59}
60
Christopher Faulet5cb513a2020-05-13 17:56:56 +020061/* Release memory allocated by HTTP actions relying on an http reply. Concretly,
62 * it releases <.arg.http_reply>
63 */
64static void release_act_http_reply(struct act_rule *rule)
65{
66 release_http_reply(rule->arg.http_reply);
67 rule->arg.http_reply = NULL;
68}
69
70
71/* Check function for HTTP actions relying on an http reply. The function
72 * returns 1 in success case, otherwise, it returns 0 and err is filled.
73 */
74static int check_act_http_reply(struct act_rule *rule, struct proxy *px, char **err)
75{
76 struct http_reply *reply = rule->arg.http_reply;
77
78 if (!http_check_http_reply(reply, px, err)) {
79 release_act_http_reply(rule);
80 return 0;
81 }
82 return 1;
83}
84
Willy Tarreau79e57332018-10-02 16:01:16 +020085
86/* This function executes one of the set-{method,path,query,uri} actions. It
87 * builds a string in the trash from the specified format string. It finds
Christopher Faulet2c22a692019-12-18 15:39:56 +010088 * the action to be performed in <.action>, previously filled by function
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +050089 * parse_set_req_line(). The replacement action is executed by the function
Christopher Faulete00d06c2019-12-16 17:18:42 +010090 * http_action_set_req_line(). On success, it returns ACT_RET_CONT. If an error
91 * occurs while soft rewrites are enabled, the action is canceled, but the rule
92 * processing continue. Otherwsize ACT_RET_ERR is returned.
Willy Tarreau79e57332018-10-02 16:01:16 +020093 */
94static enum act_return http_action_set_req_line(struct act_rule *rule, struct proxy *px,
95 struct session *sess, struct stream *s, int flags)
96{
97 struct buffer *replace;
Christopher Faulet13403762019-12-13 09:01:57 +010098 enum act_return ret = ACT_RET_CONT;
Willy Tarreau79e57332018-10-02 16:01:16 +020099
100 replace = alloc_trash_chunk();
101 if (!replace)
Christopher Faulete00d06c2019-12-16 17:18:42 +0100102 goto fail_alloc;
Willy Tarreau79e57332018-10-02 16:01:16 +0200103
104 /* If we have to create a query string, prepare a '?'. */
Christopher Faulet2c22a692019-12-18 15:39:56 +0100105 if (rule->action == 2) // set-query
Willy Tarreau79e57332018-10-02 16:01:16 +0200106 replace->area[replace->data++] = '?';
107 replace->data += build_logline(s, replace->area + replace->data,
108 replace->size - replace->data,
Christopher Faulet96bff762019-12-17 13:46:18 +0100109 &rule->arg.http.fmt);
Willy Tarreau79e57332018-10-02 16:01:16 +0200110
Christopher Faulet2c22a692019-12-18 15:39:56 +0100111 if (http_req_replace_stline(rule->action, replace->area, replace->data, px, s) == -1)
Christopher Faulete00d06c2019-12-16 17:18:42 +0100112 goto fail_rewrite;
Willy Tarreau79e57332018-10-02 16:01:16 +0200113
Christopher Faulete00d06c2019-12-16 17:18:42 +0100114 leave:
Willy Tarreau79e57332018-10-02 16:01:16 +0200115 free_trash_chunk(replace);
116 return ret;
Christopher Faulete00d06c2019-12-16 17:18:42 +0100117
118 fail_alloc:
119 if (!(s->flags & SF_ERR_MASK))
120 s->flags |= SF_ERR_RESOURCE;
121 ret = ACT_RET_ERR;
122 goto leave;
123
124 fail_rewrite:
Willy Tarreau4781b152021-04-06 13:53:36 +0200125 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_rewrites);
Christopher Faulete00d06c2019-12-16 17:18:42 +0100126 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200127 _HA_ATOMIC_INC(&s->be->be_counters.failed_rewrites);
William Lallemand36119de2021-03-08 15:26:48 +0100128 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200129 _HA_ATOMIC_INC(&sess->listener->counters->failed_rewrites);
Christopher Faulete00d06c2019-12-16 17:18:42 +0100130 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +0200131 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_rewrites);
Christopher Faulete00d06c2019-12-16 17:18:42 +0100132
Christopher Faulet333bf8c2020-01-22 14:38:05 +0100133 if (!(s->txn->req.flags & HTTP_MSGF_SOFT_RW)) {
Christopher Faulete00d06c2019-12-16 17:18:42 +0100134 ret = ACT_RET_ERR;
Christopher Faulet333bf8c2020-01-22 14:38:05 +0100135 if (!(s->flags & SF_ERR_MASK))
136 s->flags |= SF_ERR_PRXCOND;
137 }
Christopher Faulete00d06c2019-12-16 17:18:42 +0100138 goto leave;
Willy Tarreau79e57332018-10-02 16:01:16 +0200139}
140
141/* parse an http-request action among :
142 * set-method
143 * set-path
Christopher Faulet312294f2020-09-02 17:17:44 +0200144 * set-pathq
Willy Tarreau79e57332018-10-02 16:01:16 +0200145 * set-query
146 * set-uri
147 *
148 * All of them accept a single argument of type string representing a log-format.
Christopher Faulet96bff762019-12-17 13:46:18 +0100149 * The resulting rule makes use of <http.fmt> to store the log-format list head,
Christopher Faulet2c22a692019-12-18 15:39:56 +0100150 * and <.action> to store the action type as an int (0=method, 1=path, 2=query,
151 * 3=uri). It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Willy Tarreau79e57332018-10-02 16:01:16 +0200152 */
153static enum act_parse_ret parse_set_req_line(const char **args, int *orig_arg, struct proxy *px,
154 struct act_rule *rule, char **err)
155{
156 int cur_arg = *orig_arg;
157
Willy Tarreau79e57332018-10-02 16:01:16 +0200158 switch (args[0][4]) {
159 case 'm' :
Christopher Faulet2c22a692019-12-18 15:39:56 +0100160 rule->action = 0; // set-method
Willy Tarreau79e57332018-10-02 16:01:16 +0200161 break;
162 case 'p' :
Christopher Faulet312294f2020-09-02 17:17:44 +0200163 if (args[0][8] == 'q')
164 rule->action = 4; // set-pathq
165 else
166 rule->action = 1; // set-path
Willy Tarreau79e57332018-10-02 16:01:16 +0200167 break;
168 case 'q' :
Christopher Faulet2c22a692019-12-18 15:39:56 +0100169 rule->action = 2; // set-query
Willy Tarreau79e57332018-10-02 16:01:16 +0200170 break;
171 case 'u' :
Christopher Faulet2c22a692019-12-18 15:39:56 +0100172 rule->action = 3; // set-uri
Willy Tarreau79e57332018-10-02 16:01:16 +0200173 break;
174 default:
175 memprintf(err, "internal error: unhandled action '%s'", args[0]);
176 return ACT_RET_PRS_ERR;
177 }
Christopher Faulet96bff762019-12-17 13:46:18 +0100178 rule->action_ptr = http_action_set_req_line;
Christopher Faulet2eb53962020-01-14 14:47:34 +0100179 rule->release_ptr = release_http_action;
Willy Tarreau79e57332018-10-02 16:01:16 +0200180
181 if (!*args[cur_arg] ||
182 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
183 memprintf(err, "expects exactly 1 argument <format>");
184 return ACT_RET_PRS_ERR;
185 }
186
Christopher Faulet96bff762019-12-17 13:46:18 +0100187 LIST_INIT(&rule->arg.http.fmt);
Willy Tarreau79e57332018-10-02 16:01:16 +0200188 px->conf.args.ctx = ARGC_HRQ;
Christopher Faulet96bff762019-12-17 13:46:18 +0100189 if (!parse_logformat_string(args[cur_arg], px, &rule->arg.http.fmt, LOG_OPT_HTTP,
Willy Tarreau79e57332018-10-02 16:01:16 +0200190 (px->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, err)) {
191 return ACT_RET_PRS_ERR;
192 }
193
194 (*orig_arg)++;
195 return ACT_RET_PRS_OK;
196}
197
Tim Duesterhusd2bedcc2021-04-15 21:45:57 +0200198/* This function executes the http-request normalize-uri action.
199 * `rule->action` is expected to be a value from `enum act_normalize_uri`.
200 *
201 * On success, it returns ACT_RET_CONT. If an error
202 * occurs while soft rewrites are enabled, the action is canceled, but the rule
203 * processing continue. Otherwsize ACT_RET_ERR is returned.
204 */
205static enum act_return http_action_normalize_uri(struct act_rule *rule, struct proxy *px,
206 struct session *sess, struct stream *s, int flags)
207{
208 enum act_return ret = ACT_RET_CONT;
209 struct htx *htx = htxbuf(&s->req.buf);
210 const struct ist uri = htx_sl_req_uri(http_get_stline(htx));
211 struct buffer *replace = alloc_trash_chunk();
212 enum uri_normalizer_err err = URI_NORMALIZER_ERR_INTERNAL_ERROR;
213
214 if (!replace)
215 goto fail_alloc;
216
217 switch ((enum act_normalize_uri) rule->action) {
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200218 case ACT_NORMALIZE_URI_PATH_MERGE_SLASHES: {
Tim Duesterhusd371e992021-04-15 21:45:58 +0200219 const struct ist path = http_get_path(uri);
220 struct ist newpath = ist2(replace->area, replace->size);
221
222 if (!isttest(path))
223 goto leave;
224
225 err = uri_normalizer_path_merge_slashes(iststop(path, '?'), &newpath);
226
227 if (err != URI_NORMALIZER_ERR_NONE)
228 break;
229
230 if (!http_replace_req_path(htx, newpath, 0))
231 goto fail_rewrite;
232
233 break;
234 }
Maximilian Maderff3bb8b2021-04-21 00:22:50 +0200235 case ACT_NORMALIZE_URI_PATH_STRIP_DOT: {
236 const struct ist path = http_get_path(uri);
237 struct ist newpath = ist2(replace->area, replace->size);
238
239 if (!isttest(path))
240 goto leave;
241
242 err = uri_normalizer_path_dot(iststop(path, '?'), &newpath);
243
244 if (err != URI_NORMALIZER_ERR_NONE)
245 break;
246
247 if (!http_replace_req_path(htx, newpath, 0))
248 goto fail_rewrite;
249
250 break;
251 }
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200252 case ACT_NORMALIZE_URI_PATH_STRIP_DOTDOT:
253 case ACT_NORMALIZE_URI_PATH_STRIP_DOTDOT_FULL: {
Tim Duesterhus9982fc22021-04-15 21:45:59 +0200254 const struct ist path = http_get_path(uri);
255 struct ist newpath = ist2(replace->area, replace->size);
256
257 if (!isttest(path))
258 goto leave;
259
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200260 err = uri_normalizer_path_dotdot(iststop(path, '?'), rule->action == ACT_NORMALIZE_URI_PATH_STRIP_DOTDOT_FULL, &newpath);
Tim Duesterhus9982fc22021-04-15 21:45:59 +0200261
262 if (err != URI_NORMALIZER_ERR_NONE)
263 break;
264
265 if (!http_replace_req_path(htx, newpath, 0))
266 goto fail_rewrite;
267
268 break;
269 }
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200270 case ACT_NORMALIZE_URI_QUERY_SORT_BY_NAME: {
Tim Duesterhusd7b89be2021-04-15 21:46:01 +0200271 const struct ist path = http_get_path(uri);
272 struct ist newquery = ist2(replace->area, replace->size);
273
274 if (!isttest(path))
275 goto leave;
276
277 err = uri_normalizer_query_sort(istfind(path, '?'), '&', &newquery);
278
279 if (err != URI_NORMALIZER_ERR_NONE)
280 break;
281
282 if (!http_replace_req_query(htx, newquery))
283 goto fail_rewrite;
284
285 break;
286 }
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200287 case ACT_NORMALIZE_URI_PERCENT_TO_UPPERCASE:
288 case ACT_NORMALIZE_URI_PERCENT_TO_UPPERCASE_STRICT: {
Tim Duesterhusa4071932021-04-15 21:46:02 +0200289 const struct ist path = http_get_path(uri);
290 struct ist newpath = ist2(replace->area, replace->size);
291
292 if (!isttest(path))
293 goto leave;
294
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200295 err = uri_normalizer_percent_upper(path, rule->action == ACT_NORMALIZE_URI_PERCENT_TO_UPPERCASE_STRICT, &newpath);
Tim Duesterhusa4071932021-04-15 21:46:02 +0200296
297 if (err != URI_NORMALIZER_ERR_NONE)
298 break;
299
300 if (!http_replace_req_path(htx, newpath, 1))
301 goto fail_rewrite;
302
303 break;
304 }
Tim Duesterhus2e4a18e2021-04-21 21:20:36 +0200305 case ACT_NORMALIZE_URI_PERCENT_DECODE_UNRESERVED:
306 case ACT_NORMALIZE_URI_PERCENT_DECODE_UNRESERVED_STRICT: {
307 const struct ist path = http_get_path(uri);
308 struct ist newpath = ist2(replace->area, replace->size);
309
310 if (!isttest(path))
311 goto leave;
312
313 err = uri_normalizer_percent_decode_unreserved(path, rule->action == ACT_NORMALIZE_URI_PERCENT_DECODE_UNRESERVED_STRICT, &newpath);
314
315 if (err != URI_NORMALIZER_ERR_NONE)
316 break;
317
318 if (!http_replace_req_path(htx, newpath, 1))
319 goto fail_rewrite;
320
321 break;
322 }
Tim Duesterhusc9e05ab2021-05-10 17:28:25 +0200323 case ACT_NORMALIZE_URI_FRAGMENT_STRIP: {
324 const struct ist path = http_get_path(uri);
325 struct ist newpath = ist2(replace->area, replace->size);
326
327 if (!isttest(path))
328 goto leave;
329
330 err = uri_normalizer_fragment_strip(path, &newpath);
331
332 if (err != URI_NORMALIZER_ERR_NONE)
333 break;
334
335 if (!http_replace_req_path(htx, newpath, 1))
336 goto fail_rewrite;
337
338 break;
339 }
Tim Duesterhusdec1c362021-05-10 17:28:26 +0200340 case ACT_NORMALIZE_URI_FRAGMENT_ENCODE: {
341 const struct ist path = http_get_path(uri);
342 struct ist newpath = ist2(replace->area, replace->size);
343
344 if (!isttest(path))
345 goto leave;
346
347 err = uri_normalizer_fragment_encode(path, &newpath);
348
349 if (err != URI_NORMALIZER_ERR_NONE)
350 break;
351
352 if (!http_replace_req_path(htx, newpath, 1))
353 goto fail_rewrite;
354
355 break;
356 }
Tim Duesterhusd2bedcc2021-04-15 21:45:57 +0200357 }
358
359 switch (err) {
360 case URI_NORMALIZER_ERR_NONE:
361 break;
362 case URI_NORMALIZER_ERR_INTERNAL_ERROR:
363 ret = ACT_RET_ERR;
364 break;
365 case URI_NORMALIZER_ERR_INVALID_INPUT:
366 ret = ACT_RET_INV;
367 break;
368 case URI_NORMALIZER_ERR_ALLOC:
369 goto fail_alloc;
370 }
371
372 leave:
373 free_trash_chunk(replace);
374 return ret;
375
376 fail_alloc:
377 if (!(s->flags & SF_ERR_MASK))
378 s->flags |= SF_ERR_RESOURCE;
379 ret = ACT_RET_ERR;
380 goto leave;
381
382 fail_rewrite:
383 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_rewrites, 1);
384 if (s->flags & SF_BE_ASSIGNED)
385 _HA_ATOMIC_ADD(&s->be->be_counters.failed_rewrites, 1);
386 if (sess->listener && sess->listener->counters)
387 _HA_ATOMIC_ADD(&sess->listener->counters->failed_rewrites, 1);
388 if (objt_server(s->target))
389 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_rewrites, 1);
390
391 if (!(s->txn->req.flags & HTTP_MSGF_SOFT_RW)) {
392 ret = ACT_RET_ERR;
393 if (!(s->flags & SF_ERR_MASK))
394 s->flags |= SF_ERR_PRXCOND;
395 }
396 goto leave;
397}
398
399/* Parses the http-request normalize-uri action. It expects a single <normalizer>
400 * argument, corresponding too a value in `enum act_normalize_uri`.
401 *
402 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
403 */
404static enum act_parse_ret parse_http_normalize_uri(const char **args, int *orig_arg, struct proxy *px,
405 struct act_rule *rule, char **err)
406{
407 int cur_arg = *orig_arg;
408
409 rule->action_ptr = http_action_normalize_uri;
410 rule->release_ptr = NULL;
411
412 if (!*args[cur_arg]) {
413 memprintf(err, "missing argument <normalizer>");
414 return ACT_RET_PRS_ERR;
415 }
416
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200417 if (strcmp(args[cur_arg], "path-merge-slashes") == 0) {
Tim Duesterhusd371e992021-04-15 21:45:58 +0200418 cur_arg++;
Tim Duesterhusd2bedcc2021-04-15 21:45:57 +0200419
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200420 rule->action = ACT_NORMALIZE_URI_PATH_MERGE_SLASHES;
Tim Duesterhusd2bedcc2021-04-15 21:45:57 +0200421 }
Maximilian Maderff3bb8b2021-04-21 00:22:50 +0200422 else if (strcmp(args[cur_arg], "path-strip-dot") == 0) {
423 cur_arg++;
424
425 rule->action = ACT_NORMALIZE_URI_PATH_STRIP_DOT;
426 }
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200427 else if (strcmp(args[cur_arg], "path-strip-dotdot") == 0) {
Tim Duesterhus9982fc22021-04-15 21:45:59 +0200428 cur_arg++;
429
Tim Duesterhus560e1a62021-04-15 21:46:00 +0200430 if (strcmp(args[cur_arg], "full") == 0) {
431 cur_arg++;
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200432 rule->action = ACT_NORMALIZE_URI_PATH_STRIP_DOTDOT_FULL;
Tim Duesterhus560e1a62021-04-15 21:46:00 +0200433 }
434 else if (!*args[cur_arg]) {
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200435 rule->action = ACT_NORMALIZE_URI_PATH_STRIP_DOTDOT;
Tim Duesterhus560e1a62021-04-15 21:46:00 +0200436 }
437 else if (strcmp(args[cur_arg], "if") != 0 && strcmp(args[cur_arg], "unless") != 0) {
Tim Duesterhus2f413132021-05-10 23:21:20 +0200438 memprintf(err, "unknown argument '%s' for 'path-strip-dotdot' normalizer", args[cur_arg]);
Tim Duesterhus560e1a62021-04-15 21:46:00 +0200439 return ACT_RET_PRS_ERR;
440 }
Tim Duesterhus9982fc22021-04-15 21:45:59 +0200441 }
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200442 else if (strcmp(args[cur_arg], "query-sort-by-name") == 0) {
Tim Duesterhusd7b89be2021-04-15 21:46:01 +0200443 cur_arg++;
444
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200445 rule->action = ACT_NORMALIZE_URI_QUERY_SORT_BY_NAME;
Tim Duesterhusd7b89be2021-04-15 21:46:01 +0200446 }
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200447 else if (strcmp(args[cur_arg], "percent-to-uppercase") == 0) {
Tim Duesterhusa4071932021-04-15 21:46:02 +0200448 cur_arg++;
449
450 if (strcmp(args[cur_arg], "strict") == 0) {
451 cur_arg++;
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200452 rule->action = ACT_NORMALIZE_URI_PERCENT_TO_UPPERCASE_STRICT;
Tim Duesterhusa4071932021-04-15 21:46:02 +0200453 }
454 else if (!*args[cur_arg]) {
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200455 rule->action = ACT_NORMALIZE_URI_PERCENT_TO_UPPERCASE;
Tim Duesterhusa4071932021-04-15 21:46:02 +0200456 }
457 else if (strcmp(args[cur_arg], "if") != 0 && strcmp(args[cur_arg], "unless") != 0) {
Tim Duesterhus2f413132021-05-10 23:21:20 +0200458 memprintf(err, "unknown argument '%s' for 'percent-to-uppercase' normalizer", args[cur_arg]);
Tim Duesterhusa4071932021-04-15 21:46:02 +0200459 return ACT_RET_PRS_ERR;
460 }
461 }
Tim Duesterhus2e4a18e2021-04-21 21:20:36 +0200462 else if (strcmp(args[cur_arg], "percent-decode-unreserved") == 0) {
463 cur_arg++;
464
465 if (strcmp(args[cur_arg], "strict") == 0) {
466 cur_arg++;
467 rule->action = ACT_NORMALIZE_URI_PERCENT_DECODE_UNRESERVED_STRICT;
468 }
469 else if (!*args[cur_arg]) {
470 rule->action = ACT_NORMALIZE_URI_PERCENT_DECODE_UNRESERVED;
471 }
472 else if (strcmp(args[cur_arg], "if") != 0 && strcmp(args[cur_arg], "unless") != 0) {
473 memprintf(err, "unknown argument '%s' for 'percent-decode-unreserved' normalizer", args[cur_arg]);
474 return ACT_RET_PRS_ERR;
475 }
476 }
Tim Duesterhusc9e05ab2021-05-10 17:28:25 +0200477 else if (strcmp(args[cur_arg], "fragment-strip") == 0) {
478 cur_arg++;
479
480 rule->action = ACT_NORMALIZE_URI_FRAGMENT_STRIP;
481 }
Tim Duesterhusdec1c362021-05-10 17:28:26 +0200482 else if (strcmp(args[cur_arg], "fragment-encode") == 0) {
483 cur_arg++;
484
485 rule->action = ACT_NORMALIZE_URI_FRAGMENT_ENCODE;
486 }
Tim Duesterhusd2bedcc2021-04-15 21:45:57 +0200487 else {
488 memprintf(err, "unknown normalizer '%s'", args[cur_arg]);
489 return ACT_RET_PRS_ERR;
490 }
491
492 *orig_arg = cur_arg;
493 return ACT_RET_PRS_OK;
494}
495
Willy Tarreau33810222019-06-12 17:44:02 +0200496/* This function executes a replace-uri action. It finds its arguments in
Christopher Faulet96bff762019-12-17 13:46:18 +0100497 * <rule>.arg.http. It builds a string in the trash from the format string
Willy Tarreau33810222019-06-12 17:44:02 +0200498 * previously filled by function parse_replace_uri() and will execute the regex
Christopher Faulet96bff762019-12-17 13:46:18 +0100499 * in <http.re> to replace the URI. It uses the format string present in
Christopher Faulet2c22a692019-12-18 15:39:56 +0100500 * <http.fmt>. The component to act on (path/uri) is taken from <.action> which
Christopher Faulet96bff762019-12-17 13:46:18 +0100501 * contains 1 for the path or 3 for the URI (values used by
502 * http_req_replace_stline()). On success, it returns ACT_RET_CONT. If an error
503 * occurs while soft rewrites are enabled, the action is canceled, but the rule
504 * processing continue. Otherwsize ACT_RET_ERR is returned.
Willy Tarreau33810222019-06-12 17:44:02 +0200505 */
506static enum act_return http_action_replace_uri(struct act_rule *rule, struct proxy *px,
507 struct session *sess, struct stream *s, int flags)
508{
Christopher Faulet13403762019-12-13 09:01:57 +0100509 enum act_return ret = ACT_RET_CONT;
Willy Tarreau33810222019-06-12 17:44:02 +0200510 struct buffer *replace, *output;
511 struct ist uri;
512 int len;
513
514 replace = alloc_trash_chunk();
515 output = alloc_trash_chunk();
516 if (!replace || !output)
Christopher Faulete00d06c2019-12-16 17:18:42 +0100517 goto fail_alloc;
Christopher Faulet12c28b62019-07-15 16:30:24 +0200518 uri = htx_sl_req_uri(http_get_stline(htxbuf(&s->req.buf)));
Willy Tarreau262c3f12019-12-17 06:52:51 +0100519
Christopher Faulet1fa0cc12020-09-02 11:10:38 +0200520 if (rule->action == 1) // replace-path
521 uri = iststop(http_get_path(uri), '?');
Christopher Faulet312294f2020-09-02 17:17:44 +0200522 else if (rule->action == 4) // replace-pathq
523 uri = http_get_path(uri);
Willy Tarreau262c3f12019-12-17 06:52:51 +0100524
Christopher Faulet96bff762019-12-17 13:46:18 +0100525 if (!regex_exec_match2(rule->arg.http.re, uri.ptr, uri.len, MAX_MATCH, pmatch, 0))
Willy Tarreau33810222019-06-12 17:44:02 +0200526 goto leave;
527
Christopher Faulet96bff762019-12-17 13:46:18 +0100528 replace->data = build_logline(s, replace->area, replace->size, &rule->arg.http.fmt);
Willy Tarreau33810222019-06-12 17:44:02 +0200529
530 /* note: uri.ptr doesn't need to be zero-terminated because it will
531 * only be used to pick pmatch references.
532 */
533 len = exp_replace(output->area, output->size, uri.ptr, replace->area, pmatch);
534 if (len == -1)
Christopher Faulete00d06c2019-12-16 17:18:42 +0100535 goto fail_rewrite;
Willy Tarreau33810222019-06-12 17:44:02 +0200536
Christopher Faulet2c22a692019-12-18 15:39:56 +0100537 if (http_req_replace_stline(rule->action, output->area, len, px, s) == -1)
Christopher Faulete00d06c2019-12-16 17:18:42 +0100538 goto fail_rewrite;
Willy Tarreau33810222019-06-12 17:44:02 +0200539
Christopher Faulete00d06c2019-12-16 17:18:42 +0100540 leave:
Willy Tarreau33810222019-06-12 17:44:02 +0200541 free_trash_chunk(output);
542 free_trash_chunk(replace);
543 return ret;
Christopher Faulete00d06c2019-12-16 17:18:42 +0100544
545 fail_alloc:
546 if (!(s->flags & SF_ERR_MASK))
547 s->flags |= SF_ERR_RESOURCE;
548 ret = ACT_RET_ERR;
549 goto leave;
550
551 fail_rewrite:
Willy Tarreau4781b152021-04-06 13:53:36 +0200552 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_rewrites);
Christopher Faulete00d06c2019-12-16 17:18:42 +0100553 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200554 _HA_ATOMIC_INC(&s->be->be_counters.failed_rewrites);
William Lallemand36119de2021-03-08 15:26:48 +0100555 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200556 _HA_ATOMIC_INC(&sess->listener->counters->failed_rewrites);
Christopher Faulete00d06c2019-12-16 17:18:42 +0100557 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +0200558 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_rewrites);
Christopher Faulete00d06c2019-12-16 17:18:42 +0100559
Christopher Faulet333bf8c2020-01-22 14:38:05 +0100560 if (!(s->txn->req.flags & HTTP_MSGF_SOFT_RW)) {
Christopher Faulete00d06c2019-12-16 17:18:42 +0100561 ret = ACT_RET_ERR;
Christopher Faulet333bf8c2020-01-22 14:38:05 +0100562 if (!(s->flags & SF_ERR_MASK))
563 s->flags |= SF_ERR_PRXCOND;
564 }
Christopher Faulete00d06c2019-12-16 17:18:42 +0100565 goto leave;
Willy Tarreau33810222019-06-12 17:44:02 +0200566}
567
Christopher Faulet312294f2020-09-02 17:17:44 +0200568/* parse a "replace-uri", "replace-path" or "replace-pathq"
569 * http-request action.
Willy Tarreau33810222019-06-12 17:44:02 +0200570 * This action takes 2 arguments (a regex and a replacement format string).
Christopher Faulet2c22a692019-12-18 15:39:56 +0100571 * The resulting rule makes use of <.action> to store the action (1/3 for now),
Christopher Faulet96bff762019-12-17 13:46:18 +0100572 * <http.re> to store the compiled regex, and <http.fmt> to store the log-format
Willy Tarreau33810222019-06-12 17:44:02 +0200573 * list head. It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
574 */
575static enum act_parse_ret parse_replace_uri(const char **args, int *orig_arg, struct proxy *px,
576 struct act_rule *rule, char **err)
577{
578 int cur_arg = *orig_arg;
579 char *error = NULL;
580
Christopher Faulet312294f2020-09-02 17:17:44 +0200581 switch (args[0][8]) {
582 case 'p':
583 if (args[0][12] == 'q')
584 rule->action = 4; // replace-pathq, same as set-pathq
585 else
586 rule->action = 1; // replace-path, same as set-path
587 break;
588 case 'u':
Christopher Faulet2c22a692019-12-18 15:39:56 +0100589 rule->action = 3; // replace-uri, same as set-uri
Christopher Faulet312294f2020-09-02 17:17:44 +0200590 break;
591 default:
592 memprintf(err, "internal error: unhandled action '%s'", args[0]);
593 return ACT_RET_PRS_ERR;
594 }
Willy Tarreau262c3f12019-12-17 06:52:51 +0100595
Willy Tarreau33810222019-06-12 17:44:02 +0200596 rule->action_ptr = http_action_replace_uri;
Christopher Faulet2eb53962020-01-14 14:47:34 +0100597 rule->release_ptr = release_http_action;
Willy Tarreau33810222019-06-12 17:44:02 +0200598
599 if (!*args[cur_arg] || !*args[cur_arg+1] ||
600 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
601 memprintf(err, "expects exactly 2 arguments <match-regex> and <replace-format>");
602 return ACT_RET_PRS_ERR;
603 }
604
Christopher Faulet96bff762019-12-17 13:46:18 +0100605 if (!(rule->arg.http.re = regex_comp(args[cur_arg], 1, 1, &error))) {
Willy Tarreau33810222019-06-12 17:44:02 +0200606 memprintf(err, "failed to parse the regex : %s", error);
607 free(error);
608 return ACT_RET_PRS_ERR;
609 }
610
Christopher Faulet96bff762019-12-17 13:46:18 +0100611 LIST_INIT(&rule->arg.http.fmt);
Willy Tarreau33810222019-06-12 17:44:02 +0200612 px->conf.args.ctx = ARGC_HRQ;
Christopher Faulet96bff762019-12-17 13:46:18 +0100613 if (!parse_logformat_string(args[cur_arg + 1], px, &rule->arg.http.fmt, LOG_OPT_HTTP,
Willy Tarreau33810222019-06-12 17:44:02 +0200614 (px->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, err)) {
Christopher Faulet1337b322020-01-14 14:50:55 +0100615 regex_free(rule->arg.http.re);
Willy Tarreau33810222019-06-12 17:44:02 +0200616 return ACT_RET_PRS_ERR;
617 }
618
619 (*orig_arg) += 2;
620 return ACT_RET_PRS_OK;
621}
622
Willy Tarreau79e57332018-10-02 16:01:16 +0200623/* This function is just a compliant action wrapper for "set-status". */
624static enum act_return action_http_set_status(struct act_rule *rule, struct proxy *px,
625 struct session *sess, struct stream *s, int flags)
626{
Christopher Faulet96bff762019-12-17 13:46:18 +0100627 if (http_res_set_status(rule->arg.http.i, rule->arg.http.str, s) == -1) {
Willy Tarreau4781b152021-04-06 13:53:36 +0200628 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_rewrites);
Christopher Faulete00d06c2019-12-16 17:18:42 +0100629 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200630 _HA_ATOMIC_INC(&s->be->be_counters.failed_rewrites);
William Lallemand36119de2021-03-08 15:26:48 +0100631 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200632 _HA_ATOMIC_INC(&sess->listener->counters->failed_rewrites);
Christopher Faulete00d06c2019-12-16 17:18:42 +0100633 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +0200634 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_rewrites);
Christopher Faulete00d06c2019-12-16 17:18:42 +0100635
Christopher Faulet333bf8c2020-01-22 14:38:05 +0100636 if (!(s->txn->req.flags & HTTP_MSGF_SOFT_RW)) {
Christopher Faulet333bf8c2020-01-22 14:38:05 +0100637 if (!(s->flags & SF_ERR_MASK))
638 s->flags |= SF_ERR_PRXCOND;
Christopher Faulet692a6c22020-02-07 10:22:31 +0100639 return ACT_RET_ERR;
Christopher Faulet333bf8c2020-01-22 14:38:05 +0100640 }
Christopher Faulete00d06c2019-12-16 17:18:42 +0100641 }
642
Willy Tarreau79e57332018-10-02 16:01:16 +0200643 return ACT_RET_CONT;
644}
645
646/* parse set-status action:
647 * This action accepts a single argument of type int representing
648 * an http status code. It returns ACT_RET_PRS_OK on success,
649 * ACT_RET_PRS_ERR on error.
650 */
651static enum act_parse_ret parse_http_set_status(const char **args, int *orig_arg, struct proxy *px,
652 struct act_rule *rule, char **err)
653{
654 char *error;
655
656 rule->action = ACT_CUSTOM;
657 rule->action_ptr = action_http_set_status;
Christopher Faulet2eb53962020-01-14 14:47:34 +0100658 rule->release_ptr = release_http_action;
Willy Tarreau79e57332018-10-02 16:01:16 +0200659
660 /* Check if an argument is available */
661 if (!*args[*orig_arg]) {
662 memprintf(err, "expects 1 argument: <status>; or 3 arguments: <status> reason <fmt>");
663 return ACT_RET_PRS_ERR;
664 }
665
666 /* convert status code as integer */
Christopher Faulet96bff762019-12-17 13:46:18 +0100667 rule->arg.http.i = strtol(args[*orig_arg], &error, 10);
668 if (*error != '\0' || rule->arg.http.i < 100 || rule->arg.http.i > 999) {
Willy Tarreau79e57332018-10-02 16:01:16 +0200669 memprintf(err, "expects an integer status code between 100 and 999");
670 return ACT_RET_PRS_ERR;
671 }
672
673 (*orig_arg)++;
674
675 /* set custom reason string */
Christopher Faulet96bff762019-12-17 13:46:18 +0100676 rule->arg.http.str = ist(NULL); // If null, we use the default reason for the status code.
Willy Tarreau79e57332018-10-02 16:01:16 +0200677 if (*args[*orig_arg] && strcmp(args[*orig_arg], "reason") == 0 &&
678 (*args[*orig_arg + 1] && strcmp(args[*orig_arg + 1], "if") != 0 && strcmp(args[*orig_arg + 1], "unless") != 0)) {
679 (*orig_arg)++;
Christopher Faulet96bff762019-12-17 13:46:18 +0100680 rule->arg.http.str.ptr = strdup(args[*orig_arg]);
681 rule->arg.http.str.len = strlen(rule->arg.http.str.ptr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200682 (*orig_arg)++;
683 }
684
Christopher Fauletc20b3712020-01-27 15:51:56 +0100685 LIST_INIT(&rule->arg.http.fmt);
Willy Tarreau79e57332018-10-02 16:01:16 +0200686 return ACT_RET_PRS_OK;
687}
688
689/* This function executes the "reject" HTTP action. It clears the request and
690 * response buffer without sending any response. It can be useful as an HTTP
691 * alternative to the silent-drop action to defend against DoS attacks, and may
692 * also be used with HTTP/2 to close a connection instead of just a stream.
693 * The txn status is unchanged, indicating no response was sent. The termination
Christopher Faulet90d22a82020-03-06 11:18:39 +0100694 * flags will indicate "PR". It always returns ACT_RET_ABRT.
Willy Tarreau79e57332018-10-02 16:01:16 +0200695 */
696static enum act_return http_action_reject(struct act_rule *rule, struct proxy *px,
697 struct session *sess, struct stream *s, int flags)
698{
Willy Tarreau0f9cd7b2019-01-31 19:02:43 +0100699 si_must_kill_conn(chn_prod(&s->req));
Willy Tarreau79e57332018-10-02 16:01:16 +0200700 channel_abort(&s->req);
701 channel_abort(&s->res);
Christopher Fauletd4a824e2020-03-06 15:07:09 +0100702 s->req.analysers &= AN_REQ_FLT_END;
703 s->res.analysers &= AN_RES_FLT_END;
Willy Tarreau79e57332018-10-02 16:01:16 +0200704
Willy Tarreau4781b152021-04-06 13:53:36 +0200705 _HA_ATOMIC_INC(&s->be->be_counters.denied_req);
706 _HA_ATOMIC_INC(&sess->fe->fe_counters.denied_req);
Willy Tarreau79e57332018-10-02 16:01:16 +0200707 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200708 _HA_ATOMIC_INC(&sess->listener->counters->denied_req);
Willy Tarreau79e57332018-10-02 16:01:16 +0200709
710 if (!(s->flags & SF_ERR_MASK))
711 s->flags |= SF_ERR_PRXCOND;
712 if (!(s->flags & SF_FINST_MASK))
713 s->flags |= SF_FINST_R;
714
Christopher Faulet90d22a82020-03-06 11:18:39 +0100715 return ACT_RET_ABRT;
Willy Tarreau79e57332018-10-02 16:01:16 +0200716}
717
718/* parse the "reject" action:
719 * This action takes no argument and returns ACT_RET_PRS_OK on success,
720 * ACT_RET_PRS_ERR on error.
721 */
722static enum act_parse_ret parse_http_action_reject(const char **args, int *orig_arg, struct proxy *px,
723 struct act_rule *rule, char **err)
724{
725 rule->action = ACT_CUSTOM;
726 rule->action_ptr = http_action_reject;
727 return ACT_RET_PRS_OK;
728}
729
Olivier Houchard602bf7d2019-05-10 13:59:15 +0200730/* This function executes the "disable-l7-retry" HTTP action.
731 * It disables L7 retries (all retry except for a connection failure). This
732 * can be useful for example to avoid retrying on POST requests.
733 * It just removes the L7 retry flag on the stream_interface, and always
734 * return ACT_RET_CONT;
735 */
736static enum act_return http_req_disable_l7_retry(struct act_rule *rule, struct proxy *px,
737 struct session *sess, struct stream *s, int flags)
738{
739 struct stream_interface *si = &s->si[1];
740
741 /* In theory, the SI_FL_L7_RETRY flags isn't set at this point, but
742 * let's be future-proof and remove it anyway.
743 */
744 si->flags &= ~SI_FL_L7_RETRY;
745 si->flags |= SI_FL_D_L7_RETRY;
746 return ACT_RET_CONT;
747}
748
749/* parse the "disable-l7-retry" action:
750 * This action takes no argument and returns ACT_RET_PRS_OK on success,
751 * ACT_RET_PRS_ERR on error.
752 */
753static enum act_parse_ret parse_http_req_disable_l7_retry(const char **args,
754 int *orig_args, struct proxy *px,
755 struct act_rule *rule, char **err)
756{
757 rule->action = ACT_CUSTOM;
758 rule->action_ptr = http_req_disable_l7_retry;
759 return ACT_RET_PRS_OK;
760}
761
Willy Tarreau79e57332018-10-02 16:01:16 +0200762/* This function executes the "capture" action. It executes a fetch expression,
763 * turns the result into a string and puts it in a capture slot. It always
764 * returns 1. If an error occurs the action is cancelled, but the rule
765 * processing continues.
766 */
767static enum act_return http_action_req_capture(struct act_rule *rule, struct proxy *px,
768 struct session *sess, struct stream *s, int flags)
769{
770 struct sample *key;
771 struct cap_hdr *h = rule->arg.cap.hdr;
772 char **cap = s->req_cap;
773 int len;
774
775 key = sample_fetch_as_type(s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.cap.expr, SMP_T_STR);
776 if (!key)
777 return ACT_RET_CONT;
778
779 if (cap[h->index] == NULL)
780 cap[h->index] = pool_alloc(h->pool);
781
782 if (cap[h->index] == NULL) /* no more capture memory */
783 return ACT_RET_CONT;
784
785 len = key->data.u.str.data;
786 if (len > h->len)
787 len = h->len;
788
789 memcpy(cap[h->index], key->data.u.str.area, len);
790 cap[h->index][len] = 0;
791 return ACT_RET_CONT;
792}
793
794/* This function executes the "capture" action and store the result in a
795 * capture slot if exists. It executes a fetch expression, turns the result
796 * into a string and puts it in a capture slot. It always returns 1. If an
797 * error occurs the action is cancelled, but the rule processing continues.
798 */
799static enum act_return http_action_req_capture_by_id(struct act_rule *rule, struct proxy *px,
800 struct session *sess, struct stream *s, int flags)
801{
802 struct sample *key;
803 struct cap_hdr *h;
804 char **cap = s->req_cap;
805 struct proxy *fe = strm_fe(s);
806 int len;
807 int i;
808
809 /* Look for the original configuration. */
810 for (h = fe->req_cap, i = fe->nb_req_cap - 1;
811 h != NULL && i != rule->arg.capid.idx ;
812 i--, h = h->next);
813 if (!h)
814 return ACT_RET_CONT;
815
816 key = sample_fetch_as_type(s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.capid.expr, SMP_T_STR);
817 if (!key)
818 return ACT_RET_CONT;
819
820 if (cap[h->index] == NULL)
821 cap[h->index] = pool_alloc(h->pool);
822
823 if (cap[h->index] == NULL) /* no more capture memory */
824 return ACT_RET_CONT;
825
826 len = key->data.u.str.data;
827 if (len > h->len)
828 len = h->len;
829
830 memcpy(cap[h->index], key->data.u.str.area, len);
831 cap[h->index][len] = 0;
832 return ACT_RET_CONT;
833}
834
835/* Check an "http-request capture" action.
836 *
837 * The function returns 1 in success case, otherwise, it returns 0 and err is
838 * filled.
839 */
840static int check_http_req_capture(struct act_rule *rule, struct proxy *px, char **err)
841{
842 if (rule->action_ptr != http_action_req_capture_by_id)
843 return 1;
844
Baptiste Assmann19a69b32020-01-16 14:34:22 +0100845 /* capture slots can only be declared in frontends, so we can't check their
846 * existence in backends at configuration parsing step
847 */
848 if (px->cap & PR_CAP_FE && rule->arg.capid.idx >= px->nb_req_cap) {
Willy Tarreau79e57332018-10-02 16:01:16 +0200849 memprintf(err, "unable to find capture id '%d' referenced by http-request capture rule",
850 rule->arg.capid.idx);
851 return 0;
852 }
853
854 return 1;
855}
856
Christopher Faulet2eb53962020-01-14 14:47:34 +0100857/* Release memory allocate by an http capture action */
858static void release_http_capture(struct act_rule *rule)
859{
860 if (rule->action_ptr == http_action_req_capture)
861 release_sample_expr(rule->arg.cap.expr);
862 else
863 release_sample_expr(rule->arg.capid.expr);
864}
865
Willy Tarreau79e57332018-10-02 16:01:16 +0200866/* parse an "http-request capture" action. It takes a single argument which is
867 * a sample fetch expression. It stores the expression into arg->act.p[0] and
868 * the allocated hdr_cap struct or the preallocated "id" into arg->act.p[1].
869 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
870 */
871static enum act_parse_ret parse_http_req_capture(const char **args, int *orig_arg, struct proxy *px,
872 struct act_rule *rule, char **err)
873{
874 struct sample_expr *expr;
875 struct cap_hdr *hdr;
876 int cur_arg;
877 int len = 0;
878
879 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
880 if (strcmp(args[cur_arg], "if") == 0 ||
881 strcmp(args[cur_arg], "unless") == 0)
882 break;
883
884 if (cur_arg < *orig_arg + 3) {
885 memprintf(err, "expects <expression> [ 'len' <length> | id <idx> ]");
886 return ACT_RET_PRS_ERR;
887 }
888
889 cur_arg = *orig_arg;
Willy Tarreaue3b57bf2020-02-14 16:50:14 +0100890 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args, NULL);
Willy Tarreau79e57332018-10-02 16:01:16 +0200891 if (!expr)
892 return ACT_RET_PRS_ERR;
893
894 if (!(expr->fetch->val & SMP_VAL_FE_HRQ_HDR)) {
895 memprintf(err,
896 "fetch method '%s' extracts information from '%s', none of which is available here",
897 args[cur_arg-1], sample_src_names(expr->fetch->use));
Christopher Faulet1337b322020-01-14 14:50:55 +0100898 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200899 return ACT_RET_PRS_ERR;
900 }
901
902 if (!args[cur_arg] || !*args[cur_arg]) {
903 memprintf(err, "expects 'len or 'id'");
Christopher Faulet1337b322020-01-14 14:50:55 +0100904 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200905 return ACT_RET_PRS_ERR;
906 }
907
908 if (strcmp(args[cur_arg], "len") == 0) {
909 cur_arg++;
910
911 if (!(px->cap & PR_CAP_FE)) {
912 memprintf(err, "proxy '%s' has no frontend capability", px->id);
Christopher Faulet1337b322020-01-14 14:50:55 +0100913 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200914 return ACT_RET_PRS_ERR;
915 }
916
917 px->conf.args.ctx = ARGC_CAP;
918
919 if (!args[cur_arg]) {
920 memprintf(err, "missing length value");
Christopher Faulet1337b322020-01-14 14:50:55 +0100921 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200922 return ACT_RET_PRS_ERR;
923 }
924 /* we copy the table name for now, it will be resolved later */
925 len = atoi(args[cur_arg]);
926 if (len <= 0) {
927 memprintf(err, "length must be > 0");
Christopher Faulet1337b322020-01-14 14:50:55 +0100928 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200929 return ACT_RET_PRS_ERR;
930 }
931 cur_arg++;
932
Willy Tarreau79e57332018-10-02 16:01:16 +0200933 hdr = calloc(1, sizeof(*hdr));
Remi Tricot-Le Breton737d8b52021-05-12 17:54:17 +0200934 if (!hdr) {
935 memprintf(err, "out of memory");
936 release_sample_expr(expr);
937 return ACT_RET_PRS_ERR;
938 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200939 hdr->next = px->req_cap;
940 hdr->name = NULL; /* not a header capture */
941 hdr->namelen = 0;
942 hdr->len = len;
943 hdr->pool = create_pool("caphdr", hdr->len + 1, MEM_F_SHARED);
944 hdr->index = px->nb_req_cap++;
945
946 px->req_cap = hdr;
947 px->to_log |= LW_REQHDR;
948
949 rule->action = ACT_CUSTOM;
950 rule->action_ptr = http_action_req_capture;
Christopher Faulet2eb53962020-01-14 14:47:34 +0100951 rule->release_ptr = release_http_capture;
Willy Tarreau79e57332018-10-02 16:01:16 +0200952 rule->arg.cap.expr = expr;
953 rule->arg.cap.hdr = hdr;
954 }
955
956 else if (strcmp(args[cur_arg], "id") == 0) {
957 int id;
958 char *error;
959
960 cur_arg++;
961
962 if (!args[cur_arg]) {
963 memprintf(err, "missing id value");
Christopher Faulet1337b322020-01-14 14:50:55 +0100964 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200965 return ACT_RET_PRS_ERR;
966 }
967
968 id = strtol(args[cur_arg], &error, 10);
969 if (*error != '\0') {
970 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
Christopher Faulet1337b322020-01-14 14:50:55 +0100971 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200972 return ACT_RET_PRS_ERR;
973 }
974 cur_arg++;
975
976 px->conf.args.ctx = ARGC_CAP;
977
978 rule->action = ACT_CUSTOM;
979 rule->action_ptr = http_action_req_capture_by_id;
980 rule->check_ptr = check_http_req_capture;
Christopher Faulet2eb53962020-01-14 14:47:34 +0100981 rule->release_ptr = release_http_capture;
Willy Tarreau79e57332018-10-02 16:01:16 +0200982 rule->arg.capid.expr = expr;
983 rule->arg.capid.idx = id;
984 }
985
986 else {
987 memprintf(err, "expects 'len' or 'id', found '%s'", args[cur_arg]);
Christopher Faulet1337b322020-01-14 14:50:55 +0100988 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200989 return ACT_RET_PRS_ERR;
990 }
991
992 *orig_arg = cur_arg;
993 return ACT_RET_PRS_OK;
994}
995
996/* This function executes the "capture" action and store the result in a
997 * capture slot if exists. It executes a fetch expression, turns the result
998 * into a string and puts it in a capture slot. It always returns 1. If an
999 * error occurs the action is cancelled, but the rule processing continues.
1000 */
1001static enum act_return http_action_res_capture_by_id(struct act_rule *rule, struct proxy *px,
1002 struct session *sess, struct stream *s, int flags)
1003{
1004 struct sample *key;
1005 struct cap_hdr *h;
1006 char **cap = s->res_cap;
1007 struct proxy *fe = strm_fe(s);
1008 int len;
1009 int i;
1010
1011 /* Look for the original configuration. */
1012 for (h = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
1013 h != NULL && i != rule->arg.capid.idx ;
1014 i--, h = h->next);
1015 if (!h)
1016 return ACT_RET_CONT;
1017
1018 key = sample_fetch_as_type(s->be, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL, rule->arg.capid.expr, SMP_T_STR);
1019 if (!key)
1020 return ACT_RET_CONT;
1021
1022 if (cap[h->index] == NULL)
1023 cap[h->index] = pool_alloc(h->pool);
1024
1025 if (cap[h->index] == NULL) /* no more capture memory */
1026 return ACT_RET_CONT;
1027
1028 len = key->data.u.str.data;
1029 if (len > h->len)
1030 len = h->len;
1031
1032 memcpy(cap[h->index], key->data.u.str.area, len);
1033 cap[h->index][len] = 0;
1034 return ACT_RET_CONT;
1035}
1036
1037/* Check an "http-response capture" action.
1038 *
1039 * The function returns 1 in success case, otherwise, it returns 0 and err is
1040 * filled.
1041 */
1042static int check_http_res_capture(struct act_rule *rule, struct proxy *px, char **err)
1043{
1044 if (rule->action_ptr != http_action_res_capture_by_id)
1045 return 1;
1046
Tim Duesterhusf3f4aa02020-07-03 13:43:42 +02001047 /* capture slots can only be declared in frontends, so we can't check their
1048 * existence in backends at configuration parsing step
1049 */
1050 if (px->cap & PR_CAP_FE && rule->arg.capid.idx >= px->nb_rsp_cap) {
Willy Tarreau79e57332018-10-02 16:01:16 +02001051 memprintf(err, "unable to find capture id '%d' referenced by http-response capture rule",
1052 rule->arg.capid.idx);
1053 return 0;
1054 }
1055
1056 return 1;
1057}
1058
1059/* parse an "http-response capture" action. It takes a single argument which is
1060 * a sample fetch expression. It stores the expression into arg->act.p[0] and
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -07001061 * the allocated hdr_cap struct of the preallocated id into arg->act.p[1].
Willy Tarreau79e57332018-10-02 16:01:16 +02001062 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
1063 */
1064static enum act_parse_ret parse_http_res_capture(const char **args, int *orig_arg, struct proxy *px,
1065 struct act_rule *rule, char **err)
1066{
1067 struct sample_expr *expr;
1068 int cur_arg;
1069 int id;
1070 char *error;
1071
1072 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
1073 if (strcmp(args[cur_arg], "if") == 0 ||
1074 strcmp(args[cur_arg], "unless") == 0)
1075 break;
1076
1077 if (cur_arg < *orig_arg + 3) {
1078 memprintf(err, "expects <expression> id <idx>");
1079 return ACT_RET_PRS_ERR;
1080 }
1081
1082 cur_arg = *orig_arg;
Willy Tarreaue3b57bf2020-02-14 16:50:14 +01001083 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 +02001084 if (!expr)
1085 return ACT_RET_PRS_ERR;
1086
1087 if (!(expr->fetch->val & SMP_VAL_FE_HRS_HDR)) {
1088 memprintf(err,
1089 "fetch method '%s' extracts information from '%s', none of which is available here",
1090 args[cur_arg-1], sample_src_names(expr->fetch->use));
Christopher Faulet1337b322020-01-14 14:50:55 +01001091 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +02001092 return ACT_RET_PRS_ERR;
1093 }
1094
1095 if (!args[cur_arg] || !*args[cur_arg]) {
1096 memprintf(err, "expects 'id'");
Christopher Faulet1337b322020-01-14 14:50:55 +01001097 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +02001098 return ACT_RET_PRS_ERR;
1099 }
1100
1101 if (strcmp(args[cur_arg], "id") != 0) {
1102 memprintf(err, "expects 'id', found '%s'", args[cur_arg]);
Christopher Faulet1337b322020-01-14 14:50:55 +01001103 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +02001104 return ACT_RET_PRS_ERR;
1105 }
1106
1107 cur_arg++;
1108
1109 if (!args[cur_arg]) {
1110 memprintf(err, "missing id value");
Christopher Faulet1337b322020-01-14 14:50:55 +01001111 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +02001112 return ACT_RET_PRS_ERR;
1113 }
1114
1115 id = strtol(args[cur_arg], &error, 10);
1116 if (*error != '\0') {
1117 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
Christopher Faulet1337b322020-01-14 14:50:55 +01001118 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +02001119 return ACT_RET_PRS_ERR;
1120 }
1121 cur_arg++;
1122
1123 px->conf.args.ctx = ARGC_CAP;
1124
1125 rule->action = ACT_CUSTOM;
1126 rule->action_ptr = http_action_res_capture_by_id;
1127 rule->check_ptr = check_http_res_capture;
Christopher Faulet2eb53962020-01-14 14:47:34 +01001128 rule->release_ptr = release_http_capture;
Willy Tarreau79e57332018-10-02 16:01:16 +02001129 rule->arg.capid.expr = expr;
1130 rule->arg.capid.idx = id;
1131
1132 *orig_arg = cur_arg;
1133 return ACT_RET_PRS_OK;
1134}
1135
Christopher Faulet81e20172019-12-12 16:40:30 +01001136/* Parse a "allow" action for a request or a response rule. It takes no argument. It
1137 * returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
1138 */
1139static enum act_parse_ret parse_http_allow(const char **args, int *orig_arg, struct proxy *px,
1140 struct act_rule *rule, char **err)
1141{
1142 rule->action = ACT_ACTION_ALLOW;
Christopher Faulet245cf792019-12-18 14:58:12 +01001143 rule->flags |= ACT_FLAG_FINAL;
Christopher Faulet81e20172019-12-12 16:40:30 +01001144 return ACT_RET_PRS_OK;
1145}
1146
Christopher Faulete0fca292020-01-13 21:49:03 +01001147/* Parse "deny" or "tarpit" actions for a request rule or "deny" action for a
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001148 * response rule. It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on
1149 * error. It relies on http_parse_http_reply() to set
1150 * <.arg.http_reply>.
Christopher Faulet81e20172019-12-12 16:40:30 +01001151 */
Christopher Faulete0fca292020-01-13 21:49:03 +01001152static enum act_parse_ret parse_http_deny(const char **args, int *orig_arg, struct proxy *px,
1153 struct act_rule *rule, char **err)
Christopher Faulet81e20172019-12-12 16:40:30 +01001154{
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001155 int default_status;
1156 int cur_arg, arg = 0;
Christopher Faulet81e20172019-12-12 16:40:30 +01001157
1158 cur_arg = *orig_arg;
Christopher Faulete0fca292020-01-13 21:49:03 +01001159 if (rule->from == ACT_F_HTTP_REQ) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001160 if (strcmp(args[cur_arg - 1], "tarpit") == 0) {
Christopher Faulete0fca292020-01-13 21:49:03 +01001161 rule->action = ACT_HTTP_REQ_TARPIT;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001162 default_status = 500;
Christopher Faulet81e20172019-12-12 16:40:30 +01001163 }
Christopher Faulete0fca292020-01-13 21:49:03 +01001164 else {
1165 rule->action = ACT_ACTION_DENY;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001166 default_status = 403;
Christopher Faulet81e20172019-12-12 16:40:30 +01001167 }
Christopher Faulet81e20172019-12-12 16:40:30 +01001168 }
Christopher Faulete0fca292020-01-13 21:49:03 +01001169 else {
Christopher Faulet554c0eb2020-01-14 12:00:28 +01001170 rule->action = ACT_ACTION_DENY;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001171 default_status = 502;
Christopher Faulete0fca292020-01-13 21:49:03 +01001172 }
Christopher Faulet040c8cd2020-01-13 16:43:45 +01001173
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001174 /* If no args or only a deny_status specified, fallback on the legacy
1175 * mode and use default error files despite the fact that
1176 * default-errorfiles is not used. Otherwise, parse an http reply.
1177 */
Christopher Faulet040c8cd2020-01-13 16:43:45 +01001178
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001179 /* Prepare parsing of log-format strings */
1180 px->conf.args.ctx = ((rule->from == ACT_F_HTTP_REQ) ? ARGC_HRQ : ARGC_HRS);
Christopher Faulet554c0eb2020-01-14 12:00:28 +01001181
Christopher Faulet9467f182020-06-30 09:32:01 +02001182 if (!*(args[cur_arg]) || strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001183 rule->arg.http_reply = http_parse_http_reply((const char *[]){"default-errorfiles", ""}, &arg, px, default_status, err);
1184 goto end;
Christopher Faulet554c0eb2020-01-14 12:00:28 +01001185 }
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001186
1187 if (strcmp(args[cur_arg], "deny_status") == 0) {
Christopher Faulet9467f182020-06-30 09:32:01 +02001188 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 +02001189 rule->arg.http_reply = http_parse_http_reply((const char *[]){"status", args[cur_arg+1], "default-errorfiles", ""},
1190 &arg, px, default_status, err);
1191 *orig_arg += 2;
1192 goto end;
Christopher Faulet554c0eb2020-01-14 12:00:28 +01001193 }
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001194 args[cur_arg] += 5; /* skip "deny_" for the parsing */
Christopher Faulet554c0eb2020-01-14 12:00:28 +01001195 }
1196
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001197 rule->arg.http_reply = http_parse_http_reply(args, orig_arg, px, default_status, err);
Christopher Faulet554c0eb2020-01-14 12:00:28 +01001198
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001199 end:
1200 if (!rule->arg.http_reply)
1201 return ACT_RET_PRS_ERR;
1202
1203 rule->flags |= ACT_FLAG_FINAL;
1204 rule->check_ptr = check_act_http_reply;
1205 rule->release_ptr = release_act_http_reply;
Christopher Faulet81e20172019-12-12 16:40:30 +01001206 return ACT_RET_PRS_OK;
1207}
1208
Christopher Fauletb3048832020-05-27 15:26:43 +02001209
1210/* This function executes a auth action. It builds an 401/407 HTX message using
1211 * the corresponding proxy's error message. On success, it returns
1212 * ACT_RET_ABRT. If an error occurs ACT_RET_ERR is returned.
1213 */
1214static enum act_return http_action_auth(struct act_rule *rule, struct proxy *px,
1215 struct session *sess, struct stream *s, int flags)
1216{
1217 struct channel *req = &s->req;
1218 struct channel *res = &s->res;
1219 struct htx *htx = htx_from_buf(&res->buf);
1220 struct http_reply *reply;
1221 const char *auth_realm;
1222 struct http_hdr_ctx ctx;
1223 struct ist hdr;
1224
1225 /* Auth might be performed on regular http-req rules as well as on stats */
1226 auth_realm = rule->arg.http.str.ptr;
1227 if (!auth_realm) {
1228 if (px->uri_auth && s->current_rule_list == &px->uri_auth->http_req_rules)
1229 auth_realm = STATS_DEFAULT_REALM;
1230 else
1231 auth_realm = px->id;
1232 }
1233
1234 if (!(s->txn->flags & TX_USE_PX_CONN)) {
1235 s->txn->status = 401;
1236 hdr = ist("WWW-Authenticate");
1237 }
1238 else {
1239 s->txn->status = 407;
1240 hdr = ist("Proxy-Authenticate");
1241 }
1242 reply = http_error_message(s);
1243 channel_htx_truncate(res, htx);
1244
1245 if (chunk_printf(&trash, "Basic realm=\"%s\"", auth_realm) == -1)
1246 goto fail;
1247
1248 /* Write the generic 40x message */
1249 if (http_reply_to_htx(s, htx, reply) == -1)
1250 goto fail;
1251
1252 /* Remove all existing occurrences of the XXX-Authenticate header */
1253 ctx.blk = NULL;
1254 while (http_find_header(htx, hdr, &ctx, 1))
1255 http_remove_header(htx, &ctx);
1256
1257 /* Now a the right XXX-Authenticate header */
1258 if (!http_add_header(htx, hdr, ist2(b_orig(&trash), b_data(&trash))))
1259 goto fail;
1260
1261 /* Finally forward the reply */
1262 htx_to_buf(htx, &res->buf);
1263 if (!http_forward_proxy_resp(s, 1))
1264 goto fail;
1265
1266 /* Note: Only eval on the request */
1267 s->logs.tv_request = now;
1268 req->analysers &= AN_REQ_FLT_END;
1269
1270 if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Willy Tarreau4781b152021-04-06 13:53:36 +02001271 _HA_ATOMIC_INC(&s->sess->fe->fe_counters.intercepted_req);
Christopher Fauletb3048832020-05-27 15:26:43 +02001272
1273 if (!(s->flags & SF_ERR_MASK))
1274 s->flags |= SF_ERR_LOCAL;
1275 if (!(s->flags & SF_FINST_MASK))
1276 s->flags |= SF_FINST_R;
1277
1278 stream_inc_http_err_ctr(s);
1279 return ACT_RET_ABRT;
1280
1281 fail:
1282 /* If an error occurred, remove the incomplete HTTP response from the
1283 * buffer */
1284 channel_htx_truncate(res, htx);
1285 return ACT_RET_ERR;
1286}
1287
Christopher Faulet81e20172019-12-12 16:40:30 +01001288/* Parse a "auth" action. It may take 2 optional arguments to define a "realm"
1289 * parameter. It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
1290 */
1291static enum act_parse_ret parse_http_auth(const char **args, int *orig_arg, struct proxy *px,
1292 struct act_rule *rule, char **err)
1293{
1294 int cur_arg;
1295
Christopher Fauletb3048832020-05-27 15:26:43 +02001296 rule->action = ACT_CUSTOM;
Christopher Faulet245cf792019-12-18 14:58:12 +01001297 rule->flags |= ACT_FLAG_FINAL;
Christopher Fauletb3048832020-05-27 15:26:43 +02001298 rule->action_ptr = http_action_auth;
Christopher Faulet2eb53962020-01-14 14:47:34 +01001299 rule->release_ptr = release_http_action;
Christopher Faulet81e20172019-12-12 16:40:30 +01001300
1301 cur_arg = *orig_arg;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001302 if (strcmp(args[cur_arg], "realm") == 0) {
Christopher Faulet81e20172019-12-12 16:40:30 +01001303 cur_arg++;
1304 if (!*args[cur_arg]) {
1305 memprintf(err, "missing realm value.\n");
1306 return ACT_RET_PRS_ERR;
1307 }
Christopher Faulet96bff762019-12-17 13:46:18 +01001308 rule->arg.http.str.ptr = strdup(args[cur_arg]);
1309 rule->arg.http.str.len = strlen(rule->arg.http.str.ptr);
Christopher Faulet81e20172019-12-12 16:40:30 +01001310 cur_arg++;
1311 }
1312
Christopher Fauletc20b3712020-01-27 15:51:56 +01001313 LIST_INIT(&rule->arg.http.fmt);
Christopher Faulet81e20172019-12-12 16:40:30 +01001314 *orig_arg = cur_arg;
1315 return ACT_RET_PRS_OK;
1316}
1317
1318/* Parse a "set-nice" action. It takes the nice value as argument. It returns
1319 * ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
1320 */
1321static enum act_parse_ret parse_http_set_nice(const char **args, int *orig_arg, struct proxy *px,
1322 struct act_rule *rule, char **err)
1323{
1324 int cur_arg;
1325
1326 rule->action = ACT_HTTP_SET_NICE;
1327
1328 cur_arg = *orig_arg;
1329 if (!*args[cur_arg]) {
1330 memprintf(err, "expects exactly 1 argument (integer value)");
1331 return ACT_RET_PRS_ERR;
1332 }
Christopher Faulet96bff762019-12-17 13:46:18 +01001333 rule->arg.http.i = atoi(args[cur_arg]);
1334 if (rule->arg.http.i < -1024)
1335 rule->arg.http.i = -1024;
1336 else if (rule->arg.http.i > 1024)
1337 rule->arg.http.i = 1024;
Christopher Faulet81e20172019-12-12 16:40:30 +01001338
Christopher Fauletc20b3712020-01-27 15:51:56 +01001339 LIST_INIT(&rule->arg.http.fmt);
Christopher Faulet81e20172019-12-12 16:40:30 +01001340 *orig_arg = cur_arg + 1;
1341 return ACT_RET_PRS_OK;
1342}
1343
1344/* Parse a "set-tos" action. It takes the TOS value as argument. It returns
1345 * ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
1346 */
1347static enum act_parse_ret parse_http_set_tos(const char **args, int *orig_arg, struct proxy *px,
1348 struct act_rule *rule, char **err)
1349{
1350#ifdef IP_TOS
1351 char *endp;
1352 int cur_arg;
1353
1354 rule->action = ACT_HTTP_SET_TOS;
1355
1356 cur_arg = *orig_arg;
1357 if (!*args[cur_arg]) {
1358 memprintf(err, "expects exactly 1 argument (integer/hex value)");
1359 return ACT_RET_PRS_ERR;
1360 }
Christopher Faulet96bff762019-12-17 13:46:18 +01001361 rule->arg.http.i = strtol(args[cur_arg], &endp, 0);
Christopher Faulet81e20172019-12-12 16:40:30 +01001362 if (endp && *endp != '\0') {
1363 memprintf(err, "invalid character starting at '%s' (integer/hex value expected)", endp);
1364 return ACT_RET_PRS_ERR;
1365 }
1366
Christopher Fauletc20b3712020-01-27 15:51:56 +01001367 LIST_INIT(&rule->arg.http.fmt);
Christopher Faulet81e20172019-12-12 16:40:30 +01001368 *orig_arg = cur_arg + 1;
1369 return ACT_RET_PRS_OK;
1370#else
1371 memprintf(err, "not supported on this platform (IP_TOS undefined)");
1372 return ACT_RET_PRS_ERR;
1373#endif
1374}
1375
1376/* Parse a "set-mark" action. It takes the MARK value as argument. It returns
1377 * ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
1378 */
1379static enum act_parse_ret parse_http_set_mark(const char **args, int *orig_arg, struct proxy *px,
1380 struct act_rule *rule, char **err)
1381{
1382#ifdef SO_MARK
1383 char *endp;
1384 int cur_arg;
1385
1386 rule->action = ACT_HTTP_SET_MARK;
1387
1388 cur_arg = *orig_arg;
1389 if (!*args[cur_arg]) {
1390 memprintf(err, "expects exactly 1 argument (integer/hex value)");
1391 return ACT_RET_PRS_ERR;
1392 }
Christopher Faulet96bff762019-12-17 13:46:18 +01001393 rule->arg.http.i = strtoul(args[cur_arg], &endp, 0);
Christopher Faulet81e20172019-12-12 16:40:30 +01001394 if (endp && *endp != '\0') {
1395 memprintf(err, "invalid character starting at '%s' (integer/hex value expected)", endp);
1396 return ACT_RET_PRS_ERR;
1397 }
1398
Christopher Fauletc20b3712020-01-27 15:51:56 +01001399 LIST_INIT(&rule->arg.http.fmt);
Christopher Faulet81e20172019-12-12 16:40:30 +01001400 *orig_arg = cur_arg + 1;
1401 global.last_checks |= LSTCHK_NETADM;
1402 return ACT_RET_PRS_OK;
1403#else
1404 memprintf(err, "not supported on this platform (SO_MARK undefined)");
1405 return ACT_RET_PRS_ERR;
1406#endif
1407}
1408
1409/* Parse a "set-log-level" action. It takes the level value as argument. It
1410 * returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
1411 */
1412static enum act_parse_ret parse_http_set_log_level(const char **args, int *orig_arg, struct proxy *px,
1413 struct act_rule *rule, char **err)
1414{
1415 int cur_arg;
1416
1417 rule->action = ACT_HTTP_SET_LOGL;
1418
1419 cur_arg = *orig_arg;
1420 if (!*args[cur_arg]) {
1421 bad_log_level:
1422 memprintf(err, "expects exactly 1 argument (log level name or 'silent')");
1423 return ACT_RET_PRS_ERR;
1424 }
1425 if (strcmp(args[cur_arg], "silent") == 0)
Christopher Faulet96bff762019-12-17 13:46:18 +01001426 rule->arg.http.i = -1;
1427 else if ((rule->arg.http.i = get_log_level(args[cur_arg]) + 1) == 0)
Christopher Faulet81e20172019-12-12 16:40:30 +01001428 goto bad_log_level;
1429
Christopher Fauletc20b3712020-01-27 15:51:56 +01001430 LIST_INIT(&rule->arg.http.fmt);
Christopher Faulet81e20172019-12-12 16:40:30 +01001431 *orig_arg = cur_arg + 1;
1432 return ACT_RET_PRS_OK;
1433}
1434
Christopher Faulet91b3ec12020-01-17 22:30:06 +01001435/* This function executes a early-hint action. It adds an HTTP Early Hint HTTP
1436 * 103 response header with <.arg.http.str> name and with a value built
1437 * according to <.arg.http.fmt> log line format. If it is the first early-hint
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001438 * rule of series, the 103 response start-line is added first. At the end, if
Christopher Faulet91b3ec12020-01-17 22:30:06 +01001439 * the next rule is not an early-hint rule or if it is the last rule, the EOH
1440 * block is added to terminate the response. On success, it returns
1441 * ACT_RET_CONT. If an error occurs while soft rewrites are enabled, the action
1442 * is canceled, but the rule processing continue. Otherwsize ACT_RET_ERR is
1443 * returned.
1444 */
1445static enum act_return http_action_early_hint(struct act_rule *rule, struct proxy *px,
1446 struct session *sess, struct stream *s, int flags)
1447{
1448 struct act_rule *prev_rule, *next_rule;
1449 struct channel *res = &s->res;
1450 struct htx *htx = htx_from_buf(&res->buf);
1451 struct buffer *value = alloc_trash_chunk();
1452 enum act_return ret = ACT_RET_CONT;
1453
1454 if (!(s->txn->req.flags & HTTP_MSGF_VER_11))
1455 goto leave;
1456
1457 if (!value) {
1458 if (!(s->flags & SF_ERR_MASK))
1459 s->flags |= SF_ERR_RESOURCE;
1460 goto error;
1461 }
1462
1463 /* get previous and next rules */
1464 prev_rule = LIST_PREV(&rule->list, typeof(rule), list);
1465 next_rule = LIST_NEXT(&rule->list, typeof(rule), list);
1466
1467 /* if no previous rule or previous rule is not early-hint, start a new response. Otherwise,
1468 * continue to add link to a previously started response */
1469 if (&prev_rule->list == s->current_rule_list || prev_rule->action_ptr != http_action_early_hint) {
1470 struct htx_sl *sl;
1471 unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|
1472 HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
1473
1474 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
1475 ist("HTTP/1.1"), ist("103"), ist("Early Hints"));
1476 if (!sl)
1477 goto error;
1478 sl->info.res.status = 103;
1479 }
1480
1481 /* Add the HTTP Early Hint HTTP 103 response heade */
1482 value->data = build_logline(s, b_tail(value), b_room(value), &rule->arg.http.fmt);
1483 if (!htx_add_header(htx, rule->arg.http.str, ist2(b_head(value), b_data(value))))
1484 goto error;
1485
1486 /* if it is the last rule or the next one is not an early-hint, terminate the current
1487 * response. */
1488 if (&next_rule->list == s->current_rule_list || next_rule->action_ptr != http_action_early_hint) {
Christopher Faulet91b3ec12020-01-17 22:30:06 +01001489 if (!htx_add_endof(htx, HTX_BLK_EOH)) {
1490 /* If an error occurred during an Early-hint rule,
1491 * remove the incomplete HTTP 103 response from the
1492 * buffer */
1493 goto error;
1494 }
1495
Christopher Fauleta72a7e42020-01-28 09:28:11 +01001496 if (!http_forward_proxy_resp(s, 0))
1497 goto error;
Christopher Faulet91b3ec12020-01-17 22:30:06 +01001498 }
1499
1500 leave:
1501 free_trash_chunk(value);
1502 return ret;
1503
1504 error:
1505 /* If an error occurred during an Early-hint rule, remove the incomplete
1506 * HTTP 103 response from the buffer */
1507 channel_htx_truncate(res, htx);
1508 ret = ACT_RET_ERR;
1509 goto leave;
1510}
1511
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001512/* This function executes a set-header or add-header actions. It builds a string
1513 * in the trash from the specified format string. It finds the action to be
1514 * performed in <.action>, previously filled by function parse_set_header(). The
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001515 * replacement action is executed by the function http_action_set_header(). On
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001516 * success, it returns ACT_RET_CONT. If an error occurs while soft rewrites are
1517 * enabled, the action is canceled, but the rule processing continue. Otherwsize
1518 * ACT_RET_ERR is returned.
1519 */
1520static enum act_return http_action_set_header(struct act_rule *rule, struct proxy *px,
1521 struct session *sess, struct stream *s, int flags)
1522{
Christopher Faulet91e31d82020-01-24 15:37:13 +01001523 struct http_msg *msg = ((rule->from == ACT_F_HTTP_REQ) ? &s->txn->req : &s->txn->rsp);
1524 struct htx *htx = htxbuf(&msg->chn->buf);
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001525 enum act_return ret = ACT_RET_CONT;
1526 struct buffer *replace;
1527 struct http_hdr_ctx ctx;
1528 struct ist n, v;
1529
1530 replace = alloc_trash_chunk();
1531 if (!replace)
1532 goto fail_alloc;
1533
1534 replace->data = build_logline(s, replace->area, replace->size, &rule->arg.http.fmt);
1535 n = rule->arg.http.str;
1536 v = ist2(replace->area, replace->data);
1537
1538 if (rule->action == 0) { // set-header
1539 /* remove all occurrences of the header */
1540 ctx.blk = NULL;
1541 while (http_find_header(htx, n, &ctx, 1))
1542 http_remove_header(htx, &ctx);
1543 }
1544
1545 /* Now add header */
1546 if (!http_add_header(htx, n, v))
1547 goto fail_rewrite;
1548
1549 leave:
1550 free_trash_chunk(replace);
1551 return ret;
1552
1553 fail_alloc:
1554 if (!(s->flags & SF_ERR_MASK))
1555 s->flags |= SF_ERR_RESOURCE;
1556 ret = ACT_RET_ERR;
1557 goto leave;
1558
1559 fail_rewrite:
Willy Tarreau4781b152021-04-06 13:53:36 +02001560 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_rewrites);
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001561 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +02001562 _HA_ATOMIC_INC(&s->be->be_counters.failed_rewrites);
William Lallemand36119de2021-03-08 15:26:48 +01001563 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001564 _HA_ATOMIC_INC(&sess->listener->counters->failed_rewrites);
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001565 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001566 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_rewrites);
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001567
Christopher Faulet333bf8c2020-01-22 14:38:05 +01001568 if (!(msg->flags & HTTP_MSGF_SOFT_RW)) {
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001569 ret = ACT_RET_ERR;
Christopher Faulet333bf8c2020-01-22 14:38:05 +01001570 if (!(s->flags & SF_ERR_MASK))
1571 s->flags |= SF_ERR_PRXCOND;
1572 }
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001573 goto leave;
1574}
1575
Christopher Faulet81e20172019-12-12 16:40:30 +01001576/* Parse a "set-header", "add-header" or "early-hint" actions. It takes an
1577 * header name and a log-format string as arguments. It returns ACT_RET_PRS_OK
1578 * on success, ACT_RET_PRS_ERR on error.
1579 *
1580 * Note: same function is used for the request and the response. However
1581 * "early-hint" rules are only supported for request rules.
1582 */
1583static enum act_parse_ret parse_http_set_header(const char **args, int *orig_arg, struct proxy *px,
1584 struct act_rule *rule, char **err)
1585{
Christopher Faulet81e20172019-12-12 16:40:30 +01001586 int cap, cur_arg;
1587
Christopher Faulet91b3ec12020-01-17 22:30:06 +01001588 if (args[*orig_arg-1][0] == 'e') {
1589 rule->action = ACT_CUSTOM;
1590 rule->action_ptr = http_action_early_hint;
1591 }
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001592 else {
1593 if (args[*orig_arg-1][0] == 's')
1594 rule->action = 0; // set-header
1595 else
1596 rule->action = 1; // add-header
1597 rule->action_ptr = http_action_set_header;
1598 }
Christopher Faulet2eb53962020-01-14 14:47:34 +01001599 rule->release_ptr = release_http_action;
Christopher Faulet81e20172019-12-12 16:40:30 +01001600
1601 cur_arg = *orig_arg;
1602 if (!*args[cur_arg] || !*args[cur_arg+1]) {
1603 memprintf(err, "expects exactly 2 arguments");
1604 return ACT_RET_PRS_ERR;
1605 }
1606
Christopher Faulet81e20172019-12-12 16:40:30 +01001607
Christopher Faulet96bff762019-12-17 13:46:18 +01001608 rule->arg.http.str.ptr = strdup(args[cur_arg]);
1609 rule->arg.http.str.len = strlen(rule->arg.http.str.ptr);
1610 LIST_INIT(&rule->arg.http.fmt);
Christopher Faulet81e20172019-12-12 16:40:30 +01001611
1612 if (rule->from == ACT_F_HTTP_REQ) {
1613 px->conf.args.ctx = ARGC_HRQ;
1614 cap = (px->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR;
1615 }
1616 else{
1617 px->conf.args.ctx = ARGC_HRS;
1618 cap = (px->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR;
1619 }
1620
1621 cur_arg++;
Christopher Faulet1337b322020-01-14 14:50:55 +01001622 if (!parse_logformat_string(args[cur_arg], px, &rule->arg.http.fmt, LOG_OPT_HTTP, cap, err)) {
Tim Duesterhused526372020-03-05 17:56:33 +01001623 istfree(&rule->arg.http.str);
Christopher Faulet81e20172019-12-12 16:40:30 +01001624 return ACT_RET_PRS_ERR;
Christopher Faulet1337b322020-01-14 14:50:55 +01001625 }
Christopher Faulet81e20172019-12-12 16:40:30 +01001626
1627 free(px->conf.lfs_file);
1628 px->conf.lfs_file = strdup(px->conf.args.file);
1629 px->conf.lfs_line = px->conf.args.line;
1630
1631 *orig_arg = cur_arg + 1;
1632 return ACT_RET_PRS_OK;
1633}
1634
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001635/* This function executes a replace-header or replace-value actions. It
1636 * builds a string in the trash from the specified format string. It finds
1637 * the action to be performed in <.action>, previously filled by function
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001638 * parse_replace_header(). The replacement action is executed by the function
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001639 * http_action_replace_header(). On success, it returns ACT_RET_CONT. If an error
1640 * occurs while soft rewrites are enabled, the action is canceled, but the rule
1641 * processing continue. Otherwsize ACT_RET_ERR is returned.
1642 */
1643static enum act_return http_action_replace_header(struct act_rule *rule, struct proxy *px,
1644 struct session *sess, struct stream *s, int flags)
1645{
Christopher Faulet91e31d82020-01-24 15:37:13 +01001646 struct http_msg *msg = ((rule->from == ACT_F_HTTP_REQ) ? &s->txn->req : &s->txn->rsp);
1647 struct htx *htx = htxbuf(&msg->chn->buf);
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001648 enum act_return ret = ACT_RET_CONT;
1649 struct buffer *replace;
1650 int r;
1651
1652 replace = alloc_trash_chunk();
1653 if (!replace)
1654 goto fail_alloc;
1655
1656 replace->data = build_logline(s, replace->area, replace->size, &rule->arg.http.fmt);
1657
1658 r = http_replace_hdrs(s, htx, rule->arg.http.str, replace->area, rule->arg.http.re, (rule->action == 0));
1659 if (r == -1)
1660 goto fail_rewrite;
1661
1662 leave:
1663 free_trash_chunk(replace);
1664 return ret;
1665
1666 fail_alloc:
1667 if (!(s->flags & SF_ERR_MASK))
1668 s->flags |= SF_ERR_RESOURCE;
1669 ret = ACT_RET_ERR;
1670 goto leave;
1671
1672 fail_rewrite:
Willy Tarreau4781b152021-04-06 13:53:36 +02001673 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_rewrites);
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001674 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +02001675 _HA_ATOMIC_INC(&s->be->be_counters.failed_rewrites);
William Lallemand36119de2021-03-08 15:26:48 +01001676 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001677 _HA_ATOMIC_INC(&sess->listener->counters->failed_rewrites);
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001678 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001679 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_rewrites);
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001680
Christopher Faulet333bf8c2020-01-22 14:38:05 +01001681 if (!(msg->flags & HTTP_MSGF_SOFT_RW)) {
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001682 ret = ACT_RET_ERR;
Christopher Faulet333bf8c2020-01-22 14:38:05 +01001683 if (!(s->flags & SF_ERR_MASK))
1684 s->flags |= SF_ERR_PRXCOND;
1685 }
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001686 goto leave;
1687}
1688
Christopher Faulet81e20172019-12-12 16:40:30 +01001689/* Parse a "replace-header" or "replace-value" actions. It takes an header name,
1690 * a regex and replacement string as arguments. It returns ACT_RET_PRS_OK on
1691 * success, ACT_RET_PRS_ERR on error.
1692 */
1693static enum act_parse_ret parse_http_replace_header(const char **args, int *orig_arg, struct proxy *px,
1694 struct act_rule *rule, char **err)
1695{
1696 int cap, cur_arg;
1697
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001698 if (args[*orig_arg-1][8] == 'h')
1699 rule->action = 0; // replace-header
1700 else
1701 rule->action = 1; // replace-value
1702 rule->action_ptr = http_action_replace_header;
Christopher Faulet2eb53962020-01-14 14:47:34 +01001703 rule->release_ptr = release_http_action;
Christopher Faulet81e20172019-12-12 16:40:30 +01001704
1705 cur_arg = *orig_arg;
1706 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2]) {
1707 memprintf(err, "expects exactly 3 arguments");
1708 return ACT_RET_PRS_ERR;
1709 }
1710
Christopher Faulet96bff762019-12-17 13:46:18 +01001711 rule->arg.http.str.ptr = strdup(args[cur_arg]);
1712 rule->arg.http.str.len = strlen(rule->arg.http.str.ptr);
1713 LIST_INIT(&rule->arg.http.fmt);
Christopher Faulet81e20172019-12-12 16:40:30 +01001714
1715 cur_arg++;
Christopher Faulet1337b322020-01-14 14:50:55 +01001716 if (!(rule->arg.http.re = regex_comp(args[cur_arg], 1, 1, err))) {
Tim Duesterhused526372020-03-05 17:56:33 +01001717 istfree(&rule->arg.http.str);
Christopher Faulet81e20172019-12-12 16:40:30 +01001718 return ACT_RET_PRS_ERR;
Christopher Faulet1337b322020-01-14 14:50:55 +01001719 }
Christopher Faulet81e20172019-12-12 16:40:30 +01001720
1721 if (rule->from == ACT_F_HTTP_REQ) {
1722 px->conf.args.ctx = ARGC_HRQ;
1723 cap = (px->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR;
1724 }
1725 else{
1726 px->conf.args.ctx = ARGC_HRS;
1727 cap = (px->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR;
1728 }
1729
1730 cur_arg++;
Christopher Faulet1337b322020-01-14 14:50:55 +01001731 if (!parse_logformat_string(args[cur_arg], px, &rule->arg.http.fmt, LOG_OPT_HTTP, cap, err)) {
Tim Duesterhused526372020-03-05 17:56:33 +01001732 istfree(&rule->arg.http.str);
Christopher Faulet1337b322020-01-14 14:50:55 +01001733 regex_free(rule->arg.http.re);
Christopher Faulet81e20172019-12-12 16:40:30 +01001734 return ACT_RET_PRS_ERR;
Christopher Faulet1337b322020-01-14 14:50:55 +01001735 }
Christopher Faulet81e20172019-12-12 16:40:30 +01001736
1737 free(px->conf.lfs_file);
1738 px->conf.lfs_file = strdup(px->conf.args.file);
1739 px->conf.lfs_line = px->conf.args.line;
1740
1741 *orig_arg = cur_arg + 1;
1742 return ACT_RET_PRS_OK;
1743}
1744
Maciej Zdebebdd4c52020-11-20 13:58:48 +00001745/* This function executes a del-header action with selected matching mode for
1746 * header name. It finds the matching method to be performed in <.action>, previously
1747 * filled by function parse_http_del_header(). On success, it returns ACT_RET_CONT.
1748 * Otherwise ACT_RET_ERR is returned.
1749 */
1750static enum act_return http_action_del_header(struct act_rule *rule, struct proxy *px,
1751 struct session *sess, struct stream *s, int flags)
1752{
1753 struct http_hdr_ctx ctx;
1754 struct http_msg *msg = ((rule->from == ACT_F_HTTP_REQ) ? &s->txn->req : &s->txn->rsp);
1755 struct htx *htx = htxbuf(&msg->chn->buf);
1756 enum act_return ret = ACT_RET_CONT;
1757
1758 /* remove all occurrences of the header */
1759 ctx.blk = NULL;
1760 switch (rule->action) {
1761 case PAT_MATCH_STR:
1762 while (http_find_header(htx, rule->arg.http.str, &ctx, 1))
1763 http_remove_header(htx, &ctx);
1764 break;
1765 case PAT_MATCH_BEG:
1766 while (http_find_pfx_header(htx, rule->arg.http.str, &ctx, 1))
1767 http_remove_header(htx, &ctx);
1768 break;
1769 case PAT_MATCH_END:
1770 while (http_find_sfx_header(htx, rule->arg.http.str, &ctx, 1))
1771 http_remove_header(htx, &ctx);
1772 break;
1773 case PAT_MATCH_SUB:
1774 while (http_find_sub_header(htx, rule->arg.http.str, &ctx, 1))
1775 http_remove_header(htx, &ctx);
1776 break;
1777 case PAT_MATCH_REG:
1778 while (http_match_header(htx, rule->arg.http.re, &ctx, 1))
1779 http_remove_header(htx, &ctx);
1780 break;
1781 default:
1782 return ACT_RET_ERR;
1783 }
1784 return ret;
1785}
1786
1787/* Parse a "del-header" action. It takes string as a required argument,
1788 * optional flag (currently only -m) and optional matching method of input string
1789 * with header name to be deleted. Default matching method is exact match (-m str).
1790 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Christopher Faulet81e20172019-12-12 16:40:30 +01001791 */
1792static enum act_parse_ret parse_http_del_header(const char **args, int *orig_arg, struct proxy *px,
1793 struct act_rule *rule, char **err)
1794{
1795 int cur_arg;
Maciej Zdebebdd4c52020-11-20 13:58:48 +00001796 int pat_idx;
Christopher Faulet81e20172019-12-12 16:40:30 +01001797
Maciej Zdebebdd4c52020-11-20 13:58:48 +00001798 /* set exact matching (-m str) as default */
1799 rule->action = PAT_MATCH_STR;
1800 rule->action_ptr = http_action_del_header;
Christopher Faulet2eb53962020-01-14 14:47:34 +01001801 rule->release_ptr = release_http_action;
Christopher Faulet81e20172019-12-12 16:40:30 +01001802
1803 cur_arg = *orig_arg;
1804 if (!*args[cur_arg]) {
Maciej Zdebebdd4c52020-11-20 13:58:48 +00001805 memprintf(err, "expects at least 1 argument");
Christopher Faulet81e20172019-12-12 16:40:30 +01001806 return ACT_RET_PRS_ERR;
1807 }
1808
Christopher Faulet96bff762019-12-17 13:46:18 +01001809 rule->arg.http.str.ptr = strdup(args[cur_arg]);
1810 rule->arg.http.str.len = strlen(rule->arg.http.str.ptr);
Christopher Faulet81e20172019-12-12 16:40:30 +01001811 px->conf.args.ctx = (rule->from == ACT_F_HTTP_REQ ? ARGC_HRQ : ARGC_HRS);
1812
Maciej Zdeb6dee9962020-11-23 16:03:09 +00001813 LIST_INIT(&rule->arg.http.fmt);
Maciej Zdebebdd4c52020-11-20 13:58:48 +00001814 if (strcmp(args[cur_arg+1], "-m") == 0) {
1815 cur_arg++;
1816 if (!*args[cur_arg+1]) {
1817 memprintf(err, "-m flag expects exactly 1 argument");
1818 return ACT_RET_PRS_ERR;
1819 }
1820
1821 cur_arg++;
1822 pat_idx = pat_find_match_name(args[cur_arg]);
1823 switch (pat_idx) {
1824 case PAT_MATCH_REG:
1825 if (!(rule->arg.http.re = regex_comp(rule->arg.http.str.ptr, 1, 1, err)))
1826 return ACT_RET_PRS_ERR;
1827 /* fall through */
1828 case PAT_MATCH_STR:
1829 case PAT_MATCH_BEG:
1830 case PAT_MATCH_END:
1831 case PAT_MATCH_SUB:
1832 rule->action = pat_idx;
1833 break;
1834 default:
1835 memprintf(err, "-m with unsupported matching method '%s'", args[cur_arg]);
1836 return ACT_RET_PRS_ERR;
1837 }
1838 }
1839
Christopher Faulet81e20172019-12-12 16:40:30 +01001840 *orig_arg = cur_arg + 1;
1841 return ACT_RET_PRS_OK;
1842}
1843
Christopher Faulet2eb53962020-01-14 14:47:34 +01001844/* Release memory allocated by an http redirect action. */
1845static void release_http_redir(struct act_rule *rule)
1846{
1847 struct logformat_node *lf, *lfb;
1848 struct redirect_rule *redir;
1849
1850 redir = rule->arg.redir;
Willy Tarreau2b718102021-04-21 07:32:39 +02001851 LIST_DELETE(&redir->list);
Christopher Faulet2eb53962020-01-14 14:47:34 +01001852 if (redir->cond) {
1853 prune_acl_cond(redir->cond);
1854 free(redir->cond);
1855 }
1856 free(redir->rdr_str);
1857 free(redir->cookie_str);
1858 list_for_each_entry_safe(lf, lfb, &redir->rdr_fmt, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001859 LIST_DELETE(&lf->list);
Christopher Faulet2eb53962020-01-14 14:47:34 +01001860 free(lf);
1861 }
1862 free(redir);
1863}
1864
Christopher Faulet81e20172019-12-12 16:40:30 +01001865/* Parse a "redirect" action. It returns ACT_RET_PRS_OK on success,
1866 * ACT_RET_PRS_ERR on error.
1867 */
1868static enum act_parse_ret parse_http_redirect(const char **args, int *orig_arg, struct proxy *px,
1869 struct act_rule *rule, char **err)
1870{
1871 struct redirect_rule *redir;
1872 int dir, cur_arg;
1873
1874 rule->action = ACT_HTTP_REDIR;
Christopher Faulet245cf792019-12-18 14:58:12 +01001875 rule->flags |= ACT_FLAG_FINAL;
Christopher Faulet2eb53962020-01-14 14:47:34 +01001876 rule->release_ptr = release_http_redir;
Christopher Faulet81e20172019-12-12 16:40:30 +01001877
1878 cur_arg = *orig_arg;
1879
1880 dir = (rule->from == ACT_F_HTTP_REQ ? 0 : 1);
1881 if ((redir = http_parse_redirect_rule(px->conf.args.file, px->conf.args.line, px, &args[cur_arg], err, 1, dir)) == NULL)
1882 return ACT_RET_PRS_ERR;
1883
1884 rule->arg.redir = redir;
1885 rule->cond = redir->cond;
1886 redir->cond = NULL;
1887
1888 /* skip all arguments */
1889 while (*args[cur_arg])
1890 cur_arg++;
1891
1892 *orig_arg = cur_arg;
1893 return ACT_RET_PRS_OK;
1894}
1895
Christopher Faulet046cf442019-12-17 15:45:23 +01001896/* This function executes a add-acl, del-acl, set-map or del-map actions. On
1897 * success, it returns ACT_RET_CONT. Otherwsize ACT_RET_ERR is returned.
1898 */
1899static enum act_return http_action_set_map(struct act_rule *rule, struct proxy *px,
1900 struct session *sess, struct stream *s, int flags)
1901{
1902 struct pat_ref *ref;
1903 struct buffer *key = NULL, *value = NULL;
1904 enum act_return ret = ACT_RET_CONT;
1905
1906 /* collect reference */
1907 ref = pat_ref_lookup(rule->arg.map.ref);
1908 if (!ref)
1909 goto leave;
1910
1911 /* allocate key */
1912 key = alloc_trash_chunk();
1913 if (!key)
1914 goto fail_alloc;
1915
1916 /* collect key */
1917 key->data = build_logline(s, key->area, key->size, &rule->arg.map.key);
1918 key->area[key->data] = '\0';
1919
1920 switch (rule->action) {
1921 case 0: // add-acl
1922 /* add entry only if it does not already exist */
1923 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
1924 if (pat_ref_find_elt(ref, key->area) == NULL)
1925 pat_ref_add(ref, key->area, NULL, NULL);
1926 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
1927 break;
1928
1929 case 1: // set-map
1930 /* allocate value */
1931 value = alloc_trash_chunk();
1932 if (!value)
1933 goto fail_alloc;
1934
1935 /* collect value */
1936 value->data = build_logline(s, value->area, value->size, &rule->arg.map.value);
1937 value->area[value->data] = '\0';
1938
1939 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
1940 if (pat_ref_find_elt(ref, key->area) != NULL) {
1941 /* update entry if it exists */
1942 pat_ref_set(ref, key->area, value->area, NULL);
1943 }
1944 else {
1945 /* insert a new entry */
1946 pat_ref_add(ref, key->area, value->area, NULL);
1947 }
1948 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
1949 break;
1950
1951 case 2: // del-acl
1952 case 3: // del-map
1953 /* returned code: 1=ok, 0=ko */
1954 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
1955 pat_ref_delete(ref, key->area);
1956 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
1957 break;
1958
1959 default:
1960 ret = ACT_RET_ERR;
1961 }
1962
1963
1964 leave:
1965 free_trash_chunk(key);
1966 free_trash_chunk(value);
1967 return ret;
1968
1969 fail_alloc:
1970 if (!(s->flags & SF_ERR_MASK))
1971 s->flags |= SF_ERR_RESOURCE;
1972 ret = ACT_RET_ERR;
1973 goto leave;
1974}
1975
Christopher Faulet2eb53962020-01-14 14:47:34 +01001976/* Release memory allocated by an http map/acl action. */
1977static void release_http_map(struct act_rule *rule)
1978{
1979 struct logformat_node *lf, *lfb;
1980
1981 free(rule->arg.map.ref);
1982 list_for_each_entry_safe(lf, lfb, &rule->arg.map.key, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001983 LIST_DELETE(&lf->list);
Christopher Faulet2eb53962020-01-14 14:47:34 +01001984 release_sample_expr(lf->expr);
1985 free(lf->arg);
1986 free(lf);
1987 }
1988 if (rule->action == 1) {
1989 list_for_each_entry_safe(lf, lfb, &rule->arg.map.value, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001990 LIST_DELETE(&lf->list);
Christopher Faulet2eb53962020-01-14 14:47:34 +01001991 release_sample_expr(lf->expr);
1992 free(lf->arg);
1993 free(lf);
1994 }
1995 }
1996}
1997
Christopher Faulet81e20172019-12-12 16:40:30 +01001998/* Parse a "add-acl", "del-acl", "set-map" or "del-map" actions. It takes one or
Christopher Faulet046cf442019-12-17 15:45:23 +01001999 * two log-format string as argument depending on the action. The action is
2000 * stored in <.action> as an int (0=add-acl, 1=set-map, 2=del-acl,
2001 * 3=del-map). It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Christopher Faulet81e20172019-12-12 16:40:30 +01002002 */
2003static enum act_parse_ret parse_http_set_map(const char **args, int *orig_arg, struct proxy *px,
2004 struct act_rule *rule, char **err)
2005{
2006 int cap, cur_arg;
2007
Christopher Faulet046cf442019-12-17 15:45:23 +01002008 if (args[*orig_arg-1][0] == 'a') // add-acl
2009 rule->action = 0;
2010 else if (args[*orig_arg-1][0] == 's') // set-map
2011 rule->action = 1;
2012 else if (args[*orig_arg-1][4] == 'a') // del-acl
2013 rule->action = 2;
2014 else if (args[*orig_arg-1][4] == 'm') // del-map
2015 rule->action = 3;
2016 else {
2017 memprintf(err, "internal error: unhandled action '%s'", args[0]);
2018 return ACT_RET_PRS_ERR;
2019 }
2020 rule->action_ptr = http_action_set_map;
Christopher Faulet2eb53962020-01-14 14:47:34 +01002021 rule->release_ptr = release_http_map;
Christopher Faulet81e20172019-12-12 16:40:30 +01002022
2023 cur_arg = *orig_arg;
Christopher Faulet046cf442019-12-17 15:45:23 +01002024 if (rule->action == 1 && (!*args[cur_arg] || !*args[cur_arg+1])) {
2025 /* 2 args for set-map */
Christopher Faulet81e20172019-12-12 16:40:30 +01002026 memprintf(err, "expects exactly 2 arguments");
2027 return ACT_RET_PRS_ERR;
2028 }
2029 else if (!*args[cur_arg]) {
Christopher Faulet046cf442019-12-17 15:45:23 +01002030 /* only one arg for other actions */
Christopher Faulet81e20172019-12-12 16:40:30 +01002031 memprintf(err, "expects exactly 1 arguments");
2032 return ACT_RET_PRS_ERR;
2033 }
2034
2035 /*
2036 * '+ 8' for 'set-map(' (same for del-map)
2037 * '- 9' for 'set-map(' + trailing ')' (same for del-map)
2038 */
2039 rule->arg.map.ref = my_strndup(args[cur_arg-1] + 8, strlen(args[cur_arg-1]) - 9);
2040
2041 if (rule->from == ACT_F_HTTP_REQ) {
2042 px->conf.args.ctx = ARGC_HRQ;
2043 cap = (px->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR;
2044 }
2045 else{
2046 px->conf.args.ctx = ARGC_HRS;
2047 cap = (px->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR;
2048 }
2049
2050 /* key pattern */
2051 LIST_INIT(&rule->arg.map.key);
Christopher Faulet1337b322020-01-14 14:50:55 +01002052 if (!parse_logformat_string(args[cur_arg], px, &rule->arg.map.key, LOG_OPT_HTTP, cap, err)) {
2053 free(rule->arg.map.ref);
Christopher Faulet81e20172019-12-12 16:40:30 +01002054 return ACT_RET_PRS_ERR;
Christopher Faulet1337b322020-01-14 14:50:55 +01002055 }
Christopher Faulet81e20172019-12-12 16:40:30 +01002056
Christopher Faulet046cf442019-12-17 15:45:23 +01002057 if (rule->action == 1) {
Christopher Faulet81e20172019-12-12 16:40:30 +01002058 /* value pattern for set-map only */
2059 cur_arg++;
2060 LIST_INIT(&rule->arg.map.value);
Christopher Faulet1337b322020-01-14 14:50:55 +01002061 if (!parse_logformat_string(args[cur_arg], px, &rule->arg.map.value, LOG_OPT_HTTP, cap, err)) {
2062 free(rule->arg.map.ref);
Christopher Faulet81e20172019-12-12 16:40:30 +01002063 return ACT_RET_PRS_ERR;
Christopher Faulet1337b322020-01-14 14:50:55 +01002064 }
Christopher Faulet81e20172019-12-12 16:40:30 +01002065 }
2066
2067 free(px->conf.lfs_file);
2068 px->conf.lfs_file = strdup(px->conf.args.file);
2069 px->conf.lfs_line = px->conf.args.line;
2070
2071 *orig_arg = cur_arg + 1;
2072 return ACT_RET_PRS_OK;
2073}
2074
Christopher Fauletac98d812019-12-18 09:20:16 +01002075/* This function executes a track-sc* actions. On success, it returns
2076 * ACT_RET_CONT. Otherwsize ACT_RET_ERR is returned.
2077 */
2078static enum act_return http_action_track_sc(struct act_rule *rule, struct proxy *px,
2079 struct session *sess, struct stream *s, int flags)
2080{
2081 struct stktable *t;
2082 struct stksess *ts;
2083 struct stktable_key *key;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002084 void *ptr1, *ptr2, *ptr3, *ptr4, *ptr5, *ptr6;
Christopher Fauletac98d812019-12-18 09:20:16 +01002085 int opt;
2086
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002087 ptr1 = ptr2 = ptr3 = ptr4 = ptr5 = ptr6 = NULL;
Christopher Fauletac98d812019-12-18 09:20:16 +01002088 opt = ((rule->from == ACT_F_HTTP_REQ) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES) | SMP_OPT_FINAL;
2089
2090 t = rule->arg.trk_ctr.table.t;
Emeric Brun362d25e2021-03-10 16:58:03 +01002091
2092 if (stkctr_entry(&s->stkctr[rule->action]))
2093 goto end;
2094
Christopher Fauletac98d812019-12-18 09:20:16 +01002095 key = stktable_fetch_key(t, s->be, sess, s, opt, rule->arg.trk_ctr.expr, NULL);
2096
2097 if (!key)
2098 goto end;
2099 ts = stktable_get_entry(t, key);
2100 if (!ts)
2101 goto end;
2102
2103 stream_track_stkctr(&s->stkctr[rule->action], t, ts);
2104
2105 /* let's count a new HTTP request as it's the first time we do it */
2106 ptr1 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
2107 ptr2 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
2108
2109 /* When the client triggers a 4xx from the server, it's most often due
2110 * to a missing object or permission. These events should be tracked
2111 * because if they happen often, it may indicate a brute force or a
2112 * vulnerability scan. Normally this is done when receiving the response
2113 * but here we're tracking after this ought to have been done so we have
2114 * to do it on purpose.
2115 */
2116 if (rule->from == ACT_F_HTTP_RES && (unsigned)(s->txn->status - 400) < 100) {
2117 ptr3 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_CNT);
2118 ptr4 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_RATE);
2119 }
2120
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002121 if (rule->from == ACT_F_HTTP_RES && (unsigned)(s->txn->status - 500) < 100 &&
2122 s->txn->status != 501 && s->txn->status != 505) {
2123 ptr5 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_FAIL_CNT);
2124 ptr6 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_FAIL_RATE);
2125 }
2126
2127 if (ptr1 || ptr2 || ptr3 || ptr4 || ptr5 || ptr6) {
Christopher Fauletac98d812019-12-18 09:20:16 +01002128 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
2129
2130 if (ptr1)
2131 stktable_data_cast(ptr1, http_req_cnt)++;
2132 if (ptr2)
2133 update_freq_ctr_period(&stktable_data_cast(ptr2, http_req_rate),
2134 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
2135 if (ptr3)
2136 stktable_data_cast(ptr3, http_err_cnt)++;
2137 if (ptr4)
2138 update_freq_ctr_period(&stktable_data_cast(ptr4, http_err_rate),
2139 t->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1);
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002140 if (ptr5)
2141 stktable_data_cast(ptr5, http_fail_cnt)++;
2142 if (ptr6)
2143 update_freq_ctr_period(&stktable_data_cast(ptr6, http_fail_rate),
2144 t->data_arg[STKTABLE_DT_HTTP_FAIL_RATE].u, 1);
Christopher Fauletac98d812019-12-18 09:20:16 +01002145
2146 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
2147
2148 /* If data was modified, we need to touch to re-schedule sync */
2149 stktable_touch_local(t, ts, 0);
2150 }
2151
2152 stkctr_set_flags(&s->stkctr[rule->action], STKCTR_TRACK_CONTENT);
2153 if (sess->fe != s->be)
2154 stkctr_set_flags(&s->stkctr[rule->action], STKCTR_TRACK_BACKEND);
2155
2156 end:
2157 return ACT_RET_CONT;
2158}
Christopher Faulet81e20172019-12-12 16:40:30 +01002159
Christopher Faulet2eb53962020-01-14 14:47:34 +01002160static void release_http_track_sc(struct act_rule *rule)
2161{
2162 release_sample_expr(rule->arg.trk_ctr.expr);
2163}
2164
Christopher Faulet81e20172019-12-12 16:40:30 +01002165/* Parse a "track-sc*" actions. It returns ACT_RET_PRS_OK on success,
2166 * ACT_RET_PRS_ERR on error.
2167 */
2168static enum act_parse_ret parse_http_track_sc(const char **args, int *orig_arg, struct proxy *px,
2169 struct act_rule *rule, char **err)
2170{
2171 struct sample_expr *expr;
2172 unsigned int where;
2173 unsigned int tsc_num;
2174 const char *tsc_num_str;
2175 int cur_arg;
2176
2177 tsc_num_str = &args[*orig_arg-1][8];
2178 if (cfg_parse_track_sc_num(&tsc_num, tsc_num_str, tsc_num_str + strlen(tsc_num_str), err) == -1)
2179 return ACT_RET_PRS_ERR;
2180
2181 cur_arg = *orig_arg;
2182 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line,
Willy Tarreaue3b57bf2020-02-14 16:50:14 +01002183 err, &px->conf.args, NULL);
Christopher Faulet81e20172019-12-12 16:40:30 +01002184 if (!expr)
2185 return ACT_RET_PRS_ERR;
2186
2187 where = 0;
2188 if (px->cap & PR_CAP_FE)
2189 where |= (rule->from == ACT_F_HTTP_REQ ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_FE_HRS_HDR);
2190 if (px->cap & PR_CAP_BE)
2191 where |= (rule->from == ACT_F_HTTP_REQ ? SMP_VAL_BE_HRQ_HDR : SMP_VAL_BE_HRS_HDR);
2192
2193 if (!(expr->fetch->val & where)) {
2194 memprintf(err, "fetch method '%s' extracts information from '%s', none of which is available here",
2195 args[cur_arg-1], sample_src_names(expr->fetch->use));
Christopher Faulet1337b322020-01-14 14:50:55 +01002196 release_sample_expr(expr);
Christopher Faulet81e20172019-12-12 16:40:30 +01002197 return ACT_RET_PRS_ERR;
2198 }
2199
2200 if (strcmp(args[cur_arg], "table") == 0) {
2201 cur_arg++;
2202 if (!*args[cur_arg]) {
2203 memprintf(err, "missing table name");
Christopher Faulet1337b322020-01-14 14:50:55 +01002204 release_sample_expr(expr);
Christopher Faulet81e20172019-12-12 16:40:30 +01002205 return ACT_RET_PRS_ERR;
2206 }
2207
2208 /* we copy the table name for now, it will be resolved later */
2209 rule->arg.trk_ctr.table.n = strdup(args[cur_arg]);
2210 cur_arg++;
2211 }
2212
Christopher Fauletac98d812019-12-18 09:20:16 +01002213 rule->action = tsc_num;
Christopher Faulet81e20172019-12-12 16:40:30 +01002214 rule->arg.trk_ctr.expr = expr;
Christopher Fauletac98d812019-12-18 09:20:16 +01002215 rule->action_ptr = http_action_track_sc;
Christopher Faulet2eb53962020-01-14 14:47:34 +01002216 rule->release_ptr = release_http_track_sc;
Christopher Faulet81e20172019-12-12 16:40:30 +01002217 rule->check_ptr = check_trk_action;
2218
2219 *orig_arg = cur_arg;
2220 return ACT_RET_PRS_OK;
2221}
2222
Amaury Denoyelle8d228232020-12-10 13:43:54 +01002223static enum act_return action_timeout_set_stream_timeout(struct act_rule *rule,
2224 struct proxy *px,
2225 struct session *sess,
2226 struct stream *s,
2227 int flags)
2228{
2229 struct sample *key;
2230
2231 if (rule->arg.timeout.expr) {
2232 key = sample_fetch_as_type(px, sess, s, SMP_OPT_FINAL, rule->arg.timeout.expr, SMP_T_SINT);
2233 if (!key)
2234 return ACT_RET_CONT;
2235
2236 stream_set_timeout(s, rule->arg.timeout.type, MS_TO_TICKS(key->data.u.sint));
2237 }
2238 else {
2239 stream_set_timeout(s, rule->arg.timeout.type, MS_TO_TICKS(rule->arg.timeout.value));
2240 }
2241
2242 return ACT_RET_CONT;
2243}
2244
2245/* Parse a "set-timeout" action. Returns ACT_RET_PRS_ERR if parsing error.
2246 */
2247static enum act_parse_ret parse_http_set_timeout(const char **args,
2248 int *orig_arg,
2249 struct proxy *px,
2250 struct act_rule *rule, char **err)
2251{
2252 int cur_arg;
2253
2254 rule->action = ACT_CUSTOM;
2255 rule->action_ptr = action_timeout_set_stream_timeout;
2256 rule->release_ptr = release_timeout_action;
2257
2258 cur_arg = *orig_arg;
2259 if (!*args[cur_arg] || !*args[cur_arg + 1]) {
2260 memprintf(err, "expects exactly 2 arguments");
2261 return ACT_RET_PRS_ERR;
2262 }
2263
2264 if (!(px->cap & PR_CAP_BE)) {
2265 memprintf(err, "proxy '%s' has no backend capability", px->id);
2266 return ACT_RET_PRS_ERR;
2267 }
2268
2269 if (cfg_parse_rule_set_timeout(args, cur_arg,
2270 &rule->arg.timeout.value,
2271 &rule->arg.timeout.type,
2272 &rule->arg.timeout.expr,
2273 err,
2274 px->conf.args.file,
2275 px->conf.args.line, &px->conf.args) == -1) {
2276 return ACT_RET_PRS_ERR;
2277 }
2278
2279 *orig_arg = cur_arg + 2;
2280
2281 return ACT_RET_PRS_OK;
2282}
2283
Christopher Faulet46f95542019-12-20 10:07:22 +01002284/* This function executes a strict-mode actions. On success, it always returns
2285 * ACT_RET_CONT
2286 */
2287static enum act_return http_action_strict_mode(struct act_rule *rule, struct proxy *px,
2288 struct session *sess, struct stream *s, int flags)
2289{
2290 struct http_msg *msg = ((rule->from == ACT_F_HTTP_REQ) ? &s->txn->req : &s->txn->rsp);
2291
2292 if (rule->action == 0) // strict-mode on
2293 msg->flags &= ~HTTP_MSGF_SOFT_RW;
2294 else // strict-mode off
2295 msg->flags |= HTTP_MSGF_SOFT_RW;
2296 return ACT_RET_CONT;
2297}
2298
2299/* Parse a "strict-mode" action. It returns ACT_RET_PRS_OK on success,
2300 * ACT_RET_PRS_ERR on error.
2301 */
2302static enum act_parse_ret parse_http_strict_mode(const char **args, int *orig_arg, struct proxy *px,
2303 struct act_rule *rule, char **err)
2304{
2305 int cur_arg;
2306
Christopher Faulet46f95542019-12-20 10:07:22 +01002307 cur_arg = *orig_arg;
2308 if (!*args[cur_arg]) {
2309 memprintf(err, "expects exactly 1 arguments");
2310 return ACT_RET_PRS_ERR;
2311 }
2312
2313 if (strcasecmp(args[cur_arg], "on") == 0)
2314 rule->action = 0; // strict-mode on
2315 else if (strcasecmp(args[cur_arg], "off") == 0)
2316 rule->action = 1; // strict-mode off
2317 else {
2318 memprintf(err, "Unexpected value '%s'. Only 'on' and 'off' are supported", args[cur_arg]);
2319 return ACT_RET_PRS_ERR;
2320 }
2321 rule->action_ptr = http_action_strict_mode;
2322
2323 *orig_arg = cur_arg + 1;
2324 return ACT_RET_PRS_OK;
2325}
2326
Christopher Faulet24231ab2020-01-24 17:44:23 +01002327/* This function executes a return action. It builds an HTX message from an
2328 * errorfile, an raw file or a log-format string, depending on <.action>
2329 * value. On success, it returns ACT_RET_ABRT. If an error occurs ACT_RET_ERR is
2330 * returned.
2331 */
2332static enum act_return http_action_return(struct act_rule *rule, struct proxy *px,
2333 struct session *sess, struct stream *s, int flags)
2334{
2335 struct channel *req = &s->req;
Christopher Faulet24231ab2020-01-24 17:44:23 +01002336
Christopher Faulet2d36df22021-02-19 11:41:01 +01002337 s->txn->status = rule->arg.http_reply->status;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02002338 if (http_reply_message(s, rule->arg.http_reply) == -1)
2339 return ACT_RET_ERR;
Christopher Faulet24231ab2020-01-24 17:44:23 +01002340
Christopher Faulet24231ab2020-01-24 17:44:23 +01002341 if (rule->from == ACT_F_HTTP_REQ) {
2342 /* let's log the request time */
2343 s->logs.tv_request = now;
2344 req->analysers &= AN_REQ_FLT_END;
2345
2346 if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Willy Tarreau4781b152021-04-06 13:53:36 +02002347 _HA_ATOMIC_INC(&s->sess->fe->fe_counters.intercepted_req);
Christopher Faulet24231ab2020-01-24 17:44:23 +01002348 }
2349
2350 if (!(s->flags & SF_ERR_MASK))
2351 s->flags |= SF_ERR_LOCAL;
2352 if (!(s->flags & SF_FINST_MASK))
2353 s->flags |= ((rule->from == ACT_F_HTTP_REQ) ? SF_FINST_R : SF_FINST_H);
2354
Christopher Faulet0e2ad612020-05-13 16:38:37 +02002355 return ACT_RET_ABRT;
Christopher Faulet24231ab2020-01-24 17:44:23 +01002356}
2357
Christopher Faulet24231ab2020-01-24 17:44:23 +01002358/* Parse a "return" action. It returns ACT_RET_PRS_OK on success,
Christopher Faulet47e791e2020-05-13 14:36:55 +02002359 * ACT_RET_PRS_ERR on error. It relies on http_parse_http_reply() to set
2360 * <.arg.http_reply>.
Christopher Faulet24231ab2020-01-24 17:44:23 +01002361 */
2362static enum act_parse_ret parse_http_return(const char **args, int *orig_arg, struct proxy *px,
2363 struct act_rule *rule, char **err)
2364{
Christopher Faulet47e791e2020-05-13 14:36:55 +02002365 /* Prepare parsing of log-format strings */
2366 px->conf.args.ctx = ((rule->from == ACT_F_HTTP_REQ) ? ARGC_HRQ : ARGC_HRS);
2367 rule->arg.http_reply = http_parse_http_reply(args, orig_arg, px, 200, err);
2368 if (!rule->arg.http_reply)
2369 return ACT_RET_PRS_ERR;
Christopher Faulet24231ab2020-01-24 17:44:23 +01002370
Christopher Fauletba946bf2020-05-13 08:50:07 +02002371 rule->flags |= ACT_FLAG_FINAL;
Christopher Faulet5ff0c642020-05-12 18:33:37 +02002372 rule->action = ACT_CUSTOM;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002373 rule->check_ptr = check_act_http_reply;
Christopher Faulet24231ab2020-01-24 17:44:23 +01002374 rule->action_ptr = http_action_return;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002375 rule->release_ptr = release_act_http_reply;
Christopher Faulet24231ab2020-01-24 17:44:23 +01002376 return ACT_RET_PRS_OK;
Christopher Faulet24231ab2020-01-24 17:44:23 +01002377}
2378
Christopher Faulet021a8e42021-03-29 10:46:38 +02002379
2380
2381/* This function executes a wait-for-body action. It waits for the message
2382 * payload for a max configured time (.arg.p[0]) and eventually for only first
2383 * <arg.p[1]> bytes (0 means no limit). It relies on http_wait_for_msg_body()
2384 * function. it returns ACT_RET_CONT when conditions are met to stop to wait.
2385 * Otherwise ACT_RET_YIELD is returned to wait for more data. ACT_RET_INV is
2386 * returned if a parsing error is raised by lower level and ACT_RET_ERR if an
Ilya Shipitsinb2be9a12021-04-24 13:25:42 +05002387 * internal error occurred. Finally ACT_RET_ABRT is returned when a timeout
2388 * occurred.
Christopher Faulet021a8e42021-03-29 10:46:38 +02002389 */
2390static enum act_return http_action_wait_for_body(struct act_rule *rule, struct proxy *px,
2391 struct session *sess, struct stream *s, int flags)
2392{
2393 struct channel *chn = ((rule->from == ACT_F_HTTP_REQ) ? &s->req : &s->res);
2394 unsigned int time = (uintptr_t)rule->arg.act.p[0];
2395 unsigned int bytes = (uintptr_t)rule->arg.act.p[1];
2396
2397 switch (http_wait_for_msg_body(s, chn, time, bytes)) {
2398 case HTTP_RULE_RES_CONT:
2399 return ACT_RET_CONT;
2400 case HTTP_RULE_RES_YIELD:
2401 return ACT_RET_YIELD;
2402 case HTTP_RULE_RES_BADREQ:
2403 return ACT_RET_INV;
2404 case HTTP_RULE_RES_ERROR:
2405 return ACT_RET_ERR;
2406 case HTTP_RULE_RES_ABRT:
2407 return ACT_RET_ABRT;
2408 default:
2409 return ACT_RET_ERR;
2410 }
2411}
2412
2413/* Parse a "wait-for-body" action. It returns ACT_RET_PRS_OK on success,
2414 * ACT_RET_PRS_ERR on error.
2415 */
2416static enum act_parse_ret parse_http_wait_for_body(const char **args, int *orig_arg, struct proxy *px,
2417 struct act_rule *rule, char **err)
2418{
2419 int cur_arg;
2420 unsigned int time, bytes;
2421 const char *res;
2422
2423 cur_arg = *orig_arg;
2424 if (!*args[cur_arg]) {
2425 memprintf(err, "expects time <time> [ at-least <bytes> ]");
2426 return ACT_RET_PRS_ERR;
2427 }
2428
2429 time = UINT_MAX; /* To be sure it is set */
2430 bytes = 0; /* Default value, wait all the body */
2431 while (*(args[cur_arg])) {
2432 if (strcmp(args[cur_arg], "time") == 0) {
2433 if (!*args[cur_arg + 1]) {
2434 memprintf(err, "missing argument for '%s'", args[cur_arg]);
2435 return ACT_RET_PRS_ERR;
2436 }
2437 res = parse_time_err(args[cur_arg+1], &time, TIME_UNIT_MS);
2438 if (res == PARSE_TIME_OVER) {
2439 memprintf(err, "time overflow (maximum value is 2147483647 ms or ~24.8 days)");
2440 return ACT_RET_PRS_ERR;
2441 }
2442 if (res == PARSE_TIME_UNDER) {
2443 memprintf(err, "time underflow (minimum non-null value is 1 ms)");
2444 return ACT_RET_PRS_ERR;
2445 }
2446 if (res) {
2447 memprintf(err, "unexpected character '%c'", *res);
2448 return ACT_RET_PRS_ERR;
2449 }
2450 cur_arg++;
2451 }
2452 else if (strcmp(args[cur_arg], "at-least") == 0) {
2453 if (!*args[cur_arg + 1]) {
2454 memprintf(err, "missing argument for '%s'", args[cur_arg]);
2455 return ACT_RET_PRS_ERR;
2456 }
2457 res = parse_size_err(args[cur_arg+1], &bytes);
2458 if (res) {
2459 memprintf(err, "unexpected character '%c'", *res);
2460 return ACT_RET_PRS_ERR;
2461 }
2462 cur_arg++;
2463 }
2464 else
2465 break;
2466 cur_arg++;
2467 }
2468
2469 if (time == UINT_MAX) {
2470 memprintf(err, "expects time <time> [ at-least <bytes> ]");
2471 return ACT_RET_PRS_ERR;
2472 }
2473
2474 rule->arg.act.p[0] = (void *)(uintptr_t)time;
2475 rule->arg.act.p[1] = (void *)(uintptr_t)bytes;
2476
2477 *orig_arg = cur_arg;
2478
2479 rule->action = ACT_CUSTOM;
2480 rule->action_ptr = http_action_wait_for_body;
2481 return ACT_RET_PRS_OK;
2482}
2483
Willy Tarreau79e57332018-10-02 16:01:16 +02002484/************************************************************************/
2485/* All supported http-request action keywords must be declared here. */
2486/************************************************************************/
2487
2488static struct action_kw_list http_req_actions = {
2489 .kw = {
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02002490 { "add-acl", parse_http_set_map, KWF_MATCH_PREFIX },
Christopher Faulet81e20172019-12-12 16:40:30 +01002491 { "add-header", parse_http_set_header, 0 },
2492 { "allow", parse_http_allow, 0 },
2493 { "auth", parse_http_auth, 0 },
2494 { "capture", parse_http_req_capture, 0 },
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02002495 { "del-acl", parse_http_set_map, KWF_MATCH_PREFIX },
Christopher Faulet81e20172019-12-12 16:40:30 +01002496 { "del-header", parse_http_del_header, 0 },
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02002497 { "del-map", parse_http_set_map, KWF_MATCH_PREFIX },
Christopher Faulete0fca292020-01-13 21:49:03 +01002498 { "deny", parse_http_deny, 0 },
Christopher Faulet81e20172019-12-12 16:40:30 +01002499 { "disable-l7-retry", parse_http_req_disable_l7_retry, 0 },
2500 { "early-hint", parse_http_set_header, 0 },
Amaury Denoyellea9e639a2021-05-06 15:50:12 +02002501 { "normalize-uri", parse_http_normalize_uri, KWF_EXPERIMENTAL },
Christopher Faulet81e20172019-12-12 16:40:30 +01002502 { "redirect", parse_http_redirect, 0 },
2503 { "reject", parse_http_action_reject, 0 },
2504 { "replace-header", parse_http_replace_header, 0 },
2505 { "replace-path", parse_replace_uri, 0 },
Christopher Faulet312294f2020-09-02 17:17:44 +02002506 { "replace-pathq", parse_replace_uri, 0 },
Christopher Faulet81e20172019-12-12 16:40:30 +01002507 { "replace-uri", parse_replace_uri, 0 },
2508 { "replace-value", parse_http_replace_header, 0 },
Christopher Faulet24231ab2020-01-24 17:44:23 +01002509 { "return", parse_http_return, 0 },
Christopher Faulet81e20172019-12-12 16:40:30 +01002510 { "set-header", parse_http_set_header, 0 },
2511 { "set-log-level", parse_http_set_log_level, 0 },
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02002512 { "set-map", parse_http_set_map, KWF_MATCH_PREFIX },
Christopher Faulet81e20172019-12-12 16:40:30 +01002513 { "set-method", parse_set_req_line, 0 },
2514 { "set-mark", parse_http_set_mark, 0 },
2515 { "set-nice", parse_http_set_nice, 0 },
2516 { "set-path", parse_set_req_line, 0 },
Christopher Faulet312294f2020-09-02 17:17:44 +02002517 { "set-pathq", parse_set_req_line, 0 },
Christopher Faulet81e20172019-12-12 16:40:30 +01002518 { "set-query", parse_set_req_line, 0 },
2519 { "set-tos", parse_http_set_tos, 0 },
2520 { "set-uri", parse_set_req_line, 0 },
Christopher Faulet46f95542019-12-20 10:07:22 +01002521 { "strict-mode", parse_http_strict_mode, 0 },
Christopher Faulete0fca292020-01-13 21:49:03 +01002522 { "tarpit", parse_http_deny, 0 },
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02002523 { "track-sc", parse_http_track_sc, KWF_MATCH_PREFIX },
Amaury Denoyelle8d228232020-12-10 13:43:54 +01002524 { "set-timeout", parse_http_set_timeout, 0 },
Christopher Faulet021a8e42021-03-29 10:46:38 +02002525 { "wait-for-body", parse_http_wait_for_body, 0 },
Willy Tarreau79e57332018-10-02 16:01:16 +02002526 { NULL, NULL }
2527 }
2528};
2529
Willy Tarreau0108d902018-11-25 19:14:37 +01002530INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_actions);
2531
Willy Tarreau79e57332018-10-02 16:01:16 +02002532static struct action_kw_list http_res_actions = {
2533 .kw = {
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02002534 { "add-acl", parse_http_set_map, KWF_MATCH_PREFIX },
Christopher Faulet81e20172019-12-12 16:40:30 +01002535 { "add-header", parse_http_set_header, 0 },
2536 { "allow", parse_http_allow, 0 },
2537 { "capture", parse_http_res_capture, 0 },
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02002538 { "del-acl", parse_http_set_map, KWF_MATCH_PREFIX },
Christopher Faulet81e20172019-12-12 16:40:30 +01002539 { "del-header", parse_http_del_header, 0 },
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02002540 { "del-map", parse_http_set_map, KWF_MATCH_PREFIX },
Christopher Faulete0fca292020-01-13 21:49:03 +01002541 { "deny", parse_http_deny, 0 },
Christopher Faulet81e20172019-12-12 16:40:30 +01002542 { "redirect", parse_http_redirect, 0 },
2543 { "replace-header", parse_http_replace_header, 0 },
2544 { "replace-value", parse_http_replace_header, 0 },
Christopher Faulet24231ab2020-01-24 17:44:23 +01002545 { "return", parse_http_return, 0 },
Christopher Faulet81e20172019-12-12 16:40:30 +01002546 { "set-header", parse_http_set_header, 0 },
2547 { "set-log-level", parse_http_set_log_level, 0 },
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02002548 { "set-map", parse_http_set_map, KWF_MATCH_PREFIX },
Christopher Faulet81e20172019-12-12 16:40:30 +01002549 { "set-mark", parse_http_set_mark, 0 },
2550 { "set-nice", parse_http_set_nice, 0 },
2551 { "set-status", parse_http_set_status, 0 },
2552 { "set-tos", parse_http_set_tos, 0 },
Christopher Faulet46f95542019-12-20 10:07:22 +01002553 { "strict-mode", parse_http_strict_mode, 0 },
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02002554 { "track-sc", parse_http_track_sc, KWF_MATCH_PREFIX },
Christopher Faulet021a8e42021-03-29 10:46:38 +02002555 { "wait-for-body", parse_http_wait_for_body, 0 },
Willy Tarreau79e57332018-10-02 16:01:16 +02002556 { NULL, NULL }
2557 }
2558};
2559
Willy Tarreau0108d902018-11-25 19:14:37 +01002560INITCALL1(STG_REGISTER, http_res_keywords_register, &http_res_actions);
Willy Tarreau79e57332018-10-02 16:01:16 +02002561
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002562static struct action_kw_list http_after_res_actions = {
2563 .kw = {
2564 { "add-header", parse_http_set_header, 0 },
2565 { "allow", parse_http_allow, 0 },
2566 { "del-header", parse_http_del_header, 0 },
2567 { "replace-header", parse_http_replace_header, 0 },
2568 { "replace-value", parse_http_replace_header, 0 },
2569 { "set-header", parse_http_set_header, 0 },
2570 { "set-status", parse_http_set_status, 0 },
2571 { "strict-mode", parse_http_strict_mode, 0 },
2572 { NULL, NULL }
2573 }
2574};
2575
2576INITCALL1(STG_REGISTER, http_after_res_keywords_register, &http_after_res_actions);
2577
Willy Tarreau79e57332018-10-02 16:01:16 +02002578/*
2579 * Local variables:
2580 * c-indent-level: 8
2581 * c-basic-offset: 8
2582 * End:
2583 */