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