This script takes an LDIF as produced by Thunderbird 1.5RC2 and slurps it into an OpenLDAP directory. If you run this on Unix/Linux with an LDIF produced on Windows™, you must ensure that the LDIF input has no carriage-returns (\r) at the end of the lines

perl -pe 's|\s+$|\n|' < mozilla-ab.ldif > mozilla-ab-unix.ldif
#!/usr/bin/perl
 
use strict;
use Net::LDAP;
use Net::LDAP::LDIF;
 
my $HOST = 'localhost';
my $BASE = 'ou=Contacts, dc=example, dc=com';
my $BINDDN   = 'cn=manager' . $BASE;
my $BINDPW   = 'secret';
 
my $filename = $ARGV[0];
my ($ldif, $entry, $rc, $val);
 
die "Usage: $0 mozilla-ab.ldif\n" unless ($filename && -f $filename && -r $filename);
 
my $ld = Net::LDAP->new($HOST) or die "$@";
my $msg = $ld->bind($BINDDN, password => "$BINDPW");
$msg->code && die("Can't bind to directory: " . $msg->error);
 
 
$ldif = Net::LDAP::LDIF->new( $filename, "r", onerror => 'warn');
while( not $ldif->eof() ) {
    $entry = $ldif->read_entry();
    if ( $ldif->error() ) {
        print "Error msg: ", $ldif->error ( ), "\n";
        print "Error lines:\n", $ldif->error_lines ( ), "\n";
    } else {
        my $dn = $entry->dn();
 
        print "Adding: $dn...\n";
 
        # Sanitize DN and add baseDN
 
        $dn =~ s/,[ ]?mail=/+mail=/;
        $dn .= ",$BASE";
 
        # Delete DN on server to allow subsequent ADD
        $rc = $ld->delete($dn);
 
        # Rename this entry's DN
 
        $entry->dn($dn);
 
        # Uppercase the boolean value of mozillaUseHtmlMail for OpenLDAP
        if (($val = $entry->get_value('mozillaUseHtmlMail'))) {
            $val = uc $val;
            $entry->replace('mozillaUseHtmlMail', $val);
        }
 
        # Rename `department' to `departmentNumber'
        if (($val = $entry->get_value('department'))) {
            $entry->add('departmentNumber' => "$val");
            $entry->delete('department');
        }
 
        # Rename `company' to `o' (organization)
        if (($val = $entry->get_value('company'))) {
            $entry->add('o' => "$val");
            $entry->delete('company');
        }
 
        $entry->delete('homeStreet');
        $entry->delete('modifytimestamp');
 
        $rc = $entry->update($ld);
        $rc->code && die "$0: failed to create entry $dn: ", $rc->error ;
    }
}
$ldif->done();
$ld->unbind();

Comments

blog comments powered by Disqus