1 #include <fcntl.h>
2 #include <sepol/policydb/policydb.h>
3 #include <sepol/policydb/util.h>
4 #include <sys/mman.h>
5 #include <sys/stat.h>
6 #include <unistd.h>
7
8 #include "utils.h"
9
10 bool USAGE_ERROR = false;
11
display_allow(policydb_t * policydb,avtab_key_t * key,int idx,uint32_t perms)12 void display_allow(policydb_t *policydb, avtab_key_t *key, int idx, uint32_t perms)
13 {
14 printf(" allow %s %s:%s { %s };\n",
15 policydb->p_type_val_to_name[key->source_type
16 ? key->source_type - 1 : idx],
17 key->target_type == key->source_type ? "self" :
18 policydb->p_type_val_to_name[key->target_type
19 ? key->target_type - 1 : idx],
20 policydb->p_class_val_to_name[key->target_class - 1],
21 sepol_av_to_string
22 (policydb, key->target_class, perms));
23 }
24
load_policy(char * filename,policydb_t * policydb,struct policy_file * pf)25 bool load_policy(char *filename, policydb_t * policydb, struct policy_file *pf)
26 {
27 int fd = -1;
28 struct stat sb;
29 void *map = MAP_FAILED;
30 bool ret = false;
31
32 fd = open(filename, O_RDONLY);
33 if (fd < 0) {
34 fprintf(stderr, "Can't open '%s': %s\n", filename, strerror(errno));
35 goto cleanup;
36 }
37 if (fstat(fd, &sb) < 0) {
38 fprintf(stderr, "Can't stat '%s': %s\n", filename, strerror(errno));
39 goto cleanup;
40 }
41 map = mmap(NULL, sb.st_size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
42 if (map == MAP_FAILED) {
43 fprintf(stderr, "Can't mmap '%s': %s\n", filename, strerror(errno));
44 goto cleanup;
45 }
46
47 policy_file_init(pf);
48 pf->type = PF_USE_MEMORY;
49 pf->data = map;
50 pf->len = sb.st_size;
51 if (policydb_init(policydb)) {
52 fprintf(stderr, "Could not initialize policydb!\n");
53 goto cleanup;
54 }
55 if (policydb_read(policydb, pf, 0)) {
56 fprintf(stderr, "error(s) encountered while parsing configuration\n");
57 goto cleanup;
58 }
59
60 ret = true;
61
62 cleanup:
63 if (map != MAP_FAILED) {
64 munmap(map, sb.st_size);
65 }
66 if (fd >= 0) {
67 close(fd);
68 }
69 return ret;
70 }
71