summaryrefslogtreecommitdiffstats
path: root/sechecker/modules/template/xx.c
blob: 20f7f5f538f396678b673b71d94297f5fd56eab3 (plain)
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
/**
 *  @file
 *  Implementation of the xx module.
 *
 *  @author Jeremy A. Mowery jmowery@tresys.com
 *
 *  Copyright (C) 2005-2007 Tresys Technology, LLC
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program 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 General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

/* NOTE: TODO This is a module template, which includes all the necessary
 * infrastructure to implement a basic SEChecker module. To use this template
 * first replace all instances of the string xx with the name of the module,
 * then edit or complete all sections marked TODO as instructed. */

#include <config.h>

#include "sechecker.h"
#include <apol/policy.h>
#include "xx.h"

#include <stdio.h>
#include <string.h>
#include <errno.h>

/* This string is the name of the module and should match the stem of the file
 * name; it should also match the prefix of all functions defined in this
 * module and the private data storage structure */
static const char *const mod_name = "xx";

/* The register function registers all of a module's functions
 * with the library. TODO: Edit the description fields to include all
 * options, requirements, and dependencies. Also provide a brief summary
 * of the steps performed in this module's checks. If you are adding
 * additional functions you need other modules to call, see the note at
 * the bottom of this function to do so. */
int xx_register(sechk_lib_t * lib)
{
	sechk_module_t *mod = NULL;
	sechk_fn_t *fn_struct = NULL;
	sechk_name_value_t *nv = NULL;

	if (!lib) {
		ERR(NULL, "No library");
		errno = EINVAL;
		return -1;
	}

	/* Modules are declared by the register list file and their name and options
	 * are stored in the module vector of the library. The name is looked up to
	 * determine where to store the function structures */
	mod = sechk_lib_get_module(mod_name, lib);
	if (!mod) {
		ERR(lib->policy, "Module unknown \"%s\"", mod_name);
		errno = ENOENT;
		return -1;
	}

	mod->parent_lib = lib;

	/* TODO: assign the descriptions */
	mod->brief_description = "";
	mod->detailed_description =
		"--------------------------------------------------------------------------------\n"
		"TODO: detailed description for this module.\n";
	mod->opt_description =
		"  Module requirements:\n" "    none\n" "  Module dependencies:\n" "    none\n" "  Module options:\n" "    none\n";
	mod->severity = "TODO: set proper severity";

	/* TODO: assign default options (remove if none)
	 * fill name and value and repeat as needed */
	nv = sechk_name_value_new("", "");
	apol_vector_append(mod->options, (void *)nv);

	/* TODO: assign requirements (remove if none)
	 * fill name and value and repeat as needed */
	nv = sechk_name_value_new("", "");
	apol_vector_append(mod->requirements, (void *)nv);

	/* TODO: assign dependencies (remove if not needed)
	 * fill name and value and repeat as needed */
	nv = sechk_name_value_new("", "");
	apol_vector_append(mod->dependencies, (void *)nv);

	/* register functions */
	fn_struct = sechk_fn_new();
	if (!fn_struct) {
		ERR(lib->policy, "%s", strerror(ENOMEM));
		errno = ENOMEM;
		return -1;
	}
	fn_struct->name = strdup(SECHK_MOD_FN_INIT);
	if (!fn_struct->name) {
		ERR(lib->policy, "%s", strerror(ENOMEM));
		errno = ENOMEM;
		return -1;
	}
	fn_struct->fn = xx_init;
	apol_vector_append(mod->functions, (void *)fn_struct);

	fn_struct = sechk_fn_new();
	if (!fn_struct) {
		ERR(lib->policy, "%s", strerror(ENOMEM));
		errno = ENOMEM;
		return -1;
	}
	fn_struct->name = strdup(SECHK_MOD_FN_RUN);
	if (!fn_struct->name) {
		ERR(lib->policy, "%s", strerror(ENOMEM));
		errno = ENOMEM;
		return -1;
	}
	fn_struct->fn = xx_run;
	apol_vector_append(mod->functions, (void *)fn_struct);

	/* TODO: if the module does not have a private data structure
	 * set this function pointer to NULL */
	mod->data_free = xx_data_free;

	fn_struct = sechk_fn_new();
	if (!fn_struct) {
		ERR(lib->policy, "%s", strerror(ENOMEM));
		errno = ENOMEM;
		return -1;
	}
	fn_struct->name = strdup(SECHK_MOD_FN_PRINT);
	if (!fn_struct->name) {
		ERR(lib->policy, "%s", strerror(ENOMEM));
		errno = ENOMEM;
		return -1;
	}
	fn_struct->fn = xx_print;
	apol_vector_append(mod->functions, (void *)fn_struct);

	/* TODO: (optional) add any other functions needed here,
	 * add a block as above for each additional function */

	return 0;
}

