Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/AnonR/anonr.TX.../proc/self/root/opt/maint/bin
File: audit_resellers.py
#!/opt/imh-python/bin/python3
[0] Fix | Delete
"""
[1] Fix | Delete
Reseller audit script. Does the following:
[2] Fix | Delete
1) Makes sure that all resellers are owned by 'inmotion' or 'hubhost'
[3] Fix | Delete
2) Resets reseller ACL limits and IP pools
[4] Fix | Delete
3) Checks for orphaned accounts (accounts that have a non-existent owner)
[5] Fix | Delete
"""
[6] Fix | Delete
from collections import defaultdict
[7] Fix | Delete
import configparser
[8] Fix | Delete
import argparse
[9] Fix | Delete
import logging
[10] Fix | Delete
import platform
[11] Fix | Delete
import sys
[12] Fix | Delete
import time
[13] Fix | Delete
import pwd
[14] Fix | Delete
from pathlib import Path
[15] Fix | Delete
from typing import Union
[16] Fix | Delete
import yaml
[17] Fix | Delete
import rads
[18] Fix | Delete
from cpapis import whmapi1, CpAPIError
[19] Fix | Delete
[20] Fix | Delete
[21] Fix | Delete
APIPA = '169.254.100.100' # the old moveuser used this for reseller moves
[22] Fix | Delete
HOST = platform.node().split('.')[0]
[23] Fix | Delete
RESELLER = 'hubhost' if rads.IMH_CLASS == 'hub' else 'inmotion'
[24] Fix | Delete
[25] Fix | Delete
[26] Fix | Delete
def parse_args() -> tuple[int, bool]:
[27] Fix | Delete
"""Parse sys.argv"""
[28] Fix | Delete
parser = argparse.ArgumentParser(
[29] Fix | Delete
description=__doc__,
[30] Fix | Delete
formatter_class=argparse.RawDescriptionHelpFormatter,
[31] Fix | Delete
)
[32] Fix | Delete
parser.add_argument(
[33] Fix | Delete
'--loglevel',
[34] Fix | Delete
'-l',
[35] Fix | Delete
default='INFO',
[36] Fix | Delete
choices=['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'],
[37] Fix | Delete
)
[38] Fix | Delete
parser.add_argument(
[39] Fix | Delete
'--noop',
[40] Fix | Delete
'--dry-run',
[41] Fix | Delete
'-n',
[42] Fix | Delete
dest='noop',
[43] Fix | Delete
action='store_true',
[44] Fix | Delete
help="Make no changes",
[45] Fix | Delete
)
[46] Fix | Delete
args = parser.parse_args()
[47] Fix | Delete
loglevel = getattr(logging, args.loglevel)
[48] Fix | Delete
return loglevel, args.noop
[49] Fix | Delete
[50] Fix | Delete
[51] Fix | Delete
def get_dips() -> dict[str, set[str]]:
[52] Fix | Delete
"""Get a mapping of ipaddr -> resellers from /var/cpanel/dips"""
[53] Fix | Delete
dips = defaultdict(set)
[54] Fix | Delete
try:
[55] Fix | Delete
for res_path in Path('/var/cpanel/dips').iterdir():
[56] Fix | Delete
try:
[57] Fix | Delete
res_ips = set(res_path.read_text('ascii').split())
[58] Fix | Delete
except OSError:
[59] Fix | Delete
continue
[60] Fix | Delete
try:
[61] Fix | Delete
res_ips.remove(APIPA)
[62] Fix | Delete
except KeyError:
[63] Fix | Delete
pass
[64] Fix | Delete
for ipaddr in res_ips:
[65] Fix | Delete
dips[ipaddr].add(res_path.name)
[66] Fix | Delete
except FileNotFoundError:
[67] Fix | Delete
pass
[68] Fix | Delete
return dict(dips)
[69] Fix | Delete
[70] Fix | Delete
[71] Fix | Delete
def check_double_ip_delegations(resellers: set[str], noop: bool):
[72] Fix | Delete
"""Check for IPs which are assigned to more than one reseller"""
[73] Fix | Delete
double_delegations = {
[74] Fix | Delete
ipaddr: resellers
[75] Fix | Delete
for ipaddr, resellers in get_dips().items()
[76] Fix | Delete
if len(resellers) > 1
[77] Fix | Delete
}
[78] Fix | Delete
if double_delegations:
[79] Fix | Delete
auto_fix_double_dips(resellers, double_delegations, noop)
[80] Fix | Delete
if not double_delegations:
[81] Fix | Delete
return
[82] Fix | Delete
logging.warning("Double-delegated IP addresses detected - sending ticket")
[83] Fix | Delete
logging.debug('double delegations: %r', double_delegations)
[84] Fix | Delete
if noop:
[85] Fix | Delete
return
[86] Fix | Delete
body = (
[87] Fix | Delete
"The following IP addresses were detected as being delegated to "
[88] Fix | Delete
"more than one reseller and must be corrected:\n"
[89] Fix | Delete
)
[90] Fix | Delete
for ip_addr, res in double_delegations.items():
[91] Fix | Delete
body = f"{body}\n{ip_addr}: {', '.join(res)}"
[92] Fix | Delete
rads.send_email(
[93] Fix | Delete
to_addr="str@imhadmin.net",
[94] Fix | Delete
subject="Reseller IP delegation conflict",
[95] Fix | Delete
body=body,
[96] Fix | Delete
)
[97] Fix | Delete
[98] Fix | Delete
[99] Fix | Delete
def auto_fix_double_dips(
[100] Fix | Delete
resellers: set[str], double_delegations: dict[str, set[str]], noop: bool
[101] Fix | Delete
):
[102] Fix | Delete
"""Attempt to automatically fix IP double-delegations by checking if the IP
[103] Fix | Delete
is actually in use, and removing it from resellers which aren't using it"""
[104] Fix | Delete
user_ips: dict[str, str] = yaml.load(
[105] Fix | Delete
Path('/etc/userips').read_text('ascii'), rads.DumbYamlLoader
[106] Fix | Delete
)
[107] Fix | Delete
user_resellers: dict[str, str] = yaml.load(
[108] Fix | Delete
Path('/etc/trueuserowners').read_text('ascii'), rads.DumbYamlLoader
[109] Fix | Delete
)
[110] Fix | Delete
user_resellers = {
[111] Fix | Delete
k: k if k in resellers else v for k, v in user_resellers.items()
[112] Fix | Delete
}
[113] Fix | Delete
for ipaddr, res in double_delegations.copy().items():
[114] Fix | Delete
if res.intersection(rads.OUR_RESELLERS):
[115] Fix | Delete
# if there's a conflict involving one of our resellers, don't try
[116] Fix | Delete
# to auto-fix it
[117] Fix | Delete
continue
[118] Fix | Delete
# collect resellers actually using the IP
[119] Fix | Delete
using = list(
[120] Fix | Delete
{user_resellers[k] for k, v in user_ips.items() if v == ipaddr}
[121] Fix | Delete
)
[122] Fix | Delete
if len(using) > 1:
[123] Fix | Delete
continue # legit conflict. don't auto-fix
[124] Fix | Delete
if len(using) == 0:
[125] Fix | Delete
# No one is using this IP. Take it away from all but one reseller.
[126] Fix | Delete
# If this takes away any reseller's last IP, the next run of this
[127] Fix | Delete
# cron should fix it.
[128] Fix | Delete
for remove in list(res)[1:]:
[129] Fix | Delete
remove_dip(ipaddr, remove, double_delegations, noop)
[130] Fix | Delete
elif using[0] in res:
[131] Fix | Delete
# else one reseller is using it but it's delegated to multiple
[132] Fix | Delete
for remove in list(res):
[133] Fix | Delete
if remove != using[0]:
[134] Fix | Delete
remove_dip(ipaddr, remove, double_delegations, noop)
[135] Fix | Delete
[136] Fix | Delete
[137] Fix | Delete
def remove_dip(
[138] Fix | Delete
ipaddr: str,
[139] Fix | Delete
reseller: str,
[140] Fix | Delete
double_delegations: dict[str, set[str]],
[141] Fix | Delete
noop: bool,
[142] Fix | Delete
) -> None:
[143] Fix | Delete
"""Remove an IP from a reseller's pool to fix a double delegation"""
[144] Fix | Delete
# make sure it wasn't their main. the calling function already checked that
[145] Fix | Delete
# the reseller didn't have it assigned
[146] Fix | Delete
main_ip = Path('/var/cpanel/mainips', reseller).read_text('ascii').strip()
[147] Fix | Delete
if main_ip == ipaddr:
[148] Fix | Delete
return
[149] Fix | Delete
logging.warning("removing %s from %s's IP pool", ipaddr, reseller)
[150] Fix | Delete
pool = whmapi1.getresellerips(reseller)['ip']
[151] Fix | Delete
try:
[152] Fix | Delete
pool.remove(ipaddr)
[153] Fix | Delete
except ValueError:
[154] Fix | Delete
# but the previous lookup had it?
[155] Fix | Delete
logging.error("Could not remove %s from %s's IP pool", ipaddr, reseller)
[156] Fix | Delete
return
[157] Fix | Delete
if not noop:
[158] Fix | Delete
try:
[159] Fix | Delete
whmapi1.setresellerips(reseller, pool, delegate=True)
[160] Fix | Delete
except CpAPIError as exc:
[161] Fix | Delete
logging.error(
[162] Fix | Delete
"Could not remove %s from %s's IP pool: %s",
[163] Fix | Delete
ipaddr,
[164] Fix | Delete
reseller,
[165] Fix | Delete
exc,
[166] Fix | Delete
)
[167] Fix | Delete
return
[168] Fix | Delete
double_delegations[ipaddr].remove(reseller)
[169] Fix | Delete
if len(double_delegations[ipaddr]) < 2:
[170] Fix | Delete
double_delegations.pop(ipaddr)
[171] Fix | Delete
[172] Fix | Delete
[173] Fix | Delete
class CpanelConf(configparser.ConfigParser):
[174] Fix | Delete
"""Handles reading /var/cpanel/users and /var/cpanel/packages files"""
[175] Fix | Delete
[176] Fix | Delete
def __init__(self, path: Path):
[177] Fix | Delete
super().__init__(allow_no_value=True, interpolation=None, strict=False)
[178] Fix | Delete
try:
[179] Fix | Delete
self.read_string(f"[config]\n{path.read_text('utf-8')}")
[180] Fix | Delete
except Exception as exc:
[181] Fix | Delete
logging.error('%s - %s: %s', path, type(exc).__name__, exc)
[182] Fix | Delete
raise
[183] Fix | Delete
[184] Fix | Delete
@classmethod
[185] Fix | Delete
def user_conf(cls, user: str):
[186] Fix | Delete
"""Read /var/cpanel/users/{user}"""
[187] Fix | Delete
return cls(Path('/var/cpanel/users', user))
[188] Fix | Delete
[189] Fix | Delete
@classmethod
[190] Fix | Delete
def pkg_conf(cls, pkg: str):
[191] Fix | Delete
"""Read /var/cpanel/packages/{pkg}"""
[192] Fix | Delete
return cls(Path('/var/cpanel/packages', pkg))
[193] Fix | Delete
[194] Fix | Delete
@property
[195] Fix | Delete
def res_limits(self) -> dict[str, str]:
[196] Fix | Delete
"""Read imh custom reseller limits from a cPanel package
[197] Fix | Delete
(use only with pkg_conf)"""
[198] Fix | Delete
imh_keys = (
[199] Fix | Delete
'account_limit',
[200] Fix | Delete
'bandwidth_limit',
[201] Fix | Delete
'diskspace_limit',
[202] Fix | Delete
'enable_account_limit',
[203] Fix | Delete
'enable_resource_limits',
[204] Fix | Delete
'enable_overselling',
[205] Fix | Delete
'enable_overselling_bandwidth',
[206] Fix | Delete
'enable_overselling_diskspace',
[207] Fix | Delete
)
[208] Fix | Delete
return {
[209] Fix | Delete
x: self.get('config', f'imh_{x}', fallback='') for x in imh_keys
[210] Fix | Delete
}
[211] Fix | Delete
[212] Fix | Delete
[213] Fix | Delete
def get_main_ips() -> set[str]:
[214] Fix | Delete
"""Collect IPs from /var/cpanel/mainip and /var/cpanel/mainips/root"""
[215] Fix | Delete
with open('/var/cpanel/mainip', encoding='ascii') as ip_file:
[216] Fix | Delete
ips = set(ip_file.read().split())
[217] Fix | Delete
try:
[218] Fix | Delete
with open('/var/cpanel/mainips/root', encoding='ascii') as ip_file:
[219] Fix | Delete
ips.update(ip_file.read().split())
[220] Fix | Delete
except FileNotFoundError:
[221] Fix | Delete
pass
[222] Fix | Delete
return ips
[223] Fix | Delete
[224] Fix | Delete
[225] Fix | Delete
def get_new_ip() -> str:
[226] Fix | Delete
"""Get an IP which is not already in use"""
[227] Fix | Delete
with open('/etc/ipaddrpool', encoding='ascii') as pool:
[228] Fix | Delete
# not assigned as dedicated, but may be in a reseller pool
[229] Fix | Delete
unassigned = pool.read().split()
[230] Fix | Delete
for ip_addr in unassigned:
[231] Fix | Delete
if not assigned_to_res(ip_addr):
[232] Fix | Delete
return ip_addr
[233] Fix | Delete
return ''
[234] Fix | Delete
[235] Fix | Delete
[236] Fix | Delete
def assigned_to_res(ip_addr):
[237] Fix | Delete
"""Determine if an IP is already delegated to a reseller"""
[238] Fix | Delete
for entry in Path('/var/cpanel/dips').iterdir():
[239] Fix | Delete
with entry.open('r', encoding='ascii') as dips:
[240] Fix | Delete
if ip_addr in dips.read().split():
[241] Fix | Delete
return True
[242] Fix | Delete
return False
[243] Fix | Delete
[244] Fix | Delete
[245] Fix | Delete
def non_res_checks(noop: bool):
[246] Fix | Delete
"""Reseller-owner checks on non-reseller servers"""
[247] Fix | Delete
for path in Path('/var/cpanel/users').iterdir():
[248] Fix | Delete
user = path.name
[249] Fix | Delete
if user == 'root':
[250] Fix | Delete
logging.warning('%s exists. Skipping.', path)
[251] Fix | Delete
continue
[252] Fix | Delete
if user in rads.OUR_RESELLERS:
[253] Fix | Delete
try:
[254] Fix | Delete
whmapi1.set_owner(user, 'root')
[255] Fix | Delete
except CpAPIError as exc:
[256] Fix | Delete
logging.error(
[257] Fix | Delete
"Error changing owner of %s to root: %s", user, exc
[258] Fix | Delete
)
[259] Fix | Delete
continue
[260] Fix | Delete
try:
[261] Fix | Delete
user_conf = CpanelConf.user_conf(user)
[262] Fix | Delete
except Exception:
[263] Fix | Delete
continue
[264] Fix | Delete
try:
[265] Fix | Delete
owner = user_conf.get('config', 'owner')
[266] Fix | Delete
except configparser.NoOptionError:
[267] Fix | Delete
logging.warning(
[268] Fix | Delete
'%s is missing OWNER and may not be a valid CPanel user file',
[269] Fix | Delete
path,
[270] Fix | Delete
)
[271] Fix | Delete
continue
[272] Fix | Delete
if owner != RESELLER:
[273] Fix | Delete
set_owner(user, owner, RESELLER, noop)
[274] Fix | Delete
[275] Fix | Delete
[276] Fix | Delete
def get_resellers() -> set[str]:
[277] Fix | Delete
"""Read resellers from /var/cpanel/resellers"""
[278] Fix | Delete
resellers = set()
[279] Fix | Delete
with open('/var/cpanel/resellers', encoding='utf-8') as res_file:
[280] Fix | Delete
for line in res_file:
[281] Fix | Delete
if res := line.split(':', maxsplit=1)[0]:
[282] Fix | Delete
resellers.add(res)
[283] Fix | Delete
return resellers
[284] Fix | Delete
[285] Fix | Delete
[286] Fix | Delete
def main():
[287] Fix | Delete
"""Cron main"""
[288] Fix | Delete
loglevel, noop = parse_args()
[289] Fix | Delete
if noop:
[290] Fix | Delete
logfmt = '%(asctime)s %(levelname)s NOOP %(message)s'
[291] Fix | Delete
else:
[292] Fix | Delete
logfmt = '%(asctime)s %(levelname)s %(message)s'
[293] Fix | Delete
rads.setup_logging(
[294] Fix | Delete
path=None, loglevel=loglevel, fmt=logfmt, print_out=sys.stdout
[295] Fix | Delete
)
[296] Fix | Delete
if rads.IMH_ROLE != 'shared':
[297] Fix | Delete
logging.critical("rads.IMH_CLASS=%r", rads.IMH_ROLE)
[298] Fix | Delete
sys.exit(1)
[299] Fix | Delete
if 'res' in HOST and rads.IMH_CLASS != 'reseller':
[300] Fix | Delete
logging.critical(
[301] Fix | Delete
"hostname=%r but rads.IMH_CLASS=%r", HOST, rads.IMH_CLASS
[302] Fix | Delete
)
[303] Fix | Delete
sys.exit(1)
[304] Fix | Delete
resellers = get_resellers()
[305] Fix | Delete
all_res = resellers | set(rads.OUR_RESELLERS) | {"system", rads.SECURE_USER}
[306] Fix | Delete
if rads.IMH_CLASS == 'reseller':
[307] Fix | Delete
main_ips = get_main_ips()
[308] Fix | Delete
for reseller in resellers:
[309] Fix | Delete
res_checks(reseller, main_ips, noop)
[310] Fix | Delete
orphan_storage = defaultdict(list)
[311] Fix | Delete
term_fails = defaultdict(list)
[312] Fix | Delete
for entry in Path("/var/cpanel/users").iterdir():
[313] Fix | Delete
user = entry.name
[314] Fix | Delete
if user in all_res:
[315] Fix | Delete
continue
[316] Fix | Delete
try:
[317] Fix | Delete
pwd.getpwnam(user)
[318] Fix | Delete
except KeyError:
[319] Fix | Delete
logging.warning("Removing erroneous file at %s", entry)
[320] Fix | Delete
if not noop:
[321] Fix | Delete
entry.unlink()
[322] Fix | Delete
continue
[323] Fix | Delete
check_orphans(user, main_ips, orphan_storage, term_fails, noop)
[324] Fix | Delete
for reseller, orphans in orphan_storage.items():
[325] Fix | Delete
orphans_notify(reseller, orphans, noop)
[326] Fix | Delete
for reseller, orphans in term_fails.items():
[327] Fix | Delete
term_fail_notice(reseller, orphans, noop)
[328] Fix | Delete
else:
[329] Fix | Delete
non_res_checks(noop)
[330] Fix | Delete
cleanup_delegations(all_res, noop)
[331] Fix | Delete
check_double_ip_delegations(resellers, noop)
[332] Fix | Delete
[333] Fix | Delete
[334] Fix | Delete
def cleanup_delegations(all_res: set[str], noop: bool):
[335] Fix | Delete
"""Remove /var/cpanel/dips (ip delegation) files for deleted resellers"""
[336] Fix | Delete
for entry in Path('/var/cpanel/dips').iterdir():
[337] Fix | Delete
if entry.name not in all_res:
[338] Fix | Delete
logging.debug('deleting %s', entry)
[339] Fix | Delete
if not noop:
[340] Fix | Delete
entry.unlink()
[341] Fix | Delete
[342] Fix | Delete
[343] Fix | Delete
def check_orphans(
[344] Fix | Delete
user: str,
[345] Fix | Delete
main_ips: set[str],
[346] Fix | Delete
orphan_storage: defaultdict[list],
[347] Fix | Delete
term_fails: defaultdict[list],
[348] Fix | Delete
noop: bool,
[349] Fix | Delete
):
[350] Fix | Delete
"""Find orphaned accounts (accounts that have no existing owner)"""
[351] Fix | Delete
try:
[352] Fix | Delete
user_conf = CpanelConf.user_conf(user)
[353] Fix | Delete
except Exception:
[354] Fix | Delete
return
[355] Fix | Delete
owner = user_conf.get('config', 'owner', fallback=None)
[356] Fix | Delete
if not owner:
[357] Fix | Delete
return
[358] Fix | Delete
ip_address = user_conf.get('config', 'ip', fallback=None)
[359] Fix | Delete
if (
[360] Fix | Delete
not Path('/var/cpanel/users', owner).exists()
[361] Fix | Delete
or owner in rads.OUR_RESELLERS
[362] Fix | Delete
):
[363] Fix | Delete
# this is an orphaned account
[364] Fix | Delete
try:
[365] Fix | Delete
susp_time = Path('/var/cpanel/suspended', user).stat().st_mtime
[366] Fix | Delete
except FileNotFoundError:
[367] Fix | Delete
# the orphaned account is not suspended
[368] Fix | Delete
orphan_storage[owner].append(user)
[369] Fix | Delete
return
[370] Fix | Delete
# If the orphan is suspended for more than 14 days, terminate it
[371] Fix | Delete
if time.time() - susp_time > 14 * 86400:
[372] Fix | Delete
logging.info("Terminating suspended orphan user %s", user)
[373] Fix | Delete
if noop:
[374] Fix | Delete
return
[375] Fix | Delete
try:
[376] Fix | Delete
whmapi1.removeacct(user, keepdns=False)
[377] Fix | Delete
except CpAPIError as exc:
[378] Fix | Delete
logging.warning("Failed to terminate user %s: %s", user, exc)
[379] Fix | Delete
term_fails[owner].append(user)
[380] Fix | Delete
else:
[381] Fix | Delete
logging.debug(
[382] Fix | Delete
"Orphaned user %s has not been suspended long "
[383] Fix | Delete
"enough for auto-terminate",
[384] Fix | Delete
user,
[385] Fix | Delete
)
[386] Fix | Delete
return
[387] Fix | Delete
# This is a non-orphaned, child account.
[388] Fix | Delete
# While we're here, make sure the user's IP is correct.
[389] Fix | Delete
if not ip_address or ip_address in main_ips:
[390] Fix | Delete
# Assign the user their owner's IP
[391] Fix | Delete
set_child_owner_ip(user, owner, noop)
[392] Fix | Delete
[393] Fix | Delete
[394] Fix | Delete
def orphans_notify(reseller: str, orphans: list[str], noop: bool) -> None:
[395] Fix | Delete
"""Notify for unsuspended orphan accounts"""
[396] Fix | Delete
logging.warning(
[397] Fix | Delete
'%s orphaned accounts exist under the reseller %s. Sending STR.',
[398] Fix | Delete
len(orphans),
[399] Fix | Delete
reseller,
[400] Fix | Delete
)
[401] Fix | Delete
logging.debug('Orphans under %s: %r', reseller, orphans)
[402] Fix | Delete
if noop:
[403] Fix | Delete
return
[404] Fix | Delete
str_body = f"""
[405] Fix | Delete
The following orphan accounts have been located under owner {reseller}:
[406] Fix | Delete
[407] Fix | Delete
{' '.join(orphans)}
[408] Fix | Delete
[409] Fix | Delete
They appear to have an owner that does not exist, or is a reseller missing
[410] Fix | Delete
reseller privileges. If the orphan's owner exists in PowerPanel, please set
[411] Fix | Delete
their owner to 'inmotion' or 'hubhost' as appropriate. If the orphan's owner is
[412] Fix | Delete
a reseller, add reseller privileges. If the orphan account does not exist,
[413] Fix | Delete
please suspend them on the server with the command
[414] Fix | Delete
[415] Fix | Delete
"for orphan in {' '.join(orphans)}; do suspend_user $orphan -r orphan; done"
[416] Fix | Delete
[417] Fix | Delete
Thank you,
[418] Fix | Delete
{HOST}"""
[419] Fix | Delete
rads.send_email(
[420] Fix | Delete
to_addr="str@imhadmin.net",
[421] Fix | Delete
subject=f"Orphan accounts on {HOST} with owner {reseller}",
[422] Fix | Delete
body=str_body,
[423] Fix | Delete
)
[424] Fix | Delete
[425] Fix | Delete
[426] Fix | Delete
def term_fail_notice(reseller: str, orphans: list[str], noop: bool) -> None:
[427] Fix | Delete
"""Separate notification for orphans that failed to auto-term, because
[428] Fix | Delete
suspending them again won't fix the problem"""
[429] Fix | Delete
logging.warning(
[430] Fix | Delete
"%s orphaned accounts failed to auto-terminate under the reseller %s. "
[431] Fix | Delete
"Sending STR.",
[432] Fix | Delete
len(orphans),
[433] Fix | Delete
reseller,
[434] Fix | Delete
)
[435] Fix | Delete
logging.debug("terms failed for %r", orphans)
[436] Fix | Delete
if noop:
[437] Fix | Delete
return
[438] Fix | Delete
str_body = f"""
[439] Fix | Delete
The following orphan accounts were found under owner {reseller} and were
[440] Fix | Delete
suspended long enough to auto-terminate, but auto-termination failed:
[441] Fix | Delete
[442] Fix | Delete
{' '.join(orphans)}
[443] Fix | Delete
[444] Fix | Delete
Please investigate and if appropriate, run removeacct on the orphan accounts.
[445] Fix | Delete
[446] Fix | Delete
Thank you,
[447] Fix | Delete
{HOST}"""
[448] Fix | Delete
rads.send_email(
[449] Fix | Delete
to_addr="str@imhadmin.net",
[450] Fix | Delete
subject=f"Failed to auto-term orphans on {HOST} with owner {reseller}",
[451] Fix | Delete
body=str_body,
[452] Fix | Delete
)
[453] Fix | Delete
[454] Fix | Delete
[455] Fix | Delete
def set_child_owner_ip(user: str, owner: str, noop: bool) -> None:
[456] Fix | Delete
"""Assign the user their owner's IP"""
[457] Fix | Delete
try:
[458] Fix | Delete
owner_conf = CpanelConf.user_conf(owner)
[459] Fix | Delete
except Exception:
[460] Fix | Delete
owner_ipaddr = None
[461] Fix | Delete
else:
[462] Fix | Delete
owner_ipaddr = owner_conf.get('config', 'ip')
[463] Fix | Delete
if not owner_ipaddr:
[464] Fix | Delete
logging.error(
[465] Fix | Delete
"User %s has shared IP, but couldn't determine the IP of "
[466] Fix | Delete
"the owner %s to assign it to the child account",
[467] Fix | Delete
user,
[468] Fix | Delete
owner,
[469] Fix | Delete
)
[470] Fix | Delete
return
[471] Fix | Delete
logging.warning(
[472] Fix | Delete
"User %s has shared IP. Changing to owner %s's IP of %s",
[473] Fix | Delete
user,
[474] Fix | Delete
owner,
[475] Fix | Delete
owner_ipaddr,
[476] Fix | Delete
)
[477] Fix | Delete
if noop:
[478] Fix | Delete
return
[479] Fix | Delete
try:
[480] Fix | Delete
whmapi1.setsiteip(user, owner_ipaddr)
[481] Fix | Delete
except CpAPIError as exc:
[482] Fix | Delete
logging.error(
[483] Fix | Delete
"Error changing IP of %s to %s: %s", user, owner_ipaddr, exc
[484] Fix | Delete
)
[485] Fix | Delete
[486] Fix | Delete
[487] Fix | Delete
def set_owner(user: str, old: str, new: str, noop: bool):
[488] Fix | Delete
"""Change user owner and log"""
[489] Fix | Delete
logging.info("Changing ownership of %s from %s to %s", user, old, new)
[490] Fix | Delete
if noop:
[491] Fix | Delete
return
[492] Fix | Delete
try:
[493] Fix | Delete
whmapi1.set_owner(user, new)
[494] Fix | Delete
except CpAPIError as exc:
[495] Fix | Delete
logging.error(
[496] Fix | Delete
"Error changing ownership of %s to %s: %s", user, new, exc
[497] Fix | Delete
)
[498] Fix | Delete
[499] Fix | Delete
12
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function