Skip to content

Commit e00e2cc

Browse files
committed
Do not use main/server contexts for creating/merging configuration
1 parent 71ede63 commit e00e2cc

File tree

1 file changed

+50
-118
lines changed

1 file changed

+50
-118
lines changed

src/ngx_http_modsecurity_module.c

Lines changed: 50 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,9 @@
2525
#include <ngx_http.h>
2626

2727
static ngx_int_t ngx_http_modsecurity_init(ngx_conf_t *cf);
28-
static void *ngx_http_modsecurity_create_main_conf(ngx_conf_t *cf);
2928
static void *ngx_http_modsecurity_create_conf(ngx_conf_t *cf);
30-
static char *ngx_http_modsecurity_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child);
31-
static char *ngx_http_modsecurity_merge_srv_conf(ngx_conf_t *cf, void *parent, void *child);
29+
static char *ngx_http_modsecurity_merge_conf(ngx_conf_t *cf, void *parent, void *child);
3230
static void ngx_http_modsecurity_config_cleanup(void *data);
33-
static char *ngx_http_modsecurity_init_main_conf(ngx_conf_t *cf, void *conf);
3431

3532

3633
/*
@@ -234,31 +231,30 @@ ngx_http_modsecurity_cleanup(void *data)
234231
ngx_inline ngx_http_modsecurity_ctx_t *
235232
ngx_http_modsecurity_create_ctx(ngx_http_request_t *r)
236233
{
237-
ngx_http_modsecurity_ctx_t *ctx;
238-
ngx_http_modsecurity_conf_t *loc_cf = NULL;
239-
ngx_http_modsecurity_conf_t *cf = NULL;
240-
ngx_pool_cleanup_t *cln = NULL;
241-
ngx_str_t s;
234+
ngx_str_t s;
235+
ngx_pool_cleanup_t *cln;
236+
ngx_http_modsecurity_ctx_t *ctx;
237+
ngx_http_modsecurity_conf_t *mcf;
242238

243239
ctx = ngx_pcalloc(r->pool, sizeof(ngx_http_modsecurity_ctx_t));
244240
if (ctx == NULL)
245241
{
246242
dd("failed to allocate memory for the context.");
247243
return NULL;
248244
}
249-
cf = ngx_http_get_module_main_conf(r, ngx_http_modsecurity_module);
250-
loc_cf = ngx_http_get_module_loc_conf(r, ngx_http_modsecurity_module);
251245

252-
dd("creating transaction with the following rules: '%p' -- ms: '%p'", loc_cf->rules_set, cf->modsec);
246+
mcf = ngx_http_get_module_loc_conf(r, ngx_http_modsecurity_module);
247+
248+
dd("creating transaction with the following rules: '%p' -- ms: '%p'", mcf->rules_set, mcf->modsec);
253249

254-
if (loc_cf->transaction_id) {
255-
if (ngx_http_complex_value(r, loc_cf->transaction_id, &s) != NGX_OK) {
250+
if (mcf->transaction_id) {
251+
if (ngx_http_complex_value(r, mcf->transaction_id, &s) != NGX_OK) {
256252
return NGX_CONF_ERROR;
257253
}
258-
ctx->modsec_transaction = msc_new_transaction_with_id(cf->modsec, loc_cf->rules_set, (char *) s.data, r->connection->log);
254+
ctx->modsec_transaction = msc_new_transaction_with_id(mcf->modsec, mcf->rules_set, (char *) s.data, r->connection->log);
259255

260256
} else {
261-
ctx->modsec_transaction = msc_new_transaction(cf->modsec, loc_cf->rules_set, r->connection->log);
257+
ctx->modsec_transaction = msc_new_transaction(mcf->modsec, mcf->rules_set, r->connection->log);
262258
}
263259

264260
dd("transaction created");
@@ -437,32 +433,32 @@ static ngx_command_t ngx_http_modsecurity_commands[] = {
437433

438434

439435
static ngx_http_module_t ngx_http_modsecurity_ctx = {
440-
NULL, /* preconfiguration */
441-
ngx_http_modsecurity_init, /* postconfiguration */
436+
NULL, /* preconfiguration */
437+
ngx_http_modsecurity_init, /* postconfiguration */
442438

