chown.c 6.5 KB

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