Skip to content

Commit fa30a72

Browse files
committed
Release 2.4.7
2 parents 7062e04 + 5adb986 commit fa30a72

File tree

12 files changed

+180
-145
lines changed

12 files changed

+180
-145
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jobs:
1818

1919
- uses: actions/setup-go@v4
2020
with:
21-
go-version: '1.20.3'
21+
go-version: '1.20.4'
2222

2323
- name: Ensure linux agent compiles
2424
run: |

agent/agent.go

Lines changed: 78 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -40,37 +40,39 @@ import (
4040

4141
// Agent struct
4242
type Agent struct {
43-
Hostname string
44-
Arch string
45-
AgentID string
46-
BaseURL string
47-
ApiURL string
48-
Token string
49-
AgentPK int
50-
Cert string
51-
ProgramDir string
52-
EXE string
53-
SystemDrive string
54-
MeshInstaller string
55-
MeshSystemEXE string
56-
MeshSVC string
57-
PyBin string
58-
Headers map[string]string
59-
Logger *logrus.Logger
60-
Version string
61-
Debug bool
62-
rClient *resty.Client
63-
Proxy string
64-
LogTo string
65-
LogFile *os.File
66-
Platform string
67-
GoArch string
68-
ServiceConfig *service.Config
69-
NatsServer string
70-
NatsProxyPath string
71-
NatsProxyPort string
72-
NatsPingInterval int
73-
NatsWSCompression bool
43+
Hostname string
44+
Arch string
45+
AgentID string
46+
BaseURL string
47+
ApiURL string
48+
Token string
49+
AgentPK int
50+
Cert string
51+
ProgramDir string
52+
EXE string
53+
SystemDrive string
54+
WinTmpDir string
55+
WinRunAsUserTmpDir string
56+
MeshInstaller string
57+
MeshSystemEXE string
58+
MeshSVC string
59+
PyBin string
60+
Headers map[string]string
61+
Logger *logrus.Logger
62+
Version string
63+
Debug bool
64+
rClient *resty.Client
65+
Proxy string
66+
LogTo string
67+
LogFile *os.File
68+
Platform string
69+
GoArch string
70+
ServiceConfig *service.Config
71+
NatsServer string
72+
NatsProxyPath string
73+
NatsProxyPort string
74+
NatsPingInterval int
75+
NatsWSCompression bool
7476
}
7577

7678
const (
@@ -88,7 +90,7 @@ const (
8890
defaultMacMeshSvcDir = "/usr/local/mesh_services"
8991
)
9092

91-
var winTempDir = filepath.Join(os.Getenv("PROGRAMDATA"), "TacticalRMM")
93+
var defaultWinTmpDir = filepath.Join(os.Getenv("PROGRAMDATA"), "TacticalRMM")
9294
var winMeshDir = filepath.Join(os.Getenv("PROGRAMFILES"), "Mesh Agent")
9395
var natsCheckin = []string{"agent-hello", "agent-agentinfo", "agent-disks", "agent-winsvc", "agent-publicip", "agent-wmi"}
9496
var limitNatsData = []string{"agent-winsvc", "agent-wmi"}
@@ -99,6 +101,8 @@ func New(logger *logrus.Logger, version string) *Agent {
99101
pd := filepath.Join(os.Getenv("ProgramFiles"), progFilesName)
100102
exe := filepath.Join(pd, winExeName)
101103
sd := os.Getenv("SystemDrive")
104+
winTempDir := defaultWinTmpDir
105+
winRunAsUserTmpDir := defaultWinTmpDir
102106

103107
var pybin string
104108
switch runtime.GOARCH {
@@ -130,6 +134,14 @@ func New(logger *logrus.Logger, version string) *Agent {
130134
restyC.SetRootCertificate(ac.Cert)
131135
}
132136

137+
if len(ac.WinTmpDir) > 0 {
138+
winTempDir = ac.WinTmpDir
139+
}
140+
141+
if len(ac.WinRunAsUserTmpDir) > 0 {
142+
winRunAsUserTmpDir = ac.WinRunAsUserTmpDir
143+
}
144+
133145
var MeshSysExe string
134146
switch runtime.GOOS {
135147
case "windows":
@@ -189,34 +201,36 @@ func New(logger *logrus.Logger, version string) *Agent {
189201
}
190202

191203
return &Agent{
192-
Hostname: info.Hostname,
193-
BaseURL: ac.BaseURL,
194-
AgentID: ac.AgentID,
195-
ApiURL: ac.APIURL,
196-
Token: ac.Token,
197-
AgentPK: ac.PK,
198-
Cert: ac.Cert,
199-
ProgramDir: pd,
200-
EXE: exe,
201-
SystemDrive: sd,
202-
MeshInstaller: "meshagent.exe",
203-
MeshSystemEXE: MeshSysExe,
204-
MeshSVC: meshSvcName,
205-
PyBin: pybin,
206-
Headers: headers,
207-
Logger: logger,
208-
Version: version,
209-
Debug: logger.IsLevelEnabled(logrus.DebugLevel),
210-
rClient: restyC,
211-
Proxy: ac.Proxy,
212-
Platform: runtime.GOOS,
213-
GoArch: runtime.GOARCH,
214-
ServiceConfig: svcConf,
215-
NatsServer: natsServer,
216-
NatsProxyPath: natsProxyPath,
217-
NatsProxyPort: natsProxyPort,
218-
NatsPingInterval: natsPingInterval,
219-
NatsWSCompression: natsWsCompression,
204+
Hostname: info.Hostname,
205+
BaseURL: ac.BaseURL,
206+
AgentID: ac.AgentID,
207+
ApiURL: ac.APIURL,
208+
Token: ac.Token,
209+
AgentPK: ac.PK,
210+
Cert: ac.Cert,
211+
ProgramDir: pd,
212+
EXE: exe,
213+
SystemDrive: sd,
214+
WinTmpDir: winTempDir,
215+
WinRunAsUserTmpDir: winRunAsUserTmpDir,
216+
MeshInstaller: "meshagent.exe",
217+
MeshSystemEXE: MeshSysExe,
218+
MeshSVC: meshSvcName,
219+
PyBin: pybin,
220+
Headers: headers,
221+
Logger: logger,
222+
Version: version,
223+
Debug: logger.IsLevelEnabled(logrus.DebugLevel),
224+
rClient: restyC,
225+
Proxy: ac.Proxy,
226+
Platform: runtime.GOOS,
227+
GoArch: runtime.GOARCH,
228+
ServiceConfig: svcConf,
229+
NatsServer: natsServer,
230+
NatsProxyPath: natsProxyPath,
231+
NatsProxyPort: natsProxyPort,
232+
NatsPingInterval: natsPingInterval,
233+
NatsWSCompression: natsWsCompression,
220234
}
221235
}
222236

@@ -457,7 +471,7 @@ func (a *Agent) GetUninstallExe() string {
457471

458472
func (a *Agent) CleanupAgentUpdates() {
459473
// TODO remove a.ProgramDir, updates are now in winTempDir
460-
dirs := [3]string{winTempDir, os.Getenv("TMP"), a.ProgramDir}
474+
dirs := [3]string{a.WinTmpDir, os.Getenv("TMP"), a.ProgramDir}
461475
for _, dir := range dirs {
462476
err := os.Chdir(dir)
463477
if err != nil {
@@ -491,7 +505,7 @@ func (a *Agent) CleanupAgentUpdates() {
491505

492506
func (a *Agent) RunPythonCode(code string, timeout int, args []string) (string, error) {
493507
content := []byte(code)
494-
tmpfn, _ := ioutil.TempFile(winTempDir, "*.py")
508+
tmpfn, _ := ioutil.TempFile(a.WinTmpDir, "*.py")
495509
if _, err := tmpfn.Write(content); err != nil {
496510
a.Logger.Debugln(err)
497511
return "", err
@@ -537,8 +551,8 @@ func (a *Agent) RunPythonCode(code string, timeout int, args []string) (string,
537551
}
538552

539553
func createWinTempDir() error {
540-
if !trmm.FileExists(winTempDir) {
541-
err := os.Mkdir(winTempDir, 0775)
554+
if !trmm.FileExists(defaultWinTmpDir) {
555+
err := os.Mkdir(defaultWinTmpDir, 0775)
542556
if err != nil {
543557
return err
544558
}

agent/agent_unix.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package agent
1616

1717
import (
1818
"bufio"
19+
"errors"
1920
"fmt"
2021
"os"
2122
"path/filepath"
@@ -218,12 +219,12 @@ func (a *Agent) seEnforcing() bool {
218219
return out.Status.Exit == 0 && strings.Contains(out.Stdout, "Enforcing")
219220
}
220221

221-
func (a *Agent) AgentUpdate(url, inno, version string) {
222+
func (a *Agent) AgentUpdate(url, inno, version string) error {
222223

223224
self, err := os.Executable()
224225
if err != nil {
225226
a.Logger.Errorln("AgentUpdate() os.Executable():", err)
226-
return
227+
return err
227228
}
228229

229230
// more reliable method to get current working directory than os.Getwd()
@@ -233,7 +234,7 @@ func (a *Agent) AgentUpdate(url, inno, version string) {
233234
f, err := os.CreateTemp(cwd, "trmm")
234235
if err != nil {
235236
a.Logger.Errorln("AgentUpdate() os.CreateTemp:", err)
236-
return
237+
return err
237238
}
238239
defer os.Remove(f.Name())
239240

@@ -252,20 +253,20 @@ func (a *Agent) AgentUpdate(url, inno, version string) {
252253
if err != nil {
253254
a.Logger.Errorln("AgentUpdate() download:", err)
254255
f.Close()
255-
return
256+
return err
256257
}
257258
if r.IsError() {
258259
a.Logger.Errorln("AgentUpdate() status code:", r.StatusCode())
259260
f.Close()
260-
return
261+
return errors.New("err")
261262
}
262263

263264
f.Close()
264265
os.Chmod(f.Name(), 0755)
265266
err = os.Rename(f.Name(), self)
266267
if err != nil {
267268
a.Logger.Errorln("AgentUpdate() os.Rename():", err)
268-
return
269+
return err
269270
}
270271

271272
if runtime.GOOS == "linux" && a.seEnforcing() {
@@ -283,10 +284,11 @@ func (a *Agent) AgentUpdate(url, inno, version string) {
283284
case "darwin":
284285
opts.Command = "launchctl kickstart -k system/tacticalagent"
285286
default:
286-
return
287+
return nil
287288
}
288289

289290
a.CmdV2(opts)
291+
return nil
290292
}
291293

292294
func (a *Agent) AgentUninstall(code string) {

agent/agent_windows.go

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -62,26 +62,30 @@ func NewAgentConfig() *rmm.AgentConfig {
6262
cert, _, _ := k.GetStringValue("Cert")
6363
proxy, _, _ := k.GetStringValue("Proxy")
6464
customMeshDir, _, _ := k.GetStringValue("MeshDir")
65+
winTmpDir, _, _ := k.GetStringValue("WinTmpDir")
66+
winRunAsUserTmpDir, _, _ := k.GetStringValue("WinRunAsUserTmpDir")
6567
natsProxyPath, _, _ := k.GetStringValue("NatsProxyPath")
6668
natsProxyPort, _, _ := k.GetStringValue("NatsProxyPort")
6769
natsStandardPort, _, _ := k.GetStringValue("NatsStandardPort")
6870
natsPingInterval, _, _ := k.GetStringValue("NatsPingInterval")
6971
npi, _ := strconv.Atoi(natsPingInterval)
7072

7173
return &rmm.AgentConfig{
72-
BaseURL: baseurl,
73-
AgentID: agentid,
74-
APIURL: apiurl,
75-
Token: token,
76-
AgentPK: agentpk,
77-
PK: pk,
78-
Cert: cert,
79-
Proxy: proxy,
80-
CustomMeshDir: customMeshDir,
81-
NatsProxyPath: natsProxyPath,
82-
NatsProxyPort: natsProxyPort,
83-
NatsStandardPort: natsStandardPort,
84-
NatsPingInterval: npi,
74+
BaseURL: baseurl,
75+
AgentID: agentid,
76+
APIURL: apiurl,
77+
Token: token,
78+
AgentPK: agentpk,
79+
PK: pk,
80+
Cert: cert,
81+
Proxy: proxy,
82+
CustomMeshDir: customMeshDir,
83+
WinTmpDir: winTmpDir,
84+
WinRunAsUserTmpDir: winRunAsUserTmpDir,
85+
NatsProxyPath: natsProxyPath,
86+
NatsProxyPort: natsProxyPort,
87+
NatsStandardPort: natsStandardPort,
88+
NatsPingInterval: npi,
8589
}
8690
}
8791

@@ -114,7 +118,13 @@ func (a *Agent) RunScript(code string, shell string, args []string, timeout int,
114118
ext = "*.bat"
115119
}
116120

117-
tmpfn, err := ioutil.TempFile(winTempDir, ext)
121+
tmpDir := a.WinTmpDir
122+
123+
if runasuser {
124+
tmpDir = a.WinRunAsUserTmpDir
125+
}
126+
127+
tmpfn, err := ioutil.TempFile(tmpDir, ext)
118128
if err != nil {
119129
a.Logger.Errorln(err)
120130
return "", err.Error(), 85, err
@@ -133,7 +143,7 @@ func (a *Agent) RunScript(code string, shell string, args []string, timeout int,
133143
switch shell {
134144
case "powershell":
135145
exe = getPowershellExe()
136-
cmdArgs = []string{"-NonInteractive", "-NoProfile", "-ExecutionPolicy", "Bypass", tmpfn.Name()}
146+
cmdArgs = []string{"-NonInteractive", "-NoProfile", "-ExecutionPolicy", "Bypass", "-File", tmpfn.Name()}
137147
case "python":
138148
exe = a.PyBin
139149
cmdArgs = []string{tmpfn.Name()}
@@ -587,15 +597,16 @@ func (a *Agent) UninstallCleanup() {
587597
a.PatchMgmnt(false)
588598
a.CleanupAgentUpdates()
589599
CleanupSchedTasks()
590-
os.RemoveAll(winTempDir)
600+
os.RemoveAll(a.WinTmpDir)
601+
os.RemoveAll(a.WinRunAsUserTmpDir)
591602
}
592603

593-
func (a *Agent) AgentUpdate(url, inno, version string) {
604+
func (a *Agent) AgentUpdate(url, inno, version string) error {
594605
time.Sleep(time.Duration(randRange(1, 15)) * time.Second)
595606
a.KillHungUpdates()
596607
time.Sleep(1 * time.Second)
597608
a.CleanupAgentUpdates()
598-
updater := filepath.Join(winTempDir, inno)
609+
updater := filepath.Join(a.WinTmpDir, inno)
599610
a.Logger.Infof("Agent updating from %s to %s", a.Version, version)
600611
a.Logger.Debugln("Downloading agent update from", url)
601612

@@ -609,16 +620,14 @@ func (a *Agent) AgentUpdate(url, inno, version string) {
609620
r, err := rClient.R().SetOutput(updater).Get(url)
610621
if err != nil {
611622
a.Logger.Errorln(err)
612-
CMD("net", []string{"start", winSvcName}, 10, false)
613-
return
623+
return err
614624
}
615625
if r.IsError() {
616626
a.Logger.Errorln("Download failed with status code", r.StatusCode())
617-
CMD("net", []string{"start", winSvcName}, 10, false)
618-
return
627+
return err
619628
}
620629

621-
innoLogFile := filepath.Join(winTempDir, fmt.Sprintf("tacticalagent_update_v%s.txt", version))
630+
innoLogFile := filepath.Join(a.WinTmpDir, fmt.Sprintf("tacticalagent_update_v%s.txt", version))
622631

623632
args := []string{"/C", updater, "/VERYSILENT", fmt.Sprintf("/LOG=%s", innoLogFile)}
624633
cmd := exec.Command("cmd.exe", args...)
@@ -627,6 +636,7 @@ func (a *Agent) AgentUpdate(url, inno, version string) {
627636
}
628637
cmd.Start()
629638
time.Sleep(1 * time.Second)
639+
return nil
630640
}
631641

632642
func (a *Agent) osString() string {

0 commit comments

Comments
 (0)