Skip to content

Commit 2ae2a10

Browse files
committed
Include number of loaded rules in log message at start
1 parent 6b7e59e commit 2ae2a10

File tree

2 files changed

+66
-23
lines changed

2 files changed

+66
-23
lines changed

Diff for: src/ngx_http_modsecurity_common.h

+3
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@ typedef struct {
8989
typedef struct {
9090
void *pool;
9191
ModSecurity *modsec;
92+
ngx_uint_t rules_inline;
93+
ngx_uint_t rules_file;
94+
ngx_uint_t rules_remote;
9295
} ngx_http_modsecurity_main_conf_t;
9396

9497

Diff for: src/ngx_http_modsecurity_module.c

+63-23
Original file line numberDiff line numberDiff line change
@@ -286,13 +286,19 @@ ngx_http_modsecurity_create_ctx(ngx_http_request_t *r)
286286
}
287287

288288

289-
char *ngx_conf_set_rules(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) {
290-
ngx_str_t *value = cf->args->elts;
291-
int res;
292-
const char *error = NULL;
293-
char *rules = ngx_str_to_char(value[1], cf->pool);
294-
ngx_pool_t *old_pool;
295-
ngx_http_modsecurity_conf_t *mcf = conf;
289+
char *
290+
ngx_conf_set_rules(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
291+
{
292+
int res;
293+
char *rules;
294+
ngx_str_t *value;
295+
const char *error;
296+
ngx_pool_t *old_pool;
297+
ngx_http_modsecurity_conf_t *mcf = conf;
298+
ngx_http_modsecurity_main_conf_t *mmcf;
299+
300+
value = cf->args->elts;
301+
rules = ngx_str_to_char(value[1], cf->pool);
296302

297303
if (rules == (char *)-1) {
298304
return NGX_CONF_ERROR;
@@ -301,22 +307,32 @@ char *ngx_conf_set_rules(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) {
301307
old_pool = ngx_http_modsecurity_pcre_malloc_init(cf->pool);
302308
res = msc_rules_add(mcf->rules_set, rules, &error);
303309
ngx_http_modsecurity_pcre_malloc_done(old_pool);
310+
304311
if (res < 0) {
305312
dd("Failed to load the rules: '%s' - reason: '%s'", rules, error);
306313
return strdup(error);
307314
}
308315

316+
mmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_modsecurity_module);
317+
mmcf->rules_inline += res;
318+
309319
return NGX_CONF_OK;
310320
}
311321

312322

313-
char *ngx_conf_set_rules_file(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) {
314-
ngx_str_t *value = cf->args->elts;
315-
int res;
316-
const char *error = NULL;
317-
ngx_pool_t *old_pool;
318-
ngx_http_modsecurity_conf_t *mcf = conf;
319-
char *rules_set = ngx_str_to_char(value[1], cf->pool);
323+
char *
324+
ngx_conf_set_rules_file(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
325+
{
326+
int res;
327+
char *rules_set;
328+
ngx_str_t *value;
329+
const char *error;
330+
ngx_pool_t *old_pool;
331+
ngx_http_modsecurity_conf_t *mcf = conf;
332+
ngx_http_modsecurity_main_conf_t *mmcf;
333+
334+
value = cf->args->elts;
335+
rules_set = ngx_str_to_char(value[1], cf->pool);
320336

321337
if (rules_set == (char *)-1) {
322338
return NGX_CONF_ERROR;
@@ -325,39 +341,54 @@ char *ngx_conf_set_rules_file(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) {
325341
old_pool = ngx_http_modsecurity_pcre_malloc_init(cf->pool);
326342
res = msc_rules_add_file(mcf->rules_set, rules_set, &error);
327343
ngx_http_modsecurity_pcre_malloc_done(old_pool);
344+
328345
if (res < 0) {
329346
dd("Failed to load the rules from: '%s' - reason: '%s'", rules_set, error);
330347
return strdup(error);
331348
}
332349

350+
mmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_modsecurity_module);
351+
mmcf->rules_file += res;
352+
333353
return NGX_CONF_OK;
334354
}
335355

336356

337-
char *ngx_conf_set_rules_remote(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) {
338-
ngx_str_t *value = cf->args->elts;
339-
int res;
340-
const char *error = NULL;
341-
const char *rules_remote_key = ngx_str_to_char(value[1], cf->pool);
342-
const char *rules_remote_server = ngx_str_to_char(value[2], cf->pool);
343-
ngx_pool_t *old_pool;
344-
ngx_http_modsecurity_conf_t *mcf = conf;
357+
char *
358+
ngx_conf_set_rules_remote(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
359+
{
360+
int res;
361+
ngx_str_t *value;
362+
const char *error;
363+
const char *rules_remote_key, *rules_remote_server;
364+
ngx_pool_t *old_pool;
365+
ngx_http_modsecurity_conf_t *mcf = conf;
366+
ngx_http_modsecurity_main_conf_t *mmcf;
367+
368+
value = cf->args->elts;
369+
rules_remote_key = ngx_str_to_char(value[1], cf->pool);
370+
rules_remote_server = ngx_str_to_char(value[2], cf->pool);
345371

346372
if (rules_remote_server == (char *)-1) {
347373
return NGX_CONF_ERROR;
348374
}
375+
349376
if (rules_remote_key == (char *)-1) {
350377
return NGX_CONF_ERROR;
351378
}
352379

353380
old_pool = ngx_http_modsecurity_pcre_malloc_init(cf->pool);
354381
res = msc_rules_add_remote(mcf->rules_set, rules_remote_key, rules_remote_server, &error);
355382
ngx_http_modsecurity_pcre_malloc_done(old_pool);
383+
356384
if (res < 0) {
357385
dd("Failed to load the rules from: '%s' - reason: '%s'", rules_remote_server, error);
358386
return strdup(error);
359387
}
360388

389+
mmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_modsecurity_module);
390+
mmcf->rules_remote += res;
391+
361392
return NGX_CONF_OK;
362393
}
363394

@@ -564,6 +595,9 @@ ngx_http_modsecurity_create_main_conf(ngx_conf_t *cf)
564595
*
565596
* conf->modsec = NULL;
566597
* conf->pool = NULL;
598+
* conf->rules_inline = 0;
599+
* conf->rules_file = 0;
600+
* conf->rules_remote = 0;
567601
*/
568602

569603
cln = ngx_pool_cleanup_add(cf->pool, 0);
@@ -597,7 +631,13 @@ ngx_http_modsecurity_create_main_conf(ngx_conf_t *cf)
597631
static char *
598632
ngx_http_modsecurity_init_main_conf(ngx_conf_t *cf, void *conf)
599633
{
600-
ngx_log_error(NGX_LOG_NOTICE, cf->log, 0, MODSECURITY_NGINX_WHOAMI);
634+
ngx_http_modsecurity_main_conf_t *mmcf;
635+
mmcf = (ngx_http_modsecurity_main_conf_t *) conf;
636+
637+
ngx_log_error(NGX_LOG_NOTICE, cf->log, 0,
638+
"%s (rules loaded inline/local/remote: %ui/%ui/%ui)",
639+
MODSECURITY_NGINX_WHOAMI, mmcf->rules_inline,
640+
mmcf->rules_file, mmcf->rules_remote);
601641

602642
return NGX_CONF_OK;
603643
}

0 commit comments

Comments
 (0)