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

View File

@ -41,15 +41,18 @@ func pwgen() string {
}
func cmdQuickstart(c *cmd) {
c.params = "user@domain"
c.params = "user@domain [user | uid]"
c.help = `Quickstart generates configuration files and prints instructions to quickly set up a mox instance.
Quickstart prints initial admin and account passwords, configuration files, DNS
records you should create, instructions for setting correct user/group and
permissions, and if you run it on Linux it prints a systemd service file.
Quickstart writes configuration files, prints initial admin and account
passwords, DNS records you should create. If you run it on Linux it writes a
systemd service file and prints commands to enable and start mox as service.
The user or uid is optional, defaults to "mox", and is the user or uid/gid mox
will run as after initialization.
`
args := c.Parse()
if len(args) != 1 {
if len(args) != 1 && len(args) != 2 {
c.Usage()
}
@ -342,19 +345,27 @@ This likely means one of two things:
}
cancel()
user := "mox"
if len(args) == 2 {
user = args[1]
}
dc := config.Dynamic{}
sc := config.Static{DataDir: "../data"}
sc := config.Static{
DataDir: "../data",
User: user,
LogLevel: "info",
Hostname: hostname.Name(),
ACME: map[string]config.ACME{
"letsencrypt": {
DirectoryURL: "https://acme-v02.api.letsencrypt.org/directory",
ContactEmail: args[0], // todo: let user specify an alternative fallback address?
},
},
AdminPasswordFile: "adminpasswd",
}
dataDir := "data" // ../data is relative to config/
os.MkdirAll(dataDir, 0770)
sc.LogLevel = "info"
sc.Hostname = hostname.Name()
sc.ACME = map[string]config.ACME{
"letsencrypt": {
DirectoryURL: "https://acme-v02.api.letsencrypt.org/directory",
ContactEmail: args[0], // todo: let user specify an alternative fallback address?
},
}
sc.AdminPasswordFile = "adminpasswd"
adminpw := pwgen()
adminpwhash, err := bcrypt.GenerateFromPassword([]byte(adminpw), bcrypt.DefaultCost)
if err != nil {
@ -399,7 +410,8 @@ This likely means one of two things:
mox.Conf.DynamicLastCheck = time.Now() // Prevent error logging by Make calls below.
accountConf := mox.MakeAccountConfig(addr)
confDomain, keyPaths, err := mox.MakeDomainConfig(context.Background(), domain, hostname, username)
const withMTASTS = true
confDomain, keyPaths, err := mox.MakeDomainConfig(context.Background(), domain, hostname, username, withMTASTS)
if err != nil {
fatalf("making domain config: %s", err)
}
@ -529,47 +541,20 @@ or if you are sending email for your domain from other machines/services, you
should understand the consequences of the DNS records above before
continuing!
You can now start mox with "mox serve", but see below for recommended ownership
and permissions.
You can now start mox with "./mox serve", as root. File ownership and
permissions are automatically set correctly by mox when starting up. On linux,
you may want to enable mox as a systemd service.
`)
if os.Getenv("MOX_DOCKER") == "" {
fmt.Printf(`Assuming the mox binary is in the current directory, and you will run mox under
user name "mox", and the admin user is the current user, the following commands
set the correct permissions:
sudo useradd -d $PWD mox
sudo chown $(id -nu):mox . mox
sudo chown -R mox:$(id -ng) config data
sudo chmod 751 .
sudo chmod 750 mox
sudo chmod -R u=rwX,g=rwX,o= config data
sudo chmod g+s $(find . -type d)
`)
} else {
fmt.Printf(`Assuming you will run mox under user name "mox", and the admin user is the
current user, the following commands set the correct permissions:
sudo useradd -d $PWD mox
sudo chown $(id -nu):mox .
sudo chown -R mox:$(id -ng) config data
sudo chmod 751 .
sudo chmod -R u=rwX,g=rwX,o= config data
sudo chmod g+s $(find . -type d)
`)
}
// For now, we only give service config instructions for linux when not running in docker.
if runtime.GOOS == "linux" && os.Getenv("MOX_DOCKER") == "" {
pwd, err := os.Getwd()
if err != nil {
log.Printf("current working directory: %v", err)
pwd = "/home/service/mox"
pwd = "/home/mox"
}
service := strings.ReplaceAll(moxService, "/home/service/mox", pwd)
service := strings.ReplaceAll(moxService, "/home/mox", pwd)
xwritefile("mox.service", []byte(service), 0644)
cleanupPaths = append(cleanupPaths, "mox.service")
fmt.Printf(`See mox.service for a systemd service file. To enable and start: