chown.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Mini chown implementation for busybox
  4. *
  5. * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
  6. *
  7. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  8. */
  9. //config:config CHOWN
  10. //config: bool "chown (7.2 kb)"
  11. //config: default y
  12. //config: help
  13. //config: chown is used to change the user and/or group ownership
  14. //config: of files.
  15. //config:
  16. //config:config FEATURE_CHOWN_LONG_OPTIONS
  17. //config: bool "Enable long options"
  18. //config: default y
  19. //config: depends on CHOWN && LONG_OPTS
  20. //applet:IF_CHOWN(APPLET_NOEXEC(chown, chown, BB_DIR_BIN, BB_SUID_DROP, chown))
  21. //kbuild:lib-$(CONFIG_CHOWN) += chown.o
  22. /* BB_AUDIT SUSv3 defects - none? */
  23. /* http://www.opengroup.org/onlinepubs/007904975/utilities/chown.html */
  24. //usage:#define chown_trivial_usage
  25. //usage: "[-Rh"IF_DESKTOP("LHPcvf")"]... USER[:[GRP]] FILE..."
  26. //usage:#define chown_full_usage "\n\n"
  27. //usage: "Change the owner and/or group of each FILE to USER and/or GRP\n"
  28. //usage: "\n -R Recurse"
  29. //usage: "\n -h Affect symlinks instead of symlink targets"
  30. //usage: IF_DESKTOP(
  31. //usage: "\n -L Traverse all symlinks to directories"
  32. //usage: "\n -H Traverse symlinks on command line only"
  33. //usage: "\n -P Don't traverse symlinks (default)"
  34. //usage: "\n -c List changed files"
  35. //usage: "\n -v List all files"
  36. //usage: "\n -f Hide errors"
  37. //usage: )
  38. //usage:
  39. //usage:#define chown_example_usage
  40. //usage: "$ ls -l /tmp/foo\n"
  41. //usage: "-r--r--r-- 1 andersen andersen 0 Apr 12 18:25 /tmp/foo\n"
  42. //usage: "$ chown root /tmp/foo\n"
  43. //usage: "$ ls -l /tmp/foo\n"
  44. //usage: "-r--r--r-- 1 root andersen 0 Apr 12 18:25 /tmp/foo\n"
  45. //usage: "$ chown root.root /tmp/foo\n"
  46. //usage: "ls -l /tmp/foo\n"
  47. //usage: "-r--r--r-- 1 root root 0 Apr 12 18:25 /tmp/foo\n"
  48. #include "libbb.h"
  49. /* This is a NOEXEC applet. Be very careful! */
  50. #define OPT_STR "Rh" IF_DESKTOP("vcfLHP")
  51. #define BIT_RECURSE 1
  52. #define OPT_RECURSE (opt & 1)
  53. #define OPT_NODEREF (opt & 2)
  54. #define OPT_VERBOSE (IF_DESKTOP(opt & 0x04) IF_NOT_DESKTOP(0))
  55. #define OPT_CHANGED (IF_DESKTOP(opt & 0x08) IF_NOT_DESKTOP(0))
  56. #define OPT_QUIET (IF_DESKTOP(opt & 0x10) IF_NOT_DESKTOP(0))
  57. /* POSIX options
  58. * -L traverse every symbolic link to a directory encountered
  59. * -H if a command line argument is a symbolic link to a directory, traverse it
  60. * -P do not traverse any symbolic links (default)
  61. * We do not conform to the following:
  62. * "Specifying more than one of -H, -L, and -P is not an error.
  63. * The last option specified shall determine the behavior of the utility." */
  64. /* -L */
  65. #define BIT_TRAVERSE 0x20
  66. #define OPT_TRAVERSE (IF_DESKTOP(opt & BIT_TRAVERSE) IF_NOT_DESKTOP(0))
  67. /* -H or -L */
  68. #define BIT_TRAVERSE_TOP (0x20|0x40)
  69. #define OPT_TRAVERSE_TOP (IF_DESKTOP(opt & BIT_TRAVERSE_TOP) IF_NOT_DESKTOP(0))
  70. #if ENABLE_FEATURE_CHOWN_LONG_OPTIONS
  71. static const char chown_longopts[] ALIGN1 =
  72. "recursive\0" No_argument "R"
  73. "dereference\0" No_argument "\xff"
  74. "no-dereference\0" No_argument "h"
  75. # if ENABLE_DESKTOP
  76. "changes\0" No_argument "c"
  77. "silent\0" No_argument "f"
  78. "quiet\0" No_argument "f"
  79. "verbose\0" No_argument "v"
  80. # endif
  81. ;
  82. #endif
  83. typedef int (*chown_fptr)(const char *, uid_t, gid_t);
  84. struct param_t {
  85. struct bb_uidgid_t ugid;
  86. chown_fptr chown_func;
  87. };
  88. static int FAST_FUNC fileAction(const char *fileName, struct stat *statbuf,
  89. void *vparam, int depth UNUSED_PARAM)
  90. {
  91. #define param (*(struct param_t*)vparam)
  92. #define opt option_mask32
  93. uid_t u = (param.ugid.uid == (uid_t)-1L) ? statbuf->st_uid : param.ugid.uid;
  94. gid_t g = (param.ugid.gid == (gid_t)-1L) ? statbuf->st_gid : param.ugid.gid;
  95. if (param.chown_func(fileName, u, g) == 0) {
  96. if (OPT_VERBOSE
  97. || (OPT_CHANGED && (statbuf->st_uid != u || statbuf->st_gid != g))
  98. ) {
  99. printf("changed ownership of '%s' to %u:%u\n",
  100. fileName, (unsigned)u, (unsigned)g);
  101. }
  102. return TRUE;
  103. }
  104. if (!OPT_QUIET)
  105. bb_simple_perror_msg(fileName);
  106. return FALSE;
  107. #undef opt
  108. #undef param
  109. }
  110. int chown_main(int argc UNUSED_PARAM, char **argv)
  111. {
  112. int retval = EXIT_SUCCESS;
  113. int opt, flags;
  114. struct param_t param;
  115. #if ENABLE_FEATURE_CHOWN_LONG_OPTIONS
  116. opt = getopt32long(argv, "^" OPT_STR "\0" "-2", chown_longopts);
  117. #else
  118. opt = getopt32(argv, "^" OPT_STR "\0" "-2");
  119. #endif
  120. argv += optind;
  121. /* This matches coreutils behavior (almost - see below) */
  122. param.chown_func = chown;
  123. if (OPT_NODEREF
  124. /* || (OPT_RECURSE && !OPT_TRAVERSE_TOP): */
  125. IF_DESKTOP( || (opt & (BIT_RECURSE|BIT_TRAVERSE_TOP)) == BIT_RECURSE)
  126. ) {
  127. param.chown_func = lchown;
  128. }
  129. flags = ACTION_DEPTHFIRST; /* match coreutils order */
  130. if (OPT_RECURSE)
  131. flags |= ACTION_RECURSE;
  132. if (OPT_TRAVERSE_TOP)
  133. flags |= ACTION_FOLLOWLINKS_L0; /* -H/-L: follow links on depth 0 */
  134. if (OPT_TRAVERSE)
  135. flags |= ACTION_FOLLOWLINKS; /* follow links if -L */
  136. parse_chown_usergroup_or_die(&param.ugid, argv[0]);
  137. /* Ok, ready to do the deed now */
  138. while (*++argv) {
  139. if (!recursive_action(*argv,
  140. flags, /* flags */
  141. fileAction, /* file action */
  142. fileAction, /* dir action */
  143. &param, /* user data */
  144. 0) /* depth */
  145. ) {
  146. retval = EXIT_FAILURE;
  147. }
  148. }
  149. return retval;
  150. }
  151. /*
  152. Testcase. Run in empty directory.
  153. #!/bin/sh
  154. t1="/tmp/busybox chown"
  155. t2="/usr/bin/chown"
  156. create() {
  157. rm -rf $1; mkdir $1
  158. (
  159. cd $1 || exit 1
  160. mkdir dir dir2
  161. >up
  162. >file
  163. >dir/file
  164. >dir2/file
  165. ln -s dir linkdir
  166. ln -s file linkfile
  167. ln -s ../up dir/linkup
  168. ln -s ../dir2 dir/linkupdir2
  169. )
  170. chown -R 0:0 $1
  171. }
  172. tst() {
  173. create test1
  174. create test2
  175. echo "[$1]" >>test1.out
  176. echo "[$1]" >>test2.out
  177. (cd test1; $t1 $1) >>test1.out 2>&1
  178. (cd test2; $t2 $1) >>test2.out 2>&1
  179. (cd test1; ls -lnR) >out1
  180. (cd test2; ls -lnR) >out2
  181. echo "chown $1" >out.diff
  182. if ! diff -u out1 out2 >>out.diff; then exit 1; fi
  183. rm out.diff
  184. }
  185. tst_for_each() {
  186. tst "$1 1:1 file"
  187. tst "$1 1:1 dir"
  188. tst "$1 1:1 linkdir"
  189. tst "$1 1:1 linkfile"
  190. }
  191. echo "If script produced 'out.diff' file, then at least one testcase failed"
  192. >test1.out
  193. >test2.out
  194. # These match coreutils 6.8:
  195. tst_for_each "-v"
  196. tst_for_each "-vR"
  197. tst_for_each "-vRP"
  198. tst_for_each "-vRL"
  199. tst_for_each "-vRH"
  200. tst_for_each "-vh"
  201. tst_for_each "-vhR"
  202. tst_for_each "-vhRP"
  203. tst_for_each "-vhRL"
  204. tst_for_each "-vhRH"
  205. # Fix `name' in coreutils output
  206. sed 's/`/'"'"'/g' -i test2.out
  207. # Compare us with coreutils output
  208. diff -u test1.out test2.out
  209. */