bh: utils/admin convert-ldif-21.py,NONE,1.1

cvs at kolab.org cvs at kolab.org
Thu Oct 12 18:18:13 CEST 2006


Author: bh

Update of /kolabrepository/utils/admin
In directory doto:/tmp/cvs-serv26424/admin

Added Files:
	convert-ldif-21.py 
Log Message:
Add a script to convert LDAP data from kolab server 2.0 to 2.1


--- NEW FILE: convert-ldif-21.py ---
#! /usr/bin/python
# Copyright (C) 2006 by Intevation GmbH
# Authors:
# Bernhard Herzog <bh at intevation.de>
#
# This program is free software under the GPL (>=v2)
# Read the file COPYING coming with the software for details.

# Requirements:
#
# Python >= 2.1
# python-ldap >= 2.0

"""Upgrade kolab ldap data from server 2.0 to 2.1

Usage:

   convert-ldif-21.py [--normalize] [INFILE [OUTFILE]]

The program reads the kolab ldap data from INFILE and writes the
converted data to OUTFILE.  The data should be in ldif format as
produced by e.g. slapcat and writes the output in the same format.  The
filenames are optional.  If omitted the program reads from stdin and
writes to stdout.

When the --normalize option is given, the data is only read, parsed and
written out again without any further modification.  The output data
will differ only in formatting with.  This can be useful when doing
comparisons with diff.
"""

__version__ = "$Revision: 1.1 $"
# $Id: convert-ldif-21.py,v 1.1 2006/10/12 16:18:10 bh Exp $


import sys
import getopt

import ldif

def joindn(split_dn):
    """Join a split dn again.  This is the inverse to splitdn"""
    return ",".join(split_dn)

def splitdn(dn):
    return dn.split(",")


class KolabConfig:

    dn_prefix = "k=kolab,"

    def __init__(self, dn, record):
        self.base_dn = dn[len(self.dn_prefix):]
        self.domain = record["postfix-mydomain"][0]


class KolabLDAPUpgrader(ldif.LDIFParser):

    def __init__(self, inputstream, writer, handlers):
        self.__writer = writer
        self.__handlers = handlers
        self.kolab_config = None
        ldif.LDIFParser.__init__(self, inputstream)

    def handle(self, dn, record):
        self.extract_kolab_config(dn, record)
        for handler in self.__handlers:
            newdn = handler(dn, record, self.kolab_config)
            if newdn:
                dn = newdn
        self.__writer.unparse(dn, record)

    def extract_kolab_config(self, dn, record):
        if dn.startswith(KolabConfig.dn_prefix):
            self.kolab_config = KolabConfig(dn, record)

def upgrade_k(dn, record, kolab_config):
    """Upgrades the main kolab record"""
    if dn.startswith("k=kolab,"):
        record["postfix-mydestination"] = record["postfix-mydomain"]

def upgrade_distribution_list(dn, record, kolab_config):
    """Upgrades a distribution list record"""
    if "kolabGroupOfNames" in record["objectClass"] and record.has_key("mail"):
        assert dn.startswith("cn=")
        record["cn"] = record["mail"]
        return joindn(["cn=" + record["cn"][0]] + splitdn(dn)[1:])

def upgrade_shared_folder(dn, record, kolab_config):
    """Upgrades a distribution list record"""
    if "kolabSharedFolder" in record["objectClass"]:
        assert dn.startswith("cn=")
        mail = record["cn"][0] + "@" + kolab_config.domain
        record["cn"] = [mail]
        return joindn(["cn=" + record["cn"][0]] + splitdn(dn)[1:])

def remove_kolabEncryptedPassword(dn, record, kolab_config):
    """Removes kolabEncryptedPassword attributes"""
    if record.has_key("kolabEncryptedPassword"):
        del record["kolabEncryptedPassword"]


found_domains_object = 0
def find_domains_object(dn, record, kolab_config):
    global found_domains_object
    if dn.startswith("dn: cn=domains,cn=internal,"):
        found_domains_object = 1


def write_domains_objects(writer, kolab_config):
    if not kolab_config:
        return
    domain = kolab_config.domain
    base_dn = kolab_config.base_dn
    manager = "cn=manager,cn=internal," + base_dn

    writer.unparse("cn=domain-maintainer,cn=internal," + base_dn,
                   {"objectClass": ["top", "kolabGroupOfNames"],
                    "cn": ["domain-maintainer"],
                    "member": [manager]})
    writer.unparse("cn=domains,cn=internal," + base_dn,
                   {"objectClass": ["top", "kolabNamedObject"],
                    "cn": ["domains"]})
    writer.unparse("cn=" + domain + ",cn=domains,cn=internal," + base_dn,
                   {"objectClass": ["top", "kolabGroupOfNames"],
                    "cn": [domain],
                    "member": [manager]})


def main():
    action = "upgrade"

    opts, rest = getopt.getopt(sys.argv[1:], '', ['normalize', 'help'])
    for optchar, value in opts:
        if optchar == "--normalize":
            action = "normalize"
        elif optchar == "--help":
            print __doc__
            sys.exit(0)
        else:
            print >>sys.stderr, "unknown option %r" % optchar
            sys.exit(1)

    if len(rest) >= 1:
        infile = open(rest[0])
    else:
        infile = sys.stdin
    if len(rest) >= 2:
        outfile = open(rest[1], "w")
    else:
        outfile = sys.stdout

    if action == "upgrade":
        handlers = [upgrade_k,
                    upgrade_distribution_list,
                    upgrade_shared_folder,
                    remove_kolabEncryptedPassword,
                    ]
    elif action == "normalize":
        handlers = []
    else:
        print >>sys.stderr, "unknown action %r" % (action,)
        sys.exit(1)

    writer = ldif.LDIFWriter(outfile)
    upgrader = KolabLDAPUpgrader(infile, writer, handlers)
    upgrader.parse()

    if action == "upgrade":
        if not found_domains_object:
            write_domains_objects(writer, upgrader.kolab_config)

main()





More information about the commits mailing list