diff --git a/docs/deployment.md b/docs/deployment.md index 2bfdfe0..e45c9da 100644 --- a/docs/deployment.md +++ b/docs/deployment.md @@ -1280,6 +1280,22 @@ Each request captures the reviewed SOA serial and records. You can edit the zone while it publishes. Publish the newer revision after the current request finishes. Use **Retry publishing** if the current revision failed. +To let the customer and the assigned reseller edit a zone's records, enable +**Delegate record editing** on that zone. They can also publish the zone when +Coulobre publishing is enabled. The operator controls the authoritative +nameservers, SOA responsible name, and automatic mail DNS setting. In +**Additional records**, enter one fully qualified name, type, and value per +line. Use `@` for the zone apex. + +```text +www.example.net CNAME example.net. +_sip._tcp.example.net SRV 10 0 5060 sip.example.net. +@ CAA 0 issue "letsencrypt.org" +``` + +Upgrade a separate BIND9 node to the same Coulobre version before publishing +zones with these records. + ## Provision local mailboxes from the panel To provision Postfix and Dovecot on another host, use the diff --git a/docs/development.md b/docs/development.md index 5071b3b..c29614b 100644 --- a/docs/development.md +++ b/docs/development.md @@ -349,6 +349,24 @@ Supply TXT values as plain JSON strings. The renderer quotes the values and splits long UTF-8 strings into DNS character strings. DKIM readers concatenate the strings in the TXT record. [RFC 1035](https://www.rfc-editor.org/rfc/rfc1035.html) and [RFC 6376](https://www.rfc-editor.org/rfc/rfc6376.html) +### Include CNAME, SRV, and CAA records in a manifest + +Version 3 adds an `extra_records` array and retains the two arrays from +version 2. The panel emits version 3 for zones that use an extra record type. +The node still accepts version 1 and 2 manifests. + +```json +{ + "name": "_sip._tcp.example.net", + "record_type": "SRV", + "value": "10 0 5060 sip.example.net." +} +``` + +Use fully qualified names for CNAME targets and SRV target hosts. Use `.` +as an SRV target for an unavailable service. A CNAME cannot share its owner +with any other record or form a loop within the zone. + ### Apply a zone manifest Install `bind9` and `bind9-utils` on the DNS node. Use BIND9's default @@ -386,7 +404,7 @@ sudo systemctl enable --now coulobre-dns-apply.socket ``` Grant the socket group only to the service identity responsible for publishing -zones. Send one version 1 or 2 zone manifest as UTF-8 JSON to +zones. Send one version 1, 2, or 3 zone manifest as UTF-8 JSON to `/run/coulobre-dns-apply.sock`, with a limit of 1 MiB. Shut down the writing half of the connection and read the JSON reply to EOF. For a valid manifest, the reply identifies the domain and SOA serial. A `configured` reply means @@ -810,8 +828,9 @@ python manage.py import_directadmin_zone user.admin.customer.tar.zst 1 python manage.py import_directadmin_zone user.admin.customer.tar.zst 1 --apply ``` -Coulobre uses its own TTL and SOA timers. Record types outside the mapping, -such as CNAME, SRV, delegated NS, and DNSSEC records, appear in the preview. +Coulobre imports A, AAAA, MX, TXT, CNAME, SRV, and CAA with its own TTL and +SOA timers. Record types outside the mapping, such as delegated NS and DNSSEC +records, appear in the preview. Use `--apply --skip-unsupported` only after deciding how the omitted records will be served. A differing destination zone blocks the import. An identical zone can be previewed or reapplied without creating another copy. Adjust diff --git a/src/coulobre/directadmin_dns.py b/src/coulobre/directadmin_dns.py index b7bb80e..5675480 100644 --- a/src/coulobre/directadmin_dns.py +++ b/src/coulobre/directadmin_dns.py @@ -4,7 +4,7 @@ import dns.zone from .directadmin import MAX_METADATA_SIZE from .directadmin_payload import read_resource -from .dns_node import AddressRecord, DnsZone, MxRecord, TxtRecord +from .dns_node import AddressRecord, DnsZone, ExtraRecord, MxRecord, TxtRecord from .domain_names import canonical_domain @@ -49,7 +49,8 @@ def convert_zone(text, domain): if master in nameservers: nameservers.remove(master) nameservers.insert(0, master) - addresses, mx_records, txt_records, unsupported = [], [], [], [] + addresses, mx_records, txt_records, extra_records = [], [], [], [] + unsupported = [] for owner, records in source.iterate_rdatasets(): name = str(owner) record_type = dns.rdatatype.to_text(records.rdtype) @@ -72,6 +73,10 @@ def convert_zone(text, domain): name, b"".join(record.strings).decode("utf-8") ) ) + case "CNAME" | "SRV" | "CAA": + extra_records.append( + ExtraRecord(name, record_type, record.to_text()) + ) case _: unsupported.append( { @@ -97,5 +102,6 @@ def convert_zone(text, domain): tuple(addresses), tuple(mx_records), tuple(txt_records), + tuple(extra_records), ) return zone, unsupported diff --git a/src/coulobre/dns_node.py b/src/coulobre/dns_node.py index 4eb6354..788f620 100644 --- a/src/coulobre/dns_node.py +++ b/src/coulobre/dns_node.py @@ -6,6 +6,9 @@ import sys from dataclasses import asdict, dataclass from pathlib import Path +import dns.exception +import dns.name +import dns.rdata import idna from .domain_names import canonical_domain @@ -14,6 +17,8 @@ from .node_retirement import RetirementRequest, parse_retirement BASE_MANIFEST_VERSION = 1 MAIL_RECORD_MANIFEST_VERSION = 2 +EXTENDED_MANIFEST_VERSION = 3 +EXTRA_RECORD_TYPES = ("CNAME", "SRV", "CAA") MANIFEST_KIND = "bind9_zone" MAX_SOA_SERIAL = 2**32 - 1 TXT_CHUNK_SIZE = 255 @@ -168,6 +173,62 @@ class TxtRecord: return f"{self.name}. IN TXT {text}" +@dataclass(frozen=True) +class ExtraRecord: + name: str + record_type: str + value: str + + def __post_init__(self): + name = txt_owner_name(self.name) + if ( + self.record_type not in EXTRA_RECORD_TYPES + or not isinstance(self.value, str) + or not self.value + or not self.value.isprintable() + ): + raise ValueError("Enter a CNAME, SRV, or CAA record value.") + if self.record_type == "SRV" and not re.match( + r"^_[a-z0-9-]+\._[a-z0-9-]+\.", name + ): + raise ValueError("SRV names start with _service._protocol.") + try: + value = dns.rdata.from_text( + "IN", + self.record_type, + self.value, + origin=dns.name.root, + relativize=False, + ) + except (dns.exception.DNSException, ValueError): + raise ValueError( + f"Invalid {self.record_type} record value." + ) from None + if self.record_type == "CNAME": + target = txt_owner_name(value.target.to_text()) + text = f"{target}." + elif self.record_type == "SRV" and value.target != dns.name.root: + target = dns_name(value.target.to_text()) + text = f"{value.priority} {value.weight} {value.port} {target}." + else: + text = value.to_text() + object.__setattr__(self, "name", name) + object.__setattr__(self, "value", text) + + @classmethod + def from_manifest(cls, data): + if not isinstance(data, dict) or data.keys() != { + "name", + "record_type", + "value", + }: + raise ValueError("Expected a DNS record name, type, and value.") + return cls(**data) + + def render(self): + return f"{self.name}. IN {self.record_type} {self.value}" + + def read_records(data, key, record_type): records = data.get(key, []) if not isinstance(records, list): @@ -184,6 +245,7 @@ class DnsZone: addresses: tuple[AddressRecord, ...] = () mx_records: tuple[MxRecord, ...] = () txt_records: tuple[TxtRecord, ...] = () + extra_records: tuple[ExtraRecord, ...] = () def __post_init__(self): object.__setattr__(self, "domain", dns_name(self.domain)) @@ -215,6 +277,37 @@ class DnsZone: "Each in-zone nameserver needs an address record." ) self.check_mail_records(address_names) + self.check_extra_records(address_names) + + def check_extra_records(self, address_names): + ordinary = ( + address_names + | {self.domain} + | {record.name for record in (*self.mx_records, *self.txt_records)} + ) + aliases = {} + for record in self.extra_records: + if not in_zone(record.name, self.domain): + raise ValueError("DNS record names must belong to the zone.") + if record.record_type == "CNAME": + if record.name in aliases: + raise ValueError("A CNAME name may only have one target.") + aliases[record.name] = txt_owner_name(record.value) + else: + ordinary.add(record.name) + if ordinary.intersection(aliases): + raise ValueError( + "A CNAME cannot share its name with another record." + ) + visited = set() + for name in aliases: + chain = set() + while name in aliases and name not in visited: + if name in chain: + raise ValueError("The CNAME records create a loop.") + chain.add(name) + name = aliases[name] + visited.update(chain) def check_mail_records(self, address_names): if any( @@ -249,6 +342,7 @@ class DnsZone: if type(version) is not int or version not in ( BASE_MANIFEST_VERSION, MAIL_RECORD_MANIFEST_VERSION, + EXTENDED_MANIFEST_VERSION, ): raise ValueError("Unsupported DNS zone manifest version.") fields = { @@ -260,8 +354,10 @@ class DnsZone: "responsible_name", "addresses", } - if version == MAIL_RECORD_MANIFEST_VERSION: + if version >= MAIL_RECORD_MANIFEST_VERSION: fields.update(("mx_records", "txt_records")) + if version == EXTENDED_MANIFEST_VERSION: + fields.add("extra_records") if data.keys() != fields: raise ValueError("Expected a BIND9 zone manifest.") if not isinstance(data["nameservers"], list): @@ -274,13 +370,18 @@ class DnsZone: addresses=read_records(data, "addresses", AddressRecord), mx_records=read_records(data, "mx_records", MxRecord), txt_records=read_records(data, "txt_records", TxtRecord), + extra_records=read_records(data, "extra_records", ExtraRecord), ) def manifest_identity(self): version = ( - MAIL_RECORD_MANIFEST_VERSION - if self.mx_records or self.txt_records - else BASE_MANIFEST_VERSION + EXTENDED_MANIFEST_VERSION + if self.extra_records + else ( + MAIL_RECORD_MANIFEST_VERSION + if self.mx_records or self.txt_records + else BASE_MANIFEST_VERSION + ) ) return { "version": version, @@ -295,11 +396,15 @@ class DnsZone: "responsible_name": self.responsible_name, "addresses": [asdict(record) for record in self.addresses], } - if manifest["version"] == MAIL_RECORD_MANIFEST_VERSION: + if manifest["version"] >= MAIL_RECORD_MANIFEST_VERSION: manifest.update( mx_records=[asdict(record) for record in self.mx_records], txt_records=[asdict(record) for record in self.txt_records], ) + if self.extra_records: + manifest["extra_records"] = [ + asdict(record) for record in self.extra_records + ] return manifest def render(self): @@ -316,6 +421,7 @@ class DnsZone: ) lines.extend(record.render() for record in self.mx_records) lines.extend(record.render() for record in self.txt_records) + lines.extend(record.render() for record in self.extra_records) return "\n".join(lines) + "\n" diff --git a/src/coulobre/hosting/access.py b/src/coulobre/hosting/access.py index 453bc22..144ed28 100644 --- a/src/coulobre/hosting/access.py +++ b/src/coulobre/hosting/access.py @@ -36,6 +36,14 @@ def is_tenant(user): ) +def manages_account(user, account): + customer = customer_for_user(user) + if customer is not None: + return account.customer_id == customer.pk + reseller = reseller_for_user(user) + return reseller is not None and account.reseller_id == reseller.pk + + def panel_access(request): customer = customer_for_user(request.user) is not None reseller = not customer and reseller_for_user(request.user) is not None diff --git a/src/coulobre/hosting/dns_forms.py b/src/coulobre/hosting/dns_forms.py index cd430f8..f0d7d67 100644 --- a/src/coulobre/hosting/dns_forms.py +++ b/src/coulobre/hosting/dns_forms.py @@ -6,6 +6,7 @@ from coulobre.dns_node import ( MAX_SOA_SERIAL, AddressRecord, DnsZone, + ExtraRecord, MxRecord, TxtRecord, ) @@ -69,6 +70,20 @@ class DnsZoneForm(forms.Form): "Coulobre DNS node." ), ) + extra_records = forms.CharField( + label="CNAME, SRV and CAA records", + required=False, + widget=forms.Textarea(attrs={"rows": 4, "spellcheck": "false"}), + help_text=( + "Enter a name, type and value per line, such as " + '@ CAA 0 issue "example.net".' + ), + ) + delegate_dns = forms.BooleanField( + label="Delegate record editing", + help_text="Allow the customer and assigned reseller to edit records.", + required=False, + ) def __init__(self, *args, domain, **kwargs): super().__init__(*args, **kwargs) @@ -100,6 +115,20 @@ class DnsZoneForm(forms.Form): def clean_txt_records(self): return self.clean_mail_records("txt_records", self.parse_txt_record) + def clean_extra_records(self): + return self.clean_mail_records( + "extra_records", self.parse_extra_record + ) + + def parse_extra_record(self, line): + try: + name, record_type, value = line.split(maxsplit=2) + except ValueError: + raise ValueError("Enter a name, type, and value.") from None + return ExtraRecord( + self.domain if name == "@" else name, record_type.upper(), value + ) + def clean_mail_records(self, field, parse): records = [] for number, line in enumerate( @@ -152,6 +181,7 @@ class DnsZoneForm(forms.Form): addresses=data["addresses"], mx_records=data["mx_records"], txt_records=data["txt_records"], + extra_records=data["extra_records"], ) except ValueError as error: raise forms.ValidationError(str(error)) from error @@ -162,6 +192,7 @@ class DnsZoneForm(forms.Form): addresses=manifest["addresses"], mx_records=manifest.get("mx_records", []), txt_records=manifest.get("txt_records", []), + extra_records=manifest.get("extra_records", []), ) return data diff --git a/src/coulobre/hosting/dns_publication.py b/src/coulobre/hosting/dns_publication.py index 92e7bce..9835c40 100644 --- a/src/coulobre/hosting/dns_publication.py +++ b/src/coulobre/hosting/dns_publication.py @@ -1,28 +1,38 @@ from django.conf import settings +from django.core.exceptions import PermissionDenied from django.db import transaction from django.db.models import Q from coulobre.dns_node import DNS_REQUEST_LIMIT +from .access import is_tenant, manages_account from .models import DnsPublication, HostingDnsZone from .node_client import encode_manifest from .resource_requests import lock_hosting_account, require_open_account -def queue_dns_publication(zone_id, expected_serial): +def queue_dns_publication(zone_id, expected_serial, *, actor=None): account_id = HostingDnsZone.objects.values_list( "account_id", flat=True ).get(pk=zone_id) - return publish_for_account(account_id, zone_id, expected_serial) + return publish_for_account( + account_id, zone_id, expected_serial, actor=actor + ) @transaction.atomic -def publish_for_account(account_id, zone_id, expected_serial): +def publish_for_account(account_id, zone_id, expected_serial, *, actor=None): account = lock_hosting_account(account_id, customer_id=None) - require_open_account(account) zone = HostingDnsZone.objects.select_for_update().get( pk=zone_id, account=account ) + if ( + actor is not None + and is_tenant(actor) + and (not zone.delegate_dns or not manages_account(actor, account)) + ): + raise PermissionDenied + require_open_account(account) if str(zone.serial) != expected_serial: raise ValueError( "This zone changed before publication. " diff --git a/src/coulobre/hosting/migrations/0043_dns_parity.py b/src/coulobre/hosting/migrations/0043_dns_parity.py new file mode 100644 index 0000000..55c2fba --- /dev/null +++ b/src/coulobre/hosting/migrations/0043_dns_parity.py @@ -0,0 +1,22 @@ +# Generated by Django 5.2.19 on 2026-11-22 09:52 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + dependencies = [ + ("hosting", "0042_website_php_versions"), + ] + + operations = [ + migrations.AddField( + model_name="hostingdnszone", + name="delegate_dns", + field=models.BooleanField(default=False), + ), + migrations.AddField( + model_name="hostingdnszone", + name="extra_records", + field=models.JSONField(default=list), + ), + ] diff --git a/src/coulobre/hosting/models.py b/src/coulobre/hosting/models.py index eb24670..b588013 100644 --- a/src/coulobre/hosting/models.py +++ b/src/coulobre/hosting/models.py @@ -23,6 +23,7 @@ from coulobre.dns_node import ( MAX_SOA_SERIAL, AddressRecord, DnsZone, + ExtraRecord, MxRecord, TxtRecord, ) @@ -533,6 +534,8 @@ class HostingDnsZone(models.Model): addresses = models.JSONField(default=list) mx_records = models.JSONField(default=list) txt_records = models.JSONField(default=list) + extra_records = models.JSONField(default=list) + delegate_dns = models.BooleanField(default=False) manage_mail_dns = models.BooleanField(default=False) class Meta: @@ -563,6 +566,9 @@ class HostingDnsZone(models.Model): txt_records=tuple( TxtRecord.from_manifest(item) for item in self.txt_records ), + extra_records=tuple( + ExtraRecord.from_manifest(item) for item in self.extra_records + ), ) diff --git a/src/coulobre/hosting/templates/hosting/account_detail.html b/src/coulobre/hosting/templates/hosting/account_detail.html index 2ea8cf7..d43af59 100644 --- a/src/coulobre/hosting/templates/hosting/account_detail.html +++ b/src/coulobre/hosting/templates/hosting/account_detail.html @@ -154,14 +154,14 @@