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