blob: cf0d8bf84efec4bff53ded95705437004a6bc41d [file] [log] [blame]
Willy Tarreau79e57332018-10-02 16:01:16 +02001/*
2 * HTTP actions
3 *
4 * Copyright 2000-2018 Willy Tarreau <w@1wt.eu>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <sys/types.h>
14
15#include <ctype.h>
16#include <string.h>
17#include <time.h>
18
Christopher Faulet81e20172019-12-12 16:40:30 +010019#include <common/cfgparse.h>
Willy Tarreau79e57332018-10-02 16:01:16 +020020#include <common/chunk.h>
21#include <common/compat.h>
22#include <common/config.h>
23#include <common/debug.h>
24#include <common/http.h>
Willy Tarreau0108d902018-11-25 19:14:37 +010025#include <common/initcall.h>
Willy Tarreau79e57332018-10-02 16:01:16 +020026#include <common/memory.h>
27#include <common/standard.h>
28#include <common/version.h>
29
30#include <types/capture.h>
31#include <types/global.h>
32
33#include <proto/acl.h>
34#include <proto/arg.h>
Christopher Faulet81e20172019-12-12 16:40:30 +010035#include <proto/action.h>
Willy Tarreau61c112a2018-10-02 16:43:32 +020036#include <proto/http_rules.h>
Willy Tarreau33810222019-06-12 17:44:02 +020037#include <proto/http_htx.h>
Willy Tarreau79e57332018-10-02 16:01:16 +020038#include <proto/log.h>
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020039#include <proto/http_ana.h>
Willy Tarreau0f9cd7b2019-01-31 19:02:43 +010040#include <proto/stream_interface.h>
Willy Tarreau79e57332018-10-02 16:01:16 +020041
42
43/* This function executes one of the set-{method,path,query,uri} actions. It
44 * builds a string in the trash from the specified format string. It finds
45 * the action to be performed in <http.action>, previously filled by function
46 * parse_set_req_line(). The replacement action is excuted by the function
Christopher Faulete00d06c2019-12-16 17:18:42 +010047 * http_action_set_req_line(). On success, it returns ACT_RET_CONT. If an error
48 * occurs while soft rewrites are enabled, the action is canceled, but the rule
49 * processing continue. Otherwsize ACT_RET_ERR is returned.
Willy Tarreau79e57332018-10-02 16:01:16 +020050 */
51static enum act_return http_action_set_req_line(struct act_rule *rule, struct proxy *px,
52 struct session *sess, struct stream *s, int flags)
53{
54 struct buffer *replace;
Christopher Faulet13403762019-12-13 09:01:57 +010055 enum act_return ret = ACT_RET_CONT;
Willy Tarreau79e57332018-10-02 16:01:16 +020056
57 replace = alloc_trash_chunk();
58 if (!replace)
Christopher Faulete00d06c2019-12-16 17:18:42 +010059 goto fail_alloc;
Willy Tarreau79e57332018-10-02 16:01:16 +020060
61 /* If we have to create a query string, prepare a '?'. */
62 if (rule->arg.http.action == 2)
63 replace->area[replace->data++] = '?';
64 replace->data += build_logline(s, replace->area + replace->data,
65 replace->size - replace->data,
66 &rule->arg.http.logfmt);
67
Christopher Faulete00d06c2019-12-16 17:18:42 +010068 if (http_req_replace_stline(rule->arg.http.action, replace->area,
69 replace->data, px, s) == -1)
70 goto fail_rewrite;
Willy Tarreau79e57332018-10-02 16:01:16 +020071
Christopher Faulete00d06c2019-12-16 17:18:42 +010072 leave:
Willy Tarreau79e57332018-10-02 16:01:16 +020073 free_trash_chunk(replace);
74 return ret;
Christopher Faulete00d06c2019-12-16 17:18:42 +010075
76 fail_alloc:
77 if (!(s->flags & SF_ERR_MASK))
78 s->flags |= SF_ERR_RESOURCE;
79 ret = ACT_RET_ERR;
80 goto leave;
81
82 fail_rewrite:
83 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_rewrites, 1);
84 if (s->flags & SF_BE_ASSIGNED)
85 _HA_ATOMIC_ADD(&s->be->be_counters.failed_rewrites, 1);
86 if (sess->listener->counters)
87 _HA_ATOMIC_ADD(&sess->listener->counters->failed_rewrites, 1);
88 if (objt_server(s->target))
89 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_rewrites, 1);
90
91 if (!(s->txn->req.flags & HTTP_MSGF_SOFT_RW))
92 ret = ACT_RET_ERR;
93 goto leave;
Willy Tarreau79e57332018-10-02 16:01:16 +020094}
95
96/* parse an http-request action among :
97 * set-method
98 * set-path
99 * set-query
100 * set-uri
101 *
102 * All of them accept a single argument of type string representing a log-format.
103 * The resulting rule makes use of arg->act.p[0..1] to store the log-format list
104 * head, and p[2] to store the action as an int (0=method, 1=path, 2=query, 3=uri).
105 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
106 */
107static enum act_parse_ret parse_set_req_line(const char **args, int *orig_arg, struct proxy *px,
108 struct act_rule *rule, char **err)
109{
110 int cur_arg = *orig_arg;
111
112 rule->action = ACT_CUSTOM;
113
114 switch (args[0][4]) {
115 case 'm' :
116 rule->arg.http.action = 0;
117 rule->action_ptr = http_action_set_req_line;
118 break;
119 case 'p' :
120 rule->arg.http.action = 1;
121 rule->action_ptr = http_action_set_req_line;
122 break;
123 case 'q' :
124 rule->arg.http.action = 2;
125 rule->action_ptr = http_action_set_req_line;
126 break;
127 case 'u' :
128 rule->arg.http.action = 3;
129 rule->action_ptr = http_action_set_req_line;
130 break;
131 default:
132 memprintf(err, "internal error: unhandled action '%s'", args[0]);
133 return ACT_RET_PRS_ERR;
134 }
135
136 if (!*args[cur_arg] ||
137 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
138 memprintf(err, "expects exactly 1 argument <format>");
139 return ACT_RET_PRS_ERR;
140 }
141
142 LIST_INIT(&rule->arg.http.logfmt);
143 px->conf.args.ctx = ARGC_HRQ;
144 if (!parse_logformat_string(args[cur_arg], px, &rule->arg.http.logfmt, LOG_OPT_HTTP,
145 (px->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, err)) {
146 return ACT_RET_PRS_ERR;
147 }
148
149 (*orig_arg)++;
150 return ACT_RET_PRS_OK;
151}
152
Willy Tarreau33810222019-06-12 17:44:02 +0200153/* This function executes a replace-uri action. It finds its arguments in
154 * <rule>.arg.act.p[]. It builds a string in the trash from the format string
155 * previously filled by function parse_replace_uri() and will execute the regex
156 * in p[1] to replace the URI. It uses the format string present in act.p[2..3].
Willy Tarreau262c3f12019-12-17 06:52:51 +0100157 * The component to act on (path/uri) is taken from act.p[0] which contains 1
158 * for the path or 3 for the URI (values used by http_req_replace_stline()).
Christopher Faulete00d06c2019-12-16 17:18:42 +0100159 * On success, it returns ACT_RET_CONT. If an error occurs while soft rewrites
160 * are enabled, the action is canceled, but the rule processing continue.
161 * Otherwsize ACT_RET_ERR is returned.
Willy Tarreau33810222019-06-12 17:44:02 +0200162 */
163static enum act_return http_action_replace_uri(struct act_rule *rule, struct proxy *px,
164 struct session *sess, struct stream *s, int flags)
165{
Christopher Faulet13403762019-12-13 09:01:57 +0100166 enum act_return ret = ACT_RET_CONT;
Willy Tarreau33810222019-06-12 17:44:02 +0200167 struct buffer *replace, *output;
168 struct ist uri;
169 int len;
170
171 replace = alloc_trash_chunk();
172 output = alloc_trash_chunk();
173 if (!replace || !output)
Christopher Faulete00d06c2019-12-16 17:18:42 +0100174 goto fail_alloc;
Christopher Faulet12c28b62019-07-15 16:30:24 +0200175 uri = htx_sl_req_uri(http_get_stline(htxbuf(&s->req.buf)));
Willy Tarreau262c3f12019-12-17 06:52:51 +0100176
177 if (rule->arg.act.p[0] == (void *)1)
178 uri = http_get_path(uri); // replace path
179
Willy Tarreau33810222019-06-12 17:44:02 +0200180 if (!regex_exec_match2(rule->arg.act.p[1], uri.ptr, uri.len, MAX_MATCH, pmatch, 0))
181 goto leave;
182
183 replace->data = build_logline(s, replace->area, replace->size, (struct list *)&rule->arg.act.p[2]);
184
185 /* note: uri.ptr doesn't need to be zero-terminated because it will
186 * only be used to pick pmatch references.
187 */
188 len = exp_replace(output->area, output->size, uri.ptr, replace->area, pmatch);
189 if (len == -1)
Christopher Faulete00d06c2019-12-16 17:18:42 +0100190 goto fail_rewrite;
Willy Tarreau33810222019-06-12 17:44:02 +0200191
Christopher Faulete00d06c2019-12-16 17:18:42 +0100192 if (http_req_replace_stline((long)rule->arg.act.p[0], output->area, len, px, s) == -1)
193 goto fail_rewrite;
Willy Tarreau33810222019-06-12 17:44:02 +0200194
Christopher Faulete00d06c2019-12-16 17:18:42 +0100195 leave:
Willy Tarreau33810222019-06-12 17:44:02 +0200196 free_trash_chunk(output);
197 free_trash_chunk(replace);
198 return ret;
Christopher Faulete00d06c2019-12-16 17:18:42 +0100199
200 fail_alloc:
201 if (!(s->flags & SF_ERR_MASK))
202 s->flags |= SF_ERR_RESOURCE;
203 ret = ACT_RET_ERR;
204 goto leave;
205
206 fail_rewrite:
207 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_rewrites, 1);
208 if (s->flags & SF_BE_ASSIGNED)
209 _HA_ATOMIC_ADD(&s->be->be_counters.failed_rewrites, 1);
210 if (sess->listener->counters)
211 _HA_ATOMIC_ADD(&sess->listener->counters->failed_rewrites, 1);
212 if (objt_server(s->target))
213 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_rewrites, 1);
214
215 if (!(s->txn->req.flags & HTTP_MSGF_SOFT_RW))
216 ret = ACT_RET_ERR;
217 goto leave;
Willy Tarreau33810222019-06-12 17:44:02 +0200218}
219
Willy Tarreau262c3f12019-12-17 06:52:51 +0100220/* parse a "replace-uri" or "replace-path" http-request action.
Willy Tarreau33810222019-06-12 17:44:02 +0200221 * This action takes 2 arguments (a regex and a replacement format string).
Willy Tarreau262c3f12019-12-17 06:52:51 +0100222 * The resulting rule makes use of arg->act.p[0] to store the action (1/3 for now),
Willy Tarreau33810222019-06-12 17:44:02 +0200223 * p[1] to store the compiled regex, and arg->act.p[2..3] to store the log-format
224 * list head. It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
225 */
226static enum act_parse_ret parse_replace_uri(const char **args, int *orig_arg, struct proxy *px,
227 struct act_rule *rule, char **err)
228{
229 int cur_arg = *orig_arg;
230 char *error = NULL;
231
232 rule->action = ACT_CUSTOM;
Willy Tarreau262c3f12019-12-17 06:52:51 +0100233 if (strcmp(args[cur_arg-1], "replace-path") == 0)
234 rule->arg.act.p[0] = (void *)1; // replace-path
235 else
236 rule->arg.act.p[0] = (void *)3; // replace-uri
237
Willy Tarreau33810222019-06-12 17:44:02 +0200238 rule->action_ptr = http_action_replace_uri;
239
240 if (!*args[cur_arg] || !*args[cur_arg+1] ||
241 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
242 memprintf(err, "expects exactly 2 arguments <match-regex> and <replace-format>");
243 return ACT_RET_PRS_ERR;
244 }
245
246 if (!(rule->arg.act.p[1] = regex_comp(args[cur_arg], 1, 1, &error))) {
247 memprintf(err, "failed to parse the regex : %s", error);
248 free(error);
249 return ACT_RET_PRS_ERR;
250 }
251
252 LIST_INIT((struct list *)&rule->arg.act.p[2]);
253 px->conf.args.ctx = ARGC_HRQ;
254 if (!parse_logformat_string(args[cur_arg + 1], px, (struct list *)&rule->arg.act.p[2], LOG_OPT_HTTP,
255 (px->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, err)) {
256 return ACT_RET_PRS_ERR;
257 }
258
259 (*orig_arg) += 2;
260 return ACT_RET_PRS_OK;
261}
262
Willy Tarreau79e57332018-10-02 16:01:16 +0200263/* This function is just a compliant action wrapper for "set-status". */
264static enum act_return action_http_set_status(struct act_rule *rule, struct proxy *px,
265 struct session *sess, struct stream *s, int flags)
266{
Christopher Faulete00d06c2019-12-16 17:18:42 +0100267 if (http_res_set_status(rule->arg.status.code, rule->arg.status.reason, s) == -1) {
268 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_rewrites, 1);
269 if (s->flags & SF_BE_ASSIGNED)
270 _HA_ATOMIC_ADD(&s->be->be_counters.failed_rewrites, 1);
271 if (sess->listener->counters)
272 _HA_ATOMIC_ADD(&sess->listener->counters->failed_rewrites, 1);
273 if (objt_server(s->target))
274 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_rewrites, 1);
275
276 if (!(s->txn->req.flags & HTTP_MSGF_SOFT_RW))
277 return ACT_RET_ERR;
278 }
279
Willy Tarreau79e57332018-10-02 16:01:16 +0200280 return ACT_RET_CONT;
281}
282
283/* parse set-status action:
284 * This action accepts a single argument of type int representing
285 * an http status code. It returns ACT_RET_PRS_OK on success,
286 * ACT_RET_PRS_ERR on error.
287 */
288static enum act_parse_ret parse_http_set_status(const char **args, int *orig_arg, struct proxy *px,
289 struct act_rule *rule, char **err)
290{
291 char *error;
292
293 rule->action = ACT_CUSTOM;
294 rule->action_ptr = action_http_set_status;
295
296 /* Check if an argument is available */
297 if (!*args[*orig_arg]) {
298 memprintf(err, "expects 1 argument: <status>; or 3 arguments: <status> reason <fmt>");
299 return ACT_RET_PRS_ERR;
300 }
301
302 /* convert status code as integer */
303 rule->arg.status.code = strtol(args[*orig_arg], &error, 10);
304 if (*error != '\0' || rule->arg.status.code < 100 || rule->arg.status.code > 999) {
305 memprintf(err, "expects an integer status code between 100 and 999");
306 return ACT_RET_PRS_ERR;
307 }
308
309 (*orig_arg)++;
310
311 /* set custom reason string */
312 rule->arg.status.reason = NULL; // If null, we use the default reason for the status code.
313 if (*args[*orig_arg] && strcmp(args[*orig_arg], "reason") == 0 &&
314 (*args[*orig_arg + 1] && strcmp(args[*orig_arg + 1], "if") != 0 && strcmp(args[*orig_arg + 1], "unless") != 0)) {
315 (*orig_arg)++;
316 rule->arg.status.reason = strdup(args[*orig_arg]);
317 (*orig_arg)++;
318 }
319
320 return ACT_RET_PRS_OK;
321}
322
323/* This function executes the "reject" HTTP action. It clears the request and
324 * response buffer without sending any response. It can be useful as an HTTP
325 * alternative to the silent-drop action to defend against DoS attacks, and may
326 * also be used with HTTP/2 to close a connection instead of just a stream.
327 * The txn status is unchanged, indicating no response was sent. The termination
Christopher Faulet8f1aa772019-07-04 11:27:15 +0200328 * flags will indicate "PR". It always returns ACT_RET_DONE.
Willy Tarreau79e57332018-10-02 16:01:16 +0200329 */
330static enum act_return http_action_reject(struct act_rule *rule, struct proxy *px,
331 struct session *sess, struct stream *s, int flags)
332{
Willy Tarreau0f9cd7b2019-01-31 19:02:43 +0100333 si_must_kill_conn(chn_prod(&s->req));
Willy Tarreau79e57332018-10-02 16:01:16 +0200334 channel_abort(&s->req);
335 channel_abort(&s->res);
336 s->req.analysers = 0;
337 s->res.analysers = 0;
338
Olivier Houcharda798bf52019-03-08 18:52:00 +0100339 _HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
340 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Willy Tarreau79e57332018-10-02 16:01:16 +0200341 if (sess->listener && sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100342 _HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Willy Tarreau79e57332018-10-02 16:01:16 +0200343
344 if (!(s->flags & SF_ERR_MASK))
345 s->flags |= SF_ERR_PRXCOND;
346 if (!(s->flags & SF_FINST_MASK))
347 s->flags |= SF_FINST_R;
348
Christopher Faulet8f1aa772019-07-04 11:27:15 +0200349 return ACT_RET_DONE;
Willy Tarreau79e57332018-10-02 16:01:16 +0200350}
351
352/* parse the "reject" action:
353 * This action takes no argument and returns ACT_RET_PRS_OK on success,
354 * ACT_RET_PRS_ERR on error.
355 */
356static enum act_parse_ret parse_http_action_reject(const char **args, int *orig_arg, struct proxy *px,
357 struct act_rule *rule, char **err)
358{
359 rule->action = ACT_CUSTOM;
360 rule->action_ptr = http_action_reject;
361 return ACT_RET_PRS_OK;
362}
363
Olivier Houchard602bf7d2019-05-10 13:59:15 +0200364/* This function executes the "disable-l7-retry" HTTP action.
365 * It disables L7 retries (all retry except for a connection failure). This
366 * can be useful for example to avoid retrying on POST requests.
367 * It just removes the L7 retry flag on the stream_interface, and always
368 * return ACT_RET_CONT;
369 */
370static enum act_return http_req_disable_l7_retry(struct act_rule *rule, struct proxy *px,
371 struct session *sess, struct stream *s, int flags)
372{
373 struct stream_interface *si = &s->si[1];
374
375 /* In theory, the SI_FL_L7_RETRY flags isn't set at this point, but
376 * let's be future-proof and remove it anyway.
377 */
378 si->flags &= ~SI_FL_L7_RETRY;
379 si->flags |= SI_FL_D_L7_RETRY;
380 return ACT_RET_CONT;
381}
382
383/* parse the "disable-l7-retry" action:
384 * This action takes no argument and returns ACT_RET_PRS_OK on success,
385 * ACT_RET_PRS_ERR on error.
386 */
387static enum act_parse_ret parse_http_req_disable_l7_retry(const char **args,
388 int *orig_args, struct proxy *px,
389 struct act_rule *rule, char **err)
390{
391 rule->action = ACT_CUSTOM;
392 rule->action_ptr = http_req_disable_l7_retry;
393 return ACT_RET_PRS_OK;
394}
395
Willy Tarreau79e57332018-10-02 16:01:16 +0200396/* This function executes the "capture" action. It executes a fetch expression,
397 * turns the result into a string and puts it in a capture slot. It always
398 * returns 1. If an error occurs the action is cancelled, but the rule
399 * processing continues.
400 */
401static enum act_return http_action_req_capture(struct act_rule *rule, struct proxy *px,
402 struct session *sess, struct stream *s, int flags)
403{
404 struct sample *key;
405 struct cap_hdr *h = rule->arg.cap.hdr;
406 char **cap = s->req_cap;
407 int len;
408
409 key = sample_fetch_as_type(s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.cap.expr, SMP_T_STR);
410 if (!key)
411 return ACT_RET_CONT;
412
413 if (cap[h->index] == NULL)
414 cap[h->index] = pool_alloc(h->pool);
415
416 if (cap[h->index] == NULL) /* no more capture memory */
417 return ACT_RET_CONT;
418
419 len = key->data.u.str.data;
420 if (len > h->len)
421 len = h->len;
422
423 memcpy(cap[h->index], key->data.u.str.area, len);
424 cap[h->index][len] = 0;
425 return ACT_RET_CONT;
426}
427
428/* This function executes the "capture" action and store the result in a
429 * capture slot if exists. It executes a fetch expression, turns the result
430 * into a string and puts it in a capture slot. It always returns 1. If an
431 * error occurs the action is cancelled, but the rule processing continues.
432 */
433static enum act_return http_action_req_capture_by_id(struct act_rule *rule, struct proxy *px,
434 struct session *sess, struct stream *s, int flags)
435{
436 struct sample *key;
437 struct cap_hdr *h;
438 char **cap = s->req_cap;
439 struct proxy *fe = strm_fe(s);
440 int len;
441 int i;
442
443 /* Look for the original configuration. */
444 for (h = fe->req_cap, i = fe->nb_req_cap - 1;
445 h != NULL && i != rule->arg.capid.idx ;
446 i--, h = h->next);
447 if (!h)
448 return ACT_RET_CONT;
449
450 key = sample_fetch_as_type(s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.capid.expr, SMP_T_STR);
451 if (!key)
452 return ACT_RET_CONT;
453
454 if (cap[h->index] == NULL)
455 cap[h->index] = pool_alloc(h->pool);
456
457 if (cap[h->index] == NULL) /* no more capture memory */
458 return ACT_RET_CONT;
459
460 len = key->data.u.str.data;
461 if (len > h->len)
462 len = h->len;
463
464 memcpy(cap[h->index], key->data.u.str.area, len);
465 cap[h->index][len] = 0;
466 return ACT_RET_CONT;
467}
468
469/* Check an "http-request capture" action.
470 *
471 * The function returns 1 in success case, otherwise, it returns 0 and err is
472 * filled.
473 */
474static int check_http_req_capture(struct act_rule *rule, struct proxy *px, char **err)
475{
476 if (rule->action_ptr != http_action_req_capture_by_id)
477 return 1;
478
479 if (rule->arg.capid.idx >= px->nb_req_cap) {
480 memprintf(err, "unable to find capture id '%d' referenced by http-request capture rule",
481 rule->arg.capid.idx);
482 return 0;
483 }
484
485 return 1;
486}
487
488/* parse an "http-request capture" action. It takes a single argument which is
489 * a sample fetch expression. It stores the expression into arg->act.p[0] and
490 * the allocated hdr_cap struct or the preallocated "id" into arg->act.p[1].
491 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
492 */
493static enum act_parse_ret parse_http_req_capture(const char **args, int *orig_arg, struct proxy *px,
494 struct act_rule *rule, char **err)
495{
496 struct sample_expr *expr;
497 struct cap_hdr *hdr;
498 int cur_arg;
499 int len = 0;
500
501 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
502 if (strcmp(args[cur_arg], "if") == 0 ||
503 strcmp(args[cur_arg], "unless") == 0)
504 break;
505
506 if (cur_arg < *orig_arg + 3) {
507 memprintf(err, "expects <expression> [ 'len' <length> | id <idx> ]");
508 return ACT_RET_PRS_ERR;
509 }
510
511 cur_arg = *orig_arg;
512 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
513 if (!expr)
514 return ACT_RET_PRS_ERR;
515
516 if (!(expr->fetch->val & SMP_VAL_FE_HRQ_HDR)) {
517 memprintf(err,
518 "fetch method '%s' extracts information from '%s', none of which is available here",
519 args[cur_arg-1], sample_src_names(expr->fetch->use));
520 free(expr);
521 return ACT_RET_PRS_ERR;
522 }
523
524 if (!args[cur_arg] || !*args[cur_arg]) {
525 memprintf(err, "expects 'len or 'id'");
526 free(expr);
527 return ACT_RET_PRS_ERR;
528 }
529
530 if (strcmp(args[cur_arg], "len") == 0) {
531 cur_arg++;
532
533 if (!(px->cap & PR_CAP_FE)) {
534 memprintf(err, "proxy '%s' has no frontend capability", px->id);
535 return ACT_RET_PRS_ERR;
536 }
537
538 px->conf.args.ctx = ARGC_CAP;
539
540 if (!args[cur_arg]) {
541 memprintf(err, "missing length value");
542 free(expr);
543 return ACT_RET_PRS_ERR;
544 }
545 /* we copy the table name for now, it will be resolved later */
546 len = atoi(args[cur_arg]);
547 if (len <= 0) {
548 memprintf(err, "length must be > 0");
549 free(expr);
550 return ACT_RET_PRS_ERR;
551 }
552 cur_arg++;
553
Willy Tarreau79e57332018-10-02 16:01:16 +0200554 hdr = calloc(1, sizeof(*hdr));
555 hdr->next = px->req_cap;
556 hdr->name = NULL; /* not a header capture */
557 hdr->namelen = 0;
558 hdr->len = len;
559 hdr->pool = create_pool("caphdr", hdr->len + 1, MEM_F_SHARED);
560 hdr->index = px->nb_req_cap++;
561
562 px->req_cap = hdr;
563 px->to_log |= LW_REQHDR;
564
565 rule->action = ACT_CUSTOM;
566 rule->action_ptr = http_action_req_capture;
567 rule->arg.cap.expr = expr;
568 rule->arg.cap.hdr = hdr;
569 }
570
571 else if (strcmp(args[cur_arg], "id") == 0) {
572 int id;
573 char *error;
574
575 cur_arg++;
576
577 if (!args[cur_arg]) {
578 memprintf(err, "missing id value");
579 free(expr);
580 return ACT_RET_PRS_ERR;
581 }
582
583 id = strtol(args[cur_arg], &error, 10);
584 if (*error != '\0') {
585 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
586 free(expr);
587 return ACT_RET_PRS_ERR;
588 }
589 cur_arg++;
590
591 px->conf.args.ctx = ARGC_CAP;
592
593 rule->action = ACT_CUSTOM;
594 rule->action_ptr = http_action_req_capture_by_id;
595 rule->check_ptr = check_http_req_capture;
596 rule->arg.capid.expr = expr;
597 rule->arg.capid.idx = id;
598 }
599
600 else {
601 memprintf(err, "expects 'len' or 'id', found '%s'", args[cur_arg]);
602 free(expr);
603 return ACT_RET_PRS_ERR;
604 }
605
606 *orig_arg = cur_arg;
607 return ACT_RET_PRS_OK;
608}
609
610/* This function executes the "capture" action and store the result in a
611 * capture slot if exists. It executes a fetch expression, turns the result
612 * into a string and puts it in a capture slot. It always returns 1. If an
613 * error occurs the action is cancelled, but the rule processing continues.
614 */
615static enum act_return http_action_res_capture_by_id(struct act_rule *rule, struct proxy *px,
616 struct session *sess, struct stream *s, int flags)
617{
618 struct sample *key;
619 struct cap_hdr *h;
620 char **cap = s->res_cap;
621 struct proxy *fe = strm_fe(s);
622 int len;
623 int i;
624
625 /* Look for the original configuration. */
626 for (h = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
627 h != NULL && i != rule->arg.capid.idx ;
628 i--, h = h->next);
629 if (!h)
630 return ACT_RET_CONT;
631
632 key = sample_fetch_as_type(s->be, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL, rule->arg.capid.expr, SMP_T_STR);
633 if (!key)
634 return ACT_RET_CONT;
635
636 if (cap[h->index] == NULL)
637 cap[h->index] = pool_alloc(h->pool);
638
639 if (cap[h->index] == NULL) /* no more capture memory */
640 return ACT_RET_CONT;
641
642 len = key->data.u.str.data;
643 if (len > h->len)
644 len = h->len;
645
646 memcpy(cap[h->index], key->data.u.str.area, len);
647 cap[h->index][len] = 0;
648 return ACT_RET_CONT;
649}
650
651/* Check an "http-response capture" action.
652 *
653 * The function returns 1 in success case, otherwise, it returns 0 and err is
654 * filled.
655 */
656static int check_http_res_capture(struct act_rule *rule, struct proxy *px, char **err)
657{
658 if (rule->action_ptr != http_action_res_capture_by_id)
659 return 1;
660
661 if (rule->arg.capid.idx >= px->nb_rsp_cap) {
662 memprintf(err, "unable to find capture id '%d' referenced by http-response capture rule",
663 rule->arg.capid.idx);
664 return 0;
665 }
666
667 return 1;
668}
669
670/* parse an "http-response capture" action. It takes a single argument which is
671 * a sample fetch expression. It stores the expression into arg->act.p[0] and
672 * the allocated hdr_cap struct od the preallocated id into arg->act.p[1].
673 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
674 */
675static enum act_parse_ret parse_http_res_capture(const char **args, int *orig_arg, struct proxy *px,
676 struct act_rule *rule, char **err)
677{
678 struct sample_expr *expr;
679 int cur_arg;
680 int id;
681 char *error;
682
683 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
684 if (strcmp(args[cur_arg], "if") == 0 ||
685 strcmp(args[cur_arg], "unless") == 0)
686 break;
687
688 if (cur_arg < *orig_arg + 3) {
689 memprintf(err, "expects <expression> id <idx>");
690 return ACT_RET_PRS_ERR;
691 }
692
693 cur_arg = *orig_arg;
694 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
695 if (!expr)
696 return ACT_RET_PRS_ERR;
697
698 if (!(expr->fetch->val & SMP_VAL_FE_HRS_HDR)) {
699 memprintf(err,
700 "fetch method '%s' extracts information from '%s', none of which is available here",
701 args[cur_arg-1], sample_src_names(expr->fetch->use));
702 free(expr);
703 return ACT_RET_PRS_ERR;
704 }
705
706 if (!args[cur_arg] || !*args[cur_arg]) {
707 memprintf(err, "expects 'id'");
708 free(expr);
709 return ACT_RET_PRS_ERR;
710 }
711
712 if (strcmp(args[cur_arg], "id") != 0) {
713 memprintf(err, "expects 'id', found '%s'", args[cur_arg]);
714 free(expr);
715 return ACT_RET_PRS_ERR;
716 }
717
718 cur_arg++;
719
720 if (!args[cur_arg]) {
721 memprintf(err, "missing id value");
722 free(expr);
723 return ACT_RET_PRS_ERR;
724 }
725
726 id = strtol(args[cur_arg], &error, 10);
727 if (*error != '\0') {
728 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
729 free(expr);
730 return ACT_RET_PRS_ERR;
731 }
732 cur_arg++;
733
734 px->conf.args.ctx = ARGC_CAP;
735
736 rule->action = ACT_CUSTOM;
737 rule->action_ptr = http_action_res_capture_by_id;
738 rule->check_ptr = check_http_res_capture;
739 rule->arg.capid.expr = expr;
740 rule->arg.capid.idx = id;
741
742 *orig_arg = cur_arg;
743 return ACT_RET_PRS_OK;
744}
745
Christopher Faulet81e20172019-12-12 16:40:30 +0100746/* Parse a "allow" action for a request or a response rule. It takes no argument. It
747 * returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
748 */
749static enum act_parse_ret parse_http_allow(const char **args, int *orig_arg, struct proxy *px,
750 struct act_rule *rule, char **err)
751{
752 rule->action = ACT_ACTION_ALLOW;
753 return ACT_RET_PRS_OK;
754}
755
756/* Parse "deny" or "tarpit" actions for a request rule. It may take 2 optional arguments
757 * to define the status code. It returns ACT_RET_PRS_OK on success,
758 * ACT_RET_PRS_ERR on error.
759 */
760static enum act_parse_ret parse_http_req_deny(const char **args, int *orig_arg, struct proxy *px,
761 struct act_rule *rule, char **err)
762{
763 int code, hc, cur_arg;
764
765 cur_arg = *orig_arg;
766 if (!strcmp(args[cur_arg-1], "tarpit")) {
767 rule->action = ACT_HTTP_REQ_TARPIT;
768 rule->deny_status = HTTP_ERR_500;
769 }
770 else {
771 rule->action = ACT_ACTION_DENY;
772 rule->deny_status = HTTP_ERR_403;
773 }
774
775 if (strcmp(args[cur_arg], "deny_status") == 0) {
776 cur_arg++;
777 if (!*args[cur_arg]) {
778 memprintf(err, "missing status code.\n");
779 return ACT_RET_PRS_ERR;
780 }
781
782 code = atol(args[cur_arg]);
783 cur_arg++;
784 for (hc = 0; hc < HTTP_ERR_SIZE; hc++) {
785 if (http_err_codes[hc] == code) {
786 rule->deny_status = hc;
787 break;
788 }
789 }
790 if (hc >= HTTP_ERR_SIZE)
791 memprintf(err, "status code %d not handled, using default code %d",
792 code, http_err_codes[rule->deny_status]);
793 }
794
795 *orig_arg = cur_arg;
796 return ACT_RET_PRS_OK;
797}
798
799/* Parse a "deny" action for a response rule. It takes no argument. It returns
800 * ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
801 */
802static enum act_parse_ret parse_http_res_deny(const char **args, int *orig_arg, struct proxy *px,
803 struct act_rule *rule, char **err)
804{
805 rule->action = ACT_ACTION_DENY;
806 return ACT_RET_PRS_OK;
807}
808
809/* Parse a "auth" action. It may take 2 optional arguments to define a "realm"
810 * parameter. It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
811 */
812static enum act_parse_ret parse_http_auth(const char **args, int *orig_arg, struct proxy *px,
813 struct act_rule *rule, char **err)
814{
815 int cur_arg;
816
817 rule->action = ACT_HTTP_REQ_AUTH;
818
819 cur_arg = *orig_arg;
820 if (!strcmp(args[cur_arg], "realm")) {
821 cur_arg++;
822 if (!*args[cur_arg]) {
823 memprintf(err, "missing realm value.\n");
824 return ACT_RET_PRS_ERR;
825 }
826 rule->arg.auth.realm = strdup(args[cur_arg]);
827 cur_arg++;
828 }
829
830 *orig_arg = cur_arg;
831 return ACT_RET_PRS_OK;
832}
833
834/* Parse a "set-nice" action. It takes the nice value as argument. It returns
835 * ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
836 */
837static enum act_parse_ret parse_http_set_nice(const char **args, int *orig_arg, struct proxy *px,
838 struct act_rule *rule, char **err)
839{
840 int cur_arg;
841
842 rule->action = ACT_HTTP_SET_NICE;
843
844 cur_arg = *orig_arg;
845 if (!*args[cur_arg]) {
846 memprintf(err, "expects exactly 1 argument (integer value)");
847 return ACT_RET_PRS_ERR;
848 }
849 rule->arg.nice = atoi(args[cur_arg]);
850 if (rule->arg.nice < -1024)
851 rule->arg.nice = -1024;
852 else if (rule->arg.nice > 1024)
853 rule->arg.nice = 1024;
854
855 *orig_arg = cur_arg + 1;
856 return ACT_RET_PRS_OK;
857}
858
859/* Parse a "set-tos" action. It takes the TOS value as argument. It returns
860 * ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
861 */
862static enum act_parse_ret parse_http_set_tos(const char **args, int *orig_arg, struct proxy *px,
863 struct act_rule *rule, char **err)
864{
865#ifdef IP_TOS
866 char *endp;
867 int cur_arg;
868
869 rule->action = ACT_HTTP_SET_TOS;
870
871 cur_arg = *orig_arg;
872 if (!*args[cur_arg]) {
873 memprintf(err, "expects exactly 1 argument (integer/hex value)");
874 return ACT_RET_PRS_ERR;
875 }
876 rule->arg.tos = strtol(args[cur_arg], &endp, 0);
877 if (endp && *endp != '\0') {
878 memprintf(err, "invalid character starting at '%s' (integer/hex value expected)", endp);
879 return ACT_RET_PRS_ERR;
880 }
881
882 *orig_arg = cur_arg + 1;
883 return ACT_RET_PRS_OK;
884#else
885 memprintf(err, "not supported on this platform (IP_TOS undefined)");
886 return ACT_RET_PRS_ERR;
887#endif
888}
889
890/* Parse a "set-mark" action. It takes the MARK value as argument. It returns
891 * ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
892 */
893static enum act_parse_ret parse_http_set_mark(const char **args, int *orig_arg, struct proxy *px,
894 struct act_rule *rule, char **err)
895{
896#ifdef SO_MARK
897 char *endp;
898 int cur_arg;
899
900 rule->action = ACT_HTTP_SET_MARK;
901
902 cur_arg = *orig_arg;
903 if (!*args[cur_arg]) {
904 memprintf(err, "expects exactly 1 argument (integer/hex value)");
905 return ACT_RET_PRS_ERR;
906 }
907 rule->arg.mark = strtoul(args[cur_arg], &endp, 0);
908 if (endp && *endp != '\0') {
909 memprintf(err, "invalid character starting at '%s' (integer/hex value expected)", endp);
910 return ACT_RET_PRS_ERR;
911 }
912
913 *orig_arg = cur_arg + 1;
914 global.last_checks |= LSTCHK_NETADM;
915 return ACT_RET_PRS_OK;
916#else
917 memprintf(err, "not supported on this platform (SO_MARK undefined)");
918 return ACT_RET_PRS_ERR;
919#endif
920}
921
922/* Parse a "set-log-level" action. It takes the level value as argument. It
923 * returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
924 */
925static enum act_parse_ret parse_http_set_log_level(const char **args, int *orig_arg, struct proxy *px,
926 struct act_rule *rule, char **err)
927{
928 int cur_arg;
929
930 rule->action = ACT_HTTP_SET_LOGL;
931
932 cur_arg = *orig_arg;
933 if (!*args[cur_arg]) {
934 bad_log_level:
935 memprintf(err, "expects exactly 1 argument (log level name or 'silent')");
936 return ACT_RET_PRS_ERR;
937 }
938 if (strcmp(args[cur_arg], "silent") == 0)
939 rule->arg.loglevel = -1;
940 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
941 goto bad_log_level;
942
943 *orig_arg = cur_arg + 1;
944 return ACT_RET_PRS_OK;
945}
946
947/* Parse a "set-header", "add-header" or "early-hint" actions. It takes an
948 * header name and a log-format string as arguments. It returns ACT_RET_PRS_OK
949 * on success, ACT_RET_PRS_ERR on error.
950 *
951 * Note: same function is used for the request and the response. However
952 * "early-hint" rules are only supported for request rules.
953 */
954static enum act_parse_ret parse_http_set_header(const char **args, int *orig_arg, struct proxy *px,
955 struct act_rule *rule, char **err)
956{
957 char **hdr_name;
958 int *hdr_name_len;
959 struct list *fmt;
960 int cap, cur_arg;
961
962 rule->action = (*args[*orig_arg-1] == 'a' ? ACT_HTTP_ADD_HDR :
963 *args[*orig_arg-1] == 's' ? ACT_HTTP_SET_HDR : ACT_HTTP_EARLY_HINT);
964
965 cur_arg = *orig_arg;
966 if (!*args[cur_arg] || !*args[cur_arg+1]) {
967 memprintf(err, "expects exactly 2 arguments");
968 return ACT_RET_PRS_ERR;
969 }
970
971 hdr_name = (*args[cur_arg-1] == 'e' ? &rule->arg.early_hint.name : &rule->arg.hdr_add.name);
972 hdr_name_len = (*args[cur_arg-1] == 'e' ? &rule->arg.early_hint.name_len : &rule->arg.hdr_add.name_len);
973 fmt = (*args[cur_arg-1] == 'e' ? &rule->arg.early_hint.fmt : &rule->arg.hdr_add.fmt);
974
975 *hdr_name = strdup(args[cur_arg]);
976 *hdr_name_len = strlen(*hdr_name);
977 LIST_INIT(fmt);
978
979 if (rule->from == ACT_F_HTTP_REQ) {
980 px->conf.args.ctx = ARGC_HRQ;
981 cap = (px->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR;
982 }
983 else{
984 px->conf.args.ctx = ARGC_HRS;
985 cap = (px->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR;
986 }
987
988 cur_arg++;
989 if (!parse_logformat_string(args[cur_arg], px, fmt, LOG_OPT_HTTP, cap, err))
990 return ACT_RET_PRS_ERR;
991
992 free(px->conf.lfs_file);
993 px->conf.lfs_file = strdup(px->conf.args.file);
994 px->conf.lfs_line = px->conf.args.line;
995
996 *orig_arg = cur_arg + 1;
997 return ACT_RET_PRS_OK;
998}
999
1000/* Parse a "replace-header" or "replace-value" actions. It takes an header name,
1001 * a regex and replacement string as arguments. It returns ACT_RET_PRS_OK on
1002 * success, ACT_RET_PRS_ERR on error.
1003 */
1004static enum act_parse_ret parse_http_replace_header(const char **args, int *orig_arg, struct proxy *px,
1005 struct act_rule *rule, char **err)
1006{
1007 int cap, cur_arg;
1008
1009 rule->action = args[*orig_arg-1][8] == 'h' ? ACT_HTTP_REPLACE_HDR : ACT_HTTP_REPLACE_VAL;
1010
1011 cur_arg = *orig_arg;
1012 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2]) {
1013 memprintf(err, "expects exactly 3 arguments");
1014 return ACT_RET_PRS_ERR;
1015 }
1016
1017 rule->arg.hdr_add.name = strdup(args[cur_arg]);
1018 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
1019 LIST_INIT(&rule->arg.hdr_add.fmt);
1020
1021 cur_arg++;
1022 if (!(rule->arg.hdr_add.re = regex_comp(args[cur_arg], 1, 1, err)))
1023 return ACT_RET_PRS_ERR;
1024
1025 if (rule->from == ACT_F_HTTP_REQ) {
1026 px->conf.args.ctx = ARGC_HRQ;
1027 cap = (px->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR;
1028 }
1029 else{
1030 px->conf.args.ctx = ARGC_HRS;
1031 cap = (px->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR;
1032 }
1033
1034 cur_arg++;
1035 if (!parse_logformat_string(args[cur_arg], px, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP, cap, err))
1036 return ACT_RET_PRS_ERR;
1037
1038 free(px->conf.lfs_file);
1039 px->conf.lfs_file = strdup(px->conf.args.file);
1040 px->conf.lfs_line = px->conf.args.line;
1041
1042 *orig_arg = cur_arg + 1;
1043 return ACT_RET_PRS_OK;
1044}
1045
1046/* Parse a "del-header" action. It takes an header name as argument. It returns
1047 * ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
1048 */
1049static enum act_parse_ret parse_http_del_header(const char **args, int *orig_arg, struct proxy *px,
1050 struct act_rule *rule, char **err)
1051{
1052 int cur_arg;
1053
1054 rule->action = ACT_HTTP_DEL_HDR;
1055
1056 cur_arg = *orig_arg;
1057 if (!*args[cur_arg]) {
1058 memprintf(err, "expects exactly 1 arguments");
1059 return ACT_RET_PRS_ERR;
1060 }
1061
1062 rule->arg.hdr_add.name = strdup(args[cur_arg]);
1063 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
1064
1065 px->conf.args.ctx = (rule->from == ACT_F_HTTP_REQ ? ARGC_HRQ : ARGC_HRS);
1066
1067 *orig_arg = cur_arg + 1;
1068 return ACT_RET_PRS_OK;
1069}
1070
1071/* Parse a "redirect" action. It returns ACT_RET_PRS_OK on success,
1072 * ACT_RET_PRS_ERR on error.
1073 */
1074static enum act_parse_ret parse_http_redirect(const char **args, int *orig_arg, struct proxy *px,
1075 struct act_rule *rule, char **err)
1076{
1077 struct redirect_rule *redir;
1078 int dir, cur_arg;
1079
1080 rule->action = ACT_HTTP_REDIR;
1081
1082 cur_arg = *orig_arg;
1083
1084 dir = (rule->from == ACT_F_HTTP_REQ ? 0 : 1);
1085 if ((redir = http_parse_redirect_rule(px->conf.args.file, px->conf.args.line, px, &args[cur_arg], err, 1, dir)) == NULL)
1086 return ACT_RET_PRS_ERR;
1087
1088 rule->arg.redir = redir;
1089 rule->cond = redir->cond;
1090 redir->cond = NULL;
1091
1092 /* skip all arguments */
1093 while (*args[cur_arg])
1094 cur_arg++;
1095
1096 *orig_arg = cur_arg;
1097 return ACT_RET_PRS_OK;
1098}
1099
1100/* Parse a "add-acl", "del-acl", "set-map" or "del-map" actions. It takes one or
1101 * two log-format string as argument depending on the action. It returns
1102 * ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
1103 */
1104static enum act_parse_ret parse_http_set_map(const char **args, int *orig_arg, struct proxy *px,
1105 struct act_rule *rule, char **err)
1106{
1107 int cap, cur_arg;
1108
1109 rule->action = (args[*orig_arg-1][0] == 'a' ? ACT_HTTP_ADD_ACL :
1110 (args[*orig_arg-1][0] == 's' ? ACT_HTTP_SET_MAP :
1111 (args[*orig_arg-1][4] == 'a' ? ACT_HTTP_DEL_ACL : ACT_HTTP_DEL_MAP)));
1112
1113 cur_arg = *orig_arg;
1114 if (rule->action == ACT_HTTP_SET_MAP && (!*args[cur_arg] || !*args[cur_arg+1])) {
1115 memprintf(err, "expects exactly 2 arguments");
1116 return ACT_RET_PRS_ERR;
1117 }
1118 else if (!*args[cur_arg]) {
1119 memprintf(err, "expects exactly 1 arguments");
1120 return ACT_RET_PRS_ERR;
1121 }
1122
1123 /*
1124 * '+ 8' for 'set-map(' (same for del-map)
1125 * '- 9' for 'set-map(' + trailing ')' (same for del-map)
1126 */
1127 rule->arg.map.ref = my_strndup(args[cur_arg-1] + 8, strlen(args[cur_arg-1]) - 9);
1128
1129 if (rule->from == ACT_F_HTTP_REQ) {
1130 px->conf.args.ctx = ARGC_HRQ;
1131 cap = (px->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR;
1132 }
1133 else{
1134 px->conf.args.ctx = ARGC_HRS;
1135 cap = (px->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR;
1136 }
1137
1138 /* key pattern */
1139 LIST_INIT(&rule->arg.map.key);
1140 if (!parse_logformat_string(args[cur_arg], px, &rule->arg.map.key, LOG_OPT_HTTP, cap, err))
1141 return ACT_RET_PRS_ERR;
1142
1143 if (rule->action == ACT_HTTP_SET_MAP) {
1144 /* value pattern for set-map only */
1145 cur_arg++;
1146 LIST_INIT(&rule->arg.map.value);
1147 if (!parse_logformat_string(args[cur_arg], px, &rule->arg.map.value, LOG_OPT_HTTP, cap, err))
1148 return ACT_RET_PRS_ERR;
1149 }
1150
1151 free(px->conf.lfs_file);
1152 px->conf.lfs_file = strdup(px->conf.args.file);
1153 px->conf.lfs_line = px->conf.args.line;
1154
1155 *orig_arg = cur_arg + 1;
1156 return ACT_RET_PRS_OK;
1157}
1158
1159
1160/* Parse a "track-sc*" actions. It returns ACT_RET_PRS_OK on success,
1161 * ACT_RET_PRS_ERR on error.
1162 */
1163static enum act_parse_ret parse_http_track_sc(const char **args, int *orig_arg, struct proxy *px,
1164 struct act_rule *rule, char **err)
1165{
1166 struct sample_expr *expr;
1167 unsigned int where;
1168 unsigned int tsc_num;
1169 const char *tsc_num_str;
1170 int cur_arg;
1171
1172 tsc_num_str = &args[*orig_arg-1][8];
1173 if (cfg_parse_track_sc_num(&tsc_num, tsc_num_str, tsc_num_str + strlen(tsc_num_str), err) == -1)
1174 return ACT_RET_PRS_ERR;
1175
1176 cur_arg = *orig_arg;
1177 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line,
1178 err, &px->conf.args);
1179 if (!expr)
1180 return ACT_RET_PRS_ERR;
1181
1182 where = 0;
1183 if (px->cap & PR_CAP_FE)
1184 where |= (rule->from == ACT_F_HTTP_REQ ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_FE_HRS_HDR);
1185 if (px->cap & PR_CAP_BE)
1186 where |= (rule->from == ACT_F_HTTP_REQ ? SMP_VAL_BE_HRQ_HDR : SMP_VAL_BE_HRS_HDR);
1187
1188 if (!(expr->fetch->val & where)) {
1189 memprintf(err, "fetch method '%s' extracts information from '%s', none of which is available here",
1190 args[cur_arg-1], sample_src_names(expr->fetch->use));
1191 return ACT_RET_PRS_ERR;
1192 }
1193
1194 if (strcmp(args[cur_arg], "table") == 0) {
1195 cur_arg++;
1196 if (!*args[cur_arg]) {
1197 memprintf(err, "missing table name");
1198 return ACT_RET_PRS_ERR;
1199 }
1200
1201 /* we copy the table name for now, it will be resolved later */
1202 rule->arg.trk_ctr.table.n = strdup(args[cur_arg]);
1203 cur_arg++;
1204 }
1205
1206 rule->arg.trk_ctr.expr = expr;
1207 rule->action = ACT_ACTION_TRK_SC0 + tsc_num;
1208 rule->check_ptr = check_trk_action;
1209
1210 *orig_arg = cur_arg;
1211 return ACT_RET_PRS_OK;
1212}
1213
Christopher Faulet46f95542019-12-20 10:07:22 +01001214/* This function executes a strict-mode actions. On success, it always returns
1215 * ACT_RET_CONT
1216 */
1217static enum act_return http_action_strict_mode(struct act_rule *rule, struct proxy *px,
1218 struct session *sess, struct stream *s, int flags)
1219{
1220 struct http_msg *msg = ((rule->from == ACT_F_HTTP_REQ) ? &s->txn->req : &s->txn->rsp);
1221
1222 if (rule->action == 0) // strict-mode on
1223 msg->flags &= ~HTTP_MSGF_SOFT_RW;
1224 else // strict-mode off
1225 msg->flags |= HTTP_MSGF_SOFT_RW;
1226 return ACT_RET_CONT;
1227}
1228
1229/* Parse a "strict-mode" action. It returns ACT_RET_PRS_OK on success,
1230 * ACT_RET_PRS_ERR on error.
1231 */
1232static enum act_parse_ret parse_http_strict_mode(const char **args, int *orig_arg, struct proxy *px,
1233 struct act_rule *rule, char **err)
1234{
1235 int cur_arg;
1236
1237
1238 cur_arg = *orig_arg;
1239 if (!*args[cur_arg]) {
1240 memprintf(err, "expects exactly 1 arguments");
1241 return ACT_RET_PRS_ERR;
1242 }
1243
1244 if (strcasecmp(args[cur_arg], "on") == 0)
1245 rule->action = 0; // strict-mode on
1246 else if (strcasecmp(args[cur_arg], "off") == 0)
1247 rule->action = 1; // strict-mode off
1248 else {
1249 memprintf(err, "Unexpected value '%s'. Only 'on' and 'off' are supported", args[cur_arg]);
1250 return ACT_RET_PRS_ERR;
1251 }
1252 rule->action_ptr = http_action_strict_mode;
1253
1254 *orig_arg = cur_arg + 1;
1255 return ACT_RET_PRS_OK;
1256}
1257
Willy Tarreau79e57332018-10-02 16:01:16 +02001258/************************************************************************/
1259/* All supported http-request action keywords must be declared here. */
1260/************************************************************************/
1261
1262static struct action_kw_list http_req_actions = {
1263 .kw = {
Christopher Faulet81e20172019-12-12 16:40:30 +01001264 { "add-acl", parse_http_set_map, 1 },
1265 { "add-header", parse_http_set_header, 0 },
1266 { "allow", parse_http_allow, 0 },
1267 { "auth", parse_http_auth, 0 },
1268 { "capture", parse_http_req_capture, 0 },
1269 { "del-acl", parse_http_set_map, 1 },
1270 { "del-header", parse_http_del_header, 0 },
1271 { "del-map", parse_http_set_map, 1 },
1272 { "deny", parse_http_req_deny, 0 },
1273 { "disable-l7-retry", parse_http_req_disable_l7_retry, 0 },
1274 { "early-hint", parse_http_set_header, 0 },
1275 { "redirect", parse_http_redirect, 0 },
1276 { "reject", parse_http_action_reject, 0 },
1277 { "replace-header", parse_http_replace_header, 0 },
1278 { "replace-path", parse_replace_uri, 0 },
1279 { "replace-uri", parse_replace_uri, 0 },
1280 { "replace-value", parse_http_replace_header, 0 },
1281 { "set-header", parse_http_set_header, 0 },
1282 { "set-log-level", parse_http_set_log_level, 0 },
1283 { "set-map", parse_http_set_map, 1 },
1284 { "set-method", parse_set_req_line, 0 },
1285 { "set-mark", parse_http_set_mark, 0 },
1286 { "set-nice", parse_http_set_nice, 0 },
1287 { "set-path", parse_set_req_line, 0 },
1288 { "set-query", parse_set_req_line, 0 },
1289 { "set-tos", parse_http_set_tos, 0 },
1290 { "set-uri", parse_set_req_line, 0 },
Christopher Faulet46f95542019-12-20 10:07:22 +01001291 { "strict-mode", parse_http_strict_mode, 0 },
Christopher Faulet81e20172019-12-12 16:40:30 +01001292 { "tarpit", parse_http_req_deny, 0 },
1293 { "track-sc", parse_http_track_sc, 1 },
Willy Tarreau79e57332018-10-02 16:01:16 +02001294 { NULL, NULL }
1295 }
1296};
1297
Willy Tarreau0108d902018-11-25 19:14:37 +01001298INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_actions);
1299
Willy Tarreau79e57332018-10-02 16:01:16 +02001300static struct action_kw_list http_res_actions = {
1301 .kw = {
Christopher Faulet81e20172019-12-12 16:40:30 +01001302 { "add-acl", parse_http_set_map, 1 },
1303 { "add-header", parse_http_set_header, 0 },
1304 { "allow", parse_http_allow, 0 },
1305 { "capture", parse_http_res_capture, 0 },
1306 { "del-acl", parse_http_set_map, 1 },
1307 { "del-header", parse_http_del_header, 0 },
1308 { "del-map", parse_http_set_map, 1 },
1309 { "deny", parse_http_res_deny, 0 },
1310 { "redirect", parse_http_redirect, 0 },
1311 { "replace-header", parse_http_replace_header, 0 },
1312 { "replace-value", parse_http_replace_header, 0 },
1313 { "set-header", parse_http_set_header, 0 },
1314 { "set-log-level", parse_http_set_log_level, 0 },
1315 { "set-map", parse_http_set_map, 1 },
1316 { "set-mark", parse_http_set_mark, 0 },
1317 { "set-nice", parse_http_set_nice, 0 },
1318 { "set-status", parse_http_set_status, 0 },
1319 { "set-tos", parse_http_set_tos, 0 },
Christopher Faulet46f95542019-12-20 10:07:22 +01001320 { "strict-mode", parse_http_strict_mode, 0 },
Christopher Faulet81e20172019-12-12 16:40:30 +01001321 { "track-sc", parse_http_track_sc, 1 },
Willy Tarreau79e57332018-10-02 16:01:16 +02001322 { NULL, NULL }
1323 }
1324};
1325
Willy Tarreau0108d902018-11-25 19:14:37 +01001326INITCALL1(STG_REGISTER, http_res_keywords_register, &http_res_actions);
Willy Tarreau79e57332018-10-02 16:01:16 +02001327
1328/*
1329 * Local variables:
1330 * c-indent-level: 8
1331 * c-basic-offset: 8
1332 * End:
1333 */