source: branches/3.0/mindi-busybox/loginutils/deluser.c @ 2899

Last change on this file since 2899 was 2725, checked in by bruno, 8 years ago
  • Update mindi-busybox to 1.18.3 to avoid problems with the tar command which is now failing on recent versions with busybox 1.7.3
File size: 2.8 KB
Line 
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 */
12#include "libbb.h"
13
14int deluser_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
15int deluser_main(int argc, char **argv)
16{
17    /* User or group name */
18    char *name;
19    /* Username (non-NULL only in "delgroup USER GROUP" case) */
20    char *member;
21    /* Name of passwd or group file */
22    const char *pfile;
23    /* Name of shadow or gshadow file */
24    const char *sfile;
25    /* Are we deluser or delgroup? */
26    int do_deluser = (ENABLE_DELUSER && (!ENABLE_DELGROUP || applet_name[3] == 'u'));
27
28    if (geteuid() != 0)
29        bb_error_msg_and_die(bb_msg_perm_denied_are_you_root);
30
31    name = argv[1];
32    member = NULL;
33
34    switch (argc) {
35    case 3:
36        if (!ENABLE_FEATURE_DEL_USER_FROM_GROUP || do_deluser)
37            break;
38        /* It's "delgroup USER GROUP" */
39        member = name;
40        name = argv[2];
41        /* Fallthrough */
42
43    case 2:
44        if (do_deluser) {
45            /* "deluser USER" */
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;
50        } else {
51            struct group *gr;
52 do_delgroup:
53            /* "delgroup GROUP" or "delgroup USER GROUP" */
54            if (do_deluser < 0) { /* delgroup after deluser? */
55                gr = getgrnam(name);
56                if (!gr)
57                    return EXIT_SUCCESS;
58            } else {
59                gr = xgetgrnam(name); /* bail out if GROUP is wrong */
60            }
61            if (!member) {
62                /* "delgroup GROUP" */
63                struct passwd *pw;
64                struct passwd pwent;
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);
70                }
71                //endpwent();
72            }
73            pfile = bb_path_group_file;
74            if (ENABLE_FEATURE_SHADOWPASSWDS)
75                sfile = bb_path_gshadow_file;
76        }
77
78        /* Modify pfile, then sfile */
79        do {
80            if (update_passwd(pfile, name, NULL, member) == -1)
81                return EXIT_FAILURE;
82            if (ENABLE_FEATURE_SHADOWPASSWDS) {
83                pfile = sfile;
84                sfile = NULL;
85            }
86        } while (ENABLE_FEATURE_SHADOWPASSWDS && pfile);
87
88        if (ENABLE_DELGROUP && do_deluser > 0) {
89            /* "deluser USER" also should try to delete
90             * same-named group. IOW: do "delgroup USER"
91             */
92// On debian deluser is a perl script that calls userdel.
93// From man 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.
96            do_deluser = -1;
97            goto do_delgroup;
98        }
99        return EXIT_SUCCESS;
100    }
101    /* Reached only if number of command line args is wrong */
102    bb_show_usage();
103}
Note: See TracBrowser for help on using the repository browser.