File: oscap-info.c

package info (click to toggle)
openscap 1.0.9-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 129,588 kB
  • ctags: 26,325
  • sloc: xml: 611,156; ansic: 90,367; sh: 26,693; makefile: 2,463; python: 804; perl: 445; cpp: 123
file content (418 lines) | stat: -rw-r--r-- 14,945 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
/*
 * Copyright 2010--2013 Red Hat Inc., Durham, North Carolina.
 * All Rights Reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Authors:
 *      Peter Vrabec  <pvrabec@redhat.com>
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

/* Standard header files */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
#include <assert.h>
#include <math.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <linux/limits.h>

#include <oscap.h>
#include <xccdf_policy.h>
#include <oval_results.h>
#include <oval_variables.h>
#include <oval_system_characteristics.h>
#include <oval_directives.h>
#include <scap_ds.h>

#include "oscap-tool.h"

static bool getopt_info(int argc, char **argv, struct oscap_action *action);
static int app_info(const struct oscap_action *action);

struct oscap_module OSCAP_INFO_MODULE = {
    .name = "info",
    .parent = &OSCAP_ROOT_MODULE,
    .summary = "info module",
    .usage = "some-file.xml",
    .help = "Print information about a file",
    .opt_parser = getopt_info,
    .func = app_info
};

static void print_time(const char *file) {
	struct stat buffer;
	char timeStr[ 100 ] = "";

	if(!stat(file, &buffer)) {
		strftime(timeStr, 100, "%Y-%m-%dT%H:%M:%S", localtime(&buffer.st_mtime));
		printf("Imported: %s\n", timeStr);
	}
}


