#!/usr/bin/python -tt import cdb import sys import libxml2 import time def usage(ecode=0): print """vmcdb2xml.py passwd.cdb This will dump the contents of passwd.cdb as an XML file. """ sys.exit(ecode) def main(args): if not len(args): usage(1) try: db = cdb.init(args[0]) except Exception, e: print "Could not open %s: does not look like CDB." % args[0] print "Error returned: %s" % e print usage(1) doc = libxml2.newDoc('1.0') root = doc.newChild(None, 'vmailmgr', None) comment = doc.newDocComment('All sizes are in bytes') root.addChild(comment) for key in db.keys(): print 'Processing %s' % key enode = root.newChild(None, 'entry', None) raw = db[key].split('\x00') enode.newChild(None, 'username', key) password = raw[1] if password == '*': password = None enode.newChild(None, 'password', password) mailbox = raw[2] if mailbox == '': mailbox = None enode.newChild(None, 'mailbox', mailbox) aliases = raw[3:-9] anode = enode.newChild(None, 'aliases', None) for alias in aliases: anode.newChild(None, 'alias', alias) personal = raw[-8] if personal == '': personal = None enode.newChild(None, 'personal', personal) hardquota = raw[-7] if hardquota == '-': hardquota = None enode.newChild(None, 'hardquota', hardquota) softquota = raw[-6] if softquota == '-': softquota = None enode.newChild(None, 'softquota', softquota) msgsize = raw[-5] if msgsize == '-': msgsize = None enode.newChild(None, 'msgsizelimit', msgsize) count = raw[-4] if count == '-': count = None enode.newChild(None, 'msgcountlimit', count) try: modified = int(raw[-3]) except Exception, e: print (" Error reading modification time for %s. Using now." % key) modified = int(time.time()) modtime = time.ctime(modified) enode.newChild(None, 'last-modified', modtime).setProp('timestamp', str(modified)) expire = raw[-2] if expire != '-': expire = int(expire) expire = modified + expire enode.newChild(None, 'expires', time.ctime(expire)).setProp( 'timestamp', str(expire)) else: enode.newChild(None, 'expires', None) out = open('%s.xml' % args[0], 'w') out.write(doc.serialize(format=1)) out.close() print "Results written into %s.xml" % args[0] if __name__ == '__main__': main(sys.argv[1:])