diff --git a/agent/agent_darwin.go b/agent/agent_darwin.go deleted file mode 100644 index c7741c4..0000000 --- a/agent/agent_darwin.go +++ /dev/null @@ -1,16 +0,0 @@ -/* -Copyright 2022 AmidaWare LLC. - -Licensed under the Tactical RMM License Version 1.0 (the “License”). -You may only use the Licensed Software in accordance with the License. -A copy of the License is available at: - -https://license.tacticalrmm.com - -*/ - -package agent - -func tmpNoExec() bool { - return false -} diff --git a/agent/agent_linux.go b/agent/agent_linux.go deleted file mode 100644 index c7d16e1..0000000 --- a/agent/agent_linux.go +++ /dev/null @@ -1,30 +0,0 @@ -/* -Copyright 2022 AmidaWare LLC. - -Licensed under the Tactical RMM License Version 1.0 (the “License”). -You may only use the Licensed Software in accordance with the License. -A copy of the License is available at: - -https://license.tacticalrmm.com - -*/ - -package agent - -import ( - "os" - "syscall" -) - -func tmpNoExec() bool { - var stat syscall.Statfs_t - var noexec bool - - tmpdir := os.TempDir() - if err := syscall.Statfs(tmpdir, &stat); err == nil { - if stat.Flags&syscall.MS_NOEXEC != 0 { - noexec = true - } - } - return noexec -} diff --git a/agent/agent_unix.go b/agent/agent_unix.go index c75e4bb..42f51ee 100644 --- a/agent/agent_unix.go +++ b/agent/agent_unix.go @@ -230,11 +230,9 @@ func (a *Agent) AgentUpdate(url, inno, version string) { cwd := filepath.Dir(self) // create a tmpfile in same location as current binary // avoids issues with /tmp dir and other fs mount issues - tmpfile := filepath.Join(cwd, GenerateAgentID()) - - f, err := os.Create(tmpfile) + f, err := os.CreateTemp(cwd, "trmm") if err != nil { - a.Logger.Errorln("AgentUpdate() os.Create(tmpfile)", err) + a.Logger.Errorln("AgentUpdate() os.CreateTemp:", err) return } defer os.Remove(f.Name()) @@ -477,26 +475,6 @@ func (a *Agent) GetWMIInfo() map[string]interface{} { return wmiInfo } -func createNixTmpFile() (*os.File, error) { - var f *os.File - noexec := tmpNoExec() - f, err := os.CreateTemp("", "trmm") - if err != nil || noexec { - if noexec { - os.Remove(f.Name()) - } - cwd, err := os.Getwd() - if err != nil { - return f, err - } - f, err = os.CreateTemp(cwd, "trmm") - if err != nil { - return f, err - } - } - return f, nil -} - // windows only below TODO add into stub file func (a *Agent) GetAgentCheckInConfig(ret AgentCheckInConfig) AgentCheckInConfig { return ret diff --git a/agent/agent_windows.go b/agent/agent_windows.go index cc5da54..b19a185 100644 --- a/agent/agent_windows.go +++ b/agent/agent_windows.go @@ -906,12 +906,3 @@ func (a *Agent) GetAgentCheckInConfig(ret AgentCheckInConfig) AgentCheckInConfig func (a *Agent) NixMeshNodeID() string { return "not implemented" } - -func tmpNoExec() bool { - return false -} - -func createNixTmpFile() (*os.File, error) { - var f *os.File - return f, nil -} diff --git a/agent/utils.go b/agent/utils.go index 4d41ecb..8db3341 100644 --- a/agent/utils.go +++ b/agent/utils.go @@ -340,3 +340,28 @@ func getCMDExe() string { } return cmdExe } + +// more accurate than os.Getwd() +func getCwd() (string, error) { + self, err := os.Executable() + if err != nil { + return "", err + } + + return filepath.Dir(self), nil +} + +func createNixTmpFile() (*os.File, error) { + var f *os.File + cwd, err := getCwd() + if err != nil { + return f, err + } + + f, err = os.CreateTemp(cwd, "trmm") + if err != nil { + return f, err + } + + return f, nil +}