From 76143061fd6506918d03c979ec56a40a04b7fce5 Mon Sep 17 00:00:00 2001 From: usmannasir Date: Tue, 23 Jan 2024 16:22:09 +0500 Subject: [PATCH] bug fix: avoid creation of duplicate dns records https://github.com/usmannasir/cyberpanel/issues/1190 --- plogical/dnsUtilities.py | 536 ++++++++++++++++++++++---------------- plogical/mailUtilities.py | 3 + 2 files changed, 310 insertions(+), 229 deletions(-) diff --git a/plogical/dnsUtilities.py b/plogical/dnsUtilities.py index 46b0deb59..23506ed6b 100755 --- a/plogical/dnsUtilities.py +++ b/plogical/dnsUtilities.py @@ -201,118 +201,136 @@ def dnsTemplate(domain, admin): 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() + # soaRecord = Records(domainOwner=zone, + # domain_id=zone.id, + # name=topLevelDomain, + # type="SOA", + # content=content, + # ttl=3600, + # prio=0, + # disabled=0, + # auth=1) + # soaRecord.save() + + DNS.createDNSRecord(zone, topLevelDomain, "SOA", content, 0, 3600) ## 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() + # record = Records(domainOwner=zone, + # domain_id=zone.id, + # name=topLevelDomain, + # type="A", + # content=ipAddress, + # ttl=3600, + # prio=0, + # disabled=0, + # auth=1) + # record.save() + + DNS.createDNSRecord(zone, topLevelDomain, "A", ipAddress, 0, 3600) # 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() + # record = Records(domainOwner=zone, + # domain_id=zone.id, + # name=cNameValue, + # type="CNAME", + # content=topLevelDomain, + # ttl=3600, + # prio=0, + # disabled=0, + # auth=1) + # record.save() + + DNS.createDNSRecord(zone, cNameValue, "CNAME", topLevelDomain, 0, 3600) 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() + # record = Records(domainOwner=zone, + # domain_id=zone.id, + # name=cNameValue, + # type="CNAME", + # content=topLevelDomain, + # ttl=3600, + # prio=0, + # disabled=0, + # auth=1) + # record.save() + + DNS.createDNSRecord(zone, cNameValue, "CNAME", topLevelDomain, 0, 3600) ## 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() + # record = Records(domainOwner=zone, + # domain_id=zone.id, + # name=topLevelDomain, + # type="MX", + # content=mxValue, + # ttl=3600, + # prio="10", + # disabled=0, + # auth=1) + # record.save() + + DNS.createDNSRecord(zone, topLevelDomain, "MX", mxValue, 10, 3600) + + # record = Records(domainOwner=zone, + # domain_id=zone.id, + # name=mxValue, + # type="A", + # content=ipAddress, + # ttl=3600, + # prio=0, + # disabled=0, + # auth=1) + # record.save() + + DNS.createDNSRecord(zone, mxValue, "A", ipAddress, 0, 3600) ## 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() + # 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() + + DNS.createDNSRecord(zone, topLevelDomain, "TXT", "v=spf1 a mx ip4:" + ipAddress + " ~all", 0, 3600) + + # 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() + + DNS.createDNSRecord(zone, "_dmarc." + topLevelDomain, "TXT", "v=DMARC1; p=none", 0, 3600) + + # 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() + + DNS.createDNSRecord(zone, "_domainkey." + topLevelDomain, "TXT", "t=y; o=~;", 0, 3600) else: if Domains.objects.filter(name=topLevelDomain).count() == 0: try: @@ -328,118 +346,136 @@ def dnsTemplate(domain, admin): 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() + # soaRecord = Records(domainOwner=zone, + # domain_id=zone.id, + # name=topLevelDomain, + # type="SOA", + # content=content, + # ttl=3600, + # prio=0, + # disabled=0, + # auth=1) + # soaRecord.save() + + DNS.createDNSRecord(zone, topLevelDomain, "SOA", content, 0, 3600) ## 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() + # record = Records(domainOwner=zone, + # domain_id=zone.id, + # name=topLevelDomain, + # type="A", + # content=ipAddress, + # ttl=3600, + # prio=0, + # disabled=0, + # auth=1) + # record.save() + + DNS.createDNSRecord(zone, topLevelDomain, "A", ipAddress, 0, 3600) # 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() + # record = Records(domainOwner=zone, + # domain_id=zone.id, + # name=cNameValue, + # type="CNAME", + # content=topLevelDomain, + # ttl=3600, + # prio=0, + # disabled=0, + # auth=1) + # record.save() + + DNS.createDNSRecord(zone, cNameValue, "CNAME", topLevelDomain, 0, 3600) 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() + # record = Records(domainOwner=zone, + # domain_id=zone.id, + # name=cNameValue, + # type="CNAME", + # content=topLevelDomain, + # ttl=3600, + # prio=0, + # disabled=0, + # auth=1) + # record.save() + + DNS.createDNSRecord(zone, cNameValue, "CNAME", topLevelDomain, 0, 3600) ## 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() + # record = Records(domainOwner=zone, + # domain_id=zone.id, + # name=topLevelDomain, + # type="MX", + # content=mxValue, + # ttl=3600, + # prio="10", + # disabled=0, + # auth=1) + # record.save() + + DNS.createDNSRecord(zone, mxValue, "MX", mxValue, 10, 3600) + + # record = Records(domainOwner=zone, + # domain_id=zone.id, + # name=mxValue, + # type="A", + # content=ipAddress, + # ttl=3600, + # prio=0, + # disabled=0, + # auth=1) + # record.save() + + DNS.createDNSRecord(zone, mxValue, "A", ipAddress, 0, 3600) ## 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() + # 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() + + DNS.createDNSRecord(zone, topLevelDomain, "TXT", "v=spf1 a mx ip4:" + ipAddress + " ~all", 0, 3600) + + # 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() + + DNS.createDNSRecord(zone, "_dmarc." + topLevelDomain, "TXT", "v=DMARC1; p=none", 0, 3600) + + # 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() + + DNS.createDNSRecord(zone, "_domainkey." + topLevelDomain, "TXT", "t=y; o=~;", 0, 3600) ## Creating sub-domain level record. @@ -466,51 +502,59 @@ def dnsTemplate(domain, admin): mxValue = "mail." + actualSubDomain - record = Records(domainOwner=zone, - domain_id=zone.id, - name=actualSubDomain, - type="MX", - content=mxValue, - ttl=3600, - prio="10", - disabled=0, - auth=1) - record.save() + # record = Records(domainOwner=zone, + # domain_id=zone.id, + # name=actualSubDomain, + # type="MX", + # content=mxValue, + # ttl=3600, + # prio="10", + # disabled=0, + # auth=1) + # record.save() - ## TXT Records - - record = Records(domainOwner=zone, - domain_id=zone.id, - name=actualSubDomain, - type="TXT", - content="v=spf1 a mx ip4:" + ipAddress + " ~all", - ttl=3600, - prio=0, - disabled=0, - auth=1) - record.save() + DNS.createDNSRecord(zone, actualSubDomain, "MX", mxValue, 10, 3600) - record = Records(domainOwner=zone, - domain_id=zone.id, - name="_dmarc." + actualSubDomain, - type="TXT", - content="v=DMARC1; p=none", - ttl=3600, - prio=0, - disabled=0, - auth=1) - record.save() + ## TXT Records - record = Records(domainOwner=zone, - domain_id=zone.id, - name="_domainkey." + actualSubDomain, - type="TXT", - content="t=y; o=~;", - ttl=3600, - prio=0, - disabled=0, - auth=1) - record.save() + # record = Records(domainOwner=zone, + # domain_id=zone.id, + # name=actualSubDomain, + # type="TXT", + # content="v=spf1 a mx ip4:" + ipAddress + " ~all", + # ttl=3600, + # prio=0, + # disabled=0, + # auth=1) + # record.save() + + DNS.createDNSRecord(zone, actualSubDomain, "TXT", "v=spf1 a mx ip4:" + ipAddress + " ~all", 0, 3600) + + # record = Records(domainOwner=zone, + # domain_id=zone.id, + # name="_dmarc." + actualSubDomain, + # type="TXT", + # content="v=DMARC1; p=none", + # ttl=3600, + # prio=0, + # disabled=0, + # auth=1) + # record.save() + + DNS.createDNSRecord(zone, "_dmarc." + actualSubDomain, "TXT", "v=DMARC1; p=none", 0, 3600) + + # record = Records(domainOwner=zone, + # domain_id=zone.id, + # name="_domainkey." + actualSubDomain, + # type="TXT", + # content="t=y; o=~;", + # ttl=3600, + # prio=0, + # disabled=0, + # auth=1) + # record.save() + + DNS.createDNSRecord(zone, "_domainkey." + actualSubDomain, "TXT", "t=y; o=~;", 0, 3600) if ProcessUtilities.decideDistro() == ProcessUtilities.ubuntu or ProcessUtilities.decideDistro() == ProcessUtilities.ubuntu20: command = 'sudo systemctl restart pdns' @@ -562,6 +606,12 @@ def createDKIMRecords(domain): disabled=0, auth=1) record.save() + #### in else we need to update record if new key found + else: + rcrd = Records.objects.get(domainOwner=zone, name="default._domainkey." + topLevelDomain) + rcrd.content = output[leftIndex:rightIndex] + rcrd.save() + if len(subDomain) > 0: if Records.objects.filter(domainOwner=zone, name="default._domainkey." + domain).count() == 0: @@ -575,6 +625,11 @@ def createDKIMRecords(domain): disabled=0, auth=1) record.save() + #### in else we need to update record of new key found + else: + rcrd = Records.objects.get(domainOwner=zone, name="default._domainkey." + domain) + rcrd.content = output[leftIndex:rightIndex] + rcrd.save() if ProcessUtilities.decideDistro() == ProcessUtilities.ubuntu or ProcessUtilities.decideDistro() == ProcessUtilities.ubuntu20: command = ' systemctl restart pdns' @@ -670,6 +725,29 @@ def createDNSRecord(zone, name, type, value, priority, ttl): return + if type == 'SOA': + 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 or ProcessUtilities.decideDistro() == ProcessUtilities.ubuntu20: + command = 'ls -la /etc/systemd/system/multi-user.target.wants/pdns.service' + result = ProcessUtilities.outputExecutioner(command) + + if result.find('No such file') == -1: + 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, @@ -699,7 +777,7 @@ def createDNSRecord(zone, name, type, value, priority, ttl): type=type, content=value, ttl=ttl, - prio=priority, + prio=str(priority), disabled=0, auth=1) record.save() diff --git a/plogical/mailUtilities.py b/plogical/mailUtilities.py index dba899a75..0b8fa320b 100755 --- a/plogical/mailUtilities.py +++ b/plogical/mailUtilities.py @@ -2178,12 +2178,15 @@ def installOpenDKIMNew(self): return 1 def SetupDKIMFromResetMail(self): + from plogical.dnsUtilities import DNS for website in Websites.objects.all(): mailUtilities.setupDKIM(website.domain) + DNS.createDKIMRecords(website.domain) for website in ChildDomains.objects.all(): mailUtilities.setupDKIM(website.domain) + DNS.createDKIMRecords(website.domain) def ResetEmailConfigurations(self): try: