Commits

Zhang Huangbin committed dcc6587

Supports PostgreSQL.
Don't query mail user in plugin 'sql_alias_access_policy.py'.

Comments (0)

Files changed (5)

+iRedAPD-1.3.7:
+    * Supports PostgreSQL.
+    * Don't query mail user in plugin 'sql_alias_access_policy.py'.
+
 iRedAPD-1.3.6:
     * Use MySQLdb directly for SQL related operations. Avoid 'too many
       connections' issue.

etc/iredapd.ini.sample

 #             first one in enabled plugin list.
 #   * ldap_maillist_access_policy: mail list deliver restrictions.
 #   * block_amavisd_blacklisted_senders: per-user white/blacklist support.
-plugins = ldap_maillist_access_policy
+plugins = ldap_maillist_access_policy, block_amavisd_blacklisted_senders
 
-[mysql]
-# For MySQL backend only.
+[sql]
+# For MySQL and PostgreSQL backends
 server      = 127.0.0.1
+port        = 3306
 db          = vmail
 user        = vmail
 password    = Psaf68wsuVctYSbj4PJzRqmFsE0rlQ

src/iredapd-rr.py

 import logging
 import daemon
 
-__version__ = '1.3.6'
+__version__ = '1.3.7'
 
 ACTION_ACCEPT = 'DUNNO'
 ACTION_DEFER = 'DEFER_IF_PERMIT Service temporarily unavailable'
                 if cfg.get('general', 'backend', 'ldap') == 'ldap':
                     modeler = LDAPModeler()
                 else:
-                    modeler = MySQLModeler()
+                    modeler = SQLModeler()
 
                 result = modeler.handle_data(self.map)
                 if result != None:
         channel = apdChannel(conn, remoteaddr)
 
 
-class MySQLModeler:
+class SQLModeler:
     def __init__(self):
         import web
 
 
         self.db = web.database(
             dbn='mysql',
-            host=cfg.get('mysql', 'server', 'localhost'),
-            db=cfg.get('mysql', 'db', 'vmail'),
-            user=cfg.get('mysql', 'user', 'vmail'),
-            pw=cfg.get('mysql', 'password'),
+            host=cfg.get('sql', 'server', 'localhost'),
+            db=cfg.get('sql', 'db', 'vmail'),
+            user=cfg.get('sql', 'user', 'vmail'),
+            pw=cfg.get('sql', 'password'),
         )
 
     def handle_data(self, map):
         if 'sender' in map.keys() and 'recipient' in map.keys():
             # Get plugin module name and convert plugin list to python list type.
-            self.plugins = cfg.get('mysql', 'plugins', '')
+            self.plugins = cfg.get('sql', 'plugins', '')
             self.plugins = [v.strip() for v in self.plugins.split(',')]
 
             # Get sender, recipient.
 import logging
 import daemon
 
-__version__ = '1.3.6'
+__version__ = '1.3.7'
 
 ACTION_ACCEPT = 'DUNNO'
 ACTION_DEFER = 'DEFER_IF_PERMIT Service temporarily unavailable'
                 value = line.split('=', 1)[1]
                 self.map[key] = value
         elif len(self.map) != 0:
+            self.backend = cfg.get('general', 'backend', 'ldap')
             try:
-                if cfg.get('general', 'backend', 'ldap') == 'ldap':
+                if self.backend == 'ldap':
                     modeler = LDAPModeler()
                 else:
-                    modeler = MySQLModeler()
+                    modeler = SQLModeler()
 
                 result = modeler.handle_data(self.map)
                 if result != None:
         channel = apdChannel(conn, remoteaddr)
 
 
-class MySQLModeler:
+class SQLModeler:
     def handle_data(self, map):
         if 'sender' in map.keys() and 'recipient' in map.keys():
             if len(map['sender']) < 6:
                 return 'DUNNO'
 
             # Get plugin module name and convert plugin list to python list type.
-            self.plugins = cfg.get('mysql', 'plugins', '')
+            self.plugins = cfg.get('sql', 'plugins', '')
             self.plugins = [v.strip() for v in self.plugins.split(',')]
 
             # Get sender, recipient.
                 for module in self.modules:
                     try:
                         logging.debug('Apply plugin (%s).' % (module.__name__, ))
-                        import MySQLdb
-                        try:
-                            db = MySQLdb.connect(
-                                host=cfg.get('mysql', 'server', 'localhost'),
-                                db=cfg.get('mysql', 'db', 'vmail'),
-                                user=cfg.get('mysql', 'user', 'vmail'),
-                                passwd=cfg.get('mysql', 'password'),
-                            )
-                            cursor= db.cursor()
-                        except Exception, e:
-                            logging.error("Error while creating database connection: %s" % str(e))
+                        if cfg.get('general', 'backend', 'ldap') == 'mysql':
+                            import MySQLdb
+                            try:
+                                db = MySQLdb.connect(
+                                    host=cfg.get('sql', 'server', 'localhost'),
+                                    db=cfg.get('sql', 'db', 'vmail'),
+                                    user=cfg.get('sql', 'user', 'vmail'),
+                                    passwd=cfg.get('sql', 'password'),
+                                )
+                                cursor = db.cursor()
+                            except Exception, e:
+                                logging.error("Error while creating database connection: %s" % str(e))
+                        elif cfg.get('general', 'backend', 'ldap') == 'pgsql':
+                            import psycopg2
+                            try:
+                                db = psycopg2.connect(
+                                    host=cfg.get('pgsql', 'server', 'localhost'),
+                                    port=cfg.get('pgsql', 'port', '5432'),
+                                    database=cfg.get('pgsql', 'db', 'vmail'),
+                                    user=cfg.get('pgsql', 'user', 'vmail'),
+                                    password=cfg.get('pgsql', 'password'),
+                                )
+                                cursor = db.cursor()
+                            except Exception, e:
+                                logging.error("Error while creating database connection: %s" % str(e))
+                        else:
+                            return ACTION_DEFAULT
 
                         pluginAction = module.restriction(
                             dbConn=cursor,
                             cursor.close()
                             logging.debug('Closed SQL connection.')
                         except Exception, e:
-                            logging.debug('%s' % str(e))
+                            logging.debug('Error while closing connection: %s' % str(e))
 
                         logging.debug('Response from plugin (%s): %s' % (module.__name__, pluginAction))
                         if not pluginAction.startswith('DUNNO'):
             return ACTION_DEFER
 
 
-
 class LDAPModeler:
     def __init__(self):
         import ldap

src/plugins/sql_alias_access_policy.py

 
 def restriction(dbConn, senderReceiver, smtpSessionData, logger, **kargs):
 
-    sql = '''SELECT accesspolicy, goto, moderators \
-            FROM alias \
-            WHERE address=%s AND domain=%s AND active=1 \
-            LIMIT 1 \
-    ''' % (sqlquote(senderReceiver.get('recipient')), sqlquote(senderReceiver.get('recipient_domain')),)
+    sql = '''SELECT accesspolicy, goto, moderators
+            FROM alias
+            WHERE
+                address=%s
+                AND address <> goto
+                AND domain=%s
+                AND active=1
+            LIMIT 1
+    ''' % (sqlquote(senderReceiver.get('recipient')),
+           sqlquote(senderReceiver.get('recipient_domain')),
+          )
     logger.debug('SQL: %s' % sql)
 
     dbConn.execute(sql)