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