2026-11-22 17:51:05 +01:00
|
|
|
import argparse
|
|
|
|
|
import grp
|
|
|
|
|
import json
|
|
|
|
|
import os
|
|
|
|
|
import subprocess
|
2026-12-03 01:09:42 +01:00
|
|
|
from contextlib import suppress
|
2026-11-22 17:51:05 +01:00
|
|
|
from pathlib import Path
|
|
|
|
|
|
|
|
|
|
from . import engine, roles
|
|
|
|
|
from .config import InstallError, address, platform_settings
|
|
|
|
|
|
|
|
|
|
BIND = Path("/etc/bind")
|
|
|
|
|
KEY_PATH = BIND / "coulobre-transfer.key"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def configuration(role, peer):
|
|
|
|
|
from coulobre.dns_catalog import CATALOG, KEY, PATH
|
|
|
|
|
|
|
|
|
|
server = f'{peer} key "{KEY}";'
|
|
|
|
|
if role == "primary":
|
|
|
|
|
options = (
|
|
|
|
|
f' allow-transfer {{ key "{KEY}"; }};\n'
|
|
|
|
|
f" also-notify {{ {server} }};\n"
|
|
|
|
|
)
|
|
|
|
|
zone = (
|
|
|
|
|
f'zone "{CATALOG}" {{\n'
|
|
|
|
|
f' type primary; file "{PATH}";\n'
|
|
|
|
|
f' allow-query {{ key "{KEY}"; }};\n'
|
|
|
|
|
"};\n"
|
|
|
|
|
)
|
|
|
|
|
else:
|
|
|
|
|
options = (
|
|
|
|
|
" allow-transfer { none; };\n"
|
|
|
|
|
f' allow-notify {{ key "{KEY}"; }};\n'
|
|
|
|
|
f' catalog-zones {{ zone "{CATALOG}" '
|
|
|
|
|
f"default-primaries {{ {server} }} "
|
|
|
|
|
'zone-directory "/var/cache/bind" min-update-interval 5; };\n'
|
|
|
|
|
)
|
|
|
|
|
zone = (
|
|
|
|
|
f'zone "{CATALOG}" {{\n'
|
|
|
|
|
f" type secondary; primaries {{ {server} }};\n"
|
|
|
|
|
' file "/var/cache/bind/coulobre-catalog";\n'
|
|
|
|
|
" allow-query { none; };\n"
|
|
|
|
|
"};\n"
|
|
|
|
|
)
|
|
|
|
|
base = roles.BIND_CONFIG.replace(
|
|
|
|
|
" allow-transfer { none; };\n", options
|
|
|
|
|
)
|
|
|
|
|
return f'include "{KEY_PATH}";\n' + base + zone
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def seed_catalog():
|
|
|
|
|
from coulobre.dns_catalog import PATH, render_catalog
|
|
|
|
|
from coulobre.dns_node_apply import (
|
|
|
|
|
ZONE_DIRECTORY,
|
|
|
|
|
check_registration,
|
|
|
|
|
file_serial,
|
|
|
|
|
zone_configuration,
|
|
|
|
|
)
|
|
|
|
|
from coulobre.domain_names import canonical_domain
|
|
|
|
|
from coulobre.node_files import read_root_file
|
|
|
|
|
|
|
|
|
|
engine.root_directory(PATH.parent)
|
|
|
|
|
if PATH.exists():
|
|
|
|
|
engine.root_file(PATH, 0o644)
|
|
|
|
|
return
|
|
|
|
|
members = set()
|
|
|
|
|
for path in ZONE_DIRECTORY.iterdir():
|
|
|
|
|
if not path.is_file() or path.name.startswith("."):
|
|
|
|
|
continue
|
|
|
|
|
domain = canonical_domain(path.name)
|
|
|
|
|
file_serial(domain, read_root_file(path, mode=0o644))
|
|
|
|
|
if check_registration(domain, zone_configuration(path)):
|
|
|
|
|
members.add(domain)
|
|
|
|
|
engine.put_file(PATH, render_catalog(1, members))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def setup(role, peer):
|
|
|
|
|
from coulobre.dns_catalog import PATH
|
|
|
|
|
from coulobre.dns_node_apply import ZONE_DIRECTORY
|
|
|
|
|
from coulobre.node_files import locked_root_directory
|
|
|
|
|
|
|
|
|
|
with locked_root_directory(ZONE_DIRECTORY):
|
|
|
|
|
seeded = PATH.exists()
|
|
|
|
|
try:
|
|
|
|
|
install_configuration(role, peer)
|
|
|
|
|
except (OSError, ValueError, InstallError):
|
|
|
|
|
if not seeded and role == "primary":
|
|
|
|
|
PATH.unlink(missing_ok=True)
|
|
|
|
|
raise
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def install_configuration(role, peer):
|
2026-12-03 01:09:42 +01:00
|
|
|
from coulobre.dns_catalog import PATH
|
|
|
|
|
|
2026-11-22 17:51:05 +01:00
|
|
|
root = BIND / "named.conf"
|
|
|
|
|
engine.root_file(root, 0o644)
|
|
|
|
|
current = root.read_text()
|
|
|
|
|
wanted = configuration(role, peer)
|
|
|
|
|
if current not in (roles.BIND_CONFIG, wanted):
|
|
|
|
|
raise InstallError(
|
|
|
|
|
"Use a dedicated Coulobre-managed DNS configuration."
|
|
|
|
|
)
|
|
|
|
|
engine.root_file(KEY_PATH, 0o640)
|
|
|
|
|
if KEY_PATH.stat().st_gid != grp.getgrnam("bind").gr_gid:
|
|
|
|
|
raise InstallError("Give the bind group read access to the TSIG key.")
|
|
|
|
|
if role == "primary":
|
|
|
|
|
seed_catalog()
|
|
|
|
|
else:
|
|
|
|
|
from coulobre.dns_node_apply import ZONE_DIRECTORY
|
|
|
|
|
|
|
|
|
|
if any(
|
|
|
|
|
path.is_file() and not path.name.startswith(".")
|
|
|
|
|
for path in ZONE_DIRECTORY.iterdir()
|
|
|
|
|
):
|
|
|
|
|
raise InstallError(
|
|
|
|
|
"Use a separate DNS host without managed primary zones."
|
|
|
|
|
)
|
2026-12-03 01:09:42 +01:00
|
|
|
group = root.stat().st_gid
|
|
|
|
|
engine.put_file(root, wanted, group=group, update=True)
|
|
|
|
|
new_override = None
|
2026-11-22 17:51:05 +01:00
|
|
|
try:
|
|
|
|
|
engine.run(
|
|
|
|
|
"named-checkconf", label="Validate the DNS catalog configuration"
|
|
|
|
|
)
|
2026-12-03 01:09:42 +01:00
|
|
|
if role == "primary":
|
|
|
|
|
directory = engine.SYSTEMD / "coulobre-dns-apply@.service.d"
|
|
|
|
|
engine.root_directory(directory)
|
|
|
|
|
override = directory / "catalog.conf"
|
|
|
|
|
if not os.path.lexists(override):
|
|
|
|
|
new_override = override
|
|
|
|
|
engine.put_file(
|
|
|
|
|
override, f"[Service]\nReadWritePaths={PATH.parent}\n"
|
|
|
|
|
)
|
|
|
|
|
engine.systemctl("daemon-reload")
|
2026-11-22 17:51:05 +01:00
|
|
|
engine.run(
|
|
|
|
|
"rndc",
|
|
|
|
|
"-s",
|
|
|
|
|
"127.0.0.1",
|
|
|
|
|
"reconfig",
|
|
|
|
|
label="Load the DNS catalog configuration",
|
|
|
|
|
)
|
2026-12-03 01:09:42 +01:00
|
|
|
except (OSError, InstallError):
|
|
|
|
|
if new_override is not None and new_override.exists():
|
|
|
|
|
new_override.unlink()
|
|
|
|
|
with suppress(InstallError):
|
|
|
|
|
engine.systemctl("daemon-reload")
|
|
|
|
|
engine.put_file(root, current, group=group, update=True)
|
2026-11-22 17:51:05 +01:00
|
|
|
raise
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
|
parser = argparse.ArgumentParser(
|
|
|
|
|
description="Configure a Coulobre DNS catalog node."
|
|
|
|
|
)
|
|
|
|
|
parser.add_argument("role", choices=("primary", "secondary"))
|
|
|
|
|
parser.add_argument(
|
|
|
|
|
"peer", help="The other node address used for DNS transfers"
|
|
|
|
|
)
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
try:
|
|
|
|
|
if os.geteuid() != 0:
|
|
|
|
|
raise InstallError("Run DNS catalog setup as root.")
|
|
|
|
|
platform_settings()
|
|
|
|
|
peer = address(args.peer, "the other DNS node")
|
|
|
|
|
addresses = json.loads(
|
|
|
|
|
engine.run("ip", "-j", "address", label="Check DNS node addresses")
|
|
|
|
|
)
|
|
|
|
|
if any(
|
|
|
|
|
peer == item.get("local")
|
|
|
|
|
for interface in addresses
|
|
|
|
|
for item in interface.get("addr_info", ())
|
|
|
|
|
):
|
|
|
|
|
raise InstallError("Choose a DNS peer on a different host.")
|
|
|
|
|
if args.role == "secondary" and (
|
|
|
|
|
BIND / "named.conf"
|
|
|
|
|
).read_text() != configuration(args.role, peer):
|
|
|
|
|
roles.configure_dns()
|
|
|
|
|
setup(args.role, peer)
|
|
|
|
|
except (
|
|
|
|
|
InstallError,
|
|
|
|
|
OSError,
|
|
|
|
|
ValueError,
|
|
|
|
|
subprocess.SubprocessError,
|
|
|
|
|
) as error:
|
|
|
|
|
parser.exit(1, f"Coulobre DNS catalog: {error}\n")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
main()
|