Files
Homelab-manager/web/src/components/DnsZoneEditor.tsx
T
bobbanandClaude Sonnet 5.5 6f144b4e36 Edit Technitium zones: switch off and on, and their options
An Edit zone panel on the open zone of a Technitium provider switches the zone
off and on (its records stay) and edits who may ask it (query access), who may
copy it (zone transfer, with the TSIG keys the server has) and who is told when
it changes (notify). Only the groups that apply to the kind of zone are shown,
a mode that goes by a list needs it, and entries must be addresses or networks;
only groups that differ are sent, each whole, and the zone is read again
afterwards so anything else that moved is named on the page and in the audit
log. The server's own zones are not touched. Administrators only, audited.
Zones in the list show when they are off and what kind they are.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-01 23:20:19 +01:00

240 lines
11 KiB
TypeScript

import { useEffect, useState, type FormEvent } from "react";
import { api, type DnsZone, type DnsZoneOptionsChange, type DnsZoneOptionsInfo } from "../api/client";
import { ErrorNotice } from "./Notice";
import { confirmDialog } from "../utils/dialogs";
import { readableError } from "../utils/errors";
import { toast } from "../utils/toast";
const QUERY_ACCESS: [string, string][] = [
["Allow", "Everyone"],
["Deny", "Nobody"],
["AllowOnlyPrivateNetworks", "Only private networks"],
["AllowOnlyZoneNameServers", "Only the zone's name servers"],
["UseSpecifiedNetworkACL", "Only the networks listed"],
["AllowZoneNameServersAndUseSpecifiedNetworkACL", "The zone's name servers and the networks listed"],
];
const ZONE_TRANSFER: [string, string][] = [
["Deny", "Nobody"],
["Allow", "Everyone"],
["AllowOnlyZoneNameServers", "Only the zone's name servers"],
["UseSpecifiedNetworkACL", "Only the networks listed"],
["AllowZoneNameServersAndUseSpecifiedNetworkACL", "The zone's name servers and the networks listed"],
];
const NOTIFY: [string, string][] = [
["None", "Nobody"],
["ZoneNameServers", "The zone's name servers"],
["SpecifiedNameServers", "Only the servers listed"],
["BothZoneAndSpecifiedNameServers", "The zone's name servers and the servers listed"],
];
const usesAcl = (mode: string) => mode === "UseSpecifiedNetworkACL" || mode === "AllowZoneNameServersAndUseSpecifiedNetworkACL";
const usesServers = (mode: string) => mode === "SpecifiedNameServers" || mode === "BothZoneAndSpecifiedNameServers";
/** What was typed as a list: one to a line, or separated by commas or spaces. */
const listOf = (text: string) => text.split(/[\s,]+/).map((s) => s.trim()).filter(Boolean);
const NAMES: Record<string, string> = { queryAccess: "who may ask", zoneTransfer: "who may copy it", notify: "who is told" };
function Choice({ id, label, value, options, onChange }: { id: string; label: string; value: string; options: [string, string][]; onChange: (v: string) => void }) {
return (
<div className="mb-2">
<label className="form-label" htmlFor={id}>
{label}
</label>
<select id={id} className="form-select form-select-sm" value={value} onChange={(e) => onChange(e.target.value)}>
{options.map(([v, text]) => (
<option key={v} value={v}>
{text}
</option>
))}
</select>
</div>
);
}
/**
* Editing one zone of a provider that can (Technitium): switching it off and on, and who may ask it, who may copy it and who is told when it changes. It reads the zone's options when it opens;
* what Save sends is checked against the kind of zone, and if the server also changed something that was not asked, this says so. Only the groups that apply to the kind of zone are shown.
*/
export default function DnsZoneEditor({ providerId, zone, onChanged, onClose }: { providerId: number; zone: DnsZone; onChanged: () => void; onClose: () => void }) {
const [options, setOptions] = useState<DnsZoneOptionsInfo | null>(null);
const [error, setError] = useState<string | null>(null);
const [busy, setBusy] = useState(false);
const [unexpected, setUnexpected] = useState<string[]>([]);
const [queryAccess, setQueryAccess] = useState("Allow");
const [queryAcl, setQueryAcl] = useState("");
const [transfer, setTransfer] = useState("Deny");
const [transferAcl, setTransferAcl] = useState("");
const [keys, setKeys] = useState<string[]>([]);
const [notify, setNotify] = useState("None");
const [notifyServers, setNotifyServers] = useState("");
const fill = (o: DnsZoneOptionsInfo) => {
setOptions(o);
setQueryAccess(o.queryAccess ?? "Allow");
setQueryAcl(o.queryAccessNetworkACL.join("\n"));
setTransfer(o.zoneTransfer ?? "Deny");
setTransferAcl(o.zoneTransferNetworkACL.join("\n"));
setKeys(o.zoneTransferTsigKeyNames);
setNotify(o.notify ?? "None");
setNotifyServers(o.notifyNameServers.join("\n"));
};
useEffect(() => {
setOptions(null);
setError(null);
setUnexpected([]);
api.dns.zones
.options(providerId, zone.id)
.then((r) => fill(r.options))
.catch((err) => setError(readableError(err)));
}, [providerId, zone.id]);
const type = options?.type ?? zone.type ?? "";
const hasQuery = type !== "SecondaryCatalog";
const hasTransferAndNotify = type === "Primary" || type === "Secondary";
async function switchTo(on: boolean) {
if (!options) return;
if (!on && !(await confirmDialog({ title: "Switch off zone", message: `Switch off "${zone.name}"? The server stops answering for it. Its records stay, and switching it on again brings it all back.`, confirmLabel: "Switch off", danger: true }))) return;
setBusy(true);
setError(null);
try {
await api.dns.zones.setEnabled(providerId, zone.id, on);
toast.success(`${zone.name} is ${on ? "on" : "off"}.`);
setOptions({ ...options, disabled: !on });
onChanged();
} catch (err) {
setError(readableError(err));
} finally {
setBusy(false);
}
}
async function save(e: FormEvent) {
e.preventDefault();
if (!options || busy) return;
const change: DnsZoneOptionsChange = {};
if (hasQuery) {
change.queryAccess = queryAccess;
if (usesAcl(queryAccess)) change.queryAccessNetworkACL = listOf(queryAcl);
}
if (hasTransferAndNotify) {
change.zoneTransfer = transfer;
if (usesAcl(transfer)) change.zoneTransferNetworkACL = listOf(transferAcl);
change.zoneTransferTsigKeyNames = keys;
change.notify = notify;
if (usesServers(notify)) change.notifyNameServers = listOf(notifyServers);
}
setBusy(true);
setError(null);
setUnexpected([]);
try {
const r = await api.dns.zones.updateOptions(providerId, zone.id, change);
if (r.options) fill(r.options);
setUnexpected(r.unexpected);
toast.success(r.changed.length === 0 ? "Nothing was different." : `Saved: ${r.changed.map((c) => NAMES[c] ?? c).join(", ")}.`);
} catch (err) {
setError(readableError(err));
} finally {
setBusy(false);
}
}
return (
<div className="card-body border-bottom">
<div className="d-flex align-items-center mb-2">
<h4 className="mb-0">Edit {zone.name}</h4>
<button type="button" className="btn btn-sm btn-link text-secondary ms-auto" onClick={onClose}>
Close
</button>
</div>
<ErrorNotice error={error} />
{!options && !error && <div className="text-secondary small">Reading how the zone is set…</div>}
{options && (
<>
<div className="d-flex flex-wrap align-items-center gap-2 mb-3">
<span>
{type ? `${type} zone — ` : ""}
{options.disabled ? <span className="text-yellow">switched off: the server does not answer for it.</span> : "on."}
</span>
<button type="button" className={`btn btn-sm ${options.disabled ? "btn-primary" : "btn-outline-secondary"}`} disabled={busy} onClick={() => void switchTo(options.disabled)}>
{options.disabled ? "Switch on" : "Switch off"}
</button>
</div>
{!hasQuery && !hasTransferAndNotify ? (
<div className="text-secondary small">There are no options to edit on this kind of zone from here.</div>
) : (
<form onSubmit={save}>
{hasQuery && (
<fieldset className="mb-3">
<legend className="fs-5">Who may ask this zone</legend>
<Choice id="zopt-query" label="Questions about it are answered for" value={queryAccess} options={QUERY_ACCESS} onChange={setQueryAccess} />
{usesAcl(queryAccess) && (
<div className="mb-2">
<label className="form-label" htmlFor="zopt-query-acl">
Networks
</label>
<textarea id="zopt-query-acl" className="form-control form-control-sm" rows={3} placeholder={"192.168.1.0/24\n!192.168.1.66"} value={queryAcl} onChange={(e) => setQueryAcl(e.target.value)} />
<div className="text-secondary small">One to a line, in order: an address or a network, with ! in front to refuse it. Anyone not listed is refused.</div>
</div>
)}
</fieldset>
)}
{hasTransferAndNotify && (
<>
<fieldset className="mb-3">
<legend className="fs-5">Who may copy this zone</legend>
<Choice id="zopt-transfer" label="A zone transfer is given to" value={transfer} options={ZONE_TRANSFER} onChange={setTransfer} />
{usesAcl(transfer) && (
<div className="mb-2">
<label className="form-label" htmlFor="zopt-transfer-acl">
Networks
</label>
<textarea id="zopt-transfer-acl" className="form-control form-control-sm" rows={3} value={transferAcl} onChange={(e) => setTransferAcl(e.target.value)} />
</div>
)}
<div className="mb-1">
<div className="form-label">Only with one of these TSIG keys</div>
{options.availableTsigKeyNames.length === 0 ? (
<div className="text-secondary small">The server has no TSIG keys, so a transfer is not limited by one.</div>
) : (
options.availableTsigKeyNames.map((k) => (
<label key={k} className="form-check form-check-inline">
<input type="checkbox" className="form-check-input" checked={keys.includes(k)} onChange={() => setKeys((cur) => (cur.includes(k) ? cur.filter((x) => x !== k) : [...cur, k]))} />
<span className="form-check-label">{k}</span>
</label>
))
)}
</div>
</fieldset>
<fieldset className="mb-3">
<legend className="fs-5">Who is told when it changes</legend>
<Choice id="zopt-notify" label="A notification goes to" value={notify} options={NOTIFY} onChange={setNotify} />
{usesServers(notify) && (
<div className="mb-2">
<label className="form-label" htmlFor="zopt-notify-servers">
Servers
</label>
<textarea id="zopt-notify-servers" className="form-control form-control-sm" rows={2} placeholder="10.0.0.2" value={notifyServers} onChange={(e) => setNotifyServers(e.target.value)} />
<div className="text-secondary small">Addresses, one to a line.</div>
</div>
)}
</fieldset>
</>
)}
{unexpected.length > 0 && (
<div className="alert alert-warning small">
Saved, but Technitium also changed something that was not asked: <strong>{unexpected.join(", ")}</strong>. Look at the zone in its own console.
</div>
)}
<button type="submit" className="btn btn-sm btn-primary" disabled={busy}>
{busy ? "Saving…" : "Save"}
</button>
</form>
)}
</>
)}
</div>
);
}