-
Notifications
You must be signed in to change notification settings - Fork 20
/
convertgroup.pl
198 lines (154 loc) · 5.16 KB
/
convertgroup.pl
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
#!/usr/bin/perl
#====================================================================
# Script to convert groups for server migration
#
# Copyright (C) 2011 Clement OUDOT
# Copyright (C) 2011 LTB-project.org
#
# 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.
#
# GPL License: http://www.gnu.org/licenses/gpl.txt
#
#====================================================================
#====================================================================
# Configuration
#====================================================================
# Source group object class
my $srcGroupOC = "nsRoleDefinition";
# Source group attribute name
my $srcGroupAT = "cn";
# Source member object class
my $srcMemberOC = "inetOrgPerson";
# Source reverse membership attribute
my $srcMemberAT = "nsRoleDN";
# Destination group objectClass
my $dstGroupOC = "groupOfUniqueNames";
# Destination group attribute name (RDN)
my $dstGroupATName = "cn";
# Destination group branch
my $dstGroupBranch = "ou=groups,dc=example,dc=com";
# Destination group attribute member
my $dstGroupATMember = "uniqueMember";
# Destination group attribute member value
my $dstGroupATMemberValue = "dn";
# Default member value for empty groups
my $dstGroupATMemberDefaultValue = "cn=empty";
# Branches to exclude
my $branch_exclude = [
qw(
cn=config
cn=monitor
)
];
# LDIF Options
# Sort attributes
my $ldif_sort = 0;
# DN encoding
# none, base64 or canonical
my $ldif_encode = "base64";
# Convert attribute names in lowercase
my $ldif_lowercase = 0;
# Columns wrapping
my $ldif_wrap = 78;
#====================================================================
# Modules
#====================================================================
use Net::LDAP::LDIF;
use Net::LDAP::Util qw/ldap_explode_dn/;
use strict;
use utf8;
#====================================================================
# Get command line arguments
#====================================================================
# Input file
my $file = shift @ARGV;
unless ($file) {
print STDERR "Usage: $0 file.ldif\n";
exit 1;
}
my $inldif = Net::LDAP::LDIF->new($file);
# Output file
my $outldif = Net::LDAP::LDIF->new(
"$file.groupconvert", "w",
sort => $ldif_sort,
encode => $ldif_encode,
lowercase => $ldif_lowercase,
wrap => $ldif_wrap,
);
my $groupMembership;
my $groups;
# Parse source LDIF
while ( not $inldif->eof() ) {
my $entry = $inldif->read_entry();
next unless $entry;
if ( $inldif->error() ) {
print STDERR "Error msg: ", $inldif->error(), "\n";
print STDERR "Error lines:\n", $inldif->error_lines(), "\n";
}
# Test 1: excluded branch
my $dn = $entry->dn();
my $exclude_entry = 0;
foreach my $branch (@$branch_exclude) {
if ( $dn =~ /$branch$/i ) {
print STDERR "DN $dn exlcuded (belongs to branch $branch)\n";
$exclude_entry = 1;
last;
}
}
next if $exclude_entry;
# Check objectClass
next unless ( $entry->exists('objectClass') );
my $ocvalues = $entry->get_value( 'objectClass', asref => 1 );
# Reverse membership
if ( grep ( /^$srcMemberOC$/i, @$ocvalues ) ) {
# Check reverse membership attribute
next unless ( $entry->exists($srcMemberAT) );
# Add user to group
my $userRef =
( $dstGroupATMemberValue eq "dn" )
? $entry->dn()
: $entry->get_value($dstGroupATMemberValue);
foreach my $group ( $entry->get_value($srcMemberAT) ) {
# This value is a DN, extract the RDN
my $dnval = ldap_explode_dn( $group, casefold => 'lower' );
my $groupName = $dnval->[0]->{$srcGroupAT};
push @{ $groupMembership->{$groupName} }, $userRef;
}
}
elsif ( grep ( /^$srcGroupOC$/i, @$ocvalues ) ) {
next unless ( $entry->exists($srcGroupAT) );
push @$groups, scalar $entry->get_value($srcGroupAT);
}
}
foreach my $newGroup (@$groups) {
# Build new group
my $newGroupDN = $dstGroupATName . "=" . $newGroup . "," . $dstGroupBranch;
my $newGroupEntry = Net::LDAP::Entry->new();
$newGroupEntry->dn($newGroupDN);
$newGroupEntry->add( 'objectClass' => $dstGroupOC );
# Find members
if ( exists $groupMembership->{$newGroup} ) {
$newGroupEntry->add(
$dstGroupATMember => $groupMembership->{$newGroup} );
}
else {
$newGroupEntry->add(
$dstGroupATMember => $dstGroupATMemberDefaultValue );
}
# Write entry
$outldif->write_entry($newGroupEntry);
}
#====================================================================
# Exit
#====================================================================
$inldif->done();
$outldif->done();
exit 0;