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