#!/opt/imh-python/bin/python3
"""Check for bugged addon domains which cpanel adds as parked"""
from argparse import ArgumentParser
from logging import Logger
def check_serveraliases(log: Logger, noop: bool):
"""Iterate primary domain configs and check serveraliases on each"""
for cpuser_entry in Path('/var/cpanel/users').iterdir():
if not rads.is_cpuser(user):
userdata = rads.UserData(user)
log.warning('%s - %s: %s', user, type(exc).__name__, exc)
primary = userdata.primary.domain
userdata_path = Path('/var/cpanel/userdata', user)
addons = {x.domain for x in userdata.addons}
addons.update(f"www.{x.domain}" for x in userdata.addons)
check_primary_file(log, noop, userdata_path / primary, addons)
check_primary_file(log, noop, userdata_path / f"{primary}_SSL", addons)
def check_primary_file(log: Logger, noop: bool, path: Path, addons: list[str]):
"""Check a primary domain's config file in userdata to see if an addon
domain was erroneously added to the "serveralias" line"""
with open(path, encoding='utf-8') as file:
data = yaml.load(file, Loader=yaml.SafeLoader)
except (OSError, ValueError):
old = data['serveralias'].split()
# there should have been at least a www alias for the main
# domain, so if we hit here, the file is mangled or the
# file format changed. Don't touch it
new = [x for x in old if x not in addons]
log.info("old %s serveraliases: %s", path, ' '.join(old))
log.info("new %s serveraliases: %s", path, ' '.join(new))
data['serveralias'] = ' '.join(new)
def save_conf(path: Path, data: dict):
"""Save config changes by writing to a temp file and then moving it into
place. Saving in this method avoids race conditions from cPanel trying
to read it before we're done flushing contents to disk"""
tmp_path = path.parent / f".{path.name}.tmp"
with open(tmp_path, 'w', encoding='utf-8') as file:
yaml.dump(data, file, default_flow_style=False)
"""Setup logging and run check_serveraliases"""
# blank argparse adds -h/--help
parser = ArgumentParser(description=__doc__)
parser.add_argument('--noop', action='store_true', help='test mode')
noop: bool = parser.parse_args().noop
fmt = '%(asctime)s %(levelname)s NOOP: %(message)s'
fmt = '%(asctime)s %(levelname)s %(message)s'
log = rads.setup_logging(
path='/var/log/maint/addon_serveraliases.log',
name='addon_serveraliases',
check_serveraliases(log, noop)
except KeyboardInterrupt:
log.warning("Canceling run on ^C")
log.critical(traceback.format_exc())
if __name__ == '__main__':