blob: c2fee04d644b02baeb5f896578956df30fb737c2 [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: {
Amaury Denoyellec453f952021-07-06 11:40:12 +0200219 struct http_uri_parser parser = http_uri_parser_init(uri);
220 const struct ist path = http_parse_path(&parser);
Tim Duesterhusd371e992021-04-15 21:45:58 +0200221 struct ist newpath = ist2(replace->area, replace->size);
222
223 if (!isttest(path))
224 goto leave;
225
226 err = uri_normalizer_path_merge_slashes(iststop(path, '?'), &newpath);
227
228 if (err != URI_NORMALIZER_ERR_NONE)
229 break;
230
231 if (!http_replace_req_path(htx, newpath, 0))
232 goto fail_rewrite;
233
234 break;
235 }
Maximilian Maderff3bb8b2021-04-21 00:22:50 +0200236 case ACT_NORMALIZE_URI_PATH_STRIP_DOT: {
Amaury Denoyellec453f952021-07-06 11:40:12 +0200237 struct http_uri_parser parser = http_uri_parser_init(uri);
238 const struct ist path = http_parse_path(&parser);
Maximilian Maderff3bb8b2021-04-21 00:22:50 +0200239 struct ist newpath = ist2(replace->area, replace->size);
240
241 if (!isttest(path))
242 goto leave;
243
244 err = uri_normalizer_path_dot(iststop(path, '?'), &newpath);
245
246 if (err != URI_NORMALIZER_ERR_NONE)
247 break;
248
249 if (!http_replace_req_path(htx, newpath, 0))
250 goto fail_rewrite;
251
252 break;
253 }
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200254 case ACT_NORMALIZE_URI_PATH_STRIP_DOTDOT:
255 case ACT_NORMALIZE_URI_PATH_STRIP_DOTDOT_FULL: {
Amaury Denoyellec453f952021-07-06 11:40:12 +0200256 struct http_uri_parser parser = http_uri_parser_init(uri);
257 const struct ist path = http_parse_path(&parser);
Tim Duesterhus9982fc22021-04-15 21:45:59 +0200258 struct ist newpath = ist2(replace->area, replace->size);
259
260 if (!isttest(path))
261 goto leave;
262
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200263 err = uri_normalizer_path_dotdot(iststop(path, '?'), rule->action == ACT_NORMALIZE_URI_PATH_STRIP_DOTDOT_FULL, &newpath);
Tim Duesterhus9982fc22021-04-15 21:45:59 +0200264
265 if (err != URI_NORMALIZER_ERR_NONE)
266 break;
267
268 if (!http_replace_req_path(htx, newpath, 0))
269 goto fail_rewrite;
270
271 break;
272 }
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200273 case ACT_NORMALIZE_URI_QUERY_SORT_BY_NAME: {
Amaury Denoyellec453f952021-07-06 11:40:12 +0200274 struct http_uri_parser parser = http_uri_parser_init(uri);
275 const struct ist path = http_parse_path(&parser);
Tim Duesterhusd7b89be2021-04-15 21:46:01 +0200276 struct ist newquery = ist2(replace->area, replace->size);
277
278 if (!isttest(path))
279 goto leave;
280
281 err = uri_normalizer_query_sort(istfind(path, '?'), '&', &newquery);
282
283 if (err != URI_NORMALIZER_ERR_NONE)
284 break;
285
286 if (!http_replace_req_query(htx, newquery))
287 goto fail_rewrite;
288
289 break;
290 }
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200291 case ACT_NORMALIZE_URI_PERCENT_TO_UPPERCASE:
292 case ACT_NORMALIZE_URI_PERCENT_TO_UPPERCASE_STRICT: {
Amaury Denoyellec453f952021-07-06 11:40:12 +0200293 struct http_uri_parser parser = http_uri_parser_init(uri);
294 const struct ist path = http_parse_path(&parser);
Tim Duesterhusa4071932021-04-15 21:46:02 +0200295 struct ist newpath = ist2(replace->area, replace->size);
296
297 if (!isttest(path))
298 goto leave;
299
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200300 err = uri_normalizer_percent_upper(path, rule->action == ACT_NORMALIZE_URI_PERCENT_TO_UPPERCASE_STRICT, &newpath);
Tim Duesterhusa4071932021-04-15 21:46:02 +0200301
302 if (err != URI_NORMALIZER_ERR_NONE)
303 break;
304
305 if (!http_replace_req_path(htx, newpath, 1))
306 goto fail_rewrite;
307
308 break;
309 }
Tim Duesterhus2e4a18e2021-04-21 21:20:36 +0200310 case ACT_NORMALIZE_URI_PERCENT_DECODE_UNRESERVED:
311 case ACT_NORMALIZE_URI_PERCENT_DECODE_UNRESERVED_STRICT: {
Amaury Denoyellec453f952021-07-06 11:40:12 +0200312 struct http_uri_parser parser = http_uri_parser_init(uri);
313 const struct ist path = http_parse_path(&parser);
Tim Duesterhus2e4a18e2021-04-21 21:20:36 +0200314 struct ist newpath = ist2(replace->area, replace->size);
315
316 if (!isttest(path))
317 goto leave;
318
319 err = uri_normalizer_percent_decode_unreserved(path, rule->action == ACT_NORMALIZE_URI_PERCENT_DECODE_UNRESERVED_STRICT, &newpath);
320
321 if (err != URI_NORMALIZER_ERR_NONE)
322 break;
323
324 if (!http_replace_req_path(htx, newpath, 1))
325 goto fail_rewrite;
326
327 break;
328 }
Tim Duesterhusc9e05ab2021-05-10 17:28:25 +0200329 case ACT_NORMALIZE_URI_FRAGMENT_STRIP: {
Amaury Denoyellec453f952021-07-06 11:40:12 +0200330 struct http_uri_parser parser = http_uri_parser_init(uri);
331 const struct ist path = http_parse_path(&parser);
Tim Duesterhusc9e05ab2021-05-10 17:28:25 +0200332 struct ist newpath = ist2(replace->area, replace->size);
333
334 if (!isttest(path))
335 goto leave;
336
337 err = uri_normalizer_fragment_strip(path, &newpath);
338
339 if (err != URI_NORMALIZER_ERR_NONE)
340 break;
341
342 if (!http_replace_req_path(htx, newpath, 1))
343 goto fail_rewrite;
344
345 break;
346 }
Tim Duesterhusdec1c362021-05-10 17:28:26 +0200347 case ACT_NORMALIZE_URI_FRAGMENT_ENCODE: {
Amaury Denoyellec453f952021-07-06 11:40:12 +0200348 struct http_uri_parser parser = http_uri_parser_init(uri);
349 const struct ist path = http_parse_path(&parser);
Tim Duesterhusdec1c362021-05-10 17:28:26 +0200350 struct ist newpath = ist2(replace->area, replace->size);
351
352 if (!isttest(path))
353 goto leave;
354
355 err = uri_normalizer_fragment_encode(path, &newpath);
356
357 if (err != URI_NORMALIZER_ERR_NONE)
358 break;
359
360 if (!http_replace_req_path(htx, newpath, 1))
361 goto fail_rewrite;
362
363 break;
364 }
Tim Duesterhusd2bedcc2021-04-15 21:45:57 +0200365 }
366
367 switch (err) {
368 case URI_NORMALIZER_ERR_NONE:
369 break;
370 case URI_NORMALIZER_ERR_INTERNAL_ERROR:
371 ret = ACT_RET_ERR;
372 break;
373 case URI_NORMALIZER_ERR_INVALID_INPUT:
374 ret = ACT_RET_INV;
375 break;
376 case URI_NORMALIZER_ERR_ALLOC:
377 goto fail_alloc;
378 }
379
380 leave:
381 free_trash_chunk(replace);
382 return ret;
383
384 fail_alloc:
385 if (!(s->flags & SF_ERR_MASK))
386 s->flags |= SF_ERR_RESOURCE;
387 ret = ACT_RET_ERR;
388 goto leave;
389
390 fail_rewrite:
391 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_rewrites, 1);
392 if (s->flags & SF_BE_ASSIGNED)
393 _HA_ATOMIC_ADD(&s->be->be_counters.failed_rewrites, 1);
394 if (sess->listener && sess->listener->counters)
395 _HA_ATOMIC_ADD(&sess->listener->counters->failed_rewrites, 1);
396 if (objt_server(s->target))
397 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_rewrites, 1);
398
399 if (!(s->txn->req.flags & HTTP_MSGF_SOFT_RW)) {
400 ret = ACT_RET_ERR;
401 if (!(s->flags & SF_ERR_MASK))
402 s->flags |= SF_ERR_PRXCOND;
403 }
404 goto leave;
405}
406
407/* Parses the http-request normalize-uri action. It expects a single <normalizer>
408 * argument, corresponding too a value in `enum act_normalize_uri`.
409 *
410 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
411 */
412static enum act_parse_ret parse_http_normalize_uri(const char **args, int *orig_arg, struct proxy *px,
413 struct act_rule *rule, char **err)
414{
415 int cur_arg = *orig_arg;
416
417 rule->action_ptr = http_action_normalize_uri;
418 rule->release_ptr = NULL;
419
420 if (!*args[cur_arg]) {
421 memprintf(err, "missing argument <normalizer>");
422 return ACT_RET_PRS_ERR;
423 }
424
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200425 if (strcmp(args[cur_arg], "path-merge-slashes") == 0) {
Tim Duesterhusd371e992021-04-15 21:45:58 +0200426 cur_arg++;
Tim Duesterhusd2bedcc2021-04-15 21:45:57 +0200427
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200428 rule->action = ACT_NORMALIZE_URI_PATH_MERGE_SLASHES;
Tim Duesterhusd2bedcc2021-04-15 21:45:57 +0200429 }
Maximilian Maderff3bb8b2021-04-21 00:22:50 +0200430 else if (strcmp(args[cur_arg], "path-strip-dot") == 0) {
431 cur_arg++;
432
433 rule->action = ACT_NORMALIZE_URI_PATH_STRIP_DOT;
434 }
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200435 else if (strcmp(args[cur_arg], "path-strip-dotdot") == 0) {
Tim Duesterhus9982fc22021-04-15 21:45:59 +0200436 cur_arg++;
437
Tim Duesterhus560e1a62021-04-15 21:46:00 +0200438 if (strcmp(args[cur_arg], "full") == 0) {
439 cur_arg++;
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200440 rule->action = ACT_NORMALIZE_URI_PATH_STRIP_DOTDOT_FULL;
Tim Duesterhus560e1a62021-04-15 21:46:00 +0200441 }
442 else if (!*args[cur_arg]) {
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200443 rule->action = ACT_NORMALIZE_URI_PATH_STRIP_DOTDOT;
Tim Duesterhus560e1a62021-04-15 21:46:00 +0200444 }
445 else if (strcmp(args[cur_arg], "if") != 0 && strcmp(args[cur_arg], "unless") != 0) {
Tim Duesterhus2f413132021-05-10 23:21:20 +0200446 memprintf(err, "unknown argument '%s' for 'path-strip-dotdot' normalizer", args[cur_arg]);
Tim Duesterhus560e1a62021-04-15 21:46:00 +0200447 return ACT_RET_PRS_ERR;
448 }
Tim Duesterhus9982fc22021-04-15 21:45:59 +0200449 }
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200450 else if (strcmp(args[cur_arg], "query-sort-by-name") == 0) {
Tim Duesterhusd7b89be2021-04-15 21:46:01 +0200451 cur_arg++;
452
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200453 rule->action = ACT_NORMALIZE_URI_QUERY_SORT_BY_NAME;
Tim Duesterhusd7b89be2021-04-15 21:46:01 +0200454 }
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200455 else if (strcmp(args[cur_arg], "percent-to-uppercase") == 0) {
Tim Duesterhusa4071932021-04-15 21:46:02 +0200456 cur_arg++;
457
458 if (strcmp(args[cur_arg], "strict") == 0) {
459 cur_arg++;
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200460 rule->action = ACT_NORMALIZE_URI_PERCENT_TO_UPPERCASE_STRICT;
Tim Duesterhusa4071932021-04-15 21:46:02 +0200461 }
462 else if (!*args[cur_arg]) {
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200463 rule->action = ACT_NORMALIZE_URI_PERCENT_TO_UPPERCASE;
Tim Duesterhusa4071932021-04-15 21:46:02 +0200464 }
465 else if (strcmp(args[cur_arg], "if") != 0 && strcmp(args[cur_arg], "unless") != 0) {
Tim Duesterhus2f413132021-05-10 23:21:20 +0200466 memprintf(err, "unknown argument '%s' for 'percent-to-uppercase' normalizer", args[cur_arg]);
Tim Duesterhusa4071932021-04-15 21:46:02 +0200467 return ACT_RET_PRS_ERR;
468 }
469 }
Tim Duesterhus2e4a18e2021-04-21 21:20:36 +0200470 else if (strcmp(args[cur_arg], "percent-decode-unreserved") == 0) {
471 cur_arg++;
472
473 if (strcmp(args[cur_arg], "strict") == 0) {
474 cur_arg++;
475 rule->action = ACT_NORMALIZE_URI_PERCENT_DECODE_UNRESERVED_STRICT;
476 }
477 else if (!*args[cur_arg]) {
478 rule->action = ACT_NORMALIZE_URI_PERCENT_DECODE_UNRESERVED;
479 }
480 else if (strcmp(args[cur_arg], "if") != 0 && strcmp(args[cur_arg], "unless") != 0) {
481 memprintf(err, "unknown argument '%s' for 'percent-decode-unreserved' normalizer", args[cur_arg]);
482 return ACT_RET_PRS_ERR;
483 }
484 }
Tim Duesterhusc9e05ab2021-05-10 17:28:25 +0200485 else if (strcmp(args[cur_arg], "fragment-strip") == 0) {
486 cur_arg++;
487
488 rule->action = ACT_NORMALIZE_URI_FRAGMENT_STRIP;
489 }
Tim Duesterhusdec1c362021-05-10 17:28:26 +0200490 else if (strcmp(args[cur_arg], "fragment-encode") == 0) {
491 cur_arg++;
492
493 rule->action = ACT_NORMALIZE_URI_FRAGMENT_ENCODE;
494 }
Tim Duesterhusd2bedcc2021-04-15 21:45:57 +0200495 else {
496 memprintf(err, "unknown normalizer '%s'", args[cur_arg]);
497 return ACT_RET_PRS_ERR;
498 }
499
500 *orig_arg = cur_arg;
501 return ACT_RET_PRS_OK;
502}
503
Willy Tarreau33810222019-06-12 17:44:02 +0200504/* This function executes a replace-uri action. It finds its arguments in
Christopher Faulet96bff762019-12-17 13:46:18 +0100505 * <rule>.arg.http. It builds a string in the trash from the format string
Willy Tarreau33810222019-06-12 17:44:02 +0200506 * previously filled by function parse_replace_uri() and will execute the regex
Christopher Faulet96bff762019-12-17 13:46:18 +0100507 * in <http.re> to replace the URI. It uses the format string present in
Christopher Faulet2c22a692019-12-18 15:39:56 +0100508 * <http.fmt>. The component to act on (path/uri) is taken from <.action> which
Christopher Faulet96bff762019-12-17 13:46:18 +0100509 * contains 1 for the path or 3 for the URI (values used by
510 * http_req_replace_stline()). On success, it returns ACT_RET_CONT. If an error
511 * occurs while soft rewrites are enabled, the action is canceled, but the rule
512 * processing continue. Otherwsize ACT_RET_ERR is returned.
Willy Tarreau33810222019-06-12 17:44:02 +0200513 */
514static enum act_return http_action_replace_uri(struct act_rule *rule, struct proxy *px,
515 struct session *sess, struct stream *s, int flags)
516{
Christopher Faulet13403762019-12-13 09:01:57 +0100517 enum act_return ret = ACT_RET_CONT;
Willy Tarreau33810222019-06-12 17:44:02 +0200518 struct buffer *replace, *output;
519 struct ist uri;
520 int len;
521
522 replace = alloc_trash_chunk();
523 output = alloc_trash_chunk();
524 if (!replace || !output)
Christopher Faulete00d06c2019-12-16 17:18:42 +0100525 goto fail_alloc;
Christopher Faulet12c28b62019-07-15 16:30:24 +0200526 uri = htx_sl_req_uri(http_get_stline(htxbuf(&s->req.buf)));
Willy Tarreau262c3f12019-12-17 06:52:51 +0100527
Amaury Denoyellec453f952021-07-06 11:40:12 +0200528 if (rule->action == 1) { // replace-path
529 struct http_uri_parser parser = http_uri_parser_init(uri);
530 uri = iststop(http_parse_path(&parser), '?');
531 }
532 else if (rule->action == 4) { // replace-pathq
533 struct http_uri_parser parser = http_uri_parser_init(uri);
534 uri = http_parse_path(&parser);
535 }
Willy Tarreau262c3f12019-12-17 06:52:51 +0100536
Christopher Faulet96bff762019-12-17 13:46:18 +0100537 if (!regex_exec_match2(rule->arg.http.re, uri.ptr, uri.len, MAX_MATCH, pmatch, 0))
Willy Tarreau33810222019-06-12 17:44:02 +0200538 goto leave;
539
Christopher Faulet96bff762019-12-17 13:46:18 +0100540 replace->data = build_logline(s, replace->area, replace->size, &rule->arg.http.fmt);
Willy Tarreau33810222019-06-12 17:44:02 +0200541
542 /* note: uri.ptr doesn't need to be zero-terminated because it will
543 * only be used to pick pmatch references.
544 */
545 len = exp_replace(output->area, output->size, uri.ptr, replace->area, pmatch);
546 if (len == -1)
Christopher Faulete00d06c2019-12-16 17:18:42 +0100547 goto fail_rewrite;
Willy Tarreau33810222019-06-12 17:44:02 +0200548
Christopher Faulet2c22a692019-12-18 15:39:56 +0100549 if (http_req_replace_stline(rule->action, output->area, len, px, s) == -1)
Christopher Faulete00d06c2019-12-16 17:18:42 +0100550 goto fail_rewrite;
Willy Tarreau33810222019-06-12 17:44:02 +0200551
Christopher Faulete00d06c2019-12-16 17:18:42 +0100552 leave:
Willy Tarreau33810222019-06-12 17:44:02 +0200553 free_trash_chunk(output);
554 free_trash_chunk(replace);
555 return ret;
Christopher Faulete00d06c2019-12-16 17:18:42 +0100556
557 fail_alloc:
558 if (!(s->flags & SF_ERR_MASK))
559 s->flags |= SF_ERR_RESOURCE;
560 ret = ACT_RET_ERR;
561 goto leave;
562
563 fail_rewrite:
Willy Tarreau4781b152021-04-06 13:53:36 +0200564 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_rewrites);
Christopher Faulete00d06c2019-12-16 17:18:42 +0100565 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200566 _HA_ATOMIC_INC(&s->be->be_counters.failed_rewrites);
William Lallemand36119de2021-03-08 15:26:48 +0100567 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200568 _HA_ATOMIC_INC(&sess->listener->counters->failed_rewrites);
Christopher Faulete00d06c2019-12-16 17:18:42 +0100569 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +0200570 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_rewrites);
Christopher Faulete00d06c2019-12-16 17:18:42 +0100571
Christopher Faulet333bf8c2020-01-22 14:38:05 +0100572 if (!(s->txn->req.flags & HTTP_MSGF_SOFT_RW)) {
Christopher Faulete00d06c2019-12-16 17:18:42 +0100573 ret = ACT_RET_ERR;
Christopher Faulet333bf8c2020-01-22 14:38:05 +0100574 if (!(s->flags & SF_ERR_MASK))
575 s->flags |= SF_ERR_PRXCOND;
576 }
Christopher Faulete00d06c2019-12-16 17:18:42 +0100577 goto leave;
Willy Tarreau33810222019-06-12 17:44:02 +0200578}
579
Christopher Faulet312294f2020-09-02 17:17:44 +0200580/* parse a "replace-uri", "replace-path" or "replace-pathq"
581 * http-request action.
Willy Tarreau33810222019-06-12 17:44:02 +0200582 * This action takes 2 arguments (a regex and a replacement format string).
Christopher Faulet2c22a692019-12-18 15:39:56 +0100583 * The resulting rule makes use of <.action> to store the action (1/3 for now),
Christopher Faulet96bff762019-12-17 13:46:18 +0100584 * <http.re> to store the compiled regex, and <http.fmt> to store the log-format
Willy Tarreau33810222019-06-12 17:44:02 +0200585 * list head. It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
586 */
587static enum act_parse_ret parse_replace_uri(const char **args, int *orig_arg, struct proxy *px,
588 struct act_rule *rule, char **err)
589{
590 int cur_arg = *orig_arg;
591 char *error = NULL;
592
Christopher Faulet312294f2020-09-02 17:17:44 +0200593 switch (args[0][8]) {
594 case 'p':
595 if (args[0][12] == 'q')
596 rule->action = 4; // replace-pathq, same as set-pathq
597 else
598 rule->action = 1; // replace-path, same as set-path
599 break;
600 case 'u':
Christopher Faulet2c22a692019-12-18 15:39:56 +0100601 rule->action = 3; // replace-uri, same as set-uri
Christopher Faulet312294f2020-09-02 17:17:44 +0200602 break;
603 default:
604 memprintf(err, "internal error: unhandled action '%s'", args[0]);
605 return ACT_RET_PRS_ERR;
606 }
Willy Tarreau262c3f12019-12-17 06:52:51 +0100607
Willy Tarreau33810222019-06-12 17:44:02 +0200608 rule->action_ptr = http_action_replace_uri;
Christopher Faulet2eb53962020-01-14 14:47:34 +0100609 rule->release_ptr = release_http_action;
Willy Tarreau33810222019-06-12 17:44:02 +0200610
611 if (!*args[cur_arg] || !*args[cur_arg+1] ||
612 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
613 memprintf(err, "expects exactly 2 arguments <match-regex> and <replace-format>");
614 return ACT_RET_PRS_ERR;
615 }
616
Christopher Faulet96bff762019-12-17 13:46:18 +0100617 if (!(rule->arg.http.re = regex_comp(args[cur_arg], 1, 1, &error))) {
Willy Tarreau33810222019-06-12 17:44:02 +0200618 memprintf(err, "failed to parse the regex : %s", error);
619 free(error);
620 return ACT_RET_PRS_ERR;
621 }
622
Christopher Faulet96bff762019-12-17 13:46:18 +0100623 LIST_INIT(&rule->arg.http.fmt);
Willy Tarreau33810222019-06-12 17:44:02 +0200624 px->conf.args.ctx = ARGC_HRQ;
Christopher Faulet96bff762019-12-17 13:46:18 +0100625 if (!parse_logformat_string(args[cur_arg + 1], px, &rule->arg.http.fmt, LOG_OPT_HTTP,
Willy Tarreau33810222019-06-12 17:44:02 +0200626 (px->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, err)) {
Christopher Faulet1337b322020-01-14 14:50:55 +0100627 regex_free(rule->arg.http.re);
Willy Tarreau33810222019-06-12 17:44:02 +0200628 return ACT_RET_PRS_ERR;
629 }
630
631 (*orig_arg) += 2;
632 return ACT_RET_PRS_OK;
633}
634
Willy Tarreau79e57332018-10-02 16:01:16 +0200635/* This function is just a compliant action wrapper for "set-status". */
636static enum act_return action_http_set_status(struct act_rule *rule, struct proxy *px,
637 struct session *sess, struct stream *s, int flags)
638{
Christopher Faulet96bff762019-12-17 13:46:18 +0100639 if (http_res_set_status(rule->arg.http.i, rule->arg.http.str, s) == -1) {
Willy Tarreau4781b152021-04-06 13:53:36 +0200640 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_rewrites);
Christopher Faulete00d06c2019-12-16 17:18:42 +0100641 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200642 _HA_ATOMIC_INC(&s->be->be_counters.failed_rewrites);
William Lallemand36119de2021-03-08 15:26:48 +0100643 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200644 _HA_ATOMIC_INC(&sess->listener->counters->failed_rewrites);
Christopher Faulete00d06c2019-12-16 17:18:42 +0100645 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +0200646 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_rewrites);
Christopher Faulete00d06c2019-12-16 17:18:42 +0100647
Christopher Faulet333bf8c2020-01-22 14:38:05 +0100648 if (!(s->txn->req.flags & HTTP_MSGF_SOFT_RW)) {
Christopher Faulet333bf8c2020-01-22 14:38:05 +0100649 if (!(s->flags & SF_ERR_MASK))
650 s->flags |= SF_ERR_PRXCOND;
Christopher Faulet692a6c22020-02-07 10:22:31 +0100651 return ACT_RET_ERR;
Christopher Faulet333bf8c2020-01-22 14:38:05 +0100652 }
Christopher Faulete00d06c2019-12-16 17:18:42 +0100653 }
654
Willy Tarreau79e57332018-10-02 16:01:16 +0200655 return ACT_RET_CONT;
656}
657
658/* parse set-status action:
659 * This action accepts a single argument of type int representing
660 * an http status code. It returns ACT_RET_PRS_OK on success,
661 * ACT_RET_PRS_ERR on error.
662 */
663static enum act_parse_ret parse_http_set_status(const char **args, int *orig_arg, struct proxy *px,
664 struct act_rule *rule, char **err)
665{
666 char *error;
667
668 rule->action = ACT_CUSTOM;
669 rule->action_ptr = action_http_set_status;
Christopher Faulet2eb53962020-01-14 14:47:34 +0100670 rule->release_ptr = release_http_action;
Willy Tarreau79e57332018-10-02 16:01:16 +0200671
672 /* Check if an argument is available */
673 if (!*args[*orig_arg]) {
674 memprintf(err, "expects 1 argument: <status>; or 3 arguments: <status> reason <fmt>");
675 return ACT_RET_PRS_ERR;
676 }
677
678 /* convert status code as integer */
Christopher Faulet96bff762019-12-17 13:46:18 +0100679 rule->arg.http.i = strtol(args[*orig_arg], &error, 10);
680 if (*error != '\0' || rule->arg.http.i < 100 || rule->arg.http.i > 999) {
Willy Tarreau79e57332018-10-02 16:01:16 +0200681 memprintf(err, "expects an integer status code between 100 and 999");
682 return ACT_RET_PRS_ERR;
683 }
684
685 (*orig_arg)++;
686
687 /* set custom reason string */
Christopher Faulet96bff762019-12-17 13:46:18 +0100688 rule->arg.http.str = ist(NULL); // If null, we use the default reason for the status code.
Willy Tarreau79e57332018-10-02 16:01:16 +0200689 if (*args[*orig_arg] && strcmp(args[*orig_arg], "reason") == 0 &&
690 (*args[*orig_arg + 1] && strcmp(args[*orig_arg + 1], "if") != 0 && strcmp(args[*orig_arg + 1], "unless") != 0)) {
691 (*orig_arg)++;
Christopher Faulet96bff762019-12-17 13:46:18 +0100692 rule->arg.http.str.ptr = strdup(args[*orig_arg]);
693 rule->arg.http.str.len = strlen(rule->arg.http.str.ptr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200694 (*orig_arg)++;
695 }
696
Christopher Fauletc20b3712020-01-27 15:51:56 +0100697 LIST_INIT(&rule->arg.http.fmt);
Willy Tarreau79e57332018-10-02 16:01:16 +0200698 return ACT_RET_PRS_OK;
699}
700
701/* This function executes the "reject" HTTP action. It clears the request and
702 * response buffer without sending any response. It can be useful as an HTTP
703 * alternative to the silent-drop action to defend against DoS attacks, and may
704 * also be used with HTTP/2 to close a connection instead of just a stream.
705 * The txn status is unchanged, indicating no response was sent. The termination
Christopher Faulet90d22a82020-03-06 11:18:39 +0100706 * flags will indicate "PR". It always returns ACT_RET_ABRT.
Willy Tarreau79e57332018-10-02 16:01:16 +0200707 */
708static enum act_return http_action_reject(struct act_rule *rule, struct proxy *px,
709 struct session *sess, struct stream *s, int flags)
710{
Willy Tarreau0f9cd7b2019-01-31 19:02:43 +0100711 si_must_kill_conn(chn_prod(&s->req));
Willy Tarreau79e57332018-10-02 16:01:16 +0200712 channel_abort(&s->req);
713 channel_abort(&s->res);
Christopher Fauletd4a824e2020-03-06 15:07:09 +0100714 s->req.analysers &= AN_REQ_FLT_END;
715 s->res.analysers &= AN_RES_FLT_END;
Willy Tarreau79e57332018-10-02 16:01:16 +0200716
Willy Tarreau4781b152021-04-06 13:53:36 +0200717 _HA_ATOMIC_INC(&s->be->be_counters.denied_req);
718 _HA_ATOMIC_INC(&sess->fe->fe_counters.denied_req);
Willy Tarreau79e57332018-10-02 16:01:16 +0200719 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200720 _HA_ATOMIC_INC(&sess->listener->counters->denied_req);
Willy Tarreau79e57332018-10-02 16:01:16 +0200721
722 if (!(s->flags & SF_ERR_MASK))
723 s->flags |= SF_ERR_PRXCOND;
724 if (!(s->flags & SF_FINST_MASK))
725 s->flags |= SF_FINST_R;
726
Christopher Faulet90d22a82020-03-06 11:18:39 +0100727 return ACT_RET_ABRT;
Willy Tarreau79e57332018-10-02 16:01:16 +0200728}
729
730/* parse the "reject" action:
731 * This action takes no argument and returns ACT_RET_PRS_OK on success,
732 * ACT_RET_PRS_ERR on error.
733 */
734static enum act_parse_ret parse_http_action_reject(const char **args, int *orig_arg, struct proxy *px,
735 struct act_rule *rule, char **err)
736{
737 rule->action = ACT_CUSTOM;
738 rule->action_ptr = http_action_reject;
739 return ACT_RET_PRS_OK;
740}
741
Olivier Houchard602bf7d2019-05-10 13:59:15 +0200742/* This function executes the "disable-l7-retry" HTTP action.
743 * It disables L7 retries (all retry except for a connection failure). This
744 * can be useful for example to avoid retrying on POST requests.
745 * It just removes the L7 retry flag on the stream_interface, and always
746 * return ACT_RET_CONT;
747 */
748static enum act_return http_req_disable_l7_retry(struct act_rule *rule, struct proxy *px,
749 struct session *sess, struct stream *s, int flags)
750{
751 struct stream_interface *si = &s->si[1];
752
753 /* In theory, the SI_FL_L7_RETRY flags isn't set at this point, but
754 * let's be future-proof and remove it anyway.
755 */
756 si->flags &= ~SI_FL_L7_RETRY;
757 si->flags |= SI_FL_D_L7_RETRY;
758 return ACT_RET_CONT;
759}
760
761/* parse the "disable-l7-retry" action:
762 * This action takes no argument and returns ACT_RET_PRS_OK on success,
763 * ACT_RET_PRS_ERR on error.
764 */
765static enum act_parse_ret parse_http_req_disable_l7_retry(const char **args,
766 int *orig_args, struct proxy *px,
767 struct act_rule *rule, char **err)
768{
769 rule->action = ACT_CUSTOM;
770 rule->action_ptr = http_req_disable_l7_retry;
771 return ACT_RET_PRS_OK;
772}
773
Willy Tarreau79e57332018-10-02 16:01:16 +0200774/* This function executes the "capture" action. It executes a fetch expression,
775 * turns the result into a string and puts it in a capture slot. It always
776 * returns 1. If an error occurs the action is cancelled, but the rule
777 * processing continues.
778 */
779static enum act_return http_action_req_capture(struct act_rule *rule, struct proxy *px,
780 struct session *sess, struct stream *s, int flags)
781{
782 struct sample *key;
783 struct cap_hdr *h = rule->arg.cap.hdr;
784 char **cap = s->req_cap;
785 int len;
786
787 key = sample_fetch_as_type(s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.cap.expr, SMP_T_STR);
788 if (!key)
789 return ACT_RET_CONT;
790
791 if (cap[h->index] == NULL)
792 cap[h->index] = pool_alloc(h->pool);
793
794 if (cap[h->index] == NULL) /* no more capture memory */
795 return ACT_RET_CONT;
796
797 len = key->data.u.str.data;
798 if (len > h->len)
799 len = h->len;
800
801 memcpy(cap[h->index], key->data.u.str.area, len);
802 cap[h->index][len] = 0;
803 return ACT_RET_CONT;
804}
805
806/* This function executes the "capture" action and store the result in a
807 * capture slot if exists. It executes a fetch expression, turns the result
808 * into a string and puts it in a capture slot. It always returns 1. If an
809 * error occurs the action is cancelled, but the rule processing continues.
810 */
811static enum act_return http_action_req_capture_by_id(struct act_rule *rule, struct proxy *px,
812 struct session *sess, struct stream *s, int flags)
813{
814 struct sample *key;
815 struct cap_hdr *h;
816 char **cap = s->req_cap;
817 struct proxy *fe = strm_fe(s);
818 int len;
819 int i;
820
821 /* Look for the original configuration. */
822 for (h = fe->req_cap, i = fe->nb_req_cap - 1;
823 h != NULL && i != rule->arg.capid.idx ;
824 i--, h = h->next);
825 if (!h)
826 return ACT_RET_CONT;
827
828 key = sample_fetch_as_type(s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.capid.expr, SMP_T_STR);
829 if (!key)
830 return ACT_RET_CONT;
831
832 if (cap[h->index] == NULL)
833 cap[h->index] = pool_alloc(h->pool);
834
835 if (cap[h->index] == NULL) /* no more capture memory */
836 return ACT_RET_CONT;
837
838 len = key->data.u.str.data;
839 if (len > h->len)
840 len = h->len;
841
842 memcpy(cap[h->index], key->data.u.str.area, len);
843 cap[h->index][len] = 0;
844 return ACT_RET_CONT;
845}
846
847/* Check an "http-request capture" action.
848 *
849 * The function returns 1 in success case, otherwise, it returns 0 and err is
850 * filled.
851 */
852static int check_http_req_capture(struct act_rule *rule, struct proxy *px, char **err)
853{
854 if (rule->action_ptr != http_action_req_capture_by_id)
855 return 1;
856
Baptiste Assmann19a69b32020-01-16 14:34:22 +0100857 /* capture slots can only be declared in frontends, so we can't check their
858 * existence in backends at configuration parsing step
859 */
860 if (px->cap & PR_CAP_FE && rule->arg.capid.idx >= px->nb_req_cap) {
Willy Tarreau79e57332018-10-02 16:01:16 +0200861 memprintf(err, "unable to find capture id '%d' referenced by http-request capture rule",
862 rule->arg.capid.idx);
863 return 0;
864 }
865
866 return 1;
867}
868
Christopher Faulet2eb53962020-01-14 14:47:34 +0100869/* Release memory allocate by an http capture action */
870static void release_http_capture(struct act_rule *rule)
871{
872 if (rule->action_ptr == http_action_req_capture)
873 release_sample_expr(rule->arg.cap.expr);
874 else
875 release_sample_expr(rule->arg.capid.expr);
876}
877
Willy Tarreau79e57332018-10-02 16:01:16 +0200878/* parse an "http-request capture" action. It takes a single argument which is
879 * a sample fetch expression. It stores the expression into arg->act.p[0] and
880 * the allocated hdr_cap struct or the preallocated "id" into arg->act.p[1].
881 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
882 */
883static enum act_parse_ret parse_http_req_capture(const char **args, int *orig_arg, struct proxy *px,
884 struct act_rule *rule, char **err)
885{
886 struct sample_expr *expr;
887 struct cap_hdr *hdr;
888 int cur_arg;
889 int len = 0;
890
891 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
892 if (strcmp(args[cur_arg], "if") == 0 ||
893 strcmp(args[cur_arg], "unless") == 0)
894 break;
895
896 if (cur_arg < *orig_arg + 3) {
897 memprintf(err, "expects <expression> [ 'len' <length> | id <idx> ]");
898 return ACT_RET_PRS_ERR;
899 }
900
901 cur_arg = *orig_arg;
Willy Tarreaue3b57bf2020-02-14 16:50:14 +0100902 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 +0200903 if (!expr)
904 return ACT_RET_PRS_ERR;
905
906 if (!(expr->fetch->val & SMP_VAL_FE_HRQ_HDR)) {
907 memprintf(err,
908 "fetch method '%s' extracts information from '%s', none of which is available here",
909 args[cur_arg-1], sample_src_names(expr->fetch->use));
Christopher Faulet1337b322020-01-14 14:50:55 +0100910 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200911 return ACT_RET_PRS_ERR;
912 }
913
914 if (!args[cur_arg] || !*args[cur_arg]) {
915 memprintf(err, "expects 'len or 'id'");
Christopher Faulet1337b322020-01-14 14:50:55 +0100916 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200917 return ACT_RET_PRS_ERR;
918 }
919
920 if (strcmp(args[cur_arg], "len") == 0) {
921 cur_arg++;
922
923 if (!(px->cap & PR_CAP_FE)) {
924 memprintf(err, "proxy '%s' has no frontend capability", px->id);
Christopher Faulet1337b322020-01-14 14:50:55 +0100925 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200926 return ACT_RET_PRS_ERR;
927 }
928
929 px->conf.args.ctx = ARGC_CAP;
930
931 if (!args[cur_arg]) {
932 memprintf(err, "missing length value");
Christopher Faulet1337b322020-01-14 14:50:55 +0100933 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200934 return ACT_RET_PRS_ERR;
935 }
936 /* we copy the table name for now, it will be resolved later */
937 len = atoi(args[cur_arg]);
938 if (len <= 0) {
939 memprintf(err, "length must be > 0");
Christopher Faulet1337b322020-01-14 14:50:55 +0100940 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200941 return ACT_RET_PRS_ERR;
942 }
943 cur_arg++;
944
Willy Tarreau79e57332018-10-02 16:01:16 +0200945 hdr = calloc(1, sizeof(*hdr));
Remi Tricot-Le Bretona4bf8a02021-05-12 17:54:17 +0200946 if (!hdr) {
947 memprintf(err, "out of memory");
948 release_sample_expr(expr);
949 return ACT_RET_PRS_ERR;
950 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200951 hdr->next = px->req_cap;
952 hdr->name = NULL; /* not a header capture */
953 hdr->namelen = 0;
954 hdr->len = len;
955 hdr->pool = create_pool("caphdr", hdr->len + 1, MEM_F_SHARED);
956 hdr->index = px->nb_req_cap++;
957
958 px->req_cap = hdr;
959 px->to_log |= LW_REQHDR;
960
961 rule->action = ACT_CUSTOM;
962 rule->action_ptr = http_action_req_capture;
Christopher Faulet2eb53962020-01-14 14:47:34 +0100963 rule->release_ptr = release_http_capture;
Willy Tarreau79e57332018-10-02 16:01:16 +0200964 rule->arg.cap.expr = expr;
965 rule->arg.cap.hdr = hdr;
966 }
967
968 else if (strcmp(args[cur_arg], "id") == 0) {
969 int id;
970 char *error;
971
972 cur_arg++;
973
974 if (!args[cur_arg]) {
975 memprintf(err, "missing id value");
Christopher Faulet1337b322020-01-14 14:50:55 +0100976 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200977 return ACT_RET_PRS_ERR;
978 }
979
980 id = strtol(args[cur_arg], &error, 10);
981 if (*error != '\0') {
982 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
Christopher Faulet1337b322020-01-14 14:50:55 +0100983 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200984 return ACT_RET_PRS_ERR;
985 }
986 cur_arg++;
987
988 px->conf.args.ctx = ARGC_CAP;
989
990 rule->action = ACT_CUSTOM;
991 rule->action_ptr = http_action_req_capture_by_id;
992 rule->check_ptr = check_http_req_capture;
Christopher Faulet2eb53962020-01-14 14:47:34 +0100993 rule->release_ptr = release_http_capture;
Willy Tarreau79e57332018-10-02 16:01:16 +0200994 rule->arg.capid.expr = expr;
995 rule->arg.capid.idx = id;
996 }
997
998 else {
999 memprintf(err, "expects 'len' or 'id', found '%s'", args[cur_arg]);
Christopher Faulet1337b322020-01-14 14:50:55 +01001000 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +02001001 return ACT_RET_PRS_ERR;
1002 }
1003
1004 *orig_arg = cur_arg;
1005 return ACT_RET_PRS_OK;
1006}
1007
1008/* This function executes the "capture" action and store the result in a
1009 * capture slot if exists. It executes a fetch expression, turns the result
1010 * into a string and puts it in a capture slot. It always returns 1. If an
1011 * error occurs the action is cancelled, but the rule processing continues.
1012 */
1013static enum act_return http_action_res_capture_by_id(struct act_rule *rule, struct proxy *px,
1014 struct session *sess, struct stream *s, int flags)
1015{
1016 struct sample *key;
1017 struct cap_hdr *h;
1018 char **cap = s->res_cap;
1019 struct proxy *fe = strm_fe(s);
1020 int len;
1021 int i;
1022
1023 /* Look for the original configuration. */
1024 for (h = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
1025 h != NULL && i != rule->arg.capid.idx ;
1026 i--, h = h->next);
1027 if (!h)
1028 return ACT_RET_CONT;
1029
1030 key = sample_fetch_as_type(s->be, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL, rule->arg.capid.expr, SMP_T_STR);
1031 if (!key)
1032 return ACT_RET_CONT;
1033
1034 if (cap[h->index] == NULL)
1035 cap[h->index] = pool_alloc(h->pool);
1036
1037 if (cap[h->index] == NULL) /* no more capture memory */
1038 return ACT_RET_CONT;
1039
1040 len = key->data.u.str.data;
1041 if (len > h->len)
1042 len = h->len;
1043
1044 memcpy(cap[h->index], key->data.u.str.area, len);
1045 cap[h->index][len] = 0;
1046 return ACT_RET_CONT;
1047}
1048
1049/* Check an "http-response capture" action.
1050 *
1051 * The function returns 1 in success case, otherwise, it returns 0 and err is
1052 * filled.
1053 */
1054static int check_http_res_capture(struct act_rule *rule, struct proxy *px, char **err)
1055{
1056 if (rule->action_ptr != http_action_res_capture_by_id)
1057 return 1;
1058
Tim Duesterhusf3f4aa02020-07-03 13:43:42 +02001059 /* capture slots can only be declared in frontends, so we can't check their
1060 * existence in backends at configuration parsing step
1061 */
1062 if (px->cap & PR_CAP_FE && rule->arg.capid.idx >= px->nb_rsp_cap) {
Willy Tarreau79e57332018-10-02 16:01:16 +02001063 memprintf(err, "unable to find capture id '%d' referenced by http-response capture rule",
1064 rule->arg.capid.idx);
1065 return 0;
1066 }
1067
1068 return 1;
1069}
1070
1071/* parse an "http-response capture" action. It takes a single argument which is
1072 * a sample fetch expression. It stores the expression into arg->act.p[0] and
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -07001073 * the allocated hdr_cap struct of the preallocated id into arg->act.p[1].
Willy Tarreau79e57332018-10-02 16:01:16 +02001074 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
1075 */
1076static enum act_parse_ret parse_http_res_capture(const char **args, int *orig_arg, struct proxy *px,
1077 struct act_rule *rule, char **err)
1078{
1079 struct sample_expr *expr;
1080 int cur_arg;
1081 int id;
1082 char *error;
1083
1084 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
1085 if (strcmp(args[cur_arg], "if") == 0 ||
1086 strcmp(args[cur_arg], "unless") == 0)
1087 break;
1088
1089 if (cur_arg < *orig_arg + 3) {
1090 memprintf(err, "expects <expression> id <idx>");
1091 return ACT_RET_PRS_ERR;
1092 }
1093
1094 cur_arg = *orig_arg;
Willy Tarreaue3b57bf2020-02-14 16:50:14 +01001095 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 +02001096 if (!expr)
1097 return ACT_RET_PRS_ERR;
1098
1099 if (!(expr->fetch->val & SMP_VAL_FE_HRS_HDR)) {
1100 memprintf(err,
1101 "fetch method '%s' extracts information from '%s', none of which is available here",
1102 args[cur_arg-1], sample_src_names(expr->fetch->use));
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 if (!args[cur_arg] || !*args[cur_arg]) {
1108 memprintf(err, "expects 'id'");
Christopher Faulet1337b322020-01-14 14:50:55 +01001109 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +02001110 return ACT_RET_PRS_ERR;
1111 }
1112
1113 if (strcmp(args[cur_arg], "id") != 0) {
1114 memprintf(err, "expects 'id', found '%s'", args[cur_arg]);
Christopher Faulet1337b322020-01-14 14:50:55 +01001115 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +02001116 return ACT_RET_PRS_ERR;
1117 }
1118
1119 cur_arg++;
1120
1121 if (!args[cur_arg]) {
1122 memprintf(err, "missing id value");
Christopher Faulet1337b322020-01-14 14:50:55 +01001123 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +02001124 return ACT_RET_PRS_ERR;
1125 }
1126
1127 id = strtol(args[cur_arg], &error, 10);
1128 if (*error != '\0') {
1129 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
Christopher Faulet1337b322020-01-14 14:50:55 +01001130 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +02001131 return ACT_RET_PRS_ERR;
1132 }
1133 cur_arg++;
1134
1135 px->conf.args.ctx = ARGC_CAP;
1136
1137 rule->action = ACT_CUSTOM;
1138 rule->action_ptr = http_action_res_capture_by_id;
1139 rule->check_ptr = check_http_res_capture;
Christopher Faulet2eb53962020-01-14 14:47:34 +01001140 rule->release_ptr = release_http_capture;
Willy Tarreau79e57332018-10-02 16:01:16 +02001141 rule->arg.capid.expr = expr;
1142 rule->arg.capid.idx = id;
1143
1144 *orig_arg = cur_arg;
1145 return ACT_RET_PRS_OK;
1146}
1147
Christopher Faulet81e20172019-12-12 16:40:30 +01001148/* Parse a "allow" action for a request or a response rule. It takes no argument. It
1149 * returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
1150 */
1151static enum act_parse_ret parse_http_allow(const char **args, int *orig_arg, struct proxy *px,
1152 struct act_rule *rule, char **err)
1153{
1154 rule->action = ACT_ACTION_ALLOW;
Christopher Faulet245cf792019-12-18 14:58:12 +01001155 rule->flags |= ACT_FLAG_FINAL;
Christopher Faulet81e20172019-12-12 16:40:30 +01001156 return ACT_RET_PRS_OK;
1157}
1158
Christopher Faulete0fca292020-01-13 21:49:03 +01001159/* Parse "deny" or "tarpit" actions for a request rule or "deny" action for a
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001160 * response rule. It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on
1161 * error. It relies on http_parse_http_reply() to set
1162 * <.arg.http_reply>.
Christopher Faulet81e20172019-12-12 16:40:30 +01001163 */
Christopher Faulete0fca292020-01-13 21:49:03 +01001164static enum act_parse_ret parse_http_deny(const char **args, int *orig_arg, struct proxy *px,
1165 struct act_rule *rule, char **err)
Christopher Faulet81e20172019-12-12 16:40:30 +01001166{
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001167 int default_status;
1168 int cur_arg, arg = 0;
Christopher Faulet81e20172019-12-12 16:40:30 +01001169
1170 cur_arg = *orig_arg;
Christopher Faulete0fca292020-01-13 21:49:03 +01001171 if (rule->from == ACT_F_HTTP_REQ) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001172 if (strcmp(args[cur_arg - 1], "tarpit") == 0) {
Christopher Faulete0fca292020-01-13 21:49:03 +01001173 rule->action = ACT_HTTP_REQ_TARPIT;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001174 default_status = 500;
Christopher Faulet81e20172019-12-12 16:40:30 +01001175 }
Christopher Faulete0fca292020-01-13 21:49:03 +01001176 else {
1177 rule->action = ACT_ACTION_DENY;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001178 default_status = 403;
Christopher Faulet81e20172019-12-12 16:40:30 +01001179 }
Christopher Faulet81e20172019-12-12 16:40:30 +01001180 }
Christopher Faulete0fca292020-01-13 21:49:03 +01001181 else {
Christopher Faulet554c0eb2020-01-14 12:00:28 +01001182 rule->action = ACT_ACTION_DENY;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001183 default_status = 502;
Christopher Faulete0fca292020-01-13 21:49:03 +01001184 }
Christopher Faulet040c8cd2020-01-13 16:43:45 +01001185
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001186 /* If no args or only a deny_status specified, fallback on the legacy
1187 * mode and use default error files despite the fact that
1188 * default-errorfiles is not used. Otherwise, parse an http reply.
1189 */
Christopher Faulet040c8cd2020-01-13 16:43:45 +01001190
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001191 /* Prepare parsing of log-format strings */
1192 px->conf.args.ctx = ((rule->from == ACT_F_HTTP_REQ) ? ARGC_HRQ : ARGC_HRS);
Christopher Faulet554c0eb2020-01-14 12:00:28 +01001193
Christopher Faulet9467f182020-06-30 09:32:01 +02001194 if (!*(args[cur_arg]) || strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001195 rule->arg.http_reply = http_parse_http_reply((const char *[]){"default-errorfiles", ""}, &arg, px, default_status, err);
1196 goto end;
Christopher Faulet554c0eb2020-01-14 12:00:28 +01001197 }
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001198
1199 if (strcmp(args[cur_arg], "deny_status") == 0) {
Christopher Faulet9467f182020-06-30 09:32:01 +02001200 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 +02001201 rule->arg.http_reply = http_parse_http_reply((const char *[]){"status", args[cur_arg+1], "default-errorfiles", ""},
1202 &arg, px, default_status, err);
1203 *orig_arg += 2;
1204 goto end;
Christopher Faulet554c0eb2020-01-14 12:00:28 +01001205 }
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001206 args[cur_arg] += 5; /* skip "deny_" for the parsing */
Christopher Faulet554c0eb2020-01-14 12:00:28 +01001207 }
1208
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001209 rule->arg.http_reply = http_parse_http_reply(args, orig_arg, px, default_status, err);
Christopher Faulet554c0eb2020-01-14 12:00:28 +01001210
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001211 end:
1212 if (!rule->arg.http_reply)
1213 return ACT_RET_PRS_ERR;
1214
1215 rule->flags |= ACT_FLAG_FINAL;
1216 rule->check_ptr = check_act_http_reply;
1217 rule->release_ptr = release_act_http_reply;
Christopher Faulet81e20172019-12-12 16:40:30 +01001218 return ACT_RET_PRS_OK;
1219}
1220
Christopher Fauletb3048832020-05-27 15:26:43 +02001221
1222/* This function executes a auth action. It builds an 401/407 HTX message using
1223 * the corresponding proxy's error message. On success, it returns
1224 * ACT_RET_ABRT. If an error occurs ACT_RET_ERR is returned.
1225 */
1226static enum act_return http_action_auth(struct act_rule *rule, struct proxy *px,
1227 struct session *sess, struct stream *s, int flags)
1228{
1229 struct channel *req = &s->req;
1230 struct channel *res = &s->res;
1231 struct htx *htx = htx_from_buf(&res->buf);
1232 struct http_reply *reply;
1233 const char *auth_realm;
1234 struct http_hdr_ctx ctx;
1235 struct ist hdr;
1236
1237 /* Auth might be performed on regular http-req rules as well as on stats */
1238 auth_realm = rule->arg.http.str.ptr;
1239 if (!auth_realm) {
1240 if (px->uri_auth && s->current_rule_list == &px->uri_auth->http_req_rules)
1241 auth_realm = STATS_DEFAULT_REALM;
1242 else
1243 auth_realm = px->id;
1244 }
1245
1246 if (!(s->txn->flags & TX_USE_PX_CONN)) {
1247 s->txn->status = 401;
1248 hdr = ist("WWW-Authenticate");
1249 }
1250 else {
1251 s->txn->status = 407;
1252 hdr = ist("Proxy-Authenticate");
1253 }
1254 reply = http_error_message(s);
1255 channel_htx_truncate(res, htx);
1256
1257 if (chunk_printf(&trash, "Basic realm=\"%s\"", auth_realm) == -1)
1258 goto fail;
1259
1260 /* Write the generic 40x message */
1261 if (http_reply_to_htx(s, htx, reply) == -1)
1262 goto fail;
1263
1264 /* Remove all existing occurrences of the XXX-Authenticate header */
1265 ctx.blk = NULL;
1266 while (http_find_header(htx, hdr, &ctx, 1))
1267 http_remove_header(htx, &ctx);
1268
1269 /* Now a the right XXX-Authenticate header */
1270 if (!http_add_header(htx, hdr, ist2(b_orig(&trash), b_data(&trash))))
1271 goto fail;
1272
1273 /* Finally forward the reply */
1274 htx_to_buf(htx, &res->buf);
1275 if (!http_forward_proxy_resp(s, 1))
1276 goto fail;
1277
1278 /* Note: Only eval on the request */
1279 s->logs.tv_request = now;
1280 req->analysers &= AN_REQ_FLT_END;
1281
1282 if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Willy Tarreau4781b152021-04-06 13:53:36 +02001283 _HA_ATOMIC_INC(&s->sess->fe->fe_counters.intercepted_req);
Christopher Fauletb3048832020-05-27 15:26:43 +02001284
1285 if (!(s->flags & SF_ERR_MASK))
1286 s->flags |= SF_ERR_LOCAL;
1287 if (!(s->flags & SF_FINST_MASK))
1288 s->flags |= SF_FINST_R;
1289
1290 stream_inc_http_err_ctr(s);
1291 return ACT_RET_ABRT;
1292
1293 fail:
1294 /* If an error occurred, remove the incomplete HTTP response from the
1295 * buffer */
1296 channel_htx_truncate(res, htx);
1297 return ACT_RET_ERR;
1298}
1299
Christopher Faulet81e20172019-12-12 16:40:30 +01001300/* Parse a "auth" action. It may take 2 optional arguments to define a "realm"
1301 * parameter. It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
1302 */
1303static enum act_parse_ret parse_http_auth(const char **args, int *orig_arg, struct proxy *px,
1304 struct act_rule *rule, char **err)
1305{
1306 int cur_arg;
1307
Christopher Fauletb3048832020-05-27 15:26:43 +02001308 rule->action = ACT_CUSTOM;
Christopher Faulet245cf792019-12-18 14:58:12 +01001309 rule->flags |= ACT_FLAG_FINAL;
Christopher Fauletb3048832020-05-27 15:26:43 +02001310 rule->action_ptr = http_action_auth;
Christopher Faulet2eb53962020-01-14 14:47:34 +01001311 rule->release_ptr = release_http_action;
Christopher Faulet81e20172019-12-12 16:40:30 +01001312
1313 cur_arg = *orig_arg;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001314 if (strcmp(args[cur_arg], "realm") == 0) {
Christopher Faulet81e20172019-12-12 16:40:30 +01001315 cur_arg++;
1316 if (!*args[cur_arg]) {
1317 memprintf(err, "missing realm value.\n");
1318 return ACT_RET_PRS_ERR;
1319 }
Christopher Faulet96bff762019-12-17 13:46:18 +01001320 rule->arg.http.str.ptr = strdup(args[cur_arg]);
1321 rule->arg.http.str.len = strlen(rule->arg.http.str.ptr);
Christopher Faulet81e20172019-12-12 16:40:30 +01001322 cur_arg++;
1323 }
1324
Christopher Fauletc20b3712020-01-27 15:51:56 +01001325 LIST_INIT(&rule->arg.http.fmt);
Christopher Faulet81e20172019-12-12 16:40:30 +01001326 *orig_arg = cur_arg;
1327 return ACT_RET_PRS_OK;
1328}
1329
Christopher Faulet91b3ec12020-01-17 22:30:06 +01001330/* This function executes a early-hint action. It adds an HTTP Early Hint HTTP
1331 * 103 response header with <.arg.http.str> name and with a value built
1332 * according to <.arg.http.fmt> log line format. If it is the first early-hint
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001333 * rule of series, the 103 response start-line is added first. At the end, if
Christopher Faulet91b3ec12020-01-17 22:30:06 +01001334 * the next rule is not an early-hint rule or if it is the last rule, the EOH
1335 * block is added to terminate the response. On success, it returns
1336 * ACT_RET_CONT. If an error occurs while soft rewrites are enabled, the action
1337 * is canceled, but the rule processing continue. Otherwsize ACT_RET_ERR is
1338 * returned.
1339 */
1340static enum act_return http_action_early_hint(struct act_rule *rule, struct proxy *px,
1341 struct session *sess, struct stream *s, int flags)
1342{
1343 struct act_rule *prev_rule, *next_rule;
1344 struct channel *res = &s->res;
1345 struct htx *htx = htx_from_buf(&res->buf);
1346 struct buffer *value = alloc_trash_chunk();
1347 enum act_return ret = ACT_RET_CONT;
1348
1349 if (!(s->txn->req.flags & HTTP_MSGF_VER_11))
1350 goto leave;
1351
1352 if (!value) {
1353 if (!(s->flags & SF_ERR_MASK))
1354 s->flags |= SF_ERR_RESOURCE;
1355 goto error;
1356 }
1357
1358 /* get previous and next rules */
1359 prev_rule = LIST_PREV(&rule->list, typeof(rule), list);
1360 next_rule = LIST_NEXT(&rule->list, typeof(rule), list);
1361
1362 /* if no previous rule or previous rule is not early-hint, start a new response. Otherwise,
1363 * continue to add link to a previously started response */
1364 if (&prev_rule->list == s->current_rule_list || prev_rule->action_ptr != http_action_early_hint) {
1365 struct htx_sl *sl;
1366 unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|
1367 HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
1368
1369 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
1370 ist("HTTP/1.1"), ist("103"), ist("Early Hints"));
1371 if (!sl)
1372 goto error;
1373 sl->info.res.status = 103;
1374 }
1375
1376 /* Add the HTTP Early Hint HTTP 103 response heade */
1377 value->data = build_logline(s, b_tail(value), b_room(value), &rule->arg.http.fmt);
1378 if (!htx_add_header(htx, rule->arg.http.str, ist2(b_head(value), b_data(value))))
1379 goto error;
1380
1381 /* if it is the last rule or the next one is not an early-hint, terminate the current
1382 * response. */
1383 if (&next_rule->list == s->current_rule_list || next_rule->action_ptr != http_action_early_hint) {
Christopher Faulet91b3ec12020-01-17 22:30:06 +01001384 if (!htx_add_endof(htx, HTX_BLK_EOH)) {
1385 /* If an error occurred during an Early-hint rule,
1386 * remove the incomplete HTTP 103 response from the
1387 * buffer */
1388 goto error;
1389 }
1390
Christopher Fauleta72a7e42020-01-28 09:28:11 +01001391 if (!http_forward_proxy_resp(s, 0))
1392 goto error;
Christopher Faulet91b3ec12020-01-17 22:30:06 +01001393 }
1394
1395 leave:
1396 free_trash_chunk(value);
1397 return ret;
1398
1399 error:
1400 /* If an error occurred during an Early-hint rule, remove the incomplete
1401 * HTTP 103 response from the buffer */
1402 channel_htx_truncate(res, htx);
1403 ret = ACT_RET_ERR;
1404 goto leave;
1405}
1406
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001407/* This function executes a set-header or add-header actions. It builds a string
1408 * in the trash from the specified format string. It finds the action to be
1409 * performed in <.action>, previously filled by function parse_set_header(). The
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001410 * replacement action is executed by the function http_action_set_header(). On
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001411 * success, it returns ACT_RET_CONT. If an error occurs while soft rewrites are
1412 * enabled, the action is canceled, but the rule processing continue. Otherwsize
1413 * ACT_RET_ERR is returned.
1414 */
1415static enum act_return http_action_set_header(struct act_rule *rule, struct proxy *px,
1416 struct session *sess, struct stream *s, int flags)
1417{
Christopher Faulet91e31d82020-01-24 15:37:13 +01001418 struct http_msg *msg = ((rule->from == ACT_F_HTTP_REQ) ? &s->txn->req : &s->txn->rsp);
1419 struct htx *htx = htxbuf(&msg->chn->buf);
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001420 enum act_return ret = ACT_RET_CONT;
1421 struct buffer *replace;
1422 struct http_hdr_ctx ctx;
1423 struct ist n, v;
1424
1425 replace = alloc_trash_chunk();
1426 if (!replace)
1427 goto fail_alloc;
1428
1429 replace->data = build_logline(s, replace->area, replace->size, &rule->arg.http.fmt);
1430 n = rule->arg.http.str;
1431 v = ist2(replace->area, replace->data);
1432
1433 if (rule->action == 0) { // set-header
1434 /* remove all occurrences of the header */
1435 ctx.blk = NULL;
1436 while (http_find_header(htx, n, &ctx, 1))
1437 http_remove_header(htx, &ctx);
1438 }
1439
1440 /* Now add header */
1441 if (!http_add_header(htx, n, v))
1442 goto fail_rewrite;
1443
1444 leave:
1445 free_trash_chunk(replace);
1446 return ret;
1447
1448 fail_alloc:
1449 if (!(s->flags & SF_ERR_MASK))
1450 s->flags |= SF_ERR_RESOURCE;
1451 ret = ACT_RET_ERR;
1452 goto leave;
1453
1454 fail_rewrite:
Willy Tarreau4781b152021-04-06 13:53:36 +02001455 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_rewrites);
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001456 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +02001457 _HA_ATOMIC_INC(&s->be->be_counters.failed_rewrites);
William Lallemand36119de2021-03-08 15:26:48 +01001458 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001459 _HA_ATOMIC_INC(&sess->listener->counters->failed_rewrites);
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001460 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001461 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_rewrites);
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001462
Christopher Faulet333bf8c2020-01-22 14:38:05 +01001463 if (!(msg->flags & HTTP_MSGF_SOFT_RW)) {
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001464 ret = ACT_RET_ERR;
Christopher Faulet333bf8c2020-01-22 14:38:05 +01001465 if (!(s->flags & SF_ERR_MASK))
1466 s->flags |= SF_ERR_PRXCOND;
1467 }
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001468 goto leave;
1469}
1470
Christopher Faulet81e20172019-12-12 16:40:30 +01001471/* Parse a "set-header", "add-header" or "early-hint" actions. It takes an
1472 * header name and a log-format string as arguments. It returns ACT_RET_PRS_OK
1473 * on success, ACT_RET_PRS_ERR on error.
1474 *
1475 * Note: same function is used for the request and the response. However
1476 * "early-hint" rules are only supported for request rules.
1477 */
1478static enum act_parse_ret parse_http_set_header(const char **args, int *orig_arg, struct proxy *px,
1479 struct act_rule *rule, char **err)
1480{
Christopher Faulet81e20172019-12-12 16:40:30 +01001481 int cap, cur_arg;
1482
Christopher Faulet91b3ec12020-01-17 22:30:06 +01001483 if (args[*orig_arg-1][0] == 'e') {
1484 rule->action = ACT_CUSTOM;
1485 rule->action_ptr = http_action_early_hint;
1486 }
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001487 else {
1488 if (args[*orig_arg-1][0] == 's')
1489 rule->action = 0; // set-header
1490 else
1491 rule->action = 1; // add-header
1492 rule->action_ptr = http_action_set_header;
1493 }
Christopher Faulet2eb53962020-01-14 14:47:34 +01001494 rule->release_ptr = release_http_action;
Christopher Faulet81e20172019-12-12 16:40:30 +01001495
1496 cur_arg = *orig_arg;
1497 if (!*args[cur_arg] || !*args[cur_arg+1]) {
1498 memprintf(err, "expects exactly 2 arguments");
1499 return ACT_RET_PRS_ERR;
1500 }
1501
Christopher Faulet81e20172019-12-12 16:40:30 +01001502
Christopher Faulet96bff762019-12-17 13:46:18 +01001503 rule->arg.http.str.ptr = strdup(args[cur_arg]);
1504 rule->arg.http.str.len = strlen(rule->arg.http.str.ptr);
1505 LIST_INIT(&rule->arg.http.fmt);
Christopher Faulet81e20172019-12-12 16:40:30 +01001506
1507 if (rule->from == ACT_F_HTTP_REQ) {
1508 px->conf.args.ctx = ARGC_HRQ;
1509 cap = (px->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR;
1510 }
1511 else{
1512 px->conf.args.ctx = ARGC_HRS;
1513 cap = (px->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR;
1514 }
1515
1516 cur_arg++;
Christopher Faulet1337b322020-01-14 14:50:55 +01001517 if (!parse_logformat_string(args[cur_arg], px, &rule->arg.http.fmt, LOG_OPT_HTTP, cap, err)) {
Tim Duesterhused526372020-03-05 17:56:33 +01001518 istfree(&rule->arg.http.str);
Christopher Faulet81e20172019-12-12 16:40:30 +01001519 return ACT_RET_PRS_ERR;
Christopher Faulet1337b322020-01-14 14:50:55 +01001520 }
Christopher Faulet81e20172019-12-12 16:40:30 +01001521
1522 free(px->conf.lfs_file);
1523 px->conf.lfs_file = strdup(px->conf.args.file);
1524 px->conf.lfs_line = px->conf.args.line;
1525
1526 *orig_arg = cur_arg + 1;
1527 return ACT_RET_PRS_OK;
1528}
1529
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001530/* This function executes a replace-header or replace-value actions. It
1531 * builds a string in the trash from the specified format string. It finds
1532 * the action to be performed in <.action>, previously filled by function
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001533 * parse_replace_header(). The replacement action is executed by the function
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001534 * http_action_replace_header(). On success, it returns ACT_RET_CONT. If an error
1535 * occurs while soft rewrites are enabled, the action is canceled, but the rule
1536 * processing continue. Otherwsize ACT_RET_ERR is returned.
1537 */
1538static enum act_return http_action_replace_header(struct act_rule *rule, struct proxy *px,
1539 struct session *sess, struct stream *s, int flags)
1540{
Christopher Faulet91e31d82020-01-24 15:37:13 +01001541 struct http_msg *msg = ((rule->from == ACT_F_HTTP_REQ) ? &s->txn->req : &s->txn->rsp);
1542 struct htx *htx = htxbuf(&msg->chn->buf);
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001543 enum act_return ret = ACT_RET_CONT;
1544 struct buffer *replace;
1545 int r;
1546
1547 replace = alloc_trash_chunk();
1548 if (!replace)
1549 goto fail_alloc;
1550
1551 replace->data = build_logline(s, replace->area, replace->size, &rule->arg.http.fmt);
1552
1553 r = http_replace_hdrs(s, htx, rule->arg.http.str, replace->area, rule->arg.http.re, (rule->action == 0));
1554 if (r == -1)
1555 goto fail_rewrite;
1556
1557 leave:
1558 free_trash_chunk(replace);
1559 return ret;
1560
1561 fail_alloc:
1562 if (!(s->flags & SF_ERR_MASK))
1563 s->flags |= SF_ERR_RESOURCE;
1564 ret = ACT_RET_ERR;
1565 goto leave;
1566
1567 fail_rewrite:
Willy Tarreau4781b152021-04-06 13:53:36 +02001568 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_rewrites);
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001569 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +02001570 _HA_ATOMIC_INC(&s->be->be_counters.failed_rewrites);
William Lallemand36119de2021-03-08 15:26:48 +01001571 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001572 _HA_ATOMIC_INC(&sess->listener->counters->failed_rewrites);
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001573 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001574 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_rewrites);
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001575
Christopher Faulet333bf8c2020-01-22 14:38:05 +01001576 if (!(msg->flags & HTTP_MSGF_SOFT_RW)) {
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001577 ret = ACT_RET_ERR;
Christopher Faulet333bf8c2020-01-22 14:38:05 +01001578 if (!(s->flags & SF_ERR_MASK))
1579 s->flags |= SF_ERR_PRXCOND;
1580 }
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001581 goto leave;
1582}
1583
Christopher Faulet81e20172019-12-12 16:40:30 +01001584/* Parse a "replace-header" or "replace-value" actions. It takes an header name,
1585 * a regex and replacement string as arguments. It returns ACT_RET_PRS_OK on
1586 * success, ACT_RET_PRS_ERR on error.
1587 */
1588static enum act_parse_ret parse_http_replace_header(const char **args, int *orig_arg, struct proxy *px,
1589 struct act_rule *rule, char **err)
1590{
1591 int cap, cur_arg;
1592
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001593 if (args[*orig_arg-1][8] == 'h')
1594 rule->action = 0; // replace-header
1595 else
1596 rule->action = 1; // replace-value
1597 rule->action_ptr = http_action_replace_header;
Christopher Faulet2eb53962020-01-14 14:47:34 +01001598 rule->release_ptr = release_http_action;
Christopher Faulet81e20172019-12-12 16:40:30 +01001599
1600 cur_arg = *orig_arg;
1601 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2]) {
1602 memprintf(err, "expects exactly 3 arguments");
1603 return ACT_RET_PRS_ERR;
1604 }
1605
Christopher Faulet96bff762019-12-17 13:46:18 +01001606 rule->arg.http.str.ptr = strdup(args[cur_arg]);
1607 rule->arg.http.str.len = strlen(rule->arg.http.str.ptr);
1608 LIST_INIT(&rule->arg.http.fmt);
Christopher Faulet81e20172019-12-12 16:40:30 +01001609
1610 cur_arg++;
Christopher Faulet1337b322020-01-14 14:50:55 +01001611 if (!(rule->arg.http.re = regex_comp(args[cur_arg], 1, 1, err))) {
Tim Duesterhused526372020-03-05 17:56:33 +01001612 istfree(&rule->arg.http.str);
Christopher Faulet81e20172019-12-12 16:40:30 +01001613 return ACT_RET_PRS_ERR;
Christopher Faulet1337b322020-01-14 14:50:55 +01001614 }
Christopher Faulet81e20172019-12-12 16:40:30 +01001615
1616 if (rule->from == ACT_F_HTTP_REQ) {
1617 px->conf.args.ctx = ARGC_HRQ;
1618 cap = (px->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR;
1619 }
1620 else{
1621 px->conf.args.ctx = ARGC_HRS;
1622 cap = (px->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR;
1623 }
1624
1625 cur_arg++;
Christopher Faulet1337b322020-01-14 14:50:55 +01001626 if (!parse_logformat_string(args[cur_arg], px, &rule->arg.http.fmt, LOG_OPT_HTTP, cap, err)) {
Tim Duesterhused526372020-03-05 17:56:33 +01001627 istfree(&rule->arg.http.str);
Christopher Faulet1337b322020-01-14 14:50:55 +01001628 regex_free(rule->arg.http.re);
Christopher Faulet81e20172019-12-12 16:40:30 +01001629 return ACT_RET_PRS_ERR;
Christopher Faulet1337b322020-01-14 14:50:55 +01001630 }
Christopher Faulet81e20172019-12-12 16:40:30 +01001631
1632 free(px->conf.lfs_file);
1633 px->conf.lfs_file = strdup(px->conf.args.file);
1634 px->conf.lfs_line = px->conf.args.line;
1635
1636 *orig_arg = cur_arg + 1;
1637 return ACT_RET_PRS_OK;
1638}
1639
Maciej Zdebebdd4c52020-11-20 13:58:48 +00001640/* This function executes a del-header action with selected matching mode for
1641 * header name. It finds the matching method to be performed in <.action>, previously
1642 * filled by function parse_http_del_header(). On success, it returns ACT_RET_CONT.
1643 * Otherwise ACT_RET_ERR is returned.
1644 */
1645static enum act_return http_action_del_header(struct act_rule *rule, struct proxy *px,
1646 struct session *sess, struct stream *s, int flags)
1647{
1648 struct http_hdr_ctx ctx;
1649 struct http_msg *msg = ((rule->from == ACT_F_HTTP_REQ) ? &s->txn->req : &s->txn->rsp);
1650 struct htx *htx = htxbuf(&msg->chn->buf);
1651 enum act_return ret = ACT_RET_CONT;
1652
1653 /* remove all occurrences of the header */
1654 ctx.blk = NULL;
1655 switch (rule->action) {
1656 case PAT_MATCH_STR:
1657 while (http_find_header(htx, rule->arg.http.str, &ctx, 1))
1658 http_remove_header(htx, &ctx);
1659 break;
1660 case PAT_MATCH_BEG:
1661 while (http_find_pfx_header(htx, rule->arg.http.str, &ctx, 1))
1662 http_remove_header(htx, &ctx);
1663 break;
1664 case PAT_MATCH_END:
1665 while (http_find_sfx_header(htx, rule->arg.http.str, &ctx, 1))
1666 http_remove_header(htx, &ctx);
1667 break;
1668 case PAT_MATCH_SUB:
1669 while (http_find_sub_header(htx, rule->arg.http.str, &ctx, 1))
1670 http_remove_header(htx, &ctx);
1671 break;
1672 case PAT_MATCH_REG:
1673 while (http_match_header(htx, rule->arg.http.re, &ctx, 1))
1674 http_remove_header(htx, &ctx);
1675 break;
1676 default:
1677 return ACT_RET_ERR;
1678 }
1679 return ret;
1680}
1681
1682/* Parse a "del-header" action. It takes string as a required argument,
1683 * optional flag (currently only -m) and optional matching method of input string
1684 * with header name to be deleted. Default matching method is exact match (-m str).
1685 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Christopher Faulet81e20172019-12-12 16:40:30 +01001686 */
1687static enum act_parse_ret parse_http_del_header(const char **args, int *orig_arg, struct proxy *px,
1688 struct act_rule *rule, char **err)
1689{
1690 int cur_arg;
Maciej Zdebebdd4c52020-11-20 13:58:48 +00001691 int pat_idx;
Christopher Faulet81e20172019-12-12 16:40:30 +01001692
Maciej Zdebebdd4c52020-11-20 13:58:48 +00001693 /* set exact matching (-m str) as default */
1694 rule->action = PAT_MATCH_STR;
1695 rule->action_ptr = http_action_del_header;
Christopher Faulet2eb53962020-01-14 14:47:34 +01001696 rule->release_ptr = release_http_action;
Christopher Faulet81e20172019-12-12 16:40:30 +01001697
1698 cur_arg = *orig_arg;
1699 if (!*args[cur_arg]) {
Maciej Zdebebdd4c52020-11-20 13:58:48 +00001700 memprintf(err, "expects at least 1 argument");
Christopher Faulet81e20172019-12-12 16:40:30 +01001701 return ACT_RET_PRS_ERR;
1702 }
1703
Christopher Faulet96bff762019-12-17 13:46:18 +01001704 rule->arg.http.str.ptr = strdup(args[cur_arg]);
1705 rule->arg.http.str.len = strlen(rule->arg.http.str.ptr);
Christopher Faulet81e20172019-12-12 16:40:30 +01001706 px->conf.args.ctx = (rule->from == ACT_F_HTTP_REQ ? ARGC_HRQ : ARGC_HRS);
1707
Maciej Zdeb6dee9962020-11-23 16:03:09 +00001708 LIST_INIT(&rule->arg.http.fmt);
Maciej Zdebebdd4c52020-11-20 13:58:48 +00001709 if (strcmp(args[cur_arg+1], "-m") == 0) {
1710 cur_arg++;
1711 if (!*args[cur_arg+1]) {
1712 memprintf(err, "-m flag expects exactly 1 argument");
1713 return ACT_RET_PRS_ERR;
1714 }
1715
1716 cur_arg++;
1717 pat_idx = pat_find_match_name(args[cur_arg]);
1718 switch (pat_idx) {
1719 case PAT_MATCH_REG:
1720 if (!(rule->arg.http.re = regex_comp(rule->arg.http.str.ptr, 1, 1, err)))
1721 return ACT_RET_PRS_ERR;
1722 /* fall through */
1723 case PAT_MATCH_STR:
1724 case PAT_MATCH_BEG:
1725 case PAT_MATCH_END:
1726 case PAT_MATCH_SUB:
1727 rule->action = pat_idx;
1728 break;
1729 default:
1730 memprintf(err, "-m with unsupported matching method '%s'", args[cur_arg]);
1731 return ACT_RET_PRS_ERR;
1732 }
1733 }
1734
Christopher Faulet81e20172019-12-12 16:40:30 +01001735 *orig_arg = cur_arg + 1;
1736 return ACT_RET_PRS_OK;
1737}
1738
Christopher Faulet2eb53962020-01-14 14:47:34 +01001739/* Release memory allocated by an http redirect action. */
1740static void release_http_redir(struct act_rule *rule)
1741{
1742 struct logformat_node *lf, *lfb;
1743 struct redirect_rule *redir;
1744
1745 redir = rule->arg.redir;
Willy Tarreau2b718102021-04-21 07:32:39 +02001746 LIST_DELETE(&redir->list);
Christopher Faulet2eb53962020-01-14 14:47:34 +01001747 if (redir->cond) {
1748 prune_acl_cond(redir->cond);
1749 free(redir->cond);
1750 }
1751 free(redir->rdr_str);
1752 free(redir->cookie_str);
1753 list_for_each_entry_safe(lf, lfb, &redir->rdr_fmt, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001754 LIST_DELETE(&lf->list);
Christopher Faulet2eb53962020-01-14 14:47:34 +01001755 free(lf);
1756 }
1757 free(redir);
1758}
1759
Christopher Faulet81e20172019-12-12 16:40:30 +01001760/* Parse a "redirect" action. It returns ACT_RET_PRS_OK on success,
1761 * ACT_RET_PRS_ERR on error.
1762 */
1763static enum act_parse_ret parse_http_redirect(const char **args, int *orig_arg, struct proxy *px,
1764 struct act_rule *rule, char **err)
1765{
1766 struct redirect_rule *redir;
1767 int dir, cur_arg;
1768
1769 rule->action = ACT_HTTP_REDIR;
Christopher Faulet245cf792019-12-18 14:58:12 +01001770 rule->flags |= ACT_FLAG_FINAL;
Christopher Faulet2eb53962020-01-14 14:47:34 +01001771 rule->release_ptr = release_http_redir;
Christopher Faulet81e20172019-12-12 16:40:30 +01001772
1773 cur_arg = *orig_arg;
1774
1775 dir = (rule->from == ACT_F_HTTP_REQ ? 0 : 1);
1776 if ((redir = http_parse_redirect_rule(px->conf.args.file, px->conf.args.line, px, &args[cur_arg], err, 1, dir)) == NULL)
1777 return ACT_RET_PRS_ERR;
1778
1779 rule->arg.redir = redir;
1780 rule->cond = redir->cond;
1781 redir->cond = NULL;
1782
1783 /* skip all arguments */
1784 while (*args[cur_arg])
1785 cur_arg++;
1786
1787 *orig_arg = cur_arg;
1788 return ACT_RET_PRS_OK;
1789}
1790
Christopher Faulet046cf442019-12-17 15:45:23 +01001791/* This function executes a add-acl, del-acl, set-map or del-map actions. On
1792 * success, it returns ACT_RET_CONT. Otherwsize ACT_RET_ERR is returned.
1793 */
1794static enum act_return http_action_set_map(struct act_rule *rule, struct proxy *px,
1795 struct session *sess, struct stream *s, int flags)
1796{
1797 struct pat_ref *ref;
1798 struct buffer *key = NULL, *value = NULL;
1799 enum act_return ret = ACT_RET_CONT;
1800
1801 /* collect reference */
1802 ref = pat_ref_lookup(rule->arg.map.ref);
1803 if (!ref)
1804 goto leave;
1805
1806 /* allocate key */
1807 key = alloc_trash_chunk();
1808 if (!key)
1809 goto fail_alloc;
1810
1811 /* collect key */
1812 key->data = build_logline(s, key->area, key->size, &rule->arg.map.key);
1813 key->area[key->data] = '\0';
1814
1815 switch (rule->action) {
1816 case 0: // add-acl
1817 /* add entry only if it does not already exist */
1818 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
1819 if (pat_ref_find_elt(ref, key->area) == NULL)
1820 pat_ref_add(ref, key->area, NULL, NULL);
1821 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
1822 break;
1823
1824 case 1: // set-map
1825 /* allocate value */
1826 value = alloc_trash_chunk();
1827 if (!value)
1828 goto fail_alloc;
1829
1830 /* collect value */
1831 value->data = build_logline(s, value->area, value->size, &rule->arg.map.value);
1832 value->area[value->data] = '\0';
1833
1834 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
1835 if (pat_ref_find_elt(ref, key->area) != NULL) {
1836 /* update entry if it exists */
1837 pat_ref_set(ref, key->area, value->area, NULL);
1838 }
1839 else {
1840 /* insert a new entry */
1841 pat_ref_add(ref, key->area, value->area, NULL);
1842 }
1843 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
1844 break;
1845
1846 case 2: // del-acl
1847 case 3: // del-map
1848 /* returned code: 1=ok, 0=ko */
1849 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
1850 pat_ref_delete(ref, key->area);
1851 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
1852 break;
1853
1854 default:
1855 ret = ACT_RET_ERR;
1856 }
1857
1858
1859 leave:
1860 free_trash_chunk(key);
1861 free_trash_chunk(value);
1862 return ret;
1863
1864 fail_alloc:
1865 if (!(s->flags & SF_ERR_MASK))
1866 s->flags |= SF_ERR_RESOURCE;
1867 ret = ACT_RET_ERR;
1868 goto leave;
1869}
1870
Christopher Faulet2eb53962020-01-14 14:47:34 +01001871/* Release memory allocated by an http map/acl action. */
1872static void release_http_map(struct act_rule *rule)
1873{
1874 struct logformat_node *lf, *lfb;
1875
1876 free(rule->arg.map.ref);
1877 list_for_each_entry_safe(lf, lfb, &rule->arg.map.key, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001878 LIST_DELETE(&lf->list);
Christopher Faulet2eb53962020-01-14 14:47:34 +01001879 release_sample_expr(lf->expr);
1880 free(lf->arg);
1881 free(lf);
1882 }
1883 if (rule->action == 1) {
1884 list_for_each_entry_safe(lf, lfb, &rule->arg.map.value, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001885 LIST_DELETE(&lf->list);
Christopher Faulet2eb53962020-01-14 14:47:34 +01001886 release_sample_expr(lf->expr);
1887 free(lf->arg);
1888 free(lf);
1889 }
1890 }
1891}
1892
Christopher Faulet81e20172019-12-12 16:40:30 +01001893/* Parse a "add-acl", "del-acl", "set-map" or "del-map" actions. It takes one or
Christopher Faulet046cf442019-12-17 15:45:23 +01001894 * two log-format string as argument depending on the action. The action is
1895 * stored in <.action> as an int (0=add-acl, 1=set-map, 2=del-acl,
1896 * 3=del-map). It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Christopher Faulet81e20172019-12-12 16:40:30 +01001897 */
1898static enum act_parse_ret parse_http_set_map(const char **args, int *orig_arg, struct proxy *px,
1899 struct act_rule *rule, char **err)
1900{
1901 int cap, cur_arg;
1902
Christopher Faulet046cf442019-12-17 15:45:23 +01001903 if (args[*orig_arg-1][0] == 'a') // add-acl
1904 rule->action = 0;
1905 else if (args[*orig_arg-1][0] == 's') // set-map
1906 rule->action = 1;
1907 else if (args[*orig_arg-1][4] == 'a') // del-acl
1908 rule->action = 2;
1909 else if (args[*orig_arg-1][4] == 'm') // del-map
1910 rule->action = 3;
1911 else {
1912 memprintf(err, "internal error: unhandled action '%s'", args[0]);
1913 return ACT_RET_PRS_ERR;
1914 }
1915 rule->action_ptr = http_action_set_map;
Christopher Faulet2eb53962020-01-14 14:47:34 +01001916 rule->release_ptr = release_http_map;
Christopher Faulet81e20172019-12-12 16:40:30 +01001917
1918 cur_arg = *orig_arg;
Christopher Faulet046cf442019-12-17 15:45:23 +01001919 if (rule->action == 1 && (!*args[cur_arg] || !*args[cur_arg+1])) {
1920 /* 2 args for set-map */
Christopher Faulet81e20172019-12-12 16:40:30 +01001921 memprintf(err, "expects exactly 2 arguments");
1922 return ACT_RET_PRS_ERR;
1923 }
1924 else if (!*args[cur_arg]) {
Christopher Faulet046cf442019-12-17 15:45:23 +01001925 /* only one arg for other actions */
Christopher Faulet81e20172019-12-12 16:40:30 +01001926 memprintf(err, "expects exactly 1 arguments");
1927 return ACT_RET_PRS_ERR;
1928 }
1929
1930 /*
1931 * '+ 8' for 'set-map(' (same for del-map)
1932 * '- 9' for 'set-map(' + trailing ')' (same for del-map)
1933 */
1934 rule->arg.map.ref = my_strndup(args[cur_arg-1] + 8, strlen(args[cur_arg-1]) - 9);
1935
1936 if (rule->from == ACT_F_HTTP_REQ) {
1937 px->conf.args.ctx = ARGC_HRQ;
1938 cap = (px->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR;
1939 }
1940 else{
1941 px->conf.args.ctx = ARGC_HRS;
1942 cap = (px->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR;
1943 }
1944
1945 /* key pattern */
1946 LIST_INIT(&rule->arg.map.key);
Christopher Faulet1337b322020-01-14 14:50:55 +01001947 if (!parse_logformat_string(args[cur_arg], px, &rule->arg.map.key, LOG_OPT_HTTP, cap, err)) {
1948 free(rule->arg.map.ref);
Christopher Faulet81e20172019-12-12 16:40:30 +01001949 return ACT_RET_PRS_ERR;
Christopher Faulet1337b322020-01-14 14:50:55 +01001950 }
Christopher Faulet81e20172019-12-12 16:40:30 +01001951
Christopher Faulet046cf442019-12-17 15:45:23 +01001952 if (rule->action == 1) {
Christopher Faulet81e20172019-12-12 16:40:30 +01001953 /* value pattern for set-map only */
1954 cur_arg++;
1955 LIST_INIT(&rule->arg.map.value);
Christopher Faulet1337b322020-01-14 14:50:55 +01001956 if (!parse_logformat_string(args[cur_arg], px, &rule->arg.map.value, LOG_OPT_HTTP, cap, err)) {
1957 free(rule->arg.map.ref);
Christopher Faulet81e20172019-12-12 16:40:30 +01001958 return ACT_RET_PRS_ERR;
Christopher Faulet1337b322020-01-14 14:50:55 +01001959 }
Christopher Faulet81e20172019-12-12 16:40:30 +01001960 }
1961
1962 free(px->conf.lfs_file);
1963 px->conf.lfs_file = strdup(px->conf.args.file);
1964 px->conf.lfs_line = px->conf.args.line;
1965
1966 *orig_arg = cur_arg + 1;
1967 return ACT_RET_PRS_OK;
1968}
1969
Christopher Fauletac98d812019-12-18 09:20:16 +01001970/* This function executes a track-sc* actions. On success, it returns
1971 * ACT_RET_CONT. Otherwsize ACT_RET_ERR is returned.
1972 */
1973static enum act_return http_action_track_sc(struct act_rule *rule, struct proxy *px,
1974 struct session *sess, struct stream *s, int flags)
1975{
1976 struct stktable *t;
1977 struct stksess *ts;
1978 struct stktable_key *key;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001979 void *ptr1, *ptr2, *ptr3, *ptr4, *ptr5, *ptr6;
Christopher Fauletac98d812019-12-18 09:20:16 +01001980 int opt;
1981
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001982 ptr1 = ptr2 = ptr3 = ptr4 = ptr5 = ptr6 = NULL;
Christopher Fauletac98d812019-12-18 09:20:16 +01001983 opt = ((rule->from == ACT_F_HTTP_REQ) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES) | SMP_OPT_FINAL;
1984
1985 t = rule->arg.trk_ctr.table.t;
Emeric Brun362d25e2021-03-10 16:58:03 +01001986
1987 if (stkctr_entry(&s->stkctr[rule->action]))
1988 goto end;
1989
Christopher Fauletac98d812019-12-18 09:20:16 +01001990 key = stktable_fetch_key(t, s->be, sess, s, opt, rule->arg.trk_ctr.expr, NULL);
1991
1992 if (!key)
1993 goto end;
1994 ts = stktable_get_entry(t, key);
1995 if (!ts)
1996 goto end;
1997
1998 stream_track_stkctr(&s->stkctr[rule->action], t, ts);
1999
2000 /* let's count a new HTTP request as it's the first time we do it */
2001 ptr1 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
2002 ptr2 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
2003
2004 /* When the client triggers a 4xx from the server, it's most often due
2005 * to a missing object or permission. These events should be tracked
2006 * because if they happen often, it may indicate a brute force or a
2007 * vulnerability scan. Normally this is done when receiving the response
2008 * but here we're tracking after this ought to have been done so we have
2009 * to do it on purpose.
2010 */
2011 if (rule->from == ACT_F_HTTP_RES && (unsigned)(s->txn->status - 400) < 100) {
2012 ptr3 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_CNT);
2013 ptr4 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_RATE);
2014 }
2015
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002016 if (rule->from == ACT_F_HTTP_RES && (unsigned)(s->txn->status - 500) < 100 &&
2017 s->txn->status != 501 && s->txn->status != 505) {
2018 ptr5 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_FAIL_CNT);
2019 ptr6 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_FAIL_RATE);
2020 }
2021
2022 if (ptr1 || ptr2 || ptr3 || ptr4 || ptr5 || ptr6) {
Christopher Fauletac98d812019-12-18 09:20:16 +01002023 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
2024
2025 if (ptr1)
Emeric Brun0e3457b2021-06-30 17:18:28 +02002026 stktable_data_cast(ptr1, std_t_uint)++;
Christopher Fauletac98d812019-12-18 09:20:16 +01002027 if (ptr2)
Emeric Brun0e3457b2021-06-30 17:18:28 +02002028 update_freq_ctr_period(&stktable_data_cast(ptr2, std_t_frqp),
Christopher Fauletac98d812019-12-18 09:20:16 +01002029 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
2030 if (ptr3)
Emeric Brun0e3457b2021-06-30 17:18:28 +02002031 stktable_data_cast(ptr3, std_t_uint)++;
Christopher Fauletac98d812019-12-18 09:20:16 +01002032 if (ptr4)
Emeric Brun0e3457b2021-06-30 17:18:28 +02002033 update_freq_ctr_period(&stktable_data_cast(ptr4, std_t_frqp),
Christopher Fauletac98d812019-12-18 09:20:16 +01002034 t->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1);
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002035 if (ptr5)
Emeric Brun0e3457b2021-06-30 17:18:28 +02002036 stktable_data_cast(ptr5, std_t_uint)++;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002037 if (ptr6)
Emeric Brun0e3457b2021-06-30 17:18:28 +02002038 update_freq_ctr_period(&stktable_data_cast(ptr6, std_t_frqp),
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002039 t->data_arg[STKTABLE_DT_HTTP_FAIL_RATE].u, 1);
Christopher Fauletac98d812019-12-18 09:20:16 +01002040
2041 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
2042
2043 /* If data was modified, we need to touch to re-schedule sync */
2044 stktable_touch_local(t, ts, 0);
2045 }
2046
2047 stkctr_set_flags(&s->stkctr[rule->action], STKCTR_TRACK_CONTENT);
2048 if (sess->fe != s->be)
2049 stkctr_set_flags(&s->stkctr[rule->action], STKCTR_TRACK_BACKEND);
2050
2051 end:
2052 return ACT_RET_CONT;
2053}
Christopher Faulet81e20172019-12-12 16:40:30 +01002054
Christopher Faulet2eb53962020-01-14 14:47:34 +01002055static void release_http_track_sc(struct act_rule *rule)
2056{
2057 release_sample_expr(rule->arg.trk_ctr.expr);
2058}
2059
Christopher Faulet81e20172019-12-12 16:40:30 +01002060/* Parse a "track-sc*" actions. It returns ACT_RET_PRS_OK on success,
2061 * ACT_RET_PRS_ERR on error.
2062 */
2063static enum act_parse_ret parse_http_track_sc(const char **args, int *orig_arg, struct proxy *px,
2064 struct act_rule *rule, char **err)
2065{
2066 struct sample_expr *expr;
2067 unsigned int where;
2068 unsigned int tsc_num;
2069 const char *tsc_num_str;
2070 int cur_arg;
2071
2072 tsc_num_str = &args[*orig_arg-1][8];
2073 if (cfg_parse_track_sc_num(&tsc_num, tsc_num_str, tsc_num_str + strlen(tsc_num_str), err) == -1)
2074 return ACT_RET_PRS_ERR;
2075
2076 cur_arg = *orig_arg;
2077 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line,
Willy Tarreaue3b57bf2020-02-14 16:50:14 +01002078 err, &px->conf.args, NULL);
Christopher Faulet81e20172019-12-12 16:40:30 +01002079 if (!expr)
2080 return ACT_RET_PRS_ERR;
2081
2082 where = 0;
2083 if (px->cap & PR_CAP_FE)
2084 where |= (rule->from == ACT_F_HTTP_REQ ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_FE_HRS_HDR);
2085 if (px->cap & PR_CAP_BE)
2086 where |= (rule->from == ACT_F_HTTP_REQ ? SMP_VAL_BE_HRQ_HDR : SMP_VAL_BE_HRS_HDR);
2087
2088 if (!(expr->fetch->val & where)) {
2089 memprintf(err, "fetch method '%s' extracts information from '%s', none of which is available here",
2090 args[cur_arg-1], sample_src_names(expr->fetch->use));
Christopher Faulet1337b322020-01-14 14:50:55 +01002091 release_sample_expr(expr);
Christopher Faulet81e20172019-12-12 16:40:30 +01002092 return ACT_RET_PRS_ERR;
2093 }
2094
2095 if (strcmp(args[cur_arg], "table") == 0) {
2096 cur_arg++;
2097 if (!*args[cur_arg]) {
2098 memprintf(err, "missing table name");
Christopher Faulet1337b322020-01-14 14:50:55 +01002099 release_sample_expr(expr);
Christopher Faulet81e20172019-12-12 16:40:30 +01002100 return ACT_RET_PRS_ERR;
2101 }
2102
2103 /* we copy the table name for now, it will be resolved later */
2104 rule->arg.trk_ctr.table.n = strdup(args[cur_arg]);
2105 cur_arg++;
2106 }
2107
Christopher Fauletac98d812019-12-18 09:20:16 +01002108 rule->action = tsc_num;
Christopher Faulet81e20172019-12-12 16:40:30 +01002109 rule->arg.trk_ctr.expr = expr;
Christopher Fauletac98d812019-12-18 09:20:16 +01002110 rule->action_ptr = http_action_track_sc;
Christopher Faulet2eb53962020-01-14 14:47:34 +01002111 rule->release_ptr = release_http_track_sc;
Christopher Faulet81e20172019-12-12 16:40:30 +01002112 rule->check_ptr = check_trk_action;
2113
2114 *orig_arg = cur_arg;
2115 return ACT_RET_PRS_OK;
2116}
2117
Amaury Denoyelle8d228232020-12-10 13:43:54 +01002118static enum act_return action_timeout_set_stream_timeout(struct act_rule *rule,
2119 struct proxy *px,
2120 struct session *sess,
2121 struct stream *s,
2122 int flags)
2123{
2124 struct sample *key;
2125
2126 if (rule->arg.timeout.expr) {
2127 key = sample_fetch_as_type(px, sess, s, SMP_OPT_FINAL, rule->arg.timeout.expr, SMP_T_SINT);
2128 if (!key)
2129 return ACT_RET_CONT;
2130
2131 stream_set_timeout(s, rule->arg.timeout.type, MS_TO_TICKS(key->data.u.sint));
2132 }
2133 else {
2134 stream_set_timeout(s, rule->arg.timeout.type, MS_TO_TICKS(rule->arg.timeout.value));
2135 }
2136
2137 return ACT_RET_CONT;
2138}
2139
2140/* Parse a "set-timeout" action. Returns ACT_RET_PRS_ERR if parsing error.
2141 */
2142static enum act_parse_ret parse_http_set_timeout(const char **args,
2143 int *orig_arg,
2144 struct proxy *px,
2145 struct act_rule *rule, char **err)
2146{
2147 int cur_arg;
2148
2149 rule->action = ACT_CUSTOM;
2150 rule->action_ptr = action_timeout_set_stream_timeout;
2151 rule->release_ptr = release_timeout_action;
2152
2153 cur_arg = *orig_arg;
2154 if (!*args[cur_arg] || !*args[cur_arg + 1]) {
2155 memprintf(err, "expects exactly 2 arguments");
2156 return ACT_RET_PRS_ERR;
2157 }
2158
2159 if (!(px->cap & PR_CAP_BE)) {
2160 memprintf(err, "proxy '%s' has no backend capability", px->id);
2161 return ACT_RET_PRS_ERR;
2162 }
2163
2164 if (cfg_parse_rule_set_timeout(args, cur_arg,
2165 &rule->arg.timeout.value,
2166 &rule->arg.timeout.type,
2167 &rule->arg.timeout.expr,
2168 err,
2169 px->conf.args.file,
2170 px->conf.args.line, &px->conf.args) == -1) {
2171 return ACT_RET_PRS_ERR;
2172 }
2173
2174 *orig_arg = cur_arg + 2;
2175
2176 return ACT_RET_PRS_OK;
2177}
2178
Christopher Faulet46f95542019-12-20 10:07:22 +01002179/* This function executes a strict-mode actions. On success, it always returns
2180 * ACT_RET_CONT
2181 */
2182static enum act_return http_action_strict_mode(struct act_rule *rule, struct proxy *px,
2183 struct session *sess, struct stream *s, int flags)
2184{
2185 struct http_msg *msg = ((rule->from == ACT_F_HTTP_REQ) ? &s->txn->req : &s->txn->rsp);
2186
2187 if (rule->action == 0) // strict-mode on
2188 msg->flags &= ~HTTP_MSGF_SOFT_RW;
2189 else // strict-mode off
2190 msg->flags |= HTTP_MSGF_SOFT_RW;
2191 return ACT_RET_CONT;
2192}
2193
2194/* Parse a "strict-mode" action. It returns ACT_RET_PRS_OK on success,
2195 * ACT_RET_PRS_ERR on error.
2196 */
2197static enum act_parse_ret parse_http_strict_mode(const char **args, int *orig_arg, struct proxy *px,
2198 struct act_rule *rule, char **err)
2199{
2200 int cur_arg;
2201
Christopher Faulet46f95542019-12-20 10:07:22 +01002202 cur_arg = *orig_arg;
2203 if (!*args[cur_arg]) {
2204 memprintf(err, "expects exactly 1 arguments");
2205 return ACT_RET_PRS_ERR;
2206 }
2207
2208 if (strcasecmp(args[cur_arg], "on") == 0)
2209 rule->action = 0; // strict-mode on
2210 else if (strcasecmp(args[cur_arg], "off") == 0)
2211 rule->action = 1; // strict-mode off
2212 else {
2213 memprintf(err, "Unexpected value '%s'. Only 'on' and 'off' are supported", args[cur_arg]);
2214 return ACT_RET_PRS_ERR;
2215 }
2216 rule->action_ptr = http_action_strict_mode;
2217
2218 *orig_arg = cur_arg + 1;
2219 return ACT_RET_PRS_OK;
2220}
2221
Christopher Faulet24231ab2020-01-24 17:44:23 +01002222/* This function executes a return action. It builds an HTX message from an
2223 * errorfile, an raw file or a log-format string, depending on <.action>
2224 * value. On success, it returns ACT_RET_ABRT. If an error occurs ACT_RET_ERR is
2225 * returned.
2226 */
2227static enum act_return http_action_return(struct act_rule *rule, struct proxy *px,
2228 struct session *sess, struct stream *s, int flags)
2229{
2230 struct channel *req = &s->req;
Christopher Faulet24231ab2020-01-24 17:44:23 +01002231
Christopher Faulet2d36df22021-02-19 11:41:01 +01002232 s->txn->status = rule->arg.http_reply->status;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02002233 if (http_reply_message(s, rule->arg.http_reply) == -1)
2234 return ACT_RET_ERR;
Christopher Faulet24231ab2020-01-24 17:44:23 +01002235
Christopher Faulet24231ab2020-01-24 17:44:23 +01002236 if (rule->from == ACT_F_HTTP_REQ) {
2237 /* let's log the request time */
2238 s->logs.tv_request = now;
2239 req->analysers &= AN_REQ_FLT_END;
2240
2241 if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Willy Tarreau4781b152021-04-06 13:53:36 +02002242 _HA_ATOMIC_INC(&s->sess->fe->fe_counters.intercepted_req);
Christopher Faulet24231ab2020-01-24 17:44:23 +01002243 }
2244
2245 if (!(s->flags & SF_ERR_MASK))
2246 s->flags |= SF_ERR_LOCAL;
2247 if (!(s->flags & SF_FINST_MASK))
2248 s->flags |= ((rule->from == ACT_F_HTTP_REQ) ? SF_FINST_R : SF_FINST_H);
2249
Christopher Faulet0e2ad612020-05-13 16:38:37 +02002250 return ACT_RET_ABRT;
Christopher Faulet24231ab2020-01-24 17:44:23 +01002251}
2252
Christopher Faulet24231ab2020-01-24 17:44:23 +01002253/* Parse a "return" action. It returns ACT_RET_PRS_OK on success,
Christopher Faulet47e791e2020-05-13 14:36:55 +02002254 * ACT_RET_PRS_ERR on error. It relies on http_parse_http_reply() to set
2255 * <.arg.http_reply>.
Christopher Faulet24231ab2020-01-24 17:44:23 +01002256 */
2257static enum act_parse_ret parse_http_return(const char **args, int *orig_arg, struct proxy *px,
2258 struct act_rule *rule, char **err)
2259{
Christopher Faulet47e791e2020-05-13 14:36:55 +02002260 /* Prepare parsing of log-format strings */
2261 px->conf.args.ctx = ((rule->from == ACT_F_HTTP_REQ) ? ARGC_HRQ : ARGC_HRS);
2262 rule->arg.http_reply = http_parse_http_reply(args, orig_arg, px, 200, err);
2263 if (!rule->arg.http_reply)
2264 return ACT_RET_PRS_ERR;
Christopher Faulet24231ab2020-01-24 17:44:23 +01002265
Christopher Fauletba946bf2020-05-13 08:50:07 +02002266 rule->flags |= ACT_FLAG_FINAL;
Christopher Faulet5ff0c642020-05-12 18:33:37 +02002267 rule->action = ACT_CUSTOM;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002268 rule->check_ptr = check_act_http_reply;
Christopher Faulet24231ab2020-01-24 17:44:23 +01002269 rule->action_ptr = http_action_return;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002270 rule->release_ptr = release_act_http_reply;
Christopher Faulet24231ab2020-01-24 17:44:23 +01002271 return ACT_RET_PRS_OK;
Christopher Faulet24231ab2020-01-24 17:44:23 +01002272}
2273
Christopher Faulet021a8e42021-03-29 10:46:38 +02002274
2275
2276/* This function executes a wait-for-body action. It waits for the message
2277 * payload for a max configured time (.arg.p[0]) and eventually for only first
2278 * <arg.p[1]> bytes (0 means no limit). It relies on http_wait_for_msg_body()
2279 * function. it returns ACT_RET_CONT when conditions are met to stop to wait.
2280 * Otherwise ACT_RET_YIELD is returned to wait for more data. ACT_RET_INV is
2281 * returned if a parsing error is raised by lower level and ACT_RET_ERR if an
Ilya Shipitsinb2be9a12021-04-24 13:25:42 +05002282 * internal error occurred. Finally ACT_RET_ABRT is returned when a timeout
2283 * occurred.
Christopher Faulet021a8e42021-03-29 10:46:38 +02002284 */
2285static enum act_return http_action_wait_for_body(struct act_rule *rule, struct proxy *px,
2286 struct session *sess, struct stream *s, int flags)
2287{
2288 struct channel *chn = ((rule->from == ACT_F_HTTP_REQ) ? &s->req : &s->res);
2289 unsigned int time = (uintptr_t)rule->arg.act.p[0];
2290 unsigned int bytes = (uintptr_t)rule->arg.act.p[1];
2291
2292 switch (http_wait_for_msg_body(s, chn, time, bytes)) {
2293 case HTTP_RULE_RES_CONT:
2294 return ACT_RET_CONT;
2295 case HTTP_RULE_RES_YIELD:
2296 return ACT_RET_YIELD;
2297 case HTTP_RULE_RES_BADREQ:
2298 return ACT_RET_INV;
2299 case HTTP_RULE_RES_ERROR:
2300 return ACT_RET_ERR;
2301 case HTTP_RULE_RES_ABRT:
2302 return ACT_RET_ABRT;
2303 default:
2304 return ACT_RET_ERR;
2305 }
2306}
2307
2308/* Parse a "wait-for-body" action. It returns ACT_RET_PRS_OK on success,
2309 * ACT_RET_PRS_ERR on error.
2310 */
2311static enum act_parse_ret parse_http_wait_for_body(const char **args, int *orig_arg, struct proxy *px,
2312 struct act_rule *rule, char **err)
2313{
2314 int cur_arg;
2315 unsigned int time, bytes;
2316 const char *res;
2317
2318 cur_arg = *orig_arg;
2319 if (!*args[cur_arg]) {
2320 memprintf(err, "expects time <time> [ at-least <bytes> ]");
2321 return ACT_RET_PRS_ERR;
2322 }
2323
2324 time = UINT_MAX; /* To be sure it is set */
2325 bytes = 0; /* Default value, wait all the body */
2326 while (*(args[cur_arg])) {
2327 if (strcmp(args[cur_arg], "time") == 0) {
2328 if (!*args[cur_arg + 1]) {
2329 memprintf(err, "missing argument for '%s'", args[cur_arg]);
2330 return ACT_RET_PRS_ERR;
2331 }
2332 res = parse_time_err(args[cur_arg+1], &time, TIME_UNIT_MS);
2333 if (res == PARSE_TIME_OVER) {
2334 memprintf(err, "time overflow (maximum value is 2147483647 ms or ~24.8 days)");
2335 return ACT_RET_PRS_ERR;
2336 }
2337 if (res == PARSE_TIME_UNDER) {
2338 memprintf(err, "time underflow (minimum non-null value is 1 ms)");
2339 return ACT_RET_PRS_ERR;
2340 }
2341 if (res) {
2342 memprintf(err, "unexpected character '%c'", *res);
2343 return ACT_RET_PRS_ERR;
2344 }
2345 cur_arg++;
2346 }
2347 else if (strcmp(args[cur_arg], "at-least") == 0) {
2348 if (!*args[cur_arg + 1]) {
2349 memprintf(err, "missing argument for '%s'", args[cur_arg]);
2350 return ACT_RET_PRS_ERR;
2351 }
2352 res = parse_size_err(args[cur_arg+1], &bytes);
2353 if (res) {
2354 memprintf(err, "unexpected character '%c'", *res);
2355 return ACT_RET_PRS_ERR;
2356 }
2357 cur_arg++;
2358 }
2359 else
2360 break;
2361 cur_arg++;
2362 }
2363
2364 if (time == UINT_MAX) {
2365 memprintf(err, "expects time <time> [ at-least <bytes> ]");
2366 return ACT_RET_PRS_ERR;
2367 }
2368
2369 rule->arg.act.p[0] = (void *)(uintptr_t)time;
2370 rule->arg.act.p[1] = (void *)(uintptr_t)bytes;
2371
2372 *orig_arg = cur_arg;
2373
2374 rule->action = ACT_CUSTOM;
2375 rule->action_ptr = http_action_wait_for_body;
2376 return ACT_RET_PRS_OK;
2377}
2378
Willy Tarreau79e57332018-10-02 16:01:16 +02002379/************************************************************************/
2380/* All supported http-request action keywords must be declared here. */
2381/************************************************************************/
2382
2383static struct action_kw_list http_req_actions = {
2384 .kw = {
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02002385 { "add-acl", parse_http_set_map, KWF_MATCH_PREFIX },
Christopher Faulet81e20172019-12-12 16:40:30 +01002386 { "add-header", parse_http_set_header, 0 },
2387 { "allow", parse_http_allow, 0 },
2388 { "auth", parse_http_auth, 0 },
2389 { "capture", parse_http_req_capture, 0 },
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02002390 { "del-acl", parse_http_set_map, KWF_MATCH_PREFIX },
Christopher Faulet81e20172019-12-12 16:40:30 +01002391 { "del-header", parse_http_del_header, 0 },
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02002392 { "del-map", parse_http_set_map, KWF_MATCH_PREFIX },
Christopher Faulete0fca292020-01-13 21:49:03 +01002393 { "deny", parse_http_deny, 0 },
Christopher Faulet81e20172019-12-12 16:40:30 +01002394 { "disable-l7-retry", parse_http_req_disable_l7_retry, 0 },
2395 { "early-hint", parse_http_set_header, 0 },
Amaury Denoyellea9e639a2021-05-06 15:50:12 +02002396 { "normalize-uri", parse_http_normalize_uri, KWF_EXPERIMENTAL },
Christopher Faulet81e20172019-12-12 16:40:30 +01002397 { "redirect", parse_http_redirect, 0 },
2398 { "reject", parse_http_action_reject, 0 },
2399 { "replace-header", parse_http_replace_header, 0 },
2400 { "replace-path", parse_replace_uri, 0 },
Christopher Faulet312294f2020-09-02 17:17:44 +02002401 { "replace-pathq", parse_replace_uri, 0 },
Christopher Faulet81e20172019-12-12 16:40:30 +01002402 { "replace-uri", parse_replace_uri, 0 },
2403 { "replace-value", parse_http_replace_header, 0 },
Christopher Faulet24231ab2020-01-24 17:44:23 +01002404 { "return", parse_http_return, 0 },
Christopher Faulet81e20172019-12-12 16:40:30 +01002405 { "set-header", parse_http_set_header, 0 },
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02002406 { "set-map", parse_http_set_map, KWF_MATCH_PREFIX },
Christopher Faulet81e20172019-12-12 16:40:30 +01002407 { "set-method", parse_set_req_line, 0 },
Christopher Faulet81e20172019-12-12 16:40:30 +01002408 { "set-path", parse_set_req_line, 0 },
Christopher Faulet312294f2020-09-02 17:17:44 +02002409 { "set-pathq", parse_set_req_line, 0 },
Christopher Faulet81e20172019-12-12 16:40:30 +01002410 { "set-query", parse_set_req_line, 0 },
Christopher Faulet81e20172019-12-12 16:40:30 +01002411 { "set-uri", parse_set_req_line, 0 },
Christopher Faulet46f95542019-12-20 10:07:22 +01002412 { "strict-mode", parse_http_strict_mode, 0 },
Christopher Faulete0fca292020-01-13 21:49:03 +01002413 { "tarpit", parse_http_deny, 0 },
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02002414 { "track-sc", parse_http_track_sc, KWF_MATCH_PREFIX },
Amaury Denoyelle8d228232020-12-10 13:43:54 +01002415 { "set-timeout", parse_http_set_timeout, 0 },
Christopher Faulet021a8e42021-03-29 10:46:38 +02002416 { "wait-for-body", parse_http_wait_for_body, 0 },
Willy Tarreau79e57332018-10-02 16:01:16 +02002417 { NULL, NULL }
2418 }
2419};
2420
Willy Tarreau0108d902018-11-25 19:14:37 +01002421INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_actions);
2422
Willy Tarreau79e57332018-10-02 16:01:16 +02002423static struct action_kw_list http_res_actions = {
2424 .kw = {
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02002425 { "add-acl", parse_http_set_map, KWF_MATCH_PREFIX },
Christopher Faulet81e20172019-12-12 16:40:30 +01002426 { "add-header", parse_http_set_header, 0 },
2427 { "allow", parse_http_allow, 0 },
2428 { "capture", parse_http_res_capture, 0 },
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02002429 { "del-acl", parse_http_set_map, KWF_MATCH_PREFIX },
Christopher Faulet81e20172019-12-12 16:40:30 +01002430 { "del-header", parse_http_del_header, 0 },
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02002431 { "del-map", parse_http_set_map, KWF_MATCH_PREFIX },
Christopher Faulete0fca292020-01-13 21:49:03 +01002432 { "deny", parse_http_deny, 0 },
Christopher Faulet81e20172019-12-12 16:40:30 +01002433 { "redirect", parse_http_redirect, 0 },
2434 { "replace-header", parse_http_replace_header, 0 },
2435 { "replace-value", parse_http_replace_header, 0 },
Christopher Faulet24231ab2020-01-24 17:44:23 +01002436 { "return", parse_http_return, 0 },
Christopher Faulet81e20172019-12-12 16:40:30 +01002437 { "set-header", parse_http_set_header, 0 },
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02002438 { "set-map", parse_http_set_map, KWF_MATCH_PREFIX },
Christopher Faulet81e20172019-12-12 16:40:30 +01002439 { "set-status", parse_http_set_status, 0 },
Christopher Faulet46f95542019-12-20 10:07:22 +01002440 { "strict-mode", parse_http_strict_mode, 0 },
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02002441 { "track-sc", parse_http_track_sc, KWF_MATCH_PREFIX },
Christopher Faulet021a8e42021-03-29 10:46:38 +02002442 { "wait-for-body", parse_http_wait_for_body, 0 },
Willy Tarreau79e57332018-10-02 16:01:16 +02002443 { NULL, NULL }
2444 }
2445};
2446
Willy Tarreau0108d902018-11-25 19:14:37 +01002447INITCALL1(STG_REGISTER, http_res_keywords_register, &http_res_actions);
Willy Tarreau79e57332018-10-02 16:01:16 +02002448
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002449static struct action_kw_list http_after_res_actions = {
2450 .kw = {
2451 { "add-header", parse_http_set_header, 0 },
2452 { "allow", parse_http_allow, 0 },
2453 { "del-header", parse_http_del_header, 0 },
2454 { "replace-header", parse_http_replace_header, 0 },
2455 { "replace-value", parse_http_replace_header, 0 },
2456 { "set-header", parse_http_set_header, 0 },
2457 { "set-status", parse_http_set_status, 0 },
2458 { "strict-mode", parse_http_strict_mode, 0 },
2459 { NULL, NULL }
2460 }
2461};
2462
2463INITCALL1(STG_REGISTER, http_after_res_keywords_register, &http_after_res_actions);
2464
Willy Tarreau79e57332018-10-02 16:01:16 +02002465/*
2466 * Local variables:
2467 * c-indent-level: 8
2468 * c-basic-offset: 8
2469 * End:
2470 */