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 bool 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;
52 /* "delgroup GROUP" or "delgroup USER GROUP" */
53 xgetgrnam(name); /* bail out if GROUP is wrong */
56 * If user with the same name exists,
59 //BUG: check should be done by GID, not by matching name!
61 //2. check that /etc/passwd doesn't have lines of the form
62 // user:pwd:uid:GID:...
63 //3. bail out if at least one such line exists
64 if (getpwnam(name) != NULL)
65 bb_error_msg_and_die("'%s' still has '%s' as their primary group!", name, name);
67 pfile = bb_path_group_file;
68 if (ENABLE_FEATURE_SHADOWPASSWDS)
69 sfile = bb_path_gshadow_file;
72 /* Modify pfile, then sfile */
74 if (update_passwd(pfile, name, NULL, member) == -1)
76 if (ENABLE_FEATURE_SHADOWPASSWDS) {
80 } while (ENABLE_FEATURE_SHADOWPASSWDS && pfile);
82 if (ENABLE_DELGROUP && do_deluser) {
83 /* "deluser USER" also should try to delete
84 * same-named group. IOW: do "delgroup USER"
86 //TODO: check how it actually works in upstream.
87 //I suspect it is only done if group has no more members.
93 /* Reached only if number of command line args is wrong */