summaryrefslogtreecommitdiffstats
path: root/examples/LDAP/smbldap-tools/smbldap-groupmod.pl
blob: a35e7ea898de50a5d614ecc04f75573abd058dd2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#!/usr/bin/perl 

# $Id: smbldap-groupmod.pl,v 1.1.4.1 2002/06/17 18:35:08 jerry Exp $
#
#  This code was developped by IDEALX (http://IDEALX.org/) and
#  contributors (their names can be found in the CONTRIBUTORS file).
#
#                 Copyright (C) 2001-2002 IDEALX
#
#  This program is free software; you can redistribute it and/or
#  modify it under the terms of the GNU General Public License
#  as published by the Free Software Foundation; either version 2
#  of the License, or (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
#  USA.

# Purpose of smbldap-groupmod : group (posix) modification


use strict;
use smbldap_tools;
use smbldap_conf;


#####################

use Getopt::Std;
my %Options;

my $ok = getopts('og:n:m:x:?', \%Options);
if ( (!$ok) || (@ARGV < 1) || ($Options{'?'}) ) {
	print "Usage: $0 [-g gid [-o]] [-n name] [-m members(,)] [-x members (,)] groupname\n";
	print "  -g	new gid\n";
	print "  -o	gid is not unique\n";
	print "  -n	new group name\n";
	print "  -m	add members (comma delimited)\n";
	print "  -x	delete members (comma delimted)\n";
	print "  -?	show this help message\n";
	exit (1);
}

my $groupName = $ARGV[0];

if (!defined(get_group_dn($groupName))) {
    print "$0: group $groupName doesn't exist\n";
    exit (6);
}

my $newname = $Options{'n'};

my $nscd_status = system "/etc/init.d/nscd status >/dev/null 2>&1";

if ($nscd_status == 0) {
   system "/etc/init.d/nscd restart > /dev/null 2>&1";
}

my $gid = getgrnam($groupName);

my $tmp;
if (defined($tmp = $Options{'g'}) and $tmp =~ /\d+/) {
    if (!defined($Options{'o'})) {
	if (defined(getgrgid($tmp))) {
	    print "$0: gid $tmp exists\n";
	    exit (6);
	}
    }
    if (!($gid == $tmp)) {
	my $tmpldif =
"dn: cn=$groupName,$groupsdn
changetype: modify
replace: gidNumber
gidNumber: $tmp

";
	die "$0: error while modifying group $groupName\n"
	    unless (do_ldapmodify($tmpldif) == 0);
	undef $tmpldif;

    }
}

if (defined($newname)) {
    my $FILE="|$ldapmodrdn >/dev/null";
    open (FILE, $FILE) || die "$!\n";
    print FILE <<EOF;
cn=$groupName,$groupsdn
cn=$newname

EOF
    ;
    close FILE;
    die "$0: error while modifying group $groupName\n" if ($?);

    my $tmpldif =
"dn: cn=$newname,$groupsdn
changetype: modify
delete: cn
-
add: cn
cn: $newname

";
    die "$0: error while modifying group $groupName\n"
	unless (do_ldapmodify($tmpldif) == 0);
    undef $tmpldif;

}

# Add members
if (defined($Options{'m'})) {
	my $members = $Options{'m'};
	my @members = split( /,/, $members );
	my $member;
	foreach $member ( @members ) {
		my $tmpldif =
"dn: cn=$groupName,$groupsdn
changetype: modify
add: memberUid
memberUid: $member

";
		die "$0: error while modifying group $groupName\n"
			unless (do_ldapmodify($tmpldif) == 0);
		undef $tmpldif;
	}
}

# Delete members
if (defined($Options{'x'})) {
        my $members = $Options{'x'};
        my @members = split( /,/, $members );
        my $member;
        foreach $member ( @members ) {
                my $tmpldif =
"dn: cn=$groupName,$groupsdn
changetype: modify
delete: memberUid
memberUid: $member

";
                die "$0: error while modifying group $groupName\n"
                        unless (do_ldapmodify($tmpldif) == 0);
                undef $tmpldif;
        }
}

$nscd_status = system "/etc/init.d/nscd status >/dev/null 2>&1";

if ($nscd_status == 0) {
   system "/etc/init.d/nscd restart > /dev/null 2>&1";
}

exit (0);

############################################################

=head1 NAME

       smbldap-groupmod.pl - Modify a group

=head1 SYNOPSIS

       smbldap-groupmod.pl [-g gid [-o]] [-n group_name ] group

=head1 DESCRIPTION

       The smbldap-groupmod.pl command modifies the system account files to
       reflect the changes that are specified on the command line.
       The options which apply to the smbldap-groupmod command are

       -g gid The numerical value of the group's ID. This value must be
              unique, unless the -o option is used. The value must be non-
              negative. Any files which the old group ID is the file
              group ID must have the file group ID changed manually.

       -n group_name
              The name of the group will be changed from group to group_name.

       -m members
	      The members to be added to the group in comma-delimeted form.

       -x members
	      The members to be removed from the group in comma-delimted form.

=head1 EXAMPLES

       smbldap-groupmod.pl -g 253 development
	      This will change the GID of the 'development' group to '253'.

       smbldap-groupmod.pl -n Idiots Managers
	      This will change the name of the 'Managers' group to 'Idiots'.

       smbldap-groupmod.pl -m "jdoe,jsmith" "Domain Admins"
	      This will add 'jdoe' and 'jsmith' to the 'Domain Admins' group.

       smbldap-groupmod.pl -x "jdoe,jsmith" "Domain Admins"
	      This will remove 'jdoe' and 'jsmith' from the 'Domain Admins' group.

=head1 SEE ALSO

       groupmod(1)

=cut

#'