443-
ngx_http_modsecurity_create_main_conf, /* create main configuration */
444-
ngx_http_modsecurity_init_main_conf, /* init main configuration */
439+
NULL, /* create main configuration */
440+
NULL, /* init main configuration */
445441

446-
ngx_http_modsecurity_create_conf, /* create server configuration */
447-
ngx_http_modsecurity_merge_srv_conf, /* merge server configuration */
442+
NULL, /* create server configuration */
443+
NULL, /* merge server configuration */
448444

449-
ngx_http_modsecurity_create_conf, /* create location configuration */
450-
ngx_http_modsecurity_merge_loc_conf /* merge location configuration */
445+
ngx_http_modsecurity_create_conf, /* create location configuration */
446+
ngx_http_modsecurity_merge_conf /* merge location configuration */
451447
};
452448

453449

454450
ngx_module_t ngx_http_modsecurity_module = {
455451
NGX_MODULE_V1,
456-
&ngx_http_modsecurity_ctx, /* module context */
457-
ngx_http_modsecurity_commands, /* module directives */
458-
NGX_HTTP_MODULE, /* module type */
459-
NULL, /* init master */
460-
NULL, /* init module */
461-
NULL, /* init process */
462-
NULL, /* init thread */
463-
NULL, /* exit thread */
464-
NULL, /* exit process */
465-
NULL, /* exit master */
452+
&ngx_http_modsecurity_ctx, /* module context */
453+
ngx_http_modsecurity_commands, /* module directives */
454+
NGX_HTTP_MODULE, /* module type */
455+
NULL, /* init master */
456+
NULL, /* init module */
457+
NULL, /* init process */
458+
NULL, /* init thread */
459+
NULL, /* exit thread */
460+
NULL, /* exit process */
461+
NULL, /* exit master */
466462
NGX_MODULE_V1_PADDING
467463
};
468464

@@ -545,51 +541,15 @@ ngx_http_modsecurity_init(ngx_conf_t *cf)
545541

546542

547543
static void *
548-
ngx_http_modsecurity_create_main_conf(ngx_conf_t *cf)
544+
ngx_http_modsecurity_create_conf(ngx_conf_t *cf)
549545
{
550-
ngx_http_modsecurity_conf_t *conf;
546+
ngx_pool_cleanup_t *cln;
547+
ngx_http_modsecurity_conf_t *conf;
551548

552549
ngx_log_error(NGX_LOG_NOTICE, cf->log, 0, MODSECURITY_NGINX_WHOAMI);
553550

554-
/* ngx_pcalloc already sets all of this scructure to zeros. */
555-
conf = ngx_http_modsecurity_create_conf(cf);
556-
557-
if (conf == NULL || conf == NGX_CONF_ERROR) {
558-
dd("failed to allocate space for the ModSecurity configuration");
559-
return NGX_CONF_ERROR;
560-
}
561-
562-
dd ("conf crated at: '%p'", conf);
563-
564-
/* Create our ModSecurity instace */
565-
conf->modsec = msc_init();
566-
if (conf->modsec == NULL)
567-
{
568-
dd("failed to create the ModSecurity instance");
569-
return NGX_CONF_ERROR;
570-
}
571-
572-
/* Provide our connector information to LibModSecurity */
573-
msc_set_connector_info(conf->modsec, MODSECURITY_NGINX_WHOAMI);
574-
msc_set_log_cb(conf->modsec, ngx_http_modsecurity_log);
575-
576-
return conf;
577-
}
578-
579-
580-
static char *ngx_http_modsecurity_init_main_conf(ngx_conf_t *cf, void *conf)
581-
{
582-
dd("modsec main conf init. Loaded rules:");
583-
584-
return NGX_CONF_OK;
585-
}
586-
587-
588-
static void *ngx_http_modsecurity_create_conf(ngx_conf_t *cf)
589-
{
590-
ngx_pool_cleanup_t *cln = NULL;
591-
ngx_http_modsecurity_conf_t *conf = (ngx_http_modsecurity_conf_t *)
592-
ngx_pcalloc(cf->pool, sizeof(ngx_http_modsecurity_conf_t));
551+
conf = (ngx_http_modsecurity_conf_t *) ngx_pcalloc(cf->pool,
552+
sizeof(ngx_http_modsecurity_conf_t));
593553

594554
if (conf == NULL)
595555
{
@@ -619,58 +579,30 @@ static void *ngx_http_modsecurity_create_conf(ngx_conf_t *cf)
619579
dd("failed to create the ModSecurity configuration cleanup");
620580
return NGX_CONF_ERROR;
621581
}
582+
622583
cln->handler = ngx_http_modsecurity_config_cleanup;
623584
cln->data = conf;
624585

625-
return conf;
626-
}
627-
586+
dd ("conf created at: '%p'", conf);
628587

