Skip to content

Commit

Permalink
fix most file ownership issues in 'init' commands when run as root wi…
Browse files Browse the repository at this point in the history
…th a username given

* there will still be paths not fixed
* further work required

(cherry picked from commit 272ed16)
  • Loading branch information
pgalbavy-itrs committed Nov 2, 2022
1 parent 9bcb8a9 commit 1d64064
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 15 deletions.
13 changes: 10 additions & 3 deletions tools/geneos/internal/geneos/archives.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
)

// OpenArchive locates and returns an io.ReadCloser for an archive for
// the component given. TODO: Archives must currently be local.
// the component given
func OpenArchive(ct *Component, options ...GeneosOptions) (body io.ReadCloser, filename string, err error) {
var resp *http.Response

Expand Down Expand Up @@ -128,6 +128,12 @@ func OpenArchive(ct *Component, options ...GeneosOptions) (body io.ReadCloser, f
return
}
body = w

// chown package
if utils.IsSuperuser() {
uid, gid, _, _ := utils.GetIDs(opts.localusername)
w.Chown(uid, gid)
}
return
}

Expand Down Expand Up @@ -267,20 +273,21 @@ func Unarchive(h *host.Host, ct *Component, filename string, gz io.Reader, optio
if err = h.Symlink(hdr.Linkname, fullpath); err != nil {
log.Fatal().Err(err).Msg("")
}

}

default:
log.Warn().Msgf("unsupported file type %c\n", hdr.Typeflag)
}
}

// if root, chown of created tree to default user
// if root, (l)chown of created tree to default user
if h == host.LOCAL && utils.IsSuperuser() {
uid, gid, _, err := utils.GetIDs(h.GetString("username"))
if err == nil {
filepath.WalkDir(h.Path(basedir), func(path string, dir fs.DirEntry, err error) error {
if err == nil {
err = host.LOCAL.Chown(path, uid, gid)
err = host.LOCAL.Lchown(path, uid, gid)
}
return err
})
Expand Down
7 changes: 7 additions & 0 deletions tools/geneos/internal/geneos/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import (
"path/filepath"

"github.com/itrs-group/cordial/pkg/config"

"github.com/itrs-group/cordial/tools/geneos/internal/host"
"github.com/itrs-group/cordial/tools/geneos/internal/utils"

"github.com/rs/zerolog/log"
)

Expand Down Expand Up @@ -175,6 +178,10 @@ func MakeComponentDirs(h *host.Host, ct *Component) (err error) {
if err = h.MkdirAll(dir, 0775); err != nil {
return
}
if utils.IsSuperuser() {
uid, gid, _, _ := utils.GetIDs("")
h.Chown(dir, uid, gid)
}
}
return
}
Expand Down
15 changes: 8 additions & 7 deletions tools/geneos/internal/geneos/geneos.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,16 +83,17 @@ func Init(r *host.Host, options ...GeneosOptions) (err error) {
config.GetConfig().Set("geneos", opts.homedir)
config.GetConfig().Set("defaultuser", opts.localusername)

userConfFile := UserConfigFilePaths()[0]
if utils.IsSuperuser() {
if err = host.WriteConfigFile(GlobalConfigPath, "root", 0664, config.GetConfig()); err != nil {
log.Fatal().Err(err).Msg("cannot write global config")
userConfDir, err := config.UserConfigDir(opts.localusername)
if err != nil {
log.Fatal().Err(err).Msg("")
}
} else {
userConfFile := UserConfigFilePaths()[0]
userConfFile = filepath.Join(userConfDir, ConfigSubdirName, UserConfigFile)
}

if err = host.WriteConfigFile(userConfFile, opts.localusername, 0664, config.GetConfig()); err != nil {
return err
}
if err = host.WriteConfigFile(userConfFile, opts.localusername, 0664, config.GetConfig()); err != nil {
return err
}
}

Expand Down
11 changes: 10 additions & 1 deletion tools/geneos/internal/host/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,16 @@ func WriteHostConfigFile() error {
return true
})

return n.WriteConfigAs(UserHostsFilePath())
// return n.WriteConfigAs(UserHostsFilePath())

if err := n.WriteConfigAs(UserHostsFilePath()); err != nil {
return err
}
if utils.IsSuperuser() {
uid, gid, _, _ := utils.GetIDs("")
LOCAL.Chown(UserHostsFilePath(), uid, gid)
}
return nil
}

