25
25
#include <ngx_http.h>
26
26
27
27
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 );
28
29
static char * ngx_http_modsecurity_init_main_conf (ngx_conf_t * cf , void * conf );
29
30
static void * ngx_http_modsecurity_create_conf (ngx_conf_t * cf );
30
31
static 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 );
32
34
33
35
34
36
/*
@@ -232,10 +234,11 @@ ngx_http_modsecurity_cleanup(void *data)
232
234
ngx_inline ngx_http_modsecurity_ctx_t *
233
235
ngx_http_modsecurity_create_ctx (ngx_http_request_t * r )
234
236
{
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 ;
239
242
240
243
ctx = ngx_pcalloc (r -> pool , sizeof (ngx_http_modsecurity_ctx_t ));
241
244
if (ctx == NULL )
@@ -244,18 +247,19 @@ ngx_http_modsecurity_create_ctx(ngx_http_request_t *r)
244
247
return NULL ;
245
248
}
246
249
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 );
248
252
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 );
250
254
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 ) {
253
257
return NGX_CONF_ERROR ;
254
258
}
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 );
256
260
257
261
} 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 );
259
263
}
260
264
261
265
dd ("transaction created" );
@@ -437,7 +441,7 @@ static ngx_http_module_t ngx_http_modsecurity_ctx = {
437
441
NULL , /* preconfiguration */
438
442
ngx_http_modsecurity_init , /* postconfiguration */
439
443
440
- NULL , /* create main configuration */
444
+ ngx_http_modsecurity_create_main_conf , /* create main configuration */
441
445
ngx_http_modsecurity_init_main_conf , /* init main configuration */
442
446
443
447
NULL , /* create server configuration */
@@ -541,6 +545,55 @@ ngx_http_modsecurity_init(ngx_conf_t *cf)
541
545
}
542
546
543
547
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
+
544
597
static char *
545
598
ngx_http_modsecurity_init_main_conf (ngx_conf_t * cf , void * conf )
546
599
{
@@ -568,7 +621,6 @@ ngx_http_modsecurity_create_conf(ngx_conf_t *cf)
568
621
/*
569
622
* set by ngx_pcalloc():
570
623
*
571
- * conf->modsec = NULL;
572
624
* conf->enable = 0;
573
625
* conf->sanity_checks_enabled = 0;
574
626
* conf->rules_set = NULL;
@@ -577,34 +629,24 @@ ngx_http_modsecurity_create_conf(ngx_conf_t *cf)
577
629
*/
578
630
579
631
conf -> enable = NGX_CONF_UNSET ;
580
- conf -> sanity_checks_enabled = NGX_CONF_UNSET ;
581
632
conf -> rules_set = msc_create_rules_set ();
582
633
conf -> pool = cf -> pool ;
583
634
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
584
638
585
639
cln = ngx_pool_cleanup_add (cf -> pool , 0 );
586
640
if (cln == NULL ) {
587
641
dd ("failed to create the ModSecurity configuration cleanup" );
588
642
return NGX_CONF_ERROR ;
589
643
}
590
644
591
- cln -> handler = ngx_http_modsecurity_config_cleanup ;
645
+ cln -> handler = ngx_http_modsecurity_cleanup_rules ;
592
646
cln -> data = conf ;
593
647
594
648
dd ("conf created at: '%p'" , conf );
595
649
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
-
608
650
return conf ;
609
651
}
610
652
@@ -628,8 +670,10 @@ ngx_http_modsecurity_merge_conf(ngx_conf_t *cf, void *parent, void *child)
628
670
(int ) c -> enable , (int ) p -> enable );
629
671
630
672
ngx_conf_merge_value (c -> enable , p -> enable , 0 );
631
- ngx_conf_merge_value (c -> sanity_checks_enabled , p -> sanity_checks_enabled , 0 );
632
673
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
633
677
634
678
#if defined(MODSECURITY_DDEBUG ) && (MODSECURITY_DDEBUG )
635
679
dd ("PARENT RULES" );
@@ -652,20 +696,38 @@ ngx_http_modsecurity_merge_conf(ngx_conf_t *cf, void *parent, void *child)
652
696
653
697
654
698
static void
655
- ngx_http_modsecurity_config_cleanup (void * data )
699
+ ngx_http_modsecurity_cleanup_instance (void * data )
656
700
{
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 ;
659
723
660
- dd ("deleting a loc conf -- RuleSet is: \"%p\"" , t -> rules_set );
724
+ dd ("deleting a loc conf -- RuleSet is: \"%p\"" , conf -> rules_set );
661
725
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 );
665
728
ngx_http_modsecurity_pcre_malloc_done (old_pool );
666
729
667
- t -> rules_set = NULL ;
668
- t -> modsec = NULL ;
730
+ conf -> rules_set = NULL ;
669
731
}
670
732
671
733
0 commit comments