fallback to cwd if unable to create tmpdir in /tmp amidaware/tacticalrmm#1017

This commit is contained in:
wh1te909 2022-03-24 01:13:22 +00:00
parent 118608999c
commit c57c5a0cbf
2 changed files with 22 additions and 6 deletions

View File

@ -136,9 +136,9 @@ func (a *Agent) RunScript(code string, shell string, args []string, timeout int)
code = removeWinNewLines(code) code = removeWinNewLines(code)
content := []byte(code) content := []byte(code)
f, err := os.CreateTemp("", "trmm") f, err := createTmpFile()
if err != nil { if err != nil {
a.Logger.Errorln(err) a.Logger.Errorln("RunScript createTmpFile()", err)
return "", err.Error(), 85, err return "", err.Error(), 85, err
} }
defer os.Remove(f.Name()) defer os.Remove(f.Name())
@ -187,9 +187,9 @@ func (a *Agent) AgentUpdate(url, inno, version string) {
return return
} }
f, err := os.CreateTemp("", "") f, err := createTmpFile()
if err != nil { if err != nil {
a.Logger.Errorln("AgentUpdate()", err) a.Logger.Errorln("AgentUpdate createTmpFile()", err)
return return
} }
defer os.Remove(f.Name()) defer os.Remove(f.Name())
@ -232,9 +232,9 @@ func (a *Agent) AgentUpdate(url, inno, version string) {
} }
func (a *Agent) AgentUninstall(code string) { func (a *Agent) AgentUninstall(code string) {
f, err := os.CreateTemp("", "trmm") f, err := createTmpFile()
if err != nil { if err != nil {
a.Logger.Errorln("AgentUninstall CreateTemp:", err) a.Logger.Errorln("AgentUninstall createTmpFile():", err)
return return
} }

View File

@ -302,3 +302,19 @@ func randomCheckDelay() {
func removeWinNewLines(s string) string { func removeWinNewLines(s string) string {
return strings.ReplaceAll(s, "\r\n", "\n") 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
}