deluser.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * deluser/delgroup implementation for busybox
  4. *
  5. * Copyright (C) 1999 by Lineo, inc. and John Beppu
  6. * Copyright (C) 1999,2000,2001 by John Beppu <beppu@codepoet.org>
  7. * Copyright (C) 2007 by Tito Ragusa <farmatito@tiscali.it>
  8. *
  9. * Licensed under GPLv2, see file LICENSE in this source tree.
  10. */
  11. //config:config DELUSER
  12. //config: bool "deluser (9.1 kb)"
  13. //config: default y
  14. //config: help
  15. //config: Utility for deleting a user account.
  16. //config:
  17. //config:config DELGROUP
  18. //config: bool "delgroup (6.4 kb)"
  19. //config: default y
  20. //config: help
  21. //config: Utility for deleting a group account.
  22. //config:
  23. //config:config FEATURE_DEL_USER_FROM_GROUP
  24. //config: bool "Support removing users from groups"
  25. //config: default y
  26. //config: depends on DELGROUP
  27. //config: help
  28. //config: If called with two non-option arguments, deluser
  29. //config: or delgroup will remove an user from a specified group.
  30. // APPLET_NOEXEC:name main location suid_type help
  31. //applet:IF_DELUSER( APPLET_NOEXEC(deluser, deluser, BB_DIR_USR_SBIN, BB_SUID_DROP, deluser))
  32. //applet:IF_DELGROUP(APPLET_NOEXEC(delgroup, deluser, BB_DIR_USR_SBIN, BB_SUID_DROP, delgroup))
  33. //kbuild:lib-$(CONFIG_DELUSER) += deluser.o
  34. //kbuild:lib-$(CONFIG_DELGROUP) += deluser.o
  35. //usage:#define deluser_trivial_usage
  36. //usage: IF_LONG_OPTS("[--remove-home] ") "USER"
  37. //usage:#define deluser_full_usage "\n\n"
  38. //usage: "Delete USER from the system"
  39. // --remove-home is self-explanatory enough to put it in --help
  40. //usage:#define delgroup_trivial_usage
  41. //usage: IF_FEATURE_DEL_USER_FROM_GROUP("[USER] ")"GROUP"
  42. //usage:#define delgroup_full_usage "\n\n"
  43. //usage: "Delete group GROUP from the system"
  44. //usage: IF_FEATURE_DEL_USER_FROM_GROUP(" or user USER from group GROUP")
  45. #include "libbb.h"
  46. int deluser_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  47. int deluser_main(int argc, char **argv)
  48. {
  49. /* User or group name */
  50. char *name;
  51. /* Username (non-NULL only in "delgroup USER GROUP" case) */
  52. char *member;
  53. /* Name of passwd or group file */
  54. const char *pfile;
  55. /* Name of shadow or gshadow file */
  56. const char *sfile;
  57. /* Are we deluser or delgroup? */
  58. int do_deluser = (ENABLE_DELUSER && (!ENABLE_DELGROUP || applet_name[3] == 'u'));
  59. #if !ENABLE_LONG_OPTS
  60. const int opt_delhome = 0;
  61. #else
  62. int opt_delhome = 0;
  63. if (do_deluser) {
  64. opt_delhome = getopt32long(argv, "",
  65. "remove-home\0" No_argument "\xff");
  66. argv += opt_delhome;
  67. argc -= opt_delhome;
  68. }
  69. #endif
  70. if (geteuid() != 0)
  71. bb_error_msg_and_die(bb_msg_perm_denied_are_you_root);
  72. name = argv[1];
  73. member = NULL;
  74. switch (argc) {
  75. case 3:
  76. if (!ENABLE_FEATURE_DEL_USER_FROM_GROUP || do_deluser)
  77. break;
  78. /* It's "delgroup USER GROUP" */
  79. member = name;
  80. name = argv[2];
  81. /* Fallthrough */
  82. case 2:
  83. if (do_deluser) {
  84. /* "deluser USER" */
  85. struct passwd *pw;
  86. pw = xgetpwnam(name); /* bail out if USER is wrong */
  87. pfile = bb_path_passwd_file;
  88. if (ENABLE_FEATURE_SHADOWPASSWDS)
  89. sfile = bb_path_shadow_file;
  90. if (opt_delhome)
  91. remove_file(pw->pw_dir, FILEUTILS_RECUR);
  92. } else {
  93. struct group *gr;
  94. do_delgroup:
  95. /* "delgroup GROUP" or "delgroup USER GROUP" */
  96. if (do_deluser < 0) { /* delgroup after deluser? */
  97. gr = getgrnam(name);
  98. if (!gr)
  99. return EXIT_SUCCESS;
  100. } else {
  101. gr = xgetgrnam(name); /* bail out if GROUP is wrong */
  102. }
  103. if (!member) {
  104. /* "delgroup GROUP" */
  105. struct passwd *pw;
  106. /* Check if the group is in use */
  107. while ((pw = getpwent()) != NULL) {
  108. if (pw->pw_gid == gr->gr_gid)
  109. bb_error_msg_and_die("'%s' still has '%s' as their primary group!",
  110. pw->pw_name, name);
  111. }
  112. //endpwent();
  113. }
  114. pfile = bb_path_group_file;
  115. if (ENABLE_FEATURE_SHADOWPASSWDS)
  116. sfile = bb_path_gshadow_file;
  117. }
  118. /* Modify pfile, then sfile */
  119. do {
  120. if (update_passwd(pfile, name, NULL, member) == -1)
  121. return EXIT_FAILURE;
  122. if (ENABLE_FEATURE_SHADOWPASSWDS) {
  123. pfile = sfile;
  124. sfile = NULL;
  125. }
  126. } while (ENABLE_FEATURE_SHADOWPASSWDS && pfile);
  127. if (do_deluser > 0) {
  128. /* Delete user from all groups */
  129. if (update_passwd(bb_path_group_file, NULL, NULL, name) == -1)
  130. return EXIT_FAILURE;
  131. if (ENABLE_DELGROUP) {
  132. /* "deluser USER" also should try to delete
  133. * same-named group. IOW: do "delgroup USER"
  134. */
  135. // On debian deluser is a perl script that calls userdel.
  136. // From man userdel:
  137. // If USERGROUPS_ENAB is defined to yes in /etc/login.defs, userdel will
  138. // delete the group with the same name as the user.
  139. do_deluser = -1;
  140. goto do_delgroup;
  141. }
  142. }
  143. return EXIT_SUCCESS;
  144. }
  145. /* Reached only if number of command line args is wrong */
  146. bb_show_usage();
  147. }