chown.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 (option_mask32 & 1)
  17. #define OPT_NODEREF (option_mask32 & 2)
  18. #define OPT_VERBOSE (USE_DESKTOP(option_mask32 & 0x04) SKIP_DESKTOP(0))
  19. #define OPT_CHANGED (USE_DESKTOP(option_mask32 & 0x08) SKIP_DESKTOP(0))
  20. #define OPT_QUIET (USE_DESKTOP(option_mask32 & 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(option_mask32 & BIT_TRAVERSE) SKIP_DESKTOP(0))
  31. /* -H or -L */
  32. #define BIT_TRAVERSE_TOP (0x20|0x40)
  33. #define OPT_TRAVERSE_TOP (USE_DESKTOP(option_mask32 & BIT_TRAVERSE_TOP) SKIP_DESKTOP(0))
  34. typedef int (*chown_fptr)(const char *, uid_t, gid_t);
  35. static struct bb_uidgid_t ugid = { -1, -1 };
  36. static int fileAction(const char *fileName, struct stat *statbuf,
  37. void *cf, int depth ATTRIBUTE_UNUSED)
  38. {
  39. uid_t u = (ugid.uid == (uid_t)-1) ? statbuf->st_uid : ugid.uid;
  40. gid_t g = (ugid.gid == (gid_t)-1) ? statbuf->st_gid : ugid.gid;
  41. if (!((chown_fptr)cf)(fileName, u, g)) {
  42. if (OPT_VERBOSE
  43. || (OPT_CHANGED && (statbuf->st_uid != u || statbuf->st_gid != g))
  44. ) {
  45. printf("changed ownership of '%s' to %u:%u\n",
  46. fileName, (unsigned)u, (unsigned)g);
  47. }
  48. return TRUE;
  49. }
  50. if (!OPT_QUIET)
  51. bb_simple_perror_msg(fileName); /* A filename can have % in it... */
  52. return FALSE;
  53. }
  54. int chown_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  55. int chown_main(int argc ATTRIBUTE_UNUSED, char **argv)
  56. {
  57. int retval = EXIT_SUCCESS;
  58. int flags;
  59. chown_fptr chown_func;
  60. opt_complementary = "-2";
  61. getopt32(argv, OPT_STR);
  62. argv += optind;
  63. /* This matches coreutils behavior (almost - see below) */
  64. chown_func = chown;
  65. if (OPT_NODEREF
  66. /* || (OPT_RECURSE && !OPT_TRAVERSE_TOP): */
  67. USE_DESKTOP( || (option_mask32 & (BIT_RECURSE|BIT_TRAVERSE_TOP)) == BIT_RECURSE)
  68. ) {
  69. chown_func = lchown;
  70. }
  71. flags = ACTION_DEPTHFIRST; /* match coreutils order */
  72. if (OPT_RECURSE)
  73. flags |= ACTION_RECURSE;
  74. if (OPT_TRAVERSE_TOP)
  75. flags |= ACTION_FOLLOWLINKS_L0; /* -H/-L: follow links on depth 0 */
  76. if (OPT_TRAVERSE)
  77. flags |= ACTION_FOLLOWLINKS; /* follow links if -L */
  78. parse_chown_usergroup_or_die(&ugid, argv[0]);
  79. /* Ok, ready to do the deed now */
  80. argv++;
  81. do {
  82. if (!recursive_action(*argv,
  83. flags, /* flags */
  84. fileAction, /* file action */
  85. fileAction, /* dir action */
  86. chown_func, /* user data */
  87. 0) /* depth */
  88. ) {
  89. retval = EXIT_FAILURE;
  90. }
  91. } while (*++argv);
  92. return retval;
  93. }
  94. /*
  95. Testcase. Run in empty directory.
  96. #!/bin/sh
  97. t1="/tmp/busybox chown"
  98. t2="/usr/bin/chown"
  99. create() {
  100. rm -rf $1; mkdir $1
  101. (
  102. cd $1 || exit 1
  103. mkdir dir dir2
  104. >up
  105. >file
  106. >dir/file
  107. >dir2/file
  108. ln -s dir linkdir
  109. ln -s file linkfile
  110. ln -s ../up dir/linkup
  111. ln -s ../dir2 dir/linkupdir2
  112. )
  113. chown -R 0:0 $1
  114. }
  115. tst() {
  116. create test1
  117. create test2
  118. echo "[$1]" >>test1.out
  119. echo "[$1]" >>test2.out
  120. (cd test1; $t1 $1) >>test1.out 2>&1
  121. (cd test2; $t2 $1) >>test2.out 2>&1
  122. (cd test1; ls -lnR) >out1
  123. (cd test2; ls -lnR) >out2
  124. echo "chown $1" >out.diff
  125. if ! diff -u out1 out2 >>out.diff; then exit 1; fi
  126. rm out.diff
  127. }
  128. tst_for_each() {
  129. tst "$1 1:1 file"
  130. tst "$1 1:1 dir"
  131. tst "$1 1:1 linkdir"
  132. tst "$1 1:1 linkfile"
  133. }
  134. echo "If script produced 'out.diff' file, then at least one testcase failed"
  135. >test1.out
  136. >test2.out
  137. # These match coreutils 6.8:
  138. tst_for_each "-v"
  139. tst_for_each "-vR"
  140. tst_for_each "-vRP"
  141. tst_for_each "-vRL"
  142. tst_for_each "-vRH"
  143. tst_for_each "-vh"
  144. tst_for_each "-vhR"
  145. tst_for_each "-vhRP"
  146. tst_for_each "-vhRL"
  147. tst_for_each "-vhRH"
  148. # Fix `name' in coreutils output
  149. sed 's/`/'"'"'/g' -i test2.out
  150. # Compare us with coreutils output
  151. diff -u test1.out test2.out
  152. */