Skip to content

Commit 860fff0

Browse files
committed
fix dkim: Shouldn’t be different for domain and subdomain (causes issue while sending mail)
1 parent 4d92f10 commit 860fff0

3 files changed

Lines changed: 97 additions & 30 deletions

File tree

mailServer/mailserverManager.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -652,6 +652,12 @@ def fetchDKIMKeys(self):
652652
return ACLManager.loadError()
653653

654654
try:
655+
656+
import tldextract
657+
658+
extractDomain = tldextract.extract(domainName)
659+
domainName = extractDomain.domain + '.' + extractDomain.suffix
660+
655661
path = "/etc/opendkim/keys/" + domainName + "/default.txt"
656662
command = "sudo cat " + path
657663
output = ProcessUtilities.outputExecutioner(command, 'opendkim')

plogical/dnsUtilities.py

Lines changed: 67 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,6 @@ def dnsTemplate(domain, admin):
121121
subDomain = extractDomain.subdomain
122122

123123
if len(subDomain) == 0:
124-
125124
if Domains.objects.filter(name=topLevelDomain).count() == 0:
126125
try:
127126
pdns = PDNSStatus.objects.get(pk=1)
@@ -440,12 +439,66 @@ def dnsTemplate(domain, admin):
440439

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

442+
## Mail Record
443+
444+
DNS.createDNSRecord(zone, 'mail.' + actualSubDomain, "A", ipAddress, 0, 3600)
445+
443446
# CNAME Records.
444447

445448
cNameValue = "www." + actualSubDomain
446449

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

452+
## MX Records
453+
454+
mxValue = "mail." + actualSubDomain
455+
456+
record = Records(domainOwner=zone,
457+
domain_id=zone.id,
458+
name=actualSubDomain,
459+
type="MX",
460+
content=mxValue,
461+
ttl=3600,
462+
prio="10",
463+
disabled=0,
464+
auth=1)
465+
record.save()
466+
467+
## TXT Records
468+
469+
record = Records(domainOwner=zone,
470+
domain_id=zone.id,
471+
name=actualSubDomain,
472+
type="TXT",
473+
content="v=spf1 a mx ip4:" + ipAddress + " ~all",
474+
ttl=3600,
475+
prio=0,
476+
disabled=0,
477+
auth=1)
478+
record.save()
479+
480+
record = Records(domainOwner=zone,
481+
domain_id=zone.id,
482+
name="_dmarc." + actualSubDomain,
483+
type="TXT",
484+
content="v=DMARC1; p=none",
485+
ttl=3600,
486+
prio=0,
487+
disabled=0,
488+
auth=1)
489+
record.save()
490+
491+
record = Records(domainOwner=zone,
492+
domain_id=zone.id,
493+
name="_domainkey." + actualSubDomain,
494+
type="TXT",
495+
content="t=y; o=~;",
496+
ttl=3600,
497+
prio=0,
498+
disabled=0,
499+
auth=1)
500+
record.save()
501+
449502
if ProcessUtilities.decideDistro() == ProcessUtilities.ubuntu or ProcessUtilities.decideDistro() == ProcessUtilities.ubuntu20:
450503
command = 'sudo systemctl restart pdns'
451504
ProcessUtilities.executioner(command)
@@ -465,6 +518,7 @@ def createDKIMRecords(domain):
465518

466519
extractDomain = tldextract.extract(domain)
467520
topLevelDomain = extractDomain.domain + '.' + extractDomain.suffix
521+
subDomain = extractDomain.subdomain
468522

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

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

490-
if ProcessUtilities.decideDistro() == ProcessUtilities.ubuntu or ProcessUtilities.decideDistro() == ProcessUtilities.ubuntu20:
491-
command = ' systemctl restart pdns'
492-
ProcessUtilities.executioner(command)
544+
if len(subDomain) > 0:
545+
if Records.objects.filter(domainOwner=zone, name="default._domainkey." + domain).count() == 0:
546+
record = Records(domainOwner=zone,
547+
domain_id=zone.id,
548+
name="default._domainkey." + domain,
549+
type="TXT",
550+
content=output[leftIndex:rightIndex],
551+
ttl=3600,
552+
prio=0,
553+
disabled=0,
554+
auth=1)
555+
record.save()
493556

