Skip to content

Commit a2eafcc

Browse files
author
nono303
committed
https://github.com/SpiderLabs/modsec-sdbm-util/pull/20
fix SpiderLabs#20 (comment) add p2(" - ModSecurity variables:\n");
1 parent 99d631e commit a2eafcc

File tree

1 file changed

+26
-15
lines changed

1 file changed

+26
-15
lines changed

modsec-sdbm-util.c

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
* Copyright (c) 2004-2013 Trustwave Holdings, Inc. (http://www.trustwave.com/)
44
*
55
* You may not use this file except in compliance with
6-
* the License.  You may obtain a copy of the License at
6+
* the License.  You may obtain a copy of the License at
77
*
8-
*     http://www.apache.org/licenses/LICENSE-2.0
8+
*     http://www.apache.org/licenses/LICENSE-2.0
99
*
1010
* If any of the files related to licensing are missing or if you have any
1111
* other questions related to licensing please contact Trustwave Holdings, Inc.
@@ -115,6 +115,7 @@ int
115115
#define p printf
116116
#define v if (verbose) printf
117117
#endif
118+
#define p2 if (!quiet) printf
118119

119120
#define IS_EXPIRED 128
120121

@@ -126,7 +127,7 @@ int
126127
#define EXTRACT 64
127128

128129

129-
int verbose = 0;
130+
int verbose = 0, quiet = 0;
130131
static char progress_feedback[] = {'|', '/', '-', '\\'};
131132

132133

@@ -251,7 +252,7 @@ int modsec_unpack(apr_pool_t *pool, const unsigned char *blob,
251252

252253
void print_modsec_variables(apr_pool_t *pool, const unsigned char *blob, unsigned int blob_size)
253254
{
254-
p(" - ModSecurity variables:\n");
255+
p2(" - ModSecurity variables:\n");
255256
modsec_unpack(pool, blob, blob_size, PRINT);
256257
}
257258

@@ -306,6 +307,12 @@ static int dump_database(apr_pool_t *pool, apr_sdbm_t *db, int action, char *new
306307
char *file_name = "/new_db";
307308
int full_len = 1 + strlen(new_db_path) + strlen(file_name);
308309
char *full_path = (char *) malloc(full_len);
310+
if (!full_path) {
311+
v("Cannot allocate memory.\n");
312+
fret = -1;
313+
goto end;
314+
}
315+
309316
strcpy(full_path, new_db_path);
310317
strcat(full_path, file_name);
311318
full_path[full_len] = '\0';
@@ -418,13 +425,13 @@ static int dump_database(apr_pool_t *pool, apr_sdbm_t *db, int action, char *new
418425
}
419426
if (action & SHRINK || action & STATUS)
420427
{
421-
printf("\n");
422-
printf("Total of %.0f elements processed.\n", elements);
423-
printf("%d elements removed.\n", removed);
424-
printf("Expired elements: %d, inconsistent items: %d\n", expired_datum,
428+
p2("\n");
429+
p2("Total of %.0f elements processed.\n", elements);
430+
p2("%d elements removed.\n", removed);
431+
p2("Expired elements: %d, inconsistent items: %d\n", expired_datum,
425432
bad_datum);
426433
if (expired_datum+bad_datum != 0 && elements !=0)
427-
printf("Fragmentation rate: %2.2f%% of the database is/was dirty " \
434+
p2("Fragmentation rate: %2.2f%% of the database is/was dirty " \
428435
"data.\n", 100*(expired_datum+bad_datum)/elements);
429436
}
430437

@@ -472,6 +479,7 @@ void help (void) {
472479
p(" -r, remove: Expects to receive a key as a paramter to be removed;\n");
473480
p(" -V. version: Print version information.\n");
474481
p(" -v, verbose: Some extra information about what this utility is doing.\n");
482+
p(" -q, quiet: Only display needed information.\n");
475483
p(" -h, help: this message.\n\n");
476484

477485
}
@@ -491,7 +499,7 @@ int main (int argc, char **argv)
491499
return 0;
492500
}
493501

494-
while ((c = getopt (argc, argv, "nkxsdahVvur:D:")) != -1)
502+
while ((c = getopt (argc, argv, "nkxsdahqVvur:D:")) != -1)
495503
switch (c)
496504
{
497505
case 'd':
@@ -522,6 +530,9 @@ int main (int argc, char **argv)
522530
case 'v':
523531
verbose = 1;
524532
break;
533+
case 'q':
534+
quiet = 1;
535+
break;
525536
case 'V':
526537
version();
527538
return 0;
@@ -560,27 +571,27 @@ int main (int argc, char **argv)
560571
apr_dir_t *db_dest_dir;
561572

562573
// test to see if the target directory exists
563-
printf ("Checking target directory: %s\n", new_db_path);
574+
p2("Checking target directory: %s\n", new_db_path);
564575
ret = apr_dir_open(&db_dest_dir, new_db_path, pool);
565576
if (ret != APR_SUCCESS) {
566577
char errmsg[120];
567578
p("Could not open target directory %s: %s\n", new_db_path, apr_strerror(ret, errmsg, sizeof errmsg));
568579
goto that_is_all_folks;
569580
}
570581
apr_dir_close(db_dest_dir);
571-
printf("Target directory exists.\n");
582+
p2("Target directory exists.\n");
572583

573-
printf ("Opening file: %s\n", file);
584+
p2("Opening file: %s\n", file);
574585
ret = open_sdbm(pool, &db, argv[index]);
575586
if (ret < 0)
576587
{
577588
printf("Failed to open sdbm: %s\n", file);
578589
goto that_is_all_folks;
579590
}
580-
printf("Database ready to be used.\n");
591+
p2("Database ready to be used.\n");
581592

582593
if (to_remove) {
583-
printf("Removing key: %s\n", to_remove);
594+
p2("Removing key: %s\n", to_remove);
584595
remove_key(pool, db, to_remove);
585596
continue;
586597
}

0 commit comments

Comments
 (0)