chmod.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Mini chmod implementation for busybox
  4. *
  5. * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
  6. *
  7. * Reworked by (C) 2002 Vladimir Oleynik <dzo@simtreas.ru>
  8. * to correctly parse '-rwxgoa'
  9. *
  10. * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  11. */
  12. /* BB_AUDIT SUSv3 compliant */
  13. /* BB_AUDIT GNU defects - unsupported long options. */
  14. /* http://www.opengroup.org/onlinepubs/007904975/utilities/chmod.html */
  15. #include "libbb.h"
  16. /* This is a NOEXEC applet. Be very careful! */
  17. #define OPT_RECURSE (option_mask32 & 1)
  18. #define OPT_VERBOSE (USE_DESKTOP(option_mask32 & 2) SKIP_DESKTOP(0))
  19. #define OPT_CHANGED (USE_DESKTOP(option_mask32 & 4) SKIP_DESKTOP(0))
  20. #define OPT_QUIET (USE_DESKTOP(option_mask32 & 8) SKIP_DESKTOP(0))
  21. #define OPT_STR "R" USE_DESKTOP("vcf")
  22. /* coreutils:
  23. * chmod never changes the permissions of symbolic links; the chmod
  24. * system call cannot change their permissions. This is not a problem
  25. * since the permissions of symbolic links are never used.
  26. * However, for each symbolic link listed on the command line, chmod changes
  27. * the permissions of the pointed-to file. In contrast, chmod ignores
  28. * symbolic links encountered during recursive directory traversals.
  29. */
  30. static int FAST_FUNC fileAction(const char *fileName, struct stat *statbuf, void* param, int depth)
  31. {
  32. mode_t newmode;
  33. /* match coreutils behavior */
  34. if (depth == 0) {
  35. /* statbuf holds lstat result, but we need stat (follow link) */
  36. if (stat(fileName, statbuf))
  37. goto err;
  38. } else { /* depth > 0: skip links */
  39. if (S_ISLNK(statbuf->st_mode))
  40. return TRUE;
  41. }
  42. newmode = statbuf->st_mode;
  43. if (!bb_parse_mode((char *)param, &newmode))
  44. bb_error_msg_and_die("invalid mode: %s", (char *)param);
  45. if (chmod(fileName, newmode) == 0) {
  46. if (OPT_VERBOSE
  47. || (OPT_CHANGED && statbuf->st_mode != newmode)
  48. ) {
  49. printf("mode of '%s' changed to %04o (%s)\n", fileName,
  50. newmode & 07777, bb_mode_string(newmode)+1);
  51. }
  52. return TRUE;
  53. }
  54. err:
  55. if (!OPT_QUIET)
  56. bb_simple_perror_msg(fileName);
  57. return FALSE;
  58. }
  59. int chmod_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  60. int chmod_main(int argc UNUSED_PARAM, char **argv)
  61. {
  62. int retval = EXIT_SUCCESS;
  63. char *arg, **argp;
  64. char *smode;
  65. /* Convert first encountered -r into ar, -w into aw etc
  66. * so that getopt would not eat it */
  67. argp = argv;
  68. while ((arg = *++argp)) {
  69. /* Mode spec must be the first arg (sans -R etc) */
  70. /* (protect against mishandling e.g. "chmod 644 -r") */
  71. if (arg[0] != '-') {
  72. arg = NULL;
  73. break;
  74. }
  75. /* An option. Not a -- or valid option? */
  76. if (arg[1] && !strchr("-"OPT_STR, arg[1])) {
  77. arg[0] = 'a';
  78. break;
  79. }
  80. }
  81. /* Parse options */
  82. opt_complementary = "-2";
  83. getopt32(argv, ("-"OPT_STR) + 1); /* Reuse string */
  84. argv += optind;
  85. /* Restore option-like mode if needed */
  86. if (arg) arg[0] = '-';
  87. /* Ok, ready to do the deed now */
  88. smode = *argv++;
  89. do {
  90. if (!recursive_action(*argv,
  91. OPT_RECURSE, // recurse
  92. fileAction, // file action
  93. fileAction, // dir action
  94. smode, // user data
  95. 0) // depth
  96. ) {
  97. retval = EXIT_FAILURE;
  98. }
  99. } while (*++argv);
  100. return retval;
  101. }
  102. /*
  103. Security: chmod is too important and too subtle.
  104. This is a test script (busybox chmod versus coreutils).
  105. Run it in empty directory.
  106. #!/bin/sh
  107. t1="/tmp/busybox chmod"
  108. t2="/usr/bin/chmod"
  109. create() {
  110. rm -rf $1; mkdir $1
  111. (
  112. cd $1 || exit 1
  113. mkdir dir
  114. >up
  115. >file
  116. >dir/file
  117. ln -s dir linkdir
  118. ln -s file linkfile
  119. ln -s ../up dir/up
  120. )
  121. }
  122. tst() {
  123. (cd test1; $t1 $1)
  124. (cd test2; $t2 $1)
  125. (cd test1; ls -lR) >out1
  126. (cd test2; ls -lR) >out2
  127. echo "chmod $1" >out.diff
  128. if ! diff -u out1 out2 >>out.diff; then exit 1; fi
  129. rm out.diff
  130. }
  131. echo "If script produced 'out.diff' file, then at least one testcase failed"
  132. create test1; create test2
  133. tst "a+w file"
  134. tst "a-w dir"
  135. tst "a+w linkfile"
  136. tst "a-w linkdir"
  137. tst "-R a+w file"
  138. tst "-R a-w dir"
  139. tst "-R a+w linkfile"
  140. tst "-R a-w linkdir"
  141. tst "a-r,a+x linkfile"
  142. */