Skip to content

Commit e283a12

Browse files
committed
Add null SPF, DMARC, and MX records for automatically generated autoconfig, autodiscover, and mta-sts subdomains; add null MX records for custom A-record subdomains
All A/AAAA-resolvable domains that don't send or receive mail should have these null records. This simplifies the handling of domains a bit by handling automatically generated subdomains more like other domains.
1 parent e421add commit e283a12

File tree

3 files changed

+82
-85
lines changed

3 files changed

+82
-85
lines changed

CHANGELOG.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,15 @@ CHANGELOG
44
In Development
55
--------------
66

7-
* Migrate to the ECDSAP256SHA256 DNSSEC algorithm. If a DS record is set for any of your domain names that have DNS hosted on your box, you will be prompted by status checks to update the DS record.
7+
Mail:
8+
89
* Roundcube's login cookie is updated to use a new encryption algorithm (AES-256-CBC instead of DES-EDE-CBC).
910

11+
DNS:
12+
13+
* The ECDSAP256SHA256 DNSSEC algorithm is now available. If a DS record is set for any of your domain names that have DNS hosted on your box, you will be prompted by status checks to update the DS record at your convenience.
14+
* Null MX records are added for domains that do not serve mail.
15+
1016
v0.53a (May 8, 2021)
1117
--------------------
1218

management/dns_update.py

