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