chown.c 6.0 KB

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