494557
if ProcessUtilities.decideDistro() == ProcessUtilities.ubuntu or ProcessUtilities.decideDistro() == ProcessUtilities.ubuntu20:
495558
command = ' systemctl restart pdns'

plogical/mailUtilities.py

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -214,41 +214,39 @@ def setupDKIM(virtualHostName):
214214

215215
import tldextract
216216

217-
#extractDomain = tldextract.extract(virtualHostName)
218-
#virtualHostName = extractDomain.domain + '.' + extractDomain.suffix
217+
actualDomain = virtualHostName
218+
extractDomain = tldextract.extract(virtualHostName)
219+
virtualHostName = extractDomain.domain + '.' + extractDomain.suffix
219220

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

226+
## Generate keys
223227

224-
path = '/etc/opendkim/keys/%s' % (virtualHostName)
225-
command = 'mkdir %s' % (path)
226-
ProcessUtilities.normalExecutioner(command)
227-
228-
## Generate keys
229-
230-
if ProcessUtilities.decideDistro() == ProcessUtilities.centos:
231-
command = "/usr/sbin/opendkim-genkey -D /etc/opendkim/keys/%s -d %s -s default" % (virtualHostName, virtualHostName)
232-
else:
233-
command = "opendkim-genkey -D /etc/opendkim/keys/%s -d %s -s default" % (
234-
virtualHostName, virtualHostName)
235-
ProcessUtilities.normalExecutioner(command)
236-
## Fix permissions
228+
if ProcessUtilities.decideDistro() == ProcessUtilities.centos:
229+
command = "/usr/sbin/opendkim-genkey -D /etc/opendkim/keys/%s -d %s -s default" % (virtualHostName, virtualHostName)
230+
else:
231+
command = "opendkim-genkey -D /etc/opendkim/keys/%s -d %s -s default" % (
232+
virtualHostName, virtualHostName)
233+
ProcessUtilities.normalExecutioner(command)
234+
## Fix permissions
237235

238-
command = "chown -R root:opendkim /etc/opendkim/keys/" + virtualHostName
239-
ProcessUtilities.normalExecutioner(command)
236+
command = "chown -R root:opendkim /etc/opendkim/keys/" + virtualHostName
237+
ProcessUtilities.normalExecutioner(command)
240238

241-
command = "chmod 640 /etc/opendkim/keys/" + virtualHostName + "/default.private"
242-
ProcessUtilities.normalExecutioner(command)
239+
command = "chmod 640 /etc/opendkim/keys/" + virtualHostName + "/default.private"
240+
ProcessUtilities.normalExecutioner(command)
243241

244-
command = "chmod 644 /etc/opendkim/keys/" + virtualHostName + "/default.txt"
245-
ProcessUtilities.normalExecutioner(command)
242+
command = "chmod 644 /etc/opendkim/keys/" + virtualHostName + "/default.txt"
243+
ProcessUtilities.normalExecutioner(command)
246244

247245
## Edit key file
248246

249247

250248
keyTable = "/etc/opendkim/KeyTable"
251-
configToWrite = "default._domainkey." + virtualHostName + " " + virtualHostName + ":default:/etc/opendkim/keys/" + virtualHostName + "/default.private\n"
249+
configToWrite = "default._domainkey." + actualDomain + " " + actualDomain + ":default:/etc/opendkim/keys/" + virtualHostName + "/default.private\n"
252250

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

259257
signingTable = "/etc/opendkim/SigningTable"
260-
configToWrite = "*@" + virtualHostName + " default._domainkey." + virtualHostName + "\n"
258+
configToWrite = "*@" + actualDomain + " default._domainkey." + actualDomain + "\n"
261259

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

268266
trustedHosts = "/etc/opendkim/TrustedHosts"
269-
configToWrite = virtualHostName + "\n"
267+
configToWrite = actualDomain + "\n"
270268

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

0 commit comments

Comments
 (0)