syncing examples
[Samba.git] / examples / LDAP / smbldap-tools / smbldap-groupmod.pl
blob7b5a46b06edf1035de27af6baf56e02499377527
1 # This code was developped by IDEALX (http://IDEALX.org/) and
2 # contributors (their names can be found in the CONTRIBUTORS file).
4 # Copyright (C) 2001-2002 IDEALX
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.
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.
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.
21 # Purpose of smbldap-groupmod : group (posix) modification
24 use strict;
25 use smbldap_tools;
26 use smbldap_conf;
29 #####################
31 use Getopt::Std;
32 my %Options;
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);
46 my $groupName = $ARGV[0];
48 if (!defined(get_group_dn($groupName))) {
49 print "$0: group $groupName doesn't exist\n";
50 exit (6);
53 my $newname = $Options{'n'};
55 my $nscd_status = system "/etc/init.d/nscd status >/dev/null 2>&1";
57 if ($nscd_status == 0) {
58 system "/etc/init.d/nscd restart > /dev/null 2>&1";
61 my $gid = getgrnam($groupName);
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);
71 if (!($gid == $tmp)) {
72 my $tmpldif =
73 "dn: cn=$groupName,$groupsdn
74 changetype: modify
75 replace: gidNumber
76 gidNumber: $tmp
79 die "$0: error while modifying group $groupName\n"
80 unless (do_ldapmodify($tmpldif) == 0);
81 undef $tmpldif;
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
93 EOF
95 close FILE;
96 die "$0: error while modifying group $groupName\n" if ($?);
98 my $tmpldif =
99 "dn: cn=$newname,$groupsdn
100 changetype: modify
101 delete: cn
103 add: cn
104 cn: $newname
107 die "$0: error while modifying group $groupName\n"
108 unless (do_ldapmodify($tmpldif) == 0);
109 undef $tmpldif;
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
126 die "$0: error while modifying group $groupName\n"
127 unless (do_ldapmodify($tmpldif) == 0);
128 undef $tmpldif;
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
145 die "$0: error while modifying group $groupName\n"
146 unless (do_ldapmodify($tmpldif) == 0);
147 undef $tmpldif;
151 $nscd_status = system "/etc/init.d/nscd status >/dev/null 2>&1";
153 if ($nscd_status == 0) {
154 system "/etc/init.d/nscd restart > /dev/null 2>&1";
157 exit (0);
159 ############################################################
161 =head1 NAME
163 smbldap-groupmod.pl - Modify a group
165 =head1 SYNOPSIS
167 smbldap-groupmod.pl [-g gid [-o]] [-n group_name ] group
169 =head1 DESCRIPTION
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
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.
180 -n group_name
181 The name of the group will be changed from group to group_name.
183 -m members
184 The members to be added to the group in comma-delimeted form.
186 -x members
187 The members to be removed from the group in comma-delimted form.
189 =head1 EXAMPLES
191 smbldap-groupmod.pl -g 253 development
192 This will change the GID of the 'development' group to '253'.
194 smbldap-groupmod.pl -n Idiots Managers
195 This will change the name of the 'Managers' group to 'Idiots'.
197 smbldap-groupmod.pl -m "jdoe,jsmith" "Domain Admins"
198 This will add 'jdoe' and 'jsmith' to the 'Domain Admins' group.
200 smbldap-groupmod.pl -x "jdoe,jsmith" "Domain Admins"
201 This will remove 'jdoe' and 'jsmith' from the 'Domain Admins' group.
203 =head1 SEE ALSO
205 groupmod(1)
207 =cut