1d410645aec7d51d49884a05863cf10497e43f0bcRob Landley/* load_policy.c - Load a policy file
2d6d4ad0663dc91a448139d9bac4fecab43a41b30Elliott Hughes *
3d6d4ad0663dc91a448139d9bac4fecab43a41b30Elliott Hughes * Copyright 2015 The Android Open Source Project
4d6d4ad0663dc91a448139d9bac4fecab43a41b30Elliott Hughes
5d6d4ad0663dc91a448139d9bac4fecab43a41b30Elliott HughesUSE_LOAD_POLICY(NEWTOY(load_policy, "<1>1", TOYFLAG_USR|TOYFLAG_SBIN))
6d6d4ad0663dc91a448139d9bac4fecab43a41b30Elliott Hughes
7d6d4ad0663dc91a448139d9bac4fecab43a41b30Elliott Hughesconfig LOAD_POLICY
8d6d4ad0663dc91a448139d9bac4fecab43a41b30Elliott Hughes  bool "load_policy"
9d6d4ad0663dc91a448139d9bac4fecab43a41b30Elliott Hughes  depends on TOYBOX_SELINUX
109c25bcb645c754f4c09d9d6b8da41bbded862f6cRob Landley  default y
11d6d4ad0663dc91a448139d9bac4fecab43a41b30Elliott Hughes  help
12d6d4ad0663dc91a448139d9bac4fecab43a41b30Elliott Hughes    usage: load_policy FILE
13d6d4ad0663dc91a448139d9bac4fecab43a41b30Elliott Hughes
14d6d4ad0663dc91a448139d9bac4fecab43a41b30Elliott Hughes    Load the specified policy file.
15d6d4ad0663dc91a448139d9bac4fecab43a41b30Elliott Hughes*/
16d6d4ad0663dc91a448139d9bac4fecab43a41b30Elliott Hughes
17d6d4ad0663dc91a448139d9bac4fecab43a41b30Elliott Hughes#define FOR_load_policy
18d6d4ad0663dc91a448139d9bac4fecab43a41b30Elliott Hughes#include "toys.h"
19d6d4ad0663dc91a448139d9bac4fecab43a41b30Elliott Hughes
20d6d4ad0663dc91a448139d9bac4fecab43a41b30Elliott Hughesvoid load_policy_main(void)
21d6d4ad0663dc91a448139d9bac4fecab43a41b30Elliott Hughes{
22d6d4ad0663dc91a448139d9bac4fecab43a41b30Elliott Hughes  char *path = *toys.optargs;
23f697e055e906e35908cac0a356d986e466765de6Elliott Hughes  int fd = xopen(path, O_RDONLY);
24f697e055e906e35908cac0a356d986e466765de6Elliott Hughes  off_t policy_len = fdlength(fd);
25f697e055e906e35908cac0a356d986e466765de6Elliott Hughes  char *policy_data = mmap(0, policy_len, PROT_READ, MAP_PRIVATE, fd, 0);
26d6d4ad0663dc91a448139d9bac4fecab43a41b30Elliott Hughes
27f697e055e906e35908cac0a356d986e466765de6Elliott Hughes  close(fd);
28d410645aec7d51d49884a05863cf10497e43f0bcRob Landley  if (!policy_data || security_load_policy(policy_data, policy_len) < 0)
29f697e055e906e35908cac0a356d986e466765de6Elliott Hughes    perror_exit("Couldn't %s %s", policy_data ? "load" : "read", path);
30d6d4ad0663dc91a448139d9bac4fecab43a41b30Elliott Hughes
31d6d4ad0663dc91a448139d9bac4fecab43a41b30Elliott Hughes  munmap(policy_data, policy_len);
32d6d4ad0663dc91a448139d9bac4fecab43a41b30Elliott Hughes}
33