syncing examples
[samba.git] / examples / LDAP / smbldap-tools / smbldap-groupmod.pl
1 #  This code was developped by IDEALX (http://IDEALX.org/) and
2 #  contributors (their names can be found in the CONTRIBUTORS file).
3 #
4 #                 Copyright (C) 2001-2002 IDEALX
5 #
6 #  This program is free software; you can redistribute it and/or
7 #  modify it under the terms of the GNU General Public License
8 #  as published by the Free Software Foundation; either version 2
9 #  of the License, or (at your option) any later version.
10 #
11 #  This program is distributed in the hope that it will be useful,
12 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 #  GNU General Public License for more details.
15 #
16 #  You should have received a copy of the GNU General Public License
17 #  along with this program; if not, write to the Free Software
18 #  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19 #  USA.
20
21 # Purpose of smbldap-groupmod : group (posix) modification
22
23
24 use strict;
25 use smbldap_tools;
26 use smbldap_conf;
27
28
29 #####################
30
31 use Getopt::Std;
32 my %Options;
33
34 my $ok = getopts('og:n:m:x:?', \%Options);
35 if ( (!$ok) || (@ARGV < 1) || ($Options{'?'}) ) {
36         print "Usage: $0 [-g gid [-o]] [-n name] [-m members(,)] [-x members (,)] groupname\n";
37         print "  -g     new gid\n";
38         print "  -o     gid is not unique\n";
39         print "  -n     new group name\n";
40         print "  -m     add members (comma delimited)\n";
41         print "  -x     delete members (comma delimted)\n";
42         print "  -?     show this help message\n";
43         exit (1);
44 }
45
46 my $groupName = $ARGV[0];
47
48 if (!defined(get_group_dn($groupName))) {
49     print "$0: group $groupName doesn't exist\n";
50     exit (6);
51 }
52
53 my $newname = $Options{'n'};
54
55 my $nscd_status = system "/etc/init.d/nscd status >/dev/null 2>&1";
56
57 if ($nscd_status == 0) {
58    system "/etc/init.d/nscd restart > /dev/null 2>&1";
59 }
60
61 my $gid = getgrnam($groupName);
62
63 my $tmp;
64 if (defined($tmp = $Options{'g'}) and $tmp =~ /\d+/) {
65     if (!defined($Options{'o'})) {
66         if (defined(getgrgid($tmp))) {
67             print "$0: gid $tmp exists\n";
68             exit (6);
69         }
70     }
71     if (!($gid == $tmp)) {
72         my $tmpldif =
73 "dn: cn=$groupName,$groupsdn
74 changetype: modify
75 replace: gidNumber
76 gidNumber: $tmp
77
78 ";
79         die "$0: error while modifying group $groupName\n"
80             unless (do_ldapmodify($tmpldif) == 0);
81         undef $tmpldif;
82
83     }
84 }
85
86 if (defined($newname)) {
87     my $FILE="|$ldapmodrdn >/dev/null";
88     open (FILE, $FILE) || die "$!\n";
89     print FILE <<EOF;
90 cn=$groupName,$groupsdn
91 cn=$newname
92
93 EOF
94     ;
95     close FILE;
96     die "$0: error while modifying group $groupName\n" if ($?);
97
98     my $tmpldif =
99 "dn: cn=$newname,$groupsdn
100 changetype: modify
101 delete: cn
102 -
103 add: cn
104 cn: $newname
105
106 ";
107     die "$0: error while modifying group $groupName\n"
108         unless (do_ldapmodify($tmpldif) == 0);
109     undef $tmpldif;
110
111 }
112
113 # Add members
114 if (defined($Options{'m'})) {
115         my $members = $Options{'m'};
116         my @members = split( /,/, $members );
117         my $member;
118         foreach $member ( @members ) {
119                 my $tmpldif =
120 "dn: cn=$groupName,$groupsdn
121 changetype: modify
122 add: memberUid
123 memberUid: $member
124
125 ";
126                 die "$0: error while modifying group $groupName\n"
127                         unless (do_ldapmodify($tmpldif) == 0);
128                 undef $tmpldif;
129         }
130 }
131
132 # Delete members
133 if (defined($Options{'x'})) {
134         my $members = $Options{'x'};
135         my @members = split( /,/, $members );
136         my $member;
137         foreach $member ( @members ) {
138                 my $tmpldif =
139 "dn: cn=$groupName,$groupsdn
140 changetype: modify
141 delete: memberUid
142 memberUid: $member
143
144 ";
145                 die "$0: error while modifying group $groupName\n"
146                         unless (do_ldapmodify($tmpldif) == 0);
147                 undef $tmpldif;
148         }
149 }
150
151 $nscd_status = system "/etc/init.d/nscd status >/dev/null 2>&1";
152
153 if ($nscd_status == 0) {
154    system "/etc/init.d/nscd restart > /dev/null 2>&1";
155 }
156
157 exit (0);
158
159 ############################################################
160
161 =head1 NAME
162
163        smbldap-groupmod.pl - Modify a group
164
165 =head1 SYNOPSIS
166
167        smbldap-groupmod.pl [-g gid [-o]] [-n group_name ] group
168
169 =head1 DESCRIPTION
170
171        The smbldap-groupmod.pl command modifies the system account files to
172        reflect the changes that are specified on the command line.
173        The options which apply to the smbldap-groupmod command are
174
175        -g gid The numerical value of the group's ID. This value must be
176               unique, unless the -o option is used. The value must be non-
177               negative. Any files which the old group ID is the file
178               group ID must have the file group ID changed manually.
179
180        -n group_name
181               The name of the group will be changed from group to group_name.
182
183        -m members
184               The members to be added to the group in comma-delimeted form.
185
186        -x members
187               The members to be removed from the group in comma-delimted form.
188
189 =head1 EXAMPLES
190
191        smbldap-groupmod.pl -g 253 development
192               This will change the GID of the 'development' group to '253'.
193
194        smbldap-groupmod.pl -n Idiots Managers
195               This will change the name of the 'Managers' group to 'Idiots'.
196
197        smbldap-groupmod.pl -m "jdoe,jsmith" "Domain Admins"
198               This will add 'jdoe' and 'jsmith' to the 'Domain Admins' group.
199
200        smbldap-groupmod.pl -x "jdoe,jsmith" "Domain Admins"
201               This will remove 'jdoe' and 'jsmith' from the 'Domain Admins' group.
202
203 =head1 SEE ALSO
204
205        groupmod(1)
206
207 =cut
208
209 #'