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 | |
Willy Tarreau | dcc048a | 2020-06-04 19:11:43 +0200 | [diff] [blame] | 19 | #include <haproxy/acl.h> |
Willy Tarreau | 122eba9 | 2020-06-04 10:15:32 +0200 | [diff] [blame] | 20 | #include <haproxy/action.h> |
Willy Tarreau | 4c7e4b7 | 2020-05-27 12:58:42 +0200 | [diff] [blame] | 21 | #include <haproxy/api.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 22 | #include <haproxy/arg.h> |
| 23 | #include <haproxy/capture-t.h> |
Willy Tarreau | 6be7849 | 2020-06-05 00:00:29 +0200 | [diff] [blame] | 24 | #include <haproxy/cfgparse.h> |
Willy Tarreau | c13ed53 | 2020-06-02 10:22:45 +0200 | [diff] [blame] | 25 | #include <haproxy/chunk.h> |
Willy Tarreau | f268ee8 | 2020-06-04 17:05:57 +0200 | [diff] [blame] | 26 | #include <haproxy/global.h> |
Willy Tarreau | cd72d8c | 2020-06-02 19:11:26 +0200 | [diff] [blame] | 27 | #include <haproxy/http.h> |
Willy Tarreau | c2b1ff0 | 2020-06-04 21:21:03 +0200 | [diff] [blame] | 28 | #include <haproxy/http_ana.h> |
Willy Tarreau | 8773533 | 2020-06-04 09:08:41 +0200 | [diff] [blame] | 29 | #include <haproxy/http_htx.h> |
Willy Tarreau | c761f84 | 2020-06-04 11:40:28 +0200 | [diff] [blame] | 30 | #include <haproxy/http_rules.h> |
Willy Tarreau | 36979d9 | 2020-06-05 17:27:29 +0200 | [diff] [blame] | 31 | #include <haproxy/log.h> |
Willy Tarreau | 225a90a | 2020-06-04 15:06:28 +0200 | [diff] [blame] | 32 | #include <haproxy/pattern.h> |
Willy Tarreau | d0ef439 | 2020-06-02 09:38:52 +0200 | [diff] [blame] | 33 | #include <haproxy/pool.h> |
Willy Tarreau | 7cd8b6e | 2020-06-02 17:32:26 +0200 | [diff] [blame] | 34 | #include <haproxy/regex.h> |
Willy Tarreau | e6ce10b | 2020-06-04 15:33:47 +0200 | [diff] [blame] | 35 | #include <haproxy/sample.h> |
Willy Tarreau | 5e539c9 | 2020-06-04 20:45:39 +0200 | [diff] [blame] | 36 | #include <haproxy/stream_interface.h> |
Willy Tarreau | 48fbcae | 2020-06-03 18:09:46 +0200 | [diff] [blame] | 37 | #include <haproxy/tools.h> |
Willy Tarreau | 8c42b8a | 2020-06-04 19:27:34 +0200 | [diff] [blame] | 38 | #include <haproxy/uri_auth-t.h> |
Tim Duesterhus | d2bedcc | 2021-04-15 21:45:57 +0200 | [diff] [blame] | 39 | #include <haproxy/uri_normalizer.h> |
Willy Tarreau | d678805 | 2020-05-27 15:59:00 +0200 | [diff] [blame] | 40 | #include <haproxy/version.h> |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 41 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 42 | |
Christopher Faulet | 2eb5396 | 2020-01-14 14:47:34 +0100 | [diff] [blame] | 43 | /* Release memory allocated by most of HTTP actions. Concretly, it releases |
| 44 | * <arg.http>. |
| 45 | */ |
| 46 | static void release_http_action(struct act_rule *rule) |
| 47 | { |
| 48 | struct logformat_node *lf, *lfb; |
| 49 | |
Tim Duesterhus | ed52637 | 2020-03-05 17:56:33 +0100 | [diff] [blame] | 50 | istfree(&rule->arg.http.str); |
Christopher Faulet | 2eb5396 | 2020-01-14 14:47:34 +0100 | [diff] [blame] | 51 | if (rule->arg.http.re) |
| 52 | regex_free(rule->arg.http.re); |
| 53 | list_for_each_entry_safe(lf, lfb, &rule->arg.http.fmt, list) { |
| 54 | LIST_DEL(&lf->list); |
| 55 | release_sample_expr(lf->expr); |
| 56 | free(lf->arg); |
| 57 | free(lf); |
| 58 | } |
| 59 | } |
| 60 | |
Christopher Faulet | 5cb513a | 2020-05-13 17:56:56 +0200 | [diff] [blame] | 61 | /* Release memory allocated by HTTP actions relying on an http reply. Concretly, |
| 62 | * it releases <.arg.http_reply> |
| 63 | */ |
| 64 | static void release_act_http_reply(struct act_rule *rule) |
| 65 | { |
| 66 | release_http_reply(rule->arg.http_reply); |
| 67 | rule->arg.http_reply = NULL; |
| 68 | } |
| 69 | |
| 70 | |
| 71 | /* Check function for HTTP actions relying on an http reply. The function |
| 72 | * returns 1 in success case, otherwise, it returns 0 and err is filled. |
| 73 | */ |
| 74 | static int check_act_http_reply(struct act_rule *rule, struct proxy *px, char **err) |
| 75 | { |
| 76 | struct http_reply *reply = rule->arg.http_reply; |
| 77 | |
| 78 | if (!http_check_http_reply(reply, px, err)) { |
| 79 | release_act_http_reply(rule); |
| 80 | return 0; |
| 81 | } |
| 82 | return 1; |
| 83 | } |
| 84 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 85 | |
| 86 | /* This function executes one of the set-{method,path,query,uri} actions. It |
| 87 | * 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] | 88 | * the action to be performed in <.action>, previously filled by function |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 89 | * parse_set_req_line(). The replacement action is executed by the function |
Christopher Faulet | e00d06c | 2019-12-16 17:18:42 +0100 | [diff] [blame] | 90 | * http_action_set_req_line(). On success, it returns ACT_RET_CONT. If an error |
| 91 | * occurs while soft rewrites are enabled, the action is canceled, but the rule |
| 92 | * processing continue. Otherwsize ACT_RET_ERR is returned. |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 93 | */ |
| 94 | static enum act_return http_action_set_req_line(struct act_rule *rule, struct proxy *px, |
| 95 | struct session *sess, struct stream *s, int flags) |
| 96 | { |
| 97 | struct buffer *replace; |
Christopher Faulet | 1340376 | 2019-12-13 09:01:57 +0100 | [diff] [blame] | 98 | enum act_return ret = ACT_RET_CONT; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 99 | |
| 100 | replace = alloc_trash_chunk(); |
| 101 | if (!replace) |
Christopher Faulet | e00d06c | 2019-12-16 17:18:42 +0100 | [diff] [blame] | 102 | goto fail_alloc; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 103 | |
| 104 | /* If we have to create a query string, prepare a '?'. */ |
Christopher Faulet | 2c22a69 | 2019-12-18 15:39:56 +0100 | [diff] [blame] | 105 | if (rule->action == 2) // set-query |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 106 | replace->area[replace->data++] = '?'; |
| 107 | replace->data += build_logline(s, replace->area + replace->data, |
| 108 | replace->size - replace->data, |
Christopher Faulet | 96bff76 | 2019-12-17 13:46:18 +0100 | [diff] [blame] | 109 | &rule->arg.http.fmt); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 110 | |
Christopher Faulet | 2c22a69 | 2019-12-18 15:39:56 +0100 | [diff] [blame] | 111 | 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] | 112 | goto fail_rewrite; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 113 | |
Christopher Faulet | e00d06c | 2019-12-16 17:18:42 +0100 | [diff] [blame] | 114 | leave: |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 115 | free_trash_chunk(replace); |
| 116 | return ret; |
Christopher Faulet | e00d06c | 2019-12-16 17:18:42 +0100 | [diff] [blame] | 117 | |
| 118 | fail_alloc: |
| 119 | if (!(s->flags & SF_ERR_MASK)) |
| 120 | s->flags |= SF_ERR_RESOURCE; |
| 121 | ret = ACT_RET_ERR; |
| 122 | goto leave; |
| 123 | |
| 124 | fail_rewrite: |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 125 | _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_rewrites); |
Christopher Faulet | e00d06c | 2019-12-16 17:18:42 +0100 | [diff] [blame] | 126 | if (s->flags & SF_BE_ASSIGNED) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 127 | _HA_ATOMIC_INC(&s->be->be_counters.failed_rewrites); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 128 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 129 | _HA_ATOMIC_INC(&sess->listener->counters->failed_rewrites); |
Christopher Faulet | e00d06c | 2019-12-16 17:18:42 +0100 | [diff] [blame] | 130 | if (objt_server(s->target)) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 131 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_rewrites); |
Christopher Faulet | e00d06c | 2019-12-16 17:18:42 +0100 | [diff] [blame] | 132 | |
Christopher Faulet | 333bf8c | 2020-01-22 14:38:05 +0100 | [diff] [blame] | 133 | if (!(s->txn->req.flags & HTTP_MSGF_SOFT_RW)) { |
Christopher Faulet | e00d06c | 2019-12-16 17:18:42 +0100 | [diff] [blame] | 134 | ret = ACT_RET_ERR; |
Christopher Faulet | 333bf8c | 2020-01-22 14:38:05 +0100 | [diff] [blame] | 135 | if (!(s->flags & SF_ERR_MASK)) |
| 136 | s->flags |= SF_ERR_PRXCOND; |
| 137 | } |
Christopher Faulet | e00d06c | 2019-12-16 17:18:42 +0100 | [diff] [blame] | 138 | goto leave; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | /* parse an http-request action among : |
| 142 | * set-method |
| 143 | * set-path |
Christopher Faulet | 312294f | 2020-09-02 17:17:44 +0200 | [diff] [blame] | 144 | * set-pathq |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 145 | * set-query |
| 146 | * set-uri |
| 147 | * |
| 148 | * 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] | 149 | * 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] | 150 | * and <.action> to store the action type as an int (0=method, 1=path, 2=query, |
| 151 | * 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] | 152 | */ |
| 153 | static enum act_parse_ret parse_set_req_line(const char **args, int *orig_arg, struct proxy *px, |
| 154 | struct act_rule *rule, char **err) |
| 155 | { |
| 156 | int cur_arg = *orig_arg; |
| 157 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 158 | switch (args[0][4]) { |
| 159 | case 'm' : |
Christopher Faulet | 2c22a69 | 2019-12-18 15:39:56 +0100 | [diff] [blame] | 160 | rule->action = 0; // set-method |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 161 | break; |
| 162 | case 'p' : |
Christopher Faulet | 312294f | 2020-09-02 17:17:44 +0200 | [diff] [blame] | 163 | if (args[0][8] == 'q') |
| 164 | rule->action = 4; // set-pathq |
| 165 | else |
| 166 | rule->action = 1; // set-path |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 167 | break; |
| 168 | case 'q' : |
Christopher Faulet | 2c22a69 | 2019-12-18 15:39:56 +0100 | [diff] [blame] | 169 | rule->action = 2; // set-query |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 170 | break; |
| 171 | case 'u' : |
Christopher Faulet | 2c22a69 | 2019-12-18 15:39:56 +0100 | [diff] [blame] | 172 | rule->action = 3; // set-uri |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 173 | break; |
| 174 | default: |
| 175 | memprintf(err, "internal error: unhandled action '%s'", args[0]); |
| 176 | return ACT_RET_PRS_ERR; |
| 177 | } |
Christopher Faulet | 96bff76 | 2019-12-17 13:46:18 +0100 | [diff] [blame] | 178 | rule->action_ptr = http_action_set_req_line; |
Christopher Faulet | 2eb5396 | 2020-01-14 14:47:34 +0100 | [diff] [blame] | 179 | rule->release_ptr = release_http_action; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 180 | |
| 181 | if (!*args[cur_arg] || |
| 182 | (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) { |
| 183 | memprintf(err, "expects exactly 1 argument <format>"); |
| 184 | return ACT_RET_PRS_ERR; |
| 185 | } |
| 186 | |
Christopher Faulet | 96bff76 | 2019-12-17 13:46:18 +0100 | [diff] [blame] | 187 | LIST_INIT(&rule->arg.http.fmt); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 188 | px->conf.args.ctx = ARGC_HRQ; |
Christopher Faulet | 96bff76 | 2019-12-17 13:46:18 +0100 | [diff] [blame] | 189 | 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] | 190 | (px->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, err)) { |
| 191 | return ACT_RET_PRS_ERR; |
| 192 | } |
| 193 | |
| 194 | (*orig_arg)++; |
| 195 | return ACT_RET_PRS_OK; |
| 196 | } |
| 197 | |
Tim Duesterhus | d2bedcc | 2021-04-15 21:45:57 +0200 | [diff] [blame] | 198 | /* This function executes the http-request normalize-uri action. |
| 199 | * `rule->action` is expected to be a value from `enum act_normalize_uri`. |
| 200 | * |
| 201 | * On success, it returns ACT_RET_CONT. If an error |
| 202 | * occurs while soft rewrites are enabled, the action is canceled, but the rule |
| 203 | * processing continue. Otherwsize ACT_RET_ERR is returned. |
| 204 | */ |
| 205 | static enum act_return http_action_normalize_uri(struct act_rule *rule, struct proxy *px, |
| 206 | struct session *sess, struct stream *s, int flags) |
| 207 | { |
| 208 | enum act_return ret = ACT_RET_CONT; |
| 209 | struct htx *htx = htxbuf(&s->req.buf); |
| 210 | const struct ist uri = htx_sl_req_uri(http_get_stline(htx)); |
| 211 | struct buffer *replace = alloc_trash_chunk(); |
| 212 | enum uri_normalizer_err err = URI_NORMALIZER_ERR_INTERNAL_ERROR; |
| 213 | |
| 214 | if (!replace) |
| 215 | goto fail_alloc; |
| 216 | |
| 217 | switch ((enum act_normalize_uri) rule->action) { |
Tim Duesterhus | d371e99 | 2021-04-15 21:45:58 +0200 | [diff] [blame] | 218 | case ACT_NORMALIZE_URI_MERGE_SLASHES: { |
| 219 | const struct ist path = http_get_path(uri); |
| 220 | struct ist newpath = ist2(replace->area, replace->size); |
| 221 | |
| 222 | if (!isttest(path)) |
| 223 | goto leave; |
| 224 | |
| 225 | err = uri_normalizer_path_merge_slashes(iststop(path, '?'), &newpath); |
| 226 | |
| 227 | if (err != URI_NORMALIZER_ERR_NONE) |
| 228 | break; |
| 229 | |
| 230 | if (!http_replace_req_path(htx, newpath, 0)) |
| 231 | goto fail_rewrite; |
| 232 | |
| 233 | break; |
| 234 | } |
Tim Duesterhus | 560e1a6 | 2021-04-15 21:46:00 +0200 | [diff] [blame] | 235 | case ACT_NORMALIZE_URI_DOTDOT: |
| 236 | case ACT_NORMALIZE_URI_DOTDOT_FULL: { |
Tim Duesterhus | 9982fc2 | 2021-04-15 21:45:59 +0200 | [diff] [blame] | 237 | const struct ist path = http_get_path(uri); |
| 238 | struct ist newpath = ist2(replace->area, replace->size); |
| 239 | |
| 240 | if (!isttest(path)) |
| 241 | goto leave; |
| 242 | |
Tim Duesterhus | 560e1a6 | 2021-04-15 21:46:00 +0200 | [diff] [blame] | 243 | err = uri_normalizer_path_dotdot(iststop(path, '?'), rule->action == ACT_NORMALIZE_URI_DOTDOT_FULL, &newpath); |
Tim Duesterhus | 9982fc2 | 2021-04-15 21:45:59 +0200 | [diff] [blame] | 244 | |
| 245 | if (err != URI_NORMALIZER_ERR_NONE) |
| 246 | break; |
| 247 | |
| 248 | if (!http_replace_req_path(htx, newpath, 0)) |
| 249 | goto fail_rewrite; |
| 250 | |
| 251 | break; |
| 252 | } |
Tim Duesterhus | d7b89be | 2021-04-15 21:46:01 +0200 | [diff] [blame^] | 253 | case ACT_NORMALIZE_URI_SORT_QUERY: { |
| 254 | const struct ist path = http_get_path(uri); |
| 255 | struct ist newquery = ist2(replace->area, replace->size); |
| 256 | |
| 257 | if (!isttest(path)) |
| 258 | goto leave; |
| 259 | |
| 260 | err = uri_normalizer_query_sort(istfind(path, '?'), '&', &newquery); |
| 261 | |
| 262 | if (err != URI_NORMALIZER_ERR_NONE) |
| 263 | break; |
| 264 | |
| 265 | if (!http_replace_req_query(htx, newquery)) |
| 266 | goto fail_rewrite; |
| 267 | |
| 268 | break; |
| 269 | } |
Tim Duesterhus | d2bedcc | 2021-04-15 21:45:57 +0200 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | switch (err) { |
| 273 | case URI_NORMALIZER_ERR_NONE: |
| 274 | break; |
| 275 | case URI_NORMALIZER_ERR_INTERNAL_ERROR: |
| 276 | ret = ACT_RET_ERR; |
| 277 | break; |
| 278 | case URI_NORMALIZER_ERR_INVALID_INPUT: |
| 279 | ret = ACT_RET_INV; |
| 280 | break; |
| 281 | case URI_NORMALIZER_ERR_ALLOC: |
| 282 | goto fail_alloc; |
| 283 | } |
| 284 | |
| 285 | leave: |
| 286 | free_trash_chunk(replace); |
| 287 | return ret; |
| 288 | |
| 289 | fail_alloc: |
| 290 | if (!(s->flags & SF_ERR_MASK)) |
| 291 | s->flags |= SF_ERR_RESOURCE; |
| 292 | ret = ACT_RET_ERR; |
| 293 | goto leave; |
| 294 | |
| 295 | fail_rewrite: |
| 296 | _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_rewrites, 1); |
| 297 | if (s->flags & SF_BE_ASSIGNED) |
| 298 | _HA_ATOMIC_ADD(&s->be->be_counters.failed_rewrites, 1); |
| 299 | if (sess->listener && sess->listener->counters) |
| 300 | _HA_ATOMIC_ADD(&sess->listener->counters->failed_rewrites, 1); |
| 301 | if (objt_server(s->target)) |
| 302 | _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_rewrites, 1); |
| 303 | |
| 304 | if (!(s->txn->req.flags & HTTP_MSGF_SOFT_RW)) { |
| 305 | ret = ACT_RET_ERR; |
| 306 | if (!(s->flags & SF_ERR_MASK)) |
| 307 | s->flags |= SF_ERR_PRXCOND; |
| 308 | } |
| 309 | goto leave; |
| 310 | } |
| 311 | |
| 312 | /* Parses the http-request normalize-uri action. It expects a single <normalizer> |
| 313 | * argument, corresponding too a value in `enum act_normalize_uri`. |
| 314 | * |
| 315 | * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error. |
| 316 | */ |
| 317 | static enum act_parse_ret parse_http_normalize_uri(const char **args, int *orig_arg, struct proxy *px, |
| 318 | struct act_rule *rule, char **err) |
| 319 | { |
| 320 | int cur_arg = *orig_arg; |
| 321 | |
| 322 | rule->action_ptr = http_action_normalize_uri; |
| 323 | rule->release_ptr = NULL; |
| 324 | |
| 325 | if (!*args[cur_arg]) { |
| 326 | memprintf(err, "missing argument <normalizer>"); |
| 327 | return ACT_RET_PRS_ERR; |
| 328 | } |
| 329 | |
Tim Duesterhus | d371e99 | 2021-04-15 21:45:58 +0200 | [diff] [blame] | 330 | if (strcmp(args[cur_arg], "merge-slashes") == 0) { |
| 331 | cur_arg++; |
Tim Duesterhus | d2bedcc | 2021-04-15 21:45:57 +0200 | [diff] [blame] | 332 | |
Tim Duesterhus | d371e99 | 2021-04-15 21:45:58 +0200 | [diff] [blame] | 333 | rule->action = ACT_NORMALIZE_URI_MERGE_SLASHES; |
Tim Duesterhus | d2bedcc | 2021-04-15 21:45:57 +0200 | [diff] [blame] | 334 | } |
Tim Duesterhus | 9982fc2 | 2021-04-15 21:45:59 +0200 | [diff] [blame] | 335 | else if (strcmp(args[cur_arg], "dotdot") == 0) { |
| 336 | cur_arg++; |
| 337 | |
Tim Duesterhus | 560e1a6 | 2021-04-15 21:46:00 +0200 | [diff] [blame] | 338 | if (strcmp(args[cur_arg], "full") == 0) { |
| 339 | cur_arg++; |
| 340 | rule->action = ACT_NORMALIZE_URI_DOTDOT_FULL; |
| 341 | } |
| 342 | else if (!*args[cur_arg]) { |
| 343 | rule->action = ACT_NORMALIZE_URI_DOTDOT; |
| 344 | } |
| 345 | else if (strcmp(args[cur_arg], "if") != 0 && strcmp(args[cur_arg], "unless") != 0) { |
| 346 | memprintf(err, "unknown argument '%s' for 'dotdot' normalizer", args[cur_arg]); |
| 347 | return ACT_RET_PRS_ERR; |
| 348 | } |
Tim Duesterhus | 9982fc2 | 2021-04-15 21:45:59 +0200 | [diff] [blame] | 349 | } |
Tim Duesterhus | d7b89be | 2021-04-15 21:46:01 +0200 | [diff] [blame^] | 350 | else if (strcmp(args[cur_arg], "sort-query") == 0) { |
| 351 | cur_arg++; |
| 352 | |
| 353 | rule->action = ACT_NORMALIZE_URI_SORT_QUERY; |
| 354 | } |
Tim Duesterhus | d2bedcc | 2021-04-15 21:45:57 +0200 | [diff] [blame] | 355 | else { |
| 356 | memprintf(err, "unknown normalizer '%s'", args[cur_arg]); |
| 357 | return ACT_RET_PRS_ERR; |
| 358 | } |
| 359 | |
| 360 | *orig_arg = cur_arg; |
| 361 | return ACT_RET_PRS_OK; |
| 362 | } |
| 363 | |
Willy Tarreau | 3381022 | 2019-06-12 17:44:02 +0200 | [diff] [blame] | 364 | /* This function executes a replace-uri action. It finds its arguments in |
Christopher Faulet | 96bff76 | 2019-12-17 13:46:18 +0100 | [diff] [blame] | 365 | * <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] | 366 | * previously filled by function parse_replace_uri() and will execute the regex |
Christopher Faulet | 96bff76 | 2019-12-17 13:46:18 +0100 | [diff] [blame] | 367 | * 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] | 368 | * <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] | 369 | * contains 1 for the path or 3 for the URI (values used by |
| 370 | * http_req_replace_stline()). On success, it returns ACT_RET_CONT. If an error |
| 371 | * occurs while soft rewrites are enabled, the action is canceled, but the rule |
| 372 | * processing continue. Otherwsize ACT_RET_ERR is returned. |
Willy Tarreau | 3381022 | 2019-06-12 17:44:02 +0200 | [diff] [blame] | 373 | */ |
| 374 | static enum act_return http_action_replace_uri(struct act_rule *rule, struct proxy *px, |
| 375 | struct session *sess, struct stream *s, int flags) |
| 376 | { |
Christopher Faulet | 1340376 | 2019-12-13 09:01:57 +0100 | [diff] [blame] | 377 | enum act_return ret = ACT_RET_CONT; |
Willy Tarreau | 3381022 | 2019-06-12 17:44:02 +0200 | [diff] [blame] | 378 | struct buffer *replace, *output; |
| 379 | struct ist uri; |
| 380 | int len; |
| 381 | |
| 382 | replace = alloc_trash_chunk(); |
| 383 | output = alloc_trash_chunk(); |
| 384 | if (!replace || !output) |
Christopher Faulet | e00d06c | 2019-12-16 17:18:42 +0100 | [diff] [blame] | 385 | goto fail_alloc; |
Christopher Faulet | 12c28b6 | 2019-07-15 16:30:24 +0200 | [diff] [blame] | 386 | uri = htx_sl_req_uri(http_get_stline(htxbuf(&s->req.buf))); |
Willy Tarreau | 262c3f1 | 2019-12-17 06:52:51 +0100 | [diff] [blame] | 387 | |
Christopher Faulet | 1fa0cc1 | 2020-09-02 11:10:38 +0200 | [diff] [blame] | 388 | if (rule->action == 1) // replace-path |
| 389 | uri = iststop(http_get_path(uri), '?'); |
Christopher Faulet | 312294f | 2020-09-02 17:17:44 +0200 | [diff] [blame] | 390 | else if (rule->action == 4) // replace-pathq |
| 391 | uri = http_get_path(uri); |
Willy Tarreau | 262c3f1 | 2019-12-17 06:52:51 +0100 | [diff] [blame] | 392 | |
Christopher Faulet | 96bff76 | 2019-12-17 13:46:18 +0100 | [diff] [blame] | 393 | 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] | 394 | goto leave; |
| 395 | |
Christopher Faulet | 96bff76 | 2019-12-17 13:46:18 +0100 | [diff] [blame] | 396 | 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] | 397 | |
| 398 | /* note: uri.ptr doesn't need to be zero-terminated because it will |
| 399 | * only be used to pick pmatch references. |
| 400 | */ |
| 401 | len = exp_replace(output->area, output->size, uri.ptr, replace->area, pmatch); |
| 402 | if (len == -1) |
Christopher Faulet | e00d06c | 2019-12-16 17:18:42 +0100 | [diff] [blame] | 403 | goto fail_rewrite; |
Willy Tarreau | 3381022 | 2019-06-12 17:44:02 +0200 | [diff] [blame] | 404 | |
Christopher Faulet | 2c22a69 | 2019-12-18 15:39:56 +0100 | [diff] [blame] | 405 | 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] | 406 | goto fail_rewrite; |
Willy Tarreau | 3381022 | 2019-06-12 17:44:02 +0200 | [diff] [blame] | 407 | |
Christopher Faulet | e00d06c | 2019-12-16 17:18:42 +0100 | [diff] [blame] | 408 | leave: |
Willy Tarreau | 3381022 | 2019-06-12 17:44:02 +0200 | [diff] [blame] | 409 | free_trash_chunk(output); |
| 410 | free_trash_chunk(replace); |
| 411 | return ret; |
Christopher Faulet | e00d06c | 2019-12-16 17:18:42 +0100 | [diff] [blame] | 412 | |
| 413 | fail_alloc: |
| 414 | if (!(s->flags & SF_ERR_MASK)) |
| 415 | s->flags |= SF_ERR_RESOURCE; |
| 416 | ret = ACT_RET_ERR; |
| 417 | goto leave; |
| 418 | |
| 419 | fail_rewrite: |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 420 | _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_rewrites); |
Christopher Faulet | e00d06c | 2019-12-16 17:18:42 +0100 | [diff] [blame] | 421 | if (s->flags & SF_BE_ASSIGNED) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 422 | _HA_ATOMIC_INC(&s->be->be_counters.failed_rewrites); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 423 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 424 | _HA_ATOMIC_INC(&sess->listener->counters->failed_rewrites); |
Christopher Faulet | e00d06c | 2019-12-16 17:18:42 +0100 | [diff] [blame] | 425 | if (objt_server(s->target)) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 426 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_rewrites); |
Christopher Faulet | e00d06c | 2019-12-16 17:18:42 +0100 | [diff] [blame] | 427 | |
Christopher Faulet | 333bf8c | 2020-01-22 14:38:05 +0100 | [diff] [blame] | 428 | if (!(s->txn->req.flags & HTTP_MSGF_SOFT_RW)) { |
Christopher Faulet | e00d06c | 2019-12-16 17:18:42 +0100 | [diff] [blame] | 429 | ret = ACT_RET_ERR; |
Christopher Faulet | 333bf8c | 2020-01-22 14:38:05 +0100 | [diff] [blame] | 430 | if (!(s->flags & SF_ERR_MASK)) |
| 431 | s->flags |= SF_ERR_PRXCOND; |
| 432 | } |
Christopher Faulet | e00d06c | 2019-12-16 17:18:42 +0100 | [diff] [blame] | 433 | goto leave; |
Willy Tarreau | 3381022 | 2019-06-12 17:44:02 +0200 | [diff] [blame] | 434 | } |
| 435 | |
Christopher Faulet | 312294f | 2020-09-02 17:17:44 +0200 | [diff] [blame] | 436 | /* parse a "replace-uri", "replace-path" or "replace-pathq" |
| 437 | * http-request action. |
Willy Tarreau | 3381022 | 2019-06-12 17:44:02 +0200 | [diff] [blame] | 438 | * This action takes 2 arguments (a regex and a replacement format string). |
Christopher Faulet | 2c22a69 | 2019-12-18 15:39:56 +0100 | [diff] [blame] | 439 | * 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] | 440 | * <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] | 441 | * list head. It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error. |
| 442 | */ |
| 443 | static enum act_parse_ret parse_replace_uri(const char **args, int *orig_arg, struct proxy *px, |
| 444 | struct act_rule *rule, char **err) |
| 445 | { |
| 446 | int cur_arg = *orig_arg; |
| 447 | char *error = NULL; |
| 448 | |
Christopher Faulet | 312294f | 2020-09-02 17:17:44 +0200 | [diff] [blame] | 449 | switch (args[0][8]) { |
| 450 | case 'p': |
| 451 | if (args[0][12] == 'q') |
| 452 | rule->action = 4; // replace-pathq, same as set-pathq |
| 453 | else |
| 454 | rule->action = 1; // replace-path, same as set-path |
| 455 | break; |
| 456 | case 'u': |
Christopher Faulet | 2c22a69 | 2019-12-18 15:39:56 +0100 | [diff] [blame] | 457 | rule->action = 3; // replace-uri, same as set-uri |
Christopher Faulet | 312294f | 2020-09-02 17:17:44 +0200 | [diff] [blame] | 458 | break; |
| 459 | default: |
| 460 | memprintf(err, "internal error: unhandled action '%s'", args[0]); |
| 461 | return ACT_RET_PRS_ERR; |
| 462 | } |
Willy Tarreau | 262c3f1 | 2019-12-17 06:52:51 +0100 | [diff] [blame] | 463 | |
Willy Tarreau | 3381022 | 2019-06-12 17:44:02 +0200 | [diff] [blame] | 464 | rule->action_ptr = http_action_replace_uri; |
Christopher Faulet | 2eb5396 | 2020-01-14 14:47:34 +0100 | [diff] [blame] | 465 | rule->release_ptr = release_http_action; |
Willy Tarreau | 3381022 | 2019-06-12 17:44:02 +0200 | [diff] [blame] | 466 | |
| 467 | if (!*args[cur_arg] || !*args[cur_arg+1] || |
| 468 | (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) { |
| 469 | memprintf(err, "expects exactly 2 arguments <match-regex> and <replace-format>"); |
| 470 | return ACT_RET_PRS_ERR; |
| 471 | } |
| 472 | |
Christopher Faulet | 96bff76 | 2019-12-17 13:46:18 +0100 | [diff] [blame] | 473 | 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] | 474 | memprintf(err, "failed to parse the regex : %s", error); |
| 475 | free(error); |
| 476 | return ACT_RET_PRS_ERR; |
| 477 | } |
| 478 | |
Christopher Faulet | 96bff76 | 2019-12-17 13:46:18 +0100 | [diff] [blame] | 479 | LIST_INIT(&rule->arg.http.fmt); |
Willy Tarreau | 3381022 | 2019-06-12 17:44:02 +0200 | [diff] [blame] | 480 | px->conf.args.ctx = ARGC_HRQ; |
Christopher Faulet | 96bff76 | 2019-12-17 13:46:18 +0100 | [diff] [blame] | 481 | 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] | 482 | (px->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, err)) { |
Christopher Faulet | 1337b32 | 2020-01-14 14:50:55 +0100 | [diff] [blame] | 483 | regex_free(rule->arg.http.re); |
Willy Tarreau | 3381022 | 2019-06-12 17:44:02 +0200 | [diff] [blame] | 484 | return ACT_RET_PRS_ERR; |
| 485 | } |
| 486 | |
| 487 | (*orig_arg) += 2; |
| 488 | return ACT_RET_PRS_OK; |
| 489 | } |
| 490 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 491 | /* This function is just a compliant action wrapper for "set-status". */ |
| 492 | static enum act_return action_http_set_status(struct act_rule *rule, struct proxy *px, |
| 493 | struct session *sess, struct stream *s, int flags) |
| 494 | { |
Christopher Faulet | 96bff76 | 2019-12-17 13:46:18 +0100 | [diff] [blame] | 495 | if (http_res_set_status(rule->arg.http.i, rule->arg.http.str, s) == -1) { |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 496 | _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_rewrites); |
Christopher Faulet | e00d06c | 2019-12-16 17:18:42 +0100 | [diff] [blame] | 497 | if (s->flags & SF_BE_ASSIGNED) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 498 | _HA_ATOMIC_INC(&s->be->be_counters.failed_rewrites); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 499 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 500 | _HA_ATOMIC_INC(&sess->listener->counters->failed_rewrites); |
Christopher Faulet | e00d06c | 2019-12-16 17:18:42 +0100 | [diff] [blame] | 501 | if (objt_server(s->target)) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 502 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_rewrites); |
Christopher Faulet | e00d06c | 2019-12-16 17:18:42 +0100 | [diff] [blame] | 503 | |
Christopher Faulet | 333bf8c | 2020-01-22 14:38:05 +0100 | [diff] [blame] | 504 | if (!(s->txn->req.flags & HTTP_MSGF_SOFT_RW)) { |
Christopher Faulet | 333bf8c | 2020-01-22 14:38:05 +0100 | [diff] [blame] | 505 | if (!(s->flags & SF_ERR_MASK)) |
| 506 | s->flags |= SF_ERR_PRXCOND; |
Christopher Faulet | 692a6c2 | 2020-02-07 10:22:31 +0100 | [diff] [blame] | 507 | return ACT_RET_ERR; |
Christopher Faulet | 333bf8c | 2020-01-22 14:38:05 +0100 | [diff] [blame] | 508 | } |
Christopher Faulet | e00d06c | 2019-12-16 17:18:42 +0100 | [diff] [blame] | 509 | } |
| 510 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 511 | return ACT_RET_CONT; |
| 512 | } |
| 513 | |
| 514 | /* parse set-status action: |
| 515 | * This action accepts a single argument of type int representing |
| 516 | * an http status code. It returns ACT_RET_PRS_OK on success, |
| 517 | * ACT_RET_PRS_ERR on error. |
| 518 | */ |
| 519 | static enum act_parse_ret parse_http_set_status(const char **args, int *orig_arg, struct proxy *px, |
| 520 | struct act_rule *rule, char **err) |
| 521 | { |
| 522 | char *error; |
| 523 | |
| 524 | rule->action = ACT_CUSTOM; |
| 525 | rule->action_ptr = action_http_set_status; |
Christopher Faulet | 2eb5396 | 2020-01-14 14:47:34 +0100 | [diff] [blame] | 526 | rule->release_ptr = release_http_action; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 527 | |
| 528 | /* Check if an argument is available */ |
| 529 | if (!*args[*orig_arg]) { |
| 530 | memprintf(err, "expects 1 argument: <status>; or 3 arguments: <status> reason <fmt>"); |
| 531 | return ACT_RET_PRS_ERR; |
| 532 | } |
| 533 | |
| 534 | /* convert status code as integer */ |
Christopher Faulet | 96bff76 | 2019-12-17 13:46:18 +0100 | [diff] [blame] | 535 | rule->arg.http.i = strtol(args[*orig_arg], &error, 10); |
| 536 | 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] | 537 | memprintf(err, "expects an integer status code between 100 and 999"); |
| 538 | return ACT_RET_PRS_ERR; |
| 539 | } |
| 540 | |
| 541 | (*orig_arg)++; |
| 542 | |
| 543 | /* set custom reason string */ |
Christopher Faulet | 96bff76 | 2019-12-17 13:46:18 +0100 | [diff] [blame] | 544 | 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] | 545 | if (*args[*orig_arg] && strcmp(args[*orig_arg], "reason") == 0 && |
| 546 | (*args[*orig_arg + 1] && strcmp(args[*orig_arg + 1], "if") != 0 && strcmp(args[*orig_arg + 1], "unless") != 0)) { |
| 547 | (*orig_arg)++; |
Christopher Faulet | 96bff76 | 2019-12-17 13:46:18 +0100 | [diff] [blame] | 548 | rule->arg.http.str.ptr = strdup(args[*orig_arg]); |
| 549 | rule->arg.http.str.len = strlen(rule->arg.http.str.ptr); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 550 | (*orig_arg)++; |
| 551 | } |
| 552 | |
Christopher Faulet | c20b371 | 2020-01-27 15:51:56 +0100 | [diff] [blame] | 553 | LIST_INIT(&rule->arg.http.fmt); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 554 | return ACT_RET_PRS_OK; |
| 555 | } |
| 556 | |
| 557 | /* This function executes the "reject" HTTP action. It clears the request and |
| 558 | * response buffer without sending any response. It can be useful as an HTTP |
| 559 | * alternative to the silent-drop action to defend against DoS attacks, and may |
| 560 | * also be used with HTTP/2 to close a connection instead of just a stream. |
| 561 | * The txn status is unchanged, indicating no response was sent. The termination |
Christopher Faulet | 90d22a8 | 2020-03-06 11:18:39 +0100 | [diff] [blame] | 562 | * flags will indicate "PR". It always returns ACT_RET_ABRT. |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 563 | */ |
| 564 | static enum act_return http_action_reject(struct act_rule *rule, struct proxy *px, |
| 565 | struct session *sess, struct stream *s, int flags) |
| 566 | { |
Willy Tarreau | 0f9cd7b | 2019-01-31 19:02:43 +0100 | [diff] [blame] | 567 | si_must_kill_conn(chn_prod(&s->req)); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 568 | channel_abort(&s->req); |
| 569 | channel_abort(&s->res); |
Christopher Faulet | d4a824e | 2020-03-06 15:07:09 +0100 | [diff] [blame] | 570 | s->req.analysers &= AN_REQ_FLT_END; |
| 571 | s->res.analysers &= AN_RES_FLT_END; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 572 | |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 573 | _HA_ATOMIC_INC(&s->be->be_counters.denied_req); |
| 574 | _HA_ATOMIC_INC(&sess->fe->fe_counters.denied_req); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 575 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 576 | _HA_ATOMIC_INC(&sess->listener->counters->denied_req); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 577 | |
| 578 | if (!(s->flags & SF_ERR_MASK)) |
| 579 | s->flags |= SF_ERR_PRXCOND; |
| 580 | if (!(s->flags & SF_FINST_MASK)) |
| 581 | s->flags |= SF_FINST_R; |
| 582 | |
Christopher Faulet | 90d22a8 | 2020-03-06 11:18:39 +0100 | [diff] [blame] | 583 | return ACT_RET_ABRT; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 584 | } |
| 585 | |
| 586 | /* parse the "reject" action: |
| 587 | * This action takes no argument and returns ACT_RET_PRS_OK on success, |
| 588 | * ACT_RET_PRS_ERR on error. |
| 589 | */ |
| 590 | static enum act_parse_ret parse_http_action_reject(const char **args, int *orig_arg, struct proxy *px, |
| 591 | struct act_rule *rule, char **err) |
| 592 | { |
| 593 | rule->action = ACT_CUSTOM; |
| 594 | rule->action_ptr = http_action_reject; |
| 595 | return ACT_RET_PRS_OK; |
| 596 | } |
| 597 | |
Olivier Houchard | 602bf7d | 2019-05-10 13:59:15 +0200 | [diff] [blame] | 598 | /* This function executes the "disable-l7-retry" HTTP action. |
| 599 | * It disables L7 retries (all retry except for a connection failure). This |
| 600 | * can be useful for example to avoid retrying on POST requests. |
| 601 | * It just removes the L7 retry flag on the stream_interface, and always |
| 602 | * return ACT_RET_CONT; |
| 603 | */ |
| 604 | static enum act_return http_req_disable_l7_retry(struct act_rule *rule, struct proxy *px, |
| 605 | struct session *sess, struct stream *s, int flags) |
| 606 | { |
| 607 | struct stream_interface *si = &s->si[1]; |
| 608 | |
| 609 | /* In theory, the SI_FL_L7_RETRY flags isn't set at this point, but |
| 610 | * let's be future-proof and remove it anyway. |
| 611 | */ |
| 612 | si->flags &= ~SI_FL_L7_RETRY; |
| 613 | si->flags |= SI_FL_D_L7_RETRY; |
| 614 | return ACT_RET_CONT; |
| 615 | } |
| 616 | |
| 617 | /* parse the "disable-l7-retry" action: |
| 618 | * This action takes no argument and returns ACT_RET_PRS_OK on success, |
| 619 | * ACT_RET_PRS_ERR on error. |
| 620 | */ |
| 621 | static enum act_parse_ret parse_http_req_disable_l7_retry(const char **args, |
| 622 | int *orig_args, struct proxy *px, |
| 623 | struct act_rule *rule, char **err) |
| 624 | { |
| 625 | rule->action = ACT_CUSTOM; |
| 626 | rule->action_ptr = http_req_disable_l7_retry; |
| 627 | return ACT_RET_PRS_OK; |
| 628 | } |
| 629 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 630 | /* This function executes the "capture" action. It executes a fetch expression, |
| 631 | * turns the result into a string and puts it in a capture slot. It always |
| 632 | * returns 1. If an error occurs the action is cancelled, but the rule |
| 633 | * processing continues. |
| 634 | */ |
| 635 | static enum act_return http_action_req_capture(struct act_rule *rule, struct proxy *px, |
| 636 | struct session *sess, struct stream *s, int flags) |
| 637 | { |
| 638 | struct sample *key; |
| 639 | struct cap_hdr *h = rule->arg.cap.hdr; |
| 640 | char **cap = s->req_cap; |
| 641 | int len; |
| 642 | |
| 643 | key = sample_fetch_as_type(s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.cap.expr, SMP_T_STR); |
| 644 | if (!key) |
| 645 | return ACT_RET_CONT; |
| 646 | |
| 647 | if (cap[h->index] == NULL) |
| 648 | cap[h->index] = pool_alloc(h->pool); |
| 649 | |
| 650 | if (cap[h->index] == NULL) /* no more capture memory */ |
| 651 | return ACT_RET_CONT; |
| 652 | |
| 653 | len = key->data.u.str.data; |
| 654 | if (len > h->len) |
| 655 | len = h->len; |
| 656 | |
| 657 | memcpy(cap[h->index], key->data.u.str.area, len); |
| 658 | cap[h->index][len] = 0; |
| 659 | return ACT_RET_CONT; |
| 660 | } |
| 661 | |
| 662 | /* This function executes the "capture" action and store the result in a |
| 663 | * capture slot if exists. It executes a fetch expression, turns the result |
| 664 | * into a string and puts it in a capture slot. It always returns 1. If an |
| 665 | * error occurs the action is cancelled, but the rule processing continues. |
| 666 | */ |
| 667 | static enum act_return http_action_req_capture_by_id(struct act_rule *rule, struct proxy *px, |
| 668 | struct session *sess, struct stream *s, int flags) |
| 669 | { |
| 670 | struct sample *key; |
| 671 | struct cap_hdr *h; |
| 672 | char **cap = s->req_cap; |
| 673 | struct proxy *fe = strm_fe(s); |
| 674 | int len; |
| 675 | int i; |
| 676 | |
| 677 | /* Look for the original configuration. */ |
| 678 | for (h = fe->req_cap, i = fe->nb_req_cap - 1; |
| 679 | h != NULL && i != rule->arg.capid.idx ; |
| 680 | i--, h = h->next); |
| 681 | if (!h) |
| 682 | return ACT_RET_CONT; |
| 683 | |
| 684 | key = sample_fetch_as_type(s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.capid.expr, SMP_T_STR); |
| 685 | if (!key) |
| 686 | return ACT_RET_CONT; |
| 687 | |
| 688 | if (cap[h->index] == NULL) |
| 689 | cap[h->index] = pool_alloc(h->pool); |
| 690 | |
| 691 | if (cap[h->index] == NULL) /* no more capture memory */ |
| 692 | return ACT_RET_CONT; |
| 693 | |
| 694 | len = key->data.u.str.data; |
| 695 | if (len > h->len) |
| 696 | len = h->len; |
| 697 | |
| 698 | memcpy(cap[h->index], key->data.u.str.area, len); |
| 699 | cap[h->index][len] = 0; |
| 700 | return ACT_RET_CONT; |
| 701 | } |
| 702 | |
| 703 | /* Check an "http-request capture" action. |
| 704 | * |
| 705 | * The function returns 1 in success case, otherwise, it returns 0 and err is |
| 706 | * filled. |
| 707 | */ |
| 708 | static int check_http_req_capture(struct act_rule *rule, struct proxy *px, char **err) |
| 709 | { |
| 710 | if (rule->action_ptr != http_action_req_capture_by_id) |
| 711 | return 1; |
| 712 | |
Baptiste Assmann | 19a69b3 | 2020-01-16 14:34:22 +0100 | [diff] [blame] | 713 | /* capture slots can only be declared in frontends, so we can't check their |
| 714 | * existence in backends at configuration parsing step |
| 715 | */ |
| 716 | if (px->cap & PR_CAP_FE && rule->arg.capid.idx >= px->nb_req_cap) { |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 717 | memprintf(err, "unable to find capture id '%d' referenced by http-request capture rule", |
| 718 | rule->arg.capid.idx); |
| 719 | return 0; |
| 720 | } |
| 721 | |
| 722 | return 1; |
| 723 | } |
| 724 | |
Christopher Faulet | 2eb5396 | 2020-01-14 14:47:34 +0100 | [diff] [blame] | 725 | /* Release memory allocate by an http capture action */ |
| 726 | static void release_http_capture(struct act_rule *rule) |
| 727 | { |
| 728 | if (rule->action_ptr == http_action_req_capture) |
| 729 | release_sample_expr(rule->arg.cap.expr); |
| 730 | else |
| 731 | release_sample_expr(rule->arg.capid.expr); |
| 732 | } |
| 733 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 734 | /* parse an "http-request capture" action. It takes a single argument which is |
| 735 | * a sample fetch expression. It stores the expression into arg->act.p[0] and |
| 736 | * the allocated hdr_cap struct or the preallocated "id" into arg->act.p[1]. |
| 737 | * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error. |
| 738 | */ |
| 739 | static enum act_parse_ret parse_http_req_capture(const char **args, int *orig_arg, struct proxy *px, |
| 740 | struct act_rule *rule, char **err) |
| 741 | { |
| 742 | struct sample_expr *expr; |
| 743 | struct cap_hdr *hdr; |
| 744 | int cur_arg; |
| 745 | int len = 0; |
| 746 | |
| 747 | for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++) |
| 748 | if (strcmp(args[cur_arg], "if") == 0 || |
| 749 | strcmp(args[cur_arg], "unless") == 0) |
| 750 | break; |
| 751 | |
| 752 | if (cur_arg < *orig_arg + 3) { |
| 753 | memprintf(err, "expects <expression> [ 'len' <length> | id <idx> ]"); |
| 754 | return ACT_RET_PRS_ERR; |
| 755 | } |
| 756 | |
| 757 | cur_arg = *orig_arg; |
Willy Tarreau | e3b57bf | 2020-02-14 16:50:14 +0100 | [diff] [blame] | 758 | expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args, NULL); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 759 | if (!expr) |
| 760 | return ACT_RET_PRS_ERR; |
| 761 | |
| 762 | if (!(expr->fetch->val & SMP_VAL_FE_HRQ_HDR)) { |
| 763 | memprintf(err, |
| 764 | "fetch method '%s' extracts information from '%s', none of which is available here", |
| 765 | args[cur_arg-1], sample_src_names(expr->fetch->use)); |
Christopher Faulet | 1337b32 | 2020-01-14 14:50:55 +0100 | [diff] [blame] | 766 | release_sample_expr(expr); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 767 | return ACT_RET_PRS_ERR; |
| 768 | } |
| 769 | |
| 770 | if (!args[cur_arg] || !*args[cur_arg]) { |
| 771 | memprintf(err, "expects 'len or 'id'"); |
Christopher Faulet | 1337b32 | 2020-01-14 14:50:55 +0100 | [diff] [blame] | 772 | release_sample_expr(expr); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 773 | return ACT_RET_PRS_ERR; |
| 774 | } |
| 775 | |
| 776 | if (strcmp(args[cur_arg], "len") == 0) { |
| 777 | cur_arg++; |
| 778 | |
| 779 | if (!(px->cap & PR_CAP_FE)) { |
| 780 | memprintf(err, "proxy '%s' has no frontend capability", px->id); |
Christopher Faulet | 1337b32 | 2020-01-14 14:50:55 +0100 | [diff] [blame] | 781 | release_sample_expr(expr); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 782 | return ACT_RET_PRS_ERR; |
| 783 | } |
| 784 | |
| 785 | px->conf.args.ctx = ARGC_CAP; |
| 786 | |
| 787 | if (!args[cur_arg]) { |
| 788 | memprintf(err, "missing length value"); |
Christopher Faulet | 1337b32 | 2020-01-14 14:50:55 +0100 | [diff] [blame] | 789 | release_sample_expr(expr); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 790 | return ACT_RET_PRS_ERR; |
| 791 | } |
| 792 | /* we copy the table name for now, it will be resolved later */ |
| 793 | len = atoi(args[cur_arg]); |
| 794 | if (len <= 0) { |
| 795 | memprintf(err, "length must be > 0"); |
Christopher Faulet | 1337b32 | 2020-01-14 14:50:55 +0100 | [diff] [blame] | 796 | release_sample_expr(expr); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 797 | return ACT_RET_PRS_ERR; |
| 798 | } |
| 799 | cur_arg++; |
| 800 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 801 | hdr = calloc(1, sizeof(*hdr)); |
| 802 | hdr->next = px->req_cap; |
| 803 | hdr->name = NULL; /* not a header capture */ |
| 804 | hdr->namelen = 0; |
| 805 | hdr->len = len; |
| 806 | hdr->pool = create_pool("caphdr", hdr->len + 1, MEM_F_SHARED); |
| 807 | hdr->index = px->nb_req_cap++; |
| 808 | |
| 809 | px->req_cap = hdr; |
| 810 | px->to_log |= LW_REQHDR; |
| 811 | |
| 812 | rule->action = ACT_CUSTOM; |
| 813 | rule->action_ptr = http_action_req_capture; |
Christopher Faulet | 2eb5396 | 2020-01-14 14:47:34 +0100 | [diff] [blame] | 814 | rule->release_ptr = release_http_capture; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 815 | rule->arg.cap.expr = expr; |
| 816 | rule->arg.cap.hdr = hdr; |
| 817 | } |
| 818 | |
| 819 | else if (strcmp(args[cur_arg], "id") == 0) { |
| 820 | int id; |
| 821 | char *error; |
| 822 | |
| 823 | cur_arg++; |
| 824 | |
| 825 | if (!args[cur_arg]) { |
| 826 | memprintf(err, "missing id value"); |
Christopher Faulet | 1337b32 | 2020-01-14 14:50:55 +0100 | [diff] [blame] | 827 | release_sample_expr(expr); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 828 | return ACT_RET_PRS_ERR; |
| 829 | } |
| 830 | |
| 831 | id = strtol(args[cur_arg], &error, 10); |
| 832 | if (*error != '\0') { |
| 833 | memprintf(err, "cannot parse id '%s'", args[cur_arg]); |
Christopher Faulet | 1337b32 | 2020-01-14 14:50:55 +0100 | [diff] [blame] | 834 | release_sample_expr(expr); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 835 | return ACT_RET_PRS_ERR; |
| 836 | } |
| 837 | cur_arg++; |
| 838 | |
| 839 | px->conf.args.ctx = ARGC_CAP; |
| 840 | |
| 841 | rule->action = ACT_CUSTOM; |
| 842 | rule->action_ptr = http_action_req_capture_by_id; |
| 843 | rule->check_ptr = check_http_req_capture; |
Christopher Faulet | 2eb5396 | 2020-01-14 14:47:34 +0100 | [diff] [blame] | 844 | rule->release_ptr = release_http_capture; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 845 | rule->arg.capid.expr = expr; |
| 846 | rule->arg.capid.idx = id; |
| 847 | } |
| 848 | |
| 849 | else { |
| 850 | memprintf(err, "expects 'len' or 'id', found '%s'", args[cur_arg]); |
Christopher Faulet | 1337b32 | 2020-01-14 14:50:55 +0100 | [diff] [blame] | 851 | release_sample_expr(expr); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 852 | return ACT_RET_PRS_ERR; |
| 853 | } |
| 854 | |
| 855 | *orig_arg = cur_arg; |
| 856 | return ACT_RET_PRS_OK; |
| 857 | } |
| 858 | |
| 859 | /* This function executes the "capture" action and store the result in a |
| 860 | * capture slot if exists. It executes a fetch expression, turns the result |
| 861 | * into a string and puts it in a capture slot. It always returns 1. If an |
| 862 | * error occurs the action is cancelled, but the rule processing continues. |
| 863 | */ |
| 864 | static enum act_return http_action_res_capture_by_id(struct act_rule *rule, struct proxy *px, |
| 865 | struct session *sess, struct stream *s, int flags) |
| 866 | { |
| 867 | struct sample *key; |
| 868 | struct cap_hdr *h; |
| 869 | char **cap = s->res_cap; |
| 870 | struct proxy *fe = strm_fe(s); |
| 871 | int len; |
| 872 | int i; |
| 873 | |
| 874 | /* Look for the original configuration. */ |
| 875 | for (h = fe->rsp_cap, i = fe->nb_rsp_cap - 1; |
| 876 | h != NULL && i != rule->arg.capid.idx ; |
| 877 | i--, h = h->next); |
| 878 | if (!h) |
| 879 | return ACT_RET_CONT; |
| 880 | |
| 881 | key = sample_fetch_as_type(s->be, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL, rule->arg.capid.expr, SMP_T_STR); |
| 882 | if (!key) |
| 883 | return ACT_RET_CONT; |
| 884 | |
| 885 | if (cap[h->index] == NULL) |
| 886 | cap[h->index] = pool_alloc(h->pool); |
| 887 | |
| 888 | if (cap[h->index] == NULL) /* no more capture memory */ |
| 889 | return ACT_RET_CONT; |
| 890 | |
| 891 | len = key->data.u.str.data; |
| 892 | if (len > h->len) |
| 893 | len = h->len; |
| 894 | |
| 895 | memcpy(cap[h->index], key->data.u.str.area, len); |
| 896 | cap[h->index][len] = 0; |
| 897 | return ACT_RET_CONT; |
| 898 | } |
| 899 | |
| 900 | /* Check an "http-response capture" action. |
| 901 | * |
| 902 | * The function returns 1 in success case, otherwise, it returns 0 and err is |
| 903 | * filled. |
| 904 | */ |
| 905 | static int check_http_res_capture(struct act_rule *rule, struct proxy *px, char **err) |
| 906 | { |
| 907 | if (rule->action_ptr != http_action_res_capture_by_id) |
| 908 | return 1; |
| 909 | |
Tim Duesterhus | f3f4aa0 | 2020-07-03 13:43:42 +0200 | [diff] [blame] | 910 | /* capture slots can only be declared in frontends, so we can't check their |
| 911 | * existence in backends at configuration parsing step |
| 912 | */ |
| 913 | if (px->cap & PR_CAP_FE && rule->arg.capid.idx >= px->nb_rsp_cap) { |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 914 | memprintf(err, "unable to find capture id '%d' referenced by http-response capture rule", |
| 915 | rule->arg.capid.idx); |
| 916 | return 0; |
| 917 | } |
| 918 | |
| 919 | return 1; |
| 920 | } |
| 921 | |
| 922 | /* parse an "http-response capture" action. It takes a single argument which is |
| 923 | * a sample fetch expression. It stores the expression into arg->act.p[0] and |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 924 | * the allocated hdr_cap struct of the preallocated id into arg->act.p[1]. |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 925 | * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error. |
| 926 | */ |
| 927 | static enum act_parse_ret parse_http_res_capture(const char **args, int *orig_arg, struct proxy *px, |
| 928 | struct act_rule *rule, char **err) |
| 929 | { |
| 930 | struct sample_expr *expr; |
| 931 | int cur_arg; |
| 932 | int id; |
| 933 | char *error; |
| 934 | |
| 935 | for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++) |
| 936 | if (strcmp(args[cur_arg], "if") == 0 || |
| 937 | strcmp(args[cur_arg], "unless") == 0) |
| 938 | break; |
| 939 | |
| 940 | if (cur_arg < *orig_arg + 3) { |
| 941 | memprintf(err, "expects <expression> id <idx>"); |
| 942 | return ACT_RET_PRS_ERR; |
| 943 | } |
| 944 | |
| 945 | cur_arg = *orig_arg; |
Willy Tarreau | e3b57bf | 2020-02-14 16:50:14 +0100 | [diff] [blame] | 946 | expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args, NULL); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 947 | if (!expr) |
| 948 | return ACT_RET_PRS_ERR; |
| 949 | |
| 950 | if (!(expr->fetch->val & SMP_VAL_FE_HRS_HDR)) { |
| 951 | memprintf(err, |
| 952 | "fetch method '%s' extracts information from '%s', none of which is available here", |
| 953 | args[cur_arg-1], sample_src_names(expr->fetch->use)); |
Christopher Faulet | 1337b32 | 2020-01-14 14:50:55 +0100 | [diff] [blame] | 954 | release_sample_expr(expr); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 955 | return ACT_RET_PRS_ERR; |
| 956 | } |
| 957 | |
| 958 | if (!args[cur_arg] || !*args[cur_arg]) { |
| 959 | memprintf(err, "expects 'id'"); |
Christopher Faulet | 1337b32 | 2020-01-14 14:50:55 +0100 | [diff] [blame] | 960 | release_sample_expr(expr); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 961 | return ACT_RET_PRS_ERR; |
| 962 | } |
| 963 | |
| 964 | if (strcmp(args[cur_arg], "id") != 0) { |
| 965 | memprintf(err, "expects 'id', found '%s'", args[cur_arg]); |
Christopher Faulet | 1337b32 | 2020-01-14 14:50:55 +0100 | [diff] [blame] | 966 | release_sample_expr(expr); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 967 | return ACT_RET_PRS_ERR; |
| 968 | } |
| 969 | |
| 970 | cur_arg++; |
| 971 | |
| 972 | if (!args[cur_arg]) { |
| 973 | memprintf(err, "missing id value"); |
Christopher Faulet | 1337b32 | 2020-01-14 14:50:55 +0100 | [diff] [blame] | 974 | release_sample_expr(expr); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 975 | return ACT_RET_PRS_ERR; |
| 976 | } |
| 977 | |
| 978 | id = strtol(args[cur_arg], &error, 10); |
| 979 | if (*error != '\0') { |
| 980 | memprintf(err, "cannot parse id '%s'", args[cur_arg]); |
Christopher Faulet | 1337b32 | 2020-01-14 14:50:55 +0100 | [diff] [blame] | 981 | release_sample_expr(expr); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 982 | return ACT_RET_PRS_ERR; |
| 983 | } |
| 984 | cur_arg++; |
| 985 | |
| 986 | px->conf.args.ctx = ARGC_CAP; |
| 987 | |
| 988 | rule->action = ACT_CUSTOM; |
| 989 | rule->action_ptr = http_action_res_capture_by_id; |
| 990 | rule->check_ptr = check_http_res_capture; |
Christopher Faulet | 2eb5396 | 2020-01-14 14:47:34 +0100 | [diff] [blame] | 991 | rule->release_ptr = release_http_capture; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 992 | rule->arg.capid.expr = expr; |
| 993 | rule->arg.capid.idx = id; |
| 994 | |
| 995 | *orig_arg = cur_arg; |
| 996 | return ACT_RET_PRS_OK; |
| 997 | } |
| 998 | |
Christopher Faulet | 81e2017 | 2019-12-12 16:40:30 +0100 | [diff] [blame] | 999 | /* Parse a "allow" action for a request or a response rule. It takes no argument. It |
| 1000 | * returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error. |
| 1001 | */ |
| 1002 | static enum act_parse_ret parse_http_allow(const char **args, int *orig_arg, struct proxy *px, |
| 1003 | struct act_rule *rule, char **err) |
| 1004 | { |
| 1005 | rule->action = ACT_ACTION_ALLOW; |
Christopher Faulet | 245cf79 | 2019-12-18 14:58:12 +0100 | [diff] [blame] | 1006 | rule->flags |= ACT_FLAG_FINAL; |
Christopher Faulet | 81e2017 | 2019-12-12 16:40:30 +0100 | [diff] [blame] | 1007 | return ACT_RET_PRS_OK; |
| 1008 | } |
| 1009 | |
Christopher Faulet | e0fca29 | 2020-01-13 21:49:03 +0100 | [diff] [blame] | 1010 | /* Parse "deny" or "tarpit" actions for a request rule or "deny" action for a |
Christopher Faulet | 5cb513a | 2020-05-13 17:56:56 +0200 | [diff] [blame] | 1011 | * response rule. It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on |
| 1012 | * error. It relies on http_parse_http_reply() to set |
| 1013 | * <.arg.http_reply>. |
Christopher Faulet | 81e2017 | 2019-12-12 16:40:30 +0100 | [diff] [blame] | 1014 | */ |
Christopher Faulet | e0fca29 | 2020-01-13 21:49:03 +0100 | [diff] [blame] | 1015 | static enum act_parse_ret parse_http_deny(const char **args, int *orig_arg, struct proxy *px, |
| 1016 | struct act_rule *rule, char **err) |
Christopher Faulet | 81e2017 | 2019-12-12 16:40:30 +0100 | [diff] [blame] | 1017 | { |
Christopher Faulet | 5cb513a | 2020-05-13 17:56:56 +0200 | [diff] [blame] | 1018 | int default_status; |
| 1019 | int cur_arg, arg = 0; |
Christopher Faulet | 81e2017 | 2019-12-12 16:40:30 +0100 | [diff] [blame] | 1020 | |
| 1021 | cur_arg = *orig_arg; |
Christopher Faulet | e0fca29 | 2020-01-13 21:49:03 +0100 | [diff] [blame] | 1022 | if (rule->from == ACT_F_HTTP_REQ) { |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 1023 | if (strcmp(args[cur_arg - 1], "tarpit") == 0) { |
Christopher Faulet | e0fca29 | 2020-01-13 21:49:03 +0100 | [diff] [blame] | 1024 | rule->action = ACT_HTTP_REQ_TARPIT; |
Christopher Faulet | 5cb513a | 2020-05-13 17:56:56 +0200 | [diff] [blame] | 1025 | default_status = 500; |
Christopher Faulet | 81e2017 | 2019-12-12 16:40:30 +0100 | [diff] [blame] | 1026 | } |
Christopher Faulet | e0fca29 | 2020-01-13 21:49:03 +0100 | [diff] [blame] | 1027 | else { |
| 1028 | rule->action = ACT_ACTION_DENY; |
Christopher Faulet | 5cb513a | 2020-05-13 17:56:56 +0200 | [diff] [blame] | 1029 | default_status = 403; |
Christopher Faulet | 81e2017 | 2019-12-12 16:40:30 +0100 | [diff] [blame] | 1030 | } |
Christopher Faulet | 81e2017 | 2019-12-12 16:40:30 +0100 | [diff] [blame] | 1031 | } |
Christopher Faulet | e0fca29 | 2020-01-13 21:49:03 +0100 | [diff] [blame] | 1032 | else { |
Christopher Faulet | 554c0eb | 2020-01-14 12:00:28 +0100 | [diff] [blame] | 1033 | rule->action = ACT_ACTION_DENY; |
Christopher Faulet | 5cb513a | 2020-05-13 17:56:56 +0200 | [diff] [blame] | 1034 | default_status = 502; |
Christopher Faulet | e0fca29 | 2020-01-13 21:49:03 +0100 | [diff] [blame] | 1035 | } |
Christopher Faulet | 040c8cd | 2020-01-13 16:43:45 +0100 | [diff] [blame] | 1036 | |
Christopher Faulet | 5cb513a | 2020-05-13 17:56:56 +0200 | [diff] [blame] | 1037 | /* If no args or only a deny_status specified, fallback on the legacy |
| 1038 | * mode and use default error files despite the fact that |
| 1039 | * default-errorfiles is not used. Otherwise, parse an http reply. |
| 1040 | */ |
Christopher Faulet | 040c8cd | 2020-01-13 16:43:45 +0100 | [diff] [blame] | 1041 | |
Christopher Faulet | 5cb513a | 2020-05-13 17:56:56 +0200 | [diff] [blame] | 1042 | /* Prepare parsing of log-format strings */ |
| 1043 | px->conf.args.ctx = ((rule->from == ACT_F_HTTP_REQ) ? ARGC_HRQ : ARGC_HRS); |
Christopher Faulet | 554c0eb | 2020-01-14 12:00:28 +0100 | [diff] [blame] | 1044 | |
Christopher Faulet | 9467f18 | 2020-06-30 09:32:01 +0200 | [diff] [blame] | 1045 | if (!*(args[cur_arg]) || strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) { |
Christopher Faulet | 5cb513a | 2020-05-13 17:56:56 +0200 | [diff] [blame] | 1046 | rule->arg.http_reply = http_parse_http_reply((const char *[]){"default-errorfiles", ""}, &arg, px, default_status, err); |
| 1047 | goto end; |
Christopher Faulet | 554c0eb | 2020-01-14 12:00:28 +0100 | [diff] [blame] | 1048 | } |
Christopher Faulet | 5cb513a | 2020-05-13 17:56:56 +0200 | [diff] [blame] | 1049 | |
| 1050 | if (strcmp(args[cur_arg], "deny_status") == 0) { |
Christopher Faulet | 9467f18 | 2020-06-30 09:32:01 +0200 | [diff] [blame] | 1051 | if (!*(args[cur_arg+2]) || strcmp(args[cur_arg+2], "if") == 0 || strcmp(args[cur_arg+2], "unless") == 0) { |
Christopher Faulet | 5cb513a | 2020-05-13 17:56:56 +0200 | [diff] [blame] | 1052 | rule->arg.http_reply = http_parse_http_reply((const char *[]){"status", args[cur_arg+1], "default-errorfiles", ""}, |
| 1053 | &arg, px, default_status, err); |
| 1054 | *orig_arg += 2; |
| 1055 | goto end; |
Christopher Faulet | 554c0eb | 2020-01-14 12:00:28 +0100 | [diff] [blame] | 1056 | } |
Christopher Faulet | 5cb513a | 2020-05-13 17:56:56 +0200 | [diff] [blame] | 1057 | args[cur_arg] += 5; /* skip "deny_" for the parsing */ |
Christopher Faulet | 554c0eb | 2020-01-14 12:00:28 +0100 | [diff] [blame] | 1058 | } |
| 1059 | |
Christopher Faulet | 5cb513a | 2020-05-13 17:56:56 +0200 | [diff] [blame] | 1060 | rule->arg.http_reply = http_parse_http_reply(args, orig_arg, px, default_status, err); |
Christopher Faulet | 554c0eb | 2020-01-14 12:00:28 +0100 | [diff] [blame] | 1061 | |
Christopher Faulet | 5cb513a | 2020-05-13 17:56:56 +0200 | [diff] [blame] | 1062 | end: |
| 1063 | if (!rule->arg.http_reply) |
| 1064 | return ACT_RET_PRS_ERR; |
| 1065 | |
| 1066 | rule->flags |= ACT_FLAG_FINAL; |
| 1067 | rule->check_ptr = check_act_http_reply; |
| 1068 | rule->release_ptr = release_act_http_reply; |
Christopher Faulet | 81e2017 | 2019-12-12 16:40:30 +0100 | [diff] [blame] | 1069 | return ACT_RET_PRS_OK; |
| 1070 | } |
| 1071 | |
Christopher Faulet | b304883 | 2020-05-27 15:26:43 +0200 | [diff] [blame] | 1072 | |
| 1073 | /* This function executes a auth action. It builds an 401/407 HTX message using |
| 1074 | * the corresponding proxy's error message. On success, it returns |
| 1075 | * ACT_RET_ABRT. If an error occurs ACT_RET_ERR is returned. |
| 1076 | */ |
| 1077 | static enum act_return http_action_auth(struct act_rule *rule, struct proxy *px, |
| 1078 | struct session *sess, struct stream *s, int flags) |
| 1079 | { |
| 1080 | struct channel *req = &s->req; |
| 1081 | struct channel *res = &s->res; |
| 1082 | struct htx *htx = htx_from_buf(&res->buf); |
| 1083 | struct http_reply *reply; |
| 1084 | const char *auth_realm; |
| 1085 | struct http_hdr_ctx ctx; |
| 1086 | struct ist hdr; |
| 1087 | |
| 1088 | /* Auth might be performed on regular http-req rules as well as on stats */ |
| 1089 | auth_realm = rule->arg.http.str.ptr; |
| 1090 | if (!auth_realm) { |
| 1091 | if (px->uri_auth && s->current_rule_list == &px->uri_auth->http_req_rules) |
| 1092 | auth_realm = STATS_DEFAULT_REALM; |
| 1093 | else |
| 1094 | auth_realm = px->id; |
| 1095 | } |
| 1096 | |
| 1097 | if (!(s->txn->flags & TX_USE_PX_CONN)) { |
| 1098 | s->txn->status = 401; |
| 1099 | hdr = ist("WWW-Authenticate"); |
| 1100 | } |
| 1101 | else { |
| 1102 | s->txn->status = 407; |
| 1103 | hdr = ist("Proxy-Authenticate"); |
| 1104 | } |
| 1105 | reply = http_error_message(s); |
| 1106 | channel_htx_truncate(res, htx); |
| 1107 | |
| 1108 | if (chunk_printf(&trash, "Basic realm=\"%s\"", auth_realm) == -1) |
| 1109 | goto fail; |
| 1110 | |
| 1111 | /* Write the generic 40x message */ |
| 1112 | if (http_reply_to_htx(s, htx, reply) == -1) |
| 1113 | goto fail; |
| 1114 | |
| 1115 | /* Remove all existing occurrences of the XXX-Authenticate header */ |
| 1116 | ctx.blk = NULL; |
| 1117 | while (http_find_header(htx, hdr, &ctx, 1)) |
| 1118 | http_remove_header(htx, &ctx); |
| 1119 | |
| 1120 | /* Now a the right XXX-Authenticate header */ |
| 1121 | if (!http_add_header(htx, hdr, ist2(b_orig(&trash), b_data(&trash)))) |
| 1122 | goto fail; |
| 1123 | |
| 1124 | /* Finally forward the reply */ |
| 1125 | htx_to_buf(htx, &res->buf); |
| 1126 | if (!http_forward_proxy_resp(s, 1)) |
| 1127 | goto fail; |
| 1128 | |
| 1129 | /* Note: Only eval on the request */ |
| 1130 | s->logs.tv_request = now; |
| 1131 | req->analysers &= AN_REQ_FLT_END; |
| 1132 | |
| 1133 | if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */ |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1134 | _HA_ATOMIC_INC(&s->sess->fe->fe_counters.intercepted_req); |
Christopher Faulet | b304883 | 2020-05-27 15:26:43 +0200 | [diff] [blame] | 1135 | |
| 1136 | if (!(s->flags & SF_ERR_MASK)) |
| 1137 | s->flags |= SF_ERR_LOCAL; |
| 1138 | if (!(s->flags & SF_FINST_MASK)) |
| 1139 | s->flags |= SF_FINST_R; |
| 1140 | |
| 1141 | stream_inc_http_err_ctr(s); |
| 1142 | return ACT_RET_ABRT; |
| 1143 | |
| 1144 | fail: |
| 1145 | /* If an error occurred, remove the incomplete HTTP response from the |
| 1146 | * buffer */ |
| 1147 | channel_htx_truncate(res, htx); |
| 1148 | return ACT_RET_ERR; |
| 1149 | } |
| 1150 | |
Christopher Faulet | 81e2017 | 2019-12-12 16:40:30 +0100 | [diff] [blame] | 1151 | /* Parse a "auth" action. It may take 2 optional arguments to define a "realm" |
| 1152 | * parameter. It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error. |
| 1153 | */ |
| 1154 | static enum act_parse_ret parse_http_auth(const char **args, int *orig_arg, struct proxy *px, |
| 1155 | struct act_rule *rule, char **err) |
| 1156 | { |
| 1157 | int cur_arg; |
| 1158 | |
Christopher Faulet | b304883 | 2020-05-27 15:26:43 +0200 | [diff] [blame] | 1159 | rule->action = ACT_CUSTOM; |
Christopher Faulet | 245cf79 | 2019-12-18 14:58:12 +0100 | [diff] [blame] | 1160 | rule->flags |= ACT_FLAG_FINAL; |
Christopher Faulet | b304883 | 2020-05-27 15:26:43 +0200 | [diff] [blame] | 1161 | rule->action_ptr = http_action_auth; |
Christopher Faulet | 2eb5396 | 2020-01-14 14:47:34 +0100 | [diff] [blame] | 1162 | rule->release_ptr = release_http_action; |
Christopher Faulet | 81e2017 | 2019-12-12 16:40:30 +0100 | [diff] [blame] | 1163 | |
| 1164 | cur_arg = *orig_arg; |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 1165 | if (strcmp(args[cur_arg], "realm") == 0) { |
Christopher Faulet | 81e2017 | 2019-12-12 16:40:30 +0100 | [diff] [blame] | 1166 | cur_arg++; |
| 1167 | if (!*args[cur_arg]) { |
| 1168 | memprintf(err, "missing realm value.\n"); |
| 1169 | return ACT_RET_PRS_ERR; |
| 1170 | } |
Christopher Faulet | 96bff76 | 2019-12-17 13:46:18 +0100 | [diff] [blame] | 1171 | rule->arg.http.str.ptr = strdup(args[cur_arg]); |
| 1172 | rule->arg.http.str.len = strlen(rule->arg.http.str.ptr); |
Christopher Faulet | 81e2017 | 2019-12-12 16:40:30 +0100 | [diff] [blame] | 1173 | cur_arg++; |
| 1174 | } |
| 1175 | |
Christopher Faulet | c20b371 | 2020-01-27 15:51:56 +0100 | [diff] [blame] | 1176 | LIST_INIT(&rule->arg.http.fmt); |
Christopher Faulet | 81e2017 | 2019-12-12 16:40:30 +0100 | [diff] [blame] | 1177 | *orig_arg = cur_arg; |
| 1178 | return ACT_RET_PRS_OK; |
| 1179 | } |
| 1180 | |
| 1181 | /* Parse a "set-nice" action. It takes the nice value as argument. It returns |
| 1182 | * ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error. |
| 1183 | */ |
| 1184 | static enum act_parse_ret parse_http_set_nice(const char **args, int *orig_arg, struct proxy *px, |
| 1185 | struct act_rule *rule, char **err) |
| 1186 | { |
| 1187 | int cur_arg; |
| 1188 | |
| 1189 | rule->action = ACT_HTTP_SET_NICE; |
| 1190 | |
| 1191 | cur_arg = *orig_arg; |
| 1192 | if (!*args[cur_arg]) { |
| 1193 | memprintf(err, "expects exactly 1 argument (integer value)"); |
| 1194 | return ACT_RET_PRS_ERR; |
| 1195 | } |
Christopher Faulet | 96bff76 | 2019-12-17 13:46:18 +0100 | [diff] [blame] | 1196 | rule->arg.http.i = atoi(args[cur_arg]); |
| 1197 | if (rule->arg.http.i < -1024) |
| 1198 | rule->arg.http.i = -1024; |
| 1199 | else if (rule->arg.http.i > 1024) |
| 1200 | rule->arg.http.i = 1024; |
Christopher Faulet | 81e2017 | 2019-12-12 16:40:30 +0100 | [diff] [blame] | 1201 | |
Christopher Faulet | c20b371 | 2020-01-27 15:51:56 +0100 | [diff] [blame] | 1202 | LIST_INIT(&rule->arg.http.fmt); |
Christopher Faulet | 81e2017 | 2019-12-12 16:40:30 +0100 | [diff] [blame] | 1203 | *orig_arg = cur_arg + 1; |
| 1204 | return ACT_RET_PRS_OK; |
| 1205 | } |
| 1206 | |
| 1207 | /* Parse a "set-tos" action. It takes the TOS value as argument. It returns |
| 1208 | * ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error. |
| 1209 | */ |
| 1210 | static enum act_parse_ret parse_http_set_tos(const char **args, int *orig_arg, struct proxy *px, |
| 1211 | struct act_rule *rule, char **err) |
| 1212 | { |
| 1213 | #ifdef IP_TOS |
| 1214 | char *endp; |
| 1215 | int cur_arg; |
| 1216 | |
| 1217 | rule->action = ACT_HTTP_SET_TOS; |
| 1218 | |
| 1219 | cur_arg = *orig_arg; |
| 1220 | if (!*args[cur_arg]) { |
| 1221 | memprintf(err, "expects exactly 1 argument (integer/hex value)"); |
| 1222 | return ACT_RET_PRS_ERR; |
| 1223 | } |
Christopher Faulet | 96bff76 | 2019-12-17 13:46:18 +0100 | [diff] [blame] | 1224 | rule->arg.http.i = strtol(args[cur_arg], &endp, 0); |
Christopher Faulet | 81e2017 | 2019-12-12 16:40:30 +0100 | [diff] [blame] | 1225 | if (endp && *endp != '\0') { |
| 1226 | memprintf(err, "invalid character starting at '%s' (integer/hex value expected)", endp); |
| 1227 | return ACT_RET_PRS_ERR; |
| 1228 | } |
| 1229 | |
Christopher Faulet | c20b371 | 2020-01-27 15:51:56 +0100 | [diff] [blame] | 1230 | LIST_INIT(&rule->arg.http.fmt); |
Christopher Faulet | 81e2017 | 2019-12-12 16:40:30 +0100 | [diff] [blame] | 1231 | *orig_arg = cur_arg + 1; |
| 1232 | return ACT_RET_PRS_OK; |
| 1233 | #else |
| 1234 | memprintf(err, "not supported on this platform (IP_TOS undefined)"); |
| 1235 | return ACT_RET_PRS_ERR; |
| 1236 | #endif |
| 1237 | } |
| 1238 | |
| 1239 | /* Parse a "set-mark" action. It takes the MARK value as argument. It returns |
| 1240 | * ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error. |
| 1241 | */ |
| 1242 | static enum act_parse_ret parse_http_set_mark(const char **args, int *orig_arg, struct proxy *px, |
| 1243 | struct act_rule *rule, char **err) |
| 1244 | { |
| 1245 | #ifdef SO_MARK |
| 1246 | char *endp; |
| 1247 | int cur_arg; |
| 1248 | |
| 1249 | rule->action = ACT_HTTP_SET_MARK; |
| 1250 | |
| 1251 | cur_arg = *orig_arg; |
| 1252 | if (!*args[cur_arg]) { |
| 1253 | memprintf(err, "expects exactly 1 argument (integer/hex value)"); |
| 1254 | return ACT_RET_PRS_ERR; |
| 1255 | } |
Christopher Faulet | 96bff76 | 2019-12-17 13:46:18 +0100 | [diff] [blame] | 1256 | rule->arg.http.i = strtoul(args[cur_arg], &endp, 0); |
Christopher Faulet | 81e2017 | 2019-12-12 16:40:30 +0100 | [diff] [blame] | 1257 | if (endp && *endp != '\0') { |
| 1258 | memprintf(err, "invalid character starting at '%s' (integer/hex value expected)", endp); |
| 1259 | return ACT_RET_PRS_ERR; |
| 1260 | } |
| 1261 | |
Christopher Faulet | c20b371 | 2020-01-27 15:51:56 +0100 | [diff] [blame] | 1262 | LIST_INIT(&rule->arg.http.fmt); |
Christopher Faulet | 81e2017 | 2019-12-12 16:40:30 +0100 | [diff] [blame] | 1263 | *orig_arg = cur_arg + 1; |
| 1264 | global.last_checks |= LSTCHK_NETADM; |
| 1265 | return ACT_RET_PRS_OK; |
| 1266 | #else |
| 1267 | memprintf(err, "not supported on this platform (SO_MARK undefined)"); |
| 1268 | return ACT_RET_PRS_ERR; |
| 1269 | #endif |
| 1270 | } |
| 1271 | |
| 1272 | /* Parse a "set-log-level" action. It takes the level value as argument. It |
| 1273 | * returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error. |
| 1274 | */ |
| 1275 | static enum act_parse_ret parse_http_set_log_level(const char **args, int *orig_arg, struct proxy *px, |
| 1276 | struct act_rule *rule, char **err) |
| 1277 | { |
| 1278 | int cur_arg; |
| 1279 | |
| 1280 | rule->action = ACT_HTTP_SET_LOGL; |
| 1281 | |
| 1282 | cur_arg = *orig_arg; |
| 1283 | if (!*args[cur_arg]) { |
| 1284 | bad_log_level: |
| 1285 | memprintf(err, "expects exactly 1 argument (log level name or 'silent')"); |
| 1286 | return ACT_RET_PRS_ERR; |
| 1287 | } |
| 1288 | if (strcmp(args[cur_arg], "silent") == 0) |
Christopher Faulet | 96bff76 | 2019-12-17 13:46:18 +0100 | [diff] [blame] | 1289 | rule->arg.http.i = -1; |
| 1290 | 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] | 1291 | goto bad_log_level; |
| 1292 | |
Christopher Faulet | c20b371 | 2020-01-27 15:51:56 +0100 | [diff] [blame] | 1293 | LIST_INIT(&rule->arg.http.fmt); |
Christopher Faulet | 81e2017 | 2019-12-12 16:40:30 +0100 | [diff] [blame] | 1294 | *orig_arg = cur_arg + 1; |
| 1295 | return ACT_RET_PRS_OK; |
| 1296 | } |
| 1297 | |
Christopher Faulet | 91b3ec1 | 2020-01-17 22:30:06 +0100 | [diff] [blame] | 1298 | /* This function executes a early-hint action. It adds an HTTP Early Hint HTTP |
| 1299 | * 103 response header with <.arg.http.str> name and with a value built |
| 1300 | * according to <.arg.http.fmt> log line format. If it is the first early-hint |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 1301 | * rule of series, the 103 response start-line is added first. At the end, if |
Christopher Faulet | 91b3ec1 | 2020-01-17 22:30:06 +0100 | [diff] [blame] | 1302 | * the next rule is not an early-hint rule or if it is the last rule, the EOH |
| 1303 | * block is added to terminate the response. On success, it returns |
| 1304 | * ACT_RET_CONT. If an error occurs while soft rewrites are enabled, the action |
| 1305 | * is canceled, but the rule processing continue. Otherwsize ACT_RET_ERR is |
| 1306 | * returned. |
| 1307 | */ |
| 1308 | static enum act_return http_action_early_hint(struct act_rule *rule, struct proxy *px, |
| 1309 | struct session *sess, struct stream *s, int flags) |
| 1310 | { |
| 1311 | struct act_rule *prev_rule, *next_rule; |
| 1312 | struct channel *res = &s->res; |
| 1313 | struct htx *htx = htx_from_buf(&res->buf); |
| 1314 | struct buffer *value = alloc_trash_chunk(); |
| 1315 | enum act_return ret = ACT_RET_CONT; |
| 1316 | |
| 1317 | if (!(s->txn->req.flags & HTTP_MSGF_VER_11)) |
| 1318 | goto leave; |
| 1319 | |
| 1320 | if (!value) { |
| 1321 | if (!(s->flags & SF_ERR_MASK)) |
| 1322 | s->flags |= SF_ERR_RESOURCE; |
| 1323 | goto error; |
| 1324 | } |
| 1325 | |
| 1326 | /* get previous and next rules */ |
| 1327 | prev_rule = LIST_PREV(&rule->list, typeof(rule), list); |
| 1328 | next_rule = LIST_NEXT(&rule->list, typeof(rule), list); |
| 1329 | |
| 1330 | /* if no previous rule or previous rule is not early-hint, start a new response. Otherwise, |
| 1331 | * continue to add link to a previously started response */ |
| 1332 | if (&prev_rule->list == s->current_rule_list || prev_rule->action_ptr != http_action_early_hint) { |
| 1333 | struct htx_sl *sl; |
| 1334 | unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11| |
| 1335 | HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS); |
| 1336 | |
| 1337 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, |
| 1338 | ist("HTTP/1.1"), ist("103"), ist("Early Hints")); |
| 1339 | if (!sl) |
| 1340 | goto error; |
| 1341 | sl->info.res.status = 103; |
| 1342 | } |
| 1343 | |
| 1344 | /* Add the HTTP Early Hint HTTP 103 response heade */ |
| 1345 | value->data = build_logline(s, b_tail(value), b_room(value), &rule->arg.http.fmt); |
| 1346 | if (!htx_add_header(htx, rule->arg.http.str, ist2(b_head(value), b_data(value)))) |
| 1347 | goto error; |
| 1348 | |
| 1349 | /* if it is the last rule or the next one is not an early-hint, terminate the current |
| 1350 | * response. */ |
| 1351 | if (&next_rule->list == s->current_rule_list || next_rule->action_ptr != http_action_early_hint) { |
Christopher Faulet | 91b3ec1 | 2020-01-17 22:30:06 +0100 | [diff] [blame] | 1352 | if (!htx_add_endof(htx, HTX_BLK_EOH)) { |
| 1353 | /* If an error occurred during an Early-hint rule, |
| 1354 | * remove the incomplete HTTP 103 response from the |
| 1355 | * buffer */ |
| 1356 | goto error; |
| 1357 | } |
| 1358 | |
Christopher Faulet | a72a7e4 | 2020-01-28 09:28:11 +0100 | [diff] [blame] | 1359 | if (!http_forward_proxy_resp(s, 0)) |
| 1360 | goto error; |
Christopher Faulet | 91b3ec1 | 2020-01-17 22:30:06 +0100 | [diff] [blame] | 1361 | } |
| 1362 | |
| 1363 | leave: |
| 1364 | free_trash_chunk(value); |
| 1365 | return ret; |
| 1366 | |
| 1367 | error: |
| 1368 | /* If an error occurred during an Early-hint rule, remove the incomplete |
| 1369 | * HTTP 103 response from the buffer */ |
| 1370 | channel_htx_truncate(res, htx); |
| 1371 | ret = ACT_RET_ERR; |
| 1372 | goto leave; |
| 1373 | } |
| 1374 | |
Christopher Faulet | d1f27e3 | 2019-12-17 09:33:38 +0100 | [diff] [blame] | 1375 | /* This function executes a set-header or add-header actions. It builds a string |
| 1376 | * in the trash from the specified format string. It finds the action to be |
| 1377 | * performed in <.action>, previously filled by function parse_set_header(). The |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 1378 | * replacement action is executed by the function http_action_set_header(). On |
Christopher Faulet | d1f27e3 | 2019-12-17 09:33:38 +0100 | [diff] [blame] | 1379 | * success, it returns ACT_RET_CONT. If an error occurs while soft rewrites are |
| 1380 | * enabled, the action is canceled, but the rule processing continue. Otherwsize |
| 1381 | * ACT_RET_ERR is returned. |
| 1382 | */ |
| 1383 | static enum act_return http_action_set_header(struct act_rule *rule, struct proxy *px, |
| 1384 | struct session *sess, struct stream *s, int flags) |
| 1385 | { |
Christopher Faulet | 91e31d8 | 2020-01-24 15:37:13 +0100 | [diff] [blame] | 1386 | struct http_msg *msg = ((rule->from == ACT_F_HTTP_REQ) ? &s->txn->req : &s->txn->rsp); |
| 1387 | struct htx *htx = htxbuf(&msg->chn->buf); |
Christopher Faulet | d1f27e3 | 2019-12-17 09:33:38 +0100 | [diff] [blame] | 1388 | enum act_return ret = ACT_RET_CONT; |
| 1389 | struct buffer *replace; |
| 1390 | struct http_hdr_ctx ctx; |
| 1391 | struct ist n, v; |
| 1392 | |
| 1393 | replace = alloc_trash_chunk(); |
| 1394 | if (!replace) |
| 1395 | goto fail_alloc; |
| 1396 | |
| 1397 | replace->data = build_logline(s, replace->area, replace->size, &rule->arg.http.fmt); |
| 1398 | n = rule->arg.http.str; |
| 1399 | v = ist2(replace->area, replace->data); |
| 1400 | |
| 1401 | if (rule->action == 0) { // set-header |
| 1402 | /* remove all occurrences of the header */ |
| 1403 | ctx.blk = NULL; |
| 1404 | while (http_find_header(htx, n, &ctx, 1)) |
| 1405 | http_remove_header(htx, &ctx); |
| 1406 | } |
| 1407 | |
| 1408 | /* Now add header */ |
| 1409 | if (!http_add_header(htx, n, v)) |
| 1410 | goto fail_rewrite; |
| 1411 | |
| 1412 | leave: |
| 1413 | free_trash_chunk(replace); |
| 1414 | return ret; |
| 1415 | |
| 1416 | fail_alloc: |
| 1417 | if (!(s->flags & SF_ERR_MASK)) |
| 1418 | s->flags |= SF_ERR_RESOURCE; |
| 1419 | ret = ACT_RET_ERR; |
| 1420 | goto leave; |
| 1421 | |
| 1422 | fail_rewrite: |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1423 | _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_rewrites); |
Christopher Faulet | d1f27e3 | 2019-12-17 09:33:38 +0100 | [diff] [blame] | 1424 | if (s->flags & SF_BE_ASSIGNED) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1425 | _HA_ATOMIC_INC(&s->be->be_counters.failed_rewrites); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 1426 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1427 | _HA_ATOMIC_INC(&sess->listener->counters->failed_rewrites); |
Christopher Faulet | d1f27e3 | 2019-12-17 09:33:38 +0100 | [diff] [blame] | 1428 | if (objt_server(s->target)) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1429 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_rewrites); |
Christopher Faulet | d1f27e3 | 2019-12-17 09:33:38 +0100 | [diff] [blame] | 1430 | |
Christopher Faulet | 333bf8c | 2020-01-22 14:38:05 +0100 | [diff] [blame] | 1431 | if (!(msg->flags & HTTP_MSGF_SOFT_RW)) { |
Christopher Faulet | d1f27e3 | 2019-12-17 09:33:38 +0100 | [diff] [blame] | 1432 | ret = ACT_RET_ERR; |
Christopher Faulet | 333bf8c | 2020-01-22 14:38:05 +0100 | [diff] [blame] | 1433 | if (!(s->flags & SF_ERR_MASK)) |
| 1434 | s->flags |= SF_ERR_PRXCOND; |
| 1435 | } |
Christopher Faulet | d1f27e3 | 2019-12-17 09:33:38 +0100 | [diff] [blame] | 1436 | goto leave; |
| 1437 | } |
| 1438 | |
Christopher Faulet | 81e2017 | 2019-12-12 16:40:30 +0100 | [diff] [blame] | 1439 | /* Parse a "set-header", "add-header" or "early-hint" actions. It takes an |
| 1440 | * header name and a log-format string as arguments. It returns ACT_RET_PRS_OK |
| 1441 | * on success, ACT_RET_PRS_ERR on error. |
| 1442 | * |
| 1443 | * Note: same function is used for the request and the response. However |
| 1444 | * "early-hint" rules are only supported for request rules. |
| 1445 | */ |
| 1446 | static enum act_parse_ret parse_http_set_header(const char **args, int *orig_arg, struct proxy *px, |
| 1447 | struct act_rule *rule, char **err) |
| 1448 | { |
Christopher Faulet | 81e2017 | 2019-12-12 16:40:30 +0100 | [diff] [blame] | 1449 | int cap, cur_arg; |
| 1450 | |
Christopher Faulet | 91b3ec1 | 2020-01-17 22:30:06 +0100 | [diff] [blame] | 1451 | if (args[*orig_arg-1][0] == 'e') { |
| 1452 | rule->action = ACT_CUSTOM; |
| 1453 | rule->action_ptr = http_action_early_hint; |
| 1454 | } |
Christopher Faulet | d1f27e3 | 2019-12-17 09:33:38 +0100 | [diff] [blame] | 1455 | else { |
| 1456 | if (args[*orig_arg-1][0] == 's') |
| 1457 | rule->action = 0; // set-header |
| 1458 | else |
| 1459 | rule->action = 1; // add-header |
| 1460 | rule->action_ptr = http_action_set_header; |
| 1461 | } |
Christopher Faulet | 2eb5396 | 2020-01-14 14:47:34 +0100 | [diff] [blame] | 1462 | rule->release_ptr = release_http_action; |
Christopher Faulet | 81e2017 | 2019-12-12 16:40:30 +0100 | [diff] [blame] | 1463 | |
| 1464 | cur_arg = *orig_arg; |
| 1465 | if (!*args[cur_arg] || !*args[cur_arg+1]) { |
| 1466 | memprintf(err, "expects exactly 2 arguments"); |
| 1467 | return ACT_RET_PRS_ERR; |
| 1468 | } |
| 1469 | |
Christopher Faulet | 81e2017 | 2019-12-12 16:40:30 +0100 | [diff] [blame] | 1470 | |
Christopher Faulet | 96bff76 | 2019-12-17 13:46:18 +0100 | [diff] [blame] | 1471 | rule->arg.http.str.ptr = strdup(args[cur_arg]); |
| 1472 | rule->arg.http.str.len = strlen(rule->arg.http.str.ptr); |
| 1473 | LIST_INIT(&rule->arg.http.fmt); |
Christopher Faulet | 81e2017 | 2019-12-12 16:40:30 +0100 | [diff] [blame] | 1474 | |
| 1475 | if (rule->from == ACT_F_HTTP_REQ) { |
| 1476 | px->conf.args.ctx = ARGC_HRQ; |
| 1477 | cap = (px->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR; |
| 1478 | } |
| 1479 | else{ |
| 1480 | px->conf.args.ctx = ARGC_HRS; |
| 1481 | cap = (px->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR; |
| 1482 | } |
| 1483 | |
| 1484 | cur_arg++; |
Christopher Faulet | 1337b32 | 2020-01-14 14:50:55 +0100 | [diff] [blame] | 1485 | if (!parse_logformat_string(args[cur_arg], px, &rule->arg.http.fmt, LOG_OPT_HTTP, cap, err)) { |
Tim Duesterhus | ed52637 | 2020-03-05 17:56:33 +0100 | [diff] [blame] | 1486 | istfree(&rule->arg.http.str); |
Christopher Faulet | 81e2017 | 2019-12-12 16:40:30 +0100 | [diff] [blame] | 1487 | return ACT_RET_PRS_ERR; |
Christopher Faulet | 1337b32 | 2020-01-14 14:50:55 +0100 | [diff] [blame] | 1488 | } |
Christopher Faulet | 81e2017 | 2019-12-12 16:40:30 +0100 | [diff] [blame] | 1489 | |
| 1490 | free(px->conf.lfs_file); |
| 1491 | px->conf.lfs_file = strdup(px->conf.args.file); |
| 1492 | px->conf.lfs_line = px->conf.args.line; |
| 1493 | |
| 1494 | *orig_arg = cur_arg + 1; |
| 1495 | return ACT_RET_PRS_OK; |
| 1496 | } |
| 1497 | |
Christopher Faulet | 92d34fe | 2019-12-17 09:20:34 +0100 | [diff] [blame] | 1498 | /* This function executes a replace-header or replace-value actions. It |
| 1499 | * builds a string in the trash from the specified format string. It finds |
| 1500 | * the action to be performed in <.action>, previously filled by function |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 1501 | * parse_replace_header(). The replacement action is executed by the function |
Christopher Faulet | 92d34fe | 2019-12-17 09:20:34 +0100 | [diff] [blame] | 1502 | * http_action_replace_header(). On success, it returns ACT_RET_CONT. If an error |
| 1503 | * occurs while soft rewrites are enabled, the action is canceled, but the rule |
| 1504 | * processing continue. Otherwsize ACT_RET_ERR is returned. |
| 1505 | */ |
| 1506 | static enum act_return http_action_replace_header(struct act_rule *rule, struct proxy *px, |
| 1507 | struct session *sess, struct stream *s, int flags) |
| 1508 | { |
Christopher Faulet | 91e31d8 | 2020-01-24 15:37:13 +0100 | [diff] [blame] | 1509 | struct http_msg *msg = ((rule->from == ACT_F_HTTP_REQ) ? &s->txn->req : &s->txn->rsp); |
| 1510 | struct htx *htx = htxbuf(&msg->chn->buf); |
Christopher Faulet | 92d34fe | 2019-12-17 09:20:34 +0100 | [diff] [blame] | 1511 | enum act_return ret = ACT_RET_CONT; |
| 1512 | struct buffer *replace; |
| 1513 | int r; |
| 1514 | |
| 1515 | replace = alloc_trash_chunk(); |
| 1516 | if (!replace) |
| 1517 | goto fail_alloc; |
| 1518 | |
| 1519 | replace->data = build_logline(s, replace->area, replace->size, &rule->arg.http.fmt); |
| 1520 | |
| 1521 | r = http_replace_hdrs(s, htx, rule->arg.http.str, replace->area, rule->arg.http.re, (rule->action == 0)); |
| 1522 | if (r == -1) |
| 1523 | goto fail_rewrite; |
| 1524 | |
| 1525 | leave: |
| 1526 | free_trash_chunk(replace); |
| 1527 | return ret; |
| 1528 | |
| 1529 | fail_alloc: |
| 1530 | if (!(s->flags & SF_ERR_MASK)) |
| 1531 | s->flags |= SF_ERR_RESOURCE; |
| 1532 | ret = ACT_RET_ERR; |
| 1533 | goto leave; |
| 1534 | |
| 1535 | fail_rewrite: |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1536 | _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_rewrites); |
Christopher Faulet | 92d34fe | 2019-12-17 09:20:34 +0100 | [diff] [blame] | 1537 | if (s->flags & SF_BE_ASSIGNED) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1538 | _HA_ATOMIC_INC(&s->be->be_counters.failed_rewrites); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 1539 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1540 | _HA_ATOMIC_INC(&sess->listener->counters->failed_rewrites); |
Christopher Faulet | 92d34fe | 2019-12-17 09:20:34 +0100 | [diff] [blame] | 1541 | if (objt_server(s->target)) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1542 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_rewrites); |
Christopher Faulet | 92d34fe | 2019-12-17 09:20:34 +0100 | [diff] [blame] | 1543 | |
Christopher Faulet | 333bf8c | 2020-01-22 14:38:05 +0100 | [diff] [blame] | 1544 | if (!(msg->flags & HTTP_MSGF_SOFT_RW)) { |
Christopher Faulet | 92d34fe | 2019-12-17 09:20:34 +0100 | [diff] [blame] | 1545 | ret = ACT_RET_ERR; |
Christopher Faulet | 333bf8c | 2020-01-22 14:38:05 +0100 | [diff] [blame] | 1546 | if (!(s->flags & SF_ERR_MASK)) |
| 1547 | s->flags |= SF_ERR_PRXCOND; |
| 1548 | } |
Christopher Faulet | 92d34fe | 2019-12-17 09:20:34 +0100 | [diff] [blame] | 1549 | goto leave; |
| 1550 | } |
| 1551 | |
Christopher Faulet | 81e2017 | 2019-12-12 16:40:30 +0100 | [diff] [blame] | 1552 | /* Parse a "replace-header" or "replace-value" actions. It takes an header name, |
| 1553 | * a regex and replacement string as arguments. It returns ACT_RET_PRS_OK on |
| 1554 | * success, ACT_RET_PRS_ERR on error. |
| 1555 | */ |
| 1556 | static enum act_parse_ret parse_http_replace_header(const char **args, int *orig_arg, struct proxy *px, |
| 1557 | struct act_rule *rule, char **err) |
| 1558 | { |
| 1559 | int cap, cur_arg; |
| 1560 | |
Christopher Faulet | 92d34fe | 2019-12-17 09:20:34 +0100 | [diff] [blame] | 1561 | if (args[*orig_arg-1][8] == 'h') |
| 1562 | rule->action = 0; // replace-header |
| 1563 | else |
| 1564 | rule->action = 1; // replace-value |
| 1565 | rule->action_ptr = http_action_replace_header; |
Christopher Faulet | 2eb5396 | 2020-01-14 14:47:34 +0100 | [diff] [blame] | 1566 | rule->release_ptr = release_http_action; |
Christopher Faulet | 81e2017 | 2019-12-12 16:40:30 +0100 | [diff] [blame] | 1567 | |
| 1568 | cur_arg = *orig_arg; |
| 1569 | if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2]) { |
| 1570 | memprintf(err, "expects exactly 3 arguments"); |
| 1571 | return ACT_RET_PRS_ERR; |
| 1572 | } |
| 1573 | |
Christopher Faulet | 96bff76 | 2019-12-17 13:46:18 +0100 | [diff] [blame] | 1574 | rule->arg.http.str.ptr = strdup(args[cur_arg]); |
| 1575 | rule->arg.http.str.len = strlen(rule->arg.http.str.ptr); |
| 1576 | LIST_INIT(&rule->arg.http.fmt); |
Christopher Faulet | 81e2017 | 2019-12-12 16:40:30 +0100 | [diff] [blame] | 1577 | |
| 1578 | cur_arg++; |
Christopher Faulet | 1337b32 | 2020-01-14 14:50:55 +0100 | [diff] [blame] | 1579 | if (!(rule->arg.http.re = regex_comp(args[cur_arg], 1, 1, err))) { |
Tim Duesterhus | ed52637 | 2020-03-05 17:56:33 +0100 | [diff] [blame] | 1580 | istfree(&rule->arg.http.str); |
Christopher Faulet | 81e2017 | 2019-12-12 16:40:30 +0100 | [diff] [blame] | 1581 | return ACT_RET_PRS_ERR; |
Christopher Faulet | 1337b32 | 2020-01-14 14:50:55 +0100 | [diff] [blame] | 1582 | } |
Christopher Faulet | 81e2017 | 2019-12-12 16:40:30 +0100 | [diff] [blame] | 1583 | |
| 1584 | if (rule->from == ACT_F_HTTP_REQ) { |
| 1585 | px->conf.args.ctx = ARGC_HRQ; |
| 1586 | cap = (px->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR; |
| 1587 | } |
| 1588 | else{ |
| 1589 | px->conf.args.ctx = ARGC_HRS; |
| 1590 | cap = (px->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR; |
| 1591 | } |
| 1592 | |
| 1593 | cur_arg++; |
Christopher Faulet | 1337b32 | 2020-01-14 14:50:55 +0100 | [diff] [blame] | 1594 | if (!parse_logformat_string(args[cur_arg], px, &rule->arg.http.fmt, LOG_OPT_HTTP, cap, err)) { |
Tim Duesterhus | ed52637 | 2020-03-05 17:56:33 +0100 | [diff] [blame] | 1595 | istfree(&rule->arg.http.str); |
Christopher Faulet | 1337b32 | 2020-01-14 14:50:55 +0100 | [diff] [blame] | 1596 | regex_free(rule->arg.http.re); |
Christopher Faulet | 81e2017 | 2019-12-12 16:40:30 +0100 | [diff] [blame] | 1597 | return ACT_RET_PRS_ERR; |
Christopher Faulet | 1337b32 | 2020-01-14 14:50:55 +0100 | [diff] [blame] | 1598 | } |
Christopher Faulet | 81e2017 | 2019-12-12 16:40:30 +0100 | [diff] [blame] | 1599 | |
| 1600 | free(px->conf.lfs_file); |
| 1601 | px->conf.lfs_file = strdup(px->conf.args.file); |
| 1602 | px->conf.lfs_line = px->conf.args.line; |
| 1603 | |
| 1604 | *orig_arg = cur_arg + 1; |
| 1605 | return ACT_RET_PRS_OK; |
| 1606 | } |
| 1607 | |
Maciej Zdeb | ebdd4c5 | 2020-11-20 13:58:48 +0000 | [diff] [blame] | 1608 | /* This function executes a del-header action with selected matching mode for |
| 1609 | * header name. It finds the matching method to be performed in <.action>, previously |
| 1610 | * filled by function parse_http_del_header(). On success, it returns ACT_RET_CONT. |
| 1611 | * Otherwise ACT_RET_ERR is returned. |
| 1612 | */ |
| 1613 | static enum act_return http_action_del_header(struct act_rule *rule, struct proxy *px, |
| 1614 | struct session *sess, struct stream *s, int flags) |
| 1615 | { |
| 1616 | struct http_hdr_ctx ctx; |
| 1617 | struct http_msg *msg = ((rule->from == ACT_F_HTTP_REQ) ? &s->txn->req : &s->txn->rsp); |
| 1618 | struct htx *htx = htxbuf(&msg->chn->buf); |
| 1619 | enum act_return ret = ACT_RET_CONT; |
| 1620 | |
| 1621 | /* remove all occurrences of the header */ |
| 1622 | ctx.blk = NULL; |
| 1623 | switch (rule->action) { |
| 1624 | case PAT_MATCH_STR: |
| 1625 | while (http_find_header(htx, rule->arg.http.str, &ctx, 1)) |
| 1626 | http_remove_header(htx, &ctx); |
| 1627 | break; |
| 1628 | case PAT_MATCH_BEG: |
| 1629 | while (http_find_pfx_header(htx, rule->arg.http.str, &ctx, 1)) |
| 1630 | http_remove_header(htx, &ctx); |
| 1631 | break; |
| 1632 | case PAT_MATCH_END: |
| 1633 | while (http_find_sfx_header(htx, rule->arg.http.str, &ctx, 1)) |
| 1634 | http_remove_header(htx, &ctx); |
| 1635 | break; |
| 1636 | case PAT_MATCH_SUB: |
| 1637 | while (http_find_sub_header(htx, rule->arg.http.str, &ctx, 1)) |
| 1638 | http_remove_header(htx, &ctx); |
| 1639 | break; |
| 1640 | case PAT_MATCH_REG: |
| 1641 | while (http_match_header(htx, rule->arg.http.re, &ctx, 1)) |
| 1642 | http_remove_header(htx, &ctx); |
| 1643 | break; |
| 1644 | default: |
| 1645 | return ACT_RET_ERR; |
| 1646 | } |
| 1647 | return ret; |
| 1648 | } |
| 1649 | |
| 1650 | /* Parse a "del-header" action. It takes string as a required argument, |
| 1651 | * optional flag (currently only -m) and optional matching method of input string |
| 1652 | * with header name to be deleted. Default matching method is exact match (-m str). |
| 1653 | * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error. |
Christopher Faulet | 81e2017 | 2019-12-12 16:40:30 +0100 | [diff] [blame] | 1654 | */ |
| 1655 | static enum act_parse_ret parse_http_del_header(const char **args, int *orig_arg, struct proxy *px, |
| 1656 | struct act_rule *rule, char **err) |
| 1657 | { |
| 1658 | int cur_arg; |
Maciej Zdeb | ebdd4c5 | 2020-11-20 13:58:48 +0000 | [diff] [blame] | 1659 | int pat_idx; |
Christopher Faulet | 81e2017 | 2019-12-12 16:40:30 +0100 | [diff] [blame] | 1660 | |
Maciej Zdeb | ebdd4c5 | 2020-11-20 13:58:48 +0000 | [diff] [blame] | 1661 | /* set exact matching (-m str) as default */ |
| 1662 | rule->action = PAT_MATCH_STR; |
| 1663 | rule->action_ptr = http_action_del_header; |
Christopher Faulet | 2eb5396 | 2020-01-14 14:47:34 +0100 | [diff] [blame] | 1664 | rule->release_ptr = release_http_action; |
Christopher Faulet | 81e2017 | 2019-12-12 16:40:30 +0100 | [diff] [blame] | 1665 | |
| 1666 | cur_arg = *orig_arg; |
| 1667 | if (!*args[cur_arg]) { |
Maciej Zdeb | ebdd4c5 | 2020-11-20 13:58:48 +0000 | [diff] [blame] | 1668 | memprintf(err, "expects at least 1 argument"); |
Christopher Faulet | 81e2017 | 2019-12-12 16:40:30 +0100 | [diff] [blame] | 1669 | return ACT_RET_PRS_ERR; |
| 1670 | } |
| 1671 | |
Christopher Faulet | 96bff76 | 2019-12-17 13:46:18 +0100 | [diff] [blame] | 1672 | rule->arg.http.str.ptr = strdup(args[cur_arg]); |
| 1673 | rule->arg.http.str.len = strlen(rule->arg.http.str.ptr); |
Christopher Faulet | 81e2017 | 2019-12-12 16:40:30 +0100 | [diff] [blame] | 1674 | px->conf.args.ctx = (rule->from == ACT_F_HTTP_REQ ? ARGC_HRQ : ARGC_HRS); |
| 1675 | |
Maciej Zdeb | 6dee996 | 2020-11-23 16:03:09 +0000 | [diff] [blame] | 1676 | LIST_INIT(&rule->arg.http.fmt); |
Maciej Zdeb | ebdd4c5 | 2020-11-20 13:58:48 +0000 | [diff] [blame] | 1677 | if (strcmp(args[cur_arg+1], "-m") == 0) { |
| 1678 | cur_arg++; |
| 1679 | if (!*args[cur_arg+1]) { |
| 1680 | memprintf(err, "-m flag expects exactly 1 argument"); |
| 1681 | return ACT_RET_PRS_ERR; |
| 1682 | } |
| 1683 | |
| 1684 | cur_arg++; |
| 1685 | pat_idx = pat_find_match_name(args[cur_arg]); |
| 1686 | switch (pat_idx) { |
| 1687 | case PAT_MATCH_REG: |
| 1688 | if (!(rule->arg.http.re = regex_comp(rule->arg.http.str.ptr, 1, 1, err))) |
| 1689 | return ACT_RET_PRS_ERR; |
| 1690 | /* fall through */ |
| 1691 | case PAT_MATCH_STR: |
| 1692 | case PAT_MATCH_BEG: |
| 1693 | case PAT_MATCH_END: |
| 1694 | case PAT_MATCH_SUB: |
| 1695 | rule->action = pat_idx; |
| 1696 | break; |
| 1697 | default: |
| 1698 | memprintf(err, "-m with unsupported matching method '%s'", args[cur_arg]); |
| 1699 | return ACT_RET_PRS_ERR; |
| 1700 | } |
| 1701 | } |
| 1702 | |
Christopher Faulet | 81e2017 | 2019-12-12 16:40:30 +0100 | [diff] [blame] | 1703 | *orig_arg = cur_arg + 1; |
| 1704 | return ACT_RET_PRS_OK; |
| 1705 | } |
| 1706 | |
Christopher Faulet | 2eb5396 | 2020-01-14 14:47:34 +0100 | [diff] [blame] | 1707 | /* Release memory allocated by an http redirect action. */ |
| 1708 | static void release_http_redir(struct act_rule *rule) |
| 1709 | { |
| 1710 | struct logformat_node *lf, *lfb; |
| 1711 | struct redirect_rule *redir; |
| 1712 | |
| 1713 | redir = rule->arg.redir; |
| 1714 | LIST_DEL(&redir->list); |
| 1715 | if (redir->cond) { |
| 1716 | prune_acl_cond(redir->cond); |
| 1717 | free(redir->cond); |
| 1718 | } |
| 1719 | free(redir->rdr_str); |
| 1720 | free(redir->cookie_str); |
| 1721 | list_for_each_entry_safe(lf, lfb, &redir->rdr_fmt, list) { |
| 1722 | LIST_DEL(&lf->list); |
| 1723 | free(lf); |
| 1724 | } |
| 1725 | free(redir); |
| 1726 | } |
| 1727 | |
Christopher Faulet | 81e2017 | 2019-12-12 16:40:30 +0100 | [diff] [blame] | 1728 | /* Parse a "redirect" action. It returns ACT_RET_PRS_OK on success, |
| 1729 | * ACT_RET_PRS_ERR on error. |
| 1730 | */ |
| 1731 | static enum act_parse_ret parse_http_redirect(const char **args, int *orig_arg, struct proxy *px, |
| 1732 | struct act_rule *rule, char **err) |
| 1733 | { |
| 1734 | struct redirect_rule *redir; |
| 1735 | int dir, cur_arg; |
| 1736 | |
| 1737 | rule->action = ACT_HTTP_REDIR; |
Christopher Faulet | 245cf79 | 2019-12-18 14:58:12 +0100 | [diff] [blame] | 1738 | rule->flags |= ACT_FLAG_FINAL; |
Christopher Faulet | 2eb5396 | 2020-01-14 14:47:34 +0100 | [diff] [blame] | 1739 | rule->release_ptr = release_http_redir; |
Christopher Faulet | 81e2017 | 2019-12-12 16:40:30 +0100 | [diff] [blame] | 1740 | |
| 1741 | cur_arg = *orig_arg; |
| 1742 | |
| 1743 | dir = (rule->from == ACT_F_HTTP_REQ ? 0 : 1); |
| 1744 | if ((redir = http_parse_redirect_rule(px->conf.args.file, px->conf.args.line, px, &args[cur_arg], err, 1, dir)) == NULL) |
| 1745 | return ACT_RET_PRS_ERR; |
| 1746 | |
| 1747 | rule->arg.redir = redir; |
| 1748 | rule->cond = redir->cond; |
| 1749 | redir->cond = NULL; |
| 1750 | |
| 1751 | /* skip all arguments */ |
| 1752 | while (*args[cur_arg]) |
| 1753 | cur_arg++; |
| 1754 | |
| 1755 | *orig_arg = cur_arg; |
| 1756 | return ACT_RET_PRS_OK; |
| 1757 | } |
| 1758 | |
Christopher Faulet | 046cf44 | 2019-12-17 15:45:23 +0100 | [diff] [blame] | 1759 | /* This function executes a add-acl, del-acl, set-map or del-map actions. On |
| 1760 | * success, it returns ACT_RET_CONT. Otherwsize ACT_RET_ERR is returned. |
| 1761 | */ |
| 1762 | static enum act_return http_action_set_map(struct act_rule *rule, struct proxy *px, |
| 1763 | struct session *sess, struct stream *s, int flags) |
| 1764 | { |
| 1765 | struct pat_ref *ref; |
| 1766 | struct buffer *key = NULL, *value = NULL; |
| 1767 | enum act_return ret = ACT_RET_CONT; |
| 1768 | |
| 1769 | /* collect reference */ |
| 1770 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 1771 | if (!ref) |
| 1772 | goto leave; |
| 1773 | |
| 1774 | /* allocate key */ |
| 1775 | key = alloc_trash_chunk(); |
| 1776 | if (!key) |
| 1777 | goto fail_alloc; |
| 1778 | |
| 1779 | /* collect key */ |
| 1780 | key->data = build_logline(s, key->area, key->size, &rule->arg.map.key); |
| 1781 | key->area[key->data] = '\0'; |
| 1782 | |
| 1783 | switch (rule->action) { |
| 1784 | case 0: // add-acl |
| 1785 | /* add entry only if it does not already exist */ |
| 1786 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
| 1787 | if (pat_ref_find_elt(ref, key->area) == NULL) |
| 1788 | pat_ref_add(ref, key->area, NULL, NULL); |
| 1789 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
| 1790 | break; |
| 1791 | |
| 1792 | case 1: // set-map |
| 1793 | /* allocate value */ |
| 1794 | value = alloc_trash_chunk(); |
| 1795 | if (!value) |
| 1796 | goto fail_alloc; |
| 1797 | |
| 1798 | /* collect value */ |
| 1799 | value->data = build_logline(s, value->area, value->size, &rule->arg.map.value); |
| 1800 | value->area[value->data] = '\0'; |
| 1801 | |
| 1802 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
| 1803 | if (pat_ref_find_elt(ref, key->area) != NULL) { |
| 1804 | /* update entry if it exists */ |
| 1805 | pat_ref_set(ref, key->area, value->area, NULL); |
| 1806 | } |
| 1807 | else { |
| 1808 | /* insert a new entry */ |
| 1809 | pat_ref_add(ref, key->area, value->area, NULL); |
| 1810 | } |
| 1811 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
| 1812 | break; |
| 1813 | |
| 1814 | case 2: // del-acl |
| 1815 | case 3: // del-map |
| 1816 | /* returned code: 1=ok, 0=ko */ |
| 1817 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
| 1818 | pat_ref_delete(ref, key->area); |
| 1819 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
| 1820 | break; |
| 1821 | |
| 1822 | default: |
| 1823 | ret = ACT_RET_ERR; |
| 1824 | } |
| 1825 | |
| 1826 | |
| 1827 | leave: |
| 1828 | free_trash_chunk(key); |
| 1829 | free_trash_chunk(value); |
| 1830 | return ret; |
| 1831 | |
| 1832 | fail_alloc: |
| 1833 | if (!(s->flags & SF_ERR_MASK)) |
| 1834 | s->flags |= SF_ERR_RESOURCE; |
| 1835 | ret = ACT_RET_ERR; |
| 1836 | goto leave; |
| 1837 | } |
| 1838 | |
Christopher Faulet | 2eb5396 | 2020-01-14 14:47:34 +0100 | [diff] [blame] | 1839 | /* Release memory allocated by an http map/acl action. */ |
| 1840 | static void release_http_map(struct act_rule *rule) |
| 1841 | { |
| 1842 | struct logformat_node *lf, *lfb; |
| 1843 | |
| 1844 | free(rule->arg.map.ref); |
| 1845 | list_for_each_entry_safe(lf, lfb, &rule->arg.map.key, list) { |
| 1846 | LIST_DEL(&lf->list); |
| 1847 | release_sample_expr(lf->expr); |
| 1848 | free(lf->arg); |
| 1849 | free(lf); |
| 1850 | } |
| 1851 | if (rule->action == 1) { |
| 1852 | list_for_each_entry_safe(lf, lfb, &rule->arg.map.value, list) { |
| 1853 | LIST_DEL(&lf->list); |
| 1854 | release_sample_expr(lf->expr); |
| 1855 | free(lf->arg); |
| 1856 | free(lf); |
| 1857 | } |
| 1858 | } |
| 1859 | } |
| 1860 | |
Christopher Faulet | 81e2017 | 2019-12-12 16:40:30 +0100 | [diff] [blame] | 1861 | /* Parse a "add-acl", "del-acl", "set-map" or "del-map" actions. It takes one or |
Christopher Faulet | 046cf44 | 2019-12-17 15:45:23 +0100 | [diff] [blame] | 1862 | * two log-format string as argument depending on the action. The action is |
| 1863 | * stored in <.action> as an int (0=add-acl, 1=set-map, 2=del-acl, |
| 1864 | * 3=del-map). It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error. |
Christopher Faulet | 81e2017 | 2019-12-12 16:40:30 +0100 | [diff] [blame] | 1865 | */ |
| 1866 | static enum act_parse_ret parse_http_set_map(const char **args, int *orig_arg, struct proxy *px, |
| 1867 | struct act_rule *rule, char **err) |
| 1868 | { |
| 1869 | int cap, cur_arg; |
| 1870 | |
Christopher Faulet | 046cf44 | 2019-12-17 15:45:23 +0100 | [diff] [blame] | 1871 | if (args[*orig_arg-1][0] == 'a') // add-acl |
| 1872 | rule->action = 0; |
| 1873 | else if (args[*orig_arg-1][0] == 's') // set-map |
| 1874 | rule->action = 1; |
| 1875 | else if (args[*orig_arg-1][4] == 'a') // del-acl |
| 1876 | rule->action = 2; |
| 1877 | else if (args[*orig_arg-1][4] == 'm') // del-map |
| 1878 | rule->action = 3; |
| 1879 | else { |
| 1880 | memprintf(err, "internal error: unhandled action '%s'", args[0]); |
| 1881 | return ACT_RET_PRS_ERR; |
| 1882 | } |
| 1883 | rule->action_ptr = http_action_set_map; |
Christopher Faulet | 2eb5396 | 2020-01-14 14:47:34 +0100 | [diff] [blame] | 1884 | rule->release_ptr = release_http_map; |
Christopher Faulet | 81e2017 | 2019-12-12 16:40:30 +0100 | [diff] [blame] | 1885 | |
| 1886 | cur_arg = *orig_arg; |
Christopher Faulet | 046cf44 | 2019-12-17 15:45:23 +0100 | [diff] [blame] | 1887 | if (rule->action == 1 && (!*args[cur_arg] || !*args[cur_arg+1])) { |
| 1888 | /* 2 args for set-map */ |
Christopher Faulet | 81e2017 | 2019-12-12 16:40:30 +0100 | [diff] [blame] | 1889 | memprintf(err, "expects exactly 2 arguments"); |
| 1890 | return ACT_RET_PRS_ERR; |
| 1891 | } |
| 1892 | else if (!*args[cur_arg]) { |
Christopher Faulet | 046cf44 | 2019-12-17 15:45:23 +0100 | [diff] [blame] | 1893 | /* only one arg for other actions */ |
Christopher Faulet | 81e2017 | 2019-12-12 16:40:30 +0100 | [diff] [blame] | 1894 | memprintf(err, "expects exactly 1 arguments"); |
| 1895 | return ACT_RET_PRS_ERR; |
| 1896 | } |
| 1897 | |
| 1898 | /* |
| 1899 | * '+ 8' for 'set-map(' (same for del-map) |
| 1900 | * '- 9' for 'set-map(' + trailing ')' (same for del-map) |
| 1901 | */ |
| 1902 | rule->arg.map.ref = my_strndup(args[cur_arg-1] + 8, strlen(args[cur_arg-1]) - 9); |
| 1903 | |
| 1904 | if (rule->from == ACT_F_HTTP_REQ) { |
| 1905 | px->conf.args.ctx = ARGC_HRQ; |
| 1906 | cap = (px->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR; |
| 1907 | } |
| 1908 | else{ |
| 1909 | px->conf.args.ctx = ARGC_HRS; |
| 1910 | cap = (px->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR; |
| 1911 | } |
| 1912 | |
| 1913 | /* key pattern */ |
| 1914 | LIST_INIT(&rule->arg.map.key); |
Christopher Faulet | 1337b32 | 2020-01-14 14:50:55 +0100 | [diff] [blame] | 1915 | if (!parse_logformat_string(args[cur_arg], px, &rule->arg.map.key, LOG_OPT_HTTP, cap, err)) { |
| 1916 | free(rule->arg.map.ref); |
Christopher Faulet | 81e2017 | 2019-12-12 16:40:30 +0100 | [diff] [blame] | 1917 | return ACT_RET_PRS_ERR; |
Christopher Faulet | 1337b32 | 2020-01-14 14:50:55 +0100 | [diff] [blame] | 1918 | } |
Christopher Faulet | 81e2017 | 2019-12-12 16:40:30 +0100 | [diff] [blame] | 1919 | |
Christopher Faulet | 046cf44 | 2019-12-17 15:45:23 +0100 | [diff] [blame] | 1920 | if (rule->action == 1) { |
Christopher Faulet | 81e2017 | 2019-12-12 16:40:30 +0100 | [diff] [blame] | 1921 | /* value pattern for set-map only */ |
| 1922 | cur_arg++; |
| 1923 | LIST_INIT(&rule->arg.map.value); |
Christopher Faulet | 1337b32 | 2020-01-14 14:50:55 +0100 | [diff] [blame] | 1924 | if (!parse_logformat_string(args[cur_arg], px, &rule->arg.map.value, LOG_OPT_HTTP, cap, err)) { |
| 1925 | free(rule->arg.map.ref); |
Christopher Faulet | 81e2017 | 2019-12-12 16:40:30 +0100 | [diff] [blame] | 1926 | return ACT_RET_PRS_ERR; |
Christopher Faulet | 1337b32 | 2020-01-14 14:50:55 +0100 | [diff] [blame] | 1927 | } |
Christopher Faulet | 81e2017 | 2019-12-12 16:40:30 +0100 | [diff] [blame] | 1928 | } |
| 1929 | |
| 1930 | free(px->conf.lfs_file); |
| 1931 | px->conf.lfs_file = strdup(px->conf.args.file); |
| 1932 | px->conf.lfs_line = px->conf.args.line; |
| 1933 | |
| 1934 | *orig_arg = cur_arg + 1; |
| 1935 | return ACT_RET_PRS_OK; |
| 1936 | } |
| 1937 | |
Christopher Faulet | ac98d81 | 2019-12-18 09:20:16 +0100 | [diff] [blame] | 1938 | /* This function executes a track-sc* actions. On success, it returns |
| 1939 | * ACT_RET_CONT. Otherwsize ACT_RET_ERR is returned. |
| 1940 | */ |
| 1941 | static enum act_return http_action_track_sc(struct act_rule *rule, struct proxy *px, |
| 1942 | struct session *sess, struct stream *s, int flags) |
| 1943 | { |
| 1944 | struct stktable *t; |
| 1945 | struct stksess *ts; |
| 1946 | struct stktable_key *key; |
Willy Tarreau | 826f3ab | 2021-02-10 12:07:15 +0100 | [diff] [blame] | 1947 | void *ptr1, *ptr2, *ptr3, *ptr4, *ptr5, *ptr6; |
Christopher Faulet | ac98d81 | 2019-12-18 09:20:16 +0100 | [diff] [blame] | 1948 | int opt; |
| 1949 | |
Willy Tarreau | 826f3ab | 2021-02-10 12:07:15 +0100 | [diff] [blame] | 1950 | ptr1 = ptr2 = ptr3 = ptr4 = ptr5 = ptr6 = NULL; |
Christopher Faulet | ac98d81 | 2019-12-18 09:20:16 +0100 | [diff] [blame] | 1951 | opt = ((rule->from == ACT_F_HTTP_REQ) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES) | SMP_OPT_FINAL; |
| 1952 | |
| 1953 | t = rule->arg.trk_ctr.table.t; |
Emeric Brun | 362d25e | 2021-03-10 16:58:03 +0100 | [diff] [blame] | 1954 | |
| 1955 | if (stkctr_entry(&s->stkctr[rule->action])) |
| 1956 | goto end; |
| 1957 | |
Christopher Faulet | ac98d81 | 2019-12-18 09:20:16 +0100 | [diff] [blame] | 1958 | key = stktable_fetch_key(t, s->be, sess, s, opt, rule->arg.trk_ctr.expr, NULL); |
| 1959 | |
| 1960 | if (!key) |
| 1961 | goto end; |
| 1962 | ts = stktable_get_entry(t, key); |
| 1963 | if (!ts) |
| 1964 | goto end; |
| 1965 | |
| 1966 | stream_track_stkctr(&s->stkctr[rule->action], t, ts); |
| 1967 | |
| 1968 | /* let's count a new HTTP request as it's the first time we do it */ |
| 1969 | ptr1 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT); |
| 1970 | ptr2 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE); |
| 1971 | |
| 1972 | /* When the client triggers a 4xx from the server, it's most often due |
| 1973 | * to a missing object or permission. These events should be tracked |
| 1974 | * because if they happen often, it may indicate a brute force or a |
| 1975 | * vulnerability scan. Normally this is done when receiving the response |
| 1976 | * but here we're tracking after this ought to have been done so we have |
| 1977 | * to do it on purpose. |
| 1978 | */ |
| 1979 | if (rule->from == ACT_F_HTTP_RES && (unsigned)(s->txn->status - 400) < 100) { |
| 1980 | ptr3 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_CNT); |
| 1981 | ptr4 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_RATE); |
| 1982 | } |
| 1983 | |
Willy Tarreau | 826f3ab | 2021-02-10 12:07:15 +0100 | [diff] [blame] | 1984 | if (rule->from == ACT_F_HTTP_RES && (unsigned)(s->txn->status - 500) < 100 && |
| 1985 | s->txn->status != 501 && s->txn->status != 505) { |
| 1986 | ptr5 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_FAIL_CNT); |
| 1987 | ptr6 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_FAIL_RATE); |
| 1988 | } |
| 1989 | |
| 1990 | if (ptr1 || ptr2 || ptr3 || ptr4 || ptr5 || ptr6) { |
Christopher Faulet | ac98d81 | 2019-12-18 09:20:16 +0100 | [diff] [blame] | 1991 | HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock); |
| 1992 | |
| 1993 | if (ptr1) |
| 1994 | stktable_data_cast(ptr1, http_req_cnt)++; |
| 1995 | if (ptr2) |
| 1996 | update_freq_ctr_period(&stktable_data_cast(ptr2, http_req_rate), |
| 1997 | t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1); |
| 1998 | if (ptr3) |
| 1999 | stktable_data_cast(ptr3, http_err_cnt)++; |
| 2000 | if (ptr4) |
| 2001 | update_freq_ctr_period(&stktable_data_cast(ptr4, http_err_rate), |
| 2002 | t->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1); |
Willy Tarreau | 826f3ab | 2021-02-10 12:07:15 +0100 | [diff] [blame] | 2003 | if (ptr5) |
| 2004 | stktable_data_cast(ptr5, http_fail_cnt)++; |
| 2005 | if (ptr6) |
| 2006 | update_freq_ctr_period(&stktable_data_cast(ptr6, http_fail_rate), |
| 2007 | t->data_arg[STKTABLE_DT_HTTP_FAIL_RATE].u, 1); |
Christopher Faulet | ac98d81 | 2019-12-18 09:20:16 +0100 | [diff] [blame] | 2008 | |
| 2009 | HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock); |
| 2010 | |
| 2011 | /* If data was modified, we need to touch to re-schedule sync */ |
| 2012 | stktable_touch_local(t, ts, 0); |
| 2013 | } |
| 2014 | |
| 2015 | stkctr_set_flags(&s->stkctr[rule->action], STKCTR_TRACK_CONTENT); |
| 2016 | if (sess->fe != s->be) |
| 2017 | stkctr_set_flags(&s->stkctr[rule->action], STKCTR_TRACK_BACKEND); |
| 2018 | |
| 2019 | end: |
| 2020 | return ACT_RET_CONT; |
| 2021 | } |
Christopher Faulet | 81e2017 | 2019-12-12 16:40:30 +0100 | [diff] [blame] | 2022 | |
Christopher Faulet | 2eb5396 | 2020-01-14 14:47:34 +0100 | [diff] [blame] | 2023 | static void release_http_track_sc(struct act_rule *rule) |
| 2024 | { |
| 2025 | release_sample_expr(rule->arg.trk_ctr.expr); |
| 2026 | } |
| 2027 | |
Christopher Faulet | 81e2017 | 2019-12-12 16:40:30 +0100 | [diff] [blame] | 2028 | /* Parse a "track-sc*" actions. It returns ACT_RET_PRS_OK on success, |
| 2029 | * ACT_RET_PRS_ERR on error. |
| 2030 | */ |
| 2031 | static enum act_parse_ret parse_http_track_sc(const char **args, int *orig_arg, struct proxy *px, |
| 2032 | struct act_rule *rule, char **err) |
| 2033 | { |
| 2034 | struct sample_expr *expr; |
| 2035 | unsigned int where; |
| 2036 | unsigned int tsc_num; |
| 2037 | const char *tsc_num_str; |
| 2038 | int cur_arg; |
| 2039 | |
| 2040 | tsc_num_str = &args[*orig_arg-1][8]; |
| 2041 | if (cfg_parse_track_sc_num(&tsc_num, tsc_num_str, tsc_num_str + strlen(tsc_num_str), err) == -1) |
| 2042 | return ACT_RET_PRS_ERR; |
| 2043 | |
| 2044 | cur_arg = *orig_arg; |
| 2045 | expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, |
Willy Tarreau | e3b57bf | 2020-02-14 16:50:14 +0100 | [diff] [blame] | 2046 | err, &px->conf.args, NULL); |
Christopher Faulet | 81e2017 | 2019-12-12 16:40:30 +0100 | [diff] [blame] | 2047 | if (!expr) |
| 2048 | return ACT_RET_PRS_ERR; |
| 2049 | |
| 2050 | where = 0; |
| 2051 | if (px->cap & PR_CAP_FE) |
| 2052 | where |= (rule->from == ACT_F_HTTP_REQ ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_FE_HRS_HDR); |
| 2053 | if (px->cap & PR_CAP_BE) |
| 2054 | where |= (rule->from == ACT_F_HTTP_REQ ? SMP_VAL_BE_HRQ_HDR : SMP_VAL_BE_HRS_HDR); |
| 2055 | |
| 2056 | if (!(expr->fetch->val & where)) { |
| 2057 | memprintf(err, "fetch method '%s' extracts information from '%s', none of which is available here", |
| 2058 | args[cur_arg-1], sample_src_names(expr->fetch->use)); |
Christopher Faulet | 1337b32 | 2020-01-14 14:50:55 +0100 | [diff] [blame] | 2059 | release_sample_expr(expr); |
Christopher Faulet | 81e2017 | 2019-12-12 16:40:30 +0100 | [diff] [blame] | 2060 | return ACT_RET_PRS_ERR; |
| 2061 | } |
| 2062 | |
| 2063 | if (strcmp(args[cur_arg], "table") == 0) { |
| 2064 | cur_arg++; |
| 2065 | if (!*args[cur_arg]) { |
| 2066 | memprintf(err, "missing table name"); |
Christopher Faulet | 1337b32 | 2020-01-14 14:50:55 +0100 | [diff] [blame] | 2067 | release_sample_expr(expr); |
Christopher Faulet | 81e2017 | 2019-12-12 16:40:30 +0100 | [diff] [blame] | 2068 | return ACT_RET_PRS_ERR; |
| 2069 | } |
| 2070 | |
| 2071 | /* we copy the table name for now, it will be resolved later */ |
| 2072 | rule->arg.trk_ctr.table.n = strdup(args[cur_arg]); |
| 2073 | cur_arg++; |
| 2074 | } |
| 2075 | |
Christopher Faulet | ac98d81 | 2019-12-18 09:20:16 +0100 | [diff] [blame] | 2076 | rule->action = tsc_num; |
Christopher Faulet | 81e2017 | 2019-12-12 16:40:30 +0100 | [diff] [blame] | 2077 | rule->arg.trk_ctr.expr = expr; |
Christopher Faulet | ac98d81 | 2019-12-18 09:20:16 +0100 | [diff] [blame] | 2078 | rule->action_ptr = http_action_track_sc; |
Christopher Faulet | 2eb5396 | 2020-01-14 14:47:34 +0100 | [diff] [blame] | 2079 | rule->release_ptr = release_http_track_sc; |
Christopher Faulet | 81e2017 | 2019-12-12 16:40:30 +0100 | [diff] [blame] | 2080 | rule->check_ptr = check_trk_action; |
| 2081 | |
| 2082 | *orig_arg = cur_arg; |
| 2083 | return ACT_RET_PRS_OK; |
| 2084 | } |
| 2085 | |
Amaury Denoyelle | 8d22823 | 2020-12-10 13:43:54 +0100 | [diff] [blame] | 2086 | static enum act_return action_timeout_set_stream_timeout(struct act_rule *rule, |
| 2087 | struct proxy *px, |
| 2088 | struct session *sess, |
| 2089 | struct stream *s, |
| 2090 | int flags) |
| 2091 | { |
| 2092 | struct sample *key; |
| 2093 | |
| 2094 | if (rule->arg.timeout.expr) { |
| 2095 | key = sample_fetch_as_type(px, sess, s, SMP_OPT_FINAL, rule->arg.timeout.expr, SMP_T_SINT); |
| 2096 | if (!key) |
| 2097 | return ACT_RET_CONT; |
| 2098 | |
| 2099 | stream_set_timeout(s, rule->arg.timeout.type, MS_TO_TICKS(key->data.u.sint)); |
| 2100 | } |
| 2101 | else { |
| 2102 | stream_set_timeout(s, rule->arg.timeout.type, MS_TO_TICKS(rule->arg.timeout.value)); |
| 2103 | } |
| 2104 | |
| 2105 | return ACT_RET_CONT; |
| 2106 | } |
| 2107 | |
| 2108 | /* Parse a "set-timeout" action. Returns ACT_RET_PRS_ERR if parsing error. |
| 2109 | */ |
| 2110 | static enum act_parse_ret parse_http_set_timeout(const char **args, |
| 2111 | int *orig_arg, |
| 2112 | struct proxy *px, |
| 2113 | struct act_rule *rule, char **err) |
| 2114 | { |
| 2115 | int cur_arg; |
| 2116 | |
| 2117 | rule->action = ACT_CUSTOM; |
| 2118 | rule->action_ptr = action_timeout_set_stream_timeout; |
| 2119 | rule->release_ptr = release_timeout_action; |
| 2120 | |
| 2121 | cur_arg = *orig_arg; |
| 2122 | if (!*args[cur_arg] || !*args[cur_arg + 1]) { |
| 2123 | memprintf(err, "expects exactly 2 arguments"); |
| 2124 | return ACT_RET_PRS_ERR; |
| 2125 | } |
| 2126 | |
| 2127 | if (!(px->cap & PR_CAP_BE)) { |
| 2128 | memprintf(err, "proxy '%s' has no backend capability", px->id); |
| 2129 | return ACT_RET_PRS_ERR; |
| 2130 | } |
| 2131 | |
| 2132 | if (cfg_parse_rule_set_timeout(args, cur_arg, |
| 2133 | &rule->arg.timeout.value, |
| 2134 | &rule->arg.timeout.type, |
| 2135 | &rule->arg.timeout.expr, |
| 2136 | err, |
| 2137 | px->conf.args.file, |
| 2138 | px->conf.args.line, &px->conf.args) == -1) { |
| 2139 | return ACT_RET_PRS_ERR; |
| 2140 | } |
| 2141 | |
| 2142 | *orig_arg = cur_arg + 2; |
| 2143 | |
| 2144 | return ACT_RET_PRS_OK; |
| 2145 | } |
| 2146 | |
Christopher Faulet | 46f9554 | 2019-12-20 10:07:22 +0100 | [diff] [blame] | 2147 | /* This function executes a strict-mode actions. On success, it always returns |
| 2148 | * ACT_RET_CONT |
| 2149 | */ |
| 2150 | static enum act_return http_action_strict_mode(struct act_rule *rule, struct proxy *px, |
| 2151 | struct session *sess, struct stream *s, int flags) |
| 2152 | { |
| 2153 | struct http_msg *msg = ((rule->from == ACT_F_HTTP_REQ) ? &s->txn->req : &s->txn->rsp); |
| 2154 | |
| 2155 | if (rule->action == 0) // strict-mode on |
| 2156 | msg->flags &= ~HTTP_MSGF_SOFT_RW; |
| 2157 | else // strict-mode off |
| 2158 | msg->flags |= HTTP_MSGF_SOFT_RW; |
| 2159 | return ACT_RET_CONT; |
| 2160 | } |
| 2161 | |
| 2162 | /* Parse a "strict-mode" action. It returns ACT_RET_PRS_OK on success, |
| 2163 | * ACT_RET_PRS_ERR on error. |
| 2164 | */ |
| 2165 | static enum act_parse_ret parse_http_strict_mode(const char **args, int *orig_arg, struct proxy *px, |
| 2166 | struct act_rule *rule, char **err) |
| 2167 | { |
| 2168 | int cur_arg; |
| 2169 | |
Christopher Faulet | 46f9554 | 2019-12-20 10:07:22 +0100 | [diff] [blame] | 2170 | cur_arg = *orig_arg; |
| 2171 | if (!*args[cur_arg]) { |
| 2172 | memprintf(err, "expects exactly 1 arguments"); |
| 2173 | return ACT_RET_PRS_ERR; |
| 2174 | } |
| 2175 | |
| 2176 | if (strcasecmp(args[cur_arg], "on") == 0) |
| 2177 | rule->action = 0; // strict-mode on |
| 2178 | else if (strcasecmp(args[cur_arg], "off") == 0) |
| 2179 | rule->action = 1; // strict-mode off |
| 2180 | else { |
| 2181 | memprintf(err, "Unexpected value '%s'. Only 'on' and 'off' are supported", args[cur_arg]); |
| 2182 | return ACT_RET_PRS_ERR; |
| 2183 | } |
| 2184 | rule->action_ptr = http_action_strict_mode; |
| 2185 | |
| 2186 | *orig_arg = cur_arg + 1; |
| 2187 | return ACT_RET_PRS_OK; |
| 2188 | } |
| 2189 | |
Christopher Faulet | 24231ab | 2020-01-24 17:44:23 +0100 | [diff] [blame] | 2190 | /* This function executes a return action. It builds an HTX message from an |
| 2191 | * errorfile, an raw file or a log-format string, depending on <.action> |
| 2192 | * value. On success, it returns ACT_RET_ABRT. If an error occurs ACT_RET_ERR is |
| 2193 | * returned. |
| 2194 | */ |
| 2195 | static enum act_return http_action_return(struct act_rule *rule, struct proxy *px, |
| 2196 | struct session *sess, struct stream *s, int flags) |
| 2197 | { |
| 2198 | struct channel *req = &s->req; |
Christopher Faulet | 24231ab | 2020-01-24 17:44:23 +0100 | [diff] [blame] | 2199 | |
Christopher Faulet | 2d36df2 | 2021-02-19 11:41:01 +0100 | [diff] [blame] | 2200 | s->txn->status = rule->arg.http_reply->status; |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 2201 | if (http_reply_message(s, rule->arg.http_reply) == -1) |
| 2202 | return ACT_RET_ERR; |
Christopher Faulet | 24231ab | 2020-01-24 17:44:23 +0100 | [diff] [blame] | 2203 | |
Christopher Faulet | 24231ab | 2020-01-24 17:44:23 +0100 | [diff] [blame] | 2204 | if (rule->from == ACT_F_HTTP_REQ) { |
| 2205 | /* let's log the request time */ |
| 2206 | s->logs.tv_request = now; |
| 2207 | req->analysers &= AN_REQ_FLT_END; |
| 2208 | |
| 2209 | if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */ |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2210 | _HA_ATOMIC_INC(&s->sess->fe->fe_counters.intercepted_req); |
Christopher Faulet | 24231ab | 2020-01-24 17:44:23 +0100 | [diff] [blame] | 2211 | } |
| 2212 | |
| 2213 | if (!(s->flags & SF_ERR_MASK)) |
| 2214 | s->flags |= SF_ERR_LOCAL; |
| 2215 | if (!(s->flags & SF_FINST_MASK)) |
| 2216 | s->flags |= ((rule->from == ACT_F_HTTP_REQ) ? SF_FINST_R : SF_FINST_H); |
| 2217 | |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 2218 | return ACT_RET_ABRT; |
Christopher Faulet | 24231ab | 2020-01-24 17:44:23 +0100 | [diff] [blame] | 2219 | } |
| 2220 | |
Christopher Faulet | 24231ab | 2020-01-24 17:44:23 +0100 | [diff] [blame] | 2221 | /* Parse a "return" action. It returns ACT_RET_PRS_OK on success, |
Christopher Faulet | 47e791e | 2020-05-13 14:36:55 +0200 | [diff] [blame] | 2222 | * ACT_RET_PRS_ERR on error. It relies on http_parse_http_reply() to set |
| 2223 | * <.arg.http_reply>. |
Christopher Faulet | 24231ab | 2020-01-24 17:44:23 +0100 | [diff] [blame] | 2224 | */ |
| 2225 | static enum act_parse_ret parse_http_return(const char **args, int *orig_arg, struct proxy *px, |
| 2226 | struct act_rule *rule, char **err) |
| 2227 | { |
Christopher Faulet | 47e791e | 2020-05-13 14:36:55 +0200 | [diff] [blame] | 2228 | /* Prepare parsing of log-format strings */ |
| 2229 | px->conf.args.ctx = ((rule->from == ACT_F_HTTP_REQ) ? ARGC_HRQ : ARGC_HRS); |
| 2230 | rule->arg.http_reply = http_parse_http_reply(args, orig_arg, px, 200, err); |
| 2231 | if (!rule->arg.http_reply) |
| 2232 | return ACT_RET_PRS_ERR; |
Christopher Faulet | 24231ab | 2020-01-24 17:44:23 +0100 | [diff] [blame] | 2233 | |
Christopher Faulet | ba946bf | 2020-05-13 08:50:07 +0200 | [diff] [blame] | 2234 | rule->flags |= ACT_FLAG_FINAL; |
Christopher Faulet | 5ff0c64 | 2020-05-12 18:33:37 +0200 | [diff] [blame] | 2235 | rule->action = ACT_CUSTOM; |
Christopher Faulet | 5cb513a | 2020-05-13 17:56:56 +0200 | [diff] [blame] | 2236 | rule->check_ptr = check_act_http_reply; |
Christopher Faulet | 24231ab | 2020-01-24 17:44:23 +0100 | [diff] [blame] | 2237 | rule->action_ptr = http_action_return; |
Christopher Faulet | 5cb513a | 2020-05-13 17:56:56 +0200 | [diff] [blame] | 2238 | rule->release_ptr = release_act_http_reply; |
Christopher Faulet | 24231ab | 2020-01-24 17:44:23 +0100 | [diff] [blame] | 2239 | return ACT_RET_PRS_OK; |
Christopher Faulet | 24231ab | 2020-01-24 17:44:23 +0100 | [diff] [blame] | 2240 | } |
| 2241 | |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 2242 | |
| 2243 | |
| 2244 | /* This function executes a wait-for-body action. It waits for the message |
| 2245 | * payload for a max configured time (.arg.p[0]) and eventually for only first |
| 2246 | * <arg.p[1]> bytes (0 means no limit). It relies on http_wait_for_msg_body() |
| 2247 | * function. it returns ACT_RET_CONT when conditions are met to stop to wait. |
| 2248 | * Otherwise ACT_RET_YIELD is returned to wait for more data. ACT_RET_INV is |
| 2249 | * returned if a parsing error is raised by lower level and ACT_RET_ERR if an |
| 2250 | * internal error occured. Finally ACT_RET_ABRT is returned when a timeout |
| 2251 | * occured. |
| 2252 | */ |
| 2253 | static enum act_return http_action_wait_for_body(struct act_rule *rule, struct proxy *px, |
| 2254 | struct session *sess, struct stream *s, int flags) |
| 2255 | { |
| 2256 | struct channel *chn = ((rule->from == ACT_F_HTTP_REQ) ? &s->req : &s->res); |
| 2257 | unsigned int time = (uintptr_t)rule->arg.act.p[0]; |
| 2258 | unsigned int bytes = (uintptr_t)rule->arg.act.p[1]; |
| 2259 | |
| 2260 | switch (http_wait_for_msg_body(s, chn, time, bytes)) { |
| 2261 | case HTTP_RULE_RES_CONT: |
| 2262 | return ACT_RET_CONT; |
| 2263 | case HTTP_RULE_RES_YIELD: |
| 2264 | return ACT_RET_YIELD; |
| 2265 | case HTTP_RULE_RES_BADREQ: |
| 2266 | return ACT_RET_INV; |
| 2267 | case HTTP_RULE_RES_ERROR: |
| 2268 | return ACT_RET_ERR; |
| 2269 | case HTTP_RULE_RES_ABRT: |
| 2270 | return ACT_RET_ABRT; |
| 2271 | default: |
| 2272 | return ACT_RET_ERR; |
| 2273 | } |
| 2274 | } |
| 2275 | |
| 2276 | /* Parse a "wait-for-body" action. It returns ACT_RET_PRS_OK on success, |
| 2277 | * ACT_RET_PRS_ERR on error. |
| 2278 | */ |
| 2279 | static enum act_parse_ret parse_http_wait_for_body(const char **args, int *orig_arg, struct proxy *px, |
| 2280 | struct act_rule *rule, char **err) |
| 2281 | { |
| 2282 | int cur_arg; |
| 2283 | unsigned int time, bytes; |
| 2284 | const char *res; |
| 2285 | |
| 2286 | cur_arg = *orig_arg; |
| 2287 | if (!*args[cur_arg]) { |
| 2288 | memprintf(err, "expects time <time> [ at-least <bytes> ]"); |
| 2289 | return ACT_RET_PRS_ERR; |
| 2290 | } |
| 2291 | |
| 2292 | time = UINT_MAX; /* To be sure it is set */ |
| 2293 | bytes = 0; /* Default value, wait all the body */ |
| 2294 | while (*(args[cur_arg])) { |
| 2295 | if (strcmp(args[cur_arg], "time") == 0) { |
| 2296 | if (!*args[cur_arg + 1]) { |
| 2297 | memprintf(err, "missing argument for '%s'", args[cur_arg]); |
| 2298 | return ACT_RET_PRS_ERR; |
| 2299 | } |
| 2300 | res = parse_time_err(args[cur_arg+1], &time, TIME_UNIT_MS); |
| 2301 | if (res == PARSE_TIME_OVER) { |
| 2302 | memprintf(err, "time overflow (maximum value is 2147483647 ms or ~24.8 days)"); |
| 2303 | return ACT_RET_PRS_ERR; |
| 2304 | } |
| 2305 | if (res == PARSE_TIME_UNDER) { |
| 2306 | memprintf(err, "time underflow (minimum non-null value is 1 ms)"); |
| 2307 | return ACT_RET_PRS_ERR; |
| 2308 | } |
| 2309 | if (res) { |
| 2310 | memprintf(err, "unexpected character '%c'", *res); |
| 2311 | return ACT_RET_PRS_ERR; |
| 2312 | } |
| 2313 | cur_arg++; |
| 2314 | } |
| 2315 | else if (strcmp(args[cur_arg], "at-least") == 0) { |
| 2316 | if (!*args[cur_arg + 1]) { |
| 2317 | memprintf(err, "missing argument for '%s'", args[cur_arg]); |
| 2318 | return ACT_RET_PRS_ERR; |
| 2319 | } |
| 2320 | res = parse_size_err(args[cur_arg+1], &bytes); |
| 2321 | if (res) { |
| 2322 | memprintf(err, "unexpected character '%c'", *res); |
| 2323 | return ACT_RET_PRS_ERR; |
| 2324 | } |
| 2325 | cur_arg++; |
| 2326 | } |
| 2327 | else |
| 2328 | break; |
| 2329 | cur_arg++; |
| 2330 | } |
| 2331 | |
| 2332 | if (time == UINT_MAX) { |
| 2333 | memprintf(err, "expects time <time> [ at-least <bytes> ]"); |
| 2334 | return ACT_RET_PRS_ERR; |
| 2335 | } |
| 2336 | |
| 2337 | rule->arg.act.p[0] = (void *)(uintptr_t)time; |
| 2338 | rule->arg.act.p[1] = (void *)(uintptr_t)bytes; |
| 2339 | |
| 2340 | *orig_arg = cur_arg; |
| 2341 | |
| 2342 | rule->action = ACT_CUSTOM; |
| 2343 | rule->action_ptr = http_action_wait_for_body; |
| 2344 | return ACT_RET_PRS_OK; |
| 2345 | } |
| 2346 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2347 | /************************************************************************/ |
| 2348 | /* All supported http-request action keywords must be declared here. */ |
| 2349 | /************************************************************************/ |
| 2350 | |
| 2351 | static struct action_kw_list http_req_actions = { |
| 2352 | .kw = { |
Christopher Faulet | 81e2017 | 2019-12-12 16:40:30 +0100 | [diff] [blame] | 2353 | { "add-acl", parse_http_set_map, 1 }, |
| 2354 | { "add-header", parse_http_set_header, 0 }, |
| 2355 | { "allow", parse_http_allow, 0 }, |
| 2356 | { "auth", parse_http_auth, 0 }, |
| 2357 | { "capture", parse_http_req_capture, 0 }, |
| 2358 | { "del-acl", parse_http_set_map, 1 }, |
| 2359 | { "del-header", parse_http_del_header, 0 }, |
| 2360 | { "del-map", parse_http_set_map, 1 }, |
Christopher Faulet | e0fca29 | 2020-01-13 21:49:03 +0100 | [diff] [blame] | 2361 | { "deny", parse_http_deny, 0 }, |
Christopher Faulet | 81e2017 | 2019-12-12 16:40:30 +0100 | [diff] [blame] | 2362 | { "disable-l7-retry", parse_http_req_disable_l7_retry, 0 }, |
| 2363 | { "early-hint", parse_http_set_header, 0 }, |
Tim Duesterhus | d2bedcc | 2021-04-15 21:45:57 +0200 | [diff] [blame] | 2364 | { "normalize-uri", parse_http_normalize_uri, 0 }, |
Christopher Faulet | 81e2017 | 2019-12-12 16:40:30 +0100 | [diff] [blame] | 2365 | { "redirect", parse_http_redirect, 0 }, |
| 2366 | { "reject", parse_http_action_reject, 0 }, |
| 2367 | { "replace-header", parse_http_replace_header, 0 }, |
| 2368 | { "replace-path", parse_replace_uri, 0 }, |
Christopher Faulet | 312294f | 2020-09-02 17:17:44 +0200 | [diff] [blame] | 2369 | { "replace-pathq", parse_replace_uri, 0 }, |
Christopher Faulet | 81e2017 | 2019-12-12 16:40:30 +0100 | [diff] [blame] | 2370 | { "replace-uri", parse_replace_uri, 0 }, |
| 2371 | { "replace-value", parse_http_replace_header, 0 }, |
Christopher Faulet | 24231ab | 2020-01-24 17:44:23 +0100 | [diff] [blame] | 2372 | { "return", parse_http_return, 0 }, |
Christopher Faulet | 81e2017 | 2019-12-12 16:40:30 +0100 | [diff] [blame] | 2373 | { "set-header", parse_http_set_header, 0 }, |
| 2374 | { "set-log-level", parse_http_set_log_level, 0 }, |
| 2375 | { "set-map", parse_http_set_map, 1 }, |
| 2376 | { "set-method", parse_set_req_line, 0 }, |
| 2377 | { "set-mark", parse_http_set_mark, 0 }, |
| 2378 | { "set-nice", parse_http_set_nice, 0 }, |
| 2379 | { "set-path", parse_set_req_line, 0 }, |
Christopher Faulet | 312294f | 2020-09-02 17:17:44 +0200 | [diff] [blame] | 2380 | { "set-pathq", parse_set_req_line, 0 }, |
Christopher Faulet | 81e2017 | 2019-12-12 16:40:30 +0100 | [diff] [blame] | 2381 | { "set-query", parse_set_req_line, 0 }, |
| 2382 | { "set-tos", parse_http_set_tos, 0 }, |
| 2383 | { "set-uri", parse_set_req_line, 0 }, |
Christopher Faulet | 46f9554 | 2019-12-20 10:07:22 +0100 | [diff] [blame] | 2384 | { "strict-mode", parse_http_strict_mode, 0 }, |
Christopher Faulet | e0fca29 | 2020-01-13 21:49:03 +0100 | [diff] [blame] | 2385 | { "tarpit", parse_http_deny, 0 }, |
Christopher Faulet | 81e2017 | 2019-12-12 16:40:30 +0100 | [diff] [blame] | 2386 | { "track-sc", parse_http_track_sc, 1 }, |
Amaury Denoyelle | 8d22823 | 2020-12-10 13:43:54 +0100 | [diff] [blame] | 2387 | { "set-timeout", parse_http_set_timeout, 0 }, |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 2388 | { "wait-for-body", parse_http_wait_for_body, 0 }, |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2389 | { NULL, NULL } |
| 2390 | } |
| 2391 | }; |
| 2392 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 2393 | INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_actions); |
| 2394 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2395 | static struct action_kw_list http_res_actions = { |
| 2396 | .kw = { |
Christopher Faulet | 81e2017 | 2019-12-12 16:40:30 +0100 | [diff] [blame] | 2397 | { "add-acl", parse_http_set_map, 1 }, |
| 2398 | { "add-header", parse_http_set_header, 0 }, |
| 2399 | { "allow", parse_http_allow, 0 }, |
| 2400 | { "capture", parse_http_res_capture, 0 }, |
| 2401 | { "del-acl", parse_http_set_map, 1 }, |
| 2402 | { "del-header", parse_http_del_header, 0 }, |
| 2403 | { "del-map", parse_http_set_map, 1 }, |
Christopher Faulet | e0fca29 | 2020-01-13 21:49:03 +0100 | [diff] [blame] | 2404 | { "deny", parse_http_deny, 0 }, |
Christopher Faulet | 81e2017 | 2019-12-12 16:40:30 +0100 | [diff] [blame] | 2405 | { "redirect", parse_http_redirect, 0 }, |
| 2406 | { "replace-header", parse_http_replace_header, 0 }, |
| 2407 | { "replace-value", parse_http_replace_header, 0 }, |
Christopher Faulet | 24231ab | 2020-01-24 17:44:23 +0100 | [diff] [blame] | 2408 | { "return", parse_http_return, 0 }, |
Christopher Faulet | 81e2017 | 2019-12-12 16:40:30 +0100 | [diff] [blame] | 2409 | { "set-header", parse_http_set_header, 0 }, |
| 2410 | { "set-log-level", parse_http_set_log_level, 0 }, |
| 2411 | { "set-map", parse_http_set_map, 1 }, |
| 2412 | { "set-mark", parse_http_set_mark, 0 }, |
| 2413 | { "set-nice", parse_http_set_nice, 0 }, |
| 2414 | { "set-status", parse_http_set_status, 0 }, |
| 2415 | { "set-tos", parse_http_set_tos, 0 }, |
Christopher Faulet | 46f9554 | 2019-12-20 10:07:22 +0100 | [diff] [blame] | 2416 | { "strict-mode", parse_http_strict_mode, 0 }, |
Christopher Faulet | 81e2017 | 2019-12-12 16:40:30 +0100 | [diff] [blame] | 2417 | { "track-sc", parse_http_track_sc, 1 }, |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 2418 | { "wait-for-body", parse_http_wait_for_body, 0 }, |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2419 | { NULL, NULL } |
| 2420 | } |
| 2421 | }; |
| 2422 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 2423 | INITCALL1(STG_REGISTER, http_res_keywords_register, &http_res_actions); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2424 | |
Christopher Faulet | 6d0c3df | 2020-01-22 09:26:35 +0100 | [diff] [blame] | 2425 | static struct action_kw_list http_after_res_actions = { |
| 2426 | .kw = { |
| 2427 | { "add-header", parse_http_set_header, 0 }, |
| 2428 | { "allow", parse_http_allow, 0 }, |
| 2429 | { "del-header", parse_http_del_header, 0 }, |
| 2430 | { "replace-header", parse_http_replace_header, 0 }, |
| 2431 | { "replace-value", parse_http_replace_header, 0 }, |
| 2432 | { "set-header", parse_http_set_header, 0 }, |
| 2433 | { "set-status", parse_http_set_status, 0 }, |
| 2434 | { "strict-mode", parse_http_strict_mode, 0 }, |
| 2435 | { NULL, NULL } |
| 2436 | } |
| 2437 | }; |
| 2438 | |
| 2439 | INITCALL1(STG_REGISTER, http_after_res_keywords_register, &http_after_res_actions); |
| 2440 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2441 | /* |
| 2442 | * Local variables: |
| 2443 | * c-indent-level: 8 |
| 2444 | * c-basic-offset: 8 |
| 2445 | * End: |
| 2446 | */ |