chcon.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*
  2. * chcon -- change security context, based on coreutils-5.97-13
  3. *
  4. * Port to busybox: KaiGai Kohei <kaigai@kaigai.gr.jp>
  5. *
  6. * Copyright (C) 2006 - 2007 KaiGai Kohei <kaigai@kaigai.gr.jp>
  7. *
  8. * Licensed under GPLv2, see file LICENSE in this source tree.
  9. */
  10. //config:config CHCON
  11. //config: bool "chcon"
  12. //config: default n
  13. //config: depends on SELINUX
  14. //config: help
  15. //config: Enable support to change the security context of file.
  16. //config:
  17. //config:config FEATURE_CHCON_LONG_OPTIONS
  18. //config: bool "Enable long options"
  19. //config: default y
  20. //config: depends on CHCON && LONG_OPTS
  21. //config: help
  22. //config: Support long options for the chcon applet.
  23. //applet:IF_CHCON(APPLET(chcon, BB_DIR_USR_BIN, BB_SUID_DROP))
  24. //kbuild:lib-$(CONFIG_CHCON) += chcon.o
  25. //usage:#define chcon_trivial_usage
  26. //usage: "[OPTIONS] CONTEXT FILE..."
  27. //usage: "\n chcon [OPTIONS] [-u USER] [-r ROLE] [-l RANGE] [-t TYPE] FILE..."
  28. //usage: IF_FEATURE_CHCON_LONG_OPTIONS(
  29. //usage: "\n chcon [OPTIONS] --reference=RFILE FILE..."
  30. //usage: )
  31. //usage:#define chcon_full_usage "\n\n"
  32. //usage: "Change the security context of each FILE to CONTEXT\n"
  33. //usage: IF_FEATURE_CHCON_LONG_OPTIONS(
  34. //usage: "\n -v,--verbose Verbose"
  35. //usage: "\n -c,--changes Report changes made"
  36. //usage: "\n -h,--no-dereference Affect symlinks instead of their targets"
  37. //usage: "\n -f,--silent,--quiet Suppress most error messages"
  38. //usage: "\n --reference=RFILE Use RFILE's group instead of using a CONTEXT value"
  39. //usage: "\n -u,--user=USER Set user/role/type/range in the target"
  40. //usage: "\n -r,--role=ROLE security context"
  41. //usage: "\n -t,--type=TYPE"
  42. //usage: "\n -l,--range=RANGE"
  43. //usage: "\n -R,--recursive Recurse"
  44. //usage: )
  45. //usage: IF_NOT_FEATURE_CHCON_LONG_OPTIONS(
  46. //usage: "\n -v Verbose"
  47. //usage: "\n -c Report changes made"
  48. //usage: "\n -h Affect symlinks instead of their targets"
  49. //usage: "\n -f Suppress most error messages"
  50. //usage: "\n -u USER Set user/role/type/range in the target security context"
  51. //usage: "\n -r ROLE"
  52. //usage: "\n -t TYPE"
  53. //usage: "\n -l RNG"
  54. //usage: "\n -R Recurse"
  55. //usage: )
  56. #include <selinux/context.h>
  57. #include "libbb.h"
  58. #define OPT_RECURSIVE (1<<0) /* 'R' */
  59. #define OPT_CHANHES (1<<1) /* 'c' */
  60. #define OPT_NODEREFERENCE (1<<2) /* 'h' */
  61. #define OPT_QUIET (1<<3) /* 'f' */
  62. #define OPT_USER (1<<4) /* 'u' */
  63. #define OPT_ROLE (1<<5) /* 'r' */
  64. #define OPT_TYPE (1<<6) /* 't' */
  65. #define OPT_RANGE (1<<7) /* 'l' */
  66. #define OPT_VERBOSE (1<<8) /* 'v' */
  67. #define OPT_REFERENCE ((1<<9) * ENABLE_FEATURE_CHCON_LONG_OPTIONS)
  68. #define OPT_COMPONENT_SPECIFIED (OPT_USER | OPT_ROLE | OPT_TYPE | OPT_RANGE)
  69. static char *user = NULL;
  70. static char *role = NULL;
  71. static char *type = NULL;
  72. static char *range = NULL;
  73. static char *specified_context = NULL;
  74. static int FAST_FUNC change_filedir_context(
  75. const char *fname,
  76. struct stat *stbuf UNUSED_PARAM,
  77. void *userData UNUSED_PARAM,
  78. int depth UNUSED_PARAM)
  79. {
  80. context_t context = NULL;
  81. security_context_t file_context = NULL;
  82. security_context_t context_string;
  83. int rc = FALSE;
  84. int status = 0;
  85. if (option_mask32 & OPT_NODEREFERENCE) {
  86. status = lgetfilecon(fname, &file_context);
  87. } else {
  88. status = getfilecon(fname, &file_context);
  89. }
  90. if (status < 0 && errno != ENODATA) {
  91. if ((option_mask32 & OPT_QUIET) == 0)
  92. bb_error_msg("can't obtain security context: %s", fname);
  93. goto skip;
  94. }
  95. if (file_context == NULL && specified_context == NULL) {
  96. bb_error_msg("can't apply partial context to unlabeled file %s", fname);
  97. goto skip;
  98. }
  99. if (specified_context == NULL) {
  100. context = set_security_context_component(file_context,
  101. user, role, type, range);
  102. if (!context) {
  103. bb_error_msg("can't compute security context from %s", file_context);
  104. goto skip;
  105. }
  106. } else {
  107. context = context_new(specified_context);
  108. if (!context) {
  109. bb_error_msg("invalid context: %s", specified_context);
  110. goto skip;
  111. }
  112. }
  113. context_string = context_str(context);
  114. if (!context_string) {
  115. bb_error_msg("can't obtain security context in text expression");
  116. goto skip;
  117. }
  118. if (file_context == NULL || strcmp(context_string, file_context) != 0) {
  119. int fail;
  120. if (option_mask32 & OPT_NODEREFERENCE) {
  121. fail = lsetfilecon(fname, context_string);
  122. } else {
  123. fail = setfilecon(fname, context_string);
  124. }
  125. if ((option_mask32 & OPT_VERBOSE) || ((option_mask32 & OPT_CHANHES) && !fail)) {
  126. printf(!fail
  127. ? "context of %s changed to %s\n"
  128. : "can't change context of %s to %s\n",
  129. fname, context_string);
  130. }
  131. if (!fail) {
  132. rc = TRUE;
  133. } else if ((option_mask32 & OPT_QUIET) == 0) {
  134. bb_error_msg("can't change context of %s to %s",
  135. fname, context_string);
  136. }
  137. } else if (option_mask32 & OPT_VERBOSE) {
  138. printf("context of %s retained as %s\n", fname, context_string);
  139. rc = TRUE;
  140. }
  141. skip:
  142. context_free(context);
  143. freecon(file_context);
  144. return rc;
  145. }
  146. #if ENABLE_FEATURE_CHCON_LONG_OPTIONS
  147. static const char chcon_longopts[] ALIGN1 =
  148. "recursive\0" No_argument "R"
  149. "changes\0" No_argument "c"
  150. "no-dereference\0" No_argument "h"
  151. "silent\0" No_argument "f"
  152. "quiet\0" No_argument "f"
  153. "user\0" Required_argument "u"
  154. "role\0" Required_argument "r"
  155. "type\0" Required_argument "t"
  156. "range\0" Required_argument "l"
  157. "verbose\0" No_argument "v"
  158. "reference\0" Required_argument "\xff" /* no short option */
  159. ;
  160. #endif
  161. int chcon_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  162. int chcon_main(int argc UNUSED_PARAM, char **argv)
  163. {
  164. char *reference_file;
  165. char *fname;
  166. int i, errors = 0;
  167. #if ENABLE_FEATURE_CHCON_LONG_OPTIONS
  168. applet_long_options = chcon_longopts;
  169. #endif
  170. opt_complementary = "-1" /* at least 1 param */
  171. ":?" /* error if exclusivity constraints are violated */
  172. #if ENABLE_FEATURE_CHCON_LONG_OPTIONS
  173. ":\xff--urtl:u--\xff:r--\xff:t--\xff:l--\xff"
  174. #endif
  175. ":f--v:v--f"; /* 'verbose' and 'quiet' are exclusive */
  176. getopt32(argv, "Rchfu:r:t:l:v",
  177. &user, &role, &type, &range, &reference_file);
  178. argv += optind;
  179. #if ENABLE_FEATURE_CHCON_LONG_OPTIONS
  180. if (option_mask32 & OPT_REFERENCE) {
  181. /* FIXME: lgetfilecon() should be used when '-h' is specified.
  182. * But current implementation follows the original one. */
  183. if (getfilecon(reference_file, &specified_context) < 0)
  184. bb_perror_msg_and_die("getfilecon('%s') failed", reference_file);
  185. } else
  186. #endif
  187. if ((option_mask32 & OPT_COMPONENT_SPECIFIED) == 0) {
  188. specified_context = *argv++;
  189. /* specified_context is never NULL -
  190. * "-1" in opt_complementary prevents this. */
  191. if (!argv[0])
  192. bb_error_msg_and_die("too few arguments");
  193. }
  194. for (i = 0; (fname = argv[i]) != NULL; i++) {
  195. int fname_len = strlen(fname);
  196. while (fname_len > 1 && fname[fname_len - 1] == '/')
  197. fname_len--;
  198. fname[fname_len] = '\0';
  199. if (recursive_action(fname,
  200. 1<<option_mask32 & OPT_RECURSIVE,
  201. change_filedir_context,
  202. change_filedir_context,
  203. NULL, 0) != TRUE)
  204. errors = 1;
  205. }
  206. return errors;
  207. }