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