|
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); |
29 | 28 | 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); |
32 | 30 | static void ngx_http_modsecurity_config_cleanup(void *data); |
33 | | -static char *ngx_http_modsecurity_init_main_conf(ngx_conf_t *cf, void *conf); |
34 | 31 |
|
35 | 32 |
|
36 | 33 | /* |
@@ -234,31 +231,30 @@ ngx_http_modsecurity_cleanup(void *data) |
234 | 231 | ngx_inline ngx_http_modsecurity_ctx_t * |
235 | 232 | ngx_http_modsecurity_create_ctx(ngx_http_request_t *r) |
236 | 233 | { |
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; |
242 | 238 |
|
243 | 239 | ctx = ngx_pcalloc(r->pool, sizeof(ngx_http_modsecurity_ctx_t)); |
244 | 240 | if (ctx == NULL) |
245 | 241 | { |
246 | 242 | dd("failed to allocate memory for the context."); |
247 | 243 | return NULL; |
248 | 244 | } |
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); |
251 | 245 |
|
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); |
253 | 249 |
|
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) { |
256 | 252 | return NGX_CONF_ERROR; |
257 | 253 | } |
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); |
259 | 255 |
|
260 | 256 | } 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); |
262 | 258 | } |
263 | 259 |
|
264 | 260 | dd("transaction created"); |
@@ -437,32 +433,32 @@ static ngx_command_t ngx_http_modsecurity_commands[] = { |
437 | 433 |
|
438 | 434 |
|
439 | 435 | 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 */ |
442 | 438 |
|
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 */ |
445 | 441 |
|
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 */ |
448 | 444 |
|
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 */ |
451 | 447 | }; |
452 | 448 |
|
453 | 449 |
|
454 | 450 | ngx_module_t ngx_http_modsecurity_module = { |
455 | 451 | 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 */ |
466 | 462 | NGX_MODULE_V1_PADDING |
467 | 463 | }; |
468 | 464 |
|
@@ -545,51 +541,15 @@ ngx_http_modsecurity_init(ngx_conf_t *cf) |
545 | 541 |
|
546 | 542 |
|
547 | 543 | static void * |
548 | | -ngx_http_modsecurity_create_main_conf(ngx_conf_t *cf) |
| 544 | +ngx_http_modsecurity_create_conf(ngx_conf_t *cf) |
549 | 545 | { |
550 | | - ngx_http_modsecurity_conf_t *conf; |
| 546 | + ngx_pool_cleanup_t *cln; |
| 547 | + ngx_http_modsecurity_conf_t *conf; |
551 | 548 |
|
552 | 549 | ngx_log_error(NGX_LOG_NOTICE, cf->log, 0, MODSECURITY_NGINX_WHOAMI); |
553 | 550 |
|
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)); |
593 | 553 |
|
594 | 554 | if (conf == NULL) |
595 | 555 | { |
@@ -619,58 +579,30 @@ static void *ngx_http_modsecurity_create_conf(ngx_conf_t *cf) |
619 | 579 | dd("failed to create the ModSecurity configuration cleanup"); |
620 | 580 | return NGX_CONF_ERROR; |
621 | 581 | } |
| 582 | + |
622 | 583 | cln->handler = ngx_http_modsecurity_config_cleanup; |
623 | 584 | cln->data = conf; |
624 | 585 |
|
625 | | - return conf; |
626 | | -} |
627 | | - |
| 586 | + dd ("conf created at: '%p'", conf); |
628 | 587 |
|
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 | + } |
656 | 595 |
|
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); |
658 | 599 |
|
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; |
669 | 601 | } |
670 | 602 |
|
671 | 603 |
|
672 | 604 | 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) |
674 | 606 | { |
675 | 607 | ngx_http_modsecurity_conf_t *p = parent; |
676 | 608 | 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) |
704 | 636 | } |
705 | 637 |
|
706 | 638 | #if defined(MODSECURITY_DDEBUG) && (MODSECURITY_DDEBUG) |
707 | | - dd("NEW CHIELD RULES"); |
| 639 | + dd("NEW CHILD RULES"); |
708 | 640 | msc_rules_dump(c->rules_set); |
709 | 641 | #endif |
710 | 642 | return NGX_CONF_OK; |
|
0 commit comments