629-
static char *
630-
ngx_http_modsecurity_merge_srv_conf(ngx_conf_t *cf, void *parent, void *child)
631-
{
632-
ngx_http_modsecurity_conf_t *p = parent;
633-
ngx_http_modsecurity_conf_t *c = child;
634-
#if defined(MODSECURITY_DDEBUG) && (MODSECURITY_DDEBUG)
635-
ngx_http_core_srv_conf_t *clcf = ngx_http_conf_get_module_srv_conf(cf, ngx_http_core_module);
636-
#endif
637-
int rules;
638-
const char *error = NULL;
639-
640-
dd("merging srv config [%s] - parent: '%p' child: '%p'",
641-
ngx_str_to_char(clcf->server_name, cf->pool), parent,
642-
child);
643-
dd(" state - parent: '%d' child: '%d'",
644-
(int) p->enable, (int) c->enable);
645-
646-
ngx_conf_merge_value(c->enable, p->enable, 0);
647-
ngx_conf_merge_value(c->sanity_checks_enabled, p->sanity_checks_enabled, 0);
648-
ngx_conf_merge_ptr_value(c->transaction_id, p->transaction_id, NULL);
649-
650-
#if defined(MODSECURITY_DDEBUG) && (MODSECURITY_DDEBUG)
651-
dd("PARENT RULES");
652-
msc_rules_dump(p->rules_set);
653-
dd("CHILD RULES");
654-
msc_rules_dump(c->rules_set);
655-
#endif
588+
/* Create our ModSecurity instance */
589+
conf->modsec = msc_init();
590+
if (conf->modsec == NULL)
591+
{
592+
dd("failed to create the ModSecurity instance");
593+
return NGX_CONF_ERROR;
594+
}
656595

657-
rules = msc_rules_merge(c->rules_set, p->rules_set, &error);
596+
/* Provide our connector information to LibModSecurity */
597+
msc_set_connector_info(conf->modsec, MODSECURITY_NGINX_WHOAMI);
598+
msc_set_log_cb(conf->modsec, ngx_http_modsecurity_log);
658599

659-
if (rules < 0) {
660-
return strdup(error);
661-
}
662-
dd(" state - this: '%d'",
663-
(int) c->enable);
664-
#if defined(MODSECURITY_DDEBUG) && (MODSECURITY_DDEBUG)
665-
dd("NEW CHIELD RULES");
666-
msc_rules_dump(c->rules_set);
667-
#endif
668-
return NGX_CONF_OK;
600+
return conf;
669601
}
670602

671603

672604
static char *
673-
ngx_http_modsecurity_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
605+
ngx_http_modsecurity_merge_conf(ngx_conf_t *cf, void *parent, void *child)
674606
{
675607
ngx_http_modsecurity_conf_t *p = parent;
676608
ngx_http_modsecurity_conf_t *c = child;
@@ -704,7 +636,7 @@ ngx_http_modsecurity_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
704636
}
705637

706638
#if defined(MODSECURITY_DDEBUG) && (MODSECURITY_DDEBUG)
707-
dd("NEW CHIELD RULES");
639+
dd("NEW CHILD RULES");
708640
msc_rules_dump(c->rules_set);
709641
#endif
710642
return NGX_CONF_OK;

0 commit comments

Comments
 (0)