allow setting custom defined tmpdir

This commit is contained in:
wh1te909 2023-04-29 15:30:23 -07:00
parent ec49d4941d
commit 588a4bcbf7
3 changed files with 122 additions and 95 deletions

View File

@ -40,37 +40,39 @@ import (
// Agent struct // Agent struct
type Agent struct { type Agent struct {
Hostname string Hostname string
Arch string Arch string
AgentID string AgentID string
BaseURL string BaseURL string
ApiURL string ApiURL string
Token string Token string
AgentPK int AgentPK int
Cert string Cert string
ProgramDir string ProgramDir string
EXE string EXE string
SystemDrive string SystemDrive string
MeshInstaller string WinTmpDir string
MeshSystemEXE string WinRunAsUserTmpDir string
MeshSVC string MeshInstaller string
PyBin string MeshSystemEXE string
Headers map[string]string MeshSVC string
Logger *logrus.Logger PyBin string
Version string Headers map[string]string
Debug bool Logger *logrus.Logger
rClient *resty.Client Version string
Proxy string Debug bool
LogTo string rClient *resty.Client
LogFile *os.File Proxy string
Platform string LogTo string
GoArch string LogFile *os.File
ServiceConfig *service.Config Platform string
NatsServer string GoArch string
NatsProxyPath string ServiceConfig *service.Config
NatsProxyPort string NatsServer string
NatsPingInterval int NatsProxyPath string
NatsWSCompression bool NatsProxyPort string
NatsPingInterval int
NatsWSCompression bool
} }
const ( const (
@ -88,7 +90,7 @@ const (
defaultMacMeshSvcDir = "/usr/local/mesh_services" defaultMacMeshSvcDir = "/usr/local/mesh_services"
) )
var winTempDir = filepath.Join(os.Getenv("PROGRAMDATA"), "TacticalRMM") var defaultWinTmpDir = filepath.Join(os.Getenv("PROGRAMDATA"), "TacticalRMM")
var winMeshDir = filepath.Join(os.Getenv("PROGRAMFILES"), "Mesh Agent") 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"}
var limitNatsData = []string{"agent-winsvc", "agent-wmi"} var limitNatsData = []string{"agent-winsvc", "agent-wmi"}
@ -99,6 +101,8 @@ func New(logger *logrus.Logger, version string) *Agent {
pd := filepath.Join(os.Getenv("ProgramFiles"), progFilesName) pd := filepath.Join(os.Getenv("ProgramFiles"), progFilesName)
exe := filepath.Join(pd, winExeName) exe := filepath.Join(pd, winExeName)
sd := os.Getenv("SystemDrive") sd := os.Getenv("SystemDrive")
winTempDir := defaultWinTmpDir
winRunAsUserTmpDir := defaultWinTmpDir
var pybin string var pybin string
switch runtime.GOARCH { switch runtime.GOARCH {
@ -130,6 +134,14 @@ func New(logger *logrus.Logger, version string) *Agent {
restyC.SetRootCertificate(ac.Cert) restyC.SetRootCertificate(ac.Cert)
} }
if len(ac.WinTmpDir) > 0 {
winTempDir = ac.WinTmpDir
}
if len(ac.WinRunAsUserTmpDir) > 0 {
winRunAsUserTmpDir = ac.WinRunAsUserTmpDir
}
var MeshSysExe string var MeshSysExe string
switch runtime.GOOS { switch runtime.GOOS {
case "windows": case "windows":
@ -189,34 +201,36 @@ func New(logger *logrus.Logger, version string) *Agent {
} }
return &Agent{ return &Agent{
Hostname: info.Hostname, Hostname: info.Hostname,
BaseURL: ac.BaseURL, BaseURL: ac.BaseURL,
AgentID: ac.AgentID, AgentID: ac.AgentID,
ApiURL: ac.APIURL, ApiURL: ac.APIURL,
Token: ac.Token, Token: ac.Token,
AgentPK: ac.PK, AgentPK: ac.PK,
Cert: ac.Cert, Cert: ac.Cert,
ProgramDir: pd, ProgramDir: pd,
EXE: exe, EXE: exe,
SystemDrive: sd, SystemDrive: sd,
MeshInstaller: "meshagent.exe", WinTmpDir: winTempDir,
MeshSystemEXE: MeshSysExe, WinRunAsUserTmpDir: winRunAsUserTmpDir,
MeshSVC: meshSvcName, MeshInstaller: "meshagent.exe",
PyBin: pybin, MeshSystemEXE: MeshSysExe,
Headers: headers, MeshSVC: meshSvcName,
Logger: logger, PyBin: pybin,
Version: version, Headers: headers,
Debug: logger.IsLevelEnabled(logrus.DebugLevel), Logger: logger,
rClient: restyC, Version: version,
Proxy: ac.Proxy, Debug: logger.IsLevelEnabled(logrus.DebugLevel),
Platform: runtime.GOOS, rClient: restyC,
GoArch: runtime.GOARCH, Proxy: ac.Proxy,
ServiceConfig: svcConf, Platform: runtime.GOOS,
NatsServer: natsServer, GoArch: runtime.GOARCH,
NatsProxyPath: natsProxyPath, ServiceConfig: svcConf,
NatsProxyPort: natsProxyPort, NatsServer: natsServer,
NatsPingInterval: natsPingInterval, NatsProxyPath: natsProxyPath,
NatsWSCompression: natsWsCompression, NatsProxyPort: natsProxyPort,
NatsPingInterval: natsPingInterval,
NatsWSCompression: natsWsCompression,
} }
} }
@ -457,7 +471,7 @@ func (a *Agent) GetUninstallExe() string {
func (a *Agent) CleanupAgentUpdates() { func (a *Agent) CleanupAgentUpdates() {
// TODO remove a.ProgramDir, updates are now in winTempDir // TODO remove a.ProgramDir, updates are now in winTempDir
dirs := [3]string{winTempDir, os.Getenv("TMP"), a.ProgramDir} dirs := [3]string{a.WinTmpDir, os.Getenv("TMP"), a.ProgramDir}
for _, dir := range dirs { for _, dir := range dirs {
err := os.Chdir(dir) err := os.Chdir(dir)
if err != nil { if err != nil {
@ -491,7 +505,7 @@ func (a *Agent) CleanupAgentUpdates() {
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)
tmpfn, _ := ioutil.TempFile(winTempDir, "*.py") tmpfn, _ := ioutil.TempFile(a.WinTmpDir, "*.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
@ -537,8 +551,8 @@ func (a *Agent) RunPythonCode(code string, timeout int, args []string) (string,
} }
func createWinTempDir() error { func createWinTempDir() error {
if !trmm.FileExists(winTempDir) { if !trmm.FileExists(defaultWinTmpDir) {
err := os.Mkdir(winTempDir, 0775) err := os.Mkdir(defaultWinTmpDir, 0775)
if err != nil { if err != nil {
return err return err
} }

View File

@ -62,6 +62,8 @@ func NewAgentConfig() *rmm.AgentConfig {
cert, _, _ := k.GetStringValue("Cert") cert, _, _ := k.GetStringValue("Cert")
proxy, _, _ := k.GetStringValue("Proxy") proxy, _, _ := k.GetStringValue("Proxy")
customMeshDir, _, _ := k.GetStringValue("MeshDir") customMeshDir, _, _ := k.GetStringValue("MeshDir")
winTmpDir, _, _ := k.GetStringValue("WinTmpDir")
winRunAsUserTmpDir, _, _ := k.GetStringValue("WinRunAsUserTmpDir")
natsProxyPath, _, _ := k.GetStringValue("NatsProxyPath") natsProxyPath, _, _ := k.GetStringValue("NatsProxyPath")
natsProxyPort, _, _ := k.GetStringValue("NatsProxyPort") natsProxyPort, _, _ := k.GetStringValue("NatsProxyPort")
natsStandardPort, _, _ := k.GetStringValue("NatsStandardPort") natsStandardPort, _, _ := k.GetStringValue("NatsStandardPort")
@ -69,19 +71,21 @@ func NewAgentConfig() *rmm.AgentConfig {
npi, _ := strconv.Atoi(natsPingInterval) npi, _ := strconv.Atoi(natsPingInterval)
return &rmm.AgentConfig{ return &rmm.AgentConfig{
BaseURL: baseurl, BaseURL: baseurl,
AgentID: agentid, AgentID: agentid,
APIURL: apiurl, APIURL: apiurl,
Token: token, Token: token,
AgentPK: agentpk, AgentPK: agentpk,
PK: pk, PK: pk,
Cert: cert, Cert: cert,
Proxy: proxy, Proxy: proxy,
CustomMeshDir: customMeshDir, CustomMeshDir: customMeshDir,
NatsProxyPath: natsProxyPath, WinTmpDir: winTmpDir,
NatsProxyPort: natsProxyPort, WinRunAsUserTmpDir: winRunAsUserTmpDir,
NatsStandardPort: natsStandardPort, NatsProxyPath: natsProxyPath,
NatsPingInterval: npi, NatsProxyPort: natsProxyPort,
NatsStandardPort: natsStandardPort,
NatsPingInterval: npi,
} }
} }
@ -114,7 +118,13 @@ func (a *Agent) RunScript(code string, shell string, args []string, timeout int,
ext = "*.bat" ext = "*.bat"
} }
tmpfn, err := ioutil.TempFile(winTempDir, ext) tmpDir := a.WinTmpDir
if runasuser {
tmpDir = a.WinRunAsUserTmpDir
}
tmpfn, err := ioutil.TempFile(tmpDir, 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
@ -133,7 +143,7 @@ func (a *Agent) RunScript(code string, shell string, args []string, timeout int,
switch shell { switch shell {
case "powershell": case "powershell":
exe = getPowershellExe() exe = getPowershellExe()
cmdArgs = []string{"-NonInteractive", "-NoProfile", "-ExecutionPolicy", "Bypass", tmpfn.Name()} cmdArgs = []string{"-NonInteractive", "-NoProfile", "-ExecutionPolicy", "Bypass", "-File", tmpfn.Name()}
case "python": case "python":
exe = a.PyBin exe = a.PyBin
cmdArgs = []string{tmpfn.Name()} cmdArgs = []string{tmpfn.Name()}
@ -587,7 +597,8 @@ func (a *Agent) UninstallCleanup() {
a.PatchMgmnt(false) a.PatchMgmnt(false)
a.CleanupAgentUpdates() a.CleanupAgentUpdates()
CleanupSchedTasks() CleanupSchedTasks()
os.RemoveAll(winTempDir) os.RemoveAll(a.WinTmpDir)
os.RemoveAll(a.WinRunAsUserTmpDir)
} }
func (a *Agent) AgentUpdate(url, inno, version string) { func (a *Agent) AgentUpdate(url, inno, version string) {
@ -595,7 +606,7 @@ func (a *Agent) AgentUpdate(url, inno, version string) {
a.KillHungUpdates() a.KillHungUpdates()
time.Sleep(1 * time.Second) time.Sleep(1 * time.Second)
a.CleanupAgentUpdates() a.CleanupAgentUpdates()
updater := filepath.Join(winTempDir, inno) updater := filepath.Join(a.WinTmpDir, 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)
@ -618,7 +629,7 @@ func (a *Agent) AgentUpdate(url, inno, version string) {
return return
} }
innoLogFile := filepath.Join(winTempDir, fmt.Sprintf("tacticalagent_update_v%s.txt", version)) innoLogFile := filepath.Join(a.WinTmpDir, fmt.Sprintf("tacticalagent_update_v%s.txt", version))
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...)

View File

@ -33,19 +33,21 @@ type ProcessMsg struct {
} }
type AgentConfig struct { type AgentConfig struct {
BaseURL string BaseURL string
AgentID string AgentID string
APIURL string APIURL string
Token string Token string
AgentPK string AgentPK string
PK int PK int
Cert string Cert string
Proxy string Proxy string
CustomMeshDir string CustomMeshDir string
NatsProxyPath string WinTmpDir string
NatsProxyPort string WinRunAsUserTmpDir string
NatsStandardPort string NatsProxyPath string
NatsPingInterval int NatsProxyPort string
NatsStandardPort string
NatsPingInterval int
} }
type RunScriptResp struct { type RunScriptResp struct {