Skip to content
Permalink
Browse files
fix dkim: Shouldn’t be different for domain and subdomain (causes iss…
…ue while sending mail)
  • Loading branch information
usmannasir committed May 19, 2020
1 parent 4d92f10 commit 860fff09a3b3e97018bb35f40d878d5f9aca20d2
Showing 3 changed files with 97 additions and 30 deletions.
@@ -652,6 +652,12 @@ def fetchDKIMKeys(self):
return ACLManager.loadError()

try:

import tldextract

extractDomain = tldextract.extract(domainName)
domainName = extractDomain.domain + '.' + extractDomain.suffix

path = "/etc/opendkim/keys/" + domainName + "/default.txt"
command = "sudo cat " + path
output = ProcessUtilities.outputExecutioner(command, 'opendkim')
@@ -121,7 +121,6 @@ def dnsTemplate(domain, admin):
subDomain = extractDomain.subdomain

if len(subDomain) == 0:

if Domains.objects.filter(name=topLevelDomain).count() == 0:
try:
pdns = PDNSStatus.objects.get(pk=1)
@@ -440,12 +439,66 @@ def dnsTemplate(domain, admin):

DNS.createDNSRecord(zone, actualSubDomain, "A", ipAddress, 0, 3600)

## Mail Record

DNS.createDNSRecord(zone, 'mail.' + actualSubDomain, "A", ipAddress, 0, 3600)

# CNAME Records.

cNameValue = "www." + actualSubDomain

DNS.createDNSRecord(zone, cNameValue, "CNAME", actualSubDomain, 0, 3600)

## MX Records

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()

## 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()

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()

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()

if ProcessUtilities.decideDistro() == ProcessUtilities.ubuntu or ProcessUtilities.decideDistro() == ProcessUtilities.ubuntu20:
command = 'sudo systemctl restart pdns'
ProcessUtilities.executioner(command)
@@ -465,6 +518,7 @@ def createDKIMRecords(domain):

extractDomain = tldextract.extract(domain)
topLevelDomain = extractDomain.domain + '.' + extractDomain.suffix
subDomain = extractDomain.subdomain

zone = Domains.objects.get(name=topLevelDomain)

@@ -487,9 +541,18 @@ def createDKIMRecords(domain):
auth=1)
record.save()

if ProcessUtilities.decideDistro() == ProcessUtilities.ubuntu or ProcessUtilities.decideDistro() == ProcessUtilities.ubuntu20:
command = ' systemctl restart pdns'
ProcessUtilities.executioner(command)
if len(subDomain) > 0:
if Records.objects.filter(domainOwner=zone, name="default._domainkey." + domain).count() == 0:
record = Records(domainOwner=zone,
domain_id=zone.id,
name="default._domainkey." + domain,
type="TXT",
content=output[leftIndex:rightIndex],
ttl=3600,
prio=0,
disabled=0,
auth=1)
record.save()

if ProcessUtilities.decideDistro() == ProcessUtilities.ubuntu or ProcessUtilities.decideDistro() == ProcessUtilities.ubuntu20:
command = ' systemctl restart pdns'
@@ -214,41 +214,39 @@ def setupDKIM(virtualHostName):

import tldextract

#extractDomain = tldextract.extract(virtualHostName)
#virtualHostName = extractDomain.domain + '.' + extractDomain.suffix
actualDomain = virtualHostName
extractDomain = tldextract.extract(virtualHostName)
virtualHostName = extractDomain.domain + '.' + extractDomain.suffix

if os.path.exists("/etc/opendkim/keys/" + virtualHostName + "/default.txt"):
return 1, "None"
if not os.path.exists("/etc/opendkim/keys/" + virtualHostName + "/default.txt"):
path = '/etc/opendkim/keys/%s' % (virtualHostName)
command = 'mkdir %s' % (path)
ProcessUtilities.normalExecutioner(command)

## Generate keys

path = '/etc/opendkim/keys/%s' % (virtualHostName)
command = 'mkdir %s' % (path)
ProcessUtilities.normalExecutioner(command)

## Generate keys

if ProcessUtilities.decideDistro() == ProcessUtilities.centos:
command = "/usr/sbin/opendkim-genkey -D /etc/opendkim/keys/%s -d %s -s default" % (virtualHostName, virtualHostName)
else:
command = "opendkim-genkey -D /etc/opendkim/keys/%s -d %s -s default" % (
virtualHostName, virtualHostName)
ProcessUtilities.normalExecutioner(command)
## Fix permissions
if ProcessUtilities.decideDistro() == ProcessUtilities.centos:
command = "/usr/sbin/opendkim-genkey -D /etc/opendkim/keys/%s -d %s -s default" % (virtualHostName, virtualHostName)
else:
command = "opendkim-genkey -D /etc/opendkim/keys/%s -d %s -s default" % (
virtualHostName, virtualHostName)
ProcessUtilities.normalExecutioner(command)
## Fix permissions

command = "chown -R root:opendkim /etc/opendkim/keys/" + virtualHostName
ProcessUtilities.normalExecutioner(command)
command = "chown -R root:opendkim /etc/opendkim/keys/" + virtualHostName
ProcessUtilities.normalExecutioner(command)

command = "chmod 640 /etc/opendkim/keys/" + virtualHostName + "/default.private"
ProcessUtilities.normalExecutioner(command)
command = "chmod 640 /etc/opendkim/keys/" + virtualHostName + "/default.private"
ProcessUtilities.normalExecutioner(command)

command = "chmod 644 /etc/opendkim/keys/" + virtualHostName + "/default.txt"
ProcessUtilities.normalExecutioner(command)
command = "chmod 644 /etc/opendkim/keys/" + virtualHostName + "/default.txt"
ProcessUtilities.normalExecutioner(command)

## Edit key file


keyTable = "/etc/opendkim/KeyTable"
configToWrite = "default._domainkey." + virtualHostName + " " + virtualHostName + ":default:/etc/opendkim/keys/" + virtualHostName + "/default.private\n"
configToWrite = "default._domainkey." + actualDomain + " " + actualDomain + ":default:/etc/opendkim/keys/" + virtualHostName + "/default.private\n"

writeToFile = open(keyTable, 'a')
writeToFile.write(configToWrite)
@@ -257,7 +255,7 @@ def setupDKIM(virtualHostName):
## Edit signing table

signingTable = "/etc/opendkim/SigningTable"
configToWrite = "*@" + virtualHostName + " default._domainkey." + virtualHostName + "\n"
configToWrite = "*@" + actualDomain + " default._domainkey." + actualDomain + "\n"

writeToFile = open(signingTable, 'a')
writeToFile.write(configToWrite)
@@ -266,7 +264,7 @@ def setupDKIM(virtualHostName):
## Trusted hosts

trustedHosts = "/etc/opendkim/TrustedHosts"
configToWrite = virtualHostName + "\n"
configToWrite = actualDomain + "\n"

writeToFile = open(trustedHosts, 'a')
writeToFile.write(configToWrite)

0 comments on commit 860fff0

Please sign in to comment.