mirror of
https://github.com/mjl-/mox.git
synced 2025-07-12 12:24:38 +03:00
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:
50
http/web.go
50
http/web.go
@ -9,6 +9,7 @@ import (
|
||||
golog "log"
|
||||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@ -36,9 +37,9 @@ func safeHeaders(fn http.HandlerFunc) http.HandlerFunc {
|
||||
}
|
||||
}
|
||||
|
||||
// ListenAndServe starts listeners for HTTP, including those required for ACME to
|
||||
// generate TLS certificates.
|
||||
func ListenAndServe() {
|
||||
// Listen binds to sockets for HTTP listeners, including those required for ACME to
|
||||
// generate TLS certificates. It stores the listeners so Serve can start serving them.
|
||||
func Listen() {
|
||||
type serve struct {
|
||||
kinds []string
|
||||
tlsConfig *tls.Config
|
||||
@ -179,7 +180,7 @@ func ListenAndServe() {
|
||||
|
||||
for port, srv := range portServe {
|
||||
for _, ip := range l.IPs {
|
||||
listenAndServe(ip, port, srv.tlsConfig, name, srv.kinds, srv.mux)
|
||||
listen1(ip, port, srv.tlsConfig, name, srv.kinds, srv.mux)
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -198,7 +199,11 @@ func adminIndex(w http.ResponseWriter, r *http.Request) {
|
||||
http.Redirect(w, r, "/admin/", http.StatusSeeOther)
|
||||
}
|
||||
|
||||
func listenAndServe(ip string, port int, tlsConfig *tls.Config, name string, kinds []string, mux *http.ServeMux) {
|
||||
// functions to be launched in goroutine that will serve on a listener.
|
||||
var servers []func()
|
||||
|
||||
// listen prepares a listener, and adds it to "servers", to be launched (if not running as root) through Serve.
|
||||
func listen1(ip string, port int, tlsConfig *tls.Config, name string, kinds []string, mux *http.ServeMux) {
|
||||
addr := net.JoinHostPort(ip, fmt.Sprintf("%d", port))
|
||||
|
||||
var protocol string
|
||||
@ -206,18 +211,23 @@ func listenAndServe(ip string, port int, tlsConfig *tls.Config, name string, kin
|
||||
var err error
|
||||
if tlsConfig == nil {
|
||||
protocol = "http"
|
||||
xlog.Print("http listener", mlog.Field("name", name), mlog.Field("kinds", strings.Join(kinds, ",")), mlog.Field("address", addr))
|
||||
ln, err = net.Listen(mox.Network(ip), addr)
|
||||
if os.Getuid() == 0 {
|
||||
xlog.Print("http listener", mlog.Field("name", name), mlog.Field("kinds", strings.Join(kinds, ",")), mlog.Field("address", addr))
|
||||
}
|
||||
ln, err = mox.Listen(mox.Network(ip), addr)
|
||||
if err != nil {
|
||||
xlog.Fatalx("http: listen"+mox.LinuxSetcapHint(err), err, mlog.Field("addr", addr))
|
||||
xlog.Fatalx("http: listen", err, mlog.Field("addr", addr))
|
||||
}
|
||||
} else {
|
||||
protocol = "https"
|
||||
xlog.Print("https listener", mlog.Field("name", name), mlog.Field("kinds", strings.Join(kinds, ",")), mlog.Field("address", addr))
|
||||
ln, err = tls.Listen(mox.Network(ip), addr, tlsConfig)
|
||||
if err != nil {
|
||||
xlog.Fatalx("https: listen"+mox.LinuxSetcapHint(err), err, mlog.Field("addr", addr))
|
||||
if os.Getuid() == 0 {
|
||||
xlog.Print("https listener", mlog.Field("name", name), mlog.Field("kinds", strings.Join(kinds, ",")), mlog.Field("address", addr))
|
||||
}
|
||||
ln, err = mox.Listen(mox.Network(ip), addr)
|
||||
if err != nil {
|
||||
xlog.Fatalx("https: listen", err, mlog.Field("addr", addr))
|
||||
}
|
||||
ln = tls.NewListener(ln, tlsConfig)
|
||||
}
|
||||
|
||||
server := &http.Server{
|
||||
@ -225,8 +235,20 @@ func listenAndServe(ip string, port int, tlsConfig *tls.Config, name string, kin
|
||||
TLSConfig: tlsConfig,
|
||||
ErrorLog: golog.New(mlog.ErrWriter(xlog.Fields(mlog.Field("pkg", "net/http")), mlog.LevelInfo, protocol+" error"), "", 0),
|
||||
}
|
||||
go func() {
|
||||
serve := func() {
|
||||
err := server.Serve(ln)
|
||||
xlog.Fatalx(protocol+": serve", err)
|
||||
}()
|
||||
}
|
||||
servers = append(servers, serve)
|
||||
}
|
||||
|
||||
// Serve starts serving on the initialized listeners.
|
||||
func Serve() {
|
||||
go manageAuthCache()
|
||||
go importManage()
|
||||
|
||||
for _, serve := range servers {
|
||||
go serve()
|
||||
}
|
||||
servers = nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user