blob: aa8dfff495e4f2927beb023fb62fd15afaca8059 [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
Christopher Faulet81e20172019-12-12 16:40:30 +010019#include <common/cfgparse.h>
Willy Tarreau79e57332018-10-02 16:01:16 +020020#include <common/chunk.h>
21#include <common/compat.h>
22#include <common/config.h>
23#include <common/debug.h>
24#include <common/http.h>
Willy Tarreau0108d902018-11-25 19:14:37 +010025#include <common/initcall.h>
Willy Tarreau79e57332018-10-02 16:01:16 +020026#include <common/memory.h>
27#include <common/standard.h>
28#include <common/version.h>
29
30#include <types/capture.h>
31#include <types/global.h>
32
33#include <proto/acl.h>
34#include <proto/arg.h>
Christopher Faulet81e20172019-12-12 16:40:30 +010035#include <proto/action.h>
Willy Tarreau61c112a2018-10-02 16:43:32 +020036#include <proto/http_rules.h>
Willy Tarreau33810222019-06-12 17:44:02 +020037#include <proto/http_htx.h>
Willy Tarreau79e57332018-10-02 16:01:16 +020038#include <proto/log.h>
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020039#include <proto/http_ana.h>
Willy Tarreau0f9cd7b2019-01-31 19:02:43 +010040#include <proto/stream_interface.h>
Willy Tarreau79e57332018-10-02 16:01:16 +020041
42
43/* This function executes one of the set-{method,path,query,uri} actions. It
44 * builds a string in the trash from the specified format string. It finds
Christopher Faulet2c22a692019-12-18 15:39:56 +010045 * the action to be performed in <.action>, previously filled by function
Willy Tarreau79e57332018-10-02 16:01:16 +020046 * parse_set_req_line(). The replacement action is excuted by the function
Christopher Faulete00d06c2019-12-16 17:18:42 +010047 * http_action_set_req_line(). On success, it returns ACT_RET_CONT. If an error
48 * occurs while soft rewrites are enabled, the action is canceled, but the rule
49 * processing continue. Otherwsize ACT_RET_ERR is returned.
Willy Tarreau79e57332018-10-02 16:01:16 +020050 */
51static enum act_return http_action_set_req_line(struct act_rule *rule, struct proxy *px,
52 struct session *sess, struct stream *s, int flags)
53{
54 struct buffer *replace;
Christopher Faulet13403762019-12-13 09:01:57 +010055 enum act_return ret = ACT_RET_CONT;
Willy Tarreau79e57332018-10-02 16:01:16 +020056
57 replace = alloc_trash_chunk();
58 if (!replace)
Christopher Faulete00d06c2019-12-16 17:18:42 +010059 goto fail_alloc;
Willy Tarreau79e57332018-10-02 16:01:16 +020060
61 /* If we have to create a query string, prepare a '?'. */
Christopher Faulet2c22a692019-12-18 15:39:56 +010062 if (rule->action == 2) // set-query
Willy Tarreau79e57332018-10-02 16:01:16 +020063 replace->area[replace->data++] = '?';
64 replace->data += build_logline(s, replace->area + replace->data,
65 replace->size - replace->data,
Christopher Faulet96bff762019-12-17 13:46:18 +010066 &rule->arg.http.fmt);
Willy Tarreau79e57332018-10-02 16:01:16 +020067
Christopher Faulet2c22a692019-12-18 15:39:56 +010068 if (http_req_replace_stline(rule->action, replace->area, replace->data, px, s) == -1)
Christopher Faulete00d06c2019-12-16 17:18:42 +010069 goto fail_rewrite;
Willy Tarreau79e57332018-10-02 16:01:16 +020070
Christopher Faulete00d06c2019-12-16 17:18:42 +010071 leave:
Willy Tarreau79e57332018-10-02 16:01:16 +020072 free_trash_chunk(replace);
73 return ret;
Christopher Faulete00d06c2019-12-16 17:18:42 +010074
75 fail_alloc:
76 if (!(s->flags & SF_ERR_MASK))
77 s->flags |= SF_ERR_RESOURCE;
78 ret = ACT_RET_ERR;
79 goto leave;
80
81 fail_rewrite:
82 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_rewrites, 1);
83 if (s->flags & SF_BE_ASSIGNED)
84 _HA_ATOMIC_ADD(&s->be->be_counters.failed_rewrites, 1);
85 if (sess->listener->counters)
86 _HA_ATOMIC_ADD(&sess->listener->counters->failed_rewrites, 1);
87 if (objt_server(s->target))
88 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_rewrites, 1);
89
90 if (!(s->txn->req.flags & HTTP_MSGF_SOFT_RW))
91 ret = ACT_RET_ERR;
92 goto leave;
Willy Tarreau79e57332018-10-02 16:01:16 +020093}
94
95/* parse an http-request action among :
96 * set-method
97 * set-path
98 * set-query
99 * set-uri
100 *
101 * All of them accept a single argument of type string representing a log-format.
Christopher Faulet96bff762019-12-17 13:46:18 +0100102 * The resulting rule makes use of <http.fmt> to store the log-format list head,
Christopher Faulet2c22a692019-12-18 15:39:56 +0100103 * and <.action> to store the action type as an int (0=method, 1=path, 2=query,
104 * 3=uri). It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Willy Tarreau79e57332018-10-02 16:01:16 +0200105 */
106static enum act_parse_ret parse_set_req_line(const char **args, int *orig_arg, struct proxy *px,
107 struct act_rule *rule, char **err)
108{
109 int cur_arg = *orig_arg;
110
Willy Tarreau79e57332018-10-02 16:01:16 +0200111 switch (args[0][4]) {
112 case 'm' :
Christopher Faulet2c22a692019-12-18 15:39:56 +0100113 rule->action = 0; // set-method
Willy Tarreau79e57332018-10-02 16:01:16 +0200114 break;
115 case 'p' :
Christopher Faulet2c22a692019-12-18 15:39:56 +0100116 rule->action = 1; // set-path
Willy Tarreau79e57332018-10-02 16:01:16 +0200117 break;
118 case 'q' :
Christopher Faulet2c22a692019-12-18 15:39:56 +0100119 rule->action = 2; // set-query
Willy Tarreau79e57332018-10-02 16:01:16 +0200120 break;
121 case 'u' :
Christopher Faulet2c22a692019-12-18 15:39:56 +0100122 rule->action = 3; // set-uri
Willy Tarreau79e57332018-10-02 16:01:16 +0200123 break;
124 default:
125 memprintf(err, "internal error: unhandled action '%s'", args[0]);
126 return ACT_RET_PRS_ERR;
127 }
Christopher Faulet96bff762019-12-17 13:46:18 +0100128 rule->action_ptr = http_action_set_req_line;
Willy Tarreau79e57332018-10-02 16:01:16 +0200129
130 if (!*args[cur_arg] ||
131 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
132 memprintf(err, "expects exactly 1 argument <format>");
133 return ACT_RET_PRS_ERR;
134 }
135
Christopher Faulet96bff762019-12-17 13:46:18 +0100136 LIST_INIT(&rule->arg.http.fmt);
Willy Tarreau79e57332018-10-02 16:01:16 +0200137 px->conf.args.ctx = ARGC_HRQ;
Christopher Faulet96bff762019-12-17 13:46:18 +0100138 if (!parse_logformat_string(args[cur_arg], px, &rule->arg.http.fmt, LOG_OPT_HTTP,
Willy Tarreau79e57332018-10-02 16:01:16 +0200139 (px->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, err)) {
140 return ACT_RET_PRS_ERR;
141 }
142
143 (*orig_arg)++;
144 return ACT_RET_PRS_OK;
145}
146
Willy Tarreau33810222019-06-12 17:44:02 +0200147/* This function executes a replace-uri action. It finds its arguments in
Christopher Faulet96bff762019-12-17 13:46:18 +0100148 * <rule>.arg.http. It builds a string in the trash from the format string
Willy Tarreau33810222019-06-12 17:44:02 +0200149 * previously filled by function parse_replace_uri() and will execute the regex
Christopher Faulet96bff762019-12-17 13:46:18 +0100150 * in <http.re> to replace the URI. It uses the format string present in
Christopher Faulet2c22a692019-12-18 15:39:56 +0100151 * <http.fmt>. The component to act on (path/uri) is taken from <.action> which
Christopher Faulet96bff762019-12-17 13:46:18 +0100152 * contains 1 for the path or 3 for the URI (values used by
153 * http_req_replace_stline()). On success, it returns ACT_RET_CONT. If an error
154 * occurs while soft rewrites are enabled, the action is canceled, but the rule
155 * processing continue. Otherwsize ACT_RET_ERR is returned.
Willy Tarreau33810222019-06-12 17:44:02 +0200156 */
157static enum act_return http_action_replace_uri(struct act_rule *rule, struct proxy *px,
158 struct session *sess, struct stream *s, int flags)
159{
Christopher Faulet13403762019-12-13 09:01:57 +0100160 enum act_return ret = ACT_RET_CONT;
Willy Tarreau33810222019-06-12 17:44:02 +0200161 struct buffer *replace, *output;
162 struct ist uri;
163 int len;
164
165 replace = alloc_trash_chunk();
166 output = alloc_trash_chunk();
167 if (!replace || !output)
Christopher Faulete00d06c2019-12-16 17:18:42 +0100168 goto fail_alloc;
Christopher Faulet12c28b62019-07-15 16:30:24 +0200169 uri = htx_sl_req_uri(http_get_stline(htxbuf(&s->req.buf)));
Willy Tarreau262c3f12019-12-17 06:52:51 +0100170
Christopher Faulet2c22a692019-12-18 15:39:56 +0100171 if (rule->action == 1) // replace-path
Christopher Faulet96bff762019-12-17 13:46:18 +0100172 uri = http_get_path(uri);
Willy Tarreau262c3f12019-12-17 06:52:51 +0100173
Christopher Faulet96bff762019-12-17 13:46:18 +0100174 if (!regex_exec_match2(rule->arg.http.re, uri.ptr, uri.len, MAX_MATCH, pmatch, 0))
Willy Tarreau33810222019-06-12 17:44:02 +0200175 goto leave;
176
Christopher Faulet96bff762019-12-17 13:46:18 +0100177 replace->data = build_logline(s, replace->area, replace->size, &rule->arg.http.fmt);
Willy Tarreau33810222019-06-12 17:44:02 +0200178
179 /* note: uri.ptr doesn't need to be zero-terminated because it will
180 * only be used to pick pmatch references.
181 */
182 len = exp_replace(output->area, output->size, uri.ptr, replace->area, pmatch);
183 if (len == -1)
Christopher Faulete00d06c2019-12-16 17:18:42 +0100184 goto fail_rewrite;
Willy Tarreau33810222019-06-12 17:44:02 +0200185
Christopher Faulet2c22a692019-12-18 15:39:56 +0100186 if (http_req_replace_stline(rule->action, output->area, len, px, s) == -1)
Christopher Faulete00d06c2019-12-16 17:18:42 +0100187 goto fail_rewrite;
Willy Tarreau33810222019-06-12 17:44:02 +0200188
Christopher Faulete00d06c2019-12-16 17:18:42 +0100189 leave:
Willy Tarreau33810222019-06-12 17:44:02 +0200190 free_trash_chunk(output);
191 free_trash_chunk(replace);
192 return ret;
Christopher Faulete00d06c2019-12-16 17:18:42 +0100193
194 fail_alloc:
195 if (!(s->flags & SF_ERR_MASK))
196 s->flags |= SF_ERR_RESOURCE;
197 ret = ACT_RET_ERR;
198 goto leave;
199
200 fail_rewrite:
201 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_rewrites, 1);
202 if (s->flags & SF_BE_ASSIGNED)
203 _HA_ATOMIC_ADD(&s->be->be_counters.failed_rewrites, 1);
204 if (sess->listener->counters)
205 _HA_ATOMIC_ADD(&sess->listener->counters->failed_rewrites, 1);
206 if (objt_server(s->target))
207 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_rewrites, 1);
208
209 if (!(s->txn->req.flags & HTTP_MSGF_SOFT_RW))
210 ret = ACT_RET_ERR;
211 goto leave;
Willy Tarreau33810222019-06-12 17:44:02 +0200212}
213
Willy Tarreau262c3f12019-12-17 06:52:51 +0100214/* parse a "replace-uri" or "replace-path" http-request action.
Willy Tarreau33810222019-06-12 17:44:02 +0200215 * This action takes 2 arguments (a regex and a replacement format string).
Christopher Faulet2c22a692019-12-18 15:39:56 +0100216 * The resulting rule makes use of <.action> to store the action (1/3 for now),
Christopher Faulet96bff762019-12-17 13:46:18 +0100217 * <http.re> to store the compiled regex, and <http.fmt> to store the log-format
Willy Tarreau33810222019-06-12 17:44:02 +0200218 * list head. It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
219 */
220static enum act_parse_ret parse_replace_uri(const char **args, int *orig_arg, struct proxy *px,
221 struct act_rule *rule, char **err)
222{
223 int cur_arg = *orig_arg;
224 char *error = NULL;
225
Willy Tarreau262c3f12019-12-17 06:52:51 +0100226 if (strcmp(args[cur_arg-1], "replace-path") == 0)
Christopher Faulet2c22a692019-12-18 15:39:56 +0100227 rule->action = 1; // replace-path, same as set-path
Willy Tarreau262c3f12019-12-17 06:52:51 +0100228 else
Christopher Faulet2c22a692019-12-18 15:39:56 +0100229 rule->action = 3; // replace-uri, same as set-uri
Willy Tarreau262c3f12019-12-17 06:52:51 +0100230
Willy Tarreau33810222019-06-12 17:44:02 +0200231 rule->action_ptr = http_action_replace_uri;
232
233 if (!*args[cur_arg] || !*args[cur_arg+1] ||
234 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
235 memprintf(err, "expects exactly 2 arguments <match-regex> and <replace-format>");
236 return ACT_RET_PRS_ERR;
237 }
238
Christopher Faulet96bff762019-12-17 13:46:18 +0100239 if (!(rule->arg.http.re = regex_comp(args[cur_arg], 1, 1, &error))) {
Willy Tarreau33810222019-06-12 17:44:02 +0200240 memprintf(err, "failed to parse the regex : %s", error);
241 free(error);
242 return ACT_RET_PRS_ERR;
243 }
244
Christopher Faulet96bff762019-12-17 13:46:18 +0100245 LIST_INIT(&rule->arg.http.fmt);
Willy Tarreau33810222019-06-12 17:44:02 +0200246 px->conf.args.ctx = ARGC_HRQ;
Christopher Faulet96bff762019-12-17 13:46:18 +0100247 if (!parse_logformat_string(args[cur_arg + 1], px, &rule->arg.http.fmt, LOG_OPT_HTTP,
Willy Tarreau33810222019-06-12 17:44:02 +0200248 (px->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, err)) {
249 return ACT_RET_PRS_ERR;
250 }
251
252 (*orig_arg) += 2;
253 return ACT_RET_PRS_OK;
254}
255
Willy Tarreau79e57332018-10-02 16:01:16 +0200256/* This function is just a compliant action wrapper for "set-status". */
257static enum act_return action_http_set_status(struct act_rule *rule, struct proxy *px,
258 struct session *sess, struct stream *s, int flags)
259{
Christopher Faulet96bff762019-12-17 13:46:18 +0100260 if (http_res_set_status(rule->arg.http.i, rule->arg.http.str, s) == -1) {
Christopher Faulete00d06c2019-12-16 17:18:42 +0100261 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_rewrites, 1);
262 if (s->flags & SF_BE_ASSIGNED)
263 _HA_ATOMIC_ADD(&s->be->be_counters.failed_rewrites, 1);
264 if (sess->listener->counters)
265 _HA_ATOMIC_ADD(&sess->listener->counters->failed_rewrites, 1);
266 if (objt_server(s->target))
267 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_rewrites, 1);
268
269 if (!(s->txn->req.flags & HTTP_MSGF_SOFT_RW))
270 return ACT_RET_ERR;
271 }
272
Willy Tarreau79e57332018-10-02 16:01:16 +0200273 return ACT_RET_CONT;
274}
275
276/* parse set-status action:
277 * This action accepts a single argument of type int representing
278 * an http status code. It returns ACT_RET_PRS_OK on success,
279 * ACT_RET_PRS_ERR on error.
280 */
281static enum act_parse_ret parse_http_set_status(const char **args, int *orig_arg, struct proxy *px,
282 struct act_rule *rule, char **err)
283{
284 char *error;
285
286 rule->action = ACT_CUSTOM;
287 rule->action_ptr = action_http_set_status;
288
289 /* Check if an argument is available */
290 if (!*args[*orig_arg]) {
291 memprintf(err, "expects 1 argument: <status>; or 3 arguments: <status> reason <fmt>");
292 return ACT_RET_PRS_ERR;
293 }
294
295 /* convert status code as integer */
Christopher Faulet96bff762019-12-17 13:46:18 +0100296 rule->arg.http.i = strtol(args[*orig_arg], &error, 10);
297 if (*error != '\0' || rule->arg.http.i < 100 || rule->arg.http.i > 999) {
Willy Tarreau79e57332018-10-02 16:01:16 +0200298 memprintf(err, "expects an integer status code between 100 and 999");
299 return ACT_RET_PRS_ERR;
300 }
301
302 (*orig_arg)++;
303
304 /* set custom reason string */
Christopher Faulet96bff762019-12-17 13:46:18 +0100305 rule->arg.http.str = ist(NULL); // If null, we use the default reason for the status code.
Willy Tarreau79e57332018-10-02 16:01:16 +0200306 if (*args[*orig_arg] && strcmp(args[*orig_arg], "reason") == 0 &&
307 (*args[*orig_arg + 1] && strcmp(args[*orig_arg + 1], "if") != 0 && strcmp(args[*orig_arg + 1], "unless") != 0)) {
308 (*orig_arg)++;
Christopher Faulet96bff762019-12-17 13:46:18 +0100309 rule->arg.http.str.ptr = strdup(args[*orig_arg]);
310 rule->arg.http.str.len = strlen(rule->arg.http.str.ptr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200311 (*orig_arg)++;
312 }
313
314 return ACT_RET_PRS_OK;
315}
316
317/* This function executes the "reject" HTTP action. It clears the request and
318 * response buffer without sending any response. It can be useful as an HTTP
319 * alternative to the silent-drop action to defend against DoS attacks, and may
320 * also be used with HTTP/2 to close a connection instead of just a stream.
321 * The txn status is unchanged, indicating no response was sent. The termination
Christopher Faulet8f1aa772019-07-04 11:27:15 +0200322 * flags will indicate "PR". It always returns ACT_RET_DONE.
Willy Tarreau79e57332018-10-02 16:01:16 +0200323 */
324static enum act_return http_action_reject(struct act_rule *rule, struct proxy *px,
325 struct session *sess, struct stream *s, int flags)
326{
Willy Tarreau0f9cd7b2019-01-31 19:02:43 +0100327 si_must_kill_conn(chn_prod(&s->req));
Willy Tarreau79e57332018-10-02 16:01:16 +0200328 channel_abort(&s->req);
329 channel_abort(&s->res);
330 s->req.analysers = 0;
331 s->res.analysers = 0;
332
Olivier Houcharda798bf52019-03-08 18:52:00 +0100333 _HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
334 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Willy Tarreau79e57332018-10-02 16:01:16 +0200335 if (sess->listener && sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100336 _HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Willy Tarreau79e57332018-10-02 16:01:16 +0200337
338 if (!(s->flags & SF_ERR_MASK))
339 s->flags |= SF_ERR_PRXCOND;
340 if (!(s->flags & SF_FINST_MASK))
341 s->flags |= SF_FINST_R;
342
Christopher Faulet8f1aa772019-07-04 11:27:15 +0200343 return ACT_RET_DONE;
Willy Tarreau79e57332018-10-02 16:01:16 +0200344}
345
346/* parse the "reject" action:
347 * This action takes no argument and returns ACT_RET_PRS_OK on success,
348 * ACT_RET_PRS_ERR on error.
349 */
350static enum act_parse_ret parse_http_action_reject(const char **args, int *orig_arg, struct proxy *px,
351 struct act_rule *rule, char **err)
352{
353 rule->action = ACT_CUSTOM;
354 rule->action_ptr = http_action_reject;
355 return ACT_RET_PRS_OK;
356}
357
Olivier Houchard602bf7d2019-05-10 13:59:15 +0200358/* This function executes the "disable-l7-retry" HTTP action.
359 * It disables L7 retries (all retry except for a connection failure). This
360 * can be useful for example to avoid retrying on POST requests.
361 * It just removes the L7 retry flag on the stream_interface, and always
362 * return ACT_RET_CONT;
363 */
364static enum act_return http_req_disable_l7_retry(struct act_rule *rule, struct proxy *px,
365 struct session *sess, struct stream *s, int flags)
366{
367 struct stream_interface *si = &s->si[1];
368
369 /* In theory, the SI_FL_L7_RETRY flags isn't set at this point, but
370 * let's be future-proof and remove it anyway.
371 */
372 si->flags &= ~SI_FL_L7_RETRY;
373 si->flags |= SI_FL_D_L7_RETRY;
374 return ACT_RET_CONT;
375}
376
377/* parse the "disable-l7-retry" action:
378 * This action takes no argument and returns ACT_RET_PRS_OK on success,
379 * ACT_RET_PRS_ERR on error.
380 */
381static enum act_parse_ret parse_http_req_disable_l7_retry(const char **args,
382 int *orig_args, struct proxy *px,
383 struct act_rule *rule, char **err)
384{
385 rule->action = ACT_CUSTOM;
386 rule->action_ptr = http_req_disable_l7_retry;
387 return ACT_RET_PRS_OK;
388}
389
Willy Tarreau79e57332018-10-02 16:01:16 +0200390/* This function executes the "capture" action. It executes a fetch expression,
391 * turns the result into a string and puts it in a capture slot. It always
392 * returns 1. If an error occurs the action is cancelled, but the rule
393 * processing continues.
394 */
395static enum act_return http_action_req_capture(struct act_rule *rule, struct proxy *px,
396 struct session *sess, struct stream *s, int flags)
397{
398 struct sample *key;
399 struct cap_hdr *h = rule->arg.cap.hdr;
400 char **cap = s->req_cap;
401 int len;
402
403 key = sample_fetch_as_type(s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.cap.expr, SMP_T_STR);
404 if (!key)
405 return ACT_RET_CONT;
406
407 if (cap[h->index] == NULL)
408 cap[h->index] = pool_alloc(h->pool);
409
410 if (cap[h->index] == NULL) /* no more capture memory */
411 return ACT_RET_CONT;
412
413 len = key->data.u.str.data;
414 if (len > h->len)
415 len = h->len;
416
417 memcpy(cap[h->index], key->data.u.str.area, len);
418 cap[h->index][len] = 0;
419 return ACT_RET_CONT;
420}
421
422/* This function executes the "capture" action and store the result in a
423 * capture slot if exists. It executes a fetch expression, turns the result
424 * into a string and puts it in a capture slot. It always returns 1. If an
425 * error occurs the action is cancelled, but the rule processing continues.
426 */
427static enum act_return http_action_req_capture_by_id(struct act_rule *rule, struct proxy *px,
428 struct session *sess, struct stream *s, int flags)
429{
430 struct sample *key;
431 struct cap_hdr *h;
432 char **cap = s->req_cap;
433 struct proxy *fe = strm_fe(s);
434 int len;
435 int i;
436
437 /* Look for the original configuration. */
438 for (h = fe->req_cap, i = fe->nb_req_cap - 1;
439 h != NULL && i != rule->arg.capid.idx ;
440 i--, h = h->next);
441 if (!h)
442 return ACT_RET_CONT;
443
444 key = sample_fetch_as_type(s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.capid.expr, SMP_T_STR);
445 if (!key)
446 return ACT_RET_CONT;
447
448 if (cap[h->index] == NULL)
449 cap[h->index] = pool_alloc(h->pool);
450
451 if (cap[h->index] == NULL) /* no more capture memory */
452 return ACT_RET_CONT;
453
454 len = key->data.u.str.data;
455 if (len > h->len)
456 len = h->len;
457
458 memcpy(cap[h->index], key->data.u.str.area, len);
459 cap[h->index][len] = 0;
460 return ACT_RET_CONT;
461}
462
463/* Check an "http-request capture" action.
464 *
465 * The function returns 1 in success case, otherwise, it returns 0 and err is
466 * filled.
467 */
468static int check_http_req_capture(struct act_rule *rule, struct proxy *px, char **err)
469{
470 if (rule->action_ptr != http_action_req_capture_by_id)
471 return 1;
472
473 if (rule->arg.capid.idx >= px->nb_req_cap) {
474 memprintf(err, "unable to find capture id '%d' referenced by http-request capture rule",
475 rule->arg.capid.idx);
476 return 0;
477 }
478
479 return 1;
480}
481
482/* parse an "http-request capture" action. It takes a single argument which is
483 * a sample fetch expression. It stores the expression into arg->act.p[0] and
484 * the allocated hdr_cap struct or the preallocated "id" into arg->act.p[1].
485 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
486 */
487static enum act_parse_ret parse_http_req_capture(const char **args, int *orig_arg, struct proxy *px,
488 struct act_rule *rule, char **err)
489{
490 struct sample_expr *expr;
491 struct cap_hdr *hdr;
492 int cur_arg;
493 int len = 0;
494
495 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
496 if (strcmp(args[cur_arg], "if") == 0 ||
497 strcmp(args[cur_arg], "unless") == 0)
498 break;
499
500 if (cur_arg < *orig_arg + 3) {
501 memprintf(err, "expects <expression> [ 'len' <length> | id <idx> ]");
502 return ACT_RET_PRS_ERR;
503 }
504
505 cur_arg = *orig_arg;
506 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
507 if (!expr)
508 return ACT_RET_PRS_ERR;
509
510 if (!(expr->fetch->val & SMP_VAL_FE_HRQ_HDR)) {
511 memprintf(err,
512 "fetch method '%s' extracts information from '%s', none of which is available here",
513 args[cur_arg-1], sample_src_names(expr->fetch->use));
514 free(expr);
515 return ACT_RET_PRS_ERR;
516 }
517
518 if (!args[cur_arg] || !*args[cur_arg]) {
519 memprintf(err, "expects 'len or 'id'");
520 free(expr);
521 return ACT_RET_PRS_ERR;
522 }
523
524 if (strcmp(args[cur_arg], "len") == 0) {
525 cur_arg++;
526
527 if (!(px->cap & PR_CAP_FE)) {
528 memprintf(err, "proxy '%s' has no frontend capability", px->id);
529 return ACT_RET_PRS_ERR;
530 }
531
532 px->conf.args.ctx = ARGC_CAP;
533
534 if (!args[cur_arg]) {
535 memprintf(err, "missing length value");
536 free(expr);
537 return ACT_RET_PRS_ERR;
538 }
539 /* we copy the table name for now, it will be resolved later */
540 len = atoi(args[cur_arg]);
541 if (len <= 0) {
542 memprintf(err, "length must be > 0");
543 free(expr);
544 return ACT_RET_PRS_ERR;
545 }
546 cur_arg++;
547
Willy Tarreau79e57332018-10-02 16:01:16 +0200548 hdr = calloc(1, sizeof(*hdr));
549 hdr->next = px->req_cap;
550 hdr->name = NULL; /* not a header capture */
551 hdr->namelen = 0;
552 hdr->len = len;
553 hdr->pool = create_pool("caphdr", hdr->len + 1, MEM_F_SHARED);
554 hdr->index = px->nb_req_cap++;
555
556 px->req_cap = hdr;
557 px->to_log |= LW_REQHDR;
558
559 rule->action = ACT_CUSTOM;
560 rule->action_ptr = http_action_req_capture;
561 rule->arg.cap.expr = expr;
562 rule->arg.cap.hdr = hdr;
563 }
564
565 else if (strcmp(args[cur_arg], "id") == 0) {
566 int id;
567 char *error;
568
569 cur_arg++;
570
571 if (!args[cur_arg]) {
572 memprintf(err, "missing id value");
573 free(expr);
574 return ACT_RET_PRS_ERR;
575 }
576
577 id = strtol(args[cur_arg], &error, 10);
578 if (*error != '\0') {
579 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
580 free(expr);
581 return ACT_RET_PRS_ERR;
582 }
583 cur_arg++;
584
585 px->conf.args.ctx = ARGC_CAP;
586
587 rule->action = ACT_CUSTOM;
588 rule->action_ptr = http_action_req_capture_by_id;
589 rule->check_ptr = check_http_req_capture;
590 rule->arg.capid.expr = expr;
591 rule->arg.capid.idx = id;
592 }
593
594 else {
595 memprintf(err, "expects 'len' or 'id', found '%s'", args[cur_arg]);
596 free(expr);
597 return ACT_RET_PRS_ERR;
598 }
599
600 *orig_arg = cur_arg;
601 return ACT_RET_PRS_OK;
602}
603
604/* This function executes the "capture" action and store the result in a
605 * capture slot if exists. It executes a fetch expression, turns the result
606 * into a string and puts it in a capture slot. It always returns 1. If an
607 * error occurs the action is cancelled, but the rule processing continues.
608 */
609static enum act_return http_action_res_capture_by_id(struct act_rule *rule, struct proxy *px,
610 struct session *sess, struct stream *s, int flags)
611{
612 struct sample *key;
613 struct cap_hdr *h;
614 char **cap = s->res_cap;
615 struct proxy *fe = strm_fe(s);
616 int len;
617 int i;
618
619 /* Look for the original configuration. */
620 for (h = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
621 h != NULL && i != rule->arg.capid.idx ;
622 i--, h = h->next);
623 if (!h)
624 return ACT_RET_CONT;
625
626 key = sample_fetch_as_type(s->be, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL, rule->arg.capid.expr, SMP_T_STR);
627 if (!key)
628 return ACT_RET_CONT;
629
630 if (cap[h->index] == NULL)
631 cap[h->index] = pool_alloc(h->pool);
632
633 if (cap[h->index] == NULL) /* no more capture memory */
634 return ACT_RET_CONT;
635
636 len = key->data.u.str.data;
637 if (len > h->len)
638 len = h->len;
639
640 memcpy(cap[h->index], key->data.u.str.area, len);
641 cap[h->index][len] = 0;
642 return ACT_RET_CONT;
643}
644
645/* Check an "http-response capture" action.
646 *
647 * The function returns 1 in success case, otherwise, it returns 0 and err is
648 * filled.
649 */
650static int check_http_res_capture(struct act_rule *rule, struct proxy *px, char **err)
651{
652 if (rule->action_ptr != http_action_res_capture_by_id)
653 return 1;
654
655 if (rule->arg.capid.idx >= px->nb_rsp_cap) {
656 memprintf(err, "unable to find capture id '%d' referenced by http-response capture rule",
657 rule->arg.capid.idx);
658 return 0;
659 }
660
661 return 1;
662}
663
664/* parse an "http-response capture" action. It takes a single argument which is
665 * a sample fetch expression. It stores the expression into arg->act.p[0] and
666 * the allocated hdr_cap struct od the preallocated id into arg->act.p[1].
667 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
668 */
669static enum act_parse_ret parse_http_res_capture(const char **args, int *orig_arg, struct proxy *px,
670 struct act_rule *rule, char **err)
671{
672 struct sample_expr *expr;
673 int cur_arg;
674 int id;
675 char *error;
676
677 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
678 if (strcmp(args[cur_arg], "if") == 0 ||
679 strcmp(args[cur_arg], "unless") == 0)
680 break;
681
682 if (cur_arg < *orig_arg + 3) {
683 memprintf(err, "expects <expression> id <idx>");
684 return ACT_RET_PRS_ERR;
685 }
686
687 cur_arg = *orig_arg;
688 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
689 if (!expr)
690 return ACT_RET_PRS_ERR;
691
692 if (!(expr->fetch->val & SMP_VAL_FE_HRS_HDR)) {
693 memprintf(err,
694 "fetch method '%s' extracts information from '%s', none of which is available here",
695 args[cur_arg-1], sample_src_names(expr->fetch->use));
696 free(expr);
697 return ACT_RET_PRS_ERR;
698 }
699
700 if (!args[cur_arg] || !*args[cur_arg]) {
701 memprintf(err, "expects 'id'");
702 free(expr);
703 return ACT_RET_PRS_ERR;
704 }
705
706 if (strcmp(args[cur_arg], "id") != 0) {
707 memprintf(err, "expects 'id', found '%s'", args[cur_arg]);
708 free(expr);
709 return ACT_RET_PRS_ERR;
710 }
711
712 cur_arg++;
713
714 if (!args[cur_arg]) {
715 memprintf(err, "missing id value");
716 free(expr);
717 return ACT_RET_PRS_ERR;
718 }
719
720 id = strtol(args[cur_arg], &error, 10);
721 if (*error != '\0') {
722 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
723 free(expr);
724 return ACT_RET_PRS_ERR;
725 }
726 cur_arg++;
727
728 px->conf.args.ctx = ARGC_CAP;
729
730 rule->action = ACT_CUSTOM;
731 rule->action_ptr = http_action_res_capture_by_id;
732 rule->check_ptr = check_http_res_capture;
733 rule->arg.capid.expr = expr;
734 rule->arg.capid.idx = id;
735
736 *orig_arg = cur_arg;
737 return ACT_RET_PRS_OK;
738}
739
Christopher Faulet81e20172019-12-12 16:40:30 +0100740/* Parse a "allow" action for a request or a response rule. It takes no argument. It
741 * returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
742 */
743static enum act_parse_ret parse_http_allow(const char **args, int *orig_arg, struct proxy *px,
744 struct act_rule *rule, char **err)
745{
746 rule->action = ACT_ACTION_ALLOW;
Christopher Faulet245cf792019-12-18 14:58:12 +0100747 rule->flags |= ACT_FLAG_FINAL;
Christopher Faulet81e20172019-12-12 16:40:30 +0100748 return ACT_RET_PRS_OK;
749}
750
751/* Parse "deny" or "tarpit" actions for a request rule. It may take 2 optional arguments
752 * to define the status code. It returns ACT_RET_PRS_OK on success,
753 * ACT_RET_PRS_ERR on error.
754 */
755static enum act_parse_ret parse_http_req_deny(const char **args, int *orig_arg, struct proxy *px,
756 struct act_rule *rule, char **err)
757{
758 int code, hc, cur_arg;
759
760 cur_arg = *orig_arg;
761 if (!strcmp(args[cur_arg-1], "tarpit")) {
762 rule->action = ACT_HTTP_REQ_TARPIT;
Christopher Faulet96bff762019-12-17 13:46:18 +0100763 rule->arg.http.i = HTTP_ERR_500;
Christopher Faulet81e20172019-12-12 16:40:30 +0100764 }
765 else {
766 rule->action = ACT_ACTION_DENY;
Christopher Faulet96bff762019-12-17 13:46:18 +0100767 rule->arg.http.i = HTTP_ERR_403;
Christopher Faulet81e20172019-12-12 16:40:30 +0100768 }
Christopher Faulet245cf792019-12-18 14:58:12 +0100769 rule->flags |= ACT_FLAG_FINAL;
Christopher Faulet81e20172019-12-12 16:40:30 +0100770
771 if (strcmp(args[cur_arg], "deny_status") == 0) {
772 cur_arg++;
773 if (!*args[cur_arg]) {
774 memprintf(err, "missing status code.\n");
775 return ACT_RET_PRS_ERR;
776 }
777
778 code = atol(args[cur_arg]);
779 cur_arg++;
780 for (hc = 0; hc < HTTP_ERR_SIZE; hc++) {
781 if (http_err_codes[hc] == code) {
Christopher Faulet96bff762019-12-17 13:46:18 +0100782 rule->arg.http.i = hc;
Christopher Faulet81e20172019-12-12 16:40:30 +0100783 break;
784 }
785 }
786 if (hc >= HTTP_ERR_SIZE)
787 memprintf(err, "status code %d not handled, using default code %d",
Christopher Faulet96bff762019-12-17 13:46:18 +0100788 code, http_err_codes[rule->arg.http.i]);
Christopher Faulet81e20172019-12-12 16:40:30 +0100789 }
790
791 *orig_arg = cur_arg;
792 return ACT_RET_PRS_OK;
793}
794
795/* Parse a "deny" action for a response rule. It takes no argument. It returns
796 * ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
797 */
798static enum act_parse_ret parse_http_res_deny(const char **args, int *orig_arg, struct proxy *px,
799 struct act_rule *rule, char **err)
800{
801 rule->action = ACT_ACTION_DENY;
Christopher Faulet245cf792019-12-18 14:58:12 +0100802 rule->flags |= ACT_FLAG_FINAL;
Christopher Faulet81e20172019-12-12 16:40:30 +0100803 return ACT_RET_PRS_OK;
804}
805
806/* Parse a "auth" action. It may take 2 optional arguments to define a "realm"
807 * parameter. It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
808 */
809static enum act_parse_ret parse_http_auth(const char **args, int *orig_arg, struct proxy *px,
810 struct act_rule *rule, char **err)
811{
812 int cur_arg;
813
814 rule->action = ACT_HTTP_REQ_AUTH;
Christopher Faulet245cf792019-12-18 14:58:12 +0100815 rule->flags |= ACT_FLAG_FINAL;
Christopher Faulet81e20172019-12-12 16:40:30 +0100816
817 cur_arg = *orig_arg;
818 if (!strcmp(args[cur_arg], "realm")) {
819 cur_arg++;
820 if (!*args[cur_arg]) {
821 memprintf(err, "missing realm value.\n");
822 return ACT_RET_PRS_ERR;
823 }
Christopher Faulet96bff762019-12-17 13:46:18 +0100824 rule->arg.http.str.ptr = strdup(args[cur_arg]);
825 rule->arg.http.str.len = strlen(rule->arg.http.str.ptr);
Christopher Faulet81e20172019-12-12 16:40:30 +0100826 cur_arg++;
827 }
828
829 *orig_arg = cur_arg;
830 return ACT_RET_PRS_OK;
831}
832
833/* Parse a "set-nice" action. It takes the nice value as argument. It returns
834 * ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
835 */
836static enum act_parse_ret parse_http_set_nice(const char **args, int *orig_arg, struct proxy *px,
837 struct act_rule *rule, char **err)
838{
839 int cur_arg;
840
841 rule->action = ACT_HTTP_SET_NICE;
842
843 cur_arg = *orig_arg;
844 if (!*args[cur_arg]) {
845 memprintf(err, "expects exactly 1 argument (integer value)");
846 return ACT_RET_PRS_ERR;
847 }
Christopher Faulet96bff762019-12-17 13:46:18 +0100848 rule->arg.http.i = atoi(args[cur_arg]);
849 if (rule->arg.http.i < -1024)
850 rule->arg.http.i = -1024;
851 else if (rule->arg.http.i > 1024)
852 rule->arg.http.i = 1024;
Christopher Faulet81e20172019-12-12 16:40:30 +0100853
854 *orig_arg = cur_arg + 1;
855 return ACT_RET_PRS_OK;
856}
857
858/* Parse a "set-tos" action. It takes the TOS value as argument. It returns
859 * ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
860 */
861static enum act_parse_ret parse_http_set_tos(const char **args, int *orig_arg, struct proxy *px,
862 struct act_rule *rule, char **err)
863{
864#ifdef IP_TOS
865 char *endp;
866 int cur_arg;
867
868 rule->action = ACT_HTTP_SET_TOS;
869
870 cur_arg = *orig_arg;
871 if (!*args[cur_arg]) {
872 memprintf(err, "expects exactly 1 argument (integer/hex value)");
873 return ACT_RET_PRS_ERR;
874 }
Christopher Faulet96bff762019-12-17 13:46:18 +0100875 rule->arg.http.i = strtol(args[cur_arg], &endp, 0);
Christopher Faulet81e20172019-12-12 16:40:30 +0100876 if (endp && *endp != '\0') {
877 memprintf(err, "invalid character starting at '%s' (integer/hex value expected)", endp);
878 return ACT_RET_PRS_ERR;
879 }
880
881 *orig_arg = cur_arg + 1;
882 return ACT_RET_PRS_OK;
883#else
884 memprintf(err, "not supported on this platform (IP_TOS undefined)");
885 return ACT_RET_PRS_ERR;
886#endif
887}
888
889/* Parse a "set-mark" action. It takes the MARK value as argument. It returns
890 * ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
891 */
892static enum act_parse_ret parse_http_set_mark(const char **args, int *orig_arg, struct proxy *px,
893 struct act_rule *rule, char **err)
894{
895#ifdef SO_MARK
896 char *endp;
897 int cur_arg;
898
899 rule->action = ACT_HTTP_SET_MARK;
900
901 cur_arg = *orig_arg;
902 if (!*args[cur_arg]) {
903 memprintf(err, "expects exactly 1 argument (integer/hex value)");
904 return ACT_RET_PRS_ERR;
905 }
Christopher Faulet96bff762019-12-17 13:46:18 +0100906 rule->arg.http.i = strtoul(args[cur_arg], &endp, 0);
Christopher Faulet81e20172019-12-12 16:40:30 +0100907 if (endp && *endp != '\0') {
908 memprintf(err, "invalid character starting at '%s' (integer/hex value expected)", endp);
909 return ACT_RET_PRS_ERR;
910 }
911
912 *orig_arg = cur_arg + 1;
913 global.last_checks |= LSTCHK_NETADM;
914 return ACT_RET_PRS_OK;
915#else
916 memprintf(err, "not supported on this platform (SO_MARK undefined)");
917 return ACT_RET_PRS_ERR;
918#endif
919}
920
921/* Parse a "set-log-level" action. It takes the level value as argument. It
922 * returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
923 */
924static enum act_parse_ret parse_http_set_log_level(const char **args, int *orig_arg, struct proxy *px,
925 struct act_rule *rule, char **err)
926{
927 int cur_arg;
928
929 rule->action = ACT_HTTP_SET_LOGL;
930
931 cur_arg = *orig_arg;
932 if (!*args[cur_arg]) {
933 bad_log_level:
934 memprintf(err, "expects exactly 1 argument (log level name or 'silent')");
935 return ACT_RET_PRS_ERR;
936 }
937 if (strcmp(args[cur_arg], "silent") == 0)
Christopher Faulet96bff762019-12-17 13:46:18 +0100938 rule->arg.http.i = -1;
939 else if ((rule->arg.http.i = get_log_level(args[cur_arg]) + 1) == 0)
Christopher Faulet81e20172019-12-12 16:40:30 +0100940 goto bad_log_level;
941
942 *orig_arg = cur_arg + 1;
943 return ACT_RET_PRS_OK;
944}
945
Christopher Fauletd1f27e32019-12-17 09:33:38 +0100946/* This function executes a set-header or add-header actions. It builds a string
947 * in the trash from the specified format string. It finds the action to be
948 * performed in <.action>, previously filled by function parse_set_header(). The
949 * replacement action is excuted by the function http_action_set_header(). On
950 * success, it returns ACT_RET_CONT. If an error occurs while soft rewrites are
951 * enabled, the action is canceled, but the rule processing continue. Otherwsize
952 * ACT_RET_ERR is returned.
953 */
954static enum act_return http_action_set_header(struct act_rule *rule, struct proxy *px,
955 struct session *sess, struct stream *s, int flags)
956{
957 struct htx *htx = htxbuf((rule->from == ACT_F_HTTP_REQ) ? &s->req.buf : &s->res.buf);
958 enum act_return ret = ACT_RET_CONT;
959 struct buffer *replace;
960 struct http_hdr_ctx ctx;
961 struct ist n, v;
962
963 replace = alloc_trash_chunk();
964 if (!replace)
965 goto fail_alloc;
966
967 replace->data = build_logline(s, replace->area, replace->size, &rule->arg.http.fmt);
968 n = rule->arg.http.str;
969 v = ist2(replace->area, replace->data);
970
971 if (rule->action == 0) { // set-header
972 /* remove all occurrences of the header */
973 ctx.blk = NULL;
974 while (http_find_header(htx, n, &ctx, 1))
975 http_remove_header(htx, &ctx);
976 }
977
978 /* Now add header */
979 if (!http_add_header(htx, n, v))
980 goto fail_rewrite;
981
982 leave:
983 free_trash_chunk(replace);
984 return ret;
985
986 fail_alloc:
987 if (!(s->flags & SF_ERR_MASK))
988 s->flags |= SF_ERR_RESOURCE;
989 ret = ACT_RET_ERR;
990 goto leave;
991
992 fail_rewrite:
993 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_rewrites, 1);
994 if (s->flags & SF_BE_ASSIGNED)
995 _HA_ATOMIC_ADD(&s->be->be_counters.failed_rewrites, 1);
996 if (sess->listener->counters)
997 _HA_ATOMIC_ADD(&sess->listener->counters->failed_rewrites, 1);
998 if (objt_server(s->target))
999 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_rewrites, 1);
1000
1001 if (!(s->txn->req.flags & HTTP_MSGF_SOFT_RW))
1002 ret = ACT_RET_ERR;
1003 goto leave;
1004}
1005
Christopher Faulet81e20172019-12-12 16:40:30 +01001006/* Parse a "set-header", "add-header" or "early-hint" actions. It takes an
1007 * header name and a log-format string as arguments. It returns ACT_RET_PRS_OK
1008 * on success, ACT_RET_PRS_ERR on error.
1009 *
1010 * Note: same function is used for the request and the response. However
1011 * "early-hint" rules are only supported for request rules.
1012 */
1013static enum act_parse_ret parse_http_set_header(const char **args, int *orig_arg, struct proxy *px,
1014 struct act_rule *rule, char **err)
1015{
Christopher Faulet81e20172019-12-12 16:40:30 +01001016 int cap, cur_arg;
1017
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001018 if (args[*orig_arg-1][0] == 'e')
1019 rule->action = ACT_HTTP_EARLY_HINT;
1020 else {
1021 if (args[*orig_arg-1][0] == 's')
1022 rule->action = 0; // set-header
1023 else
1024 rule->action = 1; // add-header
1025 rule->action_ptr = http_action_set_header;
1026 }
Christopher Faulet81e20172019-12-12 16:40:30 +01001027
1028 cur_arg = *orig_arg;
1029 if (!*args[cur_arg] || !*args[cur_arg+1]) {
1030 memprintf(err, "expects exactly 2 arguments");
1031 return ACT_RET_PRS_ERR;
1032 }
1033
Christopher Faulet81e20172019-12-12 16:40:30 +01001034
Christopher Faulet96bff762019-12-17 13:46:18 +01001035 rule->arg.http.str.ptr = strdup(args[cur_arg]);
1036 rule->arg.http.str.len = strlen(rule->arg.http.str.ptr);
1037 LIST_INIT(&rule->arg.http.fmt);
Christopher Faulet81e20172019-12-12 16:40:30 +01001038
1039 if (rule->from == ACT_F_HTTP_REQ) {
1040 px->conf.args.ctx = ARGC_HRQ;
1041 cap = (px->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR;
1042 }
1043 else{
1044 px->conf.args.ctx = ARGC_HRS;
1045 cap = (px->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR;
1046 }
1047
1048 cur_arg++;
Christopher Faulet96bff762019-12-17 13:46:18 +01001049 if (!parse_logformat_string(args[cur_arg], px, &rule->arg.http.fmt, LOG_OPT_HTTP, cap, err))
Christopher Faulet81e20172019-12-12 16:40:30 +01001050 return ACT_RET_PRS_ERR;
1051
1052 free(px->conf.lfs_file);
1053 px->conf.lfs_file = strdup(px->conf.args.file);
1054 px->conf.lfs_line = px->conf.args.line;
1055
1056 *orig_arg = cur_arg + 1;
1057 return ACT_RET_PRS_OK;
1058}
1059
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001060/* This function executes a replace-header or replace-value actions. It
1061 * builds a string in the trash from the specified format string. It finds
1062 * the action to be performed in <.action>, previously filled by function
1063 * parse_replace_header(). The replacement action is excuted by the function
1064 * http_action_replace_header(). On success, it returns ACT_RET_CONT. If an error
1065 * occurs while soft rewrites are enabled, the action is canceled, but the rule
1066 * processing continue. Otherwsize ACT_RET_ERR is returned.
1067 */
1068static enum act_return http_action_replace_header(struct act_rule *rule, struct proxy *px,
1069 struct session *sess, struct stream *s, int flags)
1070{
1071 struct htx *htx = htxbuf((rule->from == ACT_F_HTTP_REQ) ? &s->req.buf : &s->res.buf);
1072 enum act_return ret = ACT_RET_CONT;
1073 struct buffer *replace;
1074 int r;
1075
1076 replace = alloc_trash_chunk();
1077 if (!replace)
1078 goto fail_alloc;
1079
1080 replace->data = build_logline(s, replace->area, replace->size, &rule->arg.http.fmt);
1081
1082 r = http_replace_hdrs(s, htx, rule->arg.http.str, replace->area, rule->arg.http.re, (rule->action == 0));
1083 if (r == -1)
1084 goto fail_rewrite;
1085
1086 leave:
1087 free_trash_chunk(replace);
1088 return ret;
1089
1090 fail_alloc:
1091 if (!(s->flags & SF_ERR_MASK))
1092 s->flags |= SF_ERR_RESOURCE;
1093 ret = ACT_RET_ERR;
1094 goto leave;
1095
1096 fail_rewrite:
1097 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_rewrites, 1);
1098 if (s->flags & SF_BE_ASSIGNED)
1099 _HA_ATOMIC_ADD(&s->be->be_counters.failed_rewrites, 1);
1100 if (sess->listener->counters)
1101 _HA_ATOMIC_ADD(&sess->listener->counters->failed_rewrites, 1);
1102 if (objt_server(s->target))
1103 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_rewrites, 1);
1104
1105 if (!(s->txn->req.flags & HTTP_MSGF_SOFT_RW))
1106 ret = ACT_RET_ERR;
1107 goto leave;
1108}
1109
Christopher Faulet81e20172019-12-12 16:40:30 +01001110/* Parse a "replace-header" or "replace-value" actions. It takes an header name,
1111 * a regex and replacement string as arguments. It returns ACT_RET_PRS_OK on
1112 * success, ACT_RET_PRS_ERR on error.
1113 */
1114static enum act_parse_ret parse_http_replace_header(const char **args, int *orig_arg, struct proxy *px,
1115 struct act_rule *rule, char **err)
1116{
1117 int cap, cur_arg;
1118
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001119 if (args[*orig_arg-1][8] == 'h')
1120 rule->action = 0; // replace-header
1121 else
1122 rule->action = 1; // replace-value
1123 rule->action_ptr = http_action_replace_header;
Christopher Faulet81e20172019-12-12 16:40:30 +01001124
1125 cur_arg = *orig_arg;
1126 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2]) {
1127 memprintf(err, "expects exactly 3 arguments");
1128 return ACT_RET_PRS_ERR;
1129 }
1130
Christopher Faulet96bff762019-12-17 13:46:18 +01001131 rule->arg.http.str.ptr = strdup(args[cur_arg]);
1132 rule->arg.http.str.len = strlen(rule->arg.http.str.ptr);
1133 LIST_INIT(&rule->arg.http.fmt);
Christopher Faulet81e20172019-12-12 16:40:30 +01001134
1135 cur_arg++;
Christopher Faulet96bff762019-12-17 13:46:18 +01001136 if (!(rule->arg.http.re = regex_comp(args[cur_arg], 1, 1, err)))
Christopher Faulet81e20172019-12-12 16:40:30 +01001137 return ACT_RET_PRS_ERR;
1138
1139 if (rule->from == ACT_F_HTTP_REQ) {
1140 px->conf.args.ctx = ARGC_HRQ;
1141 cap = (px->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR;
1142 }
1143 else{
1144 px->conf.args.ctx = ARGC_HRS;
1145 cap = (px->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR;
1146 }
1147
1148 cur_arg++;
Christopher Faulet96bff762019-12-17 13:46:18 +01001149 if (!parse_logformat_string(args[cur_arg], px, &rule->arg.http.fmt, LOG_OPT_HTTP, cap, err))
Christopher Faulet81e20172019-12-12 16:40:30 +01001150 return ACT_RET_PRS_ERR;
1151
1152 free(px->conf.lfs_file);
1153 px->conf.lfs_file = strdup(px->conf.args.file);
1154 px->conf.lfs_line = px->conf.args.line;
1155
1156 *orig_arg = cur_arg + 1;
1157 return ACT_RET_PRS_OK;
1158}
1159
1160/* Parse a "del-header" action. It takes an header name as argument. It returns
1161 * ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
1162 */
1163static enum act_parse_ret parse_http_del_header(const char **args, int *orig_arg, struct proxy *px,
1164 struct act_rule *rule, char **err)
1165{
1166 int cur_arg;
1167
1168 rule->action = ACT_HTTP_DEL_HDR;
1169
1170 cur_arg = *orig_arg;
1171 if (!*args[cur_arg]) {
1172 memprintf(err, "expects exactly 1 arguments");
1173 return ACT_RET_PRS_ERR;
1174 }
1175
Christopher Faulet96bff762019-12-17 13:46:18 +01001176 rule->arg.http.str.ptr = strdup(args[cur_arg]);
1177 rule->arg.http.str.len = strlen(rule->arg.http.str.ptr);
Christopher Faulet81e20172019-12-12 16:40:30 +01001178
1179 px->conf.args.ctx = (rule->from == ACT_F_HTTP_REQ ? ARGC_HRQ : ARGC_HRS);
1180
1181 *orig_arg = cur_arg + 1;
1182 return ACT_RET_PRS_OK;
1183}
1184
1185/* Parse a "redirect" action. It returns ACT_RET_PRS_OK on success,
1186 * ACT_RET_PRS_ERR on error.
1187 */
1188static enum act_parse_ret parse_http_redirect(const char **args, int *orig_arg, struct proxy *px,
1189 struct act_rule *rule, char **err)
1190{
1191 struct redirect_rule *redir;
1192 int dir, cur_arg;
1193
1194 rule->action = ACT_HTTP_REDIR;
Christopher Faulet245cf792019-12-18 14:58:12 +01001195 rule->flags |= ACT_FLAG_FINAL;
Christopher Faulet81e20172019-12-12 16:40:30 +01001196
1197 cur_arg = *orig_arg;
1198
1199 dir = (rule->from == ACT_F_HTTP_REQ ? 0 : 1);
1200 if ((redir = http_parse_redirect_rule(px->conf.args.file, px->conf.args.line, px, &args[cur_arg], err, 1, dir)) == NULL)
1201 return ACT_RET_PRS_ERR;
1202
1203 rule->arg.redir = redir;
1204 rule->cond = redir->cond;
1205 redir->cond = NULL;
1206
1207 /* skip all arguments */
1208 while (*args[cur_arg])
1209 cur_arg++;
1210
1211 *orig_arg = cur_arg;
1212 return ACT_RET_PRS_OK;
1213}
1214
1215/* Parse a "add-acl", "del-acl", "set-map" or "del-map" actions. It takes one or
1216 * two log-format string as argument depending on the action. It returns
1217 * ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
1218 */
1219static enum act_parse_ret parse_http_set_map(const char **args, int *orig_arg, struct proxy *px,
1220 struct act_rule *rule, char **err)
1221{
1222 int cap, cur_arg;
1223
1224 rule->action = (args[*orig_arg-1][0] == 'a' ? ACT_HTTP_ADD_ACL :
1225 (args[*orig_arg-1][0] == 's' ? ACT_HTTP_SET_MAP :
1226 (args[*orig_arg-1][4] == 'a' ? ACT_HTTP_DEL_ACL : ACT_HTTP_DEL_MAP)));
1227
1228 cur_arg = *orig_arg;
1229 if (rule->action == ACT_HTTP_SET_MAP && (!*args[cur_arg] || !*args[cur_arg+1])) {
1230 memprintf(err, "expects exactly 2 arguments");
1231 return ACT_RET_PRS_ERR;
1232 }
1233 else if (!*args[cur_arg]) {
1234 memprintf(err, "expects exactly 1 arguments");
1235 return ACT_RET_PRS_ERR;
1236 }
1237
1238 /*
1239 * '+ 8' for 'set-map(' (same for del-map)
1240 * '- 9' for 'set-map(' + trailing ')' (same for del-map)
1241 */
1242 rule->arg.map.ref = my_strndup(args[cur_arg-1] + 8, strlen(args[cur_arg-1]) - 9);
1243
1244 if (rule->from == ACT_F_HTTP_REQ) {
1245 px->conf.args.ctx = ARGC_HRQ;
1246 cap = (px->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR;
1247 }
1248 else{
1249 px->conf.args.ctx = ARGC_HRS;
1250 cap = (px->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR;
1251 }
1252
1253 /* key pattern */
1254 LIST_INIT(&rule->arg.map.key);
1255 if (!parse_logformat_string(args[cur_arg], px, &rule->arg.map.key, LOG_OPT_HTTP, cap, err))
1256 return ACT_RET_PRS_ERR;
1257
1258 if (rule->action == ACT_HTTP_SET_MAP) {
1259 /* value pattern for set-map only */
1260 cur_arg++;
1261 LIST_INIT(&rule->arg.map.value);
1262 if (!parse_logformat_string(args[cur_arg], px, &rule->arg.map.value, LOG_OPT_HTTP, cap, err))
1263 return ACT_RET_PRS_ERR;
1264 }
1265
1266 free(px->conf.lfs_file);
1267 px->conf.lfs_file = strdup(px->conf.args.file);
1268 px->conf.lfs_line = px->conf.args.line;
1269
1270 *orig_arg = cur_arg + 1;
1271 return ACT_RET_PRS_OK;
1272}
1273
1274
1275/* Parse a "track-sc*" actions. It returns ACT_RET_PRS_OK on success,
1276 * ACT_RET_PRS_ERR on error.
1277 */
1278static enum act_parse_ret parse_http_track_sc(const char **args, int *orig_arg, struct proxy *px,
1279 struct act_rule *rule, char **err)
1280{
1281 struct sample_expr *expr;
1282 unsigned int where;
1283 unsigned int tsc_num;
1284 const char *tsc_num_str;
1285 int cur_arg;
1286
1287 tsc_num_str = &args[*orig_arg-1][8];
1288 if (cfg_parse_track_sc_num(&tsc_num, tsc_num_str, tsc_num_str + strlen(tsc_num_str), err) == -1)
1289 return ACT_RET_PRS_ERR;
1290
1291 cur_arg = *orig_arg;
1292 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line,
1293 err, &px->conf.args);
1294 if (!expr)
1295 return ACT_RET_PRS_ERR;
1296
1297 where = 0;
1298 if (px->cap & PR_CAP_FE)
1299 where |= (rule->from == ACT_F_HTTP_REQ ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_FE_HRS_HDR);
1300 if (px->cap & PR_CAP_BE)
1301 where |= (rule->from == ACT_F_HTTP_REQ ? SMP_VAL_BE_HRQ_HDR : SMP_VAL_BE_HRS_HDR);
1302
1303 if (!(expr->fetch->val & where)) {
1304 memprintf(err, "fetch method '%s' extracts information from '%s', none of which is available here",
1305 args[cur_arg-1], sample_src_names(expr->fetch->use));
1306 return ACT_RET_PRS_ERR;
1307 }
1308
1309 if (strcmp(args[cur_arg], "table") == 0) {
1310 cur_arg++;
1311 if (!*args[cur_arg]) {
1312 memprintf(err, "missing table name");
1313 return ACT_RET_PRS_ERR;
1314 }
1315
1316 /* we copy the table name for now, it will be resolved later */
1317 rule->arg.trk_ctr.table.n = strdup(args[cur_arg]);
1318 cur_arg++;
1319 }
1320
1321 rule->arg.trk_ctr.expr = expr;
1322 rule->action = ACT_ACTION_TRK_SC0 + tsc_num;
1323 rule->check_ptr = check_trk_action;
1324
1325 *orig_arg = cur_arg;
1326 return ACT_RET_PRS_OK;
1327}
1328
Christopher Faulet46f95542019-12-20 10:07:22 +01001329/* This function executes a strict-mode actions. On success, it always returns
1330 * ACT_RET_CONT
1331 */
1332static enum act_return http_action_strict_mode(struct act_rule *rule, struct proxy *px,
1333 struct session *sess, struct stream *s, int flags)
1334{
1335 struct http_msg *msg = ((rule->from == ACT_F_HTTP_REQ) ? &s->txn->req : &s->txn->rsp);
1336
1337 if (rule->action == 0) // strict-mode on
1338 msg->flags &= ~HTTP_MSGF_SOFT_RW;
1339 else // strict-mode off
1340 msg->flags |= HTTP_MSGF_SOFT_RW;
1341 return ACT_RET_CONT;
1342}
1343
1344/* Parse a "strict-mode" action. It returns ACT_RET_PRS_OK on success,
1345 * ACT_RET_PRS_ERR on error.
1346 */
1347static enum act_parse_ret parse_http_strict_mode(const char **args, int *orig_arg, struct proxy *px,
1348 struct act_rule *rule, char **err)
1349{
1350 int cur_arg;
1351
1352
1353 cur_arg = *orig_arg;
1354 if (!*args[cur_arg]) {
1355 memprintf(err, "expects exactly 1 arguments");
1356 return ACT_RET_PRS_ERR;
1357 }
1358
1359 if (strcasecmp(args[cur_arg], "on") == 0)
1360 rule->action = 0; // strict-mode on
1361 else if (strcasecmp(args[cur_arg], "off") == 0)
1362 rule->action = 1; // strict-mode off
1363 else {
1364 memprintf(err, "Unexpected value '%s'. Only 'on' and 'off' are supported", args[cur_arg]);
1365 return ACT_RET_PRS_ERR;
1366 }
1367 rule->action_ptr = http_action_strict_mode;
1368
1369 *orig_arg = cur_arg + 1;
1370 return ACT_RET_PRS_OK;
1371}
1372
Willy Tarreau79e57332018-10-02 16:01:16 +02001373/************************************************************************/
1374/* All supported http-request action keywords must be declared here. */
1375/************************************************************************/
1376
1377static struct action_kw_list http_req_actions = {
1378 .kw = {
Christopher Faulet81e20172019-12-12 16:40:30 +01001379 { "add-acl", parse_http_set_map, 1 },
1380 { "add-header", parse_http_set_header, 0 },
1381 { "allow", parse_http_allow, 0 },
1382 { "auth", parse_http_auth, 0 },
1383 { "capture", parse_http_req_capture, 0 },
1384 { "del-acl", parse_http_set_map, 1 },
1385 { "del-header", parse_http_del_header, 0 },
1386 { "del-map", parse_http_set_map, 1 },
1387 { "deny", parse_http_req_deny, 0 },
1388 { "disable-l7-retry", parse_http_req_disable_l7_retry, 0 },
1389 { "early-hint", parse_http_set_header, 0 },
1390 { "redirect", parse_http_redirect, 0 },
1391 { "reject", parse_http_action_reject, 0 },
1392 { "replace-header", parse_http_replace_header, 0 },
1393 { "replace-path", parse_replace_uri, 0 },
1394 { "replace-uri", parse_replace_uri, 0 },
1395 { "replace-value", parse_http_replace_header, 0 },
1396 { "set-header", parse_http_set_header, 0 },
1397 { "set-log-level", parse_http_set_log_level, 0 },
1398 { "set-map", parse_http_set_map, 1 },
1399 { "set-method", parse_set_req_line, 0 },
1400 { "set-mark", parse_http_set_mark, 0 },
1401 { "set-nice", parse_http_set_nice, 0 },
1402 { "set-path", parse_set_req_line, 0 },
1403 { "set-query", parse_set_req_line, 0 },
1404 { "set-tos", parse_http_set_tos, 0 },
1405 { "set-uri", parse_set_req_line, 0 },
Christopher Faulet46f95542019-12-20 10:07:22 +01001406 { "strict-mode", parse_http_strict_mode, 0 },
Christopher Faulet81e20172019-12-12 16:40:30 +01001407 { "tarpit", parse_http_req_deny, 0 },
1408 { "track-sc", parse_http_track_sc, 1 },
Willy Tarreau79e57332018-10-02 16:01:16 +02001409 { NULL, NULL }
1410 }
1411};
1412
Willy Tarreau0108d902018-11-25 19:14:37 +01001413INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_actions);
1414
Willy Tarreau79e57332018-10-02 16:01:16 +02001415static struct action_kw_list http_res_actions = {
1416 .kw = {
Christopher Faulet81e20172019-12-12 16:40:30 +01001417 { "add-acl", parse_http_set_map, 1 },
1418 { "add-header", parse_http_set_header, 0 },
1419 { "allow", parse_http_allow, 0 },
1420 { "capture", parse_http_res_capture, 0 },
1421 { "del-acl", parse_http_set_map, 1 },
1422 { "del-header", parse_http_del_header, 0 },
1423 { "del-map", parse_http_set_map, 1 },
1424 { "deny", parse_http_res_deny, 0 },
1425 { "redirect", parse_http_redirect, 0 },
1426 { "replace-header", parse_http_replace_header, 0 },
1427 { "replace-value", parse_http_replace_header, 0 },
1428 { "set-header", parse_http_set_header, 0 },
1429 { "set-log-level", parse_http_set_log_level, 0 },
1430 { "set-map", parse_http_set_map, 1 },
1431 { "set-mark", parse_http_set_mark, 0 },
1432 { "set-nice", parse_http_set_nice, 0 },
1433 { "set-status", parse_http_set_status, 0 },
1434 { "set-tos", parse_http_set_tos, 0 },
Christopher Faulet46f95542019-12-20 10:07:22 +01001435 { "strict-mode", parse_http_strict_mode, 0 },
Christopher Faulet81e20172019-12-12 16:40:30 +01001436 { "track-sc", parse_http_track_sc, 1 },
Willy Tarreau79e57332018-10-02 16:01:16 +02001437 { NULL, NULL }
1438 }
1439};
1440
Willy Tarreau0108d902018-11-25 19:14:37 +01001441INITCALL1(STG_REGISTER, http_res_keywords_register, &http_res_actions);
Willy Tarreau79e57332018-10-02 16:01:16 +02001442
1443/*
1444 * Local variables:
1445 * c-indent-level: 8
1446 * c-basic-offset: 8
1447 * End:
1448 */