chcon.c 6.1 KB

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