chown.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 ATTRIBUTE_UNUSED, char **argv)
  55. {
  56. int retval = EXIT_SUCCESS;
  57. int flags;
  58. chown_fptr chown_func;
  59. opt_complementary = "-2";
  60. getopt32(argv, OPT_STR);
  61. argv += optind;
  62. /* This matches coreutils behavior (almost - see below) */
  63. chown_func = chown;
  64. if (OPT_NODEREF
  65. /* || (OPT_RECURSE && !OPT_TRAVERSE_TOP): */
  66. USE_DESKTOP( || (option_mask32 & (BIT_RECURSE|BIT_TRAVERSE_TOP)) == BIT_RECURSE)
  67. ) {
  68. chown_func = lchown;
  69. }
  70. flags = ACTION_DEPTHFIRST; /* match coreutils order */
  71. if (OPT_RECURSE)
  72. flags |= ACTION_RECURSE;
  73. if (OPT_TRAVERSE_TOP)
  74. flags |= ACTION_FOLLOWLINKS_L0; /* -H/-L: follow links on depth 0 */
  75. if (OPT_TRAVERSE)
  76. flags |= ACTION_FOLLOWLINKS; /* follow links if -L */
  77. parse_chown_usergroup_or_die(&ugid, argv[0]);
  78. /* Ok, ready to do the deed now */
  79. argv++;
  80. do {
  81. if (!recursive_action(*argv,
  82. flags, /* flags */
  83. fileAction, /* file action */
  84. fileAction, /* dir action */
  85. chown_func, /* user data */
  86. 0) /* depth */
  87. ) {
  88. retval = EXIT_FAILURE;
  89. }
  90. } while (*++argv);
  91. return retval;
  92. }
  93. /*
  94. Testcase. Run in empty directory.
  95. #!/bin/sh
  96. t1="/tmp/busybox chown"
  97. t2="/usr/bin/chown"
  98. create() {
  99. rm -rf $1; mkdir $1
  100. (
  101. cd $1 || exit 1
  102. mkdir dir dir2
  103. >up
  104. >file
  105. >dir/file
  106. >dir2/file
  107. ln -s dir linkdir
  108. ln -s file linkfile
  109. ln -s ../up dir/linkup
  110. ln -s ../dir2 dir/linkupdir2
  111. )
  112. chown -R 0:0 $1
  113. }
  114. tst() {
  115. create test1
  116. create test2
  117. echo "[$1]" >>test1.out
  118. echo "[$1]" >>test2.out
  119. (cd test1; $t1 $1) >>test1.out 2>&1
  120. (cd test2; $t2 $1) >>test2.out 2>&1
  121. (cd test1; ls -lnR) >out1
  122. (cd test2; ls -lnR) >out2
  123. echo "chown $1" >out.diff
  124. if ! diff -u out1 out2 >>out.diff; then exit 1; fi
  125. rm out.diff
  126. }
  127. tst_for_each() {
  128. tst "$1 1:1 file"
  129. tst "$1 1:1 dir"
  130. tst "$1 1:1 linkdir"
  131. tst "$1 1:1 linkfile"
  132. }
  133. echo "If script produced 'out.diff' file, then at least one testcase failed"
  134. >test1.out
  135. >test2.out
  136. # These match coreutils 6.8:
  137. tst_for_each "-v"
  138. tst_for_each "-vR"
  139. tst_for_each "-vRP"
  140. tst_for_each "-vRL"
  141. tst_for_each "-vRH"
  142. tst_for_each "-vh"
  143. tst_for_each "-vhR"
  144. tst_for_each "-vhRP"
  145. tst_for_each "-vhRL"
  146. tst_for_each "-vhRH"
  147. # Fix `name' in coreutils output
  148. sed 's/`/'"'"'/g' -i test2.out
  149. # Compare us with coreutils output
  150. diff -u test1.out test2.out
  151. */