blob: 5865bd0ab6aab2e974fdce826fc17b1a1eef52cb [file] [log] [blame]
William Robertsf0e0a942012-08-27 15:41:15 -07001#include <stdio.h>
2#include <stdarg.h>
3#include <ctype.h>
4#include <stdio.h>
5#include <stdlib.h>
6#include <unistd.h>
7#include <string.h>
8#include <errno.h>
9#include <stdint.h>
10#include <search.h>
11#include <sepol/sepol.h>
12#include <sepol/policydb/policydb.h>
13
14#define TABLE_SIZE 1024
15#define KVP_NUM_OF_RULES (sizeof(rules) / sizeof(key_map))
16#define log_set_verbose() do { logging_verbose = 1; log_info("Enabling verbose\n"); } while(0)
17#define log_error(fmt, ...) log_msg(stderr, "Error: ", fmt, ##__VA_ARGS__)
18#define log_warn(fmt, ...) log_msg(stderr, "Warning: ", fmt, ##__VA_ARGS__)
19#define log_info(fmt, ...) if (logging_verbose ) { log_msg(stdout, "Info: ", fmt, ##__VA_ARGS__); }
20
21typedef struct line_order_list line_order_list;
22typedef struct hash_entry hash_entry;
23typedef enum key_dir key_dir;
24typedef enum data_type data_type;
25typedef enum rule_map_switch rule_map_switch;
William Roberts0ae3a8a2012-09-04 11:51:04 -070026typedef enum map_match map_match;
William Robertsf0e0a942012-08-27 15:41:15 -070027typedef struct key_map key_map;
28typedef struct kvp kvp;
29typedef struct rule_map rule_map;
30typedef struct policy_info policy_info;
31
William Roberts0ae3a8a2012-09-04 11:51:04 -070032enum map_match {
33 map_no_matches,
34 map_input_matched,
35 map_matched
36};
37
William Robertsf0e0a942012-08-27 15:41:15 -070038/**
39 * Whether or not the "key" from a key vaue pair is considered an
40 * input or an output.
41 */
42enum key_dir {
43 dir_in, dir_out
44};
45
46/**
47 * Used as options to rule_map_free()
48 *
49 * This is needed to get around the fact that GNU C's hash_map doesn't copy the key, so
50 * we cannot free a key when overrding rule_map's in the table.
51 */
52enum rule_map_switch {
53 rule_map_preserve_key, /** Used to preserve the key in the rule_map, ie don't free it*/
54 rule_map_destroy_key /** Used when you need a full free of the rule_map structure*/
55};
56
57/**
58 * The expected "type" of data the value in the key
59 * value pair should be.
60 */
61enum data_type {
62 dt_bool, dt_string
63};
64
65/**
66 * This list is used to store a double pointer to each
67 * hash table / line rule combination. This way a replacement
68 * in the hash table automatically updates the list. The list
69 * is also used to keep "first encountered" ordering amongst
70 * the encountered key value pairs in the rules file.
71 */
72struct line_order_list {
73 hash_entry *e;
74 line_order_list *next;
75};
76
77/**
78 * The workhorse of the logic. This struct maps key value pairs to
79 * an associated set of meta data maintained in rule_map_new()
80 */
81struct key_map {
82 char *name;
83 key_dir dir;
84 data_type type;
85 char *data;
86};
87
88/**
89 * Key value pair struct, this represents the raw kvp values coming
90 * from the rules files.
91 */
92struct kvp {
93 char *key;
94 char *value;
95};
96
97/**
98 * Rules are made up of meta data and an associated set of kvp stored in a
99 * key_map array.
100 */
101struct rule_map {
102 char *key; /** key value before hashing */
103 int length; /** length of the key map */
104 int lineno; /** Line number rule was encounter on */
105 rule_map *next; /** next pointer used in hash table for chaining on collision */
106 key_map m[]; /** key value mapping */
107};
108
109struct hash_entry {
110 rule_map *r; /** The rule map to store at that location */
111};
112
113/**
114 * Data associated for a policy file
115 */
116struct policy_info {
117
118 char *policy_file_name; /** policy file path name */
119 FILE *policy_file; /** file handle to the policy file */
120 sepol_policydb_t *db;
121 sepol_policy_file_t *pf;
122 sepol_handle_t *handle;
123 sepol_context_t *con;
124};
125
126/** Set to !0 to enable verbose logging */
127static int logging_verbose = 0;
128
129/** file handle to the output file */
130static FILE *output_file = NULL;
131
132/** file handle to the input file */
133static FILE *input_file = NULL;
134
135/** output file path name */
136static char *out_file_name = NULL;
137
138/** input file path name */
139static char *in_file_name = NULL;
140
141static policy_info pol = {
142 .policy_file_name = NULL,
143 .policy_file = NULL,
144 .db = NULL,
145 .pf = NULL,
146 .handle = NULL,
147 .con = NULL
148};
149
150/**
151 * The heart of the mapping process, this must be updated if a new key value pair is added
152 * to a rule.
153 */
154key_map rules[] = {
155 /*Inputs*/
156 { .name = "isSystemServer", .type = dt_bool, .dir = dir_in, .data = NULL },
157 { .name = "user", .type = dt_string, .dir = dir_in, .data = NULL },
158 { .name = "seinfo", .type = dt_string, .dir = dir_in, .data = NULL },
159 { .name = "name", .type = dt_string, .dir = dir_in, .data = NULL },
160 { .name = "sebool", .type = dt_string, .dir = dir_in, .data = NULL },
161 /*Outputs*/
162 { .name = "domain", .type = dt_string, .dir = dir_out, .data = NULL },
163 { .name = "type", .type = dt_string, .dir = dir_out, .data = NULL },
164 { .name = "levelFromUid", .type = dt_bool, .dir = dir_out, .data = NULL },
165 { .name = "level", .type = dt_string, .dir = dir_out, .data = NULL },
166 };
167
168/**
169 * Head pointer to a linked list of
170 * rule map table entries, used for
171 * preserving the order of entries
172 * based on "first encounter"
173 */
174static line_order_list *list_head = NULL;
175
176/**
177 * Pointer to the tail of the list for
178 * quick appends to the end of the list
179 */
180static line_order_list *list_tail = NULL;
181
182/**
183 * Send a logging message to a file
184 * @param out
185 * Output file to send message too
186 * @param prefix
187 * A special prefix to write to the file, such as "Error:"
188 * @param fmt
189 * The printf style formatter to use, such as "%d"
190 */
191static void log_msg(FILE *out, const char *prefix, const char *fmt, ...) {
192 fprintf(out, "%s", prefix);
193 va_list args;
194 va_start(args, fmt);
195 vfprintf(out, fmt, args);
196 va_end(args);
197}
198
199/**
200 * Checks for a type in the policy.
201 * @param db
202 * The policy db to search
203 * @param type
204 * The type to search for
205 * @return
206 * 1 if the type is found, 0 otherwise.
207 * @warning
208 * This function always returns 1 if libsepol is not linked
209 * statically to this executable and LINK_SEPOL_STATIC is not
210 * defined.
211 */
212int check_type(sepol_policydb_t *db, char *type) {
213
214 int rc = 1;
215#if defined(LINK_SEPOL_STATIC)
216 policydb_t *d = (policydb_t *)db;
217 hashtab_datum_t dat;
218 dat = hashtab_search(d->p_types.table, type);
219 rc = (dat == NULL) ? 0 : 1;
220#endif
221 return rc;
222}
223
224/**
225 * Validates a key_map against a set of enforcement rules, this
226 * function exits the application on a type that cannot be properly
227 * checked
228 *
229 * @param m
230 * The key map to check
231 * @param lineno
232 * The line number in the source file for the corresponding key map
233 */
234static int key_map_validate(key_map *m, int lineno) {
235
236 int rc = 1;
237 int ret = 1;
William Robertsf0e0a942012-08-27 15:41:15 -0700238 int resp;
239 char *key = m->name;
240 char *value = m->data;
241 data_type type = m->type;
242 sepol_bool_key_t *se_key;
243
William Roberts0ae3a8a2012-09-04 11:51:04 -0700244 log_info("Validating %s=%s\n", key, value);
245
William Robertsf0e0a942012-08-27 15:41:15 -0700246 /* Booleans can always be checked for sanity */
247 if (type == dt_bool && (!strcmp("true", value) || !strcmp("false", value))) {
248 goto out;
249 }
250 else if (type == dt_bool) {
William Robertsae23a1f2012-09-05 12:53:52 -0700251 log_error("Expected boolean value got: %s=%s on line: %d in file: %s\n",
252 key, value, lineno, out_file_name);
William Robertsf0e0a942012-08-27 15:41:15 -0700253 rc = 0;
254 goto out;
255 }
256
257 /*
258 * If their is no policy file present,
259 * then it is not in strict mode so just return.
260 * User and name cannot really be checked.
261 */
262 if (!pol.policy_file) {
263 goto out;
264 }
265 else if (!strcasecmp(key, "sebool")) {
266
267 ret = sepol_bool_key_create(pol.handle, value, &se_key);
268 if (ret < 0) {
269 log_error("Could not create selinux boolean key, error: %s\n",
270 strerror(errno));
271 rc = 0;
272 goto out;
273 }
274
275 ret = sepol_bool_exists(pol.handle, pol.db, se_key, &resp);
276 if (ret < 0) {
277 log_error("Could not check selinux boolean, error: %s\n",
278 strerror(errno));
279 rc = 0;
280 goto bool_err;
281 }
282
283 if(!resp) {
284 log_error("Could not find selinux boolean \"%s\" on line: %d in file: %s\n",
285 value, lineno, out_file_name);
286 rc = 0;
287 goto bool_err;
288 }
289 }
290 else if (!strcasecmp(key, "type") || !strcasecmp(key, "domain")) {
291
292 if(!check_type(pol.db, value)) {
293 log_error("Could not find selinux type \"%s\" on line: %d in file: %s\n", value,
294 lineno, out_file_name);
295 rc = 0;
296 }
297 goto out;
298 }
299
William Robertsf0e0a942012-08-27 15:41:15 -0700300 else if (!strcasecmp(key, "level")) {
301
William Roberts0ae3a8a2012-09-04 11:51:04 -0700302 ret = sepol_mls_check(pol.handle, pol.db, value);
303 if (ret < 0) {
William Robertsae23a1f2012-09-05 12:53:52 -0700304 log_error("Could not find selinux level \"%s\", on line: %d in file: %s\n", value,
305 lineno, out_file_name);
William Roberts0ae3a8a2012-09-04 11:51:04 -0700306 rc = 0;
307 goto out;
William Robertsf0e0a942012-08-27 15:41:15 -0700308 }
309 }
310
William Robertsf0e0a942012-08-27 15:41:15 -0700311bool_err:
312 sepol_bool_key_free(se_key);
William Robertsf0e0a942012-08-27 15:41:15 -0700313
William Roberts0ae3a8a2012-09-04 11:51:04 -0700314out:
315 log_info("Key map validate returning: %d\n", rc);
316 return rc;
William Robertsf0e0a942012-08-27 15:41:15 -0700317}
318
319/**
320 * Prints a rule map back to a file
321 * @param fp
322 * The file handle to print too
323 * @param r
324 * The rule map to print
325 */
326static void rule_map_print(FILE *fp, rule_map *r) {
327
328 int i;
329 key_map *m;
330
331 for (i = 0; i < r->length; i++) {
332 m = &(r->m[i]);
333 if (i < r->length - 1)
334 fprintf(fp, "%s=%s ", m->name, m->data);
335 else
336 fprintf(fp, "%s=%s", m->name, m->data);
337 }
338}
339
340/**
341 * Compare two rule maps for equality
342 * @param rmA
343 * a rule map to check
344 * @param rmB
345 * a rule map to check
346 * @return
William Robertsae23a1f2012-09-05 12:53:52 -0700347 * a map_match enum indicating the result
William Robertsf0e0a942012-08-27 15:41:15 -0700348 */
William Roberts0ae3a8a2012-09-04 11:51:04 -0700349static map_match rule_map_cmp(rule_map *rmA, rule_map *rmB) {
William Robertsf0e0a942012-08-27 15:41:15 -0700350
351 int i;
352 int j;
353 int inputs_found = 0;
354 int num_of_matched_inputs = 0;
355 int input_mode = 0;
356 int matches = 0;
357 key_map *mA;
358 key_map *mB;
359
360 if (rmA->length != rmB->length)
William Roberts0ae3a8a2012-09-04 11:51:04 -0700361 return map_no_matches;
William Robertsf0e0a942012-08-27 15:41:15 -0700362
363 for (i = 0; i < rmA->length; i++) {
364 mA = &(rmA->m[i]);
365
366 for (j = 0; j < rmB->length; j++) {
367 mB = &(rmB->m[j]);
368 input_mode = 0;
369
370 if (mA->type != mB->type)
371 continue;
372
373 if (strcmp(mA->name, mB->name))
374 continue;
375
376 if (strcmp(mA->data, mB->data))
377 continue;
378
379 if (mB->dir != mA->dir)
380 continue;
381 else if (mB->dir == dir_in) {
382 input_mode = 1;
383 inputs_found++;
384 }
385
William Roberts0ae3a8a2012-09-04 11:51:04 -0700386 if (input_mode) {
387 log_info("Matched input lines: type=%s name=%s data=%s dir=%d\n", mA->type, mA->name, mA->data, mA->dir);
William Robertsf0e0a942012-08-27 15:41:15 -0700388 num_of_matched_inputs++;
William Roberts0ae3a8a2012-09-04 11:51:04 -0700389 }
William Robertsf0e0a942012-08-27 15:41:15 -0700390
391 /* Match found, move on */
William Roberts0ae3a8a2012-09-04 11:51:04 -0700392 log_info("Matched lines: type=%s name=%s data=%s dir=%d\n", mA->type, mA->name, mA->data, mA->dir);
William Robertsf0e0a942012-08-27 15:41:15 -0700393 matches++;
394 break;
395 }
396 }
397
398 /* If they all matched*/
William Roberts0ae3a8a2012-09-04 11:51:04 -0700399 if (matches == rmA->length) {
400 log_info("Rule map cmp MATCH\n");
401 return map_matched;
402 }
William Robertsf0e0a942012-08-27 15:41:15 -0700403
404 /* They didn't all match but the input's did */
William Roberts0ae3a8a2012-09-04 11:51:04 -0700405 else if (num_of_matched_inputs == inputs_found) {
406 log_info("Rule map cmp INPUT MATCH\n");
407 return map_input_matched;
408 }
William Robertsf0e0a942012-08-27 15:41:15 -0700409
410 /* They didn't all match, and the inputs didn't match, ie it didn't
411 * match */
William Roberts0ae3a8a2012-09-04 11:51:04 -0700412 else {
413 log_info("Rule map cmp NO MATCH\n");
414 return map_no_matches;
415 }
William Robertsf0e0a942012-08-27 15:41:15 -0700416}
417
418/**
419 * Frees a rule map
420 * @param rm
421 * rule map to be freed.
422 */
423static void rule_map_free(rule_map *rm, rule_map_switch s) {
424
425 int i;
426 int len = rm->length;
427 for (i = 0; i < len; i++) {
428 key_map *m = &(rm->m[i]);
429 free(m->data);
430 }
431
432 if(s == rule_map_destroy_key && rm->key)
433 free(rm->key);
434
435 free(rm);
436}
437
438static void free_kvp(kvp *k) {
439 free(k->key);
440 free(k->value);
441}
442
443/**
444 * Given a set of key value pairs, this will construct a new rule map.
445 * On error this function calls exit.
446 * @param keys
447 * Keys from a rule line to map
448 * @param num_of_keys
449 * The length of the keys array
450 * @param lineno
451 * The line number the keys were extracted from
452 * @return
453 * A rule map pointer.
454 */
455static rule_map *rule_map_new(kvp keys[], unsigned int num_of_keys, int lineno) {
456
457 unsigned int i = 0, j = 0;
458 rule_map *new_map = NULL;
459 kvp *k = NULL;
460 key_map *r = NULL, *x = NULL;
461
462 new_map = calloc(1, (num_of_keys * sizeof(key_map)) + sizeof(rule_map));
463 if (!new_map)
464 goto oom;
465
466 new_map->length = num_of_keys;
467 new_map->lineno = lineno;
468
469 /* For all the keys in a rule line*/
470 for (i = 0; i < num_of_keys; i++) {
471 k = &(keys[i]);
472 r = &(new_map->m[i]);
473
474 for (j = 0; j < KVP_NUM_OF_RULES; j++) {
475 x = &(rules[j]);
476
477 /* Only assign key name to map name */
478 if (strcasecmp(k->key, x->name)) {
479 if (i == KVP_NUM_OF_RULES) {
480 log_error("No match for key: %s\n", k->key);
481 goto err;
482 }
483 continue;
484 }
485
486 memcpy(r, x, sizeof(key_map));
487
488 /* Assign rule map value to one from file */
489 r->data = strdup(k->value);
490 if (!r->data)
491 goto oom;
492
493 /* Enforce type check*/
William Roberts0ae3a8a2012-09-04 11:51:04 -0700494 log_info("Validating keys!\n");
William Robertsf0e0a942012-08-27 15:41:15 -0700495 if (!key_map_validate(r, lineno)) {
496 log_error("Could not validate\n");
497 goto err;
498 }
499
500 /* Only build key off of inputs*/
501 if (r->dir == dir_in) {
502 char *tmp;
503 int l = strlen(k->key);
504 l += strlen(k->value);
505 l += (new_map->key) ? strlen(new_map->key) : 0;
506 l += 1;
507
508 tmp = realloc(new_map->key, l);
509 if (!tmp)
510 goto oom;
511
512 new_map->key = tmp;
513
514 strcat(new_map->key, k->key);
515 strcat(new_map->key, k->value);
516 }
517 break;
518 }
519 free_kvp(k);
520 }
521
522 if (new_map->key == NULL) {
523 log_error("Strange, no keys found, input file corrupt perhaps?\n");
524 goto err;
525 }
526
527 return new_map;
528
529oom:
530 log_error("Out of memory!\n");
531err:
532 if(new_map) {
533 rule_map_free(new_map, rule_map_destroy_key);
534 for (; i < num_of_keys; i++) {
535 k = &(keys[i]);
536 free_kvp(k);
537 }
538 }
539 exit(EXIT_FAILURE);
540}
541
542/**
543 * Print the usage of the program
544 */
545static void usage() {
546 printf(
547 "checkseapp [options] <input file>\n"
548 "Processes an seapp_contexts file specified by argument <input file> (default stdin) "
William Robertsae23a1f2012-09-05 12:53:52 -0700549 "and allows later declarations to override previous ones on a match.\n"
William Robertsf0e0a942012-08-27 15:41:15 -0700550 "Options:\n"
551 "-h - print this help message\n"
552 "-v - enable verbose debugging informations\n"
553 "-p policy file - specify policy file for strict checking of output selectors\n"
554 "-o output file - specify output file, default is stdout\n");
555}
556
557static void init() {
558
559 /* If not set on stdin already */
560 if(!input_file) {
561 log_info("Opening input file: %s\n", in_file_name);
562 input_file = fopen(in_file_name, "r");
563 if (!input_file) {
564 log_error("Could not open file: %s error: %s\n", in_file_name, strerror(errno));
565 exit(EXIT_FAILURE);
566 }
567 }
568
569 /* If not set on std out already */
570 if(!output_file) {
571 output_file = fopen(out_file_name, "w+");
572 if (!output_file) {
573 log_error("Could not open file: %s error: %s\n", out_file_name, strerror(errno));
574 exit(EXIT_FAILURE);
575 }
576 }
577
578 if (pol.policy_file_name) {
579
580 log_info("Opening policy file: %s\n", pol.policy_file_name);
581 pol.policy_file = fopen(pol.policy_file_name, "rb");
582 if (!pol.policy_file) {
583 log_error("Could not open file: %s error: %s\n",
584 pol.policy_file_name, strerror(errno));
585 exit(EXIT_FAILURE);
586 }
587
588 pol.handle = sepol_handle_create();
589 if (!pol.handle) {
590 log_error("Could not create sepolicy handle: %s\n",
591 strerror(errno));
592 exit(EXIT_FAILURE);
593 }
594
595 if (sepol_policy_file_create(&pol.pf) < 0) {
596 log_error("Could not create sepolicy file: %s!\n",
597 strerror(errno));
598 exit(EXIT_FAILURE);
599 }
600
601 sepol_policy_file_set_fp(pol.pf, pol.policy_file);
602 sepol_policy_file_set_handle(pol.pf, pol.handle);
603
604 if (sepol_policydb_create(&pol.db) < 0) {
605 log_error("Could not create sepolicy db: %s!\n",
606 strerror(errno));
607 exit(EXIT_FAILURE);
608 }
609
610 if (sepol_policydb_read(pol.db, pol.pf) < 0) {
611 log_error("Could not lod policy file to db: %s!\n",
612 strerror(errno));
613 exit(EXIT_FAILURE);
614 }
615 }
616
617 log_info("Policy file set to: %s\n", (pol.policy_file_name == NULL) ? "None" : pol.policy_file_name);
618 log_info("Input file set to: %s\n", (in_file_name == NULL) ? "stdin" : in_file_name);
619 log_info("Output file set to: %s\n", (out_file_name == NULL) ? "stdout" : out_file_name);
620
William Roberts0ae3a8a2012-09-04 11:51:04 -0700621#if !defined(LINK_SEPOL_STATIC)
622 log_warning("LINK_SEPOL_STATIC is not defined\n""Not checking types!");
623#endif
624
William Robertsf0e0a942012-08-27 15:41:15 -0700625}
626
627/**
628 * Handle parsing and setting the global flags for the command line
629 * options. This function calls exit on failure.
630 * @param argc
631 * argument count
632 * @param argv
633 * argument list
634 */
635static void handle_options(int argc, char *argv[]) {
636
637 int c;
638 int num_of_args;
639
640 while ((c = getopt(argc, argv, "ho:p:v")) != -1) {
641 switch (c) {
642 case 'h':
643 usage();
644 exit(EXIT_SUCCESS);
645 case 'o':
646 out_file_name = optarg;
647 break;
648 case 'p':
649 pol.policy_file_name = optarg;
650 break;
651 case 'v':
652 log_set_verbose();
653 break;
654 case '?':
655 if (optopt == 'o' || optopt == 'p')
656 log_error("Option -%c requires an argument.\n", optopt);
657 else if (isprint (optopt))
658 log_error("Unknown option `-%c'.\n", optopt);
659 else {
660 log_error(
661 "Unknown option character `\\x%x'.\n",
662 optopt);
663 exit(EXIT_FAILURE);
664 }
665 break;
666 default:
667 exit(EXIT_FAILURE);
668 }
669 }
670
671 num_of_args = argc - optind;
672
673 if (num_of_args > 1) {
674 log_error("Too many arguments, expected 0 or 1, argument, got %d\n", num_of_args);
675 usage();
676 exit(EXIT_FAILURE);
677 } else if (num_of_args == 1) {
678 in_file_name = argv[argc - 1];
679 } else {
680 input_file = stdin;
681 in_file_name = "stdin";
682 }
683
684 if (!out_file_name) {
685 output_file = stdout;
686 out_file_name = "stdout";
687 }
688}
689
690/**
691 * Adds a rule_map double pointer, ie the hash table pointer to the list.
692 * By using a double pointer, the hash table can have a line be overridden
693 * and the value is updated in the list. This function calls exit on failure.
694 * @param rm
695 * the rule_map to add.
696 */
697static void list_add(hash_entry *e) {
698
699 line_order_list *node = malloc(sizeof(line_order_list));
700 if (node == NULL)
701 goto oom;
702
703 node->next = NULL;
704 node->e = e;
705
706 if (list_head == NULL)
707 list_head = list_tail = node;
708 else {
709 list_tail->next = node;
710 list_tail = list_tail->next;
711 }
712 return;
713
714oom:
715 log_error("Out of memory!\n");
716 exit(EXIT_FAILURE);
717}
718
719/**
720 * Free's the rule map list, which ultimatley contains
721 * all the malloc'd rule_maps.
722 */
723static void list_free() {
724 line_order_list *cursor, *tmp;
725 hash_entry *e;
726
727 cursor = list_head;
728 while (cursor) {
729 e = cursor->e;
730 rule_map_free(e->r, rule_map_destroy_key);
731 tmp = cursor;
732 cursor = cursor->next;
733 free(e);
734 free(tmp);
735 }
736}
737
738/**
739 * Adds a rule to the hash table and to the ordered list if needed.
740 * @param rm
741 * The rule map to add.
742 */
743static void rule_add(rule_map *rm) {
744
William Roberts0ae3a8a2012-09-04 11:51:04 -0700745 map_match cmp;
William Robertsf0e0a942012-08-27 15:41:15 -0700746 ENTRY e;
747 ENTRY *f;
748 hash_entry *entry;
749 hash_entry *tmp;
750 char *preserved_key;
751
752 e.key = rm->key;
753
William Roberts0ae3a8a2012-09-04 11:51:04 -0700754 log_info("Searching for key: %s\n", e.key);
William Robertsf0e0a942012-08-27 15:41:15 -0700755 /* Check to see if it has already been added*/
756 f = hsearch(e, FIND);
757
758 /*
759 * Since your only hashing on a partial key, the inputs we need to handle
760 * when you want to override the outputs for a given input set, as well as
761 * checking for duplicate entries.
762 */
763 if(f) {
William Roberts0ae3a8a2012-09-04 11:51:04 -0700764 log_info("Existing entry found!\n");
William Robertsf0e0a942012-08-27 15:41:15 -0700765 tmp = (hash_entry *)f->data;
766 cmp = rule_map_cmp(rm, tmp->r);
William Roberts0ae3a8a2012-09-04 11:51:04 -0700767 log_info("Comparing on rule map ret: %d\n", cmp);
William Robertsf0e0a942012-08-27 15:41:15 -0700768 /* Override be freeing the old rule map and updating
769 the pointer */
William Roberts0ae3a8a2012-09-04 11:51:04 -0700770 if(cmp != map_matched) {
William Robertsf0e0a942012-08-27 15:41:15 -0700771
772 /*
773 * DO NOT free key pointers given to the hash map, instead
774 * free the new key. The ordering here is critical!
775 */
776 preserved_key = tmp->r->key;
777 rule_map_free(tmp->r, rule_map_preserve_key);
778 free(rm->key);
779 rm->key = preserved_key;
780 tmp->r = rm;
781 }
782 /* Duplicate */
783 else {
784 log_error("Duplicate line detected in file: %s\n"
785 "Lines %d and %d match!\n",
786 out_file_name, tmp->r->lineno, rm->lineno);
787 rule_map_free(rm, rule_map_destroy_key);
788 goto err;
789 }
790 }
791 /* It wasn't found, just add the rule map to the table */
792 else {
793
794 entry = malloc(sizeof(hash_entry));
795 if (!entry)
796 goto oom;
797
798 entry->r = rm;
799 e.data = entry;
800
801 f = hsearch(e, ENTER);
802 if(f == NULL) {
803 goto oom;
804 }
805
806 /* new entries must be added to the ordered list */
807 entry->r = rm;
808 list_add(entry);
809 }
810
811 return;
812oom:
813 if (e.key)
814 free(e.key);
815 if (entry)
816 free(entry);
817 if (rm)
818 free(rm);
819 log_error("Out of memory in function: %s\n", __FUNCTION__);
820err:
821 exit(EXIT_FAILURE);
822}
823
824/**
825 * Parses the seapp_contexts file and adds them to the
826 * hash table and ordered list entries when it encounters them.
827 * Calls exit on failure.
828 */
829static void parse() {
830
831 char line_buf[BUFSIZ];
832 char *token;
833 unsigned lineno = 0;
834 char *p, *name = NULL, *value = NULL, *saveptr;
835 size_t len;
836 kvp keys[KVP_NUM_OF_RULES];
837 int token_cnt = 0;
838
839 while (fgets(line_buf, sizeof line_buf - 1, input_file)) {
840
841 lineno++;
842 log_info("Got line %d\n", lineno);
843 len = strlen(line_buf);
844 if (line_buf[len - 1] == '\n')
845 line_buf[len - 1] = 0;
846 p = line_buf;
847 while (isspace(*p))
848 p++;
849 if (*p == '#' || *p == 0)
850 continue;
851
852 token = strtok_r(p, " \t", &saveptr);
853 if (!token)
854 goto err;
855
856 token_cnt = 0;
857 memset(keys, 0, sizeof(kvp) * KVP_NUM_OF_RULES);
858 while (1) {
William Roberts0ae3a8a2012-09-04 11:51:04 -0700859
William Robertsf0e0a942012-08-27 15:41:15 -0700860 name = token;
861 value = strchr(name, '=');
862 if (!value)
863 goto err;
864 *value++ = 0;
865
866 keys[token_cnt].key = strdup(name);
867 if (!keys[token_cnt].key)
868 goto oom;
869
870 keys[token_cnt].value = strdup(value);
871 if (!keys[token_cnt].value)
872 goto oom;
873
874 token_cnt++;
875
876 token = strtok_r(NULL, " \t", &saveptr);
877 if (!token)
878 break;
879
880 } /*End token parsing */
881
882 rule_map *r = rule_map_new(keys, token_cnt, lineno);
883 rule_add(r);
884
885 } /* End file parsing */
886 return;
887
888err:
889 log_error("reading %s, line %u, name %s, value %s\n",
890 in_file_name, lineno, name, value);
891 exit(EXIT_FAILURE);
892oom:
893 log_error("In function %s: Out of memory\n", __FUNCTION__);
894 exit(EXIT_FAILURE);
895}
896
897/**
898 * Should be called after parsing to cause the printing of the rule_maps
899 * stored in the ordered list, head first, which preserves the "first encountered"
900 * ordering.
901 */
902static void output() {
903
904 rule_map *r;
905 line_order_list *cursor;
906 cursor = list_head;
907
908 while (cursor) {
909 r = cursor->e->r;
910 rule_map_print(output_file, r);
911 cursor = cursor->next;
William Robertsa8613182012-09-05 11:23:40 -0700912 fprintf(output_file, "\n");
William Robertsf0e0a942012-08-27 15:41:15 -0700913 }
914}
915
916/**
917 * This function is registered to the at exit handler and should clean up
918 * the programs dynamic resources, such as memory and fd's.
919 */
920static void cleanup() {
921
922 /* Only close this when it was opened by me and not the crt */
923 if (out_file_name && output_file) {
924 log_info("Closing file: %s\n", out_file_name);
925 fclose(output_file);
926 }
927
928 /* Only close this when it was opened by me and not the crt */
929 if (in_file_name && input_file) {
930 log_info("Closing file: %s\n", in_file_name);
931 fclose(input_file);
932 }
933
934 if (pol.policy_file) {
935
936 log_info("Closing file: %s\n", pol.policy_file_name);
937 fclose(pol.policy_file);
938
939 if (pol.db)
940 sepol_policydb_free(pol.db);
941
942 if (pol.pf)
943 sepol_policy_file_free(pol.pf);
944
945 if (pol.handle)
946 sepol_handle_destroy(pol.handle);
947 }
948
949 log_info("Freeing list\n");
950 list_free();
951 hdestroy();
952}
953
954int main(int argc, char *argv[]) {
955 if (!hcreate(TABLE_SIZE)) {
956 log_error("Could not create hash table: %s\n", strerror(errno));
957 exit(EXIT_FAILURE);
958 }
959 atexit(cleanup);
960 handle_options(argc, argv);
961 init();
962 log_info("Starting to parse\n");
963 parse();
964 log_info("Parsing completed, generating output\n");
965 output();
966 log_info("Success, generated output\n");
967 exit(EXIT_SUCCESS);
968}