~ubuntu-branches/ubuntu/trusty/busybox/trusty

« back to all changes in this revision

Viewing changes to loginutils/deluser.c

  • Committer: Scott Moser
  • Date: 2011-05-27 16:06:28 UTC
  • mfrom: (68.1.6 oneiric)
  • Revision ID: smoser@ubuntu.com-20110527160628-t1lo3pfw92ef8vnf
* Resynchronise with Debian (LP: #788888).  Remaining changes:
  - [udeb] Enable chvt, getopt (and -l), killall, losetup, NFS
    mount, od, ping, stat, and remote syslog.
  - [deb] Enable mdev.
  - [deb, static] Enable fractional sleep and CGI support for httpd.
  - Enable 'mount -f' and mount helpers for all targets.
  - Add busybox-initramfs.
  - test-bin.patch: Move test and friends to /bin.
  - static-sh-alias.patch: Add static-sh alias name for ash, and install
    /bin/static-sh symlink to busybox in busybox-static.
  - Add cross-compiling support.
* grep-o-loop.patch: dropped as fix and testcase in upstream

Show diffs side-by-side

added added

removed removed

Lines of Context:
6
6
 * Copyright (C) 1999,2000,2001 by John Beppu <beppu@codepoet.org>
7
7
 * Copyright (C) 2007 by Tito Ragusa <farmatito@tiscali.it>
8
8
 *
9
 
 * Licensed under GPL version 2, see file LICENSE in this tarball for details.
 
9
 * Licensed under GPLv2, see file LICENSE in this source tree.
10
10
 *
11
11
 */
12
12
#include "libbb.h"
13
13
 
14
 
static int del_line_matching(char **args, const char *filename)
15
 
{
16
 
        if (ENABLE_FEATURE_DEL_USER_FROM_GROUP && args[2]) {
17
 
                return update_passwd(filename, args[2], NULL, args[1]);
18
 
        }
19
 
        return update_passwd(filename, args[1], NULL, NULL);
20
 
}
21
 
 
22
14
int deluser_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
23
15
int deluser_main(int argc, char **argv)
24
16
{
25
 
        if (argc != 2
26
 
         && (!ENABLE_FEATURE_DEL_USER_FROM_GROUP
27
 
            || (applet_name[3] != 'g' || argc != 3))
28
 
        ) {
29
 
                bb_show_usage();
30
 
        }
 
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'));
31
27
 
32
 
        if (geteuid())
 
28
        if (geteuid() != 0)
33
29
                bb_error_msg_and_die(bb_msg_perm_denied_are_you_root);
34
30
 
35
 
        if ((ENABLE_FEATURE_DEL_USER_FROM_GROUP && argc != 3)
36
 
         || ENABLE_DELUSER
37
 
         || (ENABLE_DELGROUP && ENABLE_DESKTOP)
38
 
        ) {
39
 
                if (ENABLE_DELUSER
40
 
                 && (!ENABLE_DELGROUP || applet_name[3] == 'u')
41
 
                ) {
42
 
                        if (del_line_matching(argv, bb_path_passwd_file) < 0)
 
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)
43
81
                                return EXIT_FAILURE;
44
82
                        if (ENABLE_FEATURE_SHADOWPASSWDS) {
45
 
                                del_line_matching(argv, bb_path_shadow_file);
 
83
                                pfile = sfile;
 
84
                                sfile = NULL;
46
85
                        }
47
 
                } else if (ENABLE_DESKTOP && ENABLE_DELGROUP && getpwnam(argv[1]))
48
 
                        bb_error_msg_and_die("can't remove primary group of user %s", argv[1]);
49
 
        }
50
 
        if (del_line_matching(argv, bb_path_group_file) < 0)
51
 
                return EXIT_FAILURE;
52
 
        if (ENABLE_FEATURE_SHADOWPASSWDS) {
53
 
                del_line_matching(argv, bb_path_gshadow_file);
54
 
        }
55
 
        return EXIT_SUCCESS;
 
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();
56
103
}