utils.c 1.9 KB

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