chcon.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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: "[-chfRv] CONTEXT FILE..."
  20. //usage: "\n chcon [-chfRv] [-u USER] [-r ROLE] [-l RANGE] [-t TYPE] FILE..."
  21. //usage: IF_LONG_OPTS(
  22. //usage: "\n chcon [-chfRv] --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 RANGE"
  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(struct recursive_state *state UNUSED_PARAM,
  58. const char *fname,
  59. struct stat *stbuf UNUSED_PARAM)
  60. {
  61. context_t context = NULL;
  62. security_context_t file_context = NULL;
  63. security_context_t context_string;
  64. int rc = FALSE;
  65. int status = 0;
  66. if (option_mask32 & OPT_NODEREFERENCE) {
  67. status = lgetfilecon(fname, &file_context);
  68. } else {
  69. status = getfilecon(fname, &file_context);
  70. }
  71. if (status < 0 && errno != ENODATA) {
  72. if ((option_mask32 & OPT_QUIET) == 0)
  73. bb_error_msg("can't obtain security context: %s", fname);
  74. goto skip;
  75. }
  76. if (file_context == NULL && specified_context == NULL) {
  77. bb_error_msg("can't apply partial context to unlabeled file %s", fname);
  78. goto skip;
  79. }
  80. if (specified_context == NULL) {
  81. context = set_security_context_component(file_context,
  82. user, role, type, range);
  83. if (!context) {
  84. bb_error_msg("can't compute security context from %s", file_context);
  85. goto skip;
  86. }
  87. } else {
  88. context = context_new(specified_context);
  89. if (!context) {
  90. bb_error_msg("invalid context: %s", specified_context);
  91. goto skip;
  92. }
  93. }
  94. context_string = context_str(context);
  95. if (!context_string) {
  96. bb_simple_error_msg("can't obtain security context in text expression");
  97. goto skip;
  98. }
  99. if (file_context == NULL || strcmp(context_string, file_context) != 0) {
  100. int fail;
  101. if (option_mask32 & OPT_NODEREFERENCE) {
  102. fail = lsetfilecon(fname, context_string);
  103. } else {
  104. fail = setfilecon(fname, context_string);
  105. }
  106. if ((option_mask32 & OPT_VERBOSE) || ((option_mask32 & OPT_CHANHES) && !fail)) {
  107. printf(!fail
  108. ? "context of %s changed to %s\n"
  109. : "can't change context of %s to %s\n",
  110. fname, context_string);
  111. }
  112. if (!fail) {
  113. rc = TRUE;
  114. } else if ((option_mask32 & OPT_QUIET) == 0) {
  115. bb_error_msg("can't change context of %s to %s",
  116. fname, context_string);
  117. }
  118. } else {
  119. if (option_mask32 & OPT_VERBOSE) {
  120. printf("context of %s retained as %s\n", fname, context_string);
  121. }
  122. rc = TRUE;
  123. }
  124. skip:
  125. context_free(context);
  126. freecon(file_context);
  127. return rc;
  128. }
  129. #if ENABLE_LONG_OPTS
  130. static const char chcon_longopts[] ALIGN1 =
  131. "recursive\0" No_argument "R"
  132. "changes\0" No_argument "c"
  133. "no-dereference\0" No_argument "h"
  134. "silent\0" No_argument "f"
  135. "quiet\0" No_argument "f"
  136. "user\0" Required_argument "u"
  137. "role\0" Required_argument "r"
  138. "type\0" Required_argument "t"
  139. "range\0" Required_argument "l"
  140. "verbose\0" No_argument "v"
  141. "reference\0" Required_argument "\xff" /* no short option */
  142. ;
  143. #endif
  144. int chcon_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  145. int chcon_main(int argc UNUSED_PARAM, char **argv)
  146. {
  147. char *reference_file;
  148. char *fname;
  149. int i, errors = 0;
  150. getopt32long(argv, "^"
  151. "Rchfu:r:t:l:v"
  152. "\0"
  153. "-1" /* at least 1 arg */
  154. ":?" /* error if exclusivity constraints are violated */
  155. #if ENABLE_LONG_OPTS
  156. ":\xff--urtl:u--\xff:r--\xff:t--\xff:l--\xff"
  157. #endif
  158. ":f--v:v--f" /* 'verbose' and 'quiet' are exclusive */
  159. , chcon_longopts,
  160. &user, &role, &type, &range, &reference_file
  161. );
  162. argv += optind;
  163. #if ENABLE_LONG_OPTS
  164. if (option_mask32 & OPT_REFERENCE) {
  165. /* FIXME: lgetfilecon() should be used when '-h' is specified.
  166. * But current implementation follows the original one. */
  167. if (getfilecon(reference_file, &specified_context) < 0)
  168. bb_perror_msg_and_die("getfilecon('%s') failed", reference_file);
  169. } else
  170. #endif
  171. if ((option_mask32 & OPT_COMPONENT_SPECIFIED) == 0) {
  172. specified_context = *argv++;
  173. /* specified_context is never NULL -
  174. * "-1" in opt_complementary prevents this. */
  175. if (!argv[0])
  176. bb_simple_error_msg_and_die("too few arguments");
  177. }
  178. for (i = 0; (fname = argv[i]) != NULL; i++) {
  179. int fname_len = strlen(fname);
  180. while (fname_len > 1 && fname[fname_len - 1] == '/')
  181. fname_len--;
  182. fname[fname_len] = '\0';
  183. if (recursive_action(fname,
  184. ((option_mask32 & OPT_RECURSIVE) ? ACTION_RECURSE : 0),
  185. change_filedir_context,
  186. change_filedir_context,
  187. NULL) != TRUE)
  188. errors = 1;
  189. }
  190. return errors;
  191. }