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.
14 int deluser_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
15 int deluser_main(int argc, char **argv)
17 /* User or group name */
19 /* Username (non-NULL only in "delgroup USER GROUP" case) */
21 /* Name of passwd or group file */
23 /* Name of shadow or gshadow file */
25 /* Are we deluser or delgroup? */
26 int do_deluser = (ENABLE_DELUSER && (!ENABLE_DELGROUP || applet_name[3] == 'u'));
29 bb_error_msg_and_die(bb_msg_perm_denied_are_you_root);
36 if (!ENABLE_FEATURE_DEL_USER_FROM_GROUP || do_deluser)
38 /* It's "delgroup USER GROUP" */
46 xgetpwnam(name); /* bail out if USER is wrong */
47 pfile = bb_path_passwd_file;
48 if (ENABLE_FEATURE_SHADOWPASSWDS)
49 sfile = bb_path_shadow_file;
53 /* "delgroup GROUP" or "delgroup USER GROUP" */
54 if (do_deluser < 0) { /* delgroup after deluser? */
59 gr = xgetgrnam(name); /* bail out if GROUP is wrong */
62 /* "delgroup GROUP" */
65 /* Check if the group is in use */
66 #define passwd_buf bb_common_bufsiz1
67 while (!getpwent_r(&pwent, passwd_buf, sizeof(passwd_buf), &pw)) {
68 if (pwent.pw_gid == gr->gr_gid)
69 bb_error_msg_and_die("'%s' still has '%s' as their primary group!", pwent.pw_name, name);
73 pfile = bb_path_group_file;
74 if (ENABLE_FEATURE_SHADOWPASSWDS)
75 sfile = bb_path_gshadow_file;
78 /* Modify pfile, then sfile */
80 if (update_passwd(pfile, name, NULL, member) == -1)
82 if (ENABLE_FEATURE_SHADOWPASSWDS) {
86 } while (ENABLE_FEATURE_SHADOWPASSWDS && pfile);
88 if (ENABLE_DELGROUP && do_deluser > 0) {
89 /* "deluser USER" also should try to delete
90 * same-named group. IOW: do "delgroup USER"
92 // On debian deluser is a perl script that calls userdel.
94 // If USERGROUPS_ENAB is defined to yes in /etc/login.defs, userdel will
95 // delete the group with the same name as the user.
101 /* Reached only if number of command line args is wrong */