diff -Naur iRedAPD-1.9.1.old/libs/ldaplib/conn_utils.py iRedAPD-1.9.1/libs/ldaplib/conn_utils.py --- iRedAPD-1.9.1.old/libs/ldaplib/conn_utils.py 2016-05-10 00:26:17.000000000 +0000 +++ iRedAPD-1.9.1/libs/ldaplib/conn_utils.py 2016-07-21 04:41:37.000000000 +0000 @@ -1,6 +1,7 @@ # Author: Zhang Huangbin from libs.logger import logger +from libs import utils import ldap import settings from libs import MAILLIST_POLICY_MEMBERSONLY, \ @@ -63,6 +64,10 @@ basedn = domaindn search_scope = 2 + # Use 'moderatorsonly' instead of 'allowedonly' + if policy == 'allowedonly': + policy = 'moderatorsonly' + # Set search filter, attributes based on policy. # Override base dn, scope if necessary. if policy == MAILLIST_POLICY_MEMBERSONLY: @@ -73,7 +78,7 @@ ')' # Get both mail and shadowAddress. - search_attrs = ['mail', 'shadowAddress', ] + search_attrs = ['mail', 'shadowAddress'] elif policy == MAILLIST_POLICY_MEMBERSANDMODERATORSONLY: # Policy: policy== # Filter used to get both members and moderators. @@ -186,3 +191,27 @@ allowed_senders += ['.' + d for d in domains] return [u.lower() for u in allowed_senders] + + +def is_local_domain(conn, domain): + if not utils.is_domain(domain): + return False + + if utils.is_server_hostname(domain): + return True + + try: + filter_domains = '(&(objectClass=mailDomain)' + filter_domains += '(|(domainName=%s)(domainAliasName=%s))' % (domain, domain) + filter_domains += ')' + + qr = conn.search_s(settings.ldap_basedn, + 1, # 1 == ldap.SCOPE_ONELEVEL + filter_domains, + ['dn']) + if qr: + return True + except Exception, e: + logger.error(' Error while querying alias domain: %s' % str(e)) + + return False diff -Naur iRedAPD-1.9.1.old/libs/sql/__init__.py iRedAPD-1.9.1/libs/sql/__init__.py --- iRedAPD-1.9.1.old/libs/sql/__init__.py 2016-05-10 00:26:17.000000000 +0000 +++ iRedAPD-1.9.1/libs/sql/__init__.py 2016-07-21 04:41:37.000000000 +0000 @@ -0,0 +1,27 @@ +from libs.logger import logger +from libs import utils + +def is_local_domain(conn, domain): + if not utils.is_domain(domain): + return False + + if utils.is_server_hostname(domain): + return True + + try: + sql = """SELECT alias_domain + FROM alias_domain + WHERE alias_domain='%s' OR target_domain='%s' + LIMIT 1""" % (domain, domain) + logger.debug('[SQL] query alias domains: \n%s' % sql) + + qr = conn.execute(sql) + sql_record = qr.fetchone() + logger.debug('SQL query result: %s' % str(sql_record)) + + if sql_record: + return True + except Exception, e: + logger.error(' Error while querying alias domain: %s' % str(e)) + + return False diff -Naur iRedAPD-1.9.1.old/libs/utils.py iRedAPD-1.9.1/libs/utils.py --- iRedAPD-1.9.1.old/libs/utils.py 2016-05-10 00:26:17.000000000 +0000 +++ iRedAPD-1.9.1/libs/utils.py 2016-07-21 04:41:15.000000000 +0000 @@ -1,5 +1,6 @@ import re import time +import socket from sqlalchemy import create_engine @@ -355,3 +356,12 @@ return _user + '@' + _domain return mail + + +def is_server_hostname(domain): + name = socket.gethostname() + + if domain == name: + return True + else: + return False diff -Naur iRedAPD-1.9.1.old/plugins/amavisd_wblist.py iRedAPD-1.9.1/plugins/amavisd_wblist.py --- iRedAPD-1.9.1.old/plugins/amavisd_wblist.py 2016-05-10 00:26:17.000000000 +0000 +++ iRedAPD-1.9.1/plugins/amavisd_wblist.py 2016-07-21 04:41:37.000000000 +0000 @@ -50,6 +50,11 @@ REQUIRE_AMAVISD_DB = True +if settings.backend == 'ldap': + from libs.ldaplib.conn_utils import is_local_domain +else: + from libs.sql import is_local_domain + def query_external_addresses(conn, addresses): '''Return list of `mailaddr.id` of external addresses.''' @@ -179,6 +184,7 @@ def restriction(**kwargs): conn = kwargs['conn_amavisd'] + conn_vmail = kwargs['conn_vmail'] if not conn: logger.error('Error, no valid Amavisd database connection.') @@ -186,9 +192,11 @@ # Get sender sender = kwargs['sender'] + sender_domain = kwargs['sender_domain'] if kwargs['sasl_username']: # Use sasl_username as sender for outgoing email sender = kwargs['sasl_username'] + sender_domain = kwargs['sasl_username_domain'] if not sender: logger.debug('Bypass: both sender and sasl_username are empty.') @@ -221,7 +229,7 @@ logger.debug('Possible policy senders: %s' % str(valid_senders)) logger.debug('Possible policy recipients: %s' % str(valid_recipients)) - if kwargs['sasl_username']: + if kwargs['sasl_username'] or is_local_domain(conn=conn_vmail, domain=sender_domain): logger.debug('Apply wblist for outbound message.') id_of_ext_addresses = []