Zarafa DB to LDAP user plugin conversion

From Zarafa wiki

Revision as of 17:47, 25 February 2009 by Admin (Talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

The following script allows you to convert a Zarafa DB plugin to an Active Directory environment.

Before you run this script make sure you have a successful backup.


#!/usr/bin/perl -w

use strict;
use MIME::Base64;
use DBI;

if(@ARGV != 7) {
        print "Usage: $0 <mysqluser> <mysqlpass> <database> <ldaphost> <ldapbinduser> <ldapbindpass> <ldap_base_dn>\n";
        exit(1);
}

my ($dbuser, $dbpass, $db, $ldaphost, $ldapuser, $ldappass, $ldapbase) = @ARGV;

my $dbh = DBI->connect("DBI:mysql:database=$db;host=localhost",
                                $dbuser, $dbpass,
                                {'RaiseError' => 1});

open INPUT, "ldapsearch -x -H ldap://$ldaphost -b \"$ldapbase\" -D \"$ldapuser\" -w \"$ldappass\"|";

my %names;
my $objectsid;

while(<INPUT>) {
        if(/sAMAccountName: (\S+)/) {
#               print "name: $1\n";
                $names{$1}->{"objectsid"} = $objectsid;
        }
        if(/objectSid:: (\S+)/) {
#               print "objectSid: " . $dbh->quote(decode_base64($1)) . "\n";
                $objectsid = decode_base64($1);
        }
}

my $name;

foreach $name (keys %names) {
        my $q = $dbh->prepare("SELECT objectid FROM objectproperty WHERE propname='loginname' AND value='$name'");
        $q->execute;

        my $rows = $q->rows;

        if($rows == 1) {
                # Found a user with username in the database, so now we know the user id
                $names{$name}->{"userid"} = ($q->fetchrow_array())[0];
        }

my ($dbuser, $dbpass, $db, $ldaphost, $ldapuser, $ldappass, $ldapbase) = @ARGV;

my $dbh = DBI->connect("DBI:mysql:database=$db;host=localhost",
                                $dbuser, $dbpass,
                                {'RaiseError' => 1});

open INPUT, "ldapsearch -x -H ldap://$ldaphost -b \"$ldapbase\" -D \"$ldapuser\" -w \"$ldappass\"|";

my %names;
my $objectsid;

while(<INPUT>) {
        if(/sAMAccountName: (\S+)/) {
#               print "name: $1\n";
                $names{$1}->{"objectsid"} = $objectsid;
        }
        if(/objectSid:: (\S+)/) {
#               print "objectSid: " . $dbh->quote(decode_base64($1)) . "\n";
                $objectsid = decode_base64($1);
        }
}

my $name;

foreach $name (keys %names) {
        my $q = $dbh->prepare("SELECT objectid FROM objectproperty WHERE propname='loginname' AND value='$name'");
        $q->execute;

        my $rows = $q->rows;

        if($rows == 1) {
                # Found a user with username in the database, so now we know the user id
                $names{$name}->{"userid"} = ($q->fetchrow_array())[0];
        }
}

foreach $name (keys %names) {
        print "# $name\n";
        print "UPDATE users SET externid=" . $dbh->quote($names{$name}->{"objectsid"}) . " WHERE id=" . $names{$name}->{"userid"} . ";\n";

Personal tools