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
15 //usage:#define deluser_full_usage "\n\n"
16 //usage: "Delete USER from the system"
18 //usage:#define delgroup_trivial_usage
19 //usage: IF_FEATURE_DEL_USER_FROM_GROUP("[USER] ")"GROUP"
20 //usage:#define delgroup_full_usage "\n\n"
21 //usage: "Delete group GROUP from the system"
22 //usage: IF_FEATURE_DEL_USER_FROM_GROUP(" or user USER from group GROUP")
26 int deluser_main(int argc
, char **argv
) MAIN_EXTERNALLY_VISIBLE
;
27 int deluser_main(int argc
, char **argv
)
29 /* User or group name */
31 /* Username (non-NULL only in "delgroup USER GROUP" case) */
33 /* Name of passwd or group file */
35 /* Name of shadow or gshadow file */
37 /* Are we deluser or delgroup? */
38 int do_deluser
= (ENABLE_DELUSER
&& (!ENABLE_DELGROUP
|| applet_name
[3] == 'u'));
41 bb_error_msg_and_die(bb_msg_perm_denied_are_you_root
);
48 if (!ENABLE_FEATURE_DEL_USER_FROM_GROUP
|| do_deluser
)
50 /* It's "delgroup USER GROUP" */
58 xgetpwnam(name
); /* bail out if USER is wrong */
59 pfile
= bb_path_passwd_file
;
60 if (ENABLE_FEATURE_SHADOWPASSWDS
)
61 sfile
= bb_path_shadow_file
;
65 /* "delgroup GROUP" or "delgroup USER GROUP" */
66 if (do_deluser
< 0) { /* delgroup after deluser? */
71 gr
= xgetgrnam(name
); /* bail out if GROUP is wrong */
74 /* "delgroup GROUP" */
77 /* Check if the group is in use */
78 #define passwd_buf bb_common_bufsiz1
79 while (!getpwent_r(&pwent
, passwd_buf
, sizeof(passwd_buf
), &pw
)) {
80 if (pwent
.pw_gid
== gr
->gr_gid
)
81 bb_error_msg_and_die("'%s' still has '%s' as their primary group!", pwent
.pw_name
, name
);
85 pfile
= bb_path_group_file
;
86 if (ENABLE_FEATURE_SHADOWPASSWDS
)
87 sfile
= bb_path_gshadow_file
;
90 /* Modify pfile, then sfile */
92 if (update_passwd(pfile
, name
, NULL
, member
) == -1)
94 if (ENABLE_FEATURE_SHADOWPASSWDS
) {
98 } while (ENABLE_FEATURE_SHADOWPASSWDS
&& pfile
);
100 if (ENABLE_DELGROUP
&& do_deluser
> 0) {
101 /* "deluser USER" also should try to delete
102 * same-named group. IOW: do "delgroup USER"
104 // On debian deluser is a perl script that calls userdel.
106 // If USERGROUPS_ENAB is defined to yes in /etc/login.defs, userdel will
107 // delete the group with the same name as the user.
113 /* Reached only if number of command line args is wrong */