~ubuntu-branches/ubuntu/raring/smbldap-tools/raring-proposed

« back to all changes in this revision

Viewing changes to smbldap-groupmod.pl

  • Committer: Package Import Robot
  • Author(s): Leo Iannacone
  • Date: 2011-09-27 18:05:13 UTC
  • mfrom: (6.1.2 sid)
  • Revision ID: package-import@ubuntu.com-20110927180513-2b9g9u4wkqeh4kxn
Tags: 0.9.7-1ubuntu1
* Merge from debian unstable (LP: #889308).  Remaining changes:
  - Apply patch from rdratlos to resolve being unable to join a Windows
    7 or Windows 2008 machine to a Samba domain due to the use of cached
    nss credentials. (LP: #814898)
  - 0020_original_doc_html_index.patch: Add index html file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!@PERL_CMD@
 
2
 
 
3
# $Id$
 
4
 
 
5
#  This code was developped by Jerome Tournier (jtournier@gmail.com) and
 
6
#  contributors (their names can be found in the CONTRIBUTORS file).
 
7
 
 
8
#  This was first contributed by IDEALX (http://www.opentrust.com/)
 
9
 
 
10
#  This program is free software: you can redistribute it and/or modify
 
11
#  it under the terms of the GNU General Public License as published by
 
12
#  the Free Software Foundation, either version 2 of the License, or
 
13
#  (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, see <http://www.gnu.org/licenses/>.
 
22
 
 
23
# Purpose of smbldap-groupmod : group (posix) modification
 
24
 
 
25
 
 
26
use strict;
 
27
use warnings;
 
28
use smbldap_tools;
 
29
 
 
30
#####################
 
31
 
 
32
use Getopt::Std;
 
33
my %Options;
 
34
 
 
35
my $ok = getopts('ag:n:m:or:s:t:x:?', \%Options);
 
36
if ( (!$ok) || (@ARGV < 1) || ($Options{'?'}) ) {
 
37
    print_banner;
 
38
    print "Usage: $0 [-a] [-g gid] [-n name] [-m members(,)] [-o] [-r rid] [-s sid] [-t type] [-x members (,)] groupname\n";
 
39
    print "  -a   add automatic group mapping entry\n";
 
40
    print "  -g   new gid\n";
 
41
    print "  -n   new group name\n";
 
42
    print "  -m   add members (comma delimited)\n";
 
43
    print "  -o   gid is not unique\n";
 
44
    print "  -r   group-rid\n";
 
45
    print "  -s   group-sid\n";
 
46
    print "  -t   group-type\n"; 
 
47
    print "  -x   delete members (comma delimted)\n";
 
48
    print "  -?   show this help message\n";
 
49
    exit (1);
 
50
}
 
51
 
 
52
my $groupName = $ARGV[0];
 
53
my $group_entry;
 
54
 
 
55
my $ldap_master=connect_ldap_master();
 
56
 
 
57
if (! ($group_entry = read_group_entry($groupName))) {
 
58
    print "$0: group $groupName doesn't exist\n";
 
59
    exit (6);
 
60
}
 
61
 
 
62
nsc_invalidate("group");
 
63
 
 
64
my $gid=$group_entry->get_value('gidNumber');
 
65
 
 
66
unless (defined ($gid)) {
 
67
    print "$0: group $groupName not found!\n";
 
68
    exit(6);
 
69
}
 
70
 
 
71
my $tmp;
 
72
if (defined($tmp = $Options{'g'}) and $tmp =~ /\d+/) {
 
73
    if (!defined($Options{'o'})) {
 
74
        if (defined(getgrgid($tmp))) {
 
75
            print "$0: gid $tmp exists\n";
 
76
            exit (6);
 
77
        }
 
78
    }
 
79
    if (!($gid == $tmp)) {
 
80
        my $modify = $ldap_master->modify ( "cn=$groupName,$config{groupsdn}",
 
81
                                            changes => [
 
82
                                                        replace => [gidNumber => $tmp]
 
83
                                                        ]
 
84
                                            );
 
85
        $modify->code && die "failed to modify entry: ", $modify->error ;
 
86
    }
 
87
}
 
88
 
 
89
if (defined(my $newname = $Options{'n'})) {
 
90
    my $modify = $ldap_master->moddn (
 
91
                                      "cn=$groupName,$config{groupsdn}",
 
92
                                      newrdn => "cn=$newname",
 
93
                                      deleteoldrdn => "1",
 
94
                                      newsuperior => "$config{groupsdn}"
 
95
                                      );
 
96
    $modify->code && die "failed to modify entry: ", $modify->error ;
 
97
    $groupName = $newname;
 
98
    $group_entry = read_group_entry($groupName)
 
99
}
 
100
 
 
101
# Add members
 
102
if (defined($Options{'m'})) {
 
103
    my @members = ();
 
104
    foreach my $member (split(/,/, $Options{'m'})) {
 
105
        if (my $user_entry = read_user_entry($member)) {
 
106
            # Canonize user name
 
107
            $member = $user_entry->get_value('uid');
 
108
        } elsif (my @user_entry = getpwnam($member)) {
 
109
            # Canonize user name
 
110
            $member = $user_entry[0];
 
111
        } else {
 
112
            warn "User does not exist: $member\n";
 
113
            next;
 
114
        }
 
115
 
 
116
        if (is_group_member($group_entry->dn, $member)) {
 
117
            warn "User already in the group: $member\n";
 
118
            next;
 
119
        }
 
120
 
 
121
        push(@members, $member);
 
122
    }
 
123
 
 
124
    if (@members) {
 
125
        my $modify = $ldap_master->modify($group_entry->dn,
 
126
            add => {memberUid => \@members},
 
127
        );
 
128
        $modify->code && warn "Failed to add memberUid: ", $modify->error;
 
129
    }
 
130
}
 
131
 
 
132
# Delete members
 
133
if (defined($Options{'x'})) {
 
134
    my @members = ();
 
135
    foreach my $member (split(/,/, $Options{'x'})) {
 
136
        if (my $user_entry = read_user_entry($member)) {
 
137
            # Canonize user name
 
138
            $member = $user_entry->get_value('uid');
 
139
 
 
140
            my $user_pgroup_sid = $group_entry->get_value('sambaPrimaryGroupSID');
 
141
            my $group_sid = $group_entry->get_value('sambaSID');
 
142
            if (defined($user_pgroup_sid) && defined($group_sid) &&
 
143
                $user_pgroup_sid eq $group_sid) {
 
144
                warn "Cannot delete user from its primary group: $member\n";
 
145
                next;
 
146
            }
 
147
        } elsif (my @user_entry = getpwnam($member)) {
 
148
            # Canonize user name
 
149
            $member = $user_entry[0];
 
150
        }
 
151
 
 
152
        if (!is_group_member($group_entry->dn, $member)) {
 
153
            warn "User is not in the group: $member\n";
 
154
            next;
 
155
        }
 
156
 
 
157
        push(@members, $member);
 
158
    }
 
159
 
 
160
    if (@members) {
 
161
        my $modify = $ldap_master->modify($group_entry->dn,
 
162
            delete => {memberUid => \@members},
 
163
        );
 
164
        $modify->code && warn "Failed to delete memberUid: ", $modify->error;
 
165
    };
 
166
}
 
167
 
 
168
my $group_sid;
 
169
if ($tmp= $Options{'s'}) {
 
170
    if ($tmp =~ /^S-(?:\d+-)+\d+$/) {
 
171
        $group_sid = $tmp;
 
172
    } else {
 
173
        print "$0: illegal group-rid $tmp\n";
 
174
        exit(7);
 
175
    }
 
176
} elsif ($Options{'r'} || $Options{'a'}) {
 
177
    my $group_rid;
 
178
    if ($tmp= $Options{'r'}) {
 
179
        if ($tmp =~ /^\d+$/) {
 
180
            $group_rid = $tmp;
 
181
        } else {
 
182
            print "$0: illegal group-rid $tmp\n";
 
183
            exit(7);
 
184
        }
 
185
    } else {
 
186
        $group_rid = group_next_rid($gid);
 
187
    }
 
188
    $group_sid = $config{SID}.'-'.$group_rid;
 
189
}
 
190
 
 
191
if ($group_sid) {
 
192
    my @adds;
 
193
    my @mods;
 
194
    push(@mods, 'sambaSID' => $group_sid);
 
195
 
 
196
    if ($tmp= $Options{'t'}) {
 
197
        my $group_type;
 
198
        if (defined($group_type = &group_type_by_name($tmp))) {
 
199
            push(@mods, 'sambaGroupType' => $group_type);
 
200
        } else {
 
201
            print "$0: unknown group type $tmp\n";
 
202
            exit(8);
 
203
        }
 
204
    } else {
 
205
        if (! defined($group_entry->get_value('sambaGroupType'))) {
 
206
            push(@mods, 'sambaGroupType' => group_type_by_name('domain'));
 
207
        }
 
208
    }
 
209
 
 
210
    my @oc = $group_entry->get_value('objectClass');
 
211
    unless (grep($_ =~ /^sambaGroupMapping$/i, @oc)) {
 
212
        push (@adds, 'objectClass' => 'sambaGroupMapping');
 
213
    }
 
214
 
 
215
    my $modify = $ldap_master->modify ( "cn=$groupName,$config{groupsdn}",
 
216
                                        changes => [
 
217
                                                    'add' => [ @adds ],
 
218
                                                    'replace' => [ @mods ]
 
219
                                                    ]
 
220
                                        );
 
221
    $modify->code && warn "failed to delete entry: ", $modify->error ;
 
222
}
 
223
 
 
224
nsc_invalidate("group");
 
225
 
 
226
# take down session
 
227
$ldap_master->unbind;
 
228
 
 
229
exit (0);
 
230
 
 
231
############################################################
 
232
 
 
233
=head1 NAME
 
234
 
 
235
smbldap-groupmod - Modify a group
 
236
 
 
237
=head1 SYNOPSIS
 
238
 
 
239
smbldap-groupmod [-g gid [-o]] [-a] [-r rid] [-s sid] [-t group type] [-n group_name ] [-m members(,)] [-x members (,)] group
 
240
 
 
241
=head1 DESCRIPTION
 
242
 
 
243
The smbldap-groupmod 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
 
244
 
 
245
-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 roup ID must have the file group ID changed manually.
 
246
 
 
247
-n group_name
 
248
   The name of the group will be changed from group to group_name.
 
249
 
 
250
-m members
 
251
   The members to be added to the group in comma-delimeted form.
 
252
 
 
253
-x members
 
254
   The members to be removed from the group in comma-delimted form.
 
255
 
 
256
-a
 
257
   add an automatic Security ID for the group (SID).
 
258
 
 
259
-s sid
 
260
   set the group SID.
 
261
   The SID must be unique and defined with the domain Security ID ($SID) like sid=$SID-rid where rid is the group rid.
 
262
 
 
263
-r rid
 
264
   set the group rid.
 
265
   The SID is then calculated as sid=$SID-rid where $SID is the domain Security ID.
 
266
 
 
267
-t group type
 
268
   set the NT Group type for the new group. Available values are 2 (domain group), 4 (local group) and 5 (builtin group). The default group type is 2.
 
269
 
 
270
=head1 EXAMPLES
 
271
 
 
272
smbldap-groupmod -g 253 development
 
273
This will change the GID of the 'development' group to '253'.
 
274
 
 
275
smbldap-groupmod -n Idiots Managers
 
276
This will change the name of the 'Managers' group to 'Idiots'.
 
277
 
 
278
smbldap-groupmod -m "jdoe,jsmith" "Domain Admins"
 
279
This will add 'jdoe' and 'jsmith' to the 'Domain Admins' group.
 
280
 
 
281
smbldap-groupmod -x "jdoe,jsmith" "Domain Admins"
 
282
This will remove 'jdoe' and 'jsmith' from the 'Domain Admins' group.
 
283
 
 
284
=head1 SEE ALSO
 
285
 
 
286
groupmod(1)
 
287
 
 
288
=cut
 
289
 
 
290
#'