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