/* The init function creates the module's private data storage object and
 * initializes its values.  Add any option processing logic as indicated below.
 * TODO: add options processing logic */
int xx_init(sechk_module_t * mod, apol_policy_t * policy, void *arg __attribute__ ((unused)))
{
	sechk_name_value_t *opt = NULL;
	xx_data_t *datum = NULL;
	int error = 0;
	size_t i = 0;

	if (!mod || !policy) {
		ERR(policy, "%s", strerror(EINVAL));
		errno = EINVAL;
		return -1;
	}
	if (strcmp(mod_name, mod->name)) {
		ERR(policy, "Wrong module (%s)\n", mod->name);
		errno = EINVAL;
		return -1;
	}

	/* If the module doesnot have a privte data sturcture replace the following
	 * block with "mod->data = NULL" */
	datum = xx_data_new();
	if (!datum) {
		error = errno;
		ERR(policy, "Error: %s\n", strerror(error));
		errno = error;
		return -1;
	}
	mod->data = datum;

	for (i = 0; i < apol_vector_get_size(mod->options); i++) {
		opt = apol_vector_get_element(mod->options, i);
		/* TODO: check options
		 * check strings opt->name and opt->value of each option
		 * to set the members of the private data storage object
		 * (pointed to by datum).
		 * i.e. if (!strcmp(...)) {} else if (!strcmp((...)) etc.
		 * There should be relatively few options for any one module.
		 * If too many options are needed consider splitting the check
		 * into multiple modules and using dependencies.  It is desirable
		 * for all checks to be a simple and granular as is possible */
	}

	return 0;
}

/* The run function performs the check. This function runs only once
 * even if called multiple times. All test logic should be placed below
 * as instructed. This function allocates the result structure and fills
 * in all relavant item and proof data. 
 * Return Values:
 *  -1 System error
 *   0 The module "succeeded"	- no negative results found
 *   1 The module "failed" 		- some negative results found
 * TODO: add check logic */
int xx_run(sechk_module_t * mod, apol_policy_t * policy, void *arg __attribute__ ((unused)))
{
	xx_data_t *datum;
	sechk_result_t *res = NULL;
	sechk_item_t *item = NULL;
	sechk_proof_t *proof = NULL;
	int error = 0;

	/* TODO: define any aditional variables needed */

	if (!mod || !policy) {
		ERR(policy, "%s", strerror(EINVAL));
		errno = EINVAL;
		return -1;
	}
	if (strcmp(mod_name, mod->name)) {
		ERR(policy, "Wrong module (%s)", mod->name);
		return -1;
	}

	/* if already run return */
	if (mod->result)
		return 0;

	datum = (xx_data_t *) mod->data;
	res = sechk_result_new();
	if (!res) {
		error = errno;
		ERR(policy, "%s", strerror(error));
		errno = error;
		return -1;
	}
	res->test_name = strdup(mod_name);
	if (!res->test_name) {
		error = errno;
		ERR(policy, "%s", strerror(error));
		goto xx_run_fail;
	}
	/* TODO: set res->item_type to indicate which array the item_id indexes
	 * use values from the sechk_item_type_e enum (see sechecker.h) */

	/* TODO: check logic here 
	 * Perform check here. Create and initialize items and proof as found,
	 * appending to the appropriate vectors.
	 * For examples of the type of code to use here see other modules. */

	mod->result = res;

	/* If module finds something that would be considered a failure
	 * of the policy return 1 here */
	if (apol_vector_get_size(res->items) > 0)
		return 1;

	return 0;

      xx_run_fail:
	/* TODO: free any other memory allocated during check logic */
	sechk_proof_free(proof);
	sechk_item_free(item);
	sechk_result_destroy(&res);
	errno = error;
	return -1;
}

