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