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