[c5c522c] | 1 | #!/usr/bin/perl -w |
---|
| 2 | |
---|
| 3 | # $Id: smbldap-groupmod 2642 2006-09-04 11:12:56Z erwin $ |
---|
| 4 | # |
---|
| 5 | # This code was developped by IDEALX (http://IDEALX.org/) and |
---|
| 6 | # contributors (their names can be found in the CONTRIBUTORS file). |
---|
| 7 | # |
---|
| 8 | # Copyright (C) 2001-2002 IDEALX |
---|
| 9 | # |
---|
| 10 | # This program is free software; you can redistribute it and/or |
---|
| 11 | # modify it under the terms of the GNU General Public License |
---|
| 12 | # as published by the Free Software Foundation; either version 2 |
---|
| 13 | # of the License, or (at your option) any later version. |
---|
| 14 | # |
---|
| 15 | # This program is distributed in the hope that it will be useful, |
---|
| 16 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 17 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 18 | # GNU General Public License for more details. |
---|
| 19 | # |
---|
| 20 | # You should have received a copy of the GNU General Public License |
---|
| 21 | # along with this program; if not, write to the Free Software |
---|
| 22 | # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
---|
| 23 | # USA. |
---|
| 24 | |
---|
| 25 | # Purpose of smbldap-groupmod : group (posix) modification |
---|
| 26 | |
---|
| 27 | |
---|
| 28 | use strict; |
---|
| 29 | use FindBin; |
---|
| 30 | use FindBin qw($RealBin); |
---|
| 31 | use lib "$RealBin/"; |
---|
| 32 | use smbldap_tools; |
---|
| 33 | |
---|
| 34 | ##################### |
---|
| 35 | |
---|
| 36 | use Getopt::Std; |
---|
| 37 | my %Options; |
---|
| 38 | |
---|
| 39 | my $ok = getopts('ag:n:m:or:s:t:x:?', \%Options); |
---|
| 40 | if ( (!$ok) || (@ARGV < 1) || ($Options{'?'}) ) { |
---|
| 41 | print_banner; |
---|
| 42 | print "Usage: $0 [-a] [-g gid [-o]] [-n name] [-m members(,)] [-x members (,)] [-r rid] [-s sid] [-t type] groupname\n"; |
---|
| 43 | print " -a add automatic group mapping entry\n"; |
---|
| 44 | print " -g new gid\n"; |
---|
| 45 | print " -o gid is not unique\n"; |
---|
| 46 | print " -n new group name\n"; |
---|
| 47 | print " -m add members (comma delimited)\n"; |
---|
| 48 | print " -r group-rid\n"; |
---|
| 49 | print " -s group-sid\n"; |
---|
[3187b26] | 50 | print " -t group-type\n"; |
---|
[c5c522c] | 51 | print " -x delete members (comma delimted)\n"; |
---|
| 52 | print " -? show this help message\n"; |
---|
| 53 | exit (1); |
---|
| 54 | } |
---|
| 55 | |
---|
| 56 | my $groupName = $ARGV[0]; |
---|
| 57 | my $group_entry; |
---|
| 58 | |
---|
| 59 | my $ldap_master=connect_ldap_master(); |
---|
| 60 | |
---|
| 61 | if (! ($group_entry = read_group_entry($groupName))) { |
---|
| 62 | print "$0: group $groupName doesn't exist\n"; |
---|
| 63 | exit (6); |
---|
| 64 | } |
---|
| 65 | |
---|
| 66 | my $newname = $Options{'n'}; |
---|
| 67 | |
---|
[3187b26] | 68 | # my $nscd_status = system "/etc/init.d/nscd status >/dev/null 2>&1"; |
---|
| 69 | # |
---|
| 70 | # if ($nscd_status == 0) { |
---|
| 71 | # system "/etc/init.d/nscd restart > /dev/null 2>&1"; |
---|
| 72 | # } |
---|
| 73 | system "nscd -i passwd; nscd -i group"; |
---|
[c5c522c] | 74 | |
---|
| 75 | my $gid = getgrnam($groupName); |
---|
| 76 | unless (defined ($gid)) { |
---|
| 77 | print "$0: group $groupName not found!\n"; |
---|
| 78 | exit(6); |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | my $tmp; |
---|
| 82 | if (defined($tmp = $Options{'g'}) and $tmp =~ /\d+/) { |
---|
| 83 | if (!defined($Options{'o'})) { |
---|
| 84 | if (defined(getgrgid($tmp))) { |
---|
| 85 | print "$0: gid $tmp exists\n"; |
---|
| 86 | exit (6); |
---|
| 87 | } |
---|
| 88 | } |
---|
| 89 | if (!($gid == $tmp)) { |
---|
| 90 | my $modify = $ldap_master->modify ( "cn=$groupName,$config{groupsdn}", |
---|
| 91 | changes => [ |
---|
| 92 | replace => [gidNumber => $tmp] |
---|
| 93 | ] |
---|
| 94 | ); |
---|
| 95 | $modify->code && die "failed to modify entry: ", $modify->error ; |
---|
| 96 | } |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | |
---|
| 100 | if (defined($newname)) { |
---|
| 101 | my $modify = $ldap_master->moddn ( |
---|
| 102 | "cn=$groupName,$config{groupsdn}", |
---|
| 103 | newrdn => "cn=$newname", |
---|
| 104 | deleteoldrdn => "1", |
---|
| 105 | newsuperior => "$config{groupsdn}" |
---|
| 106 | ); |
---|
| 107 | $modify->code && die "failed to modify entry: ", $modify->error ; |
---|
| 108 | # take down session |
---|
| 109 | } |
---|
| 110 | |
---|
| 111 | # Add members |
---|
| 112 | if (defined($Options{'m'})) { |
---|
| 113 | my $members = $Options{'m'}; |
---|
| 114 | my @members = split( /,/, $members ); |
---|
| 115 | my $member; |
---|
| 116 | foreach $member ( @members ) { |
---|
| 117 | my $group_entry=read_group_entry($groupName); |
---|
| 118 | $config{groupsdn}=$group_entry->dn; |
---|
| 119 | if (is_unix_user($member) || is_nonldap_unix_user($member)) { |
---|
| 120 | if (is_group_member($config{groupsdn},$member)) { |
---|
| 121 | print "User $member already in the group\n"; |
---|
| 122 | } else { |
---|
| 123 | print "adding user $member to group $groupName\n"; |
---|
| 124 | my $modify = $ldap_master->modify ($config{groupsdn}, |
---|
| 125 | changes => [ |
---|
| 126 | add => [memberUid => $member] |
---|
| 127 | ] |
---|
| 128 | ); |
---|
| 129 | $modify->code && warn "failed to add entry: ", $modify->error ; |
---|
| 130 | } |
---|
| 131 | } else { |
---|
| 132 | print "User $member does not exist: create it first !\n"; |
---|
| 133 | } |
---|
| 134 | } |
---|
| 135 | } |
---|
| 136 | |
---|
| 137 | # Delete members |
---|
| 138 | if (defined($Options{'x'})) { |
---|
| 139 | my $members = $Options{'x'}; |
---|
| 140 | my @members = split( /,/, $members ); |
---|
| 141 | my $member; |
---|
| 142 | foreach $member ( @members ) { |
---|
| 143 | my $user_entry=read_user_entry($member); |
---|
| 144 | my $group_entry=read_group_entry($groupName); |
---|
| 145 | $config{groupsdn}=$group_entry->dn; |
---|
| 146 | if (is_group_member("$config{groupsdn}",$member)) { |
---|
| 147 | my $delete=1; |
---|
| 148 | if (defined $group_entry->get_value('sambaSID')) { |
---|
| 149 | if ($group_entry->get_value('sambaSID') eq $user_entry->get_value('sambaPrimaryGroupSID')) { |
---|
| 150 | $delete=0; |
---|
| 151 | print "Cannot delete user ($member) from his primary group ($groupName)\n"; |
---|
| 152 | } |
---|
| 153 | } |
---|
| 154 | if ($delete eq 1) { |
---|
| 155 | print "deleting user $member from group $groupName\n"; |
---|
| 156 | my $modify = $ldap_master->modify ($config{groupsdn}, |
---|
| 157 | changes => [ |
---|
| 158 | delete => [memberUid => $member] |
---|
| 159 | ] |
---|
| 160 | ); |
---|
| 161 | $modify->code && warn "failed to delete entry: ", $modify->error ; |
---|
| 162 | } |
---|
| 163 | } else { |
---|
| 164 | print "User $member is not in the group $groupName!\n"; |
---|
| 165 | } |
---|
| 166 | } |
---|
| 167 | } |
---|
| 168 | |
---|
| 169 | my $group_sid; |
---|
| 170 | if ($tmp= $Options{'s'}) { |
---|
| 171 | if ($tmp =~ /^S-(?:\d+-)+\d+$/) { |
---|
| 172 | $group_sid = $tmp; |
---|
| 173 | } else { |
---|
| 174 | print "$0: illegal group-rid $tmp\n"; |
---|
| 175 | exit(7); |
---|
| 176 | } |
---|
| 177 | } elsif ($Options{'r'} || $Options{'a'}) { |
---|
| 178 | my $group_rid; |
---|
| 179 | if ($tmp= $Options{'r'}) { |
---|
| 180 | if ($tmp =~ /^\d+$/) { |
---|
| 181 | $group_rid = $tmp; |
---|
| 182 | } else { |
---|
| 183 | print "$0: illegal group-rid $tmp\n"; |
---|
| 184 | exit(7); |
---|
| 185 | } |
---|
| 186 | } else { |
---|
| 187 | # algorithmic mapping |
---|
| 188 | $group_rid = 2*$gid+1001; |
---|
| 189 | } |
---|
| 190 | $group_sid = $config{SID}.'-'.$group_rid; |
---|
| 191 | } |
---|
| 192 | |
---|
| 193 | if ($group_sid) { |
---|
| 194 | my @adds; |
---|
| 195 | my @mods; |
---|
| 196 | push(@mods, 'sambaSID' => $group_sid); |
---|
| 197 | |
---|
| 198 | if ($tmp= $Options{'t'}) { |
---|
| 199 | my $group_type; |
---|
| 200 | if (defined($group_type = &group_type_by_name($tmp))) { |
---|
| 201 | push(@mods, 'sambaGroupType' => $group_type); |
---|
| 202 | } else { |
---|
| 203 | print "$0: unknown group type $tmp\n"; |
---|
| 204 | exit(8); |
---|
| 205 | } |
---|
| 206 | } else { |
---|
| 207 | if (! defined($group_entry->get_value('sambaGroupType'))) { |
---|
| 208 | push(@mods, 'sambaGroupType' => group_type_by_name('domain')); |
---|
| 209 | } |
---|
| 210 | } |
---|
| 211 | |
---|
| 212 | my @oc = $group_entry->get_value('objectClass'); |
---|
| 213 | unless (grep($_ =~ /^sambaGroupMapping$/i, @oc)) { |
---|
| 214 | push (@adds, 'objectClass' => 'sambaGroupMapping'); |
---|
| 215 | } |
---|
| 216 | |
---|
| 217 | my $modify = $ldap_master->modify ( "cn=$groupName,$config{groupsdn}", |
---|
| 218 | changes => [ |
---|
| 219 | 'add' => [ @adds ], |
---|
| 220 | 'replace' => [ @mods ] |
---|
| 221 | ] |
---|
| 222 | ); |
---|
| 223 | $modify->code && warn "failed to delete entry: ", $modify->error ; |
---|
| 224 | } |
---|
| 225 | |
---|
[3187b26] | 226 | # $nscd_status = system "/etc/init.d/nscd status >/dev/null 2>&1"; |
---|
| 227 | # |
---|
| 228 | # if ($nscd_status == 0) { |
---|
| 229 | # system "/etc/init.d/nscd restart > /dev/null 2>&1"; |
---|
| 230 | # } |
---|
| 231 | system "nscd -i passwd; nscd -i group"; |
---|
[c5c522c] | 232 | |
---|
| 233 | # take down session |
---|
| 234 | $ldap_master->unbind; |
---|
| 235 | |
---|
| 236 | exit (0); |
---|
| 237 | |
---|
| 238 | ############################################################ |
---|
| 239 | |
---|
| 240 | =head1 NAME |
---|
| 241 | |
---|
| 242 | smbldap-groupmod - Modify a group |
---|
| 243 | |
---|
| 244 | =head1 SYNOPSIS |
---|
| 245 | |
---|
| 246 | smbldap-groupmod [-g gid [-o]] [-a] [-r rid] [-s sid] [-t group type] |
---|
| 247 | [-n group_name ] [-m members(,)] [-x members (,)] group |
---|
| 248 | |
---|
| 249 | =head1 DESCRIPTION |
---|
| 250 | |
---|
| 251 | The smbldap-groupmod command modifies the system account files to |
---|
| 252 | reflect the changes that are specified on the command line. |
---|
| 253 | The options which apply to the smbldap-groupmod command are |
---|
| 254 | |
---|
| 255 | -g gid The numerical value of the group's ID. This value must be |
---|
| 256 | unique, unless the -o option is used. The value must be non- |
---|
| 257 | negative. Any files which the old group ID is the file |
---|
| 258 | group ID must have the file group ID changed manually. |
---|
| 259 | |
---|
| 260 | -n group_name |
---|
| 261 | The name of the group will be changed from group to group_name. |
---|
| 262 | |
---|
| 263 | -m members |
---|
| 264 | The members to be added to the group in comma-delimeted form. |
---|
| 265 | |
---|
| 266 | -x members |
---|
| 267 | The members to be removed from the group in comma-delimted form. |
---|
| 268 | |
---|
| 269 | -a |
---|
| 270 | add an automatic Security ID for the group (SID). |
---|
| 271 | The rid of the group is calculated from the gidNumber of the |
---|
| 272 | group as rid=2*gidNumber+1001. Thus the resulted SID of the |
---|
| 273 | group is $SID-$rid where $SID and $rid are the domain SID and |
---|
| 274 | the group rid |
---|
| 275 | |
---|
| 276 | -s sid |
---|
| 277 | set the group SID. |
---|
| 278 | The SID must be unique and defined with the domain Security ID |
---|
| 279 | ($SID) like sid=$SID-rid where rid is the group rid. |
---|
| 280 | |
---|
| 281 | -r rid |
---|
| 282 | set the group rid. |
---|
| 283 | The SID is then calculated as sid=$SID-rid where $SID is the |
---|
| 284 | domain Security ID. |
---|
| 285 | |
---|
| 286 | -t group type |
---|
| 287 | set the NT Group type for the new group. Available values are |
---|
| 288 | 2 (domain group), 4 (local group) and 5 (builtin group). |
---|
| 289 | The default group type is 2. |
---|
| 290 | |
---|
| 291 | =head1 EXAMPLES |
---|
| 292 | |
---|
| 293 | smbldap-groupmod -g 253 development |
---|
| 294 | This will change the GID of the 'development' group to '253'. |
---|
| 295 | |
---|
| 296 | smbldap-groupmod -n Idiots Managers |
---|
| 297 | This will change the name of the 'Managers' group to 'Idiots'. |
---|
| 298 | |
---|
| 299 | smbldap-groupmod -m "jdoe,jsmith" "Domain Admins" |
---|
| 300 | This will add 'jdoe' and 'jsmith' to the 'Domain Admins' group. |
---|
| 301 | |
---|
| 302 | smbldap-groupmod -x "jdoe,jsmith" "Domain Admins" |
---|
| 303 | This will remove 'jdoe' and 'jsmith' from the 'Domain Admins' group. |
---|
| 304 | |
---|
| 305 | =head1 SEE ALSO |
---|
| 306 | |
---|
| 307 | groupmod(1) |
---|
| 308 | |
---|
| 309 | =cut |
---|
| 310 | |
---|
| 311 | #' |
---|