func UserHostsFilePath() string {
Expand Down
27 changes: 25 additions & 2 deletions tools/geneos/internal/instance/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ func CreateConfigFromTemplate(c geneos.Instance, path string, name string, defau
return err
}
defer out.Close()
if utils.IsSuperuser() {
uid, gid, _, _ := utils.GetIDs("")
c.Host().Chown(path, uid, gid)
}

// m := make(map[string]string)
m := c.Config().AllSettings()
Expand Down Expand Up @@ -304,6 +308,11 @@ func writeConfig(c geneos.Instance) (err error) {
if err = c.Host().MkdirAll(utils.Dir(file), 0775); err != nil {
log.Error().Err(err).Msg("")
}
if utils.IsSuperuser() {
uid, gid, _, _ := utils.GetIDs("")
c.Host().Chown(utils.Dir(file), uid, gid)
}

nv := config.New()
for _, k := range c.Config().AllKeys() {
if _, ok := c.Type().Aliases[k]; !ok {
Expand All @@ -318,7 +327,14 @@ func writeConfig(c geneos.Instance) (err error) {
nv.SetFs(sftpfs.New(client))
}
log.Debug().Msgf("writing config for %s as %q", c, file)
return nv.WriteConfigAs(file)
if err = nv.WriteConfigAs(file); err != nil {
return err
}
if utils.IsSuperuser() {
uid, gid, _, _ := utils.GetIDs("")
c.Host().Chown(file, uid, gid)
}
return
}

// WriteConfigValues writes values to the configuration file for
Expand All @@ -343,7 +359,14 @@ func WriteConfigValues(c geneos.Instance, values map[string]interface{}) (err er
}
nv.SetFs(sftpfs.New(client))
}
return nv.WriteConfigAs(file)
if err = nv.WriteConfigAs(file); err != nil {
return err
}
if utils.IsSuperuser() {
uid, gid, _, _ := utils.GetIDs("")
c.Host().Chown(file, uid, gid)
}
return
}

// ReadConfig reads the instance configuration from the standard file.
Expand Down
4 changes: 4 additions & 0 deletions tools/geneos/internal/instance/gateway/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,10 @@ func createAESKeyFile(c geneos.Instance) (err error) {
if err = a.WriteAESValues(w); err != nil {
return
}
if utils.IsSuperuser() {
uid, gid, _, _ := utils.GetIDs("")
c.Host().Chown(instance.ComponentFilepath(c, "aes"), uid, gid)
}

c.Config().Set("keyfile", instance.ComponentFilename(c, "aes"))
return
Expand Down
3 changes: 2 additions & 1 deletion tools/geneos/internal/instance/instance_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,8 @@ func Files(c geneos.Instance) (links map[int]string) {
fd := ent.Name()
dest, err := c.Host().Readlink(utils.JoinSlash(path, fd))
if err != nil {
log.Fatal().Err(err).Msg("")
log.Debug().Err(err).Msg("")
continue
}
n, _ := strconv.Atoi(fd)
links[n] = dest
Expand Down
9 changes: 8 additions & 1 deletion tools/geneos/internal/instance/webserver/webserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ import (
"github.com/rs/zerolog/log"

"github.com/itrs-group/cordial/pkg/config"

"github.com/itrs-group/cordial/tools/geneos/internal/geneos"
"github.com/itrs-group/cordial/tools/geneos/internal/host"
"github.com/itrs-group/cordial/tools/geneos/internal/instance"
"github.com/itrs-group/cordial/tools/geneos/internal/utils"
)

var Webserver = geneos.Component{
Expand Down Expand Up @@ -188,9 +190,14 @@ func (w *Webservers) Add(username string, tmpl string, port uint16) (err error)
return
}

if err = w.Host().MkdirAll(filepath.Join(w.Home(), "webapps"), 0775); err != nil {
webappsdir := filepath.Join(w.Home(), "webapps")
if err = w.Host().MkdirAll(webappsdir, 0775); err != nil {
return
}
if utils.IsSuperuser() {
uid, gid, _, _ := utils.GetIDs("")
w.Host().Chown(webappsdir, uid, gid)
}

for _, source := range webserverFiles {
if _, err = instance.ImportFile(w.Host(), w.Home(), w.Config().GetString("user"), source); err != nil {
Expand Down

0 comments on commit 1d64064

Please sign in to comment.