def createDKIMRecords(domain): try: import tldextract extractDomain = tldextract.extract(domain) topLevelDomain = extractDomain.domain + '.' + extractDomain.suffix zone = Domains.objects.get(name=topLevelDomain) path = "/etc/opendkim/keys/" + topLevelDomain + "/default.txt" command = "sudo cat " + path output = subprocess.check_output(shlex.split(command)) leftIndex = output.index('(') + 2 rightIndex = output.rindex(')') - 1 record = Records(domainOwner=zone, domain_id=zone.id, name="default._domainkey." + topLevelDomain, type="TXT", content=output[leftIndex:rightIndex], ttl=3600, prio=0, disabled=0, auth=1) record.save() if ProcessUtilities.decideDistro() == ProcessUtilities.ubuntu: command = 'sudo systemctl restart pdns' ProcessUtilities.executioner(command) except BaseException, msg: logging.CyberCPLogFileWriter.writeToFile( "We had errors while creating DKIM record for: " + domain + ". Error message: " + str(msg))
def new(request, dom_id): cuenta = Accounts.objects.get(user = request.user.id) usados = Domains.objects.filter(accounts = cuenta).count() domain = Domains.objects.get(pk=dom_id) if not domain: return redirect(reverse('dns.views.domains.index')) record = Records() record.domain_id = domain.id if request.method == 'POST': form = RecordsForm(request.POST, instance=record, auto_id=False) if form.is_valid(): form.save() return redirect(reverse('dns.views.domains.edit', args=[dom_id])) else: form = RecordsForm(instance=record, auto_id=False) return render_to_response('dns_records_edit.html', { 'form': form, 'cuenta': cuenta, 'usados': usados, 'disponibles': cuenta.limit_dns - usados, 'total': cuenta.limit_dns, 'dom_id': record.domain.id, 'dom': record.domain.domain, 'tipo': 'Zonas', 'nuevo': True }, context_instance=RequestContext(request))
def createDKIMRecords(domain): try: import tldextract extractDomain = tldextract.extract(domain) topLevelDomain = extractDomain.domain + '.' + extractDomain.suffix zone = Domains.objects.get(name=topLevelDomain) path = "/etc/opendkim/keys/" + topLevelDomain + "/default.txt" command = "sudo cat " + path output = subprocess.check_output(shlex.split(command)) record = Records(domainOwner=zone, domain_id=zone.id, name="default._domainkey." + topLevelDomain, type="TXT", content="v=DKIM1; k=rsa; p=" + output[53:269], ttl=3600, prio=0, disabled=0, auth=1) record.save() except BaseException, msg: logging.CyberCPLogFileWriter.writeToFile( "We had errors while creating DKIM record for: " + domain + ". Error message: " + str(msg))
def create_dynamic(sender, instance, **kwargs): if not instance.id: dynamic = BillingDomains.objects.filter(domain='dynhost.es')[0] record = Records() record.data = instance.ip record.host = instance.domain record.domain = dynamic record.ttl = 3600 record.type = 'A' record.save() instance.record = record else: instance.record.data = instance.ip instance.record.host = instance.domain instance.record.save()
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 createDNSRecord(zone, name, type, value, priority, ttl): try: if zone.type == 'MASTER': getSOA = Records.objects.get(domainOwner=zone, type='SOA') soaContent = getSOA.content.split(' ') soaContent[2] = str(int(soaContent[2]) + 1) getSOA.content = " ".join(soaContent) getSOA.save() if type == 'NS': if Records.objects.filter(name=name, type=type, content=value).count() == 0: record = Records(domainOwner=zone, domain_id=zone.id, name=name, type=type, content=value, ttl=ttl, prio=priority, disabled=0, auth=1) record.save() if ProcessUtilities.decideDistro() == ProcessUtilities.ubuntu: command = 'sudo systemctl restart pdns' ProcessUtilities.executioner(command) return if type == 'TXT': if Records.objects.filter(name=name, type=type, content=value).count() == 0: record = Records(domainOwner=zone, domain_id=zone.id, name=name, type=type, content=value, ttl=ttl, prio=priority, disabled=0, auth=1) record.save() if ProcessUtilities.decideDistro() == ProcessUtilities.ubuntu: command = 'sudo systemctl restart pdns' ProcessUtilities.executioner(command) return if type == 'MX': record = Records(domainOwner=zone, domain_id=zone.id, name=name, type=type, content=value, ttl=ttl, prio=priority, disabled=0, auth=1) record.save() if ProcessUtilities.decideDistro() == ProcessUtilities.ubuntu: command = 'sudo systemctl restart pdns' ProcessUtilities.executioner(command) return if Records.objects.filter(name=name, type=type).count() == 0: record = Records(domainOwner=zone, domain_id=zone.id, name=name, type=type, content=value, ttl=ttl, prio=priority, disabled=0, auth=1) record.save() if ProcessUtilities.decideDistro() == ProcessUtilities.ubuntu: command = 'sudo systemctl restart pdns' ProcessUtilities.executioner(command) except BaseException, msg: logging.CyberCPLogFileWriter.writeToFile(str(msg) + " [createDNSRecord]")
def dnsTemplate(domain, admin): try: ipFile = "/etc/cyberpanel/machineIP" f = open(ipFile) ipData = f.read() ipAddress = ipData.split('\n', 1)[0] import tldextract extractDomain = tldextract.extract(domain) topLevelDomain = extractDomain.domain + '.' + extractDomain.suffix subDomain = extractDomain.subdomain if len(subDomain) == 0: if Domains.objects.filter(name=topLevelDomain).count() == 0: try: pdns = PDNSStatus.objects.get(pk=1) if pdns.type == 'MASTER': zone = Domains(admin=admin, name=topLevelDomain, type="MASTER") zone.save() for items in SlaveServers.objects.all(): record = Records(domainOwner=zone, domain_id=zone.id, name=topLevelDomain, type="NS", content=items.slaveServer, ttl=3600, prio=0, disabled=0, auth=1) record.save() else: zone = Domains(admin=admin, name=topLevelDomain, type="NATIVE") except: zone = Domains(admin=admin, name=topLevelDomain, type="NATIVE") zone.save() if zone.type == 'NATIVE': record = Records(domainOwner=zone, domain_id=zone.id, name=topLevelDomain, type="NS", content='hostmaster.%s' % (topLevelDomain), ttl=3600, prio=0, disabled=0, auth=1) record.save() if os.path.exists(DNS.defaultNameServersPath): defaultNS = open(DNS.defaultNameServersPath, 'r').readlines() for items in defaultNS: record = Records(domainOwner=zone, domain_id=zone.id, name=topLevelDomain, type="NS", content=items, ttl=3600, prio=0, disabled=0, auth=1) record.save() else: record = Records(domainOwner=zone, domain_id=zone.id, name=topLevelDomain, type="NS", content='ns1.%s' % (topLevelDomain), ttl=3600, prio=0, disabled=0, auth=1) record.save() record = Records(domainOwner=zone, domain_id=zone.id, name=topLevelDomain, type="NS", content='ns2.%s' % (topLevelDomain), ttl=3600, prio=0, disabled=0, auth=1) record.save() 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() ## Main A record. record = Records(domainOwner=zone, domain_id=zone.id, name=topLevelDomain, type="A", content=ipAddress, ttl=3600, prio=0, disabled=0, auth=1) record.save() # CNAME Records. cNameValue = "www." + topLevelDomain record = Records(domainOwner=zone, domain_id=zone.id, name=cNameValue, type="CNAME", content=topLevelDomain, ttl=3600, prio=0, disabled=0, auth=1) record.save() cNameValue = "ftp." + topLevelDomain record = Records(domainOwner=zone, domain_id=zone.id, name=cNameValue, type="CNAME", content=topLevelDomain, ttl=3600, prio=0, disabled=0, auth=1) record.save() ## MX Record. mxValue = "mail." + topLevelDomain record = Records(domainOwner=zone, domain_id=zone.id, name=topLevelDomain, type="MX", content=mxValue, ttl=3600, prio="10", disabled=0, auth=1) record.save() record = Records(domainOwner=zone, domain_id=zone.id, name=mxValue, type="A", content=ipAddress, ttl=3600, prio=0, disabled=0, auth=1) record.save() ## TXT Records for mail record = Records(domainOwner=zone, domain_id=zone.id, name=topLevelDomain, type="TXT", content="v=spf1 a mx ip4:" + ipAddress + " ~all", ttl=3600, prio=0, disabled=0, auth=1) record.save() record = Records(domainOwner=zone, domain_id=zone.id, name="_dmarc." + topLevelDomain, type="TXT", content="v=DMARC1; p=none", ttl=3600, prio=0, disabled=0, auth=1) record.save() record = Records(domainOwner=zone, domain_id=zone.id, name="_domainkey." + topLevelDomain, type="TXT", content="t=y; o=~;", ttl=3600, prio=0, disabled=0, auth=1) record.save() else: if Domains.objects.filter(name=topLevelDomain).count() == 0: try: pdns = PDNSStatus.objects.get(pk=1) if pdns.type == 'MASTER': zone = Domains(admin=admin, name=topLevelDomain, type="MASTER") else: zone = Domains(admin=admin, name=topLevelDomain, type="NATIVE") except: zone = Domains(admin=admin, name=topLevelDomain, type="NATIVE") 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() ## Main A record. record = Records(domainOwner=zone, domain_id=zone.id, name=topLevelDomain, type="A", content=ipAddress, ttl=3600, prio=0, disabled=0, auth=1) record.save() # CNAME Records. cNameValue = "www." + topLevelDomain record = Records(domainOwner=zone, domain_id=zone.id, name=cNameValue, type="CNAME", content=topLevelDomain, ttl=3600, prio=0, disabled=0, auth=1) record.save() cNameValue = "ftp." + topLevelDomain record = Records(domainOwner=zone, domain_id=zone.id, name=cNameValue, type="CNAME", content=topLevelDomain, ttl=3600, prio=0, disabled=0, auth=1) record.save() ## MX Record. mxValue = "mail." + topLevelDomain record = Records(domainOwner=zone, domain_id=zone.id, name=topLevelDomain, type="MX", content=mxValue, ttl=3600, prio="10", disabled=0, auth=1) record.save() record = Records(domainOwner=zone, domain_id=zone.id, name=mxValue, type="A", content=ipAddress, ttl=3600, prio=0, disabled=0, auth=1) record.save() ## TXT Records for mail record = Records(domainOwner=zone, domain_id=zone.id, name=topLevelDomain, type="TXT", content="v=spf1 a mx ip4:" + ipAddress + " ~all", ttl=3600, prio=0, disabled=0, auth=1) record.save() record = Records(domainOwner=zone, domain_id=zone.id, name="_dmarc." + topLevelDomain, type="TXT", content="v=DMARC1; p=none", ttl=3600, prio=0, disabled=0, auth=1) record.save() record = Records(domainOwner=zone, domain_id=zone.id, name="_domainkey." + topLevelDomain, type="TXT", content="t=y; o=~;", ttl=3600, prio=0, disabled=0, auth=1) record.save() ## Creating sub-domain level record. zone = Domains.objects.get(name=topLevelDomain) actualSubDomain = subDomain + "." + topLevelDomain ## Main A record. DNS.createDNSRecord(zone, actualSubDomain, "A", ipAddress, 0, 3600) # CNAME Records. cNameValue = "www." + actualSubDomain DNS.createDNSRecord(zone, cNameValue, "CNAME", actualSubDomain, 0, 3600) if ProcessUtilities.decideDistro() == ProcessUtilities.ubuntu: command = 'sudo systemctl restart pdns' ProcessUtilities.executioner(command) except BaseException, msg: logging.CyberCPLogFileWriter.writeToFile( "We had errors while creating DNS records for: " + domain + ". Error message: " + str(msg))
def createDNSRecord(zone, name, type, value, priority, ttl): try: if type == 'NS': if Records.objects.filter(name=name, type=type, content=value).count() == 0: record = Records(domainOwner=zone, domain_id=zone.id, name=name, type=type, content=value, ttl=ttl, prio=priority, disabled=0, auth=1) record.save() return if type == 'MX': record = Records(domainOwner=zone, domain_id=zone.id, name=name, type=type, content=value, ttl=ttl, prio=priority, disabled=0, auth=1) record.save() if Records.objects.filter(name=name, type=type).count() == 0: record = Records(domainOwner=zone, domain_id=zone.id, name=name, type=type, content=value, ttl=ttl, prio=priority, disabled=0, auth=1) record.save() except BaseException, msg: logging.CyberCPLogFileWriter.writeToFile( str(msg) + " [createDNSRecord]")
def dnsTemplate(domain, admin): try: ipFile = "/etc/cyberpanel/machineIP" f = open(ipFile) ipData = f.read() ipAddress = ipData.split('\n', 1)[0] import tldextract extractDomain = tldextract.extract(domain) topLevelDomain = extractDomain.domain + '.' + extractDomain.suffix subDomain = extractDomain.subdomain if len(subDomain) == 0: if Domains.objects.filter(name=topLevelDomain).count() == 0: zone = Domains(admin=admin, name=topLevelDomain, type="NATIVE") zone.save() 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() ## Main A record. record = Records(domainOwner=zone, domain_id=zone.id, name=topLevelDomain, type="A", content=ipAddress, ttl=3600, prio=0, disabled=0, auth=1) record.save() # CNAME Records. cNameValue = "www." + topLevelDomain record = Records(domainOwner=zone, domain_id=zone.id, name=cNameValue, type="CNAME", content=topLevelDomain, ttl=3600, prio=0, disabled=0, auth=1) record.save() cNameValue = "ftp." + topLevelDomain record = Records(domainOwner=zone, domain_id=zone.id, name=cNameValue, type="CNAME", content=topLevelDomain, ttl=3600, prio=0, disabled=0, auth=1) record.save() ## MX Record. mxValue = "mail." + topLevelDomain record = Records(domainOwner=zone, domain_id=zone.id, name=topLevelDomain, type="MX", content=mxValue, ttl=3600, prio="10", disabled=0, auth=1) record.save() record = Records(domainOwner=zone, domain_id=zone.id, name=mxValue, type="A", content=ipAddress, ttl=3600, prio=0, disabled=0, auth=1) record.save() ## TXT Records for mail record = Records(domainOwner=zone, domain_id=zone.id, name=topLevelDomain, type="TXT", content="v=spf1 a mx ip4:" + ipAddress + " ~all", ttl=3600, prio=0, disabled=0, auth=1) record.save() record = Records(domainOwner=zone, domain_id=zone.id, name="_dmarc." + topLevelDomain, type="TXT", content="v=DMARC1; p=none", ttl=3600, prio=0, disabled=0, auth=1) record.save() record = Records(domainOwner=zone, domain_id=zone.id, name="_domainkey." + topLevelDomain, type="TXT", content="t=y; o=~;", ttl=3600, prio=0, disabled=0, auth=1) record.save() else: if Domains.objects.filter(name=topLevelDomain).count() == 0: zone = Domains(admin=admin, name=topLevelDomain, type="NATIVE") zone.save() 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() ## Main A record. record = Records(domainOwner=zone, domain_id=zone.id, name=topLevelDomain, type="A", content=ipAddress, ttl=3600, prio=0, disabled=0, auth=1) record.save() # CNAME Records. cNameValue = "www." + topLevelDomain record = Records(domainOwner=zone, domain_id=zone.id, name=cNameValue, type="CNAME", content=topLevelDomain, ttl=3600, prio=0, disabled=0, auth=1) record.save() cNameValue = "ftp." + topLevelDomain record = Records(domainOwner=zone, domain_id=zone.id, name=cNameValue, type="CNAME", content=topLevelDomain, ttl=3600, prio=0, disabled=0, auth=1) record.save() ## MX Record. mxValue = "mail." + topLevelDomain record = Records(domainOwner=zone, domain_id=zone.id, name=topLevelDomain, type="MX", content=mxValue, ttl=3600, prio="10", disabled=0, auth=1) record.save() record = Records(domainOwner=zone, domain_id=zone.id, name=mxValue, type="A", content=ipAddress, ttl=3600, prio=0, disabled=0, auth=1) record.save() ## TXT Records for mail record = Records(domainOwner=zone, domain_id=zone.id, name=topLevelDomain, type="TXT", content="v=spf1 a mx ip4:" + ipAddress + " ~all", ttl=3600, prio=0, disabled=0, auth=1) record.save() record = Records(domainOwner=zone, domain_id=zone.id, name="_dmarc." + topLevelDomain, type="TXT", content="v=DMARC1; p=none", ttl=3600, prio=0, disabled=0, auth=1) record.save() record = Records(domainOwner=zone, domain_id=zone.id, name="_domainkey." + topLevelDomain, type="TXT", content="t=y; o=~;", ttl=3600, prio=0, disabled=0, auth=1) record.save() ## Creating sub-domain level record. zone = Domains.objects.get(name=topLevelDomain) actualSubDomain = subDomain + "." + topLevelDomain ## Main A record. DNS.createDNSRecord(zone, actualSubDomain, "A", ipAddress, 0, 3600) # CNAME Records. cNameValue = "www." + actualSubDomain DNS.createDNSRecord(zone, cNameValue, "CNAME", actualSubDomain, 0, 3600) except BaseException, msg: logging.CyberCPLogFileWriter.writeToFile( "We had errors while creating DNS records for: " + domain + ". Error message: " + str(msg))