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