1 /* vi: set sw=4 ts=4: */
3 * deluser/delgroup implementation for busybox
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>
9 * Licensed under GPLv2, see file LICENSE in this source tree.
13 //usage:#define deluser_trivial_usage
14 //usage: IF_LONG_OPTS("[--remove-home] ") "USER"
15 //usage:#define deluser_full_usage "\n\n"
16 //usage: "Delete USER from the system"
17 // --remove-home is self-explanatory enough to put it in --help
19 //usage:#define delgroup_trivial_usage
20 //usage: IF_FEATURE_DEL_USER_FROM_GROUP("[USER] ")"GROUP"
21 //usage:#define delgroup_full_usage "\n\n"
22 //usage: "Delete group GROUP from the system"
23 //usage: IF_FEATURE_DEL_USER_FROM_GROUP(" or user USER from group GROUP")
27 int deluser_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
28 int deluser_main(int argc, char **argv)
30 /* User or group name */
32 /* Username (non-NULL only in "delgroup USER GROUP" case) */
34 /* Name of passwd or group file */
36 /* Name of shadow or gshadow file */
38 /* Are we deluser or delgroup? */
39 int do_deluser = (ENABLE_DELUSER && (!ENABLE_DELGROUP || applet_name[3] == 'u'));
42 const int opt_delhome = 0;
47 "remove-home\0" No_argument "\xff";
48 opt_delhome = getopt32(argv, "");
55 bb_error_msg_and_die(bb_msg_perm_denied_are_you_root);
62 if (!ENABLE_FEATURE_DEL_USER_FROM_GROUP || do_deluser)
64 /* It's "delgroup USER GROUP" */
74 pw = xgetpwnam(name); /* bail out if USER is wrong */
75 pfile = bb_path_passwd_file;
76 if (ENABLE_FEATURE_SHADOWPASSWDS)
77 sfile = bb_path_shadow_file;
79 remove_file(pw->pw_dir, FILEUTILS_RECUR);
83 /* "delgroup GROUP" or "delgroup USER GROUP" */
84 if (do_deluser < 0) { /* delgroup after deluser? */
89 gr = xgetgrnam(name); /* bail out if GROUP is wrong */
92 /* "delgroup GROUP" */
94 /* Check if the group is in use */
95 while ((pw = getpwent()) != NULL) {
96 if (pw->pw_gid == gr->gr_gid)
97 bb_error_msg_and_die("'%s' still has '%s' as their primary group!",
102 pfile = bb_path_group_file;
103 if (ENABLE_FEATURE_SHADOWPASSWDS)
104 sfile = bb_path_gshadow_file;
107 /* Modify pfile, then sfile */
109 if (update_passwd(pfile, name, NULL, member) == -1)
111 if (ENABLE_FEATURE_SHADOWPASSWDS) {
115 } while (ENABLE_FEATURE_SHADOWPASSWDS && pfile);
117 if (do_deluser > 0) {
118 /* Delete user from all groups */
119 if (update_passwd(bb_path_group_file, NULL, NULL, name) == -1)
122 if (ENABLE_DELGROUP) {
123 /* "deluser USER" also should try to delete
124 * same-named group. IOW: do "delgroup USER"
126 // On debian deluser is a perl script that calls userdel.
128 // If USERGROUPS_ENAB is defined to yes in /etc/login.defs, userdel will
129 // delete the group with the same name as the user.
136 /* Reached only if number of command line args is wrong */