standardize windows temp dir fixes amidaware/tacticalrmm#1238

This commit is contained in:
wh1te909 2022-08-09 10:33:30 -07:00
parent 381f9696eb
commit 41597d7d26
4 changed files with 53 additions and 61 deletions

View File

@ -78,6 +78,8 @@ const (
meshSvcName = "mesh agent" meshSvcName = "mesh agent"
) )
var winTempDir = filepath.Join(os.Getenv("PROGRAMDATA"), "TacticalRMM")
var winMeshDir = filepath.Join(os.Getenv("PROGRAMFILES"), "Mesh Agent")
var natsCheckin = []string{"agent-hello", "agent-agentinfo", "agent-disks", "agent-winsvc", "agent-publicip", "agent-wmi"} var natsCheckin = []string{"agent-hello", "agent-agentinfo", "agent-disks", "agent-winsvc", "agent-publicip", "agent-wmi"}
func New(logger *logrus.Logger, version string) *Agent { func New(logger *logrus.Logger, version string) *Agent {
@ -402,50 +404,42 @@ func (a *Agent) GetUninstallExe() string {
} }
func (a *Agent) CleanupAgentUpdates() { func (a *Agent) CleanupAgentUpdates() {
cderr := os.Chdir(a.ProgramDir) // TODO remove a.ProgramDir, updates are now in winTempDir
if cderr != nil { dirs := [2]string{winTempDir, a.ProgramDir}
a.Logger.Errorln(cderr) for _, dir := range dirs {
return err := os.Chdir(dir)
} if err != nil {
a.Logger.Debugln("CleanupAgentUpdates()", dir, err)
continue
}
// winagent-v* is deprecated // TODO winagent-v* is deprecated
files, err := filepath.Glob("winagent-v*.exe") globs := [2]string{"tacticalagent-v*", "winagent-v*"}
if err == nil { for _, glob := range globs {
for _, f := range files { files, err := filepath.Glob(glob)
os.Remove(f) if err == nil {
for _, f := range files {
a.Logger.Debugln("CleanupAgentUpdates() Removing file:", f)
os.Remove(f)
}
}
} }
} }
agents, err := filepath.Glob("tacticalagent-v*.exe") err := os.Chdir(os.Getenv("TMP"))
if err == nil { if err == nil {
for _, f := range agents { dirs, err := filepath.Glob("tacticalrmm*")
os.Remove(f) if err == nil {
} for _, f := range dirs {
} os.RemoveAll(f)
}
cderr = os.Chdir(os.Getenv("TMP"))
if cderr != nil {
a.Logger.Errorln(cderr)
return
}
folders, err := filepath.Glob("tacticalrmm*")
if err == nil {
for _, f := range folders {
os.RemoveAll(f)
} }
} }
} }
func (a *Agent) RunPythonCode(code string, timeout int, args []string) (string, error) { func (a *Agent) RunPythonCode(code string, timeout int, args []string) (string, error) {
content := []byte(code) content := []byte(code)
dir, err := ioutil.TempDir("", "tacticalpy") tmpfn, _ := ioutil.TempFile(winTempDir, "*.py")
if err != nil {
a.Logger.Debugln(err)
return "", err
}
defer os.RemoveAll(dir)
tmpfn, _ := ioutil.TempFile(dir, "*.py")
if _, err := tmpfn.Write(content); err != nil { if _, err := tmpfn.Write(content); err != nil {
a.Logger.Debugln(err) a.Logger.Debugln(err)
return "", err return "", err
@ -489,13 +483,12 @@ func (a *Agent) RunPythonCode(code string, timeout int, args []string) (string,
} }
func (a *Agent) CreateTRMMTempDir() { func createWinTempDir() error {
// create the temp dir for running scripts if !trmm.FileExists(winTempDir) {
dir := filepath.Join(os.TempDir(), "trmm") err := os.Mkdir(winTempDir, 0775)
if !trmm.FileExists(dir) {
err := os.Mkdir(dir, 0775)
if err != nil { if err != nil {
a.Logger.Errorln(err) return err
} }
} }
return nil
} }

View File

@ -86,9 +86,10 @@ func (a *Agent) RunScript(code string, shell string, args []string, timeout int,
content := []byte(code) content := []byte(code)
dir := filepath.Join(os.TempDir(), "trmm") err := createWinTempDir()
if !trmm.FileExists(dir) { if err != nil {
a.CreateTRMMTempDir() a.Logger.Errorln(err)
return "", err.Error(), 85, err
} }
const defaultExitCode = 1 const defaultExitCode = 1
@ -110,7 +111,7 @@ func (a *Agent) RunScript(code string, shell string, args []string, timeout int,
ext = "*.bat" ext = "*.bat"
} }
tmpfn, err := ioutil.TempFile(dir, ext) tmpfn, err := ioutil.TempFile(winTempDir, ext)
if err != nil { if err != nil {
a.Logger.Errorln(err) a.Logger.Errorln(err)
return "", err.Error(), 85, err return "", err.Error(), 85, err
@ -581,8 +582,9 @@ func (a *Agent) UninstallCleanup() {
func (a *Agent) AgentUpdate(url, inno, version string) { func (a *Agent) AgentUpdate(url, inno, version string) {
time.Sleep(time.Duration(randRange(1, 15)) * time.Second) time.Sleep(time.Duration(randRange(1, 15)) * time.Second)
a.KillHungUpdates() a.KillHungUpdates()
time.Sleep(1 * time.Second)
a.CleanupAgentUpdates() a.CleanupAgentUpdates()
updater := filepath.Join(a.ProgramDir, inno) updater := filepath.Join(winTempDir, inno)
a.Logger.Infof("Agent updating from %s to %s", a.Version, version) a.Logger.Infof("Agent updating from %s to %s", a.Version, version)
a.Logger.Debugln("Downloading agent update from", url) a.Logger.Debugln("Downloading agent update from", url)
@ -605,14 +607,7 @@ func (a *Agent) AgentUpdate(url, inno, version string) {
return return
} }
dir, err := ioutil.TempDir("", "tacticalrmm") innoLogFile := filepath.Join(winTempDir, fmt.Sprintf("tacticalagent_update_v%s.txt", version))
if err != nil {
a.Logger.Errorln("Agentupdate create tempdir:", err)
CMD("net", []string{"start", winSvcName}, 10, false)
return
}
innoLogFile := filepath.Join(dir, "tacticalrmm.txt")
args := []string{"/C", updater, "/VERYSILENT", fmt.Sprintf("/LOG=%s", innoLogFile)} args := []string{"/C", updater, "/VERYSILENT", fmt.Sprintf("/LOG=%s", innoLogFile)}
cmd := exec.Command("cmd.exe", args...) cmd := exec.Command("cmd.exe", args...)
@ -658,12 +653,10 @@ func (a *Agent) AgentUninstall(code string) {
} }
func (a *Agent) addDefenderExlusions() { func (a *Agent) addDefenderExlusions() {
code := ` code := fmt.Sprintf(`
Add-MpPreference -ExclusionPath 'C:\Program Files\TacticalAgent\*' Add-MpPreference -ExclusionPath '%s\*'
Add-MpPreference -ExclusionPath 'C:\Windows\Temp\tacticalagent-v*.exe' Add-MpPreference -ExclusionPath '%s\*'
Add-MpPreference -ExclusionPath 'C:\Windows\Temp\trmm\*' `, winTempDir, winMeshDir)
Add-MpPreference -ExclusionPath 'C:\Program Files\Mesh Agent\*'
`
_, _, _, err := a.RunScript(code, "powershell", []string{}, 20, false) _, _, _, err := a.RunScript(code, "powershell", []string{}, 20, false)
if err != nil { if err != nil {
a.Logger.Debugln(err) a.Logger.Debugln(err)

View File

@ -224,13 +224,16 @@ func (a *Agent) Install(i *Installer) {
a.SendSoftware() a.SendSoftware()
a.Logger.Debugln("Creating temp dir") a.Logger.Debugln("Creating temp dir")
a.CreateTRMMTempDir() err := createWinTempDir()
if err != nil {
a.Logger.Errorln("Install() createWinTempDir():", err)
}
a.Logger.Debugln("Disabling automatic windows updates") a.Logger.Debugln("Disabling automatic windows updates")
a.PatchMgmnt(true) a.PatchMgmnt(true)
a.Logger.Infoln("Installing service...") a.Logger.Infoln("Installing service...")
err := a.InstallService() err = a.InstallService()
if err != nil { if err != nil {
a.installerMsg(err.Error(), "error", i.Silent) a.installerMsg(err.Error(), "error", i.Silent)
} }

View File

@ -29,7 +29,10 @@ func (a *Agent) RunAsService() {
func (a *Agent) AgentSvc() { func (a *Agent) AgentSvc() {
go a.GetPython(false) go a.GetPython(false)
a.CreateTRMMTempDir() err := createWinTempDir()
if err != nil {
a.Logger.Errorln("AgentSvc() createWinTempDir():", err)
}
a.RunMigrations() a.RunMigrations()
sleepDelay := randRange(14, 22) sleepDelay := randRange(14, 22)