syncing examples
[bbaumbach/samba-autobuild/.git] / examples / LDAP / smbldap-tools / smbldap-migrate-groups.pl
1 #!/usr/bin/perl
2
3 #  This code was developped by IDEALX (http://IDEALX.org/) and
4 #  contributors (their names can be found in the CONTRIBUTORS file).
5 #
6 #                 Copyright (C) 2002 IDEALX
7 #
8 #  This program is free software; you can redistribute it and/or
9 #  modify it under the terms of the GNU General Public License
10 #  as published by the Free Software Foundation; either version 2
11 #  of the License, or (at your option) any later version.
12 #
13 #  This program is distributed in the hope that it will be useful,
14 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
15 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 #  GNU General Public License for more details.
17 #
18 #  You should have received a copy of the GNU General Public License
19 #  along with this program; if not, write to the Free Software
20 #  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
21 #  USA.
22
23 # Purpose of smbldap-migrate-groups : to parse a Windows
24 # group dump and populate Unix groups
25 # Reads group dump on stdin
26
27
28 use strict;
29 use smbldap_tools;
30 use smbldap_conf;
31 use Getopt::Std;
32
33
34
35 sub process_rec_group
36 {
37     my ($group, $mb) = @_;
38     my @members;
39     
40     if (!(@members = group_get_members($group))) {
41         return 0;
42     }
43
44     foreach my $m (@members) {
45         if ( !($m =~ m/^\*/) ) {
46             push @{$mb}, $m;
47         } else {
48             my $gname = $m;
49             $gname =~ s/^.//;
50             if (!process_rec_group($gname, $mb)) {
51                 print "recursive group not added : $gname\n";
52             }
53         }
54     }
55 }
56
57
58 # given a group dn and a list of members, update the group
59 sub modify_group
60 {
61     my ($group, $dn_line, @members, $recgroup) = @_;
62     my $m;
63     my @new_mb;
64
65     foreach $m (@members) {
66         if ( ($m =~ m/^\*/) ) {
67             my $gname = $m;
68             $gname =~ s/^.//;
69             if (!$recgroup) {
70                 print "recursive group not added : $gname\n";
71             } else {
72                 if (!process_rec_group($gname, \@new_mb)) {
73                     print "recursive group not added : $gname\n";
74                 }
75             }
76         } else {
77             push @new_mb, $m;
78         }
79     }
80
81     # new_mb contains flat members from group dump
82     # now append them to existing members
83     push @new_mb, group_get_members($group);
84     # uniq them
85     my %saw;
86     @saw{@new_mb} = ();
87     @new_mb = keys %saw;
88
89     my $nmb = $#new_mb + 1;
90     print STDERR "Group $group now has $nmb member(s)\n"; 
91     
92     my $mbs;
93     foreach $m (@new_mb) {
94             $mbs .= "memberUid: $m\n";
95     }
96
97     my $mods="$dn_line
98 changetype: modify
99 replace: memberUid
100 $mbs
101 ";
102
103     #print "$mods\n";
104     my $tmpldif =
105 "$mods
106 ";
107
108     die "$0: error while modifying group $group\n"
109         unless (do_ldapmodify($tmpldif) == 0);
110     undef $tmpldif;
111 }
112
113 sub display_group
114 {
115     my ($group, @members) = @_;
116
117     print "Group name $group\n";
118     print "Members\n";
119     my $m;
120     my $i = 0;
121     foreach $m (@members) {
122         print "$m ";
123         if ($i % 5 == 0) {
124             print "\n";
125         }
126         $i++;
127     }
128 }
129
130 sub process_group
131 {
132     my ($group, @members, $nocreate, $noupdate, $recgroup) = @_;
133
134     my $dn_line;
135     if (!defined($dn_line = get_group_dn($group))) {
136         # group not found, create it ?
137         if (!$nocreate) {
138             system "/usr/local/sbin/smbldap-groupadd.pl \"$group\"; sleep 5";
139             if (!defined($dn_line = get_group_dn($group))) {
140                 return 1;
141             }
142             modify_group($group, $dn_line, @members, $recgroup);
143         } else {
144             # don't create
145             print "not created:\n";
146             display_group($group, @members);
147         }
148     } else {
149         # group found, update it ?
150         if (!$noupdate) {
151             modify_group($group, $dn_line, @members, $recgroup);
152         } else {
153             # don't update
154             print "not updated:\n";
155             display_group($group, @members);    
156         }
157     }
158 }
159
160 ###################################################
161
162 my %Options;
163
164 my $ok = getopts('CUr?', \%Options);
165 if ( (!$ok) || ($Options{'?'}) ) {
166     print "Usage: $0 [-CUr?] < group_dump\n";
167     print "  -C     don't create group if it doesn't exist\n";
168     print "  -U     don't update group if it exists\n";
169     print "  -r     recursively process groups\n";
170     exit(1);
171 }
172
173 my $group_name;
174 my $group_desc;
175 my $has_members = 0;
176 my @members = ();
177
178 while (<>)
179 {
180   my $line = $_;
181   chomp($line);
182   next if ( $line =~ m/^\s*$/ );
183
184   if ($group_name eq "") {
185       if ( $line =~ m/^Group name\s+(.+).$/ ) {
186           $group_name = $1;
187           next;
188       }
189   }
190   if ($group_desc eq "") {
191       if ( $line =~ m/^Comment\s+(.*)$/ ) {
192           $group_desc = $1;
193           next;
194       }
195   }
196   next if ( $line =~ m/^-+.$/ );
197   if (!$has_members) {
198       if ( $line =~ m/^Members/ ) {
199           $has_members = 1;
200           next;
201       }
202   } else {
203       if ( $line =~ m/^The command completed successfully/ ) {
204           last;
205       } else {
206           push(@members, split(/\s+/, $line));
207           next;
208       }
209   }
210
211   #print;
212 }
213
214 if ( $#members > -1) {
215     process_group($group_name, @members, $Options{'C'}, $Options{'U'}, $Options{'r'});
216 }
217
218 #print "gn=$group_name\n";
219 #print "gd=$group_desc\n";
220 #my $m;
221 #foreach $m (@members)
222 #{
223 #    print "$m ";
224 #}
225 #print "\n";