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 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
|
#!/usr/bin/perl -w
# Created by P.Wieleba@iem.pw.edu.pl in 2004
use strict;
use Getopt::Std;
use FindBin;
use FindBin qw($RealBin);
use lib "$RealBin/";
use smbldap_tools;
# function declaration
sub exist_in_tab;
sub add_to_tab;
# smbldap-migrate-unix-groups (-? or -h for help)
#
#
my %Options;
my $ok = getopts('G:nv?ha', \%Options);
if ( (!$ok) || ($Options{'?'}) || ($Options{'h'}) || (!keys(%Options)) ) {
print "Usage: $0 [-Gnv?ha]\n";
print " -?|-h show this help message\n";
print " -G file import group file\n";
print " -v displays modified entries to STDOUT\n";
print " -n do everything execpt updating LDAP\n";
print " -a adds sambaGroupMapping objectClass\n";
exit (1);
}
my $INFILE = undef;
if ( $Options{'G'} ) {
open($INFILE,$Options{'G'}) or
die "I cannot open file: " . $Options{'G'} . "\n";
}
my $ldap_master=connect_ldap_master();
while ( my $line=<$INFILE> ) {
chop($line);
next if ( $line =~ /^\s*$/ ); # whitespace
next if ( $line =~ /^#/ );
next if ( $line =~ /^\+/ );
my $entry = undef;
if ($Options{'G'}) {
my($group, $pwd, $gid, $users) = split(/:/,$line);
# if user is not in LDAP new entry will be created
$entry = get_group_entry($ldap_master,$group);
$entry = migrate_group($entry,$group, $pwd, $gid, $users);
}
if ($entry) {
# if used "-a" and sambaGroupMapping doesn't exist.
if ( $Options{'a'} and !exist_in_tab([$entry->get_value('objectClass')],'sambaGroupMapping') ) {
my @objectClass = $entry->get_value( 'objectClass' );
$entry->replace( 'objectclass' => [add_to_tab(\@objectClass,'sambaGroupMapping')] );
# the below part comes from smbldap-groupadd and
# maybe it should be replaced by a new subroutine.
my $groupGidNumber = $entry->get_value('gidNumber');
# as rid we use 2 * gid + 1001
my $group_rid = 2*$groupGidNumber+1001;
# let's test if this SID already exist
my $group_sid = "$config{SID}-$group_rid";
my $test_exist_sid=does_sid_exist($group_sid,$config{groupsdn});
if ($test_exist_sid->count == 1) {
warn "Group SID already owned by\n";
# there should not exist more than one entry, but ...
foreach my $entry ($test_exist_sid->all_entries) {
my $dn= $entry->dn;
chomp($dn);
warn "$dn\n";
}
} else {
$entry->replace( 'sambaSID' => $group_sid );
$entry->replace( 'sambaGroupType' => group_type_by_name('domain') );
}
}
if ($Options{'v'}) {
$entry->dump();
}
if (!$Options{'n'}) {
my $mesg = $entry->update($ldap_master);
if ($mesg->is_error()) {
print "Error: " . $mesg->error() . "\n";
}
}
}
}
$INFILE and close($INFILE);
# take down the session
$ldap_master and $ldap_master->unbind;
# returns updated $entry
sub migrate_group
{
my($entry,$group, $pwd, $gid, $users) = @_;
# posixGroup MUST ( cn $ gidNumber )
my @objectClass = $entry->get_value( 'objectClass' );
$entry->replace( 'objectClass' => [add_to_tab(\@objectClass,'posixGroup')] );
$entry->replace( 'cn' => $group );
($pwd) and $entry->replace( 'userPassword' => "{crypt}" . $pwd );
($gid ne "") and $entry->replace( 'gidNumber' => $gid );
my @users = split(',',$users);
# choose only unique users
my %unique_users;
foreach my $user (@users) {
$unique_users{$user} = 1;
}
@users = keys(%unique_users);
($users) and $entry->replace( 'memberUid' => [ @users ] );
return $entry;
}
# creates a _new_entry_ if group doesn't exist in ldap
# else return's ldap user entry
sub get_group_entry
{
my($ldap_master,$group) = @_;
# do not use try read_user_entry()
my $mesg = $ldap_master->search( base => $config{groupsdn},
scope => 'one',
filter => "(cn=$group)"
);
my $entry;
if ( $mesg->count() != 1 ) {
$entry = Net::LDAP::Entry->new();
$entry->dn("cn=$group,$config{groupsdn}");
} else {
$entry = $mesg->entry(0); # ????
}
return $entry;
}
# Check if a $text element exists in @table
# eg. exist_in_tab(\@table,$text);
sub exist_in_tab
{
my($ref_tab,$text) = @_;
my @tab = @$ref_tab;
foreach my $elem (@tab) {
if ( lc($elem) eq lc($text) ) {
return 1;
}
}
return 0;
}
# Add $text to tab if it doesn't exist there
sub add_to_tab
{
my($ref_tab,$text) = @_;
my @tab = @$ref_tab;
if ( !exist_in_tab(\@tab,$text) ) {
push(@tab,$text);
}
return @tab;
}
########################################
=head1 NAME
smbldap-migrate-unix-groups - Migrate unix groups to LDAP
=head1 SYNOPSIS
smbldap-migrate-unix-groups [-G file] [-n] [-v] [-h] [-?] [-a]
=head1 DESCRIPTION
This command processes one file as defined by option and
creates new or changes existing ldap group entry.
New attributes are added, and existing are changed.
None of the existing attributes is deleted.
-G group_file
Processes group_file and uptades LDAP. Creates new ldap group
entry or just adds posixGroup objectclass and corresponding
attributes to the ldap group entry or just uptades their values.
-h show the help message
-? the same as -h
-v displayes modified entries to STDOUT
-n do everything execpt updating LDAP. It is useful when used
with -v switch.
-a adds sambaGroupMapping objectClass, generates sambaSID
and adds sambaGroupType attribute
=cut
#'
# The End
|