Source

iredapd / src / plugins / block_amavisd_blacklisted_senders.py

Full commit
Zhang Huangbin 8ede84b 
Zhang Huangbin 4332627 
Zhang Huangbin 40fe3c3 
Zhang Huangbin 4332627 
Zhang Huangbin 40fe3c3 
Zhang Huangbin 8ede84b 

Zhang Huangbin 4332627 

Zhang Huangbin 07a9f44 
Zhang Huangbin 40fe3c3 












Zhang Huangbin 07a9f44 

Zhang Huangbin 40fe3c3 
Zhang Huangbin 07a9f44 
Zhang Huangbin 4332627 
Zhang Huangbin 40fe3c3 

Zhang Huangbin 7273b0e 
Zhang Huangbin 40fe3c3 
Zhang Huangbin 4332627 
Zhang Huangbin 7273b0e 
Zhang Huangbin 40fe3c3 
Zhang Huangbin 7273b0e 
Zhang Huangbin 8ede84b 
Zhang Huangbin 40fe3c3 

Zhang Huangbin 7273b0e 

Zhang Huangbin 40fe3c3 
Zhang Huangbin ca03b65 
Zhang Huangbin 7273b0e 

Zhang Huangbin 40fe3c3 
Zhang Huangbin ca03b65 
Zhang Huangbin 7273b0e 

Zhang Huangbin ca03b65 
# Author: Zhang Huangbin <zhb@iredmail.org>

# Priority: whitelist first, then blacklist.

PLUGIN_NAME = 'block_amavisd_blacklisted_senders'

def restriction(smtpSessionData, ldapRecipientLdif, logger, **kargs):
    # Get sender address.
    sender = smtpSessionData.get('sender').lower()

    # Get valid Amavisd sender, sender domain and sub-domain(s).
    # - Sample user: user@sub2.sub1.com.cn
    # - Valid Amavisd senders:
    #   -> user@sub2.sub1.com.cn
    #   -> @sub2.sub1.com.cn
    #   -> @.sub2.sub1.com.cn
    #   -> @.sub1.com.cn
    #   -> @.com.cn
    #   -> @.cn
    splited_sender_domain = str(sender.split('@', 1)[-1]).split('.')

    # Default senders (user@domain.ltd): ['user@domain.ltd', @domain.ltd']
    valid_amavisd_senders = [sender, '@'+sender.split('@', 1)[-1],]
    for counter in range(len(splited_sender_domain)):
        # Append domain and sub-domain.
        valid_amavisd_senders += ['@.' + '.'.join(splited_sender_domain)]
        splited_sender_domain.pop(0)

    valid_amavisd_senders = set(valid_amavisd_senders)

    # Get list of amavisBlacklistedSender.
    blSenders = set([v.lower() for v in ldapRecipientLdif.get('amavisBlacklistSender', [])])

    # Get list of amavisWhitelistSender.
    wlSenders = set([v.lower() for v in ldapRecipientLdif.get('amavisWhitelistSender', [])])

    logger.debug('(%s) Sender: %s' % (PLUGIN_NAME, sender))
    logger.debug('(%s) Whitelisted senders: %s' % (PLUGIN_NAME, str(wlSenders)))
    logger.debug('(%s) Blacklisted senders: %s' % (PLUGIN_NAME, str(blSenders)))

    # Bypass whitelisted senders.
    if len(valid_amavisd_senders & wlSenders) > 0:
        return 'DUNNO Whitelisted'

    # Reject blacklisted senders.
    if len(valid_amavisd_senders & blSenders) > 0:
        return 'REJECT Blacklisted'

    # Neither blacklisted nor whitelisted.
    return 'DUNNO No white-/blacklist records found.'