Skip to content

Commit 3cf3ba2

Browse files
authored
Merge pull request #2220 from jan-cerny/verbose_option
Fix verbose command line option
2 parents 6e75408 + baf343c commit 3cf3ba2

File tree

9 files changed

+241
-74
lines changed

9 files changed

+241
-74
lines changed

docs/developer/developer.adoc

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,8 +248,6 @@ or possible bugs in their security policies have these possibilities:
248248
The verbose mode provides user additional information about process of system
249249
scanning. The mode is useful for diagnostics of SCAP content evaluation
250250
and also for debugging. It produces a detailed report log with various messages.
251-
The mode is available for `xccdf eval`, `oval eval`, `oval collect`
252-
and `oval analyse` modules.
253251
There is no need to special compilation, the feature is available for all
254252
OpenSCAP users.
255253

tests/utils/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
add_oscap_test("autotailor_integration_test.sh")
22
add_oscap_test("test_utils_args.sh")
3+
add_oscap_test("test_verbose_options.sh")
34

45
if(PY_PYTEST)
56
add_test(
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/usr/bin/env bash
2+
3+
. $builddir/tests/test_common.sh
4+
5+
set -e -o pipefail
6+
7+
xccdf="$srcdir/test_utils_args_xccdf_profile.xml"
8+
stderr=$(mktemp)
9+
results=$(mktemp)
10+
verblofile=$(mktemp)
11+
12+
$OSCAP info --verbose INFO --profiles "$xccdf" >/dev/null 2> "$stderr"
13+
grep -q "I: oscap: Identified document type: Benchmark" "$stderr"
14+
$OSCAP info --profiles --verbose INFO "$xccdf" >/dev/null 2> "$stderr"
15+
grep -q "I: oscap: Identified document type: Benchmark" "$stderr"
16+
$OSCAP xccdf eval --verbose INFO --profile default --results "$results" "$xccdf" >/dev/null 2> "$stderr"
17+
grep -q "I: oscap: Identified document type: Benchmark" "$stderr"
18+
$OSCAP xccdf eval --profile default --verbose INFO --results "$results" "$xccdf" >/dev/null 2> "$stderr"
19+
grep -q "I: oscap: Identified document type: Benchmark" "$stderr"
20+
$OSCAP xccdf eval --profile default --results "$results" --verbose INFO "$xccdf" >/dev/null 2> "$stderr"
21+
grep -q "I: oscap: Identified document type: Benchmark" "$stderr"
22+
$OSCAP xccdf eval --verbose INFO --verbose-log-file "$verblofile" --profile default --results "$results" "$xccdf" >/dev/null 2>/dev/null
23+
grep -q "I: oscap: Identified document type: Benchmark" "$verblofile"
24+
$OSCAP xccdf eval --profile default --verbose INFO --verbose-log-file "$verblofile" --results "$results" "$xccdf" >/dev/null 2>/dev/null
25+
grep -q "I: oscap: Identified document type: Benchmark" "$verblofile"
26+
$OSCAP xccdf eval --profile default --results "$results" --verbose INFO --verbose-log-file "$verblofile" "$xccdf" >/dev/null 2>/dev/null
27+
grep -q "I: oscap: Identified document type: Benchmark" "$verblofile"
28+
$OSCAP xccdf eval --verbose INFO --profile default --results "$results" --verbose-log-file "$verblofile" "$xccdf" >/dev/null 2>/dev/null
29+
grep -q "I: oscap: Identified document type: Benchmark" "$verblofile"
30+
$OSCAP xccdf eval --verbose-log-file "$verblofile" --profile default --verbose INFO --results "$results" "$xccdf" >/dev/null 2>/dev/null
31+
grep -q "I: oscap: Identified document type: Benchmark" "$verblofile"
32+
$OSCAP xccdf eval --verbose INFO --profile default --results "$results" --verbose-log-file "$verblofile" "$xccdf" >/dev/null 2>/dev/null
33+
grep -q "I: oscap: Identified document type: Benchmark" "$verblofile"
34+
35+
rm -f "$results" "$stderr" "$verblofile"

utils/oscap-cpe.c

Lines changed: 48 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,10 @@ static struct oscap_module CPE_MATCH_MODULE = {
6060
.parent = &OSCAP_CPE_MODULE,
6161
.summary = "Match CPE name against provided dictionary",
6262
.usage = "name dictionary.xml",
63-
.help = NULL,
63+
.help = "Options:\n"
64+
" --verbose <verbosity_level> - Turn on verbose mode at specified verbosity level.\n"
65+
" Verbosity level must be one of: DEVEL, INFO, WARNING, ERROR.\n"
66+
" --verbose-log-file <file> - Write verbose information into file.\n",
6467
.opt_parser = getopt_cpe,
6568
.func = app_cpe_match
6669
};
@@ -70,7 +73,10 @@ static struct oscap_module CPE_CHECK_MODULE = {
7073
.parent = &OSCAP_CPE_MODULE,
7174
.summary = "Check if CPE name is valid",
7275
.usage = "name",
73-
.help = NULL,
76+
.help = "Options:\n"
77+
" --verbose <verbosity_level> - Turn on verbose mode at specified verbosity level.\n"
78+
" Verbosity level must be one of: DEVEL, INFO, WARNING, ERROR.\n"
79+
" --verbose-log-file <file> - Write verbose information into file.\n",
7480
.opt_parser = getopt_cpe,
7581
.func = app_cpe_check
7682
};
@@ -80,7 +86,10 @@ static struct oscap_module CPE_VALIDATE = {
8086
.parent = &OSCAP_CPE_MODULE,
8187
.summary = "Validate CPE Dictionary content",
8288
.usage = "cpe-dict.xml",
83-
.help = NULL,
89+
.help = "Options:\n"
90+
" --verbose <verbosity_level> - Turn on verbose mode at specified verbosity level.\n"
91+
" Verbosity level must be one of: DEVEL, INFO, WARNING, ERROR.\n"
92+
" --verbose-log-file <file> - Write verbose information into file.\n",
8493
.opt_parser = getopt_cpe,
8594
.func = app_cpe_validate
8695
};
@@ -93,37 +102,48 @@ static struct oscap_module* CPE_SUBMODULES[CPE_SUBMODULES_NUM] = {
93102
};
94103

95104
bool getopt_cpe(int argc, char **argv, struct oscap_action *action) {
96-
97-
if (action->module == &CPE_MATCH_MODULE) {
98-
if( argc != 5 ) {
99-
oscap_module_usage(action->module, stderr, "Wrong number of parameters.\n");
100-
return false;
105+
enum oscap_cpe_opts {
106+
OSCAP_CPE_OPT_VERBOSE = 1,
107+
OSCAP_CPE_OPT_VERBOSE_LOG_FILE,
108+
};
109+
const struct option long_options[] = {
110+
// options
111+
{"verbose", required_argument, NULL, OSCAP_CPE_OPT_VERBOSE},
112+
{"verbose-log-file", required_argument, NULL, OSCAP_CPE_OPT_VERBOSE_LOG_FILE},
113+
// end
114+
{0, 0, 0, 0}
115+
};
116+
int c;
117+
while ((c = getopt_long(argc, argv, "o:i:", long_options, NULL)) != -1) {
118+
switch (c) {
119+
case OSCAP_CPE_OPT_VERBOSE:
120+
action->verbosity_level = optarg;
121+
break;
122+
case OSCAP_CPE_OPT_VERBOSE_LOG_FILE:
123+
action->f_verbose_log = optarg;
124+
break;
125+
case 0: break;
126+
default: return oscap_module_usage(action->module, stderr, NULL);
101127
}
102-
action->cpe_action = malloc(sizeof(struct cpe_action));
103-
action->cpe_action->name=argv[3];
104-
action->cpe_action->dict=argv[4];
105128
}
106-
129+
if (optind >= argc) {
130+
return oscap_module_usage(action->module, stderr, "Wrong number of arguments!\n");
131+
}
132+
action->cpe_action = malloc(sizeof(struct cpe_action));
133+
if (action->cpe_action == NULL) {
134+
fprintf(stderr, "Memory allocation error.\n");
135+
return false;
136+
}
137+
if (action->module == &CPE_MATCH_MODULE) {
138+
action->cpe_action->name = argv[optind];
139+
action->cpe_action->dict = argv[optind + 1];
140+
}
107141
if (action->module == &CPE_CHECK_MODULE) {
108-
if( argc != 4 ) {
109-
oscap_module_usage(action->module, stderr, "Wrong number of parameters.\n");
110-
return false;
111-
}
112-
action->cpe_action = malloc(sizeof(struct cpe_action));
113-
action->cpe_action->name=argv[3];
142+
action->cpe_action->name = argv[optind];
114143
}
115-
116144
if (action->module == &CPE_VALIDATE) {
117-
if( argc != 4 ) {
118-
oscap_module_usage(action->module, stderr, "Wrong number of parameters.\n");
119-
return false;
120-
}
121-
122-
action->cpe_action = malloc(sizeof(struct cpe_action));
123-
action->cpe_action->dict=argv[3];
145+
action->cpe_action->dict = argv[optind];
124146
}
125-
126-
127147
return true;
128148
}
129149

utils/oscap-ds.c

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,10 @@ static struct oscap_module DS_SDS_VALIDATE_MODULE = {
7373
.parent = &OSCAP_DS_MODULE,
7474
.summary = "Validate given source data stream",
7575
.usage = "source_datastream.xml",
76-
.help = NULL,
76+
.help = "Options:\n"
77+
" --verbose <verbosity_level> - Turn on verbose mode at specified verbosity level.\n"
78+
" Verbosity level must be one of: DEVEL, INFO, WARNING, ERROR.\n"
79+
" --verbose-log-file <file> - Write verbose information into file.\n",
7780
.opt_parser = getopt_ds,
7881
.func = app_ds_sds_validate
7982
};
@@ -83,7 +86,10 @@ static struct oscap_module DS_RDS_VALIDATE_MODULE = {
8386
.parent = &OSCAP_DS_MODULE,
8487
.summary = "Validate given result data stream",
8588
.usage = "[options] result_datastream.xml",
86-
.help = NULL,
89+
.help = "Options:\n"
90+
" --verbose <verbosity_level> - Turn on verbose mode at specified verbosity level.\n"
91+
" Verbosity level must be one of: DEVEL, INFO, WARNING, ERROR.\n"
92+
" --verbose-log-file <file> - Write verbose information into file.\n",
8793
.opt_parser = getopt_ds,
8894
.func = app_ds_rds_validate
8995
};
@@ -98,7 +104,9 @@ enum ds_opt {
98104
DS_OPT_DATASTREAM_ID = 1,
99105
DS_OPT_XCCDF_ID,
100106
DS_OPT_REPORT_ID,
101-
DS_OPT_LOCAL_FILES
107+
DS_OPT_LOCAL_FILES,
108+
DS_OPT_VERBOSE,
109+
DS_OPT_VERBOSE_LOG_FILE,
102110
};
103111

104112
bool getopt_ds(int argc, char **argv, struct oscap_action *action) {
@@ -113,6 +121,8 @@ bool getopt_ds(int argc, char **argv, struct oscap_action *action) {
113121
{"report-id", required_argument, NULL, DS_OPT_REPORT_ID},
114122
{"fetch-remote-resources", no_argument, &action->remote_resources, 1},
115123
{"local-files", required_argument, NULL, DS_OPT_LOCAL_FILES},
124+
{"verbose", required_argument, NULL, DS_OPT_VERBOSE},
125+
{"verbose-log-file", required_argument, NULL, DS_OPT_VERBOSE_LOG_FILE},
116126
// end
117127
{0, 0, 0, 0}
118128
};
@@ -127,6 +137,12 @@ bool getopt_ds(int argc, char **argv, struct oscap_action *action) {
127137
case DS_OPT_LOCAL_FILES:
128138
action->local_files = optarg;
129139
break;
140+
case DS_OPT_VERBOSE:
141+
action->verbosity_level = optarg;
142+
break;
143+
case DS_OPT_VERBOSE_LOG_FILE:
144+
action->f_verbose_log = optarg;
145+
break;
130146
case 0: break;
131147
default: return oscap_module_usage(action->module, stderr, NULL);
132148
}

utils/oscap-info.c

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,10 @@ struct oscap_module OSCAP_INFO_MODULE = {
6666
" --local-files <dir> - Use locally downloaded copies of remote resources stored in the given directory.\n"
6767
" --profile <id> - Show info of the profile with the given ID.\n"
6868
" --profiles - Show profiles from the input file in the <id>:<title> format, one line per profile.\n"
69-
" --references - Show references available in the input benchmark",
69+
" --references - Show references available in the input benchmark\n"
70+
" --verbose <verbosity_level> - Turn on verbose mode at specified verbosity level.\n"
71+
" Verbosity level must be one of: DEVEL, INFO, WARNING, ERROR.\n"
72+
" --verbose-log-file <file> - Write verbose information into file.\n",
7073
.opt_parser = getopt_info,
7174
.func = app_info
7275
};
@@ -761,34 +764,51 @@ bool getopt_info(int argc, char **argv, struct oscap_action *action)
761764
{
762765
assert(action != NULL);
763766

767+
enum oscap_info_opts {
768+
OSCAP_INFO_OPT_REMOTE_RESOURCES,
769+
OSCAP_INFO_OPT_LOCAL_FILES,
770+
OSCAP_INFO_OPT_PROFILE,
771+
OSCAP_INFO_OPT_PROFILES,
772+
OSCAP_INFO_OPT_REFERENCES,
773+
OSCAP_INFO_OPT_VERBOSE,
774+
OSCAP_INFO_OPT_VERBOSE_LOG_FILE,
775+
};
764776
/* Command-options */
765777
const struct option long_options[] = {
766778
{"fetch-remote-resources", no_argument, &action->remote_resources, 1},
767-
{"local-files", required_argument, NULL, 'l'},
768-
{"profile", required_argument, 0, 'p'},
769-
{"profiles", no_argument, 0, 'n'},
770-
{"references", no_argument, 0, 'r'},
779+
{"local-files", required_argument, NULL, OSCAP_INFO_OPT_LOCAL_FILES},
780+
{"profile", required_argument, 0, OSCAP_INFO_OPT_PROFILE},
781+
{"profiles", no_argument, 0, OSCAP_INFO_OPT_PROFILES},
782+
{"references", no_argument, 0, OSCAP_INFO_OPT_REFERENCES},
783+
{"verbose", required_argument, NULL, OSCAP_INFO_OPT_VERBOSE},
784+
{"verbose-log-file", required_argument, NULL, OSCAP_INFO_OPT_VERBOSE_LOG_FILE},
771785
// end
772786
{0, 0, 0, 0}
773787
};
774788

775789
int c;
776-
while ((c = getopt_long(argc, argv, "o:i:p:", long_options, NULL)) != -1) {
790+
while ((c = getopt_long(argc, argv, "", long_options, NULL)) != -1) {
777791
switch(c) {
778792
case 0: break;
779-
case 'p':
793+
case OSCAP_INFO_OPT_PROFILE:
780794
action->profile = optarg;
781795
break;
782-
case 'n':
796+
case OSCAP_INFO_OPT_PROFILES:
783797
action->show_profiles_only = 1;
784798
action->provide_machine_readable_output = 1;
785799
break;
786-
case 'l':
800+
case OSCAP_INFO_OPT_LOCAL_FILES:
787801
action->local_files = optarg;
788802
break;
789-
case 'r':
803+
case OSCAP_INFO_OPT_REFERENCES:
790804
action->references = 1;
791805
break;
806+
case OSCAP_INFO_OPT_VERBOSE:
807+
action->verbosity_level = optarg;
808+
break;
809+
case OSCAP_INFO_OPT_VERBOSE_LOG_FILE:
810+
action->f_verbose_log = optarg;
811+
break;
792812
default: return oscap_module_usage(action->module, stderr, NULL);
793813
}
794814
}

0 commit comments

Comments
 (0)