chcon.c 5.0 KB

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