Files
CyberPanel/plogical/vhost.py

870 lines
34 KiB
Python
Raw Normal View History

2018-06-30 15:29:56 +05:00
#!/usr/local/CyberCP/bin/python2
2018-06-01 02:08:21 +05:00
import os
import os.path
import sys
import django
sys.path.append('/usr/local/CyberCP')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "CyberCP.settings")
2019-07-18 14:08:00 +05:00
try:
django.setup()
except:
pass
2018-06-01 02:08:21 +05:00
import shutil
import installUtilities
2019-07-18 14:08:00 +05:00
2018-06-01 02:08:21 +05:00
import subprocess
import shlex
import CyberCPLogFileWriter as logging
2019-07-18 14:08:00 +05:00
2018-06-01 02:08:21 +05:00
from mysqlUtilities import mysqlUtilities
from dnsUtilities import DNS
from random import randint
2018-11-09 22:01:28 +05:00
from processUtilities import ProcessUtilities
2018-12-17 18:46:34 +05:00
from managePHP.phpManager import PHPManager
2019-01-28 17:13:19 +05:00
from vhostConfs import vhostConfs
2019-06-08 21:41:43 +00:00
from ApachController.ApacheVhosts import ApacheVhost
2019-07-18 14:08:00 +05:00
try:
from websiteFunctions.models import Websites, ChildDomains, aliasDomains
from databases.models import Databases
except:
pass
2018-06-01 02:08:21 +05:00
## If you want justice, you have come to the wrong place.
class vhost:
Server_root = "/usr/local/lsws"
cyberPanel = "/usr/local/CyberCP"
@staticmethod
def addUser(virtualHostUser, path):
try:
FNULL = open(os.devnull, 'w')
2018-11-08 12:11:42 +05:00
if os.path.exists("/etc/lsb-release"):
command = 'adduser --no-create-home --home ' + path + ' --disabled-login --gecos "" ' + virtualHostUser
else:
command = "adduser " + virtualHostUser + " -M -d " + path
2018-06-01 02:08:21 +05:00
cmd = shlex.split(command)
2019-03-26 16:19:03 +05:00
subprocess.call(cmd, stdout=FNULL, stderr=subprocess.STDOUT)
2018-06-01 02:08:21 +05:00
command = "groupadd " + virtualHostUser
cmd = shlex.split(command)
2019-03-26 16:19:03 +05:00
subprocess.call(cmd, stdout=FNULL, stderr=subprocess.STDOUT)
2018-06-01 02:08:21 +05:00
command = "usermod -a -G " + virtualHostUser + " " + virtualHostUser
cmd = shlex.split(command)
2019-03-26 16:19:03 +05:00
subprocess.call(cmd, stdout=FNULL, stderr=subprocess.STDOUT)
2018-06-01 02:08:21 +05:00
2019-12-10 15:09:10 +05:00
except BaseException as msg:
2018-06-01 02:08:21 +05:00
logging.CyberCPLogFileWriter.writeToFile(str(msg) + " [addingUsers]")
@staticmethod
def createDirectories(path, virtualHostUser, pathHTML, pathLogs, confPath, completePathToConfigFile):
try:
FNULL = open(os.devnull, 'w')
try:
command = 'chmod 711 /home'
cmd = shlex.split(command)
2019-03-26 16:19:03 +05:00
subprocess.call(cmd, stdout=FNULL, stderr=subprocess.STDOUT)
except:
pass
2018-06-01 02:08:21 +05:00
try:
os.makedirs(path)
command = "chown " + virtualHostUser + ":" + virtualHostUser + " " + path
cmd = shlex.split(command)
2019-03-26 16:19:03 +05:00
subprocess.call(cmd, stdout=FNULL, stderr=subprocess.STDOUT)
2018-06-01 02:08:21 +05:00
command = "chmod 711 " + path
cmd = shlex.split(command)
2019-03-26 16:19:03 +05:00
subprocess.call(cmd, stdout=FNULL, stderr=subprocess.STDOUT)
2019-12-10 15:09:10 +05:00
except OSError as msg:
2018-06-01 02:08:21 +05:00
logging.CyberCPLogFileWriter.writeToFile(
str(msg) + " [27 Not able create to directories for virtual host [createDirectories]]")
return [0, "[27 Not able to directories for virtual host [createDirectories]]"]
try:
os.makedirs(pathHTML)
command = "chown " + virtualHostUser + ":" + virtualHostUser + " " + pathHTML
cmd = shlex.split(command)
2019-03-26 16:19:03 +05:00
subprocess.call(cmd, stdout=FNULL, stderr=subprocess.STDOUT)
2018-06-01 02:08:21 +05:00
2019-12-10 15:09:10 +05:00
except OSError as msg:
2018-06-01 02:08:21 +05:00
logging.CyberCPLogFileWriter.writeToFile(
str(msg) + " [33 Not able to directories for virtual host [createDirectories]]")
return [0, "[33 Not able to directories for virtual host [createDirectories]]"]
try:
os.makedirs(pathLogs)
2019-08-03 14:53:31 +05:00
if ProcessUtilities.decideDistro() == ProcessUtilities.centos:
groupName = 'nobody'
else:
groupName = 'nogroup'
command = "chown %s:%s %s" % ('root', groupName, pathLogs)
2018-06-01 02:08:21 +05:00
cmd = shlex.split(command)
2019-03-26 16:19:03 +05:00
subprocess.call(cmd, stdout=FNULL, stderr=subprocess.STDOUT)
2018-06-01 02:08:21 +05:00
2018-11-09 22:01:28 +05:00
if ProcessUtilities.decideServer() == ProcessUtilities.OLS:
command = "chmod -R 750 " + pathLogs
2018-11-09 22:01:28 +05:00
else:
command = "chmod -R 750 " + pathLogs
2018-11-09 22:01:28 +05:00
2018-06-01 02:08:21 +05:00
cmd = shlex.split(command)
2019-03-26 16:19:03 +05:00
subprocess.call(cmd, stdout=FNULL, stderr=subprocess.STDOUT)
2018-06-01 02:08:21 +05:00
2019-12-10 15:09:10 +05:00
except OSError as msg:
2018-06-01 02:08:21 +05:00
logging.CyberCPLogFileWriter.writeToFile(
str(msg) + " [39 Not able to directories for virtual host [createDirectories]]")
return [0, "[39 Not able to directories for virtual host [createDirectories]]"]
try:
## For configuration files permissions will be changed later globally.
os.makedirs(confPath)
2019-12-10 15:09:10 +05:00
except OSError as msg:
2018-06-01 02:08:21 +05:00
logging.CyberCPLogFileWriter.writeToFile(
str(msg) + " [45 Not able to directories for virtual host [createDirectories]]")
return [0, "[45 Not able to directories for virtual host [createDirectories]]"]
try:
## For configuration files permissions will be changed later globally.
file = open(completePathToConfigFile, "w+")
command = "chown " + "lsadm" + ":" + "lsadm" + " " + completePathToConfigFile
cmd = shlex.split(command)
2019-03-26 16:19:03 +05:00
subprocess.call(cmd, stdout=FNULL, stderr=subprocess.STDOUT)
2018-06-01 02:08:21 +05:00
command = 'chmod 600 %s' % (completePathToConfigFile)
cmd = shlex.split(command)
subprocess.call(cmd, stdout=FNULL, stderr=subprocess.STDOUT)
2019-12-10 15:09:10 +05:00
except IOError as msg:
2018-06-01 02:08:21 +05:00
logging.CyberCPLogFileWriter.writeToFile(str(msg) + " [createDirectories]]")
return [0, "[45 Not able to directories for virtual host [createDirectories]]"]
return [1, 'None']
2019-12-10 15:09:10 +05:00
except BaseException as msg:
2018-06-01 02:08:21 +05:00
logging.CyberCPLogFileWriter.writeToFile(str(msg) + " [createDirectories]")
return [0, str(msg)]
@staticmethod
def finalizeVhostCreation(virtualHostName, virtualHostUser):
try:
FNULL = open(os.devnull, 'w')
shutil.copy("/usr/local/CyberCP/index.html", "/home/" + virtualHostName + "/public_html/index.html")
command = "chown " + virtualHostUser + ":" + virtualHostUser + " " + "/home/" + virtualHostName + "/public_html/index.html"
cmd = shlex.split(command)
2019-03-26 16:19:03 +05:00
subprocess.call(cmd, stdout=FNULL, stderr=subprocess.STDOUT)
2018-06-01 02:08:21 +05:00
vhostPath = vhost.Server_root + "/conf/vhosts"
command = "chown -R " + "lsadm" + ":" + "lsadm" + " " + vhostPath
cmd = shlex.split(command)
2019-03-26 16:19:03 +05:00
subprocess.call(cmd, stdout=FNULL, stderr=subprocess.STDOUT)
2018-06-01 02:08:21 +05:00
2019-12-10 15:09:10 +05:00
except BaseException as msg:
2018-06-01 02:08:21 +05:00
logging.CyberCPLogFileWriter.writeToFile(str(msg) + " [finalizeVhostCreation]")
@staticmethod
def createDirectoryForVirtualHost(virtualHostName,administratorEmail,virtualHostUser, phpVersion, openBasedir):
path = "/home/" + virtualHostName
pathHTML = "/home/" + virtualHostName + "/public_html"
pathLogs = "/home/" + virtualHostName + "/logs"
confPath = vhost.Server_root + "/conf/vhosts/"+virtualHostName
completePathToConfigFile = confPath +"/vhost.conf"
## adding user
vhost.addUser(virtualHostUser, path)
## Creating Directories
result = vhost.createDirectories(path, virtualHostUser, pathHTML, pathLogs, confPath, completePathToConfigFile)
if result[0] == 0:
return [0, result[1]]
## Creating Per vhost Configuration File
if vhost.perHostVirtualConf(completePathToConfigFile,administratorEmail,virtualHostUser,phpVersion, virtualHostName, openBasedir) == 1:
return [1,"None"]
else:
return [0,"[61 Not able to create per host virtual configurations [perHostVirtualConf]"]
@staticmethod
def perHostVirtualConf(vhFile, administratorEmail,virtualHostUser, phpVersion, virtualHostName, openBasedir):
# General Configurations tab
2018-11-09 22:01:28 +05:00
if ProcessUtilities.decideServer() == ProcessUtilities.OLS:
try:
confFile = open(vhFile, "w+")
2018-12-17 18:46:34 +05:00
php = PHPManager.getPHPString(phpVersion)
2018-11-09 22:01:28 +05:00
2019-01-28 17:13:19 +05:00
currentConf = vhostConfs.olsMasterConf
currentConf = currentConf.replace('{adminEmails}', administratorEmail)
currentConf = currentConf.replace('{virtualHostUser}', virtualHostUser)
currentConf = currentConf.replace('{php}', php)
currentConf = currentConf.replace('{adminEmails}', administratorEmail)
currentConf = currentConf.replace('{php}', php)
2018-11-09 22:01:28 +05:00
if openBasedir == 1:
2019-01-28 17:13:19 +05:00
currentConf = currentConf.replace('{open_basedir}', 'php_admin_value open_basedir "/tmp:$VH_ROOT"')
else:
currentConf = currentConf.replace('{open_basedir}', '')
2018-11-09 22:01:28 +05:00
2019-01-28 17:13:19 +05:00
confFile.write(currentConf)
2018-11-09 22:01:28 +05:00
confFile.close()
2019-12-10 15:09:10 +05:00
except BaseException as msg:
2018-11-09 22:01:28 +05:00
logging.CyberCPLogFileWriter.writeToFile(
str(msg) + " [IO Error with per host config file [perHostVirtualConf]]")
return 0
return 1
else:
try:
confFile = open(vhFile, "w+")
2018-12-17 18:46:34 +05:00
php = PHPManager.getPHPString(phpVersion)
2018-11-09 22:01:28 +05:00
2019-01-29 16:31:59 +05:00
currentConf = vhostConfs.lswsMasterConf
2018-11-09 22:01:28 +05:00
2019-01-29 16:31:59 +05:00
currentConf = currentConf.replace('{virtualHostName}', virtualHostName)
currentConf = currentConf.replace('{administratorEmail}', administratorEmail)
currentConf = currentConf.replace('{externalApp}', virtualHostUser)
currentConf = currentConf.replace('{php}', php)
confFile.write(currentConf)
2018-11-09 22:01:28 +05:00
confFile.close()
2019-12-10 15:09:10 +05:00
except BaseException as msg:
2018-11-09 22:01:28 +05:00
logging.CyberCPLogFileWriter.writeToFile(
str(msg) + " [IO Error with per host config file [perHostVirtualConf]]")
return 0
return 1
2018-06-01 02:08:21 +05:00
@staticmethod
def createNONSSLMapEntry(virtualHostName):
try:
data = open("/usr/local/lsws/conf/httpd_config.conf").readlines()
writeDataToFile = open("/usr/local/lsws/conf/httpd_config.conf", 'w')
map = " map " + virtualHostName + " " + virtualHostName + "\n"
mapchecker = 1
for items in data:
if (mapchecker == 1 and (items.find("listener") > -1 and items.find("Default") > -1)):
writeDataToFile.writelines(items)
writeDataToFile.writelines(map)
mapchecker = 0
else:
writeDataToFile.writelines(items)
return 1
2019-12-10 15:09:10 +05:00
except BaseException as msg:
2018-06-01 02:08:21 +05:00
logging.CyberCPLogFileWriter.writeToFile(str(msg))
return 0
@staticmethod
def createConfigInMainVirtualHostFile(virtualHostName):
2018-11-09 22:01:28 +05:00
if ProcessUtilities.decideServer() == ProcessUtilities.OLS:
try:
2018-06-01 02:08:21 +05:00
2018-11-09 22:01:28 +05:00
if vhost.createNONSSLMapEntry(virtualHostName) == 0:
return [0, "Failed to create NON SSL Map Entry [createConfigInMainVirtualHostFile]"]
2018-06-01 02:08:21 +05:00
2018-11-09 22:01:28 +05:00
writeDataToFile = open("/usr/local/lsws/conf/httpd_config.conf", 'a')
2018-06-01 02:08:21 +05:00
2019-01-29 16:31:59 +05:00
currentConf = vhostConfs.olsMasterMainConf
currentConf = currentConf.replace('{virtualHostName}', virtualHostName)
writeDataToFile.write(currentConf)
2018-06-01 02:08:21 +05:00
2018-11-09 22:01:28 +05:00
writeDataToFile.close()
2018-06-01 02:08:21 +05:00
2018-11-09 22:01:28 +05:00
return [1,"None"]
2019-12-10 15:09:10 +05:00
except BaseException as msg:
2018-11-09 22:01:28 +05:00
logging.CyberCPLogFileWriter.writeToFile(str(msg) + "223 [IO Error with main config file [createConfigInMainVirtualHostFile]]")
return [0,"223 [IO Error with main config file [createConfigInMainVirtualHostFile]]"]
else:
try:
writeDataToFile = open("/usr/local/lsws/conf/httpd.conf", 'a')
configFile = 'Include /usr/local/lsws/conf/vhosts/' + virtualHostName + '/vhost.conf\n'
writeDataToFile.writelines(configFile)
writeDataToFile.close()
writeDataToFile.close()
return [1, "None"]
2019-12-10 15:09:10 +05:00
except BaseException as msg:
2018-11-09 22:01:28 +05:00
logging.CyberCPLogFileWriter.writeToFile(
str(msg) + "223 [IO Error with main config file [createConfigInMainVirtualHostFile]]")
return [0, "223 [IO Error with main config file [createConfigInMainVirtualHostFile]]"]
2018-06-01 02:08:21 +05:00
@staticmethod
2018-08-26 04:55:51 +05:00
def deleteVirtualHostConfigurations(virtualHostName):
2018-11-09 22:01:28 +05:00
if ProcessUtilities.decideServer() == ProcessUtilities.OLS:
try:
2018-06-01 02:08:21 +05:00
2018-11-09 22:01:28 +05:00
## Deleting master conf
numberOfSites = str(Websites.objects.count() + ChildDomains.objects.count())
vhost.deleteCoreConf(virtualHostName, numberOfSites)
2018-06-01 02:08:21 +05:00
2018-11-09 22:01:28 +05:00
delWebsite = Websites.objects.get(domain=virtualHostName)
2019-09-14 11:51:44 +05:00
## Cagefs
command = '/usr/sbin/cagefsctl --disable %s' % (delWebsite.externalApp)
ProcessUtilities.normalExecutioner(command)
2018-11-09 22:01:28 +05:00
databases = Databases.objects.filter(website=delWebsite)
2018-06-01 02:08:21 +05:00
2018-11-09 22:01:28 +05:00
childDomains = delWebsite.childdomains_set.all()
2018-06-01 02:08:21 +05:00
2018-11-09 22:01:28 +05:00
## Deleting child domains
2018-06-01 02:08:21 +05:00
2018-11-09 22:01:28 +05:00
for items in childDomains:
numberOfSites = Websites.objects.count() + ChildDomains.objects.count()
vhost.deleteCoreConf(items.domain, numberOfSites)
2018-06-01 02:08:21 +05:00
2018-11-09 22:01:28 +05:00
for items in databases:
mysqlUtilities.deleteDatabase(items.dbName, items.dbUser)
2018-06-01 02:08:21 +05:00
2018-11-09 22:01:28 +05:00
delWebsite.delete()
2018-06-01 02:08:21 +05:00
2018-11-09 22:01:28 +05:00
## Deleting DNS Zone if there is any.
2018-06-01 02:08:21 +05:00
2018-11-09 22:01:28 +05:00
DNS.deleteDNSZone(virtualHostName)
2018-06-01 02:08:21 +05:00
2018-11-09 22:01:28 +05:00
installUtilities.installUtilities.reStartLiteSpeed()
2018-06-01 02:08:21 +05:00
2018-11-09 22:01:28 +05:00
## Delete mail accounts
command = "sudo rm -rf /home/vmail/" + virtualHostName
2019-03-26 16:19:03 +05:00
subprocess.call(shlex.split(command))
2019-12-10 15:09:10 +05:00
except BaseException as msg:
2018-11-09 22:01:28 +05:00
logging.CyberCPLogFileWriter.writeToFile(str(msg) + " [Not able to remove virtual host configuration from main configuration file.]")
return 0
return 1
else:
try:
## Deleting master conf
numberOfSites = str(Websites.objects.count() + ChildDomains.objects.count())
vhost.deleteCoreConf(virtualHostName, numberOfSites)
2018-06-01 02:08:21 +05:00
2018-11-09 22:01:28 +05:00
delWebsite = Websites.objects.get(domain=virtualHostName)
2019-09-14 11:51:44 +05:00
## Cagefs
command = '/usr/sbin/cagefsctl --disable %s' % (delWebsite.externalApp)
ProcessUtilities.normalExecutioner(command)
2018-11-09 22:01:28 +05:00
databases = Databases.objects.filter(website=delWebsite)
2018-06-01 02:08:21 +05:00
2018-11-09 22:01:28 +05:00
childDomains = delWebsite.childdomains_set.all()
2018-06-01 02:08:21 +05:00
2018-11-09 22:01:28 +05:00
## Deleting child domains
for items in childDomains:
numberOfSites = Websites.objects.count() + ChildDomains.objects.count()
vhost.deleteCoreConf(items.domain, numberOfSites)
for items in databases:
mysqlUtilities.deleteDatabase(items.dbName, items.dbUser)
delWebsite.delete()
## Deleting DNS Zone if there is any.
DNS.deleteDNSZone(virtualHostName)
installUtilities.installUtilities.reStartLiteSpeed()
## Delete mail accounts
command = "sudo rm -rf /home/vmail/" + virtualHostName
2019-03-26 16:19:03 +05:00
subprocess.call(shlex.split(command))
2019-12-10 15:09:10 +05:00
except BaseException as msg:
2018-11-09 22:01:28 +05:00
logging.CyberCPLogFileWriter.writeToFile(
str(msg) + " [Not able to remove virtual host configuration from main configuration file.]")
return 0
return 1
2018-06-01 02:08:21 +05:00
@staticmethod
def deleteCoreConf(virtualHostName, numberOfSites):
2018-11-09 22:01:28 +05:00
if ProcessUtilities.decideServer() == ProcessUtilities.OLS:
try:
virtualHostPath = "/home/" + virtualHostName
if os.path.exists(virtualHostPath):
shutil.rmtree(virtualHostPath)
confPath = vhost.Server_root + "/conf/vhosts/" + virtualHostName
if os.path.exists(confPath):
shutil.rmtree(confPath)
data = open("/usr/local/lsws/conf/httpd_config.conf").readlines()
writeDataToFile = open("/usr/local/lsws/conf/httpd_config.conf", 'w')
check = 1
sslCheck = 1
2018-06-01 02:08:21 +05:00
2018-11-09 22:01:28 +05:00
for items in data:
if numberOfSites == 1:
if (items.find(' ' + virtualHostName) > -1 and items.find(" map " + virtualHostName) > -1):
continue
if (items.find(' ' + virtualHostName) > -1 and (items.find("virtualHost") > -1 or items.find("virtualhost") > -1)):
check = 0
if items.find("listener") > -1 and items.find("SSL") > -1:
sslCheck = 0
if (check == 1 and sslCheck == 1):
writeDataToFile.writelines(items)
if (items.find("}") > -1 and (check == 0 or sslCheck == 0)):
check = 1
sslCheck = 1
else:
if (items.find(' ' + virtualHostName) > -1 and items.find(" map " + virtualHostName) > -1):
continue
if (items.find(' ' + virtualHostName) > -1 and (items.find("virtualHost") > -1 or items.find("virtualhost") > -1)):
check = 0
if (check == 1):
writeDataToFile.writelines(items)
if (items.find("}") > -1 and check == 0):
check = 1
2019-06-08 21:41:43 +00:00
## Delete Apache Conf
ApacheVhost.DeleteApacheVhost(virtualHostName)
2019-12-10 15:09:10 +05:00
except BaseException as msg:
2018-11-09 22:01:28 +05:00
logging.CyberCPLogFileWriter.writeToFile(
str(msg) + " [Not able to remove virtual host configuration from main configuration file.]")
return 0
return 1
else:
2018-10-12 18:18:10 +05:00
virtualHostPath = "/home/" + virtualHostName
2018-11-09 22:01:28 +05:00
try:
2018-10-12 18:18:10 +05:00
shutil.rmtree(virtualHostPath)
2019-12-10 15:09:10 +05:00
except BaseException as msg:
2018-11-09 22:01:28 +05:00
logging.CyberCPLogFileWriter.writeToFile(
str(msg) + " [Not able to remove virtual host directory from /home continuing..]")
2018-06-01 02:08:21 +05:00
2018-11-09 22:01:28 +05:00
try:
confPath = vhost.Server_root + "/conf/vhosts/" + virtualHostName
2018-10-12 18:18:10 +05:00
shutil.rmtree(confPath)
2019-12-10 15:09:10 +05:00
except BaseException as msg:
2018-11-09 22:01:28 +05:00
logging.CyberCPLogFileWriter.writeToFile(
str(msg) + " [Not able to remove virtual host configuration directory from /conf ]")
2018-06-01 02:08:21 +05:00
2018-11-09 22:01:28 +05:00
try:
data = open("/usr/local/lsws/conf/httpd.conf").readlines()
2018-06-01 02:08:21 +05:00
2018-11-09 22:01:28 +05:00
writeDataToFile = open("/usr/local/lsws/conf/httpd.conf", 'w')
2018-06-01 02:08:21 +05:00
2018-11-09 22:01:28 +05:00
for items in data:
if items.find('/' + virtualHostName + '/') > -1:
pass
else:
2018-06-01 02:08:21 +05:00
writeDataToFile.writelines(items)
2018-11-09 22:01:28 +05:00
writeDataToFile.close()
2019-12-10 15:09:10 +05:00
except BaseException as msg:
2018-11-09 22:01:28 +05:00
logging.CyberCPLogFileWriter.writeToFile(
str(msg) + " [Not able to remove virtual host configuration from main configuration file.]")
return 0
return 1
2018-06-01 02:08:21 +05:00
@staticmethod
def checkIfVirtualHostExists(virtualHostName):
if os.path.exists("/home/" + virtualHostName):
return 1
return 0
@staticmethod
def changePHP(vhFile, phpVersion):
phpDetachUpdatePath = '/home/%s/.lsphp_restart.txt' % (vhFile.split('/')[-2])
2018-11-09 22:01:28 +05:00
if ProcessUtilities.decideServer() == ProcessUtilities.OLS:
try:
2019-06-08 21:41:43 +00:00
if ApacheVhost.changePHP(phpVersion, vhFile) == 0:
data = open(vhFile, "r").readlines()
2018-11-09 22:01:28 +05:00
2019-06-08 21:41:43 +00:00
php = PHPManager.getPHPString(phpVersion)
2018-11-09 22:01:28 +05:00
2019-06-08 21:41:43 +00:00
if not os.path.exists("/usr/local/lsws/lsphp" + str(php) + "/bin/lsphp"):
2019-12-10 15:09:10 +05:00
print(0, 'This PHP version is not available on your CyberPanel.')
2019-06-08 21:41:43 +00:00
return [0, "[This PHP version is not available on your CyberPanel. [changePHP]"]
2019-06-08 21:41:43 +00:00
writeDataToFile = open(vhFile, "w")
2018-11-09 22:01:28 +05:00
2019-06-08 21:41:43 +00:00
path = " path /usr/local/lsws/lsphp" + str(php) + "/bin/lsphp\n"
2018-11-09 22:01:28 +05:00
2019-06-08 21:41:43 +00:00
for items in data:
if items.find("/usr/local/lsws/lsphp") > -1 and items.find("path") > -1:
writeDataToFile.writelines(path)
else:
writeDataToFile.writelines(items)
2018-06-01 02:08:21 +05:00
2019-06-08 21:41:43 +00:00
writeDataToFile.close()
2018-06-01 02:08:21 +05:00
writeToFile = open(phpDetachUpdatePath, 'w')
writeToFile.close()
2019-06-08 21:41:43 +00:00
installUtilities.installUtilities.reStartLiteSpeed()
try:
os.remove(phpDetachUpdatePath)
except:
pass
2019-06-08 21:41:43 +00:00
else:
php = PHPManager.getPHPString(phpVersion)
command = "systemctl restart php%s-php-fpm" % (php)
ProcessUtilities.normalExecutioner(command)
2018-06-01 02:08:21 +05:00
2019-12-10 15:09:10 +05:00
print("1,None")
2018-11-09 22:01:28 +05:00
return 1,'None'
2019-12-10 15:09:10 +05:00
except BaseException as msg:
2018-11-09 22:01:28 +05:00
logging.CyberCPLogFileWriter.writeToFile(
str(msg) + " [IO Error with per host config file [changePHP]")
2019-12-10 15:09:10 +05:00
print(0,str(msg))
return [0, str(msg) + " [IO Error with per host config file [changePHP]"]
2018-11-09 22:01:28 +05:00
else:
try:
data = open(vhFile, "r").readlines()
2018-12-17 18:46:34 +05:00
php = PHPManager.getPHPString(phpVersion)
2018-11-09 22:01:28 +05:00
if not os.path.exists("/usr/local/lsws/lsphp" + str(php) + "/bin/lsphp"):
2019-12-10 15:09:10 +05:00
print(0, 'This PHP version is not available on your CyberPanel.')
return [0, "[This PHP version is not available on your CyberPanel. [changePHP]"]
2018-11-09 22:01:28 +05:00
writeDataToFile = open(vhFile, "w")
2018-12-17 18:46:34 +05:00
finalString = ' AddHandler application/x-httpd-php' + str(php) + ' .php\n'
2018-11-09 22:01:28 +05:00
for items in data:
if items.find("AddHandler application/x-httpd") > -1:
writeDataToFile.writelines(finalString)
else:
writeDataToFile.writelines(items)
2018-06-01 02:08:21 +05:00
2018-11-09 22:01:28 +05:00
writeDataToFile.close()
2018-06-01 02:08:21 +05:00
writeToFile = open(phpDetachUpdatePath, 'w')
writeToFile.close()
2018-11-09 22:01:28 +05:00
installUtilities.installUtilities.reStartLiteSpeed()
try:
os.remove(phpDetachUpdatePath)
except:
pass
2018-06-01 02:08:21 +05:00
2019-12-10 15:09:10 +05:00
print("1,None")
2018-11-09 22:01:28 +05:00
return 1, 'None'
2019-12-10 15:09:10 +05:00
except BaseException as msg:
2018-11-09 22:01:28 +05:00
logging.CyberCPLogFileWriter.writeToFile(
str(msg) + " [IO Error with per host config file [changePHP]]")
2019-12-10 15:09:10 +05:00
print(0, str(msg))
2018-11-09 22:01:28 +05:00
return [0, str(msg) + " [IO Error with per host config file [changePHP]]"]
2018-06-01 02:08:21 +05:00
@staticmethod
def addRewriteRules(virtualHostName, fileName=None):
try:
2018-07-05 15:22:48 +05:00
pass
2019-12-10 15:09:10 +05:00
except BaseException as msg:
2018-06-01 02:08:21 +05:00
logging.CyberCPLogFileWriter.writeToFile(str(msg) + " [IO Error with per host config file [changePHP]]")
return 0
return 1
@staticmethod
def checkIfRewriteEnabled(data):
try:
for items in data:
if items.find(".htaccess") > -1:
return 1
return 0
2019-12-10 15:09:10 +05:00
except BaseException as msg:
2018-06-01 02:08:21 +05:00
logging.CyberCPLogFileWriter.writeToFile(
str(msg) + " [IO Error with per host config file [checkIfRewriteEnabled]]")
return 0
@staticmethod
def findDomainBW(domainName, totalAllowed):
try:
path = "/home/" + domainName + "/logs/" + domainName + ".access_log"
if not os.path.exists("/home/" + domainName + "/logs"):
2019-12-10 15:09:10 +05:00
print("0,0")
2018-06-01 02:08:21 +05:00
bwmeta = "/home/" + domainName + "/logs/bwmeta"
if not os.path.exists(path):
2019-12-10 15:09:10 +05:00
print("0,0")
2018-06-01 02:08:21 +05:00
if os.path.exists(bwmeta):
try:
data = open(bwmeta).readlines()
currentUsed = int(data[0].strip("\n"))
inMB = int(float(currentUsed) / (1024.0 * 1024.0))
2018-08-28 01:19:34 +05:00
if totalAllowed == 0:
totalAllowed = 999999
2018-06-01 02:08:21 +05:00
2018-08-28 01:19:34 +05:00
percentage = float(100) / float(totalAllowed)
2018-06-01 02:08:21 +05:00
percentage = float(percentage) * float(inMB)
except:
2019-12-10 15:09:10 +05:00
print("0,0")
2018-06-01 02:08:21 +05:00
if percentage > 100.0:
percentage = 100
2019-12-10 15:09:10 +05:00
print(str(inMB) + "," + str(percentage))
2018-06-01 02:08:21 +05:00
else:
2019-12-10 15:09:10 +05:00
print("0,0")
except OSError as msg:
2018-06-01 02:08:21 +05:00
logging.CyberCPLogFileWriter.writeToFile(str(msg) + " [findDomainBW]")
2019-12-10 15:09:10 +05:00
print("0,0")
except ValueError as msg:
2018-06-01 02:08:21 +05:00
logging.CyberCPLogFileWriter.writeToFile(str(msg) + " [findDomainBW]")
2019-12-10 15:09:10 +05:00
print("0,0")
2018-06-01 02:08:21 +05:00
@staticmethod
def permissionControl(path):
try:
command = 'sudo chown -R cyberpanel:cyberpanel ' + path
cmd = shlex.split(command)
2019-03-26 16:19:03 +05:00
res = subprocess.call(cmd)
2019-12-10 15:09:10 +05:00
except BaseException as msg:
2018-06-01 02:08:21 +05:00
logging.CyberCPLogFileWriter.writeToFile(str(msg))
@staticmethod
def leaveControl(path):
try:
command = 'sudo chown -R root:root ' + path
cmd = shlex.split(command)
2019-03-26 16:19:03 +05:00
res = subprocess.call(cmd)
2018-06-01 02:08:21 +05:00
2019-12-10 15:09:10 +05:00
except BaseException as msg:
2018-06-01 02:08:21 +05:00
logging.CyberCPLogFileWriter.writeToFile(str(msg))
@staticmethod
def checkIfAliasExists(aliasDomain):
try:
2018-08-23 15:39:28 +05:00
alias = aliasDomains.objects.get(aliasDomain=aliasDomain)
2018-06-01 02:08:21 +05:00
return 1
2019-12-10 15:09:10 +05:00
except BaseException as msg:
2018-08-23 15:39:28 +05:00
return 0
2018-06-01 02:08:21 +05:00
@staticmethod
def checkIfSSLAliasExists(data, aliasDomain):
try:
for items in data:
if items.strip(',').strip('\n') == aliasDomain:
return 1
return 0
2019-12-10 15:09:10 +05:00
except BaseException as msg:
2018-06-01 02:08:21 +05:00
logging.CyberCPLogFileWriter.writeToFile(str(msg) + " [checkIfSSLAliasExists]")
return 1
@staticmethod
def createAliasSSLMap(confPath, masterDomain, aliasDomain):
try:
data = open(confPath, 'r').readlines()
writeToFile = open(confPath, 'w')
sslCheck = 0
2018-10-12 18:18:10 +05:00
2018-06-01 02:08:21 +05:00
for items in data:
if (items.find("listener SSL") > -1):
sslCheck = 1
if items.find(masterDomain) > -1 and items.find('map') > -1 and sslCheck == 1:
2019-12-10 15:09:10 +05:00
data = [_f for _f in items.split(" ") if _f]
2018-06-01 02:08:21 +05:00
if data[1] == masterDomain:
if vhost.checkIfSSLAliasExists(data, aliasDomain) == 0:
writeToFile.writelines(items.rstrip('\n') + ", " + aliasDomain + "\n")
sslCheck = 0
else:
writeToFile.writelines(items)
else:
writeToFile.writelines(items)
writeToFile.close()
installUtilities.installUtilities.reStartLiteSpeed()
2019-12-10 15:09:10 +05:00
except BaseException as msg:
2018-06-01 02:08:21 +05:00
logging.CyberCPLogFileWriter.writeToFile(str(msg) + " [createAliasSSLMap]")
## Child Domain Functions
@staticmethod
def finalizeDomainCreation(virtualHostUser, path):
try:
FNULL = open(os.devnull, 'w')
shutil.copy("/usr/local/CyberCP/index.html", path + "/index.html")
command = "chown " + virtualHostUser + ":" + virtualHostUser + " " + path + "/index.html"
cmd = shlex.split(command)
2019-03-26 16:19:03 +05:00
subprocess.call(cmd, stdout=FNULL, stderr=subprocess.STDOUT)
2018-06-01 02:08:21 +05:00
vhostPath = vhost.Server_root + "/conf/vhosts"
command = "chown -R " + "lsadm" + ":" + "lsadm" + " " + vhostPath
cmd = shlex.split(command)
2019-03-26 16:19:03 +05:00
subprocess.call(cmd, stdout=FNULL, stderr=subprocess.STDOUT)
2018-06-01 02:08:21 +05:00
2019-12-10 15:09:10 +05:00
except BaseException as msg:
2018-06-01 02:08:21 +05:00
logging.CyberCPLogFileWriter.writeToFile(str(msg) + " [finalizeDomainCreation]")
@staticmethod
def createDirectoryForDomain(masterDomain, domain, phpVersion, path, administratorEmail, virtualHostUser,
openBasedir):
FNULL = open(os.devnull, 'w')
confPath = vhost.Server_root + "/conf/vhosts/" + domain
completePathToConfigFile = confPath + "/vhost.conf"
try:
os.makedirs(path)
command = "chown " + virtualHostUser + ":" + virtualHostUser + " " + path
cmd = shlex.split(command)
2019-03-26 16:19:03 +05:00
subprocess.call(cmd, stdout=FNULL, stderr=subprocess.STDOUT)
2019-12-10 15:09:10 +05:00
except OSError as msg:
2018-06-01 02:08:21 +05:00
logging.CyberCPLogFileWriter.writeToFile(
str(msg) + "329 [Not able to create directories for virtual host [createDirectoryForDomain]]")
try:
## For configuration files permissions will be changed later globally.
os.makedirs(confPath)
2019-12-10 15:09:10 +05:00
except OSError as msg:
2018-06-01 02:08:21 +05:00
logging.CyberCPLogFileWriter.writeToFile(
str(msg) + "335 [Not able to create directories for virtual host [createDirectoryForDomain]]")
return [0, "[344 Not able to directories for virtual host [createDirectoryForDomain]]"]
try:
## For configuration files permissions will be changed later globally.
file = open(completePathToConfigFile, "w+")
2019-12-10 15:09:10 +05:00
except IOError as msg:
2018-06-01 02:08:21 +05:00
logging.CyberCPLogFileWriter.writeToFile(str(msg) + " [createDirectoryForDomain]]")
return [0, "[351 Not able to directories for virtual host [createDirectoryForDomain]]"]
2018-11-09 22:01:28 +05:00
if vhost.perHostDomainConf(path, masterDomain, domain, completePathToConfigFile,
administratorEmail, phpVersion, virtualHostUser, openBasedir) == 1:
2018-06-01 02:08:21 +05:00
return [1, "None"]
else:
2019-01-29 16:31:59 +05:00
return [0, "[359 Not able to create per host virtual configurations [createDirectoryForDomain]"]
2018-06-01 02:08:21 +05:00
@staticmethod
2018-11-09 22:01:28 +05:00
def perHostDomainConf(path, masterDomain, domain, vhFile, administratorEmail, phpVersion, virtualHostUser, openBasedir):
if ProcessUtilities.decideServer() == ProcessUtilities.OLS:
try:
2019-01-29 16:31:59 +05:00
php = PHPManager.getPHPString(phpVersion)
externalApp = virtualHostUser + str(randint(1000, 9999))
2018-11-09 22:01:28 +05:00
2019-01-29 16:31:59 +05:00
currentConf = vhostConfs.olsChildConf
currentConf = currentConf.replace('{path}', path)
currentConf = currentConf.replace('{masterDomain}', masterDomain)
currentConf = currentConf.replace('{adminEmails}', administratorEmail)
currentConf = currentConf.replace('{externalApp}', externalApp)
2019-02-25 14:53:02 +05:00
currentConf = currentConf.replace('{externalAppMaster}', virtualHostUser)
2019-01-29 16:31:59 +05:00
currentConf = currentConf.replace('{php}', php)
currentConf = currentConf.replace('{adminEmails}', administratorEmail)
currentConf = currentConf.replace('{php}', php)
2018-11-09 22:01:28 +05:00
2019-01-29 16:31:59 +05:00
if openBasedir == 1:
currentConf = currentConf.replace('{open_basedir}', 'php_admin_value open_basedir "/tmp:$VH_ROOT"')
else:
currentConf = currentConf.replace('{open_basedir}', '')
2018-11-09 22:01:28 +05:00
2019-01-29 16:31:59 +05:00
confFile = open(vhFile, "w+")
confFile.write(currentConf)
2018-11-09 22:01:28 +05:00
confFile.close()
2019-01-29 16:31:59 +05:00
2019-12-10 15:09:10 +05:00
except BaseException as msg:
2018-11-09 22:01:28 +05:00
logging.CyberCPLogFileWriter.writeToFile(
str(msg) + " [IO Error with per host config file [perHostDomainConf]]")
return 0
return 1
else:
try:
2018-06-01 02:08:21 +05:00
2018-11-09 22:01:28 +05:00
confFile = open(vhFile, "w+")
2019-01-29 16:31:59 +05:00
php = PHPManager.getPHPString(phpVersion)
2018-11-09 22:01:28 +05:00
2019-01-29 16:31:59 +05:00
currentConf = vhostConfs.lswsChildConf
2018-11-09 22:01:28 +05:00
2019-01-29 16:31:59 +05:00
currentConf = currentConf.replace('{virtualHostName}', domain)
currentConf = currentConf.replace('{masterDomain}', masterDomain)
currentConf = currentConf.replace('{administratorEmail}', administratorEmail)
currentConf = currentConf.replace('{externalApp}', virtualHostUser)
currentConf = currentConf.replace('{path}', path)
currentConf = currentConf.replace('{php}', php)
2018-11-09 22:01:28 +05:00
2019-01-29 16:31:59 +05:00
confFile.write(currentConf)
2018-11-09 22:01:28 +05:00
confFile.close()
2019-12-10 15:09:10 +05:00
except BaseException as msg:
2018-11-09 22:01:28 +05:00
logging.CyberCPLogFileWriter.writeToFile(
str(msg) + " [IO Error with per host config file [perHostDomainConf]]")
return 0
return 1
2018-06-01 02:08:21 +05:00
@staticmethod
def createConfigInMainDomainHostFile(domain, masterDomain):
2018-11-09 22:01:28 +05:00
if ProcessUtilities.decideServer() == ProcessUtilities.OLS:
try:
2018-06-01 02:08:21 +05:00
2018-11-09 22:01:28 +05:00
if vhost.createNONSSLMapEntry(domain) == 0:
return [0, "Failed to create NON SSL Map Entry [createConfigInMainVirtualHostFile]"]
2018-06-01 02:08:21 +05:00
2018-11-09 22:01:28 +05:00
writeDataToFile = open("/usr/local/lsws/conf/httpd_config.conf", 'a')
2018-06-01 02:08:21 +05:00
2019-01-29 16:31:59 +05:00
currentConf = vhostConfs.olsChildMainConf
currentConf = currentConf.replace('{virtualHostName}', domain)
currentConf = currentConf.replace('{masterDomain}', masterDomain)
writeDataToFile.write(currentConf)
2018-06-01 02:08:21 +05:00
2018-11-09 22:01:28 +05:00
writeDataToFile.close()
2018-06-01 02:08:21 +05:00
2018-11-09 22:01:28 +05:00
return [1, "None"]
2018-06-01 02:08:21 +05:00
2019-12-10 15:09:10 +05:00
except BaseException as msg:
2018-11-09 22:01:28 +05:00
logging.CyberCPLogFileWriter.writeToFile(
str(msg) + "223 [IO Error with main config file [createConfigInMainDomainHostFile]]")
return [0, "223 [IO Error with main config file [createConfigInMainDomainHostFile]]"]
else:
try:
writeDataToFile = open("/usr/local/lsws/conf/httpd.conf", 'a')
configFile = 'Include /usr/local/lsws/conf/vhosts/' + domain + '/vhost.conf\n'
writeDataToFile.writelines(configFile)
writeDataToFile.close()
return [1, "None"]
2019-12-10 15:09:10 +05:00
except BaseException as msg:
2018-11-09 22:01:28 +05:00
logging.CyberCPLogFileWriter.writeToFile(
str(msg) + "223 [IO Error with main config file [createConfigInMainDomainHostFile]]")
return [0, "223 [IO Error with main config file [createConfigInMainDomainHostFile]]"]