3
0

chmod.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 source tree.
  11. */
  12. //config:config CHMOD
  13. //config: bool "chmod (5.1 kb)"
  14. //config: default y
  15. //config: help
  16. //config: chmod is used to change the access permission of files.
  17. //applet:IF_CHMOD(APPLET_NOEXEC(chmod, chmod, BB_DIR_BIN, BB_SUID_DROP, chmod))
  18. //kbuild:lib-$(CONFIG_CHMOD) += chmod.o
  19. /* BB_AUDIT SUSv3 compliant */
  20. /* BB_AUDIT GNU defects - unsupported long options. */
  21. /* http://www.opengroup.org/onlinepubs/007904975/utilities/chmod.html */
  22. //usage:#define chmod_trivial_usage
  23. //usage: "[-R"IF_DESKTOP("cvf")"] MODE[,MODE]... FILE..."
  24. //usage:#define chmod_full_usage "\n\n"
  25. //usage: "Each MODE is one or more of the letters ugoa, one of the\n"
  26. //usage: "symbols +-= and one or more of the letters rwxst\n"
  27. //usage: "\n -R Recurse"
  28. //usage: IF_DESKTOP(
  29. //usage: "\n -c List changed files"
  30. //usage: "\n -v List all files"
  31. //usage: "\n -f Hide errors"
  32. //usage: )
  33. //usage:
  34. //usage:#define chmod_example_usage
  35. //usage: "$ ls -l /tmp/foo\n"
  36. //usage: "-rw-rw-r-- 1 root root 0 Apr 12 18:25 /tmp/foo\n"
  37. //usage: "$ chmod u+x /tmp/foo\n"
  38. //usage: "$ ls -l /tmp/foo\n"
  39. //usage: "-rwxrw-r-- 1 root root 0 Apr 12 18:25 /tmp/foo*\n"
  40. //usage: "$ chmod 444 /tmp/foo\n"
  41. //usage: "$ ls -l /tmp/foo\n"
  42. //usage: "-r--r--r-- 1 root root 0 Apr 12 18:25 /tmp/foo\n"
  43. #include "libbb.h"
  44. /* This is a NOEXEC applet. Be very careful! */
  45. #define OPT_RECURSE (option_mask32 & 1)
  46. #define OPT_VERBOSE (IF_DESKTOP(option_mask32 & 2) IF_NOT_DESKTOP(0))
  47. #define OPT_CHANGED (IF_DESKTOP(option_mask32 & 4) IF_NOT_DESKTOP(0))
  48. #define OPT_QUIET (IF_DESKTOP(option_mask32 & 8) IF_NOT_DESKTOP(0))
  49. #define OPT_STR "R" IF_DESKTOP("vcf")
  50. /* coreutils:
  51. * chmod never changes the permissions of symbolic links; the chmod
  52. * system call cannot change their permissions. This is not a problem
  53. * since the permissions of symbolic links are never used.
  54. * However, for each symbolic link listed on the command line, chmod changes
  55. * the permissions of the pointed-to file. In contrast, chmod ignores
  56. * symbolic links encountered during recursive directory traversals.
  57. */
  58. static int FAST_FUNC fileAction(const char *fileName, struct stat *statbuf, void* param, int depth)
  59. {
  60. mode_t newmode;
  61. /* match coreutils behavior */
  62. if (depth == 0) {
  63. /* statbuf holds lstat result, but we need stat (follow link) */
  64. if (stat(fileName, statbuf))
  65. goto err;
  66. } else { /* depth > 0: skip links */
  67. if (S_ISLNK(statbuf->st_mode))
  68. return TRUE;
  69. }
  70. newmode = bb_parse_mode((char *)param, statbuf->st_mode);
  71. if (newmode == (mode_t)-1)
  72. bb_error_msg_and_die("invalid mode '%s'", (char *)param);
  73. if (chmod(fileName, newmode) == 0) {
  74. if (OPT_VERBOSE
  75. || (OPT_CHANGED && statbuf->st_mode != newmode)
  76. ) {
  77. printf("mode of '%s' changed to %04o (%s)\n", fileName,
  78. newmode & 07777, bb_mode_string(newmode)+1);
  79. }
  80. return TRUE;
  81. }
  82. err:
  83. if (!OPT_QUIET)
  84. bb_simple_perror_msg(fileName);
  85. return FALSE;
  86. }
  87. int chmod_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  88. int chmod_main(int argc UNUSED_PARAM, char **argv)
  89. {
  90. int retval = EXIT_SUCCESS;
  91. char *arg, **argp;
  92. char *smode;
  93. /* Convert first encountered -r into ar, -w into aw etc
  94. * so that getopt would not eat it */
  95. argp = argv;
  96. while ((arg = *++argp)) {
  97. /* Mode spec must be the first arg (sans -R etc) */
  98. /* (protect against mishandling e.g. "chmod 644 -r") */
  99. if (arg[0] != '-') {
  100. arg = NULL;
  101. break;
  102. }
  103. /* An option. Not a -- or valid option? */
  104. if (arg[1] && !strchr("-"OPT_STR, arg[1])) {
  105. arg[0] = 'a';
  106. break;
  107. }
  108. }
  109. /* Parse options */
  110. getopt32(argv, "^" OPT_STR "\0" "-2");
  111. argv += optind;
  112. /* Restore option-like mode if needed */
  113. if (arg) arg[0] = '-';
  114. /* Ok, ready to do the deed now */
  115. smode = *argv++;
  116. do {
  117. if (!recursive_action(*argv,
  118. OPT_RECURSE, // recurse
  119. fileAction, // file action
  120. fileAction, // dir action
  121. smode, // user data
  122. 0) // depth
  123. ) {
  124. retval = EXIT_FAILURE;
  125. }
  126. } while (*++argv);
  127. return retval;
  128. }
  129. /*
  130. Security: chmod is too important and too subtle.
  131. This is a test script (busybox chmod versus coreutils).
  132. Run it in empty directory.
  133. #!/bin/sh
  134. t1="/tmp/busybox chmod"
  135. t2="/usr/bin/chmod"
  136. create() {
  137. rm -rf $1; mkdir $1
  138. (
  139. cd $1 || exit 1
  140. mkdir dir
  141. >up
  142. >file
  143. >dir/file
  144. ln -s dir linkdir
  145. ln -s file linkfile
  146. ln -s ../up dir/up
  147. )
  148. }
  149. tst() {
  150. (cd test1; $t1 $1)
  151. (cd test2; $t2 $1)
  152. (cd test1; ls -lR) >out1
  153. (cd test2; ls -lR) >out2
  154. echo "chmod $1" >out.diff
  155. if ! diff -u out1 out2 >>out.diff; then exit 1; fi
  156. rm out.diff
  157. }
  158. echo "If script produced 'out.diff' file, then at least one testcase failed"
  159. create test1; create test2
  160. tst "a+w file"
  161. tst "a-w dir"
  162. tst "a+w linkfile"
  163. tst "a-w linkdir"
  164. tst "-R a+w file"
  165. tst "-R a-w dir"
  166. tst "-R a+w linkfile"
  167. tst "-R a-w linkdir"
  168. tst "a-r,a+x linkfile"
  169. */