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