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