chmod.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 "busybox.h"
  16. #define OPT_RECURSE (option_mask32 & 1)
  17. #define OPT_VERBOSE (USE_DESKTOP(option_mask32 & 2) SKIP_DESKTOP(0))
  18. #define OPT_CHANGED (USE_DESKTOP(option_mask32 & 4) SKIP_DESKTOP(0))
  19. #define OPT_QUIET (USE_DESKTOP(option_mask32 & 8) SKIP_DESKTOP(0))
  20. #define OPT_STR "R" USE_DESKTOP("vcf")
  21. /* coreutils:
  22. * chmod never changes the permissions of symbolic links; the chmod
  23. * system call cannot change their permissions. This is not a problem
  24. * since the permissions of symbolic links are never used.
  25. * However, for each symbolic link listed on the command line, chmod changes
  26. * the permissions of the pointed-to file. In contrast, chmod ignores
  27. * symbolic links encountered during recursive directory traversals.
  28. */
  29. static int fileAction(const char *fileName, struct stat *statbuf, void* junk, int depth)
  30. {
  31. mode_t newmode;
  32. /* match coreutils behavior */
  33. if (depth == 0) {
  34. /* statbuf holds lstat result, but we need stat (follow link) */
  35. if (stat(fileName, statbuf))
  36. goto err;
  37. } else { /* depth > 0: skip links */
  38. if (S_ISLNK(statbuf->st_mode))
  39. return TRUE;
  40. }
  41. newmode = statbuf->st_mode;
  42. if (!bb_parse_mode((char *)junk, &newmode))
  43. bb_error_msg_and_die("invalid mode: %s", (char *)junk);
  44. if (chmod(fileName, newmode) == 0) {
  45. if (OPT_VERBOSE
  46. || (OPT_CHANGED && statbuf->st_mode != newmode)
  47. ) {
  48. printf("mode of '%s' changed to %04o (%s)\n", fileName,
  49. newmode & 07777, bb_mode_string(newmode)+1);
  50. }
  51. return TRUE;
  52. }
  53. err:
  54. if (!OPT_QUIET)
  55. bb_perror_msg("%s", fileName);
  56. return FALSE;
  57. }
  58. int chmod_main(int argc, char **argv)
  59. {
  60. int retval = EXIT_SUCCESS;
  61. char *arg, **argp;
  62. char *smode;
  63. /* Convert first encountered -r into ar, -w into aw etc
  64. * so that getopt would not eat it */
  65. argp = argv;
  66. while ((arg = *++argp)) {
  67. /* Mode spec must be the first arg (sans -R etc) */
  68. /* (protect against mishandling e.g. "chmod 644 -r") */
  69. if (arg[0] != '-') {
  70. arg = NULL;
  71. break;
  72. }
  73. /* An option. Not a -- or valid option? */
  74. if (arg[1] && !strchr("-"OPT_STR, arg[1])) {
  75. arg[0] = 'a';
  76. break;
  77. }
  78. }
  79. /* Parse options */
  80. opt_complementary = "-2";
  81. getopt32(argc, argv, ("-"OPT_STR) + 1); /* Reuse string */
  82. argv += optind;
  83. /* Restore option-like mode if needed */
  84. if (arg) arg[0] = '-';
  85. /* Ok, ready to do the deed now */
  86. smode = *argv++;
  87. do {
  88. if (!recursive_action(*argv,
  89. OPT_RECURSE, // recurse
  90. FALSE, // follow links: coreutils doesn't
  91. FALSE, // depth first
  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 dir. Probably requires bash.
  106. #!/bin/sh
  107. function create() {
  108. rm -rf $1; mkdir $1
  109. (
  110. cd $1 || exit 1
  111. mkdir dir
  112. >up
  113. >file
  114. >dir/file
  115. ln -s dir linkdir
  116. ln -s file linkfile
  117. ln -s ../up dir/up
  118. )
  119. }
  120. function tst() {
  121. (cd test1; $t1 $1)
  122. (cd test2; $t2 $1)
  123. (cd test1; ls -lR) >out1
  124. (cd test2; ls -lR) >out2
  125. echo "chmod $1" >out.diff
  126. if ! diff -u out1 out2 >>out.diff; then exit 1; fi
  127. mv out.diff out1.diff
  128. }
  129. t1="/tmp/busybox chmod"
  130. t2="/usr/bin/chmod"
  131. create test1; create test2
  132. tst "a+w file"
  133. tst "a-w dir"
  134. tst "a+w linkfile"
  135. tst "a-w linkdir"
  136. tst "-R a+w file"
  137. tst "-R a-w dir"
  138. tst "-R a+w linkfile"
  139. tst "-R a-w linkdir"
  140. tst "a-r,a+x linkfile"
  141. */