Files
inbuxa-installer/internal/apply/hostserver.go
T
jcoffey-dev 82771a7ed7 Host server: root steps no longer follow links in the server's own directory
The server's home (<dir>/server) belongs to its user, and the server
runs as that user with write access there. Install, cluster join and
move then worked on paths inside it as root in ways that follow
symbolic links. Code running as the server's user (a compromised
server, for example) could put a link at data/, config.json,
config.json.prev or a secret file, and the next install, converge,
move or cluster join would chown the link's target to that user, write
over it, or copy a root-only file into an inbuxa-owned rollback copy:
the server's user became root.

Now install refuses a data/ that is not a plain directory and changes
ownership with chown -h and no trailing slash. Cluster join refuses a
link at config.json, its rollback copy or a secret in the server's
directory, reads config.json without following a link, and writes
each file beside its place, gives it to the server's user through the
open file and renames it into place. A move reads config.json without
following a link and replaces it by a rename, and refuses a data/ that
is a link before emptying it.

Test: TestServerDirsRefusesALinkedDataDir, TestJoinRefusesLinksInTheServersDirectory,
TestJoinDoesNotWriteThroughALinkPlantedAfterThePlan and
TestRewriteDataPathRefusesALink failed before (the link was chowned or
written through) and now pass.
2026-11-17 14:59:54 -07:00

407 lines
15 KiB
Go

// 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"
"errors"
"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
modeFenced // as it is, loopback only: a server moved here, until its listeners are fitted
)
// hostServer is the server as a host install, rooted in the deployment
// directory: <dir>/server holds config.json and data/, <dir>/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, and
// says whether it replaced the binary, which a running server only picks up
// on a restart.
func (h hostServer) Install(ctx context.Context) (replaced bool, err error) {
if path := os.Getenv("INBUXA_SERVER_BINARY"); path != "" {
// A server build to test, in place of the release. Said loudly,
// because nothing vouches for it the way the pinned checksum
// vouches for a release.
h.log.Step("installing %s instead of the release, as INBUXA_SERVER_BINARY says (no checksum)", path)
if out, err := exec.CommandContext(ctx, path, "--version").CombinedOutput(); err != nil {
return false, fmt.Errorf("%s does not run here: %s", path, strings.TrimSpace(string(out)))
}
if err := runQuiet(ctx, "install", "-m", "0755", path, HostServerBinary); err != nil {
return false, err
}
replaced = true
} else if v := binaryVersion(ctx, HostServerBinary); isVersion(v, release.ServerVersion) {
h.log.Info("%s is already %s", HostServerBinary, release.ServerVersion)
} else {
if err := h.fetchBinary(ctx); err != nil {
return false, err
}
replaced = true
}
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 false, 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 false, err
}
if err := os.Chmod(h.deploy, 0o711); err != nil {
return false, err
}
if err := h.serverDirs(ctx); err != nil {
return false, err
}
return replaced, nil
}
// chownServer runs chown; a variable so a test can watch it without root.
var chownServer = func(ctx context.Context, args ...string) error {
return runQuiet(ctx, "chown", args...)
}
// serverDirs makes the server's home and its data directory and gives them
// to the server's user. The home is in the deployment directory, which is
// root's, but data/ is in the home, which that user can write: something
// running as it could have put a symbolic link there, and following the
// link would give it whatever directory the link names. So a data/ that is
// not a plain directory is refused, and chown is given -h and no trailing
// slash, so a link swapped in after the check is what changes owner, not
// what it points at.
func (h hostServer) serverDirs(ctx context.Context) error {
for _, d := range []string{h.home(), filepath.Join(h.home(), "data")} {
if err := plainDir(d, 0o750); err != nil {
return err
}
if err := chownServer(ctx, "-h", HostServerUser+":"+HostServerUser, d); err != nil {
return err
}
}
return nil
}
// plainDir makes dir when it is not there, and refuses one that is a
// symbolic link or not a directory at all.
func plainDir(dir string, mode os.FileMode) error {
fi, err := os.Lstat(dir)
if errors.Is(err, os.ErrNotExist) {
if err := os.Mkdir(dir, mode); err != nil && !errors.Is(err, os.ErrExist) {
return err
}
fi, err = os.Lstat(dir)
}
if err != nil {
return err
}
return isPlainDir(dir, fi)
}
func isPlainDir(dir string, fi os.FileInfo) error {
switch {
case fi.Mode()&os.ModeSymlink != 0:
return fmt.Errorf("%s is a symbolic link, where the installer keeps a directory of the server's; "+
"nothing was changed through it. Something running as %s may have put it there: look at where it points, "+
"then move it aside and run this again", dir, HostServerUser)
case !fi.IsDir():
return fmt.Errorf("%s is not a directory", dir)
}
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 !isVersion(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 and says whether it changed. configured adds the
// condition that a finished install keeps: no configuration file, no start.
func (h hostServer) WriteUnit(ctx context.Context, configured bool) (bool, error) {
changed, err := writeIfChanged(hostUnitPath, h.unit(configured), 0o644)
if err != nil {
return false, err
}
if changed {
if err := runQuiet(ctx, "systemctl", "daemon-reload"); err != nil {
return false, err
}
}
return changed, runQuiet(ctx, "systemctl", "enable", "--quiet", HostServerUnit)
}
// unit is the unit's text.
func (h hostServer) unit(configured bool) string {
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"
}
return 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
# The log files the console's Logs page reads: systemd makes
# /var/log/inbuxa at each start, owned by the user above.
LogsDirectory=inbuxa
LogsDirectoryMode=0750
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())
}
// 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"
switch mode {
case modeBootstrap:
env = "INBUXA_RECOVERY_ADMIN=admin:" + bootPass + "\n"
case modeFenced:
env = ""
}
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 or a move 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))
}
// isVersion says whether what a binary said --version is names exactly the
// release want. The server prints its version as the first word; a match
// anywhere in the output would take 27.1-rc.1, 27.1.3 or 27.10.2 for 27.1.
func isVersion(out, want string) bool {
f := strings.Fields(out)
return len(f) > 0 && f[0] == want
}
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
}