Lines changed: 74 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,14 @@ def build_zones(env):
135135
web_domains = set(get_web_domains(env))
136136
auto_domains = web_domains - set(get_web_domains(env, include_auto=False))
137137
domains |= auto_domains # www redirects not included in the initial list, see above
138+
139+
# Add ns1/ns2+PRIMARY_HOSTNAME which must also have A/AAAA records
140+
# when the box is acting as authoritative DNS server for its domains.
141+
for ns in ("ns1", "ns2"):
142+
d = ns + "." + env["PRIMARY_HOSTNAME"]
143+
domains.add(d)
144+
auto_domains.add(d)
145+
138146
domains = {
139147
domain: {
140148
"user": domain in mail_user_domains,
@@ -161,9 +169,6 @@ def build_zones(env):
161169
def build_zone(domain, domain_properties, additional_records, env, is_zone=True):
162170
records = []
163171

164-
# Skip www redirect and autoconfiguration domains here because they are set at the zone level directly.
165-
if domain_properties[domain]["auto"]: return records
166-
167172
# For top-level zones, define the authoritative name servers.
168173
#
169174
# Normally we are our own nameservers. Some TLDs require two distinct IP addresses,
@@ -173,10 +178,10 @@ def build_zone(domain, domain_properties, additional_records, env, is_zone=True)
173178
# 'False' in the tuple indicates these records would not be used if the zone
174179
# is managed outside of the box.
175180
if is_zone:
176-
# Obligatory definition of ns1.PRIMARY_HOSTNAME.
181+
# Obligatory NS record to ns1.PRIMARY_HOSTNAME.
177182
records.append((None, "NS", "ns1.%s." % env["PRIMARY_HOSTNAME"], False))
178183

179-
# Define ns2.PRIMARY_HOSTNAME or whatever the user overrides.
184+
# NS record to ns2.PRIMARY_HOSTNAME or whatever the user overrides.
180185
# User may provide one or more additional nameservers
181186
secondary_ns_list = get_secondary_dns(additional_records, mode="NS") \
182187
or ["ns2." + env["PRIMARY_HOSTNAME"]]
@@ -186,15 +191,6 @@ def build_zone(domain, domain_properties, additional_records, env, is_zone=True)
186191

187192
# In PRIMARY_HOSTNAME...
188193
if domain == env["PRIMARY_HOSTNAME"]:
189-
# Define ns1 and ns2.
190-
# 'False' in the tuple indicates these records would not be used if the zone
191-
# is managed outside of the box.
192-
records.append(("ns1", "A", env["PUBLIC_IP"], False))
193-
records.append(("ns2", "A", env["PUBLIC_IP"], False))
194-
if env.get('PUBLIC_IPV6'):
195-
records.append(("ns1", "AAAA", env["PUBLIC_IPV6"], False))
196-
records.append(("ns2", "AAAA", env["PUBLIC_IPV6"], False))
197-
198194
# Set the A/AAAA records. Do this early for the PRIMARY_HOSTNAME so that the user cannot override them
199195
# and we can provide different explanatory text.
200196
records.append((None, "A", env["PUBLIC_IP"], "Required. Sets the IP address of the box."))
@@ -249,21 +245,23 @@ def has_rec(qname, rtype, prefix=None):
249245
continue
250246
records.append((qname, rtype, value, "(Set by user.)"))
251247

252-
# Add defaults if not overridden by the user's custom settings (and not otherwise configured).
248+
# Add A/AAAA defaults if not overridden by the user's custom settings (and not otherwise configured).
253249
# Any CNAME or A record on the qname overrides A and AAAA. But when we set the default A record,
254250
# we should not cause the default AAAA record to be skipped because it thinks a custom A record
255251
# was set. So set has_rec_base to a clone of the current set of DNS settings, and don't update
256252
# during this process.
257253
has_rec_base = list(records)
254+
a_expl = "Required. May have a different value. Sets the IP address that %s resolves to for web hosting and other services besides mail. The A record must be present but its value does not affect mail delivery." % domain
255+
if domain_properties[domain]["auto"]:
256+
if domain.startswith("ns1.") or domain.startswith("ns2."): a_expl = False # omit from 'External DNS' page since this only applies if box is its own DNS server
257+
if domain.startswith("www."): a_expl = "Optional. Sets the IP address that %s resolves to so that the box can provide a redirect to the parent domain." % domain
258+
if domain.startswith("mta-sts."): a_expl = "Optional. MTA-STS Policy Host serving /.well-known/mta-sts.txt."
259+
if domain.startswith("autoconfig."): a_expl = "Provides email configuration autodiscovery support for Thunderbird Autoconfig."
260+
if domain.startswith("autodiscover."): a_expl = "Provides email configuration autodiscovery support for Z-Push ActiveSync Autodiscover."
258261
defaults = [
259-
(None, "A", env["PUBLIC_IP"], "Required. May have a different value. Sets the IP address that %s resolves to for web hosting and other services besides mail. The A record must be present but its value does not affect mail delivery." % domain),
262+
(None, "A", env["PUBLIC_IP"], a_expl),
260263
(None, "AAAA", env.get('PUBLIC_IPV6'), "Optional. Sets the IPv6 address that %s resolves to, e.g. for web hosting. (It is not necessary for receiving mail on this domain.)" % domain),
261264
]
262-
if domain_properties.get("www." + domain, {}).get("auto"):
263-
defaults += [
264-
("www", "A", env["PUBLIC_IP"], "Optional. Sets the IP address that www.%s resolves to so that the box can provide a redirect to the parent domain." % domain),
265-
("www", "AAAA", env.get('PUBLIC_IPV6'), "Optional. Sets the IPv6 address that www.%s resolves to so that the box can provide a redirect to the parent domain." % domain),
266-
]
267265
for qname, rtype, value, explanation in defaults:
268266
if value is None or value.strip() == "": continue # skip IPV6 if not set
269267
if not is_zone and qname == "www": continue # don't create any default 'www' subdomains on what are themselves subdomains
@@ -277,63 +275,40 @@ def has_rec(qname, rtype, prefix=None):
277275
# Don't pin the list of records that has_rec checks against anymore.
278276
has_rec_base = records
279277

280-
# The MX record says where email for the domain should be delivered: Here!
281-
if not has_rec(None, "MX", prefix="10 "):
282-
records.append((None, "MX", "10 %s." % env["PRIMARY_HOSTNAME"], "Required. Specifies the hostname (and priority) of the machine that handles @%s mail." % domain))
283-
284-
# SPF record: Permit the box ('mx', see above) to send mail on behalf of
285-
# the domain, and no one else.
286-
# Skip if the user has set a custom SPF record.
287-
if not has_rec(None, "TXT", prefix="v=spf1 "):
288-
records.append((None, "TXT", 'v=spf1 mx -all', "Recommended. Specifies that only the box is permitted to send @%s mail." % domain))
289-
290-
# Append the DKIM TXT record to the zone as generated by OpenDKIM.
291-
# Skip if the user has set a DKIM record already.
292-
opendkim_record_file = os.path.join(env['STORAGE_ROOT'], 'mail/dkim/mail.txt')
293-
with open(opendkim_record_file) as orf:
294-
m = re.match(r'(\S+)\s+IN\s+TXT\s+\( ((?:"[^"]+"\s+)+)\)', orf.read(), re.S)
295-
val = "".join(re.findall(r'"([^"]+)"', m.group(2)))
296-
if not has_rec(m.group(1), "TXT", prefix="v=DKIM1; "):
297-
records.append((m.group(1), "TXT", val, "Recommended. Provides a way for recipients to verify that this machine sent @%s mail." % domain))
298-
299-
# Append a DMARC record.
300-
# Skip if the user has set a DMARC record already.
301-
if not has_rec("_dmarc", "TXT", prefix="v=DMARC1; "):
302-
records.append(("_dmarc", "TXT", 'v=DMARC1; p=quarantine', "Recommended. Specifies that mail that does not originate from the box but claims to be from @%s or which does not have a valid DKIM signature is suspect and should be quarantined by the recipient's mail system." % domain))
303-
304-
# For any subdomain with an A record but no SPF or DMARC record, add strict policy records.
305-
all_resolvable_qnames = set(r[0] for r in records if r[1] in ("A", "AAAA"))
306-
for qname in all_resolvable_qnames:
307-
if not has_rec(qname, "TXT", prefix="v=spf1 "):
308-
records.append((qname, "TXT", 'v=spf1 -all', "Recommended. Prevents use of this domain name for outbound mail by specifying that no servers are valid sources for mail from @%s. If you do send email from this domain name you should either override this record such that the SPF rule does allow the originating server, or, take the recommended approach and have the box handle mail for this domain (simply add any receiving alias at this domain name to make this machine treat the domain name as one of its mail domains)." % (qname + "." + domain)))
309-
dmarc_qname = "_dmarc" + ("" if qname is None else "." + qname)
310-
if not has_rec(dmarc_qname, "TXT", prefix="v=DMARC1; "):
311-
records.append((dmarc_qname, "TXT", 'v=DMARC1; p=reject', "Recommended. Prevents use of this domain name for outbound mail by specifying that the SPF rule should be honoured for mail from @%s." % (qname + "." + domain)))
312-
313-
# Add CardDAV/CalDAV SRV records on the non-primary hostname that points to the primary hostname
314-
# for autoconfiguration of mail clients (so only domains hosting user accounts need it).
315-
# The SRV record format is priority (0, whatever), weight (0, whatever), port, service provider hostname (w/ trailing dot).
316-
if domain != env["PRIMARY_HOSTNAME"] and domain_properties[domain]["user"]:
317-
for dav in ("card", "cal"):
318-
qname = "_" + dav + "davs._tcp"
319-
if not has_rec(qname, "SRV"):
320-
records.append((qname, "SRV", "0 0 443 " + env["PRIMARY_HOSTNAME"] + ".", "Recommended. Specifies the hostname of the server that handles CardDAV/CalDAV services for email addresses on this domain."))
321-
322-
# Adds autoconfiguration A records for all domains that there are user accounts at.
323-
# This allows the following clients to automatically configure email addresses in the respective applications.
324-
# autodiscover.* - Z-Push ActiveSync Autodiscover
325-
# autoconfig.* - Thunderbird Autoconfig
278+
if domain_properties[domain]["mail"]:
279+
# The MX record says where email for the domain should be delivered: Here!
280+
if not has_rec(None, "MX", prefix="10 "):
281+
records.append((None, "MX", "10 %s." % env["PRIMARY_HOSTNAME"], "Required. Specifies the hostname (and priority) of the machine that handles @%s mail." % domain))
282+
283+
# SPF record: Permit the box ('mx', see above) to send mail on behalf of
284+
# the domain, and no one else.
285+
# Skip if the user has set a custom SPF record.
286+
if not has_rec(None, "TXT", prefix="v=spf1 "):
287+
records.append((None, "TXT", 'v=spf1 mx -all', "Recommended. Specifies that only the box is permitted to send @%s mail." % domain))
288+
289+
# Append the DKIM TXT record to the zone as generated by OpenDKIM.
290+
# Skip if the user has set a DKIM record already.
291+
opendkim_record_file = os.path.join(env['STORAGE_ROOT'], 'mail/dkim/mail.txt')
292+
with open(opendkim_record_file) as orf:
293+
m = re.match(r'(\S+)\s+IN\s+TXT\s+\( ((?:"[^"]+"\s+)+)\)', orf.read(), re.S)
294+
val = "".join(re.findall(r'"([^"]+)"', m.group(2)))
295+
if not has_rec(m.group(1), "TXT", prefix="v=DKIM1; "):
296+
records.append((m.group(1), "TXT", val, "Recommended. Provides a way for recipients to verify that this machine sent @%s mail." % domain))
297+
298+
# Append a DMARC record.
299+
# Skip if the user has set a DMARC record already.
300+
if not has_rec("_dmarc", "TXT", prefix="v=DMARC1; "):
301+
records.append(("_dmarc", "TXT", 'v=DMARC1; p=quarantine', "Recommended. Specifies that mail that does not originate from the box but claims to be from @%s or which does not have a valid DKIM signature is suspect and should be quarantined by the recipient's mail system." % domain))
302+
326303
if domain_properties[domain]["user"]:
327-
autodiscover_records = [
328-
("autodiscover", "A", env["PUBLIC_IP"], "Provides email configuration autodiscovery support for Z-Push ActiveSync Autodiscover."),
329-
("autodiscover", "AAAA", env["PUBLIC_IPV6"], "Provides email configuration autodiscovery support for Z-Push ActiveSync Autodiscover."),
330-
("autoconfig", "A", env["PUBLIC_IP"], "Provides email configuration autodiscovery support for Thunderbird Autoconfig."),
331-
("autoconfig", "AAAA", env["PUBLIC_IPV6"], "Provides email configuration autodiscovery support for Thunderbird Autoconfig.")
332-
]
333-
for qname, rtype, value, explanation in autodiscover_records:
334-
if value is None or value.strip() == "": continue # skip IPV6 if not set
335-
if not has_rec(qname, rtype):
336-
records.append((qname, rtype, value, explanation))
304+
# Add CardDAV/CalDAV SRV records on the non-primary hostname that points to the primary hostname
305+
# for autoconfiguration of mail clients (so only domains hosting user accounts need it).
306+
# The SRV record format is priority (0, whatever), weight (0, whatever), port, service provider hostname (w/ trailing dot).
307+
if domain != env["PRIMARY_HOSTNAME"]:
308+
for dav in ("card", "cal"):
309+
qname = "_" + dav + "davs._tcp"
310+
if not has_rec(qname, "SRV"):
311+
records.append((qname, "SRV", "0 0 443 " + env["PRIMARY_HOSTNAME"] + ".", "Recommended. Specifies the hostname of the server that handles CardDAV/CalDAV services for email addresses on this domain."))
337312

338313
# If this is a domain name that there are email addresses configured for, i.e. "something@"
339314
# this domain name, then the domain name is a MTA-STS (https://tools.ietf.org/html/rfc8461)
@@ -350,11 +325,9 @@ def has_rec(qname, rtype, prefix=None):
350325
# subdomain must be valid certificate for that domain. Do not set an MTA-STS policy if either
351326
# certificate in use is not valid (e.g. because it is self-signed and a valid certificate has not
352327
# yet been provisioned). Since we cannot provision a certificate without A/AAAA records, we
353-
# always set them --- only the TXT records depend on there being valid certificates.
354-
mta_sts_records = [
355-
("mta-sts", "A", env["PUBLIC_IP"], "Optional. MTA-STS Policy Host serving /.well-known/mta-sts.txt."),
356-
("mta-sts", "AAAA", env.get('PUBLIC_IPV6'), "Optional. MTA-STS Policy Host serving /.well-known/mta-sts.txt."),
357-
]
328+
# always set them (by including them in the www domains) --- only the TXT records depend on there
329+
# being valid certificates.
330+
mta_sts_records = [ ]
358331
if domain_properties[domain]["mail"] \
359332
and domain_properties[env["PRIMARY_HOSTNAME"]]["certificate-is-valid"] \
360333
and is_domain_cert_signed_and_valid("mta-sts." + domain, env):
@@ -374,10 +347,28 @@ def has_rec(qname, rtype, prefix=None):
374347
if env.get("MTA_STS_TLSRPT_RUA") and not has_rec("_smtp._tls", "TXT", prefix="v=TLSRPTv1;"):
375348
mta_sts_records.append(("_smtp._tls", "TXT", "v=TLSRPTv1; rua=" + env["MTA_STS_TLSRPT_RUA"], "Optional. Enables MTA-STS reporting."))
376349
for qname, rtype, value, explanation in mta_sts_records:
377-
if value is None or value.strip() == "": continue # skip IPV6 if not set
378350
if not has_rec(qname, rtype):
379351
records.append((qname, rtype, value, explanation))
380352

353+
# Add no-mail-here records for any qname that has an A or AAAA record
354+
# but no MX record. This would include domain itself if domain is a
355+
# non-mail domain and also may include qnames from custom DNS records.
356+
# Do this once at the end of generating a zone.
357+
if is_zone:
358+
qnames_with_a = set(qname for (qname, rtype, value, explanation) in records if rtype in ("A", "AAAA"))
359+
qnames_with_mx = set(qname for (qname, rtype, value, explanation) in records if rtype == "MX")
360+
for qname in qnames_with_a - qnames_with_mx:
361+
# Mark this domain as not sending mail with hard-fail SPF and DMARC records.
362+
d = (qname+"." if qname else "") + domain
363+
if not has_rec(qname, "TXT", prefix="v=spf1 "):
364+
records.append((qname, "TXT", 'v=spf1 -all', "Recommended. Prevents use of this domain name for outbound mail by specifying that no servers are valid sources for mail from @%s. If you do send email from this domain name you should either override this record such that the SPF rule does allow the originating server, or, take the recommended approach and have the box handle mail for this domain (simply add any receiving alias at this domain name to make this machine treat the domain name as one of its mail domains)." % d))
365+
if not has_rec("_dmarc" + ("."+qname if qname else ""), "TXT", prefix="v=DMARC1; "):
366+
records.append(("_dmarc" + ("."+qname if qname else ""), "TXT", 'v=DMARC1; p=reject', "Recommended. Prevents use of this domain name for outbound mail by specifying that the SPF rule should be honoured for mail from @%s." % d))
367+
368+
# And with a null MX record (https://explained-from-first-principles.com/email/#null-mx-record)
369+
if not has_rec(qname, "MX"):
370+
records.append((qname, "MX", '0 .', "Recommended. Prevents use of this domain name for incoming mail."))
371+
381372
# Sort the records. The None records *must* go first in the nsd zone file. Otherwise it doesn't matter.
382373
records.sort(key = lambda rec : list(reversed(rec[0].split(".")) if rec[0] is not None else ""))
383374

management/web_update.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def get_web_domains(env, include_www_redirects=True, include_auto=True, exclude_
2727
if include_auto:
2828
# Add Autoconfiguration domains for domains that there are user accounts at:
2929
# 'autoconfig.' for Mozilla Thunderbird auto setup.
30-
# 'autodiscover.' for Activesync autodiscovery.
30+
# 'autodiscover.' for ActiveSync autodiscovery (Z-Push).
3131
domains |= set('autoconfig.' + maildomain for maildomain in get_mail_domains(env, users_only=True))
3232
domains |= set('autodiscover.' + maildomain for maildomain in get_mail_domains(env, users_only=True))
3333

0 commit comments

Comments
 (0)