blob: e6e3df4f81ea44affa63bc585ccd4261d31ac8d7 [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
Tim Duesterhused526372020-03-05 17:56:33 +010059 istfree(&rule->arg.http.str);
Christopher Faulet2eb53962020-01-14 14:47:34 +010060 if (rule->arg.http.re)
61 regex_free(rule->arg.http.re);
62 list_for_each_entry_safe(lf, lfb, &rule->arg.http.fmt, list) {
63 LIST_DEL(&lf->list);
64 release_sample_expr(lf->expr);
65 free(lf->arg);
66 free(lf);
67 }
68}
69
Willy Tarreau79e57332018-10-02 16:01:16 +020070
71/* This function executes one of the set-{method,path,query,uri} actions. It
72 * builds a string in the trash from the specified format string. It finds
Christopher Faulet2c22a692019-12-18 15:39:56 +010073 * the action to be performed in <.action>, previously filled by function
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +050074 * parse_set_req_line(). The replacement action is executed by the function
Christopher Faulete00d06c2019-12-16 17:18:42 +010075 * http_action_set_req_line(). On success, it returns ACT_RET_CONT. If an error
76 * occurs while soft rewrites are enabled, the action is canceled, but the rule
77 * processing continue. Otherwsize ACT_RET_ERR is returned.
Willy Tarreau79e57332018-10-02 16:01:16 +020078 */
79static enum act_return http_action_set_req_line(struct act_rule *rule, struct proxy *px,
80 struct session *sess, struct stream *s, int flags)
81{
82 struct buffer *replace;
Christopher Faulet13403762019-12-13 09:01:57 +010083 enum act_return ret = ACT_RET_CONT;
Willy Tarreau79e57332018-10-02 16:01:16 +020084
85 replace = alloc_trash_chunk();
86 if (!replace)
Christopher Faulete00d06c2019-12-16 17:18:42 +010087 goto fail_alloc;
Willy Tarreau79e57332018-10-02 16:01:16 +020088
89 /* If we have to create a query string, prepare a '?'. */
Christopher Faulet2c22a692019-12-18 15:39:56 +010090 if (rule->action == 2) // set-query
Willy Tarreau79e57332018-10-02 16:01:16 +020091 replace->area[replace->data++] = '?';
92 replace->data += build_logline(s, replace->area + replace->data,
93 replace->size - replace->data,
Christopher Faulet96bff762019-12-17 13:46:18 +010094 &rule->arg.http.fmt);
Willy Tarreau79e57332018-10-02 16:01:16 +020095
Christopher Faulet2c22a692019-12-18 15:39:56 +010096 if (http_req_replace_stline(rule->action, replace->area, replace->data, px, s) == -1)
Christopher Faulete00d06c2019-12-16 17:18:42 +010097 goto fail_rewrite;
Willy Tarreau79e57332018-10-02 16:01:16 +020098
Christopher Faulete00d06c2019-12-16 17:18:42 +010099 leave:
Willy Tarreau79e57332018-10-02 16:01:16 +0200100 free_trash_chunk(replace);
101 return ret;
Christopher Faulete00d06c2019-12-16 17:18:42 +0100102
103 fail_alloc:
104 if (!(s->flags & SF_ERR_MASK))
105 s->flags |= SF_ERR_RESOURCE;
106 ret = ACT_RET_ERR;
107 goto leave;
108
109 fail_rewrite:
110 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_rewrites, 1);
111 if (s->flags & SF_BE_ASSIGNED)
112 _HA_ATOMIC_ADD(&s->be->be_counters.failed_rewrites, 1);
113 if (sess->listener->counters)
114 _HA_ATOMIC_ADD(&sess->listener->counters->failed_rewrites, 1);
115 if (objt_server(s->target))
116 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_rewrites, 1);
117
Christopher Faulet333bf8c2020-01-22 14:38:05 +0100118 if (!(s->txn->req.flags & HTTP_MSGF_SOFT_RW)) {
Christopher Faulete00d06c2019-12-16 17:18:42 +0100119 ret = ACT_RET_ERR;
Christopher Faulet333bf8c2020-01-22 14:38:05 +0100120 if (!(s->flags & SF_ERR_MASK))
121 s->flags |= SF_ERR_PRXCOND;
122 }
Christopher Faulete00d06c2019-12-16 17:18:42 +0100123 goto leave;
Willy Tarreau79e57332018-10-02 16:01:16 +0200124}
125
126/* parse an http-request action among :
127 * set-method
128 * set-path
129 * set-query
130 * set-uri
131 *
132 * All of them accept a single argument of type string representing a log-format.
Christopher Faulet96bff762019-12-17 13:46:18 +0100133 * The resulting rule makes use of <http.fmt> to store the log-format list head,
Christopher Faulet2c22a692019-12-18 15:39:56 +0100134 * and <.action> to store the action type as an int (0=method, 1=path, 2=query,
135 * 3=uri). It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Willy Tarreau79e57332018-10-02 16:01:16 +0200136 */
137static enum act_parse_ret parse_set_req_line(const char **args, int *orig_arg, struct proxy *px,
138 struct act_rule *rule, char **err)
139{
140 int cur_arg = *orig_arg;
141
Willy Tarreau79e57332018-10-02 16:01:16 +0200142 switch (args[0][4]) {
143 case 'm' :
Christopher Faulet2c22a692019-12-18 15:39:56 +0100144 rule->action = 0; // set-method
Willy Tarreau79e57332018-10-02 16:01:16 +0200145 break;
146 case 'p' :
Christopher Faulet2c22a692019-12-18 15:39:56 +0100147 rule->action = 1; // set-path
Willy Tarreau79e57332018-10-02 16:01:16 +0200148 break;
149 case 'q' :
Christopher Faulet2c22a692019-12-18 15:39:56 +0100150 rule->action = 2; // set-query
Willy Tarreau79e57332018-10-02 16:01:16 +0200151 break;
152 case 'u' :
Christopher Faulet2c22a692019-12-18 15:39:56 +0100153 rule->action = 3; // set-uri
Willy Tarreau79e57332018-10-02 16:01:16 +0200154 break;
155 default:
156 memprintf(err, "internal error: unhandled action '%s'", args[0]);
157 return ACT_RET_PRS_ERR;
158 }
Christopher Faulet96bff762019-12-17 13:46:18 +0100159 rule->action_ptr = http_action_set_req_line;
Christopher Faulet2eb53962020-01-14 14:47:34 +0100160 rule->release_ptr = release_http_action;
Willy Tarreau79e57332018-10-02 16:01:16 +0200161
162 if (!*args[cur_arg] ||
163 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
164 memprintf(err, "expects exactly 1 argument <format>");
165 return ACT_RET_PRS_ERR;
166 }
167
Christopher Faulet96bff762019-12-17 13:46:18 +0100168 LIST_INIT(&rule->arg.http.fmt);
Willy Tarreau79e57332018-10-02 16:01:16 +0200169 px->conf.args.ctx = ARGC_HRQ;
Christopher Faulet96bff762019-12-17 13:46:18 +0100170 if (!parse_logformat_string(args[cur_arg], px, &rule->arg.http.fmt, LOG_OPT_HTTP,
Willy Tarreau79e57332018-10-02 16:01:16 +0200171 (px->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, err)) {
172 return ACT_RET_PRS_ERR;
173 }
174
175 (*orig_arg)++;
176 return ACT_RET_PRS_OK;
177}
178
Willy Tarreau33810222019-06-12 17:44:02 +0200179/* This function executes a replace-uri action. It finds its arguments in
Christopher Faulet96bff762019-12-17 13:46:18 +0100180 * <rule>.arg.http. It builds a string in the trash from the format string
Willy Tarreau33810222019-06-12 17:44:02 +0200181 * previously filled by function parse_replace_uri() and will execute the regex
Christopher Faulet96bff762019-12-17 13:46:18 +0100182 * in <http.re> to replace the URI. It uses the format string present in
Christopher Faulet2c22a692019-12-18 15:39:56 +0100183 * <http.fmt>. The component to act on (path/uri) is taken from <.action> which
Christopher Faulet96bff762019-12-17 13:46:18 +0100184 * contains 1 for the path or 3 for the URI (values used by
185 * http_req_replace_stline()). On success, it returns ACT_RET_CONT. If an error
186 * occurs while soft rewrites are enabled, the action is canceled, but the rule
187 * processing continue. Otherwsize ACT_RET_ERR is returned.
Willy Tarreau33810222019-06-12 17:44:02 +0200188 */
189static enum act_return http_action_replace_uri(struct act_rule *rule, struct proxy *px,
190 struct session *sess, struct stream *s, int flags)
191{
Christopher Faulet13403762019-12-13 09:01:57 +0100192 enum act_return ret = ACT_RET_CONT;
Willy Tarreau33810222019-06-12 17:44:02 +0200193 struct buffer *replace, *output;
194 struct ist uri;
195 int len;
196
197 replace = alloc_trash_chunk();
198 output = alloc_trash_chunk();
199 if (!replace || !output)
Christopher Faulete00d06c2019-12-16 17:18:42 +0100200 goto fail_alloc;
Christopher Faulet12c28b62019-07-15 16:30:24 +0200201 uri = htx_sl_req_uri(http_get_stline(htxbuf(&s->req.buf)));
Willy Tarreau262c3f12019-12-17 06:52:51 +0100202
Christopher Faulet2c22a692019-12-18 15:39:56 +0100203 if (rule->action == 1) // replace-path
Jerome Magnin4bbc9492020-02-21 10:37:48 +0100204 uri = iststop(http_get_path(uri), '?');
Willy Tarreau262c3f12019-12-17 06:52:51 +0100205
Christopher Faulet96bff762019-12-17 13:46:18 +0100206 if (!regex_exec_match2(rule->arg.http.re, uri.ptr, uri.len, MAX_MATCH, pmatch, 0))
Willy Tarreau33810222019-06-12 17:44:02 +0200207 goto leave;
208
Christopher Faulet96bff762019-12-17 13:46:18 +0100209 replace->data = build_logline(s, replace->area, replace->size, &rule->arg.http.fmt);
Willy Tarreau33810222019-06-12 17:44:02 +0200210
211 /* note: uri.ptr doesn't need to be zero-terminated because it will
212 * only be used to pick pmatch references.
213 */
214 len = exp_replace(output->area, output->size, uri.ptr, replace->area, pmatch);
215 if (len == -1)
Christopher Faulete00d06c2019-12-16 17:18:42 +0100216 goto fail_rewrite;
Willy Tarreau33810222019-06-12 17:44:02 +0200217
Christopher Faulet2c22a692019-12-18 15:39:56 +0100218 if (http_req_replace_stline(rule->action, output->area, len, px, s) == -1)
Christopher Faulete00d06c2019-12-16 17:18:42 +0100219 goto fail_rewrite;
Willy Tarreau33810222019-06-12 17:44:02 +0200220
Christopher Faulete00d06c2019-12-16 17:18:42 +0100221 leave:
Willy Tarreau33810222019-06-12 17:44:02 +0200222 free_trash_chunk(output);
223 free_trash_chunk(replace);
224 return ret;
Christopher Faulete00d06c2019-12-16 17:18:42 +0100225
226 fail_alloc:
227 if (!(s->flags & SF_ERR_MASK))
228 s->flags |= SF_ERR_RESOURCE;
229 ret = ACT_RET_ERR;
230 goto leave;
231
232 fail_rewrite:
233 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_rewrites, 1);
234 if (s->flags & SF_BE_ASSIGNED)
235 _HA_ATOMIC_ADD(&s->be->be_counters.failed_rewrites, 1);
236 if (sess->listener->counters)
237 _HA_ATOMIC_ADD(&sess->listener->counters->failed_rewrites, 1);
238 if (objt_server(s->target))
239 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_rewrites, 1);
240
Christopher Faulet333bf8c2020-01-22 14:38:05 +0100241 if (!(s->txn->req.flags & HTTP_MSGF_SOFT_RW)) {
Christopher Faulete00d06c2019-12-16 17:18:42 +0100242 ret = ACT_RET_ERR;
Christopher Faulet333bf8c2020-01-22 14:38:05 +0100243 if (!(s->flags & SF_ERR_MASK))
244 s->flags |= SF_ERR_PRXCOND;
245 }
Christopher Faulete00d06c2019-12-16 17:18:42 +0100246 goto leave;
Willy Tarreau33810222019-06-12 17:44:02 +0200247}
248
Willy Tarreau262c3f12019-12-17 06:52:51 +0100249/* parse a "replace-uri" or "replace-path" http-request action.
Willy Tarreau33810222019-06-12 17:44:02 +0200250 * This action takes 2 arguments (a regex and a replacement format string).
Christopher Faulet2c22a692019-12-18 15:39:56 +0100251 * The resulting rule makes use of <.action> to store the action (1/3 for now),
Christopher Faulet96bff762019-12-17 13:46:18 +0100252 * <http.re> to store the compiled regex, and <http.fmt> to store the log-format
Willy Tarreau33810222019-06-12 17:44:02 +0200253 * list head. It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
254 */
255static enum act_parse_ret parse_replace_uri(const char **args, int *orig_arg, struct proxy *px,
256 struct act_rule *rule, char **err)
257{
258 int cur_arg = *orig_arg;
259 char *error = NULL;
260
Willy Tarreau262c3f12019-12-17 06:52:51 +0100261 if (strcmp(args[cur_arg-1], "replace-path") == 0)
Christopher Faulet2c22a692019-12-18 15:39:56 +0100262 rule->action = 1; // replace-path, same as set-path
Willy Tarreau262c3f12019-12-17 06:52:51 +0100263 else
Christopher Faulet2c22a692019-12-18 15:39:56 +0100264 rule->action = 3; // replace-uri, same as set-uri
Willy Tarreau262c3f12019-12-17 06:52:51 +0100265
Willy Tarreau33810222019-06-12 17:44:02 +0200266 rule->action_ptr = http_action_replace_uri;
Christopher Faulet2eb53962020-01-14 14:47:34 +0100267 rule->release_ptr = release_http_action;
Willy Tarreau33810222019-06-12 17:44:02 +0200268
269 if (!*args[cur_arg] || !*args[cur_arg+1] ||
270 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
271 memprintf(err, "expects exactly 2 arguments <match-regex> and <replace-format>");
272 return ACT_RET_PRS_ERR;
273 }
274
Christopher Faulet96bff762019-12-17 13:46:18 +0100275 if (!(rule->arg.http.re = regex_comp(args[cur_arg], 1, 1, &error))) {
Willy Tarreau33810222019-06-12 17:44:02 +0200276 memprintf(err, "failed to parse the regex : %s", error);
277 free(error);
278 return ACT_RET_PRS_ERR;
279 }
280
Christopher Faulet96bff762019-12-17 13:46:18 +0100281 LIST_INIT(&rule->arg.http.fmt);
Willy Tarreau33810222019-06-12 17:44:02 +0200282 px->conf.args.ctx = ARGC_HRQ;
Christopher Faulet96bff762019-12-17 13:46:18 +0100283 if (!parse_logformat_string(args[cur_arg + 1], px, &rule->arg.http.fmt, LOG_OPT_HTTP,
Willy Tarreau33810222019-06-12 17:44:02 +0200284 (px->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, err)) {
Christopher Faulet1337b322020-01-14 14:50:55 +0100285 regex_free(rule->arg.http.re);
Willy Tarreau33810222019-06-12 17:44:02 +0200286 return ACT_RET_PRS_ERR;
287 }
288
289 (*orig_arg) += 2;
290 return ACT_RET_PRS_OK;
291}
292
Willy Tarreau79e57332018-10-02 16:01:16 +0200293/* This function is just a compliant action wrapper for "set-status". */
294static enum act_return action_http_set_status(struct act_rule *rule, struct proxy *px,
295 struct session *sess, struct stream *s, int flags)
296{
Christopher Faulet96bff762019-12-17 13:46:18 +0100297 if (http_res_set_status(rule->arg.http.i, rule->arg.http.str, s) == -1) {
Christopher Faulete00d06c2019-12-16 17:18:42 +0100298 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_rewrites, 1);
299 if (s->flags & SF_BE_ASSIGNED)
300 _HA_ATOMIC_ADD(&s->be->be_counters.failed_rewrites, 1);
301 if (sess->listener->counters)
302 _HA_ATOMIC_ADD(&sess->listener->counters->failed_rewrites, 1);
303 if (objt_server(s->target))
304 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_rewrites, 1);
305
Christopher Faulet333bf8c2020-01-22 14:38:05 +0100306 if (!(s->txn->req.flags & HTTP_MSGF_SOFT_RW)) {
Christopher Faulet333bf8c2020-01-22 14:38:05 +0100307 if (!(s->flags & SF_ERR_MASK))
308 s->flags |= SF_ERR_PRXCOND;
Christopher Faulet692a6c22020-02-07 10:22:31 +0100309 return ACT_RET_ERR;
Christopher Faulet333bf8c2020-01-22 14:38:05 +0100310 }
Christopher Faulete00d06c2019-12-16 17:18:42 +0100311 }
312
Willy Tarreau79e57332018-10-02 16:01:16 +0200313 return ACT_RET_CONT;
314}
315
316/* parse set-status action:
317 * This action accepts a single argument of type int representing
318 * an http status code. It returns ACT_RET_PRS_OK on success,
319 * ACT_RET_PRS_ERR on error.
320 */
321static enum act_parse_ret parse_http_set_status(const char **args, int *orig_arg, struct proxy *px,
322 struct act_rule *rule, char **err)
323{
324 char *error;
325
326 rule->action = ACT_CUSTOM;
327 rule->action_ptr = action_http_set_status;
Christopher Faulet2eb53962020-01-14 14:47:34 +0100328 rule->release_ptr = release_http_action;
Willy Tarreau79e57332018-10-02 16:01:16 +0200329
330 /* Check if an argument is available */
331 if (!*args[*orig_arg]) {
332 memprintf(err, "expects 1 argument: <status>; or 3 arguments: <status> reason <fmt>");
333 return ACT_RET_PRS_ERR;
334 }
335
336 /* convert status code as integer */
Christopher Faulet96bff762019-12-17 13:46:18 +0100337 rule->arg.http.i = strtol(args[*orig_arg], &error, 10);
338 if (*error != '\0' || rule->arg.http.i < 100 || rule->arg.http.i > 999) {
Willy Tarreau79e57332018-10-02 16:01:16 +0200339 memprintf(err, "expects an integer status code between 100 and 999");
340 return ACT_RET_PRS_ERR;
341 }
342
343 (*orig_arg)++;
344
345 /* set custom reason string */
Christopher Faulet96bff762019-12-17 13:46:18 +0100346 rule->arg.http.str = ist(NULL); // If null, we use the default reason for the status code.
Willy Tarreau79e57332018-10-02 16:01:16 +0200347 if (*args[*orig_arg] && strcmp(args[*orig_arg], "reason") == 0 &&
348 (*args[*orig_arg + 1] && strcmp(args[*orig_arg + 1], "if") != 0 && strcmp(args[*orig_arg + 1], "unless") != 0)) {
349 (*orig_arg)++;
Christopher Faulet96bff762019-12-17 13:46:18 +0100350 rule->arg.http.str.ptr = strdup(args[*orig_arg]);
351 rule->arg.http.str.len = strlen(rule->arg.http.str.ptr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200352 (*orig_arg)++;
353 }
354
Christopher Fauletc20b3712020-01-27 15:51:56 +0100355 LIST_INIT(&rule->arg.http.fmt);
Willy Tarreau79e57332018-10-02 16:01:16 +0200356 return ACT_RET_PRS_OK;
357}
358
359/* This function executes the "reject" HTTP action. It clears the request and
360 * response buffer without sending any response. It can be useful as an HTTP
361 * alternative to the silent-drop action to defend against DoS attacks, and may
362 * also be used with HTTP/2 to close a connection instead of just a stream.
363 * The txn status is unchanged, indicating no response was sent. The termination
Christopher Faulet90d22a82020-03-06 11:18:39 +0100364 * flags will indicate "PR". It always returns ACT_RET_ABRT.
Willy Tarreau79e57332018-10-02 16:01:16 +0200365 */
366static enum act_return http_action_reject(struct act_rule *rule, struct proxy *px,
367 struct session *sess, struct stream *s, int flags)
368{
Willy Tarreau0f9cd7b2019-01-31 19:02:43 +0100369 si_must_kill_conn(chn_prod(&s->req));
Willy Tarreau79e57332018-10-02 16:01:16 +0200370 channel_abort(&s->req);
371 channel_abort(&s->res);
Christopher Fauletd4a824e2020-03-06 15:07:09 +0100372 s->req.analysers &= AN_REQ_FLT_END;
373 s->res.analysers &= AN_RES_FLT_END;
Willy Tarreau79e57332018-10-02 16:01:16 +0200374
Olivier Houcharda798bf52019-03-08 18:52:00 +0100375 _HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
376 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Willy Tarreau79e57332018-10-02 16:01:16 +0200377 if (sess->listener && sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100378 _HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Willy Tarreau79e57332018-10-02 16:01:16 +0200379
380 if (!(s->flags & SF_ERR_MASK))
381 s->flags |= SF_ERR_PRXCOND;
382 if (!(s->flags & SF_FINST_MASK))
383 s->flags |= SF_FINST_R;
384
Christopher Faulet90d22a82020-03-06 11:18:39 +0100385 return ACT_RET_ABRT;
Willy Tarreau79e57332018-10-02 16:01:16 +0200386}
387
388/* parse the "reject" action:
389 * This action takes no argument and returns ACT_RET_PRS_OK on success,
390 * ACT_RET_PRS_ERR on error.
391 */
392static enum act_parse_ret parse_http_action_reject(const char **args, int *orig_arg, struct proxy *px,
393 struct act_rule *rule, char **err)
394{
395 rule->action = ACT_CUSTOM;
396 rule->action_ptr = http_action_reject;
397 return ACT_RET_PRS_OK;
398}
399
Olivier Houchard602bf7d2019-05-10 13:59:15 +0200400/* This function executes the "disable-l7-retry" HTTP action.
401 * It disables L7 retries (all retry except for a connection failure). This
402 * can be useful for example to avoid retrying on POST requests.
403 * It just removes the L7 retry flag on the stream_interface, and always
404 * return ACT_RET_CONT;
405 */
406static enum act_return http_req_disable_l7_retry(struct act_rule *rule, struct proxy *px,
407 struct session *sess, struct stream *s, int flags)
408{
409 struct stream_interface *si = &s->si[1];
410
411 /* In theory, the SI_FL_L7_RETRY flags isn't set at this point, but
412 * let's be future-proof and remove it anyway.
413 */
414 si->flags &= ~SI_FL_L7_RETRY;
415 si->flags |= SI_FL_D_L7_RETRY;
416 return ACT_RET_CONT;
417}
418
419/* parse the "disable-l7-retry" action:
420 * This action takes no argument and returns ACT_RET_PRS_OK on success,
421 * ACT_RET_PRS_ERR on error.
422 */
423static enum act_parse_ret parse_http_req_disable_l7_retry(const char **args,
424 int *orig_args, struct proxy *px,
425 struct act_rule *rule, char **err)
426{
427 rule->action = ACT_CUSTOM;
428 rule->action_ptr = http_req_disable_l7_retry;
429 return ACT_RET_PRS_OK;
430}
431
Willy Tarreau79e57332018-10-02 16:01:16 +0200432/* This function executes the "capture" action. It executes a fetch expression,
433 * turns the result into a string and puts it in a capture slot. It always
434 * returns 1. If an error occurs the action is cancelled, but the rule
435 * processing continues.
436 */
437static enum act_return http_action_req_capture(struct act_rule *rule, struct proxy *px,
438 struct session *sess, struct stream *s, int flags)
439{
440 struct sample *key;
441 struct cap_hdr *h = rule->arg.cap.hdr;
442 char **cap = s->req_cap;
443 int len;
444
445 key = sample_fetch_as_type(s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.cap.expr, SMP_T_STR);
446 if (!key)
447 return ACT_RET_CONT;
448
449 if (cap[h->index] == NULL)
450 cap[h->index] = pool_alloc(h->pool);
451
452 if (cap[h->index] == NULL) /* no more capture memory */
453 return ACT_RET_CONT;
454
455 len = key->data.u.str.data;
456 if (len > h->len)
457 len = h->len;
458
459 memcpy(cap[h->index], key->data.u.str.area, len);
460 cap[h->index][len] = 0;
461 return ACT_RET_CONT;
462}
463
464/* This function executes the "capture" action and store the result in a
465 * capture slot if exists. It executes a fetch expression, turns the result
466 * into a string and puts it in a capture slot. It always returns 1. If an
467 * error occurs the action is cancelled, but the rule processing continues.
468 */
469static enum act_return http_action_req_capture_by_id(struct act_rule *rule, struct proxy *px,
470 struct session *sess, struct stream *s, int flags)
471{
472 struct sample *key;
473 struct cap_hdr *h;
474 char **cap = s->req_cap;
475 struct proxy *fe = strm_fe(s);
476 int len;
477 int i;
478
479 /* Look for the original configuration. */
480 for (h = fe->req_cap, i = fe->nb_req_cap - 1;
481 h != NULL && i != rule->arg.capid.idx ;
482 i--, h = h->next);
483 if (!h)
484 return ACT_RET_CONT;
485
486 key = sample_fetch_as_type(s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.capid.expr, SMP_T_STR);
487 if (!key)
488 return ACT_RET_CONT;
489
490 if (cap[h->index] == NULL)
491 cap[h->index] = pool_alloc(h->pool);
492
493 if (cap[h->index] == NULL) /* no more capture memory */
494 return ACT_RET_CONT;
495
496 len = key->data.u.str.data;
497 if (len > h->len)
498 len = h->len;
499
500 memcpy(cap[h->index], key->data.u.str.area, len);
501 cap[h->index][len] = 0;
502 return ACT_RET_CONT;
503}
504
505/* Check an "http-request capture" action.
506 *
507 * The function returns 1 in success case, otherwise, it returns 0 and err is
508 * filled.
509 */
510static int check_http_req_capture(struct act_rule *rule, struct proxy *px, char **err)
511{
512 if (rule->action_ptr != http_action_req_capture_by_id)
513 return 1;
514
Baptiste Assmann19a69b32020-01-16 14:34:22 +0100515 /* capture slots can only be declared in frontends, so we can't check their
516 * existence in backends at configuration parsing step
517 */
518 if (px->cap & PR_CAP_FE && rule->arg.capid.idx >= px->nb_req_cap) {
Willy Tarreau79e57332018-10-02 16:01:16 +0200519 memprintf(err, "unable to find capture id '%d' referenced by http-request capture rule",
520 rule->arg.capid.idx);
521 return 0;
522 }
523
524 return 1;
525}
526
Christopher Faulet2eb53962020-01-14 14:47:34 +0100527/* Release memory allocate by an http capture action */
528static void release_http_capture(struct act_rule *rule)
529{
530 if (rule->action_ptr == http_action_req_capture)
531 release_sample_expr(rule->arg.cap.expr);
532 else
533 release_sample_expr(rule->arg.capid.expr);
534}
535
Willy Tarreau79e57332018-10-02 16:01:16 +0200536/* parse an "http-request capture" action. It takes a single argument which is
537 * a sample fetch expression. It stores the expression into arg->act.p[0] and
538 * the allocated hdr_cap struct or the preallocated "id" into arg->act.p[1].
539 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
540 */
541static enum act_parse_ret parse_http_req_capture(const char **args, int *orig_arg, struct proxy *px,
542 struct act_rule *rule, char **err)
543{
544 struct sample_expr *expr;
545 struct cap_hdr *hdr;
546 int cur_arg;
547 int len = 0;
548
549 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
550 if (strcmp(args[cur_arg], "if") == 0 ||
551 strcmp(args[cur_arg], "unless") == 0)
552 break;
553
554 if (cur_arg < *orig_arg + 3) {
555 memprintf(err, "expects <expression> [ 'len' <length> | id <idx> ]");
556 return ACT_RET_PRS_ERR;
557 }
558
559 cur_arg = *orig_arg;
Willy Tarreaue3b57bf2020-02-14 16:50:14 +0100560 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 +0200561 if (!expr)
562 return ACT_RET_PRS_ERR;
563
564 if (!(expr->fetch->val & SMP_VAL_FE_HRQ_HDR)) {
565 memprintf(err,
566 "fetch method '%s' extracts information from '%s', none of which is available here",
567 args[cur_arg-1], sample_src_names(expr->fetch->use));
Christopher Faulet1337b322020-01-14 14:50:55 +0100568 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200569 return ACT_RET_PRS_ERR;
570 }
571
572 if (!args[cur_arg] || !*args[cur_arg]) {
573 memprintf(err, "expects 'len or 'id'");
Christopher Faulet1337b322020-01-14 14:50:55 +0100574 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200575 return ACT_RET_PRS_ERR;
576 }
577
578 if (strcmp(args[cur_arg], "len") == 0) {
579 cur_arg++;
580
581 if (!(px->cap & PR_CAP_FE)) {
582 memprintf(err, "proxy '%s' has no frontend capability", px->id);
Christopher Faulet1337b322020-01-14 14:50:55 +0100583 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200584 return ACT_RET_PRS_ERR;
585 }
586
587 px->conf.args.ctx = ARGC_CAP;
588
589 if (!args[cur_arg]) {
590 memprintf(err, "missing length value");
Christopher Faulet1337b322020-01-14 14:50:55 +0100591 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200592 return ACT_RET_PRS_ERR;
593 }
594 /* we copy the table name for now, it will be resolved later */
595 len = atoi(args[cur_arg]);
596 if (len <= 0) {
597 memprintf(err, "length must be > 0");
Christopher Faulet1337b322020-01-14 14:50:55 +0100598 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200599 return ACT_RET_PRS_ERR;
600 }
601 cur_arg++;
602
Willy Tarreau79e57332018-10-02 16:01:16 +0200603 hdr = calloc(1, sizeof(*hdr));
604 hdr->next = px->req_cap;
605 hdr->name = NULL; /* not a header capture */
606 hdr->namelen = 0;
607 hdr->len = len;
608 hdr->pool = create_pool("caphdr", hdr->len + 1, MEM_F_SHARED);
609 hdr->index = px->nb_req_cap++;
610
611 px->req_cap = hdr;
612 px->to_log |= LW_REQHDR;
613
614 rule->action = ACT_CUSTOM;
615 rule->action_ptr = http_action_req_capture;
Christopher Faulet2eb53962020-01-14 14:47:34 +0100616 rule->release_ptr = release_http_capture;
Willy Tarreau79e57332018-10-02 16:01:16 +0200617 rule->arg.cap.expr = expr;
618 rule->arg.cap.hdr = hdr;
619 }
620
621 else if (strcmp(args[cur_arg], "id") == 0) {
622 int id;
623 char *error;
624
625 cur_arg++;
626
627 if (!args[cur_arg]) {
628 memprintf(err, "missing id value");
Christopher Faulet1337b322020-01-14 14:50:55 +0100629 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200630 return ACT_RET_PRS_ERR;
631 }
632
633 id = strtol(args[cur_arg], &error, 10);
634 if (*error != '\0') {
635 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
Christopher Faulet1337b322020-01-14 14:50:55 +0100636 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200637 return ACT_RET_PRS_ERR;
638 }
639 cur_arg++;
640
641 px->conf.args.ctx = ARGC_CAP;
642
643 rule->action = ACT_CUSTOM;
644 rule->action_ptr = http_action_req_capture_by_id;
645 rule->check_ptr = check_http_req_capture;
Christopher Faulet2eb53962020-01-14 14:47:34 +0100646 rule->release_ptr = release_http_capture;
Willy Tarreau79e57332018-10-02 16:01:16 +0200647 rule->arg.capid.expr = expr;
648 rule->arg.capid.idx = id;
649 }
650
651 else {
652 memprintf(err, "expects 'len' or 'id', found '%s'", args[cur_arg]);
Christopher Faulet1337b322020-01-14 14:50:55 +0100653 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200654 return ACT_RET_PRS_ERR;
655 }
656
657 *orig_arg = cur_arg;
658 return ACT_RET_PRS_OK;
659}
660
661/* This function executes the "capture" action and store the result in a
662 * capture slot if exists. It executes a fetch expression, turns the result
663 * into a string and puts it in a capture slot. It always returns 1. If an
664 * error occurs the action is cancelled, but the rule processing continues.
665 */
666static enum act_return http_action_res_capture_by_id(struct act_rule *rule, struct proxy *px,
667 struct session *sess, struct stream *s, int flags)
668{
669 struct sample *key;
670 struct cap_hdr *h;
671 char **cap = s->res_cap;
672 struct proxy *fe = strm_fe(s);
673 int len;
674 int i;
675
676 /* Look for the original configuration. */
677 for (h = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
678 h != NULL && i != rule->arg.capid.idx ;
679 i--, h = h->next);
680 if (!h)
681 return ACT_RET_CONT;
682
683 key = sample_fetch_as_type(s->be, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL, rule->arg.capid.expr, SMP_T_STR);
684 if (!key)
685 return ACT_RET_CONT;
686
687 if (cap[h->index] == NULL)
688 cap[h->index] = pool_alloc(h->pool);
689
690 if (cap[h->index] == NULL) /* no more capture memory */
691 return ACT_RET_CONT;
692
693 len = key->data.u.str.data;
694 if (len > h->len)
695 len = h->len;
696
697 memcpy(cap[h->index], key->data.u.str.area, len);
698 cap[h->index][len] = 0;
699 return ACT_RET_CONT;
700}
701
702/* Check an "http-response capture" action.
703 *
704 * The function returns 1 in success case, otherwise, it returns 0 and err is
705 * filled.
706 */
707static int check_http_res_capture(struct act_rule *rule, struct proxy *px, char **err)
708{
709 if (rule->action_ptr != http_action_res_capture_by_id)
710 return 1;
711
712 if (rule->arg.capid.idx >= px->nb_rsp_cap) {
713 memprintf(err, "unable to find capture id '%d' referenced by http-response capture rule",
714 rule->arg.capid.idx);
715 return 0;
716 }
717
718 return 1;
719}
720
721/* parse an "http-response capture" action. It takes a single argument which is
722 * a sample fetch expression. It stores the expression into arg->act.p[0] and
723 * the allocated hdr_cap struct od the preallocated id into arg->act.p[1].
724 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
725 */
726static enum act_parse_ret parse_http_res_capture(const char **args, int *orig_arg, struct proxy *px,
727 struct act_rule *rule, char **err)
728{
729 struct sample_expr *expr;
730 int cur_arg;
731 int id;
732 char *error;
733
734 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
735 if (strcmp(args[cur_arg], "if") == 0 ||
736 strcmp(args[cur_arg], "unless") == 0)
737 break;
738
739 if (cur_arg < *orig_arg + 3) {
740 memprintf(err, "expects <expression> id <idx>");
741 return ACT_RET_PRS_ERR;
742 }
743
744 cur_arg = *orig_arg;
Willy Tarreaue3b57bf2020-02-14 16:50:14 +0100745 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 +0200746 if (!expr)
747 return ACT_RET_PRS_ERR;
748
749 if (!(expr->fetch->val & SMP_VAL_FE_HRS_HDR)) {
750 memprintf(err,
751 "fetch method '%s' extracts information from '%s', none of which is available here",
752 args[cur_arg-1], sample_src_names(expr->fetch->use));
Christopher Faulet1337b322020-01-14 14:50:55 +0100753 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200754 return ACT_RET_PRS_ERR;
755 }
756
757 if (!args[cur_arg] || !*args[cur_arg]) {
758 memprintf(err, "expects 'id'");
Christopher Faulet1337b322020-01-14 14:50:55 +0100759 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200760 return ACT_RET_PRS_ERR;
761 }
762
763 if (strcmp(args[cur_arg], "id") != 0) {
764 memprintf(err, "expects 'id', found '%s'", args[cur_arg]);
Christopher Faulet1337b322020-01-14 14:50:55 +0100765 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200766 return ACT_RET_PRS_ERR;
767 }
768
769 cur_arg++;
770
771 if (!args[cur_arg]) {
772 memprintf(err, "missing id value");
Christopher Faulet1337b322020-01-14 14:50:55 +0100773 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200774 return ACT_RET_PRS_ERR;
775 }
776
777 id = strtol(args[cur_arg], &error, 10);
778 if (*error != '\0') {
779 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
Christopher Faulet1337b322020-01-14 14:50:55 +0100780 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200781 return ACT_RET_PRS_ERR;
782 }
783 cur_arg++;
784
785 px->conf.args.ctx = ARGC_CAP;
786
787 rule->action = ACT_CUSTOM;
788 rule->action_ptr = http_action_res_capture_by_id;
789 rule->check_ptr = check_http_res_capture;
Christopher Faulet2eb53962020-01-14 14:47:34 +0100790 rule->release_ptr = release_http_capture;
Willy Tarreau79e57332018-10-02 16:01:16 +0200791 rule->arg.capid.expr = expr;
792 rule->arg.capid.idx = id;
793
794 *orig_arg = cur_arg;
795 return ACT_RET_PRS_OK;
796}
797
Christopher Faulet81e20172019-12-12 16:40:30 +0100798/* Parse a "allow" action for a request or a response rule. It takes no argument. It
799 * returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
800 */
801static enum act_parse_ret parse_http_allow(const char **args, int *orig_arg, struct proxy *px,
802 struct act_rule *rule, char **err)
803{
804 rule->action = ACT_ACTION_ALLOW;
Christopher Faulet245cf792019-12-18 14:58:12 +0100805 rule->flags |= ACT_FLAG_FINAL;
Christopher Faulet81e20172019-12-12 16:40:30 +0100806 return ACT_RET_PRS_OK;
807}
808
Christopher Faulet554c0eb2020-01-14 12:00:28 +0100809/* Check an "http-request deny" action when an http-errors section is referenced.
810 *
811 * The function returns 1 in success case, otherwise, it returns 0 and err is
812 * filled.
813 */
814static int check_http_deny_action(struct act_rule *rule, struct proxy *px, char **err)
815{
816 struct http_errors *http_errs;
817 int status = (intptr_t)(rule->arg.act.p[0]);
818 int ret = 1;
819
820 list_for_each_entry(http_errs, &http_errors_list, list) {
821 if (strcmp(http_errs->id, (char *)rule->arg.act.p[1]) == 0) {
822 free(rule->arg.act.p[1]);
823 rule->arg.http_deny.status = status;
824 rule->arg.http_deny.errmsg = http_errs->errmsg[http_get_status_idx(status)];
825 if (!rule->arg.http_deny.errmsg)
826 ha_warning("Proxy '%s': status '%d' referenced by http deny rule "
827 "not declared in http-errors section '%s'.\n",
828 px->id, status, http_errs->id);
829 break;
830 }
831 }
832
833 if (&http_errs->list == &http_errors_list) {
834 memprintf(err, "unknown http-errors section '%s' referenced by http deny rule",
835 (char *)rule->arg.act.p[1]);
836 free(rule->arg.act.p[1]);
837 ret = 0;
838 }
839
840 return ret;
841}
842
Christopher Faulete0fca292020-01-13 21:49:03 +0100843/* Parse "deny" or "tarpit" actions for a request rule or "deny" action for a
Christopher Faulet554c0eb2020-01-14 12:00:28 +0100844 * response rule. It may take optional arguments to define the status code, the
845 * error file or the http-errors section to use. It returns ACT_RET_PRS_OK on
846 * success, ACT_RET_PRS_ERR on error.
Christopher Faulet81e20172019-12-12 16:40:30 +0100847 */
Christopher Faulete0fca292020-01-13 21:49:03 +0100848static enum act_parse_ret parse_http_deny(const char **args, int *orig_arg, struct proxy *px,
849 struct act_rule *rule, char **err)
Christopher Faulet81e20172019-12-12 16:40:30 +0100850{
Christopher Faulet554c0eb2020-01-14 12:00:28 +0100851 int default_status, status, hc, cur_arg;
852
Christopher Faulet81e20172019-12-12 16:40:30 +0100853
854 cur_arg = *orig_arg;
Christopher Faulete0fca292020-01-13 21:49:03 +0100855 if (rule->from == ACT_F_HTTP_REQ) {
856 if (!strcmp(args[cur_arg-1], "tarpit")) {
857 rule->action = ACT_HTTP_REQ_TARPIT;
Christopher Faulet554c0eb2020-01-14 12:00:28 +0100858 default_status = status = 500;
Christopher Faulet81e20172019-12-12 16:40:30 +0100859 }
Christopher Faulete0fca292020-01-13 21:49:03 +0100860 else {
861 rule->action = ACT_ACTION_DENY;
Christopher Faulet554c0eb2020-01-14 12:00:28 +0100862 default_status = status = 403;
Christopher Faulet81e20172019-12-12 16:40:30 +0100863 }
Christopher Faulet81e20172019-12-12 16:40:30 +0100864 }
Christopher Faulete0fca292020-01-13 21:49:03 +0100865 else {
Christopher Faulet554c0eb2020-01-14 12:00:28 +0100866 rule->action = ACT_ACTION_DENY;
867 default_status = status = 502;
Christopher Faulete0fca292020-01-13 21:49:03 +0100868 }
Christopher Faulet245cf792019-12-18 14:58:12 +0100869 rule->flags |= ACT_FLAG_FINAL;
Christopher Faulet040c8cd2020-01-13 16:43:45 +0100870
871 if (strcmp(args[cur_arg], "deny_status") == 0) {
872 cur_arg++;
873 if (!*args[cur_arg]) {
Christopher Faulet554c0eb2020-01-14 12:00:28 +0100874 memprintf(err, "'%s' expects <status_code> as argument", args[cur_arg-1]);
Christopher Faulet040c8cd2020-01-13 16:43:45 +0100875 return ACT_RET_PRS_ERR;
876 }
877
Christopher Faulet554c0eb2020-01-14 12:00:28 +0100878 status = atol(args[cur_arg]);
Christopher Faulet040c8cd2020-01-13 16:43:45 +0100879 cur_arg++;
880 for (hc = 0; hc < HTTP_ERR_SIZE; hc++) {
Christopher Faulet554c0eb2020-01-14 12:00:28 +0100881 if (http_err_codes[hc] == status)
Christopher Faulet040c8cd2020-01-13 16:43:45 +0100882 break;
Christopher Faulet040c8cd2020-01-13 16:43:45 +0100883 }
Christopher Faulet554c0eb2020-01-14 12:00:28 +0100884 if (hc >= HTTP_ERR_SIZE) {
885 memprintf(err, "status code '%d' not handled, using default code '%d'",
886 status, default_status);
887 status = default_status;
888 hc = http_get_status_idx(status);
889 }
Christopher Faulet040c8cd2020-01-13 16:43:45 +0100890 }
891
Christopher Faulet554c0eb2020-01-14 12:00:28 +0100892 if (strcmp(args[cur_arg], "errorfile") == 0) {
893 cur_arg++;
894 if (!*args[cur_arg]) {
895 memprintf(err, "'%s' expects <file> as argument", args[cur_arg-1]);
896 return ACT_RET_PRS_ERR;
897 }
898
899 rule->arg.http_deny.errmsg = http_load_errorfile(args[cur_arg], err);
900 if (!rule->arg.http_deny.errmsg)
901 return ACT_RET_PRS_ERR;
902 cur_arg++;
903 }
904 else if (strcmp(args[cur_arg], "errorfiles") == 0) {
905 cur_arg++;
906 if (!*args[cur_arg]) {
907 memprintf(err, "'%s' expects <http_errors_name> as argument", args[cur_arg-1]);
908 return ACT_RET_PRS_ERR;
909 }
910 /* Must be resolved during the config validity check */
911 rule->arg.act.p[0] = (void *)((intptr_t)status);
912 rule->arg.act.p[1] = strdup(args[cur_arg]);
913 rule->check_ptr = check_http_deny_action;
914 cur_arg++;
915 goto out;
916 }
917
918 rule->arg.http_deny.status = status;
919
920 out:
Christopher Faulet040c8cd2020-01-13 16:43:45 +0100921 *orig_arg = cur_arg;
Christopher Faulet81e20172019-12-12 16:40:30 +0100922 return ACT_RET_PRS_OK;
923}
924
925/* Parse a "auth" action. It may take 2 optional arguments to define a "realm"
926 * parameter. It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
927 */
928static enum act_parse_ret parse_http_auth(const char **args, int *orig_arg, struct proxy *px,
929 struct act_rule *rule, char **err)
930{
931 int cur_arg;
932
933 rule->action = ACT_HTTP_REQ_AUTH;
Christopher Faulet245cf792019-12-18 14:58:12 +0100934 rule->flags |= ACT_FLAG_FINAL;
Christopher Faulet2eb53962020-01-14 14:47:34 +0100935 rule->release_ptr = release_http_action;
Christopher Faulet81e20172019-12-12 16:40:30 +0100936
937 cur_arg = *orig_arg;
938 if (!strcmp(args[cur_arg], "realm")) {
939 cur_arg++;
940 if (!*args[cur_arg]) {
941 memprintf(err, "missing realm value.\n");
942 return ACT_RET_PRS_ERR;
943 }
Christopher Faulet96bff762019-12-17 13:46:18 +0100944 rule->arg.http.str.ptr = strdup(args[cur_arg]);
945 rule->arg.http.str.len = strlen(rule->arg.http.str.ptr);
Christopher Faulet81e20172019-12-12 16:40:30 +0100946 cur_arg++;
947 }
948
Christopher Fauletc20b3712020-01-27 15:51:56 +0100949 LIST_INIT(&rule->arg.http.fmt);
Christopher Faulet81e20172019-12-12 16:40:30 +0100950 *orig_arg = cur_arg;
951 return ACT_RET_PRS_OK;
952}
953
954/* Parse a "set-nice" action. It takes the nice value as argument. It returns
955 * ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
956 */
957static enum act_parse_ret parse_http_set_nice(const char **args, int *orig_arg, struct proxy *px,
958 struct act_rule *rule, char **err)
959{
960 int cur_arg;
961
962 rule->action = ACT_HTTP_SET_NICE;
963
964 cur_arg = *orig_arg;
965 if (!*args[cur_arg]) {
966 memprintf(err, "expects exactly 1 argument (integer value)");
967 return ACT_RET_PRS_ERR;
968 }
Christopher Faulet96bff762019-12-17 13:46:18 +0100969 rule->arg.http.i = atoi(args[cur_arg]);
970 if (rule->arg.http.i < -1024)
971 rule->arg.http.i = -1024;
972 else if (rule->arg.http.i > 1024)
973 rule->arg.http.i = 1024;
Christopher Faulet81e20172019-12-12 16:40:30 +0100974
Christopher Fauletc20b3712020-01-27 15:51:56 +0100975 LIST_INIT(&rule->arg.http.fmt);
Christopher Faulet81e20172019-12-12 16:40:30 +0100976 *orig_arg = cur_arg + 1;
977 return ACT_RET_PRS_OK;
978}
979
980/* Parse a "set-tos" action. It takes the TOS value as argument. It returns
981 * ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
982 */
983static enum act_parse_ret parse_http_set_tos(const char **args, int *orig_arg, struct proxy *px,
984 struct act_rule *rule, char **err)
985{
986#ifdef IP_TOS
987 char *endp;
988 int cur_arg;
989
990 rule->action = ACT_HTTP_SET_TOS;
991
992 cur_arg = *orig_arg;
993 if (!*args[cur_arg]) {
994 memprintf(err, "expects exactly 1 argument (integer/hex value)");
995 return ACT_RET_PRS_ERR;
996 }
Christopher Faulet96bff762019-12-17 13:46:18 +0100997 rule->arg.http.i = strtol(args[cur_arg], &endp, 0);
Christopher Faulet81e20172019-12-12 16:40:30 +0100998 if (endp && *endp != '\0') {
999 memprintf(err, "invalid character starting at '%s' (integer/hex value expected)", endp);
1000 return ACT_RET_PRS_ERR;
1001 }
1002
Christopher Fauletc20b3712020-01-27 15:51:56 +01001003 LIST_INIT(&rule->arg.http.fmt);
Christopher Faulet81e20172019-12-12 16:40:30 +01001004 *orig_arg = cur_arg + 1;
1005 return ACT_RET_PRS_OK;
1006#else
1007 memprintf(err, "not supported on this platform (IP_TOS undefined)");
1008 return ACT_RET_PRS_ERR;
1009#endif
1010}
1011
1012/* Parse a "set-mark" action. It takes the MARK value as argument. It returns
1013 * ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
1014 */
1015static enum act_parse_ret parse_http_set_mark(const char **args, int *orig_arg, struct proxy *px,
1016 struct act_rule *rule, char **err)
1017{
1018#ifdef SO_MARK
1019 char *endp;
1020 int cur_arg;
1021
1022 rule->action = ACT_HTTP_SET_MARK;
1023
1024 cur_arg = *orig_arg;
1025 if (!*args[cur_arg]) {
1026 memprintf(err, "expects exactly 1 argument (integer/hex value)");
1027 return ACT_RET_PRS_ERR;
1028 }
Christopher Faulet96bff762019-12-17 13:46:18 +01001029 rule->arg.http.i = strtoul(args[cur_arg], &endp, 0);
Christopher Faulet81e20172019-12-12 16:40:30 +01001030 if (endp && *endp != '\0') {
1031 memprintf(err, "invalid character starting at '%s' (integer/hex value expected)", endp);
1032 return ACT_RET_PRS_ERR;
1033 }
1034
Christopher Fauletc20b3712020-01-27 15:51:56 +01001035 LIST_INIT(&rule->arg.http.fmt);
Christopher Faulet81e20172019-12-12 16:40:30 +01001036 *orig_arg = cur_arg + 1;
1037 global.last_checks |= LSTCHK_NETADM;
1038 return ACT_RET_PRS_OK;
1039#else
1040 memprintf(err, "not supported on this platform (SO_MARK undefined)");
1041 return ACT_RET_PRS_ERR;
1042#endif
1043}
1044
1045/* Parse a "set-log-level" action. It takes the level value as argument. It
1046 * returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
1047 */
1048static enum act_parse_ret parse_http_set_log_level(const char **args, int *orig_arg, struct proxy *px,
1049 struct act_rule *rule, char **err)
1050{
1051 int cur_arg;
1052
1053 rule->action = ACT_HTTP_SET_LOGL;
1054
1055 cur_arg = *orig_arg;
1056 if (!*args[cur_arg]) {
1057 bad_log_level:
1058 memprintf(err, "expects exactly 1 argument (log level name or 'silent')");
1059 return ACT_RET_PRS_ERR;
1060 }
1061 if (strcmp(args[cur_arg], "silent") == 0)
Christopher Faulet96bff762019-12-17 13:46:18 +01001062 rule->arg.http.i = -1;
1063 else if ((rule->arg.http.i = get_log_level(args[cur_arg]) + 1) == 0)
Christopher Faulet81e20172019-12-12 16:40:30 +01001064 goto bad_log_level;
1065
Christopher Fauletc20b3712020-01-27 15:51:56 +01001066 LIST_INIT(&rule->arg.http.fmt);
Christopher Faulet81e20172019-12-12 16:40:30 +01001067 *orig_arg = cur_arg + 1;
1068 return ACT_RET_PRS_OK;
1069}
1070
Christopher Faulet91b3ec12020-01-17 22:30:06 +01001071/* This function executes a early-hint action. It adds an HTTP Early Hint HTTP
1072 * 103 response header with <.arg.http.str> name and with a value built
1073 * according to <.arg.http.fmt> log line format. If it is the first early-hint
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001074 * rule of series, the 103 response start-line is added first. At the end, if
Christopher Faulet91b3ec12020-01-17 22:30:06 +01001075 * the next rule is not an early-hint rule or if it is the last rule, the EOH
1076 * block is added to terminate the response. On success, it returns
1077 * ACT_RET_CONT. If an error occurs while soft rewrites are enabled, the action
1078 * is canceled, but the rule processing continue. Otherwsize ACT_RET_ERR is
1079 * returned.
1080 */
1081static enum act_return http_action_early_hint(struct act_rule *rule, struct proxy *px,
1082 struct session *sess, struct stream *s, int flags)
1083{
1084 struct act_rule *prev_rule, *next_rule;
1085 struct channel *res = &s->res;
1086 struct htx *htx = htx_from_buf(&res->buf);
1087 struct buffer *value = alloc_trash_chunk();
1088 enum act_return ret = ACT_RET_CONT;
1089
1090 if (!(s->txn->req.flags & HTTP_MSGF_VER_11))
1091 goto leave;
1092
1093 if (!value) {
1094 if (!(s->flags & SF_ERR_MASK))
1095 s->flags |= SF_ERR_RESOURCE;
1096 goto error;
1097 }
1098
1099 /* get previous and next rules */
1100 prev_rule = LIST_PREV(&rule->list, typeof(rule), list);
1101 next_rule = LIST_NEXT(&rule->list, typeof(rule), list);
1102
1103 /* if no previous rule or previous rule is not early-hint, start a new response. Otherwise,
1104 * continue to add link to a previously started response */
1105 if (&prev_rule->list == s->current_rule_list || prev_rule->action_ptr != http_action_early_hint) {
1106 struct htx_sl *sl;
1107 unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|
1108 HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
1109
1110 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
1111 ist("HTTP/1.1"), ist("103"), ist("Early Hints"));
1112 if (!sl)
1113 goto error;
1114 sl->info.res.status = 103;
1115 }
1116
1117 /* Add the HTTP Early Hint HTTP 103 response heade */
1118 value->data = build_logline(s, b_tail(value), b_room(value), &rule->arg.http.fmt);
1119 if (!htx_add_header(htx, rule->arg.http.str, ist2(b_head(value), b_data(value))))
1120 goto error;
1121
1122 /* if it is the last rule or the next one is not an early-hint, terminate the current
1123 * response. */
1124 if (&next_rule->list == s->current_rule_list || next_rule->action_ptr != http_action_early_hint) {
Christopher Faulet91b3ec12020-01-17 22:30:06 +01001125 if (!htx_add_endof(htx, HTX_BLK_EOH)) {
1126 /* If an error occurred during an Early-hint rule,
1127 * remove the incomplete HTTP 103 response from the
1128 * buffer */
1129 goto error;
1130 }
1131
Christopher Fauleta72a7e42020-01-28 09:28:11 +01001132 if (!http_forward_proxy_resp(s, 0))
1133 goto error;
Christopher Faulet91b3ec12020-01-17 22:30:06 +01001134 }
1135
1136 leave:
1137 free_trash_chunk(value);
1138 return ret;
1139
1140 error:
1141 /* If an error occurred during an Early-hint rule, remove the incomplete
1142 * HTTP 103 response from the buffer */
1143 channel_htx_truncate(res, htx);
1144 ret = ACT_RET_ERR;
1145 goto leave;
1146}
1147
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001148/* This function executes a set-header or add-header actions. It builds a string
1149 * in the trash from the specified format string. It finds the action to be
1150 * performed in <.action>, previously filled by function parse_set_header(). The
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001151 * replacement action is executed by the function http_action_set_header(). On
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001152 * success, it returns ACT_RET_CONT. If an error occurs while soft rewrites are
1153 * enabled, the action is canceled, but the rule processing continue. Otherwsize
1154 * ACT_RET_ERR is returned.
1155 */
1156static enum act_return http_action_set_header(struct act_rule *rule, struct proxy *px,
1157 struct session *sess, struct stream *s, int flags)
1158{
Christopher Faulet91e31d82020-01-24 15:37:13 +01001159 struct http_msg *msg = ((rule->from == ACT_F_HTTP_REQ) ? &s->txn->req : &s->txn->rsp);
1160 struct htx *htx = htxbuf(&msg->chn->buf);
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001161 enum act_return ret = ACT_RET_CONT;
1162 struct buffer *replace;
1163 struct http_hdr_ctx ctx;
1164 struct ist n, v;
1165
1166 replace = alloc_trash_chunk();
1167 if (!replace)
1168 goto fail_alloc;
1169
1170 replace->data = build_logline(s, replace->area, replace->size, &rule->arg.http.fmt);
1171 n = rule->arg.http.str;
1172 v = ist2(replace->area, replace->data);
1173
1174 if (rule->action == 0) { // set-header
1175 /* remove all occurrences of the header */
1176 ctx.blk = NULL;
1177 while (http_find_header(htx, n, &ctx, 1))
1178 http_remove_header(htx, &ctx);
1179 }
1180
1181 /* Now add header */
1182 if (!http_add_header(htx, n, v))
1183 goto fail_rewrite;
1184
1185 leave:
1186 free_trash_chunk(replace);
1187 return ret;
1188
1189 fail_alloc:
1190 if (!(s->flags & SF_ERR_MASK))
1191 s->flags |= SF_ERR_RESOURCE;
1192 ret = ACT_RET_ERR;
1193 goto leave;
1194
1195 fail_rewrite:
1196 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_rewrites, 1);
1197 if (s->flags & SF_BE_ASSIGNED)
1198 _HA_ATOMIC_ADD(&s->be->be_counters.failed_rewrites, 1);
1199 if (sess->listener->counters)
1200 _HA_ATOMIC_ADD(&sess->listener->counters->failed_rewrites, 1);
1201 if (objt_server(s->target))
1202 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_rewrites, 1);
1203
Christopher Faulet333bf8c2020-01-22 14:38:05 +01001204 if (!(msg->flags & HTTP_MSGF_SOFT_RW)) {
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001205 ret = ACT_RET_ERR;
Christopher Faulet333bf8c2020-01-22 14:38:05 +01001206 if (!(s->flags & SF_ERR_MASK))
1207 s->flags |= SF_ERR_PRXCOND;
1208 }
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001209 goto leave;
1210}
1211
Christopher Faulet81e20172019-12-12 16:40:30 +01001212/* Parse a "set-header", "add-header" or "early-hint" actions. It takes an
1213 * header name and a log-format string as arguments. It returns ACT_RET_PRS_OK
1214 * on success, ACT_RET_PRS_ERR on error.
1215 *
1216 * Note: same function is used for the request and the response. However
1217 * "early-hint" rules are only supported for request rules.
1218 */
1219static enum act_parse_ret parse_http_set_header(const char **args, int *orig_arg, struct proxy *px,
1220 struct act_rule *rule, char **err)
1221{
Christopher Faulet81e20172019-12-12 16:40:30 +01001222 int cap, cur_arg;
1223
Christopher Faulet91b3ec12020-01-17 22:30:06 +01001224 if (args[*orig_arg-1][0] == 'e') {
1225 rule->action = ACT_CUSTOM;
1226 rule->action_ptr = http_action_early_hint;
1227 }
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001228 else {
1229 if (args[*orig_arg-1][0] == 's')
1230 rule->action = 0; // set-header
1231 else
1232 rule->action = 1; // add-header
1233 rule->action_ptr = http_action_set_header;
1234 }
Christopher Faulet2eb53962020-01-14 14:47:34 +01001235 rule->release_ptr = release_http_action;
Christopher Faulet81e20172019-12-12 16:40:30 +01001236
1237 cur_arg = *orig_arg;
1238 if (!*args[cur_arg] || !*args[cur_arg+1]) {
1239 memprintf(err, "expects exactly 2 arguments");
1240 return ACT_RET_PRS_ERR;
1241 }
1242
Christopher Faulet81e20172019-12-12 16:40:30 +01001243
Christopher Faulet96bff762019-12-17 13:46:18 +01001244 rule->arg.http.str.ptr = strdup(args[cur_arg]);
1245 rule->arg.http.str.len = strlen(rule->arg.http.str.ptr);
1246 LIST_INIT(&rule->arg.http.fmt);
Christopher Faulet81e20172019-12-12 16:40:30 +01001247
1248 if (rule->from == ACT_F_HTTP_REQ) {
1249 px->conf.args.ctx = ARGC_HRQ;
1250 cap = (px->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR;
1251 }
1252 else{
1253 px->conf.args.ctx = ARGC_HRS;
1254 cap = (px->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR;
1255 }
1256
1257 cur_arg++;
Christopher Faulet1337b322020-01-14 14:50:55 +01001258 if (!parse_logformat_string(args[cur_arg], px, &rule->arg.http.fmt, LOG_OPT_HTTP, cap, err)) {
Tim Duesterhused526372020-03-05 17:56:33 +01001259 istfree(&rule->arg.http.str);
Christopher Faulet81e20172019-12-12 16:40:30 +01001260 return ACT_RET_PRS_ERR;
Christopher Faulet1337b322020-01-14 14:50:55 +01001261 }
Christopher Faulet81e20172019-12-12 16:40:30 +01001262
1263 free(px->conf.lfs_file);
1264 px->conf.lfs_file = strdup(px->conf.args.file);
1265 px->conf.lfs_line = px->conf.args.line;
1266
1267 *orig_arg = cur_arg + 1;
1268 return ACT_RET_PRS_OK;
1269}
1270
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001271/* This function executes a replace-header or replace-value actions. It
1272 * builds a string in the trash from the specified format string. It finds
1273 * the action to be performed in <.action>, previously filled by function
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001274 * parse_replace_header(). The replacement action is executed by the function
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001275 * http_action_replace_header(). On success, it returns ACT_RET_CONT. If an error
1276 * occurs while soft rewrites are enabled, the action is canceled, but the rule
1277 * processing continue. Otherwsize ACT_RET_ERR is returned.
1278 */
1279static enum act_return http_action_replace_header(struct act_rule *rule, struct proxy *px,
1280 struct session *sess, struct stream *s, int flags)
1281{
Christopher Faulet91e31d82020-01-24 15:37:13 +01001282 struct http_msg *msg = ((rule->from == ACT_F_HTTP_REQ) ? &s->txn->req : &s->txn->rsp);
1283 struct htx *htx = htxbuf(&msg->chn->buf);
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001284 enum act_return ret = ACT_RET_CONT;
1285 struct buffer *replace;
1286 int r;
1287
1288 replace = alloc_trash_chunk();
1289 if (!replace)
1290 goto fail_alloc;
1291
1292 replace->data = build_logline(s, replace->area, replace->size, &rule->arg.http.fmt);
1293
1294 r = http_replace_hdrs(s, htx, rule->arg.http.str, replace->area, rule->arg.http.re, (rule->action == 0));
1295 if (r == -1)
1296 goto fail_rewrite;
1297
1298 leave:
1299 free_trash_chunk(replace);
1300 return ret;
1301
1302 fail_alloc:
1303 if (!(s->flags & SF_ERR_MASK))
1304 s->flags |= SF_ERR_RESOURCE;
1305 ret = ACT_RET_ERR;
1306 goto leave;
1307
1308 fail_rewrite:
1309 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_rewrites, 1);
1310 if (s->flags & SF_BE_ASSIGNED)
1311 _HA_ATOMIC_ADD(&s->be->be_counters.failed_rewrites, 1);
1312 if (sess->listener->counters)
1313 _HA_ATOMIC_ADD(&sess->listener->counters->failed_rewrites, 1);
1314 if (objt_server(s->target))
1315 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_rewrites, 1);
1316
Christopher Faulet333bf8c2020-01-22 14:38:05 +01001317 if (!(msg->flags & HTTP_MSGF_SOFT_RW)) {
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001318 ret = ACT_RET_ERR;
Christopher Faulet333bf8c2020-01-22 14:38:05 +01001319 if (!(s->flags & SF_ERR_MASK))
1320 s->flags |= SF_ERR_PRXCOND;
1321 }
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001322 goto leave;
1323}
1324
Christopher Faulet81e20172019-12-12 16:40:30 +01001325/* Parse a "replace-header" or "replace-value" actions. It takes an header name,
1326 * a regex and replacement string as arguments. It returns ACT_RET_PRS_OK on
1327 * success, ACT_RET_PRS_ERR on error.
1328 */
1329static enum act_parse_ret parse_http_replace_header(const char **args, int *orig_arg, struct proxy *px,
1330 struct act_rule *rule, char **err)
1331{
1332 int cap, cur_arg;
1333
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001334 if (args[*orig_arg-1][8] == 'h')
1335 rule->action = 0; // replace-header
1336 else
1337 rule->action = 1; // replace-value
1338 rule->action_ptr = http_action_replace_header;
Christopher Faulet2eb53962020-01-14 14:47:34 +01001339 rule->release_ptr = release_http_action;
Christopher Faulet81e20172019-12-12 16:40:30 +01001340
1341 cur_arg = *orig_arg;
1342 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2]) {
1343 memprintf(err, "expects exactly 3 arguments");
1344 return ACT_RET_PRS_ERR;
1345 }
1346
Christopher Faulet96bff762019-12-17 13:46:18 +01001347 rule->arg.http.str.ptr = strdup(args[cur_arg]);
1348 rule->arg.http.str.len = strlen(rule->arg.http.str.ptr);
1349 LIST_INIT(&rule->arg.http.fmt);
Christopher Faulet81e20172019-12-12 16:40:30 +01001350
1351 cur_arg++;
Christopher Faulet1337b322020-01-14 14:50:55 +01001352 if (!(rule->arg.http.re = regex_comp(args[cur_arg], 1, 1, err))) {
Tim Duesterhused526372020-03-05 17:56:33 +01001353 istfree(&rule->arg.http.str);
Christopher Faulet81e20172019-12-12 16:40:30 +01001354 return ACT_RET_PRS_ERR;
Christopher Faulet1337b322020-01-14 14:50:55 +01001355 }
Christopher Faulet81e20172019-12-12 16:40:30 +01001356
1357 if (rule->from == ACT_F_HTTP_REQ) {
1358 px->conf.args.ctx = ARGC_HRQ;
1359 cap = (px->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR;
1360 }
1361 else{
1362 px->conf.args.ctx = ARGC_HRS;
1363 cap = (px->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR;
1364 }
1365
1366 cur_arg++;
Christopher Faulet1337b322020-01-14 14:50:55 +01001367 if (!parse_logformat_string(args[cur_arg], px, &rule->arg.http.fmt, LOG_OPT_HTTP, cap, err)) {
Tim Duesterhused526372020-03-05 17:56:33 +01001368 istfree(&rule->arg.http.str);
Christopher Faulet1337b322020-01-14 14:50:55 +01001369 regex_free(rule->arg.http.re);
Christopher Faulet81e20172019-12-12 16:40:30 +01001370 return ACT_RET_PRS_ERR;
Christopher Faulet1337b322020-01-14 14:50:55 +01001371 }
Christopher Faulet81e20172019-12-12 16:40:30 +01001372
1373 free(px->conf.lfs_file);
1374 px->conf.lfs_file = strdup(px->conf.args.file);
1375 px->conf.lfs_line = px->conf.args.line;
1376
1377 *orig_arg = cur_arg + 1;
1378 return ACT_RET_PRS_OK;
1379}
1380
1381/* Parse a "del-header" action. It takes an header name as argument. It returns
1382 * ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
1383 */
1384static enum act_parse_ret parse_http_del_header(const char **args, int *orig_arg, struct proxy *px,
1385 struct act_rule *rule, char **err)
1386{
1387 int cur_arg;
1388
1389 rule->action = ACT_HTTP_DEL_HDR;
Christopher Faulet2eb53962020-01-14 14:47:34 +01001390 rule->release_ptr = release_http_action;
Christopher Faulet81e20172019-12-12 16:40:30 +01001391
1392 cur_arg = *orig_arg;
1393 if (!*args[cur_arg]) {
1394 memprintf(err, "expects exactly 1 arguments");
1395 return ACT_RET_PRS_ERR;
1396 }
1397
Christopher Faulet96bff762019-12-17 13:46:18 +01001398 rule->arg.http.str.ptr = strdup(args[cur_arg]);
1399 rule->arg.http.str.len = strlen(rule->arg.http.str.ptr);
Christopher Faulet81e20172019-12-12 16:40:30 +01001400 px->conf.args.ctx = (rule->from == ACT_F_HTTP_REQ ? ARGC_HRQ : ARGC_HRS);
1401
Christopher Fauletc20b3712020-01-27 15:51:56 +01001402 LIST_INIT(&rule->arg.http.fmt);
Christopher Faulet81e20172019-12-12 16:40:30 +01001403 *orig_arg = cur_arg + 1;
1404 return ACT_RET_PRS_OK;
1405}
1406
Christopher Faulet2eb53962020-01-14 14:47:34 +01001407/* Release memory allocated by an http redirect action. */
1408static void release_http_redir(struct act_rule *rule)
1409{
1410 struct logformat_node *lf, *lfb;
1411 struct redirect_rule *redir;
1412
1413 redir = rule->arg.redir;
1414 LIST_DEL(&redir->list);
1415 if (redir->cond) {
1416 prune_acl_cond(redir->cond);
1417 free(redir->cond);
1418 }
1419 free(redir->rdr_str);
1420 free(redir->cookie_str);
1421 list_for_each_entry_safe(lf, lfb, &redir->rdr_fmt, list) {
1422 LIST_DEL(&lf->list);
1423 free(lf);
1424 }
1425 free(redir);
1426}
1427
Christopher Faulet81e20172019-12-12 16:40:30 +01001428/* Parse a "redirect" action. It returns ACT_RET_PRS_OK on success,
1429 * ACT_RET_PRS_ERR on error.
1430 */
1431static enum act_parse_ret parse_http_redirect(const char **args, int *orig_arg, struct proxy *px,
1432 struct act_rule *rule, char **err)
1433{
1434 struct redirect_rule *redir;
1435 int dir, cur_arg;
1436
1437 rule->action = ACT_HTTP_REDIR;
Christopher Faulet245cf792019-12-18 14:58:12 +01001438 rule->flags |= ACT_FLAG_FINAL;
Christopher Faulet2eb53962020-01-14 14:47:34 +01001439 rule->release_ptr = release_http_redir;
Christopher Faulet81e20172019-12-12 16:40:30 +01001440
1441 cur_arg = *orig_arg;
1442
1443 dir = (rule->from == ACT_F_HTTP_REQ ? 0 : 1);
1444 if ((redir = http_parse_redirect_rule(px->conf.args.file, px->conf.args.line, px, &args[cur_arg], err, 1, dir)) == NULL)
1445 return ACT_RET_PRS_ERR;
1446
1447 rule->arg.redir = redir;
1448 rule->cond = redir->cond;
1449 redir->cond = NULL;
1450
1451 /* skip all arguments */
1452 while (*args[cur_arg])
1453 cur_arg++;
1454
1455 *orig_arg = cur_arg;
1456 return ACT_RET_PRS_OK;
1457}
1458
Christopher Faulet046cf442019-12-17 15:45:23 +01001459/* This function executes a add-acl, del-acl, set-map or del-map actions. On
1460 * success, it returns ACT_RET_CONT. Otherwsize ACT_RET_ERR is returned.
1461 */
1462static enum act_return http_action_set_map(struct act_rule *rule, struct proxy *px,
1463 struct session *sess, struct stream *s, int flags)
1464{
1465 struct pat_ref *ref;
1466 struct buffer *key = NULL, *value = NULL;
1467 enum act_return ret = ACT_RET_CONT;
1468
1469 /* collect reference */
1470 ref = pat_ref_lookup(rule->arg.map.ref);
1471 if (!ref)
1472 goto leave;
1473
1474 /* allocate key */
1475 key = alloc_trash_chunk();
1476 if (!key)
1477 goto fail_alloc;
1478
1479 /* collect key */
1480 key->data = build_logline(s, key->area, key->size, &rule->arg.map.key);
1481 key->area[key->data] = '\0';
1482
1483 switch (rule->action) {
1484 case 0: // add-acl
1485 /* add entry only if it does not already exist */
1486 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
1487 if (pat_ref_find_elt(ref, key->area) == NULL)
1488 pat_ref_add(ref, key->area, NULL, NULL);
1489 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
1490 break;
1491
1492 case 1: // set-map
1493 /* allocate value */
1494 value = alloc_trash_chunk();
1495 if (!value)
1496 goto fail_alloc;
1497
1498 /* collect value */
1499 value->data = build_logline(s, value->area, value->size, &rule->arg.map.value);
1500 value->area[value->data] = '\0';
1501
1502 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
1503 if (pat_ref_find_elt(ref, key->area) != NULL) {
1504 /* update entry if it exists */
1505 pat_ref_set(ref, key->area, value->area, NULL);
1506 }
1507 else {
1508 /* insert a new entry */
1509 pat_ref_add(ref, key->area, value->area, NULL);
1510 }
1511 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
1512 break;
1513
1514 case 2: // del-acl
1515 case 3: // del-map
1516 /* returned code: 1=ok, 0=ko */
1517 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
1518 pat_ref_delete(ref, key->area);
1519 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
1520 break;
1521
1522 default:
1523 ret = ACT_RET_ERR;
1524 }
1525
1526
1527 leave:
1528 free_trash_chunk(key);
1529 free_trash_chunk(value);
1530 return ret;
1531
1532 fail_alloc:
1533 if (!(s->flags & SF_ERR_MASK))
1534 s->flags |= SF_ERR_RESOURCE;
1535 ret = ACT_RET_ERR;
1536 goto leave;
1537}
1538
Christopher Faulet2eb53962020-01-14 14:47:34 +01001539/* Release memory allocated by an http map/acl action. */
1540static void release_http_map(struct act_rule *rule)
1541{
1542 struct logformat_node *lf, *lfb;
1543
1544 free(rule->arg.map.ref);
1545 list_for_each_entry_safe(lf, lfb, &rule->arg.map.key, list) {
1546 LIST_DEL(&lf->list);
1547 release_sample_expr(lf->expr);
1548 free(lf->arg);
1549 free(lf);
1550 }
1551 if (rule->action == 1) {
1552 list_for_each_entry_safe(lf, lfb, &rule->arg.map.value, list) {
1553 LIST_DEL(&lf->list);
1554 release_sample_expr(lf->expr);
1555 free(lf->arg);
1556 free(lf);
1557 }
1558 }
1559}
1560
Christopher Faulet81e20172019-12-12 16:40:30 +01001561/* Parse a "add-acl", "del-acl", "set-map" or "del-map" actions. It takes one or
Christopher Faulet046cf442019-12-17 15:45:23 +01001562 * two log-format string as argument depending on the action. The action is
1563 * stored in <.action> as an int (0=add-acl, 1=set-map, 2=del-acl,
1564 * 3=del-map). It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Christopher Faulet81e20172019-12-12 16:40:30 +01001565 */
1566static enum act_parse_ret parse_http_set_map(const char **args, int *orig_arg, struct proxy *px,
1567 struct act_rule *rule, char **err)
1568{
1569 int cap, cur_arg;
1570
Christopher Faulet046cf442019-12-17 15:45:23 +01001571 if (args[*orig_arg-1][0] == 'a') // add-acl
1572 rule->action = 0;
1573 else if (args[*orig_arg-1][0] == 's') // set-map
1574 rule->action = 1;
1575 else if (args[*orig_arg-1][4] == 'a') // del-acl
1576 rule->action = 2;
1577 else if (args[*orig_arg-1][4] == 'm') // del-map
1578 rule->action = 3;
1579 else {
1580 memprintf(err, "internal error: unhandled action '%s'", args[0]);
1581 return ACT_RET_PRS_ERR;
1582 }
1583 rule->action_ptr = http_action_set_map;
Christopher Faulet2eb53962020-01-14 14:47:34 +01001584 rule->release_ptr = release_http_map;
Christopher Faulet81e20172019-12-12 16:40:30 +01001585
1586 cur_arg = *orig_arg;
Christopher Faulet046cf442019-12-17 15:45:23 +01001587 if (rule->action == 1 && (!*args[cur_arg] || !*args[cur_arg+1])) {
1588 /* 2 args for set-map */
Christopher Faulet81e20172019-12-12 16:40:30 +01001589 memprintf(err, "expects exactly 2 arguments");
1590 return ACT_RET_PRS_ERR;
1591 }
1592 else if (!*args[cur_arg]) {
Christopher Faulet046cf442019-12-17 15:45:23 +01001593 /* only one arg for other actions */
Christopher Faulet81e20172019-12-12 16:40:30 +01001594 memprintf(err, "expects exactly 1 arguments");
1595 return ACT_RET_PRS_ERR;
1596 }
1597
1598 /*
1599 * '+ 8' for 'set-map(' (same for del-map)
1600 * '- 9' for 'set-map(' + trailing ')' (same for del-map)
1601 */
1602 rule->arg.map.ref = my_strndup(args[cur_arg-1] + 8, strlen(args[cur_arg-1]) - 9);
1603
1604 if (rule->from == ACT_F_HTTP_REQ) {
1605 px->conf.args.ctx = ARGC_HRQ;
1606 cap = (px->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR;
1607 }
1608 else{
1609 px->conf.args.ctx = ARGC_HRS;
1610 cap = (px->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR;
1611 }
1612
1613 /* key pattern */
1614 LIST_INIT(&rule->arg.map.key);
Christopher Faulet1337b322020-01-14 14:50:55 +01001615 if (!parse_logformat_string(args[cur_arg], px, &rule->arg.map.key, LOG_OPT_HTTP, cap, err)) {
1616 free(rule->arg.map.ref);
Christopher Faulet81e20172019-12-12 16:40:30 +01001617 return ACT_RET_PRS_ERR;
Christopher Faulet1337b322020-01-14 14:50:55 +01001618 }
Christopher Faulet81e20172019-12-12 16:40:30 +01001619
Christopher Faulet046cf442019-12-17 15:45:23 +01001620 if (rule->action == 1) {
Christopher Faulet81e20172019-12-12 16:40:30 +01001621 /* value pattern for set-map only */
1622 cur_arg++;
1623 LIST_INIT(&rule->arg.map.value);
Christopher Faulet1337b322020-01-14 14:50:55 +01001624 if (!parse_logformat_string(args[cur_arg], px, &rule->arg.map.value, LOG_OPT_HTTP, cap, err)) {
1625 free(rule->arg.map.ref);
Christopher Faulet81e20172019-12-12 16:40:30 +01001626 return ACT_RET_PRS_ERR;
Christopher Faulet1337b322020-01-14 14:50:55 +01001627 }
Christopher Faulet81e20172019-12-12 16:40:30 +01001628 }
1629
1630 free(px->conf.lfs_file);
1631 px->conf.lfs_file = strdup(px->conf.args.file);
1632 px->conf.lfs_line = px->conf.args.line;
1633
1634 *orig_arg = cur_arg + 1;
1635 return ACT_RET_PRS_OK;
1636}
1637
Christopher Fauletac98d812019-12-18 09:20:16 +01001638/* This function executes a track-sc* actions. On success, it returns
1639 * ACT_RET_CONT. Otherwsize ACT_RET_ERR is returned.
1640 */
1641static enum act_return http_action_track_sc(struct act_rule *rule, struct proxy *px,
1642 struct session *sess, struct stream *s, int flags)
1643{
1644 struct stktable *t;
1645 struct stksess *ts;
1646 struct stktable_key *key;
1647 void *ptr1, *ptr2, *ptr3, *ptr4;
1648 int opt;
1649
1650 ptr1 = ptr2 = ptr3 = ptr4 = NULL;
1651 opt = ((rule->from == ACT_F_HTTP_REQ) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES) | SMP_OPT_FINAL;
1652
1653 t = rule->arg.trk_ctr.table.t;
1654 key = stktable_fetch_key(t, s->be, sess, s, opt, rule->arg.trk_ctr.expr, NULL);
1655
1656 if (!key)
1657 goto end;
1658 ts = stktable_get_entry(t, key);
1659 if (!ts)
1660 goto end;
1661
1662 stream_track_stkctr(&s->stkctr[rule->action], t, ts);
1663
1664 /* let's count a new HTTP request as it's the first time we do it */
1665 ptr1 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
1666 ptr2 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
1667
1668 /* When the client triggers a 4xx from the server, it's most often due
1669 * to a missing object or permission. These events should be tracked
1670 * because if they happen often, it may indicate a brute force or a
1671 * vulnerability scan. Normally this is done when receiving the response
1672 * but here we're tracking after this ought to have been done so we have
1673 * to do it on purpose.
1674 */
1675 if (rule->from == ACT_F_HTTP_RES && (unsigned)(s->txn->status - 400) < 100) {
1676 ptr3 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_CNT);
1677 ptr4 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_RATE);
1678 }
1679
1680 if (ptr1 || ptr2 || ptr3 || ptr4) {
1681 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
1682
1683 if (ptr1)
1684 stktable_data_cast(ptr1, http_req_cnt)++;
1685 if (ptr2)
1686 update_freq_ctr_period(&stktable_data_cast(ptr2, http_req_rate),
1687 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
1688 if (ptr3)
1689 stktable_data_cast(ptr3, http_err_cnt)++;
1690 if (ptr4)
1691 update_freq_ctr_period(&stktable_data_cast(ptr4, http_err_rate),
1692 t->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1);
1693
1694 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
1695
1696 /* If data was modified, we need to touch to re-schedule sync */
1697 stktable_touch_local(t, ts, 0);
1698 }
1699
1700 stkctr_set_flags(&s->stkctr[rule->action], STKCTR_TRACK_CONTENT);
1701 if (sess->fe != s->be)
1702 stkctr_set_flags(&s->stkctr[rule->action], STKCTR_TRACK_BACKEND);
1703
1704 end:
1705 return ACT_RET_CONT;
1706}
Christopher Faulet81e20172019-12-12 16:40:30 +01001707
Christopher Faulet2eb53962020-01-14 14:47:34 +01001708static void release_http_track_sc(struct act_rule *rule)
1709{
1710 release_sample_expr(rule->arg.trk_ctr.expr);
1711}
1712
Christopher Faulet81e20172019-12-12 16:40:30 +01001713/* Parse a "track-sc*" actions. It returns ACT_RET_PRS_OK on success,
1714 * ACT_RET_PRS_ERR on error.
1715 */
1716static enum act_parse_ret parse_http_track_sc(const char **args, int *orig_arg, struct proxy *px,
1717 struct act_rule *rule, char **err)
1718{
1719 struct sample_expr *expr;
1720 unsigned int where;
1721 unsigned int tsc_num;
1722 const char *tsc_num_str;
1723 int cur_arg;
1724
1725 tsc_num_str = &args[*orig_arg-1][8];
1726 if (cfg_parse_track_sc_num(&tsc_num, tsc_num_str, tsc_num_str + strlen(tsc_num_str), err) == -1)
1727 return ACT_RET_PRS_ERR;
1728
1729 cur_arg = *orig_arg;
1730 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line,
Willy Tarreaue3b57bf2020-02-14 16:50:14 +01001731 err, &px->conf.args, NULL);
Christopher Faulet81e20172019-12-12 16:40:30 +01001732 if (!expr)
1733 return ACT_RET_PRS_ERR;
1734
1735 where = 0;
1736 if (px->cap & PR_CAP_FE)
1737 where |= (rule->from == ACT_F_HTTP_REQ ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_FE_HRS_HDR);
1738 if (px->cap & PR_CAP_BE)
1739 where |= (rule->from == ACT_F_HTTP_REQ ? SMP_VAL_BE_HRQ_HDR : SMP_VAL_BE_HRS_HDR);
1740
1741 if (!(expr->fetch->val & where)) {
1742 memprintf(err, "fetch method '%s' extracts information from '%s', none of which is available here",
1743 args[cur_arg-1], sample_src_names(expr->fetch->use));
Christopher Faulet1337b322020-01-14 14:50:55 +01001744 release_sample_expr(expr);
Christopher Faulet81e20172019-12-12 16:40:30 +01001745 return ACT_RET_PRS_ERR;
1746 }
1747
1748 if (strcmp(args[cur_arg], "table") == 0) {
1749 cur_arg++;
1750 if (!*args[cur_arg]) {
1751 memprintf(err, "missing table name");
Christopher Faulet1337b322020-01-14 14:50:55 +01001752 release_sample_expr(expr);
Christopher Faulet81e20172019-12-12 16:40:30 +01001753 return ACT_RET_PRS_ERR;
1754 }
1755
1756 /* we copy the table name for now, it will be resolved later */
1757 rule->arg.trk_ctr.table.n = strdup(args[cur_arg]);
1758 cur_arg++;
1759 }
1760
Christopher Fauletac98d812019-12-18 09:20:16 +01001761 rule->action = tsc_num;
Christopher Faulet81e20172019-12-12 16:40:30 +01001762 rule->arg.trk_ctr.expr = expr;
Christopher Fauletac98d812019-12-18 09:20:16 +01001763 rule->action_ptr = http_action_track_sc;
Christopher Faulet2eb53962020-01-14 14:47:34 +01001764 rule->release_ptr = release_http_track_sc;
Christopher Faulet81e20172019-12-12 16:40:30 +01001765 rule->check_ptr = check_trk_action;
1766
1767 *orig_arg = cur_arg;
1768 return ACT_RET_PRS_OK;
1769}
1770
Christopher Faulet46f95542019-12-20 10:07:22 +01001771/* This function executes a strict-mode actions. On success, it always returns
1772 * ACT_RET_CONT
1773 */
1774static enum act_return http_action_strict_mode(struct act_rule *rule, struct proxy *px,
1775 struct session *sess, struct stream *s, int flags)
1776{
1777 struct http_msg *msg = ((rule->from == ACT_F_HTTP_REQ) ? &s->txn->req : &s->txn->rsp);
1778
1779 if (rule->action == 0) // strict-mode on
1780 msg->flags &= ~HTTP_MSGF_SOFT_RW;
1781 else // strict-mode off
1782 msg->flags |= HTTP_MSGF_SOFT_RW;
1783 return ACT_RET_CONT;
1784}
1785
1786/* Parse a "strict-mode" action. It returns ACT_RET_PRS_OK on success,
1787 * ACT_RET_PRS_ERR on error.
1788 */
1789static enum act_parse_ret parse_http_strict_mode(const char **args, int *orig_arg, struct proxy *px,
1790 struct act_rule *rule, char **err)
1791{
1792 int cur_arg;
1793
Christopher Faulet46f95542019-12-20 10:07:22 +01001794 cur_arg = *orig_arg;
1795 if (!*args[cur_arg]) {
1796 memprintf(err, "expects exactly 1 arguments");
1797 return ACT_RET_PRS_ERR;
1798 }
1799
1800 if (strcasecmp(args[cur_arg], "on") == 0)
1801 rule->action = 0; // strict-mode on
1802 else if (strcasecmp(args[cur_arg], "off") == 0)
1803 rule->action = 1; // strict-mode off
1804 else {
1805 memprintf(err, "Unexpected value '%s'. Only 'on' and 'off' are supported", args[cur_arg]);
1806 return ACT_RET_PRS_ERR;
1807 }
1808 rule->action_ptr = http_action_strict_mode;
1809
1810 *orig_arg = cur_arg + 1;
1811 return ACT_RET_PRS_OK;
1812}
1813
Christopher Faulet24231ab2020-01-24 17:44:23 +01001814/* Release <.arg.http_return> */
1815static void release_http_return(struct act_rule *rule)
1816{
1817 struct logformat_node *lf, *lfb;
Christopher Faulet4a2c1422020-01-31 17:36:01 +01001818 struct http_ret_hdr *hdr, *hdrb;
Christopher Faulet24231ab2020-01-24 17:44:23 +01001819
1820 free(rule->arg.http_return.ctype);
Christopher Faulet4a2c1422020-01-31 17:36:01 +01001821
1822 if (rule->arg.http_return.hdrs) {
1823 list_for_each_entry_safe(hdr, hdrb, rule->arg.http_return.hdrs, list) {
1824 LIST_DEL(&hdr->list);
1825 list_for_each_entry_safe(lf, lfb, &hdr->value, list) {
1826 LIST_DEL(&lf->list);
1827 release_sample_expr(lf->expr);
1828 free(lf->arg);
1829 free(lf);
1830 }
Tim Duesterhused526372020-03-05 17:56:33 +01001831 istfree(&hdr->name);
Christopher Faulet4a2c1422020-01-31 17:36:01 +01001832 free(hdr);
1833 }
1834 free(rule->arg.http_return.hdrs);
1835 }
1836
Christopher Faulet24231ab2020-01-24 17:44:23 +01001837 if (rule->action == 2) {
1838 chunk_destroy(&rule->arg.http_return.body.obj);
1839 }
1840 else if (rule->action == 3) {
1841 list_for_each_entry_safe(lf, lfb, &rule->arg.http_return.body.fmt, list) {
1842 LIST_DEL(&lf->list);
1843 release_sample_expr(lf->expr);
1844 free(lf->arg);
1845 free(lf);
1846 }
1847 }
1848}
1849
1850/* This function executes a return action. It builds an HTX message from an
1851 * errorfile, an raw file or a log-format string, depending on <.action>
1852 * value. On success, it returns ACT_RET_ABRT. If an error occurs ACT_RET_ERR is
1853 * returned.
1854 */
1855static enum act_return http_action_return(struct act_rule *rule, struct proxy *px,
1856 struct session *sess, struct stream *s, int flags)
1857{
1858 struct channel *req = &s->req;
1859 struct channel *res = &s->res;
1860 struct buffer *errmsg;
1861 struct htx *htx = htx_from_buf(&res->buf);
1862 struct htx_sl *sl;
1863 struct buffer *body = NULL;
1864 const char *status, *reason, *clen, *ctype;
1865 unsigned int slflags;
Christopher Faulet90d22a82020-03-06 11:18:39 +01001866 enum act_return ret = ACT_RET_ABRT;
Christopher Faulet24231ab2020-01-24 17:44:23 +01001867
1868 s->txn->status = rule->arg.http_return.status;
1869 channel_htx_truncate(res, htx);
1870
1871 if (rule->action == 1) {
1872 /* implicit or explicit error message*/
1873 errmsg = rule->arg.http_return.body.errmsg;
1874 if (!errmsg) {
1875 /* get default error message */
1876 errmsg = http_error_message(s);
1877 }
1878 if (b_is_null(errmsg))
1879 goto end;
1880 if (!channel_htx_copy_msg(res, htx, errmsg))
1881 goto fail;
1882 }
1883 else {
1884 /* no payload, file or log-format string */
1885 if (rule->action == 2) {
1886 /* file */
1887 body = &rule->arg.http_return.body.obj;
1888 }
1889 else if (rule->action == 3) {
1890 /* log-format string */
1891 body = alloc_trash_chunk();
1892 if (!body)
1893 goto fail_alloc;
1894 body->data = build_logline(s, body->area, body->size, &rule->arg.http_return.body.fmt);
1895 }
1896 /* else no payload */
1897
1898 status = ultoa(rule->arg.http_return.status);
1899 reason = http_get_reason(rule->arg.http_return.status);
1900 slflags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CLEN);
1901 if (!body || !b_data(body))
1902 slflags |= HTX_SL_F_BODYLESS;
1903 sl = htx_add_stline(htx, HTX_BLK_RES_SL, slflags, ist("HTTP/1.1"), ist(status), ist(reason));
1904 if (!sl)
1905 goto fail;
1906 sl->info.res.status = rule->arg.http_return.status;
1907
1908 clen = (body ? ultoa(b_data(body)) : "0");
1909 ctype = rule->arg.http_return.ctype;
1910
Christopher Faulet4a2c1422020-01-31 17:36:01 +01001911 if (rule->arg.http_return.hdrs) {
1912 struct http_ret_hdr *hdr;
1913 struct buffer *value = alloc_trash_chunk();
1914
1915 if (!value)
1916 goto fail;
1917
1918 list_for_each_entry(hdr, rule->arg.http_return.hdrs, list) {
1919 chunk_reset(value);
1920 value->data = build_logline(s, value->area, value->size, &hdr->value);
1921 if (b_data(value) && !htx_add_header(htx, hdr->name, ist2(b_head(value), b_data(value)))) {
1922 free_trash_chunk(value);
1923 goto fail;
1924 }
1925 chunk_reset(value);
1926 }
1927 free_trash_chunk(value);
1928 }
1929
Christopher Faulet24231ab2020-01-24 17:44:23 +01001930 if (!htx_add_header(htx, ist("content-length"), ist(clen)) ||
1931 (body && b_data(body) && ctype && !htx_add_header(htx, ist("content-type"), ist(ctype))) ||
1932 !htx_add_endof(htx, HTX_BLK_EOH) ||
1933 (body && b_data(body) && !htx_add_data_atonce(htx, ist2(b_head(body), b_data(body)))) ||
1934 !htx_add_endof(htx, HTX_BLK_EOM))
1935 goto fail;
1936 }
1937
1938 htx_to_buf(htx, &s->res.buf);
1939 if (!http_forward_proxy_resp(s, 1))
1940 goto fail;
1941
1942 end:
1943 if (rule->from == ACT_F_HTTP_REQ) {
1944 /* let's log the request time */
1945 s->logs.tv_request = now;
1946 req->analysers &= AN_REQ_FLT_END;
1947
1948 if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */
1949 _HA_ATOMIC_ADD(&s->sess->fe->fe_counters.intercepted_req, 1);
1950 }
1951
1952 if (!(s->flags & SF_ERR_MASK))
1953 s->flags |= SF_ERR_LOCAL;
1954 if (!(s->flags & SF_FINST_MASK))
1955 s->flags |= ((rule->from == ACT_F_HTTP_REQ) ? SF_FINST_R : SF_FINST_H);
1956
1957 leave:
1958 if (rule->action == 3)
1959 free_trash_chunk(body);
1960 return ret;
1961
1962 fail_alloc:
1963 if (!(s->flags & SF_ERR_MASK))
1964 s->flags |= SF_ERR_RESOURCE;
1965 ret = ACT_RET_ERR;
1966 goto leave;
1967
1968 fail:
1969 /* If an error occurred, remove the incomplete HTTP response from the
1970 * buffer */
1971 channel_htx_truncate(res, htx);
1972 ret = ACT_RET_ERR;
1973 if (!(s->flags & SF_ERR_MASK))
1974 s->flags |= SF_ERR_PRXCOND;
1975 goto leave;
1976}
1977
1978/* Check an "http-request return" action when an http-errors section is referenced.
1979 *
1980 * The function returns 1 in success case, otherwise, it returns 0 and err is
1981 * filled.
1982 */
1983static int check_http_return_action(struct act_rule *rule, struct proxy *px, char **err)
1984{
1985 struct http_errors *http_errs;
1986 int status = (intptr_t)(rule->arg.act.p[0]);
1987 int ret = 1;
1988
1989 list_for_each_entry(http_errs, &http_errors_list, list) {
1990 if (strcmp(http_errs->id, (char *)rule->arg.act.p[1]) == 0) {
1991 free(rule->arg.act.p[1]);
1992 rule->arg.http_return.status = status;
1993 rule->arg.http_return.ctype = NULL;
1994 rule->action = 1;
1995 rule->arg.http_return.body.errmsg = http_errs->errmsg[http_get_status_idx(status)];
1996 rule->action_ptr = http_action_return;
1997 rule->release_ptr = release_http_return;
1998
1999 if (!rule->arg.http_return.body.errmsg)
2000 ha_warning("Proxy '%s': status '%d' referenced by http return rule "
2001 "not declared in http-errors section '%s'.\n",
2002 px->id, status, http_errs->id);
2003 break;
2004 }
2005 }
2006
2007 if (&http_errs->list == &http_errors_list) {
2008 memprintf(err, "unknown http-errors section '%s' referenced by http return rule",
2009 (char *)rule->arg.act.p[1]);
2010 free(rule->arg.act.p[1]);
2011 ret = 0;
2012 }
2013
2014 return ret;
2015}
2016
2017/* Parse a "return" action. It returns ACT_RET_PRS_OK on success,
2018 * ACT_RET_PRS_ERR on error. This function creates 4 action types:
2019 *
2020 * - action 0 : dummy response, no payload
2021 * - action 1 : implicit error message depending on the status code or explicit one
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05002022 * - action 2 : explicit file object ('file' argument)
Christopher Faulet24231ab2020-01-24 17:44:23 +01002023 * - action 3 : explicit log-format string ('content' argument)
2024 *
2025 * The content-type must be defined for non-empty payload. It is ignored for
2026 * error messages (implicit or explicit). When an http-errors section is
2027 * referenced, action is set to -1 and the real action is resolved during the
2028 * configuration validity check.
2029 */
2030static enum act_parse_ret parse_http_return(const char **args, int *orig_arg, struct proxy *px,
2031 struct act_rule *rule, char **err)
2032{
2033 struct logformat_node *lf, *lfb;
Christopher Faulet4a2c1422020-01-31 17:36:01 +01002034 struct http_ret_hdr *hdr, *hdrb;
2035 struct list *hdrs = NULL;
Christopher Faulet24231ab2020-01-24 17:44:23 +01002036 struct stat stat;
2037 const char *file = NULL, *act_arg = NULL;
2038 char *obj = NULL, *ctype = NULL, *name = NULL;
2039 int cur_arg, cap, objlen = 0, action = 0, status = 200, fd = -1;
2040
Christopher Faulet4a2c1422020-01-31 17:36:01 +01002041 hdrs = calloc(1, sizeof(*hdrs));
2042 if (!hdrs) {
2043 memprintf(err, "out of memory");
2044 goto error;
2045 }
2046 LIST_INIT(hdrs);
Christopher Faulet24231ab2020-01-24 17:44:23 +01002047
Christopher Faulet4a2c1422020-01-31 17:36:01 +01002048 cur_arg = *orig_arg;
Christopher Faulet24231ab2020-01-24 17:44:23 +01002049 while (*args[cur_arg]) {
2050 if (strcmp(args[cur_arg], "status") == 0) {
2051 cur_arg++;
2052 if (!*args[cur_arg]) {
2053 memprintf(err, "'%s' expects <status_code> as argument", args[cur_arg-1]);
Christopher Faulet817c4e32020-02-07 10:26:23 +01002054 goto error;
Christopher Faulet24231ab2020-01-24 17:44:23 +01002055 }
2056 status = atol(args[cur_arg]);
2057 if (status < 200 || status > 599) {
2058 memprintf(err, "Unexpected status code '%d'", status);
2059 goto error;
2060 }
2061 cur_arg++;
2062 }
2063 else if (strcmp(args[cur_arg], "content-type") == 0) {
2064 cur_arg++;
2065 if (!*args[cur_arg]) {
2066 memprintf(err, "'%s' expects <ctype> as argument", args[cur_arg-1]);
2067 goto error;
2068 }
2069 free(ctype);
2070 ctype = strdup(args[cur_arg]);
2071 cur_arg++;
2072 }
2073 else if (strcmp(args[cur_arg], "errorfiles") == 0) {
2074 if (action != 0) {
2075 memprintf(err, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
2076 goto error;
2077 }
2078 act_arg = args[cur_arg];
2079 cur_arg++;
2080 if (!*args[cur_arg]) {
2081 memprintf(err, "'%s' expects <name> as argument", args[cur_arg-1]);
2082 goto error;
2083 }
2084 /* Must be resolved during the config validity check */
2085 name = strdup(args[cur_arg]);
2086 action = -1;
2087 cur_arg++;
2088 }
2089 else if (strcmp(args[cur_arg], "default-errorfiles") == 0) {
2090 if (action != 0) {
2091 memprintf(err, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
2092 goto error;
2093 }
2094 act_arg = args[cur_arg];
2095 action = 1;
2096 cur_arg++;
2097 }
2098 else if (strcmp(args[cur_arg], "errorfile") == 0) {
2099 if (action != 0) {
2100 memprintf(err, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
2101 goto error;
2102 }
2103 act_arg = args[cur_arg];
2104 cur_arg++;
2105 if (!*args[cur_arg]) {
2106 memprintf(err, "'%s' expects <fmt> as argument", args[cur_arg-1]);
2107 goto error;
2108 }
2109 file = args[cur_arg];
2110 rule->arg.http_return.body.errmsg = http_load_errorfile(args[cur_arg], err);
2111 if (!rule->arg.http_return.body.errmsg) {
2112 goto error;
2113 }
2114 action = 1;
2115 cur_arg++;
2116 }
2117 else if (strcmp(args[cur_arg], "file") == 0) {
2118 if (action != 0) {
2119 memprintf(err, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
2120 goto error;
2121 }
2122 act_arg = args[cur_arg];
2123 cur_arg++;
2124 if (!*args[cur_arg]) {
2125 memprintf(err, "'%s' expects <file> as argument", args[cur_arg-1]);
2126 goto error;
2127 }
2128 file = args[cur_arg];
2129 fd = open(args[cur_arg], O_RDONLY);
2130 if ((fd < 0) || (fstat(fd, &stat) < 0)) {
2131 memprintf(err, "error opening file '%s'", args[cur_arg]);
2132 goto error;
2133 }
2134 if (stat.st_size > global.tune.bufsize) {
Willy Tarreaue35d1d42020-02-11 10:58:56 +01002135 memprintf(err, "file '%s' exceeds the buffer size (%lld > %d)",
2136 args[cur_arg], (long long)stat.st_size, global.tune.bufsize);
Christopher Faulet24231ab2020-01-24 17:44:23 +01002137 goto error;
2138 }
2139 objlen = stat.st_size;
2140 obj = malloc(objlen);
2141 if (!obj || read(fd, obj, objlen) != objlen) {
2142 memprintf(err, "error reading file '%s'", args[cur_arg]);
2143 goto error;
2144 }
2145 close(fd);
2146 fd = -1;
2147 action = 2;
2148 cur_arg++;
2149 }
2150 else if (strcmp(args[cur_arg], "string") == 0) {
2151 if (action != 0) {
2152 memprintf(err, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
2153 goto error;
2154 }
2155 act_arg = args[cur_arg];
2156 cur_arg++;
2157 if (!*args[cur_arg]) {
2158 memprintf(err, "'%s' expects <str> as argument", args[cur_arg-1]);
2159 goto error;
2160 }
2161 obj = strdup(args[cur_arg]);
2162 objlen = strlen(args[cur_arg]);
2163 action = 2;
2164 cur_arg++;
2165 }
2166 else if (strcmp(args[cur_arg], "lf-file") == 0) {
2167 if (action != 0) {
2168 memprintf(err, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
2169 goto error;
2170 }
2171 act_arg = args[cur_arg];
2172 cur_arg++;
2173 if (!*args[cur_arg]) {
2174 memprintf(err, "'%s' expects <file> as argument", args[cur_arg-1]);
2175 goto error;
2176 }
2177 file = args[cur_arg];
2178 fd = open(args[cur_arg], O_RDONLY);
2179 if ((fd < 0) || (fstat(fd, &stat) < 0)) {
2180 memprintf(err, "error opening file '%s'", args[cur_arg]);
2181 goto error;
2182 }
2183 if (stat.st_size > global.tune.bufsize) {
Willy Tarreaue35d1d42020-02-11 10:58:56 +01002184 memprintf(err, "file '%s' exceeds the buffer size (%lld > %d)",
2185 args[cur_arg], (long long)stat.st_size, global.tune.bufsize);
Christopher Faulet24231ab2020-01-24 17:44:23 +01002186 goto error;
2187 }
2188 objlen = stat.st_size;
2189 obj = malloc(objlen + 1);
2190 if (!obj || read(fd, obj, objlen) != objlen) {
2191 memprintf(err, "error reading file '%s'", args[cur_arg]);
2192 goto error;
2193 }
2194 close(fd);
2195 fd = -1;
2196 obj[objlen] = '\0';
2197 action = 3;
2198 cur_arg++;
2199 }
2200 else if (strcmp(args[cur_arg], "lf-string") == 0) {
2201 if (action != 0) {
2202 memprintf(err, "unexpected '%s' argument, '%s' already defined", args[cur_arg], act_arg);
2203 goto error;
2204 }
2205 act_arg = args[cur_arg];
2206 cur_arg++;
2207 if (!*args[cur_arg]) {
2208 memprintf(err, "'%s' expects <fmt> as argument", args[cur_arg-1]);
2209 goto error;
2210 }
2211 obj = strdup(args[cur_arg]);
2212 objlen = strlen(args[cur_arg]);
2213 action = 3;
2214 cur_arg++;
2215 }
Christopher Faulet4a2c1422020-01-31 17:36:01 +01002216 else if (strcmp(args[cur_arg], "hdr") == 0) {
2217 cur_arg++;
2218 if (!*args[cur_arg] || !*args[cur_arg+1]) {
2219 memprintf(err, "'%s' expects <name> and <value> as arguments", args[cur_arg-1]);
2220 goto error;
2221 }
2222 if (strcasecmp(args[cur_arg], "content-length") == 0 ||
2223 strcasecmp(args[cur_arg], "transfer-encoding") == 0 ||
2224 strcasecmp(args[cur_arg], "content-type") == 0) {
2225 ha_warning("parsing [%s:%d] : 'http-%s return' : header '%s' ignored.\n",
2226 px->conf.args.file, px->conf.args.line,
2227 (rule->from == ACT_F_HTTP_REQ ? "request" : "response"),
2228 args[cur_arg]);
2229 cur_arg += 2;
2230 continue;
2231 }
2232 hdr = calloc(1, sizeof(*hdr));
2233 if (!hdr) {
2234 memprintf(err, "'%s' : out of memory", args[cur_arg-1]);
2235 goto error;
2236 }
2237 LIST_INIT(&hdr->value);
2238 hdr->name = ist(strdup(args[cur_arg]));
2239 LIST_ADDQ(hdrs, &hdr->list);
2240
2241 if (rule->from == ACT_F_HTTP_REQ) {
2242 px->conf.args.ctx = ARGC_HRQ;
2243 cap = (px->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR;
2244 }
2245 else {
2246 px->conf.args.ctx = ARGC_HRS;
2247 cap = (px->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR;
2248 }
2249 if (!parse_logformat_string(args[cur_arg+1], px, &hdr->value, LOG_OPT_HTTP, cap, err))
2250 goto error;
2251
2252 free(px->conf.lfs_file);
2253 px->conf.lfs_file = strdup(px->conf.args.file);
2254 px->conf.lfs_line = px->conf.args.line;
2255 cur_arg += 2;
2256 }
Christopher Faulet24231ab2020-01-24 17:44:23 +01002257 else
2258 break;
2259 }
2260
2261
2262 if (action == -1) { /* errorfiles */
2263 int rc;
2264
2265 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
2266 if (http_err_codes[rc] == status)
2267 break;
2268 }
2269
2270 if (rc >= HTTP_ERR_SIZE) {
2271 memprintf(err, "status code '%d' not handled by default with '%s' argument.",
2272 status, act_arg);
2273 goto error;
2274 }
2275 if (ctype) {
2276 ha_warning("parsing [%s:%d] : 'http-%s return' : content-type '%s' ignored when the "
2277 "returned response is an erorrfile.\n",
2278 px->conf.args.file, px->conf.args.line,
2279 (rule->from == ACT_F_HTTP_REQ ? "request" : "response"),
2280 ctype);
2281 free(ctype);
2282 ctype = NULL;
2283 }
Christopher Faulet4a2c1422020-01-31 17:36:01 +01002284 if (!LIST_ISEMPTY(hdrs)) {
2285 ha_warning("parsing [%s:%d] : 'http-%s return' : hdr parameters ignored when the "
2286 "returned response is an erorrfile.\n",
2287 px->conf.args.file, px->conf.args.line,
2288 (rule->from == ACT_F_HTTP_REQ ? "request" : "response"));
2289 list_for_each_entry_safe(hdr, hdrb, hdrs, list) {
2290 LIST_DEL(&hdr->list);
2291 list_for_each_entry_safe(lf, lfb, &hdr->value, list) {
2292 LIST_DEL(&lf->list);
2293 release_sample_expr(lf->expr);
2294 free(lf->arg);
2295 free(lf);
2296 }
Tim Duesterhused526372020-03-05 17:56:33 +01002297 istfree(&hdr->name);
Christopher Faulet4a2c1422020-01-31 17:36:01 +01002298 free(hdr);
2299 }
2300 }
2301 free(hdrs);
2302 hdrs = NULL;
Christopher Faulet24231ab2020-01-24 17:44:23 +01002303
2304 rule->arg.act.p[0] = (void *)((intptr_t)status);
2305 rule->arg.act.p[1] = name;
2306 rule->check_ptr = check_http_return_action;
2307 goto out;
2308 }
2309 else if (action == 0) { /* no payload */
2310 if (ctype) {
2311 ha_warning("parsing [%s:%d] : 'http-%s return' : content-type '%s' ignored because"
2312 " neither errorfile nor payload defined.\n",
2313 px->conf.args.file, px->conf.args.line,
2314 (rule->from == ACT_F_HTTP_REQ ? "request" : "response"),
2315 ctype);
2316 free(ctype);
2317 ctype = NULL;
2318 }
2319 }
2320 else if (action == 1) { /* errorfile */
2321 if (!rule->arg.http_return.body.errmsg) { /* default errorfile */
2322 int rc;
2323
2324 for (rc = 0; rc < HTTP_ERR_SIZE; rc++) {
2325 if (http_err_codes[rc] == status)
2326 break;
2327 }
2328
2329 if (rc >= HTTP_ERR_SIZE) {
2330 memprintf(err, "status code '%d' not handled by default with '%s' argument",
2331 status, act_arg);
2332 goto error;
2333 }
2334 if (ctype) {
2335 ha_warning("parsing [%s:%d] : 'http-%s return' : content-type '%s' ignored when the "
2336 "returned response is an erorrfile.\n",
2337 px->conf.args.file, px->conf.args.line,
2338 (rule->from == ACT_F_HTTP_REQ ? "request" : "response"),
2339 ctype);
2340 free(ctype);
2341 ctype = NULL;
2342 }
2343 }
2344 else { /* explicit payload using 'errorfile' parameter */
2345 if (ctype) {
2346 ha_warning("parsing [%s:%d] : 'http-%s return' : content-type '%s' ignored because"
2347 " the errorfile '%s' is used.\n",
2348 px->conf.args.file, px->conf.args.line,
2349 (rule->from == ACT_F_HTTP_REQ ? "request" : "response"),
2350 ctype, file);
2351 free(ctype);
2352 ctype = NULL;
2353 }
2354 }
Christopher Faulet4a2c1422020-01-31 17:36:01 +01002355 if (!LIST_ISEMPTY(hdrs)) {
2356 ha_warning("parsing [%s:%d] : 'http-%s return' : hdr parameters ignored when the "
2357 "returned response is an erorrfile.\n",
2358 px->conf.args.file, px->conf.args.line,
2359 (rule->from == ACT_F_HTTP_REQ ? "request" : "response"));
2360 list_for_each_entry_safe(hdr, hdrb, hdrs, list) {
2361 LIST_DEL(&hdr->list);
2362 list_for_each_entry_safe(lf, lfb, &hdr->value, list) {
2363 LIST_DEL(&lf->list);
2364 release_sample_expr(lf->expr);
2365 free(lf->arg);
2366 free(lf);
2367 }
Tim Duesterhused526372020-03-05 17:56:33 +01002368 istfree(&hdr->name);
Christopher Faulet4a2c1422020-01-31 17:36:01 +01002369 free(hdr);
2370 }
2371 }
2372 free(hdrs);
2373 hdrs = NULL;
Christopher Faulet24231ab2020-01-24 17:44:23 +01002374 }
2375 else if (action == 2) { /* explicit parameter using 'file' parameter*/
2376 if (!ctype && objlen) {
2377 memprintf(err, "a content type must be defined when non-empty payload is configured");
2378 goto error;
2379 }
2380 if (ctype && !objlen) {
2381 ha_warning("parsing [%s:%d] : 'http-%s return' : content-type '%s' ignored when the "
2382 "configured payload is empty.\n",
2383 px->conf.args.file, px->conf.args.line,
2384 (rule->from == ACT_F_HTTP_REQ ? "request" : "response"),
2385 ctype);
2386 free(ctype);
2387 ctype = NULL;
2388 }
2389 if (global.tune.bufsize - objlen < global.tune.maxrewrite) {
Dominik Froehlich30d49ab2020-04-17 11:04:35 +02002390 ha_warning("parsing [%s:%d] : 'http-%s return' : the payload runs over the buffer space reserved to headers rewriting."
2391 " It may lead to internal errors if strict rewriting mode is enabled.\n",
Christopher Faulet24231ab2020-01-24 17:44:23 +01002392 px->conf.args.file, px->conf.args.line,
2393 (rule->from == ACT_F_HTTP_REQ ? "request" : "response"));
2394 }
2395 chunk_initlen(&rule->arg.http_return.body.obj, obj, global.tune.bufsize, objlen);
2396 }
2397 else if (action == 3) { /* log-format payload using 'lf-file' of 'lf-string' parameter */
2398 LIST_INIT(&rule->arg.http_return.body.fmt);
2399 if (!ctype) {
2400 memprintf(err, "a content type must be defined with a log-format payload");
2401 goto error;
2402 }
2403 if (rule->from == ACT_F_HTTP_REQ) {
2404 px->conf.args.ctx = ARGC_HRQ;
2405 cap = (px->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR;
2406 }
2407 else {
2408 px->conf.args.ctx = ARGC_HRS;
2409 cap = (px->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR;
2410 }
2411 if (!parse_logformat_string(obj, px, &rule->arg.http_return.body.fmt, LOG_OPT_HTTP, cap, err))
2412 goto error;
2413
2414 free(px->conf.lfs_file);
2415 px->conf.lfs_file = strdup(px->conf.args.file);
2416 px->conf.lfs_line = px->conf.args.line;
2417 free(obj);
2418 }
2419
2420 rule->arg.http_return.status = status;
2421 rule->arg.http_return.ctype = ctype;
Christopher Faulet4a2c1422020-01-31 17:36:01 +01002422 rule->arg.http_return.hdrs = hdrs;
Christopher Faulet24231ab2020-01-24 17:44:23 +01002423 rule->action = action;
2424 rule->action_ptr = http_action_return;
2425 rule->release_ptr = release_http_return;
2426
2427 out:
2428 *orig_arg = cur_arg;
2429 return ACT_RET_PRS_OK;
2430
2431 error:
2432 free(obj);
2433 free(ctype);
2434 free(name);
2435 if (fd >= 0)
2436 close(fd);
Christopher Faulet817c4e32020-02-07 10:26:23 +01002437 if (hdrs) {
2438 list_for_each_entry_safe(hdr, hdrb, hdrs, list) {
2439 LIST_DEL(&hdr->list);
2440 list_for_each_entry_safe(lf, lfb, &hdr->value, list) {
2441 LIST_DEL(&lf->list);
2442 release_sample_expr(lf->expr);
2443 free(lf->arg);
2444 free(lf);
2445 }
Tim Duesterhused526372020-03-05 17:56:33 +01002446 istfree(&hdr->name);
Christopher Faulet817c4e32020-02-07 10:26:23 +01002447 free(hdr);
Christopher Faulet4a2c1422020-01-31 17:36:01 +01002448 }
Christopher Faulet817c4e32020-02-07 10:26:23 +01002449 free(hdrs);
Christopher Faulet4a2c1422020-01-31 17:36:01 +01002450 }
Christopher Faulet24231ab2020-01-24 17:44:23 +01002451 if (action == 3) {
2452 list_for_each_entry_safe(lf, lfb, &rule->arg.http_return.body.fmt, list) {
2453 LIST_DEL(&lf->list);
2454 release_sample_expr(lf->expr);
2455 free(lf->arg);
2456 free(lf);
2457 }
2458 }
2459 free(rule->arg.http_return.ctype);
2460 return ACT_RET_PRS_ERR;
2461}
2462
Willy Tarreau79e57332018-10-02 16:01:16 +02002463/************************************************************************/
2464/* All supported http-request action keywords must be declared here. */
2465/************************************************************************/
2466
2467static struct action_kw_list http_req_actions = {
2468 .kw = {
Christopher Faulet81e20172019-12-12 16:40:30 +01002469 { "add-acl", parse_http_set_map, 1 },
2470 { "add-header", parse_http_set_header, 0 },
2471 { "allow", parse_http_allow, 0 },
2472 { "auth", parse_http_auth, 0 },
2473 { "capture", parse_http_req_capture, 0 },
2474 { "del-acl", parse_http_set_map, 1 },
2475 { "del-header", parse_http_del_header, 0 },
2476 { "del-map", parse_http_set_map, 1 },
Christopher Faulete0fca292020-01-13 21:49:03 +01002477 { "deny", parse_http_deny, 0 },
Christopher Faulet81e20172019-12-12 16:40:30 +01002478 { "disable-l7-retry", parse_http_req_disable_l7_retry, 0 },
2479 { "early-hint", parse_http_set_header, 0 },
2480 { "redirect", parse_http_redirect, 0 },
2481 { "reject", parse_http_action_reject, 0 },
2482 { "replace-header", parse_http_replace_header, 0 },
2483 { "replace-path", parse_replace_uri, 0 },
2484 { "replace-uri", parse_replace_uri, 0 },
2485 { "replace-value", parse_http_replace_header, 0 },
Christopher Faulet24231ab2020-01-24 17:44:23 +01002486 { "return", parse_http_return, 0 },
Christopher Faulet81e20172019-12-12 16:40:30 +01002487 { "set-header", parse_http_set_header, 0 },
2488 { "set-log-level", parse_http_set_log_level, 0 },
2489 { "set-map", parse_http_set_map, 1 },
2490 { "set-method", parse_set_req_line, 0 },
2491 { "set-mark", parse_http_set_mark, 0 },
2492 { "set-nice", parse_http_set_nice, 0 },
2493 { "set-path", parse_set_req_line, 0 },
2494 { "set-query", parse_set_req_line, 0 },
2495 { "set-tos", parse_http_set_tos, 0 },
2496 { "set-uri", parse_set_req_line, 0 },
Christopher Faulet46f95542019-12-20 10:07:22 +01002497 { "strict-mode", parse_http_strict_mode, 0 },
Christopher Faulete0fca292020-01-13 21:49:03 +01002498 { "tarpit", parse_http_deny, 0 },
Christopher Faulet81e20172019-12-12 16:40:30 +01002499 { "track-sc", parse_http_track_sc, 1 },
Willy Tarreau79e57332018-10-02 16:01:16 +02002500 { NULL, NULL }
2501 }
2502};
2503
Willy Tarreau0108d902018-11-25 19:14:37 +01002504INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_actions);
2505
Willy Tarreau79e57332018-10-02 16:01:16 +02002506static struct action_kw_list http_res_actions = {
2507 .kw = {
Christopher Faulet81e20172019-12-12 16:40:30 +01002508 { "add-acl", parse_http_set_map, 1 },
2509 { "add-header", parse_http_set_header, 0 },
2510 { "allow", parse_http_allow, 0 },
2511 { "capture", parse_http_res_capture, 0 },
2512 { "del-acl", parse_http_set_map, 1 },
2513 { "del-header", parse_http_del_header, 0 },
2514 { "del-map", parse_http_set_map, 1 },
Christopher Faulete0fca292020-01-13 21:49:03 +01002515 { "deny", parse_http_deny, 0 },
Christopher Faulet81e20172019-12-12 16:40:30 +01002516 { "redirect", parse_http_redirect, 0 },
2517 { "replace-header", parse_http_replace_header, 0 },
2518 { "replace-value", parse_http_replace_header, 0 },
Christopher Faulet24231ab2020-01-24 17:44:23 +01002519 { "return", parse_http_return, 0 },
Christopher Faulet81e20172019-12-12 16:40:30 +01002520 { "set-header", parse_http_set_header, 0 },
2521 { "set-log-level", parse_http_set_log_level, 0 },
2522 { "set-map", parse_http_set_map, 1 },
2523 { "set-mark", parse_http_set_mark, 0 },
2524 { "set-nice", parse_http_set_nice, 0 },
2525 { "set-status", parse_http_set_status, 0 },
2526 { "set-tos", parse_http_set_tos, 0 },
Christopher Faulet46f95542019-12-20 10:07:22 +01002527 { "strict-mode", parse_http_strict_mode, 0 },
Christopher Faulet81e20172019-12-12 16:40:30 +01002528 { "track-sc", parse_http_track_sc, 1 },
Willy Tarreau79e57332018-10-02 16:01:16 +02002529 { NULL, NULL }
2530 }
2531};
2532
Willy Tarreau0108d902018-11-25 19:14:37 +01002533INITCALL1(STG_REGISTER, http_res_keywords_register, &http_res_actions);
Willy Tarreau79e57332018-10-02 16:01:16 +02002534
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002535static struct action_kw_list http_after_res_actions = {
2536 .kw = {
2537 { "add-header", parse_http_set_header, 0 },
2538 { "allow", parse_http_allow, 0 },
2539 { "del-header", parse_http_del_header, 0 },
2540 { "replace-header", parse_http_replace_header, 0 },
2541 { "replace-value", parse_http_replace_header, 0 },
2542 { "set-header", parse_http_set_header, 0 },
2543 { "set-status", parse_http_set_status, 0 },
2544 { "strict-mode", parse_http_strict_mode, 0 },
2545 { NULL, NULL }
2546 }
2547};
2548
2549INITCALL1(STG_REGISTER, http_after_res_keywords_register, &http_after_res_actions);
2550
Willy Tarreau79e57332018-10-02 16:01:16 +02002551/*
2552 * Local variables:
2553 * c-indent-level: 8
2554 * c-basic-offset: 8
2555 * End:
2556 */