diff --git a/cmd/inbuxa/main.go b/cmd/inbuxa/main.go index 6188d03..286834a 100644 --- a/cmd/inbuxa/main.go +++ b/cmd/inbuxa/main.go @@ -1242,6 +1242,10 @@ func dnsCmd(args []string) int { if len(b) > 0 && b[len(b)-1] != '\n' { fmt.Println() } + if !apply.ZoneNamesHost(string(b), st.MailHost) { + fmt.Println("\n" + apply.StaleZoneHint(st.MailHost, false)) + return 0 + } fmt.Println("\nTo re-read them live from the server (a rotated DKIM key, say):") fmt.Println(" inbuxa dns --refresh --key -") return 0 @@ -1282,6 +1286,9 @@ func dnsCmd(args []string) int { if len(zone) > 0 && zone[len(zone)-1] != '\n' { fmt.Println() } + if !apply.ZoneNamesHost(zone, st.MailHost) { + fmt.Println("\n" + apply.StaleZoneHint(st.MailHost, true)) + } // Keep the saved copy in step with what the server now says. if err := os.WriteFile(zoneFile, []byte(zone), 0o644); err == nil { fmt.Printf("\n(also written to %s)\n", zoneFile) diff --git a/internal/apply/certs.go b/internal/apply/certs.go index ee24280..1f22bc3 100644 --- a/internal/apply/certs.go +++ b/internal/apply/certs.go @@ -311,6 +311,37 @@ func FetchDNSZone(ctx context.Context, st *state.State, domain, key string) (str return zone, nil } +// ZoneNamesHost is whether a zone fragment names host anywhere: as an MX, +// a service record's target, or in SPF. The records follow the server's own +// hostname, so a zone that never names this install's mail host was written +// before a rename, or by a server still called something else (#83). An MX +// pointing at a relay is fine: the service records still name the server. +func ZoneNamesHost(zone, host string) bool { + host = strings.ToLower(strings.TrimSuffix(host, ".")) + if host == "" { + return true + } + for _, f := range strings.FieldsFunc(strings.ToLower(zone), func(r rune) bool { + return r == ' ' || r == '\t' || r == '\n' || r == '"' || r == ':' || r == ';' + }) { + if strings.TrimSuffix(f, ".") == host { + return true + } + } + return false +} + +// StaleZoneHint says what to do about a zone that doesn't name the mail host. +func StaleZoneHint(host string, live bool) string { + if live { + return "These records don't name " + host + ": the server still calls itself something else. Set its\n" + + "hostname in the console (Settings, Network, General) and run this again." + } + return "These records don't name " + host + ", this install's mail host: they were saved before it\n" + + "was renamed. Once the server's hostname is " + host + " (in the console: Settings, Network,\n" + + "General), read them again with\n inbuxa dns --refresh --key -" +} + // CertsLogHint is where the server says why an order failed. func CertsLogHint(st *state.State) string { if st.Shapes["server"] == "host" { diff --git a/internal/apply/zone_test.go b/internal/apply/zone_test.go new file mode 100644 index 0000000..77a405a --- /dev/null +++ b/internal/apply/zone_test.go @@ -0,0 +1,41 @@ +// SPDX-FileCopyrightText: 2026 Coffey Labs LLC +// SPDX-License-Identifier: AGPL-3.0-or-later + +package apply + +import ( + "strings" + "testing" +) + +const savedZone = `example.com. 3600 IN MX 10 mail.example.com. +_imaps._tcp.example.com. 3600 IN SRV 0 1 993 mail.example.com. +example.com. 3600 IN TXT "v=spf1 a:mail.example.com -all" +_dmarc.example.com. 3600 IN TXT "v=DMARC1; p=reject; rua=mailto:postmaster@example.com" +` + +// #83: a zone saved before a rename doesn't name the new mail host. +func TestZoneNamesHost(t *testing.T) { + if !ZoneNamesHost(savedZone, "mail.example.com") || !ZoneNamesHost(savedZone, "MAIL.example.com.") { + t.Fatal("the zone names its own mail host") + } + if ZoneNamesHost(savedZone, "mail.example.net") { + t.Fatal("a renamed mail host isn't in the old zone") + } + // A name that only contains the host, or is contained in it, doesn't count. + if ZoneNamesHost(savedZone, "example") || ZoneNamesHost(savedZone, "ail.example.com") { + t.Fatal("matched part of a name") + } + // An MX at a relay still names the server in its service records. + relayed := strings.Replace(savedZone, "MX 10 mail.example.com.", "MX 10 edge.example.org.", 1) + if !ZoneNamesHost(relayed, "mail.example.com") { + t.Fatal("MX at a relay was taken for a stale zone") + } + // SPF alone counts too (a:host). + if !ZoneNamesHost(`example.com. IN TXT "v=spf1 a:mx.example.com -all"`, "mx.example.com") { + t.Fatal("SPF a: names the host") + } + if !ZoneNamesHost(savedZone, "") { + t.Fatal("no recorded mail host: nothing to compare") + } +}