#!/usr/bin/env python # encoding: utf-8 # Author: Zhang Huangbin # Updated: 2009.07.21 # Purpose: Dump disclaimer text from OpenLDAP directory server or # MySQL database. # # Shipped within iRedAdmin-Pro: # * http://www.iredmail.org/admin_panel.html # --------------------------- WARNING ------------------------------ # This script only works under iRedMail >= 0.5.0 due to ldap schema # changes. # ------------------------------------------------------------------ # -------------------------- Settings ------------------------------ # Directory used to store disclaimer files. # Default directory is /etc/postfix/disclaimer/. # Default disclaimer file name is [domain_name].txt DISCLAIMER_DIR = '/etc/postfix/disclaimer' # NOT need to end with slash ('/'). DISCLAIMER_FILE_EXT = '.txt' # Debug: True, False # WARNING: # * If set to True, cron daemon will send output message to root user. DEBUG = False # Backend used to store disclaimer: ldap, mysql. BACKEND = 'ldap' ############################################################ # LDAP Settings. Required if you use LDAP backend. # # LDAP server address. LDAP_URI = 'ldap://127.0.0.1:389' # LDAP base dn. LDAP_BASEDN = 'o=domains,dc=iredmail,dc=org' # LDAP bind dn & password. # WARNING: # * Only READ permission is requied for dumping disclaimer text. # * Use 'cn=vmailadmin' or 'cn=Manager' is *NOT* recommended. # * 'cn=vmail' IS recommended. # TIP: # You can find password for 'cn=vmail' in /etc/postfix/ldap_*.cf. LDAP_BINDDN = 'cn=vmail,dc=iredmail,dc=org' LDAP_BINDPW = 'WEx7NAeBV9WrAPbGpehyN2pwymvTe5' ############################################################ # MySQL Settings. # MYSQL_ADDR = '127.0.0.1' MYSQL_PORT = '3306' MYSQL_USER = 'vmail' MYSQL_PASSWD = 'secret_passwd' MYSQL_CHARACTER_SET = 'utf-8' # ------------------------------------------------------------------ ############################################################ # YOU DO NOT NEED TO MODIFY BELOW CODE. # import os import sys import re import logging def dumpFromLDAP(): """Dump disclaimer text from LDAP server.""" # Connect to LDAP server. logger.debug('Connecting to LDAP server ...') conn = ldap.initialize(LDAP_URI) conn.set_option(ldap.OPT_PROTOCOL_VERSION, 3) # Use LDAP v3 logger.debug('Binding with dn: %s ...' % LDAP_BINDDN) conn.bind_s(LDAP_BINDDN, LDAP_BINDPW) # Search and get disclaimer. logger.debug('Get disclaimer text ...') results = conn.search_s( LDAP_BASEDN, ldap.SCOPE_ONELEVEL, 'objectclass=maildomain', ['domainName', 'disclaimer'], ) # Dump disclaimer for every domain. logger.debug('Staring dumping...') for entry in results: # Get domain name. domainName = entry[1]['domainName'][0] # Set file name. disclaimer_file = DISCLAIMER_DIR + '/' + domainName + DISCLAIMER_FILE_EXT if entry[1].has_key('disclaimer'): # Dump disclaimer text. try: f = open(disclaimer_file, 'w') #f.write( entry[1]['disclaimer'][0].decode('utf-8') ) f.write('\n---- DISCLAIMER ----\n' + entry[1]['disclaimer'][0]) #.decode('utf-8') ) f.close() logger.debug('Dump disclaimer text to file: %s.' % disclaimer_file) except Exception, e: logger.debug('SKIP (%s): %s.' % (domainName, str(e))) else: # Remove old disclaimer file if no disclaimer setting in LDAP. try: os.remove(disclaimer_file) logger.debug("Remove old disclaimer file: %s." % (disclaimer_file)) except OSError: # File not exist. logger.debug("Skip domain: %s." % domainName) except Exception, e: # Other errors. logger.debug("Error while deleting (%s): %s." % (disclaimer_file, str(e))) # Close connection. conn.unbind() logger.debug('Closed connection.') def dumpFromMySQL(): """Dump disclaimer text from MySQL server.""" pass # Get the file patching logger logger = logging.getLogger('Dumpper') # Logging configuration, to stdout in this case console = logging.StreamHandler() logger.addHandler(console) if DEBUG is True: logger.setLevel(logging.DEBUG) else: logger.setLevel(logging.INFO) if BACKEND == 'ldap': try: import ldap except ImportError: print ''' Error: You don't have python-ldap installed, Please install it first. You can install it like this: - On RHEL/CentOS 5.x: $ sudo yum install python-ldap - On Debian & Ubuntu: $ sudo apt-get install python-ldap ''' sys.exit() # Dump it. try: dumpFromLDAP() except Exception, e: print "Error while dumping disclaimer: %s." % (str(e)) elif BACKEND == 'mysql': try: import MySQLdb except ImportError: print ''' Error: You don't have MySQL support for python, Please install it first. You can install it like this: - On RHEL/CentOS 5.x: # yum install MySQL-python - On Debian & Ubuntu: $ sudo apt-get install python-mysql ''' sys.exit() # Dump it. dumpFromMySQL()