chown.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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: "[-RhLHP"IF_DESKTOP("cvf")"]... OWNER[<.|:>[GROUP]] FILE..."
  13. //usage:#define chown_full_usage "\n\n"
  14. //usage: "Change the owner and/or group of each FILE to OWNER and/or GROUP\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. /* Just -1 might not work: uid_t may be unsigned long */
  103. param.ugid.uid = -1L;
  104. param.ugid.gid = -1L;
  105. #if ENABLE_FEATURE_CHOWN_LONG_OPTIONS
  106. applet_long_options = chown_longopts;
  107. #endif
  108. opt_complementary = "-2";
  109. opt = getopt32(argv, OPT_STR);
  110. argv += optind;
  111. /* This matches coreutils behavior (almost - see below) */
  112. param.chown_func = chown;
  113. if (OPT_NODEREF
  114. /* || (OPT_RECURSE && !OPT_TRAVERSE_TOP): */
  115. IF_DESKTOP( || (opt & (BIT_RECURSE|BIT_TRAVERSE_TOP)) == BIT_RECURSE)
  116. ) {
  117. param.chown_func = lchown;
  118. }
  119. flags = ACTION_DEPTHFIRST; /* match coreutils order */
  120. if (OPT_RECURSE)
  121. flags |= ACTION_RECURSE;
  122. if (OPT_TRAVERSE_TOP)
  123. flags |= ACTION_FOLLOWLINKS_L0; /* -H/-L: follow links on depth 0 */
  124. if (OPT_TRAVERSE)
  125. flags |= ACTION_FOLLOWLINKS; /* follow links if -L */
  126. parse_chown_usergroup_or_die(&param.ugid, argv[0]);
  127. /* Ok, ready to do the deed now */
  128. while (*++argv) {
  129. if (!recursive_action(*argv,
  130. flags, /* flags */
  131. fileAction, /* file action */
  132. fileAction, /* dir action */
  133. &param, /* user data */
  134. 0) /* depth */
  135. ) {
  136. retval = EXIT_FAILURE;
  137. }
  138. }
  139. return retval;
  140. }
  141. /*
  142. Testcase. Run in empty directory.
  143. #!/bin/sh
  144. t1="/tmp/busybox chown"
  145. t2="/usr/bin/chown"
  146. create() {
  147. rm -rf $1; mkdir $1
  148. (
  149. cd $1 || exit 1
  150. mkdir dir dir2
  151. >up
  152. >file
  153. >dir/file
  154. >dir2/file
  155. ln -s dir linkdir
  156. ln -s file linkfile
  157. ln -s ../up dir/linkup
  158. ln -s ../dir2 dir/linkupdir2
  159. )
  160. chown -R 0:0 $1
  161. }
  162. tst() {
  163. create test1
  164. create test2
  165. echo "[$1]" >>test1.out
  166. echo "[$1]" >>test2.out
  167. (cd test1; $t1 $1) >>test1.out 2>&1
  168. (cd test2; $t2 $1) >>test2.out 2>&1
  169. (cd test1; ls -lnR) >out1
  170. (cd test2; ls -lnR) >out2
  171. echo "chown $1" >out.diff
  172. if ! diff -u out1 out2 >>out.diff; then exit 1; fi
  173. rm out.diff
  174. }
  175. tst_for_each() {
  176. tst "$1 1:1 file"
  177. tst "$1 1:1 dir"
  178. tst "$1 1:1 linkdir"
  179. tst "$1 1:1 linkfile"
  180. }
  181. echo "If script produced 'out.diff' file, then at least one testcase failed"
  182. >test1.out
  183. >test2.out
  184. # These match coreutils 6.8:
  185. tst_for_each "-v"
  186. tst_for_each "-vR"
  187. tst_for_each "-vRP"
  188. tst_for_each "-vRL"
  189. tst_for_each "-vRH"
  190. tst_for_each "-vh"
  191. tst_for_each "-vhR"
  192. tst_for_each "-vhRP"
  193. tst_for_each "-vhRL"
  194. tst_for_each "-vhRH"
  195. # Fix `name' in coreutils output
  196. sed 's/`/'"'"'/g' -i test2.out
  197. # Compare us with coreutils output
  198. diff -u test1.out test2.out
  199. */