Willy Tarreau | 61c112a | 2018-10-02 16:43:32 +0200 | [diff] [blame] | 1 | /* |
| 2 | * HTTP rules parsing and registration |
| 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 | dfd3de8 | 2020-06-04 23:46:14 +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 | 0ce6dc0 | 2021-10-06 16:38:53 +0200 | [diff] [blame] | 28 | #include <haproxy/http_ana-t.h> |
Willy Tarreau | c761f84 | 2020-06-04 11:40:28 +0200 | [diff] [blame] | 29 | #include <haproxy/http_rules.h> |
Willy Tarreau | 36979d9 | 2020-06-05 17:27:29 +0200 | [diff] [blame] | 30 | #include <haproxy/log.h> |
Willy Tarreau | d0ef439 | 2020-06-02 09:38:52 +0200 | [diff] [blame] | 31 | #include <haproxy/pool.h> |
Willy Tarreau | d1dd250 | 2021-05-08 20:30:37 +0200 | [diff] [blame] | 32 | #include <haproxy/proxy.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 33 | #include <haproxy/sample.h> |
Willy Tarreau | 48fbcae | 2020-06-03 18:09:46 +0200 | [diff] [blame] | 34 | #include <haproxy/tools.h> |
Willy Tarreau | d678805 | 2020-05-27 15:59:00 +0200 | [diff] [blame] | 35 | #include <haproxy/version.h> |
Willy Tarreau | 61c112a | 2018-10-02 16:43:32 +0200 | [diff] [blame] | 36 | |
Willy Tarreau | 61c112a | 2018-10-02 16:43:32 +0200 | [diff] [blame] | 37 | |
| 38 | /* List head of all known action keywords for "http-request" */ |
| 39 | struct action_kw_list http_req_keywords = { |
| 40 | .list = LIST_HEAD_INIT(http_req_keywords.list) |
| 41 | }; |
| 42 | |
| 43 | /* List head of all known action keywords for "http-response" */ |
| 44 | struct action_kw_list http_res_keywords = { |
| 45 | .list = LIST_HEAD_INIT(http_res_keywords.list) |
| 46 | }; |
| 47 | |
Christopher Faulet | 6d0c3df | 2020-01-22 09:26:35 +0100 | [diff] [blame] | 48 | /* List head of all known action keywords for "http-after-response" */ |
| 49 | struct action_kw_list http_after_res_keywords = { |
| 50 | .list = LIST_HEAD_INIT(http_after_res_keywords.list) |
| 51 | }; |
| 52 | |
Willy Tarreau | 61c112a | 2018-10-02 16:43:32 +0200 | [diff] [blame] | 53 | /* |
| 54 | * Return the struct http_req_action_kw associated to a keyword. |
| 55 | */ |
Thierry Fournier | 7a71a6d | 2020-11-28 17:40:24 +0100 | [diff] [blame] | 56 | struct action_kw *action_http_req_custom(const char *kw) |
Willy Tarreau | 61c112a | 2018-10-02 16:43:32 +0200 | [diff] [blame] | 57 | { |
| 58 | return action_lookup(&http_req_keywords.list, kw); |
| 59 | } |
| 60 | |
| 61 | /* |
| 62 | * Return the struct http_res_action_kw associated to a keyword. |
| 63 | */ |
Thierry Fournier | 7a71a6d | 2020-11-28 17:40:24 +0100 | [diff] [blame] | 64 | struct action_kw *action_http_res_custom(const char *kw) |
Willy Tarreau | 61c112a | 2018-10-02 16:43:32 +0200 | [diff] [blame] | 65 | { |
| 66 | return action_lookup(&http_res_keywords.list, kw); |
| 67 | } |
| 68 | |
Christopher Faulet | 6d0c3df | 2020-01-22 09:26:35 +0100 | [diff] [blame] | 69 | /* |
| 70 | * Return the struct http_after_res_action_kw associated to a keyword. |
| 71 | */ |
Thierry Fournier | 7a71a6d | 2020-11-28 17:40:24 +0100 | [diff] [blame] | 72 | struct action_kw *action_http_after_res_custom(const char *kw) |
Christopher Faulet | 6d0c3df | 2020-01-22 09:26:35 +0100 | [diff] [blame] | 73 | { |
| 74 | return action_lookup(&http_after_res_keywords.list, kw); |
| 75 | } |
| 76 | |
Willy Tarreau | 61c112a | 2018-10-02 16:43:32 +0200 | [diff] [blame] | 77 | /* parse an "http-request" rule */ |
| 78 | struct act_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy) |
| 79 | { |
| 80 | struct act_rule *rule; |
Willy Tarreau | 49bf7be | 2021-03-12 12:01:34 +0100 | [diff] [blame] | 81 | const struct action_kw *custom = NULL; |
Willy Tarreau | 61c112a | 2018-10-02 16:43:32 +0200 | [diff] [blame] | 82 | int cur_arg; |
Willy Tarreau | 61c112a | 2018-10-02 16:43:32 +0200 | [diff] [blame] | 83 | |
Willy Tarreau | d535f80 | 2021-10-11 08:49:26 +0200 | [diff] [blame] | 84 | rule = new_act_rule(ACT_F_HTTP_REQ, file, linenum); |
Willy Tarreau | 61c112a | 2018-10-02 16:43:32 +0200 | [diff] [blame] | 85 | if (!rule) { |
| 86 | ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum); |
| 87 | goto out_err; |
| 88 | } |
| 89 | |
Christopher Faulet | 81e2017 | 2019-12-12 16:40:30 +0100 | [diff] [blame] | 90 | if (((custom = action_http_req_custom(args[0])) != NULL)) { |
Willy Tarreau | 61c112a | 2018-10-02 16:43:32 +0200 | [diff] [blame] | 91 | char *errmsg = NULL; |
| 92 | |
Willy Tarreau | 61c112a | 2018-10-02 16:43:32 +0200 | [diff] [blame] | 93 | cur_arg = 1; |
| 94 | /* try in the module list */ |
Willy Tarreau | 61c112a | 2018-10-02 16:43:32 +0200 | [diff] [blame] | 95 | rule->kw = custom; |
Amaury Denoyelle | 0351773 | 2021-05-07 14:25:01 +0200 | [diff] [blame] | 96 | |
| 97 | if (custom->flags & KWF_EXPERIMENTAL) { |
| 98 | if (!experimental_directives_allowed) { |
| 99 | ha_alert("parsing [%s:%d] : '%s' action is experimental, must be allowed via a global 'expose-experimental-directives'\n", |
| 100 | file, linenum, custom->kw); |
| 101 | goto out_err; |
| 102 | } |
| 103 | mark_tainted(TAINTED_CONFIG_EXP_KW_DECLARED); |
| 104 | } |
| 105 | |
Willy Tarreau | 61c112a | 2018-10-02 16:43:32 +0200 | [diff] [blame] | 106 | if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) == ACT_RET_PRS_ERR) { |
| 107 | ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n", |
| 108 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg); |
| 109 | free(errmsg); |
| 110 | goto out_err; |
| 111 | } |
Christopher Faulet | 81e2017 | 2019-12-12 16:40:30 +0100 | [diff] [blame] | 112 | else if (errmsg) { |
| 113 | ha_warning("parsing [%s:%d] : %s.\n", file, linenum, errmsg); |
| 114 | free(errmsg); |
| 115 | } |
| 116 | } |
| 117 | else { |
Willy Tarreau | 49bf7be | 2021-03-12 12:01:34 +0100 | [diff] [blame] | 118 | const char *best = action_suggest(args[0], &http_req_keywords.list, NULL); |
| 119 | |
Willy Tarreau | 61c112a | 2018-10-02 16:43:32 +0200 | [diff] [blame] | 120 | action_build_list(&http_req_keywords.list, &trash); |
Willy Tarreau | 49bf7be | 2021-03-12 12:01:34 +0100 | [diff] [blame] | 121 | ha_alert("parsing [%s:%d]: 'http-request' expects %s, but got '%s'%s.%s%s%s\n", |
| 122 | file, linenum, trash.area, |
| 123 | args[0], *args[0] ? "" : " (missing argument)", |
| 124 | best ? " Did you mean '" : "", |
| 125 | best ? best : "", |
| 126 | best ? "' maybe ?" : ""); |
Willy Tarreau | 61c112a | 2018-10-02 16:43:32 +0200 | [diff] [blame] | 127 | goto out_err; |
| 128 | } |
| 129 | |
| 130 | if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) { |
| 131 | struct acl_cond *cond; |
| 132 | char *errmsg = NULL; |
| 133 | |
| 134 | if ((cond = build_acl_cond(file, linenum, &proxy->acl, proxy, args+cur_arg, &errmsg)) == NULL) { |
| 135 | ha_alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n", |
| 136 | file, linenum, args[0], errmsg); |
| 137 | free(errmsg); |
| 138 | goto out_err; |
| 139 | } |
| 140 | rule->cond = cond; |
| 141 | } |
| 142 | else if (*args[cur_arg]) { |
Christopher Faulet | 81e2017 | 2019-12-12 16:40:30 +0100 | [diff] [blame] | 143 | ha_alert("parsing [%s:%d]: 'http-request %s' expects" |
Willy Tarreau | 61c112a | 2018-10-02 16:43:32 +0200 | [diff] [blame] | 144 | " either 'if' or 'unless' followed by a condition but found '%s'.\n", |
| 145 | file, linenum, args[0], args[cur_arg]); |
| 146 | goto out_err; |
| 147 | } |
| 148 | |
| 149 | return rule; |
| 150 | out_err: |
| 151 | free(rule); |
| 152 | return NULL; |
| 153 | } |
| 154 | |
| 155 | /* parse an "http-respose" rule */ |
| 156 | struct act_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy) |
| 157 | { |
| 158 | struct act_rule *rule; |
Willy Tarreau | 49bf7be | 2021-03-12 12:01:34 +0100 | [diff] [blame] | 159 | const struct action_kw *custom = NULL; |
Willy Tarreau | 61c112a | 2018-10-02 16:43:32 +0200 | [diff] [blame] | 160 | int cur_arg; |
Willy Tarreau | 61c112a | 2018-10-02 16:43:32 +0200 | [diff] [blame] | 161 | |
Willy Tarreau | d535f80 | 2021-10-11 08:49:26 +0200 | [diff] [blame] | 162 | rule = new_act_rule(ACT_F_HTTP_RES, file, linenum); |
Willy Tarreau | 61c112a | 2018-10-02 16:43:32 +0200 | [diff] [blame] | 163 | if (!rule) { |
| 164 | ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum); |
| 165 | goto out_err; |
| 166 | } |
Willy Tarreau | 61c112a | 2018-10-02 16:43:32 +0200 | [diff] [blame] | 167 | |
Christopher Faulet | 81e2017 | 2019-12-12 16:40:30 +0100 | [diff] [blame] | 168 | if (((custom = action_http_res_custom(args[0])) != NULL)) { |
Willy Tarreau | 61c112a | 2018-10-02 16:43:32 +0200 | [diff] [blame] | 169 | char *errmsg = NULL; |
| 170 | |
Willy Tarreau | 61c112a | 2018-10-02 16:43:32 +0200 | [diff] [blame] | 171 | cur_arg = 1; |
| 172 | /* try in the module list */ |
Willy Tarreau | 61c112a | 2018-10-02 16:43:32 +0200 | [diff] [blame] | 173 | rule->kw = custom; |
Amaury Denoyelle | 0351773 | 2021-05-07 14:25:01 +0200 | [diff] [blame] | 174 | |
| 175 | if (custom->flags & KWF_EXPERIMENTAL) { |
| 176 | if (!experimental_directives_allowed) { |
| 177 | ha_alert("parsing [%s:%d] : '%s' action is experimental, must be allowed via a global 'expose-experimental-directives'\n", |
| 178 | file, linenum, custom->kw); |
| 179 | goto out_err; |
| 180 | } |
| 181 | mark_tainted(TAINTED_CONFIG_EXP_KW_DECLARED); |
| 182 | } |
| 183 | |
Willy Tarreau | 61c112a | 2018-10-02 16:43:32 +0200 | [diff] [blame] | 184 | if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) == ACT_RET_PRS_ERR) { |
| 185 | ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n", |
| 186 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg); |
| 187 | free(errmsg); |
| 188 | goto out_err; |
| 189 | } |
Christopher Faulet | 81e2017 | 2019-12-12 16:40:30 +0100 | [diff] [blame] | 190 | else if (errmsg) { |
| 191 | ha_warning("parsing [%s:%d] : %s.\n", file, linenum, errmsg); |
| 192 | free(errmsg); |
| 193 | } |
| 194 | } |
| 195 | else { |
Willy Tarreau | 49bf7be | 2021-03-12 12:01:34 +0100 | [diff] [blame] | 196 | const char *best = action_suggest(args[0], &http_res_keywords.list, NULL); |
| 197 | |
Willy Tarreau | 61c112a | 2018-10-02 16:43:32 +0200 | [diff] [blame] | 198 | action_build_list(&http_res_keywords.list, &trash); |
Willy Tarreau | 49bf7be | 2021-03-12 12:01:34 +0100 | [diff] [blame] | 199 | ha_alert("parsing [%s:%d]: 'http-response' expects %s, but got '%s'%s.%s%s%s\n", |
| 200 | file, linenum, trash.area, |
| 201 | args[0], *args[0] ? "" : " (missing argument)", |
| 202 | best ? " Did you mean '" : "", |
| 203 | best ? best : "", |
| 204 | best ? "' maybe ?" : ""); |
Willy Tarreau | 61c112a | 2018-10-02 16:43:32 +0200 | [diff] [blame] | 205 | goto out_err; |
| 206 | } |
| 207 | |
| 208 | if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) { |
| 209 | struct acl_cond *cond; |
| 210 | char *errmsg = NULL; |
| 211 | |
| 212 | if ((cond = build_acl_cond(file, linenum, &proxy->acl, proxy, args+cur_arg, &errmsg)) == NULL) { |
| 213 | ha_alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n", |
| 214 | file, linenum, args[0], errmsg); |
| 215 | free(errmsg); |
| 216 | goto out_err; |
| 217 | } |
| 218 | rule->cond = cond; |
| 219 | } |
| 220 | else if (*args[cur_arg]) { |
| 221 | ha_alert("parsing [%s:%d]: 'http-response %s' expects" |
| 222 | " either 'if' or 'unless' followed by a condition but found '%s'.\n", |
| 223 | file, linenum, args[0], args[cur_arg]); |
| 224 | goto out_err; |
| 225 | } |
| 226 | |
| 227 | return rule; |
| 228 | out_err: |
| 229 | free(rule); |
| 230 | return NULL; |
| 231 | } |
| 232 | |
Christopher Faulet | 6d0c3df | 2020-01-22 09:26:35 +0100 | [diff] [blame] | 233 | |
| 234 | /* parse an "http-after-response" rule */ |
| 235 | struct act_rule *parse_http_after_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy) |
| 236 | { |
| 237 | struct act_rule *rule; |
Willy Tarreau | 49bf7be | 2021-03-12 12:01:34 +0100 | [diff] [blame] | 238 | const struct action_kw *custom = NULL; |
Christopher Faulet | 6d0c3df | 2020-01-22 09:26:35 +0100 | [diff] [blame] | 239 | int cur_arg; |
| 240 | |
Willy Tarreau | d535f80 | 2021-10-11 08:49:26 +0200 | [diff] [blame] | 241 | rule = new_act_rule(ACT_F_HTTP_RES, file, linenum); |
Christopher Faulet | 6d0c3df | 2020-01-22 09:26:35 +0100 | [diff] [blame] | 242 | if (!rule) { |
| 243 | ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum); |
| 244 | goto out_err; |
| 245 | } |
Christopher Faulet | 6d0c3df | 2020-01-22 09:26:35 +0100 | [diff] [blame] | 246 | |
| 247 | if (((custom = action_http_after_res_custom(args[0])) != NULL)) { |
| 248 | char *errmsg = NULL; |
| 249 | |
| 250 | cur_arg = 1; |
| 251 | /* try in the module list */ |
| 252 | rule->kw = custom; |
| 253 | if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) == ACT_RET_PRS_ERR) { |
| 254 | ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-after-response %s' rule : %s.\n", |
| 255 | file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg); |
| 256 | free(errmsg); |
| 257 | goto out_err; |
| 258 | } |
| 259 | else if (errmsg) { |
| 260 | ha_warning("parsing [%s:%d] : %s.\n", file, linenum, errmsg); |
| 261 | free(errmsg); |
| 262 | } |
| 263 | } |
| 264 | else { |
Willy Tarreau | 49bf7be | 2021-03-12 12:01:34 +0100 | [diff] [blame] | 265 | const char *best = action_suggest(args[0], &http_after_res_keywords.list, NULL); |
| 266 | |
Christopher Faulet | 6d0c3df | 2020-01-22 09:26:35 +0100 | [diff] [blame] | 267 | action_build_list(&http_after_res_keywords.list, &trash); |
Willy Tarreau | 49bf7be | 2021-03-12 12:01:34 +0100 | [diff] [blame] | 268 | ha_alert("parsing [%s:%d]: 'http-after-response' expects %s, but got '%s'%s.%s%s%s\n", |
| 269 | file, linenum, trash.area, |
| 270 | args[0], *args[0] ? "" : " (missing argument)", |
| 271 | best ? " Did you mean '" : "", |
| 272 | best ? best : "", |
| 273 | best ? "' maybe ?" : ""); |
Christopher Faulet | 6d0c3df | 2020-01-22 09:26:35 +0100 | [diff] [blame] | 274 | goto out_err; |
| 275 | } |
| 276 | |
| 277 | if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) { |
| 278 | struct acl_cond *cond; |
| 279 | char *errmsg = NULL; |
| 280 | |
| 281 | if ((cond = build_acl_cond(file, linenum, &proxy->acl, proxy, args+cur_arg, &errmsg)) == NULL) { |
| 282 | ha_alert("parsing [%s:%d] : error detected while parsing an 'http-after-response %s' condition : %s.\n", |
| 283 | file, linenum, args[0], errmsg); |
| 284 | free(errmsg); |
| 285 | goto out_err; |
| 286 | } |
| 287 | rule->cond = cond; |
| 288 | } |
| 289 | else if (*args[cur_arg]) { |
| 290 | ha_alert("parsing [%s:%d]: 'http-after-response %s' expects" |
| 291 | " either 'if' or 'unless' followed by a condition but found '%s'.\n", |
| 292 | file, linenum, args[0], args[cur_arg]); |
| 293 | goto out_err; |
| 294 | } |
| 295 | |
| 296 | return rule; |
| 297 | out_err: |
| 298 | free(rule); |
| 299 | return NULL; |
| 300 | } |
| 301 | |
Willy Tarreau | 61c112a | 2018-10-02 16:43:32 +0200 | [diff] [blame] | 302 | /* Parses a redirect rule. Returns the redirect rule on success or NULL on error, |
| 303 | * with <err> filled with the error message. If <use_fmt> is not null, builds a |
| 304 | * dynamic log-format rule instead of a static string. Parameter <dir> indicates |
| 305 | * the direction of the rule, and equals 0 for request, non-zero for responses. |
| 306 | */ |
| 307 | struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy, |
| 308 | const char **args, char **errmsg, int use_fmt, int dir) |
| 309 | { |
| 310 | struct redirect_rule *rule; |
| 311 | int cur_arg; |
| 312 | int type = REDIRECT_TYPE_NONE; |
| 313 | int code = 302; |
| 314 | const char *destination = NULL; |
| 315 | const char *cookie = NULL; |
| 316 | int cookie_set = 0; |
Christopher Faulet | c87e468 | 2020-01-28 09:13:41 +0100 | [diff] [blame] | 317 | unsigned int flags = (!dir ? REDIRECT_FLAG_FROM_REQ : REDIRECT_FLAG_NONE); |
Willy Tarreau | 61c112a | 2018-10-02 16:43:32 +0200 | [diff] [blame] | 318 | struct acl_cond *cond = NULL; |
| 319 | |
| 320 | cur_arg = 0; |
| 321 | while (*(args[cur_arg])) { |
| 322 | if (strcmp(args[cur_arg], "location") == 0) { |
| 323 | if (!*args[cur_arg + 1]) |
| 324 | goto missing_arg; |
| 325 | |
| 326 | type = REDIRECT_TYPE_LOCATION; |
| 327 | cur_arg++; |
| 328 | destination = args[cur_arg]; |
| 329 | } |
| 330 | else if (strcmp(args[cur_arg], "prefix") == 0) { |
| 331 | if (!*args[cur_arg + 1]) |
| 332 | goto missing_arg; |
| 333 | type = REDIRECT_TYPE_PREFIX; |
| 334 | cur_arg++; |
| 335 | destination = args[cur_arg]; |
| 336 | } |
| 337 | else if (strcmp(args[cur_arg], "scheme") == 0) { |
| 338 | if (!*args[cur_arg + 1]) |
| 339 | goto missing_arg; |
| 340 | |
| 341 | type = REDIRECT_TYPE_SCHEME; |
| 342 | cur_arg++; |
| 343 | destination = args[cur_arg]; |
| 344 | } |
| 345 | else if (strcmp(args[cur_arg], "set-cookie") == 0) { |
| 346 | if (!*args[cur_arg + 1]) |
| 347 | goto missing_arg; |
| 348 | |
| 349 | cur_arg++; |
| 350 | cookie = args[cur_arg]; |
| 351 | cookie_set = 1; |
| 352 | } |
| 353 | else if (strcmp(args[cur_arg], "clear-cookie") == 0) { |
| 354 | if (!*args[cur_arg + 1]) |
| 355 | goto missing_arg; |
| 356 | |
| 357 | cur_arg++; |
| 358 | cookie = args[cur_arg]; |
| 359 | cookie_set = 0; |
| 360 | } |
| 361 | else if (strcmp(args[cur_arg], "code") == 0) { |
| 362 | if (!*args[cur_arg + 1]) |
| 363 | goto missing_arg; |
| 364 | |
| 365 | cur_arg++; |
| 366 | code = atol(args[cur_arg]); |
| 367 | if (code < 301 || code > 308 || (code > 303 && code < 307)) { |
| 368 | memprintf(errmsg, |
| 369 | "'%s': unsupported HTTP code '%s' (must be one of 301, 302, 303, 307 or 308)", |
| 370 | args[cur_arg - 1], args[cur_arg]); |
| 371 | return NULL; |
| 372 | } |
| 373 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 374 | else if (strcmp(args[cur_arg], "drop-query") == 0) { |
Willy Tarreau | 61c112a | 2018-10-02 16:43:32 +0200 | [diff] [blame] | 375 | flags |= REDIRECT_FLAG_DROP_QS; |
| 376 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 377 | else if (strcmp(args[cur_arg], "append-slash") == 0) { |
Willy Tarreau | 61c112a | 2018-10-02 16:43:32 +0200 | [diff] [blame] | 378 | flags |= REDIRECT_FLAG_APPEND_SLASH; |
| 379 | } |
Willy Tarreau | bc1223b | 2021-09-02 16:54:33 +0200 | [diff] [blame] | 380 | else if (strcmp(args[cur_arg], "ignore-empty") == 0) { |
| 381 | flags |= REDIRECT_FLAG_IGNORE_EMPTY; |
| 382 | } |
Willy Tarreau | 61c112a | 2018-10-02 16:43:32 +0200 | [diff] [blame] | 383 | else if (strcmp(args[cur_arg], "if") == 0 || |
| 384 | strcmp(args[cur_arg], "unless") == 0) { |
| 385 | cond = build_acl_cond(file, linenum, &curproxy->acl, curproxy, (const char **)args + cur_arg, errmsg); |
| 386 | if (!cond) { |
| 387 | memprintf(errmsg, "error in condition: %s", *errmsg); |
| 388 | return NULL; |
| 389 | } |
| 390 | break; |
| 391 | } |
| 392 | else { |
| 393 | memprintf(errmsg, |
Willy Tarreau | bc1223b | 2021-09-02 16:54:33 +0200 | [diff] [blame] | 394 | "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query', 'ignore-empty' or 'append-slash' (was '%s')", |
Willy Tarreau | 61c112a | 2018-10-02 16:43:32 +0200 | [diff] [blame] | 395 | args[cur_arg]); |
| 396 | return NULL; |
| 397 | } |
| 398 | cur_arg++; |
| 399 | } |
| 400 | |
| 401 | if (type == REDIRECT_TYPE_NONE) { |
| 402 | memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')"); |
| 403 | return NULL; |
| 404 | } |
| 405 | |
| 406 | if (dir && type != REDIRECT_TYPE_LOCATION) { |
| 407 | memprintf(errmsg, "response only supports redirect type 'location'"); |
| 408 | return NULL; |
| 409 | } |
| 410 | |
| 411 | rule = calloc(1, sizeof(*rule)); |
Remi Tricot-Le Breton | b6864a5 | 2021-05-19 11:32:04 +0200 | [diff] [blame] | 412 | if (!rule) { |
| 413 | memprintf(errmsg, "parsing [%s:%d]: out of memory.", file, linenum); |
| 414 | return NULL; |
| 415 | } |
Willy Tarreau | 61c112a | 2018-10-02 16:43:32 +0200 | [diff] [blame] | 416 | rule->cond = cond; |
| 417 | LIST_INIT(&rule->rdr_fmt); |
| 418 | |
| 419 | if (!use_fmt) { |
| 420 | /* old-style static redirect rule */ |
| 421 | rule->rdr_str = strdup(destination); |
| 422 | rule->rdr_len = strlen(destination); |
| 423 | } |
| 424 | else { |
| 425 | /* log-format based redirect rule */ |
Christopher Faulet | 7a06ffb | 2021-10-13 17:22:17 +0200 | [diff] [blame] | 426 | int cap = 0; |
Willy Tarreau | 61c112a | 2018-10-02 16:43:32 +0200 | [diff] [blame] | 427 | |
| 428 | /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case, |
| 429 | * if prefix == "/", we don't want to add anything, otherwise it |
| 430 | * makes it hard for the user to configure a self-redirection. |
| 431 | */ |
| 432 | curproxy->conf.args.ctx = ARGC_RDR; |
Christopher Faulet | 7a06ffb | 2021-10-13 17:22:17 +0200 | [diff] [blame] | 433 | if (curproxy->cap & PR_CAP_FE) |
| 434 | cap |= (dir ? SMP_VAL_FE_HRS_HDR : SMP_VAL_FE_HRQ_HDR); |
| 435 | if (curproxy->cap & PR_CAP_BE) |
| 436 | cap |= (dir ? SMP_VAL_BE_HRS_HDR : SMP_VAL_BE_HRQ_HDR); |
Willy Tarreau | 61c112a | 2018-10-02 16:43:32 +0200 | [diff] [blame] | 437 | if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) { |
Christopher Faulet | 7a06ffb | 2021-10-13 17:22:17 +0200 | [diff] [blame] | 438 | if (!parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP, cap, errmsg)) { |
Willy Tarreau | 61c112a | 2018-10-02 16:43:32 +0200 | [diff] [blame] | 439 | return NULL; |
| 440 | } |
| 441 | free(curproxy->conf.lfs_file); |
| 442 | curproxy->conf.lfs_file = strdup(curproxy->conf.args.file); |
| 443 | curproxy->conf.lfs_line = curproxy->conf.args.line; |
| 444 | } |
| 445 | } |
| 446 | |
| 447 | if (cookie) { |
| 448 | /* depending on cookie_set, either we want to set the cookie, or to clear it. |
| 449 | * a clear consists in appending "; path=/; Max-Age=0;" at the end. |
| 450 | */ |
| 451 | rule->cookie_len = strlen(cookie); |
| 452 | if (cookie_set) { |
| 453 | rule->cookie_str = malloc(rule->cookie_len + 10); |
| 454 | memcpy(rule->cookie_str, cookie, rule->cookie_len); |
| 455 | memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10); |
| 456 | rule->cookie_len += 9; |
| 457 | } else { |
| 458 | rule->cookie_str = malloc(rule->cookie_len + 21); |
| 459 | memcpy(rule->cookie_str, cookie, rule->cookie_len); |
| 460 | memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21); |
| 461 | rule->cookie_len += 20; |
| 462 | } |
| 463 | } |
| 464 | rule->type = type; |
| 465 | rule->code = code; |
| 466 | rule->flags = flags; |
| 467 | LIST_INIT(&rule->list); |
| 468 | return rule; |
| 469 | |
| 470 | missing_arg: |
| 471 | memprintf(errmsg, "missing argument for '%s'", args[cur_arg]); |
| 472 | return NULL; |
| 473 | } |
| 474 | |
Willy Tarreau | 61c112a | 2018-10-02 16:43:32 +0200 | [diff] [blame] | 475 | __attribute__((constructor)) |
| 476 | static void __http_rules_init(void) |
| 477 | { |
| 478 | } |
| 479 | |
| 480 | /* |
| 481 | * Local variables: |
| 482 | * c-indent-level: 8 |
| 483 | * c-basic-offset: 8 |
| 484 | * End: |
| 485 | */ |