#! /opt/imh-python/bin/python3
Written by Quintin Holmes
# add and remove domains to/from /etc/skiprbldomains
def modify_domains(file_path, domains, action="add"):
# read the existing domains
with open(file_path, "r+") as file:
existing_domains = set(line.strip() for line in file)
file.seek(0) # reset file pointer to the beginning for re-writing if needed
if domain not in existing_domains:
existing_domains.add(domain)
# rewrite the file with updated domains
file.writelines(f"{domain}\n" for domain in existing_domains)
#file.truncate() # Adjust file size to match new content
initial_count = len(existing_domains)
existing_domains.difference_update(domains)
if len(existing_domains) < initial_count: # Check if any domains were removed
print(f"Domains removed from {file_path}.")
file.writelines(f"{domain}\n" for domain in existing_domains)
print(f"No specified domains were found in {file_path}.")
print(f"Permission denied: Unable to modify {file_path}")
subprocess.run(["/scripts/buildeximconf"], check=True)
subprocess.run(["systemctl", "restart", "exim"], check=True)
except subprocess.CalledProcessError as e:
print(f"Error restarting Exim: {e}")
# looks for a cp user in userdomains, then adds all domains for the user to skiprbldomains
def find_user_domains(user, userdomains_path="/etc/userdomains"):
with open(userdomains_path, "r") as file:
domain, owner = line.strip().split(':')
if owner.strip() == user:
domains.append(domain.strip())
print(f"Permission denied: Unable to read {userdomains_path}")
def child_accounts(reseller, file_path):
with open('/etc/trueuserowners', 'r') as file:
if re.search(f":{reseller}", line):
accounts.append(line.split(':')[0])
with open('/etc/trueuserdomains', 'r') as file:
if re.search(f":{account}$", line):
reseller_domains.append(line.split(':')[0].strip())
modify_domains(file_path, reseller_domains, action="add")
# Main function of the script that has argument setup
parser = argparse.ArgumentParser(description="Modify /etc/skiprbldomains file.")
parser.add_argument("-a", "--add", nargs='+', help="Add domains to the file.", metavar="DOMAIN", default=[], type=str)
parser.add_argument("-r", "--remove", nargs='+', help="Remove domains from the file.", metavar="DOMAIN", default=[], type=str)
parser.add_argument("-u", "--user", help="Add all domains owned by this user.", metavar="USERNAME", type=str)
parser.add_argument("-c", "--child", action="store_true", help="Add reseller child accounts.")
args = parser.parse_args()
file_path = "/etc/skiprbldomains"
if not os.path.isfile(file_path):
open(file_path, "a").close()
print(f"Permission denied: Unable to create or modify {file_path}")
domains_added = modify_domains(file_path, args.add, action="add")
domains_added = modify_domains(file_path, args.remove, action="remove") or domains_added
# -- user is used, add their domains
user_domains = find_user_domains(args.user)
domains_added = modify_domains(file_path, user_domains, action="add") or domains_added
child_accounts(args.user, file_path)
print("No changes made.")
if __name__ == "__main__":