3
0

chcon.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. #include <getopt.h>
  9. #include <selinux/context.h>
  10. #include "libbb.h"
  11. #define OPT_RECURSIVE (1<<0) /* 'R' */
  12. #define OPT_CHANHES (1<<1) /* 'c' */
  13. #define OPT_NODEREFERENCE (1<<2) /* 'h' */
  14. #define OPT_QUIET (1<<3) /* 'f' */
  15. #define OPT_USER (1<<4) /* 'u' */
  16. #define OPT_ROLE (1<<5) /* 'r' */
  17. #define OPT_TYPE (1<<6) /* 't' */
  18. #define OPT_RANGE (1<<7) /* 'l' */
  19. #define OPT_VERBOSE (1<<8) /* 'v' */
  20. #define OPT_REFERENCE ((1<<9) * ENABLE_FEATURE_CHCON_LONG_OPTIONS)
  21. #define OPT_COMPONENT_SPECIFIED (OPT_USER | OPT_ROLE | OPT_TYPE | OPT_RANGE)
  22. static char *user = NULL;
  23. static char *role = NULL;
  24. static char *type = NULL;
  25. static char *range = NULL;
  26. static char *specified_context = NULL;
  27. static int change_filedir_context(
  28. const char *fname,
  29. struct stat *stbuf ATTRIBUTE_UNUSED,
  30. void *userData ATTRIBUTE_UNUSED,
  31. int depth ATTRIBUTE_UNUSED)
  32. {
  33. context_t context = NULL;
  34. security_context_t file_context = NULL;
  35. security_context_t context_string;
  36. int rc = FALSE;
  37. int status = 0;
  38. if (option_mask32 & OPT_NODEREFERENCE) {
  39. status = lgetfilecon(fname, &file_context);
  40. } else {
  41. status = getfilecon(fname, &file_context);
  42. }
  43. if (status < 0 && errno != ENODATA) {
  44. if ((option_mask32 & OPT_QUIET) == 0)
  45. bb_error_msg("cannot obtain security context: %s", fname);
  46. goto skip;
  47. }
  48. if (file_context == NULL && specified_context == NULL) {
  49. bb_error_msg("cannot apply partial context to unlabeled file %s", fname);
  50. goto skip;
  51. }
  52. if (specified_context == NULL) {
  53. context = set_security_context_component(file_context,
  54. user, role, type, range);
  55. if (!context) {
  56. bb_error_msg("cannot compute security context from %s", file_context);
  57. goto skip;
  58. }
  59. } else {
  60. context = context_new(specified_context);
  61. if (!context) {
  62. bb_error_msg("invalid context: %s", specified_context);
  63. goto skip;
  64. }
  65. }
  66. context_string = context_str(context);
  67. if (!context_string) {
  68. bb_error_msg("cannot obtain security context in text expression");
  69. goto skip;
  70. }
  71. if (file_context == NULL || strcmp(context_string, file_context) != 0) {
  72. int fail;
  73. if (option_mask32 & OPT_NODEREFERENCE) {
  74. fail = lsetfilecon(fname, context_string);
  75. } else {
  76. fail = setfilecon(fname, context_string);
  77. }
  78. if ((option_mask32 & OPT_VERBOSE) || ((option_mask32 & OPT_CHANHES) && !fail)) {
  79. printf(!fail
  80. ? "context of %s changed to %s\n"
  81. : "failed to change context of %s to %s\n",
  82. fname, context_string);
  83. }
  84. if (!fail) {
  85. rc = TRUE;
  86. } else if ((option_mask32 & OPT_QUIET) == 0) {
  87. bb_error_msg("failed to change context of %s to %s",
  88. fname, context_string);
  89. }
  90. } else if (option_mask32 & OPT_VERBOSE) {
  91. printf("context of %s retained as %s\n", fname, context_string);
  92. rc = TRUE;
  93. }
  94. skip:
  95. context_free(context);
  96. freecon(file_context);
  97. return rc;
  98. }
  99. #if ENABLE_FEATURE_CHCON_LONG_OPTIONS
  100. static const char chcon_longopts[] ALIGN1 =
  101. "recursive\0" No_argument "R"
  102. "changes\0" No_argument "c"
  103. "no-dereference\0" No_argument "h"
  104. "silent\0" No_argument "f"
  105. "quiet\0" No_argument "f"
  106. "user\0" Required_argument "u"
  107. "role\0" Required_argument "r"
  108. "type\0" Required_argument "t"
  109. "range\0" Required_argument "l"
  110. "verbose\0" No_argument "v"
  111. "reference\0" Required_argument "\xff" /* no short option */
  112. ;
  113. #endif
  114. int chcon_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  115. int chcon_main(int argc ATTRIBUTE_UNUSED, char **argv)
  116. {
  117. char *reference_file;
  118. char *fname;
  119. int i, errors = 0;
  120. #if ENABLE_FEATURE_CHCON_LONG_OPTIONS
  121. applet_long_options = chcon_longopts;
  122. #endif
  123. opt_complementary = "-1" /* at least 1 param */
  124. ":?" /* error if exclusivity constraints are violated */
  125. #if ENABLE_FEATURE_CHCON_LONG_OPTIONS
  126. ":\xff--urtl:u--\xff:r--\xff:t--\xff:l--\xff"
  127. #endif
  128. ":f--v:v--f"; /* 'verbose' and 'quiet' are exclusive */
  129. getopt32(argv, "Rchfu:r:t:l:v",
  130. &user, &role, &type, &range, &reference_file);
  131. argv += optind;
  132. #if ENABLE_FEATURE_CHCON_LONG_OPTIONS
  133. if (option_mask32 & OPT_REFERENCE) {
  134. /* FIXME: lgetfilecon() should be used when '-h' is specified.
  135. But current implementation follows the original one. */
  136. if (getfilecon(reference_file, &specified_context) < 0)
  137. bb_perror_msg_and_die("getfilecon('%s') failed", reference_file);
  138. } else
  139. #endif
  140. if ((option_mask32 & OPT_COMPONENT_SPECIFIED) == 0) {
  141. specified_context = *argv++;
  142. /* specified_context is never NULL -
  143. * "-1" in opt_complementary prevents this. */
  144. if (!argv[0])
  145. bb_error_msg_and_die("too few arguments");
  146. }
  147. for (i = 0; (fname = argv[i]) != NULL; i++) {
  148. int fname_len = strlen(fname);
  149. while (fname_len > 1 && fname[fname_len - 1] == '/')
  150. fname_len--;
  151. fname[fname_len] = '\0';
  152. if (recursive_action(fname,
  153. 1<<option_mask32 & OPT_RECURSIVE,
  154. change_filedir_context,
  155. change_filedir_context,
  156. NULL, 0) != TRUE)
  157. errors = 1;
  158. }
  159. return errors;
  160. }