Skip to content

Commit f03c09d

Browse files
committed
move into init_process
1 parent 4e720fd commit f03c09d

File tree

2 files changed

+46
-42
lines changed

2 files changed

+46
-42
lines changed

src/ngx_http_modsecurity_common.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,6 @@ typedef struct {
118118
const char *rules;
119119
const char *rules_set_file;
120120
const char *rules_remote_key, *rules_remote_server;
121-
ngx_flag_t rules_loaded;
122121

123122
ngx_flag_t enable;
124123
#if defined(MODSECURITY_SANITY_CHECKS) && (MODSECURITY_SANITY_CHECKS)

src/ngx_http_modsecurity_module.c

Lines changed: 46 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -251,41 +251,6 @@ ngx_http_modsecurity_cleanup(void *data)
251251
#endif
252252
}
253253

254-
static int lazy_loading_rules(ngx_http_request_t *r) {
255-
int rules;
256-
const char *error;
257-
ngx_http_modsecurity_conf_t *mcf = ngx_http_get_module_loc_conf(r, ngx_http_modsecurity_module);
258-
#define show_loaded_rules(message) \
259-
if (rules >= 0) { \
260-
ngx_log_error(NGX_LOG_NOTICE, r->connection->log, 0, \
261-
"lazy-load %d rules from %s", rules, message); \
262-
} else { \
263-
goto clean; \
264-
}
265-
266-
if (mcf->rules_loaded != NGX_CONF_UNSET) {
267-
return NGX_OK;
268-
}
269-
if (mcf->rules != NGX_CONF_UNSET_PTR) {
270-
rules = msc_rules_add(mcf->rules_set, mcf->rules, &error);
271-
show_loaded_rules(mcf->rules);
272-
}
273-
if (mcf->rules_set_file != NGX_CONF_UNSET_PTR) {
274-
rules = msc_rules_add_file(mcf->rules_set, mcf->rules_set_file, &error);
275-
show_loaded_rules(mcf->rules_set_file);
276-
}
277-
if (mcf->rules_remote_key != NGX_CONF_UNSET_PTR
278-
&& mcf->rules_remote_server != NGX_CONF_UNSET_PTR) {
279-
rules = msc_rules_add_remote(mcf->rules_set,
280-
mcf->rules_remote_key, mcf->rules_remote_server, &error);
281-
show_loaded_rules(mcf->rules_remote_server);
282-
}
283-
mcf->rules_loaded = - NGX_CONF_UNSET;
284-
return NGX_OK;
285-
clean:
286-
ngx_log_error(NGX_ERROR_ERR, r->connection->log, 0, "cannot load rules: %s", error);
287-
return NGX_ERROR;
288-
}
289254

290255
ngx_inline ngx_http_modsecurity_ctx_t *
291256
ngx_http_modsecurity_create_ctx(ngx_http_request_t *r)
@@ -308,10 +273,6 @@ ngx_http_modsecurity_create_ctx(ngx_http_request_t *r)
308273

309274
dd("creating transaction with the following rules: '%p' -- ms: '%p'", mcf->rules_set, mmcf->modsec);
310275

311-
if (lazy_loading_rules(r) != NGX_OK) {
312-
return NGX_CONF_ERROR;
313-
}
314-
315276
if (mcf->transaction_id) {
316277
if (ngx_http_complex_value(r, mcf->transaction_id, &s) != NGX_OK) {
317278
return NGX_CONF_ERROR;
@@ -562,14 +523,59 @@ static ngx_http_module_t ngx_http_modsecurity_ctx = {
562523
};
563524

564525

526+
static int lazy_loading_rules(ngx_http_modsecurity_conf_t *mcf, ngx_log_t *log) {
527+
int rules;
528+
const char *error;
529+
#define show_loaded_rules(message) \
530+
if (rules >= 0) { \
531+
ngx_log_error(NGX_LOG_NOTICE, log, 0, \
532+
"lazy-load %d rules from %s", rules, message); \
533+
} else { \
534+
goto clean; \
535+
}
536+
537+
if (mcf->rules != NGX_CONF_UNSET_PTR) {
538+
rules = msc_rules_add(mcf->rules_set, mcf->rules, &error);
539+
show_loaded_rules(mcf->rules);
540+
}
541+
if (mcf->rules_set_file != NGX_CONF_UNSET_PTR) {
542+
rules = msc_rules_add_file(mcf->rules_set, mcf->rules_set_file, &error);
543+
show_loaded_rules(mcf->rules_set_file);
544+
}
545+
if (mcf->rules_remote_key != NGX_CONF_UNSET_PTR
546+
&& mcf->rules_remote_server != NGX_CONF_UNSET_PTR) {
547+
rules = msc_rules_add_remote(mcf->rules_set,
548+
mcf->rules_remote_key, mcf->rules_remote_server, &error);
549+
show_loaded_rules(mcf->rules_remote_server);
550+
}
551+
return NGX_OK;
552+
clean:
553+
ngx_log_error(NGX_ERROR_ERR, log, 0, "cannot load rules: %s", error);
554+
return NGX_ERROR;
555+
}
556+
557+
558+
static ngx_int_t ngx_http_modsecurity_init_process(ngx_cycle_t *cycle) {
559+
ngx_pool_cleanup_t *cleanup;
560+
for (cleanup = cycle->pool->cleanup; cleanup; cleanup = cleanup->next) {
561+
if (cleanup->handler == ngx_http_modsecurity_cleanup_rules) {
562+
if (lazy_loading_rules(cleanup->data, cycle->log) != NGX_OK) {
563+
return NGX_ERROR;
564+
}
565+
}
566+
}
567+
return NGX_OK;
568+
}
569+
570+
565571
ngx_module_t ngx_http_modsecurity_module = {
566572
NGX_MODULE_V1,
567573
&ngx_http_modsecurity_ctx, /* module context */
568574
ngx_http_modsecurity_commands, /* module directives */
569575
NGX_HTTP_MODULE, /* module type */
570576
NULL, /* init master */
571577
NULL, /* init module */
572-
NULL, /* init process */
578+
ngx_http_modsecurity_init_process, /* init process */
573579
NULL, /* init thread */
574580
NULL, /* exit thread */
575581
NULL, /* exit process */
@@ -753,7 +759,6 @@ ngx_http_modsecurity_create_conf(ngx_conf_t *cf)
753759
conf->rules_set_file = NGX_CONF_UNSET_PTR;
754760
conf->rules_remote_key = NGX_CONF_UNSET_PTR;
755761
conf->rules_remote_server = NGX_CONF_UNSET_PTR;
756-
conf->rules_loaded = NGX_CONF_UNSET;
757762
conf->pool = cf->pool;
758763
conf->transaction_id = NGX_CONF_UNSET_PTR;
759764
#if defined(MODSECURITY_SANITY_CHECKS) && (MODSECURITY_SANITY_CHECKS)

0 commit comments

Comments
 (0)