Files
CyberPanel/plogical/ftpUtilities.py

240 lines
6.9 KiB
Python
Raw Normal View History

2019-12-10 23:04:24 +05:00
#!/usr/local/CyberCP/bin/python
2018-06-05 00:53:45 +05:00
import os,sys
sys.path.append('/usr/local/CyberCP')
import django
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "CyberCP.settings")
django.setup()
2019-12-12 08:46:07 +05:00
import mysqlUtilities as sql
2017-10-24 19:16:36 +05:00
import subprocess
2019-12-12 08:46:07 +05:00
import CyberCPLogFileWriter as logging
2017-10-24 19:16:36 +05:00
import os
2017-11-05 03:02:51 +05:00
import shlex
2017-12-09 22:30:10 +05:00
import argparse
2018-06-05 00:53:45 +05:00
from websiteFunctions.models import Websites, ChildDomains
from loginSystem.models import Administrator
import pwd
import grp
import hashlib
from ftp.models import Users
from datetime import datetime
from plogical.processUtilities import ProcessUtilities
2017-10-24 19:16:36 +05:00
class FTPUtilities:
@staticmethod
def createNewFTPAccount(udb,upass,username,password,path):
try:
cmd = []
cmd.append("chown")
cmd.append("-R")
cmd.append("ftpuser:2001")
cmd.append(path)
2019-03-26 16:19:03 +05:00
res = subprocess.call(cmd)
2017-10-24 19:16:36 +05:00
if res == 1:
2019-12-10 15:09:10 +05:00
print("Permissions not changed.")
2017-10-24 19:16:36 +05:00
else:
2019-12-10 15:09:10 +05:00
print("User permissions setted.")
2017-10-24 19:16:36 +05:00
query = "INSERT INTO ftp_ftpuser (userid,passwd,homedir) VALUES ('" + username + "'" +","+"'"+password+"'"+","+"'"+path+"'"+");"
2019-12-10 15:09:10 +05:00
print(query)
2017-10-24 19:16:36 +05:00
sql.mysqlUtilities.SendQuery(udb,upass, "ftp", query)
2019-12-10 15:09:10 +05:00
except BaseException as msg:
2017-10-24 19:16:36 +05:00
logging.CyberCPLogFileWriter.writeToFile(
str(msg) + " [createNewFTPAccount]")
return 0
return 1
@staticmethod
def changePermissions(directory):
try:
2017-11-05 03:02:51 +05:00
command = "sudo chmod -R 775 " + directory
cmd = shlex.split(command)
2017-10-24 19:16:36 +05:00
2019-03-26 16:19:03 +05:00
res = subprocess.call(cmd)
2017-11-05 03:02:51 +05:00
2017-10-24 19:16:36 +05:00
if res == 1:
2019-12-10 15:09:10 +05:00
print("Permissions not changed.")
2017-10-24 19:16:36 +05:00
return 0
else:
2019-12-10 15:09:10 +05:00
print("User permissions setted.")
2017-10-24 19:16:36 +05:00
2017-11-05 03:02:51 +05:00
2018-10-12 18:18:10 +05:00
command = "sudo chown -R lscpd:cyberpanel " + directory
2017-11-05 03:02:51 +05:00
cmd = shlex.split(command)
2017-10-24 19:16:36 +05:00
2019-03-26 16:19:03 +05:00
res = subprocess.call(cmd)
2017-11-05 03:02:51 +05:00
2017-10-24 19:16:36 +05:00
if res == 1:
return 0
else:
return 1
2019-12-10 15:09:10 +05:00
except BaseException as msg:
2017-10-24 19:16:36 +05:00
logging.CyberCPLogFileWriter.writeToFile(
str(msg) + " [createNewFTPAccount]")
return 0
return 1
2017-12-09 22:30:10 +05:00
@staticmethod
def ftpFunctions(path,externalApp):
try:
command = 'mkdir %s' % (path)
ProcessUtilities.executioner(command, externalApp)
2017-12-09 22:30:10 +05:00
2018-06-05 00:53:45 +05:00
return 1,'None'
2017-12-09 22:30:10 +05:00
2019-12-10 15:09:10 +05:00
except BaseException as msg:
2017-12-09 22:30:10 +05:00
logging.CyberCPLogFileWriter.writeToFile(
2018-06-05 00:53:45 +05:00
str(msg) + " [ftpFunctions]")
return 0, str(msg)
@staticmethod
2018-11-08 13:19:36 +05:00
def submitFTPCreation(domainName, userName, password, path, owner, api = None):
2018-06-05 00:53:45 +05:00
try:
## need to get gid and uid
try:
website = ChildDomains.objects.get(domain=domainName)
externalApp = website.master.externalApp
except:
website = Websites.objects.get(domain=domainName)
externalApp = website.externalApp
uid = pwd.getpwnam(externalApp).pw_uid
gid = grp.getgrnam(externalApp).gr_gid
## gid , uid ends
path = path.lstrip("/")
if path != 'None':
path = "/home/" + domainName + "/public_html/" + path
## Security Check
if path.find("..") > -1:
raise BaseException("Specified path must be inside virtual host home!")
result = FTPUtilities.ftpFunctions(path, externalApp)
if result[0] == 1:
pass
else:
raise BaseException(result[1])
else:
path = "/home/" + domainName
if os.path.islink(path):
2019-12-10 15:09:10 +05:00
print("0, %s file is symlinked." % (path))
return 0
2018-06-05 00:53:45 +05:00
hash = hashlib.md5()
hash.update(password)
admin = Administrator.objects.get(userName=owner)
2018-11-08 13:19:36 +05:00
if api == '0':
userName = admin.userName + "_" + userName
2018-06-05 00:53:45 +05:00
if website.package.ftpAccounts == 0:
user = Users(domain=website, user=userName, password=hash.hexdigest(), uid=uid, gid=gid,
dir=path,
quotasize=website.package.diskSpace,
status="1",
ulbandwidth=500000,
dlbandwidth=500000,
date=datetime.now())
user.save()
elif website.users_set.all().count() < website.package.ftpAccounts:
user = Users(domain=website, user=userName, password=hash.hexdigest(), uid=uid, gid=gid,
dir=path, quotasize=website.package.diskSpace,
status="1",
ulbandwidth=500000,
dlbandwidth=500000,
date=datetime.now())
user.save()
else:
raise BaseException("Exceeded maximum amount of FTP accounts allowed for the package.")
2019-12-10 15:09:10 +05:00
print("1,None")
2018-06-05 00:53:45 +05:00
return 1,'None'
2019-12-10 15:09:10 +05:00
except BaseException as msg:
2018-06-05 00:53:45 +05:00
logging.CyberCPLogFileWriter.writeToFile(str(msg) + " [submitFTPCreation]")
2019-12-10 15:09:10 +05:00
print("0,"+str(msg))
2018-06-05 00:53:45 +05:00
return 0, str(msg)
@staticmethod
def submitFTPDeletion(ftpUsername):
try:
ftp = Users.objects.get(user=ftpUsername)
ftp.delete()
return 1,'None'
2019-12-10 15:09:10 +05:00
except BaseException as msg:
2018-06-05 00:53:45 +05:00
return 0, str(msg)
@staticmethod
def changeFTPPassword(userName, password):
try:
hash = hashlib.md5()
hash.update(password)
ftp = Users.objects.get(user=userName)
ftp.password = hash.hexdigest()
ftp.save()
return 1, None
2019-12-10 15:09:10 +05:00
except BaseException as msg:
2018-06-05 00:53:45 +05:00
return 0,str(msg)
@staticmethod
def getFTPRecords(virtualHostName):
try:
website = Websites.objects.get(domain=virtualHostName)
return website.users_set.all()
except:
## There does not exist a zone for this domain.
pass
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!')
2018-06-05 00:53:45 +05:00
parser.add_argument('--domainName', help='Domain to create FTP for!')
parser.add_argument('--userName', help='Username for FTP Account')
parser.add_argument('--password', help='Password for FTP Account')
parser.add_argument('--owner', help='FTP Account owner.')
2017-12-09 22:30:10 +05:00
parser.add_argument('--path', help='Path to ftp directory!')
2018-11-08 13:19:36 +05:00
parser.add_argument('--api', help='API Check!')
2017-12-09 22:30:10 +05:00
args = parser.parse_args()
2018-06-05 00:53:45 +05:00
if args.function == "submitFTPCreation":
2018-11-08 13:19:36 +05:00
FTPUtilities.submitFTPCreation(args.domainName,args.userName, args.password, args.path, args.owner, args.api)
2017-12-09 22:30:10 +05:00
if __name__ == "__main__":
2018-11-08 13:19:36 +05:00
main()