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"
)
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"}
func New(logger *logrus.Logger, version string) *Agent {
@ -402,50 +404,42 @@ func (a *Agent) GetUninstallExe() string {
}
func (a *Agent) CleanupAgentUpdates() {
cderr := os.Chdir(a.ProgramDir)
if cderr != nil {
a.Logger.Errorln(cderr)
return
}
// TODO remove a.ProgramDir, updates are now in winTempDir
dirs := [2]string{winTempDir, a.ProgramDir}
for _, dir := range dirs {
err := os.Chdir(dir)
if err != nil {
a.Logger.Debugln("CleanupAgentUpdates()", dir, err)
continue
}
// winagent-v* is deprecated
files, err := filepath.Glob("winagent-v*.exe")
if err == nil {
for _, f := range files {
os.Remove(f)
// TODO winagent-v* is deprecated
globs := [2]string{"tacticalagent-v*", "winagent-v*"}
for _, glob := range globs {
files, err := filepath.Glob(glob)
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 {
for _, f := range agents {
os.Remove(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)
dirs, err := filepath.Glob("tacticalrmm*")
if err == nil {
for _, f := range dirs {
os.RemoveAll(f)
}
}
}
}
func (a *Agent) RunPythonCode(code string, timeout int, args []string) (string, error) {
content := []byte(code)
dir, err := ioutil.TempDir("", "tacticalpy")
if err != nil {
a.Logger.Debugln(err)
return "", err
}
defer os.RemoveAll(dir)
tmpfn, _ := ioutil.TempFile(dir, "*.py")
tmpfn, _ := ioutil.TempFile(winTempDir, "*.py")
if _, err := tmpfn.Write(content); err != nil {
a.Logger.Debugln(err)
return "", err
@ -489,13 +483,12 @@ func (a *Agent) RunPythonCode(code string, timeout int, args []string) (string,
}
func (a *Agent) CreateTRMMTempDir() {
// create the temp dir for running scripts
dir := filepath.Join(os.TempDir(), "trmm")
if !trmm.FileExists(dir) {
err := os.Mkdir(dir, 0775)
func createWinTempDir() error {
if !trmm.FileExists(winTempDir) {
err := os.Mkdir(winTempDir, 0775)
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)
dir := filepath.Join(os.TempDir(), "trmm")
if !trmm.FileExists(dir) {
a.CreateTRMMTempDir()
err := createWinTempDir()
if err != nil {
a.Logger.Errorln(err)
return "", err.Error(), 85, err
}
const defaultExitCode = 1
@ -110,7 +111,7 @@ func (a *Agent) RunScript(code string, shell string, args []string, timeout int,
ext = "*.bat"
}
tmpfn, err := ioutil.TempFile(dir, ext)
tmpfn, err := ioutil.TempFile(winTempDir, ext)
if err != nil {
a.Logger.Errorln(err)
return "", err.Error(), 85, err
@ -581,8 +582,9 @@ func (a *Agent) UninstallCleanup() {
func (a *Agent) AgentUpdate(url, inno, version string) {
time.Sleep(time.Duration(randRange(1, 15)) * time.Second)
a.KillHungUpdates()
time.Sleep(1 * time.Second)
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.Debugln("Downloading agent update from", url)
@ -605,14 +607,7 @@ func (a *Agent) AgentUpdate(url, inno, version string) {
return
}
dir, err := ioutil.TempDir("", "tacticalrmm")
if err != nil {
a.Logger.Errorln("Agentupdate create tempdir:", err)
CMD("net", []string{"start", winSvcName}, 10, false)
return
}
innoLogFile := filepath.Join(dir, "tacticalrmm.txt")
innoLogFile := filepath.Join(winTempDir, fmt.Sprintf("tacticalagent_update_v%s.txt", version))
args := []string{"/C", updater, "/VERYSILENT", fmt.Sprintf("/LOG=%s", innoLogFile)}
cmd := exec.Command("cmd.exe", args...)
@ -658,12 +653,10 @@ func (a *Agent) AgentUninstall(code string) {
}
func (a *Agent) addDefenderExlusions() {
code := `
Add-MpPreference -ExclusionPath 'C:\Program Files\TacticalAgent\*'
Add-MpPreference -ExclusionPath 'C:\Windows\Temp\tacticalagent-v*.exe'
Add-MpPreference -ExclusionPath 'C:\Windows\Temp\trmm\*'
Add-MpPreference -ExclusionPath 'C:\Program Files\Mesh Agent\*'
`
code := fmt.Sprintf(`
Add-MpPreference -ExclusionPath '%s\*'
Add-MpPreference -ExclusionPath '%s\*'
`, winTempDir, winMeshDir)
_, _, _, err := a.RunScript(code, "powershell", []string{}, 20, false)
if err != nil {
a.Logger.Debugln(err)

View File

@ -224,13 +224,16 @@ func (a *Agent) Install(i *Installer) {
a.SendSoftware()
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.PatchMgmnt(true)
a.Logger.Infoln("Installing service...")
err := a.InstallService()
err = a.InstallService()
if err != nil {
a.installerMsg(err.Error(), "error", i.Silent)
}

View File

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