chown.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 tarball for details.
  8. */
  9. /* BB_AUDIT SUSv3 defects - none? */
  10. /* BB_AUDIT GNU defects - unsupported long options. */
  11. /* http://www.opengroup.org/onlinepubs/007904975/utilities/chown.html */
  12. #include "libbb.h"
  13. /* This is a NOEXEC applet. Be very careful! */
  14. #define OPT_STR ("Rh" USE_DESKTOP("vcfLHP"))
  15. #define BIT_RECURSE 1
  16. #define OPT_RECURSE (opt & 1)
  17. #define OPT_NODEREF (opt & 2)
  18. #define OPT_VERBOSE (USE_DESKTOP(opt & 0x04) SKIP_DESKTOP(0))
  19. #define OPT_CHANGED (USE_DESKTOP(opt & 0x08) SKIP_DESKTOP(0))
  20. #define OPT_QUIET (USE_DESKTOP(opt & 0x10) SKIP_DESKTOP(0))
  21. /* POSIX options
  22. * -L traverse every symbolic link to a directory encountered
  23. * -H if a command line argument is a symbolic link to a directory, traverse it
  24. * -P do not traverse any symbolic links (default)
  25. * We do not conform to the following:
  26. * "Specifying more than one of -H, -L, and -P is not an error.
  27. * The last option specified shall determine the behavior of the utility." */
  28. /* -L */
  29. #define BIT_TRAVERSE 0x20
  30. #define OPT_TRAVERSE (USE_DESKTOP(opt & BIT_TRAVERSE) SKIP_DESKTOP(0))
  31. /* -H or -L */
  32. #define BIT_TRAVERSE_TOP (0x20|0x40)
  33. #define OPT_TRAVERSE_TOP (USE_DESKTOP(opt & BIT_TRAVERSE_TOP) SKIP_DESKTOP(0))
  34. typedef int (*chown_fptr)(const char *, uid_t, gid_t);
  35. struct param_t {
  36. struct bb_uidgid_t ugid;
  37. chown_fptr chown_func;
  38. };
  39. static int FAST_FUNC fileAction(const char *fileName, struct stat *statbuf,
  40. void *vparam, int depth UNUSED_PARAM)
  41. {
  42. #define param (*(struct param_t*)vparam)
  43. #define opt option_mask32
  44. uid_t u = (param.ugid.uid == (uid_t)-1) ? statbuf->st_uid : param.ugid.uid;
  45. gid_t g = (param.ugid.gid == (gid_t)-1) ? statbuf->st_gid : param.ugid.gid;
  46. if (param.chown_func(fileName, u, g) == 0) {
  47. if (OPT_VERBOSE
  48. || (OPT_CHANGED && (statbuf->st_uid != u || statbuf->st_gid != g))
  49. ) {
  50. printf("changed ownership of '%s' to %u:%u\n",
  51. fileName, (unsigned)u, (unsigned)g);
  52. }
  53. return TRUE;
  54. }
  55. if (!OPT_QUIET)
  56. bb_simple_perror_msg(fileName); /* A filename can have % in it... */
  57. return FALSE;
  58. #undef opt
  59. #undef param
  60. }
  61. int chown_main(int argc UNUSED_PARAM, char **argv)
  62. {
  63. int retval = EXIT_SUCCESS;
  64. int opt, flags;
  65. struct param_t param;
  66. param.ugid.uid = -1;
  67. param.ugid.gid = -1;
  68. param.chown_func = chown;
  69. opt_complementary = "-2";
  70. opt = getopt32(argv, OPT_STR);
  71. argv += optind;
  72. /* This matches coreutils behavior (almost - see below) */
  73. if (OPT_NODEREF
  74. /* || (OPT_RECURSE && !OPT_TRAVERSE_TOP): */
  75. USE_DESKTOP( || (opt & (BIT_RECURSE|BIT_TRAVERSE_TOP)) == BIT_RECURSE)
  76. ) {
  77. param.chown_func = lchown;
  78. }
  79. flags = ACTION_DEPTHFIRST; /* match coreutils order */
  80. if (OPT_RECURSE)
  81. flags |= ACTION_RECURSE;
  82. if (OPT_TRAVERSE_TOP)
  83. flags |= ACTION_FOLLOWLINKS_L0; /* -H/-L: follow links on depth 0 */
  84. if (OPT_TRAVERSE)
  85. flags |= ACTION_FOLLOWLINKS; /* follow links if -L */
  86. parse_chown_usergroup_or_die(&param.ugid, argv[0]);
  87. /* Ok, ready to do the deed now */
  88. argv++;
  89. do {
  90. if (!recursive_action(*argv,
  91. flags, /* flags */
  92. fileAction, /* file action */
  93. fileAction, /* dir action */
  94. &param, /* user data */
  95. 0) /* depth */
  96. ) {
  97. retval = EXIT_FAILURE;
  98. }
  99. } while (*++argv);
  100. return retval;
  101. }
  102. /*
  103. Testcase. Run in empty directory.
  104. #!/bin/sh
  105. t1="/tmp/busybox chown"
  106. t2="/usr/bin/chown"
  107. create() {
  108. rm -rf $1; mkdir $1
  109. (
  110. cd $1 || exit 1
  111. mkdir dir dir2
  112. >up
  113. >file
  114. >dir/file
  115. >dir2/file
  116. ln -s dir linkdir
  117. ln -s file linkfile
  118. ln -s ../up dir/linkup
  119. ln -s ../dir2 dir/linkupdir2
  120. )
  121. chown -R 0:0 $1
  122. }
  123. tst() {
  124. create test1
  125. create test2
  126. echo "[$1]" >>test1.out
  127. echo "[$1]" >>test2.out
  128. (cd test1; $t1 $1) >>test1.out 2>&1
  129. (cd test2; $t2 $1) >>test2.out 2>&1
  130. (cd test1; ls -lnR) >out1
  131. (cd test2; ls -lnR) >out2
  132. echo "chown $1" >out.diff
  133. if ! diff -u out1 out2 >>out.diff; then exit 1; fi
  134. rm out.diff
  135. }
  136. tst_for_each() {
  137. tst "$1 1:1 file"
  138. tst "$1 1:1 dir"
  139. tst "$1 1:1 linkdir"
  140. tst "$1 1:1 linkfile"
  141. }
  142. echo "If script produced 'out.diff' file, then at least one testcase failed"
  143. >test1.out
  144. >test2.out
  145. # These match coreutils 6.8:
  146. tst_for_each "-v"
  147. tst_for_each "-vR"
  148. tst_for_each "-vRP"
  149. tst_for_each "-vRL"
  150. tst_for_each "-vRH"
  151. tst_for_each "-vh"
  152. tst_for_each "-vhR"
  153. tst_for_each "-vhRP"
  154. tst_for_each "-vhRL"
  155. tst_for_each "-vhRH"
  156. # Fix `name' in coreutils output
  157. sed 's/`/'"'"'/g' -i test2.out
  158. # Compare us with coreutils output
  159. diff -u test1.out test2.out
  160. */