From 3c455ffec1615617e9b977fbb781a5a1f4452f6b Mon Sep 17 00:00:00 2001 From: Usman Nasir Date: Wed, 21 Aug 2019 12:32:16 +0500 Subject: [PATCH] cPanel Importer: bug fix with emails import --- .../templates/baseTemplate/index.html | 3 + dns/dnsManager.py | 20 ++- .../dns/configureDefaultNameServers.html | 130 ++++++++++++++++++ dns/urls.py | 1 + dns/views.py | 8 ++ mailServer/mailserverManager.py | 14 +- plogical/cPanelImporter.py | 86 +++++++----- plogical/mailUtilities.py | 2 +- 8 files changed, 226 insertions(+), 38 deletions(-) create mode 100755 dns/templates/dns/configureDefaultNameServers.html diff --git a/baseTemplate/templates/baseTemplate/index.html b/baseTemplate/templates/baseTemplate/index.html index 38b79a5f8..e7db1e024 100755 --- a/baseTemplate/templates/baseTemplate/index.html +++ b/baseTemplate/templates/baseTemplate/index.html @@ -486,6 +486,9 @@
  • {% trans "Create Nameserver" %}
  • + +
  • {% trans "Create DNS Zone" %}
  • diff --git a/dns/dnsManager.py b/dns/dnsManager.py index 08e5cc2b2..4cf4eff00 100755 --- a/dns/dnsManager.py +++ b/dns/dnsManager.py @@ -488,4 +488,22 @@ class DNSManager: except BaseException, msg: final_dic = {'delete_status': 0, 'error_message': str(msg)} final_json = json.dumps(final_dic) - return HttpResponse(final_json) \ No newline at end of file + return HttpResponse(final_json) + + def configureDefaultNameServers(self, request=None, userID=None): + + try: + currentACL = ACLManager.loadedACL(userID) + + if ACLManager.currentContextPermission(currentACL, 'deleteZone') == 0: + return ACLManager.loadError() + + if not os.path.exists('/home/cyberpanel/powerdns'): + return render(request, 'dns/addDeleteDNSRecords.html', {"status": 0}) + + domainsList = ACLManager.findAllDomains(currentACL, userID) + + return render(request, 'dns/configureDefaultNameServers.html', {"domainsList": domainsList, "status": 1}) + + except BaseException, msg: + return HttpResponse(str(msg)) \ No newline at end of file diff --git a/dns/templates/dns/configureDefaultNameServers.html b/dns/templates/dns/configureDefaultNameServers.html new file mode 100755 index 000000000..ee37a9498 --- /dev/null +++ b/dns/templates/dns/configureDefaultNameServers.html @@ -0,0 +1,130 @@ +{% extends "baseTemplate/index.html" %} +{% load i18n %} +{% block title %}{% trans "Create Nameserver - CyberPanel" %}{% endblock %} +{% block content %} + +{% load static %} +{% get_current_language as LANGUAGE_CODE %} + + +
    +
    +

    {% trans "Create Nameserver" %} - {% trans "DNS Docs" %}

    +

    {% trans "You can use this page to setup nameservers using which people on the internet can resolve websites hosted on this server." %}

    +
    +
    +
    +

    + {% trans "Details" %} +

    +
    + + {% if not status %} +
    +

    {% trans "PowerDNS is disabled." %} +

    +
    + + {% else %} + +
    + +
    + +
    + +
    + +
    example.com
    + +
    + +
    + +
    + +
    + +
    ns1.example.com
    +
    + +
    + +
    + +
    +
    + +
    + +
    + +
    +
    ns2.example.com
    + +
    + +
    + +
    + +
    +
    + +
    + +
    + + +
    +
    + +
    + +
    +
    +

    {% trans "Nameserver cannot be created. Error message:" %} {$ errorMessage $}

    +
    + +
    +

    {% trans "The following nameservers were successfully created:" %}
    + + {$ nameServerOne $}
    + {$ nameServerTwo $}
    + +

    +
    + +
    +

    {% trans "Could not connect to server. Please refresh this page." %}

    +
    + + +
    + + + +
    + + +
    + + + {% endif %} + + + + +
    +
    +
    + + + +
    + + +{% endblock %} diff --git a/dns/urls.py b/dns/urls.py index c54d99e4a..fd48f7bd8 100755 --- a/dns/urls.py +++ b/dns/urls.py @@ -4,6 +4,7 @@ import views urlpatterns = [ url(r'^$', views.loadDNSHome, name='dnsHome'), url(r'^createNameserver', views.createNameserver, name='createNameserver'), + url(r'^configureDefaultNameServers$', views.configureDefaultNameServers, name='configureDefaultNameServers'), url(r'^createDNSZone', views.createDNSZone, name='createDNSZone'), url(r'^addDeleteDNSRecords', views.addDeleteDNSRecords, name='addDeleteDNSRecords'), diff --git a/dns/views.py b/dns/views.py index 87d13c65a..ee3bed88a 100755 --- a/dns/views.py +++ b/dns/views.py @@ -152,6 +152,14 @@ def submitZoneDeletion(request): return redirect(loadLoginPage) +def configureDefaultNameServers(request): + try: + userID = request.session['userID'] + dm = DNSManager() + return dm.configureDefaultNameServers(request, userID) + except KeyError: + return redirect(loadLoginPage) + diff --git a/mailServer/mailserverManager.py b/mailServer/mailserverManager.py index ed69dfc14..9e59f2ba3 100755 --- a/mailServer/mailserverManager.py +++ b/mailServer/mailserverManager.py @@ -449,10 +449,16 @@ class MailServerManager: emailDB = EUsers.objects.get(email=email) admin = Administrator.objects.get(pk=userID) - if ACLManager.checkOwnership(emailDB.emailOwner.domainOwner.domain, admin, currentACL) == 1: - pass - else: - return ACLManager.loadErrorJson() + try: + if ACLManager.checkOwnership(emailDB.emailOwner.domainOwner.domain, admin, currentACL) == 1: + pass + else: + return ACLManager.loadErrorJson() + except: + if ACLManager.checkOwnership(emailDB.emailOwner.childOwner.domain, admin, currentACL) == 1: + pass + else: + return ACLManager.loadErrorJson() CentOSPath = '/etc/redhat-release' if os.path.exists(CentOSPath): diff --git a/plogical/cPanelImporter.py b/plogical/cPanelImporter.py index 3c038f380..df0d238f3 100644 --- a/plogical/cPanelImporter.py +++ b/plogical/cPanelImporter.py @@ -21,7 +21,7 @@ import MySQLdb.cursors as cursors import shlex import subprocess from databases.models import Databases -from websiteFunctions.models import Websites +from websiteFunctions.models import Websites, ChildDomains as CDomains from plogical.vhost import vhost from plogical.virtualHostUtilities import virtualHostUtilities from plogical.mailUtilities import mailUtilities @@ -459,6 +459,11 @@ class cPanelImporter: logging.statusWriter(self.logFile, message, 1) return 0 + def createDummyChild(self, childDomain): + path = '/home/%s/public_html/%s' % (self.mainDomain, childDomain) + virtualHostUtilities.createDomain(self.mainDomain, childDomain, self.PHPVersion, path, 0, 0, + 0, 'admin', 0) + def CreateDNSRecords(self): try: @@ -717,6 +722,15 @@ class cPanelImporter: def DeleteSite(self): vhost.deleteVirtualHostConfigurations(self.mainDomain) + def checkIfExists(self, virtualHostName): + if Websites.objects.filter(domain=virtualHostName).count() > 0: + return 1 + + if CDomains.objects.filter(domain=virtualHostName).count() > 0: + return 1 + + return 0 + def RestoreEmails(self): try: @@ -757,51 +771,59 @@ class cPanelImporter: continue if items.find('.') > -1: for it in os.listdir(FinalMailDomainPath): - mailUtilities.createEmailAccount(items, it, 'cyberpanel') - finalEmailUsername = it + "@" + items - message = 'Starting restore for %s.' % (finalEmailUsername) - logging.statusWriter(self.logFile, message, 1) - eUser = EUsers.objects.get(email=finalEmailUsername) + try: + if self.checkIfExists(items) == 0: + self.createDummyChild(items) + + mailUtilities.createEmailAccount(items, it, 'cyberpanel') + finalEmailUsername = it + "@" + items + message = 'Starting restore for %s.' % (finalEmailUsername) + logging.statusWriter(self.logFile, message, 1) + eUser = EUsers.objects.get(email=finalEmailUsername) - if self.mailFormat == cPanelImporter.MailDir: - eUser.mail = 'maildir:/home/vmail/%s/%s/Maildir' % (items, it) - MailPath = '/home/vmail/%s/%s/Maildir/' % (items, it) + if self.mailFormat == cPanelImporter.MailDir: + eUser.mail = 'maildir:/home/vmail/%s/%s/Maildir' % (items, it) + MailPath = '/home/vmail/%s/%s/Maildir/' % (items, it) - command = 'mkdir -p %s' % (MailPath) - ProcessUtilities.normalExecutioner(command) + command = 'mkdir -p %s' % (MailPath) + ProcessUtilities.normalExecutioner(command) - MailPathInBackup = '%s/%s' % (FinalMailDomainPath, it) + MailPathInBackup = '%s/%s' % (FinalMailDomainPath, it) - command = 'cp -R %s/* %s' % (MailPathInBackup, MailPath) - subprocess.call(command, shell=True) + command = 'cp -R %s/* %s' % (MailPathInBackup, MailPath) + subprocess.call(command, shell=True) - else: - eUser.mail = 'mdbox:/home/vmail/%s/%s/Mdbox' % (items, it) - MailPath = '/home/vmail/%s/%s/Mdbox/' % (items, it) + else: + eUser.mail = 'mdbox:/home/vmail/%s/%s/Mdbox' % (items, it) + MailPath = '/home/vmail/%s/%s/Mdbox/' % (items, it) - command = 'mkdir -p %s' % (MailPath) - ProcessUtilities.normalExecutioner(command) + command = 'mkdir -p %s' % (MailPath) + ProcessUtilities.normalExecutioner(command) - MailPathInBackup = '%s/%s' % (FinalMailDomainPath, it) + MailPathInBackup = '%s/%s' % (FinalMailDomainPath, it) - command = 'cp -R %s/* %s' % (MailPathInBackup, MailPath) - subprocess.call(command, shell=True) + command = 'cp -R %s/* %s' % (MailPathInBackup, MailPath) + subprocess.call(command, shell=True) - ## Also update password + ## Also update password - PasswordPath = '%s/homedir/etc/%s/shadow' % (CompletPathToExtractedArchive, items) - PasswordData = open(PasswordPath, 'r').readlines() + PasswordPath = '%s/homedir/etc/%s/shadow' % (CompletPathToExtractedArchive, items) + PasswordData = open(PasswordPath, 'r').readlines() - for i in PasswordData: - if i.find(it) > -1: - finalPassword = '%s%s' % ('{CRYPT}', i.split(':')[1]) - eUser.password = finalPassword + for i in PasswordData: + if i.find(it) > -1: + finalPassword = '%s%s' % ('{CRYPT}', i.split(':')[1]) + eUser.password = finalPassword - eUser.save() + eUser.save() - message = 'Restore completed for %s.' % (finalEmailUsername) - logging.statusWriter(self.logFile, message, 1) + message = 'Restore completed for %s.' % (finalEmailUsername) + logging.statusWriter(self.logFile, message, 1) + except BaseException, msg: + message = 'Failed to restore emails from archive file %s, For domain: %s. error message: %s. [ExtractBackup]' % ( + self.backupFile, items, str(msg)) + logging.statusWriter(self.logFile, message, 1) command = 'chown -R vmail:vmail /home/vmail' ProcessUtilities.normalExecutioner(command) diff --git a/plogical/mailUtilities.py b/plogical/mailUtilities.py index 526cf40e7..fb261ad12 100755 --- a/plogical/mailUtilities.py +++ b/plogical/mailUtilities.py @@ -110,7 +110,7 @@ class mailUtilities: ## After effects - execPath = "sudo python /usr/local/CyberCP/plogical/mailUtilities.py" + execPath = "/usr/local/CyberCP/bin/python2 /usr/local/CyberCP/plogical/mailUtilities.py" execPath = execPath + " AfterEffects --domain " + domain ProcessUtilities.executioner(execPath, 'lscpd')