From 42101584816ea1c69d753ec327acc751f8a95edc Mon Sep 17 00:00:00 2001 From: John Coffey Date: Thu, 12 Nov 2026 00:00:16 -0700 Subject: [PATCH] Add an inbuxa dns command to show the records the domain needs By default it prints the dns.zone the install already wrote, which needs no key and, for most domains, never goes stale. --refresh --key - reads the records live from the running server instead, so a rotated DKIM key or a record that changed since the install shows up, and refreshes the saved copy. --- cmd/inbuxa/main.go | 106 ++++++++++++++++++++++++++++++++++++++++ cmd/inbuxa/main_test.go | 21 ++++++++ internal/apply/certs.go | 25 ++++++++++ 3 files changed, 152 insertions(+) diff --git a/cmd/inbuxa/main.go b/cmd/inbuxa/main.go index 18b845e..e7d249e 100644 --- a/cmd/inbuxa/main.go +++ b/cmd/inbuxa/main.go @@ -61,6 +61,9 @@ const usage = `inbuxa -- install the inbuxa suite on this machine inbuxa certs [--key KEY] the mail server's certificate: what its mail ports present, and with an API key a new order when it needs one + inbuxa dns the DNS records the domain needs: the saved + dns.zone, or --refresh --key - to re-read them + live from the server inbuxa export [-o FILE] write a topology file from what is here inbuxa relay install|status|uninstall inbuxa Relay, on a machine of its own in front @@ -125,6 +128,14 @@ certs flags: default: postmaster@DOMAIN --dry-run report, and say what it would do +dns flags: + --refresh read the records live from the server instead of + the saved dns.zone; needs an API key + --key KEY an API key made in the console, needed only with + --refresh; "-" reads it from standard input, and + INBUXA_API_KEY works too + --domain NAME the mail domain, if the install didn't record it + relay install flags: --token TOKEN the join token the console shows when you add the relay; "-" reads it from standard input, and @@ -203,6 +214,8 @@ func main() { os.Exit(uninstallCmd(os.Args[2:])) case "certs": os.Exit(certsCmd(os.Args[2:])) + case "dns": + os.Exit(dnsCmd(os.Args[2:])) case "relay": os.Exit(relayCmd(os.Args[2:])) case "cluster": @@ -1049,6 +1062,99 @@ func certsCmd(args []string) int { return 0 } +// dnsCmd prints the DNS records the domain needs published. By default it +// reads the dns.zone the install wrote, which needs nothing; --refresh asks +// the running server for them live, with an API key, so a rotated DKIM key or +// a record that changed since the install shows up. +func dnsCmd(args []string) int { + fs := flag.NewFlagSet("dns", flag.ContinueOnError) + fs.Usage = func() { fmt.Print(usage) } + var ( + refresh = fs.Bool("refresh", false, "") + key = fs.String("key", os.Getenv("INBUXA_API_KEY"), "") + domain = fs.String("domain", "", "") + ) + if err := fs.Parse(args); err != nil { + return 2 + } + ctx := context.Background() + st, err := state.Load() + if err != nil { + fmt.Fprintln(os.Stderr, err.Error()) + return 1 + } + if _, ok := st.Shapes["server"]; !ok || st.Removed() { + fmt.Println("The mail server isn't installed on this machine, so its DNS records are shown where it runs.") + return 0 + } + if apply.LocalInstall(st) { + fmt.Println("This is a --local install: the mail ports are on loopback and no DNS is needed. A real") + fmt.Println("install for a domain that points here writes the records it needs to dns.zone.") + return 0 + } + dom := *domain + if dom == "" { + dom = st.Domain + } + zoneFile := filepath.Join(st.Dir, "dns.zone") + + // The saved file is the no-friction default: the install wrote it, it + // needs no key, and for most domains it never goes stale. + if !*refresh { + b, err := os.ReadFile(zoneFile) + if err == nil { + fmt.Printf("The records %s needs published, from %s:\n\n", dom, zoneFile) + os.Stdout.Write(b) + if len(b) > 0 && b[len(b)-1] != '\n' { + fmt.Println() + } + fmt.Println("\nTo re-read them live from the server (a rotated DKIM key, say):") + fmt.Println(" inbuxa dns --refresh --key -") + return 0 + } + if !os.IsNotExist(err) { + fmt.Fprintln(os.Stderr, "reading "+zoneFile+": "+err.Error()) + return 1 + } + fmt.Printf("No saved records at %s. Reading them live from the server instead.\n\n", zoneFile) + } + + if *key == "-" { + line, err := bufio.NewReader(os.Stdin).ReadString('\n') + if err != nil && line == "" { + fmt.Fprintln(os.Stderr, "reading the key from standard input: "+err.Error()) + return 2 + } + *key = strings.TrimSpace(line) + } + if *key == "" { + fmt.Println("Reading the records live needs an API key: make one in the console (your account, API") + fmt.Println("keys), then") + fmt.Println(" inbuxa dns --refresh --key -") + fmt.Println("and paste it, or set INBUXA_API_KEY. It isn't kept.") + return 1 + } + zone, err := apply.FetchDNSZone(ctx, st, dom, *key) + if err != nil { + if errors.Is(err, apply.ErrKeyRefused) { + fmt.Fprintln(os.Stderr, "the server didn't take that API key") + return 1 + } + fmt.Fprintln(os.Stderr, "stopped: "+err.Error()) + return 1 + } + fmt.Printf("The records %s needs published, read live from the server:\n\n", dom) + fmt.Print(zone) + if len(zone) > 0 && zone[len(zone)-1] != '\n' { + fmt.Println() + } + // 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) + } + return 0 +} + // relayCmd is inbuxa Relay on this machine, which shares nothing with the // suite's install but the way it is asked for. func relayCmd(args []string) int { diff --git a/cmd/inbuxa/main_test.go b/cmd/inbuxa/main_test.go index d16df8c..f3c0061 100644 --- a/cmd/inbuxa/main_test.go +++ b/cmd/inbuxa/main_test.go @@ -34,3 +34,24 @@ func TestUpgradeTakesOptionsAfterComponents(t *testing.T) { t.Errorf("an unknown component was accepted: %d", code) } } + +// dns understands its flags: a bad one is a usage error, the good ones are +// not. With nothing installed it stops before touching the server, so what is +// checked is that the options parsed, not what they would have done. +func TestDNSTakesItsFlags(t *testing.T) { + if _, err := os.Stat(filepath.Join("/etc/inbuxa", "install.json")); err == nil { + t.Skip("an install is recorded on this machine") + } + for _, args := range [][]string{ + nil, + {"--refresh", "--domain", "example.org"}, + {"--refresh", "--key", ""}, + } { + if code := dnsCmd(args); code == 2 { + t.Errorf("dns %v: taken as a usage error", args) + } + } + if code := dnsCmd([]string{"--bogus"}); code != 2 { + t.Errorf("an unknown option was accepted: %d", code) + } +} diff --git a/internal/apply/certs.go b/internal/apply/certs.go index c45a17b..ee24280 100644 --- a/internal/apply/certs.go +++ b/internal/apply/certs.go @@ -286,6 +286,31 @@ func Renew(ctx context.Context, st *state.State, o CertsOptions, log Log) (*Serv } } +// FetchDNSZone asks the running server, on loopback, for the records the +// domain needs published right now -- the same zone fragment the install +// wrote to dns.zone, but re-read live, so it reflects the current DKIM key +// and any records that changed since. It needs an API key, as Renew does, +// because the server takes no password on its API once it is set up. +func FetchDNSZone(ctx context.Context, st *state.State, domain, key string) (string, error) { + if domain == "" { + return "", errors.New("no domain: this install didn't record one, so pass --domain") + } + srv := &jmap.Client{BaseURL: "http://" + hostFinalBind, Token: key} + domainID, err := srv.DomainID(ctx, domain) + if err != nil { + var he *jmap.HTTPError + if errors.As(err, &he) && (he.Status == 401 || he.Status == 403) { + return "", ErrKeyRefused + } + return "", fmt.Errorf("finding %s on the server: %w", domain, err) + } + zone, err := srv.DNSZone(ctx, domainID) + if err != nil { + return "", err + } + return zone, nil +} + // CertsLogHint is where the server says why an order failed. func CertsLogHint(st *state.State) string { if st.Shapes["server"] == "host" { -- 2.54.0