From c57c5a0cbfba57f2cbb4d6bacac5c52828fa958d Mon Sep 17 00:00:00 2001 From: wh1te909 Date: Thu, 24 Mar 2022 01:13:22 +0000 Subject: [PATCH] fallback to cwd if unable to create tmpdir in /tmp amidaware/tacticalrmm#1017 --- agent/agent_linux.go | 12 ++++++------ agent/utils.go | 16 ++++++++++++++++ 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/agent/agent_linux.go b/agent/agent_linux.go index 0b0434a..11bed39 100644 --- a/agent/agent_linux.go +++ b/agent/agent_linux.go @@ -136,9 +136,9 @@ func (a *Agent) RunScript(code string, shell string, args []string, timeout int) code = removeWinNewLines(code) content := []byte(code) - f, err := os.CreateTemp("", "trmm") + f, err := createTmpFile() if err != nil { - a.Logger.Errorln(err) + a.Logger.Errorln("RunScript createTmpFile()", err) return "", err.Error(), 85, err } defer os.Remove(f.Name()) @@ -187,9 +187,9 @@ func (a *Agent) AgentUpdate(url, inno, version string) { return } - f, err := os.CreateTemp("", "") + f, err := createTmpFile() if err != nil { - a.Logger.Errorln("AgentUpdate()", err) + a.Logger.Errorln("AgentUpdate createTmpFile()", err) return } defer os.Remove(f.Name()) @@ -232,9 +232,9 @@ func (a *Agent) AgentUpdate(url, inno, version string) { } func (a *Agent) AgentUninstall(code string) { - f, err := os.CreateTemp("", "trmm") + f, err := createTmpFile() if err != nil { - a.Logger.Errorln("AgentUninstall CreateTemp:", err) + a.Logger.Errorln("AgentUninstall createTmpFile():", err) return } diff --git a/agent/utils.go b/agent/utils.go index e4baebc..6d9a851 100644 --- a/agent/utils.go +++ b/agent/utils.go @@ -302,3 +302,19 @@ func randomCheckDelay() { func removeWinNewLines(s string) string { return strings.ReplaceAll(s, "\r\n", "\n") } + +func createTmpFile() (*os.File, error) { + var f *os.File + f, err := os.CreateTemp("", "trmm") + if err != nil { + 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 +}