From 628616ff6abd061e74531bd425c4a754c3625ea3 Mon Sep 17 00:00:00 2001 From: John Coffey Date: Wed, 18 Nov 2026 09:34:11 -0700 Subject: [PATCH] certs: order on the domain that covers the mail host (#82) certs retried the order on the install's own domain only. The server orders one certificate per domain, and a domain's default names include the server's hostname only when it is inside that domain, so a mail host on another domain was never covered: certs waited three minutes and failed. planOrder now picks the domain whose name is the closest suffix of the mail host. On another domain it orders there with the install's ACME account: adding a certificate-only domain (no DKIM keys, nothing published) when the server has none, as the console's Order a certificate does, or adding the host to names entered in the console. A domain on the default names whose server hostname is still another name gets the place to change it instead of a three-minute wait. certs also stops early when the managed Caddy serves another mail host, since the challenge can't reach the server then. RetryCertificates no longer resets a domain's names to the default set on every retry: names entered in the console are kept. --- README.md | 7 ++- cmd/inbuxa/main.go | 7 +++ internal/apply/certs.go | 107 +++++++++++++++++++++++++++---- internal/apply/order_test.go | 63 +++++++++++++++++++ internal/jmap/setup.go | 118 +++++++++++++++++++++++++++++++++-- 5 files changed, 283 insertions(+), 19 deletions(-) create mode 100644 internal/apply/order_test.go diff --git a/README.md b/README.md index 269e883..bb4771c 100644 --- a/README.md +++ b/README.md @@ -167,7 +167,12 @@ This is early. What is built: because DNS wasn't in place during the install -- or, on a domain not yet on ACME, ACME turned on where the managed Caddy or your own web server passes the challenges. It waits for the mail ports to present the new - certificate. The console's and the webmail's certificates are the + certificate. A mail host on another domain from the install's gets its + certificate on that domain: the server's own, or one added for + certificates only (no DKIM keys, nothing published), as the console's + Order a certificate does, on the install's ACME account. A domain's + default certificate names the server's hostname, so if that is still an + old name it says where to change it first. The console's and the webmail's certificates are the proxy's, which renews them itself. - **`status`** -- what is installed, what is running, and where those two diff --git a/cmd/inbuxa/main.go b/cmd/inbuxa/main.go index 6188d03..2e88cbb 100644 --- a/cmd/inbuxa/main.go +++ b/cmd/inbuxa/main.go @@ -1179,6 +1179,13 @@ func certsCmd(args []string) int { fmt.Println("and paste it, or set INBUXA_API_KEY. It isn't kept.") return 1 } + // The managed Caddy forwards the challenges only for the names it was + // installed with: a mail host it doesn't serve can't be validated (#82). + if o.Proxy == "caddy" && found.MailHost != "" && !strings.EqualFold(found.MailHost, o.MailHost) { + fmt.Printf("\nThe proxy here serves %s, not %s, so the order can't be validated. Move the install to\n", found.MailHost, o.MailHost) + fmt.Printf("the new name first:\n sudo inbuxa install --mail-host %s --dry-run\n", o.MailHost) + return 1 + } fmt.Println() o.Key = *key if _, err := apply.Renew(ctx, st, o, &printer{}); err != nil { diff --git a/internal/apply/certs.go b/internal/apply/certs.go index ee24280..a9c21ed 100644 --- a/internal/apply/certs.go +++ b/internal/apply/certs.go @@ -212,45 +212,126 @@ type CertsOptions struct { ACMEDirectory string } +// orderPlan is how Renew gets the mail host a certificate: which domain it +// orders on, and what it does to that domain first. +type orderPlan struct { + Action string // retry, set (these names on this account), add (a certificate-only domain, then set), enable (ACME on the install's domain) + DomainID string // empty for add + DomainName string + ProviderID string + Names []string // for set and add; empty means the server's default set +} + +// planOrder picks the domain whose certificate is to name host (#82). The +// server orders one certificate per domain, and a domain's default set of +// names includes the server's own hostname only when it is inside that +// domain. So a mail host on another domain from the install's needs that +// other domain's certificate to name it: the one the server has, or one +// added for certificates only, as the console's Order a certificate does, +// on the install's ACME account. +func planOrder(domains []jmap.DomainCert, installDomain, host, serverName string) (orderPlan, error) { + host = strings.ToLower(strings.TrimSuffix(host, ".")) + var install, cover *jmap.DomainCert + for i := range domains { + d := &domains[i] + name := strings.ToLower(d.Name) + if strings.EqualFold(d.Name, installDomain) { + install = d + } + if (host == name || strings.HasSuffix(host, "."+name)) && (cover == nil || len(name) > len(cover.Name)) { + cover = d + } + } + if install == nil { + return orderPlan{}, fmt.Errorf("domain %s does not exist on this server", installDomain) + } + if cover == nil { + parent := host[strings.Index(host, ".")+1:] + if !strings.Contains(host, ".") || !strings.Contains(parent, ".") { + return orderPlan{}, fmt.Errorf("%s isn't a name a certificate can be ordered for", host) + } + if install.Kind != "Automatic" || install.ProviderID == "" { + return orderPlan{}, fmt.Errorf("%s is on another domain, and %s has no ACME account yet to order with: run `inbuxa certs` without --mail-host first, or order it in the console (Settings, Security, Certificates)", host, installDomain) + } + return orderPlan{Action: "add", DomainName: parent, ProviderID: install.ProviderID, Names: []string{host}}, nil + } + if cover.Kind != "Automatic" || cover.ProviderID == "" { + if cover == install { + return orderPlan{Action: "enable", DomainID: install.ID, DomainName: install.Name}, nil + } + if install.Kind != "Automatic" || install.ProviderID == "" { + return orderPlan{}, fmt.Errorf("neither %s nor %s orders certificates from ACME yet: order one for %s in the console (Settings, Security, Certificates)", cover.Name, installDomain, host) + } + return orderPlan{Action: "set", DomainID: cover.ID, DomainName: cover.Name, ProviderID: install.ProviderID, Names: []string{host}}, nil + } + if len(cover.Names) == 0 { + // The default set: it names the server's hostname, and only that one. + if serverName != "" && !strings.EqualFold(strings.TrimSuffix(serverName, "."), host) { + return orderPlan{}, fmt.Errorf("the server's hostname is %s, so %s's certificate names that, not %s: set the hostname to %s in the console (Settings, Network, General), then run this again", serverName, cover.Name, host, host) + } + return orderPlan{Action: "retry", DomainID: cover.ID, DomainName: cover.Name}, nil + } + for _, n := range cover.Names { + if strings.EqualFold(strings.TrimSuffix(n, "."), host) { + return orderPlan{Action: "retry", DomainID: cover.ID, DomainName: cover.Name}, nil + } + } + return orderPlan{Action: "set", DomainID: cover.ID, DomainName: cover.Name, ProviderID: cover.ProviderID, Names: append(append([]string{}, cover.Names...), host)}, nil +} + // Renew gets the server a certificate: a new order on ACME, or ACME turned // on first where something here can pass its challenges. It returns what the // mail ports present afterwards. func Renew(ctx context.Context, st *state.State, o CertsOptions, log Log) (*Served, error) { srv := &jmap.Client{BaseURL: "http://" + hostFinalBind, Token: o.Key} - domainID, err := srv.DomainID(ctx, st.Domain) + domains, err := srv.DomainCerts(ctx) if err != nil { var he *jmap.HTTPError if errors.As(err, &he) && (he.Status == 401 || he.Status == 403) { return nil, ErrKeyRefused } - return nil, fmt.Errorf("finding %s on the server: %w", st.Domain, err) + return nil, fmt.Errorf("reading the server's domains: %w", err) } - kind, _, err := srv.CertificateManagement(ctx, domainID) + serverName, _ := srv.ServerName(ctx) // only sharpens the advice when it can be read + op, err := planOrder(domains, st.Domain, o.MailHost, serverName) if err != nil { return nil, err } - if kind == "Automatic" { + if op.Action != "retry" && o.Proxy == "" { + return nil, fmt.Errorf("nothing here passes the server's ACME challenges on port 80 "+ + "(the install has no proxy): the certificate for %s is yours to arrange", o.MailHost) + } + if o.Proxy == "snippets" && op.Action != "retry" { + log.Info("the challenges reach the server through your web server, so it must be loading %s first", + filepath.Join(st.Dir, "proxy")) + } + domainID := op.DomainID + switch op.Action { + case "retry": log.Step("starting a new certificate order for %s", o.MailHost) if err := srv.RetryCertificates(ctx, domainID); err != nil { return nil, err } - } else { - if o.Proxy == "" { - return nil, fmt.Errorf("the domain's certificates are %s, and nothing here passes the server's ACME challenges on port 80 "+ - "(the install has no proxy): its certificate is yours to arrange", strings.ToLower(kind)) - } + case "enable": email := o.Email if email == "" { email = "postmaster@" + st.Domain } log.Step("turning on certificates for %s, from ACME (contact %s)", o.MailHost, email) - if o.Proxy == "snippets" { - log.Info("the challenges reach the server through your web server, so it must be loading %s first", - filepath.Join(st.Dir, "proxy")) - } if _, err := srv.EnableACME(ctx, domainID, o.ACMEDirectory, email); err != nil { return nil, fmt.Errorf("enabling ACME: %w", err) } + case "add", "set": + if op.Action == "add" { + log.Step("adding %s to the server for certificates only (no DKIM keys, nothing published)", op.DomainName) + if domainID, err = srv.AddCertificateDomain(ctx, op.DomainName); err != nil { + return nil, fmt.Errorf("adding %s: %w", op.DomainName, err) + } + } + log.Step("ordering a certificate for %s on %s (%s)", o.MailHost, op.DomainName, strings.Join(op.Names, ", ")) + if err := srv.SetCertificates(ctx, domainID, op.ProviderID, op.Names); err != nil { + return nil, err + } } // Waited for as the mail ports present it, not as the server lists it: // a certificate about to expire is listed too, and is what is being diff --git a/internal/apply/order_test.go b/internal/apply/order_test.go new file mode 100644 index 0000000..7349100 --- /dev/null +++ b/internal/apply/order_test.go @@ -0,0 +1,63 @@ +// SPDX-FileCopyrightText: 2026 Coffey Labs LLC +// SPDX-License-Identifier: AGPL-3.0-or-later + +package apply + +import ( + "reflect" + "strings" + "testing" + + "git.coffeylabs.org/inbuxa/inbuxa-installer/internal/jmap" +) + +func auto(id, name string, names ...string) jmap.DomainCert { + return jmap.DomainCert{ID: id, Name: name, Kind: "Automatic", ProviderID: "acme1", Names: names} +} + +// #82: the domain that orders a certificate naming the mail host. +func TestPlanOrder(t *testing.T) { + install := auto("d1", "example.com") + cases := []struct { + name string + domains []jmap.DomainCert + host, srv string + want orderPlan + wantErrHas string + }{ + {"same domain, default names, server already called that", []jmap.DomainCert{install}, "mail.example.com", "mail.example.com", + orderPlan{Action: "retry", DomainID: "d1", DomainName: "example.com"}, ""}, + {"same domain, default names, server still has its old name", []jmap.DomainCert{install}, "mx.example.com", "mail.example.com", + orderPlan{}, "set the hostname to mx.example.com"}, + {"another domain the server doesn't have: added for certificates", []jmap.DomainCert{install}, "mail.example.net", "", + orderPlan{Action: "add", DomainName: "example.net", ProviderID: "acme1", Names: []string{"mail.example.net"}}, ""}, + {"another domain with manual certificates: the install's account, just that name", + []jmap.DomainCert{install, {ID: "d2", Name: "example.net", Kind: "Manual"}}, "mail.example.net", "", + orderPlan{Action: "set", DomainID: "d2", DomainName: "example.net", ProviderID: "acme1", Names: []string{"mail.example.net"}}, ""}, + {"names entered in the console are kept, and the host added", + []jmap.DomainCert{install, auto("d2", "example.net", "autoconfig.example.net")}, "mail.example.net", "", + orderPlan{Action: "set", DomainID: "d2", DomainName: "example.net", ProviderID: "acme1", Names: []string{"autoconfig.example.net", "mail.example.net"}}, ""}, + {"names that already include it: just order again", + []jmap.DomainCert{install, auto("d2", "example.net", "mail.example.net")}, "MAIL.example.net.", "", + orderPlan{Action: "retry", DomainID: "d2", DomainName: "example.net"}, ""}, + {"the closest domain wins", []jmap.DomainCert{install, auto("d2", "example.net"), {ID: "d3", Name: "eu.example.net", Kind: "Manual"}}, + "mail.eu.example.net", "", orderPlan{Action: "set", DomainID: "d3", DomainName: "eu.example.net", ProviderID: "acme1", Names: []string{"mail.eu.example.net"}}, ""}, + {"install domain on manual certificates: turn ACME on, as before", []jmap.DomainCert{{ID: "d1", Name: "example.com", Kind: "Manual"}}, + "mail.example.com", "", orderPlan{Action: "enable", DomainID: "d1", DomainName: "example.com"}, ""}, + {"another domain with no ACME account anywhere", []jmap.DomainCert{{ID: "d1", Name: "example.com", Kind: "Manual"}}, + "mail.example.net", "", orderPlan{}, "no ACME account"}, + {"install domain missing", nil, "mail.example.com", "", orderPlan{}, "does not exist"}, + } + for _, c := range cases { + got, err := planOrder(c.domains, "example.com", c.host, c.srv) + if c.wantErrHas != "" { + if err == nil || !strings.Contains(err.Error(), c.wantErrHas) { + t.Errorf("%s: err = %v, want one saying %q", c.name, err, c.wantErrHas) + } + continue + } + if err != nil || !reflect.DeepEqual(got, c.want) { + t.Errorf("%s:\n got %+v, %v\nwant %+v", c.name, got, err, c.want) + } + } +} diff --git a/internal/jmap/setup.go b/internal/jmap/setup.go index ba6fdc6..9d8bab7 100644 --- a/internal/jmap/setup.go +++ b/internal/jmap/setup.go @@ -12,6 +12,7 @@ import ( "encoding/json" "errors" "fmt" + "sort" "strings" "time" ) @@ -211,18 +212,36 @@ func (c *Client) CertificateManagement(ctx context.Context, domainID string) (ki // RetryCertificates starts a fresh ACME order for the domain. A failed order // is not retried on a restart; moving the domain to manual and straight back -// is what starts a new one. +// is what starts a new one. The names the domain's certificate covers are +// kept: names entered in the console stay, and an empty set stays the +// server's default one. func (c *Client) RetryCertificates(ctx context.Context, domainID string) error { - kind, providerID, err := c.CertificateManagement(ctx, domainID) + d, err := c.domainCerts(ctx, []string{domainID}) if err != nil { return err } - if kind != "Automatic" || providerID == "" { - return fmt.Errorf("the domain's certificates are %q, not managed by ACME", kind) + if len(d) != 1 { + return errors.New("x:Domain/get did not return the domain") } + if d[0].Kind != "Automatic" || d[0].ProviderID == "" { + return fmt.Errorf("the domain's certificates are %q, not managed by ACME", d[0].Kind) + } + return c.SetCertificates(ctx, domainID, d[0].ProviderID, d[0].Names) +} + +// SetCertificates puts the domain's certificates on the ACME account with +// exactly these names (none: the server's default set), and starts an order +// for them by moving it to manual and straight back. +func (c *Client) SetCertificates(ctx context.Context, domainID, providerID string, names []string) error { + auto := automaticCertificates(providerID) + sans := map[string]bool{} + for _, n := range names { + sans[n] = true + } + auto["certificateManagement"].(map[string]any)["subjectAlternativeNames"] = sans rs, err := c.Do(ctx, Call{"x:Domain/set", map[string]any{"update": map[string]any{domainID: map[string]any{"certificateManagement": map[string]any{"@type": "Manual"}}}}, "0"}, - Call{"x:Domain/set", map[string]any{"update": map[string]any{domainID: automaticCertificates(providerID)}}, "1"}, + Call{"x:Domain/set", map[string]any{"update": map[string]any{domainID: auto}}, "1"}, ) if err != nil { return err @@ -245,6 +264,95 @@ func automaticCertificates(providerID string) map[string]any { }} } +// DomainCert is a domain and how its certificates are managed. +type DomainCert struct { + ID, Name string + Kind string // Automatic or Manual + ProviderID string // the ACME account, when Automatic + Names []string // the names its certificate covers; empty is the server's default set +} + +// DomainCerts lists every domain with its certificate management. +func (c *Client) DomainCerts(ctx context.Context) ([]DomainCert, error) { + return c.domainCerts(ctx, nil) +} + +func (c *Client) domainCerts(ctx context.Context, ids []string) ([]DomainCert, error) { + args := map[string]any{"properties": []string{"name", "certificateManagement"}} + if ids != nil { + args["ids"] = ids + } else { + args["ids"] = nil + } + rs, err := c.Do(ctx, Call{"x:Domain/get", args, "0"}) + if err != nil { + return nil, err + } + var got struct { + List []struct { + ID string `json:"id"` + Name string `json:"name"` + CertificateManagement struct { + Type string `json:"@type"` + AcmeProviderID string `json:"acmeProviderId"` + SubjectAlternativeNames map[string]bool `json:"subjectAlternativeNames"` + } `json:"certificateManagement"` + } `json:"list"` + } + if err := json.Unmarshal(rs[0].Args, &got); err != nil { + return nil, err + } + out := make([]DomainCert, 0, len(got.List)) + for _, d := range got.List { + cm := d.CertificateManagement + dc := DomainCert{ID: d.ID, Name: d.Name, Kind: cm.Type, ProviderID: cm.AcmeProviderID} + for n := range cm.SubjectAlternativeNames { + dc.Names = append(dc.Names, n) + } + sort.Strings(dc.Names) + out = append(out, dc) + } + return out, nil +} + +// AddCertificateDomain adds a domain that is there only for certificates, +// as the console's Order a certificate does: no DKIM keys, nothing published. +func (c *Client) AddCertificateDomain(ctx context.Context, name string) (string, error) { + rs, err := c.Do(ctx, Call{"x:Domain/set", map[string]any{"create": map[string]any{"d": map[string]any{ + "name": name, + "description": "Added for certificates", + "dkimManagement": map[string]any{"@type": "Manual"}, + "dnsManagement": map[string]any{"@type": "Manual"}, + "certificateManagement": map[string]any{"@type": "Manual"}, + }}}, "0"}) + if err != nil { + return "", err + } + s, err := DecodeSet(rs[0]) + if err != nil { + return "", err + } + return CreatedID(s, "d") +} + +// ServerName is the server's own hostname, which a domain's default +// certificate names include only when it is inside that domain. +func (c *Client) ServerName(ctx context.Context) (string, error) { + rs, err := c.Do(ctx, Call{"x:SystemSettings/get", map[string]any{"ids": []string{"singleton"}, "properties": []string{"defaultHostname"}}, "0"}) + if err != nil { + return "", err + } + var got struct { + List []struct { + DefaultHostname string `json:"defaultHostname"` + } `json:"list"` + } + if err := json.Unmarshal(rs[0].Args, &got); err != nil || len(got.List) != 1 { + return "", errors.New("x:SystemSettings/get did not return the settings") + } + return got.List[0].DefaultHostname, nil +} + // Certificate is what the tool reports about an issued certificate. type Certificate struct { Issuer string `json:"issuer"` -- 2.54.0