From 9b5ec7d12fc75f90431535ee6c0a0633d1ac8978 Mon Sep 17 00:00:00 2001 From: usmannasir <01-134132-158@student.bahria.edu.pk> Date: Sun, 5 Nov 2017 03:02:51 +0500 Subject: [PATCH] Run CyberPanel as user cyberpanel --- api/urls.py | 1 + api/views.py | 89 +- .../templates/backup/backupDestinations.html | 6 +- backup/views.py | 173 +- .../themes/frontend/color-schemes/default.css | 81 +- .../templates/baseTemplate/index.html | 2 +- databases/views.py | 2 +- filemanager_app/filemanager.py | 8 +- firewall/views.py | 105 +- install/firewallUtilities.py | 8 +- install/gun-configs/gunicorn.service | 4 +- install/install.py | 155 +- install/installCyberPanel.py | 69 +- install/installLog.py | 11 - locale/ja/LC_MESSAGES/django.mo | Bin 0 -> 48363 bytes locale/ja/LC_MESSAGES/django.po | 2832 +++++++++++++++++ loginSystem/templates/loginSystem/login.html | 4 +- mailServer/views.py | 20 +- manageSSL/views.py | 4 +- plogical/backupSchedule.py | 3 +- plogical/backupUtilities.py | 49 +- plogical/firewallUtilities.py | 8 +- plogical/ftpUtilities.py | 23 +- plogical/installUtilities.py | 34 +- plogical/mysqlUtilities.py | 6 +- plogical/phpUtilities.py | 4 +- plogical/remoteBackup.py | 17 +- plogical/sslUtilities.py | 25 +- plogical/test.py | 71 +- plogical/virtualHostUtilities.py | 97 +- websiteFunctions/views.py | 169 +- 31 files changed, 3775 insertions(+), 305 deletions(-) create mode 100644 locale/ja/LC_MESSAGES/django.mo create mode 100644 locale/ja/LC_MESSAGES/django.po diff --git a/api/urls.py b/api/urls.py index 52c9485ed..ddee0c3e2 100644 --- a/api/urls.py +++ b/api/urls.py @@ -21,5 +21,6 @@ urlpatterns = [ url(r'^cyberPanelVersion', views.cyberPanelVersion, name='cyberPanelVersion'), + url(r'^putSSHkey', views.putSSHkey, name='putSSHkey'), ] \ No newline at end of file diff --git a/api/views.py b/api/views.py index 2bb979083..132da5e02 100644 --- a/api/views.py +++ b/api/views.py @@ -21,6 +21,8 @@ import signal from plogical.CyberCPLogFileWriter import CyberCPLogFileWriter as logging from shutil import rmtree from baseTemplate.models import version +import subprocess +import shlex # Create your views here. @@ -336,10 +338,25 @@ def fetchSSHkey(request): admin = Administrator.objects.get(userName=username) if hashPassword.check_password(admin.password, password): - pubKey = "/root/.ssh/cyberpanel.pub" + keyPath = "/home/cyberpanel/.ssh" + + if not os.path.exists(keyPath): + os.makedirs(keyPath) + command = "ssh-keygen -f " + keyPath + "/cyberpanel -t rsa -N ''" + cmd = shlex.split(command) + res = subprocess.call(cmd) + else: + if not os.path.exists(keyPath+"/cyberpanel"): + command = "ssh-keygen -f " + keyPath + "/cyberpanel -t rsa -N ''" + cmd = shlex.split(command) + res = subprocess.call(cmd) + + pubKey = keyPath + "/cyberpanel.pub" f = open(pubKey) data = f.read() + + data_ret = {'pubKeyStatus': 1, 'error_message': "None", "pubKey":data} json_data = json.dumps(data_ret) return HttpResponse(json_data) @@ -367,6 +384,8 @@ def remoteTransfer(request): admin = Administrator.objects.get(userName=username) if hashPassword.check_password(admin.password, password): dir = str(randint(1000, 9999)) + + transferRequest = rBackup.remoteBackup.remoteTransfer(ipAddress, dir,accountsToTransfer) if transferRequest[0] == 1: @@ -538,3 +557,71 @@ def cyberPanelVersion(request): json_data = json.dumps(data_ret) return HttpResponse(json_data) +def putSSHkey(request): + try: + if request.method == 'POST': + + data = json.loads(request.body) + + adminUser = data['username'] + adminPass = data['password'] + pubKey = data['putSSHKey'] + + + admin = Administrator.objects.get(userName=adminUser) + + if hashPassword.check_password(admin.password, adminPass): + keyPath = "/home/cyberpanel/.ssh" + + if not os.path.exists(keyPath): + os.makedirs(keyPath) + + + ## writeKey + + authorized_keys = keyPath+"/authorized_keys" + presenseCheck = 0 + try: + data = open(authorized_keys, "r").readlines() + for items in data: + if items.find(pubKey) > -1: + presenseCheck = 1 + except: + pass + + if presenseCheck == 0: + writeToFile = open(authorized_keys, 'a') + writeToFile.writelines("#Added by CyberPanel\n") + writeToFile.writelines("\n") + writeToFile.writelines(pubKey) + writeToFile.writelines("\n") + writeToFile.close() + + ## + + command = "sudo chmod g-w /home/cyberpanel" + cmd = shlex.split(command) + res = subprocess.call(cmd) + + os.chmod(keyPath,0700) + os.chmod(authorized_keys, 0600) + + + data_ret = {"putSSHKey": 1, + 'error_message': "None",} + + json_data = json.dumps(data_ret) + return HttpResponse(json_data) + else: + data_ret = {"putSSHKey": 0, + 'error_message': "Could not authorize access to API"} + json_data = json.dumps(data_ret) + return HttpResponse(json_data) + + except BaseException, msg: + data_ret = {"putSSHKey": 0, + 'error_message': str(msg)} + json_data = json.dumps(data_ret) + return HttpResponse(json_data) + + diff --git a/backup/templates/backup/backupDestinations.html b/backup/templates/backup/backupDestinations.html index ae8dc8162..dfe398c79 100644 --- a/backup/templates/backup/backupDestinations.html +++ b/backup/templates/backup/backupDestinations.html @@ -18,7 +18,7 @@
+ {% trans "Set up Back up Destinations (SSH port should be 22 on backup server)" %}
{% trans "Connection to" %} {$ IPAddress $} {% trans "failed. Please delete and re-add." %}
+{% trans "Connection to" %} {$ IPAddress $} {% trans "failed. Please delete and re-add. " %} {$ errorMessage $}