static int app_info(const struct oscap_action *action)
{
	oscap_document_type_t doc_type;
        int result = OSCAP_ERROR;

	if(oscap_determine_document_type(action->file, &doc_type))
		goto cleanup;

	switch (doc_type) {
	case OSCAP_DOCUMENT_OVAL_DEFINITIONS: {
		printf("Document type: OVAL Definitions\n");
		struct oval_definition_model * def_model = oval_definition_model_import(action->file);
		if(!def_model)
			goto cleanup;
		struct oval_generator *gen = oval_definition_model_get_generator(def_model);
		printf("OVAL version: %s\n", oval_generator_get_schema_version(gen));
		printf("Generated: %s\n", oval_generator_get_timestamp(gen));
		print_time(action->file);
		oval_definition_model_free(def_model);
	}
	break;
	case OSCAP_DOCUMENT_OVAL_VARIABLES: {
		printf("Document type: OVAL Variables\n");
		struct oval_variable_model * var_model = oval_variable_model_import(action->file);
		if(!var_model)
			goto cleanup;
		struct oval_generator *gen = oval_variable_model_get_generator(var_model);
		printf("OVAL version: %s\n", oval_generator_get_schema_version(gen));
		printf("Generated: %s\n", oval_generator_get_timestamp(gen));
		print_time(action->file);
		oval_variable_model_free(var_model);
	}
	break;
	case OSCAP_DOCUMENT_OVAL_DIRECTIVES: {
		printf("Document type: OVAL Directives\n");
		struct oval_directives_model *dir_model = oval_directives_model_new();
		int ret = oval_directives_model_import(dir_model, action->file);
		if(ret)
			goto cleanup;
		struct oval_generator *gen = oval_directives_model_get_generator(dir_model);
		printf("OVAL version: %s\n", oval_generator_get_schema_version(gen));
		printf("Generated: %s\n", oval_generator_get_timestamp(gen));
		print_time(action->file);
		oval_directives_model_free(dir_model);
	}
	break;
	case OSCAP_DOCUMENT_OVAL_SYSCHAR: {
		printf("Document type: OVAL System Characteristics\n");
		struct oval_definition_model * def_model = oval_definition_model_new();
		struct oval_syschar_model * sys_model = oval_syschar_model_new(def_model);
		int ret = oval_syschar_model_import(sys_model, action->file);
		if(ret)
			goto cleanup;
		struct oval_generator *gen = oval_syschar_model_get_generator(sys_model);
		printf("OVAL version: %s\n", oval_generator_get_schema_version(gen));
		printf("Generated: %s\n", oval_generator_get_timestamp(gen));
		print_time(action->file);
		oval_syschar_model_free(sys_model);
		oval_definition_model_free(def_model);
	}
	break;
	case OSCAP_DOCUMENT_OVAL_RESULTS: {
		printf("Document type: OVAL Results\n");
		struct oval_definition_model * def_model=oval_definition_model_new();
		struct oval_results_model * res_model = oval_results_model_new(def_model,NULL);
		int ret = oval_results_model_import(res_model, action->file);
		if(ret)
			goto cleanup;
		struct oval_generator *gen = oval_results_model_get_generator(res_model);
		printf("OVAL version: %s\n", oval_generator_get_schema_version(gen));
		printf("Generated: %s\n", oval_generator_get_timestamp(gen));
		print_time(action->file);
		oval_results_model_free(res_model);
		oval_definition_model_free(def_model);
	}
	break;
	case OSCAP_DOCUMENT_XCCDF: {
		printf("Document type: XCCDF Checklist\n");
		struct xccdf_benchmark* bench = xccdf_benchmark_import(action->file);
		if(!bench)
			goto cleanup;
		char * doc_version =  xccdf_detect_version(action->file);
		printf("Checklist version: %s\n", doc_version);
		/* get current status */
		struct xccdf_status * status = NULL;
		status = xccdf_benchmark_get_status_current(bench);
		if (status) {
			printf("Status: %s\n", xccdf_status_type_to_text(xccdf_status_get_status(status)));
			const time_t date_time = xccdf_status_get_date(status);
			if (date_time != 0) {
				struct tm *date = localtime(&date_time);
				char date_str[] = "YYYY-DD-MM";
				snprintf(date_str, sizeof(date_str), "%04d-%02d-%02d", date->tm_year + 1900, date->tm_mon + 1, date->tm_mday);
				printf("Generated: %s\n", date_str);
			}
		}
		print_time(action->file);
		printf("Resolved: %s\n", xccdf_benchmark_get_resolved(bench) ? "true" : "false");

		struct xccdf_profile_iterator * prof_it = xccdf_benchmark_get_profiles(bench);
		printf("Profiles:\n");
		struct xccdf_profile * prof = NULL;
		while (xccdf_profile_iterator_has_more(prof_it)) {
			prof = xccdf_profile_iterator_next(prof_it);
			printf("\t%s\n", xccdf_profile_get_id(prof));
		}
		xccdf_profile_iterator_free(prof_it);

		struct xccdf_policy_model *policy_model = xccdf_policy_model_new(bench);
		struct oscap_file_entry_list *referenced_files = xccdf_policy_model_get_systems_and_files(policy_model);
		struct oscap_file_entry_iterator *files_it = oscap_file_entry_list_get_files(referenced_files);
		printf("Referenced check files:\n");
		while (oscap_file_entry_iterator_has_more(files_it)) {
			struct oscap_file_entry *file_entry;

			file_entry = (struct oscap_file_entry *) oscap_file_entry_iterator_next(files_it);

			printf("\t%s\n", oscap_file_entry_get_file(file_entry));
			printf("\t\tsystem: %s\n", oscap_file_entry_get_system(file_entry));
		}
		oscap_file_entry_iterator_free(files_it);
		oscap_file_entry_list_free(referenced_files);

		struct xccdf_result_iterator * res_it = xccdf_benchmark_get_results(bench);
		if (xccdf_result_iterator_has_more(res_it))
			printf("Test Results:\n");
		struct xccdf_result * test_result = NULL;
		while (xccdf_result_iterator_has_more(res_it)) {
			test_result = xccdf_result_iterator_next(res_it);
			printf("\t%s\n", xccdf_result_get_id(test_result));
		}
		xccdf_result_iterator_free(res_it);

		free(doc_version);

		xccdf_policy_model_free(policy_model);
		// already freed by policy!
		//xccdf_benchmark_free(bench);
	}
	break;
	case OSCAP_DOCUMENT_CPE_LANGUAGE: {
		printf("Document type: CPE Language\n");
		print_time(action->file);
	}
	break;
	case OSCAP_DOCUMENT_CPE_DICTIONARY: {
		printf("Document type: CPE Dictionary\n");
		struct cpe_dict_model *dict_model = cpe_dict_model_import(action->file);
		if (!dict_model)
			goto cleanup;
		struct cpe_generator *gen = cpe_dict_model_get_generator(dict_model);
		if (gen != NULL) {
			printf("CPE version: %s\n", cpe_generator_get_schema_version(gen));
			printf("Generated: %s\n", cpe_generator_get_timestamp(gen));
		}
		print_time(action->file);
		cpe_dict_model_free(dict_model);
	}
	break;
	case OSCAP_DOCUMENT_SDS: {
		printf("Document type: Source Data Stream\n");
		print_time(action->file);
		/* get collection */
		struct ds_sds_index *sds = ds_sds_index_import(action->file);
		if (!sds)
			goto cleanup;
		/* iterate over streams */
		struct ds_stream_index_iterator* sds_it = ds_sds_index_get_streams(sds);
		while (ds_stream_index_iterator_has_more(sds_it)) {
			struct ds_stream_index * stream = ds_stream_index_iterator_next(sds_it);

			printf("\nStream: %s\n", ds_stream_index_get_id(stream));
			printf("Generated: %s\n", ds_stream_index_get_timestamp(stream));
			printf("Version: %s\n", ds_stream_index_get_version(stream));

			printf("Checklists:\n");
			struct oscap_string_iterator* checklist_it = ds_stream_index_get_checklists(stream);
			while (oscap_string_iterator_has_more(checklist_it)) {
				const char * id = oscap_string_iterator_next(checklist_it);
				printf("\tRef-Id: %s\n", id);

				char * temp_dir = NULL;
				char * xccdf_file = NULL;

		                temp_dir = oscap_acquire_temp_dir_bundled();
		                if (temp_dir == NULL) {
					ds_sds_index_free(sds);
					oscap_string_iterator_free(checklist_it);
					ds_stream_index_iterator_free(sds_it);
					goto cleanup;
				}

				/* decompose */
				ds_sds_decompose(action->file, ds_stream_index_get_id(stream), id, temp_dir, "xccdf.xml");

				/* import xccdf */
		                xccdf_file = malloc(PATH_MAX * sizeof(char));
				snprintf(xccdf_file, PATH_MAX, "%s/%s", temp_dir, "xccdf.xml");
				struct xccdf_benchmark* bench = NULL;
		                bench = xccdf_benchmark_import(xccdf_file);
				free(xccdf_file);
				if(!bench) {
					ds_sds_index_free(sds);
					oscap_string_iterator_free(checklist_it);
					oscap_acquire_cleanup_dir_bundled(&temp_dir);
					ds_stream_index_iterator_free(sds_it);
					goto cleanup;
				}

				/* print profiles */
				struct xccdf_profile_iterator * prof_it = xccdf_benchmark_get_profiles(bench);
				printf("\t\tProfiles:\n");
				while (xccdf_profile_iterator_has_more(prof_it)) {
					struct xccdf_profile * prof = xccdf_profile_iterator_next(prof_it);
					printf("\t\t\t%s\n", xccdf_profile_get_id(prof));
				}
				xccdf_profile_iterator_free(prof_it);

				struct xccdf_policy_model *policy_model = xccdf_policy_model_new(bench);
				struct oscap_file_entry_list *referenced_files = xccdf_policy_model_get_systems_and_files(policy_model);
				struct oscap_file_entry_iterator *files_it = oscap_file_entry_list_get_files(referenced_files);
				printf("\t\tReferenced check files:\n");
				while (oscap_file_entry_iterator_has_more(files_it)) {
					struct oscap_file_entry *file_entry;

					file_entry = (struct oscap_file_entry *) oscap_file_entry_iterator_next(files_it);

					printf("\t\t\t%s\n", oscap_file_entry_get_file(file_entry));
					printf("\t\t\t\tsystem: %s\n", oscap_file_entry_get_system(file_entry));
				}
				oscap_file_entry_iterator_free(files_it);
				oscap_file_entry_list_free(referenced_files);

				struct xccdf_result_iterator * res_it = xccdf_benchmark_get_results(bench);
				if (xccdf_result_iterator_has_more(res_it))
					printf("\t\tTest Results:\n");
				struct xccdf_result * test_result = NULL;
				while (xccdf_result_iterator_has_more(res_it)) {
					test_result = xccdf_result_iterator_next(res_it);
					printf("\t\t\t%s\n", xccdf_result_get_id(test_result));
				}
				xccdf_result_iterator_free(res_it);

				xccdf_policy_model_free(policy_model);
				// already freed by policy!
				//xccdf_benchmark_free(bench);

				oscap_acquire_cleanup_dir_bundled(&temp_dir);
				if (oscap_err()) {
					/* This might have set error, when some of the removals failed.
					   No need to abort this operation, we can safely procceed. */
					fprintf(stderr, "%s %s\n", OSCAP_ERR_MSG, oscap_err_desc());
					oscap_clearerr();
				}
			}
			oscap_string_iterator_free(checklist_it);

			printf("Checks:\n");
			struct oscap_string_iterator* checks_it = ds_stream_index_get_checks(stream);
			while (oscap_string_iterator_has_more(checks_it)) {
				const char * id = oscap_string_iterator_next(checks_it);
				printf("\tRef-Id: %s\n", id);
			}
			oscap_string_iterator_free(checks_it);

			struct oscap_string_iterator* dict_it = ds_stream_index_get_dictionaries(stream);
			if (oscap_string_iterator_has_more(dict_it))
				printf("Dictionaries:\n");
			else
				printf("No dictionaries.\n");
			while (oscap_string_iterator_has_more(dict_it)) {
				const char * id = oscap_string_iterator_next(dict_it);
				printf("\tRef-Id: %s\n", id);
			}
			oscap_string_iterator_free(dict_it);
		}
		ds_stream_index_iterator_free(sds_it);
		ds_sds_index_free(sds);
	}
	break;
	case OSCAP_DOCUMENT_ARF: {
		printf("Document type: Result Data Stream\n");
		struct rds_index *rds = rds_index_import(action->file);
		if (!rds)
			goto cleanup;

		struct rds_asset_index_iterator* asset_it = rds_index_get_assets(rds);
		while (rds_asset_index_iterator_has_more(asset_it)) {
			struct rds_asset_index* asset = rds_asset_index_iterator_next(asset_it);
			printf("\nAsset: %s\n", rds_asset_index_get_id(asset));

			struct rds_report_index_iterator* report_it = rds_asset_index_get_reports(asset);
			while (rds_report_index_iterator_has_more(report_it)) {
				struct rds_report_index* report = rds_report_index_iterator_next(report_it);
				struct rds_report_request_index* request = rds_report_index_get_request(report);

				printf(" - %s -> %s\n",
					rds_report_request_index_get_id(request),
					rds_report_index_get_id(report));
			}
			rds_report_index_iterator_free(report_it);
		}
		rds_asset_index_iterator_free(asset_it);
		rds_index_free(rds);
	}
	break;
	case OSCAP_DOCUMENT_XCCDF_TAILORING:
		printf("Document type: XCCDF Tailoring\n");

		// XCCDF Tailoring is not trivial to parse and interpret without
		// knowing which content file it is supposed to be used with.
		//
		// Since we don't have this info when `oscap info` is called, we
		// can't list profiles the tailoring file adds.
	break;
	case OSCAP_DOCUMENT_CVE_FEED:
		printf("Document type: CVE Feed\n");
		// TODO: Provide more info about CVE feeds
	break;
	case OSCAP_DOCUMENT_SCE_RESULT:
		printf("Document type: SCE Result File\n");
		// Currently, we do not have any SCE result file parsing capabilities.
	break;
	default:
		printf("Document type not handled yet\n");
		break;
	}

	result=OSCAP_OK;

cleanup:
	oscap_print_error();

	return result;
}

bool getopt_info(int argc, char **argv, struct oscap_action *action)
{
	if(  argc != 3) {
		oscap_module_usage(action->module, stderr, "Wrong number of parameteres.\n");
		return false;
	}
	action->file = argv[2];

	return true;
}