blob: 9bfa7ebe44b6d4f582c5232b8e69e8609ad92484 [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>
Christopher Faulet24231ab2020-01-24 17:44:23 +010014#include <sys/stat.h>
15#include <fcntl.h>
Willy Tarreau79e57332018-10-02 16:01:16 +020016
17#include <ctype.h>
18#include <string.h>
19#include <time.h>
20
Christopher Faulet81e20172019-12-12 16:40:30 +010021#include <common/cfgparse.h>
Willy Tarreau79e57332018-10-02 16:01:16 +020022#include <common/chunk.h>
23#include <common/compat.h>
24#include <common/config.h>
25#include <common/debug.h>
26#include <common/http.h>
Willy Tarreau0108d902018-11-25 19:14:37 +010027#include <common/initcall.h>
Willy Tarreau79e57332018-10-02 16:01:16 +020028#include <common/memory.h>
29#include <common/standard.h>
30#include <common/version.h>
31
32#include <types/capture.h>
33#include <types/global.h>
34
35#include <proto/acl.h>
36#include <proto/arg.h>
Christopher Faulet81e20172019-12-12 16:40:30 +010037#include <proto/action.h>
Willy Tarreau61c112a2018-10-02 16:43:32 +020038#include <proto/http_rules.h>
Willy Tarreau33810222019-06-12 17:44:02 +020039#include <proto/http_htx.h>
Willy Tarreau79e57332018-10-02 16:01:16 +020040#include <proto/log.h>
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020041#include <proto/http_ana.h>
Christopher Faulet046cf442019-12-17 15:45:23 +010042#include <proto/pattern.h>
Willy Tarreau0f9cd7b2019-01-31 19:02:43 +010043#include <proto/stream_interface.h>
Willy Tarreau79e57332018-10-02 16:01:16 +020044
Christopher Faulet4a2c1422020-01-31 17:36:01 +010045/* Structure used to build the header list of an HTTP return action */
46struct http_ret_hdr {
47 struct ist name; /* the header name */
48 struct list value; /* the log-format string value */
49 struct list list; /* header chained list */
50};
51
Christopher Faulet2eb53962020-01-14 14:47:34 +010052/* Release memory allocated by most of HTTP actions. Concretly, it releases
53 * <arg.http>.
54 */
55static void release_http_action(struct act_rule *rule)
56{
57 struct logformat_node *lf, *lfb;
58
59 if (rule->arg.http.str.ptr)
60 free(rule->arg.http.str.ptr);
61 if (rule->arg.http.re)
62 regex_free(rule->arg.http.re);
63 list_for_each_entry_safe(lf, lfb, &rule->arg.http.fmt, list) {
64 LIST_DEL(&lf->list);
65 release_sample_expr(lf->expr);
66 free(lf->arg);
67 free(lf);
68 }
69}
70
Willy Tarreau79e57332018-10-02 16:01:16 +020071
72/* This function executes one of the set-{method,path,query,uri} actions. It
73 * builds a string in the trash from the specified format string. It finds
Christopher Faulet2c22a692019-12-18 15:39:56 +010074 * the action to be performed in <.action>, previously filled by function
Willy Tarreau79e57332018-10-02 16:01:16 +020075 * parse_set_req_line(). The replacement action is excuted by the function
Christopher Faulete00d06c2019-12-16 17:18:42 +010076 * http_action_set_req_line(). On success, it returns ACT_RET_CONT. If an error
77 * occurs while soft rewrites are enabled, the action is canceled, but the rule
78 * processing continue. Otherwsize ACT_RET_ERR is returned.
Willy Tarreau79e57332018-10-02 16:01:16 +020079 */
80static enum act_return http_action_set_req_line(struct act_rule *rule, struct proxy *px,
81 struct session *sess, struct stream *s, int flags)
82{
83 struct buffer *replace;
Christopher Faulet13403762019-12-13 09:01:57 +010084 enum act_return ret = ACT_RET_CONT;
Willy Tarreau79e57332018-10-02 16:01:16 +020085
86 replace = alloc_trash_chunk();
87 if (!replace)
Christopher Faulete00d06c2019-12-16 17:18:42 +010088 goto fail_alloc;
Willy Tarreau79e57332018-10-02 16:01:16 +020089
90 /* If we have to create a query string, prepare a '?'. */
Christopher Faulet2c22a692019-12-18 15:39:56 +010091 if (rule->action == 2) // set-query
Willy Tarreau79e57332018-10-02 16:01:16 +020092 replace->area[replace->data++] = '?';
93 replace->data += build_logline(s, replace->area + replace->data,
94 replace->size - replace->data,
Christopher Faulet96bff762019-12-17 13:46:18 +010095 &rule->arg.http.fmt);
Willy Tarreau79e57332018-10-02 16:01:16 +020096
Christopher Faulet2c22a692019-12-18 15:39:56 +010097 if (http_req_replace_stline(rule->action, replace->area, replace->data, px, s) == -1)
Christopher Faulete00d06c2019-12-16 17:18:42 +010098 goto fail_rewrite;
Willy Tarreau79e57332018-10-02 16:01:16 +020099
Christopher Faulete00d06c2019-12-16 17:18:42 +0100100 leave:
Willy Tarreau79e57332018-10-02 16:01:16 +0200101 free_trash_chunk(replace);
102 return ret;
Christopher Faulete00d06c2019-12-16 17:18:42 +0100103
104 fail_alloc:
105 if (!(s->flags & SF_ERR_MASK))
106 s->flags |= SF_ERR_RESOURCE;
107 ret = ACT_RET_ERR;
108 goto leave;
109
110 fail_rewrite:
111 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_rewrites, 1);
112 if (s->flags & SF_BE_ASSIGNED)
113 _HA_ATOMIC_ADD(&s->be->be_counters.failed_rewrites, 1);
114 if (sess->listener->counters)
115 _HA_ATOMIC_ADD(&sess->listener->counters->failed_rewrites, 1);
116 if (objt_server(s->target))
117 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_rewrites, 1);
118
Christopher Faulet333bf8c2020-01-22 14:38:05 +0100119 if (!(s->txn->req.flags & HTTP_MSGF_SOFT_RW)) {
Christopher Faulete00d06c2019-12-16 17:18:42 +0100120 ret = ACT_RET_ERR;
Christopher Faulet333bf8c2020-01-22 14:38:05 +0100121 if (!(s->flags & SF_ERR_MASK))
122 s->flags |= SF_ERR_PRXCOND;
123 }
Christopher Faulete00d06c2019-12-16 17:18:42 +0100124 goto leave;
Willy Tarreau79e57332018-10-02 16:01:16 +0200125}
126
127/* parse an http-request action among :
128 * set-method
129 * set-path
130 * set-query
131 * set-uri
132 *
133 * All of them accept a single argument of type string representing a log-format.
Christopher Faulet96bff762019-12-17 13:46:18 +0100134 * The resulting rule makes use of <http.fmt> to store the log-format list head,
Christopher Faulet2c22a692019-12-18 15:39:56 +0100135 * and <.action> to store the action type as an int (0=method, 1=path, 2=query,
136 * 3=uri). It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Willy Tarreau79e57332018-10-02 16:01:16 +0200137 */
138static enum act_parse_ret parse_set_req_line(const char **args, int *orig_arg, struct proxy *px,
139 struct act_rule *rule, char **err)
140{
141 int cur_arg = *orig_arg;
142
Willy Tarreau79e57332018-10-02 16:01:16 +0200143 switch (args[0][4]) {
144 case 'm' :
Christopher Faulet2c22a692019-12-18 15:39:56 +0100145 rule->action = 0; // set-method
Willy Tarreau79e57332018-10-02 16:01:16 +0200146 break;
147 case 'p' :
Christopher Faulet2c22a692019-12-18 15:39:56 +0100148 rule->action = 1; // set-path
Willy Tarreau79e57332018-10-02 16:01:16 +0200149 break;
150 case 'q' :
Christopher Faulet2c22a692019-12-18 15:39:56 +0100151 rule->action = 2; // set-query
Willy Tarreau79e57332018-10-02 16:01:16 +0200152 break;
153 case 'u' :
Christopher Faulet2c22a692019-12-18 15:39:56 +0100154 rule->action = 3; // set-uri
Willy Tarreau79e57332018-10-02 16:01:16 +0200155 break;
156 default:
157 memprintf(err, "internal error: unhandled action '%s'", args[0]);
158 return ACT_RET_PRS_ERR;
159 }
Christopher Faulet96bff762019-12-17 13:46:18 +0100160 rule->action_ptr = http_action_set_req_line;
Christopher Faulet2eb53962020-01-14 14:47:34 +0100161 rule->release_ptr = release_http_action;
Willy Tarreau79e57332018-10-02 16:01:16 +0200162
163 if (!*args[cur_arg] ||
164 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
165 memprintf(err, "expects exactly 1 argument <format>");
166 return ACT_RET_PRS_ERR;
167 }
168
Christopher Faulet96bff762019-12-17 13:46:18 +0100169 LIST_INIT(&rule->arg.http.fmt);
Willy Tarreau79e57332018-10-02 16:01:16 +0200170 px->conf.args.ctx = ARGC_HRQ;
Christopher Faulet96bff762019-12-17 13:46:18 +0100171 if (!parse_logformat_string(args[cur_arg], px, &rule->arg.http.fmt, LOG_OPT_HTTP,
Willy Tarreau79e57332018-10-02 16:01:16 +0200172 (px->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, err)) {
173 return ACT_RET_PRS_ERR;
174 }
175
176 (*orig_arg)++;
177 return ACT_RET_PRS_OK;
178}
179
Willy Tarreau33810222019-06-12 17:44:02 +0200180/* This function executes a replace-uri action. It finds its arguments in
Christopher Faulet96bff762019-12-17 13:46:18 +0100181 * <rule>.arg.http. It builds a string in the trash from the format string
Willy Tarreau33810222019-06-12 17:44:02 +0200182 * previously filled by function parse_replace_uri() and will execute the regex
Christopher Faulet96bff762019-12-17 13:46:18 +0100183 * in <http.re> to replace the URI. It uses the format string present in
Christopher Faulet2c22a692019-12-18 15:39:56 +0100184 * <http.fmt>. The component to act on (path/uri) is taken from <.action> which
Christopher Faulet96bff762019-12-17 13:46:18 +0100185 * contains 1 for the path or 3 for the URI (values used by
186 * http_req_replace_stline()). On success, it returns ACT_RET_CONT. If an error
187 * occurs while soft rewrites are enabled, the action is canceled, but the rule
188 * processing continue. Otherwsize ACT_RET_ERR is returned.
Willy Tarreau33810222019-06-12 17:44:02 +0200189 */
190static enum act_return http_action_replace_uri(struct act_rule *rule, struct proxy *px,
191 struct session *sess, struct stream *s, int flags)
192{
Christopher Faulet13403762019-12-13 09:01:57 +0100193 enum act_return ret = ACT_RET_CONT;
Willy Tarreau33810222019-06-12 17:44:02 +0200194 struct buffer *replace, *output;
195 struct ist uri;
196 int len;
197
198 replace = alloc_trash_chunk();
199 output = alloc_trash_chunk();
200 if (!replace || !output)
Christopher Faulete00d06c2019-12-16 17:18:42 +0100201 goto fail_alloc;
Christopher Faulet12c28b62019-07-15 16:30:24 +0200202 uri = htx_sl_req_uri(http_get_stline(htxbuf(&s->req.buf)));
Willy Tarreau262c3f12019-12-17 06:52:51 +0100203
Christopher Faulet2c22a692019-12-18 15:39:56 +0100204 if (rule->action == 1) // replace-path
Jerome Magnin4bbc9492020-02-21 10:37:48 +0100205 uri = iststop(http_get_path(uri), '?');
Willy Tarreau262c3f12019-12-17 06:52:51 +0100206
Christopher Faulet96bff762019-12-17 13:46:18 +0100207 if (!regex_exec_match2(rule->arg.http.re, uri.ptr, uri.len, MAX_MATCH, pmatch, 0))
Willy Tarreau33810222019-06-12 17:44:02 +0200208 goto leave;
209
Christopher Faulet96bff762019-12-17 13:46:18 +0100210 replace->data = build_logline(s, replace->area, replace->size, &rule->arg.http.fmt);
Willy Tarreau33810222019-06-12 17:44:02 +0200211
212 /* note: uri.ptr doesn't need to be zero-terminated because it will
213 * only be used to pick pmatch references.
214 */
215 len = exp_replace(output->area, output->size, uri.ptr, replace->area, pmatch);
216 if (len == -1)
Christopher Faulete00d06c2019-12-16 17:18:42 +0100217 goto fail_rewrite;
Willy Tarreau33810222019-06-12 17:44:02 +0200218
Christopher Faulet2c22a692019-12-18 15:39:56 +0100219 if (http_req_replace_stline(rule->action, output->area, len, px, s) == -1)
Christopher Faulete00d06c2019-12-16 17:18:42 +0100220 goto fail_rewrite;
Willy Tarreau33810222019-06-12 17:44:02 +0200221
Christopher Faulete00d06c2019-12-16 17:18:42 +0100222 leave:
Willy Tarreau33810222019-06-12 17:44:02 +0200223 free_trash_chunk(output);
224 free_trash_chunk(replace);
225 return ret;
Christopher Faulete00d06c2019-12-16 17:18:42 +0100226
227 fail_alloc:
228 if (!(s->flags & SF_ERR_MASK))
229 s->flags |= SF_ERR_RESOURCE;
230 ret = ACT_RET_ERR;
231 goto leave;
232
233 fail_rewrite:
234 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_rewrites, 1);
235 if (s->flags & SF_BE_ASSIGNED)
236 _HA_ATOMIC_ADD(&s->be->be_counters.failed_rewrites, 1);
237 if (sess->listener->counters)
238 _HA_ATOMIC_ADD(&sess->listener->counters->failed_rewrites, 1);
239 if (objt_server(s->target))
240 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_rewrites, 1);
241
Christopher Faulet333bf8c2020-01-22 14:38:05 +0100242 if (!(s->txn->req.flags & HTTP_MSGF_SOFT_RW)) {
Christopher Faulete00d06c2019-12-16 17:18:42 +0100243 ret = ACT_RET_ERR;
Christopher Faulet333bf8c2020-01-22 14:38:05 +0100244 if (!(s->flags & SF_ERR_MASK))
245 s->flags |= SF_ERR_PRXCOND;
246 }
Christopher Faulete00d06c2019-12-16 17:18:42 +0100247 goto leave;
Willy Tarreau33810222019-06-12 17:44:02 +0200248}
249
Willy Tarreau262c3f12019-12-17 06:52:51 +0100250/* parse a "replace-uri" or "replace-path" http-request action.
Willy Tarreau33810222019-06-12 17:44:02 +0200251 * This action takes 2 arguments (a regex and a replacement format string).
Christopher Faulet2c22a692019-12-18 15:39:56 +0100252 * The resulting rule makes use of <.action> to store the action (1/3 for now),
Christopher Faulet96bff762019-12-17 13:46:18 +0100253 * <http.re> to store the compiled regex, and <http.fmt> to store the log-format
Willy Tarreau33810222019-06-12 17:44:02 +0200254 * list head. It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
255 */
256static enum act_parse_ret parse_replace_uri(const char **args, int *orig_arg, struct proxy *px,
257 struct act_rule *rule, char **err)
258{
259 int cur_arg = *orig_arg;
260 char *error = NULL;
261
Willy Tarreau262c3f12019-12-17 06:52:51 +0100262 if (strcmp(args[cur_arg-1], "replace-path") == 0)
Christopher Faulet2c22a692019-12-18 15:39:56 +0100263 rule->action = 1; // replace-path, same as set-path
Willy Tarreau262c3f12019-12-17 06:52:51 +0100264 else
Christopher Faulet2c22a692019-12-18 15:39:56 +0100265 rule->action = 3; // replace-uri, same as set-uri
Willy Tarreau262c3f12019-12-17 06:52:51 +0100266
Willy Tarreau33810222019-06-12 17:44:02 +0200267 rule->action_ptr = http_action_replace_uri;
Christopher Faulet2eb53962020-01-14 14:47:34 +0100268 rule->release_ptr = release_http_action;
Willy Tarreau33810222019-06-12 17:44:02 +0200269
270 if (!*args[cur_arg] || !*args[cur_arg+1] ||
271 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
272 memprintf(err, "expects exactly 2 arguments <match-regex> and <replace-format>");
273 return ACT_RET_PRS_ERR;
274 }
275
Christopher Faulet96bff762019-12-17 13:46:18 +0100276 if (!(rule->arg.http.re = regex_comp(args[cur_arg], 1, 1, &error))) {
Willy Tarreau33810222019-06-12 17:44:02 +0200277 memprintf(err, "failed to parse the regex : %s", error);
278 free(error);
279 return ACT_RET_PRS_ERR;
280 }
281
Christopher Faulet96bff762019-12-17 13:46:18 +0100282 LIST_INIT(&rule->arg.http.fmt);
Willy Tarreau33810222019-06-12 17:44:02 +0200283 px->conf.args.ctx = ARGC_HRQ;
Christopher Faulet96bff762019-12-17 13:46:18 +0100284 if (!parse_logformat_string(args[cur_arg + 1], px, &rule->arg.http.fmt, LOG_OPT_HTTP,
Willy Tarreau33810222019-06-12 17:44:02 +0200285 (px->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, err)) {
Christopher Faulet1337b322020-01-14 14:50:55 +0100286 regex_free(rule->arg.http.re);
Willy Tarreau33810222019-06-12 17:44:02 +0200287 return ACT_RET_PRS_ERR;
288 }
289
290 (*orig_arg) += 2;
291 return ACT_RET_PRS_OK;
292}
293
Willy Tarreau79e57332018-10-02 16:01:16 +0200294/* This function is just a compliant action wrapper for "set-status". */
295static enum act_return action_http_set_status(struct act_rule *rule, struct proxy *px,
296 struct session *sess, struct stream *s, int flags)
297{
Christopher Faulet96bff762019-12-17 13:46:18 +0100298 if (http_res_set_status(rule->arg.http.i, rule->arg.http.str, s) == -1) {
Christopher Faulete00d06c2019-12-16 17:18:42 +0100299 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_rewrites, 1);
300 if (s->flags & SF_BE_ASSIGNED)
301 _HA_ATOMIC_ADD(&s->be->be_counters.failed_rewrites, 1);
302 if (sess->listener->counters)
303 _HA_ATOMIC_ADD(&sess->listener->counters->failed_rewrites, 1);
304 if (objt_server(s->target))
305 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_rewrites, 1);
306
Christopher Faulet333bf8c2020-01-22 14:38:05 +0100307 if (!(s->txn->req.flags & HTTP_MSGF_SOFT_RW)) {
Christopher Faulet333bf8c2020-01-22 14:38:05 +0100308 if (!(s->flags & SF_ERR_MASK))
309 s->flags |= SF_ERR_PRXCOND;
Christopher Faulet692a6c22020-02-07 10:22:31 +0100310 return ACT_RET_ERR;
Christopher Faulet333bf8c2020-01-22 14:38:05 +0100311 }
Christopher Faulete00d06c2019-12-16 17:18:42 +0100312 }
313
Willy Tarreau79e57332018-10-02 16:01:16 +0200314 return ACT_RET_CONT;
315}
316
317/* parse set-status action:
318 * This action accepts a single argument of type int representing
319 * an http status code. It returns ACT_RET_PRS_OK on success,
320 * ACT_RET_PRS_ERR on error.
321 */
322static enum act_parse_ret parse_http_set_status(const char **args, int *orig_arg, struct proxy *px,
323 struct act_rule *rule, char **err)
324{
325 char *error;
326
327 rule->action = ACT_CUSTOM;
328 rule->action_ptr = action_http_set_status;
Christopher Faulet2eb53962020-01-14 14:47:34 +0100329 rule->release_ptr = release_http_action;
Willy Tarreau79e57332018-10-02 16:01:16 +0200330
331 /* Check if an argument is available */
332 if (!*args[*orig_arg]) {
333 memprintf(err, "expects 1 argument: <status>; or 3 arguments: <status> reason <fmt>");
334 return ACT_RET_PRS_ERR;
335 }
336
337 /* convert status code as integer */
Christopher Faulet96bff762019-12-17 13:46:18 +0100338 rule->arg.http.i = strtol(args[*orig_arg], &error, 10);
339 if (*error != '\0' || rule->arg.http.i < 100 || rule->arg.http.i > 999) {
Willy Tarreau79e57332018-10-02 16:01:16 +0200340 memprintf(err, "expects an integer status code between 100 and 999");
341 return ACT_RET_PRS_ERR;
342 }
343
344 (*orig_arg)++;
345
346 /* set custom reason string */
Christopher Faulet96bff762019-12-17 13:46:18 +0100347 rule->arg.http.str = ist(NULL); // If null, we use the default reason for the status code.
Willy Tarreau79e57332018-10-02 16:01:16 +0200348 if (*args[*orig_arg] && strcmp(args[*orig_arg], "reason") == 0 &&
349 (*args[*orig_arg + 1] && strcmp(args[*orig_arg + 1], "if") != 0 && strcmp(args[*orig_arg + 1], "unless") != 0)) {
350 (*orig_arg)++;
Christopher Faulet96bff762019-12-17 13:46:18 +0100351 rule->arg.http.str.ptr = strdup(args[*orig_arg]);
352 rule->arg.http.str.len = strlen(rule->arg.http.str.ptr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200353 (*orig_arg)++;
354 }
355
Christopher Fauletc20b3712020-01-27 15:51:56 +0100356 LIST_INIT(&rule->arg.http.fmt);
Willy Tarreau79e57332018-10-02 16:01:16 +0200357 return ACT_RET_PRS_OK;
358}
359
360/* This function executes the "reject" HTTP action. It clears the request and
361 * response buffer without sending any response. It can be useful as an HTTP
362 * alternative to the silent-drop action to defend against DoS attacks, and may
363 * also be used with HTTP/2 to close a connection instead of just a stream.
364 * The txn status is unchanged, indicating no response was sent. The termination
Christopher Faulet8f1aa772019-07-04 11:27:15 +0200365 * flags will indicate "PR". It always returns ACT_RET_DONE.
Willy Tarreau79e57332018-10-02 16:01:16 +0200366 */
367static enum act_return http_action_reject(struct act_rule *rule, struct proxy *px,
368 struct session *sess, struct stream *s, int flags)
369{
Willy Tarreau0f9cd7b2019-01-31 19:02:43 +0100370 si_must_kill_conn(chn_prod(&s->req));
Willy Tarreau79e57332018-10-02 16:01:16 +0200371 channel_abort(&s->req);
372 channel_abort(&s->res);
373 s->req.analysers = 0;
374 s->res.analysers = 0;
375
Olivier Houcharda798bf52019-03-08 18:52:00 +0100376 _HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
377 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Willy Tarreau79e57332018-10-02 16:01:16 +0200378 if (sess->listener && sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100379 _HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Willy Tarreau79e57332018-10-02 16:01:16 +0200380
381 if (!(s->flags & SF_ERR_MASK))
382 s->flags |= SF_ERR_PRXCOND;
383 if (!(s->flags & SF_FINST_MASK))
384 s->flags |= SF_FINST_R;
385
Christopher Faulet8f1aa772019-07-04 11:27:15 +0200386 return ACT_RET_DONE;
Willy Tarreau79e57332018-10-02 16:01:16 +0200387}
388
389/* parse the "reject" action:
390 * This action takes no argument and returns ACT_RET_PRS_OK on success,
391 * ACT_RET_PRS_ERR on error.
392 */
393static enum act_parse_ret parse_http_action_reject(const char **args, int *orig_arg, struct proxy *px,
394 struct act_rule *rule, char **err)
395{
396 rule->action = ACT_CUSTOM;
397 rule->action_ptr = http_action_reject;
398 return ACT_RET_PRS_OK;
399}
400
Olivier Houchard602bf7d2019-05-10 13:59:15 +0200401/* This function executes the "disable-l7-retry" HTTP action.
402 * It disables L7 retries (all retry except for a connection failure). This
403 * can be useful for example to avoid retrying on POST requests.
404 * It just removes the L7 retry flag on the stream_interface, and always
405 * return ACT_RET_CONT;
406 */
407static enum act_return http_req_disable_l7_retry(struct act_rule *rule, struct proxy *px,
408 struct session *sess, struct stream *s, int flags)
409{
410 struct stream_interface *si = &s->si[1];
411
412 /* In theory, the SI_FL_L7_RETRY flags isn't set at this point, but
413 * let's be future-proof and remove it anyway.
414 */
415 si->flags &= ~SI_FL_L7_RETRY;
416 si->flags |= SI_FL_D_L7_RETRY;
417 return ACT_RET_CONT;
418}
419
420/* parse the "disable-l7-retry" action:
421 * This action takes no argument and returns ACT_RET_PRS_OK on success,
422 * ACT_RET_PRS_ERR on error.
423 */
424static enum act_parse_ret parse_http_req_disable_l7_retry(const char **args,
425 int *orig_args, struct proxy *px,
426 struct act_rule *rule, char **err)
427{
428 rule->action = ACT_CUSTOM;
429 rule->action_ptr = http_req_disable_l7_retry;
430 return ACT_RET_PRS_OK;
431}
432
Willy Tarreau79e57332018-10-02 16:01:16 +0200433/* This function executes the "capture" action. It executes a fetch expression,
434 * turns the result into a string and puts it in a capture slot. It always
435 * returns 1. If an error occurs the action is cancelled, but the rule
436 * processing continues.
437 */
438static enum act_return http_action_req_capture(struct act_rule *rule, struct proxy *px,
439 struct session *sess, struct stream *s, int flags)
440{
441 struct sample *key;
442 struct cap_hdr *h = rule->arg.cap.hdr;
443 char **cap = s->req_cap;
444 int len;
445
446 key = sample_fetch_as_type(s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.cap.expr, SMP_T_STR);
447 if (!key)
448 return ACT_RET_CONT;
449
450 if (cap[h->index] == NULL)
451 cap[h->index] = pool_alloc(h->pool);
452
453 if (cap[h->index] == NULL) /* no more capture memory */
454 return ACT_RET_CONT;
455
456 len = key->data.u.str.data;
457 if (len > h->len)
458 len = h->len;
459
460 memcpy(cap[h->index], key->data.u.str.area, len);
461 cap[h->index][len] = 0;
462 return ACT_RET_CONT;
463}
464
465/* This function executes the "capture" action and store the result in a
466 * capture slot if exists. It executes a fetch expression, turns the result
467 * into a string and puts it in a capture slot. It always returns 1. If an
468 * error occurs the action is cancelled, but the rule processing continues.
469 */
470static enum act_return http_action_req_capture_by_id(struct act_rule *rule, struct proxy *px,
471 struct session *sess, struct stream *s, int flags)
472{
473 struct sample *key;
474 struct cap_hdr *h;
475 char **cap = s->req_cap;
476 struct proxy *fe = strm_fe(s);
477 int len;
478 int i;
479
480 /* Look for the original configuration. */
481 for (h = fe->req_cap, i = fe->nb_req_cap - 1;
482 h != NULL && i != rule->arg.capid.idx ;
483 i--, h = h->next);
484 if (!h)
485 return ACT_RET_CONT;
486
487 key = sample_fetch_as_type(s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.capid.expr, SMP_T_STR);
488 if (!key)
489 return ACT_RET_CONT;
490
491 if (cap[h->index] == NULL)
492 cap[h->index] = pool_alloc(h->pool);
493
494 if (cap[h->index] == NULL) /* no more capture memory */
495 return ACT_RET_CONT;
496
497 len = key->data.u.str.data;
498 if (len > h->len)
499 len = h->len;
500
501 memcpy(cap[h->index], key->data.u.str.area, len);
502 cap[h->index][len] = 0;
503 return ACT_RET_CONT;
504}
505
506/* Check an "http-request capture" action.
507 *
508 * The function returns 1 in success case, otherwise, it returns 0 and err is
509 * filled.
510 */
511static int check_http_req_capture(struct act_rule *rule, struct proxy *px, char **err)
512{
513 if (rule->action_ptr != http_action_req_capture_by_id)
514 return 1;
515
Baptiste Assmann19a69b32020-01-16 14:34:22 +0100516 /* capture slots can only be declared in frontends, so we can't check their
517 * existence in backends at configuration parsing step
518 */
519 if (px->cap & PR_CAP_FE && rule->arg.capid.idx >= px->nb_req_cap) {
Willy Tarreau79e57332018-10-02 16:01:16 +0200520 memprintf(err, "unable to find capture id '%d' referenced by http-request capture rule",
521 rule->arg.capid.idx);
522 return 0;
523 }
524
525 return 1;
526}
527
Christopher Faulet2eb53962020-01-14 14:47:34 +0100528/* Release memory allocate by an http capture action */
529static void release_http_capture(struct act_rule *rule)
530{
531 if (rule->action_ptr == http_action_req_capture)
532 release_sample_expr(rule->arg.cap.expr);
533 else
534 release_sample_expr(rule->arg.capid.expr);
535}
536
Willy Tarreau79e57332018-10-02 16:01:16 +0200537/* parse an "http-request capture" action. It takes a single argument which is
538 * a sample fetch expression. It stores the expression into arg->act.p[0] and
539 * the allocated hdr_cap struct or the preallocated "id" into arg->act.p[1].
540 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
541 */
542static enum act_parse_ret parse_http_req_capture(const char **args, int *orig_arg, struct proxy *px,
543 struct act_rule *rule, char **err)
544{
545 struct sample_expr *expr;
546 struct cap_hdr *hdr;
547 int cur_arg;
548 int len = 0;
549
550 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
551 if (strcmp(args[cur_arg], "if") == 0 ||
552 strcmp(args[cur_arg], "unless") == 0)
553 break;
554
555 if (cur_arg < *orig_arg + 3) {
556 memprintf(err, "expects <expression> [ 'len' <length> | id <idx> ]");
557 return ACT_RET_PRS_ERR;
558 }
559
560 cur_arg = *orig_arg;
Willy Tarreaue3b57bf2020-02-14 16:50:14 +0100561 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 +0200562 if (!expr)
563 return ACT_RET_PRS_ERR;
564
565 if (!(expr->fetch->val & SMP_VAL_FE_HRQ_HDR)) {
566 memprintf(err,
567 "fetch method '%s' extracts information from '%s', none of which is available here",
568 args[cur_arg-1], sample_src_names(expr->fetch->use));
Christopher Faulet1337b322020-01-14 14:50:55 +0100569 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200570 return ACT_RET_PRS_ERR;
571 }
572
573 if (!args[cur_arg] || !*args[cur_arg]) {
574 memprintf(err, "expects 'len or 'id'");
Christopher Faulet1337b322020-01-14 14:50:55 +0100575 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200576 return ACT_RET_PRS_ERR;
577 }
578
579 if (strcmp(args[cur_arg], "len") == 0) {
580 cur_arg++;
581
582 if (!(px->cap & PR_CAP_FE)) {
583 memprintf(err, "proxy '%s' has no frontend capability", px->id);
Christopher Faulet1337b322020-01-14 14:50:55 +0100584 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200585 return ACT_RET_PRS_ERR;
586 }
587
588 px->conf.args.ctx = ARGC_CAP;
589
590 if (!args[cur_arg]) {
591 memprintf(err, "missing length value");
Christopher Faulet1337b322020-01-14 14:50:55 +0100592 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200593 return ACT_RET_PRS_ERR;
594 }
595 /* we copy the table name for now, it will be resolved later */
596 len = atoi(args[cur_arg]);
597 if (len <= 0) {
598 memprintf(err, "length must be > 0");
Christopher Faulet1337b322020-01-14 14:50:55 +0100599 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200600 return ACT_RET_PRS_ERR;
601 }
602 cur_arg++;
603
Willy Tarreau79e57332018-10-02 16:01:16 +0200604 hdr = calloc(1, sizeof(*hdr));
605 hdr->next = px->req_cap;
606 hdr->name = NULL; /* not a header capture */
607 hdr->namelen = 0;
608 hdr->len = len;
609 hdr->pool = create_pool("caphdr", hdr->len + 1, MEM_F_SHARED);
610 hdr->index = px->nb_req_cap++;
611
612 px->req_cap = hdr;
613 px->to_log |= LW_REQHDR;
614
615 rule->action = ACT_CUSTOM;
616 rule->action_ptr = http_action_req_capture;
Christopher Faulet2eb53962020-01-14 14:47:34 +0100617 rule->release_ptr = release_http_capture;
Willy Tarreau79e57332018-10-02 16:01:16 +0200618 rule->arg.cap.expr = expr;
619 rule->arg.cap.hdr = hdr;
620 }
621
622 else if (strcmp(args[cur_arg], "id") == 0) {
623 int id;
624 char *error;
625
626 cur_arg++;
627
628 if (!args[cur_arg]) {
629 memprintf(err, "missing id value");
Christopher Faulet1337b322020-01-14 14:50:55 +0100630 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200631 return ACT_RET_PRS_ERR;
632 }
633
634 id = strtol(args[cur_arg], &error, 10);
635 if (*error != '\0') {
636 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
Christopher Faulet1337b322020-01-14 14:50:55 +0100637 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200638 return ACT_RET_PRS_ERR;
639 }
640 cur_arg++;
641
642 px->conf.args.ctx = ARGC_CAP;
643
644 rule->action = ACT_CUSTOM;
645 rule->action_ptr = http_action_req_capture_by_id;
646 rule->check_ptr = check_http_req_capture;
Christopher Faulet2eb53962020-01-14 14:47:34 +0100647 rule->release_ptr = release_http_capture;
Willy Tarreau79e57332018-10-02 16:01:16 +0200648 rule->arg.capid.expr = expr;
649 rule->arg.capid.idx = id;
650 }
651
652 else {
653 memprintf(err, "expects 'len' or 'id', found '%s'", args[cur_arg]);
Christopher Faulet1337b322020-01-14 14:50:55 +0100654 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200655 return ACT_RET_PRS_ERR;
656 }
657
658 *orig_arg = cur_arg;
659 return ACT_RET_PRS_OK;
660}
661
662/* This function executes the "capture" action and store the result in a
663 * capture slot if exists. It executes a fetch expression, turns the result
664 * into a string and puts it in a capture slot. It always returns 1. If an
665 * error occurs the action is cancelled, but the rule processing continues.
666 */
667static enum act_return http_action_res_capture_by_id(struct act_rule *rule, struct proxy *px,
668 struct session *sess, struct stream *s, int flags)
669{
670 struct sample *key;
671 struct cap_hdr *h;
672 char **cap = s->res_cap;
673 struct proxy *fe = strm_fe(s);
674 int len;
675 int i;
676
677 /* Look for the original configuration. */
678 for (h = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
679 h != NULL && i != rule->arg.capid.idx ;
680 i--, h = h->next);
681 if (!h)
682 return ACT_RET_CONT;
683
684 key = sample_fetch_as_type(s->be, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL, rule->arg.capid.expr, SMP_T_STR);
685 if (!key)
686 return ACT_RET_CONT;
687
688 if (cap[h->index] == NULL)
689 cap[h->index] = pool_alloc(h->pool);
690
691 if (cap[h->index] == NULL) /* no more capture memory */
692 return ACT_RET_CONT;
693
694 len = key->data.u.str.data;
695 if (len > h->len)
696 len = h->len;
697
698 memcpy(cap[h->index], key->data.u.str.area, len);
699 cap[h->index][len] = 0;
700 return ACT_RET_CONT;
701}
702
703/* Check an "http-response capture" action.
704 *
705 * The function returns 1 in success case, otherwise, it returns 0 and err is
706 * filled.
707 */
708static int check_http_res_capture(struct act_rule *rule, struct proxy *px, char **err)
709{
710 if (rule->action_ptr != http_action_res_capture_by_id)
711 return 1;
712
713 if (rule->arg.capid.idx >= px->nb_rsp_cap) {
714 memprintf(err, "unable to find capture id '%d' referenced by http-response capture rule",
715 rule->arg.capid.idx);
716 return 0;
717 }
718
719 return 1;
720}
721
722/* parse an "http-response capture" action. It takes a single argument which is
723 * a sample fetch expression. It stores the expression into arg->act.p[0] and
724 * the allocated hdr_cap struct od the preallocated id into arg->act.p[1].
725 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
726 */
727static enum act_parse_ret parse_http_res_capture(const char **args, int *orig_arg, struct proxy *px,
728 struct act_rule *rule, char **err)
729{
730 struct sample_expr *expr;
731 int cur_arg;
732 int id;
733 char *error;
734
735 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
736 if (strcmp(args[cur_arg], "if") == 0 ||
737 strcmp(args[cur_arg], "unless") == 0)
738 break;
739
740 if (cur_arg < *orig_arg + 3) {
741 memprintf(err, "expects <expression> id <idx>");
742 return ACT_RET_PRS_ERR;
743 }
744
745 cur_arg = *orig_arg;
Willy Tarreaue3b57bf2020-02-14 16:50:14 +0100746 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 +0200747 if (!expr)
748 return ACT_RET_PRS_ERR;
749
750 if (!(expr->fetch->val & SMP_VAL_FE_HRS_HDR)) {
751 memprintf(err,
752 "fetch method '%s' extracts information from '%s', none of which is available here",
753 args[cur_arg-1], sample_src_names(expr->fetch->use));
Christopher Faulet1337b322020-01-14 14:50:55 +0100754 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200755 return ACT_RET_PRS_ERR;
756 }
757
758 if (!args[cur_arg] || !*args[cur_arg]) {
759 memprintf(err, "expects 'id'");
Christopher Faulet1337b322020-01-14 14:50:55 +0100760 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200761 return ACT_RET_PRS_ERR;
762 }
763
764 if (strcmp(args[cur_arg], "id") != 0) {
765 memprintf(err, "expects 'id', found '%s'", args[cur_arg]);
Christopher Faulet1337b322020-01-14 14:50:55 +0100766 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200767 return ACT_RET_PRS_ERR;
768 }
769
770 cur_arg++;
771
772 if (!args[cur_arg]) {
773 memprintf(err, "missing id value");
Christopher Faulet1337b322020-01-14 14:50:55 +0100774 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200775 return ACT_RET_PRS_ERR;
776 }
777
778 id = strtol(args[cur_arg], &error, 10);
779 if (*error != '\0') {
780 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
Christopher Faulet1337b322020-01-14 14:50:55 +0100781 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200782 return ACT_RET_PRS_ERR;
783 }
784 cur_arg++;
785
786 px->conf.args.ctx = ARGC_CAP;
787
788 rule->action = ACT_CUSTOM;
789 rule->action_ptr = http_action_res_capture_by_id;
790 rule->check_ptr = check_http_res_capture;
Christopher Faulet2eb53962020-01-14 14:47:34 +0100791 rule->release_ptr = release_http_capture;
Willy Tarreau79e57332018-10-02 16:01:16 +0200792 rule->arg.capid.expr = expr;
793 rule->arg.capid.idx = id;
794
795 *orig_arg = cur_arg;
796 return ACT_RET_PRS_OK;
797}
798
Christopher Faulet81e20172019-12-12 16:40:30 +0100799/* Parse a "allow" action for a request or a response rule. It takes no argument. It
800 * returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
801 */
802static enum act_parse_ret parse_http_allow(const char **args, int *orig_arg, struct proxy *px,
803 struct act_rule *rule, char **err)
804{
805 rule->action = ACT_ACTION_ALLOW;
Christopher Faulet245cf792019-12-18 14:58:12 +0100806 rule->flags |= ACT_FLAG_FINAL;
Christopher Faulet81e20172019-12-12 16:40:30 +0100807 return ACT_RET_PRS_OK;
808}
809
Christopher Faulet554c0eb2020-01-14 12:00:28 +0100810/* Check an "http-request deny" action when an http-errors section is referenced.
811 *
812 * The function returns 1 in success case, otherwise, it returns 0 and err is
813 * filled.
814 */
815static int check_http_deny_action(struct act_rule *rule, struct proxy *px, char **err)
816{
817 struct http_errors *http_errs;
818 int status = (intptr_t)(rule->arg.act.p[0]);
819 int ret = 1;
820
821 list_for_each_entry(http_errs, &http_errors_list, list) {
822 if (strcmp(http_errs->id, (char *)rule->arg.act.p[1]) == 0) {
823 free(rule->arg.act.p[1]);
824 rule->arg.http_deny.status = status;
825 rule->arg.http_deny.errmsg = http_errs->errmsg[http_get_status_idx(status)];
826 if (!rule->arg.http_deny.errmsg)
827 ha_warning("Proxy '%s': status '%d' referenced by http deny rule "
828 "not declared in http-errors section '%s'.\n",
829 px->id, status, http_errs->id);
830 break;
831 }
832 }
833
834 if (&http_errs->list == &http_errors_list) {
835 memprintf(err, "unknown http-errors section '%s' referenced by http deny rule",
836 (char *)rule->arg.act.p[1]);
837 free(rule->arg.act.p[1]);
838 ret = 0;
839 }
840
841 return ret;
842}
843
Christopher Faulete0fca292020-01-13 21:49:03 +0100844/* Parse "deny" or "tarpit" actions for a request rule or "deny" action for a
Christopher Faulet554c0eb2020-01-14 12:00:28 +0100845 * response rule. It may take optional arguments to define the status code, the
846 * error file or the http-errors section to use. It returns ACT_RET_PRS_OK on
847 * success, ACT_RET_PRS_ERR on error.
Christopher Faulet81e20172019-12-12 16:40:30 +0100848 */
Christopher Faulete0fca292020-01-13 21:49:03 +0100849static enum act_parse_ret parse_http_deny(const char **args, int *orig_arg, struct proxy *px,
850 struct act_rule *rule, char **err)
Christopher Faulet81e20172019-12-12 16:40:30 +0100851{
Christopher Faulet554c0eb2020-01-14 12:00:28 +0100852 int default_status, status, hc, cur_arg;
853
Christopher Faulet81e20172019-12-12 16:40:30 +0100854
855 cur_arg = *orig_arg;
Christopher Faulete0fca292020-01-13 21:49:03 +0100856 if (rule->from == ACT_F_HTTP_REQ) {
857 if (!strcmp(args[cur_arg-1], "tarpit")) {
858 rule->action = ACT_HTTP_REQ_TARPIT;
Christopher Faulet554c0eb2020-01-14 12:00:28 +0100859 default_status = status = 500;
Christopher Faulet81e20172019-12-12 16:40:30 +0100860 }
Christopher Faulete0fca292020-01-13 21:49:03 +0100861 else {
862 rule->action = ACT_ACTION_DENY;
Christopher Faulet554c0eb2020-01-14 12:00:28 +0100863 default_status = status = 403;
Christopher Faulet81e20172019-12-12 16:40:30 +0100864 }
Christopher Faulet81e20172019-12-12 16:40:30 +0100865 }
Christopher Faulete0fca292020-01-13 21:49:03 +0100866 else {
Christopher Faulet554c0eb2020-01-14 12:00:28 +0100867 rule->action = ACT_ACTION_DENY;
868 default_status = status = 502;
Christopher Faulete0fca292020-01-13 21:49:03 +0100869 }
Christopher Faulet245cf792019-12-18 14:58:12 +0100870 rule->flags |= ACT_FLAG_FINAL;
Christopher Faulet040c8cd2020-01-13 16:43:45 +0100871
872 if (strcmp(args[cur_arg], "deny_status") == 0) {
873 cur_arg++;
874 if (!*args[cur_arg]) {
Christopher Faulet554c0eb2020-01-14 12:00:28 +0100875 memprintf(err, "'%s' expects <status_code> as argument", args[cur_arg-1]);
Christopher Faulet040c8cd2020-01-13 16:43:45 +0100876 return ACT_RET_PRS_ERR;
877 }
878
Christopher Faulet554c0eb2020-01-14 12:00:28 +0100879 status = atol(args[cur_arg]);
Christopher Faulet040c8cd2020-01-13 16:43:45 +0100880 cur_arg++;
881 for (hc = 0; hc < HTTP_ERR_SIZE; hc++) {
Christopher Faulet554c0eb2020-01-14 12:00:28 +0100882 if (http_err_codes[hc] == status)
Christopher Faulet040c8cd2020-01-13 16:43:45 +0100883 break;
Christopher Faulet040c8cd2020-01-13 16:43:45 +0100884 }
Christopher Faulet554c0eb2020-01-14 12:00:28 +0100885 if (hc >= HTTP_ERR_SIZE) {
886 memprintf(err, "status code '%d' not handled, using default code '%d'",
887 status, default_status);
888 status = default_status;
889 hc = http_get_status_idx(status);
890 }
Christopher Faulet040c8cd2020-01-13 16:43:45 +0100891 }
892
Christopher Faulet554c0eb2020-01-14 12:00:28 +0100893 if (strcmp(args[cur_arg], "errorfile") == 0) {
894 cur_arg++;
895 if (!*args[cur_arg]) {
896 memprintf(err, "'%s' expects <file> as argument", args[cur_arg-1]);
897 return ACT_RET_PRS_ERR;
898 }
899
900 rule->arg.http_deny.errmsg = http_load_errorfile(args[cur_arg], err);
901 if (!rule->arg.http_deny.errmsg)
902 return ACT_RET_PRS_ERR;
903 cur_arg++;
904 }
905 else if (strcmp(args[cur_arg], "errorfiles") == 0) {
906 cur_arg++;
907 if (!*args[cur_arg]) {
908 memprintf(err, "'%s' expects <http_errors_name> as argument", args[cur_arg-1]);
909 return ACT_RET_PRS_ERR;
910 }
911 /* Must be resolved during the config validity check */
912 rule->arg.act.p[0] = (void *)((intptr_t)status);
913 rule->arg.act.p[1] = strdup(args[cur_arg]);
914 rule->check_ptr = check_http_deny_action;
915 cur_arg++;
916 goto out;
917 }
918
919 rule->arg.http_deny.status = status;
920
921 out:
Christopher Faulet040c8cd2020-01-13 16:43:45 +0100922 *orig_arg = cur_arg;
Christopher Faulet81e20172019-12-12 16:40:30 +0100923 return ACT_RET_PRS_OK;
924}
925
926/* Parse a "auth" action. It may take 2 optional arguments to define a "realm"
927 * parameter. It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
928 */
929static enum act_parse_ret parse_http_auth(const char **args, int *orig_arg, struct proxy *px,
930 struct act_rule *rule, char **err)
931{
932 int cur_arg;
933
934 rule->action = ACT_HTTP_REQ_AUTH;
Christopher Faulet245cf792019-12-18 14:58:12 +0100935 rule->flags |= ACT_FLAG_FINAL;
Christopher Faulet2eb53962020-01-14 14:47:34 +0100936 rule->release_ptr = release_http_action;
Christopher Faulet81e20172019-12-12 16:40:30 +0100937
938 cur_arg = *orig_arg;
939 if (!strcmp(args[cur_arg], "realm")) {
940 cur_arg++;
941 if (!*args[cur_arg]) {
942 memprintf(err, "missing realm value.\n");
943 return ACT_RET_PRS_ERR;
944 }
Christopher Faulet96bff762019-12-17 13:46:18 +0100945 rule->arg.http.str.ptr = strdup(args[cur_arg]);
946 rule->arg.http.str.len = strlen(rule->arg.http.str.ptr);
Christopher Faulet81e20172019-12-12 16:40:30 +0100947 cur_arg++;
948 }
949
Christopher Fauletc20b3712020-01-27 15:51:56 +0100950 LIST_INIT(&rule->arg.http.fmt);
Christopher Faulet81e20172019-12-12 16:40:30 +0100951 *orig_arg = cur_arg;
952 return ACT_RET_PRS_OK;
953}
954
955/* Parse a "set-nice" action. It takes the nice value as argument. It returns
956 * ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
957 */
958static enum act_parse_ret parse_http_set_nice(const char **args, int *orig_arg, struct proxy *px,
959 struct act_rule *rule, char **err)
960{
961 int cur_arg;
962
963 rule->action = ACT_HTTP_SET_NICE;
964
965 cur_arg = *orig_arg;
966 if (!*args[cur_arg]) {
967 memprintf(err, "expects exactly 1 argument (integer value)");
968 return ACT_RET_PRS_ERR;
969 }
Christopher Faulet96bff762019-12-17 13:46:18 +0100970 rule->arg.http.i = atoi(args[cur_arg]);
971 if (rule->arg.http.i < -1024)
972 rule->arg.http.i = -1024;
973 else if (rule->arg.http.i > 1024)
974 rule->arg.http.i = 1024;
Christopher Faulet81e20172019-12-12 16:40:30 +0100975
Christopher Fauletc20b3712020-01-27 15:51:56 +0100976 LIST_INIT(&rule->arg.http.fmt);
Christopher Faulet81e20172019-12-12 16:40:30 +0100977 *orig_arg = cur_arg + 1;
978 return ACT_RET_PRS_OK;
979}
980
981/* Parse a "set-tos" action. It takes the TOS value as argument. It returns
982 * ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
983 */
984static enum act_parse_ret parse_http_set_tos(const char **args, int *orig_arg, struct proxy *px,
985 struct act_rule *rule, char **err)
986{
987#ifdef IP_TOS
988 char *endp;
989 int cur_arg;
990
991 rule->action = ACT_HTTP_SET_TOS;
992
993 cur_arg = *orig_arg;
994 if (!*args[cur_arg]) {
995 memprintf(err, "expects exactly 1 argument (integer/hex value)");
996 return ACT_RET_PRS_ERR;
997 }
Christopher Faulet96bff762019-12-17 13:46:18 +0100998 rule->arg.http.i = strtol(args[cur_arg], &endp, 0);
Christopher Faulet81e20172019-12-12 16:40:30 +0100999 if (endp && *endp != '\0') {
1000 memprintf(err, "invalid character starting at '%s' (integer/hex value expected)", endp);
1001 return ACT_RET_PRS_ERR;
1002 }
1003
Christopher Fauletc20b3712020-01-27 15:51:56 +01001004 LIST_INIT(&rule->arg.http.fmt);
Christopher Faulet81e20172019-12-12 16:40:30 +01001005 *orig_arg = cur_arg + 1;
1006 return ACT_RET_PRS_OK;
1007#else
1008 memprintf(err, "not supported on this platform (IP_TOS undefined)");
1009 return ACT_RET_PRS_ERR;
1010#endif
1011}
1012
1013/* Parse a "set-mark" action. It takes the MARK value as argument. It returns
1014 * ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
1015 */
1016static enum act_parse_ret parse_http_set_mark(const char **args, int *orig_arg, struct proxy *px,
1017 struct act_rule *rule, char **err)
1018{
1019#ifdef SO_MARK
1020 char *endp;
1021 int cur_arg;
1022
1023 rule->action = ACT_HTTP_SET_MARK;
1024
1025 cur_arg = *orig_arg;
1026 if (!*args[cur_arg]) {
1027 memprintf(err, "expects exactly 1 argument (integer/hex value)");
1028 return ACT_RET_PRS_ERR;
1029 }
Christopher Faulet96bff762019-12-17 13:46:18 +01001030 rule->arg.http.i = strtoul(args[cur_arg], &endp, 0);
Christopher Faulet81e20172019-12-12 16:40:30 +01001031 if (endp && *endp != '\0') {
1032 memprintf(err, "invalid character starting at '%s' (integer/hex value expected)", endp);
1033 return ACT_RET_PRS_ERR;
1034 }
1035
Christopher Fauletc20b3712020-01-27 15:51:56 +01001036 LIST_INIT(&rule->arg.http.fmt);
Christopher Faulet81e20172019-12-12 16:40:30 +01001037 *orig_arg = cur_arg + 1;
1038 global.last_checks |= LSTCHK_NETADM;
1039 return ACT_RET_PRS_OK;
1040#else
1041 memprintf(err, "not supported on this platform (SO_MARK undefined)");
1042 return ACT_RET_PRS_ERR;
1043#endif
1044}
1045
1046/* Parse a "set-log-level" action. It takes the level value as argument. It
1047 * returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
1048 */
1049static enum act_parse_ret parse_http_set_log_level(const char **args, int *orig_arg, struct proxy *px,
1050 struct act_rule *rule, char **err)
1051{
1052 int cur_arg;
1053
1054 rule->action = ACT_HTTP_SET_LOGL;
1055
1056 cur_arg = *orig_arg;
1057 if (!*args[cur_arg]) {
1058 bad_log_level:
1059 memprintf(err, "expects exactly 1 argument (log level name or 'silent')");
1060 return ACT_RET_PRS_ERR;
1061 }
1062 if (strcmp(args[cur_arg], "silent") == 0)
Christopher Faulet96bff762019-12-17 13:46:18 +01001063 rule->arg.http.i = -1;
1064 else if ((rule->arg.http.i = get_log_level(args[cur_arg]) + 1) == 0)
Christopher Faulet81e20172019-12-12 16:40:30 +01001065 goto bad_log_level;
1066
Christopher Fauletc20b3712020-01-27 15:51:56 +01001067 LIST_INIT(&rule->arg.http.fmt);
Christopher Faulet81e20172019-12-12 16:40:30 +01001068 *orig_arg = cur_arg + 1;
1069 return ACT_RET_PRS_OK;
1070}
1071
Christopher Faulet91b3ec12020-01-17 22:30:06 +01001072/* This function executes a early-hint action. It adds an HTTP Early Hint HTTP
1073 * 103 response header with <.arg.http.str> name and with a value built
1074 * according to <.arg.http.fmt> log line format. If it is the first early-hint
1075 * rule of a serie, the 103 response start-line is added first. At the end, if
1076 * the next rule is not an early-hint rule or if it is the last rule, the EOH
1077 * block is added to terminate the response. On success, it returns
1078 * ACT_RET_CONT. If an error occurs while soft rewrites are enabled, the action
1079 * is canceled, but the rule processing continue. Otherwsize ACT_RET_ERR is
1080 * returned.
1081 */
1082static enum act_return http_action_early_hint(struct act_rule *rule, struct proxy *px,
1083 struct session *sess, struct stream *s, int flags)
1084{
1085 struct act_rule *prev_rule, *next_rule;
1086 struct channel *res = &s->res;
1087 struct htx *htx = htx_from_buf(&res->buf);
1088 struct buffer *value = alloc_trash_chunk();
1089 enum act_return ret = ACT_RET_CONT;
1090
1091 if (!(s->txn->req.flags & HTTP_MSGF_VER_11))
1092 goto leave;
1093
1094 if (!value) {
1095 if (!(s->flags & SF_ERR_MASK))
1096 s->flags |= SF_ERR_RESOURCE;
1097 goto error;
1098 }
1099
1100 /* get previous and next rules */
1101 prev_rule = LIST_PREV(&rule->list, typeof(rule), list);
1102 next_rule = LIST_NEXT(&rule->list, typeof(rule), list);
1103
1104 /* if no previous rule or previous rule is not early-hint, start a new response. Otherwise,
1105 * continue to add link to a previously started response */
1106 if (&prev_rule->list == s->current_rule_list || prev_rule->action_ptr != http_action_early_hint) {
1107 struct htx_sl *sl;
1108 unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|
1109 HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
1110
1111 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
1112 ist("HTTP/1.1"), ist("103"), ist("Early Hints"));
1113 if (!sl)
1114 goto error;
1115 sl->info.res.status = 103;
1116 }
1117
1118 /* Add the HTTP Early Hint HTTP 103 response heade */
1119 value->data = build_logline(s, b_tail(value), b_room(value), &rule->arg.http.fmt);
1120 if (!htx_add_header(htx, rule->arg.http.str, ist2(b_head(value), b_data(value))))
1121 goto error;
1122
1123 /* if it is the last rule or the next one is not an early-hint, terminate the current
1124 * response. */
1125 if (&next_rule->list == s->current_rule_list || next_rule->action_ptr != http_action_early_hint) {
Christopher Faulet91b3ec12020-01-17 22:30:06 +01001126 if (!htx_add_endof(htx, HTX_BLK_EOH)) {
1127 /* If an error occurred during an Early-hint rule,
1128 * remove the incomplete HTTP 103 response from the
1129 * buffer */
1130 goto error;
1131 }
1132
Christopher Fauleta72a7e42020-01-28 09:28:11 +01001133 if (!http_forward_proxy_resp(s, 0))
1134 goto error;
Christopher Faulet91b3ec12020-01-17 22:30:06 +01001135 }
1136
1137 leave:
1138 free_trash_chunk(value);
1139 return ret;
1140
1141 error:
1142 /* If an error occurred during an Early-hint rule, remove the incomplete
1143 * HTTP 103 response from the buffer */
1144 channel_htx_truncate(res, htx);
1145 ret = ACT_RET_ERR;
1146 goto leave;
1147}
1148
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001149/* This function executes a set-header or add-header actions. It builds a string
1150 * in the trash from the specified format string. It finds the action to be
1151 * performed in <.action>, previously filled by function parse_set_header(). The
1152 * replacement action is excuted by the function http_action_set_header(). On
1153 * success, it returns ACT_RET_CONT. If an error occurs while soft rewrites are
1154 * enabled, the action is canceled, but the rule processing continue. Otherwsize
1155 * ACT_RET_ERR is returned.
1156 */
1157static enum act_return http_action_set_header(struct act_rule *rule, struct proxy *px,
1158 struct session *sess, struct stream *s, int flags)
1159{
Christopher Faulet91e31d82020-01-24 15:37:13 +01001160 struct http_msg *msg = ((rule->from == ACT_F_HTTP_REQ) ? &s->txn->req : &s->txn->rsp);
1161 struct htx *htx = htxbuf(&msg->chn->buf);
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001162 enum act_return ret = ACT_RET_CONT;
1163 struct buffer *replace;
1164 struct http_hdr_ctx ctx;
1165 struct ist n, v;
1166
1167 replace = alloc_trash_chunk();
1168 if (!replace)
1169 goto fail_alloc;
1170
1171 replace->data = build_logline(s, replace->area, replace->size, &rule->arg.http.fmt);
1172 n = rule->arg.http.str;
1173 v = ist2(replace->area, replace->data);
1174
1175 if (rule->action == 0) { // set-header
1176 /* remove all occurrences of the header */
1177 ctx.blk = NULL;
1178 while (http_find_header(htx, n, &ctx, 1))
1179 http_remove_header(htx, &ctx);
1180 }
1181
1182 /* Now add header */
1183 if (!http_add_header(htx, n, v))
1184 goto fail_rewrite;
1185
1186 leave:
1187 free_trash_chunk(replace);
1188 return ret;
1189
1190 fail_alloc:
1191 if (!(s->flags & SF_ERR_MASK))
1192 s->flags |= SF_ERR_RESOURCE;
1193 ret = ACT_RET_ERR;
1194 goto leave;
1195
1196 fail_rewrite:
1197 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_rewrites, 1);
1198 if (s->flags & SF_BE_ASSIGNED)
1199 _HA_ATOMIC_ADD(&s->be->be_counters.failed_rewrites, 1);
1200 if (sess->listener->counters)
1201 _HA_ATOMIC_ADD(&sess->listener->counters->failed_rewrites, 1);
1202 if (objt_server(s->target))
1203 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_rewrites, 1);
1204
Christopher Faulet333bf8c2020-01-22 14:38:05 +01001205 if (!(msg->flags & HTTP_MSGF_SOFT_RW)) {
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001206 ret = ACT_RET_ERR;
Christopher Faulet333bf8c2020-01-22 14:38:05 +01001207 if (!(s->flags & SF_ERR_MASK))
1208 s->flags |= SF_ERR_PRXCOND;
1209 }
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001210 goto leave;
1211}
1212
Christopher Faulet81e20172019-12-12 16:40:30 +01001213/* Parse a "set-header", "add-header" or "early-hint" actions. It takes an
1214 * header name and a log-format string as arguments. It returns ACT_RET_PRS_OK
1215 * on success, ACT_RET_PRS_ERR on error.
1216 *
1217 * Note: same function is used for the request and the response. However
1218 * "early-hint" rules are only supported for request rules.
1219 */
1220static enum act_parse_ret parse_http_set_header(const char **args, int *orig_arg, struct proxy *px,
1221 struct act_rule *rule, char **err)
1222{
Christopher Faulet81e20172019-12-12 16:40:30 +01001223 int cap, cur_arg;
1224
Christopher Faulet91b3ec12020-01-17 22:30:06 +01001225 if (args[*orig_arg-1][0] == 'e') {
1226 rule->action = ACT_CUSTOM;
1227 rule->action_ptr = http_action_early_hint;
1228 }
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001229 else {
1230 if (args[*orig_arg-1][0] == 's')
1231 rule->action = 0; // set-header
1232 else
1233 rule->action = 1; // add-header
1234 rule->action_ptr = http_action_set_header;
1235 }
Christopher Faulet2eb53962020-01-14 14:47:34 +01001236 rule->release_ptr = release_http_action;
Christopher Faulet81e20172019-12-12 16:40:30 +01001237
1238 cur_arg = *orig_arg;
1239 if (!*args[cur_arg] || !*args[cur_arg+1]) {
1240 memprintf(err, "expects exactly 2 arguments");
1241 return ACT_RET_PRS_ERR;
1242 }
1243
Christopher Faulet81e20172019-12-12 16:40:30 +01001244
Christopher Faulet96bff762019-12-17 13:46:18 +01001245 rule->arg.http.str.ptr = strdup(args[cur_arg]);
1246 rule->arg.http.str.len = strlen(rule->arg.http.str.ptr);
1247 LIST_INIT(&rule->arg.http.fmt);
Christopher Faulet81e20172019-12-12 16:40:30 +01001248
1249 if (rule->from == ACT_F_HTTP_REQ) {
1250 px->conf.args.ctx = ARGC_HRQ;
1251 cap = (px->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR;
1252 }
1253 else{
1254 px->conf.args.ctx = ARGC_HRS;
1255 cap = (px->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR;
1256 }
1257
1258 cur_arg++;
Christopher Faulet1337b322020-01-14 14:50:55 +01001259 if (!parse_logformat_string(args[cur_arg], px, &rule->arg.http.fmt, LOG_OPT_HTTP, cap, err)) {
1260 free(rule->arg.http.str.ptr);
Christopher Faulet81e20172019-12-12 16:40:30 +01001261 return ACT_RET_PRS_ERR;
Christopher Faulet1337b322020-01-14 14:50:55 +01001262 }
Christopher Faulet81e20172019-12-12 16:40:30 +01001263
1264 free(px->conf.lfs_file);
1265 px->conf.lfs_file = strdup(px->conf.args.file);
1266 px->conf.lfs_line = px->conf.args.line;
1267
1268 *orig_arg = cur_arg + 1;
1269 return ACT_RET_PRS_OK;
1270}
1271
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001272/* This function executes a replace-header or replace-value actions. It
1273 * builds a string in the trash from the specified format string. It finds
1274 * the action to be performed in <.action>, previously filled by function
1275 * parse_replace_header(). The replacement action is excuted by the function
1276 * http_action_replace_header(). On success, it returns ACT_RET_CONT. If an error
1277 * occurs while soft rewrites are enabled, the action is canceled, but the rule
1278 * processing continue. Otherwsize ACT_RET_ERR is returned.
1279 */
1280static enum act_return http_action_replace_header(struct act_rule *rule, struct proxy *px,
1281 struct session *sess, struct stream *s, int flags)
1282{
Christopher Faulet91e31d82020-01-24 15:37:13 +01001283 struct http_msg *msg = ((rule->from == ACT_F_HTTP_REQ) ? &s->txn->req : &s->txn->rsp);
1284 struct htx *htx = htxbuf(&msg->chn->buf);
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001285 enum act_return ret = ACT_RET_CONT;
1286 struct buffer *replace;
1287 int r;
1288
1289 replace = alloc_trash_chunk();
1290 if (!replace)
1291 goto fail_alloc;
1292
1293 replace->data = build_logline(s, replace->area, replace->size, &rule->arg.http.fmt);
1294
1295 r = http_replace_hdrs(s, htx, rule->arg.http.str, replace->area, rule->arg.http.re, (rule->action == 0));
1296 if (r == -1)
1297 goto fail_rewrite;
1298
1299 leave:
1300 free_trash_chunk(replace);
1301 return ret;
1302
1303 fail_alloc:
1304 if (!(s->flags & SF_ERR_MASK))
1305 s->flags |= SF_ERR_RESOURCE;
1306 ret = ACT_RET_ERR;
1307 goto leave;
1308
1309 fail_rewrite:
1310 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_rewrites, 1);
1311 if (s->flags & SF_BE_ASSIGNED)
1312 _HA_ATOMIC_ADD(&s->be->be_counters.failed_rewrites, 1);
1313 if (sess->listener->counters)
1314 _HA_ATOMIC_ADD(&sess->listener->counters->failed_rewrites, 1);
1315 if (objt_server(s->target))
1316 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_rewrites, 1);
1317
Christopher Faulet333bf8c2020-01-22 14:38:05 +01001318 if (!(msg->flags & HTTP_MSGF_SOFT_RW)) {
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001319 ret = ACT_RET_ERR;
Christopher Faulet333bf8c2020-01-22 14:38:05 +01001320 if (!(s->flags & SF_ERR_MASK))
1321 s->flags |= SF_ERR_PRXCOND;
1322 }
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001323 goto leave;
1324}
1325
Christopher Faulet81e20172019-12-12 16:40:30 +01001326/* Parse a "replace-header" or "replace-value" actions. It takes an header name,
1327 * a regex and replacement string as arguments. It returns ACT_RET_PRS_OK on
1328 * success, ACT_RET_PRS_ERR on error.
1329 */
1330static enum act_parse_ret parse_http_replace_header(const char **args, int *orig_arg, struct proxy *px,
1331 struct act_rule *rule, char **err)
1332{
1333 int cap, cur_arg;
1334
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001335 if (args[*orig_arg-1][8] == 'h')
1336 rule->action = 0; // replace-header
1337 else
1338 rule->action = 1; // replace-value
1339 rule->action_ptr = http_action_replace_header;
Christopher Faulet2eb53962020-01-14 14:47:34 +01001340 rule->release_ptr = release_http_action;
Christopher Faulet81e20172019-12-12 16:40:30 +01001341
1342 cur_arg = *orig_arg;
1343 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2]) {
1344 memprintf(err, "expects exactly 3 arguments");
1345 return ACT_RET_PRS_ERR;
1346 }
1347
Christopher Faulet96bff762019-12-17 13:46:18 +01001348 rule->arg.http.str.ptr = strdup(args[cur_arg]);
1349 rule->arg.http.str.len = strlen(rule->arg.http.str.ptr);
1350 LIST_INIT(&rule->arg.http.fmt);
Christopher Faulet81e20172019-12-12 16:40:30 +01001351
1352 cur_arg++;
Christopher Faulet1337b322020-01-14 14:50:55 +01001353 if (!(rule->arg.http.re = regex_comp(args[cur_arg], 1, 1, err))) {
1354 free(rule->arg.http.str.ptr);
Christopher Faulet81e20172019-12-12 16:40:30 +01001355 return ACT_RET_PRS_ERR;
Christopher Faulet1337b322020-01-14 14:50:55 +01001356 }
Christopher Faulet81e20172019-12-12 16:40:30 +01001357
1358 if (rule->from == ACT_F_HTTP_REQ) {
1359 px->conf.args.ctx = ARGC_HRQ;
1360 cap = (px->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR;
1361 }
1362 else{
1363 px->conf.args.ctx = ARGC_HRS;
1364 cap = (px->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR;
1365 }
1366
1367 cur_arg++;
Christopher Faulet1337b322020-01-14 14:50:55 +01001368 if (!parse_logformat_string(args[cur_arg], px, &rule->arg.http.fmt, LOG_OPT_HTTP, cap, err)) {
1369 free(rule->arg.http.str.ptr);
1370 regex_free(rule->arg.http.re);
Christopher Faulet81e20172019-12-12 16:40:30 +01001371 return ACT_RET_PRS_ERR;
Christopher Faulet1337b322020-01-14 14:50:55 +01001372 }
Christopher Faulet81e20172019-12-12 16:40:30 +01001373
1374 free(px->conf.lfs_file);
1375 px->conf.lfs_file = strdup(px->conf.args.file);
1376 px->conf.lfs_line = px->conf.args.line;
1377
1378 *orig_arg = cur_arg + 1;
1379 return ACT_RET_PRS_OK;
1380}
1381
1382/* Parse a "del-header" action. It takes an header name as argument. It returns
1383 * ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
1384 */
1385static enum act_parse_ret parse_http_del_header(const char **args, int *orig_arg, struct proxy *px,
1386 struct act_rule *rule, char **err)
1387{
1388 int cur_arg;
1389
1390 rule->action = ACT_HTTP_DEL_HDR;
Christopher Faulet2eb53962020-01-14 14:47:34 +01001391 rule->release_ptr = release_http_action;
Christopher Faulet81e20172019-12-12 16:40:30 +01001392
1393 cur_arg = *orig_arg;
1394 if (!*args[cur_arg]) {
1395 memprintf(err, "expects exactly 1 arguments");
1396 return ACT_RET_PRS_ERR;
1397 }
1398
Christopher Faulet96bff762019-12-17 13:46:18 +01001399 rule->arg.http.str.ptr = strdup(args[cur_arg]);
1400 rule->arg.http.str.len = strlen(rule->arg.http.str.ptr);
Christopher Faulet81e20172019-12-12 16:40:30 +01001401 px->conf.args.ctx = (rule->from == ACT_F_HTTP_REQ ? ARGC_HRQ : ARGC_HRS);
1402
Christopher Fauletc20b3712020-01-27 15:51:56 +01001403 LIST_INIT(&rule->arg.http.fmt);
Christopher Faulet81e20172019-12-12 16:40:30 +01001404 *orig_arg = cur_arg + 1;
1405 return ACT_RET_PRS_OK;
1406}
1407
Christopher Faulet2eb53962020-01-14 14:47:34 +01001408/* Release memory allocated by an http redirect action. */
1409static void release_http_redir(struct act_rule *rule)
1410{
1411 struct logformat_node *lf, *lfb;
1412 struct redirect_rule *redir;
1413
1414 redir = rule->arg.redir;
1415 LIST_DEL(&redir->list);
1416 if (redir->cond) {
1417 prune_acl_cond(redir->cond);
1418 free(redir->cond);
1419 }
1420 free(redir->rdr_str);
1421 free(redir->cookie_str);
1422 list_for_each_entry_safe(lf, lfb, &redir->rdr_fmt, list) {
1423 LIST_DEL(&lf->list);
1424 free(lf);
1425 }
1426 free(redir);
1427}
1428
Christopher Faulet81e20172019-12-12 16:40:30 +01001429/* Parse a "redirect" action. It returns ACT_RET_PRS_OK on success,
1430 * ACT_RET_PRS_ERR on error.
1431 */
1432static enum act_parse_ret parse_http_redirect(const char **args, int *orig_arg, struct proxy *px,
1433 struct act_rule *rule, char **err)
1434{
1435 struct redirect_rule *redir;
1436 int dir, cur_arg;
1437
1438 rule->action = ACT_HTTP_REDIR;
Christopher Faulet245cf792019-12-18 14:58:12 +01001439 rule->flags |= ACT_FLAG_FINAL;
Christopher Faulet2eb53962020-01-14 14:47:34 +01001440 rule->release_ptr = release_http_redir;
Christopher Faulet81e20172019-12-12 16:40:30 +01001441
1442 cur_arg = *orig_arg;
1443
1444 dir = (rule->from == ACT_F_HTTP_REQ ? 0 : 1);
1445 if ((redir = http_parse_redirect_rule(px->conf.args.file, px->conf.args.line, px, &args[cur_arg], err, 1, dir)) == NULL)
1446 return ACT_RET_PRS_ERR;
1447
1448 rule->arg.redir = redir;
1449 rule->cond = redir->cond;
1450 redir->cond = NULL;
1451
1452 /* skip all arguments */
1453 while (*args[cur_arg])
1454 cur_arg++;
1455
1456 *orig_arg = cur_arg;
1457 return ACT_RET_PRS_OK;
1458}
1459
Christopher Faulet046cf442019-12-17 15:45:23 +01001460/* This function executes a add-acl, del-acl, set-map or del-map actions. On
1461 * success, it returns ACT_RET_CONT. Otherwsize ACT_RET_ERR is returned.
1462 */
1463static enum act_return http_action_set_map(struct act_rule *rule, struct proxy *px,
1464 struct session *sess, struct stream *s, int flags)
1465{
1466 struct pat_ref *ref;
1467 struct buffer *key = NULL, *value = NULL;
1468 enum act_return ret = ACT_RET_CONT;
1469
1470 /* collect reference */
1471 ref = pat_ref_lookup(rule->arg.map.ref);
1472 if (!ref)
1473 goto leave;
1474
1475 /* allocate key */
1476 key = alloc_trash_chunk();
1477 if (!key)
1478 goto fail_alloc;
1479
1480 /* collect key */
1481 key->data = build_logline(s, key->area, key->size, &rule->arg.map.key);
1482 key->area[key->data] = '\0';
1483
1484 switch (rule->action) {
1485 case 0: // add-acl
1486 /* add entry only if it does not already exist */
1487 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
1488 if (pat_ref_find_elt(ref, key->area) == NULL)
1489 pat_ref_add(ref, key->area, NULL, NULL);
1490 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
1491 break;
1492
1493 case 1: // set-map
1494 /* allocate value */
1495 value = alloc_trash_chunk();
1496 if (!value)
1497 goto fail_alloc;
1498
1499 /* collect value */
1500 value->data = build_logline(s, value->area, value->size, &rule->arg.map.value);
1501 value->area[value->data] = '\0';
1502
1503 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
1504 if (pat_ref_find_elt(ref, key->area) != NULL) {
1505 /* update entry if it exists */
1506 pat_ref_set(ref, key->area, value->area, NULL);
1507 }
1508 else {
1509 /* insert a new entry */
1510 pat_ref_add(ref, key->area, value->area, NULL);
1511 }
1512 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
1513 break;
1514
1515 case 2: // del-acl
1516 case 3: // del-map
1517 /* returned code: 1=ok, 0=ko */
1518 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
1519 pat_ref_delete(ref, key->area);
1520 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
1521 break;
1522
1523 default:
1524 ret = ACT_RET_ERR;
1525 }
1526
1527
1528 leave:
1529 free_trash_chunk(key);
1530 free_trash_chunk(value);
1531 return ret;
1532
1533 fail_alloc:
1534 if (!(s->flags & SF_ERR_MASK))
1535 s->flags |= SF_ERR_RESOURCE;
1536 ret = ACT_RET_ERR;
1537 goto leave;
1538}
1539
Christopher Faulet2eb53962020-01-14 14:47:34 +01001540/* Release memory allocated by an http map/acl action. */
1541static void release_http_map(struct act_rule *rule)
1542{
1543 struct logformat_node *lf, *lfb;
1544
1545 free(rule->arg.map.ref);
1546 list_for_each_entry_safe(lf, lfb, &rule->arg.map.key, list) {
1547 LIST_DEL(&lf->list);
1548 release_sample_expr(lf->expr);
1549 free(lf->arg);
1550 free(lf);
1551 }
1552 if (rule->action == 1) {
1553 list_for_each_entry_safe(lf, lfb, &rule->arg.map.value, list) {
1554 LIST_DEL(&lf->list);
1555 release_sample_expr(lf->expr);
1556 free(lf->arg);
1557 free(lf);
1558 }
1559 }
1560}
1561
Christopher Faulet81e20172019-12-12 16:40:30 +01001562/* Parse a "add-acl", "del-acl", "set-map" or "del-map" actions. It takes one or
Christopher Faulet046cf442019-12-17 15:45:23 +01001563 * two log-format string as argument depending on the action. The action is
1564 * stored in <.action> as an int (0=add-acl, 1=set-map, 2=del-acl,
1565 * 3=del-map). It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Christopher Faulet81e20172019-12-12 16:40:30 +01001566 */
1567static enum act_parse_ret parse_http_set_map(const char **args, int *orig_arg, struct proxy *px,
1568 struct act_rule *rule, char **err)
1569{
1570 int cap, cur_arg;
1571
Christopher Faulet046cf442019-12-17 15:45:23 +01001572 if (args[*orig_arg-1][0] == 'a') // add-acl
1573 rule->action = 0;
1574 else if (args[*orig_arg-1][0] == 's') // set-map
1575 rule->action = 1;
1576 else if (args[*orig_arg-1][4] == 'a') // del-acl
1577 rule->action = 2;
1578 else if (args[*orig_arg-1][4] == 'm') // del-map
1579 rule->action = 3;
1580 else {
1581 memprintf(err, "internal error: unhandled action '%s'", args[0]);
1582 return ACT_RET_PRS_ERR;
1583 }
1584 rule->action_ptr = http_action_set_map;
Christopher Faulet2eb53962020-01-14 14:47:34 +01001585 rule->release_ptr = release_http_map;
Christopher Faulet81e20172019-12-12 16:40:30 +01001586
1587 cur_arg = *orig_arg;
Christopher Faulet046cf442019-12-17 15:45:23 +01001588 if (rule->action == 1 && (!*args[cur_arg] || !*args[cur_arg+1])) {
1589 /* 2 args for set-map */
Christopher Faulet81e20172019-12-12 16:40:30 +01001590 memprintf(err, "expects exactly 2 arguments");
1591 return ACT_RET_PRS_ERR;
1592 }
1593 else if (!*args[cur_arg]) {
Christopher Faulet046cf442019-12-17 15:45:23 +01001594 /* only one arg for other actions */
Christopher Faulet81e20172019-12-12 16:40:30 +01001595 memprintf(err, "expects exactly 1 arguments");
1596 return ACT_RET_PRS_ERR;
1597 }
1598
1599 /*
1600 * '+ 8' for 'set-map(' (same for del-map)
1601 * '- 9' for 'set-map(' + trailing ')' (same for del-map)
1602 */
1603 rule->arg.map.ref = my_strndup(args[cur_arg-1] + 8, strlen(args[cur_arg-1]) - 9);
1604
1605 if (rule->from == ACT_F_HTTP_REQ) {
1606 px->conf.args.ctx = ARGC_HRQ;
1607 cap = (px->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR;
1608 }
1609 else{
1610 px->conf.args.ctx = ARGC_HRS;
1611 cap = (px->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR;
1612 }
1613
1614 /* key pattern */
1615 LIST_INIT(&rule->arg.map.key);
Christopher Faulet1337b322020-01-14 14:50:55 +01001616 if (!parse_logformat_string(args[cur_arg], px, &rule->arg.map.key, LOG_OPT_HTTP, cap, err)) {
1617 free(rule->arg.map.ref);
Christopher Faulet81e20172019-12-12 16:40:30 +01001618 return ACT_RET_PRS_ERR;
Christopher Faulet1337b322020-01-14 14:50:55 +01001619 }
Christopher Faulet81e20172019-12-12 16:40:30 +01001620
Christopher Faulet046cf442019-12-17 15:45:23 +01001621 if (rule->action == 1) {
Christopher Faulet81e20172019-12-12 16:40:30 +01001622 /* value pattern for set-map only */
1623 cur_arg++;
1624 LIST_INIT(&rule->arg.map.value);
Christopher Faulet1337b322020-01-14 14:50:55 +01001625 if (!parse_logformat_string(args[cur_arg], px, &rule->arg.map.value, LOG_OPT_HTTP, cap, err)) {
1626 free(rule->arg.map.ref);
Christopher Faulet81e20172019-12-12 16:40:30 +01001627 return ACT_RET_PRS_ERR;
Christopher Faulet1337b322020-01-14 14:50:55 +01001628 }
Christopher Faulet81e20172019-12-12 16:40:30 +01001629 }
1630
1631 free(px->conf.lfs_file);
1632 px->conf.lfs_file = strdup(px->conf.args.file);
1633 px->conf.lfs_line = px->conf.args.line;
1634
1635 *orig_arg = cur_arg + 1;
1636 return ACT_RET_PRS_OK;
1637}
1638
Christopher Fauletac98d812019-12-18 09:20:16 +01001639/* This function executes a track-sc* actions. On success, it returns
1640 * ACT_RET_CONT. Otherwsize ACT_RET_ERR is returned.
1641 */
1642static enum act_return http_action_track_sc(struct act_rule *rule, struct proxy *px,
1643 struct session *sess, struct stream *s, int flags)
1644{
1645 struct stktable *t;
1646 struct stksess *ts;
1647 struct stktable_key *key;
1648 void *ptr1, *ptr2, *ptr3, *ptr4;
1649 int opt;
1650
1651 ptr1 = ptr2 = ptr3 = ptr4 = NULL;
1652 opt = ((rule->from == ACT_F_HTTP_REQ) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES) | SMP_OPT_FINAL;
1653
1654 t = rule->arg.trk_ctr.table.t;
1655 key = stktable_fetch_key(t, s->be, sess, s, opt, rule->arg.trk_ctr.expr, NULL);
1656
1657 if (!key)
1658 goto end;
1659 ts = stktable_get_entry(t, key);
1660 if (!ts)
1661 goto end;
1662
1663 stream_track_stkctr(&s->stkctr[rule->action], t, ts);
1664
1665 /* let's count a new HTTP request as it's the first time we do it */
1666 ptr1 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
1667 ptr2 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
1668
1669 /* When the client triggers a 4xx from the server, it's most often due
1670 * to a missing object or permission. These events should be tracked
1671 * because if they happen often, it may indicate a brute force or a
1672 * vulnerability scan. Normally this is done when receiving the response
1673 * but here we're tracking after this ought to have been done so we have
1674 * to do it on purpose.
1675 */
1676 if (rule->from == ACT_F_HTTP_RES && (unsigned)(s->txn->status - 400) < 100) {
1677 ptr3 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_CNT);
1678 ptr4 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_RATE);
1679 }
1680
1681 if (ptr1 || ptr2 || ptr3 || ptr4) {
1682 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
1683
1684 if (ptr1)
1685 stktable_data_cast(ptr1, http_req_cnt)++;
1686 if (ptr2)
1687 update_freq_ctr_period(&stktable_data_cast(ptr2, http_req_rate),
1688 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
1689 if (ptr3)
1690 stktable_data_cast(ptr3, http_err_cnt)++;
1691 if (ptr4)
1692 update_freq_ctr_period(&stktable_data_cast(ptr4, http_err_rate),
1693 t->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1);
1694
1695 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
1696
1697 /* If data was modified, we need to touch to re-schedule sync */
1698 stktable_touch_local(t, ts, 0);
1699 }
1700
1701 stkctr_set_flags(&s->stkctr[rule->action], STKCTR_TRACK_CONTENT);
1702 if (sess->fe != s->be)
1703 stkctr_set_flags(&s->stkctr[rule->action], STKCTR_TRACK_BACKEND);
1704
1705 end:
1706 return ACT_RET_CONT;
1707}
Christopher Faulet81e20172019-12-12 16:40:30 +01001708
Christopher Faulet2eb53962020-01-14 14:47:34 +01001709static void release_http_track_sc(struct act_rule *rule)
1710{
1711 release_sample_expr(rule->arg.trk_ctr.expr);
1712}
1713
Christopher Faulet81e20172019-12-12 16:40:30 +01001714/* Parse a "track-sc*" actions. It returns ACT_RET_PRS_OK on success,
1715 * ACT_RET_PRS_ERR on error.
1716 */
1717static enum act_parse_ret parse_http_track_sc(const char **args, int *orig_arg, struct proxy *px,
1718 struct act_rule *rule, char **err)
1719{
1720 struct sample_expr *expr;
1721 unsigned int where;
1722 unsigned int tsc_num;
1723 const char *tsc_num_str;
1724 int cur_arg;
1725
1726 tsc_num_str = &args[*orig_arg-1][8];
1727 if (cfg_parse_track_sc_num(&tsc_num, tsc_num_str, tsc_num_str + strlen(tsc_num_str), err) == -1)
1728 return ACT_RET_PRS_ERR;
1729
1730 cur_arg = *orig_arg;
1731 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line,
Willy Tarreaue3b57bf2020-02-14 16:50:14 +01001732 err, &px->conf.args, NULL);
Christopher Faulet81e20172019-12-12 16:40:30 +01001733 if (!expr)
1734 return ACT_RET_PRS_ERR;
1735
1736 where = 0;
1737 if (px->cap & PR_CAP_FE)
1738 where |= (rule->from == ACT_F_HTTP_REQ ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_FE_HRS_HDR);
1739 if (px->cap & PR_CAP_BE)
1740 where |= (rule->from == ACT_F_HTTP_REQ ? SMP_VAL_BE_HRQ_HDR : SMP_VAL_BE_HRS_HDR);
1741
1742 if (!(expr->fetch->val & where)) {
1743 memprintf(err, "fetch method '%s' extracts information from '%s', none of which is available here",
1744 args[cur_arg-1], sample_src_names(expr->fetch->use));
Christopher Faulet1337b322020-01-14 14:50:55 +01001745 release_sample_expr(expr);
Christopher Faulet81e20172019-12-12 16:40:30 +01001746 return ACT_RET_PRS_ERR;
1747 }
1748
1749 if (strcmp(args[cur_arg], "table") == 0) {
1750 cur_arg++;
1751 if (!*args[cur_arg]) {
1752 memprintf(err, "missing table name");
Christopher Faulet1337b322020-01-14 14:50:55 +01001753 release_sample_expr(expr);
Christopher Faulet81e20172019-12-12 16:40:30 +01001754 return ACT_RET_PRS_ERR;
1755 }
1756
1757 /* we copy the table name for now, it will be resolved later */
1758 rule->arg.trk_ctr.table.n = strdup(args[cur_arg]);
1759 cur_arg++;
1760 }
1761
Christopher Fauletac98d812019-12-18 09:20:16 +01001762 rule->action = tsc_num;
Christopher Faulet81e20172019-12-12 16:40:30 +01001763 rule->arg.trk_ctr.expr = expr;
Christopher Fauletac98d812019-12-18 09:20:16 +01001764 rule->action_ptr = http_action_track_sc;
Christopher Faulet2eb53962020-01-14 14:47:34 +01001765 rule->release_ptr = release_http_track_sc;
Christopher Faulet81e20172019-12-12 16:40:30 +01001766 rule->check_ptr = check_trk_action;
1767
1768 *orig_arg = cur_arg;
1769 return ACT_RET_PRS_OK;
1770}
1771
Christopher Faulet46f95542019-12-20 10:07:22 +01001772/* This function executes a strict-mode actions. On success, it always returns
1773 * ACT_RET_CONT
1774 */
1775static enum act_return http_action_strict_mode(struct act_rule *rule, struct proxy *px,
1776 struct session *sess, struct stream *s, int flags)
1777{
1778 struct http_msg *msg = ((rule->from == ACT_F_HTTP_REQ) ? &s->txn->req : &s->txn->rsp);
1779
1780 if (rule->action == 0) // strict-mode on
1781 msg->flags &= ~HTTP_MSGF_SOFT_RW;
1782 else // strict-mode off
1783 msg->flags |= HTTP_MSGF_SOFT_RW;
1784 return ACT_RET_CONT;
1785}
1786
1787/* Parse a "strict-mode" action. It returns ACT_RET_PRS_OK on success,
1788 * ACT_RET_PRS_ERR on error.
1789 */
1790static enum act_parse_ret parse_http_strict_mode(const char **args, int *orig_arg, struct proxy *px,
1791 struct act_rule *rule, char **err)
1792{
1793 int cur_arg;
1794
Christopher Faulet46f95542019-12-20 10:07:22 +01001795 cur_arg = *orig_arg;
1796 if (!*args[cur_arg]) {
1797 memprintf(err, "expects exactly 1 arguments");
1798 return ACT_RET_PRS_ERR;
1799 }
1800
1801 if (strcasecmp(args[cur_arg], "on") == 0)
1802 rule->action = 0; // strict-mode on
1803 else if (strcasecmp(args[cur_arg], "off") == 0)
1804 rule->action = 1; // strict-mode off
1805 else {
1806 memprintf(err, "Unexpected value '%s'. Only 'on' and 'off' are supported", args[cur_arg]);
1807 return ACT_RET_PRS_ERR;
1808 }
1809 rule->action_ptr = http_action_strict_mode;
1810
1811 *orig_arg = cur_arg + 1;
1812 return ACT_RET_PRS_OK;
1813}
1814
Christopher Faulet24231ab2020-01-24 17:44:23 +01001815/* Release <.arg.http_return> */
1816static void release_http_return(struct act_rule *rule)
1817{
1818 struct logformat_node *lf, *lfb;
Christopher Faulet4a2c1422020-01-31 17:36:01 +01001819 struct http_ret_hdr *hdr, *hdrb;
Christopher Faulet24231ab2020-01-24 17:44:23 +01001820
1821 free(rule->arg.http_return.ctype);
Christopher Faulet4a2c1422020-01-31 17:36:01 +01001822
1823 if (rule->arg.http_return.hdrs) {
1824 list_for_each_entry_safe(hdr, hdrb, rule->arg.http_return.hdrs, list) {
1825 LIST_DEL(&hdr->list);
1826 list_for_each_entry_safe(lf, lfb, &hdr->value, list) {
1827 LIST_DEL(&lf->list);
1828 release_sample_expr(lf->expr);
1829 free(lf->arg);
1830 free(lf);
1831 }
1832 free(hdr->name.ptr);
1833 free(hdr);
1834 }
1835 free(rule->arg.http_return.hdrs);
1836 }
1837
Christopher Faulet24231ab2020-01-24 17:44:23 +01001838 if (rule->action == 2) {
1839 chunk_destroy(&rule->arg.http_return.body.obj);
1840 }
1841 else if (rule->action == 3) {
1842 list_for_each_entry_safe(lf, lfb, &rule->arg.http_return.body.fmt, list) {
1843 LIST_DEL(&lf->list);
1844 release_sample_expr(lf->expr);
1845 free(lf->arg);
1846 free(lf);
1847 }
1848 }
1849}
1850
1851/* This function executes a return action. It builds an HTX message from an
1852 * errorfile, an raw file or a log-format string, depending on <.action>
1853 * value. On success, it returns ACT_RET_ABRT. If an error occurs ACT_RET_ERR is
1854 * returned.
1855 */
1856static enum act_return http_action_return(struct act_rule *rule, struct proxy *px,
1857 struct session *sess, struct stream *s, int flags)
1858{
1859 struct channel *req = &s->req;
1860 struct channel *res = &s->res;
1861 struct buffer *errmsg;
1862 struct htx *htx = htx_from_buf(&res->buf);
1863 struct htx_sl *sl;
1864 struct buffer *body = NULL;
1865 const char *status, *reason, *clen, *ctype;
1866 unsigned int slflags;
1867 enum act_return ret = ACT_RET_DONE;
1868
1869 s->txn->status = rule->arg.http_return.status;
1870 channel_htx_truncate(res, htx);
1871
1872 if (rule->action == 1) {
1873 /* implicit or explicit error message*/
1874 errmsg = rule->arg.http_return.body.errmsg;
1875 if (!errmsg) {
1876 /* get default error message */
1877 errmsg = http_error_message(s);
1878 }
1879 if (b_is_null(errmsg))
1880 goto end;
1881 if (!channel_htx_copy_msg(res, htx, errmsg))
1882 goto fail;
1883 }
1884 else {
1885 /* no payload, file or log-format string */
1886 if (rule->action == 2) {
1887 /* file */
1888 body = &rule->arg.http_return.body.obj;
1889 }
1890 else if (rule->action == 3) {
1891 /* log-format string */
1892 body = alloc_trash_chunk();
1893 if (!body)
1894 goto fail_alloc;
1895 body->data = build_logline(s, body->area, body->size, &rule->arg.http_return.body.fmt);
1896 }
1897 /* else no payload */
1898
1899 status = ultoa(rule->arg.http_return.status);
1900 reason = http_get_reason(rule->arg.http_return.status);
1901 slflags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CLEN);
1902 if (!body || !b_data(body))
1903 slflags |= HTX_SL_F_BODYLESS;
1904 sl = htx_add_stline(htx, HTX_BLK_RES_SL, slflags, ist("HTTP/1.1"), ist(status), ist(reason));
1905 if (!sl)
1906 goto fail;
1907 sl->info.res.status = rule->arg.http_return.status;
1908
1909 clen = (body ? ultoa(b_data(body)) : "0");
1910 ctype = rule->arg.http_return.ctype;
1911
Christopher Faulet4a2c1422020-01-31 17:36:01 +01001912 if (rule->arg.http_return.hdrs) {
1913 struct http_ret_hdr *hdr;
1914 struct buffer *value = alloc_trash_chunk();
1915
1916 if (!value)
1917 goto fail;
1918
1919 list_for_each_entry(hdr, rule->arg.http_return.hdrs, list) {
1920 chunk_reset(value);
1921 value->data = build_logline(s, value->area, value->size, &hdr->value);
1922 if (b_data(value) && !htx_add_header(htx, hdr->name, ist2(b_head(value), b_data(value)))) {
1923 free_trash_chunk(value);
1924 goto fail;
1925 }
1926 chunk_reset(value);
1927 }
1928 free_trash_chunk(value);
1929 }
1930
Christopher Faulet24231ab2020-01-24 17:44:23 +01001931 if (!htx_add_header(htx, ist("content-length"), ist(clen)) ||
1932 (body && b_data(body) && ctype && !htx_add_header(htx, ist("content-type"), ist(ctype))) ||
1933 !htx_add_endof(htx, HTX_BLK_EOH) ||
1934 (body && b_data(body) && !htx_add_data_atonce(htx, ist2(b_head(body), b_data(body)))) ||
1935 !htx_add_endof(htx, HTX_BLK_EOM))
1936 goto fail;
1937 }
1938
1939 htx_to_buf(htx, &s->res.buf);
1940 if (!http_forward_proxy_resp(s, 1))
1941 goto fail;
1942
1943 end:
1944 if (rule->from == ACT_F_HTTP_REQ) {
1945 /* let's log the request time */
1946 s->logs.tv_request = now;
1947 req->analysers &= AN_REQ_FLT_END;
1948
1949 if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */
1950 _HA_ATOMIC_ADD(&s->sess->fe->fe_counters.intercepted_req, 1);
1951 }
1952
1953 if (!(s->flags & SF_ERR_MASK))
1954 s->flags |= SF_ERR_LOCAL;
1955 if (!(s->flags & SF_FINST_MASK))
1956 s->flags |= ((rule->from == ACT_F_HTTP_REQ) ? SF_FINST_R : SF_FINST_H);
1957
1958 leave:
1959 if (rule->action == 3)
1960 free_trash_chunk(body);
1961 return ret;
1962
1963 fail_alloc:
1964 if (!(s->flags & SF_ERR_MASK))
1965 s->flags |= SF_ERR_RESOURCE;
1966 ret = ACT_RET_ERR;
1967 goto leave;
1968
1969 fail:
1970 /* If an error occurred, remove the incomplete HTTP response from the
1971 * buffer */
1972 channel_htx_truncate(res, htx);
1973 ret = ACT_RET_ERR;
1974 if (!(s->flags & SF_ERR_MASK))
1975 s->flags |= SF_ERR_PRXCOND;
1976 goto leave;
1977}
1978
1979/* Check an "http-request return" action when an http-errors section is referenced.
1980 *
1981 * The function returns 1 in success case, otherwise, it returns 0 and err is
1982 * filled.
1983 */
1984static int check_http_return_action(struct act_rule *rule, struct proxy *px, char **err)
1985{
1986 struct http_errors *http_errs;
1987 int status = (intptr_t)(rule->arg.act.p[0]);
1988 int ret = 1;
1989
1990 list_for_each_entry(http_errs, &http_errors_list, list) {
1991 if (strcmp(http_errs->id, (char *)rule->arg.act.p[1]) == 0) {
1992 free(rule->arg.act.p[1]);
1993 rule->arg.http_return.status = status;
1994 rule->arg.http_return.ctype = NULL;
1995 rule->action = 1;
1996 rule->arg.http_return.body.errmsg = http_errs->errmsg[http_get_status_idx(status)];
1997 rule->action_ptr = http_action_return;
1998 rule->release_ptr = release_http_return;
1999
2000 if (!rule->arg.http_return.body.errmsg)
2001 ha_warning("Proxy '%s': status '%d' referenced by http return rule "
2002 "not declared in http-errors section '%s'.\n",
2003 px->id, status, http_errs->id);
2004 break;
2005 }
2006 }
2007
2008 if (&http_errs->list == &http_errors_list) {
2009 memprintf(err, "unknown http-errors section '%s' referenced by http return rule",
2010 (char *)rule->arg.act.p[1]);
2011 free(rule->arg.act.p[1]);
2012 ret = 0;
2013 }
2014
2015 return ret;
2016}
2017
2018/* Parse a "return" action. It returns ACT_RET_PRS_OK on success,
2019 * ACT_RET_PRS_ERR on error. This function creates 4 action types:
2020 *
2021 * - action 0 : dummy response, no payload
2022 * - action 1 : implicit error message depending on the status code or explicit one
2023 * - action 2 : explicit file oject ('file' argument)
2024 * - action 3 : explicit log-format string ('content' argument)
2025 *
2026 * The content-type must be defined for non-empty payload. It is ignored for
2027 * error messages (implicit or explicit). When an http-errors section is
2028 * referenced, action is set to -1 and the real action is resolved during the
2029 * configuration validity check.
2030 */
2031static enum act_parse_ret parse_http_return(const char **args, int *orig_arg, struct proxy *px,
2032 struct act_rule *rule, char **err)
2033{
2034 struct logformat_node *lf, *lfb;
Christopher Faulet4a2c1422020-01-31 17:36:01 +01002035 struct http_ret_hdr *hdr, *hdrb;
2036 struct list *hdrs = NULL;
Christopher Faulet24231ab2020-01-24 17:44:23 +01002037 struct stat stat;
2038 const char *file = NULL, *act_arg = NULL;
2039 char *obj = NULL, *ctype = NULL, *name = NULL;
2040 int cur_arg, cap, objlen = 0, action = 0, status = 200, fd = -1;
2041
Christopher Faulet4a2c1422020-01-31 17:36:01 +01002042 hdrs = calloc(1, sizeof(*hdrs));
2043 if (!hdrs) {
2044 memprintf(err, "out of memory");
2045 goto error;
2046 }
2047 LIST_INIT(hdrs);
Christopher Faulet24231ab2020-01-24 17:44:23 +01002048
Christopher Faulet4a2c1422020-01-31 17:36:01 +01002049 cur_arg = *orig_arg;
Christopher Faulet24231ab2020-01-24 17:44:23 +01002050 while (*args[cur_arg]) {
2051 if (strcmp(args[cur_arg], "status") == 0) {
2052 cur_arg++;
2053 if (!*args[cur_arg]) {
2054 memprintf(err, "'%s' expects <status_code> as argument", args[cur_arg-1]);
Christopher Faulet817c4e32020-02-07 10:26:23 +01002055 goto error;
Christopher Faulet24231ab2020-01-24 17:44:23 +01002056 }
2057 status = atol(args[cur_arg]);
2058 if (status < 200 || status > 599) {
2059 memprintf(err, "Unexpected status code '%d'", status);
2060 goto error;
2061 }
2062 cur_arg++;
2063 }
2064 else if (strcmp(args[cur_arg], "content-type") == 0) {
2065 cur_arg++;
2066 if (!*args[cur_arg]) {
2067 memprintf(err, "'%s' expects <ctype> as argument", args[cur_arg-1]);
2068 goto error;
2069 }
2070 free(ctype);
2071 ctype = strdup(args[cur_arg]);
2072 cur_arg++;
2073 }
2074 else if (strcmp(args[cur_arg], "errorfiles") == 0) {
2075 if (action != 0) {
2076 memprintf(err, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
2077 goto error;
2078 }
2079 act_arg = args[cur_arg];
2080 cur_arg++;
2081 if (!*args[cur_arg]) {
2082 memprintf(err, "'%s' expects <name> as argument", args[cur_arg-1]);
2083 goto error;
2084 }
2085 /* Must be resolved during the config validity check */
2086 name = strdup(args[cur_arg]);
2087 action = -1;
2088 cur_arg++;
2089 }
2090 else if (strcmp(args[cur_arg], "default-errorfiles") == 0) {
2091 if (action != 0) {
2092 memprintf(err, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
2093 goto error;
2094 }
2095 act_arg = args[cur_arg];
2096 action = 1;
2097 cur_arg++;
2098 }
2099 else if (strcmp(args[cur_arg], "errorfile") == 0) {
2100 if (action != 0) {
2101 memprintf(err, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
2102 goto error;
2103 }
2104 act_arg = args[cur_arg];
2105 cur_arg++;
2106 if (!*args[cur_arg]) {
2107 memprintf(err, "'%s' expects <fmt> as argument", args[cur_arg-1]);
2108 goto error;
2109 }
2110 file = args[cur_arg];
2111 rule->arg.http_return.body.errmsg = http_load_errorfile(args[cur_arg], err);
2112 if (!rule->arg.http_return.body.errmsg) {
2113 goto error;
2114 }
2115 action = 1;
2116 cur_arg++;
2117 }
2118 else if (strcmp(args[cur_arg], "file") == 0) {
2119 if (action != 0) {
2120 memprintf(err, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
2121 goto error;
2122 }
2123 act_arg = args[cur_arg];
2124 cur_arg++;
2125 if (!*args[cur_arg]) {
2126 memprintf(err, "'%s' expects <file> as argument", args[cur_arg-1]);
2127 goto error;
2128 }
2129 file = args[cur_arg];
2130 fd = open(args[cur_arg], O_RDONLY);
2131 if ((fd < 0) || (fstat(fd, &stat) < 0)) {
2132 memprintf(err, "error opening file '%s'", args[cur_arg]);
2133 goto error;
2134 }
2135 if (stat.st_size > global.tune.bufsize) {
Willy Tarreaue35d1d42020-02-11 10:58:56 +01002136 memprintf(err, "file '%s' exceeds the buffer size (%lld > %d)",
2137 args[cur_arg], (long long)stat.st_size, global.tune.bufsize);
Christopher Faulet24231ab2020-01-24 17:44:23 +01002138 goto error;
2139 }
2140 objlen = stat.st_size;
2141 obj = malloc(objlen);
2142 if (!obj || read(fd, obj, objlen) != objlen) {
2143 memprintf(err, "error reading file '%s'", args[cur_arg]);
2144 goto error;
2145 }
2146 close(fd);
2147 fd = -1;
2148 action = 2;
2149 cur_arg++;
2150 }
2151 else if (strcmp(args[cur_arg], "string") == 0) {
2152 if (action != 0) {
2153 memprintf(err, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
2154 goto error;
2155 }
2156 act_arg = args[cur_arg];
2157 cur_arg++;
2158 if (!*args[cur_arg]) {
2159 memprintf(err, "'%s' expects <str> as argument", args[cur_arg-1]);
2160 goto error;
2161 }
2162 obj = strdup(args[cur_arg]);
2163 objlen = strlen(args[cur_arg]);
2164 action = 2;
2165 cur_arg++;
2166 }
2167 else if (strcmp(args[cur_arg], "lf-file") == 0) {
2168 if (action != 0) {
2169 memprintf(err, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
2170 goto error;
2171 }
2172 act_arg = args[cur_arg];
2173 cur_arg++;
2174 if (!*args[cur_arg]) {
2175 memprintf(err, "'%s' expects <file> as argument", args[cur_arg-1]);
2176 goto error;
2177 }
2178 file = args[cur_arg];
2179 fd = open(args[cur_arg], O_RDONLY);
2180 if ((fd < 0) || (fstat(fd, &stat) < 0)) {
2181 memprintf(err, "error opening file '%s'", args[cur_arg]);
2182 goto error;
2183 }
2184 if (stat.st_size > global.tune.bufsize) {
Willy Tarreaue35d1d42020-02-11 10:58:56 +01002185 memprintf(err, "file '%s' exceeds the buffer size (%lld > %d)",
2186 args[cur_arg], (long long)stat.st_size, global.tune.bufsize);
Christopher Faulet24231ab2020-01-24 17:44:23 +01002187 goto error;
2188 }
2189 objlen = stat.st_size;
2190 obj = malloc(objlen + 1);
2191 if (!obj || read(fd, obj, objlen) != objlen) {
2192 memprintf(err, "error reading file '%s'", args[cur_arg]);
2193 goto error;
2194 }
2195 close(fd);
2196 fd = -1;
2197 obj[objlen] = '\0';
2198 action = 3;
2199 cur_arg++;
2200 }
2201 else if (strcmp(args[cur_arg], "lf-string") == 0) {
2202 if (action != 0) {
2203 memprintf(err, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
2204 goto error;
2205 }
2206 act_arg = args[cur_arg];
2207 cur_arg++;
2208 if (!*args[cur_arg]) {
2209 memprintf(err, "'%s' expects <fmt> as argument", args[cur_arg-1]);
2210 goto error;
2211 }
2212 obj = strdup(args[cur_arg]);
2213 objlen = strlen(args[cur_arg]);
2214 action = 3;
2215 cur_arg++;
2216 }
Christopher Faulet4a2c1422020-01-31 17:36:01 +01002217 else if (strcmp(args[cur_arg], "hdr") == 0) {
2218 cur_arg++;
2219 if (!*args[cur_arg] || !*args[cur_arg+1]) {
2220 memprintf(err, "'%s' expects <name> and <value> as arguments", args[cur_arg-1]);
2221 goto error;
2222 }
2223 if (strcasecmp(args[cur_arg], "content-length") == 0 ||
2224 strcasecmp(args[cur_arg], "transfer-encoding") == 0 ||
2225 strcasecmp(args[cur_arg], "content-type") == 0) {
2226 ha_warning("parsing [%s:%d] : 'http-%s return' : header '%s' ignored.\n",
2227 px->conf.args.file, px->conf.args.line,
2228 (rule->from == ACT_F_HTTP_REQ ? "request" : "response"),
2229 args[cur_arg]);
2230 cur_arg += 2;
2231 continue;
2232 }
2233 hdr = calloc(1, sizeof(*hdr));
2234 if (!hdr) {
2235 memprintf(err, "'%s' : out of memory", args[cur_arg-1]);
2236 goto error;
2237 }
2238 LIST_INIT(&hdr->value);
2239 hdr->name = ist(strdup(args[cur_arg]));
2240 LIST_ADDQ(hdrs, &hdr->list);
2241
2242 if (rule->from == ACT_F_HTTP_REQ) {
2243 px->conf.args.ctx = ARGC_HRQ;
2244 cap = (px->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR;
2245 }
2246 else {
2247 px->conf.args.ctx = ARGC_HRS;
2248 cap = (px->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR;
2249 }
2250 if (!parse_logformat_string(args[cur_arg+1], px, &hdr->value, LOG_OPT_HTTP, cap, err))
2251 goto error;
2252
2253 free(px->conf.lfs_file);
2254 px->conf.lfs_file = strdup(px->conf.args.file);
2255 px->conf.lfs_line = px->conf.args.line;
2256 cur_arg += 2;
2257 }
Christopher Faulet24231ab2020-01-24 17:44:23 +01002258 else
2259 break;
2260 }
2261
2262
2263 if (action == -1) { /* errorfiles */
2264 int rc;
2265
2266 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
2267 if (http_err_codes[rc] == status)
2268 break;
2269 }
2270
2271 if (rc >= HTTP_ERR_SIZE) {
2272 memprintf(err, "status code '%d' not handled by default with '%s' argument.",
2273 status, act_arg);
2274 goto error;
2275 }
2276 if (ctype) {
2277 ha_warning("parsing [%s:%d] : 'http-%s return' : content-type '%s' ignored when the "
2278 "returned response is an erorrfile.\n",
2279 px->conf.args.file, px->conf.args.line,
2280 (rule->from == ACT_F_HTTP_REQ ? "request" : "response"),
2281 ctype);
2282 free(ctype);
2283 ctype = NULL;
2284 }
Christopher Faulet4a2c1422020-01-31 17:36:01 +01002285 if (!LIST_ISEMPTY(hdrs)) {
2286 ha_warning("parsing [%s:%d] : 'http-%s return' : hdr parameters ignored when the "
2287 "returned response is an erorrfile.\n",
2288 px->conf.args.file, px->conf.args.line,
2289 (rule->from == ACT_F_HTTP_REQ ? "request" : "response"));
2290 list_for_each_entry_safe(hdr, hdrb, hdrs, list) {
2291 LIST_DEL(&hdr->list);
2292 list_for_each_entry_safe(lf, lfb, &hdr->value, list) {
2293 LIST_DEL(&lf->list);
2294 release_sample_expr(lf->expr);
2295 free(lf->arg);
2296 free(lf);
2297 }
2298 free(hdr->name.ptr);
2299 free(hdr);
2300 }
2301 }
2302 free(hdrs);
2303 hdrs = NULL;
Christopher Faulet24231ab2020-01-24 17:44:23 +01002304
2305 rule->arg.act.p[0] = (void *)((intptr_t)status);
2306 rule->arg.act.p[1] = name;
2307 rule->check_ptr = check_http_return_action;
2308 goto out;
2309 }
2310 else if (action == 0) { /* no payload */
2311 if (ctype) {
2312 ha_warning("parsing [%s:%d] : 'http-%s return' : content-type '%s' ignored because"
2313 " neither errorfile nor payload defined.\n",
2314 px->conf.args.file, px->conf.args.line,
2315 (rule->from == ACT_F_HTTP_REQ ? "request" : "response"),
2316 ctype);
2317 free(ctype);
2318 ctype = NULL;
2319 }
2320 }
2321 else if (action == 1) { /* errorfile */
2322 if (!rule->arg.http_return.body.errmsg) { /* default errorfile */
2323 int rc;
2324
2325 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
2326 if (http_err_codes[rc] == status)
2327 break;
2328 }
2329
2330 if (rc >= HTTP_ERR_SIZE) {
2331 memprintf(err, "status code '%d' not handled by default with '%s' argument",
2332 status, act_arg);
2333 goto error;
2334 }
2335 if (ctype) {
2336 ha_warning("parsing [%s:%d] : 'http-%s return' : content-type '%s' ignored when the "
2337 "returned response is an erorrfile.\n",
2338 px->conf.args.file, px->conf.args.line,
2339 (rule->from == ACT_F_HTTP_REQ ? "request" : "response"),
2340 ctype);
2341 free(ctype);
2342 ctype = NULL;
2343 }
2344 }
2345 else { /* explicit payload using 'errorfile' parameter */
2346 if (ctype) {
2347 ha_warning("parsing [%s:%d] : 'http-%s return' : content-type '%s' ignored because"
2348 " the errorfile '%s' is used.\n",
2349 px->conf.args.file, px->conf.args.line,
2350 (rule->from == ACT_F_HTTP_REQ ? "request" : "response"),
2351 ctype, file);
2352 free(ctype);
2353 ctype = NULL;
2354 }
2355 }
Christopher Faulet4a2c1422020-01-31 17:36:01 +01002356 if (!LIST_ISEMPTY(hdrs)) {
2357 ha_warning("parsing [%s:%d] : 'http-%s return' : hdr parameters ignored when the "
2358 "returned response is an erorrfile.\n",
2359 px->conf.args.file, px->conf.args.line,
2360 (rule->from == ACT_F_HTTP_REQ ? "request" : "response"));
2361 list_for_each_entry_safe(hdr, hdrb, hdrs, list) {
2362 LIST_DEL(&hdr->list);
2363 list_for_each_entry_safe(lf, lfb, &hdr->value, list) {
2364 LIST_DEL(&lf->list);
2365 release_sample_expr(lf->expr);
2366 free(lf->arg);
2367 free(lf);
2368 }
2369 free(hdr->name.ptr);
2370 free(hdr);
2371 }
2372 }
2373 free(hdrs);
2374 hdrs = NULL;
Christopher Faulet24231ab2020-01-24 17:44:23 +01002375 }
2376 else if (action == 2) { /* explicit parameter using 'file' parameter*/
2377 if (!ctype && objlen) {
2378 memprintf(err, "a content type must be defined when non-empty payload is configured");
2379 goto error;
2380 }
2381 if (ctype && !objlen) {
2382 ha_warning("parsing [%s:%d] : 'http-%s return' : content-type '%s' ignored when the "
2383 "configured payload is empty.\n",
2384 px->conf.args.file, px->conf.args.line,
2385 (rule->from == ACT_F_HTTP_REQ ? "request" : "response"),
2386 ctype);
2387 free(ctype);
2388 ctype = NULL;
2389 }
2390 if (global.tune.bufsize - objlen < global.tune.maxrewrite) {
2391 ha_warning("parsing [%s:%d] : 'http-%s return' : the payload runs ober the buffer space reserved to headers rewritting."
2392 " It may lead to internal errors if strict rewritting mode is enabled.\n",
2393 px->conf.args.file, px->conf.args.line,
2394 (rule->from == ACT_F_HTTP_REQ ? "request" : "response"));
2395 }
2396 chunk_initlen(&rule->arg.http_return.body.obj, obj, global.tune.bufsize, objlen);
2397 }
2398 else if (action == 3) { /* log-format payload using 'lf-file' of 'lf-string' parameter */
2399 LIST_INIT(&rule->arg.http_return.body.fmt);
2400 if (!ctype) {
2401 memprintf(err, "a content type must be defined with a log-format payload");
2402 goto error;
2403 }
2404 if (rule->from == ACT_F_HTTP_REQ) {
2405 px->conf.args.ctx = ARGC_HRQ;
2406 cap = (px->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR;
2407 }
2408 else {
2409 px->conf.args.ctx = ARGC_HRS;
2410 cap = (px->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR;
2411 }
2412 if (!parse_logformat_string(obj, px, &rule->arg.http_return.body.fmt, LOG_OPT_HTTP, cap, err))
2413 goto error;
2414
2415 free(px->conf.lfs_file);
2416 px->conf.lfs_file = strdup(px->conf.args.file);
2417 px->conf.lfs_line = px->conf.args.line;
2418 free(obj);
2419 }
2420
2421 rule->arg.http_return.status = status;
2422 rule->arg.http_return.ctype = ctype;
Christopher Faulet4a2c1422020-01-31 17:36:01 +01002423 rule->arg.http_return.hdrs = hdrs;
Christopher Faulet24231ab2020-01-24 17:44:23 +01002424 rule->action = action;
2425 rule->action_ptr = http_action_return;
2426 rule->release_ptr = release_http_return;
2427
2428 out:
2429 *orig_arg = cur_arg;
2430 return ACT_RET_PRS_OK;
2431
2432 error:
2433 free(obj);
2434 free(ctype);
2435 free(name);
2436 if (fd >= 0)
2437 close(fd);
Christopher Faulet817c4e32020-02-07 10:26:23 +01002438 if (hdrs) {
2439 list_for_each_entry_safe(hdr, hdrb, hdrs, list) {
2440 LIST_DEL(&hdr->list);
2441 list_for_each_entry_safe(lf, lfb, &hdr->value, list) {
2442 LIST_DEL(&lf->list);
2443 release_sample_expr(lf->expr);
2444 free(lf->arg);
2445 free(lf);
2446 }
2447 free(hdr->name.ptr);
2448 free(hdr);
Christopher Faulet4a2c1422020-01-31 17:36:01 +01002449 }
Christopher Faulet817c4e32020-02-07 10:26:23 +01002450 free(hdrs);
Christopher Faulet4a2c1422020-01-31 17:36:01 +01002451 }
Christopher Faulet24231ab2020-01-24 17:44:23 +01002452 if (action == 3) {
2453 list_for_each_entry_safe(lf, lfb, &rule->arg.http_return.body.fmt, list) {
2454 LIST_DEL(&lf->list);
2455 release_sample_expr(lf->expr);
2456 free(lf->arg);
2457 free(lf);
2458 }
2459 }
2460 free(rule->arg.http_return.ctype);
2461 return ACT_RET_PRS_ERR;
2462}
2463
Willy Tarreau79e57332018-10-02 16:01:16 +02002464/************************************************************************/
2465/* All supported http-request action keywords must be declared here. */
2466/************************************************************************/
2467
2468static struct action_kw_list http_req_actions = {
2469 .kw = {
Christopher Faulet81e20172019-12-12 16:40:30 +01002470 { "add-acl", parse_http_set_map, 1 },
2471 { "add-header", parse_http_set_header, 0 },
2472 { "allow", parse_http_allow, 0 },
2473 { "auth", parse_http_auth, 0 },
2474 { "capture", parse_http_req_capture, 0 },
2475 { "del-acl", parse_http_set_map, 1 },
2476 { "del-header", parse_http_del_header, 0 },
2477 { "del-map", parse_http_set_map, 1 },
Christopher Faulete0fca292020-01-13 21:49:03 +01002478 { "deny", parse_http_deny, 0 },
Christopher Faulet81e20172019-12-12 16:40:30 +01002479 { "disable-l7-retry", parse_http_req_disable_l7_retry, 0 },
2480 { "early-hint", parse_http_set_header, 0 },
2481 { "redirect", parse_http_redirect, 0 },
2482 { "reject", parse_http_action_reject, 0 },
2483 { "replace-header", parse_http_replace_header, 0 },
2484 { "replace-path", parse_replace_uri, 0 },
2485 { "replace-uri", parse_replace_uri, 0 },
2486 { "replace-value", parse_http_replace_header, 0 },
Christopher Faulet24231ab2020-01-24 17:44:23 +01002487 { "return", parse_http_return, 0 },
Christopher Faulet81e20172019-12-12 16:40:30 +01002488 { "set-header", parse_http_set_header, 0 },
2489 { "set-log-level", parse_http_set_log_level, 0 },
2490 { "set-map", parse_http_set_map, 1 },
2491 { "set-method", parse_set_req_line, 0 },
2492 { "set-mark", parse_http_set_mark, 0 },
2493 { "set-nice", parse_http_set_nice, 0 },
2494 { "set-path", parse_set_req_line, 0 },
2495 { "set-query", parse_set_req_line, 0 },
2496 { "set-tos", parse_http_set_tos, 0 },
2497 { "set-uri", parse_set_req_line, 0 },
Christopher Faulet46f95542019-12-20 10:07:22 +01002498 { "strict-mode", parse_http_strict_mode, 0 },
Christopher Faulete0fca292020-01-13 21:49:03 +01002499 { "tarpit", parse_http_deny, 0 },
Christopher Faulet81e20172019-12-12 16:40:30 +01002500 { "track-sc", parse_http_track_sc, 1 },
Willy Tarreau79e57332018-10-02 16:01:16 +02002501 { NULL, NULL }
2502 }
2503};
2504
Willy Tarreau0108d902018-11-25 19:14:37 +01002505INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_actions);
2506
Willy Tarreau79e57332018-10-02 16:01:16 +02002507static struct action_kw_list http_res_actions = {
2508 .kw = {
Christopher Faulet81e20172019-12-12 16:40:30 +01002509 { "add-acl", parse_http_set_map, 1 },
2510 { "add-header", parse_http_set_header, 0 },
2511 { "allow", parse_http_allow, 0 },
2512 { "capture", parse_http_res_capture, 0 },
2513 { "del-acl", parse_http_set_map, 1 },
2514 { "del-header", parse_http_del_header, 0 },
2515 { "del-map", parse_http_set_map, 1 },
Christopher Faulete0fca292020-01-13 21:49:03 +01002516 { "deny", parse_http_deny, 0 },
Christopher Faulet81e20172019-12-12 16:40:30 +01002517 { "redirect", parse_http_redirect, 0 },
2518 { "replace-header", parse_http_replace_header, 0 },
2519 { "replace-value", parse_http_replace_header, 0 },
Christopher Faulet24231ab2020-01-24 17:44:23 +01002520 { "return", parse_http_return, 0 },
Christopher Faulet81e20172019-12-12 16:40:30 +01002521 { "set-header", parse_http_set_header, 0 },
2522 { "set-log-level", parse_http_set_log_level, 0 },
2523 { "set-map", parse_http_set_map, 1 },
2524 { "set-mark", parse_http_set_mark, 0 },
2525 { "set-nice", parse_http_set_nice, 0 },
2526 { "set-status", parse_http_set_status, 0 },
2527 { "set-tos", parse_http_set_tos, 0 },
Christopher Faulet46f95542019-12-20 10:07:22 +01002528 { "strict-mode", parse_http_strict_mode, 0 },
Christopher Faulet81e20172019-12-12 16:40:30 +01002529 { "track-sc", parse_http_track_sc, 1 },
Willy Tarreau79e57332018-10-02 16:01:16 +02002530 { NULL, NULL }
2531 }
2532};
2533
Willy Tarreau0108d902018-11-25 19:14:37 +01002534INITCALL1(STG_REGISTER, http_res_keywords_register, &http_res_actions);
Willy Tarreau79e57332018-10-02 16:01:16 +02002535
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002536static struct action_kw_list http_after_res_actions = {
2537 .kw = {
2538 { "add-header", parse_http_set_header, 0 },
2539 { "allow", parse_http_allow, 0 },
2540 { "del-header", parse_http_del_header, 0 },
2541 { "replace-header", parse_http_replace_header, 0 },
2542 { "replace-value", parse_http_replace_header, 0 },
2543 { "set-header", parse_http_set_header, 0 },
2544 { "set-status", parse_http_set_status, 0 },
2545 { "strict-mode", parse_http_strict_mode, 0 },
2546 { NULL, NULL }
2547 }
2548};
2549
2550INITCALL1(STG_REGISTER, http_after_res_keywords_register, &http_after_res_actions);
2551
Willy Tarreau79e57332018-10-02 16:01:16 +02002552/*
2553 * Local variables:
2554 * c-indent-level: 8
2555 * c-basic-offset: 8
2556 * End:
2557 */