Samba 3: added Samba 3.0.24 sources
[tomato.git] / release / src / router / samba3 / examples / LDAP / smbldap-tools-0.9.2 / doc / smbldap-migrate-unix-groups
blob32bb043a8d038847be607ba0ff316348d8dc4a40
1 #!/usr/bin/perl -w
3 # Created by P.Wieleba@iem.pw.edu.pl in 2004
5 use strict;
6 use Getopt::Std;
7 use FindBin;
8 use FindBin qw($RealBin);
9 use lib "$RealBin/";
10 use smbldap_tools;
12 # function declaration
13 sub exist_in_tab;
14 sub add_to_tab;
16 # smbldap-migrate-unix-groups (-? or -h for help)
20 my %Options;
22 my $ok = getopts('G:nv?ha', \%Options);
24 if ( (!$ok) || ($Options{'?'}) || ($Options{'h'}) || (!keys(%Options)) ) {
25 print "Usage: $0 [-Gnv?ha]\n";
26 print " -?|-h show this help message\n";
27 print " -G file import group file\n";
28 print " -v displays modified entries to STDOUT\n";
29 print " -n do everything execpt updating LDAP\n";
30 print " -a adds sambaGroupMapping objectClass\n";
31 exit (1);
34 my $INFILE = undef;
36 if ( $Options{'G'} ) {
37 open($INFILE,$Options{'G'}) or
38 die "I cannot open file: " . $Options{'G'} . "\n";
41 my $ldap_master=connect_ldap_master();
43 while ( my $line=<$INFILE> ) {
44 chop($line);
45 next if ( $line =~ /^\s*$/ ); # whitespace
46 next if ( $line =~ /^#/ );
47 next if ( $line =~ /^\+/ );
48 my $entry = undef;
49 if ($Options{'G'}) {
50 my($group, $pwd, $gid, $users) = split(/:/,$line);
51 # if user is not in LDAP new entry will be created
52 $entry = get_group_entry($ldap_master,$group);
53 $entry = migrate_group($entry,$group, $pwd, $gid, $users);
56 if ($entry) {
57 # if used "-a" and sambaGroupMapping doesn't exist.
58 if ( $Options{'a'} and !exist_in_tab([$entry->get_value('objectClass')],'sambaGroupMapping') ) {
59 my @objectClass = $entry->get_value( 'objectClass' );
60 $entry->replace( 'objectclass' => [add_to_tab(\@objectClass,'sambaGroupMapping')] );
62 # the below part comes from smbldap-groupadd and
63 # maybe it should be replaced by a new subroutine.
64 my $groupGidNumber = $entry->get_value('gidNumber');
65 # as rid we use 2 * gid + 1001
66 my $group_rid = 2*$groupGidNumber+1001;
67 # let's test if this SID already exist
68 my $group_sid = "$config{SID}-$group_rid";
69 my $test_exist_sid=does_sid_exist($group_sid,$config{groupsdn});
70 if ($test_exist_sid->count == 1) {
71 warn "Group SID already owned by\n";
72 # there should not exist more than one entry, but ...
73 foreach my $entry ($test_exist_sid->all_entries) {
74 my $dn= $entry->dn;
75 chomp($dn);
76 warn "$dn\n";
78 } else {
79 $entry->replace( 'sambaSID' => $group_sid );
80 $entry->replace( 'sambaGroupType' => group_type_by_name('domain') );
84 if ($Options{'v'}) {
85 $entry->dump();
87 if (!$Options{'n'}) {
88 my $mesg = $entry->update($ldap_master);
89 if ($mesg->is_error()) {
90 print "Error: " . $mesg->error() . "\n";
97 $INFILE and close($INFILE);
98 # take down the session
99 $ldap_master and $ldap_master->unbind;
101 # returns updated $entry
102 sub migrate_group
104 my($entry,$group, $pwd, $gid, $users) = @_;
106 # posixGroup MUST ( cn $ gidNumber )
107 my @objectClass = $entry->get_value( 'objectClass' );
108 $entry->replace( 'objectClass' => [add_to_tab(\@objectClass,'posixGroup')] );
110 $entry->replace( 'cn' => $group );
111 ($pwd) and $entry->replace( 'userPassword' => "{crypt}" . $pwd );
112 ($gid ne "") and $entry->replace( 'gidNumber' => $gid );
114 my @users = split(',',$users);
115 # choose only unique users
116 my %unique_users;
117 foreach my $user (@users) {
118 $unique_users{$user} = 1;
120 @users = keys(%unique_users);
121 ($users) and $entry->replace( 'memberUid' => [ @users ] );
123 return $entry;
126 # creates a _new_entry_ if group doesn't exist in ldap
127 # else return's ldap user entry
128 sub get_group_entry
130 my($ldap_master,$group) = @_;
132 # do not use try read_user_entry()
133 my $mesg = $ldap_master->search( base => $config{groupsdn},
134 scope => 'one',
135 filter => "(cn=$group)"
137 my $entry;
138 if ( $mesg->count() != 1 ) {
139 $entry = Net::LDAP::Entry->new();
140 $entry->dn("cn=$group,$config{groupsdn}");
141 } else {
142 $entry = $mesg->entry(0); # ????
144 return $entry;
147 # Check if a $text element exists in @table
148 # eg. exist_in_tab(\@table,$text);
149 sub exist_in_tab
151 my($ref_tab,$text) = @_;
152 my @tab = @$ref_tab;
154 foreach my $elem (@tab) {
155 if ( lc($elem) eq lc($text) ) {
156 return 1;
159 return 0;
162 # Add $text to tab if it doesn't exist there
163 sub add_to_tab
165 my($ref_tab,$text) = @_;
166 my @tab = @$ref_tab;
168 if ( !exist_in_tab(\@tab,$text) ) {
169 push(@tab,$text);
171 return @tab;
175 ########################################
177 =head1 NAME
179 smbldap-migrate-unix-groups - Migrate unix groups to LDAP
181 =head1 SYNOPSIS
183 smbldap-migrate-unix-groups [-G file] [-n] [-v] [-h] [-?] [-a]
185 =head1 DESCRIPTION
187 This command processes one file as defined by option and
188 creates new or changes existing ldap group entry.
189 New attributes are added, and existing are changed.
190 None of the existing attributes is deleted.
192 -G group_file
193 Processes group_file and uptades LDAP. Creates new ldap group
194 entry or just adds posixGroup objectclass and corresponding
195 attributes to the ldap group entry or just uptades their values.
197 -h show the help message
199 -? the same as -h
201 -v displayes modified entries to STDOUT
203 -n do everything execpt updating LDAP. It is useful when used
204 with -v switch.
206 -a adds sambaGroupMapping objectClass, generates sambaSID
207 and adds sambaGroupType attribute
209 =cut
213 # The End