A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.
fix(rexctl): keep state files owned by the instance owner (#3718)
Hampus7d544ad57d42
diff --git a/fluxer_rexctl/cmd/app.go b/fluxer_rexctl/cmd/app.goindex a7e45dcba..3e20b3295 100644--- a/fluxer_rexctl/cmd/app.go+++ b/fluxer_rexctl/cmd/app.go@@ -403,6 +403,10 @@ func (a *App) holding(ctx context.Context, inst *instance.Instance, l *instance.if err := rootlessGuard(inst); err != nil {return err}+ if err := checkOwnership(a, inst); err != nil {+ return err+ }+ defer giveFilesBack(a, inst)a.held = instdefer func() { a.held = nil }()a.migrate(ctx, inst)diff --git a/fluxer_rexctl/cmd/install.go b/fluxer_rexctl/cmd/install.goindex 90f56caae..bd3331b6d 100644--- a/fluxer_rexctl/cmd/install.go+++ b/fluxer_rexctl/cmd/install.go@@ -1160,14 +1160,14 @@ func (in *installer) write(ctx context.Context, inst *instance.Instance, deleteOst.Images.Refs = map[string]string{}}if err := release.WriteRelease(inst, set); err != nil {- return ui.Wrap(err, fmt.Sprintf("Couldn't write Fluxer's files to %s.", inst.Dir), "Check the free disk space, then run rexctl install again.")+ return saveFailed(inst, err, fmt.Sprintf("Couldn't write Fluxer's files to %s.", inst.Dir), "rexctl install")}r, err := inst.Render(set, stack.OverlaysFor(st.Settings), st.Images.Refs)if err != nil {- return ui.Wrap(err, fmt.Sprintf("Couldn't write Fluxer's files to %s.", inst.Dir), "Check the free disk space, then run rexctl install again.")+ return saveFailed(inst, err, fmt.Sprintf("Couldn't write Fluxer's files to %s.", inst.Dir), "rexctl install")}if err := inst.WriteCustomTemplate(); err != nil {- return ui.Wrap(err, fmt.Sprintf("Couldn't write %s to %s.", instance.CustomCompose, inst.Dir), "Check the free disk space, then run rexctl install again.")+ return saveFailed(inst, err, fmt.Sprintf("Couldn't write %s to %s.", instance.CustomCompose, inst.Dir), "rexctl install")}st.Rendered = r.Hashesif err := inst.Save(); err != nil {diff --git a/fluxer_rexctl/cmd/nospace_unix.go b/fluxer_rexctl/cmd/nospace_unix.gonew file mode 100644index 000000000..bbf18201d--- /dev/null+++ b/fluxer_rexctl/cmd/nospace_unix.go@@ -0,0 +1,14 @@+// SPDX-License-Identifier: AGPL-3.0-or-later++//go:build !windows++package cmd++import (+ "errors"+ "syscall"+)++func noSpace(err error) bool {+ return errors.Is(err, syscall.ENOSPC) || errors.Is(err, syscall.EDQUOT)+}diff --git a/fluxer_rexctl/cmd/nospace_windows.go b/fluxer_rexctl/cmd/nospace_windows.gonew file mode 100644index 000000000..52469b8f0--- /dev/null+++ b/fluxer_rexctl/cmd/nospace_windows.go@@ -0,0 +1,16 @@+// SPDX-License-Identifier: AGPL-3.0-or-later++//go:build windows++package cmd++import (+ "errors"+ "syscall"++ "golang.org/x/sys/windows"+)++func noSpace(err error) bool {+ return errors.Is(err, syscall.ENOSPC) || errors.Is(err, windows.ERROR_DISK_FULL) || errors.Is(err, windows.ERROR_HANDLE_DISK_FULL)+}diff --git a/fluxer_rexctl/cmd/ownership.go b/fluxer_rexctl/cmd/ownership.gonew file mode 100644index 000000000..65b0d3b0f--- /dev/null+++ b/fluxer_rexctl/cmd/ownership.go@@ -0,0 +1,78 @@+// SPDX-License-Identifier: AGPL-3.0-or-later++package cmd++import (+ "errors"+ "fmt"+ "io/fs"+ "os/user"+ "strconv"++ "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/instance"+ "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/ui"+)++func checkOwnership(app *App, inst *instance.Instance) error {+ if euid() == 0 {+ if err := inst.GiveFilesToOwner(); err != nil {+ app.UI.Detail("rexctl couldn't give every file in %s back to the account that owns %s: %v", inst.StateDir(), inst.Dir, err)+ }+ return nil+ }+ f, ok := inst.ForeignFile()+ if !ok {+ return nil+ }+ return ui.Refused(+ fmt.Sprintf("rexctl can't change %s. It belongs to %s and rexctl runs as %s.", f.Path, accountName(f.Owner), accountName(euid())),+ chownFix(accountName(euid()), f.Dir)+", then try again.",+ )+}++func giveFilesBack(app *App, inst *instance.Instance) {+ if euid() != 0 {+ return+ }+ if err := inst.GiveFilesToOwner(); err != nil {+ app.UI.Detail("rexctl couldn't give every file in %s back to the account that owns %s: %v", inst.StateDir(), inst.Dir, err)+ }+}++func saveFailed(inst *instance.Instance, err error, msg, command string) error {+ again := "run " + command + " again."+ switch {+ case noSpace(err):+ return ui.Wrap(err, msg, "The disk is full. Free up space, then "+again)+ case errors.Is(err, fs.ErrPermission):+ who := accountName(euid())+ if euid() == 0 {+ if uid, ok := inst.OwnerUID(); ok {+ who = accountName(uid)+ }+ }+ path, dir := inst.StateDir(), inst.StateDir()+ if f, ok := inst.ForeignFile(); ok {+ path, dir = f.Path, f.Dir+ } else {+ var pe *fs.PathError+ if errors.As(err, &pe) {+ path = pe.Path+ }+ }+ return ui.Wrap(err, msg, fmt.Sprintf("rexctl isn't allowed to write %s. %s, then %s", path, chownFix(who, dir), again))+ }+ return ui.Wrap(err, msg, fmt.Sprintf("Run %s --verbose to see why.", command))+}++func chownFix(account, dir string) string {+ return fmt.Sprintf("Run sudo chown -R %s: %s", account, dir)+}++func accountName(uid int) string {+ id := strconv.Itoa(uid)+ if u, err := user.LookupId(id); err == nil && u.Username != "" {+ return u.Username+ }+ return id+}diff --git a/fluxer_rexctl/cmd/rollback.go b/fluxer_rexctl/cmd/rollback.goindex 79e285b48..83615d00a 100644--- a/fluxer_rexctl/cmd/rollback.go+++ b/fluxer_rexctl/cmd/rollback.go@@ -159,7 +159,7 @@ func runRollback(ctx context.Context, app *App, f *rollbackFlags) error {}prevBackup := inst.State.PreviousBackupif err := release.Snapshot(inst, cur, refList(inst.State.Images.Refs)); err != nil {- return ui.Wrap(err, fmt.Sprintf("Couldn't save the files of Fluxer %s.", curShort), "Check that the disk isn't full, then run rexctl rollback again.")+ return saveFailed(inst, err, fmt.Sprintf("Couldn't save the files of Fluxer %s.", curShort), "rexctl rollback")}var put []stringerr = app.UI.Step(ctx, "Going back to "+prevShort, func(ctx context.Context, s *ui.Step) error {diff --git a/fluxer_rexctl/cmd/upgrade.go b/fluxer_rexctl/cmd/upgrade.goindex 13692f86d..3dc01e33d 100644--- a/fluxer_rexctl/cmd/upgrade.go+++ b/fluxer_rexctl/cmd/upgrade.go@@ -695,11 +695,11 @@ func upgradeStack(ctx context.Context, app *App, inst *instance.Instance, e *eng}if err := release.Snapshot(inst, from, refList(inst.State.Images.Refs)); err != nil {clearUpgrade(inst)- return ui.Wrap(err, fmt.Sprintf("Couldn't save the files of %s.", fluxerName(fromShort)), "Check that the disk isn't full, then run rexctl upgrade again.")+ return saveFailed(inst, err, fmt.Sprintf("Couldn't save the files of %s.", fluxerName(fromShort)), "rexctl upgrade")}if err := release.WriteRelease(inst, set); err != nil {clearUpgrade(inst)- return ui.Wrap(err, fmt.Sprintf("Couldn't save the files of Fluxer %s.", toShort), "Check that the disk isn't full, then run rexctl upgrade again.")+ return saveFailed(inst, err, fmt.Sprintf("Couldn't save the files of Fluxer %s.", toShort), "rexctl upgrade")}next, pins, err := nextState(inst, set, images, to)if err != nil {@@ -716,7 +716,7 @@ func upgradeStack(ctx context.Context, app *App, inst *instance.Instance, e *eng}if err := release.Snapshot(&instance.Instance{Dir: inst.Dir, State: next}, to, refList(next.Images.Refs)); err != nil {clearUpgrade(inst)- return ui.Wrap(err, fmt.Sprintf("Couldn't save the settings for Fluxer %s.", toShort), "Check that the disk isn't full, then run rexctl upgrade again.")+ return saveFailed(inst, err, fmt.Sprintf("Couldn't save the settings for Fluxer %s.", toShort), "rexctl upgrade")}f = backupFlagsFor(app, inst, from, set, next, f, toShort)old := e.Named(inst.Project(), inst.Dir)diff --git a/fluxer_rexctl/internal/instance/owner_unix.go b/fluxer_rexctl/internal/instance/owner_unix.goindex ffddd4473..01a0c8192 100644--- a/fluxer_rexctl/internal/instance/owner_unix.go+++ b/fluxer_rexctl/internal/instance/owner_unix.go@@ -39,3 +39,15 @@ func (i *Instance) giveToOwner(path string) error {}return chown(path, int(st.Uid), int(st.Gid))}++func dirIDs(dir string) (int, int, bool) {+ info, err := os.Stat(dir)+ if err != nil {+ return 0, 0, false+ }+ st, ok := info.Sys().(*syscall.Stat_t)+ if !ok {+ return 0, 0, false+ }+ return int(st.Uid), int(st.Gid), true+}diff --git a/fluxer_rexctl/internal/instance/owner_windows.go b/fluxer_rexctl/internal/instance/owner_windows.goindex 1e8bf5d2f..9bdcb01c4 100644--- a/fluxer_rexctl/internal/instance/owner_windows.go+++ b/fluxer_rexctl/internal/instance/owner_windows.go@@ -11,3 +11,5 @@ func (i *Instance) OwnerUID() (int, bool) { return 0, false }func ownerOf(os.FileInfo) (int, bool) { return 0, false }func (i *Instance) giveToOwner(string) error { return nil }++func dirIDs(string) (int, int, bool) { return 0, 0, false }diff --git a/fluxer_rexctl/internal/instance/ownership.go b/fluxer_rexctl/internal/instance/ownership.gonew file mode 100644index 000000000..00c0f0353--- /dev/null+++ b/fluxer_rexctl/internal/instance/ownership.go@@ -0,0 +1,84 @@+// SPDX-License-Identifier: AGPL-3.0-or-later++package instance++import (+ "errors"+ "io/fs"+ "os"+ "path/filepath"+)++type Foreign struct {+ Path string+ Owner int+ Dir string+}++func (i *Instance) ownDirs() []string {+ return []string{i.Path(StateDirName), i.Path(BackupsDir)}+}++func (i *Instance) GiveFilesToOwner() error {+ if euid() != 0 {+ return nil+ }+ uid, gid, ok := dirIDs(i.Dir)+ if !ok || uid == 0 {+ return nil+ }+ var errs []error+ for _, dir := range i.ownDirs() {+ err := filepath.WalkDir(dir, func(path string, d fs.DirEntry, err error) error {+ if err != nil {+ if errors.Is(err, fs.ErrNotExist) {+ return nil+ }+ return err+ }+ info, err := d.Info()+ if err != nil {+ return nil+ }+ if owner, ok := ownerOf(info); ok && owner == 0 {+ return chownIDs(path, uid, gid)+ }+ return nil+ })+ if err != nil && !errors.Is(err, fs.ErrNotExist) {+ errs = append(errs, err)+ }+ }+ return errors.Join(errs...)+}++func (i *Instance) ForeignFile() (Foreign, bool) {+ me := euid()+ if me == 0 {+ return Foreign{}, false+ }+ dir := i.StateDir()+ var found Foreign+ _ = filepath.WalkDir(dir, func(path string, d fs.DirEntry, err error) error {+ if err != nil {+ return nil+ }+ info, err := d.Info()+ if err != nil {+ return nil+ }+ if owner, ok := ownerOf(info); ok && owner != me {+ found = Foreign{Path: path, Owner: owner, Dir: dir}+ return fs.SkipAll+ }+ return nil+ })+ return found, found.Path != ""+}++func chownIDs(path string, uid, gid int) error {+ if err := chown(path, uid, gid); err != nil && !errors.Is(err, os.ErrNotExist) {+ return err+ }+ return nil+}