Mini Shell

Direktori : /proc/self/root/opt/spamassassin/bin/
Upload File :
Current File : //proc/self/root/opt/spamassassin/bin/sa_whitelist

#!/opt/imh-python/bin/python2.7
# SA outgoing mail filtering whitelist management
# Last revision: Vanessa V 4/18/13

import os, sys, subprocess, shutil, pwd
import re
from optparse import OptionParser

conf_file = '/etc/mail/spamassassin/ob_whitelist.cf'

def restart_spamd():
    p = subprocess.Popen('/usr/local/cpanel/scripts/restartsrv_spamd', shell=True)
    p.communicate()
    return p.returncode == 0

def domain_exists(domain):
    with open('/etc/userdomains', 'r') as f:
        for line in f:
            if re.match('^%s:' % domain, line):
                return True
    return False

def domain_whitelisted(domain):
    with open(conf_file, 'r') as f:
        for line in f:
            if re.search('@%s$' % domain, line):
                return True
    return False

def create_conf():
    if not os.path.isfile(conf_file):
        file(conf_file, 'w+').close()

def get_domain_owner(domain):
    try:
        user = subprocess.check_output(['/usr/local/cpanel/scripts/whoowns', '%s' % domain]).strip()

        if pwd.getpwnam(user):
            homedir = subprocess.check_output(['/usr/local/cpanel/scripts/gethomedir', '%s' % user]).strip()
            return (user, homedir)
        else:
            return False
    except:
        return False

def add(domain):
    if domain_exists(domain):
        if domain_whitelisted(domain):
            print 'Error: Domain %s appears to already be whitelisted' % domain
            return False
        else:
            with open(conf_file, 'a') as f:
                try:
                    f.write('whitelist_from *@%s\n' % domain)
                except:
                    print 'Error: Could not append domain to %s' % conf_file
                    return False

            print 'Domain %s added to whitelist' % domain

            # Add domain to tracking file
            data = get_domain_owner(domain)

            if data:
                user,homedir = data
                data_file = '%s/.imh/.sa_whitelist' % homedir
                if not os.path.exists(os.path.dirname(data_file)):
                    os.mkdir(os.path.dirname(data_file))

                if not os.path.isfile(data_file):
                    try:
                        file(data_file, 'w+').close()
                    except:
                        print 'Error: Domain %s has been whitelisted, but could not save to %s' % (domain, data_file)
                        return True

                with open(data_file, 'r') as f:
                    domain_list = f.read().splitlines()
                if domain not in domain_list:
                    with open(data_file, 'a') as f:
                        f.write('%s\n' % domain)
                
            else:
                print 'Error: Domain %s has been whitelisted, but could not save to user data file' % domain
            
            return True
            

    else:
        print 'Error: Domain %s not found on server' % domain
        return False


def delete(domain):
    if not domain_whitelisted(domain):
        print 'Error: Domain %s does not appear to be whitelisted' % domain
        return False
    with open(conf_file, 'r') as f:
        lines = f.readlines()

    try:
        shutil.copy(conf_file, '%s.bak' % conf_file)
    except:
        print 'Error: Could not create backup of %s' % conf_file
        return False

    try:
        with open('%s.bak' % conf_file, 'w') as f:
            for line in lines:
                if not re.search('@%s$' % domain, line):
                    f.write(line)
        os.rename('%s.bak' % conf_file, conf_file)
    except:
        print 'Error: Could not write to %s' % conf_file
        os.unlink('%s.bak' % conf_file)
        return False

    print 'Domain %s removed from whitelist' % domain

    if domain_exists(domain):

        # Remove domain from tracking file
        data = get_domain_owner(domain)

        if data:
            user,homedir = data
            data_file = '%s/.imh/.sa_whitelist' % homedir

            if os.path.isfile(data_file):
                with open(data_file, 'r') as f:
                    lines = f.readlines()

                if len(lines) > 0:
                    with open(data_file, 'w') as f:
                       if not re.search('@%s$' % domain, line):
                           f.write(line)

        else:
            print 'Error: Domain %s has been whitelisted, but could not remove from user data file' % domain

        return True


def opts():
    parser = OptionParser(usage='usage: %prog [options] domain',
                          version='%prog 1.0')
    parser.add_option('-a', '--add',
                      help='Adds a domain to the SA whitelist',
                      action='store_true')
    parser.add_option('-d', '--delete',
                      help='Deletes a domain from the SA whitelist',
                      action='store_true')
    (options, args) = parser.parse_args()

    if 'add' not in options.__dict__:
        parser.print_help()

    if len(args) == 0:
        parser.print_help()

    return options,args

if __name__ == '__main__':
    options,args = opts()
    create_conf()
    something_changed = False
    print '\n'
    if options.add and options.delete:
        print 'Error: Cannot use both -a and -d'
    elif options.add:
        for arg in args:
            something_changed = add(arg) or something_changed
    elif options.delete:
        for arg in args:
            something_changed = delete(arg) or something_changed
    else:
        print 'Missing option -a or -d'

    if something_changed:
        if restart_spamd():
            print 'Spamd restarted\n'
        else:
            print 'Error: Could not restart spamd\n'
            sys.exit(1)	

    print '\n'


Zerion Mini Shell 1.0