Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ExeBy/exe_root.../opt/sharedra...
File: docroot.py
#!/opt/imh-python/bin/python3
[0] Fix | Delete
"""
[1] Fix | Delete
@noaha
[2] Fix | Delete
[3] Fix | Delete
Quickly and accurately return the document root of a given domain
[4] Fix | Delete
owned by the current cPanel user as reported by cPanel userdata
[5] Fix | Delete
"""
[6] Fix | Delete
import yaml
[7] Fix | Delete
from getpass import getuser
[8] Fix | Delete
from sys import argv
[9] Fix | Delete
import sys
[10] Fix | Delete
from os import geteuid
[11] Fix | Delete
import re
[12] Fix | Delete
[13] Fix | Delete
[14] Fix | Delete
def load_yaml(filename, exit_on_fail=False):
[15] Fix | Delete
"""
[16] Fix | Delete
return yaml object from filename and
[17] Fix | Delete
handle exceptions according exit_on_fail
[18] Fix | Delete
"""
[19] Fix | Delete
try:
[20] Fix | Delete
with open(filename) as f:
[21] Fix | Delete
yaml_text = f.read()
[22] Fix | Delete
except OSError:
[23] Fix | Delete
if exit_on_fail:
[24] Fix | Delete
sys.exit("Could not read file: %s" % filename)
[25] Fix | Delete
else:
[26] Fix | Delete
raise
[27] Fix | Delete
try:
[28] Fix | Delete
return yaml.load(yaml_text, Loader=yaml.SafeLoader)
[29] Fix | Delete
except yaml.YAMLError as err:
[30] Fix | Delete
if exit_on_fail:
[31] Fix | Delete
sys.exit(f"Unable to parse YAML from {filename}:\n{err}")
[32] Fix | Delete
else:
[33] Fix | Delete
raise
[34] Fix | Delete
[35] Fix | Delete
[36] Fix | Delete
def get_docroot(user, vhost, exit_on_fail=False):
[37] Fix | Delete
"""
[38] Fix | Delete
Get the full path to a domain/vhost's document root.
[39] Fix | Delete
If exit_on_fail is True, the process will exit upon failure
[40] Fix | Delete
This is in place for use as a module
[41] Fix | Delete
[42] Fix | Delete
If disabled, throws: yaml.YAMLError, IOError,
[43] Fix | Delete
"""
[44] Fix | Delete
if not user:
[45] Fix | Delete
if exit_on_fail:
[46] Fix | Delete
sys.exit("cPanel user was not specified")
[47] Fix | Delete
raise ValueError("User must have value")
[48] Fix | Delete
if not vhost:
[49] Fix | Delete
if exit_on_fail:
[50] Fix | Delete
sys.exit("Domain was not specified")
[51] Fix | Delete
raise ValueError("vhost must have value")
[52] Fix | Delete
# We process everything in lowercase to avoid casing issues
[53] Fix | Delete
vhost = vhost.lower()
[54] Fix | Delete
userdata_file = "/var/cpanel/userdata/%s/main" % user
[55] Fix | Delete
userdata = load_yaml(userdata_file, exit_on_fail)
[56] Fix | Delete
[57] Fix | Delete
if vhost.startswith("www."):
[58] Fix | Delete
# This check is in defense against a subdomain
[59] Fix | Delete
# "www.example.domain.com" which may or may not be possible, but we
[60] Fix | Delete
# do not want to risk the confusion.
[61] Fix | Delete
if vhost not in userdata["sub_domains"]:
[62] Fix | Delete
# Otherwise, strip the first "www" from vhost
[63] Fix | Delete
vhost = vhost.split("www.", 1)[1]
[64] Fix | Delete
[65] Fix | Delete
# locate the data file that contains vhost information
[66] Fix | Delete
if userdata["main_domain"] == vhost or vhost in userdata["sub_domains"]:
[67] Fix | Delete
domain_datafile = f"/var/cpanel/userdata/{user}/{vhost}"
[68] Fix | Delete
elif vhost in userdata["parked_domains"]:
[69] Fix | Delete
domain_datafile = "/var/cpanel/userdata/{}/{}".format(
[70] Fix | Delete
user, userdata["main_domain"]
[71] Fix | Delete
)
[72] Fix | Delete
elif vhost in userdata["addon_domains"]:
[73] Fix | Delete
domain_datafile = "/var/cpanel/userdata/{}/{}".format(
[74] Fix | Delete
user, userdata["addon_domains"][vhost]
[75] Fix | Delete
)
[76] Fix | Delete
else:
[77] Fix | Delete
if exit_on_fail:
[78] Fix | Delete
sys.exit("Unable to find vhost in userdata")
[79] Fix | Delete
return None
[80] Fix | Delete
[81] Fix | Delete
domain_data = load_yaml(domain_datafile, exit_on_fail)
[82] Fix | Delete
# Double check this file actually contains our vhost
[83] Fix | Delete
if domain_data["servername"] == vhost or re.search(
[84] Fix | Delete
"(^| )%s( |$)" % vhost, domain_data["serveralias"]
[85] Fix | Delete
):
[86] Fix | Delete
return domain_data["documentroot"]
[87] Fix | Delete
if exit_on_fail:
[88] Fix | Delete
sys.exit(
[89] Fix | Delete
f"Something is wrong, the domain was not found in {domain_datafile}"
[90] Fix | Delete
)
[91] Fix | Delete
return None
[92] Fix | Delete
[93] Fix | Delete
[94] Fix | Delete
def whoowns(domain, exit_on_fail=False):
[95] Fix | Delete
"""
[96] Fix | Delete
Implement whoowns without forcing a fork.
[97] Fix | Delete
"""
[98] Fix | Delete
try:
[99] Fix | Delete
with open("/etc/userdomains") as f:
[100] Fix | Delete
userdomains = f.read().splitlines()
[101] Fix | Delete
except OSError:
[102] Fix | Delete
if exit_on_fail:
[103] Fix | Delete
sys.exit("Could not read file: /etc/userdomains")
[104] Fix | Delete
else:
[105] Fix | Delete
raise
[106] Fix | Delete
for userdomain in userdomains:
[107] Fix | Delete
split = userdomain.partition(": ")
[108] Fix | Delete
if split[0] == domain:
[109] Fix | Delete
return split[2]
[110] Fix | Delete
[111] Fix | Delete
sys.exit("Domain not found in /etc/userdomains")
[112] Fix | Delete
[113] Fix | Delete
[114] Fix | Delete
def main():
[115] Fix | Delete
if len(argv) < 2 or argv[1] is None or "-h" in argv or "--help" in argv:
[116] Fix | Delete
sys.exit(
[117] Fix | Delete
"""Usage: docroot.py domain
[118] Fix | Delete
[119] Fix | Delete
Quickly and accurately return the document root of a given domain
[120] Fix | Delete
owned by the current cPanel user as reported by cPanel userdata"""
[121] Fix | Delete
)
[122] Fix | Delete
[123] Fix | Delete
# strip protocol, perform basic validation
[124] Fix | Delete
validation_result = re.match(
[125] Fix | Delete
r"^(https?://)?([^/]+\.[^/]+)/?$", argv[1], re.I
[126] Fix | Delete
)
[127] Fix | Delete
if validation_result:
[128] Fix | Delete
vhost = validation_result.group(2)
[129] Fix | Delete
else:
[130] Fix | Delete
sys.exit("Invalid domain/vhost specified")
[131] Fix | Delete
[132] Fix | Delete
if geteuid() == 0:
[133] Fix | Delete
user = whoowns(vhost, exit_on_fail=True)
[134] Fix | Delete
else:
[135] Fix | Delete
user = getuser()
[136] Fix | Delete
[137] Fix | Delete
print(get_docroot(user, vhost, exit_on_fail=True))
[138] Fix | Delete
[139] Fix | Delete
[140] Fix | Delete
if __name__ == "__main__":
[141] Fix | Delete
main()
[142] Fix | Delete
[143] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function