Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- diff -Naur a/libs/ldaplib/conn_utils.py b/libs/ldaplib/conn_utils.py
- --- a/libs/ldaplib/conn_utils.py 2016-05-10 08:26:17.000000000 +0800
- +++ b/libs/ldaplib/conn_utils.py 2016-07-20 11:58:42.000000000 +0800
- @@ -1,6 +1,7 @@
- # Author: Zhang Huangbin <zhb _at_ iredmail.org>
- 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 a/libs/sql/__init__.py b/libs/sql/__init__.py
- --- a/libs/sql/__init__.py 2016-05-10 08:26:17.000000000 +0800
- +++ b/libs/sql/__init__.py 2016-07-20 11:58:48.000000000 +0800
- @@ -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 a/plugins/amavisd_wblist.py b/plugins/amavisd_wblist.py
- --- a/plugins/amavisd_wblist.py 2016-05-10 08:26:17.000000000 +0800
- +++ b/plugins/amavisd_wblist.py 2016-07-20 11:58:10.000000000 +0800
- @@ -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 = []
- --- a/libs/utils.py 2016-05-10 08:26:17.000000000 +0800
- +++ b/libs/utils.py 2016-07-20 12:05:44.000000000 +0800
- @@ -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
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement