Christopher Faulet | 78fbb9f | 2019-08-11 23:11:03 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Functions about FCGI applications and filters. |
| 3 | * |
| 4 | * Copyright (C) 2019 HAProxy Technologies, Christopher Faulet <cfaulet@haproxy.com> |
| 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 | |
Willy Tarreau | dcc048a | 2020-06-04 19:11:43 +0200 | [diff] [blame] | 13 | #include <haproxy/acl.h> |
Willy Tarreau | 4c7e4b7 | 2020-05-27 12:58:42 +0200 | [diff] [blame] | 14 | #include <haproxy/api.h> |
Willy Tarreau | 6be7849 | 2020-06-05 00:00:29 +0200 | [diff] [blame] | 15 | #include <haproxy/cfgparse.h> |
Willy Tarreau | c13ed53 | 2020-06-02 10:22:45 +0200 | [diff] [blame] | 16 | #include <haproxy/chunk.h> |
Willy Tarreau | 8d36697 | 2020-05-27 16:10:29 +0200 | [diff] [blame] | 17 | #include <haproxy/errors.h> |
Willy Tarreau | c659968 | 2020-06-04 21:33:21 +0200 | [diff] [blame] | 18 | #include <haproxy/fcgi-app.h> |
Willy Tarreau | c7babd8 | 2020-06-04 21:29:29 +0200 | [diff] [blame] | 19 | #include <haproxy/filters.h> |
Willy Tarreau | 126ba3a | 2020-06-04 18:26:43 +0200 | [diff] [blame] | 20 | #include <haproxy/http_fetch.h> |
Willy Tarreau | 8773533 | 2020-06-04 09:08:41 +0200 | [diff] [blame] | 21 | #include <haproxy/http_htx.h> |
Willy Tarreau | aeed4a8 | 2020-06-04 22:01:04 +0200 | [diff] [blame] | 22 | #include <haproxy/log.h> |
Willy Tarreau | 03f839d | 2021-05-08 20:23:18 +0200 | [diff] [blame] | 23 | #include <haproxy/proxy.h> |
Willy Tarreau | 7cd8b6e | 2020-06-02 17:32:26 +0200 | [diff] [blame] | 24 | #include <haproxy/regex.h> |
Willy Tarreau | e6ce10b | 2020-06-04 15:33:47 +0200 | [diff] [blame] | 25 | #include <haproxy/sample.h> |
Willy Tarreau | 1e56f92 | 2020-06-04 23:20:13 +0200 | [diff] [blame] | 26 | #include <haproxy/server-t.h> |
Willy Tarreau | 48d25b3 | 2020-06-04 18:58:52 +0200 | [diff] [blame] | 27 | #include <haproxy/session.h> |
Christopher Faulet | c97406f | 2020-06-22 11:07:18 +0200 | [diff] [blame] | 28 | #include <haproxy/sink.h> |
Willy Tarreau | 48fbcae | 2020-06-03 18:09:46 +0200 | [diff] [blame] | 29 | #include <haproxy/tools.h> |
Christopher Faulet | 78fbb9f | 2019-08-11 23:11:03 +0200 | [diff] [blame] | 30 | |
Christopher Faulet | 78fbb9f | 2019-08-11 23:11:03 +0200 | [diff] [blame] | 31 | |
Christopher Faulet | 78fbb9f | 2019-08-11 23:11:03 +0200 | [diff] [blame] | 32 | /* Global list of all FCGI applications */ |
| 33 | static struct fcgi_app *fcgi_apps = NULL; |
| 34 | |
| 35 | struct flt_ops fcgi_flt_ops; |
| 36 | const char *fcgi_flt_id = "FCGI filter"; |
| 37 | |
| 38 | DECLARE_STATIC_POOL(pool_head_fcgi_flt_ctx, "fcgi_flt_ctx", sizeof(struct fcgi_flt_ctx)); |
| 39 | DECLARE_STATIC_POOL(pool_head_fcgi_param_rule, "fcgi_param_rule", sizeof(struct fcgi_param_rule)); |
| 40 | DECLARE_STATIC_POOL(pool_head_fcgi_hdr_rule, "fcgi_hdr_rule", sizeof(struct fcgi_hdr_rule)); |
| 41 | |
| 42 | /**************************************************************************/ |
| 43 | /***************************** Uitls **************************************/ |
| 44 | /**************************************************************************/ |
| 45 | /* Makes a fcgi parameter name (prefixed by ':fcgi-') with <name> (in |
| 46 | * lowercase). All non alphanumeric character are replaced by an underscore |
Ilya Shipitsin | c6ecf56 | 2021-08-07 14:41:56 +0500 | [diff] [blame] | 47 | * ('_'). The result is copied into <dst>. the corresponding ist is returned. |
Christopher Faulet | 78fbb9f | 2019-08-11 23:11:03 +0200 | [diff] [blame] | 48 | */ |
| 49 | static struct ist fcgi_param_name(char *dst, const struct ist name) |
| 50 | { |
| 51 | size_t ofs1, ofs2; |
| 52 | |
| 53 | memcpy(dst, ":fcgi-", 6); |
| 54 | ofs1 = 6; |
| 55 | for (ofs2 = 0; ofs2 < name.len; ofs2++) { |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 56 | if (isalnum((unsigned char)name.ptr[ofs2])) |
Christopher Faulet | 78fbb9f | 2019-08-11 23:11:03 +0200 | [diff] [blame] | 57 | dst[ofs1++] = ist_lc[(unsigned char)name.ptr[ofs2]]; |
| 58 | else |
| 59 | dst[ofs1++] = '_'; |
| 60 | } |
| 61 | return ist2(dst, ofs1); |
| 62 | } |
| 63 | |
Ilya Shipitsin | 47d1718 | 2020-06-21 21:42:57 +0500 | [diff] [blame] | 64 | /* Returns a pointer to the FCGi application matching the name <name>. NULL is |
Christopher Faulet | 78fbb9f | 2019-08-11 23:11:03 +0200 | [diff] [blame] | 65 | * returned if no match found. |
| 66 | */ |
| 67 | struct fcgi_app *fcgi_app_find_by_name(const char *name) |
| 68 | { |
| 69 | struct fcgi_app *app; |
| 70 | |
| 71 | for (app = fcgi_apps; app != NULL; app = app->next) { |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 72 | if (strcmp(app->name, name) == 0) |
Christopher Faulet | 78fbb9f | 2019-08-11 23:11:03 +0200 | [diff] [blame] | 73 | return app; |
| 74 | } |
| 75 | |
| 76 | return NULL; |
| 77 | } |
| 78 | |
| 79 | struct fcgi_flt_conf *find_px_fcgi_conf(struct proxy *px) |
| 80 | { |
| 81 | struct flt_conf *fconf; |
| 82 | |
| 83 | list_for_each_entry(fconf, &px->filter_configs, list) { |
| 84 | if (fconf->id == fcgi_flt_id) |
| 85 | return fconf->conf; |
| 86 | } |
| 87 | return NULL; |
| 88 | } |
| 89 | |
| 90 | struct fcgi_flt_ctx *find_strm_fcgi_ctx(struct stream *s) |
| 91 | { |
| 92 | struct filter *filter; |
| 93 | |
| 94 | if (!s) |
| 95 | return NULL; |
| 96 | |
| 97 | list_for_each_entry(filter, &strm_flt(s)->filters, list) { |
| 98 | if (FLT_ID(filter) == fcgi_flt_id) |
| 99 | return FLT_CONF(filter); |
| 100 | } |
| 101 | return NULL; |
| 102 | } |
| 103 | |
| 104 | struct fcgi_app *get_px_fcgi_app(struct proxy *px) |
| 105 | { |
| 106 | struct fcgi_flt_conf *fcgi_conf = find_px_fcgi_conf(px); |
| 107 | |
| 108 | if (fcgi_conf) |
| 109 | return fcgi_conf->app; |
| 110 | return NULL; |
| 111 | } |
| 112 | |
| 113 | struct fcgi_app *get_strm_fcgi_app(struct stream *s) |
| 114 | { |
| 115 | struct fcgi_flt_ctx *fcgi_ctx = find_strm_fcgi_ctx(s); |
| 116 | |
| 117 | if (fcgi_ctx) |
| 118 | return fcgi_ctx->app; |
| 119 | return NULL; |
| 120 | } |
| 121 | |
| 122 | static void fcgi_release_rule_conf(struct fcgi_rule_conf *rule) |
| 123 | { |
| 124 | if (!rule) |
| 125 | return; |
| 126 | free(rule->name); |
| 127 | free(rule->value); |
| 128 | if (rule->cond) { |
| 129 | prune_acl_cond(rule->cond); |
| 130 | free(rule->cond); |
| 131 | } |
| 132 | free(rule); |
| 133 | } |
| 134 | |
| 135 | static void fcgi_release_rule(struct fcgi_rule *rule) |
| 136 | { |
| 137 | if (!rule) |
| 138 | return; |
| 139 | |
| 140 | if (!LIST_ISEMPTY(&rule->value)) { |
| 141 | struct logformat_node *lf, *lfb; |
| 142 | |
| 143 | list_for_each_entry_safe(lf, lfb, &rule->value, list) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 144 | LIST_DELETE(&lf->list); |
Christopher Faulet | 78fbb9f | 2019-08-11 23:11:03 +0200 | [diff] [blame] | 145 | release_sample_expr(lf->expr); |
| 146 | free(lf->arg); |
| 147 | free(lf); |
| 148 | } |
| 149 | } |
| 150 | /* ->cond and ->name are not owned by the rule */ |
| 151 | free(rule); |
| 152 | } |
| 153 | |
| 154 | /**************************************************************************/ |
| 155 | /*********************** FCGI Sample fetches ******************************/ |
| 156 | /**************************************************************************/ |
| 157 | |
| 158 | static int smp_fetch_fcgi_docroot(const struct arg *args, struct sample *smp, |
| 159 | const char *kw, void *private) |
| 160 | { |
| 161 | struct fcgi_app *app = get_strm_fcgi_app(smp->strm); |
| 162 | |
| 163 | if (!app) |
| 164 | return 0; |
| 165 | |
| 166 | smp->data.type = SMP_T_STR; |
| 167 | smp->data.u.str.area = app->docroot.ptr; |
| 168 | smp->data.u.str.data = app->docroot.len; |
| 169 | smp->flags = SMP_F_CONST; |
| 170 | return 1; |
| 171 | } |
| 172 | |
| 173 | static int smp_fetch_fcgi_index(const struct arg *args, struct sample *smp, |
| 174 | const char *kw, void *private) |
| 175 | { |
| 176 | struct fcgi_app *app = get_strm_fcgi_app(smp->strm); |
| 177 | |
| 178 | if (!app || !istlen(app->index)) |
| 179 | return 0; |
| 180 | |
| 181 | smp->data.type = SMP_T_STR; |
| 182 | smp->data.u.str.area = app->index.ptr; |
| 183 | smp->data.u.str.data = app->index.len; |
| 184 | smp->flags = SMP_F_CONST; |
| 185 | return 1; |
| 186 | } |
| 187 | |
| 188 | /**************************************************************************/ |
| 189 | /************************** FCGI filter ***********************************/ |
| 190 | /**************************************************************************/ |
| 191 | static int fcgi_flt_init(struct proxy *px, struct flt_conf *fconf) |
| 192 | { |
| 193 | fconf->flags |= FLT_CFG_FL_HTX; |
| 194 | return 0; |
| 195 | } |
| 196 | |
| 197 | static void fcgi_flt_deinit(struct proxy *px, struct flt_conf *fconf) |
| 198 | { |
| 199 | struct fcgi_flt_conf *fcgi_conf = fconf->conf; |
| 200 | struct fcgi_rule *rule, *back; |
| 201 | |
| 202 | if (!fcgi_conf) |
| 203 | return; |
| 204 | |
| 205 | free(fcgi_conf->name); |
| 206 | |
| 207 | list_for_each_entry_safe(rule, back, &fcgi_conf->param_rules, list) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 208 | LIST_DELETE(&rule->list); |
Christopher Faulet | 78fbb9f | 2019-08-11 23:11:03 +0200 | [diff] [blame] | 209 | fcgi_release_rule(rule); |
| 210 | } |
| 211 | |
| 212 | list_for_each_entry_safe(rule, back, &fcgi_conf->hdr_rules, list) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 213 | LIST_DELETE(&rule->list); |
Christopher Faulet | 78fbb9f | 2019-08-11 23:11:03 +0200 | [diff] [blame] | 214 | fcgi_release_rule(rule); |
| 215 | } |
| 216 | |
| 217 | free(fcgi_conf); |
| 218 | } |
| 219 | |
| 220 | static int fcgi_flt_check(struct proxy *px, struct flt_conf *fconf) |
| 221 | { |
| 222 | struct fcgi_flt_conf *fcgi_conf = fconf->conf; |
| 223 | struct fcgi_rule_conf *crule, *back; |
| 224 | struct fcgi_rule *rule = NULL; |
| 225 | struct flt_conf *f; |
| 226 | char *errmsg = NULL; |
| 227 | |
| 228 | fcgi_conf->app = fcgi_app_find_by_name(fcgi_conf->name); |
| 229 | if (!fcgi_conf->app) { |
| 230 | ha_alert("config : proxy '%s' : fcgi-app '%s' not found.\n", |
| 231 | px->id, fcgi_conf->name); |
| 232 | goto err; |
| 233 | } |
| 234 | |
| 235 | list_for_each_entry(f, &px->filter_configs, list) { |
| 236 | if (f->id == http_comp_flt_id || f->id == cache_store_flt_id) |
| 237 | continue; |
| 238 | else if ((f->id == fconf->id) && f->conf != fcgi_conf) { |
| 239 | ha_alert("config : proxy '%s' : only one fcgi-app supported per backend.\n", |
| 240 | px->id); |
| 241 | goto err; |
| 242 | } |
| 243 | else if (f->id != fconf->id) { |
| 244 | /* Implicit declaration is only allowed with the |
| 245 | * compression and cache. For other filters, an implicit |
| 246 | * declaration is required. */ |
| 247 | ha_alert("config: proxy '%s': require an explicit filter declaration " |
| 248 | "to use the fcgi-app '%s'.\n", px->id, fcgi_conf->name); |
| 249 | goto err; |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | list_for_each_entry_safe(crule, back, &fcgi_conf->app->conf.rules, list) { |
| 254 | rule = calloc(1, sizeof(*rule)); |
| 255 | if (!rule) { |
| 256 | ha_alert("config : proxy '%s' : out of memory.\n", px->id); |
| 257 | goto err; |
| 258 | } |
| 259 | rule->type = crule->type; |
| 260 | rule->name = ist(crule->name); |
| 261 | rule->cond = crule->cond; |
| 262 | LIST_INIT(&rule->value); |
| 263 | |
| 264 | if (crule->value) { |
| 265 | if (!parse_logformat_string(crule->value, px, &rule->value, LOG_OPT_HTTP, |
| 266 | SMP_VAL_BE_HRQ_HDR, &errmsg)) { |
| 267 | ha_alert("config : proxy '%s' : %s.\n", px->id, errmsg); |
| 268 | goto err; |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | if (rule->type == FCGI_RULE_SET_PARAM || rule->type == FCGI_RULE_UNSET_PARAM) |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 273 | LIST_APPEND(&fcgi_conf->param_rules, &rule->list); |
Christopher Faulet | 78fbb9f | 2019-08-11 23:11:03 +0200 | [diff] [blame] | 274 | else /* FCGI_RULE_PASS_HDR/FCGI_RULE_HIDE_HDR */ |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 275 | LIST_APPEND(&fcgi_conf->hdr_rules, &rule->list); |
Christopher Faulet | 78fbb9f | 2019-08-11 23:11:03 +0200 | [diff] [blame] | 276 | rule = NULL; |
| 277 | } |
| 278 | return 0; |
| 279 | |
| 280 | err: |
| 281 | free(errmsg); |
| 282 | free(rule); |
| 283 | return 1; |
| 284 | } |
| 285 | |
| 286 | static int fcgi_flt_start(struct stream *s, struct filter *filter) |
| 287 | { |
| 288 | struct fcgi_flt_conf *fcgi_conf = FLT_CONF(filter); |
| 289 | struct fcgi_flt_ctx *fcgi_ctx; |
| 290 | |
Willy Tarreau | 18f43d8 | 2021-03-22 15:07:37 +0100 | [diff] [blame] | 291 | fcgi_ctx = pool_alloc(pool_head_fcgi_flt_ctx); |
Christopher Faulet | 78fbb9f | 2019-08-11 23:11:03 +0200 | [diff] [blame] | 292 | if (fcgi_ctx == NULL) { |
| 293 | // FIXME: send a warning |
| 294 | return 0; |
| 295 | } |
| 296 | fcgi_ctx->filter = filter; |
| 297 | fcgi_ctx->app = fcgi_conf->app; |
| 298 | filter->ctx = fcgi_ctx; |
| 299 | |
| 300 | s->req.analysers |= AN_REQ_HTTP_BODY; |
| 301 | return 1; |
| 302 | } |
| 303 | |
| 304 | static void fcgi_flt_stop(struct stream *s, struct filter *filter) |
| 305 | { |
| 306 | struct flt_fcgi_ctx *fcgi_ctx = filter->ctx; |
| 307 | |
| 308 | if (!fcgi_ctx) |
| 309 | return; |
| 310 | pool_free(pool_head_fcgi_flt_ctx, fcgi_ctx); |
| 311 | filter->ctx = NULL; |
| 312 | } |
| 313 | |
| 314 | static int fcgi_flt_http_headers(struct stream *s, struct filter *filter, struct http_msg *msg) |
| 315 | { |
| 316 | struct session *sess = strm_sess(s); |
| 317 | struct buffer *value; |
| 318 | struct fcgi_flt_conf *fcgi_conf = FLT_CONF(filter); |
| 319 | struct fcgi_rule *rule; |
| 320 | struct fcgi_param_rule *param_rule; |
| 321 | struct fcgi_hdr_rule *hdr_rule; |
| 322 | struct ebpt_node *node, *next; |
| 323 | struct eb_root param_rules = EB_ROOT; |
| 324 | struct eb_root hdr_rules = EB_ROOT; |
| 325 | struct htx *htx; |
| 326 | struct http_hdr_ctx ctx; |
| 327 | int ret; |
| 328 | |
| 329 | htx = htxbuf(&msg->chn->buf); |
| 330 | |
| 331 | if (msg->chn->flags & CF_ISRESP) { |
| 332 | struct htx_sl *sl; |
| 333 | |
| 334 | /* Remove the header "Status:" from the response */ |
| 335 | ctx.blk = NULL; |
| 336 | while (http_find_header(htx, ist("status"), &ctx, 1)) |
| 337 | http_remove_header(htx, &ctx); |
| 338 | |
| 339 | /* Add the header "Date:" if not found */ |
| 340 | ctx.blk = NULL; |
| 341 | if (!http_find_header(htx, ist("date"), &ctx, 1)) { |
| 342 | struct tm tm; |
| 343 | |
| 344 | get_gmtime(date.tv_sec, &tm); |
| 345 | trash.data = strftime(trash.area, trash.size, "%a, %d %b %Y %T %Z", &tm); |
| 346 | if (trash.data) |
| 347 | http_add_header(htx, ist("date"), ist2(trash.area, trash.data)); |
| 348 | } |
| 349 | |
| 350 | /* Add the header "Content-Length:" if possible */ |
| 351 | sl = http_get_stline(htx); |
Christopher Faulet | cc1d2f3 | 2022-04-06 15:29:34 +0200 | [diff] [blame] | 352 | if (s->txn->meth != HTTP_METH_HEAD && sl && |
Christopher Faulet | 1cbc9a2 | 2022-04-15 15:26:24 +0200 | [diff] [blame] | 353 | (msg->flags & (HTTP_MSGF_XFER_LEN|HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK)) == HTTP_MSGF_XFER_LEN && |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 354 | (htx->flags & HTX_FL_EOM)) { |
Christopher Faulet | 78fbb9f | 2019-08-11 23:11:03 +0200 | [diff] [blame] | 355 | struct htx_blk * blk; |
| 356 | char *end; |
| 357 | size_t len = 0; |
| 358 | |
| 359 | for (blk = htx_get_first_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) { |
| 360 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 361 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 362 | if (type == HTX_BLK_TLR || type == HTX_BLK_EOT) |
Christopher Faulet | 78fbb9f | 2019-08-11 23:11:03 +0200 | [diff] [blame] | 363 | break; |
| 364 | if (type == HTX_BLK_DATA) |
| 365 | len += htx_get_blksz(blk); |
| 366 | } |
| 367 | end = ultoa_o(len, trash.area, trash.size); |
Christopher Faulet | 1cbc9a2 | 2022-04-15 15:26:24 +0200 | [diff] [blame] | 368 | if (http_add_header(htx, ist("content-length"), ist2(trash.area, end-trash.area))) { |
Christopher Faulet | 78fbb9f | 2019-08-11 23:11:03 +0200 | [diff] [blame] | 369 | sl->flags |= HTX_SL_F_CLEN; |
Christopher Faulet | 1cbc9a2 | 2022-04-15 15:26:24 +0200 | [diff] [blame] | 370 | msg->flags |= HTTP_MSGF_CNT_LEN; |
| 371 | } |
Christopher Faulet | 78fbb9f | 2019-08-11 23:11:03 +0200 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | return 1; |
| 375 | } |
| 376 | |
| 377 | /* Analyze the request's headers */ |
| 378 | |
| 379 | value = alloc_trash_chunk(); |
| 380 | if (!value) |
| 381 | goto end; |
| 382 | |
| 383 | list_for_each_entry(rule, &fcgi_conf->param_rules, list) { |
| 384 | if (rule->cond) { |
| 385 | ret = acl_exec_cond(rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL); |
| 386 | ret = acl_pass(ret); |
| 387 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 388 | ret = !ret; |
| 389 | |
| 390 | /* the rule does not match */ |
| 391 | if (!ret) |
| 392 | continue; |
| 393 | } |
| 394 | |
| 395 | param_rule = NULL; |
| 396 | node = ebis_lookup_len(¶m_rules, rule->name.ptr, rule->name.len); |
| 397 | if (node) { |
| 398 | param_rule = container_of(node, struct fcgi_param_rule, node); |
| 399 | ebpt_delete(node); |
| 400 | } |
| 401 | else { |
Willy Tarreau | 18f43d8 | 2021-03-22 15:07:37 +0100 | [diff] [blame] | 402 | param_rule = pool_alloc(pool_head_fcgi_param_rule); |
Christopher Faulet | 78fbb9f | 2019-08-11 23:11:03 +0200 | [diff] [blame] | 403 | if (param_rule == NULL) |
| 404 | goto param_rule_err; |
| 405 | } |
| 406 | |
| 407 | param_rule->node.key = rule->name.ptr; |
| 408 | param_rule->name = rule->name; |
| 409 | param_rule->value = &rule->value; |
| 410 | ebis_insert(¶m_rules, ¶m_rule->node); |
| 411 | } |
| 412 | |
| 413 | list_for_each_entry(rule, &fcgi_conf->hdr_rules, list) { |
| 414 | if (rule->cond) { |
| 415 | ret = acl_exec_cond(rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL); |
| 416 | ret = acl_pass(ret); |
| 417 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 418 | ret = !ret; |
| 419 | |
| 420 | /* the rule does not match */ |
| 421 | if (!ret) |
| 422 | continue; |
| 423 | } |
| 424 | |
| 425 | hdr_rule = NULL; |
| 426 | node = ebis_lookup_len(&hdr_rules, rule->name.ptr, rule->name.len); |
| 427 | if (node) { |
| 428 | hdr_rule = container_of(node, struct fcgi_hdr_rule, node); |
| 429 | ebpt_delete(node); |
| 430 | } |
| 431 | else { |
Willy Tarreau | 18f43d8 | 2021-03-22 15:07:37 +0100 | [diff] [blame] | 432 | hdr_rule = pool_alloc(pool_head_fcgi_hdr_rule); |
Christopher Faulet | 78fbb9f | 2019-08-11 23:11:03 +0200 | [diff] [blame] | 433 | if (hdr_rule == NULL) |
| 434 | goto hdr_rule_err; |
| 435 | } |
| 436 | |
| 437 | hdr_rule->node.key = rule->name.ptr; |
| 438 | hdr_rule->name = rule->name; |
| 439 | hdr_rule->pass = (rule->type == FCGI_RULE_PASS_HDR); |
| 440 | ebis_insert(&hdr_rules, &hdr_rule->node); |
| 441 | } |
| 442 | |
| 443 | node = ebpt_first(¶m_rules); |
| 444 | while (node) { |
| 445 | next = ebpt_next(node); |
| 446 | ebpt_delete(node); |
| 447 | param_rule = container_of(node, struct fcgi_param_rule, node); |
| 448 | node = next; |
| 449 | |
| 450 | b_reset(value); |
| 451 | value->data = build_logline(s, value->area, value->size, param_rule->value); |
Harris Kaufmann | b605a73 | 2020-07-15 16:26:13 +0200 | [diff] [blame] | 452 | if (!value->data) { |
| 453 | pool_free(pool_head_fcgi_param_rule, param_rule); |
Christopher Faulet | 78fbb9f | 2019-08-11 23:11:03 +0200 | [diff] [blame] | 454 | continue; |
Harris Kaufmann | b605a73 | 2020-07-15 16:26:13 +0200 | [diff] [blame] | 455 | } |
Christopher Faulet | 78fbb9f | 2019-08-11 23:11:03 +0200 | [diff] [blame] | 456 | if (!http_add_header(htx, param_rule->name, ist2(value->area, value->data))) |
| 457 | goto rewrite_err; |
| 458 | pool_free(pool_head_fcgi_param_rule, param_rule); |
| 459 | } |
| 460 | |
| 461 | node = ebpt_first(&hdr_rules); |
| 462 | while (node) { |
| 463 | next = ebpt_next(node); |
| 464 | ebpt_delete(node); |
| 465 | hdr_rule = container_of(node, struct fcgi_hdr_rule, node); |
| 466 | node = next; |
| 467 | |
| 468 | if (!hdr_rule->pass) { |
| 469 | ctx.blk = NULL; |
| 470 | while (http_find_header(htx, hdr_rule->name, &ctx, 1)) |
| 471 | http_remove_header(htx, &ctx); |
| 472 | } |
| 473 | pool_free(pool_head_fcgi_hdr_rule, hdr_rule); |
| 474 | } |
| 475 | |
| 476 | goto end; |
| 477 | |
| 478 | rewrite_err: |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 479 | _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_rewrites); |
| 480 | _HA_ATOMIC_INC(&s->be->be_counters.failed_rewrites); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 481 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 482 | _HA_ATOMIC_INC(&sess->listener->counters->failed_rewrites); |
Christopher Faulet | cff0f73 | 2019-12-16 16:13:44 +0100 | [diff] [blame] | 483 | if (objt_server(s->target)) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 484 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_rewrites); |
Christopher Faulet | 78fbb9f | 2019-08-11 23:11:03 +0200 | [diff] [blame] | 485 | hdr_rule_err: |
| 486 | node = ebpt_first(&hdr_rules); |
| 487 | while (node) { |
| 488 | next = ebpt_next(node); |
| 489 | ebpt_delete(node); |
| 490 | hdr_rule = container_of(node, struct fcgi_hdr_rule, node); |
| 491 | node = next; |
| 492 | pool_free(pool_head_fcgi_hdr_rule, hdr_rule); |
| 493 | } |
| 494 | param_rule_err: |
| 495 | node = ebpt_first(¶m_rules); |
| 496 | while (node) { |
| 497 | next = ebpt_next(node); |
| 498 | ebpt_delete(node); |
| 499 | param_rule = container_of(node, struct fcgi_param_rule, node); |
| 500 | node = next; |
| 501 | pool_free(pool_head_fcgi_param_rule, param_rule); |
| 502 | } |
| 503 | end: |
| 504 | free_trash_chunk(value); |
| 505 | return 1; |
| 506 | } |
| 507 | |
| 508 | struct flt_ops fcgi_flt_ops = { |
| 509 | .init = fcgi_flt_init, |
| 510 | .check = fcgi_flt_check, |
| 511 | .deinit = fcgi_flt_deinit, |
| 512 | |
| 513 | .attach = fcgi_flt_start, |
| 514 | .detach = fcgi_flt_stop, |
| 515 | |
| 516 | .http_headers = fcgi_flt_http_headers, |
| 517 | }; |
| 518 | |
| 519 | /**************************************************************************/ |
| 520 | /*********************** FCGI Config parsing ******************************/ |
| 521 | /**************************************************************************/ |
| 522 | static int |
| 523 | parse_fcgi_flt(char **args, int *cur_arg, struct proxy *px, |
| 524 | struct flt_conf *fconf, char **err, void *private) |
| 525 | { |
| 526 | struct flt_conf *f, *back; |
| 527 | struct fcgi_flt_conf *fcgi_conf = NULL; |
| 528 | char *name = NULL; |
| 529 | int pos = *cur_arg; |
| 530 | |
| 531 | /* Get the fcgi-app name*/ |
Christopher Faulet | 0ce57b0 | 2019-09-18 11:18:33 +0200 | [diff] [blame] | 532 | if (!*args[pos + 1]) { |
| 533 | memprintf(err, "%s : expects a <name> argument", args[pos]); |
| 534 | goto err; |
Christopher Faulet | 78fbb9f | 2019-08-11 23:11:03 +0200 | [diff] [blame] | 535 | } |
Christopher Faulet | 0ce57b0 | 2019-09-18 11:18:33 +0200 | [diff] [blame] | 536 | name = strdup(args[pos + 1]); |
| 537 | if (!name) { |
| 538 | memprintf(err, "%s '%s' : out of memory", args[pos], args[pos + 1]); |
| 539 | goto err; |
| 540 | } |
| 541 | pos += 2; |
| 542 | |
Christopher Faulet | 78fbb9f | 2019-08-11 23:11:03 +0200 | [diff] [blame] | 543 | /* Check if an fcgi-app filter with the same name already exists */ |
| 544 | list_for_each_entry_safe(f, back, &px->filter_configs, list) { |
| 545 | if (f->id != fcgi_flt_id) |
| 546 | continue; |
| 547 | fcgi_conf = f->conf; |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 548 | if (strcmp(name, fcgi_conf->name) != 0) { |
Christopher Faulet | 78fbb9f | 2019-08-11 23:11:03 +0200 | [diff] [blame] | 549 | fcgi_conf = NULL; |
| 550 | continue; |
| 551 | } |
| 552 | |
| 553 | /* Place the filter at its right position */ |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 554 | LIST_DELETE(&f->list); |
Christopher Faulet | 78fbb9f | 2019-08-11 23:11:03 +0200 | [diff] [blame] | 555 | free(f); |
Willy Tarreau | 61cfdf4 | 2021-02-20 10:46:51 +0100 | [diff] [blame] | 556 | ha_free(&name); |
Christopher Faulet | 78fbb9f | 2019-08-11 23:11:03 +0200 | [diff] [blame] | 557 | break; |
| 558 | } |
| 559 | |
| 560 | /* No other fcgi-app filter found, create configuration for the explicit one */ |
| 561 | if (!fcgi_conf) { |
| 562 | fcgi_conf = calloc(1, sizeof(*fcgi_conf)); |
| 563 | if (!fcgi_conf) { |
| 564 | memprintf(err, "%s: out of memory", args[*cur_arg]); |
| 565 | goto err; |
| 566 | } |
| 567 | fcgi_conf->name = name; |
| 568 | LIST_INIT(&fcgi_conf->param_rules); |
| 569 | LIST_INIT(&fcgi_conf->hdr_rules); |
| 570 | } |
| 571 | |
| 572 | fconf->id = fcgi_flt_id; |
| 573 | fconf->conf = fcgi_conf; |
| 574 | fconf->ops = &fcgi_flt_ops; |
| 575 | |
| 576 | *cur_arg = pos; |
| 577 | return 0; |
| 578 | err: |
| 579 | free(name); |
Christopher Faulet | 78fbb9f | 2019-08-11 23:11:03 +0200 | [diff] [blame] | 580 | return -1; |
| 581 | } |
| 582 | |
| 583 | /* Parses the "use-fcgi-app" proxy keyword */ |
| 584 | static int proxy_parse_use_fcgi_app(char **args, int section, struct proxy *curpx, |
Willy Tarreau | 0182516 | 2021-03-09 09:53:46 +0100 | [diff] [blame] | 585 | const struct proxy *defpx, const char *file, int line, |
Christopher Faulet | 78fbb9f | 2019-08-11 23:11:03 +0200 | [diff] [blame] | 586 | char **err) |
| 587 | { |
| 588 | struct flt_conf *fconf = NULL; |
| 589 | struct fcgi_flt_conf *fcgi_conf = NULL; |
| 590 | int retval = 0; |
| 591 | |
Aurelien DARRAGON | 22d40c6 | 2023-01-12 15:44:22 +0100 | [diff] [blame] | 592 | if ((curpx->cap & PR_CAP_DEF) || !(curpx->cap & PR_CAP_BE)) { |
Christopher Faulet | 78fbb9f | 2019-08-11 23:11:03 +0200 | [diff] [blame] | 593 | memprintf(err, "'%s' only available in backend or listen section", args[0]); |
| 594 | retval = -1; |
| 595 | goto end; |
| 596 | } |
| 597 | |
| 598 | if (!*(args[1])) { |
| 599 | memprintf(err, "'%s' expects <name> as argument", args[0]); |
| 600 | retval = -1; |
| 601 | goto end; |
| 602 | } |
| 603 | |
| 604 | /* check if a fcgi filter was already registered with this name, |
| 605 | * if that's the case, must use it. */ |
| 606 | list_for_each_entry(fconf, &curpx->filter_configs, list) { |
| 607 | if (fconf->id == fcgi_flt_id) { |
| 608 | fcgi_conf = fconf->conf; |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 609 | if (fcgi_conf && strcmp((char *)fcgi_conf->name, args[1]) == 0) |
Christopher Faulet | 78fbb9f | 2019-08-11 23:11:03 +0200 | [diff] [blame] | 610 | goto end; |
| 611 | memprintf(err, "'%s' : only one fcgi-app supported per backend", args[0]); |
| 612 | retval = -1; |
| 613 | goto end; |
| 614 | } |
| 615 | } |
| 616 | |
| 617 | /* Create the FCGI filter config */ |
| 618 | fcgi_conf = calloc(1, sizeof(*fcgi_conf)); |
| 619 | if (!fcgi_conf) |
| 620 | goto err; |
| 621 | fcgi_conf->name = strdup(args[1]); |
| 622 | LIST_INIT(&fcgi_conf->param_rules); |
| 623 | LIST_INIT(&fcgi_conf->hdr_rules); |
Christopher Faulet | 78fbb9f | 2019-08-11 23:11:03 +0200 | [diff] [blame] | 624 | |
| 625 | /* Register the filter */ |
| 626 | fconf = calloc(1, sizeof(*fconf)); |
| 627 | if (!fconf) |
| 628 | goto err; |
| 629 | fconf->id = fcgi_flt_id; |
| 630 | fconf->conf = fcgi_conf; |
| 631 | fconf->ops = &fcgi_flt_ops; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 632 | LIST_APPEND(&curpx->filter_configs, &fconf->list); |
Christopher Faulet | 78fbb9f | 2019-08-11 23:11:03 +0200 | [diff] [blame] | 633 | |
| 634 | end: |
| 635 | return retval; |
| 636 | err: |
| 637 | if (fcgi_conf) { |
| 638 | free(fcgi_conf->name); |
| 639 | free(fcgi_conf); |
| 640 | } |
| 641 | memprintf(err, "out of memory"); |
| 642 | retval = -1; |
| 643 | goto end; |
| 644 | } |
| 645 | |
| 646 | /* Finishes the parsing of FCGI application of proxies and servers */ |
| 647 | static int cfg_fcgi_apps_postparser() |
| 648 | { |
| 649 | struct fcgi_app *curapp; |
| 650 | struct proxy *px; |
| 651 | struct server *srv; |
Christopher Faulet | c97406f | 2020-06-22 11:07:18 +0200 | [diff] [blame] | 652 | struct logsrv *logsrv; |
Christopher Faulet | 78fbb9f | 2019-08-11 23:11:03 +0200 | [diff] [blame] | 653 | int err_code = 0; |
| 654 | |
| 655 | for (px = proxies_list; px; px = px->next) { |
| 656 | struct fcgi_flt_conf *fcgi_conf = find_px_fcgi_conf(px); |
| 657 | int nb_fcgi_srv = 0; |
| 658 | |
| 659 | if (px->mode == PR_MODE_TCP && fcgi_conf) { |
| 660 | ha_alert("config : proxy '%s': FCGI application cannot be used in non-HTTP mode.\n", |
| 661 | px->id); |
| 662 | err_code |= ERR_ALERT | ERR_FATAL; |
| 663 | goto end; |
| 664 | } |
| 665 | |
Christopher Faulet | 7950715 | 2022-05-16 11:43:10 +0200 | [diff] [blame] | 666 | /* By default, for FCGI-ready backend, HTTP request header names |
| 667 | * are restricted and the "delete" policy is set |
| 668 | */ |
| 669 | if (fcgi_conf && !(px->options2 & PR_O2_RSTRICT_REQ_HDR_NAMES_MASK)) |
| 670 | px->options2 |= PR_O2_RSTRICT_REQ_HDR_NAMES_DEL; |
| 671 | |
Christopher Faulet | 78fbb9f | 2019-08-11 23:11:03 +0200 | [diff] [blame] | 672 | for (srv = px->srv; srv; srv = srv->next) { |
| 673 | if (srv->mux_proto && isteq(srv->mux_proto->token, ist("fcgi"))) { |
| 674 | nb_fcgi_srv++; |
| 675 | if (fcgi_conf) |
| 676 | continue; |
| 677 | ha_alert("config : proxy '%s': FCGI server '%s' has no FCGI app configured.\n", |
| 678 | px->id, srv->id); |
| 679 | err_code |= ERR_ALERT | ERR_FATAL; |
| 680 | goto end; |
| 681 | } |
| 682 | } |
| 683 | if (fcgi_conf && !nb_fcgi_srv) { |
| 684 | ha_alert("config : proxy '%s': FCGI app configured but no FCGI server found.\n", |
| 685 | px->id); |
| 686 | err_code |= ERR_ALERT | ERR_FATAL; |
| 687 | goto end; |
| 688 | } |
| 689 | } |
| 690 | |
| 691 | for (curapp = fcgi_apps; curapp != NULL; curapp = curapp->next) { |
| 692 | if (!istlen(curapp->docroot)) { |
| 693 | ha_alert("config : fcgi-app '%s': no docroot configured.\n", |
| 694 | curapp->name); |
| 695 | err_code |= ERR_ALERT | ERR_FATAL; |
| 696 | goto end; |
| 697 | } |
| 698 | if (!(curapp->flags & (FCGI_APP_FL_MPXS_CONNS|FCGI_APP_FL_GET_VALUES))) { |
| 699 | if (curapp->maxreqs > 1) { |
| 700 | ha_warning("config : fcgi-app '%s': multiplexing not supported, " |
| 701 | "ignore the option 'max-reqs'.\n", |
| 702 | curapp->name); |
| 703 | err_code |= ERR_WARN; |
| 704 | } |
| 705 | curapp->maxreqs = 1; |
| 706 | } |
Christopher Faulet | c97406f | 2020-06-22 11:07:18 +0200 | [diff] [blame] | 707 | |
| 708 | list_for_each_entry(logsrv, &curapp->logsrvs, list) { |
| 709 | if (logsrv->type == LOG_TARGET_BUFFER) { |
| 710 | struct sink *sink = sink_find(logsrv->ring_name); |
| 711 | |
| 712 | if (!sink || sink->type != SINK_TYPE_BUFFER) { |
Ilya Shipitsin | 46a030c | 2020-07-05 16:36:08 +0500 | [diff] [blame] | 713 | ha_alert("config : fcgi-app '%s' : log server uses unknown ring named '%s'.\n", |
Christopher Faulet | c97406f | 2020-06-22 11:07:18 +0200 | [diff] [blame] | 714 | curapp->name, logsrv->ring_name); |
| 715 | err_code |= ERR_ALERT | ERR_FATAL; |
| 716 | } |
| 717 | logsrv->sink = sink; |
| 718 | } |
| 719 | } |
Christopher Faulet | 78fbb9f | 2019-08-11 23:11:03 +0200 | [diff] [blame] | 720 | } |
| 721 | |
| 722 | end: |
| 723 | return err_code; |
| 724 | } |
| 725 | |
| 726 | static int fcgi_app_add_rule(struct fcgi_app *curapp, enum fcgi_rule_type type, char *name, char *value, |
| 727 | struct acl_cond *cond, char **err) |
| 728 | { |
| 729 | struct fcgi_rule_conf *rule; |
| 730 | |
| 731 | /* Param not found, add a new one */ |
| 732 | rule = calloc(1, sizeof(*rule)); |
| 733 | if (!rule) |
| 734 | goto err; |
| 735 | LIST_INIT(&rule->list); |
| 736 | rule->type = type; |
| 737 | if (type == FCGI_RULE_SET_PARAM || type == FCGI_RULE_UNSET_PARAM) { |
| 738 | struct ist fname = fcgi_param_name(trash.area, ist(name)); |
| 739 | rule->name = my_strndup(fname.ptr, fname.len); |
| 740 | } |
Christopher Faulet | bc96c90 | 2019-12-02 10:33:31 +0100 | [diff] [blame] | 741 | else { /* FCGI_RULE_PASS_HDR/FCGI_RULE_HIDE_HDR */ |
| 742 | struct ist fname = ist2bin_lc(trash.area, ist(name)); |
| 743 | rule->name = my_strndup(fname.ptr, fname.len); |
| 744 | } |
Christopher Faulet | 78fbb9f | 2019-08-11 23:11:03 +0200 | [diff] [blame] | 745 | if (!rule->name) |
| 746 | goto err; |
| 747 | |
| 748 | if (value) { |
| 749 | rule->value = strdup(value); |
| 750 | if (!rule->value) |
| 751 | goto err; |
| 752 | } |
| 753 | rule->cond = cond; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 754 | LIST_APPEND(&curapp->conf.rules, &rule->list); |
Christopher Faulet | 78fbb9f | 2019-08-11 23:11:03 +0200 | [diff] [blame] | 755 | return 1; |
| 756 | |
| 757 | err: |
| 758 | if (rule) { |
| 759 | free(rule->name); |
| 760 | free(rule->value); |
| 761 | free(rule); |
| 762 | } |
| 763 | if (cond) { |
| 764 | prune_acl_cond(cond); |
| 765 | free(cond); |
| 766 | } |
| 767 | memprintf(err, "out of memory"); |
| 768 | return 0; |
| 769 | } |
| 770 | |
| 771 | /* Parses "fcgi-app" section */ |
| 772 | static int cfg_parse_fcgi_app(const char *file, int linenum, char **args, int kwm) |
| 773 | { |
| 774 | static struct fcgi_app *curapp = NULL; |
| 775 | struct acl_cond *cond = NULL; |
| 776 | char *name, *value = NULL; |
| 777 | enum fcgi_rule_type type; |
| 778 | int err_code = 0; |
| 779 | const char *err; |
| 780 | char *errmsg = NULL; |
| 781 | |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 782 | if (strcmp(args[0], "fcgi-app") == 0) { /* new fcgi-app */ |
Christopher Faulet | 78fbb9f | 2019-08-11 23:11:03 +0200 | [diff] [blame] | 783 | if (!*(args[1])) { |
| 784 | ha_alert("parsing [%s:%d]: '%s' expects <name> as argument.\n", |
| 785 | file, linenum, args[0]); |
| 786 | err_code |= ERR_ALERT | ERR_FATAL; |
| 787 | goto out; |
| 788 | } |
| 789 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) |
| 790 | goto out; |
| 791 | |
| 792 | err = invalid_char(args[1]); |
| 793 | if (err) { |
| 794 | ha_alert("parsing [%s:%d]: character '%c' is not permitted in '%s' name '%s'.\n", |
| 795 | file, linenum, *err, args[0], args[1]); |
| 796 | err_code |= ERR_ALERT | ERR_FATAL; |
| 797 | goto out; |
| 798 | } |
| 799 | |
| 800 | for (curapp = fcgi_apps; curapp != NULL; curapp = curapp->next) { |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 801 | if (strcmp(curapp->name, args[1]) == 0) { |
Christopher Faulet | 78fbb9f | 2019-08-11 23:11:03 +0200 | [diff] [blame] | 802 | ha_alert("Parsing [%s:%d]: fcgi-app section '%s' has the same name as another one declared at %s:%d.\n", |
| 803 | file, linenum, args[1], curapp->conf.file, curapp->conf.line); |
| 804 | err_code |= ERR_ALERT | ERR_FATAL; |
| 805 | } |
| 806 | } |
| 807 | |
| 808 | curapp = calloc(1, sizeof(*curapp)); |
| 809 | if (!curapp) { |
| 810 | ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
| 811 | err_code |= ERR_ALERT | ERR_ABORT; |
| 812 | goto out; |
| 813 | } |
| 814 | |
| 815 | curapp->next = fcgi_apps; |
| 816 | fcgi_apps = curapp; |
| 817 | curapp->flags = FCGI_APP_FL_KEEP_CONN; |
| 818 | curapp->docroot = ist(NULL); |
| 819 | curapp->index = ist(NULL); |
| 820 | curapp->pathinfo_re = NULL; |
| 821 | curapp->name = strdup(args[1]); |
| 822 | curapp->maxreqs = 1; |
| 823 | curapp->conf.file = strdup(file); |
| 824 | curapp->conf.line = linenum; |
| 825 | LIST_INIT(&curapp->acls); |
| 826 | LIST_INIT(&curapp->logsrvs); |
| 827 | LIST_INIT(&curapp->conf.args.list); |
| 828 | LIST_INIT(&curapp->conf.rules); |
| 829 | |
| 830 | /* Set info about authentication */ |
| 831 | if (!fcgi_app_add_rule(curapp, FCGI_RULE_SET_PARAM, "REMOTE_USER", "%[http_auth_user]", NULL, &errmsg) || |
| 832 | !fcgi_app_add_rule(curapp, FCGI_RULE_SET_PARAM, "AUTH_TYPE", "%[http_auth_type]", NULL, &errmsg)) { |
| 833 | ha_alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum, |
| 834 | args[1], errmsg); |
| 835 | err_code |= ERR_ALERT | ERR_FATAL; |
| 836 | } |
| 837 | |
| 838 | /* Hide hop-by-hop headers by default */ |
| 839 | if (!fcgi_app_add_rule(curapp, FCGI_RULE_HIDE_HDR, "connection", NULL, NULL, &errmsg) || |
| 840 | !fcgi_app_add_rule(curapp, FCGI_RULE_HIDE_HDR, "keep-alive", NULL, NULL, &errmsg) || |
| 841 | !fcgi_app_add_rule(curapp, FCGI_RULE_HIDE_HDR, "authorization", NULL, NULL, &errmsg) || |
| 842 | !fcgi_app_add_rule(curapp, FCGI_RULE_HIDE_HDR, "proxy", NULL, NULL, &errmsg) || |
| 843 | !fcgi_app_add_rule(curapp, FCGI_RULE_HIDE_HDR, "proxy-authorization", NULL, NULL, &errmsg) || |
| 844 | !fcgi_app_add_rule(curapp, FCGI_RULE_HIDE_HDR, "proxy-authenticate", NULL, NULL, &errmsg) || |
| 845 | !fcgi_app_add_rule(curapp, FCGI_RULE_HIDE_HDR, "te", NULL, NULL, &errmsg) || |
| 846 | !fcgi_app_add_rule(curapp, FCGI_RULE_HIDE_HDR, "trailers", NULL, NULL, &errmsg) || |
| 847 | !fcgi_app_add_rule(curapp, FCGI_RULE_HIDE_HDR, "transfer-encoding", NULL, NULL, &errmsg) || |
| 848 | !fcgi_app_add_rule(curapp, FCGI_RULE_HIDE_HDR, "upgrade", NULL, NULL, &errmsg)) { |
| 849 | ha_alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum, |
| 850 | args[1], errmsg); |
| 851 | err_code |= ERR_ALERT | ERR_FATAL; |
| 852 | } |
| 853 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 854 | else if (strcmp(args[0], "docroot") == 0) { |
Christopher Faulet | 78fbb9f | 2019-08-11 23:11:03 +0200 | [diff] [blame] | 855 | if (!*(args[1])) { |
| 856 | ha_alert("parsing [%s:%d] : '%s' expects <path> as argument.\n", |
| 857 | file, linenum, args[0]); |
| 858 | err_code |= ERR_ALERT | ERR_FATAL; |
| 859 | goto out; |
| 860 | } |
| 861 | if (alertif_too_many_args_idx(0, 1, file, linenum, args, &err_code)) |
| 862 | goto out; |
Tim Duesterhus | ed52637 | 2020-03-05 17:56:33 +0100 | [diff] [blame] | 863 | istfree(&curapp->docroot); |
Tim Duesterhus | dcf753a | 2021-03-04 17:31:47 +0100 | [diff] [blame] | 864 | curapp->docroot = ist(strdup(args[1])); |
Tim Duesterhus | ed52637 | 2020-03-05 17:56:33 +0100 | [diff] [blame] | 865 | if (!isttest(curapp->docroot)) { |
Christopher Faulet | 78fbb9f | 2019-08-11 23:11:03 +0200 | [diff] [blame] | 866 | ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
| 867 | err_code |= ERR_ALERT | ERR_ABORT; |
| 868 | } |
| 869 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 870 | else if (strcmp(args[0], "path-info") == 0) { |
Christopher Faulet | 78fbb9f | 2019-08-11 23:11:03 +0200 | [diff] [blame] | 871 | if (!*(args[1])) { |
| 872 | ha_alert("parsing [%s:%d] : '%s' expects <regex> as argument.\n", |
| 873 | file, linenum, args[0]); |
| 874 | err_code |= ERR_ALERT | ERR_FATAL; |
| 875 | goto out; |
| 876 | } |
| 877 | if (alertif_too_many_args_idx(0, 1, file, linenum, args, &err_code)) |
| 878 | goto out; |
| 879 | regex_free(curapp->pathinfo_re); |
| 880 | curapp->pathinfo_re = regex_comp(args[1], 1, 1, &errmsg); |
| 881 | if (!curapp->pathinfo_re) { |
| 882 | ha_alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum, |
| 883 | args[1], errmsg); |
| 884 | err_code |= ERR_ALERT | ERR_FATAL; |
| 885 | } |
| 886 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 887 | else if (strcmp(args[0], "index") == 0) { |
Christopher Faulet | 78fbb9f | 2019-08-11 23:11:03 +0200 | [diff] [blame] | 888 | if (!*(args[1])) { |
| 889 | ha_alert("parsing [%s:%d] : '%s' expects <filename> as argument.\n", |
| 890 | file, linenum, args[0]); |
| 891 | err_code |= ERR_ALERT | ERR_FATAL; |
| 892 | goto out; |
| 893 | } |
| 894 | if (alertif_too_many_args_idx(0, 1, file, linenum, args, &err_code)) |
| 895 | goto out; |
Tim Duesterhus | ed52637 | 2020-03-05 17:56:33 +0100 | [diff] [blame] | 896 | istfree(&curapp->index); |
Tim Duesterhus | dcf753a | 2021-03-04 17:31:47 +0100 | [diff] [blame] | 897 | curapp->index = ist(strdup(args[1])); |
Tim Duesterhus | ed52637 | 2020-03-05 17:56:33 +0100 | [diff] [blame] | 898 | if (!isttest(curapp->index)) { |
Christopher Faulet | 78fbb9f | 2019-08-11 23:11:03 +0200 | [diff] [blame] | 899 | ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
| 900 | err_code |= ERR_ALERT | ERR_ABORT; |
| 901 | } |
| 902 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 903 | else if (strcmp(args[0], "acl") == 0) { |
Christopher Faulet | 78fbb9f | 2019-08-11 23:11:03 +0200 | [diff] [blame] | 904 | const char *err; |
| 905 | err = invalid_char(args[1]); |
| 906 | if (err) { |
| 907 | ha_alert("parsing [%s:%d] : character '%c' is not permitted in acl name '%s'.\n", |
| 908 | file, linenum, *err, args[1]); |
| 909 | err_code |= ERR_ALERT | ERR_FATAL; |
Tim Duesterhus | 0cf811a | 2020-02-05 21:00:50 +0100 | [diff] [blame] | 910 | goto out; |
Christopher Faulet | 78fbb9f | 2019-08-11 23:11:03 +0200 | [diff] [blame] | 911 | } |
Tim Duesterhus | 0cf811a | 2020-02-05 21:00:50 +0100 | [diff] [blame] | 912 | if (strcasecmp(args[1], "or") == 0) { |
Tim Duesterhus | f1bc24c | 2020-02-06 22:04:03 +0100 | [diff] [blame] | 913 | ha_alert("parsing [%s:%d] : acl name '%s' will never match. 'or' is used to express a " |
Tim Duesterhus | 0cf811a | 2020-02-05 21:00:50 +0100 | [diff] [blame] | 914 | "logical disjunction within a condition.\n", |
| 915 | file, linenum, args[1]); |
| 916 | err_code |= ERR_ALERT | ERR_FATAL; |
| 917 | goto out; |
| 918 | } |
| 919 | if (parse_acl((const char **)args+1, &curapp->acls, &errmsg, &curapp->conf.args, file, linenum) == NULL) { |
Christopher Faulet | 78fbb9f | 2019-08-11 23:11:03 +0200 | [diff] [blame] | 920 | ha_alert("parsing [%s:%d] : error detected while parsing ACL '%s' : %s.\n", |
| 921 | file, linenum, args[1], errmsg); |
| 922 | err_code |= ERR_ALERT | ERR_FATAL; |
Tim Duesterhus | 0cf811a | 2020-02-05 21:00:50 +0100 | [diff] [blame] | 923 | goto out; |
Christopher Faulet | 78fbb9f | 2019-08-11 23:11:03 +0200 | [diff] [blame] | 924 | } |
| 925 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 926 | else if (strcmp(args[0], "set-param") == 0) { |
Christopher Faulet | 78fbb9f | 2019-08-11 23:11:03 +0200 | [diff] [blame] | 927 | if (!*(args[1]) || !*(args[2])) { |
| 928 | ha_alert("parsing [%s:%d] : '%s' expects <name> and <value> as arguments.\n", |
| 929 | file, linenum, args[0]); |
| 930 | err_code |= ERR_ALERT | ERR_FATAL; |
| 931 | goto out; |
| 932 | } |
| 933 | type = FCGI_RULE_SET_PARAM; |
| 934 | name = args[1]; |
| 935 | value = args[2]; |
| 936 | cond = NULL; |
| 937 | args += 3; |
| 938 | |
| 939 | parse_cond_rule: |
| 940 | if (!*(args[0])) /* No condition */ |
| 941 | goto add_rule; |
| 942 | |
| 943 | if (strcmp(args[0], "if") == 0) |
| 944 | cond = parse_acl_cond((const char **)args+1, &curapp->acls, ACL_COND_IF, &errmsg, &curapp->conf.args, |
| 945 | file, linenum); |
| 946 | else if (strcmp(args[0], "unless") == 0) |
| 947 | cond = parse_acl_cond((const char **)args+1, &curapp->acls, ACL_COND_UNLESS, &errmsg, &curapp->conf.args, |
| 948 | file, linenum); |
| 949 | if (!cond) { |
| 950 | ha_alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum, |
| 951 | name, errmsg); |
| 952 | err_code |= ERR_ALERT | ERR_FATAL; |
| 953 | } |
| 954 | add_rule: |
| 955 | if (!fcgi_app_add_rule(curapp, type, name, value, cond, &errmsg)) { |
| 956 | ha_alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum, |
| 957 | name, errmsg); |
| 958 | err_code |= ERR_ALERT | ERR_FATAL; |
| 959 | } |
| 960 | } |
| 961 | #if 0 /* Disabled for now */ |
| 962 | else if (!strcmp(args[0], "unset-param")) { |
| 963 | if (!*(args[1])) { |
| 964 | ha_alert("parsing [%s:%d] : '%s' expects <name> as arguments.\n", |
| 965 | file, linenum, args[0]); |
| 966 | err_code |= ERR_ALERT | ERR_FATAL; |
| 967 | goto out; |
| 968 | } |
| 969 | type = FCGI_RULE_UNSET_PARAM; |
| 970 | name = args[1]; |
| 971 | value = NULL; |
| 972 | cond = NULL; |
| 973 | args += 2; |
| 974 | goto parse_cond_rule; |
| 975 | } |
| 976 | #endif |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 977 | else if (strcmp(args[0], "pass-header") == 0) { |
Christopher Faulet | 78fbb9f | 2019-08-11 23:11:03 +0200 | [diff] [blame] | 978 | if (!*(args[1])) { |
| 979 | ha_alert("parsing [%s:%d] : '%s' expects <name> as arguments.\n", |
| 980 | file, linenum, args[0]); |
| 981 | err_code |= ERR_ALERT | ERR_FATAL; |
| 982 | goto out; |
| 983 | } |
| 984 | type = FCGI_RULE_PASS_HDR; |
| 985 | name = args[1]; |
| 986 | value = NULL; |
| 987 | cond = NULL; |
| 988 | args += 2; |
| 989 | goto parse_cond_rule; |
| 990 | } |
| 991 | #if 0 /* Disabled for now */ |
| 992 | else if (!strcmp(args[0], "hide-header")) { |
| 993 | if (!*(args[1])) { |
| 994 | ha_alert("parsing [%s:%d] : '%s' expects <name> as arguments.\n", |
| 995 | file, linenum, args[0]); |
| 996 | err_code |= ERR_ALERT | ERR_FATAL; |
| 997 | goto out; |
| 998 | } |
| 999 | type = FCGI_RULE_HIDE_HDR; |
| 1000 | name = args[1]; |
| 1001 | value = NULL; |
| 1002 | cond = NULL; |
| 1003 | args += 2; |
| 1004 | goto parse_cond_rule; |
| 1005 | } |
| 1006 | #endif |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 1007 | else if (strcmp(args[0], "option") == 0) { |
Christopher Faulet | 78fbb9f | 2019-08-11 23:11:03 +0200 | [diff] [blame] | 1008 | if (!*(args[1])) { |
| 1009 | ha_alert("parsing [%s:%d]: '%s' expects an option name.\n", |
| 1010 | file, linenum, args[0]); |
| 1011 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1012 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 1013 | else if (strcmp(args[1], "keep-conn") == 0) { |
Christopher Faulet | 78fbb9f | 2019-08-11 23:11:03 +0200 | [diff] [blame] | 1014 | if (alertif_too_many_args_idx(0, 1, file, linenum, args, &err_code)) |
| 1015 | goto out; |
| 1016 | if (kwm == KWM_STD) |
| 1017 | curapp->flags |= FCGI_APP_FL_KEEP_CONN; |
| 1018 | else if (kwm == KWM_NO) |
| 1019 | curapp->flags &= ~FCGI_APP_FL_KEEP_CONN; |
| 1020 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 1021 | else if (strcmp(args[1], "get-values") == 0) { |
Christopher Faulet | 78fbb9f | 2019-08-11 23:11:03 +0200 | [diff] [blame] | 1022 | if (alertif_too_many_args_idx(0, 1, file, linenum, args, &err_code)) |
| 1023 | goto out; |
| 1024 | if (kwm == KWM_STD) |
| 1025 | curapp->flags |= FCGI_APP_FL_GET_VALUES; |
| 1026 | else if (kwm == KWM_NO) |
| 1027 | curapp->flags &= ~FCGI_APP_FL_GET_VALUES; |
| 1028 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 1029 | else if (strcmp(args[1], "mpxs-conns") == 0) { |
Christopher Faulet | 78fbb9f | 2019-08-11 23:11:03 +0200 | [diff] [blame] | 1030 | if (alertif_too_many_args_idx(0, 1, file, linenum, args, &err_code)) |
| 1031 | goto out; |
| 1032 | if (kwm == KWM_STD) |
| 1033 | curapp->flags |= FCGI_APP_FL_MPXS_CONNS; |
| 1034 | else if (kwm == KWM_NO) |
| 1035 | curapp->flags &= ~FCGI_APP_FL_MPXS_CONNS; |
| 1036 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 1037 | else if (strcmp(args[1], "max-reqs") == 0) { |
Christopher Faulet | 78fbb9f | 2019-08-11 23:11:03 +0200 | [diff] [blame] | 1038 | if (kwm != KWM_STD) { |
| 1039 | ha_alert("parsing [%s:%d]: negation/default is not supported for option '%s'.\n", |
| 1040 | file, linenum, args[1]); |
| 1041 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1042 | goto out; |
| 1043 | } |
| 1044 | if (!*(args[2])) { |
| 1045 | ha_alert("parsing [%s:%d]: option '%s' expects an integer argument.\n", |
| 1046 | file, linenum, args[1]); |
| 1047 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1048 | goto out; |
| 1049 | } |
| 1050 | if (alertif_too_many_args_idx(1, 1, file, linenum, args, &err_code)) |
| 1051 | goto out; |
| 1052 | |
| 1053 | curapp->maxreqs = atol(args[2]); |
| 1054 | if (!curapp->maxreqs) { |
| 1055 | ha_alert("parsing [%s:%d]: option '%s' expects a strictly positive integer argument.\n", |
| 1056 | file, linenum, args[1]); |
| 1057 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1058 | goto out; |
| 1059 | } |
| 1060 | } |
| 1061 | else { |
| 1062 | ha_alert("parsing [%s:%d] : unknown option '%s'.\n", file, linenum, args[1]); |
| 1063 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1064 | } |
| 1065 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 1066 | else if (strcmp(args[0], "log-stderr") == 0) { |
Emeric Brun | 9533a70 | 2021-04-02 10:13:43 +0200 | [diff] [blame] | 1067 | if (!parse_logsrv(args, &curapp->logsrvs, (kwm == KWM_NO), file, linenum, &errmsg)) { |
Christopher Faulet | 78fbb9f | 2019-08-11 23:11:03 +0200 | [diff] [blame] | 1068 | ha_alert("parsing [%s:%d] : %s : %s\n", file, linenum, args[0], errmsg); |
| 1069 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1070 | } |
| 1071 | } |
| 1072 | else { |
| 1073 | ha_alert("parsing [%s:%d]: unknown keyword '%s' in '%s' section\n", file, linenum, args[0], "fcgi-app"); |
| 1074 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1075 | } |
| 1076 | |
| 1077 | out: |
| 1078 | free(errmsg); |
| 1079 | return err_code; |
| 1080 | } |
| 1081 | |
| 1082 | |
| 1083 | /**************************************************************************/ |
| 1084 | /*********************** FCGI Deinit functions ****************************/ |
| 1085 | /**************************************************************************/ |
| 1086 | void fcgi_apps_deinit() |
| 1087 | { |
| 1088 | struct fcgi_app *curapp, *nextapp; |
| 1089 | struct logsrv *log, *logb; |
| 1090 | |
| 1091 | for (curapp = fcgi_apps; curapp != NULL; curapp = nextapp) { |
| 1092 | struct fcgi_rule_conf *rule, *back; |
| 1093 | |
| 1094 | free(curapp->name); |
Tim Duesterhus | ed52637 | 2020-03-05 17:56:33 +0100 | [diff] [blame] | 1095 | istfree(&curapp->docroot); |
| 1096 | istfree(&curapp->index); |
Christopher Faulet | 78fbb9f | 2019-08-11 23:11:03 +0200 | [diff] [blame] | 1097 | regex_free(curapp->pathinfo_re); |
| 1098 | free(curapp->conf.file); |
| 1099 | |
| 1100 | list_for_each_entry_safe(log, logb, &curapp->logsrvs, list) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1101 | LIST_DELETE(&log->list); |
Christopher Faulet | 78fbb9f | 2019-08-11 23:11:03 +0200 | [diff] [blame] | 1102 | free(log); |
| 1103 | } |
| 1104 | |
| 1105 | list_for_each_entry_safe(rule, back, &curapp->conf.rules, list) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1106 | LIST_DELETE(&rule->list); |
Christopher Faulet | 78fbb9f | 2019-08-11 23:11:03 +0200 | [diff] [blame] | 1107 | fcgi_release_rule_conf(rule); |
| 1108 | } |
| 1109 | |
| 1110 | nextapp = curapp->next; |
| 1111 | free(curapp); |
| 1112 | } |
| 1113 | } |
| 1114 | |
| 1115 | |
| 1116 | /**************************************************************************/ |
| 1117 | /*************** Keywords definition and registration *********************/ |
| 1118 | /**************************************************************************/ |
| 1119 | static struct cfg_kw_list cfg_kws = {ILH, { |
| 1120 | { CFG_LISTEN, "use-fcgi-app", proxy_parse_use_fcgi_app }, |
| 1121 | { 0, NULL, NULL }, |
| 1122 | }}; |
| 1123 | |
| 1124 | // FIXME: Add rep.fcgi smp_fetch |
| 1125 | static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, { |
| 1126 | { "fcgi.docroot", smp_fetch_fcgi_docroot, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Willy Tarreau | fc41e25 | 2019-09-27 22:45:17 +0200 | [diff] [blame] | 1127 | { "fcgi.index", smp_fetch_fcgi_index, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 1128 | { /* END */ } |
Christopher Faulet | 78fbb9f | 2019-08-11 23:11:03 +0200 | [diff] [blame] | 1129 | }}; |
| 1130 | |
| 1131 | /* Declare the filter parser for "fcgi-app" keyword */ |
| 1132 | static struct flt_kw_list filter_kws = { "FCGI", { }, { |
| 1133 | { "fcgi-app", parse_fcgi_flt, NULL }, |
| 1134 | { NULL, NULL, NULL }, |
| 1135 | } |
| 1136 | }; |
| 1137 | |
| 1138 | INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords); |
| 1139 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |
| 1140 | INITCALL1(STG_REGISTER, flt_register_keywords, &filter_kws); |
| 1141 | |
| 1142 | INITCALL1(STG_REGISTER, hap_register_post_deinit, fcgi_apps_deinit); |
| 1143 | |
| 1144 | REGISTER_CONFIG_SECTION("fcgi-app", cfg_parse_fcgi_app, NULL); |
| 1145 | REGISTER_CONFIG_POSTPARSER("fcgi-apps", cfg_fcgi_apps_postparser); |
| 1146 | |
| 1147 | /* |
| 1148 | * Local variables: |
| 1149 | * c-indent-level: 8 |
| 1150 | * c-basic-offset: 8 |
| 1151 | * End: |
| 1152 | */ |