chown.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 - unsupported options -H, -L, and -P. */
  10. /* BB_AUDIT GNU defects - unsupported long options. */
  11. /* http://www.opengroup.org/onlinepubs/007904975/utilities/chown.html */
  12. #include "busybox.h"
  13. static struct bb_uidgid_t ugid = { -1, -1 };
  14. static int (*chown_func)(const char *, uid_t, gid_t) = chown;
  15. #define OPT_RECURSE (option_mask32 & 1)
  16. #define OPT_NODEREF (option_mask32 & 2)
  17. #define OPT_VERBOSE (USE_DESKTOP(option_mask32 & 4) SKIP_DESKTOP(0))
  18. #define OPT_CHANGED (USE_DESKTOP(option_mask32 & 8) SKIP_DESKTOP(0))
  19. #define OPT_QUIET (USE_DESKTOP(option_mask32 & 0x10) SKIP_DESKTOP(0))
  20. #define OPT_STR ("Rh" USE_DESKTOP("vcf"))
  21. /* TODO:
  22. * -H if a command line argument is a symbolic link to a directory, traverse it
  23. * -L traverse every symbolic link to a directory encountered
  24. * -P do not traverse any symbolic links (default)
  25. */
  26. static int fileAction(const char *fileName, struct stat *statbuf,
  27. void ATTRIBUTE_UNUSED *junk, int depth)
  28. {
  29. // TODO: -H/-L/-P
  30. // if (depth ... && S_ISLNK(statbuf->st_mode)) ....
  31. if (!chown_func(fileName,
  32. (ugid.uid == (uid_t)-1) ? statbuf->st_uid : ugid.uid,
  33. (ugid.gid == (gid_t)-1) ? statbuf->st_gid : ugid.gid)
  34. ) {
  35. if (OPT_VERBOSE
  36. || (OPT_CHANGED && (statbuf->st_uid != ugid.uid || statbuf->st_gid != ugid.gid))
  37. ) {
  38. printf("changed ownership of '%s' to %u:%u\n",
  39. fileName, ugid.uid, ugid.gid);
  40. }
  41. return TRUE;
  42. }
  43. if (!OPT_QUIET)
  44. bb_perror_msg("%s", fileName); /* A filename can have % in it... */
  45. return FALSE;
  46. }
  47. int chown_main(int argc, char **argv)
  48. {
  49. char *groupName;
  50. int retval = EXIT_SUCCESS;
  51. opt_complementary = "-2";
  52. getopt32(argc, argv, OPT_STR);
  53. argv += optind;
  54. if (OPT_NODEREF) chown_func = lchown;
  55. /* First, check if there is a group name here */
  56. groupName = strchr(*argv, '.'); /* deprecated? */
  57. if (!groupName)
  58. groupName = strchr(*argv, ':');
  59. else
  60. *groupName = ':'; /* replace '.' with ':' */
  61. /* First, try parsing "user[:[group]]" */
  62. if (!groupName) { /* "user" */
  63. ugid.uid = get_ug_id(*argv, xuname2uid);
  64. } else if (groupName == *argv) { /* ":group" */
  65. ugid.gid = get_ug_id(groupName + 1, xgroup2gid);
  66. } else {
  67. if (!groupName[1]) /* "user:" */
  68. *groupName = '\0';
  69. if (!get_uidgid(&ugid, *argv, 1))
  70. bb_error_msg_and_die("unknown user/group %s", *argv);
  71. }
  72. /* Ok, ready to do the deed now */
  73. argv++;
  74. do {
  75. if (!recursive_action(*argv,
  76. OPT_RECURSE, // recurse
  77. FALSE, // follow links: TODO: -H/-L/-P
  78. FALSE, // depth first
  79. fileAction, // file action
  80. fileAction, // dir action
  81. NULL, // user data
  82. 0) // depth
  83. ) {
  84. retval = EXIT_FAILURE;
  85. }
  86. } while (*++argv);
  87. return retval;
  88. }