Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/AnonR/anonr.TX.../proc/self/root/opt/maint/bin
File: addon_serveraliases.py
#!/opt/imh-python/bin/python3
[0] Fix | Delete
"""Check for bugged addon domains which cpanel adds as parked"""
[1] Fix | Delete
from argparse import ArgumentParser
[2] Fix | Delete
from pathlib import Path
[3] Fix | Delete
from logging import Logger
[4] Fix | Delete
import traceback
[5] Fix | Delete
import sys
[6] Fix | Delete
import rads
[7] Fix | Delete
import yaml
[8] Fix | Delete
[9] Fix | Delete
[10] Fix | Delete
def check_serveraliases(log: Logger, noop: bool):
[11] Fix | Delete
"""Iterate primary domain configs and check serveraliases on each"""
[12] Fix | Delete
for cpuser_entry in Path('/var/cpanel/users').iterdir():
[13] Fix | Delete
user = cpuser_entry.name
[14] Fix | Delete
if not rads.is_cpuser(user):
[15] Fix | Delete
continue
[16] Fix | Delete
try:
[17] Fix | Delete
userdata = rads.UserData(user)
[18] Fix | Delete
except Exception as exc:
[19] Fix | Delete
log.warning('%s - %s: %s', user, type(exc).__name__, exc)
[20] Fix | Delete
continue
[21] Fix | Delete
primary = userdata.primary.domain
[22] Fix | Delete
userdata_path = Path('/var/cpanel/userdata', user)
[23] Fix | Delete
addons = {x.domain for x in userdata.addons}
[24] Fix | Delete
if not addons:
[25] Fix | Delete
continue
[26] Fix | Delete
addons.update(f"www.{x.domain}" for x in userdata.addons)
[27] Fix | Delete
check_primary_file(log, noop, userdata_path / primary, addons)
[28] Fix | Delete
check_primary_file(log, noop, userdata_path / f"{primary}_SSL", addons)
[29] Fix | Delete
[30] Fix | Delete
[31] Fix | Delete
def check_primary_file(log: Logger, noop: bool, path: Path, addons: list[str]):
[32] Fix | Delete
"""Check a primary domain's config file in userdata to see if an addon
[33] Fix | Delete
domain was erroneously added to the "serveralias" line"""
[34] Fix | Delete
try:
[35] Fix | Delete
with open(path, encoding='utf-8') as file:
[36] Fix | Delete
data = yaml.load(file, Loader=yaml.SafeLoader)
[37] Fix | Delete
except (OSError, ValueError):
[38] Fix | Delete
return
[39] Fix | Delete
old = data['serveralias'].split()
[40] Fix | Delete
if not old:
[41] Fix | Delete
# there should have been at least a www alias for the main
[42] Fix | Delete
# domain, so if we hit here, the file is mangled or the
[43] Fix | Delete
# file format changed. Don't touch it
[44] Fix | Delete
return
[45] Fix | Delete
new = [x for x in old if x not in addons]
[46] Fix | Delete
if old == new:
[47] Fix | Delete
return # No changes
[48] Fix | Delete
if not new:
[49] Fix | Delete
return # Mangled file
[50] Fix | Delete
log.info("old %s serveraliases: %s", path, ' '.join(old))
[51] Fix | Delete
log.info("new %s serveraliases: %s", path, ' '.join(new))
[52] Fix | Delete
data['serveralias'] = ' '.join(new)
[53] Fix | Delete
if noop:
[54] Fix | Delete
return
[55] Fix | Delete
save_conf(path, data)
[56] Fix | Delete
[57] Fix | Delete
[58] Fix | Delete
def save_conf(path: Path, data: dict):
[59] Fix | Delete
"""Save config changes by writing to a temp file and then moving it into
[60] Fix | Delete
place. Saving in this method avoids race conditions from cPanel trying
[61] Fix | Delete
to read it before we're done flushing contents to disk"""
[62] Fix | Delete
tmp_path = path.parent / f".{path.name}.tmp"
[63] Fix | Delete
with open(tmp_path, 'w', encoding='utf-8') as file:
[64] Fix | Delete
yaml.dump(data, file, default_flow_style=False)
[65] Fix | Delete
tmp_path.rename(path)
[66] Fix | Delete
[67] Fix | Delete
[68] Fix | Delete
def main():
[69] Fix | Delete
"""Setup logging and run check_serveraliases"""
[70] Fix | Delete
# blank argparse adds -h/--help
[71] Fix | Delete
parser = ArgumentParser(description=__doc__)
[72] Fix | Delete
parser.add_argument('--noop', action='store_true', help='test mode')
[73] Fix | Delete
noop: bool = parser.parse_args().noop
[74] Fix | Delete
if noop:
[75] Fix | Delete
fmt = '%(asctime)s %(levelname)s NOOP: %(message)s'
[76] Fix | Delete
else:
[77] Fix | Delete
fmt = '%(asctime)s %(levelname)s %(message)s'
[78] Fix | Delete
log = rads.setup_logging(
[79] Fix | Delete
path='/var/log/maint/addon_serveraliases.log',
[80] Fix | Delete
name='addon_serveraliases',
[81] Fix | Delete
fmt=fmt,
[82] Fix | Delete
loglevel='DEBUG',
[83] Fix | Delete
print_out='stdout',
[84] Fix | Delete
chown=(0, 0),
[85] Fix | Delete
chmod=0o700,
[86] Fix | Delete
)
[87] Fix | Delete
try:
[88] Fix | Delete
check_serveraliases(log, noop)
[89] Fix | Delete
except KeyboardInterrupt:
[90] Fix | Delete
log.warning("Canceling run on ^C")
[91] Fix | Delete
sys.exit(1)
[92] Fix | Delete
except Exception:
[93] Fix | Delete
log.critical(traceback.format_exc())
[94] Fix | Delete
sys.exit(1)
[95] Fix | Delete
[96] Fix | Delete
[97] Fix | Delete
if __name__ == '__main__':
[98] Fix | Delete
main()
[99] Fix | Delete
[100] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function