2525#include <ngx_http.h>
2626
2727static ngx_int_t ngx_http_modsecurity_init (ngx_conf_t * cf );
28+ static void * ngx_http_modsecurity_create_main_conf (ngx_conf_t * cf );
2829static char * ngx_http_modsecurity_init_main_conf (ngx_conf_t * cf , void * conf );
2930static void * ngx_http_modsecurity_create_conf (ngx_conf_t * cf );
3031static char * ngx_http_modsecurity_merge_conf (ngx_conf_t * cf , void * parent , void * child );
31- static void ngx_http_modsecurity_config_cleanup (void * data );
32+ static void ngx_http_modsecurity_cleanup_instance (void * data );
33+ static void ngx_http_modsecurity_cleanup_rules (void * data );
3234
3335
3436/*
@@ -232,10 +234,11 @@ ngx_http_modsecurity_cleanup(void *data)
232234ngx_inline ngx_http_modsecurity_ctx_t *
233235ngx_http_modsecurity_create_ctx (ngx_http_request_t * r )
234236{
235- ngx_str_t s ;
236- ngx_pool_cleanup_t * cln ;
237- ngx_http_modsecurity_ctx_t * ctx ;
238- ngx_http_modsecurity_conf_t * mcf ;
237+ ngx_str_t s ;
238+ ngx_pool_cleanup_t * cln ;
239+ ngx_http_modsecurity_ctx_t * ctx ;
240+ ngx_http_modsecurity_conf_t * mlcf ;
241+ ngx_http_modsecurity_main_conf_t * mmcf ;
239242
240243 ctx = ngx_pcalloc (r -> pool , sizeof (ngx_http_modsecurity_ctx_t ));
241244 if (ctx == NULL )
@@ -244,18 +247,19 @@ ngx_http_modsecurity_create_ctx(ngx_http_request_t *r)
244247 return NULL ;
245248 }
246249
247- mcf = ngx_http_get_module_loc_conf (r , ngx_http_modsecurity_module );
250+ mmcf = ngx_http_get_module_main_conf (r , ngx_http_modsecurity_module );
251+ mlcf = ngx_http_get_module_loc_conf (r , ngx_http_modsecurity_module );
248252
249- dd ("creating transaction with the following rules: '%p' -- ms: '%p'" , mcf -> rules_set , mcf -> modsec );
253+ dd ("creating transaction with the following rules: '%p' -- ms: '%p'" , mlcf -> rules_set , mmcf -> modsec );
250254
251- if (mcf -> transaction_id ) {
252- if (ngx_http_complex_value (r , mcf -> transaction_id , & s ) != NGX_OK ) {
255+ if (mlcf -> transaction_id ) {
256+ if (ngx_http_complex_value (r , mlcf -> transaction_id , & s ) != NGX_OK ) {
253257 return NGX_CONF_ERROR ;
254258 }
255- ctx -> modsec_transaction = msc_new_transaction_with_id (mcf -> modsec , mcf -> rules_set , (char * ) s .data , r -> connection -> log );
259+ ctx -> modsec_transaction = msc_new_transaction_with_id (mmcf -> modsec , mlcf -> rules_set , (char * ) s .data , r -> connection -> log );
256260
257261 } else {
258- ctx -> modsec_transaction = msc_new_transaction (mcf -> modsec , mcf -> rules_set , r -> connection -> log );
262+ ctx -> modsec_transaction = msc_new_transaction (mmcf -> modsec , mlcf -> rules_set , r -> connection -> log );
259263 }
260264
261265 dd ("transaction created" );
@@ -437,7 +441,7 @@ static ngx_http_module_t ngx_http_modsecurity_ctx = {
437441 NULL , /* preconfiguration */
438442 ngx_http_modsecurity_init , /* postconfiguration */
439443
440- NULL , /* create main configuration */
444+ ngx_http_modsecurity_create_main_conf , /* create main configuration */
441445 ngx_http_modsecurity_init_main_conf , /* init main configuration */
442446
443447 NULL , /* create server configuration */
@@ -541,6 +545,55 @@ ngx_http_modsecurity_init(ngx_conf_t *cf)
541545}
542546
543547
548+ static void *
549+ ngx_http_modsecurity_create_main_conf (ngx_conf_t * cf )
550+ {
551+ ngx_pool_cleanup_t * cln ;
552+ ngx_http_modsecurity_main_conf_t * conf ;
553+
554+ conf = (ngx_http_modsecurity_main_conf_t * ) ngx_pcalloc (cf -> pool ,
555+ sizeof (ngx_http_modsecurity_main_conf_t ));
556+
557+ if (conf == NULL )
558+ {
559+ return NGX_CONF_ERROR ;
560+ }
561+
562+ /*
563+ * set by ngx_pcalloc():
564+ *
565+ * conf->modsec = NULL;
566+ * conf->pool = NULL;
567+ */
568+
569+ cln = ngx_pool_cleanup_add (cf -> pool , 0 );
570+ if (cln == NULL ) {
571+ return NGX_CONF_ERROR ;
572+ }
573+
574+ cln -> handler = ngx_http_modsecurity_cleanup_instance ;
575+ cln -> data = conf ;
576+
577+ conf -> pool = cf -> pool ;
578+
579+ /* Create our ModSecurity instance */
580+ conf -> modsec = msc_init ();
581+ if (conf -> modsec == NULL )
582+ {
583+ dd ("failed to create the ModSecurity instance" );
584+ return NGX_CONF_ERROR ;
585+ }
586+
587+ /* Provide our connector information to LibModSecurity */
588+ msc_set_connector_info (conf -> modsec , MODSECURITY_NGINX_WHOAMI );
589+ msc_set_log_cb (conf -> modsec , ngx_http_modsecurity_log );
590+
591+ dd ("main conf created at: '%p', instance is: '%p'" , conf , conf -> modsec );
592+
593+ return conf ;
594+ }
595+
596+
544597static char *
545598ngx_http_modsecurity_init_main_conf (ngx_conf_t * cf , void * conf )
546599{
@@ -568,7 +621,6 @@ ngx_http_modsecurity_create_conf(ngx_conf_t *cf)
568621 /*
569622 * set by ngx_pcalloc():
570623 *
571- * conf->modsec = NULL;
572624 * conf->enable = 0;
573625 * conf->sanity_checks_enabled = 0;
574626 * conf->rules_set = NULL;
@@ -577,34 +629,24 @@ ngx_http_modsecurity_create_conf(ngx_conf_t *cf)
577629 */
578630
579631 conf -> enable = NGX_CONF_UNSET ;
580- conf -> sanity_checks_enabled = NGX_CONF_UNSET ;
581632 conf -> rules_set = msc_create_rules_set ();
582633 conf -> pool = cf -> pool ;
583634 conf -> transaction_id = NGX_CONF_UNSET_PTR ;
635+ #if defined(MODSECURITY_SANITY_CHECKS ) && (MODSECURITY_SANITY_CHECKS )
636+ conf -> sanity_checks_enabled = NGX_CONF_UNSET ;
637+ #endif
584638
585639 cln = ngx_pool_cleanup_add (cf -> pool , 0 );
586640 if (cln == NULL ) {
587641 dd ("failed to create the ModSecurity configuration cleanup" );
588642 return NGX_CONF_ERROR ;
589643 }
590644
591- cln -> handler = ngx_http_modsecurity_config_cleanup ;
645+ cln -> handler = ngx_http_modsecurity_cleanup_rules ;
592646 cln -> data = conf ;
593647
594648 dd ("conf created at: '%p'" , conf );
595649
596- /* Create our ModSecurity instance */
597- conf -> modsec = msc_init ();
598- if (conf -> modsec == NULL )
599- {
600- dd ("failed to create the ModSecurity instance" );
601- return NGX_CONF_ERROR ;
602- }
603-
604- /* Provide our connector information to LibModSecurity */
605- msc_set_connector_info (conf -> modsec , MODSECURITY_NGINX_WHOAMI );
606- msc_set_log_cb (conf -> modsec , ngx_http_modsecurity_log );
607-
608650 return conf ;
609651}
610652
@@ -628,8 +670,10 @@ ngx_http_modsecurity_merge_conf(ngx_conf_t *cf, void *parent, void *child)
628670 (int ) c -> enable , (int ) p -> enable );
629671
630672 ngx_conf_merge_value (c -> enable , p -> enable , 0 );
631- ngx_conf_merge_value (c -> sanity_checks_enabled , p -> sanity_checks_enabled , 0 );
632673 ngx_conf_merge_ptr_value (c -> transaction_id , p -> transaction_id , NULL );
674+ #if defined(MODSECURITY_SANITY_CHECKS ) && (MODSECURITY_SANITY_CHECKS )
675+ ngx_conf_merge_value (c -> sanity_checks_enabled , p -> sanity_checks_enabled , 0 );
676+ #endif
633677
634678#if defined(MODSECURITY_DDEBUG ) && (MODSECURITY_DDEBUG )
635679 dd ("PARENT RULES" );
@@ -652,20 +696,38 @@ ngx_http_modsecurity_merge_conf(ngx_conf_t *cf, void *parent, void *child)
652696
653697
654698static void
655- ngx_http_modsecurity_config_cleanup (void * data )
699+ ngx_http_modsecurity_cleanup_instance (void * data )
656700{
657- ngx_pool_t * old_pool ;
658- ngx_http_modsecurity_conf_t * t = (ngx_http_modsecurity_conf_t * ) data ;
701+ ngx_pool_t * old_pool ;
702+ ngx_http_modsecurity_main_conf_t * conf ;
703+
704+ conf = (ngx_http_modsecurity_main_conf_t * ) data ;
705+
706+ dd ("deleting a main conf -- instance is: \"%p\"" , conf -> modsec );
707+
708+ old_pool = ngx_http_modsecurity_pcre_malloc_init (conf -> pool );
709+ msc_cleanup (conf -> modsec );
710+ ngx_http_modsecurity_pcre_malloc_done (old_pool );
711+
712+ conf -> modsec = NULL ;
713+ }
714+
715+
716+ static void
717+ ngx_http_modsecurity_cleanup_rules (void * data )
718+ {
719+ ngx_pool_t * old_pool ;
720+ ngx_http_modsecurity_conf_t * conf ;
721+
722+ conf = (ngx_http_modsecurity_conf_t * ) data ;
659723
660- dd ("deleting a loc conf -- RuleSet is: \"%p\"" , t -> rules_set );
724+ dd ("deleting a loc conf -- RuleSet is: \"%p\"" , conf -> rules_set );
661725
662- old_pool = ngx_http_modsecurity_pcre_malloc_init (t -> pool );
663- msc_rules_cleanup (t -> rules_set );
664- msc_cleanup (t -> modsec );
726+ old_pool = ngx_http_modsecurity_pcre_malloc_init (conf -> pool );
727+ msc_rules_cleanup (conf -> rules_set );
665728 ngx_http_modsecurity_pcre_malloc_done (old_pool );
666729
667- t -> rules_set = NULL ;
668- t -> modsec = NULL ;
730+ conf -> rules_set = NULL ;
669731}
670732
671733
0 commit comments