change mox to start as root, bind to network sockets, then drop to regular unprivileged mox user

makes it easier to run on bsd's, where you cannot (easily?) let non-root users
bind to ports <1024. starting as root also paves the way for future improvements
with privilege separation.

unfortunately, this requires changes to how you start mox. though mox will help
by automatically fix up dir/file permissions/ownership.

if you start mox from the systemd unit file, you should update it so it starts
as root and adds a few additional capabilities:

        # first update the mox binary, then, as root:
        ./mox config printservice >mox.service
        systemctl daemon-reload
        systemctl restart mox
        journalctl -f -u mox &
        # you should see mox start up, with messages about fixing permissions on dirs/files.

if you used the recommended config/ and data/ directory, in a directory just for
mox, and with the mox user called "mox", this should be enough.

if you don't want mox to modify dir/file permissions, set "NoFixPermissions:
true" in mox.conf.

if you named the mox user something else than mox, e.g. "_mox", add "User: _mox"
to mox.conf.

if you created a shared service user as originally suggested, you may want to
get rid of that as it is no longer useful and may get in the way. e.g. if you
had /home/service/mox with a "service" user, that service user can no longer
access any files: only mox and root can.

this also adds scripts for building mox docker images for alpine-supported
platforms.

the "restart" subcommand has been removed. it wasn't all that useful and got in
the way.

and another change: when adding a domain while mtasts isn't enabled, don't add
the per-domain mtasts config, as it would cause failure to add the domain.

based on report from setting up mox on openbsd from mteege.
and based on issue #3. thanks for the feedback!
This commit is contained in:
Mechiel Lukkien
2023-02-27 12:19:55 +01:00
parent eda907fc86
commit 92e018e463
37 changed files with 841 additions and 435 deletions

54
ctl.go
View File

@ -9,12 +9,10 @@ import (
"net"
"os"
"path/filepath"
"runtime"
"runtime/debug"
"sort"
"strconv"
"strings"
"syscall"
"time"
"github.com/mjl-/bstore"
@ -301,58 +299,6 @@ func servectlcmd(ctx context.Context, log *mlog.Log, ctl *ctl, xcmd *string, shu
shutdown()
os.Exit(0)
case "restart":
// First test the config.
_, errs := mox.ParseConfig(ctx, mox.ConfigStaticPath, true)
if len(errs) > 1 {
log.Error("multiple configuration errors before restart")
for _, err := range errs {
log.Errorx("config error", err)
}
ctl.xerror("restart aborted")
} else if len(errs) == 1 {
log.Errorx("configuration error, restart aborted", errs[0])
ctl.xerror(errs[0].Error())
}
binary, err := os.Executable()
ctl.xcheck(err, "finding executable")
cfr, ok := ctl.conn.(interface{ File() (*os.File, error) })
if !ok {
ctl.xerror("cannot dup ctl socket")
}
cf, err := cfr.File()
ctl.xcheck(err, "dup ctl socket")
defer cf.Close()
_, _, err = syscall.Syscall(syscall.SYS_FCNTL, cf.Fd(), syscall.F_SETFD, 0)
if err != syscall.Errno(0) {
ctl.xcheck(err, "clear close-on-exec on ctl socket")
}
ctl.xwriteok()
shutdown()
// todo future: we could gather all listen fd's, keep them open, passing them to the new process and indicate (in env var or cli flag) for which addresses they are, then exec and have the new process pick them up. not worth the trouble at the moment, our shutdown is typically quick enough.
// todo future: does this actually cleanup all M's on all platforms?
env := os.Environ()
var found bool
envv := fmt.Sprintf("MOX_RESTART_CTL_SOCKET=%d", cf.Fd())
for i, s := range env {
if strings.HasPrefix(s, "MOX_RESTART_CTL_SOCKET=") {
found = true
env[i] = envv
break
}
}
if !found {
env = append(env, envv)
}
// On success, we never get here and "serve" will write the OK on the MOX_RESTART_CTL_SOCKET and close it.
err = syscall.Exec(binary, os.Args, env)
runtime.KeepAlive(cf)
ctl.xcheck(err, "exec")
case "deliver":
/* The protocol, double quoted are literals.