Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/AnonR/smanonr..../opt/sharedra...
File: exclude_rbl.py
#! /opt/imh-python/bin/python3
[0] Fix | Delete
[1] Fix | Delete
"""
[2] Fix | Delete
Written by Quintin Holmes
[3] Fix | Delete
"""
[4] Fix | Delete
[5] Fix | Delete
import argparse
[6] Fix | Delete
import subprocess
[7] Fix | Delete
import os
[8] Fix | Delete
import re
[9] Fix | Delete
# add and remove domains to/from /etc/skiprbldomains
[10] Fix | Delete
def modify_domains(file_path, domains, action="add"):
[11] Fix | Delete
modified = False
[12] Fix | Delete
try:
[13] Fix | Delete
# read the existing domains
[14] Fix | Delete
with open(file_path, "r+") as file:
[15] Fix | Delete
existing_domains = set(line.strip() for line in file)
[16] Fix | Delete
file.seek(0) # reset file pointer to the beginning for re-writing if needed
[17] Fix | Delete
if action == "add":
[18] Fix | Delete
for domain in domains:
[19] Fix | Delete
if domain not in existing_domains:
[20] Fix | Delete
existing_domains.add(domain)
[21] Fix | Delete
modified = True
[22] Fix | Delete
# rewrite the file with updated domains
[23] Fix | Delete
if modified:
[24] Fix | Delete
file.writelines(f"{domain}\n" for domain in existing_domains)
[25] Fix | Delete
#file.truncate() # Adjust file size to match new content
[26] Fix | Delete
elif action == "remove":
[27] Fix | Delete
initial_count = len(existing_domains)
[28] Fix | Delete
existing_domains.difference_update(domains)
[29] Fix | Delete
if len(existing_domains) < initial_count: # Check if any domains were removed
[30] Fix | Delete
modified = True
[31] Fix | Delete
print(f"Domains removed from {file_path}.")
[32] Fix | Delete
file.seek(0)
[33] Fix | Delete
file.writelines(f"{domain}\n" for domain in existing_domains)
[34] Fix | Delete
file.truncate()
[35] Fix | Delete
else:
[36] Fix | Delete
print(f"No specified domains were found in {file_path}.")
[37] Fix | Delete
except PermissionError:
[38] Fix | Delete
print(f"Permission denied: Unable to modify {file_path}")
[39] Fix | Delete
return modified
[40] Fix | Delete
#restart Exim
[41] Fix | Delete
def restart_exim():
[42] Fix | Delete
try:
[43] Fix | Delete
subprocess.run(["/scripts/buildeximconf"], check=True)
[44] Fix | Delete
subprocess.run(["systemctl", "restart", "exim"], check=True)
[45] Fix | Delete
print("Exim restarted.")
[46] Fix | Delete
except subprocess.CalledProcessError as e:
[47] Fix | Delete
print(f"Error restarting Exim: {e}")
[48] Fix | Delete
# looks for a cp user in userdomains, then adds all domains for the user to skiprbldomains
[49] Fix | Delete
def find_user_domains(user, userdomains_path="/etc/userdomains"):
[50] Fix | Delete
domains = []
[51] Fix | Delete
try:
[52] Fix | Delete
with open(userdomains_path, "r") as file:
[53] Fix | Delete
for line in file:
[54] Fix | Delete
domain, owner = line.strip().split(':')
[55] Fix | Delete
if owner.strip() == user:
[56] Fix | Delete
domains.append(domain.strip())
[57] Fix | Delete
except PermissionError:
[58] Fix | Delete
print(f"Permission denied: Unable to read {userdomains_path}")
[59] Fix | Delete
return domains
[60] Fix | Delete
def child_accounts(reseller, file_path):
[61] Fix | Delete
accounts = []
[62] Fix | Delete
with open('/etc/trueuserowners', 'r') as file:
[63] Fix | Delete
for line in file:
[64] Fix | Delete
if re.search(f":{reseller}", line):
[65] Fix | Delete
accounts.append(line.split(':')[0])
[66] Fix | Delete
reseller_domains = []
[67] Fix | Delete
for account in accounts:
[68] Fix | Delete
with open('/etc/trueuserdomains', 'r') as file:
[69] Fix | Delete
for line in file:
[70] Fix | Delete
if re.search(f":{account}$", line):
[71] Fix | Delete
reseller_domains.append(line.split(':')[0].strip())
[72] Fix | Delete
if reseller_domains:
[73] Fix | Delete
modify_domains(file_path, reseller_domains, action="add")
[74] Fix | Delete
# Main function of the script that has argument setup
[75] Fix | Delete
def main():
[76] Fix | Delete
parser = argparse.ArgumentParser(description="Modify /etc/skiprbldomains file.")
[77] Fix | Delete
parser.add_argument("-a", "--add", nargs='+', help="Add domains to the file.", metavar="DOMAIN", default=[], type=str)
[78] Fix | Delete
parser.add_argument("-r", "--remove", nargs='+', help="Remove domains from the file.", metavar="DOMAIN", default=[], type=str)
[79] Fix | Delete
parser.add_argument("-u", "--user", help="Add all domains owned by this user.", metavar="USERNAME", type=str)
[80] Fix | Delete
parser.add_argument("-c", "--child", action="store_true", help="Add reseller child accounts.")
[81] Fix | Delete
args = parser.parse_args()
[82] Fix | Delete
file_path = "/etc/skiprbldomains"
[83] Fix | Delete
domains_added = False
[84] Fix | Delete
if not os.path.isfile(file_path):
[85] Fix | Delete
try:
[86] Fix | Delete
open(file_path, "a").close()
[87] Fix | Delete
except PermissionError:
[88] Fix | Delete
print(f"Permission denied: Unable to create or modify {file_path}")
[89] Fix | Delete
return
[90] Fix | Delete
# add and remove actions
[91] Fix | Delete
if args.add:
[92] Fix | Delete
domains_added = modify_domains(file_path, args.add, action="add")
[93] Fix | Delete
if args.remove:
[94] Fix | Delete
domains_added = modify_domains(file_path, args.remove, action="remove") or domains_added
[95] Fix | Delete
# -- user is used, add their domains
[96] Fix | Delete
if args.user:
[97] Fix | Delete
user_domains = find_user_domains(args.user)
[98] Fix | Delete
if user_domains:
[99] Fix | Delete
domains_added = modify_domains(file_path, user_domains, action="add") or domains_added
[100] Fix | Delete
if args.child:
[101] Fix | Delete
child_accounts(args.user, file_path)
[102] Fix | Delete
domains_added = True
[103] Fix | Delete
if domains_added:
[104] Fix | Delete
restart_exim()
[105] Fix | Delete
else:
[106] Fix | Delete
print("No changes made.")
[107] Fix | Delete
if __name__ == "__main__":
[108] Fix | Delete
main()
[109] Fix | Delete
[110] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function