def deleteDNSRecord(self, recordID): try: DNS.deleteDNSRecord(recordID) self.printStatus(1, 'None') except BaseException, msg: logger.writeforCLI(str(msg), "Error", stack()[0][3]) self.printStatus(0, str(msg))
def deleteDNSZone(self, virtualHostName): try: DNS.deleteDNSZone(virtualHostName) self.printStatus(1, 'None') except BaseException, msg: logger.writeforCLI(str(msg), "Error", stack()[0][3]) self.printStatus(0, str(msg))
def createDNSZone(self, virtualHostName, owner): try: admin = Administrator.objects.get(userName=owner) DNS.dnsTemplate(virtualHostName, admin) self.printStatus(1, 'None') except BaseException, msg: logger.writeforCLI(str(msg), "Error", stack()[0][3]) self.printStatus(0, str(msg))
def createDNSRecord(self, virtualHostName, name, recordType, value, priority, ttl): try: zone = DNS.getZoneObject(virtualHostName) DNS.createDNSRecord(zone, name, recordType, value, int(priority), int(ttl)) self.printStatus(1, 'None') except BaseException, msg: logger.writeforCLI(str(msg), "Error", stack()[0][3]) self.printStatus(0, str(msg))
def NSCreation(self, userID=None, data=None): try: admin = Administrator.objects.get(pk=userID) currentACL = ACLManager.loadedACL(userID) if ACLManager.currentContextPermission(currentACL, 'createNameServer') == 0: return ACLManager.loadErrorJson('NSCreation', 0) domainForNS = data['domainForNS'] ns1 = data['ns1'] ns2 = data['ns2'] firstNSIP = data['firstNSIP'] secondNSIP = data['secondNSIP'] DNS.dnsTemplate(domainForNS, admin) newZone = Domains.objects.get(name=domainForNS) ## NS1 record = Records(domainOwner=newZone, domain_id=newZone.id, name=ns1, type="A", content=firstNSIP, ttl=3600, prio=0, disabled=0, auth=1) record.save() ## NS2 record = Records(domainOwner=newZone, domain_id=newZone.id, name=ns2, type="A", content=secondNSIP, ttl=3600, prio=0, disabled=0, auth=1) record.save() final_dic = {'NSCreation': 1, 'error_message': "None"} final_json = json.dumps(final_dic) return HttpResponse(final_json) except BaseException, msg: final_dic = {'NSCreation': 0, 'error_message': str(msg)} final_json = json.dumps(final_dic) return HttpResponse(final_json)
def listDNSZonesJson(self): try: records = DNS.getDNSZones() json_data = "[" checker = 0 for items in records: dic = { 'id': items.id, 'name': items.name, } if checker == 0: json_data = json_data + json.dumps(dic) checker = 1 else: json_data = json_data + ',' + json.dumps(dic) json_data = json_data + ']' final_json = json.dumps(json_data) print final_json except BaseException, msg: logger.writeforCLI(str(msg), "Error", stack()[0][3]) print 0
def listDNSJson(self, virtualHostName): try: records = DNS.getDNSRecords(virtualHostName) json_data = "[" checker = 0 for items in records: dic = { 'id': items.id, 'type': items.type, 'name': items.name, 'content': items.content, 'priority': items.prio, 'ttl': items.ttl } if checker == 0: json_data = json_data + json.dumps(dic) checker = 1 else: json_data = json_data + ',' + json.dumps(dic) json_data = json_data + ']' final_json = json.dumps(json_data) print final_json except BaseException, msg: logger.writeforCLI(str(msg), "Error", stack()[0][3]) print 0
def listDNSZonesPretty(self): try: from prettytable import PrettyTable records = records = DNS.getDNSZones() table = PrettyTable(['ID', 'Name']) for items in records: table.add_row([items.id, items.name]) print table except BaseException, msg: logger.writeforCLI(str(msg), "Error", stack()[0][3]) print 0
def listDNSPretty(self, virtualHostName): try: from prettytable import PrettyTable records = DNS.getDNSRecords(virtualHostName) table = PrettyTable( ['ID', 'TYPE', 'Name', 'Value', 'Priority', 'TTL']) for items in records: if len(items.content) >= 30: content = items.content[0:30] + " ..." else: content = items.content table.add_row([ items.id, items.type, items.name, content, items.prio, items.ttl ]) print table except BaseException, msg: logger.writeforCLI(str(msg), "Error", stack()[0][3]) print 0
def addDNSRecord(request): try: val = request.session['userID'] admin = Administrator.objects.get(pk=val) try: if request.method == 'POST': data = json.loads(request.body) zoneDomain = data['selectedZone'] recordType = data['recordType'] recordName = data['recordName'] ttl = int(data['ttl']) if admin.type != 1: website = Websites.objects.get(domain=zoneDomain) if website.admin != admin: dic = { 'add_status': 0, 'error_message': "Only administrator can view this page." } json_data = json.dumps(dic) return HttpResponse(json_data) zone = Domains.objects.get(name=zoneDomain) value = "" if recordType == "A": recordContentA = data[ 'recordContentA'] ## IP or ponting value if recordName == "@": value = zoneDomain ## re.match elif match( r'([\da-z\.-]+\.[a-z\.]{2,12}|[\d\.]+)([\/:?=&#]{1}[\da-z\.-]+)*[\/\?]?', recordName, M | I): value = recordName else: value = recordName + "." + zoneDomain DNS.createDNSRecord(zone, value, recordType, recordContentA, 0, ttl) elif recordType == "MX": if recordName == "@": value = zoneDomain ## re.match elif match( r'([\da-z\.-]+\.[a-z\.]{2,12}|[\d\.]+)([\/:?=&#]{1}[\da-z\.-]+)*[\/\?]?', recordName, M | I): value = recordName else: value = recordName + "." + zoneDomain recordContentMX = data['recordContentMX'] priority = data['priority'] DNS.createDNSRecord(zone, value, recordType, recordContentMX, priority, ttl) elif recordType == "AAAA": if recordName == "@": value = zoneDomain ## re.match elif match( r'([\da-z\.-]+\.[a-z\.]{2,12}|[\d\.]+)([\/:?=&#]{1}[\da-z\.-]+)*[\/\?]?', recordName, M | I): value = recordName else: value = recordName + "." + zoneDomain recordContentAAAA = data[ 'recordContentAAAA'] ## IP or ponting value DNS.createDNSRecord(zone, value, recordType, recordContentAAAA, 0, ttl) elif recordType == "CNAME": if recordName == "@": value = zoneDomain ## re.match elif match( r'([\da-z\.-]+\.[a-z\.]{2,12}|[\d\.]+)([\/:?=&#]{1}[\da-z\.-]+)*[\/\?]?', recordName, M | I): value = recordName else: value = recordName + "." + zoneDomain recordContentCNAME = data[ 'recordContentCNAME'] ## IP or ponting value DNS.createDNSRecord(zone, value, recordType, recordContentCNAME, 0, ttl) elif recordType == "SPF": if recordName == "@": value = zoneDomain ## re.match elif match( r'([\da-z\.-]+\.[a-z\.]{2,12}|[\d\.]+)([\/:?=&#]{1}[\da-z\.-]+)*[\/\?]?', recordName, M | I): value = recordName else: value = recordName + "." + zoneDomain recordContentSPF = data[ 'recordContentSPF'] ## IP or ponting value DNS.createDNSRecord(zone, value, recordType, recordContentSPF, 0, ttl) elif recordType == "TXT": if recordName == "@": value = zoneDomain ## re.match elif match( r'([\da-z\.-]+\.[a-z\.]{2,12}|[\d\.]+)([\/:?=&#]{1}[\da-z\.-]+)*[\/\?]?', recordName, M | I): value = recordName else: value = recordName + "." + zoneDomain recordContentTXT = data[ 'recordContentTXT'] ## IP or ponting value DNS.createDNSRecord(zone, value, recordType, recordContentTXT, 0, ttl) elif recordType == "SOA": recordContentSOA = data['recordContentSOA'] DNS.createDNSRecord(zone, value, recordType, recordContentSOA, 0, ttl) elif recordType == "NS": recordContentNS = data['recordContentNS'] if recordContentNS == "@": recordContentNS = "ns1." + zoneDomain ## re.match elif match( r'([\da-z\.-]+\.[a-z\.]{2,12}|[\d\.]+)([\/:?=&#]{1}[\da-z\.-]+)*[\/\?]?', recordContentNS, M | I): recordContentNS = recordContentNS else: recordContentNS = recordContentNS + "." + zoneDomain DNS.createDNSRecord(zone, recordName, recordType, recordContentNS, 0, ttl) elif recordType == "SRV": if recordName == "@": value = zoneDomain ## re.match elif match( r'([\da-z\.-]+\.[a-z\.]{2,12}|[\d\.]+)([\/:?=&#]{1}[\da-z\.-]+)*[\/\?]?', recordName, M | I): value = recordName else: value = recordName + "." + zoneDomain recordContentSRV = data['recordContentSRV'] priority = data['priority'] DNS.createDNSRecord(zone, value, recordType, recordContentSRV, priority, ttl) final_dic = {'add_status': 1, 'error_message': "None"} final_json = json.dumps(final_dic) return HttpResponse(final_json) except BaseException, msg: final_dic = {'add_status': 0, 'error_message': str(msg)} final_json = json.dumps(final_dic) return HttpResponse(final_json) except KeyError, msg: final_dic = { 'add_status': 0, 'error_message': "Not Logged In, please refresh the page or login again." } final_json = json.dumps(final_dic) return HttpResponse(final_json)
def addDNSRecord(self, userID=None, data=None): try: currentACL = ACLManager.loadedACL(userID) if ACLManager.currentContextPermission(currentACL, 'addDeleteRecords') == 0: return ACLManager.loadErrorJson('add_status', 0) zoneDomain = data['selectedZone'] recordType = data['recordType'] recordName = data['recordName'] ttl = int(data['ttl']) admin = Administrator.objects.get(pk=userID) if ACLManager.checkOwnershipZone(zoneDomain, admin, currentACL) == 1: pass else: return ACLManager.loadErrorJson() zone = Domains.objects.get(name=zoneDomain) value = "" if recordType == "A": recordContentA = data['recordContentA'] ## IP or ponting value if recordName == "@": value = zoneDomain ## re.match elif match( r'([\da-z\.-]+\.[a-z\.]{2,12}|[\d\.]+)([\/:?=&#]{1}[\da-z\.-]+)*[\/\?]?', recordName, M | I): value = recordName else: value = recordName + "." + zoneDomain DNS.createDNSRecord(zone, value, recordType, recordContentA, 0, ttl) elif recordType == "MX": if recordName == "@": value = zoneDomain ## re.match elif match( r'([\da-z\.-]+\.[a-z\.]{2,12}|[\d\.]+)([\/:?=&#]{1}[\da-z\.-]+)*[\/\?]?', recordName, M | I): value = recordName else: value = recordName + "." + zoneDomain recordContentMX = data['recordContentMX'] priority = data['priority'] DNS.createDNSRecord(zone, value, recordType, recordContentMX, priority, ttl) elif recordType == "AAAA": if recordName == "@": value = zoneDomain ## re.match elif match( r'([\da-z\.-]+\.[a-z\.]{2,12}|[\d\.]+)([\/:?=&#]{1}[\da-z\.-]+)*[\/\?]?', recordName, M | I): value = recordName else: value = recordName + "." + zoneDomain recordContentAAAA = data[ 'recordContentAAAA'] ## IP or ponting value DNS.createDNSRecord(zone, value, recordType, recordContentAAAA, 0, ttl) elif recordType == "CNAME": if recordName == "@": value = zoneDomain ## re.match elif match( r'([\da-z\.-]+\.[a-z\.]{2,12}|[\d\.]+)([\/:?=&#]{1}[\da-z\.-]+)*[\/\?]?', recordName, M | I): value = recordName else: value = recordName + "." + zoneDomain recordContentCNAME = data[ 'recordContentCNAME'] ## IP or ponting value DNS.createDNSRecord(zone, value, recordType, recordContentCNAME, 0, ttl) elif recordType == "SPF": if recordName == "@": value = zoneDomain ## re.match elif match( r'([\da-z\.-]+\.[a-z\.]{2,12}|[\d\.]+)([\/:?=&#]{1}[\da-z\.-]+)*[\/\?]?', recordName, M | I): value = recordName else: value = recordName + "." + zoneDomain recordContentSPF = data[ 'recordContentSPF'] ## IP or ponting value DNS.createDNSRecord(zone, value, recordType, recordContentSPF, 0, ttl) elif recordType == "TXT": if recordName == "@": value = zoneDomain ## re.match elif match( r'([\da-z\.-]+\.[a-z\.]{2,12}|[\d\.]+)([\/:?=&#]{1}[\da-z\.-]+)*[\/\?]?', recordName, M | I): value = recordName else: value = recordName + "." + zoneDomain recordContentTXT = data[ 'recordContentTXT'] ## IP or ponting value DNS.createDNSRecord(zone, value, recordType, recordContentTXT, 0, ttl) elif recordType == "SOA": recordContentSOA = data['recordContentSOA'] DNS.createDNSRecord(zone, recordName, recordType, recordContentSOA, 0, ttl) elif recordType == "NS": recordContentNS = data['recordContentNS'] if recordContentNS == "@": recordContentNS = "ns1." + zoneDomain ## re.match elif match( r'([\da-z\.-]+\.[a-z\.]{2,12}|[\d\.]+)([\/:?=&#]{1}[\da-z\.-]+)*[\/\?]?', recordContentNS, M | I): recordContentNS = recordContentNS else: recordContentNS = recordContentNS + "." + zoneDomain DNS.createDNSRecord(zone, recordName, recordType, recordContentNS, 0, ttl) elif recordType == "SRV": if recordName == "@": value = zoneDomain ## re.match elif match( r'([\da-z\.-]+\.[a-z\.]{2,12}|[\d\.]+)([\/:?=&#]{1}[\da-z\.-]+)*[\/\?]?', recordName, M | I): value = recordName else: value = recordName + "." + zoneDomain recordContentSRV = data['recordContentSRV'] priority = data['priority'] DNS.createDNSRecord(zone, value, recordType, recordContentSRV, priority, ttl) elif recordType == "CAA": if recordName == "@": value = zoneDomain ## re.match elif match( r'([\da-z\.-]+\.[a-z\.]{2,12}|[\d\.]+)([\/:?=&#]{1}[\da-z\.-]+)*[\/\?]?', recordName, M | I): value = recordName else: value = recordName + "." + zoneDomain recordContentCAA = data[ 'recordContentCAA'] ## IP or ponting value DNS.createDNSRecord(zone, value, recordType, recordContentCAA, 0, ttl) final_dic = {'status': 1, 'add_status': 1, 'error_message': "None"} final_json = json.dumps(final_dic) return HttpResponse(final_json) except BaseException, msg: final_dic = { 'status': 0, 'add_status': 0, 'error_message': str(msg) } final_json = json.dumps(final_dic) return HttpResponse(final_json)
def CreateDNSRecords(self): try: message = 'We are going to create DNS records now, please note we will not create DKIM records. Make sure to create them from CyberPanel interface using our DKIM manager.' logging.statusWriter(self.logFile, message, 1) ipFile = "/etc/cyberpanel/machineIP" f = open(ipFile) ipData = f.read() ipAddress = ipData.split('\n', 1)[0] admin = Administrator.objects.get(pk=1) CompletPathToExtractedArchive = cPanelImporter.mainBackupPath + self.fileName DNSZonesPath = '%s/dnszones' % (CompletPathToExtractedArchive) for items in os.listdir(DNSZonesPath): topLevelDomain = items.replace('.db', '', 1) message = 'Creating DNS records for %s' % (topLevelDomain) logging.statusWriter(self.logFile, message, 1) try: Domains.objects.get(name=topLevelDomain).delete() except: pass try: pdns = PDNSStatus.objects.get(pk=1) if pdns.type == 'MASTER': zone = Domains(admin=admin, name=topLevelDomain, type="MASTER") zone.save() else: zone = Domains(admin=admin, name=topLevelDomain, type="NATIVE") zone.save() except: pass content = "ns1." + topLevelDomain + " hostmaster." + topLevelDomain + " 1 10800 3600 604800 3600" soaRecord = Records(domainOwner=zone, domain_id=zone.id, name=topLevelDomain, type="SOA", content=content, ttl=3600, prio=0, disabled=0, auth=1) soaRecord.save() CurrentZonePath = '%s/%s' % (DNSZonesPath, items) data = open(CurrentZonePath, 'r').readlines() SOACheck = 0 start = 0 for items in data: try: if items.find('SOA') > -1: SOACheck = 1 continue if SOACheck == 1 and items.find(')') > -1: SOACheck = 0 start = 1 continue else: pass if start == 1: if len(items) > 3: if items.find("DKIM1") > -1: continue RecordsData = items.split('\t') if RecordsData[3] == 'A': RecordsData[4] = ipAddress if RecordsData[0].find(topLevelDomain) > -1: if RecordsData[3] == 'MX': DNS.createDNSRecord( zone, RecordsData[0].rstrip('.'), RecordsData[3], RecordsData[5].rstrip('.').rstrip( '.\n'), int(RecordsData[4]), RecordsData[1]) else: DNS.createDNSRecord( zone, RecordsData[0].rstrip('.'), RecordsData[3], RecordsData[4].rstrip('.').rstrip( '.\n'), 0, RecordsData[1]) else: if RecordsData[3] == 'MX': DNS.createDNSRecord( zone, RecordsData[0] + '.' + topLevelDomain, RecordsData[3], RecordsData[5].rstrip('.').rstrip( '.\n'), RecordsData[4], RecordsData[1]) else: DNS.createDNSRecord( zone, RecordsData[0] + '.' + topLevelDomain, RecordsData[3], RecordsData[4].rstrip('.').rstrip( '.\n'), 0, RecordsData[1]) except BaseException, msg: message = 'Failed while creating DNS entry for %s, error message: %s.' % ( topLevelDomain, str(msg)) logging.statusWriter(self.logFile, message, 1) message = 'DNS records successfully created for %s.' % ( topLevelDomain) logging.statusWriter(self.logFile, message, 1) return 1
def prepareBackupMeta(self): try: ######### Generating meta ## XML Generation metaFileXML = Element('metaFile') child = SubElement(metaFileXML, 'masterDomain') child.text = self.website.domain child = SubElement(metaFileXML, 'phpSelection') child.text = self.website.phpSelection child = SubElement(metaFileXML, 'externalApp') child.text = self.website.externalApp childDomains = self.website.childdomains_set.all() databases = self.website.databases_set.all() ## Child domains XML childDomainsXML = Element('ChildDomains') for items in childDomains: childDomainXML = Element('domain') child = SubElement(childDomainXML, 'domain') child.text = items.domain child = SubElement(childDomainXML, 'phpSelection') child.text = items.phpSelection child = SubElement(childDomainXML, 'path') child.text = items.path childDomainsXML.append(childDomainXML) metaFileXML.append(childDomainsXML) ## Databases XML databasesXML = Element('Databases') for items in databases: try: dbuser = DBUsers.objects.get(user=items.dbUser) userToTry = items.dbUser except: dbusers = DBUsers.objects.all().filter(user=items.dbUser) userToTry = items.dbUser for it in dbusers: dbuser = it break userToTry = mysqlUtilities.mysqlUtilities.fetchuser( items.dbUser) try: dbuser = DBUsers.objects.get(user=userToTry) except: dbusers = DBUsers.objects.all().filter(user=userToTry) for it in dbusers: dbuser = it break databaseXML = Element('database') child = SubElement(databaseXML, 'dbName') child.text = items.dbName child = SubElement(databaseXML, 'dbUser') child.text = userToTry child = SubElement(databaseXML, 'password') child.text = dbuser.password databasesXML.append(databaseXML) metaFileXML.append(databasesXML) ## Get Aliases aliasesXML = Element('Aliases') aliases = backupUtilities.getAliases(self.website.domain) for items in aliases: child = SubElement(aliasesXML, 'alias') child.text = items metaFileXML.append(aliasesXML) ## Finish Alias ## DNS Records XML try: dnsRecordsXML = Element("dnsrecords") dnsRecords = DNS.getDNSRecords(self.website.domain) for items in dnsRecords: dnsRecordXML = Element('dnsrecord') child = SubElement(dnsRecordXML, 'type') child.text = items.type child = SubElement(dnsRecordXML, 'name') child.text = items.name child = SubElement(dnsRecordXML, 'content') child.text = items.content child = SubElement(dnsRecordXML, 'priority') child.text = str(items.prio) dnsRecordsXML.append(dnsRecordXML) metaFileXML.append(dnsRecordsXML) except BaseException, msg: logging.statusWriter(self.statusPath, '%s. [158:prepMeta]' % (str(msg)), 1) ## Email accounts XML try: emailRecordsXML = Element('emails') eDomain = eDomains.objects.get(domain=self.website.domain) emailAccounts = eDomain.eusers_set.all() for items in emailAccounts: emailRecordXML = Element('emailAccount') child = SubElement(emailRecordXML, 'email') child.text = items.email child = SubElement(emailRecordXML, 'password') child.text = items.password emailRecordsXML.append(emailRecordXML) metaFileXML.append(emailRecordsXML) except BaseException, msg: logging.statusWriter(self.statusPath, '%s. [warning:179:prepMeta]' % (str(msg)), 1)
def generateDKIMKeys(self): try: userID = self.request.session['userID'] currentACL = ACLManager.loadedACL(userID) if ACLManager.currentContextPermission(currentACL, 'dkimManager') == 0: return ACLManager.loadErrorJson('generateStatus', 0) data = json.loads(self.request.body) domainName = data['domainName'] admin = Administrator.objects.get(pk=userID) if ACLManager.checkOwnership(domainName, admin, currentACL) == 1: pass else: return ACLManager.loadErrorJson() execPath = "/usr/local/CyberCP/bin/python2 " + virtualHostUtilities.cyberPanel + "/plogical/mailUtilities.py" execPath = execPath + " generateKeys --domain " + domainName output = ProcessUtilities.outputExecutioner(execPath) admin = Administrator.objects.get(pk=userID) DNS.dnsTemplate(domainName, admin) if output.find("1,None") > -1: import tldextract extractDomain = tldextract.extract(domainName) topLevelDomain = extractDomain.domain + '.' + extractDomain.suffix zone = dnsDomains.objects.get(name=topLevelDomain) zone.save() path = "/etc/opendkim/keys/" + domainName + "/default.txt" command = "sudo cat " + path output = ProcessUtilities.outputExecutioner(command) leftIndex = output.index('(') + 2 rightIndex = output.rindex(')') - 1 DNS.createDKIMRecords(domainName) record = dnsRecords(domainOwner=zone, domain_id=zone.id, name="default._domainkey." + domainName, type="TXT", content=output[leftIndex:rightIndex], ttl=3600, prio=0, disabled=0, auth=1) record.save() data_ret = {'status': 1, 'generateStatus': 1, 'error_message': "None"} json_data = json.dumps(data_ret) return HttpResponse(json_data) else: data_ret = {'status': 0, 'generateStatus': 0, 'error_message': output} json_data = json.dumps(data_ret) return HttpResponse(json_data) except BaseException, msg: data_ret = {'status': 0, 'generateStatus': 0, 'error_message': str(msg)} json_data = json.dumps(data_ret) return HttpResponse(json_data)