/* The free function frees the private data of a module
 * TODO: be sure to free any allocated space in the private data */
void xx_data_free(void *data)
{
	xx_data_t *datum = (xx_data_t *) data;

	if (datum) {
		/* TODO: free any allocated members of the module's
		 * private data structure */
	}

	free(data);
}

/* The print function generates the text and prints the results to stdout. The
 * outline below prints the standard format of a report section. Some modules
 * may not have results in a format that can be represented by this outline and
 * will need a different specification. It is required that each of the flags
 * for output components be tested in this function (stats, list, proof,
 * detailed, and brief) TODO: fill in the indicated information in the report
 * fields as indicated below. Some alteration may be necessary for checks that
 * perform different analyses */
int xx_print(sechk_module_t * mod, apol_policy_t * policy, void *arg __attribute__ ((unused)))
{
	xx_data_t *datum = NULL;
	unsigned char outformat = 0x00;
	sechk_item_t *item = NULL;
	sechk_proof_t *proof = NULL;
	size_t i = 0, j = 0;

	if (!mod || !policy) {
		ERR(policy, "%s", strerror(EINVAL));
		errno = EINVAL;
		return -1;
	}
	if (strcmp(mod_name, mod->name)) {
		ERR(policy, "Wrong module (%s)", mod->name);
		errno = EINVAL;
		return -1;
	}

	datum = (xx_data_t *) mod->data;
	outformat = mod->outputformat;

	if (!mod->result) {
		ERR(policy, "Module %s has not been run", mod->name);
		errno = EINVAL;
		return -1;
	}

	if (!outformat || (outformat & SECHK_OUT_QUIET))
		return 0;	       /* not an error - no output is requested */

	/* TODO: display the statistics of the results
	 * typical text is "Found %i <itemtype>.\n"
	 * additional information may be printed here depending upon
	 * the amount of data gathered in the check */
	if (outformat & SECHK_OUT_STATS) {
		/* TODO: "Found %i <itemtype>.\n": enter itemtype */
		printf("Found %zd .\n", apol_vector_get_size(mod->result->items));
		/* TODO: any additional generated statistics */
	}
	/* The list report component is a display of all items found without any
	 * supporting proof. The default method is to display a comma separated list
	 * four items to a line this may need to be changed for longer items.
	 * TODO: you will need to enter the string representation of
	 * each item as the second parameter in the printf statement
	 * in place of the empty string.
	 * NOTE: if the item is a type of rule print only one per line. */
	if (outformat & SECHK_OUT_LIST) {
		printf("\n");
		for (i = 0; i < apol_vector_get_size(mod->result->items); i++) {
			item = apol_vector_get_element(mod->result->items, i);
			i++;
			/* TODO: (optional) change the number below to
			 * print more or less than 4 items per line */
			i %= 4;
			/* TODO: second parameter: item name */
			printf("%s%s", "", (i ? ", " : "\n"));
		}
		printf("\n");
	}
	/* The proof report component is a display of a list of items with an
	 * indented list of proof statements supporting the result of the check for
	 * that item (e.g. rules with a given type).  Each proof element is then
	 * displayed in an indented list one per line below it.
	 * TODO: the name of the item should be entered below.
	 * NOTE: certain checks may need to further modify this report component if
	 * the results cannot be presented in this format */
	if (outformat & SECHK_OUT_PROOF) {
		printf("\n");
		for (i = 0; i < apol_vector_get_size(mod->result->items); i++) {
			item = apol_vector_get_element(mod->result->items, i);
			printf("%s", "");	/* TODO: item name */
			printf(" - severity: %s\n", sechk_item_sev(item));
			for (j = 0; j < apol_vector_get_size(item->proof); j++) {
				proof = apol_vector_get_element(item->proof, j);
				printf("\t%s\n", proof->text);
			}
		}
		printf("\n");
	}

	return 0;
}

/* The xx_data_new function allocates and returns an initialized private data
 * storage structure for this module. 
 * TODO: initialize any non-zero/non-null data (if needed) below */
xx_data_t *xx_data_new(void)
{
	xx_data_t *datum = NULL;

	datum = (xx_data_t *) calloc(1, sizeof(xx_data_t));

	/* TODO: initialize data */

	return datum;
}