Welcome, guest | Sign In | My Account | Store | Cart

Thunderbird mail client can export its addressbook to LDIF format, but exported file is not suitable for importing into OpenLDAP server. This recipe can fix that.

Python, 51 lines
 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
#!/usr/bin/env python

import sys
from ldif import LDIFParser, LDIFWriter

basedn = 'ou=contacts,dc=linuxkorea,dc=co,dc=kr'

ignore_attribute = [
    'modifytimestamp',
    ]

copy_attribute = [
    ['sn', 'cn'],
    ]

ignore_objectclass = [
    'organizationalPerson',
    'mozillaAbPersonObsolete',
    ]

class FixLDIF(LDIFParser):
    def __init__(self, input, output):
        LDIFParser.__init__(self, input)
        self.writer = LDIFWriter(output)
    def handle(self, dn, entry):
        dn = self.fix_dn(dn)
        self.fix_entry(entry)
        self.fix_objectclass(entry['objectclass'])
        self.writer.unparse(dn, entry)
    def fix_dn(self, dn):
        head = dn.split(',', 1)[0]
        return head + ',' + basedn
    def fix_entry(self, entry):
        for value in ignore_attribute:
            if value in entry:
                del entry[value]
        for target, source in copy_attribute:
            entry[target] = entry[source]
    def fix_objectclass(self, objectclass):
        for value in ignore_objectclass:
            if value in objectclass:
                objectclass.remove(value)

if len(sys.argv) != 3:
    print sys.argv[0], 'input.ldif', 'output.ldif'
    sys.exit()

input = open(sys.argv[1], 'r')
output = open(sys.argv[2], 'w')
parser = FixLDIF(input, output)
parser.parse()

It's nice to keep your addressbook on LDAP server. But since you already have an addressbook full of mail addresses, migration is needed. Thunderbird can export to LDIF format for importing into LDAP server. Open Address Book, choose "Export" from "Tools" menu.

However, things aren't that simple. As Thunderbird doesn't ask about base DN of your server, your server will be "unwilling to perform" import, because it isn't responsible for DN which Thunderbird made up with cn and mail attribute. Thunderbird also uses its private schema with class named "mozillaAbPersonObsolete", which normal install of OpenLDAP doesn't know about. Exported LDIF contains "modifytimestamp" attribute, but it's an internal attribute which import can't modify. Finally, "sn" is mandatory for "person" class in LDAP core schema, but it's common to leave it blank in the addressbook, using the full name ("cn") instead.

This recipe will fix these issues. Edit "basedn" to fit your need. You will need to have python-ldap installed, which you can download from http://python-ldap.sourceforge.net/

This recipe demonstrates how to use LDIFParser/LDIFWriter framework in ldif module, which is part of python-ldap package.

2 comments

Michael 12 years, 3 months ago  # | flag

here're some more attributes and objectclasses to ignore:

ignore_attribute = [
 'modifytimestamp',
 'mozillaSecondEmail',
 'mozillaHomeStreet',
 'mozillaHomeCountryName',
 'mozillaHomeLocalityName',
 'mozillaHomePostalCode',
 'birthday',
 'birthmonth',
 'birthyear'
 ]

copy_attribute = [
 ['sn', 'cn'],
 ]

ignore_objectclass = [
 'organizationalPerson',
 'mozillaAbPersonObsolete',
 'mozillaAbPersonAlpha'
 ]
Juan Valentin 7 years, 5 months ago  # | flag

I am a completely python newbye and this code just creates an empty output.ldif file. What may happen ?