diff --git a/README.md b/README.md index 73f146f..5ea4a80 100644 --- a/README.md +++ b/README.md @@ -48,12 +48,20 @@ This is early. What is built: anything is put in place. `install --install-deps` does the same as part of a run. A missing dependency is an offer, not a refusal. -- **`install --yes`** -- carries the plan out, for container shapes: writes - the deployment, fetches the images, brings the mail server up in bootstrap - mode with a credential that exists only for that step, completes bootstrap, - brings the rest up without it, exempts the front ends from the auto-ban, - creates the first mailbox, writes `credentials.txt` and `dns.zone`, and - then checks that all three answer. +- **`install --yes`** -- carries the plan out: writes the deployment, + fetches the images, brings the mail server up in bootstrap mode with a + credential that exists only for that step, completes bootstrap, brings the + rest up without it, exempts the front ends from the auto-ban, creates the + first mailbox, writes `credentials.txt` and `dns.zone`, and then checks + that all three answer. +- **`install --server host`** -- the same, with the mail server as the + release binary under systemd instead of a container, checked against a + checksum in the source. It gets a user of its own, its configuration and + data in `/server`, and a unit, `inbuxa-server.service`. During setup + it is reachable from the machine itself and nowhere else; afterwards its + plain HTTP is on loopback for the proxy and the webmail, 443 is left to + the proxy, and the unit will not start without a configuration file. The + console and the webmail run in containers beside it, or not at all. - **`plan -f` / `apply -f` / `export`** -- a whole installation described in one file, however many machines. Each machine acts on its own part and @@ -66,8 +74,9 @@ This is early. What is built: installing. It exits non-zero when they disagree, so a machine can be asked in a script whether it still matches itself. -Not built yet: host installs, the terminal interface, `join`, `status`, -`upgrade`, `uninstall`. The design is in the inbuxa specification (§6.1 and +Not built yet: host installs of the console and the webmail, the terminal +interface, `join`, `upgrade`, `uninstall`, and moving the server between a +container and the host. The design is in the inbuxa specification (§6.1 and the installer draft); the phases are there too. inbuxa install --local --domain example.test --install-deps --yes @@ -106,6 +115,7 @@ is tested on a throwaway virtual machine rather than on anybody's desk: e2e/vm/run.sh e2e/cases/deps.sh the offer, and taking it e2e/vm/run.sh e2e/cases/install-local.sh a whole suite, and signing in to it e2e/vm/run.sh e2e/cases/install-public.sh the same with real ports and certificates + e2e/vm/run.sh e2e/cases/install-host.sh the server on the host, the rest beside it e2e/vm/run.sh e2e/cases/topology.sh growing and shrinking from a file e2e/vm/run.sh e2e/cases/status-export.sh intent against reality, and the file OLD_REF= e2e/vm/run.sh e2e/cases/upgrade-volumes.sh diff --git a/cmd/inbuxa/main.go b/cmd/inbuxa/main.go index fdd8d6a..7fb5f68 100644 --- a/cmd/inbuxa/main.go +++ b/cmd/inbuxa/main.go @@ -433,6 +433,8 @@ func exportCmd(args []string) int { switch { case st.Shapes[kind] != "": m.Components = append(m.Components, topology.Component{Kind: kind, Shape: st.Shapes[kind]}) + case kind == "server" && found.HostServer: + m.Components = append(m.Components, topology.Component{Kind: kind, Shape: "host"}) case found.Services[kind] != "": m.Components = append(m.Components, topology.Component{Kind: kind, Shape: "container"}) } @@ -514,6 +516,8 @@ func statusCmd() int { fmt.Printf(" %-8s installed as a %-9s %s\n", kind, shape, state) case intended: fmt.Printf(" %-8s installed as a %-9s missing from the deployment\n", kind, shape) + case kind == "server" && found.HostServer: + fmt.Printf(" %-8s %-24s %s\n", kind, "on the host", state) default: fmt.Printf(" %-8s %-24s %s\n", kind, "in the deployment", state) } diff --git a/e2e/cases/install-host.sh b/e2e/cases/install-host.sh new file mode 100644 index 0000000..7d4007b --- /dev/null +++ b/e2e/cases/install-host.sh @@ -0,0 +1,145 @@ +#!/bin/bash +# SPDX-FileCopyrightText: 2026 Coffey Labs LLC +# SPDX-License-Identifier: AGPL-3.0-or-later +# +# The mail server on the host, the console and the webmail in containers +# beside it, on loopback: the same whole system install-local.sh proves, with +# the server as a release binary under systemd instead of a container. +# +# What is particular to the host is checked as well as what is common: the +# setup's drop-in and credential are gone, the server runs as its own user +# with nothing on 443 or on 8080 of its own, the unit refuses to start +# without a configuration, and status says where the server is. +# +# Run from the host with: e2e/vm/run.sh e2e/cases/install-host.sh +set -uo pipefail + +pass=0; fail=0 +ok() { echo " ok $*"; pass=$((pass+1)); } +bad() { echo " FAIL $*"; fail=$((fail+1)); } + +DIR=/var/lib/inbuxa +UNIT=inbuxa-server.service + +rt() { command -v docker >/dev/null 2>&1 && echo docker || echo podman; } +compose() { + if [ "$(rt)" = docker ]; then + docker compose -f "$DIR/compose.yaml" "$@" + else + DOCKER_HOST=unix:///run/podman/podman.sock \ + /usr/local/lib/docker/cli-plugins/docker-compose -f "$DIR/compose.yaml" "$@" + fi +} +listening() { ss -ltnH "sport = :$1" | awk '{print $4}'; } + +echo "==> what it would do" +PLAN="$(/tmp/inbuxa install --local --domain example.test --server host --install-deps --dry-run 2>&1)" +grep -q "Install the mail server on this machine" <<<"$PLAN" && ok "the plan names the host install" || { bad "no host install in the plan"; echo "$PLAN" | sed 's/^/ /'; } +grep -q "/usr/local/bin/inbuxa-server, release" <<<"$PLAN" && ok "and the release it fetches" || bad "the plan does not name the release" + +echo +echo "==> installing" +OUT="$(/tmp/inbuxa install --local --domain example.test --server host --install-deps --yes 2>&1)"; rc=$? +echo "$OUT" | grep -v '^ |' | tail -30 | sed 's/^/ /' +[ $rc -eq 0 ] && ok "installed (exit 0)" || bad "exit $rc" + +echo +echo "==> the server is a host install" +/usr/local/bin/inbuxa-server --version 2>&1 | grep -q '[0-9]\{4\}\.[0-9]' && ok "the release binary runs: $(/usr/local/bin/inbuxa-server --version 2>&1 | head -1)" || bad "no runnable /usr/local/bin/inbuxa-server" +systemctl is-active --quiet $UNIT && ok "$UNIT is running" || { bad "$UNIT is not running"; journalctl -u $UNIT -n 20 --no-pager | sed 's/^/ /'; } +systemctl is-enabled --quiet $UNIT && ok "and enabled, so it comes back after a reboot" || bad "$UNIT is not enabled" +PID=$(systemctl show -p MainPID --value $UNIT) +[ "$(ps -o user= -p "$PID" 2>/dev/null)" = inbuxa ] && ok "it runs as the inbuxa user" || bad "it runs as '$(ps -o user= -p "$PID" 2>/dev/null)'" +[ -f "$DIR/server/config.json" ] && ok "its configuration is in $DIR/server" || bad "no $DIR/server/config.json" +[ "$(stat -c %U "$DIR/server/data")" = inbuxa ] && ok "and its data directory is its own" || bad "$DIR/server/data is owned by $(stat -c %U "$DIR/server/data")" +grep -q "^ConditionPathExists=$DIR/server/config.json" /etc/systemd/system/$UNIT && ok "the finished unit needs the configuration to start" || bad "no ConditionPathExists in the unit" +grep -q "^AmbientCapabilities=CAP_NET_BIND_SERVICE" /etc/systemd/system/$UNIT && ok "and may bind the ports below 1024" || bad "no AmbientCapabilities in the unit" +compose ps --format '{{.Service}}' | grep -qx server && bad "a server container is running too" || ok "no server container" + +echo +echo "==> the setup left nothing behind" +[ ! -e /run/systemd/system/$UNIT.d/inbuxa-setup.conf ] && ok "the setup's drop-in is gone" || bad "the setup's drop-in is still there" +[ ! -e /run/inbuxa-server-setup.env ] && ok "and so is its credential file" || bad "the setup's credential file is still there" +ENVOUT="$(tr '\0' '\n' < /proc/"$PID"/environ)" +grep -q "RECOVERY_ADMIN" <<<"$ENVOUT" && bad "the server still carries a recovery admin" || ok "no recovery admin in the running server" +grep -q "HTTP_BASIC_AUTH" <<<"$ENVOUT" && bad "the server still takes passwords everywhere" || ok "no Basic-everywhere override in the running server" +grep -q "INBUXA_WEBMAIL_CLIENT_SECRET" <<<"$ENVOUT" && ok "the webmail's client secret is in its environment" || bad "the server has no webmail client secret" +[ "$(stat -c %a "$DIR/server.env")" = 600 ] && ok "server.env is 600" || bad "server.env is $(stat -c %a "$DIR/server.env")" +[ "$(stat -c %a "$DIR/credentials.txt")" = 600 ] && ok "credentials.txt is 600" || bad "credentials.txt is $(stat -c %a "$DIR/credentials.txt")" + +echo +echo "==> its listeners fit the host" +[ "$(listening 8081)" = "127.0.0.1:8081" ] && ok "plain HTTP on 127.0.0.1:8081 only" || bad "8081 is on '$(listening 8081)'" +[ -z "$(listening 443)" ] && ok "nothing on 443: that is the proxy's" || bad "443 is held: $(listening 443)" +[ "$(listening 8080)" = "127.0.0.1:8080" ] && ok "8080 is the webmail's, on loopback" || bad "8080 is on '$(listening 8080)'" +for p in 25 465 993 995 4190; do + [ "$(listening $p)" = "127.0.0.1:$p" ] || { bad "port $p is on '$(listening $p)', not loopback"; continue; } +done +ok "the mail ports are on loopback, as a local install publishes nothing" + +echo +echo "==> and it answers" +curl -fsS -o /dev/null http://127.0.0.1:8081/.well-known/jmap 2>/dev/null +[ $? -le 22 ] && ok "the mail server answers on its loopback bind" || bad "the mail server does not answer" +curl -fsS http://127.0.0.1:8082/ 2>/dev/null | grep -q 'api-base-url' && ok "the console is served, pointed at the server" || bad "the console is not served" +curl -fsS http://127.0.0.1:8080/api/health 2>/dev/null | grep -q '"ok":true' && ok "the webmail is healthy" || bad "the webmail is not healthy" +AUSER=$(awk '/^administrator/ {print $2}' "$DIR/credentials.txt") +APASS=$(awk '/^administrator/{getline; print $2}' "$DIR/credentials.txt") +CODE=$(curl -s -o /dev/null -w '%{http_code}' -u "$AUSER:$APASS" -H 'Content-Type: application/json' \ + -d '{"using":["urn:ietf:params:jmap:core"],"methodCalls":[]}' http://127.0.0.1:8081/jmap/) +[ "$CODE" = 401 ] && ok "the administrator's password is refused on /jmap" || bad "the administrator's password on /jmap answered $CODE, not 401" + +echo +echo "==> the account it created can sign in to the webmail it installed" +USER=$(awk '/^first mailbox/ {print $3}' "$DIR/credentials.txt") +PASS=$(awk '/^first mailbox/{getline; print $2}' "$DIR/credentials.txt") +[ -n "$USER" ] && ok "credentials.txt names the first mailbox ($USER)" || bad "no first mailbox in credentials.txt" +rm -f /tmp/jar +LOC=$(curl -s -o /dev/null -c /tmp/jar -w '%{redirect_url}' "http://127.0.0.1:8080/api/auth/oauth/start?username=$USER&remember=1") +grep -q "^http://127.0.0.1:8081/" <<<"$LOC" && ok "the webmail sends sign-in to the server's loopback address" || bad "sign-in went to '${LOC:-nowhere}'" +q() { sed -n "s/.*[?&]$1=\([^&]*\).*/\1/p" <<<"$LOC"; } +urldecode() { printf '%b' "${1//%/\\x}"; } +STATE=$(q state); CHALLENGE=$(q code_challenge); REDIRECT=$(urldecode "$(q redirect_uri)") +ANSWER=$(curl -s -X POST http://127.0.0.1:8081/api/auth -H 'Content-Type: application/json' -H 'Accept: application/json' \ + -d "{\"type\":\"authCode\",\"accountName\":\"$USER\",\"accountSecret\":\"$PASS\",\"clientId\":\"ihasmail-inbuxa\",\"redirectUri\":\"$REDIRECT\",\"codeChallenge\":\"$CHALLENGE\",\"codeChallengeMethod\":\"S256\"}") +AUTHCODE=$(sed -n 's/.*"client_\{0,1\}[cC]ode":"\([^"]*\)".*/\1/p' <<<"$ANSWER") +[ -n "$AUTHCODE" ] && ok "the server's sign-in takes the first mailbox's password" || { bad "the server's sign-in answered"; head -c 200 <<<"$ANSWER" | sed 's/^/ /'; } +BACK=$(curl -s -o /dev/null -b /tmp/jar -c /tmp/jar -w '%{redirect_url}' "$REDIRECT?code=$AUTHCODE&state=$STATE") +grep -q "signin_error" <<<"$BACK" && bad "the webmail refused the code: $BACK" || ok "the webmail takes the code back" +CODE=$(curl -s -o /tmp/login.json -b /tmp/jar -w '%{http_code}' -H 'X-Requested-With: ihasmail' http://127.0.0.1:8080/api/auth/session) +[ "$CODE" = 200 ] && ok "sign-in succeeds" || { bad "the session answered $CODE"; head -c 200 /tmp/login.json | sed 's/^/ /'; } +grep -q "urn:ietf:params:jmap:mail" /tmp/login.json && ok "and the session carries the mail capability" || bad "no mail capability in the session" + +echo +echo "==> status knows where the server is" +STATUS="$(/tmp/inbuxa status 2>&1)"; rc=$? +grep -q "server.*installed as a host" <<<"$STATUS" && ok "status: the server is a host install" || { bad "status does not say so"; echo "$STATUS" | sed 's/^/ /'; } +[ $rc -eq 0 ] && ok "and intent and reality agree (exit 0)" || { bad "status exit $rc"; echo "$STATUS" | sed 's/^/ /'; } +/tmp/inbuxa export 2>/dev/null | grep -A1 '"kind": "server"' | grep -q '"shape": "host"' && ok "export writes the server as host" || bad "export does not say host" + +echo +echo "==> running it again converges rather than duplicating" +OUT="$(/tmp/inbuxa install --local --domain example.test --server host --yes 2>&1)"; rc=$? +[ $rc -eq 0 ] && ok "a second run succeeds" || { bad "second run exit $rc"; echo "$OUT" | tail -10 | sed 's/^/ /'; } +grep -q "is already" <<<"$OUT" && ok "and keeps the binary it has" || bad "the second run fetched the binary again" +COUNT=$(compose ps --format '{{.Service}}' | sort -u | wc -l) +[ "$COUNT" = 2 ] && ok "still two containers beside the server" || bad "$COUNT containers after a second run" +OUT="$(/tmp/inbuxa install --local --domain example.test --server container --yes 2>&1)"; rc=$? +[ $rc -ne 0 ] && grep -q "not built yet" <<<"$OUT" && ok "moving the server into a container is refused, with the reason" || bad "a shape change was not refused: exit $rc" + +echo +echo "==> a server without its configuration stays down" +mv "$DIR/server/config.json" /tmp/config.json.kept +systemctl restart $UNIT; sleep 2 +systemctl is-active --quiet $UNIT && bad "the server started without a configuration" || ok "it does not start in bootstrap mode" +[ -z "$(listening 8080 | grep -v 127.0.0.1:8080)" ] && ok "and nothing opened 8080 on another interface" || bad "8080 is open: $(listening 8080)" +mv /tmp/config.json.kept "$DIR/server/config.json"; chown inbuxa:inbuxa "$DIR/server/config.json" +systemctl restart $UNIT +# Any HTTP answer at all; 000 is curl failing to connect. +code() { curl -s -o /dev/null -w '%{http_code}' http://127.0.0.1:8081/.well-known/jmap; } +for _ in $(seq 1 60); do [ "$(code)" != 000 ] && break; sleep 2; done +code | grep -qE '^(200|401|30[0-9])$' && ok "with it back, the server answers again" || { bad "the server did not come back ($(code))"; journalctl -u $UNIT -n 15 --no-pager | sed 's/^/ /'; } + +echo +echo "==> $pass passed, $fail failed" +[ "$fail" -eq 0 ] diff --git a/internal/apply/apply.go b/internal/apply/apply.go index f384aaf..1eb2d5d 100644 --- a/internal/apply/apply.go +++ b/internal/apply/apply.go @@ -1,7 +1,8 @@ // SPDX-FileCopyrightText: 2026 Coffey Labs LLC // SPDX-License-Identifier: AGPL-3.0-or-later -// Package apply carries out a plan, for the shapes that are containers. +// Package apply carries out a plan: every component in a container, or the +// mail server on the host with whatever runs beside it in containers. // // The sequence is SPEC.md 6.2's, which ihasmail-oneshot worked out against a // running server: bring the mail server up in bootstrap mode with a @@ -29,10 +30,12 @@ import ( "git.coffeylabs.org/inbuxa/inbuxa-installer/internal/compose" "git.coffeylabs.org/inbuxa/inbuxa-installer/internal/deps" + "git.coffeylabs.org/inbuxa/inbuxa-installer/internal/discover" "git.coffeylabs.org/inbuxa/inbuxa-installer/internal/docker" "git.coffeylabs.org/inbuxa/inbuxa-installer/internal/host" "git.coffeylabs.org/inbuxa/inbuxa-installer/internal/jmap" "git.coffeylabs.org/inbuxa/inbuxa-installer/internal/plan" + "git.coffeylabs.org/inbuxa/inbuxa-installer/internal/release" "git.coffeylabs.org/inbuxa/inbuxa-installer/internal/state" ) @@ -41,7 +44,7 @@ import ( // here would mean this program needing a release of its own for every one of // theirs. const ( - DefaultServerImage = "registry.coffeylabs.org/inbuxa/inbuxa-server:2026.10.10.1" + DefaultServerImage = release.ServerImage DefaultConsoleImage = "registry.coffeylabs.org/inbuxa/inbuxa-admin:2026.10.5" DefaultWebmailImage = "registry.coffeylabs.org/inbuxa/inbuxa-webmail:2026.10.9-g62936ab" DefaultCaddyImage = "docker.io/library/caddy:2-alpine" @@ -70,18 +73,25 @@ type Log interface { Out() io.Writer } -// Run carries out p. It refuses anything but container shapes for now, and -// says so rather than pretending a host install happened. +// Run carries out p. The console and the webmail are containers for now, and +// it says so rather than pretending a host install of either happened. func Run(ctx context.Context, p plan.Plan, f host.Facts, log Log) (*Result, error) { o := p.Options - for _, c := range []plan.Component{plan.Server, plan.Console, plan.Webmail} { + for _, c := range []plan.Component{plan.Console, plan.Webmail} { if o.Shape(c) == plan.Host { - return nil, fmt.Errorf("host installs are not built yet: %s was asked for as a host install", c) + return nil, fmt.Errorf("host installs of the console and the webmail are not built yet: %s was asked for as a host install", c) } } - if o.Shape(plan.Server) != plan.Container { + if o.Shape(plan.Server) == plan.Skip { return nil, fmt.Errorf("this build installs the front ends only alongside a server it installs too; use join once that is built") } + onHost := o.Shape(plan.Server) == plan.Host + if st, err := state.Load(); err == nil && st.Installed() && st.Shapes["server"] != "" && st.Shapes["server"] != string(o.Shape(plan.Server)) { + return nil, fmt.Errorf("the server is installed here as a %s install; moving it to a %s install is not built yet", + st.Shapes["server"], o.Shape(plan.Server)) + } + needContainers := !onHost || o.Shape(plan.Console) == plan.Container || + o.Shape(plan.Webmail) == plan.Container || (!o.Local && o.Proxy == "caddy") if len(p.Needs) > 0 && o.InstallDeps { log.Step("installing what this machine is missing") @@ -93,7 +103,7 @@ func Run(ctx context.Context, p plan.Plan, f host.Facts, log Log) (*Result, erro // with the old facts meant reaching for a docker that was never // going to be there. f = host.Survey(ctx) - if !f.Runtime.Usable { + if needContainers && !f.Runtime.Usable { return nil, fmt.Errorf("after installing what was missing, containers still are not usable: %s", f.Runtime.Why) } log.Info("using %s %s", f.Runtime.Kind, f.Runtime.Version) @@ -103,6 +113,8 @@ func Run(ctx context.Context, p plan.Plan, f host.Facts, log Log) (*Result, erro stack := compose.Stack{ Version: "dev", Project: "inbuxa", + Server: !onHost, + HostServer: onHost, Domain: o.Domain, Email: o.ACMEEmail, Local: o.Local, @@ -155,9 +167,14 @@ func Run(ctx context.Context, p plan.Plan, f host.Facts, log Log) (*Result, erro if err := os.MkdirAll(dir, 0o750); err != nil { return nil, err } - bundle, err := docker.SystemCABundle(ctx, stack.ServerImage) + var bundle []byte + if onHost { + bundle, err = systemCABundle() + } else { + bundle, err = docker.SystemCABundle(ctx, stack.ServerImage) + } if err != nil { - return nil, fmt.Errorf("reading the server image's CA bundle: %w", err) + return nil, fmt.Errorf("reading the server's CA bundle: %w", err) } if err := os.WriteFile(filepath.Join(dir, "ca-bundle.crt"), append(bundle, root...), 0o644); err != nil { return nil, err @@ -171,19 +188,59 @@ func Run(ctx context.Context, p plan.Plan, f host.Facts, log Log) (*Result, erro stack.ACMEDirectory = o.ACMEDirectory log.Step("writing the deployment into %s", dir) - if _, err := compose.Write(dir, &stack); err != nil { - return nil, err + var sec compose.Secrets + var err error + if stack.Empty() { + if sec, err = compose.EnsureSecrets(dir); err != nil { + return nil, err + } + log.Info(".env (nothing runs in containers here)") + } else { + if sec, err = compose.Write(dir, &stack); err != nil { + return nil, err + } + log.Info("compose.yaml, .env%s", map[bool]string{true: " and Caddyfile", false: ""}[stack.Proxy]) } - log.Info("compose.yaml, .env%s", map[bool]string{true: " and Caddyfile", false: ""}[stack.Proxy]) // Whichever runtime the survey found: docker where there is one, podman // on the Red Hat family, driven through the same compose plugin. docker.UseRuntime(f.Runtime) cmp := docker.Compose{Dir: dir, Runtime: f.Runtime} - log.Step("fetching the images") - if err := cmp.Run(ctx, "pull", "--quiet"); err != nil { - return nil, err + if !stack.Empty() { + log.Step("fetching the images") + if err := cmp.Run(ctx, "pull", "--quiet"); err != nil { + return nil, err + } + } + + // The server on the host: what a container would have brought with it. + var hs *hostServer + var srvLogs logSource = cmp + if onHost { + hs = &hostServer{deploy: dir, log: log} + srvLogs = hs + log.Step("installing the mail server on this machine") + if err := hs.Install(ctx); err != nil { + return nil, err + } + env := map[string]string{ + "INBUXA_ADMIN_URL": stack.ConsoleURL, + "INBUXA_WEBMAIL_URL": stack.WebmailURL, + } + if stack.Webmail { + env["INBUXA_WEBMAIL_CLIENT_SECRET"] = sec.WebmailOAuth + } + if o.Local { + env["INBUXA_PUBLIC_URL"] = stack.ServerPublicURL + } + if stack.CABundle { + env["SSL_CERT_FILE"] = filepath.Join(dir, "ca-bundle.crt") + } + if _, err := hs.WriteEnv(env); err != nil { + return nil, err + } + log.Info("%s, %s", hostUnitPath, hs.envPath()) } // A machine that already runs this installation is converged, not set up @@ -193,7 +250,7 @@ func Run(ctx context.Context, p plan.Plan, f host.Facts, log Log) (*Result, erro // moment it was configured -- which is exactly what adding or removing a // front end used to do, and why it could not. if existing, err := state.Load(); err == nil && existing.Installed() { - return converge(ctx, o, stack, cmp, existing, f, log) + return converge(ctx, o, stack, cmp, hs, existing, f, log) } // The bootstrap credential lives in an override file for this step only, @@ -212,18 +269,30 @@ func Run(ctx context.Context, p plan.Plan, f host.Facts, log Log) (*Result, erro defer os.Remove(override) log.Step("starting the mail server in bootstrap mode") - boot := cmp - boot.Files = []string{override} - boot.Env = []string{"INBUXA_BOOTSTRAP_ADMIN=admin:" + bootPass} - if err := boot.Run(ctx, "up", "-d", "server"); err != nil { - return nil, err + serverURL := "http://" + stack.ServerBind + if onHost { + // Its default listeners until they are fitted to the host, behind a + // drop-in that keeps everything but loopback out meanwhile. + serverURL = hostSetupBind + if err := hs.WriteUnit(ctx, false); err != nil { + return nil, err + } + if err := hs.Start(ctx, modeBootstrap, bootPass); err != nil { + return nil, withLogs(ctx, err, srvLogs, "server") + } + } else { + boot := cmp + boot.Files = []string{override} + boot.Env = []string{"INBUXA_BOOTSTRAP_ADMIN=admin:" + bootPass} + if err := boot.Run(ctx, "up", "-d", "server"); err != nil { + return nil, err + } } - serverURL := "http://" + stack.ServerBind if err := waitFor(ctx, log, "the mail server", 120*time.Second, func(ctx context.Context) error { return live(ctx, serverURL) }); err != nil { - return nil, withLogs(ctx, err, cmp, "server") + return nil, withLogs(ctx, err, srvLogs, "server") } bootClient := &jmap.Client{BaseURL: serverURL, Username: "admin", Password: bootPass} @@ -232,7 +301,11 @@ func Run(ctx context.Context, p plan.Plan, f host.Facts, log Log) (*Result, erro } log.Step("setting up %s (hostname %s)", o.Domain, o.MailHost) - admin, err := bootClient.Bootstrap(ctx, o.MailHost, o.Domain) + dataPath := "" + if onHost { + dataPath = hs.DataPath() + } + admin, err := bootClient.Bootstrap(ctx, o.MailHost, o.Domain, dataPath) if err != nil { return nil, fmt.Errorf("bootstrap: %w", err) } @@ -262,10 +335,16 @@ func Run(ctx context.Context, p plan.Plan, f host.Facts, log Log) (*Result, erro } defer os.Remove(basicOverride) log.Step("restarting the mail server configured, to give the setup an API key") - basic := cmp - basic.Files = []string{basicOverride} - if err := basic.Run(ctx, "up", "-d", "server"); err != nil { - return res, err + if onHost { + if err := hs.Start(ctx, modeBasic, ""); err != nil { + return res, withLogs(ctx, err, srvLogs, "server") + } + } else { + basic := cmp + basic.Files = []string{basicOverride} + if err := basic.Run(ctx, "up", "-d", "server"); err != nil { + return res, err + } } adminBasic := &jmap.Client{BaseURL: serverURL, Username: admin.Username, Password: admin.Secret} var keyID, key string @@ -274,7 +353,7 @@ func Run(ctx context.Context, p plan.Plan, f host.Facts, log Log) (*Result, erro keyID, key, err = adminBasic.CreateAPIKey(ctx, "inbuxa installer, for this setup only", time.Hour) return err }); err != nil { - return res, withLogs(ctx, fmt.Errorf("an API key for the setup: %w", err), cmp, "server") + return res, withLogs(ctx, fmt.Errorf("an API key for the setup: %w", err), srvLogs, "server") } srv := &jmap.Client{BaseURL: serverURL, Token: key} defer func() { @@ -285,21 +364,42 @@ func Run(ctx context.Context, p plan.Plan, f host.Facts, log Log) (*Result, erro } }() + if onHost { + log.Step("fitting the server's listeners to the host") + if err := srv.HostListeners(ctx, hostFinalBind, o.Local); err != nil { + return res, fmt.Errorf("moving the server's listeners: %w", err) + } + log.Info("plain HTTP on %s for the proxy and the webmail; 443 is left to the proxy", hostFinalBind) + if o.Local { + log.Info("the mail ports on loopback only") + } + if err := hs.WriteUnit(ctx, true); err != nil { + return res, err + } + if err := hs.Start(ctx, modeNormal, ""); err != nil { + return res, withLogs(ctx, err, srvLogs, "server") + } + serverURL = "http://" + hostFinalBind + srv.BaseURL = serverURL + } + log.Step("starting the rest of the stack") // Without the override, so the server is recreated taking tokens only. // --remove-orphans: the compose file is rendered from the shapes asked // for, so a component the topology no longer lists is simply not in it // any more. Without this its container would keep running, belonging to // a project that no longer describes it. - if err := cmp.Run(ctx, "up", "-d", "--remove-orphans"); err != nil { - return res, err + if !stack.Empty() { + if err := cmp.Run(ctx, "up", "-d", "--remove-orphans"); err != nil { + return res, err + } } if err := waitFor(ctx, log, "the server to come back configured", 120*time.Second, func(ctx context.Context) error { _, err := srv.DomainID(ctx, o.Domain) return err }); err != nil { - return res, withLogs(ctx, err, cmp, "server") + return res, withLogs(ctx, err, srvLogs, "server") } domainID, err := srv.DomainID(ctx, o.Domain) if err != nil { @@ -310,32 +410,41 @@ func Run(ctx context.Context, p plan.Plan, f host.Facts, log Log) (*Result, erro // sign-in, every push stream opened and dropped as tabs come and go. The // server bans per address, so a ban on that one is a ban on everybody's // webmail. The webmail rate-limits sign-ins per real client itself. + // On the host both arrive from loopback, which is exempted once. + allowed := map[string]bool{} if stack.Webmail { log.Step("exempting the webmail from the auto-ban") if err := srv.AllowIP(ctx, stack.WebmailIP, "the webmail: every request arrives from this address"); err != nil { return res, fmt.Errorf("exempting the webmail: %w", err) } + allowed[stack.WebmailIP] = true } if stack.Proxy { log.Step("trusting the proxy's X-Forwarded-For") if err := srv.TrustForwardedFor(ctx); err != nil { return res, fmt.Errorf("trusting the proxy: %w", err) } - if err := srv.AllowIP(ctx, stack.CaddyIP, "the proxy: certificate renewals and autoconfig arrive from this address"); err != nil { - return res, fmt.Errorf("exempting the proxy: %w", err) + if !allowed[stack.CaddyIP] { + if err := srv.AllowIP(ctx, stack.CaddyIP, "the proxy: certificate renewals and autoconfig arrive from this address"); err != nil { + return res, fmt.Errorf("exempting the proxy: %w", err) + } } } if stack.Webmail || stack.Proxy { // Neither setting takes effect on a running server. log.Info("restarting the server so those take effect") - if err := cmp.Run(ctx, "restart", "server"); err != nil { + restart := func() error { return cmp.Run(ctx, "restart", "server") } + if onHost { + restart = func() error { return hs.Restart(ctx) } + } + if err := restart(); err != nil { return res, err } if err := waitFor(ctx, log, "the server to restart", 120*time.Second, func(ctx context.Context) error { _, err := srv.DomainID(ctx, o.Domain) return err }); err != nil { - return res, withLogs(ctx, err, cmp, "server") + return res, withLogs(ctx, err, srvLogs, "server") } } @@ -443,7 +552,7 @@ func waitForCertificate(ctx context.Context, srv *jmap.Client, log Log, domainID // brought up, and anything no longer in the file goes with --remove-orphans. // Nothing touches the mail: data volumes are left alone, and a component // that is removed can be added back with what it had. -func converge(ctx context.Context, o plan.Options, stack compose.Stack, cmp docker.Compose, +func converge(ctx context.Context, o plan.Options, stack compose.Stack, cmp docker.Compose, hs *hostServer, st *state.State, f host.Facts, log Log) (*Result, error) { res := &Result{ @@ -458,6 +567,10 @@ func converge(ctx context.Context, o plan.Options, stack compose.Stack, cmp dock } } + if hs != nil { + return convergeHost(ctx, o, stack, cmp, hs, st, f, log, res) + } + // Before anything is recreated: an install from before #5 keeps the // server's data where the corrected compose file no longer mounts it. moved, err := adoptServerVolumes(ctx, cmp, stack.Project, log) @@ -504,8 +617,57 @@ func converge(ctx context.Context, o plan.Options, stack compose.Stack, cmp dock } } + saveState(st, o, f, log) + return res, nil +} + +// convergeHost is converge for a server on the host: the binary, the unit +// and its environment are rewritten if they changed, whatever runs in +// containers beside it is brought in line, and the server is restarted only +// if what it reads at start is different. +func convergeHost(ctx context.Context, o plan.Options, stack compose.Stack, cmp docker.Compose, hs *hostServer, + st *state.State, f host.Facts, log Log, res *Result) (*Result, error) { + + // Run already wrote the environment; reading it back as it was before is + // not possible, so whether it changed is decided by the shapes, as for a + // container. + if err := hs.WriteUnit(ctx, true); err != nil { + return res, err + } + log.Step("bringing the stack in line with the file") + if stack.Empty() { + if _, err := os.Stat(filepath.Join(o.Dir, "compose.yaml")); err == nil { + // Everything that ran beside the server was removed. + if err := cmp.Run(ctx, "down", "--remove-orphans"); err != nil { + return res, err + } + os.Remove(filepath.Join(o.Dir, "compose.yaml")) + os.Remove(filepath.Join(o.Dir, "Caddyfile")) + } + } else if err := cmp.Run(ctx, "up", "-d", "--remove-orphans"); err != nil { + return res, err + } + _, active := discover.HostServer(ctx) + frontEnds := st.Shapes["console"] != string(o.Shape(plan.Console)) || st.Shapes["webmail"] != string(o.Shape(plan.Webmail)) + if frontEnds || !active { + log.Info("restarting the server") + if err := hs.Restart(ctx); err != nil { + return res, withLogs(ctx, err, hs, "server") + } + if err := waitFor(ctx, log, "the server to answer again", 120*time.Second, func(ctx context.Context) error { + return live(ctx, "http://"+hostFinalBind) + }); err != nil { + return res, withLogs(ctx, err, hs, "server") + } + } + saveState(st, o, f, log) + return res, nil +} + +func saveState(st *state.State, o plan.Options, f host.Facts, log Log) { st.Machine, st.Dir, st.Domain = o.Machine, o.Dir, o.Domain st.Runtime, st.Topology = f.Runtime.Kind, o.TopologyPath + st.Installer = "server " + release.ServerVersion st.Shapes = map[string]string{} for _, c := range []plan.Component{plan.Server, plan.Console, plan.Webmail} { if sh := o.Shape(c); sh != plan.Skip { @@ -515,7 +677,6 @@ func converge(ctx context.Context, o plan.Options, stack compose.Stack, cmp dock if err := st.Save(); err != nil { log.Info("could not write %s: %v", state.Path, err) } - return res, nil } // Verify is the last step: does what was installed actually answer? @@ -604,7 +765,13 @@ func waitFor(ctx context.Context, log Log, what string, limit time.Duration, pro return fmt.Errorf("waited %s for %s: %w", limit, what, last) } -func withLogs(ctx context.Context, err error, c docker.Compose, service string) error { +// logSource is wherever a server's last lines come from: its container, or +// the journal. +type logSource interface { + Logs(ctx context.Context, service string, lines int) string +} + +func withLogs(ctx context.Context, err error, c logSource, service string) error { logs := c.Logs(ctx, service, 25) if strings.TrimSpace(logs) == "" { return err diff --git a/internal/apply/hostserver.go b/internal/apply/hostserver.go new file mode 100644 index 0000000..36f6adc --- /dev/null +++ b/internal/apply/hostserver.go @@ -0,0 +1,322 @@ +// SPDX-FileCopyrightText: 2026 Coffey Labs LLC +// SPDX-License-Identifier: AGPL-3.0-or-later + +package apply + +// The mail server on the host: the release binary, a user of its own, a +// directory for its configuration and data, and a systemd unit. +// +// First boot is the same sequence as in a container, and the differences are +// all about what a container would otherwise have contained: +// +// - Setup runs behind a drop-in that lets the server talk to loopback and +// nothing else. In a container nothing is published until the compose +// file says so; on the host the server binds every interface the moment +// it starts, and during setup it takes the bootstrap credential, then +// passwords on /jmap. The drop-in lives under /run, so even a setup that +// stops halfway leaves nothing behind after a reboot. +// - The credential for each setup step is in a file only root can read, +// not in the unit, which anyone can. +// - After setup the listeners are fitted to the host (jmap.HostListeners): +// plain HTTP on loopback, where the proxy and the webmail reach it, and +// no HTTPS listener of its own on 443, which is the proxy's. +// - The finished unit refuses to start without a configuration file, so a +// server that loses its configuration stays down instead of coming back +// in bootstrap mode on every interface. + +import ( + "bytes" + "context" + "fmt" + "os" + "os/exec" + "path/filepath" + "runtime" + "sort" + "strings" + + "git.coffeylabs.org/inbuxa/inbuxa-installer/internal/deps" + "git.coffeylabs.org/inbuxa/inbuxa-installer/internal/discover" + "git.coffeylabs.org/inbuxa/inbuxa-installer/internal/release" +) + +const ( + // HostServerBinary is where the server's binary goes. Not + // /usr/local/bin/inbuxa, which is this installer. + HostServerBinary = "/usr/local/bin/inbuxa-server" + HostServerUnit = discover.HostServerUnit + HostServerUser = "inbuxa" + + hostUnitPath = discover.HostServerUnitPath + setupDropIn = "/run/systemd/system/" + HostServerUnit + ".d/inbuxa-setup.conf" + setupEnvPath = "/run/inbuxa-server-setup.env" + hostSetupBind = "http://127.0.0.1:8080" // the default listeners, before they are fitted + hostFinalBind = "127.0.0.1:8081" // where a container install publishes it, too +) + +// setupMode is how the server is started for each step of first boot. +type setupMode int + +const ( + modeBootstrap setupMode = iota // the bootstrap credential, loopback only + modeBasic // configured, passwords on /jmap, loopback only + modeNormal // as it will run from now on +) + +// hostServer is the server as a host install, rooted in the deployment +// directory: /server holds config.json and data/, /server.env what +// the unit reads into the server's environment. +type hostServer struct { + deploy string + log Log +} + +func (h hostServer) home() string { return filepath.Join(h.deploy, "server") } +func (h hostServer) configPath() string { return filepath.Join(h.home(), "config.json") } +func (h hostServer) envPath() string { return filepath.Join(h.deploy, "server.env") } + +// DataPath is the built-in store's directory, given to bootstrap. +func (h hostServer) DataPath() string { return filepath.Join(h.home(), "data") + "/" } + +// Install puts the release binary, the user and the directories in place. It +// changes nothing that is already right, so a converge can call it too. +func (h hostServer) Install(ctx context.Context) error { + if v := binaryVersion(ctx, HostServerBinary); strings.Contains(v, release.ServerVersion) { + h.log.Info("%s is already %s", HostServerBinary, release.ServerVersion) + } else { + if err := h.fetchBinary(ctx); err != nil { + return err + } + } + + if err := exec.CommandContext(ctx, "getent", "passwd", HostServerUser).Run(); err != nil { + if err := runQuiet(ctx, "useradd", "--system", "--user-group", "--no-create-home", + "--home-dir", h.home(), "--shell", nologin(), HostServerUser); err != nil { + return fmt.Errorf("creating the %s user: %w", HostServerUser, err) + } + h.log.Info("user %s (system, no login)", HostServerUser) + } + + // The deployment directory is root's and holds the credentials, which + // are 0600 on their own; the server only needs to pass through it to + // reach its own directory. + if err := os.MkdirAll(h.deploy, 0o750); err != nil { + return err + } + if err := os.Chmod(h.deploy, 0o711); err != nil { + return err + } + for _, d := range []string{h.home(), h.DataPath()} { + if err := os.MkdirAll(d, 0o750); err != nil { + return err + } + if err := runQuiet(ctx, "chown", HostServerUser+":"+HostServerUser, d); err != nil { + return err + } + } + return nil +} + +func (h hostServer) fetchBinary(ctx context.Context) error { + arch := runtime.GOARCH + sum, ok := release.ServerSHA256[arch] + if !ok { + return fmt.Errorf("inbuxa-server %s has no release binary for %s", release.ServerVersion, arch) + } + h.log.Step("fetching inbuxa-server %s for %s", release.ServerVersion, arch) + work, err := os.MkdirTemp("", "inbuxa-server-") + if err != nil { + return err + } + defer os.RemoveAll(work) + tarball := filepath.Join(work, "inbuxa.tar.gz") + if err := deps.FetchVerified(ctx, h.log.Out(), release.ServerTarball(arch), sum, tarball, 0o644); err != nil { + return err + } + if err := runQuiet(ctx, "tar", "-xzf", tarball, "-C", work, "inbuxa"); err != nil { + return fmt.Errorf("unpacking the release: %w", err) + } + // Run before it is put in place: a binary this machine's C library + // cannot load is caught here, not by a unit that restarts forever. + got := binaryVersion(ctx, filepath.Join(work, "inbuxa")) + if !strings.Contains(got, release.ServerVersion) { + return fmt.Errorf("the release binary does not run here (it said %q); run the server as a container on this machine", got) + } + if err := runQuiet(ctx, "install", "-m", "0755", filepath.Join(work, "inbuxa"), HostServerBinary); err != nil { + return err + } + h.log.Info("%s, %s", HostServerBinary, strings.TrimSpace(got)) + return nil +} + +// WriteEnv writes what the server reads from its environment, and says +// whether that changed: the server only sees it at a start. +func (h hostServer) WriteEnv(vars map[string]string) (bool, error) { + keys := make([]string, 0, len(vars)) + for k, v := range vars { + if v != "" { + keys = append(keys, k) + } + } + sort.Strings(keys) + var b strings.Builder + b.WriteString("# Written by the inbuxa installer: the mail server's environment.\n") + b.WriteString("# Read by " + HostServerUnit + " at each start.\n") + for _, k := range keys { + fmt.Fprintf(&b, "%s=%s\n", k, vars[k]) + } + return writeIfChanged(h.envPath(), b.String(), 0o600) +} + +// WriteUnit writes the unit. configured adds the condition that a finished +// install keeps: no configuration file, no start. +func (h hostServer) WriteUnit(ctx context.Context, configured bool) error { + cond := "" + if configured { + cond = "# A server that has lost its configuration stays down rather than coming\n" + + "# back in bootstrap mode.\n" + + "ConditionPathExists=" + h.configPath() + "\n" + } + unit := fmt.Sprintf(`# Written by the inbuxa installer. Changes here are replaced on its next run. +[Unit] +Description=inbuxa mail server +Documentation=https://docs.inbuxa.org/ +After=network-online.target +Wants=network-online.target +%s +[Service] +Type=simple +User=%s +Group=%s +EnvironmentFile=%s +ExecStart=%s --config %s +WorkingDirectory=%s +# The mail ports and 443 are below 1024; this lets an unprivileged user +# bind them, and survives the binary being replaced, which setcap does not. +AmbientCapabilities=CAP_NET_BIND_SERVICE +CapabilityBoundingSet=CAP_NET_BIND_SERVICE +NoNewPrivileges=true +ProtectSystem=full +ProtectHome=true +PrivateTmp=true +LimitNOFILE=65536 +KillSignal=SIGINT +Restart=on-failure +RestartSec=5 + +[Install] +WantedBy=multi-user.target +`, cond, HostServerUser, HostServerUser, h.envPath(), HostServerBinary, h.configPath(), h.home()) + changed, err := writeIfChanged(hostUnitPath, unit, 0o644) + if err != nil { + return err + } + if changed { + if err := runQuiet(ctx, "systemctl", "daemon-reload"); err != nil { + return err + } + } + return runQuiet(ctx, "systemctl", "enable", "--quiet", HostServerUnit) +} + +// Start (re)starts the server for a step of first boot, or as it runs from +// then on. +func (h hostServer) Start(ctx context.Context, mode setupMode, bootPass string) error { + switch mode { + case modeNormal: + os.Remove(setupDropIn) + os.Remove(setupEnvPath) + default: + env := "INBUXA_HTTP_BASIC_AUTH=all\n" + if mode == modeBootstrap { + env = "INBUXA_RECOVERY_ADMIN=admin:" + bootPass + "\n" + } + if err := os.WriteFile(setupEnvPath, []byte(env), 0o600); err != nil { + return err + } + if err := os.MkdirAll(filepath.Dir(setupDropIn), 0o755); err != nil { + return err + } + dropIn := "# The inbuxa installer, during first boot only. Gone at the next reboot.\n" + + "[Service]\n" + + "EnvironmentFile=" + setupEnvPath + "\n" + + "IPAddressAllow=localhost\n" + + "IPAddressDeny=any\n" + if err := os.WriteFile(setupDropIn, []byte(dropIn), 0o644); err != nil { + return err + } + } + if err := runQuiet(ctx, "systemctl", "daemon-reload"); err != nil { + return err + } + return runQuiet(ctx, "systemctl", "restart", HostServerUnit) +} + +// Restart restarts the server as it is. +func (h hostServer) Restart(ctx context.Context) error { + return runQuiet(ctx, "systemctl", "restart", HostServerUnit) +} + +// Logs is the server's last lines from the journal, for an error message. +// The service argument is there so it reads like a compose project's. +func (h hostServer) Logs(ctx context.Context, _ string, lines int) string { + out, _ := exec.CommandContext(ctx, "journalctl", "-u", HostServerUnit, + "-n", fmt.Sprint(lines), "--no-pager", "-o", "cat").CombinedOutput() + return string(out) +} + +// systemCABundle is this machine's trust store, which a private ACME CA's +// root is added to for a server on the host, as it is to the image's for a +// server in a container. +func systemCABundle() ([]byte, error) { + for _, p := range []string{ + "/etc/ssl/certs/ca-certificates.crt", // Debian, Ubuntu, Arch + "/etc/pki/tls/certs/ca-bundle.crt", // Fedora, Rocky + "/etc/ssl/ca-bundle.pem", // openSUSE + } { + if b, err := os.ReadFile(p); err == nil { + return b, nil + } + } + return nil, fmt.Errorf("no system CA bundle found") +} + +// binaryVersion is what a binary says --version is, or what it said instead +// when it could not run: a missing library is reported in that output. +func binaryVersion(ctx context.Context, path string) string { + out, _ := exec.CommandContext(ctx, path, "--version").CombinedOutput() + return strings.TrimSpace(string(out)) +} + +func nologin() string { + for _, p := range []string{"/usr/sbin/nologin", "/sbin/nologin", "/usr/bin/nologin"} { + if _, err := os.Stat(p); err == nil { + return p + } + } + return "/bin/false" +} + +func writeIfChanged(path, content string, mode os.FileMode) (bool, error) { + if old, err := os.ReadFile(path); err == nil && bytes.Equal(old, []byte(content)) { + return false, os.Chmod(path, mode) + } + if err := os.WriteFile(path, []byte(content), mode); err != nil { + return false, err + } + return true, os.Chmod(path, mode) +} + +func runQuiet(ctx context.Context, name string, args ...string) error { + var out bytes.Buffer + cmd := exec.CommandContext(ctx, name, args...) + cmd.Stdout, cmd.Stderr = &out, &out + if err := cmd.Run(); err != nil { + msg := strings.TrimSpace(out.String()) + if msg == "" { + return fmt.Errorf("%s %s: %w", name, strings.Join(args, " "), err) + } + return fmt.Errorf("%s %s: %w: %s", name, strings.Join(args, " "), err, msg) + } + return nil +} diff --git a/internal/compose/compose.go b/internal/compose/compose.go index 225a8ca..af54ff8 100644 --- a/internal/compose/compose.go +++ b/internal/compose/compose.go @@ -39,10 +39,18 @@ type Stack struct { ConsoleHost string WebmailHost string + Server bool // the server runs in this compose project Console bool Webmail bool Proxy bool + // HostServer is a mail server running on this machine but outside the + // project, under systemd. The front ends and the proxy then reach it on + // loopback, so the ones that talk to it -- the webmail and Caddy -- share + // the host's network instead of the project's. The console does not: + // only the browser talks to the server from it. + HostServer bool + ServerImage string ConsoleImage string WebmailImage string @@ -125,29 +133,18 @@ type Secrets struct { // goes, and the caller needs them: the mail server is told which addresses // the front ends speak from, and passing a copy meant telling it "". func Write(dir string, s *Stack) (Secrets, error) { - var sec Secrets - if err := os.MkdirAll(dir, 0o750); err != nil { + sec, err := EnsureSecrets(dir) + if err != nil { return sec, err } if err := s.Addresses(); err != nil { return sec, err } - - // Secrets are generated once and kept. Rewriting the deployment to add - // or remove a component must not roll the webmail's session key or the - // OAuth client secret the server was told: the point of converging is - // that everything not being changed stays as it was. - sec = readSecrets(filepath.Join(dir, ".env")) - var err error - if sec.AppSecret == "" { - if sec.AppSecret, err = secret(); err != nil { - return sec, err - } - } - if sec.WebmailOAuth == "" { - if sec.WebmailOAuth, err = secret(); err != nil { - return sec, err - } + if s.HostServer { + // Everything that talks to the server is on the host's network and + // arrives from loopback, which is the address the server is told to + // exempt from the auto-ban. + s.WebmailIP, s.CaddyIP = "127.0.0.1", "127.0.0.1" } funcs := template.FuncMap{"join": strings.Join} @@ -171,8 +168,34 @@ func Write(dir string, s *Stack) (Secrets, error) { } } - // The secrets live beside the compose file, not in it, so the compose - // file can be read to somebody, pasted into an issue, or committed. + return sec, nil +} + +// EnsureSecrets returns the deployment's secrets, generating and writing any +// that do not exist yet. They are generated once and kept: rewriting the +// deployment to add or remove a component must not roll the webmail's +// session key or the OAuth client secret the server was told, because the +// point of converging is that everything not being changed stays as it was. +// +// The secrets live in .env beside the compose file, not in it, so the compose +// file can be read to somebody, pasted into an issue, or committed. A server +// on the host reads the one it needs from there too. +func EnsureSecrets(dir string) (Secrets, error) { + if err := os.MkdirAll(dir, 0o750); err != nil { + return Secrets{}, err + } + sec := readSecrets(filepath.Join(dir, ".env")) + var err error + if sec.AppSecret == "" { + if sec.AppSecret, err = secret(); err != nil { + return sec, err + } + } + if sec.WebmailOAuth == "" { + if sec.WebmailOAuth, err = secret(); err != nil { + return sec, err + } + } env := fmt.Sprintf("APP_SECRET=%s\nWEBMAIL_CLIENT_SECRET=%s\n", sec.AppSecret, sec.WebmailOAuth) if err := os.WriteFile(filepath.Join(dir, ".env"), []byte(env), 0o600); err != nil { return sec, err @@ -180,6 +203,25 @@ func Write(dir string, s *Stack) (Secrets, error) { return sec, nil } +// Empty is whether the stack has nothing to run: a server on the host with no +// front ends and no proxy here. Then there is no compose project at all. +func (s Stack) Empty() bool { + return !s.Server && !s.Console && !s.Webmail && !s.Proxy +} + +// Upstreams are where Caddy finds each program: by service name on the +// project's network, or on loopback when it shares the host's. +func (s Stack) ServerUpstream() string { return s.pick("server:8080", "127.0.0.1:8081") } +func (s Stack) WebmailUpstream() string { return s.pick("webmail:8080", "127.0.0.1:8080") } +func (s Stack) ConsoleUpstream() string { return s.pick("console:8080", "127.0.0.1:8082") } + +func (s Stack) pick(project, host string) string { + if s.HostServer { + return host + } + return project +} + // readSecrets recovers what a previous run generated, so a converge keeps // them. A missing or unreadable file means a first install, and new ones. func readSecrets(path string) Secrets { diff --git a/internal/compose/compose_test.go b/internal/compose/compose_test.go new file mode 100644 index 0000000..dd1ccac --- /dev/null +++ b/internal/compose/compose_test.go @@ -0,0 +1,87 @@ +// SPDX-FileCopyrightText: 2026 Coffey Labs LLC +// SPDX-License-Identifier: AGPL-3.0-or-later + +package compose + +import ( + "os" + "path/filepath" + "strings" + "testing" +) + +func stack(hostServer bool) *Stack { + return &Stack{ + Version: "test", Project: "inbuxa", Domain: "example.com", Email: "postmaster@example.com", + MailHost: "mail.example.com", ConsoleHost: "admin.example.com", WebmailHost: "webmail.example.com", + Server: !hostServer, HostServer: hostServer, Console: true, Webmail: true, Proxy: true, + ServerImage: "s", ConsoleImage: "c", WebmailImage: "w", CaddyImage: "k", + ServerBind: "127.0.0.1:8081", ConsoleBind: "127.0.0.1:8082", WebmailBind: "127.0.0.1:8080", + Subnet: "172.31.253.0/24", MailPorts: []int{25}, + ServerPublicURL: "https://mail.example.com", ConsoleURL: "https://admin.example.com", WebmailURL: "https://webmail.example.com", + } +} + +func render(t *testing.T, s *Stack) (compose, caddy string) { + t.Helper() + dir := t.TempDir() + if _, err := Write(dir, s); err != nil { + t.Fatal(err) + } + a, _ := os.ReadFile(filepath.Join(dir, "compose.yaml")) + b, _ := os.ReadFile(filepath.Join(dir, "Caddyfile")) + return string(a), string(b) +} + +func TestContainerServerIsInTheProject(t *testing.T) { + c, k := render(t, stack(false)) + for _, want := range []string{"\n server:\n", "depends_on: [server]", "MAIL_SERVER_URL: http://server:8080", "inbuxa-data:/var/lib/inbuxa"} { + if !strings.Contains(c, want) { + t.Errorf("compose.yaml lacks %q", want) + } + } + if strings.Contains(c, "network_mode: host") { + t.Error("a container install put something on the host's network") + } + if !strings.Contains(k, "reverse_proxy server:8080") || !strings.Contains(k, "reverse_proxy webmail:8080") { + t.Errorf("Caddyfile does not use the project's names:\n%s", k) + } +} + +func TestHostServerIsReachedOnLoopback(t *testing.T) { + s := stack(true) + c, k := render(t, s) + for _, bad := range []string{"\n server:\n", "depends_on", "inbuxa-data:", "http://server:8080"} { + if strings.Contains(c, bad) { + t.Errorf("compose.yaml still has %q", bad) + } + } + for _, want := range []string{"network_mode: host", "HOST: 127.0.0.1", "MAIL_SERVER_URL: http://127.0.0.1:8081", "\"127.0.0.1:8082:8080\""} { + if !strings.Contains(c, want) { + t.Errorf("compose.yaml lacks %q", want) + } + } + for _, want := range []string{"reverse_proxy 127.0.0.1:8081", "reverse_proxy 127.0.0.1:8080", "reverse_proxy 127.0.0.1:8082"} { + if !strings.Contains(k, want) { + t.Errorf("Caddyfile lacks %q", want) + } + } + if s.WebmailIP != "127.0.0.1" || s.CaddyIP != "127.0.0.1" { + t.Errorf("the server would exempt %s and %s, not loopback", s.WebmailIP, s.CaddyIP) + } +} + +func TestSecretsSurviveARewrite(t *testing.T) { + dir := t.TempDir() + a, err := EnsureSecrets(dir) + if err != nil { + t.Fatal(err) + } + b, err := Write(dir, stack(true)) + if err != nil { + t.Fatal(err) + } + if a != b { + t.Error("writing the deployment rolled the secrets") + } +} diff --git a/internal/compose/templates/Caddyfile.tmpl b/internal/compose/templates/Caddyfile.tmpl index f7016ac..277e0f4 100644 --- a/internal/compose/templates/Caddyfile.tmpl +++ b/internal/compose/templates/Caddyfile.tmpl @@ -25,7 +25,7 @@ # they are written rather than buffered. {{.WebmailHost}} { encode zstd gzip - reverse_proxy webmail:8080 { + reverse_proxy {{.WebmailUpstream}} { flush_interval -1 } } @@ -36,7 +36,7 @@ # nothing here proxies the API. {{.ConsoleHost}} { encode zstd gzip - reverse_proxy console:8080 + reverse_proxy {{.ConsoleUpstream}} } {{- end}} @@ -57,13 +57,13 @@ disable_http_challenge } } - reverse_proxy server:8080 + reverse_proxy {{.ServerUpstream}} } # Port 80 for the server's names is its challenge path and a redirect. {{range $i, $n := .ServerNames}}{{if $i}}, {{end}}http://{{$n}}{{end}} { handle /.well-known/acme-challenge/* { - reverse_proxy server:8080 + reverse_proxy {{.ServerUpstream}} } handle { redir https://{host}{uri} 308 diff --git a/internal/compose/templates/compose.yaml.tmpl b/internal/compose/templates/compose.yaml.tmpl index 0c86726..a925a36 100644 --- a/internal/compose/templates/compose.yaml.tmpl +++ b/internal/compose/templates/compose.yaml.tmpl @@ -4,12 +4,20 @@ # from this directory. Secrets are in .env beside it and the administrator's # password is in credentials.txt -- both readable only by root. # +{{- if .HostServer}} +# The mail server runs on this machine under systemd (inbuxa-server.service), +# not in this project. Its plain HTTP is on loopback only, which is why the +# webmail and Caddy share the host's network: that is where they reach it, as +# http://, a leg that never leaves this machine. +{{- else}} # The mail server's plain HTTP port is published on loopback only and reached # over the private network below, which is why the front ends talk to it as # http://: that leg never leaves this machine. +{{- end}} name: {{.Project}} services: +{{- if .Server}} server: image: {{.ServerImage}} hostname: {{.MailHost}} @@ -48,12 +56,15 @@ services: networks: stack: ipv4_address: {{.ServerIP}} +{{- end}} {{- if .Console}} console: image: {{.ConsoleImage}} restart: unless-stopped +{{- if .Server}} depends_on: [server] +{{- end}} # Not read-only, unlike the webmail: this image writes the api-base-url # into index.html when it starts, which is what lets one image serve any # installation. A read-only root makes it restart forever instead. @@ -73,15 +84,28 @@ services: webmail: image: {{.WebmailImage}} restart: unless-stopped +{{- if .Server}} depends_on: [server] +{{- end}} # Immutable: read-only root, no volume, sessions in memory. A restart # signs everyone out; nothing else is lost, because nothing else is kept. read_only: true tmpfs: [/tmp] +{{- if .HostServer}} + # On the host's network, listening on loopback only, beside the server. + network_mode: host +{{- else}} ports: - "{{.WebmailBind}}:8080" +{{- end}} environment: +{{- if .HostServer}} + HOST: 127.0.0.1 + PORT: "8080" + MAIL_SERVER_URL: http://127.0.0.1:8081 +{{- else}} MAIL_SERVER_URL: http://server:8080 +{{- end}} APP_SECRET: ${APP_SECRET:?APP_SECRET is missing from .env} APP_NAME: inbuxa IMMUTABLE: "1" @@ -106,20 +130,30 @@ services: # is not the webmail. PUSH_URL: {{.WebmailURL}} {{- end}} +{{- if not .HostServer}} networks: stack: ipv4_address: {{.WebmailIP}} {{- end}} +{{- end}} {{- if .Proxy}} caddy: image: {{.CaddyImage}} restart: unless-stopped +{{- if .Server}} depends_on: [server] +{{- end}} +{{- if .HostServer}} + # On the host's network: it holds 80 and 443 itself, and reaches the + # server, the webmail and the console on loopback. + network_mode: host +{{- else}} ports: - "80:80" - "443:443" - "443:443/udp" +{{- end}} volumes: - ./Caddyfile:/etc/caddy/Caddyfile:ro - caddy-data:/data @@ -127,20 +161,28 @@ services: {{- if .ACMECARoot}} - ./acme-ca-root.pem:/etc/caddy/acme-ca-root.pem:ro {{- end}} +{{- if not .HostServer}} networks: stack: ipv4_address: {{.CaddyIP}} {{- end}} +{{- end}} +{{- if or .Server .Console}} networks: stack: ipam: config: - subnet: {{.Subnet}} +{{- end}} +{{- if or .Server .Proxy}} volumes: +{{- end}} +{{- if .Server}} inbuxa-etc: inbuxa-data: +{{- end}} {{- if .Proxy}} # Certificates and the ACME account. Losing this means asking for every # certificate again, which is how rate limits are reached. diff --git a/internal/deps/deps.go b/internal/deps/deps.go index 9a7a542..f812e52 100644 --- a/internal/deps/deps.go +++ b/internal/deps/deps.go @@ -333,6 +333,14 @@ func Fixable(needs []Need) bool { return len(needs) > 0 } +// FetchVerified downloads url to dest, and puts it there only when its +// SHA-256 is want. It is how everything this program installs from the +// network arrives: against a checksum written into the source, never one +// fetched from beside the download. +func FetchVerified(ctx context.Context, log io.Writer, url, want, dest string, mode os.FileMode) error { + return fetchVerified(ctx, log, url, want, dest, mode) +} + func fetchVerified(ctx context.Context, log io.Writer, url, want, dest string, mode os.FileMode) error { req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil) if err != nil { diff --git a/internal/discover/discover.go b/internal/discover/discover.go index 8e1103a..e0c9f93 100644 --- a/internal/discover/discover.go +++ b/internal/discover/discover.go @@ -17,6 +17,7 @@ package discover import ( "context" "os" + "os/exec" "path/filepath" "regexp" "strings" @@ -35,6 +36,22 @@ type Found struct { Runtime string // what is running them Proxy bool // the deployment includes a proxy Credential bool // credentials.txt is there + HostServer bool // the server runs on the host, under systemd +} + +// HostServerUnit is the unit a host install of the server runs as. +const ( + HostServerUnit = "inbuxa-server.service" + HostServerUnitPath = "/etc/systemd/system/" + HostServerUnit +) + +// HostServer is whether the server is installed here as a host install, and +// whether systemd has it running. +func HostServer(ctx context.Context) (installed, active bool) { + if _, err := os.Stat(HostServerUnitPath); err != nil { + return false, false + } + return true, exec.CommandContext(ctx, "systemctl", "is-active", "--quiet", HostServerUnit).Run() == nil } // Dirs are where a deployment may be. The first is what this installer @@ -52,10 +69,16 @@ func Run(ctx context.Context, f host.Facts, dirs ...string) Found { if len(dirs) == 0 { dirs = Dirs } + // A server on the host is in no compose file: systemd is what knows. + if installed, active := HostServer(ctx); installed { + found.HostServer = true + found.Services["server"] = map[bool]string{true: "running", false: "stopped"}[active] + } for _, dir := range dirs { - if _, err := os.Stat(filepath.Join(dir, "compose.yaml")); err == nil { - found.Dir = dir - break + for _, marker := range []string{"compose.yaml", "server.env"} { + if _, err := os.Stat(filepath.Join(dir, marker)); err == nil && found.Dir == "" { + found.Dir = dir + } } } if found.Dir == "" { @@ -68,6 +91,7 @@ func Run(ctx context.Context, f host.Facts, dirs ...string) Found { b, err := os.ReadFile(filepath.Join(found.Dir, "compose.yaml")) if err != nil { + // A server on the host with nothing beside it has no compose file. return found } text := string(b) diff --git a/internal/jmap/client_test.go b/internal/jmap/client_test.go index 474dcd8..ffd1537 100644 --- a/internal/jmap/client_test.go +++ b/internal/jmap/client_test.go @@ -51,7 +51,7 @@ func TestBootstrapReturnsTheAdministrator(t *testing.T) { c, seen := fake(t, map[string]string{ "x:Bootstrap/set": `{"methodResponses":[["x:Bootstrap/set",{"updated":{"singleton":{"username":"admin@example.com","secret":"s3cret"}}},"0"]]}`, }) - a, err := c.Bootstrap(context.Background(), "mail.example.com", "example.com") + a, err := c.Bootstrap(context.Background(), "mail.example.com", "example.com", "") if err != nil { t.Fatal(err) } @@ -154,3 +154,72 @@ func TestBearerReplacesBasic(t *testing.T) { t.Fatal("a wrong token was accepted: the password must not be sent alongside it") } } + +func TestBootstrapOnTheHostNamesItsDataDirectory(t *testing.T) { + c, seen := fake(t, map[string]string{ + "x:Bootstrap/set": `{"methodResponses":[["x:Bootstrap/set",{"updated":{"singleton":{"username":"admin@example.com","secret":"s3cret"}}},"0"]]}`, + }) + if _, err := c.Bootstrap(context.Background(), "mail.example.com", "example.com", "/var/lib/inbuxa/server/data/"); err != nil { + t.Fatal(err) + } + update := (*seen)[0]["args"].(map[string]any)["update"].(map[string]any)["singleton"].(map[string]any) + store, _ := update["dataStore"].(map[string]any) + if store["@type"] != "RocksDb" || store["path"] != "/var/lib/inbuxa/server/data/" { + t.Errorf("dataStore = %v", update["dataStore"]) + } +} + +const defaultListeners = `{"methodResponses":[ + ["x:NetworkListener/query",{"ids":["a","b","c"]},"0"], + ["x:NetworkListener/get",{"list":[ + {"id":"a","name":"smtp","protocol":"smtp","tlsImplicit":false,"bind":{"[::]:25":true}}, + {"id":"b","name":"https","protocol":"http","tlsImplicit":true,"bind":{"[::]:443":true}}, + {"id":"c","name":"http","protocol":"http","tlsImplicit":false,"bind":{"[::]:8080":true}} + ]},"1"]]}` + +func TestHostListenersMoveHTTPAndLeave443ToTheProxy(t *testing.T) { + c, seen := fake(t, map[string]string{ + "x:NetworkListener/query": defaultListeners, + "x:NetworkListener/set": `{"methodResponses":[["x:NetworkListener/set",{"updated":{"c":null},"destroyed":["b"]},"0"]]}`, + }) + if err := c.HostListeners(context.Background(), "127.0.0.1:8081", false); err != nil { + t.Fatal(err) + } + args := (*seen)[1]["args"].(map[string]any) + update := args["update"].(map[string]any) + if len(update) != 1 { + t.Fatalf("updated %v, want only the plain HTTP listener", update) + } + bind := update["c"].(map[string]any)["bind"].(map[string]any) + if len(bind) != 1 || bind["127.0.0.1:8081"] != true { + t.Errorf("http bind = %v", bind) + } + if d := args["destroy"].([]any); len(d) != 1 || d[0] != "b" { + t.Errorf("destroy = %v, want the HTTPS listener", d) + } +} + +func TestHostListenersOnLoopbackForALocalInstall(t *testing.T) { + c, seen := fake(t, map[string]string{ + "x:NetworkListener/query": defaultListeners, + "x:NetworkListener/set": `{"methodResponses":[["x:NetworkListener/set",{"updated":{"a":null,"c":null},"destroyed":["b"]},"0"]]}`, + }) + if err := c.HostListeners(context.Background(), "127.0.0.1:8081", true); err != nil { + t.Fatal(err) + } + update := (*seen)[1]["args"].(map[string]any)["update"].(map[string]any) + bind := update["a"].(map[string]any)["bind"].(map[string]any) + if len(bind) != 1 || bind["127.0.0.1:25"] != true { + t.Errorf("smtp bind = %v, want loopback", bind) + } +} + +func TestHostListenersRefusalIsAnError(t *testing.T) { + c, _ := fake(t, map[string]string{ + "x:NetworkListener/query": defaultListeners, + "x:NetworkListener/set": `{"methodResponses":[["x:NetworkListener/set",{"notDestroyed":{"b":{"type":"forbidden","description":"no"}}},"0"]]}`, + }) + if err := c.HostListeners(context.Background(), "127.0.0.1:8081", false); err == nil { + t.Fatal("a refused destroy was not reported") + } +} diff --git a/internal/jmap/setup.go b/internal/jmap/setup.go index 7d5a143..a440b98 100644 --- a/internal/jmap/setup.go +++ b/internal/jmap/setup.go @@ -12,6 +12,7 @@ import ( "encoding/json" "errors" "fmt" + "strings" "time" ) @@ -40,7 +41,12 @@ func (c *Client) CheckBootstrapMode(ctx context.Context) error { // Bootstrap completes the setup wizard the web UI would otherwise walk someone // through, and returns the administrator it creates. The temporary bootstrap // account stops working once the server restarts out of bootstrap mode. -func (c *Client) Bootstrap(ctx context.Context, hostname, domain string) (Admin, error) { +// +// dataPath is where the built-in store keeps its files. Empty leaves the +// server's default, /var/lib/inbuxa/, which is the image's data volume; a +// server on the host is given a directory of its own, so its store does not +// share a directory with the deployment's files. +func (c *Client) Bootstrap(ctx context.Context, hostname, domain, dataPath string) (Admin, error) { update := map[string]any{ "serverHostname": hostname, "defaultDomain": domain, @@ -57,6 +63,9 @@ func (c *Client) Bootstrap(ctx context.Context, hostname, domain string) (Admin, "events": map[string]any{}, "eventsPolicy": "exclude", }, } + if dataPath != "" { + update["dataStore"] = map[string]any{"@type": "RocksDb", "path": dataPath} + } rs, err := c.Do(ctx, Call{"x:Bootstrap/set", map[string]any{"update": map[string]any{"singleton": update}}, "0"}) if err != nil { return Admin{}, err @@ -331,3 +340,87 @@ func (c *Client) DNSZone(ctx context.Context, domainID string) (string, error) { } return got.List[0].DNSZoneFile, nil } + +// Listener is one of the server's network listeners, as much of it as +// HostListeners needs to tell them apart. +type Listener struct { + ID string `json:"id"` + Name string `json:"name"` + Protocol string `json:"protocol"` + TLSImplicit bool `json:"tlsImplicit"` + Bind map[string]bool `json:"bind"` +} + +// Listeners lists the server's network listeners. +func (c *Client) Listeners(ctx context.Context) ([]Listener, error) { + rs, err := c.Do(ctx, + Call{"x:NetworkListener/query", map[string]any{}, "0"}, + Call{"x:NetworkListener/get", map[string]any{ + "#ids": map[string]any{"resultOf": "0", "name": "x:NetworkListener/query", "path": "/ids"}, + "properties": []string{"name", "protocol", "tlsImplicit", "bind"}, + }, "1"}, + ) + if err != nil { + return nil, err + } + var got struct { + List []Listener `json:"list"` + } + return got.List, json.Unmarshal(rs[1].Args, &got) +} + +// HostListeners fits the listeners a server creates on its first configured +// start to a server running on the host rather than in a container. +// +// In a container they are all on [::] inside the container, and only what +// the compose file publishes is reachable. On the host there is nothing in +// between, so the defaults would put plain HTTP on every interface at 8080, +// where the webmail listens, and take 443, which belongs to the proxy. So: +// +// - plain HTTP moves to httpBind, on loopback, where the proxy and the +// webmail reach it -- the same place a container install publishes it; +// - the server's own HTTPS listener goes: the proxy terminates TLS for the +// server's names, as it does in front of a container; +// - with local, every other listener moves to loopback too, since a +// loopback evaluation publishes nothing. +// +// It takes effect at the next restart. +func (c *Client) HostListeners(ctx context.Context, httpBind string, local bool) error { + ls, err := c.Listeners(ctx) + if err != nil { + return err + } + update, destroy := map[string]any{}, []string{} + for _, l := range ls { + switch { + case l.Protocol == "http" && l.TLSImplicit: + destroy = append(destroy, l.ID) + case l.Protocol == "http": + update[l.ID] = map[string]any{"bind": map[string]bool{httpBind: true}} + case local: + binds := map[string]bool{} + for addr := range l.Bind { + if i := strings.LastIndex(addr, ":"); i >= 0 { + binds["127.0.0.1"+addr[i:]] = true + } + } + update[l.ID] = map[string]any{"bind": binds} + } + } + if len(update) == 0 && len(destroy) == 0 { + return nil + } + args := map[string]any{} + if len(update) > 0 { + args["update"] = update + } + if len(destroy) > 0 { + args["destroy"] = destroy + } + rs, err := c.Do(ctx, Call{"x:NetworkListener/set", args, "0"}) + if err != nil { + return err + } + _, err = DecodeSet(rs[0]) + return err +} diff --git a/internal/plan/plan.go b/internal/plan/plan.go index b2f63ed..643c2c9 100644 --- a/internal/plan/plan.go +++ b/internal/plan/plan.go @@ -11,10 +11,12 @@ package plan import ( "fmt" + "runtime" "strings" "git.coffeylabs.org/inbuxa/inbuxa-installer/internal/deps" "git.coffeylabs.org/inbuxa/inbuxa-installer/internal/host" + "git.coffeylabs.org/inbuxa/inbuxa-installer/internal/release" ) // Component is one of the three programs. @@ -157,6 +159,10 @@ func Available(f host.Facts, c Component, s Shape) Availability { case Server: // The binary is downloaded, not built here, so the machine has // to be able to run it. + if _, ok := release.ServerSHA256[runtime.GOARCH]; !ok { + return Availability{Why: fmt.Sprintf("inbuxa-server %s has no release binary for %s; run the server as a container here", + release.ServerVersion, runtime.GOARCH)} + } if f.Glibc.Version == "" { return Availability{Why: "this machine's C library could not be read, and the server binary is linked against glibc " + fmt.Sprintf("%d.%d", ServerGlibcMajor, ServerGlibcMinor)} @@ -225,6 +231,11 @@ func Build(f host.Facts, o Options) (Plan, error) { if chosen == 0 { return p, fmt.Errorf("nothing chosen: pick at least one component") } + // The proxy the installer manages is Caddy in a container, whatever the + // server is: a server on the host still needs a runtime for it. + if !o.Local && o.Proxy == "caddy" { + wantContainers = true + } // What is missing is not the same as what is impossible. Docker absent on // a Debian machine is one package and a service; Node too old is a pinned @@ -286,7 +297,7 @@ func Build(f host.Facts, o Options) (Plan, error) { p.Steps = append(p.Steps, Step{ Title: "Start the mail server as a container", Detail: []string{ - "image: registry.coffeylabs.org/inbuxa/inbuxa-server (pinned to a release tag)", + "image: " + release.ServerImage, "volumes: inbuxa-data, inbuxa-etc", "ports: 25, 465, 993, 995, 4190, and 8081 on loopback for the proxy", }, @@ -295,9 +306,11 @@ func Build(f host.Facts, o Options) (Plan, error) { p.Steps = append(p.Steps, Step{ Title: "Install the mail server on this machine", Detail: []string{ - "binary: /usr/local/bin/inbuxa-server, from the release on git.coffeylabs.org", - "user: inbuxa (system, no login), data in " + o.Dir + "/server", - "unit: /etc/systemd/system/inbuxa-server.service", + fmt.Sprintf("binary: /usr/local/bin/inbuxa-server, release %s for %s, checked against its pinned checksum", + release.ServerVersion, runtime.GOARCH), + "user: inbuxa (system, no login); configuration and data in " + o.Dir + "/server", + "unit: /etc/systemd/system/inbuxa-server.service, its environment in " + o.Dir + "/server.env", + "during setup it is reachable from this machine only; then its plain HTTP moves to 127.0.0.1:8081 and 443 is left to the proxy", }, }) } diff --git a/internal/release/release.go b/internal/release/release.go new file mode 100644 index 0000000..0aeea0f --- /dev/null +++ b/internal/release/release.go @@ -0,0 +1,33 @@ +// SPDX-FileCopyrightText: 2026 Coffey Labs LLC +// SPDX-License-Identifier: AGPL-3.0-or-later + +// Package release is which version of the mail server this installer puts on +// a machine, as an image or as a binary, and what that binary must hash to. +// +// One version for both shapes, so a server moved from a container to the host +// is the same program, and the plan, the image tag and the download cannot +// name three different releases. +package release + +import "fmt" + +// ServerVersion is the inbuxa-server release this installer installs. +const ServerVersion = "2026.10.10.1" + +// ServerImage is that release as a container image. +const ServerImage = "registry.coffeylabs.org/inbuxa/inbuxa-server:" + ServerVersion + +// ServerSHA256 is the SHA-256 of each architecture's release tarball, copied +// from the release's SHA256SUMS when the version was moved and checked here +// rather than fetched beside the download: a checksum that arrives with the +// file only proves the file arrived. +var ServerSHA256 = map[string]string{ + "amd64": "118087b0f7c98d326e3622a1732d733d1869c8b9210cc8a4eeb5c7101009daa5", + "arm64": "128c1234032bf4806e44285271469db6eba246f8944d474c92f9d3fdbbcd03f4", +} + +// ServerTarball is where the release's tarball for an architecture is. +func ServerTarball(arch string) string { + return fmt.Sprintf("https://git.coffeylabs.org/inbuxa/inbuxa-server/releases/download/v%s/inbuxa-linux-%s.tar.gz", + ServerVersion, arch) +}