Files
CyberPanel/plogical/virtualHostUtilities.py

1209 lines
45 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
2017-10-24 19:16:36 +05:00
import os.path
2018-06-01 02:08:21 +05:00
import sys
import django
sys.path.append('/usr/local/CyberCP')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "CyberCP.settings")
django.setup()
2017-10-24 19:16:36 +05:00
import shutil
2017-12-09 22:30:10 +05:00
import argparse
import installUtilities
import sslUtilities
from os.path import join
from os import listdir, rmdir
from shutil import move
2017-12-14 06:56:27 -05:00
import randomPassword as randomPassword
2018-05-24 23:37:10 +05:00
from multiprocessing import Process
2018-06-01 02:08:21 +05:00
from websiteFunctions.models import Websites, ChildDomains
from loginSystem.models import Administrator
from packages.models import Package
import subprocess
import shlex
from plogical.mailUtilities import mailUtilities
import CyberCPLogFileWriter as logging
from dnsUtilities import DNS
from vhost import vhost
2017-10-24 19:16:36 +05:00
2018-05-08 21:25:37 +05:00
## If you want justice, you have come to the wrong place.
2017-10-24 19:16:36 +05:00
class virtualHostUtilities:
Server_root = "/usr/local/lsws"
2017-12-09 22:30:10 +05:00
cyberPanel = "/usr/local/CyberCP"
2017-10-24 19:16:36 +05:00
@staticmethod
2018-06-01 02:08:21 +05:00
def createVirtualHost(virtualHostName, administratorEmail, phpVersion, virtualHostUser, numberOfSites, ssl, sslPath,
dkimCheck, openBasedir, websiteOwner, packageName):
2018-05-12 02:21:42 +05:00
try:
2017-10-24 19:16:36 +05:00
2018-06-01 02:08:21 +05:00
if Websites.objects.filter(domain=virtualHostName).count() > 0:
raise BaseException("This website already exists.")
2017-10-24 19:16:36 +05:00
2018-06-01 02:08:21 +05:00
if ChildDomains.objects.filter(domain=virtualHostName).count() > 0:
raise BaseException("This website already exists as child domain.")
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
####### Limitations Check End
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
##### Zone creation
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
admin = Administrator.objects.get(userName=websiteOwner)
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
DNS.dnsTemplate(virtualHostName, admin)
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
## zone creation
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
if vhost.checkIfVirtualHostExists(virtualHostName) == 1:
raise BaseException("Virtual Host Directory already exists!")
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
if vhost.checkIfAliasExists(virtualHostName) == 1:
raise BaseException("This domain exists as Alias.")
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
if dkimCheck == 1:
if mailUtilities.checkIfDKIMInstalled() == 0:
raise BaseException("OpenDKIM is not installed, install OpenDKIM from DKIM Manager.")
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
retValues = mailUtilities.setupDKIM(virtualHostName)
if retValues[0] == 0:
raise BaseException(retValues[1])
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
retValues = vhost.createDirectoryForVirtualHost(virtualHostName, administratorEmail,
virtualHostUser, phpVersion, openBasedir)
if retValues[0] == 0:
raise BaseException(retValues[1])
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
retValues = vhost.createConfigInMainVirtualHostFile(virtualHostName)
if retValues[0] == 0:
raise BaseException(retValues[1])
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
if ssl == 1:
installUtilities.installUtilities.reStartLiteSpeed()
retValues = sslUtilities.issueSSLForDomain(virtualHostName, administratorEmail, sslPath)
if retValues[0] == 0:
raise BaseException(retValues[1])
else:
installUtilities.installUtilities.reStartLiteSpeed()
2017-11-05 03:02:51 +05:00
2018-06-01 02:08:21 +05:00
if ssl == 0:
installUtilities.installUtilities.reStartLiteSpeed()
2017-12-09 22:30:10 +05:00
2018-06-05 00:53:45 +05:00
vhost.finalizeVhostCreation(virtualHostName, virtualHostUser)
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
## Create Configurations ends here
2017-11-05 03:02:51 +05:00
2018-06-01 02:08:21 +05:00
## DKIM Check
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
if dkimCheck == 1:
DNS.createDKIMRecords(virtualHostName)
2018-05-12 02:21:42 +05:00
2018-06-01 02:08:21 +05:00
selectedPackage = Package.objects.get(packageName=packageName)
2018-05-12 02:21:42 +05:00
2018-06-01 02:08:21 +05:00
website = Websites(admin=admin, package=selectedPackage, domain=virtualHostName,
adminEmail=administratorEmail,
phpSelection=phpVersion, ssl=ssl, externalApp=virtualHostUser)
2018-05-12 02:21:42 +05:00
2018-06-01 02:08:21 +05:00
website.save()
2018-05-12 02:21:42 +05:00
2018-06-01 02:08:21 +05:00
print "1,None"
return 1, 'None'
2017-12-09 22:30:10 +05:00
2018-05-12 02:21:42 +05:00
except BaseException, msg:
2018-06-01 02:08:21 +05:00
vhost.deleteVirtualHostConfigurations(virtualHostName, numberOfSites)
logging.CyberCPLogFileWriter.writeToFile(str(msg) + " [createVirtualHost]")
print "0," + str(msg)
return 0, str(msg)
2017-11-05 03:02:51 +05:00
2018-05-12 02:21:42 +05:00
@staticmethod
2018-06-01 02:08:21 +05:00
def issueSSL(virtualHost, path, adminEmail):
2017-11-05 03:02:51 +05:00
try:
2017-12-09 22:30:10 +05:00
2018-05-12 02:21:42 +05:00
FNULL = open(os.devnull, 'w')
2018-06-01 02:08:21 +05:00
retValues = sslUtilities.issueSSLForDomain(virtualHost, adminEmail, path)
2018-05-12 02:21:42 +05:00
2018-06-01 02:08:21 +05:00
if retValues[0] == 0:
print "0," + str(retValues[1])
return
2018-05-12 02:21:42 +05:00
2018-06-01 02:08:21 +05:00
installUtilities.installUtilities.reStartLiteSpeed()
2018-05-12 02:21:42 +05:00
vhostPath = virtualHostUtilities.Server_root + "/conf/vhosts"
command = "chown -R " + "lsadm" + ":" + "lsadm" + " " + vhostPath
cmd = shlex.split(command)
subprocess.call(cmd, stdout=FNULL, stderr=subprocess.STDOUT)
2018-06-01 02:08:21 +05:00
print "1,None"
2018-06-05 00:53:45 +05:00
return 1, None
2017-12-09 22:30:10 +05:00
except BaseException, msg:
logging.CyberCPLogFileWriter.writeToFile(
2018-06-01 02:08:21 +05:00
str(msg) + " [issueSSL]")
print "0," + str(msg)
2018-06-05 00:53:45 +05:00
return 0, str(msg)
2017-10-24 19:16:36 +05:00
@staticmethod
2018-06-01 02:08:21 +05:00
def getAccessLogs(fileName, page):
2017-10-24 19:16:36 +05:00
try:
2018-06-01 02:08:21 +05:00
numberOfTotalLines = int(subprocess.check_output(["wc", "-l", fileName]).split(" ")[0])
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
if numberOfTotalLines < 25:
data = subprocess.check_output(["cat", fileName])
else:
if page == 1:
end = numberOfTotalLines
start = end - 24
if start <= 0:
start = 1
startingAndEnding = "'" + str(start) + "," + str(end) + "p'"
command = "sed -n " + startingAndEnding + " " + fileName
proc = subprocess.Popen(shlex.split(command), stdout=subprocess.PIPE)
data = proc.stdout.read()
2017-10-24 19:16:36 +05:00
else:
2018-06-01 02:08:21 +05:00
end = numberOfTotalLines - ((page - 1) * 25)
start = end - 24
if start <= 0:
start = 1
startingAndEnding = "'" + str(start) + "," + str(end) + "p'"
command = "sed -n " + startingAndEnding + " " + fileName
proc = subprocess.Popen(shlex.split(command), stdout=subprocess.PIPE)
data = proc.stdout.read()
print data
2017-10-24 19:16:36 +05:00
except BaseException, msg:
logging.CyberCPLogFileWriter.writeToFile(
2018-06-01 02:08:21 +05:00
str(msg) + " [getAccessLogs]")
print "1,None"
2017-10-24 19:16:36 +05:00
@staticmethod
2018-06-01 02:08:21 +05:00
def getErrorLogs(fileName, page):
2017-10-24 19:16:36 +05:00
try:
2018-06-01 02:08:21 +05:00
numberOfTotalLines = int(subprocess.check_output(["wc", "-l", fileName]).split(" ")[0])
2017-10-24 19:16:36 +05:00
2018-06-01 02:08:21 +05:00
if numberOfTotalLines < 25:
data = subprocess.check_output(["cat", fileName])
2018-05-24 23:37:10 +05:00
else:
2018-06-01 02:08:21 +05:00
if page == 1:
end = numberOfTotalLines
start = end - 24
if start <= 0:
start = 1
startingAndEnding = "'" + str(start) + "," + str(end) + "p'"
command = "sed -n " + startingAndEnding + " " + fileName
proc = subprocess.Popen(shlex.split(command), stdout=subprocess.PIPE)
data = proc.stdout.read()
else:
end = numberOfTotalLines - ((page - 1) * 25)
start = end - 24
if start <= 0:
start = 1
startingAndEnding = "'" + str(start) + "," + str(end) + "p'"
command = "sed -n " + startingAndEnding + " " + fileName
proc = subprocess.Popen(shlex.split(command), stdout=subprocess.PIPE)
data = proc.stdout.read()
print data
2017-10-24 19:16:36 +05:00
except BaseException, msg:
logging.CyberCPLogFileWriter.writeToFile(
2018-06-01 02:08:21 +05:00
str(msg) + " [getErrorLogs]")
print "1,None"
2017-10-24 19:16:36 +05:00
@staticmethod
2018-06-01 02:08:21 +05:00
def saveVHostConfigs(fileName, tempPath):
2017-10-24 19:16:36 +05:00
try:
2018-06-01 02:08:21 +05:00
vhost = open(fileName, "w")
2017-10-24 19:16:36 +05:00
2018-06-01 02:08:21 +05:00
vhost.write(open(tempPath, "r").read())
2017-10-24 19:16:36 +05:00
2018-06-01 02:08:21 +05:00
vhost.close()
2017-10-24 19:16:36 +05:00
2018-06-01 02:08:21 +05:00
if os.path.exists(tempPath):
os.remove(tempPath)
2017-10-24 19:16:36 +05:00
2018-06-01 02:08:21 +05:00
installUtilities.installUtilities.reStartLiteSpeed()
2017-12-22 12:36:24 +05:00
2018-06-01 02:08:21 +05:00
print "1,None"
2017-10-24 19:16:36 +05:00
except BaseException, msg:
logging.CyberCPLogFileWriter.writeToFile(
2018-06-01 02:08:21 +05:00
str(msg) + " [saveVHostConfigs]")
print "0," + str(msg)
2018-01-02 11:49:03 +05:00
@staticmethod
2018-06-01 02:08:21 +05:00
def saveRewriteRules(virtualHost, fileName, tempPath):
2018-01-02 11:49:03 +05:00
try:
2018-06-01 02:08:21 +05:00
vhost.addRewriteRules(virtualHost, fileName)
2018-01-02 11:49:03 +05:00
2018-06-01 02:08:21 +05:00
vhostFile = open(fileName, "w")
vhostFile.write(open(tempPath, "r").read())
vhostFile.close()
2018-05-08 21:25:37 +05:00
2018-06-01 02:08:21 +05:00
if os.path.exists(tempPath):
os.remove(tempPath)
2018-05-08 21:25:37 +05:00
installUtilities.installUtilities.reStartLiteSpeed()
2018-06-01 02:08:21 +05:00
print "1,None"
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
except BaseException, msg:
logging.CyberCPLogFileWriter.writeToFile(
str(msg) + " [saveRewriteRules]")
print "0," + str(msg)
2018-05-01 00:49:47 +05:00
2018-06-01 02:08:21 +05:00
@staticmethod
def installWordPress(domainName, finalPath, virtualHostUser, dbName, dbUser, dbPassword):
try:
2018-05-14 22:26:25 +05:00
2018-06-01 02:08:21 +05:00
FNULL = open(os.devnull, 'w')
2018-05-01 00:49:47 +05:00
2018-06-01 02:08:21 +05:00
if not os.path.exists(finalPath):
os.makedirs(finalPath)
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
## checking for directories/files
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
dirFiles = os.listdir(finalPath)
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
if len(dirFiles) == 1:
if dirFiles[0] == ".well-known":
pass
else:
print "0,Target directory should be empty before installation, otherwise data loss could occur."
return
elif len(dirFiles) == 0:
pass
else:
print "0,Target directory should be empty before installation, otherwise data loss could occur."
return
2018-05-03 01:22:28 +05:00
2018-06-01 02:08:21 +05:00
## Get wordpress
2018-05-09 23:10:57 +05:00
2018-06-01 02:08:21 +05:00
if not os.path.exists("latest.tar.gz"):
command = 'wget --no-check-certificate http://wordpress.org/latest.tar.gz -O latest.tar.gz'
cmd = shlex.split(command)
res = subprocess.call(cmd, stdout=FNULL, stderr=subprocess.STDOUT)
2018-05-03 01:22:28 +05:00
2018-06-01 02:08:21 +05:00
command = 'tar -xzvf latest.tar.gz -C ' + finalPath
2018-05-03 01:22:28 +05:00
2018-06-01 02:08:21 +05:00
cmd = shlex.split(command)
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
res = subprocess.call(cmd, stdout=FNULL, stderr=subprocess.STDOUT)
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
## Get plugin
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
if not os.path.exists("litespeed-cache.1.1.5.1.zip"):
command = 'wget --no-check-certificate https://downloads.wordpress.org/plugin/litespeed-cache.1.1.5.1.zip'
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
cmd = shlex.split(command)
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
res = subprocess.call(cmd, stdout=FNULL, stderr=subprocess.STDOUT)
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
command = 'unzip litespeed-cache.1.1.5.1.zip -d ' + finalPath
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
cmd = shlex.split(command)
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
res = subprocess.call(cmd, stdout=FNULL, stderr=subprocess.STDOUT)
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
root = finalPath
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
for filename in listdir(join(root, 'wordpress')):
move(join(root, 'wordpress', filename), join(root, filename))
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
rmdir(root + "wordpress")
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
shutil.copytree(finalPath + "litespeed-cache", finalPath + "wp-content/plugins/litespeed-cache")
shutil.rmtree(finalPath + "litespeed-cache")
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
## edit config file
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
wpconfigfile = finalPath + "wp-config-sample.php"
2017-11-05 03:02:51 +05:00
2018-06-01 02:08:21 +05:00
data = open(wpconfigfile, "r").readlines()
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
writeDataToFile = open(wpconfigfile, "w")
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
defDBName = "define('DB_NAME', '" + dbName + "');" + "\n"
defDBUser = "define('DB_USER', '" + dbUser + "');" + "\n"
defDBPassword = "define('DB_PASSWORD', '" + dbPassword + "');" + "\n"
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
for items in data:
if items.find("DB_NAME") > -1:
if items.find("database_name_here") > -1:
writeDataToFile.writelines(defDBName)
elif items.find("DB_USER") > -1:
if items.find("username_here") > -1:
writeDataToFile.writelines(defDBUser)
elif items.find("DB_PASSWORD") > -1:
writeDataToFile.writelines(defDBPassword)
else:
writeDataToFile.writelines(items)
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
writeDataToFile.close()
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
os.rename(wpconfigfile, finalPath + 'wp-config.php')
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
command = "chown -R " + virtualHostUser + ":" + virtualHostUser + " " + "/home/" + domainName + "/public_html/"
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
cmd = shlex.split(command)
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
res = subprocess.call(cmd, stdout=FNULL, stderr=subprocess.STDOUT)
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
vhost.addRewriteRules(domainName)
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
installUtilities.installUtilities.reStartLiteSpeed()
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
print "1,None"
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
except BaseException, msg:
# remove the downloaded files
try:
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
shutil.rmtree(finalPath)
except:
logging.CyberCPLogFileWriter.writeToFile("shutil.rmtree(finalPath)")
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
homeDir = "/home/" + domainName + "/public_html"
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
if not os.path.exists(homeDir):
FNULL = open(os.devnull, 'w')
os.mkdir(homeDir)
command = "chown -R " + virtualHostUser + ":" + virtualHostUser + " " + homeDir
cmd = shlex.split(command)
res = subprocess.call(cmd, stdout=FNULL, stderr=subprocess.STDOUT)
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
print "0," + str(msg)
return
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
@staticmethod
def installJoomla(domainName, finalPath, virtualHostUser, dbName, dbUser, dbPassword, username, password, prefix,
sitename):
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
try:
FNULL = open(os.devnull, 'w')
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
if not os.path.exists(finalPath):
os.makedirs(finalPath)
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
## checking for directories/files
2018-06-01 02:08:21 +05:00
dirFiles = os.listdir(finalPath)
2018-06-01 02:08:21 +05:00
if len(dirFiles) == 1:
if dirFiles[0] == ".well-known":
pass
else:
print "0,Target directory should be empty before installation, otherwise data loss could occur."
return
elif len(dirFiles) == 0:
pass
else:
print "0,Target directory should be empty before installation, otherwise data loss could occur."
return
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
## Get Joomla
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
os.chdir(finalPath)
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
if not os.path.exists("staging.zip"):
command = 'wget --no-check-certificate https://github.com/joomla/joomla-cms/archive/staging.zip -P ' + finalPath
cmd = shlex.split(command)
res = subprocess.call(cmd, stdout=FNULL, stderr=subprocess.STDOUT)
else:
print "0,File already exists"
return
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
command = 'unzip ' + finalPath + 'staging.zip -d ' + finalPath
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
cmd = shlex.split(command)
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
res = subprocess.call(cmd, stdout=FNULL, stderr=subprocess.STDOUT)
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
os.remove(finalPath + 'staging.zip')
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
command = 'cp -r ' + finalPath + 'joomla-cms-staging/. ' + finalPath
2017-12-09 22:30:10 +05:00
cmd = shlex.split(command)
2018-06-01 02:08:21 +05:00
res = subprocess.call(cmd, stdout=FNULL, stderr=subprocess.STDOUT)
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
shutil.rmtree(finalPath + "joomla-cms-staging")
os.rename(finalPath + "installation/configuration.php-dist", finalPath + "configuration.php")
os.rename(finalPath + "robots.txt.dist", finalPath + "robots.txt")
os.rename(finalPath + "htaccess.txt", finalPath + ".htaccess")
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
## edit config file
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
configfile = finalPath + "configuration.php"
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
data = open(configfile, "r").readlines()
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
writeDataToFile = open(configfile, "w")
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
secret = randomPassword.generate_pass()
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
defDBName = " public $user = '" + dbName + "';" + "\n"
defDBUser = " public $db = '" + dbUser + "';" + "\n"
defDBPassword = " public $password = '" + dbPassword + "';" + "\n"
secretKey = " public $secret = '" + secret + "';" + "\n"
logPath = " public $log_path = '" + finalPath + "administrator/logs';" + "\n"
tmpPath = " public $tmp_path = '" + finalPath + "administrator/tmp';" + "\n"
dbprefix = " public $dbprefix = '" + prefix + "';" + "\n"
sitename = " public $sitename = '" + sitename + "';" + "\n"
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
for items in data:
if items.find("public $user ") > -1:
writeDataToFile.writelines(defDBUser)
elif items.find("public $password ") > -1:
writeDataToFile.writelines(defDBPassword)
elif items.find("public $db ") > -1:
writeDataToFile.writelines(defDBName)
elif items.find("public $log_path ") > -1:
writeDataToFile.writelines(logPath)
elif items.find("public $tmp_path ") > -1:
writeDataToFile.writelines(tmpPath)
elif items.find("public $secret ") > -1:
writeDataToFile.writelines(secretKey)
elif items.find("public $dbprefix ") > -1:
writeDataToFile.writelines(dbprefix)
elif items.find("public $sitename ") > -1:
writeDataToFile.writelines(sitename)
elif items.find("/*") > -1:
pass
elif items.find(" *") > -1:
pass
else:
writeDataToFile.writelines(items)
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
writeDataToFile.close()
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
# Rename SQL db prefix
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
f1 = open(finalPath + 'installation/sql/mysql/joomla.sql', 'r')
f2 = open('installation/sql/mysql/joomlaInstall.sql', 'w')
for line in f1:
f2.write(line.replace('#__', prefix))
f1.close()
f2.close()
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
# Restore SQL
proc = subprocess.Popen(["mysql", "--user=%s" % dbUser, "--password=%s" % dbPassword, dbName],
stdin=subprocess.PIPE, stdout=subprocess.PIPE)
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
usercreation = """INSERT INTO `%susers`
(`name`, `username`, `password`, `params`)
VALUES ('Administrator', '%s',
'%s', '');
INSERT INTO `%suser_usergroup_map` (`user_id`,`group_id`)
VALUES (LAST_INSERT_ID(),'8');""" % (prefix, username, password, prefix)
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
out, err = proc.communicate(
file(finalPath + 'installation/sql/mysql/joomlaInstall.sql').read() + "\n" + usercreation)
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
shutil.rmtree(finalPath + "installation")
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
htaccessCache = """
<IfModule LiteSpeed>
RewriteEngine on
CacheLookup on
CacheDisable public /
RewriteCond %{REQUEST_METHOD} ^HEAD|GET$
RewriteCond %{ORG_REQ_URI} !/administrator
RewriteRule .* - [E=cache-control:max-age=120]
</IfModule>
"""
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
f = open(finalPath + '.htaccess', "a+")
f.write(htaccessCache)
f.close()
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
command = "chown -R " + virtualHostUser + ":" + virtualHostUser + " " + "/home/" + domainName + "/public_html/"
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
cmd = shlex.split(command)
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
res = subprocess.call(cmd, stdout=FNULL, stderr=subprocess.STDOUT)
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
vhost.addRewriteRules(domainName)
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
installUtilities.installUtilities.reStartLiteSpeed()
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
print "1,None"
return
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
except BaseException, msg:
# remove the downloaded files
try:
shutil.rmtree(finalPath)
except:
logging.CyberCPLogFileWriter.writeToFile("shutil.rmtree(finalPath)")
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
homeDir = "/home/" + domainName + "/public_html"
2018-06-01 02:08:21 +05:00
if not os.path.exists(homeDir):
FNULL = open(os.devnull, 'w')
os.mkdir(homeDir)
command = "chown -R " + virtualHostUser + ":" + virtualHostUser + " " + homeDir
cmd = shlex.split(command)
res = subprocess.call(cmd, stdout=FNULL, stderr=subprocess.STDOUT)
return
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
@staticmethod
def issueSSLForHostName(virtualHost, path):
try:
2017-12-09 22:30:10 +05:00
FNULL = open(os.devnull, 'w')
2018-06-01 02:08:21 +05:00
pathToStoreSSL = virtualHostUtilities.Server_root + "/conf/vhosts/" + "SSL-" + virtualHost
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
pathToStoreSSLPrivKey = pathToStoreSSL + "/privkey.pem"
pathToStoreSSLFullChain = pathToStoreSSL + "/fullchain.pem"
2017-12-14 06:56:27 -05:00
2018-06-01 02:08:21 +05:00
destPrivKey = "/usr/local/lscp/key.pem"
destCert = "/usr/local/lscp/cert.pem"
2017-12-14 06:56:27 -05:00
2018-06-01 02:08:21 +05:00
## removing old certs for lscpd
if os.path.exists(destPrivKey):
os.remove(destPrivKey)
if os.path.exists(destCert):
os.remove(destCert)
2018-06-01 02:08:21 +05:00
letsEncryptPath = "/etc/letsencrypt/live/" + virtualHost
2017-12-14 06:56:27 -05:00
2018-06-01 02:08:21 +05:00
if os.path.exists(letsEncryptPath) and os.path.exists(pathToStoreSSL):
pass
else:
2018-06-01 02:08:21 +05:00
adminEmail = "email@" + virtualHost
2017-12-14 06:56:27 -05:00
2018-06-01 02:08:21 +05:00
retValues = sslUtilities.issueSSLForDomain(virtualHost, adminEmail, path)
2017-12-14 06:56:27 -05:00
2018-06-01 02:08:21 +05:00
if retValues[0] == 0:
print "0," + str(retValues[1])
2018-06-05 00:53:45 +05:00
return 0,retValues[1]
2018-02-14 10:39:05 +05:00
2018-06-01 02:08:21 +05:00
shutil.copy(pathToStoreSSLPrivKey, destPrivKey)
shutil.copy(pathToStoreSSLFullChain, destCert)
2018-02-14 10:39:05 +05:00
2018-06-01 02:08:21 +05:00
command = 'systemctl restart lscpd'
2017-12-14 06:56:27 -05:00
cmd = shlex.split(command)
2018-06-01 02:08:21 +05:00
subprocess.call(cmd)
2017-12-14 06:56:27 -05:00
2018-06-01 02:08:21 +05:00
vhostPath = virtualHostUtilities.Server_root + "/conf/vhosts"
command = "chown -R " + "lsadm" + ":" + "lsadm" + " " + vhostPath
cmd = shlex.split(command)
subprocess.call(cmd, stdout=FNULL, stderr=subprocess.STDOUT)
2017-12-14 06:56:27 -05:00
2018-06-01 02:08:21 +05:00
print "1,None"
2018-06-05 00:53:45 +05:00
return 1,'None'
2017-12-14 06:56:27 -05:00
2018-06-01 02:08:21 +05:00
except BaseException, msg:
logging.CyberCPLogFileWriter.writeToFile(
str(msg) + " [issueSSLForHostName]")
print "0," + str(msg)
2018-06-05 00:53:45 +05:00
return 0, str(msg)
2017-12-14 06:56:27 -05:00
2018-06-01 02:08:21 +05:00
@staticmethod
def issueSSLForMailServer(virtualHost, path):
try:
2017-12-14 06:56:27 -05:00
2018-06-01 02:08:21 +05:00
FNULL = open(os.devnull, 'w')
2017-12-14 06:56:27 -05:00
2018-06-01 02:08:21 +05:00
pathToStoreSSL = virtualHostUtilities.Server_root + "/conf/vhosts/" + "SSL-" + virtualHost
2017-12-14 06:56:27 -05:00
2018-06-01 02:08:21 +05:00
srcPrivKey = pathToStoreSSL + "/privkey.pem"
srcFullChain = pathToStoreSSL + "/fullchain.pem"
2017-12-14 06:56:27 -05:00
2018-06-01 02:08:21 +05:00
letsEncryptPath = "/etc/letsencrypt/live/" + virtualHost
2017-12-14 06:56:27 -05:00
2018-06-01 02:08:21 +05:00
if os.path.exists(letsEncryptPath) and os.path.exists(pathToStoreSSL):
2017-12-14 06:56:27 -05:00
pass
else:
2018-06-01 02:08:21 +05:00
adminEmail = "email@" + virtualHost
2017-12-14 06:56:27 -05:00
2018-06-01 02:08:21 +05:00
retValues = sslUtilities.issueSSLForDomain(virtualHost, adminEmail, path)
2017-12-14 06:56:27 -05:00
2018-06-01 02:08:21 +05:00
if retValues[0] == 0:
print "0," + str(retValues[1])
2018-06-05 00:53:45 +05:00
return 0,retValues[1]
2017-12-14 06:56:27 -05:00
2018-06-01 02:08:21 +05:00
## MailServer specific functions
2017-12-14 06:56:27 -05:00
2018-06-01 02:08:21 +05:00
if os.path.exists("/etc/postfix/cert.pem"):
os.remove("/etc/postfix/cert.pem")
2017-12-14 06:56:27 -05:00
2018-06-01 02:08:21 +05:00
if os.path.exists("/etc/postfix/key.pem"):
os.remove("/etc/postfix/key.pem")
2017-12-14 06:56:27 -05:00
2018-06-01 02:08:21 +05:00
if os.path.exists("/etc/pki/dovecot/private/dovecot.pem"):
os.remove("/etc/pki/dovecot/private/dovecot.pem")
2017-12-14 06:56:27 -05:00
2018-06-01 02:08:21 +05:00
if os.path.exists("/etc/pki/dovecot/certs/dovecot.pem"):
os.remove("/etc/pki/dovecot/certs/dovecot.pem")
2017-12-14 06:56:27 -05:00
2018-06-01 02:08:21 +05:00
if os.path.exists("/etc/dovecot/key.pem"):
os.remove("/etc/dovecot/key.pem")
2017-12-14 06:56:27 -05:00
2018-06-01 02:08:21 +05:00
if os.path.exists("/etc/dovecot/cert.pem"):
os.remove("/etc/dovecot/cert.pem")
2017-12-14 06:56:27 -05:00
2018-06-01 02:08:21 +05:00
## Postfix
2017-12-14 06:56:27 -05:00
2018-06-01 02:08:21 +05:00
shutil.copy(srcPrivKey, "/etc/postfix/key.pem")
shutil.copy(srcFullChain, "/etc/postfix/cert.pem")
2017-12-14 06:56:27 -05:00
2018-06-01 02:08:21 +05:00
## Dovecot
2017-12-14 06:56:27 -05:00
2018-06-01 02:08:21 +05:00
shutil.copy(srcPrivKey, "/etc/pki/dovecot/private/dovecot.pem")
shutil.copy(srcFullChain, "/etc/pki/dovecot/certs/dovecot.pem")
2017-12-16 11:59:47 +05:00
2018-06-01 02:08:21 +05:00
## Dovecot 2ND
2017-12-14 06:56:27 -05:00
2018-06-01 02:08:21 +05:00
shutil.copy(srcPrivKey, "/etc/dovecot/key.pem")
shutil.copy(srcFullChain, "/etc/dovecot/cert.pem")
2017-12-14 06:56:27 -05:00
2018-06-01 02:08:21 +05:00
vhostPath = virtualHostUtilities.Server_root + "/conf/vhosts"
command = "chown -R " + "lsadm" + ":" + "lsadm" + " " + vhostPath
2017-12-14 06:56:27 -05:00
cmd = shlex.split(command)
2018-06-01 02:08:21 +05:00
subprocess.call(cmd, stdout=FNULL, stderr=subprocess.STDOUT)
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
## Update postmaster address dovecot
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
filePath = "/etc/dovecot/dovecot.conf"
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
data = open(filePath, 'r').readlines()
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
writeFile = open(filePath, 'w')
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
for items in data:
if items.find('postmaster_address') > -1:
writeFile.writelines(' postmaster_address = postmaster@' + virtualHost + '\n')
else:
writeFile.writelines(items)
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
writeFile.close()
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
## Update myhostname address postfix
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
filePath = "/etc/postfix/main.cf"
2018-06-01 02:08:21 +05:00
data = open(filePath, 'r').readlines()
2018-06-01 02:08:21 +05:00
writeFile = open(filePath, 'w')
2018-06-01 02:08:21 +05:00
for items in data:
if items.find('myhostname') > -1:
writeFile.writelines('myhostname = ' + virtualHost + '\n')
else:
writeFile.writelines(items)
2018-06-01 02:08:21 +05:00
writeFile.close()
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
p = Process(target=mailUtilities.restartServices, args=('restart',))
p.start()
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
print "1,None"
2018-06-05 00:53:45 +05:00
return 1,'None'
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
except BaseException, msg:
logging.CyberCPLogFileWriter.writeToFile(
str(msg) + " [issueSSLForHostName]")
print "0," + str(msg)
2018-06-05 00:53:45 +05:00
return 0,str(msg)
2018-04-26 17:59:47 +05:00
2018-06-01 02:08:21 +05:00
@staticmethod
def createAlias(masterDomain, aliasDomain, ssl, sslPath, administratorEmail, owner=None):
try:
if owner != None:
admin = Administrator.objects.get(userName=owner)
DNS.dnsTemplate(aliasDomain, owner)
2018-04-26 17:59:47 +05:00
2018-06-01 02:08:21 +05:00
if vhost.checkIfAliasExists(aliasDomain) == 0:
2018-04-26 17:59:47 +05:00
2018-06-01 02:08:21 +05:00
confPath = os.path.join(virtualHostUtilities.Server_root, "conf/httpd_config.conf")
data = open(confPath, 'r').readlines()
writeToFile = open(confPath, 'w')
listenerTrueCheck = 0
2018-04-26 17:59:47 +05:00
2018-06-01 02:08:21 +05:00
for items in data:
if items.find("listener") > -1 and items.find("Default") > -1:
listenerTrueCheck = 1
if items.find(masterDomain) > -1 and items.find('map') > -1 and listenerTrueCheck == 1:
data = filter(None, items.split(" "))
if data[1] == masterDomain:
writeToFile.writelines(items.rstrip('\n') + ", " + aliasDomain + "\n")
listenerTrueCheck = 0
else:
writeToFile.writelines(items)
2018-04-26 17:59:47 +05:00
2018-06-01 02:08:21 +05:00
writeToFile.close()
2018-06-01 02:08:21 +05:00
installUtilities.installUtilities.reStartLiteSpeed()
else:
print "0, This domain already exists as vHost or Alias."
return
2018-06-01 02:08:21 +05:00
if ssl == 1:
retValues = sslUtilities.issueSSLForDomain(masterDomain, administratorEmail, sslPath, aliasDomain)
if retValues[0] == 0:
print "0," + str(retValues[1])
return
else:
vhost.createAliasSSLMap(confPath, masterDomain, aliasDomain)
2018-06-01 02:08:21 +05:00
print "1,None"
2018-04-26 17:59:47 +05:00
2018-06-01 02:08:21 +05:00
except BaseException, msg:
2018-04-26 17:59:47 +05:00
2018-06-01 02:08:21 +05:00
logging.CyberCPLogFileWriter.writeToFile(str(msg) + " [createAlias]")
print "0," + str(msg)
2018-04-26 17:59:47 +05:00
2018-06-01 02:08:21 +05:00
@staticmethod
def issueAliasSSL(masterDomain, aliasDomain, sslPath, administratorEmail):
try:
2018-04-26 17:59:47 +05:00
2018-06-01 02:08:21 +05:00
confPath = os.path.join(virtualHostUtilities.Server_root, "conf/httpd_config.conf")
2018-04-26 17:59:47 +05:00
2018-06-01 02:08:21 +05:00
retValues = sslUtilities.issueSSLForDomain(masterDomain, administratorEmail, sslPath, aliasDomain)
2018-04-26 17:59:47 +05:00
2018-06-01 02:08:21 +05:00
if retValues[0] == 0:
print "0," + str(retValues[1])
return
else:
vhost.createAliasSSLMap(confPath, masterDomain, aliasDomain)
2018-04-26 17:59:47 +05:00
2018-06-01 02:08:21 +05:00
print "1,None"
2018-04-26 17:59:47 +05:00
2018-06-01 02:08:21 +05:00
except BaseException, msg:
2018-04-26 17:59:47 +05:00
2018-06-01 02:08:21 +05:00
logging.CyberCPLogFileWriter.writeToFile(str(msg) + " [issueAliasSSL]")
print "0," + str(msg)
2018-04-26 17:59:47 +05:00
2018-06-01 02:08:21 +05:00
@staticmethod
def deleteAlias(masterDomain, aliasDomain):
try:
2018-04-26 17:59:47 +05:00
2018-06-01 02:08:21 +05:00
confPath = os.path.join(virtualHostUtilities.Server_root, "conf/httpd_config.conf")
2018-04-26 17:59:47 +05:00
2018-06-01 02:08:21 +05:00
data = open(confPath, 'r').readlines()
writeToFile = open(confPath, 'w')
aliases = []
2018-04-26 17:59:47 +05:00
2018-06-01 02:08:21 +05:00
for items in data:
if items.find(masterDomain) > -1 and items.find('map') > -1:
data = filter(None, items.split(" "))
if data[1] == masterDomain:
length = len(data)
for i in range(3, length):
currentAlias = data[i].rstrip(',').strip('\n')
if currentAlias != aliasDomain:
aliases.append(currentAlias)
2018-04-26 17:59:47 +05:00
2018-06-01 02:08:21 +05:00
aliasString = ""
2018-04-26 17:59:47 +05:00
2018-06-01 02:08:21 +05:00
for alias in aliases:
aliasString = ", " + alias
2018-04-26 17:59:47 +05:00
2018-06-01 02:08:21 +05:00
writeToFile.writelines(
' map ' + masterDomain + " " + masterDomain + aliasString + "\n")
aliases = []
aliasString = ""
else:
writeToFile.writelines(items)
2018-04-26 17:59:47 +05:00
2018-06-01 02:08:21 +05:00
else:
writeToFile.writelines(items)
2018-04-26 17:59:47 +05:00
2018-06-01 02:08:21 +05:00
writeToFile.close()
installUtilities.installUtilities.reStartLiteSpeed()
2018-04-26 17:59:47 +05:00
2018-06-01 02:08:21 +05:00
print "1,None"
2018-04-26 17:59:47 +05:00
2018-06-01 02:08:21 +05:00
except BaseException, msg:
logging.CyberCPLogFileWriter.writeToFile(str(msg) + " [deleteAlias]")
print "0," + str(msg)
2018-04-26 17:59:47 +05:00
2018-06-01 02:08:21 +05:00
@staticmethod
def changeOpenBasedir(domainName, openBasedirValue):
try:
2018-04-26 17:59:47 +05:00
2018-06-01 02:08:21 +05:00
confPath = virtualHostUtilities.Server_root + "/conf/vhosts/" + domainName
completePathToConfigFile = confPath + "/vhost.conf"
2018-04-26 17:59:47 +05:00
2018-06-01 02:08:21 +05:00
data = open(completePathToConfigFile, 'r').readlines()
skip = 0
if openBasedirValue == 'Disable':
writeToFile = open(completePathToConfigFile, 'w')
for items in data:
if items.find('phpIniOverride') > -1:
skip = 1
2018-04-26 17:59:47 +05:00
2018-06-01 02:08:21 +05:00
if skip == 1 and items.find('}') > -1:
skip = 0
continue
2018-04-26 17:59:47 +05:00
2018-06-01 02:08:21 +05:00
if skip == 1:
continue
2018-04-26 17:59:47 +05:00
2018-06-01 02:08:21 +05:00
writeToFile.writelines(items)
writeToFile.close()
2018-04-26 17:59:47 +05:00
else:
2018-06-01 02:08:21 +05:00
## Check if phpini already active
2018-04-26 17:59:47 +05:00
2018-06-01 02:08:21 +05:00
inistatus = 0
for items in data:
if items.find('phpIniOverride') > -1:
inistatus = 1
2018-04-26 17:59:47 +05:00
2018-06-01 02:08:21 +05:00
if inistatus == 0:
writeToFile = open(completePathToConfigFile, 'w')
for items in data:
if items.find('context /.filemanager') > -1:
writeToFile.writelines(items)
phpIniOverride = "phpIniOverride {\n"
php_admin_value = 'php_admin_value open_basedir "/tmp:/usr/local/lsws/Example/html/FileManager:$VH_ROOT"\n'
endPHPIniOverride = "}\n"
writeToFile.writelines(phpIniOverride)
writeToFile.writelines(php_admin_value)
writeToFile.writelines(endPHPIniOverride)
else:
writeToFile.writelines(items)
2018-04-26 17:59:47 +05:00
2018-06-05 00:53:45 +05:00
phpIniOverride = "\nphpIniOverride {\n"
2018-06-01 02:08:21 +05:00
php_admin_value = 'php_admin_value open_basedir "/tmp:$VH_ROOT"\n'
endPHPIniOverride = "}\n"
2018-05-08 21:25:37 +05:00
2018-06-01 02:08:21 +05:00
writeToFile.writelines(phpIniOverride)
writeToFile.writelines(php_admin_value)
writeToFile.writelines(endPHPIniOverride)
2018-05-08 21:25:37 +05:00
2018-06-01 02:08:21 +05:00
writeToFile.close()
2018-05-08 21:25:37 +05:00
installUtilities.installUtilities.reStartLiteSpeed()
2018-06-01 02:08:21 +05:00
print "1,None"
2018-05-08 21:25:37 +05:00
2018-06-01 02:08:21 +05:00
except BaseException, msg:
logging.CyberCPLogFileWriter.writeToFile(str(msg) + " [changeOpenBasedir]")
print "0," + str(msg)
2018-05-08 21:25:37 +05:00
2018-06-01 02:08:21 +05:00
@staticmethod
def saveSSL(virtualHost, pathToStoreSSL, keyPath, certPath, sslCheck):
try:
2018-05-08 21:25:37 +05:00
2018-06-01 02:08:21 +05:00
if not os.path.exists(pathToStoreSSL):
os.mkdir(pathToStoreSSL)
2018-05-08 21:25:37 +05:00
2018-06-01 02:08:21 +05:00
pathToStoreSSLPrivKey = pathToStoreSSL + "/privkey.pem"
pathToStoreSSLFullChain = pathToStoreSSL + "/fullchain.pem"
2018-05-08 21:25:37 +05:00
2018-06-01 02:08:21 +05:00
privkey = open(pathToStoreSSLPrivKey, 'w')
privkey.write(open(keyPath, "r").read())
privkey.close()
2018-05-08 21:25:37 +05:00
2018-06-01 02:08:21 +05:00
fullchain = open(pathToStoreSSLFullChain, 'w')
fullchain.write(open(certPath, "r").read())
fullchain.close()
2018-05-08 21:25:37 +05:00
2018-06-01 02:08:21 +05:00
if sslCheck == "0":
sslUtilities.sslUtilities.installSSLForDomain(virtualHost)
2018-05-08 21:25:37 +05:00
2018-06-01 02:08:21 +05:00
installUtilities.installUtilities.reStartLiteSpeed()
2018-05-08 21:25:37 +05:00
2018-06-01 02:08:21 +05:00
FNULL = open(os.devnull, 'w')
2018-05-08 21:25:37 +05:00
2018-06-01 02:08:21 +05:00
command = "chown " + "lsadm" + ":" + "lsadm" + " " + pathToStoreSSL
cmd = shlex.split(command)
subprocess.call(cmd, stdout=FNULL, stderr=subprocess.STDOUT)
2018-05-08 21:25:37 +05:00
2018-06-01 02:08:21 +05:00
print "1,None"
2018-05-08 21:25:37 +05:00
2018-06-01 02:08:21 +05:00
except BaseException, msg:
logging.CyberCPLogFileWriter.writeToFile(
str(msg) + " [saveSSL]")
print "0," + str(msg)
2018-05-08 21:25:37 +05:00
2018-06-01 02:08:21 +05:00
@staticmethod
def createDomain(masterDomain, virtualHostName, phpVersion, path, ssl, dkimCheck, openBasedir, restore, owner=None):
try:
2018-05-08 21:25:37 +05:00
2018-06-01 02:08:21 +05:00
## Check if this domain either exists as website or child domain
2018-05-08 21:25:37 +05:00
2018-06-01 02:08:21 +05:00
if restore == '0':
admin = Administrator.objects.get(userName=owner)
DNS.dnsTemplate(virtualHostName, admin)
2018-05-08 21:25:37 +05:00
2018-06-01 02:08:21 +05:00
if Websites.objects.filter(domain=virtualHostName).count() > 0:
raise BaseException("This Domain already exists as a website.")
2018-05-08 21:25:37 +05:00
2018-06-01 02:08:21 +05:00
if ChildDomains.objects.filter(domain=virtualHostName).count() > 0:
raise BaseException("This domain already exists as child domain.")
2018-05-08 21:25:37 +05:00
2018-06-01 02:08:21 +05:00
####### Limitations check
2018-05-08 21:25:37 +05:00
2018-06-01 02:08:21 +05:00
master = Websites.objects.get(domain=masterDomain)
domainsInPackage = master.package.allowedDomains
2018-05-08 21:25:37 +05:00
2018-06-01 02:08:21 +05:00
if domainsInPackage == 0:
pass
elif domainsInPackage > master.childdomains_set.all().count():
pass
2018-05-08 21:25:37 +05:00
else:
2018-06-01 02:08:21 +05:00
raise BaseException("Exceeded maximum number of domains for this package")
2018-05-08 21:25:37 +05:00
2018-06-01 02:08:21 +05:00
####### Limitations Check End
2018-05-21 21:52:35 +05:00
2018-06-01 02:08:21 +05:00
if vhost.checkIfVirtualHostExists(virtualHostName) == 1:
raise BaseException("Virtual Host Directory already exists!")
2018-05-21 21:52:35 +05:00
2018-06-01 02:08:21 +05:00
if vhost.checkIfAliasExists(virtualHostName) == 1:
raise BaseException("This domain exists as Alias.")
2018-05-21 21:52:35 +05:00
2018-06-01 02:08:21 +05:00
if dkimCheck == 1:
if mailUtilities.checkIfDKIMInstalled() == 0:
raise BaseException("OpenDKIM is not installed, install OpenDKIM from DKIM Manager.")
2018-05-21 21:52:35 +05:00
2018-06-01 02:08:21 +05:00
retValues = mailUtilities.setupDKIM(virtualHostName)
if retValues[0] == 0:
raise BaseException(retValues[1])
2018-05-21 21:52:35 +05:00
2018-06-01 02:08:21 +05:00
FNULL = open(os.devnull, 'w')
2018-05-21 21:52:35 +05:00
2018-06-01 02:08:21 +05:00
retValues = vhost.createDirectoryForDomain(masterDomain, virtualHostName, phpVersion, path,
master.adminEmail, master.externalApp, openBasedir)
if retValues[0] == 0:
raise BaseException(retValues[1])
2018-05-21 21:52:35 +05:00
2018-06-01 02:08:21 +05:00
retValues = vhost.createConfigInMainDomainHostFile(virtualHostName, masterDomain)
2018-05-21 21:52:35 +05:00
2018-06-01 02:08:21 +05:00
if retValues[0] == 0:
raise BaseException(retValues[1])
2018-05-21 21:52:35 +05:00
2018-06-01 02:08:21 +05:00
## Now restart litespeed after initial configurations are done
2018-05-21 21:52:35 +05:00
2018-06-01 02:08:21 +05:00
if ssl == 1:
installUtilities.installUtilities.reStartLiteSpeed()
retValues = sslUtilities.issueSSLForDomain(virtualHostName, master.adminEmail, path)
installUtilities.installUtilities.reStartLiteSpeed()
if retValues[0] == 0:
raise BaseException(retValues[1])
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
## Final Restart
if ssl == 0:
installUtilities.installUtilities.reStartLiteSpeed()
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
vhost.finalizeDomainCreation(master.externalApp, path)
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
## DKIM Check
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
if restore == '0':
if dkimCheck == 1:
DNS.createDKIMRecords(virtualHostName)
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
website = ChildDomains(master=master, domain=virtualHostName, path=path, phpSelection=phpVersion, ssl=ssl)
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
website.save()
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
print "1,None"
return 1, "None"
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
except BaseException, msg:
numberOfWebsites = Websites.objects.count() + ChildDomains.objects.count()
2018-06-05 00:53:45 +05:00
vhost.deleteCoreConf(virtualHostName, numberOfWebsites)
2018-06-01 02:08:21 +05:00
logging.CyberCPLogFileWriter.writeToFile(
str(msg) + " [createDomain]")
print "0," + str(msg)
return 0, str(msg)
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
@staticmethod
def deleteDomain(virtualHostName):
try:
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
numberOfWebsites = Websites.objects.count() + ChildDomains.objects.count()
vhost.deleteCoreConf(virtualHostName, numberOfWebsites)
delWebsite = ChildDomains.objects.get(domain=virtualHostName)
delWebsite.delete()
installUtilities.installUtilities.reStartLiteSpeed()
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
print "1,None"
2018-06-05 00:53:45 +05:00
return 1,'None'
2017-12-09 22:30:10 +05:00
2018-06-01 02:08:21 +05:00
except BaseException, msg:
logging.CyberCPLogFileWriter.writeToFile(
str(msg) + " [deleteDomain]")
print "0," + str(msg)
2018-06-05 00:53:45 +05:00
return 0,str(msg)
@staticmethod
def getDiskUsage(path, totalAllowed):
try:
totalUsageInMB = subprocess.check_output(["sudo", "du", "-hs", path, "--block-size=1M"]).split()[0]
percentage = float(100) / float(totalAllowed)
percentage = float(percentage) * float(totalUsageInMB)
data = [int(totalUsageInMB), int(percentage)]
return data
except BaseException, msg:
logging.CyberCPLogFileWriter.writeToFile(str(msg) + " [getDiskUsage]")
return [int(0), int(0)]
2017-12-09 22:30:10 +05:00
2018-06-25 17:53:11 +05:00
@staticmethod
def permissionControl(path):
try:
command = 'sudo chown -R cyberpanel:cyberpanel ' + path
cmd = shlex.split(command)
res = subprocess.call(cmd)
except BaseException, msg:
logging.CyberCPLogFileWriter.writeToFile(str(msg))
@staticmethod
def leaveControl(path):
try:
command = 'sudo chown -R root:root ' + path
cmd = shlex.split(command)
res = subprocess.call(cmd)
except BaseException, msg:
logging.CyberCPLogFileWriter.writeToFile(str(msg))
2017-12-09 22:30:10 +05:00
def main():
parser = argparse.ArgumentParser(description='CyberPanel Installer')
parser.add_argument('function', help='Specific a function to call!')
parser.add_argument('--virtualHostName', help='Domain name!')
parser.add_argument('--administratorEmail', help='Administration Email!')
parser.add_argument('--phpVersion', help='PHP Version')
parser.add_argument("--virtualHostUser", help="Virtual Host Directory Owner and Group!")
parser.add_argument("--numberOfSites", help="Number of sites!")
parser.add_argument("--ssl", help="Weather to activate SSL")
parser.add_argument("--sslPath", help="Path to website document root!")
2018-05-01 00:49:47 +05:00
parser.add_argument('--dkimCheck', help='To enable or disable DKIM support for domain.')
2018-05-21 21:52:35 +05:00
parser.add_argument('--openBasedir', help='To enable or disable open_basedir protection for domain.')
2018-06-01 02:08:21 +05:00
parser.add_argument('--websiteOwner', help='Website Owner Name')
parser.add_argument('--package', help='Website package')
parser.add_argument('--restore', help='Restore Check.')
2017-12-09 22:30:10 +05:00
## arguments for creation child domains
parser.add_argument('--masterDomain', help='Master Domain Needed While Creating Child Domains!')
parser.add_argument('--path', help='Path Needed for Child domains Creation!')
parser.add_argument('--restart', help='OLS Restart Frequency while child domain creation!')
## arguments for logs
parser.add_argument('--page', help='Page number to fetch logs!')
## arguments for configuration files
parser.add_argument('--tempPath', help='Temporary path where configuration data is placed!')
## save ssl arguments
parser.add_argument('--tempKeyPath', help='Temporary path to store key!')
parser.add_argument('--tempCertPath', help='Temporary path to store cert!')
parser.add_argument('--sslCheck', help='Weather SSL is already activated or not!')
## install wordpress arguments
parser.add_argument('--dbName', help='Database Name!')
parser.add_argument('--dbUser', help='Database User!')
parser.add_argument('--dbPassword', help='Database Password!')
## calculate bw arguments
parser.add_argument('--bandwidth', help='Pack Bandwidth!')
2017-12-14 06:56:27 -05:00
## extras
parser.add_argument('--username', help='Admin Username!')
parser.add_argument('--password', help='Admin Password!')
parser.add_argument('--prefix', help='Database Prefix!')
parser.add_argument('--sitename', help='Site Name!')
2017-12-09 22:30:10 +05:00
2018-05-08 21:25:37 +05:00
## Arguments for alias domain
parser.add_argument('--aliasDomain', help='Alias Domain!')
2018-05-21 21:52:35 +05:00
## Arguments for OpenBasedir
parser.add_argument('--openBasedirValue', help='open_base dir protection value!')
2018-05-08 21:25:37 +05:00
2017-12-09 22:30:10 +05:00
args = parser.parse_args()
if args.function == "createVirtualHost":
2018-05-01 00:49:47 +05:00
try:
dkimCheck = int(args.dkimCheck)
except:
dkimCheck = 0
2018-05-21 21:52:35 +05:00
try:
openBasedir = int(args.openBasedir)
except:
openBasedir = 0
2018-06-01 02:08:21 +05:00
virtualHostUtilities.createVirtualHost(args.virtualHostName, args.administratorEmail, args.phpVersion, args.virtualHostUser, int(args.numberOfSites), int(args.ssl), args.sslPath, dkimCheck, openBasedir, args.websiteOwner, args.package)
2017-12-09 22:30:10 +05:00
elif args.function == "deleteVirtualHostConfigurations":
2018-06-01 02:08:21 +05:00
vhost.deleteVirtualHostConfigurations(args.virtualHostName,int(args.numberOfSites))
2017-12-09 22:30:10 +05:00
elif args.function == "createDomain":
2018-05-03 01:22:28 +05:00
try:
dkimCheck = int(args.dkimCheck)
except:
dkimCheck = 0
2018-05-21 21:52:35 +05:00
try:
openBasedir = int(args.openBasedir)
except:
openBasedir = 0
2018-06-01 02:08:21 +05:00
virtualHostUtilities.createDomain(args.masterDomain, args.virtualHostName, args.phpVersion, args.path, int(args.ssl), dkimCheck, openBasedir, args.restore, args.websiteOwner)
2017-12-09 22:30:10 +05:00
elif args.function == "issueSSL":
2018-06-01 02:08:21 +05:00
virtualHostUtilities.issueSSL(args.virtualHostName,args.path,args.administratorEmail)
2017-12-09 22:30:10 +05:00
elif args.function == "changePHP":
2018-06-01 02:08:21 +05:00
vhost.changePHP(args.path,args.phpVersion)
2017-12-09 22:30:10 +05:00
elif args.function == "getAccessLogs":
2018-06-01 02:08:21 +05:00
virtualHostUtilities.getAccessLogs(args.path,int(args.page))
2017-12-09 22:30:10 +05:00
elif args.function == "getErrorLogs":
2018-06-01 02:08:21 +05:00
virtualHostUtilities.getErrorLogs(args.path,int(args.page))
2017-12-09 22:30:10 +05:00
elif args.function == "saveVHostConfigs":
2018-06-01 02:08:21 +05:00
virtualHostUtilities.saveVHostConfigs(args.path,args.tempPath)
2017-12-09 22:30:10 +05:00
elif args.function == "saveRewriteRules":
2018-06-01 02:08:21 +05:00
virtualHostUtilities.saveRewriteRules(args.virtualHostName,args.path,args.tempPath)
2017-12-09 22:30:10 +05:00
elif args.function == "saveSSL":
2018-06-01 02:08:21 +05:00
virtualHostUtilities.saveSSL(args.virtualHostName,args.path,args.tempKeyPath,args.tempCertPath,args.sslCheck)
2017-12-09 22:30:10 +05:00
elif args.function == "installWordPress":
2018-06-01 02:08:21 +05:00
virtualHostUtilities.installWordPress(args.virtualHostName,args.path,args.virtualHostUser,args.dbName,args.dbUser,args.dbPassword)
2017-12-14 06:56:27 -05:00
elif args.function == "installJoomla":
2018-06-01 02:08:21 +05:00
virtualHostUtilities.installJoomla(args.virtualHostName,args.path,args.virtualHostUser,args.dbName,args.dbUser,args.dbPassword,args.username,args.password,args.prefix,args.sitename)
2017-12-09 22:30:10 +05:00
elif args.function == "issueSSLForHostName":
2018-06-01 02:08:21 +05:00
virtualHostUtilities.issueSSLForHostName(args.virtualHostName,args.path)
2018-04-26 17:59:47 +05:00
elif args.function == "issueSSLForMailServer":
2018-06-01 02:08:21 +05:00
virtualHostUtilities.issueSSLForMailServer(args.virtualHostName,args.path)
2017-12-09 22:30:10 +05:00
elif args.function == "findDomainBW":
2018-06-01 02:08:21 +05:00
vhost.findDomainBW(args.virtualHostName, int(args.bandwidth))
2018-05-08 21:25:37 +05:00
elif args.function == 'createAlias':
2018-06-01 02:08:21 +05:00
virtualHostUtilities.createAlias(args.masterDomain,args.aliasDomain,int(args.ssl),args.sslPath, args.administratorEmail, args.websiteOwner)
2018-05-08 21:25:37 +05:00
elif args.function == 'issueAliasSSL':
2018-06-01 02:08:21 +05:00
virtualHostUtilities.issueAliasSSL(args.masterDomain, args.aliasDomain, args.sslPath, args.administratorEmail)
2018-05-08 21:25:37 +05:00
elif args.function == 'deleteAlias':
2018-06-01 02:08:21 +05:00
virtualHostUtilities.deleteAlias(args.masterDomain, args.aliasDomain)
2018-05-21 21:52:35 +05:00
elif args.function == 'changeOpenBasedir':
2018-06-01 02:08:21 +05:00
virtualHostUtilities.changeOpenBasedir(args.virtualHostName, args.openBasedirValue)
elif args.function == 'deleteDomain':
virtualHostUtilities.deleteDomain(args.virtualHostName)
2017-12-09 22:30:10 +05:00
if __name__ == "__main__":
main()