blob: e0f3232f9ccd123e63ea0686910cda0085ef1eb9 [file] [log] [blame]
Christopher Faulet78fbb9f2019-08-11 23:11:03 +02001/*
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 Tarreaudcc048a2020-06-04 19:11:43 +020013#include <haproxy/acl.h>
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020014#include <haproxy/api.h>
Willy Tarreau6be78492020-06-05 00:00:29 +020015#include <haproxy/cfgparse.h>
Willy Tarreauc13ed532020-06-02 10:22:45 +020016#include <haproxy/chunk.h>
Willy Tarreau8d366972020-05-27 16:10:29 +020017#include <haproxy/errors.h>
Willy Tarreauc6599682020-06-04 21:33:21 +020018#include <haproxy/fcgi-app.h>
Willy Tarreauc7babd82020-06-04 21:29:29 +020019#include <haproxy/filters.h>
Willy Tarreau126ba3a2020-06-04 18:26:43 +020020#include <haproxy/http_fetch.h>
Willy Tarreau87735332020-06-04 09:08:41 +020021#include <haproxy/http_htx.h>
Willy Tarreauaeed4a82020-06-04 22:01:04 +020022#include <haproxy/log.h>
Willy Tarreau03f839d2021-05-08 20:23:18 +020023#include <haproxy/proxy.h>
Willy Tarreau7cd8b6e2020-06-02 17:32:26 +020024#include <haproxy/regex.h>
Willy Tarreaue6ce10b2020-06-04 15:33:47 +020025#include <haproxy/sample.h>
Willy Tarreau1e56f922020-06-04 23:20:13 +020026#include <haproxy/server-t.h>
Willy Tarreau48d25b32020-06-04 18:58:52 +020027#include <haproxy/session.h>
Christopher Fauletc97406f2020-06-22 11:07:18 +020028#include <haproxy/sink.h>
Willy Tarreau48fbcae2020-06-03 18:09:46 +020029#include <haproxy/tools.h>
Christopher Faulet78fbb9f2019-08-11 23:11:03 +020030
Christopher Faulet78fbb9f2019-08-11 23:11:03 +020031
Christopher Faulet78fbb9f2019-08-11 23:11:03 +020032/* Global list of all FCGI applications */
33static struct fcgi_app *fcgi_apps = NULL;
34
35struct flt_ops fcgi_flt_ops;
36const char *fcgi_flt_id = "FCGI filter";
37
38DECLARE_STATIC_POOL(pool_head_fcgi_flt_ctx, "fcgi_flt_ctx", sizeof(struct fcgi_flt_ctx));
39DECLARE_STATIC_POOL(pool_head_fcgi_param_rule, "fcgi_param_rule", sizeof(struct fcgi_param_rule));
40DECLARE_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 Shipitsin01881082021-08-07 14:41:56 +050047 * ('_'). The result is copied into <dst>. the corresponding ist is returned.
Christopher Faulet78fbb9f2019-08-11 23:11:03 +020048 */
49static 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 Tarreau90807112020-02-25 08:16:33 +010056 if (isalnum((unsigned char)name.ptr[ofs2]))
Christopher Faulet78fbb9f2019-08-11 23:11:03 +020057 dst[ofs1++] = ist_lc[(unsigned char)name.ptr[ofs2]];
58 else
59 dst[ofs1++] = '_';
60 }
61 return ist2(dst, ofs1);
62}
63
Ilya Shipitsin47d17182020-06-21 21:42:57 +050064/* Returns a pointer to the FCGi application matching the name <name>. NULL is
Christopher Faulet78fbb9f2019-08-11 23:11:03 +020065 * returned if no match found.
66 */
67struct 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 Duesterhuse5ff1412021-01-02 22:31:53 +010072 if (strcmp(app->name, name) == 0)
Christopher Faulet78fbb9f2019-08-11 23:11:03 +020073 return app;
74 }
75
76 return NULL;
77}
78
79struct 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
90struct 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
104struct 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
113struct 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
122static void fcgi_release_rule_conf(struct fcgi_rule_conf *rule)
123{
124 if (!rule)
125 return;
126 free(rule->name);
127 free(rule->value);
Aurelien DARRAGONc6100952023-05-11 12:29:51 +0200128 free_acl_cond(rule->cond);
Christopher Faulet78fbb9f2019-08-11 23:11:03 +0200129 free(rule);
130}
131
132static void fcgi_release_rule(struct fcgi_rule *rule)
133{
134 if (!rule)
135 return;
136
137 if (!LIST_ISEMPTY(&rule->value)) {
138 struct logformat_node *lf, *lfb;
139
140 list_for_each_entry_safe(lf, lfb, &rule->value, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +0200141 LIST_DELETE(&lf->list);
Christopher Faulet78fbb9f2019-08-11 23:11:03 +0200142 release_sample_expr(lf->expr);
143 free(lf->arg);
144 free(lf);
145 }
146 }
147 /* ->cond and ->name are not owned by the rule */
148 free(rule);
149}
150
151/**************************************************************************/
152/*********************** FCGI Sample fetches ******************************/
153/**************************************************************************/
154
155static int smp_fetch_fcgi_docroot(const struct arg *args, struct sample *smp,
156 const char *kw, void *private)
157{
158 struct fcgi_app *app = get_strm_fcgi_app(smp->strm);
159
160 if (!app)
161 return 0;
162
163 smp->data.type = SMP_T_STR;
164 smp->data.u.str.area = app->docroot.ptr;
165 smp->data.u.str.data = app->docroot.len;
166 smp->flags = SMP_F_CONST;
167 return 1;
168}
169
170static int smp_fetch_fcgi_index(const struct arg *args, struct sample *smp,
171 const char *kw, void *private)
172{
173 struct fcgi_app *app = get_strm_fcgi_app(smp->strm);
174
175 if (!app || !istlen(app->index))
176 return 0;
177
178 smp->data.type = SMP_T_STR;
179 smp->data.u.str.area = app->index.ptr;
180 smp->data.u.str.data = app->index.len;
181 smp->flags = SMP_F_CONST;
182 return 1;
183}
184
185/**************************************************************************/
186/************************** FCGI filter ***********************************/
187/**************************************************************************/
188static int fcgi_flt_init(struct proxy *px, struct flt_conf *fconf)
189{
190 fconf->flags |= FLT_CFG_FL_HTX;
191 return 0;
192}
193
194static void fcgi_flt_deinit(struct proxy *px, struct flt_conf *fconf)
195{
196 struct fcgi_flt_conf *fcgi_conf = fconf->conf;
197 struct fcgi_rule *rule, *back;
198
199 if (!fcgi_conf)
200 return;
201
202 free(fcgi_conf->name);
203
204 list_for_each_entry_safe(rule, back, &fcgi_conf->param_rules, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +0200205 LIST_DELETE(&rule->list);
Christopher Faulet78fbb9f2019-08-11 23:11:03 +0200206 fcgi_release_rule(rule);
207 }
208
209 list_for_each_entry_safe(rule, back, &fcgi_conf->hdr_rules, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +0200210 LIST_DELETE(&rule->list);
Christopher Faulet78fbb9f2019-08-11 23:11:03 +0200211 fcgi_release_rule(rule);
212 }
213
214 free(fcgi_conf);
215}
216
217static int fcgi_flt_check(struct proxy *px, struct flt_conf *fconf)
218{
219 struct fcgi_flt_conf *fcgi_conf = fconf->conf;
220 struct fcgi_rule_conf *crule, *back;
221 struct fcgi_rule *rule = NULL;
222 struct flt_conf *f;
223 char *errmsg = NULL;
224
225 fcgi_conf->app = fcgi_app_find_by_name(fcgi_conf->name);
226 if (!fcgi_conf->app) {
Amaury Denoyelle11124302021-06-04 18:22:08 +0200227 ha_alert("proxy '%s' : fcgi-app '%s' not found.\n",
Christopher Faulet78fbb9f2019-08-11 23:11:03 +0200228 px->id, fcgi_conf->name);
229 goto err;
230 }
231
232 list_for_each_entry(f, &px->filter_configs, list) {
233 if (f->id == http_comp_flt_id || f->id == cache_store_flt_id)
234 continue;
235 else if ((f->id == fconf->id) && f->conf != fcgi_conf) {
Amaury Denoyelle11124302021-06-04 18:22:08 +0200236 ha_alert("proxy '%s' : only one fcgi-app supported per backend.\n",
Christopher Faulet78fbb9f2019-08-11 23:11:03 +0200237 px->id);
238 goto err;
239 }
240 else if (f->id != fconf->id) {
241 /* Implicit declaration is only allowed with the
242 * compression and cache. For other filters, an implicit
243 * declaration is required. */
244 ha_alert("config: proxy '%s': require an explicit filter declaration "
245 "to use the fcgi-app '%s'.\n", px->id, fcgi_conf->name);
246 goto err;
247 }
248 }
249
250 list_for_each_entry_safe(crule, back, &fcgi_conf->app->conf.rules, list) {
251 rule = calloc(1, sizeof(*rule));
252 if (!rule) {
Amaury Denoyelle11124302021-06-04 18:22:08 +0200253 ha_alert("proxy '%s' : out of memory.\n", px->id);
Christopher Faulet78fbb9f2019-08-11 23:11:03 +0200254 goto err;
255 }
256 rule->type = crule->type;
257 rule->name = ist(crule->name);
258 rule->cond = crule->cond;
259 LIST_INIT(&rule->value);
260
261 if (crule->value) {
262 if (!parse_logformat_string(crule->value, px, &rule->value, LOG_OPT_HTTP,
263 SMP_VAL_BE_HRQ_HDR, &errmsg)) {
Amaury Denoyelle11124302021-06-04 18:22:08 +0200264 ha_alert("proxy '%s' : %s.\n", px->id, errmsg);
Christopher Faulet78fbb9f2019-08-11 23:11:03 +0200265 goto err;
266 }
267 }
268
269 if (rule->type == FCGI_RULE_SET_PARAM || rule->type == FCGI_RULE_UNSET_PARAM)
Willy Tarreau2b718102021-04-21 07:32:39 +0200270 LIST_APPEND(&fcgi_conf->param_rules, &rule->list);
Christopher Faulet78fbb9f2019-08-11 23:11:03 +0200271 else /* FCGI_RULE_PASS_HDR/FCGI_RULE_HIDE_HDR */
Willy Tarreau2b718102021-04-21 07:32:39 +0200272 LIST_APPEND(&fcgi_conf->hdr_rules, &rule->list);
Christopher Faulet78fbb9f2019-08-11 23:11:03 +0200273 rule = NULL;
274 }
275 return 0;
276
277 err:
278 free(errmsg);
279 free(rule);
280 return 1;
281}
282
283static int fcgi_flt_start(struct stream *s, struct filter *filter)
284{
285 struct fcgi_flt_conf *fcgi_conf = FLT_CONF(filter);
286 struct fcgi_flt_ctx *fcgi_ctx;
287
Willy Tarreau18f43d82021-03-22 15:07:37 +0100288 fcgi_ctx = pool_alloc(pool_head_fcgi_flt_ctx);
Christopher Faulet78fbb9f2019-08-11 23:11:03 +0200289 if (fcgi_ctx == NULL) {
290 // FIXME: send a warning
291 return 0;
292 }
293 fcgi_ctx->filter = filter;
294 fcgi_ctx->app = fcgi_conf->app;
295 filter->ctx = fcgi_ctx;
296
297 s->req.analysers |= AN_REQ_HTTP_BODY;
298 return 1;
299}
300
301static void fcgi_flt_stop(struct stream *s, struct filter *filter)
302{
303 struct flt_fcgi_ctx *fcgi_ctx = filter->ctx;
304
305 if (!fcgi_ctx)
306 return;
307 pool_free(pool_head_fcgi_flt_ctx, fcgi_ctx);
308 filter->ctx = NULL;
309}
310
311static int fcgi_flt_http_headers(struct stream *s, struct filter *filter, struct http_msg *msg)
312{
313 struct session *sess = strm_sess(s);
314 struct buffer *value;
315 struct fcgi_flt_conf *fcgi_conf = FLT_CONF(filter);
316 struct fcgi_rule *rule;
317 struct fcgi_param_rule *param_rule;
318 struct fcgi_hdr_rule *hdr_rule;
319 struct ebpt_node *node, *next;
320 struct eb_root param_rules = EB_ROOT;
321 struct eb_root hdr_rules = EB_ROOT;
322 struct htx *htx;
323 struct http_hdr_ctx ctx;
324 int ret;
325
326 htx = htxbuf(&msg->chn->buf);
327
328 if (msg->chn->flags & CF_ISRESP) {
329 struct htx_sl *sl;
330
331 /* Remove the header "Status:" from the response */
332 ctx.blk = NULL;
333 while (http_find_header(htx, ist("status"), &ctx, 1))
334 http_remove_header(htx, &ctx);
335
336 /* Add the header "Date:" if not found */
337 ctx.blk = NULL;
338 if (!http_find_header(htx, ist("date"), &ctx, 1)) {
339 struct tm tm;
340
341 get_gmtime(date.tv_sec, &tm);
342 trash.data = strftime(trash.area, trash.size, "%a, %d %b %Y %T %Z", &tm);
343 if (trash.data)
344 http_add_header(htx, ist("date"), ist2(trash.area, trash.data));
345 }
346
347 /* Add the header "Content-Length:" if possible */
348 sl = http_get_stline(htx);
Christopher Fauletd0579602022-04-06 15:29:34 +0200349 if (s->txn->meth != HTTP_METH_HEAD && sl &&
Christopher Faulet32af9a72022-04-15 15:26:24 +0200350 (msg->flags & (HTTP_MSGF_XFER_LEN|HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK)) == HTTP_MSGF_XFER_LEN &&
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100351 (htx->flags & HTX_FL_EOM)) {
Christopher Faulet78fbb9f2019-08-11 23:11:03 +0200352 struct htx_blk * blk;
353 char *end;
354 size_t len = 0;
355
356 for (blk = htx_get_first_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) {
357 enum htx_blk_type type = htx_get_blk_type(blk);
358
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100359 if (type == HTX_BLK_TLR || type == HTX_BLK_EOT)
Christopher Faulet78fbb9f2019-08-11 23:11:03 +0200360 break;
361 if (type == HTX_BLK_DATA)
362 len += htx_get_blksz(blk);
363 }
364 end = ultoa_o(len, trash.area, trash.size);
Christopher Faulet32af9a72022-04-15 15:26:24 +0200365 if (http_add_header(htx, ist("content-length"), ist2(trash.area, end-trash.area))) {
Christopher Faulet78fbb9f2019-08-11 23:11:03 +0200366 sl->flags |= HTX_SL_F_CLEN;
Christopher Faulet32af9a72022-04-15 15:26:24 +0200367 msg->flags |= HTTP_MSGF_CNT_LEN;
368 }
Christopher Faulet78fbb9f2019-08-11 23:11:03 +0200369 }
370
371 return 1;
372 }
373
374 /* Analyze the request's headers */
375
376 value = alloc_trash_chunk();
377 if (!value)
378 goto end;
379
380 list_for_each_entry(rule, &fcgi_conf->param_rules, list) {
381 if (rule->cond) {
382 ret = acl_exec_cond(rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
383 ret = acl_pass(ret);
384 if (rule->cond->pol == ACL_COND_UNLESS)
385 ret = !ret;
386
387 /* the rule does not match */
388 if (!ret)
389 continue;
390 }
391
392 param_rule = NULL;
393 node = ebis_lookup_len(&param_rules, rule->name.ptr, rule->name.len);
394 if (node) {
395 param_rule = container_of(node, struct fcgi_param_rule, node);
396 ebpt_delete(node);
397 }
398 else {
Willy Tarreau18f43d82021-03-22 15:07:37 +0100399 param_rule = pool_alloc(pool_head_fcgi_param_rule);
Christopher Faulet78fbb9f2019-08-11 23:11:03 +0200400 if (param_rule == NULL)
401 goto param_rule_err;
402 }
403
404 param_rule->node.key = rule->name.ptr;
405 param_rule->name = rule->name;
406 param_rule->value = &rule->value;
407 ebis_insert(&param_rules, &param_rule->node);
408 }
409
410 list_for_each_entry(rule, &fcgi_conf->hdr_rules, list) {
411 if (rule->cond) {
412 ret = acl_exec_cond(rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
413 ret = acl_pass(ret);
414 if (rule->cond->pol == ACL_COND_UNLESS)
415 ret = !ret;
416
417 /* the rule does not match */
418 if (!ret)
419 continue;
420 }
421
422 hdr_rule = NULL;
423 node = ebis_lookup_len(&hdr_rules, rule->name.ptr, rule->name.len);
424 if (node) {
425 hdr_rule = container_of(node, struct fcgi_hdr_rule, node);
426 ebpt_delete(node);
427 }
428 else {
Willy Tarreau18f43d82021-03-22 15:07:37 +0100429 hdr_rule = pool_alloc(pool_head_fcgi_hdr_rule);
Christopher Faulet78fbb9f2019-08-11 23:11:03 +0200430 if (hdr_rule == NULL)
431 goto hdr_rule_err;
432 }
433
434 hdr_rule->node.key = rule->name.ptr;
435 hdr_rule->name = rule->name;
436 hdr_rule->pass = (rule->type == FCGI_RULE_PASS_HDR);
437 ebis_insert(&hdr_rules, &hdr_rule->node);
438 }
439
440 node = ebpt_first(&param_rules);
441 while (node) {
442 next = ebpt_next(node);
443 ebpt_delete(node);
444 param_rule = container_of(node, struct fcgi_param_rule, node);
445 node = next;
446
447 b_reset(value);
448 value->data = build_logline(s, value->area, value->size, param_rule->value);
Harris Kaufmannb605a732020-07-15 16:26:13 +0200449 if (!value->data) {
450 pool_free(pool_head_fcgi_param_rule, param_rule);
Christopher Faulet78fbb9f2019-08-11 23:11:03 +0200451 continue;
Harris Kaufmannb605a732020-07-15 16:26:13 +0200452 }
Christopher Faulet78fbb9f2019-08-11 23:11:03 +0200453 if (!http_add_header(htx, param_rule->name, ist2(value->area, value->data)))
454 goto rewrite_err;
455 pool_free(pool_head_fcgi_param_rule, param_rule);
456 }
457
458 node = ebpt_first(&hdr_rules);
459 while (node) {
460 next = ebpt_next(node);
461 ebpt_delete(node);
462 hdr_rule = container_of(node, struct fcgi_hdr_rule, node);
463 node = next;
464
465 if (!hdr_rule->pass) {
466 ctx.blk = NULL;
467 while (http_find_header(htx, hdr_rule->name, &ctx, 1))
468 http_remove_header(htx, &ctx);
469 }
470 pool_free(pool_head_fcgi_hdr_rule, hdr_rule);
471 }
472
473 goto end;
474
475 rewrite_err:
Willy Tarreau4781b152021-04-06 13:53:36 +0200476 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_rewrites);
477 _HA_ATOMIC_INC(&s->be->be_counters.failed_rewrites);
William Lallemand36119de2021-03-08 15:26:48 +0100478 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200479 _HA_ATOMIC_INC(&sess->listener->counters->failed_rewrites);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100480 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +0200481 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_rewrites);
Christopher Faulet78fbb9f2019-08-11 23:11:03 +0200482 hdr_rule_err:
483 node = ebpt_first(&hdr_rules);
484 while (node) {
485 next = ebpt_next(node);
486 ebpt_delete(node);
487 hdr_rule = container_of(node, struct fcgi_hdr_rule, node);
488 node = next;
489 pool_free(pool_head_fcgi_hdr_rule, hdr_rule);
490 }
491 param_rule_err:
492 node = ebpt_first(&param_rules);
493 while (node) {
494 next = ebpt_next(node);
495 ebpt_delete(node);
496 param_rule = container_of(node, struct fcgi_param_rule, node);
497 node = next;
498 pool_free(pool_head_fcgi_param_rule, param_rule);
499 }
500 end:
501 free_trash_chunk(value);
502 return 1;
503}
504
505struct flt_ops fcgi_flt_ops = {
506 .init = fcgi_flt_init,
507 .check = fcgi_flt_check,
508 .deinit = fcgi_flt_deinit,
509
510 .attach = fcgi_flt_start,
511 .detach = fcgi_flt_stop,
512
513 .http_headers = fcgi_flt_http_headers,
514};
515
516/**************************************************************************/
517/*********************** FCGI Config parsing ******************************/
518/**************************************************************************/
519static int
520parse_fcgi_flt(char **args, int *cur_arg, struct proxy *px,
521 struct flt_conf *fconf, char **err, void *private)
522{
523 struct flt_conf *f, *back;
524 struct fcgi_flt_conf *fcgi_conf = NULL;
525 char *name = NULL;
526 int pos = *cur_arg;
527
528 /* Get the fcgi-app name*/
Christopher Faulet0ce57b02019-09-18 11:18:33 +0200529 if (!*args[pos + 1]) {
530 memprintf(err, "%s : expects a <name> argument", args[pos]);
531 goto err;
Christopher Faulet78fbb9f2019-08-11 23:11:03 +0200532 }
Christopher Faulet0ce57b02019-09-18 11:18:33 +0200533 name = strdup(args[pos + 1]);
534 if (!name) {
535 memprintf(err, "%s '%s' : out of memory", args[pos], args[pos + 1]);
536 goto err;
537 }
538 pos += 2;
539
Christopher Faulet78fbb9f2019-08-11 23:11:03 +0200540 /* Check if an fcgi-app filter with the same name already exists */
541 list_for_each_entry_safe(f, back, &px->filter_configs, list) {
542 if (f->id != fcgi_flt_id)
543 continue;
544 fcgi_conf = f->conf;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100545 if (strcmp(name, fcgi_conf->name) != 0) {
Christopher Faulet78fbb9f2019-08-11 23:11:03 +0200546 fcgi_conf = NULL;
547 continue;
548 }
549
550 /* Place the filter at its right position */
Willy Tarreau2b718102021-04-21 07:32:39 +0200551 LIST_DELETE(&f->list);
Christopher Faulet78fbb9f2019-08-11 23:11:03 +0200552 free(f);
Willy Tarreau61cfdf42021-02-20 10:46:51 +0100553 ha_free(&name);
Christopher Faulet78fbb9f2019-08-11 23:11:03 +0200554 break;
555 }
556
557 /* No other fcgi-app filter found, create configuration for the explicit one */
558 if (!fcgi_conf) {
559 fcgi_conf = calloc(1, sizeof(*fcgi_conf));
560 if (!fcgi_conf) {
561 memprintf(err, "%s: out of memory", args[*cur_arg]);
562 goto err;
563 }
564 fcgi_conf->name = name;
565 LIST_INIT(&fcgi_conf->param_rules);
566 LIST_INIT(&fcgi_conf->hdr_rules);
567 }
568
569 fconf->id = fcgi_flt_id;
570 fconf->conf = fcgi_conf;
571 fconf->ops = &fcgi_flt_ops;
572
573 *cur_arg = pos;
574 return 0;
575 err:
576 free(name);
Christopher Faulet78fbb9f2019-08-11 23:11:03 +0200577 return -1;
578}
579
580/* Parses the "use-fcgi-app" proxy keyword */
581static int proxy_parse_use_fcgi_app(char **args, int section, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +0100582 const struct proxy *defpx, const char *file, int line,
Christopher Faulet78fbb9f2019-08-11 23:11:03 +0200583 char **err)
584{
585 struct flt_conf *fconf = NULL;
586 struct fcgi_flt_conf *fcgi_conf = NULL;
587 int retval = 0;
588
Aurelien DARRAGONd49a5802023-01-12 15:44:22 +0100589 if ((curpx->cap & PR_CAP_DEF) || !(curpx->cap & PR_CAP_BE)) {
Christopher Faulet78fbb9f2019-08-11 23:11:03 +0200590 memprintf(err, "'%s' only available in backend or listen section", args[0]);
591 retval = -1;
592 goto end;
593 }
594
595 if (!*(args[1])) {
596 memprintf(err, "'%s' expects <name> as argument", args[0]);
597 retval = -1;
598 goto end;
599 }
600
601 /* check if a fcgi filter was already registered with this name,
602 * if that's the case, must use it. */
603 list_for_each_entry(fconf, &curpx->filter_configs, list) {
604 if (fconf->id == fcgi_flt_id) {
605 fcgi_conf = fconf->conf;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100606 if (fcgi_conf && strcmp((char *)fcgi_conf->name, args[1]) == 0)
Christopher Faulet78fbb9f2019-08-11 23:11:03 +0200607 goto end;
608 memprintf(err, "'%s' : only one fcgi-app supported per backend", args[0]);
609 retval = -1;
610 goto end;
611 }
612 }
613
614 /* Create the FCGI filter config */
615 fcgi_conf = calloc(1, sizeof(*fcgi_conf));
616 if (!fcgi_conf)
617 goto err;
618 fcgi_conf->name = strdup(args[1]);
619 LIST_INIT(&fcgi_conf->param_rules);
620 LIST_INIT(&fcgi_conf->hdr_rules);
Christopher Faulet78fbb9f2019-08-11 23:11:03 +0200621
622 /* Register the filter */
623 fconf = calloc(1, sizeof(*fconf));
624 if (!fconf)
625 goto err;
626 fconf->id = fcgi_flt_id;
627 fconf->conf = fcgi_conf;
628 fconf->ops = &fcgi_flt_ops;
Willy Tarreau2b718102021-04-21 07:32:39 +0200629 LIST_APPEND(&curpx->filter_configs, &fconf->list);
Christopher Faulet78fbb9f2019-08-11 23:11:03 +0200630
631 end:
632 return retval;
633 err:
634 if (fcgi_conf) {
635 free(fcgi_conf->name);
636 free(fcgi_conf);
637 }
638 memprintf(err, "out of memory");
639 retval = -1;
640 goto end;
641}
642
643/* Finishes the parsing of FCGI application of proxies and servers */
644static int cfg_fcgi_apps_postparser()
645{
646 struct fcgi_app *curapp;
647 struct proxy *px;
648 struct server *srv;
Christopher Fauletc97406f2020-06-22 11:07:18 +0200649 struct logsrv *logsrv;
Christopher Faulet78fbb9f2019-08-11 23:11:03 +0200650 int err_code = 0;
651
652 for (px = proxies_list; px; px = px->next) {
653 struct fcgi_flt_conf *fcgi_conf = find_px_fcgi_conf(px);
654 int nb_fcgi_srv = 0;
655
656 if (px->mode == PR_MODE_TCP && fcgi_conf) {
Amaury Denoyelle11124302021-06-04 18:22:08 +0200657 ha_alert("proxy '%s': FCGI application cannot be used in non-HTTP mode.\n",
Christopher Faulet78fbb9f2019-08-11 23:11:03 +0200658 px->id);
659 err_code |= ERR_ALERT | ERR_FATAL;
660 goto end;
661 }
662
Christopher Faulet18c13d32022-05-16 11:43:10 +0200663 /* By default, for FCGI-ready backend, HTTP request header names
664 * are restricted and the "delete" policy is set
665 */
666 if (fcgi_conf && !(px->options2 & PR_O2_RSTRICT_REQ_HDR_NAMES_MASK))
667 px->options2 |= PR_O2_RSTRICT_REQ_HDR_NAMES_DEL;
668
Christopher Faulet78fbb9f2019-08-11 23:11:03 +0200669 for (srv = px->srv; srv; srv = srv->next) {
670 if (srv->mux_proto && isteq(srv->mux_proto->token, ist("fcgi"))) {
671 nb_fcgi_srv++;
672 if (fcgi_conf)
673 continue;
Amaury Denoyelle11124302021-06-04 18:22:08 +0200674 ha_alert("proxy '%s': FCGI server '%s' has no FCGI app configured.\n",
Christopher Faulet78fbb9f2019-08-11 23:11:03 +0200675 px->id, srv->id);
676 err_code |= ERR_ALERT | ERR_FATAL;
677 goto end;
678 }
679 }
680 if (fcgi_conf && !nb_fcgi_srv) {
Amaury Denoyelle11124302021-06-04 18:22:08 +0200681 ha_alert("proxy '%s': FCGI app configured but no FCGI server found.\n",
Christopher Faulet78fbb9f2019-08-11 23:11:03 +0200682 px->id);
683 err_code |= ERR_ALERT | ERR_FATAL;
684 goto end;
685 }
686 }
687
688 for (curapp = fcgi_apps; curapp != NULL; curapp = curapp->next) {
689 if (!istlen(curapp->docroot)) {
Amaury Denoyelle11124302021-06-04 18:22:08 +0200690 ha_alert("fcgi-app '%s': no docroot configured.\n",
Christopher Faulet78fbb9f2019-08-11 23:11:03 +0200691 curapp->name);
692 err_code |= ERR_ALERT | ERR_FATAL;
693 goto end;
694 }
695 if (!(curapp->flags & (FCGI_APP_FL_MPXS_CONNS|FCGI_APP_FL_GET_VALUES))) {
696 if (curapp->maxreqs > 1) {
Amaury Denoyelle11124302021-06-04 18:22:08 +0200697 ha_warning("fcgi-app '%s': multiplexing not supported, "
Christopher Faulet78fbb9f2019-08-11 23:11:03 +0200698 "ignore the option 'max-reqs'.\n",
699 curapp->name);
700 err_code |= ERR_WARN;
701 }
702 curapp->maxreqs = 1;
703 }
Christopher Fauletc97406f2020-06-22 11:07:18 +0200704
705 list_for_each_entry(logsrv, &curapp->logsrvs, list) {
706 if (logsrv->type == LOG_TARGET_BUFFER) {
707 struct sink *sink = sink_find(logsrv->ring_name);
708
709 if (!sink || sink->type != SINK_TYPE_BUFFER) {
Amaury Denoyelle11124302021-06-04 18:22:08 +0200710 ha_alert("fcgi-app '%s' : log server uses unknown ring named '%s'.\n",
Christopher Fauletc97406f2020-06-22 11:07:18 +0200711 curapp->name, logsrv->ring_name);
712 err_code |= ERR_ALERT | ERR_FATAL;
713 }
714 logsrv->sink = sink;
715 }
716 }
Christopher Faulet78fbb9f2019-08-11 23:11:03 +0200717 }
718
719 end:
720 return err_code;
721}
722
723static int fcgi_app_add_rule(struct fcgi_app *curapp, enum fcgi_rule_type type, char *name, char *value,
724 struct acl_cond *cond, char **err)
725{
726 struct fcgi_rule_conf *rule;
727
728 /* Param not found, add a new one */
729 rule = calloc(1, sizeof(*rule));
730 if (!rule)
731 goto err;
732 LIST_INIT(&rule->list);
733 rule->type = type;
734 if (type == FCGI_RULE_SET_PARAM || type == FCGI_RULE_UNSET_PARAM) {
735 struct ist fname = fcgi_param_name(trash.area, ist(name));
736 rule->name = my_strndup(fname.ptr, fname.len);
737 }
Christopher Fauletbc96c902019-12-02 10:33:31 +0100738 else { /* FCGI_RULE_PASS_HDR/FCGI_RULE_HIDE_HDR */
739 struct ist fname = ist2bin_lc(trash.area, ist(name));
740 rule->name = my_strndup(fname.ptr, fname.len);
741 }
Christopher Faulet78fbb9f2019-08-11 23:11:03 +0200742 if (!rule->name)
743 goto err;
744
745 if (value) {
746 rule->value = strdup(value);
747 if (!rule->value)
748 goto err;
749 }
750 rule->cond = cond;
Willy Tarreau2b718102021-04-21 07:32:39 +0200751 LIST_APPEND(&curapp->conf.rules, &rule->list);
Christopher Faulet78fbb9f2019-08-11 23:11:03 +0200752 return 1;
753
754 err:
755 if (rule) {
756 free(rule->name);
757 free(rule->value);
758 free(rule);
759 }
Aurelien DARRAGONc6100952023-05-11 12:29:51 +0200760 free_acl_cond(cond);
Christopher Faulet78fbb9f2019-08-11 23:11:03 +0200761 memprintf(err, "out of memory");
762 return 0;
763}
764
765/* Parses "fcgi-app" section */
766static int cfg_parse_fcgi_app(const char *file, int linenum, char **args, int kwm)
767{
768 static struct fcgi_app *curapp = NULL;
769 struct acl_cond *cond = NULL;
770 char *name, *value = NULL;
771 enum fcgi_rule_type type;
772 int err_code = 0;
773 const char *err;
774 char *errmsg = NULL;
775
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100776 if (strcmp(args[0], "fcgi-app") == 0) { /* new fcgi-app */
Christopher Faulet78fbb9f2019-08-11 23:11:03 +0200777 if (!*(args[1])) {
778 ha_alert("parsing [%s:%d]: '%s' expects <name> as argument.\n",
779 file, linenum, args[0]);
780 err_code |= ERR_ALERT | ERR_FATAL;
781 goto out;
782 }
783 if (alertif_too_many_args(1, file, linenum, args, &err_code))
784 goto out;
785
786 err = invalid_char(args[1]);
787 if (err) {
788 ha_alert("parsing [%s:%d]: character '%c' is not permitted in '%s' name '%s'.\n",
789 file, linenum, *err, args[0], args[1]);
790 err_code |= ERR_ALERT | ERR_FATAL;
791 goto out;
792 }
793
794 for (curapp = fcgi_apps; curapp != NULL; curapp = curapp->next) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100795 if (strcmp(curapp->name, args[1]) == 0) {
Christopher Faulet78fbb9f2019-08-11 23:11:03 +0200796 ha_alert("Parsing [%s:%d]: fcgi-app section '%s' has the same name as another one declared at %s:%d.\n",
797 file, linenum, args[1], curapp->conf.file, curapp->conf.line);
798 err_code |= ERR_ALERT | ERR_FATAL;
799 }
800 }
801
802 curapp = calloc(1, sizeof(*curapp));
803 if (!curapp) {
804 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
805 err_code |= ERR_ALERT | ERR_ABORT;
806 goto out;
807 }
808
809 curapp->next = fcgi_apps;
810 fcgi_apps = curapp;
811 curapp->flags = FCGI_APP_FL_KEEP_CONN;
812 curapp->docroot = ist(NULL);
813 curapp->index = ist(NULL);
814 curapp->pathinfo_re = NULL;
815 curapp->name = strdup(args[1]);
816 curapp->maxreqs = 1;
817 curapp->conf.file = strdup(file);
818 curapp->conf.line = linenum;
819 LIST_INIT(&curapp->acls);
820 LIST_INIT(&curapp->logsrvs);
821 LIST_INIT(&curapp->conf.args.list);
822 LIST_INIT(&curapp->conf.rules);
823
824 /* Set info about authentication */
825 if (!fcgi_app_add_rule(curapp, FCGI_RULE_SET_PARAM, "REMOTE_USER", "%[http_auth_user]", NULL, &errmsg) ||
826 !fcgi_app_add_rule(curapp, FCGI_RULE_SET_PARAM, "AUTH_TYPE", "%[http_auth_type]", NULL, &errmsg)) {
827 ha_alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
828 args[1], errmsg);
829 err_code |= ERR_ALERT | ERR_FATAL;
830 }
831
832 /* Hide hop-by-hop headers by default */
833 if (!fcgi_app_add_rule(curapp, FCGI_RULE_HIDE_HDR, "connection", NULL, NULL, &errmsg) ||
834 !fcgi_app_add_rule(curapp, FCGI_RULE_HIDE_HDR, "keep-alive", NULL, NULL, &errmsg) ||
835 !fcgi_app_add_rule(curapp, FCGI_RULE_HIDE_HDR, "authorization", NULL, NULL, &errmsg) ||
836 !fcgi_app_add_rule(curapp, FCGI_RULE_HIDE_HDR, "proxy", NULL, NULL, &errmsg) ||
837 !fcgi_app_add_rule(curapp, FCGI_RULE_HIDE_HDR, "proxy-authorization", NULL, NULL, &errmsg) ||
838 !fcgi_app_add_rule(curapp, FCGI_RULE_HIDE_HDR, "proxy-authenticate", NULL, NULL, &errmsg) ||
839 !fcgi_app_add_rule(curapp, FCGI_RULE_HIDE_HDR, "te", NULL, NULL, &errmsg) ||
840 !fcgi_app_add_rule(curapp, FCGI_RULE_HIDE_HDR, "trailers", NULL, NULL, &errmsg) ||
841 !fcgi_app_add_rule(curapp, FCGI_RULE_HIDE_HDR, "transfer-encoding", NULL, NULL, &errmsg) ||
842 !fcgi_app_add_rule(curapp, FCGI_RULE_HIDE_HDR, "upgrade", NULL, NULL, &errmsg)) {
843 ha_alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
844 args[1], errmsg);
845 err_code |= ERR_ALERT | ERR_FATAL;
846 }
847 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100848 else if (strcmp(args[0], "docroot") == 0) {
Christopher Faulet78fbb9f2019-08-11 23:11:03 +0200849 if (!*(args[1])) {
850 ha_alert("parsing [%s:%d] : '%s' expects <path> as argument.\n",
851 file, linenum, args[0]);
852 err_code |= ERR_ALERT | ERR_FATAL;
853 goto out;
854 }
855 if (alertif_too_many_args_idx(0, 1, file, linenum, args, &err_code))
856 goto out;
Tim Duesterhused526372020-03-05 17:56:33 +0100857 istfree(&curapp->docroot);
Tim Duesterhusdcf753a2021-03-04 17:31:47 +0100858 curapp->docroot = ist(strdup(args[1]));
Tim Duesterhused526372020-03-05 17:56:33 +0100859 if (!isttest(curapp->docroot)) {
Christopher Faulet78fbb9f2019-08-11 23:11:03 +0200860 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
861 err_code |= ERR_ALERT | ERR_ABORT;
862 }
863 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100864 else if (strcmp(args[0], "path-info") == 0) {
Christopher Faulet78fbb9f2019-08-11 23:11:03 +0200865 if (!*(args[1])) {
866 ha_alert("parsing [%s:%d] : '%s' expects <regex> as argument.\n",
867 file, linenum, args[0]);
868 err_code |= ERR_ALERT | ERR_FATAL;
869 goto out;
870 }
871 if (alertif_too_many_args_idx(0, 1, file, linenum, args, &err_code))
872 goto out;
873 regex_free(curapp->pathinfo_re);
874 curapp->pathinfo_re = regex_comp(args[1], 1, 1, &errmsg);
875 if (!curapp->pathinfo_re) {
876 ha_alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
877 args[1], errmsg);
878 err_code |= ERR_ALERT | ERR_FATAL;
879 }
880 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100881 else if (strcmp(args[0], "index") == 0) {
Christopher Faulet78fbb9f2019-08-11 23:11:03 +0200882 if (!*(args[1])) {
883 ha_alert("parsing [%s:%d] : '%s' expects <filename> as argument.\n",
884 file, linenum, args[0]);
885 err_code |= ERR_ALERT | ERR_FATAL;
886 goto out;
887 }
888 if (alertif_too_many_args_idx(0, 1, file, linenum, args, &err_code))
889 goto out;
Tim Duesterhused526372020-03-05 17:56:33 +0100890 istfree(&curapp->index);
Tim Duesterhusdcf753a2021-03-04 17:31:47 +0100891 curapp->index = ist(strdup(args[1]));
Tim Duesterhused526372020-03-05 17:56:33 +0100892 if (!isttest(curapp->index)) {
Christopher Faulet78fbb9f2019-08-11 23:11:03 +0200893 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
894 err_code |= ERR_ALERT | ERR_ABORT;
895 }
896 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100897 else if (strcmp(args[0], "acl") == 0) {
Christopher Faulet78fbb9f2019-08-11 23:11:03 +0200898 const char *err;
899 err = invalid_char(args[1]);
900 if (err) {
901 ha_alert("parsing [%s:%d] : character '%c' is not permitted in acl name '%s'.\n",
902 file, linenum, *err, args[1]);
903 err_code |= ERR_ALERT | ERR_FATAL;
Tim Duesterhus0cf811a2020-02-05 21:00:50 +0100904 goto out;
Christopher Faulet78fbb9f2019-08-11 23:11:03 +0200905 }
Tim Duesterhus0cf811a2020-02-05 21:00:50 +0100906 if (strcasecmp(args[1], "or") == 0) {
Tim Duesterhusf1bc24c2020-02-06 22:04:03 +0100907 ha_alert("parsing [%s:%d] : acl name '%s' will never match. 'or' is used to express a "
Tim Duesterhus0cf811a2020-02-05 21:00:50 +0100908 "logical disjunction within a condition.\n",
909 file, linenum, args[1]);
910 err_code |= ERR_ALERT | ERR_FATAL;
911 goto out;
912 }
913 if (parse_acl((const char **)args+1, &curapp->acls, &errmsg, &curapp->conf.args, file, linenum) == NULL) {
Christopher Faulet78fbb9f2019-08-11 23:11:03 +0200914 ha_alert("parsing [%s:%d] : error detected while parsing ACL '%s' : %s.\n",
915 file, linenum, args[1], errmsg);
916 err_code |= ERR_ALERT | ERR_FATAL;
Tim Duesterhus0cf811a2020-02-05 21:00:50 +0100917 goto out;
Christopher Faulet78fbb9f2019-08-11 23:11:03 +0200918 }
919 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100920 else if (strcmp(args[0], "set-param") == 0) {
Christopher Faulet78fbb9f2019-08-11 23:11:03 +0200921 if (!*(args[1]) || !*(args[2])) {
922 ha_alert("parsing [%s:%d] : '%s' expects <name> and <value> as arguments.\n",
923 file, linenum, args[0]);
924 err_code |= ERR_ALERT | ERR_FATAL;
925 goto out;
926 }
927 type = FCGI_RULE_SET_PARAM;
928 name = args[1];
929 value = args[2];
930 cond = NULL;
931 args += 3;
932
933 parse_cond_rule:
934 if (!*(args[0])) /* No condition */
935 goto add_rule;
936
937 if (strcmp(args[0], "if") == 0)
938 cond = parse_acl_cond((const char **)args+1, &curapp->acls, ACL_COND_IF, &errmsg, &curapp->conf.args,
939 file, linenum);
940 else if (strcmp(args[0], "unless") == 0)
941 cond = parse_acl_cond((const char **)args+1, &curapp->acls, ACL_COND_UNLESS, &errmsg, &curapp->conf.args,
942 file, linenum);
943 if (!cond) {
944 ha_alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
945 name, errmsg);
946 err_code |= ERR_ALERT | ERR_FATAL;
947 }
948 add_rule:
949 if (!fcgi_app_add_rule(curapp, type, name, value, cond, &errmsg)) {
950 ha_alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
951 name, errmsg);
952 err_code |= ERR_ALERT | ERR_FATAL;
953 }
954 }
955#if 0 /* Disabled for now */
956 else if (!strcmp(args[0], "unset-param")) {
957 if (!*(args[1])) {
958 ha_alert("parsing [%s:%d] : '%s' expects <name> as arguments.\n",
959 file, linenum, args[0]);
960 err_code |= ERR_ALERT | ERR_FATAL;
961 goto out;
962 }
963 type = FCGI_RULE_UNSET_PARAM;
964 name = args[1];
965 value = NULL;
966 cond = NULL;
967 args += 2;
968 goto parse_cond_rule;
969 }
970#endif
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100971 else if (strcmp(args[0], "pass-header") == 0) {
Christopher Faulet78fbb9f2019-08-11 23:11:03 +0200972 if (!*(args[1])) {
973 ha_alert("parsing [%s:%d] : '%s' expects <name> as arguments.\n",
974 file, linenum, args[0]);
975 err_code |= ERR_ALERT | ERR_FATAL;
976 goto out;
977 }
978 type = FCGI_RULE_PASS_HDR;
979 name = args[1];
980 value = NULL;
981 cond = NULL;
982 args += 2;
983 goto parse_cond_rule;
984 }
985#if 0 /* Disabled for now */
986 else if (!strcmp(args[0], "hide-header")) {
987 if (!*(args[1])) {
988 ha_alert("parsing [%s:%d] : '%s' expects <name> as arguments.\n",
989 file, linenum, args[0]);
990 err_code |= ERR_ALERT | ERR_FATAL;
991 goto out;
992 }
993 type = FCGI_RULE_HIDE_HDR;
994 name = args[1];
995 value = NULL;
996 cond = NULL;
997 args += 2;
998 goto parse_cond_rule;
999 }
1000#endif
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001001 else if (strcmp(args[0], "option") == 0) {
Christopher Faulet78fbb9f2019-08-11 23:11:03 +02001002 if (!*(args[1])) {
1003 ha_alert("parsing [%s:%d]: '%s' expects an option name.\n",
1004 file, linenum, args[0]);
1005 err_code |= ERR_ALERT | ERR_FATAL;
1006 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001007 else if (strcmp(args[1], "keep-conn") == 0) {
Christopher Faulet78fbb9f2019-08-11 23:11:03 +02001008 if (alertif_too_many_args_idx(0, 1, file, linenum, args, &err_code))
1009 goto out;
1010 if (kwm == KWM_STD)
1011 curapp->flags |= FCGI_APP_FL_KEEP_CONN;
1012 else if (kwm == KWM_NO)
1013 curapp->flags &= ~FCGI_APP_FL_KEEP_CONN;
1014 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001015 else if (strcmp(args[1], "get-values") == 0) {
Christopher Faulet78fbb9f2019-08-11 23:11:03 +02001016 if (alertif_too_many_args_idx(0, 1, file, linenum, args, &err_code))
1017 goto out;
1018 if (kwm == KWM_STD)
1019 curapp->flags |= FCGI_APP_FL_GET_VALUES;
1020 else if (kwm == KWM_NO)
1021 curapp->flags &= ~FCGI_APP_FL_GET_VALUES;
1022 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001023 else if (strcmp(args[1], "mpxs-conns") == 0) {
Christopher Faulet78fbb9f2019-08-11 23:11:03 +02001024 if (alertif_too_many_args_idx(0, 1, file, linenum, args, &err_code))
1025 goto out;
1026 if (kwm == KWM_STD)
1027 curapp->flags |= FCGI_APP_FL_MPXS_CONNS;
1028 else if (kwm == KWM_NO)
1029 curapp->flags &= ~FCGI_APP_FL_MPXS_CONNS;
1030 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001031 else if (strcmp(args[1], "max-reqs") == 0) {
Christopher Faulet78fbb9f2019-08-11 23:11:03 +02001032 if (kwm != KWM_STD) {
1033 ha_alert("parsing [%s:%d]: negation/default is not supported for option '%s'.\n",
1034 file, linenum, args[1]);
1035 err_code |= ERR_ALERT | ERR_FATAL;
1036 goto out;
1037 }
1038 if (!*(args[2])) {
1039 ha_alert("parsing [%s:%d]: option '%s' expects an integer argument.\n",
1040 file, linenum, args[1]);
1041 err_code |= ERR_ALERT | ERR_FATAL;
1042 goto out;
1043 }
1044 if (alertif_too_many_args_idx(1, 1, file, linenum, args, &err_code))
1045 goto out;
1046
1047 curapp->maxreqs = atol(args[2]);
1048 if (!curapp->maxreqs) {
1049 ha_alert("parsing [%s:%d]: option '%s' expects a strictly positive integer argument.\n",
1050 file, linenum, args[1]);
1051 err_code |= ERR_ALERT | ERR_FATAL;
1052 goto out;
1053 }
1054 }
1055 else {
1056 ha_alert("parsing [%s:%d] : unknown option '%s'.\n", file, linenum, args[1]);
1057 err_code |= ERR_ALERT | ERR_FATAL;
1058 }
1059 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001060 else if (strcmp(args[0], "log-stderr") == 0) {
Emeric Brun9533a702021-04-02 10:13:43 +02001061 if (!parse_logsrv(args, &curapp->logsrvs, (kwm == KWM_NO), file, linenum, &errmsg)) {
Christopher Faulet78fbb9f2019-08-11 23:11:03 +02001062 ha_alert("parsing [%s:%d] : %s : %s\n", file, linenum, args[0], errmsg);
1063 err_code |= ERR_ALERT | ERR_FATAL;
1064 }
1065 }
1066 else {
1067 ha_alert("parsing [%s:%d]: unknown keyword '%s' in '%s' section\n", file, linenum, args[0], "fcgi-app");
1068 err_code |= ERR_ALERT | ERR_FATAL;
1069 }
1070
1071out:
1072 free(errmsg);
1073 return err_code;
1074}
1075
1076
1077/**************************************************************************/
1078/*********************** FCGI Deinit functions ****************************/
1079/**************************************************************************/
1080void fcgi_apps_deinit()
1081{
1082 struct fcgi_app *curapp, *nextapp;
1083 struct logsrv *log, *logb;
1084
1085 for (curapp = fcgi_apps; curapp != NULL; curapp = nextapp) {
1086 struct fcgi_rule_conf *rule, *back;
1087
1088 free(curapp->name);
Tim Duesterhused526372020-03-05 17:56:33 +01001089 istfree(&curapp->docroot);
1090 istfree(&curapp->index);
Christopher Faulet78fbb9f2019-08-11 23:11:03 +02001091 regex_free(curapp->pathinfo_re);
1092 free(curapp->conf.file);
1093
1094 list_for_each_entry_safe(log, logb, &curapp->logsrvs, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001095 LIST_DELETE(&log->list);
Christopher Faulet78fbb9f2019-08-11 23:11:03 +02001096 free(log);
1097 }
1098
1099 list_for_each_entry_safe(rule, back, &curapp->conf.rules, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001100 LIST_DELETE(&rule->list);
Christopher Faulet78fbb9f2019-08-11 23:11:03 +02001101 fcgi_release_rule_conf(rule);
1102 }
1103
1104 nextapp = curapp->next;
1105 free(curapp);
1106 }
1107}
1108
1109
1110/**************************************************************************/
1111/*************** Keywords definition and registration *********************/
1112/**************************************************************************/
1113static struct cfg_kw_list cfg_kws = {ILH, {
1114 { CFG_LISTEN, "use-fcgi-app", proxy_parse_use_fcgi_app },
1115 { 0, NULL, NULL },
1116}};
1117
1118// FIXME: Add rep.fcgi smp_fetch
1119static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
1120 { "fcgi.docroot", smp_fetch_fcgi_docroot, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreaufc41e252019-09-27 22:45:17 +02001121 { "fcgi.index", smp_fetch_fcgi_index, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
1122 { /* END */ }
Christopher Faulet78fbb9f2019-08-11 23:11:03 +02001123}};
1124
1125/* Declare the filter parser for "fcgi-app" keyword */
1126static struct flt_kw_list filter_kws = { "FCGI", { }, {
1127 { "fcgi-app", parse_fcgi_flt, NULL },
1128 { NULL, NULL, NULL },
1129 }
1130};
1131
1132INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);
1133INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
1134INITCALL1(STG_REGISTER, flt_register_keywords, &filter_kws);
1135
1136INITCALL1(STG_REGISTER, hap_register_post_deinit, fcgi_apps_deinit);
1137
1138REGISTER_CONFIG_SECTION("fcgi-app", cfg_parse_fcgi_app, NULL);
1139REGISTER_CONFIG_POSTPARSER("fcgi-apps", cfg_fcgi_apps_postparser);
1140
1141/*
1142 * Local variables:
1143 * c-indent-level: 8
1144 * c-basic-offset: 8
1145 * End:
1146 */