3
0

chown.c 